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