pcmcia: CompactFlash driver for PA Semi Electra boards
[pv_ops_mirror.git] / drivers / media / video / ivtv / ivtv-ioctl.c
blob206eee7542db826803db08c73b8778c757f75bca
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-routing.h"
29 #include "ivtv-streams.h"
30 #include "ivtv-yuv.h"
31 #include "ivtv-ioctl.h"
32 #include "ivtv-gpio.h"
33 #include "ivtv-controls.h"
34 #include "ivtv-cards.h"
35 #include <media/saa7127.h>
36 #include <media/tveeprom.h>
37 #include <media/v4l2-chip-ident.h>
38 #include <linux/dvb/audio.h>
39 #include <linux/i2c-id.h>
41 u16 service2vbi(int type)
43 switch (type) {
44 case V4L2_SLICED_TELETEXT_B:
45 return IVTV_SLICED_TYPE_TELETEXT_B;
46 case V4L2_SLICED_CAPTION_525:
47 return IVTV_SLICED_TYPE_CAPTION_525;
48 case V4L2_SLICED_WSS_625:
49 return IVTV_SLICED_TYPE_WSS_625;
50 case V4L2_SLICED_VPS:
51 return IVTV_SLICED_TYPE_VPS;
52 default:
53 return 0;
57 static int valid_service_line(int field, int line, int is_pal)
59 return (is_pal && line >= 6 && (line != 23 || field == 0)) ||
60 (!is_pal && line >= 10 && line < 22);
63 static u16 select_service_from_set(int field, int line, u16 set, int is_pal)
65 u16 valid_set = (is_pal ? V4L2_SLICED_VBI_625 : V4L2_SLICED_VBI_525);
66 int i;
68 set = set & valid_set;
69 if (set == 0 || !valid_service_line(field, line, is_pal)) {
70 return 0;
72 if (!is_pal) {
73 if (line == 21 && (set & V4L2_SLICED_CAPTION_525))
74 return V4L2_SLICED_CAPTION_525;
76 else {
77 if (line == 16 && field == 0 && (set & V4L2_SLICED_VPS))
78 return V4L2_SLICED_VPS;
79 if (line == 23 && field == 0 && (set & V4L2_SLICED_WSS_625))
80 return V4L2_SLICED_WSS_625;
81 if (line == 23)
82 return 0;
84 for (i = 0; i < 32; i++) {
85 if ((1 << i) & set)
86 return 1 << i;
88 return 0;
91 void expand_service_set(struct v4l2_sliced_vbi_format *fmt, int is_pal)
93 u16 set = fmt->service_set;
94 int f, l;
96 fmt->service_set = 0;
97 for (f = 0; f < 2; f++) {
98 for (l = 0; l < 24; l++) {
99 fmt->service_lines[f][l] = select_service_from_set(f, l, set, is_pal);
104 static int check_service_set(struct v4l2_sliced_vbi_format *fmt, int is_pal)
106 int f, l;
107 u16 set = 0;
109 for (f = 0; f < 2; f++) {
110 for (l = 0; l < 24; l++) {
111 fmt->service_lines[f][l] = select_service_from_set(f, l, fmt->service_lines[f][l], is_pal);
112 set |= fmt->service_lines[f][l];
115 return set != 0;
118 u16 get_service_set(struct v4l2_sliced_vbi_format *fmt)
120 int f, l;
121 u16 set = 0;
123 for (f = 0; f < 2; f++) {
124 for (l = 0; l < 24; l++) {
125 set |= fmt->service_lines[f][l];
128 return set;
131 static const struct {
132 v4l2_std_id std;
133 char *name;
134 } enum_stds[] = {
135 { V4L2_STD_PAL_BG | V4L2_STD_PAL_H, "PAL-BGH" },
136 { V4L2_STD_PAL_DK, "PAL-DK" },
137 { V4L2_STD_PAL_I, "PAL-I" },
138 { V4L2_STD_PAL_M, "PAL-M" },
139 { V4L2_STD_PAL_N, "PAL-N" },
140 { V4L2_STD_PAL_Nc, "PAL-Nc" },
141 { V4L2_STD_SECAM_B | V4L2_STD_SECAM_G | V4L2_STD_SECAM_H, "SECAM-BGH" },
142 { V4L2_STD_SECAM_DK, "SECAM-DK" },
143 { V4L2_STD_SECAM_L, "SECAM-L" },
144 { V4L2_STD_SECAM_LC, "SECAM-L'" },
145 { V4L2_STD_NTSC_M, "NTSC-M" },
146 { V4L2_STD_NTSC_M_JP, "NTSC-J" },
147 { V4L2_STD_NTSC_M_KR, "NTSC-K" },
150 static const struct v4l2_standard ivtv_std_60hz =
152 .frameperiod = {.numerator = 1001, .denominator = 30000},
153 .framelines = 525,
156 static const struct v4l2_standard ivtv_std_50hz =
158 .frameperiod = {.numerator = 1, .denominator = 25},
159 .framelines = 625,
162 void ivtv_set_osd_alpha(struct ivtv *itv)
164 ivtv_vapi(itv, CX2341X_OSD_SET_GLOBAL_ALPHA, 3,
165 itv->osd_global_alpha_state, itv->osd_global_alpha, !itv->osd_local_alpha_state);
166 ivtv_vapi(itv, CX2341X_OSD_SET_CHROMA_KEY, 2, itv->osd_chroma_key_state, itv->osd_chroma_key);
169 int ivtv_set_speed(struct ivtv *itv, int speed)
171 u32 data[CX2341X_MBOX_MAX_DATA];
172 struct ivtv_stream *s;
173 int single_step = (speed == 1 || speed == -1);
174 DEFINE_WAIT(wait);
176 if (speed == 0) speed = 1000;
178 /* No change? */
179 if (speed == itv->speed && !single_step)
180 return 0;
182 s = &itv->streams[IVTV_DEC_STREAM_TYPE_MPG];
184 if (single_step && (speed < 0) == (itv->speed < 0)) {
185 /* Single step video and no need to change direction */
186 ivtv_vapi(itv, CX2341X_DEC_STEP_VIDEO, 1, 0);
187 itv->speed = speed;
188 return 0;
190 if (single_step)
191 /* Need to change direction */
192 speed = speed < 0 ? -1000 : 1000;
194 data[0] = (speed > 1000 || speed < -1000) ? 0x80000000 : 0;
195 data[0] |= (speed > 1000 || speed < -1500) ? 0x40000000 : 0;
196 data[1] = (speed < 0);
197 data[2] = speed < 0 ? 3 : 7;
198 data[3] = itv->params.video_b_frames;
199 data[4] = (speed == 1500 || speed == 500) ? itv->speed_mute_audio : 0;
200 data[5] = 0;
201 data[6] = 0;
203 if (speed == 1500 || speed == -1500) data[0] |= 1;
204 else if (speed == 2000 || speed == -2000) data[0] |= 2;
205 else if (speed > -1000 && speed < 0) data[0] |= (-1000 / speed);
206 else if (speed < 1000 && speed > 0) data[0] |= (1000 / speed);
208 /* If not decoding, just change speed setting */
209 if (atomic_read(&itv->decoding) > 0) {
210 int got_sig = 0;
212 /* Stop all DMA and decoding activity */
213 ivtv_vapi(itv, CX2341X_DEC_PAUSE_PLAYBACK, 1, 0);
215 /* Wait for any DMA to finish */
216 prepare_to_wait(&itv->dma_waitq, &wait, TASK_INTERRUPTIBLE);
217 while (itv->i_flags & IVTV_F_I_DMA) {
218 got_sig = signal_pending(current);
219 if (got_sig)
220 break;
221 got_sig = 0;
222 schedule();
224 finish_wait(&itv->dma_waitq, &wait);
225 if (got_sig)
226 return -EINTR;
228 /* Change Speed safely */
229 ivtv_api(itv, CX2341X_DEC_SET_PLAYBACK_SPEED, 7, data);
230 IVTV_DEBUG_INFO("Setting Speed to 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x\n",
231 data[0], data[1], data[2], data[3], data[4], data[5], data[6]);
233 if (single_step) {
234 speed = (speed < 0) ? -1 : 1;
235 ivtv_vapi(itv, CX2341X_DEC_STEP_VIDEO, 1, 0);
237 itv->speed = speed;
238 return 0;
241 static int ivtv_validate_speed(int cur_speed, int new_speed)
243 int fact = new_speed < 0 ? -1 : 1;
244 int s;
246 if (new_speed < 0) new_speed = -new_speed;
247 if (cur_speed < 0) cur_speed = -cur_speed;
249 if (cur_speed <= new_speed) {
250 if (new_speed > 1500) return fact * 2000;
251 if (new_speed > 1000) return fact * 1500;
253 else {
254 if (new_speed >= 2000) return fact * 2000;
255 if (new_speed >= 1500) return fact * 1500;
256 if (new_speed >= 1000) return fact * 1000;
258 if (new_speed == 0) return 1000;
259 if (new_speed == 1 || new_speed == 1000) return fact * new_speed;
261 s = new_speed;
262 new_speed = 1000 / new_speed;
263 if (1000 / cur_speed == new_speed)
264 new_speed += (cur_speed < s) ? -1 : 1;
265 if (new_speed > 60) return 1000 / (fact * 60);
266 return 1000 / (fact * new_speed);
269 static int ivtv_video_command(struct ivtv *itv, struct ivtv_open_id *id,
270 struct video_command *vc, int try)
272 struct ivtv_stream *s = &itv->streams[IVTV_DEC_STREAM_TYPE_MPG];
274 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
275 return -EINVAL;
277 switch (vc->cmd) {
278 case VIDEO_CMD_PLAY: {
279 vc->flags = 0;
280 vc->play.speed = ivtv_validate_speed(itv->speed, vc->play.speed);
281 if (vc->play.speed < 0)
282 vc->play.format = VIDEO_PLAY_FMT_GOP;
283 if (try) break;
285 if (ivtv_set_output_mode(itv, OUT_MPG) != OUT_MPG)
286 return -EBUSY;
287 if (test_and_clear_bit(IVTV_F_I_DEC_PAUSED, &itv->i_flags)) {
288 /* forces ivtv_set_speed to be called */
289 itv->speed = 0;
291 return ivtv_start_decoding(id, vc->play.speed);
294 case VIDEO_CMD_STOP:
295 vc->flags &= VIDEO_CMD_STOP_IMMEDIATELY|VIDEO_CMD_STOP_TO_BLACK;
296 if (vc->flags & VIDEO_CMD_STOP_IMMEDIATELY)
297 vc->stop.pts = 0;
298 if (try) break;
299 if (atomic_read(&itv->decoding) == 0)
300 return 0;
301 if (itv->output_mode != OUT_MPG)
302 return -EBUSY;
304 itv->output_mode = OUT_NONE;
305 return ivtv_stop_v4l2_decode_stream(s, vc->flags, vc->stop.pts);
307 case VIDEO_CMD_FREEZE:
308 vc->flags &= VIDEO_CMD_FREEZE_TO_BLACK;
309 if (try) break;
310 if (itv->output_mode != OUT_MPG)
311 return -EBUSY;
312 if (atomic_read(&itv->decoding) > 0) {
313 ivtv_vapi(itv, CX2341X_DEC_PAUSE_PLAYBACK, 1,
314 (vc->flags & VIDEO_CMD_FREEZE_TO_BLACK) ? 1 : 0);
315 set_bit(IVTV_F_I_DEC_PAUSED, &itv->i_flags);
317 break;
319 case VIDEO_CMD_CONTINUE:
320 vc->flags = 0;
321 if (try) break;
322 if (itv->output_mode != OUT_MPG)
323 return -EBUSY;
324 if (test_and_clear_bit(IVTV_F_I_DEC_PAUSED, &itv->i_flags)) {
325 int speed = itv->speed;
326 itv->speed = 0;
327 return ivtv_start_decoding(id, speed);
329 break;
331 default:
332 return -EINVAL;
334 return 0;
337 static int ivtv_itvc(struct ivtv *itv, unsigned int cmd, void *arg)
339 struct v4l2_register *regs = arg;
340 unsigned long flags;
341 volatile u8 __iomem *reg_start;
343 if (!capable(CAP_SYS_ADMIN))
344 return -EPERM;
345 if (regs->reg >= IVTV_REG_OFFSET && regs->reg < IVTV_REG_OFFSET + IVTV_REG_SIZE)
346 reg_start = itv->reg_mem - IVTV_REG_OFFSET;
347 else if (itv->has_cx23415 && regs->reg >= IVTV_DECODER_OFFSET &&
348 regs->reg < IVTV_DECODER_OFFSET + IVTV_DECODER_SIZE)
349 reg_start = itv->dec_mem - IVTV_DECODER_OFFSET;
350 else if (regs->reg >= 0 && regs->reg < IVTV_ENCODER_SIZE)
351 reg_start = itv->enc_mem;
352 else
353 return -EINVAL;
355 spin_lock_irqsave(&ivtv_cards_lock, flags);
356 if (cmd == VIDIOC_DBG_G_REGISTER) {
357 regs->val = readl(regs->reg + reg_start);
358 } else {
359 writel(regs->val, regs->reg + reg_start);
361 spin_unlock_irqrestore(&ivtv_cards_lock, flags);
362 return 0;
365 static int ivtv_get_fmt(struct ivtv *itv, int streamtype, struct v4l2_format *fmt)
367 switch (fmt->type) {
368 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
369 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
370 return -EINVAL;
371 fmt->fmt.pix.width = itv->main_rect.width;
372 fmt->fmt.pix.height = itv->main_rect.height;
373 fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
374 fmt->fmt.pix.field = V4L2_FIELD_INTERLACED;
375 if (itv->output_mode == OUT_UDMA_YUV) {
376 switch (itv->yuv_info.lace_mode & IVTV_YUV_MODE_MASK) {
377 case IVTV_YUV_MODE_INTERLACED:
378 fmt->fmt.pix.field = (itv->yuv_info.lace_mode & IVTV_YUV_SYNC_MASK) ?
379 V4L2_FIELD_INTERLACED_BT : V4L2_FIELD_INTERLACED_TB;
380 break;
381 case IVTV_YUV_MODE_PROGRESSIVE:
382 fmt->fmt.pix.field = V4L2_FIELD_NONE;
383 break;
384 default:
385 fmt->fmt.pix.field = V4L2_FIELD_ANY;
386 break;
388 fmt->fmt.pix.pixelformat = V4L2_PIX_FMT_HM12;
389 /* YUV size is (Y=(h*w) + UV=(h*(w/2))) */
390 fmt->fmt.pix.sizeimage =
391 fmt->fmt.pix.height * fmt->fmt.pix.width +
392 fmt->fmt.pix.height * (fmt->fmt.pix.width / 2);
394 else if (itv->output_mode == OUT_YUV ||
395 streamtype == IVTV_ENC_STREAM_TYPE_YUV ||
396 streamtype == IVTV_DEC_STREAM_TYPE_YUV) {
397 fmt->fmt.pix.pixelformat = V4L2_PIX_FMT_HM12;
398 /* YUV size is (Y=(h*w) + UV=(h*(w/2))) */
399 fmt->fmt.pix.sizeimage =
400 fmt->fmt.pix.height * fmt->fmt.pix.width +
401 fmt->fmt.pix.height * (fmt->fmt.pix.width / 2);
402 } else {
403 fmt->fmt.pix.pixelformat = V4L2_PIX_FMT_MPEG;
404 fmt->fmt.pix.sizeimage = 128 * 1024;
406 break;
408 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
409 fmt->fmt.pix.width = itv->params.width;
410 fmt->fmt.pix.height = itv->params.height;
411 fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
412 fmt->fmt.pix.field = V4L2_FIELD_INTERLACED;
413 if (streamtype == IVTV_ENC_STREAM_TYPE_YUV ||
414 streamtype == IVTV_DEC_STREAM_TYPE_YUV) {
415 fmt->fmt.pix.pixelformat = V4L2_PIX_FMT_HM12;
416 /* YUV size is (Y=(h*w) + UV=(h*(w/2))) */
417 fmt->fmt.pix.sizeimage =
418 fmt->fmt.pix.height * fmt->fmt.pix.width +
419 fmt->fmt.pix.height * (fmt->fmt.pix.width / 2);
420 } else {
421 fmt->fmt.pix.pixelformat = V4L2_PIX_FMT_MPEG;
422 fmt->fmt.pix.sizeimage = 128 * 1024;
424 break;
426 case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
427 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
428 return -EINVAL;
429 fmt->fmt.win.chromakey = itv->osd_chroma_key;
430 fmt->fmt.win.global_alpha = itv->osd_global_alpha;
431 break;
433 case V4L2_BUF_TYPE_VBI_CAPTURE:
434 fmt->fmt.vbi.sampling_rate = 27000000;
435 fmt->fmt.vbi.offset = 248;
436 fmt->fmt.vbi.samples_per_line = itv->vbi.raw_decoder_line_size - 4;
437 fmt->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
438 fmt->fmt.vbi.start[0] = itv->vbi.start[0];
439 fmt->fmt.vbi.start[1] = itv->vbi.start[1];
440 fmt->fmt.vbi.count[0] = fmt->fmt.vbi.count[1] = itv->vbi.count;
441 break;
443 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
445 struct v4l2_sliced_vbi_format *vbifmt = &fmt->fmt.sliced;
447 if (!(itv->v4l2_cap & V4L2_CAP_SLICED_VBI_OUTPUT))
448 return -EINVAL;
449 vbifmt->io_size = sizeof(struct v4l2_sliced_vbi_data) * 36;
450 memset(vbifmt->reserved, 0, sizeof(vbifmt->reserved));
451 memset(vbifmt->service_lines, 0, sizeof(vbifmt->service_lines));
452 if (itv->is_60hz) {
453 vbifmt->service_lines[0][21] = V4L2_SLICED_CAPTION_525;
454 vbifmt->service_lines[1][21] = V4L2_SLICED_CAPTION_525;
455 } else {
456 vbifmt->service_lines[0][23] = V4L2_SLICED_WSS_625;
457 vbifmt->service_lines[0][16] = V4L2_SLICED_VPS;
459 vbifmt->service_set = get_service_set(vbifmt);
460 break;
463 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
465 struct v4l2_sliced_vbi_format *vbifmt = &fmt->fmt.sliced;
467 vbifmt->io_size = sizeof(struct v4l2_sliced_vbi_data) * 36;
468 memset(vbifmt->reserved, 0, sizeof(vbifmt->reserved));
469 memset(vbifmt->service_lines, 0, sizeof(vbifmt->service_lines));
471 if (streamtype == IVTV_DEC_STREAM_TYPE_VBI) {
472 vbifmt->service_set = itv->is_50hz ? V4L2_SLICED_VBI_625 :
473 V4L2_SLICED_VBI_525;
474 expand_service_set(vbifmt, itv->is_50hz);
475 break;
478 itv->video_dec_func(itv, VIDIOC_G_FMT, fmt);
479 vbifmt->service_set = get_service_set(vbifmt);
480 break;
482 case V4L2_BUF_TYPE_VBI_OUTPUT:
483 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
484 default:
485 return -EINVAL;
487 return 0;
490 static int ivtv_try_or_set_fmt(struct ivtv *itv, int streamtype,
491 struct v4l2_format *fmt, int set_fmt)
493 struct v4l2_sliced_vbi_format *vbifmt = &fmt->fmt.sliced;
494 u16 set;
496 if (fmt->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
497 struct v4l2_rect r;
498 int field;
500 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
501 return -EINVAL;
502 field = fmt->fmt.pix.field;
503 r.top = 0;
504 r.left = 0;
505 r.width = fmt->fmt.pix.width;
506 r.height = fmt->fmt.pix.height;
507 ivtv_get_fmt(itv, streamtype, fmt);
508 if (itv->output_mode != OUT_UDMA_YUV) {
509 /* TODO: would setting the rect also be valid for this mode? */
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;
542 return 0;
545 if (fmt->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY) {
546 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
547 return -EINVAL;
548 if (set_fmt) {
549 itv->osd_chroma_key = fmt->fmt.win.chromakey;
550 itv->osd_global_alpha = fmt->fmt.win.global_alpha;
551 ivtv_set_osd_alpha(itv);
553 return 0;
556 /* set window size */
557 if (fmt->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
558 int w = fmt->fmt.pix.width;
559 int h = fmt->fmt.pix.height;
561 if (w > 720) w = 720;
562 else if (w < 1) w = 1;
563 if (h > (itv->is_50hz ? 576 : 480)) h = (itv->is_50hz ? 576 : 480);
564 else if (h < 2) h = 2;
565 ivtv_get_fmt(itv, streamtype, fmt);
566 fmt->fmt.pix.width = w;
567 fmt->fmt.pix.height = h;
569 if (!set_fmt || (itv->params.width == w && itv->params.height == h))
570 return 0;
571 if (atomic_read(&itv->capturing) > 0)
572 return -EBUSY;
574 itv->params.width = w;
575 itv->params.height = h;
576 if (w != 720 || h != (itv->is_50hz ? 576 : 480))
577 itv->params.video_temporal_filter = 0;
578 else
579 itv->params.video_temporal_filter = 8;
580 itv->video_dec_func(itv, VIDIOC_S_FMT, fmt);
581 return ivtv_get_fmt(itv, streamtype, fmt);
584 /* set raw VBI format */
585 if (fmt->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
586 if (set_fmt && atomic_read(&itv->capturing) > 0) {
587 return -EBUSY;
589 if (set_fmt) {
590 itv->vbi.sliced_in->service_set = 0;
591 itv->video_dec_func(itv, VIDIOC_S_FMT, &itv->vbi.in);
593 return ivtv_get_fmt(itv, streamtype, fmt);
596 /* set sliced VBI output
597 In principle the user could request that only certain
598 VBI types are output and that the others are ignored.
599 I.e., suppress CC in the even fields or only output
600 WSS and no VPS. Currently though there is no choice. */
601 if (fmt->type == V4L2_BUF_TYPE_SLICED_VBI_OUTPUT)
602 return ivtv_get_fmt(itv, streamtype, fmt);
604 /* any else but sliced VBI capture is an error */
605 if (fmt->type != V4L2_BUF_TYPE_SLICED_VBI_CAPTURE)
606 return -EINVAL;
608 if (streamtype == IVTV_DEC_STREAM_TYPE_VBI)
609 return ivtv_get_fmt(itv, streamtype, fmt);
611 /* set sliced VBI capture format */
612 vbifmt->io_size = sizeof(struct v4l2_sliced_vbi_data) * 36;
613 memset(vbifmt->reserved, 0, sizeof(vbifmt->reserved));
615 if (vbifmt->service_set)
616 expand_service_set(vbifmt, itv->is_50hz);
617 set = check_service_set(vbifmt, itv->is_50hz);
618 vbifmt->service_set = get_service_set(vbifmt);
620 if (!set_fmt)
621 return 0;
622 if (set == 0)
623 return -EINVAL;
624 if (atomic_read(&itv->capturing) > 0) {
625 return -EBUSY;
627 itv->video_dec_func(itv, VIDIOC_S_FMT, fmt);
628 memcpy(itv->vbi.sliced_in, vbifmt, sizeof(*itv->vbi.sliced_in));
629 return 0;
632 static int ivtv_debug_ioctls(struct file *filp, unsigned int cmd, void *arg)
634 struct ivtv_open_id *id = (struct ivtv_open_id *)filp->private_data;
635 struct ivtv *itv = id->itv;
636 struct v4l2_register *reg = arg;
638 switch (cmd) {
639 /* ioctls to allow direct access to the encoder registers for testing */
640 case VIDIOC_DBG_G_REGISTER:
641 if (v4l2_chip_match_host(reg->match_type, reg->match_chip))
642 return ivtv_itvc(itv, cmd, arg);
643 if (reg->match_type == V4L2_CHIP_MATCH_I2C_DRIVER)
644 return ivtv_i2c_id(itv, reg->match_chip, cmd, arg);
645 return ivtv_call_i2c_client(itv, reg->match_chip, cmd, arg);
647 case VIDIOC_DBG_S_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_G_CHIP_IDENT: {
655 struct v4l2_chip_ident *chip = arg;
657 chip->ident = V4L2_IDENT_NONE;
658 chip->revision = 0;
659 if (reg->match_type == V4L2_CHIP_MATCH_HOST) {
660 if (v4l2_chip_match_host(reg->match_type, reg->match_chip)) {
661 struct v4l2_chip_ident *chip = arg;
663 chip->ident = itv->has_cx23415 ? V4L2_IDENT_CX23415 : V4L2_IDENT_CX23416;
665 return 0;
667 if (reg->match_type == V4L2_CHIP_MATCH_I2C_DRIVER)
668 return ivtv_i2c_id(itv, reg->match_chip, cmd, arg);
669 if (reg->match_type == V4L2_CHIP_MATCH_I2C_ADDR)
670 return ivtv_call_i2c_client(itv, reg->match_chip, cmd, arg);
671 return -EINVAL;
674 case VIDIOC_INT_S_AUDIO_ROUTING: {
675 struct v4l2_routing *route = arg;
677 ivtv_i2c_hw(itv, itv->card->hw_audio, VIDIOC_INT_S_AUDIO_ROUTING, route);
678 break;
681 case VIDIOC_INT_RESET: {
682 u32 val = *(u32 *)arg;
684 if ((val == 0 && itv->options.newi2c) || (val & 0x01)) {
685 ivtv_reset_ir_gpio(itv);
687 if (val & 0x02) {
688 itv->video_dec_func(itv, cmd, 0);
690 break;
693 default:
694 return -EINVAL;
696 return 0;
699 int ivtv_v4l2_ioctls(struct ivtv *itv, struct file *filp, unsigned int cmd, void *arg)
701 struct ivtv_open_id *id = NULL;
702 u32 data[CX2341X_MBOX_MAX_DATA];
704 if (filp) id = (struct ivtv_open_id *)filp->private_data;
706 switch (cmd) {
707 case VIDIOC_G_PRIORITY:
709 enum v4l2_priority *p = arg;
711 *p = v4l2_prio_max(&itv->prio);
712 break;
715 case VIDIOC_S_PRIORITY:
717 enum v4l2_priority *prio = arg;
719 return v4l2_prio_change(&itv->prio, &id->prio, *prio);
722 case VIDIOC_QUERYCAP:{
723 struct v4l2_capability *vcap = arg;
725 memset(vcap, 0, sizeof(*vcap));
726 strcpy(vcap->driver, IVTV_DRIVER_NAME); /* driver name */
727 strcpy(vcap->card, itv->card_name); /* card type */
728 strcpy(vcap->bus_info, pci_name(itv->dev)); /* bus info... */
729 vcap->version = IVTV_DRIVER_VERSION; /* version */
730 vcap->capabilities = itv->v4l2_cap; /* capabilities */
732 /* reserved.. must set to 0! */
733 vcap->reserved[0] = vcap->reserved[1] =
734 vcap->reserved[2] = vcap->reserved[3] = 0;
735 break;
738 case VIDIOC_ENUMAUDIO:{
739 struct v4l2_audio *vin = arg;
741 return ivtv_get_audio_input(itv, vin->index, vin);
744 case VIDIOC_G_AUDIO:{
745 struct v4l2_audio *vin = arg;
747 vin->index = itv->audio_input;
748 return ivtv_get_audio_input(itv, vin->index, vin);
751 case VIDIOC_S_AUDIO:{
752 struct v4l2_audio *vout = arg;
754 if (vout->index >= itv->nof_audio_inputs)
755 return -EINVAL;
756 itv->audio_input = vout->index;
757 ivtv_audio_set_io(itv);
758 break;
761 case VIDIOC_ENUMAUDOUT:{
762 struct v4l2_audioout *vin = arg;
764 /* set it to defaults from our table */
765 return ivtv_get_audio_output(itv, vin->index, vin);
768 case VIDIOC_G_AUDOUT:{
769 struct v4l2_audioout *vin = arg;
771 vin->index = 0;
772 return ivtv_get_audio_output(itv, vin->index, vin);
775 case VIDIOC_S_AUDOUT:{
776 struct v4l2_audioout *vout = arg;
778 return ivtv_get_audio_output(itv, vout->index, vout);
781 case VIDIOC_ENUMINPUT:{
782 struct v4l2_input *vin = arg;
784 /* set it to defaults from our table */
785 return ivtv_get_input(itv, vin->index, vin);
788 case VIDIOC_ENUMOUTPUT:{
789 struct v4l2_output *vout = arg;
791 return ivtv_get_output(itv, vout->index, vout);
794 case VIDIOC_TRY_FMT:
795 case VIDIOC_S_FMT: {
796 struct v4l2_format *fmt = arg;
798 return ivtv_try_or_set_fmt(itv, id->type, fmt, cmd == VIDIOC_S_FMT);
801 case VIDIOC_G_FMT: {
802 struct v4l2_format *fmt = arg;
803 int type = fmt->type;
805 memset(fmt, 0, sizeof(*fmt));
806 fmt->type = type;
807 return ivtv_get_fmt(itv, id->type, fmt);
810 case VIDIOC_CROPCAP: {
811 struct v4l2_cropcap *cropcap = arg;
813 if (cropcap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
814 cropcap->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
815 return -EINVAL;
816 cropcap->bounds.top = cropcap->bounds.left = 0;
817 cropcap->bounds.width = 720;
818 if (cropcap->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
819 cropcap->bounds.height = itv->is_50hz ? 576 : 480;
820 cropcap->pixelaspect.numerator = itv->is_50hz ? 59 : 10;
821 cropcap->pixelaspect.denominator = itv->is_50hz ? 54 : 11;
822 } else {
823 cropcap->bounds.height = itv->is_out_50hz ? 576 : 480;
824 cropcap->pixelaspect.numerator = itv->is_out_50hz ? 59 : 10;
825 cropcap->pixelaspect.denominator = itv->is_out_50hz ? 54 : 11;
827 cropcap->defrect = cropcap->bounds;
828 return 0;
831 case VIDIOC_S_CROP: {
832 struct v4l2_crop *crop = arg;
834 if (crop->type == V4L2_BUF_TYPE_VIDEO_OUTPUT &&
835 (itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT)) {
836 if (!ivtv_vapi(itv, CX2341X_OSD_SET_FRAMEBUFFER_WINDOW, 4,
837 crop->c.width, crop->c.height, crop->c.left, crop->c.top)) {
838 itv->main_rect = crop->c;
839 return 0;
841 return -EINVAL;
843 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
844 return -EINVAL;
845 return itv->video_dec_func(itv, VIDIOC_S_CROP, arg);
848 case VIDIOC_G_CROP: {
849 struct v4l2_crop *crop = arg;
851 if (crop->type == V4L2_BUF_TYPE_VIDEO_OUTPUT &&
852 (itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT)) {
853 crop->c = itv->main_rect;
854 return 0;
856 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
857 return -EINVAL;
858 return itv->video_dec_func(itv, VIDIOC_G_CROP, arg);
861 case VIDIOC_ENUM_FMT: {
862 static struct v4l2_fmtdesc formats[] = {
863 { 0, 0, 0,
864 "HM12 (YUV 4:1:1)", V4L2_PIX_FMT_HM12,
865 { 0, 0, 0, 0 }
867 { 1, 0, V4L2_FMT_FLAG_COMPRESSED,
868 "MPEG", V4L2_PIX_FMT_MPEG,
869 { 0, 0, 0, 0 }
872 struct v4l2_fmtdesc *fmt = arg;
873 enum v4l2_buf_type type = fmt->type;
875 switch (type) {
876 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
877 break;
878 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
879 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
880 return -EINVAL;
881 break;
882 default:
883 return -EINVAL;
885 if (fmt->index > 1)
886 return -EINVAL;
887 *fmt = formats[fmt->index];
888 fmt->type = type;
889 return 0;
892 case VIDIOC_G_INPUT:{
893 *(int *)arg = itv->active_input;
894 break;
897 case VIDIOC_S_INPUT:{
898 int inp = *(int *)arg;
900 if (inp < 0 || inp >= itv->nof_inputs)
901 return -EINVAL;
903 if (inp == itv->active_input) {
904 IVTV_DEBUG_INFO("Input unchanged\n");
905 break;
907 if (atomic_read(&itv->capturing) > 0) {
908 return -EBUSY;
910 IVTV_DEBUG_INFO("Changing input from %d to %d\n",
911 itv->active_input, inp);
913 itv->active_input = inp;
914 /* Set the audio input to whatever is appropriate for the
915 input type. */
916 itv->audio_input = itv->card->video_inputs[inp].audio_index;
918 /* prevent others from messing with the streams until
919 we're finished changing inputs. */
920 ivtv_mute(itv);
921 ivtv_video_set_io(itv);
922 ivtv_audio_set_io(itv);
923 ivtv_unmute(itv);
924 break;
927 case VIDIOC_G_OUTPUT:{
928 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
929 return -EINVAL;
930 *(int *)arg = itv->active_output;
931 break;
934 case VIDIOC_S_OUTPUT:{
935 int outp = *(int *)arg;
936 struct v4l2_routing route;
938 if (outp >= itv->card->nof_outputs)
939 return -EINVAL;
941 if (outp == itv->active_output) {
942 IVTV_DEBUG_INFO("Output unchanged\n");
943 break;
945 IVTV_DEBUG_INFO("Changing output from %d to %d\n",
946 itv->active_output, outp);
948 itv->active_output = outp;
949 route.input = SAA7127_INPUT_TYPE_NORMAL;
950 route.output = itv->card->video_outputs[outp].video_output;
951 ivtv_saa7127(itv, VIDIOC_INT_S_VIDEO_ROUTING, &route);
952 break;
955 case VIDIOC_G_FREQUENCY:{
956 struct v4l2_frequency *vf = arg;
958 if (vf->tuner != 0)
959 return -EINVAL;
960 ivtv_call_i2c_clients(itv, cmd, arg);
961 break;
964 case VIDIOC_S_FREQUENCY:{
965 struct v4l2_frequency vf = *(struct v4l2_frequency *)arg;
967 if (vf.tuner != 0)
968 return -EINVAL;
970 ivtv_mute(itv);
971 IVTV_DEBUG_INFO("v4l2 ioctl: set frequency %d\n", vf.frequency);
972 ivtv_call_i2c_clients(itv, cmd, &vf);
973 ivtv_unmute(itv);
974 break;
977 case VIDIOC_ENUMSTD:{
978 struct v4l2_standard *vs = arg;
979 int idx = vs->index;
981 if (idx < 0 || idx >= ARRAY_SIZE(enum_stds))
982 return -EINVAL;
984 *vs = (enum_stds[idx].std & V4L2_STD_525_60) ?
985 ivtv_std_60hz : ivtv_std_50hz;
986 vs->index = idx;
987 vs->id = enum_stds[idx].std;
988 strcpy(vs->name, enum_stds[idx].name);
989 break;
992 case VIDIOC_G_STD:{
993 *(v4l2_std_id *) arg = itv->std;
994 break;
997 case VIDIOC_S_STD: {
998 v4l2_std_id std = *(v4l2_std_id *) arg;
1000 if ((std & V4L2_STD_ALL) == 0)
1001 return -EINVAL;
1003 if (std == itv->std)
1004 break;
1006 if (test_bit(IVTV_F_I_RADIO_USER, &itv->i_flags) ||
1007 atomic_read(&itv->capturing) > 0 ||
1008 atomic_read(&itv->decoding) > 0) {
1009 /* Switching standard would turn off the radio or mess
1010 with already running streams, prevent that by
1011 returning EBUSY. */
1012 return -EBUSY;
1015 itv->std = std;
1016 itv->is_60hz = (std & V4L2_STD_525_60) ? 1 : 0;
1017 itv->params.is_50hz = itv->is_50hz = !itv->is_60hz;
1018 itv->params.width = 720;
1019 itv->params.height = itv->is_50hz ? 576 : 480;
1020 itv->vbi.count = itv->is_50hz ? 18 : 12;
1021 itv->vbi.start[0] = itv->is_50hz ? 6 : 10;
1022 itv->vbi.start[1] = itv->is_50hz ? 318 : 273;
1023 if (itv->hw_flags & IVTV_HW_CX25840) {
1024 itv->vbi.sliced_decoder_line_size = itv->is_60hz ? 272 : 284;
1026 IVTV_DEBUG_INFO("Switching standard to %llx.\n", (unsigned long long)itv->std);
1028 /* Tuner */
1029 ivtv_call_i2c_clients(itv, VIDIOC_S_STD, &itv->std);
1031 if (itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT) {
1032 /* set display standard */
1033 itv->std_out = std;
1034 itv->is_out_60hz = itv->is_60hz;
1035 itv->is_out_50hz = itv->is_50hz;
1036 ivtv_call_i2c_clients(itv, VIDIOC_INT_S_STD_OUTPUT, &itv->std_out);
1037 ivtv_vapi(itv, CX2341X_DEC_SET_STANDARD, 1, itv->is_out_50hz);
1038 itv->main_rect.left = itv->main_rect.top = 0;
1039 itv->main_rect.width = 720;
1040 itv->main_rect.height = itv->params.height;
1041 ivtv_vapi(itv, CX2341X_OSD_SET_FRAMEBUFFER_WINDOW, 4,
1042 720, itv->main_rect.height, 0, 0);
1044 break;
1047 case VIDIOC_S_TUNER: { /* Setting tuner can only set audio mode */
1048 struct v4l2_tuner *vt = arg;
1050 if (vt->index != 0)
1051 return -EINVAL;
1053 ivtv_call_i2c_clients(itv, VIDIOC_S_TUNER, vt);
1054 break;
1057 case VIDIOC_G_TUNER: {
1058 struct v4l2_tuner *vt = arg;
1060 if (vt->index != 0)
1061 return -EINVAL;
1063 memset(vt, 0, sizeof(*vt));
1064 ivtv_call_i2c_clients(itv, VIDIOC_G_TUNER, vt);
1066 if (test_bit(IVTV_F_I_RADIO_USER, &itv->i_flags)) {
1067 strcpy(vt->name, "ivtv Radio Tuner");
1068 vt->type = V4L2_TUNER_RADIO;
1069 } else {
1070 strcpy(vt->name, "ivtv TV Tuner");
1071 vt->type = V4L2_TUNER_ANALOG_TV;
1073 break;
1076 case VIDIOC_G_SLICED_VBI_CAP: {
1077 struct v4l2_sliced_vbi_cap *cap = arg;
1078 int set = itv->is_50hz ? V4L2_SLICED_VBI_625 : V4L2_SLICED_VBI_525;
1079 int f, l;
1080 enum v4l2_buf_type type = cap->type;
1082 memset(cap, 0, sizeof(*cap));
1083 cap->type = type;
1084 if (type == V4L2_BUF_TYPE_SLICED_VBI_CAPTURE) {
1085 for (f = 0; f < 2; f++) {
1086 for (l = 0; l < 24; l++) {
1087 if (valid_service_line(f, l, itv->is_50hz)) {
1088 cap->service_lines[f][l] = set;
1092 return 0;
1094 if (type == V4L2_BUF_TYPE_SLICED_VBI_OUTPUT) {
1095 if (!(itv->v4l2_cap & V4L2_CAP_SLICED_VBI_OUTPUT))
1096 return -EINVAL;
1097 if (itv->is_60hz) {
1098 cap->service_lines[0][21] = V4L2_SLICED_CAPTION_525;
1099 cap->service_lines[1][21] = V4L2_SLICED_CAPTION_525;
1100 } else {
1101 cap->service_lines[0][23] = V4L2_SLICED_WSS_625;
1102 cap->service_lines[0][16] = V4L2_SLICED_VPS;
1104 return 0;
1106 return -EINVAL;
1109 case VIDIOC_G_ENC_INDEX: {
1110 struct v4l2_enc_idx *idx = arg;
1111 struct v4l2_enc_idx_entry *e = idx->entry;
1112 int entries;
1113 int i;
1115 entries = (itv->pgm_info_write_idx + IVTV_MAX_PGM_INDEX - itv->pgm_info_read_idx) %
1116 IVTV_MAX_PGM_INDEX;
1117 if (entries > V4L2_ENC_IDX_ENTRIES)
1118 entries = V4L2_ENC_IDX_ENTRIES;
1119 idx->entries = 0;
1120 for (i = 0; i < entries; i++) {
1121 *e = itv->pgm_info[(itv->pgm_info_read_idx + i) % IVTV_MAX_PGM_INDEX];
1122 if ((e->flags & V4L2_ENC_IDX_FRAME_MASK) <= V4L2_ENC_IDX_FRAME_B) {
1123 idx->entries++;
1124 e++;
1127 itv->pgm_info_read_idx = (itv->pgm_info_read_idx + idx->entries) % IVTV_MAX_PGM_INDEX;
1128 break;
1131 case VIDIOC_ENCODER_CMD:
1132 case VIDIOC_TRY_ENCODER_CMD: {
1133 struct v4l2_encoder_cmd *enc = arg;
1134 int try = cmd == VIDIOC_TRY_ENCODER_CMD;
1136 memset(&enc->raw, 0, sizeof(enc->raw));
1137 switch (enc->cmd) {
1138 case V4L2_ENC_CMD_START:
1139 IVTV_DEBUG_IOCTL("V4L2_ENC_CMD_START\n");
1140 enc->flags = 0;
1141 if (try)
1142 return 0;
1143 return ivtv_start_capture(id);
1145 case V4L2_ENC_CMD_STOP:
1146 IVTV_DEBUG_IOCTL("V4L2_ENC_CMD_STOP\n");
1147 enc->flags &= V4L2_ENC_CMD_STOP_AT_GOP_END;
1148 if (try)
1149 return 0;
1150 ivtv_stop_capture(id, enc->flags & V4L2_ENC_CMD_STOP_AT_GOP_END);
1151 return 0;
1153 case V4L2_ENC_CMD_PAUSE:
1154 IVTV_DEBUG_IOCTL("V4L2_ENC_CMD_PAUSE\n");
1155 enc->flags = 0;
1156 if (try)
1157 return 0;
1158 if (!atomic_read(&itv->capturing))
1159 return -EPERM;
1160 if (test_and_set_bit(IVTV_F_I_ENC_PAUSED, &itv->i_flags))
1161 return 0;
1162 ivtv_mute(itv);
1163 ivtv_vapi(itv, CX2341X_ENC_PAUSE_ENCODER, 1, 0);
1164 break;
1166 case V4L2_ENC_CMD_RESUME:
1167 IVTV_DEBUG_IOCTL("V4L2_ENC_CMD_RESUME\n");
1168 enc->flags = 0;
1169 if (try)
1170 return 0;
1171 if (!atomic_read(&itv->capturing))
1172 return -EPERM;
1173 if (!test_and_clear_bit(IVTV_F_I_ENC_PAUSED, &itv->i_flags))
1174 return 0;
1175 ivtv_vapi(itv, CX2341X_ENC_PAUSE_ENCODER, 1, 1);
1176 ivtv_unmute(itv);
1177 break;
1178 default:
1179 IVTV_DEBUG_IOCTL("Unknown cmd %d\n", enc->cmd);
1180 return -EINVAL;
1182 break;
1185 case VIDIOC_G_FBUF: {
1186 struct v4l2_framebuffer *fb = arg;
1187 int pixfmt;
1188 static u32 pixel_format[16] = {
1189 V4L2_PIX_FMT_PAL8, /* Uses a 256-entry RGB colormap */
1190 V4L2_PIX_FMT_RGB565,
1191 V4L2_PIX_FMT_RGB555,
1192 V4L2_PIX_FMT_RGB444,
1193 V4L2_PIX_FMT_RGB32,
1197 V4L2_PIX_FMT_PAL8, /* Uses a 256-entry YUV colormap */
1198 V4L2_PIX_FMT_YUV565,
1199 V4L2_PIX_FMT_YUV555,
1200 V4L2_PIX_FMT_YUV444,
1201 V4L2_PIX_FMT_YUV32,
1207 memset(fb, 0, sizeof(*fb));
1208 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT_OVERLAY))
1209 return -EINVAL;
1210 fb->capability = V4L2_FBUF_CAP_EXTERNOVERLAY | V4L2_FBUF_CAP_CHROMAKEY |
1211 V4L2_FBUF_CAP_GLOBAL_ALPHA;
1212 ivtv_vapi_result(itv, data, CX2341X_OSD_GET_STATE, 0);
1213 data[0] |= (read_reg(0x2a00) >> 7) & 0x40;
1214 pixfmt = (data[0] >> 3) & 0xf;
1215 fb->fmt.pixelformat = pixel_format[pixfmt];
1216 fb->fmt.width = itv->osd_rect.width;
1217 fb->fmt.height = itv->osd_rect.height;
1218 fb->base = (void *)itv->osd_video_pbase;
1219 if (itv->osd_chroma_key_state)
1220 fb->flags |= V4L2_FBUF_FLAG_CHROMAKEY;
1221 if (itv->osd_global_alpha_state)
1222 fb->flags |= V4L2_FBUF_FLAG_GLOBAL_ALPHA;
1223 pixfmt &= 7;
1224 /* no local alpha for RGB565 or unknown formats */
1225 if (pixfmt == 1 || pixfmt > 4)
1226 break;
1227 /* 16-bit formats have inverted local alpha */
1228 if (pixfmt == 2 || pixfmt == 3)
1229 fb->capability |= V4L2_FBUF_CAP_LOCAL_INV_ALPHA;
1230 else
1231 fb->capability |= V4L2_FBUF_CAP_LOCAL_ALPHA;
1232 if (itv->osd_local_alpha_state) {
1233 /* 16-bit formats have inverted local alpha */
1234 if (pixfmt == 2 || pixfmt == 3)
1235 fb->flags |= V4L2_FBUF_FLAG_LOCAL_INV_ALPHA;
1236 else
1237 fb->flags |= V4L2_FBUF_FLAG_LOCAL_ALPHA;
1239 break;
1242 case VIDIOC_S_FBUF: {
1243 struct v4l2_framebuffer *fb = arg;
1245 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT_OVERLAY))
1246 return -EINVAL;
1247 itv->osd_global_alpha_state = (fb->flags & V4L2_FBUF_FLAG_GLOBAL_ALPHA) != 0;
1248 itv->osd_local_alpha_state =
1249 (fb->flags & (V4L2_FBUF_FLAG_LOCAL_ALPHA|V4L2_FBUF_FLAG_LOCAL_INV_ALPHA)) != 0;
1250 itv->osd_chroma_key_state = (fb->flags & V4L2_FBUF_FLAG_CHROMAKEY) != 0;
1251 ivtv_set_osd_alpha(itv);
1252 break;
1255 case VIDIOC_OVERLAY: {
1256 int *on = arg;
1258 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT_OVERLAY))
1259 return -EINVAL;
1260 ivtv_vapi(itv, CX2341X_OSD_SET_STATE, 1, *on != 0);
1261 break;
1264 case VIDIOC_LOG_STATUS:
1266 int has_output = itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT;
1267 struct v4l2_input vidin;
1268 struct v4l2_audio audin;
1269 int i;
1271 IVTV_INFO("================= START STATUS CARD #%d =================\n", itv->num);
1272 IVTV_INFO("Version: %s Card: %s\n", IVTV_VERSION, itv->card_name);
1273 if (itv->hw_flags & IVTV_HW_TVEEPROM) {
1274 struct tveeprom tv;
1276 ivtv_read_eeprom(itv, &tv);
1278 ivtv_call_i2c_clients(itv, VIDIOC_LOG_STATUS, NULL);
1279 ivtv_get_input(itv, itv->active_input, &vidin);
1280 ivtv_get_audio_input(itv, itv->audio_input, &audin);
1281 IVTV_INFO("Video Input: %s\n", vidin.name);
1282 IVTV_INFO("Audio Input: %s%s\n", audin.name,
1283 (itv->dualwatch_stereo_mode & ~0x300) == 0x200 ? " (Bilingual)" : "");
1284 if (has_output) {
1285 struct v4l2_output vidout;
1286 struct v4l2_audioout audout;
1287 int mode = itv->output_mode;
1288 static const char * const output_modes[5] = {
1289 "None",
1290 "MPEG Streaming",
1291 "YUV Streaming",
1292 "YUV Frames",
1293 "Passthrough",
1295 static const char * const audio_modes[5] = {
1296 "Stereo",
1297 "Left",
1298 "Right",
1299 "Mono",
1300 "Swapped"
1302 static const char * const alpha_mode[4] = {
1303 "None",
1304 "Global",
1305 "Local",
1306 "Global and Local"
1308 static const char * const pixel_format[16] = {
1309 "ARGB Indexed",
1310 "RGB 5:6:5",
1311 "ARGB 1:5:5:5",
1312 "ARGB 1:4:4:4",
1313 "ARGB 8:8:8:8",
1314 "5",
1315 "6",
1316 "7",
1317 "AYUV Indexed",
1318 "YUV 5:6:5",
1319 "AYUV 1:5:5:5",
1320 "AYUV 1:4:4:4",
1321 "AYUV 8:8:8:8",
1322 "13",
1323 "14",
1324 "15",
1327 ivtv_get_output(itv, itv->active_output, &vidout);
1328 ivtv_get_audio_output(itv, 0, &audout);
1329 IVTV_INFO("Video Output: %s\n", vidout.name);
1330 IVTV_INFO("Audio Output: %s (Stereo/Bilingual: %s/%s)\n", audout.name,
1331 audio_modes[itv->audio_stereo_mode],
1332 audio_modes[itv->audio_bilingual_mode]);
1333 if (mode < 0 || mode > OUT_PASSTHROUGH)
1334 mode = OUT_NONE;
1335 IVTV_INFO("Output Mode: %s\n", output_modes[mode]);
1336 ivtv_vapi_result(itv, data, CX2341X_OSD_GET_STATE, 0);
1337 data[0] |= (read_reg(0x2a00) >> 7) & 0x40;
1338 IVTV_INFO("Overlay: %s, Alpha: %s, Pixel Format: %s\n",
1339 data[0] & 1 ? "On" : "Off",
1340 alpha_mode[(data[0] >> 1) & 0x3],
1341 pixel_format[(data[0] >> 3) & 0xf]);
1343 IVTV_INFO("Tuner: %s\n",
1344 test_bit(IVTV_F_I_RADIO_USER, &itv->i_flags) ? "Radio" : "TV");
1345 cx2341x_log_status(&itv->params, itv->name);
1346 IVTV_INFO("Status flags: 0x%08lx\n", itv->i_flags);
1347 for (i = 0; i < IVTV_MAX_STREAMS; i++) {
1348 struct ivtv_stream *s = &itv->streams[i];
1350 if (s->v4l2dev == NULL || s->buffers == 0)
1351 continue;
1352 IVTV_INFO("Stream %s: status 0x%04lx, %d%% of %d KiB (%d buffers) in use\n", s->name, s->s_flags,
1353 (s->buffers - s->q_free.buffers) * 100 / s->buffers,
1354 (s->buffers * s->buf_size) / 1024, s->buffers);
1356 IVTV_INFO("Read MPG/VBI: %lld/%lld bytes\n", (long long)itv->mpg_data_received, (long long)itv->vbi_data_inserted);
1357 IVTV_INFO("================== END STATUS CARD #%d ==================\n", itv->num);
1358 break;
1361 default:
1362 return -EINVAL;
1364 return 0;
1367 static int ivtv_decoder_ioctls(struct file *filp, unsigned int cmd, void *arg)
1369 struct ivtv_open_id *id = (struct ivtv_open_id *)filp->private_data;
1370 struct ivtv *itv = id->itv;
1371 int nonblocking = filp->f_flags & O_NONBLOCK;
1372 struct ivtv_stream *s = &itv->streams[id->type];
1374 switch (cmd) {
1375 case IVTV_IOC_DMA_FRAME: {
1376 struct ivtv_dma_frame *args = arg;
1378 IVTV_DEBUG_IOCTL("IVTV_IOC_DMA_FRAME\n");
1379 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
1380 return -EINVAL;
1381 if (args->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
1382 return -EINVAL;
1383 if (itv->output_mode == OUT_UDMA_YUV && args->y_source == NULL)
1384 return 0;
1385 if (ivtv_claim_stream(id, id->type)) {
1386 return -EBUSY;
1388 if (ivtv_set_output_mode(itv, OUT_UDMA_YUV) != OUT_UDMA_YUV) {
1389 ivtv_release_stream(s);
1390 return -EBUSY;
1392 /* Mark that this file handle started the UDMA_YUV mode */
1393 id->yuv_frames = 1;
1394 if (args->y_source == NULL)
1395 return 0;
1396 return ivtv_yuv_prep_frame(itv, args);
1399 case VIDEO_GET_PTS: {
1400 u32 data[CX2341X_MBOX_MAX_DATA];
1401 u64 *pts = arg;
1403 IVTV_DEBUG_IOCTL("VIDEO_GET_PTS\n");
1404 if (s->type < IVTV_DEC_STREAM_TYPE_MPG) {
1405 *pts = s->dma_pts;
1406 break;
1408 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
1409 return -EINVAL;
1411 if (test_bit(IVTV_F_I_VALID_DEC_TIMINGS, &itv->i_flags)) {
1412 *pts = (u64) ((u64)itv->last_dec_timing[2] << 32) |
1413 (u64)itv->last_dec_timing[1];
1414 break;
1416 *pts = 0;
1417 if (atomic_read(&itv->decoding)) {
1418 if (ivtv_api(itv, CX2341X_DEC_GET_TIMING_INFO, 5, data)) {
1419 IVTV_DEBUG_WARN("GET_TIMING: couldn't read clock\n");
1420 return -EIO;
1422 memcpy(itv->last_dec_timing, data, sizeof(itv->last_dec_timing));
1423 set_bit(IVTV_F_I_VALID_DEC_TIMINGS, &itv->i_flags);
1424 *pts = (u64) ((u64) data[2] << 32) | (u64) data[1];
1425 /*timing->scr = (u64) (((u64) data[4] << 32) | (u64) (data[3]));*/
1427 break;
1430 case VIDEO_GET_FRAME_COUNT: {
1431 u32 data[CX2341X_MBOX_MAX_DATA];
1432 u64 *frame = arg;
1434 IVTV_DEBUG_IOCTL("VIDEO_GET_FRAME_COUNT\n");
1435 if (s->type < IVTV_DEC_STREAM_TYPE_MPG) {
1436 *frame = 0;
1437 break;
1439 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
1440 return -EINVAL;
1442 if (test_bit(IVTV_F_I_VALID_DEC_TIMINGS, &itv->i_flags)) {
1443 *frame = itv->last_dec_timing[0];
1444 break;
1446 *frame = 0;
1447 if (atomic_read(&itv->decoding)) {
1448 if (ivtv_api(itv, CX2341X_DEC_GET_TIMING_INFO, 5, data)) {
1449 IVTV_DEBUG_WARN("GET_TIMING: couldn't read clock\n");
1450 return -EIO;
1452 memcpy(itv->last_dec_timing, data, sizeof(itv->last_dec_timing));
1453 set_bit(IVTV_F_I_VALID_DEC_TIMINGS, &itv->i_flags);
1454 *frame = data[0];
1456 break;
1459 case VIDEO_PLAY: {
1460 struct video_command vc;
1462 IVTV_DEBUG_IOCTL("VIDEO_PLAY\n");
1463 memset(&vc, 0, sizeof(vc));
1464 vc.cmd = VIDEO_CMD_PLAY;
1465 return ivtv_video_command(itv, id, &vc, 0);
1468 case VIDEO_STOP: {
1469 struct video_command vc;
1471 IVTV_DEBUG_IOCTL("VIDEO_STOP\n");
1472 memset(&vc, 0, sizeof(vc));
1473 vc.cmd = VIDEO_CMD_STOP;
1474 vc.flags = VIDEO_CMD_STOP_TO_BLACK | VIDEO_CMD_STOP_IMMEDIATELY;
1475 return ivtv_video_command(itv, id, &vc, 0);
1478 case VIDEO_FREEZE: {
1479 struct video_command vc;
1481 IVTV_DEBUG_IOCTL("VIDEO_FREEZE\n");
1482 memset(&vc, 0, sizeof(vc));
1483 vc.cmd = VIDEO_CMD_FREEZE;
1484 return ivtv_video_command(itv, id, &vc, 0);
1487 case VIDEO_CONTINUE: {
1488 struct video_command vc;
1490 IVTV_DEBUG_IOCTL("VIDEO_CONTINUE\n");
1491 memset(&vc, 0, sizeof(vc));
1492 vc.cmd = VIDEO_CMD_CONTINUE;
1493 return ivtv_video_command(itv, id, &vc, 0);
1496 case VIDEO_COMMAND:
1497 case VIDEO_TRY_COMMAND: {
1498 struct video_command *vc = arg;
1499 int try = (cmd == VIDEO_TRY_COMMAND);
1501 if (try)
1502 IVTV_DEBUG_IOCTL("VIDEO_TRY_COMMAND %d\n", vc->cmd);
1503 else
1504 IVTV_DEBUG_IOCTL("VIDEO_COMMAND %d\n", vc->cmd);
1505 return ivtv_video_command(itv, id, vc, try);
1508 case VIDEO_GET_EVENT: {
1509 struct video_event *ev = arg;
1510 DEFINE_WAIT(wait);
1512 IVTV_DEBUG_IOCTL("VIDEO_GET_EVENT\n");
1513 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
1514 return -EINVAL;
1515 memset(ev, 0, sizeof(*ev));
1516 set_bit(IVTV_F_I_EV_VSYNC_ENABLED, &itv->i_flags);
1518 while (1) {
1519 if (test_and_clear_bit(IVTV_F_I_EV_DEC_STOPPED, &itv->i_flags))
1520 ev->type = VIDEO_EVENT_DECODER_STOPPED;
1521 else if (test_and_clear_bit(IVTV_F_I_EV_VSYNC, &itv->i_flags)) {
1522 ev->type = VIDEO_EVENT_VSYNC;
1523 ev->u.vsync_field = test_bit(IVTV_F_I_EV_VSYNC_FIELD, &itv->i_flags) ?
1524 VIDEO_VSYNC_FIELD_ODD : VIDEO_VSYNC_FIELD_EVEN;
1525 if (itv->output_mode == OUT_UDMA_YUV &&
1526 (itv->yuv_info.lace_mode & IVTV_YUV_MODE_MASK) ==
1527 IVTV_YUV_MODE_PROGRESSIVE) {
1528 ev->u.vsync_field = VIDEO_VSYNC_FIELD_PROGRESSIVE;
1531 if (ev->type)
1532 return 0;
1533 if (nonblocking)
1534 return -EAGAIN;
1535 /* Wait for event. Note that serialize_lock is locked,
1536 so to allow other processes to access the driver while
1537 we are waiting unlock first and later lock again. */
1538 mutex_unlock(&itv->serialize_lock);
1539 prepare_to_wait(&itv->event_waitq, &wait, TASK_INTERRUPTIBLE);
1540 if ((itv->i_flags & (IVTV_F_I_EV_DEC_STOPPED|IVTV_F_I_EV_VSYNC)) == 0)
1541 schedule();
1542 finish_wait(&itv->event_waitq, &wait);
1543 mutex_lock(&itv->serialize_lock);
1544 if (signal_pending(current)) {
1545 /* return if a signal was received */
1546 IVTV_DEBUG_INFO("User stopped wait for event\n");
1547 return -EINTR;
1550 break;
1553 default:
1554 return -EINVAL;
1556 return 0;
1559 static int ivtv_v4l2_do_ioctl(struct inode *inode, struct file *filp,
1560 unsigned int cmd, void *arg)
1562 struct ivtv_open_id *id = (struct ivtv_open_id *)filp->private_data;
1563 struct ivtv *itv = id->itv;
1564 int ret;
1566 /* check priority */
1567 switch (cmd) {
1568 case VIDIOC_S_CTRL:
1569 case VIDIOC_S_STD:
1570 case VIDIOC_S_INPUT:
1571 case VIDIOC_S_OUTPUT:
1572 case VIDIOC_S_TUNER:
1573 case VIDIOC_S_FREQUENCY:
1574 case VIDIOC_S_FMT:
1575 case VIDIOC_S_CROP:
1576 case VIDIOC_S_AUDIO:
1577 case VIDIOC_S_AUDOUT:
1578 case VIDIOC_S_EXT_CTRLS:
1579 case VIDIOC_S_FBUF:
1580 case VIDIOC_OVERLAY:
1581 ret = v4l2_prio_check(&itv->prio, &id->prio);
1582 if (ret)
1583 return ret;
1586 switch (cmd) {
1587 case VIDIOC_DBG_G_REGISTER:
1588 case VIDIOC_DBG_S_REGISTER:
1589 case VIDIOC_G_CHIP_IDENT:
1590 case VIDIOC_INT_S_AUDIO_ROUTING:
1591 case VIDIOC_INT_RESET:
1592 if (ivtv_debug & IVTV_DBGFLG_IOCTL) {
1593 printk(KERN_INFO "ivtv%d ioctl: ", itv->num);
1594 v4l_printk_ioctl(cmd);
1596 return ivtv_debug_ioctls(filp, cmd, arg);
1598 case VIDIOC_G_PRIORITY:
1599 case VIDIOC_S_PRIORITY:
1600 case VIDIOC_QUERYCAP:
1601 case VIDIOC_ENUMINPUT:
1602 case VIDIOC_G_INPUT:
1603 case VIDIOC_S_INPUT:
1604 case VIDIOC_ENUMOUTPUT:
1605 case VIDIOC_G_OUTPUT:
1606 case VIDIOC_S_OUTPUT:
1607 case VIDIOC_G_FMT:
1608 case VIDIOC_S_FMT:
1609 case VIDIOC_TRY_FMT:
1610 case VIDIOC_ENUM_FMT:
1611 case VIDIOC_CROPCAP:
1612 case VIDIOC_G_CROP:
1613 case VIDIOC_S_CROP:
1614 case VIDIOC_G_FREQUENCY:
1615 case VIDIOC_S_FREQUENCY:
1616 case VIDIOC_ENUMSTD:
1617 case VIDIOC_G_STD:
1618 case VIDIOC_S_STD:
1619 case VIDIOC_S_TUNER:
1620 case VIDIOC_G_TUNER:
1621 case VIDIOC_ENUMAUDIO:
1622 case VIDIOC_S_AUDIO:
1623 case VIDIOC_G_AUDIO:
1624 case VIDIOC_ENUMAUDOUT:
1625 case VIDIOC_S_AUDOUT:
1626 case VIDIOC_G_AUDOUT:
1627 case VIDIOC_G_SLICED_VBI_CAP:
1628 case VIDIOC_LOG_STATUS:
1629 case VIDIOC_G_ENC_INDEX:
1630 case VIDIOC_ENCODER_CMD:
1631 case VIDIOC_TRY_ENCODER_CMD:
1632 case VIDIOC_G_FBUF:
1633 case VIDIOC_S_FBUF:
1634 case VIDIOC_OVERLAY:
1635 if (ivtv_debug & IVTV_DBGFLG_IOCTL) {
1636 printk(KERN_INFO "ivtv%d ioctl: ", itv->num);
1637 v4l_printk_ioctl(cmd);
1639 return ivtv_v4l2_ioctls(itv, filp, cmd, arg);
1641 case VIDIOC_QUERYMENU:
1642 case VIDIOC_QUERYCTRL:
1643 case VIDIOC_S_CTRL:
1644 case VIDIOC_G_CTRL:
1645 case VIDIOC_S_EXT_CTRLS:
1646 case VIDIOC_G_EXT_CTRLS:
1647 case VIDIOC_TRY_EXT_CTRLS:
1648 if (ivtv_debug & IVTV_DBGFLG_IOCTL) {
1649 printk(KERN_INFO "ivtv%d ioctl: ", itv->num);
1650 v4l_printk_ioctl(cmd);
1652 return ivtv_control_ioctls(itv, cmd, arg);
1654 case IVTV_IOC_DMA_FRAME:
1655 case VIDEO_GET_PTS:
1656 case VIDEO_GET_FRAME_COUNT:
1657 case VIDEO_GET_EVENT:
1658 case VIDEO_PLAY:
1659 case VIDEO_STOP:
1660 case VIDEO_FREEZE:
1661 case VIDEO_CONTINUE:
1662 case VIDEO_COMMAND:
1663 case VIDEO_TRY_COMMAND:
1664 return ivtv_decoder_ioctls(filp, cmd, arg);
1666 case 0x00005401: /* Handle isatty() calls */
1667 return -EINVAL;
1668 default:
1669 return v4l_compat_translate_ioctl(inode, filp, cmd, arg,
1670 ivtv_v4l2_do_ioctl);
1672 return 0;
1675 static int ivtv_serialized_ioctl(struct ivtv *itv, struct inode *inode, struct file *filp,
1676 unsigned int cmd, unsigned long arg)
1678 /* Filter dvb ioctls that cannot be handled by video_usercopy */
1679 switch (cmd) {
1680 case VIDEO_SELECT_SOURCE:
1681 IVTV_DEBUG_IOCTL("VIDEO_SELECT_SOURCE\n");
1682 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
1683 return -EINVAL;
1684 return ivtv_passthrough_mode(itv, arg == VIDEO_SOURCE_DEMUX);
1686 case AUDIO_SET_MUTE:
1687 IVTV_DEBUG_IOCTL("AUDIO_SET_MUTE\n");
1688 itv->speed_mute_audio = arg;
1689 return 0;
1691 case AUDIO_CHANNEL_SELECT:
1692 IVTV_DEBUG_IOCTL("AUDIO_CHANNEL_SELECT\n");
1693 if (arg > AUDIO_STEREO_SWAPPED)
1694 return -EINVAL;
1695 itv->audio_stereo_mode = arg;
1696 ivtv_vapi(itv, CX2341X_DEC_SET_AUDIO_MODE, 2, itv->audio_bilingual_mode, itv->audio_stereo_mode);
1697 return 0;
1699 case AUDIO_BILINGUAL_CHANNEL_SELECT:
1700 IVTV_DEBUG_IOCTL("AUDIO_BILINGUAL_CHANNEL_SELECT\n");
1701 if (arg > AUDIO_STEREO_SWAPPED)
1702 return -EINVAL;
1703 itv->audio_bilingual_mode = arg;
1704 ivtv_vapi(itv, CX2341X_DEC_SET_AUDIO_MODE, 2, itv->audio_bilingual_mode, itv->audio_stereo_mode);
1705 return 0;
1707 default:
1708 break;
1710 return video_usercopy(inode, filp, cmd, arg, ivtv_v4l2_do_ioctl);
1713 int ivtv_v4l2_ioctl(struct inode *inode, struct file *filp, unsigned int cmd,
1714 unsigned long arg)
1716 struct ivtv_open_id *id = (struct ivtv_open_id *)filp->private_data;
1717 struct ivtv *itv = id->itv;
1718 int res;
1720 mutex_lock(&itv->serialize_lock);
1721 res = ivtv_serialized_ioctl(itv, inode, filp, cmd, arg);
1722 mutex_unlock(&itv->serialize_lock);
1723 return res;