blob: 9271e5209ac1a310521d47e7213e9d6822b42547 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * scan.c - support for transforming the ACPI namespace into individual objects
3 */
4
5#include <linux/module.h>
6#include <linux/init.h>
7#include <linux/acpi.h>
8
9#include <acpi/acpi_drivers.h>
10#include <acpi/acinterp.h> /* for acpi_ex_eisa_id_to_string() */
11
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#define _COMPONENT ACPI_BUS_COMPONENT
Len Brown4be44fc2005-08-05 00:44:28 -040013ACPI_MODULE_NAME("scan")
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#define STRUCT_TO_INT(s) (*((int*)&s))
Len Brown4be44fc2005-08-05 00:44:28 -040015extern struct acpi_device *acpi_root;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016
17#define ACPI_BUS_CLASS "system_bus"
18#define ACPI_BUS_HID "ACPI_BUS"
19#define ACPI_BUS_DRIVER_NAME "ACPI Bus Driver"
20#define ACPI_BUS_DEVICE_NAME "System Bus"
21
22static LIST_HEAD(acpi_device_list);
23DEFINE_SPINLOCK(acpi_device_lock);
24LIST_HEAD(acpi_wakeup_device_list);
25
Len Brown4be44fc2005-08-05 00:44:28 -040026static int acpi_bus_trim(struct acpi_device *start, int rmdevice);
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
Len Brown4be44fc2005-08-05 00:44:28 -040028static void acpi_device_release(struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -070029{
Len Brown4be44fc2005-08-05 00:44:28 -040030 struct acpi_device *dev = container_of(kobj, struct acpi_device, kobj);
Jesper Juhl6044ec82005-11-07 01:01:32 -080031 kfree(dev->pnp.cid_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070032 kfree(dev);
33}
34
35struct acpi_device_attribute {
36 struct attribute attr;
Len Brown4be44fc2005-08-05 00:44:28 -040037 ssize_t(*show) (struct acpi_device *, char *);
38 ssize_t(*store) (struct acpi_device *, const char *, size_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -070039};
40
41typedef void acpi_device_sysfs_files(struct kobject *,
Len Brown4be44fc2005-08-05 00:44:28 -040042 const struct attribute *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
44static void setup_sys_fs_device_files(struct acpi_device *dev,
Len Brown4be44fc2005-08-05 00:44:28 -040045 acpi_device_sysfs_files * func);
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
47#define create_sysfs_device_files(dev) \
48 setup_sys_fs_device_files(dev, (acpi_device_sysfs_files *)&sysfs_create_file)
49#define remove_sysfs_device_files(dev) \
50 setup_sys_fs_device_files(dev, (acpi_device_sysfs_files *)&sysfs_remove_file)
51
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#define to_acpi_device(n) container_of(n, struct acpi_device, kobj)
53#define to_handle_attr(n) container_of(n, struct acpi_device_attribute, attr);
54
55static ssize_t acpi_device_attr_show(struct kobject *kobj,
Len Brown4be44fc2005-08-05 00:44:28 -040056 struct attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070057{
58 struct acpi_device *device = to_acpi_device(kobj);
59 struct acpi_device_attribute *attribute = to_handle_attr(attr);
Dmitry Torokhov70f28172005-04-29 01:27:34 -050060 return attribute->show ? attribute->show(device, buf) : -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061}
62static ssize_t acpi_device_attr_store(struct kobject *kobj,
Len Brown4be44fc2005-08-05 00:44:28 -040063 struct attribute *attr, const char *buf,
64 size_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -070065{
66 struct acpi_device *device = to_acpi_device(kobj);
67 struct acpi_device_attribute *attribute = to_handle_attr(attr);
Dmitry Torokhov70f28172005-04-29 01:27:34 -050068 return attribute->store ? attribute->store(device, buf, len) : -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069}
70
71static struct sysfs_ops acpi_device_sysfs_ops = {
Len Brown4be44fc2005-08-05 00:44:28 -040072 .show = acpi_device_attr_show,
73 .store = acpi_device_attr_store,
Linus Torvalds1da177e2005-04-16 15:20:36 -070074};
75
76static struct kobj_type ktype_acpi_ns = {
Len Brown4be44fc2005-08-05 00:44:28 -040077 .sysfs_ops = &acpi_device_sysfs_ops,
78 .release = acpi_device_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -070079};
80
Kay Sievers312c0042005-11-16 09:00:00 +010081static int namespace_uevent(struct kset *kset, struct kobject *kobj,
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 char **envp, int num_envp, char *buffer,
83 int buffer_size)
84{
85 struct acpi_device *dev = to_acpi_device(kobj);
86 int i = 0;
87 int len = 0;
88
89 if (!dev->driver)
90 return 0;
91
Kay Sievers312c0042005-11-16 09:00:00 +010092 if (add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &len,
93 "PHYSDEVDRIVER=%s", dev->driver->name))
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 return -ENOMEM;
95
96 envp[i] = NULL;
97
98 return 0;
99}
100
Kay Sievers312c0042005-11-16 09:00:00 +0100101static struct kset_uevent_ops namespace_uevent_ops = {
102 .uevent = &namespace_uevent,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103};
104
105static struct kset acpi_namespace_kset = {
Len Brown4be44fc2005-08-05 00:44:28 -0400106 .kobj = {
107 .name = "namespace",
108 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 .subsys = &acpi_subsys,
Len Brown4be44fc2005-08-05 00:44:28 -0400110 .ktype = &ktype_acpi_ns,
Kay Sievers312c0042005-11-16 09:00:00 +0100111 .uevent_ops = &namespace_uevent_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112};
113
Len Brown4be44fc2005-08-05 00:44:28 -0400114static void acpi_device_register(struct acpi_device *device,
115 struct acpi_device *parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116{
117 /*
118 * Linkage
119 * -------
120 * Link this device to its parent and siblings.
121 */
122 INIT_LIST_HEAD(&device->children);
123 INIT_LIST_HEAD(&device->node);
124 INIT_LIST_HEAD(&device->g_list);
125 INIT_LIST_HEAD(&device->wakeup_list);
126
127 spin_lock(&acpi_device_lock);
128 if (device->parent) {
129 list_add_tail(&device->node, &device->parent->children);
Len Brown4be44fc2005-08-05 00:44:28 -0400130 list_add_tail(&device->g_list, &device->parent->g_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 } else
Len Brown4be44fc2005-08-05 00:44:28 -0400132 list_add_tail(&device->g_list, &acpi_device_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 if (device->wakeup.flags.valid)
Len Brown4be44fc2005-08-05 00:44:28 -0400134 list_add_tail(&device->wakeup_list, &acpi_wakeup_device_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 spin_unlock(&acpi_device_lock);
136
Len Brown4be44fc2005-08-05 00:44:28 -0400137 strlcpy(device->kobj.name, device->pnp.bus_id, KOBJ_NAME_LEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 if (parent)
139 device->kobj.parent = &parent->kobj;
140 device->kobj.ktype = &ktype_acpi_ns;
141 device->kobj.kset = &acpi_namespace_kset;
142 kobject_register(&device->kobj);
143 create_sysfs_device_files(device);
144}
145
Len Brown4be44fc2005-08-05 00:44:28 -0400146static int acpi_device_unregister(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147{
148 spin_lock(&acpi_device_lock);
149 if (device->parent) {
150 list_del(&device->node);
151 list_del(&device->g_list);
152 } else
153 list_del(&device->g_list);
154
155 list_del(&device->wakeup_list);
156
157 spin_unlock(&acpi_device_lock);
158
159 acpi_detach_data(device->handle, acpi_bus_data_handler);
160 remove_sysfs_device_files(device);
161 kobject_unregister(&device->kobj);
162 return 0;
163}
164
Len Brown4be44fc2005-08-05 00:44:28 -0400165void acpi_bus_data_handler(acpi_handle handle, u32 function, void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166{
167 ACPI_FUNCTION_TRACE("acpi_bus_data_handler");
168
169 /* TBD */
170
171 return_VOID;
172}
173
Len Brown4be44fc2005-08-05 00:44:28 -0400174static int acpi_bus_get_power_flags(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175{
Len Brown4be44fc2005-08-05 00:44:28 -0400176 acpi_status status = 0;
177 acpi_handle handle = NULL;
178 u32 i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
180 ACPI_FUNCTION_TRACE("acpi_bus_get_power_flags");
181
182 /*
183 * Power Management Flags
184 */
185 status = acpi_get_handle(device->handle, "_PSC", &handle);
186 if (ACPI_SUCCESS(status))
187 device->power.flags.explicit_get = 1;
188 status = acpi_get_handle(device->handle, "_IRC", &handle);
189 if (ACPI_SUCCESS(status))
190 device->power.flags.inrush_current = 1;
191
192 /*
193 * Enumerate supported power management states
194 */
195 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3; i++) {
196 struct acpi_device_power_state *ps = &device->power.states[i];
Len Brown4be44fc2005-08-05 00:44:28 -0400197 char object_name[5] = { '_', 'P', 'R', '0' + i, '\0' };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
199 /* Evaluate "_PRx" to se if power resources are referenced */
200 acpi_evaluate_reference(device->handle, object_name, NULL,
Len Brown4be44fc2005-08-05 00:44:28 -0400201 &ps->resources);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 if (ps->resources.count) {
203 device->power.flags.power_resources = 1;
204 ps->flags.valid = 1;
205 }
206
207 /* Evaluate "_PSx" to see if we can do explicit sets */
208 object_name[2] = 'S';
209 status = acpi_get_handle(device->handle, object_name, &handle);
210 if (ACPI_SUCCESS(status)) {
211 ps->flags.explicit_set = 1;
212 ps->flags.valid = 1;
213 }
214
215 /* State is valid if we have some power control */
216 if (ps->resources.count || ps->flags.explicit_set)
217 ps->flags.valid = 1;
218
Len Brown4be44fc2005-08-05 00:44:28 -0400219 ps->power = -1; /* Unknown - driver assigned */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 ps->latency = -1; /* Unknown - driver assigned */
221 }
222
223 /* Set defaults for D0 and D3 states (always valid) */
224 device->power.states[ACPI_STATE_D0].flags.valid = 1;
225 device->power.states[ACPI_STATE_D0].power = 100;
226 device->power.states[ACPI_STATE_D3].flags.valid = 1;
227 device->power.states[ACPI_STATE_D3].power = 0;
228
229 /* TBD: System wake support and resource requirements. */
230
231 device->power.state = ACPI_STATE_UNKNOWN;
232
233 return_VALUE(0);
234}
235
Len Brown4be44fc2005-08-05 00:44:28 -0400236int acpi_match_ids(struct acpi_device *device, char *ids)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237{
238 int error = 0;
Len Brown4be44fc2005-08-05 00:44:28 -0400239 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
241 if (device->flags.hardware_id)
242 if (strstr(ids, device->pnp.hardware_id))
243 goto Done;
244
245 if (device->flags.compatible_ids) {
246 struct acpi_compatible_id_list *cid_list = device->pnp.cid_list;
247 int i;
248
249 /* compare multiple _CID entries against driver ids */
Len Brown4be44fc2005-08-05 00:44:28 -0400250 for (i = 0; i < cid_list->count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 if (strstr(ids, cid_list->id[i].value))
252 goto Done;
253 }
254 }
255 error = -ENOENT;
256
Len Brown4be44fc2005-08-05 00:44:28 -0400257 Done:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 if (buffer.pointer)
259 acpi_os_free(buffer.pointer);
260 return error;
261}
262
263static acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400264acpi_bus_extract_wakeup_device_power_package(struct acpi_device *device,
265 union acpi_object *package)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266{
Len Brown4be44fc2005-08-05 00:44:28 -0400267 int i = 0;
268 union acpi_object *element = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
270 if (!device || !package || (package->package.count < 2))
271 return AE_BAD_PARAMETER;
272
273 element = &(package->package.elements[0]);
274 if (!element)
275 return AE_BAD_PARAMETER;
276 if (element->type == ACPI_TYPE_PACKAGE) {
277 if ((element->package.count < 2) ||
Len Brown4be44fc2005-08-05 00:44:28 -0400278 (element->package.elements[0].type !=
279 ACPI_TYPE_LOCAL_REFERENCE)
280 || (element->package.elements[1].type != ACPI_TYPE_INTEGER))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 return AE_BAD_DATA;
Len Brown4be44fc2005-08-05 00:44:28 -0400282 device->wakeup.gpe_device =
283 element->package.elements[0].reference.handle;
284 device->wakeup.gpe_number =
285 (u32) element->package.elements[1].integer.value;
286 } else if (element->type == ACPI_TYPE_INTEGER) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 device->wakeup.gpe_number = element->integer.value;
Len Brown4be44fc2005-08-05 00:44:28 -0400288 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 return AE_BAD_DATA;
290
291 element = &(package->package.elements[1]);
292 if (element->type != ACPI_TYPE_INTEGER) {
293 return AE_BAD_DATA;
294 }
295 device->wakeup.sleep_state = element->integer.value;
296
297 if ((package->package.count - 2) > ACPI_MAX_HANDLES) {
298 return AE_NO_MEMORY;
299 }
300 device->wakeup.resources.count = package->package.count - 2;
Len Brown4be44fc2005-08-05 00:44:28 -0400301 for (i = 0; i < device->wakeup.resources.count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 element = &(package->package.elements[i + 2]);
Len Brown4be44fc2005-08-05 00:44:28 -0400303 if (element->type != ACPI_TYPE_ANY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 return AE_BAD_DATA;
305 }
306
307 device->wakeup.resources.handles[i] = element->reference.handle;
308 }
309
310 return AE_OK;
311}
312
Len Brown4be44fc2005-08-05 00:44:28 -0400313static int acpi_bus_get_wakeup_device_flags(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314{
Len Brown4be44fc2005-08-05 00:44:28 -0400315 acpi_status status = 0;
316 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
317 union acpi_object *package = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
319 ACPI_FUNCTION_TRACE("acpi_bus_get_wakeup_flags");
320
321 /* _PRW */
322 status = acpi_evaluate_object(device->handle, "_PRW", NULL, &buffer);
323 if (ACPI_FAILURE(status)) {
324 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Error evaluating _PRW\n"));
325 goto end;
326 }
327
Len Brown4be44fc2005-08-05 00:44:28 -0400328 package = (union acpi_object *)buffer.pointer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 status = acpi_bus_extract_wakeup_device_power_package(device, package);
330 if (ACPI_FAILURE(status)) {
Len Brown4be44fc2005-08-05 00:44:28 -0400331 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
332 "Error extracting _PRW package\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 goto end;
334 }
335
336 acpi_os_free(buffer.pointer);
337
338 device->wakeup.flags.valid = 1;
Len Brown4be44fc2005-08-05 00:44:28 -0400339 /* Power button, Lid switch always enable wakeup */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 if (!acpi_match_ids(device, "PNP0C0D,PNP0C0C,PNP0C0E"))
341 device->wakeup.flags.run_wake = 1;
342
Len Brown4be44fc2005-08-05 00:44:28 -0400343 end:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 if (ACPI_FAILURE(status))
345 device->flags.wake_capable = 0;
346 return_VALUE(0);
347}
348
349/* --------------------------------------------------------------------------
Kay Sievers312c0042005-11-16 09:00:00 +0100350 ACPI sysfs device file support
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 -------------------------------------------------------------------------- */
Len Brown4be44fc2005-08-05 00:44:28 -0400352static ssize_t acpi_eject_store(struct acpi_device *device,
353 const char *buf, size_t count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
355#define ACPI_DEVICE_ATTR(_name,_mode,_show,_store) \
356static struct acpi_device_attribute acpi_device_attr_##_name = \
357 __ATTR(_name, _mode, _show, _store)
358
359ACPI_DEVICE_ATTR(eject, 0200, NULL, acpi_eject_store);
360
361/**
362 * setup_sys_fs_device_files - sets up the device files under device namespace
Martin Waitz67be2dd2005-05-01 08:59:26 -0700363 * @dev: acpi_device object
364 * @func: function pointer to create or destroy the device file
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 */
366static void
Len Brown4be44fc2005-08-05 00:44:28 -0400367setup_sys_fs_device_files(struct acpi_device *dev,
368 acpi_device_sysfs_files * func)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369{
Len Brown4be44fc2005-08-05 00:44:28 -0400370 acpi_status status;
371 acpi_handle temp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
373 /*
374 * If device has _EJ0, 'eject' file is created that is used to trigger
375 * hot-removal function from userland.
376 */
377 status = acpi_get_handle(dev->handle, "_EJ0", &temp);
378 if (ACPI_SUCCESS(status))
Len Brown4be44fc2005-08-05 00:44:28 -0400379 (*(func)) (&dev->kobj, &acpi_device_attr_eject.attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380}
381
Len Brown4be44fc2005-08-05 00:44:28 -0400382static int acpi_eject_operation(acpi_handle handle, int lockable)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383{
384 struct acpi_object_list arg_list;
385 union acpi_object arg;
386 acpi_status status = AE_OK;
387
388 /*
389 * TBD: evaluate _PS3?
390 */
391
392 if (lockable) {
393 arg_list.count = 1;
394 arg_list.pointer = &arg;
395 arg.type = ACPI_TYPE_INTEGER;
396 arg.integer.value = 0;
397 acpi_evaluate_object(handle, "_LCK", &arg_list, NULL);
398 }
399
400 arg_list.count = 1;
401 arg_list.pointer = &arg;
402 arg.type = ACPI_TYPE_INTEGER;
403 arg.integer.value = 1;
404
405 /*
406 * TBD: _EJD support.
407 */
408
409 status = acpi_evaluate_object(handle, "_EJ0", &arg_list, NULL);
410 if (ACPI_FAILURE(status)) {
Len Brown4be44fc2005-08-05 00:44:28 -0400411 return (-ENODEV);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 }
413
Len Brown4be44fc2005-08-05 00:44:28 -0400414 return (0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415}
416
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417static ssize_t
418acpi_eject_store(struct acpi_device *device, const char *buf, size_t count)
419{
Len Brown4be44fc2005-08-05 00:44:28 -0400420 int result;
421 int ret = count;
422 int islockable;
423 acpi_status status;
424 acpi_handle handle;
425 acpi_object_type type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
427 if ((!count) || (buf[0] != '1')) {
428 return -EINVAL;
429 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430#ifndef FORCE_EJECT
431 if (device->driver == NULL) {
432 ret = -ENODEV;
433 goto err;
434 }
435#endif
436 status = acpi_get_type(device->handle, &type);
Len Brown4be44fc2005-08-05 00:44:28 -0400437 if (ACPI_FAILURE(status) || (!device->flags.ejectable)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 ret = -ENODEV;
439 goto err;
440 }
441
442 islockable = device->flags.lockable;
443 handle = device->handle;
444
445 if (type == ACPI_TYPE_PROCESSOR)
446 result = acpi_bus_trim(device, 0);
447 else
448 result = acpi_bus_trim(device, 1);
449
450 if (!result)
451 result = acpi_eject_operation(handle, islockable);
452
453 if (result) {
454 ret = -EBUSY;
455 }
Len Brown4be44fc2005-08-05 00:44:28 -0400456 err:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 return ret;
458}
459
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460/* --------------------------------------------------------------------------
461 Performance Management
462 -------------------------------------------------------------------------- */
463
Len Brown4be44fc2005-08-05 00:44:28 -0400464static int acpi_bus_get_perf_flags(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465{
466 device->performance.state = ACPI_STATE_UNKNOWN;
467 return 0;
468}
469
470/* --------------------------------------------------------------------------
471 Driver Management
472 -------------------------------------------------------------------------- */
473
474static LIST_HEAD(acpi_bus_drivers);
475static DECLARE_MUTEX(acpi_bus_drivers_lock);
476
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477/**
Randy Dunlapd758a8f2006-01-06 01:31:00 -0500478 * acpi_bus_match - match device IDs to driver's supported IDs
479 * @device: the device that we are trying to match to a driver
480 * @driver: driver whose device id table is being checked
481 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 * Checks the device's hardware (_HID) or compatible (_CID) ids to see if it
483 * matches the specified driver's criteria.
484 */
485static int
Len Brown4be44fc2005-08-05 00:44:28 -0400486acpi_bus_match(struct acpi_device *device, struct acpi_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487{
488 if (driver && driver->ops.match)
489 return driver->ops.match(device, driver);
490 return acpi_match_ids(device, driver->ids);
491}
492
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493/**
Randy Dunlapd758a8f2006-01-06 01:31:00 -0500494 * acpi_bus_driver_init - add a device to a driver
495 * @device: the device to add and initialize
496 * @driver: driver for the device
497 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 * Used to initialize a device via its device driver. Called whenever a
499 * driver is bound to a device. Invokes the driver's add() and start() ops.
500 */
501static int
Len Brown4be44fc2005-08-05 00:44:28 -0400502acpi_bus_driver_init(struct acpi_device *device, struct acpi_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503{
Len Brown4be44fc2005-08-05 00:44:28 -0400504 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505
506 ACPI_FUNCTION_TRACE("acpi_bus_driver_init");
507
508 if (!device || !driver)
509 return_VALUE(-EINVAL);
510
511 if (!driver->ops.add)
512 return_VALUE(-ENOSYS);
513
514 result = driver->ops.add(device);
515 if (result) {
516 device->driver = NULL;
517 acpi_driver_data(device) = NULL;
518 return_VALUE(result);
519 }
520
521 device->driver = driver;
522
523 /*
524 * TBD - Configuration Management: Assign resources to device based
525 * upon possible configuration and currently allocated resources.
526 */
527
Len Brown4be44fc2005-08-05 00:44:28 -0400528 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
529 "Driver successfully bound to device\n"));
Rajesh Shah3fb02732005-04-28 00:25:52 -0700530 return_VALUE(0);
531}
532
Adrian Bunk8713cbe2005-09-02 17:16:48 -0400533static int acpi_start_single_object(struct acpi_device *device)
Rajesh Shah3fb02732005-04-28 00:25:52 -0700534{
535 int result = 0;
536 struct acpi_driver *driver;
537
538 ACPI_FUNCTION_TRACE("acpi_start_single_object");
539
540 if (!(driver = device->driver))
541 return_VALUE(0);
542
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 if (driver->ops.start) {
544 result = driver->ops.start(device);
545 if (result && driver->ops.remove)
546 driver->ops.remove(device, ACPI_BUS_REMOVAL_NORMAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 }
548
Rajesh Shah3fb02732005-04-28 00:25:52 -0700549 return_VALUE(result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550}
551
Len Brown4be44fc2005-08-05 00:44:28 -0400552static int acpi_driver_attach(struct acpi_driver *drv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553{
Len Brown4be44fc2005-08-05 00:44:28 -0400554 struct list_head *node, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 int count = 0;
556
557 ACPI_FUNCTION_TRACE("acpi_driver_attach");
558
559 spin_lock(&acpi_device_lock);
560 list_for_each_safe(node, next, &acpi_device_list) {
Len Brown4be44fc2005-08-05 00:44:28 -0400561 struct acpi_device *dev =
562 container_of(node, struct acpi_device, g_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
564 if (dev->driver || !dev->status.present)
565 continue;
566 spin_unlock(&acpi_device_lock);
567
568 if (!acpi_bus_match(dev, drv)) {
569 if (!acpi_bus_driver_init(dev, drv)) {
Rajesh Shah3fb02732005-04-28 00:25:52 -0700570 acpi_start_single_object(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 atomic_inc(&drv->references);
572 count++;
Len Brown4be44fc2005-08-05 00:44:28 -0400573 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
574 "Found driver [%s] for device [%s]\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 drv->name, dev->pnp.bus_id));
576 }
577 }
578 spin_lock(&acpi_device_lock);
579 }
580 spin_unlock(&acpi_device_lock);
581 return_VALUE(count);
582}
583
Len Brown4be44fc2005-08-05 00:44:28 -0400584static int acpi_driver_detach(struct acpi_driver *drv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585{
Len Brown4be44fc2005-08-05 00:44:28 -0400586 struct list_head *node, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
588 ACPI_FUNCTION_TRACE("acpi_driver_detach");
589
590 spin_lock(&acpi_device_lock);
Len Brown4be44fc2005-08-05 00:44:28 -0400591 list_for_each_safe(node, next, &acpi_device_list) {
592 struct acpi_device *dev =
593 container_of(node, struct acpi_device, g_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594
595 if (dev->driver == drv) {
596 spin_unlock(&acpi_device_lock);
597 if (drv->ops.remove)
Len Brown4be44fc2005-08-05 00:44:28 -0400598 drv->ops.remove(dev, ACPI_BUS_REMOVAL_NORMAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 spin_lock(&acpi_device_lock);
600 dev->driver = NULL;
601 dev->driver_data = NULL;
602 atomic_dec(&drv->references);
603 }
604 }
605 spin_unlock(&acpi_device_lock);
606 return_VALUE(0);
607}
608
609/**
Randy Dunlapd758a8f2006-01-06 01:31:00 -0500610 * acpi_bus_register_driver - register a driver with the ACPI bus
611 * @driver: driver being registered
612 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 * Registers a driver with the ACPI bus. Searches the namespace for all
614 * devices that match the driver's criteria and binds. Returns the
615 * number of devices that were claimed by the driver, or a negative
616 * error status for failure.
617 */
Len Brown4be44fc2005-08-05 00:44:28 -0400618int acpi_bus_register_driver(struct acpi_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619{
620 int count;
621
622 ACPI_FUNCTION_TRACE("acpi_bus_register_driver");
623
624 if (acpi_disabled)
625 return_VALUE(-ENODEV);
626
627 if (!driver)
628 return_VALUE(-EINVAL);
629
630 spin_lock(&acpi_device_lock);
631 list_add_tail(&driver->node, &acpi_bus_drivers);
632 spin_unlock(&acpi_device_lock);
633 count = acpi_driver_attach(driver);
634
635 return_VALUE(count);
636}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
Len Brown4be44fc2005-08-05 00:44:28 -0400638EXPORT_SYMBOL(acpi_bus_register_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639
640/**
Randy Dunlapd758a8f2006-01-06 01:31:00 -0500641 * acpi_bus_unregister_driver - unregisters a driver with the APIC bus
642 * @driver: driver to unregister
643 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 * Unregisters a driver with the ACPI bus. Searches the namespace for all
645 * devices that match the driver's criteria and unbinds.
646 */
Len Brown4be44fc2005-08-05 00:44:28 -0400647int acpi_bus_unregister_driver(struct acpi_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648{
649 int error = 0;
650
651 ACPI_FUNCTION_TRACE("acpi_bus_unregister_driver");
652
653 if (driver) {
654 acpi_driver_detach(driver);
655
656 if (!atomic_read(&driver->references)) {
657 spin_lock(&acpi_device_lock);
658 list_del_init(&driver->node);
659 spin_unlock(&acpi_device_lock);
Len Brown4be44fc2005-08-05 00:44:28 -0400660 }
661 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 error = -EINVAL;
663 return_VALUE(error);
664}
Len Brown4be44fc2005-08-05 00:44:28 -0400665
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666EXPORT_SYMBOL(acpi_bus_unregister_driver);
667
668/**
Randy Dunlapd758a8f2006-01-06 01:31:00 -0500669 * acpi_bus_find_driver - check if there is a driver installed for the device
670 * @device: device that we are trying to find a supporting driver for
671 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 * Parses the list of registered drivers looking for a driver applicable for
673 * the specified device.
674 */
Len Brown4be44fc2005-08-05 00:44:28 -0400675static int acpi_bus_find_driver(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676{
Len Brown4be44fc2005-08-05 00:44:28 -0400677 int result = 0;
678 struct list_head *node, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679
680 ACPI_FUNCTION_TRACE("acpi_bus_find_driver");
681
682 spin_lock(&acpi_device_lock);
Len Brown4be44fc2005-08-05 00:44:28 -0400683 list_for_each_safe(node, next, &acpi_bus_drivers) {
684 struct acpi_driver *driver =
685 container_of(node, struct acpi_driver, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
687 atomic_inc(&driver->references);
688 spin_unlock(&acpi_device_lock);
689 if (!acpi_bus_match(device, driver)) {
690 result = acpi_bus_driver_init(device, driver);
691 if (!result)
692 goto Done;
693 }
694 atomic_dec(&driver->references);
695 spin_lock(&acpi_device_lock);
696 }
697 spin_unlock(&acpi_device_lock);
698
Len Brown4be44fc2005-08-05 00:44:28 -0400699 Done:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 return_VALUE(result);
701}
702
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703/* --------------------------------------------------------------------------
704 Device Enumeration
705 -------------------------------------------------------------------------- */
706
Len Brown4be44fc2005-08-05 00:44:28 -0400707static int acpi_bus_get_flags(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708{
Len Brown4be44fc2005-08-05 00:44:28 -0400709 acpi_status status = AE_OK;
710 acpi_handle temp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711
712 ACPI_FUNCTION_TRACE("acpi_bus_get_flags");
713
714 /* Presence of _STA indicates 'dynamic_status' */
715 status = acpi_get_handle(device->handle, "_STA", &temp);
716 if (ACPI_SUCCESS(status))
717 device->flags.dynamic_status = 1;
718
719 /* Presence of _CID indicates 'compatible_ids' */
720 status = acpi_get_handle(device->handle, "_CID", &temp);
721 if (ACPI_SUCCESS(status))
722 device->flags.compatible_ids = 1;
723
724 /* Presence of _RMV indicates 'removable' */
725 status = acpi_get_handle(device->handle, "_RMV", &temp);
726 if (ACPI_SUCCESS(status))
727 device->flags.removable = 1;
728
729 /* Presence of _EJD|_EJ0 indicates 'ejectable' */
730 status = acpi_get_handle(device->handle, "_EJD", &temp);
731 if (ACPI_SUCCESS(status))
732 device->flags.ejectable = 1;
733 else {
734 status = acpi_get_handle(device->handle, "_EJ0", &temp);
735 if (ACPI_SUCCESS(status))
736 device->flags.ejectable = 1;
737 }
738
739 /* Presence of _LCK indicates 'lockable' */
740 status = acpi_get_handle(device->handle, "_LCK", &temp);
741 if (ACPI_SUCCESS(status))
742 device->flags.lockable = 1;
743
744 /* Presence of _PS0|_PR0 indicates 'power manageable' */
745 status = acpi_get_handle(device->handle, "_PS0", &temp);
746 if (ACPI_FAILURE(status))
747 status = acpi_get_handle(device->handle, "_PR0", &temp);
748 if (ACPI_SUCCESS(status))
749 device->flags.power_manageable = 1;
750
751 /* Presence of _PRW indicates wake capable */
752 status = acpi_get_handle(device->handle, "_PRW", &temp);
753 if (ACPI_SUCCESS(status))
754 device->flags.wake_capable = 1;
755
756 /* TBD: Peformance management */
757
758 return_VALUE(0);
759}
760
Len Brown4be44fc2005-08-05 00:44:28 -0400761static void acpi_device_get_busid(struct acpi_device *device,
762 acpi_handle handle, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763{
Len Brown4be44fc2005-08-05 00:44:28 -0400764 char bus_id[5] = { '?', 0 };
765 struct acpi_buffer buffer = { sizeof(bus_id), bus_id };
766 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767
768 /*
769 * Bus ID
770 * ------
771 * The device's Bus ID is simply the object name.
772 * TBD: Shouldn't this value be unique (within the ACPI namespace)?
773 */
774 switch (type) {
775 case ACPI_BUS_TYPE_SYSTEM:
776 strcpy(device->pnp.bus_id, "ACPI");
777 break;
778 case ACPI_BUS_TYPE_POWER_BUTTON:
779 strcpy(device->pnp.bus_id, "PWRF");
780 break;
781 case ACPI_BUS_TYPE_SLEEP_BUTTON:
782 strcpy(device->pnp.bus_id, "SLPF");
783 break;
784 default:
785 acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer);
786 /* Clean up trailing underscores (if any) */
787 for (i = 3; i > 1; i--) {
788 if (bus_id[i] == '_')
789 bus_id[i] = '\0';
790 else
791 break;
792 }
793 strcpy(device->pnp.bus_id, bus_id);
794 break;
795 }
796}
797
Len Brown4be44fc2005-08-05 00:44:28 -0400798static void acpi_device_set_id(struct acpi_device *device,
799 struct acpi_device *parent, acpi_handle handle,
800 int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801{
Len Brown4be44fc2005-08-05 00:44:28 -0400802 struct acpi_device_info *info;
803 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
804 char *hid = NULL;
805 char *uid = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 struct acpi_compatible_id_list *cid_list = NULL;
Len Brown4be44fc2005-08-05 00:44:28 -0400807 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808
809 switch (type) {
810 case ACPI_BUS_TYPE_DEVICE:
811 status = acpi_get_object_info(handle, &buffer);
812 if (ACPI_FAILURE(status)) {
Len Brown4be44fc2005-08-05 00:44:28 -0400813 printk("%s: Error reading device info\n", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 return;
815 }
816
817 info = buffer.pointer;
818 if (info->valid & ACPI_VALID_HID)
819 hid = info->hardware_id.value;
820 if (info->valid & ACPI_VALID_UID)
821 uid = info->unique_id.value;
822 if (info->valid & ACPI_VALID_CID)
823 cid_list = &info->compatibility_id;
824 if (info->valid & ACPI_VALID_ADR) {
825 device->pnp.bus_address = info->address;
826 device->flags.bus_address = 1;
827 }
828 break;
829 case ACPI_BUS_TYPE_POWER:
830 hid = ACPI_POWER_HID;
831 break;
832 case ACPI_BUS_TYPE_PROCESSOR:
833 hid = ACPI_PROCESSOR_HID;
834 break;
835 case ACPI_BUS_TYPE_SYSTEM:
836 hid = ACPI_SYSTEM_HID;
837 break;
838 case ACPI_BUS_TYPE_THERMAL:
839 hid = ACPI_THERMAL_HID;
840 break;
841 case ACPI_BUS_TYPE_POWER_BUTTON:
842 hid = ACPI_BUTTON_HID_POWERF;
843 break;
844 case ACPI_BUS_TYPE_SLEEP_BUTTON:
845 hid = ACPI_BUTTON_HID_SLEEPF;
846 break;
847 }
848
849 /*
850 * \_SB
851 * ----
852 * Fix for the system root bus device -- the only root-level device.
853 */
Bob Moorec51a4de2005-11-17 13:07:00 -0500854 if (((acpi_handle)parent == ACPI_ROOT_OBJECT) && (type == ACPI_BUS_TYPE_DEVICE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 hid = ACPI_BUS_HID;
856 strcpy(device->pnp.device_name, ACPI_BUS_DEVICE_NAME);
857 strcpy(device->pnp.device_class, ACPI_BUS_CLASS);
858 }
859
860 if (hid) {
861 strcpy(device->pnp.hardware_id, hid);
862 device->flags.hardware_id = 1;
863 }
864 if (uid) {
865 strcpy(device->pnp.unique_id, uid);
866 device->flags.unique_id = 1;
867 }
868 if (cid_list) {
869 device->pnp.cid_list = kmalloc(cid_list->size, GFP_KERNEL);
870 if (device->pnp.cid_list)
871 memcpy(device->pnp.cid_list, cid_list, cid_list->size);
872 else
873 printk(KERN_ERR "Memory allocation error\n");
874 }
875
876 acpi_os_free(buffer.pointer);
877}
878
Len Brown4be44fc2005-08-05 00:44:28 -0400879static int acpi_device_set_context(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880{
881 acpi_status status = AE_OK;
882 int result = 0;
883 /*
884 * Context
885 * -------
886 * Attach this 'struct acpi_device' to the ACPI object. This makes
887 * resolutions from handle->device very efficient. Note that we need
888 * to be careful with fixed-feature devices as they all attach to the
889 * root object.
890 */
Len Brown4be44fc2005-08-05 00:44:28 -0400891 if (type != ACPI_BUS_TYPE_POWER_BUTTON &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 type != ACPI_BUS_TYPE_SLEEP_BUTTON) {
893 status = acpi_attach_data(device->handle,
Len Brown4be44fc2005-08-05 00:44:28 -0400894 acpi_bus_data_handler, device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895
896 if (ACPI_FAILURE(status)) {
897 printk("Error attaching device data\n");
898 result = -ENODEV;
899 }
900 }
901 return result;
902}
903
Len Brown4be44fc2005-08-05 00:44:28 -0400904static void acpi_device_get_debug_info(struct acpi_device *device,
905 acpi_handle handle, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906{
907#ifdef CONFIG_ACPI_DEBUG_OUTPUT
Len Brown4be44fc2005-08-05 00:44:28 -0400908 char *type_string = NULL;
909 char name[80] = { '?', '\0' };
910 struct acpi_buffer buffer = { sizeof(name), name };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911
912 switch (type) {
913 case ACPI_BUS_TYPE_DEVICE:
914 type_string = "Device";
915 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
916 break;
917 case ACPI_BUS_TYPE_POWER:
918 type_string = "Power Resource";
919 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
920 break;
921 case ACPI_BUS_TYPE_PROCESSOR:
922 type_string = "Processor";
923 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
924 break;
925 case ACPI_BUS_TYPE_SYSTEM:
926 type_string = "System";
927 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
928 break;
929 case ACPI_BUS_TYPE_THERMAL:
930 type_string = "Thermal Zone";
931 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
932 break;
933 case ACPI_BUS_TYPE_POWER_BUTTON:
934 type_string = "Power Button";
935 sprintf(name, "PWRB");
936 break;
937 case ACPI_BUS_TYPE_SLEEP_BUTTON:
938 type_string = "Sleep Button";
939 sprintf(name, "SLPB");
940 break;
941 }
942
943 printk(KERN_DEBUG "Found %s %s [%p]\n", type_string, name, handle);
Len Brown4be44fc2005-08-05 00:44:28 -0400944#endif /*CONFIG_ACPI_DEBUG_OUTPUT */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945}
946
Len Brown4be44fc2005-08-05 00:44:28 -0400947static int acpi_bus_remove(struct acpi_device *dev, int rmdevice)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948{
Len Brown4be44fc2005-08-05 00:44:28 -0400949 int result = 0;
950 struct acpi_driver *driver;
951
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 ACPI_FUNCTION_TRACE("acpi_bus_remove");
953
954 if (!dev)
955 return_VALUE(-EINVAL);
956
957 driver = dev->driver;
958
959 if ((driver) && (driver->ops.remove)) {
960
961 if (driver->ops.stop) {
962 result = driver->ops.stop(dev, ACPI_BUS_REMOVAL_EJECT);
963 if (result)
964 return_VALUE(result);
965 }
966
967 result = dev->driver->ops.remove(dev, ACPI_BUS_REMOVAL_EJECT);
968 if (result) {
969 return_VALUE(result);
970 }
971
972 atomic_dec(&dev->driver->references);
973 dev->driver = NULL;
974 acpi_driver_data(dev) = NULL;
975 }
976
977 if (!rmdevice)
978 return_VALUE(0);
979
980 if (dev->flags.bus_address) {
981 if ((dev->parent) && (dev->parent->ops.unbind))
982 dev->parent->ops.unbind(dev);
983 }
Len Brown4be44fc2005-08-05 00:44:28 -0400984
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 acpi_device_unregister(dev, ACPI_BUS_REMOVAL_EJECT);
986
987 return_VALUE(0);
988}
989
Rajesh Shah3fb02732005-04-28 00:25:52 -0700990static int
Len Brown4be44fc2005-08-05 00:44:28 -0400991acpi_add_single_object(struct acpi_device **child,
992 struct acpi_device *parent, acpi_handle handle, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993{
Len Brown4be44fc2005-08-05 00:44:28 -0400994 int result = 0;
995 struct acpi_device *device = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996
Rajesh Shah3fb02732005-04-28 00:25:52 -0700997 ACPI_FUNCTION_TRACE("acpi_add_single_object");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998
999 if (!child)
1000 return_VALUE(-EINVAL);
1001
1002 device = kmalloc(sizeof(struct acpi_device), GFP_KERNEL);
1003 if (!device) {
1004 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Memory allocation error\n"));
1005 return_VALUE(-ENOMEM);
1006 }
1007 memset(device, 0, sizeof(struct acpi_device));
1008
1009 device->handle = handle;
1010 device->parent = parent;
1011
Len Brown4be44fc2005-08-05 00:44:28 -04001012 acpi_device_get_busid(device, handle, type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013
1014 /*
1015 * Flags
1016 * -----
1017 * Get prior to calling acpi_bus_get_status() so we know whether
1018 * or not _STA is present. Note that we only look for object
1019 * handles -- cannot evaluate objects until we know the device is
1020 * present and properly initialized.
1021 */
1022 result = acpi_bus_get_flags(device);
1023 if (result)
1024 goto end;
1025
1026 /*
1027 * Status
1028 * ------
Keiichiro Tokunaga6940fab2005-03-30 23:15:47 -05001029 * See if the device is present. We always assume that non-Device
1030 * and non-Processor objects (e.g. thermal zones, power resources,
1031 * etc.) are present, functioning, etc. (at least when parent object
1032 * is present). Note that _STA has a different meaning for some
1033 * objects (e.g. power resources) so we need to be careful how we use
1034 * it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 */
1036 switch (type) {
Keiichiro Tokunaga6940fab2005-03-30 23:15:47 -05001037 case ACPI_BUS_TYPE_PROCESSOR:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 case ACPI_BUS_TYPE_DEVICE:
1039 result = acpi_bus_get_status(device);
1040 if (ACPI_FAILURE(result) || !device->status.present) {
1041 result = -ENOENT;
1042 goto end;
1043 }
1044 break;
1045 default:
1046 STRUCT_TO_INT(device->status) = 0x0F;
1047 break;
1048 }
1049
1050 /*
1051 * Initialize Device
1052 * -----------------
1053 * TBD: Synch with Core's enumeration/initialization process.
1054 */
1055
1056 /*
1057 * Hardware ID, Unique ID, & Bus Address
1058 * -------------------------------------
1059 */
Len Brown4be44fc2005-08-05 00:44:28 -04001060 acpi_device_set_id(device, parent, handle, type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061
1062 /*
1063 * Power Management
1064 * ----------------
1065 */
1066 if (device->flags.power_manageable) {
1067 result = acpi_bus_get_power_flags(device);
1068 if (result)
1069 goto end;
1070 }
1071
Len Brown4be44fc2005-08-05 00:44:28 -04001072 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 * Wakeup device management
1074 *-----------------------
1075 */
1076 if (device->flags.wake_capable) {
1077 result = acpi_bus_get_wakeup_device_flags(device);
1078 if (result)
1079 goto end;
1080 }
1081
1082 /*
1083 * Performance Management
1084 * ----------------------
1085 */
1086 if (device->flags.performance_manageable) {
1087 result = acpi_bus_get_perf_flags(device);
1088 if (result)
1089 goto end;
1090 }
1091
Len Brown4be44fc2005-08-05 00:44:28 -04001092 if ((result = acpi_device_set_context(device, type)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 goto end;
1094
Len Brown4be44fc2005-08-05 00:44:28 -04001095 acpi_device_get_debug_info(device, handle, type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096
Len Brown4be44fc2005-08-05 00:44:28 -04001097 acpi_device_register(device, parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098
1099 /*
1100 * Bind _ADR-Based Devices
1101 * -----------------------
1102 * If there's a a bus address (_ADR) then we utilize the parent's
1103 * 'bind' function (if exists) to bind the ACPI- and natively-
1104 * enumerated device representations.
1105 */
1106 if (device->flags.bus_address) {
1107 if (device->parent && device->parent->ops.bind)
1108 device->parent->ops.bind(device);
1109 }
1110
1111 /*
1112 * Locate & Attach Driver
1113 * ----------------------
1114 * If there's a hardware id (_HID) or compatible ids (_CID) we check
1115 * to see if there's a driver installed for this kind of device. Note
1116 * that drivers can install before or after a device is enumerated.
1117 *
1118 * TBD: Assumes LDM provides driver hot-plug capability.
1119 */
Thomas Renningerbd7ce5b2005-10-03 10:39:00 -07001120 acpi_bus_find_driver(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121
Len Brown4be44fc2005-08-05 00:44:28 -04001122 end:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 if (!result)
1124 *child = device;
1125 else {
Jesper Juhl6044ec82005-11-07 01:01:32 -08001126 kfree(device->pnp.cid_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 kfree(device);
1128 }
1129
1130 return_VALUE(result);
1131}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132
Len Brown4be44fc2005-08-05 00:44:28 -04001133static int acpi_bus_scan(struct acpi_device *start, struct acpi_bus_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134{
Len Brown4be44fc2005-08-05 00:44:28 -04001135 acpi_status status = AE_OK;
1136 struct acpi_device *parent = NULL;
1137 struct acpi_device *child = NULL;
1138 acpi_handle phandle = NULL;
1139 acpi_handle chandle = NULL;
1140 acpi_object_type type = 0;
1141 u32 level = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142
1143 ACPI_FUNCTION_TRACE("acpi_bus_scan");
1144
1145 if (!start)
1146 return_VALUE(-EINVAL);
1147
1148 parent = start;
1149 phandle = start->handle;
Len Brown4be44fc2005-08-05 00:44:28 -04001150
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 /*
1152 * Parse through the ACPI namespace, identify all 'devices', and
1153 * create a new 'struct acpi_device' for each.
1154 */
1155 while ((level > 0) && parent) {
1156
1157 status = acpi_get_next_object(ACPI_TYPE_ANY, phandle,
Len Brown4be44fc2005-08-05 00:44:28 -04001158 chandle, &chandle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159
1160 /*
1161 * If this scope is exhausted then move our way back up.
1162 */
1163 if (ACPI_FAILURE(status)) {
1164 level--;
1165 chandle = phandle;
1166 acpi_get_parent(phandle, &phandle);
1167 if (parent->parent)
1168 parent = parent->parent;
1169 continue;
1170 }
1171
1172 status = acpi_get_type(chandle, &type);
1173 if (ACPI_FAILURE(status))
1174 continue;
1175
1176 /*
1177 * If this is a scope object then parse it (depth-first).
1178 */
1179 if (type == ACPI_TYPE_LOCAL_SCOPE) {
1180 level++;
1181 phandle = chandle;
1182 chandle = NULL;
1183 continue;
1184 }
1185
1186 /*
1187 * We're only interested in objects that we consider 'devices'.
1188 */
1189 switch (type) {
1190 case ACPI_TYPE_DEVICE:
1191 type = ACPI_BUS_TYPE_DEVICE;
1192 break;
1193 case ACPI_TYPE_PROCESSOR:
1194 type = ACPI_BUS_TYPE_PROCESSOR;
1195 break;
1196 case ACPI_TYPE_THERMAL:
1197 type = ACPI_BUS_TYPE_THERMAL;
1198 break;
1199 case ACPI_TYPE_POWER:
1200 type = ACPI_BUS_TYPE_POWER;
1201 break;
1202 default:
1203 continue;
1204 }
1205
Rajesh Shah3fb02732005-04-28 00:25:52 -07001206 if (ops->acpi_op_add)
1207 status = acpi_add_single_object(&child, parent,
Len Brown4be44fc2005-08-05 00:44:28 -04001208 chandle, type);
1209 else
Rajesh Shah3fb02732005-04-28 00:25:52 -07001210 status = acpi_bus_get_device(chandle, &child);
1211
Len Brown4be44fc2005-08-05 00:44:28 -04001212 if (ACPI_FAILURE(status))
1213 continue;
Rajesh Shah3fb02732005-04-28 00:25:52 -07001214
1215 if (ops->acpi_op_start) {
1216 status = acpi_start_single_object(child);
1217 if (ACPI_FAILURE(status))
1218 continue;
1219 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220
1221 /*
1222 * If the device is present, enabled, and functioning then
1223 * parse its scope (depth-first). Note that we need to
1224 * represent absent devices to facilitate PnP notifications
1225 * -- but only the subtree head (not all of its children,
1226 * which will be enumerated when the parent is inserted).
1227 *
1228 * TBD: Need notifications and other detection mechanisms
Len Brown4be44fc2005-08-05 00:44:28 -04001229 * in place before we can fully implement this.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 */
1231 if (child->status.present) {
1232 status = acpi_get_next_object(ACPI_TYPE_ANY, chandle,
1233 NULL, NULL);
1234 if (ACPI_SUCCESS(status)) {
1235 level++;
1236 phandle = chandle;
1237 chandle = NULL;
1238 parent = child;
1239 }
1240 }
1241 }
1242
1243 return_VALUE(0);
1244}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245
Rajesh Shah3fb02732005-04-28 00:25:52 -07001246int
Len Brown4be44fc2005-08-05 00:44:28 -04001247acpi_bus_add(struct acpi_device **child,
1248 struct acpi_device *parent, acpi_handle handle, int type)
Rajesh Shah3fb02732005-04-28 00:25:52 -07001249{
1250 int result;
1251 struct acpi_bus_ops ops;
1252
1253 ACPI_FUNCTION_TRACE("acpi_bus_add");
1254
1255 result = acpi_add_single_object(child, parent, handle, type);
1256 if (!result) {
1257 memset(&ops, 0, sizeof(ops));
1258 ops.acpi_op_add = 1;
1259 result = acpi_bus_scan(*child, &ops);
1260 }
1261 return_VALUE(result);
1262}
Len Brown4be44fc2005-08-05 00:44:28 -04001263
Rajesh Shah3fb02732005-04-28 00:25:52 -07001264EXPORT_SYMBOL(acpi_bus_add);
1265
Len Brown4be44fc2005-08-05 00:44:28 -04001266int acpi_bus_start(struct acpi_device *device)
Rajesh Shah3fb02732005-04-28 00:25:52 -07001267{
1268 int result;
1269 struct acpi_bus_ops ops;
1270
1271 ACPI_FUNCTION_TRACE("acpi_bus_start");
1272
1273 if (!device)
1274 return_VALUE(-EINVAL);
1275
1276 result = acpi_start_single_object(device);
1277 if (!result) {
1278 memset(&ops, 0, sizeof(ops));
1279 ops.acpi_op_start = 1;
1280 result = acpi_bus_scan(device, &ops);
1281 }
1282 return_VALUE(result);
1283}
Len Brown4be44fc2005-08-05 00:44:28 -04001284
Rajesh Shah3fb02732005-04-28 00:25:52 -07001285EXPORT_SYMBOL(acpi_bus_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286
Len Brown4be44fc2005-08-05 00:44:28 -04001287static int acpi_bus_trim(struct acpi_device *start, int rmdevice)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288{
Len Brown4be44fc2005-08-05 00:44:28 -04001289 acpi_status status;
1290 struct acpi_device *parent, *child;
1291 acpi_handle phandle, chandle;
1292 acpi_object_type type;
1293 u32 level = 1;
1294 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295
Len Brown4be44fc2005-08-05 00:44:28 -04001296 parent = start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 phandle = start->handle;
1298 child = chandle = NULL;
1299
1300 while ((level > 0) && parent && (!err)) {
1301 status = acpi_get_next_object(ACPI_TYPE_ANY, phandle,
Len Brown4be44fc2005-08-05 00:44:28 -04001302 chandle, &chandle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303
1304 /*
1305 * If this scope is exhausted then move our way back up.
1306 */
1307 if (ACPI_FAILURE(status)) {
1308 level--;
1309 chandle = phandle;
1310 acpi_get_parent(phandle, &phandle);
1311 child = parent;
1312 parent = parent->parent;
1313
1314 if (level == 0)
1315 err = acpi_bus_remove(child, rmdevice);
1316 else
1317 err = acpi_bus_remove(child, 1);
1318
1319 continue;
1320 }
1321
1322 status = acpi_get_type(chandle, &type);
1323 if (ACPI_FAILURE(status)) {
1324 continue;
1325 }
1326 /*
1327 * If there is a device corresponding to chandle then
1328 * parse it (depth-first).
1329 */
1330 if (acpi_bus_get_device(chandle, &child) == 0) {
1331 level++;
1332 phandle = chandle;
1333 chandle = NULL;
1334 parent = child;
1335 }
1336 continue;
1337 }
1338 return err;
1339}
1340
Len Brown4be44fc2005-08-05 00:44:28 -04001341static int acpi_bus_scan_fixed(struct acpi_device *root)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342{
Len Brown4be44fc2005-08-05 00:44:28 -04001343 int result = 0;
1344 struct acpi_device *device = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345
1346 ACPI_FUNCTION_TRACE("acpi_bus_scan_fixed");
1347
1348 if (!root)
1349 return_VALUE(-ENODEV);
1350
1351 /*
1352 * Enumerate all fixed-feature devices.
1353 */
Rajesh Shah3fb02732005-04-28 00:25:52 -07001354 if (acpi_fadt.pwr_button == 0) {
1355 result = acpi_add_single_object(&device, acpi_root,
Len Brown4be44fc2005-08-05 00:44:28 -04001356 NULL,
1357 ACPI_BUS_TYPE_POWER_BUTTON);
Rajesh Shah3fb02732005-04-28 00:25:52 -07001358 if (!result)
1359 result = acpi_start_single_object(device);
1360 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361
Rajesh Shah3fb02732005-04-28 00:25:52 -07001362 if (acpi_fadt.sleep_button == 0) {
1363 result = acpi_add_single_object(&device, acpi_root,
Len Brown4be44fc2005-08-05 00:44:28 -04001364 NULL,
1365 ACPI_BUS_TYPE_SLEEP_BUTTON);
Rajesh Shah3fb02732005-04-28 00:25:52 -07001366 if (!result)
1367 result = acpi_start_single_object(device);
1368 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369
1370 return_VALUE(result);
1371}
1372
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373static int __init acpi_scan_init(void)
1374{
1375 int result;
Rajesh Shah3fb02732005-04-28 00:25:52 -07001376 struct acpi_bus_ops ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377
1378 ACPI_FUNCTION_TRACE("acpi_scan_init");
1379
1380 if (acpi_disabled)
1381 return_VALUE(0);
1382
1383 kset_register(&acpi_namespace_kset);
1384
1385 /*
1386 * Create the root device in the bus's device tree
1387 */
Rajesh Shah3fb02732005-04-28 00:25:52 -07001388 result = acpi_add_single_object(&acpi_root, NULL, ACPI_ROOT_OBJECT,
Len Brown4be44fc2005-08-05 00:44:28 -04001389 ACPI_BUS_TYPE_SYSTEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 if (result)
1391 goto Done;
1392
Rajesh Shah3fb02732005-04-28 00:25:52 -07001393 result = acpi_start_single_object(acpi_root);
1394
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 /*
1396 * Enumerate devices in the ACPI namespace.
1397 */
1398 result = acpi_bus_scan_fixed(acpi_root);
Rajesh Shah3fb02732005-04-28 00:25:52 -07001399 if (!result) {
1400 memset(&ops, 0, sizeof(ops));
1401 ops.acpi_op_add = 1;
1402 ops.acpi_op_start = 1;
1403 result = acpi_bus_scan(acpi_root, &ops);
1404 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405
1406 if (result)
1407 acpi_device_unregister(acpi_root, ACPI_BUS_REMOVAL_NORMAL);
1408
Len Brown4be44fc2005-08-05 00:44:28 -04001409 Done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 return_VALUE(result);
1411}
1412
1413subsys_initcall(acpi_scan_init);