3 * device driver for philips saa7134 based TV cards
4 * video4linux video interface
6 * (c) 2001-03 Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #include <linux/init.h>
24 #include <linux/list.h>
25 #include <linux/module.h>
26 #include <linux/kernel.h>
27 #include <linux/slab.h>
28 #include <linux/sort.h>
30 #include "saa7134-reg.h"
32 #include <media/v4l2-common.h>
34 #ifdef CONFIG_VIDEO_V4L1_COMPAT
35 /* Include V4L1 specific functions. Should be removed soon */
36 #include <linux/videodev.h>
39 /* ------------------------------------------------------------------ */
41 unsigned int video_debug
;
42 static unsigned int gbuffers
= 8;
43 static unsigned int noninterlaced
= 0;
44 static unsigned int gbufsize
= 720*576*4;
45 static unsigned int gbufsize_max
= 720*576*4;
46 static char secam
[] = "--";
47 module_param(video_debug
, int, 0644);
48 MODULE_PARM_DESC(video_debug
,"enable debug messages [video]");
49 module_param(gbuffers
, int, 0444);
50 MODULE_PARM_DESC(gbuffers
,"number of capture buffers, range 2-32");
51 module_param(noninterlaced
, int, 0644);
52 MODULE_PARM_DESC(noninterlaced
,"capture non interlaced video");
53 module_param_string(secam
, secam
, sizeof(secam
), 0644);
54 MODULE_PARM_DESC(secam
, "force SECAM variant, either DK,L or Lc");
57 #define dprintk(fmt, arg...) if (video_debug&0x04) \
58 printk(KERN_DEBUG "%s/video: " fmt, dev->name , ## arg)
60 /* ------------------------------------------------------------------ */
61 /* Defines for Video Output Port Register at address 0x191 */
63 /* Bit 0: VIP code T bit polarity */
65 #define VP_T_CODE_P_NON_INVERTED 0x00
66 #define VP_T_CODE_P_INVERTED 0x01
68 /* ------------------------------------------------------------------ */
69 /* Defines for Video Output Port Register at address 0x195 */
71 /* Bit 2: Video output clock delay control */
73 #define VP_CLK_CTRL2_NOT_DELAYED 0x00
74 #define VP_CLK_CTRL2_DELAYED 0x04
76 /* Bit 1: Video output clock invert control */
78 #define VP_CLK_CTRL1_NON_INVERTED 0x00
79 #define VP_CLK_CTRL1_INVERTED 0x02
81 /* ------------------------------------------------------------------ */
82 /* Defines for Video Output Port Register at address 0x196 */
84 /* Bits 2 to 0: VSYNC pin video vertical sync type */
86 #define VP_VS_TYPE_MASK 0x07
88 #define VP_VS_TYPE_OFF 0x00
89 #define VP_VS_TYPE_V123 0x01
90 #define VP_VS_TYPE_V_ITU 0x02
91 #define VP_VS_TYPE_VGATE_L 0x03
92 #define VP_VS_TYPE_RESERVED1 0x04
93 #define VP_VS_TYPE_RESERVED2 0x05
94 #define VP_VS_TYPE_F_ITU 0x06
95 #define VP_VS_TYPE_SC_FID 0x07
97 /* ------------------------------------------------------------------ */
98 /* data structs for video */
100 static int video_out
[][9] = {
101 [CCIR656
] = { 0x00, 0xb1, 0x00, 0xa1, 0x00, 0x04, 0x06, 0x00, 0x00 },
104 static struct saa7134_format formats
[] = {
106 .name
= "8 bpp gray",
107 .fourcc
= V4L2_PIX_FMT_GREY
,
111 .name
= "15 bpp RGB, le",
112 .fourcc
= V4L2_PIX_FMT_RGB555
,
116 .name
= "15 bpp RGB, be",
117 .fourcc
= V4L2_PIX_FMT_RGB555X
,
122 .name
= "16 bpp RGB, le",
123 .fourcc
= V4L2_PIX_FMT_RGB565
,
127 .name
= "16 bpp RGB, be",
128 .fourcc
= V4L2_PIX_FMT_RGB565X
,
133 .name
= "24 bpp RGB, le",
134 .fourcc
= V4L2_PIX_FMT_BGR24
,
138 .name
= "24 bpp RGB, be",
139 .fourcc
= V4L2_PIX_FMT_RGB24
,
144 .name
= "32 bpp RGB, le",
145 .fourcc
= V4L2_PIX_FMT_BGR32
,
149 .name
= "32 bpp RGB, be",
150 .fourcc
= V4L2_PIX_FMT_RGB32
,
156 .name
= "4:2:2 packed, YUYV",
157 .fourcc
= V4L2_PIX_FMT_YUYV
,
163 .name
= "4:2:2 packed, UYVY",
164 .fourcc
= V4L2_PIX_FMT_UYVY
,
169 .name
= "4:2:2 planar, Y-Cb-Cr",
170 .fourcc
= V4L2_PIX_FMT_YUV422P
,
178 .name
= "4:2:0 planar, Y-Cb-Cr",
179 .fourcc
= V4L2_PIX_FMT_YUV420
,
187 .name
= "4:2:0 planar, Y-Cb-Cr",
188 .fourcc
= V4L2_PIX_FMT_YVU420
,
198 #define FORMATS ARRAY_SIZE(formats)
200 #define NORM_625_50 \
203 .video_v_start = 24, \
204 .video_v_stop = 311, \
205 .vbi_v_start_0 = 7, \
206 .vbi_v_stop_0 = 22, \
207 .vbi_v_start_1 = 319, \
210 #define NORM_525_60 \
213 .video_v_start = 23, \
214 .video_v_stop = 262, \
215 .vbi_v_start_0 = 10, \
216 .vbi_v_stop_0 = 21, \
217 .vbi_v_start_1 = 273, \
220 #define SAA7134_NORMS \
221 V4L2_STD_PAL | V4L2_STD_PAL_N | \
222 V4L2_STD_PAL_Nc | V4L2_STD_SECAM | \
223 V4L2_STD_NTSC | V4L2_STD_PAL_M | \
226 static struct saa7134_tvnorm tvnorms
[] = {
228 .name
= "PAL", /* autodetect */
232 .sync_control
= 0x18,
233 .luma_control
= 0x40,
234 .chroma_ctrl1
= 0x81,
236 .chroma_ctrl2
= 0x06,
241 .id
= V4L2_STD_PAL_BG
,
244 .sync_control
= 0x18,
245 .luma_control
= 0x40,
246 .chroma_ctrl1
= 0x81,
248 .chroma_ctrl2
= 0x06,
253 .id
= V4L2_STD_PAL_I
,
256 .sync_control
= 0x18,
257 .luma_control
= 0x40,
258 .chroma_ctrl1
= 0x81,
260 .chroma_ctrl2
= 0x06,
265 .id
= V4L2_STD_PAL_DK
,
268 .sync_control
= 0x18,
269 .luma_control
= 0x40,
270 .chroma_ctrl1
= 0x81,
272 .chroma_ctrl2
= 0x06,
280 .sync_control
= 0x59,
281 .luma_control
= 0x40,
282 .chroma_ctrl1
= 0x89,
284 .chroma_ctrl2
= 0x0e,
289 .id
= V4L2_STD_SECAM
,
292 .sync_control
= 0x18,
293 .luma_control
= 0x1b,
294 .chroma_ctrl1
= 0xd1,
296 .chroma_ctrl2
= 0x00,
301 .id
= V4L2_STD_SECAM_DK
,
304 .sync_control
= 0x18,
305 .luma_control
= 0x1b,
306 .chroma_ctrl1
= 0xd1,
308 .chroma_ctrl2
= 0x00,
313 .id
= V4L2_STD_SECAM_L
,
316 .sync_control
= 0x18,
317 .luma_control
= 0x1b,
318 .chroma_ctrl1
= 0xd1,
320 .chroma_ctrl2
= 0x00,
325 .id
= V4L2_STD_SECAM_LC
,
328 .sync_control
= 0x18,
329 .luma_control
= 0x1b,
330 .chroma_ctrl1
= 0xd1,
332 .chroma_ctrl2
= 0x00,
337 .id
= V4L2_STD_PAL_M
,
340 .sync_control
= 0x59,
341 .luma_control
= 0x40,
342 .chroma_ctrl1
= 0xb9,
344 .chroma_ctrl2
= 0x0e,
349 .id
= V4L2_STD_PAL_Nc
,
352 .sync_control
= 0x18,
353 .luma_control
= 0x40,
354 .chroma_ctrl1
= 0xa1,
356 .chroma_ctrl2
= 0x06,
361 .id
= V4L2_STD_PAL_60
,
369 .vbi_v_start_1
= 273,
372 .sync_control
= 0x18,
373 .luma_control
= 0x40,
374 .chroma_ctrl1
= 0x81,
376 .chroma_ctrl2
= 0x06,
380 #define TVNORMS ARRAY_SIZE(tvnorms)
382 #define V4L2_CID_PRIVATE_INVERT (V4L2_CID_PRIVATE_BASE + 0)
383 #define V4L2_CID_PRIVATE_Y_ODD (V4L2_CID_PRIVATE_BASE + 1)
384 #define V4L2_CID_PRIVATE_Y_EVEN (V4L2_CID_PRIVATE_BASE + 2)
385 #define V4L2_CID_PRIVATE_AUTOMUTE (V4L2_CID_PRIVATE_BASE + 3)
386 #define V4L2_CID_PRIVATE_LASTP1 (V4L2_CID_PRIVATE_BASE + 4)
388 static const struct v4l2_queryctrl no_ctrl
= {
390 .flags
= V4L2_CTRL_FLAG_DISABLED
,
392 static const struct v4l2_queryctrl video_ctrls
[] = {
395 .id
= V4L2_CID_BRIGHTNESS
,
396 .name
= "Brightness",
400 .default_value
= 128,
401 .type
= V4L2_CTRL_TYPE_INTEGER
,
403 .id
= V4L2_CID_CONTRAST
,
409 .type
= V4L2_CTRL_TYPE_INTEGER
,
411 .id
= V4L2_CID_SATURATION
,
412 .name
= "Saturation",
417 .type
= V4L2_CTRL_TYPE_INTEGER
,
425 .type
= V4L2_CTRL_TYPE_INTEGER
,
427 .id
= V4L2_CID_HFLIP
,
431 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
435 .id
= V4L2_CID_AUDIO_MUTE
,
439 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
441 .id
= V4L2_CID_AUDIO_VOLUME
,
447 .type
= V4L2_CTRL_TYPE_INTEGER
,
449 /* --- private --- */
451 .id
= V4L2_CID_PRIVATE_INVERT
,
455 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
457 .id
= V4L2_CID_PRIVATE_Y_ODD
,
458 .name
= "y offset odd field",
462 .type
= V4L2_CTRL_TYPE_INTEGER
,
464 .id
= V4L2_CID_PRIVATE_Y_EVEN
,
465 .name
= "y offset even field",
469 .type
= V4L2_CTRL_TYPE_INTEGER
,
471 .id
= V4L2_CID_PRIVATE_AUTOMUTE
,
476 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
479 static const unsigned int CTRLS
= ARRAY_SIZE(video_ctrls
);
481 static const struct v4l2_queryctrl
* ctrl_by_id(unsigned int id
)
485 for (i
= 0; i
< CTRLS
; i
++)
486 if (video_ctrls
[i
].id
== id
)
487 return video_ctrls
+i
;
491 static struct saa7134_format
* format_by_fourcc(unsigned int fourcc
)
495 for (i
= 0; i
< FORMATS
; i
++)
496 if (formats
[i
].fourcc
== fourcc
)
501 /* ----------------------------------------------------------------------- */
502 /* resource management */
504 static int res_get(struct saa7134_dev
*dev
, struct saa7134_fh
*fh
, unsigned int bit
)
506 if (fh
->resources
& bit
)
507 /* have it already allocated */
511 mutex_lock(&dev
->lock
);
512 if (dev
->resources
& bit
) {
513 /* no, someone else uses it */
514 mutex_unlock(&dev
->lock
);
517 /* it's free, grab it */
518 fh
->resources
|= bit
;
519 dev
->resources
|= bit
;
520 dprintk("res: get %d\n",bit
);
521 mutex_unlock(&dev
->lock
);
525 static int res_check(struct saa7134_fh
*fh
, unsigned int bit
)
527 return (fh
->resources
& bit
);
530 static int res_locked(struct saa7134_dev
*dev
, unsigned int bit
)
532 return (dev
->resources
& bit
);
536 void res_free(struct saa7134_dev
*dev
, struct saa7134_fh
*fh
, unsigned int bits
)
538 BUG_ON((fh
->resources
& bits
) != bits
);
540 mutex_lock(&dev
->lock
);
541 fh
->resources
&= ~bits
;
542 dev
->resources
&= ~bits
;
543 dprintk("res: put %d\n",bits
);
544 mutex_unlock(&dev
->lock
);
547 /* ------------------------------------------------------------------ */
549 static void set_tvnorm(struct saa7134_dev
*dev
, struct saa7134_tvnorm
*norm
)
551 dprintk("set tv norm = %s\n",norm
->name
);
555 dev
->crop_bounds
.left
= norm
->h_start
;
556 dev
->crop_defrect
.left
= norm
->h_start
;
557 dev
->crop_bounds
.width
= norm
->h_stop
- norm
->h_start
+1;
558 dev
->crop_defrect
.width
= norm
->h_stop
- norm
->h_start
+1;
560 dev
->crop_bounds
.top
= (norm
->vbi_v_stop_0
+1)*2;
561 dev
->crop_defrect
.top
= norm
->video_v_start
*2;
562 dev
->crop_bounds
.height
= ((norm
->id
& V4L2_STD_525_60
) ? 524 : 624)
563 - dev
->crop_bounds
.top
;
564 dev
->crop_defrect
.height
= (norm
->video_v_stop
- norm
->video_v_start
+1)*2;
566 dev
->crop_current
= dev
->crop_defrect
;
568 saa7134_set_tvnorm_hw(dev
);
571 static void video_mux(struct saa7134_dev
*dev
, int input
)
573 dprintk("video input = %d [%s]\n", input
, card_in(dev
, input
).name
);
574 dev
->ctl_input
= input
;
575 set_tvnorm(dev
, dev
->tvnorm
);
576 saa7134_tvaudio_setinput(dev
, &card_in(dev
, input
));
580 static void saa7134_set_decoder(struct saa7134_dev
*dev
)
582 int luma_control
, sync_control
, mux
;
584 struct saa7134_tvnorm
*norm
= dev
->tvnorm
;
585 mux
= card_in(dev
, dev
->ctl_input
).vmux
;
587 luma_control
= norm
->luma_control
;
588 sync_control
= norm
->sync_control
;
591 luma_control
|= 0x80; /* svideo */
592 if (noninterlaced
|| dev
->nosignal
)
593 sync_control
|= 0x20;
595 /* setup video decoder */
596 saa_writeb(SAA7134_INCR_DELAY
, 0x08);
597 saa_writeb(SAA7134_ANALOG_IN_CTRL1
, 0xc0 | mux
);
598 saa_writeb(SAA7134_ANALOG_IN_CTRL2
, 0x00);
600 saa_writeb(SAA7134_ANALOG_IN_CTRL3
, 0x90);
601 saa_writeb(SAA7134_ANALOG_IN_CTRL4
, 0x90);
602 saa_writeb(SAA7134_HSYNC_START
, 0xeb);
603 saa_writeb(SAA7134_HSYNC_STOP
, 0xe0);
604 saa_writeb(SAA7134_SOURCE_TIMING1
, norm
->src_timing
);
606 saa_writeb(SAA7134_SYNC_CTRL
, sync_control
);
607 saa_writeb(SAA7134_LUMA_CTRL
, luma_control
);
608 saa_writeb(SAA7134_DEC_LUMA_BRIGHT
, dev
->ctl_bright
);
610 saa_writeb(SAA7134_DEC_LUMA_CONTRAST
,
611 dev
->ctl_invert
? -dev
->ctl_contrast
: dev
->ctl_contrast
);
613 saa_writeb(SAA7134_DEC_CHROMA_SATURATION
,
614 dev
->ctl_invert
? -dev
->ctl_saturation
: dev
->ctl_saturation
);
616 saa_writeb(SAA7134_DEC_CHROMA_HUE
, dev
->ctl_hue
);
617 saa_writeb(SAA7134_CHROMA_CTRL1
, norm
->chroma_ctrl1
);
618 saa_writeb(SAA7134_CHROMA_GAIN
, norm
->chroma_gain
);
620 saa_writeb(SAA7134_CHROMA_CTRL2
, norm
->chroma_ctrl2
);
621 saa_writeb(SAA7134_MODE_DELAY_CTRL
, 0x00);
623 saa_writeb(SAA7134_ANALOG_ADC
, 0x01);
624 saa_writeb(SAA7134_VGATE_START
, 0x11);
625 saa_writeb(SAA7134_VGATE_STOP
, 0xfe);
626 saa_writeb(SAA7134_MISC_VGATE_MSB
, norm
->vgate_misc
);
627 saa_writeb(SAA7134_RAW_DATA_GAIN
, 0x40);
628 saa_writeb(SAA7134_RAW_DATA_OFFSET
, 0x80);
631 void saa7134_set_tvnorm_hw(struct saa7134_dev
*dev
)
633 saa7134_set_decoder(dev
);
635 if (card_in(dev
, dev
->ctl_input
).tv
) {
636 if ((card(dev
).tuner_type
== TUNER_PHILIPS_TDA8290
)
637 && ((card(dev
).tuner_config
== 1)
638 || (card(dev
).tuner_config
== 2)))
639 saa7134_set_gpio(dev
, 22, 5);
640 saa7134_i2c_call_clients(dev
, VIDIOC_S_STD
, &dev
->tvnorm
->id
);
644 static void set_h_prescale(struct saa7134_dev
*dev
, int task
, int prescale
)
646 static const struct {
653 /* XPSC XACL XC2_1 XDCG VPFY */
665 static const int count
= ARRAY_SIZE(vals
);
668 for (i
= 0; i
< count
; i
++)
669 if (vals
[i
].xpsc
== prescale
)
674 saa_writeb(SAA7134_H_PRESCALE(task
), vals
[i
].xpsc
);
675 saa_writeb(SAA7134_ACC_LENGTH(task
), vals
[i
].xacl
);
676 saa_writeb(SAA7134_LEVEL_CTRL(task
),
677 (vals
[i
].xc2_1
<< 3) | (vals
[i
].xdcg
));
678 saa_andorb(SAA7134_FIR_PREFILTER_CTRL(task
), 0x0f,
679 (vals
[i
].vpfy
<< 2) | vals
[i
].vpfy
);
682 static void set_v_scale(struct saa7134_dev
*dev
, int task
, int yscale
)
686 saa_writeb(SAA7134_V_SCALE_RATIO1(task
), yscale
& 0xff);
687 saa_writeb(SAA7134_V_SCALE_RATIO2(task
), yscale
>> 8);
689 mirror
= (dev
->ctl_mirror
) ? 0x02 : 0x00;
692 dprintk("yscale LPI yscale=%d\n",yscale
);
693 saa_writeb(SAA7134_V_FILTER(task
), 0x00 | mirror
);
694 saa_writeb(SAA7134_LUMA_CONTRAST(task
), 0x40);
695 saa_writeb(SAA7134_CHROMA_SATURATION(task
), 0x40);
698 val
= 0x40 * 1024 / yscale
;
699 dprintk("yscale ACM yscale=%d val=0x%x\n",yscale
,val
);
700 saa_writeb(SAA7134_V_FILTER(task
), 0x01 | mirror
);
701 saa_writeb(SAA7134_LUMA_CONTRAST(task
), val
);
702 saa_writeb(SAA7134_CHROMA_SATURATION(task
), val
);
704 saa_writeb(SAA7134_LUMA_BRIGHT(task
), 0x80);
707 static void set_size(struct saa7134_dev
*dev
, int task
,
708 int width
, int height
, int interlace
)
710 int prescale
,xscale
,yscale
,y_even
,y_odd
;
711 int h_start
, h_stop
, v_start
, v_stop
;
712 int div
= interlace
? 2 : 1;
714 /* setup video scaler */
715 h_start
= dev
->crop_current
.left
;
716 v_start
= dev
->crop_current
.top
/2;
717 h_stop
= (dev
->crop_current
.left
+ dev
->crop_current
.width
-1);
718 v_stop
= (dev
->crop_current
.top
+ dev
->crop_current
.height
-1)/2;
720 saa_writeb(SAA7134_VIDEO_H_START1(task
), h_start
& 0xff);
721 saa_writeb(SAA7134_VIDEO_H_START2(task
), h_start
>> 8);
722 saa_writeb(SAA7134_VIDEO_H_STOP1(task
), h_stop
& 0xff);
723 saa_writeb(SAA7134_VIDEO_H_STOP2(task
), h_stop
>> 8);
724 saa_writeb(SAA7134_VIDEO_V_START1(task
), v_start
& 0xff);
725 saa_writeb(SAA7134_VIDEO_V_START2(task
), v_start
>> 8);
726 saa_writeb(SAA7134_VIDEO_V_STOP1(task
), v_stop
& 0xff);
727 saa_writeb(SAA7134_VIDEO_V_STOP2(task
), v_stop
>> 8);
729 prescale
= dev
->crop_current
.width
/ width
;
732 xscale
= 1024 * dev
->crop_current
.width
/ prescale
/ width
;
733 yscale
= 512 * div
* dev
->crop_current
.height
/ height
;
734 dprintk("prescale=%d xscale=%d yscale=%d\n",prescale
,xscale
,yscale
);
735 set_h_prescale(dev
,task
,prescale
);
736 saa_writeb(SAA7134_H_SCALE_INC1(task
), xscale
& 0xff);
737 saa_writeb(SAA7134_H_SCALE_INC2(task
), xscale
>> 8);
738 set_v_scale(dev
,task
,yscale
);
740 saa_writeb(SAA7134_VIDEO_PIXELS1(task
), width
& 0xff);
741 saa_writeb(SAA7134_VIDEO_PIXELS2(task
), width
>> 8);
742 saa_writeb(SAA7134_VIDEO_LINES1(task
), height
/div
& 0xff);
743 saa_writeb(SAA7134_VIDEO_LINES2(task
), height
/div
>> 8);
745 /* deinterlace y offsets */
746 y_odd
= dev
->ctl_y_odd
;
747 y_even
= dev
->ctl_y_even
;
748 saa_writeb(SAA7134_V_PHASE_OFFSET0(task
), y_odd
);
749 saa_writeb(SAA7134_V_PHASE_OFFSET1(task
), y_even
);
750 saa_writeb(SAA7134_V_PHASE_OFFSET2(task
), y_odd
);
751 saa_writeb(SAA7134_V_PHASE_OFFSET3(task
), y_even
);
754 /* ------------------------------------------------------------------ */
762 static void set_cliplist(struct saa7134_dev
*dev
, int reg
,
763 struct cliplist
*cl
, int entries
, char *name
)
768 for (i
= 0; i
< entries
; i
++) {
769 winbits
|= cl
[i
].enable
;
770 winbits
&= ~cl
[i
].disable
;
771 if (i
< 15 && cl
[i
].position
== cl
[i
+1].position
)
773 saa_writeb(reg
+ 0, winbits
);
774 saa_writeb(reg
+ 2, cl
[i
].position
& 0xff);
775 saa_writeb(reg
+ 3, cl
[i
].position
>> 8);
776 dprintk("clip: %s winbits=%02x pos=%d\n",
777 name
,winbits
,cl
[i
].position
);
780 for (; reg
< 0x400; reg
+= 8) {
781 saa_writeb(reg
+ 0, 0);
782 saa_writeb(reg
+ 1, 0);
783 saa_writeb(reg
+ 2, 0);
784 saa_writeb(reg
+ 3, 0);
788 static int clip_range(int val
)
795 /* Sort into smallest position first order */
796 static int cliplist_cmp(const void *a
, const void *b
)
798 const struct cliplist
*cla
= a
;
799 const struct cliplist
*clb
= b
;
800 if (cla
->position
< clb
->position
)
802 if (cla
->position
> clb
->position
)
807 static int setup_clipping(struct saa7134_dev
*dev
, struct v4l2_clip
*clips
,
808 int nclips
, int interlace
)
810 struct cliplist col
[16], row
[16];
811 int cols
= 0, rows
= 0, i
;
812 int div
= interlace
? 2 : 1;
814 memset(col
, 0, sizeof(col
));
815 memset(row
, 0, sizeof(row
));
816 for (i
= 0; i
< nclips
&& i
< 8; i
++) {
817 col
[cols
].position
= clip_range(clips
[i
].c
.left
);
818 col
[cols
].enable
= (1 << i
);
820 col
[cols
].position
= clip_range(clips
[i
].c
.left
+clips
[i
].c
.width
);
821 col
[cols
].disable
= (1 << i
);
823 row
[rows
].position
= clip_range(clips
[i
].c
.top
/ div
);
824 row
[rows
].enable
= (1 << i
);
826 row
[rows
].position
= clip_range((clips
[i
].c
.top
+ clips
[i
].c
.height
)
828 row
[rows
].disable
= (1 << i
);
831 sort(col
, cols
, sizeof col
[0], cliplist_cmp
, NULL
);
832 sort(row
, rows
, sizeof row
[0], cliplist_cmp
, NULL
);
833 set_cliplist(dev
,0x380,col
,cols
,"cols");
834 set_cliplist(dev
,0x384,row
,rows
,"rows");
838 static int verify_preview(struct saa7134_dev
*dev
, struct v4l2_window
*win
)
840 enum v4l2_field field
;
843 if (NULL
== dev
->ovbuf
.base
)
845 if (NULL
== dev
->ovfmt
)
847 if (win
->w
.width
< 48 || win
->w
.height
< 32)
849 if (win
->clipcount
> 2048)
853 maxw
= dev
->crop_current
.width
;
854 maxh
= dev
->crop_current
.height
;
856 if (V4L2_FIELD_ANY
== field
) {
857 field
= (win
->w
.height
> maxh
/2)
858 ? V4L2_FIELD_INTERLACED
863 case V4L2_FIELD_BOTTOM
:
866 case V4L2_FIELD_INTERLACED
:
873 if (win
->w
.width
> maxw
)
875 if (win
->w
.height
> maxh
)
876 win
->w
.height
= maxh
;
880 static int start_preview(struct saa7134_dev
*dev
, struct saa7134_fh
*fh
)
882 unsigned long base
,control
,bpl
;
885 err
= verify_preview(dev
,&fh
->win
);
889 dev
->ovfield
= fh
->win
.field
;
890 dprintk("start_preview %dx%d+%d+%d %s field=%s\n",
891 fh
->win
.w
.width
,fh
->win
.w
.height
,
892 fh
->win
.w
.left
,fh
->win
.w
.top
,
893 dev
->ovfmt
->name
,v4l2_field_names
[dev
->ovfield
]);
895 /* setup window + clipping */
896 set_size(dev
,TASK_B
,fh
->win
.w
.width
,fh
->win
.w
.height
,
897 V4L2_FIELD_HAS_BOTH(dev
->ovfield
));
898 setup_clipping(dev
,fh
->clips
,fh
->nclips
,
899 V4L2_FIELD_HAS_BOTH(dev
->ovfield
));
901 saa_andorb(SAA7134_DATA_PATH(TASK_B
), 0x3f, 0x03);
903 saa_andorb(SAA7134_DATA_PATH(TASK_B
), 0x3f, 0x01);
904 saa_writeb(SAA7134_OFMT_VIDEO_B
, dev
->ovfmt
->pm
| 0x20);
906 /* dma: setup channel 1 (= Video Task B) */
907 base
= (unsigned long)dev
->ovbuf
.base
;
908 base
+= dev
->ovbuf
.fmt
.bytesperline
* fh
->win
.w
.top
;
909 base
+= dev
->ovfmt
->depth
/8 * fh
->win
.w
.left
;
910 bpl
= dev
->ovbuf
.fmt
.bytesperline
;
911 control
= SAA7134_RS_CONTROL_BURST_16
;
912 if (dev
->ovfmt
->bswap
)
913 control
|= SAA7134_RS_CONTROL_BSWAP
;
914 if (dev
->ovfmt
->wswap
)
915 control
|= SAA7134_RS_CONTROL_WSWAP
;
916 if (V4L2_FIELD_HAS_BOTH(dev
->ovfield
)) {
917 saa_writel(SAA7134_RS_BA1(1),base
);
918 saa_writel(SAA7134_RS_BA2(1),base
+bpl
);
919 saa_writel(SAA7134_RS_PITCH(1),bpl
*2);
920 saa_writel(SAA7134_RS_CONTROL(1),control
);
922 saa_writel(SAA7134_RS_BA1(1),base
);
923 saa_writel(SAA7134_RS_BA2(1),base
);
924 saa_writel(SAA7134_RS_PITCH(1),bpl
);
925 saa_writel(SAA7134_RS_CONTROL(1),control
);
930 saa7134_set_dmabits(dev
);
935 static int stop_preview(struct saa7134_dev
*dev
, struct saa7134_fh
*fh
)
938 saa7134_set_dmabits(dev
);
942 /* ------------------------------------------------------------------ */
944 static int buffer_activate(struct saa7134_dev
*dev
,
945 struct saa7134_buf
*buf
,
946 struct saa7134_buf
*next
)
948 unsigned long base
,control
,bpl
;
949 unsigned long bpl_uv
,lines_uv
,base2
,base3
,tmp
; /* planar */
951 dprintk("buffer_activate buf=%p\n",buf
);
952 buf
->vb
.state
= VIDEOBUF_ACTIVE
;
955 set_size(dev
,TASK_A
,buf
->vb
.width
,buf
->vb
.height
,
956 V4L2_FIELD_HAS_BOTH(buf
->vb
.field
));
958 saa_andorb(SAA7134_DATA_PATH(TASK_A
), 0x3f, 0x03);
960 saa_andorb(SAA7134_DATA_PATH(TASK_A
), 0x3f, 0x01);
961 saa_writeb(SAA7134_OFMT_VIDEO_A
, buf
->fmt
->pm
);
963 /* DMA: setup channel 0 (= Video Task A0) */
964 base
= saa7134_buffer_base(buf
);
965 if (buf
->fmt
->planar
)
968 bpl
= (buf
->vb
.width
* buf
->fmt
->depth
) / 8;
969 control
= SAA7134_RS_CONTROL_BURST_16
|
970 SAA7134_RS_CONTROL_ME
|
971 (buf
->pt
->dma
>> 12);
973 control
|= SAA7134_RS_CONTROL_BSWAP
;
975 control
|= SAA7134_RS_CONTROL_WSWAP
;
976 if (V4L2_FIELD_HAS_BOTH(buf
->vb
.field
)) {
978 saa_writel(SAA7134_RS_BA1(0),base
);
979 saa_writel(SAA7134_RS_BA2(0),base
+bpl
);
980 saa_writel(SAA7134_RS_PITCH(0),bpl
*2);
983 saa_writel(SAA7134_RS_BA1(0),base
);
984 saa_writel(SAA7134_RS_BA2(0),base
);
985 saa_writel(SAA7134_RS_PITCH(0),bpl
);
987 saa_writel(SAA7134_RS_CONTROL(0),control
);
989 if (buf
->fmt
->planar
) {
990 /* DMA: setup channel 4+5 (= planar task A) */
991 bpl_uv
= bpl
>> buf
->fmt
->hshift
;
992 lines_uv
= buf
->vb
.height
>> buf
->fmt
->vshift
;
993 base2
= base
+ bpl
* buf
->vb
.height
;
994 base3
= base2
+ bpl_uv
* lines_uv
;
995 if (buf
->fmt
->uvswap
)
996 tmp
= base2
, base2
= base3
, base3
= tmp
;
997 dprintk("uv: bpl=%ld lines=%ld base2/3=%ld/%ld\n",
998 bpl_uv
,lines_uv
,base2
,base3
);
999 if (V4L2_FIELD_HAS_BOTH(buf
->vb
.field
)) {
1001 saa_writel(SAA7134_RS_BA1(4),base2
);
1002 saa_writel(SAA7134_RS_BA2(4),base2
+bpl_uv
);
1003 saa_writel(SAA7134_RS_PITCH(4),bpl_uv
*2);
1004 saa_writel(SAA7134_RS_BA1(5),base3
);
1005 saa_writel(SAA7134_RS_BA2(5),base3
+bpl_uv
);
1006 saa_writel(SAA7134_RS_PITCH(5),bpl_uv
*2);
1008 /* non-interlaced */
1009 saa_writel(SAA7134_RS_BA1(4),base2
);
1010 saa_writel(SAA7134_RS_BA2(4),base2
);
1011 saa_writel(SAA7134_RS_PITCH(4),bpl_uv
);
1012 saa_writel(SAA7134_RS_BA1(5),base3
);
1013 saa_writel(SAA7134_RS_BA2(5),base3
);
1014 saa_writel(SAA7134_RS_PITCH(5),bpl_uv
);
1016 saa_writel(SAA7134_RS_CONTROL(4),control
);
1017 saa_writel(SAA7134_RS_CONTROL(5),control
);
1021 saa7134_set_dmabits(dev
);
1022 mod_timer(&dev
->video_q
.timeout
, jiffies
+BUFFER_TIMEOUT
);
1026 static int buffer_prepare(struct videobuf_queue
*q
,
1027 struct videobuf_buffer
*vb
,
1028 enum v4l2_field field
)
1030 struct saa7134_fh
*fh
= q
->priv_data
;
1031 struct saa7134_dev
*dev
= fh
->dev
;
1032 struct saa7134_buf
*buf
= container_of(vb
,struct saa7134_buf
,vb
);
1037 if (NULL
== fh
->fmt
)
1039 if (fh
->width
< 48 ||
1041 fh
->width
/4 > dev
->crop_current
.width
||
1042 fh
->height
/4 > dev
->crop_current
.height
||
1043 fh
->width
> dev
->crop_bounds
.width
||
1044 fh
->height
> dev
->crop_bounds
.height
)
1046 size
= (fh
->width
* fh
->height
* fh
->fmt
->depth
) >> 3;
1047 if (0 != buf
->vb
.baddr
&& buf
->vb
.bsize
< size
)
1050 dprintk("buffer_prepare [%d,size=%dx%d,bytes=%d,fields=%s,%s]\n",
1051 vb
->i
,fh
->width
,fh
->height
,size
,v4l2_field_names
[field
],
1053 if (buf
->vb
.width
!= fh
->width
||
1054 buf
->vb
.height
!= fh
->height
||
1055 buf
->vb
.size
!= size
||
1056 buf
->vb
.field
!= field
||
1057 buf
->fmt
!= fh
->fmt
) {
1058 saa7134_dma_free(q
,buf
);
1061 if (VIDEOBUF_NEEDS_INIT
== buf
->vb
.state
) {
1062 struct videobuf_dmabuf
*dma
=videobuf_to_dma(&buf
->vb
);
1064 buf
->vb
.width
= fh
->width
;
1065 buf
->vb
.height
= fh
->height
;
1066 buf
->vb
.size
= size
;
1067 buf
->vb
.field
= field
;
1069 buf
->pt
= &fh
->pt_cap
;
1071 err
= videobuf_iolock(q
,&buf
->vb
,&dev
->ovbuf
);
1074 err
= saa7134_pgtable_build(dev
->pci
,buf
->pt
,
1077 saa7134_buffer_startpage(buf
));
1081 buf
->vb
.state
= VIDEOBUF_PREPARED
;
1082 buf
->activate
= buffer_activate
;
1086 saa7134_dma_free(q
,buf
);
1091 buffer_setup(struct videobuf_queue
*q
, unsigned int *count
, unsigned int *size
)
1093 struct saa7134_fh
*fh
= q
->priv_data
;
1095 *size
= fh
->fmt
->depth
* fh
->width
* fh
->height
>> 3;
1098 *count
= saa7134_buffer_count(*size
,*count
);
1102 static void buffer_queue(struct videobuf_queue
*q
, struct videobuf_buffer
*vb
)
1104 struct saa7134_fh
*fh
= q
->priv_data
;
1105 struct saa7134_buf
*buf
= container_of(vb
,struct saa7134_buf
,vb
);
1107 saa7134_buffer_queue(fh
->dev
,&fh
->dev
->video_q
,buf
);
1110 static void buffer_release(struct videobuf_queue
*q
, struct videobuf_buffer
*vb
)
1112 struct saa7134_buf
*buf
= container_of(vb
,struct saa7134_buf
,vb
);
1114 saa7134_dma_free(q
,buf
);
1117 static struct videobuf_queue_ops video_qops
= {
1118 .buf_setup
= buffer_setup
,
1119 .buf_prepare
= buffer_prepare
,
1120 .buf_queue
= buffer_queue
,
1121 .buf_release
= buffer_release
,
1124 /* ------------------------------------------------------------------ */
1126 static int vidioc_g_ctrl(struct file
*file
, void *priv
,
1127 struct v4l2_control
*c
)
1129 struct saa7134_fh
*fh
= priv
;
1130 struct saa7134_dev
*dev
= fh
->dev
;
1131 const struct v4l2_queryctrl
* ctrl
;
1133 ctrl
= ctrl_by_id(c
->id
);
1137 case V4L2_CID_BRIGHTNESS
:
1138 c
->value
= dev
->ctl_bright
;
1141 c
->value
= dev
->ctl_hue
;
1143 case V4L2_CID_CONTRAST
:
1144 c
->value
= dev
->ctl_contrast
;
1146 case V4L2_CID_SATURATION
:
1147 c
->value
= dev
->ctl_saturation
;
1149 case V4L2_CID_AUDIO_MUTE
:
1150 c
->value
= dev
->ctl_mute
;
1152 case V4L2_CID_AUDIO_VOLUME
:
1153 c
->value
= dev
->ctl_volume
;
1155 case V4L2_CID_PRIVATE_INVERT
:
1156 c
->value
= dev
->ctl_invert
;
1158 case V4L2_CID_HFLIP
:
1159 c
->value
= dev
->ctl_mirror
;
1161 case V4L2_CID_PRIVATE_Y_EVEN
:
1162 c
->value
= dev
->ctl_y_even
;
1164 case V4L2_CID_PRIVATE_Y_ODD
:
1165 c
->value
= dev
->ctl_y_odd
;
1167 case V4L2_CID_PRIVATE_AUTOMUTE
:
1168 c
->value
= dev
->ctl_automute
;
1176 static int vidioc_s_ctrl(struct file
*file
, void *f
,
1177 struct v4l2_control
*c
)
1179 const struct v4l2_queryctrl
* ctrl
;
1180 struct saa7134_fh
*fh
= f
;
1181 struct saa7134_dev
*dev
= fh
->dev
;
1182 unsigned long flags
;
1183 int restart_overlay
= 0;
1186 err
= v4l2_prio_check(&dev
->prio
, &fh
->prio
);
1190 mutex_lock(&dev
->lock
);
1192 ctrl
= ctrl_by_id(c
->id
);
1196 dprintk("set_control name=%s val=%d\n",ctrl
->name
,c
->value
);
1197 switch (ctrl
->type
) {
1198 case V4L2_CTRL_TYPE_BOOLEAN
:
1199 case V4L2_CTRL_TYPE_MENU
:
1200 case V4L2_CTRL_TYPE_INTEGER
:
1201 if (c
->value
< ctrl
->minimum
)
1202 c
->value
= ctrl
->minimum
;
1203 if (c
->value
> ctrl
->maximum
)
1204 c
->value
= ctrl
->maximum
;
1210 case V4L2_CID_BRIGHTNESS
:
1211 dev
->ctl_bright
= c
->value
;
1212 saa_writeb(SAA7134_DEC_LUMA_BRIGHT
, dev
->ctl_bright
);
1215 dev
->ctl_hue
= c
->value
;
1216 saa_writeb(SAA7134_DEC_CHROMA_HUE
, dev
->ctl_hue
);
1218 case V4L2_CID_CONTRAST
:
1219 dev
->ctl_contrast
= c
->value
;
1220 saa_writeb(SAA7134_DEC_LUMA_CONTRAST
,
1221 dev
->ctl_invert
? -dev
->ctl_contrast
: dev
->ctl_contrast
);
1223 case V4L2_CID_SATURATION
:
1224 dev
->ctl_saturation
= c
->value
;
1225 saa_writeb(SAA7134_DEC_CHROMA_SATURATION
,
1226 dev
->ctl_invert
? -dev
->ctl_saturation
: dev
->ctl_saturation
);
1228 case V4L2_CID_AUDIO_MUTE
:
1229 dev
->ctl_mute
= c
->value
;
1230 saa7134_tvaudio_setmute(dev
);
1232 case V4L2_CID_AUDIO_VOLUME
:
1233 dev
->ctl_volume
= c
->value
;
1234 saa7134_tvaudio_setvolume(dev
,dev
->ctl_volume
);
1236 case V4L2_CID_PRIVATE_INVERT
:
1237 dev
->ctl_invert
= c
->value
;
1238 saa_writeb(SAA7134_DEC_LUMA_CONTRAST
,
1239 dev
->ctl_invert
? -dev
->ctl_contrast
: dev
->ctl_contrast
);
1240 saa_writeb(SAA7134_DEC_CHROMA_SATURATION
,
1241 dev
->ctl_invert
? -dev
->ctl_saturation
: dev
->ctl_saturation
);
1243 case V4L2_CID_HFLIP
:
1244 dev
->ctl_mirror
= c
->value
;
1245 restart_overlay
= 1;
1247 case V4L2_CID_PRIVATE_Y_EVEN
:
1248 dev
->ctl_y_even
= c
->value
;
1249 restart_overlay
= 1;
1251 case V4L2_CID_PRIVATE_Y_ODD
:
1252 dev
->ctl_y_odd
= c
->value
;
1253 restart_overlay
= 1;
1255 case V4L2_CID_PRIVATE_AUTOMUTE
:
1257 struct v4l2_priv_tun_config tda9887_cfg
;
1259 tda9887_cfg
.tuner
= TUNER_TDA9887
;
1260 tda9887_cfg
.priv
= &dev
->tda9887_conf
;
1262 dev
->ctl_automute
= c
->value
;
1263 if (dev
->tda9887_conf
) {
1264 if (dev
->ctl_automute
)
1265 dev
->tda9887_conf
|= TDA9887_AUTOMUTE
;
1267 dev
->tda9887_conf
&= ~TDA9887_AUTOMUTE
;
1269 saa7134_i2c_call_clients(dev
, TUNER_SET_CONFIG
,
1277 if (restart_overlay
&& fh
&& res_check(fh
, RESOURCE_OVERLAY
)) {
1278 spin_lock_irqsave(&dev
->slock
,flags
);
1279 stop_preview(dev
,fh
);
1280 start_preview(dev
,fh
);
1281 spin_unlock_irqrestore(&dev
->slock
,flags
);
1286 mutex_unlock(&dev
->lock
);
1290 /* ------------------------------------------------------------------ */
1292 static struct videobuf_queue
* saa7134_queue(struct saa7134_fh
*fh
)
1294 struct videobuf_queue
* q
= NULL
;
1297 case V4L2_BUF_TYPE_VIDEO_CAPTURE
:
1300 case V4L2_BUF_TYPE_VBI_CAPTURE
:
1309 static int saa7134_resource(struct saa7134_fh
*fh
)
1311 if (fh
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1312 return RESOURCE_VIDEO
;
1314 if (fh
->type
== V4L2_BUF_TYPE_VBI_CAPTURE
)
1315 return RESOURCE_VBI
;
1321 static int video_open(struct inode
*inode
, struct file
*file
)
1323 int minor
= iminor(inode
);
1324 struct saa7134_dev
*dev
;
1325 struct saa7134_fh
*fh
;
1326 enum v4l2_buf_type type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1328 list_for_each_entry(dev
, &saa7134_devlist
, devlist
) {
1329 if (dev
->video_dev
&& (dev
->video_dev
->minor
== minor
))
1331 if (dev
->radio_dev
&& (dev
->radio_dev
->minor
== minor
)) {
1335 if (dev
->vbi_dev
&& (dev
->vbi_dev
->minor
== minor
)) {
1336 type
= V4L2_BUF_TYPE_VBI_CAPTURE
;
1343 dprintk("open minor=%d radio=%d type=%s\n",minor
,radio
,
1344 v4l2_type_names
[type
]);
1346 /* allocate + initialize per filehandle data */
1347 fh
= kzalloc(sizeof(*fh
),GFP_KERNEL
);
1350 file
->private_data
= fh
;
1354 fh
->fmt
= format_by_fourcc(V4L2_PIX_FMT_BGR24
);
1357 v4l2_prio_open(&dev
->prio
,&fh
->prio
);
1359 videobuf_queue_pci_init(&fh
->cap
, &video_qops
,
1360 dev
->pci
, &dev
->slock
,
1361 V4L2_BUF_TYPE_VIDEO_CAPTURE
,
1362 V4L2_FIELD_INTERLACED
,
1363 sizeof(struct saa7134_buf
),
1365 videobuf_queue_pci_init(&fh
->vbi
, &saa7134_vbi_qops
,
1366 dev
->pci
, &dev
->slock
,
1367 V4L2_BUF_TYPE_VBI_CAPTURE
,
1369 sizeof(struct saa7134_buf
),
1371 saa7134_pgtable_alloc(dev
->pci
,&fh
->pt_cap
);
1372 saa7134_pgtable_alloc(dev
->pci
,&fh
->pt_vbi
);
1375 /* switch to radio mode */
1376 saa7134_tvaudio_setinput(dev
,&card(dev
).radio
);
1377 saa7134_i2c_call_clients(dev
,AUDC_SET_RADIO
, NULL
);
1379 /* switch to video/vbi mode */
1380 video_mux(dev
,dev
->ctl_input
);
1386 video_read(struct file
*file
, char __user
*data
, size_t count
, loff_t
*ppos
)
1388 struct saa7134_fh
*fh
= file
->private_data
;
1391 case V4L2_BUF_TYPE_VIDEO_CAPTURE
:
1392 if (res_locked(fh
->dev
,RESOURCE_VIDEO
))
1394 return videobuf_read_one(saa7134_queue(fh
),
1396 file
->f_flags
& O_NONBLOCK
);
1397 case V4L2_BUF_TYPE_VBI_CAPTURE
:
1398 if (!res_get(fh
->dev
,fh
,RESOURCE_VBI
))
1400 return videobuf_read_stream(saa7134_queue(fh
),
1401 data
, count
, ppos
, 1,
1402 file
->f_flags
& O_NONBLOCK
);
1411 video_poll(struct file
*file
, struct poll_table_struct
*wait
)
1413 struct saa7134_fh
*fh
= file
->private_data
;
1414 struct videobuf_buffer
*buf
= NULL
;
1416 if (V4L2_BUF_TYPE_VBI_CAPTURE
== fh
->type
)
1417 return videobuf_poll_stream(file
, &fh
->vbi
, wait
);
1419 if (res_check(fh
,RESOURCE_VIDEO
)) {
1420 if (!list_empty(&fh
->cap
.stream
))
1421 buf
= list_entry(fh
->cap
.stream
.next
, struct videobuf_buffer
, stream
);
1423 mutex_lock(&fh
->cap
.lock
);
1424 if (UNSET
== fh
->cap
.read_off
) {
1425 /* need to capture a new frame */
1426 if (res_locked(fh
->dev
,RESOURCE_VIDEO
)) {
1427 mutex_unlock(&fh
->cap
.lock
);
1430 if (0 != fh
->cap
.ops
->buf_prepare(&fh
->cap
,fh
->cap
.read_buf
,fh
->cap
.field
)) {
1431 mutex_unlock(&fh
->cap
.lock
);
1434 fh
->cap
.ops
->buf_queue(&fh
->cap
,fh
->cap
.read_buf
);
1435 fh
->cap
.read_off
= 0;
1437 mutex_unlock(&fh
->cap
.lock
);
1438 buf
= fh
->cap
.read_buf
;
1444 poll_wait(file
, &buf
->done
, wait
);
1445 if (buf
->state
== VIDEOBUF_DONE
||
1446 buf
->state
== VIDEOBUF_ERROR
)
1447 return POLLIN
|POLLRDNORM
;
1451 static int video_release(struct inode
*inode
, struct file
*file
)
1453 struct saa7134_fh
*fh
= file
->private_data
;
1454 struct saa7134_dev
*dev
= fh
->dev
;
1455 unsigned long flags
;
1457 /* turn off overlay */
1458 if (res_check(fh
, RESOURCE_OVERLAY
)) {
1459 spin_lock_irqsave(&dev
->slock
,flags
);
1460 stop_preview(dev
,fh
);
1461 spin_unlock_irqrestore(&dev
->slock
,flags
);
1462 res_free(dev
,fh
,RESOURCE_OVERLAY
);
1465 /* stop video capture */
1466 if (res_check(fh
, RESOURCE_VIDEO
)) {
1467 videobuf_streamoff(&fh
->cap
);
1468 res_free(dev
,fh
,RESOURCE_VIDEO
);
1470 if (fh
->cap
.read_buf
) {
1471 buffer_release(&fh
->cap
,fh
->cap
.read_buf
);
1472 kfree(fh
->cap
.read_buf
);
1475 /* stop vbi capture */
1476 if (res_check(fh
, RESOURCE_VBI
)) {
1477 videobuf_stop(&fh
->vbi
);
1478 res_free(dev
,fh
,RESOURCE_VBI
);
1481 /* ts-capture will not work in planar mode, so turn it off Hac: 04.05*/
1482 saa_andorb(SAA7134_OFMT_VIDEO_A
, 0x1f, 0);
1483 saa_andorb(SAA7134_OFMT_VIDEO_B
, 0x1f, 0);
1484 saa_andorb(SAA7134_OFMT_DATA_A
, 0x1f, 0);
1485 saa_andorb(SAA7134_OFMT_DATA_B
, 0x1f, 0);
1487 saa7134_i2c_call_clients(dev
, TUNER_SET_STANDBY
, NULL
);
1490 videobuf_mmap_free(&fh
->cap
);
1491 videobuf_mmap_free(&fh
->vbi
);
1492 saa7134_pgtable_free(dev
->pci
,&fh
->pt_cap
);
1493 saa7134_pgtable_free(dev
->pci
,&fh
->pt_vbi
);
1495 v4l2_prio_close(&dev
->prio
,&fh
->prio
);
1496 file
->private_data
= NULL
;
1501 static int video_mmap(struct file
*file
, struct vm_area_struct
* vma
)
1503 struct saa7134_fh
*fh
= file
->private_data
;
1505 return videobuf_mmap_mapper(saa7134_queue(fh
), vma
);
1508 /* ------------------------------------------------------------------ */
1510 static int vidioc_try_get_set_fmt_vbi(struct file
*file
, void *priv
,
1511 struct v4l2_format
*f
)
1513 struct saa7134_fh
*fh
= priv
;
1514 struct saa7134_dev
*dev
= fh
->dev
;
1515 struct saa7134_tvnorm
*norm
= dev
->tvnorm
;
1517 f
->fmt
.vbi
.sampling_rate
= 6750000 * 4;
1518 f
->fmt
.vbi
.samples_per_line
= 2048 /* VBI_LINE_LENGTH */;
1519 f
->fmt
.vbi
.sample_format
= V4L2_PIX_FMT_GREY
;
1520 f
->fmt
.vbi
.offset
= 64 * 4;
1521 f
->fmt
.vbi
.start
[0] = norm
->vbi_v_start_0
;
1522 f
->fmt
.vbi
.count
[0] = norm
->vbi_v_stop_0
- norm
->vbi_v_start_0
+1;
1523 f
->fmt
.vbi
.start
[1] = norm
->vbi_v_start_1
;
1524 f
->fmt
.vbi
.count
[1] = f
->fmt
.vbi
.count
[0];
1525 f
->fmt
.vbi
.flags
= 0; /* VBI_UNSYNC VBI_INTERLACED */
1530 static int vidioc_g_fmt_cap(struct file
*file
, void *priv
,
1531 struct v4l2_format
*f
)
1533 struct saa7134_fh
*fh
= priv
;
1535 f
->fmt
.pix
.width
= fh
->width
;
1536 f
->fmt
.pix
.height
= fh
->height
;
1537 f
->fmt
.pix
.field
= fh
->cap
.field
;
1538 f
->fmt
.pix
.pixelformat
= fh
->fmt
->fourcc
;
1539 f
->fmt
.pix
.bytesperline
=
1540 (f
->fmt
.pix
.width
* fh
->fmt
->depth
) >> 3;
1541 f
->fmt
.pix
.sizeimage
=
1542 f
->fmt
.pix
.height
* f
->fmt
.pix
.bytesperline
;
1546 static int vidioc_g_fmt_overlay(struct file
*file
, void *priv
,
1547 struct v4l2_format
*f
)
1549 struct saa7134_fh
*fh
= priv
;
1551 if (saa7134_no_overlay
> 0) {
1552 printk(KERN_ERR
"V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
1555 f
->fmt
.win
= fh
->win
;
1560 static int vidioc_try_fmt_cap(struct file
*file
, void *priv
,
1561 struct v4l2_format
*f
)
1563 struct saa7134_fh
*fh
= priv
;
1564 struct saa7134_dev
*dev
= fh
->dev
;
1565 struct saa7134_format
*fmt
;
1566 enum v4l2_field field
;
1567 unsigned int maxw
, maxh
;
1569 fmt
= format_by_fourcc(f
->fmt
.pix
.pixelformat
);
1573 field
= f
->fmt
.pix
.field
;
1574 maxw
= min(dev
->crop_current
.width
*4, dev
->crop_bounds
.width
);
1575 maxh
= min(dev
->crop_current
.height
*4, dev
->crop_bounds
.height
);
1577 if (V4L2_FIELD_ANY
== field
) {
1578 field
= (f
->fmt
.pix
.height
> maxh
/2)
1579 ? V4L2_FIELD_INTERLACED
1580 : V4L2_FIELD_BOTTOM
;
1583 case V4L2_FIELD_TOP
:
1584 case V4L2_FIELD_BOTTOM
:
1587 case V4L2_FIELD_INTERLACED
:
1593 f
->fmt
.pix
.field
= field
;
1594 if (f
->fmt
.pix
.width
< 48)
1595 f
->fmt
.pix
.width
= 48;
1596 if (f
->fmt
.pix
.height
< 32)
1597 f
->fmt
.pix
.height
= 32;
1598 if (f
->fmt
.pix
.width
> maxw
)
1599 f
->fmt
.pix
.width
= maxw
;
1600 if (f
->fmt
.pix
.height
> maxh
)
1601 f
->fmt
.pix
.height
= maxh
;
1602 f
->fmt
.pix
.width
&= ~0x03;
1603 f
->fmt
.pix
.bytesperline
=
1604 (f
->fmt
.pix
.width
* fmt
->depth
) >> 3;
1605 f
->fmt
.pix
.sizeimage
=
1606 f
->fmt
.pix
.height
* f
->fmt
.pix
.bytesperline
;
1611 static int vidioc_try_fmt_overlay(struct file
*file
, void *priv
,
1612 struct v4l2_format
*f
)
1614 struct saa7134_fh
*fh
= priv
;
1615 struct saa7134_dev
*dev
= fh
->dev
;
1617 if (saa7134_no_overlay
> 0) {
1618 printk(KERN_ERR
"V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
1622 return verify_preview(dev
, &f
->fmt
.win
);
1625 static int vidioc_s_fmt_cap(struct file
*file
, void *priv
,
1626 struct v4l2_format
*f
)
1628 struct saa7134_fh
*fh
= priv
;
1631 err
= vidioc_try_fmt_cap(file
, priv
, f
);
1635 fh
->fmt
= format_by_fourcc(f
->fmt
.pix
.pixelformat
);
1636 fh
->width
= f
->fmt
.pix
.width
;
1637 fh
->height
= f
->fmt
.pix
.height
;
1638 fh
->cap
.field
= f
->fmt
.pix
.field
;
1642 static int vidioc_s_fmt_overlay(struct file
*file
, void *priv
,
1643 struct v4l2_format
*f
)
1645 struct saa7134_fh
*fh
= priv
;
1646 struct saa7134_dev
*dev
= fh
->dev
;
1650 if (saa7134_no_overlay
> 0) {
1651 printk(KERN_ERR
"V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
1654 err
= verify_preview(dev
, &f
->fmt
.win
);
1658 mutex_lock(&dev
->lock
);
1660 fh
->win
= f
->fmt
.win
;
1661 fh
->nclips
= f
->fmt
.win
.clipcount
;
1666 if (copy_from_user(fh
->clips
, f
->fmt
.win
.clips
,
1667 sizeof(struct v4l2_clip
)*fh
->nclips
)) {
1668 mutex_unlock(&dev
->lock
);
1672 if (res_check(fh
, RESOURCE_OVERLAY
)) {
1673 spin_lock_irqsave(&dev
->slock
, flags
);
1674 stop_preview(dev
, fh
);
1675 start_preview(dev
, fh
);
1676 spin_unlock_irqrestore(&dev
->slock
, flags
);
1679 mutex_unlock(&dev
->lock
);
1683 static int vidioc_queryctrl(struct file
*file
, void *priv
,
1684 struct v4l2_queryctrl
*c
)
1686 const struct v4l2_queryctrl
*ctrl
;
1688 if ((c
->id
< V4L2_CID_BASE
||
1689 c
->id
>= V4L2_CID_LASTP1
) &&
1690 (c
->id
< V4L2_CID_PRIVATE_BASE
||
1691 c
->id
>= V4L2_CID_PRIVATE_LASTP1
))
1693 ctrl
= ctrl_by_id(c
->id
);
1694 *c
= (NULL
!= ctrl
) ? *ctrl
: no_ctrl
;
1698 static int vidioc_enum_input(struct file
*file
, void *priv
,
1699 struct v4l2_input
*i
)
1701 struct saa7134_fh
*fh
= priv
;
1702 struct saa7134_dev
*dev
= fh
->dev
;
1706 if (n
>= SAA7134_INPUT_MAX
)
1708 if (NULL
== card_in(dev
, i
->index
).name
)
1710 memset(i
, 0, sizeof(*i
));
1712 i
->type
= V4L2_INPUT_TYPE_CAMERA
;
1713 strcpy(i
->name
, card_in(dev
, n
).name
);
1714 if (card_in(dev
, n
).tv
)
1715 i
->type
= V4L2_INPUT_TYPE_TUNER
;
1717 if (n
== dev
->ctl_input
) {
1718 int v1
= saa_readb(SAA7134_STATUS_VIDEO1
);
1719 int v2
= saa_readb(SAA7134_STATUS_VIDEO2
);
1721 if (0 != (v1
& 0x40))
1722 i
->status
|= V4L2_IN_ST_NO_H_LOCK
;
1723 if (0 != (v2
& 0x40))
1724 i
->status
|= V4L2_IN_ST_NO_SYNC
;
1725 if (0 != (v2
& 0x0e))
1726 i
->status
|= V4L2_IN_ST_MACROVISION
;
1728 i
->std
= SAA7134_NORMS
;
1732 static int vidioc_g_input(struct file
*file
, void *priv
, unsigned int *i
)
1734 struct saa7134_fh
*fh
= priv
;
1735 struct saa7134_dev
*dev
= fh
->dev
;
1737 *i
= dev
->ctl_input
;
1741 static int vidioc_s_input(struct file
*file
, void *priv
, unsigned int i
)
1743 struct saa7134_fh
*fh
= priv
;
1744 struct saa7134_dev
*dev
= fh
->dev
;
1747 err
= v4l2_prio_check(&dev
->prio
, &fh
->prio
);
1751 if (i
< 0 || i
>= SAA7134_INPUT_MAX
)
1753 if (NULL
== card_in(dev
, i
).name
)
1755 mutex_lock(&dev
->lock
);
1757 mutex_unlock(&dev
->lock
);
1761 static int vidioc_querycap(struct file
*file
, void *priv
,
1762 struct v4l2_capability
*cap
)
1764 struct saa7134_fh
*fh
= priv
;
1765 struct saa7134_dev
*dev
= fh
->dev
;
1767 unsigned int tuner_type
= dev
->tuner_type
;
1769 strcpy(cap
->driver
, "saa7134");
1770 strlcpy(cap
->card
, saa7134_boards
[dev
->board
].name
,
1772 sprintf(cap
->bus_info
, "PCI:%s", pci_name(dev
->pci
));
1773 cap
->version
= SAA7134_VERSION_CODE
;
1775 V4L2_CAP_VIDEO_CAPTURE
|
1776 V4L2_CAP_VBI_CAPTURE
|
1777 V4L2_CAP_READWRITE
|
1778 V4L2_CAP_STREAMING
|
1780 if (saa7134_no_overlay
<= 0)
1781 cap
->capabilities
|= V4L2_CAP_VIDEO_OVERLAY
;
1783 if ((tuner_type
== TUNER_ABSENT
) || (tuner_type
== UNSET
))
1784 cap
->capabilities
&= ~V4L2_CAP_TUNER
;
1788 static int vidioc_s_std(struct file
*file
, void *priv
, v4l2_std_id
* id
)
1790 struct saa7134_fh
*fh
= priv
;
1791 struct saa7134_dev
*dev
= fh
->dev
;
1792 unsigned long flags
;
1797 err
= v4l2_prio_check(&dev
->prio
, &fh
->prio
);
1801 for (i
= 0; i
< TVNORMS
; i
++)
1802 if (*id
== tvnorms
[i
].id
)
1806 for (i
= 0; i
< TVNORMS
; i
++)
1807 if (*id
& tvnorms
[i
].id
)
1812 if ((*id
& V4L2_STD_SECAM
) && (secam
[0] != '-')) {
1813 if (secam
[0] == 'L' || secam
[0] == 'l') {
1814 if (secam
[1] == 'C' || secam
[1] == 'c')
1815 fixup
= V4L2_STD_SECAM_LC
;
1817 fixup
= V4L2_STD_SECAM_L
;
1819 if (secam
[0] == 'D' || secam
[0] == 'd')
1820 fixup
= V4L2_STD_SECAM_DK
;
1822 fixup
= V4L2_STD_SECAM
;
1824 for (i
= 0; i
< TVNORMS
; i
++)
1825 if (fixup
== tvnorms
[i
].id
)
1829 *id
= tvnorms
[i
].id
;
1831 mutex_lock(&dev
->lock
);
1832 if (res_check(fh
, RESOURCE_OVERLAY
)) {
1833 spin_lock_irqsave(&dev
->slock
, flags
);
1834 stop_preview(dev
, fh
);
1835 spin_unlock_irqrestore(&dev
->slock
, flags
);
1837 set_tvnorm(dev
, &tvnorms
[i
]);
1839 spin_lock_irqsave(&dev
->slock
, flags
);
1840 start_preview(dev
, fh
);
1841 spin_unlock_irqrestore(&dev
->slock
, flags
);
1843 set_tvnorm(dev
, &tvnorms
[i
]);
1845 saa7134_tvaudio_do_scan(dev
);
1846 mutex_unlock(&dev
->lock
);
1850 static int vidioc_cropcap(struct file
*file
, void *priv
,
1851 struct v4l2_cropcap
*cap
)
1853 struct saa7134_fh
*fh
= priv
;
1854 struct saa7134_dev
*dev
= fh
->dev
;
1856 if (cap
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
&&
1857 cap
->type
!= V4L2_BUF_TYPE_VIDEO_OVERLAY
)
1859 cap
->bounds
= dev
->crop_bounds
;
1860 cap
->defrect
= dev
->crop_defrect
;
1861 cap
->pixelaspect
.numerator
= 1;
1862 cap
->pixelaspect
.denominator
= 1;
1863 if (dev
->tvnorm
->id
& V4L2_STD_525_60
) {
1864 cap
->pixelaspect
.numerator
= 11;
1865 cap
->pixelaspect
.denominator
= 10;
1867 if (dev
->tvnorm
->id
& V4L2_STD_625_50
) {
1868 cap
->pixelaspect
.numerator
= 54;
1869 cap
->pixelaspect
.denominator
= 59;
1874 static int vidioc_g_crop(struct file
*file
, void *f
, struct v4l2_crop
*crop
)
1876 struct saa7134_fh
*fh
= f
;
1877 struct saa7134_dev
*dev
= fh
->dev
;
1879 if (crop
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
&&
1880 crop
->type
!= V4L2_BUF_TYPE_VIDEO_OVERLAY
)
1882 crop
->c
= dev
->crop_current
;
1886 static int vidioc_s_crop(struct file
*file
, void *f
, struct v4l2_crop
*crop
)
1888 struct saa7134_fh
*fh
= f
;
1889 struct saa7134_dev
*dev
= fh
->dev
;
1890 struct v4l2_rect
*b
= &dev
->crop_bounds
;
1892 if (crop
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
&&
1893 crop
->type
!= V4L2_BUF_TYPE_VIDEO_OVERLAY
)
1895 if (crop
->c
.height
< 0)
1897 if (crop
->c
.width
< 0)
1900 if (res_locked(fh
->dev
, RESOURCE_OVERLAY
))
1902 if (res_locked(fh
->dev
, RESOURCE_VIDEO
))
1905 if (crop
->c
.top
< b
->top
)
1906 crop
->c
.top
= b
->top
;
1907 if (crop
->c
.top
> b
->top
+ b
->height
)
1908 crop
->c
.top
= b
->top
+ b
->height
;
1909 if (crop
->c
.height
> b
->top
- crop
->c
.top
+ b
->height
)
1910 crop
->c
.height
= b
->top
- crop
->c
.top
+ b
->height
;
1912 if (crop
->c
.left
< b
->left
)
1913 crop
->c
.left
= b
->left
;
1914 if (crop
->c
.left
> b
->left
+ b
->width
)
1915 crop
->c
.left
= b
->left
+ b
->width
;
1916 if (crop
->c
.width
> b
->left
- crop
->c
.left
+ b
->width
)
1917 crop
->c
.width
= b
->left
- crop
->c
.left
+ b
->width
;
1919 dev
->crop_current
= crop
->c
;
1923 static int vidioc_g_tuner(struct file
*file
, void *priv
,
1924 struct v4l2_tuner
*t
)
1926 struct saa7134_fh
*fh
= priv
;
1927 struct saa7134_dev
*dev
= fh
->dev
;
1932 memset(t
, 0, sizeof(*t
));
1933 for (n
= 0; n
< SAA7134_INPUT_MAX
; n
++)
1934 if (card_in(dev
, n
).tv
)
1936 if (NULL
!= card_in(dev
, n
).name
) {
1937 strcpy(t
->name
, "Television");
1938 t
->type
= V4L2_TUNER_ANALOG_TV
;
1939 t
->capability
= V4L2_TUNER_CAP_NORM
|
1940 V4L2_TUNER_CAP_STEREO
|
1941 V4L2_TUNER_CAP_LANG1
|
1942 V4L2_TUNER_CAP_LANG2
;
1943 t
->rangehigh
= 0xffffffffUL
;
1944 t
->rxsubchans
= saa7134_tvaudio_getstereo(dev
);
1945 t
->audmode
= saa7134_tvaudio_rx2mode(t
->rxsubchans
);
1947 if (0 != (saa_readb(SAA7134_STATUS_VIDEO1
) & 0x03))
1952 static int vidioc_s_tuner(struct file
*file
, void *priv
,
1953 struct v4l2_tuner
*t
)
1955 struct saa7134_fh
*fh
= priv
;
1956 struct saa7134_dev
*dev
= fh
->dev
;
1959 err
= v4l2_prio_check(&dev
->prio
, &fh
->prio
);
1963 mode
= dev
->thread
.mode
;
1964 if (UNSET
== mode
) {
1965 rx
= saa7134_tvaudio_getstereo(dev
);
1966 mode
= saa7134_tvaudio_rx2mode(t
->rxsubchans
);
1968 if (mode
!= t
->audmode
)
1969 dev
->thread
.mode
= t
->audmode
;
1974 static int vidioc_g_frequency(struct file
*file
, void *priv
,
1975 struct v4l2_frequency
*f
)
1977 struct saa7134_fh
*fh
= priv
;
1978 struct saa7134_dev
*dev
= fh
->dev
;
1980 f
->type
= fh
->radio
? V4L2_TUNER_RADIO
: V4L2_TUNER_ANALOG_TV
;
1981 f
->frequency
= dev
->ctl_freq
;
1986 static int vidioc_s_frequency(struct file
*file
, void *priv
,
1987 struct v4l2_frequency
*f
)
1989 struct saa7134_fh
*fh
= priv
;
1990 struct saa7134_dev
*dev
= fh
->dev
;
1993 err
= v4l2_prio_check(&dev
->prio
, &fh
->prio
);
1999 if (0 == fh
->radio
&& V4L2_TUNER_ANALOG_TV
!= f
->type
)
2001 if (1 == fh
->radio
&& V4L2_TUNER_RADIO
!= f
->type
)
2003 mutex_lock(&dev
->lock
);
2004 dev
->ctl_freq
= f
->frequency
;
2006 saa7134_i2c_call_clients(dev
, VIDIOC_S_FREQUENCY
, f
);
2008 saa7134_tvaudio_do_scan(dev
);
2009 mutex_unlock(&dev
->lock
);
2013 static int vidioc_g_audio(struct file
*file
, void *priv
, struct v4l2_audio
*a
)
2015 strcpy(a
->name
, "audio");
2019 static int vidioc_s_audio(struct file
*file
, void *priv
, struct v4l2_audio
*a
)
2024 static int vidioc_g_priority(struct file
*file
, void *f
, enum v4l2_priority
*p
)
2026 struct saa7134_fh
*fh
= f
;
2027 struct saa7134_dev
*dev
= fh
->dev
;
2029 *p
= v4l2_prio_max(&dev
->prio
);
2033 static int vidioc_s_priority(struct file
*file
, void *f
,
2034 enum v4l2_priority prio
)
2036 struct saa7134_fh
*fh
= f
;
2037 struct saa7134_dev
*dev
= fh
->dev
;
2039 return v4l2_prio_change(&dev
->prio
, &fh
->prio
, prio
);
2042 static int vidioc_enum_fmt_cap(struct file
*file
, void *priv
,
2043 struct v4l2_fmtdesc
*f
)
2045 if (f
->index
>= FORMATS
)
2048 strlcpy(f
->description
, formats
[f
->index
].name
,
2049 sizeof(f
->description
));
2051 f
->pixelformat
= formats
[f
->index
].fourcc
;
2056 static int vidioc_enum_fmt_overlay(struct file
*file
, void *priv
,
2057 struct v4l2_fmtdesc
*f
)
2059 if (saa7134_no_overlay
> 0) {
2060 printk(KERN_ERR
"V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
2064 if ((f
->index
>= FORMATS
) || formats
[f
->index
].planar
)
2067 strlcpy(f
->description
, formats
[f
->index
].name
,
2068 sizeof(f
->description
));
2070 f
->pixelformat
= formats
[f
->index
].fourcc
;
2075 static int vidioc_enum_fmt_vbi(struct file
*file
, void *priv
,
2076 struct v4l2_fmtdesc
*f
)
2081 f
->pixelformat
= V4L2_PIX_FMT_GREY
;
2082 strcpy(f
->description
, "vbi data");
2087 static int vidioc_g_fbuf(struct file
*file
, void *f
,
2088 struct v4l2_framebuffer
*fb
)
2090 struct saa7134_fh
*fh
= f
;
2091 struct saa7134_dev
*dev
= fh
->dev
;
2094 fb
->capability
= V4L2_FBUF_CAP_LIST_CLIPPING
;
2099 static int vidioc_s_fbuf(struct file
*file
, void *f
,
2100 struct v4l2_framebuffer
*fb
)
2102 struct saa7134_fh
*fh
= f
;
2103 struct saa7134_dev
*dev
= fh
->dev
;
2104 struct saa7134_format
*fmt
;
2106 if (!capable(CAP_SYS_ADMIN
) &&
2107 !capable(CAP_SYS_RAWIO
))
2111 fmt
= format_by_fourcc(fb
->fmt
.pixelformat
);
2118 if (0 == dev
->ovbuf
.fmt
.bytesperline
)
2119 dev
->ovbuf
.fmt
.bytesperline
=
2120 dev
->ovbuf
.fmt
.width
*fmt
->depth
/8;
2124 static int vidioc_overlay(struct file
*file
, void *f
, unsigned int on
)
2126 struct saa7134_fh
*fh
= f
;
2127 struct saa7134_dev
*dev
= fh
->dev
;
2128 unsigned long flags
;
2131 if (saa7134_no_overlay
> 0) {
2132 dprintk("no_overlay\n");
2136 if (!res_get(dev
, fh
, RESOURCE_OVERLAY
))
2138 spin_lock_irqsave(&dev
->slock
, flags
);
2139 start_preview(dev
, fh
);
2140 spin_unlock_irqrestore(&dev
->slock
, flags
);
2143 if (!res_check(fh
, RESOURCE_OVERLAY
))
2145 spin_lock_irqsave(&dev
->slock
, flags
);
2146 stop_preview(dev
, fh
);
2147 spin_unlock_irqrestore(&dev
->slock
, flags
);
2148 res_free(dev
, fh
, RESOURCE_OVERLAY
);
2153 #ifdef CONFIG_VIDEO_V4L1_COMPAT
2154 static int vidiocgmbuf(struct file
*file
, void *priv
, struct video_mbuf
*mbuf
)
2156 struct saa7134_fh
*fh
= file
->private_data
;
2157 return videobuf_cgmbuf(saa7134_queue(fh
), mbuf
, 8);
2161 static int vidioc_reqbufs(struct file
*file
, void *priv
,
2162 struct v4l2_requestbuffers
*p
)
2164 struct saa7134_fh
*fh
= priv
;
2165 return videobuf_reqbufs(saa7134_queue(fh
), p
);
2168 static int vidioc_querybuf(struct file
*file
, void *priv
,
2169 struct v4l2_buffer
*b
)
2171 struct saa7134_fh
*fh
= priv
;
2172 return videobuf_querybuf(saa7134_queue(fh
), b
);
2175 static int vidioc_qbuf(struct file
*file
, void *priv
, struct v4l2_buffer
*b
)
2177 struct saa7134_fh
*fh
= priv
;
2178 return videobuf_qbuf(saa7134_queue(fh
), b
);
2181 static int vidioc_dqbuf(struct file
*file
, void *priv
, struct v4l2_buffer
*b
)
2183 struct saa7134_fh
*fh
= priv
;
2184 return videobuf_dqbuf(saa7134_queue(fh
), b
,
2185 file
->f_flags
& O_NONBLOCK
);
2188 static int vidioc_streamon(struct file
*file
, void *priv
,
2189 enum v4l2_buf_type type
)
2191 struct saa7134_fh
*fh
= priv
;
2192 struct saa7134_dev
*dev
= fh
->dev
;
2193 int res
= saa7134_resource(fh
);
2195 if (!res_get(dev
, fh
, res
))
2198 return videobuf_streamon(saa7134_queue(fh
));
2201 static int vidioc_streamoff(struct file
*file
, void *priv
,
2202 enum v4l2_buf_type type
)
2205 struct saa7134_fh
*fh
= priv
;
2206 struct saa7134_dev
*dev
= fh
->dev
;
2207 int res
= saa7134_resource(fh
);
2209 err
= videobuf_streamoff(saa7134_queue(fh
));
2212 res_free(dev
, fh
, res
);
2216 static int vidioc_g_parm(struct file
*file
, void *fh
,
2217 struct v4l2_streamparm
*parm
)
2222 static int radio_querycap(struct file
*file
, void *priv
,
2223 struct v4l2_capability
*cap
)
2225 struct saa7134_fh
*fh
= file
->private_data
;
2226 struct saa7134_dev
*dev
= fh
->dev
;
2228 strcpy(cap
->driver
, "saa7134");
2229 strlcpy(cap
->card
, saa7134_boards
[dev
->board
].name
, sizeof(cap
->card
));
2230 sprintf(cap
->bus_info
, "PCI:%s", pci_name(dev
->pci
));
2231 cap
->version
= SAA7134_VERSION_CODE
;
2232 cap
->capabilities
= V4L2_CAP_TUNER
;
2236 static int radio_g_tuner(struct file
*file
, void *priv
,
2237 struct v4l2_tuner
*t
)
2239 struct saa7134_fh
*fh
= file
->private_data
;
2240 struct saa7134_dev
*dev
= fh
->dev
;
2245 memset(t
, 0, sizeof(*t
));
2246 strcpy(t
->name
, "Radio");
2247 t
->type
= V4L2_TUNER_RADIO
;
2249 saa7134_i2c_call_clients(dev
, VIDIOC_G_TUNER
, t
);
2250 if (dev
->input
->amux
== TV
) {
2251 t
->signal
= 0xf800 - ((saa_readb(0x581) & 0x1f) << 11);
2252 t
->rxsubchans
= (saa_readb(0x529) & 0x08) ?
2253 V4L2_TUNER_SUB_STEREO
: V4L2_TUNER_SUB_MONO
;
2257 static int radio_s_tuner(struct file
*file
, void *priv
,
2258 struct v4l2_tuner
*t
)
2260 struct saa7134_fh
*fh
= file
->private_data
;
2261 struct saa7134_dev
*dev
= fh
->dev
;
2266 saa7134_i2c_call_clients(dev
, VIDIOC_S_TUNER
, t
);
2270 static int radio_enum_input(struct file
*file
, void *priv
,
2271 struct v4l2_input
*i
)
2276 strcpy(i
->name
, "Radio");
2277 i
->type
= V4L2_INPUT_TYPE_TUNER
;
2282 static int radio_g_input(struct file
*filp
, void *priv
, unsigned int *i
)
2288 static int radio_g_audio(struct file
*file
, void *priv
,
2289 struct v4l2_audio
*a
)
2291 memset(a
, 0, sizeof(*a
));
2292 strcpy(a
->name
, "Radio");
2296 static int radio_s_audio(struct file
*file
, void *priv
,
2297 struct v4l2_audio
*a
)
2302 static int radio_s_input(struct file
*filp
, void *priv
, unsigned int i
)
2307 static int radio_s_std(struct file
*file
, void *fh
, v4l2_std_id
*norm
)
2312 static int radio_queryctrl(struct file
*file
, void *priv
,
2313 struct v4l2_queryctrl
*c
)
2315 const struct v4l2_queryctrl
*ctrl
;
2317 if (c
->id
< V4L2_CID_BASE
||
2318 c
->id
>= V4L2_CID_LASTP1
)
2320 if (c
->id
== V4L2_CID_AUDIO_MUTE
) {
2321 ctrl
= ctrl_by_id(c
->id
);
2328 static const struct file_operations video_fops
=
2330 .owner
= THIS_MODULE
,
2332 .release
= video_release
,
2336 .ioctl
= video_ioctl2
,
2337 .compat_ioctl
= v4l_compat_ioctl32
,
2338 .llseek
= no_llseek
,
2341 static const struct file_operations radio_fops
=
2343 .owner
= THIS_MODULE
,
2345 .release
= video_release
,
2346 .ioctl
= video_ioctl2
,
2347 .compat_ioctl
= v4l_compat_ioctl32
,
2348 .llseek
= no_llseek
,
2351 /* ----------------------------------------------------------- */
2352 /* exported stuff */
2354 struct video_device saa7134_video_template
=
2356 .name
= "saa7134-video",
2357 .type
= VID_TYPE_CAPTURE
|VID_TYPE_TUNER
|
2358 VID_TYPE_CLIPPING
|VID_TYPE_SCALES
,
2359 .fops
= &video_fops
,
2361 .vidioc_querycap
= vidioc_querycap
,
2362 .vidioc_enum_fmt_cap
= vidioc_enum_fmt_cap
,
2363 .vidioc_g_fmt_cap
= vidioc_g_fmt_cap
,
2364 .vidioc_try_fmt_cap
= vidioc_try_fmt_cap
,
2365 .vidioc_s_fmt_cap
= vidioc_s_fmt_cap
,
2366 .vidioc_enum_fmt_overlay
= vidioc_enum_fmt_overlay
,
2367 .vidioc_g_fmt_overlay
= vidioc_g_fmt_overlay
,
2368 .vidioc_try_fmt_overlay
= vidioc_try_fmt_overlay
,
2369 .vidioc_s_fmt_overlay
= vidioc_s_fmt_overlay
,
2370 .vidioc_enum_fmt_vbi
= vidioc_enum_fmt_vbi
,
2371 .vidioc_g_fmt_vbi
= vidioc_try_get_set_fmt_vbi
,
2372 .vidioc_try_fmt_vbi
= vidioc_try_get_set_fmt_vbi
,
2373 .vidioc_s_fmt_vbi
= vidioc_try_get_set_fmt_vbi
,
2374 .vidioc_g_audio
= vidioc_g_audio
,
2375 .vidioc_s_audio
= vidioc_s_audio
,
2376 .vidioc_cropcap
= vidioc_cropcap
,
2377 .vidioc_reqbufs
= vidioc_reqbufs
,
2378 .vidioc_querybuf
= vidioc_querybuf
,
2379 .vidioc_qbuf
= vidioc_qbuf
,
2380 .vidioc_dqbuf
= vidioc_dqbuf
,
2381 .vidioc_s_std
= vidioc_s_std
,
2382 .vidioc_enum_input
= vidioc_enum_input
,
2383 .vidioc_g_input
= vidioc_g_input
,
2384 .vidioc_s_input
= vidioc_s_input
,
2385 .vidioc_queryctrl
= vidioc_queryctrl
,
2386 .vidioc_g_ctrl
= vidioc_g_ctrl
,
2387 .vidioc_s_ctrl
= vidioc_s_ctrl
,
2388 .vidioc_streamon
= vidioc_streamon
,
2389 .vidioc_streamoff
= vidioc_streamoff
,
2390 .vidioc_g_tuner
= vidioc_g_tuner
,
2391 .vidioc_s_tuner
= vidioc_s_tuner
,
2392 #ifdef CONFIG_VIDEO_V4L1_COMPAT
2393 .vidiocgmbuf
= vidiocgmbuf
,
2395 .vidioc_g_crop
= vidioc_g_crop
,
2396 .vidioc_s_crop
= vidioc_s_crop
,
2397 .vidioc_g_fbuf
= vidioc_g_fbuf
,
2398 .vidioc_s_fbuf
= vidioc_s_fbuf
,
2399 .vidioc_overlay
= vidioc_overlay
,
2400 .vidioc_g_priority
= vidioc_g_priority
,
2401 .vidioc_s_priority
= vidioc_s_priority
,
2402 .vidioc_g_parm
= vidioc_g_parm
,
2403 .vidioc_g_frequency
= vidioc_g_frequency
,
2404 .vidioc_s_frequency
= vidioc_s_frequency
,
2405 .tvnorms
= SAA7134_NORMS
,
2406 .current_norm
= V4L2_STD_PAL
,
2409 struct video_device saa7134_vbi_template
=
2411 .name
= "saa7134-vbi",
2412 .type
= VID_TYPE_TUNER
|VID_TYPE_TELETEXT
,
2413 .fops
= &video_fops
,
2417 struct video_device saa7134_radio_template
=
2419 .name
= "saa7134-radio",
2420 .type
= VID_TYPE_TUNER
,
2421 .fops
= &radio_fops
,
2423 .vidioc_querycap
= radio_querycap
,
2424 .vidioc_g_tuner
= radio_g_tuner
,
2425 .vidioc_enum_input
= radio_enum_input
,
2426 .vidioc_g_audio
= radio_g_audio
,
2427 .vidioc_s_tuner
= radio_s_tuner
,
2428 .vidioc_s_audio
= radio_s_audio
,
2429 .vidioc_s_input
= radio_s_input
,
2430 .vidioc_s_std
= radio_s_std
,
2431 .vidioc_queryctrl
= radio_queryctrl
,
2432 .vidioc_g_input
= radio_g_input
,
2433 .vidioc_g_ctrl
= vidioc_g_ctrl
,
2434 .vidioc_s_ctrl
= vidioc_s_ctrl
,
2435 .vidioc_g_frequency
= vidioc_g_frequency
,
2436 .vidioc_s_frequency
= vidioc_s_frequency
,
2439 int saa7134_video_init1(struct saa7134_dev
*dev
)
2441 /* sanitycheck insmod options */
2442 if (gbuffers
< 2 || gbuffers
> VIDEO_MAX_FRAME
)
2444 if (gbufsize
< 0 || gbufsize
> gbufsize_max
)
2445 gbufsize
= gbufsize_max
;
2446 gbufsize
= (gbufsize
+ PAGE_SIZE
- 1) & PAGE_MASK
;
2448 /* put some sensible defaults into the data structures ... */
2449 dev
->ctl_bright
= ctrl_by_id(V4L2_CID_BRIGHTNESS
)->default_value
;
2450 dev
->ctl_contrast
= ctrl_by_id(V4L2_CID_CONTRAST
)->default_value
;
2451 dev
->ctl_hue
= ctrl_by_id(V4L2_CID_HUE
)->default_value
;
2452 dev
->ctl_saturation
= ctrl_by_id(V4L2_CID_SATURATION
)->default_value
;
2453 dev
->ctl_volume
= ctrl_by_id(V4L2_CID_AUDIO_VOLUME
)->default_value
;
2454 dev
->ctl_mute
= 1; // ctrl_by_id(V4L2_CID_AUDIO_MUTE)->default_value;
2455 dev
->ctl_invert
= ctrl_by_id(V4L2_CID_PRIVATE_INVERT
)->default_value
;
2456 dev
->ctl_automute
= ctrl_by_id(V4L2_CID_PRIVATE_AUTOMUTE
)->default_value
;
2458 if (dev
->tda9887_conf
&& dev
->ctl_automute
)
2459 dev
->tda9887_conf
|= TDA9887_AUTOMUTE
;
2462 INIT_LIST_HEAD(&dev
->video_q
.queue
);
2463 init_timer(&dev
->video_q
.timeout
);
2464 dev
->video_q
.timeout
.function
= saa7134_buffer_timeout
;
2465 dev
->video_q
.timeout
.data
= (unsigned long)(&dev
->video_q
);
2466 dev
->video_q
.dev
= dev
;
2468 if (saa7134_boards
[dev
->board
].video_out
)
2469 saa7134_videoport_init(dev
);
2474 int saa7134_videoport_init(struct saa7134_dev
*dev
)
2476 /* enable video output */
2477 int vo
= saa7134_boards
[dev
->board
].video_out
;
2479 unsigned int vid_port_opts
= saa7134_boards
[dev
->board
].vid_port_opts
;
2480 saa_writeb(SAA7134_VIDEO_PORT_CTRL0
, video_out
[vo
][0]);
2481 video_reg
= video_out
[vo
][1];
2482 if (vid_port_opts
& SET_T_CODE_POLARITY_NON_INVERTED
)
2483 video_reg
&= ~VP_T_CODE_P_INVERTED
;
2484 saa_writeb(SAA7134_VIDEO_PORT_CTRL1
, video_reg
);
2485 saa_writeb(SAA7134_VIDEO_PORT_CTRL2
, video_out
[vo
][2]);
2486 saa_writeb(SAA7134_VIDEO_PORT_CTRL3
, video_out
[vo
][3]);
2487 saa_writeb(SAA7134_VIDEO_PORT_CTRL4
, video_out
[vo
][4]);
2488 video_reg
= video_out
[vo
][5];
2489 if (vid_port_opts
& SET_CLOCK_NOT_DELAYED
)
2490 video_reg
&= ~VP_CLK_CTRL2_DELAYED
;
2491 if (vid_port_opts
& SET_CLOCK_INVERTED
)
2492 video_reg
|= VP_CLK_CTRL1_INVERTED
;
2493 saa_writeb(SAA7134_VIDEO_PORT_CTRL5
, video_reg
);
2494 video_reg
= video_out
[vo
][6];
2495 if (vid_port_opts
& SET_VSYNC_OFF
) {
2496 video_reg
&= ~VP_VS_TYPE_MASK
;
2497 video_reg
|= VP_VS_TYPE_OFF
;
2499 saa_writeb(SAA7134_VIDEO_PORT_CTRL6
, video_reg
);
2500 saa_writeb(SAA7134_VIDEO_PORT_CTRL7
, video_out
[vo
][7]);
2501 saa_writeb(SAA7134_VIDEO_PORT_CTRL8
, video_out
[vo
][8]);
2506 int saa7134_video_init2(struct saa7134_dev
*dev
)
2509 set_tvnorm(dev
,&tvnorms
[0]);
2511 saa7134_tvaudio_setmute(dev
);
2512 saa7134_tvaudio_setvolume(dev
,dev
->ctl_volume
);
2516 void saa7134_irq_video_signalchange(struct saa7134_dev
*dev
)
2518 static const char *st
[] = {
2519 "(no signal)", "NTSC", "PAL", "SECAM" };
2522 st1
= saa_readb(SAA7134_STATUS_VIDEO1
);
2523 st2
= saa_readb(SAA7134_STATUS_VIDEO2
);
2524 dprintk("DCSDT: pll: %s, sync: %s, norm: %s\n",
2525 (st1
& 0x40) ? "not locked" : "locked",
2526 (st2
& 0x40) ? "no" : "yes",
2528 dev
->nosignal
= (st1
& 0x40) || (st2
& 0x40) || !(st2
& 0x1);
2530 if (dev
->nosignal
) {
2531 /* no video signal -> mute audio */
2532 if (dev
->ctl_automute
)
2534 saa7134_tvaudio_setmute(dev
);
2536 /* wake up tvaudio audio carrier scan thread */
2537 saa7134_tvaudio_do_scan(dev
);
2540 if ((st2
& 0x80) && !noninterlaced
&& !dev
->nosignal
)
2541 saa_clearb(SAA7134_SYNC_CTRL
, 0x20);
2543 saa_setb(SAA7134_SYNC_CTRL
, 0x20);
2545 if (dev
->mops
&& dev
->mops
->signal_change
)
2546 dev
->mops
->signal_change(dev
);
2550 void saa7134_irq_video_done(struct saa7134_dev
*dev
, unsigned long status
)
2552 enum v4l2_field field
;
2554 spin_lock(&dev
->slock
);
2555 if (dev
->video_q
.curr
) {
2556 dev
->video_fieldcount
++;
2557 field
= dev
->video_q
.curr
->vb
.field
;
2558 if (V4L2_FIELD_HAS_BOTH(field
)) {
2559 /* make sure we have seen both fields */
2560 if ((status
& 0x10) == 0x00) {
2561 dev
->video_q
.curr
->top_seen
= 1;
2564 if (!dev
->video_q
.curr
->top_seen
)
2566 } else if (field
== V4L2_FIELD_TOP
) {
2567 if ((status
& 0x10) != 0x10)
2569 } else if (field
== V4L2_FIELD_BOTTOM
) {
2570 if ((status
& 0x10) != 0x00)
2573 dev
->video_q
.curr
->vb
.field_count
= dev
->video_fieldcount
;
2574 saa7134_buffer_finish(dev
,&dev
->video_q
,VIDEOBUF_DONE
);
2576 saa7134_buffer_next(dev
,&dev
->video_q
);
2579 spin_unlock(&dev
->slock
);
2582 /* ----------------------------------------------------------- */