blob: fae151a31e85a76158692c053ccf9ecbdf0c8ba0 [file] [log] [blame]
Hans Verkuil1a0adaf2007-04-27 12:31:25 -03001/*
2 init/start/stop/exit stream functions
3 Copyright (C) 2003-2004 Kevin Thayer <nufan_wfk at yahoo.com>
4 Copyright (C) 2004 Chris Kennedy <c@groovy.org>
5 Copyright (C) 2005-2007 Hans Verkuil <hverkuil@xs4all.nl>
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21
22/* License: GPL
23 * Author: Kevin Thayer <nufan_wfk at yahoo dot com>
24 *
25 * This file will hold API related functions, both internal (firmware api)
26 * and external (v4l2, etc)
27 *
28 * -----
29 * MPG600/MPG160 support by T.Adachi <tadachi@tadachi-net.com>
30 * and Takeru KOMORIYA<komoriya@paken.org>
31 *
32 * AVerMedia M179 GPIO info by Chris Pinkham <cpinkham@bc2va.org>
33 * using information provided by Jiun-Kuei Jung @ AVerMedia.
34 */
35
36#include "ivtv-driver.h"
37#include "ivtv-fileops.h"
Hans Verkuil1a0adaf2007-04-27 12:31:25 -030038#include "ivtv-queue.h"
39#include "ivtv-mailbox.h"
Hans Verkuil1a0adaf2007-04-27 12:31:25 -030040#include "ivtv-ioctl.h"
Hans Verkuil612570f2007-08-23 05:42:59 -030041#include "ivtv-yuv.h"
Hans Verkuil1a0adaf2007-04-27 12:31:25 -030042#include "ivtv-cards.h"
Hans Verkuil612570f2007-08-23 05:42:59 -030043#include "ivtv-streams.h"
Hans Verkuil1a0adaf2007-04-27 12:31:25 -030044
45static struct file_operations ivtv_v4l2_enc_fops = {
46 .owner = THIS_MODULE,
47 .read = ivtv_v4l2_read,
48 .write = ivtv_v4l2_write,
49 .open = ivtv_v4l2_open,
50 .ioctl = ivtv_v4l2_ioctl,
51 .release = ivtv_v4l2_close,
52 .poll = ivtv_v4l2_enc_poll,
53};
54
55static struct file_operations ivtv_v4l2_dec_fops = {
56 .owner = THIS_MODULE,
57 .read = ivtv_v4l2_read,
58 .write = ivtv_v4l2_write,
59 .open = ivtv_v4l2_open,
60 .ioctl = ivtv_v4l2_ioctl,
61 .release = ivtv_v4l2_close,
62 .poll = ivtv_v4l2_dec_poll,
63};
64
Hans Verkuil31ec1352007-03-03 08:50:42 -030065static struct {
Hans Verkuil1a0adaf2007-04-27 12:31:25 -030066 const char *name;
67 int vfl_type;
68 int minor_offset;
69 int dma, pio;
70 enum v4l2_buf_type buf_type;
71 struct file_operations *fops;
72} ivtv_stream_info[] = {
73 { /* IVTV_ENC_STREAM_TYPE_MPG */
Hans Verkuil7c03a442007-08-19 18:59:42 -030074 "encoder MPG",
Hans Verkuil1a0adaf2007-04-27 12:31:25 -030075 VFL_TYPE_GRABBER, 0,
76 PCI_DMA_FROMDEVICE, 0, V4L2_BUF_TYPE_VIDEO_CAPTURE,
77 &ivtv_v4l2_enc_fops
78 },
79 { /* IVTV_ENC_STREAM_TYPE_YUV */
80 "encoder YUV",
81 VFL_TYPE_GRABBER, IVTV_V4L2_ENC_YUV_OFFSET,
82 PCI_DMA_FROMDEVICE, 0, V4L2_BUF_TYPE_VIDEO_CAPTURE,
83 &ivtv_v4l2_enc_fops
84 },
85 { /* IVTV_ENC_STREAM_TYPE_VBI */
86 "encoder VBI",
87 VFL_TYPE_VBI, 0,
88 PCI_DMA_FROMDEVICE, 0, V4L2_BUF_TYPE_VBI_CAPTURE,
89 &ivtv_v4l2_enc_fops
90 },
91 { /* IVTV_ENC_STREAM_TYPE_PCM */
Hans Verkuil7c03a442007-08-19 18:59:42 -030092 "encoder PCM",
Hans Verkuil1a0adaf2007-04-27 12:31:25 -030093 VFL_TYPE_GRABBER, IVTV_V4L2_ENC_PCM_OFFSET,
94 PCI_DMA_FROMDEVICE, 0, V4L2_BUF_TYPE_PRIVATE,
95 &ivtv_v4l2_enc_fops
96 },
97 { /* IVTV_ENC_STREAM_TYPE_RAD */
98 "encoder radio",
99 VFL_TYPE_RADIO, 0,
100 PCI_DMA_NONE, 1, V4L2_BUF_TYPE_PRIVATE,
101 &ivtv_v4l2_enc_fops
102 },
103 { /* IVTV_DEC_STREAM_TYPE_MPG */
Hans Verkuil7c03a442007-08-19 18:59:42 -0300104 "decoder MPG",
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300105 VFL_TYPE_GRABBER, IVTV_V4L2_DEC_MPG_OFFSET,
106 PCI_DMA_TODEVICE, 0, V4L2_BUF_TYPE_VIDEO_OUTPUT,
107 &ivtv_v4l2_dec_fops
108 },
109 { /* IVTV_DEC_STREAM_TYPE_VBI */
110 "decoder VBI",
111 VFL_TYPE_VBI, IVTV_V4L2_DEC_VBI_OFFSET,
112 PCI_DMA_NONE, 1, V4L2_BUF_TYPE_VBI_CAPTURE,
113 &ivtv_v4l2_enc_fops
114 },
115 { /* IVTV_DEC_STREAM_TYPE_VOUT */
116 "decoder VOUT",
117 VFL_TYPE_VBI, IVTV_V4L2_DEC_VOUT_OFFSET,
118 PCI_DMA_NONE, 1, V4L2_BUF_TYPE_VBI_OUTPUT,
119 &ivtv_v4l2_dec_fops
120 },
121 { /* IVTV_DEC_STREAM_TYPE_YUV */
122 "decoder YUV",
123 VFL_TYPE_GRABBER, IVTV_V4L2_DEC_YUV_OFFSET,
124 PCI_DMA_TODEVICE, 0, V4L2_BUF_TYPE_VIDEO_OUTPUT,
125 &ivtv_v4l2_dec_fops
126 }
127};
128
129static void ivtv_stream_init(struct ivtv *itv, int type)
130{
131 struct ivtv_stream *s = &itv->streams[type];
132 struct video_device *dev = s->v4l2dev;
133
134 /* we need to keep v4l2dev, so restore it afterwards */
135 memset(s, 0, sizeof(*s));
136 s->v4l2dev = dev;
137
138 /* initialize ivtv_stream fields */
139 s->itv = itv;
140 s->type = type;
141 s->name = ivtv_stream_info[type].name;
142
143 if (ivtv_stream_info[type].pio)
144 s->dma = PCI_DMA_NONE;
145 else
146 s->dma = ivtv_stream_info[type].dma;
147 s->buf_size = itv->stream_buf_size[type];
148 if (s->buf_size)
Hans Verkuil313e91e2007-08-19 05:32:33 -0300149 s->buffers = (itv->options.kilobytes[type] * 1024 + s->buf_size - 1) / s->buf_size;
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300150 spin_lock_init(&s->qlock);
151 init_waitqueue_head(&s->waitq);
152 s->id = -1;
Hans Verkuil37093b12007-07-28 19:45:50 -0300153 s->sg_handle = IVTV_DMA_UNMAPPED;
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300154 ivtv_queue_init(&s->q_free);
155 ivtv_queue_init(&s->q_full);
156 ivtv_queue_init(&s->q_dma);
157 ivtv_queue_init(&s->q_predma);
158 ivtv_queue_init(&s->q_io);
159}
160
161static int ivtv_reg_dev(struct ivtv *itv, int type)
162{
163 struct ivtv_stream *s = &itv->streams[type];
164 int vfl_type = ivtv_stream_info[type].vfl_type;
165 int minor_offset = ivtv_stream_info[type].minor_offset;
166 int minor;
167
168 /* These four fields are always initialized. If v4l2dev == NULL, then
169 this stream is not in use. In that case no other fields but these
170 four can be used. */
171 s->v4l2dev = NULL;
172 s->itv = itv;
173 s->type = type;
174 s->name = ivtv_stream_info[type].name;
175
176 /* Check whether the radio is supported */
177 if (type == IVTV_ENC_STREAM_TYPE_RAD && !(itv->v4l2_cap & V4L2_CAP_RADIO))
178 return 0;
179 if (type >= IVTV_DEC_STREAM_TYPE_MPG && !(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
180 return 0;
181
182 if (minor_offset >= 0)
183 /* card number + user defined offset + device offset */
184 minor = itv->num + ivtv_first_minor + minor_offset;
185 else
186 minor = -1;
187
188 /* User explicitly selected 0 buffers for these streams, so don't
189 create them. */
190 if (minor >= 0 && ivtv_stream_info[type].dma != PCI_DMA_NONE &&
Hans Verkuil313e91e2007-08-19 05:32:33 -0300191 itv->options.kilobytes[type] == 0) {
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300192 IVTV_INFO("Disabled %s device\n", ivtv_stream_info[type].name);
193 return 0;
194 }
195
196 ivtv_stream_init(itv, type);
197
198 /* allocate and initialize the v4l2 video device structure */
199 s->v4l2dev = video_device_alloc();
200 if (s->v4l2dev == NULL) {
201 IVTV_ERR("Couldn't allocate v4l2 video_device for %s\n", s->name);
202 return -ENOMEM;
203 }
204
205 s->v4l2dev->type = VID_TYPE_CAPTURE | VID_TYPE_TUNER | VID_TYPE_TELETEXT |
206 VID_TYPE_CLIPPING | VID_TYPE_SCALES | VID_TYPE_MPEG_ENCODER;
207 if (itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT) {
208 s->v4l2dev->type |= VID_TYPE_MPEG_DECODER;
209 }
210 snprintf(s->v4l2dev->name, sizeof(s->v4l2dev->name), "ivtv%d %s",
211 itv->num, s->name);
212
213 s->v4l2dev->minor = minor;
214 s->v4l2dev->dev = &itv->dev->dev;
215 s->v4l2dev->fops = ivtv_stream_info[type].fops;
216 s->v4l2dev->release = video_device_release;
217
218 if (minor >= 0) {
219 /* Register device. First try the desired minor, then any free one. */
220 if (video_register_device(s->v4l2dev, vfl_type, minor) &&
221 video_register_device(s->v4l2dev, vfl_type, -1)) {
222 IVTV_ERR("Couldn't register v4l2 device for %s minor %d\n",
223 s->name, minor);
224 video_device_release(s->v4l2dev);
225 s->v4l2dev = NULL;
226 return -ENOMEM;
227 }
228 }
229 else {
230 /* Don't register a 'hidden' stream (OSD) */
231 IVTV_INFO("Created framebuffer stream for %s\n", s->name);
232 return 0;
233 }
234
235 switch (vfl_type) {
236 case VFL_TYPE_GRABBER:
Hans Verkuil313e91e2007-08-19 05:32:33 -0300237 IVTV_INFO("Registered device video%d for %s (%d kB)\n",
238 s->v4l2dev->minor, s->name, itv->options.kilobytes[type]);
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300239 break;
240 case VFL_TYPE_RADIO:
241 IVTV_INFO("Registered device radio%d for %s\n",
242 s->v4l2dev->minor - MINOR_VFL_TYPE_RADIO_MIN, s->name);
243 break;
244 case VFL_TYPE_VBI:
Hans Verkuil313e91e2007-08-19 05:32:33 -0300245 if (itv->options.kilobytes[type])
246 IVTV_INFO("Registered device vbi%d for %s (%d kB)\n",
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300247 s->v4l2dev->minor - MINOR_VFL_TYPE_VBI_MIN,
Hans Verkuil313e91e2007-08-19 05:32:33 -0300248 s->name, itv->options.kilobytes[type]);
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300249 else
250 IVTV_INFO("Registered device vbi%d for %s\n",
251 s->v4l2dev->minor - MINOR_VFL_TYPE_VBI_MIN, s->name);
252 break;
253 }
254 return 0;
255}
256
257/* Initialize v4l2 variables and register v4l2 devices */
258int ivtv_streams_setup(struct ivtv *itv)
259{
260 int type;
261
262 /* Setup V4L2 Devices */
263 for (type = 0; type < IVTV_MAX_STREAMS; type++) {
264 /* Register Device */
265 if (ivtv_reg_dev(itv, type))
266 break;
267
268 if (itv->streams[type].v4l2dev == NULL)
269 continue;
270
271 /* Allocate Stream */
272 if (ivtv_stream_alloc(&itv->streams[type]))
273 break;
274 }
275 if (type == IVTV_MAX_STREAMS) {
276 return 0;
277 }
278
279 /* One or more streams could not be initialized. Clean 'em all up. */
280 ivtv_streams_cleanup(itv);
281 return -ENOMEM;
282}
283
284/* Unregister v4l2 devices */
285void ivtv_streams_cleanup(struct ivtv *itv)
286{
287 int type;
288
289 /* Teardown all streams */
290 for (type = 0; type < IVTV_MAX_STREAMS; type++) {
291 struct video_device *vdev = itv->streams[type].v4l2dev;
292
293 itv->streams[type].v4l2dev = NULL;
294 if (vdev == NULL)
295 continue;
296
297 ivtv_stream_free(&itv->streams[type]);
298 /* Free Device */
299 if (vdev->minor == -1) /* 'Hidden' never registered stream (OSD) */
300 video_device_release(vdev);
301 else /* All others, just unregister. */
302 video_unregister_device(vdev);
303 }
304}
305
306static void ivtv_vbi_setup(struct ivtv *itv)
307{
308 int raw = itv->vbi.sliced_in->service_set == 0;
309 u32 data[CX2341X_MBOX_MAX_DATA];
310 int lines;
311 int i;
312
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300313 /* Reset VBI */
314 ivtv_vapi(itv, CX2341X_ENC_SET_VBI_LINE, 5, 0xffff , 0, 0, 0, 0);
315
316 if (itv->is_60hz) {
317 itv->vbi.count = 12;
318 itv->vbi.start[0] = 10;
319 itv->vbi.start[1] = 273;
320 } else { /* PAL/SECAM */
321 itv->vbi.count = 18;
322 itv->vbi.start[0] = 6;
323 itv->vbi.start[1] = 318;
324 }
325
326 /* setup VBI registers */
327 itv->video_dec_func(itv, VIDIOC_S_FMT, &itv->vbi.in);
328
329 /* determine number of lines and total number of VBI bytes.
330 A raw line takes 1443 bytes: 2 * 720 + 4 byte frame header - 1
331 The '- 1' byte is probably an unused U or V byte. Or something...
332 A sliced line takes 51 bytes: 4 byte frame header, 4 byte internal
333 header, 42 data bytes + checksum (to be confirmed) */
334 if (raw) {
335 lines = itv->vbi.count * 2;
336 } else {
337 lines = itv->is_60hz ? 24 : 38;
338 if (itv->is_60hz && (itv->hw_flags & IVTV_HW_CX25840))
339 lines += 2;
340 }
341
342 itv->vbi.enc_size = lines * (raw ? itv->vbi.raw_size : itv->vbi.sliced_size);
343
344 /* Note: sliced vs raw flag doesn't seem to have any effect
345 TODO: check mode (0x02) value with older ivtv versions. */
346 data[0] = raw | 0x02 | (0xbd << 8);
347
348 /* Every X number of frames a VBI interrupt arrives (frames as in 25 or 30 fps) */
349 data[1] = 1;
350 /* The VBI frames are stored in a ringbuffer with this size (with a VBI frame as unit) */
351 data[2] = raw ? 4 : 8;
352 /* The start/stop codes determine which VBI lines end up in the raw VBI data area.
353 The codes are from table 24 in the saa7115 datasheet. Each raw/sliced/video line
354 is framed with codes FF0000XX where XX is the SAV/EAV (Start/End of Active Video)
355 code. These values for raw VBI are obtained from a driver disassembly. The sliced
356 start/stop codes was deduced from this, but they do not appear in the driver.
357 Other code pairs that I found are: 0x250E6249/0x13545454 and 0x25256262/0x38137F54.
358 However, I have no idea what these values are for. */
359 if (itv->hw_flags & IVTV_HW_CX25840) {
360 /* Setup VBI for the cx25840 digitizer */
361 if (raw) {
362 data[3] = 0x20602060;
363 data[4] = 0x30703070;
364 } else {
365 data[3] = 0xB0F0B0F0;
366 data[4] = 0xA0E0A0E0;
367 }
368 /* Lines per frame */
369 data[5] = lines;
370 /* bytes per line */
371 data[6] = (raw ? itv->vbi.raw_size : itv->vbi.sliced_size);
372 } else {
373 /* Setup VBI for the saa7115 digitizer */
374 if (raw) {
375 data[3] = 0x25256262;
376 data[4] = 0x387F7F7F;
377 } else {
378 data[3] = 0xABABECEC;
379 data[4] = 0xB6F1F1F1;
380 }
381 /* Lines per frame */
382 data[5] = lines;
383 /* bytes per line */
384 data[6] = itv->vbi.enc_size / lines;
385 }
386
387 IVTV_DEBUG_INFO(
388 "Setup VBI API header 0x%08x pkts %d buffs %d ln %d sz %d\n",
389 data[0], data[1], data[2], data[5], data[6]);
390
391 ivtv_api(itv, CX2341X_ENC_SET_VBI_CONFIG, 7, data);
392
393 /* returns the VBI encoder memory area. */
394 itv->vbi.enc_start = data[2];
395 itv->vbi.fpi = data[0];
396 if (!itv->vbi.fpi)
397 itv->vbi.fpi = 1;
398
399 IVTV_DEBUG_INFO("Setup VBI start 0x%08x frames %d fpi %d lines 0x%08x\n",
400 itv->vbi.enc_start, data[1], itv->vbi.fpi, itv->digitizer);
401
402 /* select VBI lines.
403 Note that the sliced argument seems to have no effect. */
404 for (i = 2; i <= 24; i++) {
405 int valid;
406
407 if (itv->is_60hz) {
408 valid = i >= 10 && i < 22;
409 } else {
410 valid = i >= 6 && i < 24;
411 }
412 ivtv_vapi(itv, CX2341X_ENC_SET_VBI_LINE, 5, i - 1,
413 valid, 0 , 0, 0);
414 ivtv_vapi(itv, CX2341X_ENC_SET_VBI_LINE, 5, (i - 1) | 0x80000000,
415 valid, 0, 0, 0);
416 }
417
418 /* Remaining VBI questions:
419 - Is it possible to select particular VBI lines only for inclusion in the MPEG
420 stream? Currently you can only get the first X lines.
421 - Is mixed raw and sliced VBI possible?
422 - What's the meaning of the raw/sliced flag?
423 - What's the meaning of params 2, 3 & 4 of the Select VBI command? */
424}
425
426int ivtv_start_v4l2_encode_stream(struct ivtv_stream *s)
427{
428 u32 data[CX2341X_MBOX_MAX_DATA];
429 struct ivtv *itv = s->itv;
430 int captype = 0, subtype = 0;
431 int enable_passthrough = 0;
432
433 if (s->v4l2dev == NULL)
434 return -EINVAL;
435
436 IVTV_DEBUG_INFO("Start encoder stream %s\n", s->name);
437
438 switch (s->type) {
439 case IVTV_ENC_STREAM_TYPE_MPG:
440 captype = 0;
441 subtype = 3;
442
443 /* Stop Passthrough */
444 if (itv->output_mode == OUT_PASSTHROUGH) {
445 ivtv_passthrough_mode(itv, 0);
446 enable_passthrough = 1;
447 }
448 itv->mpg_data_received = itv->vbi_data_inserted = 0;
449 itv->dualwatch_jiffies = jiffies;
450 itv->dualwatch_stereo_mode = itv->params.audio_properties & 0x0300;
451 itv->search_pack_header = 0;
452 break;
453
454 case IVTV_ENC_STREAM_TYPE_YUV:
455 if (itv->output_mode == OUT_PASSTHROUGH) {
456 captype = 2;
457 subtype = 11; /* video+audio+decoder */
458 break;
459 }
460 captype = 1;
461 subtype = 1;
462 break;
463 case IVTV_ENC_STREAM_TYPE_PCM:
464 captype = 1;
465 subtype = 2;
466 break;
467 case IVTV_ENC_STREAM_TYPE_VBI:
468 captype = 1;
469 subtype = 4;
470
471 itv->vbi.frame = 0;
472 itv->vbi.inserted_frame = 0;
473 memset(itv->vbi.sliced_mpeg_size,
474 0, sizeof(itv->vbi.sliced_mpeg_size));
475 break;
476 default:
477 return -EINVAL;
478 }
479 s->subtype = subtype;
480 s->buffers_stolen = 0;
481
482 /* mute/unmute video */
483 ivtv_vapi(itv, CX2341X_ENC_MUTE_VIDEO, 1, test_bit(IVTV_F_I_RADIO_USER, &itv->i_flags) ? 1 : 0);
484
485 /* Clear Streamoff flags in case left from last capture */
486 clear_bit(IVTV_F_S_STREAMOFF, &s->s_flags);
487
488 if (atomic_read(&itv->capturing) == 0) {
489 /* Always use frame based mode. Experiments have demonstrated that byte
490 stream based mode results in dropped frames and corruption. Not often,
491 but occasionally. Many thanks go to Leonard Orb who spent a lot of
492 effort and time trying to trace the cause of the drop outs. */
493 /* 1 frame per DMA */
494 /*ivtv_vapi(itv, CX2341X_ENC_SET_DMA_BLOCK_SIZE, 2, 128, 0); */
495 ivtv_vapi(itv, CX2341X_ENC_SET_DMA_BLOCK_SIZE, 2, 1, 1);
496
497 /* Stuff from Windows, we don't know what it is */
498 ivtv_vapi(itv, CX2341X_ENC_SET_VERT_CROP_LINE, 1, 0);
499 /* According to the docs, this should be correct. However, this is
500 untested. I don't dare enable this without having tested it.
501 Only very few old cards actually have this hardware combination.
502 ivtv_vapi(itv, CX2341X_ENC_SET_VERT_CROP_LINE, 1,
503 ((itv->hw_flags & IVTV_HW_SAA7114) && itv->is_60hz) ? 10001 : 0);
504 */
505 ivtv_vapi(itv, CX2341X_ENC_MISC, 2, 3, !itv->has_cx23415);
506 ivtv_vapi(itv, CX2341X_ENC_MISC, 2, 8, 0);
507 ivtv_vapi(itv, CX2341X_ENC_MISC, 2, 4, 1);
508 ivtv_vapi(itv, CX2341X_ENC_MISC, 1, 12);
509
510 /* assign placeholder */
511 ivtv_vapi(itv, CX2341X_ENC_SET_PLACEHOLDER, 12,
512 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
513
514 ivtv_vapi(itv, CX2341X_ENC_SET_NUM_VSYNC_LINES, 2, itv->digitizer, itv->digitizer);
515
516 /* Setup VBI */
517 if (itv->v4l2_cap & V4L2_CAP_VBI_CAPTURE) {
518 ivtv_vbi_setup(itv);
519 }
520
521 /* assign program index info. Mask 7: select I/P/B, Num_req: 400 max */
522 ivtv_vapi_result(itv, data, CX2341X_ENC_SET_PGM_INDEX_INFO, 2, 7, 400);
523 itv->pgm_info_offset = data[0];
524 itv->pgm_info_num = data[1];
525 itv->pgm_info_write_idx = 0;
526 itv->pgm_info_read_idx = 0;
527
528 IVTV_DEBUG_INFO("PGM Index at 0x%08x with %d elements\n",
529 itv->pgm_info_offset, itv->pgm_info_num);
530
531 /* Setup API for Stream */
532 cx2341x_update(itv, ivtv_api_func, NULL, &itv->params);
533 }
534
535 /* Vsync Setup */
536 if (itv->has_cx23415 && !test_and_set_bit(IVTV_F_I_DIG_RST, &itv->i_flags)) {
537 /* event notification (on) */
538 ivtv_vapi(itv, CX2341X_ENC_SET_EVENT_NOTIFICATION, 4, 0, 1, IVTV_IRQ_ENC_VIM_RST, -1);
539 ivtv_clear_irq_mask(itv, IVTV_IRQ_ENC_VIM_RST);
540 }
541
542 if (atomic_read(&itv->capturing) == 0) {
543 /* Clear all Pending Interrupts */
544 ivtv_set_irq_mask(itv, IVTV_IRQ_MASK_CAPTURE);
545
546 clear_bit(IVTV_F_I_EOS, &itv->i_flags);
547
548 /* Initialize Digitizer for Capture */
Hans Verkuil3562c432007-08-18 11:46:05 -0300549 itv->video_dec_func(itv, VIDIOC_STREAMOFF, 0);
550 ivtv_msleep_timeout(300, 1);
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300551 ivtv_vapi(itv, CX2341X_ENC_INITIALIZE_INPUT, 0);
Hans Verkuil3562c432007-08-18 11:46:05 -0300552 itv->video_dec_func(itv, VIDIOC_STREAMON, 0);
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300553 }
554
555 /* begin_capture */
556 if (ivtv_vapi(itv, CX2341X_ENC_START_CAPTURE, 2, captype, subtype))
557 {
558 IVTV_DEBUG_WARN( "Error starting capture!\n");
559 return -EINVAL;
560 }
561
562 /* Start Passthrough */
563 if (enable_passthrough) {
564 ivtv_passthrough_mode(itv, 1);
565 }
566
567 if (s->type == IVTV_ENC_STREAM_TYPE_VBI)
568 ivtv_clear_irq_mask(itv, IVTV_IRQ_ENC_VBI_CAP);
569 else
570 ivtv_clear_irq_mask(itv, IVTV_IRQ_MASK_CAPTURE);
571
572 /* you're live! sit back and await interrupts :) */
573 atomic_inc(&itv->capturing);
574 return 0;
575}
576
577static int ivtv_setup_v4l2_decode_stream(struct ivtv_stream *s)
578{
579 u32 data[CX2341X_MBOX_MAX_DATA];
580 struct ivtv *itv = s->itv;
581 int datatype;
582
583 if (s->v4l2dev == NULL)
584 return -EINVAL;
585
586 IVTV_DEBUG_INFO("Setting some initial decoder settings\n");
587
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300588 /* set audio mode to left/stereo for dual/stereo mode. */
589 ivtv_vapi(itv, CX2341X_DEC_SET_AUDIO_MODE, 2, itv->audio_bilingual_mode, itv->audio_stereo_mode);
590
591 /* set number of internal decoder buffers */
592 ivtv_vapi(itv, CX2341X_DEC_SET_DISPLAY_BUFFERS, 1, 0);
593
594 /* prebuffering */
595 ivtv_vapi(itv, CX2341X_DEC_SET_PREBUFFERING, 1, 1);
596
597 /* extract from user packets */
598 ivtv_vapi_result(itv, data, CX2341X_DEC_EXTRACT_VBI, 1, 1);
599 itv->vbi.dec_start = data[0];
600
601 IVTV_DEBUG_INFO("Decoder VBI RE-Insert start 0x%08x size 0x%08x\n",
602 itv->vbi.dec_start, data[1]);
603
604 /* set decoder source settings */
605 /* Data type: 0 = mpeg from host,
606 1 = yuv from encoder,
607 2 = yuv_from_host */
608 switch (s->type) {
609 case IVTV_DEC_STREAM_TYPE_YUV:
610 datatype = itv->output_mode == OUT_PASSTHROUGH ? 1 : 2;
611 IVTV_DEBUG_INFO("Setup DEC YUV Stream data[0] = %d\n", datatype);
612 break;
613 case IVTV_DEC_STREAM_TYPE_MPG:
614 default:
615 datatype = 0;
616 break;
617 }
618 if (ivtv_vapi(itv, CX2341X_DEC_SET_DECODER_SOURCE, 4, datatype,
619 itv->params.width, itv->params.height, itv->params.audio_properties)) {
Hans Verkuil5a338c32007-07-22 09:39:56 -0300620 IVTV_DEBUG_WARN("Couldn't initialize decoder source\n");
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300621 }
622 return 0;
623}
624
625int ivtv_start_v4l2_decode_stream(struct ivtv_stream *s, int gop_offset)
626{
627 struct ivtv *itv = s->itv;
628
629 if (s->v4l2dev == NULL)
630 return -EINVAL;
631
632 if (test_and_set_bit(IVTV_F_S_STREAMING, &s->s_flags))
633 return 0; /* already started */
634
635 IVTV_DEBUG_INFO("Starting decode stream %s (gop_offset %d)\n", s->name, gop_offset);
636
637 /* Clear Streamoff */
638 if (s->type == IVTV_DEC_STREAM_TYPE_YUV) {
639 /* Initialize Decoder */
640 /* Reprogram Decoder YUV Buffers for YUV */
641 write_reg(yuv_offset[0] >> 4, 0x82c);
642 write_reg((yuv_offset[0] + IVTV_YUV_BUFFER_UV_OFFSET) >> 4, 0x830);
643 write_reg(yuv_offset[0] >> 4, 0x834);
644 write_reg((yuv_offset[0] + IVTV_YUV_BUFFER_UV_OFFSET) >> 4, 0x838);
645
646 write_reg_sync(0x00000000 | (0x0c << 16) | (0x0b << 8), 0x2d24);
647
648 write_reg_sync(0x00108080, 0x2898);
649 /* Enable YUV decoder output */
650 write_reg_sync(0x01, IVTV_REG_VDM);
651 }
652
653 ivtv_setup_v4l2_decode_stream(s);
654
655 /* set dma size to 65536 bytes */
656 ivtv_vapi(itv, CX2341X_DEC_SET_DMA_BLOCK_SIZE, 1, 65536);
657
658 clear_bit(IVTV_F_S_STREAMOFF, &s->s_flags);
659
660 /* Zero out decoder counters */
661 writel(0, &itv->dec_mbox.mbox[IVTV_MBOX_FIELD_DISPLAYED].data[0]);
662 writel(0, &itv->dec_mbox.mbox[IVTV_MBOX_FIELD_DISPLAYED].data[1]);
663 writel(0, &itv->dec_mbox.mbox[IVTV_MBOX_FIELD_DISPLAYED].data[2]);
664 writel(0, &itv->dec_mbox.mbox[IVTV_MBOX_FIELD_DISPLAYED].data[3]);
665 writel(0, &itv->dec_mbox.mbox[IVTV_MBOX_DMA].data[0]);
666 writel(0, &itv->dec_mbox.mbox[IVTV_MBOX_DMA].data[1]);
667 writel(0, &itv->dec_mbox.mbox[IVTV_MBOX_DMA].data[2]);
668 writel(0, &itv->dec_mbox.mbox[IVTV_MBOX_DMA].data[3]);
669
670 /* turn on notification of dual/stereo mode change */
671 ivtv_vapi(itv, CX2341X_DEC_SET_EVENT_NOTIFICATION, 4, 0, 1, IVTV_IRQ_DEC_AUD_MODE_CHG, -1);
672
673 /* start playback */
674 ivtv_vapi(itv, CX2341X_DEC_START_PLAYBACK, 2, gop_offset, 0);
675
676 /* Clear the following Interrupt mask bits for decoding */
677 ivtv_clear_irq_mask(itv, IVTV_IRQ_MASK_DECODE);
678 IVTV_DEBUG_IRQ("IRQ Mask is now: 0x%08x\n", itv->irqmask);
679
680 /* you're live! sit back and await interrupts :) */
681 atomic_inc(&itv->decoding);
682 return 0;
683}
684
685void ivtv_stop_all_captures(struct ivtv *itv)
686{
687 int i;
688
689 for (i = IVTV_MAX_STREAMS - 1; i >= 0; i--) {
690 struct ivtv_stream *s = &itv->streams[i];
691
692 if (s->v4l2dev == NULL)
693 continue;
694 if (test_bit(IVTV_F_S_STREAMING, &s->s_flags)) {
695 ivtv_stop_v4l2_encode_stream(s, 0);
696 }
697 }
698}
699
700int ivtv_stop_v4l2_encode_stream(struct ivtv_stream *s, int gop_end)
701{
702 struct ivtv *itv = s->itv;
703 DECLARE_WAITQUEUE(wait, current);
704 int cap_type;
705 unsigned long then;
706 int stopmode;
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300707
708 if (s->v4l2dev == NULL)
709 return -EINVAL;
710
711 /* This function assumes that you are allowed to stop the capture
712 and that we are actually capturing */
713
714 IVTV_DEBUG_INFO("Stop Capture\n");
715
716 if (s->type == IVTV_DEC_STREAM_TYPE_VOUT)
717 return 0;
718 if (atomic_read(&itv->capturing) == 0)
719 return 0;
720
721 switch (s->type) {
722 case IVTV_ENC_STREAM_TYPE_YUV:
723 cap_type = 1;
724 break;
725 case IVTV_ENC_STREAM_TYPE_PCM:
726 cap_type = 1;
727 break;
728 case IVTV_ENC_STREAM_TYPE_VBI:
729 cap_type = 1;
730 break;
731 case IVTV_ENC_STREAM_TYPE_MPG:
732 default:
733 cap_type = 0;
734 break;
735 }
736
737 /* Stop Capture Mode */
738 if (s->type == IVTV_ENC_STREAM_TYPE_MPG && gop_end) {
739 stopmode = 0;
740 } else {
741 stopmode = 1;
742 }
743
744 /* end_capture */
745 /* when: 0 = end of GOP 1 = NOW!, type: 0 = mpeg, subtype: 3 = video+audio */
746 ivtv_vapi(itv, CX2341X_ENC_STOP_CAPTURE, 3, stopmode, cap_type, s->subtype);
747
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300748 then = jiffies;
749
750 if (!test_bit(IVTV_F_S_PASSTHROUGH, &s->s_flags)) {
751 if (s->type == IVTV_ENC_STREAM_TYPE_MPG && gop_end) {
752 /* only run these if we're shutting down the last cap */
753 unsigned long duration;
754
755 then = jiffies;
756 add_wait_queue(&itv->cap_w, &wait);
757
758 set_current_state(TASK_INTERRUPTIBLE);
759
760 /* wait 2s for EOS interrupt */
Mauro Carvalho Chehab201700d2007-07-19 11:21:04 -0300761 while (!test_bit(IVTV_F_I_EOS, &itv->i_flags) &&
762 jiffies < then + msecs_to_jiffies (2000)) {
763 schedule_timeout(msecs_to_jiffies(10));
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300764 }
765
766 /* To convert jiffies to ms, we must multiply by 1000
767 * and divide by HZ. To avoid runtime division, we
768 * convert this to multiplication by 1000/HZ.
769 * Since integer division truncates, we get the best
770 * accuracy if we do a rounding calculation of the constant.
771 * Think of the case where HZ is 1024.
772 */
773 duration = ((1000 + HZ / 2) / HZ) * (jiffies - then);
774
775 if (!test_bit(IVTV_F_I_EOS, &itv->i_flags)) {
776 IVTV_DEBUG_WARN("%s: EOS interrupt not received! stopping anyway.\n", s->name);
777 IVTV_DEBUG_WARN("%s: waited %lu ms.\n", s->name, duration);
778 } else {
779 IVTV_DEBUG_INFO("%s: EOS took %lu ms to occur.\n", s->name, duration);
780 }
781 set_current_state(TASK_RUNNING);
782 remove_wait_queue(&itv->cap_w, &wait);
783 }
784
785 then = jiffies;
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300786
Hans Verkuil3562c432007-08-18 11:46:05 -0300787 /* Handle any pending interrupts */
788 ivtv_msleep_timeout(100, 1);
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300789 }
790
791 atomic_dec(&itv->capturing);
792
793 /* Clear capture and no-read bits */
794 clear_bit(IVTV_F_S_STREAMING, &s->s_flags);
795
796 if (s->type == IVTV_ENC_STREAM_TYPE_VBI)
797 ivtv_set_irq_mask(itv, IVTV_IRQ_ENC_VBI_CAP);
798
799 if (atomic_read(&itv->capturing) > 0) {
800 return 0;
801 }
802
803 /* Set the following Interrupt mask bits for capture */
804 ivtv_set_irq_mask(itv, IVTV_IRQ_MASK_CAPTURE);
Hans Verkuile17a06b2007-08-18 15:48:42 -0300805 del_timer(&itv->dma_timer);
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300806
Hans Verkuilf8859692007-07-10 14:58:33 -0300807 /* event notification (off) */
808 if (test_and_clear_bit(IVTV_F_I_DIG_RST, &itv->i_flags)) {
809 /* type: 0 = refresh */
810 /* on/off: 0 = off, intr: 0x10000000, mbox_id: -1: none */
811 ivtv_vapi(itv, CX2341X_ENC_SET_EVENT_NOTIFICATION, 4, 0, 0, IVTV_IRQ_ENC_VIM_RST, -1);
812 ivtv_set_irq_mask(itv, IVTV_IRQ_ENC_VIM_RST);
813 }
814
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300815 wake_up(&s->waitq);
816
817 return 0;
818}
819
820int ivtv_stop_v4l2_decode_stream(struct ivtv_stream *s, int flags, u64 pts)
821{
822 struct ivtv *itv = s->itv;
823
824 if (s->v4l2dev == NULL)
825 return -EINVAL;
826
827 if (s->type != IVTV_DEC_STREAM_TYPE_YUV && s->type != IVTV_DEC_STREAM_TYPE_MPG)
828 return -EINVAL;
829
830 if (!test_bit(IVTV_F_S_STREAMING, &s->s_flags))
831 return 0;
832
Hans Verkuilc4385092007-06-07 09:04:03 -0300833 IVTV_DEBUG_INFO("Stop Decode at %llu, flags: %x\n", (unsigned long long)pts, flags);
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300834
835 /* Stop Decoder */
836 if (!(flags & VIDEO_CMD_STOP_IMMEDIATELY) || pts) {
837 u32 tmp = 0;
838
839 /* Wait until the decoder is no longer running */
840 if (pts) {
841 ivtv_vapi(itv, CX2341X_DEC_STOP_PLAYBACK, 3,
842 0, (u32)(pts & 0xffffffff), (u32)(pts >> 32));
843 }
844 while (1) {
845 u32 data[CX2341X_MBOX_MAX_DATA];
846 ivtv_vapi_result(itv, data, CX2341X_DEC_GET_XFER_INFO, 0);
847 if (s->q_full.buffers + s->q_dma.buffers == 0) {
848 if (tmp == data[3])
849 break;
850 tmp = data[3];
851 }
Mauro Carvalho Chehab201700d2007-07-19 11:21:04 -0300852 if (ivtv_msleep_timeout(100, 1))
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300853 break;
854 }
855 }
856 ivtv_vapi(itv, CX2341X_DEC_STOP_PLAYBACK, 3, flags & VIDEO_CMD_STOP_TO_BLACK, 0, 0);
857
858 /* turn off notification of dual/stereo mode change */
859 ivtv_vapi(itv, CX2341X_DEC_SET_EVENT_NOTIFICATION, 4, 0, 0, IVTV_IRQ_DEC_AUD_MODE_CHG, -1);
860
861 ivtv_set_irq_mask(itv, IVTV_IRQ_MASK_DECODE);
Hans Verkuile17a06b2007-08-18 15:48:42 -0300862 del_timer(&itv->dma_timer);
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300863
864 clear_bit(IVTV_F_S_NEEDS_DATA, &s->s_flags);
865 clear_bit(IVTV_F_S_STREAMING, &s->s_flags);
866 ivtv_flush_queues(s);
867
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300868 /* decrement decoding */
869 atomic_dec(&itv->decoding);
870
871 set_bit(IVTV_F_I_EV_DEC_STOPPED, &itv->i_flags);
872 wake_up(&itv->event_waitq);
873
874 /* wake up wait queues */
875 wake_up(&s->waitq);
876
877 return 0;
878}
879
880int ivtv_passthrough_mode(struct ivtv *itv, int enable)
881{
882 struct ivtv_stream *yuv_stream = &itv->streams[IVTV_ENC_STREAM_TYPE_YUV];
883 struct ivtv_stream *dec_stream = &itv->streams[IVTV_DEC_STREAM_TYPE_YUV];
884
885 if (yuv_stream->v4l2dev == NULL || dec_stream->v4l2dev == NULL)
886 return -EINVAL;
887
888 IVTV_DEBUG_INFO("ivtv ioctl: Select passthrough mode\n");
889
890 /* Prevent others from starting/stopping streams while we
891 initiate/terminate passthrough mode */
892 if (enable) {
893 if (itv->output_mode == OUT_PASSTHROUGH) {
894 return 0;
895 }
896 if (ivtv_set_output_mode(itv, OUT_PASSTHROUGH) != OUT_PASSTHROUGH)
897 return -EBUSY;
898
899 /* Fully initialize stream, and then unflag init */
900 set_bit(IVTV_F_S_PASSTHROUGH, &dec_stream->s_flags);
901 set_bit(IVTV_F_S_STREAMING, &dec_stream->s_flags);
902
903 /* Setup YUV Decoder */
904 ivtv_setup_v4l2_decode_stream(dec_stream);
905
906 /* Start Decoder */
907 ivtv_vapi(itv, CX2341X_DEC_START_PLAYBACK, 2, 0, 1);
908 atomic_inc(&itv->decoding);
909
910 /* Setup capture if not already done */
911 if (atomic_read(&itv->capturing) == 0) {
912 cx2341x_update(itv, ivtv_api_func, NULL, &itv->params);
913 }
914
915 /* Start Passthrough Mode */
916 ivtv_vapi(itv, CX2341X_ENC_START_CAPTURE, 2, 2, 11);
917 atomic_inc(&itv->capturing);
918 return 0;
919 }
920
921 if (itv->output_mode != OUT_PASSTHROUGH)
922 return 0;
923
924 /* Stop Passthrough Mode */
925 ivtv_vapi(itv, CX2341X_ENC_STOP_CAPTURE, 3, 1, 2, 11);
926 ivtv_vapi(itv, CX2341X_DEC_STOP_PLAYBACK, 3, 1, 0, 0);
927
928 atomic_dec(&itv->capturing);
929 atomic_dec(&itv->decoding);
930 clear_bit(IVTV_F_S_PASSTHROUGH, &dec_stream->s_flags);
931 clear_bit(IVTV_F_S_STREAMING, &dec_stream->s_flags);
932 itv->output_mode = OUT_NONE;
933
934 return 0;
935}