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);
633 static void set_h_prescale(struct saa7134_dev *dev, int task, int prescale)
635 static const struct {
642 /* XPSC XACL XC2_1 XDCG VPFY */
654 static const int count = ARRAY_SIZE(vals);
657 for (i = 0; i < count; i++)
658 if (vals[i].xpsc == prescale)
663 saa_writeb(SAA7134_H_PRESCALE(task), vals[i].xpsc);
664 saa_writeb(SAA7134_ACC_LENGTH(task), vals[i].xacl);
665 saa_writeb(SAA7134_LEVEL_CTRL(task),
666 (vals[i].xc2_1 << 3) | (vals[i].xdcg));
667 saa_andorb(SAA7134_FIR_PREFILTER_CTRL(task), 0x0f,
668 (vals[i].vpfy << 2) | vals[i].vpfy);
671 static void set_v_scale(struct saa7134_dev *dev, int task, int yscale)
675 saa_writeb(SAA7134_V_SCALE_RATIO1(task), yscale & 0xff);
676 saa_writeb(SAA7134_V_SCALE_RATIO2(task), yscale >> 8);
678 mirror = (dev->ctl_mirror) ? 0x02 : 0x00;
681 dprintk("yscale LPI yscale=%d\n",yscale);
682 saa_writeb(SAA7134_V_FILTER(task), 0x00 | mirror);
683 saa_writeb(SAA7134_LUMA_CONTRAST(task), 0x40);
684 saa_writeb(SAA7134_CHROMA_SATURATION(task), 0x40);
687 val = 0x40 * 1024 / yscale;
688 dprintk("yscale ACM yscale=%d val=0x%x\n",yscale,val);
689 saa_writeb(SAA7134_V_FILTER(task), 0x01 | mirror);
690 saa_writeb(SAA7134_LUMA_CONTRAST(task), val);
691 saa_writeb(SAA7134_CHROMA_SATURATION(task), val);
693 saa_writeb(SAA7134_LUMA_BRIGHT(task), 0x80);
696 static void set_size(struct saa7134_dev *dev, int task,
697 int width, int height, int interlace)
699 int prescale,xscale,yscale,y_even,y_odd;
700 int h_start, h_stop, v_start, v_stop;
701 int div = interlace ? 2 : 1;
703 /* setup video scaler */
704 h_start = dev->crop_current.left;
705 v_start = dev->crop_current.top/2;
706 h_stop = (dev->crop_current.left + dev->crop_current.width -1);
707 v_stop = (dev->crop_current.top + dev->crop_current.height -1)/2;
709 saa_writeb(SAA7134_VIDEO_H_START1(task), h_start & 0xff);
710 saa_writeb(SAA7134_VIDEO_H_START2(task), h_start >> 8);
711 saa_writeb(SAA7134_VIDEO_H_STOP1(task), h_stop & 0xff);
712 saa_writeb(SAA7134_VIDEO_H_STOP2(task), h_stop >> 8);
713 saa_writeb(SAA7134_VIDEO_V_START1(task), v_start & 0xff);
714 saa_writeb(SAA7134_VIDEO_V_START2(task), v_start >> 8);
715 saa_writeb(SAA7134_VIDEO_V_STOP1(task), v_stop & 0xff);
716 saa_writeb(SAA7134_VIDEO_V_STOP2(task), v_stop >> 8);
718 prescale = dev->crop_current.width / width;
721 xscale = 1024 * dev->crop_current.width / prescale / width;
722 yscale = 512 * div * dev->crop_current.height / height;
723 dprintk("prescale=%d xscale=%d yscale=%d\n",prescale,xscale,yscale);
724 set_h_prescale(dev,task,prescale);
725 saa_writeb(SAA7134_H_SCALE_INC1(task), xscale & 0xff);
726 saa_writeb(SAA7134_H_SCALE_INC2(task), xscale >> 8);
727 set_v_scale(dev,task,yscale);
729 saa_writeb(SAA7134_VIDEO_PIXELS1(task), width & 0xff);
730 saa_writeb(SAA7134_VIDEO_PIXELS2(task), width >> 8);
731 saa_writeb(SAA7134_VIDEO_LINES1(task), height/div & 0xff);
732 saa_writeb(SAA7134_VIDEO_LINES2(task), height/div >> 8);
734 /* deinterlace y offsets */
735 y_odd = dev->ctl_y_odd;
736 y_even = dev->ctl_y_even;
737 saa_writeb(SAA7134_V_PHASE_OFFSET0(task), y_odd);
738 saa_writeb(SAA7134_V_PHASE_OFFSET1(task), y_even);
739 saa_writeb(SAA7134_V_PHASE_OFFSET2(task), y_odd);
740 saa_writeb(SAA7134_V_PHASE_OFFSET3(task), y_even);
743 /* ------------------------------------------------------------------ */
751 static void set_cliplist(struct saa7134_dev *dev, int reg,
752 struct cliplist *cl, int entries, char *name)
757 for (i = 0; i < entries; i++) {
758 winbits |= cl[i].enable;
759 winbits &= ~cl[i].disable;
760 if (i < 15 && cl[i].position == cl[i+1].position)
762 saa_writeb(reg + 0, winbits);
763 saa_writeb(reg + 2, cl[i].position & 0xff);
764 saa_writeb(reg + 3, cl[i].position >> 8);
765 dprintk("clip: %s winbits=%02x pos=%d\n",
766 name,winbits,cl[i].position);
769 for (; reg < 0x400; reg += 8) {
770 saa_writeb(reg+ 0, 0);
771 saa_writeb(reg + 1, 0);
772 saa_writeb(reg + 2, 0);
773 saa_writeb(reg + 3, 0);
777 static int clip_range(int val)
784 /* Sort into smallest position first order */
785 static int cliplist_cmp(const void *a, const void *b)
787 const struct cliplist *cla = a;
788 const struct cliplist *clb = b;
789 if (cla->position < clb->position)
791 if (cla->position > clb->position)
796 static int setup_clipping(struct saa7134_dev *dev, struct v4l2_clip *clips,
797 int nclips, int interlace)
799 struct cliplist col[16], row[16];
800 int cols = 0, rows = 0, i;
801 int div = interlace ? 2 : 1;
803 memset(col, 0, sizeof(col));
804 memset(row, 0, sizeof(row));
805 for (i = 0; i < nclips && i < 8; i++) {
806 col[cols].position = clip_range(clips[i].c.left);
807 col[cols].enable = (1 << i);
809 col[cols].position = clip_range(clips[i].c.left+clips[i].c.width);
810 col[cols].disable = (1 << i);
812 row[rows].position = clip_range(clips[i].c.top / div);
813 row[rows].enable = (1 << i);
815 row[rows].position = clip_range((clips[i].c.top + clips[i].c.height)
817 row[rows].disable = (1 << i);
820 sort(col, cols, sizeof col[0], cliplist_cmp, NULL);
821 sort(row, rows, sizeof row[0], cliplist_cmp, NULL);
822 set_cliplist(dev,0x380,col,cols,"cols");
823 set_cliplist(dev,0x384,row,rows,"rows");
827 static int verify_preview(struct saa7134_dev *dev, struct v4l2_window *win)
829 enum v4l2_field field;
832 if (NULL == dev->ovbuf.base)
834 if (NULL == dev->ovfmt)
836 if (win->w.width < 48 || win->w.height < 32)
838 if (win->clipcount > 2048)
842 maxw = dev->crop_current.width;
843 maxh = dev->crop_current.height;
845 if (V4L2_FIELD_ANY == field) {
846 field = (win->w.height > maxh/2)
847 ? V4L2_FIELD_INTERLACED
852 case V4L2_FIELD_BOTTOM:
855 case V4L2_FIELD_INTERLACED:
862 if (win->w.width > maxw)
864 if (win->w.height > maxh)
865 win->w.height = maxh;
869 static int start_preview(struct saa7134_dev *dev, struct saa7134_fh *fh)
871 unsigned long base,control,bpl;
874 err = verify_preview(dev,&fh->win);
878 dev->ovfield = fh->win.field;
879 dprintk("start_preview %dx%d+%d+%d %s field=%s\n",
880 fh->win.w.width,fh->win.w.height,
881 fh->win.w.left,fh->win.w.top,
882 dev->ovfmt->name,v4l2_field_names[dev->ovfield]);
884 /* setup window + clipping */
885 set_size(dev,TASK_B,fh->win.w.width,fh->win.w.height,
886 V4L2_FIELD_HAS_BOTH(dev->ovfield));
887 setup_clipping(dev,fh->clips,fh->nclips,
888 V4L2_FIELD_HAS_BOTH(dev->ovfield));
890 saa_andorb(SAA7134_DATA_PATH(TASK_B), 0x3f, 0x03);
892 saa_andorb(SAA7134_DATA_PATH(TASK_B), 0x3f, 0x01);
893 saa_writeb(SAA7134_OFMT_VIDEO_B, dev->ovfmt->pm | 0x20);
895 /* dma: setup channel 1 (= Video Task B) */
896 base = (unsigned long)dev->ovbuf.base;
897 base += dev->ovbuf.fmt.bytesperline * fh->win.w.top;
898 base += dev->ovfmt->depth/8 * fh->win.w.left;
899 bpl = dev->ovbuf.fmt.bytesperline;
900 control = SAA7134_RS_CONTROL_BURST_16;
901 if (dev->ovfmt->bswap)
902 control |= SAA7134_RS_CONTROL_BSWAP;
903 if (dev->ovfmt->wswap)
904 control |= SAA7134_RS_CONTROL_WSWAP;
905 if (V4L2_FIELD_HAS_BOTH(dev->ovfield)) {
906 saa_writel(SAA7134_RS_BA1(1),base);
907 saa_writel(SAA7134_RS_BA2(1),base+bpl);
908 saa_writel(SAA7134_RS_PITCH(1),bpl*2);
909 saa_writel(SAA7134_RS_CONTROL(1),control);
911 saa_writel(SAA7134_RS_BA1(1),base);
912 saa_writel(SAA7134_RS_BA2(1),base);
913 saa_writel(SAA7134_RS_PITCH(1),bpl);
914 saa_writel(SAA7134_RS_CONTROL(1),control);
919 saa7134_set_dmabits(dev);
924 static int stop_preview(struct saa7134_dev *dev, struct saa7134_fh *fh)
927 saa7134_set_dmabits(dev);
931 /* ------------------------------------------------------------------ */
933 static int buffer_activate(struct saa7134_dev *dev,
934 struct saa7134_buf *buf,
935 struct saa7134_buf *next)
937 unsigned long base,control,bpl;
938 unsigned long bpl_uv,lines_uv,base2,base3,tmp; /* planar */
940 dprintk("buffer_activate buf=%p\n",buf);
941 buf->vb.state = VIDEOBUF_ACTIVE;
944 set_size(dev,TASK_A,buf->vb.width,buf->vb.height,
945 V4L2_FIELD_HAS_BOTH(buf->vb.field));
947 saa_andorb(SAA7134_DATA_PATH(TASK_A), 0x3f, 0x03);
949 saa_andorb(SAA7134_DATA_PATH(TASK_A), 0x3f, 0x01);
950 saa_writeb(SAA7134_OFMT_VIDEO_A, buf->fmt->pm);
952 /* DMA: setup channel 0 (= Video Task A0) */
953 base = saa7134_buffer_base(buf);
954 if (buf->fmt->planar)
957 bpl = (buf->vb.width * buf->fmt->depth) / 8;
958 control = SAA7134_RS_CONTROL_BURST_16 |
959 SAA7134_RS_CONTROL_ME |
960 (buf->pt->dma >> 12);
962 control |= SAA7134_RS_CONTROL_BSWAP;
964 control |= SAA7134_RS_CONTROL_WSWAP;
965 if (V4L2_FIELD_HAS_BOTH(buf->vb.field)) {
967 saa_writel(SAA7134_RS_BA1(0),base);
968 saa_writel(SAA7134_RS_BA2(0),base+bpl);
969 saa_writel(SAA7134_RS_PITCH(0),bpl*2);
972 saa_writel(SAA7134_RS_BA1(0),base);
973 saa_writel(SAA7134_RS_BA2(0),base);
974 saa_writel(SAA7134_RS_PITCH(0),bpl);
976 saa_writel(SAA7134_RS_CONTROL(0),control);
978 if (buf->fmt->planar) {
979 /* DMA: setup channel 4+5 (= planar task A) */
980 bpl_uv = bpl >> buf->fmt->hshift;
981 lines_uv = buf->vb.height >> buf->fmt->vshift;
982 base2 = base + bpl * buf->vb.height;
983 base3 = base2 + bpl_uv * lines_uv;
984 if (buf->fmt->uvswap)
985 tmp = base2, base2 = base3, base3 = tmp;
986 dprintk("uv: bpl=%ld lines=%ld base2/3=%ld/%ld\n",
987 bpl_uv,lines_uv,base2,base3);
988 if (V4L2_FIELD_HAS_BOTH(buf->vb.field)) {
990 saa_writel(SAA7134_RS_BA1(4),base2);
991 saa_writel(SAA7134_RS_BA2(4),base2+bpl_uv);
992 saa_writel(SAA7134_RS_PITCH(4),bpl_uv*2);
993 saa_writel(SAA7134_RS_BA1(5),base3);
994 saa_writel(SAA7134_RS_BA2(5),base3+bpl_uv);
995 saa_writel(SAA7134_RS_PITCH(5),bpl_uv*2);
998 saa_writel(SAA7134_RS_BA1(4),base2);
999 saa_writel(SAA7134_RS_BA2(4),base2);
1000 saa_writel(SAA7134_RS_PITCH(4),bpl_uv);
1001 saa_writel(SAA7134_RS_BA1(5),base3);
1002 saa_writel(SAA7134_RS_BA2(5),base3);
1003 saa_writel(SAA7134_RS_PITCH(5),bpl_uv);
1005 saa_writel(SAA7134_RS_CONTROL(4),control);
1006 saa_writel(SAA7134_RS_CONTROL(5),control);
1010 saa7134_set_dmabits(dev);
1011 mod_timer(&dev->video_q.timeout, jiffies+BUFFER_TIMEOUT);
1015 static int buffer_prepare(struct videobuf_queue *q,
1016 struct videobuf_buffer *vb,
1017 enum v4l2_field field)
1019 struct saa7134_fh *fh = q->priv_data;
1020 struct saa7134_dev *dev = fh->dev;
1021 struct saa7134_buf *buf = container_of(vb,struct saa7134_buf,vb);
1026 if (NULL == fh->fmt)
1028 if (fh->width < 48 ||
1030 fh->width/4 > dev->crop_current.width ||
1031 fh->height/4 > dev->crop_current.height ||
1032 fh->width > dev->crop_bounds.width ||
1033 fh->height > dev->crop_bounds.height)
1035 size = (fh->width * fh->height * fh->fmt->depth) >> 3;
1036 if (0 != buf->vb.baddr && buf->vb.bsize < size)
1039 dprintk("buffer_prepare [%d,size=%dx%d,bytes=%d,fields=%s,%s]\n",
1040 vb->i,fh->width,fh->height,size,v4l2_field_names[field],
1042 if (buf->vb.width != fh->width ||
1043 buf->vb.height != fh->height ||
1044 buf->vb.size != size ||
1045 buf->vb.field != field ||
1046 buf->fmt != fh->fmt) {
1047 saa7134_dma_free(q,buf);
1050 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
1051 struct videobuf_dmabuf *dma=videobuf_to_dma(&buf->vb);
1053 buf->vb.width = fh->width;
1054 buf->vb.height = fh->height;
1055 buf->vb.size = size;
1056 buf->vb.field = field;
1058 buf->pt = &fh->pt_cap;
1060 err = videobuf_iolock(q,&buf->vb,&dev->ovbuf);
1063 err = saa7134_pgtable_build(dev->pci,buf->pt,
1066 saa7134_buffer_startpage(buf));
1070 buf->vb.state = VIDEOBUF_PREPARED;
1071 buf->activate = buffer_activate;
1075 saa7134_dma_free(q,buf);
1080 buffer_setup(struct videobuf_queue *q, unsigned int *count, unsigned int *size)
1082 struct saa7134_fh *fh = q->priv_data;
1084 *size = fh->fmt->depth * fh->width * fh->height >> 3;
1087 *count = saa7134_buffer_count(*size,*count);
1091 static void buffer_queue(struct videobuf_queue *q, struct videobuf_buffer *vb)
1093 struct saa7134_fh *fh = q->priv_data;
1094 struct saa7134_buf *buf = container_of(vb,struct saa7134_buf,vb);
1096 saa7134_buffer_queue(fh->dev,&fh->dev->video_q,buf);
1099 static void buffer_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
1101 struct saa7134_buf *buf = container_of(vb,struct saa7134_buf,vb);
1103 saa7134_dma_free(q,buf);
1106 static struct videobuf_queue_ops video_qops = {
1107 .buf_setup = buffer_setup,
1108 .buf_prepare = buffer_prepare,
1109 .buf_queue = buffer_queue,
1110 .buf_release = buffer_release,
1113 /* ------------------------------------------------------------------ */
1115 int saa7134_g_ctrl_internal(struct saa7134_dev *dev, struct saa7134_fh *fh, struct v4l2_control *c)
1117 const struct v4l2_queryctrl* ctrl;
1119 ctrl = ctrl_by_id(c->id);
1123 case V4L2_CID_BRIGHTNESS:
1124 c->value = dev->ctl_bright;
1127 c->value = dev->ctl_hue;
1129 case V4L2_CID_CONTRAST:
1130 c->value = dev->ctl_contrast;
1132 case V4L2_CID_SATURATION:
1133 c->value = dev->ctl_saturation;
1135 case V4L2_CID_AUDIO_MUTE:
1136 c->value = dev->ctl_mute;
1138 case V4L2_CID_AUDIO_VOLUME:
1139 c->value = dev->ctl_volume;
1141 case V4L2_CID_PRIVATE_INVERT:
1142 c->value = dev->ctl_invert;
1144 case V4L2_CID_HFLIP:
1145 c->value = dev->ctl_mirror;
1147 case V4L2_CID_PRIVATE_Y_EVEN:
1148 c->value = dev->ctl_y_even;
1150 case V4L2_CID_PRIVATE_Y_ODD:
1151 c->value = dev->ctl_y_odd;
1153 case V4L2_CID_PRIVATE_AUTOMUTE:
1154 c->value = dev->ctl_automute;
1161 EXPORT_SYMBOL_GPL(saa7134_g_ctrl_internal);
1163 static int saa7134_g_ctrl(struct file *file, void *priv, struct v4l2_control *c)
1165 struct saa7134_fh *fh = priv;
1167 return saa7134_g_ctrl_internal(fh->dev, fh, c);
1170 int saa7134_s_ctrl_internal(struct saa7134_dev *dev, struct saa7134_fh *fh, struct v4l2_control *c)
1172 const struct v4l2_queryctrl* ctrl;
1173 unsigned long flags;
1174 int restart_overlay = 0;
1177 /* When called from the empress code fh == NULL.
1178 That needs to be fixed somehow, but for now this is
1181 err = v4l2_prio_check(&dev->prio, &fh->prio);
1187 mutex_lock(&dev->lock);
1189 ctrl = ctrl_by_id(c->id);
1193 dprintk("set_control name=%s val=%d\n",ctrl->name,c->value);
1194 switch (ctrl->type) {
1195 case V4L2_CTRL_TYPE_BOOLEAN:
1196 case V4L2_CTRL_TYPE_MENU:
1197 case V4L2_CTRL_TYPE_INTEGER:
1198 if (c->value < ctrl->minimum)
1199 c->value = ctrl->minimum;
1200 if (c->value > ctrl->maximum)
1201 c->value = ctrl->maximum;
1207 case V4L2_CID_BRIGHTNESS:
1208 dev->ctl_bright = c->value;
1209 saa_writeb(SAA7134_DEC_LUMA_BRIGHT, dev->ctl_bright);
1212 dev->ctl_hue = c->value;
1213 saa_writeb(SAA7134_DEC_CHROMA_HUE, dev->ctl_hue);
1215 case V4L2_CID_CONTRAST:
1216 dev->ctl_contrast = c->value;
1217 saa_writeb(SAA7134_DEC_LUMA_CONTRAST,
1218 dev->ctl_invert ? -dev->ctl_contrast : dev->ctl_contrast);
1220 case V4L2_CID_SATURATION:
1221 dev->ctl_saturation = c->value;
1222 saa_writeb(SAA7134_DEC_CHROMA_SATURATION,
1223 dev->ctl_invert ? -dev->ctl_saturation : dev->ctl_saturation);
1225 case V4L2_CID_AUDIO_MUTE:
1226 dev->ctl_mute = c->value;
1227 saa7134_tvaudio_setmute(dev);
1229 case V4L2_CID_AUDIO_VOLUME:
1230 dev->ctl_volume = c->value;
1231 saa7134_tvaudio_setvolume(dev,dev->ctl_volume);
1233 case V4L2_CID_PRIVATE_INVERT:
1234 dev->ctl_invert = c->value;
1235 saa_writeb(SAA7134_DEC_LUMA_CONTRAST,
1236 dev->ctl_invert ? -dev->ctl_contrast : dev->ctl_contrast);
1237 saa_writeb(SAA7134_DEC_CHROMA_SATURATION,
1238 dev->ctl_invert ? -dev->ctl_saturation : dev->ctl_saturation);
1240 case V4L2_CID_HFLIP:
1241 dev->ctl_mirror = c->value;
1242 restart_overlay = 1;
1244 case V4L2_CID_PRIVATE_Y_EVEN:
1245 dev->ctl_y_even = c->value;
1246 restart_overlay = 1;
1248 case V4L2_CID_PRIVATE_Y_ODD:
1249 dev->ctl_y_odd = c->value;
1250 restart_overlay = 1;
1252 case V4L2_CID_PRIVATE_AUTOMUTE:
1254 struct v4l2_priv_tun_config tda9887_cfg;
1256 tda9887_cfg.tuner = TUNER_TDA9887;
1257 tda9887_cfg.priv = &dev->tda9887_conf;
1259 dev->ctl_automute = c->value;
1260 if (dev->tda9887_conf) {
1261 if (dev->ctl_automute)
1262 dev->tda9887_conf |= TDA9887_AUTOMUTE;
1264 dev->tda9887_conf &= ~TDA9887_AUTOMUTE;
1266 saa7134_i2c_call_clients(dev, TUNER_SET_CONFIG,
1274 if (restart_overlay && fh && res_check(fh, RESOURCE_OVERLAY)) {
1275 spin_lock_irqsave(&dev->slock,flags);
1276 stop_preview(dev,fh);
1277 start_preview(dev,fh);
1278 spin_unlock_irqrestore(&dev->slock,flags);
1283 mutex_unlock(&dev->lock);
1286 EXPORT_SYMBOL_GPL(saa7134_s_ctrl_internal);
1288 static int saa7134_s_ctrl(struct file *file, void *f, struct v4l2_control *c)
1290 struct saa7134_fh *fh = f;
1292 return saa7134_s_ctrl_internal(fh->dev, fh, c);
1295 /* ------------------------------------------------------------------ */
1297 static struct videobuf_queue* saa7134_queue(struct saa7134_fh *fh)
1299 struct videobuf_queue* q = NULL;
1302 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1305 case V4L2_BUF_TYPE_VBI_CAPTURE:
1314 static int saa7134_resource(struct saa7134_fh *fh)
1316 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1317 return RESOURCE_VIDEO;
1319 if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)
1320 return RESOURCE_VBI;
1326 static int video_open(struct inode *inode, struct file *file)
1328 int minor = iminor(inode);
1329 struct saa7134_dev *dev;
1330 struct saa7134_fh *fh;
1331 enum v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1333 list_for_each_entry(dev, &saa7134_devlist, devlist) {
1334 if (dev->video_dev && (dev->video_dev->minor == minor))
1336 if (dev->radio_dev && (dev->radio_dev->minor == minor)) {
1340 if (dev->vbi_dev && (dev->vbi_dev->minor == minor)) {
1341 type = V4L2_BUF_TYPE_VBI_CAPTURE;
1348 dprintk("open minor=%d radio=%d type=%s\n",minor,radio,
1349 v4l2_type_names[type]);
1351 /* allocate + initialize per filehandle data */
1352 fh = kzalloc(sizeof(*fh),GFP_KERNEL);
1355 file->private_data = fh;
1359 fh->fmt = format_by_fourcc(V4L2_PIX_FMT_BGR24);
1362 v4l2_prio_open(&dev->prio,&fh->prio);
1364 videobuf_queue_sg_init(&fh->cap, &video_qops,
1365 &dev->pci->dev, &dev->slock,
1366 V4L2_BUF_TYPE_VIDEO_CAPTURE,
1367 V4L2_FIELD_INTERLACED,
1368 sizeof(struct saa7134_buf),
1370 videobuf_queue_sg_init(&fh->vbi, &saa7134_vbi_qops,
1371 &dev->pci->dev, &dev->slock,
1372 V4L2_BUF_TYPE_VBI_CAPTURE,
1374 sizeof(struct saa7134_buf),
1376 saa7134_pgtable_alloc(dev->pci,&fh->pt_cap);
1377 saa7134_pgtable_alloc(dev->pci,&fh->pt_vbi);
1380 /* switch to radio mode */
1381 saa7134_tvaudio_setinput(dev,&card(dev).radio);
1382 saa7134_i2c_call_clients(dev,AUDC_SET_RADIO, NULL);
1384 /* switch to video/vbi mode */
1385 video_mux(dev,dev->ctl_input);
1391 video_read(struct file *file, char __user *data, size_t count, loff_t *ppos)
1393 struct saa7134_fh *fh = file->private_data;
1396 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1397 if (res_locked(fh->dev,RESOURCE_VIDEO))
1399 return videobuf_read_one(saa7134_queue(fh),
1401 file->f_flags & O_NONBLOCK);
1402 case V4L2_BUF_TYPE_VBI_CAPTURE:
1403 if (!res_get(fh->dev,fh,RESOURCE_VBI))
1405 return videobuf_read_stream(saa7134_queue(fh),
1406 data, count, ppos, 1,
1407 file->f_flags & O_NONBLOCK);
1416 video_poll(struct file *file, struct poll_table_struct *wait)
1418 struct saa7134_fh *fh = file->private_data;
1419 struct videobuf_buffer *buf = NULL;
1421 if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type)
1422 return videobuf_poll_stream(file, &fh->vbi, wait);
1424 if (res_check(fh,RESOURCE_VIDEO)) {
1425 if (!list_empty(&fh->cap.stream))
1426 buf = list_entry(fh->cap.stream.next, struct videobuf_buffer, stream);
1428 mutex_lock(&fh->cap.vb_lock);
1429 if (UNSET == fh->cap.read_off) {
1430 /* need to capture a new frame */
1431 if (res_locked(fh->dev,RESOURCE_VIDEO))
1433 if (0 != fh->cap.ops->buf_prepare(&fh->cap,fh->cap.read_buf,fh->cap.field))
1435 fh->cap.ops->buf_queue(&fh->cap,fh->cap.read_buf);
1436 fh->cap.read_off = 0;
1438 mutex_unlock(&fh->cap.vb_lock);
1439 buf = fh->cap.read_buf;
1445 poll_wait(file, &buf->done, wait);
1446 if (buf->state == VIDEOBUF_DONE ||
1447 buf->state == VIDEOBUF_ERROR)
1448 return POLLIN|POLLRDNORM;
1452 mutex_unlock(&fh->cap.vb_lock);
1456 static int video_release(struct inode *inode, struct file *file)
1458 struct saa7134_fh *fh = file->private_data;
1459 struct saa7134_dev *dev = fh->dev;
1460 unsigned long flags;
1462 /* turn off overlay */
1463 if (res_check(fh, RESOURCE_OVERLAY)) {
1464 spin_lock_irqsave(&dev->slock,flags);
1465 stop_preview(dev,fh);
1466 spin_unlock_irqrestore(&dev->slock,flags);
1467 res_free(dev,fh,RESOURCE_OVERLAY);
1470 /* stop video capture */
1471 if (res_check(fh, RESOURCE_VIDEO)) {
1472 videobuf_streamoff(&fh->cap);
1473 res_free(dev,fh,RESOURCE_VIDEO);
1475 if (fh->cap.read_buf) {
1476 buffer_release(&fh->cap,fh->cap.read_buf);
1477 kfree(fh->cap.read_buf);
1480 /* stop vbi capture */
1481 if (res_check(fh, RESOURCE_VBI)) {
1482 videobuf_stop(&fh->vbi);
1483 res_free(dev,fh,RESOURCE_VBI);
1486 /* ts-capture will not work in planar mode, so turn it off Hac: 04.05*/
1487 saa_andorb(SAA7134_OFMT_VIDEO_A, 0x1f, 0);
1488 saa_andorb(SAA7134_OFMT_VIDEO_B, 0x1f, 0);
1489 saa_andorb(SAA7134_OFMT_DATA_A, 0x1f, 0);
1490 saa_andorb(SAA7134_OFMT_DATA_B, 0x1f, 0);
1492 saa7134_i2c_call_clients(dev, TUNER_SET_STANDBY, NULL);
1495 videobuf_mmap_free(&fh->cap);
1496 videobuf_mmap_free(&fh->vbi);
1497 saa7134_pgtable_free(dev->pci,&fh->pt_cap);
1498 saa7134_pgtable_free(dev->pci,&fh->pt_vbi);
1500 v4l2_prio_close(&dev->prio,&fh->prio);
1501 file->private_data = NULL;
1506 static int video_mmap(struct file *file, struct vm_area_struct * vma)
1508 struct saa7134_fh *fh = file->private_data;
1510 return videobuf_mmap_mapper(saa7134_queue(fh), vma);
1513 /* ------------------------------------------------------------------ */
1515 static int saa7134_try_get_set_fmt_vbi_cap(struct file *file, void *priv,
1516 struct v4l2_format *f)
1518 struct saa7134_fh *fh = priv;
1519 struct saa7134_dev *dev = fh->dev;
1520 struct saa7134_tvnorm *norm = dev->tvnorm;
1522 f->fmt.vbi.sampling_rate = 6750000 * 4;
1523 f->fmt.vbi.samples_per_line = 2048 /* VBI_LINE_LENGTH */;
1524 f->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
1525 f->fmt.vbi.offset = 64 * 4;
1526 f->fmt.vbi.start[0] = norm->vbi_v_start_0;
1527 f->fmt.vbi.count[0] = norm->vbi_v_stop_0 - norm->vbi_v_start_0 +1;
1528 f->fmt.vbi.start[1] = norm->vbi_v_start_1;
1529 f->fmt.vbi.count[1] = f->fmt.vbi.count[0];
1530 f->fmt.vbi.flags = 0; /* VBI_UNSYNC VBI_INTERLACED */
1535 static int saa7134_g_fmt_vid_cap(struct file *file, void *priv,
1536 struct v4l2_format *f)
1538 struct saa7134_fh *fh = priv;
1540 f->fmt.pix.width = fh->width;
1541 f->fmt.pix.height = fh->height;
1542 f->fmt.pix.field = fh->cap.field;
1543 f->fmt.pix.pixelformat = fh->fmt->fourcc;
1544 f->fmt.pix.bytesperline =
1545 (f->fmt.pix.width * fh->fmt->depth) >> 3;
1546 f->fmt.pix.sizeimage =
1547 f->fmt.pix.height * f->fmt.pix.bytesperline;
1551 static int saa7134_g_fmt_vid_overlay(struct file *file, void *priv,
1552 struct v4l2_format *f)
1554 struct saa7134_fh *fh = priv;
1556 if (saa7134_no_overlay > 0) {
1557 printk(KERN_ERR "V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
1560 f->fmt.win = fh->win;
1565 static int saa7134_try_fmt_vid_cap(struct file *file, void *priv,
1566 struct v4l2_format *f)
1568 struct saa7134_fh *fh = priv;
1569 struct saa7134_dev *dev = fh->dev;
1570 struct saa7134_format *fmt;
1571 enum v4l2_field field;
1572 unsigned int maxw, maxh;
1574 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1578 field = f->fmt.pix.field;
1579 maxw = min(dev->crop_current.width*4, dev->crop_bounds.width);
1580 maxh = min(dev->crop_current.height*4, dev->crop_bounds.height);
1582 if (V4L2_FIELD_ANY == field) {
1583 field = (f->fmt.pix.height > maxh/2)
1584 ? V4L2_FIELD_INTERLACED
1585 : V4L2_FIELD_BOTTOM;
1588 case V4L2_FIELD_TOP:
1589 case V4L2_FIELD_BOTTOM:
1592 case V4L2_FIELD_INTERLACED:
1598 f->fmt.pix.field = field;
1599 if (f->fmt.pix.width < 48)
1600 f->fmt.pix.width = 48;
1601 if (f->fmt.pix.height < 32)
1602 f->fmt.pix.height = 32;
1603 if (f->fmt.pix.width > maxw)
1604 f->fmt.pix.width = maxw;
1605 if (f->fmt.pix.height > maxh)
1606 f->fmt.pix.height = maxh;
1607 f->fmt.pix.width &= ~0x03;
1608 f->fmt.pix.bytesperline =
1609 (f->fmt.pix.width * fmt->depth) >> 3;
1610 f->fmt.pix.sizeimage =
1611 f->fmt.pix.height * f->fmt.pix.bytesperline;
1616 static int saa7134_try_fmt_vid_overlay(struct file *file, void *priv,
1617 struct v4l2_format *f)
1619 struct saa7134_fh *fh = priv;
1620 struct saa7134_dev *dev = fh->dev;
1622 if (saa7134_no_overlay > 0) {
1623 printk(KERN_ERR "V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
1627 return verify_preview(dev, &f->fmt.win);
1630 static int saa7134_s_fmt_vid_cap(struct file *file, void *priv,
1631 struct v4l2_format *f)
1633 struct saa7134_fh *fh = priv;
1636 err = saa7134_try_fmt_vid_cap(file, priv, f);
1640 fh->fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1641 fh->width = f->fmt.pix.width;
1642 fh->height = f->fmt.pix.height;
1643 fh->cap.field = f->fmt.pix.field;
1647 static int saa7134_s_fmt_vid_overlay(struct file *file, void *priv,
1648 struct v4l2_format *f)
1650 struct saa7134_fh *fh = priv;
1651 struct saa7134_dev *dev = fh->dev;
1653 unsigned long flags;
1655 if (saa7134_no_overlay > 0) {
1656 printk(KERN_ERR "V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
1659 err = verify_preview(dev, &f->fmt.win);
1663 mutex_lock(&dev->lock);
1665 fh->win = f->fmt.win;
1666 fh->nclips = f->fmt.win.clipcount;
1671 if (copy_from_user(fh->clips, f->fmt.win.clips,
1672 sizeof(struct v4l2_clip)*fh->nclips)) {
1673 mutex_unlock(&dev->lock);
1677 if (res_check(fh, RESOURCE_OVERLAY)) {
1678 spin_lock_irqsave(&dev->slock, flags);
1679 stop_preview(dev, fh);
1680 start_preview(dev, fh);
1681 spin_unlock_irqrestore(&dev->slock, flags);
1684 mutex_unlock(&dev->lock);
1688 int saa7134_queryctrl(struct file *file, void *priv, struct v4l2_queryctrl *c)
1690 const struct v4l2_queryctrl *ctrl;
1692 if ((c->id < V4L2_CID_BASE ||
1693 c->id >= V4L2_CID_LASTP1) &&
1694 (c->id < V4L2_CID_PRIVATE_BASE ||
1695 c->id >= V4L2_CID_PRIVATE_LASTP1))
1697 ctrl = ctrl_by_id(c->id);
1698 *c = (NULL != ctrl) ? *ctrl : no_ctrl;
1701 EXPORT_SYMBOL_GPL(saa7134_queryctrl);
1703 static int saa7134_enum_input(struct file *file, void *priv,
1704 struct v4l2_input *i)
1706 struct saa7134_fh *fh = priv;
1707 struct saa7134_dev *dev = fh->dev;
1711 if (n >= SAA7134_INPUT_MAX)
1713 if (NULL == card_in(dev, i->index).name)
1715 memset(i, 0, sizeof(*i));
1717 i->type = V4L2_INPUT_TYPE_CAMERA;
1718 strcpy(i->name, card_in(dev, n).name);
1719 if (card_in(dev, n).tv)
1720 i->type = V4L2_INPUT_TYPE_TUNER;
1722 if (n == dev->ctl_input) {
1723 int v1 = saa_readb(SAA7134_STATUS_VIDEO1);
1724 int v2 = saa_readb(SAA7134_STATUS_VIDEO2);
1726 if (0 != (v1 & 0x40))
1727 i->status |= V4L2_IN_ST_NO_H_LOCK;
1728 if (0 != (v2 & 0x40))
1729 i->status |= V4L2_IN_ST_NO_SYNC;
1730 if (0 != (v2 & 0x0e))
1731 i->status |= V4L2_IN_ST_MACROVISION;
1733 i->std = SAA7134_NORMS;
1737 static int saa7134_g_input(struct file *file, void *priv, unsigned int *i)
1739 struct saa7134_fh *fh = priv;
1740 struct saa7134_dev *dev = fh->dev;
1742 *i = dev->ctl_input;
1746 static int saa7134_s_input(struct file *file, void *priv, unsigned int i)
1748 struct saa7134_fh *fh = priv;
1749 struct saa7134_dev *dev = fh->dev;
1752 err = v4l2_prio_check(&dev->prio, &fh->prio);
1756 if (i < 0 || i >= SAA7134_INPUT_MAX)
1758 if (NULL == card_in(dev, i).name)
1760 mutex_lock(&dev->lock);
1762 mutex_unlock(&dev->lock);
1766 static int saa7134_querycap(struct file *file, void *priv,
1767 struct v4l2_capability *cap)
1769 struct saa7134_fh *fh = priv;
1770 struct saa7134_dev *dev = fh->dev;
1772 unsigned int tuner_type = dev->tuner_type;
1774 strcpy(cap->driver, "saa7134");
1775 strlcpy(cap->card, saa7134_boards[dev->board].name,
1777 sprintf(cap->bus_info, "PCI:%s", pci_name(dev->pci));
1778 cap->version = SAA7134_VERSION_CODE;
1780 V4L2_CAP_VIDEO_CAPTURE |
1781 V4L2_CAP_VBI_CAPTURE |
1782 V4L2_CAP_READWRITE |
1783 V4L2_CAP_STREAMING |
1785 if (saa7134_no_overlay <= 0)
1786 cap->capabilities |= V4L2_CAP_VIDEO_OVERLAY;
1788 if ((tuner_type == TUNER_ABSENT) || (tuner_type == UNSET))
1789 cap->capabilities &= ~V4L2_CAP_TUNER;
1793 static int saa7134_s_std(struct file *file, void *priv, v4l2_std_id *id)
1795 struct saa7134_fh *fh = priv;
1796 struct saa7134_dev *dev = fh->dev;
1797 unsigned long flags;
1802 err = v4l2_prio_check(&dev->prio, &fh->prio);
1806 for (i = 0; i < TVNORMS; i++)
1807 if (*id == tvnorms[i].id)
1811 for (i = 0; i < TVNORMS; i++)
1812 if (*id & tvnorms[i].id)
1817 if ((*id & V4L2_STD_SECAM) && (secam[0] != '-')) {
1818 if (secam[0] == 'L' || secam[0] == 'l') {
1819 if (secam[1] == 'C' || secam[1] == 'c')
1820 fixup = V4L2_STD_SECAM_LC;
1822 fixup = V4L2_STD_SECAM_L;
1824 if (secam[0] == 'D' || secam[0] == 'd')
1825 fixup = V4L2_STD_SECAM_DK;
1827 fixup = V4L2_STD_SECAM;
1829 for (i = 0; i < TVNORMS; i++)
1830 if (fixup == tvnorms[i].id)
1834 *id = tvnorms[i].id;
1836 mutex_lock(&dev->lock);
1837 if (res_check(fh, RESOURCE_OVERLAY)) {
1838 spin_lock_irqsave(&dev->slock, flags);
1839 stop_preview(dev, fh);
1840 spin_unlock_irqrestore(&dev->slock, flags);
1842 set_tvnorm(dev, &tvnorms[i]);
1844 spin_lock_irqsave(&dev->slock, flags);
1845 start_preview(dev, fh);
1846 spin_unlock_irqrestore(&dev->slock, flags);
1848 set_tvnorm(dev, &tvnorms[i]);
1850 saa7134_tvaudio_do_scan(dev);
1851 mutex_unlock(&dev->lock);
1855 static int saa7134_cropcap(struct file *file, void *priv,
1856 struct v4l2_cropcap *cap)
1858 struct saa7134_fh *fh = priv;
1859 struct saa7134_dev *dev = fh->dev;
1861 if (cap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1862 cap->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
1864 cap->bounds = dev->crop_bounds;
1865 cap->defrect = dev->crop_defrect;
1866 cap->pixelaspect.numerator = 1;
1867 cap->pixelaspect.denominator = 1;
1868 if (dev->tvnorm->id & V4L2_STD_525_60) {
1869 cap->pixelaspect.numerator = 11;
1870 cap->pixelaspect.denominator = 10;
1872 if (dev->tvnorm->id & V4L2_STD_625_50) {
1873 cap->pixelaspect.numerator = 54;
1874 cap->pixelaspect.denominator = 59;
1879 static int saa7134_g_crop(struct file *file, void *f, struct v4l2_crop *crop)
1881 struct saa7134_fh *fh = f;
1882 struct saa7134_dev *dev = fh->dev;
1884 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1885 crop->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
1887 crop->c = dev->crop_current;
1891 static int saa7134_s_crop(struct file *file, void *f, struct v4l2_crop *crop)
1893 struct saa7134_fh *fh = f;
1894 struct saa7134_dev *dev = fh->dev;
1895 struct v4l2_rect *b = &dev->crop_bounds;
1897 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1898 crop->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
1900 if (crop->c.height < 0)
1902 if (crop->c.width < 0)
1905 if (res_locked(fh->dev, RESOURCE_OVERLAY))
1907 if (res_locked(fh->dev, RESOURCE_VIDEO))
1910 if (crop->c.top < b->top)
1911 crop->c.top = b->top;
1912 if (crop->c.top > b->top + b->height)
1913 crop->c.top = b->top + b->height;
1914 if (crop->c.height > b->top - crop->c.top + b->height)
1915 crop->c.height = b->top - crop->c.top + b->height;
1917 if (crop->c.left < b->left)
1918 crop->c.left = b->left;
1919 if (crop->c.left > b->left + b->width)
1920 crop->c.left = b->left + b->width;
1921 if (crop->c.width > b->left - crop->c.left + b->width)
1922 crop->c.width = b->left - crop->c.left + b->width;
1924 dev->crop_current = crop->c;
1928 static int saa7134_g_tuner(struct file *file, void *priv,
1929 struct v4l2_tuner *t)
1931 struct saa7134_fh *fh = priv;
1932 struct saa7134_dev *dev = fh->dev;
1937 memset(t, 0, sizeof(*t));
1938 for (n = 0; n < SAA7134_INPUT_MAX; n++)
1939 if (card_in(dev, n).tv)
1941 if (NULL != card_in(dev, n).name) {
1942 strcpy(t->name, "Television");
1943 t->type = V4L2_TUNER_ANALOG_TV;
1944 t->capability = V4L2_TUNER_CAP_NORM |
1945 V4L2_TUNER_CAP_STEREO |
1946 V4L2_TUNER_CAP_LANG1 |
1947 V4L2_TUNER_CAP_LANG2;
1948 t->rangehigh = 0xffffffffUL;
1949 t->rxsubchans = saa7134_tvaudio_getstereo(dev);
1950 t->audmode = saa7134_tvaudio_rx2mode(t->rxsubchans);
1952 if (0 != (saa_readb(SAA7134_STATUS_VIDEO1) & 0x03))
1957 static int saa7134_s_tuner(struct file *file, void *priv,
1958 struct v4l2_tuner *t)
1960 struct saa7134_fh *fh = priv;
1961 struct saa7134_dev *dev = fh->dev;
1964 err = v4l2_prio_check(&dev->prio, &fh->prio);
1968 mode = dev->thread.mode;
1969 if (UNSET == mode) {
1970 rx = saa7134_tvaudio_getstereo(dev);
1971 mode = saa7134_tvaudio_rx2mode(t->rxsubchans);
1973 if (mode != t->audmode)
1974 dev->thread.mode = t->audmode;
1979 static int saa7134_g_frequency(struct file *file, void *priv,
1980 struct v4l2_frequency *f)
1982 struct saa7134_fh *fh = priv;
1983 struct saa7134_dev *dev = fh->dev;
1985 f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1986 f->frequency = dev->ctl_freq;
1991 static int saa7134_s_frequency(struct file *file, void *priv,
1992 struct v4l2_frequency *f)
1994 struct saa7134_fh *fh = priv;
1995 struct saa7134_dev *dev = fh->dev;
1998 err = v4l2_prio_check(&dev->prio, &fh->prio);
2004 if (0 == fh->radio && V4L2_TUNER_ANALOG_TV != f->type)
2006 if (1 == fh->radio && V4L2_TUNER_RADIO != f->type)
2008 mutex_lock(&dev->lock);
2009 dev->ctl_freq = f->frequency;
2011 saa7134_i2c_call_clients(dev, VIDIOC_S_FREQUENCY, f);
2013 saa7134_tvaudio_do_scan(dev);
2014 mutex_unlock(&dev->lock);
2018 static int saa7134_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
2020 strcpy(a->name, "audio");
2024 static int saa7134_s_audio(struct file *file, void *priv, struct v4l2_audio *a)
2029 static int saa7134_g_priority(struct file *file, void *f, enum v4l2_priority *p)
2031 struct saa7134_fh *fh = f;
2032 struct saa7134_dev *dev = fh->dev;
2034 *p = v4l2_prio_max(&dev->prio);
2038 static int saa7134_s_priority(struct file *file, void *f,
2039 enum v4l2_priority prio)
2041 struct saa7134_fh *fh = f;
2042 struct saa7134_dev *dev = fh->dev;
2044 return v4l2_prio_change(&dev->prio, &fh->prio, prio);
2047 static int saa7134_enum_fmt_vid_cap(struct file *file, void *priv,
2048 struct v4l2_fmtdesc *f)
2050 if (f->index >= FORMATS)
2053 strlcpy(f->description, formats[f->index].name,
2054 sizeof(f->description));
2056 f->pixelformat = formats[f->index].fourcc;
2061 static int saa7134_enum_fmt_vid_overlay(struct file *file, void *priv,
2062 struct v4l2_fmtdesc *f)
2064 if (saa7134_no_overlay > 0) {
2065 printk(KERN_ERR "V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
2069 if ((f->index >= FORMATS) || formats[f->index].planar)
2072 strlcpy(f->description, formats[f->index].name,
2073 sizeof(f->description));
2075 f->pixelformat = formats[f->index].fourcc;
2080 static int saa7134_enum_fmt_vbi_cap(struct file *file, void *priv,
2081 struct v4l2_fmtdesc *f)
2086 f->pixelformat = V4L2_PIX_FMT_GREY;
2087 strcpy(f->description, "vbi data");
2092 static int saa7134_g_fbuf(struct file *file, void *f,
2093 struct v4l2_framebuffer *fb)
2095 struct saa7134_fh *fh = f;
2096 struct saa7134_dev *dev = fh->dev;
2099 fb->capability = V4L2_FBUF_CAP_LIST_CLIPPING;
2104 static int saa7134_s_fbuf(struct file *file, void *f,
2105 struct v4l2_framebuffer *fb)
2107 struct saa7134_fh *fh = f;
2108 struct saa7134_dev *dev = fh->dev;
2109 struct saa7134_format *fmt;
2111 if (!capable(CAP_SYS_ADMIN) &&
2112 !capable(CAP_SYS_RAWIO))
2116 fmt = format_by_fourcc(fb->fmt.pixelformat);
2123 if (0 == dev->ovbuf.fmt.bytesperline)
2124 dev->ovbuf.fmt.bytesperline =
2125 dev->ovbuf.fmt.width*fmt->depth/8;
2129 static int saa7134_overlay(struct file *file, void *f, unsigned int on)
2131 struct saa7134_fh *fh = f;
2132 struct saa7134_dev *dev = fh->dev;
2133 unsigned long flags;
2136 if (saa7134_no_overlay > 0) {
2137 dprintk("no_overlay\n");
2141 if (!res_get(dev, fh, RESOURCE_OVERLAY))
2143 spin_lock_irqsave(&dev->slock, flags);
2144 start_preview(dev, fh);
2145 spin_unlock_irqrestore(&dev->slock, flags);
2148 if (!res_check(fh, RESOURCE_OVERLAY))
2150 spin_lock_irqsave(&dev->slock, flags);
2151 stop_preview(dev, fh);
2152 spin_unlock_irqrestore(&dev->slock, flags);
2153 res_free(dev, fh, RESOURCE_OVERLAY);
2158 #ifdef CONFIG_VIDEO_V4L1_COMPAT
2159 static int vidiocgmbuf(struct file *file, void *priv, struct video_mbuf *mbuf)
2161 struct saa7134_fh *fh = file->private_data;
2162 return videobuf_cgmbuf(saa7134_queue(fh), mbuf, 8);
2166 static int saa7134_reqbufs(struct file *file, void *priv,
2167 struct v4l2_requestbuffers *p)
2169 struct saa7134_fh *fh = priv;
2170 return videobuf_reqbufs(saa7134_queue(fh), p);
2173 static int saa7134_querybuf(struct file *file, void *priv,
2174 struct v4l2_buffer *b)
2176 struct saa7134_fh *fh = priv;
2177 return videobuf_querybuf(saa7134_queue(fh), b);
2180 static int saa7134_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
2182 struct saa7134_fh *fh = priv;
2183 return videobuf_qbuf(saa7134_queue(fh), b);
2186 static int saa7134_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
2188 struct saa7134_fh *fh = priv;
2189 return videobuf_dqbuf(saa7134_queue(fh), b,
2190 file->f_flags & O_NONBLOCK);
2193 static int saa7134_streamon(struct file *file, void *priv,
2194 enum v4l2_buf_type type)
2196 struct saa7134_fh *fh = priv;
2197 struct saa7134_dev *dev = fh->dev;
2198 int res = saa7134_resource(fh);
2200 if (!res_get(dev, fh, res))
2203 return videobuf_streamon(saa7134_queue(fh));
2206 static int saa7134_streamoff(struct file *file, void *priv,
2207 enum v4l2_buf_type type)
2210 struct saa7134_fh *fh = priv;
2211 struct saa7134_dev *dev = fh->dev;
2212 int res = saa7134_resource(fh);
2214 err = videobuf_streamoff(saa7134_queue(fh));
2217 res_free(dev, fh, res);
2221 static int saa7134_g_parm(struct file *file, void *fh,
2222 struct v4l2_streamparm *parm)
2227 #ifdef CONFIG_VIDEO_ADV_DEBUG
2228 static int vidioc_g_register (struct file *file, void *priv,
2229 struct v4l2_register *reg)
2231 struct saa7134_fh *fh = priv;
2232 struct saa7134_dev *dev = fh->dev;
2234 if (!v4l2_chip_match_host(reg->match_type, reg->match_chip))
2236 reg->val = saa_readb(reg->reg);
2240 static int vidioc_s_register (struct file *file, void *priv,
2241 struct v4l2_register *reg)
2243 struct saa7134_fh *fh = priv;
2244 struct saa7134_dev *dev = fh->dev;
2246 if (!v4l2_chip_match_host(reg->match_type, reg->match_chip))
2248 saa_writeb(reg->reg&0xffffff, reg->val);
2253 static int radio_querycap(struct file *file, void *priv,
2254 struct v4l2_capability *cap)
2256 struct saa7134_fh *fh = file->private_data;
2257 struct saa7134_dev *dev = fh->dev;
2259 strcpy(cap->driver, "saa7134");
2260 strlcpy(cap->card, saa7134_boards[dev->board].name, sizeof(cap->card));
2261 sprintf(cap->bus_info, "PCI:%s", pci_name(dev->pci));
2262 cap->version = SAA7134_VERSION_CODE;
2263 cap->capabilities = V4L2_CAP_TUNER;
2267 static int radio_g_tuner(struct file *file, void *priv,
2268 struct v4l2_tuner *t)
2270 struct saa7134_fh *fh = file->private_data;
2271 struct saa7134_dev *dev = fh->dev;
2276 memset(t, 0, sizeof(*t));
2277 strcpy(t->name, "Radio");
2278 t->type = V4L2_TUNER_RADIO;
2280 saa7134_i2c_call_clients(dev, VIDIOC_G_TUNER, t);
2281 if (dev->input->amux == TV) {
2282 t->signal = 0xf800 - ((saa_readb(0x581) & 0x1f) << 11);
2283 t->rxsubchans = (saa_readb(0x529) & 0x08) ?
2284 V4L2_TUNER_SUB_STEREO : V4L2_TUNER_SUB_MONO;
2288 static int radio_s_tuner(struct file *file, void *priv,
2289 struct v4l2_tuner *t)
2291 struct saa7134_fh *fh = file->private_data;
2292 struct saa7134_dev *dev = fh->dev;
2297 saa7134_i2c_call_clients(dev, VIDIOC_S_TUNER, t);
2301 static int radio_enum_input(struct file *file, void *priv,
2302 struct v4l2_input *i)
2307 strcpy(i->name, "Radio");
2308 i->type = V4L2_INPUT_TYPE_TUNER;
2313 static int radio_g_input(struct file *filp, void *priv, unsigned int *i)
2319 static int radio_g_audio(struct file *file, void *priv,
2320 struct v4l2_audio *a)
2322 memset(a, 0, sizeof(*a));
2323 strcpy(a->name, "Radio");
2327 static int radio_s_audio(struct file *file, void *priv,
2328 struct v4l2_audio *a)
2333 static int radio_s_input(struct file *filp, void *priv, unsigned int i)
2338 static int radio_s_std(struct file *file, void *fh, v4l2_std_id *norm)
2343 static int radio_queryctrl(struct file *file, void *priv,
2344 struct v4l2_queryctrl *c)
2346 const struct v4l2_queryctrl *ctrl;
2348 if (c->id < V4L2_CID_BASE ||
2349 c->id >= V4L2_CID_LASTP1)
2351 if (c->id == V4L2_CID_AUDIO_MUTE) {
2352 ctrl = ctrl_by_id(c->id);
2359 static const struct file_operations video_fops =
2361 .owner = THIS_MODULE,
2363 .release = video_release,
2367 .ioctl = video_ioctl2,
2368 .compat_ioctl = v4l_compat_ioctl32,
2369 .llseek = no_llseek,
2372 static const struct v4l2_ioctl_ops video_ioctl_ops = {
2373 .vidioc_querycap = saa7134_querycap,
2374 .vidioc_enum_fmt_vid_cap = saa7134_enum_fmt_vid_cap,
2375 .vidioc_g_fmt_vid_cap = saa7134_g_fmt_vid_cap,
2376 .vidioc_try_fmt_vid_cap = saa7134_try_fmt_vid_cap,
2377 .vidioc_s_fmt_vid_cap = saa7134_s_fmt_vid_cap,
2378 .vidioc_enum_fmt_vid_overlay = saa7134_enum_fmt_vid_overlay,
2379 .vidioc_g_fmt_vid_overlay = saa7134_g_fmt_vid_overlay,
2380 .vidioc_try_fmt_vid_overlay = saa7134_try_fmt_vid_overlay,
2381 .vidioc_s_fmt_vid_overlay = saa7134_s_fmt_vid_overlay,
2382 .vidioc_enum_fmt_vbi_cap = saa7134_enum_fmt_vbi_cap,
2383 .vidioc_g_fmt_vbi_cap = saa7134_try_get_set_fmt_vbi_cap,
2384 .vidioc_try_fmt_vbi_cap = saa7134_try_get_set_fmt_vbi_cap,
2385 .vidioc_s_fmt_vbi_cap = saa7134_try_get_set_fmt_vbi_cap,
2386 .vidioc_g_audio = saa7134_g_audio,
2387 .vidioc_s_audio = saa7134_s_audio,
2388 .vidioc_cropcap = saa7134_cropcap,
2389 .vidioc_reqbufs = saa7134_reqbufs,
2390 .vidioc_querybuf = saa7134_querybuf,
2391 .vidioc_qbuf = saa7134_qbuf,
2392 .vidioc_dqbuf = saa7134_dqbuf,
2393 .vidioc_s_std = saa7134_s_std,
2394 .vidioc_enum_input = saa7134_enum_input,
2395 .vidioc_g_input = saa7134_g_input,
2396 .vidioc_s_input = saa7134_s_input,
2397 .vidioc_queryctrl = saa7134_queryctrl,
2398 .vidioc_g_ctrl = saa7134_g_ctrl,
2399 .vidioc_s_ctrl = saa7134_s_ctrl,
2400 .vidioc_streamon = saa7134_streamon,
2401 .vidioc_streamoff = saa7134_streamoff,
2402 .vidioc_g_tuner = saa7134_g_tuner,
2403 .vidioc_s_tuner = saa7134_s_tuner,
2404 #ifdef CONFIG_VIDEO_V4L1_COMPAT
2405 .vidiocgmbuf = vidiocgmbuf,
2407 .vidioc_g_crop = saa7134_g_crop,
2408 .vidioc_s_crop = saa7134_s_crop,
2409 .vidioc_g_fbuf = saa7134_g_fbuf,
2410 .vidioc_s_fbuf = saa7134_s_fbuf,
2411 .vidioc_overlay = saa7134_overlay,
2412 .vidioc_g_priority = saa7134_g_priority,
2413 .vidioc_s_priority = saa7134_s_priority,
2414 .vidioc_g_parm = saa7134_g_parm,
2415 .vidioc_g_frequency = saa7134_g_frequency,
2416 .vidioc_s_frequency = saa7134_s_frequency,
2417 #ifdef CONFIG_VIDEO_ADV_DEBUG
2418 .vidioc_g_register = vidioc_g_register,
2419 .vidioc_s_register = vidioc_s_register,
2423 static const struct file_operations radio_fops = {
2424 .owner = THIS_MODULE,
2426 .release = video_release,
2427 .ioctl = video_ioctl2,
2428 .compat_ioctl = v4l_compat_ioctl32,
2429 .llseek = no_llseek,
2432 static const struct v4l2_ioctl_ops radio_ioctl_ops = {
2433 .vidioc_querycap = radio_querycap,
2434 .vidioc_g_tuner = radio_g_tuner,
2435 .vidioc_enum_input = radio_enum_input,
2436 .vidioc_g_audio = radio_g_audio,
2437 .vidioc_s_tuner = radio_s_tuner,
2438 .vidioc_s_audio = radio_s_audio,
2439 .vidioc_s_input = radio_s_input,
2440 .vidioc_s_std = radio_s_std,
2441 .vidioc_queryctrl = radio_queryctrl,
2442 .vidioc_g_input = radio_g_input,
2443 .vidioc_g_ctrl = saa7134_g_ctrl,
2444 .vidioc_s_ctrl = saa7134_s_ctrl,
2445 .vidioc_g_frequency = saa7134_g_frequency,
2446 .vidioc_s_frequency = saa7134_s_frequency,
2449 /* ----------------------------------------------------------- */
2450 /* exported stuff */
2452 struct video_device saa7134_video_template = {
2453 .name = "saa7134-video",
2454 .fops = &video_fops,
2455 .ioctl_ops = &video_ioctl_ops,
2457 .tvnorms = SAA7134_NORMS,
2458 .current_norm = V4L2_STD_PAL,
2461 struct video_device saa7134_radio_template = {
2462 .name = "saa7134-radio",
2463 .fops = &radio_fops,
2464 .ioctl_ops = &radio_ioctl_ops,
2468 int saa7134_video_init1(struct saa7134_dev *dev)
2470 /* sanitycheck insmod options */
2471 if (gbuffers < 2 || gbuffers > VIDEO_MAX_FRAME)
2473 if (gbufsize < 0 || gbufsize > gbufsize_max)
2474 gbufsize = gbufsize_max;
2475 gbufsize = (gbufsize + PAGE_SIZE - 1) & PAGE_MASK;
2477 /* put some sensible defaults into the data structures ... */
2478 dev->ctl_bright = ctrl_by_id(V4L2_CID_BRIGHTNESS)->default_value;
2479 dev->ctl_contrast = ctrl_by_id(V4L2_CID_CONTRAST)->default_value;
2480 dev->ctl_hue = ctrl_by_id(V4L2_CID_HUE)->default_value;
2481 dev->ctl_saturation = ctrl_by_id(V4L2_CID_SATURATION)->default_value;
2482 dev->ctl_volume = ctrl_by_id(V4L2_CID_AUDIO_VOLUME)->default_value;
2483 dev->ctl_mute = 1; // ctrl_by_id(V4L2_CID_AUDIO_MUTE)->default_value;
2484 dev->ctl_invert = ctrl_by_id(V4L2_CID_PRIVATE_INVERT)->default_value;
2485 dev->ctl_automute = ctrl_by_id(V4L2_CID_PRIVATE_AUTOMUTE)->default_value;
2487 if (dev->tda9887_conf && dev->ctl_automute)
2488 dev->tda9887_conf |= TDA9887_AUTOMUTE;
2491 INIT_LIST_HEAD(&dev->video_q.queue);
2492 init_timer(&dev->video_q.timeout);
2493 dev->video_q.timeout.function = saa7134_buffer_timeout;
2494 dev->video_q.timeout.data = (unsigned long)(&dev->video_q);
2495 dev->video_q.dev = dev;
2497 if (saa7134_boards[dev->board].video_out)
2498 saa7134_videoport_init(dev);
2503 int saa7134_videoport_init(struct saa7134_dev *dev)
2505 /* enable video output */
2506 int vo = saa7134_boards[dev->board].video_out;
2508 unsigned int vid_port_opts = saa7134_boards[dev->board].vid_port_opts;
2510 /* Configure videoport */
2511 saa_writeb(SAA7134_VIDEO_PORT_CTRL0, video_out[vo][0]);
2512 video_reg = video_out[vo][1];
2513 if (vid_port_opts & SET_T_CODE_POLARITY_NON_INVERTED)
2514 video_reg &= ~VP_T_CODE_P_INVERTED;
2515 saa_writeb(SAA7134_VIDEO_PORT_CTRL1, video_reg);
2516 saa_writeb(SAA7134_VIDEO_PORT_CTRL2, video_out[vo][2]);
2517 saa_writeb(SAA7134_VIDEO_PORT_CTRL4, video_out[vo][4]);
2518 video_reg = video_out[vo][5];
2519 if (vid_port_opts & SET_CLOCK_NOT_DELAYED)
2520 video_reg &= ~VP_CLK_CTRL2_DELAYED;
2521 if (vid_port_opts & SET_CLOCK_INVERTED)
2522 video_reg |= VP_CLK_CTRL1_INVERTED;
2523 saa_writeb(SAA7134_VIDEO_PORT_CTRL5, video_reg);
2524 video_reg = video_out[vo][6];
2525 if (vid_port_opts & SET_VSYNC_OFF) {
2526 video_reg &= ~VP_VS_TYPE_MASK;
2527 video_reg |= VP_VS_TYPE_OFF;
2529 saa_writeb(SAA7134_VIDEO_PORT_CTRL6, video_reg);
2530 saa_writeb(SAA7134_VIDEO_PORT_CTRL7, video_out[vo][7]);
2531 saa_writeb(SAA7134_VIDEO_PORT_CTRL8, video_out[vo][8]);
2533 /* Start videoport */
2534 saa_writeb(SAA7134_VIDEO_PORT_CTRL3, video_out[vo][3]);
2539 int saa7134_video_init2(struct saa7134_dev *dev)
2542 set_tvnorm(dev,&tvnorms[0]);
2544 saa7134_tvaudio_setmute(dev);
2545 saa7134_tvaudio_setvolume(dev,dev->ctl_volume);
2549 void saa7134_irq_video_signalchange(struct saa7134_dev *dev)
2551 static const char *st[] = {
2552 "(no signal)", "NTSC", "PAL", "SECAM" };
2555 st1 = saa_readb(SAA7134_STATUS_VIDEO1);
2556 st2 = saa_readb(SAA7134_STATUS_VIDEO2);
2557 dprintk("DCSDT: pll: %s, sync: %s, norm: %s\n",
2558 (st1 & 0x40) ? "not locked" : "locked",
2559 (st2 & 0x40) ? "no" : "yes",
2561 dev->nosignal = (st1 & 0x40) || (st2 & 0x40) || !(st2 & 0x1);
2563 if (dev->nosignal) {
2564 /* no video signal -> mute audio */
2565 if (dev->ctl_automute)
2567 saa7134_tvaudio_setmute(dev);
2569 /* wake up tvaudio audio carrier scan thread */
2570 saa7134_tvaudio_do_scan(dev);
2573 if ((st2 & 0x80) && !noninterlaced && !dev->nosignal)
2574 saa_clearb(SAA7134_SYNC_CTRL, 0x20);
2576 saa_setb(SAA7134_SYNC_CTRL, 0x20);
2578 if (dev->mops && dev->mops->signal_change)
2579 dev->mops->signal_change(dev);
2583 void saa7134_irq_video_done(struct saa7134_dev *dev, unsigned long status)
2585 enum v4l2_field field;
2587 spin_lock(&dev->slock);
2588 if (dev->video_q.curr) {
2589 dev->video_fieldcount++;
2590 field = dev->video_q.curr->vb.field;
2591 if (V4L2_FIELD_HAS_BOTH(field)) {
2592 /* make sure we have seen both fields */
2593 if ((status & 0x10) == 0x00) {
2594 dev->video_q.curr->top_seen = 1;
2597 if (!dev->video_q.curr->top_seen)
2599 } else if (field == V4L2_FIELD_TOP) {
2600 if ((status & 0x10) != 0x10)
2602 } else if (field == V4L2_FIELD_BOTTOM) {
2603 if ((status & 0x10) != 0x00)
2606 dev->video_q.curr->vb.field_count = dev->video_fieldcount;
2607 saa7134_buffer_finish(dev,&dev->video_q,VIDEOBUF_DONE);
2609 saa7134_buffer_next(dev,&dev->video_q);
2612 spin_unlock(&dev->slock);
2615 /* ----------------------------------------------------------- */