Revert "[PATCH] paravirt: Add startup infrastructure for paravirtualization"
[pv_ops_mirror.git] / drivers / media / video / ivtv / ivtv-ioctl.c
blob794a6a02f82f16fc6b82719246fde6d0773b767e
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>
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.
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.
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
21 #include "ivtv-driver.h"
22 #include "ivtv-version.h"
23 #include "ivtv-mailbox.h"
24 #include "ivtv-i2c.h"
25 #include "ivtv-queue.h"
26 #include "ivtv-fileops.h"
27 #include "ivtv-vbi.h"
28 #include "ivtv-audio.h"
29 #include "ivtv-video.h"
30 #include "ivtv-streams.h"
31 #include "ivtv-yuv.h"
32 #include "ivtv-ioctl.h"
33 #include "ivtv-gpio.h"
34 #include "ivtv-controls.h"
35 #include "ivtv-cards.h"
36 #include <media/saa7127.h>
37 #include <media/tveeprom.h>
38 #include <media/v4l2-chip-ident.h>
39 #include <linux/dvb/audio.h>
40 #include <linux/i2c-id.h>
42 u16 service2vbi(int type)
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;
58 static int valid_service_line(int field, int line, int is_pal)
60 return (is_pal && line >= 6 && (line != 23 || field == 0)) ||
61 (!is_pal && line >= 10 && line < 22);
64 static u16 select_service_from_set(int field, int line, u16 set, int is_pal)
66 u16 valid_set = (is_pal ? V4L2_SLICED_VBI_625 : V4L2_SLICED_VBI_525);
67 int i;
69 set = set & valid_set;
70 if (set == 0 || !valid_service_line(field, line, is_pal)) {
71 return 0;
73 if (!is_pal) {
74 if (line == 21 && (set & V4L2_SLICED_CAPTION_525))
75 return V4L2_SLICED_CAPTION_525;
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;
85 for (i = 0; i < 32; i++) {
86 if ((1 << i) & set)
87 return 1 << i;
89 return 0;
92 void expand_service_set(struct v4l2_sliced_vbi_format *fmt, int is_pal)
94 u16 set = fmt->service_set;
95 int f, l;
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);
105 static int check_service_set(struct v4l2_sliced_vbi_format *fmt, int is_pal)
107 int f, l;
108 u16 set = 0;
110 for (f = 0; f < 2; f++) {
111 for (l = 0; l < 24; l++) {
112 fmt->service_lines[f][l] = select_service_from_set(f, l, fmt->service_lines[f][l], is_pal);
113 set |= fmt->service_lines[f][l];
116 return set != 0;
119 u16 get_service_set(struct v4l2_sliced_vbi_format *fmt)
121 int f, l;
122 u16 set = 0;
124 for (f = 0; f < 2; f++) {
125 for (l = 0; l < 24; l++) {
126 set |= fmt->service_lines[f][l];
129 return set;
132 static const struct {
133 v4l2_std_id std;
134 char *name;
135 } enum_stds[] = {
136 { V4L2_STD_PAL_BG | V4L2_STD_PAL_H, "PAL-BGH" },
137 { V4L2_STD_PAL_DK, "PAL-DK" },
138 { V4L2_STD_PAL_I, "PAL-I" },
139 { V4L2_STD_PAL_M, "PAL-M" },
140 { V4L2_STD_PAL_N, "PAL-N" },
141 { V4L2_STD_PAL_Nc, "PAL-Nc" },
142 { V4L2_STD_SECAM_B | V4L2_STD_SECAM_G | V4L2_STD_SECAM_H, "SECAM-BGH" },
143 { V4L2_STD_SECAM_DK, "SECAM-DK" },
144 { V4L2_STD_SECAM_L, "SECAM-L" },
145 { V4L2_STD_SECAM_LC, "SECAM-L'" },
146 { V4L2_STD_NTSC_M, "NTSC-M" },
147 { V4L2_STD_NTSC_M_JP, "NTSC-J" },
148 { V4L2_STD_NTSC_M_KR, "NTSC-K" },
151 static const struct v4l2_standard ivtv_std_60hz =
153 .frameperiod = {.numerator = 1001, .denominator = 30000},
154 .framelines = 525,
157 static const struct v4l2_standard ivtv_std_50hz =
159 .frameperiod = {.numerator = 1, .denominator = 25},
160 .framelines = 625,
163 void ivtv_set_osd_alpha(struct ivtv *itv)
165 ivtv_vapi(itv, CX2341X_OSD_SET_GLOBAL_ALPHA, 3,
166 itv->osd_global_alpha_state, itv->osd_global_alpha, !itv->osd_local_alpha_state);
167 ivtv_vapi(itv, CX2341X_OSD_SET_CHROMA_KEY, 2, itv->osd_color_key_state, itv->osd_color_key);
170 int ivtv_set_speed(struct ivtv *itv, int speed)
172 u32 data[CX2341X_MBOX_MAX_DATA];
173 struct ivtv_stream *s;
174 int single_step = (speed == 1 || speed == -1);
175 DEFINE_WAIT(wait);
177 if (speed == 0) speed = 1000;
179 /* No change? */
180 if (speed == itv->speed && !single_step)
181 return 0;
183 s = &itv->streams[IVTV_DEC_STREAM_TYPE_MPG];
185 if (single_step && (speed < 0) == (itv->speed < 0)) {
186 /* Single step video and no need to change direction */
187 ivtv_vapi(itv, CX2341X_DEC_STEP_VIDEO, 1, 0);
188 itv->speed = speed;
189 return 0;
191 if (single_step)
192 /* Need to change direction */
193 speed = speed < 0 ? -1000 : 1000;
195 data[0] = (speed > 1000 || speed < -1000) ? 0x80000000 : 0;
196 data[0] |= (speed > 1000 || speed < -1500) ? 0x40000000 : 0;
197 data[1] = (speed < 0);
198 data[2] = speed < 0 ? 3 : 7;
199 data[3] = itv->params.video_b_frames;
200 data[4] = (speed == 1500 || speed == 500) ? itv->speed_mute_audio : 0;
201 data[5] = 0;
202 data[6] = 0;
204 if (speed == 1500 || speed == -1500) data[0] |= 1;
205 else if (speed == 2000 || speed == -2000) data[0] |= 2;
206 else if (speed > -1000 && speed < 0) data[0] |= (-1000 / speed);
207 else if (speed < 1000 && speed > 0) data[0] |= (1000 / speed);
209 /* If not decoding, just change speed setting */
210 if (atomic_read(&itv->decoding) > 0) {
211 int got_sig = 0;
213 /* Stop all DMA and decoding activity */
214 ivtv_vapi(itv, CX2341X_DEC_PAUSE_PLAYBACK, 1, 0);
216 /* Wait for any DMA to finish */
217 prepare_to_wait(&itv->dma_waitq, &wait, TASK_INTERRUPTIBLE);
218 while (itv->i_flags & IVTV_F_I_DMA) {
219 got_sig = signal_pending(current);
220 if (got_sig)
221 break;
222 got_sig = 0;
223 schedule();
225 finish_wait(&itv->dma_waitq, &wait);
226 if (got_sig)
227 return -EINTR;
229 /* Change Speed safely */
230 ivtv_api(itv, CX2341X_DEC_SET_PLAYBACK_SPEED, 7, data);
231 IVTV_DEBUG_INFO("Setting Speed to 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x\n",
232 data[0], data[1], data[2], data[3], data[4], data[5], data[6]);
234 if (single_step) {
235 speed = (speed < 0) ? -1 : 1;
236 ivtv_vapi(itv, CX2341X_DEC_STEP_VIDEO, 1, 0);
238 itv->speed = speed;
239 return 0;
242 static int ivtv_validate_speed(int cur_speed, int new_speed)
244 int fact = new_speed < 0 ? -1 : 1;
245 int s;
247 if (new_speed < 0) new_speed = -new_speed;
248 if (cur_speed < 0) cur_speed = -cur_speed;
250 if (cur_speed <= new_speed) {
251 if (new_speed > 1500) return fact * 2000;
252 if (new_speed > 1000) return fact * 1500;
254 else {
255 if (new_speed >= 2000) return fact * 2000;
256 if (new_speed >= 1500) return fact * 1500;
257 if (new_speed >= 1000) return fact * 1000;
259 if (new_speed == 0) return 1000;
260 if (new_speed == 1 || new_speed == 1000) return fact * new_speed;
262 s = new_speed;
263 new_speed = 1000 / new_speed;
264 if (1000 / cur_speed == new_speed)
265 new_speed += (cur_speed < s) ? -1 : 1;
266 if (new_speed > 60) return 1000 / (fact * 60);
267 return 1000 / (fact * new_speed);
270 static int ivtv_video_command(struct ivtv *itv, struct ivtv_open_id *id,
271 struct video_command *vc, int try)
273 struct ivtv_stream *s = &itv->streams[IVTV_DEC_STREAM_TYPE_MPG];
275 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
276 return -EINVAL;
278 switch (vc->cmd) {
279 case VIDEO_CMD_PLAY: {
280 vc->flags = 0;
281 vc->play.speed = ivtv_validate_speed(itv->speed, vc->play.speed);
282 if (vc->play.speed < 0)
283 vc->play.format = VIDEO_PLAY_FMT_GOP;
284 if (try) break;
286 if (ivtv_set_output_mode(itv, OUT_MPG) != OUT_MPG)
287 return -EBUSY;
288 return ivtv_start_decoding(id, vc->play.speed);
291 case VIDEO_CMD_STOP:
292 vc->flags &= VIDEO_CMD_STOP_IMMEDIATELY|VIDEO_CMD_STOP_TO_BLACK;
293 if (vc->flags & VIDEO_CMD_STOP_IMMEDIATELY)
294 vc->stop.pts = 0;
295 if (try) break;
296 if (atomic_read(&itv->decoding) == 0)
297 return 0;
298 if (itv->output_mode != OUT_MPG)
299 return -EBUSY;
301 itv->output_mode = OUT_NONE;
302 return ivtv_stop_v4l2_decode_stream(s, vc->flags, vc->stop.pts);
304 case VIDEO_CMD_FREEZE:
305 vc->flags &= VIDEO_CMD_FREEZE_TO_BLACK;
306 if (try) break;
307 if (itv->output_mode != OUT_MPG)
308 return -EBUSY;
309 if (atomic_read(&itv->decoding) > 0) {
310 ivtv_vapi(itv, CX2341X_DEC_PAUSE_PLAYBACK, 1,
311 (vc->flags & VIDEO_CMD_FREEZE_TO_BLACK) ? 1 : 0);
313 break;
315 case VIDEO_CMD_CONTINUE:
316 vc->flags = 0;
317 if (try) break;
318 if (itv->output_mode != OUT_MPG)
319 return -EBUSY;
320 if (atomic_read(&itv->decoding) > 0) {
321 ivtv_vapi(itv, CX2341X_DEC_START_PLAYBACK, 2, 0, 0);
323 break;
325 default:
326 return -EINVAL;
328 return 0;
331 static int ivtv_itvc(struct ivtv *itv, unsigned int cmd, void *arg)
333 struct v4l2_register *regs = arg;
334 unsigned long flags;
335 volatile u8 __iomem *reg_start;
337 if (!capable(CAP_SYS_ADMIN))
338 return -EPERM;
339 if (regs->reg >= IVTV_REG_OFFSET && regs->reg < IVTV_REG_OFFSET + IVTV_REG_SIZE)
340 reg_start = itv->reg_mem - IVTV_REG_OFFSET;
341 else if (itv->has_cx23415 && regs->reg >= IVTV_DECODER_OFFSET &&
342 regs->reg < IVTV_DECODER_OFFSET + IVTV_DECODER_SIZE)
343 reg_start = itv->dec_mem - IVTV_DECODER_OFFSET;
344 else if (regs->reg >= 0 && regs->reg < IVTV_ENCODER_SIZE)
345 reg_start = itv->enc_mem;
346 else
347 return -EINVAL;
349 spin_lock_irqsave(&ivtv_cards_lock, flags);
350 if (cmd == VIDIOC_DBG_G_REGISTER) {
351 regs->val = readl(regs->reg + reg_start);
352 } else {
353 writel(regs->val, regs->reg + reg_start);
355 spin_unlock_irqrestore(&ivtv_cards_lock, flags);
356 return 0;
359 static int ivtv_get_fmt(struct ivtv *itv, int streamtype, struct v4l2_format *fmt)
361 switch (fmt->type) {
362 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
363 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
364 return -EINVAL;
365 fmt->fmt.pix.left = itv->main_rect.left;
366 fmt->fmt.pix.top = itv->main_rect.top;
367 fmt->fmt.pix.width = itv->main_rect.width;
368 fmt->fmt.pix.height = itv->main_rect.height;
369 fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
370 fmt->fmt.pix.field = V4L2_FIELD_INTERLACED;
371 if (itv->output_mode == OUT_UDMA_YUV) {
372 switch (itv->yuv_info.lace_mode & IVTV_YUV_MODE_MASK) {
373 case IVTV_YUV_MODE_INTERLACED:
374 fmt->fmt.pix.field = (itv->yuv_info.lace_mode & IVTV_YUV_SYNC_MASK) ?
375 V4L2_FIELD_INTERLACED_BT : V4L2_FIELD_INTERLACED_TB;
376 break;
377 case IVTV_YUV_MODE_PROGRESSIVE:
378 fmt->fmt.pix.field = V4L2_FIELD_NONE;
379 break;
380 default:
381 fmt->fmt.pix.field = V4L2_FIELD_ANY;
382 break;
384 fmt->fmt.pix.pixelformat = V4L2_PIX_FMT_HM12;
385 /* YUV size is (Y=(h*w) + UV=(h*(w/2))) */
386 fmt->fmt.pix.sizeimage =
387 fmt->fmt.pix.height * fmt->fmt.pix.width +
388 fmt->fmt.pix.height * (fmt->fmt.pix.width / 2);
390 else if (itv->output_mode == OUT_YUV ||
391 streamtype == IVTV_ENC_STREAM_TYPE_YUV ||
392 streamtype == IVTV_DEC_STREAM_TYPE_YUV) {
393 fmt->fmt.pix.pixelformat = V4L2_PIX_FMT_HM12;
394 /* YUV size is (Y=(h*w) + UV=(h*(w/2))) */
395 fmt->fmt.pix.sizeimage =
396 fmt->fmt.pix.height * fmt->fmt.pix.width +
397 fmt->fmt.pix.height * (fmt->fmt.pix.width / 2);
398 } else {
399 fmt->fmt.pix.pixelformat = V4L2_PIX_FMT_MPEG;
400 fmt->fmt.pix.sizeimage = 128 * 1024;
402 break;
404 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
405 fmt->fmt.pix.left = 0;
406 fmt->fmt.pix.top = 0;
407 fmt->fmt.pix.width = itv->params.width;
408 fmt->fmt.pix.height = itv->params.height;
409 fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
410 fmt->fmt.pix.field = V4L2_FIELD_INTERLACED;
411 if (streamtype == IVTV_ENC_STREAM_TYPE_YUV ||
412 streamtype == IVTV_DEC_STREAM_TYPE_YUV) {
413 fmt->fmt.pix.pixelformat = V4L2_PIX_FMT_HM12;
414 /* YUV size is (Y=(h*w) + UV=(h*(w/2))) */
415 fmt->fmt.pix.sizeimage =
416 fmt->fmt.pix.height * fmt->fmt.pix.width +
417 fmt->fmt.pix.height * (fmt->fmt.pix.width / 2);
418 } else {
419 fmt->fmt.pix.pixelformat = V4L2_PIX_FMT_MPEG;
420 fmt->fmt.pix.sizeimage = 128 * 1024;
422 break;
424 case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
425 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
426 return -EINVAL;
427 fmt->fmt.win.chromakey = itv->osd_color_key;
428 fmt->fmt.win.global_alpha = itv->osd_global_alpha;
429 break;
431 case V4L2_BUF_TYPE_VBI_CAPTURE:
432 fmt->fmt.vbi.sampling_rate = 27000000;
433 fmt->fmt.vbi.offset = 248;
434 fmt->fmt.vbi.samples_per_line = itv->vbi.raw_decoder_line_size - 4;
435 fmt->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
436 fmt->fmt.vbi.start[0] = itv->vbi.start[0];
437 fmt->fmt.vbi.start[1] = itv->vbi.start[1];
438 fmt->fmt.vbi.count[0] = fmt->fmt.vbi.count[1] = itv->vbi.count;
439 break;
441 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
443 struct v4l2_sliced_vbi_format *vbifmt = &fmt->fmt.sliced;
445 if (!(itv->v4l2_cap & V4L2_CAP_SLICED_VBI_OUTPUT))
446 return -EINVAL;
447 vbifmt->io_size = sizeof(struct v4l2_sliced_vbi_data) * 36;
448 memset(vbifmt->reserved, 0, sizeof(vbifmt->reserved));
449 memset(vbifmt->service_lines, 0, sizeof(vbifmt->service_lines));
450 if (itv->is_60hz) {
451 vbifmt->service_lines[0][21] = V4L2_SLICED_CAPTION_525;
452 vbifmt->service_lines[1][21] = V4L2_SLICED_CAPTION_525;
453 } else {
454 vbifmt->service_lines[0][23] = V4L2_SLICED_WSS_625;
455 vbifmt->service_lines[0][16] = V4L2_SLICED_VPS;
457 vbifmt->service_set = get_service_set(vbifmt);
458 break;
461 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
463 struct v4l2_sliced_vbi_format *vbifmt = &fmt->fmt.sliced;
465 vbifmt->io_size = sizeof(struct v4l2_sliced_vbi_data) * 36;
466 memset(vbifmt->reserved, 0, sizeof(vbifmt->reserved));
467 memset(vbifmt->service_lines, 0, sizeof(vbifmt->service_lines));
469 if (streamtype == IVTV_DEC_STREAM_TYPE_VBI) {
470 vbifmt->service_set = itv->is_50hz ? V4L2_SLICED_VBI_625 :
471 V4L2_SLICED_VBI_525;
472 expand_service_set(vbifmt, itv->is_50hz);
473 break;
476 itv->video_dec_func(itv, VIDIOC_G_FMT, fmt);
477 vbifmt->service_set = get_service_set(vbifmt);
478 break;
480 case V4L2_BUF_TYPE_VBI_OUTPUT:
481 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
482 default:
483 return -EINVAL;
485 return 0;
488 static int ivtv_try_or_set_fmt(struct ivtv *itv, int streamtype,
489 struct v4l2_format *fmt, int set_fmt)
491 struct v4l2_sliced_vbi_format *vbifmt = &fmt->fmt.sliced;
492 u16 set;
494 if (fmt->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
495 struct v4l2_rect r;
496 int field;
498 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
499 return -EINVAL;
500 field = fmt->fmt.pix.field;
501 r.top = fmt->fmt.pix.top;
502 r.left = fmt->fmt.pix.left;
503 r.width = fmt->fmt.pix.width;
504 r.height = fmt->fmt.pix.height;
505 ivtv_get_fmt(itv, streamtype, fmt);
506 if (itv->output_mode != OUT_UDMA_YUV) {
507 /* TODO: would setting the rect also be valid for this mode? */
508 fmt->fmt.pix.top = r.top;
509 fmt->fmt.pix.left = r.left;
510 fmt->fmt.pix.width = r.width;
511 fmt->fmt.pix.height = r.height;
513 if (itv->output_mode == OUT_UDMA_YUV) {
514 /* TODO: add checks for validity */
515 fmt->fmt.pix.field = field;
517 if (set_fmt) {
518 if (itv->output_mode == OUT_UDMA_YUV) {
519 switch (field) {
520 case V4L2_FIELD_NONE:
521 itv->yuv_info.lace_mode = IVTV_YUV_MODE_PROGRESSIVE;
522 break;
523 case V4L2_FIELD_ANY:
524 itv->yuv_info.lace_mode = IVTV_YUV_MODE_AUTO;
525 break;
526 case V4L2_FIELD_INTERLACED_BT:
527 itv->yuv_info.lace_mode =
528 IVTV_YUV_MODE_INTERLACED|IVTV_YUV_SYNC_ODD;
529 break;
530 case V4L2_FIELD_INTERLACED_TB:
531 default:
532 itv->yuv_info.lace_mode = IVTV_YUV_MODE_INTERLACED;
533 break;
535 itv->yuv_info.lace_sync_field = (itv->yuv_info.lace_mode & IVTV_YUV_SYNC_MASK) == IVTV_YUV_SYNC_EVEN ? 0 : 1;
537 /* Force update of yuv registers */
538 itv->yuv_info.yuv_forced_update = 1;
539 return 0;
541 if (!ivtv_vapi(itv, CX2341X_OSD_SET_FRAMEBUFFER_WINDOW, 4,
542 r.width, r.height, r.left, r.top))
543 itv->main_rect = r;
544 else
545 return -EINVAL;
547 return 0;
550 if (fmt->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY) {
551 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
552 return -EINVAL;
553 if (set_fmt) {
554 itv->osd_color_key = fmt->fmt.win.chromakey;
555 itv->osd_global_alpha = fmt->fmt.win.global_alpha;
556 ivtv_set_osd_alpha(itv);
558 return 0;
561 /* set window size */
562 if (fmt->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
563 int w = fmt->fmt.pix.width;
564 int h = fmt->fmt.pix.height;
566 if (w > 720) w = 720;
567 else if (w < 1) w = 1;
568 if (h > (itv->is_50hz ? 576 : 480)) h = (itv->is_50hz ? 576 : 480);
569 else if (h < 2) h = 2;
570 ivtv_get_fmt(itv, streamtype, fmt);
571 fmt->fmt.pix.width = w;
572 fmt->fmt.pix.height = h;
574 if (!set_fmt || (itv->params.width == w && itv->params.height == h))
575 return 0;
576 if (atomic_read(&itv->capturing) > 0)
577 return -EBUSY;
579 itv->params.width = w;
580 itv->params.height = h;
581 if (w != 720 || h != (itv->is_50hz ? 576 : 480))
582 itv->params.video_temporal_filter = 0;
583 else
584 itv->params.video_temporal_filter = 8;
585 itv->video_dec_func(itv, VIDIOC_S_FMT, fmt);
586 return ivtv_get_fmt(itv, streamtype, fmt);
589 /* set raw VBI format */
590 if (fmt->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
591 if (set_fmt && streamtype == IVTV_ENC_STREAM_TYPE_VBI &&
592 itv->vbi.sliced_in->service_set &&
593 atomic_read(&itv->capturing) > 0) {
594 return -EBUSY;
596 if (set_fmt) {
597 itv->vbi.sliced_in->service_set = 0;
598 itv->video_dec_func(itv, VIDIOC_S_FMT, &itv->vbi.in);
600 return ivtv_get_fmt(itv, streamtype, fmt);
603 /* set sliced VBI output
604 In principle the user could request that only certain
605 VBI types are output and that the others are ignored.
606 I.e., suppress CC in the even fields or only output
607 WSS and no VPS. Currently though there is no choice. */
608 if (fmt->type == V4L2_BUF_TYPE_SLICED_VBI_OUTPUT)
609 return ivtv_get_fmt(itv, streamtype, fmt);
611 /* any else but sliced VBI capture is an error */
612 if (fmt->type != V4L2_BUF_TYPE_SLICED_VBI_CAPTURE)
613 return -EINVAL;
615 if (streamtype == IVTV_DEC_STREAM_TYPE_VBI)
616 return ivtv_get_fmt(itv, streamtype, fmt);
618 /* set sliced VBI capture format */
619 vbifmt->io_size = sizeof(struct v4l2_sliced_vbi_data) * 36;
620 memset(vbifmt->reserved, 0, sizeof(vbifmt->reserved));
622 if (vbifmt->service_set)
623 expand_service_set(vbifmt, itv->is_50hz);
624 set = check_service_set(vbifmt, itv->is_50hz);
625 vbifmt->service_set = get_service_set(vbifmt);
627 if (!set_fmt)
628 return 0;
629 if (set == 0)
630 return -EINVAL;
631 if (atomic_read(&itv->capturing) > 0 && itv->vbi.sliced_in->service_set == 0) {
632 return -EBUSY;
634 itv->video_dec_func(itv, VIDIOC_S_FMT, fmt);
635 memcpy(itv->vbi.sliced_in, vbifmt, sizeof(*itv->vbi.sliced_in));
636 return 0;
639 static int ivtv_debug_ioctls(struct file *filp, unsigned int cmd, void *arg)
641 struct ivtv_open_id *id = (struct ivtv_open_id *)filp->private_data;
642 struct ivtv *itv = id->itv;
643 struct v4l2_register *reg = arg;
645 switch (cmd) {
646 /* ioctls to allow direct access to the encoder registers for testing */
647 case VIDIOC_DBG_G_REGISTER:
648 if (v4l2_chip_match_host(reg->match_type, reg->match_chip))
649 return ivtv_itvc(itv, cmd, arg);
650 if (reg->match_type == V4L2_CHIP_MATCH_I2C_DRIVER)
651 return ivtv_i2c_id(itv, reg->match_chip, cmd, arg);
652 return ivtv_call_i2c_client(itv, reg->match_chip, cmd, arg);
654 case VIDIOC_DBG_S_REGISTER:
655 if (v4l2_chip_match_host(reg->match_type, reg->match_chip))
656 return ivtv_itvc(itv, cmd, arg);
657 if (reg->match_type == V4L2_CHIP_MATCH_I2C_DRIVER)
658 return ivtv_i2c_id(itv, reg->match_chip, cmd, arg);
659 return ivtv_call_i2c_client(itv, reg->match_chip, cmd, arg);
661 case VIDIOC_G_CHIP_IDENT: {
662 struct v4l2_chip_ident *chip = arg;
664 chip->ident = V4L2_IDENT_NONE;
665 chip->revision = 0;
666 if (reg->match_type == V4L2_CHIP_MATCH_HOST) {
667 if (v4l2_chip_match_host(reg->match_type, reg->match_chip)) {
668 struct v4l2_chip_ident *chip = arg;
670 chip->ident = itv->has_cx23415 ? V4L2_IDENT_CX23415 : V4L2_IDENT_CX23416;
672 return 0;
674 if (reg->match_type == V4L2_CHIP_MATCH_I2C_DRIVER)
675 return ivtv_i2c_id(itv, reg->match_chip, cmd, arg);
676 if (reg->match_type == V4L2_CHIP_MATCH_I2C_ADDR)
677 return ivtv_call_i2c_client(itv, reg->match_chip, cmd, arg);
678 return -EINVAL;
681 case VIDIOC_INT_S_AUDIO_ROUTING: {
682 struct v4l2_routing *route = arg;
684 ivtv_audio_set_route(itv, route);
685 break;
688 case VIDIOC_INT_RESET:
689 ivtv_reset_ir_gpio(itv);
690 break;
692 default:
693 return -EINVAL;
695 return 0;
698 int ivtv_v4l2_ioctls(struct ivtv *itv, struct file *filp, unsigned int cmd, void *arg)
700 struct ivtv_open_id *id = NULL;
702 if (filp) id = (struct ivtv_open_id *)filp->private_data;
704 switch (cmd) {
705 case VIDIOC_G_PRIORITY:
707 enum v4l2_priority *p = arg;
709 *p = v4l2_prio_max(&itv->prio);
710 break;
713 case VIDIOC_S_PRIORITY:
715 enum v4l2_priority *prio = arg;
717 return v4l2_prio_change(&itv->prio, &id->prio, *prio);
720 case VIDIOC_QUERYCAP:{
721 struct v4l2_capability *vcap = arg;
723 memset(vcap, 0, sizeof(*vcap));
724 strcpy(vcap->driver, IVTV_DRIVER_NAME); /* driver name */
725 strcpy(vcap->card, itv->card_name); /* card type */
726 strcpy(vcap->bus_info, pci_name(itv->dev)); /* bus info... */
727 vcap->version = IVTV_DRIVER_VERSION; /* version */
728 vcap->capabilities = itv->v4l2_cap; /* capabilities */
730 /* reserved.. must set to 0! */
731 vcap->reserved[0] = vcap->reserved[1] =
732 vcap->reserved[2] = vcap->reserved[3] = 0;
733 break;
736 case VIDIOC_ENUMAUDIO:{
737 struct v4l2_audio *vin = arg;
739 return ivtv_get_audio_input(itv, vin->index, vin);
742 case VIDIOC_G_AUDIO:{
743 struct v4l2_audio *vin = arg;
745 vin->index = itv->audio_input;
746 return ivtv_get_audio_input(itv, vin->index, vin);
749 case VIDIOC_S_AUDIO:{
750 struct v4l2_audio *vout = arg;
752 if (vout->index >= itv->nof_audio_inputs)
753 return -EINVAL;
754 itv->audio_input = vout->index;
755 ivtv_audio_set_io(itv);
756 break;
759 case VIDIOC_ENUMAUDOUT:{
760 struct v4l2_audioout *vin = arg;
762 /* set it to defaults from our table */
763 return ivtv_get_audio_output(itv, vin->index, vin);
766 case VIDIOC_G_AUDOUT:{
767 struct v4l2_audioout *vin = arg;
769 vin->index = 0;
770 return ivtv_get_audio_output(itv, vin->index, vin);
773 case VIDIOC_S_AUDOUT:{
774 struct v4l2_audioout *vout = arg;
776 return ivtv_get_audio_output(itv, vout->index, vout);
779 case VIDIOC_ENUMINPUT:{
780 struct v4l2_input *vin = arg;
782 /* set it to defaults from our table */
783 return ivtv_get_input(itv, vin->index, vin);
786 case VIDIOC_ENUMOUTPUT:{
787 struct v4l2_output *vout = arg;
789 return ivtv_get_output(itv, vout->index, vout);
792 case VIDIOC_TRY_FMT:
793 case VIDIOC_S_FMT: {
794 struct v4l2_format *fmt = arg;
796 return ivtv_try_or_set_fmt(itv, id->type, fmt, cmd == VIDIOC_S_FMT);
799 case VIDIOC_G_FMT: {
800 struct v4l2_format *fmt = arg;
801 int type = fmt->type;
803 memset(fmt, 0, sizeof(*fmt));
804 fmt->type = type;
805 return ivtv_get_fmt(itv, id->type, fmt);
808 case VIDIOC_S_CROP: {
809 struct v4l2_crop *crop = arg;
811 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
812 return -EINVAL;
813 return itv->video_dec_func(itv, VIDIOC_S_CROP, arg);
816 case VIDIOC_G_CROP: {
817 struct v4l2_crop *crop = arg;
819 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
820 return -EINVAL;
821 return itv->video_dec_func(itv, VIDIOC_G_CROP, arg);
824 case VIDIOC_ENUM_FMT: {
825 static struct v4l2_fmtdesc formats[] = {
826 { 0, 0, 0,
827 "HM12 (YUV 4:1:1)", V4L2_PIX_FMT_HM12,
828 { 0, 0, 0, 0 }
830 { 1, 0, V4L2_FMT_FLAG_COMPRESSED,
831 "MPEG", V4L2_PIX_FMT_MPEG,
832 { 0, 0, 0, 0 }
835 struct v4l2_fmtdesc *fmt = arg;
836 enum v4l2_buf_type type = fmt->type;
838 switch (type) {
839 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
840 break;
841 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
842 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
843 return -EINVAL;
844 break;
845 default:
846 return -EINVAL;
848 if (fmt->index > 1)
849 return -EINVAL;
850 *fmt = formats[fmt->index];
851 fmt->type = type;
852 return 0;
855 case VIDIOC_G_INPUT:{
856 *(int *)arg = itv->active_input;
857 break;
860 case VIDIOC_S_INPUT:{
861 int inp = *(int *)arg;
863 if (inp < 0 || inp >= itv->nof_inputs)
864 return -EINVAL;
866 if (inp == itv->active_input) {
867 IVTV_DEBUG_INFO("Input unchanged\n");
868 break;
870 IVTV_DEBUG_INFO("Changing input from %d to %d\n",
871 itv->active_input, inp);
873 itv->active_input = inp;
874 /* Set the audio input to whatever is appropriate for the
875 input type. */
876 itv->audio_input = itv->card->video_inputs[inp].audio_index;
878 /* prevent others from messing with the streams until
879 we're finished changing inputs. */
880 ivtv_mute(itv);
881 ivtv_video_set_io(itv);
882 ivtv_audio_set_io(itv);
883 ivtv_unmute(itv);
884 break;
887 case VIDIOC_G_OUTPUT:{
888 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
889 return -EINVAL;
890 *(int *)arg = itv->active_output;
891 break;
894 case VIDIOC_S_OUTPUT:{
895 int outp = *(int *)arg;
896 struct v4l2_routing route;
898 if (outp >= itv->card->nof_outputs)
899 return -EINVAL;
901 if (outp == itv->active_output) {
902 IVTV_DEBUG_INFO("Output unchanged\n");
903 break;
905 IVTV_DEBUG_INFO("Changing output from %d to %d\n",
906 itv->active_output, outp);
908 itv->active_output = outp;
909 route.input = SAA7127_INPUT_TYPE_NORMAL;
910 route.output = itv->card->video_outputs[outp].video_output;
911 ivtv_saa7127(itv, VIDIOC_INT_S_VIDEO_ROUTING, &route);
912 break;
915 case VIDIOC_G_FREQUENCY:{
916 struct v4l2_frequency *vf = arg;
918 if (vf->tuner != 0)
919 return -EINVAL;
920 ivtv_call_i2c_clients(itv, cmd, arg);
921 break;
924 case VIDIOC_S_FREQUENCY:{
925 struct v4l2_frequency vf = *(struct v4l2_frequency *)arg;
927 if (vf.tuner != 0)
928 return -EINVAL;
930 ivtv_mute(itv);
931 IVTV_DEBUG_INFO("v4l2 ioctl: set frequency %d\n", vf.frequency);
932 ivtv_call_i2c_clients(itv, cmd, &vf);
933 ivtv_unmute(itv);
934 break;
937 case VIDIOC_ENUMSTD:{
938 struct v4l2_standard *vs = arg;
939 int idx = vs->index;
941 if (idx < 0 || idx >= ARRAY_SIZE(enum_stds))
942 return -EINVAL;
944 *vs = (enum_stds[idx].std & V4L2_STD_525_60) ?
945 ivtv_std_60hz : ivtv_std_50hz;
946 vs->index = idx;
947 vs->id = enum_stds[idx].std;
948 strcpy(vs->name, enum_stds[idx].name);
949 break;
952 case VIDIOC_G_STD:{
953 *(v4l2_std_id *) arg = itv->std;
954 break;
957 case VIDIOC_S_STD: {
958 v4l2_std_id std = *(v4l2_std_id *) arg;
960 if ((std & V4L2_STD_ALL) == 0)
961 return -EINVAL;
963 if (std == itv->std)
964 break;
966 if (test_bit(IVTV_F_I_RADIO_USER, &itv->i_flags) ||
967 atomic_read(&itv->capturing) > 0 ||
968 atomic_read(&itv->decoding) > 0) {
969 /* Switching standard would turn off the radio or mess
970 with already running streams, prevent that by
971 returning EBUSY. */
972 return -EBUSY;
975 itv->std = std;
976 itv->is_60hz = (std & V4L2_STD_525_60) ? 1 : 0;
977 itv->params.is_50hz = itv->is_50hz = !itv->is_60hz;
978 itv->params.width = 720;
979 itv->params.height = itv->is_50hz ? 576 : 480;
980 itv->vbi.count = itv->is_50hz ? 18 : 12;
981 itv->vbi.start[0] = itv->is_50hz ? 6 : 10;
982 itv->vbi.start[1] = itv->is_50hz ? 318 : 273;
983 if (itv->hw_flags & IVTV_HW_CX25840) {
984 itv->vbi.sliced_decoder_line_size = itv->is_60hz ? 272 : 284;
986 IVTV_DEBUG_INFO("Switching standard to %llx.\n", itv->std);
988 /* Tuner */
989 ivtv_call_i2c_clients(itv, VIDIOC_S_STD, &itv->std);
991 if (itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT) {
992 /* set display standard */
993 itv->std_out = std;
994 itv->is_out_60hz = itv->is_60hz;
995 itv->is_out_50hz = itv->is_50hz;
996 ivtv_call_i2c_clients(itv, VIDIOC_INT_S_STD_OUTPUT, &itv->std_out);
997 ivtv_vapi(itv, CX2341X_DEC_SET_STANDARD, 1, itv->is_out_50hz);
998 itv->main_rect.left = itv->main_rect.top = 0;
999 itv->main_rect.width = 720;
1000 itv->main_rect.height = itv->params.height;
1001 ivtv_vapi(itv, CX2341X_OSD_SET_FRAMEBUFFER_WINDOW, 4,
1002 720, itv->main_rect.height, 0, 0);
1004 break;
1007 case VIDIOC_S_TUNER: { /* Setting tuner can only set audio mode */
1008 struct v4l2_tuner *vt = arg;
1010 if (vt->index != 0)
1011 return -EINVAL;
1013 ivtv_call_i2c_clients(itv, VIDIOC_S_TUNER, vt);
1014 break;
1017 case VIDIOC_G_TUNER: {
1018 struct v4l2_tuner *vt = arg;
1020 if (vt->index != 0)
1021 return -EINVAL;
1023 memset(vt, 0, sizeof(*vt));
1024 ivtv_call_i2c_clients(itv, VIDIOC_G_TUNER, vt);
1026 if (test_bit(IVTV_F_I_RADIO_USER, &itv->i_flags)) {
1027 strcpy(vt->name, "ivtv Radio Tuner");
1028 vt->type = V4L2_TUNER_RADIO;
1029 } else {
1030 strcpy(vt->name, "ivtv TV Tuner");
1031 vt->type = V4L2_TUNER_ANALOG_TV;
1033 break;
1036 case VIDIOC_G_SLICED_VBI_CAP: {
1037 struct v4l2_sliced_vbi_cap *cap = arg;
1038 int set = itv->is_50hz ? V4L2_SLICED_VBI_625 : V4L2_SLICED_VBI_525;
1039 int f, l;
1040 enum v4l2_buf_type type = cap->type;
1042 memset(cap, 0, sizeof(*cap));
1043 cap->type = type;
1044 if (type == V4L2_BUF_TYPE_SLICED_VBI_CAPTURE) {
1045 for (f = 0; f < 2; f++) {
1046 for (l = 0; l < 24; l++) {
1047 if (valid_service_line(f, l, itv->is_50hz)) {
1048 cap->service_lines[f][l] = set;
1052 return 0;
1054 if (type == V4L2_BUF_TYPE_SLICED_VBI_OUTPUT) {
1055 if (!(itv->v4l2_cap & V4L2_CAP_SLICED_VBI_OUTPUT))
1056 return -EINVAL;
1057 if (itv->is_60hz) {
1058 cap->service_lines[0][21] = V4L2_SLICED_CAPTION_525;
1059 cap->service_lines[1][21] = V4L2_SLICED_CAPTION_525;
1060 } else {
1061 cap->service_lines[0][23] = V4L2_SLICED_WSS_625;
1062 cap->service_lines[0][16] = V4L2_SLICED_VPS;
1064 return 0;
1066 return -EINVAL;
1069 case VIDIOC_G_ENC_INDEX: {
1070 struct v4l2_enc_idx *idx = arg;
1071 int i;
1073 idx->entries = (itv->pgm_info_write_idx + IVTV_MAX_PGM_INDEX - itv->pgm_info_read_idx) %
1074 IVTV_MAX_PGM_INDEX;
1075 if (idx->entries > V4L2_ENC_IDX_ENTRIES)
1076 idx->entries = V4L2_ENC_IDX_ENTRIES;
1077 for (i = 0; i < idx->entries; i++) {
1078 idx->entry[i] = itv->pgm_info[(itv->pgm_info_read_idx + i) % IVTV_MAX_PGM_INDEX];
1080 itv->pgm_info_read_idx = (itv->pgm_info_read_idx + idx->entries) % IVTV_MAX_PGM_INDEX;
1081 break;
1084 case VIDIOC_ENCODER_CMD:
1085 case VIDIOC_TRY_ENCODER_CMD: {
1086 struct v4l2_encoder_cmd *enc = arg;
1087 int try = cmd == VIDIOC_TRY_ENCODER_CMD;
1089 memset(&enc->raw, 0, sizeof(enc->raw));
1090 switch (enc->cmd) {
1091 case V4L2_ENC_CMD_START:
1092 enc->flags = 0;
1093 if (try)
1094 return 0;
1095 return ivtv_start_capture(id);
1097 case V4L2_ENC_CMD_STOP:
1098 enc->flags &= V4L2_ENC_CMD_STOP_AT_GOP_END;
1099 if (try)
1100 return 0;
1101 ivtv_stop_capture(id, enc->flags & V4L2_ENC_CMD_STOP_AT_GOP_END);
1102 return 0;
1104 case V4L2_ENC_CMD_PAUSE:
1105 enc->flags = 0;
1106 if (try)
1107 return 0;
1108 if (!atomic_read(&itv->capturing))
1109 return -EPERM;
1110 if (test_and_set_bit(IVTV_F_I_ENC_PAUSED, &itv->i_flags))
1111 return 0;
1112 ivtv_mute(itv);
1113 ivtv_vapi(itv, CX2341X_ENC_PAUSE_ENCODER, 1, 0);
1114 break;
1116 case V4L2_ENC_CMD_RESUME:
1117 enc->flags = 0;
1118 if (try)
1119 return 0;
1120 if (!atomic_read(&itv->capturing))
1121 return -EPERM;
1122 if (!test_and_clear_bit(IVTV_F_I_ENC_PAUSED, &itv->i_flags))
1123 return 0;
1124 ivtv_vapi(itv, CX2341X_ENC_PAUSE_ENCODER, 1, 1);
1125 ivtv_unmute(itv);
1126 break;
1127 default:
1128 return -EINVAL;
1130 break;
1133 case VIDIOC_G_FBUF: {
1134 struct v4l2_framebuffer *fb = arg;
1136 memset(fb, 0, sizeof(*fb));
1137 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT_OVERLAY))
1138 break;
1139 fb->capability = V4L2_FBUF_CAP_EXTERNOVERLAY | V4L2_FBUF_CAP_CHROMAKEY |
1140 V4L2_FBUF_CAP_LOCAL_ALPHA | V4L2_FBUF_CAP_GLOBAL_ALPHA;
1141 fb->fmt.pixelformat = itv->osd_pixelformat;
1142 fb->fmt.width = itv->osd_rect.width;
1143 fb->fmt.height = itv->osd_rect.height;
1144 fb->fmt.left = itv->osd_rect.left;
1145 fb->fmt.top = itv->osd_rect.top;
1146 fb->base = (void *)itv->osd_video_pbase;
1147 if (itv->osd_global_alpha_state)
1148 fb->flags |= V4L2_FBUF_FLAG_GLOBAL_ALPHA;
1149 if (itv->osd_local_alpha_state)
1150 fb->flags |= V4L2_FBUF_FLAG_LOCAL_ALPHA;
1151 if (itv->osd_color_key_state)
1152 fb->flags |= V4L2_FBUF_FLAG_CHROMAKEY;
1153 break;
1156 case VIDIOC_S_FBUF: {
1157 struct v4l2_framebuffer *fb = arg;
1159 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT_OVERLAY))
1160 break;
1161 itv->osd_global_alpha_state = (fb->flags & V4L2_FBUF_FLAG_GLOBAL_ALPHA) != 0;
1162 itv->osd_local_alpha_state = (fb->flags & V4L2_FBUF_FLAG_LOCAL_ALPHA) != 0;
1163 itv->osd_color_key_state = (fb->flags & V4L2_FBUF_FLAG_CHROMAKEY) != 0;
1164 break;
1167 case VIDIOC_LOG_STATUS:
1169 int has_output = itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT;
1170 struct v4l2_input vidin;
1171 struct v4l2_audio audin;
1172 int i;
1174 IVTV_INFO("================= START STATUS CARD #%d =================\n", itv->num);
1175 if (itv->hw_flags & IVTV_HW_TVEEPROM) {
1176 struct tveeprom tv;
1178 ivtv_read_eeprom(itv, &tv);
1180 ivtv_call_i2c_clients(itv, VIDIOC_LOG_STATUS, NULL);
1181 ivtv_get_input(itv, itv->active_input, &vidin);
1182 ivtv_get_audio_input(itv, itv->audio_input, &audin);
1183 IVTV_INFO("Video Input: %s\n", vidin.name);
1184 IVTV_INFO("Audio Input: %s\n", audin.name);
1185 if (has_output) {
1186 struct v4l2_output vidout;
1187 struct v4l2_audioout audout;
1188 int mode = itv->output_mode;
1189 static const char * const output_modes[] = {
1190 "None",
1191 "MPEG Streaming",
1192 "YUV Streaming",
1193 "YUV Frames",
1194 "Passthrough",
1197 ivtv_get_output(itv, itv->active_output, &vidout);
1198 ivtv_get_audio_output(itv, 0, &audout);
1199 IVTV_INFO("Video Output: %s\n", vidout.name);
1200 IVTV_INFO("Audio Output: %s\n", audout.name);
1201 if (mode < 0 || mode > OUT_PASSTHROUGH)
1202 mode = OUT_NONE;
1203 IVTV_INFO("Output Mode: %s\n", output_modes[mode]);
1205 IVTV_INFO("Tuner: %s\n",
1206 test_bit(IVTV_F_I_RADIO_USER, &itv->i_flags) ? "Radio" : "TV");
1207 cx2341x_log_status(&itv->params, itv->name);
1208 IVTV_INFO("Status flags: 0x%08lx\n", itv->i_flags);
1209 for (i = 0; i < IVTV_MAX_STREAMS; i++) {
1210 struct ivtv_stream *s = &itv->streams[i];
1212 if (s->v4l2dev == NULL || s->buffers == 0)
1213 continue;
1214 IVTV_INFO("Stream %s: status 0x%04lx, %d%% of %d KiB (%d buffers) in use\n", s->name, s->s_flags,
1215 (s->buffers - s->q_free.buffers) * 100 / s->buffers,
1216 (s->buffers * s->buf_size) / 1024, s->buffers);
1218 IVTV_INFO("Read MPEG/VBI: %lld/%lld bytes\n", itv->mpg_data_received, itv->vbi_data_inserted);
1219 IVTV_INFO("================== END STATUS CARD #%d ==================\n", itv->num);
1220 break;
1223 default:
1224 return -EINVAL;
1226 return 0;
1229 static int ivtv_decoder_ioctls(struct file *filp, unsigned int cmd, void *arg)
1231 struct ivtv_open_id *id = (struct ivtv_open_id *)filp->private_data;
1232 struct ivtv *itv = id->itv;
1233 int nonblocking = filp->f_flags & O_NONBLOCK;
1234 struct ivtv_stream *s = &itv->streams[id->type];
1236 switch (cmd) {
1237 case IVTV_IOC_DMA_FRAME: {
1238 struct ivtv_dma_frame *args = arg;
1240 IVTV_DEBUG_IOCTL("IVTV_IOC_DMA_FRAME\n");
1241 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
1242 return -EINVAL;
1243 if (args->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
1244 return -EINVAL;
1245 if (itv->output_mode == OUT_UDMA_YUV && args->y_source == NULL)
1246 return 0;
1247 if (ivtv_claim_stream(id, id->type)) {
1248 return -EBUSY;
1250 if (ivtv_set_output_mode(itv, OUT_UDMA_YUV) != OUT_UDMA_YUV) {
1251 ivtv_release_stream(s);
1252 return -EBUSY;
1254 if (args->y_source == NULL)
1255 return 0;
1256 return ivtv_yuv_prep_frame(itv, args);
1259 case VIDEO_GET_PTS: {
1260 u32 data[CX2341X_MBOX_MAX_DATA];
1261 u64 *pts = arg;
1263 IVTV_DEBUG_IOCTL("VIDEO_GET_PTS\n");
1264 if (s->type < IVTV_DEC_STREAM_TYPE_MPG) {
1265 *pts = s->dma_pts;
1266 break;
1268 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
1269 return -EINVAL;
1271 if (test_bit(IVTV_F_I_VALID_DEC_TIMINGS, &itv->i_flags)) {
1272 *pts = (u64) ((u64)itv->last_dec_timing[2] << 32) |
1273 (u64)itv->last_dec_timing[1];
1274 break;
1276 *pts = 0;
1277 if (atomic_read(&itv->decoding)) {
1278 if (ivtv_api(itv, CX2341X_DEC_GET_TIMING_INFO, 5, data)) {
1279 IVTV_DEBUG_WARN("GET_TIMING: couldn't read clock\n");
1280 return -EIO;
1282 memcpy(itv->last_dec_timing, data, sizeof(itv->last_dec_timing));
1283 set_bit(IVTV_F_I_VALID_DEC_TIMINGS, &itv->i_flags);
1284 *pts = (u64) ((u64) data[2] << 32) | (u64) data[1];
1285 /*timing->scr = (u64) (((u64) data[4] << 32) | (u64) (data[3]));*/
1287 break;
1290 case VIDEO_GET_FRAME_COUNT: {
1291 u32 data[CX2341X_MBOX_MAX_DATA];
1292 u64 *frame = arg;
1294 IVTV_DEBUG_IOCTL("VIDEO_GET_FRAME_COUNT\n");
1295 if (s->type < IVTV_DEC_STREAM_TYPE_MPG) {
1296 *frame = 0;
1297 break;
1299 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
1300 return -EINVAL;
1302 if (test_bit(IVTV_F_I_VALID_DEC_TIMINGS, &itv->i_flags)) {
1303 *frame = itv->last_dec_timing[0];
1304 break;
1306 *frame = 0;
1307 if (atomic_read(&itv->decoding)) {
1308 if (ivtv_api(itv, CX2341X_DEC_GET_TIMING_INFO, 5, data)) {
1309 IVTV_DEBUG_WARN("GET_TIMING: couldn't read clock\n");
1310 return -EIO;
1312 memcpy(itv->last_dec_timing, data, sizeof(itv->last_dec_timing));
1313 set_bit(IVTV_F_I_VALID_DEC_TIMINGS, &itv->i_flags);
1314 *frame = data[0];
1316 break;
1319 case VIDEO_PLAY: {
1320 struct video_command vc;
1322 IVTV_DEBUG_IOCTL("VIDEO_PLAY\n");
1323 memset(&vc, 0, sizeof(vc));
1324 vc.cmd = VIDEO_CMD_PLAY;
1325 return ivtv_video_command(itv, id, &vc, 0);
1328 case VIDEO_STOP: {
1329 struct video_command vc;
1331 IVTV_DEBUG_IOCTL("VIDEO_STOP\n");
1332 memset(&vc, 0, sizeof(vc));
1333 vc.cmd = VIDEO_CMD_STOP;
1334 vc.flags = VIDEO_CMD_STOP_TO_BLACK | VIDEO_CMD_STOP_IMMEDIATELY;
1335 return ivtv_video_command(itv, id, &vc, 0);
1338 case VIDEO_FREEZE: {
1339 struct video_command vc;
1341 IVTV_DEBUG_IOCTL("VIDEO_FREEZE\n");
1342 memset(&vc, 0, sizeof(vc));
1343 vc.cmd = VIDEO_CMD_FREEZE;
1344 return ivtv_video_command(itv, id, &vc, 0);
1347 case VIDEO_CONTINUE: {
1348 struct video_command vc;
1350 IVTV_DEBUG_IOCTL("VIDEO_CONTINUE\n");
1351 memset(&vc, 0, sizeof(vc));
1352 vc.cmd = VIDEO_CMD_CONTINUE;
1353 return ivtv_video_command(itv, id, &vc, 0);
1356 case VIDEO_COMMAND:
1357 case VIDEO_TRY_COMMAND: {
1358 struct video_command *vc = arg;
1359 int try = (cmd == VIDEO_TRY_COMMAND);
1361 if (try)
1362 IVTV_DEBUG_IOCTL("VIDEO_TRY_COMMAND\n");
1363 else
1364 IVTV_DEBUG_IOCTL("VIDEO_COMMAND\n");
1365 return ivtv_video_command(itv, id, vc, try);
1368 case VIDEO_GET_EVENT: {
1369 struct video_event *ev = arg;
1370 DEFINE_WAIT(wait);
1372 IVTV_DEBUG_IOCTL("VIDEO_GET_EVENT\n");
1373 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
1374 return -EINVAL;
1375 memset(ev, 0, sizeof(*ev));
1376 set_bit(IVTV_F_I_EV_VSYNC_ENABLED, &itv->i_flags);
1378 while (1) {
1379 if (test_and_clear_bit(IVTV_F_I_EV_DEC_STOPPED, &itv->i_flags))
1380 ev->type = VIDEO_EVENT_DECODER_STOPPED;
1381 else if (test_and_clear_bit(IVTV_F_I_EV_VSYNC, &itv->i_flags)) {
1382 ev->type = VIDEO_EVENT_VSYNC;
1383 ev->u.vsync_field = test_bit(IVTV_F_I_EV_VSYNC_FIELD, &itv->i_flags) ?
1384 VIDEO_VSYNC_FIELD_ODD : VIDEO_VSYNC_FIELD_EVEN;
1385 if (itv->output_mode == OUT_UDMA_YUV &&
1386 (itv->yuv_info.lace_mode & IVTV_YUV_MODE_MASK) ==
1387 IVTV_YUV_MODE_PROGRESSIVE) {
1388 ev->u.vsync_field = VIDEO_VSYNC_FIELD_PROGRESSIVE;
1391 if (ev->type)
1392 return 0;
1393 if (nonblocking)
1394 return -EAGAIN;
1395 /* wait for event */
1396 prepare_to_wait(&itv->event_waitq, &wait, TASK_INTERRUPTIBLE);
1397 if ((itv->i_flags & (IVTV_F_I_EV_DEC_STOPPED|IVTV_F_I_EV_VSYNC)) == 0)
1398 schedule();
1399 finish_wait(&itv->event_waitq, &wait);
1400 if (signal_pending(current)) {
1401 /* return if a signal was received */
1402 IVTV_DEBUG_INFO("User stopped wait for event\n");
1403 return -EINTR;
1406 break;
1409 default:
1410 return -EINVAL;
1412 return 0;
1415 static int ivtv_v4l2_do_ioctl(struct inode *inode, struct file *filp,
1416 unsigned int cmd, void *arg)
1418 struct ivtv_open_id *id = (struct ivtv_open_id *)filp->private_data;
1419 struct ivtv *itv = id->itv;
1420 int ret;
1422 /* check priority */
1423 switch (cmd) {
1424 case VIDIOC_S_CTRL:
1425 case VIDIOC_S_STD:
1426 case VIDIOC_S_INPUT:
1427 case VIDIOC_S_OUTPUT:
1428 case VIDIOC_S_TUNER:
1429 case VIDIOC_S_FREQUENCY:
1430 case VIDIOC_S_FMT:
1431 case VIDIOC_S_CROP:
1432 case VIDIOC_S_AUDIO:
1433 case VIDIOC_S_AUDOUT:
1434 case VIDIOC_S_EXT_CTRLS:
1435 case VIDIOC_S_FBUF:
1436 ret = v4l2_prio_check(&itv->prio, &id->prio);
1437 if (ret)
1438 return ret;
1441 switch (cmd) {
1442 case VIDIOC_DBG_G_REGISTER:
1443 case VIDIOC_DBG_S_REGISTER:
1444 case VIDIOC_G_CHIP_IDENT:
1445 case VIDIOC_INT_S_AUDIO_ROUTING:
1446 case VIDIOC_INT_RESET:
1447 if (ivtv_debug & IVTV_DBGFLG_IOCTL) {
1448 printk(KERN_INFO "ivtv%d ioctl: ", itv->num);
1449 v4l_printk_ioctl(cmd);
1451 return ivtv_debug_ioctls(filp, cmd, arg);
1453 case VIDIOC_G_PRIORITY:
1454 case VIDIOC_S_PRIORITY:
1455 case VIDIOC_QUERYCAP:
1456 case VIDIOC_ENUMINPUT:
1457 case VIDIOC_G_INPUT:
1458 case VIDIOC_S_INPUT:
1459 case VIDIOC_ENUMOUTPUT:
1460 case VIDIOC_G_OUTPUT:
1461 case VIDIOC_S_OUTPUT:
1462 case VIDIOC_G_FMT:
1463 case VIDIOC_S_FMT:
1464 case VIDIOC_TRY_FMT:
1465 case VIDIOC_ENUM_FMT:
1466 case VIDIOC_G_CROP:
1467 case VIDIOC_S_CROP:
1468 case VIDIOC_G_FREQUENCY:
1469 case VIDIOC_S_FREQUENCY:
1470 case VIDIOC_ENUMSTD:
1471 case VIDIOC_G_STD:
1472 case VIDIOC_S_STD:
1473 case VIDIOC_S_TUNER:
1474 case VIDIOC_G_TUNER:
1475 case VIDIOC_ENUMAUDIO:
1476 case VIDIOC_S_AUDIO:
1477 case VIDIOC_G_AUDIO:
1478 case VIDIOC_ENUMAUDOUT:
1479 case VIDIOC_S_AUDOUT:
1480 case VIDIOC_G_AUDOUT:
1481 case VIDIOC_G_SLICED_VBI_CAP:
1482 case VIDIOC_LOG_STATUS:
1483 case VIDIOC_G_ENC_INDEX:
1484 case VIDIOC_ENCODER_CMD:
1485 case VIDIOC_TRY_ENCODER_CMD:
1486 case VIDIOC_G_FBUF:
1487 case VIDIOC_S_FBUF:
1488 if (ivtv_debug & IVTV_DBGFLG_IOCTL) {
1489 printk(KERN_INFO "ivtv%d ioctl: ", itv->num);
1490 v4l_printk_ioctl(cmd);
1492 return ivtv_v4l2_ioctls(itv, filp, cmd, arg);
1494 case VIDIOC_QUERYMENU:
1495 case VIDIOC_QUERYCTRL:
1496 case VIDIOC_S_CTRL:
1497 case VIDIOC_G_CTRL:
1498 case VIDIOC_S_EXT_CTRLS:
1499 case VIDIOC_G_EXT_CTRLS:
1500 case VIDIOC_TRY_EXT_CTRLS:
1501 if (ivtv_debug & IVTV_DBGFLG_IOCTL) {
1502 printk(KERN_INFO "ivtv%d ioctl: ", itv->num);
1503 v4l_printk_ioctl(cmd);
1505 return ivtv_control_ioctls(itv, cmd, arg);
1507 case IVTV_IOC_DMA_FRAME:
1508 case VIDEO_GET_PTS:
1509 case VIDEO_GET_FRAME_COUNT:
1510 case VIDEO_GET_EVENT:
1511 case VIDEO_PLAY:
1512 case VIDEO_STOP:
1513 case VIDEO_FREEZE:
1514 case VIDEO_CONTINUE:
1515 case VIDEO_COMMAND:
1516 case VIDEO_TRY_COMMAND:
1517 return ivtv_decoder_ioctls(filp, cmd, arg);
1519 case 0x00005401: /* Handle isatty() calls */
1520 return -EINVAL;
1521 default:
1522 return v4l_compat_translate_ioctl(inode, filp, cmd, arg,
1523 ivtv_v4l2_do_ioctl);
1525 return 0;
1528 int ivtv_v4l2_ioctl(struct inode *inode, struct file *filp, unsigned int cmd,
1529 unsigned long arg)
1531 struct ivtv_open_id *id = (struct ivtv_open_id *)filp->private_data;
1532 struct ivtv *itv = id->itv;
1534 /* Filter dvb ioctls that cannot be handled by video_usercopy */
1535 switch (cmd) {
1536 case VIDEO_SELECT_SOURCE:
1537 IVTV_DEBUG_IOCTL("VIDEO_SELECT_SOURCE\n");
1538 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
1539 return -EINVAL;
1540 return ivtv_passthrough_mode(itv, arg == VIDEO_SOURCE_DEMUX);
1542 case AUDIO_SET_MUTE:
1543 IVTV_DEBUG_IOCTL("AUDIO_SET_MUTE\n");
1544 itv->speed_mute_audio = arg;
1545 return 0;
1547 case AUDIO_CHANNEL_SELECT:
1548 IVTV_DEBUG_IOCTL("AUDIO_CHANNEL_SELECT\n");
1549 if (arg > AUDIO_STEREO_SWAPPED)
1550 return -EINVAL;
1551 itv->audio_stereo_mode = arg;
1552 ivtv_vapi(itv, CX2341X_DEC_SET_AUDIO_MODE, 2, itv->audio_bilingual_mode, itv->audio_stereo_mode);
1553 return 0;
1555 case AUDIO_BILINGUAL_CHANNEL_SELECT:
1556 IVTV_DEBUG_IOCTL("AUDIO_BILINGUAL_CHANNEL_SELECT\n");
1557 if (arg > AUDIO_STEREO_SWAPPED)
1558 return -EINVAL;
1559 itv->audio_bilingual_mode = arg;
1560 ivtv_vapi(itv, CX2341X_DEC_SET_AUDIO_MODE, 2, itv->audio_bilingual_mode, itv->audio_stereo_mode);
1561 return 0;
1563 default:
1564 break;
1566 return video_usercopy(inode, filp, cmd, arg, ivtv_v4l2_do_ioctl);