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"
34 #define PHYSICAL_NODE_NAME_SIZE (sizeof(PHYSICAL_NODE_STRING) + 10)
36 int register_acpi_bus_type(struct acpi_bus_type *type)
40 if (type && type->match && type->find_device) {
41 down_write(&bus_type_sem);
42 list_add_tail(&type->list, &bus_type_list);
43 up_write(&bus_type_sem);
44 printk(KERN_INFO PREFIX "bus type %s registered\n", type->name);
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 "bus type %s unregistered\n",
65 EXPORT_SYMBOL_GPL(unregister_acpi_bus_type);
67 static struct acpi_bus_type *acpi_get_bus_type(struct device *dev)
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->match(dev)) {
78 up_read(&bus_type_sem);
82 static acpi_status do_acpi_find_child(acpi_handle handle, u32 lvl_not_used,
83 void *addr_p, void **ret_p)
85 unsigned long long addr, sta;
88 status = acpi_evaluate_integer(handle, METHOD_NAME__ADR, NULL, &addr);
89 if (ACPI_SUCCESS(status) && addr == *((u64 *)addr_p)) {
91 status = acpi_bus_get_status_handle(handle, &sta);
92 if (ACPI_SUCCESS(status) && (sta & ACPI_STA_DEVICE_ENABLED))
93 return AE_CTRL_TERMINATE;
98 acpi_handle acpi_get_child(acpi_handle parent, u64 address)
105 acpi_walk_namespace(ACPI_TYPE_DEVICE, parent, 1, NULL,
106 do_acpi_find_child, &address, &ret);
107 return (acpi_handle)ret;
109 EXPORT_SYMBOL(acpi_get_child);
111 int acpi_bind_one(struct device *dev, acpi_handle handle)
113 struct acpi_device *acpi_dev;
115 struct acpi_device_physical_node *physical_node, *pn;
116 char physical_node_name[PHYSICAL_NODE_NAME_SIZE];
117 struct list_head *physnode_list;
118 unsigned int node_id;
119 int retval = -EINVAL;
121 if (ACPI_HANDLE(dev)) {
123 dev_warn(dev, "ACPI handle is already set\n");
126 handle = ACPI_HANDLE(dev);
133 status = acpi_bus_get_device(handle, &acpi_dev);
134 if (ACPI_FAILURE(status))
137 physical_node = kzalloc(sizeof(*physical_node), GFP_KERNEL);
138 if (!physical_node) {
143 mutex_lock(&acpi_dev->physical_node_lock);
146 * Keep the list sorted by node_id so that the IDs of removed nodes can
147 * be recycled easily.
149 physnode_list = &acpi_dev->physical_node_list;
151 list_for_each_entry(pn, &acpi_dev->physical_node_list, node) {
153 if (pn->dev == dev) {
154 dev_warn(dev, "Already associated with ACPI node\n");
157 if (pn->node_id == node_id) {
158 physnode_list = &pn->node;
163 physical_node->node_id = node_id;
164 physical_node->dev = dev;
165 list_add(&physical_node->node, physnode_list);
166 acpi_dev->physical_node_count++;
168 mutex_unlock(&acpi_dev->physical_node_lock);
170 if (!ACPI_HANDLE(dev))
171 ACPI_HANDLE_SET(dev, acpi_dev->handle);
173 if (!physical_node->node_id)
174 strcpy(physical_node_name, PHYSICAL_NODE_STRING);
176 sprintf(physical_node_name,
177 "physical_node%d", physical_node->node_id);
178 retval = sysfs_create_link(&acpi_dev->dev.kobj, &dev->kobj,
180 retval = sysfs_create_link(&dev->kobj, &acpi_dev->dev.kobj,
183 if (acpi_dev->wakeup.flags.valid)
184 device_set_wakeup_capable(dev, true);
189 ACPI_HANDLE_SET(dev, NULL);
194 mutex_unlock(&acpi_dev->physical_node_lock);
195 kfree(physical_node);
198 EXPORT_SYMBOL_GPL(acpi_bind_one);
200 int acpi_unbind_one(struct device *dev)
202 struct acpi_device_physical_node *entry;
203 struct acpi_device *acpi_dev;
205 struct list_head *node, *next;
207 if (!ACPI_HANDLE(dev))
210 status = acpi_bus_get_device(ACPI_HANDLE(dev), &acpi_dev);
211 if (ACPI_FAILURE(status))
214 mutex_lock(&acpi_dev->physical_node_lock);
215 list_for_each_safe(node, next, &acpi_dev->physical_node_list) {
216 char physical_node_name[PHYSICAL_NODE_NAME_SIZE];
218 entry = list_entry(node, struct acpi_device_physical_node,
220 if (entry->dev != dev)
225 acpi_dev->physical_node_count--;
228 strcpy(physical_node_name, PHYSICAL_NODE_STRING);
230 sprintf(physical_node_name,
231 "physical_node%d", entry->node_id);
233 sysfs_remove_link(&acpi_dev->dev.kobj, physical_node_name);
234 sysfs_remove_link(&dev->kobj, "firmware_node");
235 ACPI_HANDLE_SET(dev, NULL);
236 /* acpi_bind_one increase refcnt by one */
240 mutex_unlock(&acpi_dev->physical_node_lock);
245 dev_err(dev, "Oops, 'acpi_handle' corrupt\n");
248 EXPORT_SYMBOL_GPL(acpi_unbind_one);
250 static int acpi_platform_notify(struct device *dev)
252 struct acpi_bus_type *type = acpi_get_bus_type(dev);
256 ret = acpi_bind_one(dev, NULL);
258 ret = type->find_device(dev, &handle);
260 DBG("Unable to get handle for %s\n", dev_name(dev));
263 ret = acpi_bind_one(dev, handle);
268 if (type && type->setup)
274 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
276 acpi_get_name(ACPI_HANDLE(dev), ACPI_FULL_PATHNAME, &buffer);
277 DBG("Device %s -> %s\n", dev_name(dev), (char *)buffer.pointer);
278 kfree(buffer.pointer);
280 DBG("Device %s -> No ACPI support\n", dev_name(dev));
286 static int acpi_platform_notify_remove(struct device *dev)
288 struct acpi_bus_type *type;
290 type = acpi_get_bus_type(dev);
291 if (type && type->cleanup)
294 acpi_unbind_one(dev);
298 int __init init_acpi_device_notify(void)
300 if (platform_notify || platform_notify_remove) {
301 printk(KERN_ERR PREFIX "Can't use platform_notify\n");
304 platform_notify = acpi_platform_notify;
305 platform_notify_remove = acpi_platform_notify_remove;