1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36#include "ivtv-driver.h"
37#include "ivtv-fileops.h"
38#include "ivtv-queue.h"
39#include "ivtv-mailbox.h"
40#include "ivtv-ioctl.h"
41#include "ivtv-irq.h"
42#include "ivtv-yuv.h"
43#include "ivtv-cards.h"
44#include "ivtv-streams.h"
45#include "ivtv-firmware.h"
46#include <media/v4l2-event.h>
47
48static const struct v4l2_file_operations ivtv_v4l2_enc_fops = {
49 .owner = THIS_MODULE,
50 .read = ivtv_v4l2_read,
51 .write = ivtv_v4l2_write,
52 .open = ivtv_v4l2_open,
53 .unlocked_ioctl = video_ioctl2,
54 .release = ivtv_v4l2_close,
55 .poll = ivtv_v4l2_enc_poll,
56};
57
58static const struct v4l2_file_operations ivtv_v4l2_dec_fops = {
59 .owner = THIS_MODULE,
60 .read = ivtv_v4l2_read,
61 .write = ivtv_v4l2_write,
62 .open = ivtv_v4l2_open,
63 .unlocked_ioctl = video_ioctl2,
64 .release = ivtv_v4l2_close,
65 .poll = ivtv_v4l2_dec_poll,
66};
67
68static const struct v4l2_file_operations ivtv_v4l2_radio_fops = {
69 .owner = THIS_MODULE,
70 .open = ivtv_v4l2_open,
71 .unlocked_ioctl = video_ioctl2,
72 .release = ivtv_v4l2_close,
73 .poll = ivtv_v4l2_enc_poll,
74};
75
76#define IVTV_V4L2_DEC_MPG_OFFSET 16
77#define IVTV_V4L2_ENC_PCM_OFFSET 24
78#define IVTV_V4L2_ENC_YUV_OFFSET 32
79#define IVTV_V4L2_DEC_YUV_OFFSET 48
80#define IVTV_V4L2_DEC_VBI_OFFSET 8
81#define IVTV_V4L2_DEC_VOUT_OFFSET 16
82
83static struct {
84 const char *name;
85 int vfl_type;
86 int num_offset;
87 int dma, pio;
88 u32 v4l2_caps;
89 const struct v4l2_file_operations *fops;
90} ivtv_stream_info[] = {
91 {
92 "encoder MPG",
93 VFL_TYPE_GRABBER, 0,
94 PCI_DMA_FROMDEVICE, 0,
95 V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_TUNER |
96 V4L2_CAP_AUDIO | V4L2_CAP_READWRITE,
97 &ivtv_v4l2_enc_fops
98 },
99 {
100 "encoder YUV",
101 VFL_TYPE_GRABBER, IVTV_V4L2_ENC_YUV_OFFSET,
102 PCI_DMA_FROMDEVICE, 0,
103 V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_TUNER |
104 V4L2_CAP_AUDIO | V4L2_CAP_READWRITE,
105 &ivtv_v4l2_enc_fops
106 },
107 {
108 "encoder VBI",
109 VFL_TYPE_VBI, 0,
110 PCI_DMA_FROMDEVICE, 0,
111 V4L2_CAP_VBI_CAPTURE | V4L2_CAP_SLICED_VBI_CAPTURE | V4L2_CAP_TUNER |
112 V4L2_CAP_AUDIO | V4L2_CAP_READWRITE,
113 &ivtv_v4l2_enc_fops
114 },
115 {
116 "encoder PCM",
117 VFL_TYPE_GRABBER, IVTV_V4L2_ENC_PCM_OFFSET,
118 PCI_DMA_FROMDEVICE, 0,
119 V4L2_CAP_TUNER | V4L2_CAP_AUDIO | V4L2_CAP_READWRITE,
120 &ivtv_v4l2_enc_fops
121 },
122 {
123 "encoder radio",
124 VFL_TYPE_RADIO, 0,
125 PCI_DMA_NONE, 1,
126 V4L2_CAP_RADIO | V4L2_CAP_TUNER,
127 &ivtv_v4l2_radio_fops
128 },
129 {
130 "decoder MPG",
131 VFL_TYPE_GRABBER, IVTV_V4L2_DEC_MPG_OFFSET,
132 PCI_DMA_TODEVICE, 0,
133 V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_AUDIO | V4L2_CAP_READWRITE |
134 V4L2_CAP_VIDEO_OUTPUT_OVERLAY,
135 &ivtv_v4l2_dec_fops
136 },
137 {
138 "decoder VBI",
139 VFL_TYPE_VBI, IVTV_V4L2_DEC_VBI_OFFSET,
140 PCI_DMA_NONE, 1,
141 V4L2_CAP_SLICED_VBI_CAPTURE | V4L2_CAP_READWRITE,
142 &ivtv_v4l2_enc_fops
143 },
144 {
145 "decoder VOUT",
146 VFL_TYPE_VBI, IVTV_V4L2_DEC_VOUT_OFFSET,
147 PCI_DMA_NONE, 1,
148 V4L2_CAP_SLICED_VBI_OUTPUT | V4L2_CAP_AUDIO | V4L2_CAP_READWRITE,
149 &ivtv_v4l2_dec_fops
150 },
151 {
152 "decoder YUV",
153 VFL_TYPE_GRABBER, IVTV_V4L2_DEC_YUV_OFFSET,
154 PCI_DMA_TODEVICE, 0,
155 V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_AUDIO | V4L2_CAP_READWRITE |
156 V4L2_CAP_VIDEO_OUTPUT_OVERLAY,
157 &ivtv_v4l2_dec_fops
158 }
159};
160
161static void ivtv_stream_init(struct ivtv *itv, int type)
162{
163 struct ivtv_stream *s = &itv->streams[type];
164
165
166 memset(s, 0, sizeof(*s));
167
168
169 s->itv = itv;
170 s->type = type;
171 s->name = ivtv_stream_info[type].name;
172 s->caps = ivtv_stream_info[type].v4l2_caps;
173
174 if (ivtv_stream_info[type].pio)
175 s->dma = PCI_DMA_NONE;
176 else
177 s->dma = ivtv_stream_info[type].dma;
178 s->buf_size = itv->stream_buf_size[type];
179 if (s->buf_size)
180 s->buffers = (itv->options.kilobytes[type] * 1024 + s->buf_size - 1) / s->buf_size;
181 spin_lock_init(&s->qlock);
182 init_waitqueue_head(&s->waitq);
183 s->sg_handle = IVTV_DMA_UNMAPPED;
184 ivtv_queue_init(&s->q_free);
185 ivtv_queue_init(&s->q_full);
186 ivtv_queue_init(&s->q_dma);
187 ivtv_queue_init(&s->q_predma);
188 ivtv_queue_init(&s->q_io);
189}
190
191static int ivtv_prep_dev(struct ivtv *itv, int type)
192{
193 struct ivtv_stream *s = &itv->streams[type];
194 int num_offset = ivtv_stream_info[type].num_offset;
195 int num = itv->instance + ivtv_first_minor + num_offset;
196
197
198
199
200 s->vdev.v4l2_dev = NULL;
201 s->itv = itv;
202 s->type = type;
203 s->name = ivtv_stream_info[type].name;
204
205
206 if (type == IVTV_ENC_STREAM_TYPE_RAD && !(itv->v4l2_cap & V4L2_CAP_RADIO))
207 return 0;
208 if (type >= IVTV_DEC_STREAM_TYPE_MPG && !(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
209 return 0;
210
211
212
213 if (ivtv_stream_info[type].dma != PCI_DMA_NONE &&
214 itv->options.kilobytes[type] == 0) {
215 IVTV_INFO("Disabled %s device\n", ivtv_stream_info[type].name);
216 return 0;
217 }
218
219 ivtv_stream_init(itv, type);
220
221 snprintf(s->vdev.name, sizeof(s->vdev.name), "%s %s",
222 itv->v4l2_dev.name, s->name);
223
224 s->vdev.num = num;
225 s->vdev.v4l2_dev = &itv->v4l2_dev;
226 if (ivtv_stream_info[type].v4l2_caps &
227 (V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_SLICED_VBI_OUTPUT))
228 s->vdev.vfl_dir = VFL_DIR_TX;
229 s->vdev.fops = ivtv_stream_info[type].fops;
230 s->vdev.ctrl_handler = itv->v4l2_dev.ctrl_handler;
231 s->vdev.release = video_device_release_empty;
232 s->vdev.tvnorms = V4L2_STD_ALL;
233 s->vdev.lock = &itv->serialize_lock;
234 if (s->type == IVTV_DEC_STREAM_TYPE_VBI) {
235 v4l2_disable_ioctl(&s->vdev, VIDIOC_S_AUDIO);
236 v4l2_disable_ioctl(&s->vdev, VIDIOC_G_AUDIO);
237 v4l2_disable_ioctl(&s->vdev, VIDIOC_ENUMAUDIO);
238 v4l2_disable_ioctl(&s->vdev, VIDIOC_ENUMINPUT);
239 v4l2_disable_ioctl(&s->vdev, VIDIOC_S_INPUT);
240 v4l2_disable_ioctl(&s->vdev, VIDIOC_G_INPUT);
241 v4l2_disable_ioctl(&s->vdev, VIDIOC_S_FREQUENCY);
242 v4l2_disable_ioctl(&s->vdev, VIDIOC_G_FREQUENCY);
243 v4l2_disable_ioctl(&s->vdev, VIDIOC_S_TUNER);
244 v4l2_disable_ioctl(&s->vdev, VIDIOC_G_TUNER);
245 v4l2_disable_ioctl(&s->vdev, VIDIOC_S_STD);
246 }
247 ivtv_set_funcs(&s->vdev);
248 return 0;
249}
250
251
252int ivtv_streams_setup(struct ivtv *itv)
253{
254 int type;
255
256
257 for (type = 0; type < IVTV_MAX_STREAMS; type++) {
258
259 if (ivtv_prep_dev(itv, type))
260 break;
261
262 if (itv->streams[type].vdev.v4l2_dev == NULL)
263 continue;
264
265
266 if (ivtv_stream_alloc(&itv->streams[type]))
267 break;
268 }
269 if (type == IVTV_MAX_STREAMS)
270 return 0;
271
272
273 ivtv_streams_cleanup(itv);
274 return -ENOMEM;
275}
276
277static int ivtv_reg_dev(struct ivtv *itv, int type)
278{
279 struct ivtv_stream *s = &itv->streams[type];
280 int vfl_type = ivtv_stream_info[type].vfl_type;
281 const char *name;
282 int num;
283
284 if (s->vdev.v4l2_dev == NULL)
285 return 0;
286
287 num = s->vdev.num;
288
289 if (type != IVTV_ENC_STREAM_TYPE_MPG) {
290 struct ivtv_stream *s_mpg = &itv->streams[IVTV_ENC_STREAM_TYPE_MPG];
291
292 if (s_mpg->vdev.v4l2_dev)
293 num = s_mpg->vdev.num + ivtv_stream_info[type].num_offset;
294 }
295 video_set_drvdata(&s->vdev, s);
296
297
298 if (video_register_device_no_warn(&s->vdev, vfl_type, num)) {
299 IVTV_ERR("Couldn't register v4l2 device for %s (device node number %d)\n",
300 s->name, num);
301 return -ENOMEM;
302 }
303 name = video_device_node_name(&s->vdev);
304
305 switch (vfl_type) {
306 case VFL_TYPE_GRABBER:
307 IVTV_INFO("Registered device %s for %s (%d kB)\n",
308 name, s->name, itv->options.kilobytes[type]);
309 break;
310 case VFL_TYPE_RADIO:
311 IVTV_INFO("Registered device %s for %s\n",
312 name, s->name);
313 break;
314 case VFL_TYPE_VBI:
315 if (itv->options.kilobytes[type])
316 IVTV_INFO("Registered device %s for %s (%d kB)\n",
317 name, s->name, itv->options.kilobytes[type]);
318 else
319 IVTV_INFO("Registered device %s for %s\n",
320 name, s->name);
321 break;
322 }
323 return 0;
324}
325
326
327int ivtv_streams_register(struct ivtv *itv)
328{
329 int type;
330 int err = 0;
331
332
333 for (type = 0; type < IVTV_MAX_STREAMS; type++)
334 err |= ivtv_reg_dev(itv, type);
335
336 if (err == 0)
337 return 0;
338
339
340 ivtv_streams_cleanup(itv);
341 return -ENOMEM;
342}
343
344
345void ivtv_streams_cleanup(struct ivtv *itv)
346{
347 int type;
348
349
350 for (type = 0; type < IVTV_MAX_STREAMS; type++) {
351 struct video_device *vdev = &itv->streams[type].vdev;
352
353 if (vdev->v4l2_dev == NULL)
354 continue;
355
356 video_unregister_device(vdev);
357 ivtv_stream_free(&itv->streams[type]);
358 itv->streams[type].vdev.v4l2_dev = NULL;
359 }
360}
361
362static void ivtv_vbi_setup(struct ivtv *itv)
363{
364 int raw = ivtv_raw_vbi(itv);
365 u32 data[CX2341X_MBOX_MAX_DATA];
366 int lines;
367 int i;
368
369
370 ivtv_vapi(itv, CX2341X_ENC_SET_VBI_LINE, 5, 0xffff , 0, 0, 0, 0);
371
372
373 if (raw)
374 v4l2_subdev_call(itv->sd_video, vbi, s_raw_fmt, &itv->vbi.in.fmt.vbi);
375 else
376 v4l2_subdev_call(itv->sd_video, vbi, s_sliced_fmt, &itv->vbi.in.fmt.sliced);
377
378
379
380
381
382
383 if (raw) {
384 lines = itv->vbi.count * 2;
385 } else {
386 lines = itv->is_60hz ? 24 : 38;
387 if (itv->is_60hz && (itv->hw_flags & IVTV_HW_CX25840))
388 lines += 2;
389 }
390
391 itv->vbi.enc_size = lines * (raw ? itv->vbi.raw_size : itv->vbi.sliced_size);
392
393
394
395 data[0] = raw | 0x02 | (0xbd << 8);
396
397
398 data[1] = 1;
399
400 data[2] = raw ? 4 : 4 * (itv->vbi.raw_size / itv->vbi.enc_size);
401
402
403
404
405
406
407
408 if (itv->hw_flags & IVTV_HW_CX25840) {
409
410 if (raw) {
411 data[3] = 0x20602060;
412 data[4] = 0x30703070;
413 } else {
414 data[3] = 0xB0F0B0F0;
415 data[4] = 0xA0E0A0E0;
416 }
417
418 data[5] = lines;
419
420 data[6] = (raw ? itv->vbi.raw_size : itv->vbi.sliced_size);
421 } else {
422
423 if (raw) {
424 data[3] = 0x25256262;
425 data[4] = 0x387F7F7F;
426 } else {
427 data[3] = 0xABABECEC;
428 data[4] = 0xB6F1F1F1;
429 }
430
431 data[5] = lines;
432
433 data[6] = itv->vbi.enc_size / lines;
434 }
435
436 IVTV_DEBUG_INFO(
437 "Setup VBI API header 0x%08x pkts %d buffs %d ln %d sz %d\n",
438 data[0], data[1], data[2], data[5], data[6]);
439
440 ivtv_api(itv, CX2341X_ENC_SET_VBI_CONFIG, 7, data);
441
442
443 itv->vbi.enc_start = data[2];
444 itv->vbi.fpi = data[0];
445 if (!itv->vbi.fpi)
446 itv->vbi.fpi = 1;
447
448 IVTV_DEBUG_INFO("Setup VBI start 0x%08x frames %d fpi %d\n",
449 itv->vbi.enc_start, data[1], itv->vbi.fpi);
450
451
452
453 for (i = 2; i <= 24; i++) {
454 int valid;
455
456 if (itv->is_60hz) {
457 valid = i >= 10 && i < 22;
458 } else {
459 valid = i >= 6 && i < 24;
460 }
461 ivtv_vapi(itv, CX2341X_ENC_SET_VBI_LINE, 5, i - 1,
462 valid, 0 , 0, 0);
463 ivtv_vapi(itv, CX2341X_ENC_SET_VBI_LINE, 5, (i - 1) | 0x80000000,
464 valid, 0, 0, 0);
465 }
466
467
468
469
470
471
472
473}
474
475int ivtv_start_v4l2_encode_stream(struct ivtv_stream *s)
476{
477 u32 data[CX2341X_MBOX_MAX_DATA];
478 struct ivtv *itv = s->itv;
479 int captype = 0, subtype = 0;
480 int enable_passthrough = 0;
481
482 if (s->vdev.v4l2_dev == NULL)
483 return -EINVAL;
484
485 IVTV_DEBUG_INFO("Start encoder stream %s\n", s->name);
486
487 switch (s->type) {
488 case IVTV_ENC_STREAM_TYPE_MPG:
489 captype = 0;
490 subtype = 3;
491
492
493 if (itv->output_mode == OUT_PASSTHROUGH) {
494 ivtv_passthrough_mode(itv, 0);
495 enable_passthrough = 1;
496 }
497 itv->mpg_data_received = itv->vbi_data_inserted = 0;
498 itv->dualwatch_jiffies = jiffies;
499 itv->dualwatch_stereo_mode = v4l2_ctrl_g_ctrl(itv->cxhdl.audio_mode);
500 itv->search_pack_header = 0;
501 break;
502
503 case IVTV_ENC_STREAM_TYPE_YUV:
504 if (itv->output_mode == OUT_PASSTHROUGH) {
505 captype = 2;
506 subtype = 11;
507 break;
508 }
509 captype = 1;
510 subtype = 1;
511 break;
512 case IVTV_ENC_STREAM_TYPE_PCM:
513 captype = 1;
514 subtype = 2;
515 break;
516 case IVTV_ENC_STREAM_TYPE_VBI:
517 captype = 1;
518 subtype = 4;
519
520 itv->vbi.frame = 0;
521 itv->vbi.inserted_frame = 0;
522 memset(itv->vbi.sliced_mpeg_size,
523 0, sizeof(itv->vbi.sliced_mpeg_size));
524 break;
525 default:
526 return -EINVAL;
527 }
528 s->subtype = subtype;
529 s->buffers_stolen = 0;
530
531
532 clear_bit(IVTV_F_S_STREAMOFF, &s->s_flags);
533
534 if (atomic_read(&itv->capturing) == 0) {
535 int digitizer;
536
537
538
539
540
541
542
543 ivtv_vapi(itv, CX2341X_ENC_SET_DMA_BLOCK_SIZE, 2, 1, 1);
544
545
546 ivtv_vapi(itv, CX2341X_ENC_SET_VERT_CROP_LINE, 1, 0);
547
548
549
550
551
552
553 ivtv_vapi(itv, CX2341X_ENC_MISC, 2, 3, !itv->has_cx23415);
554 ivtv_vapi(itv, CX2341X_ENC_MISC, 2, 8, 0);
555 ivtv_vapi(itv, CX2341X_ENC_MISC, 2, 4, 1);
556 ivtv_vapi(itv, CX2341X_ENC_MISC, 1, 12);
557
558
559 ivtv_vapi(itv, CX2341X_ENC_SET_PLACEHOLDER, 12,
560 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
561
562 if (itv->card->hw_all & (IVTV_HW_SAA7115 | IVTV_HW_SAA717X))
563 digitizer = 0xF1;
564 else if (itv->card->hw_all & IVTV_HW_SAA7114)
565 digitizer = 0xEF;
566 else
567 digitizer = 0x140;
568
569 ivtv_vapi(itv, CX2341X_ENC_SET_NUM_VSYNC_LINES, 2, digitizer, digitizer);
570
571
572 if (itv->v4l2_cap & V4L2_CAP_VBI_CAPTURE) {
573 ivtv_vbi_setup(itv);
574 }
575
576
577 ivtv_vapi_result(itv, data, CX2341X_ENC_SET_PGM_INDEX_INFO, 2, 7, 400);
578 itv->pgm_info_offset = data[0];
579 itv->pgm_info_num = data[1];
580 itv->pgm_info_write_idx = 0;
581 itv->pgm_info_read_idx = 0;
582
583 IVTV_DEBUG_INFO("PGM Index at 0x%08x with %d elements\n",
584 itv->pgm_info_offset, itv->pgm_info_num);
585
586
587 cx2341x_handler_setup(&itv->cxhdl);
588
589
590 if (test_bit(IVTV_F_I_RADIO_USER, &itv->i_flags))
591 ivtv_vapi(itv, CX2341X_ENC_MUTE_VIDEO, 1,
592 1 | (v4l2_ctrl_g_ctrl(itv->cxhdl.video_mute_yuv) << 8));
593 }
594
595
596 if (itv->has_cx23415 && !test_and_set_bit(IVTV_F_I_DIG_RST, &itv->i_flags)) {
597
598 ivtv_vapi(itv, CX2341X_ENC_SET_EVENT_NOTIFICATION, 4, 0, 1, IVTV_IRQ_ENC_VIM_RST, -1);
599 ivtv_clear_irq_mask(itv, IVTV_IRQ_ENC_VIM_RST);
600 }
601
602 if (atomic_read(&itv->capturing) == 0) {
603
604 ivtv_set_irq_mask(itv, IVTV_IRQ_MASK_CAPTURE);
605
606 clear_bit(IVTV_F_I_EOS, &itv->i_flags);
607
608 cx2341x_handler_set_busy(&itv->cxhdl, 1);
609
610
611
612 v4l2_subdev_call(itv->sd_audio, audio, s_stream, 1);
613
614 v4l2_subdev_call(itv->sd_video, video, s_stream, 0);
615 ivtv_msleep_timeout(300, 0);
616 ivtv_vapi(itv, CX2341X_ENC_INITIALIZE_INPUT, 0);
617 v4l2_subdev_call(itv->sd_video, video, s_stream, 1);
618 }
619
620
621 if (ivtv_vapi(itv, CX2341X_ENC_START_CAPTURE, 2, captype, subtype))
622 {
623 IVTV_DEBUG_WARN( "Error starting capture!\n");
624 return -EINVAL;
625 }
626
627
628 if (enable_passthrough) {
629 ivtv_passthrough_mode(itv, 1);
630 }
631
632 if (s->type == IVTV_ENC_STREAM_TYPE_VBI)
633 ivtv_clear_irq_mask(itv, IVTV_IRQ_ENC_VBI_CAP);
634 else
635 ivtv_clear_irq_mask(itv, IVTV_IRQ_MASK_CAPTURE);
636
637
638 atomic_inc(&itv->capturing);
639 return 0;
640}
641EXPORT_SYMBOL(ivtv_start_v4l2_encode_stream);
642
643static int ivtv_setup_v4l2_decode_stream(struct ivtv_stream *s)
644{
645 u32 data[CX2341X_MBOX_MAX_DATA];
646 struct ivtv *itv = s->itv;
647 int datatype;
648 u16 width;
649 u16 height;
650
651 if (s->vdev.v4l2_dev == NULL)
652 return -EINVAL;
653
654 IVTV_DEBUG_INFO("Setting some initial decoder settings\n");
655
656 width = itv->cxhdl.width;
657 height = itv->cxhdl.height;
658
659
660 ivtv_vapi(itv, CX2341X_DEC_SET_AUDIO_MODE, 2, itv->audio_bilingual_mode, itv->audio_stereo_mode);
661
662
663 ivtv_vapi(itv, CX2341X_DEC_SET_DISPLAY_BUFFERS, 1, 0);
664
665
666 ivtv_vapi(itv, CX2341X_DEC_SET_PREBUFFERING, 1, 1);
667
668
669 ivtv_vapi_result(itv, data, CX2341X_DEC_EXTRACT_VBI, 1, 1);
670 itv->vbi.dec_start = data[0];
671
672 IVTV_DEBUG_INFO("Decoder VBI RE-Insert start 0x%08x size 0x%08x\n",
673 itv->vbi.dec_start, data[1]);
674
675
676
677
678
679 switch (s->type) {
680 case IVTV_DEC_STREAM_TYPE_YUV:
681 if (itv->output_mode == OUT_PASSTHROUGH) {
682 datatype = 1;
683 } else {
684
685 datatype = 2;
686 width = 720;
687 height = itv->is_out_50hz ? 576 : 480;
688 }
689 IVTV_DEBUG_INFO("Setup DEC YUV Stream data[0] = %d\n", datatype);
690 break;
691 case IVTV_DEC_STREAM_TYPE_MPG:
692 default:
693 datatype = 0;
694 break;
695 }
696 if (ivtv_vapi(itv, CX2341X_DEC_SET_DECODER_SOURCE, 4, datatype,
697 width, height, itv->cxhdl.audio_properties)) {
698 IVTV_DEBUG_WARN("Couldn't initialize decoder source\n");
699 }
700
701
702 ivtv_msleep_timeout(10, 0);
703
704
705 return ivtv_firmware_check(itv, "ivtv_setup_v4l2_decode_stream");
706}
707
708int ivtv_start_v4l2_decode_stream(struct ivtv_stream *s, int gop_offset)
709{
710 struct ivtv *itv = s->itv;
711 int rc;
712
713 if (s->vdev.v4l2_dev == NULL)
714 return -EINVAL;
715
716 if (test_and_set_bit(IVTV_F_S_STREAMING, &s->s_flags))
717 return 0;
718
719 IVTV_DEBUG_INFO("Starting decode stream %s (gop_offset %d)\n", s->name, gop_offset);
720
721 rc = ivtv_setup_v4l2_decode_stream(s);
722 if (rc < 0) {
723 clear_bit(IVTV_F_S_STREAMING, &s->s_flags);
724 return rc;
725 }
726
727
728 ivtv_vapi(itv, CX2341X_DEC_SET_DMA_BLOCK_SIZE, 1, 65536);
729
730
731 clear_bit(IVTV_F_S_STREAMOFF, &s->s_flags);
732
733
734 writel(0, &itv->dec_mbox.mbox[IVTV_MBOX_DMA_END].data[0]);
735 writel(0, &itv->dec_mbox.mbox[IVTV_MBOX_DMA_END].data[1]);
736 writel(0, &itv->dec_mbox.mbox[IVTV_MBOX_DMA_END].data[2]);
737 writel(0, &itv->dec_mbox.mbox[IVTV_MBOX_DMA_END].data[3]);
738 writel(0, &itv->dec_mbox.mbox[IVTV_MBOX_DMA].data[0]);
739 writel(0, &itv->dec_mbox.mbox[IVTV_MBOX_DMA].data[1]);
740 writel(0, &itv->dec_mbox.mbox[IVTV_MBOX_DMA].data[2]);
741 writel(0, &itv->dec_mbox.mbox[IVTV_MBOX_DMA].data[3]);
742
743
744 ivtv_vapi(itv, CX2341X_DEC_SET_EVENT_NOTIFICATION, 4, 0, 1, IVTV_IRQ_DEC_AUD_MODE_CHG, -1);
745
746
747 ivtv_vapi(itv, CX2341X_DEC_START_PLAYBACK, 2, gop_offset, 0);
748
749
750 ivtv_msleep_timeout(10, 0);
751
752
753 ivtv_clear_irq_mask(itv, IVTV_IRQ_MASK_DECODE);
754 IVTV_DEBUG_IRQ("IRQ Mask is now: 0x%08x\n", itv->irqmask);
755
756
757 atomic_inc(&itv->decoding);
758 return 0;
759}
760
761void ivtv_stop_all_captures(struct ivtv *itv)
762{
763 int i;
764
765 for (i = IVTV_MAX_STREAMS - 1; i >= 0; i--) {
766 struct ivtv_stream *s = &itv->streams[i];
767
768 if (s->vdev.v4l2_dev == NULL)
769 continue;
770 if (test_bit(IVTV_F_S_STREAMING, &s->s_flags)) {
771 ivtv_stop_v4l2_encode_stream(s, 0);
772 }
773 }
774}
775
776int ivtv_stop_v4l2_encode_stream(struct ivtv_stream *s, int gop_end)
777{
778 struct ivtv *itv = s->itv;
779 DECLARE_WAITQUEUE(wait, current);
780 int cap_type;
781 int stopmode;
782
783 if (s->vdev.v4l2_dev == NULL)
784 return -EINVAL;
785
786
787
788
789 IVTV_DEBUG_INFO("Stop Capture\n");
790
791 if (s->type == IVTV_DEC_STREAM_TYPE_VOUT)
792 return 0;
793 if (atomic_read(&itv->capturing) == 0)
794 return 0;
795
796 switch (s->type) {
797 case IVTV_ENC_STREAM_TYPE_YUV:
798 cap_type = 1;
799 break;
800 case IVTV_ENC_STREAM_TYPE_PCM:
801 cap_type = 1;
802 break;
803 case IVTV_ENC_STREAM_TYPE_VBI:
804 cap_type = 1;
805 break;
806 case IVTV_ENC_STREAM_TYPE_MPG:
807 default:
808 cap_type = 0;
809 break;
810 }
811
812
813 if (s->type == IVTV_ENC_STREAM_TYPE_MPG && gop_end) {
814 stopmode = 0;
815 } else {
816 stopmode = 1;
817 }
818
819
820
821 ivtv_vapi(itv, CX2341X_ENC_STOP_CAPTURE, 3, stopmode, cap_type, s->subtype);
822
823 if (!test_bit(IVTV_F_S_PASSTHROUGH, &s->s_flags)) {
824 if (s->type == IVTV_ENC_STREAM_TYPE_MPG && gop_end) {
825
826 unsigned long duration;
827 unsigned long then = jiffies;
828
829 add_wait_queue(&itv->eos_waitq, &wait);
830
831 set_current_state(TASK_INTERRUPTIBLE);
832
833
834 while (!test_bit(IVTV_F_I_EOS, &itv->i_flags) &&
835 time_before(jiffies,
836 then + msecs_to_jiffies(2000))) {
837 schedule_timeout(msecs_to_jiffies(10));
838 }
839
840
841
842
843
844
845
846
847 duration = ((1000 + HZ / 2) / HZ) * (jiffies - then);
848
849 if (!test_bit(IVTV_F_I_EOS, &itv->i_flags)) {
850 IVTV_DEBUG_WARN("%s: EOS interrupt not received! stopping anyway.\n", s->name);
851 IVTV_DEBUG_WARN("%s: waited %lu ms.\n", s->name, duration);
852 } else {
853 IVTV_DEBUG_INFO("%s: EOS took %lu ms to occur.\n", s->name, duration);
854 }
855 set_current_state(TASK_RUNNING);
856 remove_wait_queue(&itv->eos_waitq, &wait);
857 set_bit(IVTV_F_S_STREAMOFF, &s->s_flags);
858 }
859
860
861 ivtv_msleep_timeout(100, 0);
862 }
863
864 atomic_dec(&itv->capturing);
865
866
867 clear_bit(IVTV_F_S_STREAMING, &s->s_flags);
868
869 if (s->type == IVTV_ENC_STREAM_TYPE_VBI)
870 ivtv_set_irq_mask(itv, IVTV_IRQ_ENC_VBI_CAP);
871
872 if (atomic_read(&itv->capturing) > 0) {
873 return 0;
874 }
875
876 cx2341x_handler_set_busy(&itv->cxhdl, 0);
877
878
879 ivtv_set_irq_mask(itv, IVTV_IRQ_MASK_CAPTURE);
880 del_timer(&itv->dma_timer);
881
882
883 if (test_and_clear_bit(IVTV_F_I_DIG_RST, &itv->i_flags)) {
884
885
886 ivtv_vapi(itv, CX2341X_ENC_SET_EVENT_NOTIFICATION, 4, 0, 0, IVTV_IRQ_ENC_VIM_RST, -1);
887 ivtv_set_irq_mask(itv, IVTV_IRQ_ENC_VIM_RST);
888 }
889
890
891
892 ivtv_vapi(itv, CX2341X_ENC_STOP_CAPTURE, 3, 1, 2, 7);
893
894 wake_up(&s->waitq);
895
896 return 0;
897}
898EXPORT_SYMBOL(ivtv_stop_v4l2_encode_stream);
899
900int ivtv_stop_v4l2_decode_stream(struct ivtv_stream *s, int flags, u64 pts)
901{
902 static const struct v4l2_event ev = {
903 .type = V4L2_EVENT_EOS,
904 };
905 struct ivtv *itv = s->itv;
906
907 if (s->vdev.v4l2_dev == NULL)
908 return -EINVAL;
909
910 if (s->type != IVTV_DEC_STREAM_TYPE_YUV && s->type != IVTV_DEC_STREAM_TYPE_MPG)
911 return -EINVAL;
912
913 if (!test_bit(IVTV_F_S_STREAMING, &s->s_flags))
914 return 0;
915
916 IVTV_DEBUG_INFO("Stop Decode at %llu, flags: %x\n", (unsigned long long)pts, flags);
917
918
919 if (!(flags & V4L2_DEC_CMD_STOP_IMMEDIATELY) || pts) {
920 u32 tmp = 0;
921
922
923 if (pts) {
924 ivtv_vapi(itv, CX2341X_DEC_STOP_PLAYBACK, 3,
925 0, (u32)(pts & 0xffffffff), (u32)(pts >> 32));
926 }
927 while (1) {
928 u32 data[CX2341X_MBOX_MAX_DATA];
929 ivtv_vapi_result(itv, data, CX2341X_DEC_GET_XFER_INFO, 0);
930 if (s->q_full.buffers + s->q_dma.buffers == 0) {
931 if (tmp == data[3])
932 break;
933 tmp = data[3];
934 }
935 if (ivtv_msleep_timeout(100, 1))
936 break;
937 }
938 }
939 ivtv_vapi(itv, CX2341X_DEC_STOP_PLAYBACK, 3, flags & V4L2_DEC_CMD_STOP_TO_BLACK, 0, 0);
940
941
942 ivtv_vapi(itv, CX2341X_DEC_SET_EVENT_NOTIFICATION, 4, 0, 0, IVTV_IRQ_DEC_AUD_MODE_CHG, -1);
943
944 ivtv_set_irq_mask(itv, IVTV_IRQ_MASK_DECODE);
945 del_timer(&itv->dma_timer);
946
947 clear_bit(IVTV_F_S_NEEDS_DATA, &s->s_flags);
948 clear_bit(IVTV_F_S_STREAMING, &s->s_flags);
949 ivtv_flush_queues(s);
950
951
952 ivtv_msleep_timeout(40, 0);
953
954
955 atomic_dec(&itv->decoding);
956
957 set_bit(IVTV_F_I_EV_DEC_STOPPED, &itv->i_flags);
958 wake_up(&itv->event_waitq);
959 v4l2_event_queue(&s->vdev, &ev);
960
961
962 wake_up(&s->waitq);
963
964 return 0;
965}
966
967int ivtv_passthrough_mode(struct ivtv *itv, int enable)
968{
969 struct ivtv_stream *yuv_stream = &itv->streams[IVTV_ENC_STREAM_TYPE_YUV];
970 struct ivtv_stream *dec_stream = &itv->streams[IVTV_DEC_STREAM_TYPE_YUV];
971
972 if (yuv_stream->vdev.v4l2_dev == NULL || dec_stream->vdev.v4l2_dev == NULL)
973 return -EINVAL;
974
975 IVTV_DEBUG_INFO("ivtv ioctl: Select passthrough mode\n");
976
977
978
979 if (enable) {
980 if (itv->output_mode == OUT_PASSTHROUGH) {
981 return 0;
982 }
983 if (ivtv_set_output_mode(itv, OUT_PASSTHROUGH) != OUT_PASSTHROUGH)
984 return -EBUSY;
985
986
987 set_bit(IVTV_F_S_PASSTHROUGH, &dec_stream->s_flags);
988 set_bit(IVTV_F_S_STREAMING, &dec_stream->s_flags);
989
990
991 ivtv_setup_v4l2_decode_stream(dec_stream);
992
993
994 ivtv_vapi(itv, CX2341X_DEC_START_PLAYBACK, 2, 0, 1);
995 atomic_inc(&itv->decoding);
996
997
998 if (atomic_read(&itv->capturing) == 0) {
999 cx2341x_handler_setup(&itv->cxhdl);
1000 cx2341x_handler_set_busy(&itv->cxhdl, 1);
1001 }
1002
1003
1004 ivtv_vapi(itv, CX2341X_ENC_START_CAPTURE, 2, 2, 11);
1005 atomic_inc(&itv->capturing);
1006 return 0;
1007 }
1008
1009 if (itv->output_mode != OUT_PASSTHROUGH)
1010 return 0;
1011
1012
1013 ivtv_vapi(itv, CX2341X_ENC_STOP_CAPTURE, 3, 1, 2, 11);
1014 ivtv_vapi(itv, CX2341X_DEC_STOP_PLAYBACK, 3, 1, 0, 0);
1015
1016 atomic_dec(&itv->capturing);
1017 atomic_dec(&itv->decoding);
1018 clear_bit(IVTV_F_S_PASSTHROUGH, &dec_stream->s_flags);
1019 clear_bit(IVTV_F_S_STREAMING, &dec_stream->s_flags);
1020 itv->output_mode = OUT_NONE;
1021 if (atomic_read(&itv->capturing) == 0)
1022 cx2341x_handler_set_busy(&itv->cxhdl, 0);
1023
1024 return 0;
1025}
1026