[ACPI] whitespace
[linux-2.6] / drivers / acpi / pci_bind.c
1 /*
2  *  pci_bind.c - ACPI PCI Device Binding ($Revision: 2 $)
3  *
4  *  Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
5  *  Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
6  *
7  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8  *
9  *  This program is free software; you can redistribute it and/or modify
10  *  it under the terms of the GNU General Public License as published by
11  *  the Free Software Foundation; either version 2 of the License, or (at
12  *  your option) any later version.
13  *
14  *  This program is distributed in the hope that it will be useful, but
15  *  WITHOUT ANY WARRANTY; without even the implied warranty of
16  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  *  General Public License for more details.
18  *
19  *  You should have received a copy of the GNU General Public License along
20  *  with this program; if not, write to the Free Software Foundation, Inc.,
21  *  59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
22  *
23  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
24  */
25
26 #include <linux/kernel.h>
27 #include <linux/module.h>
28 #include <linux/init.h>
29 #include <linux/types.h>
30 #include <linux/proc_fs.h>
31 #include <linux/spinlock.h>
32 #include <linux/pm.h>
33 #include <linux/pci.h>
34 #include <linux/acpi.h>
35 #include <acpi/acpi_bus.h>
36 #include <acpi/acpi_drivers.h>
37
38
39 #define _COMPONENT              ACPI_PCI_COMPONENT
40 ACPI_MODULE_NAME                ("pci_bind")
41
42 struct acpi_pci_data {
43         struct acpi_pci_id      id;
44         struct pci_bus          *bus;
45         struct pci_dev          *dev;
46 };
47
48
49 void
50 acpi_pci_data_handler (
51         acpi_handle             handle,
52         u32                     function,
53         void                    *context)
54 {
55         ACPI_FUNCTION_TRACE("acpi_pci_data_handler");
56
57         /* TBD: Anything we need to do here? */
58
59         return_VOID;
60 }
61
62
63 /**
64  * acpi_get_pci_id
65  * ------------------
66  * This function is used by the ACPI Interpreter (a.k.a. Core Subsystem)
67  * to resolve PCI information for ACPI-PCI devices defined in the namespace.
68  * This typically occurs when resolving PCI operation region information.
69  */
70 acpi_status
71 acpi_get_pci_id (
72         acpi_handle             handle,
73         struct acpi_pci_id      *id)
74 {
75         int                     result = 0;
76         acpi_status             status = AE_OK;
77         struct acpi_device      *device = NULL;
78         struct acpi_pci_data    *data = NULL;
79
80         ACPI_FUNCTION_TRACE("acpi_get_pci_id");
81
82         if (!id)
83                 return_ACPI_STATUS(AE_BAD_PARAMETER);
84
85         result = acpi_bus_get_device(handle, &device);
86         if (result) {
87                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, 
88                         "Invalid ACPI Bus context for device %s\n",
89                         acpi_device_bid(device)));
90                 return_ACPI_STATUS(AE_NOT_EXIST);
91         }
92
93         status = acpi_get_data(handle, acpi_pci_data_handler, (void**) &data);
94         if (ACPI_FAILURE(status) || !data) {
95                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, 
96                         "Invalid ACPI-PCI context for device %s\n",
97                         acpi_device_bid(device)));
98                 return_ACPI_STATUS(status);
99         }
100
101         *id = data->id;
102         
103         /*
104         id->segment = data->id.segment;
105         id->bus = data->id.bus;
106         id->device = data->id.device;
107         id->function = data->id.function;
108         */
109
110         ACPI_DEBUG_PRINT((ACPI_DB_INFO, 
111                 "Device %s has PCI address %02x:%02x:%02x.%02x\n", 
112                 acpi_device_bid(device), id->segment, id->bus, 
113                 id->device, id->function));
114
115         return_ACPI_STATUS(AE_OK);
116 }
117 EXPORT_SYMBOL(acpi_get_pci_id);
118
119         
120 int
121 acpi_pci_bind (
122         struct acpi_device      *device)
123 {
124         int                     result = 0;
125         acpi_status             status = AE_OK;
126         struct acpi_pci_data    *data = NULL;
127         struct acpi_pci_data    *pdata = NULL;
128         char                    *pathname = NULL;
129         struct acpi_buffer      buffer = {0, NULL};
130         acpi_handle             handle = NULL;
131         struct pci_dev          *dev;
132         struct pci_bus          *bus;
133
134         ACPI_FUNCTION_TRACE("acpi_pci_bind");
135
136         if (!device || !device->parent)
137                 return_VALUE(-EINVAL);
138
139         pathname = kmalloc(ACPI_PATHNAME_MAX, GFP_KERNEL);
140         if(!pathname)
141                 return_VALUE(-ENOMEM);
142         memset(pathname, 0, ACPI_PATHNAME_MAX);
143         buffer.length = ACPI_PATHNAME_MAX;
144         buffer.pointer = pathname;
145
146         data = kmalloc(sizeof(struct acpi_pci_data), GFP_KERNEL);
147         if (!data){
148                 kfree (pathname);
149                 return_VALUE(-ENOMEM);
150         }
151         memset(data, 0, sizeof(struct acpi_pci_data));
152
153         acpi_get_name(device->handle, ACPI_FULL_PATHNAME, &buffer);
154         ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Binding PCI device [%s]...\n", 
155                 pathname));
156
157         /* 
158          * Segment & Bus
159          * -------------
160          * These are obtained via the parent device's ACPI-PCI context.
161          */
162         status = acpi_get_data(device->parent->handle, acpi_pci_data_handler, 
163                 (void**) &pdata);
164         if (ACPI_FAILURE(status) || !pdata || !pdata->bus) {
165                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, 
166                         "Invalid ACPI-PCI context for parent device %s\n",
167                         acpi_device_bid(device->parent)));
168                 result = -ENODEV;
169                 goto end;
170         }
171         data->id.segment = pdata->id.segment;
172         data->id.bus = pdata->bus->number;
173
174         /*
175          * Device & Function
176          * -----------------
177          * These are simply obtained from the device's _ADR method.  Note
178          * that a value of zero is valid.
179          */
180         data->id.device = device->pnp.bus_address >> 16;
181         data->id.function = device->pnp.bus_address & 0xFFFF;
182
183         ACPI_DEBUG_PRINT((ACPI_DB_INFO, "...to %02x:%02x:%02x.%02x\n",
184                 data->id.segment, data->id.bus, data->id.device, 
185                 data->id.function));
186
187         /*
188          * TBD: Support slot devices (e.g. function=0xFFFF).
189          */
190
191         /* 
192          * Locate PCI Device
193          * -----------------
194          * Locate matching device in PCI namespace.  If it doesn't exist
195          * this typically means that the device isn't currently inserted
196          * (e.g. docking station, port replicator, etc.).
197          * We cannot simply search the global pci device list, since
198          * PCI devices are added to the global pci list when the root
199          * bridge start ops are run, which may not have happened yet.
200          */
201         bus = pci_find_bus(data->id.segment, data->id.bus);
202         if (bus) {
203                 list_for_each_entry(dev, &bus->devices, bus_list) {
204                         if (dev->devfn == PCI_DEVFN(data->id.device,
205                                                 data->id.function)) {
206                                 data->dev = dev;
207                                 break;
208                         }
209                 }
210         }
211         if (!data->dev) {
212                 ACPI_DEBUG_PRINT((ACPI_DB_INFO, 
213                         "Device %02x:%02x:%02x.%02x not present in PCI namespace\n",
214                         data->id.segment, data->id.bus, 
215                         data->id.device, data->id.function));
216                 result = -ENODEV;
217                 goto end;
218         }
219         if (!data->dev->bus) {
220                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, 
221                         "Device %02x:%02x:%02x.%02x has invalid 'bus' field\n",
222                         data->id.segment, data->id.bus, 
223                         data->id.device, data->id.function));
224                 result = -ENODEV;
225                 goto end;
226         }
227
228         /*
229          * PCI Bridge?
230          * -----------
231          * If so, set the 'bus' field and install the 'bind' function to 
232          * facilitate callbacks for all of its children.
233          */
234         if (data->dev->subordinate) {
235                 ACPI_DEBUG_PRINT((ACPI_DB_INFO, 
236                         "Device %02x:%02x:%02x.%02x is a PCI bridge\n",
237                         data->id.segment, data->id.bus, 
238                         data->id.device, data->id.function));
239                 data->bus = data->dev->subordinate;
240                 device->ops.bind = acpi_pci_bind;
241                 device->ops.unbind = acpi_pci_unbind;
242         }
243
244         /*
245          * Attach ACPI-PCI Context
246          * -----------------------
247          * Thus binding the ACPI and PCI devices.
248          */
249         status = acpi_attach_data(device->handle, acpi_pci_data_handler, data);
250         if (ACPI_FAILURE(status)) {
251                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
252                         "Unable to attach ACPI-PCI context to device %s\n",
253                         acpi_device_bid(device)));
254                 result = -ENODEV;
255                 goto end;
256         }
257
258         /*
259          * PCI Routing Table
260          * -----------------
261          * Evaluate and parse _PRT, if exists.  This code is independent of 
262          * PCI bridges (above) to allow parsing of _PRT objects within the
263          * scope of non-bridge devices.  Note that _PRTs within the scope of
264          * a PCI bridge assume the bridge's subordinate bus number.
265          *
266          * TBD: Can _PRTs exist within the scope of non-bridge PCI devices?
267          */
268         status = acpi_get_handle(device->handle, METHOD_NAME__PRT, &handle);
269         if (ACPI_SUCCESS(status)) {
270                 if (data->bus)                              /* PCI-PCI bridge */
271                         acpi_pci_irq_add_prt(device->handle, data->id.segment, 
272                                 data->bus->number);
273                 else                                 /* non-bridge PCI device */
274                         acpi_pci_irq_add_prt(device->handle, data->id.segment,
275                                 data->id.bus);
276         }
277
278 end:
279         kfree(pathname);
280         if (result)
281                 kfree(data);
282
283         return_VALUE(result);
284 }
285
286 int acpi_pci_unbind(
287         struct acpi_device      *device)
288 {
289         int                     result = 0;
290         acpi_status             status = AE_OK;
291         struct acpi_pci_data    *data = NULL;
292         char                    *pathname = NULL;
293         struct acpi_buffer      buffer = {0, NULL};
294
295         ACPI_FUNCTION_TRACE("acpi_pci_unbind");
296
297         if (!device || !device->parent)
298                 return_VALUE(-EINVAL);
299
300         pathname = (char *) kmalloc(ACPI_PATHNAME_MAX, GFP_KERNEL);
301         if(!pathname)
302                 return_VALUE(-ENOMEM);
303         memset(pathname, 0, ACPI_PATHNAME_MAX);
304
305         buffer.length = ACPI_PATHNAME_MAX;
306         buffer.pointer = pathname;
307         acpi_get_name(device->handle, ACPI_FULL_PATHNAME, &buffer);
308         ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Unbinding PCI device [%s]...\n",
309                 pathname));
310         kfree(pathname);
311
312         status = acpi_get_data(device->handle, acpi_pci_data_handler, (void**)&data);
313         if (ACPI_FAILURE(status)) {
314                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
315                         "Unable to get data from device %s\n",
316                         acpi_device_bid(device)));
317                 result = -ENODEV;
318                 goto end;
319         }
320
321         status = acpi_detach_data(device->handle, acpi_pci_data_handler);
322         if (ACPI_FAILURE(status)) {
323                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
324                         "Unable to detach data from device %s\n",
325                         acpi_device_bid(device)));
326                 result = -ENODEV;
327                 goto end;
328         }
329         if (data->dev->subordinate) {
330                 acpi_pci_irq_del_prt(data->id.segment, data->bus->number);
331         }
332         kfree(data);
333
334 end:
335         return_VALUE(result);
336 }
337
338 int 
339 acpi_pci_bind_root (
340         struct acpi_device      *device,
341         struct acpi_pci_id      *id,
342         struct pci_bus          *bus) 
343 {
344         int                     result = 0;
345         acpi_status             status = AE_OK;
346         struct acpi_pci_data    *data = NULL;
347         char                    *pathname = NULL;
348         struct acpi_buffer      buffer = {0, NULL};
349
350         ACPI_FUNCTION_TRACE("acpi_pci_bind_root");
351
352         pathname = (char *)kmalloc(ACPI_PATHNAME_MAX, GFP_KERNEL);
353         if(!pathname)
354                 return_VALUE(-ENOMEM);
355         memset(pathname, 0, ACPI_PATHNAME_MAX);
356
357         buffer.length = ACPI_PATHNAME_MAX;
358         buffer.pointer = pathname;
359
360         if (!device || !id || !bus){
361                 kfree(pathname);
362                 return_VALUE(-EINVAL);
363         }
364
365         data = kmalloc(sizeof(struct acpi_pci_data), GFP_KERNEL);
366         if (!data){
367                 kfree(pathname);
368                 return_VALUE(-ENOMEM);
369         }
370         memset(data, 0, sizeof(struct acpi_pci_data));
371
372         data->id = *id;
373         data->bus = bus;
374         device->ops.bind = acpi_pci_bind;
375         device->ops.unbind = acpi_pci_unbind;
376
377         acpi_get_name(device->handle, ACPI_FULL_PATHNAME, &buffer);
378
379         ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Binding PCI root bridge [%s] to "
380                 "%02x:%02x\n", pathname, id->segment, id->bus));
381
382         status = acpi_attach_data(device->handle, acpi_pci_data_handler, data);
383         if (ACPI_FAILURE(status)) {
384                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
385                         "Unable to attach ACPI-PCI context to device %s\n",
386                         pathname));
387                 result = -ENODEV;
388                 goto end;
389         }
390
391 end:
392         kfree(pathname);
393         if (result != 0)
394                 kfree(data);
395
396         return_VALUE(result);
397 }