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