This source file includes following definitions.
- ivtv_service2vbi
- valid_service_line
- select_service_from_set
- ivtv_expand_service_set
- check_service_set
- ivtv_get_service_set
- ivtv_set_osd_alpha
- ivtv_set_speed
- ivtv_validate_speed
- ivtv_video_command
- ivtv_g_fmt_sliced_vbi_out
- ivtv_g_fmt_vid_cap
- ivtv_g_fmt_vbi_cap
- ivtv_g_fmt_sliced_vbi_cap
- ivtv_g_fmt_vid_out
- ivtv_g_fmt_vid_out_overlay
- ivtv_try_fmt_sliced_vbi_out
- ivtv_try_fmt_vid_cap
- ivtv_try_fmt_vbi_cap
- ivtv_try_fmt_sliced_vbi_cap
- ivtv_try_fmt_vid_out
- ivtv_try_fmt_vid_out_overlay
- ivtv_s_fmt_sliced_vbi_out
- ivtv_s_fmt_vid_cap
- ivtv_s_fmt_vbi_cap
- ivtv_s_fmt_sliced_vbi_cap
- ivtv_s_fmt_vid_out
- ivtv_s_fmt_vid_out_overlay
- ivtv_itvc
- ivtv_g_register
- ivtv_s_register
- ivtv_querycap
- ivtv_enumaudio
- ivtv_g_audio
- ivtv_s_audio
- ivtv_enumaudout
- ivtv_g_audout
- ivtv_s_audout
- ivtv_enum_input
- ivtv_enum_output
- ivtv_g_pixelaspect
- ivtv_s_selection
- ivtv_g_selection
- ivtv_enum_fmt_vid_cap
- ivtv_enum_fmt_vid_out
- ivtv_g_input
- ivtv_s_input
- ivtv_g_output
- ivtv_s_output
- ivtv_g_frequency
- ivtv_s_frequency
- ivtv_g_std
- ivtv_s_std_enc
- ivtv_s_std_dec
- ivtv_s_std
- ivtv_s_tuner
- ivtv_g_tuner
- ivtv_g_sliced_vbi_cap
- ivtv_g_enc_index
- ivtv_encoder_cmd
- ivtv_try_encoder_cmd
- ivtv_g_fbuf
- ivtv_s_fbuf
- ivtv_overlay
- ivtv_subscribe_event
- ivtv_log_status
- ivtv_decoder_cmd
- ivtv_try_decoder_cmd
- warn_deprecated_ioctl
- ivtv_decoder_ioctls
- ivtv_default
- ivtv_set_funcs
   1 
   2 
   3 
   4 
   5 
   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 
  32 u16 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 
  48 static 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 
  54 static 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 
  82 void 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 
  95 static 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 
 106 u16 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 
 119 void 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 
 126 int 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         
 135         if (speed == itv->speed && !single_step)
 136                 return 0;
 137 
 138         if (single_step && (speed < 0) == (itv->speed < 0)) {
 139                 
 140                 ivtv_vapi(itv, CX2341X_DEC_STEP_VIDEO, 1, 0);
 141                 itv->speed = speed;
 142                 return 0;
 143         }
 144         if (single_step)
 145                 
 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         
 163         if (atomic_read(&itv->decoding) > 0) {
 164                 int got_sig = 0;
 165 
 166                 
 167                 ivtv_vapi(itv, CX2341X_DEC_PAUSE_PLAYBACK, 1, 0);
 168 
 169                 
 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                 
 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 
 197 static 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 
 236 static 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                         
 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 
 314 static 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 
 336 static 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                 
 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 
 359 static 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 
 377 static 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 
 400 static 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                 
 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 
 440 static 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 
 462 static 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 
 467 static 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                 
 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 
 490 static 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 
 495 static 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         
 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 
 516 static 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         
 527 
 528 
 529 
 530 
 531 
 532 
 533 
 534 
 535 
 536 
 537 
 538 
 539 
 540 
 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 
 550 static 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 
 567 static 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 
 572 static 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 
 603 static 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 
 615 static 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 
 634 static 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         
 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 
 679 static 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
 693 static 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 
 716 static 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, ®->val);
 722 }
 723 
 724 static 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 
 733 static 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 
 745 static 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 
 752 static 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 
 760 static 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 
 773 static int ivtv_enumaudout(struct file *file, void *fh, struct v4l2_audioout *vin)
 774 {
 775         struct ivtv *itv = fh2id(fh)->itv;
 776 
 777         
 778         return ivtv_get_audio_output(itv, vin->index, vin);
 779 }
 780 
 781 static 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 
 789 static 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 
 798 static int ivtv_enum_input(struct file *file, void *fh, struct v4l2_input *vin)
 799 {
 800         struct ivtv *itv = fh2id(fh)->itv;
 801 
 802         
 803         return ivtv_get_input(itv, vin->index, vin);
 804 }
 805 
 806 static 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 
 813 static 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 
 831 static 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 
 874 static 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 
 920 static int ivtv_enum_fmt_vid_cap(struct file *file, void *fh, struct v4l2_fmtdesc *fmt)
 921 {
 922         static const struct v4l2_fmtdesc hm12 = {
 923                 0, V4L2_BUF_TYPE_VIDEO_CAPTURE, 0,
 924                 "HM12 (YUV 4:2:0)", V4L2_PIX_FMT_HM12,
 925                 { 0, 0, 0, 0 }
 926         };
 927         static const struct v4l2_fmtdesc mpeg = {
 928                 0, V4L2_BUF_TYPE_VIDEO_CAPTURE, V4L2_FMT_FLAG_COMPRESSED,
 929                 "MPEG", V4L2_PIX_FMT_MPEG,
 930                 { 0, 0, 0, 0 }
 931         };
 932         struct ivtv *itv = fh2id(fh)->itv;
 933         struct ivtv_stream *s = &itv->streams[fh2id(fh)->type];
 934 
 935         if (fmt->index)
 936                 return -EINVAL;
 937         if (s->type == IVTV_ENC_STREAM_TYPE_MPG)
 938                 *fmt = mpeg;
 939         else if (s->type == IVTV_ENC_STREAM_TYPE_YUV)
 940                 *fmt = hm12;
 941         else
 942                 return -EINVAL;
 943         return 0;
 944 }
 945 
 946 static int ivtv_enum_fmt_vid_out(struct file *file, void *fh, struct v4l2_fmtdesc *fmt)
 947 {
 948         static const struct v4l2_fmtdesc hm12 = {
 949                 0, V4L2_BUF_TYPE_VIDEO_OUTPUT, 0,
 950                 "HM12 (YUV 4:2:0)", V4L2_PIX_FMT_HM12,
 951                 { 0, 0, 0, 0 }
 952         };
 953         static const struct v4l2_fmtdesc mpeg = {
 954                 0, V4L2_BUF_TYPE_VIDEO_OUTPUT, V4L2_FMT_FLAG_COMPRESSED,
 955                 "MPEG", V4L2_PIX_FMT_MPEG,
 956                 { 0, 0, 0, 0 }
 957         };
 958         struct ivtv *itv = fh2id(fh)->itv;
 959         struct ivtv_stream *s = &itv->streams[fh2id(fh)->type];
 960 
 961         if (fmt->index)
 962                 return -EINVAL;
 963         if (s->type == IVTV_DEC_STREAM_TYPE_MPG)
 964                 *fmt = mpeg;
 965         else if (s->type == IVTV_DEC_STREAM_TYPE_YUV)
 966                 *fmt = hm12;
 967         else
 968                 return -EINVAL;
 969         return 0;
 970 }
 971 
 972 static int ivtv_g_input(struct file *file, void *fh, unsigned int *i)
 973 {
 974         struct ivtv *itv = fh2id(fh)->itv;
 975 
 976         *i = itv->active_input;
 977 
 978         return 0;
 979 }
 980 
 981 int ivtv_s_input(struct file *file, void *fh, unsigned int inp)
 982 {
 983         struct ivtv *itv = fh2id(fh)->itv;
 984         v4l2_std_id std;
 985         int i;
 986 
 987         if (inp >= itv->nof_inputs)
 988                 return -EINVAL;
 989 
 990         if (inp == itv->active_input) {
 991                 IVTV_DEBUG_INFO("Input unchanged\n");
 992                 return 0;
 993         }
 994 
 995         if (atomic_read(&itv->capturing) > 0) {
 996                 return -EBUSY;
 997         }
 998 
 999         IVTV_DEBUG_INFO("Changing input from %d to %d\n",
1000                         itv->active_input, inp);
1001 
1002         itv->active_input = inp;
1003         
1004 
1005         itv->audio_input = itv->card->video_inputs[inp].audio_index;
1006 
1007         if (itv->card->video_inputs[inp].video_type == IVTV_CARD_INPUT_VID_TUNER)
1008                 std = itv->tuner_std;
1009         else
1010                 std = V4L2_STD_ALL;
1011         for (i = 0; i <= IVTV_ENC_STREAM_TYPE_VBI; i++)
1012                 itv->streams[i].vdev.tvnorms = std;
1013 
1014         
1015 
1016         ivtv_mute(itv);
1017         ivtv_video_set_io(itv);
1018         ivtv_audio_set_io(itv);
1019         ivtv_unmute(itv);
1020 
1021         return 0;
1022 }
1023 
1024 static int ivtv_g_output(struct file *file, void *fh, unsigned int *i)
1025 {
1026         struct ivtv *itv = fh2id(fh)->itv;
1027 
1028         if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
1029                 return -EINVAL;
1030 
1031         *i = itv->active_output;
1032 
1033         return 0;
1034 }
1035 
1036 static int ivtv_s_output(struct file *file, void *fh, unsigned int outp)
1037 {
1038         struct ivtv *itv = fh2id(fh)->itv;
1039 
1040         if (outp >= itv->card->nof_outputs)
1041                 return -EINVAL;
1042 
1043         if (outp == itv->active_output) {
1044                 IVTV_DEBUG_INFO("Output unchanged\n");
1045                 return 0;
1046         }
1047         IVTV_DEBUG_INFO("Changing output from %d to %d\n",
1048                    itv->active_output, outp);
1049 
1050         itv->active_output = outp;
1051         ivtv_call_hw(itv, IVTV_HW_SAA7127, video, s_routing,
1052                         SAA7127_INPUT_TYPE_NORMAL,
1053                         itv->card->video_outputs[outp].video_output, 0);
1054 
1055         return 0;
1056 }
1057 
1058 static int ivtv_g_frequency(struct file *file, void *fh, struct v4l2_frequency *vf)
1059 {
1060         struct ivtv *itv = fh2id(fh)->itv;
1061         struct ivtv_stream *s = &itv->streams[fh2id(fh)->type];
1062 
1063         if (s->vdev.vfl_dir)
1064                 return -ENOTTY;
1065         if (vf->tuner != 0)
1066                 return -EINVAL;
1067 
1068         ivtv_call_all(itv, tuner, g_frequency, vf);
1069         return 0;
1070 }
1071 
1072 int ivtv_s_frequency(struct file *file, void *fh, const struct v4l2_frequency *vf)
1073 {
1074         struct ivtv *itv = fh2id(fh)->itv;
1075         struct ivtv_stream *s = &itv->streams[fh2id(fh)->type];
1076 
1077         if (s->vdev.vfl_dir)
1078                 return -ENOTTY;
1079         if (vf->tuner != 0)
1080                 return -EINVAL;
1081 
1082         ivtv_mute(itv);
1083         IVTV_DEBUG_INFO("v4l2 ioctl: set frequency %d\n", vf->frequency);
1084         ivtv_call_all(itv, tuner, s_frequency, vf);
1085         ivtv_unmute(itv);
1086         return 0;
1087 }
1088 
1089 static int ivtv_g_std(struct file *file, void *fh, v4l2_std_id *std)
1090 {
1091         struct ivtv *itv = fh2id(fh)->itv;
1092 
1093         *std = itv->std;
1094         return 0;
1095 }
1096 
1097 void ivtv_s_std_enc(struct ivtv *itv, v4l2_std_id std)
1098 {
1099         itv->std = std;
1100         itv->is_60hz = (std & V4L2_STD_525_60) ? 1 : 0;
1101         itv->is_50hz = !itv->is_60hz;
1102         cx2341x_handler_set_50hz(&itv->cxhdl, itv->is_50hz);
1103         itv->cxhdl.width = 720;
1104         itv->cxhdl.height = itv->is_50hz ? 576 : 480;
1105         itv->vbi.count = itv->is_50hz ? 18 : 12;
1106         itv->vbi.start[0] = itv->is_50hz ? 6 : 10;
1107         itv->vbi.start[1] = itv->is_50hz ? 318 : 273;
1108 
1109         if (itv->hw_flags & IVTV_HW_CX25840)
1110                 itv->vbi.sliced_decoder_line_size = itv->is_60hz ? 272 : 284;
1111 
1112         
1113         ivtv_call_all(itv, video, s_std, itv->std);
1114 }
1115 
1116 void ivtv_s_std_dec(struct ivtv *itv, v4l2_std_id std)
1117 {
1118         struct yuv_playback_info *yi = &itv->yuv_info;
1119         DEFINE_WAIT(wait);
1120         int f;
1121 
1122         
1123         itv->std_out = std;
1124         itv->is_out_60hz = (std & V4L2_STD_525_60) ? 1 : 0;
1125         itv->is_out_50hz = !itv->is_out_60hz;
1126         ivtv_call_all(itv, video, s_std_output, itv->std_out);
1127 
1128         
1129 
1130 
1131 
1132 
1133 
1134         mutex_unlock(&itv->serialize_lock);
1135         for (f = 0; f < 4; f++) {
1136                 prepare_to_wait(&itv->vsync_waitq, &wait,
1137                                 TASK_UNINTERRUPTIBLE);
1138                 if ((read_reg(IVTV_REG_DEC_LINE_FIELD) >> 16) < 100)
1139                         break;
1140                 schedule_timeout(msecs_to_jiffies(25));
1141         }
1142         finish_wait(&itv->vsync_waitq, &wait);
1143         mutex_lock(&itv->serialize_lock);
1144 
1145         if (f == 4)
1146                 IVTV_WARN("Mode change failed to sync to decoder\n");
1147 
1148         ivtv_vapi(itv, CX2341X_DEC_SET_STANDARD, 1, itv->is_out_50hz);
1149         itv->main_rect.left = 0;
1150         itv->main_rect.top = 0;
1151         itv->main_rect.width = 720;
1152         itv->main_rect.height = itv->is_out_50hz ? 576 : 480;
1153         ivtv_vapi(itv, CX2341X_OSD_SET_FRAMEBUFFER_WINDOW, 4,
1154                 720, itv->main_rect.height, 0, 0);
1155         yi->main_rect = itv->main_rect;
1156         if (!itv->osd_info) {
1157                 yi->osd_full_w = 720;
1158                 yi->osd_full_h = itv->is_out_50hz ? 576 : 480;
1159         }
1160 }
1161 
1162 static int ivtv_s_std(struct file *file, void *fh, v4l2_std_id std)
1163 {
1164         struct ivtv *itv = fh2id(fh)->itv;
1165 
1166         if ((std & V4L2_STD_ALL) == 0)
1167                 return -EINVAL;
1168 
1169         if (std == itv->std)
1170                 return 0;
1171 
1172         if (test_bit(IVTV_F_I_RADIO_USER, &itv->i_flags) ||
1173             atomic_read(&itv->capturing) > 0 ||
1174             atomic_read(&itv->decoding) > 0) {
1175                 
1176 
1177                 return -EBUSY;
1178         }
1179 
1180         IVTV_DEBUG_INFO("Switching standard to %llx.\n",
1181                 (unsigned long long)itv->std);
1182 
1183         ivtv_s_std_enc(itv, std);
1184         if (itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT)
1185                 ivtv_s_std_dec(itv, std);
1186 
1187         return 0;
1188 }
1189 
1190 static int ivtv_s_tuner(struct file *file, void *fh, const struct v4l2_tuner *vt)
1191 {
1192         struct ivtv_open_id *id = fh2id(fh);
1193         struct ivtv *itv = id->itv;
1194 
1195         if (vt->index != 0)
1196                 return -EINVAL;
1197 
1198         ivtv_call_all(itv, tuner, s_tuner, vt);
1199 
1200         return 0;
1201 }
1202 
1203 static int ivtv_g_tuner(struct file *file, void *fh, struct v4l2_tuner *vt)
1204 {
1205         struct ivtv *itv = fh2id(fh)->itv;
1206 
1207         if (vt->index != 0)
1208                 return -EINVAL;
1209 
1210         ivtv_call_all(itv, tuner, g_tuner, vt);
1211 
1212         if (vt->type == V4L2_TUNER_RADIO)
1213                 strscpy(vt->name, "ivtv Radio Tuner", sizeof(vt->name));
1214         else
1215                 strscpy(vt->name, "ivtv TV Tuner", sizeof(vt->name));
1216         return 0;
1217 }
1218 
1219 static int ivtv_g_sliced_vbi_cap(struct file *file, void *fh, struct v4l2_sliced_vbi_cap *cap)
1220 {
1221         struct ivtv *itv = fh2id(fh)->itv;
1222         int set = itv->is_50hz ? V4L2_SLICED_VBI_625 : V4L2_SLICED_VBI_525;
1223         int f, l;
1224 
1225         if (cap->type == V4L2_BUF_TYPE_SLICED_VBI_CAPTURE) {
1226                 for (f = 0; f < 2; f++) {
1227                         for (l = 0; l < 24; l++) {
1228                                 if (valid_service_line(f, l, itv->is_50hz))
1229                                         cap->service_lines[f][l] = set;
1230                         }
1231                 }
1232         } else if (cap->type == V4L2_BUF_TYPE_SLICED_VBI_OUTPUT) {
1233                 if (!(itv->v4l2_cap & V4L2_CAP_SLICED_VBI_OUTPUT))
1234                         return -EINVAL;
1235                 if (itv->is_60hz) {
1236                         cap->service_lines[0][21] = V4L2_SLICED_CAPTION_525;
1237                         cap->service_lines[1][21] = V4L2_SLICED_CAPTION_525;
1238                 } else {
1239                         cap->service_lines[0][23] = V4L2_SLICED_WSS_625;
1240                         cap->service_lines[0][16] = V4L2_SLICED_VPS;
1241                 }
1242         } else {
1243                 return -EINVAL;
1244         }
1245 
1246         set = 0;
1247         for (f = 0; f < 2; f++)
1248                 for (l = 0; l < 24; l++)
1249                         set |= cap->service_lines[f][l];
1250         cap->service_set = set;
1251         return 0;
1252 }
1253 
1254 static int ivtv_g_enc_index(struct file *file, void *fh, struct v4l2_enc_idx *idx)
1255 {
1256         struct ivtv *itv = fh2id(fh)->itv;
1257         struct v4l2_enc_idx_entry *e = idx->entry;
1258         int entries;
1259         int i;
1260 
1261         entries = (itv->pgm_info_write_idx + IVTV_MAX_PGM_INDEX - itv->pgm_info_read_idx) %
1262                                 IVTV_MAX_PGM_INDEX;
1263         if (entries > V4L2_ENC_IDX_ENTRIES)
1264                 entries = V4L2_ENC_IDX_ENTRIES;
1265         idx->entries = 0;
1266         idx->entries_cap = IVTV_MAX_PGM_INDEX;
1267         if (!atomic_read(&itv->capturing))
1268                 return 0;
1269         for (i = 0; i < entries; i++) {
1270                 *e = itv->pgm_info[(itv->pgm_info_read_idx + i) % IVTV_MAX_PGM_INDEX];
1271                 if ((e->flags & V4L2_ENC_IDX_FRAME_MASK) <= V4L2_ENC_IDX_FRAME_B) {
1272                         idx->entries++;
1273                         e++;
1274                 }
1275         }
1276         itv->pgm_info_read_idx = (itv->pgm_info_read_idx + idx->entries) % IVTV_MAX_PGM_INDEX;
1277         return 0;
1278 }
1279 
1280 static int ivtv_encoder_cmd(struct file *file, void *fh, struct v4l2_encoder_cmd *enc)
1281 {
1282         struct ivtv_open_id *id = fh2id(fh);
1283         struct ivtv *itv = id->itv;
1284 
1285 
1286         switch (enc->cmd) {
1287         case V4L2_ENC_CMD_START:
1288                 IVTV_DEBUG_IOCTL("V4L2_ENC_CMD_START\n");
1289                 enc->flags = 0;
1290                 return ivtv_start_capture(id);
1291 
1292         case V4L2_ENC_CMD_STOP:
1293                 IVTV_DEBUG_IOCTL("V4L2_ENC_CMD_STOP\n");
1294                 enc->flags &= V4L2_ENC_CMD_STOP_AT_GOP_END;
1295                 ivtv_stop_capture(id, enc->flags & V4L2_ENC_CMD_STOP_AT_GOP_END);
1296                 return 0;
1297 
1298         case V4L2_ENC_CMD_PAUSE:
1299                 IVTV_DEBUG_IOCTL("V4L2_ENC_CMD_PAUSE\n");
1300                 enc->flags = 0;
1301 
1302                 if (!atomic_read(&itv->capturing))
1303                         return -EPERM;
1304                 if (test_and_set_bit(IVTV_F_I_ENC_PAUSED, &itv->i_flags))
1305                         return 0;
1306 
1307                 ivtv_mute(itv);
1308                 ivtv_vapi(itv, CX2341X_ENC_PAUSE_ENCODER, 1, 0);
1309                 break;
1310 
1311         case V4L2_ENC_CMD_RESUME:
1312                 IVTV_DEBUG_IOCTL("V4L2_ENC_CMD_RESUME\n");
1313                 enc->flags = 0;
1314 
1315                 if (!atomic_read(&itv->capturing))
1316                         return -EPERM;
1317 
1318                 if (!test_and_clear_bit(IVTV_F_I_ENC_PAUSED, &itv->i_flags))
1319                         return 0;
1320 
1321                 ivtv_vapi(itv, CX2341X_ENC_PAUSE_ENCODER, 1, 1);
1322                 ivtv_unmute(itv);
1323                 break;
1324         default:
1325                 IVTV_DEBUG_IOCTL("Unknown cmd %d\n", enc->cmd);
1326                 return -EINVAL;
1327         }
1328 
1329         return 0;
1330 }
1331 
1332 static int ivtv_try_encoder_cmd(struct file *file, void *fh, struct v4l2_encoder_cmd *enc)
1333 {
1334         struct ivtv *itv = fh2id(fh)->itv;
1335 
1336         switch (enc->cmd) {
1337         case V4L2_ENC_CMD_START:
1338                 IVTV_DEBUG_IOCTL("V4L2_ENC_CMD_START\n");
1339                 enc->flags = 0;
1340                 return 0;
1341 
1342         case V4L2_ENC_CMD_STOP:
1343                 IVTV_DEBUG_IOCTL("V4L2_ENC_CMD_STOP\n");
1344                 enc->flags &= V4L2_ENC_CMD_STOP_AT_GOP_END;
1345                 return 0;
1346 
1347         case V4L2_ENC_CMD_PAUSE:
1348                 IVTV_DEBUG_IOCTL("V4L2_ENC_CMD_PAUSE\n");
1349                 enc->flags = 0;
1350                 return 0;
1351 
1352         case V4L2_ENC_CMD_RESUME:
1353                 IVTV_DEBUG_IOCTL("V4L2_ENC_CMD_RESUME\n");
1354                 enc->flags = 0;
1355                 return 0;
1356         default:
1357                 IVTV_DEBUG_IOCTL("Unknown cmd %d\n", enc->cmd);
1358                 return -EINVAL;
1359         }
1360 }
1361 
1362 static int ivtv_g_fbuf(struct file *file, void *fh, struct v4l2_framebuffer *fb)
1363 {
1364         struct ivtv *itv = fh2id(fh)->itv;
1365         struct ivtv_stream *s = &itv->streams[fh2id(fh)->type];
1366         u32 data[CX2341X_MBOX_MAX_DATA];
1367         struct yuv_playback_info *yi = &itv->yuv_info;
1368 
1369         int pixfmt;
1370         static u32 pixel_format[16] = {
1371                 V4L2_PIX_FMT_PAL8, 
1372                 V4L2_PIX_FMT_RGB565,
1373                 V4L2_PIX_FMT_RGB555,
1374                 V4L2_PIX_FMT_RGB444,
1375                 V4L2_PIX_FMT_RGB32,
1376                 0,
1377                 0,
1378                 0,
1379                 V4L2_PIX_FMT_PAL8, 
1380                 V4L2_PIX_FMT_YUV565,
1381                 V4L2_PIX_FMT_YUV555,
1382                 V4L2_PIX_FMT_YUV444,
1383                 V4L2_PIX_FMT_YUV32,
1384                 0,
1385                 0,
1386                 0,
1387         };
1388 
1389         if (!(s->caps & V4L2_CAP_VIDEO_OUTPUT_OVERLAY))
1390                 return -ENOTTY;
1391         if (!itv->osd_video_pbase)
1392                 return -ENOTTY;
1393 
1394         fb->capability = V4L2_FBUF_CAP_EXTERNOVERLAY | V4L2_FBUF_CAP_CHROMAKEY |
1395                 V4L2_FBUF_CAP_GLOBAL_ALPHA;
1396 
1397         ivtv_vapi_result(itv, data, CX2341X_OSD_GET_STATE, 0);
1398         data[0] |= (read_reg(0x2a00) >> 7) & 0x40;
1399         pixfmt = (data[0] >> 3) & 0xf;
1400 
1401         fb->fmt.pixelformat = pixel_format[pixfmt];
1402         fb->fmt.width = itv->osd_rect.width;
1403         fb->fmt.height = itv->osd_rect.height;
1404         fb->fmt.field = V4L2_FIELD_INTERLACED;
1405         fb->fmt.bytesperline = fb->fmt.width;
1406         fb->fmt.colorspace = V4L2_COLORSPACE_SMPTE170M;
1407         fb->fmt.field = V4L2_FIELD_INTERLACED;
1408         if (fb->fmt.pixelformat != V4L2_PIX_FMT_PAL8)
1409                 fb->fmt.bytesperline *= 2;
1410         if (fb->fmt.pixelformat == V4L2_PIX_FMT_RGB32 ||
1411             fb->fmt.pixelformat == V4L2_PIX_FMT_YUV32)
1412                 fb->fmt.bytesperline *= 2;
1413         fb->fmt.sizeimage = fb->fmt.bytesperline * fb->fmt.height;
1414         fb->base = (void *)itv->osd_video_pbase;
1415         fb->flags = 0;
1416 
1417         if (itv->osd_chroma_key_state)
1418                 fb->flags |= V4L2_FBUF_FLAG_CHROMAKEY;
1419 
1420         if (itv->osd_global_alpha_state)
1421                 fb->flags |= V4L2_FBUF_FLAG_GLOBAL_ALPHA;
1422 
1423         if (yi->track_osd)
1424                 fb->flags |= V4L2_FBUF_FLAG_OVERLAY;
1425 
1426         pixfmt &= 7;
1427 
1428         
1429         if (pixfmt == 1 || pixfmt > 4)
1430                 return 0;
1431 
1432         
1433         if (pixfmt == 2 || pixfmt == 3)
1434                 fb->capability |= V4L2_FBUF_CAP_LOCAL_INV_ALPHA;
1435         else
1436                 fb->capability |= V4L2_FBUF_CAP_LOCAL_ALPHA;
1437 
1438         if (itv->osd_local_alpha_state) {
1439                 
1440                 if (pixfmt == 2 || pixfmt == 3)
1441                         fb->flags |= V4L2_FBUF_FLAG_LOCAL_INV_ALPHA;
1442                 else
1443                         fb->flags |= V4L2_FBUF_FLAG_LOCAL_ALPHA;
1444         }
1445 
1446         return 0;
1447 }
1448 
1449 static int ivtv_s_fbuf(struct file *file, void *fh, const struct v4l2_framebuffer *fb)
1450 {
1451         struct ivtv_open_id *id = fh2id(fh);
1452         struct ivtv *itv = id->itv;
1453         struct ivtv_stream *s = &itv->streams[fh2id(fh)->type];
1454         struct yuv_playback_info *yi = &itv->yuv_info;
1455 
1456         if (!(s->caps & V4L2_CAP_VIDEO_OUTPUT_OVERLAY))
1457                 return -ENOTTY;
1458         if (!itv->osd_video_pbase)
1459                 return -ENOTTY;
1460 
1461         itv->osd_global_alpha_state = (fb->flags & V4L2_FBUF_FLAG_GLOBAL_ALPHA) != 0;
1462         itv->osd_local_alpha_state =
1463                 (fb->flags & (V4L2_FBUF_FLAG_LOCAL_ALPHA|V4L2_FBUF_FLAG_LOCAL_INV_ALPHA)) != 0;
1464         itv->osd_chroma_key_state = (fb->flags & V4L2_FBUF_FLAG_CHROMAKEY) != 0;
1465         ivtv_set_osd_alpha(itv);
1466         yi->track_osd = (fb->flags & V4L2_FBUF_FLAG_OVERLAY) != 0;
1467         return 0;
1468 }
1469 
1470 static int ivtv_overlay(struct file *file, void *fh, unsigned int on)
1471 {
1472         struct ivtv_open_id *id = fh2id(fh);
1473         struct ivtv *itv = id->itv;
1474         struct ivtv_stream *s = &itv->streams[fh2id(fh)->type];
1475 
1476         if (!(s->caps & V4L2_CAP_VIDEO_OUTPUT_OVERLAY))
1477                 return -ENOTTY;
1478         if (!itv->osd_video_pbase)
1479                 return -ENOTTY;
1480 
1481         ivtv_vapi(itv, CX2341X_OSD_SET_STATE, 1, on != 0);
1482 
1483         return 0;
1484 }
1485 
1486 static int ivtv_subscribe_event(struct v4l2_fh *fh, const struct v4l2_event_subscription *sub)
1487 {
1488         switch (sub->type) {
1489         case V4L2_EVENT_VSYNC:
1490         case V4L2_EVENT_EOS:
1491                 return v4l2_event_subscribe(fh, sub, 0, NULL);
1492         default:
1493                 return v4l2_ctrl_subscribe_event(fh, sub);
1494         }
1495 }
1496 
1497 static int ivtv_log_status(struct file *file, void *fh)
1498 {
1499         struct ivtv *itv = fh2id(fh)->itv;
1500         u32 data[CX2341X_MBOX_MAX_DATA];
1501 
1502         int has_output = itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT;
1503         struct v4l2_input vidin;
1504         struct v4l2_audio audin;
1505         int i;
1506 
1507         IVTV_INFO("Version: %s Card: %s\n", IVTV_VERSION, itv->card_name);
1508         if (itv->hw_flags & IVTV_HW_TVEEPROM) {
1509                 struct tveeprom tv;
1510 
1511                 ivtv_read_eeprom(itv, &tv);
1512         }
1513         ivtv_call_all(itv, core, log_status);
1514         ivtv_get_input(itv, itv->active_input, &vidin);
1515         ivtv_get_audio_input(itv, itv->audio_input, &audin);
1516         IVTV_INFO("Video Input:  %s\n", vidin.name);
1517         IVTV_INFO("Audio Input:  %s%s\n", audin.name,
1518                 itv->dualwatch_stereo_mode == V4L2_MPEG_AUDIO_MODE_DUAL ?
1519                         " (Bilingual)" : "");
1520         if (has_output) {
1521                 struct v4l2_output vidout;
1522                 struct v4l2_audioout audout;
1523                 int mode = itv->output_mode;
1524                 static const char * const output_modes[5] = {
1525                         "None",
1526                         "MPEG Streaming",
1527                         "YUV Streaming",
1528                         "YUV Frames",
1529                         "Passthrough",
1530                 };
1531                 static const char * const alpha_mode[4] = {
1532                         "None",
1533                         "Global",
1534                         "Local",
1535                         "Global and Local"
1536                 };
1537                 static const char * const pixel_format[16] = {
1538                         "ARGB Indexed",
1539                         "RGB 5:6:5",
1540                         "ARGB 1:5:5:5",
1541                         "ARGB 1:4:4:4",
1542                         "ARGB 8:8:8:8",
1543                         "5",
1544                         "6",
1545                         "7",
1546                         "AYUV Indexed",
1547                         "YUV 5:6:5",
1548                         "AYUV 1:5:5:5",
1549                         "AYUV 1:4:4:4",
1550                         "AYUV 8:8:8:8",
1551                         "13",
1552                         "14",
1553                         "15",
1554                 };
1555 
1556                 ivtv_get_output(itv, itv->active_output, &vidout);
1557                 ivtv_get_audio_output(itv, 0, &audout);
1558                 IVTV_INFO("Video Output: %s\n", vidout.name);
1559                 if (mode < 0 || mode > OUT_PASSTHROUGH)
1560                         mode = OUT_NONE;
1561                 IVTV_INFO("Output Mode:  %s\n", output_modes[mode]);
1562                 ivtv_vapi_result(itv, data, CX2341X_OSD_GET_STATE, 0);
1563                 data[0] |= (read_reg(0x2a00) >> 7) & 0x40;
1564                 IVTV_INFO("Overlay:      %s, Alpha: %s, Pixel Format: %s\n",
1565                         data[0] & 1 ? "On" : "Off",
1566                         alpha_mode[(data[0] >> 1) & 0x3],
1567                         pixel_format[(data[0] >> 3) & 0xf]);
1568         }
1569         IVTV_INFO("Tuner:  %s\n",
1570                 test_bit(IVTV_F_I_RADIO_USER, &itv->i_flags) ? "Radio" : "TV");
1571         v4l2_ctrl_handler_log_status(&itv->cxhdl.hdl, itv->v4l2_dev.name);
1572         IVTV_INFO("Status flags:    0x%08lx\n", itv->i_flags);
1573         for (i = 0; i < IVTV_MAX_STREAMS; i++) {
1574                 struct ivtv_stream *s = &itv->streams[i];
1575 
1576                 if (s->vdev.v4l2_dev == NULL || s->buffers == 0)
1577                         continue;
1578                 IVTV_INFO("Stream %s: status 0x%04lx, %d%% of %d KiB (%d buffers) in use\n", s->name, s->s_flags,
1579                                 (s->buffers - s->q_free.buffers) * 100 / s->buffers,
1580                                 (s->buffers * s->buf_size) / 1024, s->buffers);
1581         }
1582 
1583         IVTV_INFO("Read MPG/VBI: %lld/%lld bytes\n",
1584                         (long long)itv->mpg_data_received,
1585                         (long long)itv->vbi_data_inserted);
1586         return 0;
1587 }
1588 
1589 static int ivtv_decoder_cmd(struct file *file, void *fh, struct v4l2_decoder_cmd *dec)
1590 {
1591         struct ivtv_open_id *id = fh2id(file->private_data);
1592         struct ivtv *itv = id->itv;
1593 
1594         IVTV_DEBUG_IOCTL("VIDIOC_DECODER_CMD %d\n", dec->cmd);
1595         return ivtv_video_command(itv, id, dec, false);
1596 }
1597 
1598 static int ivtv_try_decoder_cmd(struct file *file, void *fh, struct v4l2_decoder_cmd *dec)
1599 {
1600         struct ivtv_open_id *id = fh2id(file->private_data);
1601         struct ivtv *itv = id->itv;
1602 
1603         IVTV_DEBUG_IOCTL("VIDIOC_TRY_DECODER_CMD %d\n", dec->cmd);
1604         return ivtv_video_command(itv, id, dec, true);
1605 }
1606 
1607 #ifdef CONFIG_VIDEO_IVTV_DEPRECATED_IOCTLS
1608 static __inline__ void warn_deprecated_ioctl(const char *name)
1609 {
1610         pr_warn_once("warning: the %s ioctl is deprecated. Don't use it, as it will be removed soon\n",
1611                      name);
1612 }
1613 
1614 #ifdef CONFIG_COMPAT
1615 struct compat_video_event {
1616         __s32 type;
1617         
1618         compat_long_t timestamp;
1619         union {
1620                 video_size_t size;
1621                 unsigned int frame_rate;        
1622                 unsigned char vsync_field;      
1623         } u;
1624 };
1625 #define VIDEO_GET_EVENT32 _IOR('o', 28, struct compat_video_event)
1626 #endif
1627 
1628 #endif
1629 
1630 static int ivtv_decoder_ioctls(struct file *filp, unsigned int cmd, void *arg)
1631 {
1632         struct ivtv_open_id *id = fh2id(filp->private_data);
1633         struct ivtv *itv = id->itv;
1634         struct ivtv_stream *s = &itv->streams[id->type];
1635 #ifdef CONFIG_VIDEO_IVTV_DEPRECATED_IOCTLS
1636         int nonblocking = filp->f_flags & O_NONBLOCK;
1637         unsigned long iarg = (unsigned long)arg;
1638 #endif
1639 
1640         switch (cmd) {
1641         case IVTV_IOC_DMA_FRAME: {
1642                 struct ivtv_dma_frame *args = arg;
1643 
1644                 IVTV_DEBUG_IOCTL("IVTV_IOC_DMA_FRAME\n");
1645                 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
1646                         return -EINVAL;
1647                 if (args->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
1648                         return -EINVAL;
1649                 if (itv->output_mode == OUT_UDMA_YUV && args->y_source == NULL)
1650                         return 0;
1651                 if (ivtv_start_decoding(id, id->type)) {
1652                         return -EBUSY;
1653                 }
1654                 if (ivtv_set_output_mode(itv, OUT_UDMA_YUV) != OUT_UDMA_YUV) {
1655                         ivtv_release_stream(s);
1656                         return -EBUSY;
1657                 }
1658                 
1659                 id->yuv_frames = 1;
1660                 if (args->y_source == NULL)
1661                         return 0;
1662                 return ivtv_yuv_prep_frame(itv, args);
1663         }
1664 
1665         case IVTV_IOC_PASSTHROUGH_MODE:
1666                 IVTV_DEBUG_IOCTL("IVTV_IOC_PASSTHROUGH_MODE\n");
1667                 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
1668                         return -EINVAL;
1669                 return ivtv_passthrough_mode(itv, *(int *)arg != 0);
1670 #ifdef CONFIG_VIDEO_IVTV_DEPRECATED_IOCTLS
1671         case VIDEO_GET_PTS: {
1672                 s64 *pts = arg;
1673                 s64 frame;
1674 
1675                 warn_deprecated_ioctl("VIDEO_GET_PTS");
1676                 if (s->type < IVTV_DEC_STREAM_TYPE_MPG) {
1677                         *pts = s->dma_pts;
1678                         break;
1679                 }
1680                 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
1681                         return -EINVAL;
1682                 return ivtv_g_pts_frame(itv, pts, &frame);
1683         }
1684 
1685         case VIDEO_GET_FRAME_COUNT: {
1686                 s64 *frame = arg;
1687                 s64 pts;
1688 
1689                 warn_deprecated_ioctl("VIDEO_GET_FRAME_COUNT");
1690                 if (s->type < IVTV_DEC_STREAM_TYPE_MPG) {
1691                         *frame = 0;
1692                         break;
1693                 }
1694                 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
1695                         return -EINVAL;
1696                 return ivtv_g_pts_frame(itv, &pts, frame);
1697         }
1698 
1699         case VIDEO_PLAY: {
1700                 struct v4l2_decoder_cmd dc;
1701 
1702                 warn_deprecated_ioctl("VIDEO_PLAY");
1703                 memset(&dc, 0, sizeof(dc));
1704                 dc.cmd = V4L2_DEC_CMD_START;
1705                 return ivtv_video_command(itv, id, &dc, 0);
1706         }
1707 
1708         case VIDEO_STOP: {
1709                 struct v4l2_decoder_cmd dc;
1710 
1711                 warn_deprecated_ioctl("VIDEO_STOP");
1712                 memset(&dc, 0, sizeof(dc));
1713                 dc.cmd = V4L2_DEC_CMD_STOP;
1714                 dc.flags = V4L2_DEC_CMD_STOP_TO_BLACK | V4L2_DEC_CMD_STOP_IMMEDIATELY;
1715                 return ivtv_video_command(itv, id, &dc, 0);
1716         }
1717 
1718         case VIDEO_FREEZE: {
1719                 struct v4l2_decoder_cmd dc;
1720 
1721                 warn_deprecated_ioctl("VIDEO_FREEZE");
1722                 memset(&dc, 0, sizeof(dc));
1723                 dc.cmd = V4L2_DEC_CMD_PAUSE;
1724                 return ivtv_video_command(itv, id, &dc, 0);
1725         }
1726 
1727         case VIDEO_CONTINUE: {
1728                 struct v4l2_decoder_cmd dc;
1729 
1730                 warn_deprecated_ioctl("VIDEO_CONTINUE");
1731                 memset(&dc, 0, sizeof(dc));
1732                 dc.cmd = V4L2_DEC_CMD_RESUME;
1733                 return ivtv_video_command(itv, id, &dc, 0);
1734         }
1735 
1736         case VIDEO_COMMAND:
1737         case VIDEO_TRY_COMMAND: {
1738                 
1739 
1740                 struct v4l2_decoder_cmd *dc = arg;
1741                 int try = (cmd == VIDEO_TRY_COMMAND);
1742 
1743                 if (try)
1744                         warn_deprecated_ioctl("VIDEO_TRY_COMMAND");
1745                 else
1746                         warn_deprecated_ioctl("VIDEO_COMMAND");
1747                 return ivtv_video_command(itv, id, dc, try);
1748         }
1749 
1750 #ifdef CONFIG_COMPAT
1751         case VIDEO_GET_EVENT32:
1752 #endif
1753         case VIDEO_GET_EVENT: {
1754 #ifdef CONFIG_COMPAT
1755                 struct compat_video_event *ev32 = arg;
1756 #endif
1757                 struct video_event *ev = arg;
1758                 DEFINE_WAIT(wait);
1759 
1760                 warn_deprecated_ioctl("VIDEO_GET_EVENT");
1761                 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
1762                         return -EINVAL;
1763                 memset(ev, 0, sizeof(*ev));
1764                 set_bit(IVTV_F_I_EV_VSYNC_ENABLED, &itv->i_flags);
1765 
1766                 while (1) {
1767                         if (test_and_clear_bit(IVTV_F_I_EV_DEC_STOPPED, &itv->i_flags))
1768                                 ev->type = VIDEO_EVENT_DECODER_STOPPED;
1769                         else if (test_and_clear_bit(IVTV_F_I_EV_VSYNC, &itv->i_flags)) {
1770                                 unsigned char vsync_field;
1771 
1772                                 ev->type = VIDEO_EVENT_VSYNC;
1773                                 vsync_field = test_bit(IVTV_F_I_EV_VSYNC_FIELD, &itv->i_flags) ?
1774                                         VIDEO_VSYNC_FIELD_ODD : VIDEO_VSYNC_FIELD_EVEN;
1775                                 if (itv->output_mode == OUT_UDMA_YUV &&
1776                                         (itv->yuv_info.lace_mode & IVTV_YUV_MODE_MASK) ==
1777                                                                 IVTV_YUV_MODE_PROGRESSIVE) {
1778                                         vsync_field = VIDEO_VSYNC_FIELD_PROGRESSIVE;
1779                                 }
1780 #ifdef CONFIG_COMPAT
1781                                 if (cmd == VIDEO_GET_EVENT32)
1782                                         ev32->u.vsync_field = vsync_field;
1783                                 else
1784 #endif
1785                                         ev->u.vsync_field = vsync_field;
1786                         }
1787                         if (ev->type)
1788                                 return 0;
1789                         if (nonblocking)
1790                                 return -EAGAIN;
1791                         
1792 
1793 
1794                         mutex_unlock(&itv->serialize_lock);
1795                         prepare_to_wait(&itv->event_waitq, &wait, TASK_INTERRUPTIBLE);
1796                         if (!test_bit(IVTV_F_I_EV_DEC_STOPPED, &itv->i_flags) &&
1797                             !test_bit(IVTV_F_I_EV_VSYNC, &itv->i_flags))
1798                                 schedule();
1799                         finish_wait(&itv->event_waitq, &wait);
1800                         mutex_lock(&itv->serialize_lock);
1801                         if (signal_pending(current)) {
1802                                 
1803                                 IVTV_DEBUG_INFO("User stopped wait for event\n");
1804                                 return -EINTR;
1805                         }
1806                 }
1807                 break;
1808         }
1809 
1810         case VIDEO_SELECT_SOURCE:
1811                 warn_deprecated_ioctl("VIDEO_SELECT_SOURCE");
1812                 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
1813                         return -EINVAL;
1814                 return ivtv_passthrough_mode(itv, iarg == VIDEO_SOURCE_DEMUX);
1815 
1816         case AUDIO_SET_MUTE:
1817                 warn_deprecated_ioctl("AUDIO_SET_MUTE");
1818                 itv->speed_mute_audio = iarg;
1819                 return 0;
1820 
1821         case AUDIO_CHANNEL_SELECT:
1822                 warn_deprecated_ioctl("AUDIO_CHANNEL_SELECT");
1823                 if (iarg > AUDIO_STEREO_SWAPPED)
1824                         return -EINVAL;
1825                 return v4l2_ctrl_s_ctrl(itv->ctrl_audio_playback, iarg + 1);
1826 
1827         case AUDIO_BILINGUAL_CHANNEL_SELECT:
1828                 warn_deprecated_ioctl("AUDIO_BILINGUAL_CHANNEL_SELECT");
1829                 if (iarg > AUDIO_STEREO_SWAPPED)
1830                         return -EINVAL;
1831                 return v4l2_ctrl_s_ctrl(itv->ctrl_audio_multilingual_playback, iarg + 1);
1832 #endif
1833         default:
1834                 return -EINVAL;
1835         }
1836         return 0;
1837 }
1838 
1839 static long ivtv_default(struct file *file, void *fh, bool valid_prio,
1840                          unsigned int cmd, void *arg)
1841 {
1842         struct ivtv *itv = fh2id(fh)->itv;
1843 
1844         if (!valid_prio) {
1845                 switch (cmd) {
1846                 case IVTV_IOC_PASSTHROUGH_MODE:
1847 #ifdef CONFIG_VIDEO_IVTV_DEPRECATED_IOCTLS
1848                 case VIDEO_PLAY:
1849                 case VIDEO_STOP:
1850                 case VIDEO_FREEZE:
1851                 case VIDEO_CONTINUE:
1852                 case VIDEO_COMMAND:
1853                 case VIDEO_SELECT_SOURCE:
1854                 case AUDIO_SET_MUTE:
1855                 case AUDIO_CHANNEL_SELECT:
1856                 case AUDIO_BILINGUAL_CHANNEL_SELECT:
1857 #endif
1858                         return -EBUSY;
1859                 }
1860         }
1861 
1862         switch (cmd) {
1863         case VIDIOC_INT_RESET: {
1864                 u32 val = *(u32 *)arg;
1865 
1866                 if ((val == 0 && itv->options.newi2c) || (val & 0x01))
1867                         ivtv_reset_ir_gpio(itv);
1868                 if (val & 0x02)
1869                         v4l2_subdev_call(itv->sd_video, core, reset, 0);
1870                 break;
1871         }
1872 
1873         case IVTV_IOC_DMA_FRAME:
1874         case IVTV_IOC_PASSTHROUGH_MODE:
1875 #ifdef CONFIG_VIDEO_IVTV_DEPRECATED_IOCTLS
1876         case VIDEO_GET_PTS:
1877         case VIDEO_GET_FRAME_COUNT:
1878         case VIDEO_GET_EVENT:
1879         case VIDEO_PLAY:
1880         case VIDEO_STOP:
1881         case VIDEO_FREEZE:
1882         case VIDEO_CONTINUE:
1883         case VIDEO_COMMAND:
1884         case VIDEO_TRY_COMMAND:
1885         case VIDEO_SELECT_SOURCE:
1886         case AUDIO_SET_MUTE:
1887         case AUDIO_CHANNEL_SELECT:
1888         case AUDIO_BILINGUAL_CHANNEL_SELECT:
1889 #endif
1890                 return ivtv_decoder_ioctls(file, cmd, (void *)arg);
1891 
1892         default:
1893                 return -ENOTTY;
1894         }
1895         return 0;
1896 }
1897 
1898 static const struct v4l2_ioctl_ops ivtv_ioctl_ops = {
1899         .vidioc_querycap                    = ivtv_querycap,
1900         .vidioc_s_audio                     = ivtv_s_audio,
1901         .vidioc_g_audio                     = ivtv_g_audio,
1902         .vidioc_enumaudio                   = ivtv_enumaudio,
1903         .vidioc_s_audout                    = ivtv_s_audout,
1904         .vidioc_g_audout                    = ivtv_g_audout,
1905         .vidioc_enum_input                  = ivtv_enum_input,
1906         .vidioc_enum_output                 = ivtv_enum_output,
1907         .vidioc_enumaudout                  = ivtv_enumaudout,
1908         .vidioc_g_pixelaspect               = ivtv_g_pixelaspect,
1909         .vidioc_s_selection                 = ivtv_s_selection,
1910         .vidioc_g_selection                 = ivtv_g_selection,
1911         .vidioc_g_input                     = ivtv_g_input,
1912         .vidioc_s_input                     = ivtv_s_input,
1913         .vidioc_g_output                    = ivtv_g_output,
1914         .vidioc_s_output                    = ivtv_s_output,
1915         .vidioc_g_frequency                 = ivtv_g_frequency,
1916         .vidioc_s_frequency                 = ivtv_s_frequency,
1917         .vidioc_s_tuner                     = ivtv_s_tuner,
1918         .vidioc_g_tuner                     = ivtv_g_tuner,
1919         .vidioc_g_enc_index                 = ivtv_g_enc_index,
1920         .vidioc_g_fbuf                      = ivtv_g_fbuf,
1921         .vidioc_s_fbuf                      = ivtv_s_fbuf,
1922         .vidioc_g_std                       = ivtv_g_std,
1923         .vidioc_s_std                       = ivtv_s_std,
1924         .vidioc_overlay                     = ivtv_overlay,
1925         .vidioc_log_status                  = ivtv_log_status,
1926         .vidioc_enum_fmt_vid_cap            = ivtv_enum_fmt_vid_cap,
1927         .vidioc_encoder_cmd                 = ivtv_encoder_cmd,
1928         .vidioc_try_encoder_cmd             = ivtv_try_encoder_cmd,
1929         .vidioc_decoder_cmd                 = ivtv_decoder_cmd,
1930         .vidioc_try_decoder_cmd             = ivtv_try_decoder_cmd,
1931         .vidioc_enum_fmt_vid_out            = ivtv_enum_fmt_vid_out,
1932         .vidioc_g_fmt_vid_cap               = ivtv_g_fmt_vid_cap,
1933         .vidioc_g_fmt_vbi_cap               = ivtv_g_fmt_vbi_cap,
1934         .vidioc_g_fmt_sliced_vbi_cap        = ivtv_g_fmt_sliced_vbi_cap,
1935         .vidioc_g_fmt_vid_out               = ivtv_g_fmt_vid_out,
1936         .vidioc_g_fmt_vid_out_overlay       = ivtv_g_fmt_vid_out_overlay,
1937         .vidioc_g_fmt_sliced_vbi_out        = ivtv_g_fmt_sliced_vbi_out,
1938         .vidioc_s_fmt_vid_cap               = ivtv_s_fmt_vid_cap,
1939         .vidioc_s_fmt_vbi_cap               = ivtv_s_fmt_vbi_cap,
1940         .vidioc_s_fmt_sliced_vbi_cap        = ivtv_s_fmt_sliced_vbi_cap,
1941         .vidioc_s_fmt_vid_out               = ivtv_s_fmt_vid_out,
1942         .vidioc_s_fmt_vid_out_overlay       = ivtv_s_fmt_vid_out_overlay,
1943         .vidioc_s_fmt_sliced_vbi_out        = ivtv_s_fmt_sliced_vbi_out,
1944         .vidioc_try_fmt_vid_cap             = ivtv_try_fmt_vid_cap,
1945         .vidioc_try_fmt_vbi_cap             = ivtv_try_fmt_vbi_cap,
1946         .vidioc_try_fmt_sliced_vbi_cap      = ivtv_try_fmt_sliced_vbi_cap,
1947         .vidioc_try_fmt_vid_out             = ivtv_try_fmt_vid_out,
1948         .vidioc_try_fmt_vid_out_overlay     = ivtv_try_fmt_vid_out_overlay,
1949         .vidioc_try_fmt_sliced_vbi_out      = ivtv_try_fmt_sliced_vbi_out,
1950         .vidioc_g_sliced_vbi_cap            = ivtv_g_sliced_vbi_cap,
1951 #ifdef CONFIG_VIDEO_ADV_DEBUG
1952         .vidioc_g_register                  = ivtv_g_register,
1953         .vidioc_s_register                  = ivtv_s_register,
1954 #endif
1955         .vidioc_default                     = ivtv_default,
1956         .vidioc_subscribe_event             = ivtv_subscribe_event,
1957         .vidioc_unsubscribe_event           = v4l2_event_unsubscribe,
1958 };
1959 
1960 void ivtv_set_funcs(struct video_device *vdev)
1961 {
1962         vdev->ioctl_ops = &ivtv_ioctl_ops;
1963 }