blob: f8ae2b7db0a718b1ec22edb6aabbe68f7845a82d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * PCI searching functions.
3 *
4 * Copyright (C) 1993 -- 1997 Drew Eckhardt, Frederic Potter,
5 * David Mosberger-Tang
6 * Copyright (C) 1997 -- 2000 Martin Mares <mj@ucw.cz>
7 * Copyright (C) 2003 -- 2004 Greg Kroah-Hartman <greg@kroah.com>
8 */
9
10#include <linux/init.h>
11#include <linux/pci.h>
12#include <linux/module.h>
13#include <linux/interrupt.h>
14#include "pci.h"
15
Zhang Yanmind71374d2006-06-02 12:35:43 +080016DECLARE_RWSEM(pci_bus_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017
18static struct pci_bus * __devinit
19pci_do_find_bus(struct pci_bus* bus, unsigned char busnr)
20{
21 struct pci_bus* child;
22 struct list_head *tmp;
23
24 if(bus->number == busnr)
25 return bus;
26
27 list_for_each(tmp, &bus->children) {
28 child = pci_do_find_bus(pci_bus_b(tmp), busnr);
29 if(child)
30 return child;
31 }
32 return NULL;
33}
34
35/**
36 * pci_find_bus - locate PCI bus from a given domain and bus number
37 * @domain: number of PCI domain to search
38 * @busnr: number of desired PCI bus
39 *
40 * Given a PCI bus number and domain number, the desired PCI bus is located
41 * in the global list of PCI buses. If the bus is found, a pointer to its
42 * data structure is returned. If no bus is found, %NULL is returned.
43 */
44struct pci_bus * __devinit pci_find_bus(int domain, int busnr)
45{
46 struct pci_bus *bus = NULL;
47 struct pci_bus *tmp_bus;
48
49 while ((bus = pci_find_next_bus(bus)) != NULL) {
50 if (pci_domain_nr(bus) != domain)
51 continue;
52 tmp_bus = pci_do_find_bus(bus, busnr);
53 if (tmp_bus)
54 return tmp_bus;
55 }
56 return NULL;
57}
58
59/**
60 * pci_find_next_bus - begin or continue searching for a PCI bus
61 * @from: Previous PCI bus found, or %NULL for new search.
62 *
63 * Iterates through the list of known PCI busses. A new search is
Randy Dunlapd75763d2006-07-30 03:03:41 -070064 * initiated by passing %NULL as the @from argument. Otherwise if
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 * @from is not %NULL, searches continue from next device on the
66 * global list.
67 */
68struct pci_bus *
69pci_find_next_bus(const struct pci_bus *from)
70{
71 struct list_head *n;
72 struct pci_bus *b = NULL;
73
74 WARN_ON(in_interrupt());
Zhang Yanmind71374d2006-06-02 12:35:43 +080075 down_read(&pci_bus_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 n = from ? from->node.next : pci_root_buses.next;
77 if (n != &pci_root_buses)
78 b = pci_bus_b(n);
Zhang Yanmind71374d2006-06-02 12:35:43 +080079 up_read(&pci_bus_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 return b;
81}
82
83/**
84 * pci_find_slot - locate PCI device from a given PCI slot
85 * @bus: number of PCI bus on which desired PCI device resides
86 * @devfn: encodes number of PCI slot in which the desired PCI
87 * device resides and the logical device number within that slot
88 * in case of multi-function devices.
89 *
90 * Given a PCI bus and slot/function number, the desired PCI device
91 * is located in system global list of PCI devices. If the device
92 * is found, a pointer to its data structure is returned. If no
93 * device is found, %NULL is returned.
94 */
95struct pci_dev *
96pci_find_slot(unsigned int bus, unsigned int devfn)
97{
98 struct pci_dev *dev = NULL;
99
100 while ((dev = pci_find_device(PCI_ANY_ID, PCI_ANY_ID, dev)) != NULL) {
101 if (dev->bus->number == bus && dev->devfn == devfn)
102 return dev;
103 }
104 return NULL;
105}
106
107/**
108 * pci_get_slot - locate PCI device for a given PCI slot
109 * @bus: PCI bus on which desired PCI device resides
110 * @devfn: encodes number of PCI slot in which the desired PCI
111 * device resides and the logical device number within that slot
112 * in case of multi-function devices.
113 *
114 * Given a PCI bus and slot/function number, the desired PCI device
115 * is located in the list of PCI devices.
116 * If the device is found, its reference count is increased and this
117 * function returns a pointer to its data structure. The caller must
118 * decrement the reference count by calling pci_dev_put().
119 * If no device is found, %NULL is returned.
120 */
121struct pci_dev * pci_get_slot(struct pci_bus *bus, unsigned int devfn)
122{
123 struct list_head *tmp;
124 struct pci_dev *dev;
125
126 WARN_ON(in_interrupt());
Zhang Yanmind71374d2006-06-02 12:35:43 +0800127 down_read(&pci_bus_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
129 list_for_each(tmp, &bus->devices) {
130 dev = pci_dev_b(tmp);
131 if (dev->devfn == devfn)
132 goto out;
133 }
134
135 dev = NULL;
136 out:
137 pci_dev_get(dev);
Zhang Yanmind71374d2006-06-02 12:35:43 +0800138 up_read(&pci_bus_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 return dev;
140}
141
142/**
143 * pci_find_subsys - begin or continue searching for a PCI device by vendor/subvendor/device/subdevice id
144 * @vendor: PCI vendor id to match, or %PCI_ANY_ID to match all vendor ids
145 * @device: PCI device id to match, or %PCI_ANY_ID to match all device ids
146 * @ss_vendor: PCI subsystem vendor id to match, or %PCI_ANY_ID to match all vendor ids
147 * @ss_device: PCI subsystem device id to match, or %PCI_ANY_ID to match all device ids
148 * @from: Previous PCI device found in search, or %NULL for new search.
149 *
150 * Iterates through the list of known PCI devices. If a PCI device is
Randy Dunlapd75763d2006-07-30 03:03:41 -0700151 * found with a matching @vendor, @device, @ss_vendor and @ss_device, a
152 * pointer to its device structure is returned. Otherwise, %NULL is returned.
153 * A new search is initiated by passing %NULL as the @from argument.
154 * Otherwise if @from is not %NULL, searches continue from next device
155 * on the global list.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 *
Randy Dunlapd75763d2006-07-30 03:03:41 -0700157 * NOTE: Do not use this function any more; use pci_get_subsys() instead, as
158 * the PCI device returned by this function can disappear at any moment in
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 * time.
160 */
161static struct pci_dev * pci_find_subsys(unsigned int vendor,
162 unsigned int device,
163 unsigned int ss_vendor,
164 unsigned int ss_device,
165 const struct pci_dev *from)
166{
167 struct list_head *n;
168 struct pci_dev *dev;
169
170 WARN_ON(in_interrupt());
Zhang Yanmind71374d2006-06-02 12:35:43 +0800171 down_read(&pci_bus_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 n = from ? from->global_list.next : pci_devices.next;
173
174 while (n && (n != &pci_devices)) {
175 dev = pci_dev_g(n);
176 if ((vendor == PCI_ANY_ID || dev->vendor == vendor) &&
177 (device == PCI_ANY_ID || dev->device == device) &&
178 (ss_vendor == PCI_ANY_ID || dev->subsystem_vendor == ss_vendor) &&
179 (ss_device == PCI_ANY_ID || dev->subsystem_device == ss_device))
180 goto exit;
181 n = n->next;
182 }
183 dev = NULL;
184exit:
Zhang Yanmind71374d2006-06-02 12:35:43 +0800185 up_read(&pci_bus_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 return dev;
187}
188
189/**
190 * pci_find_device - begin or continue searching for a PCI device by vendor/device id
191 * @vendor: PCI vendor id to match, or %PCI_ANY_ID to match all vendor ids
192 * @device: PCI device id to match, or %PCI_ANY_ID to match all device ids
193 * @from: Previous PCI device found in search, or %NULL for new search.
194 *
Randy Dunlapd75763d2006-07-30 03:03:41 -0700195 * Iterates through the list of known PCI devices. If a PCI device is found
196 * with a matching @vendor and @device, a pointer to its device structure is
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 * returned. Otherwise, %NULL is returned.
Randy Dunlapd75763d2006-07-30 03:03:41 -0700198 * A new search is initiated by passing %NULL as the @from argument.
199 * Otherwise if @from is not %NULL, searches continue from next device
200 * on the global list.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 *
Randy Dunlapd75763d2006-07-30 03:03:41 -0700202 * NOTE: Do not use this function any more; use pci_get_device() instead, as
203 * the PCI device returned by this function can disappear at any moment in
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 * time.
205 */
206struct pci_dev *
207pci_find_device(unsigned int vendor, unsigned int device, const struct pci_dev *from)
208{
209 return pci_find_subsys(vendor, device, PCI_ANY_ID, PCI_ANY_ID, from);
210}
211
212/**
213 * pci_get_subsys - begin or continue searching for a PCI device by vendor/subvendor/device/subdevice id
214 * @vendor: PCI vendor id to match, or %PCI_ANY_ID to match all vendor ids
215 * @device: PCI device id to match, or %PCI_ANY_ID to match all device ids
216 * @ss_vendor: PCI subsystem vendor id to match, or %PCI_ANY_ID to match all vendor ids
217 * @ss_device: PCI subsystem device id to match, or %PCI_ANY_ID to match all device ids
218 * @from: Previous PCI device found in search, or %NULL for new search.
219 *
Randy Dunlapd75763d2006-07-30 03:03:41 -0700220 * Iterates through the list of known PCI devices. If a PCI device is found
221 * with a matching @vendor, @device, @ss_vendor and @ss_device, a pointer to its
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 * device structure is returned, and the reference count to the device is
223 * incremented. Otherwise, %NULL is returned. A new search is initiated by
Randy Dunlapd75763d2006-07-30 03:03:41 -0700224 * passing %NULL as the @from argument. Otherwise if @from is not %NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 * searches continue from next device on the global list.
226 * The reference count for @from is always decremented if it is not %NULL.
227 */
228struct pci_dev *
229pci_get_subsys(unsigned int vendor, unsigned int device,
230 unsigned int ss_vendor, unsigned int ss_device,
231 struct pci_dev *from)
232{
233 struct list_head *n;
234 struct pci_dev *dev;
235
236 WARN_ON(in_interrupt());
Zhang Yanmind71374d2006-06-02 12:35:43 +0800237 down_read(&pci_bus_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 n = from ? from->global_list.next : pci_devices.next;
239
240 while (n && (n != &pci_devices)) {
241 dev = pci_dev_g(n);
242 if ((vendor == PCI_ANY_ID || dev->vendor == vendor) &&
243 (device == PCI_ANY_ID || dev->device == device) &&
244 (ss_vendor == PCI_ANY_ID || dev->subsystem_vendor == ss_vendor) &&
245 (ss_device == PCI_ANY_ID || dev->subsystem_device == ss_device))
246 goto exit;
247 n = n->next;
248 }
249 dev = NULL;
250exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 dev = pci_dev_get(dev);
Zhang Yanmind71374d2006-06-02 12:35:43 +0800252 up_read(&pci_bus_sem);
Alan Sternb89b7ea2006-02-23 17:12:51 -0500253 pci_dev_put(from);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 return dev;
255}
256
257/**
258 * pci_get_device - begin or continue searching for a PCI device by vendor/device id
259 * @vendor: PCI vendor id to match, or %PCI_ANY_ID to match all vendor ids
260 * @device: PCI device id to match, or %PCI_ANY_ID to match all device ids
261 * @from: Previous PCI device found in search, or %NULL for new search.
262 *
263 * Iterates through the list of known PCI devices. If a PCI device is
264 * found with a matching @vendor and @device, the reference count to the
265 * device is incremented and a pointer to its device structure is returned.
266 * Otherwise, %NULL is returned. A new search is initiated by passing %NULL
Randy Dunlapd75763d2006-07-30 03:03:41 -0700267 * as the @from argument. Otherwise if @from is not %NULL, searches continue
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 * from next device on the global list. The reference count for @from is
269 * always decremented if it is not %NULL.
270 */
271struct pci_dev *
272pci_get_device(unsigned int vendor, unsigned int device, struct pci_dev *from)
273{
274 return pci_get_subsys(vendor, device, PCI_ANY_ID, PCI_ANY_ID, from);
275}
276
277
278/**
279 * pci_find_device_reverse - begin or continue searching for a PCI device by vendor/device id
280 * @vendor: PCI vendor id to match, or %PCI_ANY_ID to match all vendor ids
281 * @device: PCI device id to match, or %PCI_ANY_ID to match all device ids
282 * @from: Previous PCI device found in search, or %NULL for new search.
283 *
Randy Dunlapd75763d2006-07-30 03:03:41 -0700284 * Iterates through the list of known PCI devices in the reverse order of
285 * pci_find_device().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 * If a PCI device is found with a matching @vendor and @device, a pointer to
287 * its device structure is returned. Otherwise, %NULL is returned.
Randy Dunlapd75763d2006-07-30 03:03:41 -0700288 * A new search is initiated by passing %NULL as the @from argument.
289 * Otherwise if @from is not %NULL, searches continue from previous device
290 * on the global list.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 */
292struct pci_dev *
293pci_find_device_reverse(unsigned int vendor, unsigned int device, const struct pci_dev *from)
294{
295 struct list_head *n;
296 struct pci_dev *dev;
297
298 WARN_ON(in_interrupt());
Zhang Yanmind71374d2006-06-02 12:35:43 +0800299 down_read(&pci_bus_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 n = from ? from->global_list.prev : pci_devices.prev;
301
302 while (n && (n != &pci_devices)) {
303 dev = pci_dev_g(n);
304 if ((vendor == PCI_ANY_ID || dev->vendor == vendor) &&
305 (device == PCI_ANY_ID || dev->device == device))
306 goto exit;
307 n = n->prev;
308 }
309 dev = NULL;
310exit:
Zhang Yanmind71374d2006-06-02 12:35:43 +0800311 up_read(&pci_bus_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 return dev;
313}
314
315/**
316 * pci_get_class - begin or continue searching for a PCI device by class
317 * @class: search for a PCI device with this class designation
318 * @from: Previous PCI device found in search, or %NULL for new search.
319 *
320 * Iterates through the list of known PCI devices. If a PCI device is
321 * found with a matching @class, the reference count to the device is
322 * incremented and a pointer to its device structure is returned.
323 * Otherwise, %NULL is returned.
Randy Dunlapd75763d2006-07-30 03:03:41 -0700324 * A new search is initiated by passing %NULL as the @from argument.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 * Otherwise if @from is not %NULL, searches continue from next device
326 * on the global list. The reference count for @from is always decremented
327 * if it is not %NULL.
328 */
329struct pci_dev *pci_get_class(unsigned int class, struct pci_dev *from)
330{
331 struct list_head *n;
332 struct pci_dev *dev;
333
334 WARN_ON(in_interrupt());
Zhang Yanmind71374d2006-06-02 12:35:43 +0800335 down_read(&pci_bus_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 n = from ? from->global_list.next : pci_devices.next;
337
338 while (n && (n != &pci_devices)) {
339 dev = pci_dev_g(n);
340 if (dev->class == class)
341 goto exit;
342 n = n->next;
343 }
344 dev = NULL;
345exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 dev = pci_dev_get(dev);
Zhang Yanmind71374d2006-06-02 12:35:43 +0800347 up_read(&pci_bus_sem);
Alan Sternb89b7ea2006-02-23 17:12:51 -0500348 pci_dev_put(from);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 return dev;
350}
351
352/**
353 * pci_dev_present - Returns 1 if device matching the device list is present, 0 if not.
354 * @ids: A pointer to a null terminated list of struct pci_device_id structures
355 * that describe the type of PCI device the caller is trying to find.
356 *
357 * Obvious fact: You do not have a reference to any device that might be found
358 * by this function, so if that device is removed from the system right after
359 * this function is finished, the value will be stale. Use this function to
360 * find devices that are usually built into a system, or for a general hint as
361 * to if another device happens to be present at this specific moment in time.
362 */
363int pci_dev_present(const struct pci_device_id *ids)
364{
365 struct pci_dev *dev;
366 int found = 0;
367
368 WARN_ON(in_interrupt());
Zhang Yanmind71374d2006-06-02 12:35:43 +0800369 down_read(&pci_bus_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 while (ids->vendor || ids->subvendor || ids->class_mask) {
371 list_for_each_entry(dev, &pci_devices, global_list) {
372 if (pci_match_one_device(ids, dev)) {
373 found = 1;
374 goto exit;
375 }
376 }
377 ids++;
378 }
Zhang Yanmind71374d2006-06-02 12:35:43 +0800379exit:
380 up_read(&pci_bus_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 return found;
382}
383EXPORT_SYMBOL(pci_dev_present);
384
385EXPORT_SYMBOL(pci_find_bus);
Prarit Bhargavae07d01e2005-07-06 15:28:40 -0700386EXPORT_SYMBOL(pci_find_next_bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387EXPORT_SYMBOL(pci_find_device);
388EXPORT_SYMBOL(pci_find_device_reverse);
389EXPORT_SYMBOL(pci_find_slot);
390EXPORT_SYMBOL(pci_get_device);
391EXPORT_SYMBOL(pci_get_subsys);
392EXPORT_SYMBOL(pci_get_slot);
393EXPORT_SYMBOL(pci_get_class);