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/moduleparam.h>
27 #include <linux/kernel.h>
28 #include <linux/slab.h>
30 #include "saa7134-reg.h"
33 /* ------------------------------------------------------------------ */
35 static unsigned int video_debug = 0;
36 static unsigned int gbuffers = 8;
37 static unsigned int noninterlaced = 0;
38 static unsigned int gbufsize = 720*576*4;
39 static unsigned int gbufsize_max = 720*576*4;
40 module_param(video_debug, int, 0644);
41 MODULE_PARM_DESC(video_debug,"enable debug messages [video]");
42 module_param(gbuffers, int, 0444);
43 MODULE_PARM_DESC(gbuffers,"number of capture buffers, range 2-32");
44 module_param(noninterlaced, int, 0644);
45 MODULE_PARM_DESC(noninterlaced,"video input is noninterlaced");
47 #define dprintk(fmt, arg...) if (video_debug) \
48 printk(KERN_DEBUG "%s/video: " fmt, dev->name , ## arg)
50 /* ------------------------------------------------------------------ */
51 /* data structs for video */
53 static int video_out[][9] = {
54 [CCIR656] = { 0x00, 0xb1, 0x00, 0xa1, 0x00, 0x04, 0x06, 0x00, 0x00 },
57 static struct saa7134_format formats[] = {
60 .fourcc = V4L2_PIX_FMT_GREY,
64 .name = "15 bpp RGB, le",
65 .fourcc = V4L2_PIX_FMT_RGB555,
69 .name = "15 bpp RGB, be",
70 .fourcc = V4L2_PIX_FMT_RGB555X,
75 .name = "16 bpp RGB, le",
76 .fourcc = V4L2_PIX_FMT_RGB565,
80 .name = "16 bpp RGB, be",
81 .fourcc = V4L2_PIX_FMT_RGB565X,
86 .name = "24 bpp RGB, le",
87 .fourcc = V4L2_PIX_FMT_BGR24,
91 .name = "24 bpp RGB, be",
92 .fourcc = V4L2_PIX_FMT_RGB24,
97 .name = "32 bpp RGB, le",
98 .fourcc = V4L2_PIX_FMT_BGR32,
102 .name = "32 bpp RGB, be",
103 .fourcc = V4L2_PIX_FMT_RGB32,
109 .name = "4:2:2 packed, YUYV",
110 .fourcc = V4L2_PIX_FMT_YUYV,
116 .name = "4:2:2 packed, UYVY",
117 .fourcc = V4L2_PIX_FMT_UYVY,
122 .name = "4:2:2 planar, Y-Cb-Cr",
123 .fourcc = V4L2_PIX_FMT_YUV422P,
131 .name = "4:2:0 planar, Y-Cb-Cr",
132 .fourcc = V4L2_PIX_FMT_YUV420,
140 .name = "4:2:0 planar, Y-Cb-Cr",
141 .fourcc = V4L2_PIX_FMT_YVU420,
151 #define FORMATS ARRAY_SIZE(formats)
153 #define NORM_625_50 \
156 .video_v_start = 24, \
157 .video_v_stop = 311, \
158 .vbi_v_start_0 = 7, \
159 .vbi_v_stop_0 = 22, \
160 .vbi_v_start_1 = 319, \
163 #define NORM_525_60 \
166 .video_v_start = 23, \
167 .video_v_stop = 262, \
168 .vbi_v_start_0 = 10, \
169 .vbi_v_stop_0 = 21, \
170 .vbi_v_start_1 = 273, \
173 static struct saa7134_tvnorm tvnorms[] = {
175 .name = "PAL", /* autodetect */
179 .sync_control = 0x18,
180 .luma_control = 0x40,
181 .chroma_ctrl1 = 0x81,
183 .chroma_ctrl2 = 0x06,
188 .id = V4L2_STD_PAL_BG,
191 .sync_control = 0x18,
192 .luma_control = 0x40,
193 .chroma_ctrl1 = 0x81,
195 .chroma_ctrl2 = 0x06,
200 .id = V4L2_STD_PAL_I,
203 .sync_control = 0x18,
204 .luma_control = 0x40,
205 .chroma_ctrl1 = 0x81,
207 .chroma_ctrl2 = 0x06,
212 .id = V4L2_STD_PAL_DK,
215 .sync_control = 0x18,
216 .luma_control = 0x40,
217 .chroma_ctrl1 = 0x81,
219 .chroma_ctrl2 = 0x06,
227 .sync_control = 0x59,
228 .luma_control = 0x40,
229 .chroma_ctrl1 = 0x89,
231 .chroma_ctrl2 = 0x0e,
236 .id = V4L2_STD_SECAM,
239 .sync_control = 0x18, /* old: 0x58, */
240 .luma_control = 0x1b,
241 .chroma_ctrl1 = 0xd1,
243 .chroma_ctrl2 = 0x00,
248 .id = V4L2_STD_PAL_M,
251 .sync_control = 0x59,
252 .luma_control = 0x40,
253 .chroma_ctrl1 = 0xb9,
255 .chroma_ctrl2 = 0x0e,
260 .id = V4L2_STD_PAL_Nc,
263 .sync_control = 0x18,
264 .luma_control = 0x40,
265 .chroma_ctrl1 = 0xa1,
267 .chroma_ctrl2 = 0x06,
272 .id = V4L2_STD_PAL_60,
280 .vbi_v_start_1 = 273,
283 .sync_control = 0x18,
284 .luma_control = 0x40,
285 .chroma_ctrl1 = 0x81,
287 .chroma_ctrl2 = 0x06,
291 #define TVNORMS ARRAY_SIZE(tvnorms)
293 #define V4L2_CID_PRIVATE_INVERT (V4L2_CID_PRIVATE_BASE + 0)
294 #define V4L2_CID_PRIVATE_Y_ODD (V4L2_CID_PRIVATE_BASE + 1)
295 #define V4L2_CID_PRIVATE_Y_EVEN (V4L2_CID_PRIVATE_BASE + 2)
296 #define V4L2_CID_PRIVATE_AUTOMUTE (V4L2_CID_PRIVATE_BASE + 3)
297 #define V4L2_CID_PRIVATE_LASTP1 (V4L2_CID_PRIVATE_BASE + 4)
299 static const struct v4l2_queryctrl no_ctrl = {
301 .flags = V4L2_CTRL_FLAG_DISABLED,
303 static const struct v4l2_queryctrl video_ctrls[] = {
306 .id = V4L2_CID_BRIGHTNESS,
307 .name = "Brightness",
311 .default_value = 128,
312 .type = V4L2_CTRL_TYPE_INTEGER,
314 .id = V4L2_CID_CONTRAST,
320 .type = V4L2_CTRL_TYPE_INTEGER,
322 .id = V4L2_CID_SATURATION,
323 .name = "Saturation",
328 .type = V4L2_CTRL_TYPE_INTEGER,
336 .type = V4L2_CTRL_TYPE_INTEGER,
338 .id = V4L2_CID_HFLIP,
342 .type = V4L2_CTRL_TYPE_BOOLEAN,
346 .id = V4L2_CID_AUDIO_MUTE,
350 .type = V4L2_CTRL_TYPE_BOOLEAN,
352 .id = V4L2_CID_AUDIO_VOLUME,
358 .type = V4L2_CTRL_TYPE_INTEGER,
360 /* --- private --- */
362 .id = V4L2_CID_PRIVATE_INVERT,
366 .type = V4L2_CTRL_TYPE_BOOLEAN,
368 .id = V4L2_CID_PRIVATE_Y_ODD,
369 .name = "y offset odd field",
373 .type = V4L2_CTRL_TYPE_INTEGER,
375 .id = V4L2_CID_PRIVATE_Y_EVEN,
376 .name = "y offset even field",
380 .type = V4L2_CTRL_TYPE_INTEGER,
382 .id = V4L2_CID_PRIVATE_AUTOMUTE,
387 .type = V4L2_CTRL_TYPE_BOOLEAN,
390 static const unsigned int CTRLS = ARRAY_SIZE(video_ctrls);
392 static const struct v4l2_queryctrl* ctrl_by_id(unsigned int id)
396 for (i = 0; i < CTRLS; i++)
397 if (video_ctrls[i].id == id)
398 return video_ctrls+i;
402 static struct saa7134_format* format_by_fourcc(unsigned int fourcc)
406 for (i = 0; i < FORMATS; i++)
407 if (formats[i].fourcc == fourcc)
412 /* ----------------------------------------------------------------------- */
413 /* resource management */
415 static int res_get(struct saa7134_dev *dev, struct saa7134_fh *fh, unsigned int bit)
417 if (fh->resources & bit)
418 /* have it already allocated */
423 if (dev->resources & bit) {
424 /* no, someone else uses it */
428 /* it's free, grab it */
429 fh->resources |= bit;
430 dev->resources |= bit;
431 dprintk("res: get %d\n",bit);
437 int res_check(struct saa7134_fh *fh, unsigned int bit)
439 return (fh->resources & bit);
443 int res_locked(struct saa7134_dev *dev, unsigned int bit)
445 return (dev->resources & bit);
449 void res_free(struct saa7134_dev *dev, struct saa7134_fh *fh, unsigned int bits)
451 if ((fh->resources & bits) != bits)
455 fh->resources &= ~bits;
456 dev->resources &= ~bits;
457 dprintk("res: put %d\n",bits);
461 /* ------------------------------------------------------------------ */
463 static void set_tvnorm(struct saa7134_dev *dev, struct saa7134_tvnorm *norm)
465 int luma_control,sync_control,mux;
467 dprintk("set tv norm = %s\n",norm->name);
470 mux = card_in(dev,dev->ctl_input).vmux;
471 luma_control = norm->luma_control;
472 sync_control = norm->sync_control;
475 luma_control |= 0x80; /* svideo */
476 if (noninterlaced || dev->nosignal)
477 sync_control |= 0x20;
480 dev->crop_bounds.left = norm->h_start;
481 dev->crop_defrect.left = norm->h_start;
482 dev->crop_bounds.width = norm->h_stop - norm->h_start +1;
483 dev->crop_defrect.width = norm->h_stop - norm->h_start +1;
485 dev->crop_bounds.top = (norm->vbi_v_stop_0+1)*2;
486 dev->crop_defrect.top = norm->video_v_start*2;
487 dev->crop_bounds.height = ((norm->id & V4L2_STD_525_60) ? 524 : 624)
488 - dev->crop_bounds.top;
489 dev->crop_defrect.height = (norm->video_v_stop - norm->video_v_start +1)*2;
491 dev->crop_current = dev->crop_defrect;
493 /* setup video decoder */
494 saa_writeb(SAA7134_INCR_DELAY, 0x08);
495 saa_writeb(SAA7134_ANALOG_IN_CTRL1, 0xc0 | mux);
496 saa_writeb(SAA7134_ANALOG_IN_CTRL2, 0x00);
498 saa_writeb(SAA7134_ANALOG_IN_CTRL3, 0x90);
499 saa_writeb(SAA7134_ANALOG_IN_CTRL4, 0x90);
500 saa_writeb(SAA7134_HSYNC_START, 0xeb);
501 saa_writeb(SAA7134_HSYNC_STOP, 0xe0);
502 saa_writeb(SAA7134_SOURCE_TIMING1, norm->src_timing);
504 saa_writeb(SAA7134_SYNC_CTRL, sync_control);
505 saa_writeb(SAA7134_LUMA_CTRL, luma_control);
506 saa_writeb(SAA7134_DEC_LUMA_BRIGHT, dev->ctl_bright);
507 saa_writeb(SAA7134_DEC_LUMA_CONTRAST, dev->ctl_contrast);
509 saa_writeb(SAA7134_DEC_CHROMA_SATURATION, dev->ctl_saturation);
510 saa_writeb(SAA7134_DEC_CHROMA_HUE, dev->ctl_hue);
511 saa_writeb(SAA7134_CHROMA_CTRL1, norm->chroma_ctrl1);
512 saa_writeb(SAA7134_CHROMA_GAIN, norm->chroma_gain);
514 saa_writeb(SAA7134_CHROMA_CTRL2, norm->chroma_ctrl2);
515 saa_writeb(SAA7134_MODE_DELAY_CTRL, 0x00);
517 saa_writeb(SAA7134_ANALOG_ADC, 0x01);
518 saa_writeb(SAA7134_VGATE_START, 0x11);
519 saa_writeb(SAA7134_VGATE_STOP, 0xfe);
520 saa_writeb(SAA7134_MISC_VGATE_MSB, norm->vgate_misc);
521 saa_writeb(SAA7134_RAW_DATA_GAIN, 0x40);
522 saa_writeb(SAA7134_RAW_DATA_OFFSET, 0x80);
524 saa7134_i2c_call_clients(dev,VIDIOC_S_STD,&norm->id);
527 static void video_mux(struct saa7134_dev *dev, int input)
529 dprintk("video input = %d [%s]\n",input,card_in(dev,input).name);
530 dev->ctl_input = input;
531 set_tvnorm(dev,dev->tvnorm);
532 saa7134_tvaudio_setinput(dev,&card_in(dev,input));
535 static void set_h_prescale(struct saa7134_dev *dev, int task, int prescale)
537 static const struct {
544 /* XPSC XACL XC2_1 XDCG VPFY */
556 static const int count = ARRAY_SIZE(vals);
559 for (i = 0; i < count; i++)
560 if (vals[i].xpsc == prescale)
565 saa_writeb(SAA7134_H_PRESCALE(task), vals[i].xpsc);
566 saa_writeb(SAA7134_ACC_LENGTH(task), vals[i].xacl);
567 saa_writeb(SAA7134_LEVEL_CTRL(task),
568 (vals[i].xc2_1 << 3) | (vals[i].xdcg));
569 saa_andorb(SAA7134_FIR_PREFILTER_CTRL(task), 0x0f,
570 (vals[i].vpfy << 2) | vals[i].vpfy);
573 static void set_v_scale(struct saa7134_dev *dev, int task, int yscale)
577 saa_writeb(SAA7134_V_SCALE_RATIO1(task), yscale & 0xff);
578 saa_writeb(SAA7134_V_SCALE_RATIO2(task), yscale >> 8);
580 mirror = (dev->ctl_mirror) ? 0x02 : 0x00;
583 dprintk("yscale LPI yscale=%d\n",yscale);
584 saa_writeb(SAA7134_V_FILTER(task), 0x00 | mirror);
585 saa_writeb(SAA7134_LUMA_CONTRAST(task), 0x40);
586 saa_writeb(SAA7134_CHROMA_SATURATION(task), 0x40);
589 val = 0x40 * 1024 / yscale;
590 dprintk("yscale ACM yscale=%d val=0x%x\n",yscale,val);
591 saa_writeb(SAA7134_V_FILTER(task), 0x01 | mirror);
592 saa_writeb(SAA7134_LUMA_CONTRAST(task), val);
593 saa_writeb(SAA7134_CHROMA_SATURATION(task), val);
595 saa_writeb(SAA7134_LUMA_BRIGHT(task), 0x80);
598 static void set_size(struct saa7134_dev *dev, int task,
599 int width, int height, int interlace)
601 int prescale,xscale,yscale,y_even,y_odd;
602 int h_start, h_stop, v_start, v_stop;
603 int div = interlace ? 2 : 1;
605 /* setup video scaler */
606 h_start = dev->crop_current.left;
607 v_start = dev->crop_current.top/2;
608 h_stop = (dev->crop_current.left + dev->crop_current.width -1);
609 v_stop = (dev->crop_current.top + dev->crop_current.height -1)/2;
611 saa_writeb(SAA7134_VIDEO_H_START1(task), h_start & 0xff);
612 saa_writeb(SAA7134_VIDEO_H_START2(task), h_start >> 8);
613 saa_writeb(SAA7134_VIDEO_H_STOP1(task), h_stop & 0xff);
614 saa_writeb(SAA7134_VIDEO_H_STOP2(task), h_stop >> 8);
615 saa_writeb(SAA7134_VIDEO_V_START1(task), v_start & 0xff);
616 saa_writeb(SAA7134_VIDEO_V_START2(task), v_start >> 8);
617 saa_writeb(SAA7134_VIDEO_V_STOP1(task), v_stop & 0xff);
618 saa_writeb(SAA7134_VIDEO_V_STOP2(task), v_stop >> 8);
620 prescale = dev->crop_current.width / width;
623 xscale = 1024 * dev->crop_current.width / prescale / width;
624 yscale = 512 * div * dev->crop_current.height / height;
625 dprintk("prescale=%d xscale=%d yscale=%d\n",prescale,xscale,yscale);
626 set_h_prescale(dev,task,prescale);
627 saa_writeb(SAA7134_H_SCALE_INC1(task), xscale & 0xff);
628 saa_writeb(SAA7134_H_SCALE_INC2(task), xscale >> 8);
629 set_v_scale(dev,task,yscale);
631 saa_writeb(SAA7134_VIDEO_PIXELS1(task), width & 0xff);
632 saa_writeb(SAA7134_VIDEO_PIXELS2(task), width >> 8);
633 saa_writeb(SAA7134_VIDEO_LINES1(task), height/div & 0xff);
634 saa_writeb(SAA7134_VIDEO_LINES2(task), height/div >> 8);
636 /* deinterlace y offsets */
637 y_odd = dev->ctl_y_odd;
638 y_even = dev->ctl_y_even;
639 saa_writeb(SAA7134_V_PHASE_OFFSET0(task), y_odd);
640 saa_writeb(SAA7134_V_PHASE_OFFSET1(task), y_even);
641 saa_writeb(SAA7134_V_PHASE_OFFSET2(task), y_odd);
642 saa_writeb(SAA7134_V_PHASE_OFFSET3(task), y_even);
645 /* ------------------------------------------------------------------ */
653 static void sort_cliplist(struct cliplist *cl, int entries)
655 struct cliplist swap;
658 for (i = entries-2; i >= 0; i--) {
659 for (n = 0, j = 0; j <= i; j++) {
660 if (cl[j].position > cl[j+1].position) {
672 static void set_cliplist(struct saa7134_dev *dev, int reg,
673 struct cliplist *cl, int entries, char *name)
678 for (i = 0; i < entries; i++) {
679 winbits |= cl[i].enable;
680 winbits &= ~cl[i].disable;
681 if (i < 15 && cl[i].position == cl[i+1].position)
683 saa_writeb(reg + 0, winbits);
684 saa_writeb(reg + 2, cl[i].position & 0xff);
685 saa_writeb(reg + 3, cl[i].position >> 8);
686 dprintk("clip: %s winbits=%02x pos=%d\n",
687 name,winbits,cl[i].position);
690 for (; reg < 0x400; reg += 8) {
691 saa_writeb(reg+ 0, 0);
692 saa_writeb(reg + 1, 0);
693 saa_writeb(reg + 2, 0);
694 saa_writeb(reg + 3, 0);
698 static int clip_range(int val)
705 static int setup_clipping(struct saa7134_dev *dev, struct v4l2_clip *clips,
706 int nclips, int interlace)
708 struct cliplist col[16], row[16];
710 int div = interlace ? 2 : 1;
712 memset(col,0,sizeof(col)); cols = 0;
713 memset(row,0,sizeof(row)); rows = 0;
714 for (i = 0; i < nclips && i < 8; i++) {
715 col[cols].position = clip_range(clips[i].c.left);
716 col[cols].enable = (1 << i);
718 col[cols].position = clip_range(clips[i].c.left+clips[i].c.width);
719 col[cols].disable = (1 << i);
721 row[rows].position = clip_range(clips[i].c.top / div);
722 row[rows].enable = (1 << i);
724 row[rows].position = clip_range((clips[i].c.top + clips[i].c.height)
726 row[rows].disable = (1 << i);
729 sort_cliplist(col,cols);
730 sort_cliplist(row,rows);
731 set_cliplist(dev,0x380,col,cols,"cols");
732 set_cliplist(dev,0x384,row,rows,"rows");
736 static int verify_preview(struct saa7134_dev *dev, struct v4l2_window *win)
738 enum v4l2_field field;
741 if (NULL == dev->ovbuf.base)
743 if (NULL == dev->ovfmt)
745 if (win->w.width < 48 || win->w.height < 32)
747 if (win->clipcount > 2048)
751 maxw = dev->crop_current.width;
752 maxh = dev->crop_current.height;
754 if (V4L2_FIELD_ANY == field) {
755 field = (win->w.height > maxh/2)
756 ? V4L2_FIELD_INTERLACED
761 case V4L2_FIELD_BOTTOM:
764 case V4L2_FIELD_INTERLACED:
771 if (win->w.width > maxw)
773 if (win->w.height > maxh)
774 win->w.height = maxh;
778 static int start_preview(struct saa7134_dev *dev, struct saa7134_fh *fh)
780 unsigned long base,control,bpl;
783 err = verify_preview(dev,&fh->win);
787 dev->ovfield = fh->win.field;
788 dprintk("start_preview %dx%d+%d+%d %s field=%s\n",
789 fh->win.w.width,fh->win.w.height,
790 fh->win.w.left,fh->win.w.top,
791 dev->ovfmt->name,v4l2_field_names[dev->ovfield]);
793 /* setup window + clipping */
794 set_size(dev,TASK_B,fh->win.w.width,fh->win.w.height,
795 V4L2_FIELD_HAS_BOTH(dev->ovfield));
796 setup_clipping(dev,fh->clips,fh->nclips,
797 V4L2_FIELD_HAS_BOTH(dev->ovfield));
799 saa_andorb(SAA7134_DATA_PATH(TASK_B), 0x3f, 0x03);
801 saa_andorb(SAA7134_DATA_PATH(TASK_B), 0x3f, 0x01);
802 saa_writeb(SAA7134_OFMT_VIDEO_B, dev->ovfmt->pm | 0x20);
804 /* dma: setup channel 1 (= Video Task B) */
805 base = (unsigned long)dev->ovbuf.base;
806 base += dev->ovbuf.fmt.bytesperline * fh->win.w.top;
807 base += dev->ovfmt->depth/8 * fh->win.w.left;
808 bpl = dev->ovbuf.fmt.bytesperline;
809 control = SAA7134_RS_CONTROL_BURST_16;
810 if (dev->ovfmt->bswap)
811 control |= SAA7134_RS_CONTROL_BSWAP;
812 if (dev->ovfmt->wswap)
813 control |= SAA7134_RS_CONTROL_WSWAP;
814 if (V4L2_FIELD_HAS_BOTH(dev->ovfield)) {
815 saa_writel(SAA7134_RS_BA1(1),base);
816 saa_writel(SAA7134_RS_BA2(1),base+bpl);
817 saa_writel(SAA7134_RS_PITCH(1),bpl*2);
818 saa_writel(SAA7134_RS_CONTROL(1),control);
820 saa_writel(SAA7134_RS_BA1(1),base);
821 saa_writel(SAA7134_RS_BA2(1),base);
822 saa_writel(SAA7134_RS_PITCH(1),bpl);
823 saa_writel(SAA7134_RS_CONTROL(1),control);
828 saa7134_set_dmabits(dev);
833 static int stop_preview(struct saa7134_dev *dev, struct saa7134_fh *fh)
836 saa7134_set_dmabits(dev);
840 /* ------------------------------------------------------------------ */
842 static int buffer_activate(struct saa7134_dev *dev,
843 struct saa7134_buf *buf,
844 struct saa7134_buf *next)
846 unsigned long base,control,bpl;
847 unsigned long bpl_uv,lines_uv,base2,base3,tmp; /* planar */
849 dprintk("buffer_activate buf=%p\n",buf);
850 buf->vb.state = STATE_ACTIVE;
853 set_size(dev,TASK_A,buf->vb.width,buf->vb.height,
854 V4L2_FIELD_HAS_BOTH(buf->vb.field));
856 saa_andorb(SAA7134_DATA_PATH(TASK_A), 0x3f, 0x03);
858 saa_andorb(SAA7134_DATA_PATH(TASK_A), 0x3f, 0x01);
859 saa_writeb(SAA7134_OFMT_VIDEO_A, buf->fmt->pm);
861 /* DMA: setup channel 0 (= Video Task A0) */
862 base = saa7134_buffer_base(buf);
863 if (buf->fmt->planar)
866 bpl = (buf->vb.width * buf->fmt->depth) / 8;
867 control = SAA7134_RS_CONTROL_BURST_16 |
868 SAA7134_RS_CONTROL_ME |
869 (buf->pt->dma >> 12);
871 control |= SAA7134_RS_CONTROL_BSWAP;
873 control |= SAA7134_RS_CONTROL_WSWAP;
874 if (V4L2_FIELD_HAS_BOTH(buf->vb.field)) {
876 saa_writel(SAA7134_RS_BA1(0),base);
877 saa_writel(SAA7134_RS_BA2(0),base+bpl);
878 saa_writel(SAA7134_RS_PITCH(0),bpl*2);
881 saa_writel(SAA7134_RS_BA1(0),base);
882 saa_writel(SAA7134_RS_BA2(0),base);
883 saa_writel(SAA7134_RS_PITCH(0),bpl);
885 saa_writel(SAA7134_RS_CONTROL(0),control);
887 if (buf->fmt->planar) {
888 /* DMA: setup channel 4+5 (= planar task A) */
889 bpl_uv = bpl >> buf->fmt->hshift;
890 lines_uv = buf->vb.height >> buf->fmt->vshift;
891 base2 = base + bpl * buf->vb.height;
892 base3 = base2 + bpl_uv * lines_uv;
893 if (buf->fmt->uvswap)
894 tmp = base2, base2 = base3, base3 = tmp;
895 dprintk("uv: bpl=%ld lines=%ld base2/3=%ld/%ld\n",
896 bpl_uv,lines_uv,base2,base3);
897 if (V4L2_FIELD_HAS_BOTH(buf->vb.field)) {
899 saa_writel(SAA7134_RS_BA1(4),base2);
900 saa_writel(SAA7134_RS_BA2(4),base2+bpl_uv);
901 saa_writel(SAA7134_RS_PITCH(4),bpl_uv*2);
902 saa_writel(SAA7134_RS_BA1(5),base3);
903 saa_writel(SAA7134_RS_BA2(5),base3+bpl_uv);
904 saa_writel(SAA7134_RS_PITCH(5),bpl_uv*2);
907 saa_writel(SAA7134_RS_BA1(4),base2);
908 saa_writel(SAA7134_RS_BA2(4),base2);
909 saa_writel(SAA7134_RS_PITCH(4),bpl_uv);
910 saa_writel(SAA7134_RS_BA1(5),base3);
911 saa_writel(SAA7134_RS_BA2(5),base3);
912 saa_writel(SAA7134_RS_PITCH(5),bpl_uv);
914 saa_writel(SAA7134_RS_CONTROL(4),control);
915 saa_writel(SAA7134_RS_CONTROL(5),control);
919 saa7134_set_dmabits(dev);
920 mod_timer(&dev->video_q.timeout, jiffies+BUFFER_TIMEOUT);
924 static int buffer_prepare(struct videobuf_queue *q,
925 struct videobuf_buffer *vb,
926 enum v4l2_field field)
928 struct saa7134_fh *fh = q->priv_data;
929 struct saa7134_dev *dev = fh->dev;
930 struct saa7134_buf *buf = container_of(vb,struct saa7134_buf,vb);
937 if (fh->width < 48 ||
939 fh->width/4 > dev->crop_current.width ||
940 fh->height/4 > dev->crop_current.height ||
941 fh->width > dev->crop_bounds.width ||
942 fh->height > dev->crop_bounds.height)
944 size = (fh->width * fh->height * fh->fmt->depth) >> 3;
945 if (0 != buf->vb.baddr && buf->vb.bsize < size)
948 dprintk("buffer_prepare [%d,size=%dx%d,bytes=%d,fields=%s,%s]\n",
949 vb->i,fh->width,fh->height,size,v4l2_field_names[field],
951 if (buf->vb.width != fh->width ||
952 buf->vb.height != fh->height ||
953 buf->vb.size != size ||
954 buf->vb.field != field ||
955 buf->fmt != fh->fmt) {
956 saa7134_dma_free(dev,buf);
959 if (STATE_NEEDS_INIT == buf->vb.state) {
960 buf->vb.width = fh->width;
961 buf->vb.height = fh->height;
963 buf->vb.field = field;
965 buf->pt = &fh->pt_cap;
967 err = videobuf_iolock(dev->pci,&buf->vb,&dev->ovbuf);
970 err = saa7134_pgtable_build(dev->pci,buf->pt,
973 saa7134_buffer_startpage(buf));
977 buf->vb.state = STATE_PREPARED;
978 buf->activate = buffer_activate;
982 saa7134_dma_free(dev,buf);
987 buffer_setup(struct videobuf_queue *q, unsigned int *count, unsigned int *size)
989 struct saa7134_fh *fh = q->priv_data;
991 *size = fh->fmt->depth * fh->width * fh->height >> 3;
994 *count = saa7134_buffer_count(*size,*count);
998 static void buffer_queue(struct videobuf_queue *q, struct videobuf_buffer *vb)
1000 struct saa7134_fh *fh = q->priv_data;
1001 struct saa7134_buf *buf = container_of(vb,struct saa7134_buf,vb);
1003 saa7134_buffer_queue(fh->dev,&fh->dev->video_q,buf);
1006 static void buffer_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
1008 struct saa7134_fh *fh = q->priv_data;
1009 struct saa7134_buf *buf = container_of(vb,struct saa7134_buf,vb);
1011 saa7134_dma_free(fh->dev,buf);
1014 static struct videobuf_queue_ops video_qops = {
1015 .buf_setup = buffer_setup,
1016 .buf_prepare = buffer_prepare,
1017 .buf_queue = buffer_queue,
1018 .buf_release = buffer_release,
1021 /* ------------------------------------------------------------------ */
1023 static int get_control(struct saa7134_dev *dev, struct v4l2_control *c)
1025 const struct v4l2_queryctrl* ctrl;
1027 ctrl = ctrl_by_id(c->id);
1031 case V4L2_CID_BRIGHTNESS:
1032 c->value = dev->ctl_bright;
1035 c->value = dev->ctl_hue;
1037 case V4L2_CID_CONTRAST:
1038 c->value = dev->ctl_contrast;
1040 case V4L2_CID_SATURATION:
1041 c->value = dev->ctl_saturation;
1043 case V4L2_CID_AUDIO_MUTE:
1044 c->value = dev->ctl_mute;
1046 case V4L2_CID_AUDIO_VOLUME:
1047 c->value = dev->ctl_volume;
1049 case V4L2_CID_PRIVATE_INVERT:
1050 c->value = dev->ctl_invert;
1052 case V4L2_CID_HFLIP:
1053 c->value = dev->ctl_mirror;
1055 case V4L2_CID_PRIVATE_Y_EVEN:
1056 c->value = dev->ctl_y_even;
1058 case V4L2_CID_PRIVATE_Y_ODD:
1059 c->value = dev->ctl_y_odd;
1061 case V4L2_CID_PRIVATE_AUTOMUTE:
1062 c->value = dev->ctl_automute;
1070 static int set_control(struct saa7134_dev *dev, struct saa7134_fh *fh,
1071 struct v4l2_control *c)
1073 const struct v4l2_queryctrl* ctrl;
1074 unsigned long flags;
1075 int restart_overlay = 0;
1077 ctrl = ctrl_by_id(c->id);
1080 dprintk("set_control name=%s val=%d\n",ctrl->name,c->value);
1081 switch (ctrl->type) {
1082 case V4L2_CTRL_TYPE_BOOLEAN:
1083 case V4L2_CTRL_TYPE_MENU:
1084 case V4L2_CTRL_TYPE_INTEGER:
1085 if (c->value < ctrl->minimum)
1086 c->value = ctrl->minimum;
1087 if (c->value > ctrl->maximum)
1088 c->value = ctrl->maximum;
1094 case V4L2_CID_BRIGHTNESS:
1095 dev->ctl_bright = c->value;
1096 saa_writeb(SAA7134_DEC_LUMA_BRIGHT, dev->ctl_bright);
1099 dev->ctl_hue = c->value;
1100 saa_writeb(SAA7134_DEC_CHROMA_HUE, dev->ctl_hue);
1102 case V4L2_CID_CONTRAST:
1103 dev->ctl_contrast = c->value;
1104 saa_writeb(SAA7134_DEC_LUMA_CONTRAST,
1105 dev->ctl_invert ? -dev->ctl_contrast : dev->ctl_contrast);
1107 case V4L2_CID_SATURATION:
1108 dev->ctl_saturation = c->value;
1109 saa_writeb(SAA7134_DEC_CHROMA_SATURATION,
1110 dev->ctl_invert ? -dev->ctl_saturation : dev->ctl_saturation);
1112 case V4L2_CID_AUDIO_MUTE:
1113 dev->ctl_mute = c->value;
1114 saa7134_tvaudio_setmute(dev);
1116 case V4L2_CID_AUDIO_VOLUME:
1117 dev->ctl_volume = c->value;
1118 saa7134_tvaudio_setvolume(dev,dev->ctl_volume);
1120 case V4L2_CID_PRIVATE_INVERT:
1121 dev->ctl_invert = c->value;
1122 saa_writeb(SAA7134_DEC_LUMA_CONTRAST,
1123 dev->ctl_invert ? -dev->ctl_contrast : dev->ctl_contrast);
1124 saa_writeb(SAA7134_DEC_CHROMA_SATURATION,
1125 dev->ctl_invert ? -dev->ctl_saturation : dev->ctl_saturation);
1127 case V4L2_CID_HFLIP:
1128 dev->ctl_mirror = c->value;
1129 restart_overlay = 1;
1131 case V4L2_CID_PRIVATE_Y_EVEN:
1132 dev->ctl_y_even = c->value;
1133 restart_overlay = 1;
1135 case V4L2_CID_PRIVATE_Y_ODD:
1136 dev->ctl_y_odd = c->value;
1137 restart_overlay = 1;
1139 case V4L2_CID_PRIVATE_AUTOMUTE:
1140 dev->ctl_automute = c->value;
1141 if (dev->tda9887_conf) {
1142 if (dev->ctl_automute)
1143 dev->tda9887_conf |= TDA9887_AUTOMUTE;
1145 dev->tda9887_conf &= ~TDA9887_AUTOMUTE;
1146 saa7134_i2c_call_clients(dev, TDA9887_SET_CONFIG,
1147 &dev->tda9887_conf);
1153 if (restart_overlay && fh && res_check(fh, RESOURCE_OVERLAY)) {
1154 spin_lock_irqsave(&dev->slock,flags);
1155 stop_preview(dev,fh);
1156 start_preview(dev,fh);
1157 spin_unlock_irqrestore(&dev->slock,flags);
1162 /* ------------------------------------------------------------------ */
1164 static struct videobuf_queue* saa7134_queue(struct saa7134_fh *fh)
1166 struct videobuf_queue* q = NULL;
1169 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1172 case V4L2_BUF_TYPE_VBI_CAPTURE:
1181 static int saa7134_resource(struct saa7134_fh *fh)
1186 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1187 res = RESOURCE_VIDEO;
1189 case V4L2_BUF_TYPE_VBI_CAPTURE:
1198 static int video_open(struct inode *inode, struct file *file)
1200 int minor = iminor(inode);
1201 struct saa7134_dev *h,*dev = NULL;
1202 struct saa7134_fh *fh;
1203 struct list_head *list;
1204 enum v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1206 list_for_each(list,&saa7134_devlist) {
1207 h = list_entry(list, struct saa7134_dev, devlist);
1208 if (h->video_dev && (h->video_dev->minor == minor))
1210 if (h->radio_dev && (h->radio_dev->minor == minor)) {
1214 if (h->vbi_dev && (h->vbi_dev->minor == minor)) {
1215 type = V4L2_BUF_TYPE_VBI_CAPTURE;
1222 dprintk("open minor=%d radio=%d type=%s\n",minor,radio,
1223 v4l2_type_names[type]);
1225 /* allocate + initialize per filehandle data */
1226 fh = kmalloc(sizeof(*fh),GFP_KERNEL);
1229 memset(fh,0,sizeof(*fh));
1230 file->private_data = fh;
1234 fh->fmt = format_by_fourcc(V4L2_PIX_FMT_BGR24);
1237 v4l2_prio_open(&dev->prio,&fh->prio);
1239 videobuf_queue_init(&fh->cap, &video_qops,
1240 dev->pci, &dev->slock,
1241 V4L2_BUF_TYPE_VIDEO_CAPTURE,
1242 V4L2_FIELD_INTERLACED,
1243 sizeof(struct saa7134_buf),
1245 videobuf_queue_init(&fh->vbi, &saa7134_vbi_qops,
1246 dev->pci, &dev->slock,
1247 V4L2_BUF_TYPE_VBI_CAPTURE,
1249 sizeof(struct saa7134_buf),
1251 saa7134_pgtable_alloc(dev->pci,&fh->pt_cap);
1252 saa7134_pgtable_alloc(dev->pci,&fh->pt_vbi);
1255 /* switch to radio mode */
1256 saa7134_tvaudio_setinput(dev,&card(dev).radio);
1257 saa7134_i2c_call_clients(dev,AUDC_SET_RADIO, NULL);
1259 /* switch to video/vbi mode */
1260 video_mux(dev,dev->ctl_input);
1266 video_read(struct file *file, char __user *data, size_t count, loff_t *ppos)
1268 struct saa7134_fh *fh = file->private_data;
1271 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1272 if (res_locked(fh->dev,RESOURCE_VIDEO))
1274 return videobuf_read_one(saa7134_queue(fh),
1276 file->f_flags & O_NONBLOCK);
1277 case V4L2_BUF_TYPE_VBI_CAPTURE:
1278 if (!res_get(fh->dev,fh,RESOURCE_VBI))
1280 return videobuf_read_stream(saa7134_queue(fh),
1281 data, count, ppos, 1,
1282 file->f_flags & O_NONBLOCK);
1291 video_poll(struct file *file, struct poll_table_struct *wait)
1293 struct saa7134_fh *fh = file->private_data;
1294 struct videobuf_buffer *buf = NULL;
1296 if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type)
1297 return videobuf_poll_stream(file, &fh->vbi, wait);
1299 if (res_check(fh,RESOURCE_VIDEO)) {
1300 if (!list_empty(&fh->cap.stream))
1301 buf = list_entry(fh->cap.stream.next, struct videobuf_buffer, stream);
1303 down(&fh->cap.lock);
1304 if (UNSET == fh->cap.read_off) {
1305 /* need to capture a new frame */
1306 if (res_locked(fh->dev,RESOURCE_VIDEO)) {
1310 if (0 != fh->cap.ops->buf_prepare(&fh->cap,fh->cap.read_buf,fh->cap.field)) {
1314 fh->cap.ops->buf_queue(&fh->cap,fh->cap.read_buf);
1315 fh->cap.read_off = 0;
1318 buf = fh->cap.read_buf;
1324 poll_wait(file, &buf->done, wait);
1325 if (buf->state == STATE_DONE ||
1326 buf->state == STATE_ERROR)
1327 return POLLIN|POLLRDNORM;
1331 static int video_release(struct inode *inode, struct file *file)
1333 struct saa7134_fh *fh = file->private_data;
1334 struct saa7134_dev *dev = fh->dev;
1335 unsigned long flags;
1337 /* turn off overlay */
1338 if (res_check(fh, RESOURCE_OVERLAY)) {
1339 spin_lock_irqsave(&dev->slock,flags);
1340 stop_preview(dev,fh);
1341 spin_unlock_irqrestore(&dev->slock,flags);
1342 res_free(dev,fh,RESOURCE_OVERLAY);
1345 /* stop video capture */
1346 if (res_check(fh, RESOURCE_VIDEO)) {
1347 videobuf_streamoff(&fh->cap);
1348 res_free(dev,fh,RESOURCE_VIDEO);
1350 if (fh->cap.read_buf) {
1351 buffer_release(&fh->cap,fh->cap.read_buf);
1352 kfree(fh->cap.read_buf);
1355 /* stop vbi capture */
1356 if (res_check(fh, RESOURCE_VBI)) {
1357 if (fh->vbi.streaming)
1358 videobuf_streamoff(&fh->vbi);
1359 if (fh->vbi.reading)
1360 videobuf_read_stop(&fh->vbi);
1361 res_free(dev,fh,RESOURCE_VBI);
1364 /* ts-capture will not work in planar mode, so turn it off Hac: 04.05*/
1365 saa_andorb(SAA7134_OFMT_VIDEO_A, 0x1f, 0);
1366 saa_andorb(SAA7134_OFMT_VIDEO_B, 0x1f, 0);
1367 saa_andorb(SAA7134_OFMT_DATA_A, 0x1f, 0);
1368 saa_andorb(SAA7134_OFMT_DATA_B, 0x1f, 0);
1370 saa7134_i2c_call_clients(dev, TUNER_SET_STANDBY, NULL);
1373 videobuf_mmap_free(&fh->cap);
1374 videobuf_mmap_free(&fh->vbi);
1375 saa7134_pgtable_free(dev->pci,&fh->pt_cap);
1376 saa7134_pgtable_free(dev->pci,&fh->pt_vbi);
1378 v4l2_prio_close(&dev->prio,&fh->prio);
1379 file->private_data = NULL;
1385 video_mmap(struct file *file, struct vm_area_struct * vma)
1387 struct saa7134_fh *fh = file->private_data;
1389 return videobuf_mmap_mapper(saa7134_queue(fh), vma);
1392 /* ------------------------------------------------------------------ */
1394 static void saa7134_vbi_fmt(struct saa7134_dev *dev, struct v4l2_format *f)
1396 struct saa7134_tvnorm *norm = dev->tvnorm;
1398 f->fmt.vbi.sampling_rate = 6750000 * 4;
1399 f->fmt.vbi.samples_per_line = 2048 /* VBI_LINE_LENGTH */;
1400 f->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
1401 f->fmt.vbi.offset = 64 * 4;
1402 f->fmt.vbi.start[0] = norm->vbi_v_start_0;
1403 f->fmt.vbi.count[0] = norm->vbi_v_stop_0 - norm->vbi_v_start_0 +1;
1404 f->fmt.vbi.start[1] = norm->vbi_v_start_1;
1405 f->fmt.vbi.count[1] = f->fmt.vbi.count[0];
1406 f->fmt.vbi.flags = 0; /* VBI_UNSYNC VBI_INTERLACED */
1410 static int saa7134_g_fmt(struct saa7134_dev *dev, struct saa7134_fh *fh,
1411 struct v4l2_format *f)
1414 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1415 memset(&f->fmt.pix,0,sizeof(f->fmt.pix));
1416 f->fmt.pix.width = fh->width;
1417 f->fmt.pix.height = fh->height;
1418 f->fmt.pix.field = fh->cap.field;
1419 f->fmt.pix.pixelformat = fh->fmt->fourcc;
1420 f->fmt.pix.bytesperline =
1421 (f->fmt.pix.width * fh->fmt->depth) >> 3;
1422 f->fmt.pix.sizeimage =
1423 f->fmt.pix.height * f->fmt.pix.bytesperline;
1425 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
1426 f->fmt.win = fh->win;
1428 case V4L2_BUF_TYPE_VBI_CAPTURE:
1429 saa7134_vbi_fmt(dev,f);
1436 static int saa7134_try_fmt(struct saa7134_dev *dev, struct saa7134_fh *fh,
1437 struct v4l2_format *f)
1442 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1444 struct saa7134_format *fmt;
1445 enum v4l2_field field;
1446 unsigned int maxw, maxh;
1448 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1452 field = f->fmt.pix.field;
1453 maxw = min(dev->crop_current.width*4, dev->crop_bounds.width);
1454 maxh = min(dev->crop_current.height*4, dev->crop_bounds.height);
1456 if (V4L2_FIELD_ANY == field) {
1457 field = (f->fmt.pix.height > maxh/2)
1458 ? V4L2_FIELD_INTERLACED
1459 : V4L2_FIELD_BOTTOM;
1462 case V4L2_FIELD_TOP:
1463 case V4L2_FIELD_BOTTOM:
1466 case V4L2_FIELD_INTERLACED:
1472 f->fmt.pix.field = field;
1473 if (f->fmt.pix.width < 48)
1474 f->fmt.pix.width = 48;
1475 if (f->fmt.pix.height < 32)
1476 f->fmt.pix.height = 32;
1477 if (f->fmt.pix.width > maxw)
1478 f->fmt.pix.width = maxw;
1479 if (f->fmt.pix.height > maxh)
1480 f->fmt.pix.height = maxh;
1481 f->fmt.pix.width &= ~0x03;
1482 f->fmt.pix.bytesperline =
1483 (f->fmt.pix.width * fmt->depth) >> 3;
1484 f->fmt.pix.sizeimage =
1485 f->fmt.pix.height * f->fmt.pix.bytesperline;
1489 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
1490 err = verify_preview(dev,&f->fmt.win);
1494 case V4L2_BUF_TYPE_VBI_CAPTURE:
1495 saa7134_vbi_fmt(dev,f);
1502 static int saa7134_s_fmt(struct saa7134_dev *dev, struct saa7134_fh *fh,
1503 struct v4l2_format *f)
1505 unsigned long flags;
1509 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1510 err = saa7134_try_fmt(dev,fh,f);
1514 fh->fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1515 fh->width = f->fmt.pix.width;
1516 fh->height = f->fmt.pix.height;
1517 fh->cap.field = f->fmt.pix.field;
1519 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
1520 err = verify_preview(dev,&f->fmt.win);
1525 fh->win = f->fmt.win;
1526 fh->nclips = f->fmt.win.clipcount;
1529 if (copy_from_user(fh->clips,f->fmt.win.clips,
1530 sizeof(struct v4l2_clip)*fh->nclips)) {
1535 if (res_check(fh, RESOURCE_OVERLAY)) {
1536 spin_lock_irqsave(&dev->slock,flags);
1537 stop_preview(dev,fh);
1538 start_preview(dev,fh);
1539 spin_unlock_irqrestore(&dev->slock,flags);
1543 case V4L2_BUF_TYPE_VBI_CAPTURE:
1544 saa7134_vbi_fmt(dev,f);
1551 int saa7134_common_ioctl(struct saa7134_dev *dev,
1552 unsigned int cmd, void *arg)
1557 case VIDIOC_QUERYCTRL:
1559 const struct v4l2_queryctrl *ctrl;
1560 struct v4l2_queryctrl *c = arg;
1562 if ((c->id < V4L2_CID_BASE ||
1563 c->id >= V4L2_CID_LASTP1) &&
1564 (c->id < V4L2_CID_PRIVATE_BASE ||
1565 c->id >= V4L2_CID_PRIVATE_LASTP1))
1567 ctrl = ctrl_by_id(c->id);
1568 *c = (NULL != ctrl) ? *ctrl : no_ctrl;
1572 return get_control(dev,arg);
1576 err = set_control(dev,NULL,arg);
1580 /* --- input switching --------------------------------------- */
1581 case VIDIOC_ENUMINPUT:
1583 struct v4l2_input *i = arg;
1587 if (n >= SAA7134_INPUT_MAX)
1589 if (NULL == card_in(dev,i->index).name)
1591 memset(i,0,sizeof(*i));
1593 i->type = V4L2_INPUT_TYPE_CAMERA;
1594 strcpy(i->name,card_in(dev,n).name);
1595 if (card_in(dev,n).tv)
1596 i->type = V4L2_INPUT_TYPE_TUNER;
1598 if (n == dev->ctl_input) {
1599 int v1 = saa_readb(SAA7134_STATUS_VIDEO1);
1600 int v2 = saa_readb(SAA7134_STATUS_VIDEO2);
1602 if (0 != (v1 & 0x40))
1603 i->status |= V4L2_IN_ST_NO_H_LOCK;
1604 if (0 != (v2 & 0x40))
1605 i->status |= V4L2_IN_ST_NO_SYNC;
1606 if (0 != (v2 & 0x0e))
1607 i->status |= V4L2_IN_ST_MACROVISION;
1609 for (n = 0; n < TVNORMS; n++)
1610 i->std |= tvnorms[n].id;
1613 case VIDIOC_G_INPUT:
1616 *i = dev->ctl_input;
1619 case VIDIOC_S_INPUT:
1623 if (*i < 0 || *i >= SAA7134_INPUT_MAX)
1625 if (NULL == card_in(dev,*i).name)
1636 EXPORT_SYMBOL(saa7134_common_ioctl);
1639 * This function is _not_ called directly, but from
1640 * video_generic_ioctl (and maybe others). userspace
1641 * copying is done already, arg is a kernel pointer.
1643 static int video_do_ioctl(struct inode *inode, struct file *file,
1644 unsigned int cmd, void *arg)
1646 struct saa7134_fh *fh = file->private_data;
1647 struct saa7134_dev *dev = fh->dev;
1648 unsigned long flags;
1651 if (video_debug > 1)
1652 saa7134_print_ioctl(dev->name,cmd);
1657 case VIDIOC_S_INPUT:
1658 case VIDIOC_S_TUNER:
1659 case VIDIOC_S_FREQUENCY:
1660 err = v4l2_prio_check(&dev->prio,&fh->prio);
1666 case VIDIOC_QUERYCAP:
1668 struct v4l2_capability *cap = arg;
1670 memset(cap,0,sizeof(*cap));
1671 strcpy(cap->driver, "saa7134");
1672 strlcpy(cap->card, saa7134_boards[dev->board].name,
1674 sprintf(cap->bus_info,"PCI:%s",pci_name(dev->pci));
1675 cap->version = SAA7134_VERSION_CODE;
1677 V4L2_CAP_VIDEO_CAPTURE |
1678 V4L2_CAP_VIDEO_OVERLAY |
1679 V4L2_CAP_VBI_CAPTURE |
1681 V4L2_CAP_READWRITE |
1686 /* --- tv standards ------------------------------------------ */
1687 case VIDIOC_ENUMSTD:
1689 struct v4l2_standard *e = arg;
1695 err = v4l2_video_std_construct(e, tvnorms[e->index].id,
1696 tvnorms[e->index].name);
1704 v4l2_std_id *id = arg;
1706 *id = dev->tvnorm->id;
1711 v4l2_std_id *id = arg;
1714 for (i = 0; i < TVNORMS; i++)
1715 if (*id == tvnorms[i].id)
1718 for (i = 0; i < TVNORMS; i++)
1719 if (*id & tvnorms[i].id)
1725 if (res_check(fh, RESOURCE_OVERLAY)) {
1726 spin_lock_irqsave(&dev->slock,flags);
1727 stop_preview(dev,fh);
1728 set_tvnorm(dev,&tvnorms[i]);
1729 start_preview(dev,fh);
1730 spin_unlock_irqrestore(&dev->slock,flags);
1732 set_tvnorm(dev,&tvnorms[i]);
1733 saa7134_tvaudio_do_scan(dev);
1738 case VIDIOC_CROPCAP:
1740 struct v4l2_cropcap *cap = arg;
1742 if (cap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1743 cap->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
1745 cap->bounds = dev->crop_bounds;
1746 cap->defrect = dev->crop_defrect;
1747 cap->pixelaspect.numerator = 1;
1748 cap->pixelaspect.denominator = 1;
1749 if (dev->tvnorm->id & V4L2_STD_525_60) {
1750 cap->pixelaspect.numerator = 11;
1751 cap->pixelaspect.denominator = 10;
1753 if (dev->tvnorm->id & V4L2_STD_625_50) {
1754 cap->pixelaspect.numerator = 54;
1755 cap->pixelaspect.denominator = 59;
1762 struct v4l2_crop * crop = arg;
1764 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1765 crop->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
1767 crop->c = dev->crop_current;
1772 struct v4l2_crop *crop = arg;
1773 struct v4l2_rect *b = &dev->crop_bounds;
1775 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1776 crop->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
1778 if (crop->c.height < 0)
1780 if (crop->c.width < 0)
1783 if (res_locked(fh->dev,RESOURCE_OVERLAY))
1785 if (res_locked(fh->dev,RESOURCE_VIDEO))
1788 if (crop->c.top < b->top)
1789 crop->c.top = b->top;
1790 if (crop->c.top > b->top + b->height)
1791 crop->c.top = b->top + b->height;
1792 if (crop->c.height > b->top - crop->c.top + b->height)
1793 crop->c.height = b->top - crop->c.top + b->height;
1795 if (crop->c.left < b->left)
1796 crop->c.top = b->left;
1797 if (crop->c.left > b->left + b->width)
1798 crop->c.top = b->left + b->width;
1799 if (crop->c.width > b->left - crop->c.left + b->width)
1800 crop->c.width = b->left - crop->c.left + b->width;
1802 dev->crop_current = crop->c;
1806 /* --- tuner ioctls ------------------------------------------ */
1807 case VIDIOC_G_TUNER:
1809 struct v4l2_tuner *t = arg;
1814 memset(t,0,sizeof(*t));
1815 for (n = 0; n < SAA7134_INPUT_MAX; n++)
1816 if (card_in(dev,n).tv)
1818 if (NULL != card_in(dev,n).name) {
1819 strcpy(t->name, "Television");
1820 t->capability = V4L2_TUNER_CAP_NORM |
1821 V4L2_TUNER_CAP_STEREO |
1822 V4L2_TUNER_CAP_LANG1 |
1823 V4L2_TUNER_CAP_LANG2;
1824 t->rangehigh = 0xffffffffUL;
1825 t->rxsubchans = saa7134_tvaudio_getstereo(dev);
1826 t->audmode = saa7134_tvaudio_rx2mode(t->rxsubchans);
1828 if (0 != (saa_readb(SAA7134_STATUS_VIDEO1) & 0x03))
1832 case VIDIOC_S_TUNER:
1834 struct v4l2_tuner *t = arg;
1837 mode = dev->thread.mode;
1838 if (UNSET == mode) {
1839 rx = saa7134_tvaudio_getstereo(dev);
1840 mode = saa7134_tvaudio_rx2mode(t->rxsubchans);
1842 if (mode != t->audmode) {
1843 dev->thread.mode = t->audmode;
1847 case VIDIOC_G_FREQUENCY:
1849 struct v4l2_frequency *f = arg;
1851 memset(f,0,sizeof(*f));
1852 f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1853 f->frequency = dev->ctl_freq;
1856 case VIDIOC_S_FREQUENCY:
1858 struct v4l2_frequency *f = arg;
1862 if (0 == fh->radio && V4L2_TUNER_ANALOG_TV != f->type)
1864 if (1 == fh->radio && V4L2_TUNER_RADIO != f->type)
1867 dev->ctl_freq = f->frequency;
1869 saa7134_i2c_call_clients(dev,VIDIOC_S_FREQUENCY,f);
1871 saa7134_tvaudio_do_scan(dev);
1876 /* --- control ioctls ---------------------------------------- */
1877 case VIDIOC_ENUMINPUT:
1878 case VIDIOC_G_INPUT:
1879 case VIDIOC_S_INPUT:
1880 case VIDIOC_QUERYCTRL:
1883 return saa7134_common_ioctl(dev, cmd, arg);
1885 case VIDIOC_G_AUDIO:
1887 struct v4l2_audio *a = arg;
1889 memset(a,0,sizeof(*a));
1890 strcpy(a->name,"audio");
1893 case VIDIOC_S_AUDIO:
1897 struct v4l2_captureparm *parm = arg;
1898 memset(parm,0,sizeof(*parm));
1902 case VIDIOC_G_PRIORITY:
1904 enum v4l2_priority *p = arg;
1906 *p = v4l2_prio_max(&dev->prio);
1909 case VIDIOC_S_PRIORITY:
1911 enum v4l2_priority *prio = arg;
1913 return v4l2_prio_change(&dev->prio, &fh->prio, *prio);
1916 /* --- preview ioctls ---------------------------------------- */
1917 case VIDIOC_ENUM_FMT:
1919 struct v4l2_fmtdesc *f = arg;
1920 enum v4l2_buf_type type;
1926 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1927 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
1928 if (index >= FORMATS)
1930 if (f->type == V4L2_BUF_TYPE_VIDEO_OVERLAY &&
1931 formats[index].planar)
1933 memset(f,0,sizeof(*f));
1936 strlcpy(f->description,formats[index].name,sizeof(f->description));
1937 f->pixelformat = formats[index].fourcc;
1939 case V4L2_BUF_TYPE_VBI_CAPTURE:
1942 memset(f,0,sizeof(*f));
1945 f->pixelformat = V4L2_PIX_FMT_GREY;
1946 strcpy(f->description,"vbi data");
1955 struct v4l2_framebuffer *fb = arg;
1958 fb->capability = V4L2_FBUF_CAP_LIST_CLIPPING;
1963 struct v4l2_framebuffer *fb = arg;
1964 struct saa7134_format *fmt;
1966 if(!capable(CAP_SYS_ADMIN) &&
1967 !capable(CAP_SYS_RAWIO))
1971 fmt = format_by_fourcc(fb->fmt.pixelformat);
1978 if (0 == dev->ovbuf.fmt.bytesperline)
1979 dev->ovbuf.fmt.bytesperline =
1980 dev->ovbuf.fmt.width*fmt->depth/8;
1983 case VIDIOC_OVERLAY:
1988 if (!res_get(dev,fh,RESOURCE_OVERLAY))
1990 spin_lock_irqsave(&dev->slock,flags);
1991 start_preview(dev,fh);
1992 spin_unlock_irqrestore(&dev->slock,flags);
1995 if (!res_check(fh, RESOURCE_OVERLAY))
1997 spin_lock_irqsave(&dev->slock,flags);
1998 stop_preview(dev,fh);
1999 spin_unlock_irqrestore(&dev->slock,flags);
2000 res_free(dev,fh,RESOURCE_OVERLAY);
2005 /* --- capture ioctls ---------------------------------------- */
2008 struct v4l2_format *f = arg;
2009 return saa7134_g_fmt(dev,fh,f);
2013 struct v4l2_format *f = arg;
2014 return saa7134_s_fmt(dev,fh,f);
2016 case VIDIOC_TRY_FMT:
2018 struct v4l2_format *f = arg;
2019 return saa7134_try_fmt(dev,fh,f);
2024 struct video_mbuf *mbuf = arg;
2025 struct videobuf_queue *q;
2026 struct v4l2_requestbuffers req;
2029 q = saa7134_queue(fh);
2030 memset(&req,0,sizeof(req));
2032 req.count = gbuffers;
2033 req.memory = V4L2_MEMORY_MMAP;
2034 err = videobuf_reqbufs(q,&req);
2037 memset(mbuf,0,sizeof(*mbuf));
2038 mbuf->frames = req.count;
2040 for (i = 0; i < mbuf->frames; i++) {
2041 mbuf->offsets[i] = q->bufs[i]->boff;
2042 mbuf->size += q->bufs[i]->bsize;
2046 case VIDIOC_REQBUFS:
2047 return videobuf_reqbufs(saa7134_queue(fh),arg);
2049 case VIDIOC_QUERYBUF:
2050 return videobuf_querybuf(saa7134_queue(fh),arg);
2053 return videobuf_qbuf(saa7134_queue(fh),arg);
2056 return videobuf_dqbuf(saa7134_queue(fh),arg,
2057 file->f_flags & O_NONBLOCK);
2059 case VIDIOC_STREAMON:
2061 int res = saa7134_resource(fh);
2063 if (!res_get(dev,fh,res))
2065 return videobuf_streamon(saa7134_queue(fh));
2067 case VIDIOC_STREAMOFF:
2069 int res = saa7134_resource(fh);
2071 err = videobuf_streamoff(saa7134_queue(fh));
2074 res_free(dev,fh,res);
2079 return v4l_compat_translate_ioctl(inode,file,cmd,arg,
2085 static int video_ioctl(struct inode *inode, struct file *file,
2086 unsigned int cmd, unsigned long arg)
2088 return video_usercopy(inode, file, cmd, arg, video_do_ioctl);
2091 static int radio_do_ioctl(struct inode *inode, struct file *file,
2092 unsigned int cmd, void *arg)
2094 struct saa7134_fh *fh = file->private_data;
2095 struct saa7134_dev *dev = fh->dev;
2097 if (video_debug > 1)
2098 saa7134_print_ioctl(dev->name,cmd);
2100 case VIDIOC_QUERYCAP:
2102 struct v4l2_capability *cap = arg;
2104 memset(cap,0,sizeof(*cap));
2105 strcpy(cap->driver, "saa7134");
2106 strlcpy(cap->card, saa7134_boards[dev->board].name,
2108 sprintf(cap->bus_info,"PCI:%s",pci_name(dev->pci));
2109 cap->version = SAA7134_VERSION_CODE;
2110 cap->capabilities = V4L2_CAP_TUNER;
2113 case VIDIOC_G_TUNER:
2115 struct v4l2_tuner *t = arg;
2120 memset(t,0,sizeof(*t));
2121 strcpy(t->name, "Radio");
2123 saa7134_i2c_call_clients(dev, VIDIOC_G_TUNER, t);
2127 case VIDIOC_S_TUNER:
2129 struct v4l2_tuner *t = arg;
2134 saa7134_i2c_call_clients(dev,VIDIOC_S_TUNER,t);
2138 case VIDIOC_ENUMINPUT:
2140 struct v4l2_input *i = arg;
2144 strcpy(i->name,"Radio");
2145 i->type = V4L2_INPUT_TYPE_TUNER;
2148 case VIDIOC_G_INPUT:
2154 case VIDIOC_G_AUDIO:
2156 struct v4l2_audio *a = arg;
2158 memset(a,0,sizeof(*a));
2159 strcpy(a->name,"Radio");
2164 v4l2_std_id *id = arg;
2168 case VIDIOC_S_AUDIO:
2169 case VIDIOC_S_INPUT:
2173 case VIDIOC_QUERYCTRL:
2175 const struct v4l2_queryctrl *ctrl;
2176 struct v4l2_queryctrl *c = arg;
2178 if (c->id < V4L2_CID_BASE ||
2179 c->id >= V4L2_CID_LASTP1)
2181 if (c->id == V4L2_CID_AUDIO_MUTE) {
2182 ctrl = ctrl_by_id(c->id);
2191 case VIDIOC_G_FREQUENCY:
2192 case VIDIOC_S_FREQUENCY:
2193 return video_do_ioctl(inode,file,cmd,arg);
2196 return v4l_compat_translate_ioctl(inode,file,cmd,arg,
2202 static int radio_ioctl(struct inode *inode, struct file *file,
2203 unsigned int cmd, unsigned long arg)
2205 return video_usercopy(inode, file, cmd, arg, radio_do_ioctl);
2208 static struct file_operations video_fops =
2210 .owner = THIS_MODULE,
2212 .release = video_release,
2216 .ioctl = video_ioctl,
2217 .llseek = no_llseek,
2220 static struct file_operations radio_fops =
2222 .owner = THIS_MODULE,
2224 .release = video_release,
2225 .ioctl = radio_ioctl,
2226 .llseek = no_llseek,
2229 /* ----------------------------------------------------------- */
2230 /* exported stuff */
2232 struct video_device saa7134_video_template =
2234 .name = "saa7134-video",
2235 .type = VID_TYPE_CAPTURE|VID_TYPE_TUNER|VID_TYPE_OVERLAY|
2236 VID_TYPE_CLIPPING|VID_TYPE_SCALES,
2238 .fops = &video_fops,
2242 struct video_device saa7134_vbi_template =
2244 .name = "saa7134-vbi",
2245 .type = VID_TYPE_TUNER|VID_TYPE_TELETEXT,
2247 .fops = &video_fops,
2251 struct video_device saa7134_radio_template =
2253 .name = "saa7134-radio",
2254 .type = VID_TYPE_TUNER,
2256 .fops = &radio_fops,
2260 int saa7134_video_init1(struct saa7134_dev *dev)
2262 /* sanitycheck insmod options */
2263 if (gbuffers < 2 || gbuffers > VIDEO_MAX_FRAME)
2265 if (gbufsize < 0 || gbufsize > gbufsize_max)
2266 gbufsize = gbufsize_max;
2267 gbufsize = (gbufsize + PAGE_SIZE - 1) & PAGE_MASK;
2269 /* put some sensible defaults into the data structures ... */
2270 dev->ctl_bright = ctrl_by_id(V4L2_CID_BRIGHTNESS)->default_value;
2271 dev->ctl_contrast = ctrl_by_id(V4L2_CID_CONTRAST)->default_value;
2272 dev->ctl_hue = ctrl_by_id(V4L2_CID_HUE)->default_value;
2273 dev->ctl_saturation = ctrl_by_id(V4L2_CID_SATURATION)->default_value;
2274 dev->ctl_volume = ctrl_by_id(V4L2_CID_AUDIO_VOLUME)->default_value;
2275 dev->ctl_mute = 1; // ctrl_by_id(V4L2_CID_AUDIO_MUTE)->default_value;
2276 dev->ctl_invert = ctrl_by_id(V4L2_CID_PRIVATE_INVERT)->default_value;
2277 dev->ctl_automute = ctrl_by_id(V4L2_CID_PRIVATE_AUTOMUTE)->default_value;
2279 if (dev->tda9887_conf && dev->ctl_automute)
2280 dev->tda9887_conf |= TDA9887_AUTOMUTE;
2283 INIT_LIST_HEAD(&dev->video_q.queue);
2284 init_timer(&dev->video_q.timeout);
2285 dev->video_q.timeout.function = saa7134_buffer_timeout;
2286 dev->video_q.timeout.data = (unsigned long)(&dev->video_q);
2287 dev->video_q.dev = dev;
2289 if (saa7134_boards[dev->board].video_out) {
2290 /* enable video output */
2291 int vo = saa7134_boards[dev->board].video_out;
2292 saa_writeb(SAA7134_VIDEO_PORT_CTRL0, video_out[vo][0]);
2293 saa_writeb(SAA7134_VIDEO_PORT_CTRL1, video_out[vo][1]);
2294 saa_writeb(SAA7134_VIDEO_PORT_CTRL2, video_out[vo][2]);
2295 saa_writeb(SAA7134_VIDEO_PORT_CTRL3, video_out[vo][3]);
2296 saa_writeb(SAA7134_VIDEO_PORT_CTRL4, video_out[vo][4]);
2297 saa_writeb(SAA7134_VIDEO_PORT_CTRL5, video_out[vo][5]);
2298 saa_writeb(SAA7134_VIDEO_PORT_CTRL6, video_out[vo][6]);
2299 saa_writeb(SAA7134_VIDEO_PORT_CTRL7, video_out[vo][7]);
2300 saa_writeb(SAA7134_VIDEO_PORT_CTRL8, video_out[vo][8]);
2306 int saa7134_video_init2(struct saa7134_dev *dev)
2309 set_tvnorm(dev,&tvnorms[0]);
2311 saa7134_tvaudio_setmute(dev);
2312 saa7134_tvaudio_setvolume(dev,dev->ctl_volume);
2316 int saa7134_video_fini(struct saa7134_dev *dev)
2322 void saa7134_irq_video_intl(struct saa7134_dev *dev)
2324 static const char *st[] = {
2325 "(no signal)", "NTSC", "PAL", "SECAM" };
2328 st1 = saa_readb(SAA7134_STATUS_VIDEO1);
2329 st2 = saa_readb(SAA7134_STATUS_VIDEO2);
2330 dprintk("DCSDT: pll: %s, sync: %s, norm: %s\n",
2331 (st1 & 0x40) ? "not locked" : "locked",
2332 (st2 & 0x40) ? "no" : "yes",
2334 dev->nosignal = (st1 & 0x40) || (st2 & 0x40);
2336 if (dev->nosignal) {
2337 /* no video signal -> mute audio */
2338 if (dev->ctl_automute)
2340 saa7134_tvaudio_setmute(dev);
2341 saa_setb(SAA7134_SYNC_CTRL, 0x20);
2343 /* wake up tvaudio audio carrier scan thread */
2344 saa7134_tvaudio_do_scan(dev);
2346 saa_clearb(SAA7134_SYNC_CTRL, 0x20);
2348 if (dev->mops && dev->mops->signal_change)
2349 dev->mops->signal_change(dev);
2352 void saa7134_irq_video_done(struct saa7134_dev *dev, unsigned long status)
2354 enum v4l2_field field;
2356 spin_lock(&dev->slock);
2357 if (dev->video_q.curr) {
2358 dev->video_fieldcount++;
2359 field = dev->video_q.curr->vb.field;
2360 if (V4L2_FIELD_HAS_BOTH(field)) {
2361 /* make sure we have seen both fields */
2362 if ((status & 0x10) == 0x00) {
2363 dev->video_q.curr->top_seen = 1;
2366 if (!dev->video_q.curr->top_seen)
2368 } else if (field == V4L2_FIELD_TOP) {
2369 if ((status & 0x10) != 0x10)
2371 } else if (field == V4L2_FIELD_BOTTOM) {
2372 if ((status & 0x10) != 0x00)
2375 dev->video_q.curr->vb.field_count = dev->video_fieldcount;
2376 saa7134_buffer_finish(dev,&dev->video_q,STATE_DONE);
2378 saa7134_buffer_next(dev,&dev->video_q);
2381 spin_unlock(&dev->slock);
2384 /* ----------------------------------------------------------- */