summaryrefslogtreecommitdiffstats
path: root/src/hardware/PCIDev.c
blob: f703bc17b9771a4e50a627e25b6169e373a2c23e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/*
 * Copyright (C) 2013 Red Hat, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * Authors: Peter Schiffer <pschiffe@redhat.com>
 */

#include "PCIDev.h"

short init_pci_access(struct pci_access **acc, const int flags)
{
    struct pci_dev *dev;

    if (!acc) {
        return -1;
    }

    if (*acc) {
        return 0;
    }

    if (!(*acc = pci_alloc())) {
      return -1;
    }

    pci_init(*acc);
    pci_scan_bus(*acc);

    for (dev = (*acc)->devices; dev; dev = dev->next) {
        pci_fill_info(dev, flags);
    }

    return 0;
}

void cleanup_pci_access(struct pci_access **acc)
{
    if (!acc) {
        return;
    }

    if (*acc) {
        pci_cleanup(*acc);
    }

    *acc = NULL;
}

void get_subid(struct pci_dev *d, u16 *subvp, u16 *subdp)
{
    u8 htype = pci_read_byte(d, PCI_HEADER_TYPE) & 0x7f;

    if (htype == PCI_HEADER_TYPE_NORMAL) {
        *subvp = pci_read_word(d, PCI_SUBSYSTEM_VENDOR_ID);
        *subdp = pci_read_word(d, PCI_SUBSYSTEM_ID);
    } else if (htype == PCI_HEADER_TYPE_CARDBUS) {
        *subvp = pci_read_word(d, PCI_CB_SUBSYSTEM_VENDOR_ID);
        *subdp = pci_read_word(d, PCI_CB_SUBSYSTEM_ID);
    } else {
        *subvp = *subdp = 0xffff;
    }
}

unsigned short get_capability(const u16 pci_cap)
{
    static struct {
        unsigned short cim_val;     /* CIM value */
        u16 pci_cap;                /* pci value */
    } values[] = {
        /*
        {0,  "Unknown"},
        {1,  "Other"},
        {2,  "Supports 66MHz"},
        {3,  "Supports User Definable Features"},
        {4,  "Supports Fast Back-to-Back Transactions"},
        */
        {5,  PCI_CAP_ID_PCIX},
        {6,  PCI_CAP_ID_PM},
        {7,  PCI_CAP_ID_MSI},
        /*
        {8,  "Parity Error Recovery Capable"},
        */
        {9,  PCI_CAP_ID_AGP},
        {10, PCI_CAP_ID_VPD},
        {11, PCI_CAP_ID_SLOTID},
        {12, PCI_CAP_ID_HOTPLUG},
        {13, PCI_CAP_ID_EXP},
        /*
        {14, "Supports PCIe Gen 2"},
        {15, "Supports PCIe Gen 3"},
        */
    };

    size_t i, val_length = sizeof(values) / sizeof(values[0]);

    for (i = 0; i < val_length; i++) {
        if (pci_cap == values[i].pci_cap) {
            return values[i].cim_val;
        }
    }

    return 1; /* Other */
}