3 * device driver for philips saa7134 based TV cards
4 * video4linux video interface
6 * (c) 2001-03 Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #include <linux/init.h>
24 #include <linux/list.h>
25 #include <linux/module.h>
26 #include <linux/kernel.h>
27 #include <linux/slab.h>
28 #include <linux/sort.h>
30 #include "saa7134-reg.h"
32 #include <media/v4l2-common.h>
33 #include <media/saa6588.h>
35 /* ------------------------------------------------------------------ */
37 unsigned int video_debug
;
38 static unsigned int gbuffers
= 8;
39 static unsigned int noninterlaced
; /* 0 */
40 static unsigned int gbufsize
= 720*576*4;
41 static unsigned int gbufsize_max
= 720*576*4;
42 static char secam
[] = "--";
43 module_param(video_debug
, int, 0644);
44 MODULE_PARM_DESC(video_debug
,"enable debug messages [video]");
45 module_param(gbuffers
, int, 0444);
46 MODULE_PARM_DESC(gbuffers
,"number of capture buffers, range 2-32");
47 module_param(noninterlaced
, int, 0644);
48 MODULE_PARM_DESC(noninterlaced
,"capture non interlaced video");
49 module_param_string(secam
, secam
, sizeof(secam
), 0644);
50 MODULE_PARM_DESC(secam
, "force SECAM variant, either DK,L or Lc");
53 #define dprintk(fmt, arg...) if (video_debug&0x04) \
54 printk(KERN_DEBUG "%s/video: " fmt, dev->name , ## arg)
56 /* ------------------------------------------------------------------ */
57 /* Defines for Video Output Port Register at address 0x191 */
59 /* Bit 0: VIP code T bit polarity */
61 #define VP_T_CODE_P_NON_INVERTED 0x00
62 #define VP_T_CODE_P_INVERTED 0x01
64 /* ------------------------------------------------------------------ */
65 /* Defines for Video Output Port Register at address 0x195 */
67 /* Bit 2: Video output clock delay control */
69 #define VP_CLK_CTRL2_NOT_DELAYED 0x00
70 #define VP_CLK_CTRL2_DELAYED 0x04
72 /* Bit 1: Video output clock invert control */
74 #define VP_CLK_CTRL1_NON_INVERTED 0x00
75 #define VP_CLK_CTRL1_INVERTED 0x02
77 /* ------------------------------------------------------------------ */
78 /* Defines for Video Output Port Register at address 0x196 */
80 /* Bits 2 to 0: VSYNC pin video vertical sync type */
82 #define VP_VS_TYPE_MASK 0x07
84 #define VP_VS_TYPE_OFF 0x00
85 #define VP_VS_TYPE_V123 0x01
86 #define VP_VS_TYPE_V_ITU 0x02
87 #define VP_VS_TYPE_VGATE_L 0x03
88 #define VP_VS_TYPE_RESERVED1 0x04
89 #define VP_VS_TYPE_RESERVED2 0x05
90 #define VP_VS_TYPE_F_ITU 0x06
91 #define VP_VS_TYPE_SC_FID 0x07
93 /* ------------------------------------------------------------------ */
94 /* data structs for video */
96 static int video_out
[][9] = {
97 [CCIR656
] = { 0x00, 0xb1, 0x00, 0xa1, 0x00, 0x04, 0x06, 0x00, 0x00 },
100 static struct saa7134_format formats
[] = {
102 .name
= "8 bpp gray",
103 .fourcc
= V4L2_PIX_FMT_GREY
,
107 .name
= "15 bpp RGB, le",
108 .fourcc
= V4L2_PIX_FMT_RGB555
,
112 .name
= "15 bpp RGB, be",
113 .fourcc
= V4L2_PIX_FMT_RGB555X
,
118 .name
= "16 bpp RGB, le",
119 .fourcc
= V4L2_PIX_FMT_RGB565
,
123 .name
= "16 bpp RGB, be",
124 .fourcc
= V4L2_PIX_FMT_RGB565X
,
129 .name
= "24 bpp RGB, le",
130 .fourcc
= V4L2_PIX_FMT_BGR24
,
134 .name
= "24 bpp RGB, be",
135 .fourcc
= V4L2_PIX_FMT_RGB24
,
140 .name
= "32 bpp RGB, le",
141 .fourcc
= V4L2_PIX_FMT_BGR32
,
145 .name
= "32 bpp RGB, be",
146 .fourcc
= V4L2_PIX_FMT_RGB32
,
152 .name
= "4:2:2 packed, YUYV",
153 .fourcc
= V4L2_PIX_FMT_YUYV
,
159 .name
= "4:2:2 packed, UYVY",
160 .fourcc
= V4L2_PIX_FMT_UYVY
,
165 .name
= "4:2:2 planar, Y-Cb-Cr",
166 .fourcc
= V4L2_PIX_FMT_YUV422P
,
174 .name
= "4:2:0 planar, Y-Cb-Cr",
175 .fourcc
= V4L2_PIX_FMT_YUV420
,
183 .name
= "4:2:0 planar, Y-Cb-Cr",
184 .fourcc
= V4L2_PIX_FMT_YVU420
,
194 #define FORMATS ARRAY_SIZE(formats)
196 #define NORM_625_50 \
199 .video_v_start = 24, \
200 .video_v_stop = 311, \
201 .vbi_v_start_0 = 7, \
202 .vbi_v_stop_0 = 22, \
203 .vbi_v_start_1 = 319, \
206 #define NORM_525_60 \
209 .video_v_start = 23, \
210 .video_v_stop = 262, \
211 .vbi_v_start_0 = 10, \
212 .vbi_v_stop_0 = 21, \
213 .vbi_v_start_1 = 273, \
216 static struct saa7134_tvnorm tvnorms
[] = {
218 .name
= "PAL", /* autodetect */
222 .sync_control
= 0x18,
223 .luma_control
= 0x40,
224 .chroma_ctrl1
= 0x81,
226 .chroma_ctrl2
= 0x06,
231 .id
= V4L2_STD_PAL_BG
,
234 .sync_control
= 0x18,
235 .luma_control
= 0x40,
236 .chroma_ctrl1
= 0x81,
238 .chroma_ctrl2
= 0x06,
243 .id
= V4L2_STD_PAL_I
,
246 .sync_control
= 0x18,
247 .luma_control
= 0x40,
248 .chroma_ctrl1
= 0x81,
250 .chroma_ctrl2
= 0x06,
255 .id
= V4L2_STD_PAL_DK
,
258 .sync_control
= 0x18,
259 .luma_control
= 0x40,
260 .chroma_ctrl1
= 0x81,
262 .chroma_ctrl2
= 0x06,
270 .sync_control
= 0x59,
271 .luma_control
= 0x40,
272 .chroma_ctrl1
= 0x89,
274 .chroma_ctrl2
= 0x0e,
279 .id
= V4L2_STD_SECAM
,
282 .sync_control
= 0x18,
283 .luma_control
= 0x1b,
284 .chroma_ctrl1
= 0xd1,
286 .chroma_ctrl2
= 0x00,
291 .id
= V4L2_STD_SECAM_DK
,
294 .sync_control
= 0x18,
295 .luma_control
= 0x1b,
296 .chroma_ctrl1
= 0xd1,
298 .chroma_ctrl2
= 0x00,
303 .id
= V4L2_STD_SECAM_L
,
306 .sync_control
= 0x18,
307 .luma_control
= 0x1b,
308 .chroma_ctrl1
= 0xd1,
310 .chroma_ctrl2
= 0x00,
315 .id
= V4L2_STD_SECAM_LC
,
318 .sync_control
= 0x18,
319 .luma_control
= 0x1b,
320 .chroma_ctrl1
= 0xd1,
322 .chroma_ctrl2
= 0x00,
327 .id
= V4L2_STD_PAL_M
,
330 .sync_control
= 0x59,
331 .luma_control
= 0x40,
332 .chroma_ctrl1
= 0xb9,
334 .chroma_ctrl2
= 0x0e,
339 .id
= V4L2_STD_PAL_Nc
,
342 .sync_control
= 0x18,
343 .luma_control
= 0x40,
344 .chroma_ctrl1
= 0xa1,
346 .chroma_ctrl2
= 0x06,
351 .id
= V4L2_STD_PAL_60
,
359 .vbi_v_start_1
= 273,
362 .sync_control
= 0x18,
363 .luma_control
= 0x40,
364 .chroma_ctrl1
= 0x81,
366 .chroma_ctrl2
= 0x06,
370 #define TVNORMS ARRAY_SIZE(tvnorms)
372 #define V4L2_CID_PRIVATE_INVERT (V4L2_CID_PRIVATE_BASE + 0)
373 #define V4L2_CID_PRIVATE_Y_ODD (V4L2_CID_PRIVATE_BASE + 1)
374 #define V4L2_CID_PRIVATE_Y_EVEN (V4L2_CID_PRIVATE_BASE + 2)
375 #define V4L2_CID_PRIVATE_AUTOMUTE (V4L2_CID_PRIVATE_BASE + 3)
376 #define V4L2_CID_PRIVATE_LASTP1 (V4L2_CID_PRIVATE_BASE + 4)
378 static const struct v4l2_queryctrl no_ctrl
= {
380 .flags
= V4L2_CTRL_FLAG_DISABLED
,
382 static const struct v4l2_queryctrl video_ctrls
[] = {
385 .id
= V4L2_CID_BRIGHTNESS
,
386 .name
= "Brightness",
390 .default_value
= 128,
391 .type
= V4L2_CTRL_TYPE_INTEGER
,
393 .id
= V4L2_CID_CONTRAST
,
399 .type
= V4L2_CTRL_TYPE_INTEGER
,
401 .id
= V4L2_CID_SATURATION
,
402 .name
= "Saturation",
407 .type
= V4L2_CTRL_TYPE_INTEGER
,
415 .type
= V4L2_CTRL_TYPE_INTEGER
,
417 .id
= V4L2_CID_HFLIP
,
421 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
425 .id
= V4L2_CID_AUDIO_MUTE
,
429 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
431 .id
= V4L2_CID_AUDIO_VOLUME
,
437 .type
= V4L2_CTRL_TYPE_INTEGER
,
439 /* --- private --- */
441 .id
= V4L2_CID_PRIVATE_INVERT
,
445 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
447 .id
= V4L2_CID_PRIVATE_Y_ODD
,
448 .name
= "y offset odd field",
453 .type
= V4L2_CTRL_TYPE_INTEGER
,
455 .id
= V4L2_CID_PRIVATE_Y_EVEN
,
456 .name
= "y offset even field",
461 .type
= V4L2_CTRL_TYPE_INTEGER
,
463 .id
= V4L2_CID_PRIVATE_AUTOMUTE
,
468 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
471 static const unsigned int CTRLS
= ARRAY_SIZE(video_ctrls
);
473 static const struct v4l2_queryctrl
* ctrl_by_id(unsigned int id
)
477 for (i
= 0; i
< CTRLS
; i
++)
478 if (video_ctrls
[i
].id
== id
)
479 return video_ctrls
+i
;
483 static struct saa7134_format
* format_by_fourcc(unsigned int fourcc
)
487 for (i
= 0; i
< FORMATS
; i
++)
488 if (formats
[i
].fourcc
== fourcc
)
493 /* ----------------------------------------------------------------------- */
494 /* resource management */
496 static int res_get(struct saa7134_dev
*dev
, struct saa7134_fh
*fh
, unsigned int bit
)
498 if (fh
->resources
& bit
)
499 /* have it already allocated */
503 mutex_lock(&dev
->lock
);
504 if (dev
->resources
& bit
) {
505 /* no, someone else uses it */
506 mutex_unlock(&dev
->lock
);
509 /* it's free, grab it */
510 fh
->resources
|= bit
;
511 dev
->resources
|= bit
;
512 dprintk("res: get %d\n",bit
);
513 mutex_unlock(&dev
->lock
);
517 static int res_check(struct saa7134_fh
*fh
, unsigned int bit
)
519 return (fh
->resources
& bit
);
522 static int res_locked(struct saa7134_dev
*dev
, unsigned int bit
)
524 return (dev
->resources
& bit
);
528 void res_free(struct saa7134_dev
*dev
, struct saa7134_fh
*fh
, unsigned int bits
)
530 BUG_ON((fh
->resources
& bits
) != bits
);
532 mutex_lock(&dev
->lock
);
533 fh
->resources
&= ~bits
;
534 dev
->resources
&= ~bits
;
535 dprintk("res: put %d\n",bits
);
536 mutex_unlock(&dev
->lock
);
539 /* ------------------------------------------------------------------ */
541 static void set_tvnorm(struct saa7134_dev
*dev
, struct saa7134_tvnorm
*norm
)
543 dprintk("set tv norm = %s\n",norm
->name
);
547 dev
->crop_bounds
.left
= norm
->h_start
;
548 dev
->crop_defrect
.left
= norm
->h_start
;
549 dev
->crop_bounds
.width
= norm
->h_stop
- norm
->h_start
+1;
550 dev
->crop_defrect
.width
= norm
->h_stop
- norm
->h_start
+1;
552 dev
->crop_bounds
.top
= (norm
->vbi_v_stop_0
+1)*2;
553 dev
->crop_defrect
.top
= norm
->video_v_start
*2;
554 dev
->crop_bounds
.height
= ((norm
->id
& V4L2_STD_525_60
) ? 524 : 624)
555 - dev
->crop_bounds
.top
;
556 dev
->crop_defrect
.height
= (norm
->video_v_stop
- norm
->video_v_start
+1)*2;
558 dev
->crop_current
= dev
->crop_defrect
;
560 saa7134_set_tvnorm_hw(dev
);
563 static void video_mux(struct saa7134_dev
*dev
, int input
)
565 dprintk("video input = %d [%s]\n", input
, card_in(dev
, input
).name
);
566 dev
->ctl_input
= input
;
567 set_tvnorm(dev
, dev
->tvnorm
);
568 saa7134_tvaudio_setinput(dev
, &card_in(dev
, input
));
572 static void saa7134_set_decoder(struct saa7134_dev
*dev
)
574 int luma_control
, sync_control
, mux
;
576 struct saa7134_tvnorm
*norm
= dev
->tvnorm
;
577 mux
= card_in(dev
, dev
->ctl_input
).vmux
;
579 luma_control
= norm
->luma_control
;
580 sync_control
= norm
->sync_control
;
583 luma_control
|= 0x80; /* svideo */
584 if (noninterlaced
|| dev
->nosignal
)
585 sync_control
|= 0x20;
587 /* setup video decoder */
588 saa_writeb(SAA7134_INCR_DELAY
, 0x08);
589 saa_writeb(SAA7134_ANALOG_IN_CTRL1
, 0xc0 | mux
);
590 saa_writeb(SAA7134_ANALOG_IN_CTRL2
, 0x00);
592 saa_writeb(SAA7134_ANALOG_IN_CTRL3
, 0x90);
593 saa_writeb(SAA7134_ANALOG_IN_CTRL4
, 0x90);
594 saa_writeb(SAA7134_HSYNC_START
, 0xeb);
595 saa_writeb(SAA7134_HSYNC_STOP
, 0xe0);
596 saa_writeb(SAA7134_SOURCE_TIMING1
, norm
->src_timing
);
598 saa_writeb(SAA7134_SYNC_CTRL
, sync_control
);
599 saa_writeb(SAA7134_LUMA_CTRL
, luma_control
);
600 saa_writeb(SAA7134_DEC_LUMA_BRIGHT
, dev
->ctl_bright
);
602 saa_writeb(SAA7134_DEC_LUMA_CONTRAST
,
603 dev
->ctl_invert
? -dev
->ctl_contrast
: dev
->ctl_contrast
);
605 saa_writeb(SAA7134_DEC_CHROMA_SATURATION
,
606 dev
->ctl_invert
? -dev
->ctl_saturation
: dev
->ctl_saturation
);
608 saa_writeb(SAA7134_DEC_CHROMA_HUE
, dev
->ctl_hue
);
609 saa_writeb(SAA7134_CHROMA_CTRL1
, norm
->chroma_ctrl1
);
610 saa_writeb(SAA7134_CHROMA_GAIN
, norm
->chroma_gain
);
612 saa_writeb(SAA7134_CHROMA_CTRL2
, norm
->chroma_ctrl2
);
613 saa_writeb(SAA7134_MODE_DELAY_CTRL
, 0x00);
615 saa_writeb(SAA7134_ANALOG_ADC
, 0x01);
616 saa_writeb(SAA7134_VGATE_START
, 0x11);
617 saa_writeb(SAA7134_VGATE_STOP
, 0xfe);
618 saa_writeb(SAA7134_MISC_VGATE_MSB
, norm
->vgate_misc
);
619 saa_writeb(SAA7134_RAW_DATA_GAIN
, 0x40);
620 saa_writeb(SAA7134_RAW_DATA_OFFSET
, 0x80);
623 void saa7134_set_tvnorm_hw(struct saa7134_dev
*dev
)
625 saa7134_set_decoder(dev
);
627 if (card_in(dev
, dev
->ctl_input
).tv
)
628 saa_call_all(dev
, core
, s_std
, dev
->tvnorm
->id
);
629 /* Set the correct norm for the saa6752hs. This function
630 does nothing if there is no saa6752hs. */
631 saa_call_empress(dev
, core
, s_std
, dev
->tvnorm
->id
);
634 static void set_h_prescale(struct saa7134_dev
*dev
, int task
, int prescale
)
636 static const struct {
643 /* XPSC XACL XC2_1 XDCG VPFY */
655 static const int count
= ARRAY_SIZE(vals
);
658 for (i
= 0; i
< count
; i
++)
659 if (vals
[i
].xpsc
== prescale
)
664 saa_writeb(SAA7134_H_PRESCALE(task
), vals
[i
].xpsc
);
665 saa_writeb(SAA7134_ACC_LENGTH(task
), vals
[i
].xacl
);
666 saa_writeb(SAA7134_LEVEL_CTRL(task
),
667 (vals
[i
].xc2_1
<< 3) | (vals
[i
].xdcg
));
668 saa_andorb(SAA7134_FIR_PREFILTER_CTRL(task
), 0x0f,
669 (vals
[i
].vpfy
<< 2) | vals
[i
].vpfy
);
672 static void set_v_scale(struct saa7134_dev
*dev
, int task
, int yscale
)
676 saa_writeb(SAA7134_V_SCALE_RATIO1(task
), yscale
& 0xff);
677 saa_writeb(SAA7134_V_SCALE_RATIO2(task
), yscale
>> 8);
679 mirror
= (dev
->ctl_mirror
) ? 0x02 : 0x00;
682 dprintk("yscale LPI yscale=%d\n",yscale
);
683 saa_writeb(SAA7134_V_FILTER(task
), 0x00 | mirror
);
684 saa_writeb(SAA7134_LUMA_CONTRAST(task
), 0x40);
685 saa_writeb(SAA7134_CHROMA_SATURATION(task
), 0x40);
688 val
= 0x40 * 1024 / yscale
;
689 dprintk("yscale ACM yscale=%d val=0x%x\n",yscale
,val
);
690 saa_writeb(SAA7134_V_FILTER(task
), 0x01 | mirror
);
691 saa_writeb(SAA7134_LUMA_CONTRAST(task
), val
);
692 saa_writeb(SAA7134_CHROMA_SATURATION(task
), val
);
694 saa_writeb(SAA7134_LUMA_BRIGHT(task
), 0x80);
697 static void set_size(struct saa7134_dev
*dev
, int task
,
698 int width
, int height
, int interlace
)
700 int prescale
,xscale
,yscale
,y_even
,y_odd
;
701 int h_start
, h_stop
, v_start
, v_stop
;
702 int div
= interlace
? 2 : 1;
704 /* setup video scaler */
705 h_start
= dev
->crop_current
.left
;
706 v_start
= dev
->crop_current
.top
/2;
707 h_stop
= (dev
->crop_current
.left
+ dev
->crop_current
.width
-1);
708 v_stop
= (dev
->crop_current
.top
+ dev
->crop_current
.height
-1)/2;
710 saa_writeb(SAA7134_VIDEO_H_START1(task
), h_start
& 0xff);
711 saa_writeb(SAA7134_VIDEO_H_START2(task
), h_start
>> 8);
712 saa_writeb(SAA7134_VIDEO_H_STOP1(task
), h_stop
& 0xff);
713 saa_writeb(SAA7134_VIDEO_H_STOP2(task
), h_stop
>> 8);
714 saa_writeb(SAA7134_VIDEO_V_START1(task
), v_start
& 0xff);
715 saa_writeb(SAA7134_VIDEO_V_START2(task
), v_start
>> 8);
716 saa_writeb(SAA7134_VIDEO_V_STOP1(task
), v_stop
& 0xff);
717 saa_writeb(SAA7134_VIDEO_V_STOP2(task
), v_stop
>> 8);
719 prescale
= dev
->crop_current
.width
/ width
;
722 xscale
= 1024 * dev
->crop_current
.width
/ prescale
/ width
;
723 yscale
= 512 * div
* dev
->crop_current
.height
/ height
;
724 dprintk("prescale=%d xscale=%d yscale=%d\n",prescale
,xscale
,yscale
);
725 set_h_prescale(dev
,task
,prescale
);
726 saa_writeb(SAA7134_H_SCALE_INC1(task
), xscale
& 0xff);
727 saa_writeb(SAA7134_H_SCALE_INC2(task
), xscale
>> 8);
728 set_v_scale(dev
,task
,yscale
);
730 saa_writeb(SAA7134_VIDEO_PIXELS1(task
), width
& 0xff);
731 saa_writeb(SAA7134_VIDEO_PIXELS2(task
), width
>> 8);
732 saa_writeb(SAA7134_VIDEO_LINES1(task
), height
/div
& 0xff);
733 saa_writeb(SAA7134_VIDEO_LINES2(task
), height
/div
>> 8);
735 /* deinterlace y offsets */
736 y_odd
= dev
->ctl_y_odd
;
737 y_even
= dev
->ctl_y_even
;
738 saa_writeb(SAA7134_V_PHASE_OFFSET0(task
), y_odd
);
739 saa_writeb(SAA7134_V_PHASE_OFFSET1(task
), y_even
);
740 saa_writeb(SAA7134_V_PHASE_OFFSET2(task
), y_odd
);
741 saa_writeb(SAA7134_V_PHASE_OFFSET3(task
), y_even
);
744 /* ------------------------------------------------------------------ */
752 static void set_cliplist(struct saa7134_dev
*dev
, int reg
,
753 struct cliplist
*cl
, int entries
, char *name
)
758 for (i
= 0; i
< entries
; i
++) {
759 winbits
|= cl
[i
].enable
;
760 winbits
&= ~cl
[i
].disable
;
761 if (i
< 15 && cl
[i
].position
== cl
[i
+1].position
)
763 saa_writeb(reg
+ 0, winbits
);
764 saa_writeb(reg
+ 2, cl
[i
].position
& 0xff);
765 saa_writeb(reg
+ 3, cl
[i
].position
>> 8);
766 dprintk("clip: %s winbits=%02x pos=%d\n",
767 name
,winbits
,cl
[i
].position
);
770 for (; reg
< 0x400; reg
+= 8) {
771 saa_writeb(reg
+ 0, 0);
772 saa_writeb(reg
+ 1, 0);
773 saa_writeb(reg
+ 2, 0);
774 saa_writeb(reg
+ 3, 0);
778 static int clip_range(int val
)
785 /* Sort into smallest position first order */
786 static int cliplist_cmp(const void *a
, const void *b
)
788 const struct cliplist
*cla
= a
;
789 const struct cliplist
*clb
= b
;
790 if (cla
->position
< clb
->position
)
792 if (cla
->position
> clb
->position
)
797 static int setup_clipping(struct saa7134_dev
*dev
, struct v4l2_clip
*clips
,
798 int nclips
, int interlace
)
800 struct cliplist col
[16], row
[16];
801 int cols
= 0, rows
= 0, i
;
802 int div
= interlace
? 2 : 1;
804 memset(col
, 0, sizeof(col
));
805 memset(row
, 0, sizeof(row
));
806 for (i
= 0; i
< nclips
&& i
< 8; i
++) {
807 col
[cols
].position
= clip_range(clips
[i
].c
.left
);
808 col
[cols
].enable
= (1 << i
);
810 col
[cols
].position
= clip_range(clips
[i
].c
.left
+clips
[i
].c
.width
);
811 col
[cols
].disable
= (1 << i
);
813 row
[rows
].position
= clip_range(clips
[i
].c
.top
/ div
);
814 row
[rows
].enable
= (1 << i
);
816 row
[rows
].position
= clip_range((clips
[i
].c
.top
+ clips
[i
].c
.height
)
818 row
[rows
].disable
= (1 << i
);
821 sort(col
, cols
, sizeof col
[0], cliplist_cmp
, NULL
);
822 sort(row
, rows
, sizeof row
[0], cliplist_cmp
, NULL
);
823 set_cliplist(dev
,0x380,col
,cols
,"cols");
824 set_cliplist(dev
,0x384,row
,rows
,"rows");
828 static int verify_preview(struct saa7134_dev
*dev
, struct v4l2_window
*win
, bool try)
830 enum v4l2_field field
;
833 if (!try && (dev
->ovbuf
.base
== NULL
|| dev
->ovfmt
== NULL
))
835 if (win
->w
.width
< 48)
837 if (win
->w
.height
< 32)
839 if (win
->clipcount
> 8)
843 win
->global_alpha
= 0;
845 maxw
= dev
->crop_current
.width
;
846 maxh
= dev
->crop_current
.height
;
848 if (V4L2_FIELD_ANY
== field
) {
849 field
= (win
->w
.height
> maxh
/2)
850 ? V4L2_FIELD_INTERLACED
855 case V4L2_FIELD_BOTTOM
:
859 field
= V4L2_FIELD_INTERLACED
;
864 if (win
->w
.width
> maxw
)
866 if (win
->w
.height
> maxh
)
867 win
->w
.height
= maxh
;
871 static int start_preview(struct saa7134_dev
*dev
, struct saa7134_fh
*fh
)
873 unsigned long base
,control
,bpl
;
876 err
= verify_preview(dev
, &dev
->win
, false);
880 dev
->ovfield
= dev
->win
.field
;
881 dprintk("start_preview %dx%d+%d+%d %s field=%s\n",
882 dev
->win
.w
.width
, dev
->win
.w
.height
,
883 dev
->win
.w
.left
, dev
->win
.w
.top
,
884 dev
->ovfmt
->name
, v4l2_field_names
[dev
->ovfield
]);
886 /* setup window + clipping */
887 set_size(dev
, TASK_B
, dev
->win
.w
.width
, dev
->win
.w
.height
,
888 V4L2_FIELD_HAS_BOTH(dev
->ovfield
));
889 setup_clipping(dev
, dev
->clips
, dev
->nclips
,
890 V4L2_FIELD_HAS_BOTH(dev
->ovfield
));
892 saa_andorb(SAA7134_DATA_PATH(TASK_B
), 0x3f, 0x03);
894 saa_andorb(SAA7134_DATA_PATH(TASK_B
), 0x3f, 0x01);
895 saa_writeb(SAA7134_OFMT_VIDEO_B
, dev
->ovfmt
->pm
| 0x20);
897 /* dma: setup channel 1 (= Video Task B) */
898 base
= (unsigned long)dev
->ovbuf
.base
;
899 base
+= dev
->ovbuf
.fmt
.bytesperline
* dev
->win
.w
.top
;
900 base
+= dev
->ovfmt
->depth
/8 * dev
->win
.w
.left
;
901 bpl
= dev
->ovbuf
.fmt
.bytesperline
;
902 control
= SAA7134_RS_CONTROL_BURST_16
;
903 if (dev
->ovfmt
->bswap
)
904 control
|= SAA7134_RS_CONTROL_BSWAP
;
905 if (dev
->ovfmt
->wswap
)
906 control
|= SAA7134_RS_CONTROL_WSWAP
;
907 if (V4L2_FIELD_HAS_BOTH(dev
->ovfield
)) {
908 saa_writel(SAA7134_RS_BA1(1),base
);
909 saa_writel(SAA7134_RS_BA2(1),base
+bpl
);
910 saa_writel(SAA7134_RS_PITCH(1),bpl
*2);
911 saa_writel(SAA7134_RS_CONTROL(1),control
);
913 saa_writel(SAA7134_RS_BA1(1),base
);
914 saa_writel(SAA7134_RS_BA2(1),base
);
915 saa_writel(SAA7134_RS_PITCH(1),bpl
);
916 saa_writel(SAA7134_RS_CONTROL(1),control
);
921 saa7134_set_dmabits(dev
);
926 static int stop_preview(struct saa7134_dev
*dev
, struct saa7134_fh
*fh
)
929 saa7134_set_dmabits(dev
);
933 /* ------------------------------------------------------------------ */
935 static int buffer_activate(struct saa7134_dev
*dev
,
936 struct saa7134_buf
*buf
,
937 struct saa7134_buf
*next
)
939 unsigned long base
,control
,bpl
;
940 unsigned long bpl_uv
,lines_uv
,base2
,base3
,tmp
; /* planar */
942 dprintk("buffer_activate buf=%p\n",buf
);
943 buf
->vb
.state
= VIDEOBUF_ACTIVE
;
946 set_size(dev
,TASK_A
,buf
->vb
.width
,buf
->vb
.height
,
947 V4L2_FIELD_HAS_BOTH(buf
->vb
.field
));
949 saa_andorb(SAA7134_DATA_PATH(TASK_A
), 0x3f, 0x03);
951 saa_andorb(SAA7134_DATA_PATH(TASK_A
), 0x3f, 0x01);
952 saa_writeb(SAA7134_OFMT_VIDEO_A
, buf
->fmt
->pm
);
954 /* DMA: setup channel 0 (= Video Task A0) */
955 base
= saa7134_buffer_base(buf
);
956 if (buf
->fmt
->planar
)
959 bpl
= (buf
->vb
.width
* buf
->fmt
->depth
) / 8;
960 control
= SAA7134_RS_CONTROL_BURST_16
|
961 SAA7134_RS_CONTROL_ME
|
962 (buf
->pt
->dma
>> 12);
964 control
|= SAA7134_RS_CONTROL_BSWAP
;
966 control
|= SAA7134_RS_CONTROL_WSWAP
;
967 if (V4L2_FIELD_HAS_BOTH(buf
->vb
.field
)) {
969 saa_writel(SAA7134_RS_BA1(0),base
);
970 saa_writel(SAA7134_RS_BA2(0),base
+bpl
);
971 saa_writel(SAA7134_RS_PITCH(0),bpl
*2);
974 saa_writel(SAA7134_RS_BA1(0),base
);
975 saa_writel(SAA7134_RS_BA2(0),base
);
976 saa_writel(SAA7134_RS_PITCH(0),bpl
);
978 saa_writel(SAA7134_RS_CONTROL(0),control
);
980 if (buf
->fmt
->planar
) {
981 /* DMA: setup channel 4+5 (= planar task A) */
982 bpl_uv
= bpl
>> buf
->fmt
->hshift
;
983 lines_uv
= buf
->vb
.height
>> buf
->fmt
->vshift
;
984 base2
= base
+ bpl
* buf
->vb
.height
;
985 base3
= base2
+ bpl_uv
* lines_uv
;
986 if (buf
->fmt
->uvswap
)
987 tmp
= base2
, base2
= base3
, base3
= tmp
;
988 dprintk("uv: bpl=%ld lines=%ld base2/3=%ld/%ld\n",
989 bpl_uv
,lines_uv
,base2
,base3
);
990 if (V4L2_FIELD_HAS_BOTH(buf
->vb
.field
)) {
992 saa_writel(SAA7134_RS_BA1(4),base2
);
993 saa_writel(SAA7134_RS_BA2(4),base2
+bpl_uv
);
994 saa_writel(SAA7134_RS_PITCH(4),bpl_uv
*2);
995 saa_writel(SAA7134_RS_BA1(5),base3
);
996 saa_writel(SAA7134_RS_BA2(5),base3
+bpl_uv
);
997 saa_writel(SAA7134_RS_PITCH(5),bpl_uv
*2);
1000 saa_writel(SAA7134_RS_BA1(4),base2
);
1001 saa_writel(SAA7134_RS_BA2(4),base2
);
1002 saa_writel(SAA7134_RS_PITCH(4),bpl_uv
);
1003 saa_writel(SAA7134_RS_BA1(5),base3
);
1004 saa_writel(SAA7134_RS_BA2(5),base3
);
1005 saa_writel(SAA7134_RS_PITCH(5),bpl_uv
);
1007 saa_writel(SAA7134_RS_CONTROL(4),control
);
1008 saa_writel(SAA7134_RS_CONTROL(5),control
);
1012 saa7134_set_dmabits(dev
);
1013 mod_timer(&dev
->video_q
.timeout
, jiffies
+BUFFER_TIMEOUT
);
1017 static int buffer_prepare(struct videobuf_queue
*q
,
1018 struct videobuf_buffer
*vb
,
1019 enum v4l2_field field
)
1021 struct saa7134_fh
*fh
= q
->priv_data
;
1022 struct saa7134_dev
*dev
= fh
->dev
;
1023 struct saa7134_buf
*buf
= container_of(vb
,struct saa7134_buf
,vb
);
1028 if (NULL
== dev
->fmt
)
1030 if (dev
->width
< 48 ||
1032 dev
->width
/4 > dev
->crop_current
.width
||
1033 dev
->height
/4 > dev
->crop_current
.height
||
1034 dev
->width
> dev
->crop_bounds
.width
||
1035 dev
->height
> dev
->crop_bounds
.height
)
1037 size
= (dev
->width
* dev
->height
* dev
->fmt
->depth
) >> 3;
1038 if (0 != buf
->vb
.baddr
&& buf
->vb
.bsize
< size
)
1041 dprintk("buffer_prepare [%d,size=%dx%d,bytes=%d,fields=%s,%s]\n",
1042 vb
->i
, dev
->width
, dev
->height
, size
, v4l2_field_names
[field
],
1044 if (buf
->vb
.width
!= dev
->width
||
1045 buf
->vb
.height
!= dev
->height
||
1046 buf
->vb
.size
!= size
||
1047 buf
->vb
.field
!= field
||
1048 buf
->fmt
!= dev
->fmt
) {
1049 saa7134_dma_free(q
,buf
);
1052 if (VIDEOBUF_NEEDS_INIT
== buf
->vb
.state
) {
1053 struct videobuf_dmabuf
*dma
=videobuf_to_dma(&buf
->vb
);
1055 buf
->vb
.width
= dev
->width
;
1056 buf
->vb
.height
= dev
->height
;
1057 buf
->vb
.size
= size
;
1058 buf
->vb
.field
= field
;
1059 buf
->fmt
= dev
->fmt
;
1060 buf
->pt
= &fh
->pt_cap
;
1061 dev
->video_q
.curr
= NULL
;
1063 err
= videobuf_iolock(q
,&buf
->vb
,&dev
->ovbuf
);
1066 err
= saa7134_pgtable_build(dev
->pci
,buf
->pt
,
1069 saa7134_buffer_startpage(buf
));
1073 buf
->vb
.state
= VIDEOBUF_PREPARED
;
1074 buf
->activate
= buffer_activate
;
1078 saa7134_dma_free(q
,buf
);
1083 buffer_setup(struct videobuf_queue
*q
, unsigned int *count
, unsigned int *size
)
1085 struct saa7134_fh
*fh
= q
->priv_data
;
1086 struct saa7134_dev
*dev
= fh
->dev
;
1088 *size
= dev
->fmt
->depth
* dev
->width
* dev
->height
>> 3;
1091 *count
= saa7134_buffer_count(*size
,*count
);
1095 static void buffer_queue(struct videobuf_queue
*q
, struct videobuf_buffer
*vb
)
1097 struct saa7134_fh
*fh
= q
->priv_data
;
1098 struct saa7134_buf
*buf
= container_of(vb
,struct saa7134_buf
,vb
);
1100 saa7134_buffer_queue(fh
->dev
,&fh
->dev
->video_q
,buf
);
1103 static void buffer_release(struct videobuf_queue
*q
, struct videobuf_buffer
*vb
)
1105 struct saa7134_buf
*buf
= container_of(vb
,struct saa7134_buf
,vb
);
1107 saa7134_dma_free(q
,buf
);
1110 static struct videobuf_queue_ops video_qops
= {
1111 .buf_setup
= buffer_setup
,
1112 .buf_prepare
= buffer_prepare
,
1113 .buf_queue
= buffer_queue
,
1114 .buf_release
= buffer_release
,
1117 /* ------------------------------------------------------------------ */
1119 int saa7134_g_ctrl_internal(struct saa7134_dev
*dev
, struct saa7134_fh
*fh
, struct v4l2_control
*c
)
1121 const struct v4l2_queryctrl
* ctrl
;
1123 ctrl
= ctrl_by_id(c
->id
);
1127 case V4L2_CID_BRIGHTNESS
:
1128 c
->value
= dev
->ctl_bright
;
1131 c
->value
= dev
->ctl_hue
;
1133 case V4L2_CID_CONTRAST
:
1134 c
->value
= dev
->ctl_contrast
;
1136 case V4L2_CID_SATURATION
:
1137 c
->value
= dev
->ctl_saturation
;
1139 case V4L2_CID_AUDIO_MUTE
:
1140 c
->value
= dev
->ctl_mute
;
1142 case V4L2_CID_AUDIO_VOLUME
:
1143 c
->value
= dev
->ctl_volume
;
1145 case V4L2_CID_PRIVATE_INVERT
:
1146 c
->value
= dev
->ctl_invert
;
1148 case V4L2_CID_HFLIP
:
1149 c
->value
= dev
->ctl_mirror
;
1151 case V4L2_CID_PRIVATE_Y_EVEN
:
1152 c
->value
= dev
->ctl_y_even
;
1154 case V4L2_CID_PRIVATE_Y_ODD
:
1155 c
->value
= dev
->ctl_y_odd
;
1157 case V4L2_CID_PRIVATE_AUTOMUTE
:
1158 c
->value
= dev
->ctl_automute
;
1165 EXPORT_SYMBOL_GPL(saa7134_g_ctrl_internal
);
1167 static int saa7134_g_ctrl(struct file
*file
, void *priv
, struct v4l2_control
*c
)
1169 struct saa7134_fh
*fh
= priv
;
1171 return saa7134_g_ctrl_internal(fh
->dev
, fh
, c
);
1174 int saa7134_s_ctrl_internal(struct saa7134_dev
*dev
, struct saa7134_fh
*fh
, struct v4l2_control
*c
)
1176 const struct v4l2_queryctrl
* ctrl
;
1177 unsigned long flags
;
1178 int restart_overlay
= 0;
1183 mutex_lock(&dev
->lock
);
1185 ctrl
= ctrl_by_id(c
->id
);
1189 dprintk("set_control name=%s val=%d\n",ctrl
->name
,c
->value
);
1190 switch (ctrl
->type
) {
1191 case V4L2_CTRL_TYPE_BOOLEAN
:
1192 case V4L2_CTRL_TYPE_MENU
:
1193 case V4L2_CTRL_TYPE_INTEGER
:
1194 if (c
->value
< ctrl
->minimum
)
1195 c
->value
= ctrl
->minimum
;
1196 if (c
->value
> ctrl
->maximum
)
1197 c
->value
= ctrl
->maximum
;
1203 case V4L2_CID_BRIGHTNESS
:
1204 dev
->ctl_bright
= c
->value
;
1205 saa_writeb(SAA7134_DEC_LUMA_BRIGHT
, dev
->ctl_bright
);
1208 dev
->ctl_hue
= c
->value
;
1209 saa_writeb(SAA7134_DEC_CHROMA_HUE
, dev
->ctl_hue
);
1211 case V4L2_CID_CONTRAST
:
1212 dev
->ctl_contrast
= c
->value
;
1213 saa_writeb(SAA7134_DEC_LUMA_CONTRAST
,
1214 dev
->ctl_invert
? -dev
->ctl_contrast
: dev
->ctl_contrast
);
1216 case V4L2_CID_SATURATION
:
1217 dev
->ctl_saturation
= c
->value
;
1218 saa_writeb(SAA7134_DEC_CHROMA_SATURATION
,
1219 dev
->ctl_invert
? -dev
->ctl_saturation
: dev
->ctl_saturation
);
1221 case V4L2_CID_AUDIO_MUTE
:
1222 dev
->ctl_mute
= c
->value
;
1223 saa7134_tvaudio_setmute(dev
);
1225 case V4L2_CID_AUDIO_VOLUME
:
1226 dev
->ctl_volume
= c
->value
;
1227 saa7134_tvaudio_setvolume(dev
,dev
->ctl_volume
);
1229 case V4L2_CID_PRIVATE_INVERT
:
1230 dev
->ctl_invert
= c
->value
;
1231 saa_writeb(SAA7134_DEC_LUMA_CONTRAST
,
1232 dev
->ctl_invert
? -dev
->ctl_contrast
: dev
->ctl_contrast
);
1233 saa_writeb(SAA7134_DEC_CHROMA_SATURATION
,
1234 dev
->ctl_invert
? -dev
->ctl_saturation
: dev
->ctl_saturation
);
1236 case V4L2_CID_HFLIP
:
1237 dev
->ctl_mirror
= c
->value
;
1238 restart_overlay
= 1;
1240 case V4L2_CID_PRIVATE_Y_EVEN
:
1241 dev
->ctl_y_even
= c
->value
;
1242 restart_overlay
= 1;
1244 case V4L2_CID_PRIVATE_Y_ODD
:
1245 dev
->ctl_y_odd
= c
->value
;
1246 restart_overlay
= 1;
1248 case V4L2_CID_PRIVATE_AUTOMUTE
:
1250 struct v4l2_priv_tun_config tda9887_cfg
;
1252 tda9887_cfg
.tuner
= TUNER_TDA9887
;
1253 tda9887_cfg
.priv
= &dev
->tda9887_conf
;
1255 dev
->ctl_automute
= c
->value
;
1256 if (dev
->tda9887_conf
) {
1257 if (dev
->ctl_automute
)
1258 dev
->tda9887_conf
|= TDA9887_AUTOMUTE
;
1260 dev
->tda9887_conf
&= ~TDA9887_AUTOMUTE
;
1262 saa_call_all(dev
, tuner
, s_config
, &tda9887_cfg
);
1269 if (restart_overlay
&& fh
&& res_check(fh
, RESOURCE_OVERLAY
)) {
1270 spin_lock_irqsave(&dev
->slock
,flags
);
1271 stop_preview(dev
,fh
);
1272 start_preview(dev
,fh
);
1273 spin_unlock_irqrestore(&dev
->slock
,flags
);
1278 mutex_unlock(&dev
->lock
);
1281 EXPORT_SYMBOL_GPL(saa7134_s_ctrl_internal
);
1283 static int saa7134_s_ctrl(struct file
*file
, void *f
, struct v4l2_control
*c
)
1285 struct saa7134_fh
*fh
= f
;
1287 return saa7134_s_ctrl_internal(fh
->dev
, fh
, c
);
1290 /* ------------------------------------------------------------------ */
1292 static struct videobuf_queue
*saa7134_queue(struct file
*file
)
1294 struct video_device
*vdev
= video_devdata(file
);
1295 struct saa7134_fh
*fh
= file
->private_data
;
1296 struct videobuf_queue
*q
= NULL
;
1298 switch (vdev
->vfl_type
) {
1299 case VFL_TYPE_GRABBER
:
1311 static int saa7134_resource(struct file
*file
)
1313 struct video_device
*vdev
= video_devdata(file
);
1315 if (vdev
->vfl_type
== VFL_TYPE_GRABBER
)
1316 return RESOURCE_VIDEO
;
1318 if (vdev
->vfl_type
== VFL_TYPE_VBI
)
1319 return RESOURCE_VBI
;
1325 static int video_open(struct file
*file
)
1327 struct video_device
*vdev
= video_devdata(file
);
1328 struct saa7134_dev
*dev
= video_drvdata(file
);
1329 struct saa7134_fh
*fh
;
1331 /* allocate + initialize per filehandle data */
1332 fh
= kzalloc(sizeof(*fh
),GFP_KERNEL
);
1336 v4l2_fh_init(&fh
->fh
, vdev
);
1337 file
->private_data
= fh
;
1340 videobuf_queue_sg_init(&fh
->cap
, &video_qops
,
1341 &dev
->pci
->dev
, &dev
->slock
,
1342 V4L2_BUF_TYPE_VIDEO_CAPTURE
,
1343 V4L2_FIELD_INTERLACED
,
1344 sizeof(struct saa7134_buf
),
1346 videobuf_queue_sg_init(&fh
->vbi
, &saa7134_vbi_qops
,
1347 &dev
->pci
->dev
, &dev
->slock
,
1348 V4L2_BUF_TYPE_VBI_CAPTURE
,
1350 sizeof(struct saa7134_buf
),
1352 saa7134_pgtable_alloc(dev
->pci
,&fh
->pt_cap
);
1353 saa7134_pgtable_alloc(dev
->pci
,&fh
->pt_vbi
);
1355 if (vdev
->vfl_type
== VFL_TYPE_RADIO
) {
1356 /* switch to radio mode */
1357 saa7134_tvaudio_setinput(dev
,&card(dev
).radio
);
1358 saa_call_all(dev
, tuner
, s_radio
);
1360 /* switch to video/vbi mode */
1361 video_mux(dev
,dev
->ctl_input
);
1363 v4l2_fh_add(&fh
->fh
);
1369 video_read(struct file
*file
, char __user
*data
, size_t count
, loff_t
*ppos
)
1371 struct video_device
*vdev
= video_devdata(file
);
1372 struct saa7134_fh
*fh
= file
->private_data
;
1374 switch (vdev
->vfl_type
) {
1375 case VFL_TYPE_GRABBER
:
1376 if (res_locked(fh
->dev
,RESOURCE_VIDEO
))
1378 return videobuf_read_one(saa7134_queue(file
),
1380 file
->f_flags
& O_NONBLOCK
);
1382 if (!res_get(fh
->dev
,fh
,RESOURCE_VBI
))
1384 return videobuf_read_stream(saa7134_queue(file
),
1385 data
, count
, ppos
, 1,
1386 file
->f_flags
& O_NONBLOCK
);
1395 video_poll(struct file
*file
, struct poll_table_struct
*wait
)
1397 struct video_device
*vdev
= video_devdata(file
);
1398 struct saa7134_fh
*fh
= file
->private_data
;
1399 struct videobuf_buffer
*buf
= NULL
;
1400 unsigned int rc
= 0;
1402 if (vdev
->vfl_type
== VFL_TYPE_VBI
)
1403 return videobuf_poll_stream(file
, &fh
->vbi
, wait
);
1405 if (res_check(fh
,RESOURCE_VIDEO
)) {
1406 mutex_lock(&fh
->cap
.vb_lock
);
1407 if (!list_empty(&fh
->cap
.stream
))
1408 buf
= list_entry(fh
->cap
.stream
.next
, struct videobuf_buffer
, stream
);
1410 mutex_lock(&fh
->cap
.vb_lock
);
1411 if (UNSET
== fh
->cap
.read_off
) {
1412 /* need to capture a new frame */
1413 if (res_locked(fh
->dev
,RESOURCE_VIDEO
))
1415 if (0 != fh
->cap
.ops
->buf_prepare(&fh
->cap
,fh
->cap
.read_buf
,fh
->cap
.field
))
1417 fh
->cap
.ops
->buf_queue(&fh
->cap
,fh
->cap
.read_buf
);
1418 fh
->cap
.read_off
= 0;
1420 buf
= fh
->cap
.read_buf
;
1426 poll_wait(file
, &buf
->done
, wait
);
1427 if (buf
->state
== VIDEOBUF_DONE
||
1428 buf
->state
== VIDEOBUF_ERROR
)
1429 rc
= POLLIN
|POLLRDNORM
;
1430 mutex_unlock(&fh
->cap
.vb_lock
);
1434 mutex_unlock(&fh
->cap
.vb_lock
);
1438 static int video_release(struct file
*file
)
1440 struct video_device
*vdev
= video_devdata(file
);
1441 struct saa7134_fh
*fh
= file
->private_data
;
1442 struct saa7134_dev
*dev
= fh
->dev
;
1443 struct saa6588_command cmd
;
1444 unsigned long flags
;
1446 saa7134_tvaudio_close(dev
);
1448 /* turn off overlay */
1449 if (res_check(fh
, RESOURCE_OVERLAY
)) {
1450 spin_lock_irqsave(&dev
->slock
,flags
);
1451 stop_preview(dev
,fh
);
1452 spin_unlock_irqrestore(&dev
->slock
,flags
);
1453 res_free(dev
,fh
,RESOURCE_OVERLAY
);
1456 /* stop video capture */
1457 if (res_check(fh
, RESOURCE_VIDEO
)) {
1458 videobuf_streamoff(&fh
->cap
);
1459 res_free(dev
,fh
,RESOURCE_VIDEO
);
1461 if (fh
->cap
.read_buf
) {
1462 buffer_release(&fh
->cap
,fh
->cap
.read_buf
);
1463 kfree(fh
->cap
.read_buf
);
1466 /* stop vbi capture */
1467 if (res_check(fh
, RESOURCE_VBI
)) {
1468 videobuf_stop(&fh
->vbi
);
1469 res_free(dev
,fh
,RESOURCE_VBI
);
1472 /* ts-capture will not work in planar mode, so turn it off Hac: 04.05*/
1473 saa_andorb(SAA7134_OFMT_VIDEO_A
, 0x1f, 0);
1474 saa_andorb(SAA7134_OFMT_VIDEO_B
, 0x1f, 0);
1475 saa_andorb(SAA7134_OFMT_DATA_A
, 0x1f, 0);
1476 saa_andorb(SAA7134_OFMT_DATA_B
, 0x1f, 0);
1478 saa_call_all(dev
, core
, s_power
, 0);
1479 if (vdev
->vfl_type
== VFL_TYPE_RADIO
)
1480 saa_call_all(dev
, core
, ioctl
, SAA6588_CMD_CLOSE
, &cmd
);
1483 videobuf_mmap_free(&fh
->cap
);
1484 videobuf_mmap_free(&fh
->vbi
);
1485 saa7134_pgtable_free(dev
->pci
,&fh
->pt_cap
);
1486 saa7134_pgtable_free(dev
->pci
,&fh
->pt_vbi
);
1488 v4l2_fh_del(&fh
->fh
);
1489 v4l2_fh_exit(&fh
->fh
);
1490 file
->private_data
= NULL
;
1495 static int video_mmap(struct file
*file
, struct vm_area_struct
* vma
)
1497 return videobuf_mmap_mapper(saa7134_queue(file
), vma
);
1500 static ssize_t
radio_read(struct file
*file
, char __user
*data
,
1501 size_t count
, loff_t
*ppos
)
1503 struct saa7134_fh
*fh
= file
->private_data
;
1504 struct saa7134_dev
*dev
= fh
->dev
;
1505 struct saa6588_command cmd
;
1507 cmd
.block_count
= count
/3;
1509 cmd
.instance
= file
;
1510 cmd
.result
= -ENODEV
;
1512 saa_call_all(dev
, core
, ioctl
, SAA6588_CMD_READ
, &cmd
);
1517 static unsigned int radio_poll(struct file
*file
, poll_table
*wait
)
1519 struct saa7134_fh
*fh
= file
->private_data
;
1520 struct saa7134_dev
*dev
= fh
->dev
;
1521 struct saa6588_command cmd
;
1523 cmd
.instance
= file
;
1524 cmd
.event_list
= wait
;
1525 cmd
.result
= -ENODEV
;
1526 saa_call_all(dev
, core
, ioctl
, SAA6588_CMD_POLL
, &cmd
);
1531 /* ------------------------------------------------------------------ */
1533 static int saa7134_try_get_set_fmt_vbi_cap(struct file
*file
, void *priv
,
1534 struct v4l2_format
*f
)
1536 struct saa7134_fh
*fh
= priv
;
1537 struct saa7134_dev
*dev
= fh
->dev
;
1538 struct saa7134_tvnorm
*norm
= dev
->tvnorm
;
1540 memset(&f
->fmt
.vbi
.reserved
, 0, sizeof(f
->fmt
.vbi
.reserved
));
1541 f
->fmt
.vbi
.sampling_rate
= 6750000 * 4;
1542 f
->fmt
.vbi
.samples_per_line
= 2048 /* VBI_LINE_LENGTH */;
1543 f
->fmt
.vbi
.sample_format
= V4L2_PIX_FMT_GREY
;
1544 f
->fmt
.vbi
.offset
= 64 * 4;
1545 f
->fmt
.vbi
.start
[0] = norm
->vbi_v_start_0
;
1546 f
->fmt
.vbi
.count
[0] = norm
->vbi_v_stop_0
- norm
->vbi_v_start_0
+1;
1547 f
->fmt
.vbi
.start
[1] = norm
->vbi_v_start_1
;
1548 f
->fmt
.vbi
.count
[1] = f
->fmt
.vbi
.count
[0];
1549 f
->fmt
.vbi
.flags
= 0; /* VBI_UNSYNC VBI_INTERLACED */
1554 static int saa7134_g_fmt_vid_cap(struct file
*file
, void *priv
,
1555 struct v4l2_format
*f
)
1557 struct saa7134_fh
*fh
= priv
;
1558 struct saa7134_dev
*dev
= fh
->dev
;
1560 f
->fmt
.pix
.width
= dev
->width
;
1561 f
->fmt
.pix
.height
= dev
->height
;
1562 f
->fmt
.pix
.field
= fh
->cap
.field
;
1563 f
->fmt
.pix
.pixelformat
= dev
->fmt
->fourcc
;
1564 f
->fmt
.pix
.bytesperline
=
1565 (f
->fmt
.pix
.width
* dev
->fmt
->depth
) >> 3;
1566 f
->fmt
.pix
.sizeimage
=
1567 f
->fmt
.pix
.height
* f
->fmt
.pix
.bytesperline
;
1568 f
->fmt
.pix
.colorspace
= V4L2_COLORSPACE_SMPTE170M
;
1569 f
->fmt
.pix
.priv
= 0;
1573 static int saa7134_g_fmt_vid_overlay(struct file
*file
, void *priv
,
1574 struct v4l2_format
*f
)
1576 struct saa7134_fh
*fh
= priv
;
1577 struct saa7134_dev
*dev
= fh
->dev
;
1578 struct v4l2_clip __user
*clips
= f
->fmt
.win
.clips
;
1579 u32 clipcount
= f
->fmt
.win
.clipcount
;
1583 if (saa7134_no_overlay
> 0) {
1584 printk(KERN_ERR
"V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
1587 mutex_lock(&dev
->lock
);
1588 f
->fmt
.win
= dev
->win
;
1589 f
->fmt
.win
.clips
= clips
;
1592 if (dev
->nclips
< clipcount
)
1593 clipcount
= dev
->nclips
;
1594 f
->fmt
.win
.clipcount
= clipcount
;
1596 for (i
= 0; !err
&& i
< clipcount
; i
++) {
1597 if (copy_to_user(&f
->fmt
.win
.clips
[i
].c
, &dev
->clips
[i
].c
,
1598 sizeof(struct v4l2_rect
)))
1601 mutex_unlock(&dev
->lock
);
1606 static int saa7134_try_fmt_vid_cap(struct file
*file
, void *priv
,
1607 struct v4l2_format
*f
)
1609 struct saa7134_fh
*fh
= priv
;
1610 struct saa7134_dev
*dev
= fh
->dev
;
1611 struct saa7134_format
*fmt
;
1612 enum v4l2_field field
;
1613 unsigned int maxw
, maxh
;
1615 fmt
= format_by_fourcc(f
->fmt
.pix
.pixelformat
);
1619 field
= f
->fmt
.pix
.field
;
1620 maxw
= min(dev
->crop_current
.width
*4, dev
->crop_bounds
.width
);
1621 maxh
= min(dev
->crop_current
.height
*4, dev
->crop_bounds
.height
);
1623 if (V4L2_FIELD_ANY
== field
) {
1624 field
= (f
->fmt
.pix
.height
> maxh
/2)
1625 ? V4L2_FIELD_INTERLACED
1626 : V4L2_FIELD_BOTTOM
;
1629 case V4L2_FIELD_TOP
:
1630 case V4L2_FIELD_BOTTOM
:
1634 field
= V4L2_FIELD_INTERLACED
;
1638 f
->fmt
.pix
.field
= field
;
1639 if (f
->fmt
.pix
.width
< 48)
1640 f
->fmt
.pix
.width
= 48;
1641 if (f
->fmt
.pix
.height
< 32)
1642 f
->fmt
.pix
.height
= 32;
1643 if (f
->fmt
.pix
.width
> maxw
)
1644 f
->fmt
.pix
.width
= maxw
;
1645 if (f
->fmt
.pix
.height
> maxh
)
1646 f
->fmt
.pix
.height
= maxh
;
1647 f
->fmt
.pix
.width
&= ~0x03;
1648 f
->fmt
.pix
.bytesperline
=
1649 (f
->fmt
.pix
.width
* fmt
->depth
) >> 3;
1650 f
->fmt
.pix
.sizeimage
=
1651 f
->fmt
.pix
.height
* f
->fmt
.pix
.bytesperline
;
1652 f
->fmt
.pix
.colorspace
= V4L2_COLORSPACE_SMPTE170M
;
1653 f
->fmt
.pix
.priv
= 0;
1658 static int saa7134_try_fmt_vid_overlay(struct file
*file
, void *priv
,
1659 struct v4l2_format
*f
)
1661 struct saa7134_fh
*fh
= priv
;
1662 struct saa7134_dev
*dev
= fh
->dev
;
1664 if (saa7134_no_overlay
> 0) {
1665 printk(KERN_ERR
"V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
1669 if (f
->fmt
.win
.clips
== NULL
)
1670 f
->fmt
.win
.clipcount
= 0;
1671 return verify_preview(dev
, &f
->fmt
.win
, true);
1674 static int saa7134_s_fmt_vid_cap(struct file
*file
, void *priv
,
1675 struct v4l2_format
*f
)
1677 struct saa7134_fh
*fh
= priv
;
1678 struct saa7134_dev
*dev
= fh
->dev
;
1681 err
= saa7134_try_fmt_vid_cap(file
, priv
, f
);
1685 dev
->fmt
= format_by_fourcc(f
->fmt
.pix
.pixelformat
);
1686 dev
->width
= f
->fmt
.pix
.width
;
1687 dev
->height
= f
->fmt
.pix
.height
;
1688 fh
->cap
.field
= f
->fmt
.pix
.field
;
1692 static int saa7134_s_fmt_vid_overlay(struct file
*file
, void *priv
,
1693 struct v4l2_format
*f
)
1695 struct saa7134_fh
*fh
= priv
;
1696 struct saa7134_dev
*dev
= fh
->dev
;
1698 unsigned long flags
;
1700 if (saa7134_no_overlay
> 0) {
1701 printk(KERN_ERR
"V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
1704 if (f
->fmt
.win
.clips
== NULL
)
1705 f
->fmt
.win
.clipcount
= 0;
1706 err
= verify_preview(dev
, &f
->fmt
.win
, true);
1710 mutex_lock(&dev
->lock
);
1712 dev
->win
= f
->fmt
.win
;
1713 dev
->nclips
= f
->fmt
.win
.clipcount
;
1715 if (copy_from_user(dev
->clips
, f
->fmt
.win
.clips
,
1716 sizeof(struct v4l2_clip
) * dev
->nclips
)) {
1717 mutex_unlock(&dev
->lock
);
1721 if (res_check(fh
, RESOURCE_OVERLAY
)) {
1722 spin_lock_irqsave(&dev
->slock
, flags
);
1723 stop_preview(dev
, fh
);
1724 start_preview(dev
, fh
);
1725 spin_unlock_irqrestore(&dev
->slock
, flags
);
1728 mutex_unlock(&dev
->lock
);
1732 int saa7134_queryctrl(struct file
*file
, void *priv
, struct v4l2_queryctrl
*c
)
1734 const struct v4l2_queryctrl
*ctrl
;
1736 if ((c
->id
< V4L2_CID_BASE
||
1737 c
->id
>= V4L2_CID_LASTP1
) &&
1738 (c
->id
< V4L2_CID_PRIVATE_BASE
||
1739 c
->id
>= V4L2_CID_PRIVATE_LASTP1
))
1741 ctrl
= ctrl_by_id(c
->id
);
1742 *c
= (NULL
!= ctrl
) ? *ctrl
: no_ctrl
;
1745 EXPORT_SYMBOL_GPL(saa7134_queryctrl
);
1747 static int saa7134_enum_input(struct file
*file
, void *priv
,
1748 struct v4l2_input
*i
)
1750 struct saa7134_fh
*fh
= priv
;
1751 struct saa7134_dev
*dev
= fh
->dev
;
1755 if (n
>= SAA7134_INPUT_MAX
)
1757 if (NULL
== card_in(dev
, i
->index
).name
)
1760 i
->type
= V4L2_INPUT_TYPE_CAMERA
;
1761 strcpy(i
->name
, card_in(dev
, n
).name
);
1762 if (card_in(dev
, n
).tv
)
1763 i
->type
= V4L2_INPUT_TYPE_TUNER
;
1764 if (n
== dev
->ctl_input
) {
1765 int v1
= saa_readb(SAA7134_STATUS_VIDEO1
);
1766 int v2
= saa_readb(SAA7134_STATUS_VIDEO2
);
1768 if (0 != (v1
& 0x40))
1769 i
->status
|= V4L2_IN_ST_NO_H_LOCK
;
1770 if (0 != (v2
& 0x40))
1771 i
->status
|= V4L2_IN_ST_NO_SYNC
;
1772 if (0 != (v2
& 0x0e))
1773 i
->status
|= V4L2_IN_ST_MACROVISION
;
1775 i
->std
= SAA7134_NORMS
;
1779 static int saa7134_g_input(struct file
*file
, void *priv
, unsigned int *i
)
1781 struct saa7134_fh
*fh
= priv
;
1782 struct saa7134_dev
*dev
= fh
->dev
;
1784 *i
= dev
->ctl_input
;
1788 static int saa7134_s_input(struct file
*file
, void *priv
, unsigned int i
)
1790 struct saa7134_fh
*fh
= priv
;
1791 struct saa7134_dev
*dev
= fh
->dev
;
1793 if (i
>= SAA7134_INPUT_MAX
)
1795 if (NULL
== card_in(dev
, i
).name
)
1797 mutex_lock(&dev
->lock
);
1799 mutex_unlock(&dev
->lock
);
1803 static int saa7134_querycap(struct file
*file
, void *priv
,
1804 struct v4l2_capability
*cap
)
1806 struct saa7134_fh
*fh
= priv
;
1807 struct saa7134_dev
*dev
= fh
->dev
;
1808 struct video_device
*vdev
= video_devdata(file
);
1809 u32 radio_caps
, video_caps
, vbi_caps
;
1811 unsigned int tuner_type
= dev
->tuner_type
;
1813 strcpy(cap
->driver
, "saa7134");
1814 strlcpy(cap
->card
, saa7134_boards
[dev
->board
].name
,
1816 sprintf(cap
->bus_info
, "PCI:%s", pci_name(dev
->pci
));
1818 cap
->device_caps
= V4L2_CAP_READWRITE
| V4L2_CAP_STREAMING
;
1819 if ((tuner_type
!= TUNER_ABSENT
) && (tuner_type
!= UNSET
))
1820 cap
->device_caps
|= V4L2_CAP_TUNER
;
1822 radio_caps
= V4L2_CAP_RADIO
;
1824 radio_caps
|= V4L2_CAP_RDS_CAPTURE
;
1826 video_caps
= V4L2_CAP_VIDEO_CAPTURE
;
1827 if (saa7134_no_overlay
<= 0)
1828 video_caps
|= V4L2_CAP_VIDEO_OVERLAY
;
1830 vbi_caps
= V4L2_CAP_VBI_CAPTURE
;
1832 switch (vdev
->vfl_type
) {
1833 case VFL_TYPE_RADIO
:
1834 cap
->device_caps
|= radio_caps
;
1836 case VFL_TYPE_GRABBER
:
1837 cap
->device_caps
|= video_caps
;
1840 cap
->device_caps
|= vbi_caps
;
1843 cap
->capabilities
= radio_caps
| video_caps
| vbi_caps
|
1844 cap
->device_caps
| V4L2_CAP_DEVICE_CAPS
;
1845 if (vdev
->vfl_type
== VFL_TYPE_RADIO
) {
1846 cap
->device_caps
&= ~V4L2_CAP_STREAMING
;
1848 cap
->device_caps
&= ~V4L2_CAP_READWRITE
;
1854 int saa7134_s_std_internal(struct saa7134_dev
*dev
, struct saa7134_fh
*fh
, v4l2_std_id id
)
1856 unsigned long flags
;
1860 if (!fh
&& res_locked(dev
, RESOURCE_OVERLAY
)) {
1861 /* Don't change the std from the mpeg device
1862 if overlay is active. */
1866 for (i
= 0; i
< TVNORMS
; i
++)
1867 if (id
== tvnorms
[i
].id
)
1871 for (i
= 0; i
< TVNORMS
; i
++)
1872 if (id
& tvnorms
[i
].id
)
1877 if ((id
& V4L2_STD_SECAM
) && (secam
[0] != '-')) {
1878 if (secam
[0] == 'L' || secam
[0] == 'l') {
1879 if (secam
[1] == 'C' || secam
[1] == 'c')
1880 fixup
= V4L2_STD_SECAM_LC
;
1882 fixup
= V4L2_STD_SECAM_L
;
1884 if (secam
[0] == 'D' || secam
[0] == 'd')
1885 fixup
= V4L2_STD_SECAM_DK
;
1887 fixup
= V4L2_STD_SECAM
;
1889 for (i
= 0; i
< TVNORMS
; i
++) {
1890 if (fixup
== tvnorms
[i
].id
)
1899 mutex_lock(&dev
->lock
);
1900 if (fh
&& res_check(fh
, RESOURCE_OVERLAY
)) {
1901 spin_lock_irqsave(&dev
->slock
, flags
);
1902 stop_preview(dev
, fh
);
1903 spin_unlock_irqrestore(&dev
->slock
, flags
);
1905 set_tvnorm(dev
, &tvnorms
[i
]);
1907 spin_lock_irqsave(&dev
->slock
, flags
);
1908 start_preview(dev
, fh
);
1909 spin_unlock_irqrestore(&dev
->slock
, flags
);
1911 set_tvnorm(dev
, &tvnorms
[i
]);
1913 saa7134_tvaudio_do_scan(dev
);
1914 mutex_unlock(&dev
->lock
);
1917 EXPORT_SYMBOL_GPL(saa7134_s_std_internal
);
1919 static int saa7134_s_std(struct file
*file
, void *priv
, v4l2_std_id id
)
1921 struct saa7134_fh
*fh
= priv
;
1923 return saa7134_s_std_internal(fh
->dev
, fh
, id
);
1926 static int saa7134_g_std(struct file
*file
, void *priv
, v4l2_std_id
*id
)
1928 struct saa7134_fh
*fh
= priv
;
1929 struct saa7134_dev
*dev
= fh
->dev
;
1931 *id
= dev
->tvnorm
->id
;
1935 static int saa7134_cropcap(struct file
*file
, void *priv
,
1936 struct v4l2_cropcap
*cap
)
1938 struct saa7134_fh
*fh
= priv
;
1939 struct saa7134_dev
*dev
= fh
->dev
;
1941 if (cap
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
&&
1942 cap
->type
!= V4L2_BUF_TYPE_VIDEO_OVERLAY
)
1944 cap
->bounds
= dev
->crop_bounds
;
1945 cap
->defrect
= dev
->crop_defrect
;
1946 cap
->pixelaspect
.numerator
= 1;
1947 cap
->pixelaspect
.denominator
= 1;
1948 if (dev
->tvnorm
->id
& V4L2_STD_525_60
) {
1949 cap
->pixelaspect
.numerator
= 11;
1950 cap
->pixelaspect
.denominator
= 10;
1952 if (dev
->tvnorm
->id
& V4L2_STD_625_50
) {
1953 cap
->pixelaspect
.numerator
= 54;
1954 cap
->pixelaspect
.denominator
= 59;
1959 static int saa7134_g_crop(struct file
*file
, void *f
, struct v4l2_crop
*crop
)
1961 struct saa7134_fh
*fh
= f
;
1962 struct saa7134_dev
*dev
= fh
->dev
;
1964 if (crop
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
&&
1965 crop
->type
!= V4L2_BUF_TYPE_VIDEO_OVERLAY
)
1967 crop
->c
= dev
->crop_current
;
1971 static int saa7134_s_crop(struct file
*file
, void *f
, const struct v4l2_crop
*crop
)
1973 struct saa7134_fh
*fh
= f
;
1974 struct saa7134_dev
*dev
= fh
->dev
;
1975 struct v4l2_rect
*b
= &dev
->crop_bounds
;
1976 struct v4l2_rect
*c
= &dev
->crop_current
;
1978 if (crop
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
&&
1979 crop
->type
!= V4L2_BUF_TYPE_VIDEO_OVERLAY
)
1981 if (crop
->c
.height
< 0)
1983 if (crop
->c
.width
< 0)
1986 if (res_locked(fh
->dev
, RESOURCE_OVERLAY
))
1988 if (res_locked(fh
->dev
, RESOURCE_VIDEO
))
1992 if (c
->top
< b
->top
)
1994 if (c
->top
> b
->top
+ b
->height
)
1995 c
->top
= b
->top
+ b
->height
;
1996 if (c
->height
> b
->top
- c
->top
+ b
->height
)
1997 c
->height
= b
->top
- c
->top
+ b
->height
;
1999 if (c
->left
< b
->left
)
2001 if (c
->left
> b
->left
+ b
->width
)
2002 c
->left
= b
->left
+ b
->width
;
2003 if (c
->width
> b
->left
- c
->left
+ b
->width
)
2004 c
->width
= b
->left
- c
->left
+ b
->width
;
2008 static int saa7134_g_tuner(struct file
*file
, void *priv
,
2009 struct v4l2_tuner
*t
)
2011 struct saa7134_fh
*fh
= priv
;
2012 struct saa7134_dev
*dev
= fh
->dev
;
2017 memset(t
, 0, sizeof(*t
));
2018 for (n
= 0; n
< SAA7134_INPUT_MAX
; n
++) {
2019 if (card_in(dev
, n
).tv
)
2022 if (n
== SAA7134_INPUT_MAX
)
2024 if (NULL
!= card_in(dev
, n
).name
) {
2025 strcpy(t
->name
, "Television");
2026 t
->type
= V4L2_TUNER_ANALOG_TV
;
2027 saa_call_all(dev
, tuner
, g_tuner
, t
);
2028 t
->capability
= V4L2_TUNER_CAP_NORM
|
2029 V4L2_TUNER_CAP_STEREO
|
2030 V4L2_TUNER_CAP_LANG1
|
2031 V4L2_TUNER_CAP_LANG2
;
2032 t
->rxsubchans
= saa7134_tvaudio_getstereo(dev
);
2033 t
->audmode
= saa7134_tvaudio_rx2mode(t
->rxsubchans
);
2035 if (0 != (saa_readb(SAA7134_STATUS_VIDEO1
) & 0x03))
2040 static int saa7134_s_tuner(struct file
*file
, void *priv
,
2041 const struct v4l2_tuner
*t
)
2043 struct saa7134_fh
*fh
= priv
;
2044 struct saa7134_dev
*dev
= fh
->dev
;
2050 mode
= dev
->thread
.mode
;
2051 if (UNSET
== mode
) {
2052 rx
= saa7134_tvaudio_getstereo(dev
);
2053 mode
= saa7134_tvaudio_rx2mode(rx
);
2055 if (mode
!= t
->audmode
)
2056 dev
->thread
.mode
= t
->audmode
;
2061 static int saa7134_g_frequency(struct file
*file
, void *priv
,
2062 struct v4l2_frequency
*f
)
2064 struct saa7134_fh
*fh
= priv
;
2065 struct saa7134_dev
*dev
= fh
->dev
;
2070 saa_call_all(dev
, tuner
, g_frequency
, f
);
2075 static int saa7134_s_frequency(struct file
*file
, void *priv
,
2076 const struct v4l2_frequency
*f
)
2078 struct saa7134_fh
*fh
= priv
;
2079 struct saa7134_dev
*dev
= fh
->dev
;
2083 mutex_lock(&dev
->lock
);
2085 saa_call_all(dev
, tuner
, s_frequency
, f
);
2087 saa7134_tvaudio_do_scan(dev
);
2088 mutex_unlock(&dev
->lock
);
2092 static int saa7134_enum_fmt_vid_cap(struct file
*file
, void *priv
,
2093 struct v4l2_fmtdesc
*f
)
2095 if (f
->index
>= FORMATS
)
2098 strlcpy(f
->description
, formats
[f
->index
].name
,
2099 sizeof(f
->description
));
2101 f
->pixelformat
= formats
[f
->index
].fourcc
;
2106 static int saa7134_enum_fmt_vid_overlay(struct file
*file
, void *priv
,
2107 struct v4l2_fmtdesc
*f
)
2109 if (saa7134_no_overlay
> 0) {
2110 printk(KERN_ERR
"V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
2114 if ((f
->index
>= FORMATS
) || formats
[f
->index
].planar
)
2117 strlcpy(f
->description
, formats
[f
->index
].name
,
2118 sizeof(f
->description
));
2120 f
->pixelformat
= formats
[f
->index
].fourcc
;
2125 static int saa7134_g_fbuf(struct file
*file
, void *f
,
2126 struct v4l2_framebuffer
*fb
)
2128 struct saa7134_fh
*fh
= f
;
2129 struct saa7134_dev
*dev
= fh
->dev
;
2132 fb
->capability
= V4L2_FBUF_CAP_LIST_CLIPPING
;
2137 static int saa7134_s_fbuf(struct file
*file
, void *f
,
2138 const struct v4l2_framebuffer
*fb
)
2140 struct saa7134_fh
*fh
= f
;
2141 struct saa7134_dev
*dev
= fh
->dev
;
2142 struct saa7134_format
*fmt
;
2144 if (!capable(CAP_SYS_ADMIN
) &&
2145 !capable(CAP_SYS_RAWIO
))
2149 fmt
= format_by_fourcc(fb
->fmt
.pixelformat
);
2156 if (0 == dev
->ovbuf
.fmt
.bytesperline
)
2157 dev
->ovbuf
.fmt
.bytesperline
=
2158 dev
->ovbuf
.fmt
.width
*fmt
->depth
/8;
2162 static int saa7134_overlay(struct file
*file
, void *f
, unsigned int on
)
2164 struct saa7134_fh
*fh
= f
;
2165 struct saa7134_dev
*dev
= fh
->dev
;
2166 unsigned long flags
;
2169 if (saa7134_no_overlay
> 0) {
2170 dprintk("no_overlay\n");
2174 if (!res_get(dev
, fh
, RESOURCE_OVERLAY
))
2176 spin_lock_irqsave(&dev
->slock
, flags
);
2177 start_preview(dev
, fh
);
2178 spin_unlock_irqrestore(&dev
->slock
, flags
);
2181 if (!res_check(fh
, RESOURCE_OVERLAY
))
2183 spin_lock_irqsave(&dev
->slock
, flags
);
2184 stop_preview(dev
, fh
);
2185 spin_unlock_irqrestore(&dev
->slock
, flags
);
2186 res_free(dev
, fh
, RESOURCE_OVERLAY
);
2191 static int saa7134_reqbufs(struct file
*file
, void *priv
,
2192 struct v4l2_requestbuffers
*p
)
2194 return videobuf_reqbufs(saa7134_queue(file
), p
);
2197 static int saa7134_querybuf(struct file
*file
, void *priv
,
2198 struct v4l2_buffer
*b
)
2200 return videobuf_querybuf(saa7134_queue(file
), b
);
2203 static int saa7134_qbuf(struct file
*file
, void *priv
, struct v4l2_buffer
*b
)
2205 return videobuf_qbuf(saa7134_queue(file
), b
);
2208 static int saa7134_dqbuf(struct file
*file
, void *priv
, struct v4l2_buffer
*b
)
2210 return videobuf_dqbuf(saa7134_queue(file
), b
,
2211 file
->f_flags
& O_NONBLOCK
);
2214 static int saa7134_streamon(struct file
*file
, void *priv
,
2215 enum v4l2_buf_type type
)
2217 struct saa7134_fh
*fh
= priv
;
2218 struct saa7134_dev
*dev
= fh
->dev
;
2219 int res
= saa7134_resource(file
);
2221 if (!res_get(dev
, fh
, res
))
2224 /* The SAA7134 has a 1K FIFO; the datasheet suggests that when
2225 * configured conservatively, there's 22 usec of buffering for video.
2226 * We therefore request a DMA latency of 20 usec, giving us 2 usec of
2227 * margin in case the FIFO is configured differently to the datasheet.
2228 * Unfortunately, I lack register-level documentation to check the
2229 * Linux FIFO setup and confirm the perfect value.
2231 pm_qos_add_request(&dev
->qos_request
,
2232 PM_QOS_CPU_DMA_LATENCY
,
2235 return videobuf_streamon(saa7134_queue(file
));
2238 static int saa7134_streamoff(struct file
*file
, void *priv
,
2239 enum v4l2_buf_type type
)
2242 struct saa7134_fh
*fh
= priv
;
2243 struct saa7134_dev
*dev
= fh
->dev
;
2244 int res
= saa7134_resource(file
);
2246 pm_qos_remove_request(&dev
->qos_request
);
2248 err
= videobuf_streamoff(saa7134_queue(file
));
2251 res_free(dev
, fh
, res
);
2255 #ifdef CONFIG_VIDEO_ADV_DEBUG
2256 static int vidioc_g_register (struct file
*file
, void *priv
,
2257 struct v4l2_dbg_register
*reg
)
2259 struct saa7134_fh
*fh
= priv
;
2260 struct saa7134_dev
*dev
= fh
->dev
;
2262 reg
->val
= saa_readb(reg
->reg
& 0xffffff);
2267 static int vidioc_s_register (struct file
*file
, void *priv
,
2268 const struct v4l2_dbg_register
*reg
)
2270 struct saa7134_fh
*fh
= priv
;
2271 struct saa7134_dev
*dev
= fh
->dev
;
2273 saa_writeb(reg
->reg
& 0xffffff, reg
->val
);
2278 static int radio_g_tuner(struct file
*file
, void *priv
,
2279 struct v4l2_tuner
*t
)
2281 struct saa7134_fh
*fh
= file
->private_data
;
2282 struct saa7134_dev
*dev
= fh
->dev
;
2287 strcpy(t
->name
, "Radio");
2289 saa_call_all(dev
, tuner
, g_tuner
, t
);
2290 t
->audmode
&= V4L2_TUNER_MODE_MONO
| V4L2_TUNER_MODE_STEREO
;
2291 if (dev
->input
->amux
== TV
) {
2292 t
->signal
= 0xf800 - ((saa_readb(0x581) & 0x1f) << 11);
2293 t
->rxsubchans
= (saa_readb(0x529) & 0x08) ?
2294 V4L2_TUNER_SUB_STEREO
: V4L2_TUNER_SUB_MONO
;
2298 static int radio_s_tuner(struct file
*file
, void *priv
,
2299 const struct v4l2_tuner
*t
)
2301 struct saa7134_fh
*fh
= file
->private_data
;
2302 struct saa7134_dev
*dev
= fh
->dev
;
2307 saa_call_all(dev
, tuner
, s_tuner
, t
);
2311 static int radio_enum_input(struct file
*file
, void *priv
,
2312 struct v4l2_input
*i
)
2317 strcpy(i
->name
, "Radio");
2318 i
->type
= V4L2_INPUT_TYPE_TUNER
;
2323 static int radio_g_input(struct file
*filp
, void *priv
, unsigned int *i
)
2329 static int radio_s_input(struct file
*filp
, void *priv
, unsigned int i
)
2334 static int radio_s_std(struct file
*file
, void *fh
, v4l2_std_id norm
)
2339 static int radio_queryctrl(struct file
*file
, void *priv
,
2340 struct v4l2_queryctrl
*c
)
2342 const struct v4l2_queryctrl
*ctrl
;
2344 if (c
->id
< V4L2_CID_BASE
||
2345 c
->id
>= V4L2_CID_LASTP1
)
2347 if (c
->id
== V4L2_CID_AUDIO_MUTE
) {
2348 ctrl
= ctrl_by_id(c
->id
);
2355 static const struct v4l2_file_operations video_fops
=
2357 .owner
= THIS_MODULE
,
2359 .release
= video_release
,
2363 .ioctl
= video_ioctl2
,
2366 static const struct v4l2_ioctl_ops video_ioctl_ops
= {
2367 .vidioc_querycap
= saa7134_querycap
,
2368 .vidioc_enum_fmt_vid_cap
= saa7134_enum_fmt_vid_cap
,
2369 .vidioc_g_fmt_vid_cap
= saa7134_g_fmt_vid_cap
,
2370 .vidioc_try_fmt_vid_cap
= saa7134_try_fmt_vid_cap
,
2371 .vidioc_s_fmt_vid_cap
= saa7134_s_fmt_vid_cap
,
2372 .vidioc_enum_fmt_vid_overlay
= saa7134_enum_fmt_vid_overlay
,
2373 .vidioc_g_fmt_vid_overlay
= saa7134_g_fmt_vid_overlay
,
2374 .vidioc_try_fmt_vid_overlay
= saa7134_try_fmt_vid_overlay
,
2375 .vidioc_s_fmt_vid_overlay
= saa7134_s_fmt_vid_overlay
,
2376 .vidioc_g_fmt_vbi_cap
= saa7134_try_get_set_fmt_vbi_cap
,
2377 .vidioc_try_fmt_vbi_cap
= saa7134_try_get_set_fmt_vbi_cap
,
2378 .vidioc_s_fmt_vbi_cap
= saa7134_try_get_set_fmt_vbi_cap
,
2379 .vidioc_cropcap
= saa7134_cropcap
,
2380 .vidioc_reqbufs
= saa7134_reqbufs
,
2381 .vidioc_querybuf
= saa7134_querybuf
,
2382 .vidioc_qbuf
= saa7134_qbuf
,
2383 .vidioc_dqbuf
= saa7134_dqbuf
,
2384 .vidioc_s_std
= saa7134_s_std
,
2385 .vidioc_g_std
= saa7134_g_std
,
2386 .vidioc_enum_input
= saa7134_enum_input
,
2387 .vidioc_g_input
= saa7134_g_input
,
2388 .vidioc_s_input
= saa7134_s_input
,
2389 .vidioc_queryctrl
= saa7134_queryctrl
,
2390 .vidioc_g_ctrl
= saa7134_g_ctrl
,
2391 .vidioc_s_ctrl
= saa7134_s_ctrl
,
2392 .vidioc_streamon
= saa7134_streamon
,
2393 .vidioc_streamoff
= saa7134_streamoff
,
2394 .vidioc_g_tuner
= saa7134_g_tuner
,
2395 .vidioc_s_tuner
= saa7134_s_tuner
,
2396 .vidioc_g_crop
= saa7134_g_crop
,
2397 .vidioc_s_crop
= saa7134_s_crop
,
2398 .vidioc_g_fbuf
= saa7134_g_fbuf
,
2399 .vidioc_s_fbuf
= saa7134_s_fbuf
,
2400 .vidioc_overlay
= saa7134_overlay
,
2401 .vidioc_g_frequency
= saa7134_g_frequency
,
2402 .vidioc_s_frequency
= saa7134_s_frequency
,
2403 #ifdef CONFIG_VIDEO_ADV_DEBUG
2404 .vidioc_g_register
= vidioc_g_register
,
2405 .vidioc_s_register
= vidioc_s_register
,
2409 static const struct v4l2_file_operations radio_fops
= {
2410 .owner
= THIS_MODULE
,
2413 .release
= video_release
,
2414 .ioctl
= video_ioctl2
,
2418 static const struct v4l2_ioctl_ops radio_ioctl_ops
= {
2419 .vidioc_querycap
= saa7134_querycap
,
2420 .vidioc_g_tuner
= radio_g_tuner
,
2421 .vidioc_enum_input
= radio_enum_input
,
2422 .vidioc_s_tuner
= radio_s_tuner
,
2423 .vidioc_s_input
= radio_s_input
,
2424 .vidioc_s_std
= radio_s_std
,
2425 .vidioc_queryctrl
= radio_queryctrl
,
2426 .vidioc_g_input
= radio_g_input
,
2427 .vidioc_g_ctrl
= saa7134_g_ctrl
,
2428 .vidioc_s_ctrl
= saa7134_s_ctrl
,
2429 .vidioc_g_frequency
= saa7134_g_frequency
,
2430 .vidioc_s_frequency
= saa7134_s_frequency
,
2433 /* ----------------------------------------------------------- */
2434 /* exported stuff */
2436 struct video_device saa7134_video_template
= {
2437 .name
= "saa7134-video",
2438 .fops
= &video_fops
,
2439 .ioctl_ops
= &video_ioctl_ops
,
2440 .tvnorms
= SAA7134_NORMS
,
2443 struct video_device saa7134_radio_template
= {
2444 .name
= "saa7134-radio",
2445 .fops
= &radio_fops
,
2446 .ioctl_ops
= &radio_ioctl_ops
,
2449 int saa7134_video_init1(struct saa7134_dev
*dev
)
2451 /* sanitycheck insmod options */
2452 if (gbuffers
< 2 || gbuffers
> VIDEO_MAX_FRAME
)
2454 if (gbufsize
> gbufsize_max
)
2455 gbufsize
= gbufsize_max
;
2456 gbufsize
= (gbufsize
+ PAGE_SIZE
- 1) & PAGE_MASK
;
2458 /* put some sensible defaults into the data structures ... */
2459 dev
->ctl_bright
= ctrl_by_id(V4L2_CID_BRIGHTNESS
)->default_value
;
2460 dev
->ctl_contrast
= ctrl_by_id(V4L2_CID_CONTRAST
)->default_value
;
2461 dev
->ctl_hue
= ctrl_by_id(V4L2_CID_HUE
)->default_value
;
2462 dev
->ctl_saturation
= ctrl_by_id(V4L2_CID_SATURATION
)->default_value
;
2463 dev
->ctl_volume
= ctrl_by_id(V4L2_CID_AUDIO_VOLUME
)->default_value
;
2464 dev
->ctl_mute
= 1; // ctrl_by_id(V4L2_CID_AUDIO_MUTE)->default_value;
2465 dev
->ctl_invert
= ctrl_by_id(V4L2_CID_PRIVATE_INVERT
)->default_value
;
2466 dev
->ctl_automute
= ctrl_by_id(V4L2_CID_PRIVATE_AUTOMUTE
)->default_value
;
2468 if (dev
->tda9887_conf
&& dev
->ctl_automute
)
2469 dev
->tda9887_conf
|= TDA9887_AUTOMUTE
;
2472 INIT_LIST_HEAD(&dev
->video_q
.queue
);
2473 init_timer(&dev
->video_q
.timeout
);
2474 dev
->video_q
.timeout
.function
= saa7134_buffer_timeout
;
2475 dev
->video_q
.timeout
.data
= (unsigned long)(&dev
->video_q
);
2476 dev
->video_q
.dev
= dev
;
2477 dev
->fmt
= format_by_fourcc(V4L2_PIX_FMT_BGR24
);
2480 dev
->win
.w
.width
= dev
->width
;
2481 dev
->win
.w
.height
= dev
->height
;
2482 dev
->win
.field
= V4L2_FIELD_INTERLACED
;
2483 dev
->ovbuf
.fmt
.width
= dev
->width
;
2484 dev
->ovbuf
.fmt
.height
= dev
->height
;
2485 dev
->ovbuf
.fmt
.pixelformat
= dev
->fmt
->fourcc
;
2486 dev
->ovbuf
.fmt
.colorspace
= V4L2_COLORSPACE_SMPTE170M
;
2488 if (saa7134_boards
[dev
->board
].video_out
)
2489 saa7134_videoport_init(dev
);
2494 int saa7134_videoport_init(struct saa7134_dev
*dev
)
2496 /* enable video output */
2497 int vo
= saa7134_boards
[dev
->board
].video_out
;
2499 unsigned int vid_port_opts
= saa7134_boards
[dev
->board
].vid_port_opts
;
2501 /* Configure videoport */
2502 saa_writeb(SAA7134_VIDEO_PORT_CTRL0
, video_out
[vo
][0]);
2503 video_reg
= video_out
[vo
][1];
2504 if (vid_port_opts
& SET_T_CODE_POLARITY_NON_INVERTED
)
2505 video_reg
&= ~VP_T_CODE_P_INVERTED
;
2506 saa_writeb(SAA7134_VIDEO_PORT_CTRL1
, video_reg
);
2507 saa_writeb(SAA7134_VIDEO_PORT_CTRL2
, video_out
[vo
][2]);
2508 saa_writeb(SAA7134_VIDEO_PORT_CTRL4
, video_out
[vo
][4]);
2509 video_reg
= video_out
[vo
][5];
2510 if (vid_port_opts
& SET_CLOCK_NOT_DELAYED
)
2511 video_reg
&= ~VP_CLK_CTRL2_DELAYED
;
2512 if (vid_port_opts
& SET_CLOCK_INVERTED
)
2513 video_reg
|= VP_CLK_CTRL1_INVERTED
;
2514 saa_writeb(SAA7134_VIDEO_PORT_CTRL5
, video_reg
);
2515 video_reg
= video_out
[vo
][6];
2516 if (vid_port_opts
& SET_VSYNC_OFF
) {
2517 video_reg
&= ~VP_VS_TYPE_MASK
;
2518 video_reg
|= VP_VS_TYPE_OFF
;
2520 saa_writeb(SAA7134_VIDEO_PORT_CTRL6
, video_reg
);
2521 saa_writeb(SAA7134_VIDEO_PORT_CTRL7
, video_out
[vo
][7]);
2522 saa_writeb(SAA7134_VIDEO_PORT_CTRL8
, video_out
[vo
][8]);
2524 /* Start videoport */
2525 saa_writeb(SAA7134_VIDEO_PORT_CTRL3
, video_out
[vo
][3]);
2530 int saa7134_video_init2(struct saa7134_dev
*dev
)
2533 set_tvnorm(dev
,&tvnorms
[0]);
2535 saa7134_tvaudio_setmute(dev
);
2536 saa7134_tvaudio_setvolume(dev
,dev
->ctl_volume
);
2540 void saa7134_irq_video_signalchange(struct saa7134_dev
*dev
)
2542 static const char *st
[] = {
2543 "(no signal)", "NTSC", "PAL", "SECAM" };
2546 st1
= saa_readb(SAA7134_STATUS_VIDEO1
);
2547 st2
= saa_readb(SAA7134_STATUS_VIDEO2
);
2548 dprintk("DCSDT: pll: %s, sync: %s, norm: %s\n",
2549 (st1
& 0x40) ? "not locked" : "locked",
2550 (st2
& 0x40) ? "no" : "yes",
2552 dev
->nosignal
= (st1
& 0x40) || (st2
& 0x40) || !(st2
& 0x1);
2554 if (dev
->nosignal
) {
2555 /* no video signal -> mute audio */
2556 if (dev
->ctl_automute
)
2558 saa7134_tvaudio_setmute(dev
);
2560 /* wake up tvaudio audio carrier scan thread */
2561 saa7134_tvaudio_do_scan(dev
);
2564 if ((st2
& 0x80) && !noninterlaced
&& !dev
->nosignal
)
2565 saa_clearb(SAA7134_SYNC_CTRL
, 0x20);
2567 saa_setb(SAA7134_SYNC_CTRL
, 0x20);
2569 if (dev
->mops
&& dev
->mops
->signal_change
)
2570 dev
->mops
->signal_change(dev
);
2574 void saa7134_irq_video_done(struct saa7134_dev
*dev
, unsigned long status
)
2576 enum v4l2_field field
;
2578 spin_lock(&dev
->slock
);
2579 if (dev
->video_q
.curr
) {
2580 dev
->video_fieldcount
++;
2581 field
= dev
->video_q
.curr
->vb
.field
;
2582 if (V4L2_FIELD_HAS_BOTH(field
)) {
2583 /* make sure we have seen both fields */
2584 if ((status
& 0x10) == 0x00) {
2585 dev
->video_q
.curr
->top_seen
= 1;
2588 if (!dev
->video_q
.curr
->top_seen
)
2590 } else if (field
== V4L2_FIELD_TOP
) {
2591 if ((status
& 0x10) != 0x10)
2593 } else if (field
== V4L2_FIELD_BOTTOM
) {
2594 if ((status
& 0x10) != 0x00)
2597 dev
->video_q
.curr
->vb
.field_count
= dev
->video_fieldcount
;
2598 saa7134_buffer_finish(dev
,&dev
->video_q
,VIDEOBUF_DONE
);
2600 saa7134_buffer_next(dev
,&dev
->video_q
);
2603 spin_unlock(&dev
->slock
);
2606 /* ----------------------------------------------------------- */