2 * Link physical devices with ACPI devices support
4 * Copyright (c) 2005 David Shaohua Li <shaohua.li@intel.com>
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 #define PHYSICAL_NODE_STRING "physical_node"
30 int register_acpi_bus_type(struct acpi_bus_type *type)
34 if (type && type->bus && type->find_device) {
35 down_write(&bus_type_sem);
36 list_add_tail(&type->list, &bus_type_list);
37 up_write(&bus_type_sem);
38 printk(KERN_INFO PREFIX "bus type %s registered\n",
44 EXPORT_SYMBOL_GPL(register_acpi_bus_type);
46 int unregister_acpi_bus_type(struct acpi_bus_type *type)
51 down_write(&bus_type_sem);
52 list_del_init(&type->list);
53 up_write(&bus_type_sem);
54 printk(KERN_INFO PREFIX "ACPI bus type %s unregistered\n",
60 EXPORT_SYMBOL_GPL(unregister_acpi_bus_type);
62 static struct acpi_bus_type *acpi_get_bus_type(struct bus_type *type)
64 struct acpi_bus_type *tmp, *ret = NULL;
66 down_read(&bus_type_sem);
67 list_for_each_entry(tmp, &bus_type_list, list) {
68 if (tmp->bus == type) {
73 up_read(&bus_type_sem);
77 static int acpi_find_bridge_device(struct device *dev, acpi_handle * handle)
79 struct acpi_bus_type *tmp;
82 down_read(&bus_type_sem);
83 list_for_each_entry(tmp, &bus_type_list, list) {
84 if (tmp->find_bridge && !tmp->find_bridge(dev, handle)) {
89 up_read(&bus_type_sem);
93 /* Get device's handler per its address under its parent */
94 struct acpi_find_child {
100 do_acpi_find_child(acpi_handle handle, u32 lvl, void *context, void **rv)
103 struct acpi_device_info *info;
104 struct acpi_find_child *find = context;
106 status = acpi_get_object_info(handle, &info);
107 if (ACPI_SUCCESS(status)) {
108 if ((info->address == find->address)
109 && (info->valid & ACPI_VALID_ADR))
110 find->handle = handle;
116 acpi_handle acpi_get_child(acpi_handle parent, u64 address)
118 struct acpi_find_child find = { NULL, address };
122 acpi_walk_namespace(ACPI_TYPE_DEVICE, parent,
123 1, do_acpi_find_child, NULL, &find, NULL);
127 EXPORT_SYMBOL(acpi_get_child);
129 static int acpi_bind_one(struct device *dev, acpi_handle handle)
131 struct acpi_device *acpi_dev;
133 struct acpi_device_physical_node *physical_node, *pn;
134 char physical_node_name[sizeof(PHYSICAL_NODE_STRING) + 2];
135 int retval = -EINVAL;
137 if (ACPI_HANDLE(dev)) {
139 dev_warn(dev, "ACPI handle is already set\n");
142 handle = ACPI_HANDLE(dev);
149 status = acpi_bus_get_device(handle, &acpi_dev);
150 if (ACPI_FAILURE(status))
153 physical_node = kzalloc(sizeof(*physical_node), GFP_KERNEL);
154 if (!physical_node) {
159 mutex_lock(&acpi_dev->physical_node_lock);
162 list_for_each_entry(pn, &acpi_dev->physical_node_list, node)
163 if (pn->dev == dev) {
164 dev_warn(dev, "Already associated with ACPI node\n");
168 /* allocate physical node id according to physical_node_id_bitmap */
169 physical_node->node_id =
170 find_first_zero_bit(acpi_dev->physical_node_id_bitmap,
171 ACPI_MAX_PHYSICAL_NODE);
172 if (physical_node->node_id >= ACPI_MAX_PHYSICAL_NODE) {
177 set_bit(physical_node->node_id, acpi_dev->physical_node_id_bitmap);
178 physical_node->dev = dev;
179 list_add_tail(&physical_node->node, &acpi_dev->physical_node_list);
180 acpi_dev->physical_node_count++;
182 mutex_unlock(&acpi_dev->physical_node_lock);
184 if (!ACPI_HANDLE(dev))
185 ACPI_HANDLE_SET(dev, acpi_dev->handle);
187 if (!physical_node->node_id)
188 strcpy(physical_node_name, PHYSICAL_NODE_STRING);
190 sprintf(physical_node_name,
191 "physical_node%d", physical_node->node_id);
192 retval = sysfs_create_link(&acpi_dev->dev.kobj, &dev->kobj,
194 retval = sysfs_create_link(&dev->kobj, &acpi_dev->dev.kobj,
197 if (acpi_dev->wakeup.flags.valid)
198 device_set_wakeup_capable(dev, true);
203 ACPI_HANDLE_SET(dev, NULL);
208 mutex_unlock(&acpi_dev->physical_node_lock);
209 kfree(physical_node);
213 static int acpi_unbind_one(struct device *dev)
215 struct acpi_device_physical_node *entry;
216 struct acpi_device *acpi_dev;
218 struct list_head *node, *next;
220 if (!ACPI_HANDLE(dev))
223 status = acpi_bus_get_device(ACPI_HANDLE(dev), &acpi_dev);
224 if (ACPI_FAILURE(status))
227 mutex_lock(&acpi_dev->physical_node_lock);
228 list_for_each_safe(node, next, &acpi_dev->physical_node_list) {
229 char physical_node_name[sizeof(PHYSICAL_NODE_STRING) + 2];
231 entry = list_entry(node, struct acpi_device_physical_node,
233 if (entry->dev != dev)
237 clear_bit(entry->node_id, acpi_dev->physical_node_id_bitmap);
239 acpi_dev->physical_node_count--;
242 strcpy(physical_node_name, PHYSICAL_NODE_STRING);
244 sprintf(physical_node_name,
245 "physical_node%d", entry->node_id);
247 sysfs_remove_link(&acpi_dev->dev.kobj, physical_node_name);
248 sysfs_remove_link(&dev->kobj, "firmware_node");
249 ACPI_HANDLE_SET(dev, NULL);
250 /* acpi_bind_one increase refcnt by one */
254 mutex_unlock(&acpi_dev->physical_node_lock);
259 dev_err(dev, "Oops, 'acpi_handle' corrupt\n");
263 static int acpi_platform_notify(struct device *dev)
265 struct acpi_bus_type *type;
269 ret = acpi_bind_one(dev, NULL);
273 if (!dev->bus || !dev->parent) {
274 /* bridge devices genernally haven't bus or parent */
275 ret = acpi_find_bridge_device(dev, &handle);
278 type = acpi_get_bus_type(dev->bus);
280 DBG("No ACPI bus support for %s\n", dev_name(dev));
284 if ((ret = type->find_device(dev, &handle)) != 0)
285 DBG("Can't get handler for %s\n", dev_name(dev));
288 acpi_bind_one(dev, handle);
293 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
295 acpi_get_name(dev->acpi_handle, ACPI_FULL_PATHNAME, &buffer);
296 DBG("Device %s -> %s\n", dev_name(dev), (char *)buffer.pointer);
297 kfree(buffer.pointer);
299 DBG("Device %s -> No ACPI support\n", dev_name(dev));
305 static int acpi_platform_notify_remove(struct device *dev)
307 acpi_unbind_one(dev);
311 int __init init_acpi_device_notify(void)
313 if (platform_notify || platform_notify_remove) {
314 printk(KERN_ERR PREFIX "Can't use platform_notify\n");
317 platform_notify = acpi_platform_notify;
318 platform_notify_remove = acpi_platform_notify_remove;