source: rtems/cpukit/libpci/pci_for_each.c @ 02550220

4.115
Last change on this file since 02550220 was a31845f7, checked in by Daniel Hellstrom <daniel@…>, on 11/28/11 at 09:11:10

LIBPCI: added PCI layer to cpukit/libpci

  • Property mode set to 100644
File size: 1.4 KB
Line 
1/*  PCI Help Function, iterate over all PCI devices. Find devices by cfg access.
2 *
3 *  COPYRIGHT (c) 2010.
4 *  Cobham Gaisler AB.
5 *
6 *  The license and distribution terms for this file may be
7 *  found in the file LICENSE in this distribution or at
8 *  http://www.rtems.com/license/LICENSE.
9 */
10
11#include <pci.h>
12#include <pci/access.h>
13
14/*#define DEBUG*/
15
16#ifdef DEBUG
17#include <rtems/bspIo.h>
18#define DBG(args...) printk(args)
19#else
20#define DBG(args...)
21#endif
22
23int pci_for_each(int (*func)(pci_dev_t, void*), void *arg)
24{
25        uint32_t id;
26        uint8_t hd;
27        int bus, dev, fun, result, fail;
28        int maxbus = pci_bus_count();
29        pci_dev_t pcidev;
30
31        for (bus = 0; bus < maxbus ; bus++) {
32                for (dev = 0; dev < PCI_MAX_DEVICES; dev++) {
33                        pcidev = PCI_DEV(bus, dev, 0);
34
35                        for (fun = 0; fun < PCI_MAX_FUNCTIONS; fun++, pcidev++) {
36                                fail = pci_cfg_r32(pcidev, PCI_VENDOR_ID, &id);
37                                if (fail || (0xffffffff == id) || (0 == id)) {
38                                        if (fun == 0)
39                                                break;
40                                        else
41                                                continue;
42                                }
43
44                                DBG("pcibus_for_each: found 0x%08lx at"
45                                    " %d/%d/%d\n", id, bus, dev, fun);
46                                result = func(pcidev, arg);
47                                if (result != 0)
48                                        return result; /* Stopped */
49
50                                /* Stop if not a multi-function device */
51                                if (fun == 0) {
52                                        pci_cfg_r8(pcidev, PCI_HEADER_TYPE,
53                                                        &hd);
54                                        if ((hd & PCI_MULTI_FUNCTION) == 0)
55                                                break;
56                                }
57                        }
58                }
59        }
60
61        return 0; /* scanned all */
62}
Note: See TracBrowser for help on using the repository browser.