blob: 964ee5c8ea1695c14d84c639f46126ede47451a4 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * scan.c - support for transforming the ACPI namespace into individual objects
3 */
4
5#include <linux/module.h>
6#include <linux/init.h>
7#include <linux/acpi.h>
8
9#include <acpi/acpi_drivers.h>
10#include <acpi/acinterp.h> /* for acpi_ex_eisa_id_to_string() */
11
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#define _COMPONENT ACPI_BUS_COMPONENT
Len Brown4be44fc2005-08-05 00:44:28 -040013ACPI_MODULE_NAME("scan")
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#define STRUCT_TO_INT(s) (*((int*)&s))
Len Brown4be44fc2005-08-05 00:44:28 -040015extern struct acpi_device *acpi_root;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016
17#define ACPI_BUS_CLASS "system_bus"
18#define ACPI_BUS_HID "ACPI_BUS"
19#define ACPI_BUS_DRIVER_NAME "ACPI Bus Driver"
20#define ACPI_BUS_DEVICE_NAME "System Bus"
21
22static LIST_HEAD(acpi_device_list);
23DEFINE_SPINLOCK(acpi_device_lock);
24LIST_HEAD(acpi_wakeup_device_list);
25
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
Len Brown4be44fc2005-08-05 00:44:28 -040027static void acpi_device_release(struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -070028{
Len Brown4be44fc2005-08-05 00:44:28 -040029 struct acpi_device *dev = container_of(kobj, struct acpi_device, kobj);
Jesper Juhl6044ec82005-11-07 01:01:32 -080030 kfree(dev->pnp.cid_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070031 kfree(dev);
32}
33
34struct acpi_device_attribute {
35 struct attribute attr;
Len Brown4be44fc2005-08-05 00:44:28 -040036 ssize_t(*show) (struct acpi_device *, char *);
37 ssize_t(*store) (struct acpi_device *, const char *, size_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -070038};
39
40typedef void acpi_device_sysfs_files(struct kobject *,
Len Brown4be44fc2005-08-05 00:44:28 -040041 const struct attribute *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43static void setup_sys_fs_device_files(struct acpi_device *dev,
Len Brown4be44fc2005-08-05 00:44:28 -040044 acpi_device_sysfs_files * func);
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
46#define create_sysfs_device_files(dev) \
47 setup_sys_fs_device_files(dev, (acpi_device_sysfs_files *)&sysfs_create_file)
48#define remove_sysfs_device_files(dev) \
49 setup_sys_fs_device_files(dev, (acpi_device_sysfs_files *)&sysfs_remove_file)
50
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#define to_acpi_device(n) container_of(n, struct acpi_device, kobj)
52#define to_handle_attr(n) container_of(n, struct acpi_device_attribute, attr);
53
54static ssize_t acpi_device_attr_show(struct kobject *kobj,
Len Brown4be44fc2005-08-05 00:44:28 -040055 struct attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070056{
57 struct acpi_device *device = to_acpi_device(kobj);
58 struct acpi_device_attribute *attribute = to_handle_attr(attr);
Dmitry Torokhov70f28172005-04-29 01:27:34 -050059 return attribute->show ? attribute->show(device, buf) : -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070060}
61static ssize_t acpi_device_attr_store(struct kobject *kobj,
Len Brown4be44fc2005-08-05 00:44:28 -040062 struct attribute *attr, const char *buf,
63 size_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -070064{
65 struct acpi_device *device = to_acpi_device(kobj);
66 struct acpi_device_attribute *attribute = to_handle_attr(attr);
Dmitry Torokhov70f28172005-04-29 01:27:34 -050067 return attribute->store ? attribute->store(device, buf, len) : -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068}
69
70static struct sysfs_ops acpi_device_sysfs_ops = {
Len Brown4be44fc2005-08-05 00:44:28 -040071 .show = acpi_device_attr_show,
72 .store = acpi_device_attr_store,
Linus Torvalds1da177e2005-04-16 15:20:36 -070073};
74
75static struct kobj_type ktype_acpi_ns = {
Len Brown4be44fc2005-08-05 00:44:28 -040076 .sysfs_ops = &acpi_device_sysfs_ops,
77 .release = acpi_device_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -070078};
79
Kay Sievers312c0042005-11-16 09:00:00 +010080static int namespace_uevent(struct kset *kset, struct kobject *kobj,
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 char **envp, int num_envp, char *buffer,
82 int buffer_size)
83{
84 struct acpi_device *dev = to_acpi_device(kobj);
85 int i = 0;
86 int len = 0;
87
88 if (!dev->driver)
89 return 0;
90
Kay Sievers312c0042005-11-16 09:00:00 +010091 if (add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &len,
92 "PHYSDEVDRIVER=%s", dev->driver->name))
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 return -ENOMEM;
94
95 envp[i] = NULL;
96
97 return 0;
98}
99
Kay Sievers312c0042005-11-16 09:00:00 +0100100static struct kset_uevent_ops namespace_uevent_ops = {
101 .uevent = &namespace_uevent,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102};
103
104static struct kset acpi_namespace_kset = {
Len Brown4be44fc2005-08-05 00:44:28 -0400105 .kobj = {
106 .name = "namespace",
107 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 .subsys = &acpi_subsys,
Len Brown4be44fc2005-08-05 00:44:28 -0400109 .ktype = &ktype_acpi_ns,
Kay Sievers312c0042005-11-16 09:00:00 +0100110 .uevent_ops = &namespace_uevent_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111};
112
Len Brown4be44fc2005-08-05 00:44:28 -0400113static void acpi_device_register(struct acpi_device *device,
114 struct acpi_device *parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115{
116 /*
117 * Linkage
118 * -------
119 * Link this device to its parent and siblings.
120 */
121 INIT_LIST_HEAD(&device->children);
122 INIT_LIST_HEAD(&device->node);
123 INIT_LIST_HEAD(&device->g_list);
124 INIT_LIST_HEAD(&device->wakeup_list);
125
126 spin_lock(&acpi_device_lock);
127 if (device->parent) {
128 list_add_tail(&device->node, &device->parent->children);
Len Brown4be44fc2005-08-05 00:44:28 -0400129 list_add_tail(&device->g_list, &device->parent->g_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 } else
Len Brown4be44fc2005-08-05 00:44:28 -0400131 list_add_tail(&device->g_list, &acpi_device_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 if (device->wakeup.flags.valid)
Len Brown4be44fc2005-08-05 00:44:28 -0400133 list_add_tail(&device->wakeup_list, &acpi_wakeup_device_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 spin_unlock(&acpi_device_lock);
135
Len Brown4be44fc2005-08-05 00:44:28 -0400136 strlcpy(device->kobj.name, device->pnp.bus_id, KOBJ_NAME_LEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 if (parent)
138 device->kobj.parent = &parent->kobj;
139 device->kobj.ktype = &ktype_acpi_ns;
140 device->kobj.kset = &acpi_namespace_kset;
141 kobject_register(&device->kobj);
142 create_sysfs_device_files(device);
143}
144
Bjorn Helgaas06ea8e02006-04-27 05:25:00 -0400145static void acpi_device_unregister(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146{
147 spin_lock(&acpi_device_lock);
148 if (device->parent) {
149 list_del(&device->node);
150 list_del(&device->g_list);
151 } else
152 list_del(&device->g_list);
153
154 list_del(&device->wakeup_list);
155
156 spin_unlock(&acpi_device_lock);
157
158 acpi_detach_data(device->handle, acpi_bus_data_handler);
159 remove_sysfs_device_files(device);
160 kobject_unregister(&device->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161}
162
Len Brown4be44fc2005-08-05 00:44:28 -0400163void acpi_bus_data_handler(acpi_handle handle, u32 function, void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164{
165 ACPI_FUNCTION_TRACE("acpi_bus_data_handler");
166
167 /* TBD */
168
169 return_VOID;
170}
171
Len Brown4be44fc2005-08-05 00:44:28 -0400172static int acpi_bus_get_power_flags(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173{
Len Brown4be44fc2005-08-05 00:44:28 -0400174 acpi_status status = 0;
175 acpi_handle handle = NULL;
176 u32 i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
178 ACPI_FUNCTION_TRACE("acpi_bus_get_power_flags");
179
180 /*
181 * Power Management Flags
182 */
183 status = acpi_get_handle(device->handle, "_PSC", &handle);
184 if (ACPI_SUCCESS(status))
185 device->power.flags.explicit_get = 1;
186 status = acpi_get_handle(device->handle, "_IRC", &handle);
187 if (ACPI_SUCCESS(status))
188 device->power.flags.inrush_current = 1;
189
190 /*
191 * Enumerate supported power management states
192 */
193 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3; i++) {
194 struct acpi_device_power_state *ps = &device->power.states[i];
Len Brown4be44fc2005-08-05 00:44:28 -0400195 char object_name[5] = { '_', 'P', 'R', '0' + i, '\0' };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
197 /* Evaluate "_PRx" to se if power resources are referenced */
198 acpi_evaluate_reference(device->handle, object_name, NULL,
Len Brown4be44fc2005-08-05 00:44:28 -0400199 &ps->resources);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 if (ps->resources.count) {
201 device->power.flags.power_resources = 1;
202 ps->flags.valid = 1;
203 }
204
205 /* Evaluate "_PSx" to see if we can do explicit sets */
206 object_name[2] = 'S';
207 status = acpi_get_handle(device->handle, object_name, &handle);
208 if (ACPI_SUCCESS(status)) {
209 ps->flags.explicit_set = 1;
210 ps->flags.valid = 1;
211 }
212
213 /* State is valid if we have some power control */
214 if (ps->resources.count || ps->flags.explicit_set)
215 ps->flags.valid = 1;
216
Len Brown4be44fc2005-08-05 00:44:28 -0400217 ps->power = -1; /* Unknown - driver assigned */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 ps->latency = -1; /* Unknown - driver assigned */
219 }
220
221 /* Set defaults for D0 and D3 states (always valid) */
222 device->power.states[ACPI_STATE_D0].flags.valid = 1;
223 device->power.states[ACPI_STATE_D0].power = 100;
224 device->power.states[ACPI_STATE_D3].flags.valid = 1;
225 device->power.states[ACPI_STATE_D3].power = 0;
226
227 /* TBD: System wake support and resource requirements. */
228
229 device->power.state = ACPI_STATE_UNKNOWN;
230
231 return_VALUE(0);
232}
233
Len Brown4be44fc2005-08-05 00:44:28 -0400234int acpi_match_ids(struct acpi_device *device, char *ids)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 if (device->flags.hardware_id)
237 if (strstr(ids, device->pnp.hardware_id))
Bjorn Helgaas1a365612006-03-28 17:04:00 -0500238 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
240 if (device->flags.compatible_ids) {
241 struct acpi_compatible_id_list *cid_list = device->pnp.cid_list;
242 int i;
243
244 /* compare multiple _CID entries against driver ids */
Len Brown4be44fc2005-08-05 00:44:28 -0400245 for (i = 0; i < cid_list->count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 if (strstr(ids, cid_list->id[i].value))
Bjorn Helgaas1a365612006-03-28 17:04:00 -0500247 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 }
249 }
Bjorn Helgaas1a365612006-03-28 17:04:00 -0500250 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251}
252
253static acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400254acpi_bus_extract_wakeup_device_power_package(struct acpi_device *device,
255 union acpi_object *package)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256{
Len Brown4be44fc2005-08-05 00:44:28 -0400257 int i = 0;
258 union acpi_object *element = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
260 if (!device || !package || (package->package.count < 2))
261 return AE_BAD_PARAMETER;
262
263 element = &(package->package.elements[0]);
264 if (!element)
265 return AE_BAD_PARAMETER;
266 if (element->type == ACPI_TYPE_PACKAGE) {
267 if ((element->package.count < 2) ||
Len Brown4be44fc2005-08-05 00:44:28 -0400268 (element->package.elements[0].type !=
269 ACPI_TYPE_LOCAL_REFERENCE)
270 || (element->package.elements[1].type != ACPI_TYPE_INTEGER))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 return AE_BAD_DATA;
Len Brown4be44fc2005-08-05 00:44:28 -0400272 device->wakeup.gpe_device =
273 element->package.elements[0].reference.handle;
274 device->wakeup.gpe_number =
275 (u32) element->package.elements[1].integer.value;
276 } else if (element->type == ACPI_TYPE_INTEGER) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 device->wakeup.gpe_number = element->integer.value;
Len Brown4be44fc2005-08-05 00:44:28 -0400278 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 return AE_BAD_DATA;
280
281 element = &(package->package.elements[1]);
282 if (element->type != ACPI_TYPE_INTEGER) {
283 return AE_BAD_DATA;
284 }
285 device->wakeup.sleep_state = element->integer.value;
286
287 if ((package->package.count - 2) > ACPI_MAX_HANDLES) {
288 return AE_NO_MEMORY;
289 }
290 device->wakeup.resources.count = package->package.count - 2;
Len Brown4be44fc2005-08-05 00:44:28 -0400291 for (i = 0; i < device->wakeup.resources.count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 element = &(package->package.elements[i + 2]);
Len Brown4be44fc2005-08-05 00:44:28 -0400293 if (element->type != ACPI_TYPE_ANY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 return AE_BAD_DATA;
295 }
296
297 device->wakeup.resources.handles[i] = element->reference.handle;
298 }
299
300 return AE_OK;
301}
302
Len Brown4be44fc2005-08-05 00:44:28 -0400303static int acpi_bus_get_wakeup_device_flags(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304{
Len Brown4be44fc2005-08-05 00:44:28 -0400305 acpi_status status = 0;
306 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
307 union acpi_object *package = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
309 ACPI_FUNCTION_TRACE("acpi_bus_get_wakeup_flags");
310
311 /* _PRW */
312 status = acpi_evaluate_object(device->handle, "_PRW", NULL, &buffer);
313 if (ACPI_FAILURE(status)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -0400314 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PRW"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 goto end;
316 }
317
Len Brown4be44fc2005-08-05 00:44:28 -0400318 package = (union acpi_object *)buffer.pointer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 status = acpi_bus_extract_wakeup_device_power_package(device, package);
320 if (ACPI_FAILURE(status)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -0400321 ACPI_EXCEPTION((AE_INFO, status, "Extracting _PRW package"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 goto end;
323 }
324
325 acpi_os_free(buffer.pointer);
326
327 device->wakeup.flags.valid = 1;
Len Brown4be44fc2005-08-05 00:44:28 -0400328 /* Power button, Lid switch always enable wakeup */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 if (!acpi_match_ids(device, "PNP0C0D,PNP0C0C,PNP0C0E"))
330 device->wakeup.flags.run_wake = 1;
331
Len Brown4be44fc2005-08-05 00:44:28 -0400332 end:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 if (ACPI_FAILURE(status))
334 device->flags.wake_capable = 0;
335 return_VALUE(0);
336}
337
338/* --------------------------------------------------------------------------
Kay Sievers312c0042005-11-16 09:00:00 +0100339 ACPI sysfs device file support
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 -------------------------------------------------------------------------- */
Len Brown4be44fc2005-08-05 00:44:28 -0400341static ssize_t acpi_eject_store(struct acpi_device *device,
342 const char *buf, size_t count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
344#define ACPI_DEVICE_ATTR(_name,_mode,_show,_store) \
345static struct acpi_device_attribute acpi_device_attr_##_name = \
346 __ATTR(_name, _mode, _show, _store)
347
348ACPI_DEVICE_ATTR(eject, 0200, NULL, acpi_eject_store);
349
350/**
351 * setup_sys_fs_device_files - sets up the device files under device namespace
Martin Waitz67be2dd2005-05-01 08:59:26 -0700352 * @dev: acpi_device object
353 * @func: function pointer to create or destroy the device file
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 */
355static void
Len Brown4be44fc2005-08-05 00:44:28 -0400356setup_sys_fs_device_files(struct acpi_device *dev,
357 acpi_device_sysfs_files * func)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358{
Len Brown4be44fc2005-08-05 00:44:28 -0400359 acpi_status status;
360 acpi_handle temp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
362 /*
363 * If device has _EJ0, 'eject' file is created that is used to trigger
364 * hot-removal function from userland.
365 */
366 status = acpi_get_handle(dev->handle, "_EJ0", &temp);
367 if (ACPI_SUCCESS(status))
Len Brown4be44fc2005-08-05 00:44:28 -0400368 (*(func)) (&dev->kobj, &acpi_device_attr_eject.attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369}
370
Len Brown4be44fc2005-08-05 00:44:28 -0400371static int acpi_eject_operation(acpi_handle handle, int lockable)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372{
373 struct acpi_object_list arg_list;
374 union acpi_object arg;
375 acpi_status status = AE_OK;
376
377 /*
378 * TBD: evaluate _PS3?
379 */
380
381 if (lockable) {
382 arg_list.count = 1;
383 arg_list.pointer = &arg;
384 arg.type = ACPI_TYPE_INTEGER;
385 arg.integer.value = 0;
386 acpi_evaluate_object(handle, "_LCK", &arg_list, NULL);
387 }
388
389 arg_list.count = 1;
390 arg_list.pointer = &arg;
391 arg.type = ACPI_TYPE_INTEGER;
392 arg.integer.value = 1;
393
394 /*
395 * TBD: _EJD support.
396 */
397
398 status = acpi_evaluate_object(handle, "_EJ0", &arg_list, NULL);
399 if (ACPI_FAILURE(status)) {
Len Brown4be44fc2005-08-05 00:44:28 -0400400 return (-ENODEV);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 }
402
Len Brown4be44fc2005-08-05 00:44:28 -0400403 return (0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404}
405
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406static ssize_t
407acpi_eject_store(struct acpi_device *device, const char *buf, size_t count)
408{
Len Brown4be44fc2005-08-05 00:44:28 -0400409 int result;
410 int ret = count;
411 int islockable;
412 acpi_status status;
413 acpi_handle handle;
414 acpi_object_type type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
416 if ((!count) || (buf[0] != '1')) {
417 return -EINVAL;
418 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419#ifndef FORCE_EJECT
420 if (device->driver == NULL) {
421 ret = -ENODEV;
422 goto err;
423 }
424#endif
425 status = acpi_get_type(device->handle, &type);
Len Brown4be44fc2005-08-05 00:44:28 -0400426 if (ACPI_FAILURE(status) || (!device->flags.ejectable)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 ret = -ENODEV;
428 goto err;
429 }
430
431 islockable = device->flags.lockable;
432 handle = device->handle;
433
Ashok Rajeefa27a2006-03-28 17:04:00 -0500434 result = acpi_bus_trim(device, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
436 if (!result)
437 result = acpi_eject_operation(handle, islockable);
438
439 if (result) {
440 ret = -EBUSY;
441 }
Len Brown4be44fc2005-08-05 00:44:28 -0400442 err:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 return ret;
444}
445
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446/* --------------------------------------------------------------------------
447 Performance Management
448 -------------------------------------------------------------------------- */
449
Len Brown4be44fc2005-08-05 00:44:28 -0400450static int acpi_bus_get_perf_flags(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451{
452 device->performance.state = ACPI_STATE_UNKNOWN;
453 return 0;
454}
455
456/* --------------------------------------------------------------------------
457 Driver Management
458 -------------------------------------------------------------------------- */
459
460static LIST_HEAD(acpi_bus_drivers);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462/**
Randy Dunlapd758a8f2006-01-06 01:31:00 -0500463 * acpi_bus_match - match device IDs to driver's supported IDs
464 * @device: the device that we are trying to match to a driver
465 * @driver: driver whose device id table is being checked
466 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 * Checks the device's hardware (_HID) or compatible (_CID) ids to see if it
468 * matches the specified driver's criteria.
469 */
470static int
Len Brown4be44fc2005-08-05 00:44:28 -0400471acpi_bus_match(struct acpi_device *device, struct acpi_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472{
473 if (driver && driver->ops.match)
474 return driver->ops.match(device, driver);
475 return acpi_match_ids(device, driver->ids);
476}
477
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478/**
Randy Dunlapd758a8f2006-01-06 01:31:00 -0500479 * acpi_bus_driver_init - add a device to a driver
480 * @device: the device to add and initialize
481 * @driver: driver for the device
482 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 * Used to initialize a device via its device driver. Called whenever a
484 * driver is bound to a device. Invokes the driver's add() and start() ops.
485 */
486static int
Len Brown4be44fc2005-08-05 00:44:28 -0400487acpi_bus_driver_init(struct acpi_device *device, struct acpi_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488{
Len Brown4be44fc2005-08-05 00:44:28 -0400489 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
491 ACPI_FUNCTION_TRACE("acpi_bus_driver_init");
492
493 if (!device || !driver)
494 return_VALUE(-EINVAL);
495
496 if (!driver->ops.add)
497 return_VALUE(-ENOSYS);
498
499 result = driver->ops.add(device);
500 if (result) {
501 device->driver = NULL;
502 acpi_driver_data(device) = NULL;
503 return_VALUE(result);
504 }
505
506 device->driver = driver;
507
508 /*
509 * TBD - Configuration Management: Assign resources to device based
510 * upon possible configuration and currently allocated resources.
511 */
512
Len Brown4be44fc2005-08-05 00:44:28 -0400513 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
514 "Driver successfully bound to device\n"));
Rajesh Shah3fb02732005-04-28 00:25:52 -0700515 return_VALUE(0);
516}
517
Adrian Bunk8713cbe2005-09-02 17:16:48 -0400518static int acpi_start_single_object(struct acpi_device *device)
Rajesh Shah3fb02732005-04-28 00:25:52 -0700519{
520 int result = 0;
521 struct acpi_driver *driver;
522
523 ACPI_FUNCTION_TRACE("acpi_start_single_object");
524
525 if (!(driver = device->driver))
526 return_VALUE(0);
527
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 if (driver->ops.start) {
529 result = driver->ops.start(device);
530 if (result && driver->ops.remove)
531 driver->ops.remove(device, ACPI_BUS_REMOVAL_NORMAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 }
533
Rajesh Shah3fb02732005-04-28 00:25:52 -0700534 return_VALUE(result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535}
536
Bjorn Helgaas9d9f7492006-03-28 17:04:00 -0500537static void acpi_driver_attach(struct acpi_driver *drv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538{
Len Brown4be44fc2005-08-05 00:44:28 -0400539 struct list_head *node, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
541 ACPI_FUNCTION_TRACE("acpi_driver_attach");
542
543 spin_lock(&acpi_device_lock);
544 list_for_each_safe(node, next, &acpi_device_list) {
Len Brown4be44fc2005-08-05 00:44:28 -0400545 struct acpi_device *dev =
546 container_of(node, struct acpi_device, g_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
548 if (dev->driver || !dev->status.present)
549 continue;
550 spin_unlock(&acpi_device_lock);
551
552 if (!acpi_bus_match(dev, drv)) {
553 if (!acpi_bus_driver_init(dev, drv)) {
Rajesh Shah3fb02732005-04-28 00:25:52 -0700554 acpi_start_single_object(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 atomic_inc(&drv->references);
Len Brown4be44fc2005-08-05 00:44:28 -0400556 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
557 "Found driver [%s] for device [%s]\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 drv->name, dev->pnp.bus_id));
559 }
560 }
561 spin_lock(&acpi_device_lock);
562 }
563 spin_unlock(&acpi_device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564}
565
Bjorn Helgaas06ea8e02006-04-27 05:25:00 -0400566static void acpi_driver_detach(struct acpi_driver *drv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567{
Len Brown4be44fc2005-08-05 00:44:28 -0400568 struct list_head *node, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
570 ACPI_FUNCTION_TRACE("acpi_driver_detach");
571
572 spin_lock(&acpi_device_lock);
Len Brown4be44fc2005-08-05 00:44:28 -0400573 list_for_each_safe(node, next, &acpi_device_list) {
574 struct acpi_device *dev =
575 container_of(node, struct acpi_device, g_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
577 if (dev->driver == drv) {
578 spin_unlock(&acpi_device_lock);
579 if (drv->ops.remove)
Len Brown4be44fc2005-08-05 00:44:28 -0400580 drv->ops.remove(dev, ACPI_BUS_REMOVAL_NORMAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 spin_lock(&acpi_device_lock);
582 dev->driver = NULL;
583 dev->driver_data = NULL;
584 atomic_dec(&drv->references);
585 }
586 }
587 spin_unlock(&acpi_device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588}
589
590/**
Randy Dunlapd758a8f2006-01-06 01:31:00 -0500591 * acpi_bus_register_driver - register a driver with the ACPI bus
592 * @driver: driver being registered
593 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 * Registers a driver with the ACPI bus. Searches the namespace for all
Bjorn Helgaas9d9f7492006-03-28 17:04:00 -0500595 * devices that match the driver's criteria and binds. Returns zero for
596 * success or a negative error status for failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 */
Len Brown4be44fc2005-08-05 00:44:28 -0400598int acpi_bus_register_driver(struct acpi_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 ACPI_FUNCTION_TRACE("acpi_bus_register_driver");
601
602 if (acpi_disabled)
603 return_VALUE(-ENODEV);
604
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 spin_lock(&acpi_device_lock);
606 list_add_tail(&driver->node, &acpi_bus_drivers);
607 spin_unlock(&acpi_device_lock);
Bjorn Helgaas9d9f7492006-03-28 17:04:00 -0500608 acpi_driver_attach(driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
Bjorn Helgaas9d9f7492006-03-28 17:04:00 -0500610 return_VALUE(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
Len Brown4be44fc2005-08-05 00:44:28 -0400613EXPORT_SYMBOL(acpi_bus_register_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
615/**
Randy Dunlapd758a8f2006-01-06 01:31:00 -0500616 * acpi_bus_unregister_driver - unregisters a driver with the APIC bus
617 * @driver: driver to unregister
618 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 * Unregisters a driver with the ACPI bus. Searches the namespace for all
620 * devices that match the driver's criteria and unbinds.
621 */
Bjorn Helgaas06ea8e02006-04-27 05:25:00 -0400622void acpi_bus_unregister_driver(struct acpi_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623{
Bjorn Helgaas1a365612006-03-28 17:04:00 -0500624 acpi_driver_detach(driver);
625
626 if (!atomic_read(&driver->references)) {
627 spin_lock(&acpi_device_lock);
628 list_del_init(&driver->node);
629 spin_unlock(&acpi_device_lock);
630 }
Bjorn Helgaas06ea8e02006-04-27 05:25:00 -0400631 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632}
Len Brown4be44fc2005-08-05 00:44:28 -0400633
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634EXPORT_SYMBOL(acpi_bus_unregister_driver);
635
636/**
Randy Dunlapd758a8f2006-01-06 01:31:00 -0500637 * acpi_bus_find_driver - check if there is a driver installed for the device
638 * @device: device that we are trying to find a supporting driver for
639 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 * Parses the list of registered drivers looking for a driver applicable for
641 * the specified device.
642 */
Len Brown4be44fc2005-08-05 00:44:28 -0400643static int acpi_bus_find_driver(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644{
Len Brown4be44fc2005-08-05 00:44:28 -0400645 int result = 0;
646 struct list_head *node, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647
648 ACPI_FUNCTION_TRACE("acpi_bus_find_driver");
649
650 spin_lock(&acpi_device_lock);
Len Brown4be44fc2005-08-05 00:44:28 -0400651 list_for_each_safe(node, next, &acpi_bus_drivers) {
652 struct acpi_driver *driver =
653 container_of(node, struct acpi_driver, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
655 atomic_inc(&driver->references);
656 spin_unlock(&acpi_device_lock);
657 if (!acpi_bus_match(device, driver)) {
658 result = acpi_bus_driver_init(device, driver);
659 if (!result)
660 goto Done;
661 }
662 atomic_dec(&driver->references);
663 spin_lock(&acpi_device_lock);
664 }
665 spin_unlock(&acpi_device_lock);
666
Len Brown4be44fc2005-08-05 00:44:28 -0400667 Done:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 return_VALUE(result);
669}
670
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671/* --------------------------------------------------------------------------
672 Device Enumeration
673 -------------------------------------------------------------------------- */
674
Len Brown4be44fc2005-08-05 00:44:28 -0400675static int acpi_bus_get_flags(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676{
Len Brown4be44fc2005-08-05 00:44:28 -0400677 acpi_status status = AE_OK;
678 acpi_handle temp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679
680 ACPI_FUNCTION_TRACE("acpi_bus_get_flags");
681
682 /* Presence of _STA indicates 'dynamic_status' */
683 status = acpi_get_handle(device->handle, "_STA", &temp);
684 if (ACPI_SUCCESS(status))
685 device->flags.dynamic_status = 1;
686
687 /* Presence of _CID indicates 'compatible_ids' */
688 status = acpi_get_handle(device->handle, "_CID", &temp);
689 if (ACPI_SUCCESS(status))
690 device->flags.compatible_ids = 1;
691
692 /* Presence of _RMV indicates 'removable' */
693 status = acpi_get_handle(device->handle, "_RMV", &temp);
694 if (ACPI_SUCCESS(status))
695 device->flags.removable = 1;
696
697 /* Presence of _EJD|_EJ0 indicates 'ejectable' */
698 status = acpi_get_handle(device->handle, "_EJD", &temp);
699 if (ACPI_SUCCESS(status))
700 device->flags.ejectable = 1;
701 else {
702 status = acpi_get_handle(device->handle, "_EJ0", &temp);
703 if (ACPI_SUCCESS(status))
704 device->flags.ejectable = 1;
705 }
706
707 /* Presence of _LCK indicates 'lockable' */
708 status = acpi_get_handle(device->handle, "_LCK", &temp);
709 if (ACPI_SUCCESS(status))
710 device->flags.lockable = 1;
711
712 /* Presence of _PS0|_PR0 indicates 'power manageable' */
713 status = acpi_get_handle(device->handle, "_PS0", &temp);
714 if (ACPI_FAILURE(status))
715 status = acpi_get_handle(device->handle, "_PR0", &temp);
716 if (ACPI_SUCCESS(status))
717 device->flags.power_manageable = 1;
718
719 /* Presence of _PRW indicates wake capable */
720 status = acpi_get_handle(device->handle, "_PRW", &temp);
721 if (ACPI_SUCCESS(status))
722 device->flags.wake_capable = 1;
723
724 /* TBD: Peformance management */
725
726 return_VALUE(0);
727}
728
Len Brown4be44fc2005-08-05 00:44:28 -0400729static void acpi_device_get_busid(struct acpi_device *device,
730 acpi_handle handle, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731{
Len Brown4be44fc2005-08-05 00:44:28 -0400732 char bus_id[5] = { '?', 0 };
733 struct acpi_buffer buffer = { sizeof(bus_id), bus_id };
734 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735
736 /*
737 * Bus ID
738 * ------
739 * The device's Bus ID is simply the object name.
740 * TBD: Shouldn't this value be unique (within the ACPI namespace)?
741 */
742 switch (type) {
743 case ACPI_BUS_TYPE_SYSTEM:
744 strcpy(device->pnp.bus_id, "ACPI");
745 break;
746 case ACPI_BUS_TYPE_POWER_BUTTON:
747 strcpy(device->pnp.bus_id, "PWRF");
748 break;
749 case ACPI_BUS_TYPE_SLEEP_BUTTON:
750 strcpy(device->pnp.bus_id, "SLPF");
751 break;
752 default:
753 acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer);
754 /* Clean up trailing underscores (if any) */
755 for (i = 3; i > 1; i--) {
756 if (bus_id[i] == '_')
757 bus_id[i] = '\0';
758 else
759 break;
760 }
761 strcpy(device->pnp.bus_id, bus_id);
762 break;
763 }
764}
765
Len Brown4be44fc2005-08-05 00:44:28 -0400766static void acpi_device_set_id(struct acpi_device *device,
767 struct acpi_device *parent, acpi_handle handle,
768 int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769{
Len Brown4be44fc2005-08-05 00:44:28 -0400770 struct acpi_device_info *info;
771 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
772 char *hid = NULL;
773 char *uid = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 struct acpi_compatible_id_list *cid_list = NULL;
Len Brown4be44fc2005-08-05 00:44:28 -0400775 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776
777 switch (type) {
778 case ACPI_BUS_TYPE_DEVICE:
779 status = acpi_get_object_info(handle, &buffer);
780 if (ACPI_FAILURE(status)) {
Len Brown4be44fc2005-08-05 00:44:28 -0400781 printk("%s: Error reading device info\n", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 return;
783 }
784
785 info = buffer.pointer;
786 if (info->valid & ACPI_VALID_HID)
787 hid = info->hardware_id.value;
788 if (info->valid & ACPI_VALID_UID)
789 uid = info->unique_id.value;
790 if (info->valid & ACPI_VALID_CID)
791 cid_list = &info->compatibility_id;
792 if (info->valid & ACPI_VALID_ADR) {
793 device->pnp.bus_address = info->address;
794 device->flags.bus_address = 1;
795 }
796 break;
797 case ACPI_BUS_TYPE_POWER:
798 hid = ACPI_POWER_HID;
799 break;
800 case ACPI_BUS_TYPE_PROCESSOR:
801 hid = ACPI_PROCESSOR_HID;
802 break;
803 case ACPI_BUS_TYPE_SYSTEM:
804 hid = ACPI_SYSTEM_HID;
805 break;
806 case ACPI_BUS_TYPE_THERMAL:
807 hid = ACPI_THERMAL_HID;
808 break;
809 case ACPI_BUS_TYPE_POWER_BUTTON:
810 hid = ACPI_BUTTON_HID_POWERF;
811 break;
812 case ACPI_BUS_TYPE_SLEEP_BUTTON:
813 hid = ACPI_BUTTON_HID_SLEEPF;
814 break;
815 }
816
817 /*
818 * \_SB
819 * ----
820 * Fix for the system root bus device -- the only root-level device.
821 */
Bob Moorec51a4de2005-11-17 13:07:00 -0500822 if (((acpi_handle)parent == ACPI_ROOT_OBJECT) && (type == ACPI_BUS_TYPE_DEVICE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 hid = ACPI_BUS_HID;
824 strcpy(device->pnp.device_name, ACPI_BUS_DEVICE_NAME);
825 strcpy(device->pnp.device_class, ACPI_BUS_CLASS);
826 }
827
828 if (hid) {
829 strcpy(device->pnp.hardware_id, hid);
830 device->flags.hardware_id = 1;
831 }
832 if (uid) {
833 strcpy(device->pnp.unique_id, uid);
834 device->flags.unique_id = 1;
835 }
836 if (cid_list) {
837 device->pnp.cid_list = kmalloc(cid_list->size, GFP_KERNEL);
838 if (device->pnp.cid_list)
839 memcpy(device->pnp.cid_list, cid_list, cid_list->size);
840 else
841 printk(KERN_ERR "Memory allocation error\n");
842 }
843
844 acpi_os_free(buffer.pointer);
845}
846
Len Brown4be44fc2005-08-05 00:44:28 -0400847static int acpi_device_set_context(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848{
849 acpi_status status = AE_OK;
850 int result = 0;
851 /*
852 * Context
853 * -------
854 * Attach this 'struct acpi_device' to the ACPI object. This makes
855 * resolutions from handle->device very efficient. Note that we need
856 * to be careful with fixed-feature devices as they all attach to the
857 * root object.
858 */
Len Brown4be44fc2005-08-05 00:44:28 -0400859 if (type != ACPI_BUS_TYPE_POWER_BUTTON &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 type != ACPI_BUS_TYPE_SLEEP_BUTTON) {
861 status = acpi_attach_data(device->handle,
Len Brown4be44fc2005-08-05 00:44:28 -0400862 acpi_bus_data_handler, device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863
864 if (ACPI_FAILURE(status)) {
865 printk("Error attaching device data\n");
866 result = -ENODEV;
867 }
868 }
869 return result;
870}
871
Len Brown4be44fc2005-08-05 00:44:28 -0400872static void acpi_device_get_debug_info(struct acpi_device *device,
873 acpi_handle handle, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874{
875#ifdef CONFIG_ACPI_DEBUG_OUTPUT
Len Brown4be44fc2005-08-05 00:44:28 -0400876 char *type_string = NULL;
877 char name[80] = { '?', '\0' };
878 struct acpi_buffer buffer = { sizeof(name), name };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879
880 switch (type) {
881 case ACPI_BUS_TYPE_DEVICE:
882 type_string = "Device";
883 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
884 break;
885 case ACPI_BUS_TYPE_POWER:
886 type_string = "Power Resource";
887 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
888 break;
889 case ACPI_BUS_TYPE_PROCESSOR:
890 type_string = "Processor";
891 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
892 break;
893 case ACPI_BUS_TYPE_SYSTEM:
894 type_string = "System";
895 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
896 break;
897 case ACPI_BUS_TYPE_THERMAL:
898 type_string = "Thermal Zone";
899 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
900 break;
901 case ACPI_BUS_TYPE_POWER_BUTTON:
902 type_string = "Power Button";
903 sprintf(name, "PWRB");
904 break;
905 case ACPI_BUS_TYPE_SLEEP_BUTTON:
906 type_string = "Sleep Button";
907 sprintf(name, "SLPB");
908 break;
909 }
910
911 printk(KERN_DEBUG "Found %s %s [%p]\n", type_string, name, handle);
Len Brown4be44fc2005-08-05 00:44:28 -0400912#endif /*CONFIG_ACPI_DEBUG_OUTPUT */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913}
914
Len Brown4be44fc2005-08-05 00:44:28 -0400915static int acpi_bus_remove(struct acpi_device *dev, int rmdevice)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916{
Len Brown4be44fc2005-08-05 00:44:28 -0400917 int result = 0;
918 struct acpi_driver *driver;
919
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 ACPI_FUNCTION_TRACE("acpi_bus_remove");
921
922 if (!dev)
923 return_VALUE(-EINVAL);
924
925 driver = dev->driver;
926
927 if ((driver) && (driver->ops.remove)) {
928
929 if (driver->ops.stop) {
930 result = driver->ops.stop(dev, ACPI_BUS_REMOVAL_EJECT);
931 if (result)
932 return_VALUE(result);
933 }
934
935 result = dev->driver->ops.remove(dev, ACPI_BUS_REMOVAL_EJECT);
936 if (result) {
937 return_VALUE(result);
938 }
939
940 atomic_dec(&dev->driver->references);
941 dev->driver = NULL;
942 acpi_driver_data(dev) = NULL;
943 }
944
945 if (!rmdevice)
946 return_VALUE(0);
947
948 if (dev->flags.bus_address) {
949 if ((dev->parent) && (dev->parent->ops.unbind))
950 dev->parent->ops.unbind(dev);
951 }
Len Brown4be44fc2005-08-05 00:44:28 -0400952
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 acpi_device_unregister(dev, ACPI_BUS_REMOVAL_EJECT);
954
955 return_VALUE(0);
956}
957
Rajesh Shah3fb02732005-04-28 00:25:52 -0700958static int
Len Brown4be44fc2005-08-05 00:44:28 -0400959acpi_add_single_object(struct acpi_device **child,
960 struct acpi_device *parent, acpi_handle handle, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961{
Len Brown4be44fc2005-08-05 00:44:28 -0400962 int result = 0;
963 struct acpi_device *device = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964
Rajesh Shah3fb02732005-04-28 00:25:52 -0700965 ACPI_FUNCTION_TRACE("acpi_add_single_object");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966
967 if (!child)
968 return_VALUE(-EINVAL);
969
970 device = kmalloc(sizeof(struct acpi_device), GFP_KERNEL);
971 if (!device) {
Thomas Renningera6fc6722006-06-26 23:58:43 -0400972 ACPI_ERROR((AE_INFO, "Memory allocation error"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 return_VALUE(-ENOMEM);
974 }
975 memset(device, 0, sizeof(struct acpi_device));
976
977 device->handle = handle;
978 device->parent = parent;
979
Len Brown4be44fc2005-08-05 00:44:28 -0400980 acpi_device_get_busid(device, handle, type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981
982 /*
983 * Flags
984 * -----
985 * Get prior to calling acpi_bus_get_status() so we know whether
986 * or not _STA is present. Note that we only look for object
987 * handles -- cannot evaluate objects until we know the device is
988 * present and properly initialized.
989 */
990 result = acpi_bus_get_flags(device);
991 if (result)
992 goto end;
993
994 /*
995 * Status
996 * ------
Keiichiro Tokunaga6940fab2005-03-30 23:15:47 -0500997 * See if the device is present. We always assume that non-Device
998 * and non-Processor objects (e.g. thermal zones, power resources,
999 * etc.) are present, functioning, etc. (at least when parent object
1000 * is present). Note that _STA has a different meaning for some
1001 * objects (e.g. power resources) so we need to be careful how we use
1002 * it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 */
1004 switch (type) {
Keiichiro Tokunaga6940fab2005-03-30 23:15:47 -05001005 case ACPI_BUS_TYPE_PROCESSOR:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 case ACPI_BUS_TYPE_DEVICE:
1007 result = acpi_bus_get_status(device);
1008 if (ACPI_FAILURE(result) || !device->status.present) {
1009 result = -ENOENT;
1010 goto end;
1011 }
1012 break;
1013 default:
1014 STRUCT_TO_INT(device->status) = 0x0F;
1015 break;
1016 }
1017
1018 /*
1019 * Initialize Device
1020 * -----------------
1021 * TBD: Synch with Core's enumeration/initialization process.
1022 */
1023
1024 /*
1025 * Hardware ID, Unique ID, & Bus Address
1026 * -------------------------------------
1027 */
Len Brown4be44fc2005-08-05 00:44:28 -04001028 acpi_device_set_id(device, parent, handle, type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029
1030 /*
1031 * Power Management
1032 * ----------------
1033 */
1034 if (device->flags.power_manageable) {
1035 result = acpi_bus_get_power_flags(device);
1036 if (result)
1037 goto end;
1038 }
1039
Len Brown4be44fc2005-08-05 00:44:28 -04001040 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 * Wakeup device management
1042 *-----------------------
1043 */
1044 if (device->flags.wake_capable) {
1045 result = acpi_bus_get_wakeup_device_flags(device);
1046 if (result)
1047 goto end;
1048 }
1049
1050 /*
1051 * Performance Management
1052 * ----------------------
1053 */
1054 if (device->flags.performance_manageable) {
1055 result = acpi_bus_get_perf_flags(device);
1056 if (result)
1057 goto end;
1058 }
1059
Len Brown4be44fc2005-08-05 00:44:28 -04001060 if ((result = acpi_device_set_context(device, type)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 goto end;
1062
Len Brown4be44fc2005-08-05 00:44:28 -04001063 acpi_device_get_debug_info(device, handle, type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064
Len Brown4be44fc2005-08-05 00:44:28 -04001065 acpi_device_register(device, parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066
1067 /*
1068 * Bind _ADR-Based Devices
1069 * -----------------------
1070 * If there's a a bus address (_ADR) then we utilize the parent's
1071 * 'bind' function (if exists) to bind the ACPI- and natively-
1072 * enumerated device representations.
1073 */
1074 if (device->flags.bus_address) {
1075 if (device->parent && device->parent->ops.bind)
1076 device->parent->ops.bind(device);
1077 }
1078
1079 /*
1080 * Locate & Attach Driver
1081 * ----------------------
1082 * If there's a hardware id (_HID) or compatible ids (_CID) we check
1083 * to see if there's a driver installed for this kind of device. Note
1084 * that drivers can install before or after a device is enumerated.
1085 *
1086 * TBD: Assumes LDM provides driver hot-plug capability.
1087 */
Thomas Renningerbd7ce5b2005-10-03 10:39:00 -07001088 acpi_bus_find_driver(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089
Len Brown4be44fc2005-08-05 00:44:28 -04001090 end:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 if (!result)
1092 *child = device;
1093 else {
Jesper Juhl6044ec82005-11-07 01:01:32 -08001094 kfree(device->pnp.cid_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 kfree(device);
1096 }
1097
1098 return_VALUE(result);
1099}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100
Len Brown4be44fc2005-08-05 00:44:28 -04001101static int acpi_bus_scan(struct acpi_device *start, struct acpi_bus_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102{
Len Brown4be44fc2005-08-05 00:44:28 -04001103 acpi_status status = AE_OK;
1104 struct acpi_device *parent = NULL;
1105 struct acpi_device *child = NULL;
1106 acpi_handle phandle = NULL;
1107 acpi_handle chandle = NULL;
1108 acpi_object_type type = 0;
1109 u32 level = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110
1111 ACPI_FUNCTION_TRACE("acpi_bus_scan");
1112
1113 if (!start)
1114 return_VALUE(-EINVAL);
1115
1116 parent = start;
1117 phandle = start->handle;
Len Brown4be44fc2005-08-05 00:44:28 -04001118
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 /*
1120 * Parse through the ACPI namespace, identify all 'devices', and
1121 * create a new 'struct acpi_device' for each.
1122 */
1123 while ((level > 0) && parent) {
1124
1125 status = acpi_get_next_object(ACPI_TYPE_ANY, phandle,
Len Brown4be44fc2005-08-05 00:44:28 -04001126 chandle, &chandle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127
1128 /*
1129 * If this scope is exhausted then move our way back up.
1130 */
1131 if (ACPI_FAILURE(status)) {
1132 level--;
1133 chandle = phandle;
1134 acpi_get_parent(phandle, &phandle);
1135 if (parent->parent)
1136 parent = parent->parent;
1137 continue;
1138 }
1139
1140 status = acpi_get_type(chandle, &type);
1141 if (ACPI_FAILURE(status))
1142 continue;
1143
1144 /*
1145 * If this is a scope object then parse it (depth-first).
1146 */
1147 if (type == ACPI_TYPE_LOCAL_SCOPE) {
1148 level++;
1149 phandle = chandle;
1150 chandle = NULL;
1151 continue;
1152 }
1153
1154 /*
1155 * We're only interested in objects that we consider 'devices'.
1156 */
1157 switch (type) {
1158 case ACPI_TYPE_DEVICE:
1159 type = ACPI_BUS_TYPE_DEVICE;
1160 break;
1161 case ACPI_TYPE_PROCESSOR:
1162 type = ACPI_BUS_TYPE_PROCESSOR;
1163 break;
1164 case ACPI_TYPE_THERMAL:
1165 type = ACPI_BUS_TYPE_THERMAL;
1166 break;
1167 case ACPI_TYPE_POWER:
1168 type = ACPI_BUS_TYPE_POWER;
1169 break;
1170 default:
1171 continue;
1172 }
1173
Rajesh Shah3fb02732005-04-28 00:25:52 -07001174 if (ops->acpi_op_add)
1175 status = acpi_add_single_object(&child, parent,
Len Brown4be44fc2005-08-05 00:44:28 -04001176 chandle, type);
1177 else
Rajesh Shah3fb02732005-04-28 00:25:52 -07001178 status = acpi_bus_get_device(chandle, &child);
1179
Len Brown4be44fc2005-08-05 00:44:28 -04001180 if (ACPI_FAILURE(status))
1181 continue;
Rajesh Shah3fb02732005-04-28 00:25:52 -07001182
1183 if (ops->acpi_op_start) {
1184 status = acpi_start_single_object(child);
1185 if (ACPI_FAILURE(status))
1186 continue;
1187 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188
1189 /*
1190 * If the device is present, enabled, and functioning then
1191 * parse its scope (depth-first). Note that we need to
1192 * represent absent devices to facilitate PnP notifications
1193 * -- but only the subtree head (not all of its children,
1194 * which will be enumerated when the parent is inserted).
1195 *
1196 * TBD: Need notifications and other detection mechanisms
Len Brown4be44fc2005-08-05 00:44:28 -04001197 * in place before we can fully implement this.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 */
1199 if (child->status.present) {
1200 status = acpi_get_next_object(ACPI_TYPE_ANY, chandle,
1201 NULL, NULL);
1202 if (ACPI_SUCCESS(status)) {
1203 level++;
1204 phandle = chandle;
1205 chandle = NULL;
1206 parent = child;
1207 }
1208 }
1209 }
1210
1211 return_VALUE(0);
1212}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213
Rajesh Shah3fb02732005-04-28 00:25:52 -07001214int
Len Brown4be44fc2005-08-05 00:44:28 -04001215acpi_bus_add(struct acpi_device **child,
1216 struct acpi_device *parent, acpi_handle handle, int type)
Rajesh Shah3fb02732005-04-28 00:25:52 -07001217{
1218 int result;
1219 struct acpi_bus_ops ops;
1220
1221 ACPI_FUNCTION_TRACE("acpi_bus_add");
1222
1223 result = acpi_add_single_object(child, parent, handle, type);
1224 if (!result) {
1225 memset(&ops, 0, sizeof(ops));
1226 ops.acpi_op_add = 1;
1227 result = acpi_bus_scan(*child, &ops);
1228 }
1229 return_VALUE(result);
1230}
Len Brown4be44fc2005-08-05 00:44:28 -04001231
Rajesh Shah3fb02732005-04-28 00:25:52 -07001232EXPORT_SYMBOL(acpi_bus_add);
1233
Len Brown4be44fc2005-08-05 00:44:28 -04001234int acpi_bus_start(struct acpi_device *device)
Rajesh Shah3fb02732005-04-28 00:25:52 -07001235{
1236 int result;
1237 struct acpi_bus_ops ops;
1238
1239 ACPI_FUNCTION_TRACE("acpi_bus_start");
1240
1241 if (!device)
1242 return_VALUE(-EINVAL);
1243
1244 result = acpi_start_single_object(device);
1245 if (!result) {
1246 memset(&ops, 0, sizeof(ops));
1247 ops.acpi_op_start = 1;
1248 result = acpi_bus_scan(device, &ops);
1249 }
1250 return_VALUE(result);
1251}
Len Brown4be44fc2005-08-05 00:44:28 -04001252
Rajesh Shah3fb02732005-04-28 00:25:52 -07001253EXPORT_SYMBOL(acpi_bus_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254
Kristen Accardiceaba662006-02-23 17:56:01 -08001255int acpi_bus_trim(struct acpi_device *start, int rmdevice)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256{
Len Brown4be44fc2005-08-05 00:44:28 -04001257 acpi_status status;
1258 struct acpi_device *parent, *child;
1259 acpi_handle phandle, chandle;
1260 acpi_object_type type;
1261 u32 level = 1;
1262 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263
Len Brown4be44fc2005-08-05 00:44:28 -04001264 parent = start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 phandle = start->handle;
1266 child = chandle = NULL;
1267
1268 while ((level > 0) && parent && (!err)) {
1269 status = acpi_get_next_object(ACPI_TYPE_ANY, phandle,
Len Brown4be44fc2005-08-05 00:44:28 -04001270 chandle, &chandle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271
1272 /*
1273 * If this scope is exhausted then move our way back up.
1274 */
1275 if (ACPI_FAILURE(status)) {
1276 level--;
1277 chandle = phandle;
1278 acpi_get_parent(phandle, &phandle);
1279 child = parent;
1280 parent = parent->parent;
1281
1282 if (level == 0)
1283 err = acpi_bus_remove(child, rmdevice);
1284 else
1285 err = acpi_bus_remove(child, 1);
1286
1287 continue;
1288 }
1289
1290 status = acpi_get_type(chandle, &type);
1291 if (ACPI_FAILURE(status)) {
1292 continue;
1293 }
1294 /*
1295 * If there is a device corresponding to chandle then
1296 * parse it (depth-first).
1297 */
1298 if (acpi_bus_get_device(chandle, &child) == 0) {
1299 level++;
1300 phandle = chandle;
1301 chandle = NULL;
1302 parent = child;
1303 }
1304 continue;
1305 }
1306 return err;
1307}
Kristen Accardiceaba662006-02-23 17:56:01 -08001308EXPORT_SYMBOL_GPL(acpi_bus_trim);
1309
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310
Len Brown4be44fc2005-08-05 00:44:28 -04001311static int acpi_bus_scan_fixed(struct acpi_device *root)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312{
Len Brown4be44fc2005-08-05 00:44:28 -04001313 int result = 0;
1314 struct acpi_device *device = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315
1316 ACPI_FUNCTION_TRACE("acpi_bus_scan_fixed");
1317
1318 if (!root)
1319 return_VALUE(-ENODEV);
1320
1321 /*
1322 * Enumerate all fixed-feature devices.
1323 */
Rajesh Shah3fb02732005-04-28 00:25:52 -07001324 if (acpi_fadt.pwr_button == 0) {
1325 result = acpi_add_single_object(&device, acpi_root,
Len Brown4be44fc2005-08-05 00:44:28 -04001326 NULL,
1327 ACPI_BUS_TYPE_POWER_BUTTON);
Rajesh Shah3fb02732005-04-28 00:25:52 -07001328 if (!result)
1329 result = acpi_start_single_object(device);
1330 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331
Rajesh Shah3fb02732005-04-28 00:25:52 -07001332 if (acpi_fadt.sleep_button == 0) {
1333 result = acpi_add_single_object(&device, acpi_root,
Len Brown4be44fc2005-08-05 00:44:28 -04001334 NULL,
1335 ACPI_BUS_TYPE_SLEEP_BUTTON);
Rajesh Shah3fb02732005-04-28 00:25:52 -07001336 if (!result)
1337 result = acpi_start_single_object(device);
1338 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339
1340 return_VALUE(result);
1341}
1342
Patrick Mochel5b327262006-05-10 10:33:00 -04001343
Len Brown531881d2006-05-15 03:06:41 -04001344static inline struct acpi_device * to_acpi_dev(struct device * dev)
Patrick Mochel5b327262006-05-10 10:33:00 -04001345{
1346 return container_of(dev, struct acpi_device, dev);
1347}
1348
1349
Len Brown531881d2006-05-15 03:06:41 -04001350static int root_suspend(struct acpi_device * acpi_dev, pm_message_t state)
Patrick Mochel5b327262006-05-10 10:33:00 -04001351{
1352 struct acpi_device * dev, * next;
1353 int result;
1354
1355 spin_lock(&acpi_device_lock);
1356 list_for_each_entry_safe_reverse(dev, next, &acpi_device_list, g_list) {
1357 if (dev->driver && dev->driver->ops.suspend) {
1358 spin_unlock(&acpi_device_lock);
Patrick Mochel5b327262006-05-10 10:33:00 -04001359 result = dev->driver->ops.suspend(dev, 0);
1360 if (result) {
1361 printk(KERN_ERR PREFIX "[%s - %s] Suspend failed: %d\n",
1362 acpi_device_name(dev),
1363 acpi_device_bid(dev), result);
1364 }
1365 spin_lock(&acpi_device_lock);
1366 }
1367 }
1368 spin_unlock(&acpi_device_lock);
1369 return 0;
1370}
1371
1372
1373static int acpi_device_suspend(struct device * dev, pm_message_t state)
1374{
1375 struct acpi_device * acpi_dev = to_acpi_dev(dev);
1376
1377 /*
1378 * For now, we should only register 1 generic device -
1379 * the ACPI root device - and from there, we walk the
1380 * tree of ACPI devices to suspend each one using the
1381 * ACPI driver methods.
1382 */
1383 if (acpi_dev->handle == ACPI_ROOT_OBJECT)
Len Brown531881d2006-05-15 03:06:41 -04001384 root_suspend(acpi_dev, state);
Patrick Mochel5b327262006-05-10 10:33:00 -04001385 return 0;
1386}
1387
1388
1389
1390static int root_resume(struct acpi_device * acpi_dev)
1391{
1392 struct acpi_device * dev, * next;
1393 int result;
1394
1395 spin_lock(&acpi_device_lock);
1396 list_for_each_entry_safe(dev, next, &acpi_device_list, g_list) {
1397 if (dev->driver && dev->driver->ops.resume) {
1398 spin_unlock(&acpi_device_lock);
Patrick Mochel5b327262006-05-10 10:33:00 -04001399 result = dev->driver->ops.resume(dev, 0);
1400 if (result) {
1401 printk(KERN_ERR PREFIX "[%s - %s] resume failed: %d\n",
1402 acpi_device_name(dev),
1403 acpi_device_bid(dev), result);
1404 }
1405 spin_lock(&acpi_device_lock);
1406 }
1407 }
1408 spin_unlock(&acpi_device_lock);
1409 return 0;
1410}
1411
1412
1413static int acpi_device_resume(struct device * dev)
1414{
1415 struct acpi_device * acpi_dev = to_acpi_dev(dev);
1416
1417 /*
1418 * For now, we should only register 1 generic device -
1419 * the ACPI root device - and from there, we walk the
1420 * tree of ACPI devices to resume each one using the
1421 * ACPI driver methods.
1422 */
1423 if (acpi_dev->handle == ACPI_ROOT_OBJECT)
1424 root_resume(acpi_dev);
1425 return 0;
1426}
1427
1428
1429struct bus_type acpi_bus_type = {
1430 .name = "acpi",
1431 .suspend = acpi_device_suspend,
1432 .resume = acpi_device_resume,
1433};
1434
1435
1436
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437static int __init acpi_scan_init(void)
1438{
1439 int result;
Rajesh Shah3fb02732005-04-28 00:25:52 -07001440 struct acpi_bus_ops ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441
1442 ACPI_FUNCTION_TRACE("acpi_scan_init");
1443
1444 if (acpi_disabled)
1445 return_VALUE(0);
1446
1447 kset_register(&acpi_namespace_kset);
1448
Patrick Mochel5b327262006-05-10 10:33:00 -04001449 result = bus_register(&acpi_bus_type);
1450 if (result) {
1451 /* We don't want to quit even if we failed to add suspend/resume */
1452 printk(KERN_ERR PREFIX "Could not register bus type\n");
1453 }
1454
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 /*
1456 * Create the root device in the bus's device tree
1457 */
Rajesh Shah3fb02732005-04-28 00:25:52 -07001458 result = acpi_add_single_object(&acpi_root, NULL, ACPI_ROOT_OBJECT,
Len Brown4be44fc2005-08-05 00:44:28 -04001459 ACPI_BUS_TYPE_SYSTEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 if (result)
1461 goto Done;
1462
Rajesh Shah3fb02732005-04-28 00:25:52 -07001463 result = acpi_start_single_object(acpi_root);
Patrick Mochel5b327262006-05-10 10:33:00 -04001464 if (result)
1465 goto Done;
1466
1467 acpi_root->dev.bus = &acpi_bus_type;
1468 snprintf(acpi_root->dev.bus_id, BUS_ID_SIZE, "%s", acpi_bus_type.name);
1469 result = device_register(&acpi_root->dev);
1470 if (result) {
1471 /* We don't want to quit even if we failed to add suspend/resume */
1472 printk(KERN_ERR PREFIX "Could not register device\n");
1473 }
Rajesh Shah3fb02732005-04-28 00:25:52 -07001474
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 /*
1476 * Enumerate devices in the ACPI namespace.
1477 */
1478 result = acpi_bus_scan_fixed(acpi_root);
Rajesh Shah3fb02732005-04-28 00:25:52 -07001479 if (!result) {
1480 memset(&ops, 0, sizeof(ops));
1481 ops.acpi_op_add = 1;
1482 ops.acpi_op_start = 1;
1483 result = acpi_bus_scan(acpi_root, &ops);
1484 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485
1486 if (result)
1487 acpi_device_unregister(acpi_root, ACPI_BUS_REMOVAL_NORMAL);
1488
Len Brown4be44fc2005-08-05 00:44:28 -04001489 Done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 return_VALUE(result);
1491}
1492
1493subsys_initcall(acpi_scan_init);