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.
24 #include "saa7134-reg.h"
26 #include <linux/init.h>
27 #include <linux/list.h>
28 #include <linux/module.h>
29 #include <linux/kernel.h>
30 #include <linux/slab.h>
31 #include <linux/sort.h>
33 #include <media/v4l2-common.h>
34 #include <media/v4l2-event.h>
35 #include <media/saa6588.h>
37 /* ------------------------------------------------------------------ */
39 unsigned int video_debug
;
40 static unsigned int gbuffers
= 8;
41 static unsigned int noninterlaced
; /* 0 */
42 static unsigned int gbufsize
= 720*576*4;
43 static unsigned int gbufsize_max
= 720*576*4;
44 static char secam
[] = "--";
45 module_param(video_debug
, int, 0644);
46 MODULE_PARM_DESC(video_debug
,"enable debug messages [video]");
47 module_param(gbuffers
, int, 0444);
48 MODULE_PARM_DESC(gbuffers
,"number of capture buffers, range 2-32");
49 module_param(noninterlaced
, int, 0644);
50 MODULE_PARM_DESC(noninterlaced
,"capture non interlaced video");
51 module_param_string(secam
, secam
, sizeof(secam
), 0644);
52 MODULE_PARM_DESC(secam
, "force SECAM variant, either DK,L or Lc");
55 #define video_dbg(fmt, arg...) do { \
56 if (video_debug & 0x04) \
57 printk(KERN_DEBUG pr_fmt("video: " fmt), ## arg); \
60 /* ------------------------------------------------------------------ */
61 /* Defines for Video Output Port Register at address 0x191 */
63 /* Bit 0: VIP code T bit polarity */
65 #define VP_T_CODE_P_NON_INVERTED 0x00
66 #define VP_T_CODE_P_INVERTED 0x01
68 /* ------------------------------------------------------------------ */
69 /* Defines for Video Output Port Register at address 0x195 */
71 /* Bit 2: Video output clock delay control */
73 #define VP_CLK_CTRL2_NOT_DELAYED 0x00
74 #define VP_CLK_CTRL2_DELAYED 0x04
76 /* Bit 1: Video output clock invert control */
78 #define VP_CLK_CTRL1_NON_INVERTED 0x00
79 #define VP_CLK_CTRL1_INVERTED 0x02
81 /* ------------------------------------------------------------------ */
82 /* Defines for Video Output Port Register at address 0x196 */
84 /* Bits 2 to 0: VSYNC pin video vertical sync type */
86 #define VP_VS_TYPE_MASK 0x07
88 #define VP_VS_TYPE_OFF 0x00
89 #define VP_VS_TYPE_V123 0x01
90 #define VP_VS_TYPE_V_ITU 0x02
91 #define VP_VS_TYPE_VGATE_L 0x03
92 #define VP_VS_TYPE_RESERVED1 0x04
93 #define VP_VS_TYPE_RESERVED2 0x05
94 #define VP_VS_TYPE_F_ITU 0x06
95 #define VP_VS_TYPE_SC_FID 0x07
97 /* ------------------------------------------------------------------ */
98 /* data structs for video */
100 static int video_out
[][9] = {
101 [CCIR656
] = { 0x00, 0xb1, 0x00, 0xa1, 0x00, 0x04, 0x06, 0x00, 0x00 },
104 static struct saa7134_format formats
[] = {
106 .name
= "8 bpp gray",
107 .fourcc
= V4L2_PIX_FMT_GREY
,
111 .name
= "15 bpp RGB, le",
112 .fourcc
= V4L2_PIX_FMT_RGB555
,
116 .name
= "15 bpp RGB, be",
117 .fourcc
= V4L2_PIX_FMT_RGB555X
,
122 .name
= "16 bpp RGB, le",
123 .fourcc
= V4L2_PIX_FMT_RGB565
,
127 .name
= "16 bpp RGB, be",
128 .fourcc
= V4L2_PIX_FMT_RGB565X
,
133 .name
= "24 bpp RGB, le",
134 .fourcc
= V4L2_PIX_FMT_BGR24
,
138 .name
= "24 bpp RGB, be",
139 .fourcc
= V4L2_PIX_FMT_RGB24
,
144 .name
= "32 bpp RGB, le",
145 .fourcc
= V4L2_PIX_FMT_BGR32
,
149 .name
= "32 bpp RGB, be",
150 .fourcc
= V4L2_PIX_FMT_RGB32
,
156 .name
= "4:2:2 packed, YUYV",
157 .fourcc
= V4L2_PIX_FMT_YUYV
,
163 .name
= "4:2:2 packed, UYVY",
164 .fourcc
= V4L2_PIX_FMT_UYVY
,
169 .name
= "4:2:2 planar, Y-Cb-Cr",
170 .fourcc
= V4L2_PIX_FMT_YUV422P
,
178 .name
= "4:2:0 planar, Y-Cb-Cr",
179 .fourcc
= V4L2_PIX_FMT_YUV420
,
187 .name
= "4:2:0 planar, Y-Cb-Cr",
188 .fourcc
= V4L2_PIX_FMT_YVU420
,
198 #define FORMATS ARRAY_SIZE(formats)
200 #define NORM_625_50 \
203 .video_v_start = 24, \
204 .video_v_stop = 311, \
205 .vbi_v_start_0 = 7, \
206 .vbi_v_stop_0 = 23, \
207 .vbi_v_start_1 = 319, \
210 #define NORM_525_60 \
213 .video_v_start = 23, \
214 .video_v_stop = 262, \
215 .vbi_v_start_0 = 10, \
216 .vbi_v_stop_0 = 21, \
217 .vbi_v_start_1 = 273, \
220 static struct saa7134_tvnorm tvnorms
[] = {
222 .name
= "PAL", /* autodetect */
226 .sync_control
= 0x18,
227 .luma_control
= 0x40,
228 .chroma_ctrl1
= 0x81,
230 .chroma_ctrl2
= 0x06,
235 .id
= V4L2_STD_PAL_BG
,
238 .sync_control
= 0x18,
239 .luma_control
= 0x40,
240 .chroma_ctrl1
= 0x81,
242 .chroma_ctrl2
= 0x06,
247 .id
= V4L2_STD_PAL_I
,
250 .sync_control
= 0x18,
251 .luma_control
= 0x40,
252 .chroma_ctrl1
= 0x81,
254 .chroma_ctrl2
= 0x06,
259 .id
= V4L2_STD_PAL_DK
,
262 .sync_control
= 0x18,
263 .luma_control
= 0x40,
264 .chroma_ctrl1
= 0x81,
266 .chroma_ctrl2
= 0x06,
274 .sync_control
= 0x59,
275 .luma_control
= 0x40,
276 .chroma_ctrl1
= 0x89,
278 .chroma_ctrl2
= 0x0e,
283 .id
= V4L2_STD_SECAM
,
286 .sync_control
= 0x18,
287 .luma_control
= 0x1b,
288 .chroma_ctrl1
= 0xd1,
290 .chroma_ctrl2
= 0x00,
295 .id
= V4L2_STD_SECAM_DK
,
298 .sync_control
= 0x18,
299 .luma_control
= 0x1b,
300 .chroma_ctrl1
= 0xd1,
302 .chroma_ctrl2
= 0x00,
307 .id
= V4L2_STD_SECAM_L
,
310 .sync_control
= 0x18,
311 .luma_control
= 0x1b,
312 .chroma_ctrl1
= 0xd1,
314 .chroma_ctrl2
= 0x00,
319 .id
= V4L2_STD_SECAM_LC
,
322 .sync_control
= 0x18,
323 .luma_control
= 0x1b,
324 .chroma_ctrl1
= 0xd1,
326 .chroma_ctrl2
= 0x00,
331 .id
= V4L2_STD_PAL_M
,
334 .sync_control
= 0x59,
335 .luma_control
= 0x40,
336 .chroma_ctrl1
= 0xb9,
338 .chroma_ctrl2
= 0x0e,
343 .id
= V4L2_STD_PAL_Nc
,
346 .sync_control
= 0x18,
347 .luma_control
= 0x40,
348 .chroma_ctrl1
= 0xa1,
350 .chroma_ctrl2
= 0x06,
355 .id
= V4L2_STD_PAL_60
,
363 .vbi_v_start_1
= 273,
366 .sync_control
= 0x18,
367 .luma_control
= 0x40,
368 .chroma_ctrl1
= 0x81,
370 .chroma_ctrl2
= 0x06,
374 #define TVNORMS ARRAY_SIZE(tvnorms)
376 static struct saa7134_format
* format_by_fourcc(unsigned int fourcc
)
380 for (i
= 0; i
< FORMATS
; i
++)
381 if (formats
[i
].fourcc
== fourcc
)
386 /* ------------------------------------------------------------------ */
388 static void set_tvnorm(struct saa7134_dev
*dev
, struct saa7134_tvnorm
*norm
)
390 video_dbg("set tv norm = %s\n", norm
->name
);
394 dev
->crop_bounds
.left
= norm
->h_start
;
395 dev
->crop_defrect
.left
= norm
->h_start
;
396 dev
->crop_bounds
.width
= norm
->h_stop
- norm
->h_start
+1;
397 dev
->crop_defrect
.width
= norm
->h_stop
- norm
->h_start
+1;
399 dev
->crop_bounds
.top
= (norm
->vbi_v_stop_0
+1)*2;
400 dev
->crop_defrect
.top
= norm
->video_v_start
*2;
401 dev
->crop_bounds
.height
= ((norm
->id
& V4L2_STD_525_60
) ? 524 : 624)
402 - dev
->crop_bounds
.top
;
403 dev
->crop_defrect
.height
= (norm
->video_v_stop
- norm
->video_v_start
+1)*2;
405 dev
->crop_current
= dev
->crop_defrect
;
407 saa7134_set_tvnorm_hw(dev
);
410 static void video_mux(struct saa7134_dev
*dev
, int input
)
412 video_dbg("video input = %d [%s]\n", input
, card_in(dev
, input
).name
);
413 dev
->ctl_input
= input
;
414 set_tvnorm(dev
, dev
->tvnorm
);
415 saa7134_tvaudio_setinput(dev
, &card_in(dev
, input
));
419 static void saa7134_set_decoder(struct saa7134_dev
*dev
)
421 int luma_control
, sync_control
, chroma_ctrl1
, mux
;
423 struct saa7134_tvnorm
*norm
= dev
->tvnorm
;
424 mux
= card_in(dev
, dev
->ctl_input
).vmux
;
426 luma_control
= norm
->luma_control
;
427 sync_control
= norm
->sync_control
;
428 chroma_ctrl1
= norm
->chroma_ctrl1
;
431 luma_control
|= 0x80; /* svideo */
432 if (noninterlaced
|| dev
->nosignal
)
433 sync_control
|= 0x20;
435 /* switch on auto standard detection */
436 sync_control
|= SAA7134_SYNC_CTRL_AUFD
;
437 chroma_ctrl1
|= SAA7134_CHROMA_CTRL1_AUTO0
;
438 chroma_ctrl1
&= ~SAA7134_CHROMA_CTRL1_FCTC
;
439 luma_control
&= ~SAA7134_LUMA_CTRL_LDEL
;
441 /* setup video decoder */
442 saa_writeb(SAA7134_INCR_DELAY
, 0x08);
443 saa_writeb(SAA7134_ANALOG_IN_CTRL1
, 0xc0 | mux
);
444 saa_writeb(SAA7134_ANALOG_IN_CTRL2
, 0x00);
446 saa_writeb(SAA7134_ANALOG_IN_CTRL3
, 0x90);
447 saa_writeb(SAA7134_ANALOG_IN_CTRL4
, 0x90);
448 saa_writeb(SAA7134_HSYNC_START
, 0xeb);
449 saa_writeb(SAA7134_HSYNC_STOP
, 0xe0);
450 saa_writeb(SAA7134_SOURCE_TIMING1
, norm
->src_timing
);
452 saa_writeb(SAA7134_SYNC_CTRL
, sync_control
);
453 saa_writeb(SAA7134_LUMA_CTRL
, luma_control
);
454 saa_writeb(SAA7134_DEC_LUMA_BRIGHT
, dev
->ctl_bright
);
456 saa_writeb(SAA7134_DEC_LUMA_CONTRAST
,
457 dev
->ctl_invert
? -dev
->ctl_contrast
: dev
->ctl_contrast
);
459 saa_writeb(SAA7134_DEC_CHROMA_SATURATION
,
460 dev
->ctl_invert
? -dev
->ctl_saturation
: dev
->ctl_saturation
);
462 saa_writeb(SAA7134_DEC_CHROMA_HUE
, dev
->ctl_hue
);
463 saa_writeb(SAA7134_CHROMA_CTRL1
, chroma_ctrl1
);
464 saa_writeb(SAA7134_CHROMA_GAIN
, norm
->chroma_gain
);
466 saa_writeb(SAA7134_CHROMA_CTRL2
, norm
->chroma_ctrl2
);
467 saa_writeb(SAA7134_MODE_DELAY_CTRL
, 0x00);
469 saa_writeb(SAA7134_ANALOG_ADC
, 0x01);
470 saa_writeb(SAA7134_VGATE_START
, 0x11);
471 saa_writeb(SAA7134_VGATE_STOP
, 0xfe);
472 saa_writeb(SAA7134_MISC_VGATE_MSB
, norm
->vgate_misc
);
473 saa_writeb(SAA7134_RAW_DATA_GAIN
, 0x40);
474 saa_writeb(SAA7134_RAW_DATA_OFFSET
, 0x80);
477 void saa7134_set_tvnorm_hw(struct saa7134_dev
*dev
)
479 saa7134_set_decoder(dev
);
481 if (card_in(dev
, dev
->ctl_input
).tv
)
482 saa_call_all(dev
, video
, s_std
, dev
->tvnorm
->id
);
483 /* Set the correct norm for the saa6752hs. This function
484 does nothing if there is no saa6752hs. */
485 saa_call_empress(dev
, video
, s_std
, dev
->tvnorm
->id
);
488 static void set_h_prescale(struct saa7134_dev
*dev
, int task
, int prescale
)
490 static const struct {
497 /* XPSC XACL XC2_1 XDCG VPFY */
509 static const int count
= ARRAY_SIZE(vals
);
512 for (i
= 0; i
< count
; i
++)
513 if (vals
[i
].xpsc
== prescale
)
518 saa_writeb(SAA7134_H_PRESCALE(task
), vals
[i
].xpsc
);
519 saa_writeb(SAA7134_ACC_LENGTH(task
), vals
[i
].xacl
);
520 saa_writeb(SAA7134_LEVEL_CTRL(task
),
521 (vals
[i
].xc2_1
<< 3) | (vals
[i
].xdcg
));
522 saa_andorb(SAA7134_FIR_PREFILTER_CTRL(task
), 0x0f,
523 (vals
[i
].vpfy
<< 2) | vals
[i
].vpfy
);
526 static void set_v_scale(struct saa7134_dev
*dev
, int task
, int yscale
)
530 saa_writeb(SAA7134_V_SCALE_RATIO1(task
), yscale
& 0xff);
531 saa_writeb(SAA7134_V_SCALE_RATIO2(task
), yscale
>> 8);
533 mirror
= (dev
->ctl_mirror
) ? 0x02 : 0x00;
536 video_dbg("yscale LPI yscale=%d\n", yscale
);
537 saa_writeb(SAA7134_V_FILTER(task
), 0x00 | mirror
);
538 saa_writeb(SAA7134_LUMA_CONTRAST(task
), 0x40);
539 saa_writeb(SAA7134_CHROMA_SATURATION(task
), 0x40);
542 val
= 0x40 * 1024 / yscale
;
543 video_dbg("yscale ACM yscale=%d val=0x%x\n", yscale
, val
);
544 saa_writeb(SAA7134_V_FILTER(task
), 0x01 | mirror
);
545 saa_writeb(SAA7134_LUMA_CONTRAST(task
), val
);
546 saa_writeb(SAA7134_CHROMA_SATURATION(task
), val
);
548 saa_writeb(SAA7134_LUMA_BRIGHT(task
), 0x80);
551 static void set_size(struct saa7134_dev
*dev
, int task
,
552 int width
, int height
, int interlace
)
554 int prescale
,xscale
,yscale
,y_even
,y_odd
;
555 int h_start
, h_stop
, v_start
, v_stop
;
556 int div
= interlace
? 2 : 1;
558 /* setup video scaler */
559 h_start
= dev
->crop_current
.left
;
560 v_start
= dev
->crop_current
.top
/2;
561 h_stop
= (dev
->crop_current
.left
+ dev
->crop_current
.width
-1);
562 v_stop
= (dev
->crop_current
.top
+ dev
->crop_current
.height
-1)/2;
564 saa_writeb(SAA7134_VIDEO_H_START1(task
), h_start
& 0xff);
565 saa_writeb(SAA7134_VIDEO_H_START2(task
), h_start
>> 8);
566 saa_writeb(SAA7134_VIDEO_H_STOP1(task
), h_stop
& 0xff);
567 saa_writeb(SAA7134_VIDEO_H_STOP2(task
), h_stop
>> 8);
568 saa_writeb(SAA7134_VIDEO_V_START1(task
), v_start
& 0xff);
569 saa_writeb(SAA7134_VIDEO_V_START2(task
), v_start
>> 8);
570 saa_writeb(SAA7134_VIDEO_V_STOP1(task
), v_stop
& 0xff);
571 saa_writeb(SAA7134_VIDEO_V_STOP2(task
), v_stop
>> 8);
573 prescale
= dev
->crop_current
.width
/ width
;
576 xscale
= 1024 * dev
->crop_current
.width
/ prescale
/ width
;
577 yscale
= 512 * div
* dev
->crop_current
.height
/ height
;
578 video_dbg("prescale=%d xscale=%d yscale=%d\n",
579 prescale
, xscale
, yscale
);
580 set_h_prescale(dev
,task
,prescale
);
581 saa_writeb(SAA7134_H_SCALE_INC1(task
), xscale
& 0xff);
582 saa_writeb(SAA7134_H_SCALE_INC2(task
), xscale
>> 8);
583 set_v_scale(dev
,task
,yscale
);
585 saa_writeb(SAA7134_VIDEO_PIXELS1(task
), width
& 0xff);
586 saa_writeb(SAA7134_VIDEO_PIXELS2(task
), width
>> 8);
587 saa_writeb(SAA7134_VIDEO_LINES1(task
), height
/div
& 0xff);
588 saa_writeb(SAA7134_VIDEO_LINES2(task
), height
/div
>> 8);
590 /* deinterlace y offsets */
591 y_odd
= dev
->ctl_y_odd
;
592 y_even
= dev
->ctl_y_even
;
593 saa_writeb(SAA7134_V_PHASE_OFFSET0(task
), y_odd
);
594 saa_writeb(SAA7134_V_PHASE_OFFSET1(task
), y_even
);
595 saa_writeb(SAA7134_V_PHASE_OFFSET2(task
), y_odd
);
596 saa_writeb(SAA7134_V_PHASE_OFFSET3(task
), y_even
);
599 /* ------------------------------------------------------------------ */
607 static void set_cliplist(struct saa7134_dev
*dev
, int reg
,
608 struct cliplist
*cl
, int entries
, char *name
)
613 for (i
= 0; i
< entries
; i
++) {
614 winbits
|= cl
[i
].enable
;
615 winbits
&= ~cl
[i
].disable
;
616 if (i
< 15 && cl
[i
].position
== cl
[i
+1].position
)
618 saa_writeb(reg
+ 0, winbits
);
619 saa_writeb(reg
+ 2, cl
[i
].position
& 0xff);
620 saa_writeb(reg
+ 3, cl
[i
].position
>> 8);
621 video_dbg("clip: %s winbits=%02x pos=%d\n",
622 name
,winbits
,cl
[i
].position
);
625 for (; reg
< 0x400; reg
+= 8) {
626 saa_writeb(reg
+ 0, 0);
627 saa_writeb(reg
+ 1, 0);
628 saa_writeb(reg
+ 2, 0);
629 saa_writeb(reg
+ 3, 0);
633 static int clip_range(int val
)
640 /* Sort into smallest position first order */
641 static int cliplist_cmp(const void *a
, const void *b
)
643 const struct cliplist
*cla
= a
;
644 const struct cliplist
*clb
= b
;
645 if (cla
->position
< clb
->position
)
647 if (cla
->position
> clb
->position
)
652 static int setup_clipping(struct saa7134_dev
*dev
, struct v4l2_clip
*clips
,
653 int nclips
, int interlace
)
655 struct cliplist col
[16], row
[16];
656 int cols
= 0, rows
= 0, i
;
657 int div
= interlace
? 2 : 1;
659 memset(col
, 0, sizeof(col
));
660 memset(row
, 0, sizeof(row
));
661 for (i
= 0; i
< nclips
&& i
< 8; i
++) {
662 col
[cols
].position
= clip_range(clips
[i
].c
.left
);
663 col
[cols
].enable
= (1 << i
);
665 col
[cols
].position
= clip_range(clips
[i
].c
.left
+clips
[i
].c
.width
);
666 col
[cols
].disable
= (1 << i
);
668 row
[rows
].position
= clip_range(clips
[i
].c
.top
/ div
);
669 row
[rows
].enable
= (1 << i
);
671 row
[rows
].position
= clip_range((clips
[i
].c
.top
+ clips
[i
].c
.height
)
673 row
[rows
].disable
= (1 << i
);
676 sort(col
, cols
, sizeof col
[0], cliplist_cmp
, NULL
);
677 sort(row
, rows
, sizeof row
[0], cliplist_cmp
, NULL
);
678 set_cliplist(dev
,0x380,col
,cols
,"cols");
679 set_cliplist(dev
,0x384,row
,rows
,"rows");
683 static int verify_preview(struct saa7134_dev
*dev
, struct v4l2_window
*win
, bool try)
685 enum v4l2_field field
;
688 if (!try && (dev
->ovbuf
.base
== NULL
|| dev
->ovfmt
== NULL
))
690 if (win
->w
.width
< 48)
692 if (win
->w
.height
< 32)
694 if (win
->clipcount
> 8)
698 win
->global_alpha
= 0;
700 maxw
= dev
->crop_current
.width
;
701 maxh
= dev
->crop_current
.height
;
703 if (V4L2_FIELD_ANY
== field
) {
704 field
= (win
->w
.height
> maxh
/2)
705 ? V4L2_FIELD_INTERLACED
710 case V4L2_FIELD_BOTTOM
:
714 field
= V4L2_FIELD_INTERLACED
;
719 if (win
->w
.width
> maxw
)
721 if (win
->w
.height
> maxh
)
722 win
->w
.height
= maxh
;
726 static int start_preview(struct saa7134_dev
*dev
)
728 unsigned long base
,control
,bpl
;
731 err
= verify_preview(dev
, &dev
->win
, false);
735 dev
->ovfield
= dev
->win
.field
;
736 video_dbg("start_preview %dx%d+%d+%d %s field=%s\n",
737 dev
->win
.w
.width
, dev
->win
.w
.height
,
738 dev
->win
.w
.left
, dev
->win
.w
.top
,
739 dev
->ovfmt
->name
, v4l2_field_names
[dev
->ovfield
]);
741 /* setup window + clipping */
742 set_size(dev
, TASK_B
, dev
->win
.w
.width
, dev
->win
.w
.height
,
743 V4L2_FIELD_HAS_BOTH(dev
->ovfield
));
744 setup_clipping(dev
, dev
->clips
, dev
->nclips
,
745 V4L2_FIELD_HAS_BOTH(dev
->ovfield
));
747 saa_andorb(SAA7134_DATA_PATH(TASK_B
), 0x3f, 0x03);
749 saa_andorb(SAA7134_DATA_PATH(TASK_B
), 0x3f, 0x01);
750 saa_writeb(SAA7134_OFMT_VIDEO_B
, dev
->ovfmt
->pm
| 0x20);
752 /* dma: setup channel 1 (= Video Task B) */
753 base
= (unsigned long)dev
->ovbuf
.base
;
754 base
+= dev
->ovbuf
.fmt
.bytesperline
* dev
->win
.w
.top
;
755 base
+= dev
->ovfmt
->depth
/8 * dev
->win
.w
.left
;
756 bpl
= dev
->ovbuf
.fmt
.bytesperline
;
757 control
= SAA7134_RS_CONTROL_BURST_16
;
758 if (dev
->ovfmt
->bswap
)
759 control
|= SAA7134_RS_CONTROL_BSWAP
;
760 if (dev
->ovfmt
->wswap
)
761 control
|= SAA7134_RS_CONTROL_WSWAP
;
762 if (V4L2_FIELD_HAS_BOTH(dev
->ovfield
)) {
763 saa_writel(SAA7134_RS_BA1(1),base
);
764 saa_writel(SAA7134_RS_BA2(1),base
+bpl
);
765 saa_writel(SAA7134_RS_PITCH(1),bpl
*2);
766 saa_writel(SAA7134_RS_CONTROL(1),control
);
768 saa_writel(SAA7134_RS_BA1(1),base
);
769 saa_writel(SAA7134_RS_BA2(1),base
);
770 saa_writel(SAA7134_RS_PITCH(1),bpl
);
771 saa_writel(SAA7134_RS_CONTROL(1),control
);
776 saa7134_set_dmabits(dev
);
781 static int stop_preview(struct saa7134_dev
*dev
)
784 saa7134_set_dmabits(dev
);
788 /* ------------------------------------------------------------------ */
790 static int buffer_activate(struct saa7134_dev
*dev
,
791 struct saa7134_buf
*buf
,
792 struct saa7134_buf
*next
)
794 struct saa7134_dmaqueue
*dmaq
= buf
->vb2
.vb2_buf
.vb2_queue
->drv_priv
;
795 unsigned long base
,control
,bpl
;
796 unsigned long bpl_uv
,lines_uv
,base2
,base3
,tmp
; /* planar */
798 video_dbg("buffer_activate buf=%p\n", buf
);
801 set_size(dev
, TASK_A
, dev
->width
, dev
->height
,
802 V4L2_FIELD_HAS_BOTH(dev
->field
));
804 saa_andorb(SAA7134_DATA_PATH(TASK_A
), 0x3f, 0x03);
806 saa_andorb(SAA7134_DATA_PATH(TASK_A
), 0x3f, 0x01);
807 saa_writeb(SAA7134_OFMT_VIDEO_A
, dev
->fmt
->pm
);
809 /* DMA: setup channel 0 (= Video Task A0) */
810 base
= saa7134_buffer_base(buf
);
811 if (dev
->fmt
->planar
)
814 bpl
= (dev
->width
* dev
->fmt
->depth
) / 8;
815 control
= SAA7134_RS_CONTROL_BURST_16
|
816 SAA7134_RS_CONTROL_ME
|
817 (dmaq
->pt
.dma
>> 12);
819 control
|= SAA7134_RS_CONTROL_BSWAP
;
821 control
|= SAA7134_RS_CONTROL_WSWAP
;
822 if (V4L2_FIELD_HAS_BOTH(dev
->field
)) {
824 saa_writel(SAA7134_RS_BA1(0),base
);
825 saa_writel(SAA7134_RS_BA2(0),base
+bpl
);
826 saa_writel(SAA7134_RS_PITCH(0),bpl
*2);
829 saa_writel(SAA7134_RS_BA1(0),base
);
830 saa_writel(SAA7134_RS_BA2(0),base
);
831 saa_writel(SAA7134_RS_PITCH(0),bpl
);
833 saa_writel(SAA7134_RS_CONTROL(0),control
);
835 if (dev
->fmt
->planar
) {
836 /* DMA: setup channel 4+5 (= planar task A) */
837 bpl_uv
= bpl
>> dev
->fmt
->hshift
;
838 lines_uv
= dev
->height
>> dev
->fmt
->vshift
;
839 base2
= base
+ bpl
* dev
->height
;
840 base3
= base2
+ bpl_uv
* lines_uv
;
841 if (dev
->fmt
->uvswap
)
842 tmp
= base2
, base2
= base3
, base3
= tmp
;
843 video_dbg("uv: bpl=%ld lines=%ld base2/3=%ld/%ld\n",
844 bpl_uv
,lines_uv
,base2
,base3
);
845 if (V4L2_FIELD_HAS_BOTH(dev
->field
)) {
847 saa_writel(SAA7134_RS_BA1(4),base2
);
848 saa_writel(SAA7134_RS_BA2(4),base2
+bpl_uv
);
849 saa_writel(SAA7134_RS_PITCH(4),bpl_uv
*2);
850 saa_writel(SAA7134_RS_BA1(5),base3
);
851 saa_writel(SAA7134_RS_BA2(5),base3
+bpl_uv
);
852 saa_writel(SAA7134_RS_PITCH(5),bpl_uv
*2);
855 saa_writel(SAA7134_RS_BA1(4),base2
);
856 saa_writel(SAA7134_RS_BA2(4),base2
);
857 saa_writel(SAA7134_RS_PITCH(4),bpl_uv
);
858 saa_writel(SAA7134_RS_BA1(5),base3
);
859 saa_writel(SAA7134_RS_BA2(5),base3
);
860 saa_writel(SAA7134_RS_PITCH(5),bpl_uv
);
862 saa_writel(SAA7134_RS_CONTROL(4),control
);
863 saa_writel(SAA7134_RS_CONTROL(5),control
);
867 saa7134_set_dmabits(dev
);
868 mod_timer(&dmaq
->timeout
, jiffies
+ BUFFER_TIMEOUT
);
872 static int buffer_init(struct vb2_buffer
*vb2
)
874 struct saa7134_dmaqueue
*dmaq
= vb2
->vb2_queue
->drv_priv
;
875 struct vb2_v4l2_buffer
*vbuf
= to_vb2_v4l2_buffer(vb2
);
876 struct saa7134_buf
*buf
= container_of(vbuf
, struct saa7134_buf
, vb2
);
879 buf
->activate
= buffer_activate
;
883 static int buffer_prepare(struct vb2_buffer
*vb2
)
885 struct saa7134_dmaqueue
*dmaq
= vb2
->vb2_queue
->drv_priv
;
886 struct saa7134_dev
*dev
= dmaq
->dev
;
887 struct vb2_v4l2_buffer
*vbuf
= to_vb2_v4l2_buffer(vb2
);
888 struct saa7134_buf
*buf
= container_of(vbuf
, struct saa7134_buf
, vb2
);
889 struct sg_table
*dma
= vb2_dma_sg_plane_desc(vb2
, 0);
892 if (dma
->sgl
->offset
) {
893 pr_err("The buffer is not page-aligned\n");
896 size
= (dev
->width
* dev
->height
* dev
->fmt
->depth
) >> 3;
897 if (vb2_plane_size(vb2
, 0) < size
)
900 vb2_set_plane_payload(vb2
, 0, size
);
901 vbuf
->field
= dev
->field
;
903 return saa7134_pgtable_build(dev
->pci
, &dmaq
->pt
, dma
->sgl
, dma
->nents
,
904 saa7134_buffer_startpage(buf
));
907 static int queue_setup(struct vb2_queue
*q
, const void *parg
,
908 unsigned int *nbuffers
, unsigned int *nplanes
,
909 unsigned int sizes
[], void *alloc_ctxs
[])
911 struct saa7134_dmaqueue
*dmaq
= q
->drv_priv
;
912 struct saa7134_dev
*dev
= dmaq
->dev
;
913 int size
= dev
->fmt
->depth
* dev
->width
* dev
->height
>> 3;
915 if (dev
->width
< 48 ||
917 dev
->width
/4 > dev
->crop_current
.width
||
918 dev
->height
/4 > dev
->crop_current
.height
||
919 dev
->width
> dev
->crop_bounds
.width
||
920 dev
->height
> dev
->crop_bounds
.height
)
923 *nbuffers
= saa7134_buffer_count(size
, *nbuffers
);
926 alloc_ctxs
[0] = dev
->alloc_ctx
;
931 * move buffer to hardware queue
933 void saa7134_vb2_buffer_queue(struct vb2_buffer
*vb
)
935 struct saa7134_dmaqueue
*dmaq
= vb
->vb2_queue
->drv_priv
;
936 struct saa7134_dev
*dev
= dmaq
->dev
;
937 struct vb2_v4l2_buffer
*vbuf
= to_vb2_v4l2_buffer(vb
);
938 struct saa7134_buf
*buf
= container_of(vbuf
, struct saa7134_buf
, vb2
);
940 saa7134_buffer_queue(dev
, dmaq
, buf
);
942 EXPORT_SYMBOL_GPL(saa7134_vb2_buffer_queue
);
944 int saa7134_vb2_start_streaming(struct vb2_queue
*vq
, unsigned int count
)
946 struct saa7134_dmaqueue
*dmaq
= vq
->drv_priv
;
947 struct saa7134_dev
*dev
= dmaq
->dev
;
950 * Planar video capture and TS share the same DMA channel,
951 * so only one can be active at a time.
953 if (card_is_empress(dev
) && vb2_is_busy(&dev
->empress_vbq
) &&
954 dmaq
== &dev
->video_q
&& dev
->fmt
->planar
) {
955 struct saa7134_buf
*buf
, *tmp
;
957 list_for_each_entry_safe(buf
, tmp
, &dmaq
->queue
, entry
) {
958 list_del(&buf
->entry
);
959 vb2_buffer_done(&buf
->vb2
.vb2_buf
,
960 VB2_BUF_STATE_QUEUED
);
963 vb2_buffer_done(&dmaq
->curr
->vb2
.vb2_buf
,
964 VB2_BUF_STATE_QUEUED
);
970 /* The SAA7134 has a 1K FIFO; the datasheet suggests that when
971 * configured conservatively, there's 22 usec of buffering for video.
972 * We therefore request a DMA latency of 20 usec, giving us 2 usec of
973 * margin in case the FIFO is configured differently to the datasheet.
974 * Unfortunately, I lack register-level documentation to check the
975 * Linux FIFO setup and confirm the perfect value.
977 if ((dmaq
== &dev
->video_q
&& !vb2_is_streaming(&dev
->vbi_vbq
)) ||
978 (dmaq
== &dev
->vbi_q
&& !vb2_is_streaming(&dev
->video_vbq
)))
979 pm_qos_add_request(&dev
->qos_request
,
980 PM_QOS_CPU_DMA_LATENCY
, 20);
986 void saa7134_vb2_stop_streaming(struct vb2_queue
*vq
)
988 struct saa7134_dmaqueue
*dmaq
= vq
->drv_priv
;
989 struct saa7134_dev
*dev
= dmaq
->dev
;
991 saa7134_stop_streaming(dev
, dmaq
);
993 if ((dmaq
== &dev
->video_q
&& !vb2_is_streaming(&dev
->vbi_vbq
)) ||
994 (dmaq
== &dev
->vbi_q
&& !vb2_is_streaming(&dev
->video_vbq
)))
995 pm_qos_remove_request(&dev
->qos_request
);
998 static struct vb2_ops vb2_qops
= {
999 .queue_setup
= queue_setup
,
1000 .buf_init
= buffer_init
,
1001 .buf_prepare
= buffer_prepare
,
1002 .buf_queue
= saa7134_vb2_buffer_queue
,
1003 .wait_prepare
= vb2_ops_wait_prepare
,
1004 .wait_finish
= vb2_ops_wait_finish
,
1005 .start_streaming
= saa7134_vb2_start_streaming
,
1006 .stop_streaming
= saa7134_vb2_stop_streaming
,
1009 /* ------------------------------------------------------------------ */
1011 static int saa7134_s_ctrl(struct v4l2_ctrl
*ctrl
)
1013 struct saa7134_dev
*dev
= container_of(ctrl
->handler
, struct saa7134_dev
, ctrl_handler
);
1014 unsigned long flags
;
1015 int restart_overlay
= 0;
1018 case V4L2_CID_BRIGHTNESS
:
1019 dev
->ctl_bright
= ctrl
->val
;
1020 saa_writeb(SAA7134_DEC_LUMA_BRIGHT
, ctrl
->val
);
1023 dev
->ctl_hue
= ctrl
->val
;
1024 saa_writeb(SAA7134_DEC_CHROMA_HUE
, ctrl
->val
);
1026 case V4L2_CID_CONTRAST
:
1027 dev
->ctl_contrast
= ctrl
->val
;
1028 saa_writeb(SAA7134_DEC_LUMA_CONTRAST
,
1029 dev
->ctl_invert
? -dev
->ctl_contrast
: dev
->ctl_contrast
);
1031 case V4L2_CID_SATURATION
:
1032 dev
->ctl_saturation
= ctrl
->val
;
1033 saa_writeb(SAA7134_DEC_CHROMA_SATURATION
,
1034 dev
->ctl_invert
? -dev
->ctl_saturation
: dev
->ctl_saturation
);
1036 case V4L2_CID_AUDIO_MUTE
:
1037 dev
->ctl_mute
= ctrl
->val
;
1038 saa7134_tvaudio_setmute(dev
);
1040 case V4L2_CID_AUDIO_VOLUME
:
1041 dev
->ctl_volume
= ctrl
->val
;
1042 saa7134_tvaudio_setvolume(dev
,dev
->ctl_volume
);
1044 case V4L2_CID_PRIVATE_INVERT
:
1045 dev
->ctl_invert
= ctrl
->val
;
1046 saa_writeb(SAA7134_DEC_LUMA_CONTRAST
,
1047 dev
->ctl_invert
? -dev
->ctl_contrast
: dev
->ctl_contrast
);
1048 saa_writeb(SAA7134_DEC_CHROMA_SATURATION
,
1049 dev
->ctl_invert
? -dev
->ctl_saturation
: dev
->ctl_saturation
);
1051 case V4L2_CID_HFLIP
:
1052 dev
->ctl_mirror
= ctrl
->val
;
1053 restart_overlay
= 1;
1055 case V4L2_CID_PRIVATE_Y_EVEN
:
1056 dev
->ctl_y_even
= ctrl
->val
;
1057 restart_overlay
= 1;
1059 case V4L2_CID_PRIVATE_Y_ODD
:
1060 dev
->ctl_y_odd
= ctrl
->val
;
1061 restart_overlay
= 1;
1063 case V4L2_CID_PRIVATE_AUTOMUTE
:
1065 struct v4l2_priv_tun_config tda9887_cfg
;
1067 tda9887_cfg
.tuner
= TUNER_TDA9887
;
1068 tda9887_cfg
.priv
= &dev
->tda9887_conf
;
1070 dev
->ctl_automute
= ctrl
->val
;
1071 if (dev
->tda9887_conf
) {
1072 if (dev
->ctl_automute
)
1073 dev
->tda9887_conf
|= TDA9887_AUTOMUTE
;
1075 dev
->tda9887_conf
&= ~TDA9887_AUTOMUTE
;
1077 saa_call_all(dev
, tuner
, s_config
, &tda9887_cfg
);
1084 if (restart_overlay
&& dev
->overlay_owner
) {
1085 spin_lock_irqsave(&dev
->slock
, flags
);
1088 spin_unlock_irqrestore(&dev
->slock
, flags
);
1093 /* ------------------------------------------------------------------ */
1095 static int video_open(struct file
*file
)
1097 struct video_device
*vdev
= video_devdata(file
);
1098 struct saa7134_dev
*dev
= video_drvdata(file
);
1099 int ret
= v4l2_fh_open(file
);
1104 mutex_lock(&dev
->lock
);
1105 if (vdev
->vfl_type
== VFL_TYPE_RADIO
) {
1106 /* switch to radio mode */
1107 saa7134_tvaudio_setinput(dev
, &card(dev
).radio
);
1108 saa_call_all(dev
, tuner
, s_radio
);
1110 /* switch to video/vbi mode */
1111 video_mux(dev
, dev
->ctl_input
);
1113 mutex_unlock(&dev
->lock
);
1118 static int video_release(struct file
*file
)
1120 struct video_device
*vdev
= video_devdata(file
);
1121 struct saa7134_dev
*dev
= video_drvdata(file
);
1122 struct v4l2_fh
*fh
= file
->private_data
;
1123 struct saa6588_command cmd
;
1124 unsigned long flags
;
1126 mutex_lock(&dev
->lock
);
1127 saa7134_tvaudio_close(dev
);
1129 /* turn off overlay */
1130 if (fh
== dev
->overlay_owner
) {
1131 spin_lock_irqsave(&dev
->slock
,flags
);
1133 spin_unlock_irqrestore(&dev
->slock
,flags
);
1134 dev
->overlay_owner
= NULL
;
1137 if (vdev
->vfl_type
== VFL_TYPE_RADIO
)
1138 v4l2_fh_release(file
);
1140 _vb2_fop_release(file
, NULL
);
1142 /* ts-capture will not work in planar mode, so turn it off Hac: 04.05*/
1143 saa_andorb(SAA7134_OFMT_VIDEO_A
, 0x1f, 0);
1144 saa_andorb(SAA7134_OFMT_VIDEO_B
, 0x1f, 0);
1145 saa_andorb(SAA7134_OFMT_DATA_A
, 0x1f, 0);
1146 saa_andorb(SAA7134_OFMT_DATA_B
, 0x1f, 0);
1148 saa_call_all(dev
, core
, s_power
, 0);
1149 if (vdev
->vfl_type
== VFL_TYPE_RADIO
)
1150 saa_call_all(dev
, core
, ioctl
, SAA6588_CMD_CLOSE
, &cmd
);
1151 mutex_unlock(&dev
->lock
);
1156 static ssize_t
radio_read(struct file
*file
, char __user
*data
,
1157 size_t count
, loff_t
*ppos
)
1159 struct saa7134_dev
*dev
= video_drvdata(file
);
1160 struct saa6588_command cmd
;
1162 cmd
.block_count
= count
/3;
1163 cmd
.nonblocking
= file
->f_flags
& O_NONBLOCK
;
1165 cmd
.instance
= file
;
1166 cmd
.result
= -ENODEV
;
1168 mutex_lock(&dev
->lock
);
1169 saa_call_all(dev
, core
, ioctl
, SAA6588_CMD_READ
, &cmd
);
1170 mutex_unlock(&dev
->lock
);
1175 static unsigned int radio_poll(struct file
*file
, poll_table
*wait
)
1177 struct saa7134_dev
*dev
= video_drvdata(file
);
1178 struct saa6588_command cmd
;
1179 unsigned int rc
= v4l2_ctrl_poll(file
, wait
);
1181 cmd
.instance
= file
;
1182 cmd
.event_list
= wait
;
1184 mutex_lock(&dev
->lock
);
1185 saa_call_all(dev
, core
, ioctl
, SAA6588_CMD_POLL
, &cmd
);
1186 mutex_unlock(&dev
->lock
);
1188 return rc
| cmd
.result
;
1191 /* ------------------------------------------------------------------ */
1193 static int saa7134_try_get_set_fmt_vbi_cap(struct file
*file
, void *priv
,
1194 struct v4l2_format
*f
)
1196 struct saa7134_dev
*dev
= video_drvdata(file
);
1197 struct saa7134_tvnorm
*norm
= dev
->tvnorm
;
1199 memset(&f
->fmt
.vbi
.reserved
, 0, sizeof(f
->fmt
.vbi
.reserved
));
1200 f
->fmt
.vbi
.sampling_rate
= 6750000 * 4;
1201 f
->fmt
.vbi
.samples_per_line
= 2048 /* VBI_LINE_LENGTH */;
1202 f
->fmt
.vbi
.sample_format
= V4L2_PIX_FMT_GREY
;
1203 f
->fmt
.vbi
.offset
= 64 * 4;
1204 f
->fmt
.vbi
.start
[0] = norm
->vbi_v_start_0
;
1205 f
->fmt
.vbi
.count
[0] = norm
->vbi_v_stop_0
- norm
->vbi_v_start_0
+1;
1206 f
->fmt
.vbi
.start
[1] = norm
->vbi_v_start_1
;
1207 f
->fmt
.vbi
.count
[1] = f
->fmt
.vbi
.count
[0];
1208 f
->fmt
.vbi
.flags
= 0; /* VBI_UNSYNC VBI_INTERLACED */
1213 static int saa7134_g_fmt_vid_cap(struct file
*file
, void *priv
,
1214 struct v4l2_format
*f
)
1216 struct saa7134_dev
*dev
= video_drvdata(file
);
1218 f
->fmt
.pix
.width
= dev
->width
;
1219 f
->fmt
.pix
.height
= dev
->height
;
1220 f
->fmt
.pix
.field
= dev
->field
;
1221 f
->fmt
.pix
.pixelformat
= dev
->fmt
->fourcc
;
1222 f
->fmt
.pix
.bytesperline
=
1223 (f
->fmt
.pix
.width
* dev
->fmt
->depth
) >> 3;
1224 f
->fmt
.pix
.sizeimage
=
1225 f
->fmt
.pix
.height
* f
->fmt
.pix
.bytesperline
;
1226 f
->fmt
.pix
.colorspace
= V4L2_COLORSPACE_SMPTE170M
;
1230 static int saa7134_g_fmt_vid_overlay(struct file
*file
, void *priv
,
1231 struct v4l2_format
*f
)
1233 struct saa7134_dev
*dev
= video_drvdata(file
);
1234 struct v4l2_clip __user
*clips
= f
->fmt
.win
.clips
;
1235 u32 clipcount
= f
->fmt
.win
.clipcount
;
1239 if (saa7134_no_overlay
> 0) {
1240 pr_err("V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
1243 f
->fmt
.win
= dev
->win
;
1244 f
->fmt
.win
.clips
= clips
;
1247 if (dev
->nclips
< clipcount
)
1248 clipcount
= dev
->nclips
;
1249 f
->fmt
.win
.clipcount
= clipcount
;
1251 for (i
= 0; !err
&& i
< clipcount
; i
++) {
1252 if (copy_to_user(&f
->fmt
.win
.clips
[i
].c
, &dev
->clips
[i
].c
,
1253 sizeof(struct v4l2_rect
)))
1260 static int saa7134_try_fmt_vid_cap(struct file
*file
, void *priv
,
1261 struct v4l2_format
*f
)
1263 struct saa7134_dev
*dev
= video_drvdata(file
);
1264 struct saa7134_format
*fmt
;
1265 enum v4l2_field field
;
1266 unsigned int maxw
, maxh
;
1268 fmt
= format_by_fourcc(f
->fmt
.pix
.pixelformat
);
1272 field
= f
->fmt
.pix
.field
;
1273 maxw
= min(dev
->crop_current
.width
*4, dev
->crop_bounds
.width
);
1274 maxh
= min(dev
->crop_current
.height
*4, dev
->crop_bounds
.height
);
1276 if (V4L2_FIELD_ANY
== field
) {
1277 field
= (f
->fmt
.pix
.height
> maxh
/2)
1278 ? V4L2_FIELD_INTERLACED
1279 : V4L2_FIELD_BOTTOM
;
1282 case V4L2_FIELD_TOP
:
1283 case V4L2_FIELD_BOTTOM
:
1287 field
= V4L2_FIELD_INTERLACED
;
1291 f
->fmt
.pix
.field
= field
;
1292 if (f
->fmt
.pix
.width
< 48)
1293 f
->fmt
.pix
.width
= 48;
1294 if (f
->fmt
.pix
.height
< 32)
1295 f
->fmt
.pix
.height
= 32;
1296 if (f
->fmt
.pix
.width
> maxw
)
1297 f
->fmt
.pix
.width
= maxw
;
1298 if (f
->fmt
.pix
.height
> maxh
)
1299 f
->fmt
.pix
.height
= maxh
;
1300 f
->fmt
.pix
.width
&= ~0x03;
1301 f
->fmt
.pix
.bytesperline
=
1302 (f
->fmt
.pix
.width
* fmt
->depth
) >> 3;
1303 f
->fmt
.pix
.sizeimage
=
1304 f
->fmt
.pix
.height
* f
->fmt
.pix
.bytesperline
;
1305 f
->fmt
.pix
.colorspace
= V4L2_COLORSPACE_SMPTE170M
;
1310 static int saa7134_try_fmt_vid_overlay(struct file
*file
, void *priv
,
1311 struct v4l2_format
*f
)
1313 struct saa7134_dev
*dev
= video_drvdata(file
);
1315 if (saa7134_no_overlay
> 0) {
1316 pr_err("V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
1320 if (f
->fmt
.win
.clips
== NULL
)
1321 f
->fmt
.win
.clipcount
= 0;
1322 return verify_preview(dev
, &f
->fmt
.win
, true);
1325 static int saa7134_s_fmt_vid_cap(struct file
*file
, void *priv
,
1326 struct v4l2_format
*f
)
1328 struct saa7134_dev
*dev
= video_drvdata(file
);
1331 err
= saa7134_try_fmt_vid_cap(file
, priv
, f
);
1335 dev
->fmt
= format_by_fourcc(f
->fmt
.pix
.pixelformat
);
1336 dev
->width
= f
->fmt
.pix
.width
;
1337 dev
->height
= f
->fmt
.pix
.height
;
1338 dev
->field
= f
->fmt
.pix
.field
;
1342 static int saa7134_s_fmt_vid_overlay(struct file
*file
, void *priv
,
1343 struct v4l2_format
*f
)
1345 struct saa7134_dev
*dev
= video_drvdata(file
);
1347 unsigned long flags
;
1349 if (saa7134_no_overlay
> 0) {
1350 pr_err("V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
1353 if (f
->fmt
.win
.clips
== NULL
)
1354 f
->fmt
.win
.clipcount
= 0;
1355 err
= verify_preview(dev
, &f
->fmt
.win
, true);
1359 dev
->win
= f
->fmt
.win
;
1360 dev
->nclips
= f
->fmt
.win
.clipcount
;
1362 if (copy_from_user(dev
->clips
, f
->fmt
.win
.clips
,
1363 sizeof(struct v4l2_clip
) * dev
->nclips
))
1366 if (priv
== dev
->overlay_owner
) {
1367 spin_lock_irqsave(&dev
->slock
, flags
);
1370 spin_unlock_irqrestore(&dev
->slock
, flags
);
1376 int saa7134_enum_input(struct file
*file
, void *priv
, struct v4l2_input
*i
)
1378 struct saa7134_dev
*dev
= video_drvdata(file
);
1382 if (n
>= SAA7134_INPUT_MAX
)
1384 if (NULL
== card_in(dev
, i
->index
).name
)
1387 i
->type
= V4L2_INPUT_TYPE_CAMERA
;
1388 strcpy(i
->name
, card_in(dev
, n
).name
);
1389 if (card_in(dev
, n
).tv
)
1390 i
->type
= V4L2_INPUT_TYPE_TUNER
;
1391 if (n
== dev
->ctl_input
) {
1392 int v1
= saa_readb(SAA7134_STATUS_VIDEO1
);
1393 int v2
= saa_readb(SAA7134_STATUS_VIDEO2
);
1395 if (0 != (v1
& 0x40))
1396 i
->status
|= V4L2_IN_ST_NO_H_LOCK
;
1397 if (0 != (v2
& 0x40))
1398 i
->status
|= V4L2_IN_ST_NO_SIGNAL
;
1399 if (0 != (v2
& 0x0e))
1400 i
->status
|= V4L2_IN_ST_MACROVISION
;
1402 i
->std
= SAA7134_NORMS
;
1405 EXPORT_SYMBOL_GPL(saa7134_enum_input
);
1407 int saa7134_g_input(struct file
*file
, void *priv
, unsigned int *i
)
1409 struct saa7134_dev
*dev
= video_drvdata(file
);
1411 *i
= dev
->ctl_input
;
1414 EXPORT_SYMBOL_GPL(saa7134_g_input
);
1416 int saa7134_s_input(struct file
*file
, void *priv
, unsigned int i
)
1418 struct saa7134_dev
*dev
= video_drvdata(file
);
1420 if (i
>= SAA7134_INPUT_MAX
)
1422 if (NULL
== card_in(dev
, i
).name
)
1427 EXPORT_SYMBOL_GPL(saa7134_s_input
);
1429 int saa7134_querycap(struct file
*file
, void *priv
,
1430 struct v4l2_capability
*cap
)
1432 struct saa7134_dev
*dev
= video_drvdata(file
);
1433 struct video_device
*vdev
= video_devdata(file
);
1434 u32 radio_caps
, video_caps
, vbi_caps
;
1436 unsigned int tuner_type
= dev
->tuner_type
;
1438 strcpy(cap
->driver
, "saa7134");
1439 strlcpy(cap
->card
, saa7134_boards
[dev
->board
].name
,
1441 sprintf(cap
->bus_info
, "PCI:%s", pci_name(dev
->pci
));
1443 cap
->device_caps
= V4L2_CAP_READWRITE
| V4L2_CAP_STREAMING
;
1444 if ((tuner_type
!= TUNER_ABSENT
) && (tuner_type
!= UNSET
))
1445 cap
->device_caps
|= V4L2_CAP_TUNER
;
1447 radio_caps
= V4L2_CAP_RADIO
;
1449 radio_caps
|= V4L2_CAP_RDS_CAPTURE
;
1451 video_caps
= V4L2_CAP_VIDEO_CAPTURE
;
1452 if (saa7134_no_overlay
<= 0 && !is_empress(file
))
1453 video_caps
|= V4L2_CAP_VIDEO_OVERLAY
;
1455 vbi_caps
= V4L2_CAP_VBI_CAPTURE
;
1457 switch (vdev
->vfl_type
) {
1458 case VFL_TYPE_RADIO
:
1459 cap
->device_caps
|= radio_caps
;
1461 case VFL_TYPE_GRABBER
:
1462 cap
->device_caps
|= video_caps
;
1465 cap
->device_caps
|= vbi_caps
;
1468 cap
->capabilities
= radio_caps
| video_caps
| vbi_caps
|
1469 cap
->device_caps
| V4L2_CAP_DEVICE_CAPS
;
1470 if (vdev
->vfl_type
== VFL_TYPE_RADIO
) {
1471 cap
->device_caps
&= ~V4L2_CAP_STREAMING
;
1473 cap
->device_caps
&= ~V4L2_CAP_READWRITE
;
1478 EXPORT_SYMBOL_GPL(saa7134_querycap
);
1480 int saa7134_s_std(struct file
*file
, void *priv
, v4l2_std_id id
)
1482 struct saa7134_dev
*dev
= video_drvdata(file
);
1483 struct v4l2_fh
*fh
= priv
;
1484 unsigned long flags
;
1488 if (is_empress(file
) && dev
->overlay_owner
) {
1489 /* Don't change the std from the mpeg device
1490 if overlay is active. */
1494 for (i
= 0; i
< TVNORMS
; i
++)
1495 if (id
== tvnorms
[i
].id
)
1499 for (i
= 0; i
< TVNORMS
; i
++)
1500 if (id
& tvnorms
[i
].id
)
1505 if ((id
& V4L2_STD_SECAM
) && (secam
[0] != '-')) {
1506 if (secam
[0] == 'L' || secam
[0] == 'l') {
1507 if (secam
[1] == 'C' || secam
[1] == 'c')
1508 fixup
= V4L2_STD_SECAM_LC
;
1510 fixup
= V4L2_STD_SECAM_L
;
1512 if (secam
[0] == 'D' || secam
[0] == 'd')
1513 fixup
= V4L2_STD_SECAM_DK
;
1515 fixup
= V4L2_STD_SECAM
;
1517 for (i
= 0; i
< TVNORMS
; i
++) {
1518 if (fixup
== tvnorms
[i
].id
)
1527 if (!is_empress(file
) && fh
== dev
->overlay_owner
) {
1528 spin_lock_irqsave(&dev
->slock
, flags
);
1530 spin_unlock_irqrestore(&dev
->slock
, flags
);
1532 set_tvnorm(dev
, &tvnorms
[i
]);
1534 spin_lock_irqsave(&dev
->slock
, flags
);
1536 spin_unlock_irqrestore(&dev
->slock
, flags
);
1538 set_tvnorm(dev
, &tvnorms
[i
]);
1540 saa7134_tvaudio_do_scan(dev
);
1543 EXPORT_SYMBOL_GPL(saa7134_s_std
);
1545 int saa7134_g_std(struct file
*file
, void *priv
, v4l2_std_id
*id
)
1547 struct saa7134_dev
*dev
= video_drvdata(file
);
1549 *id
= dev
->tvnorm
->id
;
1552 EXPORT_SYMBOL_GPL(saa7134_g_std
);
1554 static v4l2_std_id
saa7134_read_std(struct saa7134_dev
*dev
)
1556 static v4l2_std_id stds
[] = {
1562 v4l2_std_id result
= 0;
1564 u8 st1
= saa_readb(SAA7134_STATUS_VIDEO1
);
1565 u8 st2
= saa_readb(SAA7134_STATUS_VIDEO2
);
1567 if (!(st2
& 0x1)) /* RDCAP == 0 */
1568 result
= V4L2_STD_UNKNOWN
;
1570 result
= stds
[st1
& 0x03];
1575 int saa7134_querystd(struct file
*file
, void *priv
, v4l2_std_id
*std
)
1577 struct saa7134_dev
*dev
= video_drvdata(file
);
1578 *std
&= saa7134_read_std(dev
);
1581 EXPORT_SYMBOL_GPL(saa7134_querystd
);
1583 static int saa7134_cropcap(struct file
*file
, void *priv
,
1584 struct v4l2_cropcap
*cap
)
1586 struct saa7134_dev
*dev
= video_drvdata(file
);
1588 if (cap
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
&&
1589 cap
->type
!= V4L2_BUF_TYPE_VIDEO_OVERLAY
)
1591 cap
->bounds
= dev
->crop_bounds
;
1592 cap
->defrect
= dev
->crop_defrect
;
1593 cap
->pixelaspect
.numerator
= 1;
1594 cap
->pixelaspect
.denominator
= 1;
1595 if (dev
->tvnorm
->id
& V4L2_STD_525_60
) {
1596 cap
->pixelaspect
.numerator
= 11;
1597 cap
->pixelaspect
.denominator
= 10;
1599 if (dev
->tvnorm
->id
& V4L2_STD_625_50
) {
1600 cap
->pixelaspect
.numerator
= 54;
1601 cap
->pixelaspect
.denominator
= 59;
1606 static int saa7134_g_crop(struct file
*file
, void *f
, struct v4l2_crop
*crop
)
1608 struct saa7134_dev
*dev
= video_drvdata(file
);
1610 if (crop
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
&&
1611 crop
->type
!= V4L2_BUF_TYPE_VIDEO_OVERLAY
)
1613 crop
->c
= dev
->crop_current
;
1617 static int saa7134_s_crop(struct file
*file
, void *f
, const struct v4l2_crop
*crop
)
1619 struct saa7134_dev
*dev
= video_drvdata(file
);
1620 struct v4l2_rect
*b
= &dev
->crop_bounds
;
1621 struct v4l2_rect
*c
= &dev
->crop_current
;
1623 if (crop
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
&&
1624 crop
->type
!= V4L2_BUF_TYPE_VIDEO_OVERLAY
)
1627 if (dev
->overlay_owner
)
1629 if (vb2_is_streaming(&dev
->video_vbq
))
1633 if (c
->top
< b
->top
)
1635 if (c
->top
> b
->top
+ b
->height
)
1636 c
->top
= b
->top
+ b
->height
;
1637 if (c
->height
> b
->top
- c
->top
+ b
->height
)
1638 c
->height
= b
->top
- c
->top
+ b
->height
;
1640 if (c
->left
< b
->left
)
1642 if (c
->left
> b
->left
+ b
->width
)
1643 c
->left
= b
->left
+ b
->width
;
1644 if (c
->width
> b
->left
- c
->left
+ b
->width
)
1645 c
->width
= b
->left
- c
->left
+ b
->width
;
1649 int saa7134_g_tuner(struct file
*file
, void *priv
,
1650 struct v4l2_tuner
*t
)
1652 struct saa7134_dev
*dev
= video_drvdata(file
);
1657 memset(t
, 0, sizeof(*t
));
1658 for (n
= 0; n
< SAA7134_INPUT_MAX
; n
++) {
1659 if (card_in(dev
, n
).tv
)
1662 if (n
== SAA7134_INPUT_MAX
)
1664 if (NULL
!= card_in(dev
, n
).name
) {
1665 strcpy(t
->name
, "Television");
1666 t
->type
= V4L2_TUNER_ANALOG_TV
;
1667 saa_call_all(dev
, tuner
, g_tuner
, t
);
1668 t
->capability
= V4L2_TUNER_CAP_NORM
|
1669 V4L2_TUNER_CAP_STEREO
|
1670 V4L2_TUNER_CAP_LANG1
|
1671 V4L2_TUNER_CAP_LANG2
;
1672 t
->rxsubchans
= saa7134_tvaudio_getstereo(dev
);
1673 t
->audmode
= saa7134_tvaudio_rx2mode(t
->rxsubchans
);
1675 if (0 != (saa_readb(SAA7134_STATUS_VIDEO1
) & 0x03))
1679 EXPORT_SYMBOL_GPL(saa7134_g_tuner
);
1681 int saa7134_s_tuner(struct file
*file
, void *priv
,
1682 const struct v4l2_tuner
*t
)
1684 struct saa7134_dev
*dev
= video_drvdata(file
);
1690 mode
= dev
->thread
.mode
;
1691 if (UNSET
== mode
) {
1692 rx
= saa7134_tvaudio_getstereo(dev
);
1693 mode
= saa7134_tvaudio_rx2mode(rx
);
1695 if (mode
!= t
->audmode
)
1696 dev
->thread
.mode
= t
->audmode
;
1700 EXPORT_SYMBOL_GPL(saa7134_s_tuner
);
1702 int saa7134_g_frequency(struct file
*file
, void *priv
,
1703 struct v4l2_frequency
*f
)
1705 struct saa7134_dev
*dev
= video_drvdata(file
);
1710 saa_call_all(dev
, tuner
, g_frequency
, f
);
1714 EXPORT_SYMBOL_GPL(saa7134_g_frequency
);
1716 int saa7134_s_frequency(struct file
*file
, void *priv
,
1717 const struct v4l2_frequency
*f
)
1719 struct saa7134_dev
*dev
= video_drvdata(file
);
1724 saa_call_all(dev
, tuner
, s_frequency
, f
);
1726 saa7134_tvaudio_do_scan(dev
);
1729 EXPORT_SYMBOL_GPL(saa7134_s_frequency
);
1731 static int saa7134_enum_fmt_vid_cap(struct file
*file
, void *priv
,
1732 struct v4l2_fmtdesc
*f
)
1734 if (f
->index
>= FORMATS
)
1737 strlcpy(f
->description
, formats
[f
->index
].name
,
1738 sizeof(f
->description
));
1740 f
->pixelformat
= formats
[f
->index
].fourcc
;
1745 static int saa7134_enum_fmt_vid_overlay(struct file
*file
, void *priv
,
1746 struct v4l2_fmtdesc
*f
)
1748 if (saa7134_no_overlay
> 0) {
1749 pr_err("V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
1753 if ((f
->index
>= FORMATS
) || formats
[f
->index
].planar
)
1756 strlcpy(f
->description
, formats
[f
->index
].name
,
1757 sizeof(f
->description
));
1759 f
->pixelformat
= formats
[f
->index
].fourcc
;
1764 static int saa7134_g_fbuf(struct file
*file
, void *f
,
1765 struct v4l2_framebuffer
*fb
)
1767 struct saa7134_dev
*dev
= video_drvdata(file
);
1770 fb
->capability
= V4L2_FBUF_CAP_LIST_CLIPPING
;
1775 static int saa7134_s_fbuf(struct file
*file
, void *f
,
1776 const struct v4l2_framebuffer
*fb
)
1778 struct saa7134_dev
*dev
= video_drvdata(file
);
1779 struct saa7134_format
*fmt
;
1781 if (!capable(CAP_SYS_ADMIN
) &&
1782 !capable(CAP_SYS_RAWIO
))
1786 fmt
= format_by_fourcc(fb
->fmt
.pixelformat
);
1793 if (0 == dev
->ovbuf
.fmt
.bytesperline
)
1794 dev
->ovbuf
.fmt
.bytesperline
=
1795 dev
->ovbuf
.fmt
.width
*fmt
->depth
/8;
1799 static int saa7134_overlay(struct file
*file
, void *priv
, unsigned int on
)
1801 struct saa7134_dev
*dev
= video_drvdata(file
);
1802 unsigned long flags
;
1805 if (saa7134_no_overlay
> 0) {
1806 video_dbg("no_overlay\n");
1810 if (dev
->overlay_owner
&& priv
!= dev
->overlay_owner
)
1812 dev
->overlay_owner
= priv
;
1813 spin_lock_irqsave(&dev
->slock
, flags
);
1815 spin_unlock_irqrestore(&dev
->slock
, flags
);
1818 if (priv
!= dev
->overlay_owner
)
1820 spin_lock_irqsave(&dev
->slock
, flags
);
1822 spin_unlock_irqrestore(&dev
->slock
, flags
);
1823 dev
->overlay_owner
= NULL
;
1828 #ifdef CONFIG_VIDEO_ADV_DEBUG
1829 static int vidioc_g_register (struct file
*file
, void *priv
,
1830 struct v4l2_dbg_register
*reg
)
1832 struct saa7134_dev
*dev
= video_drvdata(file
);
1834 reg
->val
= saa_readb(reg
->reg
& 0xffffff);
1839 static int vidioc_s_register (struct file
*file
, void *priv
,
1840 const struct v4l2_dbg_register
*reg
)
1842 struct saa7134_dev
*dev
= video_drvdata(file
);
1844 saa_writeb(reg
->reg
& 0xffffff, reg
->val
);
1849 static int radio_g_tuner(struct file
*file
, void *priv
,
1850 struct v4l2_tuner
*t
)
1852 struct saa7134_dev
*dev
= video_drvdata(file
);
1857 strcpy(t
->name
, "Radio");
1859 saa_call_all(dev
, tuner
, g_tuner
, t
);
1860 t
->audmode
&= V4L2_TUNER_MODE_MONO
| V4L2_TUNER_MODE_STEREO
;
1861 if (dev
->input
->amux
== TV
) {
1862 t
->signal
= 0xf800 - ((saa_readb(0x581) & 0x1f) << 11);
1863 t
->rxsubchans
= (saa_readb(0x529) & 0x08) ?
1864 V4L2_TUNER_SUB_STEREO
: V4L2_TUNER_SUB_MONO
;
1868 static int radio_s_tuner(struct file
*file
, void *priv
,
1869 const struct v4l2_tuner
*t
)
1871 struct saa7134_dev
*dev
= video_drvdata(file
);
1876 saa_call_all(dev
, tuner
, s_tuner
, t
);
1880 static const struct v4l2_file_operations video_fops
=
1882 .owner
= THIS_MODULE
,
1884 .release
= video_release
,
1885 .read
= vb2_fop_read
,
1886 .poll
= vb2_fop_poll
,
1887 .mmap
= vb2_fop_mmap
,
1888 .unlocked_ioctl
= video_ioctl2
,
1891 static const struct v4l2_ioctl_ops video_ioctl_ops
= {
1892 .vidioc_querycap
= saa7134_querycap
,
1893 .vidioc_enum_fmt_vid_cap
= saa7134_enum_fmt_vid_cap
,
1894 .vidioc_g_fmt_vid_cap
= saa7134_g_fmt_vid_cap
,
1895 .vidioc_try_fmt_vid_cap
= saa7134_try_fmt_vid_cap
,
1896 .vidioc_s_fmt_vid_cap
= saa7134_s_fmt_vid_cap
,
1897 .vidioc_enum_fmt_vid_overlay
= saa7134_enum_fmt_vid_overlay
,
1898 .vidioc_g_fmt_vid_overlay
= saa7134_g_fmt_vid_overlay
,
1899 .vidioc_try_fmt_vid_overlay
= saa7134_try_fmt_vid_overlay
,
1900 .vidioc_s_fmt_vid_overlay
= saa7134_s_fmt_vid_overlay
,
1901 .vidioc_g_fmt_vbi_cap
= saa7134_try_get_set_fmt_vbi_cap
,
1902 .vidioc_try_fmt_vbi_cap
= saa7134_try_get_set_fmt_vbi_cap
,
1903 .vidioc_s_fmt_vbi_cap
= saa7134_try_get_set_fmt_vbi_cap
,
1904 .vidioc_cropcap
= saa7134_cropcap
,
1905 .vidioc_reqbufs
= vb2_ioctl_reqbufs
,
1906 .vidioc_querybuf
= vb2_ioctl_querybuf
,
1907 .vidioc_qbuf
= vb2_ioctl_qbuf
,
1908 .vidioc_dqbuf
= vb2_ioctl_dqbuf
,
1909 .vidioc_s_std
= saa7134_s_std
,
1910 .vidioc_g_std
= saa7134_g_std
,
1911 .vidioc_querystd
= saa7134_querystd
,
1912 .vidioc_enum_input
= saa7134_enum_input
,
1913 .vidioc_g_input
= saa7134_g_input
,
1914 .vidioc_s_input
= saa7134_s_input
,
1915 .vidioc_streamon
= vb2_ioctl_streamon
,
1916 .vidioc_streamoff
= vb2_ioctl_streamoff
,
1917 .vidioc_g_tuner
= saa7134_g_tuner
,
1918 .vidioc_s_tuner
= saa7134_s_tuner
,
1919 .vidioc_g_crop
= saa7134_g_crop
,
1920 .vidioc_s_crop
= saa7134_s_crop
,
1921 .vidioc_g_fbuf
= saa7134_g_fbuf
,
1922 .vidioc_s_fbuf
= saa7134_s_fbuf
,
1923 .vidioc_overlay
= saa7134_overlay
,
1924 .vidioc_g_frequency
= saa7134_g_frequency
,
1925 .vidioc_s_frequency
= saa7134_s_frequency
,
1926 #ifdef CONFIG_VIDEO_ADV_DEBUG
1927 .vidioc_g_register
= vidioc_g_register
,
1928 .vidioc_s_register
= vidioc_s_register
,
1930 .vidioc_log_status
= v4l2_ctrl_log_status
,
1931 .vidioc_subscribe_event
= v4l2_ctrl_subscribe_event
,
1932 .vidioc_unsubscribe_event
= v4l2_event_unsubscribe
,
1935 static const struct v4l2_file_operations radio_fops
= {
1936 .owner
= THIS_MODULE
,
1939 .release
= video_release
,
1940 .unlocked_ioctl
= video_ioctl2
,
1944 static const struct v4l2_ioctl_ops radio_ioctl_ops
= {
1945 .vidioc_querycap
= saa7134_querycap
,
1946 .vidioc_g_tuner
= radio_g_tuner
,
1947 .vidioc_s_tuner
= radio_s_tuner
,
1948 .vidioc_g_frequency
= saa7134_g_frequency
,
1949 .vidioc_s_frequency
= saa7134_s_frequency
,
1950 .vidioc_subscribe_event
= v4l2_ctrl_subscribe_event
,
1951 .vidioc_unsubscribe_event
= v4l2_event_unsubscribe
,
1954 /* ----------------------------------------------------------- */
1955 /* exported stuff */
1957 struct video_device saa7134_video_template
= {
1958 .name
= "saa7134-video",
1959 .fops
= &video_fops
,
1960 .ioctl_ops
= &video_ioctl_ops
,
1961 .tvnorms
= SAA7134_NORMS
,
1964 struct video_device saa7134_radio_template
= {
1965 .name
= "saa7134-radio",
1966 .fops
= &radio_fops
,
1967 .ioctl_ops
= &radio_ioctl_ops
,
1970 static const struct v4l2_ctrl_ops saa7134_ctrl_ops
= {
1971 .s_ctrl
= saa7134_s_ctrl
,
1974 static const struct v4l2_ctrl_config saa7134_ctrl_invert
= {
1975 .ops
= &saa7134_ctrl_ops
,
1976 .id
= V4L2_CID_PRIVATE_INVERT
,
1978 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
1984 static const struct v4l2_ctrl_config saa7134_ctrl_y_odd
= {
1985 .ops
= &saa7134_ctrl_ops
,
1986 .id
= V4L2_CID_PRIVATE_Y_ODD
,
1987 .name
= "Y Offset Odd Field",
1988 .type
= V4L2_CTRL_TYPE_INTEGER
,
1994 static const struct v4l2_ctrl_config saa7134_ctrl_y_even
= {
1995 .ops
= &saa7134_ctrl_ops
,
1996 .id
= V4L2_CID_PRIVATE_Y_EVEN
,
1997 .name
= "Y Offset Even Field",
1998 .type
= V4L2_CTRL_TYPE_INTEGER
,
2004 static const struct v4l2_ctrl_config saa7134_ctrl_automute
= {
2005 .ops
= &saa7134_ctrl_ops
,
2006 .id
= V4L2_CID_PRIVATE_AUTOMUTE
,
2008 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
2015 int saa7134_video_init1(struct saa7134_dev
*dev
)
2017 struct v4l2_ctrl_handler
*hdl
= &dev
->ctrl_handler
;
2018 struct vb2_queue
*q
;
2021 /* sanitycheck insmod options */
2022 if (gbuffers
< 2 || gbuffers
> VIDEO_MAX_FRAME
)
2024 if (gbufsize
> gbufsize_max
)
2025 gbufsize
= gbufsize_max
;
2026 gbufsize
= (gbufsize
+ PAGE_SIZE
- 1) & PAGE_MASK
;
2028 v4l2_ctrl_handler_init(hdl
, 11);
2029 v4l2_ctrl_new_std(hdl
, &saa7134_ctrl_ops
,
2030 V4L2_CID_BRIGHTNESS
, 0, 255, 1, 128);
2031 v4l2_ctrl_new_std(hdl
, &saa7134_ctrl_ops
,
2032 V4L2_CID_CONTRAST
, 0, 127, 1, 68);
2033 v4l2_ctrl_new_std(hdl
, &saa7134_ctrl_ops
,
2034 V4L2_CID_SATURATION
, 0, 127, 1, 64);
2035 v4l2_ctrl_new_std(hdl
, &saa7134_ctrl_ops
,
2036 V4L2_CID_HUE
, -128, 127, 1, 0);
2037 v4l2_ctrl_new_std(hdl
, &saa7134_ctrl_ops
,
2038 V4L2_CID_HFLIP
, 0, 1, 1, 0);
2039 v4l2_ctrl_new_std(hdl
, &saa7134_ctrl_ops
,
2040 V4L2_CID_AUDIO_MUTE
, 0, 1, 1, 0);
2041 v4l2_ctrl_new_std(hdl
, &saa7134_ctrl_ops
,
2042 V4L2_CID_AUDIO_VOLUME
, -15, 15, 1, 0);
2043 v4l2_ctrl_new_custom(hdl
, &saa7134_ctrl_invert
, NULL
);
2044 v4l2_ctrl_new_custom(hdl
, &saa7134_ctrl_y_odd
, NULL
);
2045 v4l2_ctrl_new_custom(hdl
, &saa7134_ctrl_y_even
, NULL
);
2046 v4l2_ctrl_new_custom(hdl
, &saa7134_ctrl_automute
, NULL
);
2049 if (card_has_radio(dev
)) {
2050 hdl
= &dev
->radio_ctrl_handler
;
2051 v4l2_ctrl_handler_init(hdl
, 2);
2052 v4l2_ctrl_add_handler(hdl
, &dev
->ctrl_handler
,
2053 v4l2_ctrl_radio_filter
);
2059 if (dev
->tda9887_conf
&& saa7134_ctrl_automute
.def
)
2060 dev
->tda9887_conf
|= TDA9887_AUTOMUTE
;
2063 INIT_LIST_HEAD(&dev
->video_q
.queue
);
2064 init_timer(&dev
->video_q
.timeout
);
2065 dev
->video_q
.timeout
.function
= saa7134_buffer_timeout
;
2066 dev
->video_q
.timeout
.data
= (unsigned long)(&dev
->video_q
);
2067 dev
->video_q
.dev
= dev
;
2068 dev
->fmt
= format_by_fourcc(V4L2_PIX_FMT_BGR24
);
2071 dev
->field
= V4L2_FIELD_INTERLACED
;
2072 dev
->win
.w
.width
= dev
->width
;
2073 dev
->win
.w
.height
= dev
->height
;
2074 dev
->win
.field
= V4L2_FIELD_INTERLACED
;
2075 dev
->ovbuf
.fmt
.width
= dev
->width
;
2076 dev
->ovbuf
.fmt
.height
= dev
->height
;
2077 dev
->ovbuf
.fmt
.pixelformat
= dev
->fmt
->fourcc
;
2078 dev
->ovbuf
.fmt
.colorspace
= V4L2_COLORSPACE_SMPTE170M
;
2080 if (saa7134_boards
[dev
->board
].video_out
)
2081 saa7134_videoport_init(dev
);
2083 q
= &dev
->video_vbq
;
2084 q
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
2086 * Do not add VB2_USERPTR unless explicitly requested: the saa7134 DMA
2087 * engine cannot handle transfers that do not start at the beginning
2088 * of a page. A user-provided pointer can start anywhere in a page, so
2089 * USERPTR support is a no-go unless the application knows about these
2090 * limitations and has special support for this.
2092 q
->io_modes
= VB2_MMAP
| VB2_READ
;
2093 if (saa7134_userptr
)
2094 q
->io_modes
|= VB2_USERPTR
;
2095 q
->drv_priv
= &dev
->video_q
;
2097 q
->gfp_flags
= GFP_DMA32
;
2098 q
->mem_ops
= &vb2_dma_sg_memops
;
2099 q
->buf_struct_size
= sizeof(struct saa7134_buf
);
2100 q
->timestamp_flags
= V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC
;
2101 q
->lock
= &dev
->lock
;
2102 ret
= vb2_queue_init(q
);
2105 saa7134_pgtable_alloc(dev
->pci
, &dev
->video_q
.pt
);
2108 q
->type
= V4L2_BUF_TYPE_VBI_CAPTURE
;
2109 /* Don't add VB2_USERPTR, see comment above */
2110 q
->io_modes
= VB2_MMAP
| VB2_READ
;
2111 if (saa7134_userptr
)
2112 q
->io_modes
|= VB2_USERPTR
;
2113 q
->drv_priv
= &dev
->vbi_q
;
2114 q
->ops
= &saa7134_vbi_qops
;
2115 q
->gfp_flags
= GFP_DMA32
;
2116 q
->mem_ops
= &vb2_dma_sg_memops
;
2117 q
->buf_struct_size
= sizeof(struct saa7134_buf
);
2118 q
->timestamp_flags
= V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC
;
2119 q
->lock
= &dev
->lock
;
2120 ret
= vb2_queue_init(q
);
2123 saa7134_pgtable_alloc(dev
->pci
, &dev
->vbi_q
.pt
);
2128 void saa7134_video_fini(struct saa7134_dev
*dev
)
2131 vb2_queue_release(&dev
->video_vbq
);
2132 saa7134_pgtable_free(dev
->pci
, &dev
->video_q
.pt
);
2133 vb2_queue_release(&dev
->vbi_vbq
);
2134 saa7134_pgtable_free(dev
->pci
, &dev
->vbi_q
.pt
);
2135 v4l2_ctrl_handler_free(&dev
->ctrl_handler
);
2136 if (card_has_radio(dev
))
2137 v4l2_ctrl_handler_free(&dev
->radio_ctrl_handler
);
2140 int saa7134_videoport_init(struct saa7134_dev
*dev
)
2142 /* enable video output */
2143 int vo
= saa7134_boards
[dev
->board
].video_out
;
2145 unsigned int vid_port_opts
= saa7134_boards
[dev
->board
].vid_port_opts
;
2147 /* Configure videoport */
2148 saa_writeb(SAA7134_VIDEO_PORT_CTRL0
, video_out
[vo
][0]);
2149 video_reg
= video_out
[vo
][1];
2150 if (vid_port_opts
& SET_T_CODE_POLARITY_NON_INVERTED
)
2151 video_reg
&= ~VP_T_CODE_P_INVERTED
;
2152 saa_writeb(SAA7134_VIDEO_PORT_CTRL1
, video_reg
);
2153 saa_writeb(SAA7134_VIDEO_PORT_CTRL2
, video_out
[vo
][2]);
2154 saa_writeb(SAA7134_VIDEO_PORT_CTRL4
, video_out
[vo
][4]);
2155 video_reg
= video_out
[vo
][5];
2156 if (vid_port_opts
& SET_CLOCK_NOT_DELAYED
)
2157 video_reg
&= ~VP_CLK_CTRL2_DELAYED
;
2158 if (vid_port_opts
& SET_CLOCK_INVERTED
)
2159 video_reg
|= VP_CLK_CTRL1_INVERTED
;
2160 saa_writeb(SAA7134_VIDEO_PORT_CTRL5
, video_reg
);
2161 video_reg
= video_out
[vo
][6];
2162 if (vid_port_opts
& SET_VSYNC_OFF
) {
2163 video_reg
&= ~VP_VS_TYPE_MASK
;
2164 video_reg
|= VP_VS_TYPE_OFF
;
2166 saa_writeb(SAA7134_VIDEO_PORT_CTRL6
, video_reg
);
2167 saa_writeb(SAA7134_VIDEO_PORT_CTRL7
, video_out
[vo
][7]);
2168 saa_writeb(SAA7134_VIDEO_PORT_CTRL8
, video_out
[vo
][8]);
2170 /* Start videoport */
2171 saa_writeb(SAA7134_VIDEO_PORT_CTRL3
, video_out
[vo
][3]);
2176 int saa7134_video_init2(struct saa7134_dev
*dev
)
2179 set_tvnorm(dev
,&tvnorms
[0]);
2181 v4l2_ctrl_handler_setup(&dev
->ctrl_handler
);
2182 saa7134_tvaudio_setmute(dev
);
2183 saa7134_tvaudio_setvolume(dev
,dev
->ctl_volume
);
2187 void saa7134_irq_video_signalchange(struct saa7134_dev
*dev
)
2189 static const char *st
[] = {
2190 "(no signal)", "NTSC", "PAL", "SECAM" };
2193 st1
= saa_readb(SAA7134_STATUS_VIDEO1
);
2194 st2
= saa_readb(SAA7134_STATUS_VIDEO2
);
2195 video_dbg("DCSDT: pll: %s, sync: %s, norm: %s\n",
2196 (st1
& 0x40) ? "not locked" : "locked",
2197 (st2
& 0x40) ? "no" : "yes",
2199 dev
->nosignal
= (st1
& 0x40) || (st2
& 0x40) || !(st2
& 0x1);
2201 if (dev
->nosignal
) {
2202 /* no video signal -> mute audio */
2203 if (dev
->ctl_automute
)
2205 saa7134_tvaudio_setmute(dev
);
2207 /* wake up tvaudio audio carrier scan thread */
2208 saa7134_tvaudio_do_scan(dev
);
2211 if ((st2
& 0x80) && !noninterlaced
&& !dev
->nosignal
)
2212 saa_clearb(SAA7134_SYNC_CTRL
, 0x20);
2214 saa_setb(SAA7134_SYNC_CTRL
, 0x20);
2216 if (dev
->mops
&& dev
->mops
->signal_change
)
2217 dev
->mops
->signal_change(dev
);
2221 void saa7134_irq_video_done(struct saa7134_dev
*dev
, unsigned long status
)
2223 enum v4l2_field field
;
2225 spin_lock(&dev
->slock
);
2226 if (dev
->video_q
.curr
) {
2228 if (V4L2_FIELD_HAS_BOTH(field
)) {
2229 /* make sure we have seen both fields */
2230 if ((status
& 0x10) == 0x00) {
2231 dev
->video_q
.curr
->top_seen
= 1;
2234 if (!dev
->video_q
.curr
->top_seen
)
2236 } else if (field
== V4L2_FIELD_TOP
) {
2237 if ((status
& 0x10) != 0x10)
2239 } else if (field
== V4L2_FIELD_BOTTOM
) {
2240 if ((status
& 0x10) != 0x00)
2243 saa7134_buffer_finish(dev
, &dev
->video_q
, VB2_BUF_STATE_DONE
);
2245 saa7134_buffer_next(dev
, &dev
->video_q
);
2248 spin_unlock(&dev
->slock
);