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.
20 #include "saa7134-reg.h"
22 #include <linux/init.h>
23 #include <linux/list.h>
24 #include <linux/module.h>
25 #include <linux/kernel.h>
26 #include <linux/slab.h>
27 #include <linux/sort.h>
29 #include <media/v4l2-common.h>
30 #include <media/v4l2-event.h>
31 #include <media/i2c/saa6588.h>
33 /* ------------------------------------------------------------------ */
35 unsigned int video_debug
;
36 static unsigned int gbuffers
= 8;
37 static unsigned int noninterlaced
; /* 0 */
38 static unsigned int gbufsize
= 720*576*4;
39 static unsigned int gbufsize_max
= 720*576*4;
40 static char secam
[] = "--";
41 module_param(video_debug
, int, 0644);
42 MODULE_PARM_DESC(video_debug
,"enable debug messages [video]");
43 module_param(gbuffers
, int, 0444);
44 MODULE_PARM_DESC(gbuffers
,"number of capture buffers, range 2-32");
45 module_param(noninterlaced
, int, 0644);
46 MODULE_PARM_DESC(noninterlaced
,"capture non interlaced video");
47 module_param_string(secam
, secam
, sizeof(secam
), 0644);
48 MODULE_PARM_DESC(secam
, "force SECAM variant, either DK,L or Lc");
51 #define video_dbg(fmt, arg...) do { \
52 if (video_debug & 0x04) \
53 printk(KERN_DEBUG pr_fmt("video: " fmt), ## 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 = 23, \
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 /* ------------------------------------------------------------------ */
384 static void set_tvnorm(struct saa7134_dev
*dev
, struct saa7134_tvnorm
*norm
)
386 video_dbg("set tv norm = %s\n", norm
->name
);
390 dev
->crop_bounds
.left
= norm
->h_start
;
391 dev
->crop_defrect
.left
= norm
->h_start
;
392 dev
->crop_bounds
.width
= norm
->h_stop
- norm
->h_start
+1;
393 dev
->crop_defrect
.width
= norm
->h_stop
- norm
->h_start
+1;
395 dev
->crop_bounds
.top
= (norm
->vbi_v_stop_0
+1)*2;
396 dev
->crop_defrect
.top
= norm
->video_v_start
*2;
397 dev
->crop_bounds
.height
= ((norm
->id
& V4L2_STD_525_60
) ? 524 : 624)
398 - dev
->crop_bounds
.top
;
399 dev
->crop_defrect
.height
= (norm
->video_v_stop
- norm
->video_v_start
+1)*2;
401 dev
->crop_current
= dev
->crop_defrect
;
403 saa7134_set_tvnorm_hw(dev
);
406 static void video_mux(struct saa7134_dev
*dev
, int input
)
408 video_dbg("video input = %d [%s]\n",
409 input
, saa7134_input_name
[card_in(dev
, input
).type
]);
410 dev
->ctl_input
= input
;
411 set_tvnorm(dev
, dev
->tvnorm
);
412 saa7134_tvaudio_setinput(dev
, &card_in(dev
, input
));
416 static void saa7134_set_decoder(struct saa7134_dev
*dev
)
418 int luma_control
, sync_control
, chroma_ctrl1
, mux
;
420 struct saa7134_tvnorm
*norm
= dev
->tvnorm
;
421 mux
= card_in(dev
, dev
->ctl_input
).vmux
;
423 luma_control
= norm
->luma_control
;
424 sync_control
= norm
->sync_control
;
425 chroma_ctrl1
= norm
->chroma_ctrl1
;
428 luma_control
|= 0x80; /* svideo */
429 if (noninterlaced
|| dev
->nosignal
)
430 sync_control
|= 0x20;
432 /* switch on auto standard detection */
433 sync_control
|= SAA7134_SYNC_CTRL_AUFD
;
434 chroma_ctrl1
|= SAA7134_CHROMA_CTRL1_AUTO0
;
435 chroma_ctrl1
&= ~SAA7134_CHROMA_CTRL1_FCTC
;
436 luma_control
&= ~SAA7134_LUMA_CTRL_LDEL
;
438 /* setup video decoder */
439 saa_writeb(SAA7134_INCR_DELAY
, 0x08);
440 saa_writeb(SAA7134_ANALOG_IN_CTRL1
, 0xc0 | mux
);
441 saa_writeb(SAA7134_ANALOG_IN_CTRL2
, 0x00);
443 saa_writeb(SAA7134_ANALOG_IN_CTRL3
, 0x90);
444 saa_writeb(SAA7134_ANALOG_IN_CTRL4
, 0x90);
445 saa_writeb(SAA7134_HSYNC_START
, 0xeb);
446 saa_writeb(SAA7134_HSYNC_STOP
, 0xe0);
447 saa_writeb(SAA7134_SOURCE_TIMING1
, norm
->src_timing
);
449 saa_writeb(SAA7134_SYNC_CTRL
, sync_control
);
450 saa_writeb(SAA7134_LUMA_CTRL
, luma_control
);
451 saa_writeb(SAA7134_DEC_LUMA_BRIGHT
, dev
->ctl_bright
);
453 saa_writeb(SAA7134_DEC_LUMA_CONTRAST
,
454 dev
->ctl_invert
? -dev
->ctl_contrast
: dev
->ctl_contrast
);
456 saa_writeb(SAA7134_DEC_CHROMA_SATURATION
,
457 dev
->ctl_invert
? -dev
->ctl_saturation
: dev
->ctl_saturation
);
459 saa_writeb(SAA7134_DEC_CHROMA_HUE
, dev
->ctl_hue
);
460 saa_writeb(SAA7134_CHROMA_CTRL1
, chroma_ctrl1
);
461 saa_writeb(SAA7134_CHROMA_GAIN
, norm
->chroma_gain
);
463 saa_writeb(SAA7134_CHROMA_CTRL2
, norm
->chroma_ctrl2
);
464 saa_writeb(SAA7134_MODE_DELAY_CTRL
, 0x00);
466 saa_writeb(SAA7134_ANALOG_ADC
, 0x01);
467 saa_writeb(SAA7134_VGATE_START
, 0x11);
468 saa_writeb(SAA7134_VGATE_STOP
, 0xfe);
469 saa_writeb(SAA7134_MISC_VGATE_MSB
, norm
->vgate_misc
);
470 saa_writeb(SAA7134_RAW_DATA_GAIN
, 0x40);
471 saa_writeb(SAA7134_RAW_DATA_OFFSET
, 0x80);
474 void saa7134_set_tvnorm_hw(struct saa7134_dev
*dev
)
476 saa7134_set_decoder(dev
);
478 saa_call_all(dev
, video
, s_std
, dev
->tvnorm
->id
);
479 /* Set the correct norm for the saa6752hs. This function
480 does nothing if there is no saa6752hs. */
481 saa_call_empress(dev
, video
, s_std
, dev
->tvnorm
->id
);
484 static void set_h_prescale(struct saa7134_dev
*dev
, int task
, int prescale
)
486 static const struct {
493 /* XPSC XACL XC2_1 XDCG VPFY */
505 static const int count
= ARRAY_SIZE(vals
);
508 for (i
= 0; i
< count
; i
++)
509 if (vals
[i
].xpsc
== prescale
)
514 saa_writeb(SAA7134_H_PRESCALE(task
), vals
[i
].xpsc
);
515 saa_writeb(SAA7134_ACC_LENGTH(task
), vals
[i
].xacl
);
516 saa_writeb(SAA7134_LEVEL_CTRL(task
),
517 (vals
[i
].xc2_1
<< 3) | (vals
[i
].xdcg
));
518 saa_andorb(SAA7134_FIR_PREFILTER_CTRL(task
), 0x0f,
519 (vals
[i
].vpfy
<< 2) | vals
[i
].vpfy
);
522 static void set_v_scale(struct saa7134_dev
*dev
, int task
, int yscale
)
526 saa_writeb(SAA7134_V_SCALE_RATIO1(task
), yscale
& 0xff);
527 saa_writeb(SAA7134_V_SCALE_RATIO2(task
), yscale
>> 8);
529 mirror
= (dev
->ctl_mirror
) ? 0x02 : 0x00;
532 video_dbg("yscale LPI yscale=%d\n", yscale
);
533 saa_writeb(SAA7134_V_FILTER(task
), 0x00 | mirror
);
534 saa_writeb(SAA7134_LUMA_CONTRAST(task
), 0x40);
535 saa_writeb(SAA7134_CHROMA_SATURATION(task
), 0x40);
538 val
= 0x40 * 1024 / yscale
;
539 video_dbg("yscale ACM yscale=%d val=0x%x\n", yscale
, val
);
540 saa_writeb(SAA7134_V_FILTER(task
), 0x01 | mirror
);
541 saa_writeb(SAA7134_LUMA_CONTRAST(task
), val
);
542 saa_writeb(SAA7134_CHROMA_SATURATION(task
), val
);
544 saa_writeb(SAA7134_LUMA_BRIGHT(task
), 0x80);
547 static void set_size(struct saa7134_dev
*dev
, int task
,
548 int width
, int height
, int interlace
)
550 int prescale
,xscale
,yscale
,y_even
,y_odd
;
551 int h_start
, h_stop
, v_start
, v_stop
;
552 int div
= interlace
? 2 : 1;
554 /* setup video scaler */
555 h_start
= dev
->crop_current
.left
;
556 v_start
= dev
->crop_current
.top
/2;
557 h_stop
= (dev
->crop_current
.left
+ dev
->crop_current
.width
-1);
558 v_stop
= (dev
->crop_current
.top
+ dev
->crop_current
.height
-1)/2;
560 saa_writeb(SAA7134_VIDEO_H_START1(task
), h_start
& 0xff);
561 saa_writeb(SAA7134_VIDEO_H_START2(task
), h_start
>> 8);
562 saa_writeb(SAA7134_VIDEO_H_STOP1(task
), h_stop
& 0xff);
563 saa_writeb(SAA7134_VIDEO_H_STOP2(task
), h_stop
>> 8);
564 saa_writeb(SAA7134_VIDEO_V_START1(task
), v_start
& 0xff);
565 saa_writeb(SAA7134_VIDEO_V_START2(task
), v_start
>> 8);
566 saa_writeb(SAA7134_VIDEO_V_STOP1(task
), v_stop
& 0xff);
567 saa_writeb(SAA7134_VIDEO_V_STOP2(task
), v_stop
>> 8);
569 prescale
= dev
->crop_current
.width
/ width
;
572 xscale
= 1024 * dev
->crop_current
.width
/ prescale
/ width
;
573 yscale
= 512 * div
* dev
->crop_current
.height
/ height
;
574 video_dbg("prescale=%d xscale=%d yscale=%d\n",
575 prescale
, xscale
, yscale
);
576 set_h_prescale(dev
,task
,prescale
);
577 saa_writeb(SAA7134_H_SCALE_INC1(task
), xscale
& 0xff);
578 saa_writeb(SAA7134_H_SCALE_INC2(task
), xscale
>> 8);
579 set_v_scale(dev
,task
,yscale
);
581 saa_writeb(SAA7134_VIDEO_PIXELS1(task
), width
& 0xff);
582 saa_writeb(SAA7134_VIDEO_PIXELS2(task
), width
>> 8);
583 saa_writeb(SAA7134_VIDEO_LINES1(task
), height
/div
& 0xff);
584 saa_writeb(SAA7134_VIDEO_LINES2(task
), height
/div
>> 8);
586 /* deinterlace y offsets */
587 y_odd
= dev
->ctl_y_odd
;
588 y_even
= dev
->ctl_y_even
;
589 saa_writeb(SAA7134_V_PHASE_OFFSET0(task
), y_odd
);
590 saa_writeb(SAA7134_V_PHASE_OFFSET1(task
), y_even
);
591 saa_writeb(SAA7134_V_PHASE_OFFSET2(task
), y_odd
);
592 saa_writeb(SAA7134_V_PHASE_OFFSET3(task
), y_even
);
595 /* ------------------------------------------------------------------ */
603 static void set_cliplist(struct saa7134_dev
*dev
, int reg
,
604 struct cliplist
*cl
, int entries
, char *name
)
609 for (i
= 0; i
< entries
; i
++) {
610 winbits
|= cl
[i
].enable
;
611 winbits
&= ~cl
[i
].disable
;
612 if (i
< 15 && cl
[i
].position
== cl
[i
+1].position
)
614 saa_writeb(reg
+ 0, winbits
);
615 saa_writeb(reg
+ 2, cl
[i
].position
& 0xff);
616 saa_writeb(reg
+ 3, cl
[i
].position
>> 8);
617 video_dbg("clip: %s winbits=%02x pos=%d\n",
618 name
,winbits
,cl
[i
].position
);
621 for (; reg
< 0x400; reg
+= 8) {
622 saa_writeb(reg
+ 0, 0);
623 saa_writeb(reg
+ 1, 0);
624 saa_writeb(reg
+ 2, 0);
625 saa_writeb(reg
+ 3, 0);
629 static int clip_range(int val
)
636 /* Sort into smallest position first order */
637 static int cliplist_cmp(const void *a
, const void *b
)
639 const struct cliplist
*cla
= a
;
640 const struct cliplist
*clb
= b
;
641 if (cla
->position
< clb
->position
)
643 if (cla
->position
> clb
->position
)
648 static int setup_clipping(struct saa7134_dev
*dev
, struct v4l2_clip
*clips
,
649 int nclips
, int interlace
)
651 struct cliplist col
[16], row
[16];
652 int cols
= 0, rows
= 0, i
;
653 int div
= interlace
? 2 : 1;
655 memset(col
, 0, sizeof(col
));
656 memset(row
, 0, sizeof(row
));
657 for (i
= 0; i
< nclips
&& i
< 8; i
++) {
658 col
[cols
].position
= clip_range(clips
[i
].c
.left
);
659 col
[cols
].enable
= (1 << i
);
661 col
[cols
].position
= clip_range(clips
[i
].c
.left
+clips
[i
].c
.width
);
662 col
[cols
].disable
= (1 << i
);
664 row
[rows
].position
= clip_range(clips
[i
].c
.top
/ div
);
665 row
[rows
].enable
= (1 << i
);
667 row
[rows
].position
= clip_range((clips
[i
].c
.top
+ clips
[i
].c
.height
)
669 row
[rows
].disable
= (1 << i
);
672 sort(col
, cols
, sizeof col
[0], cliplist_cmp
, NULL
);
673 sort(row
, rows
, sizeof row
[0], cliplist_cmp
, NULL
);
674 set_cliplist(dev
,0x380,col
,cols
,"cols");
675 set_cliplist(dev
,0x384,row
,rows
,"rows");
679 static int verify_preview(struct saa7134_dev
*dev
, struct v4l2_window
*win
, bool try)
681 enum v4l2_field field
;
684 if (!try && (dev
->ovbuf
.base
== NULL
|| dev
->ovfmt
== NULL
))
686 if (win
->w
.width
< 48)
688 if (win
->w
.height
< 32)
690 if (win
->clipcount
> 8)
694 win
->global_alpha
= 0;
696 maxw
= dev
->crop_current
.width
;
697 maxh
= dev
->crop_current
.height
;
699 if (V4L2_FIELD_ANY
== field
) {
700 field
= (win
->w
.height
> maxh
/2)
701 ? V4L2_FIELD_INTERLACED
706 case V4L2_FIELD_BOTTOM
:
710 field
= V4L2_FIELD_INTERLACED
;
715 if (win
->w
.width
> maxw
)
717 if (win
->w
.height
> maxh
)
718 win
->w
.height
= maxh
;
722 static int start_preview(struct saa7134_dev
*dev
)
724 unsigned long base
,control
,bpl
;
727 err
= verify_preview(dev
, &dev
->win
, false);
731 dev
->ovfield
= dev
->win
.field
;
732 video_dbg("start_preview %dx%d+%d+%d %s field=%s\n",
733 dev
->win
.w
.width
, dev
->win
.w
.height
,
734 dev
->win
.w
.left
, dev
->win
.w
.top
,
735 dev
->ovfmt
->name
, v4l2_field_names
[dev
->ovfield
]);
737 /* setup window + clipping */
738 set_size(dev
, TASK_B
, dev
->win
.w
.width
, dev
->win
.w
.height
,
739 V4L2_FIELD_HAS_BOTH(dev
->ovfield
));
740 setup_clipping(dev
, dev
->clips
, dev
->nclips
,
741 V4L2_FIELD_HAS_BOTH(dev
->ovfield
));
743 saa_andorb(SAA7134_DATA_PATH(TASK_B
), 0x3f, 0x03);
745 saa_andorb(SAA7134_DATA_PATH(TASK_B
), 0x3f, 0x01);
746 saa_writeb(SAA7134_OFMT_VIDEO_B
, dev
->ovfmt
->pm
| 0x20);
748 /* dma: setup channel 1 (= Video Task B) */
749 base
= (unsigned long)dev
->ovbuf
.base
;
750 base
+= dev
->ovbuf
.fmt
.bytesperline
* dev
->win
.w
.top
;
751 base
+= dev
->ovfmt
->depth
/8 * dev
->win
.w
.left
;
752 bpl
= dev
->ovbuf
.fmt
.bytesperline
;
753 control
= SAA7134_RS_CONTROL_BURST_16
;
754 if (dev
->ovfmt
->bswap
)
755 control
|= SAA7134_RS_CONTROL_BSWAP
;
756 if (dev
->ovfmt
->wswap
)
757 control
|= SAA7134_RS_CONTROL_WSWAP
;
758 if (V4L2_FIELD_HAS_BOTH(dev
->ovfield
)) {
759 saa_writel(SAA7134_RS_BA1(1),base
);
760 saa_writel(SAA7134_RS_BA2(1),base
+bpl
);
761 saa_writel(SAA7134_RS_PITCH(1),bpl
*2);
762 saa_writel(SAA7134_RS_CONTROL(1),control
);
764 saa_writel(SAA7134_RS_BA1(1),base
);
765 saa_writel(SAA7134_RS_BA2(1),base
);
766 saa_writel(SAA7134_RS_PITCH(1),bpl
);
767 saa_writel(SAA7134_RS_CONTROL(1),control
);
772 saa7134_set_dmabits(dev
);
777 static int stop_preview(struct saa7134_dev
*dev
)
780 saa7134_set_dmabits(dev
);
785 * Media Controller helper functions
788 static int saa7134_enable_analog_tuner(struct saa7134_dev
*dev
)
790 #ifdef CONFIG_MEDIA_CONTROLLER
791 struct media_device
*mdev
= dev
->media_dev
;
792 struct media_entity
*source
;
793 struct media_link
*link
, *found_link
= NULL
;
794 int ret
, active_links
= 0;
796 if (!mdev
|| !dev
->decoder
)
800 * This will find the tuner that is connected into the decoder.
801 * Technically, this is not 100% correct, as the device may be
802 * using an analog input instead of the tuner. However, as we can't
803 * do DVB streaming while the DMA engine is being used for V4L2,
804 * this should be enough for the actual needs.
806 list_for_each_entry(link
, &dev
->decoder
->links
, list
) {
807 if (link
->sink
->entity
== dev
->decoder
) {
809 if (link
->flags
& MEDIA_LNK_FL_ENABLED
)
815 if (active_links
== 1 || !found_link
)
818 source
= found_link
->source
->entity
;
819 list_for_each_entry(link
, &source
->links
, list
) {
820 struct media_entity
*sink
;
823 sink
= link
->sink
->entity
;
825 if (sink
== dev
->decoder
)
826 flags
= MEDIA_LNK_FL_ENABLED
;
828 ret
= media_entity_setup_link(link
, flags
);
830 pr_err("Couldn't change link %s->%s to %s. Error %d\n",
831 source
->name
, sink
->name
,
832 flags
? "enabled" : "disabled",
841 /* ------------------------------------------------------------------ */
843 static int buffer_activate(struct saa7134_dev
*dev
,
844 struct saa7134_buf
*buf
,
845 struct saa7134_buf
*next
)
847 struct saa7134_dmaqueue
*dmaq
= buf
->vb2
.vb2_buf
.vb2_queue
->drv_priv
;
848 unsigned long base
,control
,bpl
;
849 unsigned long bpl_uv
,lines_uv
,base2
,base3
,tmp
; /* planar */
851 video_dbg("buffer_activate buf=%p\n", buf
);
854 set_size(dev
, TASK_A
, dev
->width
, dev
->height
,
855 V4L2_FIELD_HAS_BOTH(dev
->field
));
857 saa_andorb(SAA7134_DATA_PATH(TASK_A
), 0x3f, 0x03);
859 saa_andorb(SAA7134_DATA_PATH(TASK_A
), 0x3f, 0x01);
860 saa_writeb(SAA7134_OFMT_VIDEO_A
, dev
->fmt
->pm
);
862 /* DMA: setup channel 0 (= Video Task A0) */
863 base
= saa7134_buffer_base(buf
);
864 if (dev
->fmt
->planar
)
867 bpl
= (dev
->width
* dev
->fmt
->depth
) / 8;
868 control
= SAA7134_RS_CONTROL_BURST_16
|
869 SAA7134_RS_CONTROL_ME
|
870 (dmaq
->pt
.dma
>> 12);
872 control
|= SAA7134_RS_CONTROL_BSWAP
;
874 control
|= SAA7134_RS_CONTROL_WSWAP
;
875 if (V4L2_FIELD_HAS_BOTH(dev
->field
)) {
877 saa_writel(SAA7134_RS_BA1(0),base
);
878 saa_writel(SAA7134_RS_BA2(0),base
+bpl
);
879 saa_writel(SAA7134_RS_PITCH(0),bpl
*2);
882 saa_writel(SAA7134_RS_BA1(0),base
);
883 saa_writel(SAA7134_RS_BA2(0),base
);
884 saa_writel(SAA7134_RS_PITCH(0),bpl
);
886 saa_writel(SAA7134_RS_CONTROL(0),control
);
888 if (dev
->fmt
->planar
) {
889 /* DMA: setup channel 4+5 (= planar task A) */
890 bpl_uv
= bpl
>> dev
->fmt
->hshift
;
891 lines_uv
= dev
->height
>> dev
->fmt
->vshift
;
892 base2
= base
+ bpl
* dev
->height
;
893 base3
= base2
+ bpl_uv
* lines_uv
;
894 if (dev
->fmt
->uvswap
)
895 tmp
= base2
, base2
= base3
, base3
= tmp
;
896 video_dbg("uv: bpl=%ld lines=%ld base2/3=%ld/%ld\n",
897 bpl_uv
,lines_uv
,base2
,base3
);
898 if (V4L2_FIELD_HAS_BOTH(dev
->field
)) {
900 saa_writel(SAA7134_RS_BA1(4),base2
);
901 saa_writel(SAA7134_RS_BA2(4),base2
+bpl_uv
);
902 saa_writel(SAA7134_RS_PITCH(4),bpl_uv
*2);
903 saa_writel(SAA7134_RS_BA1(5),base3
);
904 saa_writel(SAA7134_RS_BA2(5),base3
+bpl_uv
);
905 saa_writel(SAA7134_RS_PITCH(5),bpl_uv
*2);
908 saa_writel(SAA7134_RS_BA1(4),base2
);
909 saa_writel(SAA7134_RS_BA2(4),base2
);
910 saa_writel(SAA7134_RS_PITCH(4),bpl_uv
);
911 saa_writel(SAA7134_RS_BA1(5),base3
);
912 saa_writel(SAA7134_RS_BA2(5),base3
);
913 saa_writel(SAA7134_RS_PITCH(5),bpl_uv
);
915 saa_writel(SAA7134_RS_CONTROL(4),control
);
916 saa_writel(SAA7134_RS_CONTROL(5),control
);
920 saa7134_set_dmabits(dev
);
921 mod_timer(&dmaq
->timeout
, jiffies
+ BUFFER_TIMEOUT
);
925 static int buffer_init(struct vb2_buffer
*vb2
)
927 struct saa7134_dmaqueue
*dmaq
= vb2
->vb2_queue
->drv_priv
;
928 struct vb2_v4l2_buffer
*vbuf
= to_vb2_v4l2_buffer(vb2
);
929 struct saa7134_buf
*buf
= container_of(vbuf
, struct saa7134_buf
, vb2
);
932 buf
->activate
= buffer_activate
;
936 static int buffer_prepare(struct vb2_buffer
*vb2
)
938 struct saa7134_dmaqueue
*dmaq
= vb2
->vb2_queue
->drv_priv
;
939 struct saa7134_dev
*dev
= dmaq
->dev
;
940 struct vb2_v4l2_buffer
*vbuf
= to_vb2_v4l2_buffer(vb2
);
941 struct saa7134_buf
*buf
= container_of(vbuf
, struct saa7134_buf
, vb2
);
942 struct sg_table
*dma
= vb2_dma_sg_plane_desc(vb2
, 0);
945 if (dma
->sgl
->offset
) {
946 pr_err("The buffer is not page-aligned\n");
949 size
= (dev
->width
* dev
->height
* dev
->fmt
->depth
) >> 3;
950 if (vb2_plane_size(vb2
, 0) < size
)
953 vb2_set_plane_payload(vb2
, 0, size
);
954 vbuf
->field
= dev
->field
;
956 return saa7134_pgtable_build(dev
->pci
, &dmaq
->pt
, dma
->sgl
, dma
->nents
,
957 saa7134_buffer_startpage(buf
));
960 static int queue_setup(struct vb2_queue
*q
,
961 unsigned int *nbuffers
, unsigned int *nplanes
,
962 unsigned int sizes
[], struct device
*alloc_devs
[])
964 struct saa7134_dmaqueue
*dmaq
= q
->drv_priv
;
965 struct saa7134_dev
*dev
= dmaq
->dev
;
966 int size
= dev
->fmt
->depth
* dev
->width
* dev
->height
>> 3;
968 if (dev
->width
< 48 ||
970 dev
->width
/4 > dev
->crop_current
.width
||
971 dev
->height
/4 > dev
->crop_current
.height
||
972 dev
->width
> dev
->crop_bounds
.width
||
973 dev
->height
> dev
->crop_bounds
.height
)
976 *nbuffers
= saa7134_buffer_count(size
, *nbuffers
);
980 saa7134_enable_analog_tuner(dev
);
986 * move buffer to hardware queue
988 void saa7134_vb2_buffer_queue(struct vb2_buffer
*vb
)
990 struct saa7134_dmaqueue
*dmaq
= vb
->vb2_queue
->drv_priv
;
991 struct saa7134_dev
*dev
= dmaq
->dev
;
992 struct vb2_v4l2_buffer
*vbuf
= to_vb2_v4l2_buffer(vb
);
993 struct saa7134_buf
*buf
= container_of(vbuf
, struct saa7134_buf
, vb2
);
995 saa7134_buffer_queue(dev
, dmaq
, buf
);
997 EXPORT_SYMBOL_GPL(saa7134_vb2_buffer_queue
);
999 int saa7134_vb2_start_streaming(struct vb2_queue
*vq
, unsigned int count
)
1001 struct saa7134_dmaqueue
*dmaq
= vq
->drv_priv
;
1002 struct saa7134_dev
*dev
= dmaq
->dev
;
1005 * Planar video capture and TS share the same DMA channel,
1006 * so only one can be active at a time.
1008 if (card_is_empress(dev
) && vb2_is_busy(&dev
->empress_vbq
) &&
1009 dmaq
== &dev
->video_q
&& dev
->fmt
->planar
) {
1010 struct saa7134_buf
*buf
, *tmp
;
1012 list_for_each_entry_safe(buf
, tmp
, &dmaq
->queue
, entry
) {
1013 list_del(&buf
->entry
);
1014 vb2_buffer_done(&buf
->vb2
.vb2_buf
,
1015 VB2_BUF_STATE_QUEUED
);
1018 vb2_buffer_done(&dmaq
->curr
->vb2
.vb2_buf
,
1019 VB2_BUF_STATE_QUEUED
);
1025 /* The SAA7134 has a 1K FIFO; the datasheet suggests that when
1026 * configured conservatively, there's 22 usec of buffering for video.
1027 * We therefore request a DMA latency of 20 usec, giving us 2 usec of
1028 * margin in case the FIFO is configured differently to the datasheet.
1029 * Unfortunately, I lack register-level documentation to check the
1030 * Linux FIFO setup and confirm the perfect value.
1032 if ((dmaq
== &dev
->video_q
&& !vb2_is_streaming(&dev
->vbi_vbq
)) ||
1033 (dmaq
== &dev
->vbi_q
&& !vb2_is_streaming(&dev
->video_vbq
)))
1034 pm_qos_add_request(&dev
->qos_request
,
1035 PM_QOS_CPU_DMA_LATENCY
, 20);
1041 void saa7134_vb2_stop_streaming(struct vb2_queue
*vq
)
1043 struct saa7134_dmaqueue
*dmaq
= vq
->drv_priv
;
1044 struct saa7134_dev
*dev
= dmaq
->dev
;
1046 saa7134_stop_streaming(dev
, dmaq
);
1048 if ((dmaq
== &dev
->video_q
&& !vb2_is_streaming(&dev
->vbi_vbq
)) ||
1049 (dmaq
== &dev
->vbi_q
&& !vb2_is_streaming(&dev
->video_vbq
)))
1050 pm_qos_remove_request(&dev
->qos_request
);
1053 static const struct vb2_ops vb2_qops
= {
1054 .queue_setup
= queue_setup
,
1055 .buf_init
= buffer_init
,
1056 .buf_prepare
= buffer_prepare
,
1057 .buf_queue
= saa7134_vb2_buffer_queue
,
1058 .wait_prepare
= vb2_ops_wait_prepare
,
1059 .wait_finish
= vb2_ops_wait_finish
,
1060 .start_streaming
= saa7134_vb2_start_streaming
,
1061 .stop_streaming
= saa7134_vb2_stop_streaming
,
1064 /* ------------------------------------------------------------------ */
1066 static int saa7134_s_ctrl(struct v4l2_ctrl
*ctrl
)
1068 struct saa7134_dev
*dev
= container_of(ctrl
->handler
, struct saa7134_dev
, ctrl_handler
);
1069 unsigned long flags
;
1070 int restart_overlay
= 0;
1073 case V4L2_CID_BRIGHTNESS
:
1074 dev
->ctl_bright
= ctrl
->val
;
1075 saa_writeb(SAA7134_DEC_LUMA_BRIGHT
, ctrl
->val
);
1078 dev
->ctl_hue
= ctrl
->val
;
1079 saa_writeb(SAA7134_DEC_CHROMA_HUE
, ctrl
->val
);
1081 case V4L2_CID_CONTRAST
:
1082 dev
->ctl_contrast
= ctrl
->val
;
1083 saa_writeb(SAA7134_DEC_LUMA_CONTRAST
,
1084 dev
->ctl_invert
? -dev
->ctl_contrast
: dev
->ctl_contrast
);
1086 case V4L2_CID_SATURATION
:
1087 dev
->ctl_saturation
= ctrl
->val
;
1088 saa_writeb(SAA7134_DEC_CHROMA_SATURATION
,
1089 dev
->ctl_invert
? -dev
->ctl_saturation
: dev
->ctl_saturation
);
1091 case V4L2_CID_AUDIO_MUTE
:
1092 dev
->ctl_mute
= ctrl
->val
;
1093 saa7134_tvaudio_setmute(dev
);
1095 case V4L2_CID_AUDIO_VOLUME
:
1096 dev
->ctl_volume
= ctrl
->val
;
1097 saa7134_tvaudio_setvolume(dev
,dev
->ctl_volume
);
1099 case V4L2_CID_PRIVATE_INVERT
:
1100 dev
->ctl_invert
= ctrl
->val
;
1101 saa_writeb(SAA7134_DEC_LUMA_CONTRAST
,
1102 dev
->ctl_invert
? -dev
->ctl_contrast
: dev
->ctl_contrast
);
1103 saa_writeb(SAA7134_DEC_CHROMA_SATURATION
,
1104 dev
->ctl_invert
? -dev
->ctl_saturation
: dev
->ctl_saturation
);
1106 case V4L2_CID_HFLIP
:
1107 dev
->ctl_mirror
= ctrl
->val
;
1108 restart_overlay
= 1;
1110 case V4L2_CID_PRIVATE_Y_EVEN
:
1111 dev
->ctl_y_even
= ctrl
->val
;
1112 restart_overlay
= 1;
1114 case V4L2_CID_PRIVATE_Y_ODD
:
1115 dev
->ctl_y_odd
= ctrl
->val
;
1116 restart_overlay
= 1;
1118 case V4L2_CID_PRIVATE_AUTOMUTE
:
1120 struct v4l2_priv_tun_config tda9887_cfg
;
1122 tda9887_cfg
.tuner
= TUNER_TDA9887
;
1123 tda9887_cfg
.priv
= &dev
->tda9887_conf
;
1125 dev
->ctl_automute
= ctrl
->val
;
1126 if (dev
->tda9887_conf
) {
1127 if (dev
->ctl_automute
)
1128 dev
->tda9887_conf
|= TDA9887_AUTOMUTE
;
1130 dev
->tda9887_conf
&= ~TDA9887_AUTOMUTE
;
1132 saa_call_all(dev
, tuner
, s_config
, &tda9887_cfg
);
1139 if (restart_overlay
&& dev
->overlay_owner
) {
1140 spin_lock_irqsave(&dev
->slock
, flags
);
1143 spin_unlock_irqrestore(&dev
->slock
, flags
);
1148 /* ------------------------------------------------------------------ */
1150 static int video_open(struct file
*file
)
1152 struct video_device
*vdev
= video_devdata(file
);
1153 struct saa7134_dev
*dev
= video_drvdata(file
);
1154 int ret
= v4l2_fh_open(file
);
1159 mutex_lock(&dev
->lock
);
1160 if (vdev
->vfl_type
== VFL_TYPE_RADIO
) {
1161 /* switch to radio mode */
1162 saa7134_tvaudio_setinput(dev
, &card(dev
).radio
);
1163 saa_call_all(dev
, tuner
, s_radio
);
1165 /* switch to video/vbi mode */
1166 video_mux(dev
, dev
->ctl_input
);
1168 mutex_unlock(&dev
->lock
);
1173 static int video_release(struct file
*file
)
1175 struct video_device
*vdev
= video_devdata(file
);
1176 struct saa7134_dev
*dev
= video_drvdata(file
);
1177 struct v4l2_fh
*fh
= file
->private_data
;
1178 struct saa6588_command cmd
;
1179 unsigned long flags
;
1181 mutex_lock(&dev
->lock
);
1182 saa7134_tvaudio_close(dev
);
1184 /* turn off overlay */
1185 if (fh
== dev
->overlay_owner
) {
1186 spin_lock_irqsave(&dev
->slock
,flags
);
1188 spin_unlock_irqrestore(&dev
->slock
,flags
);
1189 dev
->overlay_owner
= NULL
;
1192 if (vdev
->vfl_type
== VFL_TYPE_RADIO
)
1193 v4l2_fh_release(file
);
1195 _vb2_fop_release(file
, NULL
);
1197 /* ts-capture will not work in planar mode, so turn it off Hac: 04.05*/
1198 saa_andorb(SAA7134_OFMT_VIDEO_A
, 0x1f, 0);
1199 saa_andorb(SAA7134_OFMT_VIDEO_B
, 0x1f, 0);
1200 saa_andorb(SAA7134_OFMT_DATA_A
, 0x1f, 0);
1201 saa_andorb(SAA7134_OFMT_DATA_B
, 0x1f, 0);
1203 saa_call_all(dev
, core
, s_power
, 0);
1204 if (vdev
->vfl_type
== VFL_TYPE_RADIO
)
1205 saa_call_all(dev
, core
, ioctl
, SAA6588_CMD_CLOSE
, &cmd
);
1206 mutex_unlock(&dev
->lock
);
1211 static ssize_t
radio_read(struct file
*file
, char __user
*data
,
1212 size_t count
, loff_t
*ppos
)
1214 struct saa7134_dev
*dev
= video_drvdata(file
);
1215 struct saa6588_command cmd
;
1217 cmd
.block_count
= count
/3;
1218 cmd
.nonblocking
= file
->f_flags
& O_NONBLOCK
;
1220 cmd
.instance
= file
;
1221 cmd
.result
= -ENODEV
;
1223 mutex_lock(&dev
->lock
);
1224 saa_call_all(dev
, core
, ioctl
, SAA6588_CMD_READ
, &cmd
);
1225 mutex_unlock(&dev
->lock
);
1230 static __poll_t
radio_poll(struct file
*file
, poll_table
*wait
)
1232 struct saa7134_dev
*dev
= video_drvdata(file
);
1233 struct saa6588_command cmd
;
1234 __poll_t rc
= v4l2_ctrl_poll(file
, wait
);
1236 cmd
.instance
= file
;
1237 cmd
.event_list
= wait
;
1239 mutex_lock(&dev
->lock
);
1240 saa_call_all(dev
, core
, ioctl
, SAA6588_CMD_POLL
, &cmd
);
1241 mutex_unlock(&dev
->lock
);
1243 return rc
| cmd
.result
;
1246 /* ------------------------------------------------------------------ */
1248 static int saa7134_try_get_set_fmt_vbi_cap(struct file
*file
, void *priv
,
1249 struct v4l2_format
*f
)
1251 struct saa7134_dev
*dev
= video_drvdata(file
);
1252 struct saa7134_tvnorm
*norm
= dev
->tvnorm
;
1254 memset(&f
->fmt
.vbi
.reserved
, 0, sizeof(f
->fmt
.vbi
.reserved
));
1255 f
->fmt
.vbi
.sampling_rate
= 6750000 * 4;
1256 f
->fmt
.vbi
.samples_per_line
= 2048 /* VBI_LINE_LENGTH */;
1257 f
->fmt
.vbi
.sample_format
= V4L2_PIX_FMT_GREY
;
1258 f
->fmt
.vbi
.offset
= 64 * 4;
1259 f
->fmt
.vbi
.start
[0] = norm
->vbi_v_start_0
;
1260 f
->fmt
.vbi
.count
[0] = norm
->vbi_v_stop_0
- norm
->vbi_v_start_0
+1;
1261 f
->fmt
.vbi
.start
[1] = norm
->vbi_v_start_1
;
1262 f
->fmt
.vbi
.count
[1] = f
->fmt
.vbi
.count
[0];
1263 f
->fmt
.vbi
.flags
= 0; /* VBI_UNSYNC VBI_INTERLACED */
1268 static int saa7134_g_fmt_vid_cap(struct file
*file
, void *priv
,
1269 struct v4l2_format
*f
)
1271 struct saa7134_dev
*dev
= video_drvdata(file
);
1273 f
->fmt
.pix
.width
= dev
->width
;
1274 f
->fmt
.pix
.height
= dev
->height
;
1275 f
->fmt
.pix
.field
= dev
->field
;
1276 f
->fmt
.pix
.pixelformat
= dev
->fmt
->fourcc
;
1277 if (dev
->fmt
->planar
)
1278 f
->fmt
.pix
.bytesperline
= f
->fmt
.pix
.width
;
1280 f
->fmt
.pix
.bytesperline
=
1281 (f
->fmt
.pix
.width
* dev
->fmt
->depth
) / 8;
1282 f
->fmt
.pix
.sizeimage
=
1283 (f
->fmt
.pix
.height
* f
->fmt
.pix
.width
* dev
->fmt
->depth
) / 8;
1284 f
->fmt
.pix
.colorspace
= V4L2_COLORSPACE_SMPTE170M
;
1288 static int saa7134_g_fmt_vid_overlay(struct file
*file
, void *priv
,
1289 struct v4l2_format
*f
)
1291 struct saa7134_dev
*dev
= video_drvdata(file
);
1292 struct v4l2_clip __user
*clips
= f
->fmt
.win
.clips
;
1293 u32 clipcount
= f
->fmt
.win
.clipcount
;
1297 if (saa7134_no_overlay
> 0) {
1298 pr_err("V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
1301 f
->fmt
.win
= dev
->win
;
1302 f
->fmt
.win
.clips
= clips
;
1305 if (dev
->nclips
< clipcount
)
1306 clipcount
= dev
->nclips
;
1307 f
->fmt
.win
.clipcount
= clipcount
;
1309 for (i
= 0; !err
&& i
< clipcount
; i
++) {
1310 if (copy_to_user(&f
->fmt
.win
.clips
[i
].c
, &dev
->clips
[i
].c
,
1311 sizeof(struct v4l2_rect
)))
1318 static int saa7134_try_fmt_vid_cap(struct file
*file
, void *priv
,
1319 struct v4l2_format
*f
)
1321 struct saa7134_dev
*dev
= video_drvdata(file
);
1322 struct saa7134_format
*fmt
;
1323 enum v4l2_field field
;
1324 unsigned int maxw
, maxh
;
1326 fmt
= format_by_fourcc(f
->fmt
.pix
.pixelformat
);
1330 field
= f
->fmt
.pix
.field
;
1331 maxw
= min(dev
->crop_current
.width
*4, dev
->crop_bounds
.width
);
1332 maxh
= min(dev
->crop_current
.height
*4, dev
->crop_bounds
.height
);
1334 if (V4L2_FIELD_ANY
== field
) {
1335 field
= (f
->fmt
.pix
.height
> maxh
/2)
1336 ? V4L2_FIELD_INTERLACED
1337 : V4L2_FIELD_BOTTOM
;
1340 case V4L2_FIELD_TOP
:
1341 case V4L2_FIELD_BOTTOM
:
1345 field
= V4L2_FIELD_INTERLACED
;
1349 f
->fmt
.pix
.field
= field
;
1350 if (f
->fmt
.pix
.width
< 48)
1351 f
->fmt
.pix
.width
= 48;
1352 if (f
->fmt
.pix
.height
< 32)
1353 f
->fmt
.pix
.height
= 32;
1354 if (f
->fmt
.pix
.width
> maxw
)
1355 f
->fmt
.pix
.width
= maxw
;
1356 if (f
->fmt
.pix
.height
> maxh
)
1357 f
->fmt
.pix
.height
= maxh
;
1358 f
->fmt
.pix
.width
&= ~0x03;
1360 f
->fmt
.pix
.bytesperline
= f
->fmt
.pix
.width
;
1362 f
->fmt
.pix
.bytesperline
=
1363 (f
->fmt
.pix
.width
* fmt
->depth
) / 8;
1364 f
->fmt
.pix
.sizeimage
=
1365 (f
->fmt
.pix
.height
* f
->fmt
.pix
.width
* fmt
->depth
) / 8;
1366 f
->fmt
.pix
.colorspace
= V4L2_COLORSPACE_SMPTE170M
;
1371 static int saa7134_try_fmt_vid_overlay(struct file
*file
, void *priv
,
1372 struct v4l2_format
*f
)
1374 struct saa7134_dev
*dev
= video_drvdata(file
);
1376 if (saa7134_no_overlay
> 0) {
1377 pr_err("V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
1381 if (f
->fmt
.win
.clips
== NULL
)
1382 f
->fmt
.win
.clipcount
= 0;
1383 return verify_preview(dev
, &f
->fmt
.win
, true);
1386 static int saa7134_s_fmt_vid_cap(struct file
*file
, void *priv
,
1387 struct v4l2_format
*f
)
1389 struct saa7134_dev
*dev
= video_drvdata(file
);
1392 err
= saa7134_try_fmt_vid_cap(file
, priv
, f
);
1396 dev
->fmt
= format_by_fourcc(f
->fmt
.pix
.pixelformat
);
1397 dev
->width
= f
->fmt
.pix
.width
;
1398 dev
->height
= f
->fmt
.pix
.height
;
1399 dev
->field
= f
->fmt
.pix
.field
;
1403 static int saa7134_s_fmt_vid_overlay(struct file
*file
, void *priv
,
1404 struct v4l2_format
*f
)
1406 struct saa7134_dev
*dev
= video_drvdata(file
);
1408 unsigned long flags
;
1410 if (saa7134_no_overlay
> 0) {
1411 pr_err("V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
1414 if (f
->fmt
.win
.clips
== NULL
)
1415 f
->fmt
.win
.clipcount
= 0;
1416 err
= verify_preview(dev
, &f
->fmt
.win
, true);
1420 dev
->win
= f
->fmt
.win
;
1421 dev
->nclips
= f
->fmt
.win
.clipcount
;
1423 if (copy_from_user(dev
->clips
, f
->fmt
.win
.clips
,
1424 sizeof(struct v4l2_clip
) * dev
->nclips
))
1427 if (priv
== dev
->overlay_owner
) {
1428 spin_lock_irqsave(&dev
->slock
, flags
);
1431 spin_unlock_irqrestore(&dev
->slock
, flags
);
1437 int saa7134_enum_input(struct file
*file
, void *priv
, struct v4l2_input
*i
)
1439 struct saa7134_dev
*dev
= video_drvdata(file
);
1443 if (n
>= SAA7134_INPUT_MAX
)
1445 if (card_in(dev
, i
->index
).type
== SAA7134_NO_INPUT
)
1448 strcpy(i
->name
, saa7134_input_name
[card_in(dev
, n
).type
]);
1449 switch (card_in(dev
, n
).type
) {
1450 case SAA7134_INPUT_TV
:
1451 case SAA7134_INPUT_TV_MONO
:
1452 i
->type
= V4L2_INPUT_TYPE_TUNER
;
1455 i
->type
= V4L2_INPUT_TYPE_CAMERA
;
1458 if (n
== dev
->ctl_input
) {
1459 int v1
= saa_readb(SAA7134_STATUS_VIDEO1
);
1460 int v2
= saa_readb(SAA7134_STATUS_VIDEO2
);
1462 if (0 != (v1
& 0x40))
1463 i
->status
|= V4L2_IN_ST_NO_H_LOCK
;
1464 if (0 != (v2
& 0x40))
1465 i
->status
|= V4L2_IN_ST_NO_SIGNAL
;
1466 if (0 != (v2
& 0x0e))
1467 i
->status
|= V4L2_IN_ST_MACROVISION
;
1469 i
->std
= SAA7134_NORMS
;
1472 EXPORT_SYMBOL_GPL(saa7134_enum_input
);
1474 int saa7134_g_input(struct file
*file
, void *priv
, unsigned int *i
)
1476 struct saa7134_dev
*dev
= video_drvdata(file
);
1478 *i
= dev
->ctl_input
;
1481 EXPORT_SYMBOL_GPL(saa7134_g_input
);
1483 int saa7134_s_input(struct file
*file
, void *priv
, unsigned int i
)
1485 struct saa7134_dev
*dev
= video_drvdata(file
);
1487 if (i
>= SAA7134_INPUT_MAX
)
1489 if (card_in(dev
, i
).type
== SAA7134_NO_INPUT
)
1494 EXPORT_SYMBOL_GPL(saa7134_s_input
);
1496 int saa7134_querycap(struct file
*file
, void *priv
,
1497 struct v4l2_capability
*cap
)
1499 struct saa7134_dev
*dev
= video_drvdata(file
);
1500 struct video_device
*vdev
= video_devdata(file
);
1501 u32 radio_caps
, video_caps
, vbi_caps
;
1503 unsigned int tuner_type
= dev
->tuner_type
;
1505 strcpy(cap
->driver
, "saa7134");
1506 strlcpy(cap
->card
, saa7134_boards
[dev
->board
].name
,
1508 sprintf(cap
->bus_info
, "PCI:%s", pci_name(dev
->pci
));
1510 cap
->device_caps
= V4L2_CAP_READWRITE
| V4L2_CAP_STREAMING
;
1511 if ((tuner_type
!= TUNER_ABSENT
) && (tuner_type
!= UNSET
))
1512 cap
->device_caps
|= V4L2_CAP_TUNER
;
1514 radio_caps
= V4L2_CAP_RADIO
;
1516 radio_caps
|= V4L2_CAP_RDS_CAPTURE
;
1518 video_caps
= V4L2_CAP_VIDEO_CAPTURE
;
1519 if (saa7134_no_overlay
<= 0 && !is_empress(file
))
1520 video_caps
|= V4L2_CAP_VIDEO_OVERLAY
;
1522 vbi_caps
= V4L2_CAP_VBI_CAPTURE
;
1524 switch (vdev
->vfl_type
) {
1525 case VFL_TYPE_RADIO
:
1526 cap
->device_caps
|= radio_caps
;
1528 case VFL_TYPE_GRABBER
:
1529 cap
->device_caps
|= video_caps
;
1532 cap
->device_caps
|= vbi_caps
;
1537 cap
->capabilities
= radio_caps
| video_caps
| vbi_caps
|
1538 cap
->device_caps
| V4L2_CAP_DEVICE_CAPS
;
1539 if (vdev
->vfl_type
== VFL_TYPE_RADIO
) {
1540 cap
->device_caps
&= ~V4L2_CAP_STREAMING
;
1542 cap
->device_caps
&= ~V4L2_CAP_READWRITE
;
1547 EXPORT_SYMBOL_GPL(saa7134_querycap
);
1549 int saa7134_s_std(struct file
*file
, void *priv
, v4l2_std_id id
)
1551 struct saa7134_dev
*dev
= video_drvdata(file
);
1552 struct v4l2_fh
*fh
= priv
;
1553 unsigned long flags
;
1557 if (is_empress(file
) && dev
->overlay_owner
) {
1558 /* Don't change the std from the mpeg device
1559 if overlay is active. */
1563 for (i
= 0; i
< TVNORMS
; i
++)
1564 if (id
== tvnorms
[i
].id
)
1568 for (i
= 0; i
< TVNORMS
; i
++)
1569 if (id
& tvnorms
[i
].id
)
1574 if ((id
& V4L2_STD_SECAM
) && (secam
[0] != '-')) {
1575 if (secam
[0] == 'L' || secam
[0] == 'l') {
1576 if (secam
[1] == 'C' || secam
[1] == 'c')
1577 fixup
= V4L2_STD_SECAM_LC
;
1579 fixup
= V4L2_STD_SECAM_L
;
1581 if (secam
[0] == 'D' || secam
[0] == 'd')
1582 fixup
= V4L2_STD_SECAM_DK
;
1584 fixup
= V4L2_STD_SECAM
;
1586 for (i
= 0; i
< TVNORMS
; i
++) {
1587 if (fixup
== tvnorms
[i
].id
)
1596 if (!is_empress(file
) && fh
== dev
->overlay_owner
) {
1597 spin_lock_irqsave(&dev
->slock
, flags
);
1599 spin_unlock_irqrestore(&dev
->slock
, flags
);
1601 set_tvnorm(dev
, &tvnorms
[i
]);
1603 spin_lock_irqsave(&dev
->slock
, flags
);
1605 spin_unlock_irqrestore(&dev
->slock
, flags
);
1607 set_tvnorm(dev
, &tvnorms
[i
]);
1609 saa7134_tvaudio_do_scan(dev
);
1612 EXPORT_SYMBOL_GPL(saa7134_s_std
);
1614 int saa7134_g_std(struct file
*file
, void *priv
, v4l2_std_id
*id
)
1616 struct saa7134_dev
*dev
= video_drvdata(file
);
1618 *id
= dev
->tvnorm
->id
;
1621 EXPORT_SYMBOL_GPL(saa7134_g_std
);
1623 static v4l2_std_id
saa7134_read_std(struct saa7134_dev
*dev
)
1625 static v4l2_std_id stds
[] = {
1631 v4l2_std_id result
= 0;
1633 u8 st1
= saa_readb(SAA7134_STATUS_VIDEO1
);
1634 u8 st2
= saa_readb(SAA7134_STATUS_VIDEO2
);
1636 if (!(st2
& 0x1)) /* RDCAP == 0 */
1637 result
= V4L2_STD_UNKNOWN
;
1639 result
= stds
[st1
& 0x03];
1644 int saa7134_querystd(struct file
*file
, void *priv
, v4l2_std_id
*std
)
1646 struct saa7134_dev
*dev
= video_drvdata(file
);
1647 *std
&= saa7134_read_std(dev
);
1650 EXPORT_SYMBOL_GPL(saa7134_querystd
);
1652 static int saa7134_cropcap(struct file
*file
, void *priv
,
1653 struct v4l2_cropcap
*cap
)
1655 struct saa7134_dev
*dev
= video_drvdata(file
);
1657 if (cap
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
&&
1658 cap
->type
!= V4L2_BUF_TYPE_VIDEO_OVERLAY
)
1660 cap
->pixelaspect
.numerator
= 1;
1661 cap
->pixelaspect
.denominator
= 1;
1662 if (dev
->tvnorm
->id
& V4L2_STD_525_60
) {
1663 cap
->pixelaspect
.numerator
= 11;
1664 cap
->pixelaspect
.denominator
= 10;
1666 if (dev
->tvnorm
->id
& V4L2_STD_625_50
) {
1667 cap
->pixelaspect
.numerator
= 54;
1668 cap
->pixelaspect
.denominator
= 59;
1673 static int saa7134_g_selection(struct file
*file
, void *f
, struct v4l2_selection
*sel
)
1675 struct saa7134_dev
*dev
= video_drvdata(file
);
1677 if (sel
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
&&
1678 sel
->type
!= V4L2_BUF_TYPE_VIDEO_OVERLAY
)
1681 switch (sel
->target
) {
1682 case V4L2_SEL_TGT_CROP
:
1683 sel
->r
= dev
->crop_current
;
1685 case V4L2_SEL_TGT_CROP_DEFAULT
:
1686 sel
->r
= dev
->crop_defrect
;
1688 case V4L2_SEL_TGT_CROP_BOUNDS
:
1689 sel
->r
= dev
->crop_bounds
;
1697 static int saa7134_s_selection(struct file
*file
, void *f
, struct v4l2_selection
*sel
)
1699 struct saa7134_dev
*dev
= video_drvdata(file
);
1700 struct v4l2_rect
*b
= &dev
->crop_bounds
;
1701 struct v4l2_rect
*c
= &dev
->crop_current
;
1703 if (sel
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
&&
1704 sel
->type
!= V4L2_BUF_TYPE_VIDEO_OVERLAY
)
1707 if (sel
->target
!= V4L2_SEL_TGT_CROP
)
1710 if (dev
->overlay_owner
)
1712 if (vb2_is_streaming(&dev
->video_vbq
))
1716 if (c
->top
< b
->top
)
1718 if (c
->top
> b
->top
+ b
->height
)
1719 c
->top
= b
->top
+ b
->height
;
1720 if (c
->height
> b
->top
- c
->top
+ b
->height
)
1721 c
->height
= b
->top
- c
->top
+ b
->height
;
1723 if (c
->left
< b
->left
)
1725 if (c
->left
> b
->left
+ b
->width
)
1726 c
->left
= b
->left
+ b
->width
;
1727 if (c
->width
> b
->left
- c
->left
+ b
->width
)
1728 c
->width
= b
->left
- c
->left
+ b
->width
;
1733 int saa7134_g_tuner(struct file
*file
, void *priv
,
1734 struct v4l2_tuner
*t
)
1736 struct saa7134_dev
*dev
= video_drvdata(file
);
1741 memset(t
, 0, sizeof(*t
));
1742 for (n
= 0; n
< SAA7134_INPUT_MAX
; n
++) {
1743 if (card_in(dev
, n
).type
== SAA7134_INPUT_TV
||
1744 card_in(dev
, n
).type
== SAA7134_INPUT_TV_MONO
)
1747 if (n
== SAA7134_INPUT_MAX
)
1749 if (card_in(dev
, n
).type
!= SAA7134_NO_INPUT
) {
1750 strcpy(t
->name
, "Television");
1751 t
->type
= V4L2_TUNER_ANALOG_TV
;
1752 saa_call_all(dev
, tuner
, g_tuner
, t
);
1753 t
->capability
= V4L2_TUNER_CAP_NORM
|
1754 V4L2_TUNER_CAP_STEREO
|
1755 V4L2_TUNER_CAP_LANG1
|
1756 V4L2_TUNER_CAP_LANG2
;
1757 t
->rxsubchans
= saa7134_tvaudio_getstereo(dev
);
1758 t
->audmode
= saa7134_tvaudio_rx2mode(t
->rxsubchans
);
1760 if (0 != (saa_readb(SAA7134_STATUS_VIDEO1
) & 0x03))
1764 EXPORT_SYMBOL_GPL(saa7134_g_tuner
);
1766 int saa7134_s_tuner(struct file
*file
, void *priv
,
1767 const struct v4l2_tuner
*t
)
1769 struct saa7134_dev
*dev
= video_drvdata(file
);
1775 mode
= dev
->thread
.mode
;
1776 if (UNSET
== mode
) {
1777 rx
= saa7134_tvaudio_getstereo(dev
);
1778 mode
= saa7134_tvaudio_rx2mode(rx
);
1780 if (mode
!= t
->audmode
)
1781 dev
->thread
.mode
= t
->audmode
;
1785 EXPORT_SYMBOL_GPL(saa7134_s_tuner
);
1787 int saa7134_g_frequency(struct file
*file
, void *priv
,
1788 struct v4l2_frequency
*f
)
1790 struct saa7134_dev
*dev
= video_drvdata(file
);
1795 saa_call_all(dev
, tuner
, g_frequency
, f
);
1799 EXPORT_SYMBOL_GPL(saa7134_g_frequency
);
1801 int saa7134_s_frequency(struct file
*file
, void *priv
,
1802 const struct v4l2_frequency
*f
)
1804 struct saa7134_dev
*dev
= video_drvdata(file
);
1809 saa_call_all(dev
, tuner
, s_frequency
, f
);
1811 saa7134_tvaudio_do_scan(dev
);
1814 EXPORT_SYMBOL_GPL(saa7134_s_frequency
);
1816 static int saa7134_enum_fmt_vid_cap(struct file
*file
, void *priv
,
1817 struct v4l2_fmtdesc
*f
)
1819 if (f
->index
>= FORMATS
)
1822 strlcpy(f
->description
, formats
[f
->index
].name
,
1823 sizeof(f
->description
));
1825 f
->pixelformat
= formats
[f
->index
].fourcc
;
1830 static int saa7134_enum_fmt_vid_overlay(struct file
*file
, void *priv
,
1831 struct v4l2_fmtdesc
*f
)
1833 if (saa7134_no_overlay
> 0) {
1834 pr_err("V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
1838 if ((f
->index
>= FORMATS
) || formats
[f
->index
].planar
)
1841 strlcpy(f
->description
, formats
[f
->index
].name
,
1842 sizeof(f
->description
));
1844 f
->pixelformat
= formats
[f
->index
].fourcc
;
1849 static int saa7134_g_fbuf(struct file
*file
, void *f
,
1850 struct v4l2_framebuffer
*fb
)
1852 struct saa7134_dev
*dev
= video_drvdata(file
);
1855 fb
->capability
= V4L2_FBUF_CAP_LIST_CLIPPING
;
1860 static int saa7134_s_fbuf(struct file
*file
, void *f
,
1861 const struct v4l2_framebuffer
*fb
)
1863 struct saa7134_dev
*dev
= video_drvdata(file
);
1864 struct saa7134_format
*fmt
;
1866 if (!capable(CAP_SYS_ADMIN
) &&
1867 !capable(CAP_SYS_RAWIO
))
1871 fmt
= format_by_fourcc(fb
->fmt
.pixelformat
);
1878 if (0 == dev
->ovbuf
.fmt
.bytesperline
)
1879 dev
->ovbuf
.fmt
.bytesperline
=
1880 dev
->ovbuf
.fmt
.width
*fmt
->depth
/8;
1884 static int saa7134_overlay(struct file
*file
, void *priv
, unsigned int on
)
1886 struct saa7134_dev
*dev
= video_drvdata(file
);
1887 unsigned long flags
;
1890 if (saa7134_no_overlay
> 0) {
1891 video_dbg("no_overlay\n");
1895 if (dev
->overlay_owner
&& priv
!= dev
->overlay_owner
)
1897 dev
->overlay_owner
= priv
;
1898 spin_lock_irqsave(&dev
->slock
, flags
);
1900 spin_unlock_irqrestore(&dev
->slock
, flags
);
1903 if (priv
!= dev
->overlay_owner
)
1905 spin_lock_irqsave(&dev
->slock
, flags
);
1907 spin_unlock_irqrestore(&dev
->slock
, flags
);
1908 dev
->overlay_owner
= NULL
;
1913 #ifdef CONFIG_VIDEO_ADV_DEBUG
1914 static int vidioc_g_register (struct file
*file
, void *priv
,
1915 struct v4l2_dbg_register
*reg
)
1917 struct saa7134_dev
*dev
= video_drvdata(file
);
1919 reg
->val
= saa_readb(reg
->reg
& 0xffffff);
1924 static int vidioc_s_register (struct file
*file
, void *priv
,
1925 const struct v4l2_dbg_register
*reg
)
1927 struct saa7134_dev
*dev
= video_drvdata(file
);
1929 saa_writeb(reg
->reg
& 0xffffff, reg
->val
);
1934 static int radio_g_tuner(struct file
*file
, void *priv
,
1935 struct v4l2_tuner
*t
)
1937 struct saa7134_dev
*dev
= video_drvdata(file
);
1942 strcpy(t
->name
, "Radio");
1944 saa_call_all(dev
, tuner
, g_tuner
, t
);
1945 t
->audmode
&= V4L2_TUNER_MODE_MONO
| V4L2_TUNER_MODE_STEREO
;
1946 if (dev
->input
->amux
== TV
) {
1947 t
->signal
= 0xf800 - ((saa_readb(0x581) & 0x1f) << 11);
1948 t
->rxsubchans
= (saa_readb(0x529) & 0x08) ?
1949 V4L2_TUNER_SUB_STEREO
: V4L2_TUNER_SUB_MONO
;
1953 static int radio_s_tuner(struct file
*file
, void *priv
,
1954 const struct v4l2_tuner
*t
)
1956 struct saa7134_dev
*dev
= video_drvdata(file
);
1961 saa_call_all(dev
, tuner
, s_tuner
, t
);
1965 static const struct v4l2_file_operations video_fops
=
1967 .owner
= THIS_MODULE
,
1969 .release
= video_release
,
1970 .read
= vb2_fop_read
,
1971 .poll
= vb2_fop_poll
,
1972 .mmap
= vb2_fop_mmap
,
1973 .unlocked_ioctl
= video_ioctl2
,
1976 static const struct v4l2_ioctl_ops video_ioctl_ops
= {
1977 .vidioc_querycap
= saa7134_querycap
,
1978 .vidioc_enum_fmt_vid_cap
= saa7134_enum_fmt_vid_cap
,
1979 .vidioc_g_fmt_vid_cap
= saa7134_g_fmt_vid_cap
,
1980 .vidioc_try_fmt_vid_cap
= saa7134_try_fmt_vid_cap
,
1981 .vidioc_s_fmt_vid_cap
= saa7134_s_fmt_vid_cap
,
1982 .vidioc_enum_fmt_vid_overlay
= saa7134_enum_fmt_vid_overlay
,
1983 .vidioc_g_fmt_vid_overlay
= saa7134_g_fmt_vid_overlay
,
1984 .vidioc_try_fmt_vid_overlay
= saa7134_try_fmt_vid_overlay
,
1985 .vidioc_s_fmt_vid_overlay
= saa7134_s_fmt_vid_overlay
,
1986 .vidioc_g_fmt_vbi_cap
= saa7134_try_get_set_fmt_vbi_cap
,
1987 .vidioc_try_fmt_vbi_cap
= saa7134_try_get_set_fmt_vbi_cap
,
1988 .vidioc_s_fmt_vbi_cap
= saa7134_try_get_set_fmt_vbi_cap
,
1989 .vidioc_cropcap
= saa7134_cropcap
,
1990 .vidioc_reqbufs
= vb2_ioctl_reqbufs
,
1991 .vidioc_querybuf
= vb2_ioctl_querybuf
,
1992 .vidioc_qbuf
= vb2_ioctl_qbuf
,
1993 .vidioc_dqbuf
= vb2_ioctl_dqbuf
,
1994 .vidioc_expbuf
= vb2_ioctl_expbuf
,
1995 .vidioc_s_std
= saa7134_s_std
,
1996 .vidioc_g_std
= saa7134_g_std
,
1997 .vidioc_querystd
= saa7134_querystd
,
1998 .vidioc_enum_input
= saa7134_enum_input
,
1999 .vidioc_g_input
= saa7134_g_input
,
2000 .vidioc_s_input
= saa7134_s_input
,
2001 .vidioc_streamon
= vb2_ioctl_streamon
,
2002 .vidioc_streamoff
= vb2_ioctl_streamoff
,
2003 .vidioc_g_tuner
= saa7134_g_tuner
,
2004 .vidioc_s_tuner
= saa7134_s_tuner
,
2005 .vidioc_g_selection
= saa7134_g_selection
,
2006 .vidioc_s_selection
= saa7134_s_selection
,
2007 .vidioc_g_fbuf
= saa7134_g_fbuf
,
2008 .vidioc_s_fbuf
= saa7134_s_fbuf
,
2009 .vidioc_overlay
= saa7134_overlay
,
2010 .vidioc_g_frequency
= saa7134_g_frequency
,
2011 .vidioc_s_frequency
= saa7134_s_frequency
,
2012 #ifdef CONFIG_VIDEO_ADV_DEBUG
2013 .vidioc_g_register
= vidioc_g_register
,
2014 .vidioc_s_register
= vidioc_s_register
,
2016 .vidioc_log_status
= v4l2_ctrl_log_status
,
2017 .vidioc_subscribe_event
= v4l2_ctrl_subscribe_event
,
2018 .vidioc_unsubscribe_event
= v4l2_event_unsubscribe
,
2021 static const struct v4l2_file_operations radio_fops
= {
2022 .owner
= THIS_MODULE
,
2025 .release
= video_release
,
2026 .unlocked_ioctl
= video_ioctl2
,
2030 static const struct v4l2_ioctl_ops radio_ioctl_ops
= {
2031 .vidioc_querycap
= saa7134_querycap
,
2032 .vidioc_g_tuner
= radio_g_tuner
,
2033 .vidioc_s_tuner
= radio_s_tuner
,
2034 .vidioc_g_frequency
= saa7134_g_frequency
,
2035 .vidioc_s_frequency
= saa7134_s_frequency
,
2036 .vidioc_subscribe_event
= v4l2_ctrl_subscribe_event
,
2037 .vidioc_unsubscribe_event
= v4l2_event_unsubscribe
,
2040 /* ----------------------------------------------------------- */
2041 /* exported stuff */
2043 struct video_device saa7134_video_template
= {
2044 .name
= "saa7134-video",
2045 .fops
= &video_fops
,
2046 .ioctl_ops
= &video_ioctl_ops
,
2047 .tvnorms
= SAA7134_NORMS
,
2050 struct video_device saa7134_radio_template
= {
2051 .name
= "saa7134-radio",
2052 .fops
= &radio_fops
,
2053 .ioctl_ops
= &radio_ioctl_ops
,
2056 static const struct v4l2_ctrl_ops saa7134_ctrl_ops
= {
2057 .s_ctrl
= saa7134_s_ctrl
,
2060 static const struct v4l2_ctrl_config saa7134_ctrl_invert
= {
2061 .ops
= &saa7134_ctrl_ops
,
2062 .id
= V4L2_CID_PRIVATE_INVERT
,
2064 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
2070 static const struct v4l2_ctrl_config saa7134_ctrl_y_odd
= {
2071 .ops
= &saa7134_ctrl_ops
,
2072 .id
= V4L2_CID_PRIVATE_Y_ODD
,
2073 .name
= "Y Offset Odd Field",
2074 .type
= V4L2_CTRL_TYPE_INTEGER
,
2080 static const struct v4l2_ctrl_config saa7134_ctrl_y_even
= {
2081 .ops
= &saa7134_ctrl_ops
,
2082 .id
= V4L2_CID_PRIVATE_Y_EVEN
,
2083 .name
= "Y Offset Even Field",
2084 .type
= V4L2_CTRL_TYPE_INTEGER
,
2090 static const struct v4l2_ctrl_config saa7134_ctrl_automute
= {
2091 .ops
= &saa7134_ctrl_ops
,
2092 .id
= V4L2_CID_PRIVATE_AUTOMUTE
,
2094 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
2101 int saa7134_video_init1(struct saa7134_dev
*dev
)
2103 struct v4l2_ctrl_handler
*hdl
= &dev
->ctrl_handler
;
2104 struct vb2_queue
*q
;
2107 /* sanitycheck insmod options */
2108 if (gbuffers
< 2 || gbuffers
> VIDEO_MAX_FRAME
)
2110 if (gbufsize
> gbufsize_max
)
2111 gbufsize
= gbufsize_max
;
2112 gbufsize
= (gbufsize
+ PAGE_SIZE
- 1) & PAGE_MASK
;
2114 v4l2_ctrl_handler_init(hdl
, 11);
2115 v4l2_ctrl_new_std(hdl
, &saa7134_ctrl_ops
,
2116 V4L2_CID_BRIGHTNESS
, 0, 255, 1, 128);
2117 v4l2_ctrl_new_std(hdl
, &saa7134_ctrl_ops
,
2118 V4L2_CID_CONTRAST
, 0, 127, 1, 68);
2119 v4l2_ctrl_new_std(hdl
, &saa7134_ctrl_ops
,
2120 V4L2_CID_SATURATION
, 0, 127, 1, 64);
2121 v4l2_ctrl_new_std(hdl
, &saa7134_ctrl_ops
,
2122 V4L2_CID_HUE
, -128, 127, 1, 0);
2123 v4l2_ctrl_new_std(hdl
, &saa7134_ctrl_ops
,
2124 V4L2_CID_HFLIP
, 0, 1, 1, 0);
2125 v4l2_ctrl_new_std(hdl
, &saa7134_ctrl_ops
,
2126 V4L2_CID_AUDIO_MUTE
, 0, 1, 1, 0);
2127 v4l2_ctrl_new_std(hdl
, &saa7134_ctrl_ops
,
2128 V4L2_CID_AUDIO_VOLUME
, -15, 15, 1, 0);
2129 v4l2_ctrl_new_custom(hdl
, &saa7134_ctrl_invert
, NULL
);
2130 v4l2_ctrl_new_custom(hdl
, &saa7134_ctrl_y_odd
, NULL
);
2131 v4l2_ctrl_new_custom(hdl
, &saa7134_ctrl_y_even
, NULL
);
2132 v4l2_ctrl_new_custom(hdl
, &saa7134_ctrl_automute
, NULL
);
2135 if (card_has_radio(dev
)) {
2136 hdl
= &dev
->radio_ctrl_handler
;
2137 v4l2_ctrl_handler_init(hdl
, 2);
2138 v4l2_ctrl_add_handler(hdl
, &dev
->ctrl_handler
,
2139 v4l2_ctrl_radio_filter
);
2145 if (dev
->tda9887_conf
&& saa7134_ctrl_automute
.def
)
2146 dev
->tda9887_conf
|= TDA9887_AUTOMUTE
;
2149 INIT_LIST_HEAD(&dev
->video_q
.queue
);
2150 timer_setup(&dev
->video_q
.timeout
, saa7134_buffer_timeout
, 0);
2151 dev
->video_q
.dev
= dev
;
2152 dev
->fmt
= format_by_fourcc(V4L2_PIX_FMT_BGR24
);
2155 dev
->field
= V4L2_FIELD_INTERLACED
;
2156 dev
->win
.w
.width
= dev
->width
;
2157 dev
->win
.w
.height
= dev
->height
;
2158 dev
->win
.field
= V4L2_FIELD_INTERLACED
;
2159 dev
->ovbuf
.fmt
.width
= dev
->width
;
2160 dev
->ovbuf
.fmt
.height
= dev
->height
;
2161 dev
->ovbuf
.fmt
.pixelformat
= dev
->fmt
->fourcc
;
2162 dev
->ovbuf
.fmt
.colorspace
= V4L2_COLORSPACE_SMPTE170M
;
2164 if (saa7134_boards
[dev
->board
].video_out
)
2165 saa7134_videoport_init(dev
);
2167 q
= &dev
->video_vbq
;
2168 q
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
2170 * Do not add VB2_USERPTR unless explicitly requested: the saa7134 DMA
2171 * engine cannot handle transfers that do not start at the beginning
2172 * of a page. A user-provided pointer can start anywhere in a page, so
2173 * USERPTR support is a no-go unless the application knows about these
2174 * limitations and has special support for this.
2176 q
->io_modes
= VB2_MMAP
| VB2_DMABUF
| VB2_READ
;
2177 if (saa7134_userptr
)
2178 q
->io_modes
|= VB2_USERPTR
;
2179 q
->drv_priv
= &dev
->video_q
;
2181 q
->gfp_flags
= GFP_DMA32
;
2182 q
->mem_ops
= &vb2_dma_sg_memops
;
2183 q
->buf_struct_size
= sizeof(struct saa7134_buf
);
2184 q
->timestamp_flags
= V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC
;
2185 q
->lock
= &dev
->lock
;
2186 q
->dev
= &dev
->pci
->dev
;
2187 ret
= vb2_queue_init(q
);
2190 saa7134_pgtable_alloc(dev
->pci
, &dev
->video_q
.pt
);
2193 q
->type
= V4L2_BUF_TYPE_VBI_CAPTURE
;
2194 /* Don't add VB2_USERPTR, see comment above */
2195 q
->io_modes
= VB2_MMAP
| VB2_READ
;
2196 if (saa7134_userptr
)
2197 q
->io_modes
|= VB2_USERPTR
;
2198 q
->drv_priv
= &dev
->vbi_q
;
2199 q
->ops
= &saa7134_vbi_qops
;
2200 q
->gfp_flags
= GFP_DMA32
;
2201 q
->mem_ops
= &vb2_dma_sg_memops
;
2202 q
->buf_struct_size
= sizeof(struct saa7134_buf
);
2203 q
->timestamp_flags
= V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC
;
2204 q
->lock
= &dev
->lock
;
2205 q
->dev
= &dev
->pci
->dev
;
2206 ret
= vb2_queue_init(q
);
2209 saa7134_pgtable_alloc(dev
->pci
, &dev
->vbi_q
.pt
);
2214 void saa7134_video_fini(struct saa7134_dev
*dev
)
2217 vb2_queue_release(&dev
->video_vbq
);
2218 saa7134_pgtable_free(dev
->pci
, &dev
->video_q
.pt
);
2219 vb2_queue_release(&dev
->vbi_vbq
);
2220 saa7134_pgtable_free(dev
->pci
, &dev
->vbi_q
.pt
);
2221 v4l2_ctrl_handler_free(&dev
->ctrl_handler
);
2222 if (card_has_radio(dev
))
2223 v4l2_ctrl_handler_free(&dev
->radio_ctrl_handler
);
2226 int saa7134_videoport_init(struct saa7134_dev
*dev
)
2228 /* enable video output */
2229 int vo
= saa7134_boards
[dev
->board
].video_out
;
2231 unsigned int vid_port_opts
= saa7134_boards
[dev
->board
].vid_port_opts
;
2233 /* Configure videoport */
2234 saa_writeb(SAA7134_VIDEO_PORT_CTRL0
, video_out
[vo
][0]);
2235 video_reg
= video_out
[vo
][1];
2236 if (vid_port_opts
& SET_T_CODE_POLARITY_NON_INVERTED
)
2237 video_reg
&= ~VP_T_CODE_P_INVERTED
;
2238 saa_writeb(SAA7134_VIDEO_PORT_CTRL1
, video_reg
);
2239 saa_writeb(SAA7134_VIDEO_PORT_CTRL2
, video_out
[vo
][2]);
2240 saa_writeb(SAA7134_VIDEO_PORT_CTRL4
, video_out
[vo
][4]);
2241 video_reg
= video_out
[vo
][5];
2242 if (vid_port_opts
& SET_CLOCK_NOT_DELAYED
)
2243 video_reg
&= ~VP_CLK_CTRL2_DELAYED
;
2244 if (vid_port_opts
& SET_CLOCK_INVERTED
)
2245 video_reg
|= VP_CLK_CTRL1_INVERTED
;
2246 saa_writeb(SAA7134_VIDEO_PORT_CTRL5
, video_reg
);
2247 video_reg
= video_out
[vo
][6];
2248 if (vid_port_opts
& SET_VSYNC_OFF
) {
2249 video_reg
&= ~VP_VS_TYPE_MASK
;
2250 video_reg
|= VP_VS_TYPE_OFF
;
2252 saa_writeb(SAA7134_VIDEO_PORT_CTRL6
, video_reg
);
2253 saa_writeb(SAA7134_VIDEO_PORT_CTRL7
, video_out
[vo
][7]);
2254 saa_writeb(SAA7134_VIDEO_PORT_CTRL8
, video_out
[vo
][8]);
2256 /* Start videoport */
2257 saa_writeb(SAA7134_VIDEO_PORT_CTRL3
, video_out
[vo
][3]);
2262 int saa7134_video_init2(struct saa7134_dev
*dev
)
2265 set_tvnorm(dev
,&tvnorms
[0]);
2267 v4l2_ctrl_handler_setup(&dev
->ctrl_handler
);
2268 saa7134_tvaudio_setmute(dev
);
2269 saa7134_tvaudio_setvolume(dev
,dev
->ctl_volume
);
2273 void saa7134_irq_video_signalchange(struct saa7134_dev
*dev
)
2275 static const char *st
[] = {
2276 "(no signal)", "NTSC", "PAL", "SECAM" };
2279 st1
= saa_readb(SAA7134_STATUS_VIDEO1
);
2280 st2
= saa_readb(SAA7134_STATUS_VIDEO2
);
2281 video_dbg("DCSDT: pll: %s, sync: %s, norm: %s\n",
2282 (st1
& 0x40) ? "not locked" : "locked",
2283 (st2
& 0x40) ? "no" : "yes",
2285 dev
->nosignal
= (st1
& 0x40) || (st2
& 0x40) || !(st2
& 0x1);
2287 if (dev
->nosignal
) {
2288 /* no video signal -> mute audio */
2289 if (dev
->ctl_automute
)
2291 saa7134_tvaudio_setmute(dev
);
2293 /* wake up tvaudio audio carrier scan thread */
2294 saa7134_tvaudio_do_scan(dev
);
2297 if ((st2
& 0x80) && !noninterlaced
&& !dev
->nosignal
)
2298 saa_clearb(SAA7134_SYNC_CTRL
, 0x20);
2300 saa_setb(SAA7134_SYNC_CTRL
, 0x20);
2302 if (dev
->mops
&& dev
->mops
->signal_change
)
2303 dev
->mops
->signal_change(dev
);
2307 void saa7134_irq_video_done(struct saa7134_dev
*dev
, unsigned long status
)
2309 enum v4l2_field field
;
2311 spin_lock(&dev
->slock
);
2312 if (dev
->video_q
.curr
) {
2314 if (V4L2_FIELD_HAS_BOTH(field
)) {
2315 /* make sure we have seen both fields */
2316 if ((status
& 0x10) == 0x00) {
2317 dev
->video_q
.curr
->top_seen
= 1;
2320 if (!dev
->video_q
.curr
->top_seen
)
2322 } else if (field
== V4L2_FIELD_TOP
) {
2323 if ((status
& 0x10) != 0x10)
2325 } else if (field
== V4L2_FIELD_BOTTOM
) {
2326 if ((status
& 0x10) != 0x00)
2329 saa7134_buffer_finish(dev
, &dev
->video_q
, VB2_BUF_STATE_DONE
);
2331 saa7134_buffer_next(dev
, &dev
->video_q
);
2334 spin_unlock(&dev
->slock
);