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);
407 void res_free(struct saa7134_dev *dev, struct saa7134_fh *fh, unsigned int bits)
409 BUG_ON((fh->resources & bits) != bits);
411 mutex_lock(&dev->lock);
412 fh->resources &= ~bits;
413 dev->resources &= ~bits;
414 dprintk("res: put %d\n",bits);
415 mutex_unlock(&dev->lock);
418 /* ------------------------------------------------------------------ */
420 static void set_tvnorm(struct saa7134_dev *dev, struct saa7134_tvnorm *norm)
422 dprintk("set tv norm = %s\n",norm->name);
426 dev->crop_bounds.left = norm->h_start;
427 dev->crop_defrect.left = norm->h_start;
428 dev->crop_bounds.width = norm->h_stop - norm->h_start +1;
429 dev->crop_defrect.width = norm->h_stop - norm->h_start +1;
431 dev->crop_bounds.top = (norm->vbi_v_stop_0+1)*2;
432 dev->crop_defrect.top = norm->video_v_start*2;
433 dev->crop_bounds.height = ((norm->id & V4L2_STD_525_60) ? 524 : 624)
434 - dev->crop_bounds.top;
435 dev->crop_defrect.height = (norm->video_v_stop - norm->video_v_start +1)*2;
437 dev->crop_current = dev->crop_defrect;
439 saa7134_set_tvnorm_hw(dev);
442 static void video_mux(struct saa7134_dev *dev, int input)
444 dprintk("video input = %d [%s]\n", input, card_in(dev, input).name);
445 dev->ctl_input = input;
446 set_tvnorm(dev, dev->tvnorm);
447 saa7134_tvaudio_setinput(dev, &card_in(dev, input));
451 static void saa7134_set_decoder(struct saa7134_dev *dev)
453 int luma_control, sync_control, mux;
455 struct saa7134_tvnorm *norm = dev->tvnorm;
456 mux = card_in(dev, dev->ctl_input).vmux;
458 luma_control = norm->luma_control;
459 sync_control = norm->sync_control;
462 luma_control |= 0x80; /* svideo */
463 if (noninterlaced || dev->nosignal)
464 sync_control |= 0x20;
466 /* setup video decoder */
467 saa_writeb(SAA7134_INCR_DELAY, 0x08);
468 saa_writeb(SAA7134_ANALOG_IN_CTRL1, 0xc0 | mux);
469 saa_writeb(SAA7134_ANALOG_IN_CTRL2, 0x00);
471 saa_writeb(SAA7134_ANALOG_IN_CTRL3, 0x90);
472 saa_writeb(SAA7134_ANALOG_IN_CTRL4, 0x90);
473 saa_writeb(SAA7134_HSYNC_START, 0xeb);
474 saa_writeb(SAA7134_HSYNC_STOP, 0xe0);
475 saa_writeb(SAA7134_SOURCE_TIMING1, norm->src_timing);
477 saa_writeb(SAA7134_SYNC_CTRL, sync_control);
478 saa_writeb(SAA7134_LUMA_CTRL, luma_control);
479 saa_writeb(SAA7134_DEC_LUMA_BRIGHT, dev->ctl_bright);
481 saa_writeb(SAA7134_DEC_LUMA_CONTRAST,
482 dev->ctl_invert ? -dev->ctl_contrast : dev->ctl_contrast);
484 saa_writeb(SAA7134_DEC_CHROMA_SATURATION,
485 dev->ctl_invert ? -dev->ctl_saturation : dev->ctl_saturation);
487 saa_writeb(SAA7134_DEC_CHROMA_HUE, dev->ctl_hue);
488 saa_writeb(SAA7134_CHROMA_CTRL1, norm->chroma_ctrl1);
489 saa_writeb(SAA7134_CHROMA_GAIN, norm->chroma_gain);
491 saa_writeb(SAA7134_CHROMA_CTRL2, norm->chroma_ctrl2);
492 saa_writeb(SAA7134_MODE_DELAY_CTRL, 0x00);
494 saa_writeb(SAA7134_ANALOG_ADC, 0x01);
495 saa_writeb(SAA7134_VGATE_START, 0x11);
496 saa_writeb(SAA7134_VGATE_STOP, 0xfe);
497 saa_writeb(SAA7134_MISC_VGATE_MSB, norm->vgate_misc);
498 saa_writeb(SAA7134_RAW_DATA_GAIN, 0x40);
499 saa_writeb(SAA7134_RAW_DATA_OFFSET, 0x80);
502 void saa7134_set_tvnorm_hw(struct saa7134_dev *dev)
504 saa7134_set_decoder(dev);
506 if (card_in(dev, dev->ctl_input).tv)
507 saa_call_all(dev, core, s_std, dev->tvnorm->id);
508 /* Set the correct norm for the saa6752hs. This function
509 does nothing if there is no saa6752hs. */
510 saa_call_empress(dev, core, s_std, dev->tvnorm->id);
513 static void set_h_prescale(struct saa7134_dev *dev, int task, int prescale)
515 static const struct {
522 /* XPSC XACL XC2_1 XDCG VPFY */
534 static const int count = ARRAY_SIZE(vals);
537 for (i = 0; i < count; i++)
538 if (vals[i].xpsc == prescale)
543 saa_writeb(SAA7134_H_PRESCALE(task), vals[i].xpsc);
544 saa_writeb(SAA7134_ACC_LENGTH(task), vals[i].xacl);
545 saa_writeb(SAA7134_LEVEL_CTRL(task),
546 (vals[i].xc2_1 << 3) | (vals[i].xdcg));
547 saa_andorb(SAA7134_FIR_PREFILTER_CTRL(task), 0x0f,
548 (vals[i].vpfy << 2) | vals[i].vpfy);
551 static void set_v_scale(struct saa7134_dev *dev, int task, int yscale)
555 saa_writeb(SAA7134_V_SCALE_RATIO1(task), yscale & 0xff);
556 saa_writeb(SAA7134_V_SCALE_RATIO2(task), yscale >> 8);
558 mirror = (dev->ctl_mirror) ? 0x02 : 0x00;
561 dprintk("yscale LPI yscale=%d\n",yscale);
562 saa_writeb(SAA7134_V_FILTER(task), 0x00 | mirror);
563 saa_writeb(SAA7134_LUMA_CONTRAST(task), 0x40);
564 saa_writeb(SAA7134_CHROMA_SATURATION(task), 0x40);
567 val = 0x40 * 1024 / yscale;
568 dprintk("yscale ACM yscale=%d val=0x%x\n",yscale,val);
569 saa_writeb(SAA7134_V_FILTER(task), 0x01 | mirror);
570 saa_writeb(SAA7134_LUMA_CONTRAST(task), val);
571 saa_writeb(SAA7134_CHROMA_SATURATION(task), val);
573 saa_writeb(SAA7134_LUMA_BRIGHT(task), 0x80);
576 static void set_size(struct saa7134_dev *dev, int task,
577 int width, int height, int interlace)
579 int prescale,xscale,yscale,y_even,y_odd;
580 int h_start, h_stop, v_start, v_stop;
581 int div = interlace ? 2 : 1;
583 /* setup video scaler */
584 h_start = dev->crop_current.left;
585 v_start = dev->crop_current.top/2;
586 h_stop = (dev->crop_current.left + dev->crop_current.width -1);
587 v_stop = (dev->crop_current.top + dev->crop_current.height -1)/2;
589 saa_writeb(SAA7134_VIDEO_H_START1(task), h_start & 0xff);
590 saa_writeb(SAA7134_VIDEO_H_START2(task), h_start >> 8);
591 saa_writeb(SAA7134_VIDEO_H_STOP1(task), h_stop & 0xff);
592 saa_writeb(SAA7134_VIDEO_H_STOP2(task), h_stop >> 8);
593 saa_writeb(SAA7134_VIDEO_V_START1(task), v_start & 0xff);
594 saa_writeb(SAA7134_VIDEO_V_START2(task), v_start >> 8);
595 saa_writeb(SAA7134_VIDEO_V_STOP1(task), v_stop & 0xff);
596 saa_writeb(SAA7134_VIDEO_V_STOP2(task), v_stop >> 8);
598 prescale = dev->crop_current.width / width;
601 xscale = 1024 * dev->crop_current.width / prescale / width;
602 yscale = 512 * div * dev->crop_current.height / height;
603 dprintk("prescale=%d xscale=%d yscale=%d\n",prescale,xscale,yscale);
604 set_h_prescale(dev,task,prescale);
605 saa_writeb(SAA7134_H_SCALE_INC1(task), xscale & 0xff);
606 saa_writeb(SAA7134_H_SCALE_INC2(task), xscale >> 8);
607 set_v_scale(dev,task,yscale);
609 saa_writeb(SAA7134_VIDEO_PIXELS1(task), width & 0xff);
610 saa_writeb(SAA7134_VIDEO_PIXELS2(task), width >> 8);
611 saa_writeb(SAA7134_VIDEO_LINES1(task), height/div & 0xff);
612 saa_writeb(SAA7134_VIDEO_LINES2(task), height/div >> 8);
614 /* deinterlace y offsets */
615 y_odd = dev->ctl_y_odd;
616 y_even = dev->ctl_y_even;
617 saa_writeb(SAA7134_V_PHASE_OFFSET0(task), y_odd);
618 saa_writeb(SAA7134_V_PHASE_OFFSET1(task), y_even);
619 saa_writeb(SAA7134_V_PHASE_OFFSET2(task), y_odd);
620 saa_writeb(SAA7134_V_PHASE_OFFSET3(task), y_even);
623 /* ------------------------------------------------------------------ */
631 static void set_cliplist(struct saa7134_dev *dev, int reg,
632 struct cliplist *cl, int entries, char *name)
637 for (i = 0; i < entries; i++) {
638 winbits |= cl[i].enable;
639 winbits &= ~cl[i].disable;
640 if (i < 15 && cl[i].position == cl[i+1].position)
642 saa_writeb(reg + 0, winbits);
643 saa_writeb(reg + 2, cl[i].position & 0xff);
644 saa_writeb(reg + 3, cl[i].position >> 8);
645 dprintk("clip: %s winbits=%02x pos=%d\n",
646 name,winbits,cl[i].position);
649 for (; reg < 0x400; reg += 8) {
650 saa_writeb(reg+ 0, 0);
651 saa_writeb(reg + 1, 0);
652 saa_writeb(reg + 2, 0);
653 saa_writeb(reg + 3, 0);
657 static int clip_range(int val)
664 /* Sort into smallest position first order */
665 static int cliplist_cmp(const void *a, const void *b)
667 const struct cliplist *cla = a;
668 const struct cliplist *clb = b;
669 if (cla->position < clb->position)
671 if (cla->position > clb->position)
676 static int setup_clipping(struct saa7134_dev *dev, struct v4l2_clip *clips,
677 int nclips, int interlace)
679 struct cliplist col[16], row[16];
680 int cols = 0, rows = 0, i;
681 int div = interlace ? 2 : 1;
683 memset(col, 0, sizeof(col));
684 memset(row, 0, sizeof(row));
685 for (i = 0; i < nclips && i < 8; i++) {
686 col[cols].position = clip_range(clips[i].c.left);
687 col[cols].enable = (1 << i);
689 col[cols].position = clip_range(clips[i].c.left+clips[i].c.width);
690 col[cols].disable = (1 << i);
692 row[rows].position = clip_range(clips[i].c.top / div);
693 row[rows].enable = (1 << i);
695 row[rows].position = clip_range((clips[i].c.top + clips[i].c.height)
697 row[rows].disable = (1 << i);
700 sort(col, cols, sizeof col[0], cliplist_cmp, NULL);
701 sort(row, rows, sizeof row[0], cliplist_cmp, NULL);
702 set_cliplist(dev,0x380,col,cols,"cols");
703 set_cliplist(dev,0x384,row,rows,"rows");
707 static int verify_preview(struct saa7134_dev *dev, struct v4l2_window *win, bool try)
709 enum v4l2_field field;
712 if (!try && (dev->ovbuf.base == NULL || dev->ovfmt == NULL))
714 if (win->w.width < 48)
716 if (win->w.height < 32)
718 if (win->clipcount > 8)
722 win->global_alpha = 0;
724 maxw = dev->crop_current.width;
725 maxh = dev->crop_current.height;
727 if (V4L2_FIELD_ANY == field) {
728 field = (win->w.height > maxh/2)
729 ? V4L2_FIELD_INTERLACED
734 case V4L2_FIELD_BOTTOM:
738 field = V4L2_FIELD_INTERLACED;
743 if (win->w.width > maxw)
745 if (win->w.height > maxh)
746 win->w.height = maxh;
750 static int start_preview(struct saa7134_dev *dev)
752 unsigned long base,control,bpl;
755 err = verify_preview(dev, &dev->win, false);
759 dev->ovfield = dev->win.field;
760 dprintk("start_preview %dx%d+%d+%d %s field=%s\n",
761 dev->win.w.width, dev->win.w.height,
762 dev->win.w.left, dev->win.w.top,
763 dev->ovfmt->name, v4l2_field_names[dev->ovfield]);
765 /* setup window + clipping */
766 set_size(dev, TASK_B, dev->win.w.width, dev->win.w.height,
767 V4L2_FIELD_HAS_BOTH(dev->ovfield));
768 setup_clipping(dev, dev->clips, dev->nclips,
769 V4L2_FIELD_HAS_BOTH(dev->ovfield));
771 saa_andorb(SAA7134_DATA_PATH(TASK_B), 0x3f, 0x03);
773 saa_andorb(SAA7134_DATA_PATH(TASK_B), 0x3f, 0x01);
774 saa_writeb(SAA7134_OFMT_VIDEO_B, dev->ovfmt->pm | 0x20);
776 /* dma: setup channel 1 (= Video Task B) */
777 base = (unsigned long)dev->ovbuf.base;
778 base += dev->ovbuf.fmt.bytesperline * dev->win.w.top;
779 base += dev->ovfmt->depth/8 * dev->win.w.left;
780 bpl = dev->ovbuf.fmt.bytesperline;
781 control = SAA7134_RS_CONTROL_BURST_16;
782 if (dev->ovfmt->bswap)
783 control |= SAA7134_RS_CONTROL_BSWAP;
784 if (dev->ovfmt->wswap)
785 control |= SAA7134_RS_CONTROL_WSWAP;
786 if (V4L2_FIELD_HAS_BOTH(dev->ovfield)) {
787 saa_writel(SAA7134_RS_BA1(1),base);
788 saa_writel(SAA7134_RS_BA2(1),base+bpl);
789 saa_writel(SAA7134_RS_PITCH(1),bpl*2);
790 saa_writel(SAA7134_RS_CONTROL(1),control);
792 saa_writel(SAA7134_RS_BA1(1),base);
793 saa_writel(SAA7134_RS_BA2(1),base);
794 saa_writel(SAA7134_RS_PITCH(1),bpl);
795 saa_writel(SAA7134_RS_CONTROL(1),control);
800 saa7134_set_dmabits(dev);
805 static int stop_preview(struct saa7134_dev *dev)
808 saa7134_set_dmabits(dev);
812 /* ------------------------------------------------------------------ */
814 static int buffer_activate(struct saa7134_dev *dev,
815 struct saa7134_buf *buf,
816 struct saa7134_buf *next)
818 unsigned long base,control,bpl;
819 unsigned long bpl_uv,lines_uv,base2,base3,tmp; /* planar */
821 dprintk("buffer_activate buf=%p\n",buf);
822 buf->vb.state = VIDEOBUF_ACTIVE;
825 set_size(dev,TASK_A,buf->vb.width,buf->vb.height,
826 V4L2_FIELD_HAS_BOTH(buf->vb.field));
828 saa_andorb(SAA7134_DATA_PATH(TASK_A), 0x3f, 0x03);
830 saa_andorb(SAA7134_DATA_PATH(TASK_A), 0x3f, 0x01);
831 saa_writeb(SAA7134_OFMT_VIDEO_A, buf->fmt->pm);
833 /* DMA: setup channel 0 (= Video Task A0) */
834 base = saa7134_buffer_base(buf);
835 if (buf->fmt->planar)
838 bpl = (buf->vb.width * buf->fmt->depth) / 8;
839 control = SAA7134_RS_CONTROL_BURST_16 |
840 SAA7134_RS_CONTROL_ME |
841 (buf->pt->dma >> 12);
843 control |= SAA7134_RS_CONTROL_BSWAP;
845 control |= SAA7134_RS_CONTROL_WSWAP;
846 if (V4L2_FIELD_HAS_BOTH(buf->vb.field)) {
848 saa_writel(SAA7134_RS_BA1(0),base);
849 saa_writel(SAA7134_RS_BA2(0),base+bpl);
850 saa_writel(SAA7134_RS_PITCH(0),bpl*2);
853 saa_writel(SAA7134_RS_BA1(0),base);
854 saa_writel(SAA7134_RS_BA2(0),base);
855 saa_writel(SAA7134_RS_PITCH(0),bpl);
857 saa_writel(SAA7134_RS_CONTROL(0),control);
859 if (buf->fmt->planar) {
860 /* DMA: setup channel 4+5 (= planar task A) */
861 bpl_uv = bpl >> buf->fmt->hshift;
862 lines_uv = buf->vb.height >> buf->fmt->vshift;
863 base2 = base + bpl * buf->vb.height;
864 base3 = base2 + bpl_uv * lines_uv;
865 if (buf->fmt->uvswap)
866 tmp = base2, base2 = base3, base3 = tmp;
867 dprintk("uv: bpl=%ld lines=%ld base2/3=%ld/%ld\n",
868 bpl_uv,lines_uv,base2,base3);
869 if (V4L2_FIELD_HAS_BOTH(buf->vb.field)) {
871 saa_writel(SAA7134_RS_BA1(4),base2);
872 saa_writel(SAA7134_RS_BA2(4),base2+bpl_uv);
873 saa_writel(SAA7134_RS_PITCH(4),bpl_uv*2);
874 saa_writel(SAA7134_RS_BA1(5),base3);
875 saa_writel(SAA7134_RS_BA2(5),base3+bpl_uv);
876 saa_writel(SAA7134_RS_PITCH(5),bpl_uv*2);
879 saa_writel(SAA7134_RS_BA1(4),base2);
880 saa_writel(SAA7134_RS_BA2(4),base2);
881 saa_writel(SAA7134_RS_PITCH(4),bpl_uv);
882 saa_writel(SAA7134_RS_BA1(5),base3);
883 saa_writel(SAA7134_RS_BA2(5),base3);
884 saa_writel(SAA7134_RS_PITCH(5),bpl_uv);
886 saa_writel(SAA7134_RS_CONTROL(4),control);
887 saa_writel(SAA7134_RS_CONTROL(5),control);
891 saa7134_set_dmabits(dev);
892 mod_timer(&dev->video_q.timeout, jiffies+BUFFER_TIMEOUT);
896 static int buffer_prepare(struct videobuf_queue *q,
897 struct videobuf_buffer *vb,
898 enum v4l2_field field)
900 struct saa7134_dev *dev = q->priv_data;
901 struct saa7134_buf *buf = container_of(vb,struct saa7134_buf,vb);
906 if (NULL == dev->fmt)
908 if (dev->width < 48 ||
910 dev->width/4 > dev->crop_current.width ||
911 dev->height/4 > dev->crop_current.height ||
912 dev->width > dev->crop_bounds.width ||
913 dev->height > dev->crop_bounds.height)
915 size = (dev->width * dev->height * dev->fmt->depth) >> 3;
916 if (0 != buf->vb.baddr && buf->vb.bsize < size)
919 dprintk("buffer_prepare [%d,size=%dx%d,bytes=%d,fields=%s,%s]\n",
920 vb->i, dev->width, dev->height, size, v4l2_field_names[field],
922 if (buf->vb.width != dev->width ||
923 buf->vb.height != dev->height ||
924 buf->vb.size != size ||
925 buf->vb.field != field ||
926 buf->fmt != dev->fmt) {
927 saa7134_dma_free(q,buf);
930 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
931 struct videobuf_dmabuf *dma=videobuf_to_dma(&buf->vb);
933 buf->vb.width = dev->width;
934 buf->vb.height = dev->height;
936 buf->vb.field = field;
938 buf->pt = &dev->pt_cap;
939 dev->video_q.curr = NULL;
941 err = videobuf_iolock(q,&buf->vb,&dev->ovbuf);
944 err = saa7134_pgtable_build(dev->pci,buf->pt,
947 saa7134_buffer_startpage(buf));
951 buf->vb.state = VIDEOBUF_PREPARED;
952 buf->activate = buffer_activate;
956 saa7134_dma_free(q,buf);
961 buffer_setup(struct videobuf_queue *q, unsigned int *count, unsigned int *size)
963 struct saa7134_dev *dev = q->priv_data;
965 *size = dev->fmt->depth * dev->width * dev->height >> 3;
968 *count = saa7134_buffer_count(*size,*count);
972 static void buffer_queue(struct videobuf_queue *q, struct videobuf_buffer *vb)
974 struct saa7134_dev *dev = q->priv_data;
975 struct saa7134_buf *buf = container_of(vb,struct saa7134_buf,vb);
977 saa7134_buffer_queue(dev, &dev->video_q, buf);
980 static void buffer_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
982 struct saa7134_buf *buf = container_of(vb,struct saa7134_buf,vb);
984 saa7134_dma_free(q,buf);
987 static struct videobuf_queue_ops video_qops = {
988 .buf_setup = buffer_setup,
989 .buf_prepare = buffer_prepare,
990 .buf_queue = buffer_queue,
991 .buf_release = buffer_release,
994 /* ------------------------------------------------------------------ */
996 static int saa7134_s_ctrl(struct v4l2_ctrl *ctrl)
998 struct saa7134_dev *dev = container_of(ctrl->handler, struct saa7134_dev, ctrl_handler);
1000 int restart_overlay = 0;
1003 case V4L2_CID_BRIGHTNESS:
1004 dev->ctl_bright = ctrl->val;
1005 saa_writeb(SAA7134_DEC_LUMA_BRIGHT, ctrl->val);
1008 dev->ctl_hue = ctrl->val;
1009 saa_writeb(SAA7134_DEC_CHROMA_HUE, ctrl->val);
1011 case V4L2_CID_CONTRAST:
1012 dev->ctl_contrast = ctrl->val;
1013 saa_writeb(SAA7134_DEC_LUMA_CONTRAST,
1014 dev->ctl_invert ? -dev->ctl_contrast : dev->ctl_contrast);
1016 case V4L2_CID_SATURATION:
1017 dev->ctl_saturation = ctrl->val;
1018 saa_writeb(SAA7134_DEC_CHROMA_SATURATION,
1019 dev->ctl_invert ? -dev->ctl_saturation : dev->ctl_saturation);
1021 case V4L2_CID_AUDIO_MUTE:
1022 dev->ctl_mute = ctrl->val;
1023 saa7134_tvaudio_setmute(dev);
1025 case V4L2_CID_AUDIO_VOLUME:
1026 dev->ctl_volume = ctrl->val;
1027 saa7134_tvaudio_setvolume(dev,dev->ctl_volume);
1029 case V4L2_CID_PRIVATE_INVERT:
1030 dev->ctl_invert = ctrl->val;
1031 saa_writeb(SAA7134_DEC_LUMA_CONTRAST,
1032 dev->ctl_invert ? -dev->ctl_contrast : dev->ctl_contrast);
1033 saa_writeb(SAA7134_DEC_CHROMA_SATURATION,
1034 dev->ctl_invert ? -dev->ctl_saturation : dev->ctl_saturation);
1036 case V4L2_CID_HFLIP:
1037 dev->ctl_mirror = ctrl->val;
1038 restart_overlay = 1;
1040 case V4L2_CID_PRIVATE_Y_EVEN:
1041 dev->ctl_y_even = ctrl->val;
1042 restart_overlay = 1;
1044 case V4L2_CID_PRIVATE_Y_ODD:
1045 dev->ctl_y_odd = ctrl->val;
1046 restart_overlay = 1;
1048 case V4L2_CID_PRIVATE_AUTOMUTE:
1050 struct v4l2_priv_tun_config tda9887_cfg;
1052 tda9887_cfg.tuner = TUNER_TDA9887;
1053 tda9887_cfg.priv = &dev->tda9887_conf;
1055 dev->ctl_automute = ctrl->val;
1056 if (dev->tda9887_conf) {
1057 if (dev->ctl_automute)
1058 dev->tda9887_conf |= TDA9887_AUTOMUTE;
1060 dev->tda9887_conf &= ~TDA9887_AUTOMUTE;
1062 saa_call_all(dev, tuner, s_config, &tda9887_cfg);
1069 if (restart_overlay && res_locked(dev, RESOURCE_OVERLAY)) {
1070 spin_lock_irqsave(&dev->slock, flags);
1073 spin_unlock_irqrestore(&dev->slock, flags);
1078 /* ------------------------------------------------------------------ */
1080 static struct videobuf_queue *saa7134_queue(struct file *file)
1082 struct video_device *vdev = video_devdata(file);
1083 struct saa7134_dev *dev = video_drvdata(file);
1084 struct saa7134_fh *fh = file->private_data;
1085 struct videobuf_queue *q = NULL;
1087 switch (vdev->vfl_type) {
1088 case VFL_TYPE_GRABBER:
1089 q = fh->is_empress ? &dev->empress_tsq : &dev->cap;
1100 static int saa7134_resource(struct file *file)
1102 struct video_device *vdev = video_devdata(file);
1103 struct saa7134_fh *fh = file->private_data;
1105 if (vdev->vfl_type == VFL_TYPE_GRABBER)
1106 return fh->is_empress ? RESOURCE_EMPRESS : RESOURCE_VIDEO;
1108 if (vdev->vfl_type == VFL_TYPE_VBI)
1109 return RESOURCE_VBI;
1115 static int video_open(struct file *file)
1117 struct video_device *vdev = video_devdata(file);
1118 struct saa7134_dev *dev = video_drvdata(file);
1119 struct saa7134_fh *fh;
1121 /* allocate + initialize per filehandle data */
1122 fh = kzalloc(sizeof(*fh),GFP_KERNEL);
1126 v4l2_fh_init(&fh->fh, vdev);
1127 file->private_data = fh;
1129 if (vdev->vfl_type == VFL_TYPE_RADIO) {
1130 /* switch to radio mode */
1131 saa7134_tvaudio_setinput(dev,&card(dev).radio);
1132 saa_call_all(dev, tuner, s_radio);
1134 /* switch to video/vbi mode */
1135 video_mux(dev,dev->ctl_input);
1137 v4l2_fh_add(&fh->fh);
1143 video_read(struct file *file, char __user *data, size_t count, loff_t *ppos)
1145 struct video_device *vdev = video_devdata(file);
1146 struct saa7134_dev *dev = video_drvdata(file);
1147 struct saa7134_fh *fh = file->private_data;
1149 switch (vdev->vfl_type) {
1150 case VFL_TYPE_GRABBER:
1151 if (res_locked(dev, RESOURCE_VIDEO))
1153 return videobuf_read_one(saa7134_queue(file),
1155 file->f_flags & O_NONBLOCK);
1157 if (!res_get(dev, fh, RESOURCE_VBI))
1159 return videobuf_read_stream(saa7134_queue(file),
1160 data, count, ppos, 1,
1161 file->f_flags & O_NONBLOCK);
1170 video_poll(struct file *file, struct poll_table_struct *wait)
1172 struct video_device *vdev = video_devdata(file);
1173 struct saa7134_dev *dev = video_drvdata(file);
1174 struct saa7134_fh *fh = file->private_data;
1175 struct videobuf_buffer *buf = NULL;
1176 unsigned int rc = 0;
1178 if (vdev->vfl_type == VFL_TYPE_VBI)
1179 return videobuf_poll_stream(file, &dev->vbi, wait);
1181 if (res_check(fh, RESOURCE_VIDEO)) {
1182 mutex_lock(&dev->cap.vb_lock);
1183 if (!list_empty(&dev->cap.stream))
1184 buf = list_entry(dev->cap.stream.next, struct videobuf_buffer, stream);
1186 mutex_lock(&dev->cap.vb_lock);
1187 if (UNSET == dev->cap.read_off) {
1188 /* need to capture a new frame */
1189 if (res_locked(dev, RESOURCE_VIDEO))
1191 if (0 != dev->cap.ops->buf_prepare(&dev->cap,
1192 dev->cap.read_buf, dev->cap.field))
1194 dev->cap.ops->buf_queue(&dev->cap, dev->cap.read_buf);
1195 dev->cap.read_off = 0;
1197 buf = dev->cap.read_buf;
1203 poll_wait(file, &buf->done, wait);
1204 if (buf->state == VIDEOBUF_DONE ||
1205 buf->state == VIDEOBUF_ERROR)
1206 rc = POLLIN|POLLRDNORM;
1207 mutex_unlock(&dev->cap.vb_lock);
1211 mutex_unlock(&dev->cap.vb_lock);
1215 static int video_release(struct file *file)
1217 struct video_device *vdev = video_devdata(file);
1218 struct saa7134_dev *dev = video_drvdata(file);
1219 struct saa7134_fh *fh = file->private_data;
1220 struct saa6588_command cmd;
1221 unsigned long flags;
1223 saa7134_tvaudio_close(dev);
1225 /* turn off overlay */
1226 if (res_check(fh, RESOURCE_OVERLAY)) {
1227 spin_lock_irqsave(&dev->slock,flags);
1229 spin_unlock_irqrestore(&dev->slock,flags);
1230 res_free(dev, fh, RESOURCE_OVERLAY);
1233 /* stop video capture */
1234 if (res_check(fh, RESOURCE_VIDEO)) {
1235 pm_qos_remove_request(&dev->qos_request);
1236 videobuf_streamoff(&dev->cap);
1237 res_free(dev, fh, RESOURCE_VIDEO);
1238 videobuf_mmap_free(&dev->cap);
1240 if (dev->cap.read_buf) {
1241 buffer_release(&dev->cap, dev->cap.read_buf);
1242 kfree(dev->cap.read_buf);
1245 /* stop vbi capture */
1246 if (res_check(fh, RESOURCE_VBI)) {
1247 videobuf_stop(&dev->vbi);
1248 res_free(dev, fh, RESOURCE_VBI);
1249 videobuf_mmap_free(&dev->vbi);
1252 /* ts-capture will not work in planar mode, so turn it off Hac: 04.05*/
1253 saa_andorb(SAA7134_OFMT_VIDEO_A, 0x1f, 0);
1254 saa_andorb(SAA7134_OFMT_VIDEO_B, 0x1f, 0);
1255 saa_andorb(SAA7134_OFMT_DATA_A, 0x1f, 0);
1256 saa_andorb(SAA7134_OFMT_DATA_B, 0x1f, 0);
1258 saa_call_all(dev, core, s_power, 0);
1259 if (vdev->vfl_type == VFL_TYPE_RADIO)
1260 saa_call_all(dev, core, ioctl, SAA6588_CMD_CLOSE, &cmd);
1262 v4l2_fh_del(&fh->fh);
1263 v4l2_fh_exit(&fh->fh);
1264 file->private_data = NULL;
1269 static int video_mmap(struct file *file, struct vm_area_struct * vma)
1271 return videobuf_mmap_mapper(saa7134_queue(file), vma);
1274 static ssize_t radio_read(struct file *file, char __user *data,
1275 size_t count, loff_t *ppos)
1277 struct saa7134_dev *dev = video_drvdata(file);
1278 struct saa6588_command cmd;
1280 cmd.block_count = count/3;
1282 cmd.instance = file;
1283 cmd.result = -ENODEV;
1285 saa_call_all(dev, core, ioctl, SAA6588_CMD_READ, &cmd);
1290 static unsigned int radio_poll(struct file *file, poll_table *wait)
1292 struct saa7134_dev *dev = video_drvdata(file);
1293 struct saa6588_command cmd;
1295 cmd.instance = file;
1296 cmd.event_list = wait;
1297 cmd.result = -ENODEV;
1298 saa_call_all(dev, core, ioctl, SAA6588_CMD_POLL, &cmd);
1303 /* ------------------------------------------------------------------ */
1305 static int saa7134_try_get_set_fmt_vbi_cap(struct file *file, void *priv,
1306 struct v4l2_format *f)
1308 struct saa7134_dev *dev = video_drvdata(file);
1309 struct saa7134_tvnorm *norm = dev->tvnorm;
1311 memset(&f->fmt.vbi.reserved, 0, sizeof(f->fmt.vbi.reserved));
1312 f->fmt.vbi.sampling_rate = 6750000 * 4;
1313 f->fmt.vbi.samples_per_line = 2048 /* VBI_LINE_LENGTH */;
1314 f->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
1315 f->fmt.vbi.offset = 64 * 4;
1316 f->fmt.vbi.start[0] = norm->vbi_v_start_0;
1317 f->fmt.vbi.count[0] = norm->vbi_v_stop_0 - norm->vbi_v_start_0 +1;
1318 f->fmt.vbi.start[1] = norm->vbi_v_start_1;
1319 f->fmt.vbi.count[1] = f->fmt.vbi.count[0];
1320 f->fmt.vbi.flags = 0; /* VBI_UNSYNC VBI_INTERLACED */
1325 static int saa7134_g_fmt_vid_cap(struct file *file, void *priv,
1326 struct v4l2_format *f)
1328 struct saa7134_dev *dev = video_drvdata(file);
1330 f->fmt.pix.width = dev->width;
1331 f->fmt.pix.height = dev->height;
1332 f->fmt.pix.field = dev->cap.field;
1333 f->fmt.pix.pixelformat = dev->fmt->fourcc;
1334 f->fmt.pix.bytesperline =
1335 (f->fmt.pix.width * dev->fmt->depth) >> 3;
1336 f->fmt.pix.sizeimage =
1337 f->fmt.pix.height * f->fmt.pix.bytesperline;
1338 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
1339 f->fmt.pix.priv = 0;
1343 static int saa7134_g_fmt_vid_overlay(struct file *file, void *priv,
1344 struct v4l2_format *f)
1346 struct saa7134_dev *dev = video_drvdata(file);
1347 struct v4l2_clip __user *clips = f->fmt.win.clips;
1348 u32 clipcount = f->fmt.win.clipcount;
1352 if (saa7134_no_overlay > 0) {
1353 printk(KERN_ERR "V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
1356 mutex_lock(&dev->lock);
1357 f->fmt.win = dev->win;
1358 f->fmt.win.clips = clips;
1361 if (dev->nclips < clipcount)
1362 clipcount = dev->nclips;
1363 f->fmt.win.clipcount = clipcount;
1365 for (i = 0; !err && i < clipcount; i++) {
1366 if (copy_to_user(&f->fmt.win.clips[i].c, &dev->clips[i].c,
1367 sizeof(struct v4l2_rect)))
1370 mutex_unlock(&dev->lock);
1375 static int saa7134_try_fmt_vid_cap(struct file *file, void *priv,
1376 struct v4l2_format *f)
1378 struct saa7134_dev *dev = video_drvdata(file);
1379 struct saa7134_format *fmt;
1380 enum v4l2_field field;
1381 unsigned int maxw, maxh;
1383 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1387 field = f->fmt.pix.field;
1388 maxw = min(dev->crop_current.width*4, dev->crop_bounds.width);
1389 maxh = min(dev->crop_current.height*4, dev->crop_bounds.height);
1391 if (V4L2_FIELD_ANY == field) {
1392 field = (f->fmt.pix.height > maxh/2)
1393 ? V4L2_FIELD_INTERLACED
1394 : V4L2_FIELD_BOTTOM;
1397 case V4L2_FIELD_TOP:
1398 case V4L2_FIELD_BOTTOM:
1402 field = V4L2_FIELD_INTERLACED;
1406 f->fmt.pix.field = field;
1407 if (f->fmt.pix.width < 48)
1408 f->fmt.pix.width = 48;
1409 if (f->fmt.pix.height < 32)
1410 f->fmt.pix.height = 32;
1411 if (f->fmt.pix.width > maxw)
1412 f->fmt.pix.width = maxw;
1413 if (f->fmt.pix.height > maxh)
1414 f->fmt.pix.height = maxh;
1415 f->fmt.pix.width &= ~0x03;
1416 f->fmt.pix.bytesperline =
1417 (f->fmt.pix.width * fmt->depth) >> 3;
1418 f->fmt.pix.sizeimage =
1419 f->fmt.pix.height * f->fmt.pix.bytesperline;
1420 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
1421 f->fmt.pix.priv = 0;
1426 static int saa7134_try_fmt_vid_overlay(struct file *file, void *priv,
1427 struct v4l2_format *f)
1429 struct saa7134_dev *dev = video_drvdata(file);
1431 if (saa7134_no_overlay > 0) {
1432 printk(KERN_ERR "V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
1436 if (f->fmt.win.clips == NULL)
1437 f->fmt.win.clipcount = 0;
1438 return verify_preview(dev, &f->fmt.win, true);
1441 static int saa7134_s_fmt_vid_cap(struct file *file, void *priv,
1442 struct v4l2_format *f)
1444 struct saa7134_dev *dev = video_drvdata(file);
1447 err = saa7134_try_fmt_vid_cap(file, priv, f);
1451 dev->fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1452 dev->width = f->fmt.pix.width;
1453 dev->height = f->fmt.pix.height;
1454 dev->cap.field = f->fmt.pix.field;
1458 static int saa7134_s_fmt_vid_overlay(struct file *file, void *priv,
1459 struct v4l2_format *f)
1461 struct saa7134_dev *dev = video_drvdata(file);
1463 unsigned long flags;
1465 if (saa7134_no_overlay > 0) {
1466 printk(KERN_ERR "V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
1469 if (f->fmt.win.clips == NULL)
1470 f->fmt.win.clipcount = 0;
1471 err = verify_preview(dev, &f->fmt.win, true);
1475 mutex_lock(&dev->lock);
1477 dev->win = f->fmt.win;
1478 dev->nclips = f->fmt.win.clipcount;
1480 if (copy_from_user(dev->clips, f->fmt.win.clips,
1481 sizeof(struct v4l2_clip) * dev->nclips)) {
1482 mutex_unlock(&dev->lock);
1486 if (res_check(priv, RESOURCE_OVERLAY)) {
1487 spin_lock_irqsave(&dev->slock, flags);
1490 spin_unlock_irqrestore(&dev->slock, flags);
1493 mutex_unlock(&dev->lock);
1497 int saa7134_enum_input(struct file *file, void *priv, struct v4l2_input *i)
1499 struct saa7134_dev *dev = video_drvdata(file);
1503 if (n >= SAA7134_INPUT_MAX)
1505 if (NULL == card_in(dev, i->index).name)
1508 i->type = V4L2_INPUT_TYPE_CAMERA;
1509 strcpy(i->name, card_in(dev, n).name);
1510 if (card_in(dev, n).tv)
1511 i->type = V4L2_INPUT_TYPE_TUNER;
1512 if (n == dev->ctl_input) {
1513 int v1 = saa_readb(SAA7134_STATUS_VIDEO1);
1514 int v2 = saa_readb(SAA7134_STATUS_VIDEO2);
1516 if (0 != (v1 & 0x40))
1517 i->status |= V4L2_IN_ST_NO_H_LOCK;
1518 if (0 != (v2 & 0x40))
1519 i->status |= V4L2_IN_ST_NO_SYNC;
1520 if (0 != (v2 & 0x0e))
1521 i->status |= V4L2_IN_ST_MACROVISION;
1523 i->std = SAA7134_NORMS;
1526 EXPORT_SYMBOL_GPL(saa7134_enum_input);
1528 int saa7134_g_input(struct file *file, void *priv, unsigned int *i)
1530 struct saa7134_dev *dev = video_drvdata(file);
1532 *i = dev->ctl_input;
1535 EXPORT_SYMBOL_GPL(saa7134_g_input);
1537 int saa7134_s_input(struct file *file, void *priv, unsigned int i)
1539 struct saa7134_dev *dev = video_drvdata(file);
1541 if (i >= SAA7134_INPUT_MAX)
1543 if (NULL == card_in(dev, i).name)
1545 mutex_lock(&dev->lock);
1547 mutex_unlock(&dev->lock);
1550 EXPORT_SYMBOL_GPL(saa7134_s_input);
1552 int saa7134_querycap(struct file *file, void *priv,
1553 struct v4l2_capability *cap)
1555 struct saa7134_dev *dev = video_drvdata(file);
1556 struct video_device *vdev = video_devdata(file);
1557 struct saa7134_fh *fh = priv;
1558 u32 radio_caps, video_caps, vbi_caps;
1560 unsigned int tuner_type = dev->tuner_type;
1562 strcpy(cap->driver, "saa7134");
1563 strlcpy(cap->card, saa7134_boards[dev->board].name,
1565 sprintf(cap->bus_info, "PCI:%s", pci_name(dev->pci));
1567 cap->device_caps = V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
1568 if ((tuner_type != TUNER_ABSENT) && (tuner_type != UNSET))
1569 cap->device_caps |= V4L2_CAP_TUNER;
1571 radio_caps = V4L2_CAP_RADIO;
1573 radio_caps |= V4L2_CAP_RDS_CAPTURE;
1575 video_caps = V4L2_CAP_VIDEO_CAPTURE;
1576 if (saa7134_no_overlay <= 0 && !fh->is_empress)
1577 video_caps |= V4L2_CAP_VIDEO_OVERLAY;
1579 vbi_caps = V4L2_CAP_VBI_CAPTURE;
1581 switch (vdev->vfl_type) {
1582 case VFL_TYPE_RADIO:
1583 cap->device_caps |= radio_caps;
1585 case VFL_TYPE_GRABBER:
1586 cap->device_caps |= video_caps;
1589 cap->device_caps |= vbi_caps;
1592 cap->capabilities = radio_caps | video_caps | vbi_caps |
1593 cap->device_caps | V4L2_CAP_DEVICE_CAPS;
1594 if (vdev->vfl_type == VFL_TYPE_RADIO) {
1595 cap->device_caps &= ~V4L2_CAP_STREAMING;
1597 cap->device_caps &= ~V4L2_CAP_READWRITE;
1602 EXPORT_SYMBOL_GPL(saa7134_querycap);
1604 int saa7134_s_std(struct file *file, void *priv, v4l2_std_id id)
1606 struct saa7134_dev *dev = video_drvdata(file);
1607 struct saa7134_fh *fh = priv;
1608 unsigned long flags;
1612 if (fh->is_empress && res_locked(dev, RESOURCE_OVERLAY)) {
1613 /* Don't change the std from the mpeg device
1614 if overlay is active. */
1618 for (i = 0; i < TVNORMS; i++)
1619 if (id == tvnorms[i].id)
1623 for (i = 0; i < TVNORMS; i++)
1624 if (id & tvnorms[i].id)
1629 if ((id & V4L2_STD_SECAM) && (secam[0] != '-')) {
1630 if (secam[0] == 'L' || secam[0] == 'l') {
1631 if (secam[1] == 'C' || secam[1] == 'c')
1632 fixup = V4L2_STD_SECAM_LC;
1634 fixup = V4L2_STD_SECAM_L;
1636 if (secam[0] == 'D' || secam[0] == 'd')
1637 fixup = V4L2_STD_SECAM_DK;
1639 fixup = V4L2_STD_SECAM;
1641 for (i = 0; i < TVNORMS; i++) {
1642 if (fixup == tvnorms[i].id)
1651 mutex_lock(&dev->lock);
1652 if (!fh->is_empress && res_check(fh, RESOURCE_OVERLAY)) {
1653 spin_lock_irqsave(&dev->slock, flags);
1655 spin_unlock_irqrestore(&dev->slock, flags);
1657 set_tvnorm(dev, &tvnorms[i]);
1659 spin_lock_irqsave(&dev->slock, flags);
1661 spin_unlock_irqrestore(&dev->slock, flags);
1663 set_tvnorm(dev, &tvnorms[i]);
1665 saa7134_tvaudio_do_scan(dev);
1666 mutex_unlock(&dev->lock);
1669 EXPORT_SYMBOL_GPL(saa7134_s_std);
1671 int saa7134_g_std(struct file *file, void *priv, v4l2_std_id *id)
1673 struct saa7134_dev *dev = video_drvdata(file);
1675 *id = dev->tvnorm->id;
1678 EXPORT_SYMBOL_GPL(saa7134_g_std);
1680 static int saa7134_cropcap(struct file *file, void *priv,
1681 struct v4l2_cropcap *cap)
1683 struct saa7134_dev *dev = video_drvdata(file);
1685 if (cap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1686 cap->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
1688 cap->bounds = dev->crop_bounds;
1689 cap->defrect = dev->crop_defrect;
1690 cap->pixelaspect.numerator = 1;
1691 cap->pixelaspect.denominator = 1;
1692 if (dev->tvnorm->id & V4L2_STD_525_60) {
1693 cap->pixelaspect.numerator = 11;
1694 cap->pixelaspect.denominator = 10;
1696 if (dev->tvnorm->id & V4L2_STD_625_50) {
1697 cap->pixelaspect.numerator = 54;
1698 cap->pixelaspect.denominator = 59;
1703 static int saa7134_g_crop(struct file *file, void *f, struct v4l2_crop *crop)
1705 struct saa7134_dev *dev = video_drvdata(file);
1707 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1708 crop->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
1710 crop->c = dev->crop_current;
1714 static int saa7134_s_crop(struct file *file, void *f, const struct v4l2_crop *crop)
1716 struct saa7134_dev *dev = video_drvdata(file);
1717 struct v4l2_rect *b = &dev->crop_bounds;
1718 struct v4l2_rect *c = &dev->crop_current;
1720 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1721 crop->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
1723 if (crop->c.height < 0)
1725 if (crop->c.width < 0)
1728 if (res_locked(dev, RESOURCE_OVERLAY))
1730 if (res_locked(dev, RESOURCE_VIDEO))
1734 if (c->top < b->top)
1736 if (c->top > b->top + b->height)
1737 c->top = b->top + b->height;
1738 if (c->height > b->top - c->top + b->height)
1739 c->height = b->top - c->top + b->height;
1741 if (c->left < b->left)
1743 if (c->left > b->left + b->width)
1744 c->left = b->left + b->width;
1745 if (c->width > b->left - c->left + b->width)
1746 c->width = b->left - c->left + b->width;
1750 int saa7134_g_tuner(struct file *file, void *priv,
1751 struct v4l2_tuner *t)
1753 struct saa7134_dev *dev = video_drvdata(file);
1758 memset(t, 0, sizeof(*t));
1759 for (n = 0; n < SAA7134_INPUT_MAX; n++) {
1760 if (card_in(dev, n).tv)
1763 if (n == SAA7134_INPUT_MAX)
1765 if (NULL != card_in(dev, n).name) {
1766 strcpy(t->name, "Television");
1767 t->type = V4L2_TUNER_ANALOG_TV;
1768 saa_call_all(dev, tuner, g_tuner, t);
1769 t->capability = V4L2_TUNER_CAP_NORM |
1770 V4L2_TUNER_CAP_STEREO |
1771 V4L2_TUNER_CAP_LANG1 |
1772 V4L2_TUNER_CAP_LANG2;
1773 t->rxsubchans = saa7134_tvaudio_getstereo(dev);
1774 t->audmode = saa7134_tvaudio_rx2mode(t->rxsubchans);
1776 if (0 != (saa_readb(SAA7134_STATUS_VIDEO1) & 0x03))
1780 EXPORT_SYMBOL_GPL(saa7134_g_tuner);
1782 int saa7134_s_tuner(struct file *file, void *priv,
1783 const struct v4l2_tuner *t)
1785 struct saa7134_dev *dev = video_drvdata(file);
1791 mode = dev->thread.mode;
1792 if (UNSET == mode) {
1793 rx = saa7134_tvaudio_getstereo(dev);
1794 mode = saa7134_tvaudio_rx2mode(rx);
1796 if (mode != t->audmode)
1797 dev->thread.mode = t->audmode;
1801 EXPORT_SYMBOL_GPL(saa7134_s_tuner);
1803 int saa7134_g_frequency(struct file *file, void *priv,
1804 struct v4l2_frequency *f)
1806 struct saa7134_dev *dev = video_drvdata(file);
1811 saa_call_all(dev, tuner, g_frequency, f);
1815 EXPORT_SYMBOL_GPL(saa7134_g_frequency);
1817 int saa7134_s_frequency(struct file *file, void *priv,
1818 const struct v4l2_frequency *f)
1820 struct saa7134_dev *dev = video_drvdata(file);
1824 mutex_lock(&dev->lock);
1826 saa_call_all(dev, tuner, s_frequency, f);
1828 saa7134_tvaudio_do_scan(dev);
1829 mutex_unlock(&dev->lock);
1832 EXPORT_SYMBOL_GPL(saa7134_s_frequency);
1834 static int saa7134_enum_fmt_vid_cap(struct file *file, void *priv,
1835 struct v4l2_fmtdesc *f)
1837 if (f->index >= FORMATS)
1840 strlcpy(f->description, formats[f->index].name,
1841 sizeof(f->description));
1843 f->pixelformat = formats[f->index].fourcc;
1848 static int saa7134_enum_fmt_vid_overlay(struct file *file, void *priv,
1849 struct v4l2_fmtdesc *f)
1851 if (saa7134_no_overlay > 0) {
1852 printk(KERN_ERR "V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
1856 if ((f->index >= FORMATS) || formats[f->index].planar)
1859 strlcpy(f->description, formats[f->index].name,
1860 sizeof(f->description));
1862 f->pixelformat = formats[f->index].fourcc;
1867 static int saa7134_g_fbuf(struct file *file, void *f,
1868 struct v4l2_framebuffer *fb)
1870 struct saa7134_dev *dev = video_drvdata(file);
1873 fb->capability = V4L2_FBUF_CAP_LIST_CLIPPING;
1878 static int saa7134_s_fbuf(struct file *file, void *f,
1879 const struct v4l2_framebuffer *fb)
1881 struct saa7134_dev *dev = video_drvdata(file);
1882 struct saa7134_format *fmt;
1884 if (!capable(CAP_SYS_ADMIN) &&
1885 !capable(CAP_SYS_RAWIO))
1889 fmt = format_by_fourcc(fb->fmt.pixelformat);
1896 if (0 == dev->ovbuf.fmt.bytesperline)
1897 dev->ovbuf.fmt.bytesperline =
1898 dev->ovbuf.fmt.width*fmt->depth/8;
1902 static int saa7134_overlay(struct file *file, void *priv, unsigned int on)
1904 struct saa7134_dev *dev = video_drvdata(file);
1905 unsigned long flags;
1908 if (saa7134_no_overlay > 0) {
1909 dprintk("no_overlay\n");
1913 if (!res_get(dev, priv, RESOURCE_OVERLAY))
1915 spin_lock_irqsave(&dev->slock, flags);
1917 spin_unlock_irqrestore(&dev->slock, flags);
1920 if (!res_check(priv, RESOURCE_OVERLAY))
1922 spin_lock_irqsave(&dev->slock, flags);
1924 spin_unlock_irqrestore(&dev->slock, flags);
1925 res_free(dev, priv, RESOURCE_OVERLAY);
1930 int saa7134_reqbufs(struct file *file, void *priv,
1931 struct v4l2_requestbuffers *p)
1933 return videobuf_reqbufs(saa7134_queue(file), p);
1935 EXPORT_SYMBOL_GPL(saa7134_reqbufs);
1937 int saa7134_querybuf(struct file *file, void *priv,
1938 struct v4l2_buffer *b)
1940 return videobuf_querybuf(saa7134_queue(file), b);
1942 EXPORT_SYMBOL_GPL(saa7134_querybuf);
1944 int saa7134_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1946 return videobuf_qbuf(saa7134_queue(file), b);
1948 EXPORT_SYMBOL_GPL(saa7134_qbuf);
1950 int saa7134_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1952 return videobuf_dqbuf(saa7134_queue(file), b,
1953 file->f_flags & O_NONBLOCK);
1955 EXPORT_SYMBOL_GPL(saa7134_dqbuf);
1957 int saa7134_streamon(struct file *file, void *priv,
1958 enum v4l2_buf_type type)
1960 struct saa7134_dev *dev = video_drvdata(file);
1961 int res = saa7134_resource(file);
1963 if (!res_get(dev, priv, res))
1966 /* The SAA7134 has a 1K FIFO; the datasheet suggests that when
1967 * configured conservatively, there's 22 usec of buffering for video.
1968 * We therefore request a DMA latency of 20 usec, giving us 2 usec of
1969 * margin in case the FIFO is configured differently to the datasheet.
1970 * Unfortunately, I lack register-level documentation to check the
1971 * Linux FIFO setup and confirm the perfect value.
1973 if (res != RESOURCE_EMPRESS)
1974 pm_qos_add_request(&dev->qos_request,
1975 PM_QOS_CPU_DMA_LATENCY, 20);
1977 return videobuf_streamon(saa7134_queue(file));
1979 EXPORT_SYMBOL_GPL(saa7134_streamon);
1981 int saa7134_streamoff(struct file *file, void *priv,
1982 enum v4l2_buf_type type)
1984 struct saa7134_dev *dev = video_drvdata(file);
1986 int res = saa7134_resource(file);
1988 if (res != RESOURCE_EMPRESS)
1989 pm_qos_remove_request(&dev->qos_request);
1991 err = videobuf_streamoff(saa7134_queue(file));
1994 res_free(dev, priv, res);
1997 EXPORT_SYMBOL_GPL(saa7134_streamoff);
1999 #ifdef CONFIG_VIDEO_ADV_DEBUG
2000 static int vidioc_g_register (struct file *file, void *priv,
2001 struct v4l2_dbg_register *reg)
2003 struct saa7134_dev *dev = video_drvdata(file);
2005 reg->val = saa_readb(reg->reg & 0xffffff);
2010 static int vidioc_s_register (struct file *file, void *priv,
2011 const struct v4l2_dbg_register *reg)
2013 struct saa7134_dev *dev = video_drvdata(file);
2015 saa_writeb(reg->reg & 0xffffff, reg->val);
2020 static int radio_g_tuner(struct file *file, void *priv,
2021 struct v4l2_tuner *t)
2023 struct saa7134_dev *dev = video_drvdata(file);
2028 strcpy(t->name, "Radio");
2030 saa_call_all(dev, tuner, g_tuner, t);
2031 t->audmode &= V4L2_TUNER_MODE_MONO | V4L2_TUNER_MODE_STEREO;
2032 if (dev->input->amux == TV) {
2033 t->signal = 0xf800 - ((saa_readb(0x581) & 0x1f) << 11);
2034 t->rxsubchans = (saa_readb(0x529) & 0x08) ?
2035 V4L2_TUNER_SUB_STEREO : V4L2_TUNER_SUB_MONO;
2039 static int radio_s_tuner(struct file *file, void *priv,
2040 const struct v4l2_tuner *t)
2042 struct saa7134_dev *dev = video_drvdata(file);
2047 saa_call_all(dev, tuner, s_tuner, t);
2051 static const struct v4l2_file_operations video_fops =
2053 .owner = THIS_MODULE,
2055 .release = video_release,
2059 .ioctl = video_ioctl2,
2062 static const struct v4l2_ioctl_ops video_ioctl_ops = {
2063 .vidioc_querycap = saa7134_querycap,
2064 .vidioc_enum_fmt_vid_cap = saa7134_enum_fmt_vid_cap,
2065 .vidioc_g_fmt_vid_cap = saa7134_g_fmt_vid_cap,
2066 .vidioc_try_fmt_vid_cap = saa7134_try_fmt_vid_cap,
2067 .vidioc_s_fmt_vid_cap = saa7134_s_fmt_vid_cap,
2068 .vidioc_enum_fmt_vid_overlay = saa7134_enum_fmt_vid_overlay,
2069 .vidioc_g_fmt_vid_overlay = saa7134_g_fmt_vid_overlay,
2070 .vidioc_try_fmt_vid_overlay = saa7134_try_fmt_vid_overlay,
2071 .vidioc_s_fmt_vid_overlay = saa7134_s_fmt_vid_overlay,
2072 .vidioc_g_fmt_vbi_cap = saa7134_try_get_set_fmt_vbi_cap,
2073 .vidioc_try_fmt_vbi_cap = saa7134_try_get_set_fmt_vbi_cap,
2074 .vidioc_s_fmt_vbi_cap = saa7134_try_get_set_fmt_vbi_cap,
2075 .vidioc_cropcap = saa7134_cropcap,
2076 .vidioc_reqbufs = saa7134_reqbufs,
2077 .vidioc_querybuf = saa7134_querybuf,
2078 .vidioc_qbuf = saa7134_qbuf,
2079 .vidioc_dqbuf = saa7134_dqbuf,
2080 .vidioc_s_std = saa7134_s_std,
2081 .vidioc_g_std = saa7134_g_std,
2082 .vidioc_enum_input = saa7134_enum_input,
2083 .vidioc_g_input = saa7134_g_input,
2084 .vidioc_s_input = saa7134_s_input,
2085 .vidioc_streamon = saa7134_streamon,
2086 .vidioc_streamoff = saa7134_streamoff,
2087 .vidioc_g_tuner = saa7134_g_tuner,
2088 .vidioc_s_tuner = saa7134_s_tuner,
2089 .vidioc_g_crop = saa7134_g_crop,
2090 .vidioc_s_crop = saa7134_s_crop,
2091 .vidioc_g_fbuf = saa7134_g_fbuf,
2092 .vidioc_s_fbuf = saa7134_s_fbuf,
2093 .vidioc_overlay = saa7134_overlay,
2094 .vidioc_g_frequency = saa7134_g_frequency,
2095 .vidioc_s_frequency = saa7134_s_frequency,
2096 #ifdef CONFIG_VIDEO_ADV_DEBUG
2097 .vidioc_g_register = vidioc_g_register,
2098 .vidioc_s_register = vidioc_s_register,
2102 static const struct v4l2_file_operations radio_fops = {
2103 .owner = THIS_MODULE,
2106 .release = video_release,
2107 .ioctl = video_ioctl2,
2111 static const struct v4l2_ioctl_ops radio_ioctl_ops = {
2112 .vidioc_querycap = saa7134_querycap,
2113 .vidioc_g_tuner = radio_g_tuner,
2114 .vidioc_s_tuner = radio_s_tuner,
2115 .vidioc_g_frequency = saa7134_g_frequency,
2116 .vidioc_s_frequency = saa7134_s_frequency,
2119 /* ----------------------------------------------------------- */
2120 /* exported stuff */
2122 struct video_device saa7134_video_template = {
2123 .name = "saa7134-video",
2124 .fops = &video_fops,
2125 .ioctl_ops = &video_ioctl_ops,
2126 .tvnorms = SAA7134_NORMS,
2129 struct video_device saa7134_radio_template = {
2130 .name = "saa7134-radio",
2131 .fops = &radio_fops,
2132 .ioctl_ops = &radio_ioctl_ops,
2135 static const struct v4l2_ctrl_ops saa7134_ctrl_ops = {
2136 .s_ctrl = saa7134_s_ctrl,
2139 static const struct v4l2_ctrl_config saa7134_ctrl_invert = {
2140 .ops = &saa7134_ctrl_ops,
2141 .id = V4L2_CID_PRIVATE_INVERT,
2143 .type = V4L2_CTRL_TYPE_BOOLEAN,
2149 static const struct v4l2_ctrl_config saa7134_ctrl_y_odd = {
2150 .ops = &saa7134_ctrl_ops,
2151 .id = V4L2_CID_PRIVATE_Y_ODD,
2152 .name = "Y Offset Odd Field",
2153 .type = V4L2_CTRL_TYPE_INTEGER,
2159 static const struct v4l2_ctrl_config saa7134_ctrl_y_even = {
2160 .ops = &saa7134_ctrl_ops,
2161 .id = V4L2_CID_PRIVATE_Y_EVEN,
2162 .name = "Y Offset Even Field",
2163 .type = V4L2_CTRL_TYPE_INTEGER,
2169 static const struct v4l2_ctrl_config saa7134_ctrl_automute = {
2170 .ops = &saa7134_ctrl_ops,
2171 .id = V4L2_CID_PRIVATE_AUTOMUTE,
2173 .type = V4L2_CTRL_TYPE_BOOLEAN,
2180 int saa7134_video_init1(struct saa7134_dev *dev)
2182 struct v4l2_ctrl_handler *hdl = &dev->ctrl_handler;
2184 /* sanitycheck insmod options */
2185 if (gbuffers < 2 || gbuffers > VIDEO_MAX_FRAME)
2187 if (gbufsize > gbufsize_max)
2188 gbufsize = gbufsize_max;
2189 gbufsize = (gbufsize + PAGE_SIZE - 1) & PAGE_MASK;
2191 v4l2_ctrl_handler_init(hdl, 11);
2192 v4l2_ctrl_new_std(hdl, &saa7134_ctrl_ops,
2193 V4L2_CID_BRIGHTNESS, 0, 255, 1, 128);
2194 v4l2_ctrl_new_std(hdl, &saa7134_ctrl_ops,
2195 V4L2_CID_CONTRAST, 0, 127, 1, 68);
2196 v4l2_ctrl_new_std(hdl, &saa7134_ctrl_ops,
2197 V4L2_CID_SATURATION, 0, 127, 1, 64);
2198 v4l2_ctrl_new_std(hdl, &saa7134_ctrl_ops,
2199 V4L2_CID_HUE, -128, 127, 1, 0);
2200 v4l2_ctrl_new_std(hdl, &saa7134_ctrl_ops,
2201 V4L2_CID_HFLIP, 0, 1, 1, 0);
2202 v4l2_ctrl_new_std(hdl, &saa7134_ctrl_ops,
2203 V4L2_CID_AUDIO_MUTE, 0, 1, 1, 0);
2204 v4l2_ctrl_new_std(hdl, &saa7134_ctrl_ops,
2205 V4L2_CID_AUDIO_VOLUME, -15, 15, 1, 0);
2206 v4l2_ctrl_new_custom(hdl, &saa7134_ctrl_invert, NULL);
2207 v4l2_ctrl_new_custom(hdl, &saa7134_ctrl_y_odd, NULL);
2208 v4l2_ctrl_new_custom(hdl, &saa7134_ctrl_y_even, NULL);
2209 v4l2_ctrl_new_custom(hdl, &saa7134_ctrl_automute, NULL);
2212 if (card_has_radio(dev)) {
2213 hdl = &dev->radio_ctrl_handler;
2214 v4l2_ctrl_handler_init(hdl, 2);
2215 v4l2_ctrl_add_handler(hdl, &dev->ctrl_handler,
2216 v4l2_ctrl_radio_filter);
2222 if (dev->tda9887_conf && saa7134_ctrl_automute.def)
2223 dev->tda9887_conf |= TDA9887_AUTOMUTE;
2226 INIT_LIST_HEAD(&dev->video_q.queue);
2227 init_timer(&dev->video_q.timeout);
2228 dev->video_q.timeout.function = saa7134_buffer_timeout;
2229 dev->video_q.timeout.data = (unsigned long)(&dev->video_q);
2230 dev->video_q.dev = dev;
2231 dev->fmt = format_by_fourcc(V4L2_PIX_FMT_BGR24);
2234 dev->win.w.width = dev->width;
2235 dev->win.w.height = dev->height;
2236 dev->win.field = V4L2_FIELD_INTERLACED;
2237 dev->ovbuf.fmt.width = dev->width;
2238 dev->ovbuf.fmt.height = dev->height;
2239 dev->ovbuf.fmt.pixelformat = dev->fmt->fourcc;
2240 dev->ovbuf.fmt.colorspace = V4L2_COLORSPACE_SMPTE170M;
2242 if (saa7134_boards[dev->board].video_out)
2243 saa7134_videoport_init(dev);
2245 videobuf_queue_sg_init(&dev->cap, &video_qops,
2246 &dev->pci->dev, &dev->slock,
2247 V4L2_BUF_TYPE_VIDEO_CAPTURE,
2248 V4L2_FIELD_INTERLACED,
2249 sizeof(struct saa7134_buf),
2251 videobuf_queue_sg_init(&dev->vbi, &saa7134_vbi_qops,
2252 &dev->pci->dev, &dev->slock,
2253 V4L2_BUF_TYPE_VBI_CAPTURE,
2255 sizeof(struct saa7134_buf),
2257 saa7134_pgtable_alloc(dev->pci, &dev->pt_cap);
2258 saa7134_pgtable_alloc(dev->pci, &dev->pt_vbi);
2263 void saa7134_video_fini(struct saa7134_dev *dev)
2266 saa7134_pgtable_free(dev->pci, &dev->pt_cap);
2267 saa7134_pgtable_free(dev->pci, &dev->pt_vbi);
2268 v4l2_ctrl_handler_free(&dev->ctrl_handler);
2269 if (card_has_radio(dev))
2270 v4l2_ctrl_handler_free(&dev->radio_ctrl_handler);
2273 int saa7134_videoport_init(struct saa7134_dev *dev)
2275 /* enable video output */
2276 int vo = saa7134_boards[dev->board].video_out;
2278 unsigned int vid_port_opts = saa7134_boards[dev->board].vid_port_opts;
2280 /* Configure videoport */
2281 saa_writeb(SAA7134_VIDEO_PORT_CTRL0, video_out[vo][0]);
2282 video_reg = video_out[vo][1];
2283 if (vid_port_opts & SET_T_CODE_POLARITY_NON_INVERTED)
2284 video_reg &= ~VP_T_CODE_P_INVERTED;
2285 saa_writeb(SAA7134_VIDEO_PORT_CTRL1, video_reg);
2286 saa_writeb(SAA7134_VIDEO_PORT_CTRL2, video_out[vo][2]);
2287 saa_writeb(SAA7134_VIDEO_PORT_CTRL4, video_out[vo][4]);
2288 video_reg = video_out[vo][5];
2289 if (vid_port_opts & SET_CLOCK_NOT_DELAYED)
2290 video_reg &= ~VP_CLK_CTRL2_DELAYED;
2291 if (vid_port_opts & SET_CLOCK_INVERTED)
2292 video_reg |= VP_CLK_CTRL1_INVERTED;
2293 saa_writeb(SAA7134_VIDEO_PORT_CTRL5, video_reg);
2294 video_reg = video_out[vo][6];
2295 if (vid_port_opts & SET_VSYNC_OFF) {
2296 video_reg &= ~VP_VS_TYPE_MASK;
2297 video_reg |= VP_VS_TYPE_OFF;
2299 saa_writeb(SAA7134_VIDEO_PORT_CTRL6, video_reg);
2300 saa_writeb(SAA7134_VIDEO_PORT_CTRL7, video_out[vo][7]);
2301 saa_writeb(SAA7134_VIDEO_PORT_CTRL8, video_out[vo][8]);
2303 /* Start videoport */
2304 saa_writeb(SAA7134_VIDEO_PORT_CTRL3, video_out[vo][3]);
2309 int saa7134_video_init2(struct saa7134_dev *dev)
2312 set_tvnorm(dev,&tvnorms[0]);
2314 v4l2_ctrl_handler_setup(&dev->ctrl_handler);
2315 saa7134_tvaudio_setmute(dev);
2316 saa7134_tvaudio_setvolume(dev,dev->ctl_volume);
2320 void saa7134_irq_video_signalchange(struct saa7134_dev *dev)
2322 static const char *st[] = {
2323 "(no signal)", "NTSC", "PAL", "SECAM" };
2326 st1 = saa_readb(SAA7134_STATUS_VIDEO1);
2327 st2 = saa_readb(SAA7134_STATUS_VIDEO2);
2328 dprintk("DCSDT: pll: %s, sync: %s, norm: %s\n",
2329 (st1 & 0x40) ? "not locked" : "locked",
2330 (st2 & 0x40) ? "no" : "yes",
2332 dev->nosignal = (st1 & 0x40) || (st2 & 0x40) || !(st2 & 0x1);
2334 if (dev->nosignal) {
2335 /* no video signal -> mute audio */
2336 if (dev->ctl_automute)
2338 saa7134_tvaudio_setmute(dev);
2340 /* wake up tvaudio audio carrier scan thread */
2341 saa7134_tvaudio_do_scan(dev);
2344 if ((st2 & 0x80) && !noninterlaced && !dev->nosignal)
2345 saa_clearb(SAA7134_SYNC_CTRL, 0x20);
2347 saa_setb(SAA7134_SYNC_CTRL, 0x20);
2349 if (dev->mops && dev->mops->signal_change)
2350 dev->mops->signal_change(dev);
2354 void saa7134_irq_video_done(struct saa7134_dev *dev, unsigned long status)
2356 enum v4l2_field field;
2358 spin_lock(&dev->slock);
2359 if (dev->video_q.curr) {
2360 dev->video_fieldcount++;
2361 field = dev->video_q.curr->vb.field;
2362 if (V4L2_FIELD_HAS_BOTH(field)) {
2363 /* make sure we have seen both fields */
2364 if ((status & 0x10) == 0x00) {
2365 dev->video_q.curr->top_seen = 1;
2368 if (!dev->video_q.curr->top_seen)
2370 } else if (field == V4L2_FIELD_TOP) {
2371 if ((status & 0x10) != 0x10)
2373 } else if (field == V4L2_FIELD_BOTTOM) {
2374 if ((status & 0x10) != 0x00)
2377 dev->video_q.curr->vb.field_count = dev->video_fieldcount;
2378 saa7134_buffer_finish(dev,&dev->video_q,VIDEOBUF_DONE);
2380 saa7134_buffer_next(dev,&dev->video_q);
2383 spin_unlock(&dev->slock);
2386 /* ----------------------------------------------------------- */