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/moduleparam.h>
27 #include <linux/kernel.h>
28 #include <linux/slab.h>
29 #include <linux/sort.h>
31 #include "saa7134-reg.h"
33 #include <media/v4l2-common.h>
35 #ifdef CONFIG_VIDEO_V4L1_COMPAT
36 /* Include V4L1 specific functions. Should be removed soon */
37 #include <linux/videodev.h>
40 /* ------------------------------------------------------------------ */
42 static unsigned int video_debug
= 0;
43 static unsigned int gbuffers
= 8;
44 static unsigned int noninterlaced
= 1;
45 static unsigned int gbufsize
= 720*576*4;
46 static unsigned int gbufsize_max
= 720*576*4;
47 static char secam
[] = "--";
48 module_param(video_debug
, int, 0644);
49 MODULE_PARM_DESC(video_debug
,"enable debug messages [video]");
50 module_param(gbuffers
, int, 0444);
51 MODULE_PARM_DESC(gbuffers
,"number of capture buffers, range 2-32");
52 module_param(noninterlaced
, int, 0644);
53 MODULE_PARM_DESC(noninterlaced
,"capture non interlaced video");
54 module_param_string(secam
, secam
, sizeof(secam
), 0644);
55 MODULE_PARM_DESC(secam
, "force SECAM variant, either DK,L or Lc");
58 #define dprintk(fmt, arg...) if (video_debug) \
59 printk(KERN_DEBUG "%s/video: " fmt, dev->name , ## arg)
61 /* ------------------------------------------------------------------ */
62 /* Defines for Video Output Port Register at address 0x191 */
64 /* Bit 0: VIP code T bit polarity */
66 #define VP_T_CODE_P_NON_INVERTED 0x00
67 #define VP_T_CODE_P_INVERTED 0x01
69 /* ------------------------------------------------------------------ */
70 /* Defines for Video Output Port Register at address 0x195 */
72 /* Bit 2: Video output clock delay control */
74 #define VP_CLK_CTRL2_NOT_DELAYED 0x00
75 #define VP_CLK_CTRL2_DELAYED 0x04
77 /* Bit 1: Video output clock invert control */
79 #define VP_CLK_CTRL1_NON_INVERTED 0x00
80 #define VP_CLK_CTRL1_INVERTED 0x02
82 /* ------------------------------------------------------------------ */
83 /* Defines for Video Output Port Register at address 0x196 */
85 /* Bits 2 to 0: VSYNC pin video vertical sync type */
87 #define VP_VS_TYPE_MASK 0x07
89 #define VP_VS_TYPE_OFF 0x00
90 #define VP_VS_TYPE_V123 0x01
91 #define VP_VS_TYPE_V_ITU 0x02
92 #define VP_VS_TYPE_VGATE_L 0x03
93 #define VP_VS_TYPE_RESERVED1 0x04
94 #define VP_VS_TYPE_RESERVED2 0x05
95 #define VP_VS_TYPE_F_ITU 0x06
96 #define VP_VS_TYPE_SC_FID 0x07
98 /* ------------------------------------------------------------------ */
99 /* data structs for video */
101 static int video_out
[][9] = {
102 [CCIR656
] = { 0x00, 0xb1, 0x00, 0xa1, 0x00, 0x04, 0x06, 0x00, 0x00 },
105 static struct saa7134_format formats
[] = {
107 .name
= "8 bpp gray",
108 .fourcc
= V4L2_PIX_FMT_GREY
,
112 .name
= "15 bpp RGB, le",
113 .fourcc
= V4L2_PIX_FMT_RGB555
,
117 .name
= "15 bpp RGB, be",
118 .fourcc
= V4L2_PIX_FMT_RGB555X
,
123 .name
= "16 bpp RGB, le",
124 .fourcc
= V4L2_PIX_FMT_RGB565
,
128 .name
= "16 bpp RGB, be",
129 .fourcc
= V4L2_PIX_FMT_RGB565X
,
134 .name
= "24 bpp RGB, le",
135 .fourcc
= V4L2_PIX_FMT_BGR24
,
139 .name
= "24 bpp RGB, be",
140 .fourcc
= V4L2_PIX_FMT_RGB24
,
145 .name
= "32 bpp RGB, le",
146 .fourcc
= V4L2_PIX_FMT_BGR32
,
150 .name
= "32 bpp RGB, be",
151 .fourcc
= V4L2_PIX_FMT_RGB32
,
157 .name
= "4:2:2 packed, YUYV",
158 .fourcc
= V4L2_PIX_FMT_YUYV
,
164 .name
= "4:2:2 packed, UYVY",
165 .fourcc
= V4L2_PIX_FMT_UYVY
,
170 .name
= "4:2:2 planar, Y-Cb-Cr",
171 .fourcc
= V4L2_PIX_FMT_YUV422P
,
179 .name
= "4:2:0 planar, Y-Cb-Cr",
180 .fourcc
= V4L2_PIX_FMT_YUV420
,
188 .name
= "4:2:0 planar, Y-Cb-Cr",
189 .fourcc
= V4L2_PIX_FMT_YVU420
,
199 #define FORMATS ARRAY_SIZE(formats)
201 #define NORM_625_50 \
204 .video_v_start = 24, \
205 .video_v_stop = 311, \
206 .vbi_v_start_0 = 7, \
207 .vbi_v_stop_0 = 22, \
208 .vbi_v_start_1 = 319, \
211 #define NORM_525_60 \
214 .video_v_start = 23, \
215 .video_v_stop = 262, \
216 .vbi_v_start_0 = 10, \
217 .vbi_v_stop_0 = 21, \
218 .vbi_v_start_1 = 273, \
221 static struct saa7134_tvnorm tvnorms
[] = {
223 .name
= "PAL", /* autodetect */
227 .sync_control
= 0x18,
228 .luma_control
= 0x40,
229 .chroma_ctrl1
= 0x81,
231 .chroma_ctrl2
= 0x06,
236 .id
= V4L2_STD_PAL_BG
,
239 .sync_control
= 0x18,
240 .luma_control
= 0x40,
241 .chroma_ctrl1
= 0x81,
243 .chroma_ctrl2
= 0x06,
248 .id
= V4L2_STD_PAL_I
,
251 .sync_control
= 0x18,
252 .luma_control
= 0x40,
253 .chroma_ctrl1
= 0x81,
255 .chroma_ctrl2
= 0x06,
260 .id
= V4L2_STD_PAL_DK
,
263 .sync_control
= 0x18,
264 .luma_control
= 0x40,
265 .chroma_ctrl1
= 0x81,
267 .chroma_ctrl2
= 0x06,
275 .sync_control
= 0x59,
276 .luma_control
= 0x40,
277 .chroma_ctrl1
= 0x89,
279 .chroma_ctrl2
= 0x0e,
284 .id
= V4L2_STD_SECAM
,
287 .sync_control
= 0x18,
288 .luma_control
= 0x1b,
289 .chroma_ctrl1
= 0xd1,
291 .chroma_ctrl2
= 0x00,
296 .id
= V4L2_STD_SECAM_DK
,
299 .sync_control
= 0x18,
300 .luma_control
= 0x1b,
301 .chroma_ctrl1
= 0xd1,
303 .chroma_ctrl2
= 0x00,
308 .id
= V4L2_STD_SECAM_L
,
311 .sync_control
= 0x18,
312 .luma_control
= 0x1b,
313 .chroma_ctrl1
= 0xd1,
315 .chroma_ctrl2
= 0x00,
320 .id
= V4L2_STD_SECAM_LC
,
323 .sync_control
= 0x18,
324 .luma_control
= 0x1b,
325 .chroma_ctrl1
= 0xd1,
327 .chroma_ctrl2
= 0x00,
332 .id
= V4L2_STD_PAL_M
,
335 .sync_control
= 0x59,
336 .luma_control
= 0x40,
337 .chroma_ctrl1
= 0xb9,
339 .chroma_ctrl2
= 0x0e,
344 .id
= V4L2_STD_PAL_Nc
,
347 .sync_control
= 0x18,
348 .luma_control
= 0x40,
349 .chroma_ctrl1
= 0xa1,
351 .chroma_ctrl2
= 0x06,
356 .id
= V4L2_STD_PAL_60
,
364 .vbi_v_start_1
= 273,
367 .sync_control
= 0x18,
368 .luma_control
= 0x40,
369 .chroma_ctrl1
= 0x81,
371 .chroma_ctrl2
= 0x06,
375 #define TVNORMS ARRAY_SIZE(tvnorms)
377 #define V4L2_CID_PRIVATE_INVERT (V4L2_CID_PRIVATE_BASE + 0)
378 #define V4L2_CID_PRIVATE_Y_ODD (V4L2_CID_PRIVATE_BASE + 1)
379 #define V4L2_CID_PRIVATE_Y_EVEN (V4L2_CID_PRIVATE_BASE + 2)
380 #define V4L2_CID_PRIVATE_AUTOMUTE (V4L2_CID_PRIVATE_BASE + 3)
381 #define V4L2_CID_PRIVATE_LASTP1 (V4L2_CID_PRIVATE_BASE + 4)
383 static const struct v4l2_queryctrl no_ctrl
= {
385 .flags
= V4L2_CTRL_FLAG_DISABLED
,
387 static const struct v4l2_queryctrl video_ctrls
[] = {
390 .id
= V4L2_CID_BRIGHTNESS
,
391 .name
= "Brightness",
395 .default_value
= 128,
396 .type
= V4L2_CTRL_TYPE_INTEGER
,
398 .id
= V4L2_CID_CONTRAST
,
404 .type
= V4L2_CTRL_TYPE_INTEGER
,
406 .id
= V4L2_CID_SATURATION
,
407 .name
= "Saturation",
412 .type
= V4L2_CTRL_TYPE_INTEGER
,
420 .type
= V4L2_CTRL_TYPE_INTEGER
,
422 .id
= V4L2_CID_HFLIP
,
426 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
430 .id
= V4L2_CID_AUDIO_MUTE
,
434 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
436 .id
= V4L2_CID_AUDIO_VOLUME
,
442 .type
= V4L2_CTRL_TYPE_INTEGER
,
444 /* --- private --- */
446 .id
= V4L2_CID_PRIVATE_INVERT
,
450 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
452 .id
= V4L2_CID_PRIVATE_Y_ODD
,
453 .name
= "y offset odd field",
457 .type
= V4L2_CTRL_TYPE_INTEGER
,
459 .id
= V4L2_CID_PRIVATE_Y_EVEN
,
460 .name
= "y offset even field",
464 .type
= V4L2_CTRL_TYPE_INTEGER
,
466 .id
= V4L2_CID_PRIVATE_AUTOMUTE
,
471 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
474 static const unsigned int CTRLS
= ARRAY_SIZE(video_ctrls
);
476 static const struct v4l2_queryctrl
* ctrl_by_id(unsigned int id
)
480 for (i
= 0; i
< CTRLS
; i
++)
481 if (video_ctrls
[i
].id
== id
)
482 return video_ctrls
+i
;
486 static struct saa7134_format
* format_by_fourcc(unsigned int fourcc
)
490 for (i
= 0; i
< FORMATS
; i
++)
491 if (formats
[i
].fourcc
== fourcc
)
496 /* ----------------------------------------------------------------------- */
497 /* resource management */
499 static int res_get(struct saa7134_dev
*dev
, struct saa7134_fh
*fh
, unsigned int bit
)
501 if (fh
->resources
& bit
)
502 /* have it already allocated */
506 mutex_lock(&dev
->lock
);
507 if (dev
->resources
& bit
) {
508 /* no, someone else uses it */
509 mutex_unlock(&dev
->lock
);
512 /* it's free, grab it */
513 fh
->resources
|= bit
;
514 dev
->resources
|= bit
;
515 dprintk("res: get %d\n",bit
);
516 mutex_unlock(&dev
->lock
);
520 static int res_check(struct saa7134_fh
*fh
, unsigned int bit
)
522 return (fh
->resources
& bit
);
525 static int res_locked(struct saa7134_dev
*dev
, unsigned int bit
)
527 return (dev
->resources
& bit
);
531 void res_free(struct saa7134_dev
*dev
, struct saa7134_fh
*fh
, unsigned int bits
)
533 BUG_ON((fh
->resources
& bits
) != bits
);
535 mutex_lock(&dev
->lock
);
536 fh
->resources
&= ~bits
;
537 dev
->resources
&= ~bits
;
538 dprintk("res: put %d\n",bits
);
539 mutex_unlock(&dev
->lock
);
542 /* ------------------------------------------------------------------ */
544 static void set_tvnorm(struct saa7134_dev
*dev
, struct saa7134_tvnorm
*norm
)
546 int luma_control
,sync_control
,mux
;
548 dprintk("set tv norm = %s\n",norm
->name
);
551 mux
= card_in(dev
,dev
->ctl_input
).vmux
;
552 luma_control
= norm
->luma_control
;
553 sync_control
= norm
->sync_control
;
556 luma_control
|= 0x80; /* svideo */
557 if (noninterlaced
|| dev
->nosignal
)
558 sync_control
|= 0x20;
561 dev
->crop_bounds
.left
= norm
->h_start
;
562 dev
->crop_defrect
.left
= norm
->h_start
;
563 dev
->crop_bounds
.width
= norm
->h_stop
- norm
->h_start
+1;
564 dev
->crop_defrect
.width
= norm
->h_stop
- norm
->h_start
+1;
566 dev
->crop_bounds
.top
= (norm
->vbi_v_stop_0
+1)*2;
567 dev
->crop_defrect
.top
= norm
->video_v_start
*2;
568 dev
->crop_bounds
.height
= ((norm
->id
& V4L2_STD_525_60
) ? 524 : 624)
569 - dev
->crop_bounds
.top
;
570 dev
->crop_defrect
.height
= (norm
->video_v_stop
- norm
->video_v_start
+1)*2;
572 dev
->crop_current
= dev
->crop_defrect
;
574 /* setup video decoder */
575 saa_writeb(SAA7134_INCR_DELAY
, 0x08);
576 saa_writeb(SAA7134_ANALOG_IN_CTRL1
, 0xc0 | mux
);
577 saa_writeb(SAA7134_ANALOG_IN_CTRL2
, 0x00);
579 saa_writeb(SAA7134_ANALOG_IN_CTRL3
, 0x90);
580 saa_writeb(SAA7134_ANALOG_IN_CTRL4
, 0x90);
581 saa_writeb(SAA7134_HSYNC_START
, 0xeb);
582 saa_writeb(SAA7134_HSYNC_STOP
, 0xe0);
583 saa_writeb(SAA7134_SOURCE_TIMING1
, norm
->src_timing
);
585 saa_writeb(SAA7134_SYNC_CTRL
, sync_control
);
586 saa_writeb(SAA7134_LUMA_CTRL
, luma_control
);
587 saa_writeb(SAA7134_DEC_LUMA_BRIGHT
, dev
->ctl_bright
);
588 saa_writeb(SAA7134_DEC_LUMA_CONTRAST
, dev
->ctl_contrast
);
590 saa_writeb(SAA7134_DEC_CHROMA_SATURATION
, dev
->ctl_saturation
);
591 saa_writeb(SAA7134_DEC_CHROMA_HUE
, dev
->ctl_hue
);
592 saa_writeb(SAA7134_CHROMA_CTRL1
, norm
->chroma_ctrl1
);
593 saa_writeb(SAA7134_CHROMA_GAIN
, norm
->chroma_gain
);
595 saa_writeb(SAA7134_CHROMA_CTRL2
, norm
->chroma_ctrl2
);
596 saa_writeb(SAA7134_MODE_DELAY_CTRL
, 0x00);
598 saa_writeb(SAA7134_ANALOG_ADC
, 0x01);
599 saa_writeb(SAA7134_VGATE_START
, 0x11);
600 saa_writeb(SAA7134_VGATE_STOP
, 0xfe);
601 saa_writeb(SAA7134_MISC_VGATE_MSB
, norm
->vgate_misc
);
602 saa_writeb(SAA7134_RAW_DATA_GAIN
, 0x40);
603 saa_writeb(SAA7134_RAW_DATA_OFFSET
, 0x80);
605 /* only tell the tuner if this is a tv input */
606 if (card_in(dev
,dev
->ctl_input
).tv
) {
607 if ((card(dev
).tuner_type
== TUNER_PHILIPS_TDA8290
)
608 && ((card(dev
).tuner_config
== 1)
609 || (card(dev
).tuner_config
== 2)))
610 saa7134_set_gpio(dev
, 22, 5);
611 saa7134_i2c_call_clients(dev
,VIDIOC_S_STD
,&norm
->id
);
615 static void video_mux(struct saa7134_dev
*dev
, int input
)
617 dprintk("video input = %d [%s]\n",input
,card_in(dev
,input
).name
);
618 dev
->ctl_input
= input
;
619 set_tvnorm(dev
,dev
->tvnorm
);
620 saa7134_tvaudio_setinput(dev
,&card_in(dev
,input
));
623 static void set_h_prescale(struct saa7134_dev
*dev
, int task
, int prescale
)
625 static const struct {
632 /* XPSC XACL XC2_1 XDCG VPFY */
644 static const int count
= ARRAY_SIZE(vals
);
647 for (i
= 0; i
< count
; i
++)
648 if (vals
[i
].xpsc
== prescale
)
653 saa_writeb(SAA7134_H_PRESCALE(task
), vals
[i
].xpsc
);
654 saa_writeb(SAA7134_ACC_LENGTH(task
), vals
[i
].xacl
);
655 saa_writeb(SAA7134_LEVEL_CTRL(task
),
656 (vals
[i
].xc2_1
<< 3) | (vals
[i
].xdcg
));
657 saa_andorb(SAA7134_FIR_PREFILTER_CTRL(task
), 0x0f,
658 (vals
[i
].vpfy
<< 2) | vals
[i
].vpfy
);
661 static void set_v_scale(struct saa7134_dev
*dev
, int task
, int yscale
)
665 saa_writeb(SAA7134_V_SCALE_RATIO1(task
), yscale
& 0xff);
666 saa_writeb(SAA7134_V_SCALE_RATIO2(task
), yscale
>> 8);
668 mirror
= (dev
->ctl_mirror
) ? 0x02 : 0x00;
671 dprintk("yscale LPI yscale=%d\n",yscale
);
672 saa_writeb(SAA7134_V_FILTER(task
), 0x00 | mirror
);
673 saa_writeb(SAA7134_LUMA_CONTRAST(task
), 0x40);
674 saa_writeb(SAA7134_CHROMA_SATURATION(task
), 0x40);
677 val
= 0x40 * 1024 / yscale
;
678 dprintk("yscale ACM yscale=%d val=0x%x\n",yscale
,val
);
679 saa_writeb(SAA7134_V_FILTER(task
), 0x01 | mirror
);
680 saa_writeb(SAA7134_LUMA_CONTRAST(task
), val
);
681 saa_writeb(SAA7134_CHROMA_SATURATION(task
), val
);
683 saa_writeb(SAA7134_LUMA_BRIGHT(task
), 0x80);
686 static void set_size(struct saa7134_dev
*dev
, int task
,
687 int width
, int height
, int interlace
)
689 int prescale
,xscale
,yscale
,y_even
,y_odd
;
690 int h_start
, h_stop
, v_start
, v_stop
;
691 int div
= interlace
? 2 : 1;
693 /* setup video scaler */
694 h_start
= dev
->crop_current
.left
;
695 v_start
= dev
->crop_current
.top
/2;
696 h_stop
= (dev
->crop_current
.left
+ dev
->crop_current
.width
-1);
697 v_stop
= (dev
->crop_current
.top
+ dev
->crop_current
.height
-1)/2;
699 saa_writeb(SAA7134_VIDEO_H_START1(task
), h_start
& 0xff);
700 saa_writeb(SAA7134_VIDEO_H_START2(task
), h_start
>> 8);
701 saa_writeb(SAA7134_VIDEO_H_STOP1(task
), h_stop
& 0xff);
702 saa_writeb(SAA7134_VIDEO_H_STOP2(task
), h_stop
>> 8);
703 saa_writeb(SAA7134_VIDEO_V_START1(task
), v_start
& 0xff);
704 saa_writeb(SAA7134_VIDEO_V_START2(task
), v_start
>> 8);
705 saa_writeb(SAA7134_VIDEO_V_STOP1(task
), v_stop
& 0xff);
706 saa_writeb(SAA7134_VIDEO_V_STOP2(task
), v_stop
>> 8);
708 prescale
= dev
->crop_current
.width
/ width
;
711 xscale
= 1024 * dev
->crop_current
.width
/ prescale
/ width
;
712 yscale
= 512 * div
* dev
->crop_current
.height
/ height
;
713 dprintk("prescale=%d xscale=%d yscale=%d\n",prescale
,xscale
,yscale
);
714 set_h_prescale(dev
,task
,prescale
);
715 saa_writeb(SAA7134_H_SCALE_INC1(task
), xscale
& 0xff);
716 saa_writeb(SAA7134_H_SCALE_INC2(task
), xscale
>> 8);
717 set_v_scale(dev
,task
,yscale
);
719 saa_writeb(SAA7134_VIDEO_PIXELS1(task
), width
& 0xff);
720 saa_writeb(SAA7134_VIDEO_PIXELS2(task
), width
>> 8);
721 saa_writeb(SAA7134_VIDEO_LINES1(task
), height
/div
& 0xff);
722 saa_writeb(SAA7134_VIDEO_LINES2(task
), height
/div
>> 8);
724 /* deinterlace y offsets */
725 y_odd
= dev
->ctl_y_odd
;
726 y_even
= dev
->ctl_y_even
;
727 saa_writeb(SAA7134_V_PHASE_OFFSET0(task
), y_odd
);
728 saa_writeb(SAA7134_V_PHASE_OFFSET1(task
), y_even
);
729 saa_writeb(SAA7134_V_PHASE_OFFSET2(task
), y_odd
);
730 saa_writeb(SAA7134_V_PHASE_OFFSET3(task
), y_even
);
733 /* ------------------------------------------------------------------ */
741 static void set_cliplist(struct saa7134_dev
*dev
, int reg
,
742 struct cliplist
*cl
, int entries
, char *name
)
747 for (i
= 0; i
< entries
; i
++) {
748 winbits
|= cl
[i
].enable
;
749 winbits
&= ~cl
[i
].disable
;
750 if (i
< 15 && cl
[i
].position
== cl
[i
+1].position
)
752 saa_writeb(reg
+ 0, winbits
);
753 saa_writeb(reg
+ 2, cl
[i
].position
& 0xff);
754 saa_writeb(reg
+ 3, cl
[i
].position
>> 8);
755 dprintk("clip: %s winbits=%02x pos=%d\n",
756 name
,winbits
,cl
[i
].position
);
759 for (; reg
< 0x400; reg
+= 8) {
760 saa_writeb(reg
+ 0, 0);
761 saa_writeb(reg
+ 1, 0);
762 saa_writeb(reg
+ 2, 0);
763 saa_writeb(reg
+ 3, 0);
767 static int clip_range(int val
)
774 /* Sort into smallest position first order */
775 static int cliplist_cmp(const void *a
, const void *b
)
777 const struct cliplist
*cla
= a
;
778 const struct cliplist
*clb
= b
;
779 if (cla
->position
< clb
->position
)
781 if (cla
->position
> clb
->position
)
786 static int setup_clipping(struct saa7134_dev
*dev
, struct v4l2_clip
*clips
,
787 int nclips
, int interlace
)
789 struct cliplist col
[16], row
[16];
790 int cols
= 0, rows
= 0, i
;
791 int div
= interlace
? 2 : 1;
793 memset(col
, 0, sizeof(col
));
794 memset(row
, 0, sizeof(row
));
795 for (i
= 0; i
< nclips
&& i
< 8; i
++) {
796 col
[cols
].position
= clip_range(clips
[i
].c
.left
);
797 col
[cols
].enable
= (1 << i
);
799 col
[cols
].position
= clip_range(clips
[i
].c
.left
+clips
[i
].c
.width
);
800 col
[cols
].disable
= (1 << i
);
802 row
[rows
].position
= clip_range(clips
[i
].c
.top
/ div
);
803 row
[rows
].enable
= (1 << i
);
805 row
[rows
].position
= clip_range((clips
[i
].c
.top
+ clips
[i
].c
.height
)
807 row
[rows
].disable
= (1 << i
);
810 sort(col
, cols
, sizeof col
[0], cliplist_cmp
, NULL
);
811 sort(row
, rows
, sizeof row
[0], cliplist_cmp
, NULL
);
812 set_cliplist(dev
,0x380,col
,cols
,"cols");
813 set_cliplist(dev
,0x384,row
,rows
,"rows");
817 static int verify_preview(struct saa7134_dev
*dev
, struct v4l2_window
*win
)
819 enum v4l2_field field
;
822 if (NULL
== dev
->ovbuf
.base
)
824 if (NULL
== dev
->ovfmt
)
826 if (win
->w
.width
< 48 || win
->w
.height
< 32)
828 if (win
->clipcount
> 2048)
832 maxw
= dev
->crop_current
.width
;
833 maxh
= dev
->crop_current
.height
;
835 if (V4L2_FIELD_ANY
== field
) {
836 field
= (win
->w
.height
> maxh
/2)
837 ? V4L2_FIELD_INTERLACED
842 case V4L2_FIELD_BOTTOM
:
845 case V4L2_FIELD_INTERLACED
:
852 if (win
->w
.width
> maxw
)
854 if (win
->w
.height
> maxh
)
855 win
->w
.height
= maxh
;
859 static int start_preview(struct saa7134_dev
*dev
, struct saa7134_fh
*fh
)
861 unsigned long base
,control
,bpl
;
864 err
= verify_preview(dev
,&fh
->win
);
868 dev
->ovfield
= fh
->win
.field
;
869 dprintk("start_preview %dx%d+%d+%d %s field=%s\n",
870 fh
->win
.w
.width
,fh
->win
.w
.height
,
871 fh
->win
.w
.left
,fh
->win
.w
.top
,
872 dev
->ovfmt
->name
,v4l2_field_names
[dev
->ovfield
]);
874 /* setup window + clipping */
875 set_size(dev
,TASK_B
,fh
->win
.w
.width
,fh
->win
.w
.height
,
876 V4L2_FIELD_HAS_BOTH(dev
->ovfield
));
877 setup_clipping(dev
,fh
->clips
,fh
->nclips
,
878 V4L2_FIELD_HAS_BOTH(dev
->ovfield
));
880 saa_andorb(SAA7134_DATA_PATH(TASK_B
), 0x3f, 0x03);
882 saa_andorb(SAA7134_DATA_PATH(TASK_B
), 0x3f, 0x01);
883 saa_writeb(SAA7134_OFMT_VIDEO_B
, dev
->ovfmt
->pm
| 0x20);
885 /* dma: setup channel 1 (= Video Task B) */
886 base
= (unsigned long)dev
->ovbuf
.base
;
887 base
+= dev
->ovbuf
.fmt
.bytesperline
* fh
->win
.w
.top
;
888 base
+= dev
->ovfmt
->depth
/8 * fh
->win
.w
.left
;
889 bpl
= dev
->ovbuf
.fmt
.bytesperline
;
890 control
= SAA7134_RS_CONTROL_BURST_16
;
891 if (dev
->ovfmt
->bswap
)
892 control
|= SAA7134_RS_CONTROL_BSWAP
;
893 if (dev
->ovfmt
->wswap
)
894 control
|= SAA7134_RS_CONTROL_WSWAP
;
895 if (V4L2_FIELD_HAS_BOTH(dev
->ovfield
)) {
896 saa_writel(SAA7134_RS_BA1(1),base
);
897 saa_writel(SAA7134_RS_BA2(1),base
+bpl
);
898 saa_writel(SAA7134_RS_PITCH(1),bpl
*2);
899 saa_writel(SAA7134_RS_CONTROL(1),control
);
901 saa_writel(SAA7134_RS_BA1(1),base
);
902 saa_writel(SAA7134_RS_BA2(1),base
);
903 saa_writel(SAA7134_RS_PITCH(1),bpl
);
904 saa_writel(SAA7134_RS_CONTROL(1),control
);
909 saa7134_set_dmabits(dev
);
914 static int stop_preview(struct saa7134_dev
*dev
, struct saa7134_fh
*fh
)
917 saa7134_set_dmabits(dev
);
921 /* ------------------------------------------------------------------ */
923 static int buffer_activate(struct saa7134_dev
*dev
,
924 struct saa7134_buf
*buf
,
925 struct saa7134_buf
*next
)
927 unsigned long base
,control
,bpl
;
928 unsigned long bpl_uv
,lines_uv
,base2
,base3
,tmp
; /* planar */
930 dprintk("buffer_activate buf=%p\n",buf
);
931 buf
->vb
.state
= STATE_ACTIVE
;
934 set_size(dev
,TASK_A
,buf
->vb
.width
,buf
->vb
.height
,
935 V4L2_FIELD_HAS_BOTH(buf
->vb
.field
));
937 saa_andorb(SAA7134_DATA_PATH(TASK_A
), 0x3f, 0x03);
939 saa_andorb(SAA7134_DATA_PATH(TASK_A
), 0x3f, 0x01);
940 saa_writeb(SAA7134_OFMT_VIDEO_A
, buf
->fmt
->pm
);
942 /* DMA: setup channel 0 (= Video Task A0) */
943 base
= saa7134_buffer_base(buf
);
944 if (buf
->fmt
->planar
)
947 bpl
= (buf
->vb
.width
* buf
->fmt
->depth
) / 8;
948 control
= SAA7134_RS_CONTROL_BURST_16
|
949 SAA7134_RS_CONTROL_ME
|
950 (buf
->pt
->dma
>> 12);
952 control
|= SAA7134_RS_CONTROL_BSWAP
;
954 control
|= SAA7134_RS_CONTROL_WSWAP
;
955 if (V4L2_FIELD_HAS_BOTH(buf
->vb
.field
)) {
957 saa_writel(SAA7134_RS_BA1(0),base
);
958 saa_writel(SAA7134_RS_BA2(0),base
+bpl
);
959 saa_writel(SAA7134_RS_PITCH(0),bpl
*2);
962 saa_writel(SAA7134_RS_BA1(0),base
);
963 saa_writel(SAA7134_RS_BA2(0),base
);
964 saa_writel(SAA7134_RS_PITCH(0),bpl
);
966 saa_writel(SAA7134_RS_CONTROL(0),control
);
968 if (buf
->fmt
->planar
) {
969 /* DMA: setup channel 4+5 (= planar task A) */
970 bpl_uv
= bpl
>> buf
->fmt
->hshift
;
971 lines_uv
= buf
->vb
.height
>> buf
->fmt
->vshift
;
972 base2
= base
+ bpl
* buf
->vb
.height
;
973 base3
= base2
+ bpl_uv
* lines_uv
;
974 if (buf
->fmt
->uvswap
)
975 tmp
= base2
, base2
= base3
, base3
= tmp
;
976 dprintk("uv: bpl=%ld lines=%ld base2/3=%ld/%ld\n",
977 bpl_uv
,lines_uv
,base2
,base3
);
978 if (V4L2_FIELD_HAS_BOTH(buf
->vb
.field
)) {
980 saa_writel(SAA7134_RS_BA1(4),base2
);
981 saa_writel(SAA7134_RS_BA2(4),base2
+bpl_uv
);
982 saa_writel(SAA7134_RS_PITCH(4),bpl_uv
*2);
983 saa_writel(SAA7134_RS_BA1(5),base3
);
984 saa_writel(SAA7134_RS_BA2(5),base3
+bpl_uv
);
985 saa_writel(SAA7134_RS_PITCH(5),bpl_uv
*2);
988 saa_writel(SAA7134_RS_BA1(4),base2
);
989 saa_writel(SAA7134_RS_BA2(4),base2
);
990 saa_writel(SAA7134_RS_PITCH(4),bpl_uv
);
991 saa_writel(SAA7134_RS_BA1(5),base3
);
992 saa_writel(SAA7134_RS_BA2(5),base3
);
993 saa_writel(SAA7134_RS_PITCH(5),bpl_uv
);
995 saa_writel(SAA7134_RS_CONTROL(4),control
);
996 saa_writel(SAA7134_RS_CONTROL(5),control
);
1000 saa7134_set_dmabits(dev
);
1001 mod_timer(&dev
->video_q
.timeout
, jiffies
+BUFFER_TIMEOUT
);
1005 static int buffer_prepare(struct videobuf_queue
*q
,
1006 struct videobuf_buffer
*vb
,
1007 enum v4l2_field field
)
1009 struct saa7134_fh
*fh
= q
->priv_data
;
1010 struct saa7134_dev
*dev
= fh
->dev
;
1011 struct saa7134_buf
*buf
= container_of(vb
,struct saa7134_buf
,vb
);
1016 if (NULL
== fh
->fmt
)
1018 if (fh
->width
< 48 ||
1020 fh
->width
/4 > dev
->crop_current
.width
||
1021 fh
->height
/4 > dev
->crop_current
.height
||
1022 fh
->width
> dev
->crop_bounds
.width
||
1023 fh
->height
> dev
->crop_bounds
.height
)
1025 size
= (fh
->width
* fh
->height
* fh
->fmt
->depth
) >> 3;
1026 if (0 != buf
->vb
.baddr
&& buf
->vb
.bsize
< size
)
1029 dprintk("buffer_prepare [%d,size=%dx%d,bytes=%d,fields=%s,%s]\n",
1030 vb
->i
,fh
->width
,fh
->height
,size
,v4l2_field_names
[field
],
1032 if (buf
->vb
.width
!= fh
->width
||
1033 buf
->vb
.height
!= fh
->height
||
1034 buf
->vb
.size
!= size
||
1035 buf
->vb
.field
!= field
||
1036 buf
->fmt
!= fh
->fmt
) {
1037 saa7134_dma_free(q
,buf
);
1040 if (STATE_NEEDS_INIT
== buf
->vb
.state
) {
1041 buf
->vb
.width
= fh
->width
;
1042 buf
->vb
.height
= fh
->height
;
1043 buf
->vb
.size
= size
;
1044 buf
->vb
.field
= field
;
1046 buf
->pt
= &fh
->pt_cap
;
1048 err
= videobuf_iolock(q
,&buf
->vb
,&dev
->ovbuf
);
1051 err
= saa7134_pgtable_build(dev
->pci
,buf
->pt
,
1054 saa7134_buffer_startpage(buf
));
1058 buf
->vb
.state
= STATE_PREPARED
;
1059 buf
->activate
= buffer_activate
;
1063 saa7134_dma_free(q
,buf
);
1068 buffer_setup(struct videobuf_queue
*q
, unsigned int *count
, unsigned int *size
)
1070 struct saa7134_fh
*fh
= q
->priv_data
;
1072 *size
= fh
->fmt
->depth
* fh
->width
* fh
->height
>> 3;
1075 *count
= saa7134_buffer_count(*size
,*count
);
1079 static void buffer_queue(struct videobuf_queue
*q
, struct videobuf_buffer
*vb
)
1081 struct saa7134_fh
*fh
= q
->priv_data
;
1082 struct saa7134_buf
*buf
= container_of(vb
,struct saa7134_buf
,vb
);
1084 saa7134_buffer_queue(fh
->dev
,&fh
->dev
->video_q
,buf
);
1087 static void buffer_release(struct videobuf_queue
*q
, struct videobuf_buffer
*vb
)
1089 struct saa7134_buf
*buf
= container_of(vb
,struct saa7134_buf
,vb
);
1091 saa7134_dma_free(q
,buf
);
1094 static struct videobuf_queue_ops video_qops
= {
1095 .buf_setup
= buffer_setup
,
1096 .buf_prepare
= buffer_prepare
,
1097 .buf_queue
= buffer_queue
,
1098 .buf_release
= buffer_release
,
1101 /* ------------------------------------------------------------------ */
1103 static int get_control(struct saa7134_dev
*dev
, struct v4l2_control
*c
)
1105 const struct v4l2_queryctrl
* ctrl
;
1107 ctrl
= ctrl_by_id(c
->id
);
1111 case V4L2_CID_BRIGHTNESS
:
1112 c
->value
= dev
->ctl_bright
;
1115 c
->value
= dev
->ctl_hue
;
1117 case V4L2_CID_CONTRAST
:
1118 c
->value
= dev
->ctl_contrast
;
1120 case V4L2_CID_SATURATION
:
1121 c
->value
= dev
->ctl_saturation
;
1123 case V4L2_CID_AUDIO_MUTE
:
1124 c
->value
= dev
->ctl_mute
;
1126 case V4L2_CID_AUDIO_VOLUME
:
1127 c
->value
= dev
->ctl_volume
;
1129 case V4L2_CID_PRIVATE_INVERT
:
1130 c
->value
= dev
->ctl_invert
;
1132 case V4L2_CID_HFLIP
:
1133 c
->value
= dev
->ctl_mirror
;
1135 case V4L2_CID_PRIVATE_Y_EVEN
:
1136 c
->value
= dev
->ctl_y_even
;
1138 case V4L2_CID_PRIVATE_Y_ODD
:
1139 c
->value
= dev
->ctl_y_odd
;
1141 case V4L2_CID_PRIVATE_AUTOMUTE
:
1142 c
->value
= dev
->ctl_automute
;
1150 static int set_control(struct saa7134_dev
*dev
, struct saa7134_fh
*fh
,
1151 struct v4l2_control
*c
)
1153 const struct v4l2_queryctrl
* ctrl
;
1154 unsigned long flags
;
1155 int restart_overlay
= 0;
1157 ctrl
= ctrl_by_id(c
->id
);
1160 dprintk("set_control name=%s val=%d\n",ctrl
->name
,c
->value
);
1161 switch (ctrl
->type
) {
1162 case V4L2_CTRL_TYPE_BOOLEAN
:
1163 case V4L2_CTRL_TYPE_MENU
:
1164 case V4L2_CTRL_TYPE_INTEGER
:
1165 if (c
->value
< ctrl
->minimum
)
1166 c
->value
= ctrl
->minimum
;
1167 if (c
->value
> ctrl
->maximum
)
1168 c
->value
= ctrl
->maximum
;
1174 case V4L2_CID_BRIGHTNESS
:
1175 dev
->ctl_bright
= c
->value
;
1176 saa_writeb(SAA7134_DEC_LUMA_BRIGHT
, dev
->ctl_bright
);
1179 dev
->ctl_hue
= c
->value
;
1180 saa_writeb(SAA7134_DEC_CHROMA_HUE
, dev
->ctl_hue
);
1182 case V4L2_CID_CONTRAST
:
1183 dev
->ctl_contrast
= c
->value
;
1184 saa_writeb(SAA7134_DEC_LUMA_CONTRAST
,
1185 dev
->ctl_invert
? -dev
->ctl_contrast
: dev
->ctl_contrast
);
1187 case V4L2_CID_SATURATION
:
1188 dev
->ctl_saturation
= c
->value
;
1189 saa_writeb(SAA7134_DEC_CHROMA_SATURATION
,
1190 dev
->ctl_invert
? -dev
->ctl_saturation
: dev
->ctl_saturation
);
1192 case V4L2_CID_AUDIO_MUTE
:
1193 dev
->ctl_mute
= c
->value
;
1194 saa7134_tvaudio_setmute(dev
);
1196 case V4L2_CID_AUDIO_VOLUME
:
1197 dev
->ctl_volume
= c
->value
;
1198 saa7134_tvaudio_setvolume(dev
,dev
->ctl_volume
);
1200 case V4L2_CID_PRIVATE_INVERT
:
1201 dev
->ctl_invert
= c
->value
;
1202 saa_writeb(SAA7134_DEC_LUMA_CONTRAST
,
1203 dev
->ctl_invert
? -dev
->ctl_contrast
: dev
->ctl_contrast
);
1204 saa_writeb(SAA7134_DEC_CHROMA_SATURATION
,
1205 dev
->ctl_invert
? -dev
->ctl_saturation
: dev
->ctl_saturation
);
1207 case V4L2_CID_HFLIP
:
1208 dev
->ctl_mirror
= c
->value
;
1209 restart_overlay
= 1;
1211 case V4L2_CID_PRIVATE_Y_EVEN
:
1212 dev
->ctl_y_even
= c
->value
;
1213 restart_overlay
= 1;
1215 case V4L2_CID_PRIVATE_Y_ODD
:
1216 dev
->ctl_y_odd
= c
->value
;
1217 restart_overlay
= 1;
1219 case V4L2_CID_PRIVATE_AUTOMUTE
:
1220 dev
->ctl_automute
= c
->value
;
1221 if (dev
->tda9887_conf
) {
1222 if (dev
->ctl_automute
)
1223 dev
->tda9887_conf
|= TDA9887_AUTOMUTE
;
1225 dev
->tda9887_conf
&= ~TDA9887_AUTOMUTE
;
1226 saa7134_i2c_call_clients(dev
, TDA9887_SET_CONFIG
,
1227 &dev
->tda9887_conf
);
1233 if (restart_overlay
&& fh
&& res_check(fh
, RESOURCE_OVERLAY
)) {
1234 spin_lock_irqsave(&dev
->slock
,flags
);
1235 stop_preview(dev
,fh
);
1236 start_preview(dev
,fh
);
1237 spin_unlock_irqrestore(&dev
->slock
,flags
);
1242 /* ------------------------------------------------------------------ */
1244 static struct videobuf_queue
* saa7134_queue(struct saa7134_fh
*fh
)
1246 struct videobuf_queue
* q
= NULL
;
1249 case V4L2_BUF_TYPE_VIDEO_CAPTURE
:
1252 case V4L2_BUF_TYPE_VBI_CAPTURE
:
1261 static int saa7134_resource(struct saa7134_fh
*fh
)
1263 if (fh
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1264 return RESOURCE_VIDEO
;
1266 if (fh
->type
== V4L2_BUF_TYPE_VBI_CAPTURE
)
1267 return RESOURCE_VBI
;
1273 static int video_open(struct inode
*inode
, struct file
*file
)
1275 int minor
= iminor(inode
);
1276 struct saa7134_dev
*h
,*dev
= NULL
;
1277 struct saa7134_fh
*fh
;
1278 struct list_head
*list
;
1279 enum v4l2_buf_type type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1281 list_for_each(list
,&saa7134_devlist
) {
1282 h
= list_entry(list
, struct saa7134_dev
, devlist
);
1283 if (h
->video_dev
&& (h
->video_dev
->minor
== minor
))
1285 if (h
->radio_dev
&& (h
->radio_dev
->minor
== minor
)) {
1289 if (h
->vbi_dev
&& (h
->vbi_dev
->minor
== minor
)) {
1290 type
= V4L2_BUF_TYPE_VBI_CAPTURE
;
1297 dprintk("open minor=%d radio=%d type=%s\n",minor
,radio
,
1298 v4l2_type_names
[type
]);
1300 /* allocate + initialize per filehandle data */
1301 fh
= kzalloc(sizeof(*fh
),GFP_KERNEL
);
1304 file
->private_data
= fh
;
1308 fh
->fmt
= format_by_fourcc(V4L2_PIX_FMT_BGR24
);
1311 v4l2_prio_open(&dev
->prio
,&fh
->prio
);
1313 videobuf_queue_init(&fh
->cap
, &video_qops
,
1314 dev
->pci
, &dev
->slock
,
1315 V4L2_BUF_TYPE_VIDEO_CAPTURE
,
1316 V4L2_FIELD_INTERLACED
,
1317 sizeof(struct saa7134_buf
),
1319 videobuf_queue_init(&fh
->vbi
, &saa7134_vbi_qops
,
1320 dev
->pci
, &dev
->slock
,
1321 V4L2_BUF_TYPE_VBI_CAPTURE
,
1323 sizeof(struct saa7134_buf
),
1325 saa7134_pgtable_alloc(dev
->pci
,&fh
->pt_cap
);
1326 saa7134_pgtable_alloc(dev
->pci
,&fh
->pt_vbi
);
1329 /* switch to radio mode */
1330 saa7134_tvaudio_setinput(dev
,&card(dev
).radio
);
1331 saa7134_i2c_call_clients(dev
,AUDC_SET_RADIO
, NULL
);
1333 /* switch to video/vbi mode */
1334 video_mux(dev
,dev
->ctl_input
);
1340 video_read(struct file
*file
, char __user
*data
, size_t count
, loff_t
*ppos
)
1342 struct saa7134_fh
*fh
= file
->private_data
;
1345 case V4L2_BUF_TYPE_VIDEO_CAPTURE
:
1346 if (res_locked(fh
->dev
,RESOURCE_VIDEO
))
1348 return videobuf_read_one(saa7134_queue(fh
),
1350 file
->f_flags
& O_NONBLOCK
);
1351 case V4L2_BUF_TYPE_VBI_CAPTURE
:
1352 if (!res_get(fh
->dev
,fh
,RESOURCE_VBI
))
1354 return videobuf_read_stream(saa7134_queue(fh
),
1355 data
, count
, ppos
, 1,
1356 file
->f_flags
& O_NONBLOCK
);
1365 video_poll(struct file
*file
, struct poll_table_struct
*wait
)
1367 struct saa7134_fh
*fh
= file
->private_data
;
1368 struct videobuf_buffer
*buf
= NULL
;
1370 if (V4L2_BUF_TYPE_VBI_CAPTURE
== fh
->type
)
1371 return videobuf_poll_stream(file
, &fh
->vbi
, wait
);
1373 if (res_check(fh
,RESOURCE_VIDEO
)) {
1374 if (!list_empty(&fh
->cap
.stream
))
1375 buf
= list_entry(fh
->cap
.stream
.next
, struct videobuf_buffer
, stream
);
1377 mutex_lock(&fh
->cap
.lock
);
1378 if (UNSET
== fh
->cap
.read_off
) {
1379 /* need to capture a new frame */
1380 if (res_locked(fh
->dev
,RESOURCE_VIDEO
)) {
1381 mutex_unlock(&fh
->cap
.lock
);
1384 if (0 != fh
->cap
.ops
->buf_prepare(&fh
->cap
,fh
->cap
.read_buf
,fh
->cap
.field
)) {
1385 mutex_unlock(&fh
->cap
.lock
);
1388 fh
->cap
.ops
->buf_queue(&fh
->cap
,fh
->cap
.read_buf
);
1389 fh
->cap
.read_off
= 0;
1391 mutex_unlock(&fh
->cap
.lock
);
1392 buf
= fh
->cap
.read_buf
;
1398 poll_wait(file
, &buf
->done
, wait
);
1399 if (buf
->state
== STATE_DONE
||
1400 buf
->state
== STATE_ERROR
)
1401 return POLLIN
|POLLRDNORM
;
1405 static int video_release(struct inode
*inode
, struct file
*file
)
1407 struct saa7134_fh
*fh
= file
->private_data
;
1408 struct saa7134_dev
*dev
= fh
->dev
;
1409 unsigned long flags
;
1411 /* turn off overlay */
1412 if (res_check(fh
, RESOURCE_OVERLAY
)) {
1413 spin_lock_irqsave(&dev
->slock
,flags
);
1414 stop_preview(dev
,fh
);
1415 spin_unlock_irqrestore(&dev
->slock
,flags
);
1416 res_free(dev
,fh
,RESOURCE_OVERLAY
);
1419 /* stop video capture */
1420 if (res_check(fh
, RESOURCE_VIDEO
)) {
1421 videobuf_streamoff(&fh
->cap
);
1422 res_free(dev
,fh
,RESOURCE_VIDEO
);
1424 if (fh
->cap
.read_buf
) {
1425 buffer_release(&fh
->cap
,fh
->cap
.read_buf
);
1426 kfree(fh
->cap
.read_buf
);
1429 /* stop vbi capture */
1430 if (res_check(fh
, RESOURCE_VBI
)) {
1431 if (fh
->vbi
.streaming
)
1432 videobuf_streamoff(&fh
->vbi
);
1433 if (fh
->vbi
.reading
)
1434 videobuf_read_stop(&fh
->vbi
);
1435 res_free(dev
,fh
,RESOURCE_VBI
);
1438 /* ts-capture will not work in planar mode, so turn it off Hac: 04.05*/
1439 saa_andorb(SAA7134_OFMT_VIDEO_A
, 0x1f, 0);
1440 saa_andorb(SAA7134_OFMT_VIDEO_B
, 0x1f, 0);
1441 saa_andorb(SAA7134_OFMT_DATA_A
, 0x1f, 0);
1442 saa_andorb(SAA7134_OFMT_DATA_B
, 0x1f, 0);
1444 saa7134_i2c_call_clients(dev
, TUNER_SET_STANDBY
, NULL
);
1447 videobuf_mmap_free(&fh
->cap
);
1448 videobuf_mmap_free(&fh
->vbi
);
1449 saa7134_pgtable_free(dev
->pci
,&fh
->pt_cap
);
1450 saa7134_pgtable_free(dev
->pci
,&fh
->pt_vbi
);
1452 v4l2_prio_close(&dev
->prio
,&fh
->prio
);
1453 file
->private_data
= NULL
;
1458 static int video_mmap(struct file
*file
, struct vm_area_struct
* vma
)
1460 struct saa7134_fh
*fh
= file
->private_data
;
1462 return videobuf_mmap_mapper(saa7134_queue(fh
), vma
);
1465 /* ------------------------------------------------------------------ */
1467 static void saa7134_vbi_fmt(struct saa7134_dev
*dev
, struct v4l2_format
*f
)
1469 struct saa7134_tvnorm
*norm
= dev
->tvnorm
;
1471 f
->fmt
.vbi
.sampling_rate
= 6750000 * 4;
1472 f
->fmt
.vbi
.samples_per_line
= 2048 /* VBI_LINE_LENGTH */;
1473 f
->fmt
.vbi
.sample_format
= V4L2_PIX_FMT_GREY
;
1474 f
->fmt
.vbi
.offset
= 64 * 4;
1475 f
->fmt
.vbi
.start
[0] = norm
->vbi_v_start_0
;
1476 f
->fmt
.vbi
.count
[0] = norm
->vbi_v_stop_0
- norm
->vbi_v_start_0
+1;
1477 f
->fmt
.vbi
.start
[1] = norm
->vbi_v_start_1
;
1478 f
->fmt
.vbi
.count
[1] = f
->fmt
.vbi
.count
[0];
1479 f
->fmt
.vbi
.flags
= 0; /* VBI_UNSYNC VBI_INTERLACED */
1483 static int saa7134_g_fmt(struct saa7134_dev
*dev
, struct saa7134_fh
*fh
,
1484 struct v4l2_format
*f
)
1487 case V4L2_BUF_TYPE_VIDEO_CAPTURE
:
1488 memset(&f
->fmt
.pix
,0,sizeof(f
->fmt
.pix
));
1489 f
->fmt
.pix
.width
= fh
->width
;
1490 f
->fmt
.pix
.height
= fh
->height
;
1491 f
->fmt
.pix
.field
= fh
->cap
.field
;
1492 f
->fmt
.pix
.pixelformat
= fh
->fmt
->fourcc
;
1493 f
->fmt
.pix
.bytesperline
=
1494 (f
->fmt
.pix
.width
* fh
->fmt
->depth
) >> 3;
1495 f
->fmt
.pix
.sizeimage
=
1496 f
->fmt
.pix
.height
* f
->fmt
.pix
.bytesperline
;
1498 case V4L2_BUF_TYPE_VIDEO_OVERLAY
:
1499 if (saa7134_no_overlay
> 0) {
1500 printk ("V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
1503 f
->fmt
.win
= fh
->win
;
1505 case V4L2_BUF_TYPE_VBI_CAPTURE
:
1506 saa7134_vbi_fmt(dev
,f
);
1513 static int saa7134_try_fmt(struct saa7134_dev
*dev
, struct saa7134_fh
*fh
,
1514 struct v4l2_format
*f
)
1519 case V4L2_BUF_TYPE_VIDEO_CAPTURE
:
1521 struct saa7134_format
*fmt
;
1522 enum v4l2_field field
;
1523 unsigned int maxw
, maxh
;
1525 fmt
= format_by_fourcc(f
->fmt
.pix
.pixelformat
);
1529 field
= f
->fmt
.pix
.field
;
1530 maxw
= min(dev
->crop_current
.width
*4, dev
->crop_bounds
.width
);
1531 maxh
= min(dev
->crop_current
.height
*4, dev
->crop_bounds
.height
);
1533 if (V4L2_FIELD_ANY
== field
) {
1534 field
= (f
->fmt
.pix
.height
> maxh
/2)
1535 ? V4L2_FIELD_INTERLACED
1536 : V4L2_FIELD_BOTTOM
;
1539 case V4L2_FIELD_TOP
:
1540 case V4L2_FIELD_BOTTOM
:
1543 case V4L2_FIELD_INTERLACED
:
1549 f
->fmt
.pix
.field
= field
;
1550 if (f
->fmt
.pix
.width
< 48)
1551 f
->fmt
.pix
.width
= 48;
1552 if (f
->fmt
.pix
.height
< 32)
1553 f
->fmt
.pix
.height
= 32;
1554 if (f
->fmt
.pix
.width
> maxw
)
1555 f
->fmt
.pix
.width
= maxw
;
1556 if (f
->fmt
.pix
.height
> maxh
)
1557 f
->fmt
.pix
.height
= maxh
;
1558 f
->fmt
.pix
.width
&= ~0x03;
1559 f
->fmt
.pix
.bytesperline
=
1560 (f
->fmt
.pix
.width
* fmt
->depth
) >> 3;
1561 f
->fmt
.pix
.sizeimage
=
1562 f
->fmt
.pix
.height
* f
->fmt
.pix
.bytesperline
;
1566 case V4L2_BUF_TYPE_VIDEO_OVERLAY
:
1567 if (saa7134_no_overlay
> 0) {
1568 printk ("V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
1571 err
= verify_preview(dev
,&f
->fmt
.win
);
1575 case V4L2_BUF_TYPE_VBI_CAPTURE
:
1576 saa7134_vbi_fmt(dev
,f
);
1583 static int saa7134_s_fmt(struct saa7134_dev
*dev
, struct saa7134_fh
*fh
,
1584 struct v4l2_format
*f
)
1586 unsigned long flags
;
1590 case V4L2_BUF_TYPE_VIDEO_CAPTURE
:
1591 err
= saa7134_try_fmt(dev
,fh
,f
);
1595 fh
->fmt
= format_by_fourcc(f
->fmt
.pix
.pixelformat
);
1596 fh
->width
= f
->fmt
.pix
.width
;
1597 fh
->height
= f
->fmt
.pix
.height
;
1598 fh
->cap
.field
= f
->fmt
.pix
.field
;
1600 case V4L2_BUF_TYPE_VIDEO_OVERLAY
:
1601 if (saa7134_no_overlay
> 0) {
1602 printk ("V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
1605 err
= verify_preview(dev
,&f
->fmt
.win
);
1609 mutex_lock(&dev
->lock
);
1610 fh
->win
= f
->fmt
.win
;
1611 fh
->nclips
= f
->fmt
.win
.clipcount
;
1614 if (copy_from_user(fh
->clips
,f
->fmt
.win
.clips
,
1615 sizeof(struct v4l2_clip
)*fh
->nclips
)) {
1616 mutex_unlock(&dev
->lock
);
1620 if (res_check(fh
, RESOURCE_OVERLAY
)) {
1621 spin_lock_irqsave(&dev
->slock
,flags
);
1622 stop_preview(dev
,fh
);
1623 start_preview(dev
,fh
);
1624 spin_unlock_irqrestore(&dev
->slock
,flags
);
1626 mutex_unlock(&dev
->lock
);
1628 case V4L2_BUF_TYPE_VBI_CAPTURE
:
1629 saa7134_vbi_fmt(dev
,f
);
1636 int saa7134_common_ioctl(struct saa7134_dev
*dev
,
1637 unsigned int cmd
, void *arg
)
1642 case VIDIOC_QUERYCTRL
:
1644 const struct v4l2_queryctrl
*ctrl
;
1645 struct v4l2_queryctrl
*c
= arg
;
1647 if ((c
->id
< V4L2_CID_BASE
||
1648 c
->id
>= V4L2_CID_LASTP1
) &&
1649 (c
->id
< V4L2_CID_PRIVATE_BASE
||
1650 c
->id
>= V4L2_CID_PRIVATE_LASTP1
))
1652 ctrl
= ctrl_by_id(c
->id
);
1653 *c
= (NULL
!= ctrl
) ? *ctrl
: no_ctrl
;
1657 return get_control(dev
,arg
);
1660 mutex_lock(&dev
->lock
);
1661 err
= set_control(dev
,NULL
,arg
);
1662 mutex_unlock(&dev
->lock
);
1665 /* --- input switching --------------------------------------- */
1666 case VIDIOC_ENUMINPUT
:
1668 struct v4l2_input
*i
= arg
;
1672 if (n
>= SAA7134_INPUT_MAX
)
1674 if (NULL
== card_in(dev
,i
->index
).name
)
1676 memset(i
,0,sizeof(*i
));
1678 i
->type
= V4L2_INPUT_TYPE_CAMERA
;
1679 strcpy(i
->name
,card_in(dev
,n
).name
);
1680 if (card_in(dev
,n
).tv
)
1681 i
->type
= V4L2_INPUT_TYPE_TUNER
;
1683 if (n
== dev
->ctl_input
) {
1684 int v1
= saa_readb(SAA7134_STATUS_VIDEO1
);
1685 int v2
= saa_readb(SAA7134_STATUS_VIDEO2
);
1687 if (0 != (v1
& 0x40))
1688 i
->status
|= V4L2_IN_ST_NO_H_LOCK
;
1689 if (0 != (v2
& 0x40))
1690 i
->status
|= V4L2_IN_ST_NO_SYNC
;
1691 if (0 != (v2
& 0x0e))
1692 i
->status
|= V4L2_IN_ST_MACROVISION
;
1694 for (n
= 0; n
< TVNORMS
; n
++)
1695 i
->std
|= tvnorms
[n
].id
;
1698 case VIDIOC_G_INPUT
:
1701 *i
= dev
->ctl_input
;
1704 case VIDIOC_S_INPUT
:
1708 if (*i
< 0 || *i
>= SAA7134_INPUT_MAX
)
1710 if (NULL
== card_in(dev
,*i
).name
)
1712 mutex_lock(&dev
->lock
);
1714 mutex_unlock(&dev
->lock
);
1721 EXPORT_SYMBOL(saa7134_common_ioctl
);
1724 * This function is _not_ called directly, but from
1725 * video_generic_ioctl (and maybe others). userspace
1726 * copying is done already, arg is a kernel pointer.
1728 static int video_do_ioctl(struct inode
*inode
, struct file
*file
,
1729 unsigned int cmd
, void *arg
)
1731 struct saa7134_fh
*fh
= file
->private_data
;
1732 struct saa7134_dev
*dev
= fh
->dev
;
1733 unsigned long flags
;
1736 if (video_debug
> 1)
1737 v4l_print_ioctl(dev
->name
,cmd
);
1742 case VIDIOC_S_INPUT
:
1743 case VIDIOC_S_TUNER
:
1744 case VIDIOC_S_FREQUENCY
:
1745 err
= v4l2_prio_check(&dev
->prio
,&fh
->prio
);
1751 case VIDIOC_QUERYCAP
:
1753 struct v4l2_capability
*cap
= arg
;
1754 unsigned int tuner_type
= dev
->tuner_type
;
1756 memset(cap
,0,sizeof(*cap
));
1757 strcpy(cap
->driver
, "saa7134");
1758 strlcpy(cap
->card
, saa7134_boards
[dev
->board
].name
,
1760 sprintf(cap
->bus_info
,"PCI:%s",pci_name(dev
->pci
));
1761 cap
->version
= SAA7134_VERSION_CODE
;
1763 V4L2_CAP_VIDEO_CAPTURE
|
1764 V4L2_CAP_VBI_CAPTURE
|
1765 V4L2_CAP_READWRITE
|
1766 V4L2_CAP_STREAMING
|
1768 if (saa7134_no_overlay
<= 0) {
1769 cap
->capabilities
|= V4L2_CAP_VIDEO_OVERLAY
;
1772 if ((tuner_type
== TUNER_ABSENT
) || (tuner_type
== UNSET
))
1773 cap
->capabilities
&= ~V4L2_CAP_TUNER
;
1778 /* --- tv standards ------------------------------------------ */
1779 case VIDIOC_ENUMSTD
:
1781 struct v4l2_standard
*e
= arg
;
1787 err
= v4l2_video_std_construct(e
, tvnorms
[e
->index
].id
,
1788 tvnorms
[e
->index
].name
);
1796 v4l2_std_id
*id
= arg
;
1798 *id
= dev
->tvnorm
->id
;
1803 v4l2_std_id
*id
= arg
;
1807 for (i
= 0; i
< TVNORMS
; i
++)
1808 if (*id
== tvnorms
[i
].id
)
1811 for (i
= 0; i
< TVNORMS
; i
++)
1812 if (*id
& tvnorms
[i
].id
)
1816 if ((*id
& V4L2_STD_SECAM
) && (secam
[0] != '-')) {
1817 if (secam
[0] == 'L' || secam
[0] == 'l') {
1818 if (secam
[1] == 'C' || secam
[1] == 'c')
1819 fixup
= V4L2_STD_SECAM_LC
;
1821 fixup
= V4L2_STD_SECAM_L
;
1823 if (secam
[0] == 'D' || secam
[0] == 'd')
1824 fixup
= V4L2_STD_SECAM_DK
;
1826 fixup
= V4L2_STD_SECAM
;
1828 for (i
= 0; i
< TVNORMS
; i
++)
1829 if (fixup
== tvnorms
[i
].id
)
1832 mutex_lock(&dev
->lock
);
1833 if (res_check(fh
, RESOURCE_OVERLAY
)) {
1834 spin_lock_irqsave(&dev
->slock
,flags
);
1835 stop_preview(dev
,fh
);
1836 set_tvnorm(dev
,&tvnorms
[i
]);
1837 start_preview(dev
,fh
);
1838 spin_unlock_irqrestore(&dev
->slock
,flags
);
1840 set_tvnorm(dev
,&tvnorms
[i
]);
1841 saa7134_tvaudio_do_scan(dev
);
1842 mutex_unlock(&dev
->lock
);
1846 case VIDIOC_CROPCAP
:
1848 struct v4l2_cropcap
*cap
= arg
;
1850 if (cap
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
&&
1851 cap
->type
!= V4L2_BUF_TYPE_VIDEO_OVERLAY
)
1853 cap
->bounds
= dev
->crop_bounds
;
1854 cap
->defrect
= dev
->crop_defrect
;
1855 cap
->pixelaspect
.numerator
= 1;
1856 cap
->pixelaspect
.denominator
= 1;
1857 if (dev
->tvnorm
->id
& V4L2_STD_525_60
) {
1858 cap
->pixelaspect
.numerator
= 11;
1859 cap
->pixelaspect
.denominator
= 10;
1861 if (dev
->tvnorm
->id
& V4L2_STD_625_50
) {
1862 cap
->pixelaspect
.numerator
= 54;
1863 cap
->pixelaspect
.denominator
= 59;
1870 struct v4l2_crop
* crop
= arg
;
1872 if (crop
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
&&
1873 crop
->type
!= V4L2_BUF_TYPE_VIDEO_OVERLAY
)
1875 crop
->c
= dev
->crop_current
;
1880 struct v4l2_crop
*crop
= arg
;
1881 struct v4l2_rect
*b
= &dev
->crop_bounds
;
1883 if (crop
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
&&
1884 crop
->type
!= V4L2_BUF_TYPE_VIDEO_OVERLAY
)
1886 if (crop
->c
.height
< 0)
1888 if (crop
->c
.width
< 0)
1891 if (res_locked(fh
->dev
,RESOURCE_OVERLAY
))
1893 if (res_locked(fh
->dev
,RESOURCE_VIDEO
))
1896 if (crop
->c
.top
< b
->top
)
1897 crop
->c
.top
= b
->top
;
1898 if (crop
->c
.top
> b
->top
+ b
->height
)
1899 crop
->c
.top
= b
->top
+ b
->height
;
1900 if (crop
->c
.height
> b
->top
- crop
->c
.top
+ b
->height
)
1901 crop
->c
.height
= b
->top
- crop
->c
.top
+ b
->height
;
1903 if (crop
->c
.left
< b
->left
)
1904 crop
->c
.left
= b
->left
;
1905 if (crop
->c
.left
> b
->left
+ b
->width
)
1906 crop
->c
.left
= b
->left
+ b
->width
;
1907 if (crop
->c
.width
> b
->left
- crop
->c
.left
+ b
->width
)
1908 crop
->c
.width
= b
->left
- crop
->c
.left
+ b
->width
;
1910 dev
->crop_current
= crop
->c
;
1914 /* --- tuner ioctls ------------------------------------------ */
1915 case VIDIOC_G_TUNER
:
1917 struct v4l2_tuner
*t
= arg
;
1922 memset(t
,0,sizeof(*t
));
1923 for (n
= 0; n
< SAA7134_INPUT_MAX
; n
++)
1924 if (card_in(dev
,n
).tv
)
1926 if (NULL
!= card_in(dev
,n
).name
) {
1927 strcpy(t
->name
, "Television");
1928 t
->type
= V4L2_TUNER_ANALOG_TV
;
1929 t
->capability
= V4L2_TUNER_CAP_NORM
|
1930 V4L2_TUNER_CAP_STEREO
|
1931 V4L2_TUNER_CAP_LANG1
|
1932 V4L2_TUNER_CAP_LANG2
;
1933 t
->rangehigh
= 0xffffffffUL
;
1934 t
->rxsubchans
= saa7134_tvaudio_getstereo(dev
);
1935 t
->audmode
= saa7134_tvaudio_rx2mode(t
->rxsubchans
);
1937 if (0 != (saa_readb(SAA7134_STATUS_VIDEO1
) & 0x03))
1941 case VIDIOC_S_TUNER
:
1943 struct v4l2_tuner
*t
= arg
;
1946 mode
= dev
->thread
.mode
;
1947 if (UNSET
== mode
) {
1948 rx
= saa7134_tvaudio_getstereo(dev
);
1949 mode
= saa7134_tvaudio_rx2mode(t
->rxsubchans
);
1951 if (mode
!= t
->audmode
) {
1952 dev
->thread
.mode
= t
->audmode
;
1956 case VIDIOC_G_FREQUENCY
:
1958 struct v4l2_frequency
*f
= arg
;
1960 memset(f
,0,sizeof(*f
));
1961 f
->type
= fh
->radio
? V4L2_TUNER_RADIO
: V4L2_TUNER_ANALOG_TV
;
1962 f
->frequency
= dev
->ctl_freq
;
1965 case VIDIOC_S_FREQUENCY
:
1967 struct v4l2_frequency
*f
= arg
;
1971 if (0 == fh
->radio
&& V4L2_TUNER_ANALOG_TV
!= f
->type
)
1973 if (1 == fh
->radio
&& V4L2_TUNER_RADIO
!= f
->type
)
1975 mutex_lock(&dev
->lock
);
1976 dev
->ctl_freq
= f
->frequency
;
1978 saa7134_i2c_call_clients(dev
,VIDIOC_S_FREQUENCY
,f
);
1980 saa7134_tvaudio_do_scan(dev
);
1981 mutex_unlock(&dev
->lock
);
1985 /* --- control ioctls ---------------------------------------- */
1986 case VIDIOC_ENUMINPUT
:
1987 case VIDIOC_G_INPUT
:
1988 case VIDIOC_S_INPUT
:
1989 case VIDIOC_QUERYCTRL
:
1992 return saa7134_common_ioctl(dev
, cmd
, arg
);
1994 case VIDIOC_G_AUDIO
:
1996 struct v4l2_audio
*a
= arg
;
1998 memset(a
,0,sizeof(*a
));
1999 strcpy(a
->name
,"audio");
2002 case VIDIOC_S_AUDIO
:
2006 struct v4l2_captureparm
*parm
= arg
;
2007 memset(parm
,0,sizeof(*parm
));
2011 case VIDIOC_G_PRIORITY
:
2013 enum v4l2_priority
*p
= arg
;
2015 *p
= v4l2_prio_max(&dev
->prio
);
2018 case VIDIOC_S_PRIORITY
:
2020 enum v4l2_priority
*prio
= arg
;
2022 return v4l2_prio_change(&dev
->prio
, &fh
->prio
, *prio
);
2025 /* --- preview ioctls ---------------------------------------- */
2026 case VIDIOC_ENUM_FMT
:
2028 struct v4l2_fmtdesc
*f
= arg
;
2029 enum v4l2_buf_type type
;
2035 case V4L2_BUF_TYPE_VIDEO_CAPTURE
:
2036 case V4L2_BUF_TYPE_VIDEO_OVERLAY
:
2037 if (saa7134_no_overlay
> 0) {
2038 printk ("V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
2041 if (index
>= FORMATS
)
2043 if (f
->type
== V4L2_BUF_TYPE_VIDEO_OVERLAY
&&
2044 formats
[index
].planar
)
2046 memset(f
,0,sizeof(*f
));
2049 strlcpy(f
->description
,formats
[index
].name
,sizeof(f
->description
));
2050 f
->pixelformat
= formats
[index
].fourcc
;
2052 case V4L2_BUF_TYPE_VBI_CAPTURE
:
2055 memset(f
,0,sizeof(*f
));
2058 f
->pixelformat
= V4L2_PIX_FMT_GREY
;
2059 strcpy(f
->description
,"vbi data");
2068 struct v4l2_framebuffer
*fb
= arg
;
2071 fb
->capability
= V4L2_FBUF_CAP_LIST_CLIPPING
;
2076 struct v4l2_framebuffer
*fb
= arg
;
2077 struct saa7134_format
*fmt
;
2079 if(!capable(CAP_SYS_ADMIN
) &&
2080 !capable(CAP_SYS_RAWIO
))
2084 fmt
= format_by_fourcc(fb
->fmt
.pixelformat
);
2091 if (0 == dev
->ovbuf
.fmt
.bytesperline
)
2092 dev
->ovbuf
.fmt
.bytesperline
=
2093 dev
->ovbuf
.fmt
.width
*fmt
->depth
/8;
2096 case VIDIOC_OVERLAY
:
2101 if (saa7134_no_overlay
> 0) {
2102 printk ("no_overlay\n");
2106 if (!res_get(dev
,fh
,RESOURCE_OVERLAY
))
2108 spin_lock_irqsave(&dev
->slock
,flags
);
2109 start_preview(dev
,fh
);
2110 spin_unlock_irqrestore(&dev
->slock
,flags
);
2113 if (!res_check(fh
, RESOURCE_OVERLAY
))
2115 spin_lock_irqsave(&dev
->slock
,flags
);
2116 stop_preview(dev
,fh
);
2117 spin_unlock_irqrestore(&dev
->slock
,flags
);
2118 res_free(dev
,fh
,RESOURCE_OVERLAY
);
2123 /* --- capture ioctls ---------------------------------------- */
2126 struct v4l2_format
*f
= arg
;
2127 return saa7134_g_fmt(dev
,fh
,f
);
2131 struct v4l2_format
*f
= arg
;
2132 return saa7134_s_fmt(dev
,fh
,f
);
2134 case VIDIOC_TRY_FMT
:
2136 struct v4l2_format
*f
= arg
;
2137 return saa7134_try_fmt(dev
,fh
,f
);
2139 #ifdef CONFIG_VIDEO_V4L1_COMPAT
2142 struct video_mbuf
*mbuf
= arg
;
2143 struct videobuf_queue
*q
;
2144 struct v4l2_requestbuffers req
;
2147 q
= saa7134_queue(fh
);
2148 memset(&req
,0,sizeof(req
));
2150 req
.count
= gbuffers
;
2151 req
.memory
= V4L2_MEMORY_MMAP
;
2152 err
= videobuf_reqbufs(q
,&req
);
2155 memset(mbuf
,0,sizeof(*mbuf
));
2156 mbuf
->frames
= req
.count
;
2158 for (i
= 0; i
< mbuf
->frames
; i
++) {
2159 mbuf
->offsets
[i
] = q
->bufs
[i
]->boff
;
2160 mbuf
->size
+= q
->bufs
[i
]->bsize
;
2165 case VIDIOC_REQBUFS
:
2166 return videobuf_reqbufs(saa7134_queue(fh
),arg
);
2168 case VIDIOC_QUERYBUF
:
2169 return videobuf_querybuf(saa7134_queue(fh
),arg
);
2172 return videobuf_qbuf(saa7134_queue(fh
),arg
);
2175 return videobuf_dqbuf(saa7134_queue(fh
),arg
,
2176 file
->f_flags
& O_NONBLOCK
);
2178 case VIDIOC_STREAMON
:
2180 int res
= saa7134_resource(fh
);
2182 if (!res_get(dev
,fh
,res
))
2184 return videobuf_streamon(saa7134_queue(fh
));
2186 case VIDIOC_STREAMOFF
:
2188 int res
= saa7134_resource(fh
);
2190 err
= videobuf_streamoff(saa7134_queue(fh
));
2193 res_free(dev
,fh
,res
);
2198 return v4l_compat_translate_ioctl(inode
,file
,cmd
,arg
,
2204 static int video_ioctl(struct inode
*inode
, struct file
*file
,
2205 unsigned int cmd
, unsigned long arg
)
2207 return video_usercopy(inode
, file
, cmd
, arg
, video_do_ioctl
);
2210 static int radio_do_ioctl(struct inode
*inode
, struct file
*file
,
2211 unsigned int cmd
, void *arg
)
2213 struct saa7134_fh
*fh
= file
->private_data
;
2214 struct saa7134_dev
*dev
= fh
->dev
;
2216 if (video_debug
> 1)
2217 v4l_print_ioctl(dev
->name
,cmd
);
2219 case VIDIOC_QUERYCAP
:
2221 struct v4l2_capability
*cap
= arg
;
2223 memset(cap
,0,sizeof(*cap
));
2224 strcpy(cap
->driver
, "saa7134");
2225 strlcpy(cap
->card
, saa7134_boards
[dev
->board
].name
,
2227 sprintf(cap
->bus_info
,"PCI:%s",pci_name(dev
->pci
));
2228 cap
->version
= SAA7134_VERSION_CODE
;
2229 cap
->capabilities
= V4L2_CAP_TUNER
;
2232 case VIDIOC_G_TUNER
:
2234 struct v4l2_tuner
*t
= arg
;
2239 memset(t
,0,sizeof(*t
));
2240 strcpy(t
->name
, "Radio");
2241 t
->type
= V4L2_TUNER_RADIO
;
2243 saa7134_i2c_call_clients(dev
, VIDIOC_G_TUNER
, t
);
2244 if (dev
->input
->amux
== TV
) {
2245 t
->signal
= 0xf800 - ((saa_readb(0x581) & 0x1f) << 11);
2246 t
->rxsubchans
= (saa_readb(0x529) & 0x08) ?
2247 V4L2_TUNER_SUB_STEREO
: V4L2_TUNER_SUB_MONO
;
2251 case VIDIOC_S_TUNER
:
2253 struct v4l2_tuner
*t
= arg
;
2258 saa7134_i2c_call_clients(dev
,VIDIOC_S_TUNER
,t
);
2262 case VIDIOC_ENUMINPUT
:
2264 struct v4l2_input
*i
= arg
;
2268 strcpy(i
->name
,"Radio");
2269 i
->type
= V4L2_INPUT_TYPE_TUNER
;
2272 case VIDIOC_G_INPUT
:
2278 case VIDIOC_G_AUDIO
:
2280 struct v4l2_audio
*a
= arg
;
2282 memset(a
,0,sizeof(*a
));
2283 strcpy(a
->name
,"Radio");
2288 v4l2_std_id
*id
= arg
;
2292 case VIDIOC_S_AUDIO
:
2293 case VIDIOC_S_INPUT
:
2297 case VIDIOC_QUERYCTRL
:
2299 const struct v4l2_queryctrl
*ctrl
;
2300 struct v4l2_queryctrl
*c
= arg
;
2302 if (c
->id
< V4L2_CID_BASE
||
2303 c
->id
>= V4L2_CID_LASTP1
)
2305 if (c
->id
== V4L2_CID_AUDIO_MUTE
) {
2306 ctrl
= ctrl_by_id(c
->id
);
2315 case VIDIOC_G_FREQUENCY
:
2316 case VIDIOC_S_FREQUENCY
:
2317 return video_do_ioctl(inode
,file
,cmd
,arg
);
2320 return v4l_compat_translate_ioctl(inode
,file
,cmd
,arg
,
2326 static int radio_ioctl(struct inode
*inode
, struct file
*file
,
2327 unsigned int cmd
, unsigned long arg
)
2329 return video_usercopy(inode
, file
, cmd
, arg
, radio_do_ioctl
);
2332 static const struct file_operations video_fops
=
2334 .owner
= THIS_MODULE
,
2336 .release
= video_release
,
2340 .ioctl
= video_ioctl
,
2341 .compat_ioctl
= v4l_compat_ioctl32
,
2342 .llseek
= no_llseek
,
2345 static const struct file_operations radio_fops
=
2347 .owner
= THIS_MODULE
,
2349 .release
= video_release
,
2350 .ioctl
= radio_ioctl
,
2351 .compat_ioctl
= v4l_compat_ioctl32
,
2352 .llseek
= no_llseek
,
2355 /* ----------------------------------------------------------- */
2356 /* exported stuff */
2358 struct video_device saa7134_video_template
=
2360 .name
= "saa7134-video",
2361 .type
= VID_TYPE_CAPTURE
|VID_TYPE_TUNER
|
2362 VID_TYPE_CLIPPING
|VID_TYPE_SCALES
,
2364 .fops
= &video_fops
,
2368 struct video_device saa7134_vbi_template
=
2370 .name
= "saa7134-vbi",
2371 .type
= VID_TYPE_TUNER
|VID_TYPE_TELETEXT
,
2373 .fops
= &video_fops
,
2377 struct video_device saa7134_radio_template
=
2379 .name
= "saa7134-radio",
2380 .type
= VID_TYPE_TUNER
,
2382 .fops
= &radio_fops
,
2386 int saa7134_video_init1(struct saa7134_dev
*dev
)
2388 /* sanitycheck insmod options */
2389 if (gbuffers
< 2 || gbuffers
> VIDEO_MAX_FRAME
)
2391 if (gbufsize
< 0 || gbufsize
> gbufsize_max
)
2392 gbufsize
= gbufsize_max
;
2393 gbufsize
= (gbufsize
+ PAGE_SIZE
- 1) & PAGE_MASK
;
2395 /* put some sensible defaults into the data structures ... */
2396 dev
->ctl_bright
= ctrl_by_id(V4L2_CID_BRIGHTNESS
)->default_value
;
2397 dev
->ctl_contrast
= ctrl_by_id(V4L2_CID_CONTRAST
)->default_value
;
2398 dev
->ctl_hue
= ctrl_by_id(V4L2_CID_HUE
)->default_value
;
2399 dev
->ctl_saturation
= ctrl_by_id(V4L2_CID_SATURATION
)->default_value
;
2400 dev
->ctl_volume
= ctrl_by_id(V4L2_CID_AUDIO_VOLUME
)->default_value
;
2401 dev
->ctl_mute
= 1; // ctrl_by_id(V4L2_CID_AUDIO_MUTE)->default_value;
2402 dev
->ctl_invert
= ctrl_by_id(V4L2_CID_PRIVATE_INVERT
)->default_value
;
2403 dev
->ctl_automute
= ctrl_by_id(V4L2_CID_PRIVATE_AUTOMUTE
)->default_value
;
2405 if (dev
->tda9887_conf
&& dev
->ctl_automute
)
2406 dev
->tda9887_conf
|= TDA9887_AUTOMUTE
;
2409 INIT_LIST_HEAD(&dev
->video_q
.queue
);
2410 init_timer(&dev
->video_q
.timeout
);
2411 dev
->video_q
.timeout
.function
= saa7134_buffer_timeout
;
2412 dev
->video_q
.timeout
.data
= (unsigned long)(&dev
->video_q
);
2413 dev
->video_q
.dev
= dev
;
2415 if (saa7134_boards
[dev
->board
].video_out
) {
2416 /* enable video output */
2417 int vo
= saa7134_boards
[dev
->board
].video_out
;
2419 unsigned int vid_port_opts
= saa7134_boards
[dev
->board
].vid_port_opts
;
2420 saa_writeb(SAA7134_VIDEO_PORT_CTRL0
, video_out
[vo
][0]);
2421 video_reg
= video_out
[vo
][1];
2422 if (vid_port_opts
& SET_T_CODE_POLARITY_NON_INVERTED
)
2423 video_reg
&= ~VP_T_CODE_P_INVERTED
;
2424 saa_writeb(SAA7134_VIDEO_PORT_CTRL1
, video_reg
);
2425 saa_writeb(SAA7134_VIDEO_PORT_CTRL2
, video_out
[vo
][2]);
2426 saa_writeb(SAA7134_VIDEO_PORT_CTRL3
, video_out
[vo
][3]);
2427 saa_writeb(SAA7134_VIDEO_PORT_CTRL4
, video_out
[vo
][4]);
2428 video_reg
= video_out
[vo
][5];
2429 if (vid_port_opts
& SET_CLOCK_NOT_DELAYED
)
2430 video_reg
&= ~VP_CLK_CTRL2_DELAYED
;
2431 if (vid_port_opts
& SET_CLOCK_INVERTED
)
2432 video_reg
|= VP_CLK_CTRL1_INVERTED
;
2433 saa_writeb(SAA7134_VIDEO_PORT_CTRL5
, video_reg
);
2434 video_reg
= video_out
[vo
][6];
2435 if (vid_port_opts
& SET_VSYNC_OFF
) {
2436 video_reg
&= ~VP_VS_TYPE_MASK
;
2437 video_reg
|= VP_VS_TYPE_OFF
;
2439 saa_writeb(SAA7134_VIDEO_PORT_CTRL6
, video_reg
);
2440 saa_writeb(SAA7134_VIDEO_PORT_CTRL7
, video_out
[vo
][7]);
2441 saa_writeb(SAA7134_VIDEO_PORT_CTRL8
, video_out
[vo
][8]);
2447 int saa7134_video_init2(struct saa7134_dev
*dev
)
2450 set_tvnorm(dev
,&tvnorms
[0]);
2452 saa7134_tvaudio_setmute(dev
);
2453 saa7134_tvaudio_setvolume(dev
,dev
->ctl_volume
);
2457 void saa7134_irq_video_intl(struct saa7134_dev
*dev
)
2459 static const char *st
[] = {
2460 "(no signal)", "NTSC", "PAL", "SECAM" };
2463 st1
= saa_readb(SAA7134_STATUS_VIDEO1
);
2464 st2
= saa_readb(SAA7134_STATUS_VIDEO2
);
2465 dprintk("DCSDT: pll: %s, sync: %s, norm: %s\n",
2466 (st1
& 0x40) ? "not locked" : "locked",
2467 (st2
& 0x40) ? "no" : "yes",
2469 dev
->nosignal
= (st1
& 0x40) || (st2
& 0x40);
2471 if (dev
->nosignal
) {
2472 /* no video signal -> mute audio */
2473 if (dev
->ctl_automute
)
2475 saa7134_tvaudio_setmute(dev
);
2476 saa_setb(SAA7134_SYNC_CTRL
, 0x20);
2478 /* wake up tvaudio audio carrier scan thread */
2479 saa7134_tvaudio_do_scan(dev
);
2481 saa_clearb(SAA7134_SYNC_CTRL
, 0x20);
2483 if (dev
->mops
&& dev
->mops
->signal_change
)
2484 dev
->mops
->signal_change(dev
);
2487 void saa7134_irq_video_done(struct saa7134_dev
*dev
, unsigned long status
)
2489 enum v4l2_field field
;
2491 spin_lock(&dev
->slock
);
2492 if (dev
->video_q
.curr
) {
2493 dev
->video_fieldcount
++;
2494 field
= dev
->video_q
.curr
->vb
.field
;
2495 if (V4L2_FIELD_HAS_BOTH(field
)) {
2496 /* make sure we have seen both fields */
2497 if ((status
& 0x10) == 0x00) {
2498 dev
->video_q
.curr
->top_seen
= 1;
2501 if (!dev
->video_q
.curr
->top_seen
)
2503 } else if (field
== V4L2_FIELD_TOP
) {
2504 if ((status
& 0x10) != 0x10)
2506 } else if (field
== V4L2_FIELD_BOTTOM
) {
2507 if ((status
& 0x10) != 0x00)
2510 dev
->video_q
.curr
->vb
.field_count
= dev
->video_fieldcount
;
2511 saa7134_buffer_finish(dev
,&dev
->video_q
,STATE_DONE
);
2513 saa7134_buffer_next(dev
,&dev
->video_q
);
2516 spin_unlock(&dev
->slock
);
2519 /* ----------------------------------------------------------- */