3 * device driver for philips saa7134 based TV cards
4 * video4linux video interface
6 * (c) 2001-03 Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #include <linux/init.h>
24 #include <linux/list.h>
25 #include <linux/module.h>
26 #include <linux/kernel.h>
27 #include <linux/slab.h>
28 #include <linux/sort.h>
30 #include "saa7134-reg.h"
32 #include <media/v4l2-common.h>
34 #ifdef CONFIG_VIDEO_V4L1_COMPAT
35 /* Include V4L1 specific functions. Should be removed soon */
36 #include <linux/videodev.h>
39 /* ------------------------------------------------------------------ */
41 static unsigned int video_debug
= 0;
42 static unsigned int gbuffers
= 8;
43 static unsigned int noninterlaced
= 0;
44 static unsigned int gbufsize
= 720*576*4;
45 static unsigned int gbufsize_max
= 720*576*4;
46 static char secam
[] = "--";
47 module_param(video_debug
, int, 0644);
48 MODULE_PARM_DESC(video_debug
,"enable debug messages [video]");
49 module_param(gbuffers
, int, 0444);
50 MODULE_PARM_DESC(gbuffers
,"number of capture buffers, range 2-32");
51 module_param(noninterlaced
, int, 0644);
52 MODULE_PARM_DESC(noninterlaced
,"capture non interlaced video");
53 module_param_string(secam
, secam
, sizeof(secam
), 0644);
54 MODULE_PARM_DESC(secam
, "force SECAM variant, either DK,L or Lc");
57 #define dprintk(fmt, arg...) if (video_debug) \
58 printk(KERN_DEBUG "%s/video: " fmt, dev->name , ## 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 = 22, \
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 #define V4L2_CID_PRIVATE_INVERT (V4L2_CID_PRIVATE_BASE + 0)
377 #define V4L2_CID_PRIVATE_Y_ODD (V4L2_CID_PRIVATE_BASE + 1)
378 #define V4L2_CID_PRIVATE_Y_EVEN (V4L2_CID_PRIVATE_BASE + 2)
379 #define V4L2_CID_PRIVATE_AUTOMUTE (V4L2_CID_PRIVATE_BASE + 3)
380 #define V4L2_CID_PRIVATE_LASTP1 (V4L2_CID_PRIVATE_BASE + 4)
382 static const struct v4l2_queryctrl no_ctrl
= {
384 .flags
= V4L2_CTRL_FLAG_DISABLED
,
386 static const struct v4l2_queryctrl video_ctrls
[] = {
389 .id
= V4L2_CID_BRIGHTNESS
,
390 .name
= "Brightness",
394 .default_value
= 128,
395 .type
= V4L2_CTRL_TYPE_INTEGER
,
397 .id
= V4L2_CID_CONTRAST
,
403 .type
= V4L2_CTRL_TYPE_INTEGER
,
405 .id
= V4L2_CID_SATURATION
,
406 .name
= "Saturation",
411 .type
= V4L2_CTRL_TYPE_INTEGER
,
419 .type
= V4L2_CTRL_TYPE_INTEGER
,
421 .id
= V4L2_CID_HFLIP
,
425 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
429 .id
= V4L2_CID_AUDIO_MUTE
,
433 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
435 .id
= V4L2_CID_AUDIO_VOLUME
,
441 .type
= V4L2_CTRL_TYPE_INTEGER
,
443 /* --- private --- */
445 .id
= V4L2_CID_PRIVATE_INVERT
,
449 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
451 .id
= V4L2_CID_PRIVATE_Y_ODD
,
452 .name
= "y offset odd field",
456 .type
= V4L2_CTRL_TYPE_INTEGER
,
458 .id
= V4L2_CID_PRIVATE_Y_EVEN
,
459 .name
= "y offset even field",
463 .type
= V4L2_CTRL_TYPE_INTEGER
,
465 .id
= V4L2_CID_PRIVATE_AUTOMUTE
,
470 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
473 static const unsigned int CTRLS
= ARRAY_SIZE(video_ctrls
);
475 static const struct v4l2_queryctrl
* ctrl_by_id(unsigned int id
)
479 for (i
= 0; i
< CTRLS
; i
++)
480 if (video_ctrls
[i
].id
== id
)
481 return video_ctrls
+i
;
485 static struct saa7134_format
* format_by_fourcc(unsigned int fourcc
)
489 for (i
= 0; i
< FORMATS
; i
++)
490 if (formats
[i
].fourcc
== fourcc
)
495 /* ----------------------------------------------------------------------- */
496 /* resource management */
498 static int res_get(struct saa7134_dev
*dev
, struct saa7134_fh
*fh
, unsigned int bit
)
500 if (fh
->resources
& bit
)
501 /* have it already allocated */
505 mutex_lock(&dev
->lock
);
506 if (dev
->resources
& bit
) {
507 /* no, someone else uses it */
508 mutex_unlock(&dev
->lock
);
511 /* it's free, grab it */
512 fh
->resources
|= bit
;
513 dev
->resources
|= bit
;
514 dprintk("res: get %d\n",bit
);
515 mutex_unlock(&dev
->lock
);
519 static int res_check(struct saa7134_fh
*fh
, unsigned int bit
)
521 return (fh
->resources
& bit
);
524 static int res_locked(struct saa7134_dev
*dev
, unsigned int bit
)
526 return (dev
->resources
& bit
);
530 void res_free(struct saa7134_dev
*dev
, struct saa7134_fh
*fh
, unsigned int bits
)
532 BUG_ON((fh
->resources
& bits
) != bits
);
534 mutex_lock(&dev
->lock
);
535 fh
->resources
&= ~bits
;
536 dev
->resources
&= ~bits
;
537 dprintk("res: put %d\n",bits
);
538 mutex_unlock(&dev
->lock
);
541 /* ------------------------------------------------------------------ */
543 void set_tvnorm(struct saa7134_dev
*dev
, struct saa7134_tvnorm
*norm
)
546 dprintk("set tv norm = %s\n",norm
->name
);
550 dev
->crop_bounds
.left
= norm
->h_start
;
551 dev
->crop_defrect
.left
= norm
->h_start
;
552 dev
->crop_bounds
.width
= norm
->h_stop
- norm
->h_start
+1;
553 dev
->crop_defrect
.width
= norm
->h_stop
- norm
->h_start
+1;
555 dev
->crop_bounds
.top
= (norm
->vbi_v_stop_0
+1)*2;
556 dev
->crop_defrect
.top
= norm
->video_v_start
*2;
557 dev
->crop_bounds
.height
= ((norm
->id
& V4L2_STD_525_60
) ? 524 : 624)
558 - dev
->crop_bounds
.top
;
559 dev
->crop_defrect
.height
= (norm
->video_v_stop
- norm
->video_v_start
+1)*2;
561 dev
->crop_current
= dev
->crop_defrect
;
563 saa7134_set_tvnorm_hw(dev
);
567 static void video_mux(struct saa7134_dev
*dev
, int input
)
569 dprintk("video input = %d [%s]\n", input
, card_in(dev
, input
).name
);
570 dev
->ctl_input
= input
;
571 set_tvnorm(dev
, dev
->tvnorm
);
572 saa7134_tvaudio_setinput(dev
, &card_in(dev
, input
));
576 static void saa7134_set_decoder(struct saa7134_dev
*dev
)
578 int luma_control
, sync_control
, mux
;
580 struct saa7134_tvnorm
*norm
= dev
->tvnorm
;
581 mux
= card_in(dev
, dev
->ctl_input
).vmux
;
583 luma_control
= norm
->luma_control
;
584 sync_control
= norm
->sync_control
;
587 luma_control
|= 0x80; /* svideo */
588 if (noninterlaced
|| dev
->nosignal
)
589 sync_control
|= 0x20;
591 /* setup video decoder */
592 saa_writeb(SAA7134_INCR_DELAY
, 0x08);
593 saa_writeb(SAA7134_ANALOG_IN_CTRL1
, 0xc0 | mux
);
594 saa_writeb(SAA7134_ANALOG_IN_CTRL2
, 0x00);
596 saa_writeb(SAA7134_ANALOG_IN_CTRL3
, 0x90);
597 saa_writeb(SAA7134_ANALOG_IN_CTRL4
, 0x90);
598 saa_writeb(SAA7134_HSYNC_START
, 0xeb);
599 saa_writeb(SAA7134_HSYNC_STOP
, 0xe0);
600 saa_writeb(SAA7134_SOURCE_TIMING1
, norm
->src_timing
);
602 saa_writeb(SAA7134_SYNC_CTRL
, sync_control
);
603 saa_writeb(SAA7134_LUMA_CTRL
, luma_control
);
604 saa_writeb(SAA7134_DEC_LUMA_BRIGHT
, dev
->ctl_bright
);
606 saa_writeb(SAA7134_DEC_LUMA_CONTRAST
,
607 dev
->ctl_invert
? -dev
->ctl_contrast
: dev
->ctl_contrast
);
609 saa_writeb(SAA7134_DEC_CHROMA_SATURATION
,
610 dev
->ctl_invert
? -dev
->ctl_saturation
: dev
->ctl_saturation
);
612 saa_writeb(SAA7134_DEC_CHROMA_HUE
, dev
->ctl_hue
);
613 saa_writeb(SAA7134_CHROMA_CTRL1
, norm
->chroma_ctrl1
);
614 saa_writeb(SAA7134_CHROMA_GAIN
, norm
->chroma_gain
);
616 saa_writeb(SAA7134_CHROMA_CTRL2
, norm
->chroma_ctrl2
);
617 saa_writeb(SAA7134_MODE_DELAY_CTRL
, 0x00);
619 saa_writeb(SAA7134_ANALOG_ADC
, 0x01);
620 saa_writeb(SAA7134_VGATE_START
, 0x11);
621 saa_writeb(SAA7134_VGATE_STOP
, 0xfe);
622 saa_writeb(SAA7134_MISC_VGATE_MSB
, norm
->vgate_misc
);
623 saa_writeb(SAA7134_RAW_DATA_GAIN
, 0x40);
624 saa_writeb(SAA7134_RAW_DATA_OFFSET
, 0x80);
627 void saa7134_set_tvnorm_hw(struct saa7134_dev
*dev
)
629 saa7134_set_decoder(dev
);
631 if (card_in(dev
, dev
->ctl_input
).tv
) {
632 if ((card(dev
).tuner_type
== TUNER_PHILIPS_TDA8290
)
633 && ((card(dev
).tuner_config
== 1)
634 || (card(dev
).tuner_config
== 2)))
635 saa7134_set_gpio(dev
, 22, 5);
636 saa7134_i2c_call_clients(dev
, VIDIOC_S_STD
, &dev
->tvnorm
->id
);
640 static void set_h_prescale(struct saa7134_dev
*dev
, int task
, int prescale
)
642 static const struct {
649 /* XPSC XACL XC2_1 XDCG VPFY */
661 static const int count
= ARRAY_SIZE(vals
);
664 for (i
= 0; i
< count
; i
++)
665 if (vals
[i
].xpsc
== prescale
)
670 saa_writeb(SAA7134_H_PRESCALE(task
), vals
[i
].xpsc
);
671 saa_writeb(SAA7134_ACC_LENGTH(task
), vals
[i
].xacl
);
672 saa_writeb(SAA7134_LEVEL_CTRL(task
),
673 (vals
[i
].xc2_1
<< 3) | (vals
[i
].xdcg
));
674 saa_andorb(SAA7134_FIR_PREFILTER_CTRL(task
), 0x0f,
675 (vals
[i
].vpfy
<< 2) | vals
[i
].vpfy
);
678 static void set_v_scale(struct saa7134_dev
*dev
, int task
, int yscale
)
682 saa_writeb(SAA7134_V_SCALE_RATIO1(task
), yscale
& 0xff);
683 saa_writeb(SAA7134_V_SCALE_RATIO2(task
), yscale
>> 8);
685 mirror
= (dev
->ctl_mirror
) ? 0x02 : 0x00;
688 dprintk("yscale LPI yscale=%d\n",yscale
);
689 saa_writeb(SAA7134_V_FILTER(task
), 0x00 | mirror
);
690 saa_writeb(SAA7134_LUMA_CONTRAST(task
), 0x40);
691 saa_writeb(SAA7134_CHROMA_SATURATION(task
), 0x40);
694 val
= 0x40 * 1024 / yscale
;
695 dprintk("yscale ACM yscale=%d val=0x%x\n",yscale
,val
);
696 saa_writeb(SAA7134_V_FILTER(task
), 0x01 | mirror
);
697 saa_writeb(SAA7134_LUMA_CONTRAST(task
), val
);
698 saa_writeb(SAA7134_CHROMA_SATURATION(task
), val
);
700 saa_writeb(SAA7134_LUMA_BRIGHT(task
), 0x80);
703 static void set_size(struct saa7134_dev
*dev
, int task
,
704 int width
, int height
, int interlace
)
706 int prescale
,xscale
,yscale
,y_even
,y_odd
;
707 int h_start
, h_stop
, v_start
, v_stop
;
708 int div
= interlace
? 2 : 1;
710 /* setup video scaler */
711 h_start
= dev
->crop_current
.left
;
712 v_start
= dev
->crop_current
.top
/2;
713 h_stop
= (dev
->crop_current
.left
+ dev
->crop_current
.width
-1);
714 v_stop
= (dev
->crop_current
.top
+ dev
->crop_current
.height
-1)/2;
716 saa_writeb(SAA7134_VIDEO_H_START1(task
), h_start
& 0xff);
717 saa_writeb(SAA7134_VIDEO_H_START2(task
), h_start
>> 8);
718 saa_writeb(SAA7134_VIDEO_H_STOP1(task
), h_stop
& 0xff);
719 saa_writeb(SAA7134_VIDEO_H_STOP2(task
), h_stop
>> 8);
720 saa_writeb(SAA7134_VIDEO_V_START1(task
), v_start
& 0xff);
721 saa_writeb(SAA7134_VIDEO_V_START2(task
), v_start
>> 8);
722 saa_writeb(SAA7134_VIDEO_V_STOP1(task
), v_stop
& 0xff);
723 saa_writeb(SAA7134_VIDEO_V_STOP2(task
), v_stop
>> 8);
725 prescale
= dev
->crop_current
.width
/ width
;
728 xscale
= 1024 * dev
->crop_current
.width
/ prescale
/ width
;
729 yscale
= 512 * div
* dev
->crop_current
.height
/ height
;
730 dprintk("prescale=%d xscale=%d yscale=%d\n",prescale
,xscale
,yscale
);
731 set_h_prescale(dev
,task
,prescale
);
732 saa_writeb(SAA7134_H_SCALE_INC1(task
), xscale
& 0xff);
733 saa_writeb(SAA7134_H_SCALE_INC2(task
), xscale
>> 8);
734 set_v_scale(dev
,task
,yscale
);
736 saa_writeb(SAA7134_VIDEO_PIXELS1(task
), width
& 0xff);
737 saa_writeb(SAA7134_VIDEO_PIXELS2(task
), width
>> 8);
738 saa_writeb(SAA7134_VIDEO_LINES1(task
), height
/div
& 0xff);
739 saa_writeb(SAA7134_VIDEO_LINES2(task
), height
/div
>> 8);
741 /* deinterlace y offsets */
742 y_odd
= dev
->ctl_y_odd
;
743 y_even
= dev
->ctl_y_even
;
744 saa_writeb(SAA7134_V_PHASE_OFFSET0(task
), y_odd
);
745 saa_writeb(SAA7134_V_PHASE_OFFSET1(task
), y_even
);
746 saa_writeb(SAA7134_V_PHASE_OFFSET2(task
), y_odd
);
747 saa_writeb(SAA7134_V_PHASE_OFFSET3(task
), y_even
);
750 /* ------------------------------------------------------------------ */
758 static void set_cliplist(struct saa7134_dev
*dev
, int reg
,
759 struct cliplist
*cl
, int entries
, char *name
)
764 for (i
= 0; i
< entries
; i
++) {
765 winbits
|= cl
[i
].enable
;
766 winbits
&= ~cl
[i
].disable
;
767 if (i
< 15 && cl
[i
].position
== cl
[i
+1].position
)
769 saa_writeb(reg
+ 0, winbits
);
770 saa_writeb(reg
+ 2, cl
[i
].position
& 0xff);
771 saa_writeb(reg
+ 3, cl
[i
].position
>> 8);
772 dprintk("clip: %s winbits=%02x pos=%d\n",
773 name
,winbits
,cl
[i
].position
);
776 for (; reg
< 0x400; reg
+= 8) {
777 saa_writeb(reg
+ 0, 0);
778 saa_writeb(reg
+ 1, 0);
779 saa_writeb(reg
+ 2, 0);
780 saa_writeb(reg
+ 3, 0);
784 static int clip_range(int val
)
791 /* Sort into smallest position first order */
792 static int cliplist_cmp(const void *a
, const void *b
)
794 const struct cliplist
*cla
= a
;
795 const struct cliplist
*clb
= b
;
796 if (cla
->position
< clb
->position
)
798 if (cla
->position
> clb
->position
)
803 static int setup_clipping(struct saa7134_dev
*dev
, struct v4l2_clip
*clips
,
804 int nclips
, int interlace
)
806 struct cliplist col
[16], row
[16];
807 int cols
= 0, rows
= 0, i
;
808 int div
= interlace
? 2 : 1;
810 memset(col
, 0, sizeof(col
));
811 memset(row
, 0, sizeof(row
));
812 for (i
= 0; i
< nclips
&& i
< 8; i
++) {
813 col
[cols
].position
= clip_range(clips
[i
].c
.left
);
814 col
[cols
].enable
= (1 << i
);
816 col
[cols
].position
= clip_range(clips
[i
].c
.left
+clips
[i
].c
.width
);
817 col
[cols
].disable
= (1 << i
);
819 row
[rows
].position
= clip_range(clips
[i
].c
.top
/ div
);
820 row
[rows
].enable
= (1 << i
);
822 row
[rows
].position
= clip_range((clips
[i
].c
.top
+ clips
[i
].c
.height
)
824 row
[rows
].disable
= (1 << i
);
827 sort(col
, cols
, sizeof col
[0], cliplist_cmp
, NULL
);
828 sort(row
, rows
, sizeof row
[0], cliplist_cmp
, NULL
);
829 set_cliplist(dev
,0x380,col
,cols
,"cols");
830 set_cliplist(dev
,0x384,row
,rows
,"rows");
834 static int verify_preview(struct saa7134_dev
*dev
, struct v4l2_window
*win
)
836 enum v4l2_field field
;
839 if (NULL
== dev
->ovbuf
.base
)
841 if (NULL
== dev
->ovfmt
)
843 if (win
->w
.width
< 48 || win
->w
.height
< 32)
845 if (win
->clipcount
> 2048)
849 maxw
= dev
->crop_current
.width
;
850 maxh
= dev
->crop_current
.height
;
852 if (V4L2_FIELD_ANY
== field
) {
853 field
= (win
->w
.height
> maxh
/2)
854 ? V4L2_FIELD_INTERLACED
859 case V4L2_FIELD_BOTTOM
:
862 case V4L2_FIELD_INTERLACED
:
869 if (win
->w
.width
> maxw
)
871 if (win
->w
.height
> maxh
)
872 win
->w
.height
= maxh
;
876 static int start_preview(struct saa7134_dev
*dev
, struct saa7134_fh
*fh
)
878 unsigned long base
,control
,bpl
;
881 err
= verify_preview(dev
,&fh
->win
);
885 dev
->ovfield
= fh
->win
.field
;
886 dprintk("start_preview %dx%d+%d+%d %s field=%s\n",
887 fh
->win
.w
.width
,fh
->win
.w
.height
,
888 fh
->win
.w
.left
,fh
->win
.w
.top
,
889 dev
->ovfmt
->name
,v4l2_field_names
[dev
->ovfield
]);
891 /* setup window + clipping */
892 set_size(dev
,TASK_B
,fh
->win
.w
.width
,fh
->win
.w
.height
,
893 V4L2_FIELD_HAS_BOTH(dev
->ovfield
));
894 setup_clipping(dev
,fh
->clips
,fh
->nclips
,
895 V4L2_FIELD_HAS_BOTH(dev
->ovfield
));
897 saa_andorb(SAA7134_DATA_PATH(TASK_B
), 0x3f, 0x03);
899 saa_andorb(SAA7134_DATA_PATH(TASK_B
), 0x3f, 0x01);
900 saa_writeb(SAA7134_OFMT_VIDEO_B
, dev
->ovfmt
->pm
| 0x20);
902 /* dma: setup channel 1 (= Video Task B) */
903 base
= (unsigned long)dev
->ovbuf
.base
;
904 base
+= dev
->ovbuf
.fmt
.bytesperline
* fh
->win
.w
.top
;
905 base
+= dev
->ovfmt
->depth
/8 * fh
->win
.w
.left
;
906 bpl
= dev
->ovbuf
.fmt
.bytesperline
;
907 control
= SAA7134_RS_CONTROL_BURST_16
;
908 if (dev
->ovfmt
->bswap
)
909 control
|= SAA7134_RS_CONTROL_BSWAP
;
910 if (dev
->ovfmt
->wswap
)
911 control
|= SAA7134_RS_CONTROL_WSWAP
;
912 if (V4L2_FIELD_HAS_BOTH(dev
->ovfield
)) {
913 saa_writel(SAA7134_RS_BA1(1),base
);
914 saa_writel(SAA7134_RS_BA2(1),base
+bpl
);
915 saa_writel(SAA7134_RS_PITCH(1),bpl
*2);
916 saa_writel(SAA7134_RS_CONTROL(1),control
);
918 saa_writel(SAA7134_RS_BA1(1),base
);
919 saa_writel(SAA7134_RS_BA2(1),base
);
920 saa_writel(SAA7134_RS_PITCH(1),bpl
);
921 saa_writel(SAA7134_RS_CONTROL(1),control
);
926 saa7134_set_dmabits(dev
);
931 static int stop_preview(struct saa7134_dev
*dev
, struct saa7134_fh
*fh
)
934 saa7134_set_dmabits(dev
);
938 /* ------------------------------------------------------------------ */
940 static int buffer_activate(struct saa7134_dev
*dev
,
941 struct saa7134_buf
*buf
,
942 struct saa7134_buf
*next
)
944 unsigned long base
,control
,bpl
;
945 unsigned long bpl_uv
,lines_uv
,base2
,base3
,tmp
; /* planar */
947 dprintk("buffer_activate buf=%p\n",buf
);
948 buf
->vb
.state
= STATE_ACTIVE
;
951 set_size(dev
,TASK_A
,buf
->vb
.width
,buf
->vb
.height
,
952 V4L2_FIELD_HAS_BOTH(buf
->vb
.field
));
954 saa_andorb(SAA7134_DATA_PATH(TASK_A
), 0x3f, 0x03);
956 saa_andorb(SAA7134_DATA_PATH(TASK_A
), 0x3f, 0x01);
957 saa_writeb(SAA7134_OFMT_VIDEO_A
, buf
->fmt
->pm
);
959 /* DMA: setup channel 0 (= Video Task A0) */
960 base
= saa7134_buffer_base(buf
);
961 if (buf
->fmt
->planar
)
964 bpl
= (buf
->vb
.width
* buf
->fmt
->depth
) / 8;
965 control
= SAA7134_RS_CONTROL_BURST_16
|
966 SAA7134_RS_CONTROL_ME
|
967 (buf
->pt
->dma
>> 12);
969 control
|= SAA7134_RS_CONTROL_BSWAP
;
971 control
|= SAA7134_RS_CONTROL_WSWAP
;
972 if (V4L2_FIELD_HAS_BOTH(buf
->vb
.field
)) {
974 saa_writel(SAA7134_RS_BA1(0),base
);
975 saa_writel(SAA7134_RS_BA2(0),base
+bpl
);
976 saa_writel(SAA7134_RS_PITCH(0),bpl
*2);
979 saa_writel(SAA7134_RS_BA1(0),base
);
980 saa_writel(SAA7134_RS_BA2(0),base
);
981 saa_writel(SAA7134_RS_PITCH(0),bpl
);
983 saa_writel(SAA7134_RS_CONTROL(0),control
);
985 if (buf
->fmt
->planar
) {
986 /* DMA: setup channel 4+5 (= planar task A) */
987 bpl_uv
= bpl
>> buf
->fmt
->hshift
;
988 lines_uv
= buf
->vb
.height
>> buf
->fmt
->vshift
;
989 base2
= base
+ bpl
* buf
->vb
.height
;
990 base3
= base2
+ bpl_uv
* lines_uv
;
991 if (buf
->fmt
->uvswap
)
992 tmp
= base2
, base2
= base3
, base3
= tmp
;
993 dprintk("uv: bpl=%ld lines=%ld base2/3=%ld/%ld\n",
994 bpl_uv
,lines_uv
,base2
,base3
);
995 if (V4L2_FIELD_HAS_BOTH(buf
->vb
.field
)) {
997 saa_writel(SAA7134_RS_BA1(4),base2
);
998 saa_writel(SAA7134_RS_BA2(4),base2
+bpl_uv
);
999 saa_writel(SAA7134_RS_PITCH(4),bpl_uv
*2);
1000 saa_writel(SAA7134_RS_BA1(5),base3
);
1001 saa_writel(SAA7134_RS_BA2(5),base3
+bpl_uv
);
1002 saa_writel(SAA7134_RS_PITCH(5),bpl_uv
*2);
1004 /* non-interlaced */
1005 saa_writel(SAA7134_RS_BA1(4),base2
);
1006 saa_writel(SAA7134_RS_BA2(4),base2
);
1007 saa_writel(SAA7134_RS_PITCH(4),bpl_uv
);
1008 saa_writel(SAA7134_RS_BA1(5),base3
);
1009 saa_writel(SAA7134_RS_BA2(5),base3
);
1010 saa_writel(SAA7134_RS_PITCH(5),bpl_uv
);
1012 saa_writel(SAA7134_RS_CONTROL(4),control
);
1013 saa_writel(SAA7134_RS_CONTROL(5),control
);
1017 saa7134_set_dmabits(dev
);
1018 mod_timer(&dev
->video_q
.timeout
, jiffies
+BUFFER_TIMEOUT
);
1022 static int buffer_prepare(struct videobuf_queue
*q
,
1023 struct videobuf_buffer
*vb
,
1024 enum v4l2_field field
)
1026 struct saa7134_fh
*fh
= q
->priv_data
;
1027 struct saa7134_dev
*dev
= fh
->dev
;
1028 struct saa7134_buf
*buf
= container_of(vb
,struct saa7134_buf
,vb
);
1033 if (NULL
== fh
->fmt
)
1035 if (fh
->width
< 48 ||
1037 fh
->width
/4 > dev
->crop_current
.width
||
1038 fh
->height
/4 > dev
->crop_current
.height
||
1039 fh
->width
> dev
->crop_bounds
.width
||
1040 fh
->height
> dev
->crop_bounds
.height
)
1042 size
= (fh
->width
* fh
->height
* fh
->fmt
->depth
) >> 3;
1043 if (0 != buf
->vb
.baddr
&& buf
->vb
.bsize
< size
)
1046 dprintk("buffer_prepare [%d,size=%dx%d,bytes=%d,fields=%s,%s]\n",
1047 vb
->i
,fh
->width
,fh
->height
,size
,v4l2_field_names
[field
],
1049 if (buf
->vb
.width
!= fh
->width
||
1050 buf
->vb
.height
!= fh
->height
||
1051 buf
->vb
.size
!= size
||
1052 buf
->vb
.field
!= field
||
1053 buf
->fmt
!= fh
->fmt
) {
1054 saa7134_dma_free(q
,buf
);
1057 if (STATE_NEEDS_INIT
== buf
->vb
.state
) {
1058 struct videobuf_dmabuf
*dma
=videobuf_to_dma(&buf
->vb
);
1060 buf
->vb
.width
= fh
->width
;
1061 buf
->vb
.height
= fh
->height
;
1062 buf
->vb
.size
= size
;
1063 buf
->vb
.field
= field
;
1065 buf
->pt
= &fh
->pt_cap
;
1067 err
= videobuf_iolock(q
,&buf
->vb
,&dev
->ovbuf
);
1070 err
= saa7134_pgtable_build(dev
->pci
,buf
->pt
,
1073 saa7134_buffer_startpage(buf
));
1077 buf
->vb
.state
= STATE_PREPARED
;
1078 buf
->activate
= buffer_activate
;
1082 saa7134_dma_free(q
,buf
);
1087 buffer_setup(struct videobuf_queue
*q
, unsigned int *count
, unsigned int *size
)
1089 struct saa7134_fh
*fh
= q
->priv_data
;
1091 *size
= fh
->fmt
->depth
* fh
->width
* fh
->height
>> 3;
1094 *count
= saa7134_buffer_count(*size
,*count
);
1098 static void buffer_queue(struct videobuf_queue
*q
, struct videobuf_buffer
*vb
)
1100 struct saa7134_fh
*fh
= q
->priv_data
;
1101 struct saa7134_buf
*buf
= container_of(vb
,struct saa7134_buf
,vb
);
1103 saa7134_buffer_queue(fh
->dev
,&fh
->dev
->video_q
,buf
);
1106 static void buffer_release(struct videobuf_queue
*q
, struct videobuf_buffer
*vb
)
1108 struct saa7134_buf
*buf
= container_of(vb
,struct saa7134_buf
,vb
);
1110 saa7134_dma_free(q
,buf
);
1113 static struct videobuf_queue_ops video_qops
= {
1114 .buf_setup
= buffer_setup
,
1115 .buf_prepare
= buffer_prepare
,
1116 .buf_queue
= buffer_queue
,
1117 .buf_release
= buffer_release
,
1120 /* ------------------------------------------------------------------ */
1122 static int get_control(struct saa7134_dev
*dev
, struct v4l2_control
*c
)
1124 const struct v4l2_queryctrl
* ctrl
;
1126 ctrl
= ctrl_by_id(c
->id
);
1130 case V4L2_CID_BRIGHTNESS
:
1131 c
->value
= dev
->ctl_bright
;
1134 c
->value
= dev
->ctl_hue
;
1136 case V4L2_CID_CONTRAST
:
1137 c
->value
= dev
->ctl_contrast
;
1139 case V4L2_CID_SATURATION
:
1140 c
->value
= dev
->ctl_saturation
;
1142 case V4L2_CID_AUDIO_MUTE
:
1143 c
->value
= dev
->ctl_mute
;
1145 case V4L2_CID_AUDIO_VOLUME
:
1146 c
->value
= dev
->ctl_volume
;
1148 case V4L2_CID_PRIVATE_INVERT
:
1149 c
->value
= dev
->ctl_invert
;
1151 case V4L2_CID_HFLIP
:
1152 c
->value
= dev
->ctl_mirror
;
1154 case V4L2_CID_PRIVATE_Y_EVEN
:
1155 c
->value
= dev
->ctl_y_even
;
1157 case V4L2_CID_PRIVATE_Y_ODD
:
1158 c
->value
= dev
->ctl_y_odd
;
1160 case V4L2_CID_PRIVATE_AUTOMUTE
:
1161 c
->value
= dev
->ctl_automute
;
1169 static int set_control(struct saa7134_dev
*dev
, struct saa7134_fh
*fh
,
1170 struct v4l2_control
*c
)
1172 const struct v4l2_queryctrl
* ctrl
;
1173 unsigned long flags
;
1174 int restart_overlay
= 0;
1176 ctrl
= ctrl_by_id(c
->id
);
1179 dprintk("set_control name=%s val=%d\n",ctrl
->name
,c
->value
);
1180 switch (ctrl
->type
) {
1181 case V4L2_CTRL_TYPE_BOOLEAN
:
1182 case V4L2_CTRL_TYPE_MENU
:
1183 case V4L2_CTRL_TYPE_INTEGER
:
1184 if (c
->value
< ctrl
->minimum
)
1185 c
->value
= ctrl
->minimum
;
1186 if (c
->value
> ctrl
->maximum
)
1187 c
->value
= ctrl
->maximum
;
1193 case V4L2_CID_BRIGHTNESS
:
1194 dev
->ctl_bright
= c
->value
;
1195 saa_writeb(SAA7134_DEC_LUMA_BRIGHT
, dev
->ctl_bright
);
1198 dev
->ctl_hue
= c
->value
;
1199 saa_writeb(SAA7134_DEC_CHROMA_HUE
, dev
->ctl_hue
);
1201 case V4L2_CID_CONTRAST
:
1202 dev
->ctl_contrast
= c
->value
;
1203 saa_writeb(SAA7134_DEC_LUMA_CONTRAST
,
1204 dev
->ctl_invert
? -dev
->ctl_contrast
: dev
->ctl_contrast
);
1206 case V4L2_CID_SATURATION
:
1207 dev
->ctl_saturation
= c
->value
;
1208 saa_writeb(SAA7134_DEC_CHROMA_SATURATION
,
1209 dev
->ctl_invert
? -dev
->ctl_saturation
: dev
->ctl_saturation
);
1211 case V4L2_CID_AUDIO_MUTE
:
1212 dev
->ctl_mute
= c
->value
;
1213 saa7134_tvaudio_setmute(dev
);
1215 case V4L2_CID_AUDIO_VOLUME
:
1216 dev
->ctl_volume
= c
->value
;
1217 saa7134_tvaudio_setvolume(dev
,dev
->ctl_volume
);
1219 case V4L2_CID_PRIVATE_INVERT
:
1220 dev
->ctl_invert
= c
->value
;
1221 saa_writeb(SAA7134_DEC_LUMA_CONTRAST
,
1222 dev
->ctl_invert
? -dev
->ctl_contrast
: dev
->ctl_contrast
);
1223 saa_writeb(SAA7134_DEC_CHROMA_SATURATION
,
1224 dev
->ctl_invert
? -dev
->ctl_saturation
: dev
->ctl_saturation
);
1226 case V4L2_CID_HFLIP
:
1227 dev
->ctl_mirror
= c
->value
;
1228 restart_overlay
= 1;
1230 case V4L2_CID_PRIVATE_Y_EVEN
:
1231 dev
->ctl_y_even
= c
->value
;
1232 restart_overlay
= 1;
1234 case V4L2_CID_PRIVATE_Y_ODD
:
1235 dev
->ctl_y_odd
= c
->value
;
1236 restart_overlay
= 1;
1238 case V4L2_CID_PRIVATE_AUTOMUTE
:
1239 dev
->ctl_automute
= c
->value
;
1240 if (dev
->tda9887_conf
) {
1241 if (dev
->ctl_automute
)
1242 dev
->tda9887_conf
|= TDA9887_AUTOMUTE
;
1244 dev
->tda9887_conf
&= ~TDA9887_AUTOMUTE
;
1245 saa7134_i2c_call_clients(dev
, TDA9887_SET_CONFIG
,
1246 &dev
->tda9887_conf
);
1252 if (restart_overlay
&& fh
&& res_check(fh
, RESOURCE_OVERLAY
)) {
1253 spin_lock_irqsave(&dev
->slock
,flags
);
1254 stop_preview(dev
,fh
);
1255 start_preview(dev
,fh
);
1256 spin_unlock_irqrestore(&dev
->slock
,flags
);
1261 /* ------------------------------------------------------------------ */
1263 static struct videobuf_queue
* saa7134_queue(struct saa7134_fh
*fh
)
1265 struct videobuf_queue
* q
= NULL
;
1268 case V4L2_BUF_TYPE_VIDEO_CAPTURE
:
1271 case V4L2_BUF_TYPE_VBI_CAPTURE
:
1280 static int saa7134_resource(struct saa7134_fh
*fh
)
1282 if (fh
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1283 return RESOURCE_VIDEO
;
1285 if (fh
->type
== V4L2_BUF_TYPE_VBI_CAPTURE
)
1286 return RESOURCE_VBI
;
1292 static int video_open(struct inode
*inode
, struct file
*file
)
1294 int minor
= iminor(inode
);
1295 struct saa7134_dev
*dev
;
1296 struct saa7134_fh
*fh
;
1297 enum v4l2_buf_type type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1299 list_for_each_entry(dev
, &saa7134_devlist
, devlist
) {
1300 if (dev
->video_dev
&& (dev
->video_dev
->minor
== minor
))
1302 if (dev
->radio_dev
&& (dev
->radio_dev
->minor
== minor
)) {
1306 if (dev
->vbi_dev
&& (dev
->vbi_dev
->minor
== minor
)) {
1307 type
= V4L2_BUF_TYPE_VBI_CAPTURE
;
1314 dprintk("open minor=%d radio=%d type=%s\n",minor
,radio
,
1315 v4l2_type_names
[type
]);
1317 /* allocate + initialize per filehandle data */
1318 fh
= kzalloc(sizeof(*fh
),GFP_KERNEL
);
1321 file
->private_data
= fh
;
1325 fh
->fmt
= format_by_fourcc(V4L2_PIX_FMT_BGR24
);
1328 v4l2_prio_open(&dev
->prio
,&fh
->prio
);
1330 videobuf_queue_pci_init(&fh
->cap
, &video_qops
,
1331 dev
->pci
, &dev
->slock
,
1332 V4L2_BUF_TYPE_VIDEO_CAPTURE
,
1333 V4L2_FIELD_INTERLACED
,
1334 sizeof(struct saa7134_buf
),
1336 videobuf_queue_pci_init(&fh
->vbi
, &saa7134_vbi_qops
,
1337 dev
->pci
, &dev
->slock
,
1338 V4L2_BUF_TYPE_VBI_CAPTURE
,
1340 sizeof(struct saa7134_buf
),
1342 saa7134_pgtable_alloc(dev
->pci
,&fh
->pt_cap
);
1343 saa7134_pgtable_alloc(dev
->pci
,&fh
->pt_vbi
);
1346 /* switch to radio mode */
1347 saa7134_tvaudio_setinput(dev
,&card(dev
).radio
);
1348 saa7134_i2c_call_clients(dev
,AUDC_SET_RADIO
, NULL
);
1350 /* switch to video/vbi mode */
1351 video_mux(dev
,dev
->ctl_input
);
1357 video_read(struct file
*file
, char __user
*data
, size_t count
, loff_t
*ppos
)
1359 struct saa7134_fh
*fh
= file
->private_data
;
1362 case V4L2_BUF_TYPE_VIDEO_CAPTURE
:
1363 if (res_locked(fh
->dev
,RESOURCE_VIDEO
))
1365 return videobuf_read_one(saa7134_queue(fh
),
1367 file
->f_flags
& O_NONBLOCK
);
1368 case V4L2_BUF_TYPE_VBI_CAPTURE
:
1369 if (!res_get(fh
->dev
,fh
,RESOURCE_VBI
))
1371 return videobuf_read_stream(saa7134_queue(fh
),
1372 data
, count
, ppos
, 1,
1373 file
->f_flags
& O_NONBLOCK
);
1382 video_poll(struct file
*file
, struct poll_table_struct
*wait
)
1384 struct saa7134_fh
*fh
= file
->private_data
;
1385 struct videobuf_buffer
*buf
= NULL
;
1387 if (V4L2_BUF_TYPE_VBI_CAPTURE
== fh
->type
)
1388 return videobuf_poll_stream(file
, &fh
->vbi
, wait
);
1390 if (res_check(fh
,RESOURCE_VIDEO
)) {
1391 if (!list_empty(&fh
->cap
.stream
))
1392 buf
= list_entry(fh
->cap
.stream
.next
, struct videobuf_buffer
, stream
);
1394 mutex_lock(&fh
->cap
.lock
);
1395 if (UNSET
== fh
->cap
.read_off
) {
1396 /* need to capture a new frame */
1397 if (res_locked(fh
->dev
,RESOURCE_VIDEO
)) {
1398 mutex_unlock(&fh
->cap
.lock
);
1401 if (0 != fh
->cap
.ops
->buf_prepare(&fh
->cap
,fh
->cap
.read_buf
,fh
->cap
.field
)) {
1402 mutex_unlock(&fh
->cap
.lock
);
1405 fh
->cap
.ops
->buf_queue(&fh
->cap
,fh
->cap
.read_buf
);
1406 fh
->cap
.read_off
= 0;
1408 mutex_unlock(&fh
->cap
.lock
);
1409 buf
= fh
->cap
.read_buf
;
1415 poll_wait(file
, &buf
->done
, wait
);
1416 if (buf
->state
== STATE_DONE
||
1417 buf
->state
== STATE_ERROR
)
1418 return POLLIN
|POLLRDNORM
;
1422 static int video_release(struct inode
*inode
, struct file
*file
)
1424 struct saa7134_fh
*fh
= file
->private_data
;
1425 struct saa7134_dev
*dev
= fh
->dev
;
1426 unsigned long flags
;
1428 /* turn off overlay */
1429 if (res_check(fh
, RESOURCE_OVERLAY
)) {
1430 spin_lock_irqsave(&dev
->slock
,flags
);
1431 stop_preview(dev
,fh
);
1432 spin_unlock_irqrestore(&dev
->slock
,flags
);
1433 res_free(dev
,fh
,RESOURCE_OVERLAY
);
1436 /* stop video capture */
1437 if (res_check(fh
, RESOURCE_VIDEO
)) {
1438 videobuf_streamoff(&fh
->cap
);
1439 res_free(dev
,fh
,RESOURCE_VIDEO
);
1441 if (fh
->cap
.read_buf
) {
1442 buffer_release(&fh
->cap
,fh
->cap
.read_buf
);
1443 kfree(fh
->cap
.read_buf
);
1446 /* stop vbi capture */
1447 if (res_check(fh
, RESOURCE_VBI
)) {
1448 videobuf_stop(&fh
->vbi
);
1449 res_free(dev
,fh
,RESOURCE_VBI
);
1452 /* ts-capture will not work in planar mode, so turn it off Hac: 04.05*/
1453 saa_andorb(SAA7134_OFMT_VIDEO_A
, 0x1f, 0);
1454 saa_andorb(SAA7134_OFMT_VIDEO_B
, 0x1f, 0);
1455 saa_andorb(SAA7134_OFMT_DATA_A
, 0x1f, 0);
1456 saa_andorb(SAA7134_OFMT_DATA_B
, 0x1f, 0);
1458 saa7134_i2c_call_clients(dev
, TUNER_SET_STANDBY
, NULL
);
1461 videobuf_mmap_free(&fh
->cap
);
1462 videobuf_mmap_free(&fh
->vbi
);
1463 saa7134_pgtable_free(dev
->pci
,&fh
->pt_cap
);
1464 saa7134_pgtable_free(dev
->pci
,&fh
->pt_vbi
);
1466 v4l2_prio_close(&dev
->prio
,&fh
->prio
);
1467 file
->private_data
= NULL
;
1472 static int video_mmap(struct file
*file
, struct vm_area_struct
* vma
)
1474 struct saa7134_fh
*fh
= file
->private_data
;
1476 return videobuf_mmap_mapper(saa7134_queue(fh
), vma
);
1479 /* ------------------------------------------------------------------ */
1481 static void saa7134_vbi_fmt(struct saa7134_dev
*dev
, struct v4l2_format
*f
)
1483 struct saa7134_tvnorm
*norm
= dev
->tvnorm
;
1485 f
->fmt
.vbi
.sampling_rate
= 6750000 * 4;
1486 f
->fmt
.vbi
.samples_per_line
= 2048 /* VBI_LINE_LENGTH */;
1487 f
->fmt
.vbi
.sample_format
= V4L2_PIX_FMT_GREY
;
1488 f
->fmt
.vbi
.offset
= 64 * 4;
1489 f
->fmt
.vbi
.start
[0] = norm
->vbi_v_start_0
;
1490 f
->fmt
.vbi
.count
[0] = norm
->vbi_v_stop_0
- norm
->vbi_v_start_0
+1;
1491 f
->fmt
.vbi
.start
[1] = norm
->vbi_v_start_1
;
1492 f
->fmt
.vbi
.count
[1] = f
->fmt
.vbi
.count
[0];
1493 f
->fmt
.vbi
.flags
= 0; /* VBI_UNSYNC VBI_INTERLACED */
1497 static int saa7134_g_fmt(struct saa7134_dev
*dev
, struct saa7134_fh
*fh
,
1498 struct v4l2_format
*f
)
1501 case V4L2_BUF_TYPE_VIDEO_CAPTURE
:
1502 memset(&f
->fmt
.pix
,0,sizeof(f
->fmt
.pix
));
1503 f
->fmt
.pix
.width
= fh
->width
;
1504 f
->fmt
.pix
.height
= fh
->height
;
1505 f
->fmt
.pix
.field
= fh
->cap
.field
;
1506 f
->fmt
.pix
.pixelformat
= fh
->fmt
->fourcc
;
1507 f
->fmt
.pix
.bytesperline
=
1508 (f
->fmt
.pix
.width
* fh
->fmt
->depth
) >> 3;
1509 f
->fmt
.pix
.sizeimage
=
1510 f
->fmt
.pix
.height
* f
->fmt
.pix
.bytesperline
;
1512 case V4L2_BUF_TYPE_VIDEO_OVERLAY
:
1513 if (saa7134_no_overlay
> 0) {
1514 printk ("V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
1517 f
->fmt
.win
= fh
->win
;
1519 case V4L2_BUF_TYPE_VBI_CAPTURE
:
1520 saa7134_vbi_fmt(dev
,f
);
1527 static int saa7134_try_fmt(struct saa7134_dev
*dev
, struct saa7134_fh
*fh
,
1528 struct v4l2_format
*f
)
1533 case V4L2_BUF_TYPE_VIDEO_CAPTURE
:
1535 struct saa7134_format
*fmt
;
1536 enum v4l2_field field
;
1537 unsigned int maxw
, maxh
;
1539 fmt
= format_by_fourcc(f
->fmt
.pix
.pixelformat
);
1543 field
= f
->fmt
.pix
.field
;
1544 maxw
= min(dev
->crop_current
.width
*4, dev
->crop_bounds
.width
);
1545 maxh
= min(dev
->crop_current
.height
*4, dev
->crop_bounds
.height
);
1547 if (V4L2_FIELD_ANY
== field
) {
1548 field
= (f
->fmt
.pix
.height
> maxh
/2)
1549 ? V4L2_FIELD_INTERLACED
1550 : V4L2_FIELD_BOTTOM
;
1553 case V4L2_FIELD_TOP
:
1554 case V4L2_FIELD_BOTTOM
:
1557 case V4L2_FIELD_INTERLACED
:
1563 f
->fmt
.pix
.field
= field
;
1564 if (f
->fmt
.pix
.width
< 48)
1565 f
->fmt
.pix
.width
= 48;
1566 if (f
->fmt
.pix
.height
< 32)
1567 f
->fmt
.pix
.height
= 32;
1568 if (f
->fmt
.pix
.width
> maxw
)
1569 f
->fmt
.pix
.width
= maxw
;
1570 if (f
->fmt
.pix
.height
> maxh
)
1571 f
->fmt
.pix
.height
= maxh
;
1572 f
->fmt
.pix
.width
&= ~0x03;
1573 f
->fmt
.pix
.bytesperline
=
1574 (f
->fmt
.pix
.width
* fmt
->depth
) >> 3;
1575 f
->fmt
.pix
.sizeimage
=
1576 f
->fmt
.pix
.height
* f
->fmt
.pix
.bytesperline
;
1580 case V4L2_BUF_TYPE_VIDEO_OVERLAY
:
1581 if (saa7134_no_overlay
> 0) {
1582 printk ("V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
1585 err
= verify_preview(dev
,&f
->fmt
.win
);
1589 case V4L2_BUF_TYPE_VBI_CAPTURE
:
1590 saa7134_vbi_fmt(dev
,f
);
1597 static int saa7134_s_fmt(struct saa7134_dev
*dev
, struct saa7134_fh
*fh
,
1598 struct v4l2_format
*f
)
1600 unsigned long flags
;
1604 case V4L2_BUF_TYPE_VIDEO_CAPTURE
:
1605 err
= saa7134_try_fmt(dev
,fh
,f
);
1609 fh
->fmt
= format_by_fourcc(f
->fmt
.pix
.pixelformat
);
1610 fh
->width
= f
->fmt
.pix
.width
;
1611 fh
->height
= f
->fmt
.pix
.height
;
1612 fh
->cap
.field
= f
->fmt
.pix
.field
;
1614 case V4L2_BUF_TYPE_VIDEO_OVERLAY
:
1615 if (saa7134_no_overlay
> 0) {
1616 printk ("V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
1619 err
= verify_preview(dev
,&f
->fmt
.win
);
1623 mutex_lock(&dev
->lock
);
1624 fh
->win
= f
->fmt
.win
;
1625 fh
->nclips
= f
->fmt
.win
.clipcount
;
1628 if (copy_from_user(fh
->clips
,f
->fmt
.win
.clips
,
1629 sizeof(struct v4l2_clip
)*fh
->nclips
)) {
1630 mutex_unlock(&dev
->lock
);
1634 if (res_check(fh
, RESOURCE_OVERLAY
)) {
1635 spin_lock_irqsave(&dev
->slock
,flags
);
1636 stop_preview(dev
,fh
);
1637 start_preview(dev
,fh
);
1638 spin_unlock_irqrestore(&dev
->slock
,flags
);
1640 mutex_unlock(&dev
->lock
);
1642 case V4L2_BUF_TYPE_VBI_CAPTURE
:
1643 saa7134_vbi_fmt(dev
,f
);
1650 int saa7134_common_ioctl(struct saa7134_dev
*dev
,
1651 unsigned int cmd
, void *arg
)
1656 case VIDIOC_QUERYCTRL
:
1658 const struct v4l2_queryctrl
*ctrl
;
1659 struct v4l2_queryctrl
*c
= arg
;
1661 if ((c
->id
< V4L2_CID_BASE
||
1662 c
->id
>= V4L2_CID_LASTP1
) &&
1663 (c
->id
< V4L2_CID_PRIVATE_BASE
||
1664 c
->id
>= V4L2_CID_PRIVATE_LASTP1
))
1666 ctrl
= ctrl_by_id(c
->id
);
1667 *c
= (NULL
!= ctrl
) ? *ctrl
: no_ctrl
;
1671 return get_control(dev
,arg
);
1674 mutex_lock(&dev
->lock
);
1675 err
= set_control(dev
,NULL
,arg
);
1676 mutex_unlock(&dev
->lock
);
1679 /* --- input switching --------------------------------------- */
1680 case VIDIOC_ENUMINPUT
:
1682 struct v4l2_input
*i
= arg
;
1686 if (n
>= SAA7134_INPUT_MAX
)
1688 if (NULL
== card_in(dev
,i
->index
).name
)
1690 memset(i
,0,sizeof(*i
));
1692 i
->type
= V4L2_INPUT_TYPE_CAMERA
;
1693 strcpy(i
->name
,card_in(dev
,n
).name
);
1694 if (card_in(dev
,n
).tv
)
1695 i
->type
= V4L2_INPUT_TYPE_TUNER
;
1697 if (n
== dev
->ctl_input
) {
1698 int v1
= saa_readb(SAA7134_STATUS_VIDEO1
);
1699 int v2
= saa_readb(SAA7134_STATUS_VIDEO2
);
1701 if (0 != (v1
& 0x40))
1702 i
->status
|= V4L2_IN_ST_NO_H_LOCK
;
1703 if (0 != (v2
& 0x40))
1704 i
->status
|= V4L2_IN_ST_NO_SYNC
;
1705 if (0 != (v2
& 0x0e))
1706 i
->status
|= V4L2_IN_ST_MACROVISION
;
1708 for (n
= 0; n
< TVNORMS
; n
++)
1709 i
->std
|= tvnorms
[n
].id
;
1712 case VIDIOC_G_INPUT
:
1715 *i
= dev
->ctl_input
;
1718 case VIDIOC_S_INPUT
:
1722 if (*i
< 0 || *i
>= SAA7134_INPUT_MAX
)
1724 if (NULL
== card_in(dev
,*i
).name
)
1726 mutex_lock(&dev
->lock
);
1728 mutex_unlock(&dev
->lock
);
1735 EXPORT_SYMBOL(saa7134_common_ioctl
);
1738 * This function is _not_ called directly, but from
1739 * video_generic_ioctl (and maybe others). userspace
1740 * copying is done already, arg is a kernel pointer.
1742 static int video_do_ioctl(struct inode
*inode
, struct file
*file
,
1743 unsigned int cmd
, void *arg
)
1745 struct saa7134_fh
*fh
= file
->private_data
;
1746 struct saa7134_dev
*dev
= fh
->dev
;
1747 unsigned long flags
;
1750 if (video_debug
> 1)
1751 v4l_print_ioctl(dev
->name
,cmd
);
1756 case VIDIOC_S_INPUT
:
1757 case VIDIOC_S_TUNER
:
1758 case VIDIOC_S_FREQUENCY
:
1759 err
= v4l2_prio_check(&dev
->prio
,&fh
->prio
);
1765 case VIDIOC_QUERYCAP
:
1767 struct v4l2_capability
*cap
= arg
;
1768 unsigned int tuner_type
= dev
->tuner_type
;
1770 memset(cap
,0,sizeof(*cap
));
1771 strcpy(cap
->driver
, "saa7134");
1772 strlcpy(cap
->card
, saa7134_boards
[dev
->board
].name
,
1774 sprintf(cap
->bus_info
,"PCI:%s",pci_name(dev
->pci
));
1775 cap
->version
= SAA7134_VERSION_CODE
;
1777 V4L2_CAP_VIDEO_CAPTURE
|
1778 V4L2_CAP_VBI_CAPTURE
|
1779 V4L2_CAP_READWRITE
|
1780 V4L2_CAP_STREAMING
|
1782 if (saa7134_no_overlay
<= 0) {
1783 cap
->capabilities
|= V4L2_CAP_VIDEO_OVERLAY
;
1786 if ((tuner_type
== TUNER_ABSENT
) || (tuner_type
== UNSET
))
1787 cap
->capabilities
&= ~V4L2_CAP_TUNER
;
1792 /* --- tv standards ------------------------------------------ */
1793 case VIDIOC_ENUMSTD
:
1795 struct v4l2_standard
*e
= arg
;
1801 err
= v4l2_video_std_construct(e
, tvnorms
[e
->index
].id
,
1802 tvnorms
[e
->index
].name
);
1810 v4l2_std_id
*id
= arg
;
1812 *id
= dev
->tvnorm
->id
;
1817 v4l2_std_id
*id
= arg
;
1821 for (i
= 0; i
< TVNORMS
; i
++)
1822 if (*id
== tvnorms
[i
].id
)
1825 for (i
= 0; i
< TVNORMS
; i
++)
1826 if (*id
& tvnorms
[i
].id
)
1830 if ((*id
& V4L2_STD_SECAM
) && (secam
[0] != '-')) {
1831 if (secam
[0] == 'L' || secam
[0] == 'l') {
1832 if (secam
[1] == 'C' || secam
[1] == 'c')
1833 fixup
= V4L2_STD_SECAM_LC
;
1835 fixup
= V4L2_STD_SECAM_L
;
1837 if (secam
[0] == 'D' || secam
[0] == 'd')
1838 fixup
= V4L2_STD_SECAM_DK
;
1840 fixup
= V4L2_STD_SECAM
;
1842 for (i
= 0; i
< TVNORMS
; i
++)
1843 if (fixup
== tvnorms
[i
].id
)
1846 mutex_lock(&dev
->lock
);
1847 if (res_check(fh
, RESOURCE_OVERLAY
)) {
1848 spin_lock_irqsave(&dev
->slock
,flags
);
1849 stop_preview(dev
,fh
);
1850 spin_unlock_irqrestore(&dev
->slock
, flags
);
1852 set_tvnorm(dev
,&tvnorms
[i
]);
1854 spin_lock_irqsave(&dev
->slock
, flags
);
1855 start_preview(dev
,fh
);
1856 spin_unlock_irqrestore(&dev
->slock
,flags
);
1858 set_tvnorm(dev
,&tvnorms
[i
]);
1859 saa7134_tvaudio_do_scan(dev
);
1860 mutex_unlock(&dev
->lock
);
1864 case VIDIOC_CROPCAP
:
1866 struct v4l2_cropcap
*cap
= arg
;
1868 if (cap
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
&&
1869 cap
->type
!= V4L2_BUF_TYPE_VIDEO_OVERLAY
)
1871 cap
->bounds
= dev
->crop_bounds
;
1872 cap
->defrect
= dev
->crop_defrect
;
1873 cap
->pixelaspect
.numerator
= 1;
1874 cap
->pixelaspect
.denominator
= 1;
1875 if (dev
->tvnorm
->id
& V4L2_STD_525_60
) {
1876 cap
->pixelaspect
.numerator
= 11;
1877 cap
->pixelaspect
.denominator
= 10;
1879 if (dev
->tvnorm
->id
& V4L2_STD_625_50
) {
1880 cap
->pixelaspect
.numerator
= 54;
1881 cap
->pixelaspect
.denominator
= 59;
1888 struct v4l2_crop
* crop
= arg
;
1890 if (crop
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
&&
1891 crop
->type
!= V4L2_BUF_TYPE_VIDEO_OVERLAY
)
1893 crop
->c
= dev
->crop_current
;
1898 struct v4l2_crop
*crop
= arg
;
1899 struct v4l2_rect
*b
= &dev
->crop_bounds
;
1901 if (crop
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
&&
1902 crop
->type
!= V4L2_BUF_TYPE_VIDEO_OVERLAY
)
1904 if (crop
->c
.height
< 0)
1906 if (crop
->c
.width
< 0)
1909 if (res_locked(fh
->dev
,RESOURCE_OVERLAY
))
1911 if (res_locked(fh
->dev
,RESOURCE_VIDEO
))
1914 if (crop
->c
.top
< b
->top
)
1915 crop
->c
.top
= b
->top
;
1916 if (crop
->c
.top
> b
->top
+ b
->height
)
1917 crop
->c
.top
= b
->top
+ b
->height
;
1918 if (crop
->c
.height
> b
->top
- crop
->c
.top
+ b
->height
)
1919 crop
->c
.height
= b
->top
- crop
->c
.top
+ b
->height
;
1921 if (crop
->c
.left
< b
->left
)
1922 crop
->c
.left
= b
->left
;
1923 if (crop
->c
.left
> b
->left
+ b
->width
)
1924 crop
->c
.left
= b
->left
+ b
->width
;
1925 if (crop
->c
.width
> b
->left
- crop
->c
.left
+ b
->width
)
1926 crop
->c
.width
= b
->left
- crop
->c
.left
+ b
->width
;
1928 dev
->crop_current
= crop
->c
;
1932 /* --- tuner ioctls ------------------------------------------ */
1933 case VIDIOC_G_TUNER
:
1935 struct v4l2_tuner
*t
= arg
;
1940 memset(t
,0,sizeof(*t
));
1941 for (n
= 0; n
< SAA7134_INPUT_MAX
; n
++)
1942 if (card_in(dev
,n
).tv
)
1944 if (NULL
!= card_in(dev
,n
).name
) {
1945 strcpy(t
->name
, "Television");
1946 t
->type
= V4L2_TUNER_ANALOG_TV
;
1947 t
->capability
= V4L2_TUNER_CAP_NORM
|
1948 V4L2_TUNER_CAP_STEREO
|
1949 V4L2_TUNER_CAP_LANG1
|
1950 V4L2_TUNER_CAP_LANG2
;
1951 t
->rangehigh
= 0xffffffffUL
;
1952 t
->rxsubchans
= saa7134_tvaudio_getstereo(dev
);
1953 t
->audmode
= saa7134_tvaudio_rx2mode(t
->rxsubchans
);
1955 if (0 != (saa_readb(SAA7134_STATUS_VIDEO1
) & 0x03))
1959 case VIDIOC_S_TUNER
:
1961 struct v4l2_tuner
*t
= arg
;
1964 mode
= dev
->thread
.mode
;
1965 if (UNSET
== mode
) {
1966 rx
= saa7134_tvaudio_getstereo(dev
);
1967 mode
= saa7134_tvaudio_rx2mode(t
->rxsubchans
);
1969 if (mode
!= t
->audmode
) {
1970 dev
->thread
.mode
= t
->audmode
;
1974 case VIDIOC_G_FREQUENCY
:
1976 struct v4l2_frequency
*f
= arg
;
1978 memset(f
,0,sizeof(*f
));
1979 f
->type
= fh
->radio
? V4L2_TUNER_RADIO
: V4L2_TUNER_ANALOG_TV
;
1980 f
->frequency
= dev
->ctl_freq
;
1983 case VIDIOC_S_FREQUENCY
:
1985 struct v4l2_frequency
*f
= arg
;
1989 if (0 == fh
->radio
&& V4L2_TUNER_ANALOG_TV
!= f
->type
)
1991 if (1 == fh
->radio
&& V4L2_TUNER_RADIO
!= f
->type
)
1993 mutex_lock(&dev
->lock
);
1994 dev
->ctl_freq
= f
->frequency
;
1996 saa7134_i2c_call_clients(dev
,VIDIOC_S_FREQUENCY
,f
);
1998 saa7134_tvaudio_do_scan(dev
);
1999 mutex_unlock(&dev
->lock
);
2003 /* --- control ioctls ---------------------------------------- */
2004 case VIDIOC_ENUMINPUT
:
2005 case VIDIOC_G_INPUT
:
2006 case VIDIOC_S_INPUT
:
2007 case VIDIOC_QUERYCTRL
:
2010 return saa7134_common_ioctl(dev
, cmd
, arg
);
2012 case VIDIOC_G_AUDIO
:
2014 struct v4l2_audio
*a
= arg
;
2016 memset(a
,0,sizeof(*a
));
2017 strcpy(a
->name
,"audio");
2020 case VIDIOC_S_AUDIO
:
2024 struct v4l2_captureparm
*parm
= arg
;
2025 memset(parm
,0,sizeof(*parm
));
2029 case VIDIOC_G_PRIORITY
:
2031 enum v4l2_priority
*p
= arg
;
2033 *p
= v4l2_prio_max(&dev
->prio
);
2036 case VIDIOC_S_PRIORITY
:
2038 enum v4l2_priority
*prio
= arg
;
2040 return v4l2_prio_change(&dev
->prio
, &fh
->prio
, *prio
);
2043 /* --- preview ioctls ---------------------------------------- */
2044 case VIDIOC_ENUM_FMT
:
2046 struct v4l2_fmtdesc
*f
= arg
;
2047 enum v4l2_buf_type type
;
2053 case V4L2_BUF_TYPE_VIDEO_CAPTURE
:
2054 case V4L2_BUF_TYPE_VIDEO_OVERLAY
:
2055 if (saa7134_no_overlay
> 0) {
2056 printk ("V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
2059 if (index
>= FORMATS
)
2061 if (f
->type
== V4L2_BUF_TYPE_VIDEO_OVERLAY
&&
2062 formats
[index
].planar
)
2064 memset(f
,0,sizeof(*f
));
2067 strlcpy(f
->description
,formats
[index
].name
,sizeof(f
->description
));
2068 f
->pixelformat
= formats
[index
].fourcc
;
2070 case V4L2_BUF_TYPE_VBI_CAPTURE
:
2073 memset(f
,0,sizeof(*f
));
2076 f
->pixelformat
= V4L2_PIX_FMT_GREY
;
2077 strcpy(f
->description
,"vbi data");
2086 struct v4l2_framebuffer
*fb
= arg
;
2089 fb
->capability
= V4L2_FBUF_CAP_LIST_CLIPPING
;
2094 struct v4l2_framebuffer
*fb
= arg
;
2095 struct saa7134_format
*fmt
;
2097 if(!capable(CAP_SYS_ADMIN
) &&
2098 !capable(CAP_SYS_RAWIO
))
2102 fmt
= format_by_fourcc(fb
->fmt
.pixelformat
);
2109 if (0 == dev
->ovbuf
.fmt
.bytesperline
)
2110 dev
->ovbuf
.fmt
.bytesperline
=
2111 dev
->ovbuf
.fmt
.width
*fmt
->depth
/8;
2114 case VIDIOC_OVERLAY
:
2119 if (saa7134_no_overlay
> 0) {
2120 printk ("no_overlay\n");
2124 if (!res_get(dev
,fh
,RESOURCE_OVERLAY
))
2126 spin_lock_irqsave(&dev
->slock
,flags
);
2127 start_preview(dev
,fh
);
2128 spin_unlock_irqrestore(&dev
->slock
,flags
);
2131 if (!res_check(fh
, RESOURCE_OVERLAY
))
2133 spin_lock_irqsave(&dev
->slock
,flags
);
2134 stop_preview(dev
,fh
);
2135 spin_unlock_irqrestore(&dev
->slock
,flags
);
2136 res_free(dev
,fh
,RESOURCE_OVERLAY
);
2141 /* --- capture ioctls ---------------------------------------- */
2144 struct v4l2_format
*f
= arg
;
2145 return saa7134_g_fmt(dev
,fh
,f
);
2149 struct v4l2_format
*f
= arg
;
2150 return saa7134_s_fmt(dev
,fh
,f
);
2152 case VIDIOC_TRY_FMT
:
2154 struct v4l2_format
*f
= arg
;
2155 return saa7134_try_fmt(dev
,fh
,f
);
2157 #ifdef CONFIG_VIDEO_V4L1_COMPAT
2159 return videobuf_cgmbuf(saa7134_queue(fh
), arg
, gbuffers
);
2161 case VIDIOC_REQBUFS
:
2162 return videobuf_reqbufs(saa7134_queue(fh
),arg
);
2164 case VIDIOC_QUERYBUF
:
2165 return videobuf_querybuf(saa7134_queue(fh
),arg
);
2168 return videobuf_qbuf(saa7134_queue(fh
),arg
);
2171 return videobuf_dqbuf(saa7134_queue(fh
),arg
,
2172 file
->f_flags
& O_NONBLOCK
);
2174 case VIDIOC_STREAMON
:
2176 int res
= saa7134_resource(fh
);
2178 if (!res_get(dev
,fh
,res
))
2180 return videobuf_streamon(saa7134_queue(fh
));
2182 case VIDIOC_STREAMOFF
:
2184 int res
= saa7134_resource(fh
);
2186 err
= videobuf_streamoff(saa7134_queue(fh
));
2189 res_free(dev
,fh
,res
);
2194 return v4l_compat_translate_ioctl(inode
,file
,cmd
,arg
,
2200 static int video_ioctl(struct inode
*inode
, struct file
*file
,
2201 unsigned int cmd
, unsigned long arg
)
2203 return video_usercopy(inode
, file
, cmd
, arg
, video_do_ioctl
);
2206 static int radio_do_ioctl(struct inode
*inode
, struct file
*file
,
2207 unsigned int cmd
, void *arg
)
2209 struct saa7134_fh
*fh
= file
->private_data
;
2210 struct saa7134_dev
*dev
= fh
->dev
;
2212 if (video_debug
> 1)
2213 v4l_print_ioctl(dev
->name
,cmd
);
2215 case VIDIOC_QUERYCAP
:
2217 struct v4l2_capability
*cap
= arg
;
2219 memset(cap
,0,sizeof(*cap
));
2220 strcpy(cap
->driver
, "saa7134");
2221 strlcpy(cap
->card
, saa7134_boards
[dev
->board
].name
,
2223 sprintf(cap
->bus_info
,"PCI:%s",pci_name(dev
->pci
));
2224 cap
->version
= SAA7134_VERSION_CODE
;
2225 cap
->capabilities
= V4L2_CAP_TUNER
;
2228 case VIDIOC_G_TUNER
:
2230 struct v4l2_tuner
*t
= arg
;
2235 memset(t
,0,sizeof(*t
));
2236 strcpy(t
->name
, "Radio");
2237 t
->type
= V4L2_TUNER_RADIO
;
2239 saa7134_i2c_call_clients(dev
, VIDIOC_G_TUNER
, t
);
2240 if (dev
->input
->amux
== TV
) {
2241 t
->signal
= 0xf800 - ((saa_readb(0x581) & 0x1f) << 11);
2242 t
->rxsubchans
= (saa_readb(0x529) & 0x08) ?
2243 V4L2_TUNER_SUB_STEREO
: V4L2_TUNER_SUB_MONO
;
2247 case VIDIOC_S_TUNER
:
2249 struct v4l2_tuner
*t
= arg
;
2254 saa7134_i2c_call_clients(dev
,VIDIOC_S_TUNER
,t
);
2258 case VIDIOC_ENUMINPUT
:
2260 struct v4l2_input
*i
= arg
;
2264 strcpy(i
->name
,"Radio");
2265 i
->type
= V4L2_INPUT_TYPE_TUNER
;
2268 case VIDIOC_G_INPUT
:
2274 case VIDIOC_G_AUDIO
:
2276 struct v4l2_audio
*a
= arg
;
2278 memset(a
,0,sizeof(*a
));
2279 strcpy(a
->name
,"Radio");
2284 v4l2_std_id
*id
= arg
;
2288 case VIDIOC_S_AUDIO
:
2289 case VIDIOC_S_INPUT
:
2293 case VIDIOC_QUERYCTRL
:
2295 const struct v4l2_queryctrl
*ctrl
;
2296 struct v4l2_queryctrl
*c
= arg
;
2298 if (c
->id
< V4L2_CID_BASE
||
2299 c
->id
>= V4L2_CID_LASTP1
)
2301 if (c
->id
== V4L2_CID_AUDIO_MUTE
) {
2302 ctrl
= ctrl_by_id(c
->id
);
2311 case VIDIOC_G_FREQUENCY
:
2312 case VIDIOC_S_FREQUENCY
:
2313 return video_do_ioctl(inode
,file
,cmd
,arg
);
2316 return v4l_compat_translate_ioctl(inode
,file
,cmd
,arg
,
2322 static int radio_ioctl(struct inode
*inode
, struct file
*file
,
2323 unsigned int cmd
, unsigned long arg
)
2325 return video_usercopy(inode
, file
, cmd
, arg
, radio_do_ioctl
);
2328 static const struct file_operations video_fops
=
2330 .owner
= THIS_MODULE
,
2332 .release
= video_release
,
2336 .ioctl
= video_ioctl
,
2337 .compat_ioctl
= v4l_compat_ioctl32
,
2338 .llseek
= no_llseek
,
2341 static const struct file_operations radio_fops
=
2343 .owner
= THIS_MODULE
,
2345 .release
= video_release
,
2346 .ioctl
= radio_ioctl
,
2347 .compat_ioctl
= v4l_compat_ioctl32
,
2348 .llseek
= no_llseek
,
2351 /* ----------------------------------------------------------- */
2352 /* exported stuff */
2354 struct video_device saa7134_video_template
=
2356 .name
= "saa7134-video",
2357 .type
= VID_TYPE_CAPTURE
|VID_TYPE_TUNER
|
2358 VID_TYPE_CLIPPING
|VID_TYPE_SCALES
,
2359 .fops
= &video_fops
,
2363 struct video_device saa7134_vbi_template
=
2365 .name
= "saa7134-vbi",
2366 .type
= VID_TYPE_TUNER
|VID_TYPE_TELETEXT
,
2367 .fops
= &video_fops
,
2371 struct video_device saa7134_radio_template
=
2373 .name
= "saa7134-radio",
2374 .type
= VID_TYPE_TUNER
,
2375 .fops
= &radio_fops
,
2379 int saa7134_video_init1(struct saa7134_dev
*dev
)
2381 /* sanitycheck insmod options */
2382 if (gbuffers
< 2 || gbuffers
> VIDEO_MAX_FRAME
)
2384 if (gbufsize
< 0 || gbufsize
> gbufsize_max
)
2385 gbufsize
= gbufsize_max
;
2386 gbufsize
= (gbufsize
+ PAGE_SIZE
- 1) & PAGE_MASK
;
2388 /* put some sensible defaults into the data structures ... */
2389 dev
->ctl_bright
= ctrl_by_id(V4L2_CID_BRIGHTNESS
)->default_value
;
2390 dev
->ctl_contrast
= ctrl_by_id(V4L2_CID_CONTRAST
)->default_value
;
2391 dev
->ctl_hue
= ctrl_by_id(V4L2_CID_HUE
)->default_value
;
2392 dev
->ctl_saturation
= ctrl_by_id(V4L2_CID_SATURATION
)->default_value
;
2393 dev
->ctl_volume
= ctrl_by_id(V4L2_CID_AUDIO_VOLUME
)->default_value
;
2394 dev
->ctl_mute
= 1; // ctrl_by_id(V4L2_CID_AUDIO_MUTE)->default_value;
2395 dev
->ctl_invert
= ctrl_by_id(V4L2_CID_PRIVATE_INVERT
)->default_value
;
2396 dev
->ctl_automute
= ctrl_by_id(V4L2_CID_PRIVATE_AUTOMUTE
)->default_value
;
2398 if (dev
->tda9887_conf
&& dev
->ctl_automute
)
2399 dev
->tda9887_conf
|= TDA9887_AUTOMUTE
;
2402 INIT_LIST_HEAD(&dev
->video_q
.queue
);
2403 init_timer(&dev
->video_q
.timeout
);
2404 dev
->video_q
.timeout
.function
= saa7134_buffer_timeout
;
2405 dev
->video_q
.timeout
.data
= (unsigned long)(&dev
->video_q
);
2406 dev
->video_q
.dev
= dev
;
2408 if (saa7134_boards
[dev
->board
].video_out
)
2409 saa7134_videoport_init(dev
);
2414 int saa7134_videoport_init(struct saa7134_dev
*dev
)
2416 /* enable video output */
2417 int vo
= saa7134_boards
[dev
->board
].video_out
;
2419 unsigned int vid_port_opts
= saa7134_boards
[dev
->board
].vid_port_opts
;
2420 saa_writeb(SAA7134_VIDEO_PORT_CTRL0
, video_out
[vo
][0]);
2421 video_reg
= video_out
[vo
][1];
2422 if (vid_port_opts
& SET_T_CODE_POLARITY_NON_INVERTED
)
2423 video_reg
&= ~VP_T_CODE_P_INVERTED
;
2424 saa_writeb(SAA7134_VIDEO_PORT_CTRL1
, video_reg
);
2425 saa_writeb(SAA7134_VIDEO_PORT_CTRL2
, video_out
[vo
][2]);
2426 saa_writeb(SAA7134_VIDEO_PORT_CTRL3
, video_out
[vo
][3]);
2427 saa_writeb(SAA7134_VIDEO_PORT_CTRL4
, video_out
[vo
][4]);
2428 video_reg
= video_out
[vo
][5];
2429 if (vid_port_opts
& SET_CLOCK_NOT_DELAYED
)
2430 video_reg
&= ~VP_CLK_CTRL2_DELAYED
;
2431 if (vid_port_opts
& SET_CLOCK_INVERTED
)
2432 video_reg
|= VP_CLK_CTRL1_INVERTED
;
2433 saa_writeb(SAA7134_VIDEO_PORT_CTRL5
, video_reg
);
2434 video_reg
= video_out
[vo
][6];
2435 if (vid_port_opts
& SET_VSYNC_OFF
) {
2436 video_reg
&= ~VP_VS_TYPE_MASK
;
2437 video_reg
|= VP_VS_TYPE_OFF
;
2439 saa_writeb(SAA7134_VIDEO_PORT_CTRL6
, video_reg
);
2440 saa_writeb(SAA7134_VIDEO_PORT_CTRL7
, video_out
[vo
][7]);
2441 saa_writeb(SAA7134_VIDEO_PORT_CTRL8
, video_out
[vo
][8]);
2446 int saa7134_video_init2(struct saa7134_dev
*dev
)
2449 set_tvnorm(dev
,&tvnorms
[0]);
2451 saa7134_tvaudio_setmute(dev
);
2452 saa7134_tvaudio_setvolume(dev
,dev
->ctl_volume
);
2456 void saa7134_irq_video_signalchange(struct saa7134_dev
*dev
)
2458 static const char *st
[] = {
2459 "(no signal)", "NTSC", "PAL", "SECAM" };
2462 st1
= saa_readb(SAA7134_STATUS_VIDEO1
);
2463 st2
= saa_readb(SAA7134_STATUS_VIDEO2
);
2464 dprintk("DCSDT: pll: %s, sync: %s, norm: %s\n",
2465 (st1
& 0x40) ? "not locked" : "locked",
2466 (st2
& 0x40) ? "no" : "yes",
2468 dev
->nosignal
= (st1
& 0x40) || (st2
& 0x40) || !(st2
& 0x1);
2470 if (dev
->nosignal
) {
2471 /* no video signal -> mute audio */
2472 if (dev
->ctl_automute
)
2474 saa7134_tvaudio_setmute(dev
);
2476 /* wake up tvaudio audio carrier scan thread */
2477 saa7134_tvaudio_do_scan(dev
);
2480 if ((st2
& 0x80) && !noninterlaced
&& !dev
->nosignal
)
2481 saa_clearb(SAA7134_SYNC_CTRL
, 0x20);
2483 saa_setb(SAA7134_SYNC_CTRL
, 0x20);
2485 if (dev
->mops
&& dev
->mops
->signal_change
)
2486 dev
->mops
->signal_change(dev
);
2490 void saa7134_irq_video_done(struct saa7134_dev
*dev
, unsigned long status
)
2492 enum v4l2_field field
;
2494 spin_lock(&dev
->slock
);
2495 if (dev
->video_q
.curr
) {
2496 dev
->video_fieldcount
++;
2497 field
= dev
->video_q
.curr
->vb
.field
;
2498 if (V4L2_FIELD_HAS_BOTH(field
)) {
2499 /* make sure we have seen both fields */
2500 if ((status
& 0x10) == 0x00) {
2501 dev
->video_q
.curr
->top_seen
= 1;
2504 if (!dev
->video_q
.curr
->top_seen
)
2506 } else if (field
== V4L2_FIELD_TOP
) {
2507 if ((status
& 0x10) != 0x10)
2509 } else if (field
== V4L2_FIELD_BOTTOM
) {
2510 if ((status
& 0x10) != 0x00)
2513 dev
->video_q
.curr
->vb
.field_count
= dev
->video_fieldcount
;
2514 saa7134_buffer_finish(dev
,&dev
->video_q
,STATE_DONE
);
2516 saa7134_buffer_next(dev
,&dev
->video_q
);
2519 spin_unlock(&dev
->slock
);
2522 /* ----------------------------------------------------------- */