xref: /linux/drivers/media/pci/saa7134/saa7134-video.c (revision fcc79e1714e8c2b8e216dc3149812edd37884eef)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *
4  * device driver for philips saa7134 based TV cards
5  * video4linux video interface
6  *
7  * (c) 2001-03 Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]
8  */
9 
10 #include "saa7134.h"
11 #include "saa7134-reg.h"
12 
13 #include <linux/init.h>
14 #include <linux/list.h>
15 #include <linux/module.h>
16 #include <linux/kernel.h>
17 #include <linux/slab.h>
18 #include <linux/sort.h>
19 
20 #include <media/v4l2-common.h>
21 #include <media/v4l2-event.h>
22 #include <media/i2c/saa6588.h>
23 
24 /* ------------------------------------------------------------------ */
25 
26 unsigned int video_debug;
27 static unsigned int gbuffers      = 8;
28 static unsigned int noninterlaced; /* 0 */
29 static unsigned int gbufsize      = 720*576*4;
30 static unsigned int gbufsize_max  = 720*576*4;
31 static char secam[] = "--";
32 module_param(video_debug, int, 0644);
33 MODULE_PARM_DESC(video_debug,"enable debug messages [video]");
34 module_param(gbuffers, int, 0444);
35 MODULE_PARM_DESC(gbuffers,"number of capture buffers, range 2-32");
36 module_param(noninterlaced, int, 0644);
37 MODULE_PARM_DESC(noninterlaced,"capture non interlaced video");
38 module_param_string(secam, secam, sizeof(secam), 0644);
39 MODULE_PARM_DESC(secam, "force SECAM variant, either DK,L or Lc");
40 
41 
42 #define video_dbg(fmt, arg...) do { \
43 	if (video_debug & 0x04) \
44 		printk(KERN_DEBUG pr_fmt("video: " fmt), ## arg); \
45 	} while (0)
46 
47 /* ------------------------------------------------------------------ */
48 /* Defines for Video Output Port Register at address 0x191            */
49 
50 /* Bit 0: VIP code T bit polarity */
51 
52 #define VP_T_CODE_P_NON_INVERTED	0x00
53 #define VP_T_CODE_P_INVERTED		0x01
54 
55 /* ------------------------------------------------------------------ */
56 /* Defines for Video Output Port Register at address 0x195            */
57 
58 /* Bit 2: Video output clock delay control */
59 
60 #define VP_CLK_CTRL2_NOT_DELAYED	0x00
61 #define VP_CLK_CTRL2_DELAYED		0x04
62 
63 /* Bit 1: Video output clock invert control */
64 
65 #define VP_CLK_CTRL1_NON_INVERTED	0x00
66 #define VP_CLK_CTRL1_INVERTED		0x02
67 
68 /* ------------------------------------------------------------------ */
69 /* Defines for Video Output Port Register at address 0x196            */
70 
71 /* Bits 2 to 0: VSYNC pin video vertical sync type */
72 
73 #define VP_VS_TYPE_MASK			0x07
74 
75 #define VP_VS_TYPE_OFF			0x00
76 #define VP_VS_TYPE_V123			0x01
77 #define VP_VS_TYPE_V_ITU		0x02
78 #define VP_VS_TYPE_VGATE_L		0x03
79 #define VP_VS_TYPE_RESERVED1		0x04
80 #define VP_VS_TYPE_RESERVED2		0x05
81 #define VP_VS_TYPE_F_ITU		0x06
82 #define VP_VS_TYPE_SC_FID		0x07
83 
84 /* ------------------------------------------------------------------ */
85 /* data structs for video                                             */
86 
87 static int video_out[][9] = {
88 	[CCIR656] = { 0x00, 0xb1, 0x00, 0xa1, 0x00, 0x04, 0x06, 0x00, 0x00 },
89 };
90 
91 static struct saa7134_format formats[] = {
92 	{
93 		.fourcc   = V4L2_PIX_FMT_GREY,
94 		.depth    = 8,
95 		.pm       = 0x06,
96 	},{
97 		.fourcc   = V4L2_PIX_FMT_RGB555,
98 		.depth    = 16,
99 		.pm       = 0x13 | 0x80,
100 	},{
101 		.fourcc   = V4L2_PIX_FMT_RGB555X,
102 		.depth    = 16,
103 		.pm       = 0x13 | 0x80,
104 		.bswap    = 1,
105 	},{
106 		.fourcc   = V4L2_PIX_FMT_RGB565,
107 		.depth    = 16,
108 		.pm       = 0x10 | 0x80,
109 	},{
110 		.fourcc   = V4L2_PIX_FMT_RGB565X,
111 		.depth    = 16,
112 		.pm       = 0x10 | 0x80,
113 		.bswap    = 1,
114 	},{
115 		.fourcc   = V4L2_PIX_FMT_BGR24,
116 		.depth    = 24,
117 		.pm       = 0x11,
118 	},{
119 		.fourcc   = V4L2_PIX_FMT_RGB24,
120 		.depth    = 24,
121 		.pm       = 0x11,
122 		.bswap    = 1,
123 	},{
124 		.fourcc   = V4L2_PIX_FMT_BGR32,
125 		.depth    = 32,
126 		.pm       = 0x12,
127 	},{
128 		.fourcc   = V4L2_PIX_FMT_RGB32,
129 		.depth    = 32,
130 		.pm       = 0x12,
131 		.bswap    = 1,
132 		.wswap    = 1,
133 	},{
134 		.fourcc   = V4L2_PIX_FMT_YUYV,
135 		.depth    = 16,
136 		.pm       = 0x00,
137 		.bswap    = 1,
138 		.yuv      = 1,
139 	},{
140 		.fourcc   = V4L2_PIX_FMT_UYVY,
141 		.depth    = 16,
142 		.pm       = 0x00,
143 		.yuv      = 1,
144 	},{
145 		.fourcc   = V4L2_PIX_FMT_YUV422P,
146 		.depth    = 16,
147 		.pm       = 0x09,
148 		.yuv      = 1,
149 		.planar   = 1,
150 		.hshift   = 1,
151 		.vshift   = 0,
152 	},{
153 		.fourcc   = V4L2_PIX_FMT_YUV420,
154 		.depth    = 12,
155 		.pm       = 0x0a,
156 		.yuv      = 1,
157 		.planar   = 1,
158 		.hshift   = 1,
159 		.vshift   = 1,
160 	},{
161 		.fourcc   = V4L2_PIX_FMT_YVU420,
162 		.depth    = 12,
163 		.pm       = 0x0a,
164 		.yuv      = 1,
165 		.planar   = 1,
166 		.uvswap   = 1,
167 		.hshift   = 1,
168 		.vshift   = 1,
169 	}
170 };
171 #define FORMATS ARRAY_SIZE(formats)
172 
173 #define NORM_625_50			\
174 		.h_start       = 0,	\
175 		.h_stop        = 719,	\
176 		.video_v_start = 24,	\
177 		.video_v_stop  = 311,	\
178 		.vbi_v_start_0 = 7,	\
179 		.vbi_v_stop_0  = 23,	\
180 		.vbi_v_start_1 = 319,   \
181 		.src_timing    = 4
182 
183 #define NORM_525_60			\
184 		.h_start       = 0,	\
185 		.h_stop        = 719,	\
186 		.video_v_start = 23,	\
187 		.video_v_stop  = 262,	\
188 		.vbi_v_start_0 = 10,	\
189 		.vbi_v_stop_0  = 21,	\
190 		.vbi_v_start_1 = 273,	\
191 		.src_timing    = 7
192 
193 static struct saa7134_tvnorm tvnorms[] = {
194 	{
195 		.name          = "PAL", /* autodetect */
196 		.id            = V4L2_STD_PAL,
197 		NORM_625_50,
198 
199 		.sync_control  = 0x18,
200 		.luma_control  = 0x40,
201 		.chroma_ctrl1  = 0x81,
202 		.chroma_gain   = 0x2a,
203 		.chroma_ctrl2  = 0x06,
204 		.vgate_misc    = 0x1c,
205 
206 	},{
207 		.name          = "PAL-BG",
208 		.id            = V4L2_STD_PAL_BG,
209 		NORM_625_50,
210 
211 		.sync_control  = 0x18,
212 		.luma_control  = 0x40,
213 		.chroma_ctrl1  = 0x81,
214 		.chroma_gain   = 0x2a,
215 		.chroma_ctrl2  = 0x06,
216 		.vgate_misc    = 0x1c,
217 
218 	},{
219 		.name          = "PAL-I",
220 		.id            = V4L2_STD_PAL_I,
221 		NORM_625_50,
222 
223 		.sync_control  = 0x18,
224 		.luma_control  = 0x40,
225 		.chroma_ctrl1  = 0x81,
226 		.chroma_gain   = 0x2a,
227 		.chroma_ctrl2  = 0x06,
228 		.vgate_misc    = 0x1c,
229 
230 	},{
231 		.name          = "PAL-DK",
232 		.id            = V4L2_STD_PAL_DK,
233 		NORM_625_50,
234 
235 		.sync_control  = 0x18,
236 		.luma_control  = 0x40,
237 		.chroma_ctrl1  = 0x81,
238 		.chroma_gain   = 0x2a,
239 		.chroma_ctrl2  = 0x06,
240 		.vgate_misc    = 0x1c,
241 
242 	},{
243 		.name          = "NTSC",
244 		.id            = V4L2_STD_NTSC,
245 		NORM_525_60,
246 
247 		.sync_control  = 0x59,
248 		.luma_control  = 0x40,
249 		.chroma_ctrl1  = 0x89,
250 		.chroma_gain   = 0x2a,
251 		.chroma_ctrl2  = 0x0e,
252 		.vgate_misc    = 0x18,
253 
254 	},{
255 		.name          = "SECAM",
256 		.id            = V4L2_STD_SECAM,
257 		NORM_625_50,
258 
259 		.sync_control  = 0x18,
260 		.luma_control  = 0x1b,
261 		.chroma_ctrl1  = 0xd1,
262 		.chroma_gain   = 0x80,
263 		.chroma_ctrl2  = 0x00,
264 		.vgate_misc    = 0x1c,
265 
266 	},{
267 		.name          = "SECAM-DK",
268 		.id            = V4L2_STD_SECAM_DK,
269 		NORM_625_50,
270 
271 		.sync_control  = 0x18,
272 		.luma_control  = 0x1b,
273 		.chroma_ctrl1  = 0xd1,
274 		.chroma_gain   = 0x80,
275 		.chroma_ctrl2  = 0x00,
276 		.vgate_misc    = 0x1c,
277 
278 	},{
279 		.name          = "SECAM-L",
280 		.id            = V4L2_STD_SECAM_L,
281 		NORM_625_50,
282 
283 		.sync_control  = 0x18,
284 		.luma_control  = 0x1b,
285 		.chroma_ctrl1  = 0xd1,
286 		.chroma_gain   = 0x80,
287 		.chroma_ctrl2  = 0x00,
288 		.vgate_misc    = 0x1c,
289 
290 	},{
291 		.name          = "SECAM-Lc",
292 		.id            = V4L2_STD_SECAM_LC,
293 		NORM_625_50,
294 
295 		.sync_control  = 0x18,
296 		.luma_control  = 0x1b,
297 		.chroma_ctrl1  = 0xd1,
298 		.chroma_gain   = 0x80,
299 		.chroma_ctrl2  = 0x00,
300 		.vgate_misc    = 0x1c,
301 
302 	},{
303 		.name          = "PAL-M",
304 		.id            = V4L2_STD_PAL_M,
305 		NORM_525_60,
306 
307 		.sync_control  = 0x59,
308 		.luma_control  = 0x40,
309 		.chroma_ctrl1  = 0xb9,
310 		.chroma_gain   = 0x2a,
311 		.chroma_ctrl2  = 0x0e,
312 		.vgate_misc    = 0x18,
313 
314 	},{
315 		.name          = "PAL-Nc",
316 		.id            = V4L2_STD_PAL_Nc,
317 		NORM_625_50,
318 
319 		.sync_control  = 0x18,
320 		.luma_control  = 0x40,
321 		.chroma_ctrl1  = 0xa1,
322 		.chroma_gain   = 0x2a,
323 		.chroma_ctrl2  = 0x06,
324 		.vgate_misc    = 0x1c,
325 
326 	},{
327 		.name          = "PAL-60",
328 		.id            = V4L2_STD_PAL_60,
329 
330 		.h_start       = 0,
331 		.h_stop        = 719,
332 		.video_v_start = 23,
333 		.video_v_stop  = 262,
334 		.vbi_v_start_0 = 10,
335 		.vbi_v_stop_0  = 21,
336 		.vbi_v_start_1 = 273,
337 		.src_timing    = 7,
338 
339 		.sync_control  = 0x18,
340 		.luma_control  = 0x40,
341 		.chroma_ctrl1  = 0x81,
342 		.chroma_gain   = 0x2a,
343 		.chroma_ctrl2  = 0x06,
344 		.vgate_misc    = 0x1c,
345 	}
346 };
347 #define TVNORMS ARRAY_SIZE(tvnorms)
348 
349 static struct saa7134_format* format_by_fourcc(unsigned int fourcc)
350 {
351 	unsigned int i;
352 
353 	for (i = 0; i < FORMATS; i++)
354 		if (formats[i].fourcc == fourcc)
355 			return formats+i;
356 	return NULL;
357 }
358 
359 /* ------------------------------------------------------------------ */
360 
361 static void set_tvnorm(struct saa7134_dev *dev, struct saa7134_tvnorm *norm)
362 {
363 	video_dbg("set tv norm = %s\n", norm->name);
364 	dev->tvnorm = norm;
365 
366 	/* setup cropping */
367 	dev->crop_bounds.left    = norm->h_start;
368 	dev->crop_defrect.left   = norm->h_start;
369 	dev->crop_bounds.width   = norm->h_stop - norm->h_start +1;
370 	dev->crop_defrect.width  = norm->h_stop - norm->h_start +1;
371 
372 	dev->crop_bounds.top     = (norm->vbi_v_stop_0+1)*2;
373 	dev->crop_defrect.top    = norm->video_v_start*2;
374 	dev->crop_bounds.height  = ((norm->id & V4L2_STD_525_60) ? 524 : 624)
375 		- dev->crop_bounds.top;
376 	dev->crop_defrect.height = (norm->video_v_stop - norm->video_v_start +1)*2;
377 
378 	dev->crop_current = dev->crop_defrect;
379 
380 	saa7134_set_tvnorm_hw(dev);
381 }
382 
383 static void video_mux(struct saa7134_dev *dev, int input)
384 {
385 	video_dbg("video input = %d [%s]\n",
386 		  input, saa7134_input_name[card_in(dev, input).type]);
387 	dev->ctl_input = input;
388 	set_tvnorm(dev, dev->tvnorm);
389 	saa7134_tvaudio_setinput(dev, &card_in(dev, input));
390 }
391 
392 
393 static void saa7134_set_decoder(struct saa7134_dev *dev)
394 {
395 	int luma_control, sync_control, chroma_ctrl1, mux;
396 
397 	struct saa7134_tvnorm *norm = dev->tvnorm;
398 	mux = card_in(dev, dev->ctl_input).vmux;
399 
400 	luma_control = norm->luma_control;
401 	sync_control = norm->sync_control;
402 	chroma_ctrl1 = norm->chroma_ctrl1;
403 
404 	if (mux > 5)
405 		luma_control |= 0x80; /* svideo */
406 	if (noninterlaced || dev->nosignal)
407 		sync_control |= 0x20;
408 
409 	/* switch on auto standard detection */
410 	sync_control |= SAA7134_SYNC_CTRL_AUFD;
411 	chroma_ctrl1 |= SAA7134_CHROMA_CTRL1_AUTO0;
412 	chroma_ctrl1 &= ~SAA7134_CHROMA_CTRL1_FCTC;
413 	luma_control &= ~SAA7134_LUMA_CTRL_LDEL;
414 
415 	/* setup video decoder */
416 	saa_writeb(SAA7134_INCR_DELAY,            0x08);
417 	saa_writeb(SAA7134_ANALOG_IN_CTRL1,       0xc0 | mux);
418 	saa_writeb(SAA7134_ANALOG_IN_CTRL2,       0x00);
419 
420 	saa_writeb(SAA7134_ANALOG_IN_CTRL3,       0x90);
421 	saa_writeb(SAA7134_ANALOG_IN_CTRL4,       0x90);
422 	saa_writeb(SAA7134_HSYNC_START,           0xeb);
423 	saa_writeb(SAA7134_HSYNC_STOP,            0xe0);
424 	saa_writeb(SAA7134_SOURCE_TIMING1,        norm->src_timing);
425 
426 	saa_writeb(SAA7134_SYNC_CTRL,             sync_control);
427 	saa_writeb(SAA7134_LUMA_CTRL,             luma_control);
428 	saa_writeb(SAA7134_DEC_LUMA_BRIGHT,       dev->ctl_bright);
429 
430 	saa_writeb(SAA7134_DEC_LUMA_CONTRAST,
431 		dev->ctl_invert ? -dev->ctl_contrast : dev->ctl_contrast);
432 
433 	saa_writeb(SAA7134_DEC_CHROMA_SATURATION,
434 		dev->ctl_invert ? -dev->ctl_saturation : dev->ctl_saturation);
435 
436 	saa_writeb(SAA7134_DEC_CHROMA_HUE,        dev->ctl_hue);
437 	saa_writeb(SAA7134_CHROMA_CTRL1,          chroma_ctrl1);
438 	saa_writeb(SAA7134_CHROMA_GAIN,           norm->chroma_gain);
439 
440 	saa_writeb(SAA7134_CHROMA_CTRL2,          norm->chroma_ctrl2);
441 	saa_writeb(SAA7134_MODE_DELAY_CTRL,       0x00);
442 
443 	saa_writeb(SAA7134_ANALOG_ADC,            0x01);
444 	saa_writeb(SAA7134_VGATE_START,           0x11);
445 	saa_writeb(SAA7134_VGATE_STOP,            0xfe);
446 	saa_writeb(SAA7134_MISC_VGATE_MSB,        norm->vgate_misc);
447 	saa_writeb(SAA7134_RAW_DATA_GAIN,         0x40);
448 	saa_writeb(SAA7134_RAW_DATA_OFFSET,       0x80);
449 }
450 
451 void saa7134_set_tvnorm_hw(struct saa7134_dev *dev)
452 {
453 	saa7134_set_decoder(dev);
454 
455 	saa_call_all(dev, video, s_std, dev->tvnorm->id);
456 	/* Set the correct norm for the saa6752hs. This function
457 	   does nothing if there is no saa6752hs. */
458 	saa_call_empress(dev, video, s_std, dev->tvnorm->id);
459 }
460 
461 static void set_h_prescale(struct saa7134_dev *dev, int task, int prescale)
462 {
463 	static const struct {
464 		int xpsc;
465 		int xacl;
466 		int xc2_1;
467 		int xdcg;
468 		int vpfy;
469 	} vals[] = {
470 		/* XPSC XACL XC2_1 XDCG VPFY */
471 		{    1,   0,    0,    0,   0 },
472 		{    2,   2,    1,    2,   2 },
473 		{    3,   4,    1,    3,   2 },
474 		{    4,   8,    1,    4,   2 },
475 		{    5,   8,    1,    4,   2 },
476 		{    6,   8,    1,    4,   3 },
477 		{    7,   8,    1,    4,   3 },
478 		{    8,  15,    0,    4,   3 },
479 		{    9,  15,    0,    4,   3 },
480 		{   10,  16,    1,    5,   3 },
481 	};
482 	static const int count = ARRAY_SIZE(vals);
483 	int i;
484 
485 	for (i = 0; i < count; i++)
486 		if (vals[i].xpsc == prescale)
487 			break;
488 	if (i == count)
489 		return;
490 
491 	saa_writeb(SAA7134_H_PRESCALE(task), vals[i].xpsc);
492 	saa_writeb(SAA7134_ACC_LENGTH(task), vals[i].xacl);
493 	saa_writeb(SAA7134_LEVEL_CTRL(task),
494 		   (vals[i].xc2_1 << 3) | (vals[i].xdcg));
495 	saa_andorb(SAA7134_FIR_PREFILTER_CTRL(task), 0x0f,
496 		   (vals[i].vpfy << 2) | vals[i].vpfy);
497 }
498 
499 static void set_v_scale(struct saa7134_dev *dev, int task, int yscale)
500 {
501 	int val,mirror;
502 
503 	saa_writeb(SAA7134_V_SCALE_RATIO1(task), yscale &  0xff);
504 	saa_writeb(SAA7134_V_SCALE_RATIO2(task), yscale >> 8);
505 
506 	mirror = (dev->ctl_mirror) ? 0x02 : 0x00;
507 	if (yscale < 2048) {
508 		/* LPI */
509 		video_dbg("yscale LPI yscale=%d\n", yscale);
510 		saa_writeb(SAA7134_V_FILTER(task), 0x00 | mirror);
511 		saa_writeb(SAA7134_LUMA_CONTRAST(task), 0x40);
512 		saa_writeb(SAA7134_CHROMA_SATURATION(task), 0x40);
513 	} else {
514 		/* ACM */
515 		val = 0x40 * 1024 / yscale;
516 		video_dbg("yscale ACM yscale=%d val=0x%x\n", yscale, val);
517 		saa_writeb(SAA7134_V_FILTER(task), 0x01 | mirror);
518 		saa_writeb(SAA7134_LUMA_CONTRAST(task), val);
519 		saa_writeb(SAA7134_CHROMA_SATURATION(task), val);
520 	}
521 	saa_writeb(SAA7134_LUMA_BRIGHT(task),       0x80);
522 }
523 
524 static void set_size(struct saa7134_dev *dev, int task,
525 		     int width, int height, int interlace)
526 {
527 	int prescale,xscale,yscale,y_even,y_odd;
528 	int h_start, h_stop, v_start, v_stop;
529 	int div = interlace ? 2 : 1;
530 
531 	/* setup video scaler */
532 	h_start = dev->crop_current.left;
533 	v_start = dev->crop_current.top/2;
534 	h_stop  = (dev->crop_current.left + dev->crop_current.width -1);
535 	v_stop  = (dev->crop_current.top + dev->crop_current.height -1)/2;
536 
537 	saa_writeb(SAA7134_VIDEO_H_START1(task), h_start &  0xff);
538 	saa_writeb(SAA7134_VIDEO_H_START2(task), h_start >> 8);
539 	saa_writeb(SAA7134_VIDEO_H_STOP1(task),  h_stop  &  0xff);
540 	saa_writeb(SAA7134_VIDEO_H_STOP2(task),  h_stop  >> 8);
541 	saa_writeb(SAA7134_VIDEO_V_START1(task), v_start &  0xff);
542 	saa_writeb(SAA7134_VIDEO_V_START2(task), v_start >> 8);
543 	saa_writeb(SAA7134_VIDEO_V_STOP1(task),  v_stop  &  0xff);
544 	saa_writeb(SAA7134_VIDEO_V_STOP2(task),  v_stop  >> 8);
545 
546 	prescale = dev->crop_current.width / width;
547 	if (0 == prescale)
548 		prescale = 1;
549 	xscale = 1024 * dev->crop_current.width / prescale / width;
550 	yscale = 512 * div * dev->crop_current.height / height;
551 	video_dbg("prescale=%d xscale=%d yscale=%d\n",
552 		  prescale, xscale, yscale);
553 	set_h_prescale(dev,task,prescale);
554 	saa_writeb(SAA7134_H_SCALE_INC1(task),      xscale &  0xff);
555 	saa_writeb(SAA7134_H_SCALE_INC2(task),      xscale >> 8);
556 	set_v_scale(dev,task,yscale);
557 
558 	saa_writeb(SAA7134_VIDEO_PIXELS1(task),     width  & 0xff);
559 	saa_writeb(SAA7134_VIDEO_PIXELS2(task),     width  >> 8);
560 	saa_writeb(SAA7134_VIDEO_LINES1(task),      height/div & 0xff);
561 	saa_writeb(SAA7134_VIDEO_LINES2(task),      height/div >> 8);
562 
563 	/* deinterlace y offsets */
564 	y_odd  = dev->ctl_y_odd;
565 	y_even = dev->ctl_y_even;
566 	saa_writeb(SAA7134_V_PHASE_OFFSET0(task), y_odd);
567 	saa_writeb(SAA7134_V_PHASE_OFFSET1(task), y_even);
568 	saa_writeb(SAA7134_V_PHASE_OFFSET2(task), y_odd);
569 	saa_writeb(SAA7134_V_PHASE_OFFSET3(task), y_even);
570 }
571 
572 /* ------------------------------------------------------------------ */
573 
574 /*
575  * Media Controller helper functions
576  */
577 
578 static int saa7134_enable_analog_tuner(struct saa7134_dev *dev)
579 {
580 #ifdef CONFIG_MEDIA_CONTROLLER
581 	struct media_device *mdev = dev->media_dev;
582 	struct media_entity *source;
583 	struct media_link *link, *found_link = NULL;
584 	int ret, active_links = 0;
585 
586 	if (!mdev || !dev->decoder)
587 		return 0;
588 
589 	/*
590 	 * This will find the tuner that is connected into the decoder.
591 	 * Technically, this is not 100% correct, as the device may be
592 	 * using an analog input instead of the tuner. However, as we can't
593 	 * do DVB streaming while the DMA engine is being used for V4L2,
594 	 * this should be enough for the actual needs.
595 	 */
596 	list_for_each_entry(link, &dev->decoder->links, list) {
597 		if (link->sink->entity == dev->decoder) {
598 			found_link = link;
599 			if (link->flags & MEDIA_LNK_FL_ENABLED)
600 				active_links++;
601 			break;
602 		}
603 	}
604 
605 	if (active_links == 1 || !found_link)
606 		return 0;
607 
608 	source = found_link->source->entity;
609 	list_for_each_entry(link, &source->links, list) {
610 		struct media_entity *sink;
611 		int flags = 0;
612 
613 		sink = link->sink->entity;
614 
615 		if (sink == dev->decoder)
616 			flags = MEDIA_LNK_FL_ENABLED;
617 
618 		ret = media_entity_setup_link(link, flags);
619 		if (ret) {
620 			pr_err("Couldn't change link %s->%s to %s. Error %d\n",
621 			       source->name, sink->name,
622 			       flags ? "enabled" : "disabled",
623 			       ret);
624 			return ret;
625 		}
626 	}
627 #endif
628 	return 0;
629 }
630 
631 /* ------------------------------------------------------------------ */
632 
633 static int buffer_activate(struct saa7134_dev *dev,
634 			   struct saa7134_buf *buf,
635 			   struct saa7134_buf *next)
636 {
637 	struct saa7134_dmaqueue *dmaq = buf->vb2.vb2_buf.vb2_queue->drv_priv;
638 	unsigned long base,control,bpl;
639 	unsigned long bpl_uv, lines_uv, base2, base3; /* planar */
640 
641 	video_dbg("buffer_activate buf=%p\n", buf);
642 	buf->top_seen = 0;
643 
644 	set_size(dev, TASK_A, dev->width, dev->height,
645 		 V4L2_FIELD_HAS_BOTH(dev->field));
646 	if (dev->fmt->yuv)
647 		saa_andorb(SAA7134_DATA_PATH(TASK_A), 0x3f, 0x03);
648 	else
649 		saa_andorb(SAA7134_DATA_PATH(TASK_A), 0x3f, 0x01);
650 	saa_writeb(SAA7134_OFMT_VIDEO_A, dev->fmt->pm);
651 
652 	/* DMA: setup channel 0 (= Video Task A0) */
653 	base  = saa7134_buffer_base(buf);
654 	if (dev->fmt->planar)
655 		bpl = dev->width;
656 	else
657 		bpl = (dev->width * dev->fmt->depth) / 8;
658 	control = SAA7134_RS_CONTROL_BURST_16 |
659 		SAA7134_RS_CONTROL_ME |
660 		(dmaq->pt.dma >> 12);
661 	if (dev->fmt->bswap)
662 		control |= SAA7134_RS_CONTROL_BSWAP;
663 	if (dev->fmt->wswap)
664 		control |= SAA7134_RS_CONTROL_WSWAP;
665 	if (V4L2_FIELD_HAS_BOTH(dev->field)) {
666 		/* interlaced */
667 		saa_writel(SAA7134_RS_BA1(0),base);
668 		saa_writel(SAA7134_RS_BA2(0),base+bpl);
669 		saa_writel(SAA7134_RS_PITCH(0),bpl*2);
670 	} else {
671 		/* non-interlaced */
672 		saa_writel(SAA7134_RS_BA1(0),base);
673 		saa_writel(SAA7134_RS_BA2(0),base);
674 		saa_writel(SAA7134_RS_PITCH(0),bpl);
675 	}
676 	saa_writel(SAA7134_RS_CONTROL(0),control);
677 
678 	if (dev->fmt->planar) {
679 		/* DMA: setup channel 4+5 (= planar task A) */
680 		bpl_uv   = bpl >> dev->fmt->hshift;
681 		lines_uv = dev->height >> dev->fmt->vshift;
682 		base2    = base + bpl * dev->height;
683 		base3    = base2 + bpl_uv * lines_uv;
684 		if (dev->fmt->uvswap)
685 			swap(base2, base3);
686 		video_dbg("uv: bpl=%ld lines=%ld base2/3=%ld/%ld\n",
687 			bpl_uv,lines_uv,base2,base3);
688 		if (V4L2_FIELD_HAS_BOTH(dev->field)) {
689 			/* interlaced */
690 			saa_writel(SAA7134_RS_BA1(4),base2);
691 			saa_writel(SAA7134_RS_BA2(4),base2+bpl_uv);
692 			saa_writel(SAA7134_RS_PITCH(4),bpl_uv*2);
693 			saa_writel(SAA7134_RS_BA1(5),base3);
694 			saa_writel(SAA7134_RS_BA2(5),base3+bpl_uv);
695 			saa_writel(SAA7134_RS_PITCH(5),bpl_uv*2);
696 		} else {
697 			/* non-interlaced */
698 			saa_writel(SAA7134_RS_BA1(4),base2);
699 			saa_writel(SAA7134_RS_BA2(4),base2);
700 			saa_writel(SAA7134_RS_PITCH(4),bpl_uv);
701 			saa_writel(SAA7134_RS_BA1(5),base3);
702 			saa_writel(SAA7134_RS_BA2(5),base3);
703 			saa_writel(SAA7134_RS_PITCH(5),bpl_uv);
704 		}
705 		saa_writel(SAA7134_RS_CONTROL(4),control);
706 		saa_writel(SAA7134_RS_CONTROL(5),control);
707 	}
708 
709 	/* start DMA */
710 	saa7134_set_dmabits(dev);
711 	mod_timer(&dmaq->timeout, jiffies + BUFFER_TIMEOUT);
712 	return 0;
713 }
714 
715 static int buffer_init(struct vb2_buffer *vb2)
716 {
717 	struct saa7134_dmaqueue *dmaq = vb2->vb2_queue->drv_priv;
718 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb2);
719 	struct saa7134_buf *buf = container_of(vbuf, struct saa7134_buf, vb2);
720 
721 	dmaq->curr = NULL;
722 	buf->activate = buffer_activate;
723 	return 0;
724 }
725 
726 static int buffer_prepare(struct vb2_buffer *vb2)
727 {
728 	struct saa7134_dmaqueue *dmaq = vb2->vb2_queue->drv_priv;
729 	struct saa7134_dev *dev = dmaq->dev;
730 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb2);
731 	struct saa7134_buf *buf = container_of(vbuf, struct saa7134_buf, vb2);
732 	struct sg_table *dma = vb2_dma_sg_plane_desc(vb2, 0);
733 	unsigned int size;
734 
735 	if (dma->sgl->offset) {
736 		pr_err("The buffer is not page-aligned\n");
737 		return -EINVAL;
738 	}
739 	size = (dev->width * dev->height * dev->fmt->depth) >> 3;
740 	if (vb2_plane_size(vb2, 0) < size)
741 		return -EINVAL;
742 
743 	vb2_set_plane_payload(vb2, 0, size);
744 	vbuf->field = dev->field;
745 
746 	return saa7134_pgtable_build(dev->pci, &dmaq->pt, dma->sgl, dma->nents,
747 				    saa7134_buffer_startpage(buf));
748 }
749 
750 static int queue_setup(struct vb2_queue *q,
751 			   unsigned int *nbuffers, unsigned int *nplanes,
752 			   unsigned int sizes[], struct device *alloc_devs[])
753 {
754 	struct saa7134_dmaqueue *dmaq = q->drv_priv;
755 	struct saa7134_dev *dev = dmaq->dev;
756 	int size = dev->fmt->depth * dev->width * dev->height >> 3;
757 
758 	if (dev->width    < 48 ||
759 	    dev->height   < 32 ||
760 	    dev->width/4  > dev->crop_current.width  ||
761 	    dev->height/4 > dev->crop_current.height ||
762 	    dev->width    > dev->crop_bounds.width  ||
763 	    dev->height   > dev->crop_bounds.height)
764 		return -EINVAL;
765 
766 	*nbuffers = saa7134_buffer_count(size, *nbuffers);
767 	*nplanes = 1;
768 	sizes[0] = size;
769 
770 	saa7134_enable_analog_tuner(dev);
771 
772 	return 0;
773 }
774 
775 /*
776  * move buffer to hardware queue
777  */
778 void saa7134_vb2_buffer_queue(struct vb2_buffer *vb)
779 {
780 	struct saa7134_dmaqueue *dmaq = vb->vb2_queue->drv_priv;
781 	struct saa7134_dev *dev = dmaq->dev;
782 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
783 	struct saa7134_buf *buf = container_of(vbuf, struct saa7134_buf, vb2);
784 
785 	saa7134_buffer_queue(dev, dmaq, buf);
786 }
787 EXPORT_SYMBOL_GPL(saa7134_vb2_buffer_queue);
788 
789 int saa7134_vb2_start_streaming(struct vb2_queue *vq, unsigned int count)
790 {
791 	struct saa7134_dmaqueue *dmaq = vq->drv_priv;
792 	struct saa7134_dev *dev = dmaq->dev;
793 
794 	/*
795 	 * Planar video capture and TS share the same DMA channel,
796 	 * so only one can be active at a time.
797 	 */
798 	if (card_is_empress(dev) && vb2_is_busy(&dev->empress_vbq) &&
799 	    dmaq == &dev->video_q && dev->fmt->planar) {
800 		struct saa7134_buf *buf, *tmp;
801 
802 		list_for_each_entry_safe(buf, tmp, &dmaq->queue, entry) {
803 			list_del(&buf->entry);
804 			vb2_buffer_done(&buf->vb2.vb2_buf,
805 					VB2_BUF_STATE_QUEUED);
806 		}
807 		if (dmaq->curr) {
808 			vb2_buffer_done(&dmaq->curr->vb2.vb2_buf,
809 					VB2_BUF_STATE_QUEUED);
810 			dmaq->curr = NULL;
811 		}
812 		return -EBUSY;
813 	}
814 
815 	/* The SAA7134 has a 1K FIFO; the datasheet suggests that when
816 	 * configured conservatively, there's 22 usec of buffering for video.
817 	 * We therefore request a DMA latency of 20 usec, giving us 2 usec of
818 	 * margin in case the FIFO is configured differently to the datasheet.
819 	 * Unfortunately, I lack register-level documentation to check the
820 	 * Linux FIFO setup and confirm the perfect value.
821 	 */
822 	if ((dmaq == &dev->video_q && !vb2_is_streaming(&dev->vbi_vbq)) ||
823 	    (dmaq == &dev->vbi_q && !vb2_is_streaming(&dev->video_vbq)))
824 		cpu_latency_qos_add_request(&dev->qos_request, 20);
825 	dmaq->seq_nr = 0;
826 
827 	return 0;
828 }
829 
830 void saa7134_vb2_stop_streaming(struct vb2_queue *vq)
831 {
832 	struct saa7134_dmaqueue *dmaq = vq->drv_priv;
833 	struct saa7134_dev *dev = dmaq->dev;
834 
835 	saa7134_stop_streaming(dev, dmaq);
836 
837 	if ((dmaq == &dev->video_q && !vb2_is_streaming(&dev->vbi_vbq)) ||
838 	    (dmaq == &dev->vbi_q && !vb2_is_streaming(&dev->video_vbq)))
839 		cpu_latency_qos_remove_request(&dev->qos_request);
840 }
841 
842 static const struct vb2_ops vb2_qops = {
843 	.queue_setup	= queue_setup,
844 	.buf_init	= buffer_init,
845 	.buf_prepare	= buffer_prepare,
846 	.buf_queue	= saa7134_vb2_buffer_queue,
847 	.start_streaming = saa7134_vb2_start_streaming,
848 	.stop_streaming = saa7134_vb2_stop_streaming,
849 };
850 
851 /* ------------------------------------------------------------------ */
852 
853 static int saa7134_s_ctrl(struct v4l2_ctrl *ctrl)
854 {
855 	struct saa7134_dev *dev = container_of(ctrl->handler, struct saa7134_dev, ctrl_handler);
856 
857 	switch (ctrl->id) {
858 	case V4L2_CID_BRIGHTNESS:
859 		dev->ctl_bright = ctrl->val;
860 		saa_writeb(SAA7134_DEC_LUMA_BRIGHT, ctrl->val);
861 		break;
862 	case V4L2_CID_HUE:
863 		dev->ctl_hue = ctrl->val;
864 		saa_writeb(SAA7134_DEC_CHROMA_HUE, ctrl->val);
865 		break;
866 	case V4L2_CID_CONTRAST:
867 		dev->ctl_contrast = ctrl->val;
868 		saa_writeb(SAA7134_DEC_LUMA_CONTRAST,
869 			   dev->ctl_invert ? -dev->ctl_contrast : dev->ctl_contrast);
870 		break;
871 	case V4L2_CID_SATURATION:
872 		dev->ctl_saturation = ctrl->val;
873 		saa_writeb(SAA7134_DEC_CHROMA_SATURATION,
874 			   dev->ctl_invert ? -dev->ctl_saturation : dev->ctl_saturation);
875 		break;
876 	case V4L2_CID_AUDIO_MUTE:
877 		dev->ctl_mute = ctrl->val;
878 		saa7134_tvaudio_setmute(dev);
879 		break;
880 	case V4L2_CID_AUDIO_VOLUME:
881 		dev->ctl_volume = ctrl->val;
882 		saa7134_tvaudio_setvolume(dev,dev->ctl_volume);
883 		break;
884 	case V4L2_CID_PRIVATE_INVERT:
885 		dev->ctl_invert = ctrl->val;
886 		saa_writeb(SAA7134_DEC_LUMA_CONTRAST,
887 			   dev->ctl_invert ? -dev->ctl_contrast : dev->ctl_contrast);
888 		saa_writeb(SAA7134_DEC_CHROMA_SATURATION,
889 			   dev->ctl_invert ? -dev->ctl_saturation : dev->ctl_saturation);
890 		break;
891 	case V4L2_CID_HFLIP:
892 		dev->ctl_mirror = ctrl->val;
893 		break;
894 	case V4L2_CID_PRIVATE_Y_EVEN:
895 		dev->ctl_y_even = ctrl->val;
896 		break;
897 	case V4L2_CID_PRIVATE_Y_ODD:
898 		dev->ctl_y_odd = ctrl->val;
899 		break;
900 	case V4L2_CID_PRIVATE_AUTOMUTE:
901 	{
902 		struct v4l2_priv_tun_config tda9887_cfg;
903 
904 		tda9887_cfg.tuner = TUNER_TDA9887;
905 		tda9887_cfg.priv = &dev->tda9887_conf;
906 
907 		dev->ctl_automute = ctrl->val;
908 		if (dev->tda9887_conf) {
909 			if (dev->ctl_automute)
910 				dev->tda9887_conf |= TDA9887_AUTOMUTE;
911 			else
912 				dev->tda9887_conf &= ~TDA9887_AUTOMUTE;
913 
914 			saa_call_all(dev, tuner, s_config, &tda9887_cfg);
915 		}
916 		break;
917 	}
918 	default:
919 		return -EINVAL;
920 	}
921 	return 0;
922 }
923 
924 /* ------------------------------------------------------------------ */
925 
926 static int video_open(struct file *file)
927 {
928 	struct video_device *vdev = video_devdata(file);
929 	struct saa7134_dev *dev = video_drvdata(file);
930 	int ret = v4l2_fh_open(file);
931 
932 	if (ret < 0)
933 		return ret;
934 
935 	mutex_lock(&dev->lock);
936 	if (vdev->vfl_type == VFL_TYPE_RADIO) {
937 		/* switch to radio mode */
938 		saa7134_tvaudio_setinput(dev, &card(dev).radio);
939 		saa_call_all(dev, tuner, s_radio);
940 	} else {
941 		/* switch to video/vbi mode */
942 		video_mux(dev, dev->ctl_input);
943 	}
944 	mutex_unlock(&dev->lock);
945 
946 	return 0;
947 }
948 
949 static int video_release(struct file *file)
950 {
951 	struct video_device *vdev = video_devdata(file);
952 	struct saa7134_dev *dev = video_drvdata(file);
953 	struct saa6588_command cmd;
954 
955 	mutex_lock(&dev->lock);
956 	saa7134_tvaudio_close(dev);
957 
958 	if (vdev->vfl_type == VFL_TYPE_RADIO)
959 		v4l2_fh_release(file);
960 	else
961 		_vb2_fop_release(file, NULL);
962 
963 	/* ts-capture will not work in planar mode, so turn it off Hac: 04.05*/
964 	saa_andorb(SAA7134_OFMT_VIDEO_A, 0x1f, 0);
965 	saa_andorb(SAA7134_OFMT_VIDEO_B, 0x1f, 0);
966 	saa_andorb(SAA7134_OFMT_DATA_A, 0x1f, 0);
967 	saa_andorb(SAA7134_OFMT_DATA_B, 0x1f, 0);
968 
969 	saa_call_all(dev, tuner, standby);
970 	if (vdev->vfl_type == VFL_TYPE_RADIO)
971 		saa_call_all(dev, core, command, SAA6588_CMD_CLOSE, &cmd);
972 	mutex_unlock(&dev->lock);
973 
974 	return 0;
975 }
976 
977 static ssize_t radio_read(struct file *file, char __user *data,
978 			 size_t count, loff_t *ppos)
979 {
980 	struct saa7134_dev *dev = video_drvdata(file);
981 	struct saa6588_command cmd;
982 
983 	cmd.block_count = count/3;
984 	cmd.nonblocking = file->f_flags & O_NONBLOCK;
985 	cmd.buffer = data;
986 	cmd.instance = file;
987 	cmd.result = -ENODEV;
988 
989 	mutex_lock(&dev->lock);
990 	saa_call_all(dev, core, command, SAA6588_CMD_READ, &cmd);
991 	mutex_unlock(&dev->lock);
992 
993 	return cmd.result;
994 }
995 
996 static __poll_t radio_poll(struct file *file, poll_table *wait)
997 {
998 	struct saa7134_dev *dev = video_drvdata(file);
999 	struct saa6588_command cmd;
1000 	__poll_t rc = v4l2_ctrl_poll(file, wait);
1001 
1002 	cmd.instance = file;
1003 	cmd.event_list = wait;
1004 	cmd.poll_mask = 0;
1005 	mutex_lock(&dev->lock);
1006 	saa_call_all(dev, core, command, SAA6588_CMD_POLL, &cmd);
1007 	mutex_unlock(&dev->lock);
1008 
1009 	return rc | cmd.poll_mask;
1010 }
1011 
1012 /* ------------------------------------------------------------------ */
1013 
1014 static int saa7134_try_get_set_fmt_vbi_cap(struct file *file, void *priv,
1015 						struct v4l2_format *f)
1016 {
1017 	struct saa7134_dev *dev = video_drvdata(file);
1018 	struct saa7134_tvnorm *norm = dev->tvnorm;
1019 
1020 	memset(&f->fmt.vbi.reserved, 0, sizeof(f->fmt.vbi.reserved));
1021 	f->fmt.vbi.sampling_rate = 6750000 * 4;
1022 	f->fmt.vbi.samples_per_line = 2048 /* VBI_LINE_LENGTH */;
1023 	f->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
1024 	f->fmt.vbi.offset = 64 * 4;
1025 	f->fmt.vbi.start[0] = norm->vbi_v_start_0;
1026 	f->fmt.vbi.count[0] = norm->vbi_v_stop_0 - norm->vbi_v_start_0 +1;
1027 	f->fmt.vbi.start[1] = norm->vbi_v_start_1;
1028 	f->fmt.vbi.count[1] = f->fmt.vbi.count[0];
1029 	f->fmt.vbi.flags = 0; /* VBI_UNSYNC VBI_INTERLACED */
1030 
1031 	return 0;
1032 }
1033 
1034 static int saa7134_g_fmt_vid_cap(struct file *file, void *priv,
1035 				struct v4l2_format *f)
1036 {
1037 	struct saa7134_dev *dev = video_drvdata(file);
1038 
1039 	f->fmt.pix.width        = dev->width;
1040 	f->fmt.pix.height       = dev->height;
1041 	f->fmt.pix.field        = dev->field;
1042 	f->fmt.pix.pixelformat  = dev->fmt->fourcc;
1043 	if (dev->fmt->planar)
1044 		f->fmt.pix.bytesperline = f->fmt.pix.width;
1045 	else
1046 		f->fmt.pix.bytesperline =
1047 			(f->fmt.pix.width * dev->fmt->depth) / 8;
1048 	f->fmt.pix.sizeimage =
1049 		(f->fmt.pix.height * f->fmt.pix.width * dev->fmt->depth) / 8;
1050 	f->fmt.pix.colorspace   = V4L2_COLORSPACE_SMPTE170M;
1051 	return 0;
1052 }
1053 
1054 static int saa7134_try_fmt_vid_cap(struct file *file, void *priv,
1055 						struct v4l2_format *f)
1056 {
1057 	struct saa7134_dev *dev = video_drvdata(file);
1058 	struct saa7134_format *fmt;
1059 	enum v4l2_field field;
1060 	unsigned int maxw, maxh;
1061 
1062 	fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1063 	if (NULL == fmt)
1064 		return -EINVAL;
1065 
1066 	field = f->fmt.pix.field;
1067 	maxw  = min(dev->crop_current.width*4,  dev->crop_bounds.width);
1068 	maxh  = min(dev->crop_current.height*4, dev->crop_bounds.height);
1069 
1070 	if (V4L2_FIELD_ANY == field) {
1071 		field = (f->fmt.pix.height > maxh/2)
1072 			? V4L2_FIELD_INTERLACED
1073 			: V4L2_FIELD_BOTTOM;
1074 	}
1075 	switch (field) {
1076 	case V4L2_FIELD_TOP:
1077 	case V4L2_FIELD_BOTTOM:
1078 		maxh = maxh / 2;
1079 		break;
1080 	default:
1081 		field = V4L2_FIELD_INTERLACED;
1082 		break;
1083 	}
1084 
1085 	f->fmt.pix.field = field;
1086 	if (f->fmt.pix.width  < 48)
1087 		f->fmt.pix.width  = 48;
1088 	if (f->fmt.pix.height < 32)
1089 		f->fmt.pix.height = 32;
1090 	if (f->fmt.pix.width > maxw)
1091 		f->fmt.pix.width = maxw;
1092 	if (f->fmt.pix.height > maxh)
1093 		f->fmt.pix.height = maxh;
1094 	f->fmt.pix.width &= ~0x03;
1095 	if (fmt->planar)
1096 		f->fmt.pix.bytesperline = f->fmt.pix.width;
1097 	else
1098 		f->fmt.pix.bytesperline =
1099 			(f->fmt.pix.width * fmt->depth) / 8;
1100 	f->fmt.pix.sizeimage =
1101 		(f->fmt.pix.height * f->fmt.pix.width * fmt->depth) / 8;
1102 	f->fmt.pix.colorspace   = V4L2_COLORSPACE_SMPTE170M;
1103 
1104 	return 0;
1105 }
1106 
1107 static int saa7134_s_fmt_vid_cap(struct file *file, void *priv,
1108 					struct v4l2_format *f)
1109 {
1110 	struct saa7134_dev *dev = video_drvdata(file);
1111 	int err;
1112 
1113 	err = saa7134_try_fmt_vid_cap(file, priv, f);
1114 	if (0 != err)
1115 		return err;
1116 
1117 	dev->fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1118 	dev->width = f->fmt.pix.width;
1119 	dev->height = f->fmt.pix.height;
1120 	dev->field = f->fmt.pix.field;
1121 	return 0;
1122 }
1123 
1124 int saa7134_enum_input(struct file *file, void *priv, struct v4l2_input *i)
1125 {
1126 	struct saa7134_dev *dev = video_drvdata(file);
1127 	unsigned int n;
1128 
1129 	n = i->index;
1130 	if (n >= SAA7134_INPUT_MAX)
1131 		return -EINVAL;
1132 	if (card_in(dev, i->index).type == SAA7134_NO_INPUT)
1133 		return -EINVAL;
1134 	i->index = n;
1135 	strscpy(i->name, saa7134_input_name[card_in(dev, n).type],
1136 		sizeof(i->name));
1137 	switch (card_in(dev, n).type) {
1138 	case SAA7134_INPUT_TV:
1139 	case SAA7134_INPUT_TV_MONO:
1140 		i->type = V4L2_INPUT_TYPE_TUNER;
1141 		break;
1142 	default:
1143 		i->type  = V4L2_INPUT_TYPE_CAMERA;
1144 		break;
1145 	}
1146 	if (n == dev->ctl_input) {
1147 		int v1 = saa_readb(SAA7134_STATUS_VIDEO1);
1148 		int v2 = saa_readb(SAA7134_STATUS_VIDEO2);
1149 
1150 		if (0 != (v1 & 0x40))
1151 			i->status |= V4L2_IN_ST_NO_H_LOCK;
1152 		if (0 != (v2 & 0x40))
1153 			i->status |= V4L2_IN_ST_NO_SIGNAL;
1154 		if (0 != (v2 & 0x0e))
1155 			i->status |= V4L2_IN_ST_MACROVISION;
1156 	}
1157 	i->std = SAA7134_NORMS;
1158 	return 0;
1159 }
1160 EXPORT_SYMBOL_GPL(saa7134_enum_input);
1161 
1162 int saa7134_g_input(struct file *file, void *priv, unsigned int *i)
1163 {
1164 	struct saa7134_dev *dev = video_drvdata(file);
1165 
1166 	*i = dev->ctl_input;
1167 	return 0;
1168 }
1169 EXPORT_SYMBOL_GPL(saa7134_g_input);
1170 
1171 int saa7134_s_input(struct file *file, void *priv, unsigned int i)
1172 {
1173 	struct saa7134_dev *dev = video_drvdata(file);
1174 
1175 	if (i >= SAA7134_INPUT_MAX)
1176 		return -EINVAL;
1177 	if (card_in(dev, i).type == SAA7134_NO_INPUT)
1178 		return -EINVAL;
1179 	video_mux(dev, i);
1180 	return 0;
1181 }
1182 EXPORT_SYMBOL_GPL(saa7134_s_input);
1183 
1184 int saa7134_querycap(struct file *file, void *priv,
1185 					struct v4l2_capability *cap)
1186 {
1187 	struct saa7134_dev *dev = video_drvdata(file);
1188 
1189 	strscpy(cap->driver, "saa7134", sizeof(cap->driver));
1190 	strscpy(cap->card, saa7134_boards[dev->board].name,
1191 		sizeof(cap->card));
1192 	cap->capabilities = V4L2_CAP_READWRITE | V4L2_CAP_STREAMING |
1193 			    V4L2_CAP_RADIO | V4L2_CAP_VIDEO_CAPTURE |
1194 			    V4L2_CAP_VBI_CAPTURE | V4L2_CAP_DEVICE_CAPS;
1195 	if (dev->tuner_type != TUNER_ABSENT && dev->tuner_type != UNSET)
1196 		cap->capabilities |= V4L2_CAP_TUNER;
1197 	if (dev->has_rds)
1198 		cap->capabilities |= V4L2_CAP_RDS_CAPTURE;
1199 
1200 	return 0;
1201 }
1202 EXPORT_SYMBOL_GPL(saa7134_querycap);
1203 
1204 int saa7134_s_std(struct file *file, void *priv, v4l2_std_id id)
1205 {
1206 	struct saa7134_dev *dev = video_drvdata(file);
1207 	unsigned int i;
1208 	v4l2_std_id fixup;
1209 
1210 	for (i = 0; i < TVNORMS; i++)
1211 		if (id == tvnorms[i].id)
1212 			break;
1213 
1214 	if (i == TVNORMS)
1215 		for (i = 0; i < TVNORMS; i++)
1216 			if (id & tvnorms[i].id)
1217 				break;
1218 	if (i == TVNORMS)
1219 		return -EINVAL;
1220 
1221 	if ((id & V4L2_STD_SECAM) && (secam[0] != '-')) {
1222 		if (secam[0] == 'L' || secam[0] == 'l') {
1223 			if (secam[1] == 'C' || secam[1] == 'c')
1224 				fixup = V4L2_STD_SECAM_LC;
1225 			else
1226 				fixup = V4L2_STD_SECAM_L;
1227 		} else {
1228 			if (secam[0] == 'D' || secam[0] == 'd')
1229 				fixup = V4L2_STD_SECAM_DK;
1230 			else
1231 				fixup = V4L2_STD_SECAM;
1232 		}
1233 		for (i = 0; i < TVNORMS; i++) {
1234 			if (fixup == tvnorms[i].id)
1235 				break;
1236 		}
1237 		if (i == TVNORMS)
1238 			return -EINVAL;
1239 	}
1240 
1241 	set_tvnorm(dev, &tvnorms[i]);
1242 
1243 	saa7134_tvaudio_do_scan(dev);
1244 	return 0;
1245 }
1246 EXPORT_SYMBOL_GPL(saa7134_s_std);
1247 
1248 int saa7134_g_std(struct file *file, void *priv, v4l2_std_id *id)
1249 {
1250 	struct saa7134_dev *dev = video_drvdata(file);
1251 
1252 	*id = dev->tvnorm->id;
1253 	return 0;
1254 }
1255 EXPORT_SYMBOL_GPL(saa7134_g_std);
1256 
1257 static v4l2_std_id saa7134_read_std(struct saa7134_dev *dev)
1258 {
1259 	static v4l2_std_id stds[] = {
1260 		V4L2_STD_UNKNOWN,
1261 		V4L2_STD_NTSC,
1262 		V4L2_STD_PAL,
1263 		V4L2_STD_SECAM };
1264 
1265 	v4l2_std_id result = 0;
1266 
1267 	u8 st1 = saa_readb(SAA7134_STATUS_VIDEO1);
1268 	u8 st2 = saa_readb(SAA7134_STATUS_VIDEO2);
1269 
1270 	if (!(st2 & 0x1)) /* RDCAP == 0 */
1271 		result = V4L2_STD_UNKNOWN;
1272 	else
1273 		result = stds[st1 & 0x03];
1274 
1275 	return result;
1276 }
1277 
1278 int saa7134_querystd(struct file *file, void *priv, v4l2_std_id *std)
1279 {
1280 	struct saa7134_dev *dev = video_drvdata(file);
1281 	*std &= saa7134_read_std(dev);
1282 	return 0;
1283 }
1284 EXPORT_SYMBOL_GPL(saa7134_querystd);
1285 
1286 static int saa7134_g_pixelaspect(struct file *file, void *priv,
1287 				 int type, struct v4l2_fract *f)
1288 {
1289 	struct saa7134_dev *dev = video_drvdata(file);
1290 
1291 	if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1292 		return -EINVAL;
1293 
1294 	if (dev->tvnorm->id & V4L2_STD_525_60) {
1295 		f->numerator   = 11;
1296 		f->denominator = 10;
1297 	}
1298 	if (dev->tvnorm->id & V4L2_STD_625_50) {
1299 		f->numerator   = 54;
1300 		f->denominator = 59;
1301 	}
1302 	return 0;
1303 }
1304 
1305 static int saa7134_g_selection(struct file *file, void *f, struct v4l2_selection *sel)
1306 {
1307 	struct saa7134_dev *dev = video_drvdata(file);
1308 
1309 	if (sel->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1310 		return -EINVAL;
1311 
1312 	switch (sel->target) {
1313 	case V4L2_SEL_TGT_CROP:
1314 		sel->r = dev->crop_current;
1315 		break;
1316 	case V4L2_SEL_TGT_CROP_DEFAULT:
1317 		sel->r = dev->crop_defrect;
1318 		break;
1319 	case V4L2_SEL_TGT_CROP_BOUNDS:
1320 		sel->r  = dev->crop_bounds;
1321 		break;
1322 	default:
1323 		return -EINVAL;
1324 	}
1325 	return 0;
1326 }
1327 
1328 static int saa7134_s_selection(struct file *file, void *f, struct v4l2_selection *sel)
1329 {
1330 	struct saa7134_dev *dev = video_drvdata(file);
1331 	struct v4l2_rect *b = &dev->crop_bounds;
1332 	struct v4l2_rect *c = &dev->crop_current;
1333 
1334 	if (sel->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1335 		return -EINVAL;
1336 
1337 	if (sel->target != V4L2_SEL_TGT_CROP)
1338 		return -EINVAL;
1339 
1340 	if (vb2_is_streaming(&dev->video_vbq))
1341 		return -EBUSY;
1342 
1343 	*c = sel->r;
1344 	if (c->top < b->top)
1345 		c->top = b->top;
1346 	if (c->top > b->top + b->height)
1347 		c->top = b->top + b->height;
1348 	if (c->height > b->top - c->top + b->height)
1349 		c->height = b->top - c->top + b->height;
1350 
1351 	if (c->left < b->left)
1352 		c->left = b->left;
1353 	if (c->left > b->left + b->width)
1354 		c->left = b->left + b->width;
1355 	if (c->width > b->left - c->left + b->width)
1356 		c->width = b->left - c->left + b->width;
1357 	sel->r = *c;
1358 	return 0;
1359 }
1360 
1361 int saa7134_g_tuner(struct file *file, void *priv,
1362 					struct v4l2_tuner *t)
1363 {
1364 	struct saa7134_dev *dev = video_drvdata(file);
1365 	int n;
1366 
1367 	if (0 != t->index)
1368 		return -EINVAL;
1369 	memset(t, 0, sizeof(*t));
1370 	for (n = 0; n < SAA7134_INPUT_MAX; n++) {
1371 		if (card_in(dev, n).type == SAA7134_INPUT_TV ||
1372 		    card_in(dev, n).type == SAA7134_INPUT_TV_MONO)
1373 			break;
1374 	}
1375 	if (n == SAA7134_INPUT_MAX)
1376 		return -EINVAL;
1377 	if (card_in(dev, n).type != SAA7134_NO_INPUT) {
1378 		strscpy(t->name, "Television", sizeof(t->name));
1379 		t->type = V4L2_TUNER_ANALOG_TV;
1380 		saa_call_all(dev, tuner, g_tuner, t);
1381 		t->capability = V4L2_TUNER_CAP_NORM |
1382 			V4L2_TUNER_CAP_STEREO |
1383 			V4L2_TUNER_CAP_LANG1 |
1384 			V4L2_TUNER_CAP_LANG2;
1385 		t->rxsubchans = saa7134_tvaudio_getstereo(dev);
1386 		t->audmode = saa7134_tvaudio_rx2mode(t->rxsubchans);
1387 	}
1388 	if (0 != (saa_readb(SAA7134_STATUS_VIDEO1) & 0x03))
1389 		t->signal = 0xffff;
1390 	return 0;
1391 }
1392 EXPORT_SYMBOL_GPL(saa7134_g_tuner);
1393 
1394 int saa7134_s_tuner(struct file *file, void *priv,
1395 					const struct v4l2_tuner *t)
1396 {
1397 	struct saa7134_dev *dev = video_drvdata(file);
1398 	int rx, mode;
1399 
1400 	if (0 != t->index)
1401 		return -EINVAL;
1402 
1403 	mode = dev->thread.mode;
1404 	if (UNSET == mode) {
1405 		rx   = saa7134_tvaudio_getstereo(dev);
1406 		mode = saa7134_tvaudio_rx2mode(rx);
1407 	}
1408 	if (mode != t->audmode)
1409 		dev->thread.mode = t->audmode;
1410 
1411 	return 0;
1412 }
1413 EXPORT_SYMBOL_GPL(saa7134_s_tuner);
1414 
1415 int saa7134_g_frequency(struct file *file, void *priv,
1416 					struct v4l2_frequency *f)
1417 {
1418 	struct saa7134_dev *dev = video_drvdata(file);
1419 
1420 	if (0 != f->tuner)
1421 		return -EINVAL;
1422 
1423 	saa_call_all(dev, tuner, g_frequency, f);
1424 
1425 	return 0;
1426 }
1427 EXPORT_SYMBOL_GPL(saa7134_g_frequency);
1428 
1429 int saa7134_s_frequency(struct file *file, void *priv,
1430 					const struct v4l2_frequency *f)
1431 {
1432 	struct saa7134_dev *dev = video_drvdata(file);
1433 
1434 	if (0 != f->tuner)
1435 		return -EINVAL;
1436 
1437 	saa_call_all(dev, tuner, s_frequency, f);
1438 
1439 	saa7134_tvaudio_do_scan(dev);
1440 	return 0;
1441 }
1442 EXPORT_SYMBOL_GPL(saa7134_s_frequency);
1443 
1444 static int saa7134_enum_fmt_vid_cap(struct file *file, void  *priv,
1445 					struct v4l2_fmtdesc *f)
1446 {
1447 	if (f->index >= FORMATS)
1448 		return -EINVAL;
1449 
1450 	f->pixelformat = formats[f->index].fourcc;
1451 
1452 	return 0;
1453 }
1454 
1455 #ifdef CONFIG_VIDEO_ADV_DEBUG
1456 static int vidioc_g_register (struct file *file, void *priv,
1457 			      struct v4l2_dbg_register *reg)
1458 {
1459 	struct saa7134_dev *dev = video_drvdata(file);
1460 
1461 	reg->val = saa_readb(reg->reg & 0xffffff);
1462 	reg->size = 1;
1463 	return 0;
1464 }
1465 
1466 static int vidioc_s_register (struct file *file, void *priv,
1467 				const struct v4l2_dbg_register *reg)
1468 {
1469 	struct saa7134_dev *dev = video_drvdata(file);
1470 
1471 	saa_writeb(reg->reg & 0xffffff, reg->val);
1472 	return 0;
1473 }
1474 #endif
1475 
1476 static int radio_g_tuner(struct file *file, void *priv,
1477 					struct v4l2_tuner *t)
1478 {
1479 	struct saa7134_dev *dev = video_drvdata(file);
1480 
1481 	if (0 != t->index)
1482 		return -EINVAL;
1483 
1484 	strscpy(t->name, "Radio", sizeof(t->name));
1485 
1486 	saa_call_all(dev, tuner, g_tuner, t);
1487 	t->audmode &= V4L2_TUNER_MODE_MONO | V4L2_TUNER_MODE_STEREO;
1488 	if (dev->input->amux == TV) {
1489 		t->signal = 0xf800 - ((saa_readb(0x581) & 0x1f) << 11);
1490 		t->rxsubchans = (saa_readb(0x529) & 0x08) ?
1491 				V4L2_TUNER_SUB_STEREO : V4L2_TUNER_SUB_MONO;
1492 	}
1493 	return 0;
1494 }
1495 static int radio_s_tuner(struct file *file, void *priv,
1496 					const struct v4l2_tuner *t)
1497 {
1498 	struct saa7134_dev *dev = video_drvdata(file);
1499 
1500 	if (0 != t->index)
1501 		return -EINVAL;
1502 
1503 	saa_call_all(dev, tuner, s_tuner, t);
1504 	return 0;
1505 }
1506 
1507 static const struct v4l2_file_operations video_fops =
1508 {
1509 	.owner	  = THIS_MODULE,
1510 	.open	  = video_open,
1511 	.release  = video_release,
1512 	.read	  = vb2_fop_read,
1513 	.poll     = vb2_fop_poll,
1514 	.mmap	  = vb2_fop_mmap,
1515 	.unlocked_ioctl	  = video_ioctl2,
1516 };
1517 
1518 static const struct v4l2_ioctl_ops video_ioctl_ops = {
1519 	.vidioc_querycap		= saa7134_querycap,
1520 	.vidioc_enum_fmt_vid_cap	= saa7134_enum_fmt_vid_cap,
1521 	.vidioc_g_fmt_vid_cap		= saa7134_g_fmt_vid_cap,
1522 	.vidioc_try_fmt_vid_cap		= saa7134_try_fmt_vid_cap,
1523 	.vidioc_s_fmt_vid_cap		= saa7134_s_fmt_vid_cap,
1524 	.vidioc_g_fmt_vbi_cap		= saa7134_try_get_set_fmt_vbi_cap,
1525 	.vidioc_try_fmt_vbi_cap		= saa7134_try_get_set_fmt_vbi_cap,
1526 	.vidioc_s_fmt_vbi_cap		= saa7134_try_get_set_fmt_vbi_cap,
1527 	.vidioc_g_pixelaspect		= saa7134_g_pixelaspect,
1528 	.vidioc_reqbufs			= vb2_ioctl_reqbufs,
1529 	.vidioc_querybuf		= vb2_ioctl_querybuf,
1530 	.vidioc_qbuf			= vb2_ioctl_qbuf,
1531 	.vidioc_dqbuf			= vb2_ioctl_dqbuf,
1532 	.vidioc_expbuf			= vb2_ioctl_expbuf,
1533 	.vidioc_s_std			= saa7134_s_std,
1534 	.vidioc_g_std			= saa7134_g_std,
1535 	.vidioc_querystd		= saa7134_querystd,
1536 	.vidioc_enum_input		= saa7134_enum_input,
1537 	.vidioc_g_input			= saa7134_g_input,
1538 	.vidioc_s_input			= saa7134_s_input,
1539 	.vidioc_streamon		= vb2_ioctl_streamon,
1540 	.vidioc_streamoff		= vb2_ioctl_streamoff,
1541 	.vidioc_g_tuner			= saa7134_g_tuner,
1542 	.vidioc_s_tuner			= saa7134_s_tuner,
1543 	.vidioc_g_selection		= saa7134_g_selection,
1544 	.vidioc_s_selection		= saa7134_s_selection,
1545 	.vidioc_g_frequency		= saa7134_g_frequency,
1546 	.vidioc_s_frequency		= saa7134_s_frequency,
1547 #ifdef CONFIG_VIDEO_ADV_DEBUG
1548 	.vidioc_g_register              = vidioc_g_register,
1549 	.vidioc_s_register              = vidioc_s_register,
1550 #endif
1551 	.vidioc_log_status		= v4l2_ctrl_log_status,
1552 	.vidioc_subscribe_event		= v4l2_ctrl_subscribe_event,
1553 	.vidioc_unsubscribe_event	= v4l2_event_unsubscribe,
1554 };
1555 
1556 static const struct v4l2_file_operations radio_fops = {
1557 	.owner	  = THIS_MODULE,
1558 	.open	  = video_open,
1559 	.read     = radio_read,
1560 	.release  = video_release,
1561 	.unlocked_ioctl	= video_ioctl2,
1562 	.poll     = radio_poll,
1563 };
1564 
1565 static const struct v4l2_ioctl_ops radio_ioctl_ops = {
1566 	.vidioc_querycap	= saa7134_querycap,
1567 	.vidioc_g_tuner		= radio_g_tuner,
1568 	.vidioc_s_tuner		= radio_s_tuner,
1569 	.vidioc_g_frequency	= saa7134_g_frequency,
1570 	.vidioc_s_frequency	= saa7134_s_frequency,
1571 	.vidioc_subscribe_event	= v4l2_ctrl_subscribe_event,
1572 	.vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1573 };
1574 
1575 /* ----------------------------------------------------------- */
1576 /* exported stuff                                              */
1577 
1578 struct video_device saa7134_video_template = {
1579 	.name				= "saa7134-video",
1580 	.fops				= &video_fops,
1581 	.ioctl_ops			= &video_ioctl_ops,
1582 	.tvnorms			= SAA7134_NORMS,
1583 };
1584 
1585 struct video_device saa7134_radio_template = {
1586 	.name			= "saa7134-radio",
1587 	.fops			= &radio_fops,
1588 	.ioctl_ops		= &radio_ioctl_ops,
1589 };
1590 
1591 static const struct v4l2_ctrl_ops saa7134_ctrl_ops = {
1592 	.s_ctrl = saa7134_s_ctrl,
1593 };
1594 
1595 static const struct v4l2_ctrl_config saa7134_ctrl_invert = {
1596 	.ops = &saa7134_ctrl_ops,
1597 	.id = V4L2_CID_PRIVATE_INVERT,
1598 	.name = "Invert",
1599 	.type = V4L2_CTRL_TYPE_BOOLEAN,
1600 	.min = 0,
1601 	.max = 1,
1602 	.step = 1,
1603 };
1604 
1605 static const struct v4l2_ctrl_config saa7134_ctrl_y_odd = {
1606 	.ops = &saa7134_ctrl_ops,
1607 	.id = V4L2_CID_PRIVATE_Y_ODD,
1608 	.name = "Y Offset Odd Field",
1609 	.type = V4L2_CTRL_TYPE_INTEGER,
1610 	.min = 0,
1611 	.max = 128,
1612 	.step = 1,
1613 };
1614 
1615 static const struct v4l2_ctrl_config saa7134_ctrl_y_even = {
1616 	.ops = &saa7134_ctrl_ops,
1617 	.id = V4L2_CID_PRIVATE_Y_EVEN,
1618 	.name = "Y Offset Even Field",
1619 	.type = V4L2_CTRL_TYPE_INTEGER,
1620 	.min = 0,
1621 	.max = 128,
1622 	.step = 1,
1623 };
1624 
1625 static const struct v4l2_ctrl_config saa7134_ctrl_automute = {
1626 	.ops = &saa7134_ctrl_ops,
1627 	.id = V4L2_CID_PRIVATE_AUTOMUTE,
1628 	.name = "Automute",
1629 	.type = V4L2_CTRL_TYPE_BOOLEAN,
1630 	.min = 0,
1631 	.max = 1,
1632 	.step = 1,
1633 	.def = 1,
1634 };
1635 
1636 int saa7134_video_init1(struct saa7134_dev *dev)
1637 {
1638 	struct v4l2_ctrl_handler *hdl = &dev->ctrl_handler;
1639 	struct vb2_queue *q;
1640 	int ret;
1641 
1642 	/* sanitycheck insmod options */
1643 	if (gbuffers < 2 || gbuffers > VIDEO_MAX_FRAME)
1644 		gbuffers = 2;
1645 	if (gbufsize > gbufsize_max)
1646 		gbufsize = gbufsize_max;
1647 	gbufsize = (gbufsize + PAGE_SIZE - 1) & PAGE_MASK;
1648 
1649 	v4l2_ctrl_handler_init(hdl, 11);
1650 	v4l2_ctrl_new_std(hdl, &saa7134_ctrl_ops,
1651 			V4L2_CID_BRIGHTNESS, 0, 255, 1, 128);
1652 	v4l2_ctrl_new_std(hdl, &saa7134_ctrl_ops,
1653 			V4L2_CID_CONTRAST, 0, 127, 1, 68);
1654 	v4l2_ctrl_new_std(hdl, &saa7134_ctrl_ops,
1655 			V4L2_CID_SATURATION, 0, 127, 1, 64);
1656 	v4l2_ctrl_new_std(hdl, &saa7134_ctrl_ops,
1657 			V4L2_CID_HUE, -128, 127, 1, 0);
1658 	v4l2_ctrl_new_std(hdl, &saa7134_ctrl_ops,
1659 			V4L2_CID_HFLIP, 0, 1, 1, 0);
1660 	v4l2_ctrl_new_std(hdl, &saa7134_ctrl_ops,
1661 			V4L2_CID_AUDIO_MUTE, 0, 1, 1, 0);
1662 	v4l2_ctrl_new_std(hdl, &saa7134_ctrl_ops,
1663 			V4L2_CID_AUDIO_VOLUME, -15, 15, 1, 0);
1664 	v4l2_ctrl_new_custom(hdl, &saa7134_ctrl_invert, NULL);
1665 	v4l2_ctrl_new_custom(hdl, &saa7134_ctrl_y_odd, NULL);
1666 	v4l2_ctrl_new_custom(hdl, &saa7134_ctrl_y_even, NULL);
1667 	v4l2_ctrl_new_custom(hdl, &saa7134_ctrl_automute, NULL);
1668 	if (hdl->error)
1669 		return hdl->error;
1670 	if (card_has_radio(dev)) {
1671 		hdl = &dev->radio_ctrl_handler;
1672 		v4l2_ctrl_handler_init(hdl, 2);
1673 		v4l2_ctrl_add_handler(hdl, &dev->ctrl_handler,
1674 				v4l2_ctrl_radio_filter, false);
1675 		if (hdl->error)
1676 			return hdl->error;
1677 	}
1678 	dev->ctl_mute       = 1;
1679 
1680 	if (dev->tda9887_conf && saa7134_ctrl_automute.def)
1681 		dev->tda9887_conf |= TDA9887_AUTOMUTE;
1682 	dev->automute       = 0;
1683 
1684 	INIT_LIST_HEAD(&dev->video_q.queue);
1685 	timer_setup(&dev->video_q.timeout, saa7134_buffer_timeout, 0);
1686 	dev->video_q.dev              = dev;
1687 	dev->fmt = format_by_fourcc(V4L2_PIX_FMT_BGR24);
1688 	dev->width    = 720;
1689 	dev->height   = 576;
1690 	dev->field = V4L2_FIELD_INTERLACED;
1691 
1692 	if (saa7134_boards[dev->board].video_out)
1693 		saa7134_videoport_init(dev);
1694 
1695 	q = &dev->video_vbq;
1696 	q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1697 	/*
1698 	 * Do not add VB2_USERPTR unless explicitly requested: the saa7134 DMA
1699 	 * engine cannot handle transfers that do not start at the beginning
1700 	 * of a page. A user-provided pointer can start anywhere in a page, so
1701 	 * USERPTR support is a no-go unless the application knows about these
1702 	 * limitations and has special support for this.
1703 	 */
1704 	q->io_modes = VB2_MMAP | VB2_DMABUF | VB2_READ;
1705 	if (saa7134_userptr)
1706 		q->io_modes |= VB2_USERPTR;
1707 	q->drv_priv = &dev->video_q;
1708 	q->ops = &vb2_qops;
1709 	q->gfp_flags = GFP_DMA32;
1710 	q->mem_ops = &vb2_dma_sg_memops;
1711 	q->buf_struct_size = sizeof(struct saa7134_buf);
1712 	q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1713 	q->lock = &dev->lock;
1714 	q->dev = &dev->pci->dev;
1715 	ret = vb2_queue_init(q);
1716 	if (ret)
1717 		return ret;
1718 	saa7134_pgtable_alloc(dev->pci, &dev->video_q.pt);
1719 
1720 	q = &dev->vbi_vbq;
1721 	q->type = V4L2_BUF_TYPE_VBI_CAPTURE;
1722 	/* Don't add VB2_USERPTR, see comment above */
1723 	q->io_modes = VB2_MMAP | VB2_READ;
1724 	if (saa7134_userptr)
1725 		q->io_modes |= VB2_USERPTR;
1726 	q->drv_priv = &dev->vbi_q;
1727 	q->ops = &saa7134_vbi_qops;
1728 	q->gfp_flags = GFP_DMA32;
1729 	q->mem_ops = &vb2_dma_sg_memops;
1730 	q->buf_struct_size = sizeof(struct saa7134_buf);
1731 	q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1732 	q->lock = &dev->lock;
1733 	q->dev = &dev->pci->dev;
1734 	ret = vb2_queue_init(q);
1735 	if (ret)
1736 		return ret;
1737 	saa7134_pgtable_alloc(dev->pci, &dev->vbi_q.pt);
1738 
1739 	return 0;
1740 }
1741 
1742 void saa7134_video_fini(struct saa7134_dev *dev)
1743 {
1744 	del_timer_sync(&dev->video_q.timeout);
1745 	/* free stuff */
1746 	saa7134_pgtable_free(dev->pci, &dev->video_q.pt);
1747 	saa7134_pgtable_free(dev->pci, &dev->vbi_q.pt);
1748 	v4l2_ctrl_handler_free(&dev->ctrl_handler);
1749 	if (card_has_radio(dev))
1750 		v4l2_ctrl_handler_free(&dev->radio_ctrl_handler);
1751 }
1752 
1753 int saa7134_videoport_init(struct saa7134_dev *dev)
1754 {
1755 	/* enable video output */
1756 	int vo = saa7134_boards[dev->board].video_out;
1757 	int video_reg;
1758 	unsigned int vid_port_opts = saa7134_boards[dev->board].vid_port_opts;
1759 
1760 	/* Configure videoport */
1761 	saa_writeb(SAA7134_VIDEO_PORT_CTRL0, video_out[vo][0]);
1762 	video_reg = video_out[vo][1];
1763 	if (vid_port_opts & SET_T_CODE_POLARITY_NON_INVERTED)
1764 		video_reg &= ~VP_T_CODE_P_INVERTED;
1765 	saa_writeb(SAA7134_VIDEO_PORT_CTRL1, video_reg);
1766 	saa_writeb(SAA7134_VIDEO_PORT_CTRL2, video_out[vo][2]);
1767 	saa_writeb(SAA7134_VIDEO_PORT_CTRL4, video_out[vo][4]);
1768 	video_reg = video_out[vo][5];
1769 	if (vid_port_opts & SET_CLOCK_NOT_DELAYED)
1770 		video_reg &= ~VP_CLK_CTRL2_DELAYED;
1771 	if (vid_port_opts & SET_CLOCK_INVERTED)
1772 		video_reg |= VP_CLK_CTRL1_INVERTED;
1773 	saa_writeb(SAA7134_VIDEO_PORT_CTRL5, video_reg);
1774 	video_reg = video_out[vo][6];
1775 	if (vid_port_opts & SET_VSYNC_OFF) {
1776 		video_reg &= ~VP_VS_TYPE_MASK;
1777 		video_reg |= VP_VS_TYPE_OFF;
1778 	}
1779 	saa_writeb(SAA7134_VIDEO_PORT_CTRL6, video_reg);
1780 	saa_writeb(SAA7134_VIDEO_PORT_CTRL7, video_out[vo][7]);
1781 	saa_writeb(SAA7134_VIDEO_PORT_CTRL8, video_out[vo][8]);
1782 
1783 	/* Start videoport */
1784 	saa_writeb(SAA7134_VIDEO_PORT_CTRL3, video_out[vo][3]);
1785 
1786 	return 0;
1787 }
1788 
1789 int saa7134_video_init2(struct saa7134_dev *dev)
1790 {
1791 	/* init video hw */
1792 	set_tvnorm(dev,&tvnorms[0]);
1793 	video_mux(dev,0);
1794 	v4l2_ctrl_handler_setup(&dev->ctrl_handler);
1795 	saa7134_tvaudio_setmute(dev);
1796 	saa7134_tvaudio_setvolume(dev,dev->ctl_volume);
1797 	return 0;
1798 }
1799 
1800 void saa7134_irq_video_signalchange(struct saa7134_dev *dev)
1801 {
1802 	static const char *st[] = {
1803 		"(no signal)", "NTSC", "PAL", "SECAM" };
1804 	u32 st1,st2;
1805 
1806 	st1 = saa_readb(SAA7134_STATUS_VIDEO1);
1807 	st2 = saa_readb(SAA7134_STATUS_VIDEO2);
1808 	video_dbg("DCSDT: pll: %s, sync: %s, norm: %s\n",
1809 		(st1 & 0x40) ? "not locked" : "locked",
1810 		(st2 & 0x40) ? "no"         : "yes",
1811 		st[st1 & 0x03]);
1812 	dev->nosignal = (st1 & 0x40) || (st2 & 0x40)  || !(st2 & 0x1);
1813 
1814 	if (dev->nosignal) {
1815 		/* no video signal -> mute audio */
1816 		if (dev->ctl_automute)
1817 			dev->automute = 1;
1818 		saa7134_tvaudio_setmute(dev);
1819 	} else {
1820 		/* wake up tvaudio audio carrier scan thread */
1821 		saa7134_tvaudio_do_scan(dev);
1822 	}
1823 
1824 	if ((st2 & 0x80) && !noninterlaced && !dev->nosignal)
1825 		saa_clearb(SAA7134_SYNC_CTRL, 0x20);
1826 	else
1827 		saa_setb(SAA7134_SYNC_CTRL, 0x20);
1828 
1829 	if (dev->mops && dev->mops->signal_change)
1830 		dev->mops->signal_change(dev);
1831 }
1832 
1833 
1834 void saa7134_irq_video_done(struct saa7134_dev *dev, unsigned long status)
1835 {
1836 	enum v4l2_field field;
1837 
1838 	spin_lock(&dev->slock);
1839 	if (dev->video_q.curr) {
1840 		field = dev->field;
1841 		if (V4L2_FIELD_HAS_BOTH(field)) {
1842 			/* make sure we have seen both fields */
1843 			if ((status & 0x10) == 0x00) {
1844 				dev->video_q.curr->top_seen = 1;
1845 				goto done;
1846 			}
1847 			if (!dev->video_q.curr->top_seen)
1848 				goto done;
1849 		} else if (field == V4L2_FIELD_TOP) {
1850 			if ((status & 0x10) != 0x10)
1851 				goto done;
1852 		} else if (field == V4L2_FIELD_BOTTOM) {
1853 			if ((status & 0x10) != 0x00)
1854 				goto done;
1855 		}
1856 		saa7134_buffer_finish(dev, &dev->video_q, VB2_BUF_STATE_DONE);
1857 	}
1858 	saa7134_buffer_next(dev, &dev->video_q);
1859 
1860  done:
1861 	spin_unlock(&dev->slock);
1862 }
1863