ACPI: add ACPI bus_type for driver model
[linux-2.6-block.git] / drivers / acpi / scan.c
CommitLineData
1da177e4
LT
1/*
2 * scan.c - support for transforming the ACPI namespace into individual objects
3 */
4
5#include <linux/module.h>
6#include <linux/init.h>
9b6d97b6 7#include <linux/kernel.h>
1da177e4
LT
8#include <linux/acpi.h>
9
10#include <acpi/acpi_drivers.h>
11#include <acpi/acinterp.h> /* for acpi_ex_eisa_id_to_string() */
12
1da177e4 13#define _COMPONENT ACPI_BUS_COMPONENT
4be44fcd 14ACPI_MODULE_NAME("scan")
1da177e4 15#define STRUCT_TO_INT(s) (*((int*)&s))
4be44fcd 16extern struct acpi_device *acpi_root;
1da177e4
LT
17
18#define ACPI_BUS_CLASS "system_bus"
19#define ACPI_BUS_HID "ACPI_BUS"
20#define ACPI_BUS_DRIVER_NAME "ACPI Bus Driver"
21#define ACPI_BUS_DEVICE_NAME "System Bus"
22
23static LIST_HEAD(acpi_device_list);
24DEFINE_SPINLOCK(acpi_device_lock);
25LIST_HEAD(acpi_wakeup_device_list);
26
1da177e4 27
4be44fcd 28static void acpi_device_release(struct kobject *kobj)
1da177e4 29{
4be44fcd 30 struct acpi_device *dev = container_of(kobj, struct acpi_device, kobj);
6044ec88 31 kfree(dev->pnp.cid_list);
1da177e4
LT
32 kfree(dev);
33}
34
35struct acpi_device_attribute {
36 struct attribute attr;
4be44fcd
LB
37 ssize_t(*show) (struct acpi_device *, char *);
38 ssize_t(*store) (struct acpi_device *, const char *, size_t);
1da177e4
LT
39};
40
41typedef void acpi_device_sysfs_files(struct kobject *,
4be44fcd 42 const struct attribute *);
1da177e4
LT
43
44static void setup_sys_fs_device_files(struct acpi_device *dev,
4be44fcd 45 acpi_device_sysfs_files * func);
1da177e4
LT
46
47#define create_sysfs_device_files(dev) \
48 setup_sys_fs_device_files(dev, (acpi_device_sysfs_files *)&sysfs_create_file)
49#define remove_sysfs_device_files(dev) \
50 setup_sys_fs_device_files(dev, (acpi_device_sysfs_files *)&sysfs_remove_file)
51
1d268b0a 52#define to_acpi_dev(n) container_of(n, struct acpi_device, kobj)
1da177e4
LT
53#define to_handle_attr(n) container_of(n, struct acpi_device_attribute, attr);
54
55static ssize_t acpi_device_attr_show(struct kobject *kobj,
4be44fcd 56 struct attribute *attr, char *buf)
1da177e4 57{
1d268b0a 58 struct acpi_device *device = to_acpi_dev(kobj);
1da177e4 59 struct acpi_device_attribute *attribute = to_handle_attr(attr);
70f2817a 60 return attribute->show ? attribute->show(device, buf) : -EIO;
1da177e4
LT
61}
62static ssize_t acpi_device_attr_store(struct kobject *kobj,
4be44fcd
LB
63 struct attribute *attr, const char *buf,
64 size_t len)
1da177e4 65{
1d268b0a 66 struct acpi_device *device = to_acpi_dev(kobj);
1da177e4 67 struct acpi_device_attribute *attribute = to_handle_attr(attr);
70f2817a 68 return attribute->store ? attribute->store(device, buf, len) : -EIO;
1da177e4
LT
69}
70
71static struct sysfs_ops acpi_device_sysfs_ops = {
4be44fcd
LB
72 .show = acpi_device_attr_show,
73 .store = acpi_device_attr_store,
1da177e4
LT
74};
75
76static struct kobj_type ktype_acpi_ns = {
4be44fcd
LB
77 .sysfs_ops = &acpi_device_sysfs_ops,
78 .release = acpi_device_release,
1da177e4
LT
79};
80
312c004d 81static int namespace_uevent(struct kset *kset, struct kobject *kobj,
1da177e4
LT
82 char **envp, int num_envp, char *buffer,
83 int buffer_size)
84{
1d268b0a 85 struct acpi_device *dev = to_acpi_dev(kobj);
1da177e4
LT
86 int i = 0;
87 int len = 0;
88
89 if (!dev->driver)
90 return 0;
91
312c004d
KS
92 if (add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &len,
93 "PHYSDEVDRIVER=%s", dev->driver->name))
1da177e4
LT
94 return -ENOMEM;
95
96 envp[i] = NULL;
97
98 return 0;
99}
100
312c004d
KS
101static struct kset_uevent_ops namespace_uevent_ops = {
102 .uevent = &namespace_uevent,
1da177e4
LT
103};
104
105static struct kset acpi_namespace_kset = {
4be44fcd
LB
106 .kobj = {
107 .name = "namespace",
108 },
1da177e4 109 .subsys = &acpi_subsys,
4be44fcd 110 .ktype = &ktype_acpi_ns,
312c004d 111 .uevent_ops = &namespace_uevent_ops,
1da177e4
LT
112};
113
1da177e4 114/* --------------------------------------------------------------------------
312c004d 115 ACPI sysfs device file support
1da177e4 116 -------------------------------------------------------------------------- */
4be44fcd
LB
117static ssize_t acpi_eject_store(struct acpi_device *device,
118 const char *buf, size_t count);
1da177e4
LT
119
120#define ACPI_DEVICE_ATTR(_name,_mode,_show,_store) \
121static struct acpi_device_attribute acpi_device_attr_##_name = \
122 __ATTR(_name, _mode, _show, _store)
123
124ACPI_DEVICE_ATTR(eject, 0200, NULL, acpi_eject_store);
125
126/**
127 * setup_sys_fs_device_files - sets up the device files under device namespace
67be2dd1
MW
128 * @dev: acpi_device object
129 * @func: function pointer to create or destroy the device file
1da177e4
LT
130 */
131static void
4be44fcd
LB
132setup_sys_fs_device_files(struct acpi_device *dev,
133 acpi_device_sysfs_files * func)
1da177e4 134{
4be44fcd
LB
135 acpi_status status;
136 acpi_handle temp = NULL;
1da177e4
LT
137
138 /*
139 * If device has _EJ0, 'eject' file is created that is used to trigger
140 * hot-removal function from userland.
141 */
142 status = acpi_get_handle(dev->handle, "_EJ0", &temp);
143 if (ACPI_SUCCESS(status))
4be44fcd 144 (*(func)) (&dev->kobj, &acpi_device_attr_eject.attr);
1da177e4
LT
145}
146
4be44fcd 147static int acpi_eject_operation(acpi_handle handle, int lockable)
1da177e4
LT
148{
149 struct acpi_object_list arg_list;
150 union acpi_object arg;
151 acpi_status status = AE_OK;
152
153 /*
154 * TBD: evaluate _PS3?
155 */
156
157 if (lockable) {
158 arg_list.count = 1;
159 arg_list.pointer = &arg;
160 arg.type = ACPI_TYPE_INTEGER;
161 arg.integer.value = 0;
162 acpi_evaluate_object(handle, "_LCK", &arg_list, NULL);
163 }
164
165 arg_list.count = 1;
166 arg_list.pointer = &arg;
167 arg.type = ACPI_TYPE_INTEGER;
168 arg.integer.value = 1;
169
170 /*
171 * TBD: _EJD support.
172 */
173
174 status = acpi_evaluate_object(handle, "_EJ0", &arg_list, NULL);
175 if (ACPI_FAILURE(status)) {
4be44fcd 176 return (-ENODEV);
1da177e4
LT
177 }
178
4be44fcd 179 return (0);
1da177e4
LT
180}
181
1da177e4
LT
182static ssize_t
183acpi_eject_store(struct acpi_device *device, const char *buf, size_t count)
184{
4be44fcd
LB
185 int result;
186 int ret = count;
187 int islockable;
188 acpi_status status;
189 acpi_handle handle;
190 acpi_object_type type = 0;
1da177e4
LT
191
192 if ((!count) || (buf[0] != '1')) {
193 return -EINVAL;
194 }
1da177e4
LT
195#ifndef FORCE_EJECT
196 if (device->driver == NULL) {
197 ret = -ENODEV;
198 goto err;
199 }
200#endif
201 status = acpi_get_type(device->handle, &type);
4be44fcd 202 if (ACPI_FAILURE(status) || (!device->flags.ejectable)) {
1da177e4
LT
203 ret = -ENODEV;
204 goto err;
205 }
206
207 islockable = device->flags.lockable;
208 handle = device->handle;
209
eefa27a9 210 result = acpi_bus_trim(device, 1);
1da177e4
LT
211
212 if (!result)
213 result = acpi_eject_operation(handle, islockable);
214
215 if (result) {
216 ret = -EBUSY;
217 }
4be44fcd 218 err:
1da177e4
LT
219 return ret;
220}
221
1da177e4 222/* --------------------------------------------------------------------------
9e89dde2 223 ACPI Bus operations
1da177e4 224 -------------------------------------------------------------------------- */
5d9464a4 225static int acpi_device_suspend(struct device *dev, pm_message_t state)
1da177e4 226{
5d9464a4
PM
227 struct acpi_device *acpi_dev = to_acpi_device(dev);
228 struct acpi_driver *acpi_drv = acpi_dev->driver;
9e89dde2 229
5d9464a4
PM
230 if (acpi_drv && acpi_drv->ops.suspend)
231 return acpi_drv->ops.suspend(acpi_dev, state);
1da177e4
LT
232 return 0;
233}
234
5d9464a4 235static int acpi_device_resume(struct device *dev)
9e89dde2 236{
5d9464a4
PM
237 struct acpi_device *acpi_dev = to_acpi_device(dev);
238 struct acpi_driver *acpi_drv = acpi_dev->driver;
1da177e4 239
5d9464a4
PM
240 if (acpi_drv && acpi_drv->ops.resume)
241 return acpi_drv->ops.resume(acpi_dev);
9e89dde2
ZR
242 return 0;
243}
244
5d9464a4 245static int acpi_bus_match(struct device *dev, struct device_driver *drv)
9e89dde2 246{
5d9464a4
PM
247 struct acpi_device *acpi_dev = to_acpi_device(dev);
248 struct acpi_driver *acpi_drv = to_acpi_driver(drv);
9e89dde2 249
5d9464a4
PM
250 if (acpi_drv->ops.match)
251 return !acpi_drv->ops.match(acpi_dev, acpi_drv);
252 return !acpi_match_ids(acpi_dev, acpi_drv->ids);
253}
254
255static int acpi_device_uevent(struct device *dev, char **envp, int num_envp,
256 char *buffer, int buffer_size)
257{
258 struct acpi_device *acpi_dev = to_acpi_device(dev);
259 int i = 0, length = 0, ret = 0;
260
261 if (acpi_dev->flags.hardware_id)
262 ret = add_uevent_var(envp, num_envp, &i,
263 buffer, buffer_size, &length,
264 "HWID=%s", acpi_dev->pnp.hardware_id);
265 if (ret)
266 return -ENOMEM;
267 if (acpi_dev->flags.compatible_ids) {
268 int j;
269 struct acpi_compatible_id_list *cid_list;
270
271 cid_list = acpi_dev->pnp.cid_list;
272
273 for (j = 0; j < cid_list->count; j++) {
274 ret = add_uevent_var(envp, num_envp, &i, buffer,
275 buffer_size, &length, "COMPTID=%s",
276 cid_list->id[j].value);
277 if (ret)
278 return -ENOMEM;
9e89dde2
ZR
279 }
280 }
5d9464a4
PM
281
282 envp[i] = NULL;
9e89dde2
ZR
283 return 0;
284}
285
5d9464a4
PM
286static int acpi_bus_driver_init(struct acpi_device *, struct acpi_driver *);
287static int acpi_start_single_object(struct acpi_device *);
288static int acpi_device_probe(struct device * dev)
9e89dde2 289{
5d9464a4
PM
290 struct acpi_device *acpi_dev = to_acpi_device(dev);
291 struct acpi_driver *acpi_drv = to_acpi_driver(dev->driver);
292 int ret;
293
294 ret = acpi_bus_driver_init(acpi_dev, acpi_drv);
295 if (!ret) {
296 acpi_start_single_object(acpi_dev);
297 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
298 "Found driver [%s] for device [%s]\n",
299 acpi_drv->name, acpi_dev->pnp.bus_id));
300 get_device(dev);
301 }
302 return ret;
303}
9e89dde2 304
5d9464a4
PM
305static int acpi_device_remove(struct device * dev)
306{
307 struct acpi_device *acpi_dev = to_acpi_device(dev);
308 struct acpi_driver *acpi_drv = acpi_dev->driver;
309
310 if (acpi_drv) {
311 if (acpi_drv->ops.stop)
312 acpi_drv->ops.stop(acpi_dev, ACPI_BUS_REMOVAL_NORMAL);
313 if (acpi_drv->ops.remove)
314 acpi_drv->ops.remove(acpi_dev, ACPI_BUS_REMOVAL_NORMAL);
315 }
316 acpi_dev->driver = NULL;
317 acpi_driver_data(dev) = NULL;
318
319 put_device(dev);
9e89dde2
ZR
320 return 0;
321}
1da177e4 322
5d9464a4 323static void acpi_device_shutdown(struct device *dev)
1da177e4 324{
5d9464a4
PM
325 struct acpi_device *acpi_dev = to_acpi_device(dev);
326 struct acpi_driver *acpi_drv = acpi_dev->driver;
327
328 if (acpi_drv && acpi_drv->ops.shutdown)
329 acpi_drv->ops.shutdown(acpi_dev);
330
331 return ;
1da177e4
LT
332}
333
9e89dde2
ZR
334static struct bus_type acpi_bus_type = {
335 .name = "acpi",
336 .suspend = acpi_device_suspend,
337 .resume = acpi_device_resume,
5d9464a4
PM
338 .shutdown = acpi_device_shutdown,
339 .match = acpi_bus_match,
340 .probe = acpi_device_probe,
341 .remove = acpi_device_remove,
342 .uevent = acpi_device_uevent,
9e89dde2
ZR
343};
344
345static void acpi_device_register(struct acpi_device *device,
346 struct acpi_device *parent)
347{
348 int err;
349
350 /*
351 * Linkage
352 * -------
353 * Link this device to its parent and siblings.
354 */
355 INIT_LIST_HEAD(&device->children);
356 INIT_LIST_HEAD(&device->node);
357 INIT_LIST_HEAD(&device->g_list);
358 INIT_LIST_HEAD(&device->wakeup_list);
359
360 spin_lock(&acpi_device_lock);
361 if (device->parent) {
362 list_add_tail(&device->node, &device->parent->children);
363 list_add_tail(&device->g_list, &device->parent->g_list);
364 } else
365 list_add_tail(&device->g_list, &acpi_device_list);
366 if (device->wakeup.flags.valid)
367 list_add_tail(&device->wakeup_list, &acpi_wakeup_device_list);
368 spin_unlock(&acpi_device_lock);
369
370 strlcpy(device->kobj.name, device->pnp.bus_id, KOBJ_NAME_LEN);
371 if (parent)
372 device->kobj.parent = &parent->kobj;
373 device->kobj.ktype = &ktype_acpi_ns;
374 device->kobj.kset = &acpi_namespace_kset;
375 err = kobject_register(&device->kobj);
376 if (err < 0)
377 printk(KERN_WARNING "%s: kobject_register error: %d\n",
378 __FUNCTION__, err);
379 create_sysfs_device_files(device);
380}
381
382static void acpi_device_unregister(struct acpi_device *device, int type)
383{
384 spin_lock(&acpi_device_lock);
385 if (device->parent) {
386 list_del(&device->node);
387 list_del(&device->g_list);
388 } else
389 list_del(&device->g_list);
390
391 list_del(&device->wakeup_list);
392
393 spin_unlock(&acpi_device_lock);
394
395 acpi_detach_data(device->handle, acpi_bus_data_handler);
396 remove_sysfs_device_files(device);
397 kobject_unregister(&device->kobj);
398}
399
400/* --------------------------------------------------------------------------
401 Driver Management
402 -------------------------------------------------------------------------- */
403static LIST_HEAD(acpi_bus_drivers);
404
1da177e4 405/**
d758a8fa
RD
406 * acpi_bus_driver_init - add a device to a driver
407 * @device: the device to add and initialize
408 * @driver: driver for the device
409 *
1da177e4
LT
410 * Used to initialize a device via its device driver. Called whenever a
411 * driver is bound to a device. Invokes the driver's add() and start() ops.
412 */
413static int
4be44fcd 414acpi_bus_driver_init(struct acpi_device *device, struct acpi_driver *driver)
1da177e4 415{
4be44fcd 416 int result = 0;
1da177e4 417
1da177e4
LT
418
419 if (!device || !driver)
d550d98d 420 return -EINVAL;
1da177e4
LT
421
422 if (!driver->ops.add)
d550d98d 423 return -ENOSYS;
1da177e4
LT
424
425 result = driver->ops.add(device);
426 if (result) {
427 device->driver = NULL;
428 acpi_driver_data(device) = NULL;
d550d98d 429 return result;
1da177e4
LT
430 }
431
432 device->driver = driver;
433
434 /*
435 * TBD - Configuration Management: Assign resources to device based
436 * upon possible configuration and currently allocated resources.
437 */
438
4be44fcd
LB
439 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
440 "Driver successfully bound to device\n"));
d550d98d 441 return 0;
3fb02738
RS
442}
443
8713cbef 444static int acpi_start_single_object(struct acpi_device *device)
3fb02738
RS
445{
446 int result = 0;
447 struct acpi_driver *driver;
448
3fb02738
RS
449
450 if (!(driver = device->driver))
d550d98d 451 return 0;
3fb02738 452
1da177e4
LT
453 if (driver->ops.start) {
454 result = driver->ops.start(device);
455 if (result && driver->ops.remove)
456 driver->ops.remove(device, ACPI_BUS_REMOVAL_NORMAL);
1da177e4
LT
457 }
458
d550d98d 459 return result;
1da177e4
LT
460}
461
9d9f749b 462static void acpi_driver_attach(struct acpi_driver *drv)
1da177e4 463{
4be44fcd 464 struct list_head *node, *next;
1da177e4 465
1da177e4
LT
466
467 spin_lock(&acpi_device_lock);
468 list_for_each_safe(node, next, &acpi_device_list) {
4be44fcd
LB
469 struct acpi_device *dev =
470 container_of(node, struct acpi_device, g_list);
1da177e4
LT
471
472 if (dev->driver || !dev->status.present)
473 continue;
474 spin_unlock(&acpi_device_lock);
475
5d9464a4 476 if (!acpi_bus_match(&(dev->dev), &(drv->drv))) {
1da177e4 477 if (!acpi_bus_driver_init(dev, drv)) {
3fb02738 478 acpi_start_single_object(dev);
1da177e4 479 atomic_inc(&drv->references);
4be44fcd
LB
480 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
481 "Found driver [%s] for device [%s]\n",
1da177e4
LT
482 drv->name, dev->pnp.bus_id));
483 }
484 }
485 spin_lock(&acpi_device_lock);
486 }
487 spin_unlock(&acpi_device_lock);
1da177e4
LT
488}
489
06ea8e08 490static void acpi_driver_detach(struct acpi_driver *drv)
1da177e4 491{
4be44fcd 492 struct list_head *node, *next;
1da177e4 493
1da177e4
LT
494
495 spin_lock(&acpi_device_lock);
4be44fcd
LB
496 list_for_each_safe(node, next, &acpi_device_list) {
497 struct acpi_device *dev =
498 container_of(node, struct acpi_device, g_list);
1da177e4
LT
499
500 if (dev->driver == drv) {
501 spin_unlock(&acpi_device_lock);
502 if (drv->ops.remove)
4be44fcd 503 drv->ops.remove(dev, ACPI_BUS_REMOVAL_NORMAL);
1da177e4
LT
504 spin_lock(&acpi_device_lock);
505 dev->driver = NULL;
506 dev->driver_data = NULL;
507 atomic_dec(&drv->references);
508 }
509 }
9e89dde2
ZR
510 spin_unlock(&acpi_device_lock);
511}
512
513/**
514 * acpi_bus_register_driver - register a driver with the ACPI bus
515 * @driver: driver being registered
516 *
517 * Registers a driver with the ACPI bus. Searches the namespace for all
518 * devices that match the driver's criteria and binds. Returns zero for
519 * success or a negative error status for failure.
520 */
521int acpi_bus_register_driver(struct acpi_driver *driver)
522{
523
524 if (acpi_disabled)
525 return -ENODEV;
526
527 spin_lock(&acpi_device_lock);
528 list_add_tail(&driver->node, &acpi_bus_drivers);
529 spin_unlock(&acpi_device_lock);
530 acpi_driver_attach(driver);
531
532 return 0;
533}
534
535EXPORT_SYMBOL(acpi_bus_register_driver);
536
537/**
538 * acpi_bus_unregister_driver - unregisters a driver with the APIC bus
539 * @driver: driver to unregister
540 *
541 * Unregisters a driver with the ACPI bus. Searches the namespace for all
542 * devices that match the driver's criteria and unbinds.
543 */
544void acpi_bus_unregister_driver(struct acpi_driver *driver)
545{
546 acpi_driver_detach(driver);
547
548 if (!atomic_read(&driver->references)) {
549 spin_lock(&acpi_device_lock);
550 list_del_init(&driver->node);
551 spin_unlock(&acpi_device_lock);
552 }
553 return;
554}
555
556EXPORT_SYMBOL(acpi_bus_unregister_driver);
557
558/**
559 * acpi_bus_find_driver - check if there is a driver installed for the device
560 * @device: device that we are trying to find a supporting driver for
561 *
562 * Parses the list of registered drivers looking for a driver applicable for
563 * the specified device.
564 */
565static int acpi_bus_find_driver(struct acpi_device *device)
566{
567 int result = 0;
568 struct list_head *node, *next;
569
570
571 spin_lock(&acpi_device_lock);
572 list_for_each_safe(node, next, &acpi_bus_drivers) {
573 struct acpi_driver *driver =
574 container_of(node, struct acpi_driver, node);
575
576 atomic_inc(&driver->references);
577 spin_unlock(&acpi_device_lock);
5d9464a4 578 if (!acpi_bus_match(&(device->dev), &(driver->drv))) {
9e89dde2
ZR
579 result = acpi_bus_driver_init(device, driver);
580 if (!result)
581 goto Done;
582 }
583 atomic_dec(&driver->references);
584 spin_lock(&acpi_device_lock);
585 }
586 spin_unlock(&acpi_device_lock);
587
588 Done:
589 return result;
590}
591
592/* --------------------------------------------------------------------------
593 Device Enumeration
594 -------------------------------------------------------------------------- */
595acpi_status
596acpi_bus_get_ejd(acpi_handle handle, acpi_handle *ejd)
597{
598 acpi_status status;
599 acpi_handle tmp;
600 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
601 union acpi_object *obj;
602
603 status = acpi_get_handle(handle, "_EJD", &tmp);
604 if (ACPI_FAILURE(status))
605 return status;
606
607 status = acpi_evaluate_object(handle, "_EJD", NULL, &buffer);
608 if (ACPI_SUCCESS(status)) {
609 obj = buffer.pointer;
610 status = acpi_get_handle(NULL, obj->string.pointer, ejd);
611 kfree(buffer.pointer);
612 }
613 return status;
614}
615EXPORT_SYMBOL_GPL(acpi_bus_get_ejd);
616
617void acpi_bus_data_handler(acpi_handle handle, u32 function, void *context)
618{
619
620 /* TBD */
621
622 return;
623}
624
625int acpi_match_ids(struct acpi_device *device, char *ids)
626{
627 if (device->flags.hardware_id)
628 if (strstr(ids, device->pnp.hardware_id))
629 return 0;
630
631 if (device->flags.compatible_ids) {
632 struct acpi_compatible_id_list *cid_list = device->pnp.cid_list;
633 int i;
634
635 /* compare multiple _CID entries against driver ids */
636 for (i = 0; i < cid_list->count; i++) {
637 if (strstr(ids, cid_list->id[i].value))
638 return 0;
639 }
640 }
641 return -ENOENT;
642}
643
644static int acpi_bus_get_perf_flags(struct acpi_device *device)
645{
646 device->performance.state = ACPI_STATE_UNKNOWN;
647 return 0;
648}
649
650static acpi_status
651acpi_bus_extract_wakeup_device_power_package(struct acpi_device *device,
652 union acpi_object *package)
653{
654 int i = 0;
655 union acpi_object *element = NULL;
656
657 if (!device || !package || (package->package.count < 2))
658 return AE_BAD_PARAMETER;
659
660 element = &(package->package.elements[0]);
661 if (!element)
662 return AE_BAD_PARAMETER;
663 if (element->type == ACPI_TYPE_PACKAGE) {
664 if ((element->package.count < 2) ||
665 (element->package.elements[0].type !=
666 ACPI_TYPE_LOCAL_REFERENCE)
667 || (element->package.elements[1].type != ACPI_TYPE_INTEGER))
668 return AE_BAD_DATA;
669 device->wakeup.gpe_device =
670 element->package.elements[0].reference.handle;
671 device->wakeup.gpe_number =
672 (u32) element->package.elements[1].integer.value;
673 } else if (element->type == ACPI_TYPE_INTEGER) {
674 device->wakeup.gpe_number = element->integer.value;
675 } else
676 return AE_BAD_DATA;
677
678 element = &(package->package.elements[1]);
679 if (element->type != ACPI_TYPE_INTEGER) {
680 return AE_BAD_DATA;
681 }
682 device->wakeup.sleep_state = element->integer.value;
683
684 if ((package->package.count - 2) > ACPI_MAX_HANDLES) {
685 return AE_NO_MEMORY;
686 }
687 device->wakeup.resources.count = package->package.count - 2;
688 for (i = 0; i < device->wakeup.resources.count; i++) {
689 element = &(package->package.elements[i + 2]);
690 if (element->type != ACPI_TYPE_ANY) {
691 return AE_BAD_DATA;
692 }
693
694 device->wakeup.resources.handles[i] = element->reference.handle;
695 }
696
697 return AE_OK;
1da177e4
LT
698}
699
9e89dde2 700static int acpi_bus_get_wakeup_device_flags(struct acpi_device *device)
1da177e4 701{
9e89dde2
ZR
702 acpi_status status = 0;
703 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
704 union acpi_object *package = NULL;
1da177e4 705
1da177e4 706
9e89dde2
ZR
707 /* _PRW */
708 status = acpi_evaluate_object(device->handle, "_PRW", NULL, &buffer);
709 if (ACPI_FAILURE(status)) {
710 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PRW"));
711 goto end;
712 }
1da177e4 713
9e89dde2
ZR
714 package = (union acpi_object *)buffer.pointer;
715 status = acpi_bus_extract_wakeup_device_power_package(device, package);
716 if (ACPI_FAILURE(status)) {
717 ACPI_EXCEPTION((AE_INFO, status, "Extracting _PRW package"));
718 goto end;
719 }
1da177e4 720
9e89dde2 721 kfree(buffer.pointer);
1da177e4 722
9e89dde2
ZR
723 device->wakeup.flags.valid = 1;
724 /* Power button, Lid switch always enable wakeup */
725 if (!acpi_match_ids(device, "PNP0C0D,PNP0C0C,PNP0C0E"))
726 device->wakeup.flags.run_wake = 1;
1a365616 727
9e89dde2
ZR
728 end:
729 if (ACPI_FAILURE(status))
730 device->flags.wake_capable = 0;
731 return 0;
1da177e4 732}
4be44fcd 733
9e89dde2 734static int acpi_bus_get_power_flags(struct acpi_device *device)
1da177e4 735{
9e89dde2
ZR
736 acpi_status status = 0;
737 acpi_handle handle = NULL;
738 u32 i = 0;
1da177e4 739
1da177e4 740
9e89dde2
ZR
741 /*
742 * Power Management Flags
743 */
744 status = acpi_get_handle(device->handle, "_PSC", &handle);
745 if (ACPI_SUCCESS(status))
746 device->power.flags.explicit_get = 1;
747 status = acpi_get_handle(device->handle, "_IRC", &handle);
748 if (ACPI_SUCCESS(status))
749 device->power.flags.inrush_current = 1;
1da177e4 750
9e89dde2
ZR
751 /*
752 * Enumerate supported power management states
753 */
754 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3; i++) {
755 struct acpi_device_power_state *ps = &device->power.states[i];
756 char object_name[5] = { '_', 'P', 'R', '0' + i, '\0' };
757
758 /* Evaluate "_PRx" to se if power resources are referenced */
759 acpi_evaluate_reference(device->handle, object_name, NULL,
760 &ps->resources);
761 if (ps->resources.count) {
762 device->power.flags.power_resources = 1;
763 ps->flags.valid = 1;
1da177e4 764 }
1da177e4 765
9e89dde2
ZR
766 /* Evaluate "_PSx" to see if we can do explicit sets */
767 object_name[2] = 'S';
768 status = acpi_get_handle(device->handle, object_name, &handle);
769 if (ACPI_SUCCESS(status)) {
770 ps->flags.explicit_set = 1;
771 ps->flags.valid = 1;
772 }
1da177e4 773
9e89dde2
ZR
774 /* State is valid if we have some power control */
775 if (ps->resources.count || ps->flags.explicit_set)
776 ps->flags.valid = 1;
1da177e4 777
9e89dde2
ZR
778 ps->power = -1; /* Unknown - driver assigned */
779 ps->latency = -1; /* Unknown - driver assigned */
780 }
c8f7a62c 781
9e89dde2
ZR
782 /* Set defaults for D0 and D3 states (always valid) */
783 device->power.states[ACPI_STATE_D0].flags.valid = 1;
784 device->power.states[ACPI_STATE_D0].power = 100;
785 device->power.states[ACPI_STATE_D3].flags.valid = 1;
786 device->power.states[ACPI_STATE_D3].power = 0;
c8f7a62c 787
9e89dde2
ZR
788 /* TBD: System wake support and resource requirements. */
789
790 device->power.state = ACPI_STATE_UNKNOWN;
c8f7a62c 791
9e89dde2
ZR
792 return 0;
793}
c8f7a62c 794
4be44fcd 795static int acpi_bus_get_flags(struct acpi_device *device)
1da177e4 796{
4be44fcd
LB
797 acpi_status status = AE_OK;
798 acpi_handle temp = NULL;
1da177e4 799
1da177e4
LT
800
801 /* Presence of _STA indicates 'dynamic_status' */
802 status = acpi_get_handle(device->handle, "_STA", &temp);
803 if (ACPI_SUCCESS(status))
804 device->flags.dynamic_status = 1;
805
806 /* Presence of _CID indicates 'compatible_ids' */
807 status = acpi_get_handle(device->handle, "_CID", &temp);
808 if (ACPI_SUCCESS(status))
809 device->flags.compatible_ids = 1;
810
811 /* Presence of _RMV indicates 'removable' */
812 status = acpi_get_handle(device->handle, "_RMV", &temp);
813 if (ACPI_SUCCESS(status))
814 device->flags.removable = 1;
815
816 /* Presence of _EJD|_EJ0 indicates 'ejectable' */
817 status = acpi_get_handle(device->handle, "_EJD", &temp);
818 if (ACPI_SUCCESS(status))
819 device->flags.ejectable = 1;
820 else {
821 status = acpi_get_handle(device->handle, "_EJ0", &temp);
822 if (ACPI_SUCCESS(status))
823 device->flags.ejectable = 1;
824 }
825
826 /* Presence of _LCK indicates 'lockable' */
827 status = acpi_get_handle(device->handle, "_LCK", &temp);
828 if (ACPI_SUCCESS(status))
829 device->flags.lockable = 1;
830
831 /* Presence of _PS0|_PR0 indicates 'power manageable' */
832 status = acpi_get_handle(device->handle, "_PS0", &temp);
833 if (ACPI_FAILURE(status))
834 status = acpi_get_handle(device->handle, "_PR0", &temp);
835 if (ACPI_SUCCESS(status))
836 device->flags.power_manageable = 1;
837
838 /* Presence of _PRW indicates wake capable */
839 status = acpi_get_handle(device->handle, "_PRW", &temp);
840 if (ACPI_SUCCESS(status))
841 device->flags.wake_capable = 1;
842
843 /* TBD: Peformance management */
844
d550d98d 845 return 0;
1da177e4
LT
846}
847
4be44fcd
LB
848static void acpi_device_get_busid(struct acpi_device *device,
849 acpi_handle handle, int type)
1da177e4 850{
4be44fcd
LB
851 char bus_id[5] = { '?', 0 };
852 struct acpi_buffer buffer = { sizeof(bus_id), bus_id };
853 int i = 0;
1da177e4
LT
854
855 /*
856 * Bus ID
857 * ------
858 * The device's Bus ID is simply the object name.
859 * TBD: Shouldn't this value be unique (within the ACPI namespace)?
860 */
861 switch (type) {
862 case ACPI_BUS_TYPE_SYSTEM:
863 strcpy(device->pnp.bus_id, "ACPI");
864 break;
865 case ACPI_BUS_TYPE_POWER_BUTTON:
866 strcpy(device->pnp.bus_id, "PWRF");
867 break;
868 case ACPI_BUS_TYPE_SLEEP_BUTTON:
869 strcpy(device->pnp.bus_id, "SLPF");
870 break;
871 default:
872 acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer);
873 /* Clean up trailing underscores (if any) */
874 for (i = 3; i > 1; i--) {
875 if (bus_id[i] == '_')
876 bus_id[i] = '\0';
877 else
878 break;
879 }
880 strcpy(device->pnp.bus_id, bus_id);
881 break;
882 }
883}
884
4be44fcd
LB
885static void acpi_device_set_id(struct acpi_device *device,
886 struct acpi_device *parent, acpi_handle handle,
887 int type)
1da177e4 888{
4be44fcd
LB
889 struct acpi_device_info *info;
890 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
891 char *hid = NULL;
892 char *uid = NULL;
1da177e4 893 struct acpi_compatible_id_list *cid_list = NULL;
4be44fcd 894 acpi_status status;
1da177e4
LT
895
896 switch (type) {
897 case ACPI_BUS_TYPE_DEVICE:
898 status = acpi_get_object_info(handle, &buffer);
899 if (ACPI_FAILURE(status)) {
4be44fcd 900 printk("%s: Error reading device info\n", __FUNCTION__);
1da177e4
LT
901 return;
902 }
903
904 info = buffer.pointer;
905 if (info->valid & ACPI_VALID_HID)
906 hid = info->hardware_id.value;
907 if (info->valid & ACPI_VALID_UID)
908 uid = info->unique_id.value;
909 if (info->valid & ACPI_VALID_CID)
910 cid_list = &info->compatibility_id;
911 if (info->valid & ACPI_VALID_ADR) {
912 device->pnp.bus_address = info->address;
913 device->flags.bus_address = 1;
914 }
915 break;
916 case ACPI_BUS_TYPE_POWER:
917 hid = ACPI_POWER_HID;
918 break;
919 case ACPI_BUS_TYPE_PROCESSOR:
920 hid = ACPI_PROCESSOR_HID;
921 break;
922 case ACPI_BUS_TYPE_SYSTEM:
923 hid = ACPI_SYSTEM_HID;
924 break;
925 case ACPI_BUS_TYPE_THERMAL:
926 hid = ACPI_THERMAL_HID;
927 break;
928 case ACPI_BUS_TYPE_POWER_BUTTON:
929 hid = ACPI_BUTTON_HID_POWERF;
930 break;
931 case ACPI_BUS_TYPE_SLEEP_BUTTON:
932 hid = ACPI_BUTTON_HID_SLEEPF;
933 break;
934 }
935
936 /*
937 * \_SB
938 * ----
939 * Fix for the system root bus device -- the only root-level device.
940 */
c51a4de8 941 if (((acpi_handle)parent == ACPI_ROOT_OBJECT) && (type == ACPI_BUS_TYPE_DEVICE)) {
1da177e4
LT
942 hid = ACPI_BUS_HID;
943 strcpy(device->pnp.device_name, ACPI_BUS_DEVICE_NAME);
944 strcpy(device->pnp.device_class, ACPI_BUS_CLASS);
945 }
946
947 if (hid) {
948 strcpy(device->pnp.hardware_id, hid);
949 device->flags.hardware_id = 1;
950 }
951 if (uid) {
952 strcpy(device->pnp.unique_id, uid);
953 device->flags.unique_id = 1;
954 }
955 if (cid_list) {
956 device->pnp.cid_list = kmalloc(cid_list->size, GFP_KERNEL);
957 if (device->pnp.cid_list)
958 memcpy(device->pnp.cid_list, cid_list, cid_list->size);
959 else
960 printk(KERN_ERR "Memory allocation error\n");
961 }
962
02438d87 963 kfree(buffer.pointer);
1da177e4
LT
964}
965
4be44fcd 966static int acpi_device_set_context(struct acpi_device *device, int type)
1da177e4
LT
967{
968 acpi_status status = AE_OK;
969 int result = 0;
970 /*
971 * Context
972 * -------
973 * Attach this 'struct acpi_device' to the ACPI object. This makes
974 * resolutions from handle->device very efficient. Note that we need
975 * to be careful with fixed-feature devices as they all attach to the
976 * root object.
977 */
4be44fcd 978 if (type != ACPI_BUS_TYPE_POWER_BUTTON &&
1da177e4
LT
979 type != ACPI_BUS_TYPE_SLEEP_BUTTON) {
980 status = acpi_attach_data(device->handle,
4be44fcd 981 acpi_bus_data_handler, device);
1da177e4
LT
982
983 if (ACPI_FAILURE(status)) {
984 printk("Error attaching device data\n");
985 result = -ENODEV;
986 }
987 }
988 return result;
989}
990
4be44fcd
LB
991static void acpi_device_get_debug_info(struct acpi_device *device,
992 acpi_handle handle, int type)
1da177e4
LT
993{
994#ifdef CONFIG_ACPI_DEBUG_OUTPUT
4be44fcd
LB
995 char *type_string = NULL;
996 char name[80] = { '?', '\0' };
997 struct acpi_buffer buffer = { sizeof(name), name };
1da177e4
LT
998
999 switch (type) {
1000 case ACPI_BUS_TYPE_DEVICE:
1001 type_string = "Device";
1002 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1003 break;
1004 case ACPI_BUS_TYPE_POWER:
1005 type_string = "Power Resource";
1006 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1007 break;
1008 case ACPI_BUS_TYPE_PROCESSOR:
1009 type_string = "Processor";
1010 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1011 break;
1012 case ACPI_BUS_TYPE_SYSTEM:
1013 type_string = "System";
1014 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1015 break;
1016 case ACPI_BUS_TYPE_THERMAL:
1017 type_string = "Thermal Zone";
1018 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1019 break;
1020 case ACPI_BUS_TYPE_POWER_BUTTON:
1021 type_string = "Power Button";
1022 sprintf(name, "PWRB");
1023 break;
1024 case ACPI_BUS_TYPE_SLEEP_BUTTON:
1025 type_string = "Sleep Button";
1026 sprintf(name, "SLPB");
1027 break;
1028 }
1029
1030 printk(KERN_DEBUG "Found %s %s [%p]\n", type_string, name, handle);
4be44fcd 1031#endif /*CONFIG_ACPI_DEBUG_OUTPUT */
1da177e4
LT
1032}
1033
4be44fcd 1034static int acpi_bus_remove(struct acpi_device *dev, int rmdevice)
1da177e4 1035{
4be44fcd
LB
1036 int result = 0;
1037 struct acpi_driver *driver;
1038
1da177e4
LT
1039
1040 if (!dev)
d550d98d 1041 return -EINVAL;
1da177e4
LT
1042
1043 driver = dev->driver;
1044
1045 if ((driver) && (driver->ops.remove)) {
1046
1047 if (driver->ops.stop) {
1048 result = driver->ops.stop(dev, ACPI_BUS_REMOVAL_EJECT);
1049 if (result)
d550d98d 1050 return result;
1da177e4
LT
1051 }
1052
1053 result = dev->driver->ops.remove(dev, ACPI_BUS_REMOVAL_EJECT);
1054 if (result) {
d550d98d 1055 return result;
1da177e4
LT
1056 }
1057
1058 atomic_dec(&dev->driver->references);
1059 dev->driver = NULL;
1060 acpi_driver_data(dev) = NULL;
1061 }
1062
1063 if (!rmdevice)
d550d98d 1064 return 0;
1da177e4
LT
1065
1066 if (dev->flags.bus_address) {
1067 if ((dev->parent) && (dev->parent->ops.unbind))
1068 dev->parent->ops.unbind(dev);
1069 }
4be44fcd 1070
1da177e4
LT
1071 acpi_device_unregister(dev, ACPI_BUS_REMOVAL_EJECT);
1072
d550d98d 1073 return 0;
1da177e4
LT
1074}
1075
3fb02738 1076static int
4be44fcd
LB
1077acpi_add_single_object(struct acpi_device **child,
1078 struct acpi_device *parent, acpi_handle handle, int type)
1da177e4 1079{
4be44fcd
LB
1080 int result = 0;
1081 struct acpi_device *device = NULL;
1da177e4 1082
1da177e4
LT
1083
1084 if (!child)
d550d98d 1085 return -EINVAL;
1da177e4
LT
1086
1087 device = kmalloc(sizeof(struct acpi_device), GFP_KERNEL);
1088 if (!device) {
6468463a 1089 printk(KERN_ERR PREFIX "Memory allocation error\n");
d550d98d 1090 return -ENOMEM;
1da177e4
LT
1091 }
1092 memset(device, 0, sizeof(struct acpi_device));
1093
1094 device->handle = handle;
1095 device->parent = parent;
1096
4be44fcd 1097 acpi_device_get_busid(device, handle, type);
1da177e4
LT
1098
1099 /*
1100 * Flags
1101 * -----
1102 * Get prior to calling acpi_bus_get_status() so we know whether
1103 * or not _STA is present. Note that we only look for object
1104 * handles -- cannot evaluate objects until we know the device is
1105 * present and properly initialized.
1106 */
1107 result = acpi_bus_get_flags(device);
1108 if (result)
1109 goto end;
1110
1111 /*
1112 * Status
1113 * ------
6940faba
KT
1114 * See if the device is present. We always assume that non-Device
1115 * and non-Processor objects (e.g. thermal zones, power resources,
1116 * etc.) are present, functioning, etc. (at least when parent object
1117 * is present). Note that _STA has a different meaning for some
1118 * objects (e.g. power resources) so we need to be careful how we use
1119 * it.
1da177e4
LT
1120 */
1121 switch (type) {
6940faba 1122 case ACPI_BUS_TYPE_PROCESSOR:
1da177e4
LT
1123 case ACPI_BUS_TYPE_DEVICE:
1124 result = acpi_bus_get_status(device);
1125 if (ACPI_FAILURE(result) || !device->status.present) {
1126 result = -ENOENT;
1127 goto end;
1128 }
1129 break;
1130 default:
1131 STRUCT_TO_INT(device->status) = 0x0F;
1132 break;
1133 }
1134
1135 /*
1136 * Initialize Device
1137 * -----------------
1138 * TBD: Synch with Core's enumeration/initialization process.
1139 */
1140
1141 /*
1142 * Hardware ID, Unique ID, & Bus Address
1143 * -------------------------------------
1144 */
4be44fcd 1145 acpi_device_set_id(device, parent, handle, type);
1da177e4
LT
1146
1147 /*
1148 * Power Management
1149 * ----------------
1150 */
1151 if (device->flags.power_manageable) {
1152 result = acpi_bus_get_power_flags(device);
1153 if (result)
1154 goto end;
1155 }
1156
4be44fcd 1157 /*
1da177e4
LT
1158 * Wakeup device management
1159 *-----------------------
1160 */
1161 if (device->flags.wake_capable) {
1162 result = acpi_bus_get_wakeup_device_flags(device);
1163 if (result)
1164 goto end;
1165 }
1166
1167 /*
1168 * Performance Management
1169 * ----------------------
1170 */
1171 if (device->flags.performance_manageable) {
1172 result = acpi_bus_get_perf_flags(device);
1173 if (result)
1174 goto end;
1175 }
1176
4be44fcd 1177 if ((result = acpi_device_set_context(device, type)))
1da177e4
LT
1178 goto end;
1179
4be44fcd 1180 acpi_device_get_debug_info(device, handle, type);
1da177e4 1181
4be44fcd 1182 acpi_device_register(device, parent);
1da177e4
LT
1183
1184 /*
1185 * Bind _ADR-Based Devices
1186 * -----------------------
1187 * If there's a a bus address (_ADR) then we utilize the parent's
1188 * 'bind' function (if exists) to bind the ACPI- and natively-
1189 * enumerated device representations.
1190 */
1191 if (device->flags.bus_address) {
1192 if (device->parent && device->parent->ops.bind)
1193 device->parent->ops.bind(device);
1194 }
1195
1196 /*
1197 * Locate & Attach Driver
1198 * ----------------------
1199 * If there's a hardware id (_HID) or compatible ids (_CID) we check
1200 * to see if there's a driver installed for this kind of device. Note
1201 * that drivers can install before or after a device is enumerated.
1202 *
1203 * TBD: Assumes LDM provides driver hot-plug capability.
1204 */
bd7ce5b5 1205 acpi_bus_find_driver(device);
1da177e4 1206
4be44fcd 1207 end:
1da177e4
LT
1208 if (!result)
1209 *child = device;
1210 else {
6044ec88 1211 kfree(device->pnp.cid_list);
1da177e4
LT
1212 kfree(device);
1213 }
1214
d550d98d 1215 return result;
1da177e4 1216}
1da177e4 1217
4be44fcd 1218static int acpi_bus_scan(struct acpi_device *start, struct acpi_bus_ops *ops)
1da177e4 1219{
4be44fcd
LB
1220 acpi_status status = AE_OK;
1221 struct acpi_device *parent = NULL;
1222 struct acpi_device *child = NULL;
1223 acpi_handle phandle = NULL;
1224 acpi_handle chandle = NULL;
1225 acpi_object_type type = 0;
1226 u32 level = 1;
1da177e4 1227
1da177e4
LT
1228
1229 if (!start)
d550d98d 1230 return -EINVAL;
1da177e4
LT
1231
1232 parent = start;
1233 phandle = start->handle;
4be44fcd 1234
1da177e4
LT
1235 /*
1236 * Parse through the ACPI namespace, identify all 'devices', and
1237 * create a new 'struct acpi_device' for each.
1238 */
1239 while ((level > 0) && parent) {
1240
1241 status = acpi_get_next_object(ACPI_TYPE_ANY, phandle,
4be44fcd 1242 chandle, &chandle);
1da177e4
LT
1243
1244 /*
1245 * If this scope is exhausted then move our way back up.
1246 */
1247 if (ACPI_FAILURE(status)) {
1248 level--;
1249 chandle = phandle;
1250 acpi_get_parent(phandle, &phandle);
1251 if (parent->parent)
1252 parent = parent->parent;
1253 continue;
1254 }
1255
1256 status = acpi_get_type(chandle, &type);
1257 if (ACPI_FAILURE(status))
1258 continue;
1259
1260 /*
1261 * If this is a scope object then parse it (depth-first).
1262 */
1263 if (type == ACPI_TYPE_LOCAL_SCOPE) {
1264 level++;
1265 phandle = chandle;
1266 chandle = NULL;
1267 continue;
1268 }
1269
1270 /*
1271 * We're only interested in objects that we consider 'devices'.
1272 */
1273 switch (type) {
1274 case ACPI_TYPE_DEVICE:
1275 type = ACPI_BUS_TYPE_DEVICE;
1276 break;
1277 case ACPI_TYPE_PROCESSOR:
1278 type = ACPI_BUS_TYPE_PROCESSOR;
1279 break;
1280 case ACPI_TYPE_THERMAL:
1281 type = ACPI_BUS_TYPE_THERMAL;
1282 break;
1283 case ACPI_TYPE_POWER:
1284 type = ACPI_BUS_TYPE_POWER;
1285 break;
1286 default:
1287 continue;
1288 }
1289
3fb02738
RS
1290 if (ops->acpi_op_add)
1291 status = acpi_add_single_object(&child, parent,
4be44fcd
LB
1292 chandle, type);
1293 else
3fb02738
RS
1294 status = acpi_bus_get_device(chandle, &child);
1295
4be44fcd
LB
1296 if (ACPI_FAILURE(status))
1297 continue;
3fb02738
RS
1298
1299 if (ops->acpi_op_start) {
1300 status = acpi_start_single_object(child);
1301 if (ACPI_FAILURE(status))
1302 continue;
1303 }
1da177e4
LT
1304
1305 /*
1306 * If the device is present, enabled, and functioning then
1307 * parse its scope (depth-first). Note that we need to
1308 * represent absent devices to facilitate PnP notifications
1309 * -- but only the subtree head (not all of its children,
1310 * which will be enumerated when the parent is inserted).
1311 *
1312 * TBD: Need notifications and other detection mechanisms
4be44fcd 1313 * in place before we can fully implement this.
1da177e4
LT
1314 */
1315 if (child->status.present) {
1316 status = acpi_get_next_object(ACPI_TYPE_ANY, chandle,
1317 NULL, NULL);
1318 if (ACPI_SUCCESS(status)) {
1319 level++;
1320 phandle = chandle;
1321 chandle = NULL;
1322 parent = child;
1323 }
1324 }
1325 }
1326
d550d98d 1327 return 0;
1da177e4 1328}
1da177e4 1329
3fb02738 1330int
4be44fcd
LB
1331acpi_bus_add(struct acpi_device **child,
1332 struct acpi_device *parent, acpi_handle handle, int type)
3fb02738
RS
1333{
1334 int result;
1335 struct acpi_bus_ops ops;
1336
3fb02738
RS
1337
1338 result = acpi_add_single_object(child, parent, handle, type);
1339 if (!result) {
1340 memset(&ops, 0, sizeof(ops));
1341 ops.acpi_op_add = 1;
1342 result = acpi_bus_scan(*child, &ops);
1343 }
d550d98d 1344 return result;
3fb02738 1345}
4be44fcd 1346
3fb02738
RS
1347EXPORT_SYMBOL(acpi_bus_add);
1348
4be44fcd 1349int acpi_bus_start(struct acpi_device *device)
3fb02738
RS
1350{
1351 int result;
1352 struct acpi_bus_ops ops;
1353
3fb02738
RS
1354
1355 if (!device)
d550d98d 1356 return -EINVAL;
3fb02738
RS
1357
1358 result = acpi_start_single_object(device);
1359 if (!result) {
1360 memset(&ops, 0, sizeof(ops));
1361 ops.acpi_op_start = 1;
1362 result = acpi_bus_scan(device, &ops);
1363 }
d550d98d 1364 return result;
3fb02738 1365}
4be44fcd 1366
3fb02738 1367EXPORT_SYMBOL(acpi_bus_start);
1da177e4 1368
ceaba663 1369int acpi_bus_trim(struct acpi_device *start, int rmdevice)
1da177e4 1370{
4be44fcd
LB
1371 acpi_status status;
1372 struct acpi_device *parent, *child;
1373 acpi_handle phandle, chandle;
1374 acpi_object_type type;
1375 u32 level = 1;
1376 int err = 0;
1377
1378 parent = start;
1da177e4
LT
1379 phandle = start->handle;
1380 child = chandle = NULL;
1381
1382 while ((level > 0) && parent && (!err)) {
1383 status = acpi_get_next_object(ACPI_TYPE_ANY, phandle,
4be44fcd 1384 chandle, &chandle);
1da177e4
LT
1385
1386 /*
1387 * If this scope is exhausted then move our way back up.
1388 */
1389 if (ACPI_FAILURE(status)) {
1390 level--;
1391 chandle = phandle;
1392 acpi_get_parent(phandle, &phandle);
1393 child = parent;
1394 parent = parent->parent;
1395
1396 if (level == 0)
1397 err = acpi_bus_remove(child, rmdevice);
1398 else
1399 err = acpi_bus_remove(child, 1);
1400
1401 continue;
1402 }
1403
1404 status = acpi_get_type(chandle, &type);
1405 if (ACPI_FAILURE(status)) {
1406 continue;
1407 }
1408 /*
1409 * If there is a device corresponding to chandle then
1410 * parse it (depth-first).
1411 */
1412 if (acpi_bus_get_device(chandle, &child) == 0) {
1413 level++;
1414 phandle = chandle;
1415 chandle = NULL;
1416 parent = child;
1417 }
1418 continue;
1419 }
1420 return err;
1421}
ceaba663
KA
1422EXPORT_SYMBOL_GPL(acpi_bus_trim);
1423
1da177e4 1424
4be44fcd 1425static int acpi_bus_scan_fixed(struct acpi_device *root)
1da177e4 1426{
4be44fcd
LB
1427 int result = 0;
1428 struct acpi_device *device = NULL;
1da177e4 1429
1da177e4
LT
1430
1431 if (!root)
d550d98d 1432 return -ENODEV;
1da177e4
LT
1433
1434 /*
1435 * Enumerate all fixed-feature devices.
1436 */
3fb02738
RS
1437 if (acpi_fadt.pwr_button == 0) {
1438 result = acpi_add_single_object(&device, acpi_root,
4be44fcd
LB
1439 NULL,
1440 ACPI_BUS_TYPE_POWER_BUTTON);
3fb02738
RS
1441 if (!result)
1442 result = acpi_start_single_object(device);
1443 }
1da177e4 1444
3fb02738
RS
1445 if (acpi_fadt.sleep_button == 0) {
1446 result = acpi_add_single_object(&device, acpi_root,
4be44fcd
LB
1447 NULL,
1448 ACPI_BUS_TYPE_SLEEP_BUTTON);
3fb02738
RS
1449 if (!result)
1450 result = acpi_start_single_object(device);
1451 }
1da177e4 1452
d550d98d 1453 return result;
1da177e4
LT
1454}
1455
1da177e4
LT
1456static int __init acpi_scan_init(void)
1457{
1458 int result;
3fb02738 1459 struct acpi_bus_ops ops;
1da177e4 1460
1da177e4
LT
1461
1462 if (acpi_disabled)
d550d98d 1463 return 0;
1da177e4 1464
9b6d97b6
RD
1465 result = kset_register(&acpi_namespace_kset);
1466 if (result < 0)
1467 printk(KERN_ERR PREFIX "kset_register error: %d\n", result);
1da177e4 1468
5b327265
PM
1469 result = bus_register(&acpi_bus_type);
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 bus type\n");
1473 }
1474
1da177e4
LT
1475 /*
1476 * Create the root device in the bus's device tree
1477 */
3fb02738 1478 result = acpi_add_single_object(&acpi_root, NULL, ACPI_ROOT_OBJECT,
4be44fcd 1479 ACPI_BUS_TYPE_SYSTEM);
1da177e4
LT
1480 if (result)
1481 goto Done;
1482
3fb02738 1483 result = acpi_start_single_object(acpi_root);
5b327265
PM
1484 if (result)
1485 goto Done;
1486
1487 acpi_root->dev.bus = &acpi_bus_type;
1488 snprintf(acpi_root->dev.bus_id, BUS_ID_SIZE, "%s", acpi_bus_type.name);
1489 result = device_register(&acpi_root->dev);
1490 if (result) {
1491 /* We don't want to quit even if we failed to add suspend/resume */
1492 printk(KERN_ERR PREFIX "Could not register device\n");
1493 }
3fb02738 1494
1da177e4
LT
1495 /*
1496 * Enumerate devices in the ACPI namespace.
1497 */
1498 result = acpi_bus_scan_fixed(acpi_root);
3fb02738
RS
1499 if (!result) {
1500 memset(&ops, 0, sizeof(ops));
1501 ops.acpi_op_add = 1;
1502 ops.acpi_op_start = 1;
1503 result = acpi_bus_scan(acpi_root, &ops);
1504 }
1da177e4
LT
1505
1506 if (result)
1507 acpi_device_unregister(acpi_root, ACPI_BUS_REMOVAL_NORMAL);
1508
4be44fcd 1509 Done:
d550d98d 1510 return result;
1da177e4
LT
1511}
1512
1513subsys_initcall(acpi_scan_init);