2 * device driver for philips saa7134 based TV cards
3 * video4linux video interface
5 * (c) 2001-03 Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include <linux/init.h>
23 #include <linux/list.h>
24 #include <linux/module.h>
25 #include <linux/kernel.h>
26 #include <linux/slab.h>
28 #include "saa7134-reg.h"
31 #define V4L2_I2C_CLIENTS 1
33 /* ------------------------------------------------------------------ */
35 static unsigned int video_debug = 0;
36 static unsigned int gbuffers = 8;
37 static unsigned int noninterlaced = 0;
38 static unsigned int gbufsize = 720*576*4;
39 static unsigned int gbufsize_max = 720*576*4;
40 MODULE_PARM(video_debug,"i");
41 MODULE_PARM_DESC(video_debug,"enable debug messages [video]");
42 MODULE_PARM(gbuffers,"i");
43 MODULE_PARM_DESC(gbuffers,"number of capture buffers, range 2-32");
44 MODULE_PARM(noninterlaced,"i");
45 MODULE_PARM_DESC(noninterlaced,"video input is noninterlaced");
47 #define dprintk(fmt, arg...) if (video_debug) \
48 printk(KERN_DEBUG "%s/video: " fmt, dev->name , ## arg)
50 /* ------------------------------------------------------------------ */
51 /* data structs for video */
53 static int video_out[][9] = {
54 [CCIR656] = { 0x00, 0xb1, 0x00, 0xa1, 0x00, 0x04, 0x06, 0x00, 0x00 },
57 static struct saa7134_format formats[] = {
60 .fourcc = V4L2_PIX_FMT_GREY,
64 .name = "15 bpp RGB, le",
65 .fourcc = V4L2_PIX_FMT_RGB555,
69 .name = "15 bpp RGB, be",
70 .fourcc = V4L2_PIX_FMT_RGB555X,
75 .name = "16 bpp RGB, le",
76 .fourcc = V4L2_PIX_FMT_RGB565,
80 .name = "16 bpp RGB, be",
81 .fourcc = V4L2_PIX_FMT_RGB565X,
86 .name = "24 bpp RGB, le",
87 .fourcc = V4L2_PIX_FMT_BGR24,
91 .name = "24 bpp RGB, be",
92 .fourcc = V4L2_PIX_FMT_RGB24,
97 .name = "32 bpp RGB, le",
98 .fourcc = V4L2_PIX_FMT_BGR32,
102 .name = "32 bpp RGB, be",
103 .fourcc = V4L2_PIX_FMT_RGB32,
109 .name = "4:2:2 packed, YUYV",
110 .fourcc = V4L2_PIX_FMT_YUYV,
116 .name = "4:2:2 packed, UYVY",
117 .fourcc = V4L2_PIX_FMT_UYVY,
122 .name = "4:2:2 planar, Y-Cb-Cr",
123 .fourcc = V4L2_PIX_FMT_YUV422P,
131 .name = "4:2:0 planar, Y-Cb-Cr",
132 .fourcc = V4L2_PIX_FMT_YUV420,
140 .name = "4:2:0 planar, Y-Cb-Cr",
141 .fourcc = V4L2_PIX_FMT_YVU420,
151 #define FORMATS ARRAY_SIZE(formats)
153 #define NORM_625_50 \
156 .video_v_start = 24, \
157 .video_v_stop = 311, \
162 #define NORM_525_60 \
165 .video_v_start = 22, \
166 .video_v_stop = 22+239, \
167 .vbi_v_start = 10, /* FIXME */ \
168 .vbi_v_stop = 21, /* FIXME */ \
171 static struct saa7134_tvnorm tvnorms[] = {
173 .name = "PAL", /* autodetect */
177 .sync_control = 0x18,
178 .luma_control = 0x40,
179 .chroma_ctrl1 = 0x81,
181 .chroma_ctrl2 = 0x06,
186 .id = V4L2_STD_PAL_BG,
189 .sync_control = 0x18,
190 .luma_control = 0x40,
191 .chroma_ctrl1 = 0x81,
193 .chroma_ctrl2 = 0x06,
198 .id = V4L2_STD_PAL_I,
201 .sync_control = 0x18,
202 .luma_control = 0x40,
203 .chroma_ctrl1 = 0x81,
205 .chroma_ctrl2 = 0x06,
210 .id = V4L2_STD_PAL_DK,
213 .sync_control = 0x18,
214 .luma_control = 0x40,
215 .chroma_ctrl1 = 0x81,
217 .chroma_ctrl2 = 0x06,
225 .sync_control = 0x59,
226 .luma_control = 0x40,
227 .chroma_ctrl1 = 0x89,
229 .chroma_ctrl2 = 0x0e,
234 .id = V4L2_STD_SECAM,
237 .sync_control = 0x18, /* old: 0x58, */
238 .luma_control = 0x1b,
239 .chroma_ctrl1 = 0xd1,
241 .chroma_ctrl2 = 0x00,
246 .id = V4L2_STD_PAL_M,
249 .sync_control = 0x59,
250 .luma_control = 0x40,
251 .chroma_ctrl1 = 0xb9,
253 .chroma_ctrl2 = 0x0e,
258 .id = V4L2_STD_PAL_Nc,
261 .sync_control = 0x18,
262 .luma_control = 0x40,
263 .chroma_ctrl1 = 0xa1,
265 .chroma_ctrl2 = 0x06,
270 #define TVNORMS ARRAY_SIZE(tvnorms)
272 #define V4L2_CID_PRIVATE_INVERT (V4L2_CID_PRIVATE_BASE + 0)
273 #define V4L2_CID_PRIVATE_Y_ODD (V4L2_CID_PRIVATE_BASE + 1)
274 #define V4L2_CID_PRIVATE_Y_EVEN (V4L2_CID_PRIVATE_BASE + 2)
275 #define V4L2_CID_PRIVATE_AUTOMUTE (V4L2_CID_PRIVATE_BASE + 3)
276 #define V4L2_CID_PRIVATE_LASTP1 (V4L2_CID_PRIVATE_BASE + 4)
278 static const struct v4l2_queryctrl no_ctrl = {
280 .flags = V4L2_CTRL_FLAG_DISABLED,
282 static const struct v4l2_queryctrl video_ctrls[] = {
285 .id = V4L2_CID_BRIGHTNESS,
286 .name = "Brightness",
290 .default_value = 128,
291 .type = V4L2_CTRL_TYPE_INTEGER,
293 .id = V4L2_CID_CONTRAST,
299 .type = V4L2_CTRL_TYPE_INTEGER,
301 .id = V4L2_CID_SATURATION,
302 .name = "Saturation",
307 .type = V4L2_CTRL_TYPE_INTEGER,
315 .type = V4L2_CTRL_TYPE_INTEGER,
317 .id = V4L2_CID_VFLIP,
318 .name = "vertical flip",
321 .type = V4L2_CTRL_TYPE_BOOLEAN,
325 .id = V4L2_CID_AUDIO_MUTE,
329 .type = V4L2_CTRL_TYPE_BOOLEAN,
331 .id = V4L2_CID_AUDIO_VOLUME,
337 .type = V4L2_CTRL_TYPE_INTEGER,
339 /* --- private --- */
341 .id = V4L2_CID_PRIVATE_INVERT,
345 .type = V4L2_CTRL_TYPE_BOOLEAN,
347 .id = V4L2_CID_PRIVATE_Y_ODD,
348 .name = "y offset odd field",
352 .type = V4L2_CTRL_TYPE_INTEGER,
354 .id = V4L2_CID_PRIVATE_Y_EVEN,
355 .name = "y offset even field",
359 .type = V4L2_CTRL_TYPE_INTEGER,
361 .id = V4L2_CID_PRIVATE_AUTOMUTE,
366 .type = V4L2_CTRL_TYPE_BOOLEAN,
369 static const unsigned int CTRLS = ARRAY_SIZE(video_ctrls);
371 static const struct v4l2_queryctrl* ctrl_by_id(unsigned int id)
375 for (i = 0; i < CTRLS; i++)
376 if (video_ctrls[i].id == id)
377 return video_ctrls+i;
381 static struct saa7134_format* format_by_fourcc(unsigned int fourcc)
385 for (i = 0; i < FORMATS; i++)
386 if (formats[i].fourcc == fourcc)
391 /* ----------------------------------------------------------------------- */
392 /* resource management */
394 static int res_get(struct saa7134_dev *dev, struct saa7134_fh *fh, unsigned int bit)
396 if (fh->resources & bit)
397 /* have it already allocated */
402 if (dev->resources & bit) {
403 /* no, someone else uses it */
407 /* it's free, grab it */
408 fh->resources |= bit;
409 dev->resources |= bit;
410 dprintk("res: get %d\n",bit);
416 int res_check(struct saa7134_fh *fh, unsigned int bit)
418 return (fh->resources & bit);
422 int res_locked(struct saa7134_dev *dev, unsigned int bit)
424 return (dev->resources & bit);
428 void res_free(struct saa7134_dev *dev, struct saa7134_fh *fh, unsigned int bits)
430 if ((fh->resources & bits) != bits)
434 fh->resources &= ~bits;
435 dev->resources &= ~bits;
436 dprintk("res: put %d\n",bits);
440 /* ------------------------------------------------------------------ */
442 static void set_tvnorm(struct saa7134_dev *dev, struct saa7134_tvnorm *norm)
444 int luma_control,sync_control,mux,nosignal;
446 dprintk("set tv norm = %s\n",norm->name);
448 nosignal = (0 == (saa_readb(SAA7134_STATUS_VIDEO1) & 0x03));
450 mux = card_in(dev,dev->ctl_input).vmux;
451 luma_control = norm->luma_control;
452 sync_control = norm->sync_control;
455 luma_control |= 0x80; /* svideo */
456 if (noninterlaced || nosignal)
457 sync_control |= 0x20;
460 dev->crop_bounds.left = norm->h_start;
461 dev->crop_defrect.left = norm->h_start;
462 dev->crop_bounds.width = norm->h_stop - norm->h_start +1;
463 dev->crop_defrect.width = norm->h_stop - norm->h_start +1;
465 dev->crop_bounds.top = (norm->vbi_v_stop+1)*2;
466 dev->crop_defrect.top = norm->video_v_start*2;
467 dev->crop_bounds.height = ((norm->id & V4L2_STD_525_60) ? 524 : 624)
468 - dev->crop_bounds.top;
469 dev->crop_defrect.height = (norm->video_v_stop - norm->video_v_start +1)*2;
471 dev->crop_current = dev->crop_defrect;
473 /* setup video decoder */
474 saa_writeb(SAA7134_INCR_DELAY, 0x08);
475 saa_writeb(SAA7134_ANALOG_IN_CTRL1, 0xc0 | mux);
476 saa_writeb(SAA7134_ANALOG_IN_CTRL2, 0x00);
478 saa_writeb(SAA7134_ANALOG_IN_CTRL3, 0x90);
479 saa_writeb(SAA7134_ANALOG_IN_CTRL4, 0x90);
480 saa_writeb(SAA7134_HSYNC_START, 0xeb);
481 saa_writeb(SAA7134_HSYNC_STOP, 0xe0);
482 saa_writeb(SAA7134_SOURCE_TIMING1, norm->src_timing);
484 saa_writeb(SAA7134_SYNC_CTRL, sync_control);
485 saa_writeb(SAA7134_LUMA_CTRL, luma_control);
486 saa_writeb(SAA7134_DEC_LUMA_BRIGHT, dev->ctl_bright);
487 saa_writeb(SAA7134_DEC_LUMA_CONTRAST, dev->ctl_contrast);
489 saa_writeb(SAA7134_DEC_CHROMA_SATURATION, dev->ctl_saturation);
490 saa_writeb(SAA7134_DEC_CHROMA_HUE, dev->ctl_hue);
491 saa_writeb(SAA7134_CHROMA_CTRL1, norm->chroma_ctrl1);
492 saa_writeb(SAA7134_CHROMA_GAIN, norm->chroma_gain);
494 saa_writeb(SAA7134_CHROMA_CTRL2, norm->chroma_ctrl2);
495 saa_writeb(SAA7134_MODE_DELAY_CTRL, 0x00);
497 saa_writeb(SAA7134_ANALOG_ADC, 0x01);
498 saa_writeb(SAA7134_VGATE_START, 0x11);
499 saa_writeb(SAA7134_VGATE_STOP, 0xfe);
500 saa_writeb(SAA7134_MISC_VGATE_MSB, norm->vgate_misc);
501 saa_writeb(SAA7134_RAW_DATA_GAIN, 0x40);
502 saa_writeb(SAA7134_RAW_DATA_OFFSET, 0x80);
504 #ifdef V4L2_I2C_CLIENTS
505 saa7134_i2c_call_clients(dev,VIDIOC_S_STD,&norm->id);
508 /* pass down info to the i2c chips (v4l1) */
509 struct video_channel c;
510 memset(&c,0,sizeof(c));
511 c.channel = dev->ctl_input;
512 c.norm = VIDEO_MODE_PAL;
513 if (norm->id & V4L2_STD_NTSC)
514 c.norm = VIDEO_MODE_NTSC;
515 if (norm->id & V4L2_STD_SECAM)
516 c.norm = VIDEO_MODE_SECAM;
517 saa7134_i2c_call_clients(dev,VIDIOCSCHAN,&c);
522 static void video_mux(struct saa7134_dev *dev, int input)
524 dprintk("video input = %d [%s]\n",input,card_in(dev,input).name);
525 dev->ctl_input = input;
526 set_tvnorm(dev,dev->tvnorm);
527 saa7134_tvaudio_setinput(dev,&card_in(dev,input));
530 static void set_h_prescale(struct saa7134_dev *dev, int task, int prescale)
532 static const struct {
539 /* XPSC XACL XC2_1 XDCG VPFY */
551 static const int count = ARRAY_SIZE(vals);
554 for (i = 0; i < count; i++)
555 if (vals[i].xpsc == prescale)
560 saa_writeb(SAA7134_H_PRESCALE(task), vals[i].xpsc);
561 saa_writeb(SAA7134_ACC_LENGTH(task), vals[i].xacl);
562 saa_writeb(SAA7134_LEVEL_CTRL(task),
563 (vals[i].xc2_1 << 3) | (vals[i].xdcg));
564 saa_andorb(SAA7134_FIR_PREFILTER_CTRL(task), 0x0f,
565 (vals[i].vpfy << 2) | vals[i].vpfy);
568 static void set_v_scale(struct saa7134_dev *dev, int task, int yscale)
572 saa_writeb(SAA7134_V_SCALE_RATIO1(task), yscale & 0xff);
573 saa_writeb(SAA7134_V_SCALE_RATIO2(task), yscale >> 8);
575 mirror = (dev->ctl_mirror) ? 0x02 : 0x00;
578 dprintk("yscale LPI yscale=%d\n",yscale);
579 saa_writeb(SAA7134_V_FILTER(task), 0x00 | mirror);
580 saa_writeb(SAA7134_LUMA_CONTRAST(task), 0x40);
581 saa_writeb(SAA7134_CHROMA_SATURATION(task), 0x40);
584 val = 0x40 * 1024 / yscale;
585 dprintk("yscale ACM yscale=%d val=0x%x\n",yscale,val);
586 saa_writeb(SAA7134_V_FILTER(task), 0x01 | mirror);
587 saa_writeb(SAA7134_LUMA_CONTRAST(task), val);
588 saa_writeb(SAA7134_CHROMA_SATURATION(task), val);
590 saa_writeb(SAA7134_LUMA_BRIGHT(task), 0x80);
593 static void set_size(struct saa7134_dev *dev, int task,
594 int width, int height, int interlace)
596 int prescale,xscale,yscale,y_even,y_odd;
597 int h_start, h_stop, v_start, v_stop;
598 int div = interlace ? 2 : 1;
600 /* setup video scaler */
601 h_start = dev->crop_current.left;
602 v_start = dev->crop_current.top/2;
603 h_stop = (dev->crop_current.left + dev->crop_current.width -1);
604 v_stop = (dev->crop_current.top + dev->crop_current.height -1)/2;
606 saa_writeb(SAA7134_VIDEO_H_START1(task), h_start & 0xff);
607 saa_writeb(SAA7134_VIDEO_H_START2(task), h_start >> 8);
608 saa_writeb(SAA7134_VIDEO_H_STOP1(task), h_stop & 0xff);
609 saa_writeb(SAA7134_VIDEO_H_STOP2(task), h_stop >> 8);
610 saa_writeb(SAA7134_VIDEO_V_START1(task), v_start & 0xff);
611 saa_writeb(SAA7134_VIDEO_V_START2(task), v_start >> 8);
612 saa_writeb(SAA7134_VIDEO_V_STOP1(task), v_stop & 0xff);
613 saa_writeb(SAA7134_VIDEO_V_STOP2(task), v_stop >> 8);
615 prescale = dev->crop_current.width / width;
618 xscale = 1024 * dev->crop_current.width / prescale / width;
619 yscale = 512 * div * dev->crop_current.height / height;
620 dprintk("prescale=%d xscale=%d yscale=%d\n",prescale,xscale,yscale);
621 set_h_prescale(dev,task,prescale);
622 saa_writeb(SAA7134_H_SCALE_INC1(task), xscale & 0xff);
623 saa_writeb(SAA7134_H_SCALE_INC2(task), xscale >> 8);
624 set_v_scale(dev,task,yscale);
626 saa_writeb(SAA7134_VIDEO_PIXELS1(task), width & 0xff);
627 saa_writeb(SAA7134_VIDEO_PIXELS2(task), width >> 8);
628 saa_writeb(SAA7134_VIDEO_LINES1(task), height/div & 0xff);
629 saa_writeb(SAA7134_VIDEO_LINES2(task), height/div >> 8);
631 /* deinterlace y offsets */
632 y_odd = dev->ctl_y_odd;
633 y_even = dev->ctl_y_even;
634 saa_writeb(SAA7134_V_PHASE_OFFSET0(task), y_odd);
635 saa_writeb(SAA7134_V_PHASE_OFFSET1(task), y_even);
636 saa_writeb(SAA7134_V_PHASE_OFFSET2(task), y_odd);
637 saa_writeb(SAA7134_V_PHASE_OFFSET3(task), y_even);
640 /* ------------------------------------------------------------------ */
648 static void sort_cliplist(struct cliplist *cl, int entries)
650 struct cliplist swap;
653 for (i = entries-2; i >= 0; i--) {
654 for (n = 0, j = 0; j <= i; j++) {
655 if (cl[j].position > cl[j+1].position) {
667 static void set_cliplist(struct saa7134_dev *dev, int reg,
668 struct cliplist *cl, int entries, char *name)
673 for (i = 0; i < entries; i++) {
674 winbits |= cl[i].enable;
675 winbits &= ~cl[i].disable;
676 if (i < 15 && cl[i].position == cl[i+1].position)
678 saa_writeb(reg + 0, winbits);
679 saa_writeb(reg + 2, cl[i].position & 0xff);
680 saa_writeb(reg + 3, cl[i].position >> 8);
681 dprintk("clip: %s winbits=%02x pos=%d\n",
682 name,winbits,cl[i].position);
685 for (; reg < 0x400; reg += 8) {
686 saa_writeb(reg+ 0, 0);
687 saa_writeb(reg + 1, 0);
688 saa_writeb(reg + 2, 0);
689 saa_writeb(reg + 3, 0);
693 static int clip_range(int val)
700 static int setup_clipping(struct saa7134_dev *dev, struct v4l2_clip *clips,
701 int nclips, int interlace)
703 struct cliplist col[16], row[16];
705 int div = interlace ? 2 : 1;
707 memset(col,0,sizeof(col)); cols = 0;
708 memset(row,0,sizeof(row)); rows = 0;
709 for (i = 0; i < nclips && i < 8; i++) {
710 col[cols].position = clip_range(clips[i].c.left);
711 col[cols].enable = (1 << i);
713 col[cols].position = clip_range(clips[i].c.left+clips[i].c.width);
714 col[cols].disable = (1 << i);
716 row[rows].position = clip_range(clips[i].c.top / div);
717 row[rows].enable = (1 << i);
719 row[rows].position = clip_range((clips[i].c.top + clips[i].c.height)
721 row[rows].disable = (1 << i);
724 sort_cliplist(col,cols);
725 sort_cliplist(row,rows);
726 set_cliplist(dev,0x380,col,cols,"cols");
727 set_cliplist(dev,0x384,row,rows,"rows");
731 static int verify_preview(struct saa7134_dev *dev, struct v4l2_window *win)
733 enum v4l2_field field;
736 if (NULL == dev->ovbuf.base)
738 if (NULL == dev->ovfmt)
740 if (win->w.width < 48 || win->w.height < 32)
742 if (win->clipcount > 2048)
746 maxw = dev->crop_current.width;
747 maxh = dev->crop_current.height;
749 if (V4L2_FIELD_ANY == field) {
750 field = (win->w.height > maxh/2)
751 ? V4L2_FIELD_INTERLACED
756 case V4L2_FIELD_BOTTOM:
759 case V4L2_FIELD_INTERLACED:
766 if (win->w.width > maxw)
768 if (win->w.height > maxh)
769 win->w.height = maxh;
773 static int start_preview(struct saa7134_dev *dev, struct saa7134_fh *fh)
775 unsigned long base,control,bpl;
778 err = verify_preview(dev,&fh->win);
782 dev->ovfield = fh->win.field;
783 dprintk("start_preview %dx%d+%d+%d %s field=%s\n",
784 fh->win.w.width,fh->win.w.height,
785 fh->win.w.left,fh->win.w.top,
786 dev->ovfmt->name,v4l2_field_names[dev->ovfield]);
788 /* setup window + clipping */
789 set_size(dev,TASK_B,fh->win.w.width,fh->win.w.height,
790 V4L2_FIELD_HAS_BOTH(dev->ovfield));
791 setup_clipping(dev,fh->clips,fh->nclips,
792 V4L2_FIELD_HAS_BOTH(dev->ovfield));
794 saa_andorb(SAA7134_DATA_PATH(TASK_B), 0x3f, 0x03);
796 saa_andorb(SAA7134_DATA_PATH(TASK_B), 0x3f, 0x01);
797 saa_writeb(SAA7134_OFMT_VIDEO_B, dev->ovfmt->pm | 0x20);
799 /* dma: setup channel 1 (= Video Task B) */
800 base = (unsigned long)dev->ovbuf.base;
801 base += dev->ovbuf.fmt.bytesperline * fh->win.w.top;
802 base += dev->ovfmt->depth/8 * fh->win.w.left;
803 bpl = dev->ovbuf.fmt.bytesperline;
804 control = SAA7134_RS_CONTROL_BURST_16;
805 if (dev->ovfmt->bswap)
806 control |= SAA7134_RS_CONTROL_BSWAP;
807 if (dev->ovfmt->wswap)
808 control |= SAA7134_RS_CONTROL_WSWAP;
809 if (V4L2_FIELD_HAS_BOTH(dev->ovfield)) {
810 saa_writel(SAA7134_RS_BA1(1),base);
811 saa_writel(SAA7134_RS_BA2(1),base+bpl);
812 saa_writel(SAA7134_RS_PITCH(1),bpl*2);
813 saa_writel(SAA7134_RS_CONTROL(1),control);
815 saa_writel(SAA7134_RS_BA1(1),base);
816 saa_writel(SAA7134_RS_BA2(1),base);
817 saa_writel(SAA7134_RS_PITCH(1),bpl);
818 saa_writel(SAA7134_RS_CONTROL(1),control);
823 saa7134_set_dmabits(dev);
828 static int stop_preview(struct saa7134_dev *dev, struct saa7134_fh *fh)
831 saa7134_set_dmabits(dev);
835 /* ------------------------------------------------------------------ */
837 static int buffer_activate(struct saa7134_dev *dev,
838 struct saa7134_buf *buf,
839 struct saa7134_buf *next)
841 unsigned long base,control,bpl;
842 unsigned long bpl_uv,lines_uv,base2,base3,tmp; /* planar */
844 dprintk("buffer_activate buf=%p\n",buf);
845 buf->vb.state = STATE_ACTIVE;
848 set_size(dev,TASK_A,buf->vb.width,buf->vb.height,
849 V4L2_FIELD_HAS_BOTH(buf->vb.field));
851 saa_andorb(SAA7134_DATA_PATH(TASK_A), 0x3f, 0x03);
853 saa_andorb(SAA7134_DATA_PATH(TASK_A), 0x3f, 0x01);
854 saa_writeb(SAA7134_OFMT_VIDEO_A, buf->fmt->pm);
856 /* DMA: setup channel 0 (= Video Task A0) */
857 base = saa7134_buffer_base(buf);
858 if (buf->fmt->planar)
861 bpl = (buf->vb.width * buf->fmt->depth) / 8;
862 control = SAA7134_RS_CONTROL_BURST_16 |
863 SAA7134_RS_CONTROL_ME |
864 (buf->pt->dma >> 12);
866 control |= SAA7134_RS_CONTROL_BSWAP;
868 control |= SAA7134_RS_CONTROL_WSWAP;
869 if (V4L2_FIELD_HAS_BOTH(buf->vb.field)) {
871 saa_writel(SAA7134_RS_BA1(0),base);
872 saa_writel(SAA7134_RS_BA2(0),base+bpl);
873 saa_writel(SAA7134_RS_PITCH(0),bpl*2);
876 saa_writel(SAA7134_RS_BA1(0),base);
877 saa_writel(SAA7134_RS_BA2(0),base);
878 saa_writel(SAA7134_RS_PITCH(0),bpl);
880 saa_writel(SAA7134_RS_CONTROL(0),control);
882 if (buf->fmt->planar) {
883 /* DMA: setup channel 4+5 (= planar task A) */
884 bpl_uv = bpl >> buf->fmt->hshift;
885 lines_uv = buf->vb.height >> buf->fmt->vshift;
886 base2 = base + bpl * buf->vb.height;
887 base3 = base2 + bpl_uv * lines_uv;
888 if (buf->fmt->uvswap)
889 tmp = base2, base2 = base3, base3 = tmp;
890 dprintk("uv: bpl=%ld lines=%ld base2/3=%ld/%ld\n",
891 bpl_uv,lines_uv,base2,base3);
892 if (V4L2_FIELD_HAS_BOTH(buf->vb.field)) {
894 saa_writel(SAA7134_RS_BA1(4),base2);
895 saa_writel(SAA7134_RS_BA2(4),base2+bpl_uv);
896 saa_writel(SAA7134_RS_PITCH(4),bpl_uv*2);
897 saa_writel(SAA7134_RS_BA1(5),base3);
898 saa_writel(SAA7134_RS_BA2(5),base3+bpl_uv);
899 saa_writel(SAA7134_RS_PITCH(5),bpl_uv*2);
902 saa_writel(SAA7134_RS_BA1(4),base2);
903 saa_writel(SAA7134_RS_BA2(4),base2);
904 saa_writel(SAA7134_RS_PITCH(4),bpl_uv);
905 saa_writel(SAA7134_RS_BA1(5),base3);
906 saa_writel(SAA7134_RS_BA2(5),base3);
907 saa_writel(SAA7134_RS_PITCH(5),bpl_uv);
909 saa_writel(SAA7134_RS_CONTROL(4),control);
910 saa_writel(SAA7134_RS_CONTROL(5),control);
914 saa7134_set_dmabits(dev);
915 mod_timer(&dev->video_q.timeout, jiffies+BUFFER_TIMEOUT);
919 static int buffer_prepare(struct file *file, struct videobuf_buffer *vb,
920 enum v4l2_field field)
922 struct saa7134_fh *fh = file->private_data;
923 struct saa7134_dev *dev = fh->dev;
924 struct saa7134_buf *buf = (struct saa7134_buf *)vb;
931 if (fh->width < 48 ||
933 fh->width/4 > dev->crop_current.width ||
934 fh->height/4 > dev->crop_current.height ||
935 fh->width > dev->crop_bounds.width ||
936 fh->height > dev->crop_bounds.height)
938 size = (fh->width * fh->height * fh->fmt->depth) >> 3;
939 if (0 != buf->vb.baddr && buf->vb.bsize < size)
942 dprintk("buffer_prepare [%d,size=%dx%d,bytes=%d,fields=%s,%s]\n",
943 vb->i,fh->width,fh->height,size,v4l2_field_names[field],
945 if (buf->vb.width != fh->width ||
946 buf->vb.height != fh->height ||
947 buf->vb.size != size ||
948 buf->vb.field != field ||
949 buf->fmt != fh->fmt) {
950 saa7134_dma_free(dev,buf);
953 if (STATE_NEEDS_INIT == buf->vb.state) {
954 buf->vb.width = fh->width;
955 buf->vb.height = fh->height;
957 buf->vb.field = field;
959 buf->pt = &fh->pt_cap;
961 err = videobuf_iolock(dev->pci,&buf->vb,&dev->ovbuf);
964 err = saa7134_pgtable_build(dev->pci,buf->pt,
967 saa7134_buffer_startpage(buf));
971 buf->vb.state = STATE_PREPARED;
972 buf->activate = buffer_activate;
976 saa7134_dma_free(dev,buf);
981 buffer_setup(struct file *file, unsigned int *count, unsigned int *size)
983 struct saa7134_fh *fh = file->private_data;
985 *size = fh->fmt->depth * fh->width * fh->height >> 3;
988 *count = saa7134_buffer_count(*size,*count);
992 static void buffer_queue(struct file *file, struct videobuf_buffer *vb)
994 struct saa7134_fh *fh = file->private_data;
995 struct saa7134_buf *buf = (struct saa7134_buf *)vb;
997 saa7134_buffer_queue(fh->dev,&fh->dev->video_q,buf);
1000 static void buffer_release(struct file *file, struct videobuf_buffer *vb)
1002 struct saa7134_fh *fh = file->private_data;
1003 struct saa7134_buf *buf = (struct saa7134_buf *)vb;
1005 saa7134_dma_free(fh->dev,buf);
1008 static struct videobuf_queue_ops video_qops = {
1009 .buf_setup = buffer_setup,
1010 .buf_prepare = buffer_prepare,
1011 .buf_queue = buffer_queue,
1012 .buf_release = buffer_release,
1015 /* ------------------------------------------------------------------ */
1017 static int get_control(struct saa7134_dev *dev, struct v4l2_control *c)
1019 const struct v4l2_queryctrl* ctrl;
1021 ctrl = ctrl_by_id(c->id);
1025 case V4L2_CID_BRIGHTNESS:
1026 c->value = dev->ctl_bright;
1029 c->value = dev->ctl_hue;
1031 case V4L2_CID_CONTRAST:
1032 c->value = dev->ctl_contrast;
1034 case V4L2_CID_SATURATION:
1035 c->value = dev->ctl_saturation;
1037 case V4L2_CID_AUDIO_MUTE:
1038 c->value = dev->ctl_mute;
1040 case V4L2_CID_AUDIO_VOLUME:
1041 c->value = dev->ctl_volume;
1043 case V4L2_CID_PRIVATE_INVERT:
1044 c->value = dev->ctl_invert;
1046 case V4L2_CID_VFLIP:
1047 c->value = dev->ctl_mirror;
1049 case V4L2_CID_PRIVATE_Y_EVEN:
1050 c->value = dev->ctl_y_even;
1052 case V4L2_CID_PRIVATE_Y_ODD:
1053 c->value = dev->ctl_y_odd;
1055 case V4L2_CID_PRIVATE_AUTOMUTE:
1056 c->value = dev->ctl_automute;
1064 static int set_control(struct saa7134_dev *dev, struct saa7134_fh *fh,
1065 struct v4l2_control *c)
1067 const struct v4l2_queryctrl* ctrl;
1068 unsigned long flags;
1069 int restart_overlay = 0;
1071 ctrl = ctrl_by_id(c->id);
1074 dprintk("set_control name=%s val=%d\n",ctrl->name,c->value);
1075 switch (ctrl->type) {
1076 case V4L2_CTRL_TYPE_BOOLEAN:
1077 case V4L2_CTRL_TYPE_MENU:
1078 case V4L2_CTRL_TYPE_INTEGER:
1079 if (c->value < ctrl->minimum)
1080 c->value = ctrl->minimum;
1081 if (c->value > ctrl->maximum)
1082 c->value = ctrl->maximum;
1088 case V4L2_CID_BRIGHTNESS:
1089 dev->ctl_bright = c->value;
1090 saa_writeb(SAA7134_DEC_LUMA_BRIGHT, dev->ctl_bright);
1093 dev->ctl_hue = c->value;
1094 saa_writeb(SAA7134_DEC_CHROMA_HUE, dev->ctl_hue);
1096 case V4L2_CID_CONTRAST:
1097 dev->ctl_contrast = c->value;
1098 saa_writeb(SAA7134_DEC_LUMA_CONTRAST,
1099 dev->ctl_invert ? -dev->ctl_contrast : dev->ctl_contrast);
1101 case V4L2_CID_SATURATION:
1102 dev->ctl_saturation = c->value;
1103 saa_writeb(SAA7134_DEC_CHROMA_SATURATION,
1104 dev->ctl_invert ? -dev->ctl_saturation : dev->ctl_saturation);
1106 case V4L2_CID_AUDIO_MUTE:
1107 dev->ctl_mute = c->value;
1108 saa7134_tvaudio_setmute(dev);
1110 case V4L2_CID_AUDIO_VOLUME:
1111 dev->ctl_volume = c->value;
1112 saa7134_tvaudio_setvolume(dev,dev->ctl_volume);
1114 case V4L2_CID_PRIVATE_INVERT:
1115 dev->ctl_invert = c->value;
1116 saa_writeb(SAA7134_DEC_LUMA_CONTRAST,
1117 dev->ctl_invert ? -dev->ctl_contrast : dev->ctl_contrast);
1118 saa_writeb(SAA7134_DEC_CHROMA_SATURATION,
1119 dev->ctl_invert ? -dev->ctl_saturation : dev->ctl_saturation);
1121 case V4L2_CID_VFLIP:
1122 dev->ctl_mirror = c->value;
1123 restart_overlay = 1;
1125 case V4L2_CID_PRIVATE_Y_EVEN:
1126 dev->ctl_y_even = c->value;
1127 restart_overlay = 1;
1129 case V4L2_CID_PRIVATE_Y_ODD:
1130 dev->ctl_y_odd = c->value;
1131 restart_overlay = 1;
1133 case V4L2_CID_PRIVATE_AUTOMUTE:
1134 dev->ctl_automute = c->value;
1135 if (dev->tda9887_conf) {
1136 if (dev->ctl_automute)
1137 dev->tda9887_conf |= TDA9887_AUTOMUTE;
1139 dev->tda9887_conf &= ~TDA9887_AUTOMUTE;
1140 saa7134_i2c_call_clients(dev, TDA9887_SET_CONFIG,
1141 &dev->tda9887_conf);
1147 if (restart_overlay && fh && res_check(fh, RESOURCE_OVERLAY)) {
1148 spin_lock_irqsave(&dev->slock,flags);
1149 stop_preview(dev,fh);
1150 start_preview(dev,fh);
1151 spin_unlock_irqrestore(&dev->slock,flags);
1156 /* ------------------------------------------------------------------ */
1158 static struct videobuf_queue* saa7134_queue(struct saa7134_fh *fh)
1160 struct videobuf_queue* q = NULL;
1163 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1166 case V4L2_BUF_TYPE_VBI_CAPTURE:
1175 static int saa7134_resource(struct saa7134_fh *fh)
1180 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1181 res = RESOURCE_VIDEO;
1183 case V4L2_BUF_TYPE_VBI_CAPTURE:
1192 static int video_open(struct inode *inode, struct file *file)
1194 int minor = iminor(inode);
1195 struct saa7134_dev *h,*dev = NULL;
1196 struct saa7134_fh *fh;
1197 struct list_head *list;
1198 enum v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1201 list_for_each(list,&saa7134_devlist) {
1202 h = list_entry(list, struct saa7134_dev, devlist);
1203 if (h->video_dev && (h->video_dev->minor == minor))
1205 if (h->radio_dev && (h->radio_dev->minor == minor)) {
1209 if (h->vbi_dev && (h->vbi_dev->minor == minor)) {
1210 type = V4L2_BUF_TYPE_VBI_CAPTURE;
1217 dprintk("open minor=%d radio=%d type=%s\n",minor,radio,
1218 v4l2_type_names[type]);
1220 /* allocate + initialize per filehandle data */
1221 fh = kmalloc(sizeof(*fh),GFP_KERNEL);
1224 memset(fh,0,sizeof(*fh));
1225 file->private_data = fh;
1229 fh->fmt = format_by_fourcc(V4L2_PIX_FMT_BGR24);
1232 #ifdef VIDIOC_G_PRIORITY
1233 v4l2_prio_open(&dev->prio,&fh->prio);
1236 videobuf_queue_init(&fh->cap, &video_qops,
1237 dev->pci, &dev->slock,
1238 V4L2_BUF_TYPE_VIDEO_CAPTURE,
1239 V4L2_FIELD_INTERLACED,
1240 sizeof(struct saa7134_buf));
1241 init_MUTEX(&fh->cap.lock);
1242 saa7134_pgtable_alloc(dev->pci,&fh->pt_cap);
1244 videobuf_queue_init(&fh->vbi, &saa7134_vbi_qops,
1245 dev->pci, &dev->slock,
1246 V4L2_BUF_TYPE_VBI_CAPTURE,
1248 sizeof(struct saa7134_buf));
1249 init_MUTEX(&fh->vbi.lock);
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(file, saa7134_queue(fh),
1274 case V4L2_BUF_TYPE_VBI_CAPTURE:
1275 if (!res_get(fh->dev,fh,RESOURCE_VBI))
1277 return videobuf_read_stream(file, saa7134_queue(fh),
1278 data, count, ppos, 1);
1287 video_poll(struct file *file, struct poll_table_struct *wait)
1289 struct saa7134_fh *fh = file->private_data;
1290 struct videobuf_buffer *buf = NULL;
1292 if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type)
1293 return videobuf_poll_stream(file, &fh->vbi, wait);
1295 if (res_check(fh,RESOURCE_VIDEO)) {
1296 if (!list_empty(&fh->cap.stream))
1297 buf = list_entry(fh->cap.stream.next, struct videobuf_buffer, stream);
1299 down(&fh->cap.lock);
1300 if (UNSET == fh->cap.read_off) {
1301 /* need to capture a new frame */
1302 if (res_locked(fh->dev,RESOURCE_VIDEO)) {
1306 if (0 != fh->cap.ops->buf_prepare(file,fh->cap.read_buf,fh->cap.field)) {
1310 fh->cap.ops->buf_queue(file,fh->cap.read_buf);
1311 fh->cap.read_off = 0;
1314 buf = fh->cap.read_buf;
1320 poll_wait(file, &buf->done, wait);
1321 if (buf->state == STATE_DONE ||
1322 buf->state == STATE_ERROR)
1323 return POLLIN|POLLRDNORM;
1327 static int video_release(struct inode *inode, struct file *file)
1329 struct saa7134_fh *fh = file->private_data;
1330 struct saa7134_dev *dev = fh->dev;
1331 unsigned long flags;
1333 /* turn off overlay */
1334 if (res_check(fh, RESOURCE_OVERLAY)) {
1335 spin_lock_irqsave(&dev->slock,flags);
1336 stop_preview(dev,fh);
1337 spin_unlock_irqrestore(&dev->slock,flags);
1338 res_free(dev,fh,RESOURCE_OVERLAY);
1341 /* stop video capture */
1342 if (res_check(fh, RESOURCE_VIDEO)) {
1343 videobuf_streamoff(file,&fh->cap);
1344 res_free(dev,fh,RESOURCE_VIDEO);
1346 if (fh->cap.read_buf) {
1347 buffer_release(file,fh->cap.read_buf);
1348 kfree(fh->cap.read_buf);
1351 /* stop vbi capture */
1352 if (res_check(fh, RESOURCE_VBI)) {
1353 if (fh->vbi.streaming)
1354 videobuf_streamoff(file,&fh->vbi);
1355 if (fh->vbi.reading)
1356 videobuf_read_stop(file,&fh->vbi);
1357 res_free(dev,fh,RESOURCE_VBI);
1360 saa7134_pgtable_free(dev->pci,&fh->pt_cap);
1361 saa7134_pgtable_free(dev->pci,&fh->pt_vbi);
1363 #ifdef VIDIOC_G_PRIORITY
1364 v4l2_prio_close(&dev->prio,&fh->prio);
1366 file->private_data = NULL;
1372 video_mmap(struct file *file, struct vm_area_struct * vma)
1374 struct saa7134_fh *fh = file->private_data;
1376 return videobuf_mmap_mapper(vma,saa7134_queue(fh));
1379 /* ------------------------------------------------------------------ */
1381 void saa7134_vbi_fmt(struct saa7134_dev *dev, struct v4l2_format *f)
1383 struct saa7134_tvnorm *norm = dev->tvnorm;
1385 f->fmt.vbi.sampling_rate = 6750000 * 4;
1386 f->fmt.vbi.samples_per_line = 2048 /* VBI_LINE_LENGTH */;
1387 f->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
1388 f->fmt.vbi.offset = 64 * 4;
1389 f->fmt.vbi.start[0] = norm->vbi_v_start;
1390 f->fmt.vbi.count[0] = norm->vbi_v_stop - norm->vbi_v_start +1;
1391 f->fmt.vbi.start[1] = norm->video_v_stop + norm->vbi_v_start +1;
1392 f->fmt.vbi.count[1] = f->fmt.vbi.count[0];
1393 f->fmt.vbi.flags = 0; /* VBI_UNSYNC VBI_INTERLACED */
1396 if (V4L2_STD_PAL == norm->id) {
1398 f->fmt.vbi.start[0] += 3;
1399 f->fmt.vbi.start[1] += 3*2;
1404 int saa7134_g_fmt(struct saa7134_dev *dev, struct saa7134_fh *fh,
1405 struct v4l2_format *f)
1408 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1409 memset(&f->fmt.pix,0,sizeof(f->fmt.pix));
1410 f->fmt.pix.width = fh->width;
1411 f->fmt.pix.height = fh->height;
1412 f->fmt.pix.field = fh->cap.field;
1413 f->fmt.pix.pixelformat = fh->fmt->fourcc;
1414 f->fmt.pix.bytesperline =
1415 (f->fmt.pix.width * fh->fmt->depth) >> 3;
1416 f->fmt.pix.sizeimage =
1417 f->fmt.pix.height * f->fmt.pix.bytesperline;
1419 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
1420 f->fmt.win = fh->win;
1422 case V4L2_BUF_TYPE_VBI_CAPTURE:
1423 saa7134_vbi_fmt(dev,f);
1430 int saa7134_try_fmt(struct saa7134_dev *dev, struct saa7134_fh *fh,
1431 struct v4l2_format *f)
1436 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1438 struct saa7134_format *fmt;
1439 enum v4l2_field field;
1440 unsigned int maxw, maxh;
1442 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1446 field = f->fmt.pix.field;
1447 maxw = min(dev->crop_current.width*4, dev->crop_bounds.width);
1448 maxh = min(dev->crop_current.height*4, dev->crop_bounds.height);
1450 if (V4L2_FIELD_ANY == field) {
1451 field = (f->fmt.pix.height > maxh/2)
1452 ? V4L2_FIELD_INTERLACED
1453 : V4L2_FIELD_BOTTOM;
1456 case V4L2_FIELD_TOP:
1457 case V4L2_FIELD_BOTTOM:
1460 case V4L2_FIELD_INTERLACED:
1466 f->fmt.pix.field = field;
1467 if (f->fmt.pix.width < 48)
1468 f->fmt.pix.width = 48;
1469 if (f->fmt.pix.height < 32)
1470 f->fmt.pix.height = 32;
1471 if (f->fmt.pix.width > maxw)
1472 f->fmt.pix.width = maxw;
1473 if (f->fmt.pix.height > maxh)
1474 f->fmt.pix.height = maxh;
1475 f->fmt.pix.bytesperline =
1476 (f->fmt.pix.width * fmt->depth) >> 3;
1477 f->fmt.pix.sizeimage =
1478 f->fmt.pix.height * f->fmt.pix.bytesperline;
1482 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
1483 err = verify_preview(dev,&f->fmt.win);
1487 case V4L2_BUF_TYPE_VBI_CAPTURE:
1488 saa7134_vbi_fmt(dev,f);
1495 int saa7134_s_fmt(struct saa7134_dev *dev, struct saa7134_fh *fh,
1496 struct v4l2_format *f)
1498 unsigned long flags;
1502 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1503 err = saa7134_try_fmt(dev,fh,f);
1507 fh->fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1508 fh->width = f->fmt.pix.width;
1509 fh->height = f->fmt.pix.height;
1510 fh->cap.field = f->fmt.pix.field;
1512 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
1513 err = verify_preview(dev,&f->fmt.win);
1518 fh->win = f->fmt.win;
1519 fh->nclips = f->fmt.win.clipcount;
1522 if (copy_from_user(fh->clips,f->fmt.win.clips,
1523 sizeof(struct v4l2_clip)*fh->nclips)) {
1528 if (res_check(fh, RESOURCE_OVERLAY)) {
1529 spin_lock_irqsave(&dev->slock,flags);
1530 stop_preview(dev,fh);
1531 start_preview(dev,fh);
1532 spin_unlock_irqrestore(&dev->slock,flags);
1536 case V4L2_BUF_TYPE_VBI_CAPTURE:
1537 saa7134_vbi_fmt(dev,f);
1544 int saa7134_common_ioctl(struct saa7134_dev *dev,
1545 unsigned int cmd, void *arg)
1550 case VIDIOC_QUERYCTRL:
1552 const struct v4l2_queryctrl *ctrl;
1553 struct v4l2_queryctrl *c = arg;
1555 if ((c->id < V4L2_CID_BASE ||
1556 c->id >= V4L2_CID_LASTP1) &&
1557 (c->id < V4L2_CID_PRIVATE_BASE ||
1558 c->id >= V4L2_CID_PRIVATE_LASTP1))
1560 ctrl = ctrl_by_id(c->id);
1561 *c = (NULL != ctrl) ? *ctrl : no_ctrl;
1565 return get_control(dev,arg);
1569 err = set_control(dev,NULL,arg);
1573 /* --- input switching --------------------------------------- */
1574 case VIDIOC_ENUMINPUT:
1576 struct v4l2_input *i = arg;
1580 if (n >= SAA7134_INPUT_MAX)
1582 if (NULL == card_in(dev,i->index).name)
1584 memset(i,0,sizeof(*i));
1586 i->type = V4L2_INPUT_TYPE_CAMERA;
1587 strcpy(i->name,card_in(dev,n).name);
1588 if (card_in(dev,n).tv)
1589 i->type = V4L2_INPUT_TYPE_TUNER;
1591 if (n == dev->ctl_input) {
1592 int v1 = saa_readb(SAA7134_STATUS_VIDEO1);
1593 int v2 = saa_readb(SAA7134_STATUS_VIDEO2);
1595 if (0 != (v1 & 0x40))
1596 i->status |= V4L2_IN_ST_NO_H_LOCK;
1597 if (0 != (v2 & 0x40))
1598 i->status |= V4L2_IN_ST_NO_SYNC;
1599 if (0 != (v2 & 0x0e))
1600 i->status |= V4L2_IN_ST_MACROVISION;
1602 for (n = 0; n < TVNORMS; n++)
1603 i->std |= tvnorms[n].id;
1606 case VIDIOC_G_INPUT:
1609 *i = dev->ctl_input;
1612 case VIDIOC_S_INPUT:
1616 if (*i < 0 || *i >= SAA7134_INPUT_MAX)
1618 if (NULL == card_in(dev,*i).name)
1631 * This function is _not_ called directly, but from
1632 * video_generic_ioctl (and maybe others). userspace
1633 * copying is done already, arg is a kernel pointer.
1635 static int video_do_ioctl(struct inode *inode, struct file *file,
1636 unsigned int cmd, void *arg)
1638 struct saa7134_fh *fh = file->private_data;
1639 struct saa7134_dev *dev = fh->dev;
1640 unsigned long flags;
1643 if (video_debug > 1)
1644 saa7134_print_ioctl(dev->name,cmd);
1646 #ifdef VIDIOC_G_PRIORITY
1650 case VIDIOC_S_INPUT:
1651 case VIDIOC_S_TUNER:
1652 case VIDIOC_S_FREQUENCY:
1653 err = v4l2_prio_check(&dev->prio,&fh->prio);
1660 case VIDIOC_QUERYCAP:
1662 struct v4l2_capability *cap = arg;
1664 memset(cap,0,sizeof(*cap));
1665 strcpy(cap->driver, "saa7134");
1666 strlcpy(cap->card, saa7134_boards[dev->board].name,
1668 sprintf(cap->bus_info,"PCI:%s",pci_name(dev->pci));
1669 cap->version = SAA7134_VERSION_CODE;
1671 V4L2_CAP_VIDEO_CAPTURE |
1672 V4L2_CAP_VIDEO_OVERLAY |
1673 V4L2_CAP_VBI_CAPTURE |
1675 V4L2_CAP_READWRITE |
1680 /* --- tv standards ------------------------------------------ */
1681 case VIDIOC_ENUMSTD:
1683 struct v4l2_standard *e = arg;
1689 err = v4l2_video_std_construct(e, tvnorms[e->index].id,
1690 tvnorms[e->index].name);
1698 v4l2_std_id *id = arg;
1700 *id = dev->tvnorm->id;
1705 v4l2_std_id *id = arg;
1708 for (i = 0; i < TVNORMS; i++)
1709 if (*id == tvnorms[i].id)
1712 for (i = 0; i < TVNORMS; i++)
1713 if (*id & tvnorms[i].id)
1719 if (res_check(fh, RESOURCE_OVERLAY)) {
1720 spin_lock_irqsave(&dev->slock,flags);
1721 stop_preview(dev,fh);
1722 set_tvnorm(dev,&tvnorms[i]);
1723 start_preview(dev,fh);
1724 spin_unlock_irqrestore(&dev->slock,flags);
1726 set_tvnorm(dev,&tvnorms[i]);
1727 saa7134_tvaudio_do_scan(dev);
1732 case VIDIOC_CROPCAP:
1734 struct v4l2_cropcap *cap = arg;
1736 if (cap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1737 cap->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
1739 cap->bounds = dev->crop_bounds;
1740 cap->defrect = dev->crop_defrect;
1741 cap->pixelaspect.numerator = 1;
1742 cap->pixelaspect.denominator = 1;
1743 if (dev->tvnorm->id & V4L2_STD_525_60) {
1744 cap->pixelaspect.numerator = 11;
1745 cap->pixelaspect.denominator = 10;
1747 if (dev->tvnorm->id & V4L2_STD_625_50) {
1748 cap->pixelaspect.numerator = 54;
1749 cap->pixelaspect.denominator = 59;
1756 struct v4l2_crop * crop = arg;
1758 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1759 crop->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
1761 crop->c = dev->crop_current;
1766 struct v4l2_crop *crop = arg;
1767 struct v4l2_rect *b = &dev->crop_bounds;
1769 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1770 crop->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
1772 if (crop->c.height < 0)
1774 if (crop->c.width < 0)
1777 if (res_locked(fh->dev,RESOURCE_OVERLAY))
1779 if (res_locked(fh->dev,RESOURCE_VIDEO))
1782 if (crop->c.top < b->top)
1783 crop->c.top = b->top;
1784 if (crop->c.top > b->top + b->height)
1785 crop->c.top = b->top + b->height;
1786 if (crop->c.height > b->top - crop->c.top + b->height)
1787 crop->c.height = b->top - crop->c.top + b->height;
1789 if (crop->c.left < b->left)
1790 crop->c.top = b->left;
1791 if (crop->c.left > b->left + b->width)
1792 crop->c.top = b->left + b->width;
1793 if (crop->c.width > b->left - crop->c.left + b->width)
1794 crop->c.width = b->left - crop->c.left + b->width;
1796 dev->crop_current = crop->c;
1800 /* --- tuner ioctls ------------------------------------------ */
1801 case VIDIOC_G_TUNER:
1803 struct v4l2_tuner *t = arg;
1808 memset(t,0,sizeof(*t));
1809 for (n = 0; n < SAA7134_INPUT_MAX; n++)
1810 if (card_in(dev,n).tv)
1812 if (NULL != card_in(dev,n).name) {
1813 strcpy(t->name, "Television");
1814 t->capability = V4L2_TUNER_CAP_NORM |
1815 V4L2_TUNER_CAP_STEREO |
1816 V4L2_TUNER_CAP_LANG1 |
1817 V4L2_TUNER_CAP_LANG2;
1818 t->rangehigh = 0xffffffffUL;
1819 t->rxsubchans = saa7134_tvaudio_getstereo(dev);
1820 t->audmode = saa7134_tvaudio_rx2mode(t->rxsubchans);
1822 if (0 != (saa_readb(SAA7134_STATUS_VIDEO1) & 0x03))
1826 case VIDIOC_S_TUNER:
1828 struct v4l2_tuner *t = arg;
1831 mode = dev->thread.mode;
1832 if (UNSET == mode) {
1833 rx = saa7134_tvaudio_getstereo(dev);
1834 mode = saa7134_tvaudio_rx2mode(t->rxsubchans);
1836 if (mode != t->audmode) {
1837 dev->thread.mode = t->audmode;
1841 case VIDIOC_G_FREQUENCY:
1843 struct v4l2_frequency *f = arg;
1845 memset(f,0,sizeof(*f));
1846 f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1847 f->frequency = dev->ctl_freq;
1850 case VIDIOC_S_FREQUENCY:
1852 struct v4l2_frequency *f = arg;
1856 if (0 == fh->radio && V4L2_TUNER_ANALOG_TV != f->type)
1858 if (1 == fh->radio && V4L2_TUNER_RADIO != f->type)
1861 dev->ctl_freq = f->frequency;
1862 #ifdef V4L2_I2C_CLIENTS
1863 saa7134_i2c_call_clients(dev,VIDIOC_S_FREQUENCY,f);
1865 saa7134_i2c_call_clients(dev,VIDIOCSFREQ,&dev->ctl_freq);
1867 saa7134_tvaudio_do_scan(dev);
1872 /* --- control ioctls ---------------------------------------- */
1873 case VIDIOC_ENUMINPUT:
1874 case VIDIOC_G_INPUT:
1875 case VIDIOC_S_INPUT:
1876 case VIDIOC_QUERYCTRL:
1879 return saa7134_common_ioctl(dev, cmd, arg);
1881 case VIDIOC_G_AUDIO:
1883 struct v4l2_audio *a = arg;
1885 memset(a,0,sizeof(*a));
1886 strcpy(a->name,"audio");
1889 case VIDIOC_S_AUDIO:
1893 struct v4l2_captureparm *parm = arg;
1894 memset(parm,0,sizeof(*parm));
1898 #ifdef VIDIOC_G_PRIORITY
1899 case VIDIOC_G_PRIORITY:
1901 enum v4l2_priority *p = arg;
1903 *p = v4l2_prio_max(&dev->prio);
1906 case VIDIOC_S_PRIORITY:
1908 enum v4l2_priority *prio = arg;
1910 return v4l2_prio_change(&dev->prio, &fh->prio, *prio);
1914 /* --- preview ioctls ---------------------------------------- */
1915 case VIDIOC_ENUM_FMT:
1917 struct v4l2_fmtdesc *f = arg;
1918 enum v4l2_buf_type type;
1924 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1925 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
1926 if (index >= FORMATS)
1928 if (f->type == V4L2_BUF_TYPE_VIDEO_OVERLAY &&
1929 formats[index].planar)
1931 memset(f,0,sizeof(*f));
1934 strlcpy(f->description,formats[index].name,sizeof(f->description));
1935 f->pixelformat = formats[index].fourcc;
1937 case V4L2_BUF_TYPE_VBI_CAPTURE:
1940 memset(f,0,sizeof(*f));
1943 f->pixelformat = V4L2_PIX_FMT_GREY;
1944 strcpy(f->description,"vbi data");
1953 struct v4l2_framebuffer *fb = arg;
1956 fb->capability = V4L2_FBUF_CAP_LIST_CLIPPING;
1961 struct v4l2_framebuffer *fb = arg;
1962 struct saa7134_format *fmt;
1964 if(!capable(CAP_SYS_ADMIN) &&
1965 !capable(CAP_SYS_RAWIO))
1969 fmt = format_by_fourcc(fb->fmt.pixelformat);
1976 if (0 == dev->ovbuf.fmt.bytesperline)
1977 dev->ovbuf.fmt.bytesperline =
1978 dev->ovbuf.fmt.width*fmt->depth/8;
1981 case VIDIOC_OVERLAY:
1986 if (!res_get(dev,fh,RESOURCE_OVERLAY))
1988 spin_lock_irqsave(&dev->slock,flags);
1989 start_preview(dev,fh);
1990 spin_unlock_irqrestore(&dev->slock,flags);
1993 if (!res_check(fh, RESOURCE_OVERLAY))
1995 spin_lock_irqsave(&dev->slock,flags);
1996 stop_preview(dev,fh);
1997 spin_unlock_irqrestore(&dev->slock,flags);
1998 res_free(dev,fh,RESOURCE_OVERLAY);
2003 /* --- capture ioctls ---------------------------------------- */
2006 struct v4l2_format *f = arg;
2007 return saa7134_g_fmt(dev,fh,f);
2011 struct v4l2_format *f = arg;
2012 return saa7134_s_fmt(dev,fh,f);
2014 case VIDIOC_TRY_FMT:
2016 struct v4l2_format *f = arg;
2017 return saa7134_try_fmt(dev,fh,f);
2022 struct video_mbuf *mbuf = arg;
2023 struct videobuf_queue *q;
2024 struct v4l2_requestbuffers req;
2027 q = saa7134_queue(fh);
2028 memset(&req,0,sizeof(req));
2030 req.count = gbuffers;
2031 req.memory = V4L2_MEMORY_MMAP;
2032 err = videobuf_reqbufs(file,q,&req);
2035 memset(mbuf,0,sizeof(*mbuf));
2036 mbuf->frames = req.count;
2038 for (i = 0; i < mbuf->frames; i++) {
2039 mbuf->offsets[i] = q->bufs[i]->boff;
2040 mbuf->size += q->bufs[i]->bsize;
2044 case VIDIOC_REQBUFS:
2045 return videobuf_reqbufs(file,saa7134_queue(fh),arg);
2047 case VIDIOC_QUERYBUF:
2048 return videobuf_querybuf(saa7134_queue(fh),arg);
2051 return videobuf_qbuf(file,saa7134_queue(fh),arg);
2054 return videobuf_dqbuf(file,saa7134_queue(fh),arg);
2056 case VIDIOC_STREAMON:
2058 int res = saa7134_resource(fh);
2060 if (!res_get(dev,fh,res))
2062 return videobuf_streamon(file,saa7134_queue(fh));
2064 case VIDIOC_STREAMOFF:
2066 int res = saa7134_resource(fh);
2068 err = videobuf_streamoff(file,saa7134_queue(fh));
2071 res_free(dev,fh,res);
2076 return v4l_compat_translate_ioctl(inode,file,cmd,arg,
2082 static int video_ioctl(struct inode *inode, struct file *file,
2083 unsigned int cmd, unsigned long arg)
2085 return video_usercopy(inode, file, cmd, arg, video_do_ioctl);
2088 static int radio_do_ioctl(struct inode *inode, struct file *file,
2089 unsigned int cmd, void *arg)
2091 struct saa7134_fh *fh = file->private_data;
2092 struct saa7134_dev *dev = fh->dev;
2094 if (video_debug > 1)
2095 saa7134_print_ioctl(dev->name,cmd);
2097 case VIDIOC_QUERYCAP:
2099 struct v4l2_capability *cap = arg;
2101 memset(cap,0,sizeof(*cap));
2102 strcpy(cap->driver, "saa7134");
2103 strlcpy(cap->card, saa7134_boards[dev->board].name,
2105 sprintf(cap->bus_info,"PCI:%s",pci_name(dev->pci));
2106 cap->version = SAA7134_VERSION_CODE;
2107 cap->capabilities = V4L2_CAP_TUNER;
2110 case VIDIOC_G_TUNER:
2112 struct v4l2_tuner *t = arg;
2117 memset(t,0,sizeof(*t));
2118 strcpy(t->name, "Radio");
2119 t->rangelow = (int)(65*16);
2120 t->rangehigh = (int)(108*16);
2122 #ifdef V4L2_I2C_CLIENTS
2123 saa7134_i2c_call_clients(dev,VIDIOC_G_TUNER,t);
2126 struct video_tuner vt;
2127 memset(&vt,0,sizeof(vt));
2128 saa7134_i2c_call_clients(dev,VIDIOCGTUNER,&vt);
2129 t->signal = vt.signal;
2134 case VIDIOC_ENUMINPUT:
2136 struct v4l2_input *i = arg;
2140 strcpy(i->name,"Radio");
2141 i->type = V4L2_INPUT_TYPE_TUNER;
2144 case VIDIOC_G_INPUT:
2150 case VIDIOC_G_AUDIO:
2152 struct v4l2_audio *a = arg;
2154 memset(a,0,sizeof(*a));
2155 strcpy(a->name,"Radio");
2160 v4l2_std_id *id = arg;
2164 case VIDIOC_S_AUDIO:
2165 case VIDIOC_S_TUNER:
2166 case VIDIOC_S_INPUT:
2170 case VIDIOC_QUERYCTRL:
2172 const struct v4l2_queryctrl *ctrl;
2173 struct v4l2_queryctrl *c = arg;
2175 if (c->id < V4L2_CID_BASE ||
2176 c->id >= V4L2_CID_LASTP1)
2178 if (c->id == V4L2_CID_AUDIO_MUTE) {
2179 ctrl = ctrl_by_id(c->id);
2188 case VIDIOC_G_FREQUENCY:
2189 case VIDIOC_S_FREQUENCY:
2190 return video_do_ioctl(inode,file,cmd,arg);
2193 return v4l_compat_translate_ioctl(inode,file,cmd,arg,
2199 static int radio_ioctl(struct inode *inode, struct file *file,
2200 unsigned int cmd, unsigned long arg)
2202 return video_usercopy(inode, file, cmd, arg, radio_do_ioctl);
2205 static struct file_operations video_fops =
2207 .owner = THIS_MODULE,
2209 .release = video_release,
2213 .ioctl = video_ioctl,
2214 .llseek = no_llseek,
2217 static struct file_operations radio_fops =
2219 .owner = THIS_MODULE,
2221 .release = video_release,
2222 .ioctl = radio_ioctl,
2223 .llseek = no_llseek,
2226 /* ----------------------------------------------------------- */
2227 /* exported stuff */
2229 struct video_device saa7134_video_template =
2231 .name = "saa7134-video",
2232 .type = VID_TYPE_CAPTURE|VID_TYPE_TUNER|VID_TYPE_OVERLAY|
2233 VID_TYPE_CLIPPING|VID_TYPE_SCALES,
2235 .fops = &video_fops,
2239 struct video_device saa7134_vbi_template =
2241 .name = "saa7134-vbi",
2242 .type = VID_TYPE_TUNER|VID_TYPE_TELETEXT,
2244 .fops = &video_fops,
2248 struct video_device saa7134_radio_template =
2250 .name = "saa7134-radio",
2251 .type = VID_TYPE_TUNER,
2253 .fops = &radio_fops,
2257 int saa7134_video_init1(struct saa7134_dev *dev)
2259 /* sanitycheck insmod options */
2260 if (gbuffers < 2 || gbuffers > VIDEO_MAX_FRAME)
2262 if (gbufsize < 0 || gbufsize > gbufsize_max)
2263 gbufsize = gbufsize_max;
2264 gbufsize = (gbufsize + PAGE_SIZE - 1) & PAGE_MASK;
2266 /* put some sensible defaults into the data structures ... */
2267 dev->ctl_bright = ctrl_by_id(V4L2_CID_BRIGHTNESS)->default_value;
2268 dev->ctl_contrast = ctrl_by_id(V4L2_CID_CONTRAST)->default_value;
2269 dev->ctl_hue = ctrl_by_id(V4L2_CID_HUE)->default_value;
2270 dev->ctl_saturation = ctrl_by_id(V4L2_CID_SATURATION)->default_value;
2271 dev->ctl_volume = ctrl_by_id(V4L2_CID_AUDIO_VOLUME)->default_value;
2272 dev->ctl_mute = ctrl_by_id(V4L2_CID_AUDIO_MUTE)->default_value;
2273 dev->ctl_invert = ctrl_by_id(V4L2_CID_PRIVATE_INVERT)->default_value;
2274 dev->ctl_automute = ctrl_by_id(V4L2_CID_PRIVATE_AUTOMUTE)->default_value;
2276 if (dev->tda9887_conf && dev->ctl_automute)
2277 dev->tda9887_conf |= TDA9887_AUTOMUTE;
2280 INIT_LIST_HEAD(&dev->video_q.queue);
2281 init_timer(&dev->video_q.timeout);
2282 dev->video_q.timeout.function = saa7134_buffer_timeout;
2283 dev->video_q.timeout.data = (unsigned long)(&dev->video_q);
2284 dev->video_q.dev = dev;
2286 if (saa7134_boards[dev->board].video_out) {
2287 /* enable video output */
2288 int vo = saa7134_boards[dev->board].video_out;
2289 saa_writeb(SAA7134_VIDEO_PORT_CTRL0, video_out[vo][0]);
2290 saa_writeb(SAA7134_VIDEO_PORT_CTRL1, video_out[vo][1]);
2291 saa_writeb(SAA7134_VIDEO_PORT_CTRL2, video_out[vo][2]);
2292 saa_writeb(SAA7134_VIDEO_PORT_CTRL3, video_out[vo][3]);
2293 saa_writeb(SAA7134_VIDEO_PORT_CTRL4, video_out[vo][4]);
2294 saa_writeb(SAA7134_VIDEO_PORT_CTRL5, video_out[vo][5]);
2295 saa_writeb(SAA7134_VIDEO_PORT_CTRL6, video_out[vo][6]);
2296 saa_writeb(SAA7134_VIDEO_PORT_CTRL7, video_out[vo][7]);
2297 saa_writeb(SAA7134_VIDEO_PORT_CTRL8, video_out[vo][8]);
2303 int saa7134_video_init2(struct saa7134_dev *dev)
2306 set_tvnorm(dev,&tvnorms[0]);
2308 saa7134_tvaudio_setmute(dev);
2309 saa7134_tvaudio_setvolume(dev,dev->ctl_volume);
2313 int saa7134_video_fini(struct saa7134_dev *dev)
2319 void saa7134_irq_video_intl(struct saa7134_dev *dev)
2321 static const char *st[] = {
2322 "no signal", "found NTSC", "found PAL", "found SECAM" };
2325 norm = saa_readb(SAA7134_STATUS_VIDEO1) & 0x03;
2326 dprintk("DCSDT: %s\n",st[norm]);
2329 /* wake up tvaudio audio carrier scan thread */
2330 saa7134_tvaudio_do_scan(dev);
2332 saa_clearb(SAA7134_SYNC_CTRL, 0x20);
2334 /* no video signal -> mute audio */
2335 if (dev->ctl_automute)
2337 saa7134_tvaudio_setmute(dev);
2338 saa_setb(SAA7134_SYNC_CTRL, 0x20);
2342 void saa7134_irq_video_done(struct saa7134_dev *dev, unsigned long status)
2344 enum v4l2_field field;
2346 spin_lock(&dev->slock);
2347 if (dev->video_q.curr) {
2348 dev->video_fieldcount++;
2349 field = dev->video_q.curr->vb.field;
2350 if (V4L2_FIELD_HAS_BOTH(field)) {
2351 /* make sure we have seen both fields */
2352 if ((status & 0x10) == 0x00) {
2353 dev->video_q.curr->top_seen = 1;
2356 if (!dev->video_q.curr->top_seen)
2358 } else if (field == V4L2_FIELD_TOP) {
2359 if ((status & 0x10) != 0x10)
2361 } else if (field == V4L2_FIELD_BOTTOM) {
2362 if ((status & 0x10) != 0x00)
2365 dev->video_q.curr->vb.field_count = dev->video_fieldcount;
2366 saa7134_buffer_finish(dev,&dev->video_q,STATE_DONE);
2368 saa7134_buffer_next(dev,&dev->video_q);
2371 spin_unlock(&dev->slock);
2374 /* ----------------------------------------------------------- */