2 * scan.c - support for transforming the ACPI namespace into individual objects
5 #include <linux/module.h>
6 #include <linux/init.h>
7 #include <linux/kernel.h>
8 #include <linux/acpi.h>
10 #include <acpi/acpi_drivers.h>
11 #include <acpi/acinterp.h> /* for acpi_ex_eisa_id_to_string() */
13 #define _COMPONENT ACPI_BUS_COMPONENT
14 ACPI_MODULE_NAME("scan")
15 #define STRUCT_TO_INT(s) (*((int*)&s))
16 extern struct acpi_device *acpi_root;
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"
23 static LIST_HEAD(acpi_device_list);
24 DEFINE_SPINLOCK(acpi_device_lock);
25 LIST_HEAD(acpi_wakeup_device_list);
28 static void acpi_device_release(struct kobject *kobj)
30 struct acpi_device *dev = container_of(kobj, struct acpi_device, kobj);
31 kfree(dev->pnp.cid_list);
35 struct acpi_device_attribute {
36 struct attribute attr;
37 ssize_t(*show) (struct acpi_device *, char *);
38 ssize_t(*store) (struct acpi_device *, const char *, size_t);
41 typedef void acpi_device_sysfs_files(struct kobject *,
42 const struct attribute *);
44 static void setup_sys_fs_device_files(struct acpi_device *dev,
45 acpi_device_sysfs_files * func);
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)
52 #define to_acpi_device(n) container_of(n, struct acpi_device, kobj)
53 #define to_handle_attr(n) container_of(n, struct acpi_device_attribute, attr);
55 static ssize_t acpi_device_attr_show(struct kobject *kobj,
56 struct attribute *attr, char *buf)
58 struct acpi_device *device = to_acpi_device(kobj);
59 struct acpi_device_attribute *attribute = to_handle_attr(attr);
60 return attribute->show ? attribute->show(device, buf) : -EIO;
62 static ssize_t acpi_device_attr_store(struct kobject *kobj,
63 struct attribute *attr, const char *buf,
66 struct acpi_device *device = to_acpi_device(kobj);
67 struct acpi_device_attribute *attribute = to_handle_attr(attr);
68 return attribute->store ? attribute->store(device, buf, len) : -EIO;
71 static struct sysfs_ops acpi_device_sysfs_ops = {
72 .show = acpi_device_attr_show,
73 .store = acpi_device_attr_store,
76 static struct kobj_type ktype_acpi_ns = {
77 .sysfs_ops = &acpi_device_sysfs_ops,
78 .release = acpi_device_release,
81 static int namespace_uevent(struct kset *kset, struct kobject *kobj,
82 char **envp, int num_envp, char *buffer,
85 struct acpi_device *dev = to_acpi_device(kobj);
92 if (add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &len,
93 "PHYSDEVDRIVER=%s", dev->driver->name))
101 static struct kset_uevent_ops namespace_uevent_ops = {
102 .uevent = &namespace_uevent,
105 static struct kset acpi_namespace_kset = {
109 .subsys = &acpi_subsys,
110 .ktype = &ktype_acpi_ns,
111 .uevent_ops = &namespace_uevent_ops,
114 static void acpi_device_register(struct acpi_device *device,
115 struct acpi_device *parent)
122 * Link this device to its parent and siblings.
124 INIT_LIST_HEAD(&device->children);
125 INIT_LIST_HEAD(&device->node);
126 INIT_LIST_HEAD(&device->g_list);
127 INIT_LIST_HEAD(&device->wakeup_list);
129 spin_lock(&acpi_device_lock);
130 if (device->parent) {
131 list_add_tail(&device->node, &device->parent->children);
132 list_add_tail(&device->g_list, &device->parent->g_list);
134 list_add_tail(&device->g_list, &acpi_device_list);
135 if (device->wakeup.flags.valid)
136 list_add_tail(&device->wakeup_list, &acpi_wakeup_device_list);
137 spin_unlock(&acpi_device_lock);
139 strlcpy(device->kobj.name, device->pnp.bus_id, KOBJ_NAME_LEN);
141 device->kobj.parent = &parent->kobj;
142 device->kobj.ktype = &ktype_acpi_ns;
143 device->kobj.kset = &acpi_namespace_kset;
144 err = kobject_register(&device->kobj);
146 printk(KERN_WARNING "%s: kobject_register error: %d\n",
148 create_sysfs_device_files(device);
151 static void acpi_device_unregister(struct acpi_device *device, int type)
153 spin_lock(&acpi_device_lock);
154 if (device->parent) {
155 list_del(&device->node);
156 list_del(&device->g_list);
158 list_del(&device->g_list);
160 list_del(&device->wakeup_list);
162 spin_unlock(&acpi_device_lock);
164 acpi_detach_data(device->handle, acpi_bus_data_handler);
165 remove_sysfs_device_files(device);
166 kobject_unregister(&device->kobj);
169 void acpi_bus_data_handler(acpi_handle handle, u32 function, void *context)
177 static int acpi_bus_get_power_flags(struct acpi_device *device)
179 acpi_status status = 0;
180 acpi_handle handle = NULL;
185 * Power Management Flags
187 status = acpi_get_handle(device->handle, "_PSC", &handle);
188 if (ACPI_SUCCESS(status))
189 device->power.flags.explicit_get = 1;
190 status = acpi_get_handle(device->handle, "_IRC", &handle);
191 if (ACPI_SUCCESS(status))
192 device->power.flags.inrush_current = 1;
195 * Enumerate supported power management states
197 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3; i++) {
198 struct acpi_device_power_state *ps = &device->power.states[i];
199 char object_name[5] = { '_', 'P', 'R', '0' + i, '\0' };
201 /* Evaluate "_PRx" to se if power resources are referenced */
202 acpi_evaluate_reference(device->handle, object_name, NULL,
204 if (ps->resources.count) {
205 device->power.flags.power_resources = 1;
209 /* Evaluate "_PSx" to see if we can do explicit sets */
210 object_name[2] = 'S';
211 status = acpi_get_handle(device->handle, object_name, &handle);
212 if (ACPI_SUCCESS(status)) {
213 ps->flags.explicit_set = 1;
217 /* State is valid if we have some power control */
218 if (ps->resources.count || ps->flags.explicit_set)
221 ps->power = -1; /* Unknown - driver assigned */
222 ps->latency = -1; /* Unknown - driver assigned */
225 /* Set defaults for D0 and D3 states (always valid) */
226 device->power.states[ACPI_STATE_D0].flags.valid = 1;
227 device->power.states[ACPI_STATE_D0].power = 100;
228 device->power.states[ACPI_STATE_D3].flags.valid = 1;
229 device->power.states[ACPI_STATE_D3].power = 0;
231 /* TBD: System wake support and resource requirements. */
233 device->power.state = ACPI_STATE_UNKNOWN;
238 int acpi_match_ids(struct acpi_device *device, char *ids)
240 if (device->flags.hardware_id)
241 if (strstr(ids, device->pnp.hardware_id))
244 if (device->flags.compatible_ids) {
245 struct acpi_compatible_id_list *cid_list = device->pnp.cid_list;
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))
258 acpi_bus_extract_wakeup_device_power_package(struct acpi_device *device,
259 union acpi_object *package)
262 union acpi_object *element = NULL;
264 if (!device || !package || (package->package.count < 2))
265 return AE_BAD_PARAMETER;
267 element = &(package->package.elements[0]);
269 return AE_BAD_PARAMETER;
270 if (element->type == ACPI_TYPE_PACKAGE) {
271 if ((element->package.count < 2) ||
272 (element->package.elements[0].type !=
273 ACPI_TYPE_LOCAL_REFERENCE)
274 || (element->package.elements[1].type != ACPI_TYPE_INTEGER))
276 device->wakeup.gpe_device =
277 element->package.elements[0].reference.handle;
278 device->wakeup.gpe_number =
279 (u32) element->package.elements[1].integer.value;
280 } else if (element->type == ACPI_TYPE_INTEGER) {
281 device->wakeup.gpe_number = element->integer.value;
285 element = &(package->package.elements[1]);
286 if (element->type != ACPI_TYPE_INTEGER) {
289 device->wakeup.sleep_state = element->integer.value;
291 if ((package->package.count - 2) > ACPI_MAX_HANDLES) {
294 device->wakeup.resources.count = package->package.count - 2;
295 for (i = 0; i < device->wakeup.resources.count; i++) {
296 element = &(package->package.elements[i + 2]);
297 if (element->type != ACPI_TYPE_ANY) {
301 device->wakeup.resources.handles[i] = element->reference.handle;
307 static int acpi_bus_get_wakeup_device_flags(struct acpi_device *device)
309 acpi_status status = 0;
310 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
311 union acpi_object *package = NULL;
315 status = acpi_evaluate_object(device->handle, "_PRW", NULL, &buffer);
316 if (ACPI_FAILURE(status)) {
317 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PRW"));
321 package = (union acpi_object *)buffer.pointer;
322 status = acpi_bus_extract_wakeup_device_power_package(device, package);
323 if (ACPI_FAILURE(status)) {
324 ACPI_EXCEPTION((AE_INFO, status, "Extracting _PRW package"));
328 kfree(buffer.pointer);
330 device->wakeup.flags.valid = 1;
331 /* Power button, Lid switch always enable wakeup */
332 if (!acpi_match_ids(device, "PNP0C0D,PNP0C0C,PNP0C0E"))
333 device->wakeup.flags.run_wake = 1;
336 if (ACPI_FAILURE(status))
337 device->flags.wake_capable = 0;
341 /* --------------------------------------------------------------------------
342 ACPI sysfs device file support
343 -------------------------------------------------------------------------- */
344 static ssize_t acpi_eject_store(struct acpi_device *device,
345 const char *buf, size_t count);
347 #define ACPI_DEVICE_ATTR(_name,_mode,_show,_store) \
348 static struct acpi_device_attribute acpi_device_attr_##_name = \
349 __ATTR(_name, _mode, _show, _store)
351 ACPI_DEVICE_ATTR(eject, 0200, NULL, acpi_eject_store);
354 * setup_sys_fs_device_files - sets up the device files under device namespace
355 * @dev: acpi_device object
356 * @func: function pointer to create or destroy the device file
359 setup_sys_fs_device_files(struct acpi_device *dev,
360 acpi_device_sysfs_files * func)
363 acpi_handle temp = NULL;
366 * If device has _EJ0, 'eject' file is created that is used to trigger
367 * hot-removal function from userland.
369 status = acpi_get_handle(dev->handle, "_EJ0", &temp);
370 if (ACPI_SUCCESS(status))
371 (*(func)) (&dev->kobj, &acpi_device_attr_eject.attr);
374 static int acpi_eject_operation(acpi_handle handle, int lockable)
376 struct acpi_object_list arg_list;
377 union acpi_object arg;
378 acpi_status status = AE_OK;
381 * TBD: evaluate _PS3?
386 arg_list.pointer = &arg;
387 arg.type = ACPI_TYPE_INTEGER;
388 arg.integer.value = 0;
389 acpi_evaluate_object(handle, "_LCK", &arg_list, NULL);
393 arg_list.pointer = &arg;
394 arg.type = ACPI_TYPE_INTEGER;
395 arg.integer.value = 1;
401 status = acpi_evaluate_object(handle, "_EJ0", &arg_list, NULL);
402 if (ACPI_FAILURE(status)) {
410 acpi_eject_store(struct acpi_device *device, const char *buf, size_t count)
417 acpi_object_type type = 0;
419 if ((!count) || (buf[0] != '1')) {
423 if (device->driver == NULL) {
428 status = acpi_get_type(device->handle, &type);
429 if (ACPI_FAILURE(status) || (!device->flags.ejectable)) {
434 islockable = device->flags.lockable;
435 handle = device->handle;
437 result = acpi_bus_trim(device, 1);
440 result = acpi_eject_operation(handle, islockable);
449 /* --------------------------------------------------------------------------
450 Performance Management
451 -------------------------------------------------------------------------- */
453 static int acpi_bus_get_perf_flags(struct acpi_device *device)
455 device->performance.state = ACPI_STATE_UNKNOWN;
459 /* --------------------------------------------------------------------------
461 -------------------------------------------------------------------------- */
463 static LIST_HEAD(acpi_bus_drivers);
466 * acpi_bus_match - match device IDs to driver's supported IDs
467 * @device: the device that we are trying to match to a driver
468 * @driver: driver whose device id table is being checked
470 * Checks the device's hardware (_HID) or compatible (_CID) ids to see if it
471 * matches the specified driver's criteria.
474 acpi_bus_match(struct acpi_device *device, struct acpi_driver *driver)
476 if (driver && driver->ops.match)
477 return driver->ops.match(device, driver);
478 return acpi_match_ids(device, driver->ids);
482 * acpi_bus_driver_init - add a device to a driver
483 * @device: the device to add and initialize
484 * @driver: driver for the device
486 * Used to initialize a device via its device driver. Called whenever a
487 * driver is bound to a device. Invokes the driver's add() and start() ops.
490 acpi_bus_driver_init(struct acpi_device *device, struct acpi_driver *driver)
495 if (!device || !driver)
498 if (!driver->ops.add)
501 result = driver->ops.add(device);
503 device->driver = NULL;
504 acpi_driver_data(device) = NULL;
508 device->driver = driver;
511 * TBD - Configuration Management: Assign resources to device based
512 * upon possible configuration and currently allocated resources.
515 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
516 "Driver successfully bound to device\n"));
520 static int acpi_start_single_object(struct acpi_device *device)
523 struct acpi_driver *driver;
526 if (!(driver = device->driver))
529 if (driver->ops.start) {
530 result = driver->ops.start(device);
531 if (result && driver->ops.remove)
532 driver->ops.remove(device, ACPI_BUS_REMOVAL_NORMAL);
538 static void acpi_driver_attach(struct acpi_driver *drv)
540 struct list_head *node, *next;
543 spin_lock(&acpi_device_lock);
544 list_for_each_safe(node, next, &acpi_device_list) {
545 struct acpi_device *dev =
546 container_of(node, struct acpi_device, g_list);
548 if (dev->driver || !dev->status.present)
550 spin_unlock(&acpi_device_lock);
552 if (!acpi_bus_match(dev, drv)) {
553 if (!acpi_bus_driver_init(dev, drv)) {
554 acpi_start_single_object(dev);
555 atomic_inc(&drv->references);
556 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
557 "Found driver [%s] for device [%s]\n",
558 drv->name, dev->pnp.bus_id));
561 spin_lock(&acpi_device_lock);
563 spin_unlock(&acpi_device_lock);
566 static void acpi_driver_detach(struct acpi_driver *drv)
568 struct list_head *node, *next;
571 spin_lock(&acpi_device_lock);
572 list_for_each_safe(node, next, &acpi_device_list) {
573 struct acpi_device *dev =
574 container_of(node, struct acpi_device, g_list);
576 if (dev->driver == drv) {
577 spin_unlock(&acpi_device_lock);
579 drv->ops.remove(dev, ACPI_BUS_REMOVAL_NORMAL);
580 spin_lock(&acpi_device_lock);
582 dev->driver_data = NULL;
583 atomic_dec(&drv->references);
586 spin_unlock(&acpi_device_lock);
590 * acpi_bus_register_driver - register a driver with the ACPI bus
591 * @driver: driver being registered
593 * Registers a driver with the ACPI bus. Searches the namespace for all
594 * devices that match the driver's criteria and binds. Returns zero for
595 * success or a negative error status for failure.
597 int acpi_bus_register_driver(struct acpi_driver *driver)
603 spin_lock(&acpi_device_lock);
604 list_add_tail(&driver->node, &acpi_bus_drivers);
605 spin_unlock(&acpi_device_lock);
606 acpi_driver_attach(driver);
611 EXPORT_SYMBOL(acpi_bus_register_driver);
614 * acpi_bus_unregister_driver - unregisters a driver with the APIC bus
615 * @driver: driver to unregister
617 * Unregisters a driver with the ACPI bus. Searches the namespace for all
618 * devices that match the driver's criteria and unbinds.
620 void acpi_bus_unregister_driver(struct acpi_driver *driver)
622 acpi_driver_detach(driver);
624 if (!atomic_read(&driver->references)) {
625 spin_lock(&acpi_device_lock);
626 list_del_init(&driver->node);
627 spin_unlock(&acpi_device_lock);
632 EXPORT_SYMBOL(acpi_bus_unregister_driver);
635 * acpi_bus_find_driver - check if there is a driver installed for the device
636 * @device: device that we are trying to find a supporting driver for
638 * Parses the list of registered drivers looking for a driver applicable for
639 * the specified device.
641 static int acpi_bus_find_driver(struct acpi_device *device)
644 struct list_head *node, *next;
647 spin_lock(&acpi_device_lock);
648 list_for_each_safe(node, next, &acpi_bus_drivers) {
649 struct acpi_driver *driver =
650 container_of(node, struct acpi_driver, node);
652 atomic_inc(&driver->references);
653 spin_unlock(&acpi_device_lock);
654 if (!acpi_bus_match(device, driver)) {
655 result = acpi_bus_driver_init(device, driver);
659 atomic_dec(&driver->references);
660 spin_lock(&acpi_device_lock);
662 spin_unlock(&acpi_device_lock);
668 /* --------------------------------------------------------------------------
670 -------------------------------------------------------------------------- */
673 acpi_bus_get_ejd(acpi_handle handle, acpi_handle *ejd)
677 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
678 union acpi_object *obj;
680 status = acpi_get_handle(handle, "_EJD", &tmp);
681 if (ACPI_FAILURE(status))
684 status = acpi_evaluate_object(handle, "_EJD", NULL, &buffer);
685 if (ACPI_SUCCESS(status)) {
686 obj = buffer.pointer;
687 status = acpi_get_handle(NULL, obj->string.pointer, ejd);
688 kfree(buffer.pointer);
692 EXPORT_SYMBOL_GPL(acpi_bus_get_ejd);
695 static int acpi_bus_get_flags(struct acpi_device *device)
697 acpi_status status = AE_OK;
698 acpi_handle temp = NULL;
701 /* Presence of _STA indicates 'dynamic_status' */
702 status = acpi_get_handle(device->handle, "_STA", &temp);
703 if (ACPI_SUCCESS(status))
704 device->flags.dynamic_status = 1;
706 /* Presence of _CID indicates 'compatible_ids' */
707 status = acpi_get_handle(device->handle, "_CID", &temp);
708 if (ACPI_SUCCESS(status))
709 device->flags.compatible_ids = 1;
711 /* Presence of _RMV indicates 'removable' */
712 status = acpi_get_handle(device->handle, "_RMV", &temp);
713 if (ACPI_SUCCESS(status))
714 device->flags.removable = 1;
716 /* Presence of _EJD|_EJ0 indicates 'ejectable' */
717 status = acpi_get_handle(device->handle, "_EJD", &temp);
718 if (ACPI_SUCCESS(status))
719 device->flags.ejectable = 1;
721 status = acpi_get_handle(device->handle, "_EJ0", &temp);
722 if (ACPI_SUCCESS(status))
723 device->flags.ejectable = 1;
726 /* Presence of _LCK indicates 'lockable' */
727 status = acpi_get_handle(device->handle, "_LCK", &temp);
728 if (ACPI_SUCCESS(status))
729 device->flags.lockable = 1;
731 /* Presence of _PS0|_PR0 indicates 'power manageable' */
732 status = acpi_get_handle(device->handle, "_PS0", &temp);
733 if (ACPI_FAILURE(status))
734 status = acpi_get_handle(device->handle, "_PR0", &temp);
735 if (ACPI_SUCCESS(status))
736 device->flags.power_manageable = 1;
738 /* Presence of _PRW indicates wake capable */
739 status = acpi_get_handle(device->handle, "_PRW", &temp);
740 if (ACPI_SUCCESS(status))
741 device->flags.wake_capable = 1;
743 /* TBD: Peformance management */
748 static void acpi_device_get_busid(struct acpi_device *device,
749 acpi_handle handle, int type)
751 char bus_id[5] = { '?', 0 };
752 struct acpi_buffer buffer = { sizeof(bus_id), bus_id };
758 * The device's Bus ID is simply the object name.
759 * TBD: Shouldn't this value be unique (within the ACPI namespace)?
762 case ACPI_BUS_TYPE_SYSTEM:
763 strcpy(device->pnp.bus_id, "ACPI");
765 case ACPI_BUS_TYPE_POWER_BUTTON:
766 strcpy(device->pnp.bus_id, "PWRF");
768 case ACPI_BUS_TYPE_SLEEP_BUTTON:
769 strcpy(device->pnp.bus_id, "SLPF");
772 acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer);
773 /* Clean up trailing underscores (if any) */
774 for (i = 3; i > 1; i--) {
775 if (bus_id[i] == '_')
780 strcpy(device->pnp.bus_id, bus_id);
785 static void acpi_device_set_id(struct acpi_device *device,
786 struct acpi_device *parent, acpi_handle handle,
789 struct acpi_device_info *info;
790 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
793 struct acpi_compatible_id_list *cid_list = NULL;
797 case ACPI_BUS_TYPE_DEVICE:
798 status = acpi_get_object_info(handle, &buffer);
799 if (ACPI_FAILURE(status)) {
800 printk("%s: Error reading device info\n", __FUNCTION__);
804 info = buffer.pointer;
805 if (info->valid & ACPI_VALID_HID)
806 hid = info->hardware_id.value;
807 if (info->valid & ACPI_VALID_UID)
808 uid = info->unique_id.value;
809 if (info->valid & ACPI_VALID_CID)
810 cid_list = &info->compatibility_id;
811 if (info->valid & ACPI_VALID_ADR) {
812 device->pnp.bus_address = info->address;
813 device->flags.bus_address = 1;
816 case ACPI_BUS_TYPE_POWER:
817 hid = ACPI_POWER_HID;
819 case ACPI_BUS_TYPE_PROCESSOR:
820 hid = ACPI_PROCESSOR_HID;
822 case ACPI_BUS_TYPE_SYSTEM:
823 hid = ACPI_SYSTEM_HID;
825 case ACPI_BUS_TYPE_THERMAL:
826 hid = ACPI_THERMAL_HID;
828 case ACPI_BUS_TYPE_POWER_BUTTON:
829 hid = ACPI_BUTTON_HID_POWERF;
831 case ACPI_BUS_TYPE_SLEEP_BUTTON:
832 hid = ACPI_BUTTON_HID_SLEEPF;
839 * Fix for the system root bus device -- the only root-level device.
841 if (((acpi_handle)parent == ACPI_ROOT_OBJECT) && (type == ACPI_BUS_TYPE_DEVICE)) {
843 strcpy(device->pnp.device_name, ACPI_BUS_DEVICE_NAME);
844 strcpy(device->pnp.device_class, ACPI_BUS_CLASS);
848 strcpy(device->pnp.hardware_id, hid);
849 device->flags.hardware_id = 1;
852 strcpy(device->pnp.unique_id, uid);
853 device->flags.unique_id = 1;
856 device->pnp.cid_list = kmalloc(cid_list->size, GFP_KERNEL);
857 if (device->pnp.cid_list)
858 memcpy(device->pnp.cid_list, cid_list, cid_list->size);
860 printk(KERN_ERR "Memory allocation error\n");
863 kfree(buffer.pointer);
866 static int acpi_device_set_context(struct acpi_device *device, int type)
868 acpi_status status = AE_OK;
873 * Attach this 'struct acpi_device' to the ACPI object. This makes
874 * resolutions from handle->device very efficient. Note that we need
875 * to be careful with fixed-feature devices as they all attach to the
878 if (type != ACPI_BUS_TYPE_POWER_BUTTON &&
879 type != ACPI_BUS_TYPE_SLEEP_BUTTON) {
880 status = acpi_attach_data(device->handle,
881 acpi_bus_data_handler, device);
883 if (ACPI_FAILURE(status)) {
884 printk("Error attaching device data\n");
891 static int acpi_bus_remove(struct acpi_device *dev, int rmdevice)
894 struct acpi_driver *driver;
900 driver = dev->driver;
902 if ((driver) && (driver->ops.remove)) {
904 if (driver->ops.stop) {
905 result = driver->ops.stop(dev, ACPI_BUS_REMOVAL_EJECT);
910 result = dev->driver->ops.remove(dev, ACPI_BUS_REMOVAL_EJECT);
915 atomic_dec(&dev->driver->references);
917 acpi_driver_data(dev) = NULL;
923 if (dev->flags.bus_address) {
924 if ((dev->parent) && (dev->parent->ops.unbind))
925 dev->parent->ops.unbind(dev);
928 acpi_device_unregister(dev, ACPI_BUS_REMOVAL_EJECT);
934 acpi_add_single_object(struct acpi_device **child,
935 struct acpi_device *parent, acpi_handle handle, int type)
938 struct acpi_device *device = NULL;
944 device = kzalloc(sizeof(struct acpi_device), GFP_KERNEL);
946 printk(KERN_ERR PREFIX "Memory allocation error\n");
950 device->handle = handle;
951 device->parent = parent;
953 acpi_device_get_busid(device, handle, type);
958 * Get prior to calling acpi_bus_get_status() so we know whether
959 * or not _STA is present. Note that we only look for object
960 * handles -- cannot evaluate objects until we know the device is
961 * present and properly initialized.
963 result = acpi_bus_get_flags(device);
970 * See if the device is present. We always assume that non-Device
971 * and non-Processor objects (e.g. thermal zones, power resources,
972 * etc.) are present, functioning, etc. (at least when parent object
973 * is present). Note that _STA has a different meaning for some
974 * objects (e.g. power resources) so we need to be careful how we use
978 case ACPI_BUS_TYPE_PROCESSOR:
979 case ACPI_BUS_TYPE_DEVICE:
980 result = acpi_bus_get_status(device);
981 if (ACPI_FAILURE(result) || !device->status.present) {
987 STRUCT_TO_INT(device->status) = 0x0F;
994 * TBD: Synch with Core's enumeration/initialization process.
998 * Hardware ID, Unique ID, & Bus Address
999 * -------------------------------------
1001 acpi_device_set_id(device, parent, handle, type);
1007 if (device->flags.power_manageable) {
1008 result = acpi_bus_get_power_flags(device);
1014 * Wakeup device management
1015 *-----------------------
1017 if (device->flags.wake_capable) {
1018 result = acpi_bus_get_wakeup_device_flags(device);
1024 * Performance Management
1025 * ----------------------
1027 if (device->flags.performance_manageable) {
1028 result = acpi_bus_get_perf_flags(device);
1033 if ((result = acpi_device_set_context(device, type)))
1036 acpi_device_register(device, parent);
1039 * Bind _ADR-Based Devices
1040 * -----------------------
1041 * If there's a a bus address (_ADR) then we utilize the parent's
1042 * 'bind' function (if exists) to bind the ACPI- and natively-
1043 * enumerated device representations.
1045 if (device->flags.bus_address) {
1046 if (device->parent && device->parent->ops.bind)
1047 device->parent->ops.bind(device);
1051 * Locate & Attach Driver
1052 * ----------------------
1053 * If there's a hardware id (_HID) or compatible ids (_CID) we check
1054 * to see if there's a driver installed for this kind of device. Note
1055 * that drivers can install before or after a device is enumerated.
1057 * TBD: Assumes LDM provides driver hot-plug capability.
1059 acpi_bus_find_driver(device);
1065 kfree(device->pnp.cid_list);
1072 static int acpi_bus_scan(struct acpi_device *start, struct acpi_bus_ops *ops)
1074 acpi_status status = AE_OK;
1075 struct acpi_device *parent = NULL;
1076 struct acpi_device *child = NULL;
1077 acpi_handle phandle = NULL;
1078 acpi_handle chandle = NULL;
1079 acpi_object_type type = 0;
1087 phandle = start->handle;
1090 * Parse through the ACPI namespace, identify all 'devices', and
1091 * create a new 'struct acpi_device' for each.
1093 while ((level > 0) && parent) {
1095 status = acpi_get_next_object(ACPI_TYPE_ANY, phandle,
1099 * If this scope is exhausted then move our way back up.
1101 if (ACPI_FAILURE(status)) {
1104 acpi_get_parent(phandle, &phandle);
1106 parent = parent->parent;
1110 status = acpi_get_type(chandle, &type);
1111 if (ACPI_FAILURE(status))
1115 * If this is a scope object then parse it (depth-first).
1117 if (type == ACPI_TYPE_LOCAL_SCOPE) {
1125 * We're only interested in objects that we consider 'devices'.
1128 case ACPI_TYPE_DEVICE:
1129 type = ACPI_BUS_TYPE_DEVICE;
1131 case ACPI_TYPE_PROCESSOR:
1132 type = ACPI_BUS_TYPE_PROCESSOR;
1134 case ACPI_TYPE_THERMAL:
1135 type = ACPI_BUS_TYPE_THERMAL;
1137 case ACPI_TYPE_POWER:
1138 type = ACPI_BUS_TYPE_POWER;
1144 if (ops->acpi_op_add)
1145 status = acpi_add_single_object(&child, parent,
1148 status = acpi_bus_get_device(chandle, &child);
1150 if (ACPI_FAILURE(status))
1153 if (ops->acpi_op_start) {
1154 status = acpi_start_single_object(child);
1155 if (ACPI_FAILURE(status))
1160 * If the device is present, enabled, and functioning then
1161 * parse its scope (depth-first). Note that we need to
1162 * represent absent devices to facilitate PnP notifications
1163 * -- but only the subtree head (not all of its children,
1164 * which will be enumerated when the parent is inserted).
1166 * TBD: Need notifications and other detection mechanisms
1167 * in place before we can fully implement this.
1169 if (child->status.present) {
1170 status = acpi_get_next_object(ACPI_TYPE_ANY, chandle,
1172 if (ACPI_SUCCESS(status)) {
1185 acpi_bus_add(struct acpi_device **child,
1186 struct acpi_device *parent, acpi_handle handle, int type)
1189 struct acpi_bus_ops ops;
1192 result = acpi_add_single_object(child, parent, handle, type);
1194 memset(&ops, 0, sizeof(ops));
1195 ops.acpi_op_add = 1;
1196 result = acpi_bus_scan(*child, &ops);
1201 EXPORT_SYMBOL(acpi_bus_add);
1203 int acpi_bus_start(struct acpi_device *device)
1206 struct acpi_bus_ops ops;
1212 result = acpi_start_single_object(device);
1214 memset(&ops, 0, sizeof(ops));
1215 ops.acpi_op_start = 1;
1216 result = acpi_bus_scan(device, &ops);
1221 EXPORT_SYMBOL(acpi_bus_start);
1223 int acpi_bus_trim(struct acpi_device *start, int rmdevice)
1226 struct acpi_device *parent, *child;
1227 acpi_handle phandle, chandle;
1228 acpi_object_type type;
1233 phandle = start->handle;
1234 child = chandle = NULL;
1236 while ((level > 0) && parent && (!err)) {
1237 status = acpi_get_next_object(ACPI_TYPE_ANY, phandle,
1241 * If this scope is exhausted then move our way back up.
1243 if (ACPI_FAILURE(status)) {
1246 acpi_get_parent(phandle, &phandle);
1248 parent = parent->parent;
1251 err = acpi_bus_remove(child, rmdevice);
1253 err = acpi_bus_remove(child, 1);
1258 status = acpi_get_type(chandle, &type);
1259 if (ACPI_FAILURE(status)) {
1263 * If there is a device corresponding to chandle then
1264 * parse it (depth-first).
1266 if (acpi_bus_get_device(chandle, &child) == 0) {
1276 EXPORT_SYMBOL_GPL(acpi_bus_trim);
1279 static int acpi_bus_scan_fixed(struct acpi_device *root)
1282 struct acpi_device *device = NULL;
1289 * Enumerate all fixed-feature devices.
1291 if (acpi_fadt.pwr_button == 0) {
1292 result = acpi_add_single_object(&device, acpi_root,
1294 ACPI_BUS_TYPE_POWER_BUTTON);
1296 result = acpi_start_single_object(device);
1299 if (acpi_fadt.sleep_button == 0) {
1300 result = acpi_add_single_object(&device, acpi_root,
1302 ACPI_BUS_TYPE_SLEEP_BUTTON);
1304 result = acpi_start_single_object(device);
1311 static inline struct acpi_device * to_acpi_dev(struct device * dev)
1313 return container_of(dev, struct acpi_device, dev);
1317 static int root_suspend(struct acpi_device * acpi_dev, pm_message_t state)
1319 struct acpi_device * dev, * next;
1322 spin_lock(&acpi_device_lock);
1323 list_for_each_entry_safe_reverse(dev, next, &acpi_device_list, g_list) {
1324 if (dev->driver && dev->driver->ops.suspend) {
1325 spin_unlock(&acpi_device_lock);
1326 result = dev->driver->ops.suspend(dev, 0);
1328 printk(KERN_ERR PREFIX "[%s - %s] Suspend failed: %d\n",
1329 acpi_device_name(dev),
1330 acpi_device_bid(dev), result);
1332 spin_lock(&acpi_device_lock);
1335 spin_unlock(&acpi_device_lock);
1340 static int acpi_device_suspend(struct device * dev, pm_message_t state)
1342 struct acpi_device * acpi_dev = to_acpi_dev(dev);
1345 * For now, we should only register 1 generic device -
1346 * the ACPI root device - and from there, we walk the
1347 * tree of ACPI devices to suspend each one using the
1348 * ACPI driver methods.
1350 if (acpi_dev->handle == ACPI_ROOT_OBJECT)
1351 root_suspend(acpi_dev, state);
1357 static int root_resume(struct acpi_device * acpi_dev)
1359 struct acpi_device * dev, * next;
1362 spin_lock(&acpi_device_lock);
1363 list_for_each_entry_safe(dev, next, &acpi_device_list, g_list) {
1364 if (dev->driver && dev->driver->ops.resume) {
1365 spin_unlock(&acpi_device_lock);
1366 result = dev->driver->ops.resume(dev, 0);
1368 printk(KERN_ERR PREFIX "[%s - %s] resume failed: %d\n",
1369 acpi_device_name(dev),
1370 acpi_device_bid(dev), result);
1372 spin_lock(&acpi_device_lock);
1375 spin_unlock(&acpi_device_lock);
1380 static int acpi_device_resume(struct device * dev)
1382 struct acpi_device * acpi_dev = to_acpi_dev(dev);
1385 * For now, we should only register 1 generic device -
1386 * the ACPI root device - and from there, we walk the
1387 * tree of ACPI devices to resume each one using the
1388 * ACPI driver methods.
1390 if (acpi_dev->handle == ACPI_ROOT_OBJECT)
1391 root_resume(acpi_dev);
1396 static struct bus_type acpi_bus_type = {
1398 .suspend = acpi_device_suspend,
1399 .resume = acpi_device_resume,
1404 static int __init acpi_scan_init(void)
1407 struct acpi_bus_ops ops;
1413 result = kset_register(&acpi_namespace_kset);
1415 printk(KERN_ERR PREFIX "kset_register error: %d\n", result);
1417 result = bus_register(&acpi_bus_type);
1419 /* We don't want to quit even if we failed to add suspend/resume */
1420 printk(KERN_ERR PREFIX "Could not register bus type\n");
1424 * Create the root device in the bus's device tree
1426 result = acpi_add_single_object(&acpi_root, NULL, ACPI_ROOT_OBJECT,
1427 ACPI_BUS_TYPE_SYSTEM);
1431 result = acpi_start_single_object(acpi_root);
1435 acpi_root->dev.bus = &acpi_bus_type;
1436 snprintf(acpi_root->dev.bus_id, BUS_ID_SIZE, "%s", acpi_bus_type.name);
1437 result = device_register(&acpi_root->dev);
1439 /* We don't want to quit even if we failed to add suspend/resume */
1440 printk(KERN_ERR PREFIX "Could not register device\n");
1444 * Enumerate devices in the ACPI namespace.
1446 result = acpi_bus_scan_fixed(acpi_root);
1448 memset(&ops, 0, sizeof(ops));
1449 ops.acpi_op_add = 1;
1450 ops.acpi_op_start = 1;
1451 result = acpi_bus_scan(acpi_root, &ops);
1455 acpi_device_unregister(acpi_root, ACPI_BUS_REMOVAL_NORMAL);
1461 subsys_initcall(acpi_scan_init);