3 Copyright (C) 2003-2004 Kevin Thayer <nufan_wfk at yahoo.com>
4 Copyright (C) 2005-2007 Hans Verkuil <hverkuil@xs4all.nl>
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include "ivtv-driver.h"
22 #include "ivtv-version.h"
23 #include "ivtv-mailbox.h"
25 #include "ivtv-queue.h"
26 #include "ivtv-fileops.h"
28 #include "ivtv-audio.h"
29 #include "ivtv-video.h"
30 #include "ivtv-streams.h"
32 #include "ivtv-ioctl.h"
33 #include "ivtv-gpio.h"
34 #include "ivtv-controls.h"
35 #include "ivtv-cards.h"
36 #include <media/saa7127.h>
37 #include <media/tveeprom.h>
38 #include <media/v4l2-chip-ident.h>
39 #include <linux/dvb/audio.h>
40 #include <linux/i2c-id.h>
42 u16
service2vbi(int type
)
45 case V4L2_SLICED_TELETEXT_B
:
46 return IVTV_SLICED_TYPE_TELETEXT_B
;
47 case V4L2_SLICED_CAPTION_525
:
48 return IVTV_SLICED_TYPE_CAPTION_525
;
49 case V4L2_SLICED_WSS_625
:
50 return IVTV_SLICED_TYPE_WSS_625
;
52 return IVTV_SLICED_TYPE_VPS
;
58 static int valid_service_line(int field
, int line
, int is_pal
)
60 return (is_pal
&& line
>= 6 && (line
!= 23 || field
== 0)) ||
61 (!is_pal
&& line
>= 10 && line
< 22);
64 static u16
select_service_from_set(int field
, int line
, u16 set
, int is_pal
)
66 u16 valid_set
= (is_pal
? V4L2_SLICED_VBI_625
: V4L2_SLICED_VBI_525
);
69 set
= set
& valid_set
;
70 if (set
== 0 || !valid_service_line(field
, line
, is_pal
)) {
74 if (line
== 21 && (set
& V4L2_SLICED_CAPTION_525
))
75 return V4L2_SLICED_CAPTION_525
;
78 if (line
== 16 && field
== 0 && (set
& V4L2_SLICED_VPS
))
79 return V4L2_SLICED_VPS
;
80 if (line
== 23 && field
== 0 && (set
& V4L2_SLICED_WSS_625
))
81 return V4L2_SLICED_WSS_625
;
85 for (i
= 0; i
< 32; i
++) {
92 void expand_service_set(struct v4l2_sliced_vbi_format
*fmt
, int is_pal
)
94 u16 set
= fmt
->service_set
;
98 for (f
= 0; f
< 2; f
++) {
99 for (l
= 0; l
< 24; l
++) {
100 fmt
->service_lines
[f
][l
] = select_service_from_set(f
, l
, set
, is_pal
);
105 static int check_service_set(struct v4l2_sliced_vbi_format
*fmt
, int is_pal
)
110 for (f
= 0; f
< 2; f
++) {
111 for (l
= 0; l
< 24; l
++) {
112 fmt
->service_lines
[f
][l
] = select_service_from_set(f
, l
, fmt
->service_lines
[f
][l
], is_pal
);
113 set
|= fmt
->service_lines
[f
][l
];
119 u16
get_service_set(struct v4l2_sliced_vbi_format
*fmt
)
124 for (f
= 0; f
< 2; f
++) {
125 for (l
= 0; l
< 24; l
++) {
126 set
|= fmt
->service_lines
[f
][l
];
132 static const struct {
136 { V4L2_STD_PAL_BG
| V4L2_STD_PAL_H
, "PAL-BGH" },
137 { V4L2_STD_PAL_DK
, "PAL-DK" },
138 { V4L2_STD_PAL_I
, "PAL-I" },
139 { V4L2_STD_PAL_M
, "PAL-M" },
140 { V4L2_STD_PAL_N
, "PAL-N" },
141 { V4L2_STD_PAL_Nc
, "PAL-Nc" },
142 { V4L2_STD_SECAM_B
| V4L2_STD_SECAM_G
| V4L2_STD_SECAM_H
, "SECAM-BGH" },
143 { V4L2_STD_SECAM_DK
, "SECAM-DK" },
144 { V4L2_STD_SECAM_L
, "SECAM-L" },
145 { V4L2_STD_SECAM_LC
, "SECAM-L'" },
146 { V4L2_STD_NTSC_M
, "NTSC-M" },
147 { V4L2_STD_NTSC_M_JP
, "NTSC-J" },
148 { V4L2_STD_NTSC_M_KR
, "NTSC-K" },
151 static const struct v4l2_standard ivtv_std_60hz
=
153 .frameperiod
= {.numerator
= 1001, .denominator
= 30000},
157 static const struct v4l2_standard ivtv_std_50hz
=
159 .frameperiod
= {.numerator
= 1, .denominator
= 25},
163 void ivtv_set_osd_alpha(struct ivtv
*itv
)
165 ivtv_vapi(itv
, CX2341X_OSD_SET_GLOBAL_ALPHA
, 3,
166 itv
->osd_global_alpha_state
, itv
->osd_global_alpha
, !itv
->osd_local_alpha_state
);
167 ivtv_vapi(itv
, CX2341X_OSD_SET_CHROMA_KEY
, 2, itv
->osd_color_key_state
, itv
->osd_color_key
);
170 int ivtv_set_speed(struct ivtv
*itv
, int speed
)
172 u32 data
[CX2341X_MBOX_MAX_DATA
];
173 struct ivtv_stream
*s
;
174 int single_step
= (speed
== 1 || speed
== -1);
177 if (speed
== 0) speed
= 1000;
180 if (speed
== itv
->speed
&& !single_step
)
183 s
= &itv
->streams
[IVTV_DEC_STREAM_TYPE_MPG
];
185 if (single_step
&& (speed
< 0) == (itv
->speed
< 0)) {
186 /* Single step video and no need to change direction */
187 ivtv_vapi(itv
, CX2341X_DEC_STEP_VIDEO
, 1, 0);
192 /* Need to change direction */
193 speed
= speed
< 0 ? -1000 : 1000;
195 data
[0] = (speed
> 1000 || speed
< -1000) ? 0x80000000 : 0;
196 data
[0] |= (speed
> 1000 || speed
< -1500) ? 0x40000000 : 0;
197 data
[1] = (speed
< 0);
198 data
[2] = speed
< 0 ? 3 : 7;
199 data
[3] = itv
->params
.video_b_frames
;
200 data
[4] = (speed
== 1500 || speed
== 500) ? itv
->speed_mute_audio
: 0;
204 if (speed
== 1500 || speed
== -1500) data
[0] |= 1;
205 else if (speed
== 2000 || speed
== -2000) data
[0] |= 2;
206 else if (speed
> -1000 && speed
< 0) data
[0] |= (-1000 / speed
);
207 else if (speed
< 1000 && speed
> 0) data
[0] |= (1000 / speed
);
209 /* If not decoding, just change speed setting */
210 if (atomic_read(&itv
->decoding
) > 0) {
213 /* Stop all DMA and decoding activity */
214 ivtv_vapi(itv
, CX2341X_DEC_PAUSE_PLAYBACK
, 1, 0);
216 /* Wait for any DMA to finish */
217 prepare_to_wait(&itv
->dma_waitq
, &wait
, TASK_INTERRUPTIBLE
);
218 while (itv
->i_flags
& IVTV_F_I_DMA
) {
219 got_sig
= signal_pending(current
);
225 finish_wait(&itv
->dma_waitq
, &wait
);
229 /* Change Speed safely */
230 ivtv_api(itv
, CX2341X_DEC_SET_PLAYBACK_SPEED
, 7, data
);
231 IVTV_DEBUG_INFO("Setting Speed to 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x\n",
232 data
[0], data
[1], data
[2], data
[3], data
[4], data
[5], data
[6]);
235 speed
= (speed
< 0) ? -1 : 1;
236 ivtv_vapi(itv
, CX2341X_DEC_STEP_VIDEO
, 1, 0);
242 static int ivtv_validate_speed(int cur_speed
, int new_speed
)
244 int fact
= new_speed
< 0 ? -1 : 1;
247 if (new_speed
< 0) new_speed
= -new_speed
;
248 if (cur_speed
< 0) cur_speed
= -cur_speed
;
250 if (cur_speed
<= new_speed
) {
251 if (new_speed
> 1500) return fact
* 2000;
252 if (new_speed
> 1000) return fact
* 1500;
255 if (new_speed
>= 2000) return fact
* 2000;
256 if (new_speed
>= 1500) return fact
* 1500;
257 if (new_speed
>= 1000) return fact
* 1000;
259 if (new_speed
== 0) return 1000;
260 if (new_speed
== 1 || new_speed
== 1000) return fact
* new_speed
;
263 new_speed
= 1000 / new_speed
;
264 if (1000 / cur_speed
== new_speed
)
265 new_speed
+= (cur_speed
< s
) ? -1 : 1;
266 if (new_speed
> 60) return 1000 / (fact
* 60);
267 return 1000 / (fact
* new_speed
);
270 static int ivtv_video_command(struct ivtv
*itv
, struct ivtv_open_id
*id
,
271 struct video_command
*vc
, int try)
273 struct ivtv_stream
*s
= &itv
->streams
[IVTV_DEC_STREAM_TYPE_MPG
];
275 if (!(itv
->v4l2_cap
& V4L2_CAP_VIDEO_OUTPUT
))
279 case VIDEO_CMD_PLAY
: {
281 vc
->play
.speed
= ivtv_validate_speed(itv
->speed
, vc
->play
.speed
);
282 if (vc
->play
.speed
< 0)
283 vc
->play
.format
= VIDEO_PLAY_FMT_GOP
;
286 if (ivtv_set_output_mode(itv
, OUT_MPG
) != OUT_MPG
)
288 if (test_and_clear_bit(IVTV_F_I_DEC_PAUSED
, &itv
->i_flags
)) {
289 /* forces ivtv_set_speed to be called */
292 return ivtv_start_decoding(id
, vc
->play
.speed
);
296 vc
->flags
&= VIDEO_CMD_STOP_IMMEDIATELY
|VIDEO_CMD_STOP_TO_BLACK
;
297 if (vc
->flags
& VIDEO_CMD_STOP_IMMEDIATELY
)
300 if (atomic_read(&itv
->decoding
) == 0)
302 if (itv
->output_mode
!= OUT_MPG
)
305 itv
->output_mode
= OUT_NONE
;
306 return ivtv_stop_v4l2_decode_stream(s
, vc
->flags
, vc
->stop
.pts
);
308 case VIDEO_CMD_FREEZE
:
309 vc
->flags
&= VIDEO_CMD_FREEZE_TO_BLACK
;
311 if (itv
->output_mode
!= OUT_MPG
)
313 if (atomic_read(&itv
->decoding
) > 0) {
314 ivtv_vapi(itv
, CX2341X_DEC_PAUSE_PLAYBACK
, 1,
315 (vc
->flags
& VIDEO_CMD_FREEZE_TO_BLACK
) ? 1 : 0);
316 set_bit(IVTV_F_I_DEC_PAUSED
, &itv
->i_flags
);
320 case VIDEO_CMD_CONTINUE
:
323 if (itv
->output_mode
!= OUT_MPG
)
325 if (test_and_clear_bit(IVTV_F_I_DEC_PAUSED
, &itv
->i_flags
)) {
326 int speed
= itv
->speed
;
328 return ivtv_start_decoding(id
, speed
);
338 static int ivtv_itvc(struct ivtv
*itv
, unsigned int cmd
, void *arg
)
340 struct v4l2_register
*regs
= arg
;
342 volatile u8 __iomem
*reg_start
;
344 if (!capable(CAP_SYS_ADMIN
))
346 if (regs
->reg
>= IVTV_REG_OFFSET
&& regs
->reg
< IVTV_REG_OFFSET
+ IVTV_REG_SIZE
)
347 reg_start
= itv
->reg_mem
- IVTV_REG_OFFSET
;
348 else if (itv
->has_cx23415
&& regs
->reg
>= IVTV_DECODER_OFFSET
&&
349 regs
->reg
< IVTV_DECODER_OFFSET
+ IVTV_DECODER_SIZE
)
350 reg_start
= itv
->dec_mem
- IVTV_DECODER_OFFSET
;
351 else if (regs
->reg
>= 0 && regs
->reg
< IVTV_ENCODER_SIZE
)
352 reg_start
= itv
->enc_mem
;
356 spin_lock_irqsave(&ivtv_cards_lock
, flags
);
357 if (cmd
== VIDIOC_DBG_G_REGISTER
) {
358 regs
->val
= readl(regs
->reg
+ reg_start
);
360 writel(regs
->val
, regs
->reg
+ reg_start
);
362 spin_unlock_irqrestore(&ivtv_cards_lock
, flags
);
366 static int ivtv_get_fmt(struct ivtv
*itv
, int streamtype
, struct v4l2_format
*fmt
)
369 case V4L2_BUF_TYPE_VIDEO_OUTPUT
:
370 if (!(itv
->v4l2_cap
& V4L2_CAP_VIDEO_OUTPUT
))
372 fmt
->fmt
.pix
.width
= itv
->main_rect
.width
;
373 fmt
->fmt
.pix
.height
= itv
->main_rect
.height
;
374 fmt
->fmt
.pix
.colorspace
= V4L2_COLORSPACE_SMPTE170M
;
375 fmt
->fmt
.pix
.field
= V4L2_FIELD_INTERLACED
;
376 if (itv
->output_mode
== OUT_UDMA_YUV
) {
377 switch (itv
->yuv_info
.lace_mode
& IVTV_YUV_MODE_MASK
) {
378 case IVTV_YUV_MODE_INTERLACED
:
379 fmt
->fmt
.pix
.field
= (itv
->yuv_info
.lace_mode
& IVTV_YUV_SYNC_MASK
) ?
380 V4L2_FIELD_INTERLACED_BT
: V4L2_FIELD_INTERLACED_TB
;
382 case IVTV_YUV_MODE_PROGRESSIVE
:
383 fmt
->fmt
.pix
.field
= V4L2_FIELD_NONE
;
386 fmt
->fmt
.pix
.field
= V4L2_FIELD_ANY
;
389 fmt
->fmt
.pix
.pixelformat
= V4L2_PIX_FMT_HM12
;
390 /* YUV size is (Y=(h*w) + UV=(h*(w/2))) */
391 fmt
->fmt
.pix
.sizeimage
=
392 fmt
->fmt
.pix
.height
* fmt
->fmt
.pix
.width
+
393 fmt
->fmt
.pix
.height
* (fmt
->fmt
.pix
.width
/ 2);
395 else if (itv
->output_mode
== OUT_YUV
||
396 streamtype
== IVTV_ENC_STREAM_TYPE_YUV
||
397 streamtype
== IVTV_DEC_STREAM_TYPE_YUV
) {
398 fmt
->fmt
.pix
.pixelformat
= V4L2_PIX_FMT_HM12
;
399 /* YUV size is (Y=(h*w) + UV=(h*(w/2))) */
400 fmt
->fmt
.pix
.sizeimage
=
401 fmt
->fmt
.pix
.height
* fmt
->fmt
.pix
.width
+
402 fmt
->fmt
.pix
.height
* (fmt
->fmt
.pix
.width
/ 2);
404 fmt
->fmt
.pix
.pixelformat
= V4L2_PIX_FMT_MPEG
;
405 fmt
->fmt
.pix
.sizeimage
= 128 * 1024;
409 case V4L2_BUF_TYPE_VIDEO_CAPTURE
:
410 fmt
->fmt
.pix
.width
= itv
->params
.width
;
411 fmt
->fmt
.pix
.height
= itv
->params
.height
;
412 fmt
->fmt
.pix
.colorspace
= V4L2_COLORSPACE_SMPTE170M
;
413 fmt
->fmt
.pix
.field
= V4L2_FIELD_INTERLACED
;
414 if (streamtype
== IVTV_ENC_STREAM_TYPE_YUV
||
415 streamtype
== IVTV_DEC_STREAM_TYPE_YUV
) {
416 fmt
->fmt
.pix
.pixelformat
= V4L2_PIX_FMT_HM12
;
417 /* YUV size is (Y=(h*w) + UV=(h*(w/2))) */
418 fmt
->fmt
.pix
.sizeimage
=
419 fmt
->fmt
.pix
.height
* fmt
->fmt
.pix
.width
+
420 fmt
->fmt
.pix
.height
* (fmt
->fmt
.pix
.width
/ 2);
422 fmt
->fmt
.pix
.pixelformat
= V4L2_PIX_FMT_MPEG
;
423 fmt
->fmt
.pix
.sizeimage
= 128 * 1024;
427 case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY
:
428 if (!(itv
->v4l2_cap
& V4L2_CAP_VIDEO_OUTPUT
))
430 fmt
->fmt
.win
.chromakey
= itv
->osd_color_key
;
431 fmt
->fmt
.win
.global_alpha
= itv
->osd_global_alpha
;
434 case V4L2_BUF_TYPE_VBI_CAPTURE
:
435 fmt
->fmt
.vbi
.sampling_rate
= 27000000;
436 fmt
->fmt
.vbi
.offset
= 248;
437 fmt
->fmt
.vbi
.samples_per_line
= itv
->vbi
.raw_decoder_line_size
- 4;
438 fmt
->fmt
.vbi
.sample_format
= V4L2_PIX_FMT_GREY
;
439 fmt
->fmt
.vbi
.start
[0] = itv
->vbi
.start
[0];
440 fmt
->fmt
.vbi
.start
[1] = itv
->vbi
.start
[1];
441 fmt
->fmt
.vbi
.count
[0] = fmt
->fmt
.vbi
.count
[1] = itv
->vbi
.count
;
444 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT
:
446 struct v4l2_sliced_vbi_format
*vbifmt
= &fmt
->fmt
.sliced
;
448 if (!(itv
->v4l2_cap
& V4L2_CAP_SLICED_VBI_OUTPUT
))
450 vbifmt
->io_size
= sizeof(struct v4l2_sliced_vbi_data
) * 36;
451 memset(vbifmt
->reserved
, 0, sizeof(vbifmt
->reserved
));
452 memset(vbifmt
->service_lines
, 0, sizeof(vbifmt
->service_lines
));
454 vbifmt
->service_lines
[0][21] = V4L2_SLICED_CAPTION_525
;
455 vbifmt
->service_lines
[1][21] = V4L2_SLICED_CAPTION_525
;
457 vbifmt
->service_lines
[0][23] = V4L2_SLICED_WSS_625
;
458 vbifmt
->service_lines
[0][16] = V4L2_SLICED_VPS
;
460 vbifmt
->service_set
= get_service_set(vbifmt
);
464 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE
:
466 struct v4l2_sliced_vbi_format
*vbifmt
= &fmt
->fmt
.sliced
;
468 vbifmt
->io_size
= sizeof(struct v4l2_sliced_vbi_data
) * 36;
469 memset(vbifmt
->reserved
, 0, sizeof(vbifmt
->reserved
));
470 memset(vbifmt
->service_lines
, 0, sizeof(vbifmt
->service_lines
));
472 if (streamtype
== IVTV_DEC_STREAM_TYPE_VBI
) {
473 vbifmt
->service_set
= itv
->is_50hz
? V4L2_SLICED_VBI_625
:
475 expand_service_set(vbifmt
, itv
->is_50hz
);
479 itv
->video_dec_func(itv
, VIDIOC_G_FMT
, fmt
);
480 vbifmt
->service_set
= get_service_set(vbifmt
);
483 case V4L2_BUF_TYPE_VBI_OUTPUT
:
484 case V4L2_BUF_TYPE_VIDEO_OVERLAY
:
491 static int ivtv_try_or_set_fmt(struct ivtv
*itv
, int streamtype
,
492 struct v4l2_format
*fmt
, int set_fmt
)
494 struct v4l2_sliced_vbi_format
*vbifmt
= &fmt
->fmt
.sliced
;
497 if (fmt
->type
== V4L2_BUF_TYPE_VIDEO_OUTPUT
) {
501 if (!(itv
->v4l2_cap
& V4L2_CAP_VIDEO_OUTPUT
))
503 field
= fmt
->fmt
.pix
.field
;
506 r
.width
= fmt
->fmt
.pix
.width
;
507 r
.height
= fmt
->fmt
.pix
.height
;
508 ivtv_get_fmt(itv
, streamtype
, fmt
);
509 if (itv
->output_mode
!= OUT_UDMA_YUV
) {
510 /* TODO: would setting the rect also be valid for this mode? */
511 fmt
->fmt
.pix
.width
= r
.width
;
512 fmt
->fmt
.pix
.height
= r
.height
;
514 if (itv
->output_mode
== OUT_UDMA_YUV
) {
515 /* TODO: add checks for validity */
516 fmt
->fmt
.pix
.field
= field
;
519 if (itv
->output_mode
== OUT_UDMA_YUV
) {
521 case V4L2_FIELD_NONE
:
522 itv
->yuv_info
.lace_mode
= IVTV_YUV_MODE_PROGRESSIVE
;
525 itv
->yuv_info
.lace_mode
= IVTV_YUV_MODE_AUTO
;
527 case V4L2_FIELD_INTERLACED_BT
:
528 itv
->yuv_info
.lace_mode
=
529 IVTV_YUV_MODE_INTERLACED
|IVTV_YUV_SYNC_ODD
;
531 case V4L2_FIELD_INTERLACED_TB
:
533 itv
->yuv_info
.lace_mode
= IVTV_YUV_MODE_INTERLACED
;
536 itv
->yuv_info
.lace_sync_field
= (itv
->yuv_info
.lace_mode
& IVTV_YUV_SYNC_MASK
) == IVTV_YUV_SYNC_EVEN
? 0 : 1;
538 /* Force update of yuv registers */
539 itv
->yuv_info
.yuv_forced_update
= 1;
546 if (fmt
->type
== V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY
) {
547 if (!(itv
->v4l2_cap
& V4L2_CAP_VIDEO_OUTPUT
))
550 itv
->osd_color_key
= fmt
->fmt
.win
.chromakey
;
551 itv
->osd_global_alpha
= fmt
->fmt
.win
.global_alpha
;
552 ivtv_set_osd_alpha(itv
);
557 /* set window size */
558 if (fmt
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE
) {
559 int w
= fmt
->fmt
.pix
.width
;
560 int h
= fmt
->fmt
.pix
.height
;
562 if (w
> 720) w
= 720;
563 else if (w
< 1) w
= 1;
564 if (h
> (itv
->is_50hz
? 576 : 480)) h
= (itv
->is_50hz
? 576 : 480);
565 else if (h
< 2) h
= 2;
566 ivtv_get_fmt(itv
, streamtype
, fmt
);
567 fmt
->fmt
.pix
.width
= w
;
568 fmt
->fmt
.pix
.height
= h
;
570 if (!set_fmt
|| (itv
->params
.width
== w
&& itv
->params
.height
== h
))
572 if (atomic_read(&itv
->capturing
) > 0)
575 itv
->params
.width
= w
;
576 itv
->params
.height
= h
;
577 if (w
!= 720 || h
!= (itv
->is_50hz
? 576 : 480))
578 itv
->params
.video_temporal_filter
= 0;
580 itv
->params
.video_temporal_filter
= 8;
581 itv
->video_dec_func(itv
, VIDIOC_S_FMT
, fmt
);
582 return ivtv_get_fmt(itv
, streamtype
, fmt
);
585 /* set raw VBI format */
586 if (fmt
->type
== V4L2_BUF_TYPE_VBI_CAPTURE
) {
587 if (set_fmt
&& streamtype
== IVTV_ENC_STREAM_TYPE_VBI
&&
588 itv
->vbi
.sliced_in
->service_set
&&
589 atomic_read(&itv
->capturing
) > 0) {
593 itv
->vbi
.sliced_in
->service_set
= 0;
594 itv
->video_dec_func(itv
, VIDIOC_S_FMT
, &itv
->vbi
.in
);
596 return ivtv_get_fmt(itv
, streamtype
, fmt
);
599 /* set sliced VBI output
600 In principle the user could request that only certain
601 VBI types are output and that the others are ignored.
602 I.e., suppress CC in the even fields or only output
603 WSS and no VPS. Currently though there is no choice. */
604 if (fmt
->type
== V4L2_BUF_TYPE_SLICED_VBI_OUTPUT
)
605 return ivtv_get_fmt(itv
, streamtype
, fmt
);
607 /* any else but sliced VBI capture is an error */
608 if (fmt
->type
!= V4L2_BUF_TYPE_SLICED_VBI_CAPTURE
)
611 if (streamtype
== IVTV_DEC_STREAM_TYPE_VBI
)
612 return ivtv_get_fmt(itv
, streamtype
, fmt
);
614 /* set sliced VBI capture format */
615 vbifmt
->io_size
= sizeof(struct v4l2_sliced_vbi_data
) * 36;
616 memset(vbifmt
->reserved
, 0, sizeof(vbifmt
->reserved
));
618 if (vbifmt
->service_set
)
619 expand_service_set(vbifmt
, itv
->is_50hz
);
620 set
= check_service_set(vbifmt
, itv
->is_50hz
);
621 vbifmt
->service_set
= get_service_set(vbifmt
);
627 if (atomic_read(&itv
->capturing
) > 0 && itv
->vbi
.sliced_in
->service_set
== 0) {
630 itv
->video_dec_func(itv
, VIDIOC_S_FMT
, fmt
);
631 memcpy(itv
->vbi
.sliced_in
, vbifmt
, sizeof(*itv
->vbi
.sliced_in
));
635 static int ivtv_debug_ioctls(struct file
*filp
, unsigned int cmd
, void *arg
)
637 struct ivtv_open_id
*id
= (struct ivtv_open_id
*)filp
->private_data
;
638 struct ivtv
*itv
= id
->itv
;
639 struct v4l2_register
*reg
= arg
;
642 /* ioctls to allow direct access to the encoder registers for testing */
643 case VIDIOC_DBG_G_REGISTER
:
644 if (v4l2_chip_match_host(reg
->match_type
, reg
->match_chip
))
645 return ivtv_itvc(itv
, cmd
, arg
);
646 if (reg
->match_type
== V4L2_CHIP_MATCH_I2C_DRIVER
)
647 return ivtv_i2c_id(itv
, reg
->match_chip
, cmd
, arg
);
648 return ivtv_call_i2c_client(itv
, reg
->match_chip
, cmd
, arg
);
650 case VIDIOC_DBG_S_REGISTER
:
651 if (v4l2_chip_match_host(reg
->match_type
, reg
->match_chip
))
652 return ivtv_itvc(itv
, cmd
, arg
);
653 if (reg
->match_type
== V4L2_CHIP_MATCH_I2C_DRIVER
)
654 return ivtv_i2c_id(itv
, reg
->match_chip
, cmd
, arg
);
655 return ivtv_call_i2c_client(itv
, reg
->match_chip
, cmd
, arg
);
657 case VIDIOC_G_CHIP_IDENT
: {
658 struct v4l2_chip_ident
*chip
= arg
;
660 chip
->ident
= V4L2_IDENT_NONE
;
662 if (reg
->match_type
== V4L2_CHIP_MATCH_HOST
) {
663 if (v4l2_chip_match_host(reg
->match_type
, reg
->match_chip
)) {
664 struct v4l2_chip_ident
*chip
= arg
;
666 chip
->ident
= itv
->has_cx23415
? V4L2_IDENT_CX23415
: V4L2_IDENT_CX23416
;
670 if (reg
->match_type
== V4L2_CHIP_MATCH_I2C_DRIVER
)
671 return ivtv_i2c_id(itv
, reg
->match_chip
, cmd
, arg
);
672 if (reg
->match_type
== V4L2_CHIP_MATCH_I2C_ADDR
)
673 return ivtv_call_i2c_client(itv
, reg
->match_chip
, cmd
, arg
);
677 case VIDIOC_INT_S_AUDIO_ROUTING
: {
678 struct v4l2_routing
*route
= arg
;
680 ivtv_audio_set_route(itv
, route
);
684 case VIDIOC_INT_RESET
: {
685 u32 val
= *(u32
*)arg
;
687 if ((val
== 0 && itv
->options
.newi2c
) || (val
& 0x01)) {
688 ivtv_reset_ir_gpio(itv
);
691 itv
->video_dec_func(itv
, cmd
, 0);
702 int ivtv_v4l2_ioctls(struct ivtv
*itv
, struct file
*filp
, unsigned int cmd
, void *arg
)
704 struct ivtv_open_id
*id
= NULL
;
706 if (filp
) id
= (struct ivtv_open_id
*)filp
->private_data
;
709 case VIDIOC_G_PRIORITY
:
711 enum v4l2_priority
*p
= arg
;
713 *p
= v4l2_prio_max(&itv
->prio
);
717 case VIDIOC_S_PRIORITY
:
719 enum v4l2_priority
*prio
= arg
;
721 return v4l2_prio_change(&itv
->prio
, &id
->prio
, *prio
);
724 case VIDIOC_QUERYCAP
:{
725 struct v4l2_capability
*vcap
= arg
;
727 memset(vcap
, 0, sizeof(*vcap
));
728 strcpy(vcap
->driver
, IVTV_DRIVER_NAME
); /* driver name */
729 strcpy(vcap
->card
, itv
->card_name
); /* card type */
730 strcpy(vcap
->bus_info
, pci_name(itv
->dev
)); /* bus info... */
731 vcap
->version
= IVTV_DRIVER_VERSION
; /* version */
732 vcap
->capabilities
= itv
->v4l2_cap
; /* capabilities */
734 /* reserved.. must set to 0! */
735 vcap
->reserved
[0] = vcap
->reserved
[1] =
736 vcap
->reserved
[2] = vcap
->reserved
[3] = 0;
740 case VIDIOC_ENUMAUDIO
:{
741 struct v4l2_audio
*vin
= arg
;
743 return ivtv_get_audio_input(itv
, vin
->index
, vin
);
746 case VIDIOC_G_AUDIO
:{
747 struct v4l2_audio
*vin
= arg
;
749 vin
->index
= itv
->audio_input
;
750 return ivtv_get_audio_input(itv
, vin
->index
, vin
);
753 case VIDIOC_S_AUDIO
:{
754 struct v4l2_audio
*vout
= arg
;
756 if (vout
->index
>= itv
->nof_audio_inputs
)
758 itv
->audio_input
= vout
->index
;
759 ivtv_audio_set_io(itv
);
763 case VIDIOC_ENUMAUDOUT
:{
764 struct v4l2_audioout
*vin
= arg
;
766 /* set it to defaults from our table */
767 return ivtv_get_audio_output(itv
, vin
->index
, vin
);
770 case VIDIOC_G_AUDOUT
:{
771 struct v4l2_audioout
*vin
= arg
;
774 return ivtv_get_audio_output(itv
, vin
->index
, vin
);
777 case VIDIOC_S_AUDOUT
:{
778 struct v4l2_audioout
*vout
= arg
;
780 return ivtv_get_audio_output(itv
, vout
->index
, vout
);
783 case VIDIOC_ENUMINPUT
:{
784 struct v4l2_input
*vin
= arg
;
786 /* set it to defaults from our table */
787 return ivtv_get_input(itv
, vin
->index
, vin
);
790 case VIDIOC_ENUMOUTPUT
:{
791 struct v4l2_output
*vout
= arg
;
793 return ivtv_get_output(itv
, vout
->index
, vout
);
798 struct v4l2_format
*fmt
= arg
;
800 return ivtv_try_or_set_fmt(itv
, id
->type
, fmt
, cmd
== VIDIOC_S_FMT
);
804 struct v4l2_format
*fmt
= arg
;
805 int type
= fmt
->type
;
807 memset(fmt
, 0, sizeof(*fmt
));
809 return ivtv_get_fmt(itv
, id
->type
, fmt
);
812 case VIDIOC_CROPCAP
: {
813 struct v4l2_cropcap
*cropcap
= arg
;
815 if (cropcap
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
&&
816 cropcap
->type
!= V4L2_BUF_TYPE_VIDEO_OUTPUT
)
818 cropcap
->bounds
.top
= cropcap
->bounds
.left
= 0;
819 cropcap
->bounds
.width
= 720;
820 if (cropcap
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE
) {
821 cropcap
->bounds
.height
= itv
->is_50hz
? 576 : 480;
822 cropcap
->pixelaspect
.numerator
= itv
->is_50hz
? 59 : 10;
823 cropcap
->pixelaspect
.denominator
= itv
->is_50hz
? 54 : 11;
825 cropcap
->bounds
.height
= itv
->is_out_50hz
? 576 : 480;
826 cropcap
->pixelaspect
.numerator
= itv
->is_out_50hz
? 59 : 10;
827 cropcap
->pixelaspect
.denominator
= itv
->is_out_50hz
? 54 : 11;
829 cropcap
->defrect
= cropcap
->bounds
;
833 case VIDIOC_S_CROP
: {
834 struct v4l2_crop
*crop
= arg
;
836 if (crop
->type
== V4L2_BUF_TYPE_VIDEO_OUTPUT
&&
837 (itv
->v4l2_cap
& V4L2_CAP_VIDEO_OUTPUT
)) {
838 if (!ivtv_vapi(itv
, CX2341X_OSD_SET_FRAMEBUFFER_WINDOW
, 4,
839 crop
->c
.width
, crop
->c
.height
, crop
->c
.left
, crop
->c
.top
)) {
840 itv
->main_rect
= crop
->c
;
845 if (crop
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
847 return itv
->video_dec_func(itv
, VIDIOC_S_CROP
, arg
);
850 case VIDIOC_G_CROP
: {
851 struct v4l2_crop
*crop
= arg
;
853 if (crop
->type
== V4L2_BUF_TYPE_VIDEO_OUTPUT
&&
854 (itv
->v4l2_cap
& V4L2_CAP_VIDEO_OUTPUT
)) {
855 crop
->c
= itv
->main_rect
;
858 if (crop
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
860 return itv
->video_dec_func(itv
, VIDIOC_G_CROP
, arg
);
863 case VIDIOC_ENUM_FMT
: {
864 static struct v4l2_fmtdesc formats
[] = {
866 "HM12 (YUV 4:1:1)", V4L2_PIX_FMT_HM12
,
869 { 1, 0, V4L2_FMT_FLAG_COMPRESSED
,
870 "MPEG", V4L2_PIX_FMT_MPEG
,
874 struct v4l2_fmtdesc
*fmt
= arg
;
875 enum v4l2_buf_type type
= fmt
->type
;
878 case V4L2_BUF_TYPE_VIDEO_CAPTURE
:
880 case V4L2_BUF_TYPE_VIDEO_OUTPUT
:
881 if (!(itv
->v4l2_cap
& V4L2_CAP_VIDEO_OUTPUT
))
889 *fmt
= formats
[fmt
->index
];
894 case VIDIOC_G_INPUT
:{
895 *(int *)arg
= itv
->active_input
;
899 case VIDIOC_S_INPUT
:{
900 int inp
= *(int *)arg
;
902 if (inp
< 0 || inp
>= itv
->nof_inputs
)
905 if (inp
== itv
->active_input
) {
906 IVTV_DEBUG_INFO("Input unchanged\n");
909 IVTV_DEBUG_INFO("Changing input from %d to %d\n",
910 itv
->active_input
, inp
);
912 itv
->active_input
= inp
;
913 /* Set the audio input to whatever is appropriate for the
915 itv
->audio_input
= itv
->card
->video_inputs
[inp
].audio_index
;
917 /* prevent others from messing with the streams until
918 we're finished changing inputs. */
920 ivtv_video_set_io(itv
);
921 ivtv_audio_set_io(itv
);
926 case VIDIOC_G_OUTPUT
:{
927 if (!(itv
->v4l2_cap
& V4L2_CAP_VIDEO_OUTPUT
))
929 *(int *)arg
= itv
->active_output
;
933 case VIDIOC_S_OUTPUT
:{
934 int outp
= *(int *)arg
;
935 struct v4l2_routing route
;
937 if (outp
>= itv
->card
->nof_outputs
)
940 if (outp
== itv
->active_output
) {
941 IVTV_DEBUG_INFO("Output unchanged\n");
944 IVTV_DEBUG_INFO("Changing output from %d to %d\n",
945 itv
->active_output
, outp
);
947 itv
->active_output
= outp
;
948 route
.input
= SAA7127_INPUT_TYPE_NORMAL
;
949 route
.output
= itv
->card
->video_outputs
[outp
].video_output
;
950 ivtv_saa7127(itv
, VIDIOC_INT_S_VIDEO_ROUTING
, &route
);
954 case VIDIOC_G_FREQUENCY
:{
955 struct v4l2_frequency
*vf
= arg
;
959 ivtv_call_i2c_clients(itv
, cmd
, arg
);
963 case VIDIOC_S_FREQUENCY
:{
964 struct v4l2_frequency vf
= *(struct v4l2_frequency
*)arg
;
970 IVTV_DEBUG_INFO("v4l2 ioctl: set frequency %d\n", vf
.frequency
);
971 ivtv_call_i2c_clients(itv
, cmd
, &vf
);
976 case VIDIOC_ENUMSTD
:{
977 struct v4l2_standard
*vs
= arg
;
980 if (idx
< 0 || idx
>= ARRAY_SIZE(enum_stds
))
983 *vs
= (enum_stds
[idx
].std
& V4L2_STD_525_60
) ?
984 ivtv_std_60hz
: ivtv_std_50hz
;
986 vs
->id
= enum_stds
[idx
].std
;
987 strcpy(vs
->name
, enum_stds
[idx
].name
);
992 *(v4l2_std_id
*) arg
= itv
->std
;
997 v4l2_std_id std
= *(v4l2_std_id
*) arg
;
999 if ((std
& V4L2_STD_ALL
) == 0)
1002 if (std
== itv
->std
)
1005 if (test_bit(IVTV_F_I_RADIO_USER
, &itv
->i_flags
) ||
1006 atomic_read(&itv
->capturing
) > 0 ||
1007 atomic_read(&itv
->decoding
) > 0) {
1008 /* Switching standard would turn off the radio or mess
1009 with already running streams, prevent that by
1015 itv
->is_60hz
= (std
& V4L2_STD_525_60
) ? 1 : 0;
1016 itv
->params
.is_50hz
= itv
->is_50hz
= !itv
->is_60hz
;
1017 itv
->params
.width
= 720;
1018 itv
->params
.height
= itv
->is_50hz
? 576 : 480;
1019 itv
->vbi
.count
= itv
->is_50hz
? 18 : 12;
1020 itv
->vbi
.start
[0] = itv
->is_50hz
? 6 : 10;
1021 itv
->vbi
.start
[1] = itv
->is_50hz
? 318 : 273;
1022 if (itv
->hw_flags
& IVTV_HW_CX25840
) {
1023 itv
->vbi
.sliced_decoder_line_size
= itv
->is_60hz
? 272 : 284;
1025 IVTV_DEBUG_INFO("Switching standard to %llx.\n", (unsigned long long)itv
->std
);
1028 ivtv_call_i2c_clients(itv
, VIDIOC_S_STD
, &itv
->std
);
1030 if (itv
->v4l2_cap
& V4L2_CAP_VIDEO_OUTPUT
) {
1031 /* set display standard */
1033 itv
->is_out_60hz
= itv
->is_60hz
;
1034 itv
->is_out_50hz
= itv
->is_50hz
;
1035 ivtv_call_i2c_clients(itv
, VIDIOC_INT_S_STD_OUTPUT
, &itv
->std_out
);
1036 ivtv_vapi(itv
, CX2341X_DEC_SET_STANDARD
, 1, itv
->is_out_50hz
);
1037 itv
->main_rect
.left
= itv
->main_rect
.top
= 0;
1038 itv
->main_rect
.width
= 720;
1039 itv
->main_rect
.height
= itv
->params
.height
;
1040 ivtv_vapi(itv
, CX2341X_OSD_SET_FRAMEBUFFER_WINDOW
, 4,
1041 720, itv
->main_rect
.height
, 0, 0);
1046 case VIDIOC_S_TUNER
: { /* Setting tuner can only set audio mode */
1047 struct v4l2_tuner
*vt
= arg
;
1052 ivtv_call_i2c_clients(itv
, VIDIOC_S_TUNER
, vt
);
1056 case VIDIOC_G_TUNER
: {
1057 struct v4l2_tuner
*vt
= arg
;
1062 memset(vt
, 0, sizeof(*vt
));
1063 ivtv_call_i2c_clients(itv
, VIDIOC_G_TUNER
, vt
);
1065 if (test_bit(IVTV_F_I_RADIO_USER
, &itv
->i_flags
)) {
1066 strcpy(vt
->name
, "ivtv Radio Tuner");
1067 vt
->type
= V4L2_TUNER_RADIO
;
1069 strcpy(vt
->name
, "ivtv TV Tuner");
1070 vt
->type
= V4L2_TUNER_ANALOG_TV
;
1075 case VIDIOC_G_SLICED_VBI_CAP
: {
1076 struct v4l2_sliced_vbi_cap
*cap
= arg
;
1077 int set
= itv
->is_50hz
? V4L2_SLICED_VBI_625
: V4L2_SLICED_VBI_525
;
1079 enum v4l2_buf_type type
= cap
->type
;
1081 memset(cap
, 0, sizeof(*cap
));
1083 if (type
== V4L2_BUF_TYPE_SLICED_VBI_CAPTURE
) {
1084 for (f
= 0; f
< 2; f
++) {
1085 for (l
= 0; l
< 24; l
++) {
1086 if (valid_service_line(f
, l
, itv
->is_50hz
)) {
1087 cap
->service_lines
[f
][l
] = set
;
1093 if (type
== V4L2_BUF_TYPE_SLICED_VBI_OUTPUT
) {
1094 if (!(itv
->v4l2_cap
& V4L2_CAP_SLICED_VBI_OUTPUT
))
1097 cap
->service_lines
[0][21] = V4L2_SLICED_CAPTION_525
;
1098 cap
->service_lines
[1][21] = V4L2_SLICED_CAPTION_525
;
1100 cap
->service_lines
[0][23] = V4L2_SLICED_WSS_625
;
1101 cap
->service_lines
[0][16] = V4L2_SLICED_VPS
;
1108 case VIDIOC_G_ENC_INDEX
: {
1109 struct v4l2_enc_idx
*idx
= arg
;
1110 struct v4l2_enc_idx_entry
*e
= idx
->entry
;
1114 entries
= (itv
->pgm_info_write_idx
+ IVTV_MAX_PGM_INDEX
- itv
->pgm_info_read_idx
) %
1116 if (entries
> V4L2_ENC_IDX_ENTRIES
)
1117 entries
= V4L2_ENC_IDX_ENTRIES
;
1119 for (i
= 0; i
< entries
; i
++) {
1120 *e
= itv
->pgm_info
[(itv
->pgm_info_read_idx
+ i
) % IVTV_MAX_PGM_INDEX
];
1121 if ((e
->flags
& V4L2_ENC_IDX_FRAME_MASK
) <= V4L2_ENC_IDX_FRAME_B
) {
1126 itv
->pgm_info_read_idx
= (itv
->pgm_info_read_idx
+ idx
->entries
) % IVTV_MAX_PGM_INDEX
;
1130 case VIDIOC_ENCODER_CMD
:
1131 case VIDIOC_TRY_ENCODER_CMD
: {
1132 struct v4l2_encoder_cmd
*enc
= arg
;
1133 int try = cmd
== VIDIOC_TRY_ENCODER_CMD
;
1135 memset(&enc
->raw
, 0, sizeof(enc
->raw
));
1137 case V4L2_ENC_CMD_START
:
1141 return ivtv_start_capture(id
);
1143 case V4L2_ENC_CMD_STOP
:
1144 enc
->flags
&= V4L2_ENC_CMD_STOP_AT_GOP_END
;
1147 ivtv_stop_capture(id
, enc
->flags
& V4L2_ENC_CMD_STOP_AT_GOP_END
);
1150 case V4L2_ENC_CMD_PAUSE
:
1154 if (!atomic_read(&itv
->capturing
))
1156 if (test_and_set_bit(IVTV_F_I_ENC_PAUSED
, &itv
->i_flags
))
1159 ivtv_vapi(itv
, CX2341X_ENC_PAUSE_ENCODER
, 1, 0);
1162 case V4L2_ENC_CMD_RESUME
:
1166 if (!atomic_read(&itv
->capturing
))
1168 if (!test_and_clear_bit(IVTV_F_I_ENC_PAUSED
, &itv
->i_flags
))
1170 ivtv_vapi(itv
, CX2341X_ENC_PAUSE_ENCODER
, 1, 1);
1179 case VIDIOC_G_FBUF
: {
1180 struct v4l2_framebuffer
*fb
= arg
;
1182 memset(fb
, 0, sizeof(*fb
));
1183 if (!(itv
->v4l2_cap
& V4L2_CAP_VIDEO_OUTPUT_OVERLAY
))
1185 fb
->capability
= V4L2_FBUF_CAP_EXTERNOVERLAY
| V4L2_FBUF_CAP_CHROMAKEY
|
1186 V4L2_FBUF_CAP_LOCAL_ALPHA
| V4L2_FBUF_CAP_GLOBAL_ALPHA
;
1187 fb
->fmt
.pixelformat
= itv
->osd_pixelformat
;
1188 fb
->fmt
.width
= itv
->osd_rect
.width
;
1189 fb
->fmt
.height
= itv
->osd_rect
.height
;
1190 fb
->base
= (void *)itv
->osd_video_pbase
;
1191 if (itv
->osd_global_alpha_state
)
1192 fb
->flags
|= V4L2_FBUF_FLAG_GLOBAL_ALPHA
;
1193 if (itv
->osd_local_alpha_state
)
1194 fb
->flags
|= V4L2_FBUF_FLAG_LOCAL_ALPHA
;
1195 if (itv
->osd_color_key_state
)
1196 fb
->flags
|= V4L2_FBUF_FLAG_CHROMAKEY
;
1200 case VIDIOC_S_FBUF
: {
1201 struct v4l2_framebuffer
*fb
= arg
;
1203 if (!(itv
->v4l2_cap
& V4L2_CAP_VIDEO_OUTPUT_OVERLAY
))
1205 itv
->osd_global_alpha_state
= (fb
->flags
& V4L2_FBUF_FLAG_GLOBAL_ALPHA
) != 0;
1206 itv
->osd_local_alpha_state
= (fb
->flags
& V4L2_FBUF_FLAG_LOCAL_ALPHA
) != 0;
1207 itv
->osd_color_key_state
= (fb
->flags
& V4L2_FBUF_FLAG_CHROMAKEY
) != 0;
1208 ivtv_set_osd_alpha(itv
);
1212 case VIDIOC_LOG_STATUS
:
1214 int has_output
= itv
->v4l2_cap
& V4L2_CAP_VIDEO_OUTPUT
;
1215 struct v4l2_input vidin
;
1216 struct v4l2_audio audin
;
1219 IVTV_INFO("================= START STATUS CARD #%d =================\n", itv
->num
);
1220 IVTV_INFO("Version: %s Card: %s\n", IVTV_VERSION
, itv
->card_name
);
1221 if (itv
->hw_flags
& IVTV_HW_TVEEPROM
) {
1224 ivtv_read_eeprom(itv
, &tv
);
1226 ivtv_call_i2c_clients(itv
, VIDIOC_LOG_STATUS
, NULL
);
1227 ivtv_get_input(itv
, itv
->active_input
, &vidin
);
1228 ivtv_get_audio_input(itv
, itv
->audio_input
, &audin
);
1229 IVTV_INFO("Video Input: %s\n", vidin
.name
);
1230 IVTV_INFO("Audio Input: %s\n", audin
.name
);
1232 struct v4l2_output vidout
;
1233 struct v4l2_audioout audout
;
1234 int mode
= itv
->output_mode
;
1235 static const char * const output_modes
[] = {
1243 ivtv_get_output(itv
, itv
->active_output
, &vidout
);
1244 ivtv_get_audio_output(itv
, 0, &audout
);
1245 IVTV_INFO("Video Output: %s\n", vidout
.name
);
1246 IVTV_INFO("Audio Output: %s\n", audout
.name
);
1247 if (mode
< 0 || mode
> OUT_PASSTHROUGH
)
1249 IVTV_INFO("Output Mode: %s\n", output_modes
[mode
]);
1251 IVTV_INFO("Tuner: %s\n",
1252 test_bit(IVTV_F_I_RADIO_USER
, &itv
->i_flags
) ? "Radio" : "TV");
1253 cx2341x_log_status(&itv
->params
, itv
->name
);
1254 IVTV_INFO("Status flags: 0x%08lx\n", itv
->i_flags
);
1255 for (i
= 0; i
< IVTV_MAX_STREAMS
; i
++) {
1256 struct ivtv_stream
*s
= &itv
->streams
[i
];
1258 if (s
->v4l2dev
== NULL
|| s
->buffers
== 0)
1260 IVTV_INFO("Stream %s: status 0x%04lx, %d%% of %d KiB (%d buffers) in use\n", s
->name
, s
->s_flags
,
1261 (s
->buffers
- s
->q_free
.buffers
) * 100 / s
->buffers
,
1262 (s
->buffers
* s
->buf_size
) / 1024, s
->buffers
);
1264 IVTV_INFO("Read MPEG/VBI: %lld/%lld bytes\n", (long long)itv
->mpg_data_received
, (long long)itv
->vbi_data_inserted
);
1265 IVTV_INFO("================== END STATUS CARD #%d ==================\n", itv
->num
);
1275 static int ivtv_decoder_ioctls(struct file
*filp
, unsigned int cmd
, void *arg
)
1277 struct ivtv_open_id
*id
= (struct ivtv_open_id
*)filp
->private_data
;
1278 struct ivtv
*itv
= id
->itv
;
1279 int nonblocking
= filp
->f_flags
& O_NONBLOCK
;
1280 struct ivtv_stream
*s
= &itv
->streams
[id
->type
];
1283 case IVTV_IOC_DMA_FRAME
: {
1284 struct ivtv_dma_frame
*args
= arg
;
1286 IVTV_DEBUG_IOCTL("IVTV_IOC_DMA_FRAME\n");
1287 if (!(itv
->v4l2_cap
& V4L2_CAP_VIDEO_OUTPUT
))
1289 if (args
->type
!= V4L2_BUF_TYPE_VIDEO_OUTPUT
)
1291 if (itv
->output_mode
== OUT_UDMA_YUV
&& args
->y_source
== NULL
)
1293 if (ivtv_claim_stream(id
, id
->type
)) {
1296 if (ivtv_set_output_mode(itv
, OUT_UDMA_YUV
) != OUT_UDMA_YUV
) {
1297 ivtv_release_stream(s
);
1300 if (args
->y_source
== NULL
)
1302 return ivtv_yuv_prep_frame(itv
, args
);
1305 case VIDEO_GET_PTS
: {
1306 u32 data
[CX2341X_MBOX_MAX_DATA
];
1309 IVTV_DEBUG_IOCTL("VIDEO_GET_PTS\n");
1310 if (s
->type
< IVTV_DEC_STREAM_TYPE_MPG
) {
1314 if (!(itv
->v4l2_cap
& V4L2_CAP_VIDEO_OUTPUT
))
1317 if (test_bit(IVTV_F_I_VALID_DEC_TIMINGS
, &itv
->i_flags
)) {
1318 *pts
= (u64
) ((u64
)itv
->last_dec_timing
[2] << 32) |
1319 (u64
)itv
->last_dec_timing
[1];
1323 if (atomic_read(&itv
->decoding
)) {
1324 if (ivtv_api(itv
, CX2341X_DEC_GET_TIMING_INFO
, 5, data
)) {
1325 IVTV_DEBUG_WARN("GET_TIMING: couldn't read clock\n");
1328 memcpy(itv
->last_dec_timing
, data
, sizeof(itv
->last_dec_timing
));
1329 set_bit(IVTV_F_I_VALID_DEC_TIMINGS
, &itv
->i_flags
);
1330 *pts
= (u64
) ((u64
) data
[2] << 32) | (u64
) data
[1];
1331 /*timing->scr = (u64) (((u64) data[4] << 32) | (u64) (data[3]));*/
1336 case VIDEO_GET_FRAME_COUNT
: {
1337 u32 data
[CX2341X_MBOX_MAX_DATA
];
1340 IVTV_DEBUG_IOCTL("VIDEO_GET_FRAME_COUNT\n");
1341 if (s
->type
< IVTV_DEC_STREAM_TYPE_MPG
) {
1345 if (!(itv
->v4l2_cap
& V4L2_CAP_VIDEO_OUTPUT
))
1348 if (test_bit(IVTV_F_I_VALID_DEC_TIMINGS
, &itv
->i_flags
)) {
1349 *frame
= itv
->last_dec_timing
[0];
1353 if (atomic_read(&itv
->decoding
)) {
1354 if (ivtv_api(itv
, CX2341X_DEC_GET_TIMING_INFO
, 5, data
)) {
1355 IVTV_DEBUG_WARN("GET_TIMING: couldn't read clock\n");
1358 memcpy(itv
->last_dec_timing
, data
, sizeof(itv
->last_dec_timing
));
1359 set_bit(IVTV_F_I_VALID_DEC_TIMINGS
, &itv
->i_flags
);
1366 struct video_command vc
;
1368 IVTV_DEBUG_IOCTL("VIDEO_PLAY\n");
1369 memset(&vc
, 0, sizeof(vc
));
1370 vc
.cmd
= VIDEO_CMD_PLAY
;
1371 return ivtv_video_command(itv
, id
, &vc
, 0);
1375 struct video_command vc
;
1377 IVTV_DEBUG_IOCTL("VIDEO_STOP\n");
1378 memset(&vc
, 0, sizeof(vc
));
1379 vc
.cmd
= VIDEO_CMD_STOP
;
1380 vc
.flags
= VIDEO_CMD_STOP_TO_BLACK
| VIDEO_CMD_STOP_IMMEDIATELY
;
1381 return ivtv_video_command(itv
, id
, &vc
, 0);
1384 case VIDEO_FREEZE
: {
1385 struct video_command vc
;
1387 IVTV_DEBUG_IOCTL("VIDEO_FREEZE\n");
1388 memset(&vc
, 0, sizeof(vc
));
1389 vc
.cmd
= VIDEO_CMD_FREEZE
;
1390 return ivtv_video_command(itv
, id
, &vc
, 0);
1393 case VIDEO_CONTINUE
: {
1394 struct video_command vc
;
1396 IVTV_DEBUG_IOCTL("VIDEO_CONTINUE\n");
1397 memset(&vc
, 0, sizeof(vc
));
1398 vc
.cmd
= VIDEO_CMD_CONTINUE
;
1399 return ivtv_video_command(itv
, id
, &vc
, 0);
1403 case VIDEO_TRY_COMMAND
: {
1404 struct video_command
*vc
= arg
;
1405 int try = (cmd
== VIDEO_TRY_COMMAND
);
1408 IVTV_DEBUG_IOCTL("VIDEO_TRY_COMMAND\n");
1410 IVTV_DEBUG_IOCTL("VIDEO_COMMAND\n");
1411 return ivtv_video_command(itv
, id
, vc
, try);
1414 case VIDEO_GET_EVENT
: {
1415 struct video_event
*ev
= arg
;
1418 IVTV_DEBUG_IOCTL("VIDEO_GET_EVENT\n");
1419 if (!(itv
->v4l2_cap
& V4L2_CAP_VIDEO_OUTPUT
))
1421 memset(ev
, 0, sizeof(*ev
));
1422 set_bit(IVTV_F_I_EV_VSYNC_ENABLED
, &itv
->i_flags
);
1425 if (test_and_clear_bit(IVTV_F_I_EV_DEC_STOPPED
, &itv
->i_flags
))
1426 ev
->type
= VIDEO_EVENT_DECODER_STOPPED
;
1427 else if (test_and_clear_bit(IVTV_F_I_EV_VSYNC
, &itv
->i_flags
)) {
1428 ev
->type
= VIDEO_EVENT_VSYNC
;
1429 ev
->u
.vsync_field
= test_bit(IVTV_F_I_EV_VSYNC_FIELD
, &itv
->i_flags
) ?
1430 VIDEO_VSYNC_FIELD_ODD
: VIDEO_VSYNC_FIELD_EVEN
;
1431 if (itv
->output_mode
== OUT_UDMA_YUV
&&
1432 (itv
->yuv_info
.lace_mode
& IVTV_YUV_MODE_MASK
) ==
1433 IVTV_YUV_MODE_PROGRESSIVE
) {
1434 ev
->u
.vsync_field
= VIDEO_VSYNC_FIELD_PROGRESSIVE
;
1441 /* wait for event */
1442 prepare_to_wait(&itv
->event_waitq
, &wait
, TASK_INTERRUPTIBLE
);
1443 if ((itv
->i_flags
& (IVTV_F_I_EV_DEC_STOPPED
|IVTV_F_I_EV_VSYNC
)) == 0)
1445 finish_wait(&itv
->event_waitq
, &wait
);
1446 if (signal_pending(current
)) {
1447 /* return if a signal was received */
1448 IVTV_DEBUG_INFO("User stopped wait for event\n");
1461 static int ivtv_v4l2_do_ioctl(struct inode
*inode
, struct file
*filp
,
1462 unsigned int cmd
, void *arg
)
1464 struct ivtv_open_id
*id
= (struct ivtv_open_id
*)filp
->private_data
;
1465 struct ivtv
*itv
= id
->itv
;
1468 /* check priority */
1472 case VIDIOC_S_INPUT
:
1473 case VIDIOC_S_OUTPUT
:
1474 case VIDIOC_S_TUNER
:
1475 case VIDIOC_S_FREQUENCY
:
1478 case VIDIOC_S_AUDIO
:
1479 case VIDIOC_S_AUDOUT
:
1480 case VIDIOC_S_EXT_CTRLS
:
1482 ret
= v4l2_prio_check(&itv
->prio
, &id
->prio
);
1488 case VIDIOC_DBG_G_REGISTER
:
1489 case VIDIOC_DBG_S_REGISTER
:
1490 case VIDIOC_G_CHIP_IDENT
:
1491 case VIDIOC_INT_S_AUDIO_ROUTING
:
1492 case VIDIOC_INT_RESET
:
1493 if (ivtv_debug
& IVTV_DBGFLG_IOCTL
) {
1494 printk(KERN_INFO
"ivtv%d ioctl: ", itv
->num
);
1495 v4l_printk_ioctl(cmd
);
1497 return ivtv_debug_ioctls(filp
, cmd
, arg
);
1499 case VIDIOC_G_PRIORITY
:
1500 case VIDIOC_S_PRIORITY
:
1501 case VIDIOC_QUERYCAP
:
1502 case VIDIOC_ENUMINPUT
:
1503 case VIDIOC_G_INPUT
:
1504 case VIDIOC_S_INPUT
:
1505 case VIDIOC_ENUMOUTPUT
:
1506 case VIDIOC_G_OUTPUT
:
1507 case VIDIOC_S_OUTPUT
:
1510 case VIDIOC_TRY_FMT
:
1511 case VIDIOC_ENUM_FMT
:
1512 case VIDIOC_CROPCAP
:
1515 case VIDIOC_G_FREQUENCY
:
1516 case VIDIOC_S_FREQUENCY
:
1517 case VIDIOC_ENUMSTD
:
1520 case VIDIOC_S_TUNER
:
1521 case VIDIOC_G_TUNER
:
1522 case VIDIOC_ENUMAUDIO
:
1523 case VIDIOC_S_AUDIO
:
1524 case VIDIOC_G_AUDIO
:
1525 case VIDIOC_ENUMAUDOUT
:
1526 case VIDIOC_S_AUDOUT
:
1527 case VIDIOC_G_AUDOUT
:
1528 case VIDIOC_G_SLICED_VBI_CAP
:
1529 case VIDIOC_LOG_STATUS
:
1530 case VIDIOC_G_ENC_INDEX
:
1531 case VIDIOC_ENCODER_CMD
:
1532 case VIDIOC_TRY_ENCODER_CMD
:
1535 if (ivtv_debug
& IVTV_DBGFLG_IOCTL
) {
1536 printk(KERN_INFO
"ivtv%d ioctl: ", itv
->num
);
1537 v4l_printk_ioctl(cmd
);
1539 return ivtv_v4l2_ioctls(itv
, filp
, cmd
, arg
);
1541 case VIDIOC_QUERYMENU
:
1542 case VIDIOC_QUERYCTRL
:
1545 case VIDIOC_S_EXT_CTRLS
:
1546 case VIDIOC_G_EXT_CTRLS
:
1547 case VIDIOC_TRY_EXT_CTRLS
:
1548 if (ivtv_debug
& IVTV_DBGFLG_IOCTL
) {
1549 printk(KERN_INFO
"ivtv%d ioctl: ", itv
->num
);
1550 v4l_printk_ioctl(cmd
);
1552 return ivtv_control_ioctls(itv
, cmd
, arg
);
1554 case IVTV_IOC_DMA_FRAME
:
1556 case VIDEO_GET_FRAME_COUNT
:
1557 case VIDEO_GET_EVENT
:
1561 case VIDEO_CONTINUE
:
1563 case VIDEO_TRY_COMMAND
:
1564 return ivtv_decoder_ioctls(filp
, cmd
, arg
);
1566 case 0x00005401: /* Handle isatty() calls */
1569 return v4l_compat_translate_ioctl(inode
, filp
, cmd
, arg
,
1570 ivtv_v4l2_do_ioctl
);
1575 int ivtv_v4l2_ioctl(struct inode
*inode
, struct file
*filp
, unsigned int cmd
,
1578 struct ivtv_open_id
*id
= (struct ivtv_open_id
*)filp
->private_data
;
1579 struct ivtv
*itv
= id
->itv
;
1581 /* Filter dvb ioctls that cannot be handled by video_usercopy */
1583 case VIDEO_SELECT_SOURCE
:
1584 IVTV_DEBUG_IOCTL("VIDEO_SELECT_SOURCE\n");
1585 if (!(itv
->v4l2_cap
& V4L2_CAP_VIDEO_OUTPUT
))
1587 return ivtv_passthrough_mode(itv
, arg
== VIDEO_SOURCE_DEMUX
);
1589 case AUDIO_SET_MUTE
:
1590 IVTV_DEBUG_IOCTL("AUDIO_SET_MUTE\n");
1591 itv
->speed_mute_audio
= arg
;
1594 case AUDIO_CHANNEL_SELECT
:
1595 IVTV_DEBUG_IOCTL("AUDIO_CHANNEL_SELECT\n");
1596 if (arg
> AUDIO_STEREO_SWAPPED
)
1598 itv
->audio_stereo_mode
= arg
;
1599 ivtv_vapi(itv
, CX2341X_DEC_SET_AUDIO_MODE
, 2, itv
->audio_bilingual_mode
, itv
->audio_stereo_mode
);
1602 case AUDIO_BILINGUAL_CHANNEL_SELECT
:
1603 IVTV_DEBUG_IOCTL("AUDIO_BILINGUAL_CHANNEL_SELECT\n");
1604 if (arg
> AUDIO_STEREO_SWAPPED
)
1606 itv
->audio_bilingual_mode
= arg
;
1607 ivtv_vapi(itv
, CX2341X_DEC_SET_AUDIO_MODE
, 2, itv
->audio_bilingual_mode
, itv
->audio_stereo_mode
);
1613 return video_usercopy(inode
, filp
, cmd
, arg
, ivtv_v4l2_do_ioctl
);