Merge tag 'csky-for-linus-4.20-fixup-dtb' of https://github.com/c-sky/csky-linux
[linux-block.git] / drivers / media / pci / ivtv / ivtv-streams.c
CommitLineData
1a0adaf3
HV
1/*
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"
1a0adaf3
HV
38#include "ivtv-queue.h"
39#include "ivtv-mailbox.h"
1a0adaf3 40#include "ivtv-ioctl.h"
33c0fcad 41#include "ivtv-irq.h"
612570f2 42#include "ivtv-yuv.h"
1a0adaf3 43#include "ivtv-cards.h"
612570f2 44#include "ivtv-streams.h"
914610e8 45#include "ivtv-firmware.h"
09250193 46#include <media/v4l2-event.h>
1a0adaf3 47
bec43661 48static const struct v4l2_file_operations ivtv_v4l2_enc_fops = {
daf20d95
HV
49 .owner = THIS_MODULE,
50 .read = ivtv_v4l2_read,
51 .write = ivtv_v4l2_write,
52 .open = ivtv_v4l2_open,
074689d6 53 .unlocked_ioctl = video_ioctl2,
8a24280b
AB
54#ifdef CONFIG_COMPAT
55 .compat_ioctl32 = video_ioctl2, /* for ivtv_default() */
56#endif
daf20d95
HV
57 .release = ivtv_v4l2_close,
58 .poll = ivtv_v4l2_enc_poll,
1a0adaf3
HV
59};
60
bec43661 61static const struct v4l2_file_operations ivtv_v4l2_dec_fops = {
daf20d95
HV
62 .owner = THIS_MODULE,
63 .read = ivtv_v4l2_read,
64 .write = ivtv_v4l2_write,
65 .open = ivtv_v4l2_open,
074689d6 66 .unlocked_ioctl = video_ioctl2,
8a24280b
AB
67#ifdef CONFIG_COMPAT
68 .compat_ioctl32 = video_ioctl2, /* for ivtv_default() */
69#endif
daf20d95
HV
70 .release = ivtv_v4l2_close,
71 .poll = ivtv_v4l2_dec_poll,
1a0adaf3
HV
72};
73
7eaf4966
HV
74static const struct v4l2_file_operations ivtv_v4l2_radio_fops = {
75 .owner = THIS_MODULE,
76 .open = ivtv_v4l2_open,
77 .unlocked_ioctl = video_ioctl2,
8a24280b
AB
78#ifdef CONFIG_COMPAT
79 .compat_ioctl32 = video_ioctl2, /* for ivtv_default() */
80#endif
7eaf4966
HV
81 .release = ivtv_v4l2_close,
82 .poll = ivtv_v4l2_enc_poll,
83};
84
33c0fcad
HV
85#define IVTV_V4L2_DEC_MPG_OFFSET 16 /* offset from 0 to register decoder mpg v4l2 minors on */
86#define IVTV_V4L2_ENC_PCM_OFFSET 24 /* offset from 0 to register pcm v4l2 minors on */
87#define IVTV_V4L2_ENC_YUV_OFFSET 32 /* offset from 0 to register yuv v4l2 minors on */
88#define IVTV_V4L2_DEC_YUV_OFFSET 48 /* offset from 0 to register decoder yuv v4l2 minors on */
89#define IVTV_V4L2_DEC_VBI_OFFSET 8 /* offset from 0 to register decoder vbi input v4l2 minors on */
90#define IVTV_V4L2_DEC_VOUT_OFFSET 16 /* offset from 0 to register vbi output v4l2 minors on */
91
31ec1356 92static struct {
1a0adaf3
HV
93 const char *name;
94 int vfl_type;
dd89601d 95 int num_offset;
1a0adaf3 96 int dma, pio;
d0c8b2d4 97 u32 v4l2_caps;
bec43661 98 const struct v4l2_file_operations *fops;
1a0adaf3
HV
99} ivtv_stream_info[] = {
100 { /* IVTV_ENC_STREAM_TYPE_MPG */
7c03a448 101 "encoder MPG",
1a0adaf3 102 VFL_TYPE_GRABBER, 0,
ff82b211 103 PCI_DMA_FROMDEVICE, 0,
d0c8b2d4
HV
104 V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_TUNER |
105 V4L2_CAP_AUDIO | V4L2_CAP_READWRITE,
1a0adaf3
HV
106 &ivtv_v4l2_enc_fops
107 },
108 { /* IVTV_ENC_STREAM_TYPE_YUV */
109 "encoder YUV",
110 VFL_TYPE_GRABBER, IVTV_V4L2_ENC_YUV_OFFSET,
ff82b211 111 PCI_DMA_FROMDEVICE, 0,
d0c8b2d4
HV
112 V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_TUNER |
113 V4L2_CAP_AUDIO | V4L2_CAP_READWRITE,
1a0adaf3
HV
114 &ivtv_v4l2_enc_fops
115 },
116 { /* IVTV_ENC_STREAM_TYPE_VBI */
117 "encoder VBI",
118 VFL_TYPE_VBI, 0,
ff82b211 119 PCI_DMA_FROMDEVICE, 0,
d0c8b2d4
HV
120 V4L2_CAP_VBI_CAPTURE | V4L2_CAP_SLICED_VBI_CAPTURE | V4L2_CAP_TUNER |
121 V4L2_CAP_AUDIO | V4L2_CAP_READWRITE,
1a0adaf3
HV
122 &ivtv_v4l2_enc_fops
123 },
124 { /* IVTV_ENC_STREAM_TYPE_PCM */
7c03a448 125 "encoder PCM",
1a0adaf3 126 VFL_TYPE_GRABBER, IVTV_V4L2_ENC_PCM_OFFSET,
ff82b211 127 PCI_DMA_FROMDEVICE, 0,
d0c8b2d4 128 V4L2_CAP_TUNER | V4L2_CAP_AUDIO | V4L2_CAP_READWRITE,
1a0adaf3
HV
129 &ivtv_v4l2_enc_fops
130 },
131 { /* IVTV_ENC_STREAM_TYPE_RAD */
132 "encoder radio",
133 VFL_TYPE_RADIO, 0,
ff82b211 134 PCI_DMA_NONE, 1,
d0c8b2d4 135 V4L2_CAP_RADIO | V4L2_CAP_TUNER,
7eaf4966 136 &ivtv_v4l2_radio_fops
1a0adaf3
HV
137 },
138 { /* IVTV_DEC_STREAM_TYPE_MPG */
7c03a448 139 "decoder MPG",
1a0adaf3 140 VFL_TYPE_GRABBER, IVTV_V4L2_DEC_MPG_OFFSET,
ff82b211 141 PCI_DMA_TODEVICE, 0,
38a1421d
HV
142 V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_AUDIO | V4L2_CAP_READWRITE |
143 V4L2_CAP_VIDEO_OUTPUT_OVERLAY,
1a0adaf3
HV
144 &ivtv_v4l2_dec_fops
145 },
146 { /* IVTV_DEC_STREAM_TYPE_VBI */
147 "decoder VBI",
148 VFL_TYPE_VBI, IVTV_V4L2_DEC_VBI_OFFSET,
ff82b211 149 PCI_DMA_NONE, 1,
d0c8b2d4 150 V4L2_CAP_SLICED_VBI_CAPTURE | V4L2_CAP_READWRITE,
1a0adaf3
HV
151 &ivtv_v4l2_enc_fops
152 },
153 { /* IVTV_DEC_STREAM_TYPE_VOUT */
154 "decoder VOUT",
155 VFL_TYPE_VBI, IVTV_V4L2_DEC_VOUT_OFFSET,
ff82b211 156 PCI_DMA_NONE, 1,
d0c8b2d4 157 V4L2_CAP_SLICED_VBI_OUTPUT | V4L2_CAP_AUDIO | V4L2_CAP_READWRITE,
1a0adaf3
HV
158 &ivtv_v4l2_dec_fops
159 },
160 { /* IVTV_DEC_STREAM_TYPE_YUV */
161 "decoder YUV",
162 VFL_TYPE_GRABBER, IVTV_V4L2_DEC_YUV_OFFSET,
ff82b211 163 PCI_DMA_TODEVICE, 0,
38a1421d
HV
164 V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_AUDIO | V4L2_CAP_READWRITE |
165 V4L2_CAP_VIDEO_OUTPUT_OVERLAY,
1a0adaf3
HV
166 &ivtv_v4l2_dec_fops
167 }
168};
169
170static void ivtv_stream_init(struct ivtv *itv, int type)
171{
172 struct ivtv_stream *s = &itv->streams[type];
1a0adaf3 173
8ac05ae3 174 /* we need to keep vdev, so restore it afterwards */
1a0adaf3 175 memset(s, 0, sizeof(*s));
1a0adaf3
HV
176
177 /* initialize ivtv_stream fields */
178 s->itv = itv;
179 s->type = type;
180 s->name = ivtv_stream_info[type].name;
d0c8b2d4 181 s->caps = ivtv_stream_info[type].v4l2_caps;
1a0adaf3
HV
182
183 if (ivtv_stream_info[type].pio)
184 s->dma = PCI_DMA_NONE;
185 else
186 s->dma = ivtv_stream_info[type].dma;
187 s->buf_size = itv->stream_buf_size[type];
188 if (s->buf_size)
313e91e8 189 s->buffers = (itv->options.kilobytes[type] * 1024 + s->buf_size - 1) / s->buf_size;
1a0adaf3
HV
190 spin_lock_init(&s->qlock);
191 init_waitqueue_head(&s->waitq);
37093b1e 192 s->sg_handle = IVTV_DMA_UNMAPPED;
1a0adaf3
HV
193 ivtv_queue_init(&s->q_free);
194 ivtv_queue_init(&s->q_full);
195 ivtv_queue_init(&s->q_dma);
196 ivtv_queue_init(&s->q_predma);
197 ivtv_queue_init(&s->q_io);
198}
199
18e16f9c 200static int ivtv_prep_dev(struct ivtv *itv, int type)
1a0adaf3
HV
201{
202 struct ivtv_stream *s = &itv->streams[type];
dd89601d 203 int num_offset = ivtv_stream_info[type].num_offset;
67ec09fd 204 int num = itv->instance + ivtv_first_minor + num_offset;
1a0adaf3 205
635d62f0 206 /* These four fields are always initialized. If vdev.v4l2_dev == NULL, then
1a0adaf3
HV
207 this stream is not in use. In that case no other fields but these
208 four can be used. */
635d62f0 209 s->vdev.v4l2_dev = NULL;
1a0adaf3
HV
210 s->itv = itv;
211 s->type = type;
212 s->name = ivtv_stream_info[type].name;
213
214 /* Check whether the radio is supported */
215 if (type == IVTV_ENC_STREAM_TYPE_RAD && !(itv->v4l2_cap & V4L2_CAP_RADIO))
216 return 0;
217 if (type >= IVTV_DEC_STREAM_TYPE_MPG && !(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
218 return 0;
219
1a0adaf3
HV
220 /* User explicitly selected 0 buffers for these streams, so don't
221 create them. */
18e16f9c 222 if (ivtv_stream_info[type].dma != PCI_DMA_NONE &&
313e91e8 223 itv->options.kilobytes[type] == 0) {
1a0adaf3
HV
224 IVTV_INFO("Disabled %s device\n", ivtv_stream_info[type].name);
225 return 0;
226 }
227
228 ivtv_stream_init(itv, type);
229
635d62f0 230 snprintf(s->vdev.name, sizeof(s->vdev.name), "%s %s",
8ac05ae3 231 itv->v4l2_dev.name, s->name);
1a0adaf3 232
635d62f0
HV
233 s->vdev.num = num;
234 s->vdev.v4l2_dev = &itv->v4l2_dev;
954f340f
HV
235 if (ivtv_stream_info[type].v4l2_caps &
236 (V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_SLICED_VBI_OUTPUT))
635d62f0
HV
237 s->vdev.vfl_dir = VFL_DIR_TX;
238 s->vdev.fops = ivtv_stream_info[type].fops;
239 s->vdev.ctrl_handler = itv->v4l2_dev.ctrl_handler;
240 s->vdev.release = video_device_release_empty;
241 s->vdev.tvnorms = V4L2_STD_ALL;
242 s->vdev.lock = &itv->serialize_lock;
5f9c82c0 243 if (s->type == IVTV_DEC_STREAM_TYPE_VBI) {
635d62f0
HV
244 v4l2_disable_ioctl(&s->vdev, VIDIOC_S_AUDIO);
245 v4l2_disable_ioctl(&s->vdev, VIDIOC_G_AUDIO);
246 v4l2_disable_ioctl(&s->vdev, VIDIOC_ENUMAUDIO);
247 v4l2_disable_ioctl(&s->vdev, VIDIOC_ENUMINPUT);
248 v4l2_disable_ioctl(&s->vdev, VIDIOC_S_INPUT);
249 v4l2_disable_ioctl(&s->vdev, VIDIOC_G_INPUT);
250 v4l2_disable_ioctl(&s->vdev, VIDIOC_S_FREQUENCY);
251 v4l2_disable_ioctl(&s->vdev, VIDIOC_G_FREQUENCY);
252 v4l2_disable_ioctl(&s->vdev, VIDIOC_S_TUNER);
253 v4l2_disable_ioctl(&s->vdev, VIDIOC_G_TUNER);
254 v4l2_disable_ioctl(&s->vdev, VIDIOC_S_STD);
5f9c82c0 255 }
635d62f0 256 ivtv_set_funcs(&s->vdev);
18e16f9c
HV
257 return 0;
258}
259
260/* Initialize v4l2 variables and prepare v4l2 devices */
261int ivtv_streams_setup(struct ivtv *itv)
262{
263 int type;
264
265 /* Setup V4L2 Devices */
266 for (type = 0; type < IVTV_MAX_STREAMS; type++) {
267 /* Prepare device */
268 if (ivtv_prep_dev(itv, type))
269 break;
270
635d62f0 271 if (itv->streams[type].vdev.v4l2_dev == NULL)
18e16f9c
HV
272 continue;
273
274 /* Allocate Stream */
275 if (ivtv_stream_alloc(&itv->streams[type]))
276 break;
1a0adaf3 277 }
18e16f9c 278 if (type == IVTV_MAX_STREAMS)
1a0adaf3 279 return 0;
18e16f9c
HV
280
281 /* One or more streams could not be initialized. Clean 'em all up. */
635d62f0 282 ivtv_streams_cleanup(itv);
18e16f9c
HV
283 return -ENOMEM;
284}
285
286static int ivtv_reg_dev(struct ivtv *itv, int type)
287{
288 struct ivtv_stream *s = &itv->streams[type];
289 int vfl_type = ivtv_stream_info[type].vfl_type;
38c7c036 290 const char *name;
dd89601d 291 int num;
18e16f9c 292
635d62f0 293 if (s->vdev.v4l2_dev == NULL)
18e16f9c
HV
294 return 0;
295
635d62f0 296 num = s->vdev.num;
dd89601d
HV
297 /* card number + user defined offset + device offset */
298 if (type != IVTV_ENC_STREAM_TYPE_MPG) {
299 struct ivtv_stream *s_mpg = &itv->streams[IVTV_ENC_STREAM_TYPE_MPG];
300
635d62f0
HV
301 if (s_mpg->vdev.v4l2_dev)
302 num = s_mpg->vdev.num + ivtv_stream_info[type].num_offset;
dd89601d 303 }
635d62f0 304 video_set_drvdata(&s->vdev, s);
dd89601d 305
18e16f9c 306 /* Register device. First try the desired minor, then any free one. */
635d62f0 307 if (video_register_device_no_warn(&s->vdev, vfl_type, num)) {
581644d9 308 IVTV_ERR("Couldn't register v4l2 device for %s (device node number %d)\n",
dd89601d 309 s->name, num);
18e16f9c 310 return -ENOMEM;
1a0adaf3 311 }
635d62f0 312 name = video_device_node_name(&s->vdev);
1a0adaf3
HV
313
314 switch (vfl_type) {
315 case VFL_TYPE_GRABBER:
38c7c036
LP
316 IVTV_INFO("Registered device %s for %s (%d kB)\n",
317 name, s->name, itv->options.kilobytes[type]);
1a0adaf3
HV
318 break;
319 case VFL_TYPE_RADIO:
38c7c036
LP
320 IVTV_INFO("Registered device %s for %s\n",
321 name, s->name);
1a0adaf3
HV
322 break;
323 case VFL_TYPE_VBI:
313e91e8 324 if (itv->options.kilobytes[type])
38c7c036
LP
325 IVTV_INFO("Registered device %s for %s (%d kB)\n",
326 name, s->name, itv->options.kilobytes[type]);
1a0adaf3 327 else
38c7c036
LP
328 IVTV_INFO("Registered device %s for %s\n",
329 name, s->name);
1a0adaf3
HV
330 break;
331 }
332 return 0;
333}
334
18e16f9c
HV
335/* Register v4l2 devices */
336int ivtv_streams_register(struct ivtv *itv)
1a0adaf3
HV
337{
338 int type;
18e16f9c 339 int err = 0;
1a0adaf3 340
18e16f9c
HV
341 /* Register V4L2 devices */
342 for (type = 0; type < IVTV_MAX_STREAMS; type++)
343 err |= ivtv_reg_dev(itv, type);
1a0adaf3 344
18e16f9c 345 if (err == 0)
1a0adaf3 346 return 0;
1a0adaf3
HV
347
348 /* One or more streams could not be initialized. Clean 'em all up. */
635d62f0 349 ivtv_streams_cleanup(itv);
1a0adaf3
HV
350 return -ENOMEM;
351}
352
353/* Unregister v4l2 devices */
635d62f0 354void ivtv_streams_cleanup(struct ivtv *itv)
1a0adaf3
HV
355{
356 int type;
357
358 /* Teardown all streams */
359 for (type = 0; type < IVTV_MAX_STREAMS; type++) {
635d62f0 360 struct video_device *vdev = &itv->streams[type].vdev;
1a0adaf3 361
635d62f0 362 if (vdev->v4l2_dev == NULL)
1a0adaf3
HV
363 continue;
364
635d62f0 365 video_unregister_device(vdev);
1a0adaf3 366 ivtv_stream_free(&itv->streams[type]);
635d62f0 367 itv->streams[type].vdev.v4l2_dev = NULL;
1a0adaf3
HV
368 }
369}
370
371static void ivtv_vbi_setup(struct ivtv *itv)
372{
a8b86435 373 int raw = ivtv_raw_vbi(itv);
1a0adaf3
HV
374 u32 data[CX2341X_MBOX_MAX_DATA];
375 int lines;
376 int i;
377
1a0adaf3
HV
378 /* Reset VBI */
379 ivtv_vapi(itv, CX2341X_ENC_SET_VBI_LINE, 5, 0xffff , 0, 0, 0, 0);
380
1a0adaf3 381 /* setup VBI registers */
4ff0790b
HV
382 if (raw)
383 v4l2_subdev_call(itv->sd_video, vbi, s_raw_fmt, &itv->vbi.in.fmt.vbi);
384 else
385 v4l2_subdev_call(itv->sd_video, vbi, s_sliced_fmt, &itv->vbi.in.fmt.sliced);
1a0adaf3
HV
386
387 /* determine number of lines and total number of VBI bytes.
388 A raw line takes 1443 bytes: 2 * 720 + 4 byte frame header - 1
389 The '- 1' byte is probably an unused U or V byte. Or something...
390 A sliced line takes 51 bytes: 4 byte frame header, 4 byte internal
391 header, 42 data bytes + checksum (to be confirmed) */
392 if (raw) {
393 lines = itv->vbi.count * 2;
394 } else {
395 lines = itv->is_60hz ? 24 : 38;
396 if (itv->is_60hz && (itv->hw_flags & IVTV_HW_CX25840))
397 lines += 2;
398 }
399
400 itv->vbi.enc_size = lines * (raw ? itv->vbi.raw_size : itv->vbi.sliced_size);
401
402 /* Note: sliced vs raw flag doesn't seem to have any effect
403 TODO: check mode (0x02) value with older ivtv versions. */
404 data[0] = raw | 0x02 | (0xbd << 8);
405
406 /* Every X number of frames a VBI interrupt arrives (frames as in 25 or 30 fps) */
407 data[1] = 1;
408 /* The VBI frames are stored in a ringbuffer with this size (with a VBI frame as unit) */
d526afe0 409 data[2] = raw ? 4 : 4 * (itv->vbi.raw_size / itv->vbi.enc_size);
1a0adaf3
HV
410 /* The start/stop codes determine which VBI lines end up in the raw VBI data area.
411 The codes are from table 24 in the saa7115 datasheet. Each raw/sliced/video line
412 is framed with codes FF0000XX where XX is the SAV/EAV (Start/End of Active Video)
413 code. These values for raw VBI are obtained from a driver disassembly. The sliced
414 start/stop codes was deduced from this, but they do not appear in the driver.
415 Other code pairs that I found are: 0x250E6249/0x13545454 and 0x25256262/0x38137F54.
416 However, I have no idea what these values are for. */
417 if (itv->hw_flags & IVTV_HW_CX25840) {
418 /* Setup VBI for the cx25840 digitizer */
419 if (raw) {
420 data[3] = 0x20602060;
421 data[4] = 0x30703070;
422 } else {
423 data[3] = 0xB0F0B0F0;
424 data[4] = 0xA0E0A0E0;
425 }
426 /* Lines per frame */
427 data[5] = lines;
428 /* bytes per line */
429 data[6] = (raw ? itv->vbi.raw_size : itv->vbi.sliced_size);
430 } else {
431 /* Setup VBI for the saa7115 digitizer */
432 if (raw) {
433 data[3] = 0x25256262;
434 data[4] = 0x387F7F7F;
435 } else {
436 data[3] = 0xABABECEC;
437 data[4] = 0xB6F1F1F1;
438 }
439 /* Lines per frame */
440 data[5] = lines;
441 /* bytes per line */
442 data[6] = itv->vbi.enc_size / lines;
443 }
444
445 IVTV_DEBUG_INFO(
446 "Setup VBI API header 0x%08x pkts %d buffs %d ln %d sz %d\n",
447 data[0], data[1], data[2], data[5], data[6]);
448
449 ivtv_api(itv, CX2341X_ENC_SET_VBI_CONFIG, 7, data);
450
451 /* returns the VBI encoder memory area. */
452 itv->vbi.enc_start = data[2];
453 itv->vbi.fpi = data[0];
454 if (!itv->vbi.fpi)
455 itv->vbi.fpi = 1;
456
fd8b281a
HV
457 IVTV_DEBUG_INFO("Setup VBI start 0x%08x frames %d fpi %d\n",
458 itv->vbi.enc_start, data[1], itv->vbi.fpi);
1a0adaf3
HV
459
460 /* select VBI lines.
461 Note that the sliced argument seems to have no effect. */
462 for (i = 2; i <= 24; i++) {
463 int valid;
464
465 if (itv->is_60hz) {
466 valid = i >= 10 && i < 22;
467 } else {
468 valid = i >= 6 && i < 24;
469 }
470 ivtv_vapi(itv, CX2341X_ENC_SET_VBI_LINE, 5, i - 1,
471 valid, 0 , 0, 0);
472 ivtv_vapi(itv, CX2341X_ENC_SET_VBI_LINE, 5, (i - 1) | 0x80000000,
473 valid, 0, 0, 0);
474 }
475
476 /* Remaining VBI questions:
477 - Is it possible to select particular VBI lines only for inclusion in the MPEG
478 stream? Currently you can only get the first X lines.
479 - Is mixed raw and sliced VBI possible?
480 - What's the meaning of the raw/sliced flag?
481 - What's the meaning of params 2, 3 & 4 of the Select VBI command? */
482}
483
484int ivtv_start_v4l2_encode_stream(struct ivtv_stream *s)
485{
486 u32 data[CX2341X_MBOX_MAX_DATA];
487 struct ivtv *itv = s->itv;
488 int captype = 0, subtype = 0;
489 int enable_passthrough = 0;
490
635d62f0 491 if (s->vdev.v4l2_dev == NULL)
1a0adaf3
HV
492 return -EINVAL;
493
494 IVTV_DEBUG_INFO("Start encoder stream %s\n", s->name);
495
496 switch (s->type) {
497 case IVTV_ENC_STREAM_TYPE_MPG:
498 captype = 0;
499 subtype = 3;
500
501 /* Stop Passthrough */
502 if (itv->output_mode == OUT_PASSTHROUGH) {
503 ivtv_passthrough_mode(itv, 0);
504 enable_passthrough = 1;
505 }
506 itv->mpg_data_received = itv->vbi_data_inserted = 0;
507 itv->dualwatch_jiffies = jiffies;
f7b80e69 508 itv->dualwatch_stereo_mode = v4l2_ctrl_g_ctrl(itv->cxhdl.audio_mode);
1a0adaf3
HV
509 itv->search_pack_header = 0;
510 break;
511
512 case IVTV_ENC_STREAM_TYPE_YUV:
513 if (itv->output_mode == OUT_PASSTHROUGH) {
514 captype = 2;
515 subtype = 11; /* video+audio+decoder */
516 break;
517 }
518 captype = 1;
519 subtype = 1;
520 break;
521 case IVTV_ENC_STREAM_TYPE_PCM:
522 captype = 1;
523 subtype = 2;
524 break;
525 case IVTV_ENC_STREAM_TYPE_VBI:
526 captype = 1;
527 subtype = 4;
528
529 itv->vbi.frame = 0;
530 itv->vbi.inserted_frame = 0;
531 memset(itv->vbi.sliced_mpeg_size,
532 0, sizeof(itv->vbi.sliced_mpeg_size));
533 break;
534 default:
535 return -EINVAL;
536 }
537 s->subtype = subtype;
538 s->buffers_stolen = 0;
539
1a0adaf3
HV
540 /* Clear Streamoff flags in case left from last capture */
541 clear_bit(IVTV_F_S_STREAMOFF, &s->s_flags);
542
543 if (atomic_read(&itv->capturing) == 0) {
fd8b281a
HV
544 int digitizer;
545
1a0adaf3
HV
546 /* Always use frame based mode. Experiments have demonstrated that byte
547 stream based mode results in dropped frames and corruption. Not often,
548 but occasionally. Many thanks go to Leonard Orb who spent a lot of
549 effort and time trying to trace the cause of the drop outs. */
550 /* 1 frame per DMA */
551 /*ivtv_vapi(itv, CX2341X_ENC_SET_DMA_BLOCK_SIZE, 2, 128, 0); */
552 ivtv_vapi(itv, CX2341X_ENC_SET_DMA_BLOCK_SIZE, 2, 1, 1);
553
554 /* Stuff from Windows, we don't know what it is */
555 ivtv_vapi(itv, CX2341X_ENC_SET_VERT_CROP_LINE, 1, 0);
556 /* According to the docs, this should be correct. However, this is
557 untested. I don't dare enable this without having tested it.
558 Only very few old cards actually have this hardware combination.
559 ivtv_vapi(itv, CX2341X_ENC_SET_VERT_CROP_LINE, 1,
560 ((itv->hw_flags & IVTV_HW_SAA7114) && itv->is_60hz) ? 10001 : 0);
561 */
562 ivtv_vapi(itv, CX2341X_ENC_MISC, 2, 3, !itv->has_cx23415);
563 ivtv_vapi(itv, CX2341X_ENC_MISC, 2, 8, 0);
564 ivtv_vapi(itv, CX2341X_ENC_MISC, 2, 4, 1);
565 ivtv_vapi(itv, CX2341X_ENC_MISC, 1, 12);
566
567 /* assign placeholder */
568 ivtv_vapi(itv, CX2341X_ENC_SET_PLACEHOLDER, 12,
569 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
570
fd8b281a
HV
571 if (itv->card->hw_all & (IVTV_HW_SAA7115 | IVTV_HW_SAA717X))
572 digitizer = 0xF1;
573 else if (itv->card->hw_all & IVTV_HW_SAA7114)
574 digitizer = 0xEF;
575 else /* cx25840 */
576 digitizer = 0x140;
577
578 ivtv_vapi(itv, CX2341X_ENC_SET_NUM_VSYNC_LINES, 2, digitizer, digitizer);
1a0adaf3
HV
579
580 /* Setup VBI */
581 if (itv->v4l2_cap & V4L2_CAP_VBI_CAPTURE) {
582 ivtv_vbi_setup(itv);
583 }
584
585 /* assign program index info. Mask 7: select I/P/B, Num_req: 400 max */
586 ivtv_vapi_result(itv, data, CX2341X_ENC_SET_PGM_INDEX_INFO, 2, 7, 400);
587 itv->pgm_info_offset = data[0];
588 itv->pgm_info_num = data[1];
589 itv->pgm_info_write_idx = 0;
590 itv->pgm_info_read_idx = 0;
591
592 IVTV_DEBUG_INFO("PGM Index at 0x%08x with %d elements\n",
593 itv->pgm_info_offset, itv->pgm_info_num);
594
595 /* Setup API for Stream */
f7b80e69 596 cx2341x_handler_setup(&itv->cxhdl);
254d6eb1
HV
597
598 /* mute if capturing radio */
599 if (test_bit(IVTV_F_I_RADIO_USER, &itv->i_flags))
600 ivtv_vapi(itv, CX2341X_ENC_MUTE_VIDEO, 1,
f7b80e69 601 1 | (v4l2_ctrl_g_ctrl(itv->cxhdl.video_mute_yuv) << 8));
1a0adaf3
HV
602 }
603
604 /* Vsync Setup */
605 if (itv->has_cx23415 && !test_and_set_bit(IVTV_F_I_DIG_RST, &itv->i_flags)) {
606 /* event notification (on) */
607 ivtv_vapi(itv, CX2341X_ENC_SET_EVENT_NOTIFICATION, 4, 0, 1, IVTV_IRQ_ENC_VIM_RST, -1);
608 ivtv_clear_irq_mask(itv, IVTV_IRQ_ENC_VIM_RST);
609 }
610
611 if (atomic_read(&itv->capturing) == 0) {
612 /* Clear all Pending Interrupts */
613 ivtv_set_irq_mask(itv, IVTV_IRQ_MASK_CAPTURE);
614
615 clear_bit(IVTV_F_I_EOS, &itv->i_flags);
616
f7b80e69
HV
617 cx2341x_handler_set_busy(&itv->cxhdl, 1);
618
1a0adaf3 619 /* Initialize Digitizer for Capture */
3ccc646b
AW
620 /* Avoid tinny audio problem - ensure audio clocks are going */
621 v4l2_subdev_call(itv->sd_audio, audio, s_stream, 1);
622 /* Avoid unpredictable PCI bus hang - disable video clocks */
67ec09fd 623 v4l2_subdev_call(itv->sd_video, video, s_stream, 0);
7700a0d2 624 ivtv_msleep_timeout(300, 0);
1a0adaf3 625 ivtv_vapi(itv, CX2341X_ENC_INITIALIZE_INPUT, 0);
67ec09fd 626 v4l2_subdev_call(itv->sd_video, video, s_stream, 1);
1a0adaf3
HV
627 }
628
629 /* begin_capture */
630 if (ivtv_vapi(itv, CX2341X_ENC_START_CAPTURE, 2, captype, subtype))
631 {
632 IVTV_DEBUG_WARN( "Error starting capture!\n");
633 return -EINVAL;
634 }
635
636 /* Start Passthrough */
637 if (enable_passthrough) {
638 ivtv_passthrough_mode(itv, 1);
639 }
640
641 if (s->type == IVTV_ENC_STREAM_TYPE_VBI)
642 ivtv_clear_irq_mask(itv, IVTV_IRQ_ENC_VBI_CAP);
643 else
644 ivtv_clear_irq_mask(itv, IVTV_IRQ_MASK_CAPTURE);
645
646 /* you're live! sit back and await interrupts :) */
647 atomic_inc(&itv->capturing);
648 return 0;
649}
269c11fb 650EXPORT_SYMBOL(ivtv_start_v4l2_encode_stream);
1a0adaf3
HV
651
652static int ivtv_setup_v4l2_decode_stream(struct ivtv_stream *s)
653{
654 u32 data[CX2341X_MBOX_MAX_DATA];
655 struct ivtv *itv = s->itv;
656 int datatype;
bd62307b
IA
657 u16 width;
658 u16 height;
1a0adaf3 659
635d62f0 660 if (s->vdev.v4l2_dev == NULL)
1a0adaf3
HV
661 return -EINVAL;
662
663 IVTV_DEBUG_INFO("Setting some initial decoder settings\n");
664
f7b80e69
HV
665 width = itv->cxhdl.width;
666 height = itv->cxhdl.height;
bd62307b 667
1a0adaf3
HV
668 /* set audio mode to left/stereo for dual/stereo mode. */
669 ivtv_vapi(itv, CX2341X_DEC_SET_AUDIO_MODE, 2, itv->audio_bilingual_mode, itv->audio_stereo_mode);
670
671 /* set number of internal decoder buffers */
672 ivtv_vapi(itv, CX2341X_DEC_SET_DISPLAY_BUFFERS, 1, 0);
673
674 /* prebuffering */
675 ivtv_vapi(itv, CX2341X_DEC_SET_PREBUFFERING, 1, 1);
676
677 /* extract from user packets */
678 ivtv_vapi_result(itv, data, CX2341X_DEC_EXTRACT_VBI, 1, 1);
679 itv->vbi.dec_start = data[0];
680
681 IVTV_DEBUG_INFO("Decoder VBI RE-Insert start 0x%08x size 0x%08x\n",
682 itv->vbi.dec_start, data[1]);
683
684 /* set decoder source settings */
685 /* Data type: 0 = mpeg from host,
686 1 = yuv from encoder,
687 2 = yuv_from_host */
688 switch (s->type) {
689 case IVTV_DEC_STREAM_TYPE_YUV:
bd62307b
IA
690 if (itv->output_mode == OUT_PASSTHROUGH) {
691 datatype = 1;
692 } else {
693 /* Fake size to avoid switching video standard */
694 datatype = 2;
695 width = 720;
696 height = itv->is_out_50hz ? 576 : 480;
697 }
1a0adaf3
HV
698 IVTV_DEBUG_INFO("Setup DEC YUV Stream data[0] = %d\n", datatype);
699 break;
700 case IVTV_DEC_STREAM_TYPE_MPG:
701 default:
702 datatype = 0;
703 break;
704 }
705 if (ivtv_vapi(itv, CX2341X_DEC_SET_DECODER_SOURCE, 4, datatype,
f7b80e69 706 width, height, itv->cxhdl.audio_properties)) {
5a338c38 707 IVTV_DEBUG_WARN("Couldn't initialize decoder source\n");
1a0adaf3 708 }
666092c6
IA
709
710 /* Decoder sometimes dies here, so wait a moment */
711 ivtv_msleep_timeout(10, 0);
712
914610e8 713 /* Known failure point for firmware, so check */
215659d1 714 return ivtv_firmware_check(itv, "ivtv_setup_v4l2_decode_stream");
1a0adaf3
HV
715}
716
717int ivtv_start_v4l2_decode_stream(struct ivtv_stream *s, int gop_offset)
718{
719 struct ivtv *itv = s->itv;
914610e8 720 int rc;
1a0adaf3 721
635d62f0 722 if (s->vdev.v4l2_dev == NULL)
1a0adaf3
HV
723 return -EINVAL;
724
725 if (test_and_set_bit(IVTV_F_S_STREAMING, &s->s_flags))
726 return 0; /* already started */
727
728 IVTV_DEBUG_INFO("Starting decode stream %s (gop_offset %d)\n", s->name, gop_offset);
729
914610e8
IA
730 rc = ivtv_setup_v4l2_decode_stream(s);
731 if (rc < 0) {
732 clear_bit(IVTV_F_S_STREAMING, &s->s_flags);
733 return rc;
734 }
1a0adaf3
HV
735
736 /* set dma size to 65536 bytes */
737 ivtv_vapi(itv, CX2341X_DEC_SET_DMA_BLOCK_SIZE, 1, 65536);
738
77aded6b 739 /* Clear Streamoff */
1a0adaf3
HV
740 clear_bit(IVTV_F_S_STREAMOFF, &s->s_flags);
741
742 /* Zero out decoder counters */
33c0fcad
HV
743 writel(0, &itv->dec_mbox.mbox[IVTV_MBOX_DMA_END].data[0]);
744 writel(0, &itv->dec_mbox.mbox[IVTV_MBOX_DMA_END].data[1]);
745 writel(0, &itv->dec_mbox.mbox[IVTV_MBOX_DMA_END].data[2]);
746 writel(0, &itv->dec_mbox.mbox[IVTV_MBOX_DMA_END].data[3]);
1a0adaf3
HV
747 writel(0, &itv->dec_mbox.mbox[IVTV_MBOX_DMA].data[0]);
748 writel(0, &itv->dec_mbox.mbox[IVTV_MBOX_DMA].data[1]);
749 writel(0, &itv->dec_mbox.mbox[IVTV_MBOX_DMA].data[2]);
750 writel(0, &itv->dec_mbox.mbox[IVTV_MBOX_DMA].data[3]);
751
752 /* turn on notification of dual/stereo mode change */
753 ivtv_vapi(itv, CX2341X_DEC_SET_EVENT_NOTIFICATION, 4, 0, 1, IVTV_IRQ_DEC_AUD_MODE_CHG, -1);
754
755 /* start playback */
756 ivtv_vapi(itv, CX2341X_DEC_START_PLAYBACK, 2, gop_offset, 0);
757
666092c6
IA
758 /* Let things settle before we actually start */
759 ivtv_msleep_timeout(10, 0);
760
1a0adaf3
HV
761 /* Clear the following Interrupt mask bits for decoding */
762 ivtv_clear_irq_mask(itv, IVTV_IRQ_MASK_DECODE);
763 IVTV_DEBUG_IRQ("IRQ Mask is now: 0x%08x\n", itv->irqmask);
764
765 /* you're live! sit back and await interrupts :) */
766 atomic_inc(&itv->decoding);
767 return 0;
768}
769
770void ivtv_stop_all_captures(struct ivtv *itv)
771{
772 int i;
773
774 for (i = IVTV_MAX_STREAMS - 1; i >= 0; i--) {
775 struct ivtv_stream *s = &itv->streams[i];
776
635d62f0 777 if (s->vdev.v4l2_dev == NULL)
1a0adaf3
HV
778 continue;
779 if (test_bit(IVTV_F_S_STREAMING, &s->s_flags)) {
780 ivtv_stop_v4l2_encode_stream(s, 0);
781 }
782 }
783}
784
785int ivtv_stop_v4l2_encode_stream(struct ivtv_stream *s, int gop_end)
786{
787 struct ivtv *itv = s->itv;
788 DECLARE_WAITQUEUE(wait, current);
789 int cap_type;
1a0adaf3 790 int stopmode;
1a0adaf3 791
635d62f0 792 if (s->vdev.v4l2_dev == NULL)
1a0adaf3
HV
793 return -EINVAL;
794
795 /* This function assumes that you are allowed to stop the capture
796 and that we are actually capturing */
797
798 IVTV_DEBUG_INFO("Stop Capture\n");
799
800 if (s->type == IVTV_DEC_STREAM_TYPE_VOUT)
801 return 0;
802 if (atomic_read(&itv->capturing) == 0)
803 return 0;
804
805 switch (s->type) {
806 case IVTV_ENC_STREAM_TYPE_YUV:
807 cap_type = 1;
808 break;
809 case IVTV_ENC_STREAM_TYPE_PCM:
810 cap_type = 1;
811 break;
812 case IVTV_ENC_STREAM_TYPE_VBI:
813 cap_type = 1;
814 break;
815 case IVTV_ENC_STREAM_TYPE_MPG:
816 default:
817 cap_type = 0;
818 break;
819 }
820
821 /* Stop Capture Mode */
822 if (s->type == IVTV_ENC_STREAM_TYPE_MPG && gop_end) {
823 stopmode = 0;
824 } else {
825 stopmode = 1;
826 }
827
828 /* end_capture */
829 /* when: 0 = end of GOP 1 = NOW!, type: 0 = mpeg, subtype: 3 = video+audio */
830 ivtv_vapi(itv, CX2341X_ENC_STOP_CAPTURE, 3, stopmode, cap_type, s->subtype);
831
1a0adaf3
HV
832 if (!test_bit(IVTV_F_S_PASSTHROUGH, &s->s_flags)) {
833 if (s->type == IVTV_ENC_STREAM_TYPE_MPG && gop_end) {
834 /* only run these if we're shutting down the last cap */
835 unsigned long duration;
559e196a 836 unsigned long then = jiffies;
1a0adaf3 837
fd8b281a 838 add_wait_queue(&itv->eos_waitq, &wait);
1a0adaf3
HV
839
840 set_current_state(TASK_INTERRUPTIBLE);
841
842 /* wait 2s for EOS interrupt */
201700d3 843 while (!test_bit(IVTV_F_I_EOS, &itv->i_flags) &&
168c626c
JL
844 time_before(jiffies,
845 then + msecs_to_jiffies(2000))) {
201700d3 846 schedule_timeout(msecs_to_jiffies(10));
1a0adaf3
HV
847 }
848
849 /* To convert jiffies to ms, we must multiply by 1000
850 * and divide by HZ. To avoid runtime division, we
851 * convert this to multiplication by 1000/HZ.
852 * Since integer division truncates, we get the best
853 * accuracy if we do a rounding calculation of the constant.
854 * Think of the case where HZ is 1024.
855 */
856 duration = ((1000 + HZ / 2) / HZ) * (jiffies - then);
857
858 if (!test_bit(IVTV_F_I_EOS, &itv->i_flags)) {
859 IVTV_DEBUG_WARN("%s: EOS interrupt not received! stopping anyway.\n", s->name);
860 IVTV_DEBUG_WARN("%s: waited %lu ms.\n", s->name, duration);
861 } else {
862 IVTV_DEBUG_INFO("%s: EOS took %lu ms to occur.\n", s->name, duration);
863 }
864 set_current_state(TASK_RUNNING);
fd8b281a 865 remove_wait_queue(&itv->eos_waitq, &wait);
559e196a 866 set_bit(IVTV_F_S_STREAMOFF, &s->s_flags);
1a0adaf3
HV
867 }
868
3562c43b 869 /* Handle any pending interrupts */
7700a0d2 870 ivtv_msleep_timeout(100, 0);
1a0adaf3
HV
871 }
872
873 atomic_dec(&itv->capturing);
874
875 /* Clear capture and no-read bits */
876 clear_bit(IVTV_F_S_STREAMING, &s->s_flags);
877
878 if (s->type == IVTV_ENC_STREAM_TYPE_VBI)
879 ivtv_set_irq_mask(itv, IVTV_IRQ_ENC_VBI_CAP);
880
881 if (atomic_read(&itv->capturing) > 0) {
882 return 0;
883 }
884
f7b80e69
HV
885 cx2341x_handler_set_busy(&itv->cxhdl, 0);
886
1a0adaf3
HV
887 /* Set the following Interrupt mask bits for capture */
888 ivtv_set_irq_mask(itv, IVTV_IRQ_MASK_CAPTURE);
e17a06ba 889 del_timer(&itv->dma_timer);
1a0adaf3 890
f8859691
HV
891 /* event notification (off) */
892 if (test_and_clear_bit(IVTV_F_I_DIG_RST, &itv->i_flags)) {
893 /* type: 0 = refresh */
894 /* on/off: 0 = off, intr: 0x10000000, mbox_id: -1: none */
895 ivtv_vapi(itv, CX2341X_ENC_SET_EVENT_NOTIFICATION, 4, 0, 0, IVTV_IRQ_ENC_VIM_RST, -1);
896 ivtv_set_irq_mask(itv, IVTV_IRQ_ENC_VIM_RST);
897 }
898
9a766d55
IA
899 /* Raw-passthrough is implied on start. Make sure it's stopped so
900 the encoder will re-initialize when next started */
901 ivtv_vapi(itv, CX2341X_ENC_STOP_CAPTURE, 3, 1, 2, 7);
902
1a0adaf3
HV
903 wake_up(&s->waitq);
904
905 return 0;
906}
269c11fb 907EXPORT_SYMBOL(ivtv_stop_v4l2_encode_stream);
1a0adaf3
HV
908
909int ivtv_stop_v4l2_decode_stream(struct ivtv_stream *s, int flags, u64 pts)
910{
09250193
HV
911 static const struct v4l2_event ev = {
912 .type = V4L2_EVENT_EOS,
913 };
1a0adaf3
HV
914 struct ivtv *itv = s->itv;
915
635d62f0 916 if (s->vdev.v4l2_dev == NULL)
1a0adaf3
HV
917 return -EINVAL;
918
919 if (s->type != IVTV_DEC_STREAM_TYPE_YUV && s->type != IVTV_DEC_STREAM_TYPE_MPG)
920 return -EINVAL;
921
922 if (!test_bit(IVTV_F_S_STREAMING, &s->s_flags))
923 return 0;
924
c4385098 925 IVTV_DEBUG_INFO("Stop Decode at %llu, flags: %x\n", (unsigned long long)pts, flags);
1a0adaf3
HV
926
927 /* Stop Decoder */
da8ec560 928 if (!(flags & V4L2_DEC_CMD_STOP_IMMEDIATELY) || pts) {
1a0adaf3
HV
929 u32 tmp = 0;
930
931 /* Wait until the decoder is no longer running */
932 if (pts) {
933 ivtv_vapi(itv, CX2341X_DEC_STOP_PLAYBACK, 3,
934 0, (u32)(pts & 0xffffffff), (u32)(pts >> 32));
935 }
936 while (1) {
937 u32 data[CX2341X_MBOX_MAX_DATA];
938 ivtv_vapi_result(itv, data, CX2341X_DEC_GET_XFER_INFO, 0);
939 if (s->q_full.buffers + s->q_dma.buffers == 0) {
940 if (tmp == data[3])
941 break;
942 tmp = data[3];
943 }
201700d3 944 if (ivtv_msleep_timeout(100, 1))
1a0adaf3
HV
945 break;
946 }
947 }
da8ec560 948 ivtv_vapi(itv, CX2341X_DEC_STOP_PLAYBACK, 3, flags & V4L2_DEC_CMD_STOP_TO_BLACK, 0, 0);
1a0adaf3
HV
949
950 /* turn off notification of dual/stereo mode change */
951 ivtv_vapi(itv, CX2341X_DEC_SET_EVENT_NOTIFICATION, 4, 0, 0, IVTV_IRQ_DEC_AUD_MODE_CHG, -1);
952
953 ivtv_set_irq_mask(itv, IVTV_IRQ_MASK_DECODE);
e17a06ba 954 del_timer(&itv->dma_timer);
1a0adaf3
HV
955
956 clear_bit(IVTV_F_S_NEEDS_DATA, &s->s_flags);
957 clear_bit(IVTV_F_S_STREAMING, &s->s_flags);
958 ivtv_flush_queues(s);
959
f06b9bd4
IA
960 /* decoder needs time to settle */
961 ivtv_msleep_timeout(40, 0);
962
1a0adaf3
HV
963 /* decrement decoding */
964 atomic_dec(&itv->decoding);
965
966 set_bit(IVTV_F_I_EV_DEC_STOPPED, &itv->i_flags);
967 wake_up(&itv->event_waitq);
635d62f0 968 v4l2_event_queue(&s->vdev, &ev);
1a0adaf3
HV
969
970 /* wake up wait queues */
971 wake_up(&s->waitq);
972
973 return 0;
974}
975
976int ivtv_passthrough_mode(struct ivtv *itv, int enable)
977{
978 struct ivtv_stream *yuv_stream = &itv->streams[IVTV_ENC_STREAM_TYPE_YUV];
979 struct ivtv_stream *dec_stream = &itv->streams[IVTV_DEC_STREAM_TYPE_YUV];
980
635d62f0 981 if (yuv_stream->vdev.v4l2_dev == NULL || dec_stream->vdev.v4l2_dev == NULL)
1a0adaf3
HV
982 return -EINVAL;
983
984 IVTV_DEBUG_INFO("ivtv ioctl: Select passthrough mode\n");
985
986 /* Prevent others from starting/stopping streams while we
987 initiate/terminate passthrough mode */
988 if (enable) {
989 if (itv->output_mode == OUT_PASSTHROUGH) {
990 return 0;
991 }
992 if (ivtv_set_output_mode(itv, OUT_PASSTHROUGH) != OUT_PASSTHROUGH)
993 return -EBUSY;
994
995 /* Fully initialize stream, and then unflag init */
996 set_bit(IVTV_F_S_PASSTHROUGH, &dec_stream->s_flags);
997 set_bit(IVTV_F_S_STREAMING, &dec_stream->s_flags);
998
999 /* Setup YUV Decoder */
1000 ivtv_setup_v4l2_decode_stream(dec_stream);
1001
1002 /* Start Decoder */
1003 ivtv_vapi(itv, CX2341X_DEC_START_PLAYBACK, 2, 0, 1);
1004 atomic_inc(&itv->decoding);
1005
1006 /* Setup capture if not already done */
1007 if (atomic_read(&itv->capturing) == 0) {
f7b80e69
HV
1008 cx2341x_handler_setup(&itv->cxhdl);
1009 cx2341x_handler_set_busy(&itv->cxhdl, 1);
1a0adaf3
HV
1010 }
1011
1012 /* Start Passthrough Mode */
1013 ivtv_vapi(itv, CX2341X_ENC_START_CAPTURE, 2, 2, 11);
1014 atomic_inc(&itv->capturing);
1015 return 0;
1016 }
1017
1018 if (itv->output_mode != OUT_PASSTHROUGH)
1019 return 0;
1020
1021 /* Stop Passthrough Mode */
1022 ivtv_vapi(itv, CX2341X_ENC_STOP_CAPTURE, 3, 1, 2, 11);
1023 ivtv_vapi(itv, CX2341X_DEC_STOP_PLAYBACK, 3, 1, 0, 0);
1024
1025 atomic_dec(&itv->capturing);
1026 atomic_dec(&itv->decoding);
1027 clear_bit(IVTV_F_S_PASSTHROUGH, &dec_stream->s_flags);
1028 clear_bit(IVTV_F_S_STREAMING, &dec_stream->s_flags);
1029 itv->output_mode = OUT_NONE;
f7b80e69
HV
1030 if (atomic_read(&itv->capturing) == 0)
1031 cx2341x_handler_set_busy(&itv->cxhdl, 0);
1a0adaf3
HV
1032
1033 return 0;
1034}