-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathdrm.go
161 lines (135 loc) · 2.86 KB
/
drm.go
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package drm
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"strconv"
"strings"
"unsafe"
"github.com/NeowayLabs/drm/ioctl"
)
type (
version struct {
Major int32
Minor int32
Patch int32
namelen int64
name uintptr
datelen int64
date uintptr
desclen int64
desc uintptr
}
// Version of DRM driver
Version struct {
version
Major, Minor, Patch int32
Name string // Name of the driver (eg.: i915)
Date string
Desc string
}
)
const (
driPath = "/dev/dri"
)
func Available() (Version, error) {
f, err := OpenCard(0)
if err != nil {
// handle backward linux compat?
// check /proc/dri/0 ?
return Version{}, err
}
defer f.Close()
return GetVersion(f)
}
func OpenCard(n int) (*os.File, error) {
return open(fmt.Sprintf("%s/card%d", driPath, n))
}
func OpenControlDev(n int) (*os.File, error) {
return open(fmt.Sprintf("%s/controlD%d", driPath, n))
}
func OpenRenderDev(n int) (*os.File, error) {
return open(fmt.Sprintf("%s/renderD%d", driPath, n))
}
func open(path string) (*os.File, error) {
return os.OpenFile(path, os.O_RDWR, 0)
}
func GetVersion(file *os.File) (Version, error) {
var (
name, date, desc []byte
)
version := &version{}
err := ioctl.Do(uintptr(file.Fd()), uintptr(IOCTLVersion),
uintptr(unsafe.Pointer(version)))
if err != nil {
return Version{}, err
}
if version.namelen > 0 {
name = make([]byte, version.namelen+1)
version.name = uintptr(unsafe.Pointer(&name[0]))
}
if version.datelen > 0 {
date = make([]byte, version.datelen+1)
version.date = uintptr(unsafe.Pointer(&date[0]))
}
if version.desclen > 0 {
desc = make([]byte, version.desclen+1)
version.desc = uintptr(unsafe.Pointer(&desc[0]))
}
err = ioctl.Do(uintptr(file.Fd()), uintptr(IOCTLVersion),
uintptr(unsafe.Pointer(version)))
if err != nil {
return Version{}, err
}
// remove C null byte at end
name = name[:version.namelen]
date = date[:version.datelen]
desc = desc[:version.desclen]
nozero := func(r rune) bool {
if r == 0 {
return true
} else {
return false
}
}
v := Version{
version: *version,
Major: version.Major,
Minor: version.Minor,
Patch: version.Patch,
Name: string(bytes.TrimFunc(name, nozero)),
Date: string(bytes.TrimFunc(date, nozero)),
Desc: string(bytes.TrimFunc(desc, nozero)),
}
return v, nil
}
func ListDevices() []Version {
var devices []Version
files, err := ioutil.ReadDir(driPath)
if err != nil {
return devices
}
for _, finfo := range files {
name := finfo.Name()
if len(name) <= 4 ||
!strings.HasPrefix(name, "card") {
continue
}
idstr := name[4:]
id, err := strconv.Atoi(idstr)
if err != nil {
continue
}
devfile, err := OpenCard(id)
if err != nil {
continue
}
dev, err := GetVersion(devfile)
if err != nil {
continue
}
devices = append(devices, dev)
}
return devices
}