~emersion/go-drm

go-drm/ioctl.go -rw-r--r-- 4.5 KiB
7088ebfeSimon Ser Add support for Amlogic format modifiers a month ago
                                                                                
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
//+build linux dragonfly freebsd netbsd openbsd
//+build amd64 arm64

package drm

import (
	"syscall"
	"unsafe"
)

const (
	ioctlVersion      = 0xC0406400
	ioctlGetCap       = 0xC010640C
	ioctlSetClientCap = 0x4010640D

	ioctlModeGetResources        = 0xC04064A0
	ioctlModeGetCRTC             = 0xC06864A1
	ioctlModeGetEncoder          = 0xC01464A6
	ioctlModeGetConnector        = 0xC05064A7
	ioctlModeGetPlaneResources   = 0xC01064B5
	ioctlModeGetPlane            = 0xC02064B6
	ioctlModeObjectGetProperties = 0xC02064B9
	ioctlModeGetProperty         = 0xC04064AA
	ioctlModeGetBlob             = 0xC01064AC
)

func ioctl(fd uintptr, nr int, ptr unsafe.Pointer) error {
	_, _, errno := syscall.Syscall(syscall.SYS_IOCTL, fd, uintptr(nr), uintptr(ptr))
	if errno != 0 {
		return errno
	}
	return nil
}

func allocBytes(ptr **byte, len uint64) []byte {
	b := make([]byte, len)
	*ptr = (*byte)(unsafe.Pointer(&b[0]))
	return b
}

type versionResp struct {
	major, minor, patch int32
	nameLen             uint64
	name                *byte
	dateLen             uint64
	date                *byte
	descLen             uint64
	desc                *byte
}

func version(fd uintptr, v *versionResp) error {
	return ioctl(fd, ioctlVersion, unsafe.Pointer(v))
}

type getCapArg struct {
	cap uint64
	ret uint64
}

func getCap(fd uintptr, cap uint64) (uint64, error) {
	arg := getCapArg{cap: cap}
	err := ioctl(fd, ioctlGetCap, unsafe.Pointer(&arg))
	return arg.ret, err
}

type setCapArg struct {
	cap uint64
	val uint64
}

func setClientCap(fd uintptr, cap, val uint64) error {
	arg := setCapArg{cap: cap, val: val}
	return ioctl(fd, ioctlSetClientCap, unsafe.Pointer(&arg))
}

type modeCardResp struct {
	fbs, crtcs, connectors, encoders             *uint32
	fbsLen, crtcsLen, connectorsLen, encodersLen uint32
	minWidth, maxWidth, minHeight, maxHeight     uint32
}

func modeGetResources(fd uintptr, r *modeCardResp) error {
	return ioctl(fd, ioctlModeGetResources, unsafe.Pointer(r))
}

type modeModeInfo struct {
	clock                                         uint32
	hDisplay, hSyncStart, hSyncEnd, hTotal, hSkew uint16
	vDisplay, vSyncStart, vSyncEnd, vTotal, vScan uint16

	vRefresh uint32

	flags uint32
	typ   uint32
	name  [32]byte
}

type modeCRTCResp struct {
	// For ioctlModeSetCRTC
	setConnectors    *uint32
	setConnectorsLen uint32

	id uint32
	fb uint32

	x, y uint32

	gammaSize uint32
	modeValid uint32
	mode      modeModeInfo
}

func modeGetCRTC(fd uintptr, r *modeCRTCResp) error {
	return ioctl(fd, ioctlModeGetCRTC, unsafe.Pointer(r))
}

type modeEncoderResp struct {
	id  uint32
	typ uint32

	crtc uint32

	possibleCRTCs, possibleClones uint32
}

func modeGetEncoder(fd uintptr, r *modeEncoderResp) error {
	return ioctl(fd, ioctlModeGetEncoder, unsafe.Pointer(r))
}

type modeConnectorResp struct {
	encoders   *uint32
	modes      *modeModeInfo
	propIDs    *uint32
	propValues *uint64

	modesLen    uint32
	propsLen    uint32
	encodersLen uint32

	encoder uint32
	id      uint32
	typ     uint32
	typeID  uint32

	status              uint32
	phyWidth, phyHeight uint32
	subpixel            uint32

	_ uint32
}

func modeGetConnector(fd uintptr, r *modeConnectorResp) error {
	return ioctl(fd, ioctlModeGetConnector, unsafe.Pointer(r))
}

type modePlaneResourcesResp struct {
	planes    *uint32
	planesLen uint32
}

func modeGetPlaneResources(fd uintptr, r *modePlaneResourcesResp) error {
	return ioctl(fd, ioctlModeGetPlaneResources, unsafe.Pointer(r))
}

type modePlaneResp struct {
	id uint32

	crtc uint32
	fb   uint32

	possibleCRTCs uint32
	gammaSize     uint32

	formatsLen uint32
	formats    *uint32
}

func modeGetPlane(fd uintptr, r *modePlaneResp) error {
	return ioctl(fd, ioctlModeGetPlane, unsafe.Pointer(r))
}

type modeObjectGetPropertiesResp struct {
	propIDs    *uint32
	propValues *uint64
	propsLen   uint32

	id  uint32
	typ uint32
}

func modeObjectGetProperties(fd uintptr, r *modeObjectGetPropertiesResp) error {
	return ioctl(fd, ioctlModeObjectGetProperties, unsafe.Pointer(r))
}

type modePropertyEnum struct {
	value uint64
	name  [32]byte
}

type modeGetPropertyResp struct {
	values    uintptr // values and blob lengths
	enumBlobs uintptr // enum items and blob IDs

	id    uint32
	flags uint32
	name  [32]byte

	valuesLen, enumBlobsLen uint32
}

func modeGetProperty(fd uintptr, r *modeGetPropertyResp) error {
	return ioctl(fd, ioctlModeGetProperty, unsafe.Pointer(r))
}

type modeGetBlobResp struct {
	id   uint32
	size uint32
	data *byte
}

func modeGetBlob(fd uintptr, r *modeGetBlobResp) error {
	return ioctl(fd, ioctlModeGetBlob, unsafe.Pointer(r))
}