Subversion Repositories shark

Rev

Rev 457 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
428 giacomo 1
/*
2
 *      PCI searching functions.
3
 *
4
 *      Copyright 1993 -- 1997 Drew Eckhardt, Frederic Potter,
5
 *                              David Mosberger-Tang
6
 *      Copyright 1997 -- 2000 Martin Mares <mj@ucw.cz>
7
 *      Copyright 2003 -- Greg Kroah-Hartman <greg@kroah.com>
8
 */
9
 
430 giacomo 10
#include <linuxcomp.h>
11
 
428 giacomo 12
#include <linux/init.h>
13
#include <linux/pci.h>
14
#include <linux/module.h>
15
#include <linux/interrupt.h>
16
 
17
spinlock_t pci_bus_lock = SPIN_LOCK_UNLOCKED;
18
 
19
static struct pci_bus * __devinit
20
pci_do_find_bus(struct pci_bus* bus, unsigned char busnr)
21
{
22
        struct pci_bus* child;
23
        struct list_head *tmp;
24
 
25
        if(bus->number == busnr)
26
                return bus;
27
 
28
        list_for_each(tmp, &bus->children) {
29
                child = pci_do_find_bus(pci_bus_b(tmp), busnr);
30
                if(child)
31
                        return child;
32
        }
33
        return NULL;
34
}
35
 
36
/**
37
 * pci_find_bus - locate PCI bus from a given domain and bus number
38
 * @domain: number of PCI domain to search
39
 * @busnr: number of desired PCI bus
40
 *
41
 * Given a PCI bus number and domain number, the desired PCI bus is located
42
 * in the global list of PCI buses.  If the bus is found, a pointer to its
43
 * data structure is returned.  If no bus is found, %NULL is returned.
44
 */
45
struct pci_bus * __devinit pci_find_bus(int domain, int busnr)
46
{
47
        struct pci_bus *bus = NULL;
48
        struct pci_bus *tmp_bus;
49
 
50
        while ((bus = pci_find_next_bus(bus)) != NULL)  {
456 giacomo 51
                if (pci_domain_nr(bus) != domain)
428 giacomo 52
                        continue;
53
                tmp_bus = pci_do_find_bus(bus, busnr);
54
                if (tmp_bus)
55
                        return tmp_bus;
56
        }
57
        return NULL;
58
}
59
 
60
/**
61
 * pci_find_next_bus - begin or continue searching for a PCI bus
62
 * @from: Previous PCI bus found, or %NULL for new search.
63
 *
64
 * Iterates through the list of known PCI busses.  A new search is
65
 * initiated by passing %NULL to the @from argument.  Otherwise if
66
 * @from is not %NULL, searches continue from next device on the
67
 * global list.
68
 */
69
struct pci_bus *
70
pci_find_next_bus(const struct pci_bus *from)
71
{
72
        struct list_head *n;
73
        struct pci_bus *b = NULL;
74
 
457 giacomo 75
        //WARN_ON(in_interrupt());
428 giacomo 76
        spin_lock(&pci_bus_lock);
77
        n = from ? from->node.next : pci_root_buses.next;
78
        if (n != &pci_root_buses)
79
                b = pci_bus_b(n);
80
        spin_unlock(&pci_bus_lock);
81
        return b;
82
}
83
 
84
/**
85
 * pci_find_slot - locate PCI device from a given PCI slot
86
 * @bus: number of PCI bus on which desired PCI device resides
87
 * @devfn: encodes number of PCI slot in which the desired PCI
88
 * device resides and the logical device number within that slot
89
 * in case of multi-function devices.
90
 *
91
 * Given a PCI bus and slot/function number, the desired PCI device
92
 * is located in system global list of PCI devices.  If the device
93
 * is found, a pointer to its data structure is returned.  If no
94
 * device is found, %NULL is returned.
95
 */
96
struct pci_dev *
97
pci_find_slot(unsigned int bus, unsigned int devfn)
98
{
99
        struct pci_dev *dev = NULL;
100
 
101
        while ((dev = pci_find_device(PCI_ANY_ID, PCI_ANY_ID, dev)) != NULL) {
102
                if (dev->bus->number == bus && dev->devfn == devfn)
103
                        return dev;
104
        }
105
        return NULL;
106
}
107
 
108
/**
109
 * pci_find_subsys - begin or continue searching for a PCI device by vendor/subvendor/device/subdevice id
110
 * @vendor: PCI vendor id to match, or %PCI_ANY_ID to match all vendor ids
111
 * @device: PCI device id to match, or %PCI_ANY_ID to match all device ids
112
 * @ss_vendor: PCI subsystem vendor id to match, or %PCI_ANY_ID to match all vendor ids
113
 * @ss_device: PCI subsystem device id to match, or %PCI_ANY_ID to match all device ids
114
 * @from: Previous PCI device found in search, or %NULL for new search.
115
 *
116
 * Iterates through the list of known PCI devices.  If a PCI device is
117
 * found with a matching @vendor, @device, @ss_vendor and @ss_device, a pointer to its
118
 * device structure is returned.  Otherwise, %NULL is returned.
119
 * A new search is initiated by passing %NULL to the @from argument.
120
 * Otherwise if @from is not %NULL, searches continue from next device on the global list.
121
 *
122
 * NOTE: Do not use this function anymore, use pci_get_subsys() instead, as
123
 * the pci device returned by this function can disappear at any moment in
124
 * time.
125
 */
126
struct pci_dev *
127
pci_find_subsys(unsigned int vendor, unsigned int device,
128
                unsigned int ss_vendor, unsigned int ss_device,
129
                const struct pci_dev *from)
130
{
131
        struct list_head *n;
132
        struct pci_dev *dev;
133
 
457 giacomo 134
        //WARN_ON(in_interrupt());
428 giacomo 135
        spin_lock(&pci_bus_lock);
136
        n = from ? from->global_list.next : pci_devices.next;
137
 
138
        while (n && (n != &pci_devices)) {
139
                dev = pci_dev_g(n);
140
                if ((vendor == PCI_ANY_ID || dev->vendor == vendor) &&
141
                    (device == PCI_ANY_ID || dev->device == device) &&
142
                    (ss_vendor == PCI_ANY_ID || dev->subsystem_vendor == ss_vendor) &&
143
                    (ss_device == PCI_ANY_ID || dev->subsystem_device == ss_device))
144
                        goto exit;
145
                n = n->next;
146
        }
147
        dev = NULL;
148
exit:
149
        spin_unlock(&pci_bus_lock);
150
        return dev;
151
}
152
 
153
/**
154
 * pci_find_device - begin or continue searching for a PCI device by vendor/device id
155
 * @vendor: PCI vendor id to match, or %PCI_ANY_ID to match all vendor ids
156
 * @device: PCI device id to match, or %PCI_ANY_ID to match all device ids
157
 * @from: Previous PCI device found in search, or %NULL for new search.
158
 *
159
 * Iterates through the list of known PCI devices.  If a PCI device is
160
 * found with a matching @vendor and @device, a pointer to its device structure is
161
 * returned.  Otherwise, %NULL is returned.
162
 * A new search is initiated by passing %NULL to the @from argument.
163
 * Otherwise if @from is not %NULL, searches continue from next device on the global list.
164
 *
165
 * NOTE: Do not use this function anymore, use pci_get_device() instead, as
166
 * the pci device returned by this function can disappear at any moment in
167
 * time.
168
 */
169
struct pci_dev *
170
pci_find_device(unsigned int vendor, unsigned int device, const struct pci_dev *from)
171
{
172
        return pci_find_subsys(vendor, device, PCI_ANY_ID, PCI_ANY_ID, from);
173
}
174
 
175
/**
176
 * pci_get_subsys - begin or continue searching for a PCI device by vendor/subvendor/device/subdevice id
177
 * @vendor: PCI vendor id to match, or %PCI_ANY_ID to match all vendor ids
178
 * @device: PCI device id to match, or %PCI_ANY_ID to match all device ids
179
 * @ss_vendor: PCI subsystem vendor id to match, or %PCI_ANY_ID to match all vendor ids
180
 * @ss_device: PCI subsystem device id to match, or %PCI_ANY_ID to match all device ids
181
 * @from: Previous PCI device found in search, or %NULL for new search.
182
 *
183
 * Iterates through the list of known PCI devices.  If a PCI device is
184
 * found with a matching @vendor, @device, @ss_vendor and @ss_device, a pointer to its
185
 * device structure is returned, and the reference count to the device is
186
 * incremented.  Otherwise, %NULL is returned.  A new search is initiated by
187
 * passing %NULL to the @from argument.  Otherwise if @from is not %NULL,
188
 * searches continue from next device on the global list.
189
 * The reference count for @from is always decremented if it is not %NULL.
190
 */
191
struct pci_dev *
192
pci_get_subsys(unsigned int vendor, unsigned int device,
193
               unsigned int ss_vendor, unsigned int ss_device,
194
               struct pci_dev *from)
195
{
196
        struct list_head *n;
197
        struct pci_dev *dev;
198
 
457 giacomo 199
        //WARN_ON(in_interrupt());
428 giacomo 200
        spin_lock(&pci_bus_lock);
201
        n = from ? from->global_list.next : pci_devices.next;
202
 
203
        while (n && (n != &pci_devices)) {
204
                dev = pci_dev_g(n);
205
                if ((vendor == PCI_ANY_ID || dev->vendor == vendor) &&
206
                    (device == PCI_ANY_ID || dev->device == device) &&
207
                    (ss_vendor == PCI_ANY_ID || dev->subsystem_vendor == ss_vendor) &&
208
                    (ss_device == PCI_ANY_ID || dev->subsystem_device == ss_device))
209
                        goto exit;
210
                n = n->next;
211
        }
212
        dev = NULL;
213
exit:
214
        pci_dev_put(from);
215
        dev = pci_dev_get(dev);
216
        spin_unlock(&pci_bus_lock);
217
        return dev;
218
}
219
 
220
/**
221
 * pci_get_device - begin or continue searching for a PCI device by vendor/device id
222
 * @vendor: PCI vendor id to match, or %PCI_ANY_ID to match all vendor ids
223
 * @device: PCI device id to match, or %PCI_ANY_ID to match all device ids
224
 * @from: Previous PCI device found in search, or %NULL for new search.
225
 *
226
 * Iterates through the list of known PCI devices.  If a PCI device is
227
 * found with a matching @vendor and @device, a pointer to its device structure is
228
 * returned.  Otherwise, %NULL is returned.
229
 * A new search is initiated by passing %NULL to the @from argument.
230
 * Otherwise if @from is not %NULL, searches continue from next device on the global list.
231
 *
232
 * Iterates through the list of known PCI devices.  If a PCI device is
233
 * found with a matching @vendor and @device, the reference count to the
234
 * device is incremented and a pointer to its device structure is returned.
235
 * Otherwise, %NULL is returned.  A new search is initiated by passing %NULL
236
 * to the @from argument.  Otherwise if @from is not %NULL, searches continue
237
 * from next device on the global list.  The reference count for @from is
238
 * always decremented if it is not %NULL.
239
 */
240
struct pci_dev *
241
pci_get_device(unsigned int vendor, unsigned int device, struct pci_dev *from)
242
{
243
        return pci_get_subsys(vendor, device, PCI_ANY_ID, PCI_ANY_ID, from);
244
}
245
 
246
 
247
/**
248
 * pci_find_device_reverse - begin or continue searching for a PCI device by vendor/device id
249
 * @vendor: PCI vendor id to match, or %PCI_ANY_ID to match all vendor ids
250
 * @device: PCI device id to match, or %PCI_ANY_ID to match all device ids
251
 * @from: Previous PCI device found in search, or %NULL for new search.
252
 *
253
 * Iterates through the list of known PCI devices in the reverse order of pci_find_device().
254
 * If a PCI device is found with a matching @vendor and @device, a pointer to
255
 * its device structure is returned.  Otherwise, %NULL is returned.
256
 * A new search is initiated by passing %NULL to the @from argument.
257
 * Otherwise if @from is not %NULL, searches continue from previous device on the global list.
258
 */
259
struct pci_dev *
260
pci_find_device_reverse(unsigned int vendor, unsigned int device, const struct pci_dev *from)
261
{
262
        struct list_head *n;
263
        struct pci_dev *dev;
264
 
457 giacomo 265
        //WARN_ON(in_interrupt());
428 giacomo 266
        spin_lock(&pci_bus_lock);
267
        n = from ? from->global_list.prev : pci_devices.prev;
268
 
269
        while (n && (n != &pci_devices)) {
270
                dev = pci_dev_g(n);
271
                if ((vendor == PCI_ANY_ID || dev->vendor == vendor) &&
272
                    (device == PCI_ANY_ID || dev->device == device))
273
                        goto exit;
274
                n = n->prev;
275
        }
276
        dev = NULL;
277
exit:
278
        spin_unlock(&pci_bus_lock);
279
        return dev;
280
}
281
 
282
 
283
/**
284
 * pci_find_class - begin or continue searching for a PCI device by class
285
 * @class: search for a PCI device with this class designation
286
 * @from: Previous PCI device found in search, or %NULL for new search.
287
 *
288
 * Iterates through the list of known PCI devices.  If a PCI device is
289
 * found with a matching @class, a pointer to its device structure is
290
 * returned.  Otherwise, %NULL is returned.
291
 * A new search is initiated by passing %NULL to the @from argument.
292
 * Otherwise if @from is not %NULL, searches continue from next device
293
 * on the global list.
294
 */
295
struct pci_dev *
296
pci_find_class(unsigned int class, const struct pci_dev *from)
297
{
298
        struct list_head *n;
299
        struct pci_dev *dev;
300
 
301
        spin_lock(&pci_bus_lock);
302
        n = from ? from->global_list.next : pci_devices.next;
303
 
304
        while (n && (n != &pci_devices)) {
305
                dev = pci_dev_g(n);
306
                if (dev->class == class)
307
                        goto exit;
308
                n = n->next;
309
        }
310
        dev = NULL;
311
exit:
312
        spin_unlock(&pci_bus_lock);
313
        return dev;
314
}
315
 
316
EXPORT_SYMBOL(pci_find_bus);
317
EXPORT_SYMBOL(pci_find_class);
318
EXPORT_SYMBOL(pci_find_device);
319
EXPORT_SYMBOL(pci_find_device_reverse);
320
EXPORT_SYMBOL(pci_find_slot);
321
EXPORT_SYMBOL(pci_find_subsys);
322
EXPORT_SYMBOL(pci_get_device);
323
EXPORT_SYMBOL(pci_get_subsys);