[PATCH] W1: w1_netlink: New init/fini netlink callbacks.
[linux-2.6/verdex.git] / drivers / acpi / scan.c
blobc6db591479de3e02504d40ac8e0cee2d2bebdeab
1 /*
2 * scan.c - support for transforming the ACPI namespace into individual objects
3 */
5 #include <linux/module.h>
6 #include <linux/init.h>
7 #include <linux/acpi.h>
9 #include <acpi/acpi_drivers.h>
10 #include <acpi/acinterp.h> /* for acpi_ex_eisa_id_to_string() */
12 #define _COMPONENT ACPI_BUS_COMPONENT
13 ACPI_MODULE_NAME("scan")
14 #define STRUCT_TO_INT(s) (*((int*)&s))
15 extern struct acpi_device *acpi_root;
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"
22 static LIST_HEAD(acpi_device_list);
23 DEFINE_SPINLOCK(acpi_device_lock);
24 LIST_HEAD(acpi_wakeup_device_list);
26 static int acpi_bus_trim(struct acpi_device *start, int rmdevice);
28 static void acpi_device_release(struct kobject *kobj)
30 struct acpi_device *dev = container_of(kobj, struct acpi_device, kobj);
31 if (dev->pnp.cid_list)
32 kfree(dev->pnp.cid_list);
33 kfree(dev);
36 struct acpi_device_attribute {
37 struct attribute attr;
38 ssize_t(*show) (struct acpi_device *, char *);
39 ssize_t(*store) (struct acpi_device *, const char *, size_t);
42 typedef void acpi_device_sysfs_files(struct kobject *,
43 const struct attribute *);
45 static void setup_sys_fs_device_files(struct acpi_device *dev,
46 acpi_device_sysfs_files * func);
48 #define create_sysfs_device_files(dev) \
49 setup_sys_fs_device_files(dev, (acpi_device_sysfs_files *)&sysfs_create_file)
50 #define remove_sysfs_device_files(dev) \
51 setup_sys_fs_device_files(dev, (acpi_device_sysfs_files *)&sysfs_remove_file)
53 #define to_acpi_device(n) container_of(n, struct acpi_device, kobj)
54 #define to_handle_attr(n) container_of(n, struct acpi_device_attribute, attr);
56 static ssize_t acpi_device_attr_show(struct kobject *kobj,
57 struct attribute *attr, char *buf)
59 struct acpi_device *device = to_acpi_device(kobj);
60 struct acpi_device_attribute *attribute = to_handle_attr(attr);
61 return attribute->show ? attribute->show(device, buf) : -EIO;
63 static ssize_t acpi_device_attr_store(struct kobject *kobj,
64 struct attribute *attr, const char *buf,
65 size_t len)
67 struct acpi_device *device = to_acpi_device(kobj);
68 struct acpi_device_attribute *attribute = to_handle_attr(attr);
69 return attribute->store ? attribute->store(device, buf, len) : -EIO;
72 static struct sysfs_ops acpi_device_sysfs_ops = {
73 .show = acpi_device_attr_show,
74 .store = acpi_device_attr_store,
77 static struct kobj_type ktype_acpi_ns = {
78 .sysfs_ops = &acpi_device_sysfs_ops,
79 .release = acpi_device_release,
82 static int namespace_hotplug(struct kset *kset, struct kobject *kobj,
83 char **envp, int num_envp, char *buffer,
84 int buffer_size)
86 struct acpi_device *dev = to_acpi_device(kobj);
87 int i = 0;
88 int len = 0;
90 if (!dev->driver)
91 return 0;
93 if (add_hotplug_env_var(envp, num_envp, &i, buffer, buffer_size, &len,
94 "PHYSDEVDRIVER=%s", dev->driver->name))
95 return -ENOMEM;
97 envp[i] = NULL;
99 return 0;
102 static struct kset_hotplug_ops namespace_hotplug_ops = {
103 .hotplug = &namespace_hotplug,
106 static struct kset acpi_namespace_kset = {
107 .kobj = {
108 .name = "namespace",
110 .subsys = &acpi_subsys,
111 .ktype = &ktype_acpi_ns,
112 .hotplug_ops = &namespace_hotplug_ops,
115 static void acpi_device_register(struct acpi_device *device,
116 struct acpi_device *parent)
119 * Linkage
120 * -------
121 * Link this device to its parent and siblings.
123 INIT_LIST_HEAD(&device->children);
124 INIT_LIST_HEAD(&device->node);
125 INIT_LIST_HEAD(&device->g_list);
126 INIT_LIST_HEAD(&device->wakeup_list);
128 spin_lock(&acpi_device_lock);
129 if (device->parent) {
130 list_add_tail(&device->node, &device->parent->children);
131 list_add_tail(&device->g_list, &device->parent->g_list);
132 } else
133 list_add_tail(&device->g_list, &acpi_device_list);
134 if (device->wakeup.flags.valid)
135 list_add_tail(&device->wakeup_list, &acpi_wakeup_device_list);
136 spin_unlock(&acpi_device_lock);
138 strlcpy(device->kobj.name, device->pnp.bus_id, KOBJ_NAME_LEN);
139 if (parent)
140 device->kobj.parent = &parent->kobj;
141 device->kobj.ktype = &ktype_acpi_ns;
142 device->kobj.kset = &acpi_namespace_kset;
143 kobject_register(&device->kobj);
144 create_sysfs_device_files(device);
147 static int acpi_device_unregister(struct acpi_device *device, int type)
149 spin_lock(&acpi_device_lock);
150 if (device->parent) {
151 list_del(&device->node);
152 list_del(&device->g_list);
153 } else
154 list_del(&device->g_list);
156 list_del(&device->wakeup_list);
158 spin_unlock(&acpi_device_lock);
160 acpi_detach_data(device->handle, acpi_bus_data_handler);
161 remove_sysfs_device_files(device);
162 kobject_unregister(&device->kobj);
163 return 0;
166 void acpi_bus_data_handler(acpi_handle handle, u32 function, void *context)
168 ACPI_FUNCTION_TRACE("acpi_bus_data_handler");
170 /* TBD */
172 return_VOID;
175 static int acpi_bus_get_power_flags(struct acpi_device *device)
177 acpi_status status = 0;
178 acpi_handle handle = NULL;
179 u32 i = 0;
181 ACPI_FUNCTION_TRACE("acpi_bus_get_power_flags");
184 * Power Management Flags
186 status = acpi_get_handle(device->handle, "_PSC", &handle);
187 if (ACPI_SUCCESS(status))
188 device->power.flags.explicit_get = 1;
189 status = acpi_get_handle(device->handle, "_IRC", &handle);
190 if (ACPI_SUCCESS(status))
191 device->power.flags.inrush_current = 1;
194 * Enumerate supported power management states
196 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3; i++) {
197 struct acpi_device_power_state *ps = &device->power.states[i];
198 char object_name[5] = { '_', 'P', 'R', '0' + i, '\0' };
200 /* Evaluate "_PRx" to se if power resources are referenced */
201 acpi_evaluate_reference(device->handle, object_name, NULL,
202 &ps->resources);
203 if (ps->resources.count) {
204 device->power.flags.power_resources = 1;
205 ps->flags.valid = 1;
208 /* Evaluate "_PSx" to see if we can do explicit sets */
209 object_name[2] = 'S';
210 status = acpi_get_handle(device->handle, object_name, &handle);
211 if (ACPI_SUCCESS(status)) {
212 ps->flags.explicit_set = 1;
213 ps->flags.valid = 1;
216 /* State is valid if we have some power control */
217 if (ps->resources.count || ps->flags.explicit_set)
218 ps->flags.valid = 1;
220 ps->power = -1; /* Unknown - driver assigned */
221 ps->latency = -1; /* Unknown - driver assigned */
224 /* Set defaults for D0 and D3 states (always valid) */
225 device->power.states[ACPI_STATE_D0].flags.valid = 1;
226 device->power.states[ACPI_STATE_D0].power = 100;
227 device->power.states[ACPI_STATE_D3].flags.valid = 1;
228 device->power.states[ACPI_STATE_D3].power = 0;
230 /* TBD: System wake support and resource requirements. */
232 device->power.state = ACPI_STATE_UNKNOWN;
234 return_VALUE(0);
237 int acpi_match_ids(struct acpi_device *device, char *ids)
239 int error = 0;
240 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
242 if (device->flags.hardware_id)
243 if (strstr(ids, device->pnp.hardware_id))
244 goto Done;
246 if (device->flags.compatible_ids) {
247 struct acpi_compatible_id_list *cid_list = device->pnp.cid_list;
248 int i;
250 /* compare multiple _CID entries against driver ids */
251 for (i = 0; i < cid_list->count; i++) {
252 if (strstr(ids, cid_list->id[i].value))
253 goto Done;
256 error = -ENOENT;
258 Done:
259 if (buffer.pointer)
260 acpi_os_free(buffer.pointer);
261 return error;
264 static acpi_status
265 acpi_bus_extract_wakeup_device_power_package(struct acpi_device *device,
266 union acpi_object *package)
268 int i = 0;
269 union acpi_object *element = NULL;
271 if (!device || !package || (package->package.count < 2))
272 return AE_BAD_PARAMETER;
274 element = &(package->package.elements[0]);
275 if (!element)
276 return AE_BAD_PARAMETER;
277 if (element->type == ACPI_TYPE_PACKAGE) {
278 if ((element->package.count < 2) ||
279 (element->package.elements[0].type !=
280 ACPI_TYPE_LOCAL_REFERENCE)
281 || (element->package.elements[1].type != ACPI_TYPE_INTEGER))
282 return AE_BAD_DATA;
283 device->wakeup.gpe_device =
284 element->package.elements[0].reference.handle;
285 device->wakeup.gpe_number =
286 (u32) element->package.elements[1].integer.value;
287 } else if (element->type == ACPI_TYPE_INTEGER) {
288 device->wakeup.gpe_number = element->integer.value;
289 } else
290 return AE_BAD_DATA;
292 element = &(package->package.elements[1]);
293 if (element->type != ACPI_TYPE_INTEGER) {
294 return AE_BAD_DATA;
296 device->wakeup.sleep_state = element->integer.value;
298 if ((package->package.count - 2) > ACPI_MAX_HANDLES) {
299 return AE_NO_MEMORY;
301 device->wakeup.resources.count = package->package.count - 2;
302 for (i = 0; i < device->wakeup.resources.count; i++) {
303 element = &(package->package.elements[i + 2]);
304 if (element->type != ACPI_TYPE_ANY) {
305 return AE_BAD_DATA;
308 device->wakeup.resources.handles[i] = element->reference.handle;
311 return AE_OK;
314 static int acpi_bus_get_wakeup_device_flags(struct acpi_device *device)
316 acpi_status status = 0;
317 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
318 union acpi_object *package = NULL;
320 ACPI_FUNCTION_TRACE("acpi_bus_get_wakeup_flags");
322 /* _PRW */
323 status = acpi_evaluate_object(device->handle, "_PRW", NULL, &buffer);
324 if (ACPI_FAILURE(status)) {
325 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Error evaluating _PRW\n"));
326 goto end;
329 package = (union acpi_object *)buffer.pointer;
330 status = acpi_bus_extract_wakeup_device_power_package(device, package);
331 if (ACPI_FAILURE(status)) {
332 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
333 "Error extracting _PRW package\n"));
334 goto end;
337 acpi_os_free(buffer.pointer);
339 device->wakeup.flags.valid = 1;
340 /* Power button, Lid switch always enable wakeup */
341 if (!acpi_match_ids(device, "PNP0C0D,PNP0C0C,PNP0C0E"))
342 device->wakeup.flags.run_wake = 1;
344 end:
345 if (ACPI_FAILURE(status))
346 device->flags.wake_capable = 0;
347 return_VALUE(0);
350 /* --------------------------------------------------------------------------
351 ACPI hotplug sysfs device file support
352 -------------------------------------------------------------------------- */
353 static ssize_t acpi_eject_store(struct acpi_device *device,
354 const char *buf, size_t count);
356 #define ACPI_DEVICE_ATTR(_name,_mode,_show,_store) \
357 static struct acpi_device_attribute acpi_device_attr_##_name = \
358 __ATTR(_name, _mode, _show, _store)
360 ACPI_DEVICE_ATTR(eject, 0200, NULL, acpi_eject_store);
363 * setup_sys_fs_device_files - sets up the device files under device namespace
364 * @dev: acpi_device object
365 * @func: function pointer to create or destroy the device file
367 static void
368 setup_sys_fs_device_files(struct acpi_device *dev,
369 acpi_device_sysfs_files * func)
371 acpi_status status;
372 acpi_handle temp = NULL;
375 * If device has _EJ0, 'eject' file is created that is used to trigger
376 * hot-removal function from userland.
378 status = acpi_get_handle(dev->handle, "_EJ0", &temp);
379 if (ACPI_SUCCESS(status))
380 (*(func)) (&dev->kobj, &acpi_device_attr_eject.attr);
383 static int acpi_eject_operation(acpi_handle handle, int lockable)
385 struct acpi_object_list arg_list;
386 union acpi_object arg;
387 acpi_status status = AE_OK;
390 * TBD: evaluate _PS3?
393 if (lockable) {
394 arg_list.count = 1;
395 arg_list.pointer = &arg;
396 arg.type = ACPI_TYPE_INTEGER;
397 arg.integer.value = 0;
398 acpi_evaluate_object(handle, "_LCK", &arg_list, NULL);
401 arg_list.count = 1;
402 arg_list.pointer = &arg;
403 arg.type = ACPI_TYPE_INTEGER;
404 arg.integer.value = 1;
407 * TBD: _EJD support.
410 status = acpi_evaluate_object(handle, "_EJ0", &arg_list, NULL);
411 if (ACPI_FAILURE(status)) {
412 return (-ENODEV);
415 return (0);
418 static ssize_t
419 acpi_eject_store(struct acpi_device *device, const char *buf, size_t count)
421 int result;
422 int ret = count;
423 int islockable;
424 acpi_status status;
425 acpi_handle handle;
426 acpi_object_type type = 0;
428 if ((!count) || (buf[0] != '1')) {
429 return -EINVAL;
431 #ifndef FORCE_EJECT
432 if (device->driver == NULL) {
433 ret = -ENODEV;
434 goto err;
436 #endif
437 status = acpi_get_type(device->handle, &type);
438 if (ACPI_FAILURE(status) || (!device->flags.ejectable)) {
439 ret = -ENODEV;
440 goto err;
443 islockable = device->flags.lockable;
444 handle = device->handle;
446 if (type == ACPI_TYPE_PROCESSOR)
447 result = acpi_bus_trim(device, 0);
448 else
449 result = acpi_bus_trim(device, 1);
451 if (!result)
452 result = acpi_eject_operation(handle, islockable);
454 if (result) {
455 ret = -EBUSY;
457 err:
458 return ret;
461 /* --------------------------------------------------------------------------
462 Performance Management
463 -------------------------------------------------------------------------- */
465 static int acpi_bus_get_perf_flags(struct acpi_device *device)
467 device->performance.state = ACPI_STATE_UNKNOWN;
468 return 0;
471 /* --------------------------------------------------------------------------
472 Driver Management
473 -------------------------------------------------------------------------- */
475 static LIST_HEAD(acpi_bus_drivers);
476 static DECLARE_MUTEX(acpi_bus_drivers_lock);
479 * acpi_bus_match
480 * --------------
481 * Checks the device's hardware (_HID) or compatible (_CID) ids to see if it
482 * matches the specified driver's criteria.
484 static int
485 acpi_bus_match(struct acpi_device *device, struct acpi_driver *driver)
487 if (driver && driver->ops.match)
488 return driver->ops.match(device, driver);
489 return acpi_match_ids(device, driver->ids);
493 * acpi_bus_driver_init
494 * --------------------
495 * Used to initialize a device via its device driver. Called whenever a
496 * driver is bound to a device. Invokes the driver's add() and start() ops.
498 static int
499 acpi_bus_driver_init(struct acpi_device *device, struct acpi_driver *driver)
501 int result = 0;
503 ACPI_FUNCTION_TRACE("acpi_bus_driver_init");
505 if (!device || !driver)
506 return_VALUE(-EINVAL);
508 if (!driver->ops.add)
509 return_VALUE(-ENOSYS);
511 result = driver->ops.add(device);
512 if (result) {
513 device->driver = NULL;
514 acpi_driver_data(device) = NULL;
515 return_VALUE(result);
518 device->driver = driver;
521 * TBD - Configuration Management: Assign resources to device based
522 * upon possible configuration and currently allocated resources.
525 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
526 "Driver successfully bound to device\n"));
527 return_VALUE(0);
530 static int acpi_start_single_object(struct acpi_device *device)
532 int result = 0;
533 struct acpi_driver *driver;
535 ACPI_FUNCTION_TRACE("acpi_start_single_object");
537 if (!(driver = device->driver))
538 return_VALUE(0);
540 if (driver->ops.start) {
541 result = driver->ops.start(device);
542 if (result && driver->ops.remove)
543 driver->ops.remove(device, ACPI_BUS_REMOVAL_NORMAL);
546 return_VALUE(result);
549 static int acpi_driver_attach(struct acpi_driver *drv)
551 struct list_head *node, *next;
552 int count = 0;
554 ACPI_FUNCTION_TRACE("acpi_driver_attach");
556 spin_lock(&acpi_device_lock);
557 list_for_each_safe(node, next, &acpi_device_list) {
558 struct acpi_device *dev =
559 container_of(node, struct acpi_device, g_list);
561 if (dev->driver || !dev->status.present)
562 continue;
563 spin_unlock(&acpi_device_lock);
565 if (!acpi_bus_match(dev, drv)) {
566 if (!acpi_bus_driver_init(dev, drv)) {
567 acpi_start_single_object(dev);
568 atomic_inc(&drv->references);
569 count++;
570 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
571 "Found driver [%s] for device [%s]\n",
572 drv->name, dev->pnp.bus_id));
575 spin_lock(&acpi_device_lock);
577 spin_unlock(&acpi_device_lock);
578 return_VALUE(count);
581 static int acpi_driver_detach(struct acpi_driver *drv)
583 struct list_head *node, *next;
585 ACPI_FUNCTION_TRACE("acpi_driver_detach");
587 spin_lock(&acpi_device_lock);
588 list_for_each_safe(node, next, &acpi_device_list) {
589 struct acpi_device *dev =
590 container_of(node, struct acpi_device, g_list);
592 if (dev->driver == drv) {
593 spin_unlock(&acpi_device_lock);
594 if (drv->ops.remove)
595 drv->ops.remove(dev, ACPI_BUS_REMOVAL_NORMAL);
596 spin_lock(&acpi_device_lock);
597 dev->driver = NULL;
598 dev->driver_data = NULL;
599 atomic_dec(&drv->references);
602 spin_unlock(&acpi_device_lock);
603 return_VALUE(0);
607 * acpi_bus_register_driver
608 * ------------------------
609 * Registers a driver with the ACPI bus. Searches the namespace for all
610 * devices that match the driver's criteria and binds. Returns the
611 * number of devices that were claimed by the driver, or a negative
612 * error status for failure.
614 int acpi_bus_register_driver(struct acpi_driver *driver)
616 int count;
618 ACPI_FUNCTION_TRACE("acpi_bus_register_driver");
620 if (acpi_disabled)
621 return_VALUE(-ENODEV);
623 if (!driver)
624 return_VALUE(-EINVAL);
626 spin_lock(&acpi_device_lock);
627 list_add_tail(&driver->node, &acpi_bus_drivers);
628 spin_unlock(&acpi_device_lock);
629 count = acpi_driver_attach(driver);
631 return_VALUE(count);
634 EXPORT_SYMBOL(acpi_bus_register_driver);
637 * acpi_bus_unregister_driver
638 * --------------------------
639 * Unregisters a driver with the ACPI bus. Searches the namespace for all
640 * devices that match the driver's criteria and unbinds.
642 int acpi_bus_unregister_driver(struct acpi_driver *driver)
644 int error = 0;
646 ACPI_FUNCTION_TRACE("acpi_bus_unregister_driver");
648 if (driver) {
649 acpi_driver_detach(driver);
651 if (!atomic_read(&driver->references)) {
652 spin_lock(&acpi_device_lock);
653 list_del_init(&driver->node);
654 spin_unlock(&acpi_device_lock);
656 } else
657 error = -EINVAL;
658 return_VALUE(error);
661 EXPORT_SYMBOL(acpi_bus_unregister_driver);
664 * acpi_bus_find_driver
665 * --------------------
666 * Parses the list of registered drivers looking for a driver applicable for
667 * the specified device.
669 static int acpi_bus_find_driver(struct acpi_device *device)
671 int result = 0;
672 struct list_head *node, *next;
674 ACPI_FUNCTION_TRACE("acpi_bus_find_driver");
676 spin_lock(&acpi_device_lock);
677 list_for_each_safe(node, next, &acpi_bus_drivers) {
678 struct acpi_driver *driver =
679 container_of(node, struct acpi_driver, node);
681 atomic_inc(&driver->references);
682 spin_unlock(&acpi_device_lock);
683 if (!acpi_bus_match(device, driver)) {
684 result = acpi_bus_driver_init(device, driver);
685 if (!result)
686 goto Done;
688 atomic_dec(&driver->references);
689 spin_lock(&acpi_device_lock);
691 spin_unlock(&acpi_device_lock);
693 Done:
694 return_VALUE(result);
697 /* --------------------------------------------------------------------------
698 Device Enumeration
699 -------------------------------------------------------------------------- */
701 static int acpi_bus_get_flags(struct acpi_device *device)
703 acpi_status status = AE_OK;
704 acpi_handle temp = NULL;
706 ACPI_FUNCTION_TRACE("acpi_bus_get_flags");
708 /* Presence of _STA indicates 'dynamic_status' */
709 status = acpi_get_handle(device->handle, "_STA", &temp);
710 if (ACPI_SUCCESS(status))
711 device->flags.dynamic_status = 1;
713 /* Presence of _CID indicates 'compatible_ids' */
714 status = acpi_get_handle(device->handle, "_CID", &temp);
715 if (ACPI_SUCCESS(status))
716 device->flags.compatible_ids = 1;
718 /* Presence of _RMV indicates 'removable' */
719 status = acpi_get_handle(device->handle, "_RMV", &temp);
720 if (ACPI_SUCCESS(status))
721 device->flags.removable = 1;
723 /* Presence of _EJD|_EJ0 indicates 'ejectable' */
724 status = acpi_get_handle(device->handle, "_EJD", &temp);
725 if (ACPI_SUCCESS(status))
726 device->flags.ejectable = 1;
727 else {
728 status = acpi_get_handle(device->handle, "_EJ0", &temp);
729 if (ACPI_SUCCESS(status))
730 device->flags.ejectable = 1;
733 /* Presence of _LCK indicates 'lockable' */
734 status = acpi_get_handle(device->handle, "_LCK", &temp);
735 if (ACPI_SUCCESS(status))
736 device->flags.lockable = 1;
738 /* Presence of _PS0|_PR0 indicates 'power manageable' */
739 status = acpi_get_handle(device->handle, "_PS0", &temp);
740 if (ACPI_FAILURE(status))
741 status = acpi_get_handle(device->handle, "_PR0", &temp);
742 if (ACPI_SUCCESS(status))
743 device->flags.power_manageable = 1;
745 /* Presence of _PRW indicates wake capable */
746 status = acpi_get_handle(device->handle, "_PRW", &temp);
747 if (ACPI_SUCCESS(status))
748 device->flags.wake_capable = 1;
750 /* TBD: Peformance management */
752 return_VALUE(0);
755 static void acpi_device_get_busid(struct acpi_device *device,
756 acpi_handle handle, int type)
758 char bus_id[5] = { '?', 0 };
759 struct acpi_buffer buffer = { sizeof(bus_id), bus_id };
760 int i = 0;
763 * Bus ID
764 * ------
765 * The device's Bus ID is simply the object name.
766 * TBD: Shouldn't this value be unique (within the ACPI namespace)?
768 switch (type) {
769 case ACPI_BUS_TYPE_SYSTEM:
770 strcpy(device->pnp.bus_id, "ACPI");
771 break;
772 case ACPI_BUS_TYPE_POWER_BUTTON:
773 strcpy(device->pnp.bus_id, "PWRF");
774 break;
775 case ACPI_BUS_TYPE_SLEEP_BUTTON:
776 strcpy(device->pnp.bus_id, "SLPF");
777 break;
778 default:
779 acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer);
780 /* Clean up trailing underscores (if any) */
781 for (i = 3; i > 1; i--) {
782 if (bus_id[i] == '_')
783 bus_id[i] = '\0';
784 else
785 break;
787 strcpy(device->pnp.bus_id, bus_id);
788 break;
792 static void acpi_device_set_id(struct acpi_device *device,
793 struct acpi_device *parent, acpi_handle handle,
794 int type)
796 struct acpi_device_info *info;
797 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
798 char *hid = NULL;
799 char *uid = NULL;
800 struct acpi_compatible_id_list *cid_list = NULL;
801 acpi_status status;
803 switch (type) {
804 case ACPI_BUS_TYPE_DEVICE:
805 status = acpi_get_object_info(handle, &buffer);
806 if (ACPI_FAILURE(status)) {
807 printk("%s: Error reading device info\n", __FUNCTION__);
808 return;
811 info = buffer.pointer;
812 if (info->valid & ACPI_VALID_HID)
813 hid = info->hardware_id.value;
814 if (info->valid & ACPI_VALID_UID)
815 uid = info->unique_id.value;
816 if (info->valid & ACPI_VALID_CID)
817 cid_list = &info->compatibility_id;
818 if (info->valid & ACPI_VALID_ADR) {
819 device->pnp.bus_address = info->address;
820 device->flags.bus_address = 1;
822 break;
823 case ACPI_BUS_TYPE_POWER:
824 hid = ACPI_POWER_HID;
825 break;
826 case ACPI_BUS_TYPE_PROCESSOR:
827 hid = ACPI_PROCESSOR_HID;
828 break;
829 case ACPI_BUS_TYPE_SYSTEM:
830 hid = ACPI_SYSTEM_HID;
831 break;
832 case ACPI_BUS_TYPE_THERMAL:
833 hid = ACPI_THERMAL_HID;
834 break;
835 case ACPI_BUS_TYPE_POWER_BUTTON:
836 hid = ACPI_BUTTON_HID_POWERF;
837 break;
838 case ACPI_BUS_TYPE_SLEEP_BUTTON:
839 hid = ACPI_BUTTON_HID_SLEEPF;
840 break;
844 * \_SB
845 * ----
846 * Fix for the system root bus device -- the only root-level device.
848 if ((parent == ACPI_ROOT_OBJECT) && (type == ACPI_BUS_TYPE_DEVICE)) {
849 hid = ACPI_BUS_HID;
850 strcpy(device->pnp.device_name, ACPI_BUS_DEVICE_NAME);
851 strcpy(device->pnp.device_class, ACPI_BUS_CLASS);
854 if (hid) {
855 strcpy(device->pnp.hardware_id, hid);
856 device->flags.hardware_id = 1;
858 if (uid) {
859 strcpy(device->pnp.unique_id, uid);
860 device->flags.unique_id = 1;
862 if (cid_list) {
863 device->pnp.cid_list = kmalloc(cid_list->size, GFP_KERNEL);
864 if (device->pnp.cid_list)
865 memcpy(device->pnp.cid_list, cid_list, cid_list->size);
866 else
867 printk(KERN_ERR "Memory allocation error\n");
870 acpi_os_free(buffer.pointer);
873 static int acpi_device_set_context(struct acpi_device *device, int type)
875 acpi_status status = AE_OK;
876 int result = 0;
878 * Context
879 * -------
880 * Attach this 'struct acpi_device' to the ACPI object. This makes
881 * resolutions from handle->device very efficient. Note that we need
882 * to be careful with fixed-feature devices as they all attach to the
883 * root object.
885 if (type != ACPI_BUS_TYPE_POWER_BUTTON &&
886 type != ACPI_BUS_TYPE_SLEEP_BUTTON) {
887 status = acpi_attach_data(device->handle,
888 acpi_bus_data_handler, device);
890 if (ACPI_FAILURE(status)) {
891 printk("Error attaching device data\n");
892 result = -ENODEV;
895 return result;
898 static void acpi_device_get_debug_info(struct acpi_device *device,
899 acpi_handle handle, int type)
901 #ifdef CONFIG_ACPI_DEBUG_OUTPUT
902 char *type_string = NULL;
903 char name[80] = { '?', '\0' };
904 struct acpi_buffer buffer = { sizeof(name), name };
906 switch (type) {
907 case ACPI_BUS_TYPE_DEVICE:
908 type_string = "Device";
909 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
910 break;
911 case ACPI_BUS_TYPE_POWER:
912 type_string = "Power Resource";
913 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
914 break;
915 case ACPI_BUS_TYPE_PROCESSOR:
916 type_string = "Processor";
917 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
918 break;
919 case ACPI_BUS_TYPE_SYSTEM:
920 type_string = "System";
921 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
922 break;
923 case ACPI_BUS_TYPE_THERMAL:
924 type_string = "Thermal Zone";
925 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
926 break;
927 case ACPI_BUS_TYPE_POWER_BUTTON:
928 type_string = "Power Button";
929 sprintf(name, "PWRB");
930 break;
931 case ACPI_BUS_TYPE_SLEEP_BUTTON:
932 type_string = "Sleep Button";
933 sprintf(name, "SLPB");
934 break;
937 printk(KERN_DEBUG "Found %s %s [%p]\n", type_string, name, handle);
938 #endif /*CONFIG_ACPI_DEBUG_OUTPUT */
941 static int acpi_bus_remove(struct acpi_device *dev, int rmdevice)
943 int result = 0;
944 struct acpi_driver *driver;
946 ACPI_FUNCTION_TRACE("acpi_bus_remove");
948 if (!dev)
949 return_VALUE(-EINVAL);
951 driver = dev->driver;
953 if ((driver) && (driver->ops.remove)) {
955 if (driver->ops.stop) {
956 result = driver->ops.stop(dev, ACPI_BUS_REMOVAL_EJECT);
957 if (result)
958 return_VALUE(result);
961 result = dev->driver->ops.remove(dev, ACPI_BUS_REMOVAL_EJECT);
962 if (result) {
963 return_VALUE(result);
966 atomic_dec(&dev->driver->references);
967 dev->driver = NULL;
968 acpi_driver_data(dev) = NULL;
971 if (!rmdevice)
972 return_VALUE(0);
974 if (dev->flags.bus_address) {
975 if ((dev->parent) && (dev->parent->ops.unbind))
976 dev->parent->ops.unbind(dev);
979 acpi_device_unregister(dev, ACPI_BUS_REMOVAL_EJECT);
981 return_VALUE(0);
984 static int
985 acpi_add_single_object(struct acpi_device **child,
986 struct acpi_device *parent, acpi_handle handle, int type)
988 int result = 0;
989 struct acpi_device *device = NULL;
991 ACPI_FUNCTION_TRACE("acpi_add_single_object");
993 if (!child)
994 return_VALUE(-EINVAL);
996 device = kmalloc(sizeof(struct acpi_device), GFP_KERNEL);
997 if (!device) {
998 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Memory allocation error\n"));
999 return_VALUE(-ENOMEM);
1001 memset(device, 0, sizeof(struct acpi_device));
1003 device->handle = handle;
1004 device->parent = parent;
1006 acpi_device_get_busid(device, handle, type);
1009 * Flags
1010 * -----
1011 * Get prior to calling acpi_bus_get_status() so we know whether
1012 * or not _STA is present. Note that we only look for object
1013 * handles -- cannot evaluate objects until we know the device is
1014 * present and properly initialized.
1016 result = acpi_bus_get_flags(device);
1017 if (result)
1018 goto end;
1021 * Status
1022 * ------
1023 * See if the device is present. We always assume that non-Device
1024 * and non-Processor objects (e.g. thermal zones, power resources,
1025 * etc.) are present, functioning, etc. (at least when parent object
1026 * is present). Note that _STA has a different meaning for some
1027 * objects (e.g. power resources) so we need to be careful how we use
1028 * it.
1030 switch (type) {
1031 case ACPI_BUS_TYPE_PROCESSOR:
1032 case ACPI_BUS_TYPE_DEVICE:
1033 result = acpi_bus_get_status(device);
1034 if (ACPI_FAILURE(result) || !device->status.present) {
1035 result = -ENOENT;
1036 goto end;
1038 break;
1039 default:
1040 STRUCT_TO_INT(device->status) = 0x0F;
1041 break;
1045 * Initialize Device
1046 * -----------------
1047 * TBD: Synch with Core's enumeration/initialization process.
1051 * Hardware ID, Unique ID, & Bus Address
1052 * -------------------------------------
1054 acpi_device_set_id(device, parent, handle, type);
1057 * Power Management
1058 * ----------------
1060 if (device->flags.power_manageable) {
1061 result = acpi_bus_get_power_flags(device);
1062 if (result)
1063 goto end;
1067 * Wakeup device management
1068 *-----------------------
1070 if (device->flags.wake_capable) {
1071 result = acpi_bus_get_wakeup_device_flags(device);
1072 if (result)
1073 goto end;
1077 * Performance Management
1078 * ----------------------
1080 if (device->flags.performance_manageable) {
1081 result = acpi_bus_get_perf_flags(device);
1082 if (result)
1083 goto end;
1086 if ((result = acpi_device_set_context(device, type)))
1087 goto end;
1089 acpi_device_get_debug_info(device, handle, type);
1091 acpi_device_register(device, parent);
1094 * Bind _ADR-Based Devices
1095 * -----------------------
1096 * If there's a a bus address (_ADR) then we utilize the parent's
1097 * 'bind' function (if exists) to bind the ACPI- and natively-
1098 * enumerated device representations.
1100 if (device->flags.bus_address) {
1101 if (device->parent && device->parent->ops.bind)
1102 device->parent->ops.bind(device);
1106 * Locate & Attach Driver
1107 * ----------------------
1108 * If there's a hardware id (_HID) or compatible ids (_CID) we check
1109 * to see if there's a driver installed for this kind of device. Note
1110 * that drivers can install before or after a device is enumerated.
1112 * TBD: Assumes LDM provides driver hot-plug capability.
1114 result = acpi_bus_find_driver(device);
1116 end:
1117 if (!result)
1118 *child = device;
1119 else {
1120 if (device->pnp.cid_list)
1121 kfree(device->pnp.cid_list);
1122 kfree(device);
1125 return_VALUE(result);
1128 static int acpi_bus_scan(struct acpi_device *start, struct acpi_bus_ops *ops)
1130 acpi_status status = AE_OK;
1131 struct acpi_device *parent = NULL;
1132 struct acpi_device *child = NULL;
1133 acpi_handle phandle = NULL;
1134 acpi_handle chandle = NULL;
1135 acpi_object_type type = 0;
1136 u32 level = 1;
1138 ACPI_FUNCTION_TRACE("acpi_bus_scan");
1140 if (!start)
1141 return_VALUE(-EINVAL);
1143 parent = start;
1144 phandle = start->handle;
1147 * Parse through the ACPI namespace, identify all 'devices', and
1148 * create a new 'struct acpi_device' for each.
1150 while ((level > 0) && parent) {
1152 status = acpi_get_next_object(ACPI_TYPE_ANY, phandle,
1153 chandle, &chandle);
1156 * If this scope is exhausted then move our way back up.
1158 if (ACPI_FAILURE(status)) {
1159 level--;
1160 chandle = phandle;
1161 acpi_get_parent(phandle, &phandle);
1162 if (parent->parent)
1163 parent = parent->parent;
1164 continue;
1167 status = acpi_get_type(chandle, &type);
1168 if (ACPI_FAILURE(status))
1169 continue;
1172 * If this is a scope object then parse it (depth-first).
1174 if (type == ACPI_TYPE_LOCAL_SCOPE) {
1175 level++;
1176 phandle = chandle;
1177 chandle = NULL;
1178 continue;
1182 * We're only interested in objects that we consider 'devices'.
1184 switch (type) {
1185 case ACPI_TYPE_DEVICE:
1186 type = ACPI_BUS_TYPE_DEVICE;
1187 break;
1188 case ACPI_TYPE_PROCESSOR:
1189 type = ACPI_BUS_TYPE_PROCESSOR;
1190 break;
1191 case ACPI_TYPE_THERMAL:
1192 type = ACPI_BUS_TYPE_THERMAL;
1193 break;
1194 case ACPI_TYPE_POWER:
1195 type = ACPI_BUS_TYPE_POWER;
1196 break;
1197 default:
1198 continue;
1201 if (ops->acpi_op_add)
1202 status = acpi_add_single_object(&child, parent,
1203 chandle, type);
1204 else
1205 status = acpi_bus_get_device(chandle, &child);
1207 if (ACPI_FAILURE(status))
1208 continue;
1210 if (ops->acpi_op_start) {
1211 status = acpi_start_single_object(child);
1212 if (ACPI_FAILURE(status))
1213 continue;
1217 * If the device is present, enabled, and functioning then
1218 * parse its scope (depth-first). Note that we need to
1219 * represent absent devices to facilitate PnP notifications
1220 * -- but only the subtree head (not all of its children,
1221 * which will be enumerated when the parent is inserted).
1223 * TBD: Need notifications and other detection mechanisms
1224 * in place before we can fully implement this.
1226 if (child->status.present) {
1227 status = acpi_get_next_object(ACPI_TYPE_ANY, chandle,
1228 NULL, NULL);
1229 if (ACPI_SUCCESS(status)) {
1230 level++;
1231 phandle = chandle;
1232 chandle = NULL;
1233 parent = child;
1238 return_VALUE(0);
1242 acpi_bus_add(struct acpi_device **child,
1243 struct acpi_device *parent, acpi_handle handle, int type)
1245 int result;
1246 struct acpi_bus_ops ops;
1248 ACPI_FUNCTION_TRACE("acpi_bus_add");
1250 result = acpi_add_single_object(child, parent, handle, type);
1251 if (!result) {
1252 memset(&ops, 0, sizeof(ops));
1253 ops.acpi_op_add = 1;
1254 result = acpi_bus_scan(*child, &ops);
1256 return_VALUE(result);
1259 EXPORT_SYMBOL(acpi_bus_add);
1261 int acpi_bus_start(struct acpi_device *device)
1263 int result;
1264 struct acpi_bus_ops ops;
1266 ACPI_FUNCTION_TRACE("acpi_bus_start");
1268 if (!device)
1269 return_VALUE(-EINVAL);
1271 result = acpi_start_single_object(device);
1272 if (!result) {
1273 memset(&ops, 0, sizeof(ops));
1274 ops.acpi_op_start = 1;
1275 result = acpi_bus_scan(device, &ops);
1277 return_VALUE(result);
1280 EXPORT_SYMBOL(acpi_bus_start);
1282 static int acpi_bus_trim(struct acpi_device *start, int rmdevice)
1284 acpi_status status;
1285 struct acpi_device *parent, *child;
1286 acpi_handle phandle, chandle;
1287 acpi_object_type type;
1288 u32 level = 1;
1289 int err = 0;
1291 parent = start;
1292 phandle = start->handle;
1293 child = chandle = NULL;
1295 while ((level > 0) && parent && (!err)) {
1296 status = acpi_get_next_object(ACPI_TYPE_ANY, phandle,
1297 chandle, &chandle);
1300 * If this scope is exhausted then move our way back up.
1302 if (ACPI_FAILURE(status)) {
1303 level--;
1304 chandle = phandle;
1305 acpi_get_parent(phandle, &phandle);
1306 child = parent;
1307 parent = parent->parent;
1309 if (level == 0)
1310 err = acpi_bus_remove(child, rmdevice);
1311 else
1312 err = acpi_bus_remove(child, 1);
1314 continue;
1317 status = acpi_get_type(chandle, &type);
1318 if (ACPI_FAILURE(status)) {
1319 continue;
1322 * If there is a device corresponding to chandle then
1323 * parse it (depth-first).
1325 if (acpi_bus_get_device(chandle, &child) == 0) {
1326 level++;
1327 phandle = chandle;
1328 chandle = NULL;
1329 parent = child;
1331 continue;
1333 return err;
1336 static int acpi_bus_scan_fixed(struct acpi_device *root)
1338 int result = 0;
1339 struct acpi_device *device = NULL;
1341 ACPI_FUNCTION_TRACE("acpi_bus_scan_fixed");
1343 if (!root)
1344 return_VALUE(-ENODEV);
1347 * Enumerate all fixed-feature devices.
1349 if (acpi_fadt.pwr_button == 0) {
1350 result = acpi_add_single_object(&device, acpi_root,
1351 NULL,
1352 ACPI_BUS_TYPE_POWER_BUTTON);
1353 if (!result)
1354 result = acpi_start_single_object(device);
1357 if (acpi_fadt.sleep_button == 0) {
1358 result = acpi_add_single_object(&device, acpi_root,
1359 NULL,
1360 ACPI_BUS_TYPE_SLEEP_BUTTON);
1361 if (!result)
1362 result = acpi_start_single_object(device);
1365 return_VALUE(result);
1368 static int __init acpi_scan_init(void)
1370 int result;
1371 struct acpi_bus_ops ops;
1373 ACPI_FUNCTION_TRACE("acpi_scan_init");
1375 if (acpi_disabled)
1376 return_VALUE(0);
1378 kset_register(&acpi_namespace_kset);
1381 * Create the root device in the bus's device tree
1383 result = acpi_add_single_object(&acpi_root, NULL, ACPI_ROOT_OBJECT,
1384 ACPI_BUS_TYPE_SYSTEM);
1385 if (result)
1386 goto Done;
1388 result = acpi_start_single_object(acpi_root);
1391 * Enumerate devices in the ACPI namespace.
1393 result = acpi_bus_scan_fixed(acpi_root);
1394 if (!result) {
1395 memset(&ops, 0, sizeof(ops));
1396 ops.acpi_op_add = 1;
1397 ops.acpi_op_start = 1;
1398 result = acpi_bus_scan(acpi_root, &ops);
1401 if (result)
1402 acpi_device_unregister(acpi_root, ACPI_BUS_REMOVAL_NORMAL);
1404 Done:
1405 return_VALUE(result);
1408 subsys_initcall(acpi_scan_init);