1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3  *  Driver for the NXP SAA7164 PCIe bridge
4  *
5  *  Copyright (c) 2010-2015 Steven Toth <stoth@kernellabs.com>
6  */
7 
8 /* TODO: Cleanup and shorten the namespace */
9 
10 /* Some structues are passed directly to/from the firmware and
11  * have strict alignment requirements. This is one of them.
12  */
13 struct tmComResHWDescr {
14 	u8	bLength;
15 	u8	bDescriptorType;
16 	u8	bDescriptorSubtype;
17 	u16	bcdSpecVersion;
18 	u32	dwClockFrequency;
19 	u32	dwClockUpdateRes;
20 	u8	bCapabilities;
21 	u32	dwDeviceRegistersLocation;
22 	u32	dwHostMemoryRegion;
23 	u32	dwHostMemoryRegionSize;
24 	u32	dwHostHibernatMemRegion;
25 	u32	dwHostHibernatMemRegionSize;
26 } __attribute__((packed));
27 
28 /* This is DWORD aligned on windows but I can't find the right
29  * gcc syntax to match the binary data from the device.
30  * I've manually padded with Reserved[3] bytes to match the hardware,
31  * but this could break if GCC decies to pack in a different way.
32  */
33 struct tmComResInterfaceDescr {
34 	u8	bLength;
35 	u8	bDescriptorType;
36 	u8	bDescriptorSubtype;
37 	u8	bFlags;
38 	u8	bInterfaceType;
39 	u8	bInterfaceId;
40 	u8	bBaseInterface;
41 	u8	bInterruptId;
42 	u8	bDebugInterruptId;
43 	u8	BARLocation;
44 	u8	Reserved[3];
45 };
46 
47 struct tmComResBusDescr {
48 	u64	CommandRing;
49 	u64	ResponseRing;
50 	u32	CommandWrite;
51 	u32	CommandRead;
52 	u32	ResponseWrite;
53 	u32	ResponseRead;
54 };
55 
56 enum tmBusType {
57 	NONE		= 0,
58 	TYPE_BUS_PCI	= 1,
59 	TYPE_BUS_PCIe	= 2,
60 	TYPE_BUS_USB	= 3,
61 	TYPE_BUS_I2C	= 4
62 };
63 
64 struct tmComResBusInfo {
65 	enum tmBusType Type;
66 	u16	m_wMaxReqSize;
67 	u8 __iomem *m_pdwSetRing;
68 	u32	m_dwSizeSetRing;
69 	u8 __iomem *m_pdwGetRing;
70 	u32	m_dwSizeGetRing;
71 	u32	m_dwSetWritePos;
72 	u32	m_dwSetReadPos;
73 	u32	m_dwGetWritePos;
74 	u32	m_dwGetReadPos;
75 
76 	/* All access is protected */
77 	struct mutex lock;
78 
79 };
80 
81 struct tmComResInfo {
82 	u8	id;
83 	u8	flags;
84 	u16	size;
85 	u32	command;
86 	u16	controlselector;
87 	u8	seqno;
88 } __attribute__((packed));
89 
90 enum tmComResCmd {
91 	SET_CUR  = 0x01,
92 	GET_CUR  = 0x81,
93 	GET_MIN  = 0x82,
94 	GET_MAX  = 0x83,
95 	GET_RES  = 0x84,
96 	GET_LEN  = 0x85,
97 	GET_INFO = 0x86,
98 	GET_DEF  = 0x87
99 };
100 
101 struct cmd {
102 	u8 seqno;
103 	u32 inuse;
104 	u32 timeout;
105 	u32 signalled;
106 	struct mutex lock;
107 	wait_queue_head_t wait;
108 };
109 
110 struct tmDescriptor {
111 	u32	pathid;
112 	u32	size;
113 	void	*descriptor;
114 };
115 
116 struct tmComResDescrHeader {
117 	u8	len;
118 	u8	type;
119 	u8	subtype;
120 	u8	unitid;
121 } __attribute__((packed));
122 
123 struct tmComResExtDevDescrHeader {
124 	u8	len;
125 	u8	type;
126 	u8	subtype;
127 	u8	unitid;
128 	u32	devicetype;
129 	u16	deviceid;
130 	u32	numgpiopins;
131 	u8	numgpiogroups;
132 	u8	controlsize;
133 } __attribute__((packed));
134 
135 struct tmComResGPIO {
136 	u32	pin;
137 	u8	state;
138 } __attribute__((packed));
139 
140 struct tmComResPathDescrHeader {
141 	u8	len;
142 	u8	type;
143 	u8	subtype;
144 	u8	pathid;
145 } __attribute__((packed));
146 
147 /* terminaltype */
148 enum tmComResTermType {
149 	ITT_ANTENNA              = 0x0203,
150 	LINE_CONNECTOR           = 0x0603,
151 	SPDIF_CONNECTOR          = 0x0605,
152 	COMPOSITE_CONNECTOR      = 0x0401,
153 	SVIDEO_CONNECTOR         = 0x0402,
154 	COMPONENT_CONNECTOR      = 0x0403,
155 	STANDARD_DMA             = 0xF101
156 };
157 
158 struct tmComResAntTermDescrHeader {
159 	u8	len;
160 	u8	type;
161 	u8	subtype;
162 	u8	terminalid;
163 	u16	terminaltype;
164 	u8	assocterminal;
165 	u8	iterminal;
166 	u8	controlsize;
167 } __attribute__((packed));
168 
169 struct tmComResTunerDescrHeader {
170 	u8	len;
171 	u8	type;
172 	u8	subtype;
173 	u8	unitid;
174 	u8	sourceid;
175 	u8	iunit;
176 	u32	tuningstandards;
177 	u8	controlsize;
178 	u32	controls;
179 } __attribute__((packed));
180 
181 enum tmBufferFlag {
182 	/* the buffer does not contain any valid data */
183 	TM_BUFFER_FLAG_EMPTY,
184 
185 	/* the buffer is filled with valid data */
186 	TM_BUFFER_FLAG_DONE,
187 
188 	/* the buffer is the dummy buffer - TODO??? */
189 	TM_BUFFER_FLAG_DUMMY_BUFFER
190 };
191 
192 struct tmBuffer {
193 	u64		*pagetablevirt;
194 	u64		pagetablephys;
195 	u16		offset;
196 	u8		*context;
197 	u64		timestamp;
198 	enum tmBufferFlag BufferFlag;
199 	u32		lostbuffers;
200 	u32		validbuffers;
201 	u64		*dummypagevirt;
202 	u64		dummypagephys;
203 	u64		*addressvirt;
204 };
205 
206 struct tmHWStreamParameters {
207 	u32	bitspersample;
208 	u32	samplesperline;
209 	u32	numberoflines;
210 	u32	pitch;
211 	u32	linethreshold;
212 	u64	**pagetablelistvirt;
213 	u64	*pagetablelistphys;
214 	u32	numpagetables;
215 	u32	numpagetableentries;
216 };
217 
218 struct tmStreamParameters {
219 	struct tmHWStreamParameters	HWStreamParameters;
220 	u64				qwDummyPageTablePhys;
221 	u64				*pDummyPageTableVirt;
222 };
223 
224 struct tmComResDMATermDescrHeader {
225 	u8	len;
226 	u8	type;
227 	u8	subtyle;
228 	u8	unitid;
229 	u16	terminaltype;
230 	u8	assocterminal;
231 	u8	sourceid;
232 	u8	iterminal;
233 	u32	BARLocation;
234 	u8	flags;
235 	u8	interruptid;
236 	u8	buffercount;
237 	u8	metadatasize;
238 	u8	numformats;
239 	u8	controlsize;
240 } __attribute__((packed));
241 
242 /*
243  *
244  * Description:
245  *  This is the transport stream format header.
246  *
247  * Settings:
248  *  bLength                 - The size of this descriptor in bytes.
249  *  bDescriptorType         - CS_INTERFACE.
250  *  bDescriptorSubtype      - VS_FORMAT_MPEG2TS descriptor subtype.
251  *  bFormatIndex            - A non-zero constant that uniquely identifies the
252  *                            format.
253  *  bDataOffset             - Offset to TSP packet within MPEG-2 TS transport
254  *                            stride, in bytes.
255  *  bPacketLength           - Length of TSP packet, in bytes (typically 188).
256  *  bStrideLength           - Length of MPEG-2 TS transport stride.
257  *  guidStrideFormat        - A Globally Unique Identifier indicating the
258  *                            format of the stride data (if any). Set to zeros
259  *                            if there is no Stride Data, or if the Stride
260  *                            Data is to be ignored by the application.
261  *
262  */
263 struct tmComResTSFormatDescrHeader {
264 	u8	len;
265 	u8	type;
266 	u8	subtype;
267 	u8	bFormatIndex;
268 	u8	bDataOffset;
269 	u8	bPacketLength;
270 	u8	bStrideLength;
271 	u8	guidStrideFormat[16];
272 } __attribute__((packed));
273 
274 /* Encoder related structures */
275 
276 /* A/V Mux Selector */
277 struct tmComResSelDescrHeader {
278 	u8	len;
279 	u8	type;
280 	u8	subtype;
281 	u8	unitid;
282 	u8	nrinpins;
283 	u8	sourceid;
284 } __attribute__((packed));
285 
286 /* A/V Audio processor definitions */
287 struct tmComResProcDescrHeader {
288 	u8	len;
289 	u8	type;
290 	u8	subtype;
291 	u8	unitid;
292 	u8	sourceid;
293 	u16	wreserved;
294 	u8	controlsize;
295 } __attribute__((packed));
296 
297 /* Video bitrate control message */
298 #define EU_VIDEO_BIT_RATE_MODE_CONSTANT		(0)
299 #define EU_VIDEO_BIT_RATE_MODE_VARIABLE_AVERAGE (1)
300 #define EU_VIDEO_BIT_RATE_MODE_VARIABLE_PEAK	(2)
301 struct tmComResEncVideoBitRate {
302 	u8	ucVideoBitRateMode;
303 	u32	dwVideoBitRate;
304 	u32	dwVideoBitRatePeak;
305 } __attribute__((packed));
306 
307 /* Video Encoder Aspect Ratio message */
308 struct tmComResEncVideoInputAspectRatio {
309 	u8	width;
310 	u8	height;
311 } __attribute__((packed));
312 
313 /* Video Encoder GOP IBP message */
314 /* 1. IPPPPPPPPPPPPPP */
315 /* 2. IBPBPBPBPBPBPBP */
316 /* 3. IBBPBBPBBPBBP   */
317 #define SAA7164_ENCODER_DEFAULT_GOP_DIST (1)
318 #define SAA7164_ENCODER_DEFAULT_GOP_SIZE (15)
319 struct tmComResEncVideoGopStructure {
320 	u8	ucGOPSize;	/* GOP Size 12, 15 */
321 	u8	ucRefFrameDist; /* Reference Frame Distance */
322 } __attribute__((packed));
323 
324 /* Encoder processor definition */
325 struct tmComResEncoderDescrHeader {
326 	u8	len;
327 	u8	type;
328 	u8	subtype;
329 	u8	unitid;
330 	u8	vsourceid;
331 	u8	asourceid;
332 	u8	iunit;
333 	u32	dwmControlCap;
334 	u32	dwmProfileCap;
335 	u32	dwmVidFormatCap;
336 	u8	bmVidBitrateCap;
337 	u16	wmVidResolutionsCap;
338 	u16	wmVidFrmRateCap;
339 	u32	dwmAudFormatCap;
340 	u8	bmAudBitrateCap;
341 } __attribute__((packed));
342 
343 /* Audio processor definition */
344 struct tmComResAFeatureDescrHeader {
345 	u8	len;
346 	u8	type;
347 	u8	subtype;
348 	u8	unitid;
349 	u8	sourceid;
350 	u8	controlsize;
351 } __attribute__((packed));
352 
353 /* Audio control messages */
354 struct tmComResAudioDefaults {
355 	u8	ucDecoderLevel;
356 	u8	ucDecoderFM_Level;
357 	u8	ucMonoLevel;
358 	u8	ucNICAM_Level;
359 	u8	ucSAP_Level;
360 	u8	ucADC_Level;
361 } __attribute__((packed));
362 
363 /* Audio bitrate control message */
364 struct tmComResEncAudioBitRate {
365 	u8	ucAudioBitRateMode;
366 	u32	dwAudioBitRate;
367 	u32	dwAudioBitRatePeak;
368 } __attribute__((packed));
369 
370 /* Tuner / AV Decoder messages */
371 struct tmComResTunerStandard {
372 	u8	std;
373 	u32	country;
374 } __attribute__((packed));
375 
376 struct tmComResTunerStandardAuto {
377 	u8	mode;
378 } __attribute__((packed));
379 
380 /* EEPROM definition for PS stream types */
381 struct tmComResPSFormatDescrHeader {
382 	u8	len;
383 	u8	type;
384 	u8	subtype;
385 	u8	bFormatIndex;
386 	u16	wPacketLength;
387 	u16	wPackLength;
388 	u8	bPackDataType;
389 } __attribute__((packed));
390 
391 /* VBI control structure */
392 struct tmComResVBIFormatDescrHeader {
393 	u8	len;
394 	u8	type;
395 	u8	subtype; /* VS_FORMAT_VBI */
396 	u8	bFormatIndex;
397 	u32	VideoStandard; /* See KS_AnalogVideoStandard, NTSC = 1 */
398 	u8	StartLine; /* NTSC Start = 10 */
399 	u8	EndLine; /* NTSC = 21 */
400 	u8	FieldRate; /* 60 for NTSC */
401 	u8	bNumLines; /* Unused - scheduled for removal */
402 } __attribute__((packed));
403 
404 struct tmComResProbeCommit {
405 	u16	bmHint;
406 	u8	bFormatIndex;
407 	u8	bFrameIndex;
408 } __attribute__((packed));
409 
410 struct tmComResDebugSetLevel {
411 	u32	dwDebugLevel;
412 } __attribute__((packed));
413 
414 struct tmComResDebugGetData {
415 	u32	dwResult;
416 	u8	ucDebugData[256];
417 } __attribute__((packed));
418 
419 struct tmFwInfoStruct {
420 	u32	status;
421 	u32	mode;
422 	u32	devicespec;
423 	u32	deviceinst;
424 	u32	CPULoad;
425 	u32	RemainHeap;
426 	u32	CPUClock;
427 	u32	RAMSpeed;
428 } __attribute__((packed));
429