18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci *  cx18 ioctl system call
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci *  Derived from ivtv-ioctl.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-io.h"
138c2ecf20Sopenharmony_ci#include "cx18-version.h"
148c2ecf20Sopenharmony_ci#include "cx18-mailbox.h"
158c2ecf20Sopenharmony_ci#include "cx18-i2c.h"
168c2ecf20Sopenharmony_ci#include "cx18-queue.h"
178c2ecf20Sopenharmony_ci#include "cx18-fileops.h"
188c2ecf20Sopenharmony_ci#include "cx18-vbi.h"
198c2ecf20Sopenharmony_ci#include "cx18-audio.h"
208c2ecf20Sopenharmony_ci#include "cx18-video.h"
218c2ecf20Sopenharmony_ci#include "cx18-streams.h"
228c2ecf20Sopenharmony_ci#include "cx18-ioctl.h"
238c2ecf20Sopenharmony_ci#include "cx18-gpio.h"
248c2ecf20Sopenharmony_ci#include "cx18-controls.h"
258c2ecf20Sopenharmony_ci#include "cx18-cards.h"
268c2ecf20Sopenharmony_ci#include "cx18-av-core.h"
278c2ecf20Sopenharmony_ci#include <media/tveeprom.h>
288c2ecf20Sopenharmony_ci#include <media/v4l2-event.h>
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_ciu16 cx18_service2vbi(int type)
318c2ecf20Sopenharmony_ci{
328c2ecf20Sopenharmony_ci	switch (type) {
338c2ecf20Sopenharmony_ci	case V4L2_SLICED_TELETEXT_B:
348c2ecf20Sopenharmony_ci		return CX18_SLICED_TYPE_TELETEXT_B;
358c2ecf20Sopenharmony_ci	case V4L2_SLICED_CAPTION_525:
368c2ecf20Sopenharmony_ci		return CX18_SLICED_TYPE_CAPTION_525;
378c2ecf20Sopenharmony_ci	case V4L2_SLICED_WSS_625:
388c2ecf20Sopenharmony_ci		return CX18_SLICED_TYPE_WSS_625;
398c2ecf20Sopenharmony_ci	case V4L2_SLICED_VPS:
408c2ecf20Sopenharmony_ci		return CX18_SLICED_TYPE_VPS;
418c2ecf20Sopenharmony_ci	default:
428c2ecf20Sopenharmony_ci		return 0;
438c2ecf20Sopenharmony_ci	}
448c2ecf20Sopenharmony_ci}
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_ci/* Check if VBI services are allowed on the (field, line) for the video std */
478c2ecf20Sopenharmony_cistatic int valid_service_line(int field, int line, int is_pal)
488c2ecf20Sopenharmony_ci{
498c2ecf20Sopenharmony_ci	return (is_pal && line >= 6 &&
508c2ecf20Sopenharmony_ci		((field == 0 && line <= 23) || (field == 1 && line <= 22))) ||
518c2ecf20Sopenharmony_ci	       (!is_pal && line >= 10 && line < 22);
528c2ecf20Sopenharmony_ci}
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_ci/*
558c2ecf20Sopenharmony_ci * For a (field, line, std) and inbound potential set of services for that line,
568c2ecf20Sopenharmony_ci * return the first valid service of those passed in the incoming set for that
578c2ecf20Sopenharmony_ci * line in priority order:
588c2ecf20Sopenharmony_ci * CC, VPS, or WSS over TELETEXT for well known lines
598c2ecf20Sopenharmony_ci * TELETEXT, before VPS, before CC, before WSS, for other lines
608c2ecf20Sopenharmony_ci */
618c2ecf20Sopenharmony_cistatic u16 select_service_from_set(int field, int line, u16 set, int is_pal)
628c2ecf20Sopenharmony_ci{
638c2ecf20Sopenharmony_ci	u16 valid_set = (is_pal ? V4L2_SLICED_VBI_625 : V4L2_SLICED_VBI_525);
648c2ecf20Sopenharmony_ci	int i;
658c2ecf20Sopenharmony_ci
668c2ecf20Sopenharmony_ci	set = set & valid_set;
678c2ecf20Sopenharmony_ci	if (set == 0 || !valid_service_line(field, line, is_pal))
688c2ecf20Sopenharmony_ci		return 0;
698c2ecf20Sopenharmony_ci	if (!is_pal) {
708c2ecf20Sopenharmony_ci		if (line == 21 && (set & V4L2_SLICED_CAPTION_525))
718c2ecf20Sopenharmony_ci			return V4L2_SLICED_CAPTION_525;
728c2ecf20Sopenharmony_ci	} else {
738c2ecf20Sopenharmony_ci		if (line == 16 && field == 0 && (set & V4L2_SLICED_VPS))
748c2ecf20Sopenharmony_ci			return V4L2_SLICED_VPS;
758c2ecf20Sopenharmony_ci		if (line == 23 && field == 0 && (set & V4L2_SLICED_WSS_625))
768c2ecf20Sopenharmony_ci			return V4L2_SLICED_WSS_625;
778c2ecf20Sopenharmony_ci		if (line == 23)
788c2ecf20Sopenharmony_ci			return 0;
798c2ecf20Sopenharmony_ci	}
808c2ecf20Sopenharmony_ci	for (i = 0; i < 32; i++) {
818c2ecf20Sopenharmony_ci		if (BIT(i) & set)
828c2ecf20Sopenharmony_ci			return 1 << i;
838c2ecf20Sopenharmony_ci	}
848c2ecf20Sopenharmony_ci	return 0;
858c2ecf20Sopenharmony_ci}
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_ci/*
888c2ecf20Sopenharmony_ci * Expand the service_set of *fmt into valid service_lines for the std,
898c2ecf20Sopenharmony_ci * and clear the passed in fmt->service_set
908c2ecf20Sopenharmony_ci */
918c2ecf20Sopenharmony_civoid cx18_expand_service_set(struct v4l2_sliced_vbi_format *fmt, int is_pal)
928c2ecf20Sopenharmony_ci{
938c2ecf20Sopenharmony_ci	u16 set = fmt->service_set;
948c2ecf20Sopenharmony_ci	int f, l;
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_ci	fmt->service_set = 0;
978c2ecf20Sopenharmony_ci	for (f = 0; f < 2; f++) {
988c2ecf20Sopenharmony_ci		for (l = 0; l < 24; l++)
998c2ecf20Sopenharmony_ci			fmt->service_lines[f][l] = select_service_from_set(f, l, set, is_pal);
1008c2ecf20Sopenharmony_ci	}
1018c2ecf20Sopenharmony_ci}
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_ci/*
1048c2ecf20Sopenharmony_ci * Sanitize the service_lines in *fmt per the video std, and return 1
1058c2ecf20Sopenharmony_ci * if any service_line is left as valid after santization
1068c2ecf20Sopenharmony_ci */
1078c2ecf20Sopenharmony_cistatic int check_service_set(struct v4l2_sliced_vbi_format *fmt, int is_pal)
1088c2ecf20Sopenharmony_ci{
1098c2ecf20Sopenharmony_ci	int f, l;
1108c2ecf20Sopenharmony_ci	u16 set = 0;
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_ci	for (f = 0; f < 2; f++) {
1138c2ecf20Sopenharmony_ci		for (l = 0; l < 24; l++) {
1148c2ecf20Sopenharmony_ci			fmt->service_lines[f][l] = select_service_from_set(f, l, fmt->service_lines[f][l], is_pal);
1158c2ecf20Sopenharmony_ci			set |= fmt->service_lines[f][l];
1168c2ecf20Sopenharmony_ci		}
1178c2ecf20Sopenharmony_ci	}
1188c2ecf20Sopenharmony_ci	return set != 0;
1198c2ecf20Sopenharmony_ci}
1208c2ecf20Sopenharmony_ci
1218c2ecf20Sopenharmony_ci/* Compute the service_set from the assumed valid service_lines of *fmt */
1228c2ecf20Sopenharmony_ciu16 cx18_get_service_set(struct v4l2_sliced_vbi_format *fmt)
1238c2ecf20Sopenharmony_ci{
1248c2ecf20Sopenharmony_ci	int f, l;
1258c2ecf20Sopenharmony_ci	u16 set = 0;
1268c2ecf20Sopenharmony_ci
1278c2ecf20Sopenharmony_ci	for (f = 0; f < 2; f++) {
1288c2ecf20Sopenharmony_ci		for (l = 0; l < 24; l++)
1298c2ecf20Sopenharmony_ci			set |= fmt->service_lines[f][l];
1308c2ecf20Sopenharmony_ci	}
1318c2ecf20Sopenharmony_ci	return set;
1328c2ecf20Sopenharmony_ci}
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_cistatic int cx18_g_fmt_vid_cap(struct file *file, void *fh,
1358c2ecf20Sopenharmony_ci				struct v4l2_format *fmt)
1368c2ecf20Sopenharmony_ci{
1378c2ecf20Sopenharmony_ci	struct cx18_open_id *id = fh2id(fh);
1388c2ecf20Sopenharmony_ci	struct cx18 *cx = id->cx;
1398c2ecf20Sopenharmony_ci	struct cx18_stream *s = &cx->streams[id->type];
1408c2ecf20Sopenharmony_ci	struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
1418c2ecf20Sopenharmony_ci
1428c2ecf20Sopenharmony_ci	pixfmt->width = cx->cxhdl.width;
1438c2ecf20Sopenharmony_ci	pixfmt->height = cx->cxhdl.height;
1448c2ecf20Sopenharmony_ci	pixfmt->colorspace = V4L2_COLORSPACE_SMPTE170M;
1458c2ecf20Sopenharmony_ci	pixfmt->field = V4L2_FIELD_INTERLACED;
1468c2ecf20Sopenharmony_ci	if (id->type == CX18_ENC_STREAM_TYPE_YUV) {
1478c2ecf20Sopenharmony_ci		pixfmt->pixelformat = s->pixelformat;
1488c2ecf20Sopenharmony_ci		pixfmt->sizeimage = s->vb_bytes_per_frame;
1498c2ecf20Sopenharmony_ci		pixfmt->bytesperline = s->vb_bytes_per_line;
1508c2ecf20Sopenharmony_ci	} else {
1518c2ecf20Sopenharmony_ci		pixfmt->pixelformat = V4L2_PIX_FMT_MPEG;
1528c2ecf20Sopenharmony_ci		pixfmt->sizeimage = 128 * 1024;
1538c2ecf20Sopenharmony_ci		pixfmt->bytesperline = 0;
1548c2ecf20Sopenharmony_ci	}
1558c2ecf20Sopenharmony_ci	return 0;
1568c2ecf20Sopenharmony_ci}
1578c2ecf20Sopenharmony_ci
1588c2ecf20Sopenharmony_cistatic int cx18_g_fmt_vbi_cap(struct file *file, void *fh,
1598c2ecf20Sopenharmony_ci				struct v4l2_format *fmt)
1608c2ecf20Sopenharmony_ci{
1618c2ecf20Sopenharmony_ci	struct cx18 *cx = fh2id(fh)->cx;
1628c2ecf20Sopenharmony_ci	struct v4l2_vbi_format *vbifmt = &fmt->fmt.vbi;
1638c2ecf20Sopenharmony_ci
1648c2ecf20Sopenharmony_ci	vbifmt->sampling_rate = 27000000;
1658c2ecf20Sopenharmony_ci	vbifmt->offset = 248; /* FIXME - slightly wrong for both 50 & 60 Hz */
1668c2ecf20Sopenharmony_ci	vbifmt->samples_per_line = VBI_ACTIVE_SAMPLES - 4;
1678c2ecf20Sopenharmony_ci	vbifmt->sample_format = V4L2_PIX_FMT_GREY;
1688c2ecf20Sopenharmony_ci	vbifmt->start[0] = cx->vbi.start[0];
1698c2ecf20Sopenharmony_ci	vbifmt->start[1] = cx->vbi.start[1];
1708c2ecf20Sopenharmony_ci	vbifmt->count[0] = vbifmt->count[1] = cx->vbi.count;
1718c2ecf20Sopenharmony_ci	vbifmt->flags = 0;
1728c2ecf20Sopenharmony_ci	vbifmt->reserved[0] = 0;
1738c2ecf20Sopenharmony_ci	vbifmt->reserved[1] = 0;
1748c2ecf20Sopenharmony_ci	return 0;
1758c2ecf20Sopenharmony_ci}
1768c2ecf20Sopenharmony_ci
1778c2ecf20Sopenharmony_cistatic int cx18_g_fmt_sliced_vbi_cap(struct file *file, void *fh,
1788c2ecf20Sopenharmony_ci					struct v4l2_format *fmt)
1798c2ecf20Sopenharmony_ci{
1808c2ecf20Sopenharmony_ci	struct cx18 *cx = fh2id(fh)->cx;
1818c2ecf20Sopenharmony_ci	struct v4l2_sliced_vbi_format *vbifmt = &fmt->fmt.sliced;
1828c2ecf20Sopenharmony_ci
1838c2ecf20Sopenharmony_ci	/* sane, V4L2 spec compliant, defaults */
1848c2ecf20Sopenharmony_ci	vbifmt->reserved[0] = 0;
1858c2ecf20Sopenharmony_ci	vbifmt->reserved[1] = 0;
1868c2ecf20Sopenharmony_ci	vbifmt->io_size = sizeof(struct v4l2_sliced_vbi_data) * 36;
1878c2ecf20Sopenharmony_ci	memset(vbifmt->service_lines, 0, sizeof(vbifmt->service_lines));
1888c2ecf20Sopenharmony_ci	vbifmt->service_set = 0;
1898c2ecf20Sopenharmony_ci
1908c2ecf20Sopenharmony_ci	/*
1918c2ecf20Sopenharmony_ci	 * Fetch the configured service_lines and total service_set from the
1928c2ecf20Sopenharmony_ci	 * digitizer/slicer.  Note, cx18_av_vbi() wipes the passed in
1938c2ecf20Sopenharmony_ci	 * fmt->fmt.sliced under valid calling conditions
1948c2ecf20Sopenharmony_ci	 */
1958c2ecf20Sopenharmony_ci	if (v4l2_subdev_call(cx->sd_av, vbi, g_sliced_fmt, &fmt->fmt.sliced))
1968c2ecf20Sopenharmony_ci		return -EINVAL;
1978c2ecf20Sopenharmony_ci
1988c2ecf20Sopenharmony_ci	vbifmt->service_set = cx18_get_service_set(vbifmt);
1998c2ecf20Sopenharmony_ci	return 0;
2008c2ecf20Sopenharmony_ci}
2018c2ecf20Sopenharmony_ci
2028c2ecf20Sopenharmony_cistatic int cx18_try_fmt_vid_cap(struct file *file, void *fh,
2038c2ecf20Sopenharmony_ci				struct v4l2_format *fmt)
2048c2ecf20Sopenharmony_ci{
2058c2ecf20Sopenharmony_ci	struct cx18_open_id *id = fh2id(fh);
2068c2ecf20Sopenharmony_ci	struct cx18 *cx = id->cx;
2078c2ecf20Sopenharmony_ci	int w = fmt->fmt.pix.width;
2088c2ecf20Sopenharmony_ci	int h = fmt->fmt.pix.height;
2098c2ecf20Sopenharmony_ci	int min_h = 2;
2108c2ecf20Sopenharmony_ci
2118c2ecf20Sopenharmony_ci	w = min(w, 720);
2128c2ecf20Sopenharmony_ci	w = max(w, 2);
2138c2ecf20Sopenharmony_ci	if (id->type == CX18_ENC_STREAM_TYPE_YUV) {
2148c2ecf20Sopenharmony_ci		/* YUV height must be a multiple of 32 */
2158c2ecf20Sopenharmony_ci		h &= ~0x1f;
2168c2ecf20Sopenharmony_ci		min_h = 32;
2178c2ecf20Sopenharmony_ci	}
2188c2ecf20Sopenharmony_ci	h = min(h, cx->is_50hz ? 576 : 480);
2198c2ecf20Sopenharmony_ci	h = max(h, min_h);
2208c2ecf20Sopenharmony_ci
2218c2ecf20Sopenharmony_ci	fmt->fmt.pix.width = w;
2228c2ecf20Sopenharmony_ci	fmt->fmt.pix.height = h;
2238c2ecf20Sopenharmony_ci	return 0;
2248c2ecf20Sopenharmony_ci}
2258c2ecf20Sopenharmony_ci
2268c2ecf20Sopenharmony_cistatic int cx18_try_fmt_vbi_cap(struct file *file, void *fh,
2278c2ecf20Sopenharmony_ci				struct v4l2_format *fmt)
2288c2ecf20Sopenharmony_ci{
2298c2ecf20Sopenharmony_ci	return cx18_g_fmt_vbi_cap(file, fh, fmt);
2308c2ecf20Sopenharmony_ci}
2318c2ecf20Sopenharmony_ci
2328c2ecf20Sopenharmony_cistatic int cx18_try_fmt_sliced_vbi_cap(struct file *file, void *fh,
2338c2ecf20Sopenharmony_ci					struct v4l2_format *fmt)
2348c2ecf20Sopenharmony_ci{
2358c2ecf20Sopenharmony_ci	struct cx18 *cx = fh2id(fh)->cx;
2368c2ecf20Sopenharmony_ci	struct v4l2_sliced_vbi_format *vbifmt = &fmt->fmt.sliced;
2378c2ecf20Sopenharmony_ci
2388c2ecf20Sopenharmony_ci	vbifmt->io_size = sizeof(struct v4l2_sliced_vbi_data) * 36;
2398c2ecf20Sopenharmony_ci	vbifmt->reserved[0] = 0;
2408c2ecf20Sopenharmony_ci	vbifmt->reserved[1] = 0;
2418c2ecf20Sopenharmony_ci
2428c2ecf20Sopenharmony_ci	/* If given a service set, expand it validly & clear passed in set */
2438c2ecf20Sopenharmony_ci	if (vbifmt->service_set)
2448c2ecf20Sopenharmony_ci		cx18_expand_service_set(vbifmt, cx->is_50hz);
2458c2ecf20Sopenharmony_ci	/* Sanitize the service_lines, and compute the new set if any valid */
2468c2ecf20Sopenharmony_ci	if (check_service_set(vbifmt, cx->is_50hz))
2478c2ecf20Sopenharmony_ci		vbifmt->service_set = cx18_get_service_set(vbifmt);
2488c2ecf20Sopenharmony_ci	return 0;
2498c2ecf20Sopenharmony_ci}
2508c2ecf20Sopenharmony_ci
2518c2ecf20Sopenharmony_cistatic int cx18_s_fmt_vid_cap(struct file *file, void *fh,
2528c2ecf20Sopenharmony_ci				struct v4l2_format *fmt)
2538c2ecf20Sopenharmony_ci{
2548c2ecf20Sopenharmony_ci	struct cx18_open_id *id = fh2id(fh);
2558c2ecf20Sopenharmony_ci	struct cx18 *cx = id->cx;
2568c2ecf20Sopenharmony_ci	struct v4l2_subdev_format format = {
2578c2ecf20Sopenharmony_ci		.which = V4L2_SUBDEV_FORMAT_ACTIVE,
2588c2ecf20Sopenharmony_ci	};
2598c2ecf20Sopenharmony_ci	struct cx18_stream *s = &cx->streams[id->type];
2608c2ecf20Sopenharmony_ci	int ret;
2618c2ecf20Sopenharmony_ci	int w, h;
2628c2ecf20Sopenharmony_ci
2638c2ecf20Sopenharmony_ci	ret = cx18_try_fmt_vid_cap(file, fh, fmt);
2648c2ecf20Sopenharmony_ci	if (ret)
2658c2ecf20Sopenharmony_ci		return ret;
2668c2ecf20Sopenharmony_ci	w = fmt->fmt.pix.width;
2678c2ecf20Sopenharmony_ci	h = fmt->fmt.pix.height;
2688c2ecf20Sopenharmony_ci
2698c2ecf20Sopenharmony_ci	if (cx->cxhdl.width == w && cx->cxhdl.height == h &&
2708c2ecf20Sopenharmony_ci	    s->pixelformat == fmt->fmt.pix.pixelformat)
2718c2ecf20Sopenharmony_ci		return 0;
2728c2ecf20Sopenharmony_ci
2738c2ecf20Sopenharmony_ci	if (atomic_read(&cx->ana_capturing) > 0)
2748c2ecf20Sopenharmony_ci		return -EBUSY;
2758c2ecf20Sopenharmony_ci
2768c2ecf20Sopenharmony_ci	s->pixelformat = fmt->fmt.pix.pixelformat;
2778c2ecf20Sopenharmony_ci	/* HM12 YUV size is (Y=(h*720) + UV=(h*(720/2)))
2788c2ecf20Sopenharmony_ci	   UYUV YUV size is (Y=(h*720) + UV=(h*(720))) */
2798c2ecf20Sopenharmony_ci	if (s->pixelformat == V4L2_PIX_FMT_HM12) {
2808c2ecf20Sopenharmony_ci		s->vb_bytes_per_frame = h * 720 * 3 / 2;
2818c2ecf20Sopenharmony_ci		s->vb_bytes_per_line = 720; /* First plane */
2828c2ecf20Sopenharmony_ci	} else {
2838c2ecf20Sopenharmony_ci		s->vb_bytes_per_frame = h * 720 * 2;
2848c2ecf20Sopenharmony_ci		s->vb_bytes_per_line = 1440; /* Packed */
2858c2ecf20Sopenharmony_ci	}
2868c2ecf20Sopenharmony_ci
2878c2ecf20Sopenharmony_ci	format.format.width = cx->cxhdl.width = w;
2888c2ecf20Sopenharmony_ci	format.format.height = cx->cxhdl.height = h;
2898c2ecf20Sopenharmony_ci	format.format.code = MEDIA_BUS_FMT_FIXED;
2908c2ecf20Sopenharmony_ci	v4l2_subdev_call(cx->sd_av, pad, set_fmt, NULL, &format);
2918c2ecf20Sopenharmony_ci	return cx18_g_fmt_vid_cap(file, fh, fmt);
2928c2ecf20Sopenharmony_ci}
2938c2ecf20Sopenharmony_ci
2948c2ecf20Sopenharmony_cistatic int cx18_s_fmt_vbi_cap(struct file *file, void *fh,
2958c2ecf20Sopenharmony_ci				struct v4l2_format *fmt)
2968c2ecf20Sopenharmony_ci{
2978c2ecf20Sopenharmony_ci	struct cx18_open_id *id = fh2id(fh);
2988c2ecf20Sopenharmony_ci	struct cx18 *cx = id->cx;
2998c2ecf20Sopenharmony_ci	int ret;
3008c2ecf20Sopenharmony_ci
3018c2ecf20Sopenharmony_ci	/*
3028c2ecf20Sopenharmony_ci	 * Changing the Encoder's Raw VBI parameters won't have any effect
3038c2ecf20Sopenharmony_ci	 * if any analog capture is ongoing
3048c2ecf20Sopenharmony_ci	 */
3058c2ecf20Sopenharmony_ci	if (!cx18_raw_vbi(cx) && atomic_read(&cx->ana_capturing) > 0)
3068c2ecf20Sopenharmony_ci		return -EBUSY;
3078c2ecf20Sopenharmony_ci
3088c2ecf20Sopenharmony_ci	/*
3098c2ecf20Sopenharmony_ci	 * Set the digitizer registers for raw active VBI.
3108c2ecf20Sopenharmony_ci	 * Note cx18_av_vbi_wipes out a lot of the passed in fmt under valid
3118c2ecf20Sopenharmony_ci	 * calling conditions
3128c2ecf20Sopenharmony_ci	 */
3138c2ecf20Sopenharmony_ci	ret = v4l2_subdev_call(cx->sd_av, vbi, s_raw_fmt, &fmt->fmt.vbi);
3148c2ecf20Sopenharmony_ci	if (ret)
3158c2ecf20Sopenharmony_ci		return ret;
3168c2ecf20Sopenharmony_ci
3178c2ecf20Sopenharmony_ci	/* Store our new v4l2 (non-)sliced VBI state */
3188c2ecf20Sopenharmony_ci	cx->vbi.sliced_in->service_set = 0;
3198c2ecf20Sopenharmony_ci	cx->vbi.in.type = V4L2_BUF_TYPE_VBI_CAPTURE;
3208c2ecf20Sopenharmony_ci
3218c2ecf20Sopenharmony_ci	return cx18_g_fmt_vbi_cap(file, fh, fmt);
3228c2ecf20Sopenharmony_ci}
3238c2ecf20Sopenharmony_ci
3248c2ecf20Sopenharmony_cistatic int cx18_s_fmt_sliced_vbi_cap(struct file *file, void *fh,
3258c2ecf20Sopenharmony_ci					struct v4l2_format *fmt)
3268c2ecf20Sopenharmony_ci{
3278c2ecf20Sopenharmony_ci	struct cx18_open_id *id = fh2id(fh);
3288c2ecf20Sopenharmony_ci	struct cx18 *cx = id->cx;
3298c2ecf20Sopenharmony_ci	int ret;
3308c2ecf20Sopenharmony_ci	struct v4l2_sliced_vbi_format *vbifmt = &fmt->fmt.sliced;
3318c2ecf20Sopenharmony_ci
3328c2ecf20Sopenharmony_ci	cx18_try_fmt_sliced_vbi_cap(file, fh, fmt);
3338c2ecf20Sopenharmony_ci
3348c2ecf20Sopenharmony_ci	/*
3358c2ecf20Sopenharmony_ci	 * Changing the Encoder's Raw VBI parameters won't have any effect
3368c2ecf20Sopenharmony_ci	 * if any analog capture is ongoing
3378c2ecf20Sopenharmony_ci	 */
3388c2ecf20Sopenharmony_ci	if (cx18_raw_vbi(cx) && atomic_read(&cx->ana_capturing) > 0)
3398c2ecf20Sopenharmony_ci		return -EBUSY;
3408c2ecf20Sopenharmony_ci
3418c2ecf20Sopenharmony_ci	/*
3428c2ecf20Sopenharmony_ci	 * Set the service_lines requested in the digitizer/slicer registers.
3438c2ecf20Sopenharmony_ci	 * Note, cx18_av_vbi() wipes some "impossible" service lines in the
3448c2ecf20Sopenharmony_ci	 * passed in fmt->fmt.sliced under valid calling conditions
3458c2ecf20Sopenharmony_ci	 */
3468c2ecf20Sopenharmony_ci	ret = v4l2_subdev_call(cx->sd_av, vbi, s_sliced_fmt, &fmt->fmt.sliced);
3478c2ecf20Sopenharmony_ci	if (ret)
3488c2ecf20Sopenharmony_ci		return ret;
3498c2ecf20Sopenharmony_ci	/* Store our current v4l2 sliced VBI settings */
3508c2ecf20Sopenharmony_ci	cx->vbi.in.type =  V4L2_BUF_TYPE_SLICED_VBI_CAPTURE;
3518c2ecf20Sopenharmony_ci	memcpy(cx->vbi.sliced_in, vbifmt, sizeof(*cx->vbi.sliced_in));
3528c2ecf20Sopenharmony_ci	return 0;
3538c2ecf20Sopenharmony_ci}
3548c2ecf20Sopenharmony_ci
3558c2ecf20Sopenharmony_ci#ifdef CONFIG_VIDEO_ADV_DEBUG
3568c2ecf20Sopenharmony_cistatic int cx18_g_register(struct file *file, void *fh,
3578c2ecf20Sopenharmony_ci				struct v4l2_dbg_register *reg)
3588c2ecf20Sopenharmony_ci{
3598c2ecf20Sopenharmony_ci	struct cx18 *cx = fh2id(fh)->cx;
3608c2ecf20Sopenharmony_ci
3618c2ecf20Sopenharmony_ci	if (reg->reg & 0x3)
3628c2ecf20Sopenharmony_ci		return -EINVAL;
3638c2ecf20Sopenharmony_ci	if (reg->reg >= CX18_MEM_OFFSET + CX18_MEM_SIZE)
3648c2ecf20Sopenharmony_ci		return -EINVAL;
3658c2ecf20Sopenharmony_ci	reg->size = 4;
3668c2ecf20Sopenharmony_ci	reg->val = cx18_read_enc(cx, reg->reg);
3678c2ecf20Sopenharmony_ci	return 0;
3688c2ecf20Sopenharmony_ci}
3698c2ecf20Sopenharmony_ci
3708c2ecf20Sopenharmony_cistatic int cx18_s_register(struct file *file, void *fh,
3718c2ecf20Sopenharmony_ci				const struct v4l2_dbg_register *reg)
3728c2ecf20Sopenharmony_ci{
3738c2ecf20Sopenharmony_ci	struct cx18 *cx = fh2id(fh)->cx;
3748c2ecf20Sopenharmony_ci
3758c2ecf20Sopenharmony_ci	if (reg->reg & 0x3)
3768c2ecf20Sopenharmony_ci		return -EINVAL;
3778c2ecf20Sopenharmony_ci	if (reg->reg >= CX18_MEM_OFFSET + CX18_MEM_SIZE)
3788c2ecf20Sopenharmony_ci		return -EINVAL;
3798c2ecf20Sopenharmony_ci	cx18_write_enc(cx, reg->val, reg->reg);
3808c2ecf20Sopenharmony_ci	return 0;
3818c2ecf20Sopenharmony_ci}
3828c2ecf20Sopenharmony_ci#endif
3838c2ecf20Sopenharmony_ci
3848c2ecf20Sopenharmony_cistatic int cx18_querycap(struct file *file, void *fh,
3858c2ecf20Sopenharmony_ci				struct v4l2_capability *vcap)
3868c2ecf20Sopenharmony_ci{
3878c2ecf20Sopenharmony_ci	struct cx18_open_id *id = fh2id(fh);
3888c2ecf20Sopenharmony_ci	struct cx18 *cx = id->cx;
3898c2ecf20Sopenharmony_ci
3908c2ecf20Sopenharmony_ci	strscpy(vcap->driver, CX18_DRIVER_NAME, sizeof(vcap->driver));
3918c2ecf20Sopenharmony_ci	strscpy(vcap->card, cx->card_name, sizeof(vcap->card));
3928c2ecf20Sopenharmony_ci	snprintf(vcap->bus_info, sizeof(vcap->bus_info),
3938c2ecf20Sopenharmony_ci		 "PCI:%s", pci_name(cx->pci_dev));
3948c2ecf20Sopenharmony_ci	vcap->capabilities = cx->v4l2_cap | V4L2_CAP_DEVICE_CAPS;
3958c2ecf20Sopenharmony_ci	return 0;
3968c2ecf20Sopenharmony_ci}
3978c2ecf20Sopenharmony_ci
3988c2ecf20Sopenharmony_cistatic int cx18_enumaudio(struct file *file, void *fh, struct v4l2_audio *vin)
3998c2ecf20Sopenharmony_ci{
4008c2ecf20Sopenharmony_ci	struct cx18 *cx = fh2id(fh)->cx;
4018c2ecf20Sopenharmony_ci
4028c2ecf20Sopenharmony_ci	return cx18_get_audio_input(cx, vin->index, vin);
4038c2ecf20Sopenharmony_ci}
4048c2ecf20Sopenharmony_ci
4058c2ecf20Sopenharmony_cistatic int cx18_g_audio(struct file *file, void *fh, struct v4l2_audio *vin)
4068c2ecf20Sopenharmony_ci{
4078c2ecf20Sopenharmony_ci	struct cx18 *cx = fh2id(fh)->cx;
4088c2ecf20Sopenharmony_ci
4098c2ecf20Sopenharmony_ci	vin->index = cx->audio_input;
4108c2ecf20Sopenharmony_ci	return cx18_get_audio_input(cx, vin->index, vin);
4118c2ecf20Sopenharmony_ci}
4128c2ecf20Sopenharmony_ci
4138c2ecf20Sopenharmony_cistatic int cx18_s_audio(struct file *file, void *fh, const struct v4l2_audio *vout)
4148c2ecf20Sopenharmony_ci{
4158c2ecf20Sopenharmony_ci	struct cx18 *cx = fh2id(fh)->cx;
4168c2ecf20Sopenharmony_ci
4178c2ecf20Sopenharmony_ci	if (vout->index >= cx->nof_audio_inputs)
4188c2ecf20Sopenharmony_ci		return -EINVAL;
4198c2ecf20Sopenharmony_ci	cx->audio_input = vout->index;
4208c2ecf20Sopenharmony_ci	cx18_audio_set_io(cx);
4218c2ecf20Sopenharmony_ci	return 0;
4228c2ecf20Sopenharmony_ci}
4238c2ecf20Sopenharmony_ci
4248c2ecf20Sopenharmony_cistatic int cx18_enum_input(struct file *file, void *fh, struct v4l2_input *vin)
4258c2ecf20Sopenharmony_ci{
4268c2ecf20Sopenharmony_ci	struct cx18 *cx = fh2id(fh)->cx;
4278c2ecf20Sopenharmony_ci
4288c2ecf20Sopenharmony_ci	/* set it to defaults from our table */
4298c2ecf20Sopenharmony_ci	return cx18_get_input(cx, vin->index, vin);
4308c2ecf20Sopenharmony_ci}
4318c2ecf20Sopenharmony_ci
4328c2ecf20Sopenharmony_cistatic int cx18_g_pixelaspect(struct file *file, void *fh,
4338c2ecf20Sopenharmony_ci			      int type, struct v4l2_fract *f)
4348c2ecf20Sopenharmony_ci{
4358c2ecf20Sopenharmony_ci	struct cx18 *cx = fh2id(fh)->cx;
4368c2ecf20Sopenharmony_ci
4378c2ecf20Sopenharmony_ci	if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
4388c2ecf20Sopenharmony_ci		return -EINVAL;
4398c2ecf20Sopenharmony_ci
4408c2ecf20Sopenharmony_ci	f->numerator = cx->is_50hz ? 54 : 11;
4418c2ecf20Sopenharmony_ci	f->denominator = cx->is_50hz ? 59 : 10;
4428c2ecf20Sopenharmony_ci	return 0;
4438c2ecf20Sopenharmony_ci}
4448c2ecf20Sopenharmony_ci
4458c2ecf20Sopenharmony_cistatic int cx18_g_selection(struct file *file, void *fh,
4468c2ecf20Sopenharmony_ci			    struct v4l2_selection *sel)
4478c2ecf20Sopenharmony_ci{
4488c2ecf20Sopenharmony_ci	struct cx18 *cx = fh2id(fh)->cx;
4498c2ecf20Sopenharmony_ci
4508c2ecf20Sopenharmony_ci	if (sel->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
4518c2ecf20Sopenharmony_ci		return -EINVAL;
4528c2ecf20Sopenharmony_ci	switch (sel->target) {
4538c2ecf20Sopenharmony_ci	case V4L2_SEL_TGT_CROP_BOUNDS:
4548c2ecf20Sopenharmony_ci	case V4L2_SEL_TGT_CROP_DEFAULT:
4558c2ecf20Sopenharmony_ci		sel->r.top = sel->r.left = 0;
4568c2ecf20Sopenharmony_ci		sel->r.width = 720;
4578c2ecf20Sopenharmony_ci		sel->r.height = cx->is_50hz ? 576 : 480;
4588c2ecf20Sopenharmony_ci		break;
4598c2ecf20Sopenharmony_ci	default:
4608c2ecf20Sopenharmony_ci		return -EINVAL;
4618c2ecf20Sopenharmony_ci	}
4628c2ecf20Sopenharmony_ci	return 0;
4638c2ecf20Sopenharmony_ci}
4648c2ecf20Sopenharmony_ci
4658c2ecf20Sopenharmony_cistatic int cx18_enum_fmt_vid_cap(struct file *file, void *fh,
4668c2ecf20Sopenharmony_ci					struct v4l2_fmtdesc *fmt)
4678c2ecf20Sopenharmony_ci{
4688c2ecf20Sopenharmony_ci	static const struct v4l2_fmtdesc formats[] = {
4698c2ecf20Sopenharmony_ci		{
4708c2ecf20Sopenharmony_ci			.index = 0,
4718c2ecf20Sopenharmony_ci			.type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
4728c2ecf20Sopenharmony_ci			.description = "HM12 (YUV 4:1:1)",
4738c2ecf20Sopenharmony_ci			.pixelformat = V4L2_PIX_FMT_HM12,
4748c2ecf20Sopenharmony_ci		},
4758c2ecf20Sopenharmony_ci		{
4768c2ecf20Sopenharmony_ci			.index = 1,
4778c2ecf20Sopenharmony_ci			.type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
4788c2ecf20Sopenharmony_ci			.flags = V4L2_FMT_FLAG_COMPRESSED,
4798c2ecf20Sopenharmony_ci			.description = "MPEG",
4808c2ecf20Sopenharmony_ci			.pixelformat = V4L2_PIX_FMT_MPEG,
4818c2ecf20Sopenharmony_ci		},
4828c2ecf20Sopenharmony_ci		{
4838c2ecf20Sopenharmony_ci			.index = 2,
4848c2ecf20Sopenharmony_ci			.type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
4858c2ecf20Sopenharmony_ci			.description = "UYVY 4:2:2",
4868c2ecf20Sopenharmony_ci			.pixelformat = V4L2_PIX_FMT_UYVY,
4878c2ecf20Sopenharmony_ci		},
4888c2ecf20Sopenharmony_ci	};
4898c2ecf20Sopenharmony_ci
4908c2ecf20Sopenharmony_ci	if (fmt->index > ARRAY_SIZE(formats) - 1)
4918c2ecf20Sopenharmony_ci		return -EINVAL;
4928c2ecf20Sopenharmony_ci	*fmt = formats[fmt->index];
4938c2ecf20Sopenharmony_ci	return 0;
4948c2ecf20Sopenharmony_ci}
4958c2ecf20Sopenharmony_ci
4968c2ecf20Sopenharmony_cistatic int cx18_g_input(struct file *file, void *fh, unsigned int *i)
4978c2ecf20Sopenharmony_ci{
4988c2ecf20Sopenharmony_ci	struct cx18 *cx = fh2id(fh)->cx;
4998c2ecf20Sopenharmony_ci
5008c2ecf20Sopenharmony_ci	*i = cx->active_input;
5018c2ecf20Sopenharmony_ci	return 0;
5028c2ecf20Sopenharmony_ci}
5038c2ecf20Sopenharmony_ci
5048c2ecf20Sopenharmony_ciint cx18_s_input(struct file *file, void *fh, unsigned int inp)
5058c2ecf20Sopenharmony_ci{
5068c2ecf20Sopenharmony_ci	struct cx18_open_id *id = fh2id(fh);
5078c2ecf20Sopenharmony_ci	struct cx18 *cx = id->cx;
5088c2ecf20Sopenharmony_ci	v4l2_std_id std = V4L2_STD_ALL;
5098c2ecf20Sopenharmony_ci	const struct cx18_card_video_input *card_input =
5108c2ecf20Sopenharmony_ci				cx->card->video_inputs + inp;
5118c2ecf20Sopenharmony_ci
5128c2ecf20Sopenharmony_ci	if (inp >= cx->nof_inputs)
5138c2ecf20Sopenharmony_ci		return -EINVAL;
5148c2ecf20Sopenharmony_ci
5158c2ecf20Sopenharmony_ci	if (inp == cx->active_input) {
5168c2ecf20Sopenharmony_ci		CX18_DEBUG_INFO("Input unchanged\n");
5178c2ecf20Sopenharmony_ci		return 0;
5188c2ecf20Sopenharmony_ci	}
5198c2ecf20Sopenharmony_ci
5208c2ecf20Sopenharmony_ci	CX18_DEBUG_INFO("Changing input from %d to %d\n",
5218c2ecf20Sopenharmony_ci			cx->active_input, inp);
5228c2ecf20Sopenharmony_ci
5238c2ecf20Sopenharmony_ci	cx->active_input = inp;
5248c2ecf20Sopenharmony_ci	/* Set the audio input to whatever is appropriate for the input type. */
5258c2ecf20Sopenharmony_ci	cx->audio_input = cx->card->video_inputs[inp].audio_index;
5268c2ecf20Sopenharmony_ci	if (card_input->video_type == V4L2_INPUT_TYPE_TUNER)
5278c2ecf20Sopenharmony_ci		std = cx->tuner_std;
5288c2ecf20Sopenharmony_ci	cx->streams[CX18_ENC_STREAM_TYPE_MPG].video_dev.tvnorms = std;
5298c2ecf20Sopenharmony_ci	cx->streams[CX18_ENC_STREAM_TYPE_YUV].video_dev.tvnorms = std;
5308c2ecf20Sopenharmony_ci	cx->streams[CX18_ENC_STREAM_TYPE_VBI].video_dev.tvnorms = std;
5318c2ecf20Sopenharmony_ci
5328c2ecf20Sopenharmony_ci	/* prevent others from messing with the streams until
5338c2ecf20Sopenharmony_ci	   we're finished changing inputs. */
5348c2ecf20Sopenharmony_ci	cx18_mute(cx);
5358c2ecf20Sopenharmony_ci	cx18_video_set_io(cx);
5368c2ecf20Sopenharmony_ci	cx18_audio_set_io(cx);
5378c2ecf20Sopenharmony_ci	cx18_unmute(cx);
5388c2ecf20Sopenharmony_ci	return 0;
5398c2ecf20Sopenharmony_ci}
5408c2ecf20Sopenharmony_ci
5418c2ecf20Sopenharmony_cistatic int cx18_g_frequency(struct file *file, void *fh,
5428c2ecf20Sopenharmony_ci				struct v4l2_frequency *vf)
5438c2ecf20Sopenharmony_ci{
5448c2ecf20Sopenharmony_ci	struct cx18 *cx = fh2id(fh)->cx;
5458c2ecf20Sopenharmony_ci
5468c2ecf20Sopenharmony_ci	if (vf->tuner != 0)
5478c2ecf20Sopenharmony_ci		return -EINVAL;
5488c2ecf20Sopenharmony_ci
5498c2ecf20Sopenharmony_ci	cx18_call_all(cx, tuner, g_frequency, vf);
5508c2ecf20Sopenharmony_ci	return 0;
5518c2ecf20Sopenharmony_ci}
5528c2ecf20Sopenharmony_ci
5538c2ecf20Sopenharmony_ciint cx18_s_frequency(struct file *file, void *fh, const struct v4l2_frequency *vf)
5548c2ecf20Sopenharmony_ci{
5558c2ecf20Sopenharmony_ci	struct cx18_open_id *id = fh2id(fh);
5568c2ecf20Sopenharmony_ci	struct cx18 *cx = id->cx;
5578c2ecf20Sopenharmony_ci
5588c2ecf20Sopenharmony_ci	if (vf->tuner != 0)
5598c2ecf20Sopenharmony_ci		return -EINVAL;
5608c2ecf20Sopenharmony_ci
5618c2ecf20Sopenharmony_ci	cx18_mute(cx);
5628c2ecf20Sopenharmony_ci	CX18_DEBUG_INFO("v4l2 ioctl: set frequency %d\n", vf->frequency);
5638c2ecf20Sopenharmony_ci	cx18_call_all(cx, tuner, s_frequency, vf);
5648c2ecf20Sopenharmony_ci	cx18_unmute(cx);
5658c2ecf20Sopenharmony_ci	return 0;
5668c2ecf20Sopenharmony_ci}
5678c2ecf20Sopenharmony_ci
5688c2ecf20Sopenharmony_cistatic int cx18_g_std(struct file *file, void *fh, v4l2_std_id *std)
5698c2ecf20Sopenharmony_ci{
5708c2ecf20Sopenharmony_ci	struct cx18 *cx = fh2id(fh)->cx;
5718c2ecf20Sopenharmony_ci
5728c2ecf20Sopenharmony_ci	*std = cx->std;
5738c2ecf20Sopenharmony_ci	return 0;
5748c2ecf20Sopenharmony_ci}
5758c2ecf20Sopenharmony_ci
5768c2ecf20Sopenharmony_ciint cx18_s_std(struct file *file, void *fh, v4l2_std_id std)
5778c2ecf20Sopenharmony_ci{
5788c2ecf20Sopenharmony_ci	struct cx18_open_id *id = fh2id(fh);
5798c2ecf20Sopenharmony_ci	struct cx18 *cx = id->cx;
5808c2ecf20Sopenharmony_ci
5818c2ecf20Sopenharmony_ci	if ((std & V4L2_STD_ALL) == 0)
5828c2ecf20Sopenharmony_ci		return -EINVAL;
5838c2ecf20Sopenharmony_ci
5848c2ecf20Sopenharmony_ci	if (std == cx->std)
5858c2ecf20Sopenharmony_ci		return 0;
5868c2ecf20Sopenharmony_ci
5878c2ecf20Sopenharmony_ci	if (test_bit(CX18_F_I_RADIO_USER, &cx->i_flags) ||
5888c2ecf20Sopenharmony_ci	    atomic_read(&cx->ana_capturing) > 0) {
5898c2ecf20Sopenharmony_ci		/* Switching standard would turn off the radio or mess
5908c2ecf20Sopenharmony_ci		   with already running streams, prevent that by
5918c2ecf20Sopenharmony_ci		   returning EBUSY. */
5928c2ecf20Sopenharmony_ci		return -EBUSY;
5938c2ecf20Sopenharmony_ci	}
5948c2ecf20Sopenharmony_ci
5958c2ecf20Sopenharmony_ci	cx->std = std;
5968c2ecf20Sopenharmony_ci	cx->is_60hz = (std & V4L2_STD_525_60) ? 1 : 0;
5978c2ecf20Sopenharmony_ci	cx->is_50hz = !cx->is_60hz;
5988c2ecf20Sopenharmony_ci	cx2341x_handler_set_50hz(&cx->cxhdl, cx->is_50hz);
5998c2ecf20Sopenharmony_ci	cx->cxhdl.width = 720;
6008c2ecf20Sopenharmony_ci	cx->cxhdl.height = cx->is_50hz ? 576 : 480;
6018c2ecf20Sopenharmony_ci	cx->vbi.count = cx->is_50hz ? 18 : 12;
6028c2ecf20Sopenharmony_ci	cx->vbi.start[0] = cx->is_50hz ? 6 : 10;
6038c2ecf20Sopenharmony_ci	cx->vbi.start[1] = cx->is_50hz ? 318 : 273;
6048c2ecf20Sopenharmony_ci	CX18_DEBUG_INFO("Switching standard to %llx.\n",
6058c2ecf20Sopenharmony_ci			(unsigned long long) cx->std);
6068c2ecf20Sopenharmony_ci
6078c2ecf20Sopenharmony_ci	/* Tuner */
6088c2ecf20Sopenharmony_ci	cx18_call_all(cx, video, s_std, cx->std);
6098c2ecf20Sopenharmony_ci	return 0;
6108c2ecf20Sopenharmony_ci}
6118c2ecf20Sopenharmony_ci
6128c2ecf20Sopenharmony_cistatic int cx18_s_tuner(struct file *file, void *fh, const struct v4l2_tuner *vt)
6138c2ecf20Sopenharmony_ci{
6148c2ecf20Sopenharmony_ci	struct cx18_open_id *id = fh2id(fh);
6158c2ecf20Sopenharmony_ci	struct cx18 *cx = id->cx;
6168c2ecf20Sopenharmony_ci
6178c2ecf20Sopenharmony_ci	if (vt->index != 0)
6188c2ecf20Sopenharmony_ci		return -EINVAL;
6198c2ecf20Sopenharmony_ci
6208c2ecf20Sopenharmony_ci	cx18_call_all(cx, tuner, s_tuner, vt);
6218c2ecf20Sopenharmony_ci	return 0;
6228c2ecf20Sopenharmony_ci}
6238c2ecf20Sopenharmony_ci
6248c2ecf20Sopenharmony_cistatic int cx18_g_tuner(struct file *file, void *fh, struct v4l2_tuner *vt)
6258c2ecf20Sopenharmony_ci{
6268c2ecf20Sopenharmony_ci	struct cx18 *cx = fh2id(fh)->cx;
6278c2ecf20Sopenharmony_ci
6288c2ecf20Sopenharmony_ci	if (vt->index != 0)
6298c2ecf20Sopenharmony_ci		return -EINVAL;
6308c2ecf20Sopenharmony_ci
6318c2ecf20Sopenharmony_ci	cx18_call_all(cx, tuner, g_tuner, vt);
6328c2ecf20Sopenharmony_ci
6338c2ecf20Sopenharmony_ci	if (vt->type == V4L2_TUNER_RADIO)
6348c2ecf20Sopenharmony_ci		strscpy(vt->name, "cx18 Radio Tuner", sizeof(vt->name));
6358c2ecf20Sopenharmony_ci	else
6368c2ecf20Sopenharmony_ci		strscpy(vt->name, "cx18 TV Tuner", sizeof(vt->name));
6378c2ecf20Sopenharmony_ci	return 0;
6388c2ecf20Sopenharmony_ci}
6398c2ecf20Sopenharmony_ci
6408c2ecf20Sopenharmony_cistatic int cx18_g_sliced_vbi_cap(struct file *file, void *fh,
6418c2ecf20Sopenharmony_ci					struct v4l2_sliced_vbi_cap *cap)
6428c2ecf20Sopenharmony_ci{
6438c2ecf20Sopenharmony_ci	struct cx18 *cx = fh2id(fh)->cx;
6448c2ecf20Sopenharmony_ci	int set = cx->is_50hz ? V4L2_SLICED_VBI_625 : V4L2_SLICED_VBI_525;
6458c2ecf20Sopenharmony_ci	int f, l;
6468c2ecf20Sopenharmony_ci
6478c2ecf20Sopenharmony_ci	if (cap->type != V4L2_BUF_TYPE_SLICED_VBI_CAPTURE)
6488c2ecf20Sopenharmony_ci		return -EINVAL;
6498c2ecf20Sopenharmony_ci
6508c2ecf20Sopenharmony_ci	cap->service_set = 0;
6518c2ecf20Sopenharmony_ci	for (f = 0; f < 2; f++) {
6528c2ecf20Sopenharmony_ci		for (l = 0; l < 24; l++) {
6538c2ecf20Sopenharmony_ci			if (valid_service_line(f, l, cx->is_50hz)) {
6548c2ecf20Sopenharmony_ci				/*
6558c2ecf20Sopenharmony_ci				 * We can find all v4l2 supported vbi services
6568c2ecf20Sopenharmony_ci				 * for the standard, on a valid line for the std
6578c2ecf20Sopenharmony_ci				 */
6588c2ecf20Sopenharmony_ci				cap->service_lines[f][l] = set;
6598c2ecf20Sopenharmony_ci				cap->service_set |= set;
6608c2ecf20Sopenharmony_ci			} else
6618c2ecf20Sopenharmony_ci				cap->service_lines[f][l] = 0;
6628c2ecf20Sopenharmony_ci		}
6638c2ecf20Sopenharmony_ci	}
6648c2ecf20Sopenharmony_ci	for (f = 0; f < 3; f++)
6658c2ecf20Sopenharmony_ci		cap->reserved[f] = 0;
6668c2ecf20Sopenharmony_ci	return 0;
6678c2ecf20Sopenharmony_ci}
6688c2ecf20Sopenharmony_ci
6698c2ecf20Sopenharmony_cistatic int _cx18_process_idx_data(struct cx18_buffer *buf,
6708c2ecf20Sopenharmony_ci				  struct v4l2_enc_idx *idx)
6718c2ecf20Sopenharmony_ci{
6728c2ecf20Sopenharmony_ci	int consumed, remaining;
6738c2ecf20Sopenharmony_ci	struct v4l2_enc_idx_entry *e_idx;
6748c2ecf20Sopenharmony_ci	struct cx18_enc_idx_entry *e_buf;
6758c2ecf20Sopenharmony_ci
6768c2ecf20Sopenharmony_ci	/* Frame type lookup: 1=I, 2=P, 4=B */
6778c2ecf20Sopenharmony_ci	static const int mapping[8] = {
6788c2ecf20Sopenharmony_ci		-1, V4L2_ENC_IDX_FRAME_I, V4L2_ENC_IDX_FRAME_P,
6798c2ecf20Sopenharmony_ci		-1, V4L2_ENC_IDX_FRAME_B, -1, -1, -1
6808c2ecf20Sopenharmony_ci	};
6818c2ecf20Sopenharmony_ci
6828c2ecf20Sopenharmony_ci	/*
6838c2ecf20Sopenharmony_ci	 * Assumption here is that a buf holds an integral number of
6848c2ecf20Sopenharmony_ci	 * struct cx18_enc_idx_entry objects and is properly aligned.
6858c2ecf20Sopenharmony_ci	 * This is enforced by the module options on IDX buffer sizes.
6868c2ecf20Sopenharmony_ci	 */
6878c2ecf20Sopenharmony_ci	remaining = buf->bytesused - buf->readpos;
6888c2ecf20Sopenharmony_ci	consumed = 0;
6898c2ecf20Sopenharmony_ci	e_idx = &idx->entry[idx->entries];
6908c2ecf20Sopenharmony_ci	e_buf = (struct cx18_enc_idx_entry *) &buf->buf[buf->readpos];
6918c2ecf20Sopenharmony_ci
6928c2ecf20Sopenharmony_ci	while (remaining >= sizeof(struct cx18_enc_idx_entry) &&
6938c2ecf20Sopenharmony_ci	       idx->entries < V4L2_ENC_IDX_ENTRIES) {
6948c2ecf20Sopenharmony_ci
6958c2ecf20Sopenharmony_ci		e_idx->offset = (((u64) le32_to_cpu(e_buf->offset_high)) << 32)
6968c2ecf20Sopenharmony_ci				| le32_to_cpu(e_buf->offset_low);
6978c2ecf20Sopenharmony_ci
6988c2ecf20Sopenharmony_ci		e_idx->pts = (((u64) (le32_to_cpu(e_buf->pts_high) & 1)) << 32)
6998c2ecf20Sopenharmony_ci			     | le32_to_cpu(e_buf->pts_low);
7008c2ecf20Sopenharmony_ci
7018c2ecf20Sopenharmony_ci		e_idx->length = le32_to_cpu(e_buf->length);
7028c2ecf20Sopenharmony_ci
7038c2ecf20Sopenharmony_ci		e_idx->flags = mapping[le32_to_cpu(e_buf->flags) & 0x7];
7048c2ecf20Sopenharmony_ci
7058c2ecf20Sopenharmony_ci		e_idx->reserved[0] = 0;
7068c2ecf20Sopenharmony_ci		e_idx->reserved[1] = 0;
7078c2ecf20Sopenharmony_ci
7088c2ecf20Sopenharmony_ci		idx->entries++;
7098c2ecf20Sopenharmony_ci		e_idx = &idx->entry[idx->entries];
7108c2ecf20Sopenharmony_ci		e_buf++;
7118c2ecf20Sopenharmony_ci
7128c2ecf20Sopenharmony_ci		remaining -= sizeof(struct cx18_enc_idx_entry);
7138c2ecf20Sopenharmony_ci		consumed += sizeof(struct cx18_enc_idx_entry);
7148c2ecf20Sopenharmony_ci	}
7158c2ecf20Sopenharmony_ci
7168c2ecf20Sopenharmony_ci	/* Swallow any partial entries at the end, if there are any */
7178c2ecf20Sopenharmony_ci	if (remaining > 0 && remaining < sizeof(struct cx18_enc_idx_entry))
7188c2ecf20Sopenharmony_ci		consumed += remaining;
7198c2ecf20Sopenharmony_ci
7208c2ecf20Sopenharmony_ci	buf->readpos += consumed;
7218c2ecf20Sopenharmony_ci	return consumed;
7228c2ecf20Sopenharmony_ci}
7238c2ecf20Sopenharmony_ci
7248c2ecf20Sopenharmony_cistatic int cx18_process_idx_data(struct cx18_stream *s, struct cx18_mdl *mdl,
7258c2ecf20Sopenharmony_ci				 struct v4l2_enc_idx *idx)
7268c2ecf20Sopenharmony_ci{
7278c2ecf20Sopenharmony_ci	if (s->type != CX18_ENC_STREAM_TYPE_IDX)
7288c2ecf20Sopenharmony_ci		return -EINVAL;
7298c2ecf20Sopenharmony_ci
7308c2ecf20Sopenharmony_ci	if (mdl->curr_buf == NULL)
7318c2ecf20Sopenharmony_ci		mdl->curr_buf = list_first_entry(&mdl->buf_list,
7328c2ecf20Sopenharmony_ci						 struct cx18_buffer, list);
7338c2ecf20Sopenharmony_ci
7348c2ecf20Sopenharmony_ci	if (list_entry_is_past_end(mdl->curr_buf, &mdl->buf_list, list)) {
7358c2ecf20Sopenharmony_ci		/*
7368c2ecf20Sopenharmony_ci		 * For some reason we've exhausted the buffers, but the MDL
7378c2ecf20Sopenharmony_ci		 * object still said some data was unread.
7388c2ecf20Sopenharmony_ci		 * Fix that and bail out.
7398c2ecf20Sopenharmony_ci		 */
7408c2ecf20Sopenharmony_ci		mdl->readpos = mdl->bytesused;
7418c2ecf20Sopenharmony_ci		return 0;
7428c2ecf20Sopenharmony_ci	}
7438c2ecf20Sopenharmony_ci
7448c2ecf20Sopenharmony_ci	list_for_each_entry_from(mdl->curr_buf, &mdl->buf_list, list) {
7458c2ecf20Sopenharmony_ci
7468c2ecf20Sopenharmony_ci		/* Skip any empty buffers in the MDL */
7478c2ecf20Sopenharmony_ci		if (mdl->curr_buf->readpos >= mdl->curr_buf->bytesused)
7488c2ecf20Sopenharmony_ci			continue;
7498c2ecf20Sopenharmony_ci
7508c2ecf20Sopenharmony_ci		mdl->readpos += _cx18_process_idx_data(mdl->curr_buf, idx);
7518c2ecf20Sopenharmony_ci
7528c2ecf20Sopenharmony_ci		/* exit when MDL drained or request satisfied */
7538c2ecf20Sopenharmony_ci		if (idx->entries >= V4L2_ENC_IDX_ENTRIES ||
7548c2ecf20Sopenharmony_ci		    mdl->curr_buf->readpos < mdl->curr_buf->bytesused ||
7558c2ecf20Sopenharmony_ci		    mdl->readpos >= mdl->bytesused)
7568c2ecf20Sopenharmony_ci			break;
7578c2ecf20Sopenharmony_ci	}
7588c2ecf20Sopenharmony_ci	return 0;
7598c2ecf20Sopenharmony_ci}
7608c2ecf20Sopenharmony_ci
7618c2ecf20Sopenharmony_cistatic int cx18_g_enc_index(struct file *file, void *fh,
7628c2ecf20Sopenharmony_ci				struct v4l2_enc_idx *idx)
7638c2ecf20Sopenharmony_ci{
7648c2ecf20Sopenharmony_ci	struct cx18 *cx = fh2id(fh)->cx;
7658c2ecf20Sopenharmony_ci	struct cx18_stream *s = &cx->streams[CX18_ENC_STREAM_TYPE_IDX];
7668c2ecf20Sopenharmony_ci	s32 tmp;
7678c2ecf20Sopenharmony_ci	struct cx18_mdl *mdl;
7688c2ecf20Sopenharmony_ci
7698c2ecf20Sopenharmony_ci	if (!cx18_stream_enabled(s)) /* Module options inhibited IDX stream */
7708c2ecf20Sopenharmony_ci		return -EINVAL;
7718c2ecf20Sopenharmony_ci
7728c2ecf20Sopenharmony_ci	/* Compute the best case number of entries we can buffer */
7738c2ecf20Sopenharmony_ci	tmp = s->buffers -
7748c2ecf20Sopenharmony_ci			  s->bufs_per_mdl * CX18_ENC_STREAM_TYPE_IDX_FW_MDL_MIN;
7758c2ecf20Sopenharmony_ci	if (tmp <= 0)
7768c2ecf20Sopenharmony_ci		tmp = 1;
7778c2ecf20Sopenharmony_ci	tmp = tmp * s->buf_size / sizeof(struct cx18_enc_idx_entry);
7788c2ecf20Sopenharmony_ci
7798c2ecf20Sopenharmony_ci	/* Fill out the header of the return structure */
7808c2ecf20Sopenharmony_ci	idx->entries = 0;
7818c2ecf20Sopenharmony_ci	idx->entries_cap = tmp;
7828c2ecf20Sopenharmony_ci	memset(idx->reserved, 0, sizeof(idx->reserved));
7838c2ecf20Sopenharmony_ci
7848c2ecf20Sopenharmony_ci	/* Pull IDX MDLs and buffers from q_full and populate the entries */
7858c2ecf20Sopenharmony_ci	do {
7868c2ecf20Sopenharmony_ci		mdl = cx18_dequeue(s, &s->q_full);
7878c2ecf20Sopenharmony_ci		if (mdl == NULL) /* No more IDX data right now */
7888c2ecf20Sopenharmony_ci			break;
7898c2ecf20Sopenharmony_ci
7908c2ecf20Sopenharmony_ci		/* Extract the Index entry data from the MDL and buffers */
7918c2ecf20Sopenharmony_ci		cx18_process_idx_data(s, mdl, idx);
7928c2ecf20Sopenharmony_ci		if (mdl->readpos < mdl->bytesused) {
7938c2ecf20Sopenharmony_ci			/* We finished with data remaining, push the MDL back */
7948c2ecf20Sopenharmony_ci			cx18_push(s, mdl, &s->q_full);
7958c2ecf20Sopenharmony_ci			break;
7968c2ecf20Sopenharmony_ci		}
7978c2ecf20Sopenharmony_ci
7988c2ecf20Sopenharmony_ci		/* We drained this MDL, schedule it to go to the firmware */
7998c2ecf20Sopenharmony_ci		cx18_enqueue(s, mdl, &s->q_free);
8008c2ecf20Sopenharmony_ci
8018c2ecf20Sopenharmony_ci	} while (idx->entries < V4L2_ENC_IDX_ENTRIES);
8028c2ecf20Sopenharmony_ci
8038c2ecf20Sopenharmony_ci	/* Tell the work handler to send free IDX MDLs to the firmware */
8048c2ecf20Sopenharmony_ci	cx18_stream_load_fw_queue(s);
8058c2ecf20Sopenharmony_ci	return 0;
8068c2ecf20Sopenharmony_ci}
8078c2ecf20Sopenharmony_ci
8088c2ecf20Sopenharmony_cistatic struct videobuf_queue *cx18_vb_queue(struct cx18_open_id *id)
8098c2ecf20Sopenharmony_ci{
8108c2ecf20Sopenharmony_ci	struct videobuf_queue *q = NULL;
8118c2ecf20Sopenharmony_ci	struct cx18 *cx = id->cx;
8128c2ecf20Sopenharmony_ci	struct cx18_stream *s = &cx->streams[id->type];
8138c2ecf20Sopenharmony_ci
8148c2ecf20Sopenharmony_ci	switch (s->vb_type) {
8158c2ecf20Sopenharmony_ci	case V4L2_BUF_TYPE_VIDEO_CAPTURE:
8168c2ecf20Sopenharmony_ci		q = &s->vbuf_q;
8178c2ecf20Sopenharmony_ci		break;
8188c2ecf20Sopenharmony_ci	case V4L2_BUF_TYPE_VBI_CAPTURE:
8198c2ecf20Sopenharmony_ci		break;
8208c2ecf20Sopenharmony_ci	default:
8218c2ecf20Sopenharmony_ci		break;
8228c2ecf20Sopenharmony_ci	}
8238c2ecf20Sopenharmony_ci	return q;
8248c2ecf20Sopenharmony_ci}
8258c2ecf20Sopenharmony_ci
8268c2ecf20Sopenharmony_cistatic int cx18_streamon(struct file *file, void *priv,
8278c2ecf20Sopenharmony_ci	enum v4l2_buf_type type)
8288c2ecf20Sopenharmony_ci{
8298c2ecf20Sopenharmony_ci	struct cx18_open_id *id = file->private_data;
8308c2ecf20Sopenharmony_ci	struct cx18 *cx = id->cx;
8318c2ecf20Sopenharmony_ci	struct cx18_stream *s = &cx->streams[id->type];
8328c2ecf20Sopenharmony_ci
8338c2ecf20Sopenharmony_ci	/* Start the hardware only if we're the video device */
8348c2ecf20Sopenharmony_ci	if ((s->vb_type != V4L2_BUF_TYPE_VIDEO_CAPTURE) &&
8358c2ecf20Sopenharmony_ci		(s->vb_type != V4L2_BUF_TYPE_VBI_CAPTURE))
8368c2ecf20Sopenharmony_ci		return -EINVAL;
8378c2ecf20Sopenharmony_ci
8388c2ecf20Sopenharmony_ci	if (id->type != CX18_ENC_STREAM_TYPE_YUV)
8398c2ecf20Sopenharmony_ci		return -EINVAL;
8408c2ecf20Sopenharmony_ci
8418c2ecf20Sopenharmony_ci	/* Establish a buffer timeout */
8428c2ecf20Sopenharmony_ci	mod_timer(&s->vb_timeout, msecs_to_jiffies(2000) + jiffies);
8438c2ecf20Sopenharmony_ci
8448c2ecf20Sopenharmony_ci	return videobuf_streamon(cx18_vb_queue(id));
8458c2ecf20Sopenharmony_ci}
8468c2ecf20Sopenharmony_ci
8478c2ecf20Sopenharmony_cistatic int cx18_streamoff(struct file *file, void *priv,
8488c2ecf20Sopenharmony_ci	enum v4l2_buf_type type)
8498c2ecf20Sopenharmony_ci{
8508c2ecf20Sopenharmony_ci	struct cx18_open_id *id = file->private_data;
8518c2ecf20Sopenharmony_ci	struct cx18 *cx = id->cx;
8528c2ecf20Sopenharmony_ci	struct cx18_stream *s = &cx->streams[id->type];
8538c2ecf20Sopenharmony_ci
8548c2ecf20Sopenharmony_ci	/* Start the hardware only if we're the video device */
8558c2ecf20Sopenharmony_ci	if ((s->vb_type != V4L2_BUF_TYPE_VIDEO_CAPTURE) &&
8568c2ecf20Sopenharmony_ci		(s->vb_type != V4L2_BUF_TYPE_VBI_CAPTURE))
8578c2ecf20Sopenharmony_ci		return -EINVAL;
8588c2ecf20Sopenharmony_ci
8598c2ecf20Sopenharmony_ci	if (id->type != CX18_ENC_STREAM_TYPE_YUV)
8608c2ecf20Sopenharmony_ci		return -EINVAL;
8618c2ecf20Sopenharmony_ci
8628c2ecf20Sopenharmony_ci	return videobuf_streamoff(cx18_vb_queue(id));
8638c2ecf20Sopenharmony_ci}
8648c2ecf20Sopenharmony_ci
8658c2ecf20Sopenharmony_cistatic int cx18_reqbufs(struct file *file, void *priv,
8668c2ecf20Sopenharmony_ci	struct v4l2_requestbuffers *rb)
8678c2ecf20Sopenharmony_ci{
8688c2ecf20Sopenharmony_ci	struct cx18_open_id *id = file->private_data;
8698c2ecf20Sopenharmony_ci	struct cx18 *cx = id->cx;
8708c2ecf20Sopenharmony_ci	struct cx18_stream *s = &cx->streams[id->type];
8718c2ecf20Sopenharmony_ci
8728c2ecf20Sopenharmony_ci	if ((s->vb_type != V4L2_BUF_TYPE_VIDEO_CAPTURE) &&
8738c2ecf20Sopenharmony_ci		(s->vb_type != V4L2_BUF_TYPE_VBI_CAPTURE))
8748c2ecf20Sopenharmony_ci		return -EINVAL;
8758c2ecf20Sopenharmony_ci
8768c2ecf20Sopenharmony_ci	return videobuf_reqbufs(cx18_vb_queue(id), rb);
8778c2ecf20Sopenharmony_ci}
8788c2ecf20Sopenharmony_ci
8798c2ecf20Sopenharmony_cistatic int cx18_querybuf(struct file *file, void *priv,
8808c2ecf20Sopenharmony_ci	struct v4l2_buffer *b)
8818c2ecf20Sopenharmony_ci{
8828c2ecf20Sopenharmony_ci	struct cx18_open_id *id = file->private_data;
8838c2ecf20Sopenharmony_ci	struct cx18 *cx = id->cx;
8848c2ecf20Sopenharmony_ci	struct cx18_stream *s = &cx->streams[id->type];
8858c2ecf20Sopenharmony_ci
8868c2ecf20Sopenharmony_ci	if ((s->vb_type != V4L2_BUF_TYPE_VIDEO_CAPTURE) &&
8878c2ecf20Sopenharmony_ci		(s->vb_type != V4L2_BUF_TYPE_VBI_CAPTURE))
8888c2ecf20Sopenharmony_ci		return -EINVAL;
8898c2ecf20Sopenharmony_ci
8908c2ecf20Sopenharmony_ci	return videobuf_querybuf(cx18_vb_queue(id), b);
8918c2ecf20Sopenharmony_ci}
8928c2ecf20Sopenharmony_ci
8938c2ecf20Sopenharmony_cistatic int cx18_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
8948c2ecf20Sopenharmony_ci{
8958c2ecf20Sopenharmony_ci	struct cx18_open_id *id = file->private_data;
8968c2ecf20Sopenharmony_ci	struct cx18 *cx = id->cx;
8978c2ecf20Sopenharmony_ci	struct cx18_stream *s = &cx->streams[id->type];
8988c2ecf20Sopenharmony_ci
8998c2ecf20Sopenharmony_ci	if ((s->vb_type != V4L2_BUF_TYPE_VIDEO_CAPTURE) &&
9008c2ecf20Sopenharmony_ci		(s->vb_type != V4L2_BUF_TYPE_VBI_CAPTURE))
9018c2ecf20Sopenharmony_ci		return -EINVAL;
9028c2ecf20Sopenharmony_ci
9038c2ecf20Sopenharmony_ci	return videobuf_qbuf(cx18_vb_queue(id), b);
9048c2ecf20Sopenharmony_ci}
9058c2ecf20Sopenharmony_ci
9068c2ecf20Sopenharmony_cistatic int cx18_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
9078c2ecf20Sopenharmony_ci{
9088c2ecf20Sopenharmony_ci	struct cx18_open_id *id = file->private_data;
9098c2ecf20Sopenharmony_ci	struct cx18 *cx = id->cx;
9108c2ecf20Sopenharmony_ci	struct cx18_stream *s = &cx->streams[id->type];
9118c2ecf20Sopenharmony_ci
9128c2ecf20Sopenharmony_ci	if ((s->vb_type != V4L2_BUF_TYPE_VIDEO_CAPTURE) &&
9138c2ecf20Sopenharmony_ci		(s->vb_type != V4L2_BUF_TYPE_VBI_CAPTURE))
9148c2ecf20Sopenharmony_ci		return -EINVAL;
9158c2ecf20Sopenharmony_ci
9168c2ecf20Sopenharmony_ci	return videobuf_dqbuf(cx18_vb_queue(id), b, file->f_flags & O_NONBLOCK);
9178c2ecf20Sopenharmony_ci}
9188c2ecf20Sopenharmony_ci
9198c2ecf20Sopenharmony_cistatic int cx18_encoder_cmd(struct file *file, void *fh,
9208c2ecf20Sopenharmony_ci				struct v4l2_encoder_cmd *enc)
9218c2ecf20Sopenharmony_ci{
9228c2ecf20Sopenharmony_ci	struct cx18_open_id *id = fh2id(fh);
9238c2ecf20Sopenharmony_ci	struct cx18 *cx = id->cx;
9248c2ecf20Sopenharmony_ci	u32 h;
9258c2ecf20Sopenharmony_ci
9268c2ecf20Sopenharmony_ci	switch (enc->cmd) {
9278c2ecf20Sopenharmony_ci	case V4L2_ENC_CMD_START:
9288c2ecf20Sopenharmony_ci		CX18_DEBUG_IOCTL("V4L2_ENC_CMD_START\n");
9298c2ecf20Sopenharmony_ci		enc->flags = 0;
9308c2ecf20Sopenharmony_ci		return cx18_start_capture(id);
9318c2ecf20Sopenharmony_ci
9328c2ecf20Sopenharmony_ci	case V4L2_ENC_CMD_STOP:
9338c2ecf20Sopenharmony_ci		CX18_DEBUG_IOCTL("V4L2_ENC_CMD_STOP\n");
9348c2ecf20Sopenharmony_ci		enc->flags &= V4L2_ENC_CMD_STOP_AT_GOP_END;
9358c2ecf20Sopenharmony_ci		cx18_stop_capture(id,
9368c2ecf20Sopenharmony_ci				  enc->flags & V4L2_ENC_CMD_STOP_AT_GOP_END);
9378c2ecf20Sopenharmony_ci		break;
9388c2ecf20Sopenharmony_ci
9398c2ecf20Sopenharmony_ci	case V4L2_ENC_CMD_PAUSE:
9408c2ecf20Sopenharmony_ci		CX18_DEBUG_IOCTL("V4L2_ENC_CMD_PAUSE\n");
9418c2ecf20Sopenharmony_ci		enc->flags = 0;
9428c2ecf20Sopenharmony_ci		if (!atomic_read(&cx->ana_capturing))
9438c2ecf20Sopenharmony_ci			return -EPERM;
9448c2ecf20Sopenharmony_ci		if (test_and_set_bit(CX18_F_I_ENC_PAUSED, &cx->i_flags))
9458c2ecf20Sopenharmony_ci			return 0;
9468c2ecf20Sopenharmony_ci		h = cx18_find_handle(cx);
9478c2ecf20Sopenharmony_ci		if (h == CX18_INVALID_TASK_HANDLE) {
9488c2ecf20Sopenharmony_ci			CX18_ERR("Can't find valid task handle for V4L2_ENC_CMD_PAUSE\n");
9498c2ecf20Sopenharmony_ci			return -EBADFD;
9508c2ecf20Sopenharmony_ci		}
9518c2ecf20Sopenharmony_ci		cx18_mute(cx);
9528c2ecf20Sopenharmony_ci		cx18_vapi(cx, CX18_CPU_CAPTURE_PAUSE, 1, h);
9538c2ecf20Sopenharmony_ci		break;
9548c2ecf20Sopenharmony_ci
9558c2ecf20Sopenharmony_ci	case V4L2_ENC_CMD_RESUME:
9568c2ecf20Sopenharmony_ci		CX18_DEBUG_IOCTL("V4L2_ENC_CMD_RESUME\n");
9578c2ecf20Sopenharmony_ci		enc->flags = 0;
9588c2ecf20Sopenharmony_ci		if (!atomic_read(&cx->ana_capturing))
9598c2ecf20Sopenharmony_ci			return -EPERM;
9608c2ecf20Sopenharmony_ci		if (!test_and_clear_bit(CX18_F_I_ENC_PAUSED, &cx->i_flags))
9618c2ecf20Sopenharmony_ci			return 0;
9628c2ecf20Sopenharmony_ci		h = cx18_find_handle(cx);
9638c2ecf20Sopenharmony_ci		if (h == CX18_INVALID_TASK_HANDLE) {
9648c2ecf20Sopenharmony_ci			CX18_ERR("Can't find valid task handle for V4L2_ENC_CMD_RESUME\n");
9658c2ecf20Sopenharmony_ci			return -EBADFD;
9668c2ecf20Sopenharmony_ci		}
9678c2ecf20Sopenharmony_ci		cx18_vapi(cx, CX18_CPU_CAPTURE_RESUME, 1, h);
9688c2ecf20Sopenharmony_ci		cx18_unmute(cx);
9698c2ecf20Sopenharmony_ci		break;
9708c2ecf20Sopenharmony_ci
9718c2ecf20Sopenharmony_ci	default:
9728c2ecf20Sopenharmony_ci		CX18_DEBUG_IOCTL("Unknown cmd %d\n", enc->cmd);
9738c2ecf20Sopenharmony_ci		return -EINVAL;
9748c2ecf20Sopenharmony_ci	}
9758c2ecf20Sopenharmony_ci	return 0;
9768c2ecf20Sopenharmony_ci}
9778c2ecf20Sopenharmony_ci
9788c2ecf20Sopenharmony_cistatic int cx18_try_encoder_cmd(struct file *file, void *fh,
9798c2ecf20Sopenharmony_ci				struct v4l2_encoder_cmd *enc)
9808c2ecf20Sopenharmony_ci{
9818c2ecf20Sopenharmony_ci	struct cx18 *cx = fh2id(fh)->cx;
9828c2ecf20Sopenharmony_ci
9838c2ecf20Sopenharmony_ci	switch (enc->cmd) {
9848c2ecf20Sopenharmony_ci	case V4L2_ENC_CMD_START:
9858c2ecf20Sopenharmony_ci		CX18_DEBUG_IOCTL("V4L2_ENC_CMD_START\n");
9868c2ecf20Sopenharmony_ci		enc->flags = 0;
9878c2ecf20Sopenharmony_ci		break;
9888c2ecf20Sopenharmony_ci
9898c2ecf20Sopenharmony_ci	case V4L2_ENC_CMD_STOP:
9908c2ecf20Sopenharmony_ci		CX18_DEBUG_IOCTL("V4L2_ENC_CMD_STOP\n");
9918c2ecf20Sopenharmony_ci		enc->flags &= V4L2_ENC_CMD_STOP_AT_GOP_END;
9928c2ecf20Sopenharmony_ci		break;
9938c2ecf20Sopenharmony_ci
9948c2ecf20Sopenharmony_ci	case V4L2_ENC_CMD_PAUSE:
9958c2ecf20Sopenharmony_ci		CX18_DEBUG_IOCTL("V4L2_ENC_CMD_PAUSE\n");
9968c2ecf20Sopenharmony_ci		enc->flags = 0;
9978c2ecf20Sopenharmony_ci		break;
9988c2ecf20Sopenharmony_ci
9998c2ecf20Sopenharmony_ci	case V4L2_ENC_CMD_RESUME:
10008c2ecf20Sopenharmony_ci		CX18_DEBUG_IOCTL("V4L2_ENC_CMD_RESUME\n");
10018c2ecf20Sopenharmony_ci		enc->flags = 0;
10028c2ecf20Sopenharmony_ci		break;
10038c2ecf20Sopenharmony_ci
10048c2ecf20Sopenharmony_ci	default:
10058c2ecf20Sopenharmony_ci		CX18_DEBUG_IOCTL("Unknown cmd %d\n", enc->cmd);
10068c2ecf20Sopenharmony_ci		return -EINVAL;
10078c2ecf20Sopenharmony_ci	}
10088c2ecf20Sopenharmony_ci	return 0;
10098c2ecf20Sopenharmony_ci}
10108c2ecf20Sopenharmony_ci
10118c2ecf20Sopenharmony_cistatic int cx18_log_status(struct file *file, void *fh)
10128c2ecf20Sopenharmony_ci{
10138c2ecf20Sopenharmony_ci	struct cx18 *cx = fh2id(fh)->cx;
10148c2ecf20Sopenharmony_ci	struct v4l2_input vidin;
10158c2ecf20Sopenharmony_ci	struct v4l2_audio audin;
10168c2ecf20Sopenharmony_ci	int i;
10178c2ecf20Sopenharmony_ci
10188c2ecf20Sopenharmony_ci	CX18_INFO("Version: %s  Card: %s\n", CX18_VERSION, cx->card_name);
10198c2ecf20Sopenharmony_ci	if (cx->hw_flags & CX18_HW_TVEEPROM) {
10208c2ecf20Sopenharmony_ci		struct tveeprom tv;
10218c2ecf20Sopenharmony_ci
10228c2ecf20Sopenharmony_ci		cx18_read_eeprom(cx, &tv);
10238c2ecf20Sopenharmony_ci	}
10248c2ecf20Sopenharmony_ci	cx18_call_all(cx, core, log_status);
10258c2ecf20Sopenharmony_ci	cx18_get_input(cx, cx->active_input, &vidin);
10268c2ecf20Sopenharmony_ci	cx18_get_audio_input(cx, cx->audio_input, &audin);
10278c2ecf20Sopenharmony_ci	CX18_INFO("Video Input: %s\n", vidin.name);
10288c2ecf20Sopenharmony_ci	CX18_INFO("Audio Input: %s\n", audin.name);
10298c2ecf20Sopenharmony_ci	mutex_lock(&cx->gpio_lock);
10308c2ecf20Sopenharmony_ci	CX18_INFO("GPIO:  direction 0x%08x, value 0x%08x\n",
10318c2ecf20Sopenharmony_ci		cx->gpio_dir, cx->gpio_val);
10328c2ecf20Sopenharmony_ci	mutex_unlock(&cx->gpio_lock);
10338c2ecf20Sopenharmony_ci	CX18_INFO("Tuner: %s\n",
10348c2ecf20Sopenharmony_ci		test_bit(CX18_F_I_RADIO_USER, &cx->i_flags) ?  "Radio" : "TV");
10358c2ecf20Sopenharmony_ci	v4l2_ctrl_handler_log_status(&cx->cxhdl.hdl, cx->v4l2_dev.name);
10368c2ecf20Sopenharmony_ci	CX18_INFO("Status flags: 0x%08lx\n", cx->i_flags);
10378c2ecf20Sopenharmony_ci	for (i = 0; i < CX18_MAX_STREAMS; i++) {
10388c2ecf20Sopenharmony_ci		struct cx18_stream *s = &cx->streams[i];
10398c2ecf20Sopenharmony_ci
10408c2ecf20Sopenharmony_ci		if (s->video_dev.v4l2_dev == NULL || s->buffers == 0)
10418c2ecf20Sopenharmony_ci			continue;
10428c2ecf20Sopenharmony_ci		CX18_INFO("Stream %s: status 0x%04lx, %d%% of %d KiB (%d buffers) in use\n",
10438c2ecf20Sopenharmony_ci			  s->name, s->s_flags,
10448c2ecf20Sopenharmony_ci			  atomic_read(&s->q_full.depth) * s->bufs_per_mdl * 100
10458c2ecf20Sopenharmony_ci			   / s->buffers,
10468c2ecf20Sopenharmony_ci			  (s->buffers * s->buf_size) / 1024, s->buffers);
10478c2ecf20Sopenharmony_ci	}
10488c2ecf20Sopenharmony_ci	CX18_INFO("Read MPEG/VBI: %lld/%lld bytes\n",
10498c2ecf20Sopenharmony_ci			(long long)cx->mpg_data_received,
10508c2ecf20Sopenharmony_ci			(long long)cx->vbi_data_inserted);
10518c2ecf20Sopenharmony_ci	return 0;
10528c2ecf20Sopenharmony_ci}
10538c2ecf20Sopenharmony_ci
10548c2ecf20Sopenharmony_cistatic long cx18_default(struct file *file, void *fh, bool valid_prio,
10558c2ecf20Sopenharmony_ci			 unsigned int cmd, void *arg)
10568c2ecf20Sopenharmony_ci{
10578c2ecf20Sopenharmony_ci	struct cx18 *cx = fh2id(fh)->cx;
10588c2ecf20Sopenharmony_ci
10598c2ecf20Sopenharmony_ci	switch (cmd) {
10608c2ecf20Sopenharmony_ci	case VIDIOC_INT_RESET: {
10618c2ecf20Sopenharmony_ci		u32 val = *(u32 *)arg;
10628c2ecf20Sopenharmony_ci
10638c2ecf20Sopenharmony_ci		if ((val == 0) || (val & 0x01))
10648c2ecf20Sopenharmony_ci			cx18_call_hw(cx, CX18_HW_GPIO_RESET_CTRL, core, reset,
10658c2ecf20Sopenharmony_ci				     (u32) CX18_GPIO_RESET_Z8F0811);
10668c2ecf20Sopenharmony_ci		break;
10678c2ecf20Sopenharmony_ci	}
10688c2ecf20Sopenharmony_ci
10698c2ecf20Sopenharmony_ci	default:
10708c2ecf20Sopenharmony_ci		return -ENOTTY;
10718c2ecf20Sopenharmony_ci	}
10728c2ecf20Sopenharmony_ci	return 0;
10738c2ecf20Sopenharmony_ci}
10748c2ecf20Sopenharmony_ci
10758c2ecf20Sopenharmony_cistatic const struct v4l2_ioctl_ops cx18_ioctl_ops = {
10768c2ecf20Sopenharmony_ci	.vidioc_querycap                = cx18_querycap,
10778c2ecf20Sopenharmony_ci	.vidioc_s_audio                 = cx18_s_audio,
10788c2ecf20Sopenharmony_ci	.vidioc_g_audio                 = cx18_g_audio,
10798c2ecf20Sopenharmony_ci	.vidioc_enumaudio               = cx18_enumaudio,
10808c2ecf20Sopenharmony_ci	.vidioc_enum_input              = cx18_enum_input,
10818c2ecf20Sopenharmony_ci	.vidioc_g_pixelaspect           = cx18_g_pixelaspect,
10828c2ecf20Sopenharmony_ci	.vidioc_g_selection             = cx18_g_selection,
10838c2ecf20Sopenharmony_ci	.vidioc_g_input                 = cx18_g_input,
10848c2ecf20Sopenharmony_ci	.vidioc_s_input                 = cx18_s_input,
10858c2ecf20Sopenharmony_ci	.vidioc_g_frequency             = cx18_g_frequency,
10868c2ecf20Sopenharmony_ci	.vidioc_s_frequency             = cx18_s_frequency,
10878c2ecf20Sopenharmony_ci	.vidioc_s_tuner                 = cx18_s_tuner,
10888c2ecf20Sopenharmony_ci	.vidioc_g_tuner                 = cx18_g_tuner,
10898c2ecf20Sopenharmony_ci	.vidioc_g_enc_index             = cx18_g_enc_index,
10908c2ecf20Sopenharmony_ci	.vidioc_g_std                   = cx18_g_std,
10918c2ecf20Sopenharmony_ci	.vidioc_s_std                   = cx18_s_std,
10928c2ecf20Sopenharmony_ci	.vidioc_log_status              = cx18_log_status,
10938c2ecf20Sopenharmony_ci	.vidioc_enum_fmt_vid_cap        = cx18_enum_fmt_vid_cap,
10948c2ecf20Sopenharmony_ci	.vidioc_encoder_cmd             = cx18_encoder_cmd,
10958c2ecf20Sopenharmony_ci	.vidioc_try_encoder_cmd         = cx18_try_encoder_cmd,
10968c2ecf20Sopenharmony_ci	.vidioc_g_fmt_vid_cap           = cx18_g_fmt_vid_cap,
10978c2ecf20Sopenharmony_ci	.vidioc_g_fmt_vbi_cap           = cx18_g_fmt_vbi_cap,
10988c2ecf20Sopenharmony_ci	.vidioc_g_fmt_sliced_vbi_cap    = cx18_g_fmt_sliced_vbi_cap,
10998c2ecf20Sopenharmony_ci	.vidioc_s_fmt_vid_cap           = cx18_s_fmt_vid_cap,
11008c2ecf20Sopenharmony_ci	.vidioc_s_fmt_vbi_cap           = cx18_s_fmt_vbi_cap,
11018c2ecf20Sopenharmony_ci	.vidioc_s_fmt_sliced_vbi_cap    = cx18_s_fmt_sliced_vbi_cap,
11028c2ecf20Sopenharmony_ci	.vidioc_try_fmt_vid_cap         = cx18_try_fmt_vid_cap,
11038c2ecf20Sopenharmony_ci	.vidioc_try_fmt_vbi_cap         = cx18_try_fmt_vbi_cap,
11048c2ecf20Sopenharmony_ci	.vidioc_try_fmt_sliced_vbi_cap  = cx18_try_fmt_sliced_vbi_cap,
11058c2ecf20Sopenharmony_ci	.vidioc_g_sliced_vbi_cap        = cx18_g_sliced_vbi_cap,
11068c2ecf20Sopenharmony_ci#ifdef CONFIG_VIDEO_ADV_DEBUG
11078c2ecf20Sopenharmony_ci	.vidioc_g_register              = cx18_g_register,
11088c2ecf20Sopenharmony_ci	.vidioc_s_register              = cx18_s_register,
11098c2ecf20Sopenharmony_ci#endif
11108c2ecf20Sopenharmony_ci	.vidioc_default                 = cx18_default,
11118c2ecf20Sopenharmony_ci	.vidioc_streamon                = cx18_streamon,
11128c2ecf20Sopenharmony_ci	.vidioc_streamoff               = cx18_streamoff,
11138c2ecf20Sopenharmony_ci	.vidioc_reqbufs                 = cx18_reqbufs,
11148c2ecf20Sopenharmony_ci	.vidioc_querybuf                = cx18_querybuf,
11158c2ecf20Sopenharmony_ci	.vidioc_qbuf                    = cx18_qbuf,
11168c2ecf20Sopenharmony_ci	.vidioc_dqbuf                   = cx18_dqbuf,
11178c2ecf20Sopenharmony_ci	.vidioc_subscribe_event		= v4l2_ctrl_subscribe_event,
11188c2ecf20Sopenharmony_ci	.vidioc_unsubscribe_event	= v4l2_event_unsubscribe,
11198c2ecf20Sopenharmony_ci};
11208c2ecf20Sopenharmony_ci
11218c2ecf20Sopenharmony_civoid cx18_set_funcs(struct video_device *vdev)
11228c2ecf20Sopenharmony_ci{
11238c2ecf20Sopenharmony_ci	vdev->ioctl_ops = &cx18_ioctl_ops;
11248c2ecf20Sopenharmony_ci}
1125