Merge branch 'acpi-scan'
[cascardo/linux.git] / drivers / acpi / glue.c
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  */
9 #include <linux/export.h>
10 #include <linux/init.h>
11 #include <linux/list.h>
12 #include <linux/device.h>
13 #include <linux/slab.h>
14 #include <linux/rwsem.h>
15 #include <linux/acpi.h>
16
17 #include "internal.h"
18
19 #define ACPI_GLUE_DEBUG 0
20 #if ACPI_GLUE_DEBUG
21 #define DBG(fmt, ...)                                           \
22         printk(KERN_DEBUG PREFIX fmt, ##__VA_ARGS__)
23 #else
24 #define DBG(fmt, ...)                                           \
25 do {                                                            \
26         if (0)                                                  \
27                 printk(KERN_DEBUG PREFIX fmt, ##__VA_ARGS__);   \
28 } while (0)
29 #endif
30 static LIST_HEAD(bus_type_list);
31 static DECLARE_RWSEM(bus_type_sem);
32
33 #define PHYSICAL_NODE_STRING "physical_node"
34
35 int 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);
43                 printk(KERN_INFO PREFIX "bus type %s registered\n",
44                        type->bus->name);
45                 return 0;
46         }
47         return -ENODEV;
48 }
49 EXPORT_SYMBOL_GPL(register_acpi_bus_type);
50
51 int 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);
59                 printk(KERN_INFO PREFIX "ACPI bus type %s unregistered\n",
60                        type->bus->name);
61                 return 0;
62         }
63         return -ENODEV;
64 }
65 EXPORT_SYMBOL_GPL(unregister_acpi_bus_type);
66
67 static struct acpi_bus_type *acpi_get_bus_type(struct bus_type *type)
68 {
69         struct acpi_bus_type *tmp, *ret = NULL;
70
71         if (!type)
72                 return NULL;
73
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
85 static 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
101 /* Get device's handler per its address under its parent */
102 struct acpi_find_child {
103         acpi_handle handle;
104         u64 address;
105 };
106
107 static acpi_status
108 do_acpi_find_child(acpi_handle handle, u32 lvl, void *context, void **rv)
109 {
110         acpi_status status;
111         struct acpi_device_info *info;
112         struct acpi_find_child *find = context;
113
114         status = acpi_get_object_info(handle, &info);
115         if (ACPI_SUCCESS(status)) {
116                 if ((info->address == find->address)
117                         && (info->valid & ACPI_VALID_ADR))
118                         find->handle = handle;
119                 kfree(info);
120         }
121         return AE_OK;
122 }
123
124 acpi_handle acpi_get_child(acpi_handle parent, u64 address)
125 {
126         struct acpi_find_child find = { NULL, address };
127
128         if (!parent)
129                 return NULL;
130         acpi_walk_namespace(ACPI_TYPE_DEVICE, parent,
131                             1, do_acpi_find_child, NULL, &find, NULL);
132         return find.handle;
133 }
134
135 EXPORT_SYMBOL(acpi_get_child);
136
137 static int acpi_bind_one(struct device *dev, acpi_handle handle)
138 {
139         struct acpi_device *acpi_dev;
140         acpi_status status;
141         struct acpi_device_physical_node *physical_node, *pn;
142         char physical_node_name[sizeof(PHYSICAL_NODE_STRING) + 2];
143         int retval = -EINVAL;
144
145         if (ACPI_HANDLE(dev)) {
146                 if (handle) {
147                         dev_warn(dev, "ACPI handle is already set\n");
148                         return -EINVAL;
149                 } else {
150                         handle = ACPI_HANDLE(dev);
151                 }
152         }
153         if (!handle)
154                 return -EINVAL;
155
156         get_device(dev);
157         status = acpi_bus_get_device(handle, &acpi_dev);
158         if (ACPI_FAILURE(status))
159                 goto err;
160
161         physical_node = kzalloc(sizeof(*physical_node), GFP_KERNEL);
162         if (!physical_node) {
163                 retval = -ENOMEM;
164                 goto err;
165         }
166
167         mutex_lock(&acpi_dev->physical_node_lock);
168
169         /* Sanity check. */
170         list_for_each_entry(pn, &acpi_dev->physical_node_list, node)
171                 if (pn->dev == dev) {
172                         dev_warn(dev, "Already associated with ACPI node\n");
173                         goto err_free;
174                 }
175
176         /* allocate physical node id according to physical_node_id_bitmap */
177         physical_node->node_id =
178                 find_first_zero_bit(acpi_dev->physical_node_id_bitmap,
179                 ACPI_MAX_PHYSICAL_NODE);
180         if (physical_node->node_id >= ACPI_MAX_PHYSICAL_NODE) {
181                 retval = -ENOSPC;
182                 goto err_free;
183         }
184
185         set_bit(physical_node->node_id, acpi_dev->physical_node_id_bitmap);
186         physical_node->dev = dev;
187         list_add_tail(&physical_node->node, &acpi_dev->physical_node_list);
188         acpi_dev->physical_node_count++;
189
190         mutex_unlock(&acpi_dev->physical_node_lock);
191
192         if (!ACPI_HANDLE(dev))
193                 ACPI_HANDLE_SET(dev, acpi_dev->handle);
194
195         if (!physical_node->node_id)
196                 strcpy(physical_node_name, PHYSICAL_NODE_STRING);
197         else
198                 sprintf(physical_node_name,
199                         "physical_node%d", physical_node->node_id);
200         retval = sysfs_create_link(&acpi_dev->dev.kobj, &dev->kobj,
201                         physical_node_name);
202         retval = sysfs_create_link(&dev->kobj, &acpi_dev->dev.kobj,
203                 "firmware_node");
204
205         if (acpi_dev->wakeup.flags.valid)
206                 device_set_wakeup_capable(dev, true);
207
208         return 0;
209
210  err:
211         ACPI_HANDLE_SET(dev, NULL);
212         put_device(dev);
213         return retval;
214
215  err_free:
216         mutex_unlock(&acpi_dev->physical_node_lock);
217         kfree(physical_node);
218         goto err;
219 }
220
221 static int acpi_unbind_one(struct device *dev)
222 {
223         struct acpi_device_physical_node *entry;
224         struct acpi_device *acpi_dev;
225         acpi_status status;
226         struct list_head *node, *next;
227
228         if (!ACPI_HANDLE(dev))
229                 return 0;
230
231         status = acpi_bus_get_device(ACPI_HANDLE(dev), &acpi_dev);
232         if (ACPI_FAILURE(status))
233                 goto err;
234
235         mutex_lock(&acpi_dev->physical_node_lock);
236         list_for_each_safe(node, next, &acpi_dev->physical_node_list) {
237                 char physical_node_name[sizeof(PHYSICAL_NODE_STRING) + 2];
238
239                 entry = list_entry(node, struct acpi_device_physical_node,
240                         node);
241                 if (entry->dev != dev)
242                         continue;
243
244                 list_del(node);
245                 clear_bit(entry->node_id, acpi_dev->physical_node_id_bitmap);
246
247                 acpi_dev->physical_node_count--;
248
249                 if (!entry->node_id)
250                         strcpy(physical_node_name, PHYSICAL_NODE_STRING);
251                 else
252                         sprintf(physical_node_name,
253                                 "physical_node%d", entry->node_id);
254
255                 sysfs_remove_link(&acpi_dev->dev.kobj, physical_node_name);
256                 sysfs_remove_link(&dev->kobj, "firmware_node");
257                 ACPI_HANDLE_SET(dev, NULL);
258                 /* acpi_bind_one increase refcnt by one */
259                 put_device(dev);
260                 kfree(entry);
261         }
262         mutex_unlock(&acpi_dev->physical_node_lock);
263
264         return 0;
265
266 err:
267         dev_err(dev, "Oops, 'acpi_handle' corrupt\n");
268         return -EINVAL;
269 }
270
271 static int acpi_platform_notify(struct device *dev)
272 {
273         struct acpi_bus_type *type;
274         acpi_handle handle;
275         int ret;
276
277         ret = acpi_bind_one(dev, NULL);
278         if (ret && (!dev->bus || !dev->parent)) {
279                 /* bridge devices genernally haven't bus or parent */
280                 ret = acpi_find_bridge_device(dev, &handle);
281                 if (!ret) {
282                         ret = acpi_bind_one(dev, handle);
283                         if (ret)
284                                 goto out;
285                 }
286         }
287
288         type = acpi_get_bus_type(dev->bus);
289         if (ret) {
290                 if (!type || !type->find_device) {
291                         DBG("No ACPI bus support for %s\n", dev_name(dev));
292                         ret = -EINVAL;
293                         goto out;
294                 }
295
296                 ret = type->find_device(dev, &handle);
297                 if (ret) {
298                         DBG("Unable to get handle for %s\n", dev_name(dev));
299                         goto out;
300                 }
301                 ret = acpi_bind_one(dev, handle);
302                 if (ret)
303                         goto out;
304         }
305
306         if (type && type->setup)
307                 type->setup(dev);
308
309  out:
310 #if ACPI_GLUE_DEBUG
311         if (!ret) {
312                 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
313
314                 acpi_get_name(ACPI_HANDLE(dev), ACPI_FULL_PATHNAME, &buffer);
315                 DBG("Device %s -> %s\n", dev_name(dev), (char *)buffer.pointer);
316                 kfree(buffer.pointer);
317         } else
318                 DBG("Device %s -> No ACPI support\n", dev_name(dev));
319 #endif
320
321         return ret;
322 }
323
324 static int acpi_platform_notify_remove(struct device *dev)
325 {
326         struct acpi_bus_type *type;
327
328         type = acpi_get_bus_type(dev->bus);
329         if (type && type->cleanup)
330                 type->cleanup(dev);
331
332         acpi_unbind_one(dev);
333         return 0;
334 }
335
336 int __init init_acpi_device_notify(void)
337 {
338         if (platform_notify || platform_notify_remove) {
339                 printk(KERN_ERR PREFIX "Can't use platform_notify\n");
340                 return 0;
341         }
342         platform_notify = acpi_platform_notify;
343         platform_notify_remove = acpi_platform_notify_remove;
344         return 0;
345 }