xref: /linux/drivers/media/pci/ivtv/ivtv-streams.c (revision 44f57d78)
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"
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 
48 static 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 #ifdef CONFIG_COMPAT
55 	.compat_ioctl32 = video_ioctl2, /* for ivtv_default() */
56 #endif
57 	.release = ivtv_v4l2_close,
58 	.poll = ivtv_v4l2_enc_poll,
59 };
60 
61 static const struct v4l2_file_operations ivtv_v4l2_dec_fops = {
62 	.owner = THIS_MODULE,
63 	.read = ivtv_v4l2_read,
64 	.write = ivtv_v4l2_write,
65 	.open = ivtv_v4l2_open,
66 	.unlocked_ioctl = video_ioctl2,
67 #ifdef CONFIG_COMPAT
68 	.compat_ioctl32 = video_ioctl2, /* for ivtv_default() */
69 #endif
70 	.release = ivtv_v4l2_close,
71 	.poll = ivtv_v4l2_dec_poll,
72 };
73 
74 static const struct v4l2_file_operations ivtv_v4l2_radio_fops = {
75 	.owner = THIS_MODULE,
76 	.open = ivtv_v4l2_open,
77 	.unlocked_ioctl = video_ioctl2,
78 #ifdef CONFIG_COMPAT
79 	.compat_ioctl32 = video_ioctl2, /* for ivtv_default() */
80 #endif
81 	.release = ivtv_v4l2_close,
82 	.poll = ivtv_v4l2_enc_poll,
83 };
84 
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 
92 static struct {
93 	const char *name;
94 	int vfl_type;
95 	int num_offset;
96 	int dma, pio;
97 	u32 v4l2_caps;
98 	const struct v4l2_file_operations *fops;
99 } ivtv_stream_info[] = {
100 	{	/* IVTV_ENC_STREAM_TYPE_MPG */
101 		"encoder MPG",
102 		VFL_TYPE_GRABBER, 0,
103 		PCI_DMA_FROMDEVICE, 0,
104 		V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_TUNER |
105 			V4L2_CAP_AUDIO | V4L2_CAP_READWRITE,
106 		&ivtv_v4l2_enc_fops
107 	},
108 	{	/* IVTV_ENC_STREAM_TYPE_YUV */
109 		"encoder YUV",
110 		VFL_TYPE_GRABBER, IVTV_V4L2_ENC_YUV_OFFSET,
111 		PCI_DMA_FROMDEVICE, 0,
112 		V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_TUNER |
113 			V4L2_CAP_AUDIO | V4L2_CAP_READWRITE,
114 		&ivtv_v4l2_enc_fops
115 	},
116 	{	/* IVTV_ENC_STREAM_TYPE_VBI */
117 		"encoder VBI",
118 		VFL_TYPE_VBI, 0,
119 		PCI_DMA_FROMDEVICE, 0,
120 		V4L2_CAP_VBI_CAPTURE | V4L2_CAP_SLICED_VBI_CAPTURE | V4L2_CAP_TUNER |
121 			V4L2_CAP_AUDIO | V4L2_CAP_READWRITE,
122 		&ivtv_v4l2_enc_fops
123 	},
124 	{	/* IVTV_ENC_STREAM_TYPE_PCM */
125 		"encoder PCM",
126 		VFL_TYPE_GRABBER, IVTV_V4L2_ENC_PCM_OFFSET,
127 		PCI_DMA_FROMDEVICE, 0,
128 		V4L2_CAP_TUNER | V4L2_CAP_AUDIO | V4L2_CAP_READWRITE,
129 		&ivtv_v4l2_enc_fops
130 	},
131 	{	/* IVTV_ENC_STREAM_TYPE_RAD */
132 		"encoder radio",
133 		VFL_TYPE_RADIO, 0,
134 		PCI_DMA_NONE, 1,
135 		V4L2_CAP_RADIO | V4L2_CAP_TUNER,
136 		&ivtv_v4l2_radio_fops
137 	},
138 	{	/* IVTV_DEC_STREAM_TYPE_MPG */
139 		"decoder MPG",
140 		VFL_TYPE_GRABBER, IVTV_V4L2_DEC_MPG_OFFSET,
141 		PCI_DMA_TODEVICE, 0,
142 		V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_AUDIO | V4L2_CAP_READWRITE |
143 		V4L2_CAP_VIDEO_OUTPUT_OVERLAY,
144 		&ivtv_v4l2_dec_fops
145 	},
146 	{	/* IVTV_DEC_STREAM_TYPE_VBI */
147 		"decoder VBI",
148 		VFL_TYPE_VBI, IVTV_V4L2_DEC_VBI_OFFSET,
149 		PCI_DMA_NONE, 1,
150 		V4L2_CAP_SLICED_VBI_CAPTURE | V4L2_CAP_READWRITE,
151 		&ivtv_v4l2_enc_fops
152 	},
153 	{	/* IVTV_DEC_STREAM_TYPE_VOUT */
154 		"decoder VOUT",
155 		VFL_TYPE_VBI, IVTV_V4L2_DEC_VOUT_OFFSET,
156 		PCI_DMA_NONE, 1,
157 		V4L2_CAP_SLICED_VBI_OUTPUT | V4L2_CAP_AUDIO | V4L2_CAP_READWRITE,
158 		&ivtv_v4l2_dec_fops
159 	},
160 	{	/* IVTV_DEC_STREAM_TYPE_YUV */
161 		"decoder YUV",
162 		VFL_TYPE_GRABBER, IVTV_V4L2_DEC_YUV_OFFSET,
163 		PCI_DMA_TODEVICE, 0,
164 		V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_AUDIO | V4L2_CAP_READWRITE |
165 		V4L2_CAP_VIDEO_OUTPUT_OVERLAY,
166 		&ivtv_v4l2_dec_fops
167 	}
168 };
169 
170 static void ivtv_stream_init(struct ivtv *itv, int type)
171 {
172 	struct ivtv_stream *s = &itv->streams[type];
173 
174 	/* we need to keep vdev, so restore it afterwards */
175 	memset(s, 0, sizeof(*s));
176 
177 	/* initialize ivtv_stream fields */
178 	s->itv = itv;
179 	s->type = type;
180 	s->name = ivtv_stream_info[type].name;
181 	s->caps = ivtv_stream_info[type].v4l2_caps;
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)
189 		s->buffers = (itv->options.kilobytes[type] * 1024 + s->buf_size - 1) / s->buf_size;
190 	spin_lock_init(&s->qlock);
191 	init_waitqueue_head(&s->waitq);
192 	s->sg_handle = IVTV_DMA_UNMAPPED;
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 
200 static int ivtv_prep_dev(struct ivtv *itv, int type)
201 {
202 	struct ivtv_stream *s = &itv->streams[type];
203 	int num_offset = ivtv_stream_info[type].num_offset;
204 	int num = itv->instance + ivtv_first_minor + num_offset;
205 
206 	/* These four fields are always initialized. If vdev.v4l2_dev == NULL, then
207 	   this stream is not in use. In that case no other fields but these
208 	   four can be used. */
209 	s->vdev.v4l2_dev = NULL;
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 
220 	/* User explicitly selected 0 buffers for these streams, so don't
221 	   create them. */
222 	if (ivtv_stream_info[type].dma != PCI_DMA_NONE &&
223 	    itv->options.kilobytes[type] == 0) {
224 		IVTV_INFO("Disabled %s device\n", ivtv_stream_info[type].name);
225 		return 0;
226 	}
227 
228 	ivtv_stream_init(itv, type);
229 
230 	snprintf(s->vdev.name, sizeof(s->vdev.name), "%s %s",
231 			itv->v4l2_dev.name, s->name);
232 
233 	s->vdev.num = num;
234 	s->vdev.v4l2_dev = &itv->v4l2_dev;
235 	if (ivtv_stream_info[type].v4l2_caps &
236 			(V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_SLICED_VBI_OUTPUT))
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;
243 	if (s->type == IVTV_DEC_STREAM_TYPE_VBI) {
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);
255 	}
256 	ivtv_set_funcs(&s->vdev);
257 	return 0;
258 }
259 
260 /* Initialize v4l2 variables and prepare v4l2 devices */
261 int 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 
271 		if (itv->streams[type].vdev.v4l2_dev == NULL)
272 			continue;
273 
274 		/* Allocate Stream */
275 		if (ivtv_stream_alloc(&itv->streams[type]))
276 			break;
277 	}
278 	if (type == IVTV_MAX_STREAMS)
279 		return 0;
280 
281 	/* One or more streams could not be initialized. Clean 'em all up. */
282 	ivtv_streams_cleanup(itv);
283 	return -ENOMEM;
284 }
285 
286 static 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;
290 	const char *name;
291 	int num;
292 
293 	if (s->vdev.v4l2_dev == NULL)
294 		return 0;
295 
296 	num = s->vdev.num;
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 
301 		if (s_mpg->vdev.v4l2_dev)
302 			num = s_mpg->vdev.num + ivtv_stream_info[type].num_offset;
303 	}
304 	video_set_drvdata(&s->vdev, s);
305 
306 	/* Register device. First try the desired minor, then any free one. */
307 	if (video_register_device_no_warn(&s->vdev, vfl_type, num)) {
308 		IVTV_ERR("Couldn't register v4l2 device for %s (device node number %d)\n",
309 				s->name, num);
310 		return -ENOMEM;
311 	}
312 	name = video_device_node_name(&s->vdev);
313 
314 	switch (vfl_type) {
315 	case VFL_TYPE_GRABBER:
316 		IVTV_INFO("Registered device %s for %s (%d kB)\n",
317 			name, s->name, itv->options.kilobytes[type]);
318 		break;
319 	case VFL_TYPE_RADIO:
320 		IVTV_INFO("Registered device %s for %s\n",
321 			name, s->name);
322 		break;
323 	case VFL_TYPE_VBI:
324 		if (itv->options.kilobytes[type])
325 			IVTV_INFO("Registered device %s for %s (%d kB)\n",
326 				name, s->name, itv->options.kilobytes[type]);
327 		else
328 			IVTV_INFO("Registered device %s for %s\n",
329 				name, s->name);
330 		break;
331 	}
332 	return 0;
333 }
334 
335 /* Register v4l2 devices */
336 int ivtv_streams_register(struct ivtv *itv)
337 {
338 	int type;
339 	int err = 0;
340 
341 	/* Register V4L2 devices */
342 	for (type = 0; type < IVTV_MAX_STREAMS; type++)
343 		err |= ivtv_reg_dev(itv, type);
344 
345 	if (err == 0)
346 		return 0;
347 
348 	/* One or more streams could not be initialized. Clean 'em all up. */
349 	ivtv_streams_cleanup(itv);
350 	return -ENOMEM;
351 }
352 
353 /* Unregister v4l2 devices */
354 void ivtv_streams_cleanup(struct ivtv *itv)
355 {
356 	int type;
357 
358 	/* Teardown all streams */
359 	for (type = 0; type < IVTV_MAX_STREAMS; type++) {
360 		struct video_device *vdev = &itv->streams[type].vdev;
361 
362 		if (vdev->v4l2_dev == NULL)
363 			continue;
364 
365 		video_unregister_device(vdev);
366 		ivtv_stream_free(&itv->streams[type]);
367 		itv->streams[type].vdev.v4l2_dev = NULL;
368 	}
369 }
370 
371 static void ivtv_vbi_setup(struct ivtv *itv)
372 {
373 	int raw = ivtv_raw_vbi(itv);
374 	u32 data[CX2341X_MBOX_MAX_DATA];
375 	int lines;
376 	int i;
377 
378 	/* Reset VBI */
379 	ivtv_vapi(itv, CX2341X_ENC_SET_VBI_LINE, 5, 0xffff , 0, 0, 0, 0);
380 
381 	/* setup VBI registers */
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);
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) */
409 	data[2] = raw ? 4 : 4 * (itv->vbi.raw_size / itv->vbi.enc_size);
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 
457 	IVTV_DEBUG_INFO("Setup VBI start 0x%08x frames %d fpi %d\n",
458 		itv->vbi.enc_start, data[1], itv->vbi.fpi);
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 
484 int 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 
491 	if (s->vdev.v4l2_dev == NULL)
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;
508 		itv->dualwatch_stereo_mode = v4l2_ctrl_g_ctrl(itv->cxhdl.audio_mode);
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 
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) {
544 		int digitizer;
545 
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 
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);
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 */
596 		cx2341x_handler_setup(&itv->cxhdl);
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,
601 				1 | (v4l2_ctrl_g_ctrl(itv->cxhdl.video_mute_yuv) << 8));
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 
617 		cx2341x_handler_set_busy(&itv->cxhdl, 1);
618 
619 		/* Initialize Digitizer for Capture */
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 */
623 		v4l2_subdev_call(itv->sd_video, video, s_stream, 0);
624 		ivtv_msleep_timeout(300, 0);
625 		ivtv_vapi(itv, CX2341X_ENC_INITIALIZE_INPUT, 0);
626 		v4l2_subdev_call(itv->sd_video, video, s_stream, 1);
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 }
650 EXPORT_SYMBOL(ivtv_start_v4l2_encode_stream);
651 
652 static 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;
657 	u16 width;
658 	u16 height;
659 
660 	if (s->vdev.v4l2_dev == NULL)
661 		return -EINVAL;
662 
663 	IVTV_DEBUG_INFO("Setting some initial decoder settings\n");
664 
665 	width = itv->cxhdl.width;
666 	height = itv->cxhdl.height;
667 
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:
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 		}
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,
706 			width, height, itv->cxhdl.audio_properties)) {
707 		IVTV_DEBUG_WARN("Couldn't initialize decoder source\n");
708 	}
709 
710 	/* Decoder sometimes dies here, so wait a moment */
711 	ivtv_msleep_timeout(10, 0);
712 
713 	/* Known failure point for firmware, so check */
714 	return ivtv_firmware_check(itv, "ivtv_setup_v4l2_decode_stream");
715 }
716 
717 int ivtv_start_v4l2_decode_stream(struct ivtv_stream *s, int gop_offset)
718 {
719 	struct ivtv *itv = s->itv;
720 	int rc;
721 
722 	if (s->vdev.v4l2_dev == NULL)
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 
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 	}
735 
736 	/* set dma size to 65536 bytes */
737 	ivtv_vapi(itv, CX2341X_DEC_SET_DMA_BLOCK_SIZE, 1, 65536);
738 
739 	/* Clear Streamoff */
740 	clear_bit(IVTV_F_S_STREAMOFF, &s->s_flags);
741 
742 	/* Zero out decoder counters */
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]);
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 
758 	/* Let things settle before we actually start */
759 	ivtv_msleep_timeout(10, 0);
760 
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 
770 void 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 
777 		if (s->vdev.v4l2_dev == NULL)
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 
785 int 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;
790 	int stopmode;
791 
792 	if (s->vdev.v4l2_dev == NULL)
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 
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;
836 			unsigned long then = jiffies;
837 
838 			add_wait_queue(&itv->eos_waitq, &wait);
839 
840 			set_current_state(TASK_INTERRUPTIBLE);
841 
842 			/* wait 2s for EOS interrupt */
843 			while (!test_bit(IVTV_F_I_EOS, &itv->i_flags) &&
844 				time_before(jiffies,
845 					    then + msecs_to_jiffies(2000))) {
846 				schedule_timeout(msecs_to_jiffies(10));
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);
865 			remove_wait_queue(&itv->eos_waitq, &wait);
866 			set_bit(IVTV_F_S_STREAMOFF, &s->s_flags);
867 		}
868 
869 		/* Handle any pending interrupts */
870 		ivtv_msleep_timeout(100, 0);
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 
885 	cx2341x_handler_set_busy(&itv->cxhdl, 0);
886 
887 	/* Set the following Interrupt mask bits for capture */
888 	ivtv_set_irq_mask(itv, IVTV_IRQ_MASK_CAPTURE);
889 	del_timer(&itv->dma_timer);
890 
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 
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 
903 	wake_up(&s->waitq);
904 
905 	return 0;
906 }
907 EXPORT_SYMBOL(ivtv_stop_v4l2_encode_stream);
908 
909 int ivtv_stop_v4l2_decode_stream(struct ivtv_stream *s, int flags, u64 pts)
910 {
911 	static const struct v4l2_event ev = {
912 		.type = V4L2_EVENT_EOS,
913 	};
914 	struct ivtv *itv = s->itv;
915 
916 	if (s->vdev.v4l2_dev == NULL)
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 
925 	IVTV_DEBUG_INFO("Stop Decode at %llu, flags: %x\n", (unsigned long long)pts, flags);
926 
927 	/* Stop Decoder */
928 	if (!(flags & V4L2_DEC_CMD_STOP_IMMEDIATELY) || pts) {
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 			}
944 			if (ivtv_msleep_timeout(100, 1))
945 				break;
946 		}
947 	}
948 	ivtv_vapi(itv, CX2341X_DEC_STOP_PLAYBACK, 3, flags & V4L2_DEC_CMD_STOP_TO_BLACK, 0, 0);
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);
954 	del_timer(&itv->dma_timer);
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 
960 	/* decoder needs time to settle */
961 	ivtv_msleep_timeout(40, 0);
962 
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);
968 	v4l2_event_queue(&s->vdev, &ev);
969 
970 	/* wake up wait queues */
971 	wake_up(&s->waitq);
972 
973 	return 0;
974 }
975 
976 int 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 
981 	if (yuv_stream->vdev.v4l2_dev == NULL || dec_stream->vdev.v4l2_dev == NULL)
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) {
1008 			cx2341x_handler_setup(&itv->cxhdl);
1009 			cx2341x_handler_set_busy(&itv->cxhdl, 1);
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;
1030 	if (atomic_read(&itv->capturing) == 0)
1031 		cx2341x_handler_set_busy(&itv->cxhdl, 0);
1032 
1033 	return 0;
1034 }
1035