18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci *  cx18 file operation functions
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci *  Derived from ivtv-fileops.c
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci *  Copyright (C) 2007  Hans Verkuil <hverkuil@xs4all.nl>
88c2ecf20Sopenharmony_ci *  Copyright (C) 2008  Andy Walls <awalls@md.metrocast.net>
98c2ecf20Sopenharmony_ci */
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_ci#include "cx18-driver.h"
128c2ecf20Sopenharmony_ci#include "cx18-fileops.h"
138c2ecf20Sopenharmony_ci#include "cx18-i2c.h"
148c2ecf20Sopenharmony_ci#include "cx18-queue.h"
158c2ecf20Sopenharmony_ci#include "cx18-vbi.h"
168c2ecf20Sopenharmony_ci#include "cx18-audio.h"
178c2ecf20Sopenharmony_ci#include "cx18-mailbox.h"
188c2ecf20Sopenharmony_ci#include "cx18-scb.h"
198c2ecf20Sopenharmony_ci#include "cx18-streams.h"
208c2ecf20Sopenharmony_ci#include "cx18-controls.h"
218c2ecf20Sopenharmony_ci#include "cx18-ioctl.h"
228c2ecf20Sopenharmony_ci#include "cx18-cards.h"
238c2ecf20Sopenharmony_ci#include <media/v4l2-event.h>
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_ci/* This function tries to claim the stream for a specific file descriptor.
268c2ecf20Sopenharmony_ci   If no one else is using this stream then the stream is claimed and
278c2ecf20Sopenharmony_ci   associated VBI and IDX streams are also automatically claimed.
288c2ecf20Sopenharmony_ci   Possible error returns: -EBUSY if someone else has claimed
298c2ecf20Sopenharmony_ci   the stream or 0 on success. */
308c2ecf20Sopenharmony_ciint cx18_claim_stream(struct cx18_open_id *id, int type)
318c2ecf20Sopenharmony_ci{
328c2ecf20Sopenharmony_ci	struct cx18 *cx = id->cx;
338c2ecf20Sopenharmony_ci	struct cx18_stream *s = &cx->streams[type];
348c2ecf20Sopenharmony_ci	struct cx18_stream *s_assoc;
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_ci	/* Nothing should ever try to directly claim the IDX stream */
378c2ecf20Sopenharmony_ci	if (type == CX18_ENC_STREAM_TYPE_IDX) {
388c2ecf20Sopenharmony_ci		CX18_WARN("MPEG Index stream cannot be claimed directly, but something tried.\n");
398c2ecf20Sopenharmony_ci		return -EINVAL;
408c2ecf20Sopenharmony_ci	}
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_ci	if (test_and_set_bit(CX18_F_S_CLAIMED, &s->s_flags)) {
438c2ecf20Sopenharmony_ci		/* someone already claimed this stream */
448c2ecf20Sopenharmony_ci		if (s->id == id->open_id) {
458c2ecf20Sopenharmony_ci			/* yes, this file descriptor did. So that's OK. */
468c2ecf20Sopenharmony_ci			return 0;
478c2ecf20Sopenharmony_ci		}
488c2ecf20Sopenharmony_ci		if (s->id == -1 && type == CX18_ENC_STREAM_TYPE_VBI) {
498c2ecf20Sopenharmony_ci			/* VBI is handled already internally, now also assign
508c2ecf20Sopenharmony_ci			   the file descriptor to this stream for external
518c2ecf20Sopenharmony_ci			   reading of the stream. */
528c2ecf20Sopenharmony_ci			s->id = id->open_id;
538c2ecf20Sopenharmony_ci			CX18_DEBUG_INFO("Start Read VBI\n");
548c2ecf20Sopenharmony_ci			return 0;
558c2ecf20Sopenharmony_ci		}
568c2ecf20Sopenharmony_ci		/* someone else is using this stream already */
578c2ecf20Sopenharmony_ci		CX18_DEBUG_INFO("Stream %d is busy\n", type);
588c2ecf20Sopenharmony_ci		return -EBUSY;
598c2ecf20Sopenharmony_ci	}
608c2ecf20Sopenharmony_ci	s->id = id->open_id;
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_ci	/*
638c2ecf20Sopenharmony_ci	 * CX18_ENC_STREAM_TYPE_MPG needs to claim:
648c2ecf20Sopenharmony_ci	 * CX18_ENC_STREAM_TYPE_VBI, if VBI insertion is on for sliced VBI, or
658c2ecf20Sopenharmony_ci	 * CX18_ENC_STREAM_TYPE_IDX, if VBI insertion is off for sliced VBI
668c2ecf20Sopenharmony_ci	 * (We don't yet fix up MPEG Index entries for our inserted packets).
678c2ecf20Sopenharmony_ci	 *
688c2ecf20Sopenharmony_ci	 * For all other streams we're done.
698c2ecf20Sopenharmony_ci	 */
708c2ecf20Sopenharmony_ci	if (type != CX18_ENC_STREAM_TYPE_MPG)
718c2ecf20Sopenharmony_ci		return 0;
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_ci	s_assoc = &cx->streams[CX18_ENC_STREAM_TYPE_IDX];
748c2ecf20Sopenharmony_ci	if (cx->vbi.insert_mpeg && !cx18_raw_vbi(cx))
758c2ecf20Sopenharmony_ci		s_assoc = &cx->streams[CX18_ENC_STREAM_TYPE_VBI];
768c2ecf20Sopenharmony_ci	else if (!cx18_stream_enabled(s_assoc))
778c2ecf20Sopenharmony_ci		return 0;
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_ci	set_bit(CX18_F_S_CLAIMED, &s_assoc->s_flags);
808c2ecf20Sopenharmony_ci
818c2ecf20Sopenharmony_ci	/* mark that it is used internally */
828c2ecf20Sopenharmony_ci	set_bit(CX18_F_S_INTERNAL_USE, &s_assoc->s_flags);
838c2ecf20Sopenharmony_ci	return 0;
848c2ecf20Sopenharmony_ci}
858c2ecf20Sopenharmony_ciEXPORT_SYMBOL(cx18_claim_stream);
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_ci/* This function releases a previously claimed stream. It will take into
888c2ecf20Sopenharmony_ci   account associated VBI streams. */
898c2ecf20Sopenharmony_civoid cx18_release_stream(struct cx18_stream *s)
908c2ecf20Sopenharmony_ci{
918c2ecf20Sopenharmony_ci	struct cx18 *cx = s->cx;
928c2ecf20Sopenharmony_ci	struct cx18_stream *s_assoc;
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_ci	s->id = -1;
958c2ecf20Sopenharmony_ci	if (s->type == CX18_ENC_STREAM_TYPE_IDX) {
968c2ecf20Sopenharmony_ci		/*
978c2ecf20Sopenharmony_ci		 * The IDX stream is only used internally, and can
988c2ecf20Sopenharmony_ci		 * only be indirectly unclaimed by unclaiming the MPG stream.
998c2ecf20Sopenharmony_ci		 */
1008c2ecf20Sopenharmony_ci		return;
1018c2ecf20Sopenharmony_ci	}
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_ci	if (s->type == CX18_ENC_STREAM_TYPE_VBI &&
1048c2ecf20Sopenharmony_ci		test_bit(CX18_F_S_INTERNAL_USE, &s->s_flags)) {
1058c2ecf20Sopenharmony_ci		/* this stream is still in use internally */
1068c2ecf20Sopenharmony_ci		return;
1078c2ecf20Sopenharmony_ci	}
1088c2ecf20Sopenharmony_ci	if (!test_and_clear_bit(CX18_F_S_CLAIMED, &s->s_flags)) {
1098c2ecf20Sopenharmony_ci		CX18_DEBUG_WARN("Release stream %s not in use!\n", s->name);
1108c2ecf20Sopenharmony_ci		return;
1118c2ecf20Sopenharmony_ci	}
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_ci	cx18_flush_queues(s);
1148c2ecf20Sopenharmony_ci
1158c2ecf20Sopenharmony_ci	/*
1168c2ecf20Sopenharmony_ci	 * CX18_ENC_STREAM_TYPE_MPG needs to release the
1178c2ecf20Sopenharmony_ci	 * CX18_ENC_STREAM_TYPE_VBI and/or CX18_ENC_STREAM_TYPE_IDX streams.
1188c2ecf20Sopenharmony_ci	 *
1198c2ecf20Sopenharmony_ci	 * For all other streams we're done.
1208c2ecf20Sopenharmony_ci	 */
1218c2ecf20Sopenharmony_ci	if (s->type != CX18_ENC_STREAM_TYPE_MPG)
1228c2ecf20Sopenharmony_ci		return;
1238c2ecf20Sopenharmony_ci
1248c2ecf20Sopenharmony_ci	/* Unclaim the associated MPEG Index stream */
1258c2ecf20Sopenharmony_ci	s_assoc = &cx->streams[CX18_ENC_STREAM_TYPE_IDX];
1268c2ecf20Sopenharmony_ci	if (test_and_clear_bit(CX18_F_S_INTERNAL_USE, &s_assoc->s_flags)) {
1278c2ecf20Sopenharmony_ci		clear_bit(CX18_F_S_CLAIMED, &s_assoc->s_flags);
1288c2ecf20Sopenharmony_ci		cx18_flush_queues(s_assoc);
1298c2ecf20Sopenharmony_ci	}
1308c2ecf20Sopenharmony_ci
1318c2ecf20Sopenharmony_ci	/* Unclaim the associated VBI stream */
1328c2ecf20Sopenharmony_ci	s_assoc = &cx->streams[CX18_ENC_STREAM_TYPE_VBI];
1338c2ecf20Sopenharmony_ci	if (test_and_clear_bit(CX18_F_S_INTERNAL_USE, &s_assoc->s_flags)) {
1348c2ecf20Sopenharmony_ci		if (s_assoc->id == -1) {
1358c2ecf20Sopenharmony_ci			/*
1368c2ecf20Sopenharmony_ci			 * The VBI stream is not still claimed by a file
1378c2ecf20Sopenharmony_ci			 * descriptor, so completely unclaim it.
1388c2ecf20Sopenharmony_ci			 */
1398c2ecf20Sopenharmony_ci			clear_bit(CX18_F_S_CLAIMED, &s_assoc->s_flags);
1408c2ecf20Sopenharmony_ci			cx18_flush_queues(s_assoc);
1418c2ecf20Sopenharmony_ci		}
1428c2ecf20Sopenharmony_ci	}
1438c2ecf20Sopenharmony_ci}
1448c2ecf20Sopenharmony_ciEXPORT_SYMBOL(cx18_release_stream);
1458c2ecf20Sopenharmony_ci
1468c2ecf20Sopenharmony_cistatic void cx18_dualwatch(struct cx18 *cx)
1478c2ecf20Sopenharmony_ci{
1488c2ecf20Sopenharmony_ci	struct v4l2_tuner vt;
1498c2ecf20Sopenharmony_ci	u32 new_stereo_mode;
1508c2ecf20Sopenharmony_ci	const u32 dual = 0x0200;
1518c2ecf20Sopenharmony_ci
1528c2ecf20Sopenharmony_ci	new_stereo_mode = v4l2_ctrl_g_ctrl(cx->cxhdl.audio_mode);
1538c2ecf20Sopenharmony_ci	memset(&vt, 0, sizeof(vt));
1548c2ecf20Sopenharmony_ci	cx18_call_all(cx, tuner, g_tuner, &vt);
1558c2ecf20Sopenharmony_ci	if (vt.audmode == V4L2_TUNER_MODE_LANG1_LANG2 &&
1568c2ecf20Sopenharmony_ci			(vt.rxsubchans & V4L2_TUNER_SUB_LANG2))
1578c2ecf20Sopenharmony_ci		new_stereo_mode = dual;
1588c2ecf20Sopenharmony_ci
1598c2ecf20Sopenharmony_ci	if (new_stereo_mode == cx->dualwatch_stereo_mode)
1608c2ecf20Sopenharmony_ci		return;
1618c2ecf20Sopenharmony_ci
1628c2ecf20Sopenharmony_ci	CX18_DEBUG_INFO("dualwatch: change stereo flag from 0x%x to 0x%x.\n",
1638c2ecf20Sopenharmony_ci			   cx->dualwatch_stereo_mode, new_stereo_mode);
1648c2ecf20Sopenharmony_ci	if (v4l2_ctrl_s_ctrl(cx->cxhdl.audio_mode, new_stereo_mode))
1658c2ecf20Sopenharmony_ci		CX18_DEBUG_INFO("dualwatch: changing stereo flag failed\n");
1668c2ecf20Sopenharmony_ci}
1678c2ecf20Sopenharmony_ci
1688c2ecf20Sopenharmony_ci
1698c2ecf20Sopenharmony_cistatic struct cx18_mdl *cx18_get_mdl(struct cx18_stream *s, int non_block,
1708c2ecf20Sopenharmony_ci				     int *err)
1718c2ecf20Sopenharmony_ci{
1728c2ecf20Sopenharmony_ci	struct cx18 *cx = s->cx;
1738c2ecf20Sopenharmony_ci	struct cx18_stream *s_vbi = &cx->streams[CX18_ENC_STREAM_TYPE_VBI];
1748c2ecf20Sopenharmony_ci	struct cx18_mdl *mdl;
1758c2ecf20Sopenharmony_ci	DEFINE_WAIT(wait);
1768c2ecf20Sopenharmony_ci
1778c2ecf20Sopenharmony_ci	*err = 0;
1788c2ecf20Sopenharmony_ci	while (1) {
1798c2ecf20Sopenharmony_ci		if (s->type == CX18_ENC_STREAM_TYPE_MPG) {
1808c2ecf20Sopenharmony_ci			/* Process pending program updates and VBI data */
1818c2ecf20Sopenharmony_ci			if (time_after(jiffies, cx->dualwatch_jiffies + msecs_to_jiffies(1000))) {
1828c2ecf20Sopenharmony_ci				cx->dualwatch_jiffies = jiffies;
1838c2ecf20Sopenharmony_ci				cx18_dualwatch(cx);
1848c2ecf20Sopenharmony_ci			}
1858c2ecf20Sopenharmony_ci			if (test_bit(CX18_F_S_INTERNAL_USE, &s_vbi->s_flags) &&
1868c2ecf20Sopenharmony_ci			    !test_bit(CX18_F_S_APPL_IO, &s_vbi->s_flags)) {
1878c2ecf20Sopenharmony_ci				while ((mdl = cx18_dequeue(s_vbi,
1888c2ecf20Sopenharmony_ci							   &s_vbi->q_full))) {
1898c2ecf20Sopenharmony_ci					/* byteswap and process VBI data */
1908c2ecf20Sopenharmony_ci					cx18_process_vbi_data(cx, mdl,
1918c2ecf20Sopenharmony_ci							      s_vbi->type);
1928c2ecf20Sopenharmony_ci					cx18_stream_put_mdl_fw(s_vbi, mdl);
1938c2ecf20Sopenharmony_ci				}
1948c2ecf20Sopenharmony_ci			}
1958c2ecf20Sopenharmony_ci			mdl = &cx->vbi.sliced_mpeg_mdl;
1968c2ecf20Sopenharmony_ci			if (mdl->readpos != mdl->bytesused)
1978c2ecf20Sopenharmony_ci				return mdl;
1988c2ecf20Sopenharmony_ci		}
1998c2ecf20Sopenharmony_ci
2008c2ecf20Sopenharmony_ci		/* do we have new data? */
2018c2ecf20Sopenharmony_ci		mdl = cx18_dequeue(s, &s->q_full);
2028c2ecf20Sopenharmony_ci		if (mdl) {
2038c2ecf20Sopenharmony_ci			if (!test_and_clear_bit(CX18_F_M_NEED_SWAP,
2048c2ecf20Sopenharmony_ci						&mdl->m_flags))
2058c2ecf20Sopenharmony_ci				return mdl;
2068c2ecf20Sopenharmony_ci			if (s->type == CX18_ENC_STREAM_TYPE_MPG)
2078c2ecf20Sopenharmony_ci				/* byteswap MPG data */
2088c2ecf20Sopenharmony_ci				cx18_mdl_swap(mdl);
2098c2ecf20Sopenharmony_ci			else {
2108c2ecf20Sopenharmony_ci				/* byteswap and process VBI data */
2118c2ecf20Sopenharmony_ci				cx18_process_vbi_data(cx, mdl, s->type);
2128c2ecf20Sopenharmony_ci			}
2138c2ecf20Sopenharmony_ci			return mdl;
2148c2ecf20Sopenharmony_ci		}
2158c2ecf20Sopenharmony_ci
2168c2ecf20Sopenharmony_ci		/* return if end of stream */
2178c2ecf20Sopenharmony_ci		if (!test_bit(CX18_F_S_STREAMING, &s->s_flags)) {
2188c2ecf20Sopenharmony_ci			CX18_DEBUG_INFO("EOS %s\n", s->name);
2198c2ecf20Sopenharmony_ci			return NULL;
2208c2ecf20Sopenharmony_ci		}
2218c2ecf20Sopenharmony_ci
2228c2ecf20Sopenharmony_ci		/* return if file was opened with O_NONBLOCK */
2238c2ecf20Sopenharmony_ci		if (non_block) {
2248c2ecf20Sopenharmony_ci			*err = -EAGAIN;
2258c2ecf20Sopenharmony_ci			return NULL;
2268c2ecf20Sopenharmony_ci		}
2278c2ecf20Sopenharmony_ci
2288c2ecf20Sopenharmony_ci		/* wait for more data to arrive */
2298c2ecf20Sopenharmony_ci		prepare_to_wait(&s->waitq, &wait, TASK_INTERRUPTIBLE);
2308c2ecf20Sopenharmony_ci		/* New buffers might have become available before we were added
2318c2ecf20Sopenharmony_ci		   to the waitqueue */
2328c2ecf20Sopenharmony_ci		if (!atomic_read(&s->q_full.depth))
2338c2ecf20Sopenharmony_ci			schedule();
2348c2ecf20Sopenharmony_ci		finish_wait(&s->waitq, &wait);
2358c2ecf20Sopenharmony_ci		if (signal_pending(current)) {
2368c2ecf20Sopenharmony_ci			/* return if a signal was received */
2378c2ecf20Sopenharmony_ci			CX18_DEBUG_INFO("User stopped %s\n", s->name);
2388c2ecf20Sopenharmony_ci			*err = -EINTR;
2398c2ecf20Sopenharmony_ci			return NULL;
2408c2ecf20Sopenharmony_ci		}
2418c2ecf20Sopenharmony_ci	}
2428c2ecf20Sopenharmony_ci}
2438c2ecf20Sopenharmony_ci
2448c2ecf20Sopenharmony_cistatic void cx18_setup_sliced_vbi_mdl(struct cx18 *cx)
2458c2ecf20Sopenharmony_ci{
2468c2ecf20Sopenharmony_ci	struct cx18_mdl *mdl = &cx->vbi.sliced_mpeg_mdl;
2478c2ecf20Sopenharmony_ci	struct cx18_buffer *buf = &cx->vbi.sliced_mpeg_buf;
2488c2ecf20Sopenharmony_ci	int idx = cx->vbi.inserted_frame % CX18_VBI_FRAMES;
2498c2ecf20Sopenharmony_ci
2508c2ecf20Sopenharmony_ci	buf->buf = cx->vbi.sliced_mpeg_data[idx];
2518c2ecf20Sopenharmony_ci	buf->bytesused = cx->vbi.sliced_mpeg_size[idx];
2528c2ecf20Sopenharmony_ci	buf->readpos = 0;
2538c2ecf20Sopenharmony_ci
2548c2ecf20Sopenharmony_ci	mdl->curr_buf = NULL;
2558c2ecf20Sopenharmony_ci	mdl->bytesused = cx->vbi.sliced_mpeg_size[idx];
2568c2ecf20Sopenharmony_ci	mdl->readpos = 0;
2578c2ecf20Sopenharmony_ci}
2588c2ecf20Sopenharmony_ci
2598c2ecf20Sopenharmony_cistatic size_t cx18_copy_buf_to_user(struct cx18_stream *s,
2608c2ecf20Sopenharmony_ci	struct cx18_buffer *buf, char __user *ubuf, size_t ucount, bool *stop)
2618c2ecf20Sopenharmony_ci{
2628c2ecf20Sopenharmony_ci	struct cx18 *cx = s->cx;
2638c2ecf20Sopenharmony_ci	size_t len = buf->bytesused - buf->readpos;
2648c2ecf20Sopenharmony_ci
2658c2ecf20Sopenharmony_ci	*stop = false;
2668c2ecf20Sopenharmony_ci	if (len > ucount)
2678c2ecf20Sopenharmony_ci		len = ucount;
2688c2ecf20Sopenharmony_ci	if (cx->vbi.insert_mpeg && s->type == CX18_ENC_STREAM_TYPE_MPG &&
2698c2ecf20Sopenharmony_ci	    !cx18_raw_vbi(cx) && buf != &cx->vbi.sliced_mpeg_buf) {
2708c2ecf20Sopenharmony_ci		/*
2718c2ecf20Sopenharmony_ci		 * Try to find a good splice point in the PS, just before
2728c2ecf20Sopenharmony_ci		 * an MPEG-2 Program Pack start code, and provide only
2738c2ecf20Sopenharmony_ci		 * up to that point to the user, so it's easy to insert VBI data
2748c2ecf20Sopenharmony_ci		 * the next time around.
2758c2ecf20Sopenharmony_ci		 *
2768c2ecf20Sopenharmony_ci		 * This will not work for an MPEG-2 TS and has only been
2778c2ecf20Sopenharmony_ci		 * verified by analysis to work for an MPEG-2 PS.  Helen Buus
2788c2ecf20Sopenharmony_ci		 * pointed out this works for the CX23416 MPEG-2 DVD compatible
2798c2ecf20Sopenharmony_ci		 * stream, and research indicates both the MPEG 2 SVCD and DVD
2808c2ecf20Sopenharmony_ci		 * stream types use an MPEG-2 PS container.
2818c2ecf20Sopenharmony_ci		 */
2828c2ecf20Sopenharmony_ci		/*
2838c2ecf20Sopenharmony_ci		 * An MPEG-2 Program Stream (PS) is a series of
2848c2ecf20Sopenharmony_ci		 * MPEG-2 Program Packs terminated by an
2858c2ecf20Sopenharmony_ci		 * MPEG Program End Code after the last Program Pack.
2868c2ecf20Sopenharmony_ci		 * A Program Pack may hold a PS System Header packet and any
2878c2ecf20Sopenharmony_ci		 * number of Program Elementary Stream (PES) Packets
2888c2ecf20Sopenharmony_ci		 */
2898c2ecf20Sopenharmony_ci		const char *start = buf->buf + buf->readpos;
2908c2ecf20Sopenharmony_ci		const char *p = start + 1;
2918c2ecf20Sopenharmony_ci		const u8 *q;
2928c2ecf20Sopenharmony_ci		u8 ch = cx->search_pack_header ? 0xba : 0xe0;
2938c2ecf20Sopenharmony_ci		int stuffing, i;
2948c2ecf20Sopenharmony_ci
2958c2ecf20Sopenharmony_ci		while (start + len > p) {
2968c2ecf20Sopenharmony_ci			/* Scan for a 0 to find a potential MPEG-2 start code */
2978c2ecf20Sopenharmony_ci			q = memchr(p, 0, start + len - p);
2988c2ecf20Sopenharmony_ci			if (q == NULL)
2998c2ecf20Sopenharmony_ci				break;
3008c2ecf20Sopenharmony_ci			p = q + 1;
3018c2ecf20Sopenharmony_ci			/*
3028c2ecf20Sopenharmony_ci			 * Keep looking if not a
3038c2ecf20Sopenharmony_ci			 * MPEG-2 Pack header start code:  0x00 0x00 0x01 0xba
3048c2ecf20Sopenharmony_ci			 * or MPEG-2 video PES start code: 0x00 0x00 0x01 0xe0
3058c2ecf20Sopenharmony_ci			 */
3068c2ecf20Sopenharmony_ci			if ((char *)q + 15 >= buf->buf + buf->bytesused ||
3078c2ecf20Sopenharmony_ci			    q[1] != 0 || q[2] != 1 || q[3] != ch)
3088c2ecf20Sopenharmony_ci				continue;
3098c2ecf20Sopenharmony_ci
3108c2ecf20Sopenharmony_ci			/* If expecting the primary video PES */
3118c2ecf20Sopenharmony_ci			if (!cx->search_pack_header) {
3128c2ecf20Sopenharmony_ci				/* Continue if it couldn't be a PES packet */
3138c2ecf20Sopenharmony_ci				if ((q[6] & 0xc0) != 0x80)
3148c2ecf20Sopenharmony_ci					continue;
3158c2ecf20Sopenharmony_ci				/* Check if a PTS or PTS & DTS follow */
3168c2ecf20Sopenharmony_ci				if (((q[7] & 0xc0) == 0x80 &&  /* PTS only */
3178c2ecf20Sopenharmony_ci				     (q[9] & 0xf0) == 0x20) || /* PTS only */
3188c2ecf20Sopenharmony_ci				    ((q[7] & 0xc0) == 0xc0 &&  /* PTS & DTS */
3198c2ecf20Sopenharmony_ci				     (q[9] & 0xf0) == 0x30)) { /* DTS follows */
3208c2ecf20Sopenharmony_ci					/* Assume we found the video PES hdr */
3218c2ecf20Sopenharmony_ci					ch = 0xba; /* next want a Program Pack*/
3228c2ecf20Sopenharmony_ci					cx->search_pack_header = 1;
3238c2ecf20Sopenharmony_ci					p = q + 9; /* Skip this video PES hdr */
3248c2ecf20Sopenharmony_ci				}
3258c2ecf20Sopenharmony_ci				continue;
3268c2ecf20Sopenharmony_ci			}
3278c2ecf20Sopenharmony_ci
3288c2ecf20Sopenharmony_ci			/* We may have found a Program Pack start code */
3298c2ecf20Sopenharmony_ci
3308c2ecf20Sopenharmony_ci			/* Get the count of stuffing bytes & verify them */
3318c2ecf20Sopenharmony_ci			stuffing = q[13] & 7;
3328c2ecf20Sopenharmony_ci			/* all stuffing bytes must be 0xff */
3338c2ecf20Sopenharmony_ci			for (i = 0; i < stuffing; i++)
3348c2ecf20Sopenharmony_ci				if (q[14 + i] != 0xff)
3358c2ecf20Sopenharmony_ci					break;
3368c2ecf20Sopenharmony_ci			if (i == stuffing && /* right number of stuffing bytes*/
3378c2ecf20Sopenharmony_ci			    (q[4] & 0xc4) == 0x44 && /* marker check */
3388c2ecf20Sopenharmony_ci			    (q[12] & 3) == 3 &&  /* marker check */
3398c2ecf20Sopenharmony_ci			    q[14 + stuffing] == 0 && /* PES Pack or Sys Hdr */
3408c2ecf20Sopenharmony_ci			    q[15 + stuffing] == 0 &&
3418c2ecf20Sopenharmony_ci			    q[16 + stuffing] == 1) {
3428c2ecf20Sopenharmony_ci				/* We declare we actually found a Program Pack*/
3438c2ecf20Sopenharmony_ci				cx->search_pack_header = 0; /* expect vid PES */
3448c2ecf20Sopenharmony_ci				len = (char *)q - start;
3458c2ecf20Sopenharmony_ci				cx18_setup_sliced_vbi_mdl(cx);
3468c2ecf20Sopenharmony_ci				*stop = true;
3478c2ecf20Sopenharmony_ci				break;
3488c2ecf20Sopenharmony_ci			}
3498c2ecf20Sopenharmony_ci		}
3508c2ecf20Sopenharmony_ci	}
3518c2ecf20Sopenharmony_ci	if (copy_to_user(ubuf, (u8 *)buf->buf + buf->readpos, len)) {
3528c2ecf20Sopenharmony_ci		CX18_DEBUG_WARN("copy %zd bytes to user failed for %s\n",
3538c2ecf20Sopenharmony_ci				len, s->name);
3548c2ecf20Sopenharmony_ci		return -EFAULT;
3558c2ecf20Sopenharmony_ci	}
3568c2ecf20Sopenharmony_ci	buf->readpos += len;
3578c2ecf20Sopenharmony_ci	if (s->type == CX18_ENC_STREAM_TYPE_MPG &&
3588c2ecf20Sopenharmony_ci	    buf != &cx->vbi.sliced_mpeg_buf)
3598c2ecf20Sopenharmony_ci		cx->mpg_data_received += len;
3608c2ecf20Sopenharmony_ci	return len;
3618c2ecf20Sopenharmony_ci}
3628c2ecf20Sopenharmony_ci
3638c2ecf20Sopenharmony_cistatic size_t cx18_copy_mdl_to_user(struct cx18_stream *s,
3648c2ecf20Sopenharmony_ci		struct cx18_mdl *mdl, char __user *ubuf, size_t ucount)
3658c2ecf20Sopenharmony_ci{
3668c2ecf20Sopenharmony_ci	size_t tot_written = 0;
3678c2ecf20Sopenharmony_ci	int rc;
3688c2ecf20Sopenharmony_ci	bool stop = false;
3698c2ecf20Sopenharmony_ci
3708c2ecf20Sopenharmony_ci	if (mdl->curr_buf == NULL)
3718c2ecf20Sopenharmony_ci		mdl->curr_buf = list_first_entry(&mdl->buf_list,
3728c2ecf20Sopenharmony_ci						 struct cx18_buffer, list);
3738c2ecf20Sopenharmony_ci
3748c2ecf20Sopenharmony_ci	if (list_entry_is_past_end(mdl->curr_buf, &mdl->buf_list, list)) {
3758c2ecf20Sopenharmony_ci		/*
3768c2ecf20Sopenharmony_ci		 * For some reason we've exhausted the buffers, but the MDL
3778c2ecf20Sopenharmony_ci		 * object still said some data was unread.
3788c2ecf20Sopenharmony_ci		 * Fix that and bail out.
3798c2ecf20Sopenharmony_ci		 */
3808c2ecf20Sopenharmony_ci		mdl->readpos = mdl->bytesused;
3818c2ecf20Sopenharmony_ci		return 0;
3828c2ecf20Sopenharmony_ci	}
3838c2ecf20Sopenharmony_ci
3848c2ecf20Sopenharmony_ci	list_for_each_entry_from(mdl->curr_buf, &mdl->buf_list, list) {
3858c2ecf20Sopenharmony_ci
3868c2ecf20Sopenharmony_ci		if (mdl->curr_buf->readpos >= mdl->curr_buf->bytesused)
3878c2ecf20Sopenharmony_ci			continue;
3888c2ecf20Sopenharmony_ci
3898c2ecf20Sopenharmony_ci		rc = cx18_copy_buf_to_user(s, mdl->curr_buf, ubuf + tot_written,
3908c2ecf20Sopenharmony_ci					   ucount - tot_written, &stop);
3918c2ecf20Sopenharmony_ci		if (rc < 0)
3928c2ecf20Sopenharmony_ci			return rc;
3938c2ecf20Sopenharmony_ci		mdl->readpos += rc;
3948c2ecf20Sopenharmony_ci		tot_written += rc;
3958c2ecf20Sopenharmony_ci
3968c2ecf20Sopenharmony_ci		if (stop ||	/* Forced stopping point for VBI insertion */
3978c2ecf20Sopenharmony_ci		    tot_written >= ucount ||	/* Reader request satisfied */
3988c2ecf20Sopenharmony_ci		    mdl->curr_buf->readpos < mdl->curr_buf->bytesused ||
3998c2ecf20Sopenharmony_ci		    mdl->readpos >= mdl->bytesused) /* MDL buffers drained */
4008c2ecf20Sopenharmony_ci			break;
4018c2ecf20Sopenharmony_ci	}
4028c2ecf20Sopenharmony_ci	return tot_written;
4038c2ecf20Sopenharmony_ci}
4048c2ecf20Sopenharmony_ci
4058c2ecf20Sopenharmony_cistatic ssize_t cx18_read(struct cx18_stream *s, char __user *ubuf,
4068c2ecf20Sopenharmony_ci		size_t tot_count, int non_block)
4078c2ecf20Sopenharmony_ci{
4088c2ecf20Sopenharmony_ci	struct cx18 *cx = s->cx;
4098c2ecf20Sopenharmony_ci	size_t tot_written = 0;
4108c2ecf20Sopenharmony_ci	int single_frame = 0;
4118c2ecf20Sopenharmony_ci
4128c2ecf20Sopenharmony_ci	if (atomic_read(&cx->ana_capturing) == 0 && s->id == -1) {
4138c2ecf20Sopenharmony_ci		/* shouldn't happen */
4148c2ecf20Sopenharmony_ci		CX18_DEBUG_WARN("Stream %s not initialized before read\n",
4158c2ecf20Sopenharmony_ci				s->name);
4168c2ecf20Sopenharmony_ci		return -EIO;
4178c2ecf20Sopenharmony_ci	}
4188c2ecf20Sopenharmony_ci
4198c2ecf20Sopenharmony_ci	/* Each VBI buffer is one frame, the v4l2 API says that for VBI the
4208c2ecf20Sopenharmony_ci	   frames should arrive one-by-one, so make sure we never output more
4218c2ecf20Sopenharmony_ci	   than one VBI frame at a time */
4228c2ecf20Sopenharmony_ci	if (s->type == CX18_ENC_STREAM_TYPE_VBI && !cx18_raw_vbi(cx))
4238c2ecf20Sopenharmony_ci		single_frame = 1;
4248c2ecf20Sopenharmony_ci
4258c2ecf20Sopenharmony_ci	for (;;) {
4268c2ecf20Sopenharmony_ci		struct cx18_mdl *mdl;
4278c2ecf20Sopenharmony_ci		int rc;
4288c2ecf20Sopenharmony_ci
4298c2ecf20Sopenharmony_ci		mdl = cx18_get_mdl(s, non_block, &rc);
4308c2ecf20Sopenharmony_ci		/* if there is no data available... */
4318c2ecf20Sopenharmony_ci		if (mdl == NULL) {
4328c2ecf20Sopenharmony_ci			/* if we got data, then return that regardless */
4338c2ecf20Sopenharmony_ci			if (tot_written)
4348c2ecf20Sopenharmony_ci				break;
4358c2ecf20Sopenharmony_ci			/* EOS condition */
4368c2ecf20Sopenharmony_ci			if (rc == 0) {
4378c2ecf20Sopenharmony_ci				clear_bit(CX18_F_S_STREAMOFF, &s->s_flags);
4388c2ecf20Sopenharmony_ci				clear_bit(CX18_F_S_APPL_IO, &s->s_flags);
4398c2ecf20Sopenharmony_ci				cx18_release_stream(s);
4408c2ecf20Sopenharmony_ci			}
4418c2ecf20Sopenharmony_ci			/* set errno */
4428c2ecf20Sopenharmony_ci			return rc;
4438c2ecf20Sopenharmony_ci		}
4448c2ecf20Sopenharmony_ci
4458c2ecf20Sopenharmony_ci		rc = cx18_copy_mdl_to_user(s, mdl, ubuf + tot_written,
4468c2ecf20Sopenharmony_ci				tot_count - tot_written);
4478c2ecf20Sopenharmony_ci
4488c2ecf20Sopenharmony_ci		if (mdl != &cx->vbi.sliced_mpeg_mdl) {
4498c2ecf20Sopenharmony_ci			if (mdl->readpos == mdl->bytesused)
4508c2ecf20Sopenharmony_ci				cx18_stream_put_mdl_fw(s, mdl);
4518c2ecf20Sopenharmony_ci			else
4528c2ecf20Sopenharmony_ci				cx18_push(s, mdl, &s->q_full);
4538c2ecf20Sopenharmony_ci		} else if (mdl->readpos == mdl->bytesused) {
4548c2ecf20Sopenharmony_ci			int idx = cx->vbi.inserted_frame % CX18_VBI_FRAMES;
4558c2ecf20Sopenharmony_ci
4568c2ecf20Sopenharmony_ci			cx->vbi.sliced_mpeg_size[idx] = 0;
4578c2ecf20Sopenharmony_ci			cx->vbi.inserted_frame++;
4588c2ecf20Sopenharmony_ci			cx->vbi_data_inserted += mdl->bytesused;
4598c2ecf20Sopenharmony_ci		}
4608c2ecf20Sopenharmony_ci		if (rc < 0)
4618c2ecf20Sopenharmony_ci			return rc;
4628c2ecf20Sopenharmony_ci		tot_written += rc;
4638c2ecf20Sopenharmony_ci
4648c2ecf20Sopenharmony_ci		if (tot_written == tot_count || single_frame)
4658c2ecf20Sopenharmony_ci			break;
4668c2ecf20Sopenharmony_ci	}
4678c2ecf20Sopenharmony_ci	return tot_written;
4688c2ecf20Sopenharmony_ci}
4698c2ecf20Sopenharmony_ci
4708c2ecf20Sopenharmony_cistatic ssize_t cx18_read_pos(struct cx18_stream *s, char __user *ubuf,
4718c2ecf20Sopenharmony_ci		size_t count, loff_t *pos, int non_block)
4728c2ecf20Sopenharmony_ci{
4738c2ecf20Sopenharmony_ci	ssize_t rc = count ? cx18_read(s, ubuf, count, non_block) : 0;
4748c2ecf20Sopenharmony_ci	struct cx18 *cx = s->cx;
4758c2ecf20Sopenharmony_ci
4768c2ecf20Sopenharmony_ci	CX18_DEBUG_HI_FILE("read %zd from %s, got %zd\n", count, s->name, rc);
4778c2ecf20Sopenharmony_ci	if (rc > 0)
4788c2ecf20Sopenharmony_ci		*pos += rc;
4798c2ecf20Sopenharmony_ci	return rc;
4808c2ecf20Sopenharmony_ci}
4818c2ecf20Sopenharmony_ci
4828c2ecf20Sopenharmony_ciint cx18_start_capture(struct cx18_open_id *id)
4838c2ecf20Sopenharmony_ci{
4848c2ecf20Sopenharmony_ci	struct cx18 *cx = id->cx;
4858c2ecf20Sopenharmony_ci	struct cx18_stream *s = &cx->streams[id->type];
4868c2ecf20Sopenharmony_ci	struct cx18_stream *s_vbi;
4878c2ecf20Sopenharmony_ci	struct cx18_stream *s_idx;
4888c2ecf20Sopenharmony_ci
4898c2ecf20Sopenharmony_ci	if (s->type == CX18_ENC_STREAM_TYPE_RAD) {
4908c2ecf20Sopenharmony_ci		/* you cannot read from these stream types. */
4918c2ecf20Sopenharmony_ci		return -EPERM;
4928c2ecf20Sopenharmony_ci	}
4938c2ecf20Sopenharmony_ci
4948c2ecf20Sopenharmony_ci	/* Try to claim this stream. */
4958c2ecf20Sopenharmony_ci	if (cx18_claim_stream(id, s->type))
4968c2ecf20Sopenharmony_ci		return -EBUSY;
4978c2ecf20Sopenharmony_ci
4988c2ecf20Sopenharmony_ci	/* If capture is already in progress, then we also have to
4998c2ecf20Sopenharmony_ci	   do nothing extra. */
5008c2ecf20Sopenharmony_ci	if (test_bit(CX18_F_S_STREAMOFF, &s->s_flags) ||
5018c2ecf20Sopenharmony_ci	    test_and_set_bit(CX18_F_S_STREAMING, &s->s_flags)) {
5028c2ecf20Sopenharmony_ci		set_bit(CX18_F_S_APPL_IO, &s->s_flags);
5038c2ecf20Sopenharmony_ci		return 0;
5048c2ecf20Sopenharmony_ci	}
5058c2ecf20Sopenharmony_ci
5068c2ecf20Sopenharmony_ci	/* Start associated VBI or IDX stream capture if required */
5078c2ecf20Sopenharmony_ci	s_vbi = &cx->streams[CX18_ENC_STREAM_TYPE_VBI];
5088c2ecf20Sopenharmony_ci	s_idx = &cx->streams[CX18_ENC_STREAM_TYPE_IDX];
5098c2ecf20Sopenharmony_ci	if (s->type == CX18_ENC_STREAM_TYPE_MPG) {
5108c2ecf20Sopenharmony_ci		/*
5118c2ecf20Sopenharmony_ci		 * The VBI and IDX streams should have been claimed
5128c2ecf20Sopenharmony_ci		 * automatically, if for internal use, when the MPG stream was
5138c2ecf20Sopenharmony_ci		 * claimed.  We only need to start these streams capturing.
5148c2ecf20Sopenharmony_ci		 */
5158c2ecf20Sopenharmony_ci		if (test_bit(CX18_F_S_INTERNAL_USE, &s_idx->s_flags) &&
5168c2ecf20Sopenharmony_ci		    !test_and_set_bit(CX18_F_S_STREAMING, &s_idx->s_flags)) {
5178c2ecf20Sopenharmony_ci			if (cx18_start_v4l2_encode_stream(s_idx)) {
5188c2ecf20Sopenharmony_ci				CX18_DEBUG_WARN("IDX capture start failed\n");
5198c2ecf20Sopenharmony_ci				clear_bit(CX18_F_S_STREAMING, &s_idx->s_flags);
5208c2ecf20Sopenharmony_ci				goto start_failed;
5218c2ecf20Sopenharmony_ci			}
5228c2ecf20Sopenharmony_ci			CX18_DEBUG_INFO("IDX capture started\n");
5238c2ecf20Sopenharmony_ci		}
5248c2ecf20Sopenharmony_ci		if (test_bit(CX18_F_S_INTERNAL_USE, &s_vbi->s_flags) &&
5258c2ecf20Sopenharmony_ci		    !test_and_set_bit(CX18_F_S_STREAMING, &s_vbi->s_flags)) {
5268c2ecf20Sopenharmony_ci			if (cx18_start_v4l2_encode_stream(s_vbi)) {
5278c2ecf20Sopenharmony_ci				CX18_DEBUG_WARN("VBI capture start failed\n");
5288c2ecf20Sopenharmony_ci				clear_bit(CX18_F_S_STREAMING, &s_vbi->s_flags);
5298c2ecf20Sopenharmony_ci				goto start_failed;
5308c2ecf20Sopenharmony_ci			}
5318c2ecf20Sopenharmony_ci			CX18_DEBUG_INFO("VBI insertion started\n");
5328c2ecf20Sopenharmony_ci		}
5338c2ecf20Sopenharmony_ci	}
5348c2ecf20Sopenharmony_ci
5358c2ecf20Sopenharmony_ci	/* Tell the card to start capturing */
5368c2ecf20Sopenharmony_ci	if (!cx18_start_v4l2_encode_stream(s)) {
5378c2ecf20Sopenharmony_ci		/* We're done */
5388c2ecf20Sopenharmony_ci		set_bit(CX18_F_S_APPL_IO, &s->s_flags);
5398c2ecf20Sopenharmony_ci		/* Resume a possibly paused encoder */
5408c2ecf20Sopenharmony_ci		if (test_and_clear_bit(CX18_F_I_ENC_PAUSED, &cx->i_flags))
5418c2ecf20Sopenharmony_ci			cx18_vapi(cx, CX18_CPU_CAPTURE_PAUSE, 1, s->handle);
5428c2ecf20Sopenharmony_ci		return 0;
5438c2ecf20Sopenharmony_ci	}
5448c2ecf20Sopenharmony_ci
5458c2ecf20Sopenharmony_cistart_failed:
5468c2ecf20Sopenharmony_ci	CX18_DEBUG_WARN("Failed to start capturing for stream %s\n", s->name);
5478c2ecf20Sopenharmony_ci
5488c2ecf20Sopenharmony_ci	/*
5498c2ecf20Sopenharmony_ci	 * The associated VBI and IDX streams for internal use are released
5508c2ecf20Sopenharmony_ci	 * automatically when the MPG stream is released.  We only need to stop
5518c2ecf20Sopenharmony_ci	 * the associated stream.
5528c2ecf20Sopenharmony_ci	 */
5538c2ecf20Sopenharmony_ci	if (s->type == CX18_ENC_STREAM_TYPE_MPG) {
5548c2ecf20Sopenharmony_ci		/* Stop the IDX stream which is always for internal use */
5558c2ecf20Sopenharmony_ci		if (test_bit(CX18_F_S_STREAMING, &s_idx->s_flags)) {
5568c2ecf20Sopenharmony_ci			cx18_stop_v4l2_encode_stream(s_idx, 0);
5578c2ecf20Sopenharmony_ci			clear_bit(CX18_F_S_STREAMING, &s_idx->s_flags);
5588c2ecf20Sopenharmony_ci		}
5598c2ecf20Sopenharmony_ci		/* Stop the VBI stream, if only running for internal use */
5608c2ecf20Sopenharmony_ci		if (test_bit(CX18_F_S_STREAMING, &s_vbi->s_flags) &&
5618c2ecf20Sopenharmony_ci		    !test_bit(CX18_F_S_APPL_IO, &s_vbi->s_flags)) {
5628c2ecf20Sopenharmony_ci			cx18_stop_v4l2_encode_stream(s_vbi, 0);
5638c2ecf20Sopenharmony_ci			clear_bit(CX18_F_S_STREAMING, &s_vbi->s_flags);
5648c2ecf20Sopenharmony_ci		}
5658c2ecf20Sopenharmony_ci	}
5668c2ecf20Sopenharmony_ci	clear_bit(CX18_F_S_STREAMING, &s->s_flags);
5678c2ecf20Sopenharmony_ci	cx18_release_stream(s); /* Also releases associated streams */
5688c2ecf20Sopenharmony_ci	return -EIO;
5698c2ecf20Sopenharmony_ci}
5708c2ecf20Sopenharmony_ci
5718c2ecf20Sopenharmony_cissize_t cx18_v4l2_read(struct file *filp, char __user *buf, size_t count,
5728c2ecf20Sopenharmony_ci		loff_t *pos)
5738c2ecf20Sopenharmony_ci{
5748c2ecf20Sopenharmony_ci	struct cx18_open_id *id = file2id(filp);
5758c2ecf20Sopenharmony_ci	struct cx18 *cx = id->cx;
5768c2ecf20Sopenharmony_ci	struct cx18_stream *s = &cx->streams[id->type];
5778c2ecf20Sopenharmony_ci	int rc;
5788c2ecf20Sopenharmony_ci
5798c2ecf20Sopenharmony_ci	CX18_DEBUG_HI_FILE("read %zd bytes from %s\n", count, s->name);
5808c2ecf20Sopenharmony_ci
5818c2ecf20Sopenharmony_ci	mutex_lock(&cx->serialize_lock);
5828c2ecf20Sopenharmony_ci	rc = cx18_start_capture(id);
5838c2ecf20Sopenharmony_ci	mutex_unlock(&cx->serialize_lock);
5848c2ecf20Sopenharmony_ci	if (rc)
5858c2ecf20Sopenharmony_ci		return rc;
5868c2ecf20Sopenharmony_ci
5878c2ecf20Sopenharmony_ci	if ((s->vb_type == V4L2_BUF_TYPE_VIDEO_CAPTURE) &&
5888c2ecf20Sopenharmony_ci		(id->type == CX18_ENC_STREAM_TYPE_YUV)) {
5898c2ecf20Sopenharmony_ci		return videobuf_read_stream(&s->vbuf_q, buf, count, pos, 0,
5908c2ecf20Sopenharmony_ci			filp->f_flags & O_NONBLOCK);
5918c2ecf20Sopenharmony_ci	}
5928c2ecf20Sopenharmony_ci
5938c2ecf20Sopenharmony_ci	return cx18_read_pos(s, buf, count, pos, filp->f_flags & O_NONBLOCK);
5948c2ecf20Sopenharmony_ci}
5958c2ecf20Sopenharmony_ci
5968c2ecf20Sopenharmony_ci__poll_t cx18_v4l2_enc_poll(struct file *filp, poll_table *wait)
5978c2ecf20Sopenharmony_ci{
5988c2ecf20Sopenharmony_ci	__poll_t req_events = poll_requested_events(wait);
5998c2ecf20Sopenharmony_ci	struct cx18_open_id *id = file2id(filp);
6008c2ecf20Sopenharmony_ci	struct cx18 *cx = id->cx;
6018c2ecf20Sopenharmony_ci	struct cx18_stream *s = &cx->streams[id->type];
6028c2ecf20Sopenharmony_ci	int eof = test_bit(CX18_F_S_STREAMOFF, &s->s_flags);
6038c2ecf20Sopenharmony_ci	__poll_t res = 0;
6048c2ecf20Sopenharmony_ci
6058c2ecf20Sopenharmony_ci	/* Start a capture if there is none */
6068c2ecf20Sopenharmony_ci	if (!eof && !test_bit(CX18_F_S_STREAMING, &s->s_flags) &&
6078c2ecf20Sopenharmony_ci			(req_events & (EPOLLIN | EPOLLRDNORM))) {
6088c2ecf20Sopenharmony_ci		int rc;
6098c2ecf20Sopenharmony_ci
6108c2ecf20Sopenharmony_ci		mutex_lock(&cx->serialize_lock);
6118c2ecf20Sopenharmony_ci		rc = cx18_start_capture(id);
6128c2ecf20Sopenharmony_ci		mutex_unlock(&cx->serialize_lock);
6138c2ecf20Sopenharmony_ci		if (rc) {
6148c2ecf20Sopenharmony_ci			CX18_DEBUG_INFO("Could not start capture for %s (%d)\n",
6158c2ecf20Sopenharmony_ci					s->name, rc);
6168c2ecf20Sopenharmony_ci			return EPOLLERR;
6178c2ecf20Sopenharmony_ci		}
6188c2ecf20Sopenharmony_ci		CX18_DEBUG_FILE("Encoder poll started capture\n");
6198c2ecf20Sopenharmony_ci	}
6208c2ecf20Sopenharmony_ci
6218c2ecf20Sopenharmony_ci	if ((s->vb_type == V4L2_BUF_TYPE_VIDEO_CAPTURE) &&
6228c2ecf20Sopenharmony_ci		(id->type == CX18_ENC_STREAM_TYPE_YUV)) {
6238c2ecf20Sopenharmony_ci		__poll_t videobuf_poll = videobuf_poll_stream(filp, &s->vbuf_q, wait);
6248c2ecf20Sopenharmony_ci
6258c2ecf20Sopenharmony_ci		if (v4l2_event_pending(&id->fh))
6268c2ecf20Sopenharmony_ci			res |= EPOLLPRI;
6278c2ecf20Sopenharmony_ci		if (eof && videobuf_poll == EPOLLERR)
6288c2ecf20Sopenharmony_ci			return res | EPOLLHUP;
6298c2ecf20Sopenharmony_ci		return res | videobuf_poll;
6308c2ecf20Sopenharmony_ci	}
6318c2ecf20Sopenharmony_ci
6328c2ecf20Sopenharmony_ci	/* add stream's waitq to the poll list */
6338c2ecf20Sopenharmony_ci	CX18_DEBUG_HI_FILE("Encoder poll\n");
6348c2ecf20Sopenharmony_ci	if (v4l2_event_pending(&id->fh))
6358c2ecf20Sopenharmony_ci		res |= EPOLLPRI;
6368c2ecf20Sopenharmony_ci	else
6378c2ecf20Sopenharmony_ci		poll_wait(filp, &s->waitq, wait);
6388c2ecf20Sopenharmony_ci
6398c2ecf20Sopenharmony_ci	if (atomic_read(&s->q_full.depth))
6408c2ecf20Sopenharmony_ci		return res | EPOLLIN | EPOLLRDNORM;
6418c2ecf20Sopenharmony_ci	if (eof)
6428c2ecf20Sopenharmony_ci		return res | EPOLLHUP;
6438c2ecf20Sopenharmony_ci	return res;
6448c2ecf20Sopenharmony_ci}
6458c2ecf20Sopenharmony_ci
6468c2ecf20Sopenharmony_ciint cx18_v4l2_mmap(struct file *file, struct vm_area_struct *vma)
6478c2ecf20Sopenharmony_ci{
6488c2ecf20Sopenharmony_ci	struct cx18_open_id *id = file->private_data;
6498c2ecf20Sopenharmony_ci	struct cx18 *cx = id->cx;
6508c2ecf20Sopenharmony_ci	struct cx18_stream *s = &cx->streams[id->type];
6518c2ecf20Sopenharmony_ci	int eof = test_bit(CX18_F_S_STREAMOFF, &s->s_flags);
6528c2ecf20Sopenharmony_ci
6538c2ecf20Sopenharmony_ci	if ((s->vb_type == V4L2_BUF_TYPE_VIDEO_CAPTURE) &&
6548c2ecf20Sopenharmony_ci		(id->type == CX18_ENC_STREAM_TYPE_YUV)) {
6558c2ecf20Sopenharmony_ci
6568c2ecf20Sopenharmony_ci		/* Start a capture if there is none */
6578c2ecf20Sopenharmony_ci		if (!eof && !test_bit(CX18_F_S_STREAMING, &s->s_flags)) {
6588c2ecf20Sopenharmony_ci			int rc;
6598c2ecf20Sopenharmony_ci
6608c2ecf20Sopenharmony_ci			mutex_lock(&cx->serialize_lock);
6618c2ecf20Sopenharmony_ci			rc = cx18_start_capture(id);
6628c2ecf20Sopenharmony_ci			mutex_unlock(&cx->serialize_lock);
6638c2ecf20Sopenharmony_ci			if (rc) {
6648c2ecf20Sopenharmony_ci				CX18_DEBUG_INFO(
6658c2ecf20Sopenharmony_ci					"Could not start capture for %s (%d)\n",
6668c2ecf20Sopenharmony_ci					s->name, rc);
6678c2ecf20Sopenharmony_ci				return -EINVAL;
6688c2ecf20Sopenharmony_ci			}
6698c2ecf20Sopenharmony_ci			CX18_DEBUG_FILE("Encoder mmap started capture\n");
6708c2ecf20Sopenharmony_ci		}
6718c2ecf20Sopenharmony_ci
6728c2ecf20Sopenharmony_ci		return videobuf_mmap_mapper(&s->vbuf_q, vma);
6738c2ecf20Sopenharmony_ci	}
6748c2ecf20Sopenharmony_ci
6758c2ecf20Sopenharmony_ci	return -EINVAL;
6768c2ecf20Sopenharmony_ci}
6778c2ecf20Sopenharmony_ci
6788c2ecf20Sopenharmony_civoid cx18_vb_timeout(struct timer_list *t)
6798c2ecf20Sopenharmony_ci{
6808c2ecf20Sopenharmony_ci	struct cx18_stream *s = from_timer(s, t, vb_timeout);
6818c2ecf20Sopenharmony_ci	struct cx18_videobuf_buffer *buf;
6828c2ecf20Sopenharmony_ci	unsigned long flags;
6838c2ecf20Sopenharmony_ci
6848c2ecf20Sopenharmony_ci	/* Return all of the buffers in error state, so the vbi/vid inode
6858c2ecf20Sopenharmony_ci	 * can return from blocking.
6868c2ecf20Sopenharmony_ci	 */
6878c2ecf20Sopenharmony_ci	spin_lock_irqsave(&s->vb_lock, flags);
6888c2ecf20Sopenharmony_ci	while (!list_empty(&s->vb_capture)) {
6898c2ecf20Sopenharmony_ci		buf = list_entry(s->vb_capture.next,
6908c2ecf20Sopenharmony_ci			struct cx18_videobuf_buffer, vb.queue);
6918c2ecf20Sopenharmony_ci		list_del(&buf->vb.queue);
6928c2ecf20Sopenharmony_ci		buf->vb.state = VIDEOBUF_ERROR;
6938c2ecf20Sopenharmony_ci		wake_up(&buf->vb.done);
6948c2ecf20Sopenharmony_ci	}
6958c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&s->vb_lock, flags);
6968c2ecf20Sopenharmony_ci}
6978c2ecf20Sopenharmony_ci
6988c2ecf20Sopenharmony_civoid cx18_stop_capture(struct cx18_open_id *id, int gop_end)
6998c2ecf20Sopenharmony_ci{
7008c2ecf20Sopenharmony_ci	struct cx18 *cx = id->cx;
7018c2ecf20Sopenharmony_ci	struct cx18_stream *s = &cx->streams[id->type];
7028c2ecf20Sopenharmony_ci	struct cx18_stream *s_vbi = &cx->streams[CX18_ENC_STREAM_TYPE_VBI];
7038c2ecf20Sopenharmony_ci	struct cx18_stream *s_idx = &cx->streams[CX18_ENC_STREAM_TYPE_IDX];
7048c2ecf20Sopenharmony_ci
7058c2ecf20Sopenharmony_ci	CX18_DEBUG_IOCTL("close() of %s\n", s->name);
7068c2ecf20Sopenharmony_ci
7078c2ecf20Sopenharmony_ci	/* 'Unclaim' this stream */
7088c2ecf20Sopenharmony_ci
7098c2ecf20Sopenharmony_ci	/* Stop capturing */
7108c2ecf20Sopenharmony_ci	if (test_bit(CX18_F_S_STREAMING, &s->s_flags)) {
7118c2ecf20Sopenharmony_ci		CX18_DEBUG_INFO("close stopping capture\n");
7128c2ecf20Sopenharmony_ci		if (id->type == CX18_ENC_STREAM_TYPE_MPG) {
7138c2ecf20Sopenharmony_ci			/* Stop internal use associated VBI and IDX streams */
7148c2ecf20Sopenharmony_ci			if (test_bit(CX18_F_S_STREAMING, &s_vbi->s_flags) &&
7158c2ecf20Sopenharmony_ci			    !test_bit(CX18_F_S_APPL_IO, &s_vbi->s_flags)) {
7168c2ecf20Sopenharmony_ci				CX18_DEBUG_INFO("close stopping embedded VBI capture\n");
7178c2ecf20Sopenharmony_ci				cx18_stop_v4l2_encode_stream(s_vbi, 0);
7188c2ecf20Sopenharmony_ci			}
7198c2ecf20Sopenharmony_ci			if (test_bit(CX18_F_S_STREAMING, &s_idx->s_flags)) {
7208c2ecf20Sopenharmony_ci				CX18_DEBUG_INFO("close stopping IDX capture\n");
7218c2ecf20Sopenharmony_ci				cx18_stop_v4l2_encode_stream(s_idx, 0);
7228c2ecf20Sopenharmony_ci			}
7238c2ecf20Sopenharmony_ci		}
7248c2ecf20Sopenharmony_ci		if (id->type == CX18_ENC_STREAM_TYPE_VBI &&
7258c2ecf20Sopenharmony_ci		    test_bit(CX18_F_S_INTERNAL_USE, &s->s_flags))
7268c2ecf20Sopenharmony_ci			/* Also used internally, don't stop capturing */
7278c2ecf20Sopenharmony_ci			s->id = -1;
7288c2ecf20Sopenharmony_ci		else
7298c2ecf20Sopenharmony_ci			cx18_stop_v4l2_encode_stream(s, gop_end);
7308c2ecf20Sopenharmony_ci	}
7318c2ecf20Sopenharmony_ci	if (!gop_end) {
7328c2ecf20Sopenharmony_ci		clear_bit(CX18_F_S_APPL_IO, &s->s_flags);
7338c2ecf20Sopenharmony_ci		clear_bit(CX18_F_S_STREAMOFF, &s->s_flags);
7348c2ecf20Sopenharmony_ci		cx18_release_stream(s);
7358c2ecf20Sopenharmony_ci	}
7368c2ecf20Sopenharmony_ci}
7378c2ecf20Sopenharmony_ci
7388c2ecf20Sopenharmony_ciint cx18_v4l2_close(struct file *filp)
7398c2ecf20Sopenharmony_ci{
7408c2ecf20Sopenharmony_ci	struct v4l2_fh *fh = filp->private_data;
7418c2ecf20Sopenharmony_ci	struct cx18_open_id *id = fh2id(fh);
7428c2ecf20Sopenharmony_ci	struct cx18 *cx = id->cx;
7438c2ecf20Sopenharmony_ci	struct cx18_stream *s = &cx->streams[id->type];
7448c2ecf20Sopenharmony_ci
7458c2ecf20Sopenharmony_ci	CX18_DEBUG_IOCTL("close() of %s\n", s->name);
7468c2ecf20Sopenharmony_ci
7478c2ecf20Sopenharmony_ci	mutex_lock(&cx->serialize_lock);
7488c2ecf20Sopenharmony_ci	/* Stop radio */
7498c2ecf20Sopenharmony_ci	if (id->type == CX18_ENC_STREAM_TYPE_RAD &&
7508c2ecf20Sopenharmony_ci			v4l2_fh_is_singular_file(filp)) {
7518c2ecf20Sopenharmony_ci		/* Closing radio device, return to TV mode */
7528c2ecf20Sopenharmony_ci		cx18_mute(cx);
7538c2ecf20Sopenharmony_ci		/* Mark that the radio is no longer in use */
7548c2ecf20Sopenharmony_ci		clear_bit(CX18_F_I_RADIO_USER, &cx->i_flags);
7558c2ecf20Sopenharmony_ci		/* Switch tuner to TV */
7568c2ecf20Sopenharmony_ci		cx18_call_all(cx, video, s_std, cx->std);
7578c2ecf20Sopenharmony_ci		/* Select correct audio input (i.e. TV tuner or Line in) */
7588c2ecf20Sopenharmony_ci		cx18_audio_set_io(cx);
7598c2ecf20Sopenharmony_ci		if (atomic_read(&cx->ana_capturing) > 0) {
7608c2ecf20Sopenharmony_ci			/* Undo video mute */
7618c2ecf20Sopenharmony_ci			cx18_vapi(cx, CX18_CPU_SET_VIDEO_MUTE, 2, s->handle,
7628c2ecf20Sopenharmony_ci			    (v4l2_ctrl_g_ctrl(cx->cxhdl.video_mute) |
7638c2ecf20Sopenharmony_ci			    (v4l2_ctrl_g_ctrl(cx->cxhdl.video_mute_yuv) << 8)));
7648c2ecf20Sopenharmony_ci		}
7658c2ecf20Sopenharmony_ci		/* Done! Unmute and continue. */
7668c2ecf20Sopenharmony_ci		cx18_unmute(cx);
7678c2ecf20Sopenharmony_ci	}
7688c2ecf20Sopenharmony_ci
7698c2ecf20Sopenharmony_ci	v4l2_fh_del(fh);
7708c2ecf20Sopenharmony_ci	v4l2_fh_exit(fh);
7718c2ecf20Sopenharmony_ci
7728c2ecf20Sopenharmony_ci	/* 'Unclaim' this stream */
7738c2ecf20Sopenharmony_ci	if (s->id == id->open_id)
7748c2ecf20Sopenharmony_ci		cx18_stop_capture(id, 0);
7758c2ecf20Sopenharmony_ci	kfree(id);
7768c2ecf20Sopenharmony_ci	mutex_unlock(&cx->serialize_lock);
7778c2ecf20Sopenharmony_ci	return 0;
7788c2ecf20Sopenharmony_ci}
7798c2ecf20Sopenharmony_ci
7808c2ecf20Sopenharmony_cistatic int cx18_serialized_open(struct cx18_stream *s, struct file *filp)
7818c2ecf20Sopenharmony_ci{
7828c2ecf20Sopenharmony_ci	struct cx18 *cx = s->cx;
7838c2ecf20Sopenharmony_ci	struct cx18_open_id *item;
7848c2ecf20Sopenharmony_ci
7858c2ecf20Sopenharmony_ci	CX18_DEBUG_FILE("open %s\n", s->name);
7868c2ecf20Sopenharmony_ci
7878c2ecf20Sopenharmony_ci	/* Allocate memory */
7888c2ecf20Sopenharmony_ci	item = kzalloc(sizeof(struct cx18_open_id), GFP_KERNEL);
7898c2ecf20Sopenharmony_ci	if (NULL == item) {
7908c2ecf20Sopenharmony_ci		CX18_DEBUG_WARN("nomem on v4l2 open\n");
7918c2ecf20Sopenharmony_ci		return -ENOMEM;
7928c2ecf20Sopenharmony_ci	}
7938c2ecf20Sopenharmony_ci	v4l2_fh_init(&item->fh, &s->video_dev);
7948c2ecf20Sopenharmony_ci
7958c2ecf20Sopenharmony_ci	item->cx = cx;
7968c2ecf20Sopenharmony_ci	item->type = s->type;
7978c2ecf20Sopenharmony_ci
7988c2ecf20Sopenharmony_ci	item->open_id = cx->open_id++;
7998c2ecf20Sopenharmony_ci	filp->private_data = &item->fh;
8008c2ecf20Sopenharmony_ci	v4l2_fh_add(&item->fh);
8018c2ecf20Sopenharmony_ci
8028c2ecf20Sopenharmony_ci	if (item->type == CX18_ENC_STREAM_TYPE_RAD &&
8038c2ecf20Sopenharmony_ci			v4l2_fh_is_singular_file(filp)) {
8048c2ecf20Sopenharmony_ci		if (!test_bit(CX18_F_I_RADIO_USER, &cx->i_flags)) {
8058c2ecf20Sopenharmony_ci			if (atomic_read(&cx->ana_capturing) > 0) {
8068c2ecf20Sopenharmony_ci				/* switching to radio while capture is
8078c2ecf20Sopenharmony_ci				   in progress is not polite */
8088c2ecf20Sopenharmony_ci				v4l2_fh_del(&item->fh);
8098c2ecf20Sopenharmony_ci				v4l2_fh_exit(&item->fh);
8108c2ecf20Sopenharmony_ci				kfree(item);
8118c2ecf20Sopenharmony_ci				return -EBUSY;
8128c2ecf20Sopenharmony_ci			}
8138c2ecf20Sopenharmony_ci		}
8148c2ecf20Sopenharmony_ci
8158c2ecf20Sopenharmony_ci		/* Mark that the radio is being used. */
8168c2ecf20Sopenharmony_ci		set_bit(CX18_F_I_RADIO_USER, &cx->i_flags);
8178c2ecf20Sopenharmony_ci		/* We have the radio */
8188c2ecf20Sopenharmony_ci		cx18_mute(cx);
8198c2ecf20Sopenharmony_ci		/* Switch tuner to radio */
8208c2ecf20Sopenharmony_ci		cx18_call_all(cx, tuner, s_radio);
8218c2ecf20Sopenharmony_ci		/* Select the correct audio input (i.e. radio tuner) */
8228c2ecf20Sopenharmony_ci		cx18_audio_set_io(cx);
8238c2ecf20Sopenharmony_ci		/* Done! Unmute and continue. */
8248c2ecf20Sopenharmony_ci		cx18_unmute(cx);
8258c2ecf20Sopenharmony_ci	}
8268c2ecf20Sopenharmony_ci	return 0;
8278c2ecf20Sopenharmony_ci}
8288c2ecf20Sopenharmony_ci
8298c2ecf20Sopenharmony_ciint cx18_v4l2_open(struct file *filp)
8308c2ecf20Sopenharmony_ci{
8318c2ecf20Sopenharmony_ci	int res;
8328c2ecf20Sopenharmony_ci	struct video_device *video_dev = video_devdata(filp);
8338c2ecf20Sopenharmony_ci	struct cx18_stream *s = video_get_drvdata(video_dev);
8348c2ecf20Sopenharmony_ci	struct cx18 *cx = s->cx;
8358c2ecf20Sopenharmony_ci
8368c2ecf20Sopenharmony_ci	mutex_lock(&cx->serialize_lock);
8378c2ecf20Sopenharmony_ci	if (cx18_init_on_first_open(cx)) {
8388c2ecf20Sopenharmony_ci		CX18_ERR("Failed to initialize on %s\n",
8398c2ecf20Sopenharmony_ci			 video_device_node_name(video_dev));
8408c2ecf20Sopenharmony_ci		mutex_unlock(&cx->serialize_lock);
8418c2ecf20Sopenharmony_ci		return -ENXIO;
8428c2ecf20Sopenharmony_ci	}
8438c2ecf20Sopenharmony_ci	res = cx18_serialized_open(s, filp);
8448c2ecf20Sopenharmony_ci	mutex_unlock(&cx->serialize_lock);
8458c2ecf20Sopenharmony_ci	return res;
8468c2ecf20Sopenharmony_ci}
8478c2ecf20Sopenharmony_ci
8488c2ecf20Sopenharmony_civoid cx18_mute(struct cx18 *cx)
8498c2ecf20Sopenharmony_ci{
8508c2ecf20Sopenharmony_ci	u32 h;
8518c2ecf20Sopenharmony_ci	if (atomic_read(&cx->ana_capturing)) {
8528c2ecf20Sopenharmony_ci		h = cx18_find_handle(cx);
8538c2ecf20Sopenharmony_ci		if (h != CX18_INVALID_TASK_HANDLE)
8548c2ecf20Sopenharmony_ci			cx18_vapi(cx, CX18_CPU_SET_AUDIO_MUTE, 2, h, 1);
8558c2ecf20Sopenharmony_ci		else
8568c2ecf20Sopenharmony_ci			CX18_ERR("Can't find valid task handle for mute\n");
8578c2ecf20Sopenharmony_ci	}
8588c2ecf20Sopenharmony_ci	CX18_DEBUG_INFO("Mute\n");
8598c2ecf20Sopenharmony_ci}
8608c2ecf20Sopenharmony_ci
8618c2ecf20Sopenharmony_civoid cx18_unmute(struct cx18 *cx)
8628c2ecf20Sopenharmony_ci{
8638c2ecf20Sopenharmony_ci	u32 h;
8648c2ecf20Sopenharmony_ci	if (atomic_read(&cx->ana_capturing)) {
8658c2ecf20Sopenharmony_ci		h = cx18_find_handle(cx);
8668c2ecf20Sopenharmony_ci		if (h != CX18_INVALID_TASK_HANDLE) {
8678c2ecf20Sopenharmony_ci			cx18_msleep_timeout(100, 0);
8688c2ecf20Sopenharmony_ci			cx18_vapi(cx, CX18_CPU_SET_MISC_PARAMETERS, 2, h, 12);
8698c2ecf20Sopenharmony_ci			cx18_vapi(cx, CX18_CPU_SET_AUDIO_MUTE, 2, h, 0);
8708c2ecf20Sopenharmony_ci		} else
8718c2ecf20Sopenharmony_ci			CX18_ERR("Can't find valid task handle for unmute\n");
8728c2ecf20Sopenharmony_ci	}
8738c2ecf20Sopenharmony_ci	CX18_DEBUG_INFO("Unmute\n");
8748c2ecf20Sopenharmony_ci}
875