selinux: quiet the filesystem labeling behavior message
[GitHub/LineageOS/android_kernel_motorola_exynos9610.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
bdbdbf91
RW
154static void acpi_physnode_link_name(char *buf, unsigned int node_id)
155{
156 if (node_id > 0)
157 snprintf(buf, PHYSICAL_NODE_NAME_SIZE,
158 PHYSICAL_NODE_STRING "%u", node_id);
159 else
160 strcpy(buf, PHYSICAL_NODE_STRING);
161}
162
24dee1fc 163int acpi_bind_one(struct device *dev, struct acpi_device *acpi_dev)
4e10d12a 164{
f3fd0c8a 165 struct acpi_device_physical_node *physical_node, *pn;
007ccfcf
RW
166 char physical_node_name[PHYSICAL_NODE_NAME_SIZE];
167 struct list_head *physnode_list;
168 unsigned int node_id;
1033f904 169 int retval = -EINVAL;
4e10d12a 170
7b199811 171 if (ACPI_COMPANION(dev)) {
24dee1fc 172 if (acpi_dev) {
7b199811 173 dev_warn(dev, "ACPI companion already set\n");
f3fd0c8a
RW
174 return -EINVAL;
175 } else {
7b199811 176 acpi_dev = ACPI_COMPANION(dev);
f3fd0c8a 177 }
4e10d12a 178 }
7b199811 179 if (!acpi_dev)
f3fd0c8a 180 return -EINVAL;
1033f904 181
a104b4d4 182 get_device(&acpi_dev->dev);
4e10d12a 183 get_device(dev);
f3fd0c8a 184 physical_node = kzalloc(sizeof(*physical_node), GFP_KERNEL);
1033f904
LT
185 if (!physical_node) {
186 retval = -ENOMEM;
187 goto err;
4e10d12a 188 }
4e10d12a 189
1033f904 190 mutex_lock(&acpi_dev->physical_node_lock);
f3fd0c8a 191
007ccfcf
RW
192 /*
193 * Keep the list sorted by node_id so that the IDs of removed nodes can
194 * be recycled easily.
195 */
196 physnode_list = &acpi_dev->physical_node_list;
197 node_id = 0;
198 list_for_each_entry(pn, &acpi_dev->physical_node_list, node) {
199 /* Sanity check. */
f3fd0c8a 200 if (pn->dev == dev) {
3342c753
RW
201 mutex_unlock(&acpi_dev->physical_node_lock);
202
f3fd0c8a 203 dev_warn(dev, "Already associated with ACPI node\n");
3342c753 204 kfree(physical_node);
7b199811 205 if (ACPI_COMPANION(dev) != acpi_dev)
3342c753 206 goto err;
3fe444ad 207
3342c753 208 put_device(dev);
a104b4d4 209 put_device(&acpi_dev->dev);
3342c753 210 return 0;
f3fd0c8a 211 }
007ccfcf
RW
212 if (pn->node_id == node_id) {
213 physnode_list = &pn->node;
214 node_id++;
215 }
1071695f
DB
216 }
217
007ccfcf 218 physical_node->node_id = node_id;
1033f904 219 physical_node->dev = dev;
007ccfcf 220 list_add(&physical_node->node, physnode_list);
1033f904 221 acpi_dev->physical_node_count++;
f3fd0c8a 222
7b199811
RW
223 if (!ACPI_COMPANION(dev))
224 ACPI_COMPANION_SET(dev, acpi_dev);
1033f904 225
bdbdbf91 226 acpi_physnode_link_name(physical_node_name, node_id);
1033f904 227 retval = sysfs_create_link(&acpi_dev->dev.kobj, &dev->kobj,
f501b6ec 228 physical_node_name);
464c1147
RW
229 if (retval)
230 dev_err(&acpi_dev->dev, "Failed to create link %s (%d)\n",
231 physical_node_name, retval);
232
1033f904 233 retval = sysfs_create_link(&dev->kobj, &acpi_dev->dev.kobj,
f501b6ec 234 "firmware_node");
464c1147
RW
235 if (retval)
236 dev_err(dev, "Failed to create link firmware_node (%d)\n",
237 retval);
1033f904 238
40055206
RW
239 mutex_unlock(&acpi_dev->physical_node_lock);
240
1033f904
LT
241 if (acpi_dev->wakeup.flags.valid)
242 device_set_wakeup_capable(dev, true);
243
4e10d12a 244 return 0;
1033f904
LT
245
246 err:
7b199811 247 ACPI_COMPANION_SET(dev, NULL);
1033f904 248 put_device(dev);
a104b4d4 249 put_device(&acpi_dev->dev);
1033f904 250 return retval;
4e10d12a 251}
ac212b69 252EXPORT_SYMBOL_GPL(acpi_bind_one);
4e10d12a 253
ac212b69 254int acpi_unbind_one(struct device *dev)
4e10d12a 255{
7b199811 256 struct acpi_device *acpi_dev = ACPI_COMPANION(dev);
1033f904 257 struct acpi_device_physical_node *entry;
1033f904 258
7b199811 259 if (!acpi_dev)
4e10d12a 260 return 0;
1071695f 261
1033f904 262 mutex_lock(&acpi_dev->physical_node_lock);
3e332783
RW
263
264 list_for_each_entry(entry, &acpi_dev->physical_node_list, node)
265 if (entry->dev == dev) {
266 char physnode_name[PHYSICAL_NODE_NAME_SIZE];
267
268 list_del(&entry->node);
269 acpi_dev->physical_node_count--;
270
271 acpi_physnode_link_name(physnode_name, entry->node_id);
272 sysfs_remove_link(&acpi_dev->dev.kobj, physnode_name);
273 sysfs_remove_link(&dev->kobj, "firmware_node");
7b199811 274 ACPI_COMPANION_SET(dev, NULL);
a104b4d4 275 /* Drop references taken by acpi_bind_one(). */
3e332783 276 put_device(dev);
a104b4d4 277 put_device(&acpi_dev->dev);
3e332783
RW
278 kfree(entry);
279 break;
280 }
281
1033f904 282 mutex_unlock(&acpi_dev->physical_node_lock);
4e10d12a
DSL
283 return 0;
284}
ac212b69 285EXPORT_SYMBOL_GPL(acpi_unbind_one);
4e10d12a
DSL
286
287static int acpi_platform_notify(struct device *dev)
288{
53540098 289 struct acpi_bus_type *type = acpi_get_bus_type(dev);
9cb32acf 290 struct acpi_device *adev;
11909ca1 291 int ret;
4e10d12a 292
f3fd0c8a 293 ret = acpi_bind_one(dev, NULL);
92414481 294 if (ret && type) {
e3f02c52
RW
295 struct acpi_device *adev;
296
297 adev = type->find_companion(dev);
298 if (!adev) {
11909ca1 299 DBG("Unable to get handle for %s\n", dev_name(dev));
e3f02c52 300 ret = -ENODEV;
11909ca1
RW
301 goto out;
302 }
24dee1fc 303 ret = acpi_bind_one(dev, adev);
11909ca1
RW
304 if (ret)
305 goto out;
4e10d12a 306 }
9cb32acf
RW
307 adev = ACPI_COMPANION(dev);
308 if (!adev)
309 goto out;
11909ca1
RW
310
311 if (type && type->setup)
312 type->setup(dev);
9cb32acf
RW
313 else if (adev->handler && adev->handler->bind)
314 adev->handler->bind(dev);
4e10d12a 315
f3fd0c8a 316 out:
4e10d12a
DSL
317#if ACPI_GLUE_DEBUG
318 if (!ret) {
319 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
320
a412a11d 321 acpi_get_name(ACPI_HANDLE(dev), ACPI_FULL_PATHNAME, &buffer);
db1461ad 322 DBG("Device %s -> %s\n", dev_name(dev), (char *)buffer.pointer);
02438d87 323 kfree(buffer.pointer);
4e10d12a 324 } else
db1461ad 325 DBG("Device %s -> No ACPI support\n", dev_name(dev));
4e10d12a
DSL
326#endif
327
328 return ret;
329}
330
331static int acpi_platform_notify_remove(struct device *dev)
332{
9cb32acf 333 struct acpi_device *adev = ACPI_COMPANION(dev);
11909ca1
RW
334 struct acpi_bus_type *type;
335
9cb32acf
RW
336 if (!adev)
337 return 0;
338
53540098 339 type = acpi_get_bus_type(dev);
11909ca1
RW
340 if (type && type->cleanup)
341 type->cleanup(dev);
9cb32acf
RW
342 else if (adev->handler && adev->handler->unbind)
343 adev->handler->unbind(dev);
11909ca1 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}