blob: 5fcb50c7b77802d135cbbe1368f96e8fabd6877e [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 Helgaas06ea8e082006-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{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
166 /* TBD */
167
Patrick Mocheld550d982006-06-27 00:41:40 -0400168 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169}
170
Len Brown4be44fc2005-08-05 00:44:28 -0400171static int acpi_bus_get_power_flags(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172{
Len Brown4be44fc2005-08-05 00:44:28 -0400173 acpi_status status = 0;
174 acpi_handle handle = NULL;
175 u32 i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
178 /*
179 * Power Management Flags
180 */
181 status = acpi_get_handle(device->handle, "_PSC", &handle);
182 if (ACPI_SUCCESS(status))
183 device->power.flags.explicit_get = 1;
184 status = acpi_get_handle(device->handle, "_IRC", &handle);
185 if (ACPI_SUCCESS(status))
186 device->power.flags.inrush_current = 1;
187
188 /*
189 * Enumerate supported power management states
190 */
191 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3; i++) {
192 struct acpi_device_power_state *ps = &device->power.states[i];
Len Brown4be44fc2005-08-05 00:44:28 -0400193 char object_name[5] = { '_', 'P', 'R', '0' + i, '\0' };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
195 /* Evaluate "_PRx" to se if power resources are referenced */
196 acpi_evaluate_reference(device->handle, object_name, NULL,
Len Brown4be44fc2005-08-05 00:44:28 -0400197 &ps->resources);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 if (ps->resources.count) {
199 device->power.flags.power_resources = 1;
200 ps->flags.valid = 1;
201 }
202
203 /* Evaluate "_PSx" to see if we can do explicit sets */
204 object_name[2] = 'S';
205 status = acpi_get_handle(device->handle, object_name, &handle);
206 if (ACPI_SUCCESS(status)) {
207 ps->flags.explicit_set = 1;
208 ps->flags.valid = 1;
209 }
210
211 /* State is valid if we have some power control */
212 if (ps->resources.count || ps->flags.explicit_set)
213 ps->flags.valid = 1;
214
Len Brown4be44fc2005-08-05 00:44:28 -0400215 ps->power = -1; /* Unknown - driver assigned */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 ps->latency = -1; /* Unknown - driver assigned */
217 }
218
219 /* Set defaults for D0 and D3 states (always valid) */
220 device->power.states[ACPI_STATE_D0].flags.valid = 1;
221 device->power.states[ACPI_STATE_D0].power = 100;
222 device->power.states[ACPI_STATE_D3].flags.valid = 1;
223 device->power.states[ACPI_STATE_D3].power = 0;
224
225 /* TBD: System wake support and resource requirements. */
226
227 device->power.state = ACPI_STATE_UNKNOWN;
228
Patrick Mocheld550d982006-06-27 00:41:40 -0400229 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230}
231
Len Brown4be44fc2005-08-05 00:44:28 -0400232int acpi_match_ids(struct acpi_device *device, char *ids)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 if (device->flags.hardware_id)
235 if (strstr(ids, device->pnp.hardware_id))
Bjorn Helgaas1a365612006-03-28 17:04:00 -0500236 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
238 if (device->flags.compatible_ids) {
239 struct acpi_compatible_id_list *cid_list = device->pnp.cid_list;
240 int i;
241
242 /* compare multiple _CID entries against driver ids */
Len Brown4be44fc2005-08-05 00:44:28 -0400243 for (i = 0; i < cid_list->count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 if (strstr(ids, cid_list->id[i].value))
Bjorn Helgaas1a365612006-03-28 17:04:00 -0500245 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 }
247 }
Bjorn Helgaas1a365612006-03-28 17:04:00 -0500248 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249}
250
251static acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400252acpi_bus_extract_wakeup_device_power_package(struct acpi_device *device,
253 union acpi_object *package)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254{
Len Brown4be44fc2005-08-05 00:44:28 -0400255 int i = 0;
256 union acpi_object *element = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
258 if (!device || !package || (package->package.count < 2))
259 return AE_BAD_PARAMETER;
260
261 element = &(package->package.elements[0]);
262 if (!element)
263 return AE_BAD_PARAMETER;
264 if (element->type == ACPI_TYPE_PACKAGE) {
265 if ((element->package.count < 2) ||
Len Brown4be44fc2005-08-05 00:44:28 -0400266 (element->package.elements[0].type !=
267 ACPI_TYPE_LOCAL_REFERENCE)
268 || (element->package.elements[1].type != ACPI_TYPE_INTEGER))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 return AE_BAD_DATA;
Len Brown4be44fc2005-08-05 00:44:28 -0400270 device->wakeup.gpe_device =
271 element->package.elements[0].reference.handle;
272 device->wakeup.gpe_number =
273 (u32) element->package.elements[1].integer.value;
274 } else if (element->type == ACPI_TYPE_INTEGER) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 device->wakeup.gpe_number = element->integer.value;
Len Brown4be44fc2005-08-05 00:44:28 -0400276 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 return AE_BAD_DATA;
278
279 element = &(package->package.elements[1]);
280 if (element->type != ACPI_TYPE_INTEGER) {
281 return AE_BAD_DATA;
282 }
283 device->wakeup.sleep_state = element->integer.value;
284
285 if ((package->package.count - 2) > ACPI_MAX_HANDLES) {
286 return AE_NO_MEMORY;
287 }
288 device->wakeup.resources.count = package->package.count - 2;
Len Brown4be44fc2005-08-05 00:44:28 -0400289 for (i = 0; i < device->wakeup.resources.count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 element = &(package->package.elements[i + 2]);
Len Brown4be44fc2005-08-05 00:44:28 -0400291 if (element->type != ACPI_TYPE_ANY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 return AE_BAD_DATA;
293 }
294
295 device->wakeup.resources.handles[i] = element->reference.handle;
296 }
297
298 return AE_OK;
299}
300
Len Brown4be44fc2005-08-05 00:44:28 -0400301static int acpi_bus_get_wakeup_device_flags(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302{
Len Brown4be44fc2005-08-05 00:44:28 -0400303 acpi_status status = 0;
304 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
305 union acpi_object *package = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
308 /* _PRW */
309 status = acpi_evaluate_object(device->handle, "_PRW", NULL, &buffer);
310 if (ACPI_FAILURE(status)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -0400311 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PRW"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 goto end;
313 }
314
Len Brown4be44fc2005-08-05 00:44:28 -0400315 package = (union acpi_object *)buffer.pointer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 status = acpi_bus_extract_wakeup_device_power_package(device, package);
317 if (ACPI_FAILURE(status)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -0400318 ACPI_EXCEPTION((AE_INFO, status, "Extracting _PRW package"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 goto end;
320 }
321
Len Brown02438d82006-06-30 03:19:10 -0400322 kfree(buffer.pointer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
324 device->wakeup.flags.valid = 1;
Len Brown4be44fc2005-08-05 00:44:28 -0400325 /* Power button, Lid switch always enable wakeup */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 if (!acpi_match_ids(device, "PNP0C0D,PNP0C0C,PNP0C0E"))
327 device->wakeup.flags.run_wake = 1;
328
Len Brown4be44fc2005-08-05 00:44:28 -0400329 end:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 if (ACPI_FAILURE(status))
331 device->flags.wake_capable = 0;
Patrick Mocheld550d982006-06-27 00:41:40 -0400332 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333}
334
335/* --------------------------------------------------------------------------
Kay Sievers312c0042005-11-16 09:00:00 +0100336 ACPI sysfs device file support
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 -------------------------------------------------------------------------- */
Len Brown4be44fc2005-08-05 00:44:28 -0400338static ssize_t acpi_eject_store(struct acpi_device *device,
339 const char *buf, size_t count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
341#define ACPI_DEVICE_ATTR(_name,_mode,_show,_store) \
342static struct acpi_device_attribute acpi_device_attr_##_name = \
343 __ATTR(_name, _mode, _show, _store)
344
345ACPI_DEVICE_ATTR(eject, 0200, NULL, acpi_eject_store);
346
347/**
348 * setup_sys_fs_device_files - sets up the device files under device namespace
Martin Waitz67be2dd2005-05-01 08:59:26 -0700349 * @dev: acpi_device object
350 * @func: function pointer to create or destroy the device file
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 */
352static void
Len Brown4be44fc2005-08-05 00:44:28 -0400353setup_sys_fs_device_files(struct acpi_device *dev,
354 acpi_device_sysfs_files * func)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355{
Len Brown4be44fc2005-08-05 00:44:28 -0400356 acpi_status status;
357 acpi_handle temp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
359 /*
360 * If device has _EJ0, 'eject' file is created that is used to trigger
361 * hot-removal function from userland.
362 */
363 status = acpi_get_handle(dev->handle, "_EJ0", &temp);
364 if (ACPI_SUCCESS(status))
Len Brown4be44fc2005-08-05 00:44:28 -0400365 (*(func)) (&dev->kobj, &acpi_device_attr_eject.attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366}
367
Len Brown4be44fc2005-08-05 00:44:28 -0400368static int acpi_eject_operation(acpi_handle handle, int lockable)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369{
370 struct acpi_object_list arg_list;
371 union acpi_object arg;
372 acpi_status status = AE_OK;
373
374 /*
375 * TBD: evaluate _PS3?
376 */
377
378 if (lockable) {
379 arg_list.count = 1;
380 arg_list.pointer = &arg;
381 arg.type = ACPI_TYPE_INTEGER;
382 arg.integer.value = 0;
383 acpi_evaluate_object(handle, "_LCK", &arg_list, NULL);
384 }
385
386 arg_list.count = 1;
387 arg_list.pointer = &arg;
388 arg.type = ACPI_TYPE_INTEGER;
389 arg.integer.value = 1;
390
391 /*
392 * TBD: _EJD support.
393 */
394
395 status = acpi_evaluate_object(handle, "_EJ0", &arg_list, NULL);
396 if (ACPI_FAILURE(status)) {
Len Brown4be44fc2005-08-05 00:44:28 -0400397 return (-ENODEV);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 }
399
Len Brown4be44fc2005-08-05 00:44:28 -0400400 return (0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401}
402
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403static ssize_t
404acpi_eject_store(struct acpi_device *device, const char *buf, size_t count)
405{
Len Brown4be44fc2005-08-05 00:44:28 -0400406 int result;
407 int ret = count;
408 int islockable;
409 acpi_status status;
410 acpi_handle handle;
411 acpi_object_type type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
413 if ((!count) || (buf[0] != '1')) {
414 return -EINVAL;
415 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416#ifndef FORCE_EJECT
417 if (device->driver == NULL) {
418 ret = -ENODEV;
419 goto err;
420 }
421#endif
422 status = acpi_get_type(device->handle, &type);
Len Brown4be44fc2005-08-05 00:44:28 -0400423 if (ACPI_FAILURE(status) || (!device->flags.ejectable)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 ret = -ENODEV;
425 goto err;
426 }
427
428 islockable = device->flags.lockable;
429 handle = device->handle;
430
Ashok Rajeefa27a2006-03-28 17:04:00 -0500431 result = acpi_bus_trim(device, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
433 if (!result)
434 result = acpi_eject_operation(handle, islockable);
435
436 if (result) {
437 ret = -EBUSY;
438 }
Len Brown4be44fc2005-08-05 00:44:28 -0400439 err:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 return ret;
441}
442
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443/* --------------------------------------------------------------------------
444 Performance Management
445 -------------------------------------------------------------------------- */
446
Len Brown4be44fc2005-08-05 00:44:28 -0400447static int acpi_bus_get_perf_flags(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448{
449 device->performance.state = ACPI_STATE_UNKNOWN;
450 return 0;
451}
452
453/* --------------------------------------------------------------------------
454 Driver Management
455 -------------------------------------------------------------------------- */
456
457static LIST_HEAD(acpi_bus_drivers);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459/**
Randy Dunlapd758a8f2006-01-06 01:31:00 -0500460 * acpi_bus_match - match device IDs to driver's supported IDs
461 * @device: the device that we are trying to match to a driver
462 * @driver: driver whose device id table is being checked
463 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 * Checks the device's hardware (_HID) or compatible (_CID) ids to see if it
465 * matches the specified driver's criteria.
466 */
467static int
Len Brown4be44fc2005-08-05 00:44:28 -0400468acpi_bus_match(struct acpi_device *device, struct acpi_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469{
470 if (driver && driver->ops.match)
471 return driver->ops.match(device, driver);
472 return acpi_match_ids(device, driver->ids);
473}
474
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475/**
Randy Dunlapd758a8f2006-01-06 01:31:00 -0500476 * acpi_bus_driver_init - add a device to a driver
477 * @device: the device to add and initialize
478 * @driver: driver for the device
479 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 * Used to initialize a device via its device driver. Called whenever a
481 * driver is bound to a device. Invokes the driver's add() and start() ops.
482 */
483static int
Len Brown4be44fc2005-08-05 00:44:28 -0400484acpi_bus_driver_init(struct acpi_device *device, struct acpi_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485{
Len Brown4be44fc2005-08-05 00:44:28 -0400486 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
489 if (!device || !driver)
Patrick Mocheld550d982006-06-27 00:41:40 -0400490 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
492 if (!driver->ops.add)
Patrick Mocheld550d982006-06-27 00:41:40 -0400493 return -ENOSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
495 result = driver->ops.add(device);
496 if (result) {
497 device->driver = NULL;
498 acpi_driver_data(device) = NULL;
Patrick Mocheld550d982006-06-27 00:41:40 -0400499 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 }
501
502 device->driver = driver;
503
504 /*
505 * TBD - Configuration Management: Assign resources to device based
506 * upon possible configuration and currently allocated resources.
507 */
508
Len Brown4be44fc2005-08-05 00:44:28 -0400509 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
510 "Driver successfully bound to device\n"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400511 return 0;
Rajesh Shah3fb02732005-04-28 00:25:52 -0700512}
513
Adrian Bunk8713cbe2005-09-02 17:16:48 -0400514static int acpi_start_single_object(struct acpi_device *device)
Rajesh Shah3fb02732005-04-28 00:25:52 -0700515{
516 int result = 0;
517 struct acpi_driver *driver;
518
Rajesh Shah3fb02732005-04-28 00:25:52 -0700519
520 if (!(driver = device->driver))
Patrick Mocheld550d982006-06-27 00:41:40 -0400521 return 0;
Rajesh Shah3fb02732005-04-28 00:25:52 -0700522
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 if (driver->ops.start) {
524 result = driver->ops.start(device);
525 if (result && driver->ops.remove)
526 driver->ops.remove(device, ACPI_BUS_REMOVAL_NORMAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 }
528
Patrick Mocheld550d982006-06-27 00:41:40 -0400529 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530}
531
Bjorn Helgaas9d9f7492006-03-28 17:04:00 -0500532static void acpi_driver_attach(struct acpi_driver *drv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533{
Len Brown4be44fc2005-08-05 00:44:28 -0400534 struct list_head *node, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
537 spin_lock(&acpi_device_lock);
538 list_for_each_safe(node, next, &acpi_device_list) {
Len Brown4be44fc2005-08-05 00:44:28 -0400539 struct acpi_device *dev =
540 container_of(node, struct acpi_device, g_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
542 if (dev->driver || !dev->status.present)
543 continue;
544 spin_unlock(&acpi_device_lock);
545
546 if (!acpi_bus_match(dev, drv)) {
547 if (!acpi_bus_driver_init(dev, drv)) {
Rajesh Shah3fb02732005-04-28 00:25:52 -0700548 acpi_start_single_object(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 atomic_inc(&drv->references);
Len Brown4be44fc2005-08-05 00:44:28 -0400550 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
551 "Found driver [%s] for device [%s]\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 drv->name, dev->pnp.bus_id));
553 }
554 }
555 spin_lock(&acpi_device_lock);
556 }
557 spin_unlock(&acpi_device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558}
559
Bjorn Helgaas06ea8e082006-04-27 05:25:00 -0400560static void acpi_driver_detach(struct acpi_driver *drv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561{
Len Brown4be44fc2005-08-05 00:44:28 -0400562 struct list_head *node, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
565 spin_lock(&acpi_device_lock);
Len Brown4be44fc2005-08-05 00:44:28 -0400566 list_for_each_safe(node, next, &acpi_device_list) {
567 struct acpi_device *dev =
568 container_of(node, struct acpi_device, g_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
570 if (dev->driver == drv) {
571 spin_unlock(&acpi_device_lock);
572 if (drv->ops.remove)
Len Brown4be44fc2005-08-05 00:44:28 -0400573 drv->ops.remove(dev, ACPI_BUS_REMOVAL_NORMAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 spin_lock(&acpi_device_lock);
575 dev->driver = NULL;
576 dev->driver_data = NULL;
577 atomic_dec(&drv->references);
578 }
579 }
580 spin_unlock(&acpi_device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581}
582
583/**
Randy Dunlapd758a8f2006-01-06 01:31:00 -0500584 * acpi_bus_register_driver - register a driver with the ACPI bus
585 * @driver: driver being registered
586 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 * Registers a driver with the ACPI bus. Searches the namespace for all
Bjorn Helgaas9d9f7492006-03-28 17:04:00 -0500588 * devices that match the driver's criteria and binds. Returns zero for
589 * success or a negative error status for failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 */
Len Brown4be44fc2005-08-05 00:44:28 -0400591int acpi_bus_register_driver(struct acpi_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
594 if (acpi_disabled)
Patrick Mocheld550d982006-06-27 00:41:40 -0400595 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 spin_lock(&acpi_device_lock);
598 list_add_tail(&driver->node, &acpi_bus_drivers);
599 spin_unlock(&acpi_device_lock);
Bjorn Helgaas9d9f7492006-03-28 17:04:00 -0500600 acpi_driver_attach(driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
Patrick Mocheld550d982006-06-27 00:41:40 -0400602 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
Len Brown4be44fc2005-08-05 00:44:28 -0400605EXPORT_SYMBOL(acpi_bus_register_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
607/**
Randy Dunlapd758a8f2006-01-06 01:31:00 -0500608 * acpi_bus_unregister_driver - unregisters a driver with the APIC bus
609 * @driver: driver to unregister
610 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 * Unregisters a driver with the ACPI bus. Searches the namespace for all
612 * devices that match the driver's criteria and unbinds.
613 */
Bjorn Helgaas06ea8e082006-04-27 05:25:00 -0400614void acpi_bus_unregister_driver(struct acpi_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615{
Bjorn Helgaas1a365612006-03-28 17:04:00 -0500616 acpi_driver_detach(driver);
617
618 if (!atomic_read(&driver->references)) {
619 spin_lock(&acpi_device_lock);
620 list_del_init(&driver->node);
621 spin_unlock(&acpi_device_lock);
622 }
Bjorn Helgaas06ea8e082006-04-27 05:25:00 -0400623 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624}
Len Brown4be44fc2005-08-05 00:44:28 -0400625
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626EXPORT_SYMBOL(acpi_bus_unregister_driver);
627
628/**
Randy Dunlapd758a8f2006-01-06 01:31:00 -0500629 * acpi_bus_find_driver - check if there is a driver installed for the device
630 * @device: device that we are trying to find a supporting driver for
631 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 * Parses the list of registered drivers looking for a driver applicable for
633 * the specified device.
634 */
Len Brown4be44fc2005-08-05 00:44:28 -0400635static int acpi_bus_find_driver(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636{
Len Brown4be44fc2005-08-05 00:44:28 -0400637 int result = 0;
638 struct list_head *node, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640
641 spin_lock(&acpi_device_lock);
Len Brown4be44fc2005-08-05 00:44:28 -0400642 list_for_each_safe(node, next, &acpi_bus_drivers) {
643 struct acpi_driver *driver =
644 container_of(node, struct acpi_driver, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645
646 atomic_inc(&driver->references);
647 spin_unlock(&acpi_device_lock);
648 if (!acpi_bus_match(device, driver)) {
649 result = acpi_bus_driver_init(device, driver);
650 if (!result)
651 goto Done;
652 }
653 atomic_dec(&driver->references);
654 spin_lock(&acpi_device_lock);
655 }
656 spin_unlock(&acpi_device_lock);
657
Len Brown4be44fc2005-08-05 00:44:28 -0400658 Done:
Patrick Mocheld550d982006-06-27 00:41:40 -0400659 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660}
661
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662/* --------------------------------------------------------------------------
663 Device Enumeration
664 -------------------------------------------------------------------------- */
665
Len Brownc8f7a62c2006-07-09 17:22:28 -0400666acpi_status
667acpi_bus_get_ejd(acpi_handle handle, acpi_handle *ejd)
668{
669 acpi_status status;
670 acpi_handle tmp;
671 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
672 union acpi_object *obj;
673
674 status = acpi_get_handle(handle, "_EJD", &tmp);
675 if (ACPI_FAILURE(status))
676 return status;
677
678 status = acpi_evaluate_object(handle, "_EJD", NULL, &buffer);
679 if (ACPI_SUCCESS(status)) {
680 obj = buffer.pointer;
681 status = acpi_get_handle(NULL, obj->string.pointer, ejd);
682 kfree(buffer.pointer);
683 }
684 return status;
685}
686EXPORT_SYMBOL_GPL(acpi_bus_get_ejd);
687
688
Len Brown4be44fc2005-08-05 00:44:28 -0400689static int acpi_bus_get_flags(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690{
Len Brown4be44fc2005-08-05 00:44:28 -0400691 acpi_status status = AE_OK;
692 acpi_handle temp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
695 /* Presence of _STA indicates 'dynamic_status' */
696 status = acpi_get_handle(device->handle, "_STA", &temp);
697 if (ACPI_SUCCESS(status))
698 device->flags.dynamic_status = 1;
699
700 /* Presence of _CID indicates 'compatible_ids' */
701 status = acpi_get_handle(device->handle, "_CID", &temp);
702 if (ACPI_SUCCESS(status))
703 device->flags.compatible_ids = 1;
704
705 /* Presence of _RMV indicates 'removable' */
706 status = acpi_get_handle(device->handle, "_RMV", &temp);
707 if (ACPI_SUCCESS(status))
708 device->flags.removable = 1;
709
710 /* Presence of _EJD|_EJ0 indicates 'ejectable' */
711 status = acpi_get_handle(device->handle, "_EJD", &temp);
712 if (ACPI_SUCCESS(status))
713 device->flags.ejectable = 1;
714 else {
715 status = acpi_get_handle(device->handle, "_EJ0", &temp);
716 if (ACPI_SUCCESS(status))
717 device->flags.ejectable = 1;
718 }
719
720 /* Presence of _LCK indicates 'lockable' */
721 status = acpi_get_handle(device->handle, "_LCK", &temp);
722 if (ACPI_SUCCESS(status))
723 device->flags.lockable = 1;
724
725 /* Presence of _PS0|_PR0 indicates 'power manageable' */
726 status = acpi_get_handle(device->handle, "_PS0", &temp);
727 if (ACPI_FAILURE(status))
728 status = acpi_get_handle(device->handle, "_PR0", &temp);
729 if (ACPI_SUCCESS(status))
730 device->flags.power_manageable = 1;
731
732 /* Presence of _PRW indicates wake capable */
733 status = acpi_get_handle(device->handle, "_PRW", &temp);
734 if (ACPI_SUCCESS(status))
735 device->flags.wake_capable = 1;
736
737 /* TBD: Peformance management */
738
Patrick Mocheld550d982006-06-27 00:41:40 -0400739 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740}
741
Len Brown4be44fc2005-08-05 00:44:28 -0400742static void acpi_device_get_busid(struct acpi_device *device,
743 acpi_handle handle, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744{
Len Brown4be44fc2005-08-05 00:44:28 -0400745 char bus_id[5] = { '?', 0 };
746 struct acpi_buffer buffer = { sizeof(bus_id), bus_id };
747 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748
749 /*
750 * Bus ID
751 * ------
752 * The device's Bus ID is simply the object name.
753 * TBD: Shouldn't this value be unique (within the ACPI namespace)?
754 */
755 switch (type) {
756 case ACPI_BUS_TYPE_SYSTEM:
757 strcpy(device->pnp.bus_id, "ACPI");
758 break;
759 case ACPI_BUS_TYPE_POWER_BUTTON:
760 strcpy(device->pnp.bus_id, "PWRF");
761 break;
762 case ACPI_BUS_TYPE_SLEEP_BUTTON:
763 strcpy(device->pnp.bus_id, "SLPF");
764 break;
765 default:
766 acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer);
767 /* Clean up trailing underscores (if any) */
768 for (i = 3; i > 1; i--) {
769 if (bus_id[i] == '_')
770 bus_id[i] = '\0';
771 else
772 break;
773 }
774 strcpy(device->pnp.bus_id, bus_id);
775 break;
776 }
777}
778
Len Brown4be44fc2005-08-05 00:44:28 -0400779static void acpi_device_set_id(struct acpi_device *device,
780 struct acpi_device *parent, acpi_handle handle,
781 int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782{
Len Brown4be44fc2005-08-05 00:44:28 -0400783 struct acpi_device_info *info;
784 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
785 char *hid = NULL;
786 char *uid = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 struct acpi_compatible_id_list *cid_list = NULL;
Len Brown4be44fc2005-08-05 00:44:28 -0400788 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789
790 switch (type) {
791 case ACPI_BUS_TYPE_DEVICE:
792 status = acpi_get_object_info(handle, &buffer);
793 if (ACPI_FAILURE(status)) {
Len Brown4be44fc2005-08-05 00:44:28 -0400794 printk("%s: Error reading device info\n", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 return;
796 }
797
798 info = buffer.pointer;
799 if (info->valid & ACPI_VALID_HID)
800 hid = info->hardware_id.value;
801 if (info->valid & ACPI_VALID_UID)
802 uid = info->unique_id.value;
803 if (info->valid & ACPI_VALID_CID)
804 cid_list = &info->compatibility_id;
805 if (info->valid & ACPI_VALID_ADR) {
806 device->pnp.bus_address = info->address;
807 device->flags.bus_address = 1;
808 }
809 break;
810 case ACPI_BUS_TYPE_POWER:
811 hid = ACPI_POWER_HID;
812 break;
813 case ACPI_BUS_TYPE_PROCESSOR:
814 hid = ACPI_PROCESSOR_HID;
815 break;
816 case ACPI_BUS_TYPE_SYSTEM:
817 hid = ACPI_SYSTEM_HID;
818 break;
819 case ACPI_BUS_TYPE_THERMAL:
820 hid = ACPI_THERMAL_HID;
821 break;
822 case ACPI_BUS_TYPE_POWER_BUTTON:
823 hid = ACPI_BUTTON_HID_POWERF;
824 break;
825 case ACPI_BUS_TYPE_SLEEP_BUTTON:
826 hid = ACPI_BUTTON_HID_SLEEPF;
827 break;
828 }
829
830 /*
831 * \_SB
832 * ----
833 * Fix for the system root bus device -- the only root-level device.
834 */
Bob Moorec51a4de2005-11-17 13:07:00 -0500835 if (((acpi_handle)parent == ACPI_ROOT_OBJECT) && (type == ACPI_BUS_TYPE_DEVICE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 hid = ACPI_BUS_HID;
837 strcpy(device->pnp.device_name, ACPI_BUS_DEVICE_NAME);
838 strcpy(device->pnp.device_class, ACPI_BUS_CLASS);
839 }
840
841 if (hid) {
842 strcpy(device->pnp.hardware_id, hid);
843 device->flags.hardware_id = 1;
844 }
845 if (uid) {
846 strcpy(device->pnp.unique_id, uid);
847 device->flags.unique_id = 1;
848 }
849 if (cid_list) {
850 device->pnp.cid_list = kmalloc(cid_list->size, GFP_KERNEL);
851 if (device->pnp.cid_list)
852 memcpy(device->pnp.cid_list, cid_list, cid_list->size);
853 else
854 printk(KERN_ERR "Memory allocation error\n");
855 }
856
Len Brown02438d82006-06-30 03:19:10 -0400857 kfree(buffer.pointer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858}
859
Len Brown4be44fc2005-08-05 00:44:28 -0400860static int acpi_device_set_context(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861{
862 acpi_status status = AE_OK;
863 int result = 0;
864 /*
865 * Context
866 * -------
867 * Attach this 'struct acpi_device' to the ACPI object. This makes
868 * resolutions from handle->device very efficient. Note that we need
869 * to be careful with fixed-feature devices as they all attach to the
870 * root object.
871 */
Len Brown4be44fc2005-08-05 00:44:28 -0400872 if (type != ACPI_BUS_TYPE_POWER_BUTTON &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 type != ACPI_BUS_TYPE_SLEEP_BUTTON) {
874 status = acpi_attach_data(device->handle,
Len Brown4be44fc2005-08-05 00:44:28 -0400875 acpi_bus_data_handler, device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876
877 if (ACPI_FAILURE(status)) {
878 printk("Error attaching device data\n");
879 result = -ENODEV;
880 }
881 }
882 return result;
883}
884
Len Brown4be44fc2005-08-05 00:44:28 -0400885static void acpi_device_get_debug_info(struct acpi_device *device,
886 acpi_handle handle, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887{
888#ifdef CONFIG_ACPI_DEBUG_OUTPUT
Len Brown4be44fc2005-08-05 00:44:28 -0400889 char *type_string = NULL;
890 char name[80] = { '?', '\0' };
891 struct acpi_buffer buffer = { sizeof(name), name };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892
893 switch (type) {
894 case ACPI_BUS_TYPE_DEVICE:
895 type_string = "Device";
896 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
897 break;
898 case ACPI_BUS_TYPE_POWER:
899 type_string = "Power Resource";
900 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
901 break;
902 case ACPI_BUS_TYPE_PROCESSOR:
903 type_string = "Processor";
904 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
905 break;
906 case ACPI_BUS_TYPE_SYSTEM:
907 type_string = "System";
908 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
909 break;
910 case ACPI_BUS_TYPE_THERMAL:
911 type_string = "Thermal Zone";
912 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
913 break;
914 case ACPI_BUS_TYPE_POWER_BUTTON:
915 type_string = "Power Button";
916 sprintf(name, "PWRB");
917 break;
918 case ACPI_BUS_TYPE_SLEEP_BUTTON:
919 type_string = "Sleep Button";
920 sprintf(name, "SLPB");
921 break;
922 }
923
924 printk(KERN_DEBUG "Found %s %s [%p]\n", type_string, name, handle);
Len Brown4be44fc2005-08-05 00:44:28 -0400925#endif /*CONFIG_ACPI_DEBUG_OUTPUT */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926}
927
Len Brown4be44fc2005-08-05 00:44:28 -0400928static int acpi_bus_remove(struct acpi_device *dev, int rmdevice)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929{
Len Brown4be44fc2005-08-05 00:44:28 -0400930 int result = 0;
931 struct acpi_driver *driver;
932
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933
934 if (!dev)
Patrick Mocheld550d982006-06-27 00:41:40 -0400935 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936
937 driver = dev->driver;
938
939 if ((driver) && (driver->ops.remove)) {
940
941 if (driver->ops.stop) {
942 result = driver->ops.stop(dev, ACPI_BUS_REMOVAL_EJECT);
943 if (result)
Patrick Mocheld550d982006-06-27 00:41:40 -0400944 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 }
946
947 result = dev->driver->ops.remove(dev, ACPI_BUS_REMOVAL_EJECT);
948 if (result) {
Patrick Mocheld550d982006-06-27 00:41:40 -0400949 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 }
951
952 atomic_dec(&dev->driver->references);
953 dev->driver = NULL;
954 acpi_driver_data(dev) = NULL;
955 }
956
957 if (!rmdevice)
Patrick Mocheld550d982006-06-27 00:41:40 -0400958 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959
960 if (dev->flags.bus_address) {
961 if ((dev->parent) && (dev->parent->ops.unbind))
962 dev->parent->ops.unbind(dev);
963 }
Len Brown4be44fc2005-08-05 00:44:28 -0400964
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 acpi_device_unregister(dev, ACPI_BUS_REMOVAL_EJECT);
966
Patrick Mocheld550d982006-06-27 00:41:40 -0400967 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968}
969
Rajesh Shah3fb02732005-04-28 00:25:52 -0700970static int
Len Brown4be44fc2005-08-05 00:44:28 -0400971acpi_add_single_object(struct acpi_device **child,
972 struct acpi_device *parent, acpi_handle handle, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973{
Len Brown4be44fc2005-08-05 00:44:28 -0400974 int result = 0;
975 struct acpi_device *device = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977
978 if (!child)
Patrick Mocheld550d982006-06-27 00:41:40 -0400979 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980
981 device = kmalloc(sizeof(struct acpi_device), GFP_KERNEL);
982 if (!device) {
Len Brown64684632006-06-26 23:41:38 -0400983 printk(KERN_ERR PREFIX "Memory allocation error\n");
Patrick Mocheld550d982006-06-27 00:41:40 -0400984 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 }
986 memset(device, 0, sizeof(struct acpi_device));
987
988 device->handle = handle;
989 device->parent = parent;
990
Len Brown4be44fc2005-08-05 00:44:28 -0400991 acpi_device_get_busid(device, handle, type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992
993 /*
994 * Flags
995 * -----
996 * Get prior to calling acpi_bus_get_status() so we know whether
997 * or not _STA is present. Note that we only look for object
998 * handles -- cannot evaluate objects until we know the device is
999 * present and properly initialized.
1000 */
1001 result = acpi_bus_get_flags(device);
1002 if (result)
1003 goto end;
1004
1005 /*
1006 * Status
1007 * ------
Keiichiro Tokunaga6940fab2005-03-30 23:15:47 -05001008 * See if the device is present. We always assume that non-Device
1009 * and non-Processor objects (e.g. thermal zones, power resources,
1010 * etc.) are present, functioning, etc. (at least when parent object
1011 * is present). Note that _STA has a different meaning for some
1012 * objects (e.g. power resources) so we need to be careful how we use
1013 * it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 */
1015 switch (type) {
Keiichiro Tokunaga6940fab2005-03-30 23:15:47 -05001016 case ACPI_BUS_TYPE_PROCESSOR:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 case ACPI_BUS_TYPE_DEVICE:
1018 result = acpi_bus_get_status(device);
1019 if (ACPI_FAILURE(result) || !device->status.present) {
1020 result = -ENOENT;
1021 goto end;
1022 }
1023 break;
1024 default:
1025 STRUCT_TO_INT(device->status) = 0x0F;
1026 break;
1027 }
1028
1029 /*
1030 * Initialize Device
1031 * -----------------
1032 * TBD: Synch with Core's enumeration/initialization process.
1033 */
1034
1035 /*
1036 * Hardware ID, Unique ID, & Bus Address
1037 * -------------------------------------
1038 */
Len Brown4be44fc2005-08-05 00:44:28 -04001039 acpi_device_set_id(device, parent, handle, type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040
1041 /*
1042 * Power Management
1043 * ----------------
1044 */
1045 if (device->flags.power_manageable) {
1046 result = acpi_bus_get_power_flags(device);
1047 if (result)
1048 goto end;
1049 }
1050
Len Brown4be44fc2005-08-05 00:44:28 -04001051 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 * Wakeup device management
1053 *-----------------------
1054 */
1055 if (device->flags.wake_capable) {
1056 result = acpi_bus_get_wakeup_device_flags(device);
1057 if (result)
1058 goto end;
1059 }
1060
1061 /*
1062 * Performance Management
1063 * ----------------------
1064 */
1065 if (device->flags.performance_manageable) {
1066 result = acpi_bus_get_perf_flags(device);
1067 if (result)
1068 goto end;
1069 }
1070
Len Brown4be44fc2005-08-05 00:44:28 -04001071 if ((result = acpi_device_set_context(device, type)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 goto end;
1073
Len Brown4be44fc2005-08-05 00:44:28 -04001074 acpi_device_get_debug_info(device, handle, type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075
Len Brown4be44fc2005-08-05 00:44:28 -04001076 acpi_device_register(device, parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077
1078 /*
1079 * Bind _ADR-Based Devices
1080 * -----------------------
1081 * If there's a a bus address (_ADR) then we utilize the parent's
1082 * 'bind' function (if exists) to bind the ACPI- and natively-
1083 * enumerated device representations.
1084 */
1085 if (device->flags.bus_address) {
1086 if (device->parent && device->parent->ops.bind)
1087 device->parent->ops.bind(device);
1088 }
1089
1090 /*
1091 * Locate & Attach Driver
1092 * ----------------------
1093 * If there's a hardware id (_HID) or compatible ids (_CID) we check
1094 * to see if there's a driver installed for this kind of device. Note
1095 * that drivers can install before or after a device is enumerated.
1096 *
1097 * TBD: Assumes LDM provides driver hot-plug capability.
1098 */
Thomas Renningerbd7ce5b2005-10-03 10:39:00 -07001099 acpi_bus_find_driver(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100
Len Brown4be44fc2005-08-05 00:44:28 -04001101 end:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 if (!result)
1103 *child = device;
1104 else {
Jesper Juhl6044ec82005-11-07 01:01:32 -08001105 kfree(device->pnp.cid_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 kfree(device);
1107 }
1108
Patrick Mocheld550d982006-06-27 00:41:40 -04001109 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111
Len Brown4be44fc2005-08-05 00:44:28 -04001112static int acpi_bus_scan(struct acpi_device *start, struct acpi_bus_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113{
Len Brown4be44fc2005-08-05 00:44:28 -04001114 acpi_status status = AE_OK;
1115 struct acpi_device *parent = NULL;
1116 struct acpi_device *child = NULL;
1117 acpi_handle phandle = NULL;
1118 acpi_handle chandle = NULL;
1119 acpi_object_type type = 0;
1120 u32 level = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122
1123 if (!start)
Patrick Mocheld550d982006-06-27 00:41:40 -04001124 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125
1126 parent = start;
1127 phandle = start->handle;
Len Brown4be44fc2005-08-05 00:44:28 -04001128
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 /*
1130 * Parse through the ACPI namespace, identify all 'devices', and
1131 * create a new 'struct acpi_device' for each.
1132 */
1133 while ((level > 0) && parent) {
1134
1135 status = acpi_get_next_object(ACPI_TYPE_ANY, phandle,
Len Brown4be44fc2005-08-05 00:44:28 -04001136 chandle, &chandle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137
1138 /*
1139 * If this scope is exhausted then move our way back up.
1140 */
1141 if (ACPI_FAILURE(status)) {
1142 level--;
1143 chandle = phandle;
1144 acpi_get_parent(phandle, &phandle);
1145 if (parent->parent)
1146 parent = parent->parent;
1147 continue;
1148 }
1149
1150 status = acpi_get_type(chandle, &type);
1151 if (ACPI_FAILURE(status))
1152 continue;
1153
1154 /*
1155 * If this is a scope object then parse it (depth-first).
1156 */
1157 if (type == ACPI_TYPE_LOCAL_SCOPE) {
1158 level++;
1159 phandle = chandle;
1160 chandle = NULL;
1161 continue;
1162 }
1163
1164 /*
1165 * We're only interested in objects that we consider 'devices'.
1166 */
1167 switch (type) {
1168 case ACPI_TYPE_DEVICE:
1169 type = ACPI_BUS_TYPE_DEVICE;
1170 break;
1171 case ACPI_TYPE_PROCESSOR:
1172 type = ACPI_BUS_TYPE_PROCESSOR;
1173 break;
1174 case ACPI_TYPE_THERMAL:
1175 type = ACPI_BUS_TYPE_THERMAL;
1176 break;
1177 case ACPI_TYPE_POWER:
1178 type = ACPI_BUS_TYPE_POWER;
1179 break;
1180 default:
1181 continue;
1182 }
1183
Rajesh Shah3fb02732005-04-28 00:25:52 -07001184 if (ops->acpi_op_add)
1185 status = acpi_add_single_object(&child, parent,
Len Brown4be44fc2005-08-05 00:44:28 -04001186 chandle, type);
1187 else
Rajesh Shah3fb02732005-04-28 00:25:52 -07001188 status = acpi_bus_get_device(chandle, &child);
1189
Len Brown4be44fc2005-08-05 00:44:28 -04001190 if (ACPI_FAILURE(status))
1191 continue;
Rajesh Shah3fb02732005-04-28 00:25:52 -07001192
1193 if (ops->acpi_op_start) {
1194 status = acpi_start_single_object(child);
1195 if (ACPI_FAILURE(status))
1196 continue;
1197 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198
1199 /*
1200 * If the device is present, enabled, and functioning then
1201 * parse its scope (depth-first). Note that we need to
1202 * represent absent devices to facilitate PnP notifications
1203 * -- but only the subtree head (not all of its children,
1204 * which will be enumerated when the parent is inserted).
1205 *
1206 * TBD: Need notifications and other detection mechanisms
Len Brown4be44fc2005-08-05 00:44:28 -04001207 * in place before we can fully implement this.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 */
1209 if (child->status.present) {
1210 status = acpi_get_next_object(ACPI_TYPE_ANY, chandle,
1211 NULL, NULL);
1212 if (ACPI_SUCCESS(status)) {
1213 level++;
1214 phandle = chandle;
1215 chandle = NULL;
1216 parent = child;
1217 }
1218 }
1219 }
1220
Patrick Mocheld550d982006-06-27 00:41:40 -04001221 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223
Rajesh Shah3fb02732005-04-28 00:25:52 -07001224int
Len Brown4be44fc2005-08-05 00:44:28 -04001225acpi_bus_add(struct acpi_device **child,
1226 struct acpi_device *parent, acpi_handle handle, int type)
Rajesh Shah3fb02732005-04-28 00:25:52 -07001227{
1228 int result;
1229 struct acpi_bus_ops ops;
1230
Rajesh Shah3fb02732005-04-28 00:25:52 -07001231
1232 result = acpi_add_single_object(child, parent, handle, type);
1233 if (!result) {
1234 memset(&ops, 0, sizeof(ops));
1235 ops.acpi_op_add = 1;
1236 result = acpi_bus_scan(*child, &ops);
1237 }
Patrick Mocheld550d982006-06-27 00:41:40 -04001238 return result;
Rajesh Shah3fb02732005-04-28 00:25:52 -07001239}
Len Brown4be44fc2005-08-05 00:44:28 -04001240
Rajesh Shah3fb02732005-04-28 00:25:52 -07001241EXPORT_SYMBOL(acpi_bus_add);
1242
Len Brown4be44fc2005-08-05 00:44:28 -04001243int acpi_bus_start(struct acpi_device *device)
Rajesh Shah3fb02732005-04-28 00:25:52 -07001244{
1245 int result;
1246 struct acpi_bus_ops ops;
1247
Rajesh Shah3fb02732005-04-28 00:25:52 -07001248
1249 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -04001250 return -EINVAL;
Rajesh Shah3fb02732005-04-28 00:25:52 -07001251
1252 result = acpi_start_single_object(device);
1253 if (!result) {
1254 memset(&ops, 0, sizeof(ops));
1255 ops.acpi_op_start = 1;
1256 result = acpi_bus_scan(device, &ops);
1257 }
Patrick Mocheld550d982006-06-27 00:41:40 -04001258 return result;
Rajesh Shah3fb02732005-04-28 00:25:52 -07001259}
Len Brown4be44fc2005-08-05 00:44:28 -04001260
Rajesh Shah3fb02732005-04-28 00:25:52 -07001261EXPORT_SYMBOL(acpi_bus_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262
Kristen Accardiceaba662006-02-23 17:56:01 -08001263int acpi_bus_trim(struct acpi_device *start, int rmdevice)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264{
Len Brown4be44fc2005-08-05 00:44:28 -04001265 acpi_status status;
1266 struct acpi_device *parent, *child;
1267 acpi_handle phandle, chandle;
1268 acpi_object_type type;
1269 u32 level = 1;
1270 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271
Len Brown4be44fc2005-08-05 00:44:28 -04001272 parent = start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 phandle = start->handle;
1274 child = chandle = NULL;
1275
1276 while ((level > 0) && parent && (!err)) {
1277 status = acpi_get_next_object(ACPI_TYPE_ANY, phandle,
Len Brown4be44fc2005-08-05 00:44:28 -04001278 chandle, &chandle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279
1280 /*
1281 * If this scope is exhausted then move our way back up.
1282 */
1283 if (ACPI_FAILURE(status)) {
1284 level--;
1285 chandle = phandle;
1286 acpi_get_parent(phandle, &phandle);
1287 child = parent;
1288 parent = parent->parent;
1289
1290 if (level == 0)
1291 err = acpi_bus_remove(child, rmdevice);
1292 else
1293 err = acpi_bus_remove(child, 1);
1294
1295 continue;
1296 }
1297
1298 status = acpi_get_type(chandle, &type);
1299 if (ACPI_FAILURE(status)) {
1300 continue;
1301 }
1302 /*
1303 * If there is a device corresponding to chandle then
1304 * parse it (depth-first).
1305 */
1306 if (acpi_bus_get_device(chandle, &child) == 0) {
1307 level++;
1308 phandle = chandle;
1309 chandle = NULL;
1310 parent = child;
1311 }
1312 continue;
1313 }
1314 return err;
1315}
Kristen Accardiceaba662006-02-23 17:56:01 -08001316EXPORT_SYMBOL_GPL(acpi_bus_trim);
1317
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318
Len Brown4be44fc2005-08-05 00:44:28 -04001319static int acpi_bus_scan_fixed(struct acpi_device *root)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320{
Len Brown4be44fc2005-08-05 00:44:28 -04001321 int result = 0;
1322 struct acpi_device *device = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324
1325 if (!root)
Patrick Mocheld550d982006-06-27 00:41:40 -04001326 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327
1328 /*
1329 * Enumerate all fixed-feature devices.
1330 */
Rajesh Shah3fb02732005-04-28 00:25:52 -07001331 if (acpi_fadt.pwr_button == 0) {
1332 result = acpi_add_single_object(&device, acpi_root,
Len Brown4be44fc2005-08-05 00:44:28 -04001333 NULL,
1334 ACPI_BUS_TYPE_POWER_BUTTON);
Rajesh Shah3fb02732005-04-28 00:25:52 -07001335 if (!result)
1336 result = acpi_start_single_object(device);
1337 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338
Rajesh Shah3fb02732005-04-28 00:25:52 -07001339 if (acpi_fadt.sleep_button == 0) {
1340 result = acpi_add_single_object(&device, acpi_root,
Len Brown4be44fc2005-08-05 00:44:28 -04001341 NULL,
1342 ACPI_BUS_TYPE_SLEEP_BUTTON);
Rajesh Shah3fb02732005-04-28 00:25:52 -07001343 if (!result)
1344 result = acpi_start_single_object(device);
1345 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346
Patrick Mocheld550d982006-06-27 00:41:40 -04001347 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348}
1349
Patrick Mochel5b327262006-05-10 10:33:00 -04001350
Len Brown531881d2006-05-15 03:06:41 -04001351static inline struct acpi_device * to_acpi_dev(struct device * dev)
Patrick Mochel5b327262006-05-10 10:33:00 -04001352{
1353 return container_of(dev, struct acpi_device, dev);
1354}
1355
1356
Len Brown531881d2006-05-15 03:06:41 -04001357static int root_suspend(struct acpi_device * acpi_dev, pm_message_t state)
Patrick Mochel5b327262006-05-10 10:33:00 -04001358{
1359 struct acpi_device * dev, * next;
1360 int result;
1361
1362 spin_lock(&acpi_device_lock);
1363 list_for_each_entry_safe_reverse(dev, next, &acpi_device_list, g_list) {
1364 if (dev->driver && dev->driver->ops.suspend) {
1365 spin_unlock(&acpi_device_lock);
Patrick Mochel5b327262006-05-10 10:33:00 -04001366 result = dev->driver->ops.suspend(dev, 0);
1367 if (result) {
1368 printk(KERN_ERR PREFIX "[%s - %s] Suspend failed: %d\n",
1369 acpi_device_name(dev),
1370 acpi_device_bid(dev), result);
1371 }
1372 spin_lock(&acpi_device_lock);
1373 }
1374 }
1375 spin_unlock(&acpi_device_lock);
1376 return 0;
1377}
1378
1379
1380static int acpi_device_suspend(struct device * dev, pm_message_t state)
1381{
1382 struct acpi_device * acpi_dev = to_acpi_dev(dev);
1383
1384 /*
1385 * For now, we should only register 1 generic device -
1386 * the ACPI root device - and from there, we walk the
1387 * tree of ACPI devices to suspend each one using the
1388 * ACPI driver methods.
1389 */
1390 if (acpi_dev->handle == ACPI_ROOT_OBJECT)
Len Brown531881d2006-05-15 03:06:41 -04001391 root_suspend(acpi_dev, state);
Patrick Mochel5b327262006-05-10 10:33:00 -04001392 return 0;
1393}
1394
1395
1396
1397static int root_resume(struct acpi_device * acpi_dev)
1398{
1399 struct acpi_device * dev, * next;
1400 int result;
1401
1402 spin_lock(&acpi_device_lock);
1403 list_for_each_entry_safe(dev, next, &acpi_device_list, g_list) {
1404 if (dev->driver && dev->driver->ops.resume) {
1405 spin_unlock(&acpi_device_lock);
Patrick Mochel5b327262006-05-10 10:33:00 -04001406 result = dev->driver->ops.resume(dev, 0);
1407 if (result) {
1408 printk(KERN_ERR PREFIX "[%s - %s] resume failed: %d\n",
1409 acpi_device_name(dev),
1410 acpi_device_bid(dev), result);
1411 }
1412 spin_lock(&acpi_device_lock);
1413 }
1414 }
1415 spin_unlock(&acpi_device_lock);
1416 return 0;
1417}
1418
1419
1420static int acpi_device_resume(struct device * dev)
1421{
1422 struct acpi_device * acpi_dev = to_acpi_dev(dev);
1423
1424 /*
1425 * For now, we should only register 1 generic device -
1426 * the ACPI root device - and from there, we walk the
1427 * tree of ACPI devices to resume each one using the
1428 * ACPI driver methods.
1429 */
1430 if (acpi_dev->handle == ACPI_ROOT_OBJECT)
1431 root_resume(acpi_dev);
1432 return 0;
1433}
1434
1435
Adrian Bunk9e5289f2006-06-30 05:15:00 -04001436static struct bus_type acpi_bus_type = {
Patrick Mochel5b327262006-05-10 10:33:00 -04001437 .name = "acpi",
1438 .suspend = acpi_device_suspend,
1439 .resume = acpi_device_resume,
1440};
1441
1442
1443
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444static int __init acpi_scan_init(void)
1445{
1446 int result;
Rajesh Shah3fb02732005-04-28 00:25:52 -07001447 struct acpi_bus_ops ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449
1450 if (acpi_disabled)
Patrick Mocheld550d982006-06-27 00:41:40 -04001451 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452
1453 kset_register(&acpi_namespace_kset);
1454
Patrick Mochel5b327262006-05-10 10:33:00 -04001455 result = bus_register(&acpi_bus_type);
1456 if (result) {
1457 /* We don't want to quit even if we failed to add suspend/resume */
1458 printk(KERN_ERR PREFIX "Could not register bus type\n");
1459 }
1460
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 /*
1462 * Create the root device in the bus's device tree
1463 */
Rajesh Shah3fb02732005-04-28 00:25:52 -07001464 result = acpi_add_single_object(&acpi_root, NULL, ACPI_ROOT_OBJECT,
Len Brown4be44fc2005-08-05 00:44:28 -04001465 ACPI_BUS_TYPE_SYSTEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 if (result)
1467 goto Done;
1468
Rajesh Shah3fb02732005-04-28 00:25:52 -07001469 result = acpi_start_single_object(acpi_root);
Patrick Mochel5b327262006-05-10 10:33:00 -04001470 if (result)
1471 goto Done;
1472
1473 acpi_root->dev.bus = &acpi_bus_type;
1474 snprintf(acpi_root->dev.bus_id, BUS_ID_SIZE, "%s", acpi_bus_type.name);
1475 result = device_register(&acpi_root->dev);
1476 if (result) {
1477 /* We don't want to quit even if we failed to add suspend/resume */
1478 printk(KERN_ERR PREFIX "Could not register device\n");
1479 }
Rajesh Shah3fb02732005-04-28 00:25:52 -07001480
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 /*
1482 * Enumerate devices in the ACPI namespace.
1483 */
1484 result = acpi_bus_scan_fixed(acpi_root);
Rajesh Shah3fb02732005-04-28 00:25:52 -07001485 if (!result) {
1486 memset(&ops, 0, sizeof(ops));
1487 ops.acpi_op_add = 1;
1488 ops.acpi_op_start = 1;
1489 result = acpi_bus_scan(acpi_root, &ops);
1490 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491
1492 if (result)
1493 acpi_device_unregister(acpi_root, ACPI_BUS_REMOVAL_NORMAL);
1494
Len Brown4be44fc2005-08-05 00:44:28 -04001495 Done:
Patrick Mocheld550d982006-06-27 00:41:40 -04001496 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497}
1498
1499subsys_initcall(acpi_scan_init);