3 * device driver for philips saa7134 based TV cards
4 * video4linux video interface
6 * (c) 2001-03 Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #include <linux/init.h>
24 #include <linux/list.h>
25 #include <linux/module.h>
26 #include <linux/kernel.h>
27 #include <linux/slab.h>
28 #include <linux/sort.h>
30 #include "saa7134-reg.h"
32 #include <media/v4l2-common.h>
33 #include <media/saa6588.h>
35 /* ------------------------------------------------------------------ */
37 unsigned int video_debug;
38 static unsigned int gbuffers = 8;
39 static unsigned int noninterlaced; /* 0 */
40 static unsigned int gbufsize = 720*576*4;
41 static unsigned int gbufsize_max = 720*576*4;
42 static char secam[] = "--";
43 module_param(video_debug, int, 0644);
44 MODULE_PARM_DESC(video_debug,"enable debug messages [video]");
45 module_param(gbuffers, int, 0444);
46 MODULE_PARM_DESC(gbuffers,"number of capture buffers, range 2-32");
47 module_param(noninterlaced, int, 0644);
48 MODULE_PARM_DESC(noninterlaced,"capture non interlaced video");
49 module_param_string(secam, secam, sizeof(secam), 0644);
50 MODULE_PARM_DESC(secam, "force SECAM variant, either DK,L or Lc");
53 #define dprintk(fmt, arg...) if (video_debug&0x04) \
54 printk(KERN_DEBUG "%s/video: " fmt, dev->name , ## arg)
56 /* ------------------------------------------------------------------ */
57 /* Defines for Video Output Port Register at address 0x191 */
59 /* Bit 0: VIP code T bit polarity */
61 #define VP_T_CODE_P_NON_INVERTED 0x00
62 #define VP_T_CODE_P_INVERTED 0x01
64 /* ------------------------------------------------------------------ */
65 /* Defines for Video Output Port Register at address 0x195 */
67 /* Bit 2: Video output clock delay control */
69 #define VP_CLK_CTRL2_NOT_DELAYED 0x00
70 #define VP_CLK_CTRL2_DELAYED 0x04
72 /* Bit 1: Video output clock invert control */
74 #define VP_CLK_CTRL1_NON_INVERTED 0x00
75 #define VP_CLK_CTRL1_INVERTED 0x02
77 /* ------------------------------------------------------------------ */
78 /* Defines for Video Output Port Register at address 0x196 */
80 /* Bits 2 to 0: VSYNC pin video vertical sync type */
82 #define VP_VS_TYPE_MASK 0x07
84 #define VP_VS_TYPE_OFF 0x00
85 #define VP_VS_TYPE_V123 0x01
86 #define VP_VS_TYPE_V_ITU 0x02
87 #define VP_VS_TYPE_VGATE_L 0x03
88 #define VP_VS_TYPE_RESERVED1 0x04
89 #define VP_VS_TYPE_RESERVED2 0x05
90 #define VP_VS_TYPE_F_ITU 0x06
91 #define VP_VS_TYPE_SC_FID 0x07
93 /* ------------------------------------------------------------------ */
94 /* data structs for video */
96 static int video_out[][9] = {
97 [CCIR656] = { 0x00, 0xb1, 0x00, 0xa1, 0x00, 0x04, 0x06, 0x00, 0x00 },
100 static struct saa7134_format formats[] = {
102 .name = "8 bpp gray",
103 .fourcc = V4L2_PIX_FMT_GREY,
107 .name = "15 bpp RGB, le",
108 .fourcc = V4L2_PIX_FMT_RGB555,
112 .name = "15 bpp RGB, be",
113 .fourcc = V4L2_PIX_FMT_RGB555X,
118 .name = "16 bpp RGB, le",
119 .fourcc = V4L2_PIX_FMT_RGB565,
123 .name = "16 bpp RGB, be",
124 .fourcc = V4L2_PIX_FMT_RGB565X,
129 .name = "24 bpp RGB, le",
130 .fourcc = V4L2_PIX_FMT_BGR24,
134 .name = "24 bpp RGB, be",
135 .fourcc = V4L2_PIX_FMT_RGB24,
140 .name = "32 bpp RGB, le",
141 .fourcc = V4L2_PIX_FMT_BGR32,
145 .name = "32 bpp RGB, be",
146 .fourcc = V4L2_PIX_FMT_RGB32,
152 .name = "4:2:2 packed, YUYV",
153 .fourcc = V4L2_PIX_FMT_YUYV,
159 .name = "4:2:2 packed, UYVY",
160 .fourcc = V4L2_PIX_FMT_UYVY,
165 .name = "4:2:2 planar, Y-Cb-Cr",
166 .fourcc = V4L2_PIX_FMT_YUV422P,
174 .name = "4:2:0 planar, Y-Cb-Cr",
175 .fourcc = V4L2_PIX_FMT_YUV420,
183 .name = "4:2:0 planar, Y-Cb-Cr",
184 .fourcc = V4L2_PIX_FMT_YVU420,
194 #define FORMATS ARRAY_SIZE(formats)
196 #define NORM_625_50 \
199 .video_v_start = 24, \
200 .video_v_stop = 311, \
201 .vbi_v_start_0 = 7, \
202 .vbi_v_stop_0 = 22, \
203 .vbi_v_start_1 = 319, \
206 #define NORM_525_60 \
209 .video_v_start = 23, \
210 .video_v_stop = 262, \
211 .vbi_v_start_0 = 10, \
212 .vbi_v_stop_0 = 21, \
213 .vbi_v_start_1 = 273, \
216 static struct saa7134_tvnorm tvnorms[] = {
218 .name = "PAL", /* autodetect */
222 .sync_control = 0x18,
223 .luma_control = 0x40,
224 .chroma_ctrl1 = 0x81,
226 .chroma_ctrl2 = 0x06,
231 .id = V4L2_STD_PAL_BG,
234 .sync_control = 0x18,
235 .luma_control = 0x40,
236 .chroma_ctrl1 = 0x81,
238 .chroma_ctrl2 = 0x06,
243 .id = V4L2_STD_PAL_I,
246 .sync_control = 0x18,
247 .luma_control = 0x40,
248 .chroma_ctrl1 = 0x81,
250 .chroma_ctrl2 = 0x06,
255 .id = V4L2_STD_PAL_DK,
258 .sync_control = 0x18,
259 .luma_control = 0x40,
260 .chroma_ctrl1 = 0x81,
262 .chroma_ctrl2 = 0x06,
270 .sync_control = 0x59,
271 .luma_control = 0x40,
272 .chroma_ctrl1 = 0x89,
274 .chroma_ctrl2 = 0x0e,
279 .id = V4L2_STD_SECAM,
282 .sync_control = 0x18,
283 .luma_control = 0x1b,
284 .chroma_ctrl1 = 0xd1,
286 .chroma_ctrl2 = 0x00,
291 .id = V4L2_STD_SECAM_DK,
294 .sync_control = 0x18,
295 .luma_control = 0x1b,
296 .chroma_ctrl1 = 0xd1,
298 .chroma_ctrl2 = 0x00,
303 .id = V4L2_STD_SECAM_L,
306 .sync_control = 0x18,
307 .luma_control = 0x1b,
308 .chroma_ctrl1 = 0xd1,
310 .chroma_ctrl2 = 0x00,
315 .id = V4L2_STD_SECAM_LC,
318 .sync_control = 0x18,
319 .luma_control = 0x1b,
320 .chroma_ctrl1 = 0xd1,
322 .chroma_ctrl2 = 0x00,
327 .id = V4L2_STD_PAL_M,
330 .sync_control = 0x59,
331 .luma_control = 0x40,
332 .chroma_ctrl1 = 0xb9,
334 .chroma_ctrl2 = 0x0e,
339 .id = V4L2_STD_PAL_Nc,
342 .sync_control = 0x18,
343 .luma_control = 0x40,
344 .chroma_ctrl1 = 0xa1,
346 .chroma_ctrl2 = 0x06,
351 .id = V4L2_STD_PAL_60,
359 .vbi_v_start_1 = 273,
362 .sync_control = 0x18,
363 .luma_control = 0x40,
364 .chroma_ctrl1 = 0x81,
366 .chroma_ctrl2 = 0x06,
370 #define TVNORMS ARRAY_SIZE(tvnorms)
372 static struct saa7134_format* format_by_fourcc(unsigned int fourcc)
376 for (i = 0; i < FORMATS; i++)
377 if (formats[i].fourcc == fourcc)
382 /* ----------------------------------------------------------------------- */
383 /* resource management */
385 static int res_get(struct saa7134_dev *dev, struct saa7134_fh *fh, unsigned int bit)
387 if (fh->resources & bit)
388 /* have it already allocated */
392 mutex_lock(&dev->lock);
393 if (dev->resources & bit) {
394 /* no, someone else uses it */
395 mutex_unlock(&dev->lock);
398 /* it's free, grab it */
399 fh->resources |= bit;
400 dev->resources |= bit;
401 dprintk("res: get %d\n",bit);
402 mutex_unlock(&dev->lock);
406 static int res_check(struct saa7134_fh *fh, unsigned int bit)
408 return (fh->resources & bit);
411 static int res_locked(struct saa7134_dev *dev, unsigned int bit)
413 return (dev->resources & bit);
417 void res_free(struct saa7134_dev *dev, struct saa7134_fh *fh, unsigned int bits)
419 BUG_ON((fh->resources & bits) != bits);
421 mutex_lock(&dev->lock);
422 fh->resources &= ~bits;
423 dev->resources &= ~bits;
424 dprintk("res: put %d\n",bits);
425 mutex_unlock(&dev->lock);
428 /* ------------------------------------------------------------------ */
430 static void set_tvnorm(struct saa7134_dev *dev, struct saa7134_tvnorm *norm)
432 dprintk("set tv norm = %s\n",norm->name);
436 dev->crop_bounds.left = norm->h_start;
437 dev->crop_defrect.left = norm->h_start;
438 dev->crop_bounds.width = norm->h_stop - norm->h_start +1;
439 dev->crop_defrect.width = norm->h_stop - norm->h_start +1;
441 dev->crop_bounds.top = (norm->vbi_v_stop_0+1)*2;
442 dev->crop_defrect.top = norm->video_v_start*2;
443 dev->crop_bounds.height = ((norm->id & V4L2_STD_525_60) ? 524 : 624)
444 - dev->crop_bounds.top;
445 dev->crop_defrect.height = (norm->video_v_stop - norm->video_v_start +1)*2;
447 dev->crop_current = dev->crop_defrect;
449 saa7134_set_tvnorm_hw(dev);
452 static void video_mux(struct saa7134_dev *dev, int input)
454 dprintk("video input = %d [%s]\n", input, card_in(dev, input).name);
455 dev->ctl_input = input;
456 set_tvnorm(dev, dev->tvnorm);
457 saa7134_tvaudio_setinput(dev, &card_in(dev, input));
461 static void saa7134_set_decoder(struct saa7134_dev *dev)
463 int luma_control, sync_control, mux;
465 struct saa7134_tvnorm *norm = dev->tvnorm;
466 mux = card_in(dev, dev->ctl_input).vmux;
468 luma_control = norm->luma_control;
469 sync_control = norm->sync_control;
472 luma_control |= 0x80; /* svideo */
473 if (noninterlaced || dev->nosignal)
474 sync_control |= 0x20;
476 /* setup video decoder */
477 saa_writeb(SAA7134_INCR_DELAY, 0x08);
478 saa_writeb(SAA7134_ANALOG_IN_CTRL1, 0xc0 | mux);
479 saa_writeb(SAA7134_ANALOG_IN_CTRL2, 0x00);
481 saa_writeb(SAA7134_ANALOG_IN_CTRL3, 0x90);
482 saa_writeb(SAA7134_ANALOG_IN_CTRL4, 0x90);
483 saa_writeb(SAA7134_HSYNC_START, 0xeb);
484 saa_writeb(SAA7134_HSYNC_STOP, 0xe0);
485 saa_writeb(SAA7134_SOURCE_TIMING1, norm->src_timing);
487 saa_writeb(SAA7134_SYNC_CTRL, sync_control);
488 saa_writeb(SAA7134_LUMA_CTRL, luma_control);
489 saa_writeb(SAA7134_DEC_LUMA_BRIGHT, dev->ctl_bright);
491 saa_writeb(SAA7134_DEC_LUMA_CONTRAST,
492 dev->ctl_invert ? -dev->ctl_contrast : dev->ctl_contrast);
494 saa_writeb(SAA7134_DEC_CHROMA_SATURATION,
495 dev->ctl_invert ? -dev->ctl_saturation : dev->ctl_saturation);
497 saa_writeb(SAA7134_DEC_CHROMA_HUE, dev->ctl_hue);
498 saa_writeb(SAA7134_CHROMA_CTRL1, norm->chroma_ctrl1);
499 saa_writeb(SAA7134_CHROMA_GAIN, norm->chroma_gain);
501 saa_writeb(SAA7134_CHROMA_CTRL2, norm->chroma_ctrl2);
502 saa_writeb(SAA7134_MODE_DELAY_CTRL, 0x00);
504 saa_writeb(SAA7134_ANALOG_ADC, 0x01);
505 saa_writeb(SAA7134_VGATE_START, 0x11);
506 saa_writeb(SAA7134_VGATE_STOP, 0xfe);
507 saa_writeb(SAA7134_MISC_VGATE_MSB, norm->vgate_misc);
508 saa_writeb(SAA7134_RAW_DATA_GAIN, 0x40);
509 saa_writeb(SAA7134_RAW_DATA_OFFSET, 0x80);
512 void saa7134_set_tvnorm_hw(struct saa7134_dev *dev)
514 saa7134_set_decoder(dev);
516 if (card_in(dev, dev->ctl_input).tv)
517 saa_call_all(dev, core, s_std, dev->tvnorm->id);
518 /* Set the correct norm for the saa6752hs. This function
519 does nothing if there is no saa6752hs. */
520 saa_call_empress(dev, core, s_std, dev->tvnorm->id);
523 static void set_h_prescale(struct saa7134_dev *dev, int task, int prescale)
525 static const struct {
532 /* XPSC XACL XC2_1 XDCG VPFY */
544 static const int count = ARRAY_SIZE(vals);
547 for (i = 0; i < count; i++)
548 if (vals[i].xpsc == prescale)
553 saa_writeb(SAA7134_H_PRESCALE(task), vals[i].xpsc);
554 saa_writeb(SAA7134_ACC_LENGTH(task), vals[i].xacl);
555 saa_writeb(SAA7134_LEVEL_CTRL(task),
556 (vals[i].xc2_1 << 3) | (vals[i].xdcg));
557 saa_andorb(SAA7134_FIR_PREFILTER_CTRL(task), 0x0f,
558 (vals[i].vpfy << 2) | vals[i].vpfy);
561 static void set_v_scale(struct saa7134_dev *dev, int task, int yscale)
565 saa_writeb(SAA7134_V_SCALE_RATIO1(task), yscale & 0xff);
566 saa_writeb(SAA7134_V_SCALE_RATIO2(task), yscale >> 8);
568 mirror = (dev->ctl_mirror) ? 0x02 : 0x00;
571 dprintk("yscale LPI yscale=%d\n",yscale);
572 saa_writeb(SAA7134_V_FILTER(task), 0x00 | mirror);
573 saa_writeb(SAA7134_LUMA_CONTRAST(task), 0x40);
574 saa_writeb(SAA7134_CHROMA_SATURATION(task), 0x40);
577 val = 0x40 * 1024 / yscale;
578 dprintk("yscale ACM yscale=%d val=0x%x\n",yscale,val);
579 saa_writeb(SAA7134_V_FILTER(task), 0x01 | mirror);
580 saa_writeb(SAA7134_LUMA_CONTRAST(task), val);
581 saa_writeb(SAA7134_CHROMA_SATURATION(task), val);
583 saa_writeb(SAA7134_LUMA_BRIGHT(task), 0x80);
586 static void set_size(struct saa7134_dev *dev, int task,
587 int width, int height, int interlace)
589 int prescale,xscale,yscale,y_even,y_odd;
590 int h_start, h_stop, v_start, v_stop;
591 int div = interlace ? 2 : 1;
593 /* setup video scaler */
594 h_start = dev->crop_current.left;
595 v_start = dev->crop_current.top/2;
596 h_stop = (dev->crop_current.left + dev->crop_current.width -1);
597 v_stop = (dev->crop_current.top + dev->crop_current.height -1)/2;
599 saa_writeb(SAA7134_VIDEO_H_START1(task), h_start & 0xff);
600 saa_writeb(SAA7134_VIDEO_H_START2(task), h_start >> 8);
601 saa_writeb(SAA7134_VIDEO_H_STOP1(task), h_stop & 0xff);
602 saa_writeb(SAA7134_VIDEO_H_STOP2(task), h_stop >> 8);
603 saa_writeb(SAA7134_VIDEO_V_START1(task), v_start & 0xff);
604 saa_writeb(SAA7134_VIDEO_V_START2(task), v_start >> 8);
605 saa_writeb(SAA7134_VIDEO_V_STOP1(task), v_stop & 0xff);
606 saa_writeb(SAA7134_VIDEO_V_STOP2(task), v_stop >> 8);
608 prescale = dev->crop_current.width / width;
611 xscale = 1024 * dev->crop_current.width / prescale / width;
612 yscale = 512 * div * dev->crop_current.height / height;
613 dprintk("prescale=%d xscale=%d yscale=%d\n",prescale,xscale,yscale);
614 set_h_prescale(dev,task,prescale);
615 saa_writeb(SAA7134_H_SCALE_INC1(task), xscale & 0xff);
616 saa_writeb(SAA7134_H_SCALE_INC2(task), xscale >> 8);
617 set_v_scale(dev,task,yscale);
619 saa_writeb(SAA7134_VIDEO_PIXELS1(task), width & 0xff);
620 saa_writeb(SAA7134_VIDEO_PIXELS2(task), width >> 8);
621 saa_writeb(SAA7134_VIDEO_LINES1(task), height/div & 0xff);
622 saa_writeb(SAA7134_VIDEO_LINES2(task), height/div >> 8);
624 /* deinterlace y offsets */
625 y_odd = dev->ctl_y_odd;
626 y_even = dev->ctl_y_even;
627 saa_writeb(SAA7134_V_PHASE_OFFSET0(task), y_odd);
628 saa_writeb(SAA7134_V_PHASE_OFFSET1(task), y_even);
629 saa_writeb(SAA7134_V_PHASE_OFFSET2(task), y_odd);
630 saa_writeb(SAA7134_V_PHASE_OFFSET3(task), y_even);
633 /* ------------------------------------------------------------------ */
641 static void set_cliplist(struct saa7134_dev *dev, int reg,
642 struct cliplist *cl, int entries, char *name)
647 for (i = 0; i < entries; i++) {
648 winbits |= cl[i].enable;
649 winbits &= ~cl[i].disable;
650 if (i < 15 && cl[i].position == cl[i+1].position)
652 saa_writeb(reg + 0, winbits);
653 saa_writeb(reg + 2, cl[i].position & 0xff);
654 saa_writeb(reg + 3, cl[i].position >> 8);
655 dprintk("clip: %s winbits=%02x pos=%d\n",
656 name,winbits,cl[i].position);
659 for (; reg < 0x400; reg += 8) {
660 saa_writeb(reg+ 0, 0);
661 saa_writeb(reg + 1, 0);
662 saa_writeb(reg + 2, 0);
663 saa_writeb(reg + 3, 0);
667 static int clip_range(int val)
674 /* Sort into smallest position first order */
675 static int cliplist_cmp(const void *a, const void *b)
677 const struct cliplist *cla = a;
678 const struct cliplist *clb = b;
679 if (cla->position < clb->position)
681 if (cla->position > clb->position)
686 static int setup_clipping(struct saa7134_dev *dev, struct v4l2_clip *clips,
687 int nclips, int interlace)
689 struct cliplist col[16], row[16];
690 int cols = 0, rows = 0, i;
691 int div = interlace ? 2 : 1;
693 memset(col, 0, sizeof(col));
694 memset(row, 0, sizeof(row));
695 for (i = 0; i < nclips && i < 8; i++) {
696 col[cols].position = clip_range(clips[i].c.left);
697 col[cols].enable = (1 << i);
699 col[cols].position = clip_range(clips[i].c.left+clips[i].c.width);
700 col[cols].disable = (1 << i);
702 row[rows].position = clip_range(clips[i].c.top / div);
703 row[rows].enable = (1 << i);
705 row[rows].position = clip_range((clips[i].c.top + clips[i].c.height)
707 row[rows].disable = (1 << i);
710 sort(col, cols, sizeof col[0], cliplist_cmp, NULL);
711 sort(row, rows, sizeof row[0], cliplist_cmp, NULL);
712 set_cliplist(dev,0x380,col,cols,"cols");
713 set_cliplist(dev,0x384,row,rows,"rows");
717 static int verify_preview(struct saa7134_dev *dev, struct v4l2_window *win, bool try)
719 enum v4l2_field field;
722 if (!try && (dev->ovbuf.base == NULL || dev->ovfmt == NULL))
724 if (win->w.width < 48)
726 if (win->w.height < 32)
728 if (win->clipcount > 8)
732 win->global_alpha = 0;
734 maxw = dev->crop_current.width;
735 maxh = dev->crop_current.height;
737 if (V4L2_FIELD_ANY == field) {
738 field = (win->w.height > maxh/2)
739 ? V4L2_FIELD_INTERLACED
744 case V4L2_FIELD_BOTTOM:
748 field = V4L2_FIELD_INTERLACED;
753 if (win->w.width > maxw)
755 if (win->w.height > maxh)
756 win->w.height = maxh;
760 static int start_preview(struct saa7134_dev *dev)
762 unsigned long base,control,bpl;
765 err = verify_preview(dev, &dev->win, false);
769 dev->ovfield = dev->win.field;
770 dprintk("start_preview %dx%d+%d+%d %s field=%s\n",
771 dev->win.w.width, dev->win.w.height,
772 dev->win.w.left, dev->win.w.top,
773 dev->ovfmt->name, v4l2_field_names[dev->ovfield]);
775 /* setup window + clipping */
776 set_size(dev, TASK_B, dev->win.w.width, dev->win.w.height,
777 V4L2_FIELD_HAS_BOTH(dev->ovfield));
778 setup_clipping(dev, dev->clips, dev->nclips,
779 V4L2_FIELD_HAS_BOTH(dev->ovfield));
781 saa_andorb(SAA7134_DATA_PATH(TASK_B), 0x3f, 0x03);
783 saa_andorb(SAA7134_DATA_PATH(TASK_B), 0x3f, 0x01);
784 saa_writeb(SAA7134_OFMT_VIDEO_B, dev->ovfmt->pm | 0x20);
786 /* dma: setup channel 1 (= Video Task B) */
787 base = (unsigned long)dev->ovbuf.base;
788 base += dev->ovbuf.fmt.bytesperline * dev->win.w.top;
789 base += dev->ovfmt->depth/8 * dev->win.w.left;
790 bpl = dev->ovbuf.fmt.bytesperline;
791 control = SAA7134_RS_CONTROL_BURST_16;
792 if (dev->ovfmt->bswap)
793 control |= SAA7134_RS_CONTROL_BSWAP;
794 if (dev->ovfmt->wswap)
795 control |= SAA7134_RS_CONTROL_WSWAP;
796 if (V4L2_FIELD_HAS_BOTH(dev->ovfield)) {
797 saa_writel(SAA7134_RS_BA1(1),base);
798 saa_writel(SAA7134_RS_BA2(1),base+bpl);
799 saa_writel(SAA7134_RS_PITCH(1),bpl*2);
800 saa_writel(SAA7134_RS_CONTROL(1),control);
802 saa_writel(SAA7134_RS_BA1(1),base);
803 saa_writel(SAA7134_RS_BA2(1),base);
804 saa_writel(SAA7134_RS_PITCH(1),bpl);
805 saa_writel(SAA7134_RS_CONTROL(1),control);
810 saa7134_set_dmabits(dev);
815 static int stop_preview(struct saa7134_dev *dev)
818 saa7134_set_dmabits(dev);
822 /* ------------------------------------------------------------------ */
824 static int buffer_activate(struct saa7134_dev *dev,
825 struct saa7134_buf *buf,
826 struct saa7134_buf *next)
828 unsigned long base,control,bpl;
829 unsigned long bpl_uv,lines_uv,base2,base3,tmp; /* planar */
831 dprintk("buffer_activate buf=%p\n",buf);
832 buf->vb.state = VIDEOBUF_ACTIVE;
835 set_size(dev,TASK_A,buf->vb.width,buf->vb.height,
836 V4L2_FIELD_HAS_BOTH(buf->vb.field));
838 saa_andorb(SAA7134_DATA_PATH(TASK_A), 0x3f, 0x03);
840 saa_andorb(SAA7134_DATA_PATH(TASK_A), 0x3f, 0x01);
841 saa_writeb(SAA7134_OFMT_VIDEO_A, buf->fmt->pm);
843 /* DMA: setup channel 0 (= Video Task A0) */
844 base = saa7134_buffer_base(buf);
845 if (buf->fmt->planar)
848 bpl = (buf->vb.width * buf->fmt->depth) / 8;
849 control = SAA7134_RS_CONTROL_BURST_16 |
850 SAA7134_RS_CONTROL_ME |
851 (buf->pt->dma >> 12);
853 control |= SAA7134_RS_CONTROL_BSWAP;
855 control |= SAA7134_RS_CONTROL_WSWAP;
856 if (V4L2_FIELD_HAS_BOTH(buf->vb.field)) {
858 saa_writel(SAA7134_RS_BA1(0),base);
859 saa_writel(SAA7134_RS_BA2(0),base+bpl);
860 saa_writel(SAA7134_RS_PITCH(0),bpl*2);
863 saa_writel(SAA7134_RS_BA1(0),base);
864 saa_writel(SAA7134_RS_BA2(0),base);
865 saa_writel(SAA7134_RS_PITCH(0),bpl);
867 saa_writel(SAA7134_RS_CONTROL(0),control);
869 if (buf->fmt->planar) {
870 /* DMA: setup channel 4+5 (= planar task A) */
871 bpl_uv = bpl >> buf->fmt->hshift;
872 lines_uv = buf->vb.height >> buf->fmt->vshift;
873 base2 = base + bpl * buf->vb.height;
874 base3 = base2 + bpl_uv * lines_uv;
875 if (buf->fmt->uvswap)
876 tmp = base2, base2 = base3, base3 = tmp;
877 dprintk("uv: bpl=%ld lines=%ld base2/3=%ld/%ld\n",
878 bpl_uv,lines_uv,base2,base3);
879 if (V4L2_FIELD_HAS_BOTH(buf->vb.field)) {
881 saa_writel(SAA7134_RS_BA1(4),base2);
882 saa_writel(SAA7134_RS_BA2(4),base2+bpl_uv);
883 saa_writel(SAA7134_RS_PITCH(4),bpl_uv*2);
884 saa_writel(SAA7134_RS_BA1(5),base3);
885 saa_writel(SAA7134_RS_BA2(5),base3+bpl_uv);
886 saa_writel(SAA7134_RS_PITCH(5),bpl_uv*2);
889 saa_writel(SAA7134_RS_BA1(4),base2);
890 saa_writel(SAA7134_RS_BA2(4),base2);
891 saa_writel(SAA7134_RS_PITCH(4),bpl_uv);
892 saa_writel(SAA7134_RS_BA1(5),base3);
893 saa_writel(SAA7134_RS_BA2(5),base3);
894 saa_writel(SAA7134_RS_PITCH(5),bpl_uv);
896 saa_writel(SAA7134_RS_CONTROL(4),control);
897 saa_writel(SAA7134_RS_CONTROL(5),control);
901 saa7134_set_dmabits(dev);
902 mod_timer(&dev->video_q.timeout, jiffies+BUFFER_TIMEOUT);
906 static int buffer_prepare(struct videobuf_queue *q,
907 struct videobuf_buffer *vb,
908 enum v4l2_field field)
910 struct saa7134_dev *dev = q->priv_data;
911 struct saa7134_buf *buf = container_of(vb,struct saa7134_buf,vb);
916 if (NULL == dev->fmt)
918 if (dev->width < 48 ||
920 dev->width/4 > dev->crop_current.width ||
921 dev->height/4 > dev->crop_current.height ||
922 dev->width > dev->crop_bounds.width ||
923 dev->height > dev->crop_bounds.height)
925 size = (dev->width * dev->height * dev->fmt->depth) >> 3;
926 if (0 != buf->vb.baddr && buf->vb.bsize < size)
929 dprintk("buffer_prepare [%d,size=%dx%d,bytes=%d,fields=%s,%s]\n",
930 vb->i, dev->width, dev->height, size, v4l2_field_names[field],
932 if (buf->vb.width != dev->width ||
933 buf->vb.height != dev->height ||
934 buf->vb.size != size ||
935 buf->vb.field != field ||
936 buf->fmt != dev->fmt) {
937 saa7134_dma_free(q,buf);
940 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
941 struct videobuf_dmabuf *dma=videobuf_to_dma(&buf->vb);
943 buf->vb.width = dev->width;
944 buf->vb.height = dev->height;
946 buf->vb.field = field;
948 buf->pt = &dev->pt_cap;
949 dev->video_q.curr = NULL;
951 err = videobuf_iolock(q,&buf->vb,&dev->ovbuf);
954 err = saa7134_pgtable_build(dev->pci,buf->pt,
957 saa7134_buffer_startpage(buf));
961 buf->vb.state = VIDEOBUF_PREPARED;
962 buf->activate = buffer_activate;
966 saa7134_dma_free(q,buf);
971 buffer_setup(struct videobuf_queue *q, unsigned int *count, unsigned int *size)
973 struct saa7134_dev *dev = q->priv_data;
975 *size = dev->fmt->depth * dev->width * dev->height >> 3;
978 *count = saa7134_buffer_count(*size,*count);
982 static void buffer_queue(struct videobuf_queue *q, struct videobuf_buffer *vb)
984 struct saa7134_dev *dev = q->priv_data;
985 struct saa7134_buf *buf = container_of(vb,struct saa7134_buf,vb);
987 saa7134_buffer_queue(dev, &dev->video_q, buf);
990 static void buffer_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
992 struct saa7134_buf *buf = container_of(vb,struct saa7134_buf,vb);
994 saa7134_dma_free(q,buf);
997 static struct videobuf_queue_ops video_qops = {
998 .buf_setup = buffer_setup,
999 .buf_prepare = buffer_prepare,
1000 .buf_queue = buffer_queue,
1001 .buf_release = buffer_release,
1004 /* ------------------------------------------------------------------ */
1006 static int saa7134_s_ctrl(struct v4l2_ctrl *ctrl)
1008 struct saa7134_dev *dev = container_of(ctrl->handler, struct saa7134_dev, ctrl_handler);
1009 unsigned long flags;
1010 int restart_overlay = 0;
1013 case V4L2_CID_BRIGHTNESS:
1014 dev->ctl_bright = ctrl->val;
1015 saa_writeb(SAA7134_DEC_LUMA_BRIGHT, ctrl->val);
1018 dev->ctl_hue = ctrl->val;
1019 saa_writeb(SAA7134_DEC_CHROMA_HUE, ctrl->val);
1021 case V4L2_CID_CONTRAST:
1022 dev->ctl_contrast = ctrl->val;
1023 saa_writeb(SAA7134_DEC_LUMA_CONTRAST,
1024 dev->ctl_invert ? -dev->ctl_contrast : dev->ctl_contrast);
1026 case V4L2_CID_SATURATION:
1027 dev->ctl_saturation = ctrl->val;
1028 saa_writeb(SAA7134_DEC_CHROMA_SATURATION,
1029 dev->ctl_invert ? -dev->ctl_saturation : dev->ctl_saturation);
1031 case V4L2_CID_AUDIO_MUTE:
1032 dev->ctl_mute = ctrl->val;
1033 saa7134_tvaudio_setmute(dev);
1035 case V4L2_CID_AUDIO_VOLUME:
1036 dev->ctl_volume = ctrl->val;
1037 saa7134_tvaudio_setvolume(dev,dev->ctl_volume);
1039 case V4L2_CID_PRIVATE_INVERT:
1040 dev->ctl_invert = ctrl->val;
1041 saa_writeb(SAA7134_DEC_LUMA_CONTRAST,
1042 dev->ctl_invert ? -dev->ctl_contrast : dev->ctl_contrast);
1043 saa_writeb(SAA7134_DEC_CHROMA_SATURATION,
1044 dev->ctl_invert ? -dev->ctl_saturation : dev->ctl_saturation);
1046 case V4L2_CID_HFLIP:
1047 dev->ctl_mirror = ctrl->val;
1048 restart_overlay = 1;
1050 case V4L2_CID_PRIVATE_Y_EVEN:
1051 dev->ctl_y_even = ctrl->val;
1052 restart_overlay = 1;
1054 case V4L2_CID_PRIVATE_Y_ODD:
1055 dev->ctl_y_odd = ctrl->val;
1056 restart_overlay = 1;
1058 case V4L2_CID_PRIVATE_AUTOMUTE:
1060 struct v4l2_priv_tun_config tda9887_cfg;
1062 tda9887_cfg.tuner = TUNER_TDA9887;
1063 tda9887_cfg.priv = &dev->tda9887_conf;
1065 dev->ctl_automute = ctrl->val;
1066 if (dev->tda9887_conf) {
1067 if (dev->ctl_automute)
1068 dev->tda9887_conf |= TDA9887_AUTOMUTE;
1070 dev->tda9887_conf &= ~TDA9887_AUTOMUTE;
1072 saa_call_all(dev, tuner, s_config, &tda9887_cfg);
1079 if (restart_overlay && res_locked(dev, RESOURCE_OVERLAY)) {
1080 spin_lock_irqsave(&dev->slock, flags);
1083 spin_unlock_irqrestore(&dev->slock, flags);
1088 /* ------------------------------------------------------------------ */
1090 static struct videobuf_queue *saa7134_queue(struct file *file)
1092 struct video_device *vdev = video_devdata(file);
1093 struct saa7134_fh *fh = file->private_data;
1094 struct saa7134_dev *dev = fh->dev;
1095 struct videobuf_queue *q = NULL;
1097 switch (vdev->vfl_type) {
1098 case VFL_TYPE_GRABBER:
1110 static int saa7134_resource(struct file *file)
1112 struct video_device *vdev = video_devdata(file);
1114 if (vdev->vfl_type == VFL_TYPE_GRABBER)
1115 return RESOURCE_VIDEO;
1117 if (vdev->vfl_type == VFL_TYPE_VBI)
1118 return RESOURCE_VBI;
1124 static int video_open(struct file *file)
1126 struct video_device *vdev = video_devdata(file);
1127 struct saa7134_dev *dev = video_drvdata(file);
1128 struct saa7134_fh *fh;
1130 /* allocate + initialize per filehandle data */
1131 fh = kzalloc(sizeof(*fh),GFP_KERNEL);
1135 v4l2_fh_init(&fh->fh, vdev);
1136 file->private_data = fh;
1139 if (vdev->vfl_type == VFL_TYPE_RADIO) {
1140 /* switch to radio mode */
1141 saa7134_tvaudio_setinput(dev,&card(dev).radio);
1142 saa_call_all(dev, tuner, s_radio);
1144 /* switch to video/vbi mode */
1145 video_mux(dev,dev->ctl_input);
1147 v4l2_fh_add(&fh->fh);
1153 video_read(struct file *file, char __user *data, size_t count, loff_t *ppos)
1155 struct video_device *vdev = video_devdata(file);
1156 struct saa7134_fh *fh = file->private_data;
1158 switch (vdev->vfl_type) {
1159 case VFL_TYPE_GRABBER:
1160 if (res_locked(fh->dev,RESOURCE_VIDEO))
1162 return videobuf_read_one(saa7134_queue(file),
1164 file->f_flags & O_NONBLOCK);
1166 if (!res_get(fh->dev,fh,RESOURCE_VBI))
1168 return videobuf_read_stream(saa7134_queue(file),
1169 data, count, ppos, 1,
1170 file->f_flags & O_NONBLOCK);
1179 video_poll(struct file *file, struct poll_table_struct *wait)
1181 struct video_device *vdev = video_devdata(file);
1182 struct saa7134_fh *fh = file->private_data;
1183 struct saa7134_dev *dev = fh->dev;
1184 struct videobuf_buffer *buf = NULL;
1185 unsigned int rc = 0;
1187 if (vdev->vfl_type == VFL_TYPE_VBI)
1188 return videobuf_poll_stream(file, &dev->vbi, wait);
1190 if (res_check(fh,RESOURCE_VIDEO)) {
1191 mutex_lock(&dev->cap.vb_lock);
1192 if (!list_empty(&dev->cap.stream))
1193 buf = list_entry(dev->cap.stream.next, struct videobuf_buffer, stream);
1195 mutex_lock(&dev->cap.vb_lock);
1196 if (UNSET == dev->cap.read_off) {
1197 /* need to capture a new frame */
1198 if (res_locked(fh->dev,RESOURCE_VIDEO))
1200 if (0 != dev->cap.ops->buf_prepare(&dev->cap,
1201 dev->cap.read_buf, dev->cap.field))
1203 dev->cap.ops->buf_queue(&dev->cap, dev->cap.read_buf);
1204 dev->cap.read_off = 0;
1206 buf = dev->cap.read_buf;
1212 poll_wait(file, &buf->done, wait);
1213 if (buf->state == VIDEOBUF_DONE ||
1214 buf->state == VIDEOBUF_ERROR)
1215 rc = POLLIN|POLLRDNORM;
1216 mutex_unlock(&dev->cap.vb_lock);
1220 mutex_unlock(&dev->cap.vb_lock);
1224 static int video_release(struct file *file)
1226 struct video_device *vdev = video_devdata(file);
1227 struct saa7134_fh *fh = file->private_data;
1228 struct saa7134_dev *dev = fh->dev;
1229 struct saa6588_command cmd;
1230 unsigned long flags;
1232 saa7134_tvaudio_close(dev);
1234 /* turn off overlay */
1235 if (res_check(fh, RESOURCE_OVERLAY)) {
1236 spin_lock_irqsave(&dev->slock,flags);
1238 spin_unlock_irqrestore(&dev->slock,flags);
1239 res_free(dev,fh,RESOURCE_OVERLAY);
1242 /* stop video capture */
1243 if (res_check(fh, RESOURCE_VIDEO)) {
1244 pm_qos_remove_request(&dev->qos_request);
1245 videobuf_streamoff(&dev->cap);
1246 res_free(dev,fh,RESOURCE_VIDEO);
1247 videobuf_mmap_free(&dev->cap);
1249 if (dev->cap.read_buf) {
1250 buffer_release(&dev->cap, dev->cap.read_buf);
1251 kfree(dev->cap.read_buf);
1254 /* stop vbi capture */
1255 if (res_check(fh, RESOURCE_VBI)) {
1256 videobuf_stop(&dev->vbi);
1257 res_free(dev,fh,RESOURCE_VBI);
1258 videobuf_mmap_free(&dev->vbi);
1261 /* ts-capture will not work in planar mode, so turn it off Hac: 04.05*/
1262 saa_andorb(SAA7134_OFMT_VIDEO_A, 0x1f, 0);
1263 saa_andorb(SAA7134_OFMT_VIDEO_B, 0x1f, 0);
1264 saa_andorb(SAA7134_OFMT_DATA_A, 0x1f, 0);
1265 saa_andorb(SAA7134_OFMT_DATA_B, 0x1f, 0);
1267 saa_call_all(dev, core, s_power, 0);
1268 if (vdev->vfl_type == VFL_TYPE_RADIO)
1269 saa_call_all(dev, core, ioctl, SAA6588_CMD_CLOSE, &cmd);
1271 v4l2_fh_del(&fh->fh);
1272 v4l2_fh_exit(&fh->fh);
1273 file->private_data = NULL;
1278 static int video_mmap(struct file *file, struct vm_area_struct * vma)
1280 return videobuf_mmap_mapper(saa7134_queue(file), vma);
1283 static ssize_t radio_read(struct file *file, char __user *data,
1284 size_t count, loff_t *ppos)
1286 struct saa7134_fh *fh = file->private_data;
1287 struct saa7134_dev *dev = fh->dev;
1288 struct saa6588_command cmd;
1290 cmd.block_count = count/3;
1292 cmd.instance = file;
1293 cmd.result = -ENODEV;
1295 saa_call_all(dev, core, ioctl, SAA6588_CMD_READ, &cmd);
1300 static unsigned int radio_poll(struct file *file, poll_table *wait)
1302 struct saa7134_fh *fh = file->private_data;
1303 struct saa7134_dev *dev = fh->dev;
1304 struct saa6588_command cmd;
1306 cmd.instance = file;
1307 cmd.event_list = wait;
1308 cmd.result = -ENODEV;
1309 saa_call_all(dev, core, ioctl, SAA6588_CMD_POLL, &cmd);
1314 /* ------------------------------------------------------------------ */
1316 static int saa7134_try_get_set_fmt_vbi_cap(struct file *file, void *priv,
1317 struct v4l2_format *f)
1319 struct saa7134_fh *fh = priv;
1320 struct saa7134_dev *dev = fh->dev;
1321 struct saa7134_tvnorm *norm = dev->tvnorm;
1323 memset(&f->fmt.vbi.reserved, 0, sizeof(f->fmt.vbi.reserved));
1324 f->fmt.vbi.sampling_rate = 6750000 * 4;
1325 f->fmt.vbi.samples_per_line = 2048 /* VBI_LINE_LENGTH */;
1326 f->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
1327 f->fmt.vbi.offset = 64 * 4;
1328 f->fmt.vbi.start[0] = norm->vbi_v_start_0;
1329 f->fmt.vbi.count[0] = norm->vbi_v_stop_0 - norm->vbi_v_start_0 +1;
1330 f->fmt.vbi.start[1] = norm->vbi_v_start_1;
1331 f->fmt.vbi.count[1] = f->fmt.vbi.count[0];
1332 f->fmt.vbi.flags = 0; /* VBI_UNSYNC VBI_INTERLACED */
1337 static int saa7134_g_fmt_vid_cap(struct file *file, void *priv,
1338 struct v4l2_format *f)
1340 struct saa7134_fh *fh = priv;
1341 struct saa7134_dev *dev = fh->dev;
1343 f->fmt.pix.width = dev->width;
1344 f->fmt.pix.height = dev->height;
1345 f->fmt.pix.field = dev->cap.field;
1346 f->fmt.pix.pixelformat = dev->fmt->fourcc;
1347 f->fmt.pix.bytesperline =
1348 (f->fmt.pix.width * dev->fmt->depth) >> 3;
1349 f->fmt.pix.sizeimage =
1350 f->fmt.pix.height * f->fmt.pix.bytesperline;
1351 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
1352 f->fmt.pix.priv = 0;
1356 static int saa7134_g_fmt_vid_overlay(struct file *file, void *priv,
1357 struct v4l2_format *f)
1359 struct saa7134_fh *fh = priv;
1360 struct saa7134_dev *dev = fh->dev;
1361 struct v4l2_clip __user *clips = f->fmt.win.clips;
1362 u32 clipcount = f->fmt.win.clipcount;
1366 if (saa7134_no_overlay > 0) {
1367 printk(KERN_ERR "V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
1370 mutex_lock(&dev->lock);
1371 f->fmt.win = dev->win;
1372 f->fmt.win.clips = clips;
1375 if (dev->nclips < clipcount)
1376 clipcount = dev->nclips;
1377 f->fmt.win.clipcount = clipcount;
1379 for (i = 0; !err && i < clipcount; i++) {
1380 if (copy_to_user(&f->fmt.win.clips[i].c, &dev->clips[i].c,
1381 sizeof(struct v4l2_rect)))
1384 mutex_unlock(&dev->lock);
1389 static int saa7134_try_fmt_vid_cap(struct file *file, void *priv,
1390 struct v4l2_format *f)
1392 struct saa7134_fh *fh = priv;
1393 struct saa7134_dev *dev = fh->dev;
1394 struct saa7134_format *fmt;
1395 enum v4l2_field field;
1396 unsigned int maxw, maxh;
1398 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1402 field = f->fmt.pix.field;
1403 maxw = min(dev->crop_current.width*4, dev->crop_bounds.width);
1404 maxh = min(dev->crop_current.height*4, dev->crop_bounds.height);
1406 if (V4L2_FIELD_ANY == field) {
1407 field = (f->fmt.pix.height > maxh/2)
1408 ? V4L2_FIELD_INTERLACED
1409 : V4L2_FIELD_BOTTOM;
1412 case V4L2_FIELD_TOP:
1413 case V4L2_FIELD_BOTTOM:
1417 field = V4L2_FIELD_INTERLACED;
1421 f->fmt.pix.field = field;
1422 if (f->fmt.pix.width < 48)
1423 f->fmt.pix.width = 48;
1424 if (f->fmt.pix.height < 32)
1425 f->fmt.pix.height = 32;
1426 if (f->fmt.pix.width > maxw)
1427 f->fmt.pix.width = maxw;
1428 if (f->fmt.pix.height > maxh)
1429 f->fmt.pix.height = maxh;
1430 f->fmt.pix.width &= ~0x03;
1431 f->fmt.pix.bytesperline =
1432 (f->fmt.pix.width * fmt->depth) >> 3;
1433 f->fmt.pix.sizeimage =
1434 f->fmt.pix.height * f->fmt.pix.bytesperline;
1435 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
1436 f->fmt.pix.priv = 0;
1441 static int saa7134_try_fmt_vid_overlay(struct file *file, void *priv,
1442 struct v4l2_format *f)
1444 struct saa7134_fh *fh = priv;
1445 struct saa7134_dev *dev = fh->dev;
1447 if (saa7134_no_overlay > 0) {
1448 printk(KERN_ERR "V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
1452 if (f->fmt.win.clips == NULL)
1453 f->fmt.win.clipcount = 0;
1454 return verify_preview(dev, &f->fmt.win, true);
1457 static int saa7134_s_fmt_vid_cap(struct file *file, void *priv,
1458 struct v4l2_format *f)
1460 struct saa7134_fh *fh = priv;
1461 struct saa7134_dev *dev = fh->dev;
1464 err = saa7134_try_fmt_vid_cap(file, priv, f);
1468 dev->fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1469 dev->width = f->fmt.pix.width;
1470 dev->height = f->fmt.pix.height;
1471 dev->cap.field = f->fmt.pix.field;
1475 static int saa7134_s_fmt_vid_overlay(struct file *file, void *priv,
1476 struct v4l2_format *f)
1478 struct saa7134_fh *fh = priv;
1479 struct saa7134_dev *dev = fh->dev;
1481 unsigned long flags;
1483 if (saa7134_no_overlay > 0) {
1484 printk(KERN_ERR "V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
1487 if (f->fmt.win.clips == NULL)
1488 f->fmt.win.clipcount = 0;
1489 err = verify_preview(dev, &f->fmt.win, true);
1493 mutex_lock(&dev->lock);
1495 dev->win = f->fmt.win;
1496 dev->nclips = f->fmt.win.clipcount;
1498 if (copy_from_user(dev->clips, f->fmt.win.clips,
1499 sizeof(struct v4l2_clip) * dev->nclips)) {
1500 mutex_unlock(&dev->lock);
1504 if (res_check(fh, RESOURCE_OVERLAY)) {
1505 spin_lock_irqsave(&dev->slock, flags);
1508 spin_unlock_irqrestore(&dev->slock, flags);
1511 mutex_unlock(&dev->lock);
1515 static int saa7134_enum_input(struct file *file, void *priv,
1516 struct v4l2_input *i)
1518 struct saa7134_fh *fh = priv;
1519 struct saa7134_dev *dev = fh->dev;
1523 if (n >= SAA7134_INPUT_MAX)
1525 if (NULL == card_in(dev, i->index).name)
1528 i->type = V4L2_INPUT_TYPE_CAMERA;
1529 strcpy(i->name, card_in(dev, n).name);
1530 if (card_in(dev, n).tv)
1531 i->type = V4L2_INPUT_TYPE_TUNER;
1532 if (n == dev->ctl_input) {
1533 int v1 = saa_readb(SAA7134_STATUS_VIDEO1);
1534 int v2 = saa_readb(SAA7134_STATUS_VIDEO2);
1536 if (0 != (v1 & 0x40))
1537 i->status |= V4L2_IN_ST_NO_H_LOCK;
1538 if (0 != (v2 & 0x40))
1539 i->status |= V4L2_IN_ST_NO_SYNC;
1540 if (0 != (v2 & 0x0e))
1541 i->status |= V4L2_IN_ST_MACROVISION;
1543 i->std = SAA7134_NORMS;
1547 static int saa7134_g_input(struct file *file, void *priv, unsigned int *i)
1549 struct saa7134_fh *fh = priv;
1550 struct saa7134_dev *dev = fh->dev;
1552 *i = dev->ctl_input;
1556 static int saa7134_s_input(struct file *file, void *priv, unsigned int i)
1558 struct saa7134_fh *fh = priv;
1559 struct saa7134_dev *dev = fh->dev;
1561 if (i >= SAA7134_INPUT_MAX)
1563 if (NULL == card_in(dev, i).name)
1565 mutex_lock(&dev->lock);
1567 mutex_unlock(&dev->lock);
1571 static int saa7134_querycap(struct file *file, void *priv,
1572 struct v4l2_capability *cap)
1574 struct saa7134_fh *fh = priv;
1575 struct saa7134_dev *dev = fh->dev;
1576 struct video_device *vdev = video_devdata(file);
1577 u32 radio_caps, video_caps, vbi_caps;
1579 unsigned int tuner_type = dev->tuner_type;
1581 strcpy(cap->driver, "saa7134");
1582 strlcpy(cap->card, saa7134_boards[dev->board].name,
1584 sprintf(cap->bus_info, "PCI:%s", pci_name(dev->pci));
1586 cap->device_caps = V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
1587 if ((tuner_type != TUNER_ABSENT) && (tuner_type != UNSET))
1588 cap->device_caps |= V4L2_CAP_TUNER;
1590 radio_caps = V4L2_CAP_RADIO;
1592 radio_caps |= V4L2_CAP_RDS_CAPTURE;
1594 video_caps = V4L2_CAP_VIDEO_CAPTURE;
1595 if (saa7134_no_overlay <= 0)
1596 video_caps |= V4L2_CAP_VIDEO_OVERLAY;
1598 vbi_caps = V4L2_CAP_VBI_CAPTURE;
1600 switch (vdev->vfl_type) {
1601 case VFL_TYPE_RADIO:
1602 cap->device_caps |= radio_caps;
1604 case VFL_TYPE_GRABBER:
1605 cap->device_caps |= video_caps;
1608 cap->device_caps |= vbi_caps;
1611 cap->capabilities = radio_caps | video_caps | vbi_caps |
1612 cap->device_caps | V4L2_CAP_DEVICE_CAPS;
1613 if (vdev->vfl_type == VFL_TYPE_RADIO) {
1614 cap->device_caps &= ~V4L2_CAP_STREAMING;
1616 cap->device_caps &= ~V4L2_CAP_READWRITE;
1622 int saa7134_s_std_internal(struct saa7134_dev *dev, struct saa7134_fh *fh, v4l2_std_id id)
1624 unsigned long flags;
1628 if (!fh && res_locked(dev, RESOURCE_OVERLAY)) {
1629 /* Don't change the std from the mpeg device
1630 if overlay is active. */
1634 for (i = 0; i < TVNORMS; i++)
1635 if (id == tvnorms[i].id)
1639 for (i = 0; i < TVNORMS; i++)
1640 if (id & tvnorms[i].id)
1645 if ((id & V4L2_STD_SECAM) && (secam[0] != '-')) {
1646 if (secam[0] == 'L' || secam[0] == 'l') {
1647 if (secam[1] == 'C' || secam[1] == 'c')
1648 fixup = V4L2_STD_SECAM_LC;
1650 fixup = V4L2_STD_SECAM_L;
1652 if (secam[0] == 'D' || secam[0] == 'd')
1653 fixup = V4L2_STD_SECAM_DK;
1655 fixup = V4L2_STD_SECAM;
1657 for (i = 0; i < TVNORMS; i++) {
1658 if (fixup == tvnorms[i].id)
1667 mutex_lock(&dev->lock);
1668 if (fh && res_check(fh, RESOURCE_OVERLAY)) {
1669 spin_lock_irqsave(&dev->slock, flags);
1671 spin_unlock_irqrestore(&dev->slock, flags);
1673 set_tvnorm(dev, &tvnorms[i]);
1675 spin_lock_irqsave(&dev->slock, flags);
1677 spin_unlock_irqrestore(&dev->slock, flags);
1679 set_tvnorm(dev, &tvnorms[i]);
1681 saa7134_tvaudio_do_scan(dev);
1682 mutex_unlock(&dev->lock);
1685 EXPORT_SYMBOL_GPL(saa7134_s_std_internal);
1687 static int saa7134_s_std(struct file *file, void *priv, v4l2_std_id id)
1689 struct saa7134_fh *fh = priv;
1691 return saa7134_s_std_internal(fh->dev, fh, id);
1694 static int saa7134_g_std(struct file *file, void *priv, v4l2_std_id *id)
1696 struct saa7134_fh *fh = priv;
1697 struct saa7134_dev *dev = fh->dev;
1699 *id = dev->tvnorm->id;
1703 static int saa7134_cropcap(struct file *file, void *priv,
1704 struct v4l2_cropcap *cap)
1706 struct saa7134_fh *fh = priv;
1707 struct saa7134_dev *dev = fh->dev;
1709 if (cap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1710 cap->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
1712 cap->bounds = dev->crop_bounds;
1713 cap->defrect = dev->crop_defrect;
1714 cap->pixelaspect.numerator = 1;
1715 cap->pixelaspect.denominator = 1;
1716 if (dev->tvnorm->id & V4L2_STD_525_60) {
1717 cap->pixelaspect.numerator = 11;
1718 cap->pixelaspect.denominator = 10;
1720 if (dev->tvnorm->id & V4L2_STD_625_50) {
1721 cap->pixelaspect.numerator = 54;
1722 cap->pixelaspect.denominator = 59;
1727 static int saa7134_g_crop(struct file *file, void *f, struct v4l2_crop *crop)
1729 struct saa7134_fh *fh = f;
1730 struct saa7134_dev *dev = fh->dev;
1732 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1733 crop->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
1735 crop->c = dev->crop_current;
1739 static int saa7134_s_crop(struct file *file, void *f, const struct v4l2_crop *crop)
1741 struct saa7134_fh *fh = f;
1742 struct saa7134_dev *dev = fh->dev;
1743 struct v4l2_rect *b = &dev->crop_bounds;
1744 struct v4l2_rect *c = &dev->crop_current;
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))
1760 if (c->top < b->top)
1762 if (c->top > b->top + b->height)
1763 c->top = b->top + b->height;
1764 if (c->height > b->top - c->top + b->height)
1765 c->height = b->top - c->top + b->height;
1767 if (c->left < b->left)
1769 if (c->left > b->left + b->width)
1770 c->left = b->left + b->width;
1771 if (c->width > b->left - c->left + b->width)
1772 c->width = b->left - c->left + b->width;
1776 static int saa7134_g_tuner(struct file *file, void *priv,
1777 struct v4l2_tuner *t)
1779 struct saa7134_fh *fh = priv;
1780 struct saa7134_dev *dev = fh->dev;
1785 memset(t, 0, sizeof(*t));
1786 for (n = 0; n < SAA7134_INPUT_MAX; n++) {
1787 if (card_in(dev, n).tv)
1790 if (n == SAA7134_INPUT_MAX)
1792 if (NULL != card_in(dev, n).name) {
1793 strcpy(t->name, "Television");
1794 t->type = V4L2_TUNER_ANALOG_TV;
1795 saa_call_all(dev, tuner, g_tuner, t);
1796 t->capability = V4L2_TUNER_CAP_NORM |
1797 V4L2_TUNER_CAP_STEREO |
1798 V4L2_TUNER_CAP_LANG1 |
1799 V4L2_TUNER_CAP_LANG2;
1800 t->rxsubchans = saa7134_tvaudio_getstereo(dev);
1801 t->audmode = saa7134_tvaudio_rx2mode(t->rxsubchans);
1803 if (0 != (saa_readb(SAA7134_STATUS_VIDEO1) & 0x03))
1808 static int saa7134_s_tuner(struct file *file, void *priv,
1809 const struct v4l2_tuner *t)
1811 struct saa7134_fh *fh = priv;
1812 struct saa7134_dev *dev = fh->dev;
1818 mode = dev->thread.mode;
1819 if (UNSET == mode) {
1820 rx = saa7134_tvaudio_getstereo(dev);
1821 mode = saa7134_tvaudio_rx2mode(rx);
1823 if (mode != t->audmode)
1824 dev->thread.mode = t->audmode;
1829 static int saa7134_g_frequency(struct file *file, void *priv,
1830 struct v4l2_frequency *f)
1832 struct saa7134_fh *fh = priv;
1833 struct saa7134_dev *dev = fh->dev;
1838 saa_call_all(dev, tuner, g_frequency, f);
1843 static int saa7134_s_frequency(struct file *file, void *priv,
1844 const struct v4l2_frequency *f)
1846 struct saa7134_fh *fh = priv;
1847 struct saa7134_dev *dev = fh->dev;
1851 mutex_lock(&dev->lock);
1853 saa_call_all(dev, tuner, s_frequency, f);
1855 saa7134_tvaudio_do_scan(dev);
1856 mutex_unlock(&dev->lock);
1860 static int saa7134_enum_fmt_vid_cap(struct file *file, void *priv,
1861 struct v4l2_fmtdesc *f)
1863 if (f->index >= FORMATS)
1866 strlcpy(f->description, formats[f->index].name,
1867 sizeof(f->description));
1869 f->pixelformat = formats[f->index].fourcc;
1874 static int saa7134_enum_fmt_vid_overlay(struct file *file, void *priv,
1875 struct v4l2_fmtdesc *f)
1877 if (saa7134_no_overlay > 0) {
1878 printk(KERN_ERR "V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
1882 if ((f->index >= FORMATS) || formats[f->index].planar)
1885 strlcpy(f->description, formats[f->index].name,
1886 sizeof(f->description));
1888 f->pixelformat = formats[f->index].fourcc;
1893 static int saa7134_g_fbuf(struct file *file, void *f,
1894 struct v4l2_framebuffer *fb)
1896 struct saa7134_fh *fh = f;
1897 struct saa7134_dev *dev = fh->dev;
1900 fb->capability = V4L2_FBUF_CAP_LIST_CLIPPING;
1905 static int saa7134_s_fbuf(struct file *file, void *f,
1906 const struct v4l2_framebuffer *fb)
1908 struct saa7134_fh *fh = f;
1909 struct saa7134_dev *dev = fh->dev;
1910 struct saa7134_format *fmt;
1912 if (!capable(CAP_SYS_ADMIN) &&
1913 !capable(CAP_SYS_RAWIO))
1917 fmt = format_by_fourcc(fb->fmt.pixelformat);
1924 if (0 == dev->ovbuf.fmt.bytesperline)
1925 dev->ovbuf.fmt.bytesperline =
1926 dev->ovbuf.fmt.width*fmt->depth/8;
1930 static int saa7134_overlay(struct file *file, void *f, unsigned int on)
1932 struct saa7134_fh *fh = f;
1933 struct saa7134_dev *dev = fh->dev;
1934 unsigned long flags;
1937 if (saa7134_no_overlay > 0) {
1938 dprintk("no_overlay\n");
1942 if (!res_get(dev, fh, RESOURCE_OVERLAY))
1944 spin_lock_irqsave(&dev->slock, flags);
1946 spin_unlock_irqrestore(&dev->slock, flags);
1949 if (!res_check(fh, RESOURCE_OVERLAY))
1951 spin_lock_irqsave(&dev->slock, flags);
1953 spin_unlock_irqrestore(&dev->slock, flags);
1954 res_free(dev, fh, RESOURCE_OVERLAY);
1959 static int saa7134_reqbufs(struct file *file, void *priv,
1960 struct v4l2_requestbuffers *p)
1962 return videobuf_reqbufs(saa7134_queue(file), p);
1965 static int saa7134_querybuf(struct file *file, void *priv,
1966 struct v4l2_buffer *b)
1968 return videobuf_querybuf(saa7134_queue(file), b);
1971 static int saa7134_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1973 return videobuf_qbuf(saa7134_queue(file), b);
1976 static int saa7134_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1978 return videobuf_dqbuf(saa7134_queue(file), b,
1979 file->f_flags & O_NONBLOCK);
1982 static int saa7134_streamon(struct file *file, void *priv,
1983 enum v4l2_buf_type type)
1985 struct saa7134_fh *fh = priv;
1986 struct saa7134_dev *dev = fh->dev;
1987 int res = saa7134_resource(file);
1989 if (!res_get(dev, fh, res))
1992 /* The SAA7134 has a 1K FIFO; the datasheet suggests that when
1993 * configured conservatively, there's 22 usec of buffering for video.
1994 * We therefore request a DMA latency of 20 usec, giving us 2 usec of
1995 * margin in case the FIFO is configured differently to the datasheet.
1996 * Unfortunately, I lack register-level documentation to check the
1997 * Linux FIFO setup and confirm the perfect value.
1999 pm_qos_add_request(&dev->qos_request,
2000 PM_QOS_CPU_DMA_LATENCY,
2003 return videobuf_streamon(saa7134_queue(file));
2006 static int saa7134_streamoff(struct file *file, void *priv,
2007 enum v4l2_buf_type type)
2010 struct saa7134_fh *fh = priv;
2011 struct saa7134_dev *dev = fh->dev;
2012 int res = saa7134_resource(file);
2014 pm_qos_remove_request(&dev->qos_request);
2016 err = videobuf_streamoff(saa7134_queue(file));
2019 res_free(dev, fh, res);
2023 #ifdef CONFIG_VIDEO_ADV_DEBUG
2024 static int vidioc_g_register (struct file *file, void *priv,
2025 struct v4l2_dbg_register *reg)
2027 struct saa7134_fh *fh = priv;
2028 struct saa7134_dev *dev = fh->dev;
2030 reg->val = saa_readb(reg->reg & 0xffffff);
2035 static int vidioc_s_register (struct file *file, void *priv,
2036 const struct v4l2_dbg_register *reg)
2038 struct saa7134_fh *fh = priv;
2039 struct saa7134_dev *dev = fh->dev;
2041 saa_writeb(reg->reg & 0xffffff, reg->val);
2046 static int radio_g_tuner(struct file *file, void *priv,
2047 struct v4l2_tuner *t)
2049 struct saa7134_fh *fh = file->private_data;
2050 struct saa7134_dev *dev = fh->dev;
2055 strcpy(t->name, "Radio");
2057 saa_call_all(dev, tuner, g_tuner, t);
2058 t->audmode &= V4L2_TUNER_MODE_MONO | V4L2_TUNER_MODE_STEREO;
2059 if (dev->input->amux == TV) {
2060 t->signal = 0xf800 - ((saa_readb(0x581) & 0x1f) << 11);
2061 t->rxsubchans = (saa_readb(0x529) & 0x08) ?
2062 V4L2_TUNER_SUB_STEREO : V4L2_TUNER_SUB_MONO;
2066 static int radio_s_tuner(struct file *file, void *priv,
2067 const struct v4l2_tuner *t)
2069 struct saa7134_fh *fh = file->private_data;
2070 struct saa7134_dev *dev = fh->dev;
2075 saa_call_all(dev, tuner, s_tuner, t);
2079 static int radio_enum_input(struct file *file, void *priv,
2080 struct v4l2_input *i)
2085 strcpy(i->name, "Radio");
2086 i->type = V4L2_INPUT_TYPE_TUNER;
2091 static int radio_g_input(struct file *filp, void *priv, unsigned int *i)
2097 static int radio_s_input(struct file *filp, void *priv, unsigned int i)
2102 static int radio_s_std(struct file *file, void *fh, v4l2_std_id norm)
2107 static const struct v4l2_file_operations video_fops =
2109 .owner = THIS_MODULE,
2111 .release = video_release,
2115 .ioctl = video_ioctl2,
2118 static const struct v4l2_ioctl_ops video_ioctl_ops = {
2119 .vidioc_querycap = saa7134_querycap,
2120 .vidioc_enum_fmt_vid_cap = saa7134_enum_fmt_vid_cap,
2121 .vidioc_g_fmt_vid_cap = saa7134_g_fmt_vid_cap,
2122 .vidioc_try_fmt_vid_cap = saa7134_try_fmt_vid_cap,
2123 .vidioc_s_fmt_vid_cap = saa7134_s_fmt_vid_cap,
2124 .vidioc_enum_fmt_vid_overlay = saa7134_enum_fmt_vid_overlay,
2125 .vidioc_g_fmt_vid_overlay = saa7134_g_fmt_vid_overlay,
2126 .vidioc_try_fmt_vid_overlay = saa7134_try_fmt_vid_overlay,
2127 .vidioc_s_fmt_vid_overlay = saa7134_s_fmt_vid_overlay,
2128 .vidioc_g_fmt_vbi_cap = saa7134_try_get_set_fmt_vbi_cap,
2129 .vidioc_try_fmt_vbi_cap = saa7134_try_get_set_fmt_vbi_cap,
2130 .vidioc_s_fmt_vbi_cap = saa7134_try_get_set_fmt_vbi_cap,
2131 .vidioc_cropcap = saa7134_cropcap,
2132 .vidioc_reqbufs = saa7134_reqbufs,
2133 .vidioc_querybuf = saa7134_querybuf,
2134 .vidioc_qbuf = saa7134_qbuf,
2135 .vidioc_dqbuf = saa7134_dqbuf,
2136 .vidioc_s_std = saa7134_s_std,
2137 .vidioc_g_std = saa7134_g_std,
2138 .vidioc_enum_input = saa7134_enum_input,
2139 .vidioc_g_input = saa7134_g_input,
2140 .vidioc_s_input = saa7134_s_input,
2141 .vidioc_streamon = saa7134_streamon,
2142 .vidioc_streamoff = saa7134_streamoff,
2143 .vidioc_g_tuner = saa7134_g_tuner,
2144 .vidioc_s_tuner = saa7134_s_tuner,
2145 .vidioc_g_crop = saa7134_g_crop,
2146 .vidioc_s_crop = saa7134_s_crop,
2147 .vidioc_g_fbuf = saa7134_g_fbuf,
2148 .vidioc_s_fbuf = saa7134_s_fbuf,
2149 .vidioc_overlay = saa7134_overlay,
2150 .vidioc_g_frequency = saa7134_g_frequency,
2151 .vidioc_s_frequency = saa7134_s_frequency,
2152 #ifdef CONFIG_VIDEO_ADV_DEBUG
2153 .vidioc_g_register = vidioc_g_register,
2154 .vidioc_s_register = vidioc_s_register,
2158 static const struct v4l2_file_operations radio_fops = {
2159 .owner = THIS_MODULE,
2162 .release = video_release,
2163 .ioctl = video_ioctl2,
2167 static const struct v4l2_ioctl_ops radio_ioctl_ops = {
2168 .vidioc_querycap = saa7134_querycap,
2169 .vidioc_g_tuner = radio_g_tuner,
2170 .vidioc_enum_input = radio_enum_input,
2171 .vidioc_s_tuner = radio_s_tuner,
2172 .vidioc_s_input = radio_s_input,
2173 .vidioc_s_std = radio_s_std,
2174 .vidioc_g_input = radio_g_input,
2175 .vidioc_g_frequency = saa7134_g_frequency,
2176 .vidioc_s_frequency = saa7134_s_frequency,
2179 /* ----------------------------------------------------------- */
2180 /* exported stuff */
2182 struct video_device saa7134_video_template = {
2183 .name = "saa7134-video",
2184 .fops = &video_fops,
2185 .ioctl_ops = &video_ioctl_ops,
2186 .tvnorms = SAA7134_NORMS,
2189 struct video_device saa7134_radio_template = {
2190 .name = "saa7134-radio",
2191 .fops = &radio_fops,
2192 .ioctl_ops = &radio_ioctl_ops,
2195 static const struct v4l2_ctrl_ops saa7134_ctrl_ops = {
2196 .s_ctrl = saa7134_s_ctrl,
2199 static const struct v4l2_ctrl_config saa7134_ctrl_invert = {
2200 .ops = &saa7134_ctrl_ops,
2201 .id = V4L2_CID_PRIVATE_INVERT,
2203 .type = V4L2_CTRL_TYPE_BOOLEAN,
2209 static const struct v4l2_ctrl_config saa7134_ctrl_y_odd = {
2210 .ops = &saa7134_ctrl_ops,
2211 .id = V4L2_CID_PRIVATE_Y_ODD,
2212 .name = "Y Offset Odd Field",
2213 .type = V4L2_CTRL_TYPE_INTEGER,
2219 static const struct v4l2_ctrl_config saa7134_ctrl_y_even = {
2220 .ops = &saa7134_ctrl_ops,
2221 .id = V4L2_CID_PRIVATE_Y_EVEN,
2222 .name = "Y Offset Even Field",
2223 .type = V4L2_CTRL_TYPE_INTEGER,
2229 static const struct v4l2_ctrl_config saa7134_ctrl_automute = {
2230 .ops = &saa7134_ctrl_ops,
2231 .id = V4L2_CID_PRIVATE_AUTOMUTE,
2233 .type = V4L2_CTRL_TYPE_BOOLEAN,
2240 int saa7134_video_init1(struct saa7134_dev *dev)
2242 struct v4l2_ctrl_handler *hdl = &dev->ctrl_handler;
2244 /* sanitycheck insmod options */
2245 if (gbuffers < 2 || gbuffers > VIDEO_MAX_FRAME)
2247 if (gbufsize > gbufsize_max)
2248 gbufsize = gbufsize_max;
2249 gbufsize = (gbufsize + PAGE_SIZE - 1) & PAGE_MASK;
2251 v4l2_ctrl_handler_init(hdl, 11);
2252 v4l2_ctrl_new_std(hdl, &saa7134_ctrl_ops,
2253 V4L2_CID_BRIGHTNESS, 0, 255, 1, 128);
2254 v4l2_ctrl_new_std(hdl, &saa7134_ctrl_ops,
2255 V4L2_CID_CONTRAST, 0, 127, 1, 68);
2256 v4l2_ctrl_new_std(hdl, &saa7134_ctrl_ops,
2257 V4L2_CID_SATURATION, 0, 127, 1, 64);
2258 v4l2_ctrl_new_std(hdl, &saa7134_ctrl_ops,
2259 V4L2_CID_HUE, -128, 127, 1, 0);
2260 v4l2_ctrl_new_std(hdl, &saa7134_ctrl_ops,
2261 V4L2_CID_HFLIP, 0, 1, 1, 0);
2262 v4l2_ctrl_new_std(hdl, &saa7134_ctrl_ops,
2263 V4L2_CID_AUDIO_MUTE, 0, 1, 1, 0);
2264 v4l2_ctrl_new_std(hdl, &saa7134_ctrl_ops,
2265 V4L2_CID_AUDIO_VOLUME, -15, 15, 1, 0);
2266 v4l2_ctrl_new_custom(hdl, &saa7134_ctrl_invert, NULL);
2267 v4l2_ctrl_new_custom(hdl, &saa7134_ctrl_y_odd, NULL);
2268 v4l2_ctrl_new_custom(hdl, &saa7134_ctrl_y_even, NULL);
2269 v4l2_ctrl_new_custom(hdl, &saa7134_ctrl_automute, NULL);
2272 if (card_has_radio(dev)) {
2273 hdl = &dev->radio_ctrl_handler;
2274 v4l2_ctrl_handler_init(hdl, 2);
2275 v4l2_ctrl_add_handler(hdl, &dev->ctrl_handler,
2276 v4l2_ctrl_radio_filter);
2282 if (dev->tda9887_conf && saa7134_ctrl_automute.def)
2283 dev->tda9887_conf |= TDA9887_AUTOMUTE;
2286 INIT_LIST_HEAD(&dev->video_q.queue);
2287 init_timer(&dev->video_q.timeout);
2288 dev->video_q.timeout.function = saa7134_buffer_timeout;
2289 dev->video_q.timeout.data = (unsigned long)(&dev->video_q);
2290 dev->video_q.dev = dev;
2291 dev->fmt = format_by_fourcc(V4L2_PIX_FMT_BGR24);
2294 dev->win.w.width = dev->width;
2295 dev->win.w.height = dev->height;
2296 dev->win.field = V4L2_FIELD_INTERLACED;
2297 dev->ovbuf.fmt.width = dev->width;
2298 dev->ovbuf.fmt.height = dev->height;
2299 dev->ovbuf.fmt.pixelformat = dev->fmt->fourcc;
2300 dev->ovbuf.fmt.colorspace = V4L2_COLORSPACE_SMPTE170M;
2302 if (saa7134_boards[dev->board].video_out)
2303 saa7134_videoport_init(dev);
2305 videobuf_queue_sg_init(&dev->cap, &video_qops,
2306 &dev->pci->dev, &dev->slock,
2307 V4L2_BUF_TYPE_VIDEO_CAPTURE,
2308 V4L2_FIELD_INTERLACED,
2309 sizeof(struct saa7134_buf),
2311 videobuf_queue_sg_init(&dev->vbi, &saa7134_vbi_qops,
2312 &dev->pci->dev, &dev->slock,
2313 V4L2_BUF_TYPE_VBI_CAPTURE,
2315 sizeof(struct saa7134_buf),
2317 saa7134_pgtable_alloc(dev->pci, &dev->pt_cap);
2318 saa7134_pgtable_alloc(dev->pci, &dev->pt_vbi);
2323 void saa7134_video_fini(struct saa7134_dev *dev)
2326 saa7134_pgtable_free(dev->pci, &dev->pt_cap);
2327 saa7134_pgtable_free(dev->pci, &dev->pt_vbi);
2328 v4l2_ctrl_handler_free(&dev->ctrl_handler);
2329 if (card_has_radio(dev))
2330 v4l2_ctrl_handler_free(&dev->radio_ctrl_handler);
2333 int saa7134_videoport_init(struct saa7134_dev *dev)
2335 /* enable video output */
2336 int vo = saa7134_boards[dev->board].video_out;
2338 unsigned int vid_port_opts = saa7134_boards[dev->board].vid_port_opts;
2340 /* Configure videoport */
2341 saa_writeb(SAA7134_VIDEO_PORT_CTRL0, video_out[vo][0]);
2342 video_reg = video_out[vo][1];
2343 if (vid_port_opts & SET_T_CODE_POLARITY_NON_INVERTED)
2344 video_reg &= ~VP_T_CODE_P_INVERTED;
2345 saa_writeb(SAA7134_VIDEO_PORT_CTRL1, video_reg);
2346 saa_writeb(SAA7134_VIDEO_PORT_CTRL2, video_out[vo][2]);
2347 saa_writeb(SAA7134_VIDEO_PORT_CTRL4, video_out[vo][4]);
2348 video_reg = video_out[vo][5];
2349 if (vid_port_opts & SET_CLOCK_NOT_DELAYED)
2350 video_reg &= ~VP_CLK_CTRL2_DELAYED;
2351 if (vid_port_opts & SET_CLOCK_INVERTED)
2352 video_reg |= VP_CLK_CTRL1_INVERTED;
2353 saa_writeb(SAA7134_VIDEO_PORT_CTRL5, video_reg);
2354 video_reg = video_out[vo][6];
2355 if (vid_port_opts & SET_VSYNC_OFF) {
2356 video_reg &= ~VP_VS_TYPE_MASK;
2357 video_reg |= VP_VS_TYPE_OFF;
2359 saa_writeb(SAA7134_VIDEO_PORT_CTRL6, video_reg);
2360 saa_writeb(SAA7134_VIDEO_PORT_CTRL7, video_out[vo][7]);
2361 saa_writeb(SAA7134_VIDEO_PORT_CTRL8, video_out[vo][8]);
2363 /* Start videoport */
2364 saa_writeb(SAA7134_VIDEO_PORT_CTRL3, video_out[vo][3]);
2369 int saa7134_video_init2(struct saa7134_dev *dev)
2372 set_tvnorm(dev,&tvnorms[0]);
2374 v4l2_ctrl_handler_setup(&dev->ctrl_handler);
2375 saa7134_tvaudio_setmute(dev);
2376 saa7134_tvaudio_setvolume(dev,dev->ctl_volume);
2380 void saa7134_irq_video_signalchange(struct saa7134_dev *dev)
2382 static const char *st[] = {
2383 "(no signal)", "NTSC", "PAL", "SECAM" };
2386 st1 = saa_readb(SAA7134_STATUS_VIDEO1);
2387 st2 = saa_readb(SAA7134_STATUS_VIDEO2);
2388 dprintk("DCSDT: pll: %s, sync: %s, norm: %s\n",
2389 (st1 & 0x40) ? "not locked" : "locked",
2390 (st2 & 0x40) ? "no" : "yes",
2392 dev->nosignal = (st1 & 0x40) || (st2 & 0x40) || !(st2 & 0x1);
2394 if (dev->nosignal) {
2395 /* no video signal -> mute audio */
2396 if (dev->ctl_automute)
2398 saa7134_tvaudio_setmute(dev);
2400 /* wake up tvaudio audio carrier scan thread */
2401 saa7134_tvaudio_do_scan(dev);
2404 if ((st2 & 0x80) && !noninterlaced && !dev->nosignal)
2405 saa_clearb(SAA7134_SYNC_CTRL, 0x20);
2407 saa_setb(SAA7134_SYNC_CTRL, 0x20);
2409 if (dev->mops && dev->mops->signal_change)
2410 dev->mops->signal_change(dev);
2414 void saa7134_irq_video_done(struct saa7134_dev *dev, unsigned long status)
2416 enum v4l2_field field;
2418 spin_lock(&dev->slock);
2419 if (dev->video_q.curr) {
2420 dev->video_fieldcount++;
2421 field = dev->video_q.curr->vb.field;
2422 if (V4L2_FIELD_HAS_BOTH(field)) {
2423 /* make sure we have seen both fields */
2424 if ((status & 0x10) == 0x00) {
2425 dev->video_q.curr->top_seen = 1;
2428 if (!dev->video_q.curr->top_seen)
2430 } else if (field == V4L2_FIELD_TOP) {
2431 if ((status & 0x10) != 0x10)
2433 } else if (field == V4L2_FIELD_BOTTOM) {
2434 if ((status & 0x10) != 0x00)
2437 dev->video_q.curr->vb.field_count = dev->video_fieldcount;
2438 saa7134_buffer_finish(dev,&dev->video_q,VIDEOBUF_DONE);
2440 saa7134_buffer_next(dev,&dev->video_q);
2443 spin_unlock(&dev->slock);
2446 /* ----------------------------------------------------------- */