Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003     ioctl system call
0004     Copyright (C) 2003-2004  Kevin Thayer <nufan_wfk at yahoo.com>
0005     Copyright (C) 2005-2007  Hans Verkuil <hverkuil@xs4all.nl>
0006 
0007  */
0008 
0009 #include "ivtv-driver.h"
0010 #include "ivtv-version.h"
0011 #include "ivtv-mailbox.h"
0012 #include "ivtv-i2c.h"
0013 #include "ivtv-queue.h"
0014 #include "ivtv-fileops.h"
0015 #include "ivtv-vbi.h"
0016 #include "ivtv-routing.h"
0017 #include "ivtv-streams.h"
0018 #include "ivtv-yuv.h"
0019 #include "ivtv-ioctl.h"
0020 #include "ivtv-gpio.h"
0021 #include "ivtv-controls.h"
0022 #include "ivtv-cards.h"
0023 #include <media/i2c/saa7127.h>
0024 #include <media/tveeprom.h>
0025 #include <media/v4l2-event.h>
0026 
0027 u16 ivtv_service2vbi(int type)
0028 {
0029     switch (type) {
0030         case V4L2_SLICED_TELETEXT_B:
0031             return IVTV_SLICED_TYPE_TELETEXT_B;
0032         case V4L2_SLICED_CAPTION_525:
0033             return IVTV_SLICED_TYPE_CAPTION_525;
0034         case V4L2_SLICED_WSS_625:
0035             return IVTV_SLICED_TYPE_WSS_625;
0036         case V4L2_SLICED_VPS:
0037             return IVTV_SLICED_TYPE_VPS;
0038         default:
0039             return 0;
0040     }
0041 }
0042 
0043 static int valid_service_line(int field, int line, int is_pal)
0044 {
0045     return (is_pal && line >= 6 && (line != 23 || field == 0)) ||
0046            (!is_pal && line >= 10 && line < 22);
0047 }
0048 
0049 static u16 select_service_from_set(int field, int line, u16 set, int is_pal)
0050 {
0051     u16 valid_set = (is_pal ? V4L2_SLICED_VBI_625 : V4L2_SLICED_VBI_525);
0052     int i;
0053 
0054     set = set & valid_set;
0055     if (set == 0 || !valid_service_line(field, line, is_pal)) {
0056         return 0;
0057     }
0058     if (!is_pal) {
0059         if (line == 21 && (set & V4L2_SLICED_CAPTION_525))
0060             return V4L2_SLICED_CAPTION_525;
0061     }
0062     else {
0063         if (line == 16 && field == 0 && (set & V4L2_SLICED_VPS))
0064             return V4L2_SLICED_VPS;
0065         if (line == 23 && field == 0 && (set & V4L2_SLICED_WSS_625))
0066             return V4L2_SLICED_WSS_625;
0067         if (line == 23)
0068             return 0;
0069     }
0070     for (i = 0; i < 32; i++) {
0071         if (BIT(i) & set)
0072             return BIT(i);
0073     }
0074     return 0;
0075 }
0076 
0077 void ivtv_expand_service_set(struct v4l2_sliced_vbi_format *fmt, int is_pal)
0078 {
0079     u16 set = fmt->service_set;
0080     int f, l;
0081 
0082     fmt->service_set = 0;
0083     for (f = 0; f < 2; f++) {
0084         for (l = 0; l < 24; l++) {
0085             fmt->service_lines[f][l] = select_service_from_set(f, l, set, is_pal);
0086         }
0087     }
0088 }
0089 
0090 static void check_service_set(struct v4l2_sliced_vbi_format *fmt, int is_pal)
0091 {
0092     int f, l;
0093 
0094     for (f = 0; f < 2; f++) {
0095         for (l = 0; l < 24; l++) {
0096             fmt->service_lines[f][l] = select_service_from_set(f, l, fmt->service_lines[f][l], is_pal);
0097         }
0098     }
0099 }
0100 
0101 u16 ivtv_get_service_set(struct v4l2_sliced_vbi_format *fmt)
0102 {
0103     int f, l;
0104     u16 set = 0;
0105 
0106     for (f = 0; f < 2; f++) {
0107         for (l = 0; l < 24; l++) {
0108             set |= fmt->service_lines[f][l];
0109         }
0110     }
0111     return set;
0112 }
0113 
0114 void ivtv_set_osd_alpha(struct ivtv *itv)
0115 {
0116     ivtv_vapi(itv, CX2341X_OSD_SET_GLOBAL_ALPHA, 3,
0117         itv->osd_global_alpha_state, itv->osd_global_alpha, !itv->osd_local_alpha_state);
0118     ivtv_vapi(itv, CX2341X_OSD_SET_CHROMA_KEY, 2, itv->osd_chroma_key_state, itv->osd_chroma_key);
0119 }
0120 
0121 int ivtv_set_speed(struct ivtv *itv, int speed)
0122 {
0123     u32 data[CX2341X_MBOX_MAX_DATA];
0124     int single_step = (speed == 1 || speed == -1);
0125     DEFINE_WAIT(wait);
0126 
0127     if (speed == 0) speed = 1000;
0128 
0129     /* No change? */
0130     if (speed == itv->speed && !single_step)
0131         return 0;
0132 
0133     if (single_step && (speed < 0) == (itv->speed < 0)) {
0134         /* Single step video and no need to change direction */
0135         ivtv_vapi(itv, CX2341X_DEC_STEP_VIDEO, 1, 0);
0136         itv->speed = speed;
0137         return 0;
0138     }
0139     if (single_step)
0140         /* Need to change direction */
0141         speed = speed < 0 ? -1000 : 1000;
0142 
0143     data[0] = (speed > 1000 || speed < -1000) ? 0x80000000 : 0;
0144     data[0] |= (speed > 1000 || speed < -1500) ? 0x40000000 : 0;
0145     data[1] = (speed < 0);
0146     data[2] = speed < 0 ? 3 : 7;
0147     data[3] = v4l2_ctrl_g_ctrl(itv->cxhdl.video_b_frames);
0148     data[4] = (speed == 1500 || speed == 500) ? itv->speed_mute_audio : 0;
0149     data[5] = 0;
0150     data[6] = 0;
0151 
0152     if (speed == 1500 || speed == -1500) data[0] |= 1;
0153     else if (speed == 2000 || speed == -2000) data[0] |= 2;
0154     else if (speed > -1000 && speed < 0) data[0] |= (-1000 / speed);
0155     else if (speed < 1000 && speed > 0) data[0] |= (1000 / speed);
0156 
0157     /* If not decoding, just change speed setting */
0158     if (atomic_read(&itv->decoding) > 0) {
0159         int got_sig = 0;
0160 
0161         /* Stop all DMA and decoding activity */
0162         ivtv_vapi(itv, CX2341X_DEC_PAUSE_PLAYBACK, 1, 0);
0163 
0164         /* Wait for any DMA to finish */
0165         mutex_unlock(&itv->serialize_lock);
0166         prepare_to_wait(&itv->dma_waitq, &wait, TASK_INTERRUPTIBLE);
0167         while (test_bit(IVTV_F_I_DMA, &itv->i_flags)) {
0168             got_sig = signal_pending(current);
0169             if (got_sig)
0170                 break;
0171             got_sig = 0;
0172             schedule();
0173         }
0174         finish_wait(&itv->dma_waitq, &wait);
0175         mutex_lock(&itv->serialize_lock);
0176         if (got_sig)
0177             return -EINTR;
0178 
0179         /* Change Speed safely */
0180         ivtv_api(itv, CX2341X_DEC_SET_PLAYBACK_SPEED, 7, data);
0181         IVTV_DEBUG_INFO("Setting Speed to 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x\n",
0182                 data[0], data[1], data[2], data[3], data[4], data[5], data[6]);
0183     }
0184     if (single_step) {
0185         speed = (speed < 0) ? -1 : 1;
0186         ivtv_vapi(itv, CX2341X_DEC_STEP_VIDEO, 1, 0);
0187     }
0188     itv->speed = speed;
0189     return 0;
0190 }
0191 
0192 static int ivtv_validate_speed(int cur_speed, int new_speed)
0193 {
0194     int fact = new_speed < 0 ? -1 : 1;
0195     int s;
0196 
0197     if (cur_speed == 0)
0198         cur_speed = 1000;
0199     if (new_speed < 0)
0200         new_speed = -new_speed;
0201     if (cur_speed < 0)
0202         cur_speed = -cur_speed;
0203 
0204     if (cur_speed <= new_speed) {
0205         if (new_speed > 1500)
0206             return fact * 2000;
0207         if (new_speed > 1000)
0208             return fact * 1500;
0209     }
0210     else {
0211         if (new_speed >= 2000)
0212             return fact * 2000;
0213         if (new_speed >= 1500)
0214             return fact * 1500;
0215         if (new_speed >= 1000)
0216             return fact * 1000;
0217     }
0218     if (new_speed == 0)
0219         return 1000;
0220     if (new_speed == 1 || new_speed == 1000)
0221         return fact * new_speed;
0222 
0223     s = new_speed;
0224     new_speed = 1000 / new_speed;
0225     if (1000 / cur_speed == new_speed)
0226         new_speed += (cur_speed < s) ? -1 : 1;
0227     if (new_speed > 60) return 1000 / (fact * 60);
0228     return 1000 / (fact * new_speed);
0229 }
0230 
0231 static int ivtv_video_command(struct ivtv *itv, struct ivtv_open_id *id,
0232         struct v4l2_decoder_cmd *dc, int try)
0233 {
0234     struct ivtv_stream *s = &itv->streams[IVTV_DEC_STREAM_TYPE_MPG];
0235 
0236     if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
0237         return -EINVAL;
0238 
0239     switch (dc->cmd) {
0240     case V4L2_DEC_CMD_START: {
0241         dc->flags &= V4L2_DEC_CMD_START_MUTE_AUDIO;
0242         dc->start.speed = ivtv_validate_speed(itv->speed, dc->start.speed);
0243         if (dc->start.speed < 0)
0244             dc->start.format = V4L2_DEC_START_FMT_GOP;
0245         else
0246             dc->start.format = V4L2_DEC_START_FMT_NONE;
0247         if (dc->start.speed != 500 && dc->start.speed != 1500)
0248             dc->flags = dc->start.speed == 1000 ? 0 :
0249                     V4L2_DEC_CMD_START_MUTE_AUDIO;
0250         if (try) break;
0251 
0252         itv->speed_mute_audio = dc->flags & V4L2_DEC_CMD_START_MUTE_AUDIO;
0253         if (ivtv_set_output_mode(itv, OUT_MPG) != OUT_MPG)
0254             return -EBUSY;
0255         if (test_and_clear_bit(IVTV_F_I_DEC_PAUSED, &itv->i_flags)) {
0256             /* forces ivtv_set_speed to be called */
0257             itv->speed = 0;
0258         }
0259         return ivtv_start_decoding(id, dc->start.speed);
0260     }
0261 
0262     case V4L2_DEC_CMD_STOP:
0263         dc->flags &= V4L2_DEC_CMD_STOP_IMMEDIATELY | V4L2_DEC_CMD_STOP_TO_BLACK;
0264         if (dc->flags & V4L2_DEC_CMD_STOP_IMMEDIATELY)
0265             dc->stop.pts = 0;
0266         if (try) break;
0267         if (atomic_read(&itv->decoding) == 0)
0268             return 0;
0269         if (itv->output_mode != OUT_MPG)
0270             return -EBUSY;
0271 
0272         itv->output_mode = OUT_NONE;
0273         return ivtv_stop_v4l2_decode_stream(s, dc->flags, dc->stop.pts);
0274 
0275     case V4L2_DEC_CMD_PAUSE:
0276         dc->flags &= V4L2_DEC_CMD_PAUSE_TO_BLACK;
0277         if (try) break;
0278         if (!atomic_read(&itv->decoding))
0279             return -EPERM;
0280         if (itv->output_mode != OUT_MPG)
0281             return -EBUSY;
0282         if (atomic_read(&itv->decoding) > 0) {
0283             ivtv_vapi(itv, CX2341X_DEC_PAUSE_PLAYBACK, 1,
0284                 (dc->flags & V4L2_DEC_CMD_PAUSE_TO_BLACK) ? 1 : 0);
0285             set_bit(IVTV_F_I_DEC_PAUSED, &itv->i_flags);
0286         }
0287         break;
0288 
0289     case V4L2_DEC_CMD_RESUME:
0290         dc->flags = 0;
0291         if (try) break;
0292         if (!atomic_read(&itv->decoding))
0293             return -EPERM;
0294         if (itv->output_mode != OUT_MPG)
0295             return -EBUSY;
0296         if (test_and_clear_bit(IVTV_F_I_DEC_PAUSED, &itv->i_flags)) {
0297             int speed = itv->speed;
0298             itv->speed = 0;
0299             return ivtv_start_decoding(id, speed);
0300         }
0301         break;
0302 
0303     default:
0304         return -EINVAL;
0305     }
0306     return 0;
0307 }
0308 
0309 static int ivtv_g_fmt_sliced_vbi_out(struct file *file, void *fh, struct v4l2_format *fmt)
0310 {
0311     struct ivtv *itv = fh2id(fh)->itv;
0312     struct v4l2_sliced_vbi_format *vbifmt = &fmt->fmt.sliced;
0313 
0314     vbifmt->reserved[0] = 0;
0315     vbifmt->reserved[1] = 0;
0316     if (!(itv->v4l2_cap & V4L2_CAP_SLICED_VBI_OUTPUT))
0317         return -EINVAL;
0318     vbifmt->io_size = sizeof(struct v4l2_sliced_vbi_data) * 36;
0319     memset(vbifmt->service_lines, 0, sizeof(vbifmt->service_lines));
0320     if (itv->is_60hz) {
0321         vbifmt->service_lines[0][21] = V4L2_SLICED_CAPTION_525;
0322         vbifmt->service_lines[1][21] = V4L2_SLICED_CAPTION_525;
0323     } else {
0324         vbifmt->service_lines[0][23] = V4L2_SLICED_WSS_625;
0325         vbifmt->service_lines[0][16] = V4L2_SLICED_VPS;
0326     }
0327     vbifmt->service_set = ivtv_get_service_set(vbifmt);
0328     return 0;
0329 }
0330 
0331 static int ivtv_g_fmt_vid_cap(struct file *file, void *fh, struct v4l2_format *fmt)
0332 {
0333     struct ivtv_open_id *id = fh2id(fh);
0334     struct ivtv *itv = id->itv;
0335     struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
0336 
0337     pixfmt->width = itv->cxhdl.width;
0338     pixfmt->height = itv->cxhdl.height;
0339     pixfmt->colorspace = V4L2_COLORSPACE_SMPTE170M;
0340     pixfmt->field = V4L2_FIELD_INTERLACED;
0341     if (id->type == IVTV_ENC_STREAM_TYPE_YUV) {
0342         pixfmt->pixelformat = V4L2_PIX_FMT_NV12_16L16;
0343         /* YUV size is (Y=(h*720) + UV=(h*(720/2))) */
0344         pixfmt->sizeimage = pixfmt->height * 720 * 3 / 2;
0345         pixfmt->bytesperline = 720;
0346     } else {
0347         pixfmt->pixelformat = V4L2_PIX_FMT_MPEG;
0348         pixfmt->sizeimage = 128 * 1024;
0349         pixfmt->bytesperline = 0;
0350     }
0351     return 0;
0352 }
0353 
0354 static int ivtv_g_fmt_vbi_cap(struct file *file, void *fh, struct v4l2_format *fmt)
0355 {
0356     struct ivtv *itv = fh2id(fh)->itv;
0357     struct v4l2_vbi_format *vbifmt = &fmt->fmt.vbi;
0358 
0359     vbifmt->sampling_rate = 27000000;
0360     vbifmt->offset = 248;
0361     vbifmt->samples_per_line = itv->vbi.raw_decoder_line_size - 4;
0362     vbifmt->sample_format = V4L2_PIX_FMT_GREY;
0363     vbifmt->start[0] = itv->vbi.start[0];
0364     vbifmt->start[1] = itv->vbi.start[1];
0365     vbifmt->count[0] = vbifmt->count[1] = itv->vbi.count;
0366     vbifmt->flags = 0;
0367     vbifmt->reserved[0] = 0;
0368     vbifmt->reserved[1] = 0;
0369     return 0;
0370 }
0371 
0372 static int ivtv_g_fmt_sliced_vbi_cap(struct file *file, void *fh, struct v4l2_format *fmt)
0373 {
0374     struct v4l2_sliced_vbi_format *vbifmt = &fmt->fmt.sliced;
0375     struct ivtv_open_id *id = fh2id(fh);
0376     struct ivtv *itv = id->itv;
0377 
0378     vbifmt->reserved[0] = 0;
0379     vbifmt->reserved[1] = 0;
0380     vbifmt->io_size = sizeof(struct v4l2_sliced_vbi_data) * 36;
0381 
0382     if (id->type == IVTV_DEC_STREAM_TYPE_VBI) {
0383         vbifmt->service_set = itv->is_50hz ? V4L2_SLICED_VBI_625 :
0384             V4L2_SLICED_VBI_525;
0385         ivtv_expand_service_set(vbifmt, itv->is_50hz);
0386         vbifmt->service_set = ivtv_get_service_set(vbifmt);
0387         return 0;
0388     }
0389 
0390     v4l2_subdev_call(itv->sd_video, vbi, g_sliced_fmt, vbifmt);
0391     vbifmt->service_set = ivtv_get_service_set(vbifmt);
0392     return 0;
0393 }
0394 
0395 static int ivtv_g_fmt_vid_out(struct file *file, void *fh, struct v4l2_format *fmt)
0396 {
0397     struct ivtv_open_id *id = fh2id(fh);
0398     struct ivtv *itv = id->itv;
0399     struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
0400 
0401     if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
0402         return -EINVAL;
0403     pixfmt->width = itv->main_rect.width;
0404     pixfmt->height = itv->main_rect.height;
0405     pixfmt->colorspace = V4L2_COLORSPACE_SMPTE170M;
0406     pixfmt->field = V4L2_FIELD_INTERLACED;
0407     if (id->type == IVTV_DEC_STREAM_TYPE_YUV) {
0408         switch (itv->yuv_info.lace_mode & IVTV_YUV_MODE_MASK) {
0409         case IVTV_YUV_MODE_INTERLACED:
0410             pixfmt->field = (itv->yuv_info.lace_mode & IVTV_YUV_SYNC_MASK) ?
0411                 V4L2_FIELD_INTERLACED_BT : V4L2_FIELD_INTERLACED_TB;
0412             break;
0413         case IVTV_YUV_MODE_PROGRESSIVE:
0414             pixfmt->field = V4L2_FIELD_NONE;
0415             break;
0416         default:
0417             pixfmt->field = V4L2_FIELD_ANY;
0418             break;
0419         }
0420         pixfmt->pixelformat = V4L2_PIX_FMT_NV12_16L16;
0421         pixfmt->bytesperline = 720;
0422         pixfmt->width = itv->yuv_info.v4l2_src_w;
0423         pixfmt->height = itv->yuv_info.v4l2_src_h;
0424         /* YUV size is (Y=(h*w) + UV=(h*(w/2))) */
0425         pixfmt->sizeimage =
0426             1080 * ((pixfmt->height + 31) & ~31);
0427     } else {
0428         pixfmt->pixelformat = V4L2_PIX_FMT_MPEG;
0429         pixfmt->sizeimage = 128 * 1024;
0430         pixfmt->bytesperline = 0;
0431     }
0432     return 0;
0433 }
0434 
0435 static int ivtv_g_fmt_vid_out_overlay(struct file *file, void *fh, struct v4l2_format *fmt)
0436 {
0437     struct ivtv *itv = fh2id(fh)->itv;
0438     struct ivtv_stream *s = &itv->streams[fh2id(fh)->type];
0439     struct v4l2_window *winfmt = &fmt->fmt.win;
0440 
0441     if (!(s->vdev.device_caps & V4L2_CAP_VIDEO_OUTPUT_OVERLAY))
0442         return -EINVAL;
0443     if (!itv->osd_video_pbase)
0444         return -EINVAL;
0445     winfmt->chromakey = itv->osd_chroma_key;
0446     winfmt->global_alpha = itv->osd_global_alpha;
0447     winfmt->field = V4L2_FIELD_INTERLACED;
0448     winfmt->clips = NULL;
0449     winfmt->clipcount = 0;
0450     winfmt->bitmap = NULL;
0451     winfmt->w.top = winfmt->w.left = 0;
0452     winfmt->w.width = itv->osd_rect.width;
0453     winfmt->w.height = itv->osd_rect.height;
0454     return 0;
0455 }
0456 
0457 static int ivtv_try_fmt_sliced_vbi_out(struct file *file, void *fh, struct v4l2_format *fmt)
0458 {
0459     return ivtv_g_fmt_sliced_vbi_out(file, fh, fmt);
0460 }
0461 
0462 static int ivtv_try_fmt_vid_cap(struct file *file, void *fh, struct v4l2_format *fmt)
0463 {
0464     struct ivtv_open_id *id = fh2id(fh);
0465     struct ivtv *itv = id->itv;
0466     int w = fmt->fmt.pix.width;
0467     int h = fmt->fmt.pix.height;
0468     int min_h = 2;
0469 
0470     w = min(w, 720);
0471     w = max(w, 2);
0472     if (id->type == IVTV_ENC_STREAM_TYPE_YUV) {
0473         /* YUV height must be a multiple of 32 */
0474         h &= ~0x1f;
0475         min_h = 32;
0476     }
0477     h = min(h, itv->is_50hz ? 576 : 480);
0478     h = max(h, min_h);
0479     ivtv_g_fmt_vid_cap(file, fh, fmt);
0480     fmt->fmt.pix.width = w;
0481     fmt->fmt.pix.height = h;
0482     return 0;
0483 }
0484 
0485 static int ivtv_try_fmt_vbi_cap(struct file *file, void *fh, struct v4l2_format *fmt)
0486 {
0487     return ivtv_g_fmt_vbi_cap(file, fh, fmt);
0488 }
0489 
0490 static int ivtv_try_fmt_sliced_vbi_cap(struct file *file, void *fh, struct v4l2_format *fmt)
0491 {
0492     struct v4l2_sliced_vbi_format *vbifmt = &fmt->fmt.sliced;
0493     struct ivtv_open_id *id = fh2id(fh);
0494     struct ivtv *itv = id->itv;
0495 
0496     if (id->type == IVTV_DEC_STREAM_TYPE_VBI)
0497         return ivtv_g_fmt_sliced_vbi_cap(file, fh, fmt);
0498 
0499     /* set sliced VBI capture format */
0500     vbifmt->io_size = sizeof(struct v4l2_sliced_vbi_data) * 36;
0501     vbifmt->reserved[0] = 0;
0502     vbifmt->reserved[1] = 0;
0503 
0504     if (vbifmt->service_set)
0505         ivtv_expand_service_set(vbifmt, itv->is_50hz);
0506     check_service_set(vbifmt, itv->is_50hz);
0507     vbifmt->service_set = ivtv_get_service_set(vbifmt);
0508     return 0;
0509 }
0510 
0511 static int ivtv_try_fmt_vid_out(struct file *file, void *fh, struct v4l2_format *fmt)
0512 {
0513     struct ivtv_open_id *id = fh2id(fh);
0514     s32 w = fmt->fmt.pix.width;
0515     s32 h = fmt->fmt.pix.height;
0516     int field = fmt->fmt.pix.field;
0517     int ret = ivtv_g_fmt_vid_out(file, fh, fmt);
0518 
0519     w = min(w, 720);
0520     w = max(w, 2);
0521     /* Why can the height be 576 even when the output is NTSC?
0522 
0523        Internally the buffers of the PVR350 are always set to 720x576. The
0524        decoded video frame will always be placed in the top left corner of
0525        this buffer. For any video which is not 720x576, the buffer will
0526        then be cropped to remove the unused right and lower areas, with
0527        the remaining image being scaled by the hardware to fit the display
0528        area. The video can be scaled both up and down, so a 720x480 video
0529        can be displayed full-screen on PAL and a 720x576 video can be
0530        displayed without cropping on NTSC.
0531 
0532        Note that the scaling only occurs on the video stream, the osd
0533        resolution is locked to the broadcast standard and not scaled.
0534 
0535        Thanks to Ian Armstrong for this explanation. */
0536     h = min(h, 576);
0537     h = max(h, 2);
0538     if (id->type == IVTV_DEC_STREAM_TYPE_YUV)
0539         fmt->fmt.pix.field = field;
0540     fmt->fmt.pix.width = w;
0541     fmt->fmt.pix.height = h;
0542     return ret;
0543 }
0544 
0545 static int ivtv_try_fmt_vid_out_overlay(struct file *file, void *fh, struct v4l2_format *fmt)
0546 {
0547     struct ivtv *itv = fh2id(fh)->itv;
0548     struct ivtv_stream *s = &itv->streams[fh2id(fh)->type];
0549     u32 chromakey = fmt->fmt.win.chromakey;
0550     u8 global_alpha = fmt->fmt.win.global_alpha;
0551 
0552     if (!(s->vdev.device_caps & V4L2_CAP_VIDEO_OUTPUT_OVERLAY))
0553         return -EINVAL;
0554     if (!itv->osd_video_pbase)
0555         return -EINVAL;
0556     ivtv_g_fmt_vid_out_overlay(file, fh, fmt);
0557     fmt->fmt.win.chromakey = chromakey;
0558     fmt->fmt.win.global_alpha = global_alpha;
0559     return 0;
0560 }
0561 
0562 static int ivtv_s_fmt_sliced_vbi_out(struct file *file, void *fh, struct v4l2_format *fmt)
0563 {
0564     return ivtv_g_fmt_sliced_vbi_out(file, fh, fmt);
0565 }
0566 
0567 static int ivtv_s_fmt_vid_cap(struct file *file, void *fh, struct v4l2_format *fmt)
0568 {
0569     struct ivtv_open_id *id = fh2id(fh);
0570     struct ivtv *itv = id->itv;
0571     struct v4l2_subdev_format format = {
0572         .which = V4L2_SUBDEV_FORMAT_ACTIVE,
0573     };
0574     int ret = ivtv_try_fmt_vid_cap(file, fh, fmt);
0575     int w = fmt->fmt.pix.width;
0576     int h = fmt->fmt.pix.height;
0577 
0578     if (ret)
0579         return ret;
0580 
0581     if (itv->cxhdl.width == w && itv->cxhdl.height == h)
0582         return 0;
0583 
0584     if (atomic_read(&itv->capturing) > 0)
0585         return -EBUSY;
0586 
0587     itv->cxhdl.width = w;
0588     itv->cxhdl.height = h;
0589     if (v4l2_ctrl_g_ctrl(itv->cxhdl.video_encoding) == V4L2_MPEG_VIDEO_ENCODING_MPEG_1)
0590         fmt->fmt.pix.width /= 2;
0591     format.format.width = fmt->fmt.pix.width;
0592     format.format.height = h;
0593     format.format.code = MEDIA_BUS_FMT_FIXED;
0594     v4l2_subdev_call(itv->sd_video, pad, set_fmt, NULL, &format);
0595     return ivtv_g_fmt_vid_cap(file, fh, fmt);
0596 }
0597 
0598 static int ivtv_s_fmt_vbi_cap(struct file *file, void *fh, struct v4l2_format *fmt)
0599 {
0600     struct ivtv *itv = fh2id(fh)->itv;
0601 
0602     if (!ivtv_raw_vbi(itv) && atomic_read(&itv->capturing) > 0)
0603         return -EBUSY;
0604     itv->vbi.sliced_in->service_set = 0;
0605     itv->vbi.in.type = V4L2_BUF_TYPE_VBI_CAPTURE;
0606     v4l2_subdev_call(itv->sd_video, vbi, s_raw_fmt, &fmt->fmt.vbi);
0607     return ivtv_g_fmt_vbi_cap(file, fh, fmt);
0608 }
0609 
0610 static int ivtv_s_fmt_sliced_vbi_cap(struct file *file, void *fh, struct v4l2_format *fmt)
0611 {
0612     struct v4l2_sliced_vbi_format *vbifmt = &fmt->fmt.sliced;
0613     struct ivtv_open_id *id = fh2id(fh);
0614     struct ivtv *itv = id->itv;
0615     int ret = ivtv_try_fmt_sliced_vbi_cap(file, fh, fmt);
0616 
0617     if (ret || id->type == IVTV_DEC_STREAM_TYPE_VBI)
0618         return ret;
0619 
0620     check_service_set(vbifmt, itv->is_50hz);
0621     if (ivtv_raw_vbi(itv) && atomic_read(&itv->capturing) > 0)
0622         return -EBUSY;
0623     itv->vbi.in.type = V4L2_BUF_TYPE_SLICED_VBI_CAPTURE;
0624     v4l2_subdev_call(itv->sd_video, vbi, s_sliced_fmt, vbifmt);
0625     memcpy(itv->vbi.sliced_in, vbifmt, sizeof(*itv->vbi.sliced_in));
0626     return 0;
0627 }
0628 
0629 static int ivtv_s_fmt_vid_out(struct file *file, void *fh, struct v4l2_format *fmt)
0630 {
0631     struct ivtv_open_id *id = fh2id(fh);
0632     struct ivtv *itv = id->itv;
0633     struct yuv_playback_info *yi = &itv->yuv_info;
0634     int ret = ivtv_try_fmt_vid_out(file, fh, fmt);
0635 
0636     if (ret)
0637         return ret;
0638 
0639     if (id->type != IVTV_DEC_STREAM_TYPE_YUV)
0640         return 0;
0641 
0642     /* Return now if we already have some frame data */
0643     if (yi->stream_size)
0644         return -EBUSY;
0645 
0646     yi->v4l2_src_w = fmt->fmt.pix.width;
0647     yi->v4l2_src_h = fmt->fmt.pix.height;
0648 
0649     switch (fmt->fmt.pix.field) {
0650     case V4L2_FIELD_NONE:
0651         yi->lace_mode = IVTV_YUV_MODE_PROGRESSIVE;
0652         break;
0653     case V4L2_FIELD_ANY:
0654         yi->lace_mode = IVTV_YUV_MODE_AUTO;
0655         break;
0656     case V4L2_FIELD_INTERLACED_BT:
0657         yi->lace_mode =
0658             IVTV_YUV_MODE_INTERLACED|IVTV_YUV_SYNC_ODD;
0659         break;
0660     case V4L2_FIELD_INTERLACED_TB:
0661     default:
0662         yi->lace_mode = IVTV_YUV_MODE_INTERLACED;
0663         break;
0664     }
0665     yi->lace_sync_field = (yi->lace_mode & IVTV_YUV_SYNC_MASK) == IVTV_YUV_SYNC_EVEN ? 0 : 1;
0666 
0667     if (test_bit(IVTV_F_I_DEC_YUV, &itv->i_flags))
0668         itv->dma_data_req_size =
0669             1080 * ((yi->v4l2_src_h + 31) & ~31);
0670 
0671     return 0;
0672 }
0673 
0674 static int ivtv_s_fmt_vid_out_overlay(struct file *file, void *fh, struct v4l2_format *fmt)
0675 {
0676     struct ivtv *itv = fh2id(fh)->itv;
0677     int ret = ivtv_try_fmt_vid_out_overlay(file, fh, fmt);
0678 
0679     if (ret == 0) {
0680         itv->osd_chroma_key = fmt->fmt.win.chromakey;
0681         itv->osd_global_alpha = fmt->fmt.win.global_alpha;
0682         ivtv_set_osd_alpha(itv);
0683     }
0684     return ret;
0685 }
0686 
0687 #ifdef CONFIG_VIDEO_ADV_DEBUG
0688 static int ivtv_itvc(struct ivtv *itv, bool get, u64 reg, u64 *val)
0689 {
0690     volatile u8 __iomem *reg_start;
0691 
0692     if (reg & 0x3)
0693         return -EINVAL;
0694     if (reg >= IVTV_REG_OFFSET && reg < IVTV_REG_OFFSET + IVTV_REG_SIZE)
0695         reg_start = itv->reg_mem - IVTV_REG_OFFSET;
0696     else if (itv->has_cx23415 && reg >= IVTV_DECODER_OFFSET &&
0697             reg < IVTV_DECODER_OFFSET + IVTV_DECODER_SIZE)
0698         reg_start = itv->dec_mem - IVTV_DECODER_OFFSET;
0699     else if (reg < IVTV_ENCODER_SIZE)
0700         reg_start = itv->enc_mem;
0701     else
0702         return -EINVAL;
0703 
0704     if (get)
0705         *val = readl(reg + reg_start);
0706     else
0707         writel(*val, reg + reg_start);
0708     return 0;
0709 }
0710 
0711 static int ivtv_g_register(struct file *file, void *fh, struct v4l2_dbg_register *reg)
0712 {
0713     struct ivtv *itv = fh2id(fh)->itv;
0714 
0715     reg->size = 4;
0716     return ivtv_itvc(itv, true, reg->reg, &reg->val);
0717 }
0718 
0719 static int ivtv_s_register(struct file *file, void *fh, const struct v4l2_dbg_register *reg)
0720 {
0721     struct ivtv *itv = fh2id(fh)->itv;
0722     u64 val = reg->val;
0723 
0724     return ivtv_itvc(itv, false, reg->reg, &val);
0725 }
0726 #endif
0727 
0728 static int ivtv_querycap(struct file *file, void *fh, struct v4l2_capability *vcap)
0729 {
0730     struct ivtv_open_id *id = fh2id(file->private_data);
0731     struct ivtv *itv = id->itv;
0732 
0733     strscpy(vcap->driver, IVTV_DRIVER_NAME, sizeof(vcap->driver));
0734     strscpy(vcap->card, itv->card_name, sizeof(vcap->card));
0735     vcap->capabilities = itv->v4l2_cap | V4L2_CAP_DEVICE_CAPS;
0736     return 0;
0737 }
0738 
0739 static int ivtv_enumaudio(struct file *file, void *fh, struct v4l2_audio *vin)
0740 {
0741     struct ivtv *itv = fh2id(fh)->itv;
0742 
0743     return ivtv_get_audio_input(itv, vin->index, vin);
0744 }
0745 
0746 static int ivtv_g_audio(struct file *file, void *fh, struct v4l2_audio *vin)
0747 {
0748     struct ivtv *itv = fh2id(fh)->itv;
0749 
0750     vin->index = itv->audio_input;
0751     return ivtv_get_audio_input(itv, vin->index, vin);
0752 }
0753 
0754 static int ivtv_s_audio(struct file *file, void *fh, const struct v4l2_audio *vout)
0755 {
0756     struct ivtv *itv = fh2id(fh)->itv;
0757 
0758     if (vout->index >= itv->nof_audio_inputs)
0759         return -EINVAL;
0760 
0761     itv->audio_input = vout->index;
0762     ivtv_audio_set_io(itv);
0763 
0764     return 0;
0765 }
0766 
0767 static int ivtv_enumaudout(struct file *file, void *fh, struct v4l2_audioout *vin)
0768 {
0769     struct ivtv *itv = fh2id(fh)->itv;
0770 
0771     /* set it to defaults from our table */
0772     return ivtv_get_audio_output(itv, vin->index, vin);
0773 }
0774 
0775 static int ivtv_g_audout(struct file *file, void *fh, struct v4l2_audioout *vin)
0776 {
0777     struct ivtv *itv = fh2id(fh)->itv;
0778 
0779     vin->index = 0;
0780     return ivtv_get_audio_output(itv, vin->index, vin);
0781 }
0782 
0783 static int ivtv_s_audout(struct file *file, void *fh, const struct v4l2_audioout *vout)
0784 {
0785     struct ivtv *itv = fh2id(fh)->itv;
0786 
0787     if (itv->card->video_outputs == NULL || vout->index != 0)
0788         return -EINVAL;
0789     return 0;
0790 }
0791 
0792 static int ivtv_enum_input(struct file *file, void *fh, struct v4l2_input *vin)
0793 {
0794     struct ivtv *itv = fh2id(fh)->itv;
0795 
0796     /* set it to defaults from our table */
0797     return ivtv_get_input(itv, vin->index, vin);
0798 }
0799 
0800 static int ivtv_enum_output(struct file *file, void *fh, struct v4l2_output *vout)
0801 {
0802     struct ivtv *itv = fh2id(fh)->itv;
0803 
0804     return ivtv_get_output(itv, vout->index, vout);
0805 }
0806 
0807 static int ivtv_g_pixelaspect(struct file *file, void *fh,
0808                   int type, struct v4l2_fract *f)
0809 {
0810     struct ivtv_open_id *id = fh2id(fh);
0811     struct ivtv *itv = id->itv;
0812 
0813     if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
0814         f->numerator = itv->is_50hz ? 54 : 11;
0815         f->denominator = itv->is_50hz ? 59 : 10;
0816     } else if (type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
0817         f->numerator = itv->is_out_50hz ? 54 : 11;
0818         f->denominator = itv->is_out_50hz ? 59 : 10;
0819     } else {
0820         return -EINVAL;
0821     }
0822     return 0;
0823 }
0824 
0825 static int ivtv_s_selection(struct file *file, void *fh,
0826                 struct v4l2_selection *sel)
0827 {
0828     struct ivtv_open_id *id = fh2id(fh);
0829     struct ivtv *itv = id->itv;
0830     struct yuv_playback_info *yi = &itv->yuv_info;
0831     struct v4l2_rect r = { 0, 0, 720, 0 };
0832     int streamtype = id->type;
0833 
0834     if (sel->type != V4L2_BUF_TYPE_VIDEO_OUTPUT ||
0835         !(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
0836         return -EINVAL;
0837 
0838     if (sel->target != V4L2_SEL_TGT_COMPOSE)
0839         return -EINVAL;
0840 
0841 
0842     if (sel->type != V4L2_BUF_TYPE_VIDEO_OUTPUT ||
0843         !(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
0844         return -EINVAL;
0845 
0846     r.height = itv->is_out_50hz ? 576 : 480;
0847     if (streamtype == IVTV_DEC_STREAM_TYPE_YUV && yi->track_osd) {
0848         r.width = yi->osd_full_w;
0849         r.height = yi->osd_full_h;
0850     }
0851     sel->r.width = clamp(sel->r.width, 16U, r.width);
0852     sel->r.height = clamp(sel->r.height, 16U, r.height);
0853     sel->r.left = clamp_t(unsigned, sel->r.left, 0, r.width - sel->r.width);
0854     sel->r.top = clamp_t(unsigned, sel->r.top, 0, r.height - sel->r.height);
0855 
0856     if (streamtype == IVTV_DEC_STREAM_TYPE_YUV) {
0857         yi->main_rect = sel->r;
0858         return 0;
0859     }
0860     if (!ivtv_vapi(itv, CX2341X_OSD_SET_FRAMEBUFFER_WINDOW, 4,
0861             sel->r.width, sel->r.height, sel->r.left, sel->r.top)) {
0862         itv->main_rect = sel->r;
0863         return 0;
0864     }
0865     return -EINVAL;
0866 }
0867 
0868 static int ivtv_g_selection(struct file *file, void *fh,
0869                 struct v4l2_selection *sel)
0870 {
0871     struct ivtv_open_id *id = fh2id(fh);
0872     struct ivtv *itv = id->itv;
0873     struct yuv_playback_info *yi = &itv->yuv_info;
0874     struct v4l2_rect r = { 0, 0, 720, 0 };
0875     int streamtype = id->type;
0876 
0877     if (sel->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
0878         switch (sel->target) {
0879         case V4L2_SEL_TGT_CROP_DEFAULT:
0880         case V4L2_SEL_TGT_CROP_BOUNDS:
0881             sel->r.top = sel->r.left = 0;
0882             sel->r.width = 720;
0883             sel->r.height = itv->is_50hz ? 576 : 480;
0884             return 0;
0885         default:
0886             return -EINVAL;
0887         }
0888     }
0889 
0890     if (sel->type != V4L2_BUF_TYPE_VIDEO_OUTPUT ||
0891         !(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
0892         return -EINVAL;
0893 
0894     switch (sel->target) {
0895     case V4L2_SEL_TGT_COMPOSE:
0896         if (streamtype == IVTV_DEC_STREAM_TYPE_YUV)
0897             sel->r = yi->main_rect;
0898         else
0899             sel->r = itv->main_rect;
0900         return 0;
0901     case V4L2_SEL_TGT_COMPOSE_DEFAULT:
0902     case V4L2_SEL_TGT_COMPOSE_BOUNDS:
0903         r.height = itv->is_out_50hz ? 576 : 480;
0904         if (streamtype == IVTV_DEC_STREAM_TYPE_YUV && yi->track_osd) {
0905             r.width = yi->osd_full_w;
0906             r.height = yi->osd_full_h;
0907         }
0908         sel->r = r;
0909         return 0;
0910     }
0911     return -EINVAL;
0912 }
0913 
0914 static int ivtv_enum_fmt_vid_cap(struct file *file, void *fh, struct v4l2_fmtdesc *fmt)
0915 {
0916     static const struct v4l2_fmtdesc hm12 = {
0917         .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
0918         .description = "HM12 (YUV 4:2:0)",
0919         .pixelformat = V4L2_PIX_FMT_NV12_16L16,
0920     };
0921     static const struct v4l2_fmtdesc mpeg = {
0922         .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
0923         .flags = V4L2_FMT_FLAG_COMPRESSED,
0924         .description = "MPEG",
0925         .pixelformat = V4L2_PIX_FMT_MPEG,
0926     };
0927     struct ivtv *itv = fh2id(fh)->itv;
0928     struct ivtv_stream *s = &itv->streams[fh2id(fh)->type];
0929 
0930     if (fmt->index)
0931         return -EINVAL;
0932     if (s->type == IVTV_ENC_STREAM_TYPE_MPG)
0933         *fmt = mpeg;
0934     else if (s->type == IVTV_ENC_STREAM_TYPE_YUV)
0935         *fmt = hm12;
0936     else
0937         return -EINVAL;
0938     return 0;
0939 }
0940 
0941 static int ivtv_enum_fmt_vid_out(struct file *file, void *fh, struct v4l2_fmtdesc *fmt)
0942 {
0943     static const struct v4l2_fmtdesc hm12 = {
0944         .type = V4L2_BUF_TYPE_VIDEO_OUTPUT,
0945         .description = "HM12 (YUV 4:2:0)",
0946         .pixelformat = V4L2_PIX_FMT_NV12_16L16,
0947     };
0948     static const struct v4l2_fmtdesc mpeg = {
0949         .type = V4L2_BUF_TYPE_VIDEO_OUTPUT,
0950         .flags = V4L2_FMT_FLAG_COMPRESSED,
0951         .description = "MPEG",
0952         .pixelformat = V4L2_PIX_FMT_MPEG,
0953     };
0954     struct ivtv *itv = fh2id(fh)->itv;
0955     struct ivtv_stream *s = &itv->streams[fh2id(fh)->type];
0956 
0957     if (fmt->index)
0958         return -EINVAL;
0959     if (s->type == IVTV_DEC_STREAM_TYPE_MPG)
0960         *fmt = mpeg;
0961     else if (s->type == IVTV_DEC_STREAM_TYPE_YUV)
0962         *fmt = hm12;
0963     else
0964         return -EINVAL;
0965     return 0;
0966 }
0967 
0968 static int ivtv_g_input(struct file *file, void *fh, unsigned int *i)
0969 {
0970     struct ivtv *itv = fh2id(fh)->itv;
0971 
0972     *i = itv->active_input;
0973 
0974     return 0;
0975 }
0976 
0977 int ivtv_s_input(struct file *file, void *fh, unsigned int inp)
0978 {
0979     struct ivtv *itv = fh2id(fh)->itv;
0980     v4l2_std_id std;
0981     int i;
0982 
0983     if (inp >= itv->nof_inputs)
0984         return -EINVAL;
0985 
0986     if (inp == itv->active_input) {
0987         IVTV_DEBUG_INFO("Input unchanged\n");
0988         return 0;
0989     }
0990 
0991     if (atomic_read(&itv->capturing) > 0) {
0992         return -EBUSY;
0993     }
0994 
0995     IVTV_DEBUG_INFO("Changing input from %d to %d\n",
0996             itv->active_input, inp);
0997 
0998     itv->active_input = inp;
0999     /* Set the audio input to whatever is appropriate for the
1000        input type. */
1001     itv->audio_input = itv->card->video_inputs[inp].audio_index;
1002 
1003     if (itv->card->video_inputs[inp].video_type == IVTV_CARD_INPUT_VID_TUNER)
1004         std = itv->tuner_std;
1005     else
1006         std = V4L2_STD_ALL;
1007     for (i = 0; i <= IVTV_ENC_STREAM_TYPE_VBI; i++)
1008         itv->streams[i].vdev.tvnorms = std;
1009 
1010     /* prevent others from messing with the streams until
1011        we're finished changing inputs. */
1012     ivtv_mute(itv);
1013     ivtv_video_set_io(itv);
1014     ivtv_audio_set_io(itv);
1015     ivtv_unmute(itv);
1016 
1017     return 0;
1018 }
1019 
1020 static int ivtv_g_output(struct file *file, void *fh, unsigned int *i)
1021 {
1022     struct ivtv *itv = fh2id(fh)->itv;
1023 
1024     if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
1025         return -EINVAL;
1026 
1027     *i = itv->active_output;
1028 
1029     return 0;
1030 }
1031 
1032 static int ivtv_s_output(struct file *file, void *fh, unsigned int outp)
1033 {
1034     struct ivtv *itv = fh2id(fh)->itv;
1035 
1036     if (outp >= itv->card->nof_outputs)
1037         return -EINVAL;
1038 
1039     if (outp == itv->active_output) {
1040         IVTV_DEBUG_INFO("Output unchanged\n");
1041         return 0;
1042     }
1043     IVTV_DEBUG_INFO("Changing output from %d to %d\n",
1044            itv->active_output, outp);
1045 
1046     itv->active_output = outp;
1047     ivtv_call_hw(itv, IVTV_HW_SAA7127, video, s_routing,
1048             SAA7127_INPUT_TYPE_NORMAL,
1049             itv->card->video_outputs[outp].video_output, 0);
1050 
1051     return 0;
1052 }
1053 
1054 static int ivtv_g_frequency(struct file *file, void *fh, struct v4l2_frequency *vf)
1055 {
1056     struct ivtv *itv = fh2id(fh)->itv;
1057     struct ivtv_stream *s = &itv->streams[fh2id(fh)->type];
1058 
1059     if (s->vdev.vfl_dir)
1060         return -ENOTTY;
1061     if (vf->tuner != 0)
1062         return -EINVAL;
1063 
1064     ivtv_call_all(itv, tuner, g_frequency, vf);
1065     return 0;
1066 }
1067 
1068 int ivtv_s_frequency(struct file *file, void *fh, const struct v4l2_frequency *vf)
1069 {
1070     struct ivtv *itv = fh2id(fh)->itv;
1071     struct ivtv_stream *s = &itv->streams[fh2id(fh)->type];
1072 
1073     if (s->vdev.vfl_dir)
1074         return -ENOTTY;
1075     if (vf->tuner != 0)
1076         return -EINVAL;
1077 
1078     ivtv_mute(itv);
1079     IVTV_DEBUG_INFO("v4l2 ioctl: set frequency %d\n", vf->frequency);
1080     ivtv_call_all(itv, tuner, s_frequency, vf);
1081     ivtv_unmute(itv);
1082     return 0;
1083 }
1084 
1085 static int ivtv_g_std(struct file *file, void *fh, v4l2_std_id *std)
1086 {
1087     struct ivtv *itv = fh2id(fh)->itv;
1088 
1089     *std = itv->std;
1090     return 0;
1091 }
1092 
1093 void ivtv_s_std_enc(struct ivtv *itv, v4l2_std_id std)
1094 {
1095     itv->std = std;
1096     itv->is_60hz = (std & V4L2_STD_525_60) ? 1 : 0;
1097     itv->is_50hz = !itv->is_60hz;
1098     cx2341x_handler_set_50hz(&itv->cxhdl, itv->is_50hz);
1099     itv->cxhdl.width = 720;
1100     itv->cxhdl.height = itv->is_50hz ? 576 : 480;
1101     itv->vbi.count = itv->is_50hz ? 18 : 12;
1102     itv->vbi.start[0] = itv->is_50hz ? 6 : 10;
1103     itv->vbi.start[1] = itv->is_50hz ? 318 : 273;
1104 
1105     if (itv->hw_flags & IVTV_HW_CX25840)
1106         itv->vbi.sliced_decoder_line_size = itv->is_60hz ? 272 : 284;
1107 
1108     /* Tuner */
1109     ivtv_call_all(itv, video, s_std, itv->std);
1110 }
1111 
1112 void ivtv_s_std_dec(struct ivtv *itv, v4l2_std_id std)
1113 {
1114     struct yuv_playback_info *yi = &itv->yuv_info;
1115     DEFINE_WAIT(wait);
1116     int f;
1117 
1118     /* set display standard */
1119     itv->std_out = std;
1120     itv->is_out_60hz = (std & V4L2_STD_525_60) ? 1 : 0;
1121     itv->is_out_50hz = !itv->is_out_60hz;
1122     ivtv_call_all(itv, video, s_std_output, itv->std_out);
1123 
1124     /*
1125      * The next firmware call is time sensitive. Time it to
1126      * avoid risk of a hard lock, by trying to ensure the call
1127      * happens within the first 100 lines of the top field.
1128      * Make 4 attempts to sync to the decoder before giving up.
1129      */
1130     mutex_unlock(&itv->serialize_lock);
1131     for (f = 0; f < 4; f++) {
1132         prepare_to_wait(&itv->vsync_waitq, &wait,
1133                 TASK_UNINTERRUPTIBLE);
1134         if ((read_reg(IVTV_REG_DEC_LINE_FIELD) >> 16) < 100)
1135             break;
1136         schedule_timeout(msecs_to_jiffies(25));
1137     }
1138     finish_wait(&itv->vsync_waitq, &wait);
1139     mutex_lock(&itv->serialize_lock);
1140 
1141     if (f == 4)
1142         IVTV_WARN("Mode change failed to sync to decoder\n");
1143 
1144     ivtv_vapi(itv, CX2341X_DEC_SET_STANDARD, 1, itv->is_out_50hz);
1145     itv->main_rect.left = 0;
1146     itv->main_rect.top = 0;
1147     itv->main_rect.width = 720;
1148     itv->main_rect.height = itv->is_out_50hz ? 576 : 480;
1149     ivtv_vapi(itv, CX2341X_OSD_SET_FRAMEBUFFER_WINDOW, 4,
1150         720, itv->main_rect.height, 0, 0);
1151     yi->main_rect = itv->main_rect;
1152     if (!itv->osd_info) {
1153         yi->osd_full_w = 720;
1154         yi->osd_full_h = itv->is_out_50hz ? 576 : 480;
1155     }
1156 }
1157 
1158 static int ivtv_s_std(struct file *file, void *fh, v4l2_std_id std)
1159 {
1160     struct ivtv *itv = fh2id(fh)->itv;
1161 
1162     if ((std & V4L2_STD_ALL) == 0)
1163         return -EINVAL;
1164 
1165     if (std == itv->std)
1166         return 0;
1167 
1168     if (test_bit(IVTV_F_I_RADIO_USER, &itv->i_flags) ||
1169         atomic_read(&itv->capturing) > 0 ||
1170         atomic_read(&itv->decoding) > 0) {
1171         /* Switching standard would mess with already running
1172            streams, prevent that by returning EBUSY. */
1173         return -EBUSY;
1174     }
1175 
1176     IVTV_DEBUG_INFO("Switching standard to %llx.\n",
1177         (unsigned long long)itv->std);
1178 
1179     ivtv_s_std_enc(itv, std);
1180     if (itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT)
1181         ivtv_s_std_dec(itv, std);
1182 
1183     return 0;
1184 }
1185 
1186 static int ivtv_s_tuner(struct file *file, void *fh, const struct v4l2_tuner *vt)
1187 {
1188     struct ivtv_open_id *id = fh2id(fh);
1189     struct ivtv *itv = id->itv;
1190 
1191     if (vt->index != 0)
1192         return -EINVAL;
1193 
1194     ivtv_call_all(itv, tuner, s_tuner, vt);
1195 
1196     return 0;
1197 }
1198 
1199 static int ivtv_g_tuner(struct file *file, void *fh, struct v4l2_tuner *vt)
1200 {
1201     struct ivtv *itv = fh2id(fh)->itv;
1202 
1203     if (vt->index != 0)
1204         return -EINVAL;
1205 
1206     ivtv_call_all(itv, tuner, g_tuner, vt);
1207 
1208     if (vt->type == V4L2_TUNER_RADIO)
1209         strscpy(vt->name, "ivtv Radio Tuner", sizeof(vt->name));
1210     else
1211         strscpy(vt->name, "ivtv TV Tuner", sizeof(vt->name));
1212     return 0;
1213 }
1214 
1215 static int ivtv_g_sliced_vbi_cap(struct file *file, void *fh, struct v4l2_sliced_vbi_cap *cap)
1216 {
1217     struct ivtv *itv = fh2id(fh)->itv;
1218     int set = itv->is_50hz ? V4L2_SLICED_VBI_625 : V4L2_SLICED_VBI_525;
1219     int f, l;
1220 
1221     if (cap->type == V4L2_BUF_TYPE_SLICED_VBI_CAPTURE) {
1222         for (f = 0; f < 2; f++) {
1223             for (l = 0; l < 24; l++) {
1224                 if (valid_service_line(f, l, itv->is_50hz))
1225                     cap->service_lines[f][l] = set;
1226             }
1227         }
1228     } else if (cap->type == V4L2_BUF_TYPE_SLICED_VBI_OUTPUT) {
1229         if (!(itv->v4l2_cap & V4L2_CAP_SLICED_VBI_OUTPUT))
1230             return -EINVAL;
1231         if (itv->is_60hz) {
1232             cap->service_lines[0][21] = V4L2_SLICED_CAPTION_525;
1233             cap->service_lines[1][21] = V4L2_SLICED_CAPTION_525;
1234         } else {
1235             cap->service_lines[0][23] = V4L2_SLICED_WSS_625;
1236             cap->service_lines[0][16] = V4L2_SLICED_VPS;
1237         }
1238     } else {
1239         return -EINVAL;
1240     }
1241 
1242     set = 0;
1243     for (f = 0; f < 2; f++)
1244         for (l = 0; l < 24; l++)
1245             set |= cap->service_lines[f][l];
1246     cap->service_set = set;
1247     return 0;
1248 }
1249 
1250 static int ivtv_g_enc_index(struct file *file, void *fh, struct v4l2_enc_idx *idx)
1251 {
1252     struct ivtv *itv = fh2id(fh)->itv;
1253     struct v4l2_enc_idx_entry *e = idx->entry;
1254     int entries;
1255     int i;
1256 
1257     entries = (itv->pgm_info_write_idx + IVTV_MAX_PGM_INDEX - itv->pgm_info_read_idx) %
1258                 IVTV_MAX_PGM_INDEX;
1259     if (entries > V4L2_ENC_IDX_ENTRIES)
1260         entries = V4L2_ENC_IDX_ENTRIES;
1261     idx->entries = 0;
1262     idx->entries_cap = IVTV_MAX_PGM_INDEX;
1263     if (!atomic_read(&itv->capturing))
1264         return 0;
1265     for (i = 0; i < entries; i++) {
1266         *e = itv->pgm_info[(itv->pgm_info_read_idx + i) % IVTV_MAX_PGM_INDEX];
1267         if ((e->flags & V4L2_ENC_IDX_FRAME_MASK) <= V4L2_ENC_IDX_FRAME_B) {
1268             idx->entries++;
1269             e++;
1270         }
1271     }
1272     itv->pgm_info_read_idx = (itv->pgm_info_read_idx + idx->entries) % IVTV_MAX_PGM_INDEX;
1273     return 0;
1274 }
1275 
1276 static int ivtv_encoder_cmd(struct file *file, void *fh, struct v4l2_encoder_cmd *enc)
1277 {
1278     struct ivtv_open_id *id = fh2id(fh);
1279     struct ivtv *itv = id->itv;
1280 
1281 
1282     switch (enc->cmd) {
1283     case V4L2_ENC_CMD_START:
1284         IVTV_DEBUG_IOCTL("V4L2_ENC_CMD_START\n");
1285         enc->flags = 0;
1286         return ivtv_start_capture(id);
1287 
1288     case V4L2_ENC_CMD_STOP:
1289         IVTV_DEBUG_IOCTL("V4L2_ENC_CMD_STOP\n");
1290         enc->flags &= V4L2_ENC_CMD_STOP_AT_GOP_END;
1291         ivtv_stop_capture(id, enc->flags & V4L2_ENC_CMD_STOP_AT_GOP_END);
1292         return 0;
1293 
1294     case V4L2_ENC_CMD_PAUSE:
1295         IVTV_DEBUG_IOCTL("V4L2_ENC_CMD_PAUSE\n");
1296         enc->flags = 0;
1297 
1298         if (!atomic_read(&itv->capturing))
1299             return -EPERM;
1300         if (test_and_set_bit(IVTV_F_I_ENC_PAUSED, &itv->i_flags))
1301             return 0;
1302 
1303         ivtv_mute(itv);
1304         ivtv_vapi(itv, CX2341X_ENC_PAUSE_ENCODER, 1, 0);
1305         break;
1306 
1307     case V4L2_ENC_CMD_RESUME:
1308         IVTV_DEBUG_IOCTL("V4L2_ENC_CMD_RESUME\n");
1309         enc->flags = 0;
1310 
1311         if (!atomic_read(&itv->capturing))
1312             return -EPERM;
1313 
1314         if (!test_and_clear_bit(IVTV_F_I_ENC_PAUSED, &itv->i_flags))
1315             return 0;
1316 
1317         ivtv_vapi(itv, CX2341X_ENC_PAUSE_ENCODER, 1, 1);
1318         ivtv_unmute(itv);
1319         break;
1320     default:
1321         IVTV_DEBUG_IOCTL("Unknown cmd %d\n", enc->cmd);
1322         return -EINVAL;
1323     }
1324 
1325     return 0;
1326 }
1327 
1328 static int ivtv_try_encoder_cmd(struct file *file, void *fh, struct v4l2_encoder_cmd *enc)
1329 {
1330     struct ivtv *itv = fh2id(fh)->itv;
1331 
1332     switch (enc->cmd) {
1333     case V4L2_ENC_CMD_START:
1334         IVTV_DEBUG_IOCTL("V4L2_ENC_CMD_START\n");
1335         enc->flags = 0;
1336         return 0;
1337 
1338     case V4L2_ENC_CMD_STOP:
1339         IVTV_DEBUG_IOCTL("V4L2_ENC_CMD_STOP\n");
1340         enc->flags &= V4L2_ENC_CMD_STOP_AT_GOP_END;
1341         return 0;
1342 
1343     case V4L2_ENC_CMD_PAUSE:
1344         IVTV_DEBUG_IOCTL("V4L2_ENC_CMD_PAUSE\n");
1345         enc->flags = 0;
1346         return 0;
1347 
1348     case V4L2_ENC_CMD_RESUME:
1349         IVTV_DEBUG_IOCTL("V4L2_ENC_CMD_RESUME\n");
1350         enc->flags = 0;
1351         return 0;
1352     default:
1353         IVTV_DEBUG_IOCTL("Unknown cmd %d\n", enc->cmd);
1354         return -EINVAL;
1355     }
1356 }
1357 
1358 static int ivtv_g_fbuf(struct file *file, void *fh, struct v4l2_framebuffer *fb)
1359 {
1360     struct ivtv *itv = fh2id(fh)->itv;
1361     struct ivtv_stream *s = &itv->streams[fh2id(fh)->type];
1362     u32 data[CX2341X_MBOX_MAX_DATA];
1363     struct yuv_playback_info *yi = &itv->yuv_info;
1364 
1365     int pixfmt;
1366     static u32 pixel_format[16] = {
1367         V4L2_PIX_FMT_PAL8, /* Uses a 256-entry RGB colormap */
1368         V4L2_PIX_FMT_RGB565,
1369         V4L2_PIX_FMT_RGB555,
1370         V4L2_PIX_FMT_RGB444,
1371         V4L2_PIX_FMT_RGB32,
1372         0,
1373         0,
1374         0,
1375         V4L2_PIX_FMT_PAL8, /* Uses a 256-entry YUV colormap */
1376         V4L2_PIX_FMT_YUV565,
1377         V4L2_PIX_FMT_YUV555,
1378         V4L2_PIX_FMT_YUV444,
1379         V4L2_PIX_FMT_YUV32,
1380         0,
1381         0,
1382         0,
1383     };
1384 
1385     if (!(s->vdev.device_caps & V4L2_CAP_VIDEO_OUTPUT_OVERLAY))
1386         return -ENOTTY;
1387     if (!itv->osd_video_pbase)
1388         return -ENOTTY;
1389 
1390     fb->capability = V4L2_FBUF_CAP_EXTERNOVERLAY | V4L2_FBUF_CAP_CHROMAKEY |
1391         V4L2_FBUF_CAP_GLOBAL_ALPHA;
1392 
1393     ivtv_vapi_result(itv, data, CX2341X_OSD_GET_STATE, 0);
1394     data[0] |= (read_reg(0x2a00) >> 7) & 0x40;
1395     pixfmt = (data[0] >> 3) & 0xf;
1396 
1397     fb->fmt.pixelformat = pixel_format[pixfmt];
1398     fb->fmt.width = itv->osd_rect.width;
1399     fb->fmt.height = itv->osd_rect.height;
1400     fb->fmt.field = V4L2_FIELD_INTERLACED;
1401     fb->fmt.bytesperline = fb->fmt.width;
1402     fb->fmt.colorspace = V4L2_COLORSPACE_SMPTE170M;
1403     fb->fmt.field = V4L2_FIELD_INTERLACED;
1404     if (fb->fmt.pixelformat != V4L2_PIX_FMT_PAL8)
1405         fb->fmt.bytesperline *= 2;
1406     if (fb->fmt.pixelformat == V4L2_PIX_FMT_RGB32 ||
1407         fb->fmt.pixelformat == V4L2_PIX_FMT_YUV32)
1408         fb->fmt.bytesperline *= 2;
1409     fb->fmt.sizeimage = fb->fmt.bytesperline * fb->fmt.height;
1410     fb->base = (void *)itv->osd_video_pbase;
1411     fb->flags = 0;
1412 
1413     if (itv->osd_chroma_key_state)
1414         fb->flags |= V4L2_FBUF_FLAG_CHROMAKEY;
1415 
1416     if (itv->osd_global_alpha_state)
1417         fb->flags |= V4L2_FBUF_FLAG_GLOBAL_ALPHA;
1418 
1419     if (yi->track_osd)
1420         fb->flags |= V4L2_FBUF_FLAG_OVERLAY;
1421 
1422     pixfmt &= 7;
1423 
1424     /* no local alpha for RGB565 or unknown formats */
1425     if (pixfmt == 1 || pixfmt > 4)
1426         return 0;
1427 
1428     /* 16-bit formats have inverted local alpha */
1429     if (pixfmt == 2 || pixfmt == 3)
1430         fb->capability |= V4L2_FBUF_CAP_LOCAL_INV_ALPHA;
1431     else
1432         fb->capability |= V4L2_FBUF_CAP_LOCAL_ALPHA;
1433 
1434     if (itv->osd_local_alpha_state) {
1435         /* 16-bit formats have inverted local alpha */
1436         if (pixfmt == 2 || pixfmt == 3)
1437             fb->flags |= V4L2_FBUF_FLAG_LOCAL_INV_ALPHA;
1438         else
1439             fb->flags |= V4L2_FBUF_FLAG_LOCAL_ALPHA;
1440     }
1441 
1442     return 0;
1443 }
1444 
1445 static int ivtv_s_fbuf(struct file *file, void *fh, const struct v4l2_framebuffer *fb)
1446 {
1447     struct ivtv_open_id *id = fh2id(fh);
1448     struct ivtv *itv = id->itv;
1449     struct ivtv_stream *s = &itv->streams[fh2id(fh)->type];
1450     struct yuv_playback_info *yi = &itv->yuv_info;
1451 
1452     if (!(s->vdev.device_caps & V4L2_CAP_VIDEO_OUTPUT_OVERLAY))
1453         return -ENOTTY;
1454     if (!itv->osd_video_pbase)
1455         return -ENOTTY;
1456 
1457     itv->osd_global_alpha_state = (fb->flags & V4L2_FBUF_FLAG_GLOBAL_ALPHA) != 0;
1458     itv->osd_local_alpha_state =
1459         (fb->flags & (V4L2_FBUF_FLAG_LOCAL_ALPHA|V4L2_FBUF_FLAG_LOCAL_INV_ALPHA)) != 0;
1460     itv->osd_chroma_key_state = (fb->flags & V4L2_FBUF_FLAG_CHROMAKEY) != 0;
1461     ivtv_set_osd_alpha(itv);
1462     yi->track_osd = (fb->flags & V4L2_FBUF_FLAG_OVERLAY) != 0;
1463     return 0;
1464 }
1465 
1466 static int ivtv_overlay(struct file *file, void *fh, unsigned int on)
1467 {
1468     struct ivtv_open_id *id = fh2id(fh);
1469     struct ivtv *itv = id->itv;
1470     struct ivtv_stream *s = &itv->streams[fh2id(fh)->type];
1471 
1472     if (!(s->vdev.device_caps & V4L2_CAP_VIDEO_OUTPUT_OVERLAY))
1473         return -ENOTTY;
1474     if (!itv->osd_video_pbase)
1475         return -ENOTTY;
1476 
1477     ivtv_vapi(itv, CX2341X_OSD_SET_STATE, 1, on != 0);
1478 
1479     return 0;
1480 }
1481 
1482 static int ivtv_subscribe_event(struct v4l2_fh *fh, const struct v4l2_event_subscription *sub)
1483 {
1484     switch (sub->type) {
1485     case V4L2_EVENT_VSYNC:
1486     case V4L2_EVENT_EOS:
1487         return v4l2_event_subscribe(fh, sub, 0, NULL);
1488     default:
1489         return v4l2_ctrl_subscribe_event(fh, sub);
1490     }
1491 }
1492 
1493 static int ivtv_log_status(struct file *file, void *fh)
1494 {
1495     struct ivtv *itv = fh2id(fh)->itv;
1496     u32 data[CX2341X_MBOX_MAX_DATA];
1497 
1498     int has_output = itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT;
1499     struct v4l2_input vidin;
1500     struct v4l2_audio audin;
1501     int i;
1502 
1503     IVTV_INFO("Version: %s Card: %s\n", IVTV_VERSION, itv->card_name);
1504     if (itv->hw_flags & IVTV_HW_TVEEPROM) {
1505         struct tveeprom tv;
1506 
1507         ivtv_read_eeprom(itv, &tv);
1508     }
1509     ivtv_call_all(itv, core, log_status);
1510     ivtv_get_input(itv, itv->active_input, &vidin);
1511     ivtv_get_audio_input(itv, itv->audio_input, &audin);
1512     IVTV_INFO("Video Input:  %s\n", vidin.name);
1513     IVTV_INFO("Audio Input:  %s%s\n", audin.name,
1514         itv->dualwatch_stereo_mode == V4L2_MPEG_AUDIO_MODE_DUAL ?
1515             " (Bilingual)" : "");
1516     if (has_output) {
1517         struct v4l2_output vidout;
1518         struct v4l2_audioout audout;
1519         int mode = itv->output_mode;
1520         static const char * const output_modes[5] = {
1521             "None",
1522             "MPEG Streaming",
1523             "YUV Streaming",
1524             "YUV Frames",
1525             "Passthrough",
1526         };
1527         static const char * const alpha_mode[4] = {
1528             "None",
1529             "Global",
1530             "Local",
1531             "Global and Local"
1532         };
1533         static const char * const pixel_format[16] = {
1534             "ARGB Indexed",
1535             "RGB 5:6:5",
1536             "ARGB 1:5:5:5",
1537             "ARGB 1:4:4:4",
1538             "ARGB 8:8:8:8",
1539             "5",
1540             "6",
1541             "7",
1542             "AYUV Indexed",
1543             "YUV 5:6:5",
1544             "AYUV 1:5:5:5",
1545             "AYUV 1:4:4:4",
1546             "AYUV 8:8:8:8",
1547             "13",
1548             "14",
1549             "15",
1550         };
1551 
1552         ivtv_get_output(itv, itv->active_output, &vidout);
1553         ivtv_get_audio_output(itv, 0, &audout);
1554         IVTV_INFO("Video Output: %s\n", vidout.name);
1555         if (mode < 0 || mode > OUT_PASSTHROUGH)
1556             mode = OUT_NONE;
1557         IVTV_INFO("Output Mode:  %s\n", output_modes[mode]);
1558         ivtv_vapi_result(itv, data, CX2341X_OSD_GET_STATE, 0);
1559         data[0] |= (read_reg(0x2a00) >> 7) & 0x40;
1560         IVTV_INFO("Overlay:      %s, Alpha: %s, Pixel Format: %s\n",
1561             data[0] & 1 ? "On" : "Off",
1562             alpha_mode[(data[0] >> 1) & 0x3],
1563             pixel_format[(data[0] >> 3) & 0xf]);
1564     }
1565     IVTV_INFO("Tuner:  %s\n",
1566         test_bit(IVTV_F_I_RADIO_USER, &itv->i_flags) ? "Radio" : "TV");
1567     v4l2_ctrl_handler_log_status(&itv->cxhdl.hdl, itv->v4l2_dev.name);
1568     IVTV_INFO("Status flags:    0x%08lx\n", itv->i_flags);
1569     for (i = 0; i < IVTV_MAX_STREAMS; i++) {
1570         struct ivtv_stream *s = &itv->streams[i];
1571 
1572         if (s->vdev.v4l2_dev == NULL || s->buffers == 0)
1573             continue;
1574         IVTV_INFO("Stream %s: status 0x%04lx, %d%% of %d KiB (%d buffers) in use\n", s->name, s->s_flags,
1575                 (s->buffers - s->q_free.buffers) * 100 / s->buffers,
1576                 (s->buffers * s->buf_size) / 1024, s->buffers);
1577     }
1578 
1579     IVTV_INFO("Read MPG/VBI: %lld/%lld bytes\n",
1580             (long long)itv->mpg_data_received,
1581             (long long)itv->vbi_data_inserted);
1582     return 0;
1583 }
1584 
1585 static int ivtv_decoder_cmd(struct file *file, void *fh, struct v4l2_decoder_cmd *dec)
1586 {
1587     struct ivtv_open_id *id = fh2id(file->private_data);
1588     struct ivtv *itv = id->itv;
1589 
1590     IVTV_DEBUG_IOCTL("VIDIOC_DECODER_CMD %d\n", dec->cmd);
1591     return ivtv_video_command(itv, id, dec, false);
1592 }
1593 
1594 static int ivtv_try_decoder_cmd(struct file *file, void *fh, struct v4l2_decoder_cmd *dec)
1595 {
1596     struct ivtv_open_id *id = fh2id(file->private_data);
1597     struct ivtv *itv = id->itv;
1598 
1599     IVTV_DEBUG_IOCTL("VIDIOC_TRY_DECODER_CMD %d\n", dec->cmd);
1600     return ivtv_video_command(itv, id, dec, true);
1601 }
1602 
1603 static int ivtv_decoder_ioctls(struct file *filp, unsigned int cmd, void *arg)
1604 {
1605     struct ivtv_open_id *id = fh2id(filp->private_data);
1606     struct ivtv *itv = id->itv;
1607     struct ivtv_stream *s = &itv->streams[id->type];
1608 
1609     switch (cmd) {
1610     case IVTV_IOC_DMA_FRAME: {
1611         struct ivtv_dma_frame *args = arg;
1612 
1613         IVTV_DEBUG_IOCTL("IVTV_IOC_DMA_FRAME\n");
1614         if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
1615             return -EINVAL;
1616         if (args->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
1617             return -EINVAL;
1618         if (itv->output_mode == OUT_UDMA_YUV && args->y_source == NULL)
1619             return 0;
1620         if (ivtv_start_decoding(id, id->type)) {
1621             return -EBUSY;
1622         }
1623         if (ivtv_set_output_mode(itv, OUT_UDMA_YUV) != OUT_UDMA_YUV) {
1624             ivtv_release_stream(s);
1625             return -EBUSY;
1626         }
1627         /* Mark that this file handle started the UDMA_YUV mode */
1628         id->yuv_frames = 1;
1629         if (args->y_source == NULL)
1630             return 0;
1631         return ivtv_yuv_prep_frame(itv, args);
1632     }
1633 
1634     case IVTV_IOC_PASSTHROUGH_MODE:
1635         IVTV_DEBUG_IOCTL("IVTV_IOC_PASSTHROUGH_MODE\n");
1636         if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
1637             return -EINVAL;
1638         return ivtv_passthrough_mode(itv, *(int *)arg != 0);
1639     default:
1640         return -EINVAL;
1641     }
1642     return 0;
1643 }
1644 
1645 static long ivtv_default(struct file *file, void *fh, bool valid_prio,
1646              unsigned int cmd, void *arg)
1647 {
1648     struct ivtv *itv = fh2id(fh)->itv;
1649 
1650     if (!valid_prio) {
1651         switch (cmd) {
1652         case IVTV_IOC_PASSTHROUGH_MODE:
1653             return -EBUSY;
1654         }
1655     }
1656 
1657     switch (cmd) {
1658     case VIDIOC_INT_RESET: {
1659         u32 val = *(u32 *)arg;
1660 
1661         if ((val == 0 && itv->options.newi2c) || (val & 0x01))
1662             ivtv_reset_ir_gpio(itv);
1663         if (val & 0x02)
1664             v4l2_subdev_call(itv->sd_video, core, reset, 0);
1665         break;
1666     }
1667 
1668     case IVTV_IOC_DMA_FRAME:
1669     case IVTV_IOC_PASSTHROUGH_MODE:
1670         return ivtv_decoder_ioctls(file, cmd, (void *)arg);
1671 
1672     default:
1673         return -ENOTTY;
1674     }
1675     return 0;
1676 }
1677 
1678 static const struct v4l2_ioctl_ops ivtv_ioctl_ops = {
1679     .vidioc_querycap            = ivtv_querycap,
1680     .vidioc_s_audio             = ivtv_s_audio,
1681     .vidioc_g_audio             = ivtv_g_audio,
1682     .vidioc_enumaudio           = ivtv_enumaudio,
1683     .vidioc_s_audout            = ivtv_s_audout,
1684     .vidioc_g_audout            = ivtv_g_audout,
1685     .vidioc_enum_input          = ivtv_enum_input,
1686     .vidioc_enum_output         = ivtv_enum_output,
1687     .vidioc_enumaudout          = ivtv_enumaudout,
1688     .vidioc_g_pixelaspect           = ivtv_g_pixelaspect,
1689     .vidioc_s_selection         = ivtv_s_selection,
1690     .vidioc_g_selection         = ivtv_g_selection,
1691     .vidioc_g_input             = ivtv_g_input,
1692     .vidioc_s_input             = ivtv_s_input,
1693     .vidioc_g_output            = ivtv_g_output,
1694     .vidioc_s_output            = ivtv_s_output,
1695     .vidioc_g_frequency         = ivtv_g_frequency,
1696     .vidioc_s_frequency         = ivtv_s_frequency,
1697     .vidioc_s_tuner             = ivtv_s_tuner,
1698     .vidioc_g_tuner             = ivtv_g_tuner,
1699     .vidioc_g_enc_index         = ivtv_g_enc_index,
1700     .vidioc_g_fbuf              = ivtv_g_fbuf,
1701     .vidioc_s_fbuf              = ivtv_s_fbuf,
1702     .vidioc_g_std               = ivtv_g_std,
1703     .vidioc_s_std               = ivtv_s_std,
1704     .vidioc_overlay             = ivtv_overlay,
1705     .vidioc_log_status          = ivtv_log_status,
1706     .vidioc_enum_fmt_vid_cap        = ivtv_enum_fmt_vid_cap,
1707     .vidioc_encoder_cmd         = ivtv_encoder_cmd,
1708     .vidioc_try_encoder_cmd         = ivtv_try_encoder_cmd,
1709     .vidioc_decoder_cmd         = ivtv_decoder_cmd,
1710     .vidioc_try_decoder_cmd         = ivtv_try_decoder_cmd,
1711     .vidioc_enum_fmt_vid_out        = ivtv_enum_fmt_vid_out,
1712     .vidioc_g_fmt_vid_cap           = ivtv_g_fmt_vid_cap,
1713     .vidioc_g_fmt_vbi_cap           = ivtv_g_fmt_vbi_cap,
1714     .vidioc_g_fmt_sliced_vbi_cap        = ivtv_g_fmt_sliced_vbi_cap,
1715     .vidioc_g_fmt_vid_out               = ivtv_g_fmt_vid_out,
1716     .vidioc_g_fmt_vid_out_overlay       = ivtv_g_fmt_vid_out_overlay,
1717     .vidioc_g_fmt_sliced_vbi_out        = ivtv_g_fmt_sliced_vbi_out,
1718     .vidioc_s_fmt_vid_cap           = ivtv_s_fmt_vid_cap,
1719     .vidioc_s_fmt_vbi_cap           = ivtv_s_fmt_vbi_cap,
1720     .vidioc_s_fmt_sliced_vbi_cap        = ivtv_s_fmt_sliced_vbi_cap,
1721     .vidioc_s_fmt_vid_out               = ivtv_s_fmt_vid_out,
1722     .vidioc_s_fmt_vid_out_overlay       = ivtv_s_fmt_vid_out_overlay,
1723     .vidioc_s_fmt_sliced_vbi_out        = ivtv_s_fmt_sliced_vbi_out,
1724     .vidioc_try_fmt_vid_cap         = ivtv_try_fmt_vid_cap,
1725     .vidioc_try_fmt_vbi_cap         = ivtv_try_fmt_vbi_cap,
1726     .vidioc_try_fmt_sliced_vbi_cap      = ivtv_try_fmt_sliced_vbi_cap,
1727     .vidioc_try_fmt_vid_out         = ivtv_try_fmt_vid_out,
1728     .vidioc_try_fmt_vid_out_overlay     = ivtv_try_fmt_vid_out_overlay,
1729     .vidioc_try_fmt_sliced_vbi_out      = ivtv_try_fmt_sliced_vbi_out,
1730     .vidioc_g_sliced_vbi_cap        = ivtv_g_sliced_vbi_cap,
1731 #ifdef CONFIG_VIDEO_ADV_DEBUG
1732     .vidioc_g_register          = ivtv_g_register,
1733     .vidioc_s_register          = ivtv_s_register,
1734 #endif
1735     .vidioc_default             = ivtv_default,
1736     .vidioc_subscribe_event         = ivtv_subscribe_event,
1737     .vidioc_unsubscribe_event       = v4l2_event_unsubscribe,
1738 };
1739 
1740 void ivtv_set_funcs(struct video_device *vdev)
1741 {
1742     vdev->ioctl_ops = &ivtv_ioctl_ops;
1743 }