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/rds.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
)
830 enum v4l2_field field
;
833 if (NULL
== dev
->ovbuf
.base
)
835 if (NULL
== dev
->ovfmt
)
837 if (win
->w
.width
< 48 || win
->w
.height
< 32)
839 if (win
->clipcount
> 2048)
843 maxw
= dev
->crop_current
.width
;
844 maxh
= dev
->crop_current
.height
;
846 if (V4L2_FIELD_ANY
== field
) {
847 field
= (win
->w
.height
> maxh
/2)
848 ? V4L2_FIELD_INTERLACED
853 case V4L2_FIELD_BOTTOM
:
856 case V4L2_FIELD_INTERLACED
:
863 if (win
->w
.width
> maxw
)
865 if (win
->w
.height
> maxh
)
866 win
->w
.height
= maxh
;
870 static int start_preview(struct saa7134_dev
*dev
, struct saa7134_fh
*fh
)
872 unsigned long base
,control
,bpl
;
875 err
= verify_preview(dev
,&fh
->win
);
879 dev
->ovfield
= fh
->win
.field
;
880 dprintk("start_preview %dx%d+%d+%d %s field=%s\n",
881 fh
->win
.w
.width
,fh
->win
.w
.height
,
882 fh
->win
.w
.left
,fh
->win
.w
.top
,
883 dev
->ovfmt
->name
,v4l2_field_names
[dev
->ovfield
]);
885 /* setup window + clipping */
886 set_size(dev
,TASK_B
,fh
->win
.w
.width
,fh
->win
.w
.height
,
887 V4L2_FIELD_HAS_BOTH(dev
->ovfield
));
888 setup_clipping(dev
,fh
->clips
,fh
->nclips
,
889 V4L2_FIELD_HAS_BOTH(dev
->ovfield
));
891 saa_andorb(SAA7134_DATA_PATH(TASK_B
), 0x3f, 0x03);
893 saa_andorb(SAA7134_DATA_PATH(TASK_B
), 0x3f, 0x01);
894 saa_writeb(SAA7134_OFMT_VIDEO_B
, dev
->ovfmt
->pm
| 0x20);
896 /* dma: setup channel 1 (= Video Task B) */
897 base
= (unsigned long)dev
->ovbuf
.base
;
898 base
+= dev
->ovbuf
.fmt
.bytesperline
* fh
->win
.w
.top
;
899 base
+= dev
->ovfmt
->depth
/8 * fh
->win
.w
.left
;
900 bpl
= dev
->ovbuf
.fmt
.bytesperline
;
901 control
= SAA7134_RS_CONTROL_BURST_16
;
902 if (dev
->ovfmt
->bswap
)
903 control
|= SAA7134_RS_CONTROL_BSWAP
;
904 if (dev
->ovfmt
->wswap
)
905 control
|= SAA7134_RS_CONTROL_WSWAP
;
906 if (V4L2_FIELD_HAS_BOTH(dev
->ovfield
)) {
907 saa_writel(SAA7134_RS_BA1(1),base
);
908 saa_writel(SAA7134_RS_BA2(1),base
+bpl
);
909 saa_writel(SAA7134_RS_PITCH(1),bpl
*2);
910 saa_writel(SAA7134_RS_CONTROL(1),control
);
912 saa_writel(SAA7134_RS_BA1(1),base
);
913 saa_writel(SAA7134_RS_BA2(1),base
);
914 saa_writel(SAA7134_RS_PITCH(1),bpl
);
915 saa_writel(SAA7134_RS_CONTROL(1),control
);
920 saa7134_set_dmabits(dev
);
925 static int stop_preview(struct saa7134_dev
*dev
, struct saa7134_fh
*fh
)
928 saa7134_set_dmabits(dev
);
932 /* ------------------------------------------------------------------ */
934 static int buffer_activate(struct saa7134_dev
*dev
,
935 struct saa7134_buf
*buf
,
936 struct saa7134_buf
*next
)
938 unsigned long base
,control
,bpl
;
939 unsigned long bpl_uv
,lines_uv
,base2
,base3
,tmp
; /* planar */
941 dprintk("buffer_activate buf=%p\n",buf
);
942 buf
->vb
.state
= VIDEOBUF_ACTIVE
;
945 set_size(dev
,TASK_A
,buf
->vb
.width
,buf
->vb
.height
,
946 V4L2_FIELD_HAS_BOTH(buf
->vb
.field
));
948 saa_andorb(SAA7134_DATA_PATH(TASK_A
), 0x3f, 0x03);
950 saa_andorb(SAA7134_DATA_PATH(TASK_A
), 0x3f, 0x01);
951 saa_writeb(SAA7134_OFMT_VIDEO_A
, buf
->fmt
->pm
);
953 /* DMA: setup channel 0 (= Video Task A0) */
954 base
= saa7134_buffer_base(buf
);
955 if (buf
->fmt
->planar
)
958 bpl
= (buf
->vb
.width
* buf
->fmt
->depth
) / 8;
959 control
= SAA7134_RS_CONTROL_BURST_16
|
960 SAA7134_RS_CONTROL_ME
|
961 (buf
->pt
->dma
>> 12);
963 control
|= SAA7134_RS_CONTROL_BSWAP
;
965 control
|= SAA7134_RS_CONTROL_WSWAP
;
966 if (V4L2_FIELD_HAS_BOTH(buf
->vb
.field
)) {
968 saa_writel(SAA7134_RS_BA1(0),base
);
969 saa_writel(SAA7134_RS_BA2(0),base
+bpl
);
970 saa_writel(SAA7134_RS_PITCH(0),bpl
*2);
973 saa_writel(SAA7134_RS_BA1(0),base
);
974 saa_writel(SAA7134_RS_BA2(0),base
);
975 saa_writel(SAA7134_RS_PITCH(0),bpl
);
977 saa_writel(SAA7134_RS_CONTROL(0),control
);
979 if (buf
->fmt
->planar
) {
980 /* DMA: setup channel 4+5 (= planar task A) */
981 bpl_uv
= bpl
>> buf
->fmt
->hshift
;
982 lines_uv
= buf
->vb
.height
>> buf
->fmt
->vshift
;
983 base2
= base
+ bpl
* buf
->vb
.height
;
984 base3
= base2
+ bpl_uv
* lines_uv
;
985 if (buf
->fmt
->uvswap
)
986 tmp
= base2
, base2
= base3
, base3
= tmp
;
987 dprintk("uv: bpl=%ld lines=%ld base2/3=%ld/%ld\n",
988 bpl_uv
,lines_uv
,base2
,base3
);
989 if (V4L2_FIELD_HAS_BOTH(buf
->vb
.field
)) {
991 saa_writel(SAA7134_RS_BA1(4),base2
);
992 saa_writel(SAA7134_RS_BA2(4),base2
+bpl_uv
);
993 saa_writel(SAA7134_RS_PITCH(4),bpl_uv
*2);
994 saa_writel(SAA7134_RS_BA1(5),base3
);
995 saa_writel(SAA7134_RS_BA2(5),base3
+bpl_uv
);
996 saa_writel(SAA7134_RS_PITCH(5),bpl_uv
*2);
999 saa_writel(SAA7134_RS_BA1(4),base2
);
1000 saa_writel(SAA7134_RS_BA2(4),base2
);
1001 saa_writel(SAA7134_RS_PITCH(4),bpl_uv
);
1002 saa_writel(SAA7134_RS_BA1(5),base3
);
1003 saa_writel(SAA7134_RS_BA2(5),base3
);
1004 saa_writel(SAA7134_RS_PITCH(5),bpl_uv
);
1006 saa_writel(SAA7134_RS_CONTROL(4),control
);
1007 saa_writel(SAA7134_RS_CONTROL(5),control
);
1011 saa7134_set_dmabits(dev
);
1012 mod_timer(&dev
->video_q
.timeout
, jiffies
+BUFFER_TIMEOUT
);
1016 static int buffer_prepare(struct videobuf_queue
*q
,
1017 struct videobuf_buffer
*vb
,
1018 enum v4l2_field field
)
1020 struct saa7134_fh
*fh
= q
->priv_data
;
1021 struct saa7134_dev
*dev
= fh
->dev
;
1022 struct saa7134_buf
*buf
= container_of(vb
,struct saa7134_buf
,vb
);
1027 if (NULL
== fh
->fmt
)
1029 if (fh
->width
< 48 ||
1031 fh
->width
/4 > dev
->crop_current
.width
||
1032 fh
->height
/4 > dev
->crop_current
.height
||
1033 fh
->width
> dev
->crop_bounds
.width
||
1034 fh
->height
> dev
->crop_bounds
.height
)
1036 size
= (fh
->width
* fh
->height
* fh
->fmt
->depth
) >> 3;
1037 if (0 != buf
->vb
.baddr
&& buf
->vb
.bsize
< size
)
1040 dprintk("buffer_prepare [%d,size=%dx%d,bytes=%d,fields=%s,%s]\n",
1041 vb
->i
,fh
->width
,fh
->height
,size
,v4l2_field_names
[field
],
1043 if (buf
->vb
.width
!= fh
->width
||
1044 buf
->vb
.height
!= fh
->height
||
1045 buf
->vb
.size
!= size
||
1046 buf
->vb
.field
!= field
||
1047 buf
->fmt
!= fh
->fmt
) {
1048 saa7134_dma_free(q
,buf
);
1051 if (VIDEOBUF_NEEDS_INIT
== buf
->vb
.state
) {
1052 struct videobuf_dmabuf
*dma
=videobuf_to_dma(&buf
->vb
);
1054 buf
->vb
.width
= fh
->width
;
1055 buf
->vb
.height
= fh
->height
;
1056 buf
->vb
.size
= size
;
1057 buf
->vb
.field
= field
;
1059 buf
->pt
= &fh
->pt_cap
;
1060 dev
->video_q
.curr
= NULL
;
1062 err
= videobuf_iolock(q
,&buf
->vb
,&dev
->ovbuf
);
1065 err
= saa7134_pgtable_build(dev
->pci
,buf
->pt
,
1068 saa7134_buffer_startpage(buf
));
1072 buf
->vb
.state
= VIDEOBUF_PREPARED
;
1073 buf
->activate
= buffer_activate
;
1077 saa7134_dma_free(q
,buf
);
1082 buffer_setup(struct videobuf_queue
*q
, unsigned int *count
, unsigned int *size
)
1084 struct saa7134_fh
*fh
= q
->priv_data
;
1086 *size
= fh
->fmt
->depth
* fh
->width
* fh
->height
>> 3;
1089 *count
= saa7134_buffer_count(*size
,*count
);
1093 static void buffer_queue(struct videobuf_queue
*q
, struct videobuf_buffer
*vb
)
1095 struct saa7134_fh
*fh
= q
->priv_data
;
1096 struct saa7134_buf
*buf
= container_of(vb
,struct saa7134_buf
,vb
);
1098 saa7134_buffer_queue(fh
->dev
,&fh
->dev
->video_q
,buf
);
1101 static void buffer_release(struct videobuf_queue
*q
, struct videobuf_buffer
*vb
)
1103 struct saa7134_buf
*buf
= container_of(vb
,struct saa7134_buf
,vb
);
1105 saa7134_dma_free(q
,buf
);
1108 static struct videobuf_queue_ops video_qops
= {
1109 .buf_setup
= buffer_setup
,
1110 .buf_prepare
= buffer_prepare
,
1111 .buf_queue
= buffer_queue
,
1112 .buf_release
= buffer_release
,
1115 /* ------------------------------------------------------------------ */
1117 int saa7134_g_ctrl_internal(struct saa7134_dev
*dev
, struct saa7134_fh
*fh
, struct v4l2_control
*c
)
1119 const struct v4l2_queryctrl
* ctrl
;
1121 ctrl
= ctrl_by_id(c
->id
);
1125 case V4L2_CID_BRIGHTNESS
:
1126 c
->value
= dev
->ctl_bright
;
1129 c
->value
= dev
->ctl_hue
;
1131 case V4L2_CID_CONTRAST
:
1132 c
->value
= dev
->ctl_contrast
;
1134 case V4L2_CID_SATURATION
:
1135 c
->value
= dev
->ctl_saturation
;
1137 case V4L2_CID_AUDIO_MUTE
:
1138 c
->value
= dev
->ctl_mute
;
1140 case V4L2_CID_AUDIO_VOLUME
:
1141 c
->value
= dev
->ctl_volume
;
1143 case V4L2_CID_PRIVATE_INVERT
:
1144 c
->value
= dev
->ctl_invert
;
1146 case V4L2_CID_HFLIP
:
1147 c
->value
= dev
->ctl_mirror
;
1149 case V4L2_CID_PRIVATE_Y_EVEN
:
1150 c
->value
= dev
->ctl_y_even
;
1152 case V4L2_CID_PRIVATE_Y_ODD
:
1153 c
->value
= dev
->ctl_y_odd
;
1155 case V4L2_CID_PRIVATE_AUTOMUTE
:
1156 c
->value
= dev
->ctl_automute
;
1163 EXPORT_SYMBOL_GPL(saa7134_g_ctrl_internal
);
1165 static int saa7134_g_ctrl(struct file
*file
, void *priv
, struct v4l2_control
*c
)
1167 struct saa7134_fh
*fh
= priv
;
1169 return saa7134_g_ctrl_internal(fh
->dev
, fh
, c
);
1172 int saa7134_s_ctrl_internal(struct saa7134_dev
*dev
, struct saa7134_fh
*fh
, struct v4l2_control
*c
)
1174 const struct v4l2_queryctrl
* ctrl
;
1175 unsigned long flags
;
1176 int restart_overlay
= 0;
1179 /* When called from the empress code fh == NULL.
1180 That needs to be fixed somehow, but for now this is
1183 err
= v4l2_prio_check(&dev
->prio
, &fh
->prio
);
1189 mutex_lock(&dev
->lock
);
1191 ctrl
= ctrl_by_id(c
->id
);
1195 dprintk("set_control name=%s val=%d\n",ctrl
->name
,c
->value
);
1196 switch (ctrl
->type
) {
1197 case V4L2_CTRL_TYPE_BOOLEAN
:
1198 case V4L2_CTRL_TYPE_MENU
:
1199 case V4L2_CTRL_TYPE_INTEGER
:
1200 if (c
->value
< ctrl
->minimum
)
1201 c
->value
= ctrl
->minimum
;
1202 if (c
->value
> ctrl
->maximum
)
1203 c
->value
= ctrl
->maximum
;
1209 case V4L2_CID_BRIGHTNESS
:
1210 dev
->ctl_bright
= c
->value
;
1211 saa_writeb(SAA7134_DEC_LUMA_BRIGHT
, dev
->ctl_bright
);
1214 dev
->ctl_hue
= c
->value
;
1215 saa_writeb(SAA7134_DEC_CHROMA_HUE
, dev
->ctl_hue
);
1217 case V4L2_CID_CONTRAST
:
1218 dev
->ctl_contrast
= c
->value
;
1219 saa_writeb(SAA7134_DEC_LUMA_CONTRAST
,
1220 dev
->ctl_invert
? -dev
->ctl_contrast
: dev
->ctl_contrast
);
1222 case V4L2_CID_SATURATION
:
1223 dev
->ctl_saturation
= c
->value
;
1224 saa_writeb(SAA7134_DEC_CHROMA_SATURATION
,
1225 dev
->ctl_invert
? -dev
->ctl_saturation
: dev
->ctl_saturation
);
1227 case V4L2_CID_AUDIO_MUTE
:
1228 dev
->ctl_mute
= c
->value
;
1229 saa7134_tvaudio_setmute(dev
);
1231 case V4L2_CID_AUDIO_VOLUME
:
1232 dev
->ctl_volume
= c
->value
;
1233 saa7134_tvaudio_setvolume(dev
,dev
->ctl_volume
);
1235 case V4L2_CID_PRIVATE_INVERT
:
1236 dev
->ctl_invert
= c
->value
;
1237 saa_writeb(SAA7134_DEC_LUMA_CONTRAST
,
1238 dev
->ctl_invert
? -dev
->ctl_contrast
: dev
->ctl_contrast
);
1239 saa_writeb(SAA7134_DEC_CHROMA_SATURATION
,
1240 dev
->ctl_invert
? -dev
->ctl_saturation
: dev
->ctl_saturation
);
1242 case V4L2_CID_HFLIP
:
1243 dev
->ctl_mirror
= c
->value
;
1244 restart_overlay
= 1;
1246 case V4L2_CID_PRIVATE_Y_EVEN
:
1247 dev
->ctl_y_even
= c
->value
;
1248 restart_overlay
= 1;
1250 case V4L2_CID_PRIVATE_Y_ODD
:
1251 dev
->ctl_y_odd
= c
->value
;
1252 restart_overlay
= 1;
1254 case V4L2_CID_PRIVATE_AUTOMUTE
:
1256 struct v4l2_priv_tun_config tda9887_cfg
;
1258 tda9887_cfg
.tuner
= TUNER_TDA9887
;
1259 tda9887_cfg
.priv
= &dev
->tda9887_conf
;
1261 dev
->ctl_automute
= c
->value
;
1262 if (dev
->tda9887_conf
) {
1263 if (dev
->ctl_automute
)
1264 dev
->tda9887_conf
|= TDA9887_AUTOMUTE
;
1266 dev
->tda9887_conf
&= ~TDA9887_AUTOMUTE
;
1268 saa_call_all(dev
, tuner
, s_config
, &tda9887_cfg
);
1275 if (restart_overlay
&& fh
&& res_check(fh
, RESOURCE_OVERLAY
)) {
1276 spin_lock_irqsave(&dev
->slock
,flags
);
1277 stop_preview(dev
,fh
);
1278 start_preview(dev
,fh
);
1279 spin_unlock_irqrestore(&dev
->slock
,flags
);
1284 mutex_unlock(&dev
->lock
);
1287 EXPORT_SYMBOL_GPL(saa7134_s_ctrl_internal
);
1289 static int saa7134_s_ctrl(struct file
*file
, void *f
, struct v4l2_control
*c
)
1291 struct saa7134_fh
*fh
= f
;
1293 return saa7134_s_ctrl_internal(fh
->dev
, fh
, c
);
1296 /* ------------------------------------------------------------------ */
1298 static struct videobuf_queue
* saa7134_queue(struct saa7134_fh
*fh
)
1300 struct videobuf_queue
* q
= NULL
;
1303 case V4L2_BUF_TYPE_VIDEO_CAPTURE
:
1306 case V4L2_BUF_TYPE_VBI_CAPTURE
:
1315 static int saa7134_resource(struct saa7134_fh
*fh
)
1317 if (fh
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1318 return RESOURCE_VIDEO
;
1320 if (fh
->type
== V4L2_BUF_TYPE_VBI_CAPTURE
)
1321 return RESOURCE_VBI
;
1327 static int video_open(struct file
*file
)
1329 int minor
= video_devdata(file
)->minor
;
1330 struct saa7134_dev
*dev
;
1331 struct saa7134_fh
*fh
;
1332 enum v4l2_buf_type type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1335 mutex_lock(&saa7134_devlist_lock
);
1336 list_for_each_entry(dev
, &saa7134_devlist
, devlist
) {
1337 if (dev
->video_dev
&& (dev
->video_dev
->minor
== minor
))
1339 if (dev
->radio_dev
&& (dev
->radio_dev
->minor
== minor
)) {
1343 if (dev
->vbi_dev
&& (dev
->vbi_dev
->minor
== minor
)) {
1344 type
= V4L2_BUF_TYPE_VBI_CAPTURE
;
1348 mutex_unlock(&saa7134_devlist_lock
);
1352 mutex_unlock(&saa7134_devlist_lock
);
1354 dprintk("open minor=%d radio=%d type=%s\n",minor
,radio
,
1355 v4l2_type_names
[type
]);
1357 /* allocate + initialize per filehandle data */
1358 fh
= kzalloc(sizeof(*fh
),GFP_KERNEL
);
1362 file
->private_data
= fh
;
1366 fh
->fmt
= format_by_fourcc(V4L2_PIX_FMT_BGR24
);
1369 v4l2_prio_open(&dev
->prio
,&fh
->prio
);
1371 videobuf_queue_sg_init(&fh
->cap
, &video_qops
,
1372 &dev
->pci
->dev
, &dev
->slock
,
1373 V4L2_BUF_TYPE_VIDEO_CAPTURE
,
1374 V4L2_FIELD_INTERLACED
,
1375 sizeof(struct saa7134_buf
),
1377 videobuf_queue_sg_init(&fh
->vbi
, &saa7134_vbi_qops
,
1378 &dev
->pci
->dev
, &dev
->slock
,
1379 V4L2_BUF_TYPE_VBI_CAPTURE
,
1381 sizeof(struct saa7134_buf
),
1383 saa7134_pgtable_alloc(dev
->pci
,&fh
->pt_cap
);
1384 saa7134_pgtable_alloc(dev
->pci
,&fh
->pt_vbi
);
1387 /* switch to radio mode */
1388 saa7134_tvaudio_setinput(dev
,&card(dev
).radio
);
1389 saa_call_all(dev
, tuner
, s_radio
);
1391 /* switch to video/vbi mode */
1392 video_mux(dev
,dev
->ctl_input
);
1398 video_read(struct file
*file
, char __user
*data
, size_t count
, loff_t
*ppos
)
1400 struct saa7134_fh
*fh
= file
->private_data
;
1403 case V4L2_BUF_TYPE_VIDEO_CAPTURE
:
1404 if (res_locked(fh
->dev
,RESOURCE_VIDEO
))
1406 return videobuf_read_one(saa7134_queue(fh
),
1408 file
->f_flags
& O_NONBLOCK
);
1409 case V4L2_BUF_TYPE_VBI_CAPTURE
:
1410 if (!res_get(fh
->dev
,fh
,RESOURCE_VBI
))
1412 return videobuf_read_stream(saa7134_queue(fh
),
1413 data
, count
, ppos
, 1,
1414 file
->f_flags
& O_NONBLOCK
);
1423 video_poll(struct file
*file
, struct poll_table_struct
*wait
)
1425 struct saa7134_fh
*fh
= file
->private_data
;
1426 struct videobuf_buffer
*buf
= NULL
;
1427 unsigned int rc
= 0;
1429 if (V4L2_BUF_TYPE_VBI_CAPTURE
== fh
->type
)
1430 return videobuf_poll_stream(file
, &fh
->vbi
, wait
);
1432 if (res_check(fh
,RESOURCE_VIDEO
)) {
1433 mutex_lock(&fh
->cap
.vb_lock
);
1434 if (!list_empty(&fh
->cap
.stream
))
1435 buf
= list_entry(fh
->cap
.stream
.next
, struct videobuf_buffer
, stream
);
1437 mutex_lock(&fh
->cap
.vb_lock
);
1438 if (UNSET
== fh
->cap
.read_off
) {
1439 /* need to capture a new frame */
1440 if (res_locked(fh
->dev
,RESOURCE_VIDEO
))
1442 if (0 != fh
->cap
.ops
->buf_prepare(&fh
->cap
,fh
->cap
.read_buf
,fh
->cap
.field
))
1444 fh
->cap
.ops
->buf_queue(&fh
->cap
,fh
->cap
.read_buf
);
1445 fh
->cap
.read_off
= 0;
1447 buf
= fh
->cap
.read_buf
;
1453 poll_wait(file
, &buf
->done
, wait
);
1454 if (buf
->state
== VIDEOBUF_DONE
||
1455 buf
->state
== VIDEOBUF_ERROR
)
1456 rc
= POLLIN
|POLLRDNORM
;
1457 mutex_unlock(&fh
->cap
.vb_lock
);
1461 mutex_unlock(&fh
->cap
.vb_lock
);
1465 static int video_release(struct file
*file
)
1467 struct saa7134_fh
*fh
= file
->private_data
;
1468 struct saa7134_dev
*dev
= fh
->dev
;
1469 struct rds_command cmd
;
1470 unsigned long flags
;
1472 /* turn off overlay */
1473 if (res_check(fh
, RESOURCE_OVERLAY
)) {
1474 spin_lock_irqsave(&dev
->slock
,flags
);
1475 stop_preview(dev
,fh
);
1476 spin_unlock_irqrestore(&dev
->slock
,flags
);
1477 res_free(dev
,fh
,RESOURCE_OVERLAY
);
1480 /* stop video capture */
1481 if (res_check(fh
, RESOURCE_VIDEO
)) {
1482 videobuf_streamoff(&fh
->cap
);
1483 res_free(dev
,fh
,RESOURCE_VIDEO
);
1485 if (fh
->cap
.read_buf
) {
1486 buffer_release(&fh
->cap
,fh
->cap
.read_buf
);
1487 kfree(fh
->cap
.read_buf
);
1490 /* stop vbi capture */
1491 if (res_check(fh
, RESOURCE_VBI
)) {
1492 videobuf_stop(&fh
->vbi
);
1493 res_free(dev
,fh
,RESOURCE_VBI
);
1496 /* ts-capture will not work in planar mode, so turn it off Hac: 04.05*/
1497 saa_andorb(SAA7134_OFMT_VIDEO_A
, 0x1f, 0);
1498 saa_andorb(SAA7134_OFMT_VIDEO_B
, 0x1f, 0);
1499 saa_andorb(SAA7134_OFMT_DATA_A
, 0x1f, 0);
1500 saa_andorb(SAA7134_OFMT_DATA_B
, 0x1f, 0);
1502 saa_call_all(dev
, tuner
, s_standby
);
1504 saa_call_all(dev
, core
, ioctl
, RDS_CMD_CLOSE
, &cmd
);
1507 videobuf_mmap_free(&fh
->cap
);
1508 videobuf_mmap_free(&fh
->vbi
);
1509 saa7134_pgtable_free(dev
->pci
,&fh
->pt_cap
);
1510 saa7134_pgtable_free(dev
->pci
,&fh
->pt_vbi
);
1512 v4l2_prio_close(&dev
->prio
,&fh
->prio
);
1513 file
->private_data
= NULL
;
1518 static int video_mmap(struct file
*file
, struct vm_area_struct
* vma
)
1520 struct saa7134_fh
*fh
= file
->private_data
;
1522 return videobuf_mmap_mapper(saa7134_queue(fh
), vma
);
1525 static ssize_t
radio_read(struct file
*file
, char __user
*data
,
1526 size_t count
, loff_t
*ppos
)
1528 struct saa7134_fh
*fh
= file
->private_data
;
1529 struct saa7134_dev
*dev
= fh
->dev
;
1530 struct rds_command cmd
;
1532 cmd
.block_count
= count
/3;
1534 cmd
.instance
= file
;
1535 cmd
.result
= -ENODEV
;
1537 saa_call_all(dev
, core
, ioctl
, RDS_CMD_READ
, &cmd
);
1542 static unsigned int radio_poll(struct file
*file
, poll_table
*wait
)
1544 struct saa7134_fh
*fh
= file
->private_data
;
1545 struct saa7134_dev
*dev
= fh
->dev
;
1546 struct rds_command cmd
;
1548 cmd
.instance
= file
;
1549 cmd
.event_list
= wait
;
1550 cmd
.result
= -ENODEV
;
1551 saa_call_all(dev
, core
, ioctl
, RDS_CMD_POLL
, &cmd
);
1556 /* ------------------------------------------------------------------ */
1558 static int saa7134_try_get_set_fmt_vbi_cap(struct file
*file
, void *priv
,
1559 struct v4l2_format
*f
)
1561 struct saa7134_fh
*fh
= priv
;
1562 struct saa7134_dev
*dev
= fh
->dev
;
1563 struct saa7134_tvnorm
*norm
= dev
->tvnorm
;
1565 f
->fmt
.vbi
.sampling_rate
= 6750000 * 4;
1566 f
->fmt
.vbi
.samples_per_line
= 2048 /* VBI_LINE_LENGTH */;
1567 f
->fmt
.vbi
.sample_format
= V4L2_PIX_FMT_GREY
;
1568 f
->fmt
.vbi
.offset
= 64 * 4;
1569 f
->fmt
.vbi
.start
[0] = norm
->vbi_v_start_0
;
1570 f
->fmt
.vbi
.count
[0] = norm
->vbi_v_stop_0
- norm
->vbi_v_start_0
+1;
1571 f
->fmt
.vbi
.start
[1] = norm
->vbi_v_start_1
;
1572 f
->fmt
.vbi
.count
[1] = f
->fmt
.vbi
.count
[0];
1573 f
->fmt
.vbi
.flags
= 0; /* VBI_UNSYNC VBI_INTERLACED */
1578 static int saa7134_g_fmt_vid_cap(struct file
*file
, void *priv
,
1579 struct v4l2_format
*f
)
1581 struct saa7134_fh
*fh
= priv
;
1583 f
->fmt
.pix
.width
= fh
->width
;
1584 f
->fmt
.pix
.height
= fh
->height
;
1585 f
->fmt
.pix
.field
= fh
->cap
.field
;
1586 f
->fmt
.pix
.pixelformat
= fh
->fmt
->fourcc
;
1587 f
->fmt
.pix
.bytesperline
=
1588 (f
->fmt
.pix
.width
* fh
->fmt
->depth
) >> 3;
1589 f
->fmt
.pix
.sizeimage
=
1590 f
->fmt
.pix
.height
* f
->fmt
.pix
.bytesperline
;
1594 static int saa7134_g_fmt_vid_overlay(struct file
*file
, void *priv
,
1595 struct v4l2_format
*f
)
1597 struct saa7134_fh
*fh
= priv
;
1599 if (saa7134_no_overlay
> 0) {
1600 printk(KERN_ERR
"V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
1603 f
->fmt
.win
= fh
->win
;
1608 static int saa7134_try_fmt_vid_cap(struct file
*file
, void *priv
,
1609 struct v4l2_format
*f
)
1611 struct saa7134_fh
*fh
= priv
;
1612 struct saa7134_dev
*dev
= fh
->dev
;
1613 struct saa7134_format
*fmt
;
1614 enum v4l2_field field
;
1615 unsigned int maxw
, maxh
;
1617 fmt
= format_by_fourcc(f
->fmt
.pix
.pixelformat
);
1621 field
= f
->fmt
.pix
.field
;
1622 maxw
= min(dev
->crop_current
.width
*4, dev
->crop_bounds
.width
);
1623 maxh
= min(dev
->crop_current
.height
*4, dev
->crop_bounds
.height
);
1625 if (V4L2_FIELD_ANY
== field
) {
1626 field
= (f
->fmt
.pix
.height
> maxh
/2)
1627 ? V4L2_FIELD_INTERLACED
1628 : V4L2_FIELD_BOTTOM
;
1631 case V4L2_FIELD_TOP
:
1632 case V4L2_FIELD_BOTTOM
:
1635 case V4L2_FIELD_INTERLACED
:
1641 f
->fmt
.pix
.field
= field
;
1642 v4l_bound_align_image(&f
->fmt
.pix
.width
, 48, maxw
, 2,
1643 &f
->fmt
.pix
.height
, 32, maxh
, 0, 0);
1644 f
->fmt
.pix
.bytesperline
=
1645 (f
->fmt
.pix
.width
* fmt
->depth
) >> 3;
1646 f
->fmt
.pix
.sizeimage
=
1647 f
->fmt
.pix
.height
* f
->fmt
.pix
.bytesperline
;
1652 static int saa7134_try_fmt_vid_overlay(struct file
*file
, void *priv
,
1653 struct v4l2_format
*f
)
1655 struct saa7134_fh
*fh
= priv
;
1656 struct saa7134_dev
*dev
= fh
->dev
;
1658 if (saa7134_no_overlay
> 0) {
1659 printk(KERN_ERR
"V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
1663 return verify_preview(dev
, &f
->fmt
.win
);
1666 static int saa7134_s_fmt_vid_cap(struct file
*file
, void *priv
,
1667 struct v4l2_format
*f
)
1669 struct saa7134_fh
*fh
= priv
;
1672 err
= saa7134_try_fmt_vid_cap(file
, priv
, f
);
1676 fh
->fmt
= format_by_fourcc(f
->fmt
.pix
.pixelformat
);
1677 fh
->width
= f
->fmt
.pix
.width
;
1678 fh
->height
= f
->fmt
.pix
.height
;
1679 fh
->cap
.field
= f
->fmt
.pix
.field
;
1683 static int saa7134_s_fmt_vid_overlay(struct file
*file
, void *priv
,
1684 struct v4l2_format
*f
)
1686 struct saa7134_fh
*fh
= priv
;
1687 struct saa7134_dev
*dev
= fh
->dev
;
1689 unsigned long flags
;
1691 if (saa7134_no_overlay
> 0) {
1692 printk(KERN_ERR
"V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
1695 err
= verify_preview(dev
, &f
->fmt
.win
);
1699 mutex_lock(&dev
->lock
);
1701 fh
->win
= f
->fmt
.win
;
1702 fh
->nclips
= f
->fmt
.win
.clipcount
;
1707 if (copy_from_user(fh
->clips
, f
->fmt
.win
.clips
,
1708 sizeof(struct v4l2_clip
)*fh
->nclips
)) {
1709 mutex_unlock(&dev
->lock
);
1713 if (res_check(fh
, RESOURCE_OVERLAY
)) {
1714 spin_lock_irqsave(&dev
->slock
, flags
);
1715 stop_preview(dev
, fh
);
1716 start_preview(dev
, fh
);
1717 spin_unlock_irqrestore(&dev
->slock
, flags
);
1720 mutex_unlock(&dev
->lock
);
1724 int saa7134_queryctrl(struct file
*file
, void *priv
, struct v4l2_queryctrl
*c
)
1726 const struct v4l2_queryctrl
*ctrl
;
1728 if ((c
->id
< V4L2_CID_BASE
||
1729 c
->id
>= V4L2_CID_LASTP1
) &&
1730 (c
->id
< V4L2_CID_PRIVATE_BASE
||
1731 c
->id
>= V4L2_CID_PRIVATE_LASTP1
))
1733 ctrl
= ctrl_by_id(c
->id
);
1734 *c
= (NULL
!= ctrl
) ? *ctrl
: no_ctrl
;
1737 EXPORT_SYMBOL_GPL(saa7134_queryctrl
);
1739 static int saa7134_enum_input(struct file
*file
, void *priv
,
1740 struct v4l2_input
*i
)
1742 struct saa7134_fh
*fh
= priv
;
1743 struct saa7134_dev
*dev
= fh
->dev
;
1747 if (n
>= SAA7134_INPUT_MAX
)
1749 if (NULL
== card_in(dev
, i
->index
).name
)
1751 memset(i
, 0, sizeof(*i
));
1753 i
->type
= V4L2_INPUT_TYPE_CAMERA
;
1754 strcpy(i
->name
, card_in(dev
, n
).name
);
1755 if (card_in(dev
, n
).tv
)
1756 i
->type
= V4L2_INPUT_TYPE_TUNER
;
1758 if (n
== dev
->ctl_input
) {
1759 int v1
= saa_readb(SAA7134_STATUS_VIDEO1
);
1760 int v2
= saa_readb(SAA7134_STATUS_VIDEO2
);
1762 if (0 != (v1
& 0x40))
1763 i
->status
|= V4L2_IN_ST_NO_H_LOCK
;
1764 if (0 != (v2
& 0x40))
1765 i
->status
|= V4L2_IN_ST_NO_SYNC
;
1766 if (0 != (v2
& 0x0e))
1767 i
->status
|= V4L2_IN_ST_MACROVISION
;
1769 i
->std
= SAA7134_NORMS
;
1773 static int saa7134_g_input(struct file
*file
, void *priv
, unsigned int *i
)
1775 struct saa7134_fh
*fh
= priv
;
1776 struct saa7134_dev
*dev
= fh
->dev
;
1778 *i
= dev
->ctl_input
;
1782 static int saa7134_s_input(struct file
*file
, void *priv
, unsigned int i
)
1784 struct saa7134_fh
*fh
= priv
;
1785 struct saa7134_dev
*dev
= fh
->dev
;
1788 err
= v4l2_prio_check(&dev
->prio
, &fh
->prio
);
1792 if (i
>= SAA7134_INPUT_MAX
)
1794 if (NULL
== card_in(dev
, i
).name
)
1796 mutex_lock(&dev
->lock
);
1798 mutex_unlock(&dev
->lock
);
1802 static int saa7134_querycap(struct file
*file
, void *priv
,
1803 struct v4l2_capability
*cap
)
1805 struct saa7134_fh
*fh
= priv
;
1806 struct saa7134_dev
*dev
= fh
->dev
;
1808 unsigned int tuner_type
= dev
->tuner_type
;
1810 strcpy(cap
->driver
, "saa7134");
1811 strlcpy(cap
->card
, saa7134_boards
[dev
->board
].name
,
1813 sprintf(cap
->bus_info
, "PCI:%s", pci_name(dev
->pci
));
1814 cap
->version
= SAA7134_VERSION_CODE
;
1816 V4L2_CAP_VIDEO_CAPTURE
|
1817 V4L2_CAP_VBI_CAPTURE
|
1818 V4L2_CAP_READWRITE
|
1819 V4L2_CAP_STREAMING
|
1822 cap
->capabilities
|= V4L2_CAP_RDS_CAPTURE
;
1823 if (saa7134_no_overlay
<= 0)
1824 cap
->capabilities
|= V4L2_CAP_VIDEO_OVERLAY
;
1826 if ((tuner_type
== TUNER_ABSENT
) || (tuner_type
== UNSET
))
1827 cap
->capabilities
&= ~V4L2_CAP_TUNER
;
1831 int saa7134_s_std_internal(struct saa7134_dev
*dev
, struct saa7134_fh
*fh
, v4l2_std_id
*id
)
1833 unsigned long flags
;
1838 /* When called from the empress code fh == NULL.
1839 That needs to be fixed somehow, but for now this is
1842 err
= v4l2_prio_check(&dev
->prio
, &fh
->prio
);
1845 } else if (res_locked(dev
, RESOURCE_OVERLAY
)) {
1846 /* Don't change the std from the mpeg device
1847 if overlay is active. */
1851 for (i
= 0; i
< TVNORMS
; i
++)
1852 if (*id
== tvnorms
[i
].id
)
1856 for (i
= 0; i
< TVNORMS
; i
++)
1857 if (*id
& tvnorms
[i
].id
)
1862 if ((*id
& V4L2_STD_SECAM
) && (secam
[0] != '-')) {
1863 if (secam
[0] == 'L' || secam
[0] == 'l') {
1864 if (secam
[1] == 'C' || secam
[1] == 'c')
1865 fixup
= V4L2_STD_SECAM_LC
;
1867 fixup
= V4L2_STD_SECAM_L
;
1869 if (secam
[0] == 'D' || secam
[0] == 'd')
1870 fixup
= V4L2_STD_SECAM_DK
;
1872 fixup
= V4L2_STD_SECAM
;
1874 for (i
= 0; i
< TVNORMS
; i
++)
1875 if (fixup
== tvnorms
[i
].id
)
1879 *id
= tvnorms
[i
].id
;
1881 mutex_lock(&dev
->lock
);
1882 if (fh
&& res_check(fh
, RESOURCE_OVERLAY
)) {
1883 spin_lock_irqsave(&dev
->slock
, flags
);
1884 stop_preview(dev
, fh
);
1885 spin_unlock_irqrestore(&dev
->slock
, flags
);
1887 set_tvnorm(dev
, &tvnorms
[i
]);
1889 spin_lock_irqsave(&dev
->slock
, flags
);
1890 start_preview(dev
, fh
);
1891 spin_unlock_irqrestore(&dev
->slock
, flags
);
1893 set_tvnorm(dev
, &tvnorms
[i
]);
1895 saa7134_tvaudio_do_scan(dev
);
1896 mutex_unlock(&dev
->lock
);
1899 EXPORT_SYMBOL_GPL(saa7134_s_std_internal
);
1901 static int saa7134_s_std(struct file
*file
, void *priv
, v4l2_std_id
*id
)
1903 struct saa7134_fh
*fh
= priv
;
1905 return saa7134_s_std_internal(fh
->dev
, fh
, id
);
1908 static int saa7134_g_std(struct file
*file
, void *priv
, v4l2_std_id
*id
)
1910 struct saa7134_fh
*fh
= priv
;
1911 struct saa7134_dev
*dev
= fh
->dev
;
1913 *id
= dev
->tvnorm
->id
;
1917 static int saa7134_cropcap(struct file
*file
, void *priv
,
1918 struct v4l2_cropcap
*cap
)
1920 struct saa7134_fh
*fh
= priv
;
1921 struct saa7134_dev
*dev
= fh
->dev
;
1923 if (cap
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
&&
1924 cap
->type
!= V4L2_BUF_TYPE_VIDEO_OVERLAY
)
1926 cap
->bounds
= dev
->crop_bounds
;
1927 cap
->defrect
= dev
->crop_defrect
;
1928 cap
->pixelaspect
.numerator
= 1;
1929 cap
->pixelaspect
.denominator
= 1;
1930 if (dev
->tvnorm
->id
& V4L2_STD_525_60
) {
1931 cap
->pixelaspect
.numerator
= 11;
1932 cap
->pixelaspect
.denominator
= 10;
1934 if (dev
->tvnorm
->id
& V4L2_STD_625_50
) {
1935 cap
->pixelaspect
.numerator
= 54;
1936 cap
->pixelaspect
.denominator
= 59;
1941 static int saa7134_g_crop(struct file
*file
, void *f
, struct v4l2_crop
*crop
)
1943 struct saa7134_fh
*fh
= f
;
1944 struct saa7134_dev
*dev
= fh
->dev
;
1946 if (crop
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
&&
1947 crop
->type
!= V4L2_BUF_TYPE_VIDEO_OVERLAY
)
1949 crop
->c
= dev
->crop_current
;
1953 static int saa7134_s_crop(struct file
*file
, void *f
, struct v4l2_crop
*crop
)
1955 struct saa7134_fh
*fh
= f
;
1956 struct saa7134_dev
*dev
= fh
->dev
;
1957 struct v4l2_rect
*b
= &dev
->crop_bounds
;
1959 if (crop
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
&&
1960 crop
->type
!= V4L2_BUF_TYPE_VIDEO_OVERLAY
)
1962 if (crop
->c
.height
< 0)
1964 if (crop
->c
.width
< 0)
1967 if (res_locked(fh
->dev
, RESOURCE_OVERLAY
))
1969 if (res_locked(fh
->dev
, RESOURCE_VIDEO
))
1972 if (crop
->c
.top
< b
->top
)
1973 crop
->c
.top
= b
->top
;
1974 if (crop
->c
.top
> b
->top
+ b
->height
)
1975 crop
->c
.top
= b
->top
+ b
->height
;
1976 if (crop
->c
.height
> b
->top
- crop
->c
.top
+ b
->height
)
1977 crop
->c
.height
= b
->top
- crop
->c
.top
+ b
->height
;
1979 if (crop
->c
.left
< b
->left
)
1980 crop
->c
.left
= b
->left
;
1981 if (crop
->c
.left
> b
->left
+ b
->width
)
1982 crop
->c
.left
= b
->left
+ b
->width
;
1983 if (crop
->c
.width
> b
->left
- crop
->c
.left
+ b
->width
)
1984 crop
->c
.width
= b
->left
- crop
->c
.left
+ b
->width
;
1986 dev
->crop_current
= crop
->c
;
1990 static int saa7134_g_tuner(struct file
*file
, void *priv
,
1991 struct v4l2_tuner
*t
)
1993 struct saa7134_fh
*fh
= priv
;
1994 struct saa7134_dev
*dev
= fh
->dev
;
1999 memset(t
, 0, sizeof(*t
));
2000 for (n
= 0; n
< SAA7134_INPUT_MAX
; n
++)
2001 if (card_in(dev
, n
).tv
)
2003 if (NULL
!= card_in(dev
, n
).name
) {
2004 strcpy(t
->name
, "Television");
2005 t
->type
= V4L2_TUNER_ANALOG_TV
;
2006 t
->capability
= V4L2_TUNER_CAP_NORM
|
2007 V4L2_TUNER_CAP_STEREO
|
2008 V4L2_TUNER_CAP_LANG1
|
2009 V4L2_TUNER_CAP_LANG2
;
2010 t
->rangehigh
= 0xffffffffUL
;
2011 t
->rxsubchans
= saa7134_tvaudio_getstereo(dev
);
2012 t
->audmode
= saa7134_tvaudio_rx2mode(t
->rxsubchans
);
2014 if (0 != (saa_readb(SAA7134_STATUS_VIDEO1
) & 0x03))
2019 static int saa7134_s_tuner(struct file
*file
, void *priv
,
2020 struct v4l2_tuner
*t
)
2022 struct saa7134_fh
*fh
= priv
;
2023 struct saa7134_dev
*dev
= fh
->dev
;
2026 err
= v4l2_prio_check(&dev
->prio
, &fh
->prio
);
2030 mode
= dev
->thread
.mode
;
2031 if (UNSET
== mode
) {
2032 rx
= saa7134_tvaudio_getstereo(dev
);
2033 mode
= saa7134_tvaudio_rx2mode(t
->rxsubchans
);
2035 if (mode
!= t
->audmode
)
2036 dev
->thread
.mode
= t
->audmode
;
2041 static int saa7134_g_frequency(struct file
*file
, void *priv
,
2042 struct v4l2_frequency
*f
)
2044 struct saa7134_fh
*fh
= priv
;
2045 struct saa7134_dev
*dev
= fh
->dev
;
2047 f
->type
= fh
->radio
? V4L2_TUNER_RADIO
: V4L2_TUNER_ANALOG_TV
;
2048 f
->frequency
= dev
->ctl_freq
;
2053 static int saa7134_s_frequency(struct file
*file
, void *priv
,
2054 struct v4l2_frequency
*f
)
2056 struct saa7134_fh
*fh
= priv
;
2057 struct saa7134_dev
*dev
= fh
->dev
;
2060 err
= v4l2_prio_check(&dev
->prio
, &fh
->prio
);
2066 if (0 == fh
->radio
&& V4L2_TUNER_ANALOG_TV
!= f
->type
)
2068 if (1 == fh
->radio
&& V4L2_TUNER_RADIO
!= f
->type
)
2070 mutex_lock(&dev
->lock
);
2071 dev
->ctl_freq
= f
->frequency
;
2073 saa_call_all(dev
, tuner
, s_frequency
, f
);
2075 saa7134_tvaudio_do_scan(dev
);
2076 mutex_unlock(&dev
->lock
);
2080 static int saa7134_g_audio(struct file
*file
, void *priv
, struct v4l2_audio
*a
)
2082 strcpy(a
->name
, "audio");
2086 static int saa7134_s_audio(struct file
*file
, void *priv
, struct v4l2_audio
*a
)
2091 static int saa7134_g_priority(struct file
*file
, void *f
, enum v4l2_priority
*p
)
2093 struct saa7134_fh
*fh
= f
;
2094 struct saa7134_dev
*dev
= fh
->dev
;
2096 *p
= v4l2_prio_max(&dev
->prio
);
2100 static int saa7134_s_priority(struct file
*file
, void *f
,
2101 enum v4l2_priority prio
)
2103 struct saa7134_fh
*fh
= f
;
2104 struct saa7134_dev
*dev
= fh
->dev
;
2106 return v4l2_prio_change(&dev
->prio
, &fh
->prio
, prio
);
2109 static int saa7134_enum_fmt_vid_cap(struct file
*file
, void *priv
,
2110 struct v4l2_fmtdesc
*f
)
2112 if (f
->index
>= FORMATS
)
2115 strlcpy(f
->description
, formats
[f
->index
].name
,
2116 sizeof(f
->description
));
2118 f
->pixelformat
= formats
[f
->index
].fourcc
;
2123 static int saa7134_enum_fmt_vid_overlay(struct file
*file
, void *priv
,
2124 struct v4l2_fmtdesc
*f
)
2126 if (saa7134_no_overlay
> 0) {
2127 printk(KERN_ERR
"V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
2131 if ((f
->index
>= FORMATS
) || formats
[f
->index
].planar
)
2134 strlcpy(f
->description
, formats
[f
->index
].name
,
2135 sizeof(f
->description
));
2137 f
->pixelformat
= formats
[f
->index
].fourcc
;
2142 static int saa7134_g_fbuf(struct file
*file
, void *f
,
2143 struct v4l2_framebuffer
*fb
)
2145 struct saa7134_fh
*fh
= f
;
2146 struct saa7134_dev
*dev
= fh
->dev
;
2149 fb
->capability
= V4L2_FBUF_CAP_LIST_CLIPPING
;
2154 static int saa7134_s_fbuf(struct file
*file
, void *f
,
2155 struct v4l2_framebuffer
*fb
)
2157 struct saa7134_fh
*fh
= f
;
2158 struct saa7134_dev
*dev
= fh
->dev
;
2159 struct saa7134_format
*fmt
;
2161 if (!capable(CAP_SYS_ADMIN
) &&
2162 !capable(CAP_SYS_RAWIO
))
2166 fmt
= format_by_fourcc(fb
->fmt
.pixelformat
);
2173 if (0 == dev
->ovbuf
.fmt
.bytesperline
)
2174 dev
->ovbuf
.fmt
.bytesperline
=
2175 dev
->ovbuf
.fmt
.width
*fmt
->depth
/8;
2179 static int saa7134_overlay(struct file
*file
, void *f
, unsigned int on
)
2181 struct saa7134_fh
*fh
= f
;
2182 struct saa7134_dev
*dev
= fh
->dev
;
2183 unsigned long flags
;
2186 if (saa7134_no_overlay
> 0) {
2187 dprintk("no_overlay\n");
2191 if (!res_get(dev
, fh
, RESOURCE_OVERLAY
))
2193 spin_lock_irqsave(&dev
->slock
, flags
);
2194 start_preview(dev
, fh
);
2195 spin_unlock_irqrestore(&dev
->slock
, flags
);
2198 if (!res_check(fh
, RESOURCE_OVERLAY
))
2200 spin_lock_irqsave(&dev
->slock
, flags
);
2201 stop_preview(dev
, fh
);
2202 spin_unlock_irqrestore(&dev
->slock
, flags
);
2203 res_free(dev
, fh
, RESOURCE_OVERLAY
);
2208 #ifdef CONFIG_VIDEO_V4L1_COMPAT
2209 static int vidiocgmbuf(struct file
*file
, void *priv
, struct video_mbuf
*mbuf
)
2211 struct saa7134_fh
*fh
= file
->private_data
;
2212 return videobuf_cgmbuf(saa7134_queue(fh
), mbuf
, 8);
2216 static int saa7134_reqbufs(struct file
*file
, void *priv
,
2217 struct v4l2_requestbuffers
*p
)
2219 struct saa7134_fh
*fh
= priv
;
2220 return videobuf_reqbufs(saa7134_queue(fh
), p
);
2223 static int saa7134_querybuf(struct file
*file
, void *priv
,
2224 struct v4l2_buffer
*b
)
2226 struct saa7134_fh
*fh
= priv
;
2227 return videobuf_querybuf(saa7134_queue(fh
), b
);
2230 static int saa7134_qbuf(struct file
*file
, void *priv
, struct v4l2_buffer
*b
)
2232 struct saa7134_fh
*fh
= priv
;
2233 return videobuf_qbuf(saa7134_queue(fh
), b
);
2236 static int saa7134_dqbuf(struct file
*file
, void *priv
, struct v4l2_buffer
*b
)
2238 struct saa7134_fh
*fh
= priv
;
2239 return videobuf_dqbuf(saa7134_queue(fh
), b
,
2240 file
->f_flags
& O_NONBLOCK
);
2243 static int saa7134_streamon(struct file
*file
, void *priv
,
2244 enum v4l2_buf_type type
)
2246 struct saa7134_fh
*fh
= priv
;
2247 struct saa7134_dev
*dev
= fh
->dev
;
2248 int res
= saa7134_resource(fh
);
2250 if (!res_get(dev
, fh
, res
))
2253 return videobuf_streamon(saa7134_queue(fh
));
2256 static int saa7134_streamoff(struct file
*file
, void *priv
,
2257 enum v4l2_buf_type type
)
2260 struct saa7134_fh
*fh
= priv
;
2261 struct saa7134_dev
*dev
= fh
->dev
;
2262 int res
= saa7134_resource(fh
);
2264 err
= videobuf_streamoff(saa7134_queue(fh
));
2267 res_free(dev
, fh
, res
);
2271 static int saa7134_g_parm(struct file
*file
, void *fh
,
2272 struct v4l2_streamparm
*parm
)
2277 #ifdef CONFIG_VIDEO_ADV_DEBUG
2278 static int vidioc_g_register (struct file
*file
, void *priv
,
2279 struct v4l2_dbg_register
*reg
)
2281 struct saa7134_fh
*fh
= priv
;
2282 struct saa7134_dev
*dev
= fh
->dev
;
2284 if (!v4l2_chip_match_host(®
->match
))
2286 reg
->val
= saa_readb(reg
->reg
);
2291 static int vidioc_s_register (struct file
*file
, void *priv
,
2292 struct v4l2_dbg_register
*reg
)
2294 struct saa7134_fh
*fh
= priv
;
2295 struct saa7134_dev
*dev
= fh
->dev
;
2297 if (!v4l2_chip_match_host(®
->match
))
2299 saa_writeb(reg
->reg
&0xffffff, reg
->val
);
2304 static int radio_querycap(struct file
*file
, void *priv
,
2305 struct v4l2_capability
*cap
)
2307 struct saa7134_fh
*fh
= file
->private_data
;
2308 struct saa7134_dev
*dev
= fh
->dev
;
2310 strcpy(cap
->driver
, "saa7134");
2311 strlcpy(cap
->card
, saa7134_boards
[dev
->board
].name
, sizeof(cap
->card
));
2312 sprintf(cap
->bus_info
, "PCI:%s", pci_name(dev
->pci
));
2313 cap
->version
= SAA7134_VERSION_CODE
;
2314 cap
->capabilities
= V4L2_CAP_TUNER
;
2318 static int radio_g_tuner(struct file
*file
, void *priv
,
2319 struct v4l2_tuner
*t
)
2321 struct saa7134_fh
*fh
= file
->private_data
;
2322 struct saa7134_dev
*dev
= fh
->dev
;
2327 memset(t
, 0, sizeof(*t
));
2328 strcpy(t
->name
, "Radio");
2329 t
->type
= V4L2_TUNER_RADIO
;
2331 saa_call_all(dev
, tuner
, g_tuner
, t
);
2332 if (dev
->input
->amux
== TV
) {
2333 t
->signal
= 0xf800 - ((saa_readb(0x581) & 0x1f) << 11);
2334 t
->rxsubchans
= (saa_readb(0x529) & 0x08) ?
2335 V4L2_TUNER_SUB_STEREO
: V4L2_TUNER_SUB_MONO
;
2339 static int radio_s_tuner(struct file
*file
, void *priv
,
2340 struct v4l2_tuner
*t
)
2342 struct saa7134_fh
*fh
= file
->private_data
;
2343 struct saa7134_dev
*dev
= fh
->dev
;
2348 saa_call_all(dev
, tuner
, s_tuner
, t
);
2352 static int radio_enum_input(struct file
*file
, void *priv
,
2353 struct v4l2_input
*i
)
2358 strcpy(i
->name
, "Radio");
2359 i
->type
= V4L2_INPUT_TYPE_TUNER
;
2364 static int radio_g_input(struct file
*filp
, void *priv
, unsigned int *i
)
2370 static int radio_g_audio(struct file
*file
, void *priv
,
2371 struct v4l2_audio
*a
)
2373 memset(a
, 0, sizeof(*a
));
2374 strcpy(a
->name
, "Radio");
2378 static int radio_s_audio(struct file
*file
, void *priv
,
2379 struct v4l2_audio
*a
)
2384 static int radio_s_input(struct file
*filp
, void *priv
, unsigned int i
)
2389 static int radio_s_std(struct file
*file
, void *fh
, v4l2_std_id
*norm
)
2394 static int radio_queryctrl(struct file
*file
, void *priv
,
2395 struct v4l2_queryctrl
*c
)
2397 const struct v4l2_queryctrl
*ctrl
;
2399 if (c
->id
< V4L2_CID_BASE
||
2400 c
->id
>= V4L2_CID_LASTP1
)
2402 if (c
->id
== V4L2_CID_AUDIO_MUTE
) {
2403 ctrl
= ctrl_by_id(c
->id
);
2410 static const struct v4l2_file_operations video_fops
=
2412 .owner
= THIS_MODULE
,
2414 .release
= video_release
,
2418 .ioctl
= video_ioctl2
,
2421 static const struct v4l2_ioctl_ops video_ioctl_ops
= {
2422 .vidioc_querycap
= saa7134_querycap
,
2423 .vidioc_enum_fmt_vid_cap
= saa7134_enum_fmt_vid_cap
,
2424 .vidioc_g_fmt_vid_cap
= saa7134_g_fmt_vid_cap
,
2425 .vidioc_try_fmt_vid_cap
= saa7134_try_fmt_vid_cap
,
2426 .vidioc_s_fmt_vid_cap
= saa7134_s_fmt_vid_cap
,
2427 .vidioc_enum_fmt_vid_overlay
= saa7134_enum_fmt_vid_overlay
,
2428 .vidioc_g_fmt_vid_overlay
= saa7134_g_fmt_vid_overlay
,
2429 .vidioc_try_fmt_vid_overlay
= saa7134_try_fmt_vid_overlay
,
2430 .vidioc_s_fmt_vid_overlay
= saa7134_s_fmt_vid_overlay
,
2431 .vidioc_g_fmt_vbi_cap
= saa7134_try_get_set_fmt_vbi_cap
,
2432 .vidioc_try_fmt_vbi_cap
= saa7134_try_get_set_fmt_vbi_cap
,
2433 .vidioc_s_fmt_vbi_cap
= saa7134_try_get_set_fmt_vbi_cap
,
2434 .vidioc_g_audio
= saa7134_g_audio
,
2435 .vidioc_s_audio
= saa7134_s_audio
,
2436 .vidioc_cropcap
= saa7134_cropcap
,
2437 .vidioc_reqbufs
= saa7134_reqbufs
,
2438 .vidioc_querybuf
= saa7134_querybuf
,
2439 .vidioc_qbuf
= saa7134_qbuf
,
2440 .vidioc_dqbuf
= saa7134_dqbuf
,
2441 .vidioc_s_std
= saa7134_s_std
,
2442 .vidioc_g_std
= saa7134_g_std
,
2443 .vidioc_enum_input
= saa7134_enum_input
,
2444 .vidioc_g_input
= saa7134_g_input
,
2445 .vidioc_s_input
= saa7134_s_input
,
2446 .vidioc_queryctrl
= saa7134_queryctrl
,
2447 .vidioc_g_ctrl
= saa7134_g_ctrl
,
2448 .vidioc_s_ctrl
= saa7134_s_ctrl
,
2449 .vidioc_streamon
= saa7134_streamon
,
2450 .vidioc_streamoff
= saa7134_streamoff
,
2451 .vidioc_g_tuner
= saa7134_g_tuner
,
2452 .vidioc_s_tuner
= saa7134_s_tuner
,
2453 #ifdef CONFIG_VIDEO_V4L1_COMPAT
2454 .vidiocgmbuf
= vidiocgmbuf
,
2456 .vidioc_g_crop
= saa7134_g_crop
,
2457 .vidioc_s_crop
= saa7134_s_crop
,
2458 .vidioc_g_fbuf
= saa7134_g_fbuf
,
2459 .vidioc_s_fbuf
= saa7134_s_fbuf
,
2460 .vidioc_overlay
= saa7134_overlay
,
2461 .vidioc_g_priority
= saa7134_g_priority
,
2462 .vidioc_s_priority
= saa7134_s_priority
,
2463 .vidioc_g_parm
= saa7134_g_parm
,
2464 .vidioc_g_frequency
= saa7134_g_frequency
,
2465 .vidioc_s_frequency
= saa7134_s_frequency
,
2466 #ifdef CONFIG_VIDEO_ADV_DEBUG
2467 .vidioc_g_register
= vidioc_g_register
,
2468 .vidioc_s_register
= vidioc_s_register
,
2472 static const struct v4l2_file_operations radio_fops
= {
2473 .owner
= THIS_MODULE
,
2476 .release
= video_release
,
2477 .ioctl
= video_ioctl2
,
2481 static const struct v4l2_ioctl_ops radio_ioctl_ops
= {
2482 .vidioc_querycap
= radio_querycap
,
2483 .vidioc_g_tuner
= radio_g_tuner
,
2484 .vidioc_enum_input
= radio_enum_input
,
2485 .vidioc_g_audio
= radio_g_audio
,
2486 .vidioc_s_tuner
= radio_s_tuner
,
2487 .vidioc_s_audio
= radio_s_audio
,
2488 .vidioc_s_input
= radio_s_input
,
2489 .vidioc_s_std
= radio_s_std
,
2490 .vidioc_queryctrl
= radio_queryctrl
,
2491 .vidioc_g_input
= radio_g_input
,
2492 .vidioc_g_ctrl
= saa7134_g_ctrl
,
2493 .vidioc_s_ctrl
= saa7134_s_ctrl
,
2494 .vidioc_g_frequency
= saa7134_g_frequency
,
2495 .vidioc_s_frequency
= saa7134_s_frequency
,
2498 /* ----------------------------------------------------------- */
2499 /* exported stuff */
2501 struct video_device saa7134_video_template
= {
2502 .name
= "saa7134-video",
2503 .fops
= &video_fops
,
2504 .ioctl_ops
= &video_ioctl_ops
,
2506 .tvnorms
= SAA7134_NORMS
,
2507 .current_norm
= V4L2_STD_PAL
,
2510 struct video_device saa7134_radio_template
= {
2511 .name
= "saa7134-radio",
2512 .fops
= &radio_fops
,
2513 .ioctl_ops
= &radio_ioctl_ops
,
2517 int saa7134_video_init1(struct saa7134_dev
*dev
)
2519 /* sanitycheck insmod options */
2520 if (gbuffers
< 2 || gbuffers
> VIDEO_MAX_FRAME
)
2522 if (gbufsize
< 0 || gbufsize
> gbufsize_max
)
2523 gbufsize
= gbufsize_max
;
2524 gbufsize
= (gbufsize
+ PAGE_SIZE
- 1) & PAGE_MASK
;
2526 /* put some sensible defaults into the data structures ... */
2527 dev
->ctl_bright
= ctrl_by_id(V4L2_CID_BRIGHTNESS
)->default_value
;
2528 dev
->ctl_contrast
= ctrl_by_id(V4L2_CID_CONTRAST
)->default_value
;
2529 dev
->ctl_hue
= ctrl_by_id(V4L2_CID_HUE
)->default_value
;
2530 dev
->ctl_saturation
= ctrl_by_id(V4L2_CID_SATURATION
)->default_value
;
2531 dev
->ctl_volume
= ctrl_by_id(V4L2_CID_AUDIO_VOLUME
)->default_value
;
2532 dev
->ctl_mute
= 1; // ctrl_by_id(V4L2_CID_AUDIO_MUTE)->default_value;
2533 dev
->ctl_invert
= ctrl_by_id(V4L2_CID_PRIVATE_INVERT
)->default_value
;
2534 dev
->ctl_automute
= ctrl_by_id(V4L2_CID_PRIVATE_AUTOMUTE
)->default_value
;
2536 if (dev
->tda9887_conf
&& dev
->ctl_automute
)
2537 dev
->tda9887_conf
|= TDA9887_AUTOMUTE
;
2540 INIT_LIST_HEAD(&dev
->video_q
.queue
);
2541 init_timer(&dev
->video_q
.timeout
);
2542 dev
->video_q
.timeout
.function
= saa7134_buffer_timeout
;
2543 dev
->video_q
.timeout
.data
= (unsigned long)(&dev
->video_q
);
2544 dev
->video_q
.dev
= dev
;
2546 if (saa7134_boards
[dev
->board
].video_out
)
2547 saa7134_videoport_init(dev
);
2552 int saa7134_videoport_init(struct saa7134_dev
*dev
)
2554 /* enable video output */
2555 int vo
= saa7134_boards
[dev
->board
].video_out
;
2557 unsigned int vid_port_opts
= saa7134_boards
[dev
->board
].vid_port_opts
;
2559 /* Configure videoport */
2560 saa_writeb(SAA7134_VIDEO_PORT_CTRL0
, video_out
[vo
][0]);
2561 video_reg
= video_out
[vo
][1];
2562 if (vid_port_opts
& SET_T_CODE_POLARITY_NON_INVERTED
)
2563 video_reg
&= ~VP_T_CODE_P_INVERTED
;
2564 saa_writeb(SAA7134_VIDEO_PORT_CTRL1
, video_reg
);
2565 saa_writeb(SAA7134_VIDEO_PORT_CTRL2
, video_out
[vo
][2]);
2566 saa_writeb(SAA7134_VIDEO_PORT_CTRL4
, video_out
[vo
][4]);
2567 video_reg
= video_out
[vo
][5];
2568 if (vid_port_opts
& SET_CLOCK_NOT_DELAYED
)
2569 video_reg
&= ~VP_CLK_CTRL2_DELAYED
;
2570 if (vid_port_opts
& SET_CLOCK_INVERTED
)
2571 video_reg
|= VP_CLK_CTRL1_INVERTED
;
2572 saa_writeb(SAA7134_VIDEO_PORT_CTRL5
, video_reg
);
2573 video_reg
= video_out
[vo
][6];
2574 if (vid_port_opts
& SET_VSYNC_OFF
) {
2575 video_reg
&= ~VP_VS_TYPE_MASK
;
2576 video_reg
|= VP_VS_TYPE_OFF
;
2578 saa_writeb(SAA7134_VIDEO_PORT_CTRL6
, video_reg
);
2579 saa_writeb(SAA7134_VIDEO_PORT_CTRL7
, video_out
[vo
][7]);
2580 saa_writeb(SAA7134_VIDEO_PORT_CTRL8
, video_out
[vo
][8]);
2582 /* Start videoport */
2583 saa_writeb(SAA7134_VIDEO_PORT_CTRL3
, video_out
[vo
][3]);
2588 int saa7134_video_init2(struct saa7134_dev
*dev
)
2591 set_tvnorm(dev
,&tvnorms
[0]);
2593 saa7134_tvaudio_setmute(dev
);
2594 saa7134_tvaudio_setvolume(dev
,dev
->ctl_volume
);
2598 void saa7134_irq_video_signalchange(struct saa7134_dev
*dev
)
2600 static const char *st
[] = {
2601 "(no signal)", "NTSC", "PAL", "SECAM" };
2604 st1
= saa_readb(SAA7134_STATUS_VIDEO1
);
2605 st2
= saa_readb(SAA7134_STATUS_VIDEO2
);
2606 dprintk("DCSDT: pll: %s, sync: %s, norm: %s\n",
2607 (st1
& 0x40) ? "not locked" : "locked",
2608 (st2
& 0x40) ? "no" : "yes",
2610 dev
->nosignal
= (st1
& 0x40) || (st2
& 0x40) || !(st2
& 0x1);
2612 if (dev
->nosignal
) {
2613 /* no video signal -> mute audio */
2614 if (dev
->ctl_automute
)
2616 saa7134_tvaudio_setmute(dev
);
2618 /* wake up tvaudio audio carrier scan thread */
2619 saa7134_tvaudio_do_scan(dev
);
2622 if ((st2
& 0x80) && !noninterlaced
&& !dev
->nosignal
)
2623 saa_clearb(SAA7134_SYNC_CTRL
, 0x20);
2625 saa_setb(SAA7134_SYNC_CTRL
, 0x20);
2627 if (dev
->mops
&& dev
->mops
->signal_change
)
2628 dev
->mops
->signal_change(dev
);
2632 void saa7134_irq_video_done(struct saa7134_dev
*dev
, unsigned long status
)
2634 enum v4l2_field field
;
2636 spin_lock(&dev
->slock
);
2637 if (dev
->video_q
.curr
) {
2638 dev
->video_fieldcount
++;
2639 field
= dev
->video_q
.curr
->vb
.field
;
2640 if (V4L2_FIELD_HAS_BOTH(field
)) {
2641 /* make sure we have seen both fields */
2642 if ((status
& 0x10) == 0x00) {
2643 dev
->video_q
.curr
->top_seen
= 1;
2646 if (!dev
->video_q
.curr
->top_seen
)
2648 } else if (field
== V4L2_FIELD_TOP
) {
2649 if ((status
& 0x10) != 0x10)
2651 } else if (field
== V4L2_FIELD_BOTTOM
) {
2652 if ((status
& 0x10) != 0x00)
2655 dev
->video_q
.curr
->vb
.field_count
= dev
->video_fieldcount
;
2656 saa7134_buffer_finish(dev
,&dev
->video_q
,VIDEOBUF_DONE
);
2658 saa7134_buffer_next(dev
,&dev
->video_q
);
2661 spin_unlock(&dev
->slock
);
2664 /* ----------------------------------------------------------- */