blob: 49b297e788dbe3e7b51b7e7179a68e94da5ea835 [file] [log] [blame]
Hans Verkuil1a0adaf2007-04-27 12:31:25 -03001/*
2 ioctl system call
3 Copyright (C) 2003-2004 Kevin Thayer <nufan_wfk at yahoo.com>
4 Copyright (C) 2005-2007 Hans Verkuil <hverkuil@xs4all.nl>
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21#include "ivtv-driver.h"
22#include "ivtv-version.h"
23#include "ivtv-mailbox.h"
24#include "ivtv-i2c.h"
25#include "ivtv-queue.h"
26#include "ivtv-fileops.h"
27#include "ivtv-vbi.h"
Hans Verkuil33c0fca2007-08-23 06:32:46 -030028#include "ivtv-routing.h"
Hans Verkuil1a0adaf2007-04-27 12:31:25 -030029#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>
40
Hans Verkuilfeb5bce2008-05-01 09:22:13 -030041u16 ivtv_service2vbi(int type)
Hans Verkuil1a0adaf2007-04-27 12:31:25 -030042{
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;
54 }
55}
56
57static int valid_service_line(int field, int line, int is_pal)
58{
59 return (is_pal && line >= 6 && (line != 23 || field == 0)) ||
60 (!is_pal && line >= 10 && line < 22);
61}
62
63static u16 select_service_from_set(int field, int line, u16 set, int is_pal)
64{
65 u16 valid_set = (is_pal ? V4L2_SLICED_VBI_625 : V4L2_SLICED_VBI_525);
66 int i;
67
68 set = set & valid_set;
69 if (set == 0 || !valid_service_line(field, line, is_pal)) {
70 return 0;
71 }
72 if (!is_pal) {
73 if (line == 21 && (set & V4L2_SLICED_CAPTION_525))
74 return V4L2_SLICED_CAPTION_525;
75 }
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;
83 }
84 for (i = 0; i < 32; i++) {
85 if ((1 << i) & set)
86 return 1 << i;
87 }
88 return 0;
89}
90
Hans Verkuilfeb5bce2008-05-01 09:22:13 -030091void ivtv_expand_service_set(struct v4l2_sliced_vbi_format *fmt, int is_pal)
Hans Verkuil1a0adaf2007-04-27 12:31:25 -030092{
93 u16 set = fmt->service_set;
94 int f, l;
95
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);
100 }
101 }
102}
103
104static int check_service_set(struct v4l2_sliced_vbi_format *fmt, int is_pal)
105{
106 int f, l;
107 u16 set = 0;
108
109 for (f = 0; f < 2; f++) {
110 for (l = 0; l < 24; l++) {
111 fmt->service_lines[f][l] = select_service_from_set(f, l, fmt->service_lines[f][l], is_pal);
112 set |= fmt->service_lines[f][l];
113 }
114 }
115 return set != 0;
116}
117
Hans Verkuilfeb5bce2008-05-01 09:22:13 -0300118u16 ivtv_get_service_set(struct v4l2_sliced_vbi_format *fmt)
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300119{
120 int f, l;
121 u16 set = 0;
122
123 for (f = 0; f < 2; f++) {
124 for (l = 0; l < 24; l++) {
125 set |= fmt->service_lines[f][l];
126 }
127 }
128 return set;
129}
130
131static 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" },
148};
149
150static const struct v4l2_standard ivtv_std_60hz =
151{
152 .frameperiod = {.numerator = 1001, .denominator = 30000},
153 .framelines = 525,
154};
155
156static const struct v4l2_standard ivtv_std_50hz =
157{
158 .frameperiod = {.numerator = 1, .denominator = 25},
159 .framelines = 625,
160};
161
162void ivtv_set_osd_alpha(struct ivtv *itv)
163{
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);
Hans Verkuilfd8b2812007-08-23 10:13:15 -0300166 ivtv_vapi(itv, CX2341X_OSD_SET_CHROMA_KEY, 2, itv->osd_chroma_key_state, itv->osd_chroma_key);
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300167}
168
169int ivtv_set_speed(struct ivtv *itv, int speed)
170{
171 u32 data[CX2341X_MBOX_MAX_DATA];
172 struct ivtv_stream *s;
173 int single_step = (speed == 1 || speed == -1);
174 DEFINE_WAIT(wait);
175
176 if (speed == 0) speed = 1000;
177
178 /* No change? */
179 if (speed == itv->speed && !single_step)
180 return 0;
181
182 s = &itv->streams[IVTV_DEC_STREAM_TYPE_MPG];
183
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;
189 }
190 if (single_step)
191 /* Need to change direction */
192 speed = speed < 0 ? -1000 : 1000;
193
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;
202
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);
207
208 /* If not decoding, just change speed setting */
209 if (atomic_read(&itv->decoding) > 0) {
210 int got_sig = 0;
211
212 /* Stop all DMA and decoding activity */
213 ivtv_vapi(itv, CX2341X_DEC_PAUSE_PLAYBACK, 1, 0);
214
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();
223 }
224 finish_wait(&itv->dma_waitq, &wait);
225 if (got_sig)
226 return -EINTR;
227
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]);
232 }
233 if (single_step) {
234 speed = (speed < 0) ? -1 : 1;
235 ivtv_vapi(itv, CX2341X_DEC_STEP_VIDEO, 1, 0);
236 }
237 itv->speed = speed;
238 return 0;
239}
240
241static int ivtv_validate_speed(int cur_speed, int new_speed)
242{
243 int fact = new_speed < 0 ? -1 : 1;
244 int s;
245
Hans Verkuil94dee762008-04-26 09:26:13 -0300246 if (cur_speed == 0)
247 cur_speed = 1000;
248 if (new_speed < 0)
249 new_speed = -new_speed;
250 if (cur_speed < 0)
251 cur_speed = -cur_speed;
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300252
253 if (cur_speed <= new_speed) {
Hans Verkuil94dee762008-04-26 09:26:13 -0300254 if (new_speed > 1500)
255 return fact * 2000;
256 if (new_speed > 1000)
257 return fact * 1500;
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300258 }
259 else {
Hans Verkuil94dee762008-04-26 09:26:13 -0300260 if (new_speed >= 2000)
261 return fact * 2000;
262 if (new_speed >= 1500)
263 return fact * 1500;
264 if (new_speed >= 1000)
265 return fact * 1000;
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300266 }
Hans Verkuil94dee762008-04-26 09:26:13 -0300267 if (new_speed == 0)
268 return 1000;
269 if (new_speed == 1 || new_speed == 1000)
270 return fact * new_speed;
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300271
272 s = new_speed;
273 new_speed = 1000 / new_speed;
274 if (1000 / cur_speed == new_speed)
275 new_speed += (cur_speed < s) ? -1 : 1;
276 if (new_speed > 60) return 1000 / (fact * 60);
277 return 1000 / (fact * new_speed);
278}
279
280static int ivtv_video_command(struct ivtv *itv, struct ivtv_open_id *id,
281 struct video_command *vc, int try)
282{
283 struct ivtv_stream *s = &itv->streams[IVTV_DEC_STREAM_TYPE_MPG];
284
285 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
286 return -EINVAL;
287
288 switch (vc->cmd) {
289 case VIDEO_CMD_PLAY: {
Hans Verkuil25415cf2007-03-10 18:29:48 -0300290 vc->flags = 0;
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300291 vc->play.speed = ivtv_validate_speed(itv->speed, vc->play.speed);
292 if (vc->play.speed < 0)
293 vc->play.format = VIDEO_PLAY_FMT_GOP;
294 if (try) break;
295
296 if (ivtv_set_output_mode(itv, OUT_MPG) != OUT_MPG)
297 return -EBUSY;
Hans Verkuilac425142007-07-22 08:46:38 -0300298 if (test_and_clear_bit(IVTV_F_I_DEC_PAUSED, &itv->i_flags)) {
299 /* forces ivtv_set_speed to be called */
300 itv->speed = 0;
301 }
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300302 return ivtv_start_decoding(id, vc->play.speed);
303 }
304
305 case VIDEO_CMD_STOP:
Hans Verkuil018ba852007-04-10 18:59:09 -0300306 vc->flags &= VIDEO_CMD_STOP_IMMEDIATELY|VIDEO_CMD_STOP_TO_BLACK;
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300307 if (vc->flags & VIDEO_CMD_STOP_IMMEDIATELY)
308 vc->stop.pts = 0;
309 if (try) break;
310 if (atomic_read(&itv->decoding) == 0)
311 return 0;
312 if (itv->output_mode != OUT_MPG)
313 return -EBUSY;
314
315 itv->output_mode = OUT_NONE;
316 return ivtv_stop_v4l2_decode_stream(s, vc->flags, vc->stop.pts);
317
318 case VIDEO_CMD_FREEZE:
Hans Verkuil018ba852007-04-10 18:59:09 -0300319 vc->flags &= VIDEO_CMD_FREEZE_TO_BLACK;
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300320 if (try) break;
321 if (itv->output_mode != OUT_MPG)
322 return -EBUSY;
323 if (atomic_read(&itv->decoding) > 0) {
324 ivtv_vapi(itv, CX2341X_DEC_PAUSE_PLAYBACK, 1,
325 (vc->flags & VIDEO_CMD_FREEZE_TO_BLACK) ? 1 : 0);
Hans Verkuilac425142007-07-22 08:46:38 -0300326 set_bit(IVTV_F_I_DEC_PAUSED, &itv->i_flags);
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300327 }
328 break;
329
330 case VIDEO_CMD_CONTINUE:
Hans Verkuil25415cf2007-03-10 18:29:48 -0300331 vc->flags = 0;
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300332 if (try) break;
333 if (itv->output_mode != OUT_MPG)
334 return -EBUSY;
Hans Verkuilac425142007-07-22 08:46:38 -0300335 if (test_and_clear_bit(IVTV_F_I_DEC_PAUSED, &itv->i_flags)) {
336 int speed = itv->speed;
337 itv->speed = 0;
338 return ivtv_start_decoding(id, speed);
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300339 }
340 break;
341
342 default:
343 return -EINVAL;
344 }
345 return 0;
346}
347
348static int ivtv_itvc(struct ivtv *itv, unsigned int cmd, void *arg)
349{
350 struct v4l2_register *regs = arg;
351 unsigned long flags;
352 volatile u8 __iomem *reg_start;
353
354 if (!capable(CAP_SYS_ADMIN))
355 return -EPERM;
356 if (regs->reg >= IVTV_REG_OFFSET && regs->reg < IVTV_REG_OFFSET + IVTV_REG_SIZE)
357 reg_start = itv->reg_mem - IVTV_REG_OFFSET;
358 else if (itv->has_cx23415 && regs->reg >= IVTV_DECODER_OFFSET &&
359 regs->reg < IVTV_DECODER_OFFSET + IVTV_DECODER_SIZE)
360 reg_start = itv->dec_mem - IVTV_DECODER_OFFSET;
361 else if (regs->reg >= 0 && regs->reg < IVTV_ENCODER_SIZE)
362 reg_start = itv->enc_mem;
363 else
364 return -EINVAL;
365
366 spin_lock_irqsave(&ivtv_cards_lock, flags);
367 if (cmd == VIDIOC_DBG_G_REGISTER) {
368 regs->val = readl(regs->reg + reg_start);
369 } else {
370 writel(regs->val, regs->reg + reg_start);
371 }
372 spin_unlock_irqrestore(&ivtv_cards_lock, flags);
373 return 0;
374}
375
Hans Verkuil3f038d82008-05-29 16:43:54 -0300376static int ivtv_g_fmt_sliced_vbi_out(struct file *file, void *fh, struct v4l2_format *fmt)
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300377{
Hans Verkuil3f038d82008-05-29 16:43:54 -0300378 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
379 struct v4l2_sliced_vbi_format *vbifmt = &fmt->fmt.sliced;
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300380
Hans Verkuil3f038d82008-05-29 16:43:54 -0300381 if (!(itv->v4l2_cap & V4L2_CAP_SLICED_VBI_OUTPUT))
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300382 return -EINVAL;
Hans Verkuil3f038d82008-05-29 16:43:54 -0300383 vbifmt->io_size = sizeof(struct v4l2_sliced_vbi_data) * 36;
384 if (itv->is_60hz) {
385 vbifmt->service_lines[0][21] = V4L2_SLICED_CAPTION_525;
386 vbifmt->service_lines[1][21] = V4L2_SLICED_CAPTION_525;
387 } else {
388 vbifmt->service_lines[0][23] = V4L2_SLICED_WSS_625;
389 vbifmt->service_lines[0][16] = V4L2_SLICED_VPS;
390 }
391 vbifmt->service_set = ivtv_get_service_set(vbifmt);
392 return 0;
393}
394
395static int ivtv_g_fmt_vid_cap(struct file *file, void *fh, struct v4l2_format *fmt)
396{
397 struct ivtv_open_id *id = fh;
398 struct ivtv *itv = id->itv;
399
400 fmt->fmt.pix.width = itv->params.width;
401 fmt->fmt.pix.height = itv->params.height;
402 fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
403 fmt->fmt.pix.field = V4L2_FIELD_INTERLACED;
404 if (id->type == IVTV_ENC_STREAM_TYPE_YUV ||
405 id->type == IVTV_DEC_STREAM_TYPE_YUV) {
406 fmt->fmt.pix.pixelformat = V4L2_PIX_FMT_HM12;
407 /* YUV size is (Y=(h*w) + UV=(h*(w/2))) */
408 fmt->fmt.pix.sizeimage =
409 fmt->fmt.pix.height * fmt->fmt.pix.width +
410 fmt->fmt.pix.height * (fmt->fmt.pix.width / 2);
411 } else {
412 fmt->fmt.pix.pixelformat = V4L2_PIX_FMT_MPEG;
413 fmt->fmt.pix.sizeimage = 128 * 1024;
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300414 }
415 return 0;
416}
417
Hans Verkuil3f038d82008-05-29 16:43:54 -0300418static int ivtv_g_fmt_vbi_cap(struct file *file, void *fh, struct v4l2_format *fmt)
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300419{
Hans Verkuil3f038d82008-05-29 16:43:54 -0300420 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
421
422 fmt->fmt.vbi.sampling_rate = 27000000;
423 fmt->fmt.vbi.offset = 248;
424 fmt->fmt.vbi.samples_per_line = itv->vbi.raw_decoder_line_size - 4;
425 fmt->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
426 fmt->fmt.vbi.start[0] = itv->vbi.start[0];
427 fmt->fmt.vbi.start[1] = itv->vbi.start[1];
428 fmt->fmt.vbi.count[0] = fmt->fmt.vbi.count[1] = itv->vbi.count;
429 return 0;
430}
431
432static int ivtv_g_fmt_sliced_vbi_cap(struct file *file, void *fh, struct v4l2_format *fmt)
433{
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300434 struct v4l2_sliced_vbi_format *vbifmt = &fmt->fmt.sliced;
Hans Verkuil3f038d82008-05-29 16:43:54 -0300435 struct ivtv_open_id *id = fh;
436 struct ivtv *itv = id->itv;
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300437
Hans Verkuil3f038d82008-05-29 16:43:54 -0300438 vbifmt->io_size = sizeof(struct v4l2_sliced_vbi_data) * 36;
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300439
Hans Verkuil3f038d82008-05-29 16:43:54 -0300440 if (id->type == IVTV_DEC_STREAM_TYPE_VBI) {
441 vbifmt->service_set = itv->is_50hz ? V4L2_SLICED_VBI_625 :
442 V4L2_SLICED_VBI_525;
443 ivtv_expand_service_set(vbifmt, itv->is_50hz);
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300444 return 0;
445 }
446
Hans Verkuil3f038d82008-05-29 16:43:54 -0300447 itv->video_dec_func(itv, VIDIOC_G_FMT, fmt);
448 vbifmt->service_set = ivtv_get_service_set(vbifmt);
449 return 0;
450}
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300451
Hans Verkuil3f038d82008-05-29 16:43:54 -0300452static int ivtv_g_fmt_vid_out(struct file *file, void *fh, struct v4l2_format *fmt)
453{
454 struct ivtv_open_id *id = fh;
455 struct ivtv *itv = id->itv;
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300456
Hans Verkuil3f038d82008-05-29 16:43:54 -0300457 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300458 return -EINVAL;
Hans Verkuil3f038d82008-05-29 16:43:54 -0300459 fmt->fmt.pix.width = itv->main_rect.width;
460 fmt->fmt.pix.height = itv->main_rect.height;
461 fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
462 fmt->fmt.pix.field = V4L2_FIELD_INTERLACED;
463 if (id->type == IVTV_DEC_STREAM_TYPE_YUV) {
464 switch (itv->yuv_info.lace_mode & IVTV_YUV_MODE_MASK) {
465 case IVTV_YUV_MODE_INTERLACED:
466 fmt->fmt.pix.field = (itv->yuv_info.lace_mode & IVTV_YUV_SYNC_MASK) ?
467 V4L2_FIELD_INTERLACED_BT : V4L2_FIELD_INTERLACED_TB;
468 break;
469 case IVTV_YUV_MODE_PROGRESSIVE:
470 fmt->fmt.pix.field = V4L2_FIELD_NONE;
471 break;
472 default:
473 fmt->fmt.pix.field = V4L2_FIELD_ANY;
474 break;
475 }
476 fmt->fmt.pix.pixelformat = V4L2_PIX_FMT_HM12;
477 fmt->fmt.pix.bytesperline = 720;
478 fmt->fmt.pix.width = itv->yuv_info.v4l2_src_w;
479 fmt->fmt.pix.height = itv->yuv_info.v4l2_src_h;
480 /* YUV size is (Y=(h*w) + UV=(h*(w/2))) */
481 fmt->fmt.pix.sizeimage =
482 1080 * ((fmt->fmt.pix.height + 31) & ~31);
483 } else if (id->type == IVTV_ENC_STREAM_TYPE_YUV) {
484 fmt->fmt.pix.pixelformat = V4L2_PIX_FMT_HM12;
485 /* YUV size is (Y=(h*w) + UV=(h*(w/2))) */
486 fmt->fmt.pix.sizeimage =
487 fmt->fmt.pix.height * fmt->fmt.pix.width +
488 fmt->fmt.pix.height * (fmt->fmt.pix.width / 2);
489 } else {
490 fmt->fmt.pix.pixelformat = V4L2_PIX_FMT_MPEG;
491 fmt->fmt.pix.sizeimage = 128 * 1024;
492 }
493 return 0;
494}
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300495
Hans Verkuil3f038d82008-05-29 16:43:54 -0300496static int ivtv_g_fmt_vid_out_overlay(struct file *file, void *fh, struct v4l2_format *fmt)
497{
498 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
499
500 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
501 return -EINVAL;
502 fmt->fmt.win.chromakey = itv->osd_chroma_key;
503 fmt->fmt.win.global_alpha = itv->osd_global_alpha;
504 return 0;
505}
506
507static int ivtv_try_fmt_sliced_vbi_out(struct file *file, void *fh, struct v4l2_format *fmt)
508{
509 return ivtv_g_fmt_sliced_vbi_out(file, fh, fmt);
510}
511
512static int ivtv_try_fmt_vid_cap(struct file *file, void *fh, struct v4l2_format *fmt)
513{
514 struct ivtv_open_id *id = fh;
515 struct ivtv *itv = id->itv;
516 int w = fmt->fmt.pix.width;
517 int h = fmt->fmt.pix.height;
518
519 w = min(w, 720);
520 w = max(w, 1);
521 h = min(h, itv->is_50hz ? 576 : 480);
522 h = max(h, 2);
523 ivtv_g_fmt_vid_cap(file, fh, fmt);
524 fmt->fmt.pix.width = w;
525 fmt->fmt.pix.height = h;
526 return 0;
527}
528
529static int ivtv_try_fmt_vbi_cap(struct file *file, void *fh, struct v4l2_format *fmt)
530{
531 return ivtv_g_fmt_vbi_cap(file, fh, fmt);
532}
533
534static int ivtv_try_fmt_sliced_vbi_cap(struct file *file, void *fh, struct v4l2_format *fmt)
535{
536 struct v4l2_sliced_vbi_format *vbifmt = &fmt->fmt.sliced;
537 struct ivtv_open_id *id = fh;
538 struct ivtv *itv = id->itv;
539
540 if (id->type == IVTV_DEC_STREAM_TYPE_VBI)
541 return ivtv_g_fmt_sliced_vbi_cap(file, fh, fmt);
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300542
543 /* set sliced VBI capture format */
544 vbifmt->io_size = sizeof(struct v4l2_sliced_vbi_data) * 36;
545 memset(vbifmt->reserved, 0, sizeof(vbifmt->reserved));
546
547 if (vbifmt->service_set)
Hans Verkuilfeb5bce2008-05-01 09:22:13 -0300548 ivtv_expand_service_set(vbifmt, itv->is_50hz);
Hans Verkuil3f038d82008-05-29 16:43:54 -0300549 check_service_set(vbifmt, itv->is_50hz);
Hans Verkuilfeb5bce2008-05-01 09:22:13 -0300550 vbifmt->service_set = ivtv_get_service_set(vbifmt);
Hans Verkuil3f038d82008-05-29 16:43:54 -0300551 return 0;
552}
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300553
Hans Verkuil3f038d82008-05-29 16:43:54 -0300554static int ivtv_try_fmt_vid_out(struct file *file, void *fh, struct v4l2_format *fmt)
555{
556 struct ivtv_open_id *id = fh;
557 s32 w, h;
558 int field;
559 int ret;
560
561 w = fmt->fmt.pix.width;
562 h = fmt->fmt.pix.height;
563 field = fmt->fmt.pix.field;
564 ret = ivtv_g_fmt_vid_out(file, fh, fmt);
565 fmt->fmt.pix.width = w;
566 fmt->fmt.pix.height = h;
567 if (!ret && id->type == IVTV_DEC_STREAM_TYPE_YUV) {
568 fmt->fmt.pix.field = field;
569 if (fmt->fmt.pix.width < 2)
570 fmt->fmt.pix.width = 2;
571 if (fmt->fmt.pix.width > 720)
572 fmt->fmt.pix.width = 720;
573 if (fmt->fmt.pix.height < 2)
574 fmt->fmt.pix.height = 2;
575 if (fmt->fmt.pix.height > 576)
576 fmt->fmt.pix.height = 576;
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300577 }
Hans Verkuil3f038d82008-05-29 16:43:54 -0300578 return ret;
579}
580
581static int ivtv_try_fmt_vid_out_overlay(struct file *file, void *fh, struct v4l2_format *fmt)
582{
583 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
584
585 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
586 return -EINVAL;
587 return 0;
588}
589
590static int ivtv_s_fmt_sliced_vbi_out(struct file *file, void *fh, struct v4l2_format *fmt)
591{
592 return ivtv_g_fmt_sliced_vbi_out(file, fh, fmt);
593}
594
595static int ivtv_s_fmt_vid_cap(struct file *file, void *fh, struct v4l2_format *fmt)
596{
597 struct ivtv_open_id *id = fh;
598 struct ivtv *itv = id->itv;
599 struct cx2341x_mpeg_params *p = &itv->params;
600 int w = fmt->fmt.pix.width;
601 int h = fmt->fmt.pix.height;
602 int ret = ivtv_try_fmt_vid_cap(file, fh, fmt);
603
604 if (ret)
605 return ret;
606
607 if (p->width == w && p->height == h)
608 return 0;
609
610 if (atomic_read(&itv->capturing) > 0)
611 return -EBUSY;
612
613 p->width = w;
614 p->height = h;
615 if (w != 720 || h != (itv->is_50hz ? 576 : 480))
616 p->video_temporal_filter = 0;
617 else
618 p->video_temporal_filter = 8;
619 if (p->video_encoding == V4L2_MPEG_VIDEO_ENCODING_MPEG_1)
620 fmt->fmt.pix.width /= 2;
621 itv->video_dec_func(itv, VIDIOC_S_FMT, fmt);
622 return ivtv_g_fmt_vid_cap(file, fh, fmt);
623}
624
625static int ivtv_s_fmt_vbi_cap(struct file *file, void *fh, struct v4l2_format *fmt)
626{
627 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
628
629 itv->vbi.sliced_in->service_set = 0;
630 itv->video_dec_func(itv, VIDIOC_S_FMT, &itv->vbi.in);
631 return ivtv_g_fmt_vbi_cap(file, fh, fmt);
632}
633
634static int ivtv_s_fmt_sliced_vbi_cap(struct file *file, void *fh, struct v4l2_format *fmt)
635{
636 struct v4l2_sliced_vbi_format *vbifmt = &fmt->fmt.sliced;
637 struct ivtv_open_id *id = fh;
638 struct ivtv *itv = id->itv;
639 int ret = ivtv_try_fmt_sliced_vbi_cap(file, fh, fmt);
640
641 if (ret || id->type == IVTV_DEC_STREAM_TYPE_VBI)
642 return ret;
643
644 if (check_service_set(vbifmt, itv->is_50hz) == 0)
645 return -EINVAL;
646 if (atomic_read(&itv->capturing) > 0)
647 return -EBUSY;
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300648 itv->video_dec_func(itv, VIDIOC_S_FMT, fmt);
649 memcpy(itv->vbi.sliced_in, vbifmt, sizeof(*itv->vbi.sliced_in));
650 return 0;
651}
652
Hans Verkuil3f038d82008-05-29 16:43:54 -0300653static int ivtv_s_fmt_vid_out(struct file *file, void *fh, struct v4l2_format *fmt)
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300654{
Hans Verkuil3f038d82008-05-29 16:43:54 -0300655 struct ivtv_open_id *id = fh;
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300656 struct ivtv *itv = id->itv;
Hans Verkuil3f038d82008-05-29 16:43:54 -0300657 struct yuv_playback_info *yi = &itv->yuv_info;
658 int ret = ivtv_try_fmt_vid_out(file, fh, fmt);
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300659
Hans Verkuil3f038d82008-05-29 16:43:54 -0300660 if (ret)
661 return ret;
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300662
Hans Verkuil3f038d82008-05-29 16:43:54 -0300663 if (id->type != IVTV_DEC_STREAM_TYPE_YUV)
664 return 0;
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300665
Hans Verkuil3f038d82008-05-29 16:43:54 -0300666 /* Return now if we already have some frame data */
667 if (yi->stream_size)
668 return -EBUSY;
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300669
Hans Verkuil3f038d82008-05-29 16:43:54 -0300670 yi->v4l2_src_w = fmt->fmt.pix.width;
671 yi->v4l2_src_h = fmt->fmt.pix.height;
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300672
Hans Verkuil3f038d82008-05-29 16:43:54 -0300673 switch (fmt->fmt.pix.field) {
674 case V4L2_FIELD_NONE:
675 yi->lace_mode = IVTV_YUV_MODE_PROGRESSIVE;
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300676 break;
Hans Verkuil3f038d82008-05-29 16:43:54 -0300677 case V4L2_FIELD_ANY:
678 yi->lace_mode = IVTV_YUV_MODE_AUTO;
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300679 break;
Hans Verkuil3f038d82008-05-29 16:43:54 -0300680 case V4L2_FIELD_INTERLACED_BT:
681 yi->lace_mode =
682 IVTV_YUV_MODE_INTERLACED|IVTV_YUV_SYNC_ODD;
683 break;
684 case V4L2_FIELD_INTERLACED_TB:
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300685 default:
Hans Verkuil3f038d82008-05-29 16:43:54 -0300686 yi->lace_mode = IVTV_YUV_MODE_INTERLACED;
687 break;
688 }
689 yi->lace_sync_field = (yi->lace_mode & IVTV_YUV_SYNC_MASK) == IVTV_YUV_SYNC_EVEN ? 0 : 1;
690
691 if (test_bit(IVTV_F_I_DEC_YUV, &itv->i_flags))
692 itv->dma_data_req_size =
693 1080 * ((yi->v4l2_src_h + 31) & ~31);
694
695 /* Force update of yuv registers */
696 yi->yuv_forced_update = 1;
697 return 0;
698}
699
700static int ivtv_s_fmt_vid_out_overlay(struct file *file, void *fh, struct v4l2_format *fmt)
701{
702 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
703 int ret = ivtv_try_fmt_vid_out_overlay(file, fh, fmt);
704
705 if (ret == 0) {
706 itv->osd_chroma_key = fmt->fmt.win.chromakey;
707 itv->osd_global_alpha = fmt->fmt.win.global_alpha;
708 ivtv_set_osd_alpha(itv);
709 }
710 return ret;
711}
712
713static int ivtv_g_chip_ident(struct file *file, void *fh, struct v4l2_chip_ident *chip)
714{
715 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
716
717 chip->ident = V4L2_IDENT_NONE;
718 chip->revision = 0;
719 if (chip->match_type == V4L2_CHIP_MATCH_HOST) {
720 if (v4l2_chip_match_host(chip->match_type, chip->match_chip))
721 chip->ident = itv->has_cx23415 ? V4L2_IDENT_CX23415 : V4L2_IDENT_CX23416;
722 return 0;
723 }
724 if (chip->match_type == V4L2_CHIP_MATCH_I2C_DRIVER)
725 return ivtv_i2c_id(itv, chip->match_chip, VIDIOC_G_CHIP_IDENT, chip);
726 if (chip->match_type == V4L2_CHIP_MATCH_I2C_ADDR)
727 return ivtv_call_i2c_client(itv, chip->match_chip, VIDIOC_G_CHIP_IDENT, chip);
728 return -EINVAL;
729}
730
731static int ivtv_g_register(struct file *file, void *fh, struct v4l2_register *reg)
732{
733 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
734
735 if (v4l2_chip_match_host(reg->match_type, reg->match_chip))
736 return ivtv_itvc(itv, VIDIOC_DBG_G_REGISTER, reg);
737 if (reg->match_type == V4L2_CHIP_MATCH_I2C_DRIVER)
738 return ivtv_i2c_id(itv, reg->match_chip, VIDIOC_DBG_G_REGISTER, reg);
739 return ivtv_call_i2c_client(itv, reg->match_chip, VIDIOC_DBG_G_REGISTER, reg);
740}
741
742static int ivtv_s_register(struct file *file, void *fh, struct v4l2_register *reg)
743{
744 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
745
746 if (v4l2_chip_match_host(reg->match_type, reg->match_chip))
747 return ivtv_itvc(itv, VIDIOC_DBG_S_REGISTER, reg);
748 if (reg->match_type == V4L2_CHIP_MATCH_I2C_DRIVER)
749 return ivtv_i2c_id(itv, reg->match_chip, VIDIOC_DBG_S_REGISTER, reg);
750 return ivtv_call_i2c_client(itv, reg->match_chip, VIDIOC_DBG_S_REGISTER, reg);
751}
752
753static int ivtv_g_priority(struct file *file, void *fh, enum v4l2_priority *p)
754{
755 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
756
757 *p = v4l2_prio_max(&itv->prio);
758
759 return 0;
760}
761
762static int ivtv_s_priority(struct file *file, void *fh, enum v4l2_priority prio)
763{
764 struct ivtv_open_id *id = fh;
765 struct ivtv *itv = id->itv;
766
767 return v4l2_prio_change(&itv->prio, &id->prio, prio);
768}
769
770static int ivtv_querycap(struct file *file, void *fh, struct v4l2_capability *vcap)
771{
772 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
773
774 memset(vcap, 0, sizeof(*vcap));
775 strlcpy(vcap->driver, IVTV_DRIVER_NAME, sizeof(vcap->driver));
776 strlcpy(vcap->card, itv->card_name, sizeof(vcap->card));
777 strlcpy(vcap->bus_info, pci_name(itv->dev), sizeof(vcap->bus_info));
778 vcap->version = IVTV_DRIVER_VERSION; /* version */
779 vcap->capabilities = itv->v4l2_cap; /* capabilities */
780 /* reserved.. must set to 0! */
781 vcap->reserved[0] = vcap->reserved[1] =
782 vcap->reserved[2] = vcap->reserved[3] = 0;
783 return 0;
784}
785
786static int ivtv_enumaudio(struct file *file, void *fh, struct v4l2_audio *vin)
787{
788 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
789
790 return ivtv_get_audio_input(itv, vin->index, vin);
791}
792
793static int ivtv_g_audio(struct file *file, void *fh, struct v4l2_audio *vin)
794{
795 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
796
797 vin->index = itv->audio_input;
798 return ivtv_get_audio_input(itv, vin->index, vin);
799}
800
801static int ivtv_s_audio(struct file *file, void *fh, struct v4l2_audio *vout)
802{
803 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
804
805 if (vout->index >= itv->nof_audio_inputs)
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300806 return -EINVAL;
Hans Verkuil3f038d82008-05-29 16:43:54 -0300807
808 itv->audio_input = vout->index;
809 ivtv_audio_set_io(itv);
810
811 return 0;
812}
813
814static int ivtv_enumaudout(struct file *file, void *fh, struct v4l2_audioout *vin)
815{
816 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
817
818 /* set it to defaults from our table */
819 return ivtv_get_audio_output(itv, vin->index, vin);
820}
821
822static int ivtv_g_audout(struct file *file, void *fh, struct v4l2_audioout *vin)
823{
824 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
825
826 vin->index = 0;
827 return ivtv_get_audio_output(itv, vin->index, vin);
828}
829
830static int ivtv_s_audout(struct file *file, void *fh, struct v4l2_audioout *vout)
831{
832 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
833
834 return ivtv_get_audio_output(itv, vout->index, vout);
835}
836
837static int ivtv_enum_input(struct file *file, void *fh, struct v4l2_input *vin)
838{
839 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
840
841 /* set it to defaults from our table */
842 return ivtv_get_input(itv, vin->index, vin);
843}
844
845static int ivtv_enum_output(struct file *file, void *fh, struct v4l2_output *vout)
846{
847 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
848
849 return ivtv_get_output(itv, vout->index, vout);
850}
851
852static int ivtv_cropcap(struct file *file, void *fh, struct v4l2_cropcap *cropcap)
853{
854 struct ivtv_open_id *id = fh;
855 struct ivtv *itv = id->itv;
856 struct yuv_playback_info *yi = &itv->yuv_info;
857 int streamtype;
858
859 streamtype = id->type;
860
861 if (cropcap->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
862 return -EINVAL;
863 cropcap->bounds.top = cropcap->bounds.left = 0;
864 cropcap->bounds.width = 720;
865 if (cropcap->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
866 cropcap->bounds.height = itv->is_50hz ? 576 : 480;
867 cropcap->pixelaspect.numerator = itv->is_50hz ? 59 : 10;
868 cropcap->pixelaspect.denominator = itv->is_50hz ? 54 : 11;
869 } else if (streamtype == IVTV_DEC_STREAM_TYPE_YUV) {
870 if (yi->track_osd) {
871 cropcap->bounds.width = yi->osd_full_w;
872 cropcap->bounds.height = yi->osd_full_h;
873 } else {
874 cropcap->bounds.width = 720;
875 cropcap->bounds.height =
876 itv->is_out_50hz ? 576 : 480;
877 }
878 cropcap->pixelaspect.numerator = itv->is_out_50hz ? 59 : 10;
879 cropcap->pixelaspect.denominator = itv->is_out_50hz ? 54 : 11;
880 } else {
881 cropcap->bounds.height = itv->is_out_50hz ? 576 : 480;
882 cropcap->pixelaspect.numerator = itv->is_out_50hz ? 59 : 10;
883 cropcap->pixelaspect.denominator = itv->is_out_50hz ? 54 : 11;
884 }
885 cropcap->defrect = cropcap->bounds;
886 return 0;
887}
888
889static int ivtv_s_crop(struct file *file, void *fh, struct v4l2_crop *crop)
890{
891 struct ivtv_open_id *id = fh;
892 struct ivtv *itv = id->itv;
893 struct yuv_playback_info *yi = &itv->yuv_info;
894 int streamtype;
895
896 streamtype = id->type;
897
898 if (ivtv_debug & IVTV_DBGFLG_IOCTL) {
899 printk(KERN_INFO "ivtv%d ioctl: ", itv->num);
900 /* Should be replaced */
901 /* v4l_printk_ioctl(VIDIOC_S_CROP); */
902 }
903
904 if (crop->type == V4L2_BUF_TYPE_VIDEO_OUTPUT &&
905 (itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT)) {
906 if (streamtype == IVTV_DEC_STREAM_TYPE_YUV) {
907 yi->main_rect = crop->c;
908 return 0;
909 } else {
910 if (!ivtv_vapi(itv, CX2341X_OSD_SET_FRAMEBUFFER_WINDOW, 4,
911 crop->c.width, crop->c.height, crop->c.left, crop->c.top)) {
912 itv->main_rect = crop->c;
913 return 0;
914 }
915 }
916 return -EINVAL;
917 }
918 return -EINVAL;
919}
920
921static int ivtv_g_crop(struct file *file, void *fh, struct v4l2_crop *crop)
922{
923 struct ivtv_open_id *id = fh;
924 struct ivtv *itv = id->itv;
925 struct yuv_playback_info *yi = &itv->yuv_info;
926 int streamtype;
927
928 streamtype = id->type;
929
930 if (crop->type == V4L2_BUF_TYPE_VIDEO_OUTPUT &&
931 (itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT)) {
932 if (streamtype == IVTV_DEC_STREAM_TYPE_YUV)
933 crop->c = yi->main_rect;
934 else
935 crop->c = itv->main_rect;
936 return 0;
937 }
938 return -EINVAL;
939}
940
941static int ivtv_enum_fmt_vid_cap(struct file *file, void *fh, struct v4l2_fmtdesc *fmt)
942{
943 static struct v4l2_fmtdesc formats[] = {
944 { 0, 0, 0,
945 "HM12 (YUV 4:2:0)", V4L2_PIX_FMT_HM12,
946 { 0, 0, 0, 0 }
947 },
948 { 1, 0, V4L2_FMT_FLAG_COMPRESSED,
949 "MPEG", V4L2_PIX_FMT_MPEG,
950 { 0, 0, 0, 0 }
951 }
952 };
953 enum v4l2_buf_type type = fmt->type;
954
955 if (fmt->index > 1)
956 return -EINVAL;
957
958 *fmt = formats[fmt->index];
959 fmt->type = type;
960 return 0;
961}
962
963static int ivtv_enum_fmt_vid_out(struct file *file, void *fh, struct v4l2_fmtdesc *fmt)
964{
965 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
966
967 static struct v4l2_fmtdesc formats[] = {
968 { 0, 0, 0,
969 "HM12 (YUV 4:2:0)", V4L2_PIX_FMT_HM12,
970 { 0, 0, 0, 0 }
971 },
972 { 1, 0, V4L2_FMT_FLAG_COMPRESSED,
973 "MPEG", V4L2_PIX_FMT_MPEG,
974 { 0, 0, 0, 0 }
975 }
976 };
977 enum v4l2_buf_type type = fmt->type;
978
979 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
980 return -EINVAL;
981
982 if (fmt->index > 1)
983 return -EINVAL;
984
985 *fmt = formats[fmt->index];
986 fmt->type = type;
987
988 return 0;
989}
990
991static int ivtv_g_input(struct file *file, void *fh, unsigned int *i)
992{
993 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
994
995 *i = itv->active_input;
996
997 return 0;
998}
999
1000int ivtv_s_input(struct file *file, void *fh, unsigned int inp)
1001{
1002 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
1003
1004 if (inp < 0 || inp >= itv->nof_inputs)
1005 return -EINVAL;
1006
1007 if (inp == itv->active_input) {
1008 IVTV_DEBUG_INFO("Input unchanged\n");
1009 return 0;
1010 }
1011
1012 if (atomic_read(&itv->capturing) > 0) {
1013 return -EBUSY;
1014 }
1015
1016 IVTV_DEBUG_INFO("Changing input from %d to %d\n",
1017 itv->active_input, inp);
1018
1019 itv->active_input = inp;
1020 /* Set the audio input to whatever is appropriate for the
1021 input type. */
1022 itv->audio_input = itv->card->video_inputs[inp].audio_index;
1023
1024 /* prevent others from messing with the streams until
1025 we're finished changing inputs. */
1026 ivtv_mute(itv);
1027 ivtv_video_set_io(itv);
1028 ivtv_audio_set_io(itv);
1029 ivtv_unmute(itv);
1030
1031 return 0;
1032}
1033
1034static int ivtv_g_output(struct file *file, void *fh, unsigned int *i)
1035{
1036 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
1037
1038 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
1039 return -EINVAL;
1040
1041 *i = itv->active_output;
1042
1043 return 0;
1044}
1045
1046static int ivtv_s_output(struct file *file, void *fh, unsigned int outp)
1047{
1048 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
1049 struct v4l2_routing route;
1050
1051 if (outp >= itv->card->nof_outputs)
1052 return -EINVAL;
1053
1054 if (outp == itv->active_output) {
1055 IVTV_DEBUG_INFO("Output unchanged\n");
1056 return 0;
1057 }
1058 IVTV_DEBUG_INFO("Changing output from %d to %d\n",
1059 itv->active_output, outp);
1060
1061 itv->active_output = outp;
1062 route.input = SAA7127_INPUT_TYPE_NORMAL;
1063 route.output = itv->card->video_outputs[outp].video_output;
1064 ivtv_saa7127(itv, VIDIOC_INT_S_VIDEO_ROUTING, &route);
1065
1066 return 0;
1067}
1068
1069static int ivtv_g_frequency(struct file *file, void *fh, struct v4l2_frequency *vf)
1070{
1071 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
1072
1073 if (vf->tuner != 0)
1074 return -EINVAL;
1075
1076 ivtv_call_i2c_clients(itv, VIDIOC_G_FREQUENCY, vf);
1077 return 0;
1078}
1079
1080int ivtv_s_frequency(struct file *file, void *fh, struct v4l2_frequency *vf)
1081{
1082 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
1083
1084 if (vf->tuner != 0)
1085 return -EINVAL;
1086
1087 ivtv_mute(itv);
1088 IVTV_DEBUG_INFO("v4l2 ioctl: set frequency %d\n", vf->frequency);
1089 ivtv_call_i2c_clients(itv, VIDIOC_S_FREQUENCY, vf);
1090 ivtv_unmute(itv);
1091 return 0;
1092}
1093
1094static int ivtv_g_std(struct file *file, void *fh, v4l2_std_id *std)
1095{
1096 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
1097
1098 *std = itv->std;
1099 return 0;
1100}
1101
1102int ivtv_s_std(struct file *file, void *fh, v4l2_std_id *std)
1103{
1104 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
1105 struct yuv_playback_info *yi = &itv->yuv_info;
1106
1107 if ((*std & V4L2_STD_ALL) == 0)
1108 return -EINVAL;
1109
1110 if (*std == itv->std)
1111 return 0;
1112
1113 if (test_bit(IVTV_F_I_RADIO_USER, &itv->i_flags) ||
1114 atomic_read(&itv->capturing) > 0 ||
1115 atomic_read(&itv->decoding) > 0) {
1116 /* Switching standard would turn off the radio or mess
1117 with already running streams, prevent that by
1118 returning EBUSY. */
1119 return -EBUSY;
1120 }
1121
1122 itv->std = *std;
1123 itv->is_60hz = (*std & V4L2_STD_525_60) ? 1 : 0;
1124 itv->params.is_50hz = itv->is_50hz = !itv->is_60hz;
1125 itv->params.width = 720;
1126 itv->params.height = itv->is_50hz ? 576 : 480;
1127 itv->vbi.count = itv->is_50hz ? 18 : 12;
1128 itv->vbi.start[0] = itv->is_50hz ? 6 : 10;
1129 itv->vbi.start[1] = itv->is_50hz ? 318 : 273;
1130
1131 if (itv->hw_flags & IVTV_HW_CX25840)
1132 itv->vbi.sliced_decoder_line_size = itv->is_60hz ? 272 : 284;
1133
1134 IVTV_DEBUG_INFO("Switching standard to %llx.\n", (unsigned long long)itv->std);
1135
1136 /* Tuner */
1137 ivtv_call_i2c_clients(itv, VIDIOC_S_STD, &itv->std);
1138
1139 if (itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT) {
1140 /* set display standard */
1141 itv->std_out = *std;
1142 itv->is_out_60hz = itv->is_60hz;
1143 itv->is_out_50hz = itv->is_50hz;
1144 ivtv_call_i2c_clients(itv, VIDIOC_INT_S_STD_OUTPUT, &itv->std_out);
1145 ivtv_vapi(itv, CX2341X_DEC_SET_STANDARD, 1, itv->is_out_50hz);
1146 itv->main_rect.left = itv->main_rect.top = 0;
1147 itv->main_rect.width = 720;
1148 itv->main_rect.height = itv->params.height;
1149 ivtv_vapi(itv, CX2341X_OSD_SET_FRAMEBUFFER_WINDOW, 4,
1150 720, itv->main_rect.height, 0, 0);
1151 yi->main_rect = itv->main_rect;
1152 if (!itv->osd_info) {
1153 yi->osd_full_w = 720;
1154 yi->osd_full_h = itv->is_out_50hz ? 576 : 480;
1155 }
Hans Verkuil1a0adaf2007-04-27 12:31:25 -03001156 }
1157 return 0;
1158}
1159
Hans Verkuil3f038d82008-05-29 16:43:54 -03001160static int ivtv_s_tuner(struct file *file, void *fh, struct v4l2_tuner *vt)
Hans Verkuil1a0adaf2007-04-27 12:31:25 -03001161{
Hans Verkuil3f038d82008-05-29 16:43:54 -03001162 struct ivtv_open_id *id = fh;
1163 struct ivtv *itv = id->itv;
1164
1165 if (vt->index != 0)
1166 return -EINVAL;
1167
1168 ivtv_call_i2c_clients(itv, VIDIOC_S_TUNER, vt);
1169
1170 return 0;
1171}
1172
1173static int ivtv_g_tuner(struct file *file, void *fh, struct v4l2_tuner *vt)
1174{
1175 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
1176
1177 if (vt->index != 0)
1178 return -EINVAL;
1179
1180 memset(vt, 0, sizeof(*vt));
1181 ivtv_call_i2c_clients(itv, VIDIOC_G_TUNER, vt);
1182
1183 if (test_bit(IVTV_F_I_RADIO_USER, &itv->i_flags)) {
1184 strlcpy(vt->name, "ivtv Radio Tuner", sizeof(vt->name));
1185 vt->type = V4L2_TUNER_RADIO;
1186 } else {
1187 strlcpy(vt->name, "ivtv TV Tuner", sizeof(vt->name));
1188 vt->type = V4L2_TUNER_ANALOG_TV;
1189 }
1190
1191 return 0;
1192}
1193
1194static int ivtv_g_sliced_vbi_cap(struct file *file, void *fh, struct v4l2_sliced_vbi_cap *cap)
1195{
1196 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
1197 int set = itv->is_50hz ? V4L2_SLICED_VBI_625 : V4L2_SLICED_VBI_525;
1198 int f, l;
1199 enum v4l2_buf_type type = cap->type;
1200
1201 memset(cap, 0, sizeof(*cap));
1202 cap->type = type;
1203 if (type == V4L2_BUF_TYPE_SLICED_VBI_CAPTURE) {
1204 for (f = 0; f < 2; f++) {
1205 for (l = 0; l < 24; l++) {
1206 if (valid_service_line(f, l, itv->is_50hz))
1207 cap->service_lines[f][l] = set;
1208 }
1209 }
1210 return 0;
1211 }
1212 if (type == V4L2_BUF_TYPE_SLICED_VBI_OUTPUT) {
1213 if (!(itv->v4l2_cap & V4L2_CAP_SLICED_VBI_OUTPUT))
1214 return -EINVAL;
1215 if (itv->is_60hz) {
1216 cap->service_lines[0][21] = V4L2_SLICED_CAPTION_525;
1217 cap->service_lines[1][21] = V4L2_SLICED_CAPTION_525;
1218 } else {
1219 cap->service_lines[0][23] = V4L2_SLICED_WSS_625;
1220 cap->service_lines[0][16] = V4L2_SLICED_VPS;
1221 }
1222 return 0;
1223 }
1224 return -EINVAL;
1225}
1226
1227static int ivtv_g_enc_index(struct file *file, void *fh, struct v4l2_enc_idx *idx)
1228{
1229 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
1230 struct v4l2_enc_idx_entry *e = idx->entry;
1231 int entries;
1232 int i;
1233
1234 entries = (itv->pgm_info_write_idx + IVTV_MAX_PGM_INDEX - itv->pgm_info_read_idx) %
1235 IVTV_MAX_PGM_INDEX;
1236 if (entries > V4L2_ENC_IDX_ENTRIES)
1237 entries = V4L2_ENC_IDX_ENTRIES;
1238 idx->entries = 0;
1239 for (i = 0; i < entries; i++) {
1240 *e = itv->pgm_info[(itv->pgm_info_read_idx + i) % IVTV_MAX_PGM_INDEX];
1241 if ((e->flags & V4L2_ENC_IDX_FRAME_MASK) <= V4L2_ENC_IDX_FRAME_B) {
1242 idx->entries++;
1243 e++;
1244 }
1245 }
1246 itv->pgm_info_read_idx = (itv->pgm_info_read_idx + idx->entries) % IVTV_MAX_PGM_INDEX;
1247 return 0;
1248}
1249
1250static int ivtv_encoder_cmd(struct file *file, void *fh, struct v4l2_encoder_cmd *enc)
1251{
1252 struct ivtv_open_id *id = fh;
1253 struct ivtv *itv = id->itv;
1254
1255 memset(&enc->raw, 0, sizeof(enc->raw));
1256
1257 switch (enc->cmd) {
1258 case V4L2_ENC_CMD_START:
1259 IVTV_DEBUG_IOCTL("V4L2_ENC_CMD_START\n");
1260 enc->flags = 0;
1261 return ivtv_start_capture(id);
1262
1263 case V4L2_ENC_CMD_STOP:
1264 IVTV_DEBUG_IOCTL("V4L2_ENC_CMD_STOP\n");
1265 enc->flags &= V4L2_ENC_CMD_STOP_AT_GOP_END;
1266 ivtv_stop_capture(id, enc->flags & V4L2_ENC_CMD_STOP_AT_GOP_END);
1267 return 0;
1268
1269 case V4L2_ENC_CMD_PAUSE:
1270 IVTV_DEBUG_IOCTL("V4L2_ENC_CMD_PAUSE\n");
1271 enc->flags = 0;
1272
1273 if (!atomic_read(&itv->capturing))
1274 return -EPERM;
1275 if (test_and_set_bit(IVTV_F_I_ENC_PAUSED, &itv->i_flags))
1276 return 0;
1277
1278 ivtv_mute(itv);
1279 ivtv_vapi(itv, CX2341X_ENC_PAUSE_ENCODER, 1, 0);
1280 break;
1281
1282 case V4L2_ENC_CMD_RESUME:
1283 IVTV_DEBUG_IOCTL("V4L2_ENC_CMD_RESUME\n");
1284 enc->flags = 0;
1285
1286 if (!atomic_read(&itv->capturing))
1287 return -EPERM;
1288
1289 if (!test_and_clear_bit(IVTV_F_I_ENC_PAUSED, &itv->i_flags))
1290 return 0;
1291
1292 ivtv_vapi(itv, CX2341X_ENC_PAUSE_ENCODER, 1, 1);
1293 ivtv_unmute(itv);
1294 break;
1295 default:
1296 IVTV_DEBUG_IOCTL("Unknown cmd %d\n", enc->cmd);
1297 return -EINVAL;
1298 }
1299
1300 return 0;
1301}
1302
1303static int ivtv_try_encoder_cmd(struct file *file, void *fh, struct v4l2_encoder_cmd *enc)
1304{
1305 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
1306
1307 memset(&enc->raw, 0, sizeof(enc->raw));
1308
1309 switch (enc->cmd) {
1310 case V4L2_ENC_CMD_START:
1311 IVTV_DEBUG_IOCTL("V4L2_ENC_CMD_START\n");
1312 enc->flags = 0;
1313 return 0;
1314
1315 case V4L2_ENC_CMD_STOP:
1316 IVTV_DEBUG_IOCTL("V4L2_ENC_CMD_STOP\n");
1317 enc->flags &= V4L2_ENC_CMD_STOP_AT_GOP_END;
1318 return 0;
1319
1320 case V4L2_ENC_CMD_PAUSE:
1321 IVTV_DEBUG_IOCTL("V4L2_ENC_CMD_PAUSE\n");
1322 enc->flags = 0;
1323 return 0;
1324
1325 case V4L2_ENC_CMD_RESUME:
1326 IVTV_DEBUG_IOCTL("V4L2_ENC_CMD_RESUME\n");
1327 enc->flags = 0;
1328 return 0;
1329 default:
1330 IVTV_DEBUG_IOCTL("Unknown cmd %d\n", enc->cmd);
1331 return -EINVAL;
1332 }
1333}
1334
1335static int ivtv_g_fbuf(struct file *file, void *fh, struct v4l2_framebuffer *fb)
1336{
1337 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
Hans Verkuil2d4d5f12007-08-23 21:15:24 -03001338 u32 data[CX2341X_MBOX_MAX_DATA];
Hans Verkuil3f038d82008-05-29 16:43:54 -03001339 struct yuv_playback_info *yi = &itv->yuv_info;
Hans Verkuil1a0adaf2007-04-27 12:31:25 -03001340
Hans Verkuil3f038d82008-05-29 16:43:54 -03001341 int pixfmt;
1342 static u32 pixel_format[16] = {
1343 V4L2_PIX_FMT_PAL8, /* Uses a 256-entry RGB colormap */
1344 V4L2_PIX_FMT_RGB565,
1345 V4L2_PIX_FMT_RGB555,
1346 V4L2_PIX_FMT_RGB444,
1347 V4L2_PIX_FMT_RGB32,
1348 0,
1349 0,
1350 0,
1351 V4L2_PIX_FMT_PAL8, /* Uses a 256-entry YUV colormap */
1352 V4L2_PIX_FMT_YUV565,
1353 V4L2_PIX_FMT_YUV555,
1354 V4L2_PIX_FMT_YUV444,
1355 V4L2_PIX_FMT_YUV32,
1356 0,
1357 0,
1358 0,
1359 };
Hans Verkuil1a0adaf2007-04-27 12:31:25 -03001360
Hans Verkuil3f038d82008-05-29 16:43:54 -03001361 memset(fb, 0, sizeof(*fb));
Hans Verkuild46c17d2007-03-10 17:59:15 -03001362
Hans Verkuil3f038d82008-05-29 16:43:54 -03001363 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT_OVERLAY))
1364 return -EINVAL;
Hans Verkuild46c17d2007-03-10 17:59:15 -03001365
Hans Verkuil3f038d82008-05-29 16:43:54 -03001366 fb->capability = V4L2_FBUF_CAP_EXTERNOVERLAY | V4L2_FBUF_CAP_CHROMAKEY |
1367 V4L2_FBUF_CAP_GLOBAL_ALPHA;
Hans Verkuild46c17d2007-03-10 17:59:15 -03001368
Hans Verkuil3f038d82008-05-29 16:43:54 -03001369 ivtv_vapi_result(itv, data, CX2341X_OSD_GET_STATE, 0);
1370 data[0] |= (read_reg(0x2a00) >> 7) & 0x40;
1371 pixfmt = (data[0] >> 3) & 0xf;
Hans Verkuild46c17d2007-03-10 17:59:15 -03001372
Hans Verkuil3f038d82008-05-29 16:43:54 -03001373 fb->fmt.pixelformat = pixel_format[pixfmt];
1374 fb->fmt.width = itv->osd_rect.width;
1375 fb->fmt.height = itv->osd_rect.height;
1376 fb->base = (void *)itv->osd_video_pbase;
Hans Verkuil1a0adaf2007-04-27 12:31:25 -03001377
Hans Verkuil3f038d82008-05-29 16:43:54 -03001378 if (itv->osd_chroma_key_state)
1379 fb->flags |= V4L2_FBUF_FLAG_CHROMAKEY;
Hans Verkuil1a0adaf2007-04-27 12:31:25 -03001380
Hans Verkuil3f038d82008-05-29 16:43:54 -03001381 if (itv->osd_global_alpha_state)
1382 fb->flags |= V4L2_FBUF_FLAG_GLOBAL_ALPHA;
Hans Verkuil1a0adaf2007-04-27 12:31:25 -03001383
Hans Verkuil3f038d82008-05-29 16:43:54 -03001384 pixfmt &= 7;
Hans Verkuil1a0adaf2007-04-27 12:31:25 -03001385
Hans Verkuil3f038d82008-05-29 16:43:54 -03001386 /* no local alpha for RGB565 or unknown formats */
1387 if (pixfmt == 1 || pixfmt > 4)
Hans Verkuil987e00b2007-05-29 13:03:27 -03001388 return 0;
Hans Verkuil987e00b2007-05-29 13:03:27 -03001389
Hans Verkuil3f038d82008-05-29 16:43:54 -03001390 /* 16-bit formats have inverted local alpha */
1391 if (pixfmt == 2 || pixfmt == 3)
1392 fb->capability |= V4L2_FBUF_CAP_LOCAL_INV_ALPHA;
1393 else
1394 fb->capability |= V4L2_FBUF_CAP_LOCAL_ALPHA;
Hans Verkuil1a0adaf2007-04-27 12:31:25 -03001395
Hans Verkuil3f038d82008-05-29 16:43:54 -03001396 if (itv->osd_local_alpha_state) {
Hans Verkuil2d4d5f12007-08-23 21:15:24 -03001397 /* 16-bit formats have inverted local alpha */
1398 if (pixfmt == 2 || pixfmt == 3)
Hans Verkuil3f038d82008-05-29 16:43:54 -03001399 fb->flags |= V4L2_FBUF_FLAG_LOCAL_INV_ALPHA;
Hans Verkuil2d4d5f12007-08-23 21:15:24 -03001400 else
Hans Verkuil3f038d82008-05-29 16:43:54 -03001401 fb->flags |= V4L2_FBUF_FLAG_LOCAL_ALPHA;
Hans Verkuild4e7ee32007-03-10 18:19:12 -03001402 }
Hans Verkuil3f038d82008-05-29 16:43:54 -03001403 if (yi->track_osd)
1404 fb->flags |= V4L2_FBUF_FLAG_OVERLAY;
Hans Verkuild4e7ee32007-03-10 18:19:12 -03001405
Hans Verkuil3f038d82008-05-29 16:43:54 -03001406 return 0;
1407}
Hans Verkuild4e7ee32007-03-10 18:19:12 -03001408
Hans Verkuil3f038d82008-05-29 16:43:54 -03001409static int ivtv_s_fbuf(struct file *file, void *fh, struct v4l2_framebuffer *fb)
1410{
1411 struct ivtv_open_id *id = fh;
1412 struct ivtv *itv = id->itv;
1413 struct yuv_playback_info *yi = &itv->yuv_info;
Hans Verkuild4e7ee32007-03-10 18:19:12 -03001414
Hans Verkuil3f038d82008-05-29 16:43:54 -03001415 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT_OVERLAY))
Hans Verkuil1a0adaf2007-04-27 12:31:25 -03001416 return -EINVAL;
Hans Verkuil3f038d82008-05-29 16:43:54 -03001417
1418 itv->osd_global_alpha_state = (fb->flags & V4L2_FBUF_FLAG_GLOBAL_ALPHA) != 0;
1419 itv->osd_local_alpha_state =
1420 (fb->flags & (V4L2_FBUF_FLAG_LOCAL_ALPHA|V4L2_FBUF_FLAG_LOCAL_INV_ALPHA)) != 0;
1421 itv->osd_chroma_key_state = (fb->flags & V4L2_FBUF_FLAG_CHROMAKEY) != 0;
1422 ivtv_set_osd_alpha(itv);
1423 yi->track_osd = (fb->flags & V4L2_FBUF_FLAG_OVERLAY) != 0;
1424
1425 return 0;
1426}
1427
1428static int ivtv_overlay(struct file *file, void *fh, unsigned int on)
1429{
1430 struct ivtv_open_id *id = fh;
1431 struct ivtv *itv = id->itv;
1432
1433 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT_OVERLAY))
1434 return -EINVAL;
1435
1436 ivtv_vapi(itv, CX2341X_OSD_SET_STATE, 1, on != 0);
1437
1438 return 0;
1439}
1440
1441static int ivtv_log_status(struct file *file, void *fh)
1442{
1443 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
1444 u32 data[CX2341X_MBOX_MAX_DATA];
1445
1446 int has_output = itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT;
1447 struct v4l2_input vidin;
1448 struct v4l2_audio audin;
1449 int i;
1450
1451 IVTV_INFO("================= START STATUS CARD #%d =================\n", itv->num);
1452 IVTV_INFO("Version: %s Card: %s\n", IVTV_VERSION, itv->card_name);
1453 if (itv->hw_flags & IVTV_HW_TVEEPROM) {
1454 struct tveeprom tv;
1455
1456 ivtv_read_eeprom(itv, &tv);
Hans Verkuil1a0adaf2007-04-27 12:31:25 -03001457 }
Hans Verkuil3f038d82008-05-29 16:43:54 -03001458 ivtv_call_i2c_clients(itv, VIDIOC_LOG_STATUS, NULL);
1459 ivtv_get_input(itv, itv->active_input, &vidin);
1460 ivtv_get_audio_input(itv, itv->audio_input, &audin);
1461 IVTV_INFO("Video Input: %s\n", vidin.name);
1462 IVTV_INFO("Audio Input: %s%s\n", audin.name,
1463 (itv->dualwatch_stereo_mode & ~0x300) == 0x200 ? " (Bilingual)" : "");
1464 if (has_output) {
1465 struct v4l2_output vidout;
1466 struct v4l2_audioout audout;
1467 int mode = itv->output_mode;
1468 static const char * const output_modes[5] = {
1469 "None",
1470 "MPEG Streaming",
1471 "YUV Streaming",
1472 "YUV Frames",
1473 "Passthrough",
1474 };
1475 static const char * const audio_modes[5] = {
1476 "Stereo",
1477 "Left",
1478 "Right",
1479 "Mono",
1480 "Swapped"
1481 };
1482 static const char * const alpha_mode[4] = {
1483 "None",
1484 "Global",
1485 "Local",
1486 "Global and Local"
1487 };
1488 static const char * const pixel_format[16] = {
1489 "ARGB Indexed",
1490 "RGB 5:6:5",
1491 "ARGB 1:5:5:5",
1492 "ARGB 1:4:4:4",
1493 "ARGB 8:8:8:8",
1494 "5",
1495 "6",
1496 "7",
1497 "AYUV Indexed",
1498 "YUV 5:6:5",
1499 "AYUV 1:5:5:5",
1500 "AYUV 1:4:4:4",
1501 "AYUV 8:8:8:8",
1502 "13",
1503 "14",
1504 "15",
1505 };
1506
1507 ivtv_get_output(itv, itv->active_output, &vidout);
1508 ivtv_get_audio_output(itv, 0, &audout);
1509 IVTV_INFO("Video Output: %s\n", vidout.name);
1510 IVTV_INFO("Audio Output: %s (Stereo/Bilingual: %s/%s)\n", audout.name,
1511 audio_modes[itv->audio_stereo_mode],
1512 audio_modes[itv->audio_bilingual_mode]);
1513 if (mode < 0 || mode > OUT_PASSTHROUGH)
1514 mode = OUT_NONE;
1515 IVTV_INFO("Output Mode: %s\n", output_modes[mode]);
1516 ivtv_vapi_result(itv, data, CX2341X_OSD_GET_STATE, 0);
1517 data[0] |= (read_reg(0x2a00) >> 7) & 0x40;
1518 IVTV_INFO("Overlay: %s, Alpha: %s, Pixel Format: %s\n",
1519 data[0] & 1 ? "On" : "Off",
1520 alpha_mode[(data[0] >> 1) & 0x3],
1521 pixel_format[(data[0] >> 3) & 0xf]);
1522 }
1523 IVTV_INFO("Tuner: %s\n",
1524 test_bit(IVTV_F_I_RADIO_USER, &itv->i_flags) ? "Radio" : "TV");
1525 cx2341x_log_status(&itv->params, itv->name);
1526 IVTV_INFO("Status flags: 0x%08lx\n", itv->i_flags);
1527 for (i = 0; i < IVTV_MAX_STREAMS; i++) {
1528 struct ivtv_stream *s = &itv->streams[i];
1529
1530 if (s->v4l2dev == NULL || s->buffers == 0)
1531 continue;
1532 IVTV_INFO("Stream %s: status 0x%04lx, %d%% of %d KiB (%d buffers) in use\n", s->name, s->s_flags,
1533 (s->buffers - s->q_free.buffers) * 100 / s->buffers,
1534 (s->buffers * s->buf_size) / 1024, s->buffers);
1535 }
1536
1537 IVTV_INFO("Read MPG/VBI: %lld/%lld bytes\n", (long long)itv->mpg_data_received, (long long)itv->vbi_data_inserted);
1538 IVTV_INFO("================== END STATUS CARD #%d ==================\n", itv->num);
1539
Hans Verkuil1a0adaf2007-04-27 12:31:25 -03001540 return 0;
1541}
1542
Hans Verkuild4e7ee32007-03-10 18:19:12 -03001543static int ivtv_decoder_ioctls(struct file *filp, unsigned int cmd, void *arg)
Hans Verkuil1a0adaf2007-04-27 12:31:25 -03001544{
1545 struct ivtv_open_id *id = (struct ivtv_open_id *)filp->private_data;
1546 struct ivtv *itv = id->itv;
1547 int nonblocking = filp->f_flags & O_NONBLOCK;
1548 struct ivtv_stream *s = &itv->streams[id->type];
1549
1550 switch (cmd) {
1551 case IVTV_IOC_DMA_FRAME: {
1552 struct ivtv_dma_frame *args = arg;
1553
1554 IVTV_DEBUG_IOCTL("IVTV_IOC_DMA_FRAME\n");
1555 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
1556 return -EINVAL;
1557 if (args->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
1558 return -EINVAL;
1559 if (itv->output_mode == OUT_UDMA_YUV && args->y_source == NULL)
1560 return 0;
1561 if (ivtv_claim_stream(id, id->type)) {
1562 return -EBUSY;
1563 }
1564 if (ivtv_set_output_mode(itv, OUT_UDMA_YUV) != OUT_UDMA_YUV) {
1565 ivtv_release_stream(s);
1566 return -EBUSY;
1567 }
Hans Verkuilad8ff0f2007-08-20 16:01:58 -03001568 /* Mark that this file handle started the UDMA_YUV mode */
1569 id->yuv_frames = 1;
Hans Verkuil1a0adaf2007-04-27 12:31:25 -03001570 if (args->y_source == NULL)
1571 return 0;
1572 return ivtv_yuv_prep_frame(itv, args);
1573 }
1574
1575 case VIDEO_GET_PTS: {
1576 u32 data[CX2341X_MBOX_MAX_DATA];
1577 u64 *pts = arg;
1578
1579 IVTV_DEBUG_IOCTL("VIDEO_GET_PTS\n");
1580 if (s->type < IVTV_DEC_STREAM_TYPE_MPG) {
1581 *pts = s->dma_pts;
1582 break;
1583 }
1584 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
1585 return -EINVAL;
1586
1587 if (test_bit(IVTV_F_I_VALID_DEC_TIMINGS, &itv->i_flags)) {
1588 *pts = (u64) ((u64)itv->last_dec_timing[2] << 32) |
1589 (u64)itv->last_dec_timing[1];
1590 break;
1591 }
1592 *pts = 0;
1593 if (atomic_read(&itv->decoding)) {
1594 if (ivtv_api(itv, CX2341X_DEC_GET_TIMING_INFO, 5, data)) {
1595 IVTV_DEBUG_WARN("GET_TIMING: couldn't read clock\n");
1596 return -EIO;
1597 }
1598 memcpy(itv->last_dec_timing, data, sizeof(itv->last_dec_timing));
1599 set_bit(IVTV_F_I_VALID_DEC_TIMINGS, &itv->i_flags);
1600 *pts = (u64) ((u64) data[2] << 32) | (u64) data[1];
1601 /*timing->scr = (u64) (((u64) data[4] << 32) | (u64) (data[3]));*/
1602 }
1603 break;
1604 }
1605
1606 case VIDEO_GET_FRAME_COUNT: {
1607 u32 data[CX2341X_MBOX_MAX_DATA];
1608 u64 *frame = arg;
1609
1610 IVTV_DEBUG_IOCTL("VIDEO_GET_FRAME_COUNT\n");
1611 if (s->type < IVTV_DEC_STREAM_TYPE_MPG) {
1612 *frame = 0;
1613 break;
1614 }
1615 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
1616 return -EINVAL;
1617
1618 if (test_bit(IVTV_F_I_VALID_DEC_TIMINGS, &itv->i_flags)) {
1619 *frame = itv->last_dec_timing[0];
1620 break;
1621 }
1622 *frame = 0;
1623 if (atomic_read(&itv->decoding)) {
1624 if (ivtv_api(itv, CX2341X_DEC_GET_TIMING_INFO, 5, data)) {
1625 IVTV_DEBUG_WARN("GET_TIMING: couldn't read clock\n");
1626 return -EIO;
1627 }
1628 memcpy(itv->last_dec_timing, data, sizeof(itv->last_dec_timing));
1629 set_bit(IVTV_F_I_VALID_DEC_TIMINGS, &itv->i_flags);
1630 *frame = data[0];
1631 }
1632 break;
1633 }
1634
1635 case VIDEO_PLAY: {
1636 struct video_command vc;
1637
1638 IVTV_DEBUG_IOCTL("VIDEO_PLAY\n");
1639 memset(&vc, 0, sizeof(vc));
1640 vc.cmd = VIDEO_CMD_PLAY;
1641 return ivtv_video_command(itv, id, &vc, 0);
1642 }
1643
1644 case VIDEO_STOP: {
1645 struct video_command vc;
1646
1647 IVTV_DEBUG_IOCTL("VIDEO_STOP\n");
1648 memset(&vc, 0, sizeof(vc));
1649 vc.cmd = VIDEO_CMD_STOP;
1650 vc.flags = VIDEO_CMD_STOP_TO_BLACK | VIDEO_CMD_STOP_IMMEDIATELY;
1651 return ivtv_video_command(itv, id, &vc, 0);
1652 }
1653
1654 case VIDEO_FREEZE: {
1655 struct video_command vc;
1656
1657 IVTV_DEBUG_IOCTL("VIDEO_FREEZE\n");
1658 memset(&vc, 0, sizeof(vc));
1659 vc.cmd = VIDEO_CMD_FREEZE;
1660 return ivtv_video_command(itv, id, &vc, 0);
1661 }
1662
1663 case VIDEO_CONTINUE: {
1664 struct video_command vc;
1665
1666 IVTV_DEBUG_IOCTL("VIDEO_CONTINUE\n");
1667 memset(&vc, 0, sizeof(vc));
1668 vc.cmd = VIDEO_CMD_CONTINUE;
1669 return ivtv_video_command(itv, id, &vc, 0);
1670 }
1671
1672 case VIDEO_COMMAND:
1673 case VIDEO_TRY_COMMAND: {
1674 struct video_command *vc = arg;
1675 int try = (cmd == VIDEO_TRY_COMMAND);
1676
1677 if (try)
Hans Verkuil1aa32c22007-08-19 06:08:58 -03001678 IVTV_DEBUG_IOCTL("VIDEO_TRY_COMMAND %d\n", vc->cmd);
Hans Verkuil1a0adaf2007-04-27 12:31:25 -03001679 else
Hans Verkuil1aa32c22007-08-19 06:08:58 -03001680 IVTV_DEBUG_IOCTL("VIDEO_COMMAND %d\n", vc->cmd);
Hans Verkuil1a0adaf2007-04-27 12:31:25 -03001681 return ivtv_video_command(itv, id, vc, try);
1682 }
1683
1684 case VIDEO_GET_EVENT: {
1685 struct video_event *ev = arg;
1686 DEFINE_WAIT(wait);
1687
1688 IVTV_DEBUG_IOCTL("VIDEO_GET_EVENT\n");
1689 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
1690 return -EINVAL;
1691 memset(ev, 0, sizeof(*ev));
1692 set_bit(IVTV_F_I_EV_VSYNC_ENABLED, &itv->i_flags);
1693
1694 while (1) {
1695 if (test_and_clear_bit(IVTV_F_I_EV_DEC_STOPPED, &itv->i_flags))
1696 ev->type = VIDEO_EVENT_DECODER_STOPPED;
1697 else if (test_and_clear_bit(IVTV_F_I_EV_VSYNC, &itv->i_flags)) {
1698 ev->type = VIDEO_EVENT_VSYNC;
Hans Verkuil037c86c2007-03-10 06:30:19 -03001699 ev->u.vsync_field = test_bit(IVTV_F_I_EV_VSYNC_FIELD, &itv->i_flags) ?
1700 VIDEO_VSYNC_FIELD_ODD : VIDEO_VSYNC_FIELD_EVEN;
1701 if (itv->output_mode == OUT_UDMA_YUV &&
1702 (itv->yuv_info.lace_mode & IVTV_YUV_MODE_MASK) ==
1703 IVTV_YUV_MODE_PROGRESSIVE) {
1704 ev->u.vsync_field = VIDEO_VSYNC_FIELD_PROGRESSIVE;
1705 }
Hans Verkuil1a0adaf2007-04-27 12:31:25 -03001706 }
1707 if (ev->type)
1708 return 0;
1709 if (nonblocking)
1710 return -EAGAIN;
Hans Verkuilbaa40722007-08-19 07:10:55 -03001711 /* Wait for event. Note that serialize_lock is locked,
1712 so to allow other processes to access the driver while
1713 we are waiting unlock first and later lock again. */
1714 mutex_unlock(&itv->serialize_lock);
Hans Verkuil1a0adaf2007-04-27 12:31:25 -03001715 prepare_to_wait(&itv->event_waitq, &wait, TASK_INTERRUPTIBLE);
1716 if ((itv->i_flags & (IVTV_F_I_EV_DEC_STOPPED|IVTV_F_I_EV_VSYNC)) == 0)
1717 schedule();
1718 finish_wait(&itv->event_waitq, &wait);
Hans Verkuilbaa40722007-08-19 07:10:55 -03001719 mutex_lock(&itv->serialize_lock);
Hans Verkuil1a0adaf2007-04-27 12:31:25 -03001720 if (signal_pending(current)) {
1721 /* return if a signal was received */
1722 IVTV_DEBUG_INFO("User stopped wait for event\n");
1723 return -EINTR;
1724 }
1725 }
1726 break;
1727 }
1728
Hans Verkuil1a0adaf2007-04-27 12:31:25 -03001729 default:
1730 return -EINVAL;
1731 }
1732 return 0;
1733}
1734
Hans Verkuil3f038d82008-05-29 16:43:54 -03001735static int ivtv_default(struct file *file, void *fh, int cmd, void *arg)
Hans Verkuil1a0adaf2007-04-27 12:31:25 -03001736{
Hans Verkuil3f038d82008-05-29 16:43:54 -03001737 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
Hans Verkuil1a0adaf2007-04-27 12:31:25 -03001738
Hans Verkuild46c17d2007-03-10 17:59:15 -03001739 switch (cmd) {
Hans Verkuil3f038d82008-05-29 16:43:54 -03001740 case VIDIOC_INT_S_AUDIO_ROUTING: {
1741 struct v4l2_routing *route = arg;
1742
1743 ivtv_i2c_hw(itv, itv->card->hw_audio, VIDIOC_INT_S_AUDIO_ROUTING, route);
1744 break;
Hans Verkuild46c17d2007-03-10 17:59:15 -03001745 }
1746
Hans Verkuil3f038d82008-05-29 16:43:54 -03001747 case VIDIOC_INT_RESET: {
1748 u32 val = *(u32 *)arg;
Hans Verkuil1a0adaf2007-04-27 12:31:25 -03001749
Hans Verkuil3f038d82008-05-29 16:43:54 -03001750 if ((val == 0 && itv->options.newi2c) || (val & 0x01))
1751 ivtv_reset_ir_gpio(itv);
1752 if (val & 0x02)
1753 itv->video_dec_func(itv, cmd, NULL);
1754 break;
1755 }
Hans Verkuil1a0adaf2007-04-27 12:31:25 -03001756
Hans Verkuil1a0adaf2007-04-27 12:31:25 -03001757 default:
Hans Verkuil3f038d82008-05-29 16:43:54 -03001758 return -EINVAL;
Hans Verkuil1a0adaf2007-04-27 12:31:25 -03001759 }
1760 return 0;
1761}
1762
Hans Verkuilbaa40722007-08-19 07:10:55 -03001763static int ivtv_serialized_ioctl(struct ivtv *itv, struct inode *inode, struct file *filp,
1764 unsigned int cmd, unsigned long arg)
Hans Verkuil1a0adaf2007-04-27 12:31:25 -03001765{
Hans Verkuil3f038d82008-05-29 16:43:54 -03001766 struct ivtv_open_id *id = (struct ivtv_open_id *)filp->private_data;
1767 int ret;
1768
1769 /* Filter dvb ioctls that cannot be handled by the v4l ioctl framework */
Hans Verkuil1a0adaf2007-04-27 12:31:25 -03001770 switch (cmd) {
1771 case VIDEO_SELECT_SOURCE:
1772 IVTV_DEBUG_IOCTL("VIDEO_SELECT_SOURCE\n");
1773 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
1774 return -EINVAL;
1775 return ivtv_passthrough_mode(itv, arg == VIDEO_SOURCE_DEMUX);
1776
1777 case AUDIO_SET_MUTE:
1778 IVTV_DEBUG_IOCTL("AUDIO_SET_MUTE\n");
1779 itv->speed_mute_audio = arg;
1780 return 0;
1781
1782 case AUDIO_CHANNEL_SELECT:
1783 IVTV_DEBUG_IOCTL("AUDIO_CHANNEL_SELECT\n");
1784 if (arg > AUDIO_STEREO_SWAPPED)
1785 return -EINVAL;
1786 itv->audio_stereo_mode = arg;
1787 ivtv_vapi(itv, CX2341X_DEC_SET_AUDIO_MODE, 2, itv->audio_bilingual_mode, itv->audio_stereo_mode);
1788 return 0;
1789
1790 case AUDIO_BILINGUAL_CHANNEL_SELECT:
1791 IVTV_DEBUG_IOCTL("AUDIO_BILINGUAL_CHANNEL_SELECT\n");
1792 if (arg > AUDIO_STEREO_SWAPPED)
1793 return -EINVAL;
1794 itv->audio_bilingual_mode = arg;
1795 ivtv_vapi(itv, CX2341X_DEC_SET_AUDIO_MODE, 2, itv->audio_bilingual_mode, itv->audio_stereo_mode);
1796 return 0;
1797
Hans Verkuil3f038d82008-05-29 16:43:54 -03001798 case IVTV_IOC_DMA_FRAME:
1799 case VIDEO_GET_PTS:
1800 case VIDEO_GET_FRAME_COUNT:
1801 case VIDEO_GET_EVENT:
1802 case VIDEO_PLAY:
1803 case VIDEO_STOP:
1804 case VIDEO_FREEZE:
1805 case VIDEO_CONTINUE:
1806 case VIDEO_COMMAND:
1807 case VIDEO_TRY_COMMAND:
1808 return ivtv_decoder_ioctls(filp, cmd, (void *)arg);
1809
Hans Verkuil1a0adaf2007-04-27 12:31:25 -03001810 default:
1811 break;
1812 }
Hans Verkuil3f038d82008-05-29 16:43:54 -03001813
1814 /* check priority */
1815 switch (cmd) {
1816 case VIDIOC_S_CTRL:
1817 case VIDIOC_S_STD:
1818 case VIDIOC_S_INPUT:
1819 case VIDIOC_S_OUTPUT:
1820 case VIDIOC_S_TUNER:
1821 case VIDIOC_S_FREQUENCY:
1822 case VIDIOC_S_FMT:
1823 case VIDIOC_S_CROP:
1824 case VIDIOC_S_AUDIO:
1825 case VIDIOC_S_AUDOUT:
1826 case VIDIOC_S_EXT_CTRLS:
1827 case VIDIOC_S_FBUF:
1828 case VIDIOC_OVERLAY:
1829 ret = v4l2_prio_check(&itv->prio, &id->prio);
1830 if (ret)
1831 return ret;
1832 }
1833
1834 if (ivtv_debug & IVTV_DBGFLG_IOCTL) {
1835 printk(KERN_INFO "ivtv%d ioctl: ", itv->num);
1836 v4l_printk_ioctl(cmd);
1837 printk("\n");
1838 }
1839
1840 return video_ioctl2(inode, filp, cmd, arg);
Hans Verkuil1a0adaf2007-04-27 12:31:25 -03001841}
Hans Verkuilbaa40722007-08-19 07:10:55 -03001842
1843int ivtv_v4l2_ioctl(struct inode *inode, struct file *filp, unsigned int cmd,
1844 unsigned long arg)
1845{
1846 struct ivtv_open_id *id = (struct ivtv_open_id *)filp->private_data;
1847 struct ivtv *itv = id->itv;
1848 int res;
1849
1850 mutex_lock(&itv->serialize_lock);
1851 res = ivtv_serialized_ioctl(itv, inode, filp, cmd, arg);
1852 mutex_unlock(&itv->serialize_lock);
1853 return res;
1854}
Hans Verkuil3f038d82008-05-29 16:43:54 -03001855
1856void ivtv_set_funcs(struct video_device *vdev)
1857{
1858 vdev->vidioc_querycap = ivtv_querycap;
1859 vdev->vidioc_g_priority = ivtv_g_priority;
1860 vdev->vidioc_s_priority = ivtv_s_priority;
1861 vdev->vidioc_s_audio = ivtv_s_audio;
1862 vdev->vidioc_g_audio = ivtv_g_audio;
1863 vdev->vidioc_enumaudio = ivtv_enumaudio;
1864 vdev->vidioc_s_audout = ivtv_s_audout;
1865 vdev->vidioc_g_audout = ivtv_g_audout;
1866 vdev->vidioc_enum_input = ivtv_enum_input;
1867 vdev->vidioc_enum_output = ivtv_enum_output;
1868 vdev->vidioc_enumaudout = ivtv_enumaudout;
1869 vdev->vidioc_cropcap = ivtv_cropcap;
1870 vdev->vidioc_s_crop = ivtv_s_crop;
1871 vdev->vidioc_g_crop = ivtv_g_crop;
1872 vdev->vidioc_g_input = ivtv_g_input;
1873 vdev->vidioc_s_input = ivtv_s_input;
1874 vdev->vidioc_g_output = ivtv_g_output;
1875 vdev->vidioc_s_output = ivtv_s_output;
1876 vdev->vidioc_g_frequency = ivtv_g_frequency;
1877 vdev->vidioc_s_frequency = ivtv_s_frequency;
1878 vdev->vidioc_s_tuner = ivtv_s_tuner;
1879 vdev->vidioc_g_tuner = ivtv_g_tuner;
1880 vdev->vidioc_g_enc_index = ivtv_g_enc_index;
1881 vdev->vidioc_g_fbuf = ivtv_g_fbuf;
1882 vdev->vidioc_s_fbuf = ivtv_s_fbuf;
1883 vdev->vidioc_g_std = ivtv_g_std;
1884 vdev->vidioc_s_std = ivtv_s_std;
1885 vdev->vidioc_overlay = ivtv_overlay;
1886 vdev->vidioc_log_status = ivtv_log_status;
1887 vdev->vidioc_enum_fmt_vid_cap = ivtv_enum_fmt_vid_cap;
1888 vdev->vidioc_encoder_cmd = ivtv_encoder_cmd;
1889 vdev->vidioc_try_encoder_cmd = ivtv_try_encoder_cmd;
1890 vdev->vidioc_enum_fmt_vid_out = ivtv_enum_fmt_vid_out;
1891 vdev->vidioc_g_fmt_vid_cap = ivtv_g_fmt_vid_cap;
1892 vdev->vidioc_g_fmt_vbi_cap = ivtv_g_fmt_vbi_cap;
1893 vdev->vidioc_g_fmt_sliced_vbi_cap = ivtv_g_fmt_sliced_vbi_cap;
1894 vdev->vidioc_g_fmt_vid_out = ivtv_g_fmt_vid_out;
1895 vdev->vidioc_g_fmt_vid_out_overlay = ivtv_g_fmt_vid_out_overlay;
1896 vdev->vidioc_g_fmt_sliced_vbi_out = ivtv_g_fmt_sliced_vbi_out;
1897 vdev->vidioc_s_fmt_vid_cap = ivtv_s_fmt_vid_cap;
1898 vdev->vidioc_s_fmt_vbi_cap = ivtv_s_fmt_vbi_cap;
1899 vdev->vidioc_s_fmt_sliced_vbi_cap = ivtv_s_fmt_sliced_vbi_cap;
1900 vdev->vidioc_s_fmt_vid_out = ivtv_s_fmt_vid_out;
1901 vdev->vidioc_s_fmt_vid_out_overlay = ivtv_s_fmt_vid_out_overlay;
1902 vdev->vidioc_s_fmt_sliced_vbi_out = ivtv_s_fmt_sliced_vbi_out;
1903 vdev->vidioc_try_fmt_vid_cap = ivtv_try_fmt_vid_cap;
1904 vdev->vidioc_try_fmt_vbi_cap = ivtv_try_fmt_vbi_cap;
1905 vdev->vidioc_try_fmt_sliced_vbi_cap = ivtv_try_fmt_sliced_vbi_cap;
1906 vdev->vidioc_try_fmt_vid_out = ivtv_try_fmt_vid_out;
1907 vdev->vidioc_try_fmt_vid_out_overlay = ivtv_try_fmt_vid_out_overlay;
1908 vdev->vidioc_try_fmt_sliced_vbi_out = ivtv_try_fmt_sliced_vbi_out;
1909 vdev->vidioc_g_sliced_vbi_cap = ivtv_g_sliced_vbi_cap;
1910 vdev->vidioc_g_chip_ident = ivtv_g_chip_ident;
1911 vdev->vidioc_g_register = ivtv_g_register;
1912 vdev->vidioc_s_register = ivtv_s_register;
1913 vdev->vidioc_default = ivtv_default;
1914 vdev->vidioc_queryctrl = ivtv_queryctrl;
1915 vdev->vidioc_querymenu = ivtv_querymenu;
1916 vdev->vidioc_g_ctrl = ivtv_g_ctrl;
1917 vdev->vidioc_s_ctrl = ivtv_s_ctrl;
1918 vdev->vidioc_g_ext_ctrls = ivtv_g_ext_ctrls;
1919 vdev->vidioc_s_ext_ctrls = ivtv_s_ext_ctrls;
1920 vdev->vidioc_try_ext_ctrls = ivtv_try_ext_ctrls;
1921}