2 * $Id: saa7134-video.c,v 1.25 2004/12/10 12:33:39 kraxel Exp $
4 * device driver for philips saa7134 based TV cards
5 * video4linux video interface
7 * (c) 2001-03 Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include <linux/init.h>
25 #include <linux/list.h>
26 #include <linux/module.h>
27 #include <linux/moduleparam.h>
28 #include <linux/kernel.h>
29 #include <linux/slab.h>
31 #include "saa7134-reg.h"
34 #define V4L2_I2C_CLIENTS 1
36 /* ------------------------------------------------------------------ */
38 static unsigned int video_debug = 0;
39 static unsigned int gbuffers = 8;
40 static unsigned int noninterlaced = 0;
41 static unsigned int gbufsize = 720*576*4;
42 static unsigned int gbufsize_max = 720*576*4;
43 module_param(video_debug, int, 0644);
44 MODULE_PARM_DESC(video_debug,"enable debug messages [video]");
45 module_param(gbuffers, int, 0444);
46 MODULE_PARM_DESC(gbuffers,"number of capture buffers, range 2-32");
47 module_param(noninterlaced, int, 0644);
48 MODULE_PARM_DESC(noninterlaced,"video input is noninterlaced");
50 #define dprintk(fmt, arg...) if (video_debug) \
51 printk(KERN_DEBUG "%s/video: " fmt, dev->name , ## arg)
53 /* ------------------------------------------------------------------ */
54 /* data structs for video */
56 static int video_out[][9] = {
57 [CCIR656] = { 0x00, 0xb1, 0x00, 0xa1, 0x00, 0x04, 0x06, 0x00, 0x00 },
60 static struct saa7134_format formats[] = {
63 .fourcc = V4L2_PIX_FMT_GREY,
67 .name = "15 bpp RGB, le",
68 .fourcc = V4L2_PIX_FMT_RGB555,
72 .name = "15 bpp RGB, be",
73 .fourcc = V4L2_PIX_FMT_RGB555X,
78 .name = "16 bpp RGB, le",
79 .fourcc = V4L2_PIX_FMT_RGB565,
83 .name = "16 bpp RGB, be",
84 .fourcc = V4L2_PIX_FMT_RGB565X,
89 .name = "24 bpp RGB, le",
90 .fourcc = V4L2_PIX_FMT_BGR24,
94 .name = "24 bpp RGB, be",
95 .fourcc = V4L2_PIX_FMT_RGB24,
100 .name = "32 bpp RGB, le",
101 .fourcc = V4L2_PIX_FMT_BGR32,
105 .name = "32 bpp RGB, be",
106 .fourcc = V4L2_PIX_FMT_RGB32,
112 .name = "4:2:2 packed, YUYV",
113 .fourcc = V4L2_PIX_FMT_YUYV,
119 .name = "4:2:2 packed, UYVY",
120 .fourcc = V4L2_PIX_FMT_UYVY,
125 .name = "4:2:2 planar, Y-Cb-Cr",
126 .fourcc = V4L2_PIX_FMT_YUV422P,
134 .name = "4:2:0 planar, Y-Cb-Cr",
135 .fourcc = V4L2_PIX_FMT_YUV420,
143 .name = "4:2:0 planar, Y-Cb-Cr",
144 .fourcc = V4L2_PIX_FMT_YVU420,
154 #define FORMATS ARRAY_SIZE(formats)
156 #define NORM_625_50 \
159 .video_v_start = 24, \
160 .video_v_stop = 311, \
165 #define NORM_525_60 \
168 .video_v_start = 22, \
169 .video_v_stop = 22+239, \
170 .vbi_v_start = 10, /* FIXME */ \
171 .vbi_v_stop = 21, /* FIXME */ \
174 static struct saa7134_tvnorm tvnorms[] = {
176 .name = "PAL", /* autodetect */
180 .sync_control = 0x18,
181 .luma_control = 0x40,
182 .chroma_ctrl1 = 0x81,
184 .chroma_ctrl2 = 0x06,
189 .id = V4L2_STD_PAL_BG,
192 .sync_control = 0x18,
193 .luma_control = 0x40,
194 .chroma_ctrl1 = 0x81,
196 .chroma_ctrl2 = 0x06,
201 .id = V4L2_STD_PAL_I,
204 .sync_control = 0x18,
205 .luma_control = 0x40,
206 .chroma_ctrl1 = 0x81,
208 .chroma_ctrl2 = 0x06,
213 .id = V4L2_STD_PAL_DK,
216 .sync_control = 0x18,
217 .luma_control = 0x40,
218 .chroma_ctrl1 = 0x81,
220 .chroma_ctrl2 = 0x06,
228 .sync_control = 0x59,
229 .luma_control = 0x40,
230 .chroma_ctrl1 = 0x89,
232 .chroma_ctrl2 = 0x0e,
237 .id = V4L2_STD_SECAM,
240 .sync_control = 0x18, /* old: 0x58, */
241 .luma_control = 0x1b,
242 .chroma_ctrl1 = 0xd1,
244 .chroma_ctrl2 = 0x00,
249 .id = V4L2_STD_PAL_M,
252 .sync_control = 0x59,
253 .luma_control = 0x40,
254 .chroma_ctrl1 = 0xb9,
256 .chroma_ctrl2 = 0x0e,
261 .id = V4L2_STD_PAL_Nc,
264 .sync_control = 0x18,
265 .luma_control = 0x40,
266 .chroma_ctrl1 = 0xa1,
268 .chroma_ctrl2 = 0x06,
273 #define TVNORMS ARRAY_SIZE(tvnorms)
275 #define V4L2_CID_PRIVATE_INVERT (V4L2_CID_PRIVATE_BASE + 0)
276 #define V4L2_CID_PRIVATE_Y_ODD (V4L2_CID_PRIVATE_BASE + 1)
277 #define V4L2_CID_PRIVATE_Y_EVEN (V4L2_CID_PRIVATE_BASE + 2)
278 #define V4L2_CID_PRIVATE_AUTOMUTE (V4L2_CID_PRIVATE_BASE + 3)
279 #define V4L2_CID_PRIVATE_LASTP1 (V4L2_CID_PRIVATE_BASE + 4)
281 static const struct v4l2_queryctrl no_ctrl = {
283 .flags = V4L2_CTRL_FLAG_DISABLED,
285 static const struct v4l2_queryctrl video_ctrls[] = {
288 .id = V4L2_CID_BRIGHTNESS,
289 .name = "Brightness",
293 .default_value = 128,
294 .type = V4L2_CTRL_TYPE_INTEGER,
296 .id = V4L2_CID_CONTRAST,
302 .type = V4L2_CTRL_TYPE_INTEGER,
304 .id = V4L2_CID_SATURATION,
305 .name = "Saturation",
310 .type = V4L2_CTRL_TYPE_INTEGER,
318 .type = V4L2_CTRL_TYPE_INTEGER,
320 .id = V4L2_CID_VFLIP,
321 .name = "vertical flip",
324 .type = V4L2_CTRL_TYPE_BOOLEAN,
328 .id = V4L2_CID_AUDIO_MUTE,
332 .type = V4L2_CTRL_TYPE_BOOLEAN,
334 .id = V4L2_CID_AUDIO_VOLUME,
340 .type = V4L2_CTRL_TYPE_INTEGER,
342 /* --- private --- */
344 .id = V4L2_CID_PRIVATE_INVERT,
348 .type = V4L2_CTRL_TYPE_BOOLEAN,
350 .id = V4L2_CID_PRIVATE_Y_ODD,
351 .name = "y offset odd field",
355 .type = V4L2_CTRL_TYPE_INTEGER,
357 .id = V4L2_CID_PRIVATE_Y_EVEN,
358 .name = "y offset even field",
362 .type = V4L2_CTRL_TYPE_INTEGER,
364 .id = V4L2_CID_PRIVATE_AUTOMUTE,
369 .type = V4L2_CTRL_TYPE_BOOLEAN,
372 static const unsigned int CTRLS = ARRAY_SIZE(video_ctrls);
374 static const struct v4l2_queryctrl* ctrl_by_id(unsigned int id)
378 for (i = 0; i < CTRLS; i++)
379 if (video_ctrls[i].id == id)
380 return video_ctrls+i;
384 static struct saa7134_format* format_by_fourcc(unsigned int fourcc)
388 for (i = 0; i < FORMATS; i++)
389 if (formats[i].fourcc == fourcc)
394 /* ----------------------------------------------------------------------- */
395 /* resource management */
397 static int res_get(struct saa7134_dev *dev, struct saa7134_fh *fh, unsigned int bit)
399 if (fh->resources & bit)
400 /* have it already allocated */
405 if (dev->resources & bit) {
406 /* no, someone else uses it */
410 /* it's free, grab it */
411 fh->resources |= bit;
412 dev->resources |= bit;
413 dprintk("res: get %d\n",bit);
419 int res_check(struct saa7134_fh *fh, unsigned int bit)
421 return (fh->resources & bit);
425 int res_locked(struct saa7134_dev *dev, unsigned int bit)
427 return (dev->resources & bit);
431 void res_free(struct saa7134_dev *dev, struct saa7134_fh *fh, unsigned int bits)
433 if ((fh->resources & bits) != bits)
437 fh->resources &= ~bits;
438 dev->resources &= ~bits;
439 dprintk("res: put %d\n",bits);
443 /* ------------------------------------------------------------------ */
445 static void set_tvnorm(struct saa7134_dev *dev, struct saa7134_tvnorm *norm)
447 int luma_control,sync_control,mux;
449 dprintk("set tv norm = %s\n",norm->name);
452 mux = card_in(dev,dev->ctl_input).vmux;
453 luma_control = norm->luma_control;
454 sync_control = norm->sync_control;
457 luma_control |= 0x80; /* svideo */
458 if (noninterlaced || dev->nosignal)
459 sync_control |= 0x20;
462 dev->crop_bounds.left = norm->h_start;
463 dev->crop_defrect.left = norm->h_start;
464 dev->crop_bounds.width = norm->h_stop - norm->h_start +1;
465 dev->crop_defrect.width = norm->h_stop - norm->h_start +1;
467 dev->crop_bounds.top = (norm->vbi_v_stop+1)*2;
468 dev->crop_defrect.top = norm->video_v_start*2;
469 dev->crop_bounds.height = ((norm->id & V4L2_STD_525_60) ? 524 : 624)
470 - dev->crop_bounds.top;
471 dev->crop_defrect.height = (norm->video_v_stop - norm->video_v_start +1)*2;
473 dev->crop_current = dev->crop_defrect;
475 /* setup video decoder */
476 saa_writeb(SAA7134_INCR_DELAY, 0x08);
477 saa_writeb(SAA7134_ANALOG_IN_CTRL1, 0xc0 | mux);
478 saa_writeb(SAA7134_ANALOG_IN_CTRL2, 0x00);
480 saa_writeb(SAA7134_ANALOG_IN_CTRL3, 0x90);
481 saa_writeb(SAA7134_ANALOG_IN_CTRL4, 0x90);
482 saa_writeb(SAA7134_HSYNC_START, 0xeb);
483 saa_writeb(SAA7134_HSYNC_STOP, 0xe0);
484 saa_writeb(SAA7134_SOURCE_TIMING1, norm->src_timing);
486 saa_writeb(SAA7134_SYNC_CTRL, sync_control);
487 saa_writeb(SAA7134_LUMA_CTRL, luma_control);
488 saa_writeb(SAA7134_DEC_LUMA_BRIGHT, dev->ctl_bright);
489 saa_writeb(SAA7134_DEC_LUMA_CONTRAST, dev->ctl_contrast);
491 saa_writeb(SAA7134_DEC_CHROMA_SATURATION, dev->ctl_saturation);
492 saa_writeb(SAA7134_DEC_CHROMA_HUE, dev->ctl_hue);
493 saa_writeb(SAA7134_CHROMA_CTRL1, norm->chroma_ctrl1);
494 saa_writeb(SAA7134_CHROMA_GAIN, norm->chroma_gain);
496 saa_writeb(SAA7134_CHROMA_CTRL2, norm->chroma_ctrl2);
497 saa_writeb(SAA7134_MODE_DELAY_CTRL, 0x00);
499 saa_writeb(SAA7134_ANALOG_ADC, 0x01);
500 saa_writeb(SAA7134_VGATE_START, 0x11);
501 saa_writeb(SAA7134_VGATE_STOP, 0xfe);
502 saa_writeb(SAA7134_MISC_VGATE_MSB, norm->vgate_misc);
503 saa_writeb(SAA7134_RAW_DATA_GAIN, 0x40);
504 saa_writeb(SAA7134_RAW_DATA_OFFSET, 0x80);
506 #ifdef V4L2_I2C_CLIENTS
507 saa7134_i2c_call_clients(dev,VIDIOC_S_STD,&norm->id);
510 /* pass down info to the i2c chips (v4l1) */
511 struct video_channel c;
512 memset(&c,0,sizeof(c));
513 c.channel = dev->ctl_input;
514 c.norm = VIDEO_MODE_PAL;
515 if (norm->id & V4L2_STD_NTSC)
516 c.norm = VIDEO_MODE_NTSC;
517 if (norm->id & V4L2_STD_SECAM)
518 c.norm = VIDEO_MODE_SECAM;
519 saa7134_i2c_call_clients(dev,VIDIOCSCHAN,&c);
524 static void video_mux(struct saa7134_dev *dev, int input)
526 dprintk("video input = %d [%s]\n",input,card_in(dev,input).name);
527 dev->ctl_input = input;
528 set_tvnorm(dev,dev->tvnorm);
529 saa7134_tvaudio_setinput(dev,&card_in(dev,input));
532 static void set_h_prescale(struct saa7134_dev *dev, int task, int prescale)
534 static const struct {
541 /* XPSC XACL XC2_1 XDCG VPFY */
553 static const int count = ARRAY_SIZE(vals);
556 for (i = 0; i < count; i++)
557 if (vals[i].xpsc == prescale)
562 saa_writeb(SAA7134_H_PRESCALE(task), vals[i].xpsc);
563 saa_writeb(SAA7134_ACC_LENGTH(task), vals[i].xacl);
564 saa_writeb(SAA7134_LEVEL_CTRL(task),
565 (vals[i].xc2_1 << 3) | (vals[i].xdcg));
566 saa_andorb(SAA7134_FIR_PREFILTER_CTRL(task), 0x0f,
567 (vals[i].vpfy << 2) | vals[i].vpfy);
570 static void set_v_scale(struct saa7134_dev *dev, int task, int yscale)
574 saa_writeb(SAA7134_V_SCALE_RATIO1(task), yscale & 0xff);
575 saa_writeb(SAA7134_V_SCALE_RATIO2(task), yscale >> 8);
577 mirror = (dev->ctl_mirror) ? 0x02 : 0x00;
580 dprintk("yscale LPI yscale=%d\n",yscale);
581 saa_writeb(SAA7134_V_FILTER(task), 0x00 | mirror);
582 saa_writeb(SAA7134_LUMA_CONTRAST(task), 0x40);
583 saa_writeb(SAA7134_CHROMA_SATURATION(task), 0x40);
586 val = 0x40 * 1024 / yscale;
587 dprintk("yscale ACM yscale=%d val=0x%x\n",yscale,val);
588 saa_writeb(SAA7134_V_FILTER(task), 0x01 | mirror);
589 saa_writeb(SAA7134_LUMA_CONTRAST(task), val);
590 saa_writeb(SAA7134_CHROMA_SATURATION(task), val);
592 saa_writeb(SAA7134_LUMA_BRIGHT(task), 0x80);
595 static void set_size(struct saa7134_dev *dev, int task,
596 int width, int height, int interlace)
598 int prescale,xscale,yscale,y_even,y_odd;
599 int h_start, h_stop, v_start, v_stop;
600 int div = interlace ? 2 : 1;
602 /* setup video scaler */
603 h_start = dev->crop_current.left;
604 v_start = dev->crop_current.top/2;
605 h_stop = (dev->crop_current.left + dev->crop_current.width -1);
606 v_stop = (dev->crop_current.top + dev->crop_current.height -1)/2;
608 saa_writeb(SAA7134_VIDEO_H_START1(task), h_start & 0xff);
609 saa_writeb(SAA7134_VIDEO_H_START2(task), h_start >> 8);
610 saa_writeb(SAA7134_VIDEO_H_STOP1(task), h_stop & 0xff);
611 saa_writeb(SAA7134_VIDEO_H_STOP2(task), h_stop >> 8);
612 saa_writeb(SAA7134_VIDEO_V_START1(task), v_start & 0xff);
613 saa_writeb(SAA7134_VIDEO_V_START2(task), v_start >> 8);
614 saa_writeb(SAA7134_VIDEO_V_STOP1(task), v_stop & 0xff);
615 saa_writeb(SAA7134_VIDEO_V_STOP2(task), v_stop >> 8);
617 prescale = dev->crop_current.width / width;
620 xscale = 1024 * dev->crop_current.width / prescale / width;
621 yscale = 512 * div * dev->crop_current.height / height;
622 dprintk("prescale=%d xscale=%d yscale=%d\n",prescale,xscale,yscale);
623 set_h_prescale(dev,task,prescale);
624 saa_writeb(SAA7134_H_SCALE_INC1(task), xscale & 0xff);
625 saa_writeb(SAA7134_H_SCALE_INC2(task), xscale >> 8);
626 set_v_scale(dev,task,yscale);
628 saa_writeb(SAA7134_VIDEO_PIXELS1(task), width & 0xff);
629 saa_writeb(SAA7134_VIDEO_PIXELS2(task), width >> 8);
630 saa_writeb(SAA7134_VIDEO_LINES1(task), height/div & 0xff);
631 saa_writeb(SAA7134_VIDEO_LINES2(task), height/div >> 8);
633 /* deinterlace y offsets */
634 y_odd = dev->ctl_y_odd;
635 y_even = dev->ctl_y_even;
636 saa_writeb(SAA7134_V_PHASE_OFFSET0(task), y_odd);
637 saa_writeb(SAA7134_V_PHASE_OFFSET1(task), y_even);
638 saa_writeb(SAA7134_V_PHASE_OFFSET2(task), y_odd);
639 saa_writeb(SAA7134_V_PHASE_OFFSET3(task), y_even);
642 /* ------------------------------------------------------------------ */
650 static void sort_cliplist(struct cliplist *cl, int entries)
652 struct cliplist swap;
655 for (i = entries-2; i >= 0; i--) {
656 for (n = 0, j = 0; j <= i; j++) {
657 if (cl[j].position > cl[j+1].position) {
669 static void set_cliplist(struct saa7134_dev *dev, int reg,
670 struct cliplist *cl, int entries, char *name)
675 for (i = 0; i < entries; i++) {
676 winbits |= cl[i].enable;
677 winbits &= ~cl[i].disable;
678 if (i < 15 && cl[i].position == cl[i+1].position)
680 saa_writeb(reg + 0, winbits);
681 saa_writeb(reg + 2, cl[i].position & 0xff);
682 saa_writeb(reg + 3, cl[i].position >> 8);
683 dprintk("clip: %s winbits=%02x pos=%d\n",
684 name,winbits,cl[i].position);
687 for (; reg < 0x400; reg += 8) {
688 saa_writeb(reg+ 0, 0);
689 saa_writeb(reg + 1, 0);
690 saa_writeb(reg + 2, 0);
691 saa_writeb(reg + 3, 0);
695 static int clip_range(int val)
702 static int setup_clipping(struct saa7134_dev *dev, struct v4l2_clip *clips,
703 int nclips, int interlace)
705 struct cliplist col[16], row[16];
707 int div = interlace ? 2 : 1;
709 memset(col,0,sizeof(col)); cols = 0;
710 memset(row,0,sizeof(row)); rows = 0;
711 for (i = 0; i < nclips && i < 8; i++) {
712 col[cols].position = clip_range(clips[i].c.left);
713 col[cols].enable = (1 << i);
715 col[cols].position = clip_range(clips[i].c.left+clips[i].c.width);
716 col[cols].disable = (1 << i);
718 row[rows].position = clip_range(clips[i].c.top / div);
719 row[rows].enable = (1 << i);
721 row[rows].position = clip_range((clips[i].c.top + clips[i].c.height)
723 row[rows].disable = (1 << i);
726 sort_cliplist(col,cols);
727 sort_cliplist(row,rows);
728 set_cliplist(dev,0x380,col,cols,"cols");
729 set_cliplist(dev,0x384,row,rows,"rows");
733 static int verify_preview(struct saa7134_dev *dev, struct v4l2_window *win)
735 enum v4l2_field field;
738 if (NULL == dev->ovbuf.base)
740 if (NULL == dev->ovfmt)
742 if (win->w.width < 48 || win->w.height < 32)
744 if (win->clipcount > 2048)
748 maxw = dev->crop_current.width;
749 maxh = dev->crop_current.height;
751 if (V4L2_FIELD_ANY == field) {
752 field = (win->w.height > maxh/2)
753 ? V4L2_FIELD_INTERLACED
758 case V4L2_FIELD_BOTTOM:
761 case V4L2_FIELD_INTERLACED:
768 if (win->w.width > maxw)
770 if (win->w.height > maxh)
771 win->w.height = maxh;
775 static int start_preview(struct saa7134_dev *dev, struct saa7134_fh *fh)
777 unsigned long base,control,bpl;
780 err = verify_preview(dev,&fh->win);
784 dev->ovfield = fh->win.field;
785 dprintk("start_preview %dx%d+%d+%d %s field=%s\n",
786 fh->win.w.width,fh->win.w.height,
787 fh->win.w.left,fh->win.w.top,
788 dev->ovfmt->name,v4l2_field_names[dev->ovfield]);
790 /* setup window + clipping */
791 set_size(dev,TASK_B,fh->win.w.width,fh->win.w.height,
792 V4L2_FIELD_HAS_BOTH(dev->ovfield));
793 setup_clipping(dev,fh->clips,fh->nclips,
794 V4L2_FIELD_HAS_BOTH(dev->ovfield));
796 saa_andorb(SAA7134_DATA_PATH(TASK_B), 0x3f, 0x03);
798 saa_andorb(SAA7134_DATA_PATH(TASK_B), 0x3f, 0x01);
799 saa_writeb(SAA7134_OFMT_VIDEO_B, dev->ovfmt->pm | 0x20);
801 /* dma: setup channel 1 (= Video Task B) */
802 base = (unsigned long)dev->ovbuf.base;
803 base += dev->ovbuf.fmt.bytesperline * fh->win.w.top;
804 base += dev->ovfmt->depth/8 * fh->win.w.left;
805 bpl = dev->ovbuf.fmt.bytesperline;
806 control = SAA7134_RS_CONTROL_BURST_16;
807 if (dev->ovfmt->bswap)
808 control |= SAA7134_RS_CONTROL_BSWAP;
809 if (dev->ovfmt->wswap)
810 control |= SAA7134_RS_CONTROL_WSWAP;
811 if (V4L2_FIELD_HAS_BOTH(dev->ovfield)) {
812 saa_writel(SAA7134_RS_BA1(1),base);
813 saa_writel(SAA7134_RS_BA2(1),base+bpl);
814 saa_writel(SAA7134_RS_PITCH(1),bpl*2);
815 saa_writel(SAA7134_RS_CONTROL(1),control);
817 saa_writel(SAA7134_RS_BA1(1),base);
818 saa_writel(SAA7134_RS_BA2(1),base);
819 saa_writel(SAA7134_RS_PITCH(1),bpl);
820 saa_writel(SAA7134_RS_CONTROL(1),control);
825 saa7134_set_dmabits(dev);
830 static int stop_preview(struct saa7134_dev *dev, struct saa7134_fh *fh)
833 saa7134_set_dmabits(dev);
837 /* ------------------------------------------------------------------ */
839 static int buffer_activate(struct saa7134_dev *dev,
840 struct saa7134_buf *buf,
841 struct saa7134_buf *next)
843 unsigned long base,control,bpl;
844 unsigned long bpl_uv,lines_uv,base2,base3,tmp; /* planar */
846 dprintk("buffer_activate buf=%p\n",buf);
847 buf->vb.state = STATE_ACTIVE;
850 set_size(dev,TASK_A,buf->vb.width,buf->vb.height,
851 V4L2_FIELD_HAS_BOTH(buf->vb.field));
853 saa_andorb(SAA7134_DATA_PATH(TASK_A), 0x3f, 0x03);
855 saa_andorb(SAA7134_DATA_PATH(TASK_A), 0x3f, 0x01);
856 saa_writeb(SAA7134_OFMT_VIDEO_A, buf->fmt->pm);
858 /* DMA: setup channel 0 (= Video Task A0) */
859 base = saa7134_buffer_base(buf);
860 if (buf->fmt->planar)
863 bpl = (buf->vb.width * buf->fmt->depth) / 8;
864 control = SAA7134_RS_CONTROL_BURST_16 |
865 SAA7134_RS_CONTROL_ME |
866 (buf->pt->dma >> 12);
868 control |= SAA7134_RS_CONTROL_BSWAP;
870 control |= SAA7134_RS_CONTROL_WSWAP;
871 if (V4L2_FIELD_HAS_BOTH(buf->vb.field)) {
873 saa_writel(SAA7134_RS_BA1(0),base);
874 saa_writel(SAA7134_RS_BA2(0),base+bpl);
875 saa_writel(SAA7134_RS_PITCH(0),bpl*2);
878 saa_writel(SAA7134_RS_BA1(0),base);
879 saa_writel(SAA7134_RS_BA2(0),base);
880 saa_writel(SAA7134_RS_PITCH(0),bpl);
882 saa_writel(SAA7134_RS_CONTROL(0),control);
884 if (buf->fmt->planar) {
885 /* DMA: setup channel 4+5 (= planar task A) */
886 bpl_uv = bpl >> buf->fmt->hshift;
887 lines_uv = buf->vb.height >> buf->fmt->vshift;
888 base2 = base + bpl * buf->vb.height;
889 base3 = base2 + bpl_uv * lines_uv;
890 if (buf->fmt->uvswap)
891 tmp = base2, base2 = base3, base3 = tmp;
892 dprintk("uv: bpl=%ld lines=%ld base2/3=%ld/%ld\n",
893 bpl_uv,lines_uv,base2,base3);
894 if (V4L2_FIELD_HAS_BOTH(buf->vb.field)) {
896 saa_writel(SAA7134_RS_BA1(4),base2);
897 saa_writel(SAA7134_RS_BA2(4),base2+bpl_uv);
898 saa_writel(SAA7134_RS_PITCH(4),bpl_uv*2);
899 saa_writel(SAA7134_RS_BA1(5),base3);
900 saa_writel(SAA7134_RS_BA2(5),base3+bpl_uv);
901 saa_writel(SAA7134_RS_PITCH(5),bpl_uv*2);
904 saa_writel(SAA7134_RS_BA1(4),base2);
905 saa_writel(SAA7134_RS_BA2(4),base2);
906 saa_writel(SAA7134_RS_PITCH(4),bpl_uv);
907 saa_writel(SAA7134_RS_BA1(5),base3);
908 saa_writel(SAA7134_RS_BA2(5),base3);
909 saa_writel(SAA7134_RS_PITCH(5),bpl_uv);
911 saa_writel(SAA7134_RS_CONTROL(4),control);
912 saa_writel(SAA7134_RS_CONTROL(5),control);
916 saa7134_set_dmabits(dev);
917 mod_timer(&dev->video_q.timeout, jiffies+BUFFER_TIMEOUT);
921 static int buffer_prepare(struct videobuf_queue *q,
922 struct videobuf_buffer *vb,
923 enum v4l2_field field)
925 struct saa7134_fh *fh = q->priv_data;
926 struct saa7134_dev *dev = fh->dev;
927 struct saa7134_buf *buf = container_of(vb,struct saa7134_buf,vb);
934 if (fh->width < 48 ||
936 fh->width/4 > dev->crop_current.width ||
937 fh->height/4 > dev->crop_current.height ||
938 fh->width > dev->crop_bounds.width ||
939 fh->height > dev->crop_bounds.height)
941 size = (fh->width * fh->height * fh->fmt->depth) >> 3;
942 if (0 != buf->vb.baddr && buf->vb.bsize < size)
945 dprintk("buffer_prepare [%d,size=%dx%d,bytes=%d,fields=%s,%s]\n",
946 vb->i,fh->width,fh->height,size,v4l2_field_names[field],
948 if (buf->vb.width != fh->width ||
949 buf->vb.height != fh->height ||
950 buf->vb.size != size ||
951 buf->vb.field != field ||
952 buf->fmt != fh->fmt) {
953 saa7134_dma_free(dev,buf);
956 if (STATE_NEEDS_INIT == buf->vb.state) {
957 buf->vb.width = fh->width;
958 buf->vb.height = fh->height;
960 buf->vb.field = field;
962 buf->pt = &fh->pt_cap;
964 err = videobuf_iolock(dev->pci,&buf->vb,&dev->ovbuf);
967 err = saa7134_pgtable_build(dev->pci,buf->pt,
970 saa7134_buffer_startpage(buf));
974 buf->vb.state = STATE_PREPARED;
975 buf->activate = buffer_activate;
979 saa7134_dma_free(dev,buf);
984 buffer_setup(struct videobuf_queue *q, unsigned int *count, unsigned int *size)
986 struct saa7134_fh *fh = q->priv_data;
988 *size = fh->fmt->depth * fh->width * fh->height >> 3;
991 *count = saa7134_buffer_count(*size,*count);
995 static void buffer_queue(struct videobuf_queue *q, struct videobuf_buffer *vb)
997 struct saa7134_fh *fh = q->priv_data;
998 struct saa7134_buf *buf = container_of(vb,struct saa7134_buf,vb);
1000 saa7134_buffer_queue(fh->dev,&fh->dev->video_q,buf);
1003 static void buffer_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
1005 struct saa7134_fh *fh = q->priv_data;
1006 struct saa7134_buf *buf = container_of(vb,struct saa7134_buf,vb);
1008 saa7134_dma_free(fh->dev,buf);
1011 static struct videobuf_queue_ops video_qops = {
1012 .buf_setup = buffer_setup,
1013 .buf_prepare = buffer_prepare,
1014 .buf_queue = buffer_queue,
1015 .buf_release = buffer_release,
1018 /* ------------------------------------------------------------------ */
1020 static int get_control(struct saa7134_dev *dev, struct v4l2_control *c)
1022 const struct v4l2_queryctrl* ctrl;
1024 ctrl = ctrl_by_id(c->id);
1028 case V4L2_CID_BRIGHTNESS:
1029 c->value = dev->ctl_bright;
1032 c->value = dev->ctl_hue;
1034 case V4L2_CID_CONTRAST:
1035 c->value = dev->ctl_contrast;
1037 case V4L2_CID_SATURATION:
1038 c->value = dev->ctl_saturation;
1040 case V4L2_CID_AUDIO_MUTE:
1041 c->value = dev->ctl_mute;
1043 case V4L2_CID_AUDIO_VOLUME:
1044 c->value = dev->ctl_volume;
1046 case V4L2_CID_PRIVATE_INVERT:
1047 c->value = dev->ctl_invert;
1049 case V4L2_CID_VFLIP:
1050 c->value = dev->ctl_mirror;
1052 case V4L2_CID_PRIVATE_Y_EVEN:
1053 c->value = dev->ctl_y_even;
1055 case V4L2_CID_PRIVATE_Y_ODD:
1056 c->value = dev->ctl_y_odd;
1058 case V4L2_CID_PRIVATE_AUTOMUTE:
1059 c->value = dev->ctl_automute;
1067 static int set_control(struct saa7134_dev *dev, struct saa7134_fh *fh,
1068 struct v4l2_control *c)
1070 const struct v4l2_queryctrl* ctrl;
1071 unsigned long flags;
1072 int restart_overlay = 0;
1074 ctrl = ctrl_by_id(c->id);
1077 dprintk("set_control name=%s val=%d\n",ctrl->name,c->value);
1078 switch (ctrl->type) {
1079 case V4L2_CTRL_TYPE_BOOLEAN:
1080 case V4L2_CTRL_TYPE_MENU:
1081 case V4L2_CTRL_TYPE_INTEGER:
1082 if (c->value < ctrl->minimum)
1083 c->value = ctrl->minimum;
1084 if (c->value > ctrl->maximum)
1085 c->value = ctrl->maximum;
1091 case V4L2_CID_BRIGHTNESS:
1092 dev->ctl_bright = c->value;
1093 saa_writeb(SAA7134_DEC_LUMA_BRIGHT, dev->ctl_bright);
1096 dev->ctl_hue = c->value;
1097 saa_writeb(SAA7134_DEC_CHROMA_HUE, dev->ctl_hue);
1099 case V4L2_CID_CONTRAST:
1100 dev->ctl_contrast = c->value;
1101 saa_writeb(SAA7134_DEC_LUMA_CONTRAST,
1102 dev->ctl_invert ? -dev->ctl_contrast : dev->ctl_contrast);
1104 case V4L2_CID_SATURATION:
1105 dev->ctl_saturation = c->value;
1106 saa_writeb(SAA7134_DEC_CHROMA_SATURATION,
1107 dev->ctl_invert ? -dev->ctl_saturation : dev->ctl_saturation);
1109 case V4L2_CID_AUDIO_MUTE:
1110 dev->ctl_mute = c->value;
1111 saa7134_tvaudio_setmute(dev);
1113 case V4L2_CID_AUDIO_VOLUME:
1114 dev->ctl_volume = c->value;
1115 saa7134_tvaudio_setvolume(dev,dev->ctl_volume);
1117 case V4L2_CID_PRIVATE_INVERT:
1118 dev->ctl_invert = c->value;
1119 saa_writeb(SAA7134_DEC_LUMA_CONTRAST,
1120 dev->ctl_invert ? -dev->ctl_contrast : dev->ctl_contrast);
1121 saa_writeb(SAA7134_DEC_CHROMA_SATURATION,
1122 dev->ctl_invert ? -dev->ctl_saturation : dev->ctl_saturation);
1124 case V4L2_CID_VFLIP:
1125 dev->ctl_mirror = c->value;
1126 restart_overlay = 1;
1128 case V4L2_CID_PRIVATE_Y_EVEN:
1129 dev->ctl_y_even = c->value;
1130 restart_overlay = 1;
1132 case V4L2_CID_PRIVATE_Y_ODD:
1133 dev->ctl_y_odd = c->value;
1134 restart_overlay = 1;
1136 case V4L2_CID_PRIVATE_AUTOMUTE:
1137 dev->ctl_automute = c->value;
1138 if (dev->tda9887_conf) {
1139 if (dev->ctl_automute)
1140 dev->tda9887_conf |= TDA9887_AUTOMUTE;
1142 dev->tda9887_conf &= ~TDA9887_AUTOMUTE;
1143 saa7134_i2c_call_clients(dev, TDA9887_SET_CONFIG,
1144 &dev->tda9887_conf);
1150 if (restart_overlay && fh && res_check(fh, RESOURCE_OVERLAY)) {
1151 spin_lock_irqsave(&dev->slock,flags);
1152 stop_preview(dev,fh);
1153 start_preview(dev,fh);
1154 spin_unlock_irqrestore(&dev->slock,flags);
1159 /* ------------------------------------------------------------------ */
1161 static struct videobuf_queue* saa7134_queue(struct saa7134_fh *fh)
1163 struct videobuf_queue* q = NULL;
1166 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1169 case V4L2_BUF_TYPE_VBI_CAPTURE:
1178 static int saa7134_resource(struct saa7134_fh *fh)
1183 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1184 res = RESOURCE_VIDEO;
1186 case V4L2_BUF_TYPE_VBI_CAPTURE:
1195 static int video_open(struct inode *inode, struct file *file)
1197 int minor = iminor(inode);
1198 struct saa7134_dev *h,*dev = NULL;
1199 struct saa7134_fh *fh;
1200 struct list_head *list;
1201 enum v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1204 list_for_each(list,&saa7134_devlist) {
1205 h = list_entry(list, struct saa7134_dev, devlist);
1206 if (h->video_dev && (h->video_dev->minor == minor))
1208 if (h->radio_dev && (h->radio_dev->minor == minor)) {
1212 if (h->vbi_dev && (h->vbi_dev->minor == minor)) {
1213 type = V4L2_BUF_TYPE_VBI_CAPTURE;
1220 dprintk("open minor=%d radio=%d type=%s\n",minor,radio,
1221 v4l2_type_names[type]);
1223 /* allocate + initialize per filehandle data */
1224 fh = kmalloc(sizeof(*fh),GFP_KERNEL);
1227 memset(fh,0,sizeof(*fh));
1228 file->private_data = fh;
1232 fh->fmt = format_by_fourcc(V4L2_PIX_FMT_BGR24);
1235 v4l2_prio_open(&dev->prio,&fh->prio);
1237 videobuf_queue_init(&fh->cap, &video_qops,
1238 dev->pci, &dev->slock,
1239 V4L2_BUF_TYPE_VIDEO_CAPTURE,
1240 V4L2_FIELD_INTERLACED,
1241 sizeof(struct saa7134_buf),
1243 videobuf_queue_init(&fh->vbi, &saa7134_vbi_qops,
1244 dev->pci, &dev->slock,
1245 V4L2_BUF_TYPE_VBI_CAPTURE,
1247 sizeof(struct saa7134_buf),
1249 saa7134_pgtable_alloc(dev->pci,&fh->pt_cap);
1250 saa7134_pgtable_alloc(dev->pci,&fh->pt_vbi);
1253 /* switch to radio mode */
1254 saa7134_tvaudio_setinput(dev,&card(dev).radio);
1255 saa7134_i2c_call_clients(dev,AUDC_SET_RADIO,NULL);
1257 /* switch to video/vbi mode */
1258 video_mux(dev,dev->ctl_input);
1264 video_read(struct file *file, char __user *data, size_t count, loff_t *ppos)
1266 struct saa7134_fh *fh = file->private_data;
1269 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1270 if (res_locked(fh->dev,RESOURCE_VIDEO))
1272 return videobuf_read_one(saa7134_queue(fh),
1274 file->f_flags & O_NONBLOCK);
1275 case V4L2_BUF_TYPE_VBI_CAPTURE:
1276 if (!res_get(fh->dev,fh,RESOURCE_VBI))
1278 return videobuf_read_stream(saa7134_queue(fh),
1279 data, count, ppos, 1,
1280 file->f_flags & O_NONBLOCK);
1289 video_poll(struct file *file, struct poll_table_struct *wait)
1291 struct saa7134_fh *fh = file->private_data;
1292 struct videobuf_buffer *buf = NULL;
1294 if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type)
1295 return videobuf_poll_stream(file, &fh->vbi, wait);
1297 if (res_check(fh,RESOURCE_VIDEO)) {
1298 if (!list_empty(&fh->cap.stream))
1299 buf = list_entry(fh->cap.stream.next, struct videobuf_buffer, stream);
1301 down(&fh->cap.lock);
1302 if (UNSET == fh->cap.read_off) {
1303 /* need to capture a new frame */
1304 if (res_locked(fh->dev,RESOURCE_VIDEO)) {
1308 if (0 != fh->cap.ops->buf_prepare(&fh->cap,fh->cap.read_buf,fh->cap.field)) {
1312 fh->cap.ops->buf_queue(&fh->cap,fh->cap.read_buf);
1313 fh->cap.read_off = 0;
1316 buf = fh->cap.read_buf;
1322 poll_wait(file, &buf->done, wait);
1323 if (buf->state == STATE_DONE ||
1324 buf->state == STATE_ERROR)
1325 return POLLIN|POLLRDNORM;
1329 static int video_release(struct inode *inode, struct file *file)
1331 struct saa7134_fh *fh = file->private_data;
1332 struct saa7134_dev *dev = fh->dev;
1333 unsigned long flags;
1335 /* turn off overlay */
1336 if (res_check(fh, RESOURCE_OVERLAY)) {
1337 spin_lock_irqsave(&dev->slock,flags);
1338 stop_preview(dev,fh);
1339 spin_unlock_irqrestore(&dev->slock,flags);
1340 res_free(dev,fh,RESOURCE_OVERLAY);
1343 /* stop video capture */
1344 if (res_check(fh, RESOURCE_VIDEO)) {
1345 videobuf_streamoff(&fh->cap);
1346 res_free(dev,fh,RESOURCE_VIDEO);
1348 if (fh->cap.read_buf) {
1349 buffer_release(&fh->cap,fh->cap.read_buf);
1350 kfree(fh->cap.read_buf);
1353 /* stop vbi capture */
1354 if (res_check(fh, RESOURCE_VBI)) {
1355 if (fh->vbi.streaming)
1356 videobuf_streamoff(&fh->vbi);
1357 if (fh->vbi.reading)
1358 videobuf_read_stop(&fh->vbi);
1359 res_free(dev,fh,RESOURCE_VBI);
1363 videobuf_mmap_free(&fh->cap);
1364 videobuf_mmap_free(&fh->vbi);
1365 saa7134_pgtable_free(dev->pci,&fh->pt_cap);
1366 saa7134_pgtable_free(dev->pci,&fh->pt_vbi);
1368 v4l2_prio_close(&dev->prio,&fh->prio);
1369 file->private_data = NULL;
1375 video_mmap(struct file *file, struct vm_area_struct * vma)
1377 struct saa7134_fh *fh = file->private_data;
1379 return videobuf_mmap_mapper(saa7134_queue(fh), vma);
1382 /* ------------------------------------------------------------------ */
1384 static void saa7134_vbi_fmt(struct saa7134_dev *dev, struct v4l2_format *f)
1386 struct saa7134_tvnorm *norm = dev->tvnorm;
1388 f->fmt.vbi.sampling_rate = 6750000 * 4;
1389 f->fmt.vbi.samples_per_line = 2048 /* VBI_LINE_LENGTH */;
1390 f->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
1391 f->fmt.vbi.offset = 64 * 4;
1392 f->fmt.vbi.start[0] = norm->vbi_v_start;
1393 f->fmt.vbi.count[0] = norm->vbi_v_stop - norm->vbi_v_start +1;
1394 f->fmt.vbi.start[1] = norm->video_v_stop + norm->vbi_v_start +1;
1395 f->fmt.vbi.count[1] = f->fmt.vbi.count[0];
1396 f->fmt.vbi.flags = 0; /* VBI_UNSYNC VBI_INTERLACED */
1399 if (V4L2_STD_PAL == norm->id) {
1401 f->fmt.vbi.start[0] += 3;
1402 f->fmt.vbi.start[1] += 3*2;
1407 static int saa7134_g_fmt(struct saa7134_dev *dev, struct saa7134_fh *fh,
1408 struct v4l2_format *f)
1411 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1412 memset(&f->fmt.pix,0,sizeof(f->fmt.pix));
1413 f->fmt.pix.width = fh->width;
1414 f->fmt.pix.height = fh->height;
1415 f->fmt.pix.field = fh->cap.field;
1416 f->fmt.pix.pixelformat = fh->fmt->fourcc;
1417 f->fmt.pix.bytesperline =
1418 (f->fmt.pix.width * fh->fmt->depth) >> 3;
1419 f->fmt.pix.sizeimage =
1420 f->fmt.pix.height * f->fmt.pix.bytesperline;
1422 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
1423 f->fmt.win = fh->win;
1425 case V4L2_BUF_TYPE_VBI_CAPTURE:
1426 saa7134_vbi_fmt(dev,f);
1433 static int saa7134_try_fmt(struct saa7134_dev *dev, struct saa7134_fh *fh,
1434 struct v4l2_format *f)
1439 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1441 struct saa7134_format *fmt;
1442 enum v4l2_field field;
1443 unsigned int maxw, maxh;
1445 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1449 field = f->fmt.pix.field;
1450 maxw = min(dev->crop_current.width*4, dev->crop_bounds.width);
1451 maxh = min(dev->crop_current.height*4, dev->crop_bounds.height);
1453 if (V4L2_FIELD_ANY == field) {
1454 field = (f->fmt.pix.height > maxh/2)
1455 ? V4L2_FIELD_INTERLACED
1456 : V4L2_FIELD_BOTTOM;
1459 case V4L2_FIELD_TOP:
1460 case V4L2_FIELD_BOTTOM:
1463 case V4L2_FIELD_INTERLACED:
1469 f->fmt.pix.field = field;
1470 if (f->fmt.pix.width < 48)
1471 f->fmt.pix.width = 48;
1472 if (f->fmt.pix.height < 32)
1473 f->fmt.pix.height = 32;
1474 if (f->fmt.pix.width > maxw)
1475 f->fmt.pix.width = maxw;
1476 if (f->fmt.pix.height > maxh)
1477 f->fmt.pix.height = maxh;
1478 f->fmt.pix.width &= ~0x03;
1479 f->fmt.pix.bytesperline =
1480 (f->fmt.pix.width * fmt->depth) >> 3;
1481 f->fmt.pix.sizeimage =
1482 f->fmt.pix.height * f->fmt.pix.bytesperline;
1486 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
1487 err = verify_preview(dev,&f->fmt.win);
1491 case V4L2_BUF_TYPE_VBI_CAPTURE:
1492 saa7134_vbi_fmt(dev,f);
1499 static int saa7134_s_fmt(struct saa7134_dev *dev, struct saa7134_fh *fh,
1500 struct v4l2_format *f)
1502 unsigned long flags;
1506 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1507 err = saa7134_try_fmt(dev,fh,f);
1511 fh->fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1512 fh->width = f->fmt.pix.width;
1513 fh->height = f->fmt.pix.height;
1514 fh->cap.field = f->fmt.pix.field;
1516 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
1517 err = verify_preview(dev,&f->fmt.win);
1522 fh->win = f->fmt.win;
1523 fh->nclips = f->fmt.win.clipcount;
1526 if (copy_from_user(fh->clips,f->fmt.win.clips,
1527 sizeof(struct v4l2_clip)*fh->nclips)) {
1532 if (res_check(fh, RESOURCE_OVERLAY)) {
1533 spin_lock_irqsave(&dev->slock,flags);
1534 stop_preview(dev,fh);
1535 start_preview(dev,fh);
1536 spin_unlock_irqrestore(&dev->slock,flags);
1540 case V4L2_BUF_TYPE_VBI_CAPTURE:
1541 saa7134_vbi_fmt(dev,f);
1548 int saa7134_common_ioctl(struct saa7134_dev *dev,
1549 unsigned int cmd, void *arg)
1554 case VIDIOC_QUERYCTRL:
1556 const struct v4l2_queryctrl *ctrl;
1557 struct v4l2_queryctrl *c = arg;
1559 if ((c->id < V4L2_CID_BASE ||
1560 c->id >= V4L2_CID_LASTP1) &&
1561 (c->id < V4L2_CID_PRIVATE_BASE ||
1562 c->id >= V4L2_CID_PRIVATE_LASTP1))
1564 ctrl = ctrl_by_id(c->id);
1565 *c = (NULL != ctrl) ? *ctrl : no_ctrl;
1569 return get_control(dev,arg);
1573 err = set_control(dev,NULL,arg);
1577 /* --- input switching --------------------------------------- */
1578 case VIDIOC_ENUMINPUT:
1580 struct v4l2_input *i = arg;
1584 if (n >= SAA7134_INPUT_MAX)
1586 if (NULL == card_in(dev,i->index).name)
1588 memset(i,0,sizeof(*i));
1590 i->type = V4L2_INPUT_TYPE_CAMERA;
1591 strcpy(i->name,card_in(dev,n).name);
1592 if (card_in(dev,n).tv)
1593 i->type = V4L2_INPUT_TYPE_TUNER;
1595 if (n == dev->ctl_input) {
1596 int v1 = saa_readb(SAA7134_STATUS_VIDEO1);
1597 int v2 = saa_readb(SAA7134_STATUS_VIDEO2);
1599 if (0 != (v1 & 0x40))
1600 i->status |= V4L2_IN_ST_NO_H_LOCK;
1601 if (0 != (v2 & 0x40))
1602 i->status |= V4L2_IN_ST_NO_SYNC;
1603 if (0 != (v2 & 0x0e))
1604 i->status |= V4L2_IN_ST_MACROVISION;
1606 for (n = 0; n < TVNORMS; n++)
1607 i->std |= tvnorms[n].id;
1610 case VIDIOC_G_INPUT:
1613 *i = dev->ctl_input;
1616 case VIDIOC_S_INPUT:
1620 if (*i < 0 || *i >= SAA7134_INPUT_MAX)
1622 if (NULL == card_in(dev,*i).name)
1633 EXPORT_SYMBOL(saa7134_common_ioctl);
1636 * This function is _not_ called directly, but from
1637 * video_generic_ioctl (and maybe others). userspace
1638 * copying is done already, arg is a kernel pointer.
1640 static int video_do_ioctl(struct inode *inode, struct file *file,
1641 unsigned int cmd, void *arg)
1643 struct saa7134_fh *fh = file->private_data;
1644 struct saa7134_dev *dev = fh->dev;
1645 unsigned long flags;
1648 if (video_debug > 1)
1649 saa7134_print_ioctl(dev->name,cmd);
1654 case VIDIOC_S_INPUT:
1655 case VIDIOC_S_TUNER:
1656 case VIDIOC_S_FREQUENCY:
1657 err = v4l2_prio_check(&dev->prio,&fh->prio);
1663 case VIDIOC_QUERYCAP:
1665 struct v4l2_capability *cap = arg;
1667 memset(cap,0,sizeof(*cap));
1668 strcpy(cap->driver, "saa7134");
1669 strlcpy(cap->card, saa7134_boards[dev->board].name,
1671 sprintf(cap->bus_info,"PCI:%s",pci_name(dev->pci));
1672 cap->version = SAA7134_VERSION_CODE;
1674 V4L2_CAP_VIDEO_CAPTURE |
1675 V4L2_CAP_VIDEO_OVERLAY |
1676 V4L2_CAP_VBI_CAPTURE |
1678 V4L2_CAP_READWRITE |
1683 /* --- tv standards ------------------------------------------ */
1684 case VIDIOC_ENUMSTD:
1686 struct v4l2_standard *e = arg;
1692 err = v4l2_video_std_construct(e, tvnorms[e->index].id,
1693 tvnorms[e->index].name);
1701 v4l2_std_id *id = arg;
1703 *id = dev->tvnorm->id;
1708 v4l2_std_id *id = arg;
1711 for (i = 0; i < TVNORMS; i++)
1712 if (*id == tvnorms[i].id)
1715 for (i = 0; i < TVNORMS; i++)
1716 if (*id & tvnorms[i].id)
1722 if (res_check(fh, RESOURCE_OVERLAY)) {
1723 spin_lock_irqsave(&dev->slock,flags);
1724 stop_preview(dev,fh);
1725 set_tvnorm(dev,&tvnorms[i]);
1726 start_preview(dev,fh);
1727 spin_unlock_irqrestore(&dev->slock,flags);
1729 set_tvnorm(dev,&tvnorms[i]);
1730 saa7134_tvaudio_do_scan(dev);
1735 case VIDIOC_CROPCAP:
1737 struct v4l2_cropcap *cap = arg;
1739 if (cap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1740 cap->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
1742 cap->bounds = dev->crop_bounds;
1743 cap->defrect = dev->crop_defrect;
1744 cap->pixelaspect.numerator = 1;
1745 cap->pixelaspect.denominator = 1;
1746 if (dev->tvnorm->id & V4L2_STD_525_60) {
1747 cap->pixelaspect.numerator = 11;
1748 cap->pixelaspect.denominator = 10;
1750 if (dev->tvnorm->id & V4L2_STD_625_50) {
1751 cap->pixelaspect.numerator = 54;
1752 cap->pixelaspect.denominator = 59;
1759 struct v4l2_crop * crop = arg;
1761 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1762 crop->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
1764 crop->c = dev->crop_current;
1769 struct v4l2_crop *crop = arg;
1770 struct v4l2_rect *b = &dev->crop_bounds;
1772 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1773 crop->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
1775 if (crop->c.height < 0)
1777 if (crop->c.width < 0)
1780 if (res_locked(fh->dev,RESOURCE_OVERLAY))
1782 if (res_locked(fh->dev,RESOURCE_VIDEO))
1785 if (crop->c.top < b->top)
1786 crop->c.top = b->top;
1787 if (crop->c.top > b->top + b->height)
1788 crop->c.top = b->top + b->height;
1789 if (crop->c.height > b->top - crop->c.top + b->height)
1790 crop->c.height = b->top - crop->c.top + b->height;
1792 if (crop->c.left < b->left)
1793 crop->c.top = b->left;
1794 if (crop->c.left > b->left + b->width)
1795 crop->c.top = b->left + b->width;
1796 if (crop->c.width > b->left - crop->c.left + b->width)
1797 crop->c.width = b->left - crop->c.left + b->width;
1799 dev->crop_current = crop->c;
1803 /* --- tuner ioctls ------------------------------------------ */
1804 case VIDIOC_G_TUNER:
1806 struct v4l2_tuner *t = arg;
1811 memset(t,0,sizeof(*t));
1812 for (n = 0; n < SAA7134_INPUT_MAX; n++)
1813 if (card_in(dev,n).tv)
1815 if (NULL != card_in(dev,n).name) {
1816 strcpy(t->name, "Television");
1817 t->capability = V4L2_TUNER_CAP_NORM |
1818 V4L2_TUNER_CAP_STEREO |
1819 V4L2_TUNER_CAP_LANG1 |
1820 V4L2_TUNER_CAP_LANG2;
1821 t->rangehigh = 0xffffffffUL;
1822 t->rxsubchans = saa7134_tvaudio_getstereo(dev);
1823 t->audmode = saa7134_tvaudio_rx2mode(t->rxsubchans);
1825 if (0 != (saa_readb(SAA7134_STATUS_VIDEO1) & 0x03))
1829 case VIDIOC_S_TUNER:
1831 struct v4l2_tuner *t = arg;
1834 mode = dev->thread.mode;
1835 if (UNSET == mode) {
1836 rx = saa7134_tvaudio_getstereo(dev);
1837 mode = saa7134_tvaudio_rx2mode(t->rxsubchans);
1839 if (mode != t->audmode) {
1840 dev->thread.mode = t->audmode;
1844 case VIDIOC_G_FREQUENCY:
1846 struct v4l2_frequency *f = arg;
1848 memset(f,0,sizeof(*f));
1849 f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1850 f->frequency = dev->ctl_freq;
1853 case VIDIOC_S_FREQUENCY:
1855 struct v4l2_frequency *f = arg;
1859 if (0 == fh->radio && V4L2_TUNER_ANALOG_TV != f->type)
1861 if (1 == fh->radio && V4L2_TUNER_RADIO != f->type)
1864 dev->ctl_freq = f->frequency;
1865 #ifdef V4L2_I2C_CLIENTS
1866 saa7134_i2c_call_clients(dev,VIDIOC_S_FREQUENCY,f);
1868 saa7134_i2c_call_clients(dev,VIDIOCSFREQ,&dev->ctl_freq);
1870 saa7134_tvaudio_do_scan(dev);
1875 /* --- control ioctls ---------------------------------------- */
1876 case VIDIOC_ENUMINPUT:
1877 case VIDIOC_G_INPUT:
1878 case VIDIOC_S_INPUT:
1879 case VIDIOC_QUERYCTRL:
1882 return saa7134_common_ioctl(dev, cmd, arg);
1884 case VIDIOC_G_AUDIO:
1886 struct v4l2_audio *a = arg;
1888 memset(a,0,sizeof(*a));
1889 strcpy(a->name,"audio");
1892 case VIDIOC_S_AUDIO:
1896 struct v4l2_captureparm *parm = arg;
1897 memset(parm,0,sizeof(*parm));
1901 case VIDIOC_G_PRIORITY:
1903 enum v4l2_priority *p = arg;
1905 *p = v4l2_prio_max(&dev->prio);
1908 case VIDIOC_S_PRIORITY:
1910 enum v4l2_priority *prio = arg;
1912 return v4l2_prio_change(&dev->prio, &fh->prio, *prio);
1915 /* --- preview ioctls ---------------------------------------- */
1916 case VIDIOC_ENUM_FMT:
1918 struct v4l2_fmtdesc *f = arg;
1919 enum v4l2_buf_type type;
1925 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1926 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
1927 if (index >= FORMATS)
1929 if (f->type == V4L2_BUF_TYPE_VIDEO_OVERLAY &&
1930 formats[index].planar)
1932 memset(f,0,sizeof(*f));
1935 strlcpy(f->description,formats[index].name,sizeof(f->description));
1936 f->pixelformat = formats[index].fourcc;
1938 case V4L2_BUF_TYPE_VBI_CAPTURE:
1941 memset(f,0,sizeof(*f));
1944 f->pixelformat = V4L2_PIX_FMT_GREY;
1945 strcpy(f->description,"vbi data");
1954 struct v4l2_framebuffer *fb = arg;
1957 fb->capability = V4L2_FBUF_CAP_LIST_CLIPPING;
1962 struct v4l2_framebuffer *fb = arg;
1963 struct saa7134_format *fmt;
1965 if(!capable(CAP_SYS_ADMIN) &&
1966 !capable(CAP_SYS_RAWIO))
1970 fmt = format_by_fourcc(fb->fmt.pixelformat);
1977 if (0 == dev->ovbuf.fmt.bytesperline)
1978 dev->ovbuf.fmt.bytesperline =
1979 dev->ovbuf.fmt.width*fmt->depth/8;
1982 case VIDIOC_OVERLAY:
1987 if (!res_get(dev,fh,RESOURCE_OVERLAY))
1989 spin_lock_irqsave(&dev->slock,flags);
1990 start_preview(dev,fh);
1991 spin_unlock_irqrestore(&dev->slock,flags);
1994 if (!res_check(fh, RESOURCE_OVERLAY))
1996 spin_lock_irqsave(&dev->slock,flags);
1997 stop_preview(dev,fh);
1998 spin_unlock_irqrestore(&dev->slock,flags);
1999 res_free(dev,fh,RESOURCE_OVERLAY);
2004 /* --- capture ioctls ---------------------------------------- */
2007 struct v4l2_format *f = arg;
2008 return saa7134_g_fmt(dev,fh,f);
2012 struct v4l2_format *f = arg;
2013 return saa7134_s_fmt(dev,fh,f);
2015 case VIDIOC_TRY_FMT:
2017 struct v4l2_format *f = arg;
2018 return saa7134_try_fmt(dev,fh,f);
2023 struct video_mbuf *mbuf = arg;
2024 struct videobuf_queue *q;
2025 struct v4l2_requestbuffers req;
2028 q = saa7134_queue(fh);
2029 memset(&req,0,sizeof(req));
2031 req.count = gbuffers;
2032 req.memory = V4L2_MEMORY_MMAP;
2033 err = videobuf_reqbufs(q,&req);
2036 memset(mbuf,0,sizeof(*mbuf));
2037 mbuf->frames = req.count;
2039 for (i = 0; i < mbuf->frames; i++) {
2040 mbuf->offsets[i] = q->bufs[i]->boff;
2041 mbuf->size += q->bufs[i]->bsize;
2045 case VIDIOC_REQBUFS:
2046 return videobuf_reqbufs(saa7134_queue(fh),arg);
2048 case VIDIOC_QUERYBUF:
2049 return videobuf_querybuf(saa7134_queue(fh),arg);
2052 return videobuf_qbuf(saa7134_queue(fh),arg);
2055 return videobuf_dqbuf(saa7134_queue(fh),arg,
2056 file->f_flags & O_NONBLOCK);
2058 case VIDIOC_STREAMON:
2060 int res = saa7134_resource(fh);
2062 if (!res_get(dev,fh,res))
2064 return videobuf_streamon(saa7134_queue(fh));
2066 case VIDIOC_STREAMOFF:
2068 int res = saa7134_resource(fh);
2070 err = videobuf_streamoff(saa7134_queue(fh));
2073 res_free(dev,fh,res);
2078 return v4l_compat_translate_ioctl(inode,file,cmd,arg,
2084 static int video_ioctl(struct inode *inode, struct file *file,
2085 unsigned int cmd, unsigned long arg)
2087 return video_usercopy(inode, file, cmd, arg, video_do_ioctl);
2090 static int radio_do_ioctl(struct inode *inode, struct file *file,
2091 unsigned int cmd, void *arg)
2093 struct saa7134_fh *fh = file->private_data;
2094 struct saa7134_dev *dev = fh->dev;
2096 if (video_debug > 1)
2097 saa7134_print_ioctl(dev->name,cmd);
2099 case VIDIOC_QUERYCAP:
2101 struct v4l2_capability *cap = arg;
2103 memset(cap,0,sizeof(*cap));
2104 strcpy(cap->driver, "saa7134");
2105 strlcpy(cap->card, saa7134_boards[dev->board].name,
2107 sprintf(cap->bus_info,"PCI:%s",pci_name(dev->pci));
2108 cap->version = SAA7134_VERSION_CODE;
2109 cap->capabilities = V4L2_CAP_TUNER;
2112 case VIDIOC_G_TUNER:
2114 struct v4l2_tuner *t = arg;
2119 memset(t,0,sizeof(*t));
2120 strcpy(t->name, "Radio");
2121 t->rangelow = (int)(65*16);
2122 t->rangehigh = (int)(108*16);
2124 #ifdef V4L2_I2C_CLIENTS
2125 saa7134_i2c_call_clients(dev,VIDIOC_G_TUNER,t);
2128 struct video_tuner vt;
2129 memset(&vt,0,sizeof(vt));
2130 saa7134_i2c_call_clients(dev,VIDIOCGTUNER,&vt);
2131 t->signal = vt.signal;
2136 case VIDIOC_ENUMINPUT:
2138 struct v4l2_input *i = arg;
2142 strcpy(i->name,"Radio");
2143 i->type = V4L2_INPUT_TYPE_TUNER;
2146 case VIDIOC_G_INPUT:
2152 case VIDIOC_G_AUDIO:
2154 struct v4l2_audio *a = arg;
2156 memset(a,0,sizeof(*a));
2157 strcpy(a->name,"Radio");
2162 v4l2_std_id *id = arg;
2166 case VIDIOC_S_AUDIO:
2167 case VIDIOC_S_TUNER:
2168 case VIDIOC_S_INPUT:
2172 case VIDIOC_QUERYCTRL:
2174 const struct v4l2_queryctrl *ctrl;
2175 struct v4l2_queryctrl *c = arg;
2177 if (c->id < V4L2_CID_BASE ||
2178 c->id >= V4L2_CID_LASTP1)
2180 if (c->id == V4L2_CID_AUDIO_MUTE) {
2181 ctrl = ctrl_by_id(c->id);
2190 case VIDIOC_G_FREQUENCY:
2191 case VIDIOC_S_FREQUENCY:
2192 return video_do_ioctl(inode,file,cmd,arg);
2195 return v4l_compat_translate_ioctl(inode,file,cmd,arg,
2201 static int radio_ioctl(struct inode *inode, struct file *file,
2202 unsigned int cmd, unsigned long arg)
2204 return video_usercopy(inode, file, cmd, arg, radio_do_ioctl);
2207 static struct file_operations video_fops =
2209 .owner = THIS_MODULE,
2211 .release = video_release,
2215 .ioctl = video_ioctl,
2216 .llseek = no_llseek,
2219 static struct file_operations radio_fops =
2221 .owner = THIS_MODULE,
2223 .release = video_release,
2224 .ioctl = radio_ioctl,
2225 .llseek = no_llseek,
2228 /* ----------------------------------------------------------- */
2229 /* exported stuff */
2231 struct video_device saa7134_video_template =
2233 .name = "saa7134-video",
2234 .type = VID_TYPE_CAPTURE|VID_TYPE_TUNER|VID_TYPE_OVERLAY|
2235 VID_TYPE_CLIPPING|VID_TYPE_SCALES,
2237 .fops = &video_fops,
2241 struct video_device saa7134_vbi_template =
2243 .name = "saa7134-vbi",
2244 .type = VID_TYPE_TUNER|VID_TYPE_TELETEXT,
2246 .fops = &video_fops,
2250 struct video_device saa7134_radio_template =
2252 .name = "saa7134-radio",
2253 .type = VID_TYPE_TUNER,
2255 .fops = &radio_fops,
2259 int saa7134_video_init1(struct saa7134_dev *dev)
2261 /* sanitycheck insmod options */
2262 if (gbuffers < 2 || gbuffers > VIDEO_MAX_FRAME)
2264 if (gbufsize < 0 || gbufsize > gbufsize_max)
2265 gbufsize = gbufsize_max;
2266 gbufsize = (gbufsize + PAGE_SIZE - 1) & PAGE_MASK;
2268 /* put some sensible defaults into the data structures ... */
2269 dev->ctl_bright = ctrl_by_id(V4L2_CID_BRIGHTNESS)->default_value;
2270 dev->ctl_contrast = ctrl_by_id(V4L2_CID_CONTRAST)->default_value;
2271 dev->ctl_hue = ctrl_by_id(V4L2_CID_HUE)->default_value;
2272 dev->ctl_saturation = ctrl_by_id(V4L2_CID_SATURATION)->default_value;
2273 dev->ctl_volume = ctrl_by_id(V4L2_CID_AUDIO_VOLUME)->default_value;
2274 dev->ctl_mute = 1; // ctrl_by_id(V4L2_CID_AUDIO_MUTE)->default_value;
2275 dev->ctl_invert = ctrl_by_id(V4L2_CID_PRIVATE_INVERT)->default_value;
2276 dev->ctl_automute = ctrl_by_id(V4L2_CID_PRIVATE_AUTOMUTE)->default_value;
2278 if (dev->tda9887_conf && dev->ctl_automute)
2279 dev->tda9887_conf |= TDA9887_AUTOMUTE;
2282 INIT_LIST_HEAD(&dev->video_q.queue);
2283 init_timer(&dev->video_q.timeout);
2284 dev->video_q.timeout.function = saa7134_buffer_timeout;
2285 dev->video_q.timeout.data = (unsigned long)(&dev->video_q);
2286 dev->video_q.dev = dev;
2288 if (saa7134_boards[dev->board].video_out) {
2289 /* enable video output */
2290 int vo = saa7134_boards[dev->board].video_out;
2291 saa_writeb(SAA7134_VIDEO_PORT_CTRL0, video_out[vo][0]);
2292 saa_writeb(SAA7134_VIDEO_PORT_CTRL1, video_out[vo][1]);
2293 saa_writeb(SAA7134_VIDEO_PORT_CTRL2, video_out[vo][2]);
2294 saa_writeb(SAA7134_VIDEO_PORT_CTRL3, video_out[vo][3]);
2295 saa_writeb(SAA7134_VIDEO_PORT_CTRL4, video_out[vo][4]);
2296 saa_writeb(SAA7134_VIDEO_PORT_CTRL5, video_out[vo][5]);
2297 saa_writeb(SAA7134_VIDEO_PORT_CTRL6, video_out[vo][6]);
2298 saa_writeb(SAA7134_VIDEO_PORT_CTRL7, video_out[vo][7]);
2299 saa_writeb(SAA7134_VIDEO_PORT_CTRL8, video_out[vo][8]);
2305 int saa7134_video_init2(struct saa7134_dev *dev)
2308 set_tvnorm(dev,&tvnorms[0]);
2310 saa7134_tvaudio_setmute(dev);
2311 saa7134_tvaudio_setvolume(dev,dev->ctl_volume);
2315 int saa7134_video_fini(struct saa7134_dev *dev)
2321 void saa7134_irq_video_intl(struct saa7134_dev *dev)
2323 static const char *st[] = {
2324 "(no signal)", "NTSC", "PAL", "SECAM" };
2327 st1 = saa_readb(SAA7134_STATUS_VIDEO1);
2328 st2 = saa_readb(SAA7134_STATUS_VIDEO2);
2329 dprintk("DCSDT: pll: %s, sync: %s, norm: %s\n",
2330 (st1 & 0x40) ? "not locked" : "locked",
2331 (st2 & 0x40) ? "no" : "yes",
2333 dev->nosignal = (st1 & 0x40) || (st2 & 0x40);
2335 if (dev->nosignal) {
2336 /* no video signal -> mute audio */
2337 if (dev->ctl_automute)
2339 saa7134_tvaudio_setmute(dev);
2340 saa_setb(SAA7134_SYNC_CTRL, 0x20);
2342 /* wake up tvaudio audio carrier scan thread */
2343 saa7134_tvaudio_do_scan(dev);
2345 saa_clearb(SAA7134_SYNC_CTRL, 0x20);
2347 if (dev->mops && dev->mops->signal_change)
2348 dev->mops->signal_change(dev);
2351 void saa7134_irq_video_done(struct saa7134_dev *dev, unsigned long status)
2353 enum v4l2_field field;
2355 spin_lock(&dev->slock);
2356 if (dev->video_q.curr) {
2357 dev->video_fieldcount++;
2358 field = dev->video_q.curr->vb.field;
2359 if (V4L2_FIELD_HAS_BOTH(field)) {
2360 /* make sure we have seen both fields */
2361 if ((status & 0x10) == 0x00) {
2362 dev->video_q.curr->top_seen = 1;
2365 if (!dev->video_q.curr->top_seen)
2367 } else if (field == V4L2_FIELD_TOP) {
2368 if ((status & 0x10) != 0x10)
2370 } else if (field == V4L2_FIELD_BOTTOM) {
2371 if ((status & 0x10) != 0x00)
2374 dev->video_q.curr->vb.field_count = dev->video_fieldcount;
2375 saa7134_buffer_finish(dev,&dev->video_q,STATE_DONE);
2377 saa7134_buffer_next(dev,&dev->video_q);
2380 spin_unlock(&dev->slock);
2383 /* ----------------------------------------------------------- */