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 struct cliplist {
575 	__u16 position;
576 	__u8  enable;
577 	__u8  disable;
578 };
579 
580 static void set_cliplist(struct saa7134_dev *dev, int reg,
581 			struct cliplist *cl, int entries, char *name)
582 {
583 	__u8 winbits = 0;
584 	int i;
585 
586 	for (i = 0; i < entries; i++) {
587 		winbits |= cl[i].enable;
588 		winbits &= ~cl[i].disable;
589 		if (i < 15 && cl[i].position == cl[i+1].position)
590 			continue;
591 		saa_writeb(reg + 0, winbits);
592 		saa_writeb(reg + 2, cl[i].position & 0xff);
593 		saa_writeb(reg + 3, cl[i].position >> 8);
594 		video_dbg("clip: %s winbits=%02x pos=%d\n",
595 			name,winbits,cl[i].position);
596 		reg += 8;
597 	}
598 	for (; reg < 0x400; reg += 8) {
599 		saa_writeb(reg+ 0, 0);
600 		saa_writeb(reg + 1, 0);
601 		saa_writeb(reg + 2, 0);
602 		saa_writeb(reg + 3, 0);
603 	}
604 }
605 
606 static int clip_range(int val)
607 {
608 	if (val < 0)
609 		val = 0;
610 	return val;
611 }
612 
613 /* Sort into smallest position first order */
614 static int cliplist_cmp(const void *a, const void *b)
615 {
616 	const struct cliplist *cla = a;
617 	const struct cliplist *clb = b;
618 	if (cla->position < clb->position)
619 		return -1;
620 	if (cla->position > clb->position)
621 		return 1;
622 	return 0;
623 }
624 
625 static int setup_clipping(struct saa7134_dev *dev, struct v4l2_clip *clips,
626 			  int nclips, int interlace)
627 {
628 	struct cliplist col[16], row[16];
629 	int cols = 0, rows = 0, i;
630 	int div = interlace ? 2 : 1;
631 
632 	memset(col, 0, sizeof(col));
633 	memset(row, 0, sizeof(row));
634 	for (i = 0; i < nclips && i < 8; i++) {
635 		col[cols].position = clip_range(clips[i].c.left);
636 		col[cols].enable   = (1 << i);
637 		cols++;
638 		col[cols].position = clip_range(clips[i].c.left+clips[i].c.width);
639 		col[cols].disable  = (1 << i);
640 		cols++;
641 		row[rows].position = clip_range(clips[i].c.top / div);
642 		row[rows].enable   = (1 << i);
643 		rows++;
644 		row[rows].position = clip_range((clips[i].c.top + clips[i].c.height)
645 						/ div);
646 		row[rows].disable  = (1 << i);
647 		rows++;
648 	}
649 	sort(col, cols, sizeof col[0], cliplist_cmp, NULL);
650 	sort(row, rows, sizeof row[0], cliplist_cmp, NULL);
651 	set_cliplist(dev,0x380,col,cols,"cols");
652 	set_cliplist(dev,0x384,row,rows,"rows");
653 	return 0;
654 }
655 
656 static int verify_preview(struct saa7134_dev *dev, struct v4l2_window *win, bool try)
657 {
658 	enum v4l2_field field;
659 	int maxw, maxh;
660 
661 	if (!try && (dev->ovbuf.base == NULL || dev->ovfmt == NULL))
662 		return -EINVAL;
663 	if (win->w.width < 48)
664 		win->w.width = 48;
665 	if (win->w.height < 32)
666 		win->w.height = 32;
667 	if (win->clipcount > 8)
668 		win->clipcount = 8;
669 
670 	win->chromakey = 0;
671 	win->global_alpha = 0;
672 	field = win->field;
673 	maxw  = dev->crop_current.width;
674 	maxh  = dev->crop_current.height;
675 
676 	if (V4L2_FIELD_ANY == field) {
677 		field = (win->w.height > maxh/2)
678 			? V4L2_FIELD_INTERLACED
679 			: V4L2_FIELD_TOP;
680 	}
681 	switch (field) {
682 	case V4L2_FIELD_TOP:
683 	case V4L2_FIELD_BOTTOM:
684 		maxh = maxh / 2;
685 		break;
686 	default:
687 		field = V4L2_FIELD_INTERLACED;
688 		break;
689 	}
690 
691 	win->field = field;
692 	if (win->w.width > maxw)
693 		win->w.width = maxw;
694 	if (win->w.height > maxh)
695 		win->w.height = maxh;
696 	return 0;
697 }
698 
699 static int start_preview(struct saa7134_dev *dev)
700 {
701 	unsigned long base,control,bpl;
702 	int err;
703 
704 	err = verify_preview(dev, &dev->win, false);
705 	if (0 != err)
706 		return err;
707 
708 	dev->ovfield = dev->win.field;
709 	video_dbg("%s %dx%d+%d+%d 0x%08x field=%s\n", __func__,
710 		  dev->win.w.width, dev->win.w.height,
711 		  dev->win.w.left, dev->win.w.top,
712 		  dev->ovfmt->fourcc, v4l2_field_names[dev->ovfield]);
713 
714 	/* setup window + clipping */
715 	set_size(dev, TASK_B, dev->win.w.width, dev->win.w.height,
716 		 V4L2_FIELD_HAS_BOTH(dev->ovfield));
717 	setup_clipping(dev, dev->clips, dev->nclips,
718 		       V4L2_FIELD_HAS_BOTH(dev->ovfield));
719 	if (dev->ovfmt->yuv)
720 		saa_andorb(SAA7134_DATA_PATH(TASK_B), 0x3f, 0x03);
721 	else
722 		saa_andorb(SAA7134_DATA_PATH(TASK_B), 0x3f, 0x01);
723 	saa_writeb(SAA7134_OFMT_VIDEO_B, dev->ovfmt->pm | 0x20);
724 
725 	/* dma: setup channel 1 (= Video Task B) */
726 	base  = (unsigned long)dev->ovbuf.base;
727 	base += dev->ovbuf.fmt.bytesperline * dev->win.w.top;
728 	base += dev->ovfmt->depth/8         * dev->win.w.left;
729 	bpl   = dev->ovbuf.fmt.bytesperline;
730 	control = SAA7134_RS_CONTROL_BURST_16;
731 	if (dev->ovfmt->bswap)
732 		control |= SAA7134_RS_CONTROL_BSWAP;
733 	if (dev->ovfmt->wswap)
734 		control |= SAA7134_RS_CONTROL_WSWAP;
735 	if (V4L2_FIELD_HAS_BOTH(dev->ovfield)) {
736 		saa_writel(SAA7134_RS_BA1(1),base);
737 		saa_writel(SAA7134_RS_BA2(1),base+bpl);
738 		saa_writel(SAA7134_RS_PITCH(1),bpl*2);
739 		saa_writel(SAA7134_RS_CONTROL(1),control);
740 	} else {
741 		saa_writel(SAA7134_RS_BA1(1),base);
742 		saa_writel(SAA7134_RS_BA2(1),base);
743 		saa_writel(SAA7134_RS_PITCH(1),bpl);
744 		saa_writel(SAA7134_RS_CONTROL(1),control);
745 	}
746 
747 	/* start dma */
748 	dev->ovenable = 1;
749 	saa7134_set_dmabits(dev);
750 
751 	return 0;
752 }
753 
754 static int stop_preview(struct saa7134_dev *dev)
755 {
756 	dev->ovenable = 0;
757 	saa7134_set_dmabits(dev);
758 	return 0;
759 }
760 
761 /*
762  * Media Controller helper functions
763  */
764 
765 static int saa7134_enable_analog_tuner(struct saa7134_dev *dev)
766 {
767 #ifdef CONFIG_MEDIA_CONTROLLER
768 	struct media_device *mdev = dev->media_dev;
769 	struct media_entity *source;
770 	struct media_link *link, *found_link = NULL;
771 	int ret, active_links = 0;
772 
773 	if (!mdev || !dev->decoder)
774 		return 0;
775 
776 	/*
777 	 * This will find the tuner that is connected into the decoder.
778 	 * Technically, this is not 100% correct, as the device may be
779 	 * using an analog input instead of the tuner. However, as we can't
780 	 * do DVB streaming while the DMA engine is being used for V4L2,
781 	 * this should be enough for the actual needs.
782 	 */
783 	list_for_each_entry(link, &dev->decoder->links, list) {
784 		if (link->sink->entity == dev->decoder) {
785 			found_link = link;
786 			if (link->flags & MEDIA_LNK_FL_ENABLED)
787 				active_links++;
788 			break;
789 		}
790 	}
791 
792 	if (active_links == 1 || !found_link)
793 		return 0;
794 
795 	source = found_link->source->entity;
796 	list_for_each_entry(link, &source->links, list) {
797 		struct media_entity *sink;
798 		int flags = 0;
799 
800 		sink = link->sink->entity;
801 
802 		if (sink == dev->decoder)
803 			flags = MEDIA_LNK_FL_ENABLED;
804 
805 		ret = media_entity_setup_link(link, flags);
806 		if (ret) {
807 			pr_err("Couldn't change link %s->%s to %s. Error %d\n",
808 			       source->name, sink->name,
809 			       flags ? "enabled" : "disabled",
810 			       ret);
811 			return ret;
812 		}
813 	}
814 #endif
815 	return 0;
816 }
817 
818 /* ------------------------------------------------------------------ */
819 
820 static int buffer_activate(struct saa7134_dev *dev,
821 			   struct saa7134_buf *buf,
822 			   struct saa7134_buf *next)
823 {
824 	struct saa7134_dmaqueue *dmaq = buf->vb2.vb2_buf.vb2_queue->drv_priv;
825 	unsigned long base,control,bpl;
826 	unsigned long bpl_uv,lines_uv,base2,base3,tmp; /* planar */
827 
828 	video_dbg("buffer_activate buf=%p\n", buf);
829 	buf->top_seen = 0;
830 
831 	set_size(dev, TASK_A, dev->width, dev->height,
832 		 V4L2_FIELD_HAS_BOTH(dev->field));
833 	if (dev->fmt->yuv)
834 		saa_andorb(SAA7134_DATA_PATH(TASK_A), 0x3f, 0x03);
835 	else
836 		saa_andorb(SAA7134_DATA_PATH(TASK_A), 0x3f, 0x01);
837 	saa_writeb(SAA7134_OFMT_VIDEO_A, dev->fmt->pm);
838 
839 	/* DMA: setup channel 0 (= Video Task A0) */
840 	base  = saa7134_buffer_base(buf);
841 	if (dev->fmt->planar)
842 		bpl = dev->width;
843 	else
844 		bpl = (dev->width * dev->fmt->depth) / 8;
845 	control = SAA7134_RS_CONTROL_BURST_16 |
846 		SAA7134_RS_CONTROL_ME |
847 		(dmaq->pt.dma >> 12);
848 	if (dev->fmt->bswap)
849 		control |= SAA7134_RS_CONTROL_BSWAP;
850 	if (dev->fmt->wswap)
851 		control |= SAA7134_RS_CONTROL_WSWAP;
852 	if (V4L2_FIELD_HAS_BOTH(dev->field)) {
853 		/* interlaced */
854 		saa_writel(SAA7134_RS_BA1(0),base);
855 		saa_writel(SAA7134_RS_BA2(0),base+bpl);
856 		saa_writel(SAA7134_RS_PITCH(0),bpl*2);
857 	} else {
858 		/* non-interlaced */
859 		saa_writel(SAA7134_RS_BA1(0),base);
860 		saa_writel(SAA7134_RS_BA2(0),base);
861 		saa_writel(SAA7134_RS_PITCH(0),bpl);
862 	}
863 	saa_writel(SAA7134_RS_CONTROL(0),control);
864 
865 	if (dev->fmt->planar) {
866 		/* DMA: setup channel 4+5 (= planar task A) */
867 		bpl_uv   = bpl >> dev->fmt->hshift;
868 		lines_uv = dev->height >> dev->fmt->vshift;
869 		base2    = base + bpl * dev->height;
870 		base3    = base2 + bpl_uv * lines_uv;
871 		if (dev->fmt->uvswap) {
872 			tmp = base2;
873 			base2 = base3;
874 			base3 = tmp;
875 		}
876 		video_dbg("uv: bpl=%ld lines=%ld base2/3=%ld/%ld\n",
877 			bpl_uv,lines_uv,base2,base3);
878 		if (V4L2_FIELD_HAS_BOTH(dev->field)) {
879 			/* interlaced */
880 			saa_writel(SAA7134_RS_BA1(4),base2);
881 			saa_writel(SAA7134_RS_BA2(4),base2+bpl_uv);
882 			saa_writel(SAA7134_RS_PITCH(4),bpl_uv*2);
883 			saa_writel(SAA7134_RS_BA1(5),base3);
884 			saa_writel(SAA7134_RS_BA2(5),base3+bpl_uv);
885 			saa_writel(SAA7134_RS_PITCH(5),bpl_uv*2);
886 		} else {
887 			/* non-interlaced */
888 			saa_writel(SAA7134_RS_BA1(4),base2);
889 			saa_writel(SAA7134_RS_BA2(4),base2);
890 			saa_writel(SAA7134_RS_PITCH(4),bpl_uv);
891 			saa_writel(SAA7134_RS_BA1(5),base3);
892 			saa_writel(SAA7134_RS_BA2(5),base3);
893 			saa_writel(SAA7134_RS_PITCH(5),bpl_uv);
894 		}
895 		saa_writel(SAA7134_RS_CONTROL(4),control);
896 		saa_writel(SAA7134_RS_CONTROL(5),control);
897 	}
898 
899 	/* start DMA */
900 	saa7134_set_dmabits(dev);
901 	mod_timer(&dmaq->timeout, jiffies + BUFFER_TIMEOUT);
902 	return 0;
903 }
904 
905 static int buffer_init(struct vb2_buffer *vb2)
906 {
907 	struct saa7134_dmaqueue *dmaq = vb2->vb2_queue->drv_priv;
908 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb2);
909 	struct saa7134_buf *buf = container_of(vbuf, struct saa7134_buf, vb2);
910 
911 	dmaq->curr = NULL;
912 	buf->activate = buffer_activate;
913 	return 0;
914 }
915 
916 static int buffer_prepare(struct vb2_buffer *vb2)
917 {
918 	struct saa7134_dmaqueue *dmaq = vb2->vb2_queue->drv_priv;
919 	struct saa7134_dev *dev = dmaq->dev;
920 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb2);
921 	struct saa7134_buf *buf = container_of(vbuf, struct saa7134_buf, vb2);
922 	struct sg_table *dma = vb2_dma_sg_plane_desc(vb2, 0);
923 	unsigned int size;
924 
925 	if (dma->sgl->offset) {
926 		pr_err("The buffer is not page-aligned\n");
927 		return -EINVAL;
928 	}
929 	size = (dev->width * dev->height * dev->fmt->depth) >> 3;
930 	if (vb2_plane_size(vb2, 0) < size)
931 		return -EINVAL;
932 
933 	vb2_set_plane_payload(vb2, 0, size);
934 	vbuf->field = dev->field;
935 
936 	return saa7134_pgtable_build(dev->pci, &dmaq->pt, dma->sgl, dma->nents,
937 				    saa7134_buffer_startpage(buf));
938 }
939 
940 static int queue_setup(struct vb2_queue *q,
941 			   unsigned int *nbuffers, unsigned int *nplanes,
942 			   unsigned int sizes[], struct device *alloc_devs[])
943 {
944 	struct saa7134_dmaqueue *dmaq = q->drv_priv;
945 	struct saa7134_dev *dev = dmaq->dev;
946 	int size = dev->fmt->depth * dev->width * dev->height >> 3;
947 
948 	if (dev->width    < 48 ||
949 	    dev->height   < 32 ||
950 	    dev->width/4  > dev->crop_current.width  ||
951 	    dev->height/4 > dev->crop_current.height ||
952 	    dev->width    > dev->crop_bounds.width  ||
953 	    dev->height   > dev->crop_bounds.height)
954 		return -EINVAL;
955 
956 	*nbuffers = saa7134_buffer_count(size, *nbuffers);
957 	*nplanes = 1;
958 	sizes[0] = size;
959 
960 	saa7134_enable_analog_tuner(dev);
961 
962 	return 0;
963 }
964 
965 /*
966  * move buffer to hardware queue
967  */
968 void saa7134_vb2_buffer_queue(struct vb2_buffer *vb)
969 {
970 	struct saa7134_dmaqueue *dmaq = vb->vb2_queue->drv_priv;
971 	struct saa7134_dev *dev = dmaq->dev;
972 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
973 	struct saa7134_buf *buf = container_of(vbuf, struct saa7134_buf, vb2);
974 
975 	saa7134_buffer_queue(dev, dmaq, buf);
976 }
977 EXPORT_SYMBOL_GPL(saa7134_vb2_buffer_queue);
978 
979 int saa7134_vb2_start_streaming(struct vb2_queue *vq, unsigned int count)
980 {
981 	struct saa7134_dmaqueue *dmaq = vq->drv_priv;
982 	struct saa7134_dev *dev = dmaq->dev;
983 
984 	/*
985 	 * Planar video capture and TS share the same DMA channel,
986 	 * so only one can be active at a time.
987 	 */
988 	if (card_is_empress(dev) && vb2_is_busy(&dev->empress_vbq) &&
989 	    dmaq == &dev->video_q && dev->fmt->planar) {
990 		struct saa7134_buf *buf, *tmp;
991 
992 		list_for_each_entry_safe(buf, tmp, &dmaq->queue, entry) {
993 			list_del(&buf->entry);
994 			vb2_buffer_done(&buf->vb2.vb2_buf,
995 					VB2_BUF_STATE_QUEUED);
996 		}
997 		if (dmaq->curr) {
998 			vb2_buffer_done(&dmaq->curr->vb2.vb2_buf,
999 					VB2_BUF_STATE_QUEUED);
1000 			dmaq->curr = NULL;
1001 		}
1002 		return -EBUSY;
1003 	}
1004 
1005 	/* The SAA7134 has a 1K FIFO; the datasheet suggests that when
1006 	 * configured conservatively, there's 22 usec of buffering for video.
1007 	 * We therefore request a DMA latency of 20 usec, giving us 2 usec of
1008 	 * margin in case the FIFO is configured differently to the datasheet.
1009 	 * Unfortunately, I lack register-level documentation to check the
1010 	 * Linux FIFO setup and confirm the perfect value.
1011 	 */
1012 	if ((dmaq == &dev->video_q && !vb2_is_streaming(&dev->vbi_vbq)) ||
1013 	    (dmaq == &dev->vbi_q && !vb2_is_streaming(&dev->video_vbq)))
1014 		cpu_latency_qos_add_request(&dev->qos_request, 20);
1015 	dmaq->seq_nr = 0;
1016 
1017 	return 0;
1018 }
1019 
1020 void saa7134_vb2_stop_streaming(struct vb2_queue *vq)
1021 {
1022 	struct saa7134_dmaqueue *dmaq = vq->drv_priv;
1023 	struct saa7134_dev *dev = dmaq->dev;
1024 
1025 	saa7134_stop_streaming(dev, dmaq);
1026 
1027 	if ((dmaq == &dev->video_q && !vb2_is_streaming(&dev->vbi_vbq)) ||
1028 	    (dmaq == &dev->vbi_q && !vb2_is_streaming(&dev->video_vbq)))
1029 		cpu_latency_qos_remove_request(&dev->qos_request);
1030 }
1031 
1032 static const struct vb2_ops vb2_qops = {
1033 	.queue_setup	= queue_setup,
1034 	.buf_init	= buffer_init,
1035 	.buf_prepare	= buffer_prepare,
1036 	.buf_queue	= saa7134_vb2_buffer_queue,
1037 	.wait_prepare	= vb2_ops_wait_prepare,
1038 	.wait_finish	= vb2_ops_wait_finish,
1039 	.start_streaming = saa7134_vb2_start_streaming,
1040 	.stop_streaming = saa7134_vb2_stop_streaming,
1041 };
1042 
1043 /* ------------------------------------------------------------------ */
1044 
1045 static int saa7134_s_ctrl(struct v4l2_ctrl *ctrl)
1046 {
1047 	struct saa7134_dev *dev = container_of(ctrl->handler, struct saa7134_dev, ctrl_handler);
1048 	unsigned long flags;
1049 	int restart_overlay = 0;
1050 
1051 	switch (ctrl->id) {
1052 	case V4L2_CID_BRIGHTNESS:
1053 		dev->ctl_bright = ctrl->val;
1054 		saa_writeb(SAA7134_DEC_LUMA_BRIGHT, ctrl->val);
1055 		break;
1056 	case V4L2_CID_HUE:
1057 		dev->ctl_hue = ctrl->val;
1058 		saa_writeb(SAA7134_DEC_CHROMA_HUE, ctrl->val);
1059 		break;
1060 	case V4L2_CID_CONTRAST:
1061 		dev->ctl_contrast = ctrl->val;
1062 		saa_writeb(SAA7134_DEC_LUMA_CONTRAST,
1063 			   dev->ctl_invert ? -dev->ctl_contrast : dev->ctl_contrast);
1064 		break;
1065 	case V4L2_CID_SATURATION:
1066 		dev->ctl_saturation = ctrl->val;
1067 		saa_writeb(SAA7134_DEC_CHROMA_SATURATION,
1068 			   dev->ctl_invert ? -dev->ctl_saturation : dev->ctl_saturation);
1069 		break;
1070 	case V4L2_CID_AUDIO_MUTE:
1071 		dev->ctl_mute = ctrl->val;
1072 		saa7134_tvaudio_setmute(dev);
1073 		break;
1074 	case V4L2_CID_AUDIO_VOLUME:
1075 		dev->ctl_volume = ctrl->val;
1076 		saa7134_tvaudio_setvolume(dev,dev->ctl_volume);
1077 		break;
1078 	case V4L2_CID_PRIVATE_INVERT:
1079 		dev->ctl_invert = ctrl->val;
1080 		saa_writeb(SAA7134_DEC_LUMA_CONTRAST,
1081 			   dev->ctl_invert ? -dev->ctl_contrast : dev->ctl_contrast);
1082 		saa_writeb(SAA7134_DEC_CHROMA_SATURATION,
1083 			   dev->ctl_invert ? -dev->ctl_saturation : dev->ctl_saturation);
1084 		break;
1085 	case V4L2_CID_HFLIP:
1086 		dev->ctl_mirror = ctrl->val;
1087 		restart_overlay = 1;
1088 		break;
1089 	case V4L2_CID_PRIVATE_Y_EVEN:
1090 		dev->ctl_y_even = ctrl->val;
1091 		restart_overlay = 1;
1092 		break;
1093 	case V4L2_CID_PRIVATE_Y_ODD:
1094 		dev->ctl_y_odd = ctrl->val;
1095 		restart_overlay = 1;
1096 		break;
1097 	case V4L2_CID_PRIVATE_AUTOMUTE:
1098 	{
1099 		struct v4l2_priv_tun_config tda9887_cfg;
1100 
1101 		tda9887_cfg.tuner = TUNER_TDA9887;
1102 		tda9887_cfg.priv = &dev->tda9887_conf;
1103 
1104 		dev->ctl_automute = ctrl->val;
1105 		if (dev->tda9887_conf) {
1106 			if (dev->ctl_automute)
1107 				dev->tda9887_conf |= TDA9887_AUTOMUTE;
1108 			else
1109 				dev->tda9887_conf &= ~TDA9887_AUTOMUTE;
1110 
1111 			saa_call_all(dev, tuner, s_config, &tda9887_cfg);
1112 		}
1113 		break;
1114 	}
1115 	default:
1116 		return -EINVAL;
1117 	}
1118 	if (restart_overlay && dev->overlay_owner) {
1119 		spin_lock_irqsave(&dev->slock, flags);
1120 		stop_preview(dev);
1121 		start_preview(dev);
1122 		spin_unlock_irqrestore(&dev->slock, flags);
1123 	}
1124 	return 0;
1125 }
1126 
1127 /* ------------------------------------------------------------------ */
1128 
1129 static int video_open(struct file *file)
1130 {
1131 	struct video_device *vdev = video_devdata(file);
1132 	struct saa7134_dev *dev = video_drvdata(file);
1133 	int ret = v4l2_fh_open(file);
1134 
1135 	if (ret < 0)
1136 		return ret;
1137 
1138 	mutex_lock(&dev->lock);
1139 	if (vdev->vfl_type == VFL_TYPE_RADIO) {
1140 		/* switch to radio mode */
1141 		saa7134_tvaudio_setinput(dev, &card(dev).radio);
1142 		saa_call_all(dev, tuner, s_radio);
1143 	} else {
1144 		/* switch to video/vbi mode */
1145 		video_mux(dev, dev->ctl_input);
1146 	}
1147 	mutex_unlock(&dev->lock);
1148 
1149 	return 0;
1150 }
1151 
1152 static int video_release(struct file *file)
1153 {
1154 	struct video_device *vdev = video_devdata(file);
1155 	struct saa7134_dev *dev = video_drvdata(file);
1156 	struct v4l2_fh *fh = file->private_data;
1157 	struct saa6588_command cmd;
1158 	unsigned long flags;
1159 
1160 	mutex_lock(&dev->lock);
1161 	saa7134_tvaudio_close(dev);
1162 
1163 	/* turn off overlay */
1164 	if (fh == dev->overlay_owner) {
1165 		spin_lock_irqsave(&dev->slock,flags);
1166 		stop_preview(dev);
1167 		spin_unlock_irqrestore(&dev->slock,flags);
1168 		dev->overlay_owner = NULL;
1169 	}
1170 
1171 	if (vdev->vfl_type == VFL_TYPE_RADIO)
1172 		v4l2_fh_release(file);
1173 	else
1174 		_vb2_fop_release(file, NULL);
1175 
1176 	/* ts-capture will not work in planar mode, so turn it off Hac: 04.05*/
1177 	saa_andorb(SAA7134_OFMT_VIDEO_A, 0x1f, 0);
1178 	saa_andorb(SAA7134_OFMT_VIDEO_B, 0x1f, 0);
1179 	saa_andorb(SAA7134_OFMT_DATA_A, 0x1f, 0);
1180 	saa_andorb(SAA7134_OFMT_DATA_B, 0x1f, 0);
1181 
1182 	saa_call_all(dev, tuner, standby);
1183 	if (vdev->vfl_type == VFL_TYPE_RADIO)
1184 		saa_call_all(dev, core, ioctl, SAA6588_CMD_CLOSE, &cmd);
1185 	mutex_unlock(&dev->lock);
1186 
1187 	return 0;
1188 }
1189 
1190 static ssize_t radio_read(struct file *file, char __user *data,
1191 			 size_t count, loff_t *ppos)
1192 {
1193 	struct saa7134_dev *dev = video_drvdata(file);
1194 	struct saa6588_command cmd;
1195 
1196 	cmd.block_count = count/3;
1197 	cmd.nonblocking = file->f_flags & O_NONBLOCK;
1198 	cmd.buffer = data;
1199 	cmd.instance = file;
1200 	cmd.result = -ENODEV;
1201 
1202 	mutex_lock(&dev->lock);
1203 	saa_call_all(dev, core, ioctl, SAA6588_CMD_READ, &cmd);
1204 	mutex_unlock(&dev->lock);
1205 
1206 	return cmd.result;
1207 }
1208 
1209 static __poll_t radio_poll(struct file *file, poll_table *wait)
1210 {
1211 	struct saa7134_dev *dev = video_drvdata(file);
1212 	struct saa6588_command cmd;
1213 	__poll_t rc = v4l2_ctrl_poll(file, wait);
1214 
1215 	cmd.instance = file;
1216 	cmd.event_list = wait;
1217 	cmd.poll_mask = 0;
1218 	mutex_lock(&dev->lock);
1219 	saa_call_all(dev, core, ioctl, SAA6588_CMD_POLL, &cmd);
1220 	mutex_unlock(&dev->lock);
1221 
1222 	return rc | cmd.poll_mask;
1223 }
1224 
1225 /* ------------------------------------------------------------------ */
1226 
1227 static int saa7134_try_get_set_fmt_vbi_cap(struct file *file, void *priv,
1228 						struct v4l2_format *f)
1229 {
1230 	struct saa7134_dev *dev = video_drvdata(file);
1231 	struct saa7134_tvnorm *norm = dev->tvnorm;
1232 
1233 	memset(&f->fmt.vbi.reserved, 0, sizeof(f->fmt.vbi.reserved));
1234 	f->fmt.vbi.sampling_rate = 6750000 * 4;
1235 	f->fmt.vbi.samples_per_line = 2048 /* VBI_LINE_LENGTH */;
1236 	f->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
1237 	f->fmt.vbi.offset = 64 * 4;
1238 	f->fmt.vbi.start[0] = norm->vbi_v_start_0;
1239 	f->fmt.vbi.count[0] = norm->vbi_v_stop_0 - norm->vbi_v_start_0 +1;
1240 	f->fmt.vbi.start[1] = norm->vbi_v_start_1;
1241 	f->fmt.vbi.count[1] = f->fmt.vbi.count[0];
1242 	f->fmt.vbi.flags = 0; /* VBI_UNSYNC VBI_INTERLACED */
1243 
1244 	return 0;
1245 }
1246 
1247 static int saa7134_g_fmt_vid_cap(struct file *file, void *priv,
1248 				struct v4l2_format *f)
1249 {
1250 	struct saa7134_dev *dev = video_drvdata(file);
1251 
1252 	f->fmt.pix.width        = dev->width;
1253 	f->fmt.pix.height       = dev->height;
1254 	f->fmt.pix.field        = dev->field;
1255 	f->fmt.pix.pixelformat  = dev->fmt->fourcc;
1256 	if (dev->fmt->planar)
1257 		f->fmt.pix.bytesperline = f->fmt.pix.width;
1258 	else
1259 		f->fmt.pix.bytesperline =
1260 			(f->fmt.pix.width * dev->fmt->depth) / 8;
1261 	f->fmt.pix.sizeimage =
1262 		(f->fmt.pix.height * f->fmt.pix.width * dev->fmt->depth) / 8;
1263 	f->fmt.pix.colorspace   = V4L2_COLORSPACE_SMPTE170M;
1264 	return 0;
1265 }
1266 
1267 static int saa7134_g_fmt_vid_overlay(struct file *file, void *priv,
1268 				struct v4l2_format *f)
1269 {
1270 	struct saa7134_dev *dev = video_drvdata(file);
1271 	u32 clipcount = f->fmt.win.clipcount;
1272 	int i;
1273 
1274 	if (saa7134_no_overlay > 0) {
1275 		pr_err("V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
1276 		return -EINVAL;
1277 	}
1278 	f->fmt.win = dev->win;
1279 	if (!f->fmt.win.clips)
1280 		clipcount = 0;
1281 	if (dev->nclips < clipcount)
1282 		clipcount = dev->nclips;
1283 	f->fmt.win.clipcount = clipcount;
1284 
1285 	for (i = 0; i < clipcount; i++) {
1286 		memcpy(&f->fmt.win.clips[i].c, &dev->clips[i].c,
1287 		       sizeof(struct v4l2_rect));
1288 	}
1289 
1290 	return 0;
1291 }
1292 
1293 static int saa7134_try_fmt_vid_cap(struct file *file, void *priv,
1294 						struct v4l2_format *f)
1295 {
1296 	struct saa7134_dev *dev = video_drvdata(file);
1297 	struct saa7134_format *fmt;
1298 	enum v4l2_field field;
1299 	unsigned int maxw, maxh;
1300 
1301 	fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1302 	if (NULL == fmt)
1303 		return -EINVAL;
1304 
1305 	field = f->fmt.pix.field;
1306 	maxw  = min(dev->crop_current.width*4,  dev->crop_bounds.width);
1307 	maxh  = min(dev->crop_current.height*4, dev->crop_bounds.height);
1308 
1309 	if (V4L2_FIELD_ANY == field) {
1310 		field = (f->fmt.pix.height > maxh/2)
1311 			? V4L2_FIELD_INTERLACED
1312 			: V4L2_FIELD_BOTTOM;
1313 	}
1314 	switch (field) {
1315 	case V4L2_FIELD_TOP:
1316 	case V4L2_FIELD_BOTTOM:
1317 		maxh = maxh / 2;
1318 		break;
1319 	default:
1320 		field = V4L2_FIELD_INTERLACED;
1321 		break;
1322 	}
1323 
1324 	f->fmt.pix.field = field;
1325 	if (f->fmt.pix.width  < 48)
1326 		f->fmt.pix.width  = 48;
1327 	if (f->fmt.pix.height < 32)
1328 		f->fmt.pix.height = 32;
1329 	if (f->fmt.pix.width > maxw)
1330 		f->fmt.pix.width = maxw;
1331 	if (f->fmt.pix.height > maxh)
1332 		f->fmt.pix.height = maxh;
1333 	f->fmt.pix.width &= ~0x03;
1334 	if (fmt->planar)
1335 		f->fmt.pix.bytesperline = f->fmt.pix.width;
1336 	else
1337 		f->fmt.pix.bytesperline =
1338 			(f->fmt.pix.width * fmt->depth) / 8;
1339 	f->fmt.pix.sizeimage =
1340 		(f->fmt.pix.height * f->fmt.pix.width * fmt->depth) / 8;
1341 	f->fmt.pix.colorspace   = V4L2_COLORSPACE_SMPTE170M;
1342 
1343 	return 0;
1344 }
1345 
1346 static int saa7134_try_fmt_vid_overlay(struct file *file, void *priv,
1347 						struct v4l2_format *f)
1348 {
1349 	struct saa7134_dev *dev = video_drvdata(file);
1350 
1351 	if (saa7134_no_overlay > 0) {
1352 		pr_err("V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
1353 		return -EINVAL;
1354 	}
1355 
1356 	if (f->fmt.win.clips == NULL)
1357 		f->fmt.win.clipcount = 0;
1358 	return verify_preview(dev, &f->fmt.win, true);
1359 }
1360 
1361 static int saa7134_s_fmt_vid_cap(struct file *file, void *priv,
1362 					struct v4l2_format *f)
1363 {
1364 	struct saa7134_dev *dev = video_drvdata(file);
1365 	int err;
1366 
1367 	err = saa7134_try_fmt_vid_cap(file, priv, f);
1368 	if (0 != err)
1369 		return err;
1370 
1371 	dev->fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1372 	dev->width = f->fmt.pix.width;
1373 	dev->height = f->fmt.pix.height;
1374 	dev->field = f->fmt.pix.field;
1375 	return 0;
1376 }
1377 
1378 static int saa7134_s_fmt_vid_overlay(struct file *file, void *priv,
1379 					struct v4l2_format *f)
1380 {
1381 	struct saa7134_dev *dev = video_drvdata(file);
1382 	int err;
1383 	unsigned long flags;
1384 
1385 	if (saa7134_no_overlay > 0) {
1386 		pr_err("V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
1387 		return -EINVAL;
1388 	}
1389 	if (f->fmt.win.clips == NULL)
1390 		f->fmt.win.clipcount = 0;
1391 	err = verify_preview(dev, &f->fmt.win, true);
1392 	if (0 != err)
1393 		return err;
1394 
1395 	dev->win    = f->fmt.win;
1396 	dev->nclips = f->fmt.win.clipcount;
1397 
1398 	memcpy(dev->clips, f->fmt.win.clips,
1399 	       sizeof(struct v4l2_clip) * dev->nclips);
1400 
1401 	if (priv == dev->overlay_owner) {
1402 		spin_lock_irqsave(&dev->slock, flags);
1403 		stop_preview(dev);
1404 		start_preview(dev);
1405 		spin_unlock_irqrestore(&dev->slock, flags);
1406 	}
1407 
1408 	return 0;
1409 }
1410 
1411 int saa7134_enum_input(struct file *file, void *priv, struct v4l2_input *i)
1412 {
1413 	struct saa7134_dev *dev = video_drvdata(file);
1414 	unsigned int n;
1415 
1416 	n = i->index;
1417 	if (n >= SAA7134_INPUT_MAX)
1418 		return -EINVAL;
1419 	if (card_in(dev, i->index).type == SAA7134_NO_INPUT)
1420 		return -EINVAL;
1421 	i->index = n;
1422 	strscpy(i->name, saa7134_input_name[card_in(dev, n).type],
1423 		sizeof(i->name));
1424 	switch (card_in(dev, n).type) {
1425 	case SAA7134_INPUT_TV:
1426 	case SAA7134_INPUT_TV_MONO:
1427 		i->type = V4L2_INPUT_TYPE_TUNER;
1428 		break;
1429 	default:
1430 		i->type  = V4L2_INPUT_TYPE_CAMERA;
1431 		break;
1432 	}
1433 	if (n == dev->ctl_input) {
1434 		int v1 = saa_readb(SAA7134_STATUS_VIDEO1);
1435 		int v2 = saa_readb(SAA7134_STATUS_VIDEO2);
1436 
1437 		if (0 != (v1 & 0x40))
1438 			i->status |= V4L2_IN_ST_NO_H_LOCK;
1439 		if (0 != (v2 & 0x40))
1440 			i->status |= V4L2_IN_ST_NO_SIGNAL;
1441 		if (0 != (v2 & 0x0e))
1442 			i->status |= V4L2_IN_ST_MACROVISION;
1443 	}
1444 	i->std = SAA7134_NORMS;
1445 	return 0;
1446 }
1447 EXPORT_SYMBOL_GPL(saa7134_enum_input);
1448 
1449 int saa7134_g_input(struct file *file, void *priv, unsigned int *i)
1450 {
1451 	struct saa7134_dev *dev = video_drvdata(file);
1452 
1453 	*i = dev->ctl_input;
1454 	return 0;
1455 }
1456 EXPORT_SYMBOL_GPL(saa7134_g_input);
1457 
1458 int saa7134_s_input(struct file *file, void *priv, unsigned int i)
1459 {
1460 	struct saa7134_dev *dev = video_drvdata(file);
1461 
1462 	if (i >= SAA7134_INPUT_MAX)
1463 		return -EINVAL;
1464 	if (card_in(dev, i).type == SAA7134_NO_INPUT)
1465 		return -EINVAL;
1466 	video_mux(dev, i);
1467 	return 0;
1468 }
1469 EXPORT_SYMBOL_GPL(saa7134_s_input);
1470 
1471 int saa7134_querycap(struct file *file, void *priv,
1472 					struct v4l2_capability *cap)
1473 {
1474 	struct saa7134_dev *dev = video_drvdata(file);
1475 
1476 	strscpy(cap->driver, "saa7134", sizeof(cap->driver));
1477 	strscpy(cap->card, saa7134_boards[dev->board].name,
1478 		sizeof(cap->card));
1479 	sprintf(cap->bus_info, "PCI:%s", pci_name(dev->pci));
1480 	cap->capabilities = V4L2_CAP_READWRITE | V4L2_CAP_STREAMING |
1481 			    V4L2_CAP_RADIO | V4L2_CAP_VIDEO_CAPTURE |
1482 			    V4L2_CAP_VBI_CAPTURE | V4L2_CAP_DEVICE_CAPS;
1483 	if (dev->tuner_type != TUNER_ABSENT && dev->tuner_type != UNSET)
1484 		cap->capabilities |= V4L2_CAP_TUNER;
1485 	if (dev->has_rds)
1486 		cap->capabilities |= V4L2_CAP_RDS_CAPTURE;
1487 	if (saa7134_no_overlay <= 0)
1488 		cap->capabilities |= V4L2_CAP_VIDEO_OVERLAY;
1489 
1490 	return 0;
1491 }
1492 EXPORT_SYMBOL_GPL(saa7134_querycap);
1493 
1494 int saa7134_s_std(struct file *file, void *priv, v4l2_std_id id)
1495 {
1496 	struct saa7134_dev *dev = video_drvdata(file);
1497 	struct v4l2_fh *fh = priv;
1498 	unsigned long flags;
1499 	unsigned int i;
1500 	v4l2_std_id fixup;
1501 
1502 	if (is_empress(file) && dev->overlay_owner) {
1503 		/* Don't change the std from the mpeg device
1504 		   if overlay is active. */
1505 		return -EBUSY;
1506 	}
1507 
1508 	for (i = 0; i < TVNORMS; i++)
1509 		if (id == tvnorms[i].id)
1510 			break;
1511 
1512 	if (i == TVNORMS)
1513 		for (i = 0; i < TVNORMS; i++)
1514 			if (id & tvnorms[i].id)
1515 				break;
1516 	if (i == TVNORMS)
1517 		return -EINVAL;
1518 
1519 	if ((id & V4L2_STD_SECAM) && (secam[0] != '-')) {
1520 		if (secam[0] == 'L' || secam[0] == 'l') {
1521 			if (secam[1] == 'C' || secam[1] == 'c')
1522 				fixup = V4L2_STD_SECAM_LC;
1523 			else
1524 				fixup = V4L2_STD_SECAM_L;
1525 		} else {
1526 			if (secam[0] == 'D' || secam[0] == 'd')
1527 				fixup = V4L2_STD_SECAM_DK;
1528 			else
1529 				fixup = V4L2_STD_SECAM;
1530 		}
1531 		for (i = 0; i < TVNORMS; i++) {
1532 			if (fixup == tvnorms[i].id)
1533 				break;
1534 		}
1535 		if (i == TVNORMS)
1536 			return -EINVAL;
1537 	}
1538 
1539 	id = tvnorms[i].id;
1540 
1541 	if (!is_empress(file) && fh == dev->overlay_owner) {
1542 		spin_lock_irqsave(&dev->slock, flags);
1543 		stop_preview(dev);
1544 		spin_unlock_irqrestore(&dev->slock, flags);
1545 
1546 		set_tvnorm(dev, &tvnorms[i]);
1547 
1548 		spin_lock_irqsave(&dev->slock, flags);
1549 		start_preview(dev);
1550 		spin_unlock_irqrestore(&dev->slock, flags);
1551 	} else
1552 		set_tvnorm(dev, &tvnorms[i]);
1553 
1554 	saa7134_tvaudio_do_scan(dev);
1555 	return 0;
1556 }
1557 EXPORT_SYMBOL_GPL(saa7134_s_std);
1558 
1559 int saa7134_g_std(struct file *file, void *priv, v4l2_std_id *id)
1560 {
1561 	struct saa7134_dev *dev = video_drvdata(file);
1562 
1563 	*id = dev->tvnorm->id;
1564 	return 0;
1565 }
1566 EXPORT_SYMBOL_GPL(saa7134_g_std);
1567 
1568 static v4l2_std_id saa7134_read_std(struct saa7134_dev *dev)
1569 {
1570 	static v4l2_std_id stds[] = {
1571 		V4L2_STD_UNKNOWN,
1572 		V4L2_STD_NTSC,
1573 		V4L2_STD_PAL,
1574 		V4L2_STD_SECAM };
1575 
1576 	v4l2_std_id result = 0;
1577 
1578 	u8 st1 = saa_readb(SAA7134_STATUS_VIDEO1);
1579 	u8 st2 = saa_readb(SAA7134_STATUS_VIDEO2);
1580 
1581 	if (!(st2 & 0x1)) /* RDCAP == 0 */
1582 		result = V4L2_STD_UNKNOWN;
1583 	else
1584 		result = stds[st1 & 0x03];
1585 
1586 	return result;
1587 }
1588 
1589 int saa7134_querystd(struct file *file, void *priv, v4l2_std_id *std)
1590 {
1591 	struct saa7134_dev *dev = video_drvdata(file);
1592 	*std &= saa7134_read_std(dev);
1593 	return 0;
1594 }
1595 EXPORT_SYMBOL_GPL(saa7134_querystd);
1596 
1597 static int saa7134_g_pixelaspect(struct file *file, void *priv,
1598 				 int type, struct v4l2_fract *f)
1599 {
1600 	struct saa7134_dev *dev = video_drvdata(file);
1601 
1602 	if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1603 	    type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
1604 		return -EINVAL;
1605 
1606 	if (dev->tvnorm->id & V4L2_STD_525_60) {
1607 		f->numerator   = 11;
1608 		f->denominator = 10;
1609 	}
1610 	if (dev->tvnorm->id & V4L2_STD_625_50) {
1611 		f->numerator   = 54;
1612 		f->denominator = 59;
1613 	}
1614 	return 0;
1615 }
1616 
1617 static int saa7134_g_selection(struct file *file, void *f, struct v4l2_selection *sel)
1618 {
1619 	struct saa7134_dev *dev = video_drvdata(file);
1620 
1621 	if (sel->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1622 	    sel->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
1623 		return -EINVAL;
1624 
1625 	switch (sel->target) {
1626 	case V4L2_SEL_TGT_CROP:
1627 		sel->r = dev->crop_current;
1628 		break;
1629 	case V4L2_SEL_TGT_CROP_DEFAULT:
1630 		sel->r = dev->crop_defrect;
1631 		break;
1632 	case V4L2_SEL_TGT_CROP_BOUNDS:
1633 		sel->r  = dev->crop_bounds;
1634 		break;
1635 	default:
1636 		return -EINVAL;
1637 	}
1638 	return 0;
1639 }
1640 
1641 static int saa7134_s_selection(struct file *file, void *f, struct v4l2_selection *sel)
1642 {
1643 	struct saa7134_dev *dev = video_drvdata(file);
1644 	struct v4l2_rect *b = &dev->crop_bounds;
1645 	struct v4l2_rect *c = &dev->crop_current;
1646 
1647 	if (sel->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1648 	    sel->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
1649 		return -EINVAL;
1650 
1651 	if (sel->target != V4L2_SEL_TGT_CROP)
1652 		return -EINVAL;
1653 
1654 	if (dev->overlay_owner)
1655 		return -EBUSY;
1656 	if (vb2_is_streaming(&dev->video_vbq))
1657 		return -EBUSY;
1658 
1659 	*c = sel->r;
1660 	if (c->top < b->top)
1661 		c->top = b->top;
1662 	if (c->top > b->top + b->height)
1663 		c->top = b->top + b->height;
1664 	if (c->height > b->top - c->top + b->height)
1665 		c->height = b->top - c->top + b->height;
1666 
1667 	if (c->left < b->left)
1668 		c->left = b->left;
1669 	if (c->left > b->left + b->width)
1670 		c->left = b->left + b->width;
1671 	if (c->width > b->left - c->left + b->width)
1672 		c->width = b->left - c->left + b->width;
1673 	sel->r = *c;
1674 	return 0;
1675 }
1676 
1677 int saa7134_g_tuner(struct file *file, void *priv,
1678 					struct v4l2_tuner *t)
1679 {
1680 	struct saa7134_dev *dev = video_drvdata(file);
1681 	int n;
1682 
1683 	if (0 != t->index)
1684 		return -EINVAL;
1685 	memset(t, 0, sizeof(*t));
1686 	for (n = 0; n < SAA7134_INPUT_MAX; n++) {
1687 		if (card_in(dev, n).type == SAA7134_INPUT_TV ||
1688 		    card_in(dev, n).type == SAA7134_INPUT_TV_MONO)
1689 			break;
1690 	}
1691 	if (n == SAA7134_INPUT_MAX)
1692 		return -EINVAL;
1693 	if (card_in(dev, n).type != SAA7134_NO_INPUT) {
1694 		strscpy(t->name, "Television", sizeof(t->name));
1695 		t->type = V4L2_TUNER_ANALOG_TV;
1696 		saa_call_all(dev, tuner, g_tuner, t);
1697 		t->capability = V4L2_TUNER_CAP_NORM |
1698 			V4L2_TUNER_CAP_STEREO |
1699 			V4L2_TUNER_CAP_LANG1 |
1700 			V4L2_TUNER_CAP_LANG2;
1701 		t->rxsubchans = saa7134_tvaudio_getstereo(dev);
1702 		t->audmode = saa7134_tvaudio_rx2mode(t->rxsubchans);
1703 	}
1704 	if (0 != (saa_readb(SAA7134_STATUS_VIDEO1) & 0x03))
1705 		t->signal = 0xffff;
1706 	return 0;
1707 }
1708 EXPORT_SYMBOL_GPL(saa7134_g_tuner);
1709 
1710 int saa7134_s_tuner(struct file *file, void *priv,
1711 					const struct v4l2_tuner *t)
1712 {
1713 	struct saa7134_dev *dev = video_drvdata(file);
1714 	int rx, mode;
1715 
1716 	if (0 != t->index)
1717 		return -EINVAL;
1718 
1719 	mode = dev->thread.mode;
1720 	if (UNSET == mode) {
1721 		rx   = saa7134_tvaudio_getstereo(dev);
1722 		mode = saa7134_tvaudio_rx2mode(rx);
1723 	}
1724 	if (mode != t->audmode)
1725 		dev->thread.mode = t->audmode;
1726 
1727 	return 0;
1728 }
1729 EXPORT_SYMBOL_GPL(saa7134_s_tuner);
1730 
1731 int saa7134_g_frequency(struct file *file, void *priv,
1732 					struct v4l2_frequency *f)
1733 {
1734 	struct saa7134_dev *dev = video_drvdata(file);
1735 
1736 	if (0 != f->tuner)
1737 		return -EINVAL;
1738 
1739 	saa_call_all(dev, tuner, g_frequency, f);
1740 
1741 	return 0;
1742 }
1743 EXPORT_SYMBOL_GPL(saa7134_g_frequency);
1744 
1745 int saa7134_s_frequency(struct file *file, void *priv,
1746 					const struct v4l2_frequency *f)
1747 {
1748 	struct saa7134_dev *dev = video_drvdata(file);
1749 
1750 	if (0 != f->tuner)
1751 		return -EINVAL;
1752 
1753 	saa_call_all(dev, tuner, s_frequency, f);
1754 
1755 	saa7134_tvaudio_do_scan(dev);
1756 	return 0;
1757 }
1758 EXPORT_SYMBOL_GPL(saa7134_s_frequency);
1759 
1760 static int saa7134_enum_fmt_vid_cap(struct file *file, void  *priv,
1761 					struct v4l2_fmtdesc *f)
1762 {
1763 	if (f->index >= FORMATS)
1764 		return -EINVAL;
1765 
1766 	f->pixelformat = formats[f->index].fourcc;
1767 
1768 	return 0;
1769 }
1770 
1771 static int saa7134_enum_fmt_vid_overlay(struct file *file, void  *priv,
1772 					struct v4l2_fmtdesc *f)
1773 {
1774 	if (saa7134_no_overlay > 0) {
1775 		pr_err("V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
1776 		return -EINVAL;
1777 	}
1778 
1779 	if ((f->index >= FORMATS) || formats[f->index].planar)
1780 		return -EINVAL;
1781 
1782 	f->pixelformat = formats[f->index].fourcc;
1783 
1784 	return 0;
1785 }
1786 
1787 static int saa7134_g_fbuf(struct file *file, void *f,
1788 				struct v4l2_framebuffer *fb)
1789 {
1790 	struct saa7134_dev *dev = video_drvdata(file);
1791 
1792 	*fb = dev->ovbuf;
1793 	fb->capability = V4L2_FBUF_CAP_LIST_CLIPPING;
1794 
1795 	return 0;
1796 }
1797 
1798 static int saa7134_s_fbuf(struct file *file, void *f,
1799 					const struct v4l2_framebuffer *fb)
1800 {
1801 	struct saa7134_dev *dev = video_drvdata(file);
1802 	struct saa7134_format *fmt;
1803 
1804 	if (!capable(CAP_SYS_ADMIN) &&
1805 	   !capable(CAP_SYS_RAWIO))
1806 		return -EPERM;
1807 
1808 	/* check args */
1809 	fmt = format_by_fourcc(fb->fmt.pixelformat);
1810 	if (NULL == fmt)
1811 		return -EINVAL;
1812 
1813 	/* ok, accept it */
1814 	dev->ovbuf = *fb;
1815 	dev->ovfmt = fmt;
1816 	if (0 == dev->ovbuf.fmt.bytesperline)
1817 		dev->ovbuf.fmt.bytesperline =
1818 			dev->ovbuf.fmt.width*fmt->depth/8;
1819 	return 0;
1820 }
1821 
1822 static int saa7134_overlay(struct file *file, void *priv, unsigned int on)
1823 {
1824 	struct saa7134_dev *dev = video_drvdata(file);
1825 	unsigned long flags;
1826 
1827 	if (on) {
1828 		if (saa7134_no_overlay > 0) {
1829 			video_dbg("no_overlay\n");
1830 			return -EINVAL;
1831 		}
1832 
1833 		if (dev->overlay_owner && priv != dev->overlay_owner)
1834 			return -EBUSY;
1835 		dev->overlay_owner = priv;
1836 		spin_lock_irqsave(&dev->slock, flags);
1837 		start_preview(dev);
1838 		spin_unlock_irqrestore(&dev->slock, flags);
1839 	}
1840 	if (!on) {
1841 		if (priv != dev->overlay_owner)
1842 			return -EINVAL;
1843 		spin_lock_irqsave(&dev->slock, flags);
1844 		stop_preview(dev);
1845 		spin_unlock_irqrestore(&dev->slock, flags);
1846 		dev->overlay_owner = NULL;
1847 	}
1848 	return 0;
1849 }
1850 
1851 #ifdef CONFIG_VIDEO_ADV_DEBUG
1852 static int vidioc_g_register (struct file *file, void *priv,
1853 			      struct v4l2_dbg_register *reg)
1854 {
1855 	struct saa7134_dev *dev = video_drvdata(file);
1856 
1857 	reg->val = saa_readb(reg->reg & 0xffffff);
1858 	reg->size = 1;
1859 	return 0;
1860 }
1861 
1862 static int vidioc_s_register (struct file *file, void *priv,
1863 				const struct v4l2_dbg_register *reg)
1864 {
1865 	struct saa7134_dev *dev = video_drvdata(file);
1866 
1867 	saa_writeb(reg->reg & 0xffffff, reg->val);
1868 	return 0;
1869 }
1870 #endif
1871 
1872 static int radio_g_tuner(struct file *file, void *priv,
1873 					struct v4l2_tuner *t)
1874 {
1875 	struct saa7134_dev *dev = video_drvdata(file);
1876 
1877 	if (0 != t->index)
1878 		return -EINVAL;
1879 
1880 	strscpy(t->name, "Radio", sizeof(t->name));
1881 
1882 	saa_call_all(dev, tuner, g_tuner, t);
1883 	t->audmode &= V4L2_TUNER_MODE_MONO | V4L2_TUNER_MODE_STEREO;
1884 	if (dev->input->amux == TV) {
1885 		t->signal = 0xf800 - ((saa_readb(0x581) & 0x1f) << 11);
1886 		t->rxsubchans = (saa_readb(0x529) & 0x08) ?
1887 				V4L2_TUNER_SUB_STEREO : V4L2_TUNER_SUB_MONO;
1888 	}
1889 	return 0;
1890 }
1891 static int radio_s_tuner(struct file *file, void *priv,
1892 					const struct v4l2_tuner *t)
1893 {
1894 	struct saa7134_dev *dev = video_drvdata(file);
1895 
1896 	if (0 != t->index)
1897 		return -EINVAL;
1898 
1899 	saa_call_all(dev, tuner, s_tuner, t);
1900 	return 0;
1901 }
1902 
1903 static const struct v4l2_file_operations video_fops =
1904 {
1905 	.owner	  = THIS_MODULE,
1906 	.open	  = video_open,
1907 	.release  = video_release,
1908 	.read	  = vb2_fop_read,
1909 	.poll     = vb2_fop_poll,
1910 	.mmap	  = vb2_fop_mmap,
1911 	.unlocked_ioctl	  = video_ioctl2,
1912 };
1913 
1914 static const struct v4l2_ioctl_ops video_ioctl_ops = {
1915 	.vidioc_querycap		= saa7134_querycap,
1916 	.vidioc_enum_fmt_vid_cap	= saa7134_enum_fmt_vid_cap,
1917 	.vidioc_g_fmt_vid_cap		= saa7134_g_fmt_vid_cap,
1918 	.vidioc_try_fmt_vid_cap		= saa7134_try_fmt_vid_cap,
1919 	.vidioc_s_fmt_vid_cap		= saa7134_s_fmt_vid_cap,
1920 	.vidioc_enum_fmt_vid_overlay	= saa7134_enum_fmt_vid_overlay,
1921 	.vidioc_g_fmt_vid_overlay	= saa7134_g_fmt_vid_overlay,
1922 	.vidioc_try_fmt_vid_overlay	= saa7134_try_fmt_vid_overlay,
1923 	.vidioc_s_fmt_vid_overlay	= saa7134_s_fmt_vid_overlay,
1924 	.vidioc_g_fmt_vbi_cap		= saa7134_try_get_set_fmt_vbi_cap,
1925 	.vidioc_try_fmt_vbi_cap		= saa7134_try_get_set_fmt_vbi_cap,
1926 	.vidioc_s_fmt_vbi_cap		= saa7134_try_get_set_fmt_vbi_cap,
1927 	.vidioc_g_pixelaspect		= saa7134_g_pixelaspect,
1928 	.vidioc_reqbufs			= vb2_ioctl_reqbufs,
1929 	.vidioc_querybuf		= vb2_ioctl_querybuf,
1930 	.vidioc_qbuf			= vb2_ioctl_qbuf,
1931 	.vidioc_dqbuf			= vb2_ioctl_dqbuf,
1932 	.vidioc_expbuf			= vb2_ioctl_expbuf,
1933 	.vidioc_s_std			= saa7134_s_std,
1934 	.vidioc_g_std			= saa7134_g_std,
1935 	.vidioc_querystd		= saa7134_querystd,
1936 	.vidioc_enum_input		= saa7134_enum_input,
1937 	.vidioc_g_input			= saa7134_g_input,
1938 	.vidioc_s_input			= saa7134_s_input,
1939 	.vidioc_streamon		= vb2_ioctl_streamon,
1940 	.vidioc_streamoff		= vb2_ioctl_streamoff,
1941 	.vidioc_g_tuner			= saa7134_g_tuner,
1942 	.vidioc_s_tuner			= saa7134_s_tuner,
1943 	.vidioc_g_selection		= saa7134_g_selection,
1944 	.vidioc_s_selection		= saa7134_s_selection,
1945 	.vidioc_g_fbuf			= saa7134_g_fbuf,
1946 	.vidioc_s_fbuf			= saa7134_s_fbuf,
1947 	.vidioc_overlay			= saa7134_overlay,
1948 	.vidioc_g_frequency		= saa7134_g_frequency,
1949 	.vidioc_s_frequency		= saa7134_s_frequency,
1950 #ifdef CONFIG_VIDEO_ADV_DEBUG
1951 	.vidioc_g_register              = vidioc_g_register,
1952 	.vidioc_s_register              = vidioc_s_register,
1953 #endif
1954 	.vidioc_log_status		= v4l2_ctrl_log_status,
1955 	.vidioc_subscribe_event		= v4l2_ctrl_subscribe_event,
1956 	.vidioc_unsubscribe_event	= v4l2_event_unsubscribe,
1957 };
1958 
1959 static const struct v4l2_file_operations radio_fops = {
1960 	.owner	  = THIS_MODULE,
1961 	.open	  = video_open,
1962 	.read     = radio_read,
1963 	.release  = video_release,
1964 	.unlocked_ioctl	= video_ioctl2,
1965 	.poll     = radio_poll,
1966 };
1967 
1968 static const struct v4l2_ioctl_ops radio_ioctl_ops = {
1969 	.vidioc_querycap	= saa7134_querycap,
1970 	.vidioc_g_tuner		= radio_g_tuner,
1971 	.vidioc_s_tuner		= radio_s_tuner,
1972 	.vidioc_g_frequency	= saa7134_g_frequency,
1973 	.vidioc_s_frequency	= saa7134_s_frequency,
1974 	.vidioc_subscribe_event	= v4l2_ctrl_subscribe_event,
1975 	.vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1976 };
1977 
1978 /* ----------------------------------------------------------- */
1979 /* exported stuff                                              */
1980 
1981 struct video_device saa7134_video_template = {
1982 	.name				= "saa7134-video",
1983 	.fops				= &video_fops,
1984 	.ioctl_ops			= &video_ioctl_ops,
1985 	.tvnorms			= SAA7134_NORMS,
1986 };
1987 
1988 struct video_device saa7134_radio_template = {
1989 	.name			= "saa7134-radio",
1990 	.fops			= &radio_fops,
1991 	.ioctl_ops		= &radio_ioctl_ops,
1992 };
1993 
1994 static const struct v4l2_ctrl_ops saa7134_ctrl_ops = {
1995 	.s_ctrl = saa7134_s_ctrl,
1996 };
1997 
1998 static const struct v4l2_ctrl_config saa7134_ctrl_invert = {
1999 	.ops = &saa7134_ctrl_ops,
2000 	.id = V4L2_CID_PRIVATE_INVERT,
2001 	.name = "Invert",
2002 	.type = V4L2_CTRL_TYPE_BOOLEAN,
2003 	.min = 0,
2004 	.max = 1,
2005 	.step = 1,
2006 };
2007 
2008 static const struct v4l2_ctrl_config saa7134_ctrl_y_odd = {
2009 	.ops = &saa7134_ctrl_ops,
2010 	.id = V4L2_CID_PRIVATE_Y_ODD,
2011 	.name = "Y Offset Odd Field",
2012 	.type = V4L2_CTRL_TYPE_INTEGER,
2013 	.min = 0,
2014 	.max = 128,
2015 	.step = 1,
2016 };
2017 
2018 static const struct v4l2_ctrl_config saa7134_ctrl_y_even = {
2019 	.ops = &saa7134_ctrl_ops,
2020 	.id = V4L2_CID_PRIVATE_Y_EVEN,
2021 	.name = "Y Offset Even Field",
2022 	.type = V4L2_CTRL_TYPE_INTEGER,
2023 	.min = 0,
2024 	.max = 128,
2025 	.step = 1,
2026 };
2027 
2028 static const struct v4l2_ctrl_config saa7134_ctrl_automute = {
2029 	.ops = &saa7134_ctrl_ops,
2030 	.id = V4L2_CID_PRIVATE_AUTOMUTE,
2031 	.name = "Automute",
2032 	.type = V4L2_CTRL_TYPE_BOOLEAN,
2033 	.min = 0,
2034 	.max = 1,
2035 	.step = 1,
2036 	.def = 1,
2037 };
2038 
2039 int saa7134_video_init1(struct saa7134_dev *dev)
2040 {
2041 	struct v4l2_ctrl_handler *hdl = &dev->ctrl_handler;
2042 	struct vb2_queue *q;
2043 	int ret;
2044 
2045 	/* sanitycheck insmod options */
2046 	if (gbuffers < 2 || gbuffers > VIDEO_MAX_FRAME)
2047 		gbuffers = 2;
2048 	if (gbufsize > gbufsize_max)
2049 		gbufsize = gbufsize_max;
2050 	gbufsize = (gbufsize + PAGE_SIZE - 1) & PAGE_MASK;
2051 
2052 	v4l2_ctrl_handler_init(hdl, 11);
2053 	v4l2_ctrl_new_std(hdl, &saa7134_ctrl_ops,
2054 			V4L2_CID_BRIGHTNESS, 0, 255, 1, 128);
2055 	v4l2_ctrl_new_std(hdl, &saa7134_ctrl_ops,
2056 			V4L2_CID_CONTRAST, 0, 127, 1, 68);
2057 	v4l2_ctrl_new_std(hdl, &saa7134_ctrl_ops,
2058 			V4L2_CID_SATURATION, 0, 127, 1, 64);
2059 	v4l2_ctrl_new_std(hdl, &saa7134_ctrl_ops,
2060 			V4L2_CID_HUE, -128, 127, 1, 0);
2061 	v4l2_ctrl_new_std(hdl, &saa7134_ctrl_ops,
2062 			V4L2_CID_HFLIP, 0, 1, 1, 0);
2063 	v4l2_ctrl_new_std(hdl, &saa7134_ctrl_ops,
2064 			V4L2_CID_AUDIO_MUTE, 0, 1, 1, 0);
2065 	v4l2_ctrl_new_std(hdl, &saa7134_ctrl_ops,
2066 			V4L2_CID_AUDIO_VOLUME, -15, 15, 1, 0);
2067 	v4l2_ctrl_new_custom(hdl, &saa7134_ctrl_invert, NULL);
2068 	v4l2_ctrl_new_custom(hdl, &saa7134_ctrl_y_odd, NULL);
2069 	v4l2_ctrl_new_custom(hdl, &saa7134_ctrl_y_even, NULL);
2070 	v4l2_ctrl_new_custom(hdl, &saa7134_ctrl_automute, NULL);
2071 	if (hdl->error)
2072 		return hdl->error;
2073 	if (card_has_radio(dev)) {
2074 		hdl = &dev->radio_ctrl_handler;
2075 		v4l2_ctrl_handler_init(hdl, 2);
2076 		v4l2_ctrl_add_handler(hdl, &dev->ctrl_handler,
2077 				v4l2_ctrl_radio_filter, false);
2078 		if (hdl->error)
2079 			return hdl->error;
2080 	}
2081 	dev->ctl_mute       = 1;
2082 
2083 	if (dev->tda9887_conf && saa7134_ctrl_automute.def)
2084 		dev->tda9887_conf |= TDA9887_AUTOMUTE;
2085 	dev->automute       = 0;
2086 
2087 	INIT_LIST_HEAD(&dev->video_q.queue);
2088 	timer_setup(&dev->video_q.timeout, saa7134_buffer_timeout, 0);
2089 	dev->video_q.dev              = dev;
2090 	dev->fmt = format_by_fourcc(V4L2_PIX_FMT_BGR24);
2091 	dev->width    = 720;
2092 	dev->height   = 576;
2093 	dev->field = V4L2_FIELD_INTERLACED;
2094 	dev->win.w.width = dev->width;
2095 	dev->win.w.height = dev->height;
2096 	dev->win.field = V4L2_FIELD_INTERLACED;
2097 	dev->ovbuf.fmt.width = dev->width;
2098 	dev->ovbuf.fmt.height = dev->height;
2099 	dev->ovbuf.fmt.pixelformat = dev->fmt->fourcc;
2100 	dev->ovbuf.fmt.colorspace = V4L2_COLORSPACE_SMPTE170M;
2101 
2102 	if (saa7134_boards[dev->board].video_out)
2103 		saa7134_videoport_init(dev);
2104 
2105 	q = &dev->video_vbq;
2106 	q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2107 	/*
2108 	 * Do not add VB2_USERPTR unless explicitly requested: the saa7134 DMA
2109 	 * engine cannot handle transfers that do not start at the beginning
2110 	 * of a page. A user-provided pointer can start anywhere in a page, so
2111 	 * USERPTR support is a no-go unless the application knows about these
2112 	 * limitations and has special support for this.
2113 	 */
2114 	q->io_modes = VB2_MMAP | VB2_DMABUF | VB2_READ;
2115 	if (saa7134_userptr)
2116 		q->io_modes |= VB2_USERPTR;
2117 	q->drv_priv = &dev->video_q;
2118 	q->ops = &vb2_qops;
2119 	q->gfp_flags = GFP_DMA32;
2120 	q->mem_ops = &vb2_dma_sg_memops;
2121 	q->buf_struct_size = sizeof(struct saa7134_buf);
2122 	q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
2123 	q->lock = &dev->lock;
2124 	q->dev = &dev->pci->dev;
2125 	ret = vb2_queue_init(q);
2126 	if (ret)
2127 		return ret;
2128 	saa7134_pgtable_alloc(dev->pci, &dev->video_q.pt);
2129 
2130 	q = &dev->vbi_vbq;
2131 	q->type = V4L2_BUF_TYPE_VBI_CAPTURE;
2132 	/* Don't add VB2_USERPTR, see comment above */
2133 	q->io_modes = VB2_MMAP | VB2_READ;
2134 	if (saa7134_userptr)
2135 		q->io_modes |= VB2_USERPTR;
2136 	q->drv_priv = &dev->vbi_q;
2137 	q->ops = &saa7134_vbi_qops;
2138 	q->gfp_flags = GFP_DMA32;
2139 	q->mem_ops = &vb2_dma_sg_memops;
2140 	q->buf_struct_size = sizeof(struct saa7134_buf);
2141 	q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
2142 	q->lock = &dev->lock;
2143 	q->dev = &dev->pci->dev;
2144 	ret = vb2_queue_init(q);
2145 	if (ret)
2146 		return ret;
2147 	saa7134_pgtable_alloc(dev->pci, &dev->vbi_q.pt);
2148 
2149 	return 0;
2150 }
2151 
2152 void saa7134_video_fini(struct saa7134_dev *dev)
2153 {
2154 	/* free stuff */
2155 	saa7134_pgtable_free(dev->pci, &dev->video_q.pt);
2156 	saa7134_pgtable_free(dev->pci, &dev->vbi_q.pt);
2157 	v4l2_ctrl_handler_free(&dev->ctrl_handler);
2158 	if (card_has_radio(dev))
2159 		v4l2_ctrl_handler_free(&dev->radio_ctrl_handler);
2160 }
2161 
2162 int saa7134_videoport_init(struct saa7134_dev *dev)
2163 {
2164 	/* enable video output */
2165 	int vo = saa7134_boards[dev->board].video_out;
2166 	int video_reg;
2167 	unsigned int vid_port_opts = saa7134_boards[dev->board].vid_port_opts;
2168 
2169 	/* Configure videoport */
2170 	saa_writeb(SAA7134_VIDEO_PORT_CTRL0, video_out[vo][0]);
2171 	video_reg = video_out[vo][1];
2172 	if (vid_port_opts & SET_T_CODE_POLARITY_NON_INVERTED)
2173 		video_reg &= ~VP_T_CODE_P_INVERTED;
2174 	saa_writeb(SAA7134_VIDEO_PORT_CTRL1, video_reg);
2175 	saa_writeb(SAA7134_VIDEO_PORT_CTRL2, video_out[vo][2]);
2176 	saa_writeb(SAA7134_VIDEO_PORT_CTRL4, video_out[vo][4]);
2177 	video_reg = video_out[vo][5];
2178 	if (vid_port_opts & SET_CLOCK_NOT_DELAYED)
2179 		video_reg &= ~VP_CLK_CTRL2_DELAYED;
2180 	if (vid_port_opts & SET_CLOCK_INVERTED)
2181 		video_reg |= VP_CLK_CTRL1_INVERTED;
2182 	saa_writeb(SAA7134_VIDEO_PORT_CTRL5, video_reg);
2183 	video_reg = video_out[vo][6];
2184 	if (vid_port_opts & SET_VSYNC_OFF) {
2185 		video_reg &= ~VP_VS_TYPE_MASK;
2186 		video_reg |= VP_VS_TYPE_OFF;
2187 	}
2188 	saa_writeb(SAA7134_VIDEO_PORT_CTRL6, video_reg);
2189 	saa_writeb(SAA7134_VIDEO_PORT_CTRL7, video_out[vo][7]);
2190 	saa_writeb(SAA7134_VIDEO_PORT_CTRL8, video_out[vo][8]);
2191 
2192 	/* Start videoport */
2193 	saa_writeb(SAA7134_VIDEO_PORT_CTRL3, video_out[vo][3]);
2194 
2195 	return 0;
2196 }
2197 
2198 int saa7134_video_init2(struct saa7134_dev *dev)
2199 {
2200 	/* init video hw */
2201 	set_tvnorm(dev,&tvnorms[0]);
2202 	video_mux(dev,0);
2203 	v4l2_ctrl_handler_setup(&dev->ctrl_handler);
2204 	saa7134_tvaudio_setmute(dev);
2205 	saa7134_tvaudio_setvolume(dev,dev->ctl_volume);
2206 	return 0;
2207 }
2208 
2209 void saa7134_irq_video_signalchange(struct saa7134_dev *dev)
2210 {
2211 	static const char *st[] = {
2212 		"(no signal)", "NTSC", "PAL", "SECAM" };
2213 	u32 st1,st2;
2214 
2215 	st1 = saa_readb(SAA7134_STATUS_VIDEO1);
2216 	st2 = saa_readb(SAA7134_STATUS_VIDEO2);
2217 	video_dbg("DCSDT: pll: %s, sync: %s, norm: %s\n",
2218 		(st1 & 0x40) ? "not locked" : "locked",
2219 		(st2 & 0x40) ? "no"         : "yes",
2220 		st[st1 & 0x03]);
2221 	dev->nosignal = (st1 & 0x40) || (st2 & 0x40)  || !(st2 & 0x1);
2222 
2223 	if (dev->nosignal) {
2224 		/* no video signal -> mute audio */
2225 		if (dev->ctl_automute)
2226 			dev->automute = 1;
2227 		saa7134_tvaudio_setmute(dev);
2228 	} else {
2229 		/* wake up tvaudio audio carrier scan thread */
2230 		saa7134_tvaudio_do_scan(dev);
2231 	}
2232 
2233 	if ((st2 & 0x80) && !noninterlaced && !dev->nosignal)
2234 		saa_clearb(SAA7134_SYNC_CTRL, 0x20);
2235 	else
2236 		saa_setb(SAA7134_SYNC_CTRL, 0x20);
2237 
2238 	if (dev->mops && dev->mops->signal_change)
2239 		dev->mops->signal_change(dev);
2240 }
2241 
2242 
2243 void saa7134_irq_video_done(struct saa7134_dev *dev, unsigned long status)
2244 {
2245 	enum v4l2_field field;
2246 
2247 	spin_lock(&dev->slock);
2248 	if (dev->video_q.curr) {
2249 		field = dev->field;
2250 		if (V4L2_FIELD_HAS_BOTH(field)) {
2251 			/* make sure we have seen both fields */
2252 			if ((status & 0x10) == 0x00) {
2253 				dev->video_q.curr->top_seen = 1;
2254 				goto done;
2255 			}
2256 			if (!dev->video_q.curr->top_seen)
2257 				goto done;
2258 		} else if (field == V4L2_FIELD_TOP) {
2259 			if ((status & 0x10) != 0x10)
2260 				goto done;
2261 		} else if (field == V4L2_FIELD_BOTTOM) {
2262 			if ((status & 0x10) != 0x00)
2263 				goto done;
2264 		}
2265 		saa7134_buffer_finish(dev, &dev->video_q, VB2_BUF_STATE_DONE);
2266 	}
2267 	saa7134_buffer_next(dev, &dev->video_q);
2268 
2269  done:
2270 	spin_unlock(&dev->slock);
2271 }
2272