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 /* ------------------------------------------------------------------ */
36 unsigned int video_debug;
37 static unsigned int gbuffers = 8;
38 static unsigned int noninterlaced; /* 0 */
39 static unsigned int gbufsize = 720*576*4;
40 static unsigned int gbufsize_max = 720*576*4;
41 static char secam[] = "--";
42 module_param(video_debug, int, 0644);
43 MODULE_PARM_DESC(video_debug,"enable debug messages [video]");
44 module_param(gbuffers, int, 0444);
45 MODULE_PARM_DESC(gbuffers,"number of capture buffers, range 2-32");
46 module_param(noninterlaced, int, 0644);
47 MODULE_PARM_DESC(noninterlaced,"capture non interlaced video");
48 module_param_string(secam, secam, sizeof(secam), 0644);
49 MODULE_PARM_DESC(secam, "force SECAM variant, either DK,L or Lc");
52 #define dprintk(fmt, arg...) if (video_debug&0x04) \
53 printk(KERN_DEBUG "%s/video: " fmt, dev->name , ## arg)
55 /* ------------------------------------------------------------------ */
56 /* Defines for Video Output Port Register at address 0x191 */
58 /* Bit 0: VIP code T bit polarity */
60 #define VP_T_CODE_P_NON_INVERTED 0x00
61 #define VP_T_CODE_P_INVERTED 0x01
63 /* ------------------------------------------------------------------ */
64 /* Defines for Video Output Port Register at address 0x195 */
66 /* Bit 2: Video output clock delay control */
68 #define VP_CLK_CTRL2_NOT_DELAYED 0x00
69 #define VP_CLK_CTRL2_DELAYED 0x04
71 /* Bit 1: Video output clock invert control */
73 #define VP_CLK_CTRL1_NON_INVERTED 0x00
74 #define VP_CLK_CTRL1_INVERTED 0x02
76 /* ------------------------------------------------------------------ */
77 /* Defines for Video Output Port Register at address 0x196 */
79 /* Bits 2 to 0: VSYNC pin video vertical sync type */
81 #define VP_VS_TYPE_MASK 0x07
83 #define VP_VS_TYPE_OFF 0x00
84 #define VP_VS_TYPE_V123 0x01
85 #define VP_VS_TYPE_V_ITU 0x02
86 #define VP_VS_TYPE_VGATE_L 0x03
87 #define VP_VS_TYPE_RESERVED1 0x04
88 #define VP_VS_TYPE_RESERVED2 0x05
89 #define VP_VS_TYPE_F_ITU 0x06
90 #define VP_VS_TYPE_SC_FID 0x07
92 /* ------------------------------------------------------------------ */
93 /* data structs for video */
95 static int video_out[][9] = {
96 [CCIR656] = { 0x00, 0xb1, 0x00, 0xa1, 0x00, 0x04, 0x06, 0x00, 0x00 },
99 static struct saa7134_format formats[] = {
101 .name = "8 bpp gray",
102 .fourcc = V4L2_PIX_FMT_GREY,
106 .name = "15 bpp RGB, le",
107 .fourcc = V4L2_PIX_FMT_RGB555,
111 .name = "15 bpp RGB, be",
112 .fourcc = V4L2_PIX_FMT_RGB555X,
117 .name = "16 bpp RGB, le",
118 .fourcc = V4L2_PIX_FMT_RGB565,
122 .name = "16 bpp RGB, be",
123 .fourcc = V4L2_PIX_FMT_RGB565X,
128 .name = "24 bpp RGB, le",
129 .fourcc = V4L2_PIX_FMT_BGR24,
133 .name = "24 bpp RGB, be",
134 .fourcc = V4L2_PIX_FMT_RGB24,
139 .name = "32 bpp RGB, le",
140 .fourcc = V4L2_PIX_FMT_BGR32,
144 .name = "32 bpp RGB, be",
145 .fourcc = V4L2_PIX_FMT_RGB32,
151 .name = "4:2:2 packed, YUYV",
152 .fourcc = V4L2_PIX_FMT_YUYV,
158 .name = "4:2:2 packed, UYVY",
159 .fourcc = V4L2_PIX_FMT_UYVY,
164 .name = "4:2:2 planar, Y-Cb-Cr",
165 .fourcc = V4L2_PIX_FMT_YUV422P,
173 .name = "4:2:0 planar, Y-Cb-Cr",
174 .fourcc = V4L2_PIX_FMT_YUV420,
182 .name = "4:2:0 planar, Y-Cb-Cr",
183 .fourcc = V4L2_PIX_FMT_YVU420,
193 #define FORMATS ARRAY_SIZE(formats)
195 #define NORM_625_50 \
198 .video_v_start = 24, \
199 .video_v_stop = 311, \
200 .vbi_v_start_0 = 7, \
201 .vbi_v_stop_0 = 22, \
202 .vbi_v_start_1 = 319, \
205 #define NORM_525_60 \
208 .video_v_start = 23, \
209 .video_v_stop = 262, \
210 .vbi_v_start_0 = 10, \
211 .vbi_v_stop_0 = 21, \
212 .vbi_v_start_1 = 273, \
215 static struct saa7134_tvnorm tvnorms[] = {
217 .name = "PAL", /* autodetect */
221 .sync_control = 0x18,
222 .luma_control = 0x40,
223 .chroma_ctrl1 = 0x81,
225 .chroma_ctrl2 = 0x06,
230 .id = V4L2_STD_PAL_BG,
233 .sync_control = 0x18,
234 .luma_control = 0x40,
235 .chroma_ctrl1 = 0x81,
237 .chroma_ctrl2 = 0x06,
242 .id = V4L2_STD_PAL_I,
245 .sync_control = 0x18,
246 .luma_control = 0x40,
247 .chroma_ctrl1 = 0x81,
249 .chroma_ctrl2 = 0x06,
254 .id = V4L2_STD_PAL_DK,
257 .sync_control = 0x18,
258 .luma_control = 0x40,
259 .chroma_ctrl1 = 0x81,
261 .chroma_ctrl2 = 0x06,
269 .sync_control = 0x59,
270 .luma_control = 0x40,
271 .chroma_ctrl1 = 0x89,
273 .chroma_ctrl2 = 0x0e,
278 .id = V4L2_STD_SECAM,
281 .sync_control = 0x18,
282 .luma_control = 0x1b,
283 .chroma_ctrl1 = 0xd1,
285 .chroma_ctrl2 = 0x00,
290 .id = V4L2_STD_SECAM_DK,
293 .sync_control = 0x18,
294 .luma_control = 0x1b,
295 .chroma_ctrl1 = 0xd1,
297 .chroma_ctrl2 = 0x00,
302 .id = V4L2_STD_SECAM_L,
305 .sync_control = 0x18,
306 .luma_control = 0x1b,
307 .chroma_ctrl1 = 0xd1,
309 .chroma_ctrl2 = 0x00,
314 .id = V4L2_STD_SECAM_LC,
317 .sync_control = 0x18,
318 .luma_control = 0x1b,
319 .chroma_ctrl1 = 0xd1,
321 .chroma_ctrl2 = 0x00,
326 .id = V4L2_STD_PAL_M,
329 .sync_control = 0x59,
330 .luma_control = 0x40,
331 .chroma_ctrl1 = 0xb9,
333 .chroma_ctrl2 = 0x0e,
338 .id = V4L2_STD_PAL_Nc,
341 .sync_control = 0x18,
342 .luma_control = 0x40,
343 .chroma_ctrl1 = 0xa1,
345 .chroma_ctrl2 = 0x06,
350 .id = V4L2_STD_PAL_60,
358 .vbi_v_start_1 = 273,
361 .sync_control = 0x18,
362 .luma_control = 0x40,
363 .chroma_ctrl1 = 0x81,
365 .chroma_ctrl2 = 0x06,
369 #define TVNORMS ARRAY_SIZE(tvnorms)
371 #define V4L2_CID_PRIVATE_INVERT (V4L2_CID_PRIVATE_BASE + 0)
372 #define V4L2_CID_PRIVATE_Y_ODD (V4L2_CID_PRIVATE_BASE + 1)
373 #define V4L2_CID_PRIVATE_Y_EVEN (V4L2_CID_PRIVATE_BASE + 2)
374 #define V4L2_CID_PRIVATE_AUTOMUTE (V4L2_CID_PRIVATE_BASE + 3)
375 #define V4L2_CID_PRIVATE_LASTP1 (V4L2_CID_PRIVATE_BASE + 4)
377 static const struct v4l2_queryctrl no_ctrl = {
379 .flags = V4L2_CTRL_FLAG_DISABLED,
381 static const struct v4l2_queryctrl video_ctrls[] = {
384 .id = V4L2_CID_BRIGHTNESS,
385 .name = "Brightness",
389 .default_value = 128,
390 .type = V4L2_CTRL_TYPE_INTEGER,
392 .id = V4L2_CID_CONTRAST,
398 .type = V4L2_CTRL_TYPE_INTEGER,
400 .id = V4L2_CID_SATURATION,
401 .name = "Saturation",
406 .type = V4L2_CTRL_TYPE_INTEGER,
414 .type = V4L2_CTRL_TYPE_INTEGER,
416 .id = V4L2_CID_HFLIP,
420 .type = V4L2_CTRL_TYPE_BOOLEAN,
424 .id = V4L2_CID_AUDIO_MUTE,
428 .type = V4L2_CTRL_TYPE_BOOLEAN,
430 .id = V4L2_CID_AUDIO_VOLUME,
436 .type = V4L2_CTRL_TYPE_INTEGER,
438 /* --- private --- */
440 .id = V4L2_CID_PRIVATE_INVERT,
444 .type = V4L2_CTRL_TYPE_BOOLEAN,
446 .id = V4L2_CID_PRIVATE_Y_ODD,
447 .name = "y offset odd field",
452 .type = V4L2_CTRL_TYPE_INTEGER,
454 .id = V4L2_CID_PRIVATE_Y_EVEN,
455 .name = "y offset even field",
460 .type = V4L2_CTRL_TYPE_INTEGER,
462 .id = V4L2_CID_PRIVATE_AUTOMUTE,
467 .type = V4L2_CTRL_TYPE_BOOLEAN,
470 static const unsigned int CTRLS = ARRAY_SIZE(video_ctrls);
472 static const struct v4l2_queryctrl* ctrl_by_id(unsigned int id)
476 for (i = 0; i < CTRLS; i++)
477 if (video_ctrls[i].id == id)
478 return video_ctrls+i;
482 static struct saa7134_format* format_by_fourcc(unsigned int fourcc)
486 for (i = 0; i < FORMATS; i++)
487 if (formats[i].fourcc == fourcc)
492 /* ----------------------------------------------------------------------- */
493 /* resource management */
495 static int res_get(struct saa7134_dev *dev, struct saa7134_fh *fh, unsigned int bit)
497 if (fh->resources & bit)
498 /* have it already allocated */
502 mutex_lock(&dev->lock);
503 if (dev->resources & bit) {
504 /* no, someone else uses it */
505 mutex_unlock(&dev->lock);
508 /* it's free, grab it */
509 fh->resources |= bit;
510 dev->resources |= bit;
511 dprintk("res: get %d\n",bit);
512 mutex_unlock(&dev->lock);
516 static int res_check(struct saa7134_fh *fh, unsigned int bit)
518 return (fh->resources & bit);
521 static int res_locked(struct saa7134_dev *dev, unsigned int bit)
523 return (dev->resources & bit);
527 void res_free(struct saa7134_dev *dev, struct saa7134_fh *fh, unsigned int bits)
529 BUG_ON((fh->resources & bits) != bits);
531 mutex_lock(&dev->lock);
532 fh->resources &= ~bits;
533 dev->resources &= ~bits;
534 dprintk("res: put %d\n",bits);
535 mutex_unlock(&dev->lock);
538 /* ------------------------------------------------------------------ */
540 static void set_tvnorm(struct saa7134_dev *dev, struct saa7134_tvnorm *norm)
542 dprintk("set tv norm = %s\n",norm->name);
546 dev->crop_bounds.left = norm->h_start;
547 dev->crop_defrect.left = norm->h_start;
548 dev->crop_bounds.width = norm->h_stop - norm->h_start +1;
549 dev->crop_defrect.width = norm->h_stop - norm->h_start +1;
551 dev->crop_bounds.top = (norm->vbi_v_stop_0+1)*2;
552 dev->crop_defrect.top = norm->video_v_start*2;
553 dev->crop_bounds.height = ((norm->id & V4L2_STD_525_60) ? 524 : 624)
554 - dev->crop_bounds.top;
555 dev->crop_defrect.height = (norm->video_v_stop - norm->video_v_start +1)*2;
557 dev->crop_current = dev->crop_defrect;
559 saa7134_set_tvnorm_hw(dev);
562 static void video_mux(struct saa7134_dev *dev, int input)
564 dprintk("video input = %d [%s]\n", input, card_in(dev, input).name);
565 dev->ctl_input = input;
566 set_tvnorm(dev, dev->tvnorm);
567 saa7134_tvaudio_setinput(dev, &card_in(dev, input));
571 static void saa7134_set_decoder(struct saa7134_dev *dev)
573 int luma_control, sync_control, mux;
575 struct saa7134_tvnorm *norm = dev->tvnorm;
576 mux = card_in(dev, dev->ctl_input).vmux;
578 luma_control = norm->luma_control;
579 sync_control = norm->sync_control;
582 luma_control |= 0x80; /* svideo */
583 if (noninterlaced || dev->nosignal)
584 sync_control |= 0x20;
586 /* setup video decoder */
587 saa_writeb(SAA7134_INCR_DELAY, 0x08);
588 saa_writeb(SAA7134_ANALOG_IN_CTRL1, 0xc0 | mux);
589 saa_writeb(SAA7134_ANALOG_IN_CTRL2, 0x00);
591 saa_writeb(SAA7134_ANALOG_IN_CTRL3, 0x90);
592 saa_writeb(SAA7134_ANALOG_IN_CTRL4, 0x90);
593 saa_writeb(SAA7134_HSYNC_START, 0xeb);
594 saa_writeb(SAA7134_HSYNC_STOP, 0xe0);
595 saa_writeb(SAA7134_SOURCE_TIMING1, norm->src_timing);
597 saa_writeb(SAA7134_SYNC_CTRL, sync_control);
598 saa_writeb(SAA7134_LUMA_CTRL, luma_control);
599 saa_writeb(SAA7134_DEC_LUMA_BRIGHT, dev->ctl_bright);
601 saa_writeb(SAA7134_DEC_LUMA_CONTRAST,
602 dev->ctl_invert ? -dev->ctl_contrast : dev->ctl_contrast);
604 saa_writeb(SAA7134_DEC_CHROMA_SATURATION,
605 dev->ctl_invert ? -dev->ctl_saturation : dev->ctl_saturation);
607 saa_writeb(SAA7134_DEC_CHROMA_HUE, dev->ctl_hue);
608 saa_writeb(SAA7134_CHROMA_CTRL1, norm->chroma_ctrl1);
609 saa_writeb(SAA7134_CHROMA_GAIN, norm->chroma_gain);
611 saa_writeb(SAA7134_CHROMA_CTRL2, norm->chroma_ctrl2);
612 saa_writeb(SAA7134_MODE_DELAY_CTRL, 0x00);
614 saa_writeb(SAA7134_ANALOG_ADC, 0x01);
615 saa_writeb(SAA7134_VGATE_START, 0x11);
616 saa_writeb(SAA7134_VGATE_STOP, 0xfe);
617 saa_writeb(SAA7134_MISC_VGATE_MSB, norm->vgate_misc);
618 saa_writeb(SAA7134_RAW_DATA_GAIN, 0x40);
619 saa_writeb(SAA7134_RAW_DATA_OFFSET, 0x80);
622 void saa7134_set_tvnorm_hw(struct saa7134_dev *dev)
624 saa7134_set_decoder(dev);
626 if (card_in(dev, dev->ctl_input).tv)
627 saa_call_all(dev, tuner, s_std, dev->tvnorm->id);
628 /* Set the correct norm for the saa6752hs. This function
629 does nothing if there is no saa6752hs. */
630 saa_call_empress(dev, tuner, 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 saa_call_all(dev, tuner, s_config, &tda9887_cfg);
1273 if (restart_overlay && fh && res_check(fh, RESOURCE_OVERLAY)) {
1274 spin_lock_irqsave(&dev->slock,flags);
1275 stop_preview(dev,fh);
1276 start_preview(dev,fh);
1277 spin_unlock_irqrestore(&dev->slock,flags);
1282 mutex_unlock(&dev->lock);
1285 EXPORT_SYMBOL_GPL(saa7134_s_ctrl_internal);
1287 static int saa7134_s_ctrl(struct file *file, void *f, struct v4l2_control *c)
1289 struct saa7134_fh *fh = f;
1291 return saa7134_s_ctrl_internal(fh->dev, fh, c);
1294 /* ------------------------------------------------------------------ */
1296 static struct videobuf_queue* saa7134_queue(struct saa7134_fh *fh)
1298 struct videobuf_queue* q = NULL;
1301 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1304 case V4L2_BUF_TYPE_VBI_CAPTURE:
1313 static int saa7134_resource(struct saa7134_fh *fh)
1315 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1316 return RESOURCE_VIDEO;
1318 if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)
1319 return RESOURCE_VBI;
1325 static int video_open(struct file *file)
1327 int minor = video_devdata(file)->minor;
1328 struct saa7134_dev *dev;
1329 struct saa7134_fh *fh;
1330 enum v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1333 mutex_lock(&saa7134_devlist_lock);
1334 list_for_each_entry(dev, &saa7134_devlist, devlist) {
1335 if (dev->video_dev && (dev->video_dev->minor == minor))
1337 if (dev->radio_dev && (dev->radio_dev->minor == minor)) {
1341 if (dev->vbi_dev && (dev->vbi_dev->minor == minor)) {
1342 type = V4L2_BUF_TYPE_VBI_CAPTURE;
1346 mutex_unlock(&saa7134_devlist_lock);
1350 mutex_unlock(&saa7134_devlist_lock);
1352 dprintk("open minor=%d radio=%d type=%s\n",minor,radio,
1353 v4l2_type_names[type]);
1355 /* allocate + initialize per filehandle data */
1356 fh = kzalloc(sizeof(*fh),GFP_KERNEL);
1360 file->private_data = fh;
1364 fh->fmt = format_by_fourcc(V4L2_PIX_FMT_BGR24);
1367 v4l2_prio_open(&dev->prio,&fh->prio);
1369 videobuf_queue_sg_init(&fh->cap, &video_qops,
1370 &dev->pci->dev, &dev->slock,
1371 V4L2_BUF_TYPE_VIDEO_CAPTURE,
1372 V4L2_FIELD_INTERLACED,
1373 sizeof(struct saa7134_buf),
1375 videobuf_queue_sg_init(&fh->vbi, &saa7134_vbi_qops,
1376 &dev->pci->dev, &dev->slock,
1377 V4L2_BUF_TYPE_VBI_CAPTURE,
1379 sizeof(struct saa7134_buf),
1381 saa7134_pgtable_alloc(dev->pci,&fh->pt_cap);
1382 saa7134_pgtable_alloc(dev->pci,&fh->pt_vbi);
1385 /* switch to radio mode */
1386 saa7134_tvaudio_setinput(dev,&card(dev).radio);
1387 saa_call_all(dev, tuner, s_radio);
1389 /* switch to video/vbi mode */
1390 video_mux(dev,dev->ctl_input);
1396 video_read(struct file *file, char __user *data, size_t count, loff_t *ppos)
1398 struct saa7134_fh *fh = file->private_data;
1401 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1402 if (res_locked(fh->dev,RESOURCE_VIDEO))
1404 return videobuf_read_one(saa7134_queue(fh),
1406 file->f_flags & O_NONBLOCK);
1407 case V4L2_BUF_TYPE_VBI_CAPTURE:
1408 if (!res_get(fh->dev,fh,RESOURCE_VBI))
1410 return videobuf_read_stream(saa7134_queue(fh),
1411 data, count, ppos, 1,
1412 file->f_flags & O_NONBLOCK);
1421 video_poll(struct file *file, struct poll_table_struct *wait)
1423 struct saa7134_fh *fh = file->private_data;
1424 struct videobuf_buffer *buf = NULL;
1426 if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type)
1427 return videobuf_poll_stream(file, &fh->vbi, wait);
1429 if (res_check(fh,RESOURCE_VIDEO)) {
1430 if (!list_empty(&fh->cap.stream))
1431 buf = list_entry(fh->cap.stream.next, struct videobuf_buffer, stream);
1433 mutex_lock(&fh->cap.vb_lock);
1434 if (UNSET == fh->cap.read_off) {
1435 /* need to capture a new frame */
1436 if (res_locked(fh->dev,RESOURCE_VIDEO))
1438 if (0 != fh->cap.ops->buf_prepare(&fh->cap,fh->cap.read_buf,fh->cap.field))
1440 fh->cap.ops->buf_queue(&fh->cap,fh->cap.read_buf);
1441 fh->cap.read_off = 0;
1443 mutex_unlock(&fh->cap.vb_lock);
1444 buf = fh->cap.read_buf;
1450 poll_wait(file, &buf->done, wait);
1451 if (buf->state == VIDEOBUF_DONE ||
1452 buf->state == VIDEOBUF_ERROR)
1453 return POLLIN|POLLRDNORM;
1457 mutex_unlock(&fh->cap.vb_lock);
1461 static int video_release(struct file *file)
1463 struct saa7134_fh *fh = file->private_data;
1464 struct saa7134_dev *dev = fh->dev;
1465 unsigned long flags;
1467 /* turn off overlay */
1468 if (res_check(fh, RESOURCE_OVERLAY)) {
1469 spin_lock_irqsave(&dev->slock,flags);
1470 stop_preview(dev,fh);
1471 spin_unlock_irqrestore(&dev->slock,flags);
1472 res_free(dev,fh,RESOURCE_OVERLAY);
1475 /* stop video capture */
1476 if (res_check(fh, RESOURCE_VIDEO)) {
1477 videobuf_streamoff(&fh->cap);
1478 res_free(dev,fh,RESOURCE_VIDEO);
1480 if (fh->cap.read_buf) {
1481 buffer_release(&fh->cap,fh->cap.read_buf);
1482 kfree(fh->cap.read_buf);
1485 /* stop vbi capture */
1486 if (res_check(fh, RESOURCE_VBI)) {
1487 videobuf_stop(&fh->vbi);
1488 res_free(dev,fh,RESOURCE_VBI);
1491 /* ts-capture will not work in planar mode, so turn it off Hac: 04.05*/
1492 saa_andorb(SAA7134_OFMT_VIDEO_A, 0x1f, 0);
1493 saa_andorb(SAA7134_OFMT_VIDEO_B, 0x1f, 0);
1494 saa_andorb(SAA7134_OFMT_DATA_A, 0x1f, 0);
1495 saa_andorb(SAA7134_OFMT_DATA_B, 0x1f, 0);
1497 saa_call_all(dev, core, s_standby, 0);
1500 videobuf_mmap_free(&fh->cap);
1501 videobuf_mmap_free(&fh->vbi);
1502 saa7134_pgtable_free(dev->pci,&fh->pt_cap);
1503 saa7134_pgtable_free(dev->pci,&fh->pt_vbi);
1505 v4l2_prio_close(&dev->prio,&fh->prio);
1506 file->private_data = NULL;
1511 static int video_mmap(struct file *file, struct vm_area_struct * vma)
1513 struct saa7134_fh *fh = file->private_data;
1515 return videobuf_mmap_mapper(saa7134_queue(fh), vma);
1518 /* ------------------------------------------------------------------ */
1520 static int saa7134_try_get_set_fmt_vbi_cap(struct file *file, void *priv,
1521 struct v4l2_format *f)
1523 struct saa7134_fh *fh = priv;
1524 struct saa7134_dev *dev = fh->dev;
1525 struct saa7134_tvnorm *norm = dev->tvnorm;
1527 f->fmt.vbi.sampling_rate = 6750000 * 4;
1528 f->fmt.vbi.samples_per_line = 2048 /* VBI_LINE_LENGTH */;
1529 f->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
1530 f->fmt.vbi.offset = 64 * 4;
1531 f->fmt.vbi.start[0] = norm->vbi_v_start_0;
1532 f->fmt.vbi.count[0] = norm->vbi_v_stop_0 - norm->vbi_v_start_0 +1;
1533 f->fmt.vbi.start[1] = norm->vbi_v_start_1;
1534 f->fmt.vbi.count[1] = f->fmt.vbi.count[0];
1535 f->fmt.vbi.flags = 0; /* VBI_UNSYNC VBI_INTERLACED */
1540 static int saa7134_g_fmt_vid_cap(struct file *file, void *priv,
1541 struct v4l2_format *f)
1543 struct saa7134_fh *fh = priv;
1545 f->fmt.pix.width = fh->width;
1546 f->fmt.pix.height = fh->height;
1547 f->fmt.pix.field = fh->cap.field;
1548 f->fmt.pix.pixelformat = fh->fmt->fourcc;
1549 f->fmt.pix.bytesperline =
1550 (f->fmt.pix.width * fh->fmt->depth) >> 3;
1551 f->fmt.pix.sizeimage =
1552 f->fmt.pix.height * f->fmt.pix.bytesperline;
1556 static int saa7134_g_fmt_vid_overlay(struct file *file, void *priv,
1557 struct v4l2_format *f)
1559 struct saa7134_fh *fh = priv;
1561 if (saa7134_no_overlay > 0) {
1562 printk(KERN_ERR "V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
1565 f->fmt.win = fh->win;
1570 static int saa7134_try_fmt_vid_cap(struct file *file, void *priv,
1571 struct v4l2_format *f)
1573 struct saa7134_fh *fh = priv;
1574 struct saa7134_dev *dev = fh->dev;
1575 struct saa7134_format *fmt;
1576 enum v4l2_field field;
1577 unsigned int maxw, maxh;
1579 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1583 field = f->fmt.pix.field;
1584 maxw = min(dev->crop_current.width*4, dev->crop_bounds.width);
1585 maxh = min(dev->crop_current.height*4, dev->crop_bounds.height);
1587 if (V4L2_FIELD_ANY == field) {
1588 field = (f->fmt.pix.height > maxh/2)
1589 ? V4L2_FIELD_INTERLACED
1590 : V4L2_FIELD_BOTTOM;
1593 case V4L2_FIELD_TOP:
1594 case V4L2_FIELD_BOTTOM:
1597 case V4L2_FIELD_INTERLACED:
1603 f->fmt.pix.field = field;
1604 if (f->fmt.pix.width < 48)
1605 f->fmt.pix.width = 48;
1606 if (f->fmt.pix.height < 32)
1607 f->fmt.pix.height = 32;
1608 if (f->fmt.pix.width > maxw)
1609 f->fmt.pix.width = maxw;
1610 if (f->fmt.pix.height > maxh)
1611 f->fmt.pix.height = maxh;
1612 f->fmt.pix.width &= ~0x03;
1613 f->fmt.pix.bytesperline =
1614 (f->fmt.pix.width * fmt->depth) >> 3;
1615 f->fmt.pix.sizeimage =
1616 f->fmt.pix.height * f->fmt.pix.bytesperline;
1621 static int saa7134_try_fmt_vid_overlay(struct file *file, void *priv,
1622 struct v4l2_format *f)
1624 struct saa7134_fh *fh = priv;
1625 struct saa7134_dev *dev = fh->dev;
1627 if (saa7134_no_overlay > 0) {
1628 printk(KERN_ERR "V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
1632 return verify_preview(dev, &f->fmt.win);
1635 static int saa7134_s_fmt_vid_cap(struct file *file, void *priv,
1636 struct v4l2_format *f)
1638 struct saa7134_fh *fh = priv;
1641 err = saa7134_try_fmt_vid_cap(file, priv, f);
1645 fh->fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1646 fh->width = f->fmt.pix.width;
1647 fh->height = f->fmt.pix.height;
1648 fh->cap.field = f->fmt.pix.field;
1652 static int saa7134_s_fmt_vid_overlay(struct file *file, void *priv,
1653 struct v4l2_format *f)
1655 struct saa7134_fh *fh = priv;
1656 struct saa7134_dev *dev = fh->dev;
1658 unsigned long flags;
1660 if (saa7134_no_overlay > 0) {
1661 printk(KERN_ERR "V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
1664 err = verify_preview(dev, &f->fmt.win);
1668 mutex_lock(&dev->lock);
1670 fh->win = f->fmt.win;
1671 fh->nclips = f->fmt.win.clipcount;
1676 if (copy_from_user(fh->clips, f->fmt.win.clips,
1677 sizeof(struct v4l2_clip)*fh->nclips)) {
1678 mutex_unlock(&dev->lock);
1682 if (res_check(fh, RESOURCE_OVERLAY)) {
1683 spin_lock_irqsave(&dev->slock, flags);
1684 stop_preview(dev, fh);
1685 start_preview(dev, fh);
1686 spin_unlock_irqrestore(&dev->slock, flags);
1689 mutex_unlock(&dev->lock);
1693 int saa7134_queryctrl(struct file *file, void *priv, struct v4l2_queryctrl *c)
1695 const struct v4l2_queryctrl *ctrl;
1697 if ((c->id < V4L2_CID_BASE ||
1698 c->id >= V4L2_CID_LASTP1) &&
1699 (c->id < V4L2_CID_PRIVATE_BASE ||
1700 c->id >= V4L2_CID_PRIVATE_LASTP1))
1702 ctrl = ctrl_by_id(c->id);
1703 *c = (NULL != ctrl) ? *ctrl : no_ctrl;
1706 EXPORT_SYMBOL_GPL(saa7134_queryctrl);
1708 static int saa7134_enum_input(struct file *file, void *priv,
1709 struct v4l2_input *i)
1711 struct saa7134_fh *fh = priv;
1712 struct saa7134_dev *dev = fh->dev;
1716 if (n >= SAA7134_INPUT_MAX)
1718 if (NULL == card_in(dev, i->index).name)
1720 memset(i, 0, sizeof(*i));
1722 i->type = V4L2_INPUT_TYPE_CAMERA;
1723 strcpy(i->name, card_in(dev, n).name);
1724 if (card_in(dev, n).tv)
1725 i->type = V4L2_INPUT_TYPE_TUNER;
1727 if (n == dev->ctl_input) {
1728 int v1 = saa_readb(SAA7134_STATUS_VIDEO1);
1729 int v2 = saa_readb(SAA7134_STATUS_VIDEO2);
1731 if (0 != (v1 & 0x40))
1732 i->status |= V4L2_IN_ST_NO_H_LOCK;
1733 if (0 != (v2 & 0x40))
1734 i->status |= V4L2_IN_ST_NO_SYNC;
1735 if (0 != (v2 & 0x0e))
1736 i->status |= V4L2_IN_ST_MACROVISION;
1738 i->std = SAA7134_NORMS;
1742 static int saa7134_g_input(struct file *file, void *priv, unsigned int *i)
1744 struct saa7134_fh *fh = priv;
1745 struct saa7134_dev *dev = fh->dev;
1747 *i = dev->ctl_input;
1751 static int saa7134_s_input(struct file *file, void *priv, unsigned int i)
1753 struct saa7134_fh *fh = priv;
1754 struct saa7134_dev *dev = fh->dev;
1757 err = v4l2_prio_check(&dev->prio, &fh->prio);
1761 if (i < 0 || i >= SAA7134_INPUT_MAX)
1763 if (NULL == card_in(dev, i).name)
1765 mutex_lock(&dev->lock);
1767 mutex_unlock(&dev->lock);
1771 static int saa7134_querycap(struct file *file, void *priv,
1772 struct v4l2_capability *cap)
1774 struct saa7134_fh *fh = priv;
1775 struct saa7134_dev *dev = fh->dev;
1777 unsigned int tuner_type = dev->tuner_type;
1779 strcpy(cap->driver, "saa7134");
1780 strlcpy(cap->card, saa7134_boards[dev->board].name,
1782 sprintf(cap->bus_info, "PCI:%s", pci_name(dev->pci));
1783 cap->version = SAA7134_VERSION_CODE;
1785 V4L2_CAP_VIDEO_CAPTURE |
1786 V4L2_CAP_VBI_CAPTURE |
1787 V4L2_CAP_READWRITE |
1788 V4L2_CAP_STREAMING |
1790 if (saa7134_no_overlay <= 0)
1791 cap->capabilities |= V4L2_CAP_VIDEO_OVERLAY;
1793 if ((tuner_type == TUNER_ABSENT) || (tuner_type == UNSET))
1794 cap->capabilities &= ~V4L2_CAP_TUNER;
1798 int saa7134_s_std_internal(struct saa7134_dev *dev, struct saa7134_fh *fh, v4l2_std_id *id)
1800 unsigned long flags;
1805 /* When called from the empress code fh == NULL.
1806 That needs to be fixed somehow, but for now this is
1809 err = v4l2_prio_check(&dev->prio, &fh->prio);
1812 } else if (res_locked(dev, RESOURCE_OVERLAY)) {
1813 /* Don't change the std from the mpeg device
1814 if overlay is active. */
1818 for (i = 0; i < TVNORMS; i++)
1819 if (*id == tvnorms[i].id)
1823 for (i = 0; i < TVNORMS; i++)
1824 if (*id & tvnorms[i].id)
1829 if ((*id & V4L2_STD_SECAM) && (secam[0] != '-')) {
1830 if (secam[0] == 'L' || secam[0] == 'l') {
1831 if (secam[1] == 'C' || secam[1] == 'c')
1832 fixup = V4L2_STD_SECAM_LC;
1834 fixup = V4L2_STD_SECAM_L;
1836 if (secam[0] == 'D' || secam[0] == 'd')
1837 fixup = V4L2_STD_SECAM_DK;
1839 fixup = V4L2_STD_SECAM;
1841 for (i = 0; i < TVNORMS; i++)
1842 if (fixup == tvnorms[i].id)
1846 *id = tvnorms[i].id;
1848 mutex_lock(&dev->lock);
1849 if (fh && res_check(fh, RESOURCE_OVERLAY)) {
1850 spin_lock_irqsave(&dev->slock, flags);
1851 stop_preview(dev, fh);
1852 spin_unlock_irqrestore(&dev->slock, flags);
1854 set_tvnorm(dev, &tvnorms[i]);
1856 spin_lock_irqsave(&dev->slock, flags);
1857 start_preview(dev, fh);
1858 spin_unlock_irqrestore(&dev->slock, flags);
1860 set_tvnorm(dev, &tvnorms[i]);
1862 saa7134_tvaudio_do_scan(dev);
1863 mutex_unlock(&dev->lock);
1866 EXPORT_SYMBOL_GPL(saa7134_s_std_internal);
1868 static int saa7134_s_std(struct file *file, void *priv, v4l2_std_id *id)
1870 struct saa7134_fh *fh = priv;
1872 return saa7134_s_std_internal(fh->dev, fh, id);
1875 static int saa7134_g_std(struct file *file, void *priv, v4l2_std_id *id)
1877 struct saa7134_fh *fh = priv;
1878 struct saa7134_dev *dev = fh->dev;
1880 *id = dev->tvnorm->id;
1884 static int saa7134_cropcap(struct file *file, void *priv,
1885 struct v4l2_cropcap *cap)
1887 struct saa7134_fh *fh = priv;
1888 struct saa7134_dev *dev = fh->dev;
1890 if (cap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1891 cap->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
1893 cap->bounds = dev->crop_bounds;
1894 cap->defrect = dev->crop_defrect;
1895 cap->pixelaspect.numerator = 1;
1896 cap->pixelaspect.denominator = 1;
1897 if (dev->tvnorm->id & V4L2_STD_525_60) {
1898 cap->pixelaspect.numerator = 11;
1899 cap->pixelaspect.denominator = 10;
1901 if (dev->tvnorm->id & V4L2_STD_625_50) {
1902 cap->pixelaspect.numerator = 54;
1903 cap->pixelaspect.denominator = 59;
1908 static int saa7134_g_crop(struct file *file, void *f, struct v4l2_crop *crop)
1910 struct saa7134_fh *fh = f;
1911 struct saa7134_dev *dev = fh->dev;
1913 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1914 crop->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
1916 crop->c = dev->crop_current;
1920 static int saa7134_s_crop(struct file *file, void *f, struct v4l2_crop *crop)
1922 struct saa7134_fh *fh = f;
1923 struct saa7134_dev *dev = fh->dev;
1924 struct v4l2_rect *b = &dev->crop_bounds;
1926 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1927 crop->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
1929 if (crop->c.height < 0)
1931 if (crop->c.width < 0)
1934 if (res_locked(fh->dev, RESOURCE_OVERLAY))
1936 if (res_locked(fh->dev, RESOURCE_VIDEO))
1939 if (crop->c.top < b->top)
1940 crop->c.top = b->top;
1941 if (crop->c.top > b->top + b->height)
1942 crop->c.top = b->top + b->height;
1943 if (crop->c.height > b->top - crop->c.top + b->height)
1944 crop->c.height = b->top - crop->c.top + b->height;
1946 if (crop->c.left < b->left)
1947 crop->c.left = b->left;
1948 if (crop->c.left > b->left + b->width)
1949 crop->c.left = b->left + b->width;
1950 if (crop->c.width > b->left - crop->c.left + b->width)
1951 crop->c.width = b->left - crop->c.left + b->width;
1953 dev->crop_current = crop->c;
1957 static int saa7134_g_tuner(struct file *file, void *priv,
1958 struct v4l2_tuner *t)
1960 struct saa7134_fh *fh = priv;
1961 struct saa7134_dev *dev = fh->dev;
1966 memset(t, 0, sizeof(*t));
1967 for (n = 0; n < SAA7134_INPUT_MAX; n++)
1968 if (card_in(dev, n).tv)
1970 if (NULL != card_in(dev, n).name) {
1971 strcpy(t->name, "Television");
1972 t->type = V4L2_TUNER_ANALOG_TV;
1973 t->capability = V4L2_TUNER_CAP_NORM |
1974 V4L2_TUNER_CAP_STEREO |
1975 V4L2_TUNER_CAP_LANG1 |
1976 V4L2_TUNER_CAP_LANG2;
1977 t->rangehigh = 0xffffffffUL;
1978 t->rxsubchans = saa7134_tvaudio_getstereo(dev);
1979 t->audmode = saa7134_tvaudio_rx2mode(t->rxsubchans);
1981 if (0 != (saa_readb(SAA7134_STATUS_VIDEO1) & 0x03))
1986 static int saa7134_s_tuner(struct file *file, void *priv,
1987 struct v4l2_tuner *t)
1989 struct saa7134_fh *fh = priv;
1990 struct saa7134_dev *dev = fh->dev;
1993 err = v4l2_prio_check(&dev->prio, &fh->prio);
1997 mode = dev->thread.mode;
1998 if (UNSET == mode) {
1999 rx = saa7134_tvaudio_getstereo(dev);
2000 mode = saa7134_tvaudio_rx2mode(t->rxsubchans);
2002 if (mode != t->audmode)
2003 dev->thread.mode = t->audmode;
2008 static int saa7134_g_frequency(struct file *file, void *priv,
2009 struct v4l2_frequency *f)
2011 struct saa7134_fh *fh = priv;
2012 struct saa7134_dev *dev = fh->dev;
2014 f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
2015 f->frequency = dev->ctl_freq;
2020 static int saa7134_s_frequency(struct file *file, void *priv,
2021 struct v4l2_frequency *f)
2023 struct saa7134_fh *fh = priv;
2024 struct saa7134_dev *dev = fh->dev;
2027 err = v4l2_prio_check(&dev->prio, &fh->prio);
2033 if (0 == fh->radio && V4L2_TUNER_ANALOG_TV != f->type)
2035 if (1 == fh->radio && V4L2_TUNER_RADIO != f->type)
2037 mutex_lock(&dev->lock);
2038 dev->ctl_freq = f->frequency;
2040 saa_call_all(dev, tuner, s_frequency, f);
2042 saa7134_tvaudio_do_scan(dev);
2043 mutex_unlock(&dev->lock);
2047 static int saa7134_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
2049 strcpy(a->name, "audio");
2053 static int saa7134_s_audio(struct file *file, void *priv, struct v4l2_audio *a)
2058 static int saa7134_g_priority(struct file *file, void *f, enum v4l2_priority *p)
2060 struct saa7134_fh *fh = f;
2061 struct saa7134_dev *dev = fh->dev;
2063 *p = v4l2_prio_max(&dev->prio);
2067 static int saa7134_s_priority(struct file *file, void *f,
2068 enum v4l2_priority prio)
2070 struct saa7134_fh *fh = f;
2071 struct saa7134_dev *dev = fh->dev;
2073 return v4l2_prio_change(&dev->prio, &fh->prio, prio);
2076 static int saa7134_enum_fmt_vid_cap(struct file *file, void *priv,
2077 struct v4l2_fmtdesc *f)
2079 if (f->index >= FORMATS)
2082 strlcpy(f->description, formats[f->index].name,
2083 sizeof(f->description));
2085 f->pixelformat = formats[f->index].fourcc;
2090 static int saa7134_enum_fmt_vid_overlay(struct file *file, void *priv,
2091 struct v4l2_fmtdesc *f)
2093 if (saa7134_no_overlay > 0) {
2094 printk(KERN_ERR "V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
2098 if ((f->index >= FORMATS) || formats[f->index].planar)
2101 strlcpy(f->description, formats[f->index].name,
2102 sizeof(f->description));
2104 f->pixelformat = formats[f->index].fourcc;
2109 static int saa7134_g_fbuf(struct file *file, void *f,
2110 struct v4l2_framebuffer *fb)
2112 struct saa7134_fh *fh = f;
2113 struct saa7134_dev *dev = fh->dev;
2116 fb->capability = V4L2_FBUF_CAP_LIST_CLIPPING;
2121 static int saa7134_s_fbuf(struct file *file, void *f,
2122 struct v4l2_framebuffer *fb)
2124 struct saa7134_fh *fh = f;
2125 struct saa7134_dev *dev = fh->dev;
2126 struct saa7134_format *fmt;
2128 if (!capable(CAP_SYS_ADMIN) &&
2129 !capable(CAP_SYS_RAWIO))
2133 fmt = format_by_fourcc(fb->fmt.pixelformat);
2140 if (0 == dev->ovbuf.fmt.bytesperline)
2141 dev->ovbuf.fmt.bytesperline =
2142 dev->ovbuf.fmt.width*fmt->depth/8;
2146 static int saa7134_overlay(struct file *file, void *f, unsigned int on)
2148 struct saa7134_fh *fh = f;
2149 struct saa7134_dev *dev = fh->dev;
2150 unsigned long flags;
2153 if (saa7134_no_overlay > 0) {
2154 dprintk("no_overlay\n");
2158 if (!res_get(dev, fh, RESOURCE_OVERLAY))
2160 spin_lock_irqsave(&dev->slock, flags);
2161 start_preview(dev, fh);
2162 spin_unlock_irqrestore(&dev->slock, flags);
2165 if (!res_check(fh, RESOURCE_OVERLAY))
2167 spin_lock_irqsave(&dev->slock, flags);
2168 stop_preview(dev, fh);
2169 spin_unlock_irqrestore(&dev->slock, flags);
2170 res_free(dev, fh, RESOURCE_OVERLAY);
2175 #ifdef CONFIG_VIDEO_V4L1_COMPAT
2176 static int vidiocgmbuf(struct file *file, void *priv, struct video_mbuf *mbuf)
2178 struct saa7134_fh *fh = file->private_data;
2179 return videobuf_cgmbuf(saa7134_queue(fh), mbuf, 8);
2183 static int saa7134_reqbufs(struct file *file, void *priv,
2184 struct v4l2_requestbuffers *p)
2186 struct saa7134_fh *fh = priv;
2187 return videobuf_reqbufs(saa7134_queue(fh), p);
2190 static int saa7134_querybuf(struct file *file, void *priv,
2191 struct v4l2_buffer *b)
2193 struct saa7134_fh *fh = priv;
2194 return videobuf_querybuf(saa7134_queue(fh), b);
2197 static int saa7134_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
2199 struct saa7134_fh *fh = priv;
2200 return videobuf_qbuf(saa7134_queue(fh), b);
2203 static int saa7134_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
2205 struct saa7134_fh *fh = priv;
2206 return videobuf_dqbuf(saa7134_queue(fh), b,
2207 file->f_flags & O_NONBLOCK);
2210 static int saa7134_streamon(struct file *file, void *priv,
2211 enum v4l2_buf_type type)
2213 struct saa7134_fh *fh = priv;
2214 struct saa7134_dev *dev = fh->dev;
2215 int res = saa7134_resource(fh);
2217 if (!res_get(dev, fh, res))
2220 return videobuf_streamon(saa7134_queue(fh));
2223 static int saa7134_streamoff(struct file *file, void *priv,
2224 enum v4l2_buf_type type)
2227 struct saa7134_fh *fh = priv;
2228 struct saa7134_dev *dev = fh->dev;
2229 int res = saa7134_resource(fh);
2231 err = videobuf_streamoff(saa7134_queue(fh));
2234 res_free(dev, fh, res);
2238 static int saa7134_g_parm(struct file *file, void *fh,
2239 struct v4l2_streamparm *parm)
2244 #ifdef CONFIG_VIDEO_ADV_DEBUG
2245 static int vidioc_g_register (struct file *file, void *priv,
2246 struct v4l2_dbg_register *reg)
2248 struct saa7134_fh *fh = priv;
2249 struct saa7134_dev *dev = fh->dev;
2251 if (!v4l2_chip_match_host(®->match))
2253 reg->val = saa_readb(reg->reg);
2258 static int vidioc_s_register (struct file *file, void *priv,
2259 struct v4l2_dbg_register *reg)
2261 struct saa7134_fh *fh = priv;
2262 struct saa7134_dev *dev = fh->dev;
2264 if (!v4l2_chip_match_host(®->match))
2266 saa_writeb(reg->reg&0xffffff, reg->val);
2271 static int radio_querycap(struct file *file, void *priv,
2272 struct v4l2_capability *cap)
2274 struct saa7134_fh *fh = file->private_data;
2275 struct saa7134_dev *dev = fh->dev;
2277 strcpy(cap->driver, "saa7134");
2278 strlcpy(cap->card, saa7134_boards[dev->board].name, sizeof(cap->card));
2279 sprintf(cap->bus_info, "PCI:%s", pci_name(dev->pci));
2280 cap->version = SAA7134_VERSION_CODE;
2281 cap->capabilities = V4L2_CAP_TUNER;
2285 static int radio_g_tuner(struct file *file, void *priv,
2286 struct v4l2_tuner *t)
2288 struct saa7134_fh *fh = file->private_data;
2289 struct saa7134_dev *dev = fh->dev;
2294 memset(t, 0, sizeof(*t));
2295 strcpy(t->name, "Radio");
2296 t->type = V4L2_TUNER_RADIO;
2298 saa_call_all(dev, tuner, g_tuner, t);
2299 if (dev->input->amux == TV) {
2300 t->signal = 0xf800 - ((saa_readb(0x581) & 0x1f) << 11);
2301 t->rxsubchans = (saa_readb(0x529) & 0x08) ?
2302 V4L2_TUNER_SUB_STEREO : V4L2_TUNER_SUB_MONO;
2306 static int radio_s_tuner(struct file *file, void *priv,
2307 struct v4l2_tuner *t)
2309 struct saa7134_fh *fh = file->private_data;
2310 struct saa7134_dev *dev = fh->dev;
2315 saa_call_all(dev, tuner, s_tuner, t);
2319 static int radio_enum_input(struct file *file, void *priv,
2320 struct v4l2_input *i)
2325 strcpy(i->name, "Radio");
2326 i->type = V4L2_INPUT_TYPE_TUNER;
2331 static int radio_g_input(struct file *filp, void *priv, unsigned int *i)
2337 static int radio_g_audio(struct file *file, void *priv,
2338 struct v4l2_audio *a)
2340 memset(a, 0, sizeof(*a));
2341 strcpy(a->name, "Radio");
2345 static int radio_s_audio(struct file *file, void *priv,
2346 struct v4l2_audio *a)
2351 static int radio_s_input(struct file *filp, void *priv, unsigned int i)
2356 static int radio_s_std(struct file *file, void *fh, v4l2_std_id *norm)
2361 static int radio_queryctrl(struct file *file, void *priv,
2362 struct v4l2_queryctrl *c)
2364 const struct v4l2_queryctrl *ctrl;
2366 if (c->id < V4L2_CID_BASE ||
2367 c->id >= V4L2_CID_LASTP1)
2369 if (c->id == V4L2_CID_AUDIO_MUTE) {
2370 ctrl = ctrl_by_id(c->id);
2377 static const struct v4l2_file_operations video_fops =
2379 .owner = THIS_MODULE,
2381 .release = video_release,
2385 .ioctl = video_ioctl2,
2388 static const struct v4l2_ioctl_ops video_ioctl_ops = {
2389 .vidioc_querycap = saa7134_querycap,
2390 .vidioc_enum_fmt_vid_cap = saa7134_enum_fmt_vid_cap,
2391 .vidioc_g_fmt_vid_cap = saa7134_g_fmt_vid_cap,
2392 .vidioc_try_fmt_vid_cap = saa7134_try_fmt_vid_cap,
2393 .vidioc_s_fmt_vid_cap = saa7134_s_fmt_vid_cap,
2394 .vidioc_enum_fmt_vid_overlay = saa7134_enum_fmt_vid_overlay,
2395 .vidioc_g_fmt_vid_overlay = saa7134_g_fmt_vid_overlay,
2396 .vidioc_try_fmt_vid_overlay = saa7134_try_fmt_vid_overlay,
2397 .vidioc_s_fmt_vid_overlay = saa7134_s_fmt_vid_overlay,
2398 .vidioc_g_fmt_vbi_cap = saa7134_try_get_set_fmt_vbi_cap,
2399 .vidioc_try_fmt_vbi_cap = saa7134_try_get_set_fmt_vbi_cap,
2400 .vidioc_s_fmt_vbi_cap = saa7134_try_get_set_fmt_vbi_cap,
2401 .vidioc_g_audio = saa7134_g_audio,
2402 .vidioc_s_audio = saa7134_s_audio,
2403 .vidioc_cropcap = saa7134_cropcap,
2404 .vidioc_reqbufs = saa7134_reqbufs,
2405 .vidioc_querybuf = saa7134_querybuf,
2406 .vidioc_qbuf = saa7134_qbuf,
2407 .vidioc_dqbuf = saa7134_dqbuf,
2408 .vidioc_s_std = saa7134_s_std,
2409 .vidioc_g_std = saa7134_g_std,
2410 .vidioc_enum_input = saa7134_enum_input,
2411 .vidioc_g_input = saa7134_g_input,
2412 .vidioc_s_input = saa7134_s_input,
2413 .vidioc_queryctrl = saa7134_queryctrl,
2414 .vidioc_g_ctrl = saa7134_g_ctrl,
2415 .vidioc_s_ctrl = saa7134_s_ctrl,
2416 .vidioc_streamon = saa7134_streamon,
2417 .vidioc_streamoff = saa7134_streamoff,
2418 .vidioc_g_tuner = saa7134_g_tuner,
2419 .vidioc_s_tuner = saa7134_s_tuner,
2420 #ifdef CONFIG_VIDEO_V4L1_COMPAT
2421 .vidiocgmbuf = vidiocgmbuf,
2423 .vidioc_g_crop = saa7134_g_crop,
2424 .vidioc_s_crop = saa7134_s_crop,
2425 .vidioc_g_fbuf = saa7134_g_fbuf,
2426 .vidioc_s_fbuf = saa7134_s_fbuf,
2427 .vidioc_overlay = saa7134_overlay,
2428 .vidioc_g_priority = saa7134_g_priority,
2429 .vidioc_s_priority = saa7134_s_priority,
2430 .vidioc_g_parm = saa7134_g_parm,
2431 .vidioc_g_frequency = saa7134_g_frequency,
2432 .vidioc_s_frequency = saa7134_s_frequency,
2433 #ifdef CONFIG_VIDEO_ADV_DEBUG
2434 .vidioc_g_register = vidioc_g_register,
2435 .vidioc_s_register = vidioc_s_register,
2439 static const struct v4l2_file_operations radio_fops = {
2440 .owner = THIS_MODULE,
2442 .release = video_release,
2443 .ioctl = video_ioctl2,
2446 static const struct v4l2_ioctl_ops radio_ioctl_ops = {
2447 .vidioc_querycap = radio_querycap,
2448 .vidioc_g_tuner = radio_g_tuner,
2449 .vidioc_enum_input = radio_enum_input,
2450 .vidioc_g_audio = radio_g_audio,
2451 .vidioc_s_tuner = radio_s_tuner,
2452 .vidioc_s_audio = radio_s_audio,
2453 .vidioc_s_input = radio_s_input,
2454 .vidioc_s_std = radio_s_std,
2455 .vidioc_queryctrl = radio_queryctrl,
2456 .vidioc_g_input = radio_g_input,
2457 .vidioc_g_ctrl = saa7134_g_ctrl,
2458 .vidioc_s_ctrl = saa7134_s_ctrl,
2459 .vidioc_g_frequency = saa7134_g_frequency,
2460 .vidioc_s_frequency = saa7134_s_frequency,
2463 /* ----------------------------------------------------------- */
2464 /* exported stuff */
2466 struct video_device saa7134_video_template = {
2467 .name = "saa7134-video",
2468 .fops = &video_fops,
2469 .ioctl_ops = &video_ioctl_ops,
2471 .tvnorms = SAA7134_NORMS,
2472 .current_norm = V4L2_STD_PAL,
2475 struct video_device saa7134_radio_template = {
2476 .name = "saa7134-radio",
2477 .fops = &radio_fops,
2478 .ioctl_ops = &radio_ioctl_ops,
2482 int saa7134_video_init1(struct saa7134_dev *dev)
2484 /* sanitycheck insmod options */
2485 if (gbuffers < 2 || gbuffers > VIDEO_MAX_FRAME)
2487 if (gbufsize < 0 || gbufsize > gbufsize_max)
2488 gbufsize = gbufsize_max;
2489 gbufsize = (gbufsize + PAGE_SIZE - 1) & PAGE_MASK;
2491 /* put some sensible defaults into the data structures ... */
2492 dev->ctl_bright = ctrl_by_id(V4L2_CID_BRIGHTNESS)->default_value;
2493 dev->ctl_contrast = ctrl_by_id(V4L2_CID_CONTRAST)->default_value;
2494 dev->ctl_hue = ctrl_by_id(V4L2_CID_HUE)->default_value;
2495 dev->ctl_saturation = ctrl_by_id(V4L2_CID_SATURATION)->default_value;
2496 dev->ctl_volume = ctrl_by_id(V4L2_CID_AUDIO_VOLUME)->default_value;
2497 dev->ctl_mute = 1; // ctrl_by_id(V4L2_CID_AUDIO_MUTE)->default_value;
2498 dev->ctl_invert = ctrl_by_id(V4L2_CID_PRIVATE_INVERT)->default_value;
2499 dev->ctl_automute = ctrl_by_id(V4L2_CID_PRIVATE_AUTOMUTE)->default_value;
2501 if (dev->tda9887_conf && dev->ctl_automute)
2502 dev->tda9887_conf |= TDA9887_AUTOMUTE;
2505 INIT_LIST_HEAD(&dev->video_q.queue);
2506 init_timer(&dev->video_q.timeout);
2507 dev->video_q.timeout.function = saa7134_buffer_timeout;
2508 dev->video_q.timeout.data = (unsigned long)(&dev->video_q);
2509 dev->video_q.dev = dev;
2511 if (saa7134_boards[dev->board].video_out)
2512 saa7134_videoport_init(dev);
2517 int saa7134_videoport_init(struct saa7134_dev *dev)
2519 /* enable video output */
2520 int vo = saa7134_boards[dev->board].video_out;
2522 unsigned int vid_port_opts = saa7134_boards[dev->board].vid_port_opts;
2524 /* Configure videoport */
2525 saa_writeb(SAA7134_VIDEO_PORT_CTRL0, video_out[vo][0]);
2526 video_reg = video_out[vo][1];
2527 if (vid_port_opts & SET_T_CODE_POLARITY_NON_INVERTED)
2528 video_reg &= ~VP_T_CODE_P_INVERTED;
2529 saa_writeb(SAA7134_VIDEO_PORT_CTRL1, video_reg);
2530 saa_writeb(SAA7134_VIDEO_PORT_CTRL2, video_out[vo][2]);
2531 saa_writeb(SAA7134_VIDEO_PORT_CTRL4, video_out[vo][4]);
2532 video_reg = video_out[vo][5];
2533 if (vid_port_opts & SET_CLOCK_NOT_DELAYED)
2534 video_reg &= ~VP_CLK_CTRL2_DELAYED;
2535 if (vid_port_opts & SET_CLOCK_INVERTED)
2536 video_reg |= VP_CLK_CTRL1_INVERTED;
2537 saa_writeb(SAA7134_VIDEO_PORT_CTRL5, video_reg);
2538 video_reg = video_out[vo][6];
2539 if (vid_port_opts & SET_VSYNC_OFF) {
2540 video_reg &= ~VP_VS_TYPE_MASK;
2541 video_reg |= VP_VS_TYPE_OFF;
2543 saa_writeb(SAA7134_VIDEO_PORT_CTRL6, video_reg);
2544 saa_writeb(SAA7134_VIDEO_PORT_CTRL7, video_out[vo][7]);
2545 saa_writeb(SAA7134_VIDEO_PORT_CTRL8, video_out[vo][8]);
2547 /* Start videoport */
2548 saa_writeb(SAA7134_VIDEO_PORT_CTRL3, video_out[vo][3]);
2553 int saa7134_video_init2(struct saa7134_dev *dev)
2556 set_tvnorm(dev,&tvnorms[0]);
2558 saa7134_tvaudio_setmute(dev);
2559 saa7134_tvaudio_setvolume(dev,dev->ctl_volume);
2563 void saa7134_irq_video_signalchange(struct saa7134_dev *dev)
2565 static const char *st[] = {
2566 "(no signal)", "NTSC", "PAL", "SECAM" };
2569 st1 = saa_readb(SAA7134_STATUS_VIDEO1);
2570 st2 = saa_readb(SAA7134_STATUS_VIDEO2);
2571 dprintk("DCSDT: pll: %s, sync: %s, norm: %s\n",
2572 (st1 & 0x40) ? "not locked" : "locked",
2573 (st2 & 0x40) ? "no" : "yes",
2575 dev->nosignal = (st1 & 0x40) || (st2 & 0x40) || !(st2 & 0x1);
2577 if (dev->nosignal) {
2578 /* no video signal -> mute audio */
2579 if (dev->ctl_automute)
2581 saa7134_tvaudio_setmute(dev);
2583 /* wake up tvaudio audio carrier scan thread */
2584 saa7134_tvaudio_do_scan(dev);
2587 if ((st2 & 0x80) && !noninterlaced && !dev->nosignal)
2588 saa_clearb(SAA7134_SYNC_CTRL, 0x20);
2590 saa_setb(SAA7134_SYNC_CTRL, 0x20);
2592 if (dev->mops && dev->mops->signal_change)
2593 dev->mops->signal_change(dev);
2597 void saa7134_irq_video_done(struct saa7134_dev *dev, unsigned long status)
2599 enum v4l2_field field;
2601 spin_lock(&dev->slock);
2602 if (dev->video_q.curr) {
2603 dev->video_fieldcount++;
2604 field = dev->video_q.curr->vb.field;
2605 if (V4L2_FIELD_HAS_BOTH(field)) {
2606 /* make sure we have seen both fields */
2607 if ((status & 0x10) == 0x00) {
2608 dev->video_q.curr->top_seen = 1;
2611 if (!dev->video_q.curr->top_seen)
2613 } else if (field == V4L2_FIELD_TOP) {
2614 if ((status & 0x10) != 0x10)
2616 } else if (field == V4L2_FIELD_BOTTOM) {
2617 if ((status & 0x10) != 0x00)
2620 dev->video_q.curr->vb.field_count = dev->video_fieldcount;
2621 saa7134_buffer_finish(dev,&dev->video_q,VIDEOBUF_DONE);
2623 saa7134_buffer_next(dev,&dev->video_q);
2626 spin_unlock(&dev->slock);
2629 /* ----------------------------------------------------------- */