1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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"
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>
40
41u16 service2vbi(int type)
42{
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
91void expand_service_set(struct v4l2_sliced_vbi_format *fmt, int is_pal)
92{
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
118u16 get_service_set(struct v4l2_sliced_vbi_format *fmt)
119{
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);
166 ivtv_vapi(itv, CX2341X_OSD_SET_CHROMA_KEY, 2, itv->osd_chroma_key_state, itv->osd_chroma_key);
167}
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
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
186 ivtv_vapi(itv, CX2341X_DEC_STEP_VIDEO, 1, 0);
187 itv->speed = speed;
188 return 0;
189 }
190 if (single_step)
191
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
209 if (atomic_read(&itv->decoding) > 0) {
210 int got_sig = 0;
211
212
213 ivtv_vapi(itv, CX2341X_DEC_PAUSE_PLAYBACK, 1, 0);
214
215
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
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
246 if (new_speed < 0) new_speed = -new_speed;
247 if (cur_speed < 0) cur_speed = -cur_speed;
248
249 if (cur_speed <= new_speed) {
250 if (new_speed > 1500) return fact * 2000;
251 if (new_speed > 1000) return fact * 1500;
252 }
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;
257 }
258 if (new_speed == 0) return 1000;
259 if (new_speed == 1 || new_speed == 1000) return fact * new_speed;
260
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);
267}
268
269static int ivtv_video_command(struct ivtv *itv, struct ivtv_open_id *id,
270 struct video_command *vc, int try)
271{
272 struct ivtv_stream *s = &itv->streams[IVTV_DEC_STREAM_TYPE_MPG];
273
274 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
275 return -EINVAL;
276
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;
284
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
289 itv->speed = 0;
290 }
291 return ivtv_start_decoding(id, vc->play.speed);
292 }
293
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;
303
304 itv->output_mode = OUT_NONE;
305 return ivtv_stop_v4l2_decode_stream(s, vc->flags, vc->stop.pts);
306
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);
316 }
317 break;
318
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);
328 }
329 break;
330
331 default:
332 return -EINVAL;
333 }
334 return 0;
335}
336
337static int ivtv_itvc(struct ivtv *itv, unsigned int cmd, void *arg)
338{
339 struct v4l2_register *regs = arg;
340 unsigned long flags;
341 volatile u8 __iomem *reg_start;
342
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;
354
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);
360 }
361 spin_unlock_irqrestore(&ivtv_cards_lock, flags);
362 return 0;
363}
364
365static int ivtv_get_fmt(struct ivtv *itv, int streamtype, struct v4l2_format *fmt)
366{
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;
387 }
388 fmt->fmt.pix.pixelformat = V4L2_PIX_FMT_HM12;
389
390 fmt->fmt.pix.sizeimage =
391 fmt->fmt.pix.height * fmt->fmt.pix.width +
392 fmt->fmt.pix.height * (fmt->fmt.pix.width / 2);
393 }
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
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;
405 }
406 break;
407
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
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;
423 }
424 break;
425
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;
432
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;
442
443 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
444 {
445 struct v4l2_sliced_vbi_format *vbifmt = &fmt->fmt.sliced;
446
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;
458 }
459 vbifmt->service_set = get_service_set(vbifmt);
460 break;
461 }
462
463 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
464 {
465 struct v4l2_sliced_vbi_format *vbifmt = &fmt->fmt.sliced;
466
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));
470
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;
476 }
477
478 itv->video_dec_func(itv, VIDIOC_G_FMT, fmt);
479 vbifmt->service_set = get_service_set(vbifmt);
480 break;
481 }
482 case V4L2_BUF_TYPE_VBI_OUTPUT:
483 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
484 default:
485 return -EINVAL;
486 }
487 return 0;
488}
489
490static int ivtv_try_or_set_fmt(struct ivtv *itv, int streamtype,
491 struct v4l2_format *fmt, int set_fmt)
492{
493 struct v4l2_sliced_vbi_format *vbifmt = &fmt->fmt.sliced;
494 u16 set;
495
496 if (fmt->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
497 struct v4l2_rect r;
498 int field;
499
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
510 fmt->fmt.pix.width = r.width;
511 fmt->fmt.pix.height = r.height;
512 }
513 if (itv->output_mode == OUT_UDMA_YUV) {
514
515 fmt->fmt.pix.field = field;
516 }
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;
534 }
535 itv->yuv_info.lace_sync_field = (itv->yuv_info.lace_mode & IVTV_YUV_SYNC_MASK) == IVTV_YUV_SYNC_EVEN ? 0 : 1;
536
537
538 itv->yuv_info.yuv_forced_update = 1;
539 return 0;
540 }
541 }
542 return 0;
543 }
544
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);
552 }
553 return 0;
554 }
555
556
557 if (fmt->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
558 struct cx2341x_mpeg_params *p = &itv->params;
559 int w = fmt->fmt.pix.width;
560 int h = fmt->fmt.pix.height;
561
562 if (w > 720) w = 720;
563 else if (w < 1) w = 1;
564 if (h > (itv->is_50hz ? 576 : 480)) h = (itv->is_50hz ? 576 : 480);
565 else if (h < 2) h = 2;
566 ivtv_get_fmt(itv, streamtype, fmt);
567 fmt->fmt.pix.width = w;
568 fmt->fmt.pix.height = h;
569
570 if (!set_fmt || (p->width == w && p->height == h))
571 return 0;
572 if (atomic_read(&itv->capturing) > 0)
573 return -EBUSY;
574
575 p->width = w;
576 p->height = h;
577 if (w != 720 || h != (itv->is_50hz ? 576 : 480))
578 p->video_temporal_filter = 0;
579 else
580 p->video_temporal_filter = 8;
581 if (p->video_encoding == V4L2_MPEG_VIDEO_ENCODING_MPEG_1)
582 fmt->fmt.pix.width /= 2;
583 itv->video_dec_func(itv, VIDIOC_S_FMT, fmt);
584 return ivtv_get_fmt(itv, streamtype, fmt);
585 }
586
587
588 if (fmt->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
589 if (set_fmt && atomic_read(&itv->capturing) > 0) {
590 return -EBUSY;
591 }
592 if (set_fmt) {
593 itv->vbi.sliced_in->service_set = 0;
594 itv->video_dec_func(itv, VIDIOC_S_FMT, &itv->vbi.in);
595 }
596 return ivtv_get_fmt(itv, streamtype, fmt);
597 }
598
599
600
601
602
603
604 if (fmt->type == V4L2_BUF_TYPE_SLICED_VBI_OUTPUT)
605 return ivtv_get_fmt(itv, streamtype, fmt);
606
607
608 if (fmt->type != V4L2_BUF_TYPE_SLICED_VBI_CAPTURE)
609 return -EINVAL;
610
611 if (streamtype == IVTV_DEC_STREAM_TYPE_VBI)
612 return ivtv_get_fmt(itv, streamtype, fmt);
613
614
615 vbifmt->io_size = sizeof(struct v4l2_sliced_vbi_data) * 36;
616 memset(vbifmt->reserved, 0, sizeof(vbifmt->reserved));
617
618 if (vbifmt->service_set)
619 expand_service_set(vbifmt, itv->is_50hz);
620 set = check_service_set(vbifmt, itv->is_50hz);
621 vbifmt->service_set = get_service_set(vbifmt);
622
623 if (!set_fmt)
624 return 0;
625 if (set == 0)
626 return -EINVAL;
627 if (atomic_read(&itv->capturing) > 0) {
628 return -EBUSY;
629 }
630 itv->video_dec_func(itv, VIDIOC_S_FMT, fmt);
631 memcpy(itv->vbi.sliced_in, vbifmt, sizeof(*itv->vbi.sliced_in));
632 return 0;
633}
634
635static int ivtv_debug_ioctls(struct file *filp, unsigned int cmd, void *arg)
636{
637 struct ivtv_open_id *id = (struct ivtv_open_id *)filp->private_data;
638 struct ivtv *itv = id->itv;
639 struct v4l2_register *reg = arg;
640
641 switch (cmd) {
642
643 case VIDIOC_DBG_G_REGISTER:
644 if (v4l2_chip_match_host(reg->match_type, reg->match_chip))
645 return ivtv_itvc(itv, cmd, arg);
646 if (reg->match_type == V4L2_CHIP_MATCH_I2C_DRIVER)
647 return ivtv_i2c_id(itv, reg->match_chip, cmd, arg);
648 return ivtv_call_i2c_client(itv, reg->match_chip, cmd, arg);
649
650 case VIDIOC_DBG_S_REGISTER:
651 if (v4l2_chip_match_host(reg->match_type, reg->match_chip))
652 return ivtv_itvc(itv, cmd, arg);
653 if (reg->match_type == V4L2_CHIP_MATCH_I2C_DRIVER)
654 return ivtv_i2c_id(itv, reg->match_chip, cmd, arg);
655 return ivtv_call_i2c_client(itv, reg->match_chip, cmd, arg);
656
657 case VIDIOC_G_CHIP_IDENT: {
658 struct v4l2_chip_ident *chip = arg;
659
660 chip->ident = V4L2_IDENT_NONE;
661 chip->revision = 0;
662 if (reg->match_type == V4L2_CHIP_MATCH_HOST) {
663 if (v4l2_chip_match_host(reg->match_type, reg->match_chip)) {
664 struct v4l2_chip_ident *chip = arg;
665
666 chip->ident = itv->has_cx23415 ? V4L2_IDENT_CX23415 : V4L2_IDENT_CX23416;
667 }
668 return 0;
669 }
670 if (reg->match_type == V4L2_CHIP_MATCH_I2C_DRIVER)
671 return ivtv_i2c_id(itv, reg->match_chip, cmd, arg);
672 if (reg->match_type == V4L2_CHIP_MATCH_I2C_ADDR)
673 return ivtv_call_i2c_client(itv, reg->match_chip, cmd, arg);
674 return -EINVAL;
675 }
676
677 case VIDIOC_INT_S_AUDIO_ROUTING: {
678 struct v4l2_routing *route = arg;
679
680 ivtv_i2c_hw(itv, itv->card->hw_audio, VIDIOC_INT_S_AUDIO_ROUTING, route);
681 break;
682 }
683
684 case VIDIOC_INT_RESET: {
685 u32 val = *(u32 *)arg;
686
687 if ((val == 0 && itv->options.newi2c) || (val & 0x01)) {
688 ivtv_reset_ir_gpio(itv);
689 }
690 if (val & 0x02) {
691 itv->video_dec_func(itv, cmd, 0);
692 }
693 break;
694 }
695
696 default:
697 return -EINVAL;
698 }
699 return 0;
700}
701
702int ivtv_v4l2_ioctls(struct ivtv *itv, struct file *filp, unsigned int cmd, void *arg)
703{
704 struct ivtv_open_id *id = NULL;
705 u32 data[CX2341X_MBOX_MAX_DATA];
706
707 if (filp) id = (struct ivtv_open_id *)filp->private_data;
708
709 switch (cmd) {
710 case VIDIOC_G_PRIORITY:
711 {
712 enum v4l2_priority *p = arg;
713
714 *p = v4l2_prio_max(&itv->prio);
715 break;
716 }
717
718 case VIDIOC_S_PRIORITY:
719 {
720 enum v4l2_priority *prio = arg;
721
722 return v4l2_prio_change(&itv->prio, &id->prio, *prio);
723 }
724
725 case VIDIOC_QUERYCAP:{
726 struct v4l2_capability *vcap = arg;
727
728 memset(vcap, 0, sizeof(*vcap));
729 strcpy(vcap->driver, IVTV_DRIVER_NAME);
730 strcpy(vcap->card, itv->card_name);
731 strcpy(vcap->bus_info, pci_name(itv->dev));
732 vcap->version = IVTV_DRIVER_VERSION;
733 vcap->capabilities = itv->v4l2_cap;
734
735
736 vcap->reserved[0] = vcap->reserved[1] =
737 vcap->reserved[2] = vcap->reserved[3] = 0;
738 break;
739 }
740
741 case VIDIOC_ENUMAUDIO:{
742 struct v4l2_audio *vin = arg;
743
744 return ivtv_get_audio_input(itv, vin->index, vin);
745 }
746
747 case VIDIOC_G_AUDIO:{
748 struct v4l2_audio *vin = arg;
749
750 vin->index = itv->audio_input;
751 return ivtv_get_audio_input(itv, vin->index, vin);
752 }
753
754 case VIDIOC_S_AUDIO:{
755 struct v4l2_audio *vout = arg;
756
757 if (vout->index >= itv->nof_audio_inputs)
758 return -EINVAL;
759 itv->audio_input = vout->index;
760 ivtv_audio_set_io(itv);
761 break;
762 }
763
764 case VIDIOC_ENUMAUDOUT:{
765 struct v4l2_audioout *vin = arg;
766
767
768 return ivtv_get_audio_output(itv, vin->index, vin);
769 }
770
771 case VIDIOC_G_AUDOUT:{
772 struct v4l2_audioout *vin = arg;
773
774 vin->index = 0;
775 return ivtv_get_audio_output(itv, vin->index, vin);
776 }
777
778 case VIDIOC_S_AUDOUT:{
779 struct v4l2_audioout *vout = arg;
780
781 return ivtv_get_audio_output(itv, vout->index, vout);
782 }
783
784 case VIDIOC_ENUMINPUT:{
785 struct v4l2_input *vin = arg;
786
787
788 return ivtv_get_input(itv, vin->index, vin);
789 }
790
791 case VIDIOC_ENUMOUTPUT:{
792 struct v4l2_output *vout = arg;
793
794 return ivtv_get_output(itv, vout->index, vout);
795 }
796
797 case VIDIOC_TRY_FMT:
798 case VIDIOC_S_FMT: {
799 struct v4l2_format *fmt = arg;
800
801 return ivtv_try_or_set_fmt(itv, id->type, fmt, cmd == VIDIOC_S_FMT);
802 }
803
804 case VIDIOC_G_FMT: {
805 struct v4l2_format *fmt = arg;
806 int type = fmt->type;
807
808 memset(fmt, 0, sizeof(*fmt));
809 fmt->type = type;
810 return ivtv_get_fmt(itv, id->type, fmt);
811 }
812
813 case VIDIOC_CROPCAP: {
814 struct v4l2_cropcap *cropcap = arg;
815
816 if (cropcap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
817 cropcap->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
818 return -EINVAL;
819 cropcap->bounds.top = cropcap->bounds.left = 0;
820 cropcap->bounds.width = 720;
821 if (cropcap->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
822 cropcap->bounds.height = itv->is_50hz ? 576 : 480;
823 cropcap->pixelaspect.numerator = itv->is_50hz ? 59 : 10;
824 cropcap->pixelaspect.denominator = itv->is_50hz ? 54 : 11;
825 } else {
826 cropcap->bounds.height = itv->is_out_50hz ? 576 : 480;
827 cropcap->pixelaspect.numerator = itv->is_out_50hz ? 59 : 10;
828 cropcap->pixelaspect.denominator = itv->is_out_50hz ? 54 : 11;
829 }
830 cropcap->defrect = cropcap->bounds;
831 return 0;
832 }
833
834 case VIDIOC_S_CROP: {
835 struct v4l2_crop *crop = arg;
836
837 if (crop->type == V4L2_BUF_TYPE_VIDEO_OUTPUT &&
838 (itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT)) {
839 if (!ivtv_vapi(itv, CX2341X_OSD_SET_FRAMEBUFFER_WINDOW, 4,
840 crop->c.width, crop->c.height, crop->c.left, crop->c.top)) {
841 itv->main_rect = crop->c;
842 return 0;
843 }
844 return -EINVAL;
845 }
846 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
847 return -EINVAL;
848 return itv->video_dec_func(itv, VIDIOC_S_CROP, arg);
849 }
850
851 case VIDIOC_G_CROP: {
852 struct v4l2_crop *crop = arg;
853
854 if (crop->type == V4L2_BUF_TYPE_VIDEO_OUTPUT &&
855 (itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT)) {
856 crop->c = itv->main_rect;
857 return 0;
858 }
859 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
860 return -EINVAL;
861 return itv->video_dec_func(itv, VIDIOC_G_CROP, arg);
862 }
863
864 case VIDIOC_ENUM_FMT: {
865 static struct v4l2_fmtdesc formats[] = {
866 { 0, 0, 0,
867 "HM12 (YUV 4:1:1)", V4L2_PIX_FMT_HM12,
868 { 0, 0, 0, 0 }
869 },
870 { 1, 0, V4L2_FMT_FLAG_COMPRESSED,
871 "MPEG", V4L2_PIX_FMT_MPEG,
872 { 0, 0, 0, 0 }
873 }
874 };
875 struct v4l2_fmtdesc *fmt = arg;
876 enum v4l2_buf_type type = fmt->type;
877
878 switch (type) {
879 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
880 break;
881 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
882 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
883 return -EINVAL;
884 break;
885 default:
886 return -EINVAL;
887 }
888 if (fmt->index > 1)
889 return -EINVAL;
890 *fmt = formats[fmt->index];
891 fmt->type = type;
892 return 0;
893 }
894
895 case VIDIOC_G_INPUT:{
896 *(int *)arg = itv->active_input;
897 break;
898 }
899
900 case VIDIOC_S_INPUT:{
901 int inp = *(int *)arg;
902
903 if (inp < 0 || inp >= itv->nof_inputs)
904 return -EINVAL;
905
906 if (inp == itv->active_input) {
907 IVTV_DEBUG_INFO("Input unchanged\n");
908 break;
909 }
910 if (atomic_read(&itv->capturing) > 0) {
911 return -EBUSY;
912 }
913 IVTV_DEBUG_INFO("Changing input from %d to %d\n",
914 itv->active_input, inp);
915
916 itv->active_input = inp;
917
918
919 itv->audio_input = itv->card->video_inputs[inp].audio_index;
920
921
922
923 ivtv_mute(itv);
924 ivtv_video_set_io(itv);
925 ivtv_audio_set_io(itv);
926 ivtv_unmute(itv);
927 break;
928 }
929
930 case VIDIOC_G_OUTPUT:{
931 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
932 return -EINVAL;
933 *(int *)arg = itv->active_output;
934 break;
935 }
936
937 case VIDIOC_S_OUTPUT:{
938 int outp = *(int *)arg;
939 struct v4l2_routing route;
940
941 if (outp >= itv->card->nof_outputs)
942 return -EINVAL;
943
944 if (outp == itv->active_output) {
945 IVTV_DEBUG_INFO("Output unchanged\n");
946 break;
947 }
948 IVTV_DEBUG_INFO("Changing output from %d to %d\n",
949 itv->active_output, outp);
950
951 itv->active_output = outp;
952 route.input = SAA7127_INPUT_TYPE_NORMAL;
953 route.output = itv->card->video_outputs[outp].video_output;
954 ivtv_saa7127(itv, VIDIOC_INT_S_VIDEO_ROUTING, &route);
955 break;
956 }
957
958 case VIDIOC_G_FREQUENCY:{
959 struct v4l2_frequency *vf = arg;
960
961 if (vf->tuner != 0)
962 return -EINVAL;
963 ivtv_call_i2c_clients(itv, cmd, arg);
964 break;
965 }
966
967 case VIDIOC_S_FREQUENCY:{
968 struct v4l2_frequency vf = *(struct v4l2_frequency *)arg;
969
970 if (vf.tuner != 0)
971 return -EINVAL;
972
973 ivtv_mute(itv);
974 IVTV_DEBUG_INFO("v4l2 ioctl: set frequency %d\n", vf.frequency);
975 ivtv_call_i2c_clients(itv, cmd, &vf);
976 ivtv_unmute(itv);
977 break;
978 }
979
980 case VIDIOC_ENUMSTD:{
981 struct v4l2_standard *vs = arg;
982 int idx = vs->index;
983
984 if (idx < 0 || idx >= ARRAY_SIZE(enum_stds))
985 return -EINVAL;
986
987 *vs = (enum_stds[idx].std & V4L2_STD_525_60) ?
988 ivtv_std_60hz : ivtv_std_50hz;
989 vs->index = idx;
990 vs->id = enum_stds[idx].std;
991 strcpy(vs->name, enum_stds[idx].name);
992 break;
993 }
994
995 case VIDIOC_G_STD:{
996 *(v4l2_std_id *) arg = itv->std;
997 break;
998 }
999
1000 case VIDIOC_S_STD: {
1001 v4l2_std_id std = *(v4l2_std_id *) arg;
1002
1003 if ((std & V4L2_STD_ALL) == 0)
1004 return -EINVAL;
1005
1006 if (std == itv->std)
1007 break;
1008
1009 if (test_bit(IVTV_F_I_RADIO_USER, &itv->i_flags) ||
1010 atomic_read(&itv->capturing) > 0 ||
1011 atomic_read(&itv->decoding) > 0) {
1012
1013
1014
1015 return -EBUSY;
1016 }
1017
1018 itv->std = std;
1019 itv->is_60hz = (std & V4L2_STD_525_60) ? 1 : 0;
1020 itv->params.is_50hz = itv->is_50hz = !itv->is_60hz;
1021 itv->params.width = 720;
1022 itv->params.height = itv->is_50hz ? 576 : 480;
1023 itv->vbi.count = itv->is_50hz ? 18 : 12;
1024 itv->vbi.start[0] = itv->is_50hz ? 6 : 10;
1025 itv->vbi.start[1] = itv->is_50hz ? 318 : 273;
1026 if (itv->hw_flags & IVTV_HW_CX25840) {
1027 itv->vbi.sliced_decoder_line_size = itv->is_60hz ? 272 : 284;
1028 }
1029 IVTV_DEBUG_INFO("Switching standard to %llx.\n", (unsigned long long)itv->std);
1030
1031
1032 ivtv_call_i2c_clients(itv, VIDIOC_S_STD, &itv->std);
1033
1034 if (itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT) {
1035
1036 itv->std_out = std;
1037 itv->is_out_60hz = itv->is_60hz;
1038 itv->is_out_50hz = itv->is_50hz;
1039 ivtv_call_i2c_clients(itv, VIDIOC_INT_S_STD_OUTPUT, &itv->std_out);
1040 ivtv_vapi(itv, CX2341X_DEC_SET_STANDARD, 1, itv->is_out_50hz);
1041 itv->main_rect.left = itv->main_rect.top = 0;
1042 itv->main_rect.width = 720;
1043 itv->main_rect.height = itv->params.height;
1044 ivtv_vapi(itv, CX2341X_OSD_SET_FRAMEBUFFER_WINDOW, 4,
1045 720, itv->main_rect.height, 0, 0);
1046 }
1047 break;
1048 }
1049
1050 case VIDIOC_S_TUNER: {
1051 struct v4l2_tuner *vt = arg;
1052
1053 if (vt->index != 0)
1054 return -EINVAL;
1055
1056 ivtv_call_i2c_clients(itv, VIDIOC_S_TUNER, vt);
1057 break;
1058 }
1059
1060 case VIDIOC_G_TUNER: {
1061 struct v4l2_tuner *vt = arg;
1062
1063 if (vt->index != 0)
1064 return -EINVAL;
1065
1066 memset(vt, 0, sizeof(*vt));
1067 ivtv_call_i2c_clients(itv, VIDIOC_G_TUNER, vt);
1068
1069 if (test_bit(IVTV_F_I_RADIO_USER, &itv->i_flags)) {
1070 strcpy(vt->name, "ivtv Radio Tuner");
1071 vt->type = V4L2_TUNER_RADIO;
1072 } else {
1073 strcpy(vt->name, "ivtv TV Tuner");
1074 vt->type = V4L2_TUNER_ANALOG_TV;
1075 }
1076 break;
1077 }
1078
1079 case VIDIOC_G_SLICED_VBI_CAP: {
1080 struct v4l2_sliced_vbi_cap *cap = arg;
1081 int set = itv->is_50hz ? V4L2_SLICED_VBI_625 : V4L2_SLICED_VBI_525;
1082 int f, l;
1083 enum v4l2_buf_type type = cap->type;
1084
1085 memset(cap, 0, sizeof(*cap));
1086 cap->type = type;
1087 if (type == V4L2_BUF_TYPE_SLICED_VBI_CAPTURE) {
1088 for (f = 0; f < 2; f++) {
1089 for (l = 0; l < 24; l++) {
1090 if (valid_service_line(f, l, itv->is_50hz)) {
1091 cap->service_lines[f][l] = set;
1092 }
1093 }
1094 }
1095 return 0;
1096 }
1097 if (type == V4L2_BUF_TYPE_SLICED_VBI_OUTPUT) {
1098 if (!(itv->v4l2_cap & V4L2_CAP_SLICED_VBI_OUTPUT))
1099 return -EINVAL;
1100 if (itv->is_60hz) {
1101 cap->service_lines[0][21] = V4L2_SLICED_CAPTION_525;
1102 cap->service_lines[1][21] = V4L2_SLICED_CAPTION_525;
1103 } else {
1104 cap->service_lines[0][23] = V4L2_SLICED_WSS_625;
1105 cap->service_lines[0][16] = V4L2_SLICED_VPS;
1106 }
1107 return 0;
1108 }
1109 return -EINVAL;
1110 }
1111
1112 case VIDIOC_G_ENC_INDEX: {
1113 struct v4l2_enc_idx *idx = arg;
1114 struct v4l2_enc_idx_entry *e = idx->entry;
1115 int entries;
1116 int i;
1117
1118 entries = (itv->pgm_info_write_idx + IVTV_MAX_PGM_INDEX - itv->pgm_info_read_idx) %
1119 IVTV_MAX_PGM_INDEX;
1120 if (entries > V4L2_ENC_IDX_ENTRIES)
1121 entries = V4L2_ENC_IDX_ENTRIES;
1122 idx->entries = 0;
1123 for (i = 0; i < entries; i++) {
1124 *e = itv->pgm_info[(itv->pgm_info_read_idx + i) % IVTV_MAX_PGM_INDEX];
1125 if ((e->flags & V4L2_ENC_IDX_FRAME_MASK) <= V4L2_ENC_IDX_FRAME_B) {
1126 idx->entries++;
1127 e++;
1128 }
1129 }
1130 itv->pgm_info_read_idx = (itv->pgm_info_read_idx + idx->entries) % IVTV_MAX_PGM_INDEX;
1131 break;
1132 }
1133
1134 case VIDIOC_ENCODER_CMD:
1135 case VIDIOC_TRY_ENCODER_CMD: {
1136 struct v4l2_encoder_cmd *enc = arg;
1137 int try = cmd == VIDIOC_TRY_ENCODER_CMD;
1138
1139 memset(&enc->raw, 0, sizeof(enc->raw));
1140 switch (enc->cmd) {
1141 case V4L2_ENC_CMD_START:
1142 IVTV_DEBUG_IOCTL("V4L2_ENC_CMD_START\n");
1143 enc->flags = 0;
1144 if (try)
1145 return 0;
1146 return ivtv_start_capture(id);
1147
1148 case V4L2_ENC_CMD_STOP:
1149 IVTV_DEBUG_IOCTL("V4L2_ENC_CMD_STOP\n");
1150 enc->flags &= V4L2_ENC_CMD_STOP_AT_GOP_END;
1151 if (try)
1152 return 0;
1153 ivtv_stop_capture(id, enc->flags & V4L2_ENC_CMD_STOP_AT_GOP_END);
1154 return 0;
1155
1156 case V4L2_ENC_CMD_PAUSE:
1157 IVTV_DEBUG_IOCTL("V4L2_ENC_CMD_PAUSE\n");
1158 enc->flags = 0;
1159 if (try)
1160 return 0;
1161 if (!atomic_read(&itv->capturing))
1162 return -EPERM;
1163 if (test_and_set_bit(IVTV_F_I_ENC_PAUSED, &itv->i_flags))
1164 return 0;
1165 ivtv_mute(itv);
1166 ivtv_vapi(itv, CX2341X_ENC_PAUSE_ENCODER, 1, 0);
1167 break;
1168
1169 case V4L2_ENC_CMD_RESUME:
1170 IVTV_DEBUG_IOCTL("V4L2_ENC_CMD_RESUME\n");
1171 enc->flags = 0;
1172 if (try)
1173 return 0;
1174 if (!atomic_read(&itv->capturing))
1175 return -EPERM;
1176 if (!test_and_clear_bit(IVTV_F_I_ENC_PAUSED, &itv->i_flags))
1177 return 0;
1178 ivtv_vapi(itv, CX2341X_ENC_PAUSE_ENCODER, 1, 1);
1179 ivtv_unmute(itv);
1180 break;
1181 default:
1182 IVTV_DEBUG_IOCTL("Unknown cmd %d\n", enc->cmd);
1183 return -EINVAL;
1184 }
1185 break;
1186 }
1187
1188 case VIDIOC_G_FBUF: {
1189 struct v4l2_framebuffer *fb = arg;
1190 int pixfmt;
1191 static u32 pixel_format[16] = {
1192 V4L2_PIX_FMT_PAL8,
1193 V4L2_PIX_FMT_RGB565,
1194 V4L2_PIX_FMT_RGB555,
1195 V4L2_PIX_FMT_RGB444,
1196 V4L2_PIX_FMT_RGB32,
1197 0,
1198 0,
1199 0,
1200 V4L2_PIX_FMT_PAL8,
1201 V4L2_PIX_FMT_YUV565,
1202 V4L2_PIX_FMT_YUV555,
1203 V4L2_PIX_FMT_YUV444,
1204 V4L2_PIX_FMT_YUV32,
1205 0,
1206 0,
1207 0,
1208 };
1209
1210 memset(fb, 0, sizeof(*fb));
1211 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT_OVERLAY))
1212 return -EINVAL;
1213 fb->capability = V4L2_FBUF_CAP_EXTERNOVERLAY | V4L2_FBUF_CAP_CHROMAKEY |
1214 V4L2_FBUF_CAP_GLOBAL_ALPHA;
1215 ivtv_vapi_result(itv, data, CX2341X_OSD_GET_STATE, 0);
1216 data[0] |= (read_reg(0x2a00) >> 7) & 0x40;
1217 pixfmt = (data[0] >> 3) & 0xf;
1218 fb->fmt.pixelformat = pixel_format[pixfmt];
1219 fb->fmt.width = itv->osd_rect.width;
1220 fb->fmt.height = itv->osd_rect.height;
1221 fb->base = (void *)itv->osd_video_pbase;
1222 if (itv->osd_chroma_key_state)
1223 fb->flags |= V4L2_FBUF_FLAG_CHROMAKEY;
1224 if (itv->osd_global_alpha_state)
1225 fb->flags |= V4L2_FBUF_FLAG_GLOBAL_ALPHA;
1226 pixfmt &= 7;
1227
1228 if (pixfmt == 1 || pixfmt > 4)
1229 break;
1230
1231 if (pixfmt == 2 || pixfmt == 3)
1232 fb->capability |= V4L2_FBUF_CAP_LOCAL_INV_ALPHA;
1233 else
1234 fb->capability |= V4L2_FBUF_CAP_LOCAL_ALPHA;
1235 if (itv->osd_local_alpha_state) {
1236
1237 if (pixfmt == 2 || pixfmt == 3)
1238 fb->flags |= V4L2_FBUF_FLAG_LOCAL_INV_ALPHA;
1239 else
1240 fb->flags |= V4L2_FBUF_FLAG_LOCAL_ALPHA;
1241 }
1242 break;
1243 }
1244
1245 case VIDIOC_S_FBUF: {
1246 struct v4l2_framebuffer *fb = arg;
1247
1248 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT_OVERLAY))
1249 return -EINVAL;
1250 itv->osd_global_alpha_state = (fb->flags & V4L2_FBUF_FLAG_GLOBAL_ALPHA) != 0;
1251 itv->osd_local_alpha_state =
1252 (fb->flags & (V4L2_FBUF_FLAG_LOCAL_ALPHA|V4L2_FBUF_FLAG_LOCAL_INV_ALPHA)) != 0;
1253 itv->osd_chroma_key_state = (fb->flags & V4L2_FBUF_FLAG_CHROMAKEY) != 0;
1254 ivtv_set_osd_alpha(itv);
1255 break;
1256 }
1257
1258 case VIDIOC_OVERLAY: {
1259 int *on = arg;
1260
1261 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT_OVERLAY))
1262 return -EINVAL;
1263 ivtv_vapi(itv, CX2341X_OSD_SET_STATE, 1, *on != 0);
1264 break;
1265 }
1266
1267 case VIDIOC_LOG_STATUS:
1268 {
1269 int has_output = itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT;
1270 struct v4l2_input vidin;
1271 struct v4l2_audio audin;
1272 int i;
1273
1274 IVTV_INFO("================= START STATUS CARD #%d =================\n", itv->num);
1275 IVTV_INFO("Version: %s Card: %s\n", IVTV_VERSION, itv->card_name);
1276 if (itv->hw_flags & IVTV_HW_TVEEPROM) {
1277 struct tveeprom tv;
1278
1279 ivtv_read_eeprom(itv, &tv);
1280 }
1281 ivtv_call_i2c_clients(itv, VIDIOC_LOG_STATUS, NULL);
1282 ivtv_get_input(itv, itv->active_input, &vidin);
1283 ivtv_get_audio_input(itv, itv->audio_input, &audin);
1284 IVTV_INFO("Video Input: %s\n", vidin.name);
1285 IVTV_INFO("Audio Input: %s%s\n", audin.name,
1286 (itv->dualwatch_stereo_mode & ~0x300) == 0x200 ? " (Bilingual)" : "");
1287 if (has_output) {
1288 struct v4l2_output vidout;
1289 struct v4l2_audioout audout;
1290 int mode = itv->output_mode;
1291 static const char * const output_modes[5] = {
1292 "None",
1293 "MPEG Streaming",
1294 "YUV Streaming",
1295 "YUV Frames",
1296 "Passthrough",
1297 };
1298 static const char * const audio_modes[5] = {
1299 "Stereo",
1300 "Left",
1301 "Right",
1302 "Mono",
1303 "Swapped"
1304 };
1305 static const char * const alpha_mode[4] = {
1306 "None",
1307 "Global",
1308 "Local",
1309 "Global and Local"
1310 };
1311 static const char * const pixel_format[16] = {
1312 "ARGB Indexed",
1313 "RGB 5:6:5",
1314 "ARGB 1:5:5:5",
1315 "ARGB 1:4:4:4",
1316 "ARGB 8:8:8:8",
1317 "5",
1318 "6",
1319 "7",
1320 "AYUV Indexed",
1321 "YUV 5:6:5",
1322 "AYUV 1:5:5:5",
1323 "AYUV 1:4:4:4",
1324 "AYUV 8:8:8:8",
1325 "13",
1326 "14",
1327 "15",
1328 };
1329
1330 ivtv_get_output(itv, itv->active_output, &vidout);
1331 ivtv_get_audio_output(itv, 0, &audout);
1332 IVTV_INFO("Video Output: %s\n", vidout.name);
1333 IVTV_INFO("Audio Output: %s (Stereo/Bilingual: %s/%s)\n", audout.name,
1334 audio_modes[itv->audio_stereo_mode],
1335 audio_modes[itv->audio_bilingual_mode]);
1336 if (mode < 0 || mode > OUT_PASSTHROUGH)
1337 mode = OUT_NONE;
1338 IVTV_INFO("Output Mode: %s\n", output_modes[mode]);
1339 ivtv_vapi_result(itv, data, CX2341X_OSD_GET_STATE, 0);
1340 data[0] |= (read_reg(0x2a00) >> 7) & 0x40;
1341 IVTV_INFO("Overlay: %s, Alpha: %s, Pixel Format: %s\n",
1342 data[0] & 1 ? "On" : "Off",
1343 alpha_mode[(data[0] >> 1) & 0x3],
1344 pixel_format[(data[0] >> 3) & 0xf]);
1345 }
1346 IVTV_INFO("Tuner: %s\n",
1347 test_bit(IVTV_F_I_RADIO_USER, &itv->i_flags) ? "Radio" : "TV");
1348 cx2341x_log_status(&itv->params, itv->name);
1349 IVTV_INFO("Status flags: 0x%08lx\n", itv->i_flags);
1350 for (i = 0; i < IVTV_MAX_STREAMS; i++) {
1351 struct ivtv_stream *s = &itv->streams[i];
1352
1353 if (s->v4l2dev == NULL || s->buffers == 0)
1354 continue;
1355 IVTV_INFO("Stream %s: status 0x%04lx, %d%% of %d KiB (%d buffers) in use\n", s->name, s->s_flags,
1356 (s->buffers - s->q_free.buffers) * 100 / s->buffers,
1357 (s->buffers * s->buf_size) / 1024, s->buffers);
1358 }
1359 IVTV_INFO("Read MPG/VBI: %lld/%lld bytes\n", (long long)itv->mpg_data_received, (long long)itv->vbi_data_inserted);
1360 IVTV_INFO("================== END STATUS CARD #%d ==================\n", itv->num);
1361 break;
1362 }
1363
1364 default:
1365 return -EINVAL;
1366 }
1367 return 0;
1368}
1369
1370static int ivtv_decoder_ioctls(struct file *filp, unsigned int cmd, void *arg)
1371{
1372 struct ivtv_open_id *id = (struct ivtv_open_id *)filp->private_data;
1373 struct ivtv *itv = id->itv;
1374 int nonblocking = filp->f_flags & O_NONBLOCK;
1375 struct ivtv_stream *s = &itv->streams[id->type];
1376
1377 switch (cmd) {
1378 case IVTV_IOC_DMA_FRAME: {
1379 struct ivtv_dma_frame *args = arg;
1380
1381 IVTV_DEBUG_IOCTL("IVTV_IOC_DMA_FRAME\n");
1382 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
1383 return -EINVAL;
1384 if (args->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
1385 return -EINVAL;
1386 if (itv->output_mode == OUT_UDMA_YUV && args->y_source == NULL)
1387 return 0;
1388 if (ivtv_claim_stream(id, id->type)) {
1389 return -EBUSY;
1390 }
1391 if (ivtv_set_output_mode(itv, OUT_UDMA_YUV) != OUT_UDMA_YUV) {
1392 ivtv_release_stream(s);
1393 return -EBUSY;
1394 }
1395
1396 id->yuv_frames = 1;
1397 if (args->y_source == NULL)
1398 return 0;
1399 return ivtv_yuv_prep_frame(itv, args);
1400 }
1401
1402 case VIDEO_GET_PTS: {
1403 u32 data[CX2341X_MBOX_MAX_DATA];
1404 u64 *pts = arg;
1405
1406 IVTV_DEBUG_IOCTL("VIDEO_GET_PTS\n");
1407 if (s->type < IVTV_DEC_STREAM_TYPE_MPG) {
1408 *pts = s->dma_pts;
1409 break;
1410 }
1411 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
1412 return -EINVAL;
1413
1414 if (test_bit(IVTV_F_I_VALID_DEC_TIMINGS, &itv->i_flags)) {
1415 *pts = (u64) ((u64)itv->last_dec_timing[2] << 32) |
1416 (u64)itv->last_dec_timing[1];
1417 break;
1418 }
1419 *pts = 0;
1420 if (atomic_read(&itv->decoding)) {
1421 if (ivtv_api(itv, CX2341X_DEC_GET_TIMING_INFO, 5, data)) {
1422 IVTV_DEBUG_WARN("GET_TIMING: couldn't read clock\n");
1423 return -EIO;
1424 }
1425 memcpy(itv->last_dec_timing, data, sizeof(itv->last_dec_timing));
1426 set_bit(IVTV_F_I_VALID_DEC_TIMINGS, &itv->i_flags);
1427 *pts = (u64) ((u64) data[2] << 32) | (u64) data[1];
1428
1429 }
1430 break;
1431 }
1432
1433 case VIDEO_GET_FRAME_COUNT: {
1434 u32 data[CX2341X_MBOX_MAX_DATA];
1435 u64 *frame = arg;
1436
1437 IVTV_DEBUG_IOCTL("VIDEO_GET_FRAME_COUNT\n");
1438 if (s->type < IVTV_DEC_STREAM_TYPE_MPG) {
1439 *frame = 0;
1440 break;
1441 }
1442 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
1443 return -EINVAL;
1444
1445 if (test_bit(IVTV_F_I_VALID_DEC_TIMINGS, &itv->i_flags)) {
1446 *frame = itv->last_dec_timing[0];
1447 break;
1448 }
1449 *frame = 0;
1450 if (atomic_read(&itv->decoding)) {
1451 if (ivtv_api(itv, CX2341X_DEC_GET_TIMING_INFO, 5, data)) {
1452 IVTV_DEBUG_WARN("GET_TIMING: couldn't read clock\n");
1453 return -EIO;
1454 }
1455 memcpy(itv->last_dec_timing, data, sizeof(itv->last_dec_timing));
1456 set_bit(IVTV_F_I_VALID_DEC_TIMINGS, &itv->i_flags);
1457 *frame = data[0];
1458 }
1459 break;
1460 }
1461
1462 case VIDEO_PLAY: {
1463 struct video_command vc;
1464
1465 IVTV_DEBUG_IOCTL("VIDEO_PLAY\n");
1466 memset(&vc, 0, sizeof(vc));
1467 vc.cmd = VIDEO_CMD_PLAY;
1468 return ivtv_video_command(itv, id, &vc, 0);
1469 }
1470
1471 case VIDEO_STOP: {
1472 struct video_command vc;
1473
1474 IVTV_DEBUG_IOCTL("VIDEO_STOP\n");
1475 memset(&vc, 0, sizeof(vc));
1476 vc.cmd = VIDEO_CMD_STOP;
1477 vc.flags = VIDEO_CMD_STOP_TO_BLACK | VIDEO_CMD_STOP_IMMEDIATELY;
1478 return ivtv_video_command(itv, id, &vc, 0);
1479 }
1480
1481 case VIDEO_FREEZE: {
1482 struct video_command vc;
1483
1484 IVTV_DEBUG_IOCTL("VIDEO_FREEZE\n");
1485 memset(&vc, 0, sizeof(vc));
1486 vc.cmd = VIDEO_CMD_FREEZE;
1487 return ivtv_video_command(itv, id, &vc, 0);
1488 }
1489
1490 case VIDEO_CONTINUE: {
1491 struct video_command vc;
1492
1493 IVTV_DEBUG_IOCTL("VIDEO_CONTINUE\n");
1494 memset(&vc, 0, sizeof(vc));
1495 vc.cmd = VIDEO_CMD_CONTINUE;
1496 return ivtv_video_command(itv, id, &vc, 0);
1497 }
1498
1499 case VIDEO_COMMAND:
1500 case VIDEO_TRY_COMMAND: {
1501 struct video_command *vc = arg;
1502 int try = (cmd == VIDEO_TRY_COMMAND);
1503
1504 if (try)
1505 IVTV_DEBUG_IOCTL("VIDEO_TRY_COMMAND %d\n", vc->cmd);
1506 else
1507 IVTV_DEBUG_IOCTL("VIDEO_COMMAND %d\n", vc->cmd);
1508 return ivtv_video_command(itv, id, vc, try);
1509 }
1510
1511 case VIDEO_GET_EVENT: {
1512 struct video_event *ev = arg;
1513 DEFINE_WAIT(wait);
1514
1515 IVTV_DEBUG_IOCTL("VIDEO_GET_EVENT\n");
1516 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
1517 return -EINVAL;
1518 memset(ev, 0, sizeof(*ev));
1519 set_bit(IVTV_F_I_EV_VSYNC_ENABLED, &itv->i_flags);
1520
1521 while (1) {
1522 if (test_and_clear_bit(IVTV_F_I_EV_DEC_STOPPED, &itv->i_flags))
1523 ev->type = VIDEO_EVENT_DECODER_STOPPED;
1524 else if (test_and_clear_bit(IVTV_F_I_EV_VSYNC, &itv->i_flags)) {
1525 ev->type = VIDEO_EVENT_VSYNC;
1526 ev->u.vsync_field = test_bit(IVTV_F_I_EV_VSYNC_FIELD, &itv->i_flags) ?
1527 VIDEO_VSYNC_FIELD_ODD : VIDEO_VSYNC_FIELD_EVEN;
1528 if (itv->output_mode == OUT_UDMA_YUV &&
1529 (itv->yuv_info.lace_mode & IVTV_YUV_MODE_MASK) ==
1530 IVTV_YUV_MODE_PROGRESSIVE) {
1531 ev->u.vsync_field = VIDEO_VSYNC_FIELD_PROGRESSIVE;
1532 }
1533 }
1534 if (ev->type)
1535 return 0;
1536 if (nonblocking)
1537 return -EAGAIN;
1538
1539
1540
1541 mutex_unlock(&itv->serialize_lock);
1542 prepare_to_wait(&itv->event_waitq, &wait, TASK_INTERRUPTIBLE);
1543 if ((itv->i_flags & (IVTV_F_I_EV_DEC_STOPPED|IVTV_F_I_EV_VSYNC)) == 0)
1544 schedule();
1545 finish_wait(&itv->event_waitq, &wait);
1546 mutex_lock(&itv->serialize_lock);
1547 if (signal_pending(current)) {
1548
1549 IVTV_DEBUG_INFO("User stopped wait for event\n");
1550 return -EINTR;
1551 }
1552 }
1553 break;
1554 }
1555
1556 default:
1557 return -EINVAL;
1558 }
1559 return 0;
1560}
1561
1562static int ivtv_v4l2_do_ioctl(struct inode *inode, struct file *filp,
1563 unsigned int cmd, void *arg)
1564{
1565 struct ivtv_open_id *id = (struct ivtv_open_id *)filp->private_data;
1566 struct ivtv *itv = id->itv;
1567 int ret;
1568
1569
1570 switch (cmd) {
1571 case VIDIOC_S_CTRL:
1572 case VIDIOC_S_STD:
1573 case VIDIOC_S_INPUT:
1574 case VIDIOC_S_OUTPUT:
1575 case VIDIOC_S_TUNER:
1576 case VIDIOC_S_FREQUENCY:
1577 case VIDIOC_S_FMT:
1578 case VIDIOC_S_CROP:
1579 case VIDIOC_S_AUDIO:
1580 case VIDIOC_S_AUDOUT:
1581 case VIDIOC_S_EXT_CTRLS:
1582 case VIDIOC_S_FBUF:
1583 case VIDIOC_OVERLAY:
1584 ret = v4l2_prio_check(&itv->prio, &id->prio);
1585 if (ret)
1586 return ret;
1587 }
1588
1589 switch (cmd) {
1590 case VIDIOC_DBG_G_REGISTER:
1591 case VIDIOC_DBG_S_REGISTER:
1592 case VIDIOC_G_CHIP_IDENT:
1593 case VIDIOC_INT_S_AUDIO_ROUTING:
1594 case VIDIOC_INT_RESET:
1595 if (ivtv_debug & IVTV_DBGFLG_IOCTL) {
1596 printk(KERN_INFO "ivtv%d ioctl: ", itv->num);
1597 v4l_printk_ioctl(cmd);
1598 }
1599 return ivtv_debug_ioctls(filp, cmd, arg);
1600
1601 case VIDIOC_G_PRIORITY:
1602 case VIDIOC_S_PRIORITY:
1603 case VIDIOC_QUERYCAP:
1604 case VIDIOC_ENUMINPUT:
1605 case VIDIOC_G_INPUT:
1606 case VIDIOC_S_INPUT:
1607 case VIDIOC_ENUMOUTPUT:
1608 case VIDIOC_G_OUTPUT:
1609 case VIDIOC_S_OUTPUT:
1610 case VIDIOC_G_FMT:
1611 case VIDIOC_S_FMT:
1612 case VIDIOC_TRY_FMT:
1613 case VIDIOC_ENUM_FMT:
1614 case VIDIOC_CROPCAP:
1615 case VIDIOC_G_CROP:
1616 case VIDIOC_S_CROP:
1617 case VIDIOC_G_FREQUENCY:
1618 case VIDIOC_S_FREQUENCY:
1619 case VIDIOC_ENUMSTD:
1620 case VIDIOC_G_STD:
1621 case VIDIOC_S_STD:
1622 case VIDIOC_S_TUNER:
1623 case VIDIOC_G_TUNER:
1624 case VIDIOC_ENUMAUDIO:
1625 case VIDIOC_S_AUDIO:
1626 case VIDIOC_G_AUDIO:
1627 case VIDIOC_ENUMAUDOUT:
1628 case VIDIOC_S_AUDOUT:
1629 case VIDIOC_G_AUDOUT:
1630 case VIDIOC_G_SLICED_VBI_CAP:
1631 case VIDIOC_LOG_STATUS:
1632 case VIDIOC_G_ENC_INDEX:
1633 case VIDIOC_ENCODER_CMD:
1634 case VIDIOC_TRY_ENCODER_CMD:
1635 case VIDIOC_G_FBUF:
1636 case VIDIOC_S_FBUF:
1637 case VIDIOC_OVERLAY:
1638 if (ivtv_debug & IVTV_DBGFLG_IOCTL) {
1639 printk(KERN_INFO "ivtv%d ioctl: ", itv->num);
1640 v4l_printk_ioctl(cmd);
1641 }
1642 return ivtv_v4l2_ioctls(itv, filp, cmd, arg);
1643
1644 case VIDIOC_QUERYMENU:
1645 case VIDIOC_QUERYCTRL:
1646 case VIDIOC_S_CTRL:
1647 case VIDIOC_G_CTRL:
1648 case VIDIOC_S_EXT_CTRLS:
1649 case VIDIOC_G_EXT_CTRLS:
1650 case VIDIOC_TRY_EXT_CTRLS:
1651 if (ivtv_debug & IVTV_DBGFLG_IOCTL) {
1652 printk(KERN_INFO "ivtv%d ioctl: ", itv->num);
1653 v4l_printk_ioctl(cmd);
1654 }
1655 return ivtv_control_ioctls(itv, cmd, arg);
1656
1657 case IVTV_IOC_DMA_FRAME:
1658 case VIDEO_GET_PTS:
1659 case VIDEO_GET_FRAME_COUNT:
1660 case VIDEO_GET_EVENT:
1661 case VIDEO_PLAY:
1662 case VIDEO_STOP:
1663 case VIDEO_FREEZE:
1664 case VIDEO_CONTINUE:
1665 case VIDEO_COMMAND:
1666 case VIDEO_TRY_COMMAND:
1667 return ivtv_decoder_ioctls(filp, cmd, arg);
1668
1669 case 0x00005401:
1670 return -EINVAL;
1671 default:
1672 return v4l_compat_translate_ioctl(inode, filp, cmd, arg,
1673 ivtv_v4l2_do_ioctl);
1674 }
1675 return 0;
1676}
1677
1678static int ivtv_serialized_ioctl(struct ivtv *itv, struct inode *inode, struct file *filp,
1679 unsigned int cmd, unsigned long arg)
1680{
1681
1682 switch (cmd) {
1683 case VIDEO_SELECT_SOURCE:
1684 IVTV_DEBUG_IOCTL("VIDEO_SELECT_SOURCE\n");
1685 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
1686 return -EINVAL;
1687 return ivtv_passthrough_mode(itv, arg == VIDEO_SOURCE_DEMUX);
1688
1689 case AUDIO_SET_MUTE:
1690 IVTV_DEBUG_IOCTL("AUDIO_SET_MUTE\n");
1691 itv->speed_mute_audio = arg;
1692 return 0;
1693
1694 case AUDIO_CHANNEL_SELECT:
1695 IVTV_DEBUG_IOCTL("AUDIO_CHANNEL_SELECT\n");
1696 if (arg > AUDIO_STEREO_SWAPPED)
1697 return -EINVAL;
1698 itv->audio_stereo_mode = arg;
1699 ivtv_vapi(itv, CX2341X_DEC_SET_AUDIO_MODE, 2, itv->audio_bilingual_mode, itv->audio_stereo_mode);
1700 return 0;
1701
1702 case AUDIO_BILINGUAL_CHANNEL_SELECT:
1703 IVTV_DEBUG_IOCTL("AUDIO_BILINGUAL_CHANNEL_SELECT\n");
1704 if (arg > AUDIO_STEREO_SWAPPED)
1705 return -EINVAL;
1706 itv->audio_bilingual_mode = arg;
1707 ivtv_vapi(itv, CX2341X_DEC_SET_AUDIO_MODE, 2, itv->audio_bilingual_mode, itv->audio_stereo_mode);
1708 return 0;
1709
1710 default:
1711 break;
1712 }
1713 return video_usercopy(inode, filp, cmd, arg, ivtv_v4l2_do_ioctl);
1714}
1715
1716int ivtv_v4l2_ioctl(struct inode *inode, struct file *filp, unsigned int cmd,
1717 unsigned long arg)
1718{
1719 struct ivtv_open_id *id = (struct ivtv_open_id *)filp->private_data;
1720 struct ivtv *itv = id->itv;
1721 int res;
1722
1723 mutex_lock(&itv->serialize_lock);
1724 res = ivtv_serialized_ioctl(itv, inode, filp, cmd, arg);
1725 mutex_unlock(&itv->serialize_lock);
1726 return res;
1727}
1728