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(fmt, ...) \
22 printk(KERN_DEBUG PREFIX fmt, ##__VA_ARGS__)
24 #define DBG(fmt, ...) \
27 printk(KERN_DEBUG PREFIX fmt, ##__VA_ARGS__); \
30 static LIST_HEAD(bus_type_list);
31 static DECLARE_RWSEM(bus_type_sem);
33 #define PHYSICAL_NODE_STRING "physical_node"
35 int register_acpi_bus_type(struct acpi_bus_type *type)
39 if (type && type->bus && type->find_device) {
40 down_write(&bus_type_sem);
41 list_add_tail(&type->list, &bus_type_list);
42 up_write(&bus_type_sem);
43 printk(KERN_INFO PREFIX "bus type %s registered\n",
49 EXPORT_SYMBOL_GPL(register_acpi_bus_type);
51 int unregister_acpi_bus_type(struct acpi_bus_type *type)
56 down_write(&bus_type_sem);
57 list_del_init(&type->list);
58 up_write(&bus_type_sem);
59 printk(KERN_INFO PREFIX "ACPI bus type %s unregistered\n",
65 EXPORT_SYMBOL_GPL(unregister_acpi_bus_type);
67 static struct acpi_bus_type *acpi_get_bus_type(struct bus_type *type)
69 struct acpi_bus_type *tmp, *ret = NULL;
71 down_read(&bus_type_sem);
72 list_for_each_entry(tmp, &bus_type_list, list) {
73 if (tmp->bus == type) {
78 up_read(&bus_type_sem);
82 static int acpi_find_bridge_device(struct device *dev, acpi_handle * handle)
84 struct acpi_bus_type *tmp;
87 down_read(&bus_type_sem);
88 list_for_each_entry(tmp, &bus_type_list, list) {
89 if (tmp->find_bridge && !tmp->find_bridge(dev, handle)) {
94 up_read(&bus_type_sem);
98 /* Get device's handler per its address under its parent */
99 struct acpi_find_child {
105 do_acpi_find_child(acpi_handle handle, u32 lvl, void *context, void **rv)
108 struct acpi_device_info *info;
109 struct acpi_find_child *find = context;
111 status = acpi_get_object_info(handle, &info);
112 if (ACPI_SUCCESS(status)) {
113 if ((info->address == find->address)
114 && (info->valid & ACPI_VALID_ADR))
115 find->handle = handle;
121 acpi_handle acpi_get_child(acpi_handle parent, u64 address)
123 struct acpi_find_child find = { NULL, address };
127 acpi_walk_namespace(ACPI_TYPE_DEVICE, parent,
128 1, do_acpi_find_child, NULL, &find, NULL);
132 EXPORT_SYMBOL(acpi_get_child);
134 static int acpi_bind_one(struct device *dev, acpi_handle handle)
136 struct acpi_device *acpi_dev;
138 struct acpi_device_physical_node *physical_node, *pn;
139 char physical_node_name[sizeof(PHYSICAL_NODE_STRING) + 2];
140 int retval = -EINVAL;
142 if (ACPI_HANDLE(dev)) {
144 dev_warn(dev, "ACPI handle is already set\n");
147 handle = ACPI_HANDLE(dev);
154 status = acpi_bus_get_device(handle, &acpi_dev);
155 if (ACPI_FAILURE(status))
158 physical_node = kzalloc(sizeof(*physical_node), GFP_KERNEL);
159 if (!physical_node) {
164 mutex_lock(&acpi_dev->physical_node_lock);
167 list_for_each_entry(pn, &acpi_dev->physical_node_list, node)
168 if (pn->dev == dev) {
169 dev_warn(dev, "Already associated with ACPI node\n");
173 /* allocate physical node id according to physical_node_id_bitmap */
174 physical_node->node_id =
175 find_first_zero_bit(acpi_dev->physical_node_id_bitmap,
176 ACPI_MAX_PHYSICAL_NODE);
177 if (physical_node->node_id >= ACPI_MAX_PHYSICAL_NODE) {
182 set_bit(physical_node->node_id, acpi_dev->physical_node_id_bitmap);
183 physical_node->dev = dev;
184 list_add_tail(&physical_node->node, &acpi_dev->physical_node_list);
185 acpi_dev->physical_node_count++;
187 mutex_unlock(&acpi_dev->physical_node_lock);
189 if (!ACPI_HANDLE(dev))
190 ACPI_HANDLE_SET(dev, acpi_dev->handle);
192 if (!physical_node->node_id)
193 strcpy(physical_node_name, PHYSICAL_NODE_STRING);
195 sprintf(physical_node_name,
196 "physical_node%d", physical_node->node_id);
197 retval = sysfs_create_link(&acpi_dev->dev.kobj, &dev->kobj,
199 retval = sysfs_create_link(&dev->kobj, &acpi_dev->dev.kobj,
202 if (acpi_dev->wakeup.flags.valid)
203 device_set_wakeup_capable(dev, true);
208 ACPI_HANDLE_SET(dev, NULL);
213 mutex_unlock(&acpi_dev->physical_node_lock);
214 kfree(physical_node);
218 static int acpi_unbind_one(struct device *dev)
220 struct acpi_device_physical_node *entry;
221 struct acpi_device *acpi_dev;
223 struct list_head *node, *next;
225 if (!ACPI_HANDLE(dev))
228 status = acpi_bus_get_device(ACPI_HANDLE(dev), &acpi_dev);
229 if (ACPI_FAILURE(status))
232 mutex_lock(&acpi_dev->physical_node_lock);
233 list_for_each_safe(node, next, &acpi_dev->physical_node_list) {
234 char physical_node_name[sizeof(PHYSICAL_NODE_STRING) + 2];
236 entry = list_entry(node, struct acpi_device_physical_node,
238 if (entry->dev != dev)
242 clear_bit(entry->node_id, acpi_dev->physical_node_id_bitmap);
244 acpi_dev->physical_node_count--;
247 strcpy(physical_node_name, PHYSICAL_NODE_STRING);
249 sprintf(physical_node_name,
250 "physical_node%d", entry->node_id);
252 sysfs_remove_link(&acpi_dev->dev.kobj, physical_node_name);
253 sysfs_remove_link(&dev->kobj, "firmware_node");
254 ACPI_HANDLE_SET(dev, NULL);
255 /* acpi_bind_one increase refcnt by one */
259 mutex_unlock(&acpi_dev->physical_node_lock);
264 dev_err(dev, "Oops, 'acpi_handle' corrupt\n");
268 static int acpi_platform_notify(struct device *dev)
270 struct acpi_bus_type *type;
274 ret = acpi_bind_one(dev, NULL);
278 if (!dev->bus || !dev->parent) {
279 /* bridge devices genernally haven't bus or parent */
280 ret = acpi_find_bridge_device(dev, &handle);
283 type = acpi_get_bus_type(dev->bus);
285 DBG("No ACPI bus support for %s\n", dev_name(dev));
289 if ((ret = type->find_device(dev, &handle)) != 0)
290 DBG("Can't get handler for %s\n", dev_name(dev));
293 acpi_bind_one(dev, handle);
298 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
300 acpi_get_name(ACPI_HANDLE(dev), ACPI_FULL_PATHNAME, &buffer);
301 DBG("Device %s -> %s\n", dev_name(dev), (char *)buffer.pointer);
302 kfree(buffer.pointer);
304 DBG("Device %s -> No ACPI support\n", dev_name(dev));
310 static int acpi_platform_notify_remove(struct device *dev)
312 acpi_unbind_one(dev);
316 int __init init_acpi_device_notify(void)
318 if (platform_notify || platform_notify_remove) {
319 printk(KERN_ERR PREFIX "Can't use platform_notify\n");
322 platform_notify = acpi_platform_notify;
323 platform_notify_remove = acpi_platform_notify_remove;