linux/drivers/media/pci/ivtv/ivtv-ioctl.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0-or-later
   2/*
   3    ioctl system call
   4    Copyright (C) 2003-2004  Kevin Thayer <nufan_wfk at yahoo.com>
   5    Copyright (C) 2005-2007  Hans Verkuil <hverkuil@xs4all.nl>
   6
   7 */
   8
   9#include "ivtv-driver.h"
  10#include "ivtv-version.h"
  11#include "ivtv-mailbox.h"
  12#include "ivtv-i2c.h"
  13#include "ivtv-queue.h"
  14#include "ivtv-fileops.h"
  15#include "ivtv-vbi.h"
  16#include "ivtv-routing.h"
  17#include "ivtv-streams.h"
  18#include "ivtv-yuv.h"
  19#include "ivtv-ioctl.h"
  20#include "ivtv-gpio.h"
  21#include "ivtv-controls.h"
  22#include "ivtv-cards.h"
  23#include <media/i2c/saa7127.h>
  24#include <media/tveeprom.h>
  25#include <media/v4l2-event.h>
  26#ifdef CONFIG_VIDEO_IVTV_DEPRECATED_IOCTLS
  27#include <linux/compat.h>
  28#include <linux/dvb/audio.h>
  29#include <linux/dvb/video.h>
  30#endif
  31
  32u16 ivtv_service2vbi(int type)
  33{
  34        switch (type) {
  35                case V4L2_SLICED_TELETEXT_B:
  36                        return IVTV_SLICED_TYPE_TELETEXT_B;
  37                case V4L2_SLICED_CAPTION_525:
  38                        return IVTV_SLICED_TYPE_CAPTION_525;
  39                case V4L2_SLICED_WSS_625:
  40                        return IVTV_SLICED_TYPE_WSS_625;
  41                case V4L2_SLICED_VPS:
  42                        return IVTV_SLICED_TYPE_VPS;
  43                default:
  44                        return 0;
  45        }
  46}
  47
  48static int valid_service_line(int field, int line, int is_pal)
  49{
  50        return (is_pal && line >= 6 && (line != 23 || field == 0)) ||
  51               (!is_pal && line >= 10 && line < 22);
  52}
  53
  54static u16 select_service_from_set(int field, int line, u16 set, int is_pal)
  55{
  56        u16 valid_set = (is_pal ? V4L2_SLICED_VBI_625 : V4L2_SLICED_VBI_525);
  57        int i;
  58
  59        set = set & valid_set;
  60        if (set == 0 || !valid_service_line(field, line, is_pal)) {
  61                return 0;
  62        }
  63        if (!is_pal) {
  64                if (line == 21 && (set & V4L2_SLICED_CAPTION_525))
  65                        return V4L2_SLICED_CAPTION_525;
  66        }
  67        else {
  68                if (line == 16 && field == 0 && (set & V4L2_SLICED_VPS))
  69                        return V4L2_SLICED_VPS;
  70                if (line == 23 && field == 0 && (set & V4L2_SLICED_WSS_625))
  71                        return V4L2_SLICED_WSS_625;
  72                if (line == 23)
  73                        return 0;
  74        }
  75        for (i = 0; i < 32; i++) {
  76                if (BIT(i) & set)
  77                        return BIT(i);
  78        }
  79        return 0;
  80}
  81
  82void ivtv_expand_service_set(struct v4l2_sliced_vbi_format *fmt, int is_pal)
  83{
  84        u16 set = fmt->service_set;
  85        int f, l;
  86
  87        fmt->service_set = 0;
  88        for (f = 0; f < 2; f++) {
  89                for (l = 0; l < 24; l++) {
  90                        fmt->service_lines[f][l] = select_service_from_set(f, l, set, is_pal);
  91                }
  92        }
  93}
  94
  95static void check_service_set(struct v4l2_sliced_vbi_format *fmt, int is_pal)
  96{
  97        int f, l;
  98
  99        for (f = 0; f < 2; f++) {
 100                for (l = 0; l < 24; l++) {
 101                        fmt->service_lines[f][l] = select_service_from_set(f, l, fmt->service_lines[f][l], is_pal);
 102                }
 103        }
 104}
 105
 106u16 ivtv_get_service_set(struct v4l2_sliced_vbi_format *fmt)
 107{
 108        int f, l;
 109        u16 set = 0;
 110
 111        for (f = 0; f < 2; f++) {
 112                for (l = 0; l < 24; l++) {
 113                        set |= fmt->service_lines[f][l];
 114                }
 115        }
 116        return set;
 117}
 118
 119void ivtv_set_osd_alpha(struct ivtv *itv)
 120{
 121        ivtv_vapi(itv, CX2341X_OSD_SET_GLOBAL_ALPHA, 3,
 122                itv->osd_global_alpha_state, itv->osd_global_alpha, !itv->osd_local_alpha_state);
 123        ivtv_vapi(itv, CX2341X_OSD_SET_CHROMA_KEY, 2, itv->osd_chroma_key_state, itv->osd_chroma_key);
 124}
 125
 126int ivtv_set_speed(struct ivtv *itv, int speed)
 127{
 128        u32 data[CX2341X_MBOX_MAX_DATA];
 129        int single_step = (speed == 1 || speed == -1);
 130        DEFINE_WAIT(wait);
 131
 132        if (speed == 0) speed = 1000;
 133
 134        /* No change? */
 135        if (speed == itv->speed && !single_step)
 136                return 0;
 137
 138        if (single_step && (speed < 0) == (itv->speed < 0)) {
 139                /* Single step video and no need to change direction */
 140                ivtv_vapi(itv, CX2341X_DEC_STEP_VIDEO, 1, 0);
 141                itv->speed = speed;
 142                return 0;
 143        }
 144        if (single_step)
 145                /* Need to change direction */
 146                speed = speed < 0 ? -1000 : 1000;
 147
 148        data[0] = (speed > 1000 || speed < -1000) ? 0x80000000 : 0;
 149        data[0] |= (speed > 1000 || speed < -1500) ? 0x40000000 : 0;
 150        data[1] = (speed < 0);
 151        data[2] = speed < 0 ? 3 : 7;
 152        data[3] = v4l2_ctrl_g_ctrl(itv->cxhdl.video_b_frames);
 153        data[4] = (speed == 1500 || speed == 500) ? itv->speed_mute_audio : 0;
 154        data[5] = 0;
 155        data[6] = 0;
 156
 157        if (speed == 1500 || speed == -1500) data[0] |= 1;
 158        else if (speed == 2000 || speed == -2000) data[0] |= 2;
 159        else if (speed > -1000 && speed < 0) data[0] |= (-1000 / speed);
 160        else if (speed < 1000 && speed > 0) data[0] |= (1000 / speed);
 161
 162        /* If not decoding, just change speed setting */
 163        if (atomic_read(&itv->decoding) > 0) {
 164                int got_sig = 0;
 165
 166                /* Stop all DMA and decoding activity */
 167                ivtv_vapi(itv, CX2341X_DEC_PAUSE_PLAYBACK, 1, 0);
 168
 169                /* Wait for any DMA to finish */
 170                mutex_unlock(&itv->serialize_lock);
 171                prepare_to_wait(&itv->dma_waitq, &wait, TASK_INTERRUPTIBLE);
 172                while (test_bit(IVTV_F_I_DMA, &itv->i_flags)) {
 173                        got_sig = signal_pending(current);
 174                        if (got_sig)
 175                                break;
 176                        got_sig = 0;
 177                        schedule();
 178                }
 179                finish_wait(&itv->dma_waitq, &wait);
 180                mutex_lock(&itv->serialize_lock);
 181                if (got_sig)
 182                        return -EINTR;
 183
 184                /* Change Speed safely */
 185                ivtv_api(itv, CX2341X_DEC_SET_PLAYBACK_SPEED, 7, data);
 186                IVTV_DEBUG_INFO("Setting Speed to 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x\n",
 187                                data[0], data[1], data[2], data[3], data[4], data[5], data[6]);
 188        }
 189        if (single_step) {
 190                speed = (speed < 0) ? -1 : 1;
 191                ivtv_vapi(itv, CX2341X_DEC_STEP_VIDEO, 1, 0);
 192        }
 193        itv->speed = speed;
 194        return 0;
 195}
 196
 197static int ivtv_validate_speed(int cur_speed, int new_speed)
 198{
 199        int fact = new_speed < 0 ? -1 : 1;
 200        int s;
 201
 202        if (cur_speed == 0)
 203                cur_speed = 1000;
 204        if (new_speed < 0)
 205                new_speed = -new_speed;
 206        if (cur_speed < 0)
 207                cur_speed = -cur_speed;
 208
 209        if (cur_speed <= new_speed) {
 210                if (new_speed > 1500)
 211                        return fact * 2000;
 212                if (new_speed > 1000)
 213                        return fact * 1500;
 214        }
 215        else {
 216                if (new_speed >= 2000)
 217                        return fact * 2000;
 218                if (new_speed >= 1500)
 219                        return fact * 1500;
 220                if (new_speed >= 1000)
 221                        return fact * 1000;
 222        }
 223        if (new_speed == 0)
 224                return 1000;
 225        if (new_speed == 1 || new_speed == 1000)
 226                return fact * new_speed;
 227
 228        s = new_speed;
 229        new_speed = 1000 / new_speed;
 230        if (1000 / cur_speed == new_speed)
 231                new_speed += (cur_speed < s) ? -1 : 1;
 232        if (new_speed > 60) return 1000 / (fact * 60);
 233        return 1000 / (fact * new_speed);
 234}
 235
 236static int ivtv_video_command(struct ivtv *itv, struct ivtv_open_id *id,
 237                struct v4l2_decoder_cmd *dc, int try)
 238{
 239        struct ivtv_stream *s = &itv->streams[IVTV_DEC_STREAM_TYPE_MPG];
 240
 241        if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
 242                return -EINVAL;
 243
 244        switch (dc->cmd) {
 245        case V4L2_DEC_CMD_START: {
 246                dc->flags &= V4L2_DEC_CMD_START_MUTE_AUDIO;
 247                dc->start.speed = ivtv_validate_speed(itv->speed, dc->start.speed);
 248                if (dc->start.speed < 0)
 249                        dc->start.format = V4L2_DEC_START_FMT_GOP;
 250                else
 251                        dc->start.format = V4L2_DEC_START_FMT_NONE;
 252                if (dc->start.speed != 500 && dc->start.speed != 1500)
 253                        dc->flags = dc->start.speed == 1000 ? 0 :
 254                                        V4L2_DEC_CMD_START_MUTE_AUDIO;
 255                if (try) break;
 256
 257                itv->speed_mute_audio = dc->flags & V4L2_DEC_CMD_START_MUTE_AUDIO;
 258                if (ivtv_set_output_mode(itv, OUT_MPG) != OUT_MPG)
 259                        return -EBUSY;
 260                if (test_and_clear_bit(IVTV_F_I_DEC_PAUSED, &itv->i_flags)) {
 261                        /* forces ivtv_set_speed to be called */
 262                        itv->speed = 0;
 263                }
 264                return ivtv_start_decoding(id, dc->start.speed);
 265        }
 266
 267        case V4L2_DEC_CMD_STOP:
 268                dc->flags &= V4L2_DEC_CMD_STOP_IMMEDIATELY | V4L2_DEC_CMD_STOP_TO_BLACK;
 269                if (dc->flags & V4L2_DEC_CMD_STOP_IMMEDIATELY)
 270                        dc->stop.pts = 0;
 271                if (try) break;
 272                if (atomic_read(&itv->decoding) == 0)
 273                        return 0;
 274                if (itv->output_mode != OUT_MPG)
 275                        return -EBUSY;
 276
 277                itv->output_mode = OUT_NONE;
 278                return ivtv_stop_v4l2_decode_stream(s, dc->flags, dc->stop.pts);
 279
 280        case V4L2_DEC_CMD_PAUSE:
 281                dc->flags &= V4L2_DEC_CMD_PAUSE_TO_BLACK;
 282                if (try) break;
 283                if (!atomic_read(&itv->decoding))
 284                        return -EPERM;
 285                if (itv->output_mode != OUT_MPG)
 286                        return -EBUSY;
 287                if (atomic_read(&itv->decoding) > 0) {
 288                        ivtv_vapi(itv, CX2341X_DEC_PAUSE_PLAYBACK, 1,
 289                                (dc->flags & V4L2_DEC_CMD_PAUSE_TO_BLACK) ? 1 : 0);
 290                        set_bit(IVTV_F_I_DEC_PAUSED, &itv->i_flags);
 291                }
 292                break;
 293
 294        case V4L2_DEC_CMD_RESUME:
 295                dc->flags = 0;
 296                if (try) break;
 297                if (!atomic_read(&itv->decoding))
 298                        return -EPERM;
 299                if (itv->output_mode != OUT_MPG)
 300                        return -EBUSY;
 301                if (test_and_clear_bit(IVTV_F_I_DEC_PAUSED, &itv->i_flags)) {
 302                        int speed = itv->speed;
 303                        itv->speed = 0;
 304                        return ivtv_start_decoding(id, speed);
 305                }
 306                break;
 307
 308        default:
 309                return -EINVAL;
 310        }
 311        return 0;
 312}
 313
 314static int ivtv_g_fmt_sliced_vbi_out(struct file *file, void *fh, struct v4l2_format *fmt)
 315{
 316        struct ivtv *itv = fh2id(fh)->itv;
 317        struct v4l2_sliced_vbi_format *vbifmt = &fmt->fmt.sliced;
 318
 319        vbifmt->reserved[0] = 0;
 320        vbifmt->reserved[1] = 0;
 321        if (!(itv->v4l2_cap & V4L2_CAP_SLICED_VBI_OUTPUT))
 322                return -EINVAL;
 323        vbifmt->io_size = sizeof(struct v4l2_sliced_vbi_data) * 36;
 324        memset(vbifmt->service_lines, 0, sizeof(vbifmt->service_lines));
 325        if (itv->is_60hz) {
 326                vbifmt->service_lines[0][21] = V4L2_SLICED_CAPTION_525;
 327                vbifmt->service_lines[1][21] = V4L2_SLICED_CAPTION_525;
 328        } else {
 329                vbifmt->service_lines[0][23] = V4L2_SLICED_WSS_625;
 330                vbifmt->service_lines[0][16] = V4L2_SLICED_VPS;
 331        }
 332        vbifmt->service_set = ivtv_get_service_set(vbifmt);
 333        return 0;
 334}
 335
 336static int ivtv_g_fmt_vid_cap(struct file *file, void *fh, struct v4l2_format *fmt)
 337{
 338        struct ivtv_open_id *id = fh2id(fh);
 339        struct ivtv *itv = id->itv;
 340        struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
 341
 342        pixfmt->width = itv->cxhdl.width;
 343        pixfmt->height = itv->cxhdl.height;
 344        pixfmt->colorspace = V4L2_COLORSPACE_SMPTE170M;
 345        pixfmt->field = V4L2_FIELD_INTERLACED;
 346        if (id->type == IVTV_ENC_STREAM_TYPE_YUV) {
 347                pixfmt->pixelformat = V4L2_PIX_FMT_HM12;
 348                /* YUV size is (Y=(h*720) + UV=(h*(720/2))) */
 349                pixfmt->sizeimage = pixfmt->height * 720 * 3 / 2;
 350                pixfmt->bytesperline = 720;
 351        } else {
 352                pixfmt->pixelformat = V4L2_PIX_FMT_MPEG;
 353                pixfmt->sizeimage = 128 * 1024;
 354                pixfmt->bytesperline = 0;
 355        }
 356        return 0;
 357}
 358
 359static int ivtv_g_fmt_vbi_cap(struct file *file, void *fh, struct v4l2_format *fmt)
 360{
 361        struct ivtv *itv = fh2id(fh)->itv;
 362        struct v4l2_vbi_format *vbifmt = &fmt->fmt.vbi;
 363
 364        vbifmt->sampling_rate = 27000000;
 365        vbifmt->offset = 248;
 366        vbifmt->samples_per_line = itv->vbi.raw_decoder_line_size - 4;
 367        vbifmt->sample_format = V4L2_PIX_FMT_GREY;
 368        vbifmt->start[0] = itv->vbi.start[0];
 369        vbifmt->start[1] = itv->vbi.start[1];
 370        vbifmt->count[0] = vbifmt->count[1] = itv->vbi.count;
 371        vbifmt->flags = 0;
 372        vbifmt->reserved[0] = 0;
 373        vbifmt->reserved[1] = 0;
 374        return 0;
 375}
 376
 377static int ivtv_g_fmt_sliced_vbi_cap(struct file *file, void *fh, struct v4l2_format *fmt)
 378{
 379        struct v4l2_sliced_vbi_format *vbifmt = &fmt->fmt.sliced;
 380        struct ivtv_open_id *id = fh2id(fh);
 381        struct ivtv *itv = id->itv;
 382
 383        vbifmt->reserved[0] = 0;
 384        vbifmt->reserved[1] = 0;
 385        vbifmt->io_size = sizeof(struct v4l2_sliced_vbi_data) * 36;
 386
 387        if (id->type == IVTV_DEC_STREAM_TYPE_VBI) {
 388                vbifmt->service_set = itv->is_50hz ? V4L2_SLICED_VBI_625 :
 389                        V4L2_SLICED_VBI_525;
 390                ivtv_expand_service_set(vbifmt, itv->is_50hz);
 391                vbifmt->service_set = ivtv_get_service_set(vbifmt);
 392                return 0;
 393        }
 394
 395        v4l2_subdev_call(itv->sd_video, vbi, g_sliced_fmt, vbifmt);
 396        vbifmt->service_set = ivtv_get_service_set(vbifmt);
 397        return 0;
 398}
 399
 400static int ivtv_g_fmt_vid_out(struct file *file, void *fh, struct v4l2_format *fmt)
 401{
 402        struct ivtv_open_id *id = fh2id(fh);
 403        struct ivtv *itv = id->itv;
 404        struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
 405
 406        if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
 407                return -EINVAL;
 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;
 412        if (id->type == IVTV_DEC_STREAM_TYPE_YUV) {
 413                switch (itv->yuv_info.lace_mode & IVTV_YUV_MODE_MASK) {
 414                case IVTV_YUV_MODE_INTERLACED:
 415                        pixfmt->field = (itv->yuv_info.lace_mode & IVTV_YUV_SYNC_MASK) ?
 416                                V4L2_FIELD_INTERLACED_BT : V4L2_FIELD_INTERLACED_TB;
 417                        break;
 418                case IVTV_YUV_MODE_PROGRESSIVE:
 419                        pixfmt->field = V4L2_FIELD_NONE;
 420                        break;
 421                default:
 422                        pixfmt->field = V4L2_FIELD_ANY;
 423                        break;
 424                }
 425                pixfmt->pixelformat = V4L2_PIX_FMT_HM12;
 426                pixfmt->bytesperline = 720;
 427                pixfmt->width = itv->yuv_info.v4l2_src_w;
 428                pixfmt->height = itv->yuv_info.v4l2_src_h;
 429                /* YUV size is (Y=(h*w) + UV=(h*(w/2))) */
 430                pixfmt->sizeimage =
 431                        1080 * ((pixfmt->height + 31) & ~31);
 432        } else {
 433                pixfmt->pixelformat = V4L2_PIX_FMT_MPEG;
 434                pixfmt->sizeimage = 128 * 1024;
 435                pixfmt->bytesperline = 0;
 436        }
 437        return 0;
 438}
 439
 440static int ivtv_g_fmt_vid_out_overlay(struct file *file, void *fh, struct v4l2_format *fmt)
 441{
 442        struct ivtv *itv = fh2id(fh)->itv;
 443        struct ivtv_stream *s = &itv->streams[fh2id(fh)->type];
 444        struct v4l2_window *winfmt = &fmt->fmt.win;
 445
 446        if (!(s->caps & V4L2_CAP_VIDEO_OUTPUT_OVERLAY))
 447                return -EINVAL;
 448        if (!itv->osd_video_pbase)
 449                return -EINVAL;
 450        winfmt->chromakey = itv->osd_chroma_key;
 451        winfmt->global_alpha = itv->osd_global_alpha;
 452        winfmt->field = V4L2_FIELD_INTERLACED;
 453        winfmt->clips = NULL;
 454        winfmt->clipcount = 0;
 455        winfmt->bitmap = NULL;
 456        winfmt->w.top = winfmt->w.left = 0;
 457        winfmt->w.width = itv->osd_rect.width;
 458        winfmt->w.height = itv->osd_rect.height;
 459        return 0;
 460}
 461
 462static int ivtv_try_fmt_sliced_vbi_out(struct file *file, void *fh, struct v4l2_format *fmt)
 463{
 464        return ivtv_g_fmt_sliced_vbi_out(file, fh, fmt);
 465}
 466
 467static int ivtv_try_fmt_vid_cap(struct file *file, void *fh, struct v4l2_format *fmt)
 468{
 469        struct ivtv_open_id *id = fh2id(fh);
 470        struct ivtv *itv = id->itv;
 471        int w = fmt->fmt.pix.width;
 472        int h = fmt->fmt.pix.height;
 473        int min_h = 2;
 474
 475        w = min(w, 720);
 476        w = max(w, 2);
 477        if (id->type == IVTV_ENC_STREAM_TYPE_YUV) {
 478                /* YUV height must be a multiple of 32 */
 479                h &= ~0x1f;
 480                min_h = 32;
 481        }
 482        h = min(h, itv->is_50hz ? 576 : 480);
 483        h = max(h, min_h);
 484        ivtv_g_fmt_vid_cap(file, fh, fmt);
 485        fmt->fmt.pix.width = w;
 486        fmt->fmt.pix.height = h;
 487        return 0;
 488}
 489
 490static int ivtv_try_fmt_vbi_cap(struct file *file, void *fh, struct v4l2_format *fmt)
 491{
 492        return ivtv_g_fmt_vbi_cap(file, fh, fmt);
 493}
 494
 495static int ivtv_try_fmt_sliced_vbi_cap(struct file *file, void *fh, struct v4l2_format *fmt)
 496{
 497        struct v4l2_sliced_vbi_format *vbifmt = &fmt->fmt.sliced;
 498        struct ivtv_open_id *id = fh2id(fh);
 499        struct ivtv *itv = id->itv;
 500
 501        if (id->type == IVTV_DEC_STREAM_TYPE_VBI)
 502                return ivtv_g_fmt_sliced_vbi_cap(file, fh, fmt);
 503
 504        /* set sliced VBI capture format */
 505        vbifmt->io_size = sizeof(struct v4l2_sliced_vbi_data) * 36;
 506        vbifmt->reserved[0] = 0;
 507        vbifmt->reserved[1] = 0;
 508
 509        if (vbifmt->service_set)
 510                ivtv_expand_service_set(vbifmt, itv->is_50hz);
 511        check_service_set(vbifmt, itv->is_50hz);
 512        vbifmt->service_set = ivtv_get_service_set(vbifmt);
 513        return 0;
 514}
 515
 516static int ivtv_try_fmt_vid_out(struct file *file, void *fh, struct v4l2_format *fmt)
 517{
 518        struct ivtv_open_id *id = fh2id(fh);
 519        s32 w = fmt->fmt.pix.width;
 520        s32 h = fmt->fmt.pix.height;
 521        int field = fmt->fmt.pix.field;
 522        int ret = ivtv_g_fmt_vid_out(file, fh, fmt);
 523
 524        w = min(w, 720);
 525        w = max(w, 2);
 526        /* Why can the height be 576 even when the output is NTSC?
 527
 528           Internally the buffers of the PVR350 are always set to 720x576. The
 529           decoded video frame will always be placed in the top left corner of
 530           this buffer. For any video which is not 720x576, the buffer will
 531           then be cropped to remove the unused right and lower areas, with
 532           the remaining image being scaled by the hardware to fit the display
 533           area. The video can be scaled both up and down, so a 720x480 video
 534           can be displayed full-screen on PAL and a 720x576 video can be
 535           displayed without cropping on NTSC.
 536
 537           Note that the scaling only occurs on the video stream, the osd
 538           resolution is locked to the broadcast standard and not scaled.
 539
 540           Thanks to Ian Armstrong for this explanation. */
 541        h = min(h, 576);
 542        h = max(h, 2);
 543        if (id->type == IVTV_DEC_STREAM_TYPE_YUV)
 544                fmt->fmt.pix.field = field;
 545        fmt->fmt.pix.width = w;
 546        fmt->fmt.pix.height = h;
 547        return ret;
 548}
 549
 550static int ivtv_try_fmt_vid_out_overlay(struct file *file, void *fh, struct v4l2_format *fmt)
 551{
 552        struct ivtv *itv = fh2id(fh)->itv;
 553        struct ivtv_stream *s = &itv->streams[fh2id(fh)->type];
 554        u32 chromakey = fmt->fmt.win.chromakey;
 555        u8 global_alpha = fmt->fmt.win.global_alpha;
 556
 557        if (!(s->caps & V4L2_CAP_VIDEO_OUTPUT_OVERLAY))
 558                return -EINVAL;
 559        if (!itv->osd_video_pbase)
 560                return -EINVAL;
 561        ivtv_g_fmt_vid_out_overlay(file, fh, fmt);
 562        fmt->fmt.win.chromakey = chromakey;
 563        fmt->fmt.win.global_alpha = global_alpha;
 564        return 0;
 565}
 566
 567static int ivtv_s_fmt_sliced_vbi_out(struct file *file, void *fh, struct v4l2_format *fmt)
 568{
 569        return ivtv_g_fmt_sliced_vbi_out(file, fh, fmt);
 570}
 571
 572static int ivtv_s_fmt_vid_cap(struct file *file, void *fh, struct v4l2_format *fmt)
 573{
 574        struct ivtv_open_id *id = fh2id(fh);
 575        struct ivtv *itv = id->itv;
 576        struct v4l2_subdev_format format = {
 577                .which = V4L2_SUBDEV_FORMAT_ACTIVE,
 578        };
 579        int ret = ivtv_try_fmt_vid_cap(file, fh, fmt);
 580        int w = fmt->fmt.pix.width;
 581        int h = fmt->fmt.pix.height;
 582
 583        if (ret)
 584                return ret;
 585
 586        if (itv->cxhdl.width == w && itv->cxhdl.height == h)
 587                return 0;
 588
 589        if (atomic_read(&itv->capturing) > 0)
 590                return -EBUSY;
 591
 592        itv->cxhdl.width = w;
 593        itv->cxhdl.height = h;
 594        if (v4l2_ctrl_g_ctrl(itv->cxhdl.video_encoding) == V4L2_MPEG_VIDEO_ENCODING_MPEG_1)
 595                fmt->fmt.pix.width /= 2;
 596        format.format.width = fmt->fmt.pix.width;
 597        format.format.height = h;
 598        format.format.code = MEDIA_BUS_FMT_FIXED;
 599        v4l2_subdev_call(itv->sd_video, pad, set_fmt, NULL, &format);
 600        return ivtv_g_fmt_vid_cap(file, fh, fmt);
 601}
 602
 603static int ivtv_s_fmt_vbi_cap(struct file *file, void *fh, struct v4l2_format *fmt)
 604{
 605        struct ivtv *itv = fh2id(fh)->itv;
 606
 607        if (!ivtv_raw_vbi(itv) && atomic_read(&itv->capturing) > 0)
 608                return -EBUSY;
 609        itv->vbi.sliced_in->service_set = 0;
 610        itv->vbi.in.type = V4L2_BUF_TYPE_VBI_CAPTURE;
 611        v4l2_subdev_call(itv->sd_video, vbi, s_raw_fmt, &fmt->fmt.vbi);
 612        return ivtv_g_fmt_vbi_cap(file, fh, fmt);
 613}
 614
 615static int ivtv_s_fmt_sliced_vbi_cap(struct file *file, void *fh, struct v4l2_format *fmt)
 616{
 617        struct v4l2_sliced_vbi_format *vbifmt = &fmt->fmt.sliced;
 618        struct ivtv_open_id *id = fh2id(fh);
 619        struct ivtv *itv = id->itv;
 620        int ret = ivtv_try_fmt_sliced_vbi_cap(file, fh, fmt);
 621
 622        if (ret || id->type == IVTV_DEC_STREAM_TYPE_VBI)
 623                return ret;
 624
 625        check_service_set(vbifmt, itv->is_50hz);
 626        if (ivtv_raw_vbi(itv) && atomic_read(&itv->capturing) > 0)
 627                return -EBUSY;
 628        itv->vbi.in.type = V4L2_BUF_TYPE_SLICED_VBI_CAPTURE;
 629        v4l2_subdev_call(itv->sd_video, vbi, s_sliced_fmt, vbifmt);
 630        memcpy(itv->vbi.sliced_in, vbifmt, sizeof(*itv->vbi.sliced_in));
 631        return 0;
 632}
 633
 634static int ivtv_s_fmt_vid_out(struct file *file, void *fh, struct v4l2_format *fmt)
 635{
 636        struct ivtv_open_id *id = fh2id(fh);
 637        struct ivtv *itv = id->itv;
 638        struct yuv_playback_info *yi = &itv->yuv_info;
 639        int ret = ivtv_try_fmt_vid_out(file, fh, fmt);
 640
 641        if (ret)
 642                return ret;
 643
 644        if (id->type != IVTV_DEC_STREAM_TYPE_YUV)
 645                return 0;
 646
 647        /* Return now if we already have some frame data */
 648        if (yi->stream_size)
 649                return -EBUSY;
 650
 651        yi->v4l2_src_w = fmt->fmt.pix.width;
 652        yi->v4l2_src_h = fmt->fmt.pix.height;
 653
 654        switch (fmt->fmt.pix.field) {
 655        case V4L2_FIELD_NONE:
 656                yi->lace_mode = IVTV_YUV_MODE_PROGRESSIVE;
 657                break;
 658        case V4L2_FIELD_ANY:
 659                yi->lace_mode = IVTV_YUV_MODE_AUTO;
 660                break;
 661        case V4L2_FIELD_INTERLACED_BT:
 662                yi->lace_mode =
 663                        IVTV_YUV_MODE_INTERLACED|IVTV_YUV_SYNC_ODD;
 664                break;
 665        case V4L2_FIELD_INTERLACED_TB:
 666        default:
 667                yi->lace_mode = IVTV_YUV_MODE_INTERLACED;
 668                break;
 669        }
 670        yi->lace_sync_field = (yi->lace_mode & IVTV_YUV_SYNC_MASK) == IVTV_YUV_SYNC_EVEN ? 0 : 1;
 671
 672        if (test_bit(IVTV_F_I_DEC_YUV, &itv->i_flags))
 673                itv->dma_data_req_size =
 674                        1080 * ((yi->v4l2_src_h + 31) & ~31);
 675
 676        return 0;
 677}
 678
 679static int ivtv_s_fmt_vid_out_overlay(struct file *file, void *fh, struct v4l2_format *fmt)
 680{
 681        struct ivtv *itv = fh2id(fh)->itv;
 682        int ret = ivtv_try_fmt_vid_out_overlay(file, fh, fmt);
 683
 684        if (ret == 0) {
 685                itv->osd_chroma_key = fmt->fmt.win.chromakey;
 686                itv->osd_global_alpha = fmt->fmt.win.global_alpha;
 687                ivtv_set_osd_alpha(itv);
 688        }
 689        return ret;
 690}
 691
 692#ifdef CONFIG_VIDEO_ADV_DEBUG
 693static int ivtv_itvc(struct ivtv *itv, bool get, u64 reg, u64 *val)
 694{
 695        volatile u8 __iomem *reg_start;
 696
 697        if (reg & 0x3)
 698                return -EINVAL;
 699        if (reg >= IVTV_REG_OFFSET && reg < IVTV_REG_OFFSET + IVTV_REG_SIZE)
 700                reg_start = itv->reg_mem - IVTV_REG_OFFSET;
 701        else if (itv->has_cx23415 && reg >= IVTV_DECODER_OFFSET &&
 702                        reg < IVTV_DECODER_OFFSET + IVTV_DECODER_SIZE)
 703                reg_start = itv->dec_mem - IVTV_DECODER_OFFSET;
 704        else if (reg < IVTV_ENCODER_SIZE)
 705                reg_start = itv->enc_mem;
 706        else
 707                return -EINVAL;
 708
 709        if (get)
 710                *val = readl(reg + reg_start);
 711        else
 712                writel(*val, reg + reg_start);
 713        return 0;
 714}
 715
 716static int ivtv_g_register(struct file *file, void *fh, struct v4l2_dbg_register *reg)
 717{
 718        struct ivtv *itv = fh2id(fh)->itv;
 719
 720        reg->size = 4;
 721        return ivtv_itvc(itv, true, reg->reg, &reg->val);
 722}
 723
 724static int ivtv_s_register(struct file *file, void *fh, const struct v4l2_dbg_register *reg)
 725{
 726        struct ivtv *itv = fh2id(fh)->itv;
 727        u64 val = reg->val;
 728
 729        return ivtv_itvc(itv, false, reg->reg, &val);
 730}
 731#endif
 732
 733static int ivtv_querycap(struct file *file, void *fh, struct v4l2_capability *vcap)
 734{
 735        struct ivtv_open_id *id = fh2id(file->private_data);
 736        struct ivtv *itv = id->itv;
 737
 738        strscpy(vcap->driver, IVTV_DRIVER_NAME, sizeof(vcap->driver));
 739        strscpy(vcap->card, itv->card_name, sizeof(vcap->card));
 740        snprintf(vcap->bus_info, sizeof(vcap->bus_info), "PCI:%s", pci_name(itv->pdev));
 741        vcap->capabilities = itv->v4l2_cap | V4L2_CAP_DEVICE_CAPS;
 742        return 0;
 743}
 744
 745static int ivtv_enumaudio(struct file *file, void *fh, struct v4l2_audio *vin)
 746{
 747        struct ivtv *itv = fh2id(fh)->itv;
 748
 749        return ivtv_get_audio_input(itv, vin->index, vin);
 750}
 751
 752static int ivtv_g_audio(struct file *file, void *fh, struct v4l2_audio *vin)
 753{
 754        struct ivtv *itv = fh2id(fh)->itv;
 755
 756        vin->index = itv->audio_input;
 757        return ivtv_get_audio_input(itv, vin->index, vin);
 758}
 759
 760static int ivtv_s_audio(struct file *file, void *fh, const struct v4l2_audio *vout)
 761{
 762        struct ivtv *itv = fh2id(fh)->itv;
 763
 764        if (vout->index >= itv->nof_audio_inputs)
 765                return -EINVAL;
 766
 767        itv->audio_input = vout->index;
 768        ivtv_audio_set_io(itv);
 769
 770        return 0;
 771}
 772
 773static int ivtv_enumaudout(struct file *file, void *fh, struct v4l2_audioout *vin)
 774{
 775        struct ivtv *itv = fh2id(fh)->itv;
 776
 777        /* set it to defaults from our table */
 778        return ivtv_get_audio_output(itv, vin->index, vin);
 779}
 780
 781static int ivtv_g_audout(struct file *file, void *fh, struct v4l2_audioout *vin)
 782{
 783        struct ivtv *itv = fh2id(fh)->itv;
 784
 785        vin->index = 0;
 786        return ivtv_get_audio_output(itv, vin->index, vin);
 787}
 788
 789static int ivtv_s_audout(struct file *file, void *fh, const struct v4l2_audioout *vout)
 790{
 791        struct ivtv *itv = fh2id(fh)->itv;
 792
 793        if (itv->card->video_outputs == NULL || vout->index != 0)
 794                return -EINVAL;
 795        return 0;
 796}
 797
 798static int ivtv_enum_input(struct file *file, void *fh, struct v4l2_input *vin)
 799{
 800        struct ivtv *itv = fh2id(fh)->itv;
 801
 802        /* set it to defaults from our table */
 803        return ivtv_get_input(itv, vin->index, vin);
 804}
 805
 806static int ivtv_enum_output(struct file *file, void *fh, struct v4l2_output *vout)
 807{
 808        struct ivtv *itv = fh2id(fh)->itv;
 809
 810        return ivtv_get_output(itv, vout->index, vout);
 811}
 812
 813static int ivtv_g_pixelaspect(struct file *file, void *fh,
 814                              int type, struct v4l2_fract *f)
 815{
 816        struct ivtv_open_id *id = fh2id(fh);
 817        struct ivtv *itv = id->itv;
 818
 819        if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
 820                f->numerator = itv->is_50hz ? 54 : 11;
 821                f->denominator = itv->is_50hz ? 59 : 10;
 822        } else if (type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
 823                f->numerator = itv->is_out_50hz ? 54 : 11;
 824                f->denominator = itv->is_out_50hz ? 59 : 10;
 825        } else {
 826                return -EINVAL;
 827        }
 828        return 0;
 829}
 830
 831static int ivtv_s_selection(struct file *file, void *fh,
 832                            struct v4l2_selection *sel)
 833{
 834        struct ivtv_open_id *id = fh2id(fh);
 835        struct ivtv *itv = id->itv;
 836        struct yuv_playback_info *yi = &itv->yuv_info;
 837        struct v4l2_rect r = { 0, 0, 720, 0 };
 838        int streamtype = id->type;
 839
 840        if (sel->type != V4L2_BUF_TYPE_VIDEO_OUTPUT ||
 841            !(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
 842                return -EINVAL;
 843
 844        if (sel->target != V4L2_SEL_TGT_COMPOSE)
 845                return -EINVAL;
 846
 847
 848        if (sel->type != V4L2_BUF_TYPE_VIDEO_OUTPUT ||
 849            !(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
 850                return -EINVAL;
 851
 852        r.height = itv->is_out_50hz ? 576 : 480;
 853        if (streamtype == IVTV_DEC_STREAM_TYPE_YUV && yi->track_osd) {
 854                r.width = yi->osd_full_w;
 855                r.height = yi->osd_full_h;
 856        }
 857        sel->r.width = clamp(sel->r.width, 16U, r.width);
 858        sel->r.height = clamp(sel->r.height, 16U, r.height);
 859        sel->r.left = clamp_t(unsigned, sel->r.left, 0, r.width - sel->r.width);
 860        sel->r.top = clamp_t(unsigned, sel->r.top, 0, r.height - sel->r.height);
 861
 862        if (streamtype == IVTV_DEC_STREAM_TYPE_YUV) {
 863                yi->main_rect = sel->r;
 864                return 0;
 865        }
 866        if (!ivtv_vapi(itv, CX2341X_OSD_SET_FRAMEBUFFER_WINDOW, 4,
 867                        sel->r.width, sel->r.height, sel->r.left, sel->r.top)) {
 868                itv->main_rect = sel->r;
 869                return 0;
 870        }
 871        return -EINVAL;
 872}
 873
 874static int ivtv_g_selection(struct file *file, void *fh,
 875                            struct v4l2_selection *sel)
 876{
 877        struct ivtv_open_id *id = fh2id(fh);
 878        struct ivtv *itv = id->itv;
 879        struct yuv_playback_info *yi = &itv->yuv_info;
 880        struct v4l2_rect r = { 0, 0, 720, 0 };
 881        int streamtype = id->type;
 882
 883        if (sel->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
 884                switch (sel->target) {
 885                case V4L2_SEL_TGT_CROP_DEFAULT:
 886                case V4L2_SEL_TGT_CROP_BOUNDS:
 887                        sel->r.top = sel->r.left = 0;
 888                        sel->r.width = 720;
 889                        sel->r.height = itv->is_50hz ? 576 : 480;
 890                        return 0;
 891                default:
 892                        return -EINVAL;
 893                }
 894        }
 895
 896        if (sel->type != V4L2_BUF_TYPE_VIDEO_OUTPUT ||
 897            !(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
 898                return -EINVAL;
 899
 900        switch (sel->target) {
 901        case V4L2_SEL_TGT_COMPOSE:
 902                if (streamtype == IVTV_DEC_STREAM_TYPE_YUV)
 903                        sel->r = yi->main_rect;
 904                else
 905                        sel->r = itv->main_rect;
 906                return 0;
 907        case V4L2_SEL_TGT_COMPOSE_DEFAULT:
 908        case V4L2_SEL_TGT_COMPOSE_BOUNDS:
 909                r.height = itv->is_out_50hz ? 576 : 480;
 910                if (streamtype == IVTV_DEC_STREAM_TYPE_YUV && yi->track_osd) {
 911                        r.width = yi->osd_full_w;
 912                        r.height = yi->osd_full_h;
 913                }
 914                sel->r = r;
 915                return 0;
 916        }
 917        return -EINVAL;
 918}
 919
 920static int ivtv_enum_fmt_vid_cap(struct file *file, void *fh, struct v4l2_fmtdesc *fmt)
 921{
 922        static const struct v4l2_fmtdesc hm12 = {
 923                .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
 924                .description = "HM12 (YUV 4:2:0)",
 925                .pixelformat = V4L2_PIX_FMT_HM12,
 926        };
 927        static const struct v4l2_fmtdesc mpeg = {
 928                .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
 929                .flags = V4L2_FMT_FLAG_COMPRESSED,
 930                .description = "MPEG",
 931                .pixelformat = V4L2_PIX_FMT_MPEG,
 932        };
 933        struct ivtv *itv = fh2id(fh)->itv;
 934        struct ivtv_stream *s = &itv->streams[fh2id(fh)->type];
 935
 936        if (fmt->index)
 937                return -EINVAL;
 938        if (s->type == IVTV_ENC_STREAM_TYPE_MPG)
 939                *fmt = mpeg;
 940        else if (s->type == IVTV_ENC_STREAM_TYPE_YUV)
 941                *fmt = hm12;
 942        else
 943                return -EINVAL;
 944        return 0;
 945}
 946
 947static int ivtv_enum_fmt_vid_out(struct file *file, void *fh, struct v4l2_fmtdesc *fmt)
 948{
 949        static const struct v4l2_fmtdesc hm12 = {
 950                .type = V4L2_BUF_TYPE_VIDEO_OUTPUT,
 951                .description = "HM12 (YUV 4:2:0)",
 952                .pixelformat = V4L2_PIX_FMT_HM12,
 953        };
 954        static const struct v4l2_fmtdesc mpeg = {
 955                .type = V4L2_BUF_TYPE_VIDEO_OUTPUT,
 956                .flags = V4L2_FMT_FLAG_COMPRESSED,
 957                .description = "MPEG",
 958                .pixelformat = V4L2_PIX_FMT_MPEG,
 959        };
 960        struct ivtv *itv = fh2id(fh)->itv;
 961        struct ivtv_stream *s = &itv->streams[fh2id(fh)->type];
 962
 963        if (fmt->index)
 964                return -EINVAL;
 965        if (s->type == IVTV_DEC_STREAM_TYPE_MPG)
 966                *fmt = mpeg;
 967        else if (s->type == IVTV_DEC_STREAM_TYPE_YUV)
 968                *fmt = hm12;
 969        else
 970                return -EINVAL;
 971        return 0;
 972}
 973
 974static int ivtv_g_input(struct file *file, void *fh, unsigned int *i)
 975{
 976        struct ivtv *itv = fh2id(fh)->itv;
 977
 978        *i = itv->active_input;
 979
 980        return 0;
 981}
 982
 983int ivtv_s_input(struct file *file, void *fh, unsigned int inp)
 984{
 985        struct ivtv *itv = fh2id(fh)->itv;
 986        v4l2_std_id std;
 987        int i;
 988
 989        if (inp >= itv->nof_inputs)
 990                return -EINVAL;
 991
 992        if (inp == itv->active_input) {
 993                IVTV_DEBUG_INFO("Input unchanged\n");
 994                return 0;
 995        }
 996
 997        if (atomic_read(&itv->capturing) > 0) {
 998                return -EBUSY;
 999        }
1000
1001        IVTV_DEBUG_INFO("Changing input from %d to %d\n",
1002                        itv->active_input, inp);
1003
1004        itv->active_input = inp;
1005        /* Set the audio input to whatever is appropriate for the
1006           input type. */
1007        itv->audio_input = itv->card->video_inputs[inp].audio_index;
1008
1009        if (itv->card->video_inputs[inp].video_type == IVTV_CARD_INPUT_VID_TUNER)
1010                std = itv->tuner_std;
1011        else
1012                std = V4L2_STD_ALL;
1013        for (i = 0; i <= IVTV_ENC_STREAM_TYPE_VBI; i++)
1014                itv->streams[i].vdev.tvnorms = std;
1015
1016        /* prevent others from messing with the streams until
1017           we're finished changing inputs. */
1018        ivtv_mute(itv);
1019        ivtv_video_set_io(itv);
1020        ivtv_audio_set_io(itv);
1021        ivtv_unmute(itv);
1022
1023        return 0;
1024}
1025
1026static int ivtv_g_output(struct file *file, void *fh, unsigned int *i)
1027{
1028        struct ivtv *itv = fh2id(fh)->itv;
1029
1030        if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
1031                return -EINVAL;
1032
1033        *i = itv->active_output;
1034
1035        return 0;
1036}
1037
1038static int ivtv_s_output(struct file *file, void *fh, unsigned int outp)
1039{
1040        struct ivtv *itv = fh2id(fh)->itv;
1041
1042        if (outp >= itv->card->nof_outputs)
1043                return -EINVAL;
1044
1045        if (outp == itv->active_output) {
1046                IVTV_DEBUG_INFO("Output unchanged\n");
1047                return 0;
1048        }
1049        IVTV_DEBUG_INFO("Changing output from %d to %d\n",
1050                   itv->active_output, outp);
1051
1052        itv->active_output = outp;
1053        ivtv_call_hw(itv, IVTV_HW_SAA7127, video, s_routing,
1054                        SAA7127_INPUT_TYPE_NORMAL,
1055                        itv->card->video_outputs[outp].video_output, 0);
1056
1057        return 0;
1058}
1059
1060static int ivtv_g_frequency(struct file *file, void *fh, struct v4l2_frequency *vf)
1061{
1062        struct ivtv *itv = fh2id(fh)->itv;
1063        struct ivtv_stream *s = &itv->streams[fh2id(fh)->type];
1064
1065        if (s->vdev.vfl_dir)
1066                return -ENOTTY;
1067        if (vf->tuner != 0)
1068                return -EINVAL;
1069
1070        ivtv_call_all(itv, tuner, g_frequency, vf);
1071        return 0;
1072}
1073
1074int ivtv_s_frequency(struct file *file, void *fh, const struct v4l2_frequency *vf)
1075{
1076        struct ivtv *itv = fh2id(fh)->itv;
1077        struct ivtv_stream *s = &itv->streams[fh2id(fh)->type];
1078
1079        if (s->vdev.vfl_dir)
1080                return -ENOTTY;
1081        if (vf->tuner != 0)
1082                return -EINVAL;
1083
1084        ivtv_mute(itv);
1085        IVTV_DEBUG_INFO("v4l2 ioctl: set frequency %d\n", vf->frequency);
1086        ivtv_call_all(itv, tuner, s_frequency, vf);
1087        ivtv_unmute(itv);
1088        return 0;
1089}
1090
1091static int ivtv_g_std(struct file *file, void *fh, v4l2_std_id *std)
1092{
1093        struct ivtv *itv = fh2id(fh)->itv;
1094
1095        *std = itv->std;
1096        return 0;
1097}
1098
1099void ivtv_s_std_enc(struct ivtv *itv, v4l2_std_id std)
1100{
1101        itv->std = std;
1102        itv->is_60hz = (std & V4L2_STD_525_60) ? 1 : 0;
1103        itv->is_50hz = !itv->is_60hz;
1104        cx2341x_handler_set_50hz(&itv->cxhdl, itv->is_50hz);
1105        itv->cxhdl.width = 720;
1106        itv->cxhdl.height = itv->is_50hz ? 576 : 480;
1107        itv->vbi.count = itv->is_50hz ? 18 : 12;
1108        itv->vbi.start[0] = itv->is_50hz ? 6 : 10;
1109        itv->vbi.start[1] = itv->is_50hz ? 318 : 273;
1110
1111        if (itv->hw_flags & IVTV_HW_CX25840)
1112                itv->vbi.sliced_decoder_line_size = itv->is_60hz ? 272 : 284;
1113
1114        /* Tuner */
1115        ivtv_call_all(itv, video, s_std, itv->std);
1116}
1117
1118void ivtv_s_std_dec(struct ivtv *itv, v4l2_std_id std)
1119{
1120        struct yuv_playback_info *yi = &itv->yuv_info;
1121        DEFINE_WAIT(wait);
1122        int f;
1123
1124        /* set display standard */
1125        itv->std_out = std;
1126        itv->is_out_60hz = (std & V4L2_STD_525_60) ? 1 : 0;
1127        itv->is_out_50hz = !itv->is_out_60hz;
1128        ivtv_call_all(itv, video, s_std_output, itv->std_out);
1129
1130        /*
1131         * The next firmware call is time sensitive. Time it to
1132         * avoid risk of a hard lock, by trying to ensure the call
1133         * happens within the first 100 lines of the top field.
1134         * Make 4 attempts to sync to the decoder before giving up.
1135         */
1136        mutex_unlock(&itv->serialize_lock);
1137        for (f = 0; f < 4; f++) {
1138                prepare_to_wait(&itv->vsync_waitq, &wait,
1139                                TASK_UNINTERRUPTIBLE);
1140                if ((read_reg(IVTV_REG_DEC_LINE_FIELD) >> 16) < 100)
1141                        break;
1142                schedule_timeout(msecs_to_jiffies(25));
1143        }
1144        finish_wait(&itv->vsync_waitq, &wait);
1145        mutex_lock(&itv->serialize_lock);
1146
1147        if (f == 4)
1148                IVTV_WARN("Mode change failed to sync to decoder\n");
1149
1150        ivtv_vapi(itv, CX2341X_DEC_SET_STANDARD, 1, itv->is_out_50hz);
1151        itv->main_rect.left = 0;
1152        itv->main_rect.top = 0;
1153        itv->main_rect.width = 720;
1154        itv->main_rect.height = itv->is_out_50hz ? 576 : 480;
1155        ivtv_vapi(itv, CX2341X_OSD_SET_FRAMEBUFFER_WINDOW, 4,
1156                720, itv->main_rect.height, 0, 0);
1157        yi->main_rect = itv->main_rect;
1158        if (!itv->osd_info) {
1159                yi->osd_full_w = 720;
1160                yi->osd_full_h = itv->is_out_50hz ? 576 : 480;
1161        }
1162}
1163
1164static int ivtv_s_std(struct file *file, void *fh, v4l2_std_id std)
1165{
1166        struct ivtv *itv = fh2id(fh)->itv;
1167
1168        if ((std & V4L2_STD_ALL) == 0)
1169                return -EINVAL;
1170
1171        if (std == itv->std)
1172                return 0;
1173
1174        if (test_bit(IVTV_F_I_RADIO_USER, &itv->i_flags) ||
1175            atomic_read(&itv->capturing) > 0 ||
1176            atomic_read(&itv->decoding) > 0) {
1177                /* Switching standard would mess with already running
1178                   streams, prevent that by returning EBUSY. */
1179                return -EBUSY;
1180        }
1181
1182        IVTV_DEBUG_INFO("Switching standard to %llx.\n",
1183                (unsigned long long)itv->std);
1184
1185        ivtv_s_std_enc(itv, std);
1186        if (itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT)
1187                ivtv_s_std_dec(itv, std);
1188
1189        return 0;
1190}
1191
1192static int ivtv_s_tuner(struct file *file, void *fh, const struct v4l2_tuner *vt)
1193{
1194        struct ivtv_open_id *id = fh2id(fh);
1195        struct ivtv *itv = id->itv;
1196
1197        if (vt->index != 0)
1198                return -EINVAL;
1199
1200        ivtv_call_all(itv, tuner, s_tuner, vt);
1201
1202        return 0;
1203}
1204
1205static int ivtv_g_tuner(struct file *file, void *fh, struct v4l2_tuner *vt)
1206{
1207        struct ivtv *itv = fh2id(fh)->itv;
1208
1209        if (vt->index != 0)
1210                return -EINVAL;
1211
1212        ivtv_call_all(itv, tuner, g_tuner, vt);
1213
1214        if (vt->type == V4L2_TUNER_RADIO)
1215                strscpy(vt->name, "ivtv Radio Tuner", sizeof(vt->name));
1216        else
1217                strscpy(vt->name, "ivtv TV Tuner", sizeof(vt->name));
1218        return 0;
1219}
1220
1221static int ivtv_g_sliced_vbi_cap(struct file *file, void *fh, struct v4l2_sliced_vbi_cap *cap)
1222{
1223        struct ivtv *itv = fh2id(fh)->itv;
1224        int set = itv->is_50hz ? V4L2_SLICED_VBI_625 : V4L2_SLICED_VBI_525;
1225        int f, l;
1226
1227        if (cap->type == V4L2_BUF_TYPE_SLICED_VBI_CAPTURE) {
1228                for (f = 0; f < 2; f++) {
1229                        for (l = 0; l < 24; l++) {
1230                                if (valid_service_line(f, l, itv->is_50hz))
1231                                        cap->service_lines[f][l] = set;
1232                        }
1233                }
1234        } else if (cap->type == V4L2_BUF_TYPE_SLICED_VBI_OUTPUT) {
1235                if (!(itv->v4l2_cap & V4L2_CAP_SLICED_VBI_OUTPUT))
1236                        return -EINVAL;
1237                if (itv->is_60hz) {
1238                        cap->service_lines[0][21] = V4L2_SLICED_CAPTION_525;
1239                        cap->service_lines[1][21] = V4L2_SLICED_CAPTION_525;
1240                } else {
1241                        cap->service_lines[0][23] = V4L2_SLICED_WSS_625;
1242                        cap->service_lines[0][16] = V4L2_SLICED_VPS;
1243                }
1244        } else {
1245                return -EINVAL;
1246        }
1247
1248        set = 0;
1249        for (f = 0; f < 2; f++)
1250                for (l = 0; l < 24; l++)
1251                        set |= cap->service_lines[f][l];
1252        cap->service_set = set;
1253        return 0;
1254}
1255
1256static int ivtv_g_enc_index(struct file *file, void *fh, struct v4l2_enc_idx *idx)
1257{
1258        struct ivtv *itv = fh2id(fh)->itv;
1259        struct v4l2_enc_idx_entry *e = idx->entry;
1260        int entries;
1261        int i;
1262
1263        entries = (itv->pgm_info_write_idx + IVTV_MAX_PGM_INDEX - itv->pgm_info_read_idx) %
1264                                IVTV_MAX_PGM_INDEX;
1265        if (entries > V4L2_ENC_IDX_ENTRIES)
1266                entries = V4L2_ENC_IDX_ENTRIES;
1267        idx->entries = 0;
1268        idx->entries_cap = IVTV_MAX_PGM_INDEX;
1269        if (!atomic_read(&itv->capturing))
1270                return 0;
1271        for (i = 0; i < entries; i++) {
1272                *e = itv->pgm_info[(itv->pgm_info_read_idx + i) % IVTV_MAX_PGM_INDEX];
1273                if ((e->flags & V4L2_ENC_IDX_FRAME_MASK) <= V4L2_ENC_IDX_FRAME_B) {
1274                        idx->entries++;
1275                        e++;
1276                }
1277        }
1278        itv->pgm_info_read_idx = (itv->pgm_info_read_idx + idx->entries) % IVTV_MAX_PGM_INDEX;
1279        return 0;
1280}
1281
1282static int ivtv_encoder_cmd(struct file *file, void *fh, struct v4l2_encoder_cmd *enc)
1283{
1284        struct ivtv_open_id *id = fh2id(fh);
1285        struct ivtv *itv = id->itv;
1286
1287
1288        switch (enc->cmd) {
1289        case V4L2_ENC_CMD_START:
1290                IVTV_DEBUG_IOCTL("V4L2_ENC_CMD_START\n");
1291                enc->flags = 0;
1292                return ivtv_start_capture(id);
1293
1294        case V4L2_ENC_CMD_STOP:
1295                IVTV_DEBUG_IOCTL("V4L2_ENC_CMD_STOP\n");
1296                enc->flags &= V4L2_ENC_CMD_STOP_AT_GOP_END;
1297                ivtv_stop_capture(id, enc->flags & V4L2_ENC_CMD_STOP_AT_GOP_END);
1298                return 0;
1299
1300        case V4L2_ENC_CMD_PAUSE:
1301                IVTV_DEBUG_IOCTL("V4L2_ENC_CMD_PAUSE\n");
1302                enc->flags = 0;
1303
1304                if (!atomic_read(&itv->capturing))
1305                        return -EPERM;
1306                if (test_and_set_bit(IVTV_F_I_ENC_PAUSED, &itv->i_flags))
1307                        return 0;
1308
1309                ivtv_mute(itv);
1310                ivtv_vapi(itv, CX2341X_ENC_PAUSE_ENCODER, 1, 0);
1311                break;
1312
1313        case V4L2_ENC_CMD_RESUME:
1314                IVTV_DEBUG_IOCTL("V4L2_ENC_CMD_RESUME\n");
1315                enc->flags = 0;
1316
1317                if (!atomic_read(&itv->capturing))
1318                        return -EPERM;
1319
1320                if (!test_and_clear_bit(IVTV_F_I_ENC_PAUSED, &itv->i_flags))
1321                        return 0;
1322
1323                ivtv_vapi(itv, CX2341X_ENC_PAUSE_ENCODER, 1, 1);
1324                ivtv_unmute(itv);
1325                break;
1326        default:
1327                IVTV_DEBUG_IOCTL("Unknown cmd %d\n", enc->cmd);
1328                return -EINVAL;
1329        }
1330
1331        return 0;
1332}
1333
1334static int ivtv_try_encoder_cmd(struct file *file, void *fh, struct v4l2_encoder_cmd *enc)
1335{
1336        struct ivtv *itv = fh2id(fh)->itv;
1337
1338        switch (enc->cmd) {
1339        case V4L2_ENC_CMD_START:
1340                IVTV_DEBUG_IOCTL("V4L2_ENC_CMD_START\n");
1341                enc->flags = 0;
1342                return 0;
1343
1344        case V4L2_ENC_CMD_STOP:
1345                IVTV_DEBUG_IOCTL("V4L2_ENC_CMD_STOP\n");
1346                enc->flags &= V4L2_ENC_CMD_STOP_AT_GOP_END;
1347                return 0;
1348
1349        case V4L2_ENC_CMD_PAUSE:
1350                IVTV_DEBUG_IOCTL("V4L2_ENC_CMD_PAUSE\n");
1351                enc->flags = 0;
1352                return 0;
1353
1354        case V4L2_ENC_CMD_RESUME:
1355                IVTV_DEBUG_IOCTL("V4L2_ENC_CMD_RESUME\n");
1356                enc->flags = 0;
1357                return 0;
1358        default:
1359                IVTV_DEBUG_IOCTL("Unknown cmd %d\n", enc->cmd);
1360                return -EINVAL;
1361        }
1362}
1363
1364static int ivtv_g_fbuf(struct file *file, void *fh, struct v4l2_framebuffer *fb)
1365{
1366        struct ivtv *itv = fh2id(fh)->itv;
1367        struct ivtv_stream *s = &itv->streams[fh2id(fh)->type];
1368        u32 data[CX2341X_MBOX_MAX_DATA];
1369        struct yuv_playback_info *yi = &itv->yuv_info;
1370
1371        int pixfmt;
1372        static u32 pixel_format[16] = {
1373                V4L2_PIX_FMT_PAL8, /* Uses a 256-entry RGB colormap */
1374                V4L2_PIX_FMT_RGB565,
1375                V4L2_PIX_FMT_RGB555,
1376                V4L2_PIX_FMT_RGB444,
1377                V4L2_PIX_FMT_RGB32,
1378                0,
1379                0,
1380                0,
1381                V4L2_PIX_FMT_PAL8, /* Uses a 256-entry YUV colormap */
1382                V4L2_PIX_FMT_YUV565,
1383                V4L2_PIX_FMT_YUV555,
1384                V4L2_PIX_FMT_YUV444,
1385                V4L2_PIX_FMT_YUV32,
1386                0,
1387                0,
1388                0,
1389        };
1390
1391        if (!(s->caps & V4L2_CAP_VIDEO_OUTPUT_OVERLAY))
1392                return -ENOTTY;
1393        if (!itv->osd_video_pbase)
1394                return -ENOTTY;
1395
1396        fb->capability = V4L2_FBUF_CAP_EXTERNOVERLAY | V4L2_FBUF_CAP_CHROMAKEY |
1397                V4L2_FBUF_CAP_GLOBAL_ALPHA;
1398
1399        ivtv_vapi_result(itv, data, CX2341X_OSD_GET_STATE, 0);
1400        data[0] |= (read_reg(0x2a00) >> 7) & 0x40;
1401        pixfmt = (data[0] >> 3) & 0xf;
1402
1403        fb->fmt.pixelformat = pixel_format[pixfmt];
1404        fb->fmt.width = itv->osd_rect.width;
1405        fb->fmt.height = itv->osd_rect.height;
1406        fb->fmt.field = V4L2_FIELD_INTERLACED;
1407        fb->fmt.bytesperline = fb->fmt.width;
1408        fb->fmt.colorspace = V4L2_COLORSPACE_SMPTE170M;
1409        fb->fmt.field = V4L2_FIELD_INTERLACED;
1410        if (fb->fmt.pixelformat != V4L2_PIX_FMT_PAL8)
1411                fb->fmt.bytesperline *= 2;
1412        if (fb->fmt.pixelformat == V4L2_PIX_FMT_RGB32 ||
1413            fb->fmt.pixelformat == V4L2_PIX_FMT_YUV32)
1414                fb->fmt.bytesperline *= 2;
1415        fb->fmt.sizeimage = fb->fmt.bytesperline * fb->fmt.height;
1416        fb->base = (void *)itv->osd_video_pbase;
1417        fb->flags = 0;
1418
1419        if (itv->osd_chroma_key_state)
1420                fb->flags |= V4L2_FBUF_FLAG_CHROMAKEY;
1421
1422        if (itv->osd_global_alpha_state)
1423                fb->flags |= V4L2_FBUF_FLAG_GLOBAL_ALPHA;
1424
1425        if (yi->track_osd)
1426                fb->flags |= V4L2_FBUF_FLAG_OVERLAY;
1427
1428        pixfmt &= 7;
1429
1430        /* no local alpha for RGB565 or unknown formats */
1431        if (pixfmt == 1 || pixfmt > 4)
1432                return 0;
1433
1434        /* 16-bit formats have inverted local alpha */
1435        if (pixfmt == 2 || pixfmt == 3)
1436                fb->capability |= V4L2_FBUF_CAP_LOCAL_INV_ALPHA;
1437        else
1438                fb->capability |= V4L2_FBUF_CAP_LOCAL_ALPHA;
1439
1440        if (itv->osd_local_alpha_state) {
1441                /* 16-bit formats have inverted local alpha */
1442                if (pixfmt == 2 || pixfmt == 3)
1443                        fb->flags |= V4L2_FBUF_FLAG_LOCAL_INV_ALPHA;
1444                else
1445                        fb->flags |= V4L2_FBUF_FLAG_LOCAL_ALPHA;
1446        }
1447
1448        return 0;
1449}
1450
1451static int ivtv_s_fbuf(struct file *file, void *fh, const struct v4l2_framebuffer *fb)
1452{
1453        struct ivtv_open_id *id = fh2id(fh);
1454        struct ivtv *itv = id->itv;
1455        struct ivtv_stream *s = &itv->streams[fh2id(fh)->type];
1456        struct yuv_playback_info *yi = &itv->yuv_info;
1457
1458        if (!(s->caps & V4L2_CAP_VIDEO_OUTPUT_OVERLAY))
1459                return -ENOTTY;
1460        if (!itv->osd_video_pbase)
1461                return -ENOTTY;
1462
1463        itv->osd_global_alpha_state = (fb->flags & V4L2_FBUF_FLAG_GLOBAL_ALPHA) != 0;
1464        itv->osd_local_alpha_state =
1465                (fb->flags & (V4L2_FBUF_FLAG_LOCAL_ALPHA|V4L2_FBUF_FLAG_LOCAL_INV_ALPHA)) != 0;
1466        itv->osd_chroma_key_state = (fb->flags & V4L2_FBUF_FLAG_CHROMAKEY) != 0;
1467        ivtv_set_osd_alpha(itv);
1468        yi->track_osd = (fb->flags & V4L2_FBUF_FLAG_OVERLAY) != 0;
1469        return 0;
1470}
1471
1472static int ivtv_overlay(struct file *file, void *fh, unsigned int on)
1473{
1474        struct ivtv_open_id *id = fh2id(fh);
1475        struct ivtv *itv = id->itv;
1476        struct ivtv_stream *s = &itv->streams[fh2id(fh)->type];
1477
1478        if (!(s->caps & V4L2_CAP_VIDEO_OUTPUT_OVERLAY))
1479                return -ENOTTY;
1480        if (!itv->osd_video_pbase)
1481                return -ENOTTY;
1482
1483        ivtv_vapi(itv, CX2341X_OSD_SET_STATE, 1, on != 0);
1484
1485        return 0;
1486}
1487
1488static int ivtv_subscribe_event(struct v4l2_fh *fh, const struct v4l2_event_subscription *sub)
1489{
1490        switch (sub->type) {
1491        case V4L2_EVENT_VSYNC:
1492        case V4L2_EVENT_EOS:
1493                return v4l2_event_subscribe(fh, sub, 0, NULL);
1494        default:
1495                return v4l2_ctrl_subscribe_event(fh, sub);
1496        }
1497}
1498
1499static int ivtv_log_status(struct file *file, void *fh)
1500{
1501        struct ivtv *itv = fh2id(fh)->itv;
1502        u32 data[CX2341X_MBOX_MAX_DATA];
1503
1504        int has_output = itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT;
1505        struct v4l2_input vidin;
1506        struct v4l2_audio audin;
1507        int i;
1508
1509        IVTV_INFO("Version: %s Card: %s\n", IVTV_VERSION, itv->card_name);
1510        if (itv->hw_flags & IVTV_HW_TVEEPROM) {
1511                struct tveeprom tv;
1512
1513                ivtv_read_eeprom(itv, &tv);
1514        }
1515        ivtv_call_all(itv, core, log_status);
1516        ivtv_get_input(itv, itv->active_input, &vidin);
1517        ivtv_get_audio_input(itv, itv->audio_input, &audin);
1518        IVTV_INFO("Video Input:  %s\n", vidin.name);
1519        IVTV_INFO("Audio Input:  %s%s\n", audin.name,
1520                itv->dualwatch_stereo_mode == V4L2_MPEG_AUDIO_MODE_DUAL ?
1521                        " (Bilingual)" : "");
1522        if (has_output) {
1523                struct v4l2_output vidout;
1524                struct v4l2_audioout audout;
1525                int mode = itv->output_mode;
1526                static const char * const output_modes[5] = {
1527                        "None",
1528                        "MPEG Streaming",
1529                        "YUV Streaming",
1530                        "YUV Frames",
1531                        "Passthrough",
1532                };
1533                static const char * const alpha_mode[4] = {
1534                        "None",
1535                        "Global",
1536                        "Local",
1537                        "Global and Local"
1538                };
1539                static const char * const pixel_format[16] = {
1540                        "ARGB Indexed",
1541                        "RGB 5:6:5",
1542                        "ARGB 1:5:5:5",
1543                        "ARGB 1:4:4:4",
1544                        "ARGB 8:8:8:8",
1545                        "5",
1546                        "6",
1547                        "7",
1548                        "AYUV Indexed",
1549                        "YUV 5:6:5",
1550                        "AYUV 1:5:5:5",
1551                        "AYUV 1:4:4:4",
1552                        "AYUV 8:8:8:8",
1553                        "13",
1554                        "14",
1555                        "15",
1556                };
1557
1558                ivtv_get_output(itv, itv->active_output, &vidout);
1559                ivtv_get_audio_output(itv, 0, &audout);
1560                IVTV_INFO("Video Output: %s\n", vidout.name);
1561                if (mode < 0 || mode > OUT_PASSTHROUGH)
1562                        mode = OUT_NONE;
1563                IVTV_INFO("Output Mode:  %s\n", output_modes[mode]);
1564                ivtv_vapi_result(itv, data, CX2341X_OSD_GET_STATE, 0);
1565                data[0] |= (read_reg(0x2a00) >> 7) & 0x40;
1566                IVTV_INFO("Overlay:      %s, Alpha: %s, Pixel Format: %s\n",
1567                        data[0] & 1 ? "On" : "Off",
1568                        alpha_mode[(data[0] >> 1) & 0x3],
1569                        pixel_format[(data[0] >> 3) & 0xf]);
1570        }
1571        IVTV_INFO("Tuner:  %s\n",
1572                test_bit(IVTV_F_I_RADIO_USER, &itv->i_flags) ? "Radio" : "TV");
1573        v4l2_ctrl_handler_log_status(&itv->cxhdl.hdl, itv->v4l2_dev.name);
1574        IVTV_INFO("Status flags:    0x%08lx\n", itv->i_flags);
1575        for (i = 0; i < IVTV_MAX_STREAMS; i++) {
1576                struct ivtv_stream *s = &itv->streams[i];
1577
1578                if (s->vdev.v4l2_dev == NULL || s->buffers == 0)
1579                        continue;
1580                IVTV_INFO("Stream %s: status 0x%04lx, %d%% of %d KiB (%d buffers) in use\n", s->name, s->s_flags,
1581                                (s->buffers - s->q_free.buffers) * 100 / s->buffers,
1582                                (s->buffers * s->buf_size) / 1024, s->buffers);
1583        }
1584
1585        IVTV_INFO("Read MPG/VBI: %lld/%lld bytes\n",
1586                        (long long)itv->mpg_data_received,
1587                        (long long)itv->vbi_data_inserted);
1588        return 0;
1589}
1590
1591static int ivtv_decoder_cmd(struct file *file, void *fh, struct v4l2_decoder_cmd *dec)
1592{
1593        struct ivtv_open_id *id = fh2id(file->private_data);
1594        struct ivtv *itv = id->itv;
1595
1596        IVTV_DEBUG_IOCTL("VIDIOC_DECODER_CMD %d\n", dec->cmd);
1597        return ivtv_video_command(itv, id, dec, false);
1598}
1599
1600static int ivtv_try_decoder_cmd(struct file *file, void *fh, struct v4l2_decoder_cmd *dec)
1601{
1602        struct ivtv_open_id *id = fh2id(file->private_data);
1603        struct ivtv *itv = id->itv;
1604
1605        IVTV_DEBUG_IOCTL("VIDIOC_TRY_DECODER_CMD %d\n", dec->cmd);
1606        return ivtv_video_command(itv, id, dec, true);
1607}
1608
1609#ifdef CONFIG_VIDEO_IVTV_DEPRECATED_IOCTLS
1610static __inline__ void warn_deprecated_ioctl(const char *name)
1611{
1612        pr_warn_once("warning: the %s ioctl is deprecated. Don't use it, as it will be removed soon\n",
1613                     name);
1614}
1615
1616#ifdef CONFIG_COMPAT
1617struct compat_video_event {
1618        __s32 type;
1619        /* unused, make sure to use atomic time for y2038 if it ever gets used */
1620        compat_long_t timestamp;
1621        union {
1622                video_size_t size;
1623                unsigned int frame_rate;        /* in frames per 1000sec */
1624                unsigned char vsync_field;      /* unknown/odd/even/progressive */
1625        } u;
1626};
1627#define VIDEO_GET_EVENT32 _IOR('o', 28, struct compat_video_event)
1628#endif
1629
1630#endif
1631
1632static int ivtv_decoder_ioctls(struct file *filp, unsigned int cmd, void *arg)
1633{
1634        struct ivtv_open_id *id = fh2id(filp->private_data);
1635        struct ivtv *itv = id->itv;
1636        struct ivtv_stream *s = &itv->streams[id->type];
1637#ifdef CONFIG_VIDEO_IVTV_DEPRECATED_IOCTLS
1638        int nonblocking = filp->f_flags & O_NONBLOCK;
1639        unsigned long iarg = (unsigned long)arg;
1640#endif
1641
1642        switch (cmd) {
1643        case IVTV_IOC_DMA_FRAME: {
1644                struct ivtv_dma_frame *args = arg;
1645
1646                IVTV_DEBUG_IOCTL("IVTV_IOC_DMA_FRAME\n");
1647                if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
1648                        return -EINVAL;
1649                if (args->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
1650                        return -EINVAL;
1651                if (itv->output_mode == OUT_UDMA_YUV && args->y_source == NULL)
1652                        return 0;
1653                if (ivtv_start_decoding(id, id->type)) {
1654                        return -EBUSY;
1655                }
1656                if (ivtv_set_output_mode(itv, OUT_UDMA_YUV) != OUT_UDMA_YUV) {
1657                        ivtv_release_stream(s);
1658                        return -EBUSY;
1659                }
1660                /* Mark that this file handle started the UDMA_YUV mode */
1661                id->yuv_frames = 1;
1662                if (args->y_source == NULL)
1663                        return 0;
1664                return ivtv_yuv_prep_frame(itv, args);
1665        }
1666
1667        case IVTV_IOC_PASSTHROUGH_MODE:
1668                IVTV_DEBUG_IOCTL("IVTV_IOC_PASSTHROUGH_MODE\n");
1669                if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
1670                        return -EINVAL;
1671                return ivtv_passthrough_mode(itv, *(int *)arg != 0);
1672#ifdef CONFIG_VIDEO_IVTV_DEPRECATED_IOCTLS
1673        case VIDEO_GET_PTS: {
1674                s64 *pts = arg;
1675                s64 frame;
1676
1677                warn_deprecated_ioctl("VIDEO_GET_PTS");
1678                if (s->type < IVTV_DEC_STREAM_TYPE_MPG) {
1679                        *pts = s->dma_pts;
1680                        break;
1681                }
1682                if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
1683                        return -EINVAL;
1684                return ivtv_g_pts_frame(itv, pts, &frame);
1685        }
1686
1687        case VIDEO_GET_FRAME_COUNT: {
1688                s64 *frame = arg;
1689                s64 pts;
1690
1691                warn_deprecated_ioctl("VIDEO_GET_FRAME_COUNT");
1692                if (s->type < IVTV_DEC_STREAM_TYPE_MPG) {
1693                        *frame = 0;
1694                        break;
1695                }
1696                if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
1697                        return -EINVAL;
1698                return ivtv_g_pts_frame(itv, &pts, frame);
1699        }
1700
1701        case VIDEO_PLAY: {
1702                struct v4l2_decoder_cmd dc;
1703
1704                warn_deprecated_ioctl("VIDEO_PLAY");
1705                memset(&dc, 0, sizeof(dc));
1706                dc.cmd = V4L2_DEC_CMD_START;
1707                return ivtv_video_command(itv, id, &dc, 0);
1708        }
1709
1710        case VIDEO_STOP: {
1711                struct v4l2_decoder_cmd dc;
1712
1713                warn_deprecated_ioctl("VIDEO_STOP");
1714                memset(&dc, 0, sizeof(dc));
1715                dc.cmd = V4L2_DEC_CMD_STOP;
1716                dc.flags = V4L2_DEC_CMD_STOP_TO_BLACK | V4L2_DEC_CMD_STOP_IMMEDIATELY;
1717                return ivtv_video_command(itv, id, &dc, 0);
1718        }
1719
1720        case VIDEO_FREEZE: {
1721                struct v4l2_decoder_cmd dc;
1722
1723                warn_deprecated_ioctl("VIDEO_FREEZE");
1724                memset(&dc, 0, sizeof(dc));
1725                dc.cmd = V4L2_DEC_CMD_PAUSE;
1726                return ivtv_video_command(itv, id, &dc, 0);
1727        }
1728
1729        case VIDEO_CONTINUE: {
1730                struct v4l2_decoder_cmd dc;
1731
1732                warn_deprecated_ioctl("VIDEO_CONTINUE");
1733                memset(&dc, 0, sizeof(dc));
1734                dc.cmd = V4L2_DEC_CMD_RESUME;
1735                return ivtv_video_command(itv, id, &dc, 0);
1736        }
1737
1738        case VIDEO_COMMAND:
1739        case VIDEO_TRY_COMMAND: {
1740                /* Note: struct v4l2_decoder_cmd has the same layout as
1741                   struct video_command */
1742                struct v4l2_decoder_cmd *dc = arg;
1743                int try = (cmd == VIDEO_TRY_COMMAND);
1744
1745                if (try)
1746                        warn_deprecated_ioctl("VIDEO_TRY_COMMAND");
1747                else
1748                        warn_deprecated_ioctl("VIDEO_COMMAND");
1749                return ivtv_video_command(itv, id, dc, try);
1750        }
1751
1752#ifdef CONFIG_COMPAT
1753        case VIDEO_GET_EVENT32:
1754#endif
1755        case VIDEO_GET_EVENT: {
1756#ifdef CONFIG_COMPAT
1757                struct compat_video_event *ev32 = arg;
1758#endif
1759                struct video_event *ev = arg;
1760                DEFINE_WAIT(wait);
1761
1762                warn_deprecated_ioctl("VIDEO_GET_EVENT");
1763                if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
1764                        return -EINVAL;
1765                memset(ev, 0, sizeof(*ev));
1766                set_bit(IVTV_F_I_EV_VSYNC_ENABLED, &itv->i_flags);
1767
1768                while (1) {
1769                        if (test_and_clear_bit(IVTV_F_I_EV_DEC_STOPPED, &itv->i_flags))
1770                                ev->type = VIDEO_EVENT_DECODER_STOPPED;
1771                        else if (test_and_clear_bit(IVTV_F_I_EV_VSYNC, &itv->i_flags)) {
1772                                unsigned char vsync_field;
1773
1774                                ev->type = VIDEO_EVENT_VSYNC;
1775                                vsync_field = test_bit(IVTV_F_I_EV_VSYNC_FIELD, &itv->i_flags) ?
1776                                        VIDEO_VSYNC_FIELD_ODD : VIDEO_VSYNC_FIELD_EVEN;
1777                                if (itv->output_mode == OUT_UDMA_YUV &&
1778                                        (itv->yuv_info.lace_mode & IVTV_YUV_MODE_MASK) ==
1779                                                                IVTV_YUV_MODE_PROGRESSIVE) {
1780                                        vsync_field = VIDEO_VSYNC_FIELD_PROGRESSIVE;
1781                                }
1782#ifdef CONFIG_COMPAT
1783                                if (cmd == VIDEO_GET_EVENT32)
1784                                        ev32->u.vsync_field = vsync_field;
1785                                else
1786#endif
1787                                        ev->u.vsync_field = vsync_field;
1788                        }
1789                        if (ev->type)
1790                                return 0;
1791                        if (nonblocking)
1792                                return -EAGAIN;
1793                        /* Wait for event. Note that serialize_lock is locked,
1794                           so to allow other processes to access the driver while
1795                           we are waiting unlock first and later lock again. */
1796                        mutex_unlock(&itv->serialize_lock);
1797                        prepare_to_wait(&itv->event_waitq, &wait, TASK_INTERRUPTIBLE);
1798                        if (!test_bit(IVTV_F_I_EV_DEC_STOPPED, &itv->i_flags) &&
1799                            !test_bit(IVTV_F_I_EV_VSYNC, &itv->i_flags))
1800                                schedule();
1801                        finish_wait(&itv->event_waitq, &wait);
1802                        mutex_lock(&itv->serialize_lock);
1803                        if (signal_pending(current)) {
1804                                /* return if a signal was received */
1805                                IVTV_DEBUG_INFO("User stopped wait for event\n");
1806                                return -EINTR;
1807                        }
1808                }
1809                break;
1810        }
1811
1812        case VIDEO_SELECT_SOURCE:
1813                warn_deprecated_ioctl("VIDEO_SELECT_SOURCE");
1814                if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
1815                        return -EINVAL;
1816                return ivtv_passthrough_mode(itv, iarg == VIDEO_SOURCE_DEMUX);
1817
1818        case AUDIO_SET_MUTE:
1819                warn_deprecated_ioctl("AUDIO_SET_MUTE");
1820                itv->speed_mute_audio = iarg;
1821                return 0;
1822
1823        case AUDIO_CHANNEL_SELECT:
1824                warn_deprecated_ioctl("AUDIO_CHANNEL_SELECT");
1825                if (iarg > AUDIO_STEREO_SWAPPED)
1826                        return -EINVAL;
1827                return v4l2_ctrl_s_ctrl(itv->ctrl_audio_playback, iarg + 1);
1828
1829        case AUDIO_BILINGUAL_CHANNEL_SELECT:
1830                warn_deprecated_ioctl("AUDIO_BILINGUAL_CHANNEL_SELECT");
1831                if (iarg > AUDIO_STEREO_SWAPPED)
1832                        return -EINVAL;
1833                return v4l2_ctrl_s_ctrl(itv->ctrl_audio_multilingual_playback, iarg + 1);
1834#endif
1835        default:
1836                return -EINVAL;
1837        }
1838        return 0;
1839}
1840
1841static long ivtv_default(struct file *file, void *fh, bool valid_prio,
1842                         unsigned int cmd, void *arg)
1843{
1844        struct ivtv *itv = fh2id(fh)->itv;
1845
1846        if (!valid_prio) {
1847                switch (cmd) {
1848                case IVTV_IOC_PASSTHROUGH_MODE:
1849#ifdef CONFIG_VIDEO_IVTV_DEPRECATED_IOCTLS
1850                case VIDEO_PLAY:
1851                case VIDEO_STOP:
1852                case VIDEO_FREEZE:
1853                case VIDEO_CONTINUE:
1854                case VIDEO_COMMAND:
1855                case VIDEO_SELECT_SOURCE:
1856                case AUDIO_SET_MUTE:
1857                case AUDIO_CHANNEL_SELECT:
1858                case AUDIO_BILINGUAL_CHANNEL_SELECT:
1859#endif
1860                        return -EBUSY;
1861                }
1862        }
1863
1864        switch (cmd) {
1865        case VIDIOC_INT_RESET: {
1866                u32 val = *(u32 *)arg;
1867
1868                if ((val == 0 && itv->options.newi2c) || (val & 0x01))
1869                        ivtv_reset_ir_gpio(itv);
1870                if (val & 0x02)
1871                        v4l2_subdev_call(itv->sd_video, core, reset, 0);
1872                break;
1873        }
1874
1875        case IVTV_IOC_DMA_FRAME:
1876        case IVTV_IOC_PASSTHROUGH_MODE:
1877#ifdef CONFIG_VIDEO_IVTV_DEPRECATED_IOCTLS
1878        case VIDEO_GET_PTS:
1879        case VIDEO_GET_FRAME_COUNT:
1880        case VIDEO_GET_EVENT:
1881        case VIDEO_PLAY:
1882        case VIDEO_STOP:
1883        case VIDEO_FREEZE:
1884        case VIDEO_CONTINUE:
1885        case VIDEO_COMMAND:
1886        case VIDEO_TRY_COMMAND:
1887        case VIDEO_SELECT_SOURCE:
1888        case AUDIO_SET_MUTE:
1889        case AUDIO_CHANNEL_SELECT:
1890        case AUDIO_BILINGUAL_CHANNEL_SELECT:
1891#endif
1892                return ivtv_decoder_ioctls(file, cmd, (void *)arg);
1893
1894        default:
1895                return -ENOTTY;
1896        }
1897        return 0;
1898}
1899
1900static const struct v4l2_ioctl_ops ivtv_ioctl_ops = {
1901        .vidioc_querycap                    = ivtv_querycap,
1902        .vidioc_s_audio                     = ivtv_s_audio,
1903        .vidioc_g_audio                     = ivtv_g_audio,
1904        .vidioc_enumaudio                   = ivtv_enumaudio,
1905        .vidioc_s_audout                    = ivtv_s_audout,
1906        .vidioc_g_audout                    = ivtv_g_audout,
1907        .vidioc_enum_input                  = ivtv_enum_input,
1908        .vidioc_enum_output                 = ivtv_enum_output,
1909        .vidioc_enumaudout                  = ivtv_enumaudout,
1910        .vidioc_g_pixelaspect               = ivtv_g_pixelaspect,
1911        .vidioc_s_selection                 = ivtv_s_selection,
1912        .vidioc_g_selection                 = ivtv_g_selection,
1913        .vidioc_g_input                     = ivtv_g_input,
1914        .vidioc_s_input                     = ivtv_s_input,
1915        .vidioc_g_output                    = ivtv_g_output,
1916        .vidioc_s_output                    = ivtv_s_output,
1917        .vidioc_g_frequency                 = ivtv_g_frequency,
1918        .vidioc_s_frequency                 = ivtv_s_frequency,
1919        .vidioc_s_tuner                     = ivtv_s_tuner,
1920        .vidioc_g_tuner                     = ivtv_g_tuner,
1921        .vidioc_g_enc_index                 = ivtv_g_enc_index,
1922        .vidioc_g_fbuf                      = ivtv_g_fbuf,
1923        .vidioc_s_fbuf                      = ivtv_s_fbuf,
1924        .vidioc_g_std                       = ivtv_g_std,
1925        .vidioc_s_std                       = ivtv_s_std,
1926        .vidioc_overlay                     = ivtv_overlay,
1927        .vidioc_log_status                  = ivtv_log_status,
1928        .vidioc_enum_fmt_vid_cap            = ivtv_enum_fmt_vid_cap,
1929        .vidioc_encoder_cmd                 = ivtv_encoder_cmd,
1930        .vidioc_try_encoder_cmd             = ivtv_try_encoder_cmd,
1931        .vidioc_decoder_cmd                 = ivtv_decoder_cmd,
1932        .vidioc_try_decoder_cmd             = ivtv_try_decoder_cmd,
1933        .vidioc_enum_fmt_vid_out            = ivtv_enum_fmt_vid_out,
1934        .vidioc_g_fmt_vid_cap               = ivtv_g_fmt_vid_cap,
1935        .vidioc_g_fmt_vbi_cap               = ivtv_g_fmt_vbi_cap,
1936        .vidioc_g_fmt_sliced_vbi_cap        = ivtv_g_fmt_sliced_vbi_cap,
1937        .vidioc_g_fmt_vid_out               = ivtv_g_fmt_vid_out,
1938        .vidioc_g_fmt_vid_out_overlay       = ivtv_g_fmt_vid_out_overlay,
1939        .vidioc_g_fmt_sliced_vbi_out        = ivtv_g_fmt_sliced_vbi_out,
1940        .vidioc_s_fmt_vid_cap               = ivtv_s_fmt_vid_cap,
1941        .vidioc_s_fmt_vbi_cap               = ivtv_s_fmt_vbi_cap,
1942        .vidioc_s_fmt_sliced_vbi_cap        = ivtv_s_fmt_sliced_vbi_cap,
1943        .vidioc_s_fmt_vid_out               = ivtv_s_fmt_vid_out,
1944        .vidioc_s_fmt_vid_out_overlay       = ivtv_s_fmt_vid_out_overlay,
1945        .vidioc_s_fmt_sliced_vbi_out        = ivtv_s_fmt_sliced_vbi_out,
1946        .vidioc_try_fmt_vid_cap             = ivtv_try_fmt_vid_cap,
1947        .vidioc_try_fmt_vbi_cap             = ivtv_try_fmt_vbi_cap,
1948        .vidioc_try_fmt_sliced_vbi_cap      = ivtv_try_fmt_sliced_vbi_cap,
1949        .vidioc_try_fmt_vid_out             = ivtv_try_fmt_vid_out,
1950        .vidioc_try_fmt_vid_out_overlay     = ivtv_try_fmt_vid_out_overlay,
1951        .vidioc_try_fmt_sliced_vbi_out      = ivtv_try_fmt_sliced_vbi_out,
1952        .vidioc_g_sliced_vbi_cap            = ivtv_g_sliced_vbi_cap,
1953#ifdef CONFIG_VIDEO_ADV_DEBUG
1954        .vidioc_g_register                  = ivtv_g_register,
1955        .vidioc_s_register                  = ivtv_s_register,
1956#endif
1957        .vidioc_default                     = ivtv_default,
1958        .vidioc_subscribe_event             = ivtv_subscribe_event,
1959        .vidioc_unsubscribe_event           = v4l2_event_unsubscribe,
1960};
1961
1962void ivtv_set_funcs(struct video_device *vdev)
1963{
1964        vdev->ioctl_ops = &ivtv_ioctl_ops;
1965}
1966