V4L/DVB (11370): v4l2-subdev: move s_std from tuner to core.
[linux-2.6-xlnx.git] / drivers / media / video / saa7134 / saa7134-video.c
blob493cad941460e0fc32725332d907e19959c474e6
1 /*
3 * device driver for philips saa7134 based TV cards
4 * video4linux video interface
6 * (c) 2001-03 Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]
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.
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.
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.
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>
30 #include "saa7134-reg.h"
31 #include "saa7134.h"
32 #include <media/v4l2-common.h>
33 #include <media/rds.h>
35 /* ------------------------------------------------------------------ */
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");
53 #define dprintk(fmt, arg...) if (video_debug&0x04) \
54 printk(KERN_DEBUG "%s/video: " fmt, dev->name , ## arg)
56 /* ------------------------------------------------------------------ */
57 /* Defines for Video Output Port Register at address 0x191 */
59 /* Bit 0: VIP code T bit polarity */
61 #define VP_T_CODE_P_NON_INVERTED 0x00
62 #define VP_T_CODE_P_INVERTED 0x01
64 /* ------------------------------------------------------------------ */
65 /* Defines for Video Output Port Register at address 0x195 */
67 /* Bit 2: Video output clock delay control */
69 #define VP_CLK_CTRL2_NOT_DELAYED 0x00
70 #define VP_CLK_CTRL2_DELAYED 0x04
72 /* Bit 1: Video output clock invert control */
74 #define VP_CLK_CTRL1_NON_INVERTED 0x00
75 #define VP_CLK_CTRL1_INVERTED 0x02
77 /* ------------------------------------------------------------------ */
78 /* Defines for Video Output Port Register at address 0x196 */
80 /* Bits 2 to 0: VSYNC pin video vertical sync type */
82 #define VP_VS_TYPE_MASK 0x07
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
93 /* ------------------------------------------------------------------ */
94 /* data structs for video */
96 static int video_out[][9] = {
97 [CCIR656] = { 0x00, 0xb1, 0x00, 0xa1, 0x00, 0x04, 0x06, 0x00, 0x00 },
100 static struct saa7134_format formats[] = {
102 .name = "8 bpp gray",
103 .fourcc = V4L2_PIX_FMT_GREY,
104 .depth = 8,
105 .pm = 0x06,
107 .name = "15 bpp RGB, le",
108 .fourcc = V4L2_PIX_FMT_RGB555,
109 .depth = 16,
110 .pm = 0x13 | 0x80,
112 .name = "15 bpp RGB, be",
113 .fourcc = V4L2_PIX_FMT_RGB555X,
114 .depth = 16,
115 .pm = 0x13 | 0x80,
116 .bswap = 1,
118 .name = "16 bpp RGB, le",
119 .fourcc = V4L2_PIX_FMT_RGB565,
120 .depth = 16,
121 .pm = 0x10 | 0x80,
123 .name = "16 bpp RGB, be",
124 .fourcc = V4L2_PIX_FMT_RGB565X,
125 .depth = 16,
126 .pm = 0x10 | 0x80,
127 .bswap = 1,
129 .name = "24 bpp RGB, le",
130 .fourcc = V4L2_PIX_FMT_BGR24,
131 .depth = 24,
132 .pm = 0x11,
134 .name = "24 bpp RGB, be",
135 .fourcc = V4L2_PIX_FMT_RGB24,
136 .depth = 24,
137 .pm = 0x11,
138 .bswap = 1,
140 .name = "32 bpp RGB, le",
141 .fourcc = V4L2_PIX_FMT_BGR32,
142 .depth = 32,
143 .pm = 0x12,
145 .name = "32 bpp RGB, be",
146 .fourcc = V4L2_PIX_FMT_RGB32,
147 .depth = 32,
148 .pm = 0x12,
149 .bswap = 1,
150 .wswap = 1,
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,
159 .name = "4:2:2 packed, UYVY",
160 .fourcc = V4L2_PIX_FMT_UYVY,
161 .depth = 16,
162 .pm = 0x00,
163 .yuv = 1,
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,
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,
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,
194 #define FORMATS ARRAY_SIZE(formats)
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
206 #define NORM_525_60 \
207 .h_start = 0, \
208 .h_stop = 703, \
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
216 static struct saa7134_tvnorm tvnorms[] = {
218 .name = "PAL", /* autodetect */
219 .id = V4L2_STD_PAL,
220 NORM_625_50,
222 .sync_control = 0x18,
223 .luma_control = 0x40,
224 .chroma_ctrl1 = 0x81,
225 .chroma_gain = 0x2a,
226 .chroma_ctrl2 = 0x06,
227 .vgate_misc = 0x1c,
230 .name = "PAL-BG",
231 .id = V4L2_STD_PAL_BG,
232 NORM_625_50,
234 .sync_control = 0x18,
235 .luma_control = 0x40,
236 .chroma_ctrl1 = 0x81,
237 .chroma_gain = 0x2a,
238 .chroma_ctrl2 = 0x06,
239 .vgate_misc = 0x1c,
242 .name = "PAL-I",
243 .id = V4L2_STD_PAL_I,
244 NORM_625_50,
246 .sync_control = 0x18,
247 .luma_control = 0x40,
248 .chroma_ctrl1 = 0x81,
249 .chroma_gain = 0x2a,
250 .chroma_ctrl2 = 0x06,
251 .vgate_misc = 0x1c,
254 .name = "PAL-DK",
255 .id = V4L2_STD_PAL_DK,
256 NORM_625_50,
258 .sync_control = 0x18,
259 .luma_control = 0x40,
260 .chroma_ctrl1 = 0x81,
261 .chroma_gain = 0x2a,
262 .chroma_ctrl2 = 0x06,
263 .vgate_misc = 0x1c,
266 .name = "NTSC",
267 .id = V4L2_STD_NTSC,
268 NORM_525_60,
270 .sync_control = 0x59,
271 .luma_control = 0x40,
272 .chroma_ctrl1 = 0x89,
273 .chroma_gain = 0x2a,
274 .chroma_ctrl2 = 0x0e,
275 .vgate_misc = 0x18,
278 .name = "SECAM",
279 .id = V4L2_STD_SECAM,
280 NORM_625_50,
282 .sync_control = 0x18,
283 .luma_control = 0x1b,
284 .chroma_ctrl1 = 0xd1,
285 .chroma_gain = 0x80,
286 .chroma_ctrl2 = 0x00,
287 .vgate_misc = 0x1c,
290 .name = "SECAM-DK",
291 .id = V4L2_STD_SECAM_DK,
292 NORM_625_50,
294 .sync_control = 0x18,
295 .luma_control = 0x1b,
296 .chroma_ctrl1 = 0xd1,
297 .chroma_gain = 0x80,
298 .chroma_ctrl2 = 0x00,
299 .vgate_misc = 0x1c,
302 .name = "SECAM-L",
303 .id = V4L2_STD_SECAM_L,
304 NORM_625_50,
306 .sync_control = 0x18,
307 .luma_control = 0x1b,
308 .chroma_ctrl1 = 0xd1,
309 .chroma_gain = 0x80,
310 .chroma_ctrl2 = 0x00,
311 .vgate_misc = 0x1c,
314 .name = "SECAM-Lc",
315 .id = V4L2_STD_SECAM_LC,
316 NORM_625_50,
318 .sync_control = 0x18,
319 .luma_control = 0x1b,
320 .chroma_ctrl1 = 0xd1,
321 .chroma_gain = 0x80,
322 .chroma_ctrl2 = 0x00,
323 .vgate_misc = 0x1c,
326 .name = "PAL-M",
327 .id = V4L2_STD_PAL_M,
328 NORM_525_60,
330 .sync_control = 0x59,
331 .luma_control = 0x40,
332 .chroma_ctrl1 = 0xb9,
333 .chroma_gain = 0x2a,
334 .chroma_ctrl2 = 0x0e,
335 .vgate_misc = 0x18,
338 .name = "PAL-Nc",
339 .id = V4L2_STD_PAL_Nc,
340 NORM_625_50,
342 .sync_control = 0x18,
343 .luma_control = 0x40,
344 .chroma_ctrl1 = 0xa1,
345 .chroma_gain = 0x2a,
346 .chroma_ctrl2 = 0x06,
347 .vgate_misc = 0x1c,
350 .name = "PAL-60",
351 .id = V4L2_STD_PAL_60,
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,
362 .sync_control = 0x18,
363 .luma_control = 0x40,
364 .chroma_ctrl1 = 0x81,
365 .chroma_gain = 0x2a,
366 .chroma_ctrl2 = 0x06,
367 .vgate_misc = 0x1c,
370 #define TVNORMS ARRAY_SIZE(tvnorms)
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)
378 static const struct v4l2_queryctrl no_ctrl = {
379 .name = "42",
380 .flags = V4L2_CTRL_FLAG_DISABLED,
382 static const struct v4l2_queryctrl video_ctrls[] = {
383 /* --- video --- */
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,
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,
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,
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,
417 .id = V4L2_CID_HFLIP,
418 .name = "Mirror",
419 .minimum = 0,
420 .maximum = 1,
421 .type = V4L2_CTRL_TYPE_BOOLEAN,
423 /* --- audio --- */
425 .id = V4L2_CID_AUDIO_MUTE,
426 .name = "Mute",
427 .minimum = 0,
428 .maximum = 1,
429 .type = V4L2_CTRL_TYPE_BOOLEAN,
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,
439 /* --- private --- */
441 .id = V4L2_CID_PRIVATE_INVERT,
442 .name = "Invert",
443 .minimum = 0,
444 .maximum = 1,
445 .type = V4L2_CTRL_TYPE_BOOLEAN,
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,
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,
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,
471 static const unsigned int CTRLS = ARRAY_SIZE(video_ctrls);
473 static const struct v4l2_queryctrl* ctrl_by_id(unsigned int id)
475 unsigned int i;
477 for (i = 0; i < CTRLS; i++)
478 if (video_ctrls[i].id == id)
479 return video_ctrls+i;
480 return NULL;
483 static struct saa7134_format* format_by_fourcc(unsigned int fourcc)
485 unsigned int i;
487 for (i = 0; i < FORMATS; i++)
488 if (formats[i].fourcc == fourcc)
489 return formats+i;
490 return NULL;
493 /* ----------------------------------------------------------------------- */
494 /* resource management */
496 static int res_get(struct saa7134_dev *dev, struct saa7134_fh *fh, unsigned int bit)
498 if (fh->resources & bit)
499 /* have it already allocated */
500 return 1;
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;
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;
517 static int res_check(struct saa7134_fh *fh, unsigned int bit)
519 return (fh->resources & bit);
522 static int res_locked(struct saa7134_dev *dev, unsigned int bit)
524 return (dev->resources & bit);
527 static
528 void res_free(struct saa7134_dev *dev, struct saa7134_fh *fh, unsigned int bits)
530 BUG_ON((fh->resources & bits) != bits);
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);
539 /* ------------------------------------------------------------------ */
541 static void set_tvnorm(struct saa7134_dev *dev, struct saa7134_tvnorm *norm)
543 dprintk("set tv norm = %s\n",norm->name);
544 dev->tvnorm = norm;
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;
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;
558 dev->crop_current = dev->crop_defrect;
560 saa7134_set_tvnorm_hw(dev);
563 static void video_mux(struct saa7134_dev *dev, int input)
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));
572 static void saa7134_set_decoder(struct saa7134_dev *dev)
574 int luma_control, sync_control, mux;
576 struct saa7134_tvnorm *norm = dev->tvnorm;
577 mux = card_in(dev, dev->ctl_input).vmux;
579 luma_control = norm->luma_control;
580 sync_control = norm->sync_control;
582 if (mux > 5)
583 luma_control |= 0x80; /* svideo */
584 if (noninterlaced || dev->nosignal)
585 sync_control |= 0x20;
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);
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);
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);
602 saa_writeb(SAA7134_DEC_LUMA_CONTRAST,
603 dev->ctl_invert ? -dev->ctl_contrast : dev->ctl_contrast);
605 saa_writeb(SAA7134_DEC_CHROMA_SATURATION,
606 dev->ctl_invert ? -dev->ctl_saturation : dev->ctl_saturation);
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);
612 saa_writeb(SAA7134_CHROMA_CTRL2, norm->chroma_ctrl2);
613 saa_writeb(SAA7134_MODE_DELAY_CTRL, 0x00);
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);
623 void saa7134_set_tvnorm_hw(struct saa7134_dev *dev)
625 saa7134_set_decoder(dev);
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);
634 static void set_h_prescale(struct saa7134_dev *dev, int task, int prescale)
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 },
655 static const int count = ARRAY_SIZE(vals);
656 int i;
658 for (i = 0; i < count; i++)
659 if (vals[i].xpsc == prescale)
660 break;
661 if (i == count)
662 return;
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);
672 static void set_v_scale(struct saa7134_dev *dev, int task, int yscale)
674 int val,mirror;
676 saa_writeb(SAA7134_V_SCALE_RATIO1(task), yscale & 0xff);
677 saa_writeb(SAA7134_V_SCALE_RATIO2(task), yscale >> 8);
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);
694 saa_writeb(SAA7134_LUMA_BRIGHT(task), 0x80);
697 static void set_size(struct saa7134_dev *dev, int task,
698 int width, int height, int interlace)
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;
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;
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);
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);
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);
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);
744 /* ------------------------------------------------------------------ */
746 struct cliplist {
747 __u16 position;
748 __u8 enable;
749 __u8 disable;
752 static void set_cliplist(struct saa7134_dev *dev, int reg,
753 struct cliplist *cl, int entries, char *name)
755 __u8 winbits = 0;
756 int i;
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;
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);
778 static int clip_range(int val)
780 if (val < 0)
781 val = 0;
782 return val;
785 /* Sort into smallest position first order */
786 static int cliplist_cmp(const void *a, const void *b)
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;
797 static int setup_clipping(struct saa7134_dev *dev, struct v4l2_clip *clips,
798 int nclips, int interlace)
800 struct cliplist col[16], row[16];
801 int cols = 0, rows = 0, i;
802 int div = interlace ? 2 : 1;
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++;
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;
828 static int verify_preview(struct saa7134_dev *dev, struct v4l2_window *win)
830 enum v4l2_field field;
831 int maxw, maxh;
833 if (NULL == dev->ovbuf.base)
834 return -EINVAL;
835 if (NULL == dev->ovfmt)
836 return -EINVAL;
837 if (win->w.width < 48 || win->w.height < 32)
838 return -EINVAL;
839 if (win->clipcount > 2048)
840 return -EINVAL;
842 field = win->field;
843 maxw = dev->crop_current.width;
844 maxh = dev->crop_current.height;
846 if (V4L2_FIELD_ANY == field) {
847 field = (win->w.height > maxh/2)
848 ? V4L2_FIELD_INTERLACED
849 : V4L2_FIELD_TOP;
851 switch (field) {
852 case V4L2_FIELD_TOP:
853 case V4L2_FIELD_BOTTOM:
854 maxh = maxh / 2;
855 break;
856 case V4L2_FIELD_INTERLACED:
857 break;
858 default:
859 return -EINVAL;
862 win->field = field;
863 if (win->w.width > maxw)
864 win->w.width = maxw;
865 if (win->w.height > maxh)
866 win->w.height = maxh;
867 return 0;
870 static int start_preview(struct saa7134_dev *dev, struct saa7134_fh *fh)
872 unsigned long base,control,bpl;
873 int err;
875 err = verify_preview(dev,&fh->win);
876 if (0 != err)
877 return err;
879 dev->ovfield = fh->win.field;
880 dprintk("start_preview %dx%d+%d+%d %s field=%s\n",
881 fh->win.w.width,fh->win.w.height,
882 fh->win.w.left,fh->win.w.top,
883 dev->ovfmt->name,v4l2_field_names[dev->ovfield]);
885 /* setup window + clipping */
886 set_size(dev,TASK_B,fh->win.w.width,fh->win.w.height,
887 V4L2_FIELD_HAS_BOTH(dev->ovfield));
888 setup_clipping(dev,fh->clips,fh->nclips,
889 V4L2_FIELD_HAS_BOTH(dev->ovfield));
890 if (dev->ovfmt->yuv)
891 saa_andorb(SAA7134_DATA_PATH(TASK_B), 0x3f, 0x03);
892 else
893 saa_andorb(SAA7134_DATA_PATH(TASK_B), 0x3f, 0x01);
894 saa_writeb(SAA7134_OFMT_VIDEO_B, dev->ovfmt->pm | 0x20);
896 /* dma: setup channel 1 (= Video Task B) */
897 base = (unsigned long)dev->ovbuf.base;
898 base += dev->ovbuf.fmt.bytesperline * fh->win.w.top;
899 base += dev->ovfmt->depth/8 * fh->win.w.left;
900 bpl = dev->ovbuf.fmt.bytesperline;
901 control = SAA7134_RS_CONTROL_BURST_16;
902 if (dev->ovfmt->bswap)
903 control |= SAA7134_RS_CONTROL_BSWAP;
904 if (dev->ovfmt->wswap)
905 control |= SAA7134_RS_CONTROL_WSWAP;
906 if (V4L2_FIELD_HAS_BOTH(dev->ovfield)) {
907 saa_writel(SAA7134_RS_BA1(1),base);
908 saa_writel(SAA7134_RS_BA2(1),base+bpl);
909 saa_writel(SAA7134_RS_PITCH(1),bpl*2);
910 saa_writel(SAA7134_RS_CONTROL(1),control);
911 } else {
912 saa_writel(SAA7134_RS_BA1(1),base);
913 saa_writel(SAA7134_RS_BA2(1),base);
914 saa_writel(SAA7134_RS_PITCH(1),bpl);
915 saa_writel(SAA7134_RS_CONTROL(1),control);
918 /* start dma */
919 dev->ovenable = 1;
920 saa7134_set_dmabits(dev);
922 return 0;
925 static int stop_preview(struct saa7134_dev *dev, struct saa7134_fh *fh)
927 dev->ovenable = 0;
928 saa7134_set_dmabits(dev);
929 return 0;
932 /* ------------------------------------------------------------------ */
934 static int buffer_activate(struct saa7134_dev *dev,
935 struct saa7134_buf *buf,
936 struct saa7134_buf *next)
938 unsigned long base,control,bpl;
939 unsigned long bpl_uv,lines_uv,base2,base3,tmp; /* planar */
941 dprintk("buffer_activate buf=%p\n",buf);
942 buf->vb.state = VIDEOBUF_ACTIVE;
943 buf->top_seen = 0;
945 set_size(dev,TASK_A,buf->vb.width,buf->vb.height,
946 V4L2_FIELD_HAS_BOTH(buf->vb.field));
947 if (buf->fmt->yuv)
948 saa_andorb(SAA7134_DATA_PATH(TASK_A), 0x3f, 0x03);
949 else
950 saa_andorb(SAA7134_DATA_PATH(TASK_A), 0x3f, 0x01);
951 saa_writeb(SAA7134_OFMT_VIDEO_A, buf->fmt->pm);
953 /* DMA: setup channel 0 (= Video Task A0) */
954 base = saa7134_buffer_base(buf);
955 if (buf->fmt->planar)
956 bpl = buf->vb.width;
957 else
958 bpl = (buf->vb.width * buf->fmt->depth) / 8;
959 control = SAA7134_RS_CONTROL_BURST_16 |
960 SAA7134_RS_CONTROL_ME |
961 (buf->pt->dma >> 12);
962 if (buf->fmt->bswap)
963 control |= SAA7134_RS_CONTROL_BSWAP;
964 if (buf->fmt->wswap)
965 control |= SAA7134_RS_CONTROL_WSWAP;
966 if (V4L2_FIELD_HAS_BOTH(buf->vb.field)) {
967 /* interlaced */
968 saa_writel(SAA7134_RS_BA1(0),base);
969 saa_writel(SAA7134_RS_BA2(0),base+bpl);
970 saa_writel(SAA7134_RS_PITCH(0),bpl*2);
971 } else {
972 /* non-interlaced */
973 saa_writel(SAA7134_RS_BA1(0),base);
974 saa_writel(SAA7134_RS_BA2(0),base);
975 saa_writel(SAA7134_RS_PITCH(0),bpl);
977 saa_writel(SAA7134_RS_CONTROL(0),control);
979 if (buf->fmt->planar) {
980 /* DMA: setup channel 4+5 (= planar task A) */
981 bpl_uv = bpl >> buf->fmt->hshift;
982 lines_uv = buf->vb.height >> buf->fmt->vshift;
983 base2 = base + bpl * buf->vb.height;
984 base3 = base2 + bpl_uv * lines_uv;
985 if (buf->fmt->uvswap)
986 tmp = base2, base2 = base3, base3 = tmp;
987 dprintk("uv: bpl=%ld lines=%ld base2/3=%ld/%ld\n",
988 bpl_uv,lines_uv,base2,base3);
989 if (V4L2_FIELD_HAS_BOTH(buf->vb.field)) {
990 /* interlaced */
991 saa_writel(SAA7134_RS_BA1(4),base2);
992 saa_writel(SAA7134_RS_BA2(4),base2+bpl_uv);
993 saa_writel(SAA7134_RS_PITCH(4),bpl_uv*2);
994 saa_writel(SAA7134_RS_BA1(5),base3);
995 saa_writel(SAA7134_RS_BA2(5),base3+bpl_uv);
996 saa_writel(SAA7134_RS_PITCH(5),bpl_uv*2);
997 } else {
998 /* non-interlaced */
999 saa_writel(SAA7134_RS_BA1(4),base2);
1000 saa_writel(SAA7134_RS_BA2(4),base2);
1001 saa_writel(SAA7134_RS_PITCH(4),bpl_uv);
1002 saa_writel(SAA7134_RS_BA1(5),base3);
1003 saa_writel(SAA7134_RS_BA2(5),base3);
1004 saa_writel(SAA7134_RS_PITCH(5),bpl_uv);
1006 saa_writel(SAA7134_RS_CONTROL(4),control);
1007 saa_writel(SAA7134_RS_CONTROL(5),control);
1010 /* start DMA */
1011 saa7134_set_dmabits(dev);
1012 mod_timer(&dev->video_q.timeout, jiffies+BUFFER_TIMEOUT);
1013 return 0;
1016 static int buffer_prepare(struct videobuf_queue *q,
1017 struct videobuf_buffer *vb,
1018 enum v4l2_field field)
1020 struct saa7134_fh *fh = q->priv_data;
1021 struct saa7134_dev *dev = fh->dev;
1022 struct saa7134_buf *buf = container_of(vb,struct saa7134_buf,vb);
1023 unsigned int size;
1024 int err;
1026 /* sanity checks */
1027 if (NULL == fh->fmt)
1028 return -EINVAL;
1029 if (fh->width < 48 ||
1030 fh->height < 32 ||
1031 fh->width/4 > dev->crop_current.width ||
1032 fh->height/4 > dev->crop_current.height ||
1033 fh->width > dev->crop_bounds.width ||
1034 fh->height > dev->crop_bounds.height)
1035 return -EINVAL;
1036 size = (fh->width * fh->height * fh->fmt->depth) >> 3;
1037 if (0 != buf->vb.baddr && buf->vb.bsize < size)
1038 return -EINVAL;
1040 dprintk("buffer_prepare [%d,size=%dx%d,bytes=%d,fields=%s,%s]\n",
1041 vb->i,fh->width,fh->height,size,v4l2_field_names[field],
1042 fh->fmt->name);
1043 if (buf->vb.width != fh->width ||
1044 buf->vb.height != fh->height ||
1045 buf->vb.size != size ||
1046 buf->vb.field != field ||
1047 buf->fmt != fh->fmt) {
1048 saa7134_dma_free(q,buf);
1051 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
1052 struct videobuf_dmabuf *dma=videobuf_to_dma(&buf->vb);
1054 buf->vb.width = fh->width;
1055 buf->vb.height = fh->height;
1056 buf->vb.size = size;
1057 buf->vb.field = field;
1058 buf->fmt = fh->fmt;
1059 buf->pt = &fh->pt_cap;
1061 err = videobuf_iolock(q,&buf->vb,&dev->ovbuf);
1062 if (err)
1063 goto oops;
1064 err = saa7134_pgtable_build(dev->pci,buf->pt,
1065 dma->sglist,
1066 dma->sglen,
1067 saa7134_buffer_startpage(buf));
1068 if (err)
1069 goto oops;
1071 buf->vb.state = VIDEOBUF_PREPARED;
1072 buf->activate = buffer_activate;
1073 return 0;
1075 oops:
1076 saa7134_dma_free(q,buf);
1077 return err;
1080 static int
1081 buffer_setup(struct videobuf_queue *q, unsigned int *count, unsigned int *size)
1083 struct saa7134_fh *fh = q->priv_data;
1085 *size = fh->fmt->depth * fh->width * fh->height >> 3;
1086 if (0 == *count)
1087 *count = gbuffers;
1088 *count = saa7134_buffer_count(*size,*count);
1089 return 0;
1092 static void buffer_queue(struct videobuf_queue *q, struct videobuf_buffer *vb)
1094 struct saa7134_fh *fh = q->priv_data;
1095 struct saa7134_buf *buf = container_of(vb,struct saa7134_buf,vb);
1097 saa7134_buffer_queue(fh->dev,&fh->dev->video_q,buf);
1100 static void buffer_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
1102 struct saa7134_buf *buf = container_of(vb,struct saa7134_buf,vb);
1104 saa7134_dma_free(q,buf);
1107 static struct videobuf_queue_ops video_qops = {
1108 .buf_setup = buffer_setup,
1109 .buf_prepare = buffer_prepare,
1110 .buf_queue = buffer_queue,
1111 .buf_release = buffer_release,
1114 /* ------------------------------------------------------------------ */
1116 int saa7134_g_ctrl_internal(struct saa7134_dev *dev, struct saa7134_fh *fh, struct v4l2_control *c)
1118 const struct v4l2_queryctrl* ctrl;
1120 ctrl = ctrl_by_id(c->id);
1121 if (NULL == ctrl)
1122 return -EINVAL;
1123 switch (c->id) {
1124 case V4L2_CID_BRIGHTNESS:
1125 c->value = dev->ctl_bright;
1126 break;
1127 case V4L2_CID_HUE:
1128 c->value = dev->ctl_hue;
1129 break;
1130 case V4L2_CID_CONTRAST:
1131 c->value = dev->ctl_contrast;
1132 break;
1133 case V4L2_CID_SATURATION:
1134 c->value = dev->ctl_saturation;
1135 break;
1136 case V4L2_CID_AUDIO_MUTE:
1137 c->value = dev->ctl_mute;
1138 break;
1139 case V4L2_CID_AUDIO_VOLUME:
1140 c->value = dev->ctl_volume;
1141 break;
1142 case V4L2_CID_PRIVATE_INVERT:
1143 c->value = dev->ctl_invert;
1144 break;
1145 case V4L2_CID_HFLIP:
1146 c->value = dev->ctl_mirror;
1147 break;
1148 case V4L2_CID_PRIVATE_Y_EVEN:
1149 c->value = dev->ctl_y_even;
1150 break;
1151 case V4L2_CID_PRIVATE_Y_ODD:
1152 c->value = dev->ctl_y_odd;
1153 break;
1154 case V4L2_CID_PRIVATE_AUTOMUTE:
1155 c->value = dev->ctl_automute;
1156 break;
1157 default:
1158 return -EINVAL;
1160 return 0;
1162 EXPORT_SYMBOL_GPL(saa7134_g_ctrl_internal);
1164 static int saa7134_g_ctrl(struct file *file, void *priv, struct v4l2_control *c)
1166 struct saa7134_fh *fh = priv;
1168 return saa7134_g_ctrl_internal(fh->dev, fh, c);
1171 int saa7134_s_ctrl_internal(struct saa7134_dev *dev, struct saa7134_fh *fh, struct v4l2_control *c)
1173 const struct v4l2_queryctrl* ctrl;
1174 unsigned long flags;
1175 int restart_overlay = 0;
1176 int err;
1178 /* When called from the empress code fh == NULL.
1179 That needs to be fixed somehow, but for now this is
1180 good enough. */
1181 if (fh) {
1182 err = v4l2_prio_check(&dev->prio, &fh->prio);
1183 if (0 != err)
1184 return err;
1186 err = -EINVAL;
1188 mutex_lock(&dev->lock);
1190 ctrl = ctrl_by_id(c->id);
1191 if (NULL == ctrl)
1192 goto error;
1194 dprintk("set_control name=%s val=%d\n",ctrl->name,c->value);
1195 switch (ctrl->type) {
1196 case V4L2_CTRL_TYPE_BOOLEAN:
1197 case V4L2_CTRL_TYPE_MENU:
1198 case V4L2_CTRL_TYPE_INTEGER:
1199 if (c->value < ctrl->minimum)
1200 c->value = ctrl->minimum;
1201 if (c->value > ctrl->maximum)
1202 c->value = ctrl->maximum;
1203 break;
1204 default:
1205 /* nothing */;
1207 switch (c->id) {
1208 case V4L2_CID_BRIGHTNESS:
1209 dev->ctl_bright = c->value;
1210 saa_writeb(SAA7134_DEC_LUMA_BRIGHT, dev->ctl_bright);
1211 break;
1212 case V4L2_CID_HUE:
1213 dev->ctl_hue = c->value;
1214 saa_writeb(SAA7134_DEC_CHROMA_HUE, dev->ctl_hue);
1215 break;
1216 case V4L2_CID_CONTRAST:
1217 dev->ctl_contrast = c->value;
1218 saa_writeb(SAA7134_DEC_LUMA_CONTRAST,
1219 dev->ctl_invert ? -dev->ctl_contrast : dev->ctl_contrast);
1220 break;
1221 case V4L2_CID_SATURATION:
1222 dev->ctl_saturation = c->value;
1223 saa_writeb(SAA7134_DEC_CHROMA_SATURATION,
1224 dev->ctl_invert ? -dev->ctl_saturation : dev->ctl_saturation);
1225 break;
1226 case V4L2_CID_AUDIO_MUTE:
1227 dev->ctl_mute = c->value;
1228 saa7134_tvaudio_setmute(dev);
1229 break;
1230 case V4L2_CID_AUDIO_VOLUME:
1231 dev->ctl_volume = c->value;
1232 saa7134_tvaudio_setvolume(dev,dev->ctl_volume);
1233 break;
1234 case V4L2_CID_PRIVATE_INVERT:
1235 dev->ctl_invert = c->value;
1236 saa_writeb(SAA7134_DEC_LUMA_CONTRAST,
1237 dev->ctl_invert ? -dev->ctl_contrast : dev->ctl_contrast);
1238 saa_writeb(SAA7134_DEC_CHROMA_SATURATION,
1239 dev->ctl_invert ? -dev->ctl_saturation : dev->ctl_saturation);
1240 break;
1241 case V4L2_CID_HFLIP:
1242 dev->ctl_mirror = c->value;
1243 restart_overlay = 1;
1244 break;
1245 case V4L2_CID_PRIVATE_Y_EVEN:
1246 dev->ctl_y_even = c->value;
1247 restart_overlay = 1;
1248 break;
1249 case V4L2_CID_PRIVATE_Y_ODD:
1250 dev->ctl_y_odd = c->value;
1251 restart_overlay = 1;
1252 break;
1253 case V4L2_CID_PRIVATE_AUTOMUTE:
1255 struct v4l2_priv_tun_config tda9887_cfg;
1257 tda9887_cfg.tuner = TUNER_TDA9887;
1258 tda9887_cfg.priv = &dev->tda9887_conf;
1260 dev->ctl_automute = c->value;
1261 if (dev->tda9887_conf) {
1262 if (dev->ctl_automute)
1263 dev->tda9887_conf |= TDA9887_AUTOMUTE;
1264 else
1265 dev->tda9887_conf &= ~TDA9887_AUTOMUTE;
1267 saa_call_all(dev, tuner, s_config, &tda9887_cfg);
1269 break;
1271 default:
1272 goto error;
1274 if (restart_overlay && fh && res_check(fh, RESOURCE_OVERLAY)) {
1275 spin_lock_irqsave(&dev->slock,flags);
1276 stop_preview(dev,fh);
1277 start_preview(dev,fh);
1278 spin_unlock_irqrestore(&dev->slock,flags);
1280 err = 0;
1282 error:
1283 mutex_unlock(&dev->lock);
1284 return err;
1286 EXPORT_SYMBOL_GPL(saa7134_s_ctrl_internal);
1288 static int saa7134_s_ctrl(struct file *file, void *f, struct v4l2_control *c)
1290 struct saa7134_fh *fh = f;
1292 return saa7134_s_ctrl_internal(fh->dev, fh, c);
1295 /* ------------------------------------------------------------------ */
1297 static struct videobuf_queue* saa7134_queue(struct saa7134_fh *fh)
1299 struct videobuf_queue* q = NULL;
1301 switch (fh->type) {
1302 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1303 q = &fh->cap;
1304 break;
1305 case V4L2_BUF_TYPE_VBI_CAPTURE:
1306 q = &fh->vbi;
1307 break;
1308 default:
1309 BUG();
1311 return q;
1314 static int saa7134_resource(struct saa7134_fh *fh)
1316 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1317 return RESOURCE_VIDEO;
1319 if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)
1320 return RESOURCE_VBI;
1322 BUG();
1323 return 0;
1326 static int video_open(struct file *file)
1328 int minor = video_devdata(file)->minor;
1329 struct saa7134_dev *dev;
1330 struct saa7134_fh *fh;
1331 enum v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1332 int radio = 0;
1334 mutex_lock(&saa7134_devlist_lock);
1335 list_for_each_entry(dev, &saa7134_devlist, devlist) {
1336 if (dev->video_dev && (dev->video_dev->minor == minor))
1337 goto found;
1338 if (dev->radio_dev && (dev->radio_dev->minor == minor)) {
1339 radio = 1;
1340 goto found;
1342 if (dev->vbi_dev && (dev->vbi_dev->minor == minor)) {
1343 type = V4L2_BUF_TYPE_VBI_CAPTURE;
1344 goto found;
1347 mutex_unlock(&saa7134_devlist_lock);
1348 return -ENODEV;
1350 found:
1351 mutex_unlock(&saa7134_devlist_lock);
1353 dprintk("open minor=%d radio=%d type=%s\n",minor,radio,
1354 v4l2_type_names[type]);
1356 /* allocate + initialize per filehandle data */
1357 fh = kzalloc(sizeof(*fh),GFP_KERNEL);
1358 if (NULL == fh)
1359 return -ENOMEM;
1361 file->private_data = fh;
1362 fh->dev = dev;
1363 fh->radio = radio;
1364 fh->type = type;
1365 fh->fmt = format_by_fourcc(V4L2_PIX_FMT_BGR24);
1366 fh->width = 720;
1367 fh->height = 576;
1368 v4l2_prio_open(&dev->prio,&fh->prio);
1370 videobuf_queue_sg_init(&fh->cap, &video_qops,
1371 &dev->pci->dev, &dev->slock,
1372 V4L2_BUF_TYPE_VIDEO_CAPTURE,
1373 V4L2_FIELD_INTERLACED,
1374 sizeof(struct saa7134_buf),
1375 fh);
1376 videobuf_queue_sg_init(&fh->vbi, &saa7134_vbi_qops,
1377 &dev->pci->dev, &dev->slock,
1378 V4L2_BUF_TYPE_VBI_CAPTURE,
1379 V4L2_FIELD_SEQ_TB,
1380 sizeof(struct saa7134_buf),
1381 fh);
1382 saa7134_pgtable_alloc(dev->pci,&fh->pt_cap);
1383 saa7134_pgtable_alloc(dev->pci,&fh->pt_vbi);
1385 if (fh->radio) {
1386 /* switch to radio mode */
1387 saa7134_tvaudio_setinput(dev,&card(dev).radio);
1388 saa_call_all(dev, tuner, s_radio);
1389 } else {
1390 /* switch to video/vbi mode */
1391 video_mux(dev,dev->ctl_input);
1393 return 0;
1396 static ssize_t
1397 video_read(struct file *file, char __user *data, size_t count, loff_t *ppos)
1399 struct saa7134_fh *fh = file->private_data;
1401 switch (fh->type) {
1402 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1403 if (res_locked(fh->dev,RESOURCE_VIDEO))
1404 return -EBUSY;
1405 return videobuf_read_one(saa7134_queue(fh),
1406 data, count, ppos,
1407 file->f_flags & O_NONBLOCK);
1408 case V4L2_BUF_TYPE_VBI_CAPTURE:
1409 if (!res_get(fh->dev,fh,RESOURCE_VBI))
1410 return -EBUSY;
1411 return videobuf_read_stream(saa7134_queue(fh),
1412 data, count, ppos, 1,
1413 file->f_flags & O_NONBLOCK);
1414 break;
1415 default:
1416 BUG();
1417 return 0;
1421 static unsigned int
1422 video_poll(struct file *file, struct poll_table_struct *wait)
1424 struct saa7134_fh *fh = file->private_data;
1425 struct videobuf_buffer *buf = NULL;
1427 if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type)
1428 return videobuf_poll_stream(file, &fh->vbi, wait);
1430 if (res_check(fh,RESOURCE_VIDEO)) {
1431 if (!list_empty(&fh->cap.stream))
1432 buf = list_entry(fh->cap.stream.next, struct videobuf_buffer, stream);
1433 } else {
1434 mutex_lock(&fh->cap.vb_lock);
1435 if (UNSET == fh->cap.read_off) {
1436 /* need to capture a new frame */
1437 if (res_locked(fh->dev,RESOURCE_VIDEO))
1438 goto err;
1439 if (0 != fh->cap.ops->buf_prepare(&fh->cap,fh->cap.read_buf,fh->cap.field))
1440 goto err;
1441 fh->cap.ops->buf_queue(&fh->cap,fh->cap.read_buf);
1442 fh->cap.read_off = 0;
1444 mutex_unlock(&fh->cap.vb_lock);
1445 buf = fh->cap.read_buf;
1448 if (!buf)
1449 return POLLERR;
1451 poll_wait(file, &buf->done, wait);
1452 if (buf->state == VIDEOBUF_DONE ||
1453 buf->state == VIDEOBUF_ERROR)
1454 return POLLIN|POLLRDNORM;
1455 return 0;
1457 err:
1458 mutex_unlock(&fh->cap.vb_lock);
1459 return POLLERR;
1462 static int video_release(struct file *file)
1464 struct saa7134_fh *fh = file->private_data;
1465 struct saa7134_dev *dev = fh->dev;
1466 struct rds_command cmd;
1467 unsigned long flags;
1469 /* turn off overlay */
1470 if (res_check(fh, RESOURCE_OVERLAY)) {
1471 spin_lock_irqsave(&dev->slock,flags);
1472 stop_preview(dev,fh);
1473 spin_unlock_irqrestore(&dev->slock,flags);
1474 res_free(dev,fh,RESOURCE_OVERLAY);
1477 /* stop video capture */
1478 if (res_check(fh, RESOURCE_VIDEO)) {
1479 videobuf_streamoff(&fh->cap);
1480 res_free(dev,fh,RESOURCE_VIDEO);
1482 if (fh->cap.read_buf) {
1483 buffer_release(&fh->cap,fh->cap.read_buf);
1484 kfree(fh->cap.read_buf);
1487 /* stop vbi capture */
1488 if (res_check(fh, RESOURCE_VBI)) {
1489 videobuf_stop(&fh->vbi);
1490 res_free(dev,fh,RESOURCE_VBI);
1493 /* ts-capture will not work in planar mode, so turn it off Hac: 04.05*/
1494 saa_andorb(SAA7134_OFMT_VIDEO_A, 0x1f, 0);
1495 saa_andorb(SAA7134_OFMT_VIDEO_B, 0x1f, 0);
1496 saa_andorb(SAA7134_OFMT_DATA_A, 0x1f, 0);
1497 saa_andorb(SAA7134_OFMT_DATA_B, 0x1f, 0);
1499 saa_call_all(dev, tuner, s_standby);
1500 if (fh->radio)
1501 saa_call_all(dev, core, ioctl, RDS_CMD_CLOSE, &cmd);
1503 /* free stuff */
1504 videobuf_mmap_free(&fh->cap);
1505 videobuf_mmap_free(&fh->vbi);
1506 saa7134_pgtable_free(dev->pci,&fh->pt_cap);
1507 saa7134_pgtable_free(dev->pci,&fh->pt_vbi);
1509 v4l2_prio_close(&dev->prio,&fh->prio);
1510 file->private_data = NULL;
1511 kfree(fh);
1512 return 0;
1515 static int video_mmap(struct file *file, struct vm_area_struct * vma)
1517 struct saa7134_fh *fh = file->private_data;
1519 return videobuf_mmap_mapper(saa7134_queue(fh), vma);
1522 static ssize_t radio_read(struct file *file, char __user *data,
1523 size_t count, loff_t *ppos)
1525 struct saa7134_fh *fh = file->private_data;
1526 struct saa7134_dev *dev = fh->dev;
1527 struct rds_command cmd;
1529 cmd.block_count = count/3;
1530 cmd.buffer = data;
1531 cmd.instance = file;
1532 cmd.result = -ENODEV;
1534 saa_call_all(dev, core, ioctl, RDS_CMD_READ, &cmd);
1536 return cmd.result;
1539 static unsigned int radio_poll(struct file *file, poll_table *wait)
1541 struct saa7134_fh *fh = file->private_data;
1542 struct saa7134_dev *dev = fh->dev;
1543 struct rds_command cmd;
1545 cmd.instance = file;
1546 cmd.event_list = wait;
1547 cmd.result = -ENODEV;
1548 saa_call_all(dev, core, ioctl, RDS_CMD_POLL, &cmd);
1550 return cmd.result;
1553 /* ------------------------------------------------------------------ */
1555 static int saa7134_try_get_set_fmt_vbi_cap(struct file *file, void *priv,
1556 struct v4l2_format *f)
1558 struct saa7134_fh *fh = priv;
1559 struct saa7134_dev *dev = fh->dev;
1560 struct saa7134_tvnorm *norm = dev->tvnorm;
1562 f->fmt.vbi.sampling_rate = 6750000 * 4;
1563 f->fmt.vbi.samples_per_line = 2048 /* VBI_LINE_LENGTH */;
1564 f->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
1565 f->fmt.vbi.offset = 64 * 4;
1566 f->fmt.vbi.start[0] = norm->vbi_v_start_0;
1567 f->fmt.vbi.count[0] = norm->vbi_v_stop_0 - norm->vbi_v_start_0 +1;
1568 f->fmt.vbi.start[1] = norm->vbi_v_start_1;
1569 f->fmt.vbi.count[1] = f->fmt.vbi.count[0];
1570 f->fmt.vbi.flags = 0; /* VBI_UNSYNC VBI_INTERLACED */
1572 return 0;
1575 static int saa7134_g_fmt_vid_cap(struct file *file, void *priv,
1576 struct v4l2_format *f)
1578 struct saa7134_fh *fh = priv;
1580 f->fmt.pix.width = fh->width;
1581 f->fmt.pix.height = fh->height;
1582 f->fmt.pix.field = fh->cap.field;
1583 f->fmt.pix.pixelformat = fh->fmt->fourcc;
1584 f->fmt.pix.bytesperline =
1585 (f->fmt.pix.width * fh->fmt->depth) >> 3;
1586 f->fmt.pix.sizeimage =
1587 f->fmt.pix.height * f->fmt.pix.bytesperline;
1588 return 0;
1591 static int saa7134_g_fmt_vid_overlay(struct file *file, void *priv,
1592 struct v4l2_format *f)
1594 struct saa7134_fh *fh = priv;
1596 if (saa7134_no_overlay > 0) {
1597 printk(KERN_ERR "V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
1598 return -EINVAL;
1600 f->fmt.win = fh->win;
1602 return 0;
1605 static int saa7134_try_fmt_vid_cap(struct file *file, void *priv,
1606 struct v4l2_format *f)
1608 struct saa7134_fh *fh = priv;
1609 struct saa7134_dev *dev = fh->dev;
1610 struct saa7134_format *fmt;
1611 enum v4l2_field field;
1612 unsigned int maxw, maxh;
1614 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1615 if (NULL == fmt)
1616 return -EINVAL;
1618 field = f->fmt.pix.field;
1619 maxw = min(dev->crop_current.width*4, dev->crop_bounds.width);
1620 maxh = min(dev->crop_current.height*4, dev->crop_bounds.height);
1622 if (V4L2_FIELD_ANY == field) {
1623 field = (f->fmt.pix.height > maxh/2)
1624 ? V4L2_FIELD_INTERLACED
1625 : V4L2_FIELD_BOTTOM;
1627 switch (field) {
1628 case V4L2_FIELD_TOP:
1629 case V4L2_FIELD_BOTTOM:
1630 maxh = maxh / 2;
1631 break;
1632 case V4L2_FIELD_INTERLACED:
1633 break;
1634 default:
1635 return -EINVAL;
1638 f->fmt.pix.field = field;
1639 if (f->fmt.pix.width < 48)
1640 f->fmt.pix.width = 48;
1641 if (f->fmt.pix.height < 32)
1642 f->fmt.pix.height = 32;
1643 if (f->fmt.pix.width > maxw)
1644 f->fmt.pix.width = maxw;
1645 if (f->fmt.pix.height > maxh)
1646 f->fmt.pix.height = maxh;
1647 f->fmt.pix.width &= ~0x03;
1648 f->fmt.pix.bytesperline =
1649 (f->fmt.pix.width * fmt->depth) >> 3;
1650 f->fmt.pix.sizeimage =
1651 f->fmt.pix.height * f->fmt.pix.bytesperline;
1653 return 0;
1656 static int saa7134_try_fmt_vid_overlay(struct file *file, void *priv,
1657 struct v4l2_format *f)
1659 struct saa7134_fh *fh = priv;
1660 struct saa7134_dev *dev = fh->dev;
1662 if (saa7134_no_overlay > 0) {
1663 printk(KERN_ERR "V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
1664 return -EINVAL;
1667 return verify_preview(dev, &f->fmt.win);
1670 static int saa7134_s_fmt_vid_cap(struct file *file, void *priv,
1671 struct v4l2_format *f)
1673 struct saa7134_fh *fh = priv;
1674 int err;
1676 err = saa7134_try_fmt_vid_cap(file, priv, f);
1677 if (0 != err)
1678 return err;
1680 fh->fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1681 fh->width = f->fmt.pix.width;
1682 fh->height = f->fmt.pix.height;
1683 fh->cap.field = f->fmt.pix.field;
1684 return 0;
1687 static int saa7134_s_fmt_vid_overlay(struct file *file, void *priv,
1688 struct v4l2_format *f)
1690 struct saa7134_fh *fh = priv;
1691 struct saa7134_dev *dev = fh->dev;
1692 int err;
1693 unsigned long flags;
1695 if (saa7134_no_overlay > 0) {
1696 printk(KERN_ERR "V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
1697 return -EINVAL;
1699 err = verify_preview(dev, &f->fmt.win);
1700 if (0 != err)
1701 return err;
1703 mutex_lock(&dev->lock);
1705 fh->win = f->fmt.win;
1706 fh->nclips = f->fmt.win.clipcount;
1708 if (fh->nclips > 8)
1709 fh->nclips = 8;
1711 if (copy_from_user(fh->clips, f->fmt.win.clips,
1712 sizeof(struct v4l2_clip)*fh->nclips)) {
1713 mutex_unlock(&dev->lock);
1714 return -EFAULT;
1717 if (res_check(fh, RESOURCE_OVERLAY)) {
1718 spin_lock_irqsave(&dev->slock, flags);
1719 stop_preview(dev, fh);
1720 start_preview(dev, fh);
1721 spin_unlock_irqrestore(&dev->slock, flags);
1724 mutex_unlock(&dev->lock);
1725 return 0;
1728 int saa7134_queryctrl(struct file *file, void *priv, struct v4l2_queryctrl *c)
1730 const struct v4l2_queryctrl *ctrl;
1732 if ((c->id < V4L2_CID_BASE ||
1733 c->id >= V4L2_CID_LASTP1) &&
1734 (c->id < V4L2_CID_PRIVATE_BASE ||
1735 c->id >= V4L2_CID_PRIVATE_LASTP1))
1736 return -EINVAL;
1737 ctrl = ctrl_by_id(c->id);
1738 *c = (NULL != ctrl) ? *ctrl : no_ctrl;
1739 return 0;
1741 EXPORT_SYMBOL_GPL(saa7134_queryctrl);
1743 static int saa7134_enum_input(struct file *file, void *priv,
1744 struct v4l2_input *i)
1746 struct saa7134_fh *fh = priv;
1747 struct saa7134_dev *dev = fh->dev;
1748 unsigned int n;
1750 n = i->index;
1751 if (n >= SAA7134_INPUT_MAX)
1752 return -EINVAL;
1753 if (NULL == card_in(dev, i->index).name)
1754 return -EINVAL;
1755 memset(i, 0, sizeof(*i));
1756 i->index = n;
1757 i->type = V4L2_INPUT_TYPE_CAMERA;
1758 strcpy(i->name, card_in(dev, n).name);
1759 if (card_in(dev, n).tv)
1760 i->type = V4L2_INPUT_TYPE_TUNER;
1761 i->audioset = 1;
1762 if (n == dev->ctl_input) {
1763 int v1 = saa_readb(SAA7134_STATUS_VIDEO1);
1764 int v2 = saa_readb(SAA7134_STATUS_VIDEO2);
1766 if (0 != (v1 & 0x40))
1767 i->status |= V4L2_IN_ST_NO_H_LOCK;
1768 if (0 != (v2 & 0x40))
1769 i->status |= V4L2_IN_ST_NO_SYNC;
1770 if (0 != (v2 & 0x0e))
1771 i->status |= V4L2_IN_ST_MACROVISION;
1773 i->std = SAA7134_NORMS;
1774 return 0;
1777 static int saa7134_g_input(struct file *file, void *priv, unsigned int *i)
1779 struct saa7134_fh *fh = priv;
1780 struct saa7134_dev *dev = fh->dev;
1782 *i = dev->ctl_input;
1783 return 0;
1786 static int saa7134_s_input(struct file *file, void *priv, unsigned int i)
1788 struct saa7134_fh *fh = priv;
1789 struct saa7134_dev *dev = fh->dev;
1790 int err;
1792 err = v4l2_prio_check(&dev->prio, &fh->prio);
1793 if (0 != err)
1794 return err;
1796 if (i < 0 || i >= SAA7134_INPUT_MAX)
1797 return -EINVAL;
1798 if (NULL == card_in(dev, i).name)
1799 return -EINVAL;
1800 mutex_lock(&dev->lock);
1801 video_mux(dev, i);
1802 mutex_unlock(&dev->lock);
1803 return 0;
1806 static int saa7134_querycap(struct file *file, void *priv,
1807 struct v4l2_capability *cap)
1809 struct saa7134_fh *fh = priv;
1810 struct saa7134_dev *dev = fh->dev;
1812 unsigned int tuner_type = dev->tuner_type;
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 cap->version = SAA7134_VERSION_CODE;
1819 cap->capabilities =
1820 V4L2_CAP_VIDEO_CAPTURE |
1821 V4L2_CAP_VBI_CAPTURE |
1822 V4L2_CAP_READWRITE |
1823 V4L2_CAP_STREAMING |
1824 V4L2_CAP_TUNER;
1825 if (saa7134_no_overlay <= 0)
1826 cap->capabilities |= V4L2_CAP_VIDEO_OVERLAY;
1828 if ((tuner_type == TUNER_ABSENT) || (tuner_type == UNSET))
1829 cap->capabilities &= ~V4L2_CAP_TUNER;
1830 return 0;
1833 int saa7134_s_std_internal(struct saa7134_dev *dev, struct saa7134_fh *fh, v4l2_std_id *id)
1835 unsigned long flags;
1836 unsigned int i;
1837 v4l2_std_id fixup;
1838 int err;
1840 /* When called from the empress code fh == NULL.
1841 That needs to be fixed somehow, but for now this is
1842 good enough. */
1843 if (fh) {
1844 err = v4l2_prio_check(&dev->prio, &fh->prio);
1845 if (0 != err)
1846 return err;
1847 } else if (res_locked(dev, RESOURCE_OVERLAY)) {
1848 /* Don't change the std from the mpeg device
1849 if overlay is active. */
1850 return -EBUSY;
1853 for (i = 0; i < TVNORMS; i++)
1854 if (*id == tvnorms[i].id)
1855 break;
1857 if (i == TVNORMS)
1858 for (i = 0; i < TVNORMS; i++)
1859 if (*id & tvnorms[i].id)
1860 break;
1861 if (i == TVNORMS)
1862 return -EINVAL;
1864 if ((*id & V4L2_STD_SECAM) && (secam[0] != '-')) {
1865 if (secam[0] == 'L' || secam[0] == 'l') {
1866 if (secam[1] == 'C' || secam[1] == 'c')
1867 fixup = V4L2_STD_SECAM_LC;
1868 else
1869 fixup = V4L2_STD_SECAM_L;
1870 } else {
1871 if (secam[0] == 'D' || secam[0] == 'd')
1872 fixup = V4L2_STD_SECAM_DK;
1873 else
1874 fixup = V4L2_STD_SECAM;
1876 for (i = 0; i < TVNORMS; i++)
1877 if (fixup == tvnorms[i].id)
1878 break;
1881 *id = tvnorms[i].id;
1883 mutex_lock(&dev->lock);
1884 if (fh && res_check(fh, RESOURCE_OVERLAY)) {
1885 spin_lock_irqsave(&dev->slock, flags);
1886 stop_preview(dev, fh);
1887 spin_unlock_irqrestore(&dev->slock, flags);
1889 set_tvnorm(dev, &tvnorms[i]);
1891 spin_lock_irqsave(&dev->slock, flags);
1892 start_preview(dev, fh);
1893 spin_unlock_irqrestore(&dev->slock, flags);
1894 } else
1895 set_tvnorm(dev, &tvnorms[i]);
1897 saa7134_tvaudio_do_scan(dev);
1898 mutex_unlock(&dev->lock);
1899 return 0;
1901 EXPORT_SYMBOL_GPL(saa7134_s_std_internal);
1903 static int saa7134_s_std(struct file *file, void *priv, v4l2_std_id *id)
1905 struct saa7134_fh *fh = priv;
1907 return saa7134_s_std_internal(fh->dev, fh, id);
1910 static int saa7134_g_std(struct file *file, void *priv, v4l2_std_id *id)
1912 struct saa7134_fh *fh = priv;
1913 struct saa7134_dev *dev = fh->dev;
1915 *id = dev->tvnorm->id;
1916 return 0;
1919 static int saa7134_cropcap(struct file *file, void *priv,
1920 struct v4l2_cropcap *cap)
1922 struct saa7134_fh *fh = priv;
1923 struct saa7134_dev *dev = fh->dev;
1925 if (cap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1926 cap->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
1927 return -EINVAL;
1928 cap->bounds = dev->crop_bounds;
1929 cap->defrect = dev->crop_defrect;
1930 cap->pixelaspect.numerator = 1;
1931 cap->pixelaspect.denominator = 1;
1932 if (dev->tvnorm->id & V4L2_STD_525_60) {
1933 cap->pixelaspect.numerator = 11;
1934 cap->pixelaspect.denominator = 10;
1936 if (dev->tvnorm->id & V4L2_STD_625_50) {
1937 cap->pixelaspect.numerator = 54;
1938 cap->pixelaspect.denominator = 59;
1940 return 0;
1943 static int saa7134_g_crop(struct file *file, void *f, struct v4l2_crop *crop)
1945 struct saa7134_fh *fh = f;
1946 struct saa7134_dev *dev = fh->dev;
1948 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1949 crop->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
1950 return -EINVAL;
1951 crop->c = dev->crop_current;
1952 return 0;
1955 static int saa7134_s_crop(struct file *file, void *f, struct v4l2_crop *crop)
1957 struct saa7134_fh *fh = f;
1958 struct saa7134_dev *dev = fh->dev;
1959 struct v4l2_rect *b = &dev->crop_bounds;
1961 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1962 crop->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
1963 return -EINVAL;
1964 if (crop->c.height < 0)
1965 return -EINVAL;
1966 if (crop->c.width < 0)
1967 return -EINVAL;
1969 if (res_locked(fh->dev, RESOURCE_OVERLAY))
1970 return -EBUSY;
1971 if (res_locked(fh->dev, RESOURCE_VIDEO))
1972 return -EBUSY;
1974 if (crop->c.top < b->top)
1975 crop->c.top = b->top;
1976 if (crop->c.top > b->top + b->height)
1977 crop->c.top = b->top + b->height;
1978 if (crop->c.height > b->top - crop->c.top + b->height)
1979 crop->c.height = b->top - crop->c.top + b->height;
1981 if (crop->c.left < b->left)
1982 crop->c.left = b->left;
1983 if (crop->c.left > b->left + b->width)
1984 crop->c.left = b->left + b->width;
1985 if (crop->c.width > b->left - crop->c.left + b->width)
1986 crop->c.width = b->left - crop->c.left + b->width;
1988 dev->crop_current = crop->c;
1989 return 0;
1992 static int saa7134_g_tuner(struct file *file, void *priv,
1993 struct v4l2_tuner *t)
1995 struct saa7134_fh *fh = priv;
1996 struct saa7134_dev *dev = fh->dev;
1997 int n;
1999 if (0 != t->index)
2000 return -EINVAL;
2001 memset(t, 0, sizeof(*t));
2002 for (n = 0; n < SAA7134_INPUT_MAX; n++)
2003 if (card_in(dev, n).tv)
2004 break;
2005 if (NULL != card_in(dev, n).name) {
2006 strcpy(t->name, "Television");
2007 t->type = V4L2_TUNER_ANALOG_TV;
2008 t->capability = V4L2_TUNER_CAP_NORM |
2009 V4L2_TUNER_CAP_STEREO |
2010 V4L2_TUNER_CAP_LANG1 |
2011 V4L2_TUNER_CAP_LANG2;
2012 t->rangehigh = 0xffffffffUL;
2013 t->rxsubchans = saa7134_tvaudio_getstereo(dev);
2014 t->audmode = saa7134_tvaudio_rx2mode(t->rxsubchans);
2016 if (0 != (saa_readb(SAA7134_STATUS_VIDEO1) & 0x03))
2017 t->signal = 0xffff;
2018 return 0;
2021 static int saa7134_s_tuner(struct file *file, void *priv,
2022 struct v4l2_tuner *t)
2024 struct saa7134_fh *fh = priv;
2025 struct saa7134_dev *dev = fh->dev;
2026 int rx, mode, err;
2028 err = v4l2_prio_check(&dev->prio, &fh->prio);
2029 if (0 != err)
2030 return err;
2032 mode = dev->thread.mode;
2033 if (UNSET == mode) {
2034 rx = saa7134_tvaudio_getstereo(dev);
2035 mode = saa7134_tvaudio_rx2mode(t->rxsubchans);
2037 if (mode != t->audmode)
2038 dev->thread.mode = t->audmode;
2040 return 0;
2043 static int saa7134_g_frequency(struct file *file, void *priv,
2044 struct v4l2_frequency *f)
2046 struct saa7134_fh *fh = priv;
2047 struct saa7134_dev *dev = fh->dev;
2049 f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
2050 f->frequency = dev->ctl_freq;
2052 return 0;
2055 static int saa7134_s_frequency(struct file *file, void *priv,
2056 struct v4l2_frequency *f)
2058 struct saa7134_fh *fh = priv;
2059 struct saa7134_dev *dev = fh->dev;
2060 int err;
2062 err = v4l2_prio_check(&dev->prio, &fh->prio);
2063 if (0 != err)
2064 return err;
2066 if (0 != f->tuner)
2067 return -EINVAL;
2068 if (0 == fh->radio && V4L2_TUNER_ANALOG_TV != f->type)
2069 return -EINVAL;
2070 if (1 == fh->radio && V4L2_TUNER_RADIO != f->type)
2071 return -EINVAL;
2072 mutex_lock(&dev->lock);
2073 dev->ctl_freq = f->frequency;
2075 saa_call_all(dev, tuner, s_frequency, f);
2077 saa7134_tvaudio_do_scan(dev);
2078 mutex_unlock(&dev->lock);
2079 return 0;
2082 static int saa7134_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
2084 strcpy(a->name, "audio");
2085 return 0;
2088 static int saa7134_s_audio(struct file *file, void *priv, struct v4l2_audio *a)
2090 return 0;
2093 static int saa7134_g_priority(struct file *file, void *f, enum v4l2_priority *p)
2095 struct saa7134_fh *fh = f;
2096 struct saa7134_dev *dev = fh->dev;
2098 *p = v4l2_prio_max(&dev->prio);
2099 return 0;
2102 static int saa7134_s_priority(struct file *file, void *f,
2103 enum v4l2_priority prio)
2105 struct saa7134_fh *fh = f;
2106 struct saa7134_dev *dev = fh->dev;
2108 return v4l2_prio_change(&dev->prio, &fh->prio, prio);
2111 static int saa7134_enum_fmt_vid_cap(struct file *file, void *priv,
2112 struct v4l2_fmtdesc *f)
2114 if (f->index >= FORMATS)
2115 return -EINVAL;
2117 strlcpy(f->description, formats[f->index].name,
2118 sizeof(f->description));
2120 f->pixelformat = formats[f->index].fourcc;
2122 return 0;
2125 static int saa7134_enum_fmt_vid_overlay(struct file *file, void *priv,
2126 struct v4l2_fmtdesc *f)
2128 if (saa7134_no_overlay > 0) {
2129 printk(KERN_ERR "V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
2130 return -EINVAL;
2133 if ((f->index >= FORMATS) || formats[f->index].planar)
2134 return -EINVAL;
2136 strlcpy(f->description, formats[f->index].name,
2137 sizeof(f->description));
2139 f->pixelformat = formats[f->index].fourcc;
2141 return 0;
2144 static int saa7134_g_fbuf(struct file *file, void *f,
2145 struct v4l2_framebuffer *fb)
2147 struct saa7134_fh *fh = f;
2148 struct saa7134_dev *dev = fh->dev;
2150 *fb = dev->ovbuf;
2151 fb->capability = V4L2_FBUF_CAP_LIST_CLIPPING;
2153 return 0;
2156 static int saa7134_s_fbuf(struct file *file, void *f,
2157 struct v4l2_framebuffer *fb)
2159 struct saa7134_fh *fh = f;
2160 struct saa7134_dev *dev = fh->dev;
2161 struct saa7134_format *fmt;
2163 if (!capable(CAP_SYS_ADMIN) &&
2164 !capable(CAP_SYS_RAWIO))
2165 return -EPERM;
2167 /* check args */
2168 fmt = format_by_fourcc(fb->fmt.pixelformat);
2169 if (NULL == fmt)
2170 return -EINVAL;
2172 /* ok, accept it */
2173 dev->ovbuf = *fb;
2174 dev->ovfmt = fmt;
2175 if (0 == dev->ovbuf.fmt.bytesperline)
2176 dev->ovbuf.fmt.bytesperline =
2177 dev->ovbuf.fmt.width*fmt->depth/8;
2178 return 0;
2181 static int saa7134_overlay(struct file *file, void *f, unsigned int on)
2183 struct saa7134_fh *fh = f;
2184 struct saa7134_dev *dev = fh->dev;
2185 unsigned long flags;
2187 if (on) {
2188 if (saa7134_no_overlay > 0) {
2189 dprintk("no_overlay\n");
2190 return -EINVAL;
2193 if (!res_get(dev, fh, RESOURCE_OVERLAY))
2194 return -EBUSY;
2195 spin_lock_irqsave(&dev->slock, flags);
2196 start_preview(dev, fh);
2197 spin_unlock_irqrestore(&dev->slock, flags);
2199 if (!on) {
2200 if (!res_check(fh, RESOURCE_OVERLAY))
2201 return -EINVAL;
2202 spin_lock_irqsave(&dev->slock, flags);
2203 stop_preview(dev, fh);
2204 spin_unlock_irqrestore(&dev->slock, flags);
2205 res_free(dev, fh, RESOURCE_OVERLAY);
2207 return 0;
2210 #ifdef CONFIG_VIDEO_V4L1_COMPAT
2211 static int vidiocgmbuf(struct file *file, void *priv, struct video_mbuf *mbuf)
2213 struct saa7134_fh *fh = file->private_data;
2214 return videobuf_cgmbuf(saa7134_queue(fh), mbuf, 8);
2216 #endif
2218 static int saa7134_reqbufs(struct file *file, void *priv,
2219 struct v4l2_requestbuffers *p)
2221 struct saa7134_fh *fh = priv;
2222 return videobuf_reqbufs(saa7134_queue(fh), p);
2225 static int saa7134_querybuf(struct file *file, void *priv,
2226 struct v4l2_buffer *b)
2228 struct saa7134_fh *fh = priv;
2229 return videobuf_querybuf(saa7134_queue(fh), b);
2232 static int saa7134_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
2234 struct saa7134_fh *fh = priv;
2235 return videobuf_qbuf(saa7134_queue(fh), b);
2238 static int saa7134_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
2240 struct saa7134_fh *fh = priv;
2241 return videobuf_dqbuf(saa7134_queue(fh), b,
2242 file->f_flags & O_NONBLOCK);
2245 static int saa7134_streamon(struct file *file, void *priv,
2246 enum v4l2_buf_type type)
2248 struct saa7134_fh *fh = priv;
2249 struct saa7134_dev *dev = fh->dev;
2250 int res = saa7134_resource(fh);
2252 if (!res_get(dev, fh, res))
2253 return -EBUSY;
2255 return videobuf_streamon(saa7134_queue(fh));
2258 static int saa7134_streamoff(struct file *file, void *priv,
2259 enum v4l2_buf_type type)
2261 int err;
2262 struct saa7134_fh *fh = priv;
2263 struct saa7134_dev *dev = fh->dev;
2264 int res = saa7134_resource(fh);
2266 err = videobuf_streamoff(saa7134_queue(fh));
2267 if (err < 0)
2268 return err;
2269 res_free(dev, fh, res);
2270 return 0;
2273 static int saa7134_g_parm(struct file *file, void *fh,
2274 struct v4l2_streamparm *parm)
2276 return 0;
2279 #ifdef CONFIG_VIDEO_ADV_DEBUG
2280 static int vidioc_g_register (struct file *file, void *priv,
2281 struct v4l2_dbg_register *reg)
2283 struct saa7134_fh *fh = priv;
2284 struct saa7134_dev *dev = fh->dev;
2286 if (!v4l2_chip_match_host(&reg->match))
2287 return -EINVAL;
2288 reg->val = saa_readb(reg->reg);
2289 reg->size = 1;
2290 return 0;
2293 static int vidioc_s_register (struct file *file, void *priv,
2294 struct v4l2_dbg_register *reg)
2296 struct saa7134_fh *fh = priv;
2297 struct saa7134_dev *dev = fh->dev;
2299 if (!v4l2_chip_match_host(&reg->match))
2300 return -EINVAL;
2301 saa_writeb(reg->reg&0xffffff, reg->val);
2302 return 0;
2304 #endif
2306 static int radio_querycap(struct file *file, void *priv,
2307 struct v4l2_capability *cap)
2309 struct saa7134_fh *fh = file->private_data;
2310 struct saa7134_dev *dev = fh->dev;
2312 strcpy(cap->driver, "saa7134");
2313 strlcpy(cap->card, saa7134_boards[dev->board].name, sizeof(cap->card));
2314 sprintf(cap->bus_info, "PCI:%s", pci_name(dev->pci));
2315 cap->version = SAA7134_VERSION_CODE;
2316 cap->capabilities = V4L2_CAP_TUNER;
2317 return 0;
2320 static int radio_g_tuner(struct file *file, void *priv,
2321 struct v4l2_tuner *t)
2323 struct saa7134_fh *fh = file->private_data;
2324 struct saa7134_dev *dev = fh->dev;
2326 if (0 != t->index)
2327 return -EINVAL;
2329 memset(t, 0, sizeof(*t));
2330 strcpy(t->name, "Radio");
2331 t->type = V4L2_TUNER_RADIO;
2333 saa_call_all(dev, tuner, g_tuner, t);
2334 if (dev->input->amux == TV) {
2335 t->signal = 0xf800 - ((saa_readb(0x581) & 0x1f) << 11);
2336 t->rxsubchans = (saa_readb(0x529) & 0x08) ?
2337 V4L2_TUNER_SUB_STEREO : V4L2_TUNER_SUB_MONO;
2339 return 0;
2341 static int radio_s_tuner(struct file *file, void *priv,
2342 struct v4l2_tuner *t)
2344 struct saa7134_fh *fh = file->private_data;
2345 struct saa7134_dev *dev = fh->dev;
2347 if (0 != t->index)
2348 return -EINVAL;
2350 saa_call_all(dev, tuner, s_tuner, t);
2351 return 0;
2354 static int radio_enum_input(struct file *file, void *priv,
2355 struct v4l2_input *i)
2357 if (i->index != 0)
2358 return -EINVAL;
2360 strcpy(i->name, "Radio");
2361 i->type = V4L2_INPUT_TYPE_TUNER;
2363 return 0;
2366 static int radio_g_input(struct file *filp, void *priv, unsigned int *i)
2368 *i = 0;
2369 return 0;
2372 static int radio_g_audio(struct file *file, void *priv,
2373 struct v4l2_audio *a)
2375 memset(a, 0, sizeof(*a));
2376 strcpy(a->name, "Radio");
2377 return 0;
2380 static int radio_s_audio(struct file *file, void *priv,
2381 struct v4l2_audio *a)
2383 return 0;
2386 static int radio_s_input(struct file *filp, void *priv, unsigned int i)
2388 return 0;
2391 static int radio_s_std(struct file *file, void *fh, v4l2_std_id *norm)
2393 return 0;
2396 static int radio_queryctrl(struct file *file, void *priv,
2397 struct v4l2_queryctrl *c)
2399 const struct v4l2_queryctrl *ctrl;
2401 if (c->id < V4L2_CID_BASE ||
2402 c->id >= V4L2_CID_LASTP1)
2403 return -EINVAL;
2404 if (c->id == V4L2_CID_AUDIO_MUTE) {
2405 ctrl = ctrl_by_id(c->id);
2406 *c = *ctrl;
2407 } else
2408 *c = no_ctrl;
2409 return 0;
2412 static const struct v4l2_file_operations video_fops =
2414 .owner = THIS_MODULE,
2415 .open = video_open,
2416 .release = video_release,
2417 .read = video_read,
2418 .poll = video_poll,
2419 .mmap = video_mmap,
2420 .ioctl = video_ioctl2,
2423 static const struct v4l2_ioctl_ops video_ioctl_ops = {
2424 .vidioc_querycap = saa7134_querycap,
2425 .vidioc_enum_fmt_vid_cap = saa7134_enum_fmt_vid_cap,
2426 .vidioc_g_fmt_vid_cap = saa7134_g_fmt_vid_cap,
2427 .vidioc_try_fmt_vid_cap = saa7134_try_fmt_vid_cap,
2428 .vidioc_s_fmt_vid_cap = saa7134_s_fmt_vid_cap,
2429 .vidioc_enum_fmt_vid_overlay = saa7134_enum_fmt_vid_overlay,
2430 .vidioc_g_fmt_vid_overlay = saa7134_g_fmt_vid_overlay,
2431 .vidioc_try_fmt_vid_overlay = saa7134_try_fmt_vid_overlay,
2432 .vidioc_s_fmt_vid_overlay = saa7134_s_fmt_vid_overlay,
2433 .vidioc_g_fmt_vbi_cap = saa7134_try_get_set_fmt_vbi_cap,
2434 .vidioc_try_fmt_vbi_cap = saa7134_try_get_set_fmt_vbi_cap,
2435 .vidioc_s_fmt_vbi_cap = saa7134_try_get_set_fmt_vbi_cap,
2436 .vidioc_g_audio = saa7134_g_audio,
2437 .vidioc_s_audio = saa7134_s_audio,
2438 .vidioc_cropcap = saa7134_cropcap,
2439 .vidioc_reqbufs = saa7134_reqbufs,
2440 .vidioc_querybuf = saa7134_querybuf,
2441 .vidioc_qbuf = saa7134_qbuf,
2442 .vidioc_dqbuf = saa7134_dqbuf,
2443 .vidioc_s_std = saa7134_s_std,
2444 .vidioc_g_std = saa7134_g_std,
2445 .vidioc_enum_input = saa7134_enum_input,
2446 .vidioc_g_input = saa7134_g_input,
2447 .vidioc_s_input = saa7134_s_input,
2448 .vidioc_queryctrl = saa7134_queryctrl,
2449 .vidioc_g_ctrl = saa7134_g_ctrl,
2450 .vidioc_s_ctrl = saa7134_s_ctrl,
2451 .vidioc_streamon = saa7134_streamon,
2452 .vidioc_streamoff = saa7134_streamoff,
2453 .vidioc_g_tuner = saa7134_g_tuner,
2454 .vidioc_s_tuner = saa7134_s_tuner,
2455 #ifdef CONFIG_VIDEO_V4L1_COMPAT
2456 .vidiocgmbuf = vidiocgmbuf,
2457 #endif
2458 .vidioc_g_crop = saa7134_g_crop,
2459 .vidioc_s_crop = saa7134_s_crop,
2460 .vidioc_g_fbuf = saa7134_g_fbuf,
2461 .vidioc_s_fbuf = saa7134_s_fbuf,
2462 .vidioc_overlay = saa7134_overlay,
2463 .vidioc_g_priority = saa7134_g_priority,
2464 .vidioc_s_priority = saa7134_s_priority,
2465 .vidioc_g_parm = saa7134_g_parm,
2466 .vidioc_g_frequency = saa7134_g_frequency,
2467 .vidioc_s_frequency = saa7134_s_frequency,
2468 #ifdef CONFIG_VIDEO_ADV_DEBUG
2469 .vidioc_g_register = vidioc_g_register,
2470 .vidioc_s_register = vidioc_s_register,
2471 #endif
2474 static const struct v4l2_file_operations radio_fops = {
2475 .owner = THIS_MODULE,
2476 .open = video_open,
2477 .read = radio_read,
2478 .release = video_release,
2479 .ioctl = video_ioctl2,
2480 .poll = radio_poll,
2483 static const struct v4l2_ioctl_ops radio_ioctl_ops = {
2484 .vidioc_querycap = radio_querycap,
2485 .vidioc_g_tuner = radio_g_tuner,
2486 .vidioc_enum_input = radio_enum_input,
2487 .vidioc_g_audio = radio_g_audio,
2488 .vidioc_s_tuner = radio_s_tuner,
2489 .vidioc_s_audio = radio_s_audio,
2490 .vidioc_s_input = radio_s_input,
2491 .vidioc_s_std = radio_s_std,
2492 .vidioc_queryctrl = radio_queryctrl,
2493 .vidioc_g_input = radio_g_input,
2494 .vidioc_g_ctrl = saa7134_g_ctrl,
2495 .vidioc_s_ctrl = saa7134_s_ctrl,
2496 .vidioc_g_frequency = saa7134_g_frequency,
2497 .vidioc_s_frequency = saa7134_s_frequency,
2500 /* ----------------------------------------------------------- */
2501 /* exported stuff */
2503 struct video_device saa7134_video_template = {
2504 .name = "saa7134-video",
2505 .fops = &video_fops,
2506 .ioctl_ops = &video_ioctl_ops,
2507 .minor = -1,
2508 .tvnorms = SAA7134_NORMS,
2509 .current_norm = V4L2_STD_PAL,
2512 struct video_device saa7134_radio_template = {
2513 .name = "saa7134-radio",
2514 .fops = &radio_fops,
2515 .ioctl_ops = &radio_ioctl_ops,
2516 .minor = -1,
2519 int saa7134_video_init1(struct saa7134_dev *dev)
2521 /* sanitycheck insmod options */
2522 if (gbuffers < 2 || gbuffers > VIDEO_MAX_FRAME)
2523 gbuffers = 2;
2524 if (gbufsize < 0 || gbufsize > gbufsize_max)
2525 gbufsize = gbufsize_max;
2526 gbufsize = (gbufsize + PAGE_SIZE - 1) & PAGE_MASK;
2528 /* put some sensible defaults into the data structures ... */
2529 dev->ctl_bright = ctrl_by_id(V4L2_CID_BRIGHTNESS)->default_value;
2530 dev->ctl_contrast = ctrl_by_id(V4L2_CID_CONTRAST)->default_value;
2531 dev->ctl_hue = ctrl_by_id(V4L2_CID_HUE)->default_value;
2532 dev->ctl_saturation = ctrl_by_id(V4L2_CID_SATURATION)->default_value;
2533 dev->ctl_volume = ctrl_by_id(V4L2_CID_AUDIO_VOLUME)->default_value;
2534 dev->ctl_mute = 1; // ctrl_by_id(V4L2_CID_AUDIO_MUTE)->default_value;
2535 dev->ctl_invert = ctrl_by_id(V4L2_CID_PRIVATE_INVERT)->default_value;
2536 dev->ctl_automute = ctrl_by_id(V4L2_CID_PRIVATE_AUTOMUTE)->default_value;
2538 if (dev->tda9887_conf && dev->ctl_automute)
2539 dev->tda9887_conf |= TDA9887_AUTOMUTE;
2540 dev->automute = 0;
2542 INIT_LIST_HEAD(&dev->video_q.queue);
2543 init_timer(&dev->video_q.timeout);
2544 dev->video_q.timeout.function = saa7134_buffer_timeout;
2545 dev->video_q.timeout.data = (unsigned long)(&dev->video_q);
2546 dev->video_q.dev = dev;
2548 if (saa7134_boards[dev->board].video_out)
2549 saa7134_videoport_init(dev);
2551 return 0;
2554 int saa7134_videoport_init(struct saa7134_dev *dev)
2556 /* enable video output */
2557 int vo = saa7134_boards[dev->board].video_out;
2558 int video_reg;
2559 unsigned int vid_port_opts = saa7134_boards[dev->board].vid_port_opts;
2561 /* Configure videoport */
2562 saa_writeb(SAA7134_VIDEO_PORT_CTRL0, video_out[vo][0]);
2563 video_reg = video_out[vo][1];
2564 if (vid_port_opts & SET_T_CODE_POLARITY_NON_INVERTED)
2565 video_reg &= ~VP_T_CODE_P_INVERTED;
2566 saa_writeb(SAA7134_VIDEO_PORT_CTRL1, video_reg);
2567 saa_writeb(SAA7134_VIDEO_PORT_CTRL2, video_out[vo][2]);
2568 saa_writeb(SAA7134_VIDEO_PORT_CTRL4, video_out[vo][4]);
2569 video_reg = video_out[vo][5];
2570 if (vid_port_opts & SET_CLOCK_NOT_DELAYED)
2571 video_reg &= ~VP_CLK_CTRL2_DELAYED;
2572 if (vid_port_opts & SET_CLOCK_INVERTED)
2573 video_reg |= VP_CLK_CTRL1_INVERTED;
2574 saa_writeb(SAA7134_VIDEO_PORT_CTRL5, video_reg);
2575 video_reg = video_out[vo][6];
2576 if (vid_port_opts & SET_VSYNC_OFF) {
2577 video_reg &= ~VP_VS_TYPE_MASK;
2578 video_reg |= VP_VS_TYPE_OFF;
2580 saa_writeb(SAA7134_VIDEO_PORT_CTRL6, video_reg);
2581 saa_writeb(SAA7134_VIDEO_PORT_CTRL7, video_out[vo][7]);
2582 saa_writeb(SAA7134_VIDEO_PORT_CTRL8, video_out[vo][8]);
2584 /* Start videoport */
2585 saa_writeb(SAA7134_VIDEO_PORT_CTRL3, video_out[vo][3]);
2587 return 0;
2590 int saa7134_video_init2(struct saa7134_dev *dev)
2592 /* init video hw */
2593 set_tvnorm(dev,&tvnorms[0]);
2594 video_mux(dev,0);
2595 saa7134_tvaudio_setmute(dev);
2596 saa7134_tvaudio_setvolume(dev,dev->ctl_volume);
2597 return 0;
2600 void saa7134_irq_video_signalchange(struct saa7134_dev *dev)
2602 static const char *st[] = {
2603 "(no signal)", "NTSC", "PAL", "SECAM" };
2604 u32 st1,st2;
2606 st1 = saa_readb(SAA7134_STATUS_VIDEO1);
2607 st2 = saa_readb(SAA7134_STATUS_VIDEO2);
2608 dprintk("DCSDT: pll: %s, sync: %s, norm: %s\n",
2609 (st1 & 0x40) ? "not locked" : "locked",
2610 (st2 & 0x40) ? "no" : "yes",
2611 st[st1 & 0x03]);
2612 dev->nosignal = (st1 & 0x40) || (st2 & 0x40) || !(st2 & 0x1);
2614 if (dev->nosignal) {
2615 /* no video signal -> mute audio */
2616 if (dev->ctl_automute)
2617 dev->automute = 1;
2618 saa7134_tvaudio_setmute(dev);
2619 } else {
2620 /* wake up tvaudio audio carrier scan thread */
2621 saa7134_tvaudio_do_scan(dev);
2624 if ((st2 & 0x80) && !noninterlaced && !dev->nosignal)
2625 saa_clearb(SAA7134_SYNC_CTRL, 0x20);
2626 else
2627 saa_setb(SAA7134_SYNC_CTRL, 0x20);
2629 if (dev->mops && dev->mops->signal_change)
2630 dev->mops->signal_change(dev);
2634 void saa7134_irq_video_done(struct saa7134_dev *dev, unsigned long status)
2636 enum v4l2_field field;
2638 spin_lock(&dev->slock);
2639 if (dev->video_q.curr) {
2640 dev->video_fieldcount++;
2641 field = dev->video_q.curr->vb.field;
2642 if (V4L2_FIELD_HAS_BOTH(field)) {
2643 /* make sure we have seen both fields */
2644 if ((status & 0x10) == 0x00) {
2645 dev->video_q.curr->top_seen = 1;
2646 goto done;
2648 if (!dev->video_q.curr->top_seen)
2649 goto done;
2650 } else if (field == V4L2_FIELD_TOP) {
2651 if ((status & 0x10) != 0x10)
2652 goto done;
2653 } else if (field == V4L2_FIELD_BOTTOM) {
2654 if ((status & 0x10) != 0x00)
2655 goto done;
2657 dev->video_q.curr->vb.field_count = dev->video_fieldcount;
2658 saa7134_buffer_finish(dev,&dev->video_q,VIDEOBUF_DONE);
2660 saa7134_buffer_next(dev,&dev->video_q);
2662 done:
2663 spin_unlock(&dev->slock);
2666 /* ----------------------------------------------------------- */
2668 * Local variables:
2669 * c-basic-offset: 8
2670 * End: