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"
32 #include <media/v4l2-common.h>
34 #ifdef CONFIG_VIDEO_V4L1_COMPAT
35 /* Include V4L1 specific functions. Should be removed soon */
36 #include <linux/videodev.h>
39 /* ------------------------------------------------------------------ */
41 static unsigned int video_debug = 0;
42 static unsigned int gbuffers = 8;
43 static unsigned int noninterlaced = 1;
44 static unsigned int gbufsize = 720*576*4;
45 static unsigned int gbufsize_max = 720*576*4;
46 static char secam[] = "--";
47 module_param(video_debug, int, 0644);
48 MODULE_PARM_DESC(video_debug,"enable debug messages [video]");
49 module_param(gbuffers, int, 0444);
50 MODULE_PARM_DESC(gbuffers,"number of capture buffers, range 2-32");
51 module_param(noninterlaced, int, 0644);
52 MODULE_PARM_DESC(noninterlaced,"capture non interlaced video");
53 module_param_string(secam, secam, sizeof(secam), 0644);
54 MODULE_PARM_DESC(secam, "force SECAM variant, either DK,L or Lc");
57 #define dprintk(fmt, arg...) if (video_debug) \
58 printk(KERN_DEBUG "%s/video: " fmt, dev->name , ## arg)
60 /* ------------------------------------------------------------------ */
61 /* Defines for Video Output Port Register at address 0x191 */
63 /* Bit 0: VIP code T bit polarity */
65 #define VP_T_CODE_P_NON_INVERTED 0x00
66 #define VP_T_CODE_P_INVERTED 0x01
68 /* ------------------------------------------------------------------ */
69 /* Defines for Video Output Port Register at address 0x195 */
71 /* Bit 2: Video output clock delay control */
73 #define VP_CLK_CTRL2_NOT_DELAYED 0x00
74 #define VP_CLK_CTRL2_DELAYED 0x04
76 /* Bit 1: Video output clock invert control */
78 #define VP_CLK_CTRL1_NON_INVERTED 0x00
79 #define VP_CLK_CTRL1_INVERTED 0x02
81 /* ------------------------------------------------------------------ */
82 /* Defines for Video Output Port Register at address 0x196 */
84 /* Bits 2 to 0: VSYNC pin video vertical sync type */
86 #define VP_VS_TYPE_MASK 0x07
88 #define VP_VS_TYPE_OFF 0x00
89 #define VP_VS_TYPE_V123 0x01
90 #define VP_VS_TYPE_V_ITU 0x02
91 #define VP_VS_TYPE_VGATE_L 0x03
92 #define VP_VS_TYPE_RESERVED1 0x04
93 #define VP_VS_TYPE_RESERVED2 0x05
94 #define VP_VS_TYPE_F_ITU 0x06
95 #define VP_VS_TYPE_SC_FID 0x07
97 /* ------------------------------------------------------------------ */
98 /* data structs for video */
100 static int video_out[][9] = {
101 [CCIR656] = { 0x00, 0xb1, 0x00, 0xa1, 0x00, 0x04, 0x06, 0x00, 0x00 },
104 static struct saa7134_format formats[] = {
106 .name = "8 bpp gray",
107 .fourcc = V4L2_PIX_FMT_GREY,
111 .name = "15 bpp RGB, le",
112 .fourcc = V4L2_PIX_FMT_RGB555,
116 .name = "15 bpp RGB, be",
117 .fourcc = V4L2_PIX_FMT_RGB555X,
122 .name = "16 bpp RGB, le",
123 .fourcc = V4L2_PIX_FMT_RGB565,
127 .name = "16 bpp RGB, be",
128 .fourcc = V4L2_PIX_FMT_RGB565X,
133 .name = "24 bpp RGB, le",
134 .fourcc = V4L2_PIX_FMT_BGR24,
138 .name = "24 bpp RGB, be",
139 .fourcc = V4L2_PIX_FMT_RGB24,
144 .name = "32 bpp RGB, le",
145 .fourcc = V4L2_PIX_FMT_BGR32,
149 .name = "32 bpp RGB, be",
150 .fourcc = V4L2_PIX_FMT_RGB32,
156 .name = "4:2:2 packed, YUYV",
157 .fourcc = V4L2_PIX_FMT_YUYV,
163 .name = "4:2:2 packed, UYVY",
164 .fourcc = V4L2_PIX_FMT_UYVY,
169 .name = "4:2:2 planar, Y-Cb-Cr",
170 .fourcc = V4L2_PIX_FMT_YUV422P,
178 .name = "4:2:0 planar, Y-Cb-Cr",
179 .fourcc = V4L2_PIX_FMT_YUV420,
187 .name = "4:2:0 planar, Y-Cb-Cr",
188 .fourcc = V4L2_PIX_FMT_YVU420,
198 #define FORMATS ARRAY_SIZE(formats)
200 #define NORM_625_50 \
203 .video_v_start = 24, \
204 .video_v_stop = 311, \
205 .vbi_v_start_0 = 7, \
206 .vbi_v_stop_0 = 22, \
207 .vbi_v_start_1 = 319, \
210 #define NORM_525_60 \
213 .video_v_start = 23, \
214 .video_v_stop = 262, \
215 .vbi_v_start_0 = 10, \
216 .vbi_v_stop_0 = 21, \
217 .vbi_v_start_1 = 273, \
220 static struct saa7134_tvnorm tvnorms[] = {
222 .name = "PAL", /* autodetect */
226 .sync_control = 0x18,
227 .luma_control = 0x40,
228 .chroma_ctrl1 = 0x81,
230 .chroma_ctrl2 = 0x06,
235 .id = V4L2_STD_PAL_BG,
238 .sync_control = 0x18,
239 .luma_control = 0x40,
240 .chroma_ctrl1 = 0x81,
242 .chroma_ctrl2 = 0x06,
247 .id = V4L2_STD_PAL_I,
250 .sync_control = 0x18,
251 .luma_control = 0x40,
252 .chroma_ctrl1 = 0x81,
254 .chroma_ctrl2 = 0x06,
259 .id = V4L2_STD_PAL_DK,
262 .sync_control = 0x18,
263 .luma_control = 0x40,
264 .chroma_ctrl1 = 0x81,
266 .chroma_ctrl2 = 0x06,
274 .sync_control = 0x59,
275 .luma_control = 0x40,
276 .chroma_ctrl1 = 0x89,
278 .chroma_ctrl2 = 0x0e,
283 .id = V4L2_STD_SECAM,
286 .sync_control = 0x18,
287 .luma_control = 0x1b,
288 .chroma_ctrl1 = 0xd1,
290 .chroma_ctrl2 = 0x00,
295 .id = V4L2_STD_SECAM_DK,
298 .sync_control = 0x18,
299 .luma_control = 0x1b,
300 .chroma_ctrl1 = 0xd1,
302 .chroma_ctrl2 = 0x00,
307 .id = V4L2_STD_SECAM_L,
310 .sync_control = 0x18,
311 .luma_control = 0x1b,
312 .chroma_ctrl1 = 0xd1,
314 .chroma_ctrl2 = 0x00,
319 .id = V4L2_STD_SECAM_LC,
322 .sync_control = 0x18,
323 .luma_control = 0x1b,
324 .chroma_ctrl1 = 0xd1,
326 .chroma_ctrl2 = 0x00,
331 .id = V4L2_STD_PAL_M,
334 .sync_control = 0x59,
335 .luma_control = 0x40,
336 .chroma_ctrl1 = 0xb9,
338 .chroma_ctrl2 = 0x0e,
343 .id = V4L2_STD_PAL_Nc,
346 .sync_control = 0x18,
347 .luma_control = 0x40,
348 .chroma_ctrl1 = 0xa1,
350 .chroma_ctrl2 = 0x06,
355 .id = V4L2_STD_PAL_60,
363 .vbi_v_start_1 = 273,
366 .sync_control = 0x18,
367 .luma_control = 0x40,
368 .chroma_ctrl1 = 0x81,
370 .chroma_ctrl2 = 0x06,
374 #define TVNORMS ARRAY_SIZE(tvnorms)
376 #define V4L2_CID_PRIVATE_INVERT (V4L2_CID_PRIVATE_BASE + 0)
377 #define V4L2_CID_PRIVATE_Y_ODD (V4L2_CID_PRIVATE_BASE + 1)
378 #define V4L2_CID_PRIVATE_Y_EVEN (V4L2_CID_PRIVATE_BASE + 2)
379 #define V4L2_CID_PRIVATE_AUTOMUTE (V4L2_CID_PRIVATE_BASE + 3)
380 #define V4L2_CID_PRIVATE_LASTP1 (V4L2_CID_PRIVATE_BASE + 4)
382 static const struct v4l2_queryctrl no_ctrl = {
384 .flags = V4L2_CTRL_FLAG_DISABLED,
386 static const struct v4l2_queryctrl video_ctrls[] = {
389 .id = V4L2_CID_BRIGHTNESS,
390 .name = "Brightness",
394 .default_value = 128,
395 .type = V4L2_CTRL_TYPE_INTEGER,
397 .id = V4L2_CID_CONTRAST,
403 .type = V4L2_CTRL_TYPE_INTEGER,
405 .id = V4L2_CID_SATURATION,
406 .name = "Saturation",
411 .type = V4L2_CTRL_TYPE_INTEGER,
419 .type = V4L2_CTRL_TYPE_INTEGER,
421 .id = V4L2_CID_HFLIP,
425 .type = V4L2_CTRL_TYPE_BOOLEAN,
429 .id = V4L2_CID_AUDIO_MUTE,
433 .type = V4L2_CTRL_TYPE_BOOLEAN,
435 .id = V4L2_CID_AUDIO_VOLUME,
441 .type = V4L2_CTRL_TYPE_INTEGER,
443 /* --- private --- */
445 .id = V4L2_CID_PRIVATE_INVERT,
449 .type = V4L2_CTRL_TYPE_BOOLEAN,
451 .id = V4L2_CID_PRIVATE_Y_ODD,
452 .name = "y offset odd field",
456 .type = V4L2_CTRL_TYPE_INTEGER,
458 .id = V4L2_CID_PRIVATE_Y_EVEN,
459 .name = "y offset even field",
463 .type = V4L2_CTRL_TYPE_INTEGER,
465 .id = V4L2_CID_PRIVATE_AUTOMUTE,
470 .type = V4L2_CTRL_TYPE_BOOLEAN,
473 static const unsigned int CTRLS = ARRAY_SIZE(video_ctrls);
475 static const struct v4l2_queryctrl* ctrl_by_id(unsigned int id)
479 for (i = 0; i < CTRLS; i++)
480 if (video_ctrls[i].id == id)
481 return video_ctrls+i;
485 static struct saa7134_format* format_by_fourcc(unsigned int fourcc)
489 for (i = 0; i < FORMATS; i++)
490 if (formats[i].fourcc == fourcc)
495 /* ----------------------------------------------------------------------- */
496 /* resource management */
498 static int res_get(struct saa7134_dev *dev, struct saa7134_fh *fh, unsigned int bit)
500 if (fh->resources & bit)
501 /* have it already allocated */
505 mutex_lock(&dev->lock);
506 if (dev->resources & bit) {
507 /* no, someone else uses it */
508 mutex_unlock(&dev->lock);
511 /* it's free, grab it */
512 fh->resources |= bit;
513 dev->resources |= bit;
514 dprintk("res: get %d\n",bit);
515 mutex_unlock(&dev->lock);
519 static int res_check(struct saa7134_fh *fh, unsigned int bit)
521 return (fh->resources & bit);
524 static int res_locked(struct saa7134_dev *dev, unsigned int bit)
526 return (dev->resources & bit);
530 void res_free(struct saa7134_dev *dev, struct saa7134_fh *fh, unsigned int bits)
532 BUG_ON((fh->resources & bits) != bits);
534 mutex_lock(&dev->lock);
535 fh->resources &= ~bits;
536 dev->resources &= ~bits;
537 dprintk("res: put %d\n",bits);
538 mutex_unlock(&dev->lock);
541 /* ------------------------------------------------------------------ */
543 static void set_tvnorm(struct saa7134_dev *dev, struct saa7134_tvnorm *norm)
545 int luma_control,sync_control,mux;
547 dprintk("set tv norm = %s\n",norm->name);
550 mux = card_in(dev,dev->ctl_input).vmux;
551 luma_control = norm->luma_control;
552 sync_control = norm->sync_control;
555 luma_control |= 0x80; /* svideo */
556 if (noninterlaced || dev->nosignal)
557 sync_control |= 0x20;
560 dev->crop_bounds.left = norm->h_start;
561 dev->crop_defrect.left = norm->h_start;
562 dev->crop_bounds.width = norm->h_stop - norm->h_start +1;
563 dev->crop_defrect.width = norm->h_stop - norm->h_start +1;
565 dev->crop_bounds.top = (norm->vbi_v_stop_0+1)*2;
566 dev->crop_defrect.top = norm->video_v_start*2;
567 dev->crop_bounds.height = ((norm->id & V4L2_STD_525_60) ? 524 : 624)
568 - dev->crop_bounds.top;
569 dev->crop_defrect.height = (norm->video_v_stop - norm->video_v_start +1)*2;
571 dev->crop_current = dev->crop_defrect;
573 /* setup video decoder */
574 saa_writeb(SAA7134_INCR_DELAY, 0x08);
575 saa_writeb(SAA7134_ANALOG_IN_CTRL1, 0xc0 | mux);
576 saa_writeb(SAA7134_ANALOG_IN_CTRL2, 0x00);
578 saa_writeb(SAA7134_ANALOG_IN_CTRL3, 0x90);
579 saa_writeb(SAA7134_ANALOG_IN_CTRL4, 0x90);
580 saa_writeb(SAA7134_HSYNC_START, 0xeb);
581 saa_writeb(SAA7134_HSYNC_STOP, 0xe0);
582 saa_writeb(SAA7134_SOURCE_TIMING1, norm->src_timing);
584 saa_writeb(SAA7134_SYNC_CTRL, sync_control);
585 saa_writeb(SAA7134_LUMA_CTRL, luma_control);
586 saa_writeb(SAA7134_DEC_LUMA_BRIGHT, dev->ctl_bright);
587 saa_writeb(SAA7134_DEC_LUMA_CONTRAST, dev->ctl_contrast);
589 saa_writeb(SAA7134_DEC_CHROMA_SATURATION, dev->ctl_saturation);
590 saa_writeb(SAA7134_DEC_CHROMA_HUE, dev->ctl_hue);
591 saa_writeb(SAA7134_CHROMA_CTRL1, norm->chroma_ctrl1);
592 saa_writeb(SAA7134_CHROMA_GAIN, norm->chroma_gain);
594 saa_writeb(SAA7134_CHROMA_CTRL2, norm->chroma_ctrl2);
595 saa_writeb(SAA7134_MODE_DELAY_CTRL, 0x00);
597 saa_writeb(SAA7134_ANALOG_ADC, 0x01);
598 saa_writeb(SAA7134_VGATE_START, 0x11);
599 saa_writeb(SAA7134_VGATE_STOP, 0xfe);
600 saa_writeb(SAA7134_MISC_VGATE_MSB, norm->vgate_misc);
601 saa_writeb(SAA7134_RAW_DATA_GAIN, 0x40);
602 saa_writeb(SAA7134_RAW_DATA_OFFSET, 0x80);
604 /* only tell the tuner if this is a tv input */
605 if (card_in(dev,dev->ctl_input).tv) {
606 if ((card(dev).tuner_type == TUNER_PHILIPS_TDA8290)
607 && ((card(dev).tuner_config == 1)
608 || (card(dev).tuner_config == 2)))
609 saa7134_set_gpio(dev, 22, 5);
610 saa7134_i2c_call_clients(dev,VIDIOC_S_STD,&norm->id);
614 static void video_mux(struct saa7134_dev *dev, int input)
616 dprintk("video input = %d [%s]\n",input,card_in(dev,input).name);
617 dev->ctl_input = input;
618 set_tvnorm(dev,dev->tvnorm);
619 saa7134_tvaudio_setinput(dev,&card_in(dev,input));
622 static void set_h_prescale(struct saa7134_dev *dev, int task, int prescale)
624 static const struct {
631 /* XPSC XACL XC2_1 XDCG VPFY */
643 static const int count = ARRAY_SIZE(vals);
646 for (i = 0; i < count; i++)
647 if (vals[i].xpsc == prescale)
652 saa_writeb(SAA7134_H_PRESCALE(task), vals[i].xpsc);
653 saa_writeb(SAA7134_ACC_LENGTH(task), vals[i].xacl);
654 saa_writeb(SAA7134_LEVEL_CTRL(task),
655 (vals[i].xc2_1 << 3) | (vals[i].xdcg));
656 saa_andorb(SAA7134_FIR_PREFILTER_CTRL(task), 0x0f,
657 (vals[i].vpfy << 2) | vals[i].vpfy);
660 static void set_v_scale(struct saa7134_dev *dev, int task, int yscale)
664 saa_writeb(SAA7134_V_SCALE_RATIO1(task), yscale & 0xff);
665 saa_writeb(SAA7134_V_SCALE_RATIO2(task), yscale >> 8);
667 mirror = (dev->ctl_mirror) ? 0x02 : 0x00;
670 dprintk("yscale LPI yscale=%d\n",yscale);
671 saa_writeb(SAA7134_V_FILTER(task), 0x00 | mirror);
672 saa_writeb(SAA7134_LUMA_CONTRAST(task), 0x40);
673 saa_writeb(SAA7134_CHROMA_SATURATION(task), 0x40);
676 val = 0x40 * 1024 / yscale;
677 dprintk("yscale ACM yscale=%d val=0x%x\n",yscale,val);
678 saa_writeb(SAA7134_V_FILTER(task), 0x01 | mirror);
679 saa_writeb(SAA7134_LUMA_CONTRAST(task), val);
680 saa_writeb(SAA7134_CHROMA_SATURATION(task), val);
682 saa_writeb(SAA7134_LUMA_BRIGHT(task), 0x80);
685 static void set_size(struct saa7134_dev *dev, int task,
686 int width, int height, int interlace)
688 int prescale,xscale,yscale,y_even,y_odd;
689 int h_start, h_stop, v_start, v_stop;
690 int div = interlace ? 2 : 1;
692 /* setup video scaler */
693 h_start = dev->crop_current.left;
694 v_start = dev->crop_current.top/2;
695 h_stop = (dev->crop_current.left + dev->crop_current.width -1);
696 v_stop = (dev->crop_current.top + dev->crop_current.height -1)/2;
698 saa_writeb(SAA7134_VIDEO_H_START1(task), h_start & 0xff);
699 saa_writeb(SAA7134_VIDEO_H_START2(task), h_start >> 8);
700 saa_writeb(SAA7134_VIDEO_H_STOP1(task), h_stop & 0xff);
701 saa_writeb(SAA7134_VIDEO_H_STOP2(task), h_stop >> 8);
702 saa_writeb(SAA7134_VIDEO_V_START1(task), v_start & 0xff);
703 saa_writeb(SAA7134_VIDEO_V_START2(task), v_start >> 8);
704 saa_writeb(SAA7134_VIDEO_V_STOP1(task), v_stop & 0xff);
705 saa_writeb(SAA7134_VIDEO_V_STOP2(task), v_stop >> 8);
707 prescale = dev->crop_current.width / width;
710 xscale = 1024 * dev->crop_current.width / prescale / width;
711 yscale = 512 * div * dev->crop_current.height / height;
712 dprintk("prescale=%d xscale=%d yscale=%d\n",prescale,xscale,yscale);
713 set_h_prescale(dev,task,prescale);
714 saa_writeb(SAA7134_H_SCALE_INC1(task), xscale & 0xff);
715 saa_writeb(SAA7134_H_SCALE_INC2(task), xscale >> 8);
716 set_v_scale(dev,task,yscale);
718 saa_writeb(SAA7134_VIDEO_PIXELS1(task), width & 0xff);
719 saa_writeb(SAA7134_VIDEO_PIXELS2(task), width >> 8);
720 saa_writeb(SAA7134_VIDEO_LINES1(task), height/div & 0xff);
721 saa_writeb(SAA7134_VIDEO_LINES2(task), height/div >> 8);
723 /* deinterlace y offsets */
724 y_odd = dev->ctl_y_odd;
725 y_even = dev->ctl_y_even;
726 saa_writeb(SAA7134_V_PHASE_OFFSET0(task), y_odd);
727 saa_writeb(SAA7134_V_PHASE_OFFSET1(task), y_even);
728 saa_writeb(SAA7134_V_PHASE_OFFSET2(task), y_odd);
729 saa_writeb(SAA7134_V_PHASE_OFFSET3(task), y_even);
732 /* ------------------------------------------------------------------ */
740 static void set_cliplist(struct saa7134_dev *dev, int reg,
741 struct cliplist *cl, int entries, char *name)
746 for (i = 0; i < entries; i++) {
747 winbits |= cl[i].enable;
748 winbits &= ~cl[i].disable;
749 if (i < 15 && cl[i].position == cl[i+1].position)
751 saa_writeb(reg + 0, winbits);
752 saa_writeb(reg + 2, cl[i].position & 0xff);
753 saa_writeb(reg + 3, cl[i].position >> 8);
754 dprintk("clip: %s winbits=%02x pos=%d\n",
755 name,winbits,cl[i].position);
758 for (; reg < 0x400; reg += 8) {
759 saa_writeb(reg+ 0, 0);
760 saa_writeb(reg + 1, 0);
761 saa_writeb(reg + 2, 0);
762 saa_writeb(reg + 3, 0);
766 static int clip_range(int val)
773 /* Sort into smallest position first order */
774 static int cliplist_cmp(const void *a, const void *b)
776 const struct cliplist *cla = a;
777 const struct cliplist *clb = b;
778 if (cla->position < clb->position)
780 if (cla->position > clb->position)
785 static int setup_clipping(struct saa7134_dev *dev, struct v4l2_clip *clips,
786 int nclips, int interlace)
788 struct cliplist col[16], row[16];
789 int cols = 0, rows = 0, i;
790 int div = interlace ? 2 : 1;
792 memset(col, 0, sizeof(col));
793 memset(row, 0, sizeof(row));
794 for (i = 0; i < nclips && i < 8; i++) {
795 col[cols].position = clip_range(clips[i].c.left);
796 col[cols].enable = (1 << i);
798 col[cols].position = clip_range(clips[i].c.left+clips[i].c.width);
799 col[cols].disable = (1 << i);
801 row[rows].position = clip_range(clips[i].c.top / div);
802 row[rows].enable = (1 << i);
804 row[rows].position = clip_range((clips[i].c.top + clips[i].c.height)
806 row[rows].disable = (1 << i);
809 sort(col, cols, sizeof col[0], cliplist_cmp, NULL);
810 sort(row, rows, sizeof row[0], cliplist_cmp, NULL);
811 set_cliplist(dev,0x380,col,cols,"cols");
812 set_cliplist(dev,0x384,row,rows,"rows");
816 static int verify_preview(struct saa7134_dev *dev, struct v4l2_window *win)
818 enum v4l2_field field;
821 if (NULL == dev->ovbuf.base)
823 if (NULL == dev->ovfmt)
825 if (win->w.width < 48 || win->w.height < 32)
827 if (win->clipcount > 2048)
831 maxw = dev->crop_current.width;
832 maxh = dev->crop_current.height;
834 if (V4L2_FIELD_ANY == field) {
835 field = (win->w.height > maxh/2)
836 ? V4L2_FIELD_INTERLACED
841 case V4L2_FIELD_BOTTOM:
844 case V4L2_FIELD_INTERLACED:
851 if (win->w.width > maxw)
853 if (win->w.height > maxh)
854 win->w.height = maxh;
858 static int start_preview(struct saa7134_dev *dev, struct saa7134_fh *fh)
860 unsigned long base,control,bpl;
863 err = verify_preview(dev,&fh->win);
867 dev->ovfield = fh->win.field;
868 dprintk("start_preview %dx%d+%d+%d %s field=%s\n",
869 fh->win.w.width,fh->win.w.height,
870 fh->win.w.left,fh->win.w.top,
871 dev->ovfmt->name,v4l2_field_names[dev->ovfield]);
873 /* setup window + clipping */
874 set_size(dev,TASK_B,fh->win.w.width,fh->win.w.height,
875 V4L2_FIELD_HAS_BOTH(dev->ovfield));
876 setup_clipping(dev,fh->clips,fh->nclips,
877 V4L2_FIELD_HAS_BOTH(dev->ovfield));
879 saa_andorb(SAA7134_DATA_PATH(TASK_B), 0x3f, 0x03);
881 saa_andorb(SAA7134_DATA_PATH(TASK_B), 0x3f, 0x01);
882 saa_writeb(SAA7134_OFMT_VIDEO_B, dev->ovfmt->pm | 0x20);
884 /* dma: setup channel 1 (= Video Task B) */
885 base = (unsigned long)dev->ovbuf.base;
886 base += dev->ovbuf.fmt.bytesperline * fh->win.w.top;
887 base += dev->ovfmt->depth/8 * fh->win.w.left;
888 bpl = dev->ovbuf.fmt.bytesperline;
889 control = SAA7134_RS_CONTROL_BURST_16;
890 if (dev->ovfmt->bswap)
891 control |= SAA7134_RS_CONTROL_BSWAP;
892 if (dev->ovfmt->wswap)
893 control |= SAA7134_RS_CONTROL_WSWAP;
894 if (V4L2_FIELD_HAS_BOTH(dev->ovfield)) {
895 saa_writel(SAA7134_RS_BA1(1),base);
896 saa_writel(SAA7134_RS_BA2(1),base+bpl);
897 saa_writel(SAA7134_RS_PITCH(1),bpl*2);
898 saa_writel(SAA7134_RS_CONTROL(1),control);
900 saa_writel(SAA7134_RS_BA1(1),base);
901 saa_writel(SAA7134_RS_BA2(1),base);
902 saa_writel(SAA7134_RS_PITCH(1),bpl);
903 saa_writel(SAA7134_RS_CONTROL(1),control);
908 saa7134_set_dmabits(dev);
913 static int stop_preview(struct saa7134_dev *dev, struct saa7134_fh *fh)
916 saa7134_set_dmabits(dev);
920 /* ------------------------------------------------------------------ */
922 static int buffer_activate(struct saa7134_dev *dev,
923 struct saa7134_buf *buf,
924 struct saa7134_buf *next)
926 unsigned long base,control,bpl;
927 unsigned long bpl_uv,lines_uv,base2,base3,tmp; /* planar */
929 dprintk("buffer_activate buf=%p\n",buf);
930 buf->vb.state = STATE_ACTIVE;
933 set_size(dev,TASK_A,buf->vb.width,buf->vb.height,
934 V4L2_FIELD_HAS_BOTH(buf->vb.field));
936 saa_andorb(SAA7134_DATA_PATH(TASK_A), 0x3f, 0x03);
938 saa_andorb(SAA7134_DATA_PATH(TASK_A), 0x3f, 0x01);
939 saa_writeb(SAA7134_OFMT_VIDEO_A, buf->fmt->pm);
941 /* DMA: setup channel 0 (= Video Task A0) */
942 base = saa7134_buffer_base(buf);
943 if (buf->fmt->planar)
946 bpl = (buf->vb.width * buf->fmt->depth) / 8;
947 control = SAA7134_RS_CONTROL_BURST_16 |
948 SAA7134_RS_CONTROL_ME |
949 (buf->pt->dma >> 12);
951 control |= SAA7134_RS_CONTROL_BSWAP;
953 control |= SAA7134_RS_CONTROL_WSWAP;
954 if (V4L2_FIELD_HAS_BOTH(buf->vb.field)) {
956 saa_writel(SAA7134_RS_BA1(0),base);
957 saa_writel(SAA7134_RS_BA2(0),base+bpl);
958 saa_writel(SAA7134_RS_PITCH(0),bpl*2);
961 saa_writel(SAA7134_RS_BA1(0),base);
962 saa_writel(SAA7134_RS_BA2(0),base);
963 saa_writel(SAA7134_RS_PITCH(0),bpl);
965 saa_writel(SAA7134_RS_CONTROL(0),control);
967 if (buf->fmt->planar) {
968 /* DMA: setup channel 4+5 (= planar task A) */
969 bpl_uv = bpl >> buf->fmt->hshift;
970 lines_uv = buf->vb.height >> buf->fmt->vshift;
971 base2 = base + bpl * buf->vb.height;
972 base3 = base2 + bpl_uv * lines_uv;
973 if (buf->fmt->uvswap)
974 tmp = base2, base2 = base3, base3 = tmp;
975 dprintk("uv: bpl=%ld lines=%ld base2/3=%ld/%ld\n",
976 bpl_uv,lines_uv,base2,base3);
977 if (V4L2_FIELD_HAS_BOTH(buf->vb.field)) {
979 saa_writel(SAA7134_RS_BA1(4),base2);
980 saa_writel(SAA7134_RS_BA2(4),base2+bpl_uv);
981 saa_writel(SAA7134_RS_PITCH(4),bpl_uv*2);
982 saa_writel(SAA7134_RS_BA1(5),base3);
983 saa_writel(SAA7134_RS_BA2(5),base3+bpl_uv);
984 saa_writel(SAA7134_RS_PITCH(5),bpl_uv*2);
987 saa_writel(SAA7134_RS_BA1(4),base2);
988 saa_writel(SAA7134_RS_BA2(4),base2);
989 saa_writel(SAA7134_RS_PITCH(4),bpl_uv);
990 saa_writel(SAA7134_RS_BA1(5),base3);
991 saa_writel(SAA7134_RS_BA2(5),base3);
992 saa_writel(SAA7134_RS_PITCH(5),bpl_uv);
994 saa_writel(SAA7134_RS_CONTROL(4),control);
995 saa_writel(SAA7134_RS_CONTROL(5),control);
999 saa7134_set_dmabits(dev);
1000 mod_timer(&dev->video_q.timeout, jiffies+BUFFER_TIMEOUT);
1004 static int buffer_prepare(struct videobuf_queue *q,
1005 struct videobuf_buffer *vb,
1006 enum v4l2_field field)
1008 struct saa7134_fh *fh = q->priv_data;
1009 struct saa7134_dev *dev = fh->dev;
1010 struct saa7134_buf *buf = container_of(vb,struct saa7134_buf,vb);
1015 if (NULL == fh->fmt)
1017 if (fh->width < 48 ||
1019 fh->width/4 > dev->crop_current.width ||
1020 fh->height/4 > dev->crop_current.height ||
1021 fh->width > dev->crop_bounds.width ||
1022 fh->height > dev->crop_bounds.height)
1024 size = (fh->width * fh->height * fh->fmt->depth) >> 3;
1025 if (0 != buf->vb.baddr && buf->vb.bsize < size)
1028 dprintk("buffer_prepare [%d,size=%dx%d,bytes=%d,fields=%s,%s]\n",
1029 vb->i,fh->width,fh->height,size,v4l2_field_names[field],
1031 if (buf->vb.width != fh->width ||
1032 buf->vb.height != fh->height ||
1033 buf->vb.size != size ||
1034 buf->vb.field != field ||
1035 buf->fmt != fh->fmt) {
1036 saa7134_dma_free(q,buf);
1039 if (STATE_NEEDS_INIT == buf->vb.state) {
1040 buf->vb.width = fh->width;
1041 buf->vb.height = fh->height;
1042 buf->vb.size = size;
1043 buf->vb.field = field;
1045 buf->pt = &fh->pt_cap;
1047 err = videobuf_iolock(q,&buf->vb,&dev->ovbuf);
1050 err = saa7134_pgtable_build(dev->pci,buf->pt,
1053 saa7134_buffer_startpage(buf));
1057 buf->vb.state = STATE_PREPARED;
1058 buf->activate = buffer_activate;
1062 saa7134_dma_free(q,buf);
1067 buffer_setup(struct videobuf_queue *q, unsigned int *count, unsigned int *size)
1069 struct saa7134_fh *fh = q->priv_data;
1071 *size = fh->fmt->depth * fh->width * fh->height >> 3;
1074 *count = saa7134_buffer_count(*size,*count);
1078 static void buffer_queue(struct videobuf_queue *q, struct videobuf_buffer *vb)
1080 struct saa7134_fh *fh = q->priv_data;
1081 struct saa7134_buf *buf = container_of(vb,struct saa7134_buf,vb);
1083 saa7134_buffer_queue(fh->dev,&fh->dev->video_q,buf);
1086 static void buffer_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
1088 struct saa7134_buf *buf = container_of(vb,struct saa7134_buf,vb);
1090 saa7134_dma_free(q,buf);
1093 static struct videobuf_queue_ops video_qops = {
1094 .buf_setup = buffer_setup,
1095 .buf_prepare = buffer_prepare,
1096 .buf_queue = buffer_queue,
1097 .buf_release = buffer_release,
1100 /* ------------------------------------------------------------------ */
1102 static int get_control(struct saa7134_dev *dev, struct v4l2_control *c)
1104 const struct v4l2_queryctrl* ctrl;
1106 ctrl = ctrl_by_id(c->id);
1110 case V4L2_CID_BRIGHTNESS:
1111 c->value = dev->ctl_bright;
1114 c->value = dev->ctl_hue;
1116 case V4L2_CID_CONTRAST:
1117 c->value = dev->ctl_contrast;
1119 case V4L2_CID_SATURATION:
1120 c->value = dev->ctl_saturation;
1122 case V4L2_CID_AUDIO_MUTE:
1123 c->value = dev->ctl_mute;
1125 case V4L2_CID_AUDIO_VOLUME:
1126 c->value = dev->ctl_volume;
1128 case V4L2_CID_PRIVATE_INVERT:
1129 c->value = dev->ctl_invert;
1131 case V4L2_CID_HFLIP:
1132 c->value = dev->ctl_mirror;
1134 case V4L2_CID_PRIVATE_Y_EVEN:
1135 c->value = dev->ctl_y_even;
1137 case V4L2_CID_PRIVATE_Y_ODD:
1138 c->value = dev->ctl_y_odd;
1140 case V4L2_CID_PRIVATE_AUTOMUTE:
1141 c->value = dev->ctl_automute;
1149 static int set_control(struct saa7134_dev *dev, struct saa7134_fh *fh,
1150 struct v4l2_control *c)
1152 const struct v4l2_queryctrl* ctrl;
1153 unsigned long flags;
1154 int restart_overlay = 0;
1156 ctrl = ctrl_by_id(c->id);
1159 dprintk("set_control name=%s val=%d\n",ctrl->name,c->value);
1160 switch (ctrl->type) {
1161 case V4L2_CTRL_TYPE_BOOLEAN:
1162 case V4L2_CTRL_TYPE_MENU:
1163 case V4L2_CTRL_TYPE_INTEGER:
1164 if (c->value < ctrl->minimum)
1165 c->value = ctrl->minimum;
1166 if (c->value > ctrl->maximum)
1167 c->value = ctrl->maximum;
1173 case V4L2_CID_BRIGHTNESS:
1174 dev->ctl_bright = c->value;
1175 saa_writeb(SAA7134_DEC_LUMA_BRIGHT, dev->ctl_bright);
1178 dev->ctl_hue = c->value;
1179 saa_writeb(SAA7134_DEC_CHROMA_HUE, dev->ctl_hue);
1181 case V4L2_CID_CONTRAST:
1182 dev->ctl_contrast = c->value;
1183 saa_writeb(SAA7134_DEC_LUMA_CONTRAST,
1184 dev->ctl_invert ? -dev->ctl_contrast : dev->ctl_contrast);
1186 case V4L2_CID_SATURATION:
1187 dev->ctl_saturation = c->value;
1188 saa_writeb(SAA7134_DEC_CHROMA_SATURATION,
1189 dev->ctl_invert ? -dev->ctl_saturation : dev->ctl_saturation);
1191 case V4L2_CID_AUDIO_MUTE:
1192 dev->ctl_mute = c->value;
1193 saa7134_tvaudio_setmute(dev);
1195 case V4L2_CID_AUDIO_VOLUME:
1196 dev->ctl_volume = c->value;
1197 saa7134_tvaudio_setvolume(dev,dev->ctl_volume);
1199 case V4L2_CID_PRIVATE_INVERT:
1200 dev->ctl_invert = c->value;
1201 saa_writeb(SAA7134_DEC_LUMA_CONTRAST,
1202 dev->ctl_invert ? -dev->ctl_contrast : dev->ctl_contrast);
1203 saa_writeb(SAA7134_DEC_CHROMA_SATURATION,
1204 dev->ctl_invert ? -dev->ctl_saturation : dev->ctl_saturation);
1206 case V4L2_CID_HFLIP:
1207 dev->ctl_mirror = c->value;
1208 restart_overlay = 1;
1210 case V4L2_CID_PRIVATE_Y_EVEN:
1211 dev->ctl_y_even = c->value;
1212 restart_overlay = 1;
1214 case V4L2_CID_PRIVATE_Y_ODD:
1215 dev->ctl_y_odd = c->value;
1216 restart_overlay = 1;
1218 case V4L2_CID_PRIVATE_AUTOMUTE:
1219 dev->ctl_automute = c->value;
1220 if (dev->tda9887_conf) {
1221 if (dev->ctl_automute)
1222 dev->tda9887_conf |= TDA9887_AUTOMUTE;
1224 dev->tda9887_conf &= ~TDA9887_AUTOMUTE;
1225 saa7134_i2c_call_clients(dev, TDA9887_SET_CONFIG,
1226 &dev->tda9887_conf);
1232 if (restart_overlay && fh && res_check(fh, RESOURCE_OVERLAY)) {
1233 spin_lock_irqsave(&dev->slock,flags);
1234 stop_preview(dev,fh);
1235 start_preview(dev,fh);
1236 spin_unlock_irqrestore(&dev->slock,flags);
1241 /* ------------------------------------------------------------------ */
1243 static struct videobuf_queue* saa7134_queue(struct saa7134_fh *fh)
1245 struct videobuf_queue* q = NULL;
1248 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1251 case V4L2_BUF_TYPE_VBI_CAPTURE:
1260 static int saa7134_resource(struct saa7134_fh *fh)
1262 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1263 return RESOURCE_VIDEO;
1265 if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)
1266 return RESOURCE_VBI;
1272 static int video_open(struct inode *inode, struct file *file)
1274 int minor = iminor(inode);
1275 struct saa7134_dev *h,*dev = NULL;
1276 struct saa7134_fh *fh;
1277 struct list_head *list;
1278 enum v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1280 list_for_each(list,&saa7134_devlist) {
1281 h = list_entry(list, struct saa7134_dev, devlist);
1282 if (h->video_dev && (h->video_dev->minor == minor))
1284 if (h->radio_dev && (h->radio_dev->minor == minor)) {
1288 if (h->vbi_dev && (h->vbi_dev->minor == minor)) {
1289 type = V4L2_BUF_TYPE_VBI_CAPTURE;
1296 dprintk("open minor=%d radio=%d type=%s\n",minor,radio,
1297 v4l2_type_names[type]);
1299 /* allocate + initialize per filehandle data */
1300 fh = kzalloc(sizeof(*fh),GFP_KERNEL);
1303 file->private_data = fh;
1307 fh->fmt = format_by_fourcc(V4L2_PIX_FMT_BGR24);
1310 v4l2_prio_open(&dev->prio,&fh->prio);
1312 videobuf_queue_init(&fh->cap, &video_qops,
1313 dev->pci, &dev->slock,
1314 V4L2_BUF_TYPE_VIDEO_CAPTURE,
1315 V4L2_FIELD_INTERLACED,
1316 sizeof(struct saa7134_buf),
1318 videobuf_queue_init(&fh->vbi, &saa7134_vbi_qops,
1319 dev->pci, &dev->slock,
1320 V4L2_BUF_TYPE_VBI_CAPTURE,
1322 sizeof(struct saa7134_buf),
1324 saa7134_pgtable_alloc(dev->pci,&fh->pt_cap);
1325 saa7134_pgtable_alloc(dev->pci,&fh->pt_vbi);
1328 /* switch to radio mode */
1329 saa7134_tvaudio_setinput(dev,&card(dev).radio);
1330 saa7134_i2c_call_clients(dev,AUDC_SET_RADIO, NULL);
1332 /* switch to video/vbi mode */
1333 video_mux(dev,dev->ctl_input);
1339 video_read(struct file *file, char __user *data, size_t count, loff_t *ppos)
1341 struct saa7134_fh *fh = file->private_data;
1344 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1345 if (res_locked(fh->dev,RESOURCE_VIDEO))
1347 return videobuf_read_one(saa7134_queue(fh),
1349 file->f_flags & O_NONBLOCK);
1350 case V4L2_BUF_TYPE_VBI_CAPTURE:
1351 if (!res_get(fh->dev,fh,RESOURCE_VBI))
1353 return videobuf_read_stream(saa7134_queue(fh),
1354 data, count, ppos, 1,
1355 file->f_flags & O_NONBLOCK);
1364 video_poll(struct file *file, struct poll_table_struct *wait)
1366 struct saa7134_fh *fh = file->private_data;
1367 struct videobuf_buffer *buf = NULL;
1369 if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type)
1370 return videobuf_poll_stream(file, &fh->vbi, wait);
1372 if (res_check(fh,RESOURCE_VIDEO)) {
1373 if (!list_empty(&fh->cap.stream))
1374 buf = list_entry(fh->cap.stream.next, struct videobuf_buffer, stream);
1376 mutex_lock(&fh->cap.lock);
1377 if (UNSET == fh->cap.read_off) {
1378 /* need to capture a new frame */
1379 if (res_locked(fh->dev,RESOURCE_VIDEO)) {
1380 mutex_unlock(&fh->cap.lock);
1383 if (0 != fh->cap.ops->buf_prepare(&fh->cap,fh->cap.read_buf,fh->cap.field)) {
1384 mutex_unlock(&fh->cap.lock);
1387 fh->cap.ops->buf_queue(&fh->cap,fh->cap.read_buf);
1388 fh->cap.read_off = 0;
1390 mutex_unlock(&fh->cap.lock);
1391 buf = fh->cap.read_buf;
1397 poll_wait(file, &buf->done, wait);
1398 if (buf->state == STATE_DONE ||
1399 buf->state == STATE_ERROR)
1400 return POLLIN|POLLRDNORM;
1404 static int video_release(struct inode *inode, struct file *file)
1406 struct saa7134_fh *fh = file->private_data;
1407 struct saa7134_dev *dev = fh->dev;
1408 unsigned long flags;
1410 /* turn off overlay */
1411 if (res_check(fh, RESOURCE_OVERLAY)) {
1412 spin_lock_irqsave(&dev->slock,flags);
1413 stop_preview(dev,fh);
1414 spin_unlock_irqrestore(&dev->slock,flags);
1415 res_free(dev,fh,RESOURCE_OVERLAY);
1418 /* stop video capture */
1419 if (res_check(fh, RESOURCE_VIDEO)) {
1420 videobuf_streamoff(&fh->cap);
1421 res_free(dev,fh,RESOURCE_VIDEO);
1423 if (fh->cap.read_buf) {
1424 buffer_release(&fh->cap,fh->cap.read_buf);
1425 kfree(fh->cap.read_buf);
1428 /* stop vbi capture */
1429 if (res_check(fh, RESOURCE_VBI)) {
1430 if (fh->vbi.streaming)
1431 videobuf_streamoff(&fh->vbi);
1432 if (fh->vbi.reading)
1433 videobuf_read_stop(&fh->vbi);
1434 res_free(dev,fh,RESOURCE_VBI);
1437 /* ts-capture will not work in planar mode, so turn it off Hac: 04.05*/
1438 saa_andorb(SAA7134_OFMT_VIDEO_A, 0x1f, 0);
1439 saa_andorb(SAA7134_OFMT_VIDEO_B, 0x1f, 0);
1440 saa_andorb(SAA7134_OFMT_DATA_A, 0x1f, 0);
1441 saa_andorb(SAA7134_OFMT_DATA_B, 0x1f, 0);
1443 saa7134_i2c_call_clients(dev, TUNER_SET_STANDBY, NULL);
1446 videobuf_mmap_free(&fh->cap);
1447 videobuf_mmap_free(&fh->vbi);
1448 saa7134_pgtable_free(dev->pci,&fh->pt_cap);
1449 saa7134_pgtable_free(dev->pci,&fh->pt_vbi);
1451 v4l2_prio_close(&dev->prio,&fh->prio);
1452 file->private_data = NULL;
1457 static int video_mmap(struct file *file, struct vm_area_struct * vma)
1459 struct saa7134_fh *fh = file->private_data;
1461 return videobuf_mmap_mapper(saa7134_queue(fh), vma);
1464 /* ------------------------------------------------------------------ */
1466 static void saa7134_vbi_fmt(struct saa7134_dev *dev, struct v4l2_format *f)
1468 struct saa7134_tvnorm *norm = dev->tvnorm;
1470 f->fmt.vbi.sampling_rate = 6750000 * 4;
1471 f->fmt.vbi.samples_per_line = 2048 /* VBI_LINE_LENGTH */;
1472 f->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
1473 f->fmt.vbi.offset = 64 * 4;
1474 f->fmt.vbi.start[0] = norm->vbi_v_start_0;
1475 f->fmt.vbi.count[0] = norm->vbi_v_stop_0 - norm->vbi_v_start_0 +1;
1476 f->fmt.vbi.start[1] = norm->vbi_v_start_1;
1477 f->fmt.vbi.count[1] = f->fmt.vbi.count[0];
1478 f->fmt.vbi.flags = 0; /* VBI_UNSYNC VBI_INTERLACED */
1482 static int saa7134_g_fmt(struct saa7134_dev *dev, struct saa7134_fh *fh,
1483 struct v4l2_format *f)
1486 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1487 memset(&f->fmt.pix,0,sizeof(f->fmt.pix));
1488 f->fmt.pix.width = fh->width;
1489 f->fmt.pix.height = fh->height;
1490 f->fmt.pix.field = fh->cap.field;
1491 f->fmt.pix.pixelformat = fh->fmt->fourcc;
1492 f->fmt.pix.bytesperline =
1493 (f->fmt.pix.width * fh->fmt->depth) >> 3;
1494 f->fmt.pix.sizeimage =
1495 f->fmt.pix.height * f->fmt.pix.bytesperline;
1497 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
1498 if (saa7134_no_overlay > 0) {
1499 printk ("V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
1502 f->fmt.win = fh->win;
1504 case V4L2_BUF_TYPE_VBI_CAPTURE:
1505 saa7134_vbi_fmt(dev,f);
1512 static int saa7134_try_fmt(struct saa7134_dev *dev, struct saa7134_fh *fh,
1513 struct v4l2_format *f)
1518 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1520 struct saa7134_format *fmt;
1521 enum v4l2_field field;
1522 unsigned int maxw, maxh;
1524 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1528 field = f->fmt.pix.field;
1529 maxw = min(dev->crop_current.width*4, dev->crop_bounds.width);
1530 maxh = min(dev->crop_current.height*4, dev->crop_bounds.height);
1532 if (V4L2_FIELD_ANY == field) {
1533 field = (f->fmt.pix.height > maxh/2)
1534 ? V4L2_FIELD_INTERLACED
1535 : V4L2_FIELD_BOTTOM;
1538 case V4L2_FIELD_TOP:
1539 case V4L2_FIELD_BOTTOM:
1542 case V4L2_FIELD_INTERLACED:
1548 f->fmt.pix.field = field;
1549 if (f->fmt.pix.width < 48)
1550 f->fmt.pix.width = 48;
1551 if (f->fmt.pix.height < 32)
1552 f->fmt.pix.height = 32;
1553 if (f->fmt.pix.width > maxw)
1554 f->fmt.pix.width = maxw;
1555 if (f->fmt.pix.height > maxh)
1556 f->fmt.pix.height = maxh;
1557 f->fmt.pix.width &= ~0x03;
1558 f->fmt.pix.bytesperline =
1559 (f->fmt.pix.width * fmt->depth) >> 3;
1560 f->fmt.pix.sizeimage =
1561 f->fmt.pix.height * f->fmt.pix.bytesperline;
1565 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
1566 if (saa7134_no_overlay > 0) {
1567 printk ("V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
1570 err = verify_preview(dev,&f->fmt.win);
1574 case V4L2_BUF_TYPE_VBI_CAPTURE:
1575 saa7134_vbi_fmt(dev,f);
1582 static int saa7134_s_fmt(struct saa7134_dev *dev, struct saa7134_fh *fh,
1583 struct v4l2_format *f)
1585 unsigned long flags;
1589 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1590 err = saa7134_try_fmt(dev,fh,f);
1594 fh->fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1595 fh->width = f->fmt.pix.width;
1596 fh->height = f->fmt.pix.height;
1597 fh->cap.field = f->fmt.pix.field;
1599 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
1600 if (saa7134_no_overlay > 0) {
1601 printk ("V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
1604 err = verify_preview(dev,&f->fmt.win);
1608 mutex_lock(&dev->lock);
1609 fh->win = f->fmt.win;
1610 fh->nclips = f->fmt.win.clipcount;
1613 if (copy_from_user(fh->clips,f->fmt.win.clips,
1614 sizeof(struct v4l2_clip)*fh->nclips)) {
1615 mutex_unlock(&dev->lock);
1619 if (res_check(fh, RESOURCE_OVERLAY)) {
1620 spin_lock_irqsave(&dev->slock,flags);
1621 stop_preview(dev,fh);
1622 start_preview(dev,fh);
1623 spin_unlock_irqrestore(&dev->slock,flags);
1625 mutex_unlock(&dev->lock);
1627 case V4L2_BUF_TYPE_VBI_CAPTURE:
1628 saa7134_vbi_fmt(dev,f);
1635 int saa7134_common_ioctl(struct saa7134_dev *dev,
1636 unsigned int cmd, void *arg)
1641 case VIDIOC_QUERYCTRL:
1643 const struct v4l2_queryctrl *ctrl;
1644 struct v4l2_queryctrl *c = arg;
1646 if ((c->id < V4L2_CID_BASE ||
1647 c->id >= V4L2_CID_LASTP1) &&
1648 (c->id < V4L2_CID_PRIVATE_BASE ||
1649 c->id >= V4L2_CID_PRIVATE_LASTP1))
1651 ctrl = ctrl_by_id(c->id);
1652 *c = (NULL != ctrl) ? *ctrl : no_ctrl;
1656 return get_control(dev,arg);
1659 mutex_lock(&dev->lock);
1660 err = set_control(dev,NULL,arg);
1661 mutex_unlock(&dev->lock);
1664 /* --- input switching --------------------------------------- */
1665 case VIDIOC_ENUMINPUT:
1667 struct v4l2_input *i = arg;
1671 if (n >= SAA7134_INPUT_MAX)
1673 if (NULL == card_in(dev,i->index).name)
1675 memset(i,0,sizeof(*i));
1677 i->type = V4L2_INPUT_TYPE_CAMERA;
1678 strcpy(i->name,card_in(dev,n).name);
1679 if (card_in(dev,n).tv)
1680 i->type = V4L2_INPUT_TYPE_TUNER;
1682 if (n == dev->ctl_input) {
1683 int v1 = saa_readb(SAA7134_STATUS_VIDEO1);
1684 int v2 = saa_readb(SAA7134_STATUS_VIDEO2);
1686 if (0 != (v1 & 0x40))
1687 i->status |= V4L2_IN_ST_NO_H_LOCK;
1688 if (0 != (v2 & 0x40))
1689 i->status |= V4L2_IN_ST_NO_SYNC;
1690 if (0 != (v2 & 0x0e))
1691 i->status |= V4L2_IN_ST_MACROVISION;
1693 for (n = 0; n < TVNORMS; n++)
1694 i->std |= tvnorms[n].id;
1697 case VIDIOC_G_INPUT:
1700 *i = dev->ctl_input;
1703 case VIDIOC_S_INPUT:
1707 if (*i < 0 || *i >= SAA7134_INPUT_MAX)
1709 if (NULL == card_in(dev,*i).name)
1711 mutex_lock(&dev->lock);
1713 mutex_unlock(&dev->lock);
1720 EXPORT_SYMBOL(saa7134_common_ioctl);
1723 * This function is _not_ called directly, but from
1724 * video_generic_ioctl (and maybe others). userspace
1725 * copying is done already, arg is a kernel pointer.
1727 static int video_do_ioctl(struct inode *inode, struct file *file,
1728 unsigned int cmd, void *arg)
1730 struct saa7134_fh *fh = file->private_data;
1731 struct saa7134_dev *dev = fh->dev;
1732 unsigned long flags;
1735 if (video_debug > 1)
1736 v4l_print_ioctl(dev->name,cmd);
1741 case VIDIOC_S_INPUT:
1742 case VIDIOC_S_TUNER:
1743 case VIDIOC_S_FREQUENCY:
1744 err = v4l2_prio_check(&dev->prio,&fh->prio);
1750 case VIDIOC_QUERYCAP:
1752 struct v4l2_capability *cap = arg;
1753 unsigned int tuner_type = dev->tuner_type;
1755 memset(cap,0,sizeof(*cap));
1756 strcpy(cap->driver, "saa7134");
1757 strlcpy(cap->card, saa7134_boards[dev->board].name,
1759 sprintf(cap->bus_info,"PCI:%s",pci_name(dev->pci));
1760 cap->version = SAA7134_VERSION_CODE;
1762 V4L2_CAP_VIDEO_CAPTURE |
1763 V4L2_CAP_VBI_CAPTURE |
1764 V4L2_CAP_READWRITE |
1765 V4L2_CAP_STREAMING |
1767 if (saa7134_no_overlay <= 0) {
1768 cap->capabilities |= V4L2_CAP_VIDEO_OVERLAY;
1771 if ((tuner_type == TUNER_ABSENT) || (tuner_type == UNSET))
1772 cap->capabilities &= ~V4L2_CAP_TUNER;
1777 /* --- tv standards ------------------------------------------ */
1778 case VIDIOC_ENUMSTD:
1780 struct v4l2_standard *e = arg;
1786 err = v4l2_video_std_construct(e, tvnorms[e->index].id,
1787 tvnorms[e->index].name);
1795 v4l2_std_id *id = arg;
1797 *id = dev->tvnorm->id;
1802 v4l2_std_id *id = arg;
1806 for (i = 0; i < TVNORMS; i++)
1807 if (*id == tvnorms[i].id)
1810 for (i = 0; i < TVNORMS; i++)
1811 if (*id & tvnorms[i].id)
1815 if ((*id & V4L2_STD_SECAM) && (secam[0] != '-')) {
1816 if (secam[0] == 'L' || secam[0] == 'l') {
1817 if (secam[1] == 'C' || secam[1] == 'c')
1818 fixup = V4L2_STD_SECAM_LC;
1820 fixup = V4L2_STD_SECAM_L;
1822 if (secam[0] == 'D' || secam[0] == 'd')
1823 fixup = V4L2_STD_SECAM_DK;
1825 fixup = V4L2_STD_SECAM;
1827 for (i = 0; i < TVNORMS; i++)
1828 if (fixup == tvnorms[i].id)
1831 mutex_lock(&dev->lock);
1832 if (res_check(fh, RESOURCE_OVERLAY)) {
1833 spin_lock_irqsave(&dev->slock,flags);
1834 stop_preview(dev,fh);
1835 set_tvnorm(dev,&tvnorms[i]);
1836 start_preview(dev,fh);
1837 spin_unlock_irqrestore(&dev->slock,flags);
1839 set_tvnorm(dev,&tvnorms[i]);
1840 saa7134_tvaudio_do_scan(dev);
1841 mutex_unlock(&dev->lock);
1845 case VIDIOC_CROPCAP:
1847 struct v4l2_cropcap *cap = arg;
1849 if (cap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1850 cap->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
1852 cap->bounds = dev->crop_bounds;
1853 cap->defrect = dev->crop_defrect;
1854 cap->pixelaspect.numerator = 1;
1855 cap->pixelaspect.denominator = 1;
1856 if (dev->tvnorm->id & V4L2_STD_525_60) {
1857 cap->pixelaspect.numerator = 11;
1858 cap->pixelaspect.denominator = 10;
1860 if (dev->tvnorm->id & V4L2_STD_625_50) {
1861 cap->pixelaspect.numerator = 54;
1862 cap->pixelaspect.denominator = 59;
1869 struct v4l2_crop * crop = arg;
1871 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1872 crop->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
1874 crop->c = dev->crop_current;
1879 struct v4l2_crop *crop = arg;
1880 struct v4l2_rect *b = &dev->crop_bounds;
1882 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1883 crop->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
1885 if (crop->c.height < 0)
1887 if (crop->c.width < 0)
1890 if (res_locked(fh->dev,RESOURCE_OVERLAY))
1892 if (res_locked(fh->dev,RESOURCE_VIDEO))
1895 if (crop->c.top < b->top)
1896 crop->c.top = b->top;
1897 if (crop->c.top > b->top + b->height)
1898 crop->c.top = b->top + b->height;
1899 if (crop->c.height > b->top - crop->c.top + b->height)
1900 crop->c.height = b->top - crop->c.top + b->height;
1902 if (crop->c.left < b->left)
1903 crop->c.left = b->left;
1904 if (crop->c.left > b->left + b->width)
1905 crop->c.left = b->left + b->width;
1906 if (crop->c.width > b->left - crop->c.left + b->width)
1907 crop->c.width = b->left - crop->c.left + b->width;
1909 dev->crop_current = crop->c;
1913 /* --- tuner ioctls ------------------------------------------ */
1914 case VIDIOC_G_TUNER:
1916 struct v4l2_tuner *t = arg;
1921 memset(t,0,sizeof(*t));
1922 for (n = 0; n < SAA7134_INPUT_MAX; n++)
1923 if (card_in(dev,n).tv)
1925 if (NULL != card_in(dev,n).name) {
1926 strcpy(t->name, "Television");
1927 t->type = V4L2_TUNER_ANALOG_TV;
1928 t->capability = V4L2_TUNER_CAP_NORM |
1929 V4L2_TUNER_CAP_STEREO |
1930 V4L2_TUNER_CAP_LANG1 |
1931 V4L2_TUNER_CAP_LANG2;
1932 t->rangehigh = 0xffffffffUL;
1933 t->rxsubchans = saa7134_tvaudio_getstereo(dev);
1934 t->audmode = saa7134_tvaudio_rx2mode(t->rxsubchans);
1936 if (0 != (saa_readb(SAA7134_STATUS_VIDEO1) & 0x03))
1940 case VIDIOC_S_TUNER:
1942 struct v4l2_tuner *t = arg;
1945 mode = dev->thread.mode;
1946 if (UNSET == mode) {
1947 rx = saa7134_tvaudio_getstereo(dev);
1948 mode = saa7134_tvaudio_rx2mode(t->rxsubchans);
1950 if (mode != t->audmode) {
1951 dev->thread.mode = t->audmode;
1955 case VIDIOC_G_FREQUENCY:
1957 struct v4l2_frequency *f = arg;
1959 memset(f,0,sizeof(*f));
1960 f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1961 f->frequency = dev->ctl_freq;
1964 case VIDIOC_S_FREQUENCY:
1966 struct v4l2_frequency *f = arg;
1970 if (0 == fh->radio && V4L2_TUNER_ANALOG_TV != f->type)
1972 if (1 == fh->radio && V4L2_TUNER_RADIO != f->type)
1974 mutex_lock(&dev->lock);
1975 dev->ctl_freq = f->frequency;
1977 saa7134_i2c_call_clients(dev,VIDIOC_S_FREQUENCY,f);
1979 saa7134_tvaudio_do_scan(dev);
1980 mutex_unlock(&dev->lock);
1984 /* --- control ioctls ---------------------------------------- */
1985 case VIDIOC_ENUMINPUT:
1986 case VIDIOC_G_INPUT:
1987 case VIDIOC_S_INPUT:
1988 case VIDIOC_QUERYCTRL:
1991 return saa7134_common_ioctl(dev, cmd, arg);
1993 case VIDIOC_G_AUDIO:
1995 struct v4l2_audio *a = arg;
1997 memset(a,0,sizeof(*a));
1998 strcpy(a->name,"audio");
2001 case VIDIOC_S_AUDIO:
2005 struct v4l2_captureparm *parm = arg;
2006 memset(parm,0,sizeof(*parm));
2010 case VIDIOC_G_PRIORITY:
2012 enum v4l2_priority *p = arg;
2014 *p = v4l2_prio_max(&dev->prio);
2017 case VIDIOC_S_PRIORITY:
2019 enum v4l2_priority *prio = arg;
2021 return v4l2_prio_change(&dev->prio, &fh->prio, *prio);
2024 /* --- preview ioctls ---------------------------------------- */
2025 case VIDIOC_ENUM_FMT:
2027 struct v4l2_fmtdesc *f = arg;
2028 enum v4l2_buf_type type;
2034 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2035 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
2036 if (saa7134_no_overlay > 0) {
2037 printk ("V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
2040 if (index >= FORMATS)
2042 if (f->type == V4L2_BUF_TYPE_VIDEO_OVERLAY &&
2043 formats[index].planar)
2045 memset(f,0,sizeof(*f));
2048 strlcpy(f->description,formats[index].name,sizeof(f->description));
2049 f->pixelformat = formats[index].fourcc;
2051 case V4L2_BUF_TYPE_VBI_CAPTURE:
2054 memset(f,0,sizeof(*f));
2057 f->pixelformat = V4L2_PIX_FMT_GREY;
2058 strcpy(f->description,"vbi data");
2067 struct v4l2_framebuffer *fb = arg;
2070 fb->capability = V4L2_FBUF_CAP_LIST_CLIPPING;
2075 struct v4l2_framebuffer *fb = arg;
2076 struct saa7134_format *fmt;
2078 if(!capable(CAP_SYS_ADMIN) &&
2079 !capable(CAP_SYS_RAWIO))
2083 fmt = format_by_fourcc(fb->fmt.pixelformat);
2090 if (0 == dev->ovbuf.fmt.bytesperline)
2091 dev->ovbuf.fmt.bytesperline =
2092 dev->ovbuf.fmt.width*fmt->depth/8;
2095 case VIDIOC_OVERLAY:
2100 if (saa7134_no_overlay > 0) {
2101 printk ("no_overlay\n");
2105 if (!res_get(dev,fh,RESOURCE_OVERLAY))
2107 spin_lock_irqsave(&dev->slock,flags);
2108 start_preview(dev,fh);
2109 spin_unlock_irqrestore(&dev->slock,flags);
2112 if (!res_check(fh, RESOURCE_OVERLAY))
2114 spin_lock_irqsave(&dev->slock,flags);
2115 stop_preview(dev,fh);
2116 spin_unlock_irqrestore(&dev->slock,flags);
2117 res_free(dev,fh,RESOURCE_OVERLAY);
2122 /* --- capture ioctls ---------------------------------------- */
2125 struct v4l2_format *f = arg;
2126 return saa7134_g_fmt(dev,fh,f);
2130 struct v4l2_format *f = arg;
2131 return saa7134_s_fmt(dev,fh,f);
2133 case VIDIOC_TRY_FMT:
2135 struct v4l2_format *f = arg;
2136 return saa7134_try_fmt(dev,fh,f);
2138 #ifdef CONFIG_VIDEO_V4L1_COMPAT
2141 struct video_mbuf *mbuf = arg;
2142 struct videobuf_queue *q;
2143 struct v4l2_requestbuffers req;
2146 q = saa7134_queue(fh);
2147 memset(&req,0,sizeof(req));
2149 req.count = gbuffers;
2150 req.memory = V4L2_MEMORY_MMAP;
2151 err = videobuf_reqbufs(q,&req);
2154 memset(mbuf,0,sizeof(*mbuf));
2155 mbuf->frames = req.count;
2157 for (i = 0; i < mbuf->frames; i++) {
2158 mbuf->offsets[i] = q->bufs[i]->boff;
2159 mbuf->size += q->bufs[i]->bsize;
2164 case VIDIOC_REQBUFS:
2165 return videobuf_reqbufs(saa7134_queue(fh),arg);
2167 case VIDIOC_QUERYBUF:
2168 return videobuf_querybuf(saa7134_queue(fh),arg);
2171 return videobuf_qbuf(saa7134_queue(fh),arg);
2174 return videobuf_dqbuf(saa7134_queue(fh),arg,
2175 file->f_flags & O_NONBLOCK);
2177 case VIDIOC_STREAMON:
2179 int res = saa7134_resource(fh);
2181 if (!res_get(dev,fh,res))
2183 return videobuf_streamon(saa7134_queue(fh));
2185 case VIDIOC_STREAMOFF:
2187 int res = saa7134_resource(fh);
2189 err = videobuf_streamoff(saa7134_queue(fh));
2192 res_free(dev,fh,res);
2197 return v4l_compat_translate_ioctl(inode,file,cmd,arg,
2203 static int video_ioctl(struct inode *inode, struct file *file,
2204 unsigned int cmd, unsigned long arg)
2206 return video_usercopy(inode, file, cmd, arg, video_do_ioctl);
2209 static int radio_do_ioctl(struct inode *inode, struct file *file,
2210 unsigned int cmd, void *arg)
2212 struct saa7134_fh *fh = file->private_data;
2213 struct saa7134_dev *dev = fh->dev;
2215 if (video_debug > 1)
2216 v4l_print_ioctl(dev->name,cmd);
2218 case VIDIOC_QUERYCAP:
2220 struct v4l2_capability *cap = arg;
2222 memset(cap,0,sizeof(*cap));
2223 strcpy(cap->driver, "saa7134");
2224 strlcpy(cap->card, saa7134_boards[dev->board].name,
2226 sprintf(cap->bus_info,"PCI:%s",pci_name(dev->pci));
2227 cap->version = SAA7134_VERSION_CODE;
2228 cap->capabilities = V4L2_CAP_TUNER;
2231 case VIDIOC_G_TUNER:
2233 struct v4l2_tuner *t = arg;
2238 memset(t,0,sizeof(*t));
2239 strcpy(t->name, "Radio");
2240 t->type = V4L2_TUNER_RADIO;
2242 saa7134_i2c_call_clients(dev, VIDIOC_G_TUNER, t);
2243 if (dev->input->amux == TV) {
2244 t->signal = 0xf800 - ((saa_readb(0x581) & 0x1f) << 11);
2245 t->rxsubchans = (saa_readb(0x529) & 0x08) ?
2246 V4L2_TUNER_SUB_STEREO : V4L2_TUNER_SUB_MONO;
2250 case VIDIOC_S_TUNER:
2252 struct v4l2_tuner *t = arg;
2257 saa7134_i2c_call_clients(dev,VIDIOC_S_TUNER,t);
2261 case VIDIOC_ENUMINPUT:
2263 struct v4l2_input *i = arg;
2267 strcpy(i->name,"Radio");
2268 i->type = V4L2_INPUT_TYPE_TUNER;
2271 case VIDIOC_G_INPUT:
2277 case VIDIOC_G_AUDIO:
2279 struct v4l2_audio *a = arg;
2281 memset(a,0,sizeof(*a));
2282 strcpy(a->name,"Radio");
2287 v4l2_std_id *id = arg;
2291 case VIDIOC_S_AUDIO:
2292 case VIDIOC_S_INPUT:
2296 case VIDIOC_QUERYCTRL:
2298 const struct v4l2_queryctrl *ctrl;
2299 struct v4l2_queryctrl *c = arg;
2301 if (c->id < V4L2_CID_BASE ||
2302 c->id >= V4L2_CID_LASTP1)
2304 if (c->id == V4L2_CID_AUDIO_MUTE) {
2305 ctrl = ctrl_by_id(c->id);
2314 case VIDIOC_G_FREQUENCY:
2315 case VIDIOC_S_FREQUENCY:
2316 return video_do_ioctl(inode,file,cmd,arg);
2319 return v4l_compat_translate_ioctl(inode,file,cmd,arg,
2325 static int radio_ioctl(struct inode *inode, struct file *file,
2326 unsigned int cmd, unsigned long arg)
2328 return video_usercopy(inode, file, cmd, arg, radio_do_ioctl);
2331 static const struct file_operations video_fops =
2333 .owner = THIS_MODULE,
2335 .release = video_release,
2339 .ioctl = video_ioctl,
2340 .compat_ioctl = v4l_compat_ioctl32,
2341 .llseek = no_llseek,
2344 static const struct file_operations radio_fops =
2346 .owner = THIS_MODULE,
2348 .release = video_release,
2349 .ioctl = radio_ioctl,
2350 .compat_ioctl = v4l_compat_ioctl32,
2351 .llseek = no_llseek,
2354 /* ----------------------------------------------------------- */
2355 /* exported stuff */
2357 struct video_device saa7134_video_template =
2359 .name = "saa7134-video",
2360 .type = VID_TYPE_CAPTURE|VID_TYPE_TUNER|
2361 VID_TYPE_CLIPPING|VID_TYPE_SCALES,
2363 .fops = &video_fops,
2367 struct video_device saa7134_vbi_template =
2369 .name = "saa7134-vbi",
2370 .type = VID_TYPE_TUNER|VID_TYPE_TELETEXT,
2372 .fops = &video_fops,
2376 struct video_device saa7134_radio_template =
2378 .name = "saa7134-radio",
2379 .type = VID_TYPE_TUNER,
2381 .fops = &radio_fops,
2385 int saa7134_video_init1(struct saa7134_dev *dev)
2387 /* sanitycheck insmod options */
2388 if (gbuffers < 2 || gbuffers > VIDEO_MAX_FRAME)
2390 if (gbufsize < 0 || gbufsize > gbufsize_max)
2391 gbufsize = gbufsize_max;
2392 gbufsize = (gbufsize + PAGE_SIZE - 1) & PAGE_MASK;
2394 /* put some sensible defaults into the data structures ... */
2395 dev->ctl_bright = ctrl_by_id(V4L2_CID_BRIGHTNESS)->default_value;
2396 dev->ctl_contrast = ctrl_by_id(V4L2_CID_CONTRAST)->default_value;
2397 dev->ctl_hue = ctrl_by_id(V4L2_CID_HUE)->default_value;
2398 dev->ctl_saturation = ctrl_by_id(V4L2_CID_SATURATION)->default_value;
2399 dev->ctl_volume = ctrl_by_id(V4L2_CID_AUDIO_VOLUME)->default_value;
2400 dev->ctl_mute = 1; // ctrl_by_id(V4L2_CID_AUDIO_MUTE)->default_value;
2401 dev->ctl_invert = ctrl_by_id(V4L2_CID_PRIVATE_INVERT)->default_value;
2402 dev->ctl_automute = ctrl_by_id(V4L2_CID_PRIVATE_AUTOMUTE)->default_value;
2404 if (dev->tda9887_conf && dev->ctl_automute)
2405 dev->tda9887_conf |= TDA9887_AUTOMUTE;
2408 INIT_LIST_HEAD(&dev->video_q.queue);
2409 init_timer(&dev->video_q.timeout);
2410 dev->video_q.timeout.function = saa7134_buffer_timeout;
2411 dev->video_q.timeout.data = (unsigned long)(&dev->video_q);
2412 dev->video_q.dev = dev;
2414 if (saa7134_boards[dev->board].video_out) {
2415 /* enable video output */
2416 int vo = saa7134_boards[dev->board].video_out;
2418 unsigned int vid_port_opts = saa7134_boards[dev->board].vid_port_opts;
2419 saa_writeb(SAA7134_VIDEO_PORT_CTRL0, video_out[vo][0]);
2420 video_reg = video_out[vo][1];
2421 if (vid_port_opts & SET_T_CODE_POLARITY_NON_INVERTED)
2422 video_reg &= ~VP_T_CODE_P_INVERTED;
2423 saa_writeb(SAA7134_VIDEO_PORT_CTRL1, video_reg);
2424 saa_writeb(SAA7134_VIDEO_PORT_CTRL2, video_out[vo][2]);
2425 saa_writeb(SAA7134_VIDEO_PORT_CTRL3, video_out[vo][3]);
2426 saa_writeb(SAA7134_VIDEO_PORT_CTRL4, video_out[vo][4]);
2427 video_reg = video_out[vo][5];
2428 if (vid_port_opts & SET_CLOCK_NOT_DELAYED)
2429 video_reg &= ~VP_CLK_CTRL2_DELAYED;
2430 if (vid_port_opts & SET_CLOCK_INVERTED)
2431 video_reg |= VP_CLK_CTRL1_INVERTED;
2432 saa_writeb(SAA7134_VIDEO_PORT_CTRL5, video_reg);
2433 video_reg = video_out[vo][6];
2434 if (vid_port_opts & SET_VSYNC_OFF) {
2435 video_reg &= ~VP_VS_TYPE_MASK;
2436 video_reg |= VP_VS_TYPE_OFF;
2438 saa_writeb(SAA7134_VIDEO_PORT_CTRL6, video_reg);
2439 saa_writeb(SAA7134_VIDEO_PORT_CTRL7, video_out[vo][7]);
2440 saa_writeb(SAA7134_VIDEO_PORT_CTRL8, video_out[vo][8]);
2446 int saa7134_video_init2(struct saa7134_dev *dev)
2449 set_tvnorm(dev,&tvnorms[0]);
2451 saa7134_tvaudio_setmute(dev);
2452 saa7134_tvaudio_setvolume(dev,dev->ctl_volume);
2456 void saa7134_irq_video_intl(struct saa7134_dev *dev)
2458 static const char *st[] = {
2459 "(no signal)", "NTSC", "PAL", "SECAM" };
2462 st1 = saa_readb(SAA7134_STATUS_VIDEO1);
2463 st2 = saa_readb(SAA7134_STATUS_VIDEO2);
2464 dprintk("DCSDT: pll: %s, sync: %s, norm: %s\n",
2465 (st1 & 0x40) ? "not locked" : "locked",
2466 (st2 & 0x40) ? "no" : "yes",
2468 dev->nosignal = (st1 & 0x40) || (st2 & 0x40);
2470 if (dev->nosignal) {
2471 /* no video signal -> mute audio */
2472 if (dev->ctl_automute)
2474 saa7134_tvaudio_setmute(dev);
2475 saa_setb(SAA7134_SYNC_CTRL, 0x20);
2477 /* wake up tvaudio audio carrier scan thread */
2478 saa7134_tvaudio_do_scan(dev);
2480 saa_clearb(SAA7134_SYNC_CTRL, 0x20);
2482 if (dev->mops && dev->mops->signal_change)
2483 dev->mops->signal_change(dev);
2486 void saa7134_irq_video_done(struct saa7134_dev *dev, unsigned long status)
2488 enum v4l2_field field;
2490 spin_lock(&dev->slock);
2491 if (dev->video_q.curr) {
2492 dev->video_fieldcount++;
2493 field = dev->video_q.curr->vb.field;
2494 if (V4L2_FIELD_HAS_BOTH(field)) {
2495 /* make sure we have seen both fields */
2496 if ((status & 0x10) == 0x00) {
2497 dev->video_q.curr->top_seen = 1;
2500 if (!dev->video_q.curr->top_seen)
2502 } else if (field == V4L2_FIELD_TOP) {
2503 if ((status & 0x10) != 0x10)
2505 } else if (field == V4L2_FIELD_BOTTOM) {
2506 if ((status & 0x10) != 0x00)
2509 dev->video_q.curr->vb.field_count = dev->video_fieldcount;
2510 saa7134_buffer_finish(dev,&dev->video_q,STATE_DONE);
2512 saa7134_buffer_next(dev,&dev->video_q);
2515 spin_unlock(&dev->slock);
2518 /* ----------------------------------------------------------- */