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 unsigned int video_debug;
42 static unsigned int gbuffers = 8;
43 static unsigned int noninterlaced; /* 0 */
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&0x04) \
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 dprintk("set tv norm = %s\n",norm->name);
549 dev->crop_bounds.left = norm->h_start;
550 dev->crop_defrect.left = norm->h_start;
551 dev->crop_bounds.width = norm->h_stop - norm->h_start +1;
552 dev->crop_defrect.width = norm->h_stop - norm->h_start +1;
554 dev->crop_bounds.top = (norm->vbi_v_stop_0+1)*2;
555 dev->crop_defrect.top = norm->video_v_start*2;
556 dev->crop_bounds.height = ((norm->id & V4L2_STD_525_60) ? 524 : 624)
557 - dev->crop_bounds.top;
558 dev->crop_defrect.height = (norm->video_v_stop - norm->video_v_start +1)*2;
560 dev->crop_current = dev->crop_defrect;
562 saa7134_set_tvnorm_hw(dev);
565 static void video_mux(struct saa7134_dev *dev, int input)
567 dprintk("video input = %d [%s]\n", input, card_in(dev, input).name);
568 dev->ctl_input = input;
569 set_tvnorm(dev, dev->tvnorm);
570 saa7134_tvaudio_setinput(dev, &card_in(dev, input));
574 static void saa7134_set_decoder(struct saa7134_dev *dev)
576 int luma_control, sync_control, mux;
578 struct saa7134_tvnorm *norm = dev->tvnorm;
579 mux = card_in(dev, dev->ctl_input).vmux;
581 luma_control = norm->luma_control;
582 sync_control = norm->sync_control;
585 luma_control |= 0x80; /* svideo */
586 if (noninterlaced || dev->nosignal)
587 sync_control |= 0x20;
589 /* setup video decoder */
590 saa_writeb(SAA7134_INCR_DELAY, 0x08);
591 saa_writeb(SAA7134_ANALOG_IN_CTRL1, 0xc0 | mux);
592 saa_writeb(SAA7134_ANALOG_IN_CTRL2, 0x00);
594 saa_writeb(SAA7134_ANALOG_IN_CTRL3, 0x90);
595 saa_writeb(SAA7134_ANALOG_IN_CTRL4, 0x90);
596 saa_writeb(SAA7134_HSYNC_START, 0xeb);
597 saa_writeb(SAA7134_HSYNC_STOP, 0xe0);
598 saa_writeb(SAA7134_SOURCE_TIMING1, norm->src_timing);
600 saa_writeb(SAA7134_SYNC_CTRL, sync_control);
601 saa_writeb(SAA7134_LUMA_CTRL, luma_control);
602 saa_writeb(SAA7134_DEC_LUMA_BRIGHT, dev->ctl_bright);
604 saa_writeb(SAA7134_DEC_LUMA_CONTRAST,
605 dev->ctl_invert ? -dev->ctl_contrast : dev->ctl_contrast);
607 saa_writeb(SAA7134_DEC_CHROMA_SATURATION,
608 dev->ctl_invert ? -dev->ctl_saturation : dev->ctl_saturation);
610 saa_writeb(SAA7134_DEC_CHROMA_HUE, dev->ctl_hue);
611 saa_writeb(SAA7134_CHROMA_CTRL1, norm->chroma_ctrl1);
612 saa_writeb(SAA7134_CHROMA_GAIN, norm->chroma_gain);
614 saa_writeb(SAA7134_CHROMA_CTRL2, norm->chroma_ctrl2);
615 saa_writeb(SAA7134_MODE_DELAY_CTRL, 0x00);
617 saa_writeb(SAA7134_ANALOG_ADC, 0x01);
618 saa_writeb(SAA7134_VGATE_START, 0x11);
619 saa_writeb(SAA7134_VGATE_STOP, 0xfe);
620 saa_writeb(SAA7134_MISC_VGATE_MSB, norm->vgate_misc);
621 saa_writeb(SAA7134_RAW_DATA_GAIN, 0x40);
622 saa_writeb(SAA7134_RAW_DATA_OFFSET, 0x80);
625 void saa7134_set_tvnorm_hw(struct saa7134_dev *dev)
627 saa7134_set_decoder(dev);
629 if (card_in(dev, dev->ctl_input).tv)
630 saa7134_i2c_call_clients(dev, VIDIOC_S_STD, &dev->tvnorm->id);
631 /* Set the correct norm for the saa6752hs. This function
632 does nothing if there is no saa6752hs. */
633 saa7134_i2c_call_saa6752(dev, VIDIOC_S_STD, &dev->tvnorm->id);
636 static void set_h_prescale(struct saa7134_dev *dev, int task, int prescale)
638 static const struct {
645 /* XPSC XACL XC2_1 XDCG VPFY */
657 static const int count = ARRAY_SIZE(vals);
660 for (i = 0; i < count; i++)
661 if (vals[i].xpsc == prescale)
666 saa_writeb(SAA7134_H_PRESCALE(task), vals[i].xpsc);
667 saa_writeb(SAA7134_ACC_LENGTH(task), vals[i].xacl);
668 saa_writeb(SAA7134_LEVEL_CTRL(task),
669 (vals[i].xc2_1 << 3) | (vals[i].xdcg));
670 saa_andorb(SAA7134_FIR_PREFILTER_CTRL(task), 0x0f,
671 (vals[i].vpfy << 2) | vals[i].vpfy);
674 static void set_v_scale(struct saa7134_dev *dev, int task, int yscale)
678 saa_writeb(SAA7134_V_SCALE_RATIO1(task), yscale & 0xff);
679 saa_writeb(SAA7134_V_SCALE_RATIO2(task), yscale >> 8);
681 mirror = (dev->ctl_mirror) ? 0x02 : 0x00;
684 dprintk("yscale LPI yscale=%d\n",yscale);
685 saa_writeb(SAA7134_V_FILTER(task), 0x00 | mirror);
686 saa_writeb(SAA7134_LUMA_CONTRAST(task), 0x40);
687 saa_writeb(SAA7134_CHROMA_SATURATION(task), 0x40);
690 val = 0x40 * 1024 / yscale;
691 dprintk("yscale ACM yscale=%d val=0x%x\n",yscale,val);
692 saa_writeb(SAA7134_V_FILTER(task), 0x01 | mirror);
693 saa_writeb(SAA7134_LUMA_CONTRAST(task), val);
694 saa_writeb(SAA7134_CHROMA_SATURATION(task), val);
696 saa_writeb(SAA7134_LUMA_BRIGHT(task), 0x80);
699 static void set_size(struct saa7134_dev *dev, int task,
700 int width, int height, int interlace)
702 int prescale,xscale,yscale,y_even,y_odd;
703 int h_start, h_stop, v_start, v_stop;
704 int div = interlace ? 2 : 1;
706 /* setup video scaler */
707 h_start = dev->crop_current.left;
708 v_start = dev->crop_current.top/2;
709 h_stop = (dev->crop_current.left + dev->crop_current.width -1);
710 v_stop = (dev->crop_current.top + dev->crop_current.height -1)/2;
712 saa_writeb(SAA7134_VIDEO_H_START1(task), h_start & 0xff);
713 saa_writeb(SAA7134_VIDEO_H_START2(task), h_start >> 8);
714 saa_writeb(SAA7134_VIDEO_H_STOP1(task), h_stop & 0xff);
715 saa_writeb(SAA7134_VIDEO_H_STOP2(task), h_stop >> 8);
716 saa_writeb(SAA7134_VIDEO_V_START1(task), v_start & 0xff);
717 saa_writeb(SAA7134_VIDEO_V_START2(task), v_start >> 8);
718 saa_writeb(SAA7134_VIDEO_V_STOP1(task), v_stop & 0xff);
719 saa_writeb(SAA7134_VIDEO_V_STOP2(task), v_stop >> 8);
721 prescale = dev->crop_current.width / width;
724 xscale = 1024 * dev->crop_current.width / prescale / width;
725 yscale = 512 * div * dev->crop_current.height / height;
726 dprintk("prescale=%d xscale=%d yscale=%d\n",prescale,xscale,yscale);
727 set_h_prescale(dev,task,prescale);
728 saa_writeb(SAA7134_H_SCALE_INC1(task), xscale & 0xff);
729 saa_writeb(SAA7134_H_SCALE_INC2(task), xscale >> 8);
730 set_v_scale(dev,task,yscale);
732 saa_writeb(SAA7134_VIDEO_PIXELS1(task), width & 0xff);
733 saa_writeb(SAA7134_VIDEO_PIXELS2(task), width >> 8);
734 saa_writeb(SAA7134_VIDEO_LINES1(task), height/div & 0xff);
735 saa_writeb(SAA7134_VIDEO_LINES2(task), height/div >> 8);
737 /* deinterlace y offsets */
738 y_odd = dev->ctl_y_odd;
739 y_even = dev->ctl_y_even;
740 saa_writeb(SAA7134_V_PHASE_OFFSET0(task), y_odd);
741 saa_writeb(SAA7134_V_PHASE_OFFSET1(task), y_even);
742 saa_writeb(SAA7134_V_PHASE_OFFSET2(task), y_odd);
743 saa_writeb(SAA7134_V_PHASE_OFFSET3(task), y_even);
746 /* ------------------------------------------------------------------ */
754 static void set_cliplist(struct saa7134_dev *dev, int reg,
755 struct cliplist *cl, int entries, char *name)
760 for (i = 0; i < entries; i++) {
761 winbits |= cl[i].enable;
762 winbits &= ~cl[i].disable;
763 if (i < 15 && cl[i].position == cl[i+1].position)
765 saa_writeb(reg + 0, winbits);
766 saa_writeb(reg + 2, cl[i].position & 0xff);
767 saa_writeb(reg + 3, cl[i].position >> 8);
768 dprintk("clip: %s winbits=%02x pos=%d\n",
769 name,winbits,cl[i].position);
772 for (; reg < 0x400; reg += 8) {
773 saa_writeb(reg+ 0, 0);
774 saa_writeb(reg + 1, 0);
775 saa_writeb(reg + 2, 0);
776 saa_writeb(reg + 3, 0);
780 static int clip_range(int val)
787 /* Sort into smallest position first order */
788 static int cliplist_cmp(const void *a, const void *b)
790 const struct cliplist *cla = a;
791 const struct cliplist *clb = b;
792 if (cla->position < clb->position)
794 if (cla->position > clb->position)
799 static int setup_clipping(struct saa7134_dev *dev, struct v4l2_clip *clips,
800 int nclips, int interlace)
802 struct cliplist col[16], row[16];
803 int cols = 0, rows = 0, i;
804 int div = interlace ? 2 : 1;
806 memset(col, 0, sizeof(col));
807 memset(row, 0, sizeof(row));
808 for (i = 0; i < nclips && i < 8; i++) {
809 col[cols].position = clip_range(clips[i].c.left);
810 col[cols].enable = (1 << i);
812 col[cols].position = clip_range(clips[i].c.left+clips[i].c.width);
813 col[cols].disable = (1 << i);
815 row[rows].position = clip_range(clips[i].c.top / div);
816 row[rows].enable = (1 << i);
818 row[rows].position = clip_range((clips[i].c.top + clips[i].c.height)
820 row[rows].disable = (1 << i);
823 sort(col, cols, sizeof col[0], cliplist_cmp, NULL);
824 sort(row, rows, sizeof row[0], cliplist_cmp, NULL);
825 set_cliplist(dev,0x380,col,cols,"cols");
826 set_cliplist(dev,0x384,row,rows,"rows");
830 static int verify_preview(struct saa7134_dev *dev, struct v4l2_window *win)
832 enum v4l2_field field;
835 if (NULL == dev->ovbuf.base)
837 if (NULL == dev->ovfmt)
839 if (win->w.width < 48 || win->w.height < 32)
841 if (win->clipcount > 2048)
845 maxw = dev->crop_current.width;
846 maxh = dev->crop_current.height;
848 if (V4L2_FIELD_ANY == field) {
849 field = (win->w.height > maxh/2)
850 ? V4L2_FIELD_INTERLACED
855 case V4L2_FIELD_BOTTOM:
858 case V4L2_FIELD_INTERLACED:
865 if (win->w.width > maxw)
867 if (win->w.height > maxh)
868 win->w.height = maxh;
872 static int start_preview(struct saa7134_dev *dev, struct saa7134_fh *fh)
874 unsigned long base,control,bpl;
877 err = verify_preview(dev,&fh->win);
881 dev->ovfield = fh->win.field;
882 dprintk("start_preview %dx%d+%d+%d %s field=%s\n",
883 fh->win.w.width,fh->win.w.height,
884 fh->win.w.left,fh->win.w.top,
885 dev->ovfmt->name,v4l2_field_names[dev->ovfield]);
887 /* setup window + clipping */
888 set_size(dev,TASK_B,fh->win.w.width,fh->win.w.height,
889 V4L2_FIELD_HAS_BOTH(dev->ovfield));
890 setup_clipping(dev,fh->clips,fh->nclips,
891 V4L2_FIELD_HAS_BOTH(dev->ovfield));
893 saa_andorb(SAA7134_DATA_PATH(TASK_B), 0x3f, 0x03);
895 saa_andorb(SAA7134_DATA_PATH(TASK_B), 0x3f, 0x01);
896 saa_writeb(SAA7134_OFMT_VIDEO_B, dev->ovfmt->pm | 0x20);
898 /* dma: setup channel 1 (= Video Task B) */
899 base = (unsigned long)dev->ovbuf.base;
900 base += dev->ovbuf.fmt.bytesperline * fh->win.w.top;
901 base += dev->ovfmt->depth/8 * fh->win.w.left;
902 bpl = dev->ovbuf.fmt.bytesperline;
903 control = SAA7134_RS_CONTROL_BURST_16;
904 if (dev->ovfmt->bswap)
905 control |= SAA7134_RS_CONTROL_BSWAP;
906 if (dev->ovfmt->wswap)
907 control |= SAA7134_RS_CONTROL_WSWAP;
908 if (V4L2_FIELD_HAS_BOTH(dev->ovfield)) {
909 saa_writel(SAA7134_RS_BA1(1),base);
910 saa_writel(SAA7134_RS_BA2(1),base+bpl);
911 saa_writel(SAA7134_RS_PITCH(1),bpl*2);
912 saa_writel(SAA7134_RS_CONTROL(1),control);
914 saa_writel(SAA7134_RS_BA1(1),base);
915 saa_writel(SAA7134_RS_BA2(1),base);
916 saa_writel(SAA7134_RS_PITCH(1),bpl);
917 saa_writel(SAA7134_RS_CONTROL(1),control);
922 saa7134_set_dmabits(dev);
927 static int stop_preview(struct saa7134_dev *dev, struct saa7134_fh *fh)
930 saa7134_set_dmabits(dev);
934 /* ------------------------------------------------------------------ */
936 static int buffer_activate(struct saa7134_dev *dev,
937 struct saa7134_buf *buf,
938 struct saa7134_buf *next)
940 unsigned long base,control,bpl;
941 unsigned long bpl_uv,lines_uv,base2,base3,tmp; /* planar */
943 dprintk("buffer_activate buf=%p\n",buf);
944 buf->vb.state = VIDEOBUF_ACTIVE;
947 set_size(dev,TASK_A,buf->vb.width,buf->vb.height,
948 V4L2_FIELD_HAS_BOTH(buf->vb.field));
950 saa_andorb(SAA7134_DATA_PATH(TASK_A), 0x3f, 0x03);
952 saa_andorb(SAA7134_DATA_PATH(TASK_A), 0x3f, 0x01);
953 saa_writeb(SAA7134_OFMT_VIDEO_A, buf->fmt->pm);
955 /* DMA: setup channel 0 (= Video Task A0) */
956 base = saa7134_buffer_base(buf);
957 if (buf->fmt->planar)
960 bpl = (buf->vb.width * buf->fmt->depth) / 8;
961 control = SAA7134_RS_CONTROL_BURST_16 |
962 SAA7134_RS_CONTROL_ME |
963 (buf->pt->dma >> 12);
965 control |= SAA7134_RS_CONTROL_BSWAP;
967 control |= SAA7134_RS_CONTROL_WSWAP;
968 if (V4L2_FIELD_HAS_BOTH(buf->vb.field)) {
970 saa_writel(SAA7134_RS_BA1(0),base);
971 saa_writel(SAA7134_RS_BA2(0),base+bpl);
972 saa_writel(SAA7134_RS_PITCH(0),bpl*2);
975 saa_writel(SAA7134_RS_BA1(0),base);
976 saa_writel(SAA7134_RS_BA2(0),base);
977 saa_writel(SAA7134_RS_PITCH(0),bpl);
979 saa_writel(SAA7134_RS_CONTROL(0),control);
981 if (buf->fmt->planar) {
982 /* DMA: setup channel 4+5 (= planar task A) */
983 bpl_uv = bpl >> buf->fmt->hshift;
984 lines_uv = buf->vb.height >> buf->fmt->vshift;
985 base2 = base + bpl * buf->vb.height;
986 base3 = base2 + bpl_uv * lines_uv;
987 if (buf->fmt->uvswap)
988 tmp = base2, base2 = base3, base3 = tmp;
989 dprintk("uv: bpl=%ld lines=%ld base2/3=%ld/%ld\n",
990 bpl_uv,lines_uv,base2,base3);
991 if (V4L2_FIELD_HAS_BOTH(buf->vb.field)) {
993 saa_writel(SAA7134_RS_BA1(4),base2);
994 saa_writel(SAA7134_RS_BA2(4),base2+bpl_uv);
995 saa_writel(SAA7134_RS_PITCH(4),bpl_uv*2);
996 saa_writel(SAA7134_RS_BA1(5),base3);
997 saa_writel(SAA7134_RS_BA2(5),base3+bpl_uv);
998 saa_writel(SAA7134_RS_PITCH(5),bpl_uv*2);
1000 /* non-interlaced */
1001 saa_writel(SAA7134_RS_BA1(4),base2);
1002 saa_writel(SAA7134_RS_BA2(4),base2);
1003 saa_writel(SAA7134_RS_PITCH(4),bpl_uv);
1004 saa_writel(SAA7134_RS_BA1(5),base3);
1005 saa_writel(SAA7134_RS_BA2(5),base3);
1006 saa_writel(SAA7134_RS_PITCH(5),bpl_uv);
1008 saa_writel(SAA7134_RS_CONTROL(4),control);
1009 saa_writel(SAA7134_RS_CONTROL(5),control);
1013 saa7134_set_dmabits(dev);
1014 mod_timer(&dev->video_q.timeout, jiffies+BUFFER_TIMEOUT);
1018 static int buffer_prepare(struct videobuf_queue *q,
1019 struct videobuf_buffer *vb,
1020 enum v4l2_field field)
1022 struct saa7134_fh *fh = q->priv_data;
1023 struct saa7134_dev *dev = fh->dev;
1024 struct saa7134_buf *buf = container_of(vb,struct saa7134_buf,vb);
1029 if (NULL == fh->fmt)
1031 if (fh->width < 48 ||
1033 fh->width/4 > dev->crop_current.width ||
1034 fh->height/4 > dev->crop_current.height ||
1035 fh->width > dev->crop_bounds.width ||
1036 fh->height > dev->crop_bounds.height)
1038 size = (fh->width * fh->height * fh->fmt->depth) >> 3;
1039 if (0 != buf->vb.baddr && buf->vb.bsize < size)
1042 dprintk("buffer_prepare [%d,size=%dx%d,bytes=%d,fields=%s,%s]\n",
1043 vb->i,fh->width,fh->height,size,v4l2_field_names[field],
1045 if (buf->vb.width != fh->width ||
1046 buf->vb.height != fh->height ||
1047 buf->vb.size != size ||
1048 buf->vb.field != field ||
1049 buf->fmt != fh->fmt) {
1050 saa7134_dma_free(q,buf);
1053 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
1054 struct videobuf_dmabuf *dma=videobuf_to_dma(&buf->vb);
1056 buf->vb.width = fh->width;
1057 buf->vb.height = fh->height;
1058 buf->vb.size = size;
1059 buf->vb.field = field;
1061 buf->pt = &fh->pt_cap;
1063 err = videobuf_iolock(q,&buf->vb,&dev->ovbuf);
1066 err = saa7134_pgtable_build(dev->pci,buf->pt,
1069 saa7134_buffer_startpage(buf));
1073 buf->vb.state = VIDEOBUF_PREPARED;
1074 buf->activate = buffer_activate;
1078 saa7134_dma_free(q,buf);
1083 buffer_setup(struct videobuf_queue *q, unsigned int *count, unsigned int *size)
1085 struct saa7134_fh *fh = q->priv_data;
1087 *size = fh->fmt->depth * fh->width * fh->height >> 3;
1090 *count = saa7134_buffer_count(*size,*count);
1094 static void buffer_queue(struct videobuf_queue *q, struct videobuf_buffer *vb)
1096 struct saa7134_fh *fh = q->priv_data;
1097 struct saa7134_buf *buf = container_of(vb,struct saa7134_buf,vb);
1099 saa7134_buffer_queue(fh->dev,&fh->dev->video_q,buf);
1102 static void buffer_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
1104 struct saa7134_buf *buf = container_of(vb,struct saa7134_buf,vb);
1106 saa7134_dma_free(q,buf);
1109 static struct videobuf_queue_ops video_qops = {
1110 .buf_setup = buffer_setup,
1111 .buf_prepare = buffer_prepare,
1112 .buf_queue = buffer_queue,
1113 .buf_release = buffer_release,
1116 /* ------------------------------------------------------------------ */
1118 int saa7134_g_ctrl_internal(struct saa7134_dev *dev, struct saa7134_fh *fh, struct v4l2_control *c)
1120 const struct v4l2_queryctrl* ctrl;
1122 ctrl = ctrl_by_id(c->id);
1126 case V4L2_CID_BRIGHTNESS:
1127 c->value = dev->ctl_bright;
1130 c->value = dev->ctl_hue;
1132 case V4L2_CID_CONTRAST:
1133 c->value = dev->ctl_contrast;
1135 case V4L2_CID_SATURATION:
1136 c->value = dev->ctl_saturation;
1138 case V4L2_CID_AUDIO_MUTE:
1139 c->value = dev->ctl_mute;
1141 case V4L2_CID_AUDIO_VOLUME:
1142 c->value = dev->ctl_volume;
1144 case V4L2_CID_PRIVATE_INVERT:
1145 c->value = dev->ctl_invert;
1147 case V4L2_CID_HFLIP:
1148 c->value = dev->ctl_mirror;
1150 case V4L2_CID_PRIVATE_Y_EVEN:
1151 c->value = dev->ctl_y_even;
1153 case V4L2_CID_PRIVATE_Y_ODD:
1154 c->value = dev->ctl_y_odd;
1156 case V4L2_CID_PRIVATE_AUTOMUTE:
1157 c->value = dev->ctl_automute;
1164 EXPORT_SYMBOL_GPL(saa7134_g_ctrl_internal);
1166 static int saa7134_g_ctrl(struct file *file, void *priv, struct v4l2_control *c)
1168 struct saa7134_fh *fh = priv;
1170 return saa7134_g_ctrl_internal(fh->dev, fh, c);
1173 int saa7134_s_ctrl_internal(struct saa7134_dev *dev, struct saa7134_fh *fh, struct v4l2_control *c)
1175 const struct v4l2_queryctrl* ctrl;
1176 unsigned long flags;
1177 int restart_overlay = 0;
1180 /* When called from the empress code fh == NULL.
1181 That needs to be fixed somehow, but for now this is
1184 err = v4l2_prio_check(&dev->prio, &fh->prio);
1190 mutex_lock(&dev->lock);
1192 ctrl = ctrl_by_id(c->id);
1196 dprintk("set_control name=%s val=%d\n",ctrl->name,c->value);
1197 switch (ctrl->type) {
1198 case V4L2_CTRL_TYPE_BOOLEAN:
1199 case V4L2_CTRL_TYPE_MENU:
1200 case V4L2_CTRL_TYPE_INTEGER:
1201 if (c->value < ctrl->minimum)
1202 c->value = ctrl->minimum;
1203 if (c->value > ctrl->maximum)
1204 c->value = ctrl->maximum;
1210 case V4L2_CID_BRIGHTNESS:
1211 dev->ctl_bright = c->value;
1212 saa_writeb(SAA7134_DEC_LUMA_BRIGHT, dev->ctl_bright);
1215 dev->ctl_hue = c->value;
1216 saa_writeb(SAA7134_DEC_CHROMA_HUE, dev->ctl_hue);
1218 case V4L2_CID_CONTRAST:
1219 dev->ctl_contrast = c->value;
1220 saa_writeb(SAA7134_DEC_LUMA_CONTRAST,
1221 dev->ctl_invert ? -dev->ctl_contrast : dev->ctl_contrast);
1223 case V4L2_CID_SATURATION:
1224 dev->ctl_saturation = c->value;
1225 saa_writeb(SAA7134_DEC_CHROMA_SATURATION,
1226 dev->ctl_invert ? -dev->ctl_saturation : dev->ctl_saturation);
1228 case V4L2_CID_AUDIO_MUTE:
1229 dev->ctl_mute = c->value;
1230 saa7134_tvaudio_setmute(dev);
1232 case V4L2_CID_AUDIO_VOLUME:
1233 dev->ctl_volume = c->value;
1234 saa7134_tvaudio_setvolume(dev,dev->ctl_volume);
1236 case V4L2_CID_PRIVATE_INVERT:
1237 dev->ctl_invert = c->value;
1238 saa_writeb(SAA7134_DEC_LUMA_CONTRAST,
1239 dev->ctl_invert ? -dev->ctl_contrast : dev->ctl_contrast);
1240 saa_writeb(SAA7134_DEC_CHROMA_SATURATION,
1241 dev->ctl_invert ? -dev->ctl_saturation : dev->ctl_saturation);
1243 case V4L2_CID_HFLIP:
1244 dev->ctl_mirror = c->value;
1245 restart_overlay = 1;
1247 case V4L2_CID_PRIVATE_Y_EVEN:
1248 dev->ctl_y_even = c->value;
1249 restart_overlay = 1;
1251 case V4L2_CID_PRIVATE_Y_ODD:
1252 dev->ctl_y_odd = c->value;
1253 restart_overlay = 1;
1255 case V4L2_CID_PRIVATE_AUTOMUTE:
1257 struct v4l2_priv_tun_config tda9887_cfg;
1259 tda9887_cfg.tuner = TUNER_TDA9887;
1260 tda9887_cfg.priv = &dev->tda9887_conf;
1262 dev->ctl_automute = c->value;
1263 if (dev->tda9887_conf) {
1264 if (dev->ctl_automute)
1265 dev->tda9887_conf |= TDA9887_AUTOMUTE;
1267 dev->tda9887_conf &= ~TDA9887_AUTOMUTE;
1269 saa7134_i2c_call_clients(dev, TUNER_SET_CONFIG,
1277 if (restart_overlay && fh && res_check(fh, RESOURCE_OVERLAY)) {
1278 spin_lock_irqsave(&dev->slock,flags);
1279 stop_preview(dev,fh);
1280 start_preview(dev,fh);
1281 spin_unlock_irqrestore(&dev->slock,flags);
1286 mutex_unlock(&dev->lock);
1289 EXPORT_SYMBOL_GPL(saa7134_s_ctrl_internal);
1291 static int saa7134_s_ctrl(struct file *file, void *f, struct v4l2_control *c)
1293 struct saa7134_fh *fh = f;
1295 return saa7134_s_ctrl_internal(fh->dev, fh, c);
1298 /* ------------------------------------------------------------------ */
1300 static struct videobuf_queue* saa7134_queue(struct saa7134_fh *fh)
1302 struct videobuf_queue* q = NULL;
1305 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1308 case V4L2_BUF_TYPE_VBI_CAPTURE:
1317 static int saa7134_resource(struct saa7134_fh *fh)
1319 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1320 return RESOURCE_VIDEO;
1322 if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)
1323 return RESOURCE_VBI;
1329 static int video_open(struct file *file)
1331 int minor = video_devdata(file)->minor;
1332 struct saa7134_dev *dev;
1333 struct saa7134_fh *fh;
1334 enum v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1338 list_for_each_entry(dev, &saa7134_devlist, devlist) {
1339 if (dev->video_dev && (dev->video_dev->minor == minor))
1341 if (dev->radio_dev && (dev->radio_dev->minor == minor)) {
1345 if (dev->vbi_dev && (dev->vbi_dev->minor == minor)) {
1346 type = V4L2_BUF_TYPE_VBI_CAPTURE;
1354 dprintk("open minor=%d radio=%d type=%s\n",minor,radio,
1355 v4l2_type_names[type]);
1357 /* allocate + initialize per filehandle data */
1358 fh = kzalloc(sizeof(*fh),GFP_KERNEL);
1363 file->private_data = fh;
1367 fh->fmt = format_by_fourcc(V4L2_PIX_FMT_BGR24);
1370 v4l2_prio_open(&dev->prio,&fh->prio);
1372 videobuf_queue_sg_init(&fh->cap, &video_qops,
1373 &dev->pci->dev, &dev->slock,
1374 V4L2_BUF_TYPE_VIDEO_CAPTURE,
1375 V4L2_FIELD_INTERLACED,
1376 sizeof(struct saa7134_buf),
1378 videobuf_queue_sg_init(&fh->vbi, &saa7134_vbi_qops,
1379 &dev->pci->dev, &dev->slock,
1380 V4L2_BUF_TYPE_VBI_CAPTURE,
1382 sizeof(struct saa7134_buf),
1384 saa7134_pgtable_alloc(dev->pci,&fh->pt_cap);
1385 saa7134_pgtable_alloc(dev->pci,&fh->pt_vbi);
1388 /* switch to radio mode */
1389 saa7134_tvaudio_setinput(dev,&card(dev).radio);
1390 saa7134_i2c_call_clients(dev,AUDC_SET_RADIO, NULL);
1392 /* switch to video/vbi mode */
1393 video_mux(dev,dev->ctl_input);
1400 video_read(struct file *file, char __user *data, size_t count, loff_t *ppos)
1402 struct saa7134_fh *fh = file->private_data;
1405 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1406 if (res_locked(fh->dev,RESOURCE_VIDEO))
1408 return videobuf_read_one(saa7134_queue(fh),
1410 file->f_flags & O_NONBLOCK);
1411 case V4L2_BUF_TYPE_VBI_CAPTURE:
1412 if (!res_get(fh->dev,fh,RESOURCE_VBI))
1414 return videobuf_read_stream(saa7134_queue(fh),
1415 data, count, ppos, 1,
1416 file->f_flags & O_NONBLOCK);
1425 video_poll(struct file *file, struct poll_table_struct *wait)
1427 struct saa7134_fh *fh = file->private_data;
1428 struct videobuf_buffer *buf = NULL;
1430 if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type)
1431 return videobuf_poll_stream(file, &fh->vbi, wait);
1433 if (res_check(fh,RESOURCE_VIDEO)) {
1434 if (!list_empty(&fh->cap.stream))
1435 buf = list_entry(fh->cap.stream.next, struct videobuf_buffer, stream);
1437 mutex_lock(&fh->cap.vb_lock);
1438 if (UNSET == fh->cap.read_off) {
1439 /* need to capture a new frame */
1440 if (res_locked(fh->dev,RESOURCE_VIDEO))
1442 if (0 != fh->cap.ops->buf_prepare(&fh->cap,fh->cap.read_buf,fh->cap.field))
1444 fh->cap.ops->buf_queue(&fh->cap,fh->cap.read_buf);
1445 fh->cap.read_off = 0;
1447 mutex_unlock(&fh->cap.vb_lock);
1448 buf = fh->cap.read_buf;
1454 poll_wait(file, &buf->done, wait);
1455 if (buf->state == VIDEOBUF_DONE ||
1456 buf->state == VIDEOBUF_ERROR)
1457 return POLLIN|POLLRDNORM;
1461 mutex_unlock(&fh->cap.vb_lock);
1465 static int video_release(struct file *file)
1467 struct saa7134_fh *fh = file->private_data;
1468 struct saa7134_dev *dev = fh->dev;
1469 unsigned long flags;
1471 /* turn off overlay */
1472 if (res_check(fh, RESOURCE_OVERLAY)) {
1473 spin_lock_irqsave(&dev->slock,flags);
1474 stop_preview(dev,fh);
1475 spin_unlock_irqrestore(&dev->slock,flags);
1476 res_free(dev,fh,RESOURCE_OVERLAY);
1479 /* stop video capture */
1480 if (res_check(fh, RESOURCE_VIDEO)) {
1481 videobuf_streamoff(&fh->cap);
1482 res_free(dev,fh,RESOURCE_VIDEO);
1484 if (fh->cap.read_buf) {
1485 buffer_release(&fh->cap,fh->cap.read_buf);
1486 kfree(fh->cap.read_buf);
1489 /* stop vbi capture */
1490 if (res_check(fh, RESOURCE_VBI)) {
1491 videobuf_stop(&fh->vbi);
1492 res_free(dev,fh,RESOURCE_VBI);
1495 /* ts-capture will not work in planar mode, so turn it off Hac: 04.05*/
1496 saa_andorb(SAA7134_OFMT_VIDEO_A, 0x1f, 0);
1497 saa_andorb(SAA7134_OFMT_VIDEO_B, 0x1f, 0);
1498 saa_andorb(SAA7134_OFMT_DATA_A, 0x1f, 0);
1499 saa_andorb(SAA7134_OFMT_DATA_B, 0x1f, 0);
1501 saa7134_i2c_call_clients(dev, TUNER_SET_STANDBY, NULL);
1504 videobuf_mmap_free(&fh->cap);
1505 videobuf_mmap_free(&fh->vbi);
1506 saa7134_pgtable_free(dev->pci,&fh->pt_cap);
1507 saa7134_pgtable_free(dev->pci,&fh->pt_vbi);
1509 v4l2_prio_close(&dev->prio,&fh->prio);
1510 file->private_data = NULL;
1515 static int video_mmap(struct file *file, struct vm_area_struct * vma)
1517 struct saa7134_fh *fh = file->private_data;
1519 return videobuf_mmap_mapper(saa7134_queue(fh), vma);
1522 /* ------------------------------------------------------------------ */
1524 static int saa7134_try_get_set_fmt_vbi_cap(struct file *file, void *priv,
1525 struct v4l2_format *f)
1527 struct saa7134_fh *fh = priv;
1528 struct saa7134_dev *dev = fh->dev;
1529 struct saa7134_tvnorm *norm = dev->tvnorm;
1531 f->fmt.vbi.sampling_rate = 6750000 * 4;
1532 f->fmt.vbi.samples_per_line = 2048 /* VBI_LINE_LENGTH */;
1533 f->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
1534 f->fmt.vbi.offset = 64 * 4;
1535 f->fmt.vbi.start[0] = norm->vbi_v_start_0;
1536 f->fmt.vbi.count[0] = norm->vbi_v_stop_0 - norm->vbi_v_start_0 +1;
1537 f->fmt.vbi.start[1] = norm->vbi_v_start_1;
1538 f->fmt.vbi.count[1] = f->fmt.vbi.count[0];
1539 f->fmt.vbi.flags = 0; /* VBI_UNSYNC VBI_INTERLACED */
1544 static int saa7134_g_fmt_vid_cap(struct file *file, void *priv,
1545 struct v4l2_format *f)
1547 struct saa7134_fh *fh = priv;
1549 f->fmt.pix.width = fh->width;
1550 f->fmt.pix.height = fh->height;
1551 f->fmt.pix.field = fh->cap.field;
1552 f->fmt.pix.pixelformat = fh->fmt->fourcc;
1553 f->fmt.pix.bytesperline =
1554 (f->fmt.pix.width * fh->fmt->depth) >> 3;
1555 f->fmt.pix.sizeimage =
1556 f->fmt.pix.height * f->fmt.pix.bytesperline;
1560 static int saa7134_g_fmt_vid_overlay(struct file *file, void *priv,
1561 struct v4l2_format *f)
1563 struct saa7134_fh *fh = priv;
1565 if (saa7134_no_overlay > 0) {
1566 printk(KERN_ERR "V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
1569 f->fmt.win = fh->win;
1574 static int saa7134_try_fmt_vid_cap(struct file *file, void *priv,
1575 struct v4l2_format *f)
1577 struct saa7134_fh *fh = priv;
1578 struct saa7134_dev *dev = fh->dev;
1579 struct saa7134_format *fmt;
1580 enum v4l2_field field;
1581 unsigned int maxw, maxh;
1583 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1587 field = f->fmt.pix.field;
1588 maxw = min(dev->crop_current.width*4, dev->crop_bounds.width);
1589 maxh = min(dev->crop_current.height*4, dev->crop_bounds.height);
1591 if (V4L2_FIELD_ANY == field) {
1592 field = (f->fmt.pix.height > maxh/2)
1593 ? V4L2_FIELD_INTERLACED
1594 : V4L2_FIELD_BOTTOM;
1597 case V4L2_FIELD_TOP:
1598 case V4L2_FIELD_BOTTOM:
1601 case V4L2_FIELD_INTERLACED:
1607 f->fmt.pix.field = field;
1608 if (f->fmt.pix.width < 48)
1609 f->fmt.pix.width = 48;
1610 if (f->fmt.pix.height < 32)
1611 f->fmt.pix.height = 32;
1612 if (f->fmt.pix.width > maxw)
1613 f->fmt.pix.width = maxw;
1614 if (f->fmt.pix.height > maxh)
1615 f->fmt.pix.height = maxh;
1616 f->fmt.pix.width &= ~0x03;
1617 f->fmt.pix.bytesperline =
1618 (f->fmt.pix.width * fmt->depth) >> 3;
1619 f->fmt.pix.sizeimage =
1620 f->fmt.pix.height * f->fmt.pix.bytesperline;
1625 static int saa7134_try_fmt_vid_overlay(struct file *file, void *priv,
1626 struct v4l2_format *f)
1628 struct saa7134_fh *fh = priv;
1629 struct saa7134_dev *dev = fh->dev;
1631 if (saa7134_no_overlay > 0) {
1632 printk(KERN_ERR "V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
1636 return verify_preview(dev, &f->fmt.win);
1639 static int saa7134_s_fmt_vid_cap(struct file *file, void *priv,
1640 struct v4l2_format *f)
1642 struct saa7134_fh *fh = priv;
1645 err = saa7134_try_fmt_vid_cap(file, priv, f);
1649 fh->fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1650 fh->width = f->fmt.pix.width;
1651 fh->height = f->fmt.pix.height;
1652 fh->cap.field = f->fmt.pix.field;
1656 static int saa7134_s_fmt_vid_overlay(struct file *file, void *priv,
1657 struct v4l2_format *f)
1659 struct saa7134_fh *fh = priv;
1660 struct saa7134_dev *dev = fh->dev;
1662 unsigned long flags;
1664 if (saa7134_no_overlay > 0) {
1665 printk(KERN_ERR "V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
1668 err = verify_preview(dev, &f->fmt.win);
1672 mutex_lock(&dev->lock);
1674 fh->win = f->fmt.win;
1675 fh->nclips = f->fmt.win.clipcount;
1680 if (copy_from_user(fh->clips, f->fmt.win.clips,
1681 sizeof(struct v4l2_clip)*fh->nclips)) {
1682 mutex_unlock(&dev->lock);
1686 if (res_check(fh, RESOURCE_OVERLAY)) {
1687 spin_lock_irqsave(&dev->slock, flags);
1688 stop_preview(dev, fh);
1689 start_preview(dev, fh);
1690 spin_unlock_irqrestore(&dev->slock, flags);
1693 mutex_unlock(&dev->lock);
1697 int saa7134_queryctrl(struct file *file, void *priv, struct v4l2_queryctrl *c)
1699 const struct v4l2_queryctrl *ctrl;
1701 if ((c->id < V4L2_CID_BASE ||
1702 c->id >= V4L2_CID_LASTP1) &&
1703 (c->id < V4L2_CID_PRIVATE_BASE ||
1704 c->id >= V4L2_CID_PRIVATE_LASTP1))
1706 ctrl = ctrl_by_id(c->id);
1707 *c = (NULL != ctrl) ? *ctrl : no_ctrl;
1710 EXPORT_SYMBOL_GPL(saa7134_queryctrl);
1712 static int saa7134_enum_input(struct file *file, void *priv,
1713 struct v4l2_input *i)
1715 struct saa7134_fh *fh = priv;
1716 struct saa7134_dev *dev = fh->dev;
1720 if (n >= SAA7134_INPUT_MAX)
1722 if (NULL == card_in(dev, i->index).name)
1724 memset(i, 0, sizeof(*i));
1726 i->type = V4L2_INPUT_TYPE_CAMERA;
1727 strcpy(i->name, card_in(dev, n).name);
1728 if (card_in(dev, n).tv)
1729 i->type = V4L2_INPUT_TYPE_TUNER;
1731 if (n == dev->ctl_input) {
1732 int v1 = saa_readb(SAA7134_STATUS_VIDEO1);
1733 int v2 = saa_readb(SAA7134_STATUS_VIDEO2);
1735 if (0 != (v1 & 0x40))
1736 i->status |= V4L2_IN_ST_NO_H_LOCK;
1737 if (0 != (v2 & 0x40))
1738 i->status |= V4L2_IN_ST_NO_SYNC;
1739 if (0 != (v2 & 0x0e))
1740 i->status |= V4L2_IN_ST_MACROVISION;
1742 i->std = SAA7134_NORMS;
1746 static int saa7134_g_input(struct file *file, void *priv, unsigned int *i)
1748 struct saa7134_fh *fh = priv;
1749 struct saa7134_dev *dev = fh->dev;
1751 *i = dev->ctl_input;
1755 static int saa7134_s_input(struct file *file, void *priv, unsigned int i)
1757 struct saa7134_fh *fh = priv;
1758 struct saa7134_dev *dev = fh->dev;
1761 err = v4l2_prio_check(&dev->prio, &fh->prio);
1765 if (i < 0 || i >= SAA7134_INPUT_MAX)
1767 if (NULL == card_in(dev, i).name)
1769 mutex_lock(&dev->lock);
1771 mutex_unlock(&dev->lock);
1775 static int saa7134_querycap(struct file *file, void *priv,
1776 struct v4l2_capability *cap)
1778 struct saa7134_fh *fh = priv;
1779 struct saa7134_dev *dev = fh->dev;
1781 unsigned int tuner_type = dev->tuner_type;
1783 strcpy(cap->driver, "saa7134");
1784 strlcpy(cap->card, saa7134_boards[dev->board].name,
1786 sprintf(cap->bus_info, "PCI:%s", pci_name(dev->pci));
1787 cap->version = SAA7134_VERSION_CODE;
1789 V4L2_CAP_VIDEO_CAPTURE |
1790 V4L2_CAP_VBI_CAPTURE |
1791 V4L2_CAP_READWRITE |
1792 V4L2_CAP_STREAMING |
1794 if (saa7134_no_overlay <= 0)
1795 cap->capabilities |= V4L2_CAP_VIDEO_OVERLAY;
1797 if ((tuner_type == TUNER_ABSENT) || (tuner_type == UNSET))
1798 cap->capabilities &= ~V4L2_CAP_TUNER;
1802 int saa7134_s_std_internal(struct saa7134_dev *dev, struct saa7134_fh *fh, v4l2_std_id *id)
1804 unsigned long flags;
1809 /* When called from the empress code fh == NULL.
1810 That needs to be fixed somehow, but for now this is
1813 err = v4l2_prio_check(&dev->prio, &fh->prio);
1816 } else if (res_locked(dev, RESOURCE_OVERLAY)) {
1817 /* Don't change the std from the mpeg device
1818 if overlay is active. */
1822 for (i = 0; i < TVNORMS; i++)
1823 if (*id == tvnorms[i].id)
1827 for (i = 0; i < TVNORMS; i++)
1828 if (*id & tvnorms[i].id)
1833 if ((*id & V4L2_STD_SECAM) && (secam[0] != '-')) {
1834 if (secam[0] == 'L' || secam[0] == 'l') {
1835 if (secam[1] == 'C' || secam[1] == 'c')
1836 fixup = V4L2_STD_SECAM_LC;
1838 fixup = V4L2_STD_SECAM_L;
1840 if (secam[0] == 'D' || secam[0] == 'd')
1841 fixup = V4L2_STD_SECAM_DK;
1843 fixup = V4L2_STD_SECAM;
1845 for (i = 0; i < TVNORMS; i++)
1846 if (fixup == tvnorms[i].id)
1850 *id = tvnorms[i].id;
1852 mutex_lock(&dev->lock);
1853 if (fh && res_check(fh, RESOURCE_OVERLAY)) {
1854 spin_lock_irqsave(&dev->slock, flags);
1855 stop_preview(dev, fh);
1856 spin_unlock_irqrestore(&dev->slock, flags);
1858 set_tvnorm(dev, &tvnorms[i]);
1860 spin_lock_irqsave(&dev->slock, flags);
1861 start_preview(dev, fh);
1862 spin_unlock_irqrestore(&dev->slock, flags);
1864 set_tvnorm(dev, &tvnorms[i]);
1866 saa7134_tvaudio_do_scan(dev);
1867 mutex_unlock(&dev->lock);
1870 EXPORT_SYMBOL_GPL(saa7134_s_std_internal);
1872 static int saa7134_s_std(struct file *file, void *priv, v4l2_std_id *id)
1874 struct saa7134_fh *fh = priv;
1876 return saa7134_s_std_internal(fh->dev, fh, id);
1879 static int saa7134_g_std(struct file *file, void *priv, v4l2_std_id *id)
1881 struct saa7134_fh *fh = priv;
1882 struct saa7134_dev *dev = fh->dev;
1884 *id = dev->tvnorm->id;
1888 static int saa7134_cropcap(struct file *file, void *priv,
1889 struct v4l2_cropcap *cap)
1891 struct saa7134_fh *fh = priv;
1892 struct saa7134_dev *dev = fh->dev;
1894 if (cap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1895 cap->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
1897 cap->bounds = dev->crop_bounds;
1898 cap->defrect = dev->crop_defrect;
1899 cap->pixelaspect.numerator = 1;
1900 cap->pixelaspect.denominator = 1;
1901 if (dev->tvnorm->id & V4L2_STD_525_60) {
1902 cap->pixelaspect.numerator = 11;
1903 cap->pixelaspect.denominator = 10;
1905 if (dev->tvnorm->id & V4L2_STD_625_50) {
1906 cap->pixelaspect.numerator = 54;
1907 cap->pixelaspect.denominator = 59;
1912 static int saa7134_g_crop(struct file *file, void *f, struct v4l2_crop *crop)
1914 struct saa7134_fh *fh = f;
1915 struct saa7134_dev *dev = fh->dev;
1917 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1918 crop->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
1920 crop->c = dev->crop_current;
1924 static int saa7134_s_crop(struct file *file, void *f, struct v4l2_crop *crop)
1926 struct saa7134_fh *fh = f;
1927 struct saa7134_dev *dev = fh->dev;
1928 struct v4l2_rect *b = &dev->crop_bounds;
1930 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1931 crop->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
1933 if (crop->c.height < 0)
1935 if (crop->c.width < 0)
1938 if (res_locked(fh->dev, RESOURCE_OVERLAY))
1940 if (res_locked(fh->dev, RESOURCE_VIDEO))
1943 if (crop->c.top < b->top)
1944 crop->c.top = b->top;
1945 if (crop->c.top > b->top + b->height)
1946 crop->c.top = b->top + b->height;
1947 if (crop->c.height > b->top - crop->c.top + b->height)
1948 crop->c.height = b->top - crop->c.top + b->height;
1950 if (crop->c.left < b->left)
1951 crop->c.left = b->left;
1952 if (crop->c.left > b->left + b->width)
1953 crop->c.left = b->left + b->width;
1954 if (crop->c.width > b->left - crop->c.left + b->width)
1955 crop->c.width = b->left - crop->c.left + b->width;
1957 dev->crop_current = crop->c;
1961 static int saa7134_g_tuner(struct file *file, void *priv,
1962 struct v4l2_tuner *t)
1964 struct saa7134_fh *fh = priv;
1965 struct saa7134_dev *dev = fh->dev;
1970 memset(t, 0, sizeof(*t));
1971 for (n = 0; n < SAA7134_INPUT_MAX; n++)
1972 if (card_in(dev, n).tv)
1974 if (NULL != card_in(dev, n).name) {
1975 strcpy(t->name, "Television");
1976 t->type = V4L2_TUNER_ANALOG_TV;
1977 t->capability = V4L2_TUNER_CAP_NORM |
1978 V4L2_TUNER_CAP_STEREO |
1979 V4L2_TUNER_CAP_LANG1 |
1980 V4L2_TUNER_CAP_LANG2;
1981 t->rangehigh = 0xffffffffUL;
1982 t->rxsubchans = saa7134_tvaudio_getstereo(dev);
1983 t->audmode = saa7134_tvaudio_rx2mode(t->rxsubchans);
1985 if (0 != (saa_readb(SAA7134_STATUS_VIDEO1) & 0x03))
1990 static int saa7134_s_tuner(struct file *file, void *priv,
1991 struct v4l2_tuner *t)
1993 struct saa7134_fh *fh = priv;
1994 struct saa7134_dev *dev = fh->dev;
1997 err = v4l2_prio_check(&dev->prio, &fh->prio);
2001 mode = dev->thread.mode;
2002 if (UNSET == mode) {
2003 rx = saa7134_tvaudio_getstereo(dev);
2004 mode = saa7134_tvaudio_rx2mode(t->rxsubchans);
2006 if (mode != t->audmode)
2007 dev->thread.mode = t->audmode;
2012 static int saa7134_g_frequency(struct file *file, void *priv,
2013 struct v4l2_frequency *f)
2015 struct saa7134_fh *fh = priv;
2016 struct saa7134_dev *dev = fh->dev;
2018 f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
2019 f->frequency = dev->ctl_freq;
2024 static int saa7134_s_frequency(struct file *file, void *priv,
2025 struct v4l2_frequency *f)
2027 struct saa7134_fh *fh = priv;
2028 struct saa7134_dev *dev = fh->dev;
2031 err = v4l2_prio_check(&dev->prio, &fh->prio);
2037 if (0 == fh->radio && V4L2_TUNER_ANALOG_TV != f->type)
2039 if (1 == fh->radio && V4L2_TUNER_RADIO != f->type)
2041 mutex_lock(&dev->lock);
2042 dev->ctl_freq = f->frequency;
2044 saa7134_i2c_call_clients(dev, VIDIOC_S_FREQUENCY, f);
2046 saa7134_tvaudio_do_scan(dev);
2047 mutex_unlock(&dev->lock);
2051 static int saa7134_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
2053 strcpy(a->name, "audio");
2057 static int saa7134_s_audio(struct file *file, void *priv, struct v4l2_audio *a)
2062 static int saa7134_g_priority(struct file *file, void *f, enum v4l2_priority *p)
2064 struct saa7134_fh *fh = f;
2065 struct saa7134_dev *dev = fh->dev;
2067 *p = v4l2_prio_max(&dev->prio);
2071 static int saa7134_s_priority(struct file *file, void *f,
2072 enum v4l2_priority prio)
2074 struct saa7134_fh *fh = f;
2075 struct saa7134_dev *dev = fh->dev;
2077 return v4l2_prio_change(&dev->prio, &fh->prio, prio);
2080 static int saa7134_enum_fmt_vid_cap(struct file *file, void *priv,
2081 struct v4l2_fmtdesc *f)
2083 if (f->index >= FORMATS)
2086 strlcpy(f->description, formats[f->index].name,
2087 sizeof(f->description));
2089 f->pixelformat = formats[f->index].fourcc;
2094 static int saa7134_enum_fmt_vid_overlay(struct file *file, void *priv,
2095 struct v4l2_fmtdesc *f)
2097 if (saa7134_no_overlay > 0) {
2098 printk(KERN_ERR "V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
2102 if ((f->index >= FORMATS) || formats[f->index].planar)
2105 strlcpy(f->description, formats[f->index].name,
2106 sizeof(f->description));
2108 f->pixelformat = formats[f->index].fourcc;
2113 static int saa7134_g_fbuf(struct file *file, void *f,
2114 struct v4l2_framebuffer *fb)
2116 struct saa7134_fh *fh = f;
2117 struct saa7134_dev *dev = fh->dev;
2120 fb->capability = V4L2_FBUF_CAP_LIST_CLIPPING;
2125 static int saa7134_s_fbuf(struct file *file, void *f,
2126 struct v4l2_framebuffer *fb)
2128 struct saa7134_fh *fh = f;
2129 struct saa7134_dev *dev = fh->dev;
2130 struct saa7134_format *fmt;
2132 if (!capable(CAP_SYS_ADMIN) &&
2133 !capable(CAP_SYS_RAWIO))
2137 fmt = format_by_fourcc(fb->fmt.pixelformat);
2144 if (0 == dev->ovbuf.fmt.bytesperline)
2145 dev->ovbuf.fmt.bytesperline =
2146 dev->ovbuf.fmt.width*fmt->depth/8;
2150 static int saa7134_overlay(struct file *file, void *f, unsigned int on)
2152 struct saa7134_fh *fh = f;
2153 struct saa7134_dev *dev = fh->dev;
2154 unsigned long flags;
2157 if (saa7134_no_overlay > 0) {
2158 dprintk("no_overlay\n");
2162 if (!res_get(dev, fh, RESOURCE_OVERLAY))
2164 spin_lock_irqsave(&dev->slock, flags);
2165 start_preview(dev, fh);
2166 spin_unlock_irqrestore(&dev->slock, flags);
2169 if (!res_check(fh, RESOURCE_OVERLAY))
2171 spin_lock_irqsave(&dev->slock, flags);
2172 stop_preview(dev, fh);
2173 spin_unlock_irqrestore(&dev->slock, flags);
2174 res_free(dev, fh, RESOURCE_OVERLAY);
2179 #ifdef CONFIG_VIDEO_V4L1_COMPAT
2180 static int vidiocgmbuf(struct file *file, void *priv, struct video_mbuf *mbuf)
2182 struct saa7134_fh *fh = file->private_data;
2183 return videobuf_cgmbuf(saa7134_queue(fh), mbuf, 8);
2187 static int saa7134_reqbufs(struct file *file, void *priv,
2188 struct v4l2_requestbuffers *p)
2190 struct saa7134_fh *fh = priv;
2191 return videobuf_reqbufs(saa7134_queue(fh), p);
2194 static int saa7134_querybuf(struct file *file, void *priv,
2195 struct v4l2_buffer *b)
2197 struct saa7134_fh *fh = priv;
2198 return videobuf_querybuf(saa7134_queue(fh), b);
2201 static int saa7134_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
2203 struct saa7134_fh *fh = priv;
2204 return videobuf_qbuf(saa7134_queue(fh), b);
2207 static int saa7134_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
2209 struct saa7134_fh *fh = priv;
2210 return videobuf_dqbuf(saa7134_queue(fh), b,
2211 file->f_flags & O_NONBLOCK);
2214 static int saa7134_streamon(struct file *file, void *priv,
2215 enum v4l2_buf_type type)
2217 struct saa7134_fh *fh = priv;
2218 struct saa7134_dev *dev = fh->dev;
2219 int res = saa7134_resource(fh);
2221 if (!res_get(dev, fh, res))
2224 return videobuf_streamon(saa7134_queue(fh));
2227 static int saa7134_streamoff(struct file *file, void *priv,
2228 enum v4l2_buf_type type)
2231 struct saa7134_fh *fh = priv;
2232 struct saa7134_dev *dev = fh->dev;
2233 int res = saa7134_resource(fh);
2235 err = videobuf_streamoff(saa7134_queue(fh));
2238 res_free(dev, fh, res);
2242 static int saa7134_g_parm(struct file *file, void *fh,
2243 struct v4l2_streamparm *parm)
2248 #ifdef CONFIG_VIDEO_ADV_DEBUG
2249 static int vidioc_g_register (struct file *file, void *priv,
2250 struct v4l2_dbg_register *reg)
2252 struct saa7134_fh *fh = priv;
2253 struct saa7134_dev *dev = fh->dev;
2255 if (!v4l2_chip_match_host(®->match))
2257 reg->val = saa_readb(reg->reg);
2262 static int vidioc_s_register (struct file *file, void *priv,
2263 struct v4l2_dbg_register *reg)
2265 struct saa7134_fh *fh = priv;
2266 struct saa7134_dev *dev = fh->dev;
2268 if (!v4l2_chip_match_host(®->match))
2270 saa_writeb(reg->reg&0xffffff, reg->val);
2275 static int radio_querycap(struct file *file, void *priv,
2276 struct v4l2_capability *cap)
2278 struct saa7134_fh *fh = file->private_data;
2279 struct saa7134_dev *dev = fh->dev;
2281 strcpy(cap->driver, "saa7134");
2282 strlcpy(cap->card, saa7134_boards[dev->board].name, sizeof(cap->card));
2283 sprintf(cap->bus_info, "PCI:%s", pci_name(dev->pci));
2284 cap->version = SAA7134_VERSION_CODE;
2285 cap->capabilities = V4L2_CAP_TUNER;
2289 static int radio_g_tuner(struct file *file, void *priv,
2290 struct v4l2_tuner *t)
2292 struct saa7134_fh *fh = file->private_data;
2293 struct saa7134_dev *dev = fh->dev;
2298 memset(t, 0, sizeof(*t));
2299 strcpy(t->name, "Radio");
2300 t->type = V4L2_TUNER_RADIO;
2302 saa7134_i2c_call_clients(dev, VIDIOC_G_TUNER, t);
2303 if (dev->input->amux == TV) {
2304 t->signal = 0xf800 - ((saa_readb(0x581) & 0x1f) << 11);
2305 t->rxsubchans = (saa_readb(0x529) & 0x08) ?
2306 V4L2_TUNER_SUB_STEREO : V4L2_TUNER_SUB_MONO;
2310 static int radio_s_tuner(struct file *file, void *priv,
2311 struct v4l2_tuner *t)
2313 struct saa7134_fh *fh = file->private_data;
2314 struct saa7134_dev *dev = fh->dev;
2319 saa7134_i2c_call_clients(dev, VIDIOC_S_TUNER, t);
2323 static int radio_enum_input(struct file *file, void *priv,
2324 struct v4l2_input *i)
2329 strcpy(i->name, "Radio");
2330 i->type = V4L2_INPUT_TYPE_TUNER;
2335 static int radio_g_input(struct file *filp, void *priv, unsigned int *i)
2341 static int radio_g_audio(struct file *file, void *priv,
2342 struct v4l2_audio *a)
2344 memset(a, 0, sizeof(*a));
2345 strcpy(a->name, "Radio");
2349 static int radio_s_audio(struct file *file, void *priv,
2350 struct v4l2_audio *a)
2355 static int radio_s_input(struct file *filp, void *priv, unsigned int i)
2360 static int radio_s_std(struct file *file, void *fh, v4l2_std_id *norm)
2365 static int radio_queryctrl(struct file *file, void *priv,
2366 struct v4l2_queryctrl *c)
2368 const struct v4l2_queryctrl *ctrl;
2370 if (c->id < V4L2_CID_BASE ||
2371 c->id >= V4L2_CID_LASTP1)
2373 if (c->id == V4L2_CID_AUDIO_MUTE) {
2374 ctrl = ctrl_by_id(c->id);
2381 static const struct v4l2_file_operations video_fops =
2383 .owner = THIS_MODULE,
2385 .release = video_release,
2389 .ioctl = video_ioctl2,
2392 static const struct v4l2_ioctl_ops video_ioctl_ops = {
2393 .vidioc_querycap = saa7134_querycap,
2394 .vidioc_enum_fmt_vid_cap = saa7134_enum_fmt_vid_cap,
2395 .vidioc_g_fmt_vid_cap = saa7134_g_fmt_vid_cap,
2396 .vidioc_try_fmt_vid_cap = saa7134_try_fmt_vid_cap,
2397 .vidioc_s_fmt_vid_cap = saa7134_s_fmt_vid_cap,
2398 .vidioc_enum_fmt_vid_overlay = saa7134_enum_fmt_vid_overlay,
2399 .vidioc_g_fmt_vid_overlay = saa7134_g_fmt_vid_overlay,
2400 .vidioc_try_fmt_vid_overlay = saa7134_try_fmt_vid_overlay,
2401 .vidioc_s_fmt_vid_overlay = saa7134_s_fmt_vid_overlay,
2402 .vidioc_g_fmt_vbi_cap = saa7134_try_get_set_fmt_vbi_cap,
2403 .vidioc_try_fmt_vbi_cap = saa7134_try_get_set_fmt_vbi_cap,
2404 .vidioc_s_fmt_vbi_cap = saa7134_try_get_set_fmt_vbi_cap,
2405 .vidioc_g_audio = saa7134_g_audio,
2406 .vidioc_s_audio = saa7134_s_audio,
2407 .vidioc_cropcap = saa7134_cropcap,
2408 .vidioc_reqbufs = saa7134_reqbufs,
2409 .vidioc_querybuf = saa7134_querybuf,
2410 .vidioc_qbuf = saa7134_qbuf,
2411 .vidioc_dqbuf = saa7134_dqbuf,
2412 .vidioc_s_std = saa7134_s_std,
2413 .vidioc_g_std = saa7134_g_std,
2414 .vidioc_enum_input = saa7134_enum_input,
2415 .vidioc_g_input = saa7134_g_input,
2416 .vidioc_s_input = saa7134_s_input,
2417 .vidioc_queryctrl = saa7134_queryctrl,
2418 .vidioc_g_ctrl = saa7134_g_ctrl,
2419 .vidioc_s_ctrl = saa7134_s_ctrl,
2420 .vidioc_streamon = saa7134_streamon,
2421 .vidioc_streamoff = saa7134_streamoff,
2422 .vidioc_g_tuner = saa7134_g_tuner,
2423 .vidioc_s_tuner = saa7134_s_tuner,
2424 #ifdef CONFIG_VIDEO_V4L1_COMPAT
2425 .vidiocgmbuf = vidiocgmbuf,
2427 .vidioc_g_crop = saa7134_g_crop,
2428 .vidioc_s_crop = saa7134_s_crop,
2429 .vidioc_g_fbuf = saa7134_g_fbuf,
2430 .vidioc_s_fbuf = saa7134_s_fbuf,
2431 .vidioc_overlay = saa7134_overlay,
2432 .vidioc_g_priority = saa7134_g_priority,
2433 .vidioc_s_priority = saa7134_s_priority,
2434 .vidioc_g_parm = saa7134_g_parm,
2435 .vidioc_g_frequency = saa7134_g_frequency,
2436 .vidioc_s_frequency = saa7134_s_frequency,
2437 #ifdef CONFIG_VIDEO_ADV_DEBUG
2438 .vidioc_g_register = vidioc_g_register,
2439 .vidioc_s_register = vidioc_s_register,
2443 static const struct v4l2_file_operations radio_fops = {
2444 .owner = THIS_MODULE,
2446 .release = video_release,
2447 .ioctl = video_ioctl2,
2450 static const struct v4l2_ioctl_ops radio_ioctl_ops = {
2451 .vidioc_querycap = radio_querycap,
2452 .vidioc_g_tuner = radio_g_tuner,
2453 .vidioc_enum_input = radio_enum_input,
2454 .vidioc_g_audio = radio_g_audio,
2455 .vidioc_s_tuner = radio_s_tuner,
2456 .vidioc_s_audio = radio_s_audio,
2457 .vidioc_s_input = radio_s_input,
2458 .vidioc_s_std = radio_s_std,
2459 .vidioc_queryctrl = radio_queryctrl,
2460 .vidioc_g_input = radio_g_input,
2461 .vidioc_g_ctrl = saa7134_g_ctrl,
2462 .vidioc_s_ctrl = saa7134_s_ctrl,
2463 .vidioc_g_frequency = saa7134_g_frequency,
2464 .vidioc_s_frequency = saa7134_s_frequency,
2467 /* ----------------------------------------------------------- */
2468 /* exported stuff */
2470 struct video_device saa7134_video_template = {
2471 .name = "saa7134-video",
2472 .fops = &video_fops,
2473 .ioctl_ops = &video_ioctl_ops,
2475 .tvnorms = SAA7134_NORMS,
2476 .current_norm = V4L2_STD_PAL,
2479 struct video_device saa7134_radio_template = {
2480 .name = "saa7134-radio",
2481 .fops = &radio_fops,
2482 .ioctl_ops = &radio_ioctl_ops,
2486 int saa7134_video_init1(struct saa7134_dev *dev)
2488 /* sanitycheck insmod options */
2489 if (gbuffers < 2 || gbuffers > VIDEO_MAX_FRAME)
2491 if (gbufsize < 0 || gbufsize > gbufsize_max)
2492 gbufsize = gbufsize_max;
2493 gbufsize = (gbufsize + PAGE_SIZE - 1) & PAGE_MASK;
2495 /* put some sensible defaults into the data structures ... */
2496 dev->ctl_bright = ctrl_by_id(V4L2_CID_BRIGHTNESS)->default_value;
2497 dev->ctl_contrast = ctrl_by_id(V4L2_CID_CONTRAST)->default_value;
2498 dev->ctl_hue = ctrl_by_id(V4L2_CID_HUE)->default_value;
2499 dev->ctl_saturation = ctrl_by_id(V4L2_CID_SATURATION)->default_value;
2500 dev->ctl_volume = ctrl_by_id(V4L2_CID_AUDIO_VOLUME)->default_value;
2501 dev->ctl_mute = 1; // ctrl_by_id(V4L2_CID_AUDIO_MUTE)->default_value;
2502 dev->ctl_invert = ctrl_by_id(V4L2_CID_PRIVATE_INVERT)->default_value;
2503 dev->ctl_automute = ctrl_by_id(V4L2_CID_PRIVATE_AUTOMUTE)->default_value;
2505 if (dev->tda9887_conf && dev->ctl_automute)
2506 dev->tda9887_conf |= TDA9887_AUTOMUTE;
2509 INIT_LIST_HEAD(&dev->video_q.queue);
2510 init_timer(&dev->video_q.timeout);
2511 dev->video_q.timeout.function = saa7134_buffer_timeout;
2512 dev->video_q.timeout.data = (unsigned long)(&dev->video_q);
2513 dev->video_q.dev = dev;
2515 if (saa7134_boards[dev->board].video_out)
2516 saa7134_videoport_init(dev);
2521 int saa7134_videoport_init(struct saa7134_dev *dev)
2523 /* enable video output */
2524 int vo = saa7134_boards[dev->board].video_out;
2526 unsigned int vid_port_opts = saa7134_boards[dev->board].vid_port_opts;
2528 /* Configure videoport */
2529 saa_writeb(SAA7134_VIDEO_PORT_CTRL0, video_out[vo][0]);
2530 video_reg = video_out[vo][1];
2531 if (vid_port_opts & SET_T_CODE_POLARITY_NON_INVERTED)
2532 video_reg &= ~VP_T_CODE_P_INVERTED;
2533 saa_writeb(SAA7134_VIDEO_PORT_CTRL1, video_reg);
2534 saa_writeb(SAA7134_VIDEO_PORT_CTRL2, video_out[vo][2]);
2535 saa_writeb(SAA7134_VIDEO_PORT_CTRL4, video_out[vo][4]);
2536 video_reg = video_out[vo][5];
2537 if (vid_port_opts & SET_CLOCK_NOT_DELAYED)
2538 video_reg &= ~VP_CLK_CTRL2_DELAYED;
2539 if (vid_port_opts & SET_CLOCK_INVERTED)
2540 video_reg |= VP_CLK_CTRL1_INVERTED;
2541 saa_writeb(SAA7134_VIDEO_PORT_CTRL5, video_reg);
2542 video_reg = video_out[vo][6];
2543 if (vid_port_opts & SET_VSYNC_OFF) {
2544 video_reg &= ~VP_VS_TYPE_MASK;
2545 video_reg |= VP_VS_TYPE_OFF;
2547 saa_writeb(SAA7134_VIDEO_PORT_CTRL6, video_reg);
2548 saa_writeb(SAA7134_VIDEO_PORT_CTRL7, video_out[vo][7]);
2549 saa_writeb(SAA7134_VIDEO_PORT_CTRL8, video_out[vo][8]);
2551 /* Start videoport */
2552 saa_writeb(SAA7134_VIDEO_PORT_CTRL3, video_out[vo][3]);
2557 int saa7134_video_init2(struct saa7134_dev *dev)
2560 set_tvnorm(dev,&tvnorms[0]);
2562 saa7134_tvaudio_setmute(dev);
2563 saa7134_tvaudio_setvolume(dev,dev->ctl_volume);
2567 void saa7134_irq_video_signalchange(struct saa7134_dev *dev)
2569 static const char *st[] = {
2570 "(no signal)", "NTSC", "PAL", "SECAM" };
2573 st1 = saa_readb(SAA7134_STATUS_VIDEO1);
2574 st2 = saa_readb(SAA7134_STATUS_VIDEO2);
2575 dprintk("DCSDT: pll: %s, sync: %s, norm: %s\n",
2576 (st1 & 0x40) ? "not locked" : "locked",
2577 (st2 & 0x40) ? "no" : "yes",
2579 dev->nosignal = (st1 & 0x40) || (st2 & 0x40) || !(st2 & 0x1);
2581 if (dev->nosignal) {
2582 /* no video signal -> mute audio */
2583 if (dev->ctl_automute)
2585 saa7134_tvaudio_setmute(dev);
2587 /* wake up tvaudio audio carrier scan thread */
2588 saa7134_tvaudio_do_scan(dev);
2591 if ((st2 & 0x80) && !noninterlaced && !dev->nosignal)
2592 saa_clearb(SAA7134_SYNC_CTRL, 0x20);
2594 saa_setb(SAA7134_SYNC_CTRL, 0x20);
2596 if (dev->mops && dev->mops->signal_change)
2597 dev->mops->signal_change(dev);
2601 void saa7134_irq_video_done(struct saa7134_dev *dev, unsigned long status)
2603 enum v4l2_field field;
2605 spin_lock(&dev->slock);
2606 if (dev->video_q.curr) {
2607 dev->video_fieldcount++;
2608 field = dev->video_q.curr->vb.field;
2609 if (V4L2_FIELD_HAS_BOTH(field)) {
2610 /* make sure we have seen both fields */
2611 if ((status & 0x10) == 0x00) {
2612 dev->video_q.curr->top_seen = 1;
2615 if (!dev->video_q.curr->top_seen)
2617 } else if (field == V4L2_FIELD_TOP) {
2618 if ((status & 0x10) != 0x10)
2620 } else if (field == V4L2_FIELD_BOTTOM) {
2621 if ((status & 0x10) != 0x00)
2624 dev->video_q.curr->vb.field_count = dev->video_fieldcount;
2625 saa7134_buffer_finish(dev,&dev->video_q,VIDEOBUF_DONE);
2627 saa7134_buffer_next(dev,&dev->video_q);
2630 spin_unlock(&dev->slock);
2633 /* ----------------------------------------------------------- */