2 * $Id: saa7134-video.c,v 1.30 2005/06/07 19:00:38 nsh 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 /* ------------------------------------------------------------------ */
36 static unsigned int video_debug = 0;
37 static unsigned int gbuffers = 8;
38 static unsigned int noninterlaced = 0;
39 static unsigned int gbufsize = 720*576*4;
40 static unsigned int gbufsize_max = 720*576*4;
41 module_param(video_debug, int, 0644);
42 MODULE_PARM_DESC(video_debug,"enable debug messages [video]");
43 module_param(gbuffers, int, 0444);
44 MODULE_PARM_DESC(gbuffers,"number of capture buffers, range 2-32");
45 module_param(noninterlaced, int, 0644);
46 MODULE_PARM_DESC(noninterlaced,"video input is noninterlaced");
48 #define dprintk(fmt, arg...) if (video_debug) \
49 printk(KERN_DEBUG "%s/video: " fmt, dev->name , ## arg)
51 /* ------------------------------------------------------------------ */
52 /* data structs for video */
54 static int video_out[][9] = {
55 [CCIR656] = { 0x00, 0xb1, 0x00, 0xa1, 0x00, 0x04, 0x06, 0x00, 0x00 },
58 static struct saa7134_format formats[] = {
61 .fourcc = V4L2_PIX_FMT_GREY,
65 .name = "15 bpp RGB, le",
66 .fourcc = V4L2_PIX_FMT_RGB555,
70 .name = "15 bpp RGB, be",
71 .fourcc = V4L2_PIX_FMT_RGB555X,
76 .name = "16 bpp RGB, le",
77 .fourcc = V4L2_PIX_FMT_RGB565,
81 .name = "16 bpp RGB, be",
82 .fourcc = V4L2_PIX_FMT_RGB565X,
87 .name = "24 bpp RGB, le",
88 .fourcc = V4L2_PIX_FMT_BGR24,
92 .name = "24 bpp RGB, be",
93 .fourcc = V4L2_PIX_FMT_RGB24,
98 .name = "32 bpp RGB, le",
99 .fourcc = V4L2_PIX_FMT_BGR32,
103 .name = "32 bpp RGB, be",
104 .fourcc = V4L2_PIX_FMT_RGB32,
110 .name = "4:2:2 packed, YUYV",
111 .fourcc = V4L2_PIX_FMT_YUYV,
117 .name = "4:2:2 packed, UYVY",
118 .fourcc = V4L2_PIX_FMT_UYVY,
123 .name = "4:2:2 planar, Y-Cb-Cr",
124 .fourcc = V4L2_PIX_FMT_YUV422P,
132 .name = "4:2:0 planar, Y-Cb-Cr",
133 .fourcc = V4L2_PIX_FMT_YUV420,
141 .name = "4:2:0 planar, Y-Cb-Cr",
142 .fourcc = V4L2_PIX_FMT_YVU420,
152 #define FORMATS ARRAY_SIZE(formats)
154 #define NORM_625_50 \
157 .video_v_start = 24, \
158 .video_v_stop = 311, \
159 .vbi_v_start_0 = 7, \
160 .vbi_v_stop_0 = 22, \
161 .vbi_v_start_1 = 319, \
164 #define NORM_525_60 \
167 .video_v_start = 23, \
168 .video_v_stop = 262, \
169 .vbi_v_start_0 = 10, \
170 .vbi_v_stop_0 = 21, \
171 .vbi_v_start_1 = 273, \
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 .id = V4L2_STD_PAL_60,
281 .vbi_v_start_1 = 273,
284 .sync_control = 0x18,
285 .luma_control = 0x40,
286 .chroma_ctrl1 = 0x81,
288 .chroma_ctrl2 = 0x06,
292 #define TVNORMS ARRAY_SIZE(tvnorms)
294 #define V4L2_CID_PRIVATE_INVERT (V4L2_CID_PRIVATE_BASE + 0)
295 #define V4L2_CID_PRIVATE_Y_ODD (V4L2_CID_PRIVATE_BASE + 1)
296 #define V4L2_CID_PRIVATE_Y_EVEN (V4L2_CID_PRIVATE_BASE + 2)
297 #define V4L2_CID_PRIVATE_AUTOMUTE (V4L2_CID_PRIVATE_BASE + 3)
298 #define V4L2_CID_PRIVATE_LASTP1 (V4L2_CID_PRIVATE_BASE + 4)
300 static const struct v4l2_queryctrl no_ctrl = {
302 .flags = V4L2_CTRL_FLAG_DISABLED,
304 static const struct v4l2_queryctrl video_ctrls[] = {
307 .id = V4L2_CID_BRIGHTNESS,
308 .name = "Brightness",
312 .default_value = 128,
313 .type = V4L2_CTRL_TYPE_INTEGER,
315 .id = V4L2_CID_CONTRAST,
321 .type = V4L2_CTRL_TYPE_INTEGER,
323 .id = V4L2_CID_SATURATION,
324 .name = "Saturation",
329 .type = V4L2_CTRL_TYPE_INTEGER,
337 .type = V4L2_CTRL_TYPE_INTEGER,
339 .id = V4L2_CID_HFLIP,
343 .type = V4L2_CTRL_TYPE_BOOLEAN,
347 .id = V4L2_CID_AUDIO_MUTE,
351 .type = V4L2_CTRL_TYPE_BOOLEAN,
353 .id = V4L2_CID_AUDIO_VOLUME,
359 .type = V4L2_CTRL_TYPE_INTEGER,
361 /* --- private --- */
363 .id = V4L2_CID_PRIVATE_INVERT,
367 .type = V4L2_CTRL_TYPE_BOOLEAN,
369 .id = V4L2_CID_PRIVATE_Y_ODD,
370 .name = "y offset odd field",
374 .type = V4L2_CTRL_TYPE_INTEGER,
376 .id = V4L2_CID_PRIVATE_Y_EVEN,
377 .name = "y offset even field",
381 .type = V4L2_CTRL_TYPE_INTEGER,
383 .id = V4L2_CID_PRIVATE_AUTOMUTE,
388 .type = V4L2_CTRL_TYPE_BOOLEAN,
391 static const unsigned int CTRLS = ARRAY_SIZE(video_ctrls);
393 static const struct v4l2_queryctrl* ctrl_by_id(unsigned int id)
397 for (i = 0; i < CTRLS; i++)
398 if (video_ctrls[i].id == id)
399 return video_ctrls+i;
403 static struct saa7134_format* format_by_fourcc(unsigned int fourcc)
407 for (i = 0; i < FORMATS; i++)
408 if (formats[i].fourcc == fourcc)
413 /* ----------------------------------------------------------------------- */
414 /* resource management */
416 static int res_get(struct saa7134_dev *dev, struct saa7134_fh *fh, unsigned int bit)
418 if (fh->resources & bit)
419 /* have it already allocated */
424 if (dev->resources & bit) {
425 /* no, someone else uses it */
429 /* it's free, grab it */
430 fh->resources |= bit;
431 dev->resources |= bit;
432 dprintk("res: get %d\n",bit);
438 int res_check(struct saa7134_fh *fh, unsigned int bit)
440 return (fh->resources & bit);
444 int res_locked(struct saa7134_dev *dev, unsigned int bit)
446 return (dev->resources & bit);
450 void res_free(struct saa7134_dev *dev, struct saa7134_fh *fh, unsigned int bits)
452 if ((fh->resources & bits) != bits)
456 fh->resources &= ~bits;
457 dev->resources &= ~bits;
458 dprintk("res: put %d\n",bits);
462 /* ------------------------------------------------------------------ */
464 static void set_tvnorm(struct saa7134_dev *dev, struct saa7134_tvnorm *norm)
466 int luma_control,sync_control,mux;
468 dprintk("set tv norm = %s\n",norm->name);
471 mux = card_in(dev,dev->ctl_input).vmux;
472 luma_control = norm->luma_control;
473 sync_control = norm->sync_control;
476 luma_control |= 0x80; /* svideo */
477 if (noninterlaced || dev->nosignal)
478 sync_control |= 0x20;
481 dev->crop_bounds.left = norm->h_start;
482 dev->crop_defrect.left = norm->h_start;
483 dev->crop_bounds.width = norm->h_stop - norm->h_start +1;
484 dev->crop_defrect.width = norm->h_stop - norm->h_start +1;
486 dev->crop_bounds.top = (norm->vbi_v_stop_0+1)*2;
487 dev->crop_defrect.top = norm->video_v_start*2;
488 dev->crop_bounds.height = ((norm->id & V4L2_STD_525_60) ? 524 : 624)
489 - dev->crop_bounds.top;
490 dev->crop_defrect.height = (norm->video_v_stop - norm->video_v_start +1)*2;
492 dev->crop_current = dev->crop_defrect;
494 /* setup video decoder */
495 saa_writeb(SAA7134_INCR_DELAY, 0x08);
496 saa_writeb(SAA7134_ANALOG_IN_CTRL1, 0xc0 | mux);
497 saa_writeb(SAA7134_ANALOG_IN_CTRL2, 0x00);
499 saa_writeb(SAA7134_ANALOG_IN_CTRL3, 0x90);
500 saa_writeb(SAA7134_ANALOG_IN_CTRL4, 0x90);
501 saa_writeb(SAA7134_HSYNC_START, 0xeb);
502 saa_writeb(SAA7134_HSYNC_STOP, 0xe0);
503 saa_writeb(SAA7134_SOURCE_TIMING1, norm->src_timing);
505 saa_writeb(SAA7134_SYNC_CTRL, sync_control);
506 saa_writeb(SAA7134_LUMA_CTRL, luma_control);
507 saa_writeb(SAA7134_DEC_LUMA_BRIGHT, dev->ctl_bright);
508 saa_writeb(SAA7134_DEC_LUMA_CONTRAST, dev->ctl_contrast);
510 saa_writeb(SAA7134_DEC_CHROMA_SATURATION, dev->ctl_saturation);
511 saa_writeb(SAA7134_DEC_CHROMA_HUE, dev->ctl_hue);
512 saa_writeb(SAA7134_CHROMA_CTRL1, norm->chroma_ctrl1);
513 saa_writeb(SAA7134_CHROMA_GAIN, norm->chroma_gain);
515 saa_writeb(SAA7134_CHROMA_CTRL2, norm->chroma_ctrl2);
516 saa_writeb(SAA7134_MODE_DELAY_CTRL, 0x00);
518 saa_writeb(SAA7134_ANALOG_ADC, 0x01);
519 saa_writeb(SAA7134_VGATE_START, 0x11);
520 saa_writeb(SAA7134_VGATE_STOP, 0xfe);
521 saa_writeb(SAA7134_MISC_VGATE_MSB, norm->vgate_misc);
522 saa_writeb(SAA7134_RAW_DATA_GAIN, 0x40);
523 saa_writeb(SAA7134_RAW_DATA_OFFSET, 0x80);
525 saa7134_i2c_call_clients(dev,VIDIOC_S_STD,&norm->id);
528 static void video_mux(struct saa7134_dev *dev, int input)
530 dprintk("video input = %d [%s]\n",input,card_in(dev,input).name);
531 dev->ctl_input = input;
532 set_tvnorm(dev,dev->tvnorm);
533 saa7134_tvaudio_setinput(dev,&card_in(dev,input));
536 static void set_h_prescale(struct saa7134_dev *dev, int task, int prescale)
538 static const struct {
545 /* XPSC XACL XC2_1 XDCG VPFY */
557 static const int count = ARRAY_SIZE(vals);
560 for (i = 0; i < count; i++)
561 if (vals[i].xpsc == prescale)
566 saa_writeb(SAA7134_H_PRESCALE(task), vals[i].xpsc);
567 saa_writeb(SAA7134_ACC_LENGTH(task), vals[i].xacl);
568 saa_writeb(SAA7134_LEVEL_CTRL(task),
569 (vals[i].xc2_1 << 3) | (vals[i].xdcg));
570 saa_andorb(SAA7134_FIR_PREFILTER_CTRL(task), 0x0f,
571 (vals[i].vpfy << 2) | vals[i].vpfy);
574 static void set_v_scale(struct saa7134_dev *dev, int task, int yscale)
578 saa_writeb(SAA7134_V_SCALE_RATIO1(task), yscale & 0xff);
579 saa_writeb(SAA7134_V_SCALE_RATIO2(task), yscale >> 8);
581 mirror = (dev->ctl_mirror) ? 0x02 : 0x00;
584 dprintk("yscale LPI yscale=%d\n",yscale);
585 saa_writeb(SAA7134_V_FILTER(task), 0x00 | mirror);
586 saa_writeb(SAA7134_LUMA_CONTRAST(task), 0x40);
587 saa_writeb(SAA7134_CHROMA_SATURATION(task), 0x40);
590 val = 0x40 * 1024 / yscale;
591 dprintk("yscale ACM yscale=%d val=0x%x\n",yscale,val);
592 saa_writeb(SAA7134_V_FILTER(task), 0x01 | mirror);
593 saa_writeb(SAA7134_LUMA_CONTRAST(task), val);
594 saa_writeb(SAA7134_CHROMA_SATURATION(task), val);
596 saa_writeb(SAA7134_LUMA_BRIGHT(task), 0x80);
599 static void set_size(struct saa7134_dev *dev, int task,
600 int width, int height, int interlace)
602 int prescale,xscale,yscale,y_even,y_odd;
603 int h_start, h_stop, v_start, v_stop;
604 int div = interlace ? 2 : 1;
606 /* setup video scaler */
607 h_start = dev->crop_current.left;
608 v_start = dev->crop_current.top/2;
609 h_stop = (dev->crop_current.left + dev->crop_current.width -1);
610 v_stop = (dev->crop_current.top + dev->crop_current.height -1)/2;
612 saa_writeb(SAA7134_VIDEO_H_START1(task), h_start & 0xff);
613 saa_writeb(SAA7134_VIDEO_H_START2(task), h_start >> 8);
614 saa_writeb(SAA7134_VIDEO_H_STOP1(task), h_stop & 0xff);
615 saa_writeb(SAA7134_VIDEO_H_STOP2(task), h_stop >> 8);
616 saa_writeb(SAA7134_VIDEO_V_START1(task), v_start & 0xff);
617 saa_writeb(SAA7134_VIDEO_V_START2(task), v_start >> 8);
618 saa_writeb(SAA7134_VIDEO_V_STOP1(task), v_stop & 0xff);
619 saa_writeb(SAA7134_VIDEO_V_STOP2(task), v_stop >> 8);
621 prescale = dev->crop_current.width / width;
624 xscale = 1024 * dev->crop_current.width / prescale / width;
625 yscale = 512 * div * dev->crop_current.height / height;
626 dprintk("prescale=%d xscale=%d yscale=%d\n",prescale,xscale,yscale);
627 set_h_prescale(dev,task,prescale);
628 saa_writeb(SAA7134_H_SCALE_INC1(task), xscale & 0xff);
629 saa_writeb(SAA7134_H_SCALE_INC2(task), xscale >> 8);
630 set_v_scale(dev,task,yscale);
632 saa_writeb(SAA7134_VIDEO_PIXELS1(task), width & 0xff);
633 saa_writeb(SAA7134_VIDEO_PIXELS2(task), width >> 8);
634 saa_writeb(SAA7134_VIDEO_LINES1(task), height/div & 0xff);
635 saa_writeb(SAA7134_VIDEO_LINES2(task), height/div >> 8);
637 /* deinterlace y offsets */
638 y_odd = dev->ctl_y_odd;
639 y_even = dev->ctl_y_even;
640 saa_writeb(SAA7134_V_PHASE_OFFSET0(task), y_odd);
641 saa_writeb(SAA7134_V_PHASE_OFFSET1(task), y_even);
642 saa_writeb(SAA7134_V_PHASE_OFFSET2(task), y_odd);
643 saa_writeb(SAA7134_V_PHASE_OFFSET3(task), y_even);
646 /* ------------------------------------------------------------------ */
654 static void sort_cliplist(struct cliplist *cl, int entries)
656 struct cliplist swap;
659 for (i = entries-2; i >= 0; i--) {
660 for (n = 0, j = 0; j <= i; j++) {
661 if (cl[j].position > cl[j+1].position) {
673 static void set_cliplist(struct saa7134_dev *dev, int reg,
674 struct cliplist *cl, int entries, char *name)
679 for (i = 0; i < entries; i++) {
680 winbits |= cl[i].enable;
681 winbits &= ~cl[i].disable;
682 if (i < 15 && cl[i].position == cl[i+1].position)
684 saa_writeb(reg + 0, winbits);
685 saa_writeb(reg + 2, cl[i].position & 0xff);
686 saa_writeb(reg + 3, cl[i].position >> 8);
687 dprintk("clip: %s winbits=%02x pos=%d\n",
688 name,winbits,cl[i].position);
691 for (; reg < 0x400; reg += 8) {
692 saa_writeb(reg+ 0, 0);
693 saa_writeb(reg + 1, 0);
694 saa_writeb(reg + 2, 0);
695 saa_writeb(reg + 3, 0);
699 static int clip_range(int val)
706 static int setup_clipping(struct saa7134_dev *dev, struct v4l2_clip *clips,
707 int nclips, int interlace)
709 struct cliplist col[16], row[16];
711 int div = interlace ? 2 : 1;
713 memset(col,0,sizeof(col)); cols = 0;
714 memset(row,0,sizeof(row)); rows = 0;
715 for (i = 0; i < nclips && i < 8; i++) {
716 col[cols].position = clip_range(clips[i].c.left);
717 col[cols].enable = (1 << i);
719 col[cols].position = clip_range(clips[i].c.left+clips[i].c.width);
720 col[cols].disable = (1 << i);
722 row[rows].position = clip_range(clips[i].c.top / div);
723 row[rows].enable = (1 << i);
725 row[rows].position = clip_range((clips[i].c.top + clips[i].c.height)
727 row[rows].disable = (1 << i);
730 sort_cliplist(col,cols);
731 sort_cliplist(row,rows);
732 set_cliplist(dev,0x380,col,cols,"cols");
733 set_cliplist(dev,0x384,row,rows,"rows");
737 static int verify_preview(struct saa7134_dev *dev, struct v4l2_window *win)
739 enum v4l2_field field;
742 if (NULL == dev->ovbuf.base)
744 if (NULL == dev->ovfmt)
746 if (win->w.width < 48 || win->w.height < 32)
748 if (win->clipcount > 2048)
752 maxw = dev->crop_current.width;
753 maxh = dev->crop_current.height;
755 if (V4L2_FIELD_ANY == field) {
756 field = (win->w.height > maxh/2)
757 ? V4L2_FIELD_INTERLACED
762 case V4L2_FIELD_BOTTOM:
765 case V4L2_FIELD_INTERLACED:
772 if (win->w.width > maxw)
774 if (win->w.height > maxh)
775 win->w.height = maxh;
779 static int start_preview(struct saa7134_dev *dev, struct saa7134_fh *fh)
781 unsigned long base,control,bpl;
784 err = verify_preview(dev,&fh->win);
788 dev->ovfield = fh->win.field;
789 dprintk("start_preview %dx%d+%d+%d %s field=%s\n",
790 fh->win.w.width,fh->win.w.height,
791 fh->win.w.left,fh->win.w.top,
792 dev->ovfmt->name,v4l2_field_names[dev->ovfield]);
794 /* setup window + clipping */
795 set_size(dev,TASK_B,fh->win.w.width,fh->win.w.height,
796 V4L2_FIELD_HAS_BOTH(dev->ovfield));
797 setup_clipping(dev,fh->clips,fh->nclips,
798 V4L2_FIELD_HAS_BOTH(dev->ovfield));
800 saa_andorb(SAA7134_DATA_PATH(TASK_B), 0x3f, 0x03);
802 saa_andorb(SAA7134_DATA_PATH(TASK_B), 0x3f, 0x01);
803 saa_writeb(SAA7134_OFMT_VIDEO_B, dev->ovfmt->pm | 0x20);
805 /* dma: setup channel 1 (= Video Task B) */
806 base = (unsigned long)dev->ovbuf.base;
807 base += dev->ovbuf.fmt.bytesperline * fh->win.w.top;
808 base += dev->ovfmt->depth/8 * fh->win.w.left;
809 bpl = dev->ovbuf.fmt.bytesperline;
810 control = SAA7134_RS_CONTROL_BURST_16;
811 if (dev->ovfmt->bswap)
812 control |= SAA7134_RS_CONTROL_BSWAP;
813 if (dev->ovfmt->wswap)
814 control |= SAA7134_RS_CONTROL_WSWAP;
815 if (V4L2_FIELD_HAS_BOTH(dev->ovfield)) {
816 saa_writel(SAA7134_RS_BA1(1),base);
817 saa_writel(SAA7134_RS_BA2(1),base+bpl);
818 saa_writel(SAA7134_RS_PITCH(1),bpl*2);
819 saa_writel(SAA7134_RS_CONTROL(1),control);
821 saa_writel(SAA7134_RS_BA1(1),base);
822 saa_writel(SAA7134_RS_BA2(1),base);
823 saa_writel(SAA7134_RS_PITCH(1),bpl);
824 saa_writel(SAA7134_RS_CONTROL(1),control);
829 saa7134_set_dmabits(dev);
834 static int stop_preview(struct saa7134_dev *dev, struct saa7134_fh *fh)
837 saa7134_set_dmabits(dev);
841 /* ------------------------------------------------------------------ */
843 static int buffer_activate(struct saa7134_dev *dev,
844 struct saa7134_buf *buf,
845 struct saa7134_buf *next)
847 unsigned long base,control,bpl;
848 unsigned long bpl_uv,lines_uv,base2,base3,tmp; /* planar */
850 dprintk("buffer_activate buf=%p\n",buf);
851 buf->vb.state = STATE_ACTIVE;
854 set_size(dev,TASK_A,buf->vb.width,buf->vb.height,
855 V4L2_FIELD_HAS_BOTH(buf->vb.field));
857 saa_andorb(SAA7134_DATA_PATH(TASK_A), 0x3f, 0x03);
859 saa_andorb(SAA7134_DATA_PATH(TASK_A), 0x3f, 0x01);
860 saa_writeb(SAA7134_OFMT_VIDEO_A, buf->fmt->pm);
862 /* DMA: setup channel 0 (= Video Task A0) */
863 base = saa7134_buffer_base(buf);
864 if (buf->fmt->planar)
867 bpl = (buf->vb.width * buf->fmt->depth) / 8;
868 control = SAA7134_RS_CONTROL_BURST_16 |
869 SAA7134_RS_CONTROL_ME |
870 (buf->pt->dma >> 12);
872 control |= SAA7134_RS_CONTROL_BSWAP;
874 control |= SAA7134_RS_CONTROL_WSWAP;
875 if (V4L2_FIELD_HAS_BOTH(buf->vb.field)) {
877 saa_writel(SAA7134_RS_BA1(0),base);
878 saa_writel(SAA7134_RS_BA2(0),base+bpl);
879 saa_writel(SAA7134_RS_PITCH(0),bpl*2);
882 saa_writel(SAA7134_RS_BA1(0),base);
883 saa_writel(SAA7134_RS_BA2(0),base);
884 saa_writel(SAA7134_RS_PITCH(0),bpl);
886 saa_writel(SAA7134_RS_CONTROL(0),control);
888 if (buf->fmt->planar) {
889 /* DMA: setup channel 4+5 (= planar task A) */
890 bpl_uv = bpl >> buf->fmt->hshift;
891 lines_uv = buf->vb.height >> buf->fmt->vshift;
892 base2 = base + bpl * buf->vb.height;
893 base3 = base2 + bpl_uv * lines_uv;
894 if (buf->fmt->uvswap)
895 tmp = base2, base2 = base3, base3 = tmp;
896 dprintk("uv: bpl=%ld lines=%ld base2/3=%ld/%ld\n",
897 bpl_uv,lines_uv,base2,base3);
898 if (V4L2_FIELD_HAS_BOTH(buf->vb.field)) {
900 saa_writel(SAA7134_RS_BA1(4),base2);
901 saa_writel(SAA7134_RS_BA2(4),base2+bpl_uv);
902 saa_writel(SAA7134_RS_PITCH(4),bpl_uv*2);
903 saa_writel(SAA7134_RS_BA1(5),base3);
904 saa_writel(SAA7134_RS_BA2(5),base3+bpl_uv);
905 saa_writel(SAA7134_RS_PITCH(5),bpl_uv*2);
908 saa_writel(SAA7134_RS_BA1(4),base2);
909 saa_writel(SAA7134_RS_BA2(4),base2);
910 saa_writel(SAA7134_RS_PITCH(4),bpl_uv);
911 saa_writel(SAA7134_RS_BA1(5),base3);
912 saa_writel(SAA7134_RS_BA2(5),base3);
913 saa_writel(SAA7134_RS_PITCH(5),bpl_uv);
915 saa_writel(SAA7134_RS_CONTROL(4),control);
916 saa_writel(SAA7134_RS_CONTROL(5),control);
920 saa7134_set_dmabits(dev);
921 mod_timer(&dev->video_q.timeout, jiffies+BUFFER_TIMEOUT);
925 static int buffer_prepare(struct videobuf_queue *q,
926 struct videobuf_buffer *vb,
927 enum v4l2_field field)
929 struct saa7134_fh *fh = q->priv_data;
930 struct saa7134_dev *dev = fh->dev;
931 struct saa7134_buf *buf = container_of(vb,struct saa7134_buf,vb);
938 if (fh->width < 48 ||
940 fh->width/4 > dev->crop_current.width ||
941 fh->height/4 > dev->crop_current.height ||
942 fh->width > dev->crop_bounds.width ||
943 fh->height > dev->crop_bounds.height)
945 size = (fh->width * fh->height * fh->fmt->depth) >> 3;
946 if (0 != buf->vb.baddr && buf->vb.bsize < size)
949 dprintk("buffer_prepare [%d,size=%dx%d,bytes=%d,fields=%s,%s]\n",
950 vb->i,fh->width,fh->height,size,v4l2_field_names[field],
952 if (buf->vb.width != fh->width ||
953 buf->vb.height != fh->height ||
954 buf->vb.size != size ||
955 buf->vb.field != field ||
956 buf->fmt != fh->fmt) {
957 saa7134_dma_free(dev,buf);
960 if (STATE_NEEDS_INIT == buf->vb.state) {
961 buf->vb.width = fh->width;
962 buf->vb.height = fh->height;
964 buf->vb.field = field;
966 buf->pt = &fh->pt_cap;
968 err = videobuf_iolock(dev->pci,&buf->vb,&dev->ovbuf);
971 err = saa7134_pgtable_build(dev->pci,buf->pt,
974 saa7134_buffer_startpage(buf));
978 buf->vb.state = STATE_PREPARED;
979 buf->activate = buffer_activate;
983 saa7134_dma_free(dev,buf);
988 buffer_setup(struct videobuf_queue *q, unsigned int *count, unsigned int *size)
990 struct saa7134_fh *fh = q->priv_data;
992 *size = fh->fmt->depth * fh->width * fh->height >> 3;
995 *count = saa7134_buffer_count(*size,*count);
999 static void buffer_queue(struct videobuf_queue *q, struct videobuf_buffer *vb)
1001 struct saa7134_fh *fh = q->priv_data;
1002 struct saa7134_buf *buf = container_of(vb,struct saa7134_buf,vb);
1004 saa7134_buffer_queue(fh->dev,&fh->dev->video_q,buf);
1007 static void buffer_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
1009 struct saa7134_fh *fh = q->priv_data;
1010 struct saa7134_buf *buf = container_of(vb,struct saa7134_buf,vb);
1012 saa7134_dma_free(fh->dev,buf);
1015 static struct videobuf_queue_ops video_qops = {
1016 .buf_setup = buffer_setup,
1017 .buf_prepare = buffer_prepare,
1018 .buf_queue = buffer_queue,
1019 .buf_release = buffer_release,
1022 /* ------------------------------------------------------------------ */
1024 static int get_control(struct saa7134_dev *dev, struct v4l2_control *c)
1026 const struct v4l2_queryctrl* ctrl;
1028 ctrl = ctrl_by_id(c->id);
1032 case V4L2_CID_BRIGHTNESS:
1033 c->value = dev->ctl_bright;
1036 c->value = dev->ctl_hue;
1038 case V4L2_CID_CONTRAST:
1039 c->value = dev->ctl_contrast;
1041 case V4L2_CID_SATURATION:
1042 c->value = dev->ctl_saturation;
1044 case V4L2_CID_AUDIO_MUTE:
1045 c->value = dev->ctl_mute;
1047 case V4L2_CID_AUDIO_VOLUME:
1048 c->value = dev->ctl_volume;
1050 case V4L2_CID_PRIVATE_INVERT:
1051 c->value = dev->ctl_invert;
1053 case V4L2_CID_HFLIP:
1054 c->value = dev->ctl_mirror;
1056 case V4L2_CID_PRIVATE_Y_EVEN:
1057 c->value = dev->ctl_y_even;
1059 case V4L2_CID_PRIVATE_Y_ODD:
1060 c->value = dev->ctl_y_odd;
1062 case V4L2_CID_PRIVATE_AUTOMUTE:
1063 c->value = dev->ctl_automute;
1071 static int set_control(struct saa7134_dev *dev, struct saa7134_fh *fh,
1072 struct v4l2_control *c)
1074 const struct v4l2_queryctrl* ctrl;
1075 unsigned long flags;
1076 int restart_overlay = 0;
1078 ctrl = ctrl_by_id(c->id);
1081 dprintk("set_control name=%s val=%d\n",ctrl->name,c->value);
1082 switch (ctrl->type) {
1083 case V4L2_CTRL_TYPE_BOOLEAN:
1084 case V4L2_CTRL_TYPE_MENU:
1085 case V4L2_CTRL_TYPE_INTEGER:
1086 if (c->value < ctrl->minimum)
1087 c->value = ctrl->minimum;
1088 if (c->value > ctrl->maximum)
1089 c->value = ctrl->maximum;
1095 case V4L2_CID_BRIGHTNESS:
1096 dev->ctl_bright = c->value;
1097 saa_writeb(SAA7134_DEC_LUMA_BRIGHT, dev->ctl_bright);
1100 dev->ctl_hue = c->value;
1101 saa_writeb(SAA7134_DEC_CHROMA_HUE, dev->ctl_hue);
1103 case V4L2_CID_CONTRAST:
1104 dev->ctl_contrast = c->value;
1105 saa_writeb(SAA7134_DEC_LUMA_CONTRAST,
1106 dev->ctl_invert ? -dev->ctl_contrast : dev->ctl_contrast);
1108 case V4L2_CID_SATURATION:
1109 dev->ctl_saturation = c->value;
1110 saa_writeb(SAA7134_DEC_CHROMA_SATURATION,
1111 dev->ctl_invert ? -dev->ctl_saturation : dev->ctl_saturation);
1113 case V4L2_CID_AUDIO_MUTE:
1114 dev->ctl_mute = c->value;
1115 saa7134_tvaudio_setmute(dev);
1117 case V4L2_CID_AUDIO_VOLUME:
1118 dev->ctl_volume = c->value;
1119 saa7134_tvaudio_setvolume(dev,dev->ctl_volume);
1121 case V4L2_CID_PRIVATE_INVERT:
1122 dev->ctl_invert = c->value;
1123 saa_writeb(SAA7134_DEC_LUMA_CONTRAST,
1124 dev->ctl_invert ? -dev->ctl_contrast : dev->ctl_contrast);
1125 saa_writeb(SAA7134_DEC_CHROMA_SATURATION,
1126 dev->ctl_invert ? -dev->ctl_saturation : dev->ctl_saturation);
1128 case V4L2_CID_HFLIP:
1129 dev->ctl_mirror = c->value;
1130 restart_overlay = 1;
1132 case V4L2_CID_PRIVATE_Y_EVEN:
1133 dev->ctl_y_even = c->value;
1134 restart_overlay = 1;
1136 case V4L2_CID_PRIVATE_Y_ODD:
1137 dev->ctl_y_odd = c->value;
1138 restart_overlay = 1;
1140 case V4L2_CID_PRIVATE_AUTOMUTE:
1141 dev->ctl_automute = c->value;
1142 if (dev->tda9887_conf) {
1143 if (dev->ctl_automute)
1144 dev->tda9887_conf |= TDA9887_AUTOMUTE;
1146 dev->tda9887_conf &= ~TDA9887_AUTOMUTE;
1147 saa7134_i2c_call_clients(dev, TDA9887_SET_CONFIG,
1148 &dev->tda9887_conf);
1154 if (restart_overlay && fh && res_check(fh, RESOURCE_OVERLAY)) {
1155 spin_lock_irqsave(&dev->slock,flags);
1156 stop_preview(dev,fh);
1157 start_preview(dev,fh);
1158 spin_unlock_irqrestore(&dev->slock,flags);
1163 /* ------------------------------------------------------------------ */
1165 static struct videobuf_queue* saa7134_queue(struct saa7134_fh *fh)
1167 struct videobuf_queue* q = NULL;
1170 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1173 case V4L2_BUF_TYPE_VBI_CAPTURE:
1182 static int saa7134_resource(struct saa7134_fh *fh)
1187 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1188 res = RESOURCE_VIDEO;
1190 case V4L2_BUF_TYPE_VBI_CAPTURE:
1199 static int video_open(struct inode *inode, struct file *file)
1201 int minor = iminor(inode);
1202 struct saa7134_dev *h,*dev = NULL;
1203 struct saa7134_fh *fh;
1204 struct list_head *list;
1205 enum v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1208 list_for_each(list,&saa7134_devlist) {
1209 h = list_entry(list, struct saa7134_dev, devlist);
1210 if (h->video_dev && (h->video_dev->minor == minor))
1212 if (h->radio_dev && (h->radio_dev->minor == minor)) {
1216 if (h->vbi_dev && (h->vbi_dev->minor == minor)) {
1217 type = V4L2_BUF_TYPE_VBI_CAPTURE;
1224 dprintk("open minor=%d radio=%d type=%s\n",minor,radio,
1225 v4l2_type_names[type]);
1227 /* allocate + initialize per filehandle data */
1228 fh = kmalloc(sizeof(*fh),GFP_KERNEL);
1231 memset(fh,0,sizeof(*fh));
1232 file->private_data = fh;
1236 fh->fmt = format_by_fourcc(V4L2_PIX_FMT_BGR24);
1239 v4l2_prio_open(&dev->prio,&fh->prio);
1241 videobuf_queue_init(&fh->cap, &video_qops,
1242 dev->pci, &dev->slock,
1243 V4L2_BUF_TYPE_VIDEO_CAPTURE,
1244 V4L2_FIELD_INTERLACED,
1245 sizeof(struct saa7134_buf),
1247 videobuf_queue_init(&fh->vbi, &saa7134_vbi_qops,
1248 dev->pci, &dev->slock,
1249 V4L2_BUF_TYPE_VBI_CAPTURE,
1251 sizeof(struct saa7134_buf),
1253 saa7134_pgtable_alloc(dev->pci,&fh->pt_cap);
1254 saa7134_pgtable_alloc(dev->pci,&fh->pt_vbi);
1257 /* switch to radio mode */
1258 saa7134_tvaudio_setinput(dev,&card(dev).radio);
1259 saa7134_i2c_call_clients(dev,AUDC_SET_RADIO,NULL);
1261 /* switch to video/vbi mode */
1262 video_mux(dev,dev->ctl_input);
1268 video_read(struct file *file, char __user *data, size_t count, loff_t *ppos)
1270 struct saa7134_fh *fh = file->private_data;
1273 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1274 if (res_locked(fh->dev,RESOURCE_VIDEO))
1276 return videobuf_read_one(saa7134_queue(fh),
1278 file->f_flags & O_NONBLOCK);
1279 case V4L2_BUF_TYPE_VBI_CAPTURE:
1280 if (!res_get(fh->dev,fh,RESOURCE_VBI))
1282 return videobuf_read_stream(saa7134_queue(fh),
1283 data, count, ppos, 1,
1284 file->f_flags & O_NONBLOCK);
1293 video_poll(struct file *file, struct poll_table_struct *wait)
1295 struct saa7134_fh *fh = file->private_data;
1296 struct videobuf_buffer *buf = NULL;
1298 if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type)
1299 return videobuf_poll_stream(file, &fh->vbi, wait);
1301 if (res_check(fh,RESOURCE_VIDEO)) {
1302 if (!list_empty(&fh->cap.stream))
1303 buf = list_entry(fh->cap.stream.next, struct videobuf_buffer, stream);
1305 down(&fh->cap.lock);
1306 if (UNSET == fh->cap.read_off) {
1307 /* need to capture a new frame */
1308 if (res_locked(fh->dev,RESOURCE_VIDEO)) {
1312 if (0 != fh->cap.ops->buf_prepare(&fh->cap,fh->cap.read_buf,fh->cap.field)) {
1316 fh->cap.ops->buf_queue(&fh->cap,fh->cap.read_buf);
1317 fh->cap.read_off = 0;
1320 buf = fh->cap.read_buf;
1326 poll_wait(file, &buf->done, wait);
1327 if (buf->state == STATE_DONE ||
1328 buf->state == STATE_ERROR)
1329 return POLLIN|POLLRDNORM;
1333 static int video_release(struct inode *inode, struct file *file)
1335 struct saa7134_fh *fh = file->private_data;
1336 struct saa7134_dev *dev = fh->dev;
1337 unsigned long flags;
1339 /* turn off overlay */
1340 if (res_check(fh, RESOURCE_OVERLAY)) {
1341 spin_lock_irqsave(&dev->slock,flags);
1342 stop_preview(dev,fh);
1343 spin_unlock_irqrestore(&dev->slock,flags);
1344 res_free(dev,fh,RESOURCE_OVERLAY);
1347 /* stop video capture */
1348 if (res_check(fh, RESOURCE_VIDEO)) {
1349 videobuf_streamoff(&fh->cap);
1350 res_free(dev,fh,RESOURCE_VIDEO);
1352 if (fh->cap.read_buf) {
1353 buffer_release(&fh->cap,fh->cap.read_buf);
1354 kfree(fh->cap.read_buf);
1357 /* stop vbi capture */
1358 if (res_check(fh, RESOURCE_VBI)) {
1359 if (fh->vbi.streaming)
1360 videobuf_streamoff(&fh->vbi);
1361 if (fh->vbi.reading)
1362 videobuf_read_stop(&fh->vbi);
1363 res_free(dev,fh,RESOURCE_VBI);
1367 videobuf_mmap_free(&fh->cap);
1368 videobuf_mmap_free(&fh->vbi);
1369 saa7134_pgtable_free(dev->pci,&fh->pt_cap);
1370 saa7134_pgtable_free(dev->pci,&fh->pt_vbi);
1372 v4l2_prio_close(&dev->prio,&fh->prio);
1373 file->private_data = NULL;
1379 video_mmap(struct file *file, struct vm_area_struct * vma)
1381 struct saa7134_fh *fh = file->private_data;
1383 return videobuf_mmap_mapper(saa7134_queue(fh), vma);
1386 /* ------------------------------------------------------------------ */
1388 static void saa7134_vbi_fmt(struct saa7134_dev *dev, struct v4l2_format *f)
1390 struct saa7134_tvnorm *norm = dev->tvnorm;
1392 f->fmt.vbi.sampling_rate = 6750000 * 4;
1393 f->fmt.vbi.samples_per_line = 2048 /* VBI_LINE_LENGTH */;
1394 f->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
1395 f->fmt.vbi.offset = 64 * 4;
1396 f->fmt.vbi.start[0] = norm->vbi_v_start_0;
1397 f->fmt.vbi.count[0] = norm->vbi_v_stop_0 - norm->vbi_v_start_0 +1;
1398 f->fmt.vbi.start[1] = norm->vbi_v_start_1;
1399 f->fmt.vbi.count[1] = f->fmt.vbi.count[0];
1400 f->fmt.vbi.flags = 0; /* VBI_UNSYNC VBI_INTERLACED */
1403 if (V4L2_STD_PAL == norm->id) {
1405 f->fmt.vbi.start[0] += 3;
1406 f->fmt.vbi.start[1] += 3*2;
1411 static int saa7134_g_fmt(struct saa7134_dev *dev, struct saa7134_fh *fh,
1412 struct v4l2_format *f)
1415 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1416 memset(&f->fmt.pix,0,sizeof(f->fmt.pix));
1417 f->fmt.pix.width = fh->width;
1418 f->fmt.pix.height = fh->height;
1419 f->fmt.pix.field = fh->cap.field;
1420 f->fmt.pix.pixelformat = fh->fmt->fourcc;
1421 f->fmt.pix.bytesperline =
1422 (f->fmt.pix.width * fh->fmt->depth) >> 3;
1423 f->fmt.pix.sizeimage =
1424 f->fmt.pix.height * f->fmt.pix.bytesperline;
1426 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
1427 f->fmt.win = fh->win;
1429 case V4L2_BUF_TYPE_VBI_CAPTURE:
1430 saa7134_vbi_fmt(dev,f);
1437 static int saa7134_try_fmt(struct saa7134_dev *dev, struct saa7134_fh *fh,
1438 struct v4l2_format *f)
1443 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1445 struct saa7134_format *fmt;
1446 enum v4l2_field field;
1447 unsigned int maxw, maxh;
1449 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1453 field = f->fmt.pix.field;
1454 maxw = min(dev->crop_current.width*4, dev->crop_bounds.width);
1455 maxh = min(dev->crop_current.height*4, dev->crop_bounds.height);
1457 if (V4L2_FIELD_ANY == field) {
1458 field = (f->fmt.pix.height > maxh/2)
1459 ? V4L2_FIELD_INTERLACED
1460 : V4L2_FIELD_BOTTOM;
1463 case V4L2_FIELD_TOP:
1464 case V4L2_FIELD_BOTTOM:
1467 case V4L2_FIELD_INTERLACED:
1473 f->fmt.pix.field = field;
1474 if (f->fmt.pix.width < 48)
1475 f->fmt.pix.width = 48;
1476 if (f->fmt.pix.height < 32)
1477 f->fmt.pix.height = 32;
1478 if (f->fmt.pix.width > maxw)
1479 f->fmt.pix.width = maxw;
1480 if (f->fmt.pix.height > maxh)
1481 f->fmt.pix.height = maxh;
1482 f->fmt.pix.width &= ~0x03;
1483 f->fmt.pix.bytesperline =
1484 (f->fmt.pix.width * fmt->depth) >> 3;
1485 f->fmt.pix.sizeimage =
1486 f->fmt.pix.height * f->fmt.pix.bytesperline;
1490 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
1491 err = verify_preview(dev,&f->fmt.win);
1495 case V4L2_BUF_TYPE_VBI_CAPTURE:
1496 saa7134_vbi_fmt(dev,f);
1503 static int saa7134_s_fmt(struct saa7134_dev *dev, struct saa7134_fh *fh,
1504 struct v4l2_format *f)
1506 unsigned long flags;
1510 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1511 err = saa7134_try_fmt(dev,fh,f);
1515 fh->fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1516 fh->width = f->fmt.pix.width;
1517 fh->height = f->fmt.pix.height;
1518 fh->cap.field = f->fmt.pix.field;
1520 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
1521 err = verify_preview(dev,&f->fmt.win);
1526 fh->win = f->fmt.win;
1527 fh->nclips = f->fmt.win.clipcount;
1530 if (copy_from_user(fh->clips,f->fmt.win.clips,
1531 sizeof(struct v4l2_clip)*fh->nclips)) {
1536 if (res_check(fh, RESOURCE_OVERLAY)) {
1537 spin_lock_irqsave(&dev->slock,flags);
1538 stop_preview(dev,fh);
1539 start_preview(dev,fh);
1540 spin_unlock_irqrestore(&dev->slock,flags);
1544 case V4L2_BUF_TYPE_VBI_CAPTURE:
1545 saa7134_vbi_fmt(dev,f);
1552 int saa7134_common_ioctl(struct saa7134_dev *dev,
1553 unsigned int cmd, void *arg)
1558 case VIDIOC_QUERYCTRL:
1560 const struct v4l2_queryctrl *ctrl;
1561 struct v4l2_queryctrl *c = arg;
1563 if ((c->id < V4L2_CID_BASE ||
1564 c->id >= V4L2_CID_LASTP1) &&
1565 (c->id < V4L2_CID_PRIVATE_BASE ||
1566 c->id >= V4L2_CID_PRIVATE_LASTP1))
1568 ctrl = ctrl_by_id(c->id);
1569 *c = (NULL != ctrl) ? *ctrl : no_ctrl;
1573 return get_control(dev,arg);
1577 err = set_control(dev,NULL,arg);
1581 /* --- input switching --------------------------------------- */
1582 case VIDIOC_ENUMINPUT:
1584 struct v4l2_input *i = arg;
1588 if (n >= SAA7134_INPUT_MAX)
1590 if (NULL == card_in(dev,i->index).name)
1592 memset(i,0,sizeof(*i));
1594 i->type = V4L2_INPUT_TYPE_CAMERA;
1595 strcpy(i->name,card_in(dev,n).name);
1596 if (card_in(dev,n).tv)
1597 i->type = V4L2_INPUT_TYPE_TUNER;
1599 if (n == dev->ctl_input) {
1600 int v1 = saa_readb(SAA7134_STATUS_VIDEO1);
1601 int v2 = saa_readb(SAA7134_STATUS_VIDEO2);
1603 if (0 != (v1 & 0x40))
1604 i->status |= V4L2_IN_ST_NO_H_LOCK;
1605 if (0 != (v2 & 0x40))
1606 i->status |= V4L2_IN_ST_NO_SYNC;
1607 if (0 != (v2 & 0x0e))
1608 i->status |= V4L2_IN_ST_MACROVISION;
1610 for (n = 0; n < TVNORMS; n++)
1611 i->std |= tvnorms[n].id;
1614 case VIDIOC_G_INPUT:
1617 *i = dev->ctl_input;
1620 case VIDIOC_S_INPUT:
1624 if (*i < 0 || *i >= SAA7134_INPUT_MAX)
1626 if (NULL == card_in(dev,*i).name)
1637 EXPORT_SYMBOL(saa7134_common_ioctl);
1640 * This function is _not_ called directly, but from
1641 * video_generic_ioctl (and maybe others). userspace
1642 * copying is done already, arg is a kernel pointer.
1644 static int video_do_ioctl(struct inode *inode, struct file *file,
1645 unsigned int cmd, void *arg)
1647 struct saa7134_fh *fh = file->private_data;
1648 struct saa7134_dev *dev = fh->dev;
1649 unsigned long flags;
1652 if (video_debug > 1)
1653 saa7134_print_ioctl(dev->name,cmd);
1658 case VIDIOC_S_INPUT:
1659 case VIDIOC_S_TUNER:
1660 case VIDIOC_S_FREQUENCY:
1661 err = v4l2_prio_check(&dev->prio,&fh->prio);
1667 case VIDIOC_QUERYCAP:
1669 struct v4l2_capability *cap = arg;
1671 memset(cap,0,sizeof(*cap));
1672 strcpy(cap->driver, "saa7134");
1673 strlcpy(cap->card, saa7134_boards[dev->board].name,
1675 sprintf(cap->bus_info,"PCI:%s",pci_name(dev->pci));
1676 cap->version = SAA7134_VERSION_CODE;
1678 V4L2_CAP_VIDEO_CAPTURE |
1679 V4L2_CAP_VIDEO_OVERLAY |
1680 V4L2_CAP_VBI_CAPTURE |
1682 V4L2_CAP_READWRITE |
1687 /* --- tv standards ------------------------------------------ */
1688 case VIDIOC_ENUMSTD:
1690 struct v4l2_standard *e = arg;
1696 err = v4l2_video_std_construct(e, tvnorms[e->index].id,
1697 tvnorms[e->index].name);
1705 v4l2_std_id *id = arg;
1707 *id = dev->tvnorm->id;
1712 v4l2_std_id *id = arg;
1715 for (i = 0; i < TVNORMS; i++)
1716 if (*id == tvnorms[i].id)
1719 for (i = 0; i < TVNORMS; i++)
1720 if (*id & tvnorms[i].id)
1726 if (res_check(fh, RESOURCE_OVERLAY)) {
1727 spin_lock_irqsave(&dev->slock,flags);
1728 stop_preview(dev,fh);
1729 set_tvnorm(dev,&tvnorms[i]);
1730 start_preview(dev,fh);
1731 spin_unlock_irqrestore(&dev->slock,flags);
1733 set_tvnorm(dev,&tvnorms[i]);
1734 saa7134_tvaudio_do_scan(dev);
1739 case VIDIOC_CROPCAP:
1741 struct v4l2_cropcap *cap = arg;
1743 if (cap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1744 cap->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
1746 cap->bounds = dev->crop_bounds;
1747 cap->defrect = dev->crop_defrect;
1748 cap->pixelaspect.numerator = 1;
1749 cap->pixelaspect.denominator = 1;
1750 if (dev->tvnorm->id & V4L2_STD_525_60) {
1751 cap->pixelaspect.numerator = 11;
1752 cap->pixelaspect.denominator = 10;
1754 if (dev->tvnorm->id & V4L2_STD_625_50) {
1755 cap->pixelaspect.numerator = 54;
1756 cap->pixelaspect.denominator = 59;
1763 struct v4l2_crop * crop = arg;
1765 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1766 crop->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
1768 crop->c = dev->crop_current;
1773 struct v4l2_crop *crop = arg;
1774 struct v4l2_rect *b = &dev->crop_bounds;
1776 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1777 crop->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
1779 if (crop->c.height < 0)
1781 if (crop->c.width < 0)
1784 if (res_locked(fh->dev,RESOURCE_OVERLAY))
1786 if (res_locked(fh->dev,RESOURCE_VIDEO))
1789 if (crop->c.top < b->top)
1790 crop->c.top = b->top;
1791 if (crop->c.top > b->top + b->height)
1792 crop->c.top = b->top + b->height;
1793 if (crop->c.height > b->top - crop->c.top + b->height)
1794 crop->c.height = b->top - crop->c.top + b->height;
1796 if (crop->c.left < b->left)
1797 crop->c.top = b->left;
1798 if (crop->c.left > b->left + b->width)
1799 crop->c.top = b->left + b->width;
1800 if (crop->c.width > b->left - crop->c.left + b->width)
1801 crop->c.width = b->left - crop->c.left + b->width;
1803 dev->crop_current = crop->c;
1807 /* --- tuner ioctls ------------------------------------------ */
1808 case VIDIOC_G_TUNER:
1810 struct v4l2_tuner *t = arg;
1815 memset(t,0,sizeof(*t));
1816 for (n = 0; n < SAA7134_INPUT_MAX; n++)
1817 if (card_in(dev,n).tv)
1819 if (NULL != card_in(dev,n).name) {
1820 strcpy(t->name, "Television");
1821 t->capability = V4L2_TUNER_CAP_NORM |
1822 V4L2_TUNER_CAP_STEREO |
1823 V4L2_TUNER_CAP_LANG1 |
1824 V4L2_TUNER_CAP_LANG2;
1825 t->rangehigh = 0xffffffffUL;
1826 t->rxsubchans = saa7134_tvaudio_getstereo(dev);
1827 t->audmode = saa7134_tvaudio_rx2mode(t->rxsubchans);
1829 if (0 != (saa_readb(SAA7134_STATUS_VIDEO1) & 0x03))
1833 case VIDIOC_S_TUNER:
1835 struct v4l2_tuner *t = arg;
1838 mode = dev->thread.mode;
1839 if (UNSET == mode) {
1840 rx = saa7134_tvaudio_getstereo(dev);
1841 mode = saa7134_tvaudio_rx2mode(t->rxsubchans);
1843 if (mode != t->audmode) {
1844 dev->thread.mode = t->audmode;
1848 case VIDIOC_G_FREQUENCY:
1850 struct v4l2_frequency *f = arg;
1852 memset(f,0,sizeof(*f));
1853 f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1854 f->frequency = dev->ctl_freq;
1857 case VIDIOC_S_FREQUENCY:
1859 struct v4l2_frequency *f = arg;
1863 if (0 == fh->radio && V4L2_TUNER_ANALOG_TV != f->type)
1865 if (1 == fh->radio && V4L2_TUNER_RADIO != f->type)
1868 dev->ctl_freq = f->frequency;
1870 saa7134_i2c_call_clients(dev,VIDIOC_S_FREQUENCY,f);
1872 saa7134_tvaudio_do_scan(dev);
1877 /* --- control ioctls ---------------------------------------- */
1878 case VIDIOC_ENUMINPUT:
1879 case VIDIOC_G_INPUT:
1880 case VIDIOC_S_INPUT:
1881 case VIDIOC_QUERYCTRL:
1884 return saa7134_common_ioctl(dev, cmd, arg);
1886 case VIDIOC_G_AUDIO:
1888 struct v4l2_audio *a = arg;
1890 memset(a,0,sizeof(*a));
1891 strcpy(a->name,"audio");
1894 case VIDIOC_S_AUDIO:
1898 struct v4l2_captureparm *parm = arg;
1899 memset(parm,0,sizeof(*parm));
1903 case VIDIOC_G_PRIORITY:
1905 enum v4l2_priority *p = arg;
1907 *p = v4l2_prio_max(&dev->prio);
1910 case VIDIOC_S_PRIORITY:
1912 enum v4l2_priority *prio = arg;
1914 return v4l2_prio_change(&dev->prio, &fh->prio, *prio);
1917 /* --- preview ioctls ---------------------------------------- */
1918 case VIDIOC_ENUM_FMT:
1920 struct v4l2_fmtdesc *f = arg;
1921 enum v4l2_buf_type type;
1927 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1928 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
1929 if (index >= FORMATS)
1931 if (f->type == V4L2_BUF_TYPE_VIDEO_OVERLAY &&
1932 formats[index].planar)
1934 memset(f,0,sizeof(*f));
1937 strlcpy(f->description,formats[index].name,sizeof(f->description));
1938 f->pixelformat = formats[index].fourcc;
1940 case V4L2_BUF_TYPE_VBI_CAPTURE:
1943 memset(f,0,sizeof(*f));
1946 f->pixelformat = V4L2_PIX_FMT_GREY;
1947 strcpy(f->description,"vbi data");
1956 struct v4l2_framebuffer *fb = arg;
1959 fb->capability = V4L2_FBUF_CAP_LIST_CLIPPING;
1964 struct v4l2_framebuffer *fb = arg;
1965 struct saa7134_format *fmt;
1967 if(!capable(CAP_SYS_ADMIN) &&
1968 !capable(CAP_SYS_RAWIO))
1972 fmt = format_by_fourcc(fb->fmt.pixelformat);
1979 if (0 == dev->ovbuf.fmt.bytesperline)
1980 dev->ovbuf.fmt.bytesperline =
1981 dev->ovbuf.fmt.width*fmt->depth/8;
1984 case VIDIOC_OVERLAY:
1989 if (!res_get(dev,fh,RESOURCE_OVERLAY))
1991 spin_lock_irqsave(&dev->slock,flags);
1992 start_preview(dev,fh);
1993 spin_unlock_irqrestore(&dev->slock,flags);
1996 if (!res_check(fh, RESOURCE_OVERLAY))
1998 spin_lock_irqsave(&dev->slock,flags);
1999 stop_preview(dev,fh);
2000 spin_unlock_irqrestore(&dev->slock,flags);
2001 res_free(dev,fh,RESOURCE_OVERLAY);
2006 /* --- capture ioctls ---------------------------------------- */
2009 struct v4l2_format *f = arg;
2010 return saa7134_g_fmt(dev,fh,f);
2014 struct v4l2_format *f = arg;
2015 return saa7134_s_fmt(dev,fh,f);
2017 case VIDIOC_TRY_FMT:
2019 struct v4l2_format *f = arg;
2020 return saa7134_try_fmt(dev,fh,f);
2025 struct video_mbuf *mbuf = arg;
2026 struct videobuf_queue *q;
2027 struct v4l2_requestbuffers req;
2030 q = saa7134_queue(fh);
2031 memset(&req,0,sizeof(req));
2033 req.count = gbuffers;
2034 req.memory = V4L2_MEMORY_MMAP;
2035 err = videobuf_reqbufs(q,&req);
2038 memset(mbuf,0,sizeof(*mbuf));
2039 mbuf->frames = req.count;
2041 for (i = 0; i < mbuf->frames; i++) {
2042 mbuf->offsets[i] = q->bufs[i]->boff;
2043 mbuf->size += q->bufs[i]->bsize;
2047 case VIDIOC_REQBUFS:
2048 return videobuf_reqbufs(saa7134_queue(fh),arg);
2050 case VIDIOC_QUERYBUF:
2051 return videobuf_querybuf(saa7134_queue(fh),arg);
2054 return videobuf_qbuf(saa7134_queue(fh),arg);
2057 return videobuf_dqbuf(saa7134_queue(fh),arg,
2058 file->f_flags & O_NONBLOCK);
2060 case VIDIOC_STREAMON:
2062 int res = saa7134_resource(fh);
2064 if (!res_get(dev,fh,res))
2066 return videobuf_streamon(saa7134_queue(fh));
2068 case VIDIOC_STREAMOFF:
2070 int res = saa7134_resource(fh);
2072 err = videobuf_streamoff(saa7134_queue(fh));
2075 res_free(dev,fh,res);
2080 return v4l_compat_translate_ioctl(inode,file,cmd,arg,
2086 static int video_ioctl(struct inode *inode, struct file *file,
2087 unsigned int cmd, unsigned long arg)
2089 return video_usercopy(inode, file, cmd, arg, video_do_ioctl);
2092 static int radio_do_ioctl(struct inode *inode, struct file *file,
2093 unsigned int cmd, void *arg)
2095 struct saa7134_fh *fh = file->private_data;
2096 struct saa7134_dev *dev = fh->dev;
2098 if (video_debug > 1)
2099 saa7134_print_ioctl(dev->name,cmd);
2101 case VIDIOC_QUERYCAP:
2103 struct v4l2_capability *cap = arg;
2105 memset(cap,0,sizeof(*cap));
2106 strcpy(cap->driver, "saa7134");
2107 strlcpy(cap->card, saa7134_boards[dev->board].name,
2109 sprintf(cap->bus_info,"PCI:%s",pci_name(dev->pci));
2110 cap->version = SAA7134_VERSION_CODE;
2111 cap->capabilities = V4L2_CAP_TUNER;
2114 case VIDIOC_G_TUNER:
2116 struct v4l2_tuner *t = arg;
2121 memset(t,0,sizeof(*t));
2122 strcpy(t->name, "Radio");
2123 t->rangelow = (int)(65*16);
2124 t->rangehigh = (int)(108*16);
2126 saa7134_i2c_call_clients(dev, VIDIOC_G_TUNER, t);
2130 case VIDIOC_S_TUNER:
2132 struct v4l2_tuner *t = arg;
2137 saa7134_i2c_call_clients(dev,VIDIOC_S_TUNER,t);
2141 case VIDIOC_ENUMINPUT:
2143 struct v4l2_input *i = arg;
2147 strcpy(i->name,"Radio");
2148 i->type = V4L2_INPUT_TYPE_TUNER;
2151 case VIDIOC_G_INPUT:
2157 case VIDIOC_G_AUDIO:
2159 struct v4l2_audio *a = arg;
2161 memset(a,0,sizeof(*a));
2162 strcpy(a->name,"Radio");
2167 v4l2_std_id *id = arg;
2171 case VIDIOC_S_AUDIO:
2172 case VIDIOC_S_INPUT:
2176 case VIDIOC_QUERYCTRL:
2178 const struct v4l2_queryctrl *ctrl;
2179 struct v4l2_queryctrl *c = arg;
2181 if (c->id < V4L2_CID_BASE ||
2182 c->id >= V4L2_CID_LASTP1)
2184 if (c->id == V4L2_CID_AUDIO_MUTE) {
2185 ctrl = ctrl_by_id(c->id);
2194 case VIDIOC_G_FREQUENCY:
2195 case VIDIOC_S_FREQUENCY:
2196 return video_do_ioctl(inode,file,cmd,arg);
2199 return v4l_compat_translate_ioctl(inode,file,cmd,arg,
2205 static int radio_ioctl(struct inode *inode, struct file *file,
2206 unsigned int cmd, unsigned long arg)
2208 return video_usercopy(inode, file, cmd, arg, radio_do_ioctl);
2211 static struct file_operations video_fops =
2213 .owner = THIS_MODULE,
2215 .release = video_release,
2219 .ioctl = video_ioctl,
2220 .llseek = no_llseek,
2223 static struct file_operations radio_fops =
2225 .owner = THIS_MODULE,
2227 .release = video_release,
2228 .ioctl = radio_ioctl,
2229 .llseek = no_llseek,
2232 /* ----------------------------------------------------------- */
2233 /* exported stuff */
2235 struct video_device saa7134_video_template =
2237 .name = "saa7134-video",
2238 .type = VID_TYPE_CAPTURE|VID_TYPE_TUNER|VID_TYPE_OVERLAY|
2239 VID_TYPE_CLIPPING|VID_TYPE_SCALES,
2241 .fops = &video_fops,
2245 struct video_device saa7134_vbi_template =
2247 .name = "saa7134-vbi",
2248 .type = VID_TYPE_TUNER|VID_TYPE_TELETEXT,
2250 .fops = &video_fops,
2254 struct video_device saa7134_radio_template =
2256 .name = "saa7134-radio",
2257 .type = VID_TYPE_TUNER,
2259 .fops = &radio_fops,
2263 int saa7134_video_init1(struct saa7134_dev *dev)
2265 /* sanitycheck insmod options */
2266 if (gbuffers < 2 || gbuffers > VIDEO_MAX_FRAME)
2268 if (gbufsize < 0 || gbufsize > gbufsize_max)
2269 gbufsize = gbufsize_max;
2270 gbufsize = (gbufsize + PAGE_SIZE - 1) & PAGE_MASK;
2272 /* put some sensible defaults into the data structures ... */
2273 dev->ctl_bright = ctrl_by_id(V4L2_CID_BRIGHTNESS)->default_value;
2274 dev->ctl_contrast = ctrl_by_id(V4L2_CID_CONTRAST)->default_value;
2275 dev->ctl_hue = ctrl_by_id(V4L2_CID_HUE)->default_value;
2276 dev->ctl_saturation = ctrl_by_id(V4L2_CID_SATURATION)->default_value;
2277 dev->ctl_volume = ctrl_by_id(V4L2_CID_AUDIO_VOLUME)->default_value;
2278 dev->ctl_mute = 1; // ctrl_by_id(V4L2_CID_AUDIO_MUTE)->default_value;
2279 dev->ctl_invert = ctrl_by_id(V4L2_CID_PRIVATE_INVERT)->default_value;
2280 dev->ctl_automute = ctrl_by_id(V4L2_CID_PRIVATE_AUTOMUTE)->default_value;
2282 if (dev->tda9887_conf && dev->ctl_automute)
2283 dev->tda9887_conf |= TDA9887_AUTOMUTE;
2286 INIT_LIST_HEAD(&dev->video_q.queue);
2287 init_timer(&dev->video_q.timeout);
2288 dev->video_q.timeout.function = saa7134_buffer_timeout;
2289 dev->video_q.timeout.data = (unsigned long)(&dev->video_q);
2290 dev->video_q.dev = dev;
2292 if (saa7134_boards[dev->board].video_out) {
2293 /* enable video output */
2294 int vo = saa7134_boards[dev->board].video_out;
2295 saa_writeb(SAA7134_VIDEO_PORT_CTRL0, video_out[vo][0]);
2296 saa_writeb(SAA7134_VIDEO_PORT_CTRL1, video_out[vo][1]);
2297 saa_writeb(SAA7134_VIDEO_PORT_CTRL2, video_out[vo][2]);
2298 saa_writeb(SAA7134_VIDEO_PORT_CTRL3, video_out[vo][3]);
2299 saa_writeb(SAA7134_VIDEO_PORT_CTRL4, video_out[vo][4]);
2300 saa_writeb(SAA7134_VIDEO_PORT_CTRL5, video_out[vo][5]);
2301 saa_writeb(SAA7134_VIDEO_PORT_CTRL6, video_out[vo][6]);
2302 saa_writeb(SAA7134_VIDEO_PORT_CTRL7, video_out[vo][7]);
2303 saa_writeb(SAA7134_VIDEO_PORT_CTRL8, video_out[vo][8]);
2309 int saa7134_video_init2(struct saa7134_dev *dev)
2312 set_tvnorm(dev,&tvnorms[0]);
2314 saa7134_tvaudio_setmute(dev);
2315 saa7134_tvaudio_setvolume(dev,dev->ctl_volume);
2319 int saa7134_video_fini(struct saa7134_dev *dev)
2325 void saa7134_irq_video_intl(struct saa7134_dev *dev)
2327 static const char *st[] = {
2328 "(no signal)", "NTSC", "PAL", "SECAM" };
2331 st1 = saa_readb(SAA7134_STATUS_VIDEO1);
2332 st2 = saa_readb(SAA7134_STATUS_VIDEO2);
2333 dprintk("DCSDT: pll: %s, sync: %s, norm: %s\n",
2334 (st1 & 0x40) ? "not locked" : "locked",
2335 (st2 & 0x40) ? "no" : "yes",
2337 dev->nosignal = (st1 & 0x40) || (st2 & 0x40);
2339 if (dev->nosignal) {
2340 /* no video signal -> mute audio */
2341 if (dev->ctl_automute)
2343 saa7134_tvaudio_setmute(dev);
2344 saa_setb(SAA7134_SYNC_CTRL, 0x20);
2346 /* wake up tvaudio audio carrier scan thread */
2347 saa7134_tvaudio_do_scan(dev);
2349 saa_clearb(SAA7134_SYNC_CTRL, 0x20);
2351 if (dev->mops && dev->mops->signal_change)
2352 dev->mops->signal_change(dev);
2355 void saa7134_irq_video_done(struct saa7134_dev *dev, unsigned long status)
2357 enum v4l2_field field;
2359 spin_lock(&dev->slock);
2360 if (dev->video_q.curr) {
2361 dev->video_fieldcount++;
2362 field = dev->video_q.curr->vb.field;
2363 if (V4L2_FIELD_HAS_BOTH(field)) {
2364 /* make sure we have seen both fields */
2365 if ((status & 0x10) == 0x00) {
2366 dev->video_q.curr->top_seen = 1;
2369 if (!dev->video_q.curr->top_seen)
2371 } else if (field == V4L2_FIELD_TOP) {
2372 if ((status & 0x10) != 0x10)
2374 } else if (field == V4L2_FIELD_BOTTOM) {
2375 if ((status & 0x10) != 0x00)
2378 dev->video_q.curr->vb.field_count = dev->video_fieldcount;
2379 saa7134_buffer_finish(dev,&dev->video_q,STATE_DONE);
2381 saa7134_buffer_next(dev,&dev->video_q);
2384 spin_unlock(&dev->slock);
2387 /* ----------------------------------------------------------- */