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);
588 saa_writeb(SAA7134_DEC_LUMA_CONTRAST,
589 dev->ctl_invert ? -dev->ctl_contrast : dev->ctl_contrast);
591 saa_writeb(SAA7134_DEC_CHROMA_SATURATION,
592 dev->ctl_invert ? -dev->ctl_saturation : dev->ctl_saturation);
594 saa_writeb(SAA7134_DEC_CHROMA_HUE, dev->ctl_hue);
595 saa_writeb(SAA7134_CHROMA_CTRL1, norm->chroma_ctrl1);
596 saa_writeb(SAA7134_CHROMA_GAIN, norm->chroma_gain);
598 saa_writeb(SAA7134_CHROMA_CTRL2, norm->chroma_ctrl2);
599 saa_writeb(SAA7134_MODE_DELAY_CTRL, 0x00);
601 saa_writeb(SAA7134_ANALOG_ADC, 0x01);
602 saa_writeb(SAA7134_VGATE_START, 0x11);
603 saa_writeb(SAA7134_VGATE_STOP, 0xfe);
604 saa_writeb(SAA7134_MISC_VGATE_MSB, norm->vgate_misc);
605 saa_writeb(SAA7134_RAW_DATA_GAIN, 0x40);
606 saa_writeb(SAA7134_RAW_DATA_OFFSET, 0x80);
608 /* only tell the tuner if this is a tv input */
609 if (card_in(dev,dev->ctl_input).tv) {
610 if ((card(dev).tuner_type == TUNER_PHILIPS_TDA8290)
611 && ((card(dev).tuner_config == 1)
612 || (card(dev).tuner_config == 2)))
613 saa7134_set_gpio(dev, 22, 5);
614 saa7134_i2c_call_clients(dev,VIDIOC_S_STD,&norm->id);
618 static void video_mux(struct saa7134_dev *dev, int input)
620 dprintk("video input = %d [%s]\n",input,card_in(dev,input).name);
621 dev->ctl_input = input;
622 set_tvnorm(dev,dev->tvnorm);
623 saa7134_tvaudio_setinput(dev,&card_in(dev,input));
626 static void set_h_prescale(struct saa7134_dev *dev, int task, int prescale)
628 static const struct {
635 /* XPSC XACL XC2_1 XDCG VPFY */
647 static const int count = ARRAY_SIZE(vals);
650 for (i = 0; i < count; i++)
651 if (vals[i].xpsc == prescale)
656 saa_writeb(SAA7134_H_PRESCALE(task), vals[i].xpsc);
657 saa_writeb(SAA7134_ACC_LENGTH(task), vals[i].xacl);
658 saa_writeb(SAA7134_LEVEL_CTRL(task),
659 (vals[i].xc2_1 << 3) | (vals[i].xdcg));
660 saa_andorb(SAA7134_FIR_PREFILTER_CTRL(task), 0x0f,
661 (vals[i].vpfy << 2) | vals[i].vpfy);
664 static void set_v_scale(struct saa7134_dev *dev, int task, int yscale)
668 saa_writeb(SAA7134_V_SCALE_RATIO1(task), yscale & 0xff);
669 saa_writeb(SAA7134_V_SCALE_RATIO2(task), yscale >> 8);
671 mirror = (dev->ctl_mirror) ? 0x02 : 0x00;
674 dprintk("yscale LPI yscale=%d\n",yscale);
675 saa_writeb(SAA7134_V_FILTER(task), 0x00 | mirror);
676 saa_writeb(SAA7134_LUMA_CONTRAST(task), 0x40);
677 saa_writeb(SAA7134_CHROMA_SATURATION(task), 0x40);
680 val = 0x40 * 1024 / yscale;
681 dprintk("yscale ACM yscale=%d val=0x%x\n",yscale,val);
682 saa_writeb(SAA7134_V_FILTER(task), 0x01 | mirror);
683 saa_writeb(SAA7134_LUMA_CONTRAST(task), val);
684 saa_writeb(SAA7134_CHROMA_SATURATION(task), val);
686 saa_writeb(SAA7134_LUMA_BRIGHT(task), 0x80);
689 static void set_size(struct saa7134_dev *dev, int task,
690 int width, int height, int interlace)
692 int prescale,xscale,yscale,y_even,y_odd;
693 int h_start, h_stop, v_start, v_stop;
694 int div = interlace ? 2 : 1;
696 /* setup video scaler */
697 h_start = dev->crop_current.left;
698 v_start = dev->crop_current.top/2;
699 h_stop = (dev->crop_current.left + dev->crop_current.width -1);
700 v_stop = (dev->crop_current.top + dev->crop_current.height -1)/2;
702 saa_writeb(SAA7134_VIDEO_H_START1(task), h_start & 0xff);
703 saa_writeb(SAA7134_VIDEO_H_START2(task), h_start >> 8);
704 saa_writeb(SAA7134_VIDEO_H_STOP1(task), h_stop & 0xff);
705 saa_writeb(SAA7134_VIDEO_H_STOP2(task), h_stop >> 8);
706 saa_writeb(SAA7134_VIDEO_V_START1(task), v_start & 0xff);
707 saa_writeb(SAA7134_VIDEO_V_START2(task), v_start >> 8);
708 saa_writeb(SAA7134_VIDEO_V_STOP1(task), v_stop & 0xff);
709 saa_writeb(SAA7134_VIDEO_V_STOP2(task), v_stop >> 8);
711 prescale = dev->crop_current.width / width;
714 xscale = 1024 * dev->crop_current.width / prescale / width;
715 yscale = 512 * div * dev->crop_current.height / height;
716 dprintk("prescale=%d xscale=%d yscale=%d\n",prescale,xscale,yscale);
717 set_h_prescale(dev,task,prescale);
718 saa_writeb(SAA7134_H_SCALE_INC1(task), xscale & 0xff);
719 saa_writeb(SAA7134_H_SCALE_INC2(task), xscale >> 8);
720 set_v_scale(dev,task,yscale);
722 saa_writeb(SAA7134_VIDEO_PIXELS1(task), width & 0xff);
723 saa_writeb(SAA7134_VIDEO_PIXELS2(task), width >> 8);
724 saa_writeb(SAA7134_VIDEO_LINES1(task), height/div & 0xff);
725 saa_writeb(SAA7134_VIDEO_LINES2(task), height/div >> 8);
727 /* deinterlace y offsets */
728 y_odd = dev->ctl_y_odd;
729 y_even = dev->ctl_y_even;
730 saa_writeb(SAA7134_V_PHASE_OFFSET0(task), y_odd);
731 saa_writeb(SAA7134_V_PHASE_OFFSET1(task), y_even);
732 saa_writeb(SAA7134_V_PHASE_OFFSET2(task), y_odd);
733 saa_writeb(SAA7134_V_PHASE_OFFSET3(task), y_even);
736 /* ------------------------------------------------------------------ */
744 static void set_cliplist(struct saa7134_dev *dev, int reg,
745 struct cliplist *cl, int entries, char *name)
750 for (i = 0; i < entries; i++) {
751 winbits |= cl[i].enable;
752 winbits &= ~cl[i].disable;
753 if (i < 15 && cl[i].position == cl[i+1].position)
755 saa_writeb(reg + 0, winbits);
756 saa_writeb(reg + 2, cl[i].position & 0xff);
757 saa_writeb(reg + 3, cl[i].position >> 8);
758 dprintk("clip: %s winbits=%02x pos=%d\n",
759 name,winbits,cl[i].position);
762 for (; reg < 0x400; reg += 8) {
763 saa_writeb(reg+ 0, 0);
764 saa_writeb(reg + 1, 0);
765 saa_writeb(reg + 2, 0);
766 saa_writeb(reg + 3, 0);
770 static int clip_range(int val)
777 /* Sort into smallest position first order */
778 static int cliplist_cmp(const void *a, const void *b)
780 const struct cliplist *cla = a;
781 const struct cliplist *clb = b;
782 if (cla->position < clb->position)
784 if (cla->position > clb->position)
789 static int setup_clipping(struct saa7134_dev *dev, struct v4l2_clip *clips,
790 int nclips, int interlace)
792 struct cliplist col[16], row[16];
793 int cols = 0, rows = 0, i;
794 int div = interlace ? 2 : 1;
796 memset(col, 0, sizeof(col));
797 memset(row, 0, sizeof(row));
798 for (i = 0; i < nclips && i < 8; i++) {
799 col[cols].position = clip_range(clips[i].c.left);
800 col[cols].enable = (1 << i);
802 col[cols].position = clip_range(clips[i].c.left+clips[i].c.width);
803 col[cols].disable = (1 << i);
805 row[rows].position = clip_range(clips[i].c.top / div);
806 row[rows].enable = (1 << i);
808 row[rows].position = clip_range((clips[i].c.top + clips[i].c.height)
810 row[rows].disable = (1 << i);
813 sort(col, cols, sizeof col[0], cliplist_cmp, NULL);
814 sort(row, rows, sizeof row[0], cliplist_cmp, NULL);
815 set_cliplist(dev,0x380,col,cols,"cols");
816 set_cliplist(dev,0x384,row,rows,"rows");
820 static int verify_preview(struct saa7134_dev *dev, struct v4l2_window *win)
822 enum v4l2_field field;
825 if (NULL == dev->ovbuf.base)
827 if (NULL == dev->ovfmt)
829 if (win->w.width < 48 || win->w.height < 32)
831 if (win->clipcount > 2048)
835 maxw = dev->crop_current.width;
836 maxh = dev->crop_current.height;
838 if (V4L2_FIELD_ANY == field) {
839 field = (win->w.height > maxh/2)
840 ? V4L2_FIELD_INTERLACED
845 case V4L2_FIELD_BOTTOM:
848 case V4L2_FIELD_INTERLACED:
855 if (win->w.width > maxw)
857 if (win->w.height > maxh)
858 win->w.height = maxh;
862 static int start_preview(struct saa7134_dev *dev, struct saa7134_fh *fh)
864 unsigned long base,control,bpl;
867 err = verify_preview(dev,&fh->win);
871 dev->ovfield = fh->win.field;
872 dprintk("start_preview %dx%d+%d+%d %s field=%s\n",
873 fh->win.w.width,fh->win.w.height,
874 fh->win.w.left,fh->win.w.top,
875 dev->ovfmt->name,v4l2_field_names[dev->ovfield]);
877 /* setup window + clipping */
878 set_size(dev,TASK_B,fh->win.w.width,fh->win.w.height,
879 V4L2_FIELD_HAS_BOTH(dev->ovfield));
880 setup_clipping(dev,fh->clips,fh->nclips,
881 V4L2_FIELD_HAS_BOTH(dev->ovfield));
883 saa_andorb(SAA7134_DATA_PATH(TASK_B), 0x3f, 0x03);
885 saa_andorb(SAA7134_DATA_PATH(TASK_B), 0x3f, 0x01);
886 saa_writeb(SAA7134_OFMT_VIDEO_B, dev->ovfmt->pm | 0x20);
888 /* dma: setup channel 1 (= Video Task B) */
889 base = (unsigned long)dev->ovbuf.base;
890 base += dev->ovbuf.fmt.bytesperline * fh->win.w.top;
891 base += dev->ovfmt->depth/8 * fh->win.w.left;
892 bpl = dev->ovbuf.fmt.bytesperline;
893 control = SAA7134_RS_CONTROL_BURST_16;
894 if (dev->ovfmt->bswap)
895 control |= SAA7134_RS_CONTROL_BSWAP;
896 if (dev->ovfmt->wswap)
897 control |= SAA7134_RS_CONTROL_WSWAP;
898 if (V4L2_FIELD_HAS_BOTH(dev->ovfield)) {
899 saa_writel(SAA7134_RS_BA1(1),base);
900 saa_writel(SAA7134_RS_BA2(1),base+bpl);
901 saa_writel(SAA7134_RS_PITCH(1),bpl*2);
902 saa_writel(SAA7134_RS_CONTROL(1),control);
904 saa_writel(SAA7134_RS_BA1(1),base);
905 saa_writel(SAA7134_RS_BA2(1),base);
906 saa_writel(SAA7134_RS_PITCH(1),bpl);
907 saa_writel(SAA7134_RS_CONTROL(1),control);
912 saa7134_set_dmabits(dev);
917 static int stop_preview(struct saa7134_dev *dev, struct saa7134_fh *fh)
920 saa7134_set_dmabits(dev);
924 /* ------------------------------------------------------------------ */
926 static int buffer_activate(struct saa7134_dev *dev,
927 struct saa7134_buf *buf,
928 struct saa7134_buf *next)
930 unsigned long base,control,bpl;
931 unsigned long bpl_uv,lines_uv,base2,base3,tmp; /* planar */
933 dprintk("buffer_activate buf=%p\n",buf);
934 buf->vb.state = STATE_ACTIVE;
937 set_size(dev,TASK_A,buf->vb.width,buf->vb.height,
938 V4L2_FIELD_HAS_BOTH(buf->vb.field));
940 saa_andorb(SAA7134_DATA_PATH(TASK_A), 0x3f, 0x03);
942 saa_andorb(SAA7134_DATA_PATH(TASK_A), 0x3f, 0x01);
943 saa_writeb(SAA7134_OFMT_VIDEO_A, buf->fmt->pm);
945 /* DMA: setup channel 0 (= Video Task A0) */
946 base = saa7134_buffer_base(buf);
947 if (buf->fmt->planar)
950 bpl = (buf->vb.width * buf->fmt->depth) / 8;
951 control = SAA7134_RS_CONTROL_BURST_16 |
952 SAA7134_RS_CONTROL_ME |
953 (buf->pt->dma >> 12);
955 control |= SAA7134_RS_CONTROL_BSWAP;
957 control |= SAA7134_RS_CONTROL_WSWAP;
958 if (V4L2_FIELD_HAS_BOTH(buf->vb.field)) {
960 saa_writel(SAA7134_RS_BA1(0),base);
961 saa_writel(SAA7134_RS_BA2(0),base+bpl);
962 saa_writel(SAA7134_RS_PITCH(0),bpl*2);
965 saa_writel(SAA7134_RS_BA1(0),base);
966 saa_writel(SAA7134_RS_BA2(0),base);
967 saa_writel(SAA7134_RS_PITCH(0),bpl);
969 saa_writel(SAA7134_RS_CONTROL(0),control);
971 if (buf->fmt->planar) {
972 /* DMA: setup channel 4+5 (= planar task A) */
973 bpl_uv = bpl >> buf->fmt->hshift;
974 lines_uv = buf->vb.height >> buf->fmt->vshift;
975 base2 = base + bpl * buf->vb.height;
976 base3 = base2 + bpl_uv * lines_uv;
977 if (buf->fmt->uvswap)
978 tmp = base2, base2 = base3, base3 = tmp;
979 dprintk("uv: bpl=%ld lines=%ld base2/3=%ld/%ld\n",
980 bpl_uv,lines_uv,base2,base3);
981 if (V4L2_FIELD_HAS_BOTH(buf->vb.field)) {
983 saa_writel(SAA7134_RS_BA1(4),base2);
984 saa_writel(SAA7134_RS_BA2(4),base2+bpl_uv);
985 saa_writel(SAA7134_RS_PITCH(4),bpl_uv*2);
986 saa_writel(SAA7134_RS_BA1(5),base3);
987 saa_writel(SAA7134_RS_BA2(5),base3+bpl_uv);
988 saa_writel(SAA7134_RS_PITCH(5),bpl_uv*2);
991 saa_writel(SAA7134_RS_BA1(4),base2);
992 saa_writel(SAA7134_RS_BA2(4),base2);
993 saa_writel(SAA7134_RS_PITCH(4),bpl_uv);
994 saa_writel(SAA7134_RS_BA1(5),base3);
995 saa_writel(SAA7134_RS_BA2(5),base3);
996 saa_writel(SAA7134_RS_PITCH(5),bpl_uv);
998 saa_writel(SAA7134_RS_CONTROL(4),control);
999 saa_writel(SAA7134_RS_CONTROL(5),control);
1003 saa7134_set_dmabits(dev);
1004 mod_timer(&dev->video_q.timeout, jiffies+BUFFER_TIMEOUT);
1008 static int buffer_prepare(struct videobuf_queue *q,
1009 struct videobuf_buffer *vb,
1010 enum v4l2_field field)
1012 struct saa7134_fh *fh = q->priv_data;
1013 struct saa7134_dev *dev = fh->dev;
1014 struct saa7134_buf *buf = container_of(vb,struct saa7134_buf,vb);
1019 if (NULL == fh->fmt)
1021 if (fh->width < 48 ||
1023 fh->width/4 > dev->crop_current.width ||
1024 fh->height/4 > dev->crop_current.height ||
1025 fh->width > dev->crop_bounds.width ||
1026 fh->height > dev->crop_bounds.height)
1028 size = (fh->width * fh->height * fh->fmt->depth) >> 3;
1029 if (0 != buf->vb.baddr && buf->vb.bsize < size)
1032 dprintk("buffer_prepare [%d,size=%dx%d,bytes=%d,fields=%s,%s]\n",
1033 vb->i,fh->width,fh->height,size,v4l2_field_names[field],
1035 if (buf->vb.width != fh->width ||
1036 buf->vb.height != fh->height ||
1037 buf->vb.size != size ||
1038 buf->vb.field != field ||
1039 buf->fmt != fh->fmt) {
1040 saa7134_dma_free(q,buf);
1043 if (STATE_NEEDS_INIT == buf->vb.state) {
1044 struct videobuf_dmabuf *dma=videobuf_to_dma(&buf->vb);
1046 buf->vb.width = fh->width;
1047 buf->vb.height = fh->height;
1048 buf->vb.size = size;
1049 buf->vb.field = field;
1051 buf->pt = &fh->pt_cap;
1053 err = videobuf_iolock(q,&buf->vb,&dev->ovbuf);
1056 err = saa7134_pgtable_build(dev->pci,buf->pt,
1059 saa7134_buffer_startpage(buf));
1063 buf->vb.state = STATE_PREPARED;
1064 buf->activate = buffer_activate;
1068 saa7134_dma_free(q,buf);
1073 buffer_setup(struct videobuf_queue *q, unsigned int *count, unsigned int *size)
1075 struct saa7134_fh *fh = q->priv_data;
1077 *size = fh->fmt->depth * fh->width * fh->height >> 3;
1080 *count = saa7134_buffer_count(*size,*count);
1084 static void buffer_queue(struct videobuf_queue *q, struct videobuf_buffer *vb)
1086 struct saa7134_fh *fh = q->priv_data;
1087 struct saa7134_buf *buf = container_of(vb,struct saa7134_buf,vb);
1089 saa7134_buffer_queue(fh->dev,&fh->dev->video_q,buf);
1092 static void buffer_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
1094 struct saa7134_buf *buf = container_of(vb,struct saa7134_buf,vb);
1096 saa7134_dma_free(q,buf);
1099 static struct videobuf_queue_ops video_qops = {
1100 .buf_setup = buffer_setup,
1101 .buf_prepare = buffer_prepare,
1102 .buf_queue = buffer_queue,
1103 .buf_release = buffer_release,
1106 /* ------------------------------------------------------------------ */
1108 static int get_control(struct saa7134_dev *dev, struct v4l2_control *c)
1110 const struct v4l2_queryctrl* ctrl;
1112 ctrl = ctrl_by_id(c->id);
1116 case V4L2_CID_BRIGHTNESS:
1117 c->value = dev->ctl_bright;
1120 c->value = dev->ctl_hue;
1122 case V4L2_CID_CONTRAST:
1123 c->value = dev->ctl_contrast;
1125 case V4L2_CID_SATURATION:
1126 c->value = dev->ctl_saturation;
1128 case V4L2_CID_AUDIO_MUTE:
1129 c->value = dev->ctl_mute;
1131 case V4L2_CID_AUDIO_VOLUME:
1132 c->value = dev->ctl_volume;
1134 case V4L2_CID_PRIVATE_INVERT:
1135 c->value = dev->ctl_invert;
1137 case V4L2_CID_HFLIP:
1138 c->value = dev->ctl_mirror;
1140 case V4L2_CID_PRIVATE_Y_EVEN:
1141 c->value = dev->ctl_y_even;
1143 case V4L2_CID_PRIVATE_Y_ODD:
1144 c->value = dev->ctl_y_odd;
1146 case V4L2_CID_PRIVATE_AUTOMUTE:
1147 c->value = dev->ctl_automute;
1155 static int set_control(struct saa7134_dev *dev, struct saa7134_fh *fh,
1156 struct v4l2_control *c)
1158 const struct v4l2_queryctrl* ctrl;
1159 unsigned long flags;
1160 int restart_overlay = 0;
1162 ctrl = ctrl_by_id(c->id);
1165 dprintk("set_control name=%s val=%d\n",ctrl->name,c->value);
1166 switch (ctrl->type) {
1167 case V4L2_CTRL_TYPE_BOOLEAN:
1168 case V4L2_CTRL_TYPE_MENU:
1169 case V4L2_CTRL_TYPE_INTEGER:
1170 if (c->value < ctrl->minimum)
1171 c->value = ctrl->minimum;
1172 if (c->value > ctrl->maximum)
1173 c->value = ctrl->maximum;
1179 case V4L2_CID_BRIGHTNESS:
1180 dev->ctl_bright = c->value;
1181 saa_writeb(SAA7134_DEC_LUMA_BRIGHT, dev->ctl_bright);
1184 dev->ctl_hue = c->value;
1185 saa_writeb(SAA7134_DEC_CHROMA_HUE, dev->ctl_hue);
1187 case V4L2_CID_CONTRAST:
1188 dev->ctl_contrast = c->value;
1189 saa_writeb(SAA7134_DEC_LUMA_CONTRAST,
1190 dev->ctl_invert ? -dev->ctl_contrast : dev->ctl_contrast);
1192 case V4L2_CID_SATURATION:
1193 dev->ctl_saturation = c->value;
1194 saa_writeb(SAA7134_DEC_CHROMA_SATURATION,
1195 dev->ctl_invert ? -dev->ctl_saturation : dev->ctl_saturation);
1197 case V4L2_CID_AUDIO_MUTE:
1198 dev->ctl_mute = c->value;
1199 saa7134_tvaudio_setmute(dev);
1201 case V4L2_CID_AUDIO_VOLUME:
1202 dev->ctl_volume = c->value;
1203 saa7134_tvaudio_setvolume(dev,dev->ctl_volume);
1205 case V4L2_CID_PRIVATE_INVERT:
1206 dev->ctl_invert = c->value;
1207 saa_writeb(SAA7134_DEC_LUMA_CONTRAST,
1208 dev->ctl_invert ? -dev->ctl_contrast : dev->ctl_contrast);
1209 saa_writeb(SAA7134_DEC_CHROMA_SATURATION,
1210 dev->ctl_invert ? -dev->ctl_saturation : dev->ctl_saturation);
1212 case V4L2_CID_HFLIP:
1213 dev->ctl_mirror = c->value;
1214 restart_overlay = 1;
1216 case V4L2_CID_PRIVATE_Y_EVEN:
1217 dev->ctl_y_even = c->value;
1218 restart_overlay = 1;
1220 case V4L2_CID_PRIVATE_Y_ODD:
1221 dev->ctl_y_odd = c->value;
1222 restart_overlay = 1;
1224 case V4L2_CID_PRIVATE_AUTOMUTE:
1225 dev->ctl_automute = c->value;
1226 if (dev->tda9887_conf) {
1227 if (dev->ctl_automute)
1228 dev->tda9887_conf |= TDA9887_AUTOMUTE;
1230 dev->tda9887_conf &= ~TDA9887_AUTOMUTE;
1231 saa7134_i2c_call_clients(dev, TDA9887_SET_CONFIG,
1232 &dev->tda9887_conf);
1238 if (restart_overlay && fh && res_check(fh, RESOURCE_OVERLAY)) {
1239 spin_lock_irqsave(&dev->slock,flags);
1240 stop_preview(dev,fh);
1241 start_preview(dev,fh);
1242 spin_unlock_irqrestore(&dev->slock,flags);
1247 /* ------------------------------------------------------------------ */
1249 static struct videobuf_queue* saa7134_queue(struct saa7134_fh *fh)
1251 struct videobuf_queue* q = NULL;
1254 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1257 case V4L2_BUF_TYPE_VBI_CAPTURE:
1266 static int saa7134_resource(struct saa7134_fh *fh)
1268 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1269 return RESOURCE_VIDEO;
1271 if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)
1272 return RESOURCE_VBI;
1278 static int video_open(struct inode *inode, struct file *file)
1280 int minor = iminor(inode);
1281 struct saa7134_dev *h,*dev = NULL;
1282 struct saa7134_fh *fh;
1283 struct list_head *list;
1284 enum v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1286 list_for_each(list,&saa7134_devlist) {
1287 h = list_entry(list, struct saa7134_dev, devlist);
1288 if (h->video_dev && (h->video_dev->minor == minor))
1290 if (h->radio_dev && (h->radio_dev->minor == minor)) {
1294 if (h->vbi_dev && (h->vbi_dev->minor == minor)) {
1295 type = V4L2_BUF_TYPE_VBI_CAPTURE;
1302 dprintk("open minor=%d radio=%d type=%s\n",minor,radio,
1303 v4l2_type_names[type]);
1305 /* allocate + initialize per filehandle data */
1306 fh = kzalloc(sizeof(*fh),GFP_KERNEL);
1309 file->private_data = fh;
1313 fh->fmt = format_by_fourcc(V4L2_PIX_FMT_BGR24);
1316 v4l2_prio_open(&dev->prio,&fh->prio);
1318 videobuf_queue_pci_init(&fh->cap, &video_qops,
1319 dev->pci, &dev->slock,
1320 V4L2_BUF_TYPE_VIDEO_CAPTURE,
1321 V4L2_FIELD_INTERLACED,
1322 sizeof(struct saa7134_buf),
1324 videobuf_queue_pci_init(&fh->vbi, &saa7134_vbi_qops,
1325 dev->pci, &dev->slock,
1326 V4L2_BUF_TYPE_VBI_CAPTURE,
1328 sizeof(struct saa7134_buf),
1330 saa7134_pgtable_alloc(dev->pci,&fh->pt_cap);
1331 saa7134_pgtable_alloc(dev->pci,&fh->pt_vbi);
1334 /* switch to radio mode */
1335 saa7134_tvaudio_setinput(dev,&card(dev).radio);
1336 saa7134_i2c_call_clients(dev,AUDC_SET_RADIO, NULL);
1338 /* switch to video/vbi mode */
1339 video_mux(dev,dev->ctl_input);
1345 video_read(struct file *file, char __user *data, size_t count, loff_t *ppos)
1347 struct saa7134_fh *fh = file->private_data;
1350 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1351 if (res_locked(fh->dev,RESOURCE_VIDEO))
1353 return videobuf_read_one(saa7134_queue(fh),
1355 file->f_flags & O_NONBLOCK);
1356 case V4L2_BUF_TYPE_VBI_CAPTURE:
1357 if (!res_get(fh->dev,fh,RESOURCE_VBI))
1359 return videobuf_read_stream(saa7134_queue(fh),
1360 data, count, ppos, 1,
1361 file->f_flags & O_NONBLOCK);
1370 video_poll(struct file *file, struct poll_table_struct *wait)
1372 struct saa7134_fh *fh = file->private_data;
1373 struct videobuf_buffer *buf = NULL;
1375 if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type)
1376 return videobuf_poll_stream(file, &fh->vbi, wait);
1378 if (res_check(fh,RESOURCE_VIDEO)) {
1379 if (!list_empty(&fh->cap.stream))
1380 buf = list_entry(fh->cap.stream.next, struct videobuf_buffer, stream);
1382 mutex_lock(&fh->cap.lock);
1383 if (UNSET == fh->cap.read_off) {
1384 /* need to capture a new frame */
1385 if (res_locked(fh->dev,RESOURCE_VIDEO)) {
1386 mutex_unlock(&fh->cap.lock);
1389 if (0 != fh->cap.ops->buf_prepare(&fh->cap,fh->cap.read_buf,fh->cap.field)) {
1390 mutex_unlock(&fh->cap.lock);
1393 fh->cap.ops->buf_queue(&fh->cap,fh->cap.read_buf);
1394 fh->cap.read_off = 0;
1396 mutex_unlock(&fh->cap.lock);
1397 buf = fh->cap.read_buf;
1403 poll_wait(file, &buf->done, wait);
1404 if (buf->state == STATE_DONE ||
1405 buf->state == STATE_ERROR)
1406 return POLLIN|POLLRDNORM;
1410 static int video_release(struct inode *inode, struct file *file)
1412 struct saa7134_fh *fh = file->private_data;
1413 struct saa7134_dev *dev = fh->dev;
1414 unsigned long flags;
1416 /* turn off overlay */
1417 if (res_check(fh, RESOURCE_OVERLAY)) {
1418 spin_lock_irqsave(&dev->slock,flags);
1419 stop_preview(dev,fh);
1420 spin_unlock_irqrestore(&dev->slock,flags);
1421 res_free(dev,fh,RESOURCE_OVERLAY);
1424 /* stop video capture */
1425 if (res_check(fh, RESOURCE_VIDEO)) {
1426 videobuf_streamoff(&fh->cap);
1427 res_free(dev,fh,RESOURCE_VIDEO);
1429 if (fh->cap.read_buf) {
1430 buffer_release(&fh->cap,fh->cap.read_buf);
1431 kfree(fh->cap.read_buf);
1434 /* stop vbi capture */
1435 if (res_check(fh, RESOURCE_VBI)) {
1436 if (fh->vbi.streaming)
1437 videobuf_streamoff(&fh->vbi);
1438 if (fh->vbi.reading)
1439 videobuf_read_stop(&fh->vbi);
1440 res_free(dev,fh,RESOURCE_VBI);
1443 /* ts-capture will not work in planar mode, so turn it off Hac: 04.05*/
1444 saa_andorb(SAA7134_OFMT_VIDEO_A, 0x1f, 0);
1445 saa_andorb(SAA7134_OFMT_VIDEO_B, 0x1f, 0);
1446 saa_andorb(SAA7134_OFMT_DATA_A, 0x1f, 0);
1447 saa_andorb(SAA7134_OFMT_DATA_B, 0x1f, 0);
1449 saa7134_i2c_call_clients(dev, TUNER_SET_STANDBY, NULL);
1452 videobuf_mmap_free(&fh->cap);
1453 videobuf_mmap_free(&fh->vbi);
1454 saa7134_pgtable_free(dev->pci,&fh->pt_cap);
1455 saa7134_pgtable_free(dev->pci,&fh->pt_vbi);
1457 v4l2_prio_close(&dev->prio,&fh->prio);
1458 file->private_data = NULL;
1463 static int video_mmap(struct file *file, struct vm_area_struct * vma)
1465 struct saa7134_fh *fh = file->private_data;
1467 return videobuf_mmap_mapper(saa7134_queue(fh), vma);
1470 /* ------------------------------------------------------------------ */
1472 static void saa7134_vbi_fmt(struct saa7134_dev *dev, struct v4l2_format *f)
1474 struct saa7134_tvnorm *norm = dev->tvnorm;
1476 f->fmt.vbi.sampling_rate = 6750000 * 4;
1477 f->fmt.vbi.samples_per_line = 2048 /* VBI_LINE_LENGTH */;
1478 f->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
1479 f->fmt.vbi.offset = 64 * 4;
1480 f->fmt.vbi.start[0] = norm->vbi_v_start_0;
1481 f->fmt.vbi.count[0] = norm->vbi_v_stop_0 - norm->vbi_v_start_0 +1;
1482 f->fmt.vbi.start[1] = norm->vbi_v_start_1;
1483 f->fmt.vbi.count[1] = f->fmt.vbi.count[0];
1484 f->fmt.vbi.flags = 0; /* VBI_UNSYNC VBI_INTERLACED */
1488 static int saa7134_g_fmt(struct saa7134_dev *dev, struct saa7134_fh *fh,
1489 struct v4l2_format *f)
1492 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1493 memset(&f->fmt.pix,0,sizeof(f->fmt.pix));
1494 f->fmt.pix.width = fh->width;
1495 f->fmt.pix.height = fh->height;
1496 f->fmt.pix.field = fh->cap.field;
1497 f->fmt.pix.pixelformat = fh->fmt->fourcc;
1498 f->fmt.pix.bytesperline =
1499 (f->fmt.pix.width * fh->fmt->depth) >> 3;
1500 f->fmt.pix.sizeimage =
1501 f->fmt.pix.height * f->fmt.pix.bytesperline;
1503 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
1504 if (saa7134_no_overlay > 0) {
1505 printk ("V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
1508 f->fmt.win = fh->win;
1510 case V4L2_BUF_TYPE_VBI_CAPTURE:
1511 saa7134_vbi_fmt(dev,f);
1518 static int saa7134_try_fmt(struct saa7134_dev *dev, struct saa7134_fh *fh,
1519 struct v4l2_format *f)
1524 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1526 struct saa7134_format *fmt;
1527 enum v4l2_field field;
1528 unsigned int maxw, maxh;
1530 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1534 field = f->fmt.pix.field;
1535 maxw = min(dev->crop_current.width*4, dev->crop_bounds.width);
1536 maxh = min(dev->crop_current.height*4, dev->crop_bounds.height);
1538 if (V4L2_FIELD_ANY == field) {
1539 field = (f->fmt.pix.height > maxh/2)
1540 ? V4L2_FIELD_INTERLACED
1541 : V4L2_FIELD_BOTTOM;
1544 case V4L2_FIELD_TOP:
1545 case V4L2_FIELD_BOTTOM:
1548 case V4L2_FIELD_INTERLACED:
1554 f->fmt.pix.field = field;
1555 if (f->fmt.pix.width < 48)
1556 f->fmt.pix.width = 48;
1557 if (f->fmt.pix.height < 32)
1558 f->fmt.pix.height = 32;
1559 if (f->fmt.pix.width > maxw)
1560 f->fmt.pix.width = maxw;
1561 if (f->fmt.pix.height > maxh)
1562 f->fmt.pix.height = maxh;
1563 f->fmt.pix.width &= ~0x03;
1564 f->fmt.pix.bytesperline =
1565 (f->fmt.pix.width * fmt->depth) >> 3;
1566 f->fmt.pix.sizeimage =
1567 f->fmt.pix.height * f->fmt.pix.bytesperline;
1571 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
1572 if (saa7134_no_overlay > 0) {
1573 printk ("V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
1576 err = verify_preview(dev,&f->fmt.win);
1580 case V4L2_BUF_TYPE_VBI_CAPTURE:
1581 saa7134_vbi_fmt(dev,f);
1588 static int saa7134_s_fmt(struct saa7134_dev *dev, struct saa7134_fh *fh,
1589 struct v4l2_format *f)
1591 unsigned long flags;
1595 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1596 err = saa7134_try_fmt(dev,fh,f);
1600 fh->fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1601 fh->width = f->fmt.pix.width;
1602 fh->height = f->fmt.pix.height;
1603 fh->cap.field = f->fmt.pix.field;
1605 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
1606 if (saa7134_no_overlay > 0) {
1607 printk ("V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
1610 err = verify_preview(dev,&f->fmt.win);
1614 mutex_lock(&dev->lock);
1615 fh->win = f->fmt.win;
1616 fh->nclips = f->fmt.win.clipcount;
1619 if (copy_from_user(fh->clips,f->fmt.win.clips,
1620 sizeof(struct v4l2_clip)*fh->nclips)) {
1621 mutex_unlock(&dev->lock);
1625 if (res_check(fh, RESOURCE_OVERLAY)) {
1626 spin_lock_irqsave(&dev->slock,flags);
1627 stop_preview(dev,fh);
1628 start_preview(dev,fh);
1629 spin_unlock_irqrestore(&dev->slock,flags);
1631 mutex_unlock(&dev->lock);
1633 case V4L2_BUF_TYPE_VBI_CAPTURE:
1634 saa7134_vbi_fmt(dev,f);
1641 int saa7134_common_ioctl(struct saa7134_dev *dev,
1642 unsigned int cmd, void *arg)
1647 case VIDIOC_QUERYCTRL:
1649 const struct v4l2_queryctrl *ctrl;
1650 struct v4l2_queryctrl *c = arg;
1652 if ((c->id < V4L2_CID_BASE ||
1653 c->id >= V4L2_CID_LASTP1) &&
1654 (c->id < V4L2_CID_PRIVATE_BASE ||
1655 c->id >= V4L2_CID_PRIVATE_LASTP1))
1657 ctrl = ctrl_by_id(c->id);
1658 *c = (NULL != ctrl) ? *ctrl : no_ctrl;
1662 return get_control(dev,arg);
1665 mutex_lock(&dev->lock);
1666 err = set_control(dev,NULL,arg);
1667 mutex_unlock(&dev->lock);
1670 /* --- input switching --------------------------------------- */
1671 case VIDIOC_ENUMINPUT:
1673 struct v4l2_input *i = arg;
1677 if (n >= SAA7134_INPUT_MAX)
1679 if (NULL == card_in(dev,i->index).name)
1681 memset(i,0,sizeof(*i));
1683 i->type = V4L2_INPUT_TYPE_CAMERA;
1684 strcpy(i->name,card_in(dev,n).name);
1685 if (card_in(dev,n).tv)
1686 i->type = V4L2_INPUT_TYPE_TUNER;
1688 if (n == dev->ctl_input) {
1689 int v1 = saa_readb(SAA7134_STATUS_VIDEO1);
1690 int v2 = saa_readb(SAA7134_STATUS_VIDEO2);
1692 if (0 != (v1 & 0x40))
1693 i->status |= V4L2_IN_ST_NO_H_LOCK;
1694 if (0 != (v2 & 0x40))
1695 i->status |= V4L2_IN_ST_NO_SYNC;
1696 if (0 != (v2 & 0x0e))
1697 i->status |= V4L2_IN_ST_MACROVISION;
1699 for (n = 0; n < TVNORMS; n++)
1700 i->std |= tvnorms[n].id;
1703 case VIDIOC_G_INPUT:
1706 *i = dev->ctl_input;
1709 case VIDIOC_S_INPUT:
1713 if (*i < 0 || *i >= SAA7134_INPUT_MAX)
1715 if (NULL == card_in(dev,*i).name)
1717 mutex_lock(&dev->lock);
1719 mutex_unlock(&dev->lock);
1726 EXPORT_SYMBOL(saa7134_common_ioctl);
1729 * This function is _not_ called directly, but from
1730 * video_generic_ioctl (and maybe others). userspace
1731 * copying is done already, arg is a kernel pointer.
1733 static int video_do_ioctl(struct inode *inode, struct file *file,
1734 unsigned int cmd, void *arg)
1736 struct saa7134_fh *fh = file->private_data;
1737 struct saa7134_dev *dev = fh->dev;
1738 unsigned long flags;
1741 if (video_debug > 1)
1742 v4l_print_ioctl(dev->name,cmd);
1747 case VIDIOC_S_INPUT:
1748 case VIDIOC_S_TUNER:
1749 case VIDIOC_S_FREQUENCY:
1750 err = v4l2_prio_check(&dev->prio,&fh->prio);
1756 case VIDIOC_QUERYCAP:
1758 struct v4l2_capability *cap = arg;
1759 unsigned int tuner_type = dev->tuner_type;
1761 memset(cap,0,sizeof(*cap));
1762 strcpy(cap->driver, "saa7134");
1763 strlcpy(cap->card, saa7134_boards[dev->board].name,
1765 sprintf(cap->bus_info,"PCI:%s",pci_name(dev->pci));
1766 cap->version = SAA7134_VERSION_CODE;
1768 V4L2_CAP_VIDEO_CAPTURE |
1769 V4L2_CAP_VBI_CAPTURE |
1770 V4L2_CAP_READWRITE |
1771 V4L2_CAP_STREAMING |
1773 if (saa7134_no_overlay <= 0) {
1774 cap->capabilities |= V4L2_CAP_VIDEO_OVERLAY;
1777 if ((tuner_type == TUNER_ABSENT) || (tuner_type == UNSET))
1778 cap->capabilities &= ~V4L2_CAP_TUNER;
1783 /* --- tv standards ------------------------------------------ */
1784 case VIDIOC_ENUMSTD:
1786 struct v4l2_standard *e = arg;
1792 err = v4l2_video_std_construct(e, tvnorms[e->index].id,
1793 tvnorms[e->index].name);
1801 v4l2_std_id *id = arg;
1803 *id = dev->tvnorm->id;
1808 v4l2_std_id *id = arg;
1812 for (i = 0; i < TVNORMS; i++)
1813 if (*id == tvnorms[i].id)
1816 for (i = 0; i < TVNORMS; i++)
1817 if (*id & tvnorms[i].id)
1821 if ((*id & V4L2_STD_SECAM) && (secam[0] != '-')) {
1822 if (secam[0] == 'L' || secam[0] == 'l') {
1823 if (secam[1] == 'C' || secam[1] == 'c')
1824 fixup = V4L2_STD_SECAM_LC;
1826 fixup = V4L2_STD_SECAM_L;
1828 if (secam[0] == 'D' || secam[0] == 'd')
1829 fixup = V4L2_STD_SECAM_DK;
1831 fixup = V4L2_STD_SECAM;
1833 for (i = 0; i < TVNORMS; i++)
1834 if (fixup == tvnorms[i].id)
1837 mutex_lock(&dev->lock);
1838 if (res_check(fh, RESOURCE_OVERLAY)) {
1839 spin_lock_irqsave(&dev->slock,flags);
1840 stop_preview(dev,fh);
1841 spin_unlock_irqrestore(&dev->slock, flags);
1843 set_tvnorm(dev,&tvnorms[i]);
1845 spin_lock_irqsave(&dev->slock, flags);
1846 start_preview(dev,fh);
1847 spin_unlock_irqrestore(&dev->slock,flags);
1849 set_tvnorm(dev,&tvnorms[i]);
1850 saa7134_tvaudio_do_scan(dev);
1851 mutex_unlock(&dev->lock);
1855 case VIDIOC_CROPCAP:
1857 struct v4l2_cropcap *cap = arg;
1859 if (cap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1860 cap->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
1862 cap->bounds = dev->crop_bounds;
1863 cap->defrect = dev->crop_defrect;
1864 cap->pixelaspect.numerator = 1;
1865 cap->pixelaspect.denominator = 1;
1866 if (dev->tvnorm->id & V4L2_STD_525_60) {
1867 cap->pixelaspect.numerator = 11;
1868 cap->pixelaspect.denominator = 10;
1870 if (dev->tvnorm->id & V4L2_STD_625_50) {
1871 cap->pixelaspect.numerator = 54;
1872 cap->pixelaspect.denominator = 59;
1879 struct v4l2_crop * crop = arg;
1881 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1882 crop->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
1884 crop->c = dev->crop_current;
1889 struct v4l2_crop *crop = arg;
1890 struct v4l2_rect *b = &dev->crop_bounds;
1892 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1893 crop->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
1895 if (crop->c.height < 0)
1897 if (crop->c.width < 0)
1900 if (res_locked(fh->dev,RESOURCE_OVERLAY))
1902 if (res_locked(fh->dev,RESOURCE_VIDEO))
1905 if (crop->c.top < b->top)
1906 crop->c.top = b->top;
1907 if (crop->c.top > b->top + b->height)
1908 crop->c.top = b->top + b->height;
1909 if (crop->c.height > b->top - crop->c.top + b->height)
1910 crop->c.height = b->top - crop->c.top + b->height;
1912 if (crop->c.left < b->left)
1913 crop->c.left = b->left;
1914 if (crop->c.left > b->left + b->width)
1915 crop->c.left = b->left + b->width;
1916 if (crop->c.width > b->left - crop->c.left + b->width)
1917 crop->c.width = b->left - crop->c.left + b->width;
1919 dev->crop_current = crop->c;
1923 /* --- tuner ioctls ------------------------------------------ */
1924 case VIDIOC_G_TUNER:
1926 struct v4l2_tuner *t = arg;
1931 memset(t,0,sizeof(*t));
1932 for (n = 0; n < SAA7134_INPUT_MAX; n++)
1933 if (card_in(dev,n).tv)
1935 if (NULL != card_in(dev,n).name) {
1936 strcpy(t->name, "Television");
1937 t->type = V4L2_TUNER_ANALOG_TV;
1938 t->capability = V4L2_TUNER_CAP_NORM |
1939 V4L2_TUNER_CAP_STEREO |
1940 V4L2_TUNER_CAP_LANG1 |
1941 V4L2_TUNER_CAP_LANG2;
1942 t->rangehigh = 0xffffffffUL;
1943 t->rxsubchans = saa7134_tvaudio_getstereo(dev);
1944 t->audmode = saa7134_tvaudio_rx2mode(t->rxsubchans);
1946 if (0 != (saa_readb(SAA7134_STATUS_VIDEO1) & 0x03))
1950 case VIDIOC_S_TUNER:
1952 struct v4l2_tuner *t = arg;
1955 mode = dev->thread.mode;
1956 if (UNSET == mode) {
1957 rx = saa7134_tvaudio_getstereo(dev);
1958 mode = saa7134_tvaudio_rx2mode(t->rxsubchans);
1960 if (mode != t->audmode) {
1961 dev->thread.mode = t->audmode;
1965 case VIDIOC_G_FREQUENCY:
1967 struct v4l2_frequency *f = arg;
1969 memset(f,0,sizeof(*f));
1970 f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1971 f->frequency = dev->ctl_freq;
1974 case VIDIOC_S_FREQUENCY:
1976 struct v4l2_frequency *f = arg;
1980 if (0 == fh->radio && V4L2_TUNER_ANALOG_TV != f->type)
1982 if (1 == fh->radio && V4L2_TUNER_RADIO != f->type)
1984 mutex_lock(&dev->lock);
1985 dev->ctl_freq = f->frequency;
1987 saa7134_i2c_call_clients(dev,VIDIOC_S_FREQUENCY,f);
1989 saa7134_tvaudio_do_scan(dev);
1990 mutex_unlock(&dev->lock);
1994 /* --- control ioctls ---------------------------------------- */
1995 case VIDIOC_ENUMINPUT:
1996 case VIDIOC_G_INPUT:
1997 case VIDIOC_S_INPUT:
1998 case VIDIOC_QUERYCTRL:
2001 return saa7134_common_ioctl(dev, cmd, arg);
2003 case VIDIOC_G_AUDIO:
2005 struct v4l2_audio *a = arg;
2007 memset(a,0,sizeof(*a));
2008 strcpy(a->name,"audio");
2011 case VIDIOC_S_AUDIO:
2015 struct v4l2_captureparm *parm = arg;
2016 memset(parm,0,sizeof(*parm));
2020 case VIDIOC_G_PRIORITY:
2022 enum v4l2_priority *p = arg;
2024 *p = v4l2_prio_max(&dev->prio);
2027 case VIDIOC_S_PRIORITY:
2029 enum v4l2_priority *prio = arg;
2031 return v4l2_prio_change(&dev->prio, &fh->prio, *prio);
2034 /* --- preview ioctls ---------------------------------------- */
2035 case VIDIOC_ENUM_FMT:
2037 struct v4l2_fmtdesc *f = arg;
2038 enum v4l2_buf_type type;
2044 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2045 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
2046 if (saa7134_no_overlay > 0) {
2047 printk ("V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
2050 if (index >= FORMATS)
2052 if (f->type == V4L2_BUF_TYPE_VIDEO_OVERLAY &&
2053 formats[index].planar)
2055 memset(f,0,sizeof(*f));
2058 strlcpy(f->description,formats[index].name,sizeof(f->description));
2059 f->pixelformat = formats[index].fourcc;
2061 case V4L2_BUF_TYPE_VBI_CAPTURE:
2064 memset(f,0,sizeof(*f));
2067 f->pixelformat = V4L2_PIX_FMT_GREY;
2068 strcpy(f->description,"vbi data");
2077 struct v4l2_framebuffer *fb = arg;
2080 fb->capability = V4L2_FBUF_CAP_LIST_CLIPPING;
2085 struct v4l2_framebuffer *fb = arg;
2086 struct saa7134_format *fmt;
2088 if(!capable(CAP_SYS_ADMIN) &&
2089 !capable(CAP_SYS_RAWIO))
2093 fmt = format_by_fourcc(fb->fmt.pixelformat);
2100 if (0 == dev->ovbuf.fmt.bytesperline)
2101 dev->ovbuf.fmt.bytesperline =
2102 dev->ovbuf.fmt.width*fmt->depth/8;
2105 case VIDIOC_OVERLAY:
2110 if (saa7134_no_overlay > 0) {
2111 printk ("no_overlay\n");
2115 if (!res_get(dev,fh,RESOURCE_OVERLAY))
2117 spin_lock_irqsave(&dev->slock,flags);
2118 start_preview(dev,fh);
2119 spin_unlock_irqrestore(&dev->slock,flags);
2122 if (!res_check(fh, RESOURCE_OVERLAY))
2124 spin_lock_irqsave(&dev->slock,flags);
2125 stop_preview(dev,fh);
2126 spin_unlock_irqrestore(&dev->slock,flags);
2127 res_free(dev,fh,RESOURCE_OVERLAY);
2132 /* --- capture ioctls ---------------------------------------- */
2135 struct v4l2_format *f = arg;
2136 return saa7134_g_fmt(dev,fh,f);
2140 struct v4l2_format *f = arg;
2141 return saa7134_s_fmt(dev,fh,f);
2143 case VIDIOC_TRY_FMT:
2145 struct v4l2_format *f = arg;
2146 return saa7134_try_fmt(dev,fh,f);
2148 #ifdef CONFIG_VIDEO_V4L1_COMPAT
2150 return videobuf_cgmbuf(saa7134_queue(fh), arg, gbuffers);
2152 case VIDIOC_REQBUFS:
2153 return videobuf_reqbufs(saa7134_queue(fh),arg);
2155 case VIDIOC_QUERYBUF:
2156 return videobuf_querybuf(saa7134_queue(fh),arg);
2159 return videobuf_qbuf(saa7134_queue(fh),arg);
2162 return videobuf_dqbuf(saa7134_queue(fh),arg,
2163 file->f_flags & O_NONBLOCK);
2165 case VIDIOC_STREAMON:
2167 int res = saa7134_resource(fh);
2169 if (!res_get(dev,fh,res))
2171 return videobuf_streamon(saa7134_queue(fh));
2173 case VIDIOC_STREAMOFF:
2175 int res = saa7134_resource(fh);
2177 err = videobuf_streamoff(saa7134_queue(fh));
2180 res_free(dev,fh,res);
2185 return v4l_compat_translate_ioctl(inode,file,cmd,arg,
2191 static int video_ioctl(struct inode *inode, struct file *file,
2192 unsigned int cmd, unsigned long arg)
2194 return video_usercopy(inode, file, cmd, arg, video_do_ioctl);
2197 static int radio_do_ioctl(struct inode *inode, struct file *file,
2198 unsigned int cmd, void *arg)
2200 struct saa7134_fh *fh = file->private_data;
2201 struct saa7134_dev *dev = fh->dev;
2203 if (video_debug > 1)
2204 v4l_print_ioctl(dev->name,cmd);
2206 case VIDIOC_QUERYCAP:
2208 struct v4l2_capability *cap = arg;
2210 memset(cap,0,sizeof(*cap));
2211 strcpy(cap->driver, "saa7134");
2212 strlcpy(cap->card, saa7134_boards[dev->board].name,
2214 sprintf(cap->bus_info,"PCI:%s",pci_name(dev->pci));
2215 cap->version = SAA7134_VERSION_CODE;
2216 cap->capabilities = V4L2_CAP_TUNER;
2219 case VIDIOC_G_TUNER:
2221 struct v4l2_tuner *t = arg;
2226 memset(t,0,sizeof(*t));
2227 strcpy(t->name, "Radio");
2228 t->type = V4L2_TUNER_RADIO;
2230 saa7134_i2c_call_clients(dev, VIDIOC_G_TUNER, t);
2231 if (dev->input->amux == TV) {
2232 t->signal = 0xf800 - ((saa_readb(0x581) & 0x1f) << 11);
2233 t->rxsubchans = (saa_readb(0x529) & 0x08) ?
2234 V4L2_TUNER_SUB_STEREO : V4L2_TUNER_SUB_MONO;
2238 case VIDIOC_S_TUNER:
2240 struct v4l2_tuner *t = arg;
2245 saa7134_i2c_call_clients(dev,VIDIOC_S_TUNER,t);
2249 case VIDIOC_ENUMINPUT:
2251 struct v4l2_input *i = arg;
2255 strcpy(i->name,"Radio");
2256 i->type = V4L2_INPUT_TYPE_TUNER;
2259 case VIDIOC_G_INPUT:
2265 case VIDIOC_G_AUDIO:
2267 struct v4l2_audio *a = arg;
2269 memset(a,0,sizeof(*a));
2270 strcpy(a->name,"Radio");
2275 v4l2_std_id *id = arg;
2279 case VIDIOC_S_AUDIO:
2280 case VIDIOC_S_INPUT:
2284 case VIDIOC_QUERYCTRL:
2286 const struct v4l2_queryctrl *ctrl;
2287 struct v4l2_queryctrl *c = arg;
2289 if (c->id < V4L2_CID_BASE ||
2290 c->id >= V4L2_CID_LASTP1)
2292 if (c->id == V4L2_CID_AUDIO_MUTE) {
2293 ctrl = ctrl_by_id(c->id);
2302 case VIDIOC_G_FREQUENCY:
2303 case VIDIOC_S_FREQUENCY:
2304 return video_do_ioctl(inode,file,cmd,arg);
2307 return v4l_compat_translate_ioctl(inode,file,cmd,arg,
2313 static int radio_ioctl(struct inode *inode, struct file *file,
2314 unsigned int cmd, unsigned long arg)
2316 return video_usercopy(inode, file, cmd, arg, radio_do_ioctl);
2319 static const struct file_operations video_fops =
2321 .owner = THIS_MODULE,
2323 .release = video_release,
2327 .ioctl = video_ioctl,
2328 .compat_ioctl = v4l_compat_ioctl32,
2329 .llseek = no_llseek,
2332 static const struct file_operations radio_fops =
2334 .owner = THIS_MODULE,
2336 .release = video_release,
2337 .ioctl = radio_ioctl,
2338 .compat_ioctl = v4l_compat_ioctl32,
2339 .llseek = no_llseek,
2342 /* ----------------------------------------------------------- */
2343 /* exported stuff */
2345 struct video_device saa7134_video_template =
2347 .name = "saa7134-video",
2348 .type = VID_TYPE_CAPTURE|VID_TYPE_TUNER|
2349 VID_TYPE_CLIPPING|VID_TYPE_SCALES,
2351 .fops = &video_fops,
2355 struct video_device saa7134_vbi_template =
2357 .name = "saa7134-vbi",
2358 .type = VID_TYPE_TUNER|VID_TYPE_TELETEXT,
2360 .fops = &video_fops,
2364 struct video_device saa7134_radio_template =
2366 .name = "saa7134-radio",
2367 .type = VID_TYPE_TUNER,
2369 .fops = &radio_fops,
2373 int saa7134_video_init1(struct saa7134_dev *dev)
2375 /* sanitycheck insmod options */
2376 if (gbuffers < 2 || gbuffers > VIDEO_MAX_FRAME)
2378 if (gbufsize < 0 || gbufsize > gbufsize_max)
2379 gbufsize = gbufsize_max;
2380 gbufsize = (gbufsize + PAGE_SIZE - 1) & PAGE_MASK;
2382 /* put some sensible defaults into the data structures ... */
2383 dev->ctl_bright = ctrl_by_id(V4L2_CID_BRIGHTNESS)->default_value;
2384 dev->ctl_contrast = ctrl_by_id(V4L2_CID_CONTRAST)->default_value;
2385 dev->ctl_hue = ctrl_by_id(V4L2_CID_HUE)->default_value;
2386 dev->ctl_saturation = ctrl_by_id(V4L2_CID_SATURATION)->default_value;
2387 dev->ctl_volume = ctrl_by_id(V4L2_CID_AUDIO_VOLUME)->default_value;
2388 dev->ctl_mute = 1; // ctrl_by_id(V4L2_CID_AUDIO_MUTE)->default_value;
2389 dev->ctl_invert = ctrl_by_id(V4L2_CID_PRIVATE_INVERT)->default_value;
2390 dev->ctl_automute = ctrl_by_id(V4L2_CID_PRIVATE_AUTOMUTE)->default_value;
2392 if (dev->tda9887_conf && dev->ctl_automute)
2393 dev->tda9887_conf |= TDA9887_AUTOMUTE;
2396 INIT_LIST_HEAD(&dev->video_q.queue);
2397 init_timer(&dev->video_q.timeout);
2398 dev->video_q.timeout.function = saa7134_buffer_timeout;
2399 dev->video_q.timeout.data = (unsigned long)(&dev->video_q);
2400 dev->video_q.dev = dev;
2402 if (saa7134_boards[dev->board].video_out) {
2403 /* enable video output */
2404 int vo = saa7134_boards[dev->board].video_out;
2406 unsigned int vid_port_opts = saa7134_boards[dev->board].vid_port_opts;
2407 saa_writeb(SAA7134_VIDEO_PORT_CTRL0, video_out[vo][0]);
2408 video_reg = video_out[vo][1];
2409 if (vid_port_opts & SET_T_CODE_POLARITY_NON_INVERTED)
2410 video_reg &= ~VP_T_CODE_P_INVERTED;
2411 saa_writeb(SAA7134_VIDEO_PORT_CTRL1, video_reg);
2412 saa_writeb(SAA7134_VIDEO_PORT_CTRL2, video_out[vo][2]);
2413 saa_writeb(SAA7134_VIDEO_PORT_CTRL3, video_out[vo][3]);
2414 saa_writeb(SAA7134_VIDEO_PORT_CTRL4, video_out[vo][4]);
2415 video_reg = video_out[vo][5];
2416 if (vid_port_opts & SET_CLOCK_NOT_DELAYED)
2417 video_reg &= ~VP_CLK_CTRL2_DELAYED;
2418 if (vid_port_opts & SET_CLOCK_INVERTED)
2419 video_reg |= VP_CLK_CTRL1_INVERTED;
2420 saa_writeb(SAA7134_VIDEO_PORT_CTRL5, video_reg);
2421 video_reg = video_out[vo][6];
2422 if (vid_port_opts & SET_VSYNC_OFF) {
2423 video_reg &= ~VP_VS_TYPE_MASK;
2424 video_reg |= VP_VS_TYPE_OFF;
2426 saa_writeb(SAA7134_VIDEO_PORT_CTRL6, video_reg);
2427 saa_writeb(SAA7134_VIDEO_PORT_CTRL7, video_out[vo][7]);
2428 saa_writeb(SAA7134_VIDEO_PORT_CTRL8, video_out[vo][8]);
2434 int saa7134_video_init2(struct saa7134_dev *dev)
2437 set_tvnorm(dev,&tvnorms[0]);
2439 saa7134_tvaudio_setmute(dev);
2440 saa7134_tvaudio_setvolume(dev,dev->ctl_volume);
2444 void saa7134_irq_video_intl(struct saa7134_dev *dev)
2446 static const char *st[] = {
2447 "(no signal)", "NTSC", "PAL", "SECAM" };
2450 st1 = saa_readb(SAA7134_STATUS_VIDEO1);
2451 st2 = saa_readb(SAA7134_STATUS_VIDEO2);
2452 dprintk("DCSDT: pll: %s, sync: %s, norm: %s\n",
2453 (st1 & 0x40) ? "not locked" : "locked",
2454 (st2 & 0x40) ? "no" : "yes",
2456 dev->nosignal = (st1 & 0x40) || (st2 & 0x40);
2458 if (dev->nosignal) {
2459 /* no video signal -> mute audio */
2460 if (dev->ctl_automute)
2462 saa7134_tvaudio_setmute(dev);
2463 saa_setb(SAA7134_SYNC_CTRL, 0x20);
2465 /* wake up tvaudio audio carrier scan thread */
2466 saa7134_tvaudio_do_scan(dev);
2468 saa_clearb(SAA7134_SYNC_CTRL, 0x20);
2470 if (dev->mops && dev->mops->signal_change)
2471 dev->mops->signal_change(dev);
2474 void saa7134_irq_video_done(struct saa7134_dev *dev, unsigned long status)
2476 enum v4l2_field field;
2478 spin_lock(&dev->slock);
2479 if (dev->video_q.curr) {
2480 dev->video_fieldcount++;
2481 field = dev->video_q.curr->vb.field;
2482 if (V4L2_FIELD_HAS_BOTH(field)) {
2483 /* make sure we have seen both fields */
2484 if ((status & 0x10) == 0x00) {
2485 dev->video_q.curr->top_seen = 1;
2488 if (!dev->video_q.curr->top_seen)
2490 } else if (field == V4L2_FIELD_TOP) {
2491 if ((status & 0x10) != 0x10)
2493 } else if (field == V4L2_FIELD_BOTTOM) {
2494 if ((status & 0x10) != 0x00)
2497 dev->video_q.curr->vb.field_count = dev->video_fieldcount;
2498 saa7134_buffer_finish(dev,&dev->video_q,STATE_DONE);
2500 saa7134_buffer_next(dev,&dev->video_q);
2503 spin_unlock(&dev->slock);
2506 /* ----------------------------------------------------------- */