]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - drivers/acpi/glue.c
ACPI / porocessor: Beautify code, pr->id is u32 which is never < 0
[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
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;
39 if (type && type->bus && type->find_device) {
40 down_write(&bus_type_sem);
41 list_add_tail(&type->list, &bus_type_list);
42 up_write(&bus_type_sem);
4be44fcd
LB
43 printk(KERN_INFO PREFIX "bus type %s registered\n",
44 type->bus->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);
4be44fcd
LB
59 printk(KERN_INFO PREFIX "ACPI bus type %s unregistered\n",
60 type->bus->name);
4e10d12a
DSL
61 return 0;
62 }
63 return -ENODEV;
64}
91e4d5a1 65EXPORT_SYMBOL_GPL(unregister_acpi_bus_type);
4e10d12a 66
4e10d12a
DSL
67static struct acpi_bus_type *acpi_get_bus_type(struct bus_type *type)
68{
69 struct acpi_bus_type *tmp, *ret = NULL;
70
11909ca1
RW
71 if (!type)
72 return NULL;
73
4e10d12a
DSL
74 down_read(&bus_type_sem);
75 list_for_each_entry(tmp, &bus_type_list, list) {
76 if (tmp->bus == type) {
77 ret = tmp;
78 break;
79 }
80 }
81 up_read(&bus_type_sem);
82 return ret;
83}
84
85static int acpi_find_bridge_device(struct device *dev, acpi_handle * handle)
86{
87 struct acpi_bus_type *tmp;
88 int ret = -ENODEV;
89
90 down_read(&bus_type_sem);
91 list_for_each_entry(tmp, &bus_type_list, list) {
92 if (tmp->find_bridge && !tmp->find_bridge(dev, handle)) {
93 ret = 0;
94 break;
95 }
96 }
97 up_read(&bus_type_sem);
98 return ret;
99}
100
33f767d7
RW
101static acpi_status do_acpi_find_child(acpi_handle handle, u32 lvl_not_used,
102 void *addr_p, void **ret_p)
4e10d12a 103{
33f767d7 104 unsigned long long addr;
4e10d12a 105 acpi_status status;
33f767d7
RW
106
107 status = acpi_evaluate_integer(handle, METHOD_NAME__ADR, NULL, &addr);
108 if (ACPI_SUCCESS(status) && addr == *((u64 *)addr_p)) {
109 *ret_p = handle;
110 return AE_CTRL_TERMINATE;
4e10d12a
DSL
111 }
112 return AE_OK;
113}
114
439913ff 115acpi_handle acpi_get_child(acpi_handle parent, u64 address)
4e10d12a 116{
33f767d7 117 void *ret = NULL;
4e10d12a
DSL
118
119 if (!parent)
120 return NULL;
4e10d12a 121
33f767d7
RW
122 acpi_walk_namespace(ACPI_TYPE_DEVICE, parent, 1, NULL,
123 do_acpi_find_child, &address, &ret);
124 return (acpi_handle)ret;
125}
4e10d12a
DSL
126EXPORT_SYMBOL(acpi_get_child);
127
4e10d12a
DSL
128static int acpi_bind_one(struct device *dev, acpi_handle handle)
129{
1071695f 130 struct acpi_device *acpi_dev;
4e10d12a 131 acpi_status status;
f3fd0c8a 132 struct acpi_device_physical_node *physical_node, *pn;
1033f904
LT
133 char physical_node_name[sizeof(PHYSICAL_NODE_STRING) + 2];
134 int retval = -EINVAL;
4e10d12a 135
95f8a082 136 if (ACPI_HANDLE(dev)) {
f3fd0c8a
RW
137 if (handle) {
138 dev_warn(dev, "ACPI handle is already set\n");
139 return -EINVAL;
140 } else {
95f8a082 141 handle = ACPI_HANDLE(dev);
f3fd0c8a 142 }
4e10d12a 143 }
f3fd0c8a
RW
144 if (!handle)
145 return -EINVAL;
1033f904 146
4e10d12a 147 get_device(dev);
1033f904
LT
148 status = acpi_bus_get_device(handle, &acpi_dev);
149 if (ACPI_FAILURE(status))
150 goto err;
151
f3fd0c8a 152 physical_node = kzalloc(sizeof(*physical_node), GFP_KERNEL);
1033f904
LT
153 if (!physical_node) {
154 retval = -ENOMEM;
155 goto err;
4e10d12a 156 }
4e10d12a 157
1033f904 158 mutex_lock(&acpi_dev->physical_node_lock);
f3fd0c8a
RW
159
160 /* Sanity check. */
161 list_for_each_entry(pn, &acpi_dev->physical_node_list, node)
162 if (pn->dev == dev) {
163 dev_warn(dev, "Already associated with ACPI node\n");
164 goto err_free;
165 }
166
1033f904
LT
167 /* allocate physical node id according to physical_node_id_bitmap */
168 physical_node->node_id =
169 find_first_zero_bit(acpi_dev->physical_node_id_bitmap,
170 ACPI_MAX_PHYSICAL_NODE);
171 if (physical_node->node_id >= ACPI_MAX_PHYSICAL_NODE) {
172 retval = -ENOSPC;
f3fd0c8a 173 goto err_free;
1071695f
DB
174 }
175
1033f904
LT
176 set_bit(physical_node->node_id, acpi_dev->physical_node_id_bitmap);
177 physical_node->dev = dev;
178 list_add_tail(&physical_node->node, &acpi_dev->physical_node_list);
179 acpi_dev->physical_node_count++;
f3fd0c8a 180
1033f904
LT
181 mutex_unlock(&acpi_dev->physical_node_lock);
182
95f8a082
RW
183 if (!ACPI_HANDLE(dev))
184 ACPI_HANDLE_SET(dev, acpi_dev->handle);
1033f904
LT
185
186 if (!physical_node->node_id)
187 strcpy(physical_node_name, PHYSICAL_NODE_STRING);
188 else
189 sprintf(physical_node_name,
190 "physical_node%d", physical_node->node_id);
191 retval = sysfs_create_link(&acpi_dev->dev.kobj, &dev->kobj,
192 physical_node_name);
193 retval = sysfs_create_link(&dev->kobj, &acpi_dev->dev.kobj,
194 "firmware_node");
195
196 if (acpi_dev->wakeup.flags.valid)
197 device_set_wakeup_capable(dev, true);
198
4e10d12a 199 return 0;
1033f904
LT
200
201 err:
95f8a082 202 ACPI_HANDLE_SET(dev, NULL);
1033f904
LT
203 put_device(dev);
204 return retval;
f3fd0c8a
RW
205
206 err_free:
207 mutex_unlock(&acpi_dev->physical_node_lock);
208 kfree(physical_node);
209 goto err;
4e10d12a
DSL
210}
211
212static int acpi_unbind_one(struct device *dev)
213{
1033f904
LT
214 struct acpi_device_physical_node *entry;
215 struct acpi_device *acpi_dev;
216 acpi_status status;
217 struct list_head *node, *next;
218
95f8a082 219 if (!ACPI_HANDLE(dev))
4e10d12a 220 return 0;
1071695f 221
95f8a082 222 status = acpi_bus_get_device(ACPI_HANDLE(dev), &acpi_dev);
1033f904
LT
223 if (ACPI_FAILURE(status))
224 goto err;
1071695f 225
1033f904
LT
226 mutex_lock(&acpi_dev->physical_node_lock);
227 list_for_each_safe(node, next, &acpi_dev->physical_node_list) {
228 char physical_node_name[sizeof(PHYSICAL_NODE_STRING) + 2];
229
230 entry = list_entry(node, struct acpi_device_physical_node,
231 node);
232 if (entry->dev != dev)
233 continue;
234
235 list_del(node);
236 clear_bit(entry->node_id, acpi_dev->physical_node_id_bitmap);
1071695f 237
1033f904
LT
238 acpi_dev->physical_node_count--;
239
240 if (!entry->node_id)
241 strcpy(physical_node_name, PHYSICAL_NODE_STRING);
242 else
243 sprintf(physical_node_name,
244 "physical_node%d", entry->node_id);
245
246 sysfs_remove_link(&acpi_dev->dev.kobj, physical_node_name);
247 sysfs_remove_link(&dev->kobj, "firmware_node");
95f8a082 248 ACPI_HANDLE_SET(dev, NULL);
4e10d12a
DSL
249 /* acpi_bind_one increase refcnt by one */
250 put_device(dev);
1033f904 251 kfree(entry);
4e10d12a 252 }
1033f904
LT
253 mutex_unlock(&acpi_dev->physical_node_lock);
254
4e10d12a 255 return 0;
1033f904
LT
256
257err:
258 dev_err(dev, "Oops, 'acpi_handle' corrupt\n");
259 return -EINVAL;
4e10d12a
DSL
260}
261
262static int acpi_platform_notify(struct device *dev)
263{
264 struct acpi_bus_type *type;
265 acpi_handle handle;
11909ca1 266 int ret;
4e10d12a 267
f3fd0c8a 268 ret = acpi_bind_one(dev, NULL);
11909ca1 269 if (ret && (!dev->bus || !dev->parent)) {
4e10d12a
DSL
270 /* bridge devices genernally haven't bus or parent */
271 ret = acpi_find_bridge_device(dev, &handle);
11909ca1
RW
272 if (!ret) {
273 ret = acpi_bind_one(dev, handle);
274 if (ret)
275 goto out;
276 }
4e10d12a 277 }
11909ca1 278
4e10d12a 279 type = acpi_get_bus_type(dev->bus);
11909ca1
RW
280 if (ret) {
281 if (!type || !type->find_device) {
282 DBG("No ACPI bus support for %s\n", dev_name(dev));
283 ret = -EINVAL;
284 goto out;
285 }
286
287 ret = type->find_device(dev, &handle);
288 if (ret) {
289 DBG("Unable to get handle for %s\n", dev_name(dev));
290 goto out;
291 }
292 ret = acpi_bind_one(dev, handle);
293 if (ret)
294 goto out;
4e10d12a 295 }
11909ca1
RW
296
297 if (type && type->setup)
298 type->setup(dev);
4e10d12a 299
f3fd0c8a 300 out:
4e10d12a
DSL
301#if ACPI_GLUE_DEBUG
302 if (!ret) {
303 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
304
a412a11d 305 acpi_get_name(ACPI_HANDLE(dev), ACPI_FULL_PATHNAME, &buffer);
db1461ad 306 DBG("Device %s -> %s\n", dev_name(dev), (char *)buffer.pointer);
02438d87 307 kfree(buffer.pointer);
4e10d12a 308 } else
db1461ad 309 DBG("Device %s -> No ACPI support\n", dev_name(dev));
4e10d12a
DSL
310#endif
311
312 return ret;
313}
314
315static int acpi_platform_notify_remove(struct device *dev)
316{
11909ca1
RW
317 struct acpi_bus_type *type;
318
319 type = acpi_get_bus_type(dev->bus);
320 if (type && type->cleanup)
321 type->cleanup(dev);
322
4e10d12a
DSL
323 acpi_unbind_one(dev);
324 return 0;
325}
326
0e46517d 327int __init init_acpi_device_notify(void)
4e10d12a 328{
4e10d12a
DSL
329 if (platform_notify || platform_notify_remove) {
330 printk(KERN_ERR PREFIX "Can't use platform_notify\n");
331 return 0;
332 }
333 platform_notify = acpi_platform_notify;
334 platform_notify_remove = acpi_platform_notify_remove;
335 return 0;
336}