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