2 * Link physical devices with ACPI devices support
5 * Copyright (c) 2005 Intel Corp.
7 * This file is released under the GPLv2.
9 #include <linux/export.h>
10 #include <linux/init.h>
11 #include <linux/list.h>
12 #include <linux/device.h>
13 #include <linux/slab.h>
14 #include <linux/rwsem.h>
15 #include <linux/acpi.h>
19 #define ACPI_GLUE_DEBUG 0
21 #define DBG(x...) printk(PREFIX x)
23 #define DBG(x...) do { } while(0)
25 static LIST_HEAD(bus_type_list);
26 static DECLARE_RWSEM(bus_type_sem);
28 int register_acpi_bus_type(struct acpi_bus_type *type)
32 if (type && type->bus && type->find_device) {
33 down_write(&bus_type_sem);
34 list_add_tail(&type->list, &bus_type_list);
35 up_write(&bus_type_sem);
36 printk(KERN_INFO PREFIX "bus type %s registered\n",
43 int unregister_acpi_bus_type(struct acpi_bus_type *type)
48 down_write(&bus_type_sem);
49 list_del_init(&type->list);
50 up_write(&bus_type_sem);
51 printk(KERN_INFO PREFIX "ACPI bus type %s unregistered\n",
58 static struct acpi_bus_type *acpi_get_bus_type(struct bus_type *type)
60 struct acpi_bus_type *tmp, *ret = NULL;
62 down_read(&bus_type_sem);
63 list_for_each_entry(tmp, &bus_type_list, list) {
64 if (tmp->bus == type) {
69 up_read(&bus_type_sem);
73 static int acpi_find_bridge_device(struct device *dev, acpi_handle * handle)
75 struct acpi_bus_type *tmp;
78 down_read(&bus_type_sem);
79 list_for_each_entry(tmp, &bus_type_list, list) {
80 if (tmp->find_bridge && !tmp->find_bridge(dev, handle)) {
85 up_read(&bus_type_sem);
89 /* Get device's handler per its address under its parent */
90 struct acpi_find_child {
96 do_acpi_find_child(acpi_handle handle, u32 lvl, void *context, void **rv)
99 struct acpi_device_info *info;
100 struct acpi_find_child *find = context;
102 status = acpi_get_object_info(handle, &info);
103 if (ACPI_SUCCESS(status)) {
104 if ((info->address == find->address)
105 && (info->valid & ACPI_VALID_ADR))
106 find->handle = handle;
112 acpi_handle acpi_get_child(acpi_handle parent, u64 address)
114 struct acpi_find_child find = { NULL, address };
118 acpi_walk_namespace(ACPI_TYPE_DEVICE, parent,
119 1, do_acpi_find_child, NULL, &find, NULL);
123 EXPORT_SYMBOL(acpi_get_child);
125 /* Link ACPI devices with physical devices */
126 static void acpi_glue_data_handler(acpi_handle handle,
129 /* we provide an empty handler */
132 /* Note: a success call will increase reference count by one */
133 struct device *acpi_get_physical_device(acpi_handle handle)
138 status = acpi_get_data(handle, acpi_glue_data_handler, (void **)&dev);
139 if (ACPI_SUCCESS(status))
140 return get_device(dev);
144 EXPORT_SYMBOL(acpi_get_physical_device);
146 static int acpi_bind_one(struct device *dev, acpi_handle handle)
148 struct acpi_device *acpi_dev;
151 if (dev->archdata.acpi_handle) {
152 dev_warn(dev, "Drivers changed 'acpi_handle'\n");
156 status = acpi_attach_data(handle, acpi_glue_data_handler, dev);
157 if (ACPI_FAILURE(status)) {
161 dev->archdata.acpi_handle = handle;
163 status = acpi_bus_get_device(handle, &acpi_dev);
164 if (!ACPI_FAILURE(status)) {
167 ret = sysfs_create_link(&dev->kobj, &acpi_dev->dev.kobj,
169 ret = sysfs_create_link(&acpi_dev->dev.kobj, &dev->kobj,
171 if (acpi_dev->wakeup.flags.valid)
172 device_set_wakeup_capable(dev, true);
178 static int acpi_unbind_one(struct device *dev)
180 if (!dev->archdata.acpi_handle)
182 if (dev == acpi_get_physical_device(dev->archdata.acpi_handle)) {
183 struct acpi_device *acpi_dev;
185 /* acpi_get_physical_device increase refcnt by one */
188 if (!acpi_bus_get_device(dev->archdata.acpi_handle,
190 sysfs_remove_link(&dev->kobj, "firmware_node");
191 sysfs_remove_link(&acpi_dev->dev.kobj, "physical_node");
194 acpi_detach_data(dev->archdata.acpi_handle,
195 acpi_glue_data_handler);
196 dev->archdata.acpi_handle = NULL;
197 /* acpi_bind_one increase refcnt by one */
200 dev_err(dev, "Oops, 'acpi_handle' corrupt\n");
205 static int acpi_platform_notify(struct device *dev)
207 struct acpi_bus_type *type;
211 if (!dev->bus || !dev->parent) {
212 /* bridge devices genernally haven't bus or parent */
213 ret = acpi_find_bridge_device(dev, &handle);
216 type = acpi_get_bus_type(dev->bus);
218 DBG("No ACPI bus support for %s\n", dev_name(dev));
222 if ((ret = type->find_device(dev, &handle)) != 0)
223 DBG("Can't get handler for %s\n", dev_name(dev));
226 acpi_bind_one(dev, handle);
230 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
232 acpi_get_name(dev->archdata.acpi_handle,
233 ACPI_FULL_PATHNAME, &buffer);
234 DBG("Device %s -> %s\n", dev_name(dev), (char *)buffer.pointer);
235 kfree(buffer.pointer);
237 DBG("Device %s -> No ACPI support\n", dev_name(dev));
243 static int acpi_platform_notify_remove(struct device *dev)
245 acpi_unbind_one(dev);
249 int __init init_acpi_device_notify(void)
251 if (platform_notify || platform_notify_remove) {
252 printk(KERN_ERR PREFIX "Can't use platform_notify\n");
255 platform_notify = acpi_platform_notify;
256 platform_notify_remove = acpi_platform_notify_remove;