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->match && 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", type->name);
48 EXPORT_SYMBOL_GPL(register_acpi_bus_type);
50 int unregister_acpi_bus_type(struct acpi_bus_type *type)
55 down_write(&bus_type_sem);
56 list_del_init(&type->list);
57 up_write(&bus_type_sem);
58 printk(KERN_INFO PREFIX "bus type %s unregistered\n",
64 EXPORT_SYMBOL_GPL(unregister_acpi_bus_type);
66 static struct acpi_bus_type *acpi_get_bus_type(struct device *dev)
68 struct acpi_bus_type *tmp, *ret = NULL;
70 down_read(&bus_type_sem);
71 list_for_each_entry(tmp, &bus_type_list, list) {
72 if (tmp->match(dev)) {
77 up_read(&bus_type_sem);
81 static acpi_status acpi_dev_present(acpi_handle handle, u32 lvl_not_used,
82 void *not_used, void **ret_p)
84 struct acpi_device *adev = NULL;
86 acpi_bus_get_device(handle, &adev);
89 return AE_CTRL_TERMINATE;
94 static bool acpi_extra_checks_passed(acpi_handle handle, bool is_bridge)
96 unsigned long long sta;
99 status = acpi_bus_get_status_handle(handle, &sta);
100 if (ACPI_FAILURE(status) || !(sta & ACPI_STA_DEVICE_ENABLED))
106 /* Check if this object has at least one child device. */
107 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, 1,
108 acpi_dev_present, NULL, NULL, &test);
114 struct find_child_context {
121 static acpi_status do_find_child(acpi_handle handle, u32 lvl_not_used,
122 void *data, void **not_used)
124 struct find_child_context *context = data;
125 unsigned long long addr;
128 status = acpi_evaluate_integer(handle, METHOD_NAME__ADR, NULL, &addr);
129 if (ACPI_FAILURE(status) || addr != context->addr)
133 /* This is the first matching object. Save its handle. */
134 context->ret = handle;
138 * There is more than one matching object with the same _ADR value.
139 * That really is unexpected, so we are kind of beyond the scope of the
140 * spec here. We have to choose which one to return, though.
142 * First, check if the previously found object is good enough and return
143 * its handle if so. Second, check the same for the object that we've
146 if (!context->ret_checked) {
147 if (acpi_extra_checks_passed(context->ret, context->is_bridge))
148 return AE_CTRL_TERMINATE;
150 context->ret_checked = true;
152 if (acpi_extra_checks_passed(handle, context->is_bridge)) {
153 context->ret = handle;
154 return AE_CTRL_TERMINATE;
159 acpi_handle acpi_find_child(acpi_handle parent, u64 addr, bool is_bridge)
162 struct find_child_context context = {
164 .is_bridge = is_bridge,
167 acpi_walk_namespace(ACPI_TYPE_DEVICE, parent, 1, do_find_child,
168 NULL, &context, NULL);
173 EXPORT_SYMBOL_GPL(acpi_find_child);
175 static int acpi_bind_one(struct device *dev, acpi_handle handle)
177 struct acpi_device *acpi_dev;
179 struct acpi_device_physical_node *physical_node, *pn;
180 char physical_node_name[sizeof(PHYSICAL_NODE_STRING) + 2];
181 int retval = -EINVAL;
183 if (ACPI_HANDLE(dev)) {
185 dev_warn(dev, "ACPI handle is already set\n");
188 handle = ACPI_HANDLE(dev);
195 status = acpi_bus_get_device(handle, &acpi_dev);
196 if (ACPI_FAILURE(status))
199 physical_node = kzalloc(sizeof(*physical_node), GFP_KERNEL);
200 if (!physical_node) {
205 mutex_lock(&acpi_dev->physical_node_lock);
208 list_for_each_entry(pn, &acpi_dev->physical_node_list, node)
209 if (pn->dev == dev) {
210 dev_warn(dev, "Already associated with ACPI node\n");
214 /* allocate physical node id according to physical_node_id_bitmap */
215 physical_node->node_id =
216 find_first_zero_bit(acpi_dev->physical_node_id_bitmap,
217 ACPI_MAX_PHYSICAL_NODE);
218 if (physical_node->node_id >= ACPI_MAX_PHYSICAL_NODE) {
223 set_bit(physical_node->node_id, acpi_dev->physical_node_id_bitmap);
224 physical_node->dev = dev;
225 list_add_tail(&physical_node->node, &acpi_dev->physical_node_list);
226 acpi_dev->physical_node_count++;
228 mutex_unlock(&acpi_dev->physical_node_lock);
230 if (!ACPI_HANDLE(dev))
231 ACPI_HANDLE_SET(dev, acpi_dev->handle);
233 if (!physical_node->node_id)
234 strcpy(physical_node_name, PHYSICAL_NODE_STRING);
236 sprintf(physical_node_name,
237 "physical_node%d", physical_node->node_id);
238 retval = sysfs_create_link(&acpi_dev->dev.kobj, &dev->kobj,
240 retval = sysfs_create_link(&dev->kobj, &acpi_dev->dev.kobj,
243 if (acpi_dev->wakeup.flags.valid)
244 device_set_wakeup_capable(dev, true);
249 ACPI_HANDLE_SET(dev, NULL);
254 mutex_unlock(&acpi_dev->physical_node_lock);
255 kfree(physical_node);
259 static int acpi_unbind_one(struct device *dev)
261 struct acpi_device_physical_node *entry;
262 struct acpi_device *acpi_dev;
264 struct list_head *node, *next;
266 if (!ACPI_HANDLE(dev))
269 status = acpi_bus_get_device(ACPI_HANDLE(dev), &acpi_dev);
270 if (ACPI_FAILURE(status))
273 mutex_lock(&acpi_dev->physical_node_lock);
274 list_for_each_safe(node, next, &acpi_dev->physical_node_list) {
275 char physical_node_name[sizeof(PHYSICAL_NODE_STRING) + 2];
277 entry = list_entry(node, struct acpi_device_physical_node,
279 if (entry->dev != dev)
283 clear_bit(entry->node_id, acpi_dev->physical_node_id_bitmap);
285 acpi_dev->physical_node_count--;
288 strcpy(physical_node_name, PHYSICAL_NODE_STRING);
290 sprintf(physical_node_name,
291 "physical_node%d", entry->node_id);
293 sysfs_remove_link(&acpi_dev->dev.kobj, physical_node_name);
294 sysfs_remove_link(&dev->kobj, "firmware_node");
295 ACPI_HANDLE_SET(dev, NULL);
296 /* acpi_bind_one increase refcnt by one */
300 mutex_unlock(&acpi_dev->physical_node_lock);
305 dev_err(dev, "Oops, 'acpi_handle' corrupt\n");
309 static int acpi_platform_notify(struct device *dev)
311 struct acpi_bus_type *type = acpi_get_bus_type(dev);
315 ret = acpi_bind_one(dev, NULL);
317 ret = type->find_device(dev, &handle);
319 DBG("Unable to get handle for %s\n", dev_name(dev));
322 ret = acpi_bind_one(dev, handle);
327 if (type && type->setup)
333 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
335 acpi_get_name(ACPI_HANDLE(dev), ACPI_FULL_PATHNAME, &buffer);
336 DBG("Device %s -> %s\n", dev_name(dev), (char *)buffer.pointer);
337 kfree(buffer.pointer);
339 DBG("Device %s -> No ACPI support\n", dev_name(dev));
345 static int acpi_platform_notify_remove(struct device *dev)
347 struct acpi_bus_type *type;
349 type = acpi_get_bus_type(dev);
350 if (type && type->cleanup)
353 acpi_unbind_one(dev);
357 int __init init_acpi_device_notify(void)
359 if (platform_notify || platform_notify_remove) {
360 printk(KERN_ERR PREFIX "Can't use platform_notify\n");
363 platform_notify = acpi_platform_notify;
364 platform_notify_remove = acpi_platform_notify_remove;