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_LASTP1 (V4L2_CID_PRIVATE_BASE + 3)
277 static const struct v4l2_queryctrl no_ctrl = {
279 .flags = V4L2_CTRL_FLAG_DISABLED,
281 static const struct v4l2_queryctrl video_ctrls[] = {
284 .id = V4L2_CID_BRIGHTNESS,
285 .name = "Brightness",
289 .default_value = 128,
290 .type = V4L2_CTRL_TYPE_INTEGER,
292 .id = V4L2_CID_CONTRAST,
298 .type = V4L2_CTRL_TYPE_INTEGER,
300 .id = V4L2_CID_SATURATION,
301 .name = "Saturation",
306 .type = V4L2_CTRL_TYPE_INTEGER,
314 .type = V4L2_CTRL_TYPE_INTEGER,
316 .id = V4L2_CID_VFLIP,
317 .name = "vertical flip",
320 .type = V4L2_CTRL_TYPE_BOOLEAN,
324 .id = V4L2_CID_AUDIO_MUTE,
328 .type = V4L2_CTRL_TYPE_BOOLEAN,
330 .id = V4L2_CID_AUDIO_VOLUME,
336 .type = V4L2_CTRL_TYPE_INTEGER,
338 /* --- private --- */
340 .id = V4L2_CID_PRIVATE_INVERT,
344 .type = V4L2_CTRL_TYPE_BOOLEAN,
346 .id = V4L2_CID_PRIVATE_Y_ODD,
347 .name = "y offset odd field",
351 .type = V4L2_CTRL_TYPE_INTEGER,
353 .id = V4L2_CID_PRIVATE_Y_EVEN,
354 .name = "y offset even field",
358 .type = V4L2_CTRL_TYPE_INTEGER,
361 static const unsigned int CTRLS = ARRAY_SIZE(video_ctrls);
363 static const struct v4l2_queryctrl* ctrl_by_id(unsigned int id)
367 for (i = 0; i < CTRLS; i++)
368 if (video_ctrls[i].id == id)
369 return video_ctrls+i;
373 static struct saa7134_format* format_by_fourcc(unsigned int fourcc)
377 for (i = 0; i < FORMATS; i++)
378 if (formats[i].fourcc == fourcc)
383 /* ----------------------------------------------------------------------- */
384 /* resource management */
386 static int res_get(struct saa7134_dev *dev, struct saa7134_fh *fh, unsigned int bit)
388 if (fh->resources & bit)
389 /* have it already allocated */
394 if (dev->resources & bit) {
395 /* no, someone else uses it */
399 /* it's free, grab it */
400 fh->resources |= bit;
401 dev->resources |= bit;
402 dprintk("res: get %d\n",bit);
408 int res_check(struct saa7134_fh *fh, unsigned int bit)
410 return (fh->resources & bit);
414 int res_locked(struct saa7134_dev *dev, unsigned int bit)
416 return (dev->resources & bit);
420 void res_free(struct saa7134_dev *dev, struct saa7134_fh *fh, unsigned int bits)
422 if ((fh->resources & bits) != bits)
426 fh->resources &= ~bits;
427 dev->resources &= ~bits;
428 dprintk("res: put %d\n",bits);
432 /* ------------------------------------------------------------------ */
434 static void set_tvnorm(struct saa7134_dev *dev, struct saa7134_tvnorm *norm)
436 int luma_control,sync_control,mux;
438 dprintk("set tv norm = %s\n",norm->name);
441 mux = card_in(dev,dev->ctl_input).vmux;
442 luma_control = norm->luma_control;
443 sync_control = norm->sync_control;
446 luma_control |= 0x80; /* svideo */
448 sync_control |= 0x20;
451 dev->crop_bounds.left = norm->h_start;
452 dev->crop_defrect.left = norm->h_start;
453 dev->crop_bounds.width = norm->h_stop - norm->h_start +1;
454 dev->crop_defrect.width = norm->h_stop - norm->h_start +1;
456 dev->crop_bounds.top = (norm->vbi_v_stop+1)*2;
457 dev->crop_defrect.top = norm->video_v_start*2;
458 dev->crop_bounds.height = ((norm->id & V4L2_STD_525_60) ? 524 : 624)
459 - dev->crop_bounds.top;
460 dev->crop_defrect.height = (norm->video_v_stop - norm->video_v_start +1)*2;
462 dev->crop_current = dev->crop_defrect;
464 /* setup video decoder */
465 saa_writeb(SAA7134_INCR_DELAY, 0x08);
466 saa_writeb(SAA7134_ANALOG_IN_CTRL1, 0xc0 | mux);
467 saa_writeb(SAA7134_ANALOG_IN_CTRL2, 0x00);
469 saa_writeb(SAA7134_ANALOG_IN_CTRL3, 0x90);
470 saa_writeb(SAA7134_ANALOG_IN_CTRL4, 0x90);
471 saa_writeb(SAA7134_HSYNC_START, 0xeb);
472 saa_writeb(SAA7134_HSYNC_STOP, 0xe0);
473 saa_writeb(SAA7134_SOURCE_TIMING1, norm->src_timing);
475 saa_writeb(SAA7134_SYNC_CTRL, sync_control);
476 saa_writeb(SAA7134_LUMA_CTRL, luma_control);
477 saa_writeb(SAA7134_DEC_LUMA_BRIGHT, dev->ctl_bright);
478 saa_writeb(SAA7134_DEC_LUMA_CONTRAST, dev->ctl_contrast);
480 saa_writeb(SAA7134_DEC_CHROMA_SATURATION, dev->ctl_saturation);
481 saa_writeb(SAA7134_DEC_CHROMA_HUE, dev->ctl_hue);
482 saa_writeb(SAA7134_CHROMA_CTRL1, norm->chroma_ctrl1);
483 saa_writeb(SAA7134_CHROMA_GAIN, norm->chroma_gain);
485 saa_writeb(SAA7134_CHROMA_CTRL2, norm->chroma_ctrl2);
486 saa_writeb(SAA7134_MODE_DELAY_CTRL, 0x00);
488 saa_writeb(SAA7134_ANALOG_ADC, 0x01);
489 saa_writeb(SAA7134_VGATE_START, 0x11);
490 saa_writeb(SAA7134_VGATE_STOP, 0xfe);
491 saa_writeb(SAA7134_MISC_VGATE_MSB, norm->vgate_misc);
492 saa_writeb(SAA7134_RAW_DATA_GAIN, 0x40);
493 saa_writeb(SAA7134_RAW_DATA_OFFSET, 0x80);
495 #ifdef V4L2_I2C_CLIENTS
496 saa7134_i2c_call_clients(dev,VIDIOC_S_STD,&norm->id);
499 /* pass down info to the i2c chips (v4l1) */
500 struct video_channel c;
501 memset(&c,0,sizeof(c));
502 c.channel = dev->ctl_input;
503 c.norm = VIDEO_MODE_PAL;
504 if (norm->id & V4L2_STD_NTSC)
505 c.norm = VIDEO_MODE_NTSC;
506 if (norm->id & V4L2_STD_SECAM)
507 c.norm = VIDEO_MODE_SECAM;
508 saa7134_i2c_call_clients(dev,VIDIOCSCHAN,&c);
513 static void video_mux(struct saa7134_dev *dev, int input)
515 dprintk("video input = %d [%s]\n",input,card_in(dev,input).name);
516 dev->ctl_input = input;
517 set_tvnorm(dev,dev->tvnorm);
518 saa7134_tvaudio_setinput(dev,&card_in(dev,input));
521 static void set_h_prescale(struct saa7134_dev *dev, int task, int prescale)
523 static const struct {
530 /* XPSC XACL XC2_1 XDCG VPFY */
542 static const int count = ARRAY_SIZE(vals);
545 for (i = 0; i < count; i++)
546 if (vals[i].xpsc == prescale)
551 saa_writeb(SAA7134_H_PRESCALE(task), vals[i].xpsc);
552 saa_writeb(SAA7134_ACC_LENGTH(task), vals[i].xacl);
553 saa_writeb(SAA7134_LEVEL_CTRL(task),
554 (vals[i].xc2_1 << 3) | (vals[i].xdcg));
555 saa_andorb(SAA7134_FIR_PREFILTER_CTRL(task), 0x0f,
556 (vals[i].vpfy << 2) | vals[i].vpfy);
559 static void set_v_scale(struct saa7134_dev *dev, int task, int yscale)
563 saa_writeb(SAA7134_V_SCALE_RATIO1(task), yscale & 0xff);
564 saa_writeb(SAA7134_V_SCALE_RATIO2(task), yscale >> 8);
566 mirror = (dev->ctl_mirror) ? 0x02 : 0x00;
569 dprintk("yscale LPI yscale=%d\n",yscale);
570 saa_writeb(SAA7134_V_FILTER(task), 0x00 | mirror);
571 saa_writeb(SAA7134_LUMA_CONTRAST(task), 0x40);
572 saa_writeb(SAA7134_CHROMA_SATURATION(task), 0x40);
575 val = 0x40 * 1024 / yscale;
576 dprintk("yscale ACM yscale=%d val=0x%x\n",yscale,val);
577 saa_writeb(SAA7134_V_FILTER(task), 0x01 | mirror);
578 saa_writeb(SAA7134_LUMA_CONTRAST(task), val);
579 saa_writeb(SAA7134_CHROMA_SATURATION(task), val);
581 saa_writeb(SAA7134_LUMA_BRIGHT(task), 0x80);
584 static void set_size(struct saa7134_dev *dev, int task,
585 int width, int height, int interlace)
587 int prescale,xscale,yscale,y_even,y_odd;
588 int h_start, h_stop, v_start, v_stop;
589 int div = interlace ? 2 : 1;
591 /* setup video scaler */
592 h_start = dev->crop_current.left;
593 v_start = dev->crop_current.top/2;
594 h_stop = (dev->crop_current.left + dev->crop_current.width -1);
595 v_stop = (dev->crop_current.top + dev->crop_current.height -1)/2;
597 saa_writeb(SAA7134_VIDEO_H_START1(task), h_start & 0xff);
598 saa_writeb(SAA7134_VIDEO_H_START2(task), h_start >> 8);
599 saa_writeb(SAA7134_VIDEO_H_STOP1(task), h_stop & 0xff);
600 saa_writeb(SAA7134_VIDEO_H_STOP2(task), h_stop >> 8);
601 saa_writeb(SAA7134_VIDEO_V_START1(task), v_start & 0xff);
602 saa_writeb(SAA7134_VIDEO_V_START2(task), v_start >> 8);
603 saa_writeb(SAA7134_VIDEO_V_STOP1(task), v_stop & 0xff);
604 saa_writeb(SAA7134_VIDEO_V_STOP2(task), v_stop >> 8);
606 prescale = dev->crop_current.width / width;
609 xscale = 1024 * dev->crop_current.width / prescale / width;
610 yscale = 512 * div * dev->crop_current.height / height;
611 dprintk("prescale=%d xscale=%d yscale=%d\n",prescale,xscale,yscale);
612 set_h_prescale(dev,task,prescale);
613 saa_writeb(SAA7134_H_SCALE_INC1(task), xscale & 0xff);
614 saa_writeb(SAA7134_H_SCALE_INC2(task), xscale >> 8);
615 set_v_scale(dev,task,yscale);
617 saa_writeb(SAA7134_VIDEO_PIXELS1(task), width & 0xff);
618 saa_writeb(SAA7134_VIDEO_PIXELS2(task), width >> 8);
619 saa_writeb(SAA7134_VIDEO_LINES1(task), height/div & 0xff);
620 saa_writeb(SAA7134_VIDEO_LINES2(task), height/div >> 8);
622 /* deinterlace y offsets */
623 y_odd = dev->ctl_y_odd;
624 y_even = dev->ctl_y_even;
625 saa_writeb(SAA7134_V_PHASE_OFFSET0(task), y_odd);
626 saa_writeb(SAA7134_V_PHASE_OFFSET1(task), y_even);
627 saa_writeb(SAA7134_V_PHASE_OFFSET2(task), y_odd);
628 saa_writeb(SAA7134_V_PHASE_OFFSET3(task), y_even);
631 /* ------------------------------------------------------------------ */
639 static void sort_cliplist(struct cliplist *cl, int entries)
641 struct cliplist swap;
644 for (i = entries-2; i >= 0; i--) {
645 for (n = 0, j = 0; j <= i; j++) {
646 if (cl[j].position > cl[j+1].position) {
658 static void set_cliplist(struct saa7134_dev *dev, int reg,
659 struct cliplist *cl, int entries, char *name)
664 for (i = 0; i < entries; i++) {
665 winbits |= cl[i].enable;
666 winbits &= ~cl[i].disable;
667 if (i < 15 && cl[i].position == cl[i+1].position)
669 saa_writeb(reg + 0, winbits);
670 saa_writeb(reg + 2, cl[i].position & 0xff);
671 saa_writeb(reg + 3, cl[i].position >> 8);
672 dprintk("clip: %s winbits=%02x pos=%d\n",
673 name,winbits,cl[i].position);
676 for (; reg < 0x400; reg += 8) {
677 saa_writeb(reg+ 0, 0);
678 saa_writeb(reg + 1, 0);
679 saa_writeb(reg + 2, 0);
680 saa_writeb(reg + 3, 0);
684 static int clip_range(int val)
691 static int setup_clipping(struct saa7134_dev *dev, struct v4l2_clip *clips,
692 int nclips, int interlace)
694 struct cliplist col[16], row[16];
696 int div = interlace ? 2 : 1;
698 memset(col,0,sizeof(col)); cols = 0;
699 memset(row,0,sizeof(row)); rows = 0;
700 for (i = 0; i < nclips && i < 8; i++) {
701 col[cols].position = clip_range(clips[i].c.left);
702 col[cols].enable = (1 << i);
704 col[cols].position = clip_range(clips[i].c.left+clips[i].c.width);
705 col[cols].disable = (1 << i);
707 row[rows].position = clip_range(clips[i].c.top / div);
708 row[rows].enable = (1 << i);
710 row[rows].position = clip_range((clips[i].c.top + clips[i].c.height)
712 row[rows].disable = (1 << i);
715 sort_cliplist(col,cols);
716 sort_cliplist(row,rows);
717 set_cliplist(dev,0x380,col,cols,"cols");
718 set_cliplist(dev,0x384,row,rows,"rows");
722 static int verify_preview(struct saa7134_dev *dev, struct v4l2_window *win)
724 enum v4l2_field field;
727 if (NULL == dev->ovbuf.base)
729 if (NULL == dev->ovfmt)
731 if (win->w.width < 48 || win->w.height < 32)
733 if (win->clipcount > 2048)
737 maxw = dev->crop_current.width;
738 maxh = dev->crop_current.height;
740 if (V4L2_FIELD_ANY == field) {
741 field = (win->w.height > maxh/2)
742 ? V4L2_FIELD_INTERLACED
747 case V4L2_FIELD_BOTTOM:
750 case V4L2_FIELD_INTERLACED:
757 if (win->w.width > maxw)
759 if (win->w.height > maxh)
760 win->w.height = maxh;
764 static int start_preview(struct saa7134_dev *dev, struct saa7134_fh *fh)
766 unsigned long base,control,bpl;
769 err = verify_preview(dev,&fh->win);
773 dev->ovfield = fh->win.field;
774 dprintk("start_preview %dx%d+%d+%d %s field=%s\n",
775 fh->win.w.width,fh->win.w.height,
776 fh->win.w.left,fh->win.w.top,
777 dev->ovfmt->name,v4l2_field_names[dev->ovfield]);
779 /* setup window + clipping */
780 set_size(dev,TASK_B,fh->win.w.width,fh->win.w.height,
781 V4L2_FIELD_HAS_BOTH(dev->ovfield));
782 setup_clipping(dev,fh->clips,fh->nclips,
783 V4L2_FIELD_HAS_BOTH(dev->ovfield));
785 saa_andorb(SAA7134_DATA_PATH(TASK_B), 0x3f, 0x03);
787 saa_andorb(SAA7134_DATA_PATH(TASK_B), 0x3f, 0x01);
788 saa_writeb(SAA7134_OFMT_VIDEO_B, dev->ovfmt->pm | 0x20);
790 /* dma: setup channel 1 (= Video Task B) */
791 base = (unsigned long)dev->ovbuf.base;
792 base += dev->ovbuf.fmt.bytesperline * fh->win.w.top;
793 base += dev->ovfmt->depth/8 * fh->win.w.left;
794 bpl = dev->ovbuf.fmt.bytesperline;
795 control = SAA7134_RS_CONTROL_BURST_16;
796 if (dev->ovfmt->bswap)
797 control |= SAA7134_RS_CONTROL_BSWAP;
798 if (dev->ovfmt->wswap)
799 control |= SAA7134_RS_CONTROL_WSWAP;
800 if (V4L2_FIELD_HAS_BOTH(dev->ovfield)) {
801 saa_writel(SAA7134_RS_BA1(1),base);
802 saa_writel(SAA7134_RS_BA2(1),base+bpl);
803 saa_writel(SAA7134_RS_PITCH(1),bpl*2);
804 saa_writel(SAA7134_RS_CONTROL(1),control);
806 saa_writel(SAA7134_RS_BA1(1),base);
807 saa_writel(SAA7134_RS_BA2(1),base);
808 saa_writel(SAA7134_RS_PITCH(1),bpl);
809 saa_writel(SAA7134_RS_CONTROL(1),control);
814 saa7134_set_dmabits(dev);
819 static int stop_preview(struct saa7134_dev *dev, struct saa7134_fh *fh)
822 saa7134_set_dmabits(dev);
826 /* ------------------------------------------------------------------ */
828 static int buffer_activate(struct saa7134_dev *dev,
829 struct saa7134_buf *buf,
830 struct saa7134_buf *next)
832 unsigned long base,control,bpl;
833 unsigned long bpl_uv,lines_uv,base2,base3,tmp; /* planar */
835 dprintk("buffer_activate buf=%p\n",buf);
836 buf->vb.state = STATE_ACTIVE;
839 set_size(dev,TASK_A,buf->vb.width,buf->vb.height,
840 V4L2_FIELD_HAS_BOTH(buf->vb.field));
842 saa_andorb(SAA7134_DATA_PATH(TASK_A), 0x3f, 0x03);
844 saa_andorb(SAA7134_DATA_PATH(TASK_A), 0x3f, 0x01);
845 saa_writeb(SAA7134_OFMT_VIDEO_A, buf->fmt->pm);
847 /* DMA: setup channel 0 (= Video Task A0) */
848 base = saa7134_buffer_base(buf);
849 if (buf->fmt->planar)
852 bpl = (buf->vb.width * buf->fmt->depth) / 8;
853 control = SAA7134_RS_CONTROL_BURST_16 |
854 SAA7134_RS_CONTROL_ME |
855 (buf->pt->dma >> 12);
857 control |= SAA7134_RS_CONTROL_BSWAP;
859 control |= SAA7134_RS_CONTROL_WSWAP;
860 if (V4L2_FIELD_HAS_BOTH(buf->vb.field)) {
862 saa_writel(SAA7134_RS_BA1(0),base);
863 saa_writel(SAA7134_RS_BA2(0),base+bpl);
864 saa_writel(SAA7134_RS_PITCH(0),bpl*2);
867 saa_writel(SAA7134_RS_BA1(0),base);
868 saa_writel(SAA7134_RS_BA2(0),base);
869 saa_writel(SAA7134_RS_PITCH(0),bpl);
871 saa_writel(SAA7134_RS_CONTROL(0),control);
873 if (buf->fmt->planar) {
874 /* DMA: setup channel 4+5 (= planar task A) */
875 bpl_uv = bpl >> buf->fmt->hshift;
876 lines_uv = buf->vb.height >> buf->fmt->vshift;
877 base2 = base + bpl * buf->vb.height;
878 base3 = base2 + bpl_uv * lines_uv;
879 if (buf->fmt->uvswap)
880 tmp = base2, base2 = base3, base3 = tmp;
881 dprintk("uv: bpl=%ld lines=%ld base2/3=%ld/%ld\n",
882 bpl_uv,lines_uv,base2,base3);
883 if (V4L2_FIELD_HAS_BOTH(buf->vb.field)) {
885 saa_writel(SAA7134_RS_BA1(4),base2);
886 saa_writel(SAA7134_RS_BA2(4),base2+bpl_uv);
887 saa_writel(SAA7134_RS_PITCH(4),bpl_uv*2);
888 saa_writel(SAA7134_RS_BA1(5),base3);
889 saa_writel(SAA7134_RS_BA2(5),base3+bpl_uv);
890 saa_writel(SAA7134_RS_PITCH(5),bpl_uv*2);
893 saa_writel(SAA7134_RS_BA1(4),base2);
894 saa_writel(SAA7134_RS_BA2(4),base2);
895 saa_writel(SAA7134_RS_PITCH(4),bpl_uv);
896 saa_writel(SAA7134_RS_BA1(5),base3);
897 saa_writel(SAA7134_RS_BA2(5),base3);
898 saa_writel(SAA7134_RS_PITCH(5),bpl_uv);
900 saa_writel(SAA7134_RS_CONTROL(4),control);
901 saa_writel(SAA7134_RS_CONTROL(5),control);
905 saa7134_set_dmabits(dev);
906 mod_timer(&dev->video_q.timeout, jiffies+BUFFER_TIMEOUT);
910 static int buffer_prepare(struct file *file, struct videobuf_buffer *vb,
911 enum v4l2_field field)
913 struct saa7134_fh *fh = file->private_data;
914 struct saa7134_dev *dev = fh->dev;
915 struct saa7134_buf *buf = (struct saa7134_buf *)vb;
922 if (fh->width < 48 ||
924 fh->width/4 > dev->crop_current.width ||
925 fh->height/4 > dev->crop_current.height ||
926 fh->width > dev->crop_bounds.width ||
927 fh->height > dev->crop_bounds.height)
929 size = (fh->width * fh->height * fh->fmt->depth) >> 3;
930 if (0 != buf->vb.baddr && buf->vb.bsize < size)
933 dprintk("buffer_prepare [%d,size=%dx%d,bytes=%d,fields=%s,%s]\n",
934 vb->i,fh->width,fh->height,size,v4l2_field_names[field],
936 if (buf->vb.width != fh->width ||
937 buf->vb.height != fh->height ||
938 buf->vb.size != size ||
939 buf->vb.field != field ||
940 buf->fmt != fh->fmt) {
941 saa7134_dma_free(dev,buf);
944 if (STATE_NEEDS_INIT == buf->vb.state) {
945 buf->vb.width = fh->width;
946 buf->vb.height = fh->height;
948 buf->vb.field = field;
950 buf->pt = &fh->pt_cap;
952 err = videobuf_iolock(dev->pci,&buf->vb,&dev->ovbuf);
955 err = saa7134_pgtable_build(dev->pci,buf->pt,
958 saa7134_buffer_startpage(buf));
962 buf->vb.state = STATE_PREPARED;
963 buf->activate = buffer_activate;
967 saa7134_dma_free(dev,buf);
972 buffer_setup(struct file *file, unsigned int *count, unsigned int *size)
974 struct saa7134_fh *fh = file->private_data;
976 *size = fh->fmt->depth * fh->width * fh->height >> 3;
979 *count = saa7134_buffer_count(*size,*count);
983 static void buffer_queue(struct file *file, struct videobuf_buffer *vb)
985 struct saa7134_fh *fh = file->private_data;
986 struct saa7134_buf *buf = (struct saa7134_buf *)vb;
988 saa7134_buffer_queue(fh->dev,&fh->dev->video_q,buf);
991 static void buffer_release(struct file *file, struct videobuf_buffer *vb)
993 struct saa7134_fh *fh = file->private_data;
994 struct saa7134_buf *buf = (struct saa7134_buf *)vb;
996 saa7134_dma_free(fh->dev,buf);
999 static struct videobuf_queue_ops video_qops = {
1000 .buf_setup = buffer_setup,
1001 .buf_prepare = buffer_prepare,
1002 .buf_queue = buffer_queue,
1003 .buf_release = buffer_release,
1006 /* ------------------------------------------------------------------ */
1008 static int get_control(struct saa7134_dev *dev, struct v4l2_control *c)
1010 const struct v4l2_queryctrl* ctrl;
1012 ctrl = ctrl_by_id(c->id);
1016 case V4L2_CID_BRIGHTNESS:
1017 c->value = dev->ctl_bright;
1020 c->value = dev->ctl_hue;
1022 case V4L2_CID_CONTRAST:
1023 c->value = dev->ctl_contrast;
1025 case V4L2_CID_SATURATION:
1026 c->value = dev->ctl_saturation;
1028 case V4L2_CID_AUDIO_MUTE:
1029 c->value = dev->ctl_mute;
1031 case V4L2_CID_AUDIO_VOLUME:
1032 c->value = dev->ctl_volume;
1034 case V4L2_CID_PRIVATE_INVERT:
1035 c->value = dev->ctl_invert;
1037 case V4L2_CID_VFLIP:
1038 c->value = dev->ctl_mirror;
1040 case V4L2_CID_PRIVATE_Y_EVEN:
1041 c->value = dev->ctl_y_even;
1043 case V4L2_CID_PRIVATE_Y_ODD:
1044 c->value = dev->ctl_y_odd;
1052 static int set_control(struct saa7134_dev *dev, struct saa7134_fh *fh,
1053 struct v4l2_control *c)
1055 const struct v4l2_queryctrl* ctrl;
1056 unsigned long flags;
1057 int restart_overlay = 0;
1059 ctrl = ctrl_by_id(c->id);
1062 dprintk("set_control name=%s val=%d\n",ctrl->name,c->value);
1063 switch (ctrl->type) {
1064 case V4L2_CTRL_TYPE_BOOLEAN:
1065 case V4L2_CTRL_TYPE_MENU:
1066 case V4L2_CTRL_TYPE_INTEGER:
1067 if (c->value < ctrl->minimum)
1068 c->value = ctrl->minimum;
1069 if (c->value > ctrl->maximum)
1070 c->value = ctrl->maximum;
1076 case V4L2_CID_BRIGHTNESS:
1077 dev->ctl_bright = c->value;
1078 saa_writeb(SAA7134_DEC_LUMA_BRIGHT, dev->ctl_bright);
1081 dev->ctl_hue = c->value;
1082 saa_writeb(SAA7134_DEC_CHROMA_HUE, dev->ctl_hue);
1084 case V4L2_CID_CONTRAST:
1085 dev->ctl_contrast = c->value;
1086 saa_writeb(SAA7134_DEC_LUMA_CONTRAST,
1087 dev->ctl_invert ? -dev->ctl_contrast : dev->ctl_contrast);
1089 case V4L2_CID_SATURATION:
1090 dev->ctl_saturation = c->value;
1091 saa_writeb(SAA7134_DEC_CHROMA_SATURATION,
1092 dev->ctl_invert ? -dev->ctl_saturation : dev->ctl_saturation);
1094 case V4L2_CID_AUDIO_MUTE:
1095 dev->ctl_mute = c->value;
1096 saa7134_tvaudio_setmute(dev);
1098 case V4L2_CID_AUDIO_VOLUME:
1099 dev->ctl_volume = c->value;
1100 saa7134_tvaudio_setvolume(dev,dev->ctl_volume);
1102 case V4L2_CID_PRIVATE_INVERT:
1103 dev->ctl_invert = c->value;
1104 saa_writeb(SAA7134_DEC_LUMA_CONTRAST,
1105 dev->ctl_invert ? -dev->ctl_contrast : dev->ctl_contrast);
1106 saa_writeb(SAA7134_DEC_CHROMA_SATURATION,
1107 dev->ctl_invert ? -dev->ctl_saturation : dev->ctl_saturation);
1109 case V4L2_CID_VFLIP:
1110 dev->ctl_mirror = c->value;
1111 restart_overlay = 1;
1113 case V4L2_CID_PRIVATE_Y_EVEN:
1114 dev->ctl_y_even = c->value;
1115 restart_overlay = 1;
1117 case V4L2_CID_PRIVATE_Y_ODD:
1118 dev->ctl_y_odd = c->value;
1119 restart_overlay = 1;
1124 if (restart_overlay && fh && res_check(fh, RESOURCE_OVERLAY)) {
1125 spin_lock_irqsave(&dev->slock,flags);
1126 stop_preview(dev,fh);
1127 start_preview(dev,fh);
1128 spin_unlock_irqrestore(&dev->slock,flags);
1133 /* ------------------------------------------------------------------ */
1135 static struct videobuf_queue* saa7134_queue(struct saa7134_fh *fh)
1137 struct videobuf_queue* q = NULL;
1140 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1143 case V4L2_BUF_TYPE_VBI_CAPTURE:
1152 static int saa7134_resource(struct saa7134_fh *fh)
1157 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1158 res = RESOURCE_VIDEO;
1160 case V4L2_BUF_TYPE_VBI_CAPTURE:
1169 static int video_open(struct inode *inode, struct file *file)
1171 int minor = iminor(inode);
1172 struct saa7134_dev *h,*dev = NULL;
1173 struct saa7134_fh *fh;
1174 struct list_head *list;
1175 enum v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1178 list_for_each(list,&saa7134_devlist) {
1179 h = list_entry(list, struct saa7134_dev, devlist);
1180 if (h->video_dev && (h->video_dev->minor == minor))
1182 if (h->radio_dev && (h->radio_dev->minor == minor)) {
1186 if (h->vbi_dev && (h->vbi_dev->minor == minor)) {
1187 type = V4L2_BUF_TYPE_VBI_CAPTURE;
1194 dprintk("open minor=%d radio=%d type=%s\n",minor,radio,
1195 v4l2_type_names[type]);
1197 /* allocate + initialize per filehandle data */
1198 fh = kmalloc(sizeof(*fh),GFP_KERNEL);
1201 memset(fh,0,sizeof(*fh));
1202 file->private_data = fh;
1206 fh->fmt = format_by_fourcc(V4L2_PIX_FMT_BGR24);
1209 #ifdef VIDIOC_G_PRIORITY
1210 v4l2_prio_open(&dev->prio,&fh->prio);
1213 videobuf_queue_init(&fh->cap, &video_qops,
1214 dev->pci, &dev->slock,
1215 V4L2_BUF_TYPE_VIDEO_CAPTURE,
1216 V4L2_FIELD_INTERLACED,
1217 sizeof(struct saa7134_buf));
1218 init_MUTEX(&fh->cap.lock);
1219 saa7134_pgtable_alloc(dev->pci,&fh->pt_cap);
1221 videobuf_queue_init(&fh->vbi, &saa7134_vbi_qops,
1222 dev->pci, &dev->slock,
1223 V4L2_BUF_TYPE_VBI_CAPTURE,
1225 sizeof(struct saa7134_buf));
1226 init_MUTEX(&fh->vbi.lock);
1227 saa7134_pgtable_alloc(dev->pci,&fh->pt_vbi);
1230 /* switch to radio mode */
1231 saa7134_tvaudio_setinput(dev,&card(dev).radio);
1232 saa7134_i2c_call_clients(dev,AUDC_SET_RADIO,NULL);
1234 /* switch to video/vbi mode */
1235 video_mux(dev,dev->ctl_input);
1241 video_read(struct file *file, char __user *data, size_t count, loff_t *ppos)
1243 struct saa7134_fh *fh = file->private_data;
1246 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1247 if (res_locked(fh->dev,RESOURCE_VIDEO))
1249 return videobuf_read_one(file, saa7134_queue(fh),
1251 case V4L2_BUF_TYPE_VBI_CAPTURE:
1252 if (!res_get(fh->dev,fh,RESOURCE_VBI))
1254 return videobuf_read_stream(file, saa7134_queue(fh),
1255 data, count, ppos, 1);
1264 video_poll(struct file *file, struct poll_table_struct *wait)
1266 struct saa7134_fh *fh = file->private_data;
1267 struct videobuf_buffer *buf = NULL;
1269 if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type)
1270 return videobuf_poll_stream(file, &fh->vbi, wait);
1272 if (res_check(fh,RESOURCE_VIDEO)) {
1273 if (!list_empty(&fh->cap.stream))
1274 buf = list_entry(fh->cap.stream.next, struct videobuf_buffer, stream);
1276 down(&fh->cap.lock);
1277 if (UNSET == fh->cap.read_off) {
1278 /* need to capture a new frame */
1279 if (res_locked(fh->dev,RESOURCE_VIDEO)) {
1283 if (0 != fh->cap.ops->buf_prepare(file,fh->cap.read_buf,fh->cap.field)) {
1287 fh->cap.ops->buf_queue(file,fh->cap.read_buf);
1288 fh->cap.read_off = 0;
1291 buf = fh->cap.read_buf;
1297 poll_wait(file, &buf->done, wait);
1298 if (buf->state == STATE_DONE ||
1299 buf->state == STATE_ERROR)
1300 return POLLIN|POLLRDNORM;
1304 static int video_release(struct inode *inode, struct file *file)
1306 struct saa7134_fh *fh = file->private_data;
1307 struct saa7134_dev *dev = fh->dev;
1308 unsigned long flags;
1310 /* turn off overlay */
1311 if (res_check(fh, RESOURCE_OVERLAY)) {
1312 spin_lock_irqsave(&dev->slock,flags);
1313 stop_preview(dev,fh);
1314 spin_unlock_irqrestore(&dev->slock,flags);
1315 res_free(dev,fh,RESOURCE_OVERLAY);
1318 /* stop video capture */
1319 if (res_check(fh, RESOURCE_VIDEO)) {
1320 videobuf_streamoff(file,&fh->cap);
1321 res_free(dev,fh,RESOURCE_VIDEO);
1323 if (fh->cap.read_buf) {
1324 buffer_release(file,fh->cap.read_buf);
1325 kfree(fh->cap.read_buf);
1328 /* stop vbi capture */
1329 if (res_check(fh, RESOURCE_VBI)) {
1330 if (fh->vbi.streaming)
1331 videobuf_streamoff(file,&fh->vbi);
1332 if (fh->vbi.reading)
1333 videobuf_read_stop(file,&fh->vbi);
1334 res_free(dev,fh,RESOURCE_VBI);
1337 saa7134_pgtable_free(dev->pci,&fh->pt_cap);
1338 saa7134_pgtable_free(dev->pci,&fh->pt_vbi);
1340 #ifdef VIDIOC_G_PRIORITY
1341 v4l2_prio_close(&dev->prio,&fh->prio);
1343 file->private_data = NULL;
1349 video_mmap(struct file *file, struct vm_area_struct * vma)
1351 struct saa7134_fh *fh = file->private_data;
1353 return videobuf_mmap_mapper(vma,saa7134_queue(fh));
1356 /* ------------------------------------------------------------------ */
1358 void saa7134_vbi_fmt(struct saa7134_dev *dev, struct v4l2_format *f)
1360 struct saa7134_tvnorm *norm = dev->tvnorm;
1362 f->fmt.vbi.sampling_rate = 6750000 * 4;
1363 f->fmt.vbi.samples_per_line = 2048 /* VBI_LINE_LENGTH */;
1364 f->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
1365 f->fmt.vbi.offset = 64 * 4;
1366 f->fmt.vbi.start[0] = norm->vbi_v_start;
1367 f->fmt.vbi.count[0] = norm->vbi_v_stop - norm->vbi_v_start +1;
1368 f->fmt.vbi.start[1] = norm->video_v_stop + norm->vbi_v_start +1;
1369 f->fmt.vbi.count[1] = f->fmt.vbi.count[0];
1370 f->fmt.vbi.flags = 0; /* VBI_UNSYNC VBI_INTERLACED */
1373 if (V4L2_STD_PAL == norm->id) {
1375 f->fmt.vbi.start[0] += 3;
1376 f->fmt.vbi.start[1] += 3*2;
1381 int saa7134_g_fmt(struct saa7134_dev *dev, struct saa7134_fh *fh,
1382 struct v4l2_format *f)
1385 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1386 memset(&f->fmt.pix,0,sizeof(f->fmt.pix));
1387 f->fmt.pix.width = fh->width;
1388 f->fmt.pix.height = fh->height;
1389 f->fmt.pix.field = fh->cap.field;
1390 f->fmt.pix.pixelformat = fh->fmt->fourcc;
1391 f->fmt.pix.bytesperline =
1392 (f->fmt.pix.width * fh->fmt->depth) >> 3;
1393 f->fmt.pix.sizeimage =
1394 f->fmt.pix.height * f->fmt.pix.bytesperline;
1396 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
1397 f->fmt.win = fh->win;
1399 case V4L2_BUF_TYPE_VBI_CAPTURE:
1400 saa7134_vbi_fmt(dev,f);
1407 int saa7134_try_fmt(struct saa7134_dev *dev, struct saa7134_fh *fh,
1408 struct v4l2_format *f)
1413 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1415 struct saa7134_format *fmt;
1416 enum v4l2_field field;
1417 unsigned int maxw, maxh;
1419 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1423 field = f->fmt.pix.field;
1424 maxw = min(dev->crop_current.width*4, dev->crop_bounds.width);
1425 maxh = min(dev->crop_current.height*4, dev->crop_bounds.height);
1427 if (V4L2_FIELD_ANY == field) {
1428 field = (f->fmt.pix.height > maxh/2)
1429 ? V4L2_FIELD_INTERLACED
1430 : V4L2_FIELD_BOTTOM;
1433 case V4L2_FIELD_TOP:
1434 case V4L2_FIELD_BOTTOM:
1437 case V4L2_FIELD_INTERLACED:
1443 f->fmt.pix.field = field;
1444 if (f->fmt.pix.width < 48)
1445 f->fmt.pix.width = 48;
1446 if (f->fmt.pix.height < 32)
1447 f->fmt.pix.height = 32;
1448 if (f->fmt.pix.width > maxw)
1449 f->fmt.pix.width = maxw;
1450 if (f->fmt.pix.height > maxh)
1451 f->fmt.pix.height = maxh;
1452 f->fmt.pix.bytesperline =
1453 (f->fmt.pix.width * fmt->depth) >> 3;
1454 f->fmt.pix.sizeimage =
1455 f->fmt.pix.height * f->fmt.pix.bytesperline;
1459 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
1460 err = verify_preview(dev,&f->fmt.win);
1464 case V4L2_BUF_TYPE_VBI_CAPTURE:
1465 saa7134_vbi_fmt(dev,f);
1472 int saa7134_s_fmt(struct saa7134_dev *dev, struct saa7134_fh *fh,
1473 struct v4l2_format *f)
1475 unsigned long flags;
1479 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1480 err = saa7134_try_fmt(dev,fh,f);
1484 fh->fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1485 fh->width = f->fmt.pix.width;
1486 fh->height = f->fmt.pix.height;
1487 fh->cap.field = f->fmt.pix.field;
1489 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
1490 err = verify_preview(dev,&f->fmt.win);
1495 fh->win = f->fmt.win;
1496 fh->nclips = f->fmt.win.clipcount;
1499 if (copy_from_user(fh->clips,f->fmt.win.clips,
1500 sizeof(struct v4l2_clip)*fh->nclips)) {
1505 if (res_check(fh, RESOURCE_OVERLAY)) {
1506 spin_lock_irqsave(&dev->slock,flags);
1507 stop_preview(dev,fh);
1508 start_preview(dev,fh);
1509 spin_unlock_irqrestore(&dev->slock,flags);
1513 case V4L2_BUF_TYPE_VBI_CAPTURE:
1514 saa7134_vbi_fmt(dev,f);
1521 int saa7134_common_ioctl(struct saa7134_dev *dev,
1522 unsigned int cmd, void *arg)
1527 case VIDIOC_QUERYCTRL:
1529 const struct v4l2_queryctrl *ctrl;
1530 struct v4l2_queryctrl *c = arg;
1532 if ((c->id < V4L2_CID_BASE ||
1533 c->id >= V4L2_CID_LASTP1) &&
1534 (c->id < V4L2_CID_PRIVATE_BASE ||
1535 c->id >= V4L2_CID_PRIVATE_LASTP1))
1537 ctrl = ctrl_by_id(c->id);
1538 *c = (NULL != ctrl) ? *ctrl : no_ctrl;
1542 return get_control(dev,arg);
1546 err = set_control(dev,NULL,arg);
1550 /* --- input switching --------------------------------------- */
1551 case VIDIOC_ENUMINPUT:
1553 struct v4l2_input *i = arg;
1557 if (n >= SAA7134_INPUT_MAX)
1559 if (NULL == card_in(dev,i->index).name)
1561 memset(i,0,sizeof(*i));
1563 i->type = V4L2_INPUT_TYPE_CAMERA;
1564 strcpy(i->name,card_in(dev,n).name);
1565 if (card_in(dev,n).tv)
1566 i->type = V4L2_INPUT_TYPE_TUNER;
1568 if (n == dev->ctl_input) {
1569 int v1 = saa_readb(SAA7134_STATUS_VIDEO1);
1570 int v2 = saa_readb(SAA7134_STATUS_VIDEO2);
1572 if (0 != (v1 & 0x40))
1573 i->status |= V4L2_IN_ST_NO_H_LOCK;
1574 if (0 != (v2 & 0x40))
1575 i->status |= V4L2_IN_ST_NO_SYNC;
1576 if (0 != (v2 & 0x0e))
1577 i->status |= V4L2_IN_ST_MACROVISION;
1579 for (n = 0; n < TVNORMS; n++)
1580 i->std |= tvnorms[n].id;
1583 case VIDIOC_G_INPUT:
1586 *i = dev->ctl_input;
1589 case VIDIOC_S_INPUT:
1593 if (*i < 0 || *i >= SAA7134_INPUT_MAX)
1595 if (NULL == card_in(dev,*i).name)
1608 * This function is _not_ called directly, but from
1609 * video_generic_ioctl (and maybe others). userspace
1610 * copying is done already, arg is a kernel pointer.
1612 static int video_do_ioctl(struct inode *inode, struct file *file,
1613 unsigned int cmd, void *arg)
1615 struct saa7134_fh *fh = file->private_data;
1616 struct saa7134_dev *dev = fh->dev;
1617 unsigned long flags;
1620 if (video_debug > 1)
1621 saa7134_print_ioctl(dev->name,cmd);
1623 #ifdef VIDIOC_G_PRIORITY
1627 case VIDIOC_S_INPUT:
1628 case VIDIOC_S_TUNER:
1629 case VIDIOC_S_FREQUENCY:
1630 err = v4l2_prio_check(&dev->prio,&fh->prio);
1637 case VIDIOC_QUERYCAP:
1639 struct v4l2_capability *cap = arg;
1641 memset(cap,0,sizeof(*cap));
1642 strcpy(cap->driver, "saa7134");
1643 strlcpy(cap->card, saa7134_boards[dev->board].name,
1645 sprintf(cap->bus_info,"PCI:%s",pci_name(dev->pci));
1646 cap->version = SAA7134_VERSION_CODE;
1648 V4L2_CAP_VIDEO_CAPTURE |
1649 V4L2_CAP_VIDEO_OVERLAY |
1650 V4L2_CAP_VBI_CAPTURE |
1652 V4L2_CAP_READWRITE |
1657 /* --- tv standards ------------------------------------------ */
1658 case VIDIOC_ENUMSTD:
1660 struct v4l2_standard *e = arg;
1666 err = v4l2_video_std_construct(e, tvnorms[e->index].id,
1667 tvnorms[e->index].name);
1675 v4l2_std_id *id = arg;
1677 *id = dev->tvnorm->id;
1682 v4l2_std_id *id = arg;
1685 for (i = 0; i < TVNORMS; i++)
1686 if (*id == tvnorms[i].id)
1689 for (i = 0; i < TVNORMS; i++)
1690 if (*id & tvnorms[i].id)
1696 if (res_check(fh, RESOURCE_OVERLAY)) {
1697 spin_lock_irqsave(&dev->slock,flags);
1698 stop_preview(dev,fh);
1699 set_tvnorm(dev,&tvnorms[i]);
1700 start_preview(dev,fh);
1701 spin_unlock_irqrestore(&dev->slock,flags);
1703 set_tvnorm(dev,&tvnorms[i]);
1704 saa7134_tvaudio_do_scan(dev);
1709 case VIDIOC_CROPCAP:
1711 struct v4l2_cropcap *cap = arg;
1713 if (cap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1714 cap->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
1716 cap->bounds = dev->crop_bounds;
1717 cap->defrect = dev->crop_defrect;
1718 cap->pixelaspect.numerator = 1;
1719 cap->pixelaspect.denominator = 1;
1720 if (dev->tvnorm->id & V4L2_STD_525_60) {
1721 cap->pixelaspect.numerator = 11;
1722 cap->pixelaspect.denominator = 10;
1724 if (dev->tvnorm->id & V4L2_STD_625_50) {
1725 cap->pixelaspect.numerator = 54;
1726 cap->pixelaspect.denominator = 59;
1733 struct v4l2_crop * crop = arg;
1735 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1736 crop->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
1738 crop->c = dev->crop_current;
1743 struct v4l2_crop *crop = arg;
1744 struct v4l2_rect *b = &dev->crop_bounds;
1746 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1747 crop->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
1749 if (crop->c.height < 0)
1751 if (crop->c.width < 0)
1754 if (res_locked(fh->dev,RESOURCE_OVERLAY))
1756 if (res_locked(fh->dev,RESOURCE_VIDEO))
1759 if (crop->c.top < b->top)
1760 crop->c.top = b->top;
1761 if (crop->c.top > b->top + b->height)
1762 crop->c.top = b->top + b->height;
1763 if (crop->c.height > b->top - crop->c.top + b->height)
1764 crop->c.height = b->top - crop->c.top + b->height;
1766 if (crop->c.left < b->left)
1767 crop->c.top = b->left;
1768 if (crop->c.left > b->left + b->width)
1769 crop->c.top = b->left + b->width;
1770 if (crop->c.width > b->left - crop->c.left + b->width)
1771 crop->c.width = b->left - crop->c.left + b->width;
1773 dev->crop_current = crop->c;
1777 /* --- tuner ioctls ------------------------------------------ */
1778 case VIDIOC_G_TUNER:
1780 struct v4l2_tuner *t = arg;
1785 memset(t,0,sizeof(*t));
1786 for (n = 0; n < SAA7134_INPUT_MAX; n++)
1787 if (card_in(dev,n).tv)
1789 if (NULL != card_in(dev,n).name) {
1790 strcpy(t->name, "Television");
1791 t->capability = V4L2_TUNER_CAP_NORM |
1792 V4L2_TUNER_CAP_STEREO |
1793 V4L2_TUNER_CAP_LANG1 |
1794 V4L2_TUNER_CAP_LANG2;
1795 t->rangehigh = 0xffffffffUL;
1796 t->rxsubchans = saa7134_tvaudio_getstereo(dev);
1797 t->audmode = saa7134_tvaudio_rx2mode(t->rxsubchans);
1799 if (0 != (saa_readb(SAA7134_STATUS_VIDEO1) & 0x03))
1803 case VIDIOC_S_TUNER:
1805 struct v4l2_tuner *t = arg;
1808 mode = dev->thread.mode;
1809 if (UNSET == mode) {
1810 rx = saa7134_tvaudio_getstereo(dev);
1811 mode = saa7134_tvaudio_rx2mode(t->rxsubchans);
1813 if (mode != t->audmode) {
1814 dev->thread.mode = t->audmode;
1818 case VIDIOC_G_FREQUENCY:
1820 struct v4l2_frequency *f = arg;
1822 memset(f,0,sizeof(*f));
1823 f->type = V4L2_TUNER_ANALOG_TV;
1824 f->frequency = dev->ctl_freq;
1827 case VIDIOC_S_FREQUENCY:
1829 struct v4l2_frequency *f = arg;
1833 if (V4L2_TUNER_ANALOG_TV != f->type)
1836 dev->ctl_freq = f->frequency;
1837 #ifdef V4L2_I2C_CLIENTS
1838 saa7134_i2c_call_clients(dev,VIDIOC_S_FREQUENCY,f);
1840 saa7134_i2c_call_clients(dev,VIDIOCSFREQ,&dev->ctl_freq);
1842 saa7134_tvaudio_do_scan(dev);
1847 /* --- control ioctls ---------------------------------------- */
1848 case VIDIOC_ENUMINPUT:
1849 case VIDIOC_G_INPUT:
1850 case VIDIOC_S_INPUT:
1851 case VIDIOC_QUERYCTRL:
1854 return saa7134_common_ioctl(dev, cmd, arg);
1856 case VIDIOC_G_AUDIO:
1858 struct v4l2_audio *a = arg;
1860 memset(a,0,sizeof(*a));
1861 strcpy(a->name,"audio");
1864 case VIDIOC_S_AUDIO:
1868 struct v4l2_captureparm *parm = arg;
1869 memset(parm,0,sizeof(*parm));
1873 #ifdef VIDIOC_G_PRIORITY
1874 case VIDIOC_G_PRIORITY:
1876 enum v4l2_priority *p = arg;
1878 *p = v4l2_prio_max(&dev->prio);
1881 case VIDIOC_S_PRIORITY:
1883 enum v4l2_priority *prio = arg;
1885 return v4l2_prio_change(&dev->prio, &fh->prio, *prio);
1889 /* --- preview ioctls ---------------------------------------- */
1890 case VIDIOC_ENUM_FMT:
1892 struct v4l2_fmtdesc *f = arg;
1893 enum v4l2_buf_type type;
1899 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1900 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
1901 if (index >= FORMATS)
1903 if (f->type == V4L2_BUF_TYPE_VIDEO_OVERLAY &&
1904 formats[index].planar)
1906 memset(f,0,sizeof(*f));
1909 strlcpy(f->description,formats[index].name,sizeof(f->description));
1910 f->pixelformat = formats[index].fourcc;
1912 case V4L2_BUF_TYPE_VBI_CAPTURE:
1915 memset(f,0,sizeof(*f));
1918 f->pixelformat = V4L2_PIX_FMT_GREY;
1919 strcpy(f->description,"vbi data");
1928 struct v4l2_framebuffer *fb = arg;
1931 fb->capability = V4L2_FBUF_CAP_LIST_CLIPPING;
1936 struct v4l2_framebuffer *fb = arg;
1937 struct saa7134_format *fmt;
1939 if(!capable(CAP_SYS_ADMIN) &&
1940 !capable(CAP_SYS_RAWIO))
1944 fmt = format_by_fourcc(fb->fmt.pixelformat);
1951 if (0 == dev->ovbuf.fmt.bytesperline)
1952 dev->ovbuf.fmt.bytesperline =
1953 dev->ovbuf.fmt.width*fmt->depth/8;
1956 case VIDIOC_OVERLAY:
1961 if (!res_get(dev,fh,RESOURCE_OVERLAY))
1963 spin_lock_irqsave(&dev->slock,flags);
1964 start_preview(dev,fh);
1965 spin_unlock_irqrestore(&dev->slock,flags);
1968 if (!res_check(fh, RESOURCE_OVERLAY))
1970 spin_lock_irqsave(&dev->slock,flags);
1971 stop_preview(dev,fh);
1972 spin_unlock_irqrestore(&dev->slock,flags);
1973 res_free(dev,fh,RESOURCE_OVERLAY);
1978 /* --- capture ioctls ---------------------------------------- */
1981 struct v4l2_format *f = arg;
1982 return saa7134_g_fmt(dev,fh,f);
1986 struct v4l2_format *f = arg;
1987 return saa7134_s_fmt(dev,fh,f);
1989 case VIDIOC_TRY_FMT:
1991 struct v4l2_format *f = arg;
1992 return saa7134_try_fmt(dev,fh,f);
1997 struct video_mbuf *mbuf = arg;
1998 struct videobuf_queue *q;
1999 struct v4l2_requestbuffers req;
2002 q = saa7134_queue(fh);
2003 memset(&req,0,sizeof(req));
2005 req.count = gbuffers;
2006 req.memory = V4L2_MEMORY_MMAP;
2007 err = videobuf_reqbufs(file,q,&req);
2010 memset(mbuf,0,sizeof(*mbuf));
2011 mbuf->frames = req.count;
2013 for (i = 0; i < mbuf->frames; i++) {
2014 mbuf->offsets[i] = q->bufs[i]->boff;
2015 mbuf->size += q->bufs[i]->bsize;
2019 case VIDIOC_REQBUFS:
2020 return videobuf_reqbufs(file,saa7134_queue(fh),arg);
2022 case VIDIOC_QUERYBUF:
2023 return videobuf_querybuf(saa7134_queue(fh),arg);
2026 return videobuf_qbuf(file,saa7134_queue(fh),arg);
2029 return videobuf_dqbuf(file,saa7134_queue(fh),arg);
2031 case VIDIOC_STREAMON:
2033 int res = saa7134_resource(fh);
2035 if (!res_get(dev,fh,res))
2037 return videobuf_streamon(file,saa7134_queue(fh));
2039 case VIDIOC_STREAMOFF:
2041 int res = saa7134_resource(fh);
2043 err = videobuf_streamoff(file,saa7134_queue(fh));
2046 res_free(dev,fh,res);
2051 return v4l_compat_translate_ioctl(inode,file,cmd,arg,
2057 static int video_ioctl(struct inode *inode, struct file *file,
2058 unsigned int cmd, unsigned long arg)
2060 return video_usercopy(inode, file, cmd, arg, video_do_ioctl);
2063 static int radio_do_ioctl(struct inode *inode, struct file *file,
2064 unsigned int cmd, void *arg)
2066 struct saa7134_fh *fh = file->private_data;
2067 struct saa7134_dev *dev = fh->dev;
2069 if (video_debug > 1)
2070 saa7134_print_ioctl(dev->name,cmd);
2072 case VIDIOC_QUERYCAP:
2074 struct v4l2_capability *cap = arg;
2076 memset(cap,0,sizeof(*cap));
2077 strcpy(cap->driver, "saa7134");
2078 strlcpy(cap->card, saa7134_boards[dev->board].name,
2080 sprintf(cap->bus_info,"PCI:%s",pci_name(dev->pci));
2081 cap->version = SAA7134_VERSION_CODE;
2082 cap->capabilities = V4L2_CAP_TUNER;
2085 case VIDIOC_G_TUNER:
2087 struct v4l2_tuner *t = arg;
2092 memset(t,0,sizeof(*t));
2093 strcpy(t->name, "Radio");
2094 t->rangelow = (int)(65*16);
2095 t->rangehigh = (int)(108*16);
2097 #ifdef V4L2_I2C_CLIENTS
2098 saa7134_i2c_call_clients(dev,VIDIOC_G_TUNER,t);
2101 struct video_tuner vt;
2102 memset(&vt,0,sizeof(vt));
2103 saa7134_i2c_call_clients(dev,VIDIOCGTUNER,&vt);
2104 t->signal = vt.signal;
2109 case VIDIOC_ENUMINPUT:
2111 struct v4l2_input *i = arg;
2115 strcpy(i->name,"Radio");
2116 i->type = V4L2_INPUT_TYPE_TUNER;
2119 case VIDIOC_G_INPUT:
2125 case VIDIOC_G_AUDIO:
2127 struct v4l2_audio *a = arg;
2129 memset(a,0,sizeof(*a));
2130 strcpy(a->name,"Radio");
2135 v4l2_std_id *id = arg;
2139 case VIDIOC_S_AUDIO:
2140 case VIDIOC_S_TUNER:
2141 case VIDIOC_S_INPUT:
2145 case VIDIOC_QUERYCTRL:
2147 const struct v4l2_queryctrl *ctrl;
2148 struct v4l2_queryctrl *c = arg;
2150 if (c->id < V4L2_CID_BASE ||
2151 c->id >= V4L2_CID_LASTP1)
2153 if (c->id == V4L2_CID_AUDIO_MUTE) {
2154 ctrl = ctrl_by_id(c->id);
2163 case VIDIOC_G_FREQUENCY:
2164 case VIDIOC_S_FREQUENCY:
2165 return video_do_ioctl(inode,file,cmd,arg);
2168 return v4l_compat_translate_ioctl(inode,file,cmd,arg,
2174 static int radio_ioctl(struct inode *inode, struct file *file,
2175 unsigned int cmd, unsigned long arg)
2177 return video_usercopy(inode, file, cmd, arg, radio_do_ioctl);
2180 static struct file_operations video_fops =
2182 .owner = THIS_MODULE,
2184 .release = video_release,
2188 .ioctl = video_ioctl,
2189 .llseek = no_llseek,
2192 static struct file_operations radio_fops =
2194 .owner = THIS_MODULE,
2196 .release = video_release,
2197 .ioctl = radio_ioctl,
2198 .llseek = no_llseek,
2201 /* ----------------------------------------------------------- */
2202 /* exported stuff */
2204 struct video_device saa7134_video_template =
2206 .name = "saa7134-video",
2207 .type = VID_TYPE_CAPTURE|VID_TYPE_TUNER|VID_TYPE_OVERLAY|
2208 VID_TYPE_CLIPPING|VID_TYPE_SCALES,
2210 .fops = &video_fops,
2214 struct video_device saa7134_vbi_template =
2216 .name = "saa7134-vbi",
2217 .type = VID_TYPE_TUNER|VID_TYPE_TELETEXT,
2219 .fops = &video_fops,
2223 struct video_device saa7134_radio_template =
2225 .name = "saa7134-radio",
2226 .type = VID_TYPE_TUNER,
2228 .fops = &radio_fops,
2232 int saa7134_video_init1(struct saa7134_dev *dev)
2234 /* sanitycheck insmod options */
2235 if (gbuffers < 2 || gbuffers > VIDEO_MAX_FRAME)
2237 if (gbufsize < 0 || gbufsize > gbufsize_max)
2238 gbufsize = gbufsize_max;
2239 gbufsize = (gbufsize + PAGE_SIZE - 1) & PAGE_MASK;
2241 /* put some sensible defaults into the data structures ... */
2242 dev->ctl_bright = ctrl_by_id(V4L2_CID_BRIGHTNESS)->default_value;
2243 dev->ctl_contrast = ctrl_by_id(V4L2_CID_CONTRAST)->default_value;
2244 dev->ctl_hue = ctrl_by_id(V4L2_CID_HUE)->default_value;
2245 dev->ctl_saturation = ctrl_by_id(V4L2_CID_SATURATION)->default_value;
2246 dev->ctl_volume = ctrl_by_id(V4L2_CID_AUDIO_VOLUME)->default_value;
2248 dev->ctl_invert = 0;
2252 INIT_LIST_HEAD(&dev->video_q.queue);
2253 init_timer(&dev->video_q.timeout);
2254 dev->video_q.timeout.function = saa7134_buffer_timeout;
2255 dev->video_q.timeout.data = (unsigned long)(&dev->video_q);
2256 dev->video_q.dev = dev;
2258 if (saa7134_boards[dev->board].video_out) {
2259 /* enable video output */
2260 int vo = saa7134_boards[dev->board].video_out;
2261 saa_writeb(SAA7134_VIDEO_PORT_CTRL0, video_out[vo][0]);
2262 saa_writeb(SAA7134_VIDEO_PORT_CTRL1, video_out[vo][1]);
2263 saa_writeb(SAA7134_VIDEO_PORT_CTRL2, video_out[vo][2]);
2264 saa_writeb(SAA7134_VIDEO_PORT_CTRL3, video_out[vo][3]);
2265 saa_writeb(SAA7134_VIDEO_PORT_CTRL4, video_out[vo][4]);
2266 saa_writeb(SAA7134_VIDEO_PORT_CTRL5, video_out[vo][5]);
2267 saa_writeb(SAA7134_VIDEO_PORT_CTRL6, video_out[vo][6]);
2268 saa_writeb(SAA7134_VIDEO_PORT_CTRL7, video_out[vo][7]);
2269 saa_writeb(SAA7134_VIDEO_PORT_CTRL8, video_out[vo][8]);
2275 int saa7134_video_init2(struct saa7134_dev *dev)
2278 set_tvnorm(dev,&tvnorms[0]);
2280 saa7134_tvaudio_setmute(dev);
2281 saa7134_tvaudio_setvolume(dev,dev->ctl_volume);
2285 int saa7134_video_fini(struct saa7134_dev *dev)
2291 void saa7134_irq_video_intl(struct saa7134_dev *dev)
2293 static const char *st[] = {
2294 "no signal", "found NTSC", "found PAL", "found SECAM" };
2297 norm = saa_readb(SAA7134_STATUS_VIDEO1) & 0x03;
2298 dprintk("DCSDT: %s\n",st[norm]);
2301 /* wake up tvaudio audio carrier scan thread */
2302 saa7134_tvaudio_do_scan(dev);
2304 /* no video signal -> mute audio */
2306 saa7134_tvaudio_setmute(dev);
2310 void saa7134_irq_video_done(struct saa7134_dev *dev, unsigned long status)
2312 enum v4l2_field field;
2314 spin_lock(&dev->slock);
2315 if (dev->video_q.curr) {
2316 dev->video_fieldcount++;
2317 field = dev->video_q.curr->vb.field;
2318 if (V4L2_FIELD_HAS_BOTH(field)) {
2319 /* make sure we have seen both fields */
2320 if ((status & 0x10) == 0x00) {
2321 dev->video_q.curr->top_seen = 1;
2324 if (!dev->video_q.curr->top_seen)
2326 } else if (field == V4L2_FIELD_TOP) {
2327 if ((status & 0x10) != 0x10)
2329 } else if (field == V4L2_FIELD_BOTTOM) {
2330 if ((status & 0x10) != 0x00)
2333 dev->video_q.curr->vb.field_count = dev->video_fieldcount;
2334 saa7134_buffer_finish(dev,&dev->video_q,STATE_DONE);
2336 saa7134_buffer_next(dev,&dev->video_q);
2339 spin_unlock(&dev->slock);
2342 /* ----------------------------------------------------------- */