forked from jaypipes/ghw
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpci.go
More file actions
103 lines (93 loc) · 2.48 KB
/
Copy pathpci.go
File metadata and controls
103 lines (93 loc) · 2.48 KB
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
//
// Use and distribution licensed under the Apache license version 2.
//
// See the COPYING file in the root project directory for full text.
//
package ghw
import (
"fmt"
"regexp"
"strings"
"github.com/jaypipes/pcidb"
)
var (
RE_PCI_ADDRESS *regexp.Regexp = regexp.MustCompile(
"^(([0-9a-f]{0,4}):)?([0-9a-f]{2}):([0-9a-f]{2})\\.([0-9a-f]{1})$",
)
)
type PCIDevice struct {
Address string // The PCI address of the device
Vendor *pcidb.PCIVendor
Product *pcidb.PCIProduct
Subsystem *pcidb.PCIProduct // optional subvendor/sub-device information
Class *pcidb.PCIClass
Subclass *pcidb.PCISubclass // optional sub-class for the device
ProgrammingInterface *pcidb.PCIProgrammingInterface // optional programming interface
}
func (di *PCIDevice) String() string {
vendorName := "<unknown>"
if di.Vendor != nil {
vendorName = di.Vendor.Name
}
productName := "<unknown>"
if di.Product != nil {
productName = di.Product.Name
}
className := "<unknown>"
if di.Class != nil {
className = di.Class.Name
}
return fmt.Sprintf(
"%s -> class: '%s' vendor: '%s' product: '%s'",
di.Address,
className,
vendorName,
productName,
)
}
type PCIInfo struct {
// hash of class ID -> class information
Classes map[string]*pcidb.PCIClass
// hash of vendor ID -> vendor information
Vendors map[string]*pcidb.PCIVendor
// hash of vendor ID + product/device ID -> product information
Products map[string]*pcidb.PCIProduct
}
type PCIAddress struct {
Domain string
Bus string
Slot string
Function string
}
// Given a string address, returns a complete PCIAddress struct, filled in with
// domain, bus, slot and function components. The address string may either
// be in $BUS:$SLOT.$FUNCTION (BSF) format or it can be a full PCI address
// that includes the 4-digit $DOMAIN information as well:
// $DOMAIN:$BUS:$SLOT.$FUNCTION.
//
// Returns "" if the address string wasn't a valid PCI address.
func PCIAddressFromString(address string) *PCIAddress {
addrLowered := strings.ToLower(address)
matches := RE_PCI_ADDRESS.FindStringSubmatch(addrLowered)
if len(matches) == 6 {
dom := "0000"
if matches[1] != "" {
dom = matches[2]
}
return &PCIAddress{
Domain: dom,
Bus: matches[3],
Slot: matches[4],
Function: matches[5],
}
}
return nil
}
func PCI() (*PCIInfo, error) {
info := &PCIInfo{}
err := pciFillInfo(info)
if err != nil {
return nil, err
}
return info, nil
}