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