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