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 #include <linux/wait.h>
19 #include <linux/slab.h>
20 
21 #include "saa7164.h"
22 
23 int saa7164_api_get_load_info(struct saa7164_dev *dev, struct tmFwInfoStruct *i)
24 {
25 	int ret;
26 
27 	if (!(saa_debug & DBGLVL_CPU))
28 		return 0;
29 
30 	dprintk(DBGLVL_API, "%s()\n", __func__);
31 
32 	i->deviceinst = 0;
33 	i->devicespec = 0;
34 	i->mode = 0;
35 	i->status = 0;
36 
37 	ret = saa7164_cmd_send(dev, 0, GET_CUR,
38 		GET_FW_STATUS_CONTROL, sizeof(struct tmFwInfoStruct), i);
39 	if (ret != SAA_OK)
40 		printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
41 
42 	printk(KERN_INFO "saa7164[%d]-CPU: %d percent", dev->nr, i->CPULoad);
43 
44 	return ret;
45 }
46 
47 int saa7164_api_collect_debug(struct saa7164_dev *dev)
48 {
49 	struct tmComResDebugGetData d;
50 	u8 more = 255;
51 	int ret;
52 
53 	dprintk(DBGLVL_API, "%s()\n", __func__);
54 
55 	while (more--) {
56 
57 		memset(&d, 0, sizeof(d));
58 
59 		ret = saa7164_cmd_send(dev, 0, GET_CUR,
60 			GET_DEBUG_DATA_CONTROL, sizeof(d), &d);
61 		if (ret != SAA_OK)
62 			printk(KERN_ERR "%s() error, ret = 0x%x\n",
63 				__func__, ret);
64 
65 		if (d.dwResult != SAA_OK)
66 			break;
67 
68 		printk(KERN_INFO "saa7164[%d]-FWMSG: %s", dev->nr,
69 			d.ucDebugData);
70 	}
71 
72 	return 0;
73 }
74 
75 int saa7164_api_set_debug(struct saa7164_dev *dev, u8 level)
76 {
77 	struct tmComResDebugSetLevel lvl;
78 	int ret;
79 
80 	dprintk(DBGLVL_API, "%s(level=%d)\n", __func__, level);
81 
82 	/* Retrieve current state */
83 	ret = saa7164_cmd_send(dev, 0, GET_CUR,
84 		SET_DEBUG_LEVEL_CONTROL, sizeof(lvl), &lvl);
85 	if (ret != SAA_OK)
86 		printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
87 
88 	dprintk(DBGLVL_API, "%s() Was %d\n", __func__, lvl.dwDebugLevel);
89 
90 	lvl.dwDebugLevel = level;
91 
92 	/* set new state */
93 	ret = saa7164_cmd_send(dev, 0, SET_CUR,
94 		SET_DEBUG_LEVEL_CONTROL, sizeof(lvl), &lvl);
95 	if (ret != SAA_OK)
96 		printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
97 
98 	return ret;
99 }
100 
101 int saa7164_api_set_vbi_format(struct saa7164_port *port)
102 {
103 	struct saa7164_dev *dev = port->dev;
104 	struct tmComResProbeCommit fmt, rsp;
105 	int ret;
106 
107 	dprintk(DBGLVL_API, "%s(nr=%d, unitid=0x%x)\n", __func__,
108 		port->nr, port->hwcfg.unitid);
109 
110 	fmt.bmHint = 0;
111 	fmt.bFormatIndex = 1;
112 	fmt.bFrameIndex = 1;
113 
114 	/* Probe, see if it can support this format */
115 	ret = saa7164_cmd_send(port->dev, port->hwcfg.unitid,
116 		SET_CUR, SAA_PROBE_CONTROL, sizeof(fmt), &fmt);
117 	if (ret != SAA_OK)
118 		printk(KERN_ERR "%s() set error, ret = 0x%x\n", __func__, ret);
119 
120 	/* See of the format change was successful */
121 	ret = saa7164_cmd_send(port->dev, port->hwcfg.unitid,
122 		GET_CUR, SAA_PROBE_CONTROL, sizeof(rsp), &rsp);
123 	if (ret != SAA_OK) {
124 		printk(KERN_ERR "%s() get error, ret = 0x%x\n", __func__, ret);
125 	} else {
126 		/* Compare requested vs received, should be same */
127 		if (memcmp(&fmt, &rsp, sizeof(rsp)) == 0) {
128 			dprintk(DBGLVL_API, "SET/PROBE Verified\n");
129 
130 			/* Ask the device to select the negotiated format */
131 			ret = saa7164_cmd_send(port->dev, port->hwcfg.unitid,
132 				SET_CUR, SAA_COMMIT_CONTROL, sizeof(fmt), &fmt);
133 			if (ret != SAA_OK)
134 				printk(KERN_ERR "%s() commit error, ret = 0x%x\n",
135 					__func__, ret);
136 
137 			ret = saa7164_cmd_send(port->dev, port->hwcfg.unitid,
138 				GET_CUR, SAA_COMMIT_CONTROL, sizeof(rsp), &rsp);
139 			if (ret != SAA_OK)
140 				printk(KERN_ERR "%s() GET commit error, ret = 0x%x\n",
141 					__func__, ret);
142 
143 			if (memcmp(&fmt, &rsp, sizeof(rsp)) != 0) {
144 				printk(KERN_ERR "%s() memcmp error, ret = 0x%x\n",
145 					__func__, ret);
146 			} else
147 				dprintk(DBGLVL_API, "SET/COMMIT Verified\n");
148 
149 			dprintk(DBGLVL_API, "rsp.bmHint = 0x%x\n", rsp.bmHint);
150 			dprintk(DBGLVL_API, "rsp.bFormatIndex = 0x%x\n",
151 				rsp.bFormatIndex);
152 			dprintk(DBGLVL_API, "rsp.bFrameIndex = 0x%x\n",
153 				rsp.bFrameIndex);
154 		} else
155 			printk(KERN_ERR "%s() compare failed\n", __func__);
156 	}
157 
158 	if (ret == SAA_OK)
159 		dprintk(DBGLVL_API, "%s(nr=%d) Success\n", __func__, port->nr);
160 
161 	return ret;
162 }
163 
164 static int saa7164_api_set_gop_size(struct saa7164_port *port)
165 {
166 	struct saa7164_dev *dev = port->dev;
167 	struct tmComResEncVideoGopStructure gs;
168 	int ret;
169 
170 	dprintk(DBGLVL_ENC, "%s()\n", __func__);
171 
172 	gs.ucRefFrameDist = port->encoder_params.refdist;
173 	gs.ucGOPSize = port->encoder_params.gop_size;
174 	ret = saa7164_cmd_send(port->dev, port->hwcfg.sourceid, SET_CUR,
175 		EU_VIDEO_GOP_STRUCTURE_CONTROL,
176 		sizeof(gs), &gs);
177 	if (ret != SAA_OK)
178 		printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
179 
180 	return ret;
181 }
182 
183 int saa7164_api_set_encoder(struct saa7164_port *port)
184 {
185 	struct saa7164_dev *dev = port->dev;
186 	struct tmComResEncVideoBitRate vb;
187 	struct tmComResEncAudioBitRate ab;
188 	int ret;
189 
190 	dprintk(DBGLVL_ENC, "%s() unitid=0x%x\n", __func__,
191 		port->hwcfg.sourceid);
192 
193 	if (port->encoder_params.stream_type == V4L2_MPEG_STREAM_TYPE_MPEG2_PS)
194 		port->encoder_profile = EU_PROFILE_PS_DVD;
195 	else
196 		port->encoder_profile = EU_PROFILE_TS_HQ;
197 
198 	ret = saa7164_cmd_send(port->dev, port->hwcfg.sourceid, SET_CUR,
199 		EU_PROFILE_CONTROL, sizeof(u8), &port->encoder_profile);
200 	if (ret != SAA_OK)
201 		printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
202 
203 	/* Resolution */
204 	ret = saa7164_cmd_send(port->dev, port->hwcfg.sourceid, SET_CUR,
205 		EU_PROFILE_CONTROL, sizeof(u8), &port->encoder_profile);
206 	if (ret != SAA_OK)
207 		printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
208 
209 	/* Establish video bitrates */
210 	if (port->encoder_params.bitrate_mode ==
211 		V4L2_MPEG_VIDEO_BITRATE_MODE_CBR)
212 		vb.ucVideoBitRateMode = EU_VIDEO_BIT_RATE_MODE_CONSTANT;
213 	else
214 		vb.ucVideoBitRateMode = EU_VIDEO_BIT_RATE_MODE_VARIABLE_PEAK;
215 	vb.dwVideoBitRate = port->encoder_params.bitrate;
216 	vb.dwVideoBitRatePeak = port->encoder_params.bitrate_peak;
217 	ret = saa7164_cmd_send(port->dev, port->hwcfg.sourceid, SET_CUR,
218 		EU_VIDEO_BIT_RATE_CONTROL,
219 		sizeof(struct tmComResEncVideoBitRate),
220 		&vb);
221 	if (ret != SAA_OK)
222 		printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
223 
224 	/* Establish audio bitrates */
225 	ab.ucAudioBitRateMode = 0;
226 	ab.dwAudioBitRate = 384000;
227 	ab.dwAudioBitRatePeak = ab.dwAudioBitRate;
228 	ret = saa7164_cmd_send(port->dev, port->hwcfg.sourceid, SET_CUR,
229 		EU_AUDIO_BIT_RATE_CONTROL,
230 		sizeof(struct tmComResEncAudioBitRate),
231 		&ab);
232 	if (ret != SAA_OK)
233 		printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__,
234 			ret);
235 
236 	saa7164_api_set_aspect_ratio(port);
237 	saa7164_api_set_gop_size(port);
238 
239 	return ret;
240 }
241 
242 int saa7164_api_get_encoder(struct saa7164_port *port)
243 {
244 	struct saa7164_dev *dev = port->dev;
245 	struct tmComResEncVideoBitRate v;
246 	struct tmComResEncAudioBitRate a;
247 	struct tmComResEncVideoInputAspectRatio ar;
248 	int ret;
249 
250 	dprintk(DBGLVL_ENC, "%s() unitid=0x%x\n", __func__,
251 		port->hwcfg.sourceid);
252 
253 	port->encoder_profile = 0;
254 	port->video_format = 0;
255 	port->video_resolution = 0;
256 	port->audio_format = 0;
257 
258 	ret = saa7164_cmd_send(port->dev, port->hwcfg.sourceid, GET_CUR,
259 		EU_PROFILE_CONTROL, sizeof(u8), &port->encoder_profile);
260 	if (ret != SAA_OK)
261 		printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
262 
263 	ret = saa7164_cmd_send(port->dev, port->hwcfg.sourceid, GET_CUR,
264 		EU_VIDEO_RESOLUTION_CONTROL, sizeof(u8),
265 		&port->video_resolution);
266 	if (ret != SAA_OK)
267 		printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
268 
269 	ret = saa7164_cmd_send(port->dev, port->hwcfg.sourceid, GET_CUR,
270 		EU_VIDEO_FORMAT_CONTROL, sizeof(u8), &port->video_format);
271 	if (ret != SAA_OK)
272 		printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
273 
274 	ret = saa7164_cmd_send(port->dev, port->hwcfg.sourceid, GET_CUR,
275 		EU_VIDEO_BIT_RATE_CONTROL, sizeof(v), &v);
276 	if (ret != SAA_OK)
277 		printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
278 
279 	ret = saa7164_cmd_send(port->dev, port->hwcfg.sourceid, GET_CUR,
280 		EU_AUDIO_FORMAT_CONTROL, sizeof(u8), &port->audio_format);
281 	if (ret != SAA_OK)
282 		printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
283 
284 	ret = saa7164_cmd_send(port->dev, port->hwcfg.sourceid, GET_CUR,
285 		EU_AUDIO_BIT_RATE_CONTROL, sizeof(a), &a);
286 	if (ret != SAA_OK)
287 		printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
288 
289 	/* Aspect Ratio */
290 	ar.width = 0;
291 	ar.height = 0;
292 	ret = saa7164_cmd_send(port->dev, port->hwcfg.sourceid, GET_CUR,
293 		EU_VIDEO_INPUT_ASPECT_CONTROL,
294 		sizeof(struct tmComResEncVideoInputAspectRatio), &ar);
295 	if (ret != SAA_OK)
296 		printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
297 
298 	dprintk(DBGLVL_ENC, "encoder_profile = %d\n", port->encoder_profile);
299 	dprintk(DBGLVL_ENC, "video_format    = %d\n", port->video_format);
300 	dprintk(DBGLVL_ENC, "audio_format    = %d\n", port->audio_format);
301 	dprintk(DBGLVL_ENC, "video_resolution= %d\n", port->video_resolution);
302 	dprintk(DBGLVL_ENC, "v.ucVideoBitRateMode = %d\n",
303 		v.ucVideoBitRateMode);
304 	dprintk(DBGLVL_ENC, "v.dwVideoBitRate     = %d\n",
305 		v.dwVideoBitRate);
306 	dprintk(DBGLVL_ENC, "v.dwVideoBitRatePeak = %d\n",
307 		v.dwVideoBitRatePeak);
308 	dprintk(DBGLVL_ENC, "a.ucVideoBitRateMode = %d\n",
309 		a.ucAudioBitRateMode);
310 	dprintk(DBGLVL_ENC, "a.dwVideoBitRate     = %d\n",
311 		a.dwAudioBitRate);
312 	dprintk(DBGLVL_ENC, "a.dwVideoBitRatePeak = %d\n",
313 		a.dwAudioBitRatePeak);
314 	dprintk(DBGLVL_ENC, "aspect.width / height = %d:%d\n",
315 		ar.width, ar.height);
316 
317 	return ret;
318 }
319 
320 int saa7164_api_set_aspect_ratio(struct saa7164_port *port)
321 {
322 	struct saa7164_dev *dev = port->dev;
323 	struct tmComResEncVideoInputAspectRatio ar;
324 	int ret;
325 
326 	dprintk(DBGLVL_ENC, "%s(%d)\n", __func__,
327 		port->encoder_params.ctl_aspect);
328 
329 	switch (port->encoder_params.ctl_aspect) {
330 	case V4L2_MPEG_VIDEO_ASPECT_1x1:
331 		ar.width = 1;
332 		ar.height = 1;
333 		break;
334 	case V4L2_MPEG_VIDEO_ASPECT_4x3:
335 		ar.width = 4;
336 		ar.height = 3;
337 		break;
338 	case V4L2_MPEG_VIDEO_ASPECT_16x9:
339 		ar.width = 16;
340 		ar.height = 9;
341 		break;
342 	case V4L2_MPEG_VIDEO_ASPECT_221x100:
343 		ar.width = 221;
344 		ar.height = 100;
345 		break;
346 	default:
347 		BUG();
348 	}
349 
350 	dprintk(DBGLVL_ENC, "%s(%d) now %d:%d\n", __func__,
351 		port->encoder_params.ctl_aspect,
352 		ar.width, ar.height);
353 
354 	/* Aspect Ratio */
355 	ret = saa7164_cmd_send(port->dev, port->hwcfg.sourceid, SET_CUR,
356 		EU_VIDEO_INPUT_ASPECT_CONTROL,
357 		sizeof(struct tmComResEncVideoInputAspectRatio), &ar);
358 	if (ret != SAA_OK)
359 		printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
360 
361 	return ret;
362 }
363 
364 int saa7164_api_set_usercontrol(struct saa7164_port *port, u8 ctl)
365 {
366 	struct saa7164_dev *dev = port->dev;
367 	int ret;
368 	u16 val;
369 
370 	if (ctl == PU_BRIGHTNESS_CONTROL)
371 		val = port->ctl_brightness;
372 	else
373 	if (ctl == PU_CONTRAST_CONTROL)
374 		val = port->ctl_contrast;
375 	else
376 	if (ctl == PU_HUE_CONTROL)
377 		val = port->ctl_hue;
378 	else
379 	if (ctl == PU_SATURATION_CONTROL)
380 		val = port->ctl_saturation;
381 	else
382 	if (ctl == PU_SHARPNESS_CONTROL)
383 		val = port->ctl_sharpness;
384 	else
385 		return -EINVAL;
386 
387 	dprintk(DBGLVL_ENC, "%s() unitid=0x%x ctl=%d, val=%d\n",
388 		__func__, port->encunit.vsourceid, ctl, val);
389 
390 	ret = saa7164_cmd_send(port->dev, port->encunit.vsourceid, SET_CUR,
391 		ctl, sizeof(u16), &val);
392 	if (ret != SAA_OK)
393 		printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
394 
395 	return ret;
396 }
397 
398 int saa7164_api_get_usercontrol(struct saa7164_port *port, u8 ctl)
399 {
400 	struct saa7164_dev *dev = port->dev;
401 	int ret;
402 	u16 val;
403 
404 	ret = saa7164_cmd_send(port->dev, port->encunit.vsourceid, GET_CUR,
405 		ctl, sizeof(u16), &val);
406 	if (ret != SAA_OK) {
407 		printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
408 		return ret;
409 	}
410 
411 	dprintk(DBGLVL_ENC, "%s() ctl=%d, val=%d\n",
412 		__func__, ctl, val);
413 
414 	if (ctl == PU_BRIGHTNESS_CONTROL)
415 		port->ctl_brightness = val;
416 	else
417 	if (ctl == PU_CONTRAST_CONTROL)
418 		port->ctl_contrast = val;
419 	else
420 	if (ctl == PU_HUE_CONTROL)
421 		port->ctl_hue = val;
422 	else
423 	if (ctl == PU_SATURATION_CONTROL)
424 		port->ctl_saturation = val;
425 	else
426 	if (ctl == PU_SHARPNESS_CONTROL)
427 		port->ctl_sharpness = val;
428 
429 	return ret;
430 }
431 
432 int saa7164_api_set_videomux(struct saa7164_port *port)
433 {
434 	struct saa7164_dev *dev = port->dev;
435 	u8 inputs[] = { 1, 2, 2, 2, 5, 5, 5 };
436 	int ret;
437 
438 	dprintk(DBGLVL_ENC, "%s() v_mux=%d a_mux=%d\n",
439 		__func__, port->mux_input, inputs[port->mux_input - 1]);
440 
441 	/* Audio Mute */
442 	ret = saa7164_api_audio_mute(port, 1);
443 	if (ret != SAA_OK)
444 		printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
445 
446 	/* Video Mux */
447 	ret = saa7164_cmd_send(port->dev, port->vidproc.sourceid, SET_CUR,
448 		SU_INPUT_SELECT_CONTROL, sizeof(u8), &port->mux_input);
449 	if (ret != SAA_OK)
450 		printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
451 
452 	/* Audio Mux */
453 	ret = saa7164_cmd_send(port->dev, port->audfeat.sourceid, SET_CUR,
454 		SU_INPUT_SELECT_CONTROL, sizeof(u8),
455 		&inputs[port->mux_input - 1]);
456 	if (ret != SAA_OK)
457 		printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
458 
459 	/* Audio UnMute */
460 	ret = saa7164_api_audio_mute(port, 0);
461 	if (ret != SAA_OK)
462 		printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
463 
464 	return ret;
465 }
466 
467 int saa7164_api_audio_mute(struct saa7164_port *port, int mute)
468 {
469 	struct saa7164_dev *dev = port->dev;
470 	u8 v = mute;
471 	int ret;
472 
473 	dprintk(DBGLVL_API, "%s(%d)\n", __func__, mute);
474 
475 	ret = saa7164_cmd_send(port->dev, port->audfeat.unitid, SET_CUR,
476 		MUTE_CONTROL, sizeof(u8), &v);
477 	if (ret != SAA_OK)
478 		printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
479 
480 	return ret;
481 }
482 
483 /* 0 = silence, 0xff = full */
484 int saa7164_api_set_audio_volume(struct saa7164_port *port, s8 level)
485 {
486 	struct saa7164_dev *dev = port->dev;
487 	s16 v, min, max;
488 	int ret;
489 
490 	dprintk(DBGLVL_API, "%s(%d)\n", __func__, level);
491 
492 	/* Obtain the min/max ranges */
493 	ret = saa7164_cmd_send(port->dev, port->audfeat.unitid, GET_MIN,
494 		VOLUME_CONTROL, sizeof(u16), &min);
495 	if (ret != SAA_OK)
496 		printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
497 
498 	ret = saa7164_cmd_send(port->dev, port->audfeat.unitid, GET_MAX,
499 		VOLUME_CONTROL, sizeof(u16), &max);
500 	if (ret != SAA_OK)
501 		printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
502 
503 	ret = saa7164_cmd_send(port->dev, port->audfeat.unitid, GET_CUR,
504 		(0x01 << 8) | VOLUME_CONTROL, sizeof(u16), &v);
505 	if (ret != SAA_OK)
506 		printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
507 
508 	dprintk(DBGLVL_API, "%s(%d) min=%d max=%d cur=%d\n", __func__,
509 		level, min, max, v);
510 
511 	v = level;
512 	if (v < min)
513 		v = min;
514 	if (v > max)
515 		v = max;
516 
517 	/* Left */
518 	ret = saa7164_cmd_send(port->dev, port->audfeat.unitid, SET_CUR,
519 		(0x01 << 8) | VOLUME_CONTROL, sizeof(s16), &v);
520 	if (ret != SAA_OK)
521 		printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
522 
523 	/* Right */
524 	ret = saa7164_cmd_send(port->dev, port->audfeat.unitid, SET_CUR,
525 		(0x02 << 8) | VOLUME_CONTROL, sizeof(s16), &v);
526 	if (ret != SAA_OK)
527 		printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
528 
529 	ret = saa7164_cmd_send(port->dev, port->audfeat.unitid, GET_CUR,
530 		(0x01 << 8) | VOLUME_CONTROL, sizeof(u16), &v);
531 	if (ret != SAA_OK)
532 		printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
533 
534 	dprintk(DBGLVL_API, "%s(%d) min=%d max=%d cur=%d\n", __func__,
535 		level, min, max, v);
536 
537 	return ret;
538 }
539 
540 int saa7164_api_set_audio_std(struct saa7164_port *port)
541 {
542 	struct saa7164_dev *dev = port->dev;
543 	struct tmComResAudioDefaults lvl;
544 	struct tmComResTunerStandard tvaudio;
545 	int ret;
546 
547 	dprintk(DBGLVL_API, "%s()\n", __func__);
548 
549 	/* Establish default levels */
550 	lvl.ucDecoderLevel = TMHW_LEV_ADJ_DECLEV_DEFAULT;
551 	lvl.ucDecoderFM_Level = TMHW_LEV_ADJ_DECLEV_DEFAULT;
552 	lvl.ucMonoLevel = TMHW_LEV_ADJ_MONOLEV_DEFAULT;
553 	lvl.ucNICAM_Level = TMHW_LEV_ADJ_NICLEV_DEFAULT;
554 	lvl.ucSAP_Level = TMHW_LEV_ADJ_SAPLEV_DEFAULT;
555 	lvl.ucADC_Level = TMHW_LEV_ADJ_ADCLEV_DEFAULT;
556 	ret = saa7164_cmd_send(port->dev, port->audfeat.unitid, SET_CUR,
557 		AUDIO_DEFAULT_CONTROL, sizeof(struct tmComResAudioDefaults),
558 		&lvl);
559 	if (ret != SAA_OK)
560 		printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
561 
562 	/* Manually select the appropriate TV audio standard */
563 	if (port->encodernorm.id & V4L2_STD_NTSC) {
564 		tvaudio.std = TU_STANDARD_NTSC_M;
565 		tvaudio.country = 1;
566 	} else {
567 		tvaudio.std = TU_STANDARD_PAL_I;
568 		tvaudio.country = 44;
569 	}
570 
571 	ret = saa7164_cmd_send(port->dev, port->tunerunit.unitid, SET_CUR,
572 		TU_STANDARD_CONTROL, sizeof(tvaudio), &tvaudio);
573 	if (ret != SAA_OK)
574 		printk(KERN_ERR "%s() TU_STANDARD_CONTROL error, ret = 0x%x\n",
575 			__func__, ret);
576 	return ret;
577 }
578 
579 int saa7164_api_set_audio_detection(struct saa7164_port *port, int autodetect)
580 {
581 	struct saa7164_dev *dev = port->dev;
582 	struct tmComResTunerStandardAuto p;
583 	int ret;
584 
585 	dprintk(DBGLVL_API, "%s(%d)\n", __func__, autodetect);
586 
587 	/* Disable TV Audio autodetect if not already set (buggy) */
588 	if (autodetect)
589 		p.mode = TU_STANDARD_AUTO;
590 	else
591 		p.mode = TU_STANDARD_MANUAL;
592 	ret = saa7164_cmd_send(port->dev, port->tunerunit.unitid, SET_CUR,
593 		TU_STANDARD_AUTO_CONTROL, sizeof(p), &p);
594 	if (ret != SAA_OK)
595 		printk(KERN_ERR
596 			"%s() TU_STANDARD_AUTO_CONTROL error, ret = 0x%x\n",
597 			__func__, ret);
598 
599 	return ret;
600 }
601 
602 int saa7164_api_get_videomux(struct saa7164_port *port)
603 {
604 	struct saa7164_dev *dev = port->dev;
605 	int ret;
606 
607 	ret = saa7164_cmd_send(port->dev, port->vidproc.sourceid, GET_CUR,
608 		SU_INPUT_SELECT_CONTROL, sizeof(u8), &port->mux_input);
609 	if (ret != SAA_OK)
610 		printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
611 
612 	dprintk(DBGLVL_ENC, "%s() v_mux=%d\n",
613 		__func__, port->mux_input);
614 
615 	return ret;
616 }
617 
618 static int saa7164_api_set_dif(struct saa7164_port *port, u8 reg, u8 val)
619 {
620 	struct saa7164_dev *dev = port->dev;
621 
622 	u16 len = 0;
623 	u8 buf[256];
624 	int ret;
625 	u8 mas;
626 
627 	dprintk(DBGLVL_API, "%s(nr=%d type=%d val=%x)\n", __func__,
628 		port->nr, port->type, val);
629 
630 	if (port->nr == 0)
631 		mas = 0xd0;
632 	else
633 		mas = 0xe0;
634 
635 	memset(buf, 0, sizeof(buf));
636 
637 	buf[0x00] = 0x04;
638 	buf[0x01] = 0x00;
639 	buf[0x02] = 0x00;
640 	buf[0x03] = 0x00;
641 
642 	buf[0x04] = 0x04;
643 	buf[0x05] = 0x00;
644 	buf[0x06] = 0x00;
645 	buf[0x07] = 0x00;
646 
647 	buf[0x08] = reg;
648 	buf[0x09] = 0x26;
649 	buf[0x0a] = mas;
650 	buf[0x0b] = 0xb0;
651 
652 	buf[0x0c] = val;
653 	buf[0x0d] = 0x00;
654 	buf[0x0e] = 0x00;
655 	buf[0x0f] = 0x00;
656 
657 	ret = saa7164_cmd_send(dev, port->ifunit.unitid, GET_LEN,
658 		EXU_REGISTER_ACCESS_CONTROL, sizeof(len), &len);
659 	if (ret != SAA_OK) {
660 		printk(KERN_ERR "%s() error, ret(1) = 0x%x\n", __func__, ret);
661 		return -EIO;
662 	}
663 
664 	ret = saa7164_cmd_send(dev, port->ifunit.unitid, SET_CUR,
665 		EXU_REGISTER_ACCESS_CONTROL, len, &buf);
666 	if (ret != SAA_OK)
667 		printk(KERN_ERR "%s() error, ret(2) = 0x%x\n", __func__, ret);
668 #if 0
669 	print_hex_dump(KERN_INFO, "", DUMP_PREFIX_OFFSET, 16, 1, buf, 16,
670 		       false);
671 #endif
672 	return ret == SAA_OK ? 0 : -EIO;
673 }
674 
675 /* Disable the IF block AGC controls */
676 int saa7164_api_configure_dif(struct saa7164_port *port, u32 std)
677 {
678 	struct saa7164_dev *dev = port->dev;
679 	u8 agc_disable;
680 
681 	dprintk(DBGLVL_API, "%s(nr=%d, 0x%x)\n", __func__, port->nr, std);
682 
683 	if (std & V4L2_STD_NTSC) {
684 		dprintk(DBGLVL_API, " NTSC\n");
685 		saa7164_api_set_dif(port, 0x00, 0x01); /* Video Standard */
686 		agc_disable = 0;
687 	} else if (std & V4L2_STD_PAL_I) {
688 		dprintk(DBGLVL_API, " PAL-I\n");
689 		saa7164_api_set_dif(port, 0x00, 0x08); /* Video Standard */
690 		agc_disable = 0;
691 	} else if (std & V4L2_STD_PAL_M) {
692 		dprintk(DBGLVL_API, " PAL-M\n");
693 		saa7164_api_set_dif(port, 0x00, 0x01); /* Video Standard */
694 		agc_disable = 0;
695 	} else if (std & V4L2_STD_PAL_N) {
696 		dprintk(DBGLVL_API, " PAL-N\n");
697 		saa7164_api_set_dif(port, 0x00, 0x01); /* Video Standard */
698 		agc_disable = 0;
699 	} else if (std & V4L2_STD_PAL_Nc) {
700 		dprintk(DBGLVL_API, " PAL-Nc\n");
701 		saa7164_api_set_dif(port, 0x00, 0x01); /* Video Standard */
702 		agc_disable = 0;
703 	} else if (std & V4L2_STD_PAL_B) {
704 		dprintk(DBGLVL_API, " PAL-B\n");
705 		saa7164_api_set_dif(port, 0x00, 0x02); /* Video Standard */
706 		agc_disable = 0;
707 	} else if (std & V4L2_STD_PAL_DK) {
708 		dprintk(DBGLVL_API, " PAL-DK\n");
709 		saa7164_api_set_dif(port, 0x00, 0x10); /* Video Standard */
710 		agc_disable = 0;
711 	} else if (std & V4L2_STD_SECAM_L) {
712 		dprintk(DBGLVL_API, " SECAM-L\n");
713 		saa7164_api_set_dif(port, 0x00, 0x20); /* Video Standard */
714 		agc_disable = 0;
715 	} else {
716 		/* Unknown standard, assume DTV */
717 		dprintk(DBGLVL_API, " Unknown (assuming DTV)\n");
718 		/* Undefinded Video Standard */
719 		saa7164_api_set_dif(port, 0x00, 0x80);
720 		agc_disable = 1;
721 	}
722 
723 	saa7164_api_set_dif(port, 0x48, 0xa0); /* AGC Functions 1 */
724 	saa7164_api_set_dif(port, 0xc0, agc_disable); /* AGC Output Disable */
725 	saa7164_api_set_dif(port, 0x7c, 0x04); /* CVBS EQ */
726 	saa7164_api_set_dif(port, 0x04, 0x01); /* Active */
727 	msleep(100);
728 	saa7164_api_set_dif(port, 0x04, 0x00); /* Active (again) */
729 	msleep(100);
730 
731 	return 0;
732 }
733 
734 /* Ensure the dif is in the correct state for the operating mode
735  * (analog / dtv). We only configure the diff through the analog encoder
736  * so when we're in digital mode we need to find the appropriate encoder
737  * and use it to configure the DIF.
738  */
739 int saa7164_api_initialize_dif(struct saa7164_port *port)
740 {
741 	struct saa7164_dev *dev = port->dev;
742 	struct saa7164_port *p = NULL;
743 	int ret = -EINVAL;
744 	u32 std = 0;
745 
746 	dprintk(DBGLVL_API, "%s(nr=%d type=%d)\n", __func__,
747 		port->nr, port->type);
748 
749 	if (port->type == SAA7164_MPEG_ENCODER) {
750 		/* Pick any analog standard to init the diff.
751 		 * we'll come back during encoder_init'
752 		 * and set the correct standard if requried.
753 		 */
754 		std = V4L2_STD_NTSC;
755 	} else
756 	if (port->type == SAA7164_MPEG_DVB) {
757 		if (port->nr == SAA7164_PORT_TS1)
758 			p = &dev->ports[SAA7164_PORT_ENC1];
759 		else
760 			p = &dev->ports[SAA7164_PORT_ENC2];
761 	} else
762 	if (port->type == SAA7164_MPEG_VBI) {
763 		std = V4L2_STD_NTSC;
764 		if (port->nr == SAA7164_PORT_VBI1)
765 			p = &dev->ports[SAA7164_PORT_ENC1];
766 		else
767 			p = &dev->ports[SAA7164_PORT_ENC2];
768 	} else
769 		BUG();
770 
771 	if (p)
772 		ret = saa7164_api_configure_dif(p, std);
773 
774 	return ret;
775 }
776 
777 int saa7164_api_transition_port(struct saa7164_port *port, u8 mode)
778 {
779 	struct saa7164_dev *dev = port->dev;
780 
781 	int ret;
782 
783 	dprintk(DBGLVL_API, "%s(nr=%d unitid=0x%x,%d)\n",
784 		__func__, port->nr, port->hwcfg.unitid, mode);
785 
786 	ret = saa7164_cmd_send(port->dev, port->hwcfg.unitid, SET_CUR,
787 		SAA_STATE_CONTROL, sizeof(mode), &mode);
788 	if (ret != SAA_OK)
789 		printk(KERN_ERR "%s(portnr %d unitid 0x%x) error, ret = 0x%x\n",
790 			__func__, port->nr, port->hwcfg.unitid, ret);
791 
792 	return ret;
793 }
794 
795 int saa7164_api_get_fw_version(struct saa7164_dev *dev, u32 *version)
796 {
797 	int ret;
798 
799 	ret = saa7164_cmd_send(dev, 0, GET_CUR,
800 		GET_FW_VERSION_CONTROL, sizeof(u32), version);
801 	if (ret != SAA_OK)
802 		printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
803 
804 	return ret;
805 }
806 
807 int saa7164_api_read_eeprom(struct saa7164_dev *dev, u8 *buf, int buflen)
808 {
809 	u8 reg[] = { 0x0f, 0x00 };
810 
811 	if (buflen < 128)
812 		return -ENOMEM;
813 
814 	/* Assumption: Hauppauge eeprom is at 0xa0 on on bus 0 */
815 	/* TODO: Pull the details from the boards struct */
816 	return saa7164_api_i2c_read(&dev->i2c_bus[0], 0xa0 >> 1, sizeof(reg),
817 		&reg[0], 128, buf);
818 }
819 
820 static int saa7164_api_configure_port_vbi(struct saa7164_dev *dev,
821 					  struct saa7164_port *port)
822 {
823 	struct tmComResVBIFormatDescrHeader *fmt = &port->vbi_fmt_ntsc;
824 
825 	dprintk(DBGLVL_API, "    bFormatIndex  = 0x%x\n", fmt->bFormatIndex);
826 	dprintk(DBGLVL_API, "    VideoStandard = 0x%x\n", fmt->VideoStandard);
827 	dprintk(DBGLVL_API, "    StartLine     = %d\n", fmt->StartLine);
828 	dprintk(DBGLVL_API, "    EndLine       = %d\n", fmt->EndLine);
829 	dprintk(DBGLVL_API, "    FieldRate     = %d\n", fmt->FieldRate);
830 	dprintk(DBGLVL_API, "    bNumLines     = %d\n", fmt->bNumLines);
831 
832 	/* Cache the hardware configuration in the port */
833 
834 	port->bufcounter = port->hwcfg.BARLocation;
835 	port->pitch = port->hwcfg.BARLocation + (2 * sizeof(u32));
836 	port->bufsize = port->hwcfg.BARLocation + (3 * sizeof(u32));
837 	port->bufoffset = port->hwcfg.BARLocation + (4 * sizeof(u32));
838 	port->bufptr32l = port->hwcfg.BARLocation +
839 		(4 * sizeof(u32)) +
840 		(sizeof(u32) * port->hwcfg.buffercount) + sizeof(u32);
841 	port->bufptr32h = port->hwcfg.BARLocation +
842 		(4 * sizeof(u32)) +
843 		(sizeof(u32) * port->hwcfg.buffercount);
844 	port->bufptr64 = port->hwcfg.BARLocation +
845 		(4 * sizeof(u32)) +
846 		(sizeof(u32) * port->hwcfg.buffercount);
847 	dprintk(DBGLVL_API, "   = port->hwcfg.BARLocation = 0x%x\n",
848 		port->hwcfg.BARLocation);
849 
850 	dprintk(DBGLVL_API, "   = VS_FORMAT_VBI (becomes dev->en[%d])\n",
851 		port->nr);
852 
853 	return 0;
854 }
855 
856 static int
857 saa7164_api_configure_port_mpeg2ts(struct saa7164_dev *dev,
858 				   struct saa7164_port *port,
859 				   struct tmComResTSFormatDescrHeader *tsfmt)
860 {
861 	dprintk(DBGLVL_API, "    bFormatIndex = 0x%x\n", tsfmt->bFormatIndex);
862 	dprintk(DBGLVL_API, "    bDataOffset  = 0x%x\n", tsfmt->bDataOffset);
863 	dprintk(DBGLVL_API, "    bPacketLength= 0x%x\n", tsfmt->bPacketLength);
864 	dprintk(DBGLVL_API, "    bStrideLength= 0x%x\n", tsfmt->bStrideLength);
865 	dprintk(DBGLVL_API, "    bguid        = (....)\n");
866 
867 	/* Cache the hardware configuration in the port */
868 
869 	port->bufcounter = port->hwcfg.BARLocation;
870 	port->pitch = port->hwcfg.BARLocation + (2 * sizeof(u32));
871 	port->bufsize = port->hwcfg.BARLocation + (3 * sizeof(u32));
872 	port->bufoffset = port->hwcfg.BARLocation + (4 * sizeof(u32));
873 	port->bufptr32l = port->hwcfg.BARLocation +
874 		(4 * sizeof(u32)) +
875 		(sizeof(u32) * port->hwcfg.buffercount) + sizeof(u32);
876 	port->bufptr32h = port->hwcfg.BARLocation +
877 		(4 * sizeof(u32)) +
878 		(sizeof(u32) * port->hwcfg.buffercount);
879 	port->bufptr64 = port->hwcfg.BARLocation +
880 		(4 * sizeof(u32)) +
881 		(sizeof(u32) * port->hwcfg.buffercount);
882 	dprintk(DBGLVL_API, "   = port->hwcfg.BARLocation = 0x%x\n",
883 		port->hwcfg.BARLocation);
884 
885 	dprintk(DBGLVL_API, "   = VS_FORMAT_MPEGTS (becomes dev->ts[%d])\n",
886 		port->nr);
887 
888 	return 0;
889 }
890 
891 static int
892 saa7164_api_configure_port_mpeg2ps(struct saa7164_dev *dev,
893 				   struct saa7164_port *port,
894 				   struct tmComResPSFormatDescrHeader *fmt)
895 {
896 	dprintk(DBGLVL_API, "    bFormatIndex = 0x%x\n", fmt->bFormatIndex);
897 	dprintk(DBGLVL_API, "    wPacketLength= 0x%x\n", fmt->wPacketLength);
898 	dprintk(DBGLVL_API, "    wPackLength=   0x%x\n", fmt->wPackLength);
899 	dprintk(DBGLVL_API, "    bPackDataType= 0x%x\n", fmt->bPackDataType);
900 
901 	/* Cache the hardware configuration in the port */
902 	/* TODO: CHECK THIS in the port config */
903 	port->bufcounter = port->hwcfg.BARLocation;
904 	port->pitch = port->hwcfg.BARLocation + (2 * sizeof(u32));
905 	port->bufsize = port->hwcfg.BARLocation + (3 * sizeof(u32));
906 	port->bufoffset = port->hwcfg.BARLocation + (4 * sizeof(u32));
907 	port->bufptr32l = port->hwcfg.BARLocation +
908 		(4 * sizeof(u32)) +
909 		(sizeof(u32) * port->hwcfg.buffercount) + sizeof(u32);
910 	port->bufptr32h = port->hwcfg.BARLocation +
911 		(4 * sizeof(u32)) +
912 		(sizeof(u32) * port->hwcfg.buffercount);
913 	port->bufptr64 = port->hwcfg.BARLocation +
914 		(4 * sizeof(u32)) +
915 		(sizeof(u32) * port->hwcfg.buffercount);
916 	dprintk(DBGLVL_API, "   = port->hwcfg.BARLocation = 0x%x\n",
917 		port->hwcfg.BARLocation);
918 
919 	dprintk(DBGLVL_API, "   = VS_FORMAT_MPEGPS (becomes dev->enc[%d])\n",
920 		port->nr);
921 
922 	return 0;
923 }
924 
925 static int saa7164_api_dump_subdevs(struct saa7164_dev *dev, u8 *buf, int len)
926 {
927 	struct saa7164_port *tsport = NULL;
928 	struct saa7164_port *encport = NULL;
929 	struct saa7164_port *vbiport = NULL;
930 	u32 idx, next_offset;
931 	int i;
932 	struct tmComResDescrHeader *hdr, *t;
933 	struct tmComResExtDevDescrHeader *exthdr;
934 	struct tmComResPathDescrHeader *pathhdr;
935 	struct tmComResAntTermDescrHeader *anttermhdr;
936 	struct tmComResTunerDescrHeader *tunerunithdr;
937 	struct tmComResDMATermDescrHeader *vcoutputtermhdr;
938 	struct tmComResTSFormatDescrHeader *tsfmt;
939 	struct tmComResPSFormatDescrHeader *psfmt;
940 	struct tmComResSelDescrHeader *psel;
941 	struct tmComResProcDescrHeader *pdh;
942 	struct tmComResAFeatureDescrHeader *afd;
943 	struct tmComResEncoderDescrHeader *edh;
944 	struct tmComResVBIFormatDescrHeader *vbifmt;
945 	u32 currpath = 0;
946 
947 	dprintk(DBGLVL_API,
948 		"%s(?,?,%d) sizeof(struct tmComResDescrHeader) = %d bytes\n",
949 		__func__, len, (u32)sizeof(struct tmComResDescrHeader));
950 
951 	for (idx = 0; idx < (len - sizeof(struct tmComResDescrHeader));) {
952 
953 		hdr = (struct tmComResDescrHeader *)(buf + idx);
954 
955 		if (hdr->type != CS_INTERFACE)
956 			return SAA_ERR_NOT_SUPPORTED;
957 
958 		dprintk(DBGLVL_API, "@ 0x%x =\n", idx);
959 		switch (hdr->subtype) {
960 		case GENERAL_REQUEST:
961 			dprintk(DBGLVL_API, " GENERAL_REQUEST\n");
962 			break;
963 		case VC_TUNER_PATH:
964 			dprintk(DBGLVL_API, " VC_TUNER_PATH\n");
965 			pathhdr = (struct tmComResPathDescrHeader *)(buf + idx);
966 			dprintk(DBGLVL_API, "  pathid = 0x%x\n",
967 				pathhdr->pathid);
968 			currpath = pathhdr->pathid;
969 			break;
970 		case VC_INPUT_TERMINAL:
971 			dprintk(DBGLVL_API, " VC_INPUT_TERMINAL\n");
972 			anttermhdr =
973 				(struct tmComResAntTermDescrHeader *)(buf + idx);
974 			dprintk(DBGLVL_API, "  terminalid   = 0x%x\n",
975 				anttermhdr->terminalid);
976 			dprintk(DBGLVL_API, "  terminaltype = 0x%x\n",
977 				anttermhdr->terminaltype);
978 			switch (anttermhdr->terminaltype) {
979 			case ITT_ANTENNA:
980 				dprintk(DBGLVL_API, "   = ITT_ANTENNA\n");
981 				break;
982 			case LINE_CONNECTOR:
983 				dprintk(DBGLVL_API, "   = LINE_CONNECTOR\n");
984 				break;
985 			case SPDIF_CONNECTOR:
986 				dprintk(DBGLVL_API, "   = SPDIF_CONNECTOR\n");
987 				break;
988 			case COMPOSITE_CONNECTOR:
989 				dprintk(DBGLVL_API,
990 					"   = COMPOSITE_CONNECTOR\n");
991 				break;
992 			case SVIDEO_CONNECTOR:
993 				dprintk(DBGLVL_API, "   = SVIDEO_CONNECTOR\n");
994 				break;
995 			case COMPONENT_CONNECTOR:
996 				dprintk(DBGLVL_API,
997 					"   = COMPONENT_CONNECTOR\n");
998 				break;
999 			case STANDARD_DMA:
1000 				dprintk(DBGLVL_API, "   = STANDARD_DMA\n");
1001 				break;
1002 			default:
1003 				dprintk(DBGLVL_API, "   = undefined (0x%x)\n",
1004 					anttermhdr->terminaltype);
1005 			}
1006 			dprintk(DBGLVL_API, "  assocterminal= 0x%x\n",
1007 				anttermhdr->assocterminal);
1008 			dprintk(DBGLVL_API, "  iterminal    = 0x%x\n",
1009 				anttermhdr->iterminal);
1010 			dprintk(DBGLVL_API, "  controlsize  = 0x%x\n",
1011 				anttermhdr->controlsize);
1012 			break;
1013 		case VC_OUTPUT_TERMINAL:
1014 			dprintk(DBGLVL_API, " VC_OUTPUT_TERMINAL\n");
1015 			vcoutputtermhdr =
1016 				(struct tmComResDMATermDescrHeader *)(buf + idx);
1017 			dprintk(DBGLVL_API, "  unitid = 0x%x\n",
1018 				vcoutputtermhdr->unitid);
1019 			dprintk(DBGLVL_API, "  terminaltype = 0x%x\n",
1020 				vcoutputtermhdr->terminaltype);
1021 			switch (vcoutputtermhdr->terminaltype) {
1022 			case ITT_ANTENNA:
1023 				dprintk(DBGLVL_API, "   = ITT_ANTENNA\n");
1024 				break;
1025 			case LINE_CONNECTOR:
1026 				dprintk(DBGLVL_API, "   = LINE_CONNECTOR\n");
1027 				break;
1028 			case SPDIF_CONNECTOR:
1029 				dprintk(DBGLVL_API, "   = SPDIF_CONNECTOR\n");
1030 				break;
1031 			case COMPOSITE_CONNECTOR:
1032 				dprintk(DBGLVL_API,
1033 					"   = COMPOSITE_CONNECTOR\n");
1034 				break;
1035 			case SVIDEO_CONNECTOR:
1036 				dprintk(DBGLVL_API, "   = SVIDEO_CONNECTOR\n");
1037 				break;
1038 			case COMPONENT_CONNECTOR:
1039 				dprintk(DBGLVL_API,
1040 					"   = COMPONENT_CONNECTOR\n");
1041 				break;
1042 			case STANDARD_DMA:
1043 				dprintk(DBGLVL_API, "   = STANDARD_DMA\n");
1044 				break;
1045 			default:
1046 				dprintk(DBGLVL_API, "   = undefined (0x%x)\n",
1047 					vcoutputtermhdr->terminaltype);
1048 			}
1049 			dprintk(DBGLVL_API, "  assocterminal= 0x%x\n",
1050 				vcoutputtermhdr->assocterminal);
1051 			dprintk(DBGLVL_API, "  sourceid     = 0x%x\n",
1052 				vcoutputtermhdr->sourceid);
1053 			dprintk(DBGLVL_API, "  iterminal    = 0x%x\n",
1054 				vcoutputtermhdr->iterminal);
1055 			dprintk(DBGLVL_API, "  BARLocation  = 0x%x\n",
1056 				vcoutputtermhdr->BARLocation);
1057 			dprintk(DBGLVL_API, "  flags        = 0x%x\n",
1058 				vcoutputtermhdr->flags);
1059 			dprintk(DBGLVL_API, "  interruptid  = 0x%x\n",
1060 				vcoutputtermhdr->interruptid);
1061 			dprintk(DBGLVL_API, "  buffercount  = 0x%x\n",
1062 				vcoutputtermhdr->buffercount);
1063 			dprintk(DBGLVL_API, "  metadatasize = 0x%x\n",
1064 				vcoutputtermhdr->metadatasize);
1065 			dprintk(DBGLVL_API, "  controlsize  = 0x%x\n",
1066 				vcoutputtermhdr->controlsize);
1067 			dprintk(DBGLVL_API, "  numformats   = 0x%x\n",
1068 				vcoutputtermhdr->numformats);
1069 
1070 			t = (struct tmComResDescrHeader *)
1071 				((struct tmComResDMATermDescrHeader *)(buf + idx));
1072 			next_offset = idx + (vcoutputtermhdr->len);
1073 			for (i = 0; i < vcoutputtermhdr->numformats; i++) {
1074 				t = (struct tmComResDescrHeader *)
1075 					(buf + next_offset);
1076 				switch (t->subtype) {
1077 				case VS_FORMAT_MPEG2TS:
1078 					tsfmt =
1079 					(struct tmComResTSFormatDescrHeader *)t;
1080 					if (currpath == 1)
1081 						tsport = &dev->ports[SAA7164_PORT_TS1];
1082 					else
1083 						tsport = &dev->ports[SAA7164_PORT_TS2];
1084 					memcpy(&tsport->hwcfg, vcoutputtermhdr,
1085 						sizeof(*vcoutputtermhdr));
1086 					saa7164_api_configure_port_mpeg2ts(dev,
1087 						tsport, tsfmt);
1088 					break;
1089 				case VS_FORMAT_MPEG2PS:
1090 					psfmt =
1091 					(struct tmComResPSFormatDescrHeader *)t;
1092 					if (currpath == 1)
1093 						encport = &dev->ports[SAA7164_PORT_ENC1];
1094 					else
1095 						encport = &dev->ports[SAA7164_PORT_ENC2];
1096 					memcpy(&encport->hwcfg, vcoutputtermhdr,
1097 						sizeof(*vcoutputtermhdr));
1098 					saa7164_api_configure_port_mpeg2ps(dev,
1099 						encport, psfmt);
1100 					break;
1101 				case VS_FORMAT_VBI:
1102 					vbifmt =
1103 					(struct tmComResVBIFormatDescrHeader *)t;
1104 					if (currpath == 1)
1105 						vbiport = &dev->ports[SAA7164_PORT_VBI1];
1106 					else
1107 						vbiport = &dev->ports[SAA7164_PORT_VBI2];
1108 					memcpy(&vbiport->hwcfg, vcoutputtermhdr,
1109 						sizeof(*vcoutputtermhdr));
1110 					memcpy(&vbiport->vbi_fmt_ntsc, vbifmt,
1111 						sizeof(*vbifmt));
1112 					saa7164_api_configure_port_vbi(dev,
1113 						vbiport);
1114 					break;
1115 				case VS_FORMAT_RDS:
1116 					dprintk(DBGLVL_API,
1117 						"   = VS_FORMAT_RDS\n");
1118 					break;
1119 				case VS_FORMAT_UNCOMPRESSED:
1120 					dprintk(DBGLVL_API,
1121 					"   = VS_FORMAT_UNCOMPRESSED\n");
1122 					break;
1123 				case VS_FORMAT_TYPE:
1124 					dprintk(DBGLVL_API,
1125 						"   = VS_FORMAT_TYPE\n");
1126 					break;
1127 				default:
1128 					dprintk(DBGLVL_API,
1129 						"   = undefined (0x%x)\n",
1130 						t->subtype);
1131 				}
1132 				next_offset += t->len;
1133 			}
1134 
1135 			break;
1136 		case TUNER_UNIT:
1137 			dprintk(DBGLVL_API, " TUNER_UNIT\n");
1138 			tunerunithdr =
1139 				(struct tmComResTunerDescrHeader *)(buf + idx);
1140 			dprintk(DBGLVL_API, "  unitid = 0x%x\n",
1141 				tunerunithdr->unitid);
1142 			dprintk(DBGLVL_API, "  sourceid = 0x%x\n",
1143 				tunerunithdr->sourceid);
1144 			dprintk(DBGLVL_API, "  iunit = 0x%x\n",
1145 				tunerunithdr->iunit);
1146 			dprintk(DBGLVL_API, "  tuningstandards = 0x%x\n",
1147 				tunerunithdr->tuningstandards);
1148 			dprintk(DBGLVL_API, "  controlsize = 0x%x\n",
1149 				tunerunithdr->controlsize);
1150 			dprintk(DBGLVL_API, "  controls = 0x%x\n",
1151 				tunerunithdr->controls);
1152 
1153 			if (tunerunithdr->unitid == tunerunithdr->iunit) {
1154 				if (currpath == 1)
1155 					encport = &dev->ports[SAA7164_PORT_ENC1];
1156 				else
1157 					encport = &dev->ports[SAA7164_PORT_ENC2];
1158 				memcpy(&encport->tunerunit, tunerunithdr,
1159 					sizeof(struct tmComResTunerDescrHeader));
1160 				dprintk(DBGLVL_API,
1161 					"  (becomes dev->enc[%d] tuner)\n",
1162 					encport->nr);
1163 			}
1164 			break;
1165 		case VC_SELECTOR_UNIT:
1166 			psel = (struct tmComResSelDescrHeader *)(buf + idx);
1167 			dprintk(DBGLVL_API, " VC_SELECTOR_UNIT\n");
1168 			dprintk(DBGLVL_API, "  unitid = 0x%x\n",
1169 				psel->unitid);
1170 			dprintk(DBGLVL_API, "  nrinpins = 0x%x\n",
1171 				psel->nrinpins);
1172 			dprintk(DBGLVL_API, "  sourceid = 0x%x\n",
1173 				psel->sourceid);
1174 			break;
1175 		case VC_PROCESSING_UNIT:
1176 			pdh = (struct tmComResProcDescrHeader *)(buf + idx);
1177 			dprintk(DBGLVL_API, " VC_PROCESSING_UNIT\n");
1178 			dprintk(DBGLVL_API, "  unitid = 0x%x\n",
1179 				pdh->unitid);
1180 			dprintk(DBGLVL_API, "  sourceid = 0x%x\n",
1181 				pdh->sourceid);
1182 			dprintk(DBGLVL_API, "  controlsize = 0x%x\n",
1183 				pdh->controlsize);
1184 			if (pdh->controlsize == 0x04) {
1185 				if (currpath == 1)
1186 					encport = &dev->ports[SAA7164_PORT_ENC1];
1187 				else
1188 					encport = &dev->ports[SAA7164_PORT_ENC2];
1189 				memcpy(&encport->vidproc, pdh,
1190 					sizeof(struct tmComResProcDescrHeader));
1191 				dprintk(DBGLVL_API, "  (becomes dev->enc[%d])\n",
1192 					encport->nr);
1193 			}
1194 			break;
1195 		case FEATURE_UNIT:
1196 			afd = (struct tmComResAFeatureDescrHeader *)(buf + idx);
1197 			dprintk(DBGLVL_API, " FEATURE_UNIT\n");
1198 			dprintk(DBGLVL_API, "  unitid = 0x%x\n",
1199 				afd->unitid);
1200 			dprintk(DBGLVL_API, "  sourceid = 0x%x\n",
1201 				afd->sourceid);
1202 			dprintk(DBGLVL_API, "  controlsize = 0x%x\n",
1203 				afd->controlsize);
1204 			if (currpath == 1)
1205 				encport = &dev->ports[SAA7164_PORT_ENC1];
1206 			else
1207 				encport = &dev->ports[SAA7164_PORT_ENC2];
1208 			memcpy(&encport->audfeat, afd,
1209 				sizeof(struct tmComResAFeatureDescrHeader));
1210 			dprintk(DBGLVL_API, "  (becomes dev->enc[%d])\n",
1211 				encport->nr);
1212 			break;
1213 		case ENCODER_UNIT:
1214 			edh = (struct tmComResEncoderDescrHeader *)(buf + idx);
1215 			dprintk(DBGLVL_API, " ENCODER_UNIT\n");
1216 			dprintk(DBGLVL_API, "  subtype = 0x%x\n", edh->subtype);
1217 			dprintk(DBGLVL_API, "  unitid = 0x%x\n", edh->unitid);
1218 			dprintk(DBGLVL_API, "  vsourceid = 0x%x\n",
1219 			edh->vsourceid);
1220 			dprintk(DBGLVL_API, "  asourceid = 0x%x\n",
1221 				edh->asourceid);
1222 			dprintk(DBGLVL_API, "  iunit = 0x%x\n", edh->iunit);
1223 			if (edh->iunit == edh->unitid) {
1224 				if (currpath == 1)
1225 					encport = &dev->ports[SAA7164_PORT_ENC1];
1226 				else
1227 					encport = &dev->ports[SAA7164_PORT_ENC2];
1228 				memcpy(&encport->encunit, edh,
1229 					sizeof(struct tmComResEncoderDescrHeader));
1230 				dprintk(DBGLVL_API,
1231 					"  (becomes dev->enc[%d])\n",
1232 					encport->nr);
1233 			}
1234 			break;
1235 		case EXTENSION_UNIT:
1236 			dprintk(DBGLVL_API, " EXTENSION_UNIT\n");
1237 			exthdr = (struct tmComResExtDevDescrHeader *)(buf + idx);
1238 			dprintk(DBGLVL_API, "  unitid = 0x%x\n",
1239 				exthdr->unitid);
1240 			dprintk(DBGLVL_API, "  deviceid = 0x%x\n",
1241 				exthdr->deviceid);
1242 			dprintk(DBGLVL_API, "  devicetype = 0x%x\n",
1243 				exthdr->devicetype);
1244 			if (exthdr->devicetype & 0x1)
1245 				dprintk(DBGLVL_API, "   = Decoder Device\n");
1246 			if (exthdr->devicetype & 0x2)
1247 				dprintk(DBGLVL_API, "   = GPIO Source\n");
1248 			if (exthdr->devicetype & 0x4)
1249 				dprintk(DBGLVL_API, "   = Video Decoder\n");
1250 			if (exthdr->devicetype & 0x8)
1251 				dprintk(DBGLVL_API, "   = Audio Decoder\n");
1252 			if (exthdr->devicetype & 0x20)
1253 				dprintk(DBGLVL_API, "   = Crossbar\n");
1254 			if (exthdr->devicetype & 0x40)
1255 				dprintk(DBGLVL_API, "   = Tuner\n");
1256 			if (exthdr->devicetype & 0x80)
1257 				dprintk(DBGLVL_API, "   = IF PLL\n");
1258 			if (exthdr->devicetype & 0x100)
1259 				dprintk(DBGLVL_API, "   = Demodulator\n");
1260 			if (exthdr->devicetype & 0x200)
1261 				dprintk(DBGLVL_API, "   = RDS Decoder\n");
1262 			if (exthdr->devicetype & 0x400)
1263 				dprintk(DBGLVL_API, "   = Encoder\n");
1264 			if (exthdr->devicetype & 0x800)
1265 				dprintk(DBGLVL_API, "   = IR Decoder\n");
1266 			if (exthdr->devicetype & 0x1000)
1267 				dprintk(DBGLVL_API, "   = EEPROM\n");
1268 			if (exthdr->devicetype & 0x2000)
1269 				dprintk(DBGLVL_API,
1270 					"   = VBI Decoder\n");
1271 			if (exthdr->devicetype & 0x10000)
1272 				dprintk(DBGLVL_API,
1273 					"   = Streaming Device\n");
1274 			if (exthdr->devicetype & 0x20000)
1275 				dprintk(DBGLVL_API,
1276 					"   = DRM Device\n");
1277 			if (exthdr->devicetype & 0x40000000)
1278 				dprintk(DBGLVL_API,
1279 					"   = Generic Device\n");
1280 			if (exthdr->devicetype & 0x80000000)
1281 				dprintk(DBGLVL_API,
1282 					"   = Config Space Device\n");
1283 			dprintk(DBGLVL_API, "  numgpiopins = 0x%x\n",
1284 				exthdr->numgpiopins);
1285 			dprintk(DBGLVL_API, "  numgpiogroups = 0x%x\n",
1286 				exthdr->numgpiogroups);
1287 			dprintk(DBGLVL_API, "  controlsize = 0x%x\n",
1288 				exthdr->controlsize);
1289 			if (exthdr->devicetype & 0x80) {
1290 				if (currpath == 1)
1291 					encport = &dev->ports[SAA7164_PORT_ENC1];
1292 				else
1293 					encport = &dev->ports[SAA7164_PORT_ENC2];
1294 				memcpy(&encport->ifunit, exthdr,
1295 					sizeof(struct tmComResExtDevDescrHeader));
1296 				dprintk(DBGLVL_API,
1297 					"  (becomes dev->enc[%d])\n",
1298 					encport->nr);
1299 			}
1300 			break;
1301 		case PVC_INFRARED_UNIT:
1302 			dprintk(DBGLVL_API, " PVC_INFRARED_UNIT\n");
1303 			break;
1304 		case DRM_UNIT:
1305 			dprintk(DBGLVL_API, " DRM_UNIT\n");
1306 			break;
1307 		default:
1308 			dprintk(DBGLVL_API, "default %d\n", hdr->subtype);
1309 		}
1310 
1311 		dprintk(DBGLVL_API, " 1.%x\n", hdr->len);
1312 		dprintk(DBGLVL_API, " 2.%x\n", hdr->type);
1313 		dprintk(DBGLVL_API, " 3.%x\n", hdr->subtype);
1314 		dprintk(DBGLVL_API, " 4.%x\n", hdr->unitid);
1315 
1316 		idx += hdr->len;
1317 	}
1318 
1319 	return 0;
1320 }
1321 
1322 int saa7164_api_enum_subdevs(struct saa7164_dev *dev)
1323 {
1324 	int ret;
1325 	u32 buflen = 0;
1326 	u8 *buf;
1327 
1328 	dprintk(DBGLVL_API, "%s()\n", __func__);
1329 
1330 	/* Get the total descriptor length */
1331 	ret = saa7164_cmd_send(dev, 0, GET_LEN,
1332 		GET_DESCRIPTORS_CONTROL, sizeof(buflen), &buflen);
1333 	if (ret != SAA_OK)
1334 		printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
1335 
1336 	dprintk(DBGLVL_API, "%s() total descriptor size = %d bytes.\n",
1337 		__func__, buflen);
1338 
1339 	/* Allocate enough storage for all of the descs */
1340 	buf = kzalloc(buflen, GFP_KERNEL);
1341 	if (!buf)
1342 		return SAA_ERR_NO_RESOURCES;
1343 
1344 	/* Retrieve them */
1345 	ret = saa7164_cmd_send(dev, 0, GET_CUR,
1346 		GET_DESCRIPTORS_CONTROL, buflen, buf);
1347 	if (ret != SAA_OK) {
1348 		printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
1349 		goto out;
1350 	}
1351 
1352 	if (saa_debug & DBGLVL_API)
1353 		print_hex_dump(KERN_INFO, "", DUMP_PREFIX_OFFSET, 16, 1, buf,
1354 			       buflen & ~15, false);
1355 
1356 	saa7164_api_dump_subdevs(dev, buf, buflen);
1357 
1358 out:
1359 	kfree(buf);
1360 	return ret;
1361 }
1362 
1363 int saa7164_api_i2c_read(struct saa7164_i2c *bus, u8 addr, u32 reglen, u8 *reg,
1364 	u32 datalen, u8 *data)
1365 {
1366 	struct saa7164_dev *dev = bus->dev;
1367 	u16 len = 0;
1368 	int unitid;
1369 	u8 buf[256];
1370 	int ret;
1371 
1372 	dprintk(DBGLVL_API, "%s() addr=%x reglen=%d datalen=%d\n",
1373 		__func__, addr, reglen, datalen);
1374 
1375 	if (reglen > 4)
1376 		return -EIO;
1377 
1378 	/* Prepare the send buffer */
1379 	/* Bytes 00-03 source register length
1380 	 *       04-07 source bytes to read
1381 	 *       08... register address
1382 	 */
1383 	memset(buf, 0, sizeof(buf));
1384 	memcpy((buf + 2 * sizeof(u32) + 0), reg, reglen);
1385 	*((u32 *)(buf + 0 * sizeof(u32))) = reglen;
1386 	*((u32 *)(buf + 1 * sizeof(u32))) = datalen;
1387 
1388 	unitid = saa7164_i2caddr_to_unitid(bus, addr);
1389 	if (unitid < 0) {
1390 		printk(KERN_ERR
1391 			"%s() error, cannot translate regaddr 0x%x to unitid\n",
1392 			__func__, addr);
1393 		return -EIO;
1394 	}
1395 
1396 	ret = saa7164_cmd_send(bus->dev, unitid, GET_LEN,
1397 		EXU_REGISTER_ACCESS_CONTROL, sizeof(len), &len);
1398 	if (ret != SAA_OK) {
1399 		printk(KERN_ERR "%s() error, ret(1) = 0x%x\n", __func__, ret);
1400 		return -EIO;
1401 	}
1402 
1403 	dprintk(DBGLVL_API, "%s() len = %d bytes\n", __func__, len);
1404 
1405 	if (saa_debug & DBGLVL_I2C)
1406 		print_hex_dump(KERN_INFO, "", DUMP_PREFIX_OFFSET, 16, 1, buf,
1407 			       32, false);
1408 
1409 	ret = saa7164_cmd_send(bus->dev, unitid, GET_CUR,
1410 		EXU_REGISTER_ACCESS_CONTROL, len, &buf);
1411 	if (ret != SAA_OK)
1412 		printk(KERN_ERR "%s() error, ret(2) = 0x%x\n", __func__, ret);
1413 	else {
1414 		if (saa_debug & DBGLVL_I2C)
1415 			print_hex_dump(KERN_INFO, "", DUMP_PREFIX_OFFSET, 16, 1,
1416 				       buf, sizeof(buf), false);
1417 		memcpy(data, (buf + 2 * sizeof(u32) + reglen), datalen);
1418 	}
1419 
1420 	return ret == SAA_OK ? 0 : -EIO;
1421 }
1422 
1423 /* For a given 8 bit i2c address device, write the buffer */
1424 int saa7164_api_i2c_write(struct saa7164_i2c *bus, u8 addr, u32 datalen,
1425 	u8 *data)
1426 {
1427 	struct saa7164_dev *dev = bus->dev;
1428 	u16 len = 0;
1429 	int unitid;
1430 	int reglen;
1431 	u8 buf[256];
1432 	int ret;
1433 
1434 	dprintk(DBGLVL_API, "%s() addr=0x%2x len=0x%x\n",
1435 		__func__, addr, datalen);
1436 
1437 	if ((datalen == 0) || (datalen > 232))
1438 		return -EIO;
1439 
1440 	memset(buf, 0, sizeof(buf));
1441 
1442 	unitid = saa7164_i2caddr_to_unitid(bus, addr);
1443 	if (unitid < 0) {
1444 		printk(KERN_ERR
1445 			"%s() error, cannot translate regaddr 0x%x to unitid\n",
1446 			__func__, addr);
1447 		return -EIO;
1448 	}
1449 
1450 	reglen = saa7164_i2caddr_to_reglen(bus, addr);
1451 	if (reglen < 0) {
1452 		printk(KERN_ERR
1453 			"%s() error, cannot translate regaddr to reglen\n",
1454 			__func__);
1455 		return -EIO;
1456 	}
1457 
1458 	ret = saa7164_cmd_send(bus->dev, unitid, GET_LEN,
1459 		EXU_REGISTER_ACCESS_CONTROL, sizeof(len), &len);
1460 	if (ret != SAA_OK) {
1461 		printk(KERN_ERR "%s() error, ret(1) = 0x%x\n", __func__, ret);
1462 		return -EIO;
1463 	}
1464 
1465 	dprintk(DBGLVL_API, "%s() len = %d bytes unitid=0x%x\n", __func__,
1466 		len, unitid);
1467 
1468 	/* Prepare the send buffer */
1469 	/* Bytes 00-03 dest register length
1470 	 *       04-07 dest bytes to write
1471 	 *       08... register address
1472 	 */
1473 	*((u32 *)(buf + 0 * sizeof(u32))) = reglen;
1474 	*((u32 *)(buf + 1 * sizeof(u32))) = datalen - reglen;
1475 	memcpy((buf + 2 * sizeof(u32)), data, datalen);
1476 
1477 	if (saa_debug & DBGLVL_I2C)
1478 		print_hex_dump(KERN_INFO, "", DUMP_PREFIX_OFFSET, 16, 1,
1479 			       buf, sizeof(buf), false);
1480 
1481 	ret = saa7164_cmd_send(bus->dev, unitid, SET_CUR,
1482 		EXU_REGISTER_ACCESS_CONTROL, len, &buf);
1483 	if (ret != SAA_OK)
1484 		printk(KERN_ERR "%s() error, ret(2) = 0x%x\n", __func__, ret);
1485 
1486 	return ret == SAA_OK ? 0 : -EIO;
1487 }
1488 
1489 static int saa7164_api_modify_gpio(struct saa7164_dev *dev, u8 unitid,
1490 	u8 pin, u8 state)
1491 {
1492 	int ret;
1493 	struct tmComResGPIO t;
1494 
1495 	dprintk(DBGLVL_API, "%s(0x%x, %d, %d)\n",
1496 		__func__, unitid, pin, state);
1497 
1498 	if ((pin > 7) || (state > 2))
1499 		return SAA_ERR_BAD_PARAMETER;
1500 
1501 	t.pin = pin;
1502 	t.state = state;
1503 
1504 	ret = saa7164_cmd_send(dev, unitid, SET_CUR,
1505 		EXU_GPIO_CONTROL, sizeof(t), &t);
1506 	if (ret != SAA_OK)
1507 		printk(KERN_ERR "%s() error, ret = 0x%x\n",
1508 			__func__, ret);
1509 
1510 	return ret;
1511 }
1512 
1513 int saa7164_api_set_gpiobit(struct saa7164_dev *dev, u8 unitid,
1514 	u8 pin)
1515 {
1516 	return saa7164_api_modify_gpio(dev, unitid, pin, 1);
1517 }
1518 
1519 int saa7164_api_clear_gpiobit(struct saa7164_dev *dev, u8 unitid,
1520 	u8 pin)
1521 {
1522 	return saa7164_api_modify_gpio(dev, unitid, pin, 0);
1523 }
1524 
1525