[ACPI] Lindent all ACPI files
[deliverable/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/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 PCI root bridge's handle from its segment and bus number */
90 struct acpi_find_pci_root {
91 unsigned int seg;
92 unsigned int bus;
93 acpi_handle handle;
94 };
95
96 static acpi_status
97 do_root_bridge_busnr_callback(struct acpi_resource *resource, void *data)
98 {
99 int *busnr = (int *)data;
100 struct acpi_resource_address64 address;
101
102 if (resource->id != ACPI_RSTYPE_ADDRESS16 &&
103 resource->id != ACPI_RSTYPE_ADDRESS32 &&
104 resource->id != ACPI_RSTYPE_ADDRESS64)
105 return AE_OK;
106
107 acpi_resource_to_address64(resource, &address);
108 if ((address.address_length > 0) &&
109 (address.resource_type == ACPI_BUS_NUMBER_RANGE))
110 *busnr = address.min_address_range;
111
112 return AE_OK;
113 }
114
115 static int get_root_bridge_busnr(acpi_handle handle)
116 {
117 acpi_status status;
118 int bus, bbn;
119 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
120
121 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
122
123 status = acpi_evaluate_integer(handle, METHOD_NAME__BBN, NULL,
124 (unsigned long *)&bbn);
125 if (status == AE_NOT_FOUND) {
126 /* Assume bus = 0 */
127 printk(KERN_INFO PREFIX
128 "Assume root bridge [%s] bus is 0\n",
129 (char *)buffer.pointer);
130 status = AE_OK;
131 bbn = 0;
132 }
133 if (ACPI_FAILURE(status)) {
134 bbn = -ENODEV;
135 goto exit;
136 }
137 if (bbn > 0)
138 goto exit;
139
140 /* _BBN in some systems return 0 for all root bridges */
141 bus = -1;
142 status = acpi_walk_resources(handle, METHOD_NAME__CRS,
143 do_root_bridge_busnr_callback, &bus);
144 /* If _CRS failed, we just use _BBN */
145 if (ACPI_FAILURE(status) || (bus == -1))
146 goto exit;
147 /* We select _CRS */
148 if (bbn != bus) {
149 printk(KERN_INFO PREFIX
150 "_BBN and _CRS returns different value for %s. Select _CRS\n",
151 (char *)buffer.pointer);
152 bbn = bus;
153 }
154 exit:
155 acpi_os_free(buffer.pointer);
156 return bbn;
157 }
158
159 static acpi_status
160 find_pci_rootbridge(acpi_handle handle, u32 lvl, void *context, void **rv)
161 {
162 struct acpi_find_pci_root *find = (struct acpi_find_pci_root *)context;
163 unsigned long seg, bus;
164 acpi_status status;
165 int tmp;
166 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
167
168 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
169
170 status = acpi_evaluate_integer(handle, METHOD_NAME__SEG, NULL, &seg);
171 if (status == AE_NOT_FOUND) {
172 /* Assume seg = 0 */
173 printk(KERN_INFO PREFIX
174 "Assume root bridge [%s] segment is 0\n",
175 (char *)buffer.pointer);
176 status = AE_OK;
177 seg = 0;
178 }
179 if (ACPI_FAILURE(status)) {
180 status = AE_CTRL_DEPTH;
181 goto exit;
182 }
183
184 tmp = get_root_bridge_busnr(handle);
185 if (tmp < 0) {
186 printk(KERN_ERR PREFIX
187 "Find root bridge failed for %s\n",
188 (char *)buffer.pointer);
189 status = AE_CTRL_DEPTH;
190 goto exit;
191 }
192 bus = tmp;
193
194 if (seg == find->seg && bus == find->bus)
195 find->handle = handle;
196 status = AE_OK;
197 exit:
198 acpi_os_free(buffer.pointer);
199 return status;
200 }
201
202 acpi_handle acpi_get_pci_rootbridge_handle(unsigned int seg, unsigned int bus)
203 {
204 struct acpi_find_pci_root find = { seg, bus, NULL };
205
206 acpi_get_devices(PCI_ROOT_HID_STRING, find_pci_rootbridge, &find, NULL);
207 return find.handle;
208 }
209
210 /* Get device's handler per its address under its parent */
211 struct acpi_find_child {
212 acpi_handle handle;
213 acpi_integer address;
214 };
215
216 static acpi_status
217 do_acpi_find_child(acpi_handle handle, u32 lvl, void *context, void **rv)
218 {
219 acpi_status status;
220 struct acpi_device_info *info;
221 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
222 struct acpi_find_child *find = (struct acpi_find_child *)context;
223
224 status = acpi_get_object_info(handle, &buffer);
225 if (ACPI_SUCCESS(status)) {
226 info = buffer.pointer;
227 if (info->address == find->address)
228 find->handle = handle;
229 acpi_os_free(buffer.pointer);
230 }
231 return AE_OK;
232 }
233
234 acpi_handle acpi_get_child(acpi_handle parent, acpi_integer address)
235 {
236 struct acpi_find_child find = { NULL, address };
237
238 if (!parent)
239 return NULL;
240 acpi_walk_namespace(ACPI_TYPE_DEVICE, parent,
241 1, do_acpi_find_child, &find, NULL);
242 return find.handle;
243 }
244
245 EXPORT_SYMBOL(acpi_get_child);
246
247 /* Link ACPI devices with physical devices */
248 static void acpi_glue_data_handler(acpi_handle handle,
249 u32 function, void *context)
250 {
251 /* we provide an empty handler */
252 }
253
254 /* Note: a success call will increase reference count by one */
255 struct device *acpi_get_physical_device(acpi_handle handle)
256 {
257 acpi_status status;
258 struct device *dev;
259
260 status = acpi_get_data(handle, acpi_glue_data_handler, (void **)&dev);
261 if (ACPI_SUCCESS(status))
262 return get_device(dev);
263 return NULL;
264 }
265
266 EXPORT_SYMBOL(acpi_get_physical_device);
267
268 static int acpi_bind_one(struct device *dev, acpi_handle handle)
269 {
270 acpi_status status;
271
272 if (dev->firmware_data) {
273 printk(KERN_WARNING PREFIX
274 "Drivers changed 'firmware_data' for %s\n", dev->bus_id);
275 return -EINVAL;
276 }
277 get_device(dev);
278 status = acpi_attach_data(handle, acpi_glue_data_handler, dev);
279 if (ACPI_FAILURE(status)) {
280 put_device(dev);
281 return -EINVAL;
282 }
283 dev->firmware_data = handle;
284
285 return 0;
286 }
287
288 static int acpi_unbind_one(struct device *dev)
289 {
290 if (!dev->firmware_data)
291 return 0;
292 if (dev == acpi_get_physical_device(dev->firmware_data)) {
293 /* acpi_get_physical_device increase refcnt by one */
294 put_device(dev);
295 acpi_detach_data(dev->firmware_data, acpi_glue_data_handler);
296 dev->firmware_data = NULL;
297 /* acpi_bind_one increase refcnt by one */
298 put_device(dev);
299 } else {
300 printk(KERN_ERR PREFIX
301 "Oops, 'firmware_data' corrupt for %s\n", dev->bus_id);
302 }
303 return 0;
304 }
305
306 static int acpi_platform_notify(struct device *dev)
307 {
308 struct acpi_bus_type *type;
309 acpi_handle handle;
310 int ret = -EINVAL;
311
312 if (!dev->bus || !dev->parent) {
313 /* bridge devices genernally haven't bus or parent */
314 ret = acpi_find_bridge_device(dev, &handle);
315 goto end;
316 }
317 type = acpi_get_bus_type(dev->bus);
318 if (!type) {
319 DBG("No ACPI bus support for %s\n", dev->bus_id);
320 ret = -EINVAL;
321 goto end;
322 }
323 if ((ret = type->find_device(dev, &handle)) != 0)
324 DBG("Can't get handler for %s\n", dev->bus_id);
325 end:
326 if (!ret)
327 acpi_bind_one(dev, handle);
328
329 #if ACPI_GLUE_DEBUG
330 if (!ret) {
331 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
332
333 acpi_get_name(dev->firmware_data, ACPI_FULL_PATHNAME, &buffer);
334 DBG("Device %s -> %s\n", dev->bus_id, (char *)buffer.pointer);
335 acpi_os_free(buffer.pointer);
336 } else
337 DBG("Device %s -> No ACPI support\n", dev->bus_id);
338 #endif
339
340 return ret;
341 }
342
343 static int acpi_platform_notify_remove(struct device *dev)
344 {
345 acpi_unbind_one(dev);
346 return 0;
347 }
348
349 static int __init init_acpi_device_notify(void)
350 {
351 if (acpi_disabled)
352 return 0;
353 if (platform_notify || platform_notify_remove) {
354 printk(KERN_ERR PREFIX "Can't use platform_notify\n");
355 return 0;
356 }
357 platform_notify = acpi_platform_notify;
358 platform_notify_remove = acpi_platform_notify_remove;
359 return 0;
360 }
361
362 arch_initcall(init_acpi_device_notify);
This page took 0.0443210000000001 seconds and 5 git commands to generate.