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