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