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