ACPI: make acpi_bus_register_driver() return success/failure, not device count
[usb.git] / drivers / acpi / scan.c
blob669553553fbbf851260d5cf06119dade20557fdb
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);
27 static void acpi_device_release(struct kobject *kobj)
29 struct acpi_device *dev = container_of(kobj, struct acpi_device, kobj);
30 kfree(dev->pnp.cid_list);
31 kfree(dev);
34 struct acpi_device_attribute {
35 struct attribute attr;
36 ssize_t(*show) (struct acpi_device *, char *);
37 ssize_t(*store) (struct acpi_device *, const char *, size_t);
40 typedef void acpi_device_sysfs_files(struct kobject *,
41 const struct attribute *);
43 static void setup_sys_fs_device_files(struct acpi_device *dev,
44 acpi_device_sysfs_files * func);
46 #define create_sysfs_device_files(dev) \
47 setup_sys_fs_device_files(dev, (acpi_device_sysfs_files *)&sysfs_create_file)
48 #define remove_sysfs_device_files(dev) \
49 setup_sys_fs_device_files(dev, (acpi_device_sysfs_files *)&sysfs_remove_file)
51 #define to_acpi_device(n) container_of(n, struct acpi_device, kobj)
52 #define to_handle_attr(n) container_of(n, struct acpi_device_attribute, attr);
54 static ssize_t acpi_device_attr_show(struct kobject *kobj,
55 struct attribute *attr, char *buf)
57 struct acpi_device *device = to_acpi_device(kobj);
58 struct acpi_device_attribute *attribute = to_handle_attr(attr);
59 return attribute->show ? attribute->show(device, buf) : -EIO;
61 static ssize_t acpi_device_attr_store(struct kobject *kobj,
62 struct attribute *attr, const char *buf,
63 size_t len)
65 struct acpi_device *device = to_acpi_device(kobj);
66 struct acpi_device_attribute *attribute = to_handle_attr(attr);
67 return attribute->store ? attribute->store(device, buf, len) : -EIO;
70 static struct sysfs_ops acpi_device_sysfs_ops = {
71 .show = acpi_device_attr_show,
72 .store = acpi_device_attr_store,
75 static struct kobj_type ktype_acpi_ns = {
76 .sysfs_ops = &acpi_device_sysfs_ops,
77 .release = acpi_device_release,
80 static int namespace_uevent(struct kset *kset, struct kobject *kobj,
81 char **envp, int num_envp, char *buffer,
82 int buffer_size)
84 struct acpi_device *dev = to_acpi_device(kobj);
85 int i = 0;
86 int len = 0;
88 if (!dev->driver)
89 return 0;
91 if (add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &len,
92 "PHYSDEVDRIVER=%s", dev->driver->name))
93 return -ENOMEM;
95 envp[i] = NULL;
97 return 0;
100 static struct kset_uevent_ops namespace_uevent_ops = {
101 .uevent = &namespace_uevent,
104 static struct kset acpi_namespace_kset = {
105 .kobj = {
106 .name = "namespace",
108 .subsys = &acpi_subsys,
109 .ktype = &ktype_acpi_ns,
110 .uevent_ops = &namespace_uevent_ops,
113 static void acpi_device_register(struct acpi_device *device,
114 struct acpi_device *parent)
117 * Linkage
118 * -------
119 * Link this device to its parent and siblings.
121 INIT_LIST_HEAD(&device->children);
122 INIT_LIST_HEAD(&device->node);
123 INIT_LIST_HEAD(&device->g_list);
124 INIT_LIST_HEAD(&device->wakeup_list);
126 spin_lock(&acpi_device_lock);
127 if (device->parent) {
128 list_add_tail(&device->node, &device->parent->children);
129 list_add_tail(&device->g_list, &device->parent->g_list);
130 } else
131 list_add_tail(&device->g_list, &acpi_device_list);
132 if (device->wakeup.flags.valid)
133 list_add_tail(&device->wakeup_list, &acpi_wakeup_device_list);
134 spin_unlock(&acpi_device_lock);
136 strlcpy(device->kobj.name, device->pnp.bus_id, KOBJ_NAME_LEN);
137 if (parent)
138 device->kobj.parent = &parent->kobj;
139 device->kobj.ktype = &ktype_acpi_ns;
140 device->kobj.kset = &acpi_namespace_kset;
141 kobject_register(&device->kobj);
142 create_sysfs_device_files(device);
145 static int acpi_device_unregister(struct acpi_device *device, int type)
147 spin_lock(&acpi_device_lock);
148 if (device->parent) {
149 list_del(&device->node);
150 list_del(&device->g_list);
151 } else
152 list_del(&device->g_list);
154 list_del(&device->wakeup_list);
156 spin_unlock(&acpi_device_lock);
158 acpi_detach_data(device->handle, acpi_bus_data_handler);
159 remove_sysfs_device_files(device);
160 kobject_unregister(&device->kobj);
161 return 0;
164 void acpi_bus_data_handler(acpi_handle handle, u32 function, void *context)
166 ACPI_FUNCTION_TRACE("acpi_bus_data_handler");
168 /* TBD */
170 return_VOID;
173 static int acpi_bus_get_power_flags(struct acpi_device *device)
175 acpi_status status = 0;
176 acpi_handle handle = NULL;
177 u32 i = 0;
179 ACPI_FUNCTION_TRACE("acpi_bus_get_power_flags");
182 * Power Management Flags
184 status = acpi_get_handle(device->handle, "_PSC", &handle);
185 if (ACPI_SUCCESS(status))
186 device->power.flags.explicit_get = 1;
187 status = acpi_get_handle(device->handle, "_IRC", &handle);
188 if (ACPI_SUCCESS(status))
189 device->power.flags.inrush_current = 1;
192 * Enumerate supported power management states
194 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3; i++) {
195 struct acpi_device_power_state *ps = &device->power.states[i];
196 char object_name[5] = { '_', 'P', 'R', '0' + i, '\0' };
198 /* Evaluate "_PRx" to se if power resources are referenced */
199 acpi_evaluate_reference(device->handle, object_name, NULL,
200 &ps->resources);
201 if (ps->resources.count) {
202 device->power.flags.power_resources = 1;
203 ps->flags.valid = 1;
206 /* Evaluate "_PSx" to see if we can do explicit sets */
207 object_name[2] = 'S';
208 status = acpi_get_handle(device->handle, object_name, &handle);
209 if (ACPI_SUCCESS(status)) {
210 ps->flags.explicit_set = 1;
211 ps->flags.valid = 1;
214 /* State is valid if we have some power control */
215 if (ps->resources.count || ps->flags.explicit_set)
216 ps->flags.valid = 1;
218 ps->power = -1; /* Unknown - driver assigned */
219 ps->latency = -1; /* Unknown - driver assigned */
222 /* Set defaults for D0 and D3 states (always valid) */
223 device->power.states[ACPI_STATE_D0].flags.valid = 1;
224 device->power.states[ACPI_STATE_D0].power = 100;
225 device->power.states[ACPI_STATE_D3].flags.valid = 1;
226 device->power.states[ACPI_STATE_D3].power = 0;
228 /* TBD: System wake support and resource requirements. */
230 device->power.state = ACPI_STATE_UNKNOWN;
232 return_VALUE(0);
235 int acpi_match_ids(struct acpi_device *device, char *ids)
237 int error = 0;
238 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
240 if (device->flags.hardware_id)
241 if (strstr(ids, device->pnp.hardware_id))
242 goto Done;
244 if (device->flags.compatible_ids) {
245 struct acpi_compatible_id_list *cid_list = device->pnp.cid_list;
246 int i;
248 /* compare multiple _CID entries against driver ids */
249 for (i = 0; i < cid_list->count; i++) {
250 if (strstr(ids, cid_list->id[i].value))
251 goto Done;
254 error = -ENOENT;
256 Done:
257 if (buffer.pointer)
258 acpi_os_free(buffer.pointer);
259 return error;
262 static acpi_status
263 acpi_bus_extract_wakeup_device_power_package(struct acpi_device *device,
264 union acpi_object *package)
266 int i = 0;
267 union acpi_object *element = NULL;
269 if (!device || !package || (package->package.count < 2))
270 return AE_BAD_PARAMETER;
272 element = &(package->package.elements[0]);
273 if (!element)
274 return AE_BAD_PARAMETER;
275 if (element->type == ACPI_TYPE_PACKAGE) {
276 if ((element->package.count < 2) ||
277 (element->package.elements[0].type !=
278 ACPI_TYPE_LOCAL_REFERENCE)
279 || (element->package.elements[1].type != ACPI_TYPE_INTEGER))
280 return AE_BAD_DATA;
281 device->wakeup.gpe_device =
282 element->package.elements[0].reference.handle;
283 device->wakeup.gpe_number =
284 (u32) element->package.elements[1].integer.value;
285 } else if (element->type == ACPI_TYPE_INTEGER) {
286 device->wakeup.gpe_number = element->integer.value;
287 } else
288 return AE_BAD_DATA;
290 element = &(package->package.elements[1]);
291 if (element->type != ACPI_TYPE_INTEGER) {
292 return AE_BAD_DATA;
294 device->wakeup.sleep_state = element->integer.value;
296 if ((package->package.count - 2) > ACPI_MAX_HANDLES) {
297 return AE_NO_MEMORY;
299 device->wakeup.resources.count = package->package.count - 2;
300 for (i = 0; i < device->wakeup.resources.count; i++) {
301 element = &(package->package.elements[i + 2]);
302 if (element->type != ACPI_TYPE_ANY) {
303 return AE_BAD_DATA;
306 device->wakeup.resources.handles[i] = element->reference.handle;
309 return AE_OK;
312 static int acpi_bus_get_wakeup_device_flags(struct acpi_device *device)
314 acpi_status status = 0;
315 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
316 union acpi_object *package = NULL;
318 ACPI_FUNCTION_TRACE("acpi_bus_get_wakeup_flags");
320 /* _PRW */
321 status = acpi_evaluate_object(device->handle, "_PRW", NULL, &buffer);
322 if (ACPI_FAILURE(status)) {
323 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Error evaluating _PRW\n"));
324 goto end;
327 package = (union acpi_object *)buffer.pointer;
328 status = acpi_bus_extract_wakeup_device_power_package(device, package);
329 if (ACPI_FAILURE(status)) {
330 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
331 "Error extracting _PRW package\n"));
332 goto end;
335 acpi_os_free(buffer.pointer);
337 device->wakeup.flags.valid = 1;
338 /* Power button, Lid switch always enable wakeup */
339 if (!acpi_match_ids(device, "PNP0C0D,PNP0C0C,PNP0C0E"))
340 device->wakeup.flags.run_wake = 1;
342 end:
343 if (ACPI_FAILURE(status))
344 device->flags.wake_capable = 0;
345 return_VALUE(0);
348 /* --------------------------------------------------------------------------
349 ACPI sysfs device file support
350 -------------------------------------------------------------------------- */
351 static ssize_t acpi_eject_store(struct acpi_device *device,
352 const char *buf, size_t count);
354 #define ACPI_DEVICE_ATTR(_name,_mode,_show,_store) \
355 static struct acpi_device_attribute acpi_device_attr_##_name = \
356 __ATTR(_name, _mode, _show, _store)
358 ACPI_DEVICE_ATTR(eject, 0200, NULL, acpi_eject_store);
361 * setup_sys_fs_device_files - sets up the device files under device namespace
362 * @dev: acpi_device object
363 * @func: function pointer to create or destroy the device file
365 static void
366 setup_sys_fs_device_files(struct acpi_device *dev,
367 acpi_device_sysfs_files * func)
369 acpi_status status;
370 acpi_handle temp = NULL;
373 * If device has _EJ0, 'eject' file is created that is used to trigger
374 * hot-removal function from userland.
376 status = acpi_get_handle(dev->handle, "_EJ0", &temp);
377 if (ACPI_SUCCESS(status))
378 (*(func)) (&dev->kobj, &acpi_device_attr_eject.attr);
381 static int acpi_eject_operation(acpi_handle handle, int lockable)
383 struct acpi_object_list arg_list;
384 union acpi_object arg;
385 acpi_status status = AE_OK;
388 * TBD: evaluate _PS3?
391 if (lockable) {
392 arg_list.count = 1;
393 arg_list.pointer = &arg;
394 arg.type = ACPI_TYPE_INTEGER;
395 arg.integer.value = 0;
396 acpi_evaluate_object(handle, "_LCK", &arg_list, NULL);
399 arg_list.count = 1;
400 arg_list.pointer = &arg;
401 arg.type = ACPI_TYPE_INTEGER;
402 arg.integer.value = 1;
405 * TBD: _EJD support.
408 status = acpi_evaluate_object(handle, "_EJ0", &arg_list, NULL);
409 if (ACPI_FAILURE(status)) {
410 return (-ENODEV);
413 return (0);
416 static ssize_t
417 acpi_eject_store(struct acpi_device *device, const char *buf, size_t count)
419 int result;
420 int ret = count;
421 int islockable;
422 acpi_status status;
423 acpi_handle handle;
424 acpi_object_type type = 0;
426 if ((!count) || (buf[0] != '1')) {
427 return -EINVAL;
429 #ifndef FORCE_EJECT
430 if (device->driver == NULL) {
431 ret = -ENODEV;
432 goto err;
434 #endif
435 status = acpi_get_type(device->handle, &type);
436 if (ACPI_FAILURE(status) || (!device->flags.ejectable)) {
437 ret = -ENODEV;
438 goto err;
441 islockable = device->flags.lockable;
442 handle = device->handle;
444 if (type == ACPI_TYPE_PROCESSOR)
445 result = acpi_bus_trim(device, 0);
446 else
447 result = acpi_bus_trim(device, 1);
449 if (!result)
450 result = acpi_eject_operation(handle, islockable);
452 if (result) {
453 ret = -EBUSY;
455 err:
456 return ret;
459 /* --------------------------------------------------------------------------
460 Performance Management
461 -------------------------------------------------------------------------- */
463 static int acpi_bus_get_perf_flags(struct acpi_device *device)
465 device->performance.state = ACPI_STATE_UNKNOWN;
466 return 0;
469 /* --------------------------------------------------------------------------
470 Driver Management
471 -------------------------------------------------------------------------- */
473 static LIST_HEAD(acpi_bus_drivers);
474 static DECLARE_MUTEX(acpi_bus_drivers_lock);
477 * acpi_bus_match - match device IDs to driver's supported IDs
478 * @device: the device that we are trying to match to a driver
479 * @driver: driver whose device id table is being checked
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 - add a device to a driver
494 * @device: the device to add and initialize
495 * @driver: driver for the device
497 * Used to initialize a device via its device driver. Called whenever a
498 * driver is bound to a device. Invokes the driver's add() and start() ops.
500 static int
501 acpi_bus_driver_init(struct acpi_device *device, struct acpi_driver *driver)
503 int result = 0;
505 ACPI_FUNCTION_TRACE("acpi_bus_driver_init");
507 if (!device || !driver)
508 return_VALUE(-EINVAL);
510 if (!driver->ops.add)
511 return_VALUE(-ENOSYS);
513 result = driver->ops.add(device);
514 if (result) {
515 device->driver = NULL;
516 acpi_driver_data(device) = NULL;
517 return_VALUE(result);
520 device->driver = driver;
523 * TBD - Configuration Management: Assign resources to device based
524 * upon possible configuration and currently allocated resources.
527 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
528 "Driver successfully bound to device\n"));
529 return_VALUE(0);
532 static int acpi_start_single_object(struct acpi_device *device)
534 int result = 0;
535 struct acpi_driver *driver;
537 ACPI_FUNCTION_TRACE("acpi_start_single_object");
539 if (!(driver = device->driver))
540 return_VALUE(0);
542 if (driver->ops.start) {
543 result = driver->ops.start(device);
544 if (result && driver->ops.remove)
545 driver->ops.remove(device, ACPI_BUS_REMOVAL_NORMAL);
548 return_VALUE(result);
551 static void acpi_driver_attach(struct acpi_driver *drv)
553 struct list_head *node, *next;
555 ACPI_FUNCTION_TRACE("acpi_driver_attach");
557 spin_lock(&acpi_device_lock);
558 list_for_each_safe(node, next, &acpi_device_list) {
559 struct acpi_device *dev =
560 container_of(node, struct acpi_device, g_list);
562 if (dev->driver || !dev->status.present)
563 continue;
564 spin_unlock(&acpi_device_lock);
566 if (!acpi_bus_match(dev, drv)) {
567 if (!acpi_bus_driver_init(dev, drv)) {
568 acpi_start_single_object(dev);
569 atomic_inc(&drv->references);
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);
580 static int acpi_driver_detach(struct acpi_driver *drv)
582 struct list_head *node, *next;
584 ACPI_FUNCTION_TRACE("acpi_driver_detach");
586 spin_lock(&acpi_device_lock);
587 list_for_each_safe(node, next, &acpi_device_list) {
588 struct acpi_device *dev =
589 container_of(node, struct acpi_device, g_list);
591 if (dev->driver == drv) {
592 spin_unlock(&acpi_device_lock);
593 if (drv->ops.remove)
594 drv->ops.remove(dev, ACPI_BUS_REMOVAL_NORMAL);
595 spin_lock(&acpi_device_lock);
596 dev->driver = NULL;
597 dev->driver_data = NULL;
598 atomic_dec(&drv->references);
601 spin_unlock(&acpi_device_lock);
602 return_VALUE(0);
606 * acpi_bus_register_driver - register a driver with the ACPI bus
607 * @driver: driver being registered
609 * Registers a driver with the ACPI bus. Searches the namespace for all
610 * devices that match the driver's criteria and binds. Returns zero for
611 * success or a negative error status for failure.
613 int acpi_bus_register_driver(struct acpi_driver *driver)
615 ACPI_FUNCTION_TRACE("acpi_bus_register_driver");
617 if (acpi_disabled)
618 return_VALUE(-ENODEV);
620 if (!driver)
621 return_VALUE(-EINVAL);
623 spin_lock(&acpi_device_lock);
624 list_add_tail(&driver->node, &acpi_bus_drivers);
625 spin_unlock(&acpi_device_lock);
626 acpi_driver_attach(driver);
628 return_VALUE(0);
631 EXPORT_SYMBOL(acpi_bus_register_driver);
634 * acpi_bus_unregister_driver - unregisters a driver with the APIC bus
635 * @driver: driver to unregister
637 * Unregisters a driver with the ACPI bus. Searches the namespace for all
638 * devices that match the driver's criteria and unbinds.
640 int acpi_bus_unregister_driver(struct acpi_driver *driver)
642 int error = 0;
644 ACPI_FUNCTION_TRACE("acpi_bus_unregister_driver");
646 if (driver) {
647 acpi_driver_detach(driver);
649 if (!atomic_read(&driver->references)) {
650 spin_lock(&acpi_device_lock);
651 list_del_init(&driver->node);
652 spin_unlock(&acpi_device_lock);
654 } else
655 error = -EINVAL;
656 return_VALUE(error);
659 EXPORT_SYMBOL(acpi_bus_unregister_driver);
662 * acpi_bus_find_driver - check if there is a driver installed for the device
663 * @device: device that we are trying to find a supporting driver for
665 * Parses the list of registered drivers looking for a driver applicable for
666 * the specified device.
668 static int acpi_bus_find_driver(struct acpi_device *device)
670 int result = 0;
671 struct list_head *node, *next;
673 ACPI_FUNCTION_TRACE("acpi_bus_find_driver");
675 spin_lock(&acpi_device_lock);
676 list_for_each_safe(node, next, &acpi_bus_drivers) {
677 struct acpi_driver *driver =
678 container_of(node, struct acpi_driver, node);
680 atomic_inc(&driver->references);
681 spin_unlock(&acpi_device_lock);
682 if (!acpi_bus_match(device, driver)) {
683 result = acpi_bus_driver_init(device, driver);
684 if (!result)
685 goto Done;
687 atomic_dec(&driver->references);
688 spin_lock(&acpi_device_lock);
690 spin_unlock(&acpi_device_lock);
692 Done:
693 return_VALUE(result);
696 /* --------------------------------------------------------------------------
697 Device Enumeration
698 -------------------------------------------------------------------------- */
700 static int acpi_bus_get_flags(struct acpi_device *device)
702 acpi_status status = AE_OK;
703 acpi_handle temp = NULL;
705 ACPI_FUNCTION_TRACE("acpi_bus_get_flags");
707 /* Presence of _STA indicates 'dynamic_status' */
708 status = acpi_get_handle(device->handle, "_STA", &temp);
709 if (ACPI_SUCCESS(status))
710 device->flags.dynamic_status = 1;
712 /* Presence of _CID indicates 'compatible_ids' */
713 status = acpi_get_handle(device->handle, "_CID", &temp);
714 if (ACPI_SUCCESS(status))
715 device->flags.compatible_ids = 1;
717 /* Presence of _RMV indicates 'removable' */
718 status = acpi_get_handle(device->handle, "_RMV", &temp);
719 if (ACPI_SUCCESS(status))
720 device->flags.removable = 1;
722 /* Presence of _EJD|_EJ0 indicates 'ejectable' */
723 status = acpi_get_handle(device->handle, "_EJD", &temp);
724 if (ACPI_SUCCESS(status))
725 device->flags.ejectable = 1;
726 else {
727 status = acpi_get_handle(device->handle, "_EJ0", &temp);
728 if (ACPI_SUCCESS(status))
729 device->flags.ejectable = 1;
732 /* Presence of _LCK indicates 'lockable' */
733 status = acpi_get_handle(device->handle, "_LCK", &temp);
734 if (ACPI_SUCCESS(status))
735 device->flags.lockable = 1;
737 /* Presence of _PS0|_PR0 indicates 'power manageable' */
738 status = acpi_get_handle(device->handle, "_PS0", &temp);
739 if (ACPI_FAILURE(status))
740 status = acpi_get_handle(device->handle, "_PR0", &temp);
741 if (ACPI_SUCCESS(status))
742 device->flags.power_manageable = 1;
744 /* Presence of _PRW indicates wake capable */
745 status = acpi_get_handle(device->handle, "_PRW", &temp);
746 if (ACPI_SUCCESS(status))
747 device->flags.wake_capable = 1;
749 /* TBD: Peformance management */
751 return_VALUE(0);
754 static void acpi_device_get_busid(struct acpi_device *device,
755 acpi_handle handle, int type)
757 char bus_id[5] = { '?', 0 };
758 struct acpi_buffer buffer = { sizeof(bus_id), bus_id };
759 int i = 0;
762 * Bus ID
763 * ------
764 * The device's Bus ID is simply the object name.
765 * TBD: Shouldn't this value be unique (within the ACPI namespace)?
767 switch (type) {
768 case ACPI_BUS_TYPE_SYSTEM:
769 strcpy(device->pnp.bus_id, "ACPI");
770 break;
771 case ACPI_BUS_TYPE_POWER_BUTTON:
772 strcpy(device->pnp.bus_id, "PWRF");
773 break;
774 case ACPI_BUS_TYPE_SLEEP_BUTTON:
775 strcpy(device->pnp.bus_id, "SLPF");
776 break;
777 default:
778 acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer);
779 /* Clean up trailing underscores (if any) */
780 for (i = 3; i > 1; i--) {
781 if (bus_id[i] == '_')
782 bus_id[i] = '\0';
783 else
784 break;
786 strcpy(device->pnp.bus_id, bus_id);
787 break;
791 static void acpi_device_set_id(struct acpi_device *device,
792 struct acpi_device *parent, acpi_handle handle,
793 int type)
795 struct acpi_device_info *info;
796 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
797 char *hid = NULL;
798 char *uid = NULL;
799 struct acpi_compatible_id_list *cid_list = NULL;
800 acpi_status status;
802 switch (type) {
803 case ACPI_BUS_TYPE_DEVICE:
804 status = acpi_get_object_info(handle, &buffer);
805 if (ACPI_FAILURE(status)) {
806 printk("%s: Error reading device info\n", __FUNCTION__);
807 return;
810 info = buffer.pointer;
811 if (info->valid & ACPI_VALID_HID)
812 hid = info->hardware_id.value;
813 if (info->valid & ACPI_VALID_UID)
814 uid = info->unique_id.value;
815 if (info->valid & ACPI_VALID_CID)
816 cid_list = &info->compatibility_id;
817 if (info->valid & ACPI_VALID_ADR) {
818 device->pnp.bus_address = info->address;
819 device->flags.bus_address = 1;
821 break;
822 case ACPI_BUS_TYPE_POWER:
823 hid = ACPI_POWER_HID;
824 break;
825 case ACPI_BUS_TYPE_PROCESSOR:
826 hid = ACPI_PROCESSOR_HID;
827 break;
828 case ACPI_BUS_TYPE_SYSTEM:
829 hid = ACPI_SYSTEM_HID;
830 break;
831 case ACPI_BUS_TYPE_THERMAL:
832 hid = ACPI_THERMAL_HID;
833 break;
834 case ACPI_BUS_TYPE_POWER_BUTTON:
835 hid = ACPI_BUTTON_HID_POWERF;
836 break;
837 case ACPI_BUS_TYPE_SLEEP_BUTTON:
838 hid = ACPI_BUTTON_HID_SLEEPF;
839 break;
843 * \_SB
844 * ----
845 * Fix for the system root bus device -- the only root-level device.
847 if (((acpi_handle)parent == ACPI_ROOT_OBJECT) && (type == ACPI_BUS_TYPE_DEVICE)) {
848 hid = ACPI_BUS_HID;
849 strcpy(device->pnp.device_name, ACPI_BUS_DEVICE_NAME);
850 strcpy(device->pnp.device_class, ACPI_BUS_CLASS);
853 if (hid) {
854 strcpy(device->pnp.hardware_id, hid);
855 device->flags.hardware_id = 1;
857 if (uid) {
858 strcpy(device->pnp.unique_id, uid);
859 device->flags.unique_id = 1;
861 if (cid_list) {
862 device->pnp.cid_list = kmalloc(cid_list->size, GFP_KERNEL);
863 if (device->pnp.cid_list)
864 memcpy(device->pnp.cid_list, cid_list, cid_list->size);
865 else
866 printk(KERN_ERR "Memory allocation error\n");
869 acpi_os_free(buffer.pointer);
872 static int acpi_device_set_context(struct acpi_device *device, int type)
874 acpi_status status = AE_OK;
875 int result = 0;
877 * Context
878 * -------
879 * Attach this 'struct acpi_device' to the ACPI object. This makes
880 * resolutions from handle->device very efficient. Note that we need
881 * to be careful with fixed-feature devices as they all attach to the
882 * root object.
884 if (type != ACPI_BUS_TYPE_POWER_BUTTON &&
885 type != ACPI_BUS_TYPE_SLEEP_BUTTON) {
886 status = acpi_attach_data(device->handle,
887 acpi_bus_data_handler, device);
889 if (ACPI_FAILURE(status)) {
890 printk("Error attaching device data\n");
891 result = -ENODEV;
894 return result;
897 static void acpi_device_get_debug_info(struct acpi_device *device,
898 acpi_handle handle, int type)
900 #ifdef CONFIG_ACPI_DEBUG_OUTPUT
901 char *type_string = NULL;
902 char name[80] = { '?', '\0' };
903 struct acpi_buffer buffer = { sizeof(name), name };
905 switch (type) {
906 case ACPI_BUS_TYPE_DEVICE:
907 type_string = "Device";
908 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
909 break;
910 case ACPI_BUS_TYPE_POWER:
911 type_string = "Power Resource";
912 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
913 break;
914 case ACPI_BUS_TYPE_PROCESSOR:
915 type_string = "Processor";
916 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
917 break;
918 case ACPI_BUS_TYPE_SYSTEM:
919 type_string = "System";
920 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
921 break;
922 case ACPI_BUS_TYPE_THERMAL:
923 type_string = "Thermal Zone";
924 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
925 break;
926 case ACPI_BUS_TYPE_POWER_BUTTON:
927 type_string = "Power Button";
928 sprintf(name, "PWRB");
929 break;
930 case ACPI_BUS_TYPE_SLEEP_BUTTON:
931 type_string = "Sleep Button";
932 sprintf(name, "SLPB");
933 break;
936 printk(KERN_DEBUG "Found %s %s [%p]\n", type_string, name, handle);
937 #endif /*CONFIG_ACPI_DEBUG_OUTPUT */
940 static int acpi_bus_remove(struct acpi_device *dev, int rmdevice)
942 int result = 0;
943 struct acpi_driver *driver;
945 ACPI_FUNCTION_TRACE("acpi_bus_remove");
947 if (!dev)
948 return_VALUE(-EINVAL);
950 driver = dev->driver;
952 if ((driver) && (driver->ops.remove)) {
954 if (driver->ops.stop) {
955 result = driver->ops.stop(dev, ACPI_BUS_REMOVAL_EJECT);
956 if (result)
957 return_VALUE(result);
960 result = dev->driver->ops.remove(dev, ACPI_BUS_REMOVAL_EJECT);
961 if (result) {
962 return_VALUE(result);
965 atomic_dec(&dev->driver->references);
966 dev->driver = NULL;
967 acpi_driver_data(dev) = NULL;
970 if (!rmdevice)
971 return_VALUE(0);
973 if (dev->flags.bus_address) {
974 if ((dev->parent) && (dev->parent->ops.unbind))
975 dev->parent->ops.unbind(dev);
978 acpi_device_unregister(dev, ACPI_BUS_REMOVAL_EJECT);
980 return_VALUE(0);
983 static int
984 acpi_add_single_object(struct acpi_device **child,
985 struct acpi_device *parent, acpi_handle handle, int type)
987 int result = 0;
988 struct acpi_device *device = NULL;
990 ACPI_FUNCTION_TRACE("acpi_add_single_object");
992 if (!child)
993 return_VALUE(-EINVAL);
995 device = kmalloc(sizeof(struct acpi_device), GFP_KERNEL);
996 if (!device) {
997 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Memory allocation error\n"));
998 return_VALUE(-ENOMEM);
1000 memset(device, 0, sizeof(struct acpi_device));
1002 device->handle = handle;
1003 device->parent = parent;
1005 acpi_device_get_busid(device, handle, type);
1008 * Flags
1009 * -----
1010 * Get prior to calling acpi_bus_get_status() so we know whether
1011 * or not _STA is present. Note that we only look for object
1012 * handles -- cannot evaluate objects until we know the device is
1013 * present and properly initialized.
1015 result = acpi_bus_get_flags(device);
1016 if (result)
1017 goto end;
1020 * Status
1021 * ------
1022 * See if the device is present. We always assume that non-Device
1023 * and non-Processor objects (e.g. thermal zones, power resources,
1024 * etc.) are present, functioning, etc. (at least when parent object
1025 * is present). Note that _STA has a different meaning for some
1026 * objects (e.g. power resources) so we need to be careful how we use
1027 * it.
1029 switch (type) {
1030 case ACPI_BUS_TYPE_PROCESSOR:
1031 case ACPI_BUS_TYPE_DEVICE:
1032 result = acpi_bus_get_status(device);
1033 if (ACPI_FAILURE(result) || !device->status.present) {
1034 result = -ENOENT;
1035 goto end;
1037 break;
1038 default:
1039 STRUCT_TO_INT(device->status) = 0x0F;
1040 break;
1044 * Initialize Device
1045 * -----------------
1046 * TBD: Synch with Core's enumeration/initialization process.
1050 * Hardware ID, Unique ID, & Bus Address
1051 * -------------------------------------
1053 acpi_device_set_id(device, parent, handle, type);
1056 * Power Management
1057 * ----------------
1059 if (device->flags.power_manageable) {
1060 result = acpi_bus_get_power_flags(device);
1061 if (result)
1062 goto end;
1066 * Wakeup device management
1067 *-----------------------
1069 if (device->flags.wake_capable) {
1070 result = acpi_bus_get_wakeup_device_flags(device);
1071 if (result)
1072 goto end;
1076 * Performance Management
1077 * ----------------------
1079 if (device->flags.performance_manageable) {
1080 result = acpi_bus_get_perf_flags(device);
1081 if (result)
1082 goto end;
1085 if ((result = acpi_device_set_context(device, type)))
1086 goto end;
1088 acpi_device_get_debug_info(device, handle, type);
1090 acpi_device_register(device, parent);
1093 * Bind _ADR-Based Devices
1094 * -----------------------
1095 * If there's a a bus address (_ADR) then we utilize the parent's
1096 * 'bind' function (if exists) to bind the ACPI- and natively-
1097 * enumerated device representations.
1099 if (device->flags.bus_address) {
1100 if (device->parent && device->parent->ops.bind)
1101 device->parent->ops.bind(device);
1105 * Locate & Attach Driver
1106 * ----------------------
1107 * If there's a hardware id (_HID) or compatible ids (_CID) we check
1108 * to see if there's a driver installed for this kind of device. Note
1109 * that drivers can install before or after a device is enumerated.
1111 * TBD: Assumes LDM provides driver hot-plug capability.
1113 acpi_bus_find_driver(device);
1115 end:
1116 if (!result)
1117 *child = device;
1118 else {
1119 kfree(device->pnp.cid_list);
1120 kfree(device);
1123 return_VALUE(result);
1126 static int acpi_bus_scan(struct acpi_device *start, struct acpi_bus_ops *ops)
1128 acpi_status status = AE_OK;
1129 struct acpi_device *parent = NULL;
1130 struct acpi_device *child = NULL;
1131 acpi_handle phandle = NULL;
1132 acpi_handle chandle = NULL;
1133 acpi_object_type type = 0;
1134 u32 level = 1;
1136 ACPI_FUNCTION_TRACE("acpi_bus_scan");
1138 if (!start)
1139 return_VALUE(-EINVAL);
1141 parent = start;
1142 phandle = start->handle;
1145 * Parse through the ACPI namespace, identify all 'devices', and
1146 * create a new 'struct acpi_device' for each.
1148 while ((level > 0) && parent) {
1150 status = acpi_get_next_object(ACPI_TYPE_ANY, phandle,
1151 chandle, &chandle);
1154 * If this scope is exhausted then move our way back up.
1156 if (ACPI_FAILURE(status)) {
1157 level--;
1158 chandle = phandle;
1159 acpi_get_parent(phandle, &phandle);
1160 if (parent->parent)
1161 parent = parent->parent;
1162 continue;
1165 status = acpi_get_type(chandle, &type);
1166 if (ACPI_FAILURE(status))
1167 continue;
1170 * If this is a scope object then parse it (depth-first).
1172 if (type == ACPI_TYPE_LOCAL_SCOPE) {
1173 level++;
1174 phandle = chandle;
1175 chandle = NULL;
1176 continue;
1180 * We're only interested in objects that we consider 'devices'.
1182 switch (type) {
1183 case ACPI_TYPE_DEVICE:
1184 type = ACPI_BUS_TYPE_DEVICE;
1185 break;
1186 case ACPI_TYPE_PROCESSOR:
1187 type = ACPI_BUS_TYPE_PROCESSOR;
1188 break;
1189 case ACPI_TYPE_THERMAL:
1190 type = ACPI_BUS_TYPE_THERMAL;
1191 break;
1192 case ACPI_TYPE_POWER:
1193 type = ACPI_BUS_TYPE_POWER;
1194 break;
1195 default:
1196 continue;
1199 if (ops->acpi_op_add)
1200 status = acpi_add_single_object(&child, parent,
1201 chandle, type);
1202 else
1203 status = acpi_bus_get_device(chandle, &child);
1205 if (ACPI_FAILURE(status))
1206 continue;
1208 if (ops->acpi_op_start) {
1209 status = acpi_start_single_object(child);
1210 if (ACPI_FAILURE(status))
1211 continue;
1215 * If the device is present, enabled, and functioning then
1216 * parse its scope (depth-first). Note that we need to
1217 * represent absent devices to facilitate PnP notifications
1218 * -- but only the subtree head (not all of its children,
1219 * which will be enumerated when the parent is inserted).
1221 * TBD: Need notifications and other detection mechanisms
1222 * in place before we can fully implement this.
1224 if (child->status.present) {
1225 status = acpi_get_next_object(ACPI_TYPE_ANY, chandle,
1226 NULL, NULL);
1227 if (ACPI_SUCCESS(status)) {
1228 level++;
1229 phandle = chandle;
1230 chandle = NULL;
1231 parent = child;
1236 return_VALUE(0);
1240 acpi_bus_add(struct acpi_device **child,
1241 struct acpi_device *parent, acpi_handle handle, int type)
1243 int result;
1244 struct acpi_bus_ops ops;
1246 ACPI_FUNCTION_TRACE("acpi_bus_add");
1248 result = acpi_add_single_object(child, parent, handle, type);
1249 if (!result) {
1250 memset(&ops, 0, sizeof(ops));
1251 ops.acpi_op_add = 1;
1252 result = acpi_bus_scan(*child, &ops);
1254 return_VALUE(result);
1257 EXPORT_SYMBOL(acpi_bus_add);
1259 int acpi_bus_start(struct acpi_device *device)
1261 int result;
1262 struct acpi_bus_ops ops;
1264 ACPI_FUNCTION_TRACE("acpi_bus_start");
1266 if (!device)
1267 return_VALUE(-EINVAL);
1269 result = acpi_start_single_object(device);
1270 if (!result) {
1271 memset(&ops, 0, sizeof(ops));
1272 ops.acpi_op_start = 1;
1273 result = acpi_bus_scan(device, &ops);
1275 return_VALUE(result);
1278 EXPORT_SYMBOL(acpi_bus_start);
1280 int acpi_bus_trim(struct acpi_device *start, int rmdevice)
1282 acpi_status status;
1283 struct acpi_device *parent, *child;
1284 acpi_handle phandle, chandle;
1285 acpi_object_type type;
1286 u32 level = 1;
1287 int err = 0;
1289 parent = start;
1290 phandle = start->handle;
1291 child = chandle = NULL;
1293 while ((level > 0) && parent && (!err)) {
1294 status = acpi_get_next_object(ACPI_TYPE_ANY, phandle,
1295 chandle, &chandle);
1298 * If this scope is exhausted then move our way back up.
1300 if (ACPI_FAILURE(status)) {
1301 level--;
1302 chandle = phandle;
1303 acpi_get_parent(phandle, &phandle);
1304 child = parent;
1305 parent = parent->parent;
1307 if (level == 0)
1308 err = acpi_bus_remove(child, rmdevice);
1309 else
1310 err = acpi_bus_remove(child, 1);
1312 continue;
1315 status = acpi_get_type(chandle, &type);
1316 if (ACPI_FAILURE(status)) {
1317 continue;
1320 * If there is a device corresponding to chandle then
1321 * parse it (depth-first).
1323 if (acpi_bus_get_device(chandle, &child) == 0) {
1324 level++;
1325 phandle = chandle;
1326 chandle = NULL;
1327 parent = child;
1329 continue;
1331 return err;
1333 EXPORT_SYMBOL_GPL(acpi_bus_trim);
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);