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-routing.h"
29 #include "ivtv-streams.h"
31 #include "ivtv-ioctl.h"
32 #include "ivtv-gpio.h"
33 #include "ivtv-controls.h"
34 #include "ivtv-cards.h"
35 #include <media/saa7127.h>
36 #include <media/tveeprom.h>
37 #include <media/v4l2-chip-ident.h>
38 #include <media/v4l2-event.h>
39 #include <linux/dvb/audio.h>
40 #include <linux/i2c-id.h>
42 u16 ivtv_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 ivtv_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 void check_service_set(struct v4l2_sliced_vbi_format *fmt, int is_pal)
109 for (f = 0; f < 2; f++) {
110 for (l = 0; l < 24; l++) {
111 fmt->service_lines[f][l] = select_service_from_set(f, l, fmt->service_lines[f][l], is_pal);
116 u16 ivtv_get_service_set(struct v4l2_sliced_vbi_format *fmt)
121 for (f = 0; f < 2; f++) {
122 for (l = 0; l < 24; l++) {
123 set |= fmt->service_lines[f][l];
129 void ivtv_set_osd_alpha(struct ivtv *itv)
131 ivtv_vapi(itv, CX2341X_OSD_SET_GLOBAL_ALPHA, 3,
132 itv->osd_global_alpha_state, itv->osd_global_alpha, !itv->osd_local_alpha_state);
133 ivtv_vapi(itv, CX2341X_OSD_SET_CHROMA_KEY, 2, itv->osd_chroma_key_state, itv->osd_chroma_key);
136 int ivtv_set_speed(struct ivtv *itv, int speed)
138 u32 data[CX2341X_MBOX_MAX_DATA];
139 struct ivtv_stream *s;
140 int single_step = (speed == 1 || speed == -1);
143 if (speed == 0) speed = 1000;
146 if (speed == itv->speed && !single_step)
149 s = &itv->streams[IVTV_DEC_STREAM_TYPE_MPG];
151 if (single_step && (speed < 0) == (itv->speed < 0)) {
152 /* Single step video and no need to change direction */
153 ivtv_vapi(itv, CX2341X_DEC_STEP_VIDEO, 1, 0);
158 /* Need to change direction */
159 speed = speed < 0 ? -1000 : 1000;
161 data[0] = (speed > 1000 || speed < -1000) ? 0x80000000 : 0;
162 data[0] |= (speed > 1000 || speed < -1500) ? 0x40000000 : 0;
163 data[1] = (speed < 0);
164 data[2] = speed < 0 ? 3 : 7;
165 data[3] = itv->params.video_b_frames;
166 data[4] = (speed == 1500 || speed == 500) ? itv->speed_mute_audio : 0;
170 if (speed == 1500 || speed == -1500) data[0] |= 1;
171 else if (speed == 2000 || speed == -2000) data[0] |= 2;
172 else if (speed > -1000 && speed < 0) data[0] |= (-1000 / speed);
173 else if (speed < 1000 && speed > 0) data[0] |= (1000 / speed);
175 /* If not decoding, just change speed setting */
176 if (atomic_read(&itv->decoding) > 0) {
179 /* Stop all DMA and decoding activity */
180 ivtv_vapi(itv, CX2341X_DEC_PAUSE_PLAYBACK, 1, 0);
182 /* Wait for any DMA to finish */
183 prepare_to_wait(&itv->dma_waitq, &wait, TASK_INTERRUPTIBLE);
184 while (test_bit(IVTV_F_I_DMA, &itv->i_flags)) {
185 got_sig = signal_pending(current);
191 finish_wait(&itv->dma_waitq, &wait);
195 /* Change Speed safely */
196 ivtv_api(itv, CX2341X_DEC_SET_PLAYBACK_SPEED, 7, data);
197 IVTV_DEBUG_INFO("Setting Speed to 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x\n",
198 data[0], data[1], data[2], data[3], data[4], data[5], data[6]);
201 speed = (speed < 0) ? -1 : 1;
202 ivtv_vapi(itv, CX2341X_DEC_STEP_VIDEO, 1, 0);
208 static int ivtv_validate_speed(int cur_speed, int new_speed)
210 int fact = new_speed < 0 ? -1 : 1;
216 new_speed = -new_speed;
218 cur_speed = -cur_speed;
220 if (cur_speed <= new_speed) {
221 if (new_speed > 1500)
223 if (new_speed > 1000)
227 if (new_speed >= 2000)
229 if (new_speed >= 1500)
231 if (new_speed >= 1000)
236 if (new_speed == 1 || new_speed == 1000)
237 return fact * new_speed;
240 new_speed = 1000 / new_speed;
241 if (1000 / cur_speed == new_speed)
242 new_speed += (cur_speed < s) ? -1 : 1;
243 if (new_speed > 60) return 1000 / (fact * 60);
244 return 1000 / (fact * new_speed);
247 static int ivtv_video_command(struct ivtv *itv, struct ivtv_open_id *id,
248 struct video_command *vc, int try)
250 struct ivtv_stream *s = &itv->streams[IVTV_DEC_STREAM_TYPE_MPG];
252 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
256 case VIDEO_CMD_PLAY: {
258 vc->play.speed = ivtv_validate_speed(itv->speed, vc->play.speed);
259 if (vc->play.speed < 0)
260 vc->play.format = VIDEO_PLAY_FMT_GOP;
263 if (ivtv_set_output_mode(itv, OUT_MPG) != OUT_MPG)
265 if (test_and_clear_bit(IVTV_F_I_DEC_PAUSED, &itv->i_flags)) {
266 /* forces ivtv_set_speed to be called */
269 return ivtv_start_decoding(id, vc->play.speed);
273 vc->flags &= VIDEO_CMD_STOP_IMMEDIATELY|VIDEO_CMD_STOP_TO_BLACK;
274 if (vc->flags & VIDEO_CMD_STOP_IMMEDIATELY)
277 if (atomic_read(&itv->decoding) == 0)
279 if (itv->output_mode != OUT_MPG)
282 itv->output_mode = OUT_NONE;
283 return ivtv_stop_v4l2_decode_stream(s, vc->flags, vc->stop.pts);
285 case VIDEO_CMD_FREEZE:
286 vc->flags &= VIDEO_CMD_FREEZE_TO_BLACK;
288 if (itv->output_mode != OUT_MPG)
290 if (atomic_read(&itv->decoding) > 0) {
291 ivtv_vapi(itv, CX2341X_DEC_PAUSE_PLAYBACK, 1,
292 (vc->flags & VIDEO_CMD_FREEZE_TO_BLACK) ? 1 : 0);
293 set_bit(IVTV_F_I_DEC_PAUSED, &itv->i_flags);
297 case VIDEO_CMD_CONTINUE:
300 if (itv->output_mode != OUT_MPG)
302 if (test_and_clear_bit(IVTV_F_I_DEC_PAUSED, &itv->i_flags)) {
303 int speed = itv->speed;
305 return ivtv_start_decoding(id, speed);
315 static int ivtv_g_fmt_sliced_vbi_out(struct file *file, void *fh, struct v4l2_format *fmt)
317 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
318 struct v4l2_sliced_vbi_format *vbifmt = &fmt->fmt.sliced;
320 vbifmt->reserved[0] = 0;
321 vbifmt->reserved[1] = 0;
322 if (!(itv->v4l2_cap & V4L2_CAP_SLICED_VBI_OUTPUT))
324 vbifmt->io_size = sizeof(struct v4l2_sliced_vbi_data) * 36;
326 vbifmt->service_lines[0][21] = V4L2_SLICED_CAPTION_525;
327 vbifmt->service_lines[1][21] = V4L2_SLICED_CAPTION_525;
329 vbifmt->service_lines[0][23] = V4L2_SLICED_WSS_625;
330 vbifmt->service_lines[0][16] = V4L2_SLICED_VPS;
332 vbifmt->service_set = ivtv_get_service_set(vbifmt);
336 static int ivtv_g_fmt_vid_cap(struct file *file, void *fh, struct v4l2_format *fmt)
338 struct ivtv_open_id *id = fh;
339 struct ivtv *itv = id->itv;
340 struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
342 pixfmt->width = itv->params.width;
343 pixfmt->height = itv->params.height;
344 pixfmt->colorspace = V4L2_COLORSPACE_SMPTE170M;
345 pixfmt->field = V4L2_FIELD_INTERLACED;
347 if (id->type == IVTV_ENC_STREAM_TYPE_YUV) {
348 pixfmt->pixelformat = V4L2_PIX_FMT_HM12;
349 /* YUV size is (Y=(h*720) + UV=(h*(720/2))) */
350 pixfmt->sizeimage = pixfmt->height * 720 * 3 / 2;
351 pixfmt->bytesperline = 720;
353 pixfmt->pixelformat = V4L2_PIX_FMT_MPEG;
354 pixfmt->sizeimage = 128 * 1024;
355 pixfmt->bytesperline = 0;
360 static int ivtv_g_fmt_vbi_cap(struct file *file, void *fh, struct v4l2_format *fmt)
362 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
363 struct v4l2_vbi_format *vbifmt = &fmt->fmt.vbi;
365 vbifmt->sampling_rate = 27000000;
366 vbifmt->offset = 248;
367 vbifmt->samples_per_line = itv->vbi.raw_decoder_line_size - 4;
368 vbifmt->sample_format = V4L2_PIX_FMT_GREY;
369 vbifmt->start[0] = itv->vbi.start[0];
370 vbifmt->start[1] = itv->vbi.start[1];
371 vbifmt->count[0] = vbifmt->count[1] = itv->vbi.count;
373 vbifmt->reserved[0] = 0;
374 vbifmt->reserved[1] = 0;
378 static int ivtv_g_fmt_sliced_vbi_cap(struct file *file, void *fh, struct v4l2_format *fmt)
380 struct v4l2_sliced_vbi_format *vbifmt = &fmt->fmt.sliced;
381 struct ivtv_open_id *id = fh;
382 struct ivtv *itv = id->itv;
384 vbifmt->reserved[0] = 0;
385 vbifmt->reserved[1] = 0;
386 vbifmt->io_size = sizeof(struct v4l2_sliced_vbi_data) * 36;
388 if (id->type == IVTV_DEC_STREAM_TYPE_VBI) {
389 vbifmt->service_set = itv->is_50hz ? V4L2_SLICED_VBI_625 :
391 ivtv_expand_service_set(vbifmt, itv->is_50hz);
395 v4l2_subdev_call(itv->sd_video, vbi, g_sliced_fmt, vbifmt);
396 vbifmt->service_set = ivtv_get_service_set(vbifmt);
400 static int ivtv_g_fmt_vid_out(struct file *file, void *fh, struct v4l2_format *fmt)
402 struct ivtv_open_id *id = fh;
403 struct ivtv *itv = id->itv;
404 struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
406 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
408 pixfmt->width = itv->main_rect.width;
409 pixfmt->height = itv->main_rect.height;
410 pixfmt->colorspace = V4L2_COLORSPACE_SMPTE170M;
411 pixfmt->field = V4L2_FIELD_INTERLACED;
413 if (id->type == IVTV_DEC_STREAM_TYPE_YUV) {
414 switch (itv->yuv_info.lace_mode & IVTV_YUV_MODE_MASK) {
415 case IVTV_YUV_MODE_INTERLACED:
416 pixfmt->field = (itv->yuv_info.lace_mode & IVTV_YUV_SYNC_MASK) ?
417 V4L2_FIELD_INTERLACED_BT : V4L2_FIELD_INTERLACED_TB;
419 case IVTV_YUV_MODE_PROGRESSIVE:
420 pixfmt->field = V4L2_FIELD_NONE;
423 pixfmt->field = V4L2_FIELD_ANY;
426 pixfmt->pixelformat = V4L2_PIX_FMT_HM12;
427 pixfmt->bytesperline = 720;
428 pixfmt->width = itv->yuv_info.v4l2_src_w;
429 pixfmt->height = itv->yuv_info.v4l2_src_h;
430 /* YUV size is (Y=(h*w) + UV=(h*(w/2))) */
432 1080 * ((pixfmt->height + 31) & ~31);
434 pixfmt->pixelformat = V4L2_PIX_FMT_MPEG;
435 pixfmt->sizeimage = 128 * 1024;
436 pixfmt->bytesperline = 0;
441 static int ivtv_g_fmt_vid_out_overlay(struct file *file, void *fh, struct v4l2_format *fmt)
443 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
444 struct v4l2_window *winfmt = &fmt->fmt.win;
446 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
448 winfmt->chromakey = itv->osd_chroma_key;
449 winfmt->global_alpha = itv->osd_global_alpha;
450 winfmt->field = V4L2_FIELD_INTERLACED;
451 winfmt->clips = NULL;
452 winfmt->clipcount = 0;
453 winfmt->bitmap = NULL;
454 winfmt->w.top = winfmt->w.left = 0;
455 winfmt->w.width = itv->osd_rect.width;
456 winfmt->w.height = itv->osd_rect.height;
460 static int ivtv_try_fmt_sliced_vbi_out(struct file *file, void *fh, struct v4l2_format *fmt)
462 return ivtv_g_fmt_sliced_vbi_out(file, fh, fmt);
465 static int ivtv_try_fmt_vid_cap(struct file *file, void *fh, struct v4l2_format *fmt)
467 struct ivtv_open_id *id = fh;
468 struct ivtv *itv = id->itv;
469 int w = fmt->fmt.pix.width;
470 int h = fmt->fmt.pix.height;
475 if (id->type == IVTV_ENC_STREAM_TYPE_YUV) {
476 /* YUV height must be a multiple of 32 */
480 h = min(h, itv->is_50hz ? 576 : 480);
482 ivtv_g_fmt_vid_cap(file, fh, fmt);
483 fmt->fmt.pix.width = w;
484 fmt->fmt.pix.height = h;
488 static int ivtv_try_fmt_vbi_cap(struct file *file, void *fh, struct v4l2_format *fmt)
490 return ivtv_g_fmt_vbi_cap(file, fh, fmt);
493 static int ivtv_try_fmt_sliced_vbi_cap(struct file *file, void *fh, struct v4l2_format *fmt)
495 struct v4l2_sliced_vbi_format *vbifmt = &fmt->fmt.sliced;
496 struct ivtv_open_id *id = fh;
497 struct ivtv *itv = id->itv;
499 if (id->type == IVTV_DEC_STREAM_TYPE_VBI)
500 return ivtv_g_fmt_sliced_vbi_cap(file, fh, fmt);
502 /* set sliced VBI capture format */
503 vbifmt->io_size = sizeof(struct v4l2_sliced_vbi_data) * 36;
504 vbifmt->reserved[0] = 0;
505 vbifmt->reserved[1] = 0;
507 if (vbifmt->service_set)
508 ivtv_expand_service_set(vbifmt, itv->is_50hz);
509 check_service_set(vbifmt, itv->is_50hz);
510 vbifmt->service_set = ivtv_get_service_set(vbifmt);
514 static int ivtv_try_fmt_vid_out(struct file *file, void *fh, struct v4l2_format *fmt)
516 struct ivtv_open_id *id = fh;
517 s32 w = fmt->fmt.pix.width;
518 s32 h = fmt->fmt.pix.height;
519 int field = fmt->fmt.pix.field;
520 int ret = ivtv_g_fmt_vid_out(file, fh, fmt);
524 /* Why can the height be 576 even when the output is NTSC?
526 Internally the buffers of the PVR350 are always set to 720x576. The
527 decoded video frame will always be placed in the top left corner of
528 this buffer. For any video which is not 720x576, the buffer will
529 then be cropped to remove the unused right and lower areas, with
530 the remaining image being scaled by the hardware to fit the display
531 area. The video can be scaled both up and down, so a 720x480 video
532 can be displayed full-screen on PAL and a 720x576 video can be
533 displayed without cropping on NTSC.
535 Note that the scaling only occurs on the video stream, the osd
536 resolution is locked to the broadcast standard and not scaled.
538 Thanks to Ian Armstrong for this explanation. */
541 if (id->type == IVTV_DEC_STREAM_TYPE_YUV)
542 fmt->fmt.pix.field = field;
543 fmt->fmt.pix.width = w;
544 fmt->fmt.pix.height = h;
548 static int ivtv_try_fmt_vid_out_overlay(struct file *file, void *fh, struct v4l2_format *fmt)
550 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
551 u32 chromakey = fmt->fmt.win.chromakey;
552 u8 global_alpha = fmt->fmt.win.global_alpha;
554 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
556 ivtv_g_fmt_vid_out_overlay(file, fh, fmt);
557 fmt->fmt.win.chromakey = chromakey;
558 fmt->fmt.win.global_alpha = global_alpha;
562 static int ivtv_s_fmt_sliced_vbi_out(struct file *file, void *fh, struct v4l2_format *fmt)
564 return ivtv_g_fmt_sliced_vbi_out(file, fh, fmt);
567 static int ivtv_s_fmt_vid_cap(struct file *file, void *fh, struct v4l2_format *fmt)
569 struct ivtv_open_id *id = fh;
570 struct ivtv *itv = id->itv;
571 struct cx2341x_mpeg_params *p = &itv->params;
572 int ret = ivtv_try_fmt_vid_cap(file, fh, fmt);
573 int w = fmt->fmt.pix.width;
574 int h = fmt->fmt.pix.height;
579 if (p->width == w && p->height == h)
582 if (atomic_read(&itv->capturing) > 0)
587 if (p->video_encoding == V4L2_MPEG_VIDEO_ENCODING_MPEG_1)
588 fmt->fmt.pix.width /= 2;
589 v4l2_subdev_call(itv->sd_video, video, s_fmt, fmt);
590 return ivtv_g_fmt_vid_cap(file, fh, fmt);
593 static int ivtv_s_fmt_vbi_cap(struct file *file, void *fh, struct v4l2_format *fmt)
595 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
597 if (!ivtv_raw_vbi(itv) && atomic_read(&itv->capturing) > 0)
599 itv->vbi.sliced_in->service_set = 0;
600 itv->vbi.in.type = V4L2_BUF_TYPE_VBI_CAPTURE;
601 v4l2_subdev_call(itv->sd_video, vbi, s_raw_fmt, &fmt->fmt.vbi);
602 return ivtv_g_fmt_vbi_cap(file, fh, fmt);
605 static int ivtv_s_fmt_sliced_vbi_cap(struct file *file, void *fh, struct v4l2_format *fmt)
607 struct v4l2_sliced_vbi_format *vbifmt = &fmt->fmt.sliced;
608 struct ivtv_open_id *id = fh;
609 struct ivtv *itv = id->itv;
610 int ret = ivtv_try_fmt_sliced_vbi_cap(file, fh, fmt);
612 if (ret || id->type == IVTV_DEC_STREAM_TYPE_VBI)
615 check_service_set(vbifmt, itv->is_50hz);
616 if (ivtv_raw_vbi(itv) && atomic_read(&itv->capturing) > 0)
618 itv->vbi.in.type = V4L2_BUF_TYPE_SLICED_VBI_CAPTURE;
619 v4l2_subdev_call(itv->sd_video, vbi, s_sliced_fmt, vbifmt);
620 memcpy(itv->vbi.sliced_in, vbifmt, sizeof(*itv->vbi.sliced_in));
624 static int ivtv_s_fmt_vid_out(struct file *file, void *fh, struct v4l2_format *fmt)
626 struct ivtv_open_id *id = fh;
627 struct ivtv *itv = id->itv;
628 struct yuv_playback_info *yi = &itv->yuv_info;
629 int ret = ivtv_try_fmt_vid_out(file, fh, fmt);
634 if (id->type != IVTV_DEC_STREAM_TYPE_YUV)
637 /* Return now if we already have some frame data */
641 yi->v4l2_src_w = fmt->fmt.pix.width;
642 yi->v4l2_src_h = fmt->fmt.pix.height;
644 switch (fmt->fmt.pix.field) {
645 case V4L2_FIELD_NONE:
646 yi->lace_mode = IVTV_YUV_MODE_PROGRESSIVE;
649 yi->lace_mode = IVTV_YUV_MODE_AUTO;
651 case V4L2_FIELD_INTERLACED_BT:
653 IVTV_YUV_MODE_INTERLACED|IVTV_YUV_SYNC_ODD;
655 case V4L2_FIELD_INTERLACED_TB:
657 yi->lace_mode = IVTV_YUV_MODE_INTERLACED;
660 yi->lace_sync_field = (yi->lace_mode & IVTV_YUV_SYNC_MASK) == IVTV_YUV_SYNC_EVEN ? 0 : 1;
662 if (test_bit(IVTV_F_I_DEC_YUV, &itv->i_flags))
663 itv->dma_data_req_size =
664 1080 * ((yi->v4l2_src_h + 31) & ~31);
669 static int ivtv_s_fmt_vid_out_overlay(struct file *file, void *fh, struct v4l2_format *fmt)
671 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
672 int ret = ivtv_try_fmt_vid_out_overlay(file, fh, fmt);
675 itv->osd_chroma_key = fmt->fmt.win.chromakey;
676 itv->osd_global_alpha = fmt->fmt.win.global_alpha;
677 ivtv_set_osd_alpha(itv);
682 static int ivtv_g_chip_ident(struct file *file, void *fh, struct v4l2_dbg_chip_ident *chip)
684 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
686 chip->ident = V4L2_IDENT_NONE;
688 if (chip->match.type == V4L2_CHIP_MATCH_HOST) {
689 if (v4l2_chip_match_host(&chip->match))
690 chip->ident = itv->has_cx23415 ? V4L2_IDENT_CX23415 : V4L2_IDENT_CX23416;
693 if (chip->match.type != V4L2_CHIP_MATCH_I2C_DRIVER &&
694 chip->match.type != V4L2_CHIP_MATCH_I2C_ADDR)
696 /* TODO: is this correct? */
697 return ivtv_call_all_err(itv, core, g_chip_ident, chip);
700 #ifdef CONFIG_VIDEO_ADV_DEBUG
701 static int ivtv_itvc(struct ivtv *itv, unsigned int cmd, void *arg)
703 struct v4l2_dbg_register *regs = arg;
704 volatile u8 __iomem *reg_start;
706 if (!capable(CAP_SYS_ADMIN))
708 if (regs->reg >= IVTV_REG_OFFSET && regs->reg < IVTV_REG_OFFSET + IVTV_REG_SIZE)
709 reg_start = itv->reg_mem - IVTV_REG_OFFSET;
710 else if (itv->has_cx23415 && regs->reg >= IVTV_DECODER_OFFSET &&
711 regs->reg < IVTV_DECODER_OFFSET + IVTV_DECODER_SIZE)
712 reg_start = itv->dec_mem - IVTV_DECODER_OFFSET;
713 else if (regs->reg < IVTV_ENCODER_SIZE)
714 reg_start = itv->enc_mem;
719 if (cmd == VIDIOC_DBG_G_REGISTER)
720 regs->val = readl(regs->reg + reg_start);
722 writel(regs->val, regs->reg + reg_start);
726 static int ivtv_g_register(struct file *file, void *fh, struct v4l2_dbg_register *reg)
728 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
730 if (v4l2_chip_match_host(®->match))
731 return ivtv_itvc(itv, VIDIOC_DBG_G_REGISTER, reg);
732 /* TODO: subdev errors should not be ignored, this should become a
733 subdev helper function. */
734 ivtv_call_all(itv, core, g_register, reg);
738 static int ivtv_s_register(struct file *file, void *fh, struct v4l2_dbg_register *reg)
740 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
742 if (v4l2_chip_match_host(®->match))
743 return ivtv_itvc(itv, VIDIOC_DBG_S_REGISTER, reg);
744 /* TODO: subdev errors should not be ignored, this should become a
745 subdev helper function. */
746 ivtv_call_all(itv, core, s_register, reg);
751 static int ivtv_g_priority(struct file *file, void *fh, enum v4l2_priority *p)
753 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
755 *p = v4l2_prio_max(&itv->prio);
760 static int ivtv_s_priority(struct file *file, void *fh, enum v4l2_priority prio)
762 struct ivtv_open_id *id = fh;
763 struct ivtv *itv = id->itv;
765 return v4l2_prio_change(&itv->prio, &id->prio, prio);
768 static int ivtv_querycap(struct file *file, void *fh, struct v4l2_capability *vcap)
770 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
772 strlcpy(vcap->driver, IVTV_DRIVER_NAME, sizeof(vcap->driver));
773 strlcpy(vcap->card, itv->card_name, sizeof(vcap->card));
774 snprintf(vcap->bus_info, sizeof(vcap->bus_info), "PCI:%s", pci_name(itv->pdev));
775 vcap->version = IVTV_DRIVER_VERSION; /* version */
776 vcap->capabilities = itv->v4l2_cap; /* capabilities */
780 static int ivtv_enumaudio(struct file *file, void *fh, struct v4l2_audio *vin)
782 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
784 return ivtv_get_audio_input(itv, vin->index, vin);
787 static int ivtv_g_audio(struct file *file, void *fh, struct v4l2_audio *vin)
789 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
791 vin->index = itv->audio_input;
792 return ivtv_get_audio_input(itv, vin->index, vin);
795 static int ivtv_s_audio(struct file *file, void *fh, struct v4l2_audio *vout)
797 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
799 if (vout->index >= itv->nof_audio_inputs)
802 itv->audio_input = vout->index;
803 ivtv_audio_set_io(itv);
808 static int ivtv_enumaudout(struct file *file, void *fh, struct v4l2_audioout *vin)
810 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
812 /* set it to defaults from our table */
813 return ivtv_get_audio_output(itv, vin->index, vin);
816 static int ivtv_g_audout(struct file *file, void *fh, struct v4l2_audioout *vin)
818 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
821 return ivtv_get_audio_output(itv, vin->index, vin);
824 static int ivtv_s_audout(struct file *file, void *fh, struct v4l2_audioout *vout)
826 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
828 return ivtv_get_audio_output(itv, vout->index, vout);
831 static int ivtv_enum_input(struct file *file, void *fh, struct v4l2_input *vin)
833 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
835 /* set it to defaults from our table */
836 return ivtv_get_input(itv, vin->index, vin);
839 static int ivtv_enum_output(struct file *file, void *fh, struct v4l2_output *vout)
841 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
843 return ivtv_get_output(itv, vout->index, vout);
846 static int ivtv_cropcap(struct file *file, void *fh, struct v4l2_cropcap *cropcap)
848 struct ivtv_open_id *id = fh;
849 struct ivtv *itv = id->itv;
850 struct yuv_playback_info *yi = &itv->yuv_info;
853 streamtype = id->type;
855 if (cropcap->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
857 cropcap->bounds.top = cropcap->bounds.left = 0;
858 cropcap->bounds.width = 720;
859 if (cropcap->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
860 cropcap->bounds.height = itv->is_50hz ? 576 : 480;
861 cropcap->pixelaspect.numerator = itv->is_50hz ? 59 : 10;
862 cropcap->pixelaspect.denominator = itv->is_50hz ? 54 : 11;
863 } else if (streamtype == IVTV_DEC_STREAM_TYPE_YUV) {
865 cropcap->bounds.width = yi->osd_full_w;
866 cropcap->bounds.height = yi->osd_full_h;
868 cropcap->bounds.width = 720;
869 cropcap->bounds.height =
870 itv->is_out_50hz ? 576 : 480;
872 cropcap->pixelaspect.numerator = itv->is_out_50hz ? 59 : 10;
873 cropcap->pixelaspect.denominator = itv->is_out_50hz ? 54 : 11;
875 cropcap->bounds.height = itv->is_out_50hz ? 576 : 480;
876 cropcap->pixelaspect.numerator = itv->is_out_50hz ? 59 : 10;
877 cropcap->pixelaspect.denominator = itv->is_out_50hz ? 54 : 11;
879 cropcap->defrect = cropcap->bounds;
883 static int ivtv_s_crop(struct file *file, void *fh, struct v4l2_crop *crop)
885 struct ivtv_open_id *id = fh;
886 struct ivtv *itv = id->itv;
887 struct yuv_playback_info *yi = &itv->yuv_info;
890 streamtype = id->type;
892 if (crop->type == V4L2_BUF_TYPE_VIDEO_OUTPUT &&
893 (itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT)) {
894 if (streamtype == IVTV_DEC_STREAM_TYPE_YUV) {
895 yi->main_rect = crop->c;
898 if (!ivtv_vapi(itv, CX2341X_OSD_SET_FRAMEBUFFER_WINDOW, 4,
899 crop->c.width, crop->c.height, crop->c.left, crop->c.top)) {
900 itv->main_rect = crop->c;
909 static int ivtv_g_crop(struct file *file, void *fh, struct v4l2_crop *crop)
911 struct ivtv_open_id *id = fh;
912 struct ivtv *itv = id->itv;
913 struct yuv_playback_info *yi = &itv->yuv_info;
916 streamtype = id->type;
918 if (crop->type == V4L2_BUF_TYPE_VIDEO_OUTPUT &&
919 (itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT)) {
920 if (streamtype == IVTV_DEC_STREAM_TYPE_YUV)
921 crop->c = yi->main_rect;
923 crop->c = itv->main_rect;
929 static int ivtv_enum_fmt_vid_cap(struct file *file, void *fh, struct v4l2_fmtdesc *fmt)
931 static struct v4l2_fmtdesc formats[] = {
933 "HM12 (YUV 4:2:0)", V4L2_PIX_FMT_HM12,
936 { 1, 0, V4L2_FMT_FLAG_COMPRESSED,
937 "MPEG", V4L2_PIX_FMT_MPEG,
941 enum v4l2_buf_type type = fmt->type;
946 *fmt = formats[fmt->index];
951 static int ivtv_enum_fmt_vid_out(struct file *file, void *fh, struct v4l2_fmtdesc *fmt)
953 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
955 static struct v4l2_fmtdesc formats[] = {
957 "HM12 (YUV 4:2:0)", V4L2_PIX_FMT_HM12,
960 { 1, 0, V4L2_FMT_FLAG_COMPRESSED,
961 "MPEG", V4L2_PIX_FMT_MPEG,
965 enum v4l2_buf_type type = fmt->type;
967 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
973 *fmt = formats[fmt->index];
979 static int ivtv_g_input(struct file *file, void *fh, unsigned int *i)
981 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
983 *i = itv->active_input;
988 int ivtv_s_input(struct file *file, void *fh, unsigned int inp)
990 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
992 if (inp < 0 || inp >= itv->nof_inputs)
995 if (inp == itv->active_input) {
996 IVTV_DEBUG_INFO("Input unchanged\n");
1000 if (atomic_read(&itv->capturing) > 0) {
1004 IVTV_DEBUG_INFO("Changing input from %d to %d\n",
1005 itv->active_input, inp);
1007 itv->active_input = inp;
1008 /* Set the audio input to whatever is appropriate for the
1010 itv->audio_input = itv->card->video_inputs[inp].audio_index;
1012 /* prevent others from messing with the streams until
1013 we're finished changing inputs. */
1015 ivtv_video_set_io(itv);
1016 ivtv_audio_set_io(itv);
1022 static int ivtv_g_output(struct file *file, void *fh, unsigned int *i)
1024 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
1026 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
1029 *i = itv->active_output;
1034 static int ivtv_s_output(struct file *file, void *fh, unsigned int outp)
1036 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
1038 if (outp >= itv->card->nof_outputs)
1041 if (outp == itv->active_output) {
1042 IVTV_DEBUG_INFO("Output unchanged\n");
1045 IVTV_DEBUG_INFO("Changing output from %d to %d\n",
1046 itv->active_output, outp);
1048 itv->active_output = outp;
1049 ivtv_call_hw(itv, IVTV_HW_SAA7127, video, s_routing,
1050 SAA7127_INPUT_TYPE_NORMAL,
1051 itv->card->video_outputs[outp].video_output, 0);
1056 static int ivtv_g_frequency(struct file *file, void *fh, struct v4l2_frequency *vf)
1058 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
1063 ivtv_call_all(itv, tuner, g_frequency, vf);
1067 int ivtv_s_frequency(struct file *file, void *fh, struct v4l2_frequency *vf)
1069 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
1075 IVTV_DEBUG_INFO("v4l2 ioctl: set frequency %d\n", vf->frequency);
1076 ivtv_call_all(itv, tuner, s_frequency, vf);
1081 static int ivtv_g_std(struct file *file, void *fh, v4l2_std_id *std)
1083 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
1089 int ivtv_s_std(struct file *file, void *fh, v4l2_std_id *std)
1092 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
1093 struct yuv_playback_info *yi = &itv->yuv_info;
1096 if ((*std & V4L2_STD_ALL) == 0)
1099 if (*std == itv->std)
1102 if (test_bit(IVTV_F_I_RADIO_USER, &itv->i_flags) ||
1103 atomic_read(&itv->capturing) > 0 ||
1104 atomic_read(&itv->decoding) > 0) {
1105 /* Switching standard would turn off the radio or mess
1106 with already running streams, prevent that by
1112 itv->is_60hz = (*std & V4L2_STD_525_60) ? 1 : 0;
1113 itv->params.is_50hz = itv->is_50hz = !itv->is_60hz;
1114 itv->params.width = 720;
1115 itv->params.height = itv->is_50hz ? 576 : 480;
1116 itv->vbi.count = itv->is_50hz ? 18 : 12;
1117 itv->vbi.start[0] = itv->is_50hz ? 6 : 10;
1118 itv->vbi.start[1] = itv->is_50hz ? 318 : 273;
1120 if (itv->hw_flags & IVTV_HW_CX25840)
1121 itv->vbi.sliced_decoder_line_size = itv->is_60hz ? 272 : 284;
1123 IVTV_DEBUG_INFO("Switching standard to %llx.\n", (unsigned long long)itv->std);
1126 ivtv_call_all(itv, core, s_std, itv->std);
1128 if (itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT) {
1129 /* set display standard */
1130 itv->std_out = *std;
1131 itv->is_out_60hz = itv->is_60hz;
1132 itv->is_out_50hz = itv->is_50hz;
1133 ivtv_call_all(itv, video, s_std_output, itv->std_out);
1136 * The next firmware call is time sensitive. Time it to
1137 * avoid risk of a hard lock, by trying to ensure the call
1138 * happens within the first 100 lines of the top field.
1139 * Make 4 attempts to sync to the decoder before giving up.
1141 for (f = 0; f < 4; f++) {
1142 prepare_to_wait(&itv->vsync_waitq, &wait,
1143 TASK_UNINTERRUPTIBLE);
1144 if ((read_reg(IVTV_REG_DEC_LINE_FIELD) >> 16) < 100)
1146 schedule_timeout(msecs_to_jiffies(25));
1148 finish_wait(&itv->vsync_waitq, &wait);
1151 IVTV_WARN("Mode change failed to sync to decoder\n");
1153 ivtv_vapi(itv, CX2341X_DEC_SET_STANDARD, 1, itv->is_out_50hz);
1154 itv->main_rect.left = itv->main_rect.top = 0;
1155 itv->main_rect.width = 720;
1156 itv->main_rect.height = itv->params.height;
1157 ivtv_vapi(itv, CX2341X_OSD_SET_FRAMEBUFFER_WINDOW, 4,
1158 720, itv->main_rect.height, 0, 0);
1159 yi->main_rect = itv->main_rect;
1160 if (!itv->osd_info) {
1161 yi->osd_full_w = 720;
1162 yi->osd_full_h = itv->is_out_50hz ? 576 : 480;
1168 static int ivtv_s_tuner(struct file *file, void *fh, struct v4l2_tuner *vt)
1170 struct ivtv_open_id *id = fh;
1171 struct ivtv *itv = id->itv;
1176 ivtv_call_all(itv, tuner, s_tuner, vt);
1181 static int ivtv_g_tuner(struct file *file, void *fh, struct v4l2_tuner *vt)
1183 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
1188 ivtv_call_all(itv, tuner, g_tuner, vt);
1190 if (test_bit(IVTV_F_I_RADIO_USER, &itv->i_flags)) {
1191 strlcpy(vt->name, "ivtv Radio Tuner", sizeof(vt->name));
1192 vt->type = V4L2_TUNER_RADIO;
1194 strlcpy(vt->name, "ivtv TV Tuner", sizeof(vt->name));
1195 vt->type = V4L2_TUNER_ANALOG_TV;
1201 static int ivtv_g_sliced_vbi_cap(struct file *file, void *fh, struct v4l2_sliced_vbi_cap *cap)
1203 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
1204 int set = itv->is_50hz ? V4L2_SLICED_VBI_625 : V4L2_SLICED_VBI_525;
1207 if (cap->type == V4L2_BUF_TYPE_SLICED_VBI_CAPTURE) {
1208 for (f = 0; f < 2; f++) {
1209 for (l = 0; l < 24; l++) {
1210 if (valid_service_line(f, l, itv->is_50hz))
1211 cap->service_lines[f][l] = set;
1216 if (cap->type == V4L2_BUF_TYPE_SLICED_VBI_OUTPUT) {
1217 if (!(itv->v4l2_cap & V4L2_CAP_SLICED_VBI_OUTPUT))
1220 cap->service_lines[0][21] = V4L2_SLICED_CAPTION_525;
1221 cap->service_lines[1][21] = V4L2_SLICED_CAPTION_525;
1223 cap->service_lines[0][23] = V4L2_SLICED_WSS_625;
1224 cap->service_lines[0][16] = V4L2_SLICED_VPS;
1231 static int ivtv_g_enc_index(struct file *file, void *fh, struct v4l2_enc_idx *idx)
1233 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
1234 struct v4l2_enc_idx_entry *e = idx->entry;
1238 entries = (itv->pgm_info_write_idx + IVTV_MAX_PGM_INDEX - itv->pgm_info_read_idx) %
1240 if (entries > V4L2_ENC_IDX_ENTRIES)
1241 entries = V4L2_ENC_IDX_ENTRIES;
1243 for (i = 0; i < entries; i++) {
1244 *e = itv->pgm_info[(itv->pgm_info_read_idx + i) % IVTV_MAX_PGM_INDEX];
1245 if ((e->flags & V4L2_ENC_IDX_FRAME_MASK) <= V4L2_ENC_IDX_FRAME_B) {
1250 itv->pgm_info_read_idx = (itv->pgm_info_read_idx + idx->entries) % IVTV_MAX_PGM_INDEX;
1254 static int ivtv_encoder_cmd(struct file *file, void *fh, struct v4l2_encoder_cmd *enc)
1256 struct ivtv_open_id *id = fh;
1257 struct ivtv *itv = id->itv;
1261 case V4L2_ENC_CMD_START:
1262 IVTV_DEBUG_IOCTL("V4L2_ENC_CMD_START\n");
1264 return ivtv_start_capture(id);
1266 case V4L2_ENC_CMD_STOP:
1267 IVTV_DEBUG_IOCTL("V4L2_ENC_CMD_STOP\n");
1268 enc->flags &= V4L2_ENC_CMD_STOP_AT_GOP_END;
1269 ivtv_stop_capture(id, enc->flags & V4L2_ENC_CMD_STOP_AT_GOP_END);
1272 case V4L2_ENC_CMD_PAUSE:
1273 IVTV_DEBUG_IOCTL("V4L2_ENC_CMD_PAUSE\n");
1276 if (!atomic_read(&itv->capturing))
1278 if (test_and_set_bit(IVTV_F_I_ENC_PAUSED, &itv->i_flags))
1282 ivtv_vapi(itv, CX2341X_ENC_PAUSE_ENCODER, 1, 0);
1285 case V4L2_ENC_CMD_RESUME:
1286 IVTV_DEBUG_IOCTL("V4L2_ENC_CMD_RESUME\n");
1289 if (!atomic_read(&itv->capturing))
1292 if (!test_and_clear_bit(IVTV_F_I_ENC_PAUSED, &itv->i_flags))
1295 ivtv_vapi(itv, CX2341X_ENC_PAUSE_ENCODER, 1, 1);
1299 IVTV_DEBUG_IOCTL("Unknown cmd %d\n", enc->cmd);
1306 static int ivtv_try_encoder_cmd(struct file *file, void *fh, struct v4l2_encoder_cmd *enc)
1308 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
1311 case V4L2_ENC_CMD_START:
1312 IVTV_DEBUG_IOCTL("V4L2_ENC_CMD_START\n");
1316 case V4L2_ENC_CMD_STOP:
1317 IVTV_DEBUG_IOCTL("V4L2_ENC_CMD_STOP\n");
1318 enc->flags &= V4L2_ENC_CMD_STOP_AT_GOP_END;
1321 case V4L2_ENC_CMD_PAUSE:
1322 IVTV_DEBUG_IOCTL("V4L2_ENC_CMD_PAUSE\n");
1326 case V4L2_ENC_CMD_RESUME:
1327 IVTV_DEBUG_IOCTL("V4L2_ENC_CMD_RESUME\n");
1331 IVTV_DEBUG_IOCTL("Unknown cmd %d\n", enc->cmd);
1336 static int ivtv_g_fbuf(struct file *file, void *fh, struct v4l2_framebuffer *fb)
1338 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
1339 u32 data[CX2341X_MBOX_MAX_DATA];
1340 struct yuv_playback_info *yi = &itv->yuv_info;
1343 static u32 pixel_format[16] = {
1344 V4L2_PIX_FMT_PAL8, /* Uses a 256-entry RGB colormap */
1345 V4L2_PIX_FMT_RGB565,
1346 V4L2_PIX_FMT_RGB555,
1347 V4L2_PIX_FMT_RGB444,
1352 V4L2_PIX_FMT_PAL8, /* Uses a 256-entry YUV colormap */
1353 V4L2_PIX_FMT_YUV565,
1354 V4L2_PIX_FMT_YUV555,
1355 V4L2_PIX_FMT_YUV444,
1362 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT_OVERLAY))
1364 if (!itv->osd_video_pbase)
1367 fb->capability = V4L2_FBUF_CAP_EXTERNOVERLAY | V4L2_FBUF_CAP_CHROMAKEY |
1368 V4L2_FBUF_CAP_GLOBAL_ALPHA;
1370 ivtv_vapi_result(itv, data, CX2341X_OSD_GET_STATE, 0);
1371 data[0] |= (read_reg(0x2a00) >> 7) & 0x40;
1372 pixfmt = (data[0] >> 3) & 0xf;
1374 fb->fmt.pixelformat = pixel_format[pixfmt];
1375 fb->fmt.width = itv->osd_rect.width;
1376 fb->fmt.height = itv->osd_rect.height;
1377 fb->fmt.field = V4L2_FIELD_INTERLACED;
1378 fb->fmt.bytesperline = fb->fmt.width;
1379 fb->fmt.colorspace = V4L2_COLORSPACE_SMPTE170M;
1380 fb->fmt.field = V4L2_FIELD_INTERLACED;
1382 if (fb->fmt.pixelformat != V4L2_PIX_FMT_PAL8)
1383 fb->fmt.bytesperline *= 2;
1384 if (fb->fmt.pixelformat == V4L2_PIX_FMT_RGB32 ||
1385 fb->fmt.pixelformat == V4L2_PIX_FMT_YUV32)
1386 fb->fmt.bytesperline *= 2;
1387 fb->fmt.sizeimage = fb->fmt.bytesperline * fb->fmt.height;
1388 fb->base = (void *)itv->osd_video_pbase;
1391 if (itv->osd_chroma_key_state)
1392 fb->flags |= V4L2_FBUF_FLAG_CHROMAKEY;
1394 if (itv->osd_global_alpha_state)
1395 fb->flags |= V4L2_FBUF_FLAG_GLOBAL_ALPHA;
1398 fb->flags |= V4L2_FBUF_FLAG_OVERLAY;
1402 /* no local alpha for RGB565 or unknown formats */
1403 if (pixfmt == 1 || pixfmt > 4)
1406 /* 16-bit formats have inverted local alpha */
1407 if (pixfmt == 2 || pixfmt == 3)
1408 fb->capability |= V4L2_FBUF_CAP_LOCAL_INV_ALPHA;
1410 fb->capability |= V4L2_FBUF_CAP_LOCAL_ALPHA;
1412 if (itv->osd_local_alpha_state) {
1413 /* 16-bit formats have inverted local alpha */
1414 if (pixfmt == 2 || pixfmt == 3)
1415 fb->flags |= V4L2_FBUF_FLAG_LOCAL_INV_ALPHA;
1417 fb->flags |= V4L2_FBUF_FLAG_LOCAL_ALPHA;
1423 static int ivtv_s_fbuf(struct file *file, void *fh, struct v4l2_framebuffer *fb)
1425 struct ivtv_open_id *id = fh;
1426 struct ivtv *itv = id->itv;
1427 struct yuv_playback_info *yi = &itv->yuv_info;
1429 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT_OVERLAY))
1431 if (!itv->osd_video_pbase)
1434 itv->osd_global_alpha_state = (fb->flags & V4L2_FBUF_FLAG_GLOBAL_ALPHA) != 0;
1435 itv->osd_local_alpha_state =
1436 (fb->flags & (V4L2_FBUF_FLAG_LOCAL_ALPHA|V4L2_FBUF_FLAG_LOCAL_INV_ALPHA)) != 0;
1437 itv->osd_chroma_key_state = (fb->flags & V4L2_FBUF_FLAG_CHROMAKEY) != 0;
1438 ivtv_set_osd_alpha(itv);
1439 yi->track_osd = (fb->flags & V4L2_FBUF_FLAG_OVERLAY) != 0;
1440 return ivtv_g_fbuf(file, fh, fb);
1443 static int ivtv_overlay(struct file *file, void *fh, unsigned int on)
1445 struct ivtv_open_id *id = fh;
1446 struct ivtv *itv = id->itv;
1448 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT_OVERLAY))
1451 ivtv_vapi(itv, CX2341X_OSD_SET_STATE, 1, on != 0);
1456 static int ivtv_subscribe_event(struct v4l2_fh *fh, struct v4l2_event_subscription *sub)
1458 switch (sub->type) {
1459 case V4L2_EVENT_VSYNC:
1460 case V4L2_EVENT_EOS:
1465 return v4l2_event_subscribe(fh, sub);
1468 static int ivtv_log_status(struct file *file, void *fh)
1470 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
1471 u32 data[CX2341X_MBOX_MAX_DATA];
1473 int has_output = itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT;
1474 struct v4l2_input vidin;
1475 struct v4l2_audio audin;
1478 IVTV_INFO("================= START STATUS CARD #%d =================\n",
1480 IVTV_INFO("Version: %s Card: %s\n", IVTV_VERSION, itv->card_name);
1481 if (itv->hw_flags & IVTV_HW_TVEEPROM) {
1484 ivtv_read_eeprom(itv, &tv);
1486 ivtv_call_all(itv, core, log_status);
1487 ivtv_get_input(itv, itv->active_input, &vidin);
1488 ivtv_get_audio_input(itv, itv->audio_input, &audin);
1489 IVTV_INFO("Video Input: %s\n", vidin.name);
1490 IVTV_INFO("Audio Input: %s%s\n", audin.name,
1491 (itv->dualwatch_stereo_mode & ~0x300) == 0x200 ? " (Bilingual)" : "");
1493 struct v4l2_output vidout;
1494 struct v4l2_audioout audout;
1495 int mode = itv->output_mode;
1496 static const char * const output_modes[5] = {
1503 static const char * const audio_modes[5] = {
1510 static const char * const alpha_mode[4] = {
1516 static const char * const pixel_format[16] = {
1535 ivtv_get_output(itv, itv->active_output, &vidout);
1536 ivtv_get_audio_output(itv, 0, &audout);
1537 IVTV_INFO("Video Output: %s\n", vidout.name);
1538 IVTV_INFO("Audio Output: %s (Stereo/Bilingual: %s/%s)\n", audout.name,
1539 audio_modes[itv->audio_stereo_mode],
1540 audio_modes[itv->audio_bilingual_mode]);
1541 if (mode < 0 || mode > OUT_PASSTHROUGH)
1543 IVTV_INFO("Output Mode: %s\n", output_modes[mode]);
1544 ivtv_vapi_result(itv, data, CX2341X_OSD_GET_STATE, 0);
1545 data[0] |= (read_reg(0x2a00) >> 7) & 0x40;
1546 IVTV_INFO("Overlay: %s, Alpha: %s, Pixel Format: %s\n",
1547 data[0] & 1 ? "On" : "Off",
1548 alpha_mode[(data[0] >> 1) & 0x3],
1549 pixel_format[(data[0] >> 3) & 0xf]);
1551 IVTV_INFO("Tuner: %s\n",
1552 test_bit(IVTV_F_I_RADIO_USER, &itv->i_flags) ? "Radio" : "TV");
1553 cx2341x_log_status(&itv->params, itv->v4l2_dev.name);
1554 IVTV_INFO("Status flags: 0x%08lx\n", itv->i_flags);
1555 for (i = 0; i < IVTV_MAX_STREAMS; i++) {
1556 struct ivtv_stream *s = &itv->streams[i];
1558 if (s->vdev == NULL || s->buffers == 0)
1560 IVTV_INFO("Stream %s: status 0x%04lx, %d%% of %d KiB (%d buffers) in use\n", s->name, s->s_flags,
1561 (s->buffers - s->q_free.buffers) * 100 / s->buffers,
1562 (s->buffers * s->buf_size) / 1024, s->buffers);
1565 IVTV_INFO("Read MPG/VBI: %lld/%lld bytes\n",
1566 (long long)itv->mpg_data_received,
1567 (long long)itv->vbi_data_inserted);
1568 IVTV_INFO("================== END STATUS CARD #%d ==================\n",
1574 static int ivtv_decoder_ioctls(struct file *filp, unsigned int cmd, void *arg)
1576 struct ivtv_open_id *id = fh2id(filp->private_data);
1577 struct ivtv *itv = id->itv;
1578 int nonblocking = filp->f_flags & O_NONBLOCK;
1579 struct ivtv_stream *s = &itv->streams[id->type];
1580 unsigned long iarg = (unsigned long)arg;
1583 case IVTV_IOC_DMA_FRAME: {
1584 struct ivtv_dma_frame *args = arg;
1586 IVTV_DEBUG_IOCTL("IVTV_IOC_DMA_FRAME\n");
1587 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
1589 if (args->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
1591 if (itv->output_mode == OUT_UDMA_YUV && args->y_source == NULL)
1593 if (ivtv_start_decoding(id, id->type)) {
1596 if (ivtv_set_output_mode(itv, OUT_UDMA_YUV) != OUT_UDMA_YUV) {
1597 ivtv_release_stream(s);
1600 /* Mark that this file handle started the UDMA_YUV mode */
1602 if (args->y_source == NULL)
1604 return ivtv_yuv_prep_frame(itv, args);
1607 case VIDEO_GET_PTS: {
1608 u32 data[CX2341X_MBOX_MAX_DATA];
1611 IVTV_DEBUG_IOCTL("VIDEO_GET_PTS\n");
1612 if (s->type < IVTV_DEC_STREAM_TYPE_MPG) {
1616 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
1619 if (test_bit(IVTV_F_I_VALID_DEC_TIMINGS, &itv->i_flags)) {
1620 *pts = (u64) ((u64)itv->last_dec_timing[2] << 32) |
1621 (u64)itv->last_dec_timing[1];
1625 if (atomic_read(&itv->decoding)) {
1626 if (ivtv_api(itv, CX2341X_DEC_GET_TIMING_INFO, 5, data)) {
1627 IVTV_DEBUG_WARN("GET_TIMING: couldn't read clock\n");
1630 memcpy(itv->last_dec_timing, data, sizeof(itv->last_dec_timing));
1631 set_bit(IVTV_F_I_VALID_DEC_TIMINGS, &itv->i_flags);
1632 *pts = (u64) ((u64) data[2] << 32) | (u64) data[1];
1633 /*timing->scr = (u64) (((u64) data[4] << 32) | (u64) (data[3]));*/
1638 case VIDEO_GET_FRAME_COUNT: {
1639 u32 data[CX2341X_MBOX_MAX_DATA];
1642 IVTV_DEBUG_IOCTL("VIDEO_GET_FRAME_COUNT\n");
1643 if (s->type < IVTV_DEC_STREAM_TYPE_MPG) {
1647 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
1650 if (test_bit(IVTV_F_I_VALID_DEC_TIMINGS, &itv->i_flags)) {
1651 *frame = itv->last_dec_timing[0];
1655 if (atomic_read(&itv->decoding)) {
1656 if (ivtv_api(itv, CX2341X_DEC_GET_TIMING_INFO, 5, data)) {
1657 IVTV_DEBUG_WARN("GET_TIMING: couldn't read clock\n");
1660 memcpy(itv->last_dec_timing, data, sizeof(itv->last_dec_timing));
1661 set_bit(IVTV_F_I_VALID_DEC_TIMINGS, &itv->i_flags);
1668 struct video_command vc;
1670 IVTV_DEBUG_IOCTL("VIDEO_PLAY\n");
1671 memset(&vc, 0, sizeof(vc));
1672 vc.cmd = VIDEO_CMD_PLAY;
1673 return ivtv_video_command(itv, id, &vc, 0);
1677 struct video_command vc;
1679 IVTV_DEBUG_IOCTL("VIDEO_STOP\n");
1680 memset(&vc, 0, sizeof(vc));
1681 vc.cmd = VIDEO_CMD_STOP;
1682 vc.flags = VIDEO_CMD_STOP_TO_BLACK | VIDEO_CMD_STOP_IMMEDIATELY;
1683 return ivtv_video_command(itv, id, &vc, 0);
1686 case VIDEO_FREEZE: {
1687 struct video_command vc;
1689 IVTV_DEBUG_IOCTL("VIDEO_FREEZE\n");
1690 memset(&vc, 0, sizeof(vc));
1691 vc.cmd = VIDEO_CMD_FREEZE;
1692 return ivtv_video_command(itv, id, &vc, 0);
1695 case VIDEO_CONTINUE: {
1696 struct video_command vc;
1698 IVTV_DEBUG_IOCTL("VIDEO_CONTINUE\n");
1699 memset(&vc, 0, sizeof(vc));
1700 vc.cmd = VIDEO_CMD_CONTINUE;
1701 return ivtv_video_command(itv, id, &vc, 0);
1705 case VIDEO_TRY_COMMAND: {
1706 struct video_command *vc = arg;
1707 int try = (cmd == VIDEO_TRY_COMMAND);
1710 IVTV_DEBUG_IOCTL("VIDEO_TRY_COMMAND %d\n", vc->cmd);
1712 IVTV_DEBUG_IOCTL("VIDEO_COMMAND %d\n", vc->cmd);
1713 return ivtv_video_command(itv, id, vc, try);
1716 case VIDEO_GET_EVENT: {
1717 struct video_event *ev = arg;
1720 IVTV_DEBUG_IOCTL("VIDEO_GET_EVENT\n");
1721 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
1723 memset(ev, 0, sizeof(*ev));
1724 set_bit(IVTV_F_I_EV_VSYNC_ENABLED, &itv->i_flags);
1727 if (test_and_clear_bit(IVTV_F_I_EV_DEC_STOPPED, &itv->i_flags))
1728 ev->type = VIDEO_EVENT_DECODER_STOPPED;
1729 else if (test_and_clear_bit(IVTV_F_I_EV_VSYNC, &itv->i_flags)) {
1730 ev->type = VIDEO_EVENT_VSYNC;
1731 ev->u.vsync_field = test_bit(IVTV_F_I_EV_VSYNC_FIELD, &itv->i_flags) ?
1732 VIDEO_VSYNC_FIELD_ODD : VIDEO_VSYNC_FIELD_EVEN;
1733 if (itv->output_mode == OUT_UDMA_YUV &&
1734 (itv->yuv_info.lace_mode & IVTV_YUV_MODE_MASK) ==
1735 IVTV_YUV_MODE_PROGRESSIVE) {
1736 ev->u.vsync_field = VIDEO_VSYNC_FIELD_PROGRESSIVE;
1743 /* Wait for event. Note that serialize_lock is locked,
1744 so to allow other processes to access the driver while
1745 we are waiting unlock first and later lock again. */
1746 mutex_unlock(&itv->serialize_lock);
1747 prepare_to_wait(&itv->event_waitq, &wait, TASK_INTERRUPTIBLE);
1748 if (!test_bit(IVTV_F_I_EV_DEC_STOPPED, &itv->i_flags) &&
1749 !test_bit(IVTV_F_I_EV_VSYNC, &itv->i_flags))
1751 finish_wait(&itv->event_waitq, &wait);
1752 mutex_lock(&itv->serialize_lock);
1753 if (signal_pending(current)) {
1754 /* return if a signal was received */
1755 IVTV_DEBUG_INFO("User stopped wait for event\n");
1762 case VIDEO_SELECT_SOURCE:
1763 IVTV_DEBUG_IOCTL("VIDEO_SELECT_SOURCE\n");
1764 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
1766 return ivtv_passthrough_mode(itv, iarg == VIDEO_SOURCE_DEMUX);
1768 case AUDIO_SET_MUTE:
1769 IVTV_DEBUG_IOCTL("AUDIO_SET_MUTE\n");
1770 itv->speed_mute_audio = iarg;
1773 case AUDIO_CHANNEL_SELECT:
1774 IVTV_DEBUG_IOCTL("AUDIO_CHANNEL_SELECT\n");
1775 if (iarg > AUDIO_STEREO_SWAPPED)
1777 itv->audio_stereo_mode = iarg;
1778 ivtv_vapi(itv, CX2341X_DEC_SET_AUDIO_MODE, 2, itv->audio_bilingual_mode, itv->audio_stereo_mode);
1781 case AUDIO_BILINGUAL_CHANNEL_SELECT:
1782 IVTV_DEBUG_IOCTL("AUDIO_BILINGUAL_CHANNEL_SELECT\n");
1783 if (iarg > AUDIO_STEREO_SWAPPED)
1785 itv->audio_bilingual_mode = iarg;
1786 ivtv_vapi(itv, CX2341X_DEC_SET_AUDIO_MODE, 2, itv->audio_bilingual_mode, itv->audio_stereo_mode);
1795 static long ivtv_default(struct file *file, void *fh, int cmd, void *arg)
1797 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
1800 case VIDIOC_INT_RESET: {
1801 u32 val = *(u32 *)arg;
1803 if ((val == 0 && itv->options.newi2c) || (val & 0x01))
1804 ivtv_reset_ir_gpio(itv);
1806 v4l2_subdev_call(itv->sd_video, core, reset, 0);
1810 case IVTV_IOC_DMA_FRAME:
1812 case VIDEO_GET_FRAME_COUNT:
1813 case VIDEO_GET_EVENT:
1817 case VIDEO_CONTINUE:
1819 case VIDEO_TRY_COMMAND:
1820 case VIDEO_SELECT_SOURCE:
1821 case AUDIO_SET_MUTE:
1822 case AUDIO_CHANNEL_SELECT:
1823 case AUDIO_BILINGUAL_CHANNEL_SELECT:
1824 return ivtv_decoder_ioctls(file, cmd, (void *)arg);
1832 static long ivtv_serialized_ioctl(struct ivtv *itv, struct file *filp,
1833 unsigned int cmd, unsigned long arg)
1835 struct video_device *vfd = video_devdata(filp);
1836 struct ivtv_open_id *id = fh2id(filp->private_data);
1839 /* check priority */
1843 case VIDIOC_S_INPUT:
1844 case VIDIOC_S_OUTPUT:
1845 case VIDIOC_S_TUNER:
1846 case VIDIOC_S_FREQUENCY:
1849 case VIDIOC_S_AUDIO:
1850 case VIDIOC_S_AUDOUT:
1851 case VIDIOC_S_EXT_CTRLS:
1853 case VIDIOC_OVERLAY:
1854 ret = v4l2_prio_check(&itv->prio, id->prio);
1859 if (ivtv_debug & IVTV_DBGFLG_IOCTL)
1860 vfd->debug = V4L2_DEBUG_IOCTL | V4L2_DEBUG_IOCTL_ARG;
1861 ret = video_ioctl2(filp, cmd, arg);
1866 long ivtv_v4l2_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
1868 struct ivtv_open_id *id = fh2id(filp->private_data);
1869 struct ivtv *itv = id->itv;
1872 /* DQEVENT can block, so this should not run with the serialize lock */
1873 if (cmd == VIDIOC_DQEVENT)
1874 return ivtv_serialized_ioctl(itv, filp, cmd, arg);
1875 mutex_lock(&itv->serialize_lock);
1876 res = ivtv_serialized_ioctl(itv, filp, cmd, arg);
1877 mutex_unlock(&itv->serialize_lock);
1881 static const struct v4l2_ioctl_ops ivtv_ioctl_ops = {
1882 .vidioc_querycap = ivtv_querycap,
1883 .vidioc_g_priority = ivtv_g_priority,
1884 .vidioc_s_priority = ivtv_s_priority,
1885 .vidioc_s_audio = ivtv_s_audio,
1886 .vidioc_g_audio = ivtv_g_audio,
1887 .vidioc_enumaudio = ivtv_enumaudio,
1888 .vidioc_s_audout = ivtv_s_audout,
1889 .vidioc_g_audout = ivtv_g_audout,
1890 .vidioc_enum_input = ivtv_enum_input,
1891 .vidioc_enum_output = ivtv_enum_output,
1892 .vidioc_enumaudout = ivtv_enumaudout,
1893 .vidioc_cropcap = ivtv_cropcap,
1894 .vidioc_s_crop = ivtv_s_crop,
1895 .vidioc_g_crop = ivtv_g_crop,
1896 .vidioc_g_input = ivtv_g_input,
1897 .vidioc_s_input = ivtv_s_input,
1898 .vidioc_g_output = ivtv_g_output,
1899 .vidioc_s_output = ivtv_s_output,
1900 .vidioc_g_frequency = ivtv_g_frequency,
1901 .vidioc_s_frequency = ivtv_s_frequency,
1902 .vidioc_s_tuner = ivtv_s_tuner,
1903 .vidioc_g_tuner = ivtv_g_tuner,
1904 .vidioc_g_enc_index = ivtv_g_enc_index,
1905 .vidioc_g_fbuf = ivtv_g_fbuf,
1906 .vidioc_s_fbuf = ivtv_s_fbuf,
1907 .vidioc_g_std = ivtv_g_std,
1908 .vidioc_s_std = ivtv_s_std,
1909 .vidioc_overlay = ivtv_overlay,
1910 .vidioc_log_status = ivtv_log_status,
1911 .vidioc_enum_fmt_vid_cap = ivtv_enum_fmt_vid_cap,
1912 .vidioc_encoder_cmd = ivtv_encoder_cmd,
1913 .vidioc_try_encoder_cmd = ivtv_try_encoder_cmd,
1914 .vidioc_enum_fmt_vid_out = ivtv_enum_fmt_vid_out,
1915 .vidioc_g_fmt_vid_cap = ivtv_g_fmt_vid_cap,
1916 .vidioc_g_fmt_vbi_cap = ivtv_g_fmt_vbi_cap,
1917 .vidioc_g_fmt_sliced_vbi_cap = ivtv_g_fmt_sliced_vbi_cap,
1918 .vidioc_g_fmt_vid_out = ivtv_g_fmt_vid_out,
1919 .vidioc_g_fmt_vid_out_overlay = ivtv_g_fmt_vid_out_overlay,
1920 .vidioc_g_fmt_sliced_vbi_out = ivtv_g_fmt_sliced_vbi_out,
1921 .vidioc_s_fmt_vid_cap = ivtv_s_fmt_vid_cap,
1922 .vidioc_s_fmt_vbi_cap = ivtv_s_fmt_vbi_cap,
1923 .vidioc_s_fmt_sliced_vbi_cap = ivtv_s_fmt_sliced_vbi_cap,
1924 .vidioc_s_fmt_vid_out = ivtv_s_fmt_vid_out,
1925 .vidioc_s_fmt_vid_out_overlay = ivtv_s_fmt_vid_out_overlay,
1926 .vidioc_s_fmt_sliced_vbi_out = ivtv_s_fmt_sliced_vbi_out,
1927 .vidioc_try_fmt_vid_cap = ivtv_try_fmt_vid_cap,
1928 .vidioc_try_fmt_vbi_cap = ivtv_try_fmt_vbi_cap,
1929 .vidioc_try_fmt_sliced_vbi_cap = ivtv_try_fmt_sliced_vbi_cap,
1930 .vidioc_try_fmt_vid_out = ivtv_try_fmt_vid_out,
1931 .vidioc_try_fmt_vid_out_overlay = ivtv_try_fmt_vid_out_overlay,
1932 .vidioc_try_fmt_sliced_vbi_out = ivtv_try_fmt_sliced_vbi_out,
1933 .vidioc_g_sliced_vbi_cap = ivtv_g_sliced_vbi_cap,
1934 .vidioc_g_chip_ident = ivtv_g_chip_ident,
1935 #ifdef CONFIG_VIDEO_ADV_DEBUG
1936 .vidioc_g_register = ivtv_g_register,
1937 .vidioc_s_register = ivtv_s_register,
1939 .vidioc_default = ivtv_default,
1940 .vidioc_queryctrl = ivtv_queryctrl,
1941 .vidioc_querymenu = ivtv_querymenu,
1942 .vidioc_g_ext_ctrls = ivtv_g_ext_ctrls,
1943 .vidioc_s_ext_ctrls = ivtv_s_ext_ctrls,
1944 .vidioc_try_ext_ctrls = ivtv_try_ext_ctrls,
1945 .vidioc_subscribe_event = ivtv_subscribe_event,
1946 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1949 void ivtv_set_funcs(struct video_device *vdev)
1951 vdev->ioctl_ops = &ivtv_ioctl_ops;