]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - drivers/acpi/glue.c
ACPI / bind: Redefine acpi_preset_companion()
[mirror_ubuntu-zesty-kernel.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;
53540098 40 if (type && type->match && type->find_device) {
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
ac212b69 175int acpi_bind_one(struct device *dev, acpi_handle handle)
4e10d12a 176{
7b199811 177 struct acpi_device *acpi_dev = NULL;
f3fd0c8a 178 struct acpi_device_physical_node *physical_node, *pn;
007ccfcf
RW
179 char physical_node_name[PHYSICAL_NODE_NAME_SIZE];
180 struct list_head *physnode_list;
181 unsigned int node_id;
1033f904 182 int retval = -EINVAL;
4e10d12a 183
7b199811 184 if (ACPI_COMPANION(dev)) {
f3fd0c8a 185 if (handle) {
7b199811 186 dev_warn(dev, "ACPI companion already set\n");
f3fd0c8a
RW
187 return -EINVAL;
188 } else {
7b199811 189 acpi_dev = ACPI_COMPANION(dev);
f3fd0c8a 190 }
7b199811
RW
191 } else {
192 acpi_bus_get_device(handle, &acpi_dev);
4e10d12a 193 }
7b199811 194 if (!acpi_dev)
f3fd0c8a 195 return -EINVAL;
1033f904 196
a104b4d4 197 get_device(&acpi_dev->dev);
4e10d12a 198 get_device(dev);
f3fd0c8a 199 physical_node = kzalloc(sizeof(*physical_node), GFP_KERNEL);
1033f904
LT
200 if (!physical_node) {
201 retval = -ENOMEM;
202 goto err;
4e10d12a 203 }
4e10d12a 204
1033f904 205 mutex_lock(&acpi_dev->physical_node_lock);
f3fd0c8a 206
007ccfcf
RW
207 /*
208 * Keep the list sorted by node_id so that the IDs of removed nodes can
209 * be recycled easily.
210 */
211 physnode_list = &acpi_dev->physical_node_list;
212 node_id = 0;
213 list_for_each_entry(pn, &acpi_dev->physical_node_list, node) {
214 /* Sanity check. */
f3fd0c8a 215 if (pn->dev == dev) {
3342c753
RW
216 mutex_unlock(&acpi_dev->physical_node_lock);
217
f3fd0c8a 218 dev_warn(dev, "Already associated with ACPI node\n");
3342c753 219 kfree(physical_node);
7b199811 220 if (ACPI_COMPANION(dev) != acpi_dev)
3342c753 221 goto err;
3fe444ad 222
3342c753 223 put_device(dev);
a104b4d4 224 put_device(&acpi_dev->dev);
3342c753 225 return 0;
f3fd0c8a 226 }
007ccfcf
RW
227 if (pn->node_id == node_id) {
228 physnode_list = &pn->node;
229 node_id++;
230 }
1071695f
DB
231 }
232
007ccfcf 233 physical_node->node_id = node_id;
1033f904 234 physical_node->dev = dev;
007ccfcf 235 list_add(&physical_node->node, physnode_list);
1033f904 236 acpi_dev->physical_node_count++;
f3fd0c8a 237
7b199811
RW
238 if (!ACPI_COMPANION(dev))
239 ACPI_COMPANION_SET(dev, acpi_dev);
1033f904 240
bdbdbf91 241 acpi_physnode_link_name(physical_node_name, node_id);
1033f904 242 retval = sysfs_create_link(&acpi_dev->dev.kobj, &dev->kobj,
f501b6ec 243 physical_node_name);
464c1147
RW
244 if (retval)
245 dev_err(&acpi_dev->dev, "Failed to create link %s (%d)\n",
246 physical_node_name, retval);
247
1033f904 248 retval = sysfs_create_link(&dev->kobj, &acpi_dev->dev.kobj,
f501b6ec 249 "firmware_node");
464c1147
RW
250 if (retval)
251 dev_err(dev, "Failed to create link firmware_node (%d)\n",
252 retval);
1033f904 253
40055206
RW
254 mutex_unlock(&acpi_dev->physical_node_lock);
255
1033f904
LT
256 if (acpi_dev->wakeup.flags.valid)
257 device_set_wakeup_capable(dev, true);
258
4e10d12a 259 return 0;
1033f904
LT
260
261 err:
7b199811 262 ACPI_COMPANION_SET(dev, NULL);
1033f904 263 put_device(dev);
a104b4d4 264 put_device(&acpi_dev->dev);
1033f904 265 return retval;
4e10d12a 266}
ac212b69 267EXPORT_SYMBOL_GPL(acpi_bind_one);
4e10d12a 268
ac212b69 269int acpi_unbind_one(struct device *dev)
4e10d12a 270{
7b199811 271 struct acpi_device *acpi_dev = ACPI_COMPANION(dev);
1033f904 272 struct acpi_device_physical_node *entry;
1033f904 273
7b199811 274 if (!acpi_dev)
4e10d12a 275 return 0;
1071695f 276
1033f904 277 mutex_lock(&acpi_dev->physical_node_lock);
3e332783
RW
278
279 list_for_each_entry(entry, &acpi_dev->physical_node_list, node)
280 if (entry->dev == dev) {
281 char physnode_name[PHYSICAL_NODE_NAME_SIZE];
282
283 list_del(&entry->node);
284 acpi_dev->physical_node_count--;
285
286 acpi_physnode_link_name(physnode_name, entry->node_id);
287 sysfs_remove_link(&acpi_dev->dev.kobj, physnode_name);
288 sysfs_remove_link(&dev->kobj, "firmware_node");
7b199811 289 ACPI_COMPANION_SET(dev, NULL);
a104b4d4 290 /* Drop references taken by acpi_bind_one(). */
3e332783 291 put_device(dev);
a104b4d4 292 put_device(&acpi_dev->dev);
3e332783
RW
293 kfree(entry);
294 break;
295 }
296
1033f904 297 mutex_unlock(&acpi_dev->physical_node_lock);
4e10d12a
DSL
298 return 0;
299}
ac212b69 300EXPORT_SYMBOL_GPL(acpi_unbind_one);
4e10d12a
DSL
301
302static int acpi_platform_notify(struct device *dev)
303{
53540098 304 struct acpi_bus_type *type = acpi_get_bus_type(dev);
4e10d12a 305 acpi_handle handle;
11909ca1 306 int ret;
4e10d12a 307
f3fd0c8a 308 ret = acpi_bind_one(dev, NULL);
92414481 309 if (ret && type) {
11909ca1
RW
310 ret = type->find_device(dev, &handle);
311 if (ret) {
312 DBG("Unable to get handle for %s\n", dev_name(dev));
313 goto out;
314 }
315 ret = acpi_bind_one(dev, handle);
316 if (ret)
317 goto out;
4e10d12a 318 }
11909ca1
RW
319
320 if (type && type->setup)
321 type->setup(dev);
4e10d12a 322
f3fd0c8a 323 out:
4e10d12a
DSL
324#if ACPI_GLUE_DEBUG
325 if (!ret) {
326 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
327
a412a11d 328 acpi_get_name(ACPI_HANDLE(dev), ACPI_FULL_PATHNAME, &buffer);
db1461ad 329 DBG("Device %s -> %s\n", dev_name(dev), (char *)buffer.pointer);
02438d87 330 kfree(buffer.pointer);
4e10d12a 331 } else
db1461ad 332 DBG("Device %s -> No ACPI support\n", dev_name(dev));
4e10d12a
DSL
333#endif
334
335 return ret;
336}
337
338static int acpi_platform_notify_remove(struct device *dev)
339{
11909ca1
RW
340 struct acpi_bus_type *type;
341
53540098 342 type = acpi_get_bus_type(dev);
11909ca1
RW
343 if (type && type->cleanup)
344 type->cleanup(dev);
345
4e10d12a
DSL
346 acpi_unbind_one(dev);
347 return 0;
348}
349
0e46517d 350int __init init_acpi_device_notify(void)
4e10d12a 351{
4e10d12a
DSL
352 if (platform_notify || platform_notify_remove) {
353 printk(KERN_ERR PREFIX "Can't use platform_notify\n");
354 return 0;
355 }
356 platform_notify = acpi_platform_notify;
357 platform_notify_remove = acpi_platform_notify_remove;
358 return 0;
359}