ACPI / bind: Pass struct acpi_device pointer to acpi_bind_one()
[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 33#define PHYSICAL_NODE_STRING "physical_node"
007ccfcf 34#define PHYSICAL_NODE_NAME_SIZE (sizeof(PHYSICAL_NODE_STRING) + 10)
1033f904 35
4e10d12a
DSL
36int register_acpi_bus_type(struct acpi_bus_type *type)
37{
38 if (acpi_disabled)
39 return -ENODEV;
e3f02c52 40 if (type && type->match && type->find_companion) {
4e10d12a
DSL
41 down_write(&bus_type_sem);
42 list_add_tail(&type->list, &bus_type_list);
43 up_write(&bus_type_sem);
53540098 44 printk(KERN_INFO PREFIX "bus type %s registered\n", type->name);
4e10d12a
DSL
45 return 0;
46 }
47 return -ENODEV;
48}
91e4d5a1 49EXPORT_SYMBOL_GPL(register_acpi_bus_type);
4e10d12a 50
4e10d12a
DSL
51int unregister_acpi_bus_type(struct acpi_bus_type *type)
52{
53 if (acpi_disabled)
54 return 0;
55 if (type) {
56 down_write(&bus_type_sem);
57 list_del_init(&type->list);
58 up_write(&bus_type_sem);
53540098
RW
59 printk(KERN_INFO PREFIX "bus type %s unregistered\n",
60 type->name);
4e10d12a
DSL
61 return 0;
62 }
63 return -ENODEV;
64}
91e4d5a1 65EXPORT_SYMBOL_GPL(unregister_acpi_bus_type);
4e10d12a 66
53540098 67static struct acpi_bus_type *acpi_get_bus_type(struct device *dev)
4e10d12a
DSL
68{
69 struct acpi_bus_type *tmp, *ret = NULL;
70
71 down_read(&bus_type_sem);
72 list_for_each_entry(tmp, &bus_type_list, list) {
53540098 73 if (tmp->match(dev)) {
4e10d12a
DSL
74 ret = tmp;
75 break;
76 }
77 }
78 up_read(&bus_type_sem);
79 return ret;
80}
81
11b88ee2
RW
82#define FIND_CHILD_MIN_SCORE 1
83#define FIND_CHILD_MAX_SCORE 2
84
d9fef0c4 85static int find_child_checks(struct acpi_device *adev, bool check_children)
60f75b8e 86{
11b88ee2 87 bool sta_present = true;
60f75b8e
RW
88 unsigned long long sta;
89 acpi_status status;
90
d9fef0c4 91 status = acpi_evaluate_integer(adev->handle, "_STA", NULL, &sta);
11b88ee2
RW
92 if (status == AE_NOT_FOUND)
93 sta_present = false;
94 else if (ACPI_FAILURE(status) || !(sta & ACPI_STA_DEVICE_ENABLED))
95 return -ENODEV;
60f75b8e 96
d9fef0c4
RW
97 if (check_children && list_empty(&adev->children))
98 return -ENODEV;
60f75b8e 99
11b88ee2 100 return sta_present ? FIND_CHILD_MAX_SCORE : FIND_CHILD_MIN_SCORE;
60f75b8e
RW
101}
102
d9fef0c4
RW
103struct acpi_device *acpi_find_child_device(struct acpi_device *parent,
104 u64 address, bool check_children)
60f75b8e 105{
d9fef0c4
RW
106 struct acpi_device *adev, *ret = NULL;
107 int ret_score = 0;
108
5ce79d20
RW
109 if (!parent)
110 return NULL;
111
d9fef0c4
RW
112 list_for_each_entry(adev, &parent->children, node) {
113 unsigned long long addr;
114 acpi_status status;
115 int score;
116
117 status = acpi_evaluate_integer(adev->handle, METHOD_NAME__ADR,
118 NULL, &addr);
119 if (ACPI_FAILURE(status) || addr != address)
120 continue;
121
122 if (!ret) {
123 /* This is the first matching object. Save it. */
124 ret = adev;
125 continue;
126 }
127 /*
128 * There is more than one matching device object with the same
129 * _ADR value. That really is unexpected, so we are kind of
130 * beyond the scope of the spec here. We have to choose which
131 * one to return, though.
132 *
133 * First, check if the previously found object is good enough
134 * and return it if so. Second, do the same for the object that
135 * we've just found.
136 */
137 if (!ret_score) {
138 ret_score = find_child_checks(ret, check_children);
139 if (ret_score == FIND_CHILD_MAX_SCORE)
140 return ret;
141 }
142 score = find_child_checks(adev, check_children);
143 if (score == FIND_CHILD_MAX_SCORE) {
144 return adev;
145 } else if (score > ret_score) {
146 ret = adev;
147 ret_score = score;
148 }
4e10d12a 149 }
d9fef0c4 150 return ret;
4e10d12a 151}
9c5ad36d 152EXPORT_SYMBOL_GPL(acpi_find_child_device);
4e10d12a 153
11dcc75d 154acpi_handle acpi_get_child(acpi_handle handle, u64 addr)
4e10d12a 155{
d9fef0c4
RW
156 struct acpi_device *adev;
157
158 if (!handle || acpi_bus_get_device(handle, &adev))
159 return NULL;
160
11dcc75d 161 adev = acpi_find_child_device(adev, addr, false);
d9fef0c4 162 return adev ? adev->handle : NULL;
33f767d7 163}
11dcc75d 164EXPORT_SYMBOL_GPL(acpi_get_child);
4e10d12a 165
bdbdbf91
RW
166static void acpi_physnode_link_name(char *buf, unsigned int node_id)
167{
168 if (node_id > 0)
169 snprintf(buf, PHYSICAL_NODE_NAME_SIZE,
170 PHYSICAL_NODE_STRING "%u", node_id);
171 else
172 strcpy(buf, PHYSICAL_NODE_STRING);
173}
174
24dee1fc 175int acpi_bind_one(struct device *dev, struct acpi_device *acpi_dev)
4e10d12a 176{
f3fd0c8a 177 struct acpi_device_physical_node *physical_node, *pn;
007ccfcf
RW
178 char physical_node_name[PHYSICAL_NODE_NAME_SIZE];
179 struct list_head *physnode_list;
180 unsigned int node_id;
1033f904 181 int retval = -EINVAL;
4e10d12a 182
7b199811 183 if (ACPI_COMPANION(dev)) {
24dee1fc 184 if (acpi_dev) {
7b199811 185 dev_warn(dev, "ACPI companion already set\n");
f3fd0c8a
RW
186 return -EINVAL;
187 } else {
7b199811 188 acpi_dev = ACPI_COMPANION(dev);
f3fd0c8a 189 }
4e10d12a 190 }
7b199811 191 if (!acpi_dev)
f3fd0c8a 192 return -EINVAL;
1033f904 193
a104b4d4 194 get_device(&acpi_dev->dev);
4e10d12a 195 get_device(dev);
f3fd0c8a 196 physical_node = kzalloc(sizeof(*physical_node), GFP_KERNEL);
1033f904
LT
197 if (!physical_node) {
198 retval = -ENOMEM;
199 goto err;
4e10d12a 200 }
4e10d12a 201
1033f904 202 mutex_lock(&acpi_dev->physical_node_lock);
f3fd0c8a 203
007ccfcf
RW
204 /*
205 * Keep the list sorted by node_id so that the IDs of removed nodes can
206 * be recycled easily.
207 */
208 physnode_list = &acpi_dev->physical_node_list;
209 node_id = 0;
210 list_for_each_entry(pn, &acpi_dev->physical_node_list, node) {
211 /* Sanity check. */
f3fd0c8a 212 if (pn->dev == dev) {
3342c753
RW
213 mutex_unlock(&acpi_dev->physical_node_lock);
214
f3fd0c8a 215 dev_warn(dev, "Already associated with ACPI node\n");
3342c753 216 kfree(physical_node);
7b199811 217 if (ACPI_COMPANION(dev) != acpi_dev)
3342c753 218 goto err;
3fe444ad 219
3342c753 220 put_device(dev);
a104b4d4 221 put_device(&acpi_dev->dev);
3342c753 222 return 0;
f3fd0c8a 223 }
007ccfcf
RW
224 if (pn->node_id == node_id) {
225 physnode_list = &pn->node;
226 node_id++;
227 }
1071695f
DB
228 }
229
007ccfcf 230 physical_node->node_id = node_id;
1033f904 231 physical_node->dev = dev;
007ccfcf 232 list_add(&physical_node->node, physnode_list);
1033f904 233 acpi_dev->physical_node_count++;
f3fd0c8a 234
7b199811
RW
235 if (!ACPI_COMPANION(dev))
236 ACPI_COMPANION_SET(dev, acpi_dev);
1033f904 237
bdbdbf91 238 acpi_physnode_link_name(physical_node_name, node_id);
1033f904 239 retval = sysfs_create_link(&acpi_dev->dev.kobj, &dev->kobj,
f501b6ec 240 physical_node_name);
464c1147
RW
241 if (retval)
242 dev_err(&acpi_dev->dev, "Failed to create link %s (%d)\n",
243 physical_node_name, retval);
244
1033f904 245 retval = sysfs_create_link(&dev->kobj, &acpi_dev->dev.kobj,
f501b6ec 246 "firmware_node");
464c1147
RW
247 if (retval)
248 dev_err(dev, "Failed to create link firmware_node (%d)\n",
249 retval);
1033f904 250
40055206
RW
251 mutex_unlock(&acpi_dev->physical_node_lock);
252
1033f904
LT
253 if (acpi_dev->wakeup.flags.valid)
254 device_set_wakeup_capable(dev, true);
255
4e10d12a 256 return 0;
1033f904
LT
257
258 err:
7b199811 259 ACPI_COMPANION_SET(dev, NULL);
1033f904 260 put_device(dev);
a104b4d4 261 put_device(&acpi_dev->dev);
1033f904 262 return retval;
4e10d12a 263}
ac212b69 264EXPORT_SYMBOL_GPL(acpi_bind_one);
4e10d12a 265
ac212b69 266int acpi_unbind_one(struct device *dev)
4e10d12a 267{
7b199811 268 struct acpi_device *acpi_dev = ACPI_COMPANION(dev);
1033f904 269 struct acpi_device_physical_node *entry;
1033f904 270
7b199811 271 if (!acpi_dev)
4e10d12a 272 return 0;
1071695f 273
1033f904 274 mutex_lock(&acpi_dev->physical_node_lock);
3e332783
RW
275
276 list_for_each_entry(entry, &acpi_dev->physical_node_list, node)
277 if (entry->dev == dev) {
278 char physnode_name[PHYSICAL_NODE_NAME_SIZE];
279
280 list_del(&entry->node);
281 acpi_dev->physical_node_count--;
282
283 acpi_physnode_link_name(physnode_name, entry->node_id);
284 sysfs_remove_link(&acpi_dev->dev.kobj, physnode_name);
285 sysfs_remove_link(&dev->kobj, "firmware_node");
7b199811 286 ACPI_COMPANION_SET(dev, NULL);
a104b4d4 287 /* Drop references taken by acpi_bind_one(). */
3e332783 288 put_device(dev);
a104b4d4 289 put_device(&acpi_dev->dev);
3e332783
RW
290 kfree(entry);
291 break;
292 }
293
1033f904 294 mutex_unlock(&acpi_dev->physical_node_lock);
4e10d12a
DSL
295 return 0;
296}
ac212b69 297EXPORT_SYMBOL_GPL(acpi_unbind_one);
4e10d12a
DSL
298
299static int acpi_platform_notify(struct device *dev)
300{
53540098 301 struct acpi_bus_type *type = acpi_get_bus_type(dev);
11909ca1 302 int ret;
4e10d12a 303
f3fd0c8a 304 ret = acpi_bind_one(dev, NULL);
92414481 305 if (ret && type) {
e3f02c52
RW
306 struct acpi_device *adev;
307
308 adev = type->find_companion(dev);
309 if (!adev) {
11909ca1 310 DBG("Unable to get handle for %s\n", dev_name(dev));
e3f02c52 311 ret = -ENODEV;
11909ca1
RW
312 goto out;
313 }
24dee1fc 314 ret = acpi_bind_one(dev, adev);
11909ca1
RW
315 if (ret)
316 goto out;
4e10d12a 317 }
11909ca1
RW
318
319 if (type && type->setup)
320 type->setup(dev);
4e10d12a 321
f3fd0c8a 322 out:
4e10d12a
DSL
323#if ACPI_GLUE_DEBUG
324 if (!ret) {
325 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
326
a412a11d 327 acpi_get_name(ACPI_HANDLE(dev), ACPI_FULL_PATHNAME, &buffer);
db1461ad 328 DBG("Device %s -> %s\n", dev_name(dev), (char *)buffer.pointer);
02438d87 329 kfree(buffer.pointer);
4e10d12a 330 } else
db1461ad 331 DBG("Device %s -> No ACPI support\n", dev_name(dev));
4e10d12a
DSL
332#endif
333
334 return ret;
335}
336
337static int acpi_platform_notify_remove(struct device *dev)
338{
11909ca1
RW
339 struct acpi_bus_type *type;
340
53540098 341 type = acpi_get_bus_type(dev);
11909ca1
RW
342 if (type && type->cleanup)
343 type->cleanup(dev);
344
4e10d12a
DSL
345 acpi_unbind_one(dev);
346 return 0;
347}
348
0e46517d 349int __init init_acpi_device_notify(void)
4e10d12a 350{
4e10d12a
DSL
351 if (platform_notify || platform_notify_remove) {
352 printk(KERN_ERR PREFIX "Can't use platform_notify\n");
353 return 0;
354 }
355 platform_notify = acpi_platform_notify;
356 platform_notify_remove = acpi_platform_notify_remove;
357 return 0;
358}
This page took 0.555439 seconds and 5 git commands to generate.