18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci    init/start/stop/exit stream functions
38c2ecf20Sopenharmony_ci    Copyright (C) 2003-2004  Kevin Thayer <nufan_wfk at yahoo.com>
48c2ecf20Sopenharmony_ci    Copyright (C) 2004  Chris Kennedy <c@groovy.org>
58c2ecf20Sopenharmony_ci    Copyright (C) 2005-2007  Hans Verkuil <hverkuil@xs4all.nl>
68c2ecf20Sopenharmony_ci
78c2ecf20Sopenharmony_ci    This program is free software; you can redistribute it and/or modify
88c2ecf20Sopenharmony_ci    it under the terms of the GNU General Public License as published by
98c2ecf20Sopenharmony_ci    the Free Software Foundation; either version 2 of the License, or
108c2ecf20Sopenharmony_ci    (at your option) any later version.
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_ci    This program is distributed in the hope that it will be useful,
138c2ecf20Sopenharmony_ci    but WITHOUT ANY WARRANTY; without even the implied warranty of
148c2ecf20Sopenharmony_ci    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
158c2ecf20Sopenharmony_ci    GNU General Public License for more details.
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_ci    You should have received a copy of the GNU General Public License
188c2ecf20Sopenharmony_ci    along with this program; if not, write to the Free Software
198c2ecf20Sopenharmony_ci    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
208c2ecf20Sopenharmony_ci */
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_ci/* License: GPL
238c2ecf20Sopenharmony_ci * Author: Kevin Thayer <nufan_wfk at yahoo dot com>
248c2ecf20Sopenharmony_ci *
258c2ecf20Sopenharmony_ci * This file will hold API related functions, both internal (firmware api)
268c2ecf20Sopenharmony_ci * and external (v4l2, etc)
278c2ecf20Sopenharmony_ci *
288c2ecf20Sopenharmony_ci * -----
298c2ecf20Sopenharmony_ci * MPG600/MPG160 support by  T.Adachi <tadachi@tadachi-net.com>
308c2ecf20Sopenharmony_ci *                      and Takeru KOMORIYA<komoriya@paken.org>
318c2ecf20Sopenharmony_ci *
328c2ecf20Sopenharmony_ci * AVerMedia M179 GPIO info by Chris Pinkham <cpinkham@bc2va.org>
338c2ecf20Sopenharmony_ci *                using information provided by Jiun-Kuei Jung @ AVerMedia.
348c2ecf20Sopenharmony_ci */
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_ci#include "ivtv-driver.h"
378c2ecf20Sopenharmony_ci#include "ivtv-fileops.h"
388c2ecf20Sopenharmony_ci#include "ivtv-queue.h"
398c2ecf20Sopenharmony_ci#include "ivtv-mailbox.h"
408c2ecf20Sopenharmony_ci#include "ivtv-ioctl.h"
418c2ecf20Sopenharmony_ci#include "ivtv-irq.h"
428c2ecf20Sopenharmony_ci#include "ivtv-yuv.h"
438c2ecf20Sopenharmony_ci#include "ivtv-cards.h"
448c2ecf20Sopenharmony_ci#include "ivtv-streams.h"
458c2ecf20Sopenharmony_ci#include "ivtv-firmware.h"
468c2ecf20Sopenharmony_ci#include <media/v4l2-event.h>
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_cistatic const struct v4l2_file_operations ivtv_v4l2_enc_fops = {
498c2ecf20Sopenharmony_ci	.owner = THIS_MODULE,
508c2ecf20Sopenharmony_ci	.read = ivtv_v4l2_read,
518c2ecf20Sopenharmony_ci	.write = ivtv_v4l2_write,
528c2ecf20Sopenharmony_ci	.open = ivtv_v4l2_open,
538c2ecf20Sopenharmony_ci	.unlocked_ioctl = video_ioctl2,
548c2ecf20Sopenharmony_ci#ifdef CONFIG_COMPAT
558c2ecf20Sopenharmony_ci	.compat_ioctl32 = video_ioctl2, /* for ivtv_default() */
568c2ecf20Sopenharmony_ci#endif
578c2ecf20Sopenharmony_ci	.release = ivtv_v4l2_close,
588c2ecf20Sopenharmony_ci	.poll = ivtv_v4l2_enc_poll,
598c2ecf20Sopenharmony_ci};
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_cistatic const struct v4l2_file_operations ivtv_v4l2_dec_fops = {
628c2ecf20Sopenharmony_ci	.owner = THIS_MODULE,
638c2ecf20Sopenharmony_ci	.read = ivtv_v4l2_read,
648c2ecf20Sopenharmony_ci	.write = ivtv_v4l2_write,
658c2ecf20Sopenharmony_ci	.open = ivtv_v4l2_open,
668c2ecf20Sopenharmony_ci	.unlocked_ioctl = video_ioctl2,
678c2ecf20Sopenharmony_ci#ifdef CONFIG_COMPAT
688c2ecf20Sopenharmony_ci	.compat_ioctl32 = video_ioctl2, /* for ivtv_default() */
698c2ecf20Sopenharmony_ci#endif
708c2ecf20Sopenharmony_ci	.release = ivtv_v4l2_close,
718c2ecf20Sopenharmony_ci	.poll = ivtv_v4l2_dec_poll,
728c2ecf20Sopenharmony_ci};
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_cistatic const struct v4l2_file_operations ivtv_v4l2_radio_fops = {
758c2ecf20Sopenharmony_ci	.owner = THIS_MODULE,
768c2ecf20Sopenharmony_ci	.open = ivtv_v4l2_open,
778c2ecf20Sopenharmony_ci	.unlocked_ioctl = video_ioctl2,
788c2ecf20Sopenharmony_ci#ifdef CONFIG_COMPAT
798c2ecf20Sopenharmony_ci	.compat_ioctl32 = video_ioctl2, /* for ivtv_default() */
808c2ecf20Sopenharmony_ci#endif
818c2ecf20Sopenharmony_ci	.release = ivtv_v4l2_close,
828c2ecf20Sopenharmony_ci	.poll = ivtv_v4l2_enc_poll,
838c2ecf20Sopenharmony_ci};
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_ci#define IVTV_V4L2_DEC_MPG_OFFSET  16	/* offset from 0 to register decoder mpg v4l2 minors on */
868c2ecf20Sopenharmony_ci#define IVTV_V4L2_ENC_PCM_OFFSET  24	/* offset from 0 to register pcm v4l2 minors on */
878c2ecf20Sopenharmony_ci#define IVTV_V4L2_ENC_YUV_OFFSET  32	/* offset from 0 to register yuv v4l2 minors on */
888c2ecf20Sopenharmony_ci#define IVTV_V4L2_DEC_YUV_OFFSET  48	/* offset from 0 to register decoder yuv v4l2 minors on */
898c2ecf20Sopenharmony_ci#define IVTV_V4L2_DEC_VBI_OFFSET   8	/* offset from 0 to register decoder vbi input v4l2 minors on */
908c2ecf20Sopenharmony_ci#define IVTV_V4L2_DEC_VOUT_OFFSET 16	/* offset from 0 to register vbi output v4l2 minors on */
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_cistatic struct {
938c2ecf20Sopenharmony_ci	const char *name;
948c2ecf20Sopenharmony_ci	int vfl_type;
958c2ecf20Sopenharmony_ci	int num_offset;
968c2ecf20Sopenharmony_ci	int dma, pio;
978c2ecf20Sopenharmony_ci	u32 v4l2_caps;
988c2ecf20Sopenharmony_ci	const struct v4l2_file_operations *fops;
998c2ecf20Sopenharmony_ci} ivtv_stream_info[] = {
1008c2ecf20Sopenharmony_ci	{	/* IVTV_ENC_STREAM_TYPE_MPG */
1018c2ecf20Sopenharmony_ci		"encoder MPG",
1028c2ecf20Sopenharmony_ci		VFL_TYPE_VIDEO, 0,
1038c2ecf20Sopenharmony_ci		PCI_DMA_FROMDEVICE, 0,
1048c2ecf20Sopenharmony_ci		V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_TUNER |
1058c2ecf20Sopenharmony_ci			V4L2_CAP_AUDIO | V4L2_CAP_READWRITE,
1068c2ecf20Sopenharmony_ci		&ivtv_v4l2_enc_fops
1078c2ecf20Sopenharmony_ci	},
1088c2ecf20Sopenharmony_ci	{	/* IVTV_ENC_STREAM_TYPE_YUV */
1098c2ecf20Sopenharmony_ci		"encoder YUV",
1108c2ecf20Sopenharmony_ci		VFL_TYPE_VIDEO, IVTV_V4L2_ENC_YUV_OFFSET,
1118c2ecf20Sopenharmony_ci		PCI_DMA_FROMDEVICE, 0,
1128c2ecf20Sopenharmony_ci		V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_TUNER |
1138c2ecf20Sopenharmony_ci			V4L2_CAP_AUDIO | V4L2_CAP_READWRITE,
1148c2ecf20Sopenharmony_ci		&ivtv_v4l2_enc_fops
1158c2ecf20Sopenharmony_ci	},
1168c2ecf20Sopenharmony_ci	{	/* IVTV_ENC_STREAM_TYPE_VBI */
1178c2ecf20Sopenharmony_ci		"encoder VBI",
1188c2ecf20Sopenharmony_ci		VFL_TYPE_VBI, 0,
1198c2ecf20Sopenharmony_ci		PCI_DMA_FROMDEVICE, 0,
1208c2ecf20Sopenharmony_ci		V4L2_CAP_VBI_CAPTURE | V4L2_CAP_SLICED_VBI_CAPTURE | V4L2_CAP_TUNER |
1218c2ecf20Sopenharmony_ci			V4L2_CAP_AUDIO | V4L2_CAP_READWRITE,
1228c2ecf20Sopenharmony_ci		&ivtv_v4l2_enc_fops
1238c2ecf20Sopenharmony_ci	},
1248c2ecf20Sopenharmony_ci	{	/* IVTV_ENC_STREAM_TYPE_PCM */
1258c2ecf20Sopenharmony_ci		"encoder PCM",
1268c2ecf20Sopenharmony_ci		VFL_TYPE_VIDEO, IVTV_V4L2_ENC_PCM_OFFSET,
1278c2ecf20Sopenharmony_ci		PCI_DMA_FROMDEVICE, 0,
1288c2ecf20Sopenharmony_ci		V4L2_CAP_TUNER | V4L2_CAP_AUDIO | V4L2_CAP_READWRITE,
1298c2ecf20Sopenharmony_ci		&ivtv_v4l2_enc_fops
1308c2ecf20Sopenharmony_ci	},
1318c2ecf20Sopenharmony_ci	{	/* IVTV_ENC_STREAM_TYPE_RAD */
1328c2ecf20Sopenharmony_ci		"encoder radio",
1338c2ecf20Sopenharmony_ci		VFL_TYPE_RADIO, 0,
1348c2ecf20Sopenharmony_ci		PCI_DMA_NONE, 1,
1358c2ecf20Sopenharmony_ci		V4L2_CAP_RADIO | V4L2_CAP_TUNER,
1368c2ecf20Sopenharmony_ci		&ivtv_v4l2_radio_fops
1378c2ecf20Sopenharmony_ci	},
1388c2ecf20Sopenharmony_ci	{	/* IVTV_DEC_STREAM_TYPE_MPG */
1398c2ecf20Sopenharmony_ci		"decoder MPG",
1408c2ecf20Sopenharmony_ci		VFL_TYPE_VIDEO, IVTV_V4L2_DEC_MPG_OFFSET,
1418c2ecf20Sopenharmony_ci		PCI_DMA_TODEVICE, 0,
1428c2ecf20Sopenharmony_ci		V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_AUDIO | V4L2_CAP_READWRITE,
1438c2ecf20Sopenharmony_ci		&ivtv_v4l2_dec_fops
1448c2ecf20Sopenharmony_ci	},
1458c2ecf20Sopenharmony_ci	{	/* IVTV_DEC_STREAM_TYPE_VBI */
1468c2ecf20Sopenharmony_ci		"decoder VBI",
1478c2ecf20Sopenharmony_ci		VFL_TYPE_VBI, IVTV_V4L2_DEC_VBI_OFFSET,
1488c2ecf20Sopenharmony_ci		PCI_DMA_NONE, 1,
1498c2ecf20Sopenharmony_ci		V4L2_CAP_SLICED_VBI_CAPTURE | V4L2_CAP_READWRITE,
1508c2ecf20Sopenharmony_ci		&ivtv_v4l2_enc_fops
1518c2ecf20Sopenharmony_ci	},
1528c2ecf20Sopenharmony_ci	{	/* IVTV_DEC_STREAM_TYPE_VOUT */
1538c2ecf20Sopenharmony_ci		"decoder VOUT",
1548c2ecf20Sopenharmony_ci		VFL_TYPE_VBI, IVTV_V4L2_DEC_VOUT_OFFSET,
1558c2ecf20Sopenharmony_ci		PCI_DMA_NONE, 1,
1568c2ecf20Sopenharmony_ci		V4L2_CAP_SLICED_VBI_OUTPUT | V4L2_CAP_AUDIO | V4L2_CAP_READWRITE,
1578c2ecf20Sopenharmony_ci		&ivtv_v4l2_dec_fops
1588c2ecf20Sopenharmony_ci	},
1598c2ecf20Sopenharmony_ci	{	/* IVTV_DEC_STREAM_TYPE_YUV */
1608c2ecf20Sopenharmony_ci		"decoder YUV",
1618c2ecf20Sopenharmony_ci		VFL_TYPE_VIDEO, IVTV_V4L2_DEC_YUV_OFFSET,
1628c2ecf20Sopenharmony_ci		PCI_DMA_TODEVICE, 0,
1638c2ecf20Sopenharmony_ci		V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_AUDIO | V4L2_CAP_READWRITE,
1648c2ecf20Sopenharmony_ci		&ivtv_v4l2_dec_fops
1658c2ecf20Sopenharmony_ci	}
1668c2ecf20Sopenharmony_ci};
1678c2ecf20Sopenharmony_ci
1688c2ecf20Sopenharmony_cistatic void ivtv_stream_init(struct ivtv *itv, int type)
1698c2ecf20Sopenharmony_ci{
1708c2ecf20Sopenharmony_ci	struct ivtv_stream *s = &itv->streams[type];
1718c2ecf20Sopenharmony_ci
1728c2ecf20Sopenharmony_ci	/* we need to keep vdev, so restore it afterwards */
1738c2ecf20Sopenharmony_ci	memset(s, 0, sizeof(*s));
1748c2ecf20Sopenharmony_ci
1758c2ecf20Sopenharmony_ci	/* initialize ivtv_stream fields */
1768c2ecf20Sopenharmony_ci	s->itv = itv;
1778c2ecf20Sopenharmony_ci	s->type = type;
1788c2ecf20Sopenharmony_ci	s->name = ivtv_stream_info[type].name;
1798c2ecf20Sopenharmony_ci	s->vdev.device_caps = ivtv_stream_info[type].v4l2_caps;
1808c2ecf20Sopenharmony_ci
1818c2ecf20Sopenharmony_ci	if (ivtv_stream_info[type].pio)
1828c2ecf20Sopenharmony_ci		s->dma = PCI_DMA_NONE;
1838c2ecf20Sopenharmony_ci	else
1848c2ecf20Sopenharmony_ci		s->dma = ivtv_stream_info[type].dma;
1858c2ecf20Sopenharmony_ci	s->buf_size = itv->stream_buf_size[type];
1868c2ecf20Sopenharmony_ci	if (s->buf_size)
1878c2ecf20Sopenharmony_ci		s->buffers = (itv->options.kilobytes[type] * 1024 + s->buf_size - 1) / s->buf_size;
1888c2ecf20Sopenharmony_ci	spin_lock_init(&s->qlock);
1898c2ecf20Sopenharmony_ci	init_waitqueue_head(&s->waitq);
1908c2ecf20Sopenharmony_ci	s->sg_handle = IVTV_DMA_UNMAPPED;
1918c2ecf20Sopenharmony_ci	ivtv_queue_init(&s->q_free);
1928c2ecf20Sopenharmony_ci	ivtv_queue_init(&s->q_full);
1938c2ecf20Sopenharmony_ci	ivtv_queue_init(&s->q_dma);
1948c2ecf20Sopenharmony_ci	ivtv_queue_init(&s->q_predma);
1958c2ecf20Sopenharmony_ci	ivtv_queue_init(&s->q_io);
1968c2ecf20Sopenharmony_ci}
1978c2ecf20Sopenharmony_ci
1988c2ecf20Sopenharmony_cistatic int ivtv_prep_dev(struct ivtv *itv, int type)
1998c2ecf20Sopenharmony_ci{
2008c2ecf20Sopenharmony_ci	struct ivtv_stream *s = &itv->streams[type];
2018c2ecf20Sopenharmony_ci	int num_offset = ivtv_stream_info[type].num_offset;
2028c2ecf20Sopenharmony_ci	int num = itv->instance + ivtv_first_minor + num_offset;
2038c2ecf20Sopenharmony_ci
2048c2ecf20Sopenharmony_ci	/* These four fields are always initialized. If vdev.v4l2_dev == NULL, then
2058c2ecf20Sopenharmony_ci	   this stream is not in use. In that case no other fields but these
2068c2ecf20Sopenharmony_ci	   four can be used. */
2078c2ecf20Sopenharmony_ci	s->vdev.v4l2_dev = NULL;
2088c2ecf20Sopenharmony_ci	s->itv = itv;
2098c2ecf20Sopenharmony_ci	s->type = type;
2108c2ecf20Sopenharmony_ci	s->name = ivtv_stream_info[type].name;
2118c2ecf20Sopenharmony_ci
2128c2ecf20Sopenharmony_ci	/* Check whether the radio is supported */
2138c2ecf20Sopenharmony_ci	if (type == IVTV_ENC_STREAM_TYPE_RAD && !(itv->v4l2_cap & V4L2_CAP_RADIO))
2148c2ecf20Sopenharmony_ci		return 0;
2158c2ecf20Sopenharmony_ci	if (type >= IVTV_DEC_STREAM_TYPE_MPG && !(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
2168c2ecf20Sopenharmony_ci		return 0;
2178c2ecf20Sopenharmony_ci
2188c2ecf20Sopenharmony_ci	/* User explicitly selected 0 buffers for these streams, so don't
2198c2ecf20Sopenharmony_ci	   create them. */
2208c2ecf20Sopenharmony_ci	if (ivtv_stream_info[type].dma != PCI_DMA_NONE &&
2218c2ecf20Sopenharmony_ci	    itv->options.kilobytes[type] == 0) {
2228c2ecf20Sopenharmony_ci		IVTV_INFO("Disabled %s device\n", ivtv_stream_info[type].name);
2238c2ecf20Sopenharmony_ci		return 0;
2248c2ecf20Sopenharmony_ci	}
2258c2ecf20Sopenharmony_ci
2268c2ecf20Sopenharmony_ci	ivtv_stream_init(itv, type);
2278c2ecf20Sopenharmony_ci
2288c2ecf20Sopenharmony_ci	snprintf(s->vdev.name, sizeof(s->vdev.name), "%s %s",
2298c2ecf20Sopenharmony_ci			itv->v4l2_dev.name, s->name);
2308c2ecf20Sopenharmony_ci
2318c2ecf20Sopenharmony_ci	s->vdev.num = num;
2328c2ecf20Sopenharmony_ci	s->vdev.v4l2_dev = &itv->v4l2_dev;
2338c2ecf20Sopenharmony_ci	if (ivtv_stream_info[type].v4l2_caps &
2348c2ecf20Sopenharmony_ci			(V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_SLICED_VBI_OUTPUT))
2358c2ecf20Sopenharmony_ci		s->vdev.vfl_dir = VFL_DIR_TX;
2368c2ecf20Sopenharmony_ci	s->vdev.fops = ivtv_stream_info[type].fops;
2378c2ecf20Sopenharmony_ci	s->vdev.ctrl_handler = itv->v4l2_dev.ctrl_handler;
2388c2ecf20Sopenharmony_ci	s->vdev.release = video_device_release_empty;
2398c2ecf20Sopenharmony_ci	s->vdev.tvnorms = V4L2_STD_ALL;
2408c2ecf20Sopenharmony_ci	s->vdev.lock = &itv->serialize_lock;
2418c2ecf20Sopenharmony_ci	if (s->type == IVTV_DEC_STREAM_TYPE_VBI) {
2428c2ecf20Sopenharmony_ci		v4l2_disable_ioctl(&s->vdev, VIDIOC_S_AUDIO);
2438c2ecf20Sopenharmony_ci		v4l2_disable_ioctl(&s->vdev, VIDIOC_G_AUDIO);
2448c2ecf20Sopenharmony_ci		v4l2_disable_ioctl(&s->vdev, VIDIOC_ENUMAUDIO);
2458c2ecf20Sopenharmony_ci		v4l2_disable_ioctl(&s->vdev, VIDIOC_ENUMINPUT);
2468c2ecf20Sopenharmony_ci		v4l2_disable_ioctl(&s->vdev, VIDIOC_S_INPUT);
2478c2ecf20Sopenharmony_ci		v4l2_disable_ioctl(&s->vdev, VIDIOC_G_INPUT);
2488c2ecf20Sopenharmony_ci		v4l2_disable_ioctl(&s->vdev, VIDIOC_S_FREQUENCY);
2498c2ecf20Sopenharmony_ci		v4l2_disable_ioctl(&s->vdev, VIDIOC_G_FREQUENCY);
2508c2ecf20Sopenharmony_ci		v4l2_disable_ioctl(&s->vdev, VIDIOC_S_TUNER);
2518c2ecf20Sopenharmony_ci		v4l2_disable_ioctl(&s->vdev, VIDIOC_G_TUNER);
2528c2ecf20Sopenharmony_ci		v4l2_disable_ioctl(&s->vdev, VIDIOC_S_STD);
2538c2ecf20Sopenharmony_ci	}
2548c2ecf20Sopenharmony_ci	ivtv_set_funcs(&s->vdev);
2558c2ecf20Sopenharmony_ci	return 0;
2568c2ecf20Sopenharmony_ci}
2578c2ecf20Sopenharmony_ci
2588c2ecf20Sopenharmony_ci/* Initialize v4l2 variables and prepare v4l2 devices */
2598c2ecf20Sopenharmony_ciint ivtv_streams_setup(struct ivtv *itv)
2608c2ecf20Sopenharmony_ci{
2618c2ecf20Sopenharmony_ci	int type;
2628c2ecf20Sopenharmony_ci
2638c2ecf20Sopenharmony_ci	/* Setup V4L2 Devices */
2648c2ecf20Sopenharmony_ci	for (type = 0; type < IVTV_MAX_STREAMS; type++) {
2658c2ecf20Sopenharmony_ci		/* Prepare device */
2668c2ecf20Sopenharmony_ci		if (ivtv_prep_dev(itv, type))
2678c2ecf20Sopenharmony_ci			break;
2688c2ecf20Sopenharmony_ci
2698c2ecf20Sopenharmony_ci		if (itv->streams[type].vdev.v4l2_dev == NULL)
2708c2ecf20Sopenharmony_ci			continue;
2718c2ecf20Sopenharmony_ci
2728c2ecf20Sopenharmony_ci		/* Allocate Stream */
2738c2ecf20Sopenharmony_ci		if (ivtv_stream_alloc(&itv->streams[type]))
2748c2ecf20Sopenharmony_ci			break;
2758c2ecf20Sopenharmony_ci	}
2768c2ecf20Sopenharmony_ci	if (type == IVTV_MAX_STREAMS)
2778c2ecf20Sopenharmony_ci		return 0;
2788c2ecf20Sopenharmony_ci
2798c2ecf20Sopenharmony_ci	/* One or more streams could not be initialized. Clean 'em all up. */
2808c2ecf20Sopenharmony_ci	ivtv_streams_cleanup(itv);
2818c2ecf20Sopenharmony_ci	return -ENOMEM;
2828c2ecf20Sopenharmony_ci}
2838c2ecf20Sopenharmony_ci
2848c2ecf20Sopenharmony_cistatic int ivtv_reg_dev(struct ivtv *itv, int type)
2858c2ecf20Sopenharmony_ci{
2868c2ecf20Sopenharmony_ci	struct ivtv_stream *s = &itv->streams[type];
2878c2ecf20Sopenharmony_ci	int vfl_type = ivtv_stream_info[type].vfl_type;
2888c2ecf20Sopenharmony_ci	const char *name;
2898c2ecf20Sopenharmony_ci	int num;
2908c2ecf20Sopenharmony_ci
2918c2ecf20Sopenharmony_ci	if (s->vdev.v4l2_dev == NULL)
2928c2ecf20Sopenharmony_ci		return 0;
2938c2ecf20Sopenharmony_ci
2948c2ecf20Sopenharmony_ci	num = s->vdev.num;
2958c2ecf20Sopenharmony_ci	/* card number + user defined offset + device offset */
2968c2ecf20Sopenharmony_ci	if (type != IVTV_ENC_STREAM_TYPE_MPG) {
2978c2ecf20Sopenharmony_ci		struct ivtv_stream *s_mpg = &itv->streams[IVTV_ENC_STREAM_TYPE_MPG];
2988c2ecf20Sopenharmony_ci
2998c2ecf20Sopenharmony_ci		if (s_mpg->vdev.v4l2_dev)
3008c2ecf20Sopenharmony_ci			num = s_mpg->vdev.num + ivtv_stream_info[type].num_offset;
3018c2ecf20Sopenharmony_ci	}
3028c2ecf20Sopenharmony_ci	if (itv->osd_video_pbase && (type == IVTV_DEC_STREAM_TYPE_YUV ||
3038c2ecf20Sopenharmony_ci				     type == IVTV_DEC_STREAM_TYPE_MPG)) {
3048c2ecf20Sopenharmony_ci		s->vdev.device_caps |= V4L2_CAP_VIDEO_OUTPUT_OVERLAY;
3058c2ecf20Sopenharmony_ci		itv->v4l2_cap |= V4L2_CAP_VIDEO_OUTPUT_OVERLAY;
3068c2ecf20Sopenharmony_ci	}
3078c2ecf20Sopenharmony_ci	video_set_drvdata(&s->vdev, s);
3088c2ecf20Sopenharmony_ci
3098c2ecf20Sopenharmony_ci	/* Register device. First try the desired minor, then any free one. */
3108c2ecf20Sopenharmony_ci	if (video_register_device_no_warn(&s->vdev, vfl_type, num)) {
3118c2ecf20Sopenharmony_ci		IVTV_ERR("Couldn't register v4l2 device for %s (device node number %d)\n",
3128c2ecf20Sopenharmony_ci				s->name, num);
3138c2ecf20Sopenharmony_ci		return -ENOMEM;
3148c2ecf20Sopenharmony_ci	}
3158c2ecf20Sopenharmony_ci	name = video_device_node_name(&s->vdev);
3168c2ecf20Sopenharmony_ci
3178c2ecf20Sopenharmony_ci	switch (vfl_type) {
3188c2ecf20Sopenharmony_ci	case VFL_TYPE_VIDEO:
3198c2ecf20Sopenharmony_ci		IVTV_INFO("Registered device %s for %s (%d kB)\n",
3208c2ecf20Sopenharmony_ci			name, s->name, itv->options.kilobytes[type]);
3218c2ecf20Sopenharmony_ci		break;
3228c2ecf20Sopenharmony_ci	case VFL_TYPE_RADIO:
3238c2ecf20Sopenharmony_ci		IVTV_INFO("Registered device %s for %s\n",
3248c2ecf20Sopenharmony_ci			name, s->name);
3258c2ecf20Sopenharmony_ci		break;
3268c2ecf20Sopenharmony_ci	case VFL_TYPE_VBI:
3278c2ecf20Sopenharmony_ci		if (itv->options.kilobytes[type])
3288c2ecf20Sopenharmony_ci			IVTV_INFO("Registered device %s for %s (%d kB)\n",
3298c2ecf20Sopenharmony_ci				name, s->name, itv->options.kilobytes[type]);
3308c2ecf20Sopenharmony_ci		else
3318c2ecf20Sopenharmony_ci			IVTV_INFO("Registered device %s for %s\n",
3328c2ecf20Sopenharmony_ci				name, s->name);
3338c2ecf20Sopenharmony_ci		break;
3348c2ecf20Sopenharmony_ci	}
3358c2ecf20Sopenharmony_ci	return 0;
3368c2ecf20Sopenharmony_ci}
3378c2ecf20Sopenharmony_ci
3388c2ecf20Sopenharmony_ci/* Register v4l2 devices */
3398c2ecf20Sopenharmony_ciint ivtv_streams_register(struct ivtv *itv)
3408c2ecf20Sopenharmony_ci{
3418c2ecf20Sopenharmony_ci	int type;
3428c2ecf20Sopenharmony_ci	int err = 0;
3438c2ecf20Sopenharmony_ci
3448c2ecf20Sopenharmony_ci	/* Register V4L2 devices */
3458c2ecf20Sopenharmony_ci	for (type = 0; type < IVTV_MAX_STREAMS; type++)
3468c2ecf20Sopenharmony_ci		err |= ivtv_reg_dev(itv, type);
3478c2ecf20Sopenharmony_ci
3488c2ecf20Sopenharmony_ci	if (err == 0)
3498c2ecf20Sopenharmony_ci		return 0;
3508c2ecf20Sopenharmony_ci
3518c2ecf20Sopenharmony_ci	/* One or more streams could not be initialized. Clean 'em all up. */
3528c2ecf20Sopenharmony_ci	ivtv_streams_cleanup(itv);
3538c2ecf20Sopenharmony_ci	return -ENOMEM;
3548c2ecf20Sopenharmony_ci}
3558c2ecf20Sopenharmony_ci
3568c2ecf20Sopenharmony_ci/* Unregister v4l2 devices */
3578c2ecf20Sopenharmony_civoid ivtv_streams_cleanup(struct ivtv *itv)
3588c2ecf20Sopenharmony_ci{
3598c2ecf20Sopenharmony_ci	int type;
3608c2ecf20Sopenharmony_ci
3618c2ecf20Sopenharmony_ci	/* Teardown all streams */
3628c2ecf20Sopenharmony_ci	for (type = 0; type < IVTV_MAX_STREAMS; type++) {
3638c2ecf20Sopenharmony_ci		struct video_device *vdev = &itv->streams[type].vdev;
3648c2ecf20Sopenharmony_ci
3658c2ecf20Sopenharmony_ci		if (vdev->v4l2_dev == NULL)
3668c2ecf20Sopenharmony_ci			continue;
3678c2ecf20Sopenharmony_ci
3688c2ecf20Sopenharmony_ci		video_unregister_device(vdev);
3698c2ecf20Sopenharmony_ci		ivtv_stream_free(&itv->streams[type]);
3708c2ecf20Sopenharmony_ci		itv->streams[type].vdev.v4l2_dev = NULL;
3718c2ecf20Sopenharmony_ci	}
3728c2ecf20Sopenharmony_ci}
3738c2ecf20Sopenharmony_ci
3748c2ecf20Sopenharmony_cistatic void ivtv_vbi_setup(struct ivtv *itv)
3758c2ecf20Sopenharmony_ci{
3768c2ecf20Sopenharmony_ci	int raw = ivtv_raw_vbi(itv);
3778c2ecf20Sopenharmony_ci	u32 data[CX2341X_MBOX_MAX_DATA];
3788c2ecf20Sopenharmony_ci	int lines;
3798c2ecf20Sopenharmony_ci	int i;
3808c2ecf20Sopenharmony_ci
3818c2ecf20Sopenharmony_ci	/* Reset VBI */
3828c2ecf20Sopenharmony_ci	ivtv_vapi(itv, CX2341X_ENC_SET_VBI_LINE, 5, 0xffff , 0, 0, 0, 0);
3838c2ecf20Sopenharmony_ci
3848c2ecf20Sopenharmony_ci	/* setup VBI registers */
3858c2ecf20Sopenharmony_ci	if (raw)
3868c2ecf20Sopenharmony_ci		v4l2_subdev_call(itv->sd_video, vbi, s_raw_fmt, &itv->vbi.in.fmt.vbi);
3878c2ecf20Sopenharmony_ci	else
3888c2ecf20Sopenharmony_ci		v4l2_subdev_call(itv->sd_video, vbi, s_sliced_fmt, &itv->vbi.in.fmt.sliced);
3898c2ecf20Sopenharmony_ci
3908c2ecf20Sopenharmony_ci	/* determine number of lines and total number of VBI bytes.
3918c2ecf20Sopenharmony_ci	   A raw line takes 1443 bytes: 2 * 720 + 4 byte frame header - 1
3928c2ecf20Sopenharmony_ci	   The '- 1' byte is probably an unused U or V byte. Or something...
3938c2ecf20Sopenharmony_ci	   A sliced line takes 51 bytes: 4 byte frame header, 4 byte internal
3948c2ecf20Sopenharmony_ci	   header, 42 data bytes + checksum (to be confirmed) */
3958c2ecf20Sopenharmony_ci	if (raw) {
3968c2ecf20Sopenharmony_ci		lines = itv->vbi.count * 2;
3978c2ecf20Sopenharmony_ci	} else {
3988c2ecf20Sopenharmony_ci		lines = itv->is_60hz ? 24 : 38;
3998c2ecf20Sopenharmony_ci		if (itv->is_60hz && (itv->hw_flags & IVTV_HW_CX25840))
4008c2ecf20Sopenharmony_ci			lines += 2;
4018c2ecf20Sopenharmony_ci	}
4028c2ecf20Sopenharmony_ci
4038c2ecf20Sopenharmony_ci	itv->vbi.enc_size = lines * (raw ? itv->vbi.raw_size : itv->vbi.sliced_size);
4048c2ecf20Sopenharmony_ci
4058c2ecf20Sopenharmony_ci	/* Note: sliced vs raw flag doesn't seem to have any effect
4068c2ecf20Sopenharmony_ci	   TODO: check mode (0x02) value with older ivtv versions. */
4078c2ecf20Sopenharmony_ci	data[0] = raw | 0x02 | (0xbd << 8);
4088c2ecf20Sopenharmony_ci
4098c2ecf20Sopenharmony_ci	/* Every X number of frames a VBI interrupt arrives (frames as in 25 or 30 fps) */
4108c2ecf20Sopenharmony_ci	data[1] = 1;
4118c2ecf20Sopenharmony_ci	/* The VBI frames are stored in a ringbuffer with this size (with a VBI frame as unit) */
4128c2ecf20Sopenharmony_ci	data[2] = raw ? 4 : 4 * (itv->vbi.raw_size / itv->vbi.enc_size);
4138c2ecf20Sopenharmony_ci	/* The start/stop codes determine which VBI lines end up in the raw VBI data area.
4148c2ecf20Sopenharmony_ci	   The codes are from table 24 in the saa7115 datasheet. Each raw/sliced/video line
4158c2ecf20Sopenharmony_ci	   is framed with codes FF0000XX where XX is the SAV/EAV (Start/End of Active Video)
4168c2ecf20Sopenharmony_ci	   code. These values for raw VBI are obtained from a driver disassembly. The sliced
4178c2ecf20Sopenharmony_ci	   start/stop codes was deduced from this, but they do not appear in the driver.
4188c2ecf20Sopenharmony_ci	   Other code pairs that I found are: 0x250E6249/0x13545454 and 0x25256262/0x38137F54.
4198c2ecf20Sopenharmony_ci	   However, I have no idea what these values are for. */
4208c2ecf20Sopenharmony_ci	if (itv->hw_flags & IVTV_HW_CX25840) {
4218c2ecf20Sopenharmony_ci		/* Setup VBI for the cx25840 digitizer */
4228c2ecf20Sopenharmony_ci		if (raw) {
4238c2ecf20Sopenharmony_ci			data[3] = 0x20602060;
4248c2ecf20Sopenharmony_ci			data[4] = 0x30703070;
4258c2ecf20Sopenharmony_ci		} else {
4268c2ecf20Sopenharmony_ci			data[3] = 0xB0F0B0F0;
4278c2ecf20Sopenharmony_ci			data[4] = 0xA0E0A0E0;
4288c2ecf20Sopenharmony_ci		}
4298c2ecf20Sopenharmony_ci		/* Lines per frame */
4308c2ecf20Sopenharmony_ci		data[5] = lines;
4318c2ecf20Sopenharmony_ci		/* bytes per line */
4328c2ecf20Sopenharmony_ci		data[6] = (raw ? itv->vbi.raw_size : itv->vbi.sliced_size);
4338c2ecf20Sopenharmony_ci	} else {
4348c2ecf20Sopenharmony_ci		/* Setup VBI for the saa7115 digitizer */
4358c2ecf20Sopenharmony_ci		if (raw) {
4368c2ecf20Sopenharmony_ci			data[3] = 0x25256262;
4378c2ecf20Sopenharmony_ci			data[4] = 0x387F7F7F;
4388c2ecf20Sopenharmony_ci		} else {
4398c2ecf20Sopenharmony_ci			data[3] = 0xABABECEC;
4408c2ecf20Sopenharmony_ci			data[4] = 0xB6F1F1F1;
4418c2ecf20Sopenharmony_ci		}
4428c2ecf20Sopenharmony_ci		/* Lines per frame */
4438c2ecf20Sopenharmony_ci		data[5] = lines;
4448c2ecf20Sopenharmony_ci		/* bytes per line */
4458c2ecf20Sopenharmony_ci		data[6] = itv->vbi.enc_size / lines;
4468c2ecf20Sopenharmony_ci	}
4478c2ecf20Sopenharmony_ci
4488c2ecf20Sopenharmony_ci	IVTV_DEBUG_INFO(
4498c2ecf20Sopenharmony_ci		"Setup VBI API header 0x%08x pkts %d buffs %d ln %d sz %d\n",
4508c2ecf20Sopenharmony_ci			data[0], data[1], data[2], data[5], data[6]);
4518c2ecf20Sopenharmony_ci
4528c2ecf20Sopenharmony_ci	ivtv_api(itv, CX2341X_ENC_SET_VBI_CONFIG, 7, data);
4538c2ecf20Sopenharmony_ci
4548c2ecf20Sopenharmony_ci	/* returns the VBI encoder memory area. */
4558c2ecf20Sopenharmony_ci	itv->vbi.enc_start = data[2];
4568c2ecf20Sopenharmony_ci	itv->vbi.fpi = data[0];
4578c2ecf20Sopenharmony_ci	if (!itv->vbi.fpi)
4588c2ecf20Sopenharmony_ci		itv->vbi.fpi = 1;
4598c2ecf20Sopenharmony_ci
4608c2ecf20Sopenharmony_ci	IVTV_DEBUG_INFO("Setup VBI start 0x%08x frames %d fpi %d\n",
4618c2ecf20Sopenharmony_ci		itv->vbi.enc_start, data[1], itv->vbi.fpi);
4628c2ecf20Sopenharmony_ci
4638c2ecf20Sopenharmony_ci	/* select VBI lines.
4648c2ecf20Sopenharmony_ci	   Note that the sliced argument seems to have no effect. */
4658c2ecf20Sopenharmony_ci	for (i = 2; i <= 24; i++) {
4668c2ecf20Sopenharmony_ci		int valid;
4678c2ecf20Sopenharmony_ci
4688c2ecf20Sopenharmony_ci		if (itv->is_60hz) {
4698c2ecf20Sopenharmony_ci			valid = i >= 10 && i < 22;
4708c2ecf20Sopenharmony_ci		} else {
4718c2ecf20Sopenharmony_ci			valid = i >= 6 && i < 24;
4728c2ecf20Sopenharmony_ci		}
4738c2ecf20Sopenharmony_ci		ivtv_vapi(itv, CX2341X_ENC_SET_VBI_LINE, 5, i - 1,
4748c2ecf20Sopenharmony_ci				valid, 0 , 0, 0);
4758c2ecf20Sopenharmony_ci		ivtv_vapi(itv, CX2341X_ENC_SET_VBI_LINE, 5, (i - 1) | 0x80000000,
4768c2ecf20Sopenharmony_ci				valid, 0, 0, 0);
4778c2ecf20Sopenharmony_ci	}
4788c2ecf20Sopenharmony_ci
4798c2ecf20Sopenharmony_ci	/* Remaining VBI questions:
4808c2ecf20Sopenharmony_ci	   - Is it possible to select particular VBI lines only for inclusion in the MPEG
4818c2ecf20Sopenharmony_ci	   stream? Currently you can only get the first X lines.
4828c2ecf20Sopenharmony_ci	   - Is mixed raw and sliced VBI possible?
4838c2ecf20Sopenharmony_ci	   - What's the meaning of the raw/sliced flag?
4848c2ecf20Sopenharmony_ci	   - What's the meaning of params 2, 3 & 4 of the Select VBI command? */
4858c2ecf20Sopenharmony_ci}
4868c2ecf20Sopenharmony_ci
4878c2ecf20Sopenharmony_ciint ivtv_start_v4l2_encode_stream(struct ivtv_stream *s)
4888c2ecf20Sopenharmony_ci{
4898c2ecf20Sopenharmony_ci	u32 data[CX2341X_MBOX_MAX_DATA];
4908c2ecf20Sopenharmony_ci	struct ivtv *itv = s->itv;
4918c2ecf20Sopenharmony_ci	int captype = 0, subtype = 0;
4928c2ecf20Sopenharmony_ci	int enable_passthrough = 0;
4938c2ecf20Sopenharmony_ci
4948c2ecf20Sopenharmony_ci	if (s->vdev.v4l2_dev == NULL)
4958c2ecf20Sopenharmony_ci		return -EINVAL;
4968c2ecf20Sopenharmony_ci
4978c2ecf20Sopenharmony_ci	IVTV_DEBUG_INFO("Start encoder stream %s\n", s->name);
4988c2ecf20Sopenharmony_ci
4998c2ecf20Sopenharmony_ci	switch (s->type) {
5008c2ecf20Sopenharmony_ci	case IVTV_ENC_STREAM_TYPE_MPG:
5018c2ecf20Sopenharmony_ci		captype = 0;
5028c2ecf20Sopenharmony_ci		subtype = 3;
5038c2ecf20Sopenharmony_ci
5048c2ecf20Sopenharmony_ci		/* Stop Passthrough */
5058c2ecf20Sopenharmony_ci		if (itv->output_mode == OUT_PASSTHROUGH) {
5068c2ecf20Sopenharmony_ci			ivtv_passthrough_mode(itv, 0);
5078c2ecf20Sopenharmony_ci			enable_passthrough = 1;
5088c2ecf20Sopenharmony_ci		}
5098c2ecf20Sopenharmony_ci		itv->mpg_data_received = itv->vbi_data_inserted = 0;
5108c2ecf20Sopenharmony_ci		itv->dualwatch_jiffies = jiffies;
5118c2ecf20Sopenharmony_ci		itv->dualwatch_stereo_mode = v4l2_ctrl_g_ctrl(itv->cxhdl.audio_mode);
5128c2ecf20Sopenharmony_ci		itv->search_pack_header = 0;
5138c2ecf20Sopenharmony_ci		break;
5148c2ecf20Sopenharmony_ci
5158c2ecf20Sopenharmony_ci	case IVTV_ENC_STREAM_TYPE_YUV:
5168c2ecf20Sopenharmony_ci		if (itv->output_mode == OUT_PASSTHROUGH) {
5178c2ecf20Sopenharmony_ci			captype = 2;
5188c2ecf20Sopenharmony_ci			subtype = 11;	/* video+audio+decoder */
5198c2ecf20Sopenharmony_ci			break;
5208c2ecf20Sopenharmony_ci		}
5218c2ecf20Sopenharmony_ci		captype = 1;
5228c2ecf20Sopenharmony_ci		subtype = 1;
5238c2ecf20Sopenharmony_ci		break;
5248c2ecf20Sopenharmony_ci	case IVTV_ENC_STREAM_TYPE_PCM:
5258c2ecf20Sopenharmony_ci		captype = 1;
5268c2ecf20Sopenharmony_ci		subtype = 2;
5278c2ecf20Sopenharmony_ci		break;
5288c2ecf20Sopenharmony_ci	case IVTV_ENC_STREAM_TYPE_VBI:
5298c2ecf20Sopenharmony_ci		captype = 1;
5308c2ecf20Sopenharmony_ci		subtype = 4;
5318c2ecf20Sopenharmony_ci
5328c2ecf20Sopenharmony_ci		itv->vbi.frame = 0;
5338c2ecf20Sopenharmony_ci		itv->vbi.inserted_frame = 0;
5348c2ecf20Sopenharmony_ci		memset(itv->vbi.sliced_mpeg_size,
5358c2ecf20Sopenharmony_ci			0, sizeof(itv->vbi.sliced_mpeg_size));
5368c2ecf20Sopenharmony_ci		break;
5378c2ecf20Sopenharmony_ci	default:
5388c2ecf20Sopenharmony_ci		return -EINVAL;
5398c2ecf20Sopenharmony_ci	}
5408c2ecf20Sopenharmony_ci	s->subtype = subtype;
5418c2ecf20Sopenharmony_ci	s->buffers_stolen = 0;
5428c2ecf20Sopenharmony_ci
5438c2ecf20Sopenharmony_ci	/* Clear Streamoff flags in case left from last capture */
5448c2ecf20Sopenharmony_ci	clear_bit(IVTV_F_S_STREAMOFF, &s->s_flags);
5458c2ecf20Sopenharmony_ci
5468c2ecf20Sopenharmony_ci	if (atomic_read(&itv->capturing) == 0) {
5478c2ecf20Sopenharmony_ci		int digitizer;
5488c2ecf20Sopenharmony_ci
5498c2ecf20Sopenharmony_ci		/* Always use frame based mode. Experiments have demonstrated that byte
5508c2ecf20Sopenharmony_ci		   stream based mode results in dropped frames and corruption. Not often,
5518c2ecf20Sopenharmony_ci		   but occasionally. Many thanks go to Leonard Orb who spent a lot of
5528c2ecf20Sopenharmony_ci		   effort and time trying to trace the cause of the drop outs. */
5538c2ecf20Sopenharmony_ci		/* 1 frame per DMA */
5548c2ecf20Sopenharmony_ci		/*ivtv_vapi(itv, CX2341X_ENC_SET_DMA_BLOCK_SIZE, 2, 128, 0); */
5558c2ecf20Sopenharmony_ci		ivtv_vapi(itv, CX2341X_ENC_SET_DMA_BLOCK_SIZE, 2, 1, 1);
5568c2ecf20Sopenharmony_ci
5578c2ecf20Sopenharmony_ci		/* Stuff from Windows, we don't know what it is */
5588c2ecf20Sopenharmony_ci		ivtv_vapi(itv, CX2341X_ENC_SET_VERT_CROP_LINE, 1, 0);
5598c2ecf20Sopenharmony_ci		/* According to the docs, this should be correct. However, this is
5608c2ecf20Sopenharmony_ci		   untested. I don't dare enable this without having tested it.
5618c2ecf20Sopenharmony_ci		   Only very few old cards actually have this hardware combination.
5628c2ecf20Sopenharmony_ci		ivtv_vapi(itv, CX2341X_ENC_SET_VERT_CROP_LINE, 1,
5638c2ecf20Sopenharmony_ci			((itv->hw_flags & IVTV_HW_SAA7114) && itv->is_60hz) ? 10001 : 0);
5648c2ecf20Sopenharmony_ci		*/
5658c2ecf20Sopenharmony_ci		ivtv_vapi(itv, CX2341X_ENC_MISC, 2, 3, !itv->has_cx23415);
5668c2ecf20Sopenharmony_ci		ivtv_vapi(itv, CX2341X_ENC_MISC, 2, 8, 0);
5678c2ecf20Sopenharmony_ci		ivtv_vapi(itv, CX2341X_ENC_MISC, 2, 4, 1);
5688c2ecf20Sopenharmony_ci		ivtv_vapi(itv, CX2341X_ENC_MISC, 1, 12);
5698c2ecf20Sopenharmony_ci
5708c2ecf20Sopenharmony_ci		/* assign placeholder */
5718c2ecf20Sopenharmony_ci		ivtv_vapi(itv, CX2341X_ENC_SET_PLACEHOLDER, 12,
5728c2ecf20Sopenharmony_ci			0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
5738c2ecf20Sopenharmony_ci
5748c2ecf20Sopenharmony_ci		if (itv->card->hw_all & (IVTV_HW_SAA7115 | IVTV_HW_SAA717X))
5758c2ecf20Sopenharmony_ci		    digitizer = 0xF1;
5768c2ecf20Sopenharmony_ci		else if (itv->card->hw_all & IVTV_HW_SAA7114)
5778c2ecf20Sopenharmony_ci		    digitizer = 0xEF;
5788c2ecf20Sopenharmony_ci		else /* cx25840 */
5798c2ecf20Sopenharmony_ci		    digitizer = 0x140;
5808c2ecf20Sopenharmony_ci
5818c2ecf20Sopenharmony_ci		ivtv_vapi(itv, CX2341X_ENC_SET_NUM_VSYNC_LINES, 2, digitizer, digitizer);
5828c2ecf20Sopenharmony_ci
5838c2ecf20Sopenharmony_ci		/* Setup VBI */
5848c2ecf20Sopenharmony_ci		if (itv->v4l2_cap & V4L2_CAP_VBI_CAPTURE) {
5858c2ecf20Sopenharmony_ci			ivtv_vbi_setup(itv);
5868c2ecf20Sopenharmony_ci		}
5878c2ecf20Sopenharmony_ci
5888c2ecf20Sopenharmony_ci		/* assign program index info. Mask 7: select I/P/B, Num_req: 400 max */
5898c2ecf20Sopenharmony_ci		ivtv_vapi_result(itv, data, CX2341X_ENC_SET_PGM_INDEX_INFO, 2, 7, 400);
5908c2ecf20Sopenharmony_ci		itv->pgm_info_offset = data[0];
5918c2ecf20Sopenharmony_ci		itv->pgm_info_num = data[1];
5928c2ecf20Sopenharmony_ci		itv->pgm_info_write_idx = 0;
5938c2ecf20Sopenharmony_ci		itv->pgm_info_read_idx = 0;
5948c2ecf20Sopenharmony_ci
5958c2ecf20Sopenharmony_ci		IVTV_DEBUG_INFO("PGM Index at 0x%08x with %d elements\n",
5968c2ecf20Sopenharmony_ci				itv->pgm_info_offset, itv->pgm_info_num);
5978c2ecf20Sopenharmony_ci
5988c2ecf20Sopenharmony_ci		/* Setup API for Stream */
5998c2ecf20Sopenharmony_ci		cx2341x_handler_setup(&itv->cxhdl);
6008c2ecf20Sopenharmony_ci
6018c2ecf20Sopenharmony_ci		/* mute if capturing radio */
6028c2ecf20Sopenharmony_ci		if (test_bit(IVTV_F_I_RADIO_USER, &itv->i_flags))
6038c2ecf20Sopenharmony_ci			ivtv_vapi(itv, CX2341X_ENC_MUTE_VIDEO, 1,
6048c2ecf20Sopenharmony_ci				1 | (v4l2_ctrl_g_ctrl(itv->cxhdl.video_mute_yuv) << 8));
6058c2ecf20Sopenharmony_ci	}
6068c2ecf20Sopenharmony_ci
6078c2ecf20Sopenharmony_ci	/* Vsync Setup */
6088c2ecf20Sopenharmony_ci	if (itv->has_cx23415 && !test_and_set_bit(IVTV_F_I_DIG_RST, &itv->i_flags)) {
6098c2ecf20Sopenharmony_ci		/* event notification (on) */
6108c2ecf20Sopenharmony_ci		ivtv_vapi(itv, CX2341X_ENC_SET_EVENT_NOTIFICATION, 4, 0, 1, IVTV_IRQ_ENC_VIM_RST, -1);
6118c2ecf20Sopenharmony_ci		ivtv_clear_irq_mask(itv, IVTV_IRQ_ENC_VIM_RST);
6128c2ecf20Sopenharmony_ci	}
6138c2ecf20Sopenharmony_ci
6148c2ecf20Sopenharmony_ci	if (atomic_read(&itv->capturing) == 0) {
6158c2ecf20Sopenharmony_ci		/* Clear all Pending Interrupts */
6168c2ecf20Sopenharmony_ci		ivtv_set_irq_mask(itv, IVTV_IRQ_MASK_CAPTURE);
6178c2ecf20Sopenharmony_ci
6188c2ecf20Sopenharmony_ci		clear_bit(IVTV_F_I_EOS, &itv->i_flags);
6198c2ecf20Sopenharmony_ci
6208c2ecf20Sopenharmony_ci		cx2341x_handler_set_busy(&itv->cxhdl, 1);
6218c2ecf20Sopenharmony_ci
6228c2ecf20Sopenharmony_ci		/* Initialize Digitizer for Capture */
6238c2ecf20Sopenharmony_ci		/* Avoid tinny audio problem - ensure audio clocks are going */
6248c2ecf20Sopenharmony_ci		v4l2_subdev_call(itv->sd_audio, audio, s_stream, 1);
6258c2ecf20Sopenharmony_ci		/* Avoid unpredictable PCI bus hang - disable video clocks */
6268c2ecf20Sopenharmony_ci		v4l2_subdev_call(itv->sd_video, video, s_stream, 0);
6278c2ecf20Sopenharmony_ci		ivtv_msleep_timeout(300, 0);
6288c2ecf20Sopenharmony_ci		ivtv_vapi(itv, CX2341X_ENC_INITIALIZE_INPUT, 0);
6298c2ecf20Sopenharmony_ci		v4l2_subdev_call(itv->sd_video, video, s_stream, 1);
6308c2ecf20Sopenharmony_ci	}
6318c2ecf20Sopenharmony_ci
6328c2ecf20Sopenharmony_ci	/* begin_capture */
6338c2ecf20Sopenharmony_ci	if (ivtv_vapi(itv, CX2341X_ENC_START_CAPTURE, 2, captype, subtype))
6348c2ecf20Sopenharmony_ci	{
6358c2ecf20Sopenharmony_ci		IVTV_DEBUG_WARN( "Error starting capture!\n");
6368c2ecf20Sopenharmony_ci		return -EINVAL;
6378c2ecf20Sopenharmony_ci	}
6388c2ecf20Sopenharmony_ci
6398c2ecf20Sopenharmony_ci	/* Start Passthrough */
6408c2ecf20Sopenharmony_ci	if (enable_passthrough) {
6418c2ecf20Sopenharmony_ci		ivtv_passthrough_mode(itv, 1);
6428c2ecf20Sopenharmony_ci	}
6438c2ecf20Sopenharmony_ci
6448c2ecf20Sopenharmony_ci	if (s->type == IVTV_ENC_STREAM_TYPE_VBI)
6458c2ecf20Sopenharmony_ci		ivtv_clear_irq_mask(itv, IVTV_IRQ_ENC_VBI_CAP);
6468c2ecf20Sopenharmony_ci	else
6478c2ecf20Sopenharmony_ci		ivtv_clear_irq_mask(itv, IVTV_IRQ_MASK_CAPTURE);
6488c2ecf20Sopenharmony_ci
6498c2ecf20Sopenharmony_ci	/* you're live! sit back and await interrupts :) */
6508c2ecf20Sopenharmony_ci	atomic_inc(&itv->capturing);
6518c2ecf20Sopenharmony_ci	return 0;
6528c2ecf20Sopenharmony_ci}
6538c2ecf20Sopenharmony_ciEXPORT_SYMBOL(ivtv_start_v4l2_encode_stream);
6548c2ecf20Sopenharmony_ci
6558c2ecf20Sopenharmony_cistatic int ivtv_setup_v4l2_decode_stream(struct ivtv_stream *s)
6568c2ecf20Sopenharmony_ci{
6578c2ecf20Sopenharmony_ci	u32 data[CX2341X_MBOX_MAX_DATA];
6588c2ecf20Sopenharmony_ci	struct ivtv *itv = s->itv;
6598c2ecf20Sopenharmony_ci	int datatype;
6608c2ecf20Sopenharmony_ci	u16 width;
6618c2ecf20Sopenharmony_ci	u16 height;
6628c2ecf20Sopenharmony_ci
6638c2ecf20Sopenharmony_ci	if (s->vdev.v4l2_dev == NULL)
6648c2ecf20Sopenharmony_ci		return -EINVAL;
6658c2ecf20Sopenharmony_ci
6668c2ecf20Sopenharmony_ci	IVTV_DEBUG_INFO("Setting some initial decoder settings\n");
6678c2ecf20Sopenharmony_ci
6688c2ecf20Sopenharmony_ci	width = itv->cxhdl.width;
6698c2ecf20Sopenharmony_ci	height = itv->cxhdl.height;
6708c2ecf20Sopenharmony_ci
6718c2ecf20Sopenharmony_ci	/* set audio mode to left/stereo  for dual/stereo mode. */
6728c2ecf20Sopenharmony_ci	ivtv_vapi(itv, CX2341X_DEC_SET_AUDIO_MODE, 2, itv->audio_bilingual_mode, itv->audio_stereo_mode);
6738c2ecf20Sopenharmony_ci
6748c2ecf20Sopenharmony_ci	/* set number of internal decoder buffers */
6758c2ecf20Sopenharmony_ci	ivtv_vapi(itv, CX2341X_DEC_SET_DISPLAY_BUFFERS, 1, 0);
6768c2ecf20Sopenharmony_ci
6778c2ecf20Sopenharmony_ci	/* prebuffering */
6788c2ecf20Sopenharmony_ci	ivtv_vapi(itv, CX2341X_DEC_SET_PREBUFFERING, 1, 1);
6798c2ecf20Sopenharmony_ci
6808c2ecf20Sopenharmony_ci	/* extract from user packets */
6818c2ecf20Sopenharmony_ci	ivtv_vapi_result(itv, data, CX2341X_DEC_EXTRACT_VBI, 1, 1);
6828c2ecf20Sopenharmony_ci	itv->vbi.dec_start = data[0];
6838c2ecf20Sopenharmony_ci
6848c2ecf20Sopenharmony_ci	IVTV_DEBUG_INFO("Decoder VBI RE-Insert start 0x%08x size 0x%08x\n",
6858c2ecf20Sopenharmony_ci		itv->vbi.dec_start, data[1]);
6868c2ecf20Sopenharmony_ci
6878c2ecf20Sopenharmony_ci	/* set decoder source settings */
6888c2ecf20Sopenharmony_ci	/* Data type: 0 = mpeg from host,
6898c2ecf20Sopenharmony_ci	   1 = yuv from encoder,
6908c2ecf20Sopenharmony_ci	   2 = yuv_from_host */
6918c2ecf20Sopenharmony_ci	switch (s->type) {
6928c2ecf20Sopenharmony_ci	case IVTV_DEC_STREAM_TYPE_YUV:
6938c2ecf20Sopenharmony_ci		if (itv->output_mode == OUT_PASSTHROUGH) {
6948c2ecf20Sopenharmony_ci			datatype = 1;
6958c2ecf20Sopenharmony_ci		} else {
6968c2ecf20Sopenharmony_ci			/* Fake size to avoid switching video standard */
6978c2ecf20Sopenharmony_ci			datatype = 2;
6988c2ecf20Sopenharmony_ci			width = 720;
6998c2ecf20Sopenharmony_ci			height = itv->is_out_50hz ? 576 : 480;
7008c2ecf20Sopenharmony_ci		}
7018c2ecf20Sopenharmony_ci		IVTV_DEBUG_INFO("Setup DEC YUV Stream data[0] = %d\n", datatype);
7028c2ecf20Sopenharmony_ci		break;
7038c2ecf20Sopenharmony_ci	case IVTV_DEC_STREAM_TYPE_MPG:
7048c2ecf20Sopenharmony_ci	default:
7058c2ecf20Sopenharmony_ci		datatype = 0;
7068c2ecf20Sopenharmony_ci		break;
7078c2ecf20Sopenharmony_ci	}
7088c2ecf20Sopenharmony_ci	if (ivtv_vapi(itv, CX2341X_DEC_SET_DECODER_SOURCE, 4, datatype,
7098c2ecf20Sopenharmony_ci			width, height, itv->cxhdl.audio_properties)) {
7108c2ecf20Sopenharmony_ci		IVTV_DEBUG_WARN("Couldn't initialize decoder source\n");
7118c2ecf20Sopenharmony_ci	}
7128c2ecf20Sopenharmony_ci
7138c2ecf20Sopenharmony_ci	/* Decoder sometimes dies here, so wait a moment */
7148c2ecf20Sopenharmony_ci	ivtv_msleep_timeout(10, 0);
7158c2ecf20Sopenharmony_ci
7168c2ecf20Sopenharmony_ci	/* Known failure point for firmware, so check */
7178c2ecf20Sopenharmony_ci	return ivtv_firmware_check(itv, "ivtv_setup_v4l2_decode_stream");
7188c2ecf20Sopenharmony_ci}
7198c2ecf20Sopenharmony_ci
7208c2ecf20Sopenharmony_ciint ivtv_start_v4l2_decode_stream(struct ivtv_stream *s, int gop_offset)
7218c2ecf20Sopenharmony_ci{
7228c2ecf20Sopenharmony_ci	struct ivtv *itv = s->itv;
7238c2ecf20Sopenharmony_ci	int rc;
7248c2ecf20Sopenharmony_ci
7258c2ecf20Sopenharmony_ci	if (s->vdev.v4l2_dev == NULL)
7268c2ecf20Sopenharmony_ci		return -EINVAL;
7278c2ecf20Sopenharmony_ci
7288c2ecf20Sopenharmony_ci	if (test_and_set_bit(IVTV_F_S_STREAMING, &s->s_flags))
7298c2ecf20Sopenharmony_ci		return 0;	/* already started */
7308c2ecf20Sopenharmony_ci
7318c2ecf20Sopenharmony_ci	IVTV_DEBUG_INFO("Starting decode stream %s (gop_offset %d)\n", s->name, gop_offset);
7328c2ecf20Sopenharmony_ci
7338c2ecf20Sopenharmony_ci	rc = ivtv_setup_v4l2_decode_stream(s);
7348c2ecf20Sopenharmony_ci	if (rc < 0) {
7358c2ecf20Sopenharmony_ci		clear_bit(IVTV_F_S_STREAMING, &s->s_flags);
7368c2ecf20Sopenharmony_ci		return rc;
7378c2ecf20Sopenharmony_ci	}
7388c2ecf20Sopenharmony_ci
7398c2ecf20Sopenharmony_ci	/* set dma size to 65536 bytes */
7408c2ecf20Sopenharmony_ci	ivtv_vapi(itv, CX2341X_DEC_SET_DMA_BLOCK_SIZE, 1, 65536);
7418c2ecf20Sopenharmony_ci
7428c2ecf20Sopenharmony_ci	/* Clear Streamoff */
7438c2ecf20Sopenharmony_ci	clear_bit(IVTV_F_S_STREAMOFF, &s->s_flags);
7448c2ecf20Sopenharmony_ci
7458c2ecf20Sopenharmony_ci	/* Zero out decoder counters */
7468c2ecf20Sopenharmony_ci	writel(0, &itv->dec_mbox.mbox[IVTV_MBOX_DMA_END].data[0]);
7478c2ecf20Sopenharmony_ci	writel(0, &itv->dec_mbox.mbox[IVTV_MBOX_DMA_END].data[1]);
7488c2ecf20Sopenharmony_ci	writel(0, &itv->dec_mbox.mbox[IVTV_MBOX_DMA_END].data[2]);
7498c2ecf20Sopenharmony_ci	writel(0, &itv->dec_mbox.mbox[IVTV_MBOX_DMA_END].data[3]);
7508c2ecf20Sopenharmony_ci	writel(0, &itv->dec_mbox.mbox[IVTV_MBOX_DMA].data[0]);
7518c2ecf20Sopenharmony_ci	writel(0, &itv->dec_mbox.mbox[IVTV_MBOX_DMA].data[1]);
7528c2ecf20Sopenharmony_ci	writel(0, &itv->dec_mbox.mbox[IVTV_MBOX_DMA].data[2]);
7538c2ecf20Sopenharmony_ci	writel(0, &itv->dec_mbox.mbox[IVTV_MBOX_DMA].data[3]);
7548c2ecf20Sopenharmony_ci
7558c2ecf20Sopenharmony_ci	/* turn on notification of dual/stereo mode change */
7568c2ecf20Sopenharmony_ci	ivtv_vapi(itv, CX2341X_DEC_SET_EVENT_NOTIFICATION, 4, 0, 1, IVTV_IRQ_DEC_AUD_MODE_CHG, -1);
7578c2ecf20Sopenharmony_ci
7588c2ecf20Sopenharmony_ci	/* start playback */
7598c2ecf20Sopenharmony_ci	ivtv_vapi(itv, CX2341X_DEC_START_PLAYBACK, 2, gop_offset, 0);
7608c2ecf20Sopenharmony_ci
7618c2ecf20Sopenharmony_ci	/* Let things settle before we actually start */
7628c2ecf20Sopenharmony_ci	ivtv_msleep_timeout(10, 0);
7638c2ecf20Sopenharmony_ci
7648c2ecf20Sopenharmony_ci	/* Clear the following Interrupt mask bits for decoding */
7658c2ecf20Sopenharmony_ci	ivtv_clear_irq_mask(itv, IVTV_IRQ_MASK_DECODE);
7668c2ecf20Sopenharmony_ci	IVTV_DEBUG_IRQ("IRQ Mask is now: 0x%08x\n", itv->irqmask);
7678c2ecf20Sopenharmony_ci
7688c2ecf20Sopenharmony_ci	/* you're live! sit back and await interrupts :) */
7698c2ecf20Sopenharmony_ci	atomic_inc(&itv->decoding);
7708c2ecf20Sopenharmony_ci	return 0;
7718c2ecf20Sopenharmony_ci}
7728c2ecf20Sopenharmony_ci
7738c2ecf20Sopenharmony_civoid ivtv_stop_all_captures(struct ivtv *itv)
7748c2ecf20Sopenharmony_ci{
7758c2ecf20Sopenharmony_ci	int i;
7768c2ecf20Sopenharmony_ci
7778c2ecf20Sopenharmony_ci	for (i = IVTV_MAX_STREAMS - 1; i >= 0; i--) {
7788c2ecf20Sopenharmony_ci		struct ivtv_stream *s = &itv->streams[i];
7798c2ecf20Sopenharmony_ci
7808c2ecf20Sopenharmony_ci		if (s->vdev.v4l2_dev == NULL)
7818c2ecf20Sopenharmony_ci			continue;
7828c2ecf20Sopenharmony_ci		if (test_bit(IVTV_F_S_STREAMING, &s->s_flags)) {
7838c2ecf20Sopenharmony_ci			ivtv_stop_v4l2_encode_stream(s, 0);
7848c2ecf20Sopenharmony_ci		}
7858c2ecf20Sopenharmony_ci	}
7868c2ecf20Sopenharmony_ci}
7878c2ecf20Sopenharmony_ci
7888c2ecf20Sopenharmony_ciint ivtv_stop_v4l2_encode_stream(struct ivtv_stream *s, int gop_end)
7898c2ecf20Sopenharmony_ci{
7908c2ecf20Sopenharmony_ci	struct ivtv *itv = s->itv;
7918c2ecf20Sopenharmony_ci	DECLARE_WAITQUEUE(wait, current);
7928c2ecf20Sopenharmony_ci	int cap_type;
7938c2ecf20Sopenharmony_ci	int stopmode;
7948c2ecf20Sopenharmony_ci
7958c2ecf20Sopenharmony_ci	if (s->vdev.v4l2_dev == NULL)
7968c2ecf20Sopenharmony_ci		return -EINVAL;
7978c2ecf20Sopenharmony_ci
7988c2ecf20Sopenharmony_ci	/* This function assumes that you are allowed to stop the capture
7998c2ecf20Sopenharmony_ci	   and that we are actually capturing */
8008c2ecf20Sopenharmony_ci
8018c2ecf20Sopenharmony_ci	IVTV_DEBUG_INFO("Stop Capture\n");
8028c2ecf20Sopenharmony_ci
8038c2ecf20Sopenharmony_ci	if (s->type == IVTV_DEC_STREAM_TYPE_VOUT)
8048c2ecf20Sopenharmony_ci		return 0;
8058c2ecf20Sopenharmony_ci	if (atomic_read(&itv->capturing) == 0)
8068c2ecf20Sopenharmony_ci		return 0;
8078c2ecf20Sopenharmony_ci
8088c2ecf20Sopenharmony_ci	switch (s->type) {
8098c2ecf20Sopenharmony_ci	case IVTV_ENC_STREAM_TYPE_YUV:
8108c2ecf20Sopenharmony_ci		cap_type = 1;
8118c2ecf20Sopenharmony_ci		break;
8128c2ecf20Sopenharmony_ci	case IVTV_ENC_STREAM_TYPE_PCM:
8138c2ecf20Sopenharmony_ci		cap_type = 1;
8148c2ecf20Sopenharmony_ci		break;
8158c2ecf20Sopenharmony_ci	case IVTV_ENC_STREAM_TYPE_VBI:
8168c2ecf20Sopenharmony_ci		cap_type = 1;
8178c2ecf20Sopenharmony_ci		break;
8188c2ecf20Sopenharmony_ci	case IVTV_ENC_STREAM_TYPE_MPG:
8198c2ecf20Sopenharmony_ci	default:
8208c2ecf20Sopenharmony_ci		cap_type = 0;
8218c2ecf20Sopenharmony_ci		break;
8228c2ecf20Sopenharmony_ci	}
8238c2ecf20Sopenharmony_ci
8248c2ecf20Sopenharmony_ci	/* Stop Capture Mode */
8258c2ecf20Sopenharmony_ci	if (s->type == IVTV_ENC_STREAM_TYPE_MPG && gop_end) {
8268c2ecf20Sopenharmony_ci		stopmode = 0;
8278c2ecf20Sopenharmony_ci	} else {
8288c2ecf20Sopenharmony_ci		stopmode = 1;
8298c2ecf20Sopenharmony_ci	}
8308c2ecf20Sopenharmony_ci
8318c2ecf20Sopenharmony_ci	/* end_capture */
8328c2ecf20Sopenharmony_ci	/* when: 0 =  end of GOP  1 = NOW!, type: 0 = mpeg, subtype: 3 = video+audio */
8338c2ecf20Sopenharmony_ci	ivtv_vapi(itv, CX2341X_ENC_STOP_CAPTURE, 3, stopmode, cap_type, s->subtype);
8348c2ecf20Sopenharmony_ci
8358c2ecf20Sopenharmony_ci	if (!test_bit(IVTV_F_S_PASSTHROUGH, &s->s_flags)) {
8368c2ecf20Sopenharmony_ci		if (s->type == IVTV_ENC_STREAM_TYPE_MPG && gop_end) {
8378c2ecf20Sopenharmony_ci			/* only run these if we're shutting down the last cap */
8388c2ecf20Sopenharmony_ci			unsigned long duration;
8398c2ecf20Sopenharmony_ci			unsigned long then = jiffies;
8408c2ecf20Sopenharmony_ci
8418c2ecf20Sopenharmony_ci			add_wait_queue(&itv->eos_waitq, &wait);
8428c2ecf20Sopenharmony_ci
8438c2ecf20Sopenharmony_ci			set_current_state(TASK_INTERRUPTIBLE);
8448c2ecf20Sopenharmony_ci
8458c2ecf20Sopenharmony_ci			/* wait 2s for EOS interrupt */
8468c2ecf20Sopenharmony_ci			while (!test_bit(IVTV_F_I_EOS, &itv->i_flags) &&
8478c2ecf20Sopenharmony_ci				time_before(jiffies,
8488c2ecf20Sopenharmony_ci					    then + msecs_to_jiffies(2000))) {
8498c2ecf20Sopenharmony_ci				schedule_timeout(msecs_to_jiffies(10));
8508c2ecf20Sopenharmony_ci			}
8518c2ecf20Sopenharmony_ci
8528c2ecf20Sopenharmony_ci			/* To convert jiffies to ms, we must multiply by 1000
8538c2ecf20Sopenharmony_ci			 * and divide by HZ.  To avoid runtime division, we
8548c2ecf20Sopenharmony_ci			 * convert this to multiplication by 1000/HZ.
8558c2ecf20Sopenharmony_ci			 * Since integer division truncates, we get the best
8568c2ecf20Sopenharmony_ci			 * accuracy if we do a rounding calculation of the constant.
8578c2ecf20Sopenharmony_ci			 * Think of the case where HZ is 1024.
8588c2ecf20Sopenharmony_ci			 */
8598c2ecf20Sopenharmony_ci			duration = ((1000 + HZ / 2) / HZ) * (jiffies - then);
8608c2ecf20Sopenharmony_ci
8618c2ecf20Sopenharmony_ci			if (!test_bit(IVTV_F_I_EOS, &itv->i_flags)) {
8628c2ecf20Sopenharmony_ci				IVTV_DEBUG_WARN("%s: EOS interrupt not received! stopping anyway.\n", s->name);
8638c2ecf20Sopenharmony_ci				IVTV_DEBUG_WARN("%s: waited %lu ms.\n", s->name, duration);
8648c2ecf20Sopenharmony_ci			} else {
8658c2ecf20Sopenharmony_ci				IVTV_DEBUG_INFO("%s: EOS took %lu ms to occur.\n", s->name, duration);
8668c2ecf20Sopenharmony_ci			}
8678c2ecf20Sopenharmony_ci			set_current_state(TASK_RUNNING);
8688c2ecf20Sopenharmony_ci			remove_wait_queue(&itv->eos_waitq, &wait);
8698c2ecf20Sopenharmony_ci			set_bit(IVTV_F_S_STREAMOFF, &s->s_flags);
8708c2ecf20Sopenharmony_ci		}
8718c2ecf20Sopenharmony_ci
8728c2ecf20Sopenharmony_ci		/* Handle any pending interrupts */
8738c2ecf20Sopenharmony_ci		ivtv_msleep_timeout(100, 0);
8748c2ecf20Sopenharmony_ci	}
8758c2ecf20Sopenharmony_ci
8768c2ecf20Sopenharmony_ci	atomic_dec(&itv->capturing);
8778c2ecf20Sopenharmony_ci
8788c2ecf20Sopenharmony_ci	/* Clear capture and no-read bits */
8798c2ecf20Sopenharmony_ci	clear_bit(IVTV_F_S_STREAMING, &s->s_flags);
8808c2ecf20Sopenharmony_ci
8818c2ecf20Sopenharmony_ci	if (s->type == IVTV_ENC_STREAM_TYPE_VBI)
8828c2ecf20Sopenharmony_ci		ivtv_set_irq_mask(itv, IVTV_IRQ_ENC_VBI_CAP);
8838c2ecf20Sopenharmony_ci
8848c2ecf20Sopenharmony_ci	if (atomic_read(&itv->capturing) > 0) {
8858c2ecf20Sopenharmony_ci		return 0;
8868c2ecf20Sopenharmony_ci	}
8878c2ecf20Sopenharmony_ci
8888c2ecf20Sopenharmony_ci	cx2341x_handler_set_busy(&itv->cxhdl, 0);
8898c2ecf20Sopenharmony_ci
8908c2ecf20Sopenharmony_ci	/* Set the following Interrupt mask bits for capture */
8918c2ecf20Sopenharmony_ci	ivtv_set_irq_mask(itv, IVTV_IRQ_MASK_CAPTURE);
8928c2ecf20Sopenharmony_ci	del_timer(&itv->dma_timer);
8938c2ecf20Sopenharmony_ci
8948c2ecf20Sopenharmony_ci	/* event notification (off) */
8958c2ecf20Sopenharmony_ci	if (test_and_clear_bit(IVTV_F_I_DIG_RST, &itv->i_flags)) {
8968c2ecf20Sopenharmony_ci		/* type: 0 = refresh */
8978c2ecf20Sopenharmony_ci		/* on/off: 0 = off, intr: 0x10000000, mbox_id: -1: none */
8988c2ecf20Sopenharmony_ci		ivtv_vapi(itv, CX2341X_ENC_SET_EVENT_NOTIFICATION, 4, 0, 0, IVTV_IRQ_ENC_VIM_RST, -1);
8998c2ecf20Sopenharmony_ci		ivtv_set_irq_mask(itv, IVTV_IRQ_ENC_VIM_RST);
9008c2ecf20Sopenharmony_ci	}
9018c2ecf20Sopenharmony_ci
9028c2ecf20Sopenharmony_ci	/* Raw-passthrough is implied on start. Make sure it's stopped so
9038c2ecf20Sopenharmony_ci	   the encoder will re-initialize when next started */
9048c2ecf20Sopenharmony_ci	ivtv_vapi(itv, CX2341X_ENC_STOP_CAPTURE, 3, 1, 2, 7);
9058c2ecf20Sopenharmony_ci
9068c2ecf20Sopenharmony_ci	wake_up(&s->waitq);
9078c2ecf20Sopenharmony_ci
9088c2ecf20Sopenharmony_ci	return 0;
9098c2ecf20Sopenharmony_ci}
9108c2ecf20Sopenharmony_ciEXPORT_SYMBOL(ivtv_stop_v4l2_encode_stream);
9118c2ecf20Sopenharmony_ci
9128c2ecf20Sopenharmony_ciint ivtv_stop_v4l2_decode_stream(struct ivtv_stream *s, int flags, u64 pts)
9138c2ecf20Sopenharmony_ci{
9148c2ecf20Sopenharmony_ci	static const struct v4l2_event ev = {
9158c2ecf20Sopenharmony_ci		.type = V4L2_EVENT_EOS,
9168c2ecf20Sopenharmony_ci	};
9178c2ecf20Sopenharmony_ci	struct ivtv *itv = s->itv;
9188c2ecf20Sopenharmony_ci
9198c2ecf20Sopenharmony_ci	if (s->vdev.v4l2_dev == NULL)
9208c2ecf20Sopenharmony_ci		return -EINVAL;
9218c2ecf20Sopenharmony_ci
9228c2ecf20Sopenharmony_ci	if (s->type != IVTV_DEC_STREAM_TYPE_YUV && s->type != IVTV_DEC_STREAM_TYPE_MPG)
9238c2ecf20Sopenharmony_ci		return -EINVAL;
9248c2ecf20Sopenharmony_ci
9258c2ecf20Sopenharmony_ci	if (!test_bit(IVTV_F_S_STREAMING, &s->s_flags))
9268c2ecf20Sopenharmony_ci		return 0;
9278c2ecf20Sopenharmony_ci
9288c2ecf20Sopenharmony_ci	IVTV_DEBUG_INFO("Stop Decode at %llu, flags: %x\n", (unsigned long long)pts, flags);
9298c2ecf20Sopenharmony_ci
9308c2ecf20Sopenharmony_ci	/* Stop Decoder */
9318c2ecf20Sopenharmony_ci	if (!(flags & V4L2_DEC_CMD_STOP_IMMEDIATELY) || pts) {
9328c2ecf20Sopenharmony_ci		u32 tmp = 0;
9338c2ecf20Sopenharmony_ci
9348c2ecf20Sopenharmony_ci		/* Wait until the decoder is no longer running */
9358c2ecf20Sopenharmony_ci		if (pts) {
9368c2ecf20Sopenharmony_ci			ivtv_vapi(itv, CX2341X_DEC_STOP_PLAYBACK, 3,
9378c2ecf20Sopenharmony_ci				0, (u32)(pts & 0xffffffff), (u32)(pts >> 32));
9388c2ecf20Sopenharmony_ci		}
9398c2ecf20Sopenharmony_ci		while (1) {
9408c2ecf20Sopenharmony_ci			u32 data[CX2341X_MBOX_MAX_DATA];
9418c2ecf20Sopenharmony_ci			ivtv_vapi_result(itv, data, CX2341X_DEC_GET_XFER_INFO, 0);
9428c2ecf20Sopenharmony_ci			if (s->q_full.buffers + s->q_dma.buffers == 0) {
9438c2ecf20Sopenharmony_ci				if (tmp == data[3])
9448c2ecf20Sopenharmony_ci					break;
9458c2ecf20Sopenharmony_ci				tmp = data[3];
9468c2ecf20Sopenharmony_ci			}
9478c2ecf20Sopenharmony_ci			if (ivtv_msleep_timeout(100, 1))
9488c2ecf20Sopenharmony_ci				break;
9498c2ecf20Sopenharmony_ci		}
9508c2ecf20Sopenharmony_ci	}
9518c2ecf20Sopenharmony_ci	ivtv_vapi(itv, CX2341X_DEC_STOP_PLAYBACK, 3, flags & V4L2_DEC_CMD_STOP_TO_BLACK, 0, 0);
9528c2ecf20Sopenharmony_ci
9538c2ecf20Sopenharmony_ci	/* turn off notification of dual/stereo mode change */
9548c2ecf20Sopenharmony_ci	ivtv_vapi(itv, CX2341X_DEC_SET_EVENT_NOTIFICATION, 4, 0, 0, IVTV_IRQ_DEC_AUD_MODE_CHG, -1);
9558c2ecf20Sopenharmony_ci
9568c2ecf20Sopenharmony_ci	ivtv_set_irq_mask(itv, IVTV_IRQ_MASK_DECODE);
9578c2ecf20Sopenharmony_ci	del_timer(&itv->dma_timer);
9588c2ecf20Sopenharmony_ci
9598c2ecf20Sopenharmony_ci	clear_bit(IVTV_F_S_NEEDS_DATA, &s->s_flags);
9608c2ecf20Sopenharmony_ci	clear_bit(IVTV_F_S_STREAMING, &s->s_flags);
9618c2ecf20Sopenharmony_ci	ivtv_flush_queues(s);
9628c2ecf20Sopenharmony_ci
9638c2ecf20Sopenharmony_ci	/* decoder needs time to settle */
9648c2ecf20Sopenharmony_ci	ivtv_msleep_timeout(40, 0);
9658c2ecf20Sopenharmony_ci
9668c2ecf20Sopenharmony_ci	/* decrement decoding */
9678c2ecf20Sopenharmony_ci	atomic_dec(&itv->decoding);
9688c2ecf20Sopenharmony_ci
9698c2ecf20Sopenharmony_ci	set_bit(IVTV_F_I_EV_DEC_STOPPED, &itv->i_flags);
9708c2ecf20Sopenharmony_ci	wake_up(&itv->event_waitq);
9718c2ecf20Sopenharmony_ci	v4l2_event_queue(&s->vdev, &ev);
9728c2ecf20Sopenharmony_ci
9738c2ecf20Sopenharmony_ci	/* wake up wait queues */
9748c2ecf20Sopenharmony_ci	wake_up(&s->waitq);
9758c2ecf20Sopenharmony_ci
9768c2ecf20Sopenharmony_ci	return 0;
9778c2ecf20Sopenharmony_ci}
9788c2ecf20Sopenharmony_ci
9798c2ecf20Sopenharmony_ciint ivtv_passthrough_mode(struct ivtv *itv, int enable)
9808c2ecf20Sopenharmony_ci{
9818c2ecf20Sopenharmony_ci	struct ivtv_stream *yuv_stream = &itv->streams[IVTV_ENC_STREAM_TYPE_YUV];
9828c2ecf20Sopenharmony_ci	struct ivtv_stream *dec_stream = &itv->streams[IVTV_DEC_STREAM_TYPE_YUV];
9838c2ecf20Sopenharmony_ci
9848c2ecf20Sopenharmony_ci	if (yuv_stream->vdev.v4l2_dev == NULL || dec_stream->vdev.v4l2_dev == NULL)
9858c2ecf20Sopenharmony_ci		return -EINVAL;
9868c2ecf20Sopenharmony_ci
9878c2ecf20Sopenharmony_ci	IVTV_DEBUG_INFO("ivtv ioctl: Select passthrough mode\n");
9888c2ecf20Sopenharmony_ci
9898c2ecf20Sopenharmony_ci	/* Prevent others from starting/stopping streams while we
9908c2ecf20Sopenharmony_ci	   initiate/terminate passthrough mode */
9918c2ecf20Sopenharmony_ci	if (enable) {
9928c2ecf20Sopenharmony_ci		if (itv->output_mode == OUT_PASSTHROUGH) {
9938c2ecf20Sopenharmony_ci			return 0;
9948c2ecf20Sopenharmony_ci		}
9958c2ecf20Sopenharmony_ci		if (ivtv_set_output_mode(itv, OUT_PASSTHROUGH) != OUT_PASSTHROUGH)
9968c2ecf20Sopenharmony_ci			return -EBUSY;
9978c2ecf20Sopenharmony_ci
9988c2ecf20Sopenharmony_ci		/* Fully initialize stream, and then unflag init */
9998c2ecf20Sopenharmony_ci		set_bit(IVTV_F_S_PASSTHROUGH, &dec_stream->s_flags);
10008c2ecf20Sopenharmony_ci		set_bit(IVTV_F_S_STREAMING, &dec_stream->s_flags);
10018c2ecf20Sopenharmony_ci
10028c2ecf20Sopenharmony_ci		/* Setup YUV Decoder */
10038c2ecf20Sopenharmony_ci		ivtv_setup_v4l2_decode_stream(dec_stream);
10048c2ecf20Sopenharmony_ci
10058c2ecf20Sopenharmony_ci		/* Start Decoder */
10068c2ecf20Sopenharmony_ci		ivtv_vapi(itv, CX2341X_DEC_START_PLAYBACK, 2, 0, 1);
10078c2ecf20Sopenharmony_ci		atomic_inc(&itv->decoding);
10088c2ecf20Sopenharmony_ci
10098c2ecf20Sopenharmony_ci		/* Setup capture if not already done */
10108c2ecf20Sopenharmony_ci		if (atomic_read(&itv->capturing) == 0) {
10118c2ecf20Sopenharmony_ci			cx2341x_handler_setup(&itv->cxhdl);
10128c2ecf20Sopenharmony_ci			cx2341x_handler_set_busy(&itv->cxhdl, 1);
10138c2ecf20Sopenharmony_ci		}
10148c2ecf20Sopenharmony_ci
10158c2ecf20Sopenharmony_ci		/* Start Passthrough Mode */
10168c2ecf20Sopenharmony_ci		ivtv_vapi(itv, CX2341X_ENC_START_CAPTURE, 2, 2, 11);
10178c2ecf20Sopenharmony_ci		atomic_inc(&itv->capturing);
10188c2ecf20Sopenharmony_ci		return 0;
10198c2ecf20Sopenharmony_ci	}
10208c2ecf20Sopenharmony_ci
10218c2ecf20Sopenharmony_ci	if (itv->output_mode != OUT_PASSTHROUGH)
10228c2ecf20Sopenharmony_ci		return 0;
10238c2ecf20Sopenharmony_ci
10248c2ecf20Sopenharmony_ci	/* Stop Passthrough Mode */
10258c2ecf20Sopenharmony_ci	ivtv_vapi(itv, CX2341X_ENC_STOP_CAPTURE, 3, 1, 2, 11);
10268c2ecf20Sopenharmony_ci	ivtv_vapi(itv, CX2341X_DEC_STOP_PLAYBACK, 3, 1, 0, 0);
10278c2ecf20Sopenharmony_ci
10288c2ecf20Sopenharmony_ci	atomic_dec(&itv->capturing);
10298c2ecf20Sopenharmony_ci	atomic_dec(&itv->decoding);
10308c2ecf20Sopenharmony_ci	clear_bit(IVTV_F_S_PASSTHROUGH, &dec_stream->s_flags);
10318c2ecf20Sopenharmony_ci	clear_bit(IVTV_F_S_STREAMING, &dec_stream->s_flags);
10328c2ecf20Sopenharmony_ci	itv->output_mode = OUT_NONE;
10338c2ecf20Sopenharmony_ci	if (atomic_read(&itv->capturing) == 0)
10348c2ecf20Sopenharmony_ci		cx2341x_handler_set_busy(&itv->cxhdl, 0);
10358c2ecf20Sopenharmony_ci
10368c2ecf20Sopenharmony_ci	return 0;
10378c2ecf20Sopenharmony_ci}
1038