Merge branch 'upstream' into upstream-jgarzik
[linux-2.6] / 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/init.h>
10 #include <linux/list.h>
11 #include <linux/device.h>
12 #include <linux/rwsem.h>
13 #include <linux/acpi.h>
14
15 #define ACPI_GLUE_DEBUG 0
16 #if ACPI_GLUE_DEBUG
17 #define DBG(x...) printk(PREFIX x)
18 #else
19 #define DBG(x...)
20 #endif
21 static LIST_HEAD(bus_type_list);
22 static DECLARE_RWSEM(bus_type_sem);
23
24 int register_acpi_bus_type(struct acpi_bus_type *type)
25 {
26         if (acpi_disabled)
27                 return -ENODEV;
28         if (type && type->bus && type->find_device) {
29                 down_write(&bus_type_sem);
30                 list_add_tail(&type->list, &bus_type_list);
31                 up_write(&bus_type_sem);
32                 printk(KERN_INFO PREFIX "bus type %s registered\n",
33                        type->bus->name);
34                 return 0;
35         }
36         return -ENODEV;
37 }
38
39 EXPORT_SYMBOL(register_acpi_bus_type);
40
41 int unregister_acpi_bus_type(struct acpi_bus_type *type)
42 {
43         if (acpi_disabled)
44                 return 0;
45         if (type) {
46                 down_write(&bus_type_sem);
47                 list_del_init(&type->list);
48                 up_write(&bus_type_sem);
49                 printk(KERN_INFO PREFIX "ACPI bus type %s unregistered\n",
50                        type->bus->name);
51                 return 0;
52         }
53         return -ENODEV;
54 }
55
56 EXPORT_SYMBOL(unregister_acpi_bus_type);
57
58 static struct acpi_bus_type *acpi_get_bus_type(struct bus_type *type)
59 {
60         struct acpi_bus_type *tmp, *ret = NULL;
61
62         down_read(&bus_type_sem);
63         list_for_each_entry(tmp, &bus_type_list, list) {
64                 if (tmp->bus == type) {
65                         ret = tmp;
66                         break;
67                 }
68         }
69         up_read(&bus_type_sem);
70         return ret;
71 }
72
73 static int acpi_find_bridge_device(struct device *dev, acpi_handle * handle)
74 {
75         struct acpi_bus_type *tmp;
76         int ret = -ENODEV;
77
78         down_read(&bus_type_sem);
79         list_for_each_entry(tmp, &bus_type_list, list) {
80                 if (tmp->find_bridge && !tmp->find_bridge(dev, handle)) {
81                         ret = 0;
82                         break;
83                 }
84         }
85         up_read(&bus_type_sem);
86         return ret;
87 }
88
89 /* Get device's handler per its address under its parent */
90 struct acpi_find_child {
91         acpi_handle handle;
92         acpi_integer address;
93 };
94
95 static acpi_status
96 do_acpi_find_child(acpi_handle handle, u32 lvl, void *context, void **rv)
97 {
98         acpi_status status;
99         struct acpi_device_info *info;
100         struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
101         struct acpi_find_child *find = context;
102
103         status = acpi_get_object_info(handle, &buffer);
104         if (ACPI_SUCCESS(status)) {
105                 info = buffer.pointer;
106                 if (info->address == find->address)
107                         find->handle = handle;
108                 kfree(buffer.pointer);
109         }
110         return AE_OK;
111 }
112
113 acpi_handle acpi_get_child(acpi_handle parent, acpi_integer address)
114 {
115         struct acpi_find_child find = { NULL, address };
116
117         if (!parent)
118                 return NULL;
119         acpi_walk_namespace(ACPI_TYPE_DEVICE, parent,
120                             1, do_acpi_find_child, &find, NULL);
121         return find.handle;
122 }
123
124 EXPORT_SYMBOL(acpi_get_child);
125
126 /* Link ACPI devices with physical devices */
127 static void acpi_glue_data_handler(acpi_handle handle,
128                                    u32 function, void *context)
129 {
130         /* we provide an empty handler */
131 }
132
133 /* Note: a success call will increase reference count by one */
134 struct device *acpi_get_physical_device(acpi_handle handle)
135 {
136         acpi_status status;
137         struct device *dev;
138
139         status = acpi_get_data(handle, acpi_glue_data_handler, (void **)&dev);
140         if (ACPI_SUCCESS(status))
141                 return get_device(dev);
142         return NULL;
143 }
144
145 EXPORT_SYMBOL(acpi_get_physical_device);
146
147 static int acpi_bind_one(struct device *dev, acpi_handle handle)
148 {
149         acpi_status status;
150
151         if (dev->archdata.acpi_handle) {
152                 printk(KERN_WARNING PREFIX
153                        "Drivers changed 'acpi_handle' for %s\n", dev->bus_id);
154                 return -EINVAL;
155         }
156         get_device(dev);
157         status = acpi_attach_data(handle, acpi_glue_data_handler, dev);
158         if (ACPI_FAILURE(status)) {
159                 put_device(dev);
160                 return -EINVAL;
161         }
162         dev->archdata.acpi_handle = handle;
163
164         return 0;
165 }
166
167 static int acpi_unbind_one(struct device *dev)
168 {
169         if (!dev->archdata.acpi_handle)
170                 return 0;
171         if (dev == acpi_get_physical_device(dev->archdata.acpi_handle)) {
172                 /* acpi_get_physical_device increase refcnt by one */
173                 put_device(dev);
174                 acpi_detach_data(dev->archdata.acpi_handle,
175                                  acpi_glue_data_handler);
176                 dev->archdata.acpi_handle = NULL;
177                 /* acpi_bind_one increase refcnt by one */
178                 put_device(dev);
179         } else {
180                 printk(KERN_ERR PREFIX
181                        "Oops, 'acpi_handle' corrupt for %s\n", dev->bus_id);
182         }
183         return 0;
184 }
185
186 static int acpi_platform_notify(struct device *dev)
187 {
188         struct acpi_bus_type *type;
189         acpi_handle handle;
190         int ret = -EINVAL;
191
192         if (!dev->bus || !dev->parent) {
193                 /* bridge devices genernally haven't bus or parent */
194                 ret = acpi_find_bridge_device(dev, &handle);
195                 goto end;
196         }
197         type = acpi_get_bus_type(dev->bus);
198         if (!type) {
199                 DBG("No ACPI bus support for %s\n", dev->bus_id);
200                 ret = -EINVAL;
201                 goto end;
202         }
203         if ((ret = type->find_device(dev, &handle)) != 0)
204                 DBG("Can't get handler for %s\n", dev->bus_id);
205       end:
206         if (!ret)
207                 acpi_bind_one(dev, handle);
208
209 #if ACPI_GLUE_DEBUG
210         if (!ret) {
211                 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
212
213                 acpi_get_name(dev->archdata.acpi_handle,
214                               ACPI_FULL_PATHNAME, &buffer);
215                 DBG("Device %s -> %s\n", dev->bus_id, (char *)buffer.pointer);
216                 kfree(buffer.pointer);
217         } else
218                 DBG("Device %s -> No ACPI support\n", dev->bus_id);
219 #endif
220
221         return ret;
222 }
223
224 static int acpi_platform_notify_remove(struct device *dev)
225 {
226         acpi_unbind_one(dev);
227         return 0;
228 }
229
230 static int __init init_acpi_device_notify(void)
231 {
232         if (acpi_disabled)
233                 return 0;
234         if (platform_notify || platform_notify_remove) {
235                 printk(KERN_ERR PREFIX "Can't use platform_notify\n");
236                 return 0;
237         }
238         platform_notify = acpi_platform_notify;
239         platform_notify_remove = acpi_platform_notify_remove;
240         return 0;
241 }
242
243 arch_initcall(init_acpi_device_notify);