File Index
Files and directories
Sign in
FILE

cx18-ioctl.c

File preview with syntax highlighting.

linux-6.19.14/drivers/media/pci/cx18/cx18-ioctl.c C
   1// SPDX-License-Identifier: GPL-2.0-or-later
   2/*
   3 *  cx18 ioctl system call
   4 *
   5 *  Derived from ivtv-ioctl.c
   6 *
   7 *  Copyright (C) 2007  Hans Verkuil <hverkuil@kernel.org>
   8 *  Copyright (C) 2008  Andy Walls <awalls@md.metrocast.net>
   9 */
  10
  11#include "cx18-driver.h"
  12#include "cx18-io.h"
  13#include "cx18-version.h"
  14#include "cx18-mailbox.h"
  15#include "cx18-i2c.h"
  16#include "cx18-queue.h"
  17#include "cx18-fileops.h"
  18#include "cx18-vbi.h"
  19#include "cx18-audio.h"
  20#include "cx18-video.h"
  21#include "cx18-streams.h"
  22#include "cx18-ioctl.h"
  23#include "cx18-gpio.h"
  24#include "cx18-controls.h"
  25#include "cx18-cards.h"
  26#include "cx18-av-core.h"
  27#include <media/tveeprom.h>
  28#include <media/v4l2-event.h>
  29
  30static const struct v4l2_fmtdesc cx18_formats_yuv[] = {
  31	{
  32		.index = 0,
  33		.type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
  34		.pixelformat = V4L2_PIX_FMT_NV12_16L16,
  35	},
  36	{
  37		.index = 1,
  38		.type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
  39		.pixelformat = V4L2_PIX_FMT_UYVY,
  40	},
  41};
  42
  43static const struct v4l2_fmtdesc cx18_formats_mpeg[] = {
  44	{
  45		.index = 0,
  46		.type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
  47		.flags = V4L2_FMT_FLAG_COMPRESSED,
  48		.pixelformat = V4L2_PIX_FMT_MPEG,
  49	},
  50};
  51
  52static int cx18_g_fmt_vid_cap(struct file *file, void *fh,
  53			      struct v4l2_format *fmt)
  54{
  55	struct cx18_open_id *id = file2id(file);
  56	struct cx18 *cx = id->cx;
  57	struct cx18_stream *s = &cx->streams[id->type];
  58	struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
  59
  60	pixfmt->width = cx->cxhdl.width;
  61	pixfmt->height = cx->cxhdl.height;
  62	pixfmt->colorspace = V4L2_COLORSPACE_SMPTE170M;
  63	pixfmt->field = V4L2_FIELD_INTERLACED;
  64	if (id->type == CX18_ENC_STREAM_TYPE_YUV) {
  65		pixfmt->pixelformat = s->pixelformat;
  66		pixfmt->sizeimage = s->vb_bytes_per_frame;
  67		pixfmt->bytesperline = s->vb_bytes_per_line;
  68	} else {
  69		pixfmt->pixelformat = V4L2_PIX_FMT_MPEG;
  70		pixfmt->sizeimage = 128 * 1024;
  71		pixfmt->bytesperline = 0;
  72	}
  73	return 0;
  74}
  75
  76static int cx18_try_fmt_vid_cap(struct file *file, void *fh,
  77				struct v4l2_format *fmt)
  78{
  79	struct cx18_open_id *id = file2id(file);
  80	struct cx18 *cx = id->cx;
  81	struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
  82	int w = pixfmt->width;
  83	int h = pixfmt->height;
  84
  85	w = min(w, 720);
  86	w = max(w, 720 / 16);
  87
  88	h = min(h, cx->is_50hz ? 576 : 480);
  89	h = max(h, (cx->is_50hz ? 576 : 480) / 8);
  90
  91	if (id->type == CX18_ENC_STREAM_TYPE_YUV) {
  92		if (pixfmt->pixelformat != V4L2_PIX_FMT_NV12_16L16 &&
  93		    pixfmt->pixelformat != V4L2_PIX_FMT_UYVY)
  94			pixfmt->pixelformat = V4L2_PIX_FMT_UYVY;
  95		/* YUV height must be a multiple of 32 */
  96		h = round_up(h, 32);
  97		/*
  98		 * HM12 YUV size is (Y=(h*720) + UV=(h*(720/2)))
  99		 * UYUV YUV size is (Y=(h*720) + UV=(h*(720)))
 100		 */
 101		if (pixfmt->pixelformat == V4L2_PIX_FMT_NV12_16L16) {
 102			pixfmt->sizeimage = h * 720 * 3 / 2;
 103			pixfmt->bytesperline = 720; /* First plane */
 104		} else {
 105			pixfmt->sizeimage = h * 720 * 2;
 106			pixfmt->bytesperline = 1440; /* Packed */
 107		}
 108	} else {
 109		pixfmt->pixelformat = V4L2_PIX_FMT_MPEG;
 110		pixfmt->sizeimage = 128 * 1024;
 111		pixfmt->bytesperline = 0;
 112	}
 113
 114	pixfmt->width = w;
 115	pixfmt->height = h;
 116	pixfmt->colorspace = V4L2_COLORSPACE_SMPTE170M;
 117	pixfmt->field = V4L2_FIELD_INTERLACED;
 118	return 0;
 119}
 120
 121static int cx18_s_fmt_vid_cap(struct file *file, void *fh,
 122			      struct v4l2_format *fmt)
 123{
 124	struct cx18_open_id *id = file2id(file);
 125	struct cx18 *cx = id->cx;
 126	struct v4l2_subdev_format format = {
 127		.which = V4L2_SUBDEV_FORMAT_ACTIVE,
 128	};
 129	struct cx18_stream *s = &cx->streams[id->type];
 130	int ret;
 131	int w, h;
 132
 133	ret = cx18_try_fmt_vid_cap(file, fh, fmt);
 134	if (ret)
 135		return ret;
 136	w = fmt->fmt.pix.width;
 137	h = fmt->fmt.pix.height;
 138
 139	if (cx->cxhdl.width == w && cx->cxhdl.height == h &&
 140	    s->pixelformat == fmt->fmt.pix.pixelformat)
 141		return 0;
 142
 143	if (atomic_read(&cx->ana_capturing) > 0)
 144		return -EBUSY;
 145
 146	s->pixelformat = fmt->fmt.pix.pixelformat;
 147	s->vb_bytes_per_frame = fmt->fmt.pix.sizeimage;
 148	s->vb_bytes_per_line = fmt->fmt.pix.bytesperline;
 149
 150	format.format.width = cx->cxhdl.width = w;
 151	format.format.height = cx->cxhdl.height = h;
 152	format.format.code = MEDIA_BUS_FMT_FIXED;
 153	v4l2_subdev_call(cx->sd_av, pad, set_fmt, NULL, &format);
 154	return cx18_g_fmt_vid_cap(file, fh, fmt);
 155}
 156
 157u16 cx18_service2vbi(int type)
 158{
 159	switch (type) {
 160	case V4L2_SLICED_TELETEXT_B:
 161		return CX18_SLICED_TYPE_TELETEXT_B;
 162	case V4L2_SLICED_CAPTION_525:
 163		return CX18_SLICED_TYPE_CAPTION_525;
 164	case V4L2_SLICED_WSS_625:
 165		return CX18_SLICED_TYPE_WSS_625;
 166	case V4L2_SLICED_VPS:
 167		return CX18_SLICED_TYPE_VPS;
 168	default:
 169		return 0;
 170	}
 171}
 172
 173/* Check if VBI services are allowed on the (field, line) for the video std */
 174static int valid_service_line(int field, int line, int is_pal)
 175{
 176	return (is_pal && line >= 6 &&
 177		((field == 0 && line <= 23) || (field == 1 && line <= 22))) ||
 178	       (!is_pal && line >= 10 && line < 22);
 179}
 180
 181/*
 182 * For a (field, line, std) and inbound potential set of services for that line,
 183 * return the first valid service of those passed in the incoming set for that
 184 * line in priority order:
 185 * CC, VPS, or WSS over TELETEXT for well known lines
 186 * TELETEXT, before VPS, before CC, before WSS, for other lines
 187 */
 188static u16 select_service_from_set(int field, int line, u16 set, int is_pal)
 189{
 190	u16 valid_set = (is_pal ? V4L2_SLICED_VBI_625 : V4L2_SLICED_VBI_525);
 191	int i;
 192
 193	set = set & valid_set;
 194	if (set == 0 || !valid_service_line(field, line, is_pal))
 195		return 0;
 196	if (!is_pal) {
 197		if (line == 21 && (set & V4L2_SLICED_CAPTION_525))
 198			return V4L2_SLICED_CAPTION_525;
 199	} else {
 200		if (line == 16 && field == 0 && (set & V4L2_SLICED_VPS))
 201			return V4L2_SLICED_VPS;
 202		if (line == 23 && field == 0 && (set & V4L2_SLICED_WSS_625))
 203			return V4L2_SLICED_WSS_625;
 204		if (line == 23)
 205			return 0;
 206	}
 207	for (i = 0; i < 32; i++) {
 208		if (BIT(i) & set)
 209			return 1 << i;
 210	}
 211	return 0;
 212}
 213
 214/*
 215 * Expand the service_set of *fmt into valid service_lines for the std,
 216 * and clear the passed in fmt->service_set
 217 */
 218void cx18_expand_service_set(struct v4l2_sliced_vbi_format *fmt, int is_pal)
 219{
 220	u16 set = fmt->service_set;
 221	int f, l;
 222
 223	fmt->service_set = 0;
 224	for (f = 0; f < 2; f++) {
 225		for (l = 0; l < 24; l++)
 226			fmt->service_lines[f][l] = select_service_from_set(f, l, set, is_pal);
 227	}
 228}
 229
 230/*
 231 * Sanitize the service_lines in *fmt per the video std, and return 1
 232 * if any service_line is left as valid after santization
 233 */
 234static int check_service_set(struct v4l2_sliced_vbi_format *fmt, int is_pal)
 235{
 236	int f, l;
 237	u16 set = 0;
 238
 239	for (f = 0; f < 2; f++) {
 240		for (l = 0; l < 24; l++) {
 241			fmt->service_lines[f][l] = select_service_from_set(f, l, fmt->service_lines[f][l], is_pal);
 242			set |= fmt->service_lines[f][l];
 243		}
 244	}
 245	return set != 0;
 246}
 247
 248/* Compute the service_set from the assumed valid service_lines of *fmt */
 249u16 cx18_get_service_set(struct v4l2_sliced_vbi_format *fmt)
 250{
 251	int f, l;
 252	u16 set = 0;
 253
 254	for (f = 0; f < 2; f++) {
 255		for (l = 0; l < 24; l++)
 256			set |= fmt->service_lines[f][l];
 257	}
 258	return set;
 259}
 260
 261static int cx18_g_fmt_vbi_cap(struct file *file, void *fh,
 262				struct v4l2_format *fmt)
 263{
 264	struct cx18 *cx = file2id(file)->cx;
 265	struct v4l2_vbi_format *vbifmt = &fmt->fmt.vbi;
 266
 267	vbifmt->sampling_rate = 27000000;
 268	vbifmt->offset = 248; /* FIXME - slightly wrong for both 50 & 60 Hz */
 269	vbifmt->samples_per_line = VBI_ACTIVE_SAMPLES - 4;
 270	vbifmt->sample_format = V4L2_PIX_FMT_GREY;
 271	vbifmt->start[0] = cx->vbi.start[0];
 272	vbifmt->start[1] = cx->vbi.start[1];
 273	vbifmt->count[0] = vbifmt->count[1] = cx->vbi.count;
 274	vbifmt->flags = 0;
 275	vbifmt->reserved[0] = 0;
 276	vbifmt->reserved[1] = 0;
 277	return 0;
 278}
 279
 280static int cx18_g_fmt_sliced_vbi_cap(struct file *file, void *fh,
 281					struct v4l2_format *fmt)
 282{
 283	struct cx18 *cx = file2id(file)->cx;
 284	struct v4l2_sliced_vbi_format *vbifmt = &fmt->fmt.sliced;
 285
 286	/* sane, V4L2 spec compliant, defaults */
 287	vbifmt->reserved[0] = 0;
 288	vbifmt->reserved[1] = 0;
 289	vbifmt->io_size = sizeof(struct v4l2_sliced_vbi_data) * 36;
 290	memset(vbifmt->service_lines, 0, sizeof(vbifmt->service_lines));
 291	vbifmt->service_set = 0;
 292
 293	/*
 294	 * Fetch the configured service_lines and total service_set from the
 295	 * digitizer/slicer.  Note, cx18_av_vbi() wipes the passed in
 296	 * fmt->fmt.sliced under valid calling conditions
 297	 */
 298	if (v4l2_subdev_call(cx->sd_av, vbi, g_sliced_fmt, &fmt->fmt.sliced))
 299		return -EINVAL;
 300
 301	vbifmt->service_set = cx18_get_service_set(vbifmt);
 302	return 0;
 303}
 304
 305static int cx18_try_fmt_vbi_cap(struct file *file, void *fh,
 306				struct v4l2_format *fmt)
 307{
 308	return cx18_g_fmt_vbi_cap(file, fh, fmt);
 309}
 310
 311static int cx18_try_fmt_sliced_vbi_cap(struct file *file, void *fh,
 312					struct v4l2_format *fmt)
 313{
 314	struct cx18 *cx = file2id(file)->cx;
 315	struct v4l2_sliced_vbi_format *vbifmt = &fmt->fmt.sliced;
 316
 317	vbifmt->io_size = sizeof(struct v4l2_sliced_vbi_data) * 36;
 318	vbifmt->reserved[0] = 0;
 319	vbifmt->reserved[1] = 0;
 320
 321	/* If given a service set, expand it validly & clear passed in set */
 322	if (vbifmt->service_set)
 323		cx18_expand_service_set(vbifmt, cx->is_50hz);
 324	/* Sanitize the service_lines, and compute the new set if any valid */
 325	if (check_service_set(vbifmt, cx->is_50hz))
 326		vbifmt->service_set = cx18_get_service_set(vbifmt);
 327	return 0;
 328}
 329
 330static int cx18_s_fmt_vbi_cap(struct file *file, void *fh,
 331				struct v4l2_format *fmt)
 332{
 333	struct cx18_open_id *id = file2id(file);
 334	struct cx18 *cx = id->cx;
 335	int ret;
 336
 337	/*
 338	 * Changing the Encoder's Raw VBI parameters won't have any effect
 339	 * if any analog capture is ongoing
 340	 */
 341	if (!cx18_raw_vbi(cx) && atomic_read(&cx->ana_capturing) > 0)
 342		return -EBUSY;
 343
 344	/*
 345	 * Set the digitizer registers for raw active VBI.
 346	 * Note cx18_av_vbi_wipes out a lot of the passed in fmt under valid
 347	 * calling conditions
 348	 */
 349	ret = v4l2_subdev_call(cx->sd_av, vbi, s_raw_fmt, &fmt->fmt.vbi);
 350	if (ret)
 351		return ret;
 352
 353	/* Store our new v4l2 (non-)sliced VBI state */
 354	cx->vbi.sliced_in->service_set = 0;
 355	cx->vbi.in.type = V4L2_BUF_TYPE_VBI_CAPTURE;
 356
 357	return cx18_g_fmt_vbi_cap(file, fh, fmt);
 358}
 359
 360static int cx18_s_fmt_sliced_vbi_cap(struct file *file, void *fh,
 361					struct v4l2_format *fmt)
 362{
 363	struct cx18_open_id *id = file2id(file);
 364	struct cx18 *cx = id->cx;
 365	int ret;
 366	struct v4l2_sliced_vbi_format *vbifmt = &fmt->fmt.sliced;
 367
 368	cx18_try_fmt_sliced_vbi_cap(file, fh, fmt);
 369
 370	/*
 371	 * Changing the Encoder's Raw VBI parameters won't have any effect
 372	 * if any analog capture is ongoing
 373	 */
 374	if (cx18_raw_vbi(cx) && atomic_read(&cx->ana_capturing) > 0)
 375		return -EBUSY;
 376
 377	/*
 378	 * Set the service_lines requested in the digitizer/slicer registers.
 379	 * Note, cx18_av_vbi() wipes some "impossible" service lines in the
 380	 * passed in fmt->fmt.sliced under valid calling conditions
 381	 */
 382	ret = v4l2_subdev_call(cx->sd_av, vbi, s_sliced_fmt, &fmt->fmt.sliced);
 383	if (ret)
 384		return ret;
 385	/* Store our current v4l2 sliced VBI settings */
 386	cx->vbi.in.type =  V4L2_BUF_TYPE_SLICED_VBI_CAPTURE;
 387	memcpy(cx->vbi.sliced_in, vbifmt, sizeof(*cx->vbi.sliced_in));
 388	return 0;
 389}
 390
 391#ifdef CONFIG_VIDEO_ADV_DEBUG
 392static int cx18_g_register(struct file *file, void *fh,
 393				struct v4l2_dbg_register *reg)
 394{
 395	struct cx18 *cx = file2id(file)->cx;
 396
 397	if (reg->reg & 0x3)
 398		return -EINVAL;
 399	if (reg->reg >= CX18_MEM_OFFSET + CX18_MEM_SIZE)
 400		return -EINVAL;
 401	reg->size = 4;
 402	reg->val = cx18_read_enc(cx, reg->reg);
 403	return 0;
 404}
 405
 406static int cx18_s_register(struct file *file, void *fh,
 407				const struct v4l2_dbg_register *reg)
 408{
 409	struct cx18 *cx = file2id(file)->cx;
 410
 411	if (reg->reg & 0x3)
 412		return -EINVAL;
 413	if (reg->reg >= CX18_MEM_OFFSET + CX18_MEM_SIZE)
 414		return -EINVAL;
 415	cx18_write_enc(cx, reg->val, reg->reg);
 416	return 0;
 417}
 418#endif
 419
 420static int cx18_querycap(struct file *file, void *fh,
 421				struct v4l2_capability *vcap)
 422{
 423	struct cx18_open_id *id = file2id(file);
 424	struct cx18 *cx = id->cx;
 425
 426	strscpy(vcap->driver, CX18_DRIVER_NAME, sizeof(vcap->driver));
 427	strscpy(vcap->card, cx->card_name, sizeof(vcap->card));
 428	vcap->capabilities = cx->v4l2_cap | V4L2_CAP_DEVICE_CAPS;
 429	return 0;
 430}
 431
 432static int cx18_enumaudio(struct file *file, void *fh, struct v4l2_audio *vin)
 433{
 434	struct cx18 *cx = file2id(file)->cx;
 435
 436	return cx18_get_audio_input(cx, vin->index, vin);
 437}
 438
 439static int cx18_g_audio(struct file *file, void *fh, struct v4l2_audio *vin)
 440{
 441	struct cx18 *cx = file2id(file)->cx;
 442
 443	vin->index = cx->audio_input;
 444	return cx18_get_audio_input(cx, vin->index, vin);
 445}
 446
 447static int cx18_s_audio(struct file *file, void *fh, const struct v4l2_audio *vout)
 448{
 449	struct cx18 *cx = file2id(file)->cx;
 450
 451	if (vout->index >= cx->nof_audio_inputs)
 452		return -EINVAL;
 453	cx->audio_input = vout->index;
 454	cx18_audio_set_io(cx);
 455	return 0;
 456}
 457
 458static int cx18_enum_input(struct file *file, void *fh, struct v4l2_input *vin)
 459{
 460	struct cx18 *cx = file2id(file)->cx;
 461
 462	/* set it to defaults from our table */
 463	return cx18_get_input(cx, vin->index, vin);
 464}
 465
 466static int cx18_g_pixelaspect(struct file *file, void *fh,
 467			      int type, struct v4l2_fract *f)
 468{
 469	struct cx18 *cx = file2id(file)->cx;
 470
 471	if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
 472		return -EINVAL;
 473
 474	f->numerator = cx->is_50hz ? 54 : 11;
 475	f->denominator = cx->is_50hz ? 59 : 10;
 476	return 0;
 477}
 478
 479static int cx18_g_selection(struct file *file, void *fh,
 480			    struct v4l2_selection *sel)
 481{
 482	struct cx18 *cx = file2id(file)->cx;
 483
 484	if (sel->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
 485		return -EINVAL;
 486	switch (sel->target) {
 487	case V4L2_SEL_TGT_CROP_BOUNDS:
 488	case V4L2_SEL_TGT_CROP_DEFAULT:
 489		sel->r.top = sel->r.left = 0;
 490		sel->r.width = 720;
 491		sel->r.height = cx->is_50hz ? 576 : 480;
 492		break;
 493	default:
 494		return -EINVAL;
 495	}
 496	return 0;
 497}
 498
 499static int cx18_enum_fmt_vid_cap(struct file *file, void *fh,
 500					struct v4l2_fmtdesc *fmt)
 501{
 502	struct cx18_open_id *id = file2id(file);
 503
 504	if (id->type == CX18_ENC_STREAM_TYPE_YUV) {
 505		if (fmt->index >= ARRAY_SIZE(cx18_formats_yuv))
 506			return -EINVAL;
 507		*fmt = cx18_formats_yuv[fmt->index];
 508		return 0;
 509	}
 510	if (fmt->index)
 511		return -EINVAL;
 512	*fmt = cx18_formats_mpeg[0];
 513	return 0;
 514}
 515
 516static int cx18_g_input(struct file *file, void *fh, unsigned int *i)
 517{
 518	struct cx18 *cx = file2id(file)->cx;
 519
 520	*i = cx->active_input;
 521	return 0;
 522}
 523
 524int cx18_do_s_input(struct cx18 *cx, unsigned int inp)
 525{
 526	v4l2_std_id std = V4L2_STD_ALL;
 527	const struct cx18_card_video_input *card_input =
 528				cx->card->video_inputs + inp;
 529
 530	if (inp >= cx->nof_inputs)
 531		return -EINVAL;
 532
 533	if (inp == cx->active_input) {
 534		CX18_DEBUG_INFO("Input unchanged\n");
 535		return 0;
 536	}
 537
 538	CX18_DEBUG_INFO("Changing input from %d to %d\n",
 539			cx->active_input, inp);
 540
 541	cx->active_input = inp;
 542	/* Set the audio input to whatever is appropriate for the input type. */
 543	cx->audio_input = cx->card->video_inputs[inp].audio_index;
 544	if (card_input->video_type == V4L2_INPUT_TYPE_TUNER)
 545		std = cx->tuner_std;
 546	cx->streams[CX18_ENC_STREAM_TYPE_MPG].video_dev.tvnorms = std;
 547	cx->streams[CX18_ENC_STREAM_TYPE_YUV].video_dev.tvnorms = std;
 548	cx->streams[CX18_ENC_STREAM_TYPE_VBI].video_dev.tvnorms = std;
 549
 550	/* prevent others from messing with the streams until
 551	   we're finished changing inputs. */
 552	cx18_mute(cx);
 553	cx18_video_set_io(cx);
 554	cx18_audio_set_io(cx);
 555	cx18_unmute(cx);
 556	return 0;
 557}
 558
 559static int cx18_s_input(struct file *file, void *fh, unsigned int inp)
 560{
 561	return cx18_do_s_input(file2id(file)->cx, inp);
 562}
 563
 564static int cx18_g_frequency(struct file *file, void *fh,
 565				struct v4l2_frequency *vf)
 566{
 567	struct cx18 *cx = file2id(file)->cx;
 568
 569	if (vf->tuner != 0)
 570		return -EINVAL;
 571
 572	cx18_call_all(cx, tuner, g_frequency, vf);
 573	return 0;
 574}
 575
 576int cx18_do_s_frequency(struct cx18 *cx, const struct v4l2_frequency *vf)
 577{
 578	if (vf->tuner != 0)
 579		return -EINVAL;
 580
 581	cx18_mute(cx);
 582	CX18_DEBUG_INFO("v4l2 ioctl: set frequency %d\n", vf->frequency);
 583	cx18_call_all(cx, tuner, s_frequency, vf);
 584	cx18_unmute(cx);
 585	return 0;
 586}
 587
 588static int cx18_s_frequency(struct file *file, void *fh,
 589			    const struct v4l2_frequency *vf)
 590{
 591	return cx18_do_s_frequency(file2id(file)->cx, vf);
 592}
 593
 594static int cx18_g_std(struct file *file, void *fh, v4l2_std_id *std)
 595{
 596	struct cx18 *cx = file2id(file)->cx;
 597
 598	*std = cx->std;
 599	return 0;
 600}
 601
 602int cx18_do_s_std(struct cx18 *cx, v4l2_std_id std)
 603{
 604	if ((std & V4L2_STD_ALL) == 0)
 605		return -EINVAL;
 606
 607	if (std == cx->std)
 608		return 0;
 609
 610	if (test_bit(CX18_F_I_RADIO_USER, &cx->i_flags) ||
 611	    atomic_read(&cx->ana_capturing) > 0) {
 612		/* Switching standard would turn off the radio or mess
 613		   with already running streams, prevent that by
 614		   returning EBUSY. */
 615		return -EBUSY;
 616	}
 617
 618	cx->std = std;
 619	cx->is_60hz = (std & V4L2_STD_525_60) ? 1 : 0;
 620	cx->is_50hz = !cx->is_60hz;
 621	cx2341x_handler_set_50hz(&cx->cxhdl, cx->is_50hz);
 622	cx->cxhdl.width = 720;
 623	cx->cxhdl.height = cx->is_50hz ? 576 : 480;
 624	/*
 625	 * HM12 YUV size is (Y=(h*720) + UV=(h*(720/2)))
 626	 * UYUV YUV size is (Y=(h*720) + UV=(h*(720)))
 627	 */
 628	if (cx->streams[CX18_ENC_STREAM_TYPE_YUV].pixelformat == V4L2_PIX_FMT_NV12_16L16) {
 629		cx->streams[CX18_ENC_STREAM_TYPE_YUV].vb_bytes_per_frame =
 630			cx->cxhdl.height * 720 * 3 / 2;
 631		cx->streams[CX18_ENC_STREAM_TYPE_YUV].vb_bytes_per_line = 720;
 632	} else {
 633		cx->streams[CX18_ENC_STREAM_TYPE_YUV].vb_bytes_per_frame =
 634			cx->cxhdl.height * 720 * 2;
 635		cx->streams[CX18_ENC_STREAM_TYPE_YUV].vb_bytes_per_line = 1440;
 636	}
 637	cx->vbi.count = cx->is_50hz ? 18 : 12;
 638	cx->vbi.start[0] = cx->is_50hz ? 6 : 10;
 639	cx->vbi.start[1] = cx->is_50hz ? 318 : 273;
 640	CX18_DEBUG_INFO("Switching standard to %llx.\n",
 641			(unsigned long long) cx->std);
 642
 643	/* Tuner */
 644	cx18_call_all(cx, video, s_std, cx->std);
 645	return 0;
 646}
 647
 648static int cx18_s_std(struct file *file, void *fh, v4l2_std_id std)
 649{
 650	return cx18_do_s_std(file2id(file)->cx, std);
 651}
 652
 653static int cx18_s_tuner(struct file *file, void *fh, const struct v4l2_tuner *vt)
 654{
 655	struct cx18_open_id *id = file2id(file);
 656	struct cx18 *cx = id->cx;
 657
 658	if (vt->index != 0)
 659		return -EINVAL;
 660
 661	cx18_call_all(cx, tuner, s_tuner, vt);
 662	return 0;
 663}
 664
 665static int cx18_g_tuner(struct file *file, void *fh, struct v4l2_tuner *vt)
 666{
 667	struct cx18 *cx = file2id(file)->cx;
 668
 669	if (vt->index != 0)
 670		return -EINVAL;
 671
 672	cx18_call_all(cx, tuner, g_tuner, vt);
 673
 674	if (vt->type == V4L2_TUNER_RADIO)
 675		strscpy(vt->name, "cx18 Radio Tuner", sizeof(vt->name));
 676	else
 677		strscpy(vt->name, "cx18 TV Tuner", sizeof(vt->name));
 678	return 0;
 679}
 680
 681static int cx18_g_sliced_vbi_cap(struct file *file, void *fh,
 682					struct v4l2_sliced_vbi_cap *cap)
 683{
 684	struct cx18 *cx = file2id(file)->cx;
 685	int set = cx->is_50hz ? V4L2_SLICED_VBI_625 : V4L2_SLICED_VBI_525;
 686	int f, l;
 687
 688	if (cap->type != V4L2_BUF_TYPE_SLICED_VBI_CAPTURE)
 689		return -EINVAL;
 690
 691	cap->service_set = 0;
 692	for (f = 0; f < 2; f++) {
 693		for (l = 0; l < 24; l++) {
 694			if (valid_service_line(f, l, cx->is_50hz)) {
 695				/*
 696				 * We can find all v4l2 supported vbi services
 697				 * for the standard, on a valid line for the std
 698				 */
 699				cap->service_lines[f][l] = set;
 700				cap->service_set |= set;
 701			} else
 702				cap->service_lines[f][l] = 0;
 703		}
 704	}
 705	for (f = 0; f < 3; f++)
 706		cap->reserved[f] = 0;
 707	return 0;
 708}
 709
 710static int _cx18_process_idx_data(struct cx18_buffer *buf,
 711				  struct v4l2_enc_idx *idx)
 712{
 713	int consumed, remaining;
 714	struct v4l2_enc_idx_entry *e_idx;
 715	struct cx18_enc_idx_entry *e_buf;
 716
 717	/* Frame type lookup: 1=I, 2=P, 4=B */
 718	static const int mapping[8] = {
 719		-1, V4L2_ENC_IDX_FRAME_I, V4L2_ENC_IDX_FRAME_P,
 720		-1, V4L2_ENC_IDX_FRAME_B, -1, -1, -1
 721	};
 722
 723	/*
 724	 * Assumption here is that a buf holds an integral number of
 725	 * struct cx18_enc_idx_entry objects and is properly aligned.
 726	 * This is enforced by the module options on IDX buffer sizes.
 727	 */
 728	remaining = buf->bytesused - buf->readpos;
 729	consumed = 0;
 730	e_idx = &idx->entry[idx->entries];
 731	e_buf = (struct cx18_enc_idx_entry *) &buf->buf[buf->readpos];
 732
 733	while (remaining >= sizeof(struct cx18_enc_idx_entry) &&
 734	       idx->entries < V4L2_ENC_IDX_ENTRIES) {
 735
 736		e_idx->offset = (((u64) le32_to_cpu(e_buf->offset_high)) << 32)
 737				| le32_to_cpu(e_buf->offset_low);
 738
 739		e_idx->pts = (((u64) (le32_to_cpu(e_buf->pts_high) & 1)) << 32)
 740			     | le32_to_cpu(e_buf->pts_low);
 741
 742		e_idx->length = le32_to_cpu(e_buf->length);
 743
 744		e_idx->flags = mapping[le32_to_cpu(e_buf->flags) & 0x7];
 745
 746		e_idx->reserved[0] = 0;
 747		e_idx->reserved[1] = 0;
 748
 749		idx->entries++;
 750		e_idx = &idx->entry[idx->entries];
 751		e_buf++;
 752
 753		remaining -= sizeof(struct cx18_enc_idx_entry);
 754		consumed += sizeof(struct cx18_enc_idx_entry);
 755	}
 756
 757	/* Swallow any partial entries at the end, if there are any */
 758	if (remaining > 0 && remaining < sizeof(struct cx18_enc_idx_entry))
 759		consumed += remaining;
 760
 761	buf->readpos += consumed;
 762	return consumed;
 763}
 764
 765static int cx18_process_idx_data(struct cx18_stream *s, struct cx18_mdl *mdl,
 766				 struct v4l2_enc_idx *idx)
 767{
 768	if (s->type != CX18_ENC_STREAM_TYPE_IDX)
 769		return -EINVAL;
 770
 771	if (mdl->curr_buf == NULL)
 772		mdl->curr_buf = list_first_entry(&mdl->buf_list,
 773						 struct cx18_buffer, list);
 774
 775	if (list_entry_is_head(mdl->curr_buf, &mdl->buf_list, list)) {
 776		/*
 777		 * For some reason we've exhausted the buffers, but the MDL
 778		 * object still said some data was unread.
 779		 * Fix that and bail out.
 780		 */
 781		mdl->readpos = mdl->bytesused;
 782		return 0;
 783	}
 784
 785	list_for_each_entry_from(mdl->curr_buf, &mdl->buf_list, list) {
 786
 787		/* Skip any empty buffers in the MDL */
 788		if (mdl->curr_buf->readpos >= mdl->curr_buf->bytesused)
 789			continue;
 790
 791		mdl->readpos += _cx18_process_idx_data(mdl->curr_buf, idx);
 792
 793		/* exit when MDL drained or request satisfied */
 794		if (idx->entries >= V4L2_ENC_IDX_ENTRIES ||
 795		    mdl->curr_buf->readpos < mdl->curr_buf->bytesused ||
 796		    mdl->readpos >= mdl->bytesused)
 797			break;
 798	}
 799	return 0;
 800}
 801
 802static int cx18_g_enc_index(struct file *file, void *fh,
 803				struct v4l2_enc_idx *idx)
 804{
 805	struct cx18 *cx = file2id(file)->cx;
 806	struct cx18_stream *s = &cx->streams[CX18_ENC_STREAM_TYPE_IDX];
 807	s32 tmp;
 808	struct cx18_mdl *mdl;
 809
 810	if (!cx18_stream_enabled(s)) /* Module options inhibited IDX stream */
 811		return -EINVAL;
 812
 813	/* Compute the best case number of entries we can buffer */
 814	tmp = s->buffers -
 815			  s->bufs_per_mdl * CX18_ENC_STREAM_TYPE_IDX_FW_MDL_MIN;
 816	if (tmp <= 0)
 817		tmp = 1;
 818	tmp = tmp * s->buf_size / sizeof(struct cx18_enc_idx_entry);
 819
 820	/* Fill out the header of the return structure */
 821	idx->entries = 0;
 822	idx->entries_cap = tmp;
 823	memset(idx->reserved, 0, sizeof(idx->reserved));
 824
 825	/* Pull IDX MDLs and buffers from q_full and populate the entries */
 826	do {
 827		mdl = cx18_dequeue(s, &s->q_full);
 828		if (mdl == NULL) /* No more IDX data right now */
 829			break;
 830
 831		/* Extract the Index entry data from the MDL and buffers */
 832		cx18_process_idx_data(s, mdl, idx);
 833		if (mdl->readpos < mdl->bytesused) {
 834			/* We finished with data remaining, push the MDL back */
 835			cx18_push(s, mdl, &s->q_full);
 836			break;
 837		}
 838
 839		/* We drained this MDL, schedule it to go to the firmware */
 840		cx18_enqueue(s, mdl, &s->q_free);
 841
 842	} while (idx->entries < V4L2_ENC_IDX_ENTRIES);
 843
 844	/* Tell the work handler to send free IDX MDLs to the firmware */
 845	cx18_stream_load_fw_queue(s);
 846	return 0;
 847}
 848
 849static int cx18_encoder_cmd(struct file *file, void *fh,
 850				struct v4l2_encoder_cmd *enc)
 851{
 852	struct cx18_open_id *id = file2id(file);
 853	struct cx18 *cx = id->cx;
 854	u32 h;
 855
 856	switch (enc->cmd) {
 857	case V4L2_ENC_CMD_START:
 858		CX18_DEBUG_IOCTL("V4L2_ENC_CMD_START\n");
 859		enc->flags = 0;
 860		return cx18_start_capture(id);
 861
 862	case V4L2_ENC_CMD_STOP:
 863		CX18_DEBUG_IOCTL("V4L2_ENC_CMD_STOP\n");
 864		enc->flags &= V4L2_ENC_CMD_STOP_AT_GOP_END;
 865		cx18_stop_capture(&cx->streams[id->type],
 866				  enc->flags & V4L2_ENC_CMD_STOP_AT_GOP_END);
 867		break;
 868
 869	case V4L2_ENC_CMD_PAUSE:
 870		CX18_DEBUG_IOCTL("V4L2_ENC_CMD_PAUSE\n");
 871		enc->flags = 0;
 872		if (!atomic_read(&cx->ana_capturing))
 873			return -EPERM;
 874		if (test_and_set_bit(CX18_F_I_ENC_PAUSED, &cx->i_flags))
 875			return 0;
 876		h = cx18_find_handle(cx);
 877		if (h == CX18_INVALID_TASK_HANDLE) {
 878			CX18_ERR("Can't find valid task handle for V4L2_ENC_CMD_PAUSE\n");
 879			return -EBADFD;
 880		}
 881		cx18_mute(cx);
 882		cx18_vapi(cx, CX18_CPU_CAPTURE_PAUSE, 1, h);
 883		break;
 884
 885	case V4L2_ENC_CMD_RESUME:
 886		CX18_DEBUG_IOCTL("V4L2_ENC_CMD_RESUME\n");
 887		enc->flags = 0;
 888		if (!atomic_read(&cx->ana_capturing))
 889			return -EPERM;
 890		if (!test_and_clear_bit(CX18_F_I_ENC_PAUSED, &cx->i_flags))
 891			return 0;
 892		h = cx18_find_handle(cx);
 893		if (h == CX18_INVALID_TASK_HANDLE) {
 894			CX18_ERR("Can't find valid task handle for V4L2_ENC_CMD_RESUME\n");
 895			return -EBADFD;
 896		}
 897		cx18_vapi(cx, CX18_CPU_CAPTURE_RESUME, 1, h);
 898		cx18_unmute(cx);
 899		break;
 900
 901	default:
 902		CX18_DEBUG_IOCTL("Unknown cmd %d\n", enc->cmd);
 903		return -EINVAL;
 904	}
 905	return 0;
 906}
 907
 908static int cx18_try_encoder_cmd(struct file *file, void *fh,
 909				struct v4l2_encoder_cmd *enc)
 910{
 911	struct cx18 *cx = file2id(file)->cx;
 912
 913	switch (enc->cmd) {
 914	case V4L2_ENC_CMD_START:
 915		CX18_DEBUG_IOCTL("V4L2_ENC_CMD_START\n");
 916		enc->flags = 0;
 917		break;
 918
 919	case V4L2_ENC_CMD_STOP:
 920		CX18_DEBUG_IOCTL("V4L2_ENC_CMD_STOP\n");
 921		enc->flags &= V4L2_ENC_CMD_STOP_AT_GOP_END;
 922		break;
 923
 924	case V4L2_ENC_CMD_PAUSE:
 925		CX18_DEBUG_IOCTL("V4L2_ENC_CMD_PAUSE\n");
 926		enc->flags = 0;
 927		break;
 928
 929	case V4L2_ENC_CMD_RESUME:
 930		CX18_DEBUG_IOCTL("V4L2_ENC_CMD_RESUME\n");
 931		enc->flags = 0;
 932		break;
 933
 934	default:
 935		CX18_DEBUG_IOCTL("Unknown cmd %d\n", enc->cmd);
 936		return -EINVAL;
 937	}
 938	return 0;
 939}
 940
 941static int cx18_log_status(struct file *file, void *fh)
 942{
 943	struct cx18 *cx = file2id(file)->cx;
 944	struct v4l2_input vidin;
 945	struct v4l2_audio audin;
 946	int i;
 947
 948	CX18_INFO("Version: %s  Card: %s\n", CX18_VERSION, cx->card_name);
 949	if (cx->hw_flags & CX18_HW_TVEEPROM) {
 950		struct tveeprom tv;
 951
 952		cx18_read_eeprom(cx, &tv);
 953	}
 954	cx18_call_all(cx, core, log_status);
 955	cx18_get_input(cx, cx->active_input, &vidin);
 956	cx18_get_audio_input(cx, cx->audio_input, &audin);
 957	CX18_INFO("Video Input: %s\n", vidin.name);
 958	CX18_INFO("Audio Input: %s\n", audin.name);
 959	mutex_lock(&cx->gpio_lock);
 960	CX18_INFO("GPIO:  direction 0x%08x, value 0x%08x\n",
 961		cx->gpio_dir, cx->gpio_val);
 962	mutex_unlock(&cx->gpio_lock);
 963	CX18_INFO("Tuner: %s\n",
 964		test_bit(CX18_F_I_RADIO_USER, &cx->i_flags) ?  "Radio" : "TV");
 965	v4l2_ctrl_handler_log_status(&cx->cxhdl.hdl, cx->v4l2_dev.name);
 966	CX18_INFO("Status flags: 0x%08lx\n", cx->i_flags);
 967	for (i = 0; i < CX18_MAX_STREAMS; i++) {
 968		struct cx18_stream *s = &cx->streams[i];
 969
 970		if (s->video_dev.v4l2_dev == NULL || s->buffers == 0)
 971			continue;
 972		CX18_INFO("Stream %s: status 0x%04lx, %d%% of %d KiB (%d buffers) in use\n",
 973			  s->name, s->s_flags,
 974			  atomic_read(&s->q_full.depth) * s->bufs_per_mdl * 100
 975			   / s->buffers,
 976			  (s->buffers * s->buf_size) / 1024, s->buffers);
 977	}
 978	CX18_INFO("Read MPEG/VBI: %lld/%lld bytes\n",
 979			(long long)cx->mpg_data_received,
 980			(long long)cx->vbi_data_inserted);
 981	return 0;
 982}
 983
 984static long cx18_default(struct file *file, void *fh, bool valid_prio,
 985			 unsigned int cmd, void *arg)
 986{
 987	struct cx18 *cx = file2id(file)->cx;
 988
 989	switch (cmd) {
 990	case VIDIOC_INT_RESET: {
 991		u32 val = *(u32 *)arg;
 992
 993		if ((val == 0) || (val & 0x01))
 994			cx18_call_hw(cx, CX18_HW_GPIO_RESET_CTRL, core, reset,
 995				     (u32) CX18_GPIO_RESET_Z8F0811);
 996		break;
 997	}
 998
 999	default:
1000		return -ENOTTY;
1001	}
1002	return 0;
1003}
1004
1005static const struct v4l2_ioctl_ops cx18_ioctl_ops = {
1006	.vidioc_querycap                = cx18_querycap,
1007	.vidioc_s_audio                 = cx18_s_audio,
1008	.vidioc_g_audio                 = cx18_g_audio,
1009	.vidioc_enumaudio               = cx18_enumaudio,
1010	.vidioc_enum_input              = cx18_enum_input,
1011	.vidioc_g_pixelaspect           = cx18_g_pixelaspect,
1012	.vidioc_g_selection             = cx18_g_selection,
1013	.vidioc_g_input                 = cx18_g_input,
1014	.vidioc_s_input                 = cx18_s_input,
1015	.vidioc_g_frequency             = cx18_g_frequency,
1016	.vidioc_s_frequency             = cx18_s_frequency,
1017	.vidioc_s_tuner                 = cx18_s_tuner,
1018	.vidioc_g_tuner                 = cx18_g_tuner,
1019	.vidioc_g_enc_index             = cx18_g_enc_index,
1020	.vidioc_g_std                   = cx18_g_std,
1021	.vidioc_s_std                   = cx18_s_std,
1022	.vidioc_log_status              = cx18_log_status,
1023	.vidioc_enum_fmt_vid_cap        = cx18_enum_fmt_vid_cap,
1024	.vidioc_encoder_cmd             = cx18_encoder_cmd,
1025	.vidioc_try_encoder_cmd         = cx18_try_encoder_cmd,
1026	.vidioc_g_fmt_vid_cap           = cx18_g_fmt_vid_cap,
1027	.vidioc_g_fmt_vbi_cap           = cx18_g_fmt_vbi_cap,
1028	.vidioc_g_fmt_sliced_vbi_cap    = cx18_g_fmt_sliced_vbi_cap,
1029	.vidioc_s_fmt_vid_cap           = cx18_s_fmt_vid_cap,
1030	.vidioc_s_fmt_vbi_cap           = cx18_s_fmt_vbi_cap,
1031	.vidioc_s_fmt_sliced_vbi_cap    = cx18_s_fmt_sliced_vbi_cap,
1032	.vidioc_try_fmt_vid_cap         = cx18_try_fmt_vid_cap,
1033	.vidioc_try_fmt_vbi_cap         = cx18_try_fmt_vbi_cap,
1034	.vidioc_try_fmt_sliced_vbi_cap  = cx18_try_fmt_sliced_vbi_cap,
1035	.vidioc_g_sliced_vbi_cap        = cx18_g_sliced_vbi_cap,
1036#ifdef CONFIG_VIDEO_ADV_DEBUG
1037	.vidioc_g_register              = cx18_g_register,
1038	.vidioc_s_register              = cx18_s_register,
1039#endif
1040	.vidioc_default                 = cx18_default,
1041
1042	.vidioc_reqbufs			= vb2_ioctl_reqbufs,
1043	.vidioc_querybuf		= vb2_ioctl_querybuf,
1044	.vidioc_qbuf			= vb2_ioctl_qbuf,
1045	.vidioc_dqbuf			= vb2_ioctl_dqbuf,
1046	.vidioc_create_bufs		= vb2_ioctl_create_bufs,
1047	.vidioc_streamon		= vb2_ioctl_streamon,
1048	.vidioc_streamoff		= vb2_ioctl_streamoff,
1049	.vidioc_prepare_buf		= vb2_ioctl_prepare_buf,
1050	.vidioc_subscribe_event		= v4l2_ctrl_subscribe_event,
1051	.vidioc_unsubscribe_event	= v4l2_event_unsubscribe,
1052};
1053
1054void cx18_set_funcs(struct video_device *vdev)
1055{
1056	vdev->ioctl_ops = &cx18_ioctl_ops;
1057}
~/ Read-only file preview.