Merge tag 'doc-4.11-images' of git://git.lwn.net/linux into drm-misc-next
[sfrench/cifs-2.6.git] / drivers / media / pci / ivtv / ivtv-ioctl.c
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
43 u16 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
59 static 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
65 static 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
93 void 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
106 static 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
117 u16 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
130 void 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
137 int 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
208 static 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
247 static 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
325 static 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
347 static 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
370 static 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
388 static 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
411 static 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
451 static 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
473 static 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
478 static 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
501 static 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
506 static 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
527 static 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
561 static 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
578 static 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
583 static 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
614 static 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
626 static 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
645 static 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
690 static 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
704 static 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
727 static 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
735 static 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
744 static 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
763 static 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
770 static 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
778 static 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
791 static 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
799 static 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
807 static 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
816 static 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
824 static 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
831 static 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
848 static 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
891 static 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
937 static 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
963 static 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
989 static 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
998 int 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
1041 static 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
1053 static 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
1075 static 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
1089 int 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
1106 static 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
1114 void 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
1133 void 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
1179 static 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
1207 static 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
1220 static 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
1236 static 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
1271 static 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
1297 static 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
1349 static 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
1379 static 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
1466 static 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
1487 static 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
1503 static 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         case V4L2_EVENT_CTRL:
1510                 return v4l2_event_subscribe(fh, sub, 0, &v4l2_ctrl_sub_ev_ops);
1511         default:
1512                 return -EINVAL;
1513         }
1514 }
1515
1516 static int ivtv_log_status(struct file *file, void *fh)
1517 {
1518         struct ivtv *itv = fh2id(fh)->itv;
1519         u32 data[CX2341X_MBOX_MAX_DATA];
1520
1521         int has_output = itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT;
1522         struct v4l2_input vidin;
1523         struct v4l2_audio audin;
1524         int i;
1525
1526         IVTV_INFO("Version: %s Card: %s\n", IVTV_VERSION, itv->card_name);
1527         if (itv->hw_flags & IVTV_HW_TVEEPROM) {
1528                 struct tveeprom tv;
1529
1530                 ivtv_read_eeprom(itv, &tv);
1531         }
1532         ivtv_call_all(itv, core, log_status);
1533         ivtv_get_input(itv, itv->active_input, &vidin);
1534         ivtv_get_audio_input(itv, itv->audio_input, &audin);
1535         IVTV_INFO("Video Input:  %s\n", vidin.name);
1536         IVTV_INFO("Audio Input:  %s%s\n", audin.name,
1537                 itv->dualwatch_stereo_mode == V4L2_MPEG_AUDIO_MODE_DUAL ?
1538                         " (Bilingual)" : "");
1539         if (has_output) {
1540                 struct v4l2_output vidout;
1541                 struct v4l2_audioout audout;
1542                 int mode = itv->output_mode;
1543                 static const char * const output_modes[5] = {
1544                         "None",
1545                         "MPEG Streaming",
1546                         "YUV Streaming",
1547                         "YUV Frames",
1548                         "Passthrough",
1549                 };
1550                 static const char * const alpha_mode[4] = {
1551                         "None",
1552                         "Global",
1553                         "Local",
1554                         "Global and Local"
1555                 };
1556                 static const char * const pixel_format[16] = {
1557                         "ARGB Indexed",
1558                         "RGB 5:6:5",
1559                         "ARGB 1:5:5:5",
1560                         "ARGB 1:4:4:4",
1561                         "ARGB 8:8:8:8",
1562                         "5",
1563                         "6",
1564                         "7",
1565                         "AYUV Indexed",
1566                         "YUV 5:6:5",
1567                         "AYUV 1:5:5:5",
1568                         "AYUV 1:4:4:4",
1569                         "AYUV 8:8:8:8",
1570                         "13",
1571                         "14",
1572                         "15",
1573                 };
1574
1575                 ivtv_get_output(itv, itv->active_output, &vidout);
1576                 ivtv_get_audio_output(itv, 0, &audout);
1577                 IVTV_INFO("Video Output: %s\n", vidout.name);
1578                 if (mode < 0 || mode > OUT_PASSTHROUGH)
1579                         mode = OUT_NONE;
1580                 IVTV_INFO("Output Mode:  %s\n", output_modes[mode]);
1581                 ivtv_vapi_result(itv, data, CX2341X_OSD_GET_STATE, 0);
1582                 data[0] |= (read_reg(0x2a00) >> 7) & 0x40;
1583                 IVTV_INFO("Overlay:      %s, Alpha: %s, Pixel Format: %s\n",
1584                         data[0] & 1 ? "On" : "Off",
1585                         alpha_mode[(data[0] >> 1) & 0x3],
1586                         pixel_format[(data[0] >> 3) & 0xf]);
1587         }
1588         IVTV_INFO("Tuner:  %s\n",
1589                 test_bit(IVTV_F_I_RADIO_USER, &itv->i_flags) ? "Radio" : "TV");
1590         v4l2_ctrl_handler_log_status(&itv->cxhdl.hdl, itv->v4l2_dev.name);
1591         IVTV_INFO("Status flags:    0x%08lx\n", itv->i_flags);
1592         for (i = 0; i < IVTV_MAX_STREAMS; i++) {
1593                 struct ivtv_stream *s = &itv->streams[i];
1594
1595                 if (s->vdev.v4l2_dev == NULL || s->buffers == 0)
1596                         continue;
1597                 IVTV_INFO("Stream %s: status 0x%04lx, %d%% of %d KiB (%d buffers) in use\n", s->name, s->s_flags,
1598                                 (s->buffers - s->q_free.buffers) * 100 / s->buffers,
1599                                 (s->buffers * s->buf_size) / 1024, s->buffers);
1600         }
1601
1602         IVTV_INFO("Read MPG/VBI: %lld/%lld bytes\n",
1603                         (long long)itv->mpg_data_received,
1604                         (long long)itv->vbi_data_inserted);
1605         return 0;
1606 }
1607
1608 static int ivtv_decoder_cmd(struct file *file, void *fh, struct v4l2_decoder_cmd *dec)
1609 {
1610         struct ivtv_open_id *id = fh2id(file->private_data);
1611         struct ivtv *itv = id->itv;
1612
1613         IVTV_DEBUG_IOCTL("VIDIOC_DECODER_CMD %d\n", dec->cmd);
1614         return ivtv_video_command(itv, id, dec, false);
1615 }
1616
1617 static int ivtv_try_decoder_cmd(struct file *file, void *fh, struct v4l2_decoder_cmd *dec)
1618 {
1619         struct ivtv_open_id *id = fh2id(file->private_data);
1620         struct ivtv *itv = id->itv;
1621
1622         IVTV_DEBUG_IOCTL("VIDIOC_TRY_DECODER_CMD %d\n", dec->cmd);
1623         return ivtv_video_command(itv, id, dec, true);
1624 }
1625
1626 #ifdef CONFIG_VIDEO_IVTV_DEPRECATED_IOCTLS
1627 static __inline__ void warn_deprecated_ioctl(const char *name)
1628 {
1629         pr_warn_once("warning: the %s ioctl is deprecated. Don't use it, as it will be removed soon\n",
1630                      name);
1631 }
1632 #endif
1633
1634 static int ivtv_decoder_ioctls(struct file *filp, unsigned int cmd, void *arg)
1635 {
1636         struct ivtv_open_id *id = fh2id(filp->private_data);
1637         struct ivtv *itv = id->itv;
1638         struct ivtv_stream *s = &itv->streams[id->type];
1639 #ifdef CONFIG_VIDEO_IVTV_DEPRECATED_IOCTLS
1640         int nonblocking = filp->f_flags & O_NONBLOCK;
1641         unsigned long iarg = (unsigned long)arg;
1642 #endif
1643
1644         switch (cmd) {
1645         case IVTV_IOC_DMA_FRAME: {
1646                 struct ivtv_dma_frame *args = arg;
1647
1648                 IVTV_DEBUG_IOCTL("IVTV_IOC_DMA_FRAME\n");
1649                 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
1650                         return -EINVAL;
1651                 if (args->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
1652                         return -EINVAL;
1653                 if (itv->output_mode == OUT_UDMA_YUV && args->y_source == NULL)
1654                         return 0;
1655                 if (ivtv_start_decoding(id, id->type)) {
1656                         return -EBUSY;
1657                 }
1658                 if (ivtv_set_output_mode(itv, OUT_UDMA_YUV) != OUT_UDMA_YUV) {
1659                         ivtv_release_stream(s);
1660                         return -EBUSY;
1661                 }
1662                 /* Mark that this file handle started the UDMA_YUV mode */
1663                 id->yuv_frames = 1;
1664                 if (args->y_source == NULL)
1665                         return 0;
1666                 return ivtv_yuv_prep_frame(itv, args);
1667         }
1668
1669         case IVTV_IOC_PASSTHROUGH_MODE:
1670                 IVTV_DEBUG_IOCTL("IVTV_IOC_PASSTHROUGH_MODE\n");
1671                 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
1672                         return -EINVAL;
1673                 return ivtv_passthrough_mode(itv, *(int *)arg != 0);
1674 #ifdef CONFIG_VIDEO_IVTV_DEPRECATED_IOCTLS
1675         case VIDEO_GET_PTS: {
1676                 s64 *pts = arg;
1677                 s64 frame;
1678
1679                 warn_deprecated_ioctl("VIDEO_GET_PTS");
1680                 if (s->type < IVTV_DEC_STREAM_TYPE_MPG) {
1681                         *pts = s->dma_pts;
1682                         break;
1683                 }
1684                 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
1685                         return -EINVAL;
1686                 return ivtv_g_pts_frame(itv, pts, &frame);
1687         }
1688
1689         case VIDEO_GET_FRAME_COUNT: {
1690                 s64 *frame = arg;
1691                 s64 pts;
1692
1693                 warn_deprecated_ioctl("VIDEO_GET_FRAME_COUNT");
1694                 if (s->type < IVTV_DEC_STREAM_TYPE_MPG) {
1695                         *frame = 0;
1696                         break;
1697                 }
1698                 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
1699                         return -EINVAL;
1700                 return ivtv_g_pts_frame(itv, &pts, frame);
1701         }
1702
1703         case VIDEO_PLAY: {
1704                 struct v4l2_decoder_cmd dc;
1705
1706                 warn_deprecated_ioctl("VIDEO_PLAY");
1707                 memset(&dc, 0, sizeof(dc));
1708                 dc.cmd = V4L2_DEC_CMD_START;
1709                 return ivtv_video_command(itv, id, &dc, 0);
1710         }
1711
1712         case VIDEO_STOP: {
1713                 struct v4l2_decoder_cmd dc;
1714
1715                 warn_deprecated_ioctl("VIDEO_STOP");
1716                 memset(&dc, 0, sizeof(dc));
1717                 dc.cmd = V4L2_DEC_CMD_STOP;
1718                 dc.flags = V4L2_DEC_CMD_STOP_TO_BLACK | V4L2_DEC_CMD_STOP_IMMEDIATELY;
1719                 return ivtv_video_command(itv, id, &dc, 0);
1720         }
1721
1722         case VIDEO_FREEZE: {
1723                 struct v4l2_decoder_cmd dc;
1724
1725                 warn_deprecated_ioctl("VIDEO_FREEZE");
1726                 memset(&dc, 0, sizeof(dc));
1727                 dc.cmd = V4L2_DEC_CMD_PAUSE;
1728                 return ivtv_video_command(itv, id, &dc, 0);
1729         }
1730
1731         case VIDEO_CONTINUE: {
1732                 struct v4l2_decoder_cmd dc;
1733
1734                 warn_deprecated_ioctl("VIDEO_CONTINUE");
1735                 memset(&dc, 0, sizeof(dc));
1736                 dc.cmd = V4L2_DEC_CMD_RESUME;
1737                 return ivtv_video_command(itv, id, &dc, 0);
1738         }
1739
1740         case VIDEO_COMMAND:
1741         case VIDEO_TRY_COMMAND: {
1742                 /* Note: struct v4l2_decoder_cmd has the same layout as
1743                    struct video_command */
1744                 struct v4l2_decoder_cmd *dc = arg;
1745                 int try = (cmd == VIDEO_TRY_COMMAND);
1746
1747                 if (try)
1748                         warn_deprecated_ioctl("VIDEO_TRY_COMMAND");
1749                 else
1750                         warn_deprecated_ioctl("VIDEO_COMMAND");
1751                 return ivtv_video_command(itv, id, dc, try);
1752         }
1753
1754         case VIDEO_GET_EVENT: {
1755                 struct video_event *ev = arg;
1756                 DEFINE_WAIT(wait);
1757
1758                 warn_deprecated_ioctl("VIDEO_GET_EVENT");
1759                 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
1760                         return -EINVAL;
1761                 memset(ev, 0, sizeof(*ev));
1762                 set_bit(IVTV_F_I_EV_VSYNC_ENABLED, &itv->i_flags);
1763
1764                 while (1) {
1765                         if (test_and_clear_bit(IVTV_F_I_EV_DEC_STOPPED, &itv->i_flags))
1766                                 ev->type = VIDEO_EVENT_DECODER_STOPPED;
1767                         else if (test_and_clear_bit(IVTV_F_I_EV_VSYNC, &itv->i_flags)) {
1768                                 ev->type = VIDEO_EVENT_VSYNC;
1769                                 ev->u.vsync_field = test_bit(IVTV_F_I_EV_VSYNC_FIELD, &itv->i_flags) ?
1770                                         VIDEO_VSYNC_FIELD_ODD : VIDEO_VSYNC_FIELD_EVEN;
1771                                 if (itv->output_mode == OUT_UDMA_YUV &&
1772                                         (itv->yuv_info.lace_mode & IVTV_YUV_MODE_MASK) ==
1773                                                                 IVTV_YUV_MODE_PROGRESSIVE) {
1774                                         ev->u.vsync_field = VIDEO_VSYNC_FIELD_PROGRESSIVE;
1775                                 }
1776                         }
1777                         if (ev->type)
1778                                 return 0;
1779                         if (nonblocking)
1780                                 return -EAGAIN;
1781                         /* Wait for event. Note that serialize_lock is locked,
1782                            so to allow other processes to access the driver while
1783                            we are waiting unlock first and later lock again. */
1784                         mutex_unlock(&itv->serialize_lock);
1785                         prepare_to_wait(&itv->event_waitq, &wait, TASK_INTERRUPTIBLE);
1786                         if (!test_bit(IVTV_F_I_EV_DEC_STOPPED, &itv->i_flags) &&
1787                             !test_bit(IVTV_F_I_EV_VSYNC, &itv->i_flags))
1788                                 schedule();
1789                         finish_wait(&itv->event_waitq, &wait);
1790                         mutex_lock(&itv->serialize_lock);
1791                         if (signal_pending(current)) {
1792                                 /* return if a signal was received */
1793                                 IVTV_DEBUG_INFO("User stopped wait for event\n");
1794                                 return -EINTR;
1795                         }
1796                 }
1797                 break;
1798         }
1799
1800         case VIDEO_SELECT_SOURCE:
1801                 warn_deprecated_ioctl("VIDEO_SELECT_SOURCE");
1802                 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
1803                         return -EINVAL;
1804                 return ivtv_passthrough_mode(itv, iarg == VIDEO_SOURCE_DEMUX);
1805
1806         case AUDIO_SET_MUTE:
1807                 warn_deprecated_ioctl("AUDIO_SET_MUTE");
1808                 itv->speed_mute_audio = iarg;
1809                 return 0;
1810
1811         case AUDIO_CHANNEL_SELECT:
1812                 warn_deprecated_ioctl("AUDIO_CHANNEL_SELECT");
1813                 if (iarg > AUDIO_STEREO_SWAPPED)
1814                         return -EINVAL;
1815                 return v4l2_ctrl_s_ctrl(itv->ctrl_audio_playback, iarg + 1);
1816
1817         case AUDIO_BILINGUAL_CHANNEL_SELECT:
1818                 warn_deprecated_ioctl("AUDIO_BILINGUAL_CHANNEL_SELECT");
1819                 if (iarg > AUDIO_STEREO_SWAPPED)
1820                         return -EINVAL;
1821                 return v4l2_ctrl_s_ctrl(itv->ctrl_audio_multilingual_playback, iarg + 1);
1822 #endif
1823         default:
1824                 return -EINVAL;
1825         }
1826         return 0;
1827 }
1828
1829 static long ivtv_default(struct file *file, void *fh, bool valid_prio,
1830                          unsigned int cmd, void *arg)
1831 {
1832         struct ivtv *itv = fh2id(fh)->itv;
1833
1834         if (!valid_prio) {
1835                 switch (cmd) {
1836                 case IVTV_IOC_PASSTHROUGH_MODE:
1837 #ifdef CONFIG_VIDEO_IVTV_DEPRECATED_IOCTLS
1838                 case VIDEO_PLAY:
1839                 case VIDEO_STOP:
1840                 case VIDEO_FREEZE:
1841                 case VIDEO_CONTINUE:
1842                 case VIDEO_COMMAND:
1843                 case VIDEO_SELECT_SOURCE:
1844                 case AUDIO_SET_MUTE:
1845                 case AUDIO_CHANNEL_SELECT:
1846                 case AUDIO_BILINGUAL_CHANNEL_SELECT:
1847 #endif
1848                         return -EBUSY;
1849                 }
1850         }
1851
1852         switch (cmd) {
1853         case VIDIOC_INT_RESET: {
1854                 u32 val = *(u32 *)arg;
1855
1856                 if ((val == 0 && itv->options.newi2c) || (val & 0x01))
1857                         ivtv_reset_ir_gpio(itv);
1858                 if (val & 0x02)
1859                         v4l2_subdev_call(itv->sd_video, core, reset, 0);
1860                 break;
1861         }
1862
1863         case IVTV_IOC_DMA_FRAME:
1864         case IVTV_IOC_PASSTHROUGH_MODE:
1865 #ifdef CONFIG_VIDEO_IVTV_DEPRECATED_IOCTLS
1866         case VIDEO_GET_PTS:
1867         case VIDEO_GET_FRAME_COUNT:
1868         case VIDEO_GET_EVENT:
1869         case VIDEO_PLAY:
1870         case VIDEO_STOP:
1871         case VIDEO_FREEZE:
1872         case VIDEO_CONTINUE:
1873         case VIDEO_COMMAND:
1874         case VIDEO_TRY_COMMAND:
1875         case VIDEO_SELECT_SOURCE:
1876         case AUDIO_SET_MUTE:
1877         case AUDIO_CHANNEL_SELECT:
1878         case AUDIO_BILINGUAL_CHANNEL_SELECT:
1879 #endif
1880                 return ivtv_decoder_ioctls(file, cmd, (void *)arg);
1881
1882         default:
1883                 return -ENOTTY;
1884         }
1885         return 0;
1886 }
1887
1888 static const struct v4l2_ioctl_ops ivtv_ioctl_ops = {
1889         .vidioc_querycap                    = ivtv_querycap,
1890         .vidioc_s_audio                     = ivtv_s_audio,
1891         .vidioc_g_audio                     = ivtv_g_audio,
1892         .vidioc_enumaudio                   = ivtv_enumaudio,
1893         .vidioc_s_audout                    = ivtv_s_audout,
1894         .vidioc_g_audout                    = ivtv_g_audout,
1895         .vidioc_enum_input                  = ivtv_enum_input,
1896         .vidioc_enum_output                 = ivtv_enum_output,
1897         .vidioc_enumaudout                  = ivtv_enumaudout,
1898         .vidioc_cropcap                     = ivtv_cropcap,
1899         .vidioc_s_selection                 = ivtv_s_selection,
1900         .vidioc_g_selection                 = ivtv_g_selection,
1901         .vidioc_g_input                     = ivtv_g_input,
1902         .vidioc_s_input                     = ivtv_s_input,
1903         .vidioc_g_output                    = ivtv_g_output,
1904         .vidioc_s_output                    = ivtv_s_output,
1905         .vidioc_g_frequency                 = ivtv_g_frequency,
1906         .vidioc_s_frequency                 = ivtv_s_frequency,
1907         .vidioc_s_tuner                     = ivtv_s_tuner,
1908         .vidioc_g_tuner                     = ivtv_g_tuner,
1909         .vidioc_g_enc_index                 = ivtv_g_enc_index,
1910         .vidioc_g_fbuf                      = ivtv_g_fbuf,
1911         .vidioc_s_fbuf                      = ivtv_s_fbuf,
1912         .vidioc_g_std                       = ivtv_g_std,
1913         .vidioc_s_std                       = ivtv_s_std,
1914         .vidioc_overlay                     = ivtv_overlay,
1915         .vidioc_log_status                  = ivtv_log_status,
1916         .vidioc_enum_fmt_vid_cap            = ivtv_enum_fmt_vid_cap,
1917         .vidioc_encoder_cmd                 = ivtv_encoder_cmd,
1918         .vidioc_try_encoder_cmd             = ivtv_try_encoder_cmd,
1919         .vidioc_decoder_cmd                 = ivtv_decoder_cmd,
1920         .vidioc_try_decoder_cmd             = ivtv_try_decoder_cmd,
1921         .vidioc_enum_fmt_vid_out            = ivtv_enum_fmt_vid_out,
1922         .vidioc_g_fmt_vid_cap               = ivtv_g_fmt_vid_cap,
1923         .vidioc_g_fmt_vbi_cap               = ivtv_g_fmt_vbi_cap,
1924         .vidioc_g_fmt_sliced_vbi_cap        = ivtv_g_fmt_sliced_vbi_cap,
1925         .vidioc_g_fmt_vid_out               = ivtv_g_fmt_vid_out,
1926         .vidioc_g_fmt_vid_out_overlay       = ivtv_g_fmt_vid_out_overlay,
1927         .vidioc_g_fmt_sliced_vbi_out        = ivtv_g_fmt_sliced_vbi_out,
1928         .vidioc_s_fmt_vid_cap               = ivtv_s_fmt_vid_cap,
1929         .vidioc_s_fmt_vbi_cap               = ivtv_s_fmt_vbi_cap,
1930         .vidioc_s_fmt_sliced_vbi_cap        = ivtv_s_fmt_sliced_vbi_cap,
1931         .vidioc_s_fmt_vid_out               = ivtv_s_fmt_vid_out,
1932         .vidioc_s_fmt_vid_out_overlay       = ivtv_s_fmt_vid_out_overlay,
1933         .vidioc_s_fmt_sliced_vbi_out        = ivtv_s_fmt_sliced_vbi_out,
1934         .vidioc_try_fmt_vid_cap             = ivtv_try_fmt_vid_cap,
1935         .vidioc_try_fmt_vbi_cap             = ivtv_try_fmt_vbi_cap,
1936         .vidioc_try_fmt_sliced_vbi_cap      = ivtv_try_fmt_sliced_vbi_cap,
1937         .vidioc_try_fmt_vid_out             = ivtv_try_fmt_vid_out,
1938         .vidioc_try_fmt_vid_out_overlay     = ivtv_try_fmt_vid_out_overlay,
1939         .vidioc_try_fmt_sliced_vbi_out      = ivtv_try_fmt_sliced_vbi_out,
1940         .vidioc_g_sliced_vbi_cap            = ivtv_g_sliced_vbi_cap,
1941 #ifdef CONFIG_VIDEO_ADV_DEBUG
1942         .vidioc_g_register                  = ivtv_g_register,
1943         .vidioc_s_register                  = ivtv_s_register,
1944 #endif
1945         .vidioc_default                     = ivtv_default,
1946         .vidioc_subscribe_event             = ivtv_subscribe_event,
1947         .vidioc_unsubscribe_event           = v4l2_event_unsubscribe,
1948 };
1949
1950 void ivtv_set_funcs(struct video_device *vdev)
1951 {
1952         vdev->ioctl_ops = &ivtv_ioctl_ops;
1953 }