ACPI / glue: Add .match() callback to struct acpi_bus_type
[deliverable/linux.git] / drivers / acpi / glue.c
CommitLineData
4e10d12a
DSL
1/*
2 * Link physical devices with ACPI devices support
3 *
4 * Copyright (c) 2005 David Shaohua Li <shaohua.li@intel.com>
5 * Copyright (c) 2005 Intel Corp.
6 *
7 * This file is released under the GPLv2.
8 */
214f2c90 9#include <linux/export.h>
4e10d12a
DSL
10#include <linux/init.h>
11#include <linux/list.h>
12#include <linux/device.h>
5a0e3ad6 13#include <linux/slab.h>
4e10d12a
DSL
14#include <linux/rwsem.h>
15#include <linux/acpi.h>
16
a192a958
LB
17#include "internal.h"
18
4e10d12a
DSL
19#define ACPI_GLUE_DEBUG 0
20#if ACPI_GLUE_DEBUG
23415eb5
JP
21#define DBG(fmt, ...) \
22 printk(KERN_DEBUG PREFIX fmt, ##__VA_ARGS__)
4e10d12a 23#else
23415eb5
JP
24#define DBG(fmt, ...) \
25do { \
26 if (0) \
27 printk(KERN_DEBUG PREFIX fmt, ##__VA_ARGS__); \
28} while (0)
4e10d12a
DSL
29#endif
30static LIST_HEAD(bus_type_list);
31static DECLARE_RWSEM(bus_type_sem);
32
1033f904
LT
33#define PHYSICAL_NODE_STRING "physical_node"
34
4e10d12a
DSL
35int register_acpi_bus_type(struct acpi_bus_type *type)
36{
37 if (acpi_disabled)
38 return -ENODEV;
53540098 39 if (type && type->match && type->find_device) {
4e10d12a
DSL
40 down_write(&bus_type_sem);
41 list_add_tail(&type->list, &bus_type_list);
42 up_write(&bus_type_sem);
53540098 43 printk(KERN_INFO PREFIX "bus type %s registered\n", type->name);
4e10d12a
DSL
44 return 0;
45 }
46 return -ENODEV;
47}
91e4d5a1 48EXPORT_SYMBOL_GPL(register_acpi_bus_type);
4e10d12a 49
4e10d12a
DSL
50int unregister_acpi_bus_type(struct acpi_bus_type *type)
51{
52 if (acpi_disabled)
53 return 0;
54 if (type) {
55 down_write(&bus_type_sem);
56 list_del_init(&type->list);
57 up_write(&bus_type_sem);
53540098
RW
58 printk(KERN_INFO PREFIX "bus type %s unregistered\n",
59 type->name);
4e10d12a
DSL
60 return 0;
61 }
62 return -ENODEV;
63}
91e4d5a1 64EXPORT_SYMBOL_GPL(unregister_acpi_bus_type);
4e10d12a 65
53540098 66static struct acpi_bus_type *acpi_get_bus_type(struct device *dev)
4e10d12a
DSL
67{
68 struct acpi_bus_type *tmp, *ret = NULL;
69
70 down_read(&bus_type_sem);
71 list_for_each_entry(tmp, &bus_type_list, list) {
53540098 72 if (tmp->match(dev)) {
4e10d12a
DSL
73 ret = tmp;
74 break;
75 }
76 }
77 up_read(&bus_type_sem);
78 return ret;
79}
80
81static int acpi_find_bridge_device(struct device *dev, acpi_handle * handle)
82{
83 struct acpi_bus_type *tmp;
84 int ret = -ENODEV;
85
86 down_read(&bus_type_sem);
87 list_for_each_entry(tmp, &bus_type_list, list) {
88 if (tmp->find_bridge && !tmp->find_bridge(dev, handle)) {
89 ret = 0;
90 break;
91 }
92 }
93 up_read(&bus_type_sem);
94 return ret;
95}
96
33f767d7
RW
97static acpi_status do_acpi_find_child(acpi_handle handle, u32 lvl_not_used,
98 void *addr_p, void **ret_p)
4e10d12a 99{
33f767d7 100 unsigned long long addr;
4e10d12a 101 acpi_status status;
33f767d7
RW
102
103 status = acpi_evaluate_integer(handle, METHOD_NAME__ADR, NULL, &addr);
104 if (ACPI_SUCCESS(status) && addr == *((u64 *)addr_p)) {
105 *ret_p = handle;
106 return AE_CTRL_TERMINATE;
4e10d12a
DSL
107 }
108 return AE_OK;
109}
110
439913ff 111acpi_handle acpi_get_child(acpi_handle parent, u64 address)
4e10d12a 112{
33f767d7 113 void *ret = NULL;
4e10d12a
DSL
114
115 if (!parent)
116 return NULL;
4e10d12a 117
33f767d7
RW
118 acpi_walk_namespace(ACPI_TYPE_DEVICE, parent, 1, NULL,
119 do_acpi_find_child, &address, &ret);
120 return (acpi_handle)ret;
121}
4e10d12a
DSL
122EXPORT_SYMBOL(acpi_get_child);
123
4e10d12a
DSL
124static int acpi_bind_one(struct device *dev, acpi_handle handle)
125{
1071695f 126 struct acpi_device *acpi_dev;
4e10d12a 127 acpi_status status;
f3fd0c8a 128 struct acpi_device_physical_node *physical_node, *pn;
1033f904
LT
129 char physical_node_name[sizeof(PHYSICAL_NODE_STRING) + 2];
130 int retval = -EINVAL;
4e10d12a 131
95f8a082 132 if (ACPI_HANDLE(dev)) {
f3fd0c8a
RW
133 if (handle) {
134 dev_warn(dev, "ACPI handle is already set\n");
135 return -EINVAL;
136 } else {
95f8a082 137 handle = ACPI_HANDLE(dev);
f3fd0c8a 138 }
4e10d12a 139 }
f3fd0c8a
RW
140 if (!handle)
141 return -EINVAL;
1033f904 142
4e10d12a 143 get_device(dev);
1033f904
LT
144 status = acpi_bus_get_device(handle, &acpi_dev);
145 if (ACPI_FAILURE(status))
146 goto err;
147
f3fd0c8a 148 physical_node = kzalloc(sizeof(*physical_node), GFP_KERNEL);
1033f904
LT
149 if (!physical_node) {
150 retval = -ENOMEM;
151 goto err;
4e10d12a 152 }
4e10d12a 153
1033f904 154 mutex_lock(&acpi_dev->physical_node_lock);
f3fd0c8a
RW
155
156 /* Sanity check. */
157 list_for_each_entry(pn, &acpi_dev->physical_node_list, node)
158 if (pn->dev == dev) {
159 dev_warn(dev, "Already associated with ACPI node\n");
160 goto err_free;
161 }
162
1033f904
LT
163 /* allocate physical node id according to physical_node_id_bitmap */
164 physical_node->node_id =
165 find_first_zero_bit(acpi_dev->physical_node_id_bitmap,
166 ACPI_MAX_PHYSICAL_NODE);
167 if (physical_node->node_id >= ACPI_MAX_PHYSICAL_NODE) {
168 retval = -ENOSPC;
f3fd0c8a 169 goto err_free;
1071695f
DB
170 }
171
1033f904
LT
172 set_bit(physical_node->node_id, acpi_dev->physical_node_id_bitmap);
173 physical_node->dev = dev;
174 list_add_tail(&physical_node->node, &acpi_dev->physical_node_list);
175 acpi_dev->physical_node_count++;
f3fd0c8a 176
1033f904
LT
177 mutex_unlock(&acpi_dev->physical_node_lock);
178
95f8a082
RW
179 if (!ACPI_HANDLE(dev))
180 ACPI_HANDLE_SET(dev, acpi_dev->handle);
1033f904
LT
181
182 if (!physical_node->node_id)
183 strcpy(physical_node_name, PHYSICAL_NODE_STRING);
184 else
185 sprintf(physical_node_name,
186 "physical_node%d", physical_node->node_id);
187 retval = sysfs_create_link(&acpi_dev->dev.kobj, &dev->kobj,
188 physical_node_name);
189 retval = sysfs_create_link(&dev->kobj, &acpi_dev->dev.kobj,
190 "firmware_node");
191
192 if (acpi_dev->wakeup.flags.valid)
193 device_set_wakeup_capable(dev, true);
194
4e10d12a 195 return 0;
1033f904
LT
196
197 err:
95f8a082 198 ACPI_HANDLE_SET(dev, NULL);
1033f904
LT
199 put_device(dev);
200 return retval;
f3fd0c8a
RW
201
202 err_free:
203 mutex_unlock(&acpi_dev->physical_node_lock);
204 kfree(physical_node);
205 goto err;
4e10d12a
DSL
206}
207
208static int acpi_unbind_one(struct device *dev)
209{
1033f904
LT
210 struct acpi_device_physical_node *entry;
211 struct acpi_device *acpi_dev;
212 acpi_status status;
213 struct list_head *node, *next;
214
95f8a082 215 if (!ACPI_HANDLE(dev))
4e10d12a 216 return 0;
1071695f 217
95f8a082 218 status = acpi_bus_get_device(ACPI_HANDLE(dev), &acpi_dev);
1033f904
LT
219 if (ACPI_FAILURE(status))
220 goto err;
1071695f 221
1033f904
LT
222 mutex_lock(&acpi_dev->physical_node_lock);
223 list_for_each_safe(node, next, &acpi_dev->physical_node_list) {
224 char physical_node_name[sizeof(PHYSICAL_NODE_STRING) + 2];
225
226 entry = list_entry(node, struct acpi_device_physical_node,
227 node);
228 if (entry->dev != dev)
229 continue;
230
231 list_del(node);
232 clear_bit(entry->node_id, acpi_dev->physical_node_id_bitmap);
1071695f 233
1033f904
LT
234 acpi_dev->physical_node_count--;
235
236 if (!entry->node_id)
237 strcpy(physical_node_name, PHYSICAL_NODE_STRING);
238 else
239 sprintf(physical_node_name,
240 "physical_node%d", entry->node_id);
241
242 sysfs_remove_link(&acpi_dev->dev.kobj, physical_node_name);
243 sysfs_remove_link(&dev->kobj, "firmware_node");
95f8a082 244 ACPI_HANDLE_SET(dev, NULL);
4e10d12a
DSL
245 /* acpi_bind_one increase refcnt by one */
246 put_device(dev);
1033f904 247 kfree(entry);
4e10d12a 248 }
1033f904
LT
249 mutex_unlock(&acpi_dev->physical_node_lock);
250
4e10d12a 251 return 0;
1033f904
LT
252
253err:
254 dev_err(dev, "Oops, 'acpi_handle' corrupt\n");
255 return -EINVAL;
4e10d12a
DSL
256}
257
258static int acpi_platform_notify(struct device *dev)
259{
53540098 260 struct acpi_bus_type *type = acpi_get_bus_type(dev);
4e10d12a 261 acpi_handle handle;
11909ca1 262 int ret;
4e10d12a 263
f3fd0c8a 264 ret = acpi_bind_one(dev, NULL);
11909ca1 265 if (ret) {
53540098
RW
266 if (!type) {
267 ret = acpi_find_bridge_device(dev, &handle);
268 if (!ret)
269 ret = acpi_bind_one(dev, handle);
270
11909ca1
RW
271 goto out;
272 }
273
274 ret = type->find_device(dev, &handle);
275 if (ret) {
276 DBG("Unable to get handle for %s\n", dev_name(dev));
277 goto out;
278 }
279 ret = acpi_bind_one(dev, handle);
280 if (ret)
281 goto out;
4e10d12a 282 }
11909ca1
RW
283
284 if (type && type->setup)
285 type->setup(dev);
4e10d12a 286
f3fd0c8a 287 out:
4e10d12a
DSL
288#if ACPI_GLUE_DEBUG
289 if (!ret) {
290 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
291
a412a11d 292 acpi_get_name(ACPI_HANDLE(dev), ACPI_FULL_PATHNAME, &buffer);
db1461ad 293 DBG("Device %s -> %s\n", dev_name(dev), (char *)buffer.pointer);
02438d87 294 kfree(buffer.pointer);
4e10d12a 295 } else
db1461ad 296 DBG("Device %s -> No ACPI support\n", dev_name(dev));
4e10d12a
DSL
297#endif
298
299 return ret;
300}
301
302static int acpi_platform_notify_remove(struct device *dev)
303{
11909ca1
RW
304 struct acpi_bus_type *type;
305
53540098 306 type = acpi_get_bus_type(dev);
11909ca1
RW
307 if (type && type->cleanup)
308 type->cleanup(dev);
309
4e10d12a
DSL
310 acpi_unbind_one(dev);
311 return 0;
312}
313
0e46517d 314int __init init_acpi_device_notify(void)
4e10d12a 315{
4e10d12a
DSL
316 if (platform_notify || platform_notify_remove) {
317 printk(KERN_ERR PREFIX "Can't use platform_notify\n");
318 return 0;
319 }
320 platform_notify = acpi_platform_notify;
321 platform_notify_remove = acpi_platform_notify_remove;
322 return 0;
323}
This page took 0.653016 seconds and 5 git commands to generate.