File Index
Files and directories
Sign in
FILE

cx23885-video.c

File preview with syntax highlighting.

linux-6.19.14/drivers/media/pci/cx23885/cx23885-video.c C
   1// SPDX-License-Identifier: GPL-2.0-or-later
   2/*
   3 *  Driver for the Conexant CX23885 PCIe bridge
   4 *
   5 *  Copyright (c) 2007 Steven Toth <stoth@linuxtv.org>
   6 */
   7
   8#include "cx23885.h"
   9#include "cx23885-video.h"
  10
  11#include <linux/init.h>
  12#include <linux/list.h>
  13#include <linux/module.h>
  14#include <linux/moduleparam.h>
  15#include <linux/kmod.h>
  16#include <linux/kernel.h>
  17#include <linux/slab.h>
  18#include <linux/interrupt.h>
  19#include <linux/delay.h>
  20#include <linux/kthread.h>
  21#include <asm/div64.h>
  22
  23#include <media/v4l2-common.h>
  24#include <media/v4l2-ioctl.h>
  25#include <media/v4l2-event.h>
  26#include "cx23885-ioctl.h"
  27#include "xc2028.h"
  28
  29#include <media/drv-intf/cx25840.h>
  30
  31MODULE_DESCRIPTION("v4l2 driver module for cx23885 based TV cards");
  32MODULE_AUTHOR("Steven Toth <stoth@linuxtv.org>");
  33MODULE_LICENSE("GPL");
  34
  35/* ------------------------------------------------------------------ */
  36
  37static unsigned int video_nr[] = {[0 ... (CX23885_MAXBOARDS - 1)] = UNSET };
  38static unsigned int vbi_nr[]   = {[0 ... (CX23885_MAXBOARDS - 1)] = UNSET };
  39
  40module_param_array(video_nr, int, NULL, 0444);
  41module_param_array(vbi_nr,   int, NULL, 0444);
  42
  43MODULE_PARM_DESC(video_nr, "video device numbers");
  44MODULE_PARM_DESC(vbi_nr, "vbi device numbers");
  45
  46static unsigned int video_debug;
  47module_param(video_debug, int, 0644);
  48MODULE_PARM_DESC(video_debug, "enable debug messages [video]");
  49
  50static unsigned int irq_debug;
  51module_param(irq_debug, int, 0644);
  52MODULE_PARM_DESC(irq_debug, "enable debug messages [IRQ handler]");
  53
  54static unsigned int vid_limit = 16;
  55module_param(vid_limit, int, 0644);
  56MODULE_PARM_DESC(vid_limit, "capture memory limit in megabytes");
  57
  58#define dprintk(level, fmt, arg...)\
  59	do { if (video_debug >= level)\
  60		printk(KERN_DEBUG pr_fmt("%s: video:" fmt), \
  61			__func__, ##arg); \
  62	} while (0)
  63
  64/* ------------------------------------------------------------------- */
  65/* static data                                                         */
  66
  67#define FORMAT_FLAGS_PACKED       0x01
  68static struct cx23885_fmt formats[] = {
  69	{
  70		.fourcc   = V4L2_PIX_FMT_YUYV,
  71		.depth    = 16,
  72		.flags    = FORMAT_FLAGS_PACKED,
  73	}
  74};
  75
  76static struct cx23885_fmt *format_by_fourcc(unsigned int fourcc)
  77{
  78	unsigned int i;
  79
  80	for (i = 0; i < ARRAY_SIZE(formats); i++)
  81		if (formats[i].fourcc == fourcc)
  82			return formats+i;
  83	return NULL;
  84}
  85
  86/* ------------------------------------------------------------------- */
  87
  88void cx23885_video_wakeup(struct cx23885_dev *dev,
  89	struct cx23885_dmaqueue *q, u32 count)
  90{
  91	struct cx23885_buffer *buf;
  92
  93	if (list_empty(&q->active))
  94		return;
  95	buf = list_entry(q->active.next,
  96			struct cx23885_buffer, queue);
  97
  98	buf->vb.sequence = q->count++;
  99	buf->vb.vb2_buf.timestamp = ktime_get_ns();
 100	dprintk(2, "[%p/%d] wakeup reg=%d buf=%d\n", buf,
 101			buf->vb.vb2_buf.index, count, q->count);
 102	list_del(&buf->queue);
 103	vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_DONE);
 104}
 105
 106int cx23885_set_tvnorm(struct cx23885_dev *dev, v4l2_std_id norm)
 107{
 108	struct v4l2_subdev_format format = {
 109		.which = V4L2_SUBDEV_FORMAT_ACTIVE,
 110		.format.code = MEDIA_BUS_FMT_FIXED,
 111	};
 112
 113	dprintk(1, "%s(norm = 0x%08x) name: [%s]\n",
 114		__func__,
 115		(unsigned int)norm,
 116		v4l2_norm_to_name(norm));
 117
 118	if (dev->tvnorm == norm)
 119		return 0;
 120
 121	if (dev->tvnorm != norm) {
 122		if (vb2_is_busy(&dev->vb2_vidq) || vb2_is_busy(&dev->vb2_vbiq) ||
 123		    vb2_is_busy(&dev->vb2_mpegq))
 124			return -EBUSY;
 125	}
 126
 127	dev->tvnorm = norm;
 128	dev->width = 720;
 129	dev->height = norm_maxh(norm);
 130	dev->field = V4L2_FIELD_INTERLACED;
 131
 132	call_all(dev, video, s_std, norm);
 133
 134	format.format.width = dev->width;
 135	format.format.height = dev->height;
 136	format.format.field = dev->field;
 137	call_all(dev, pad, set_fmt, NULL, &format);
 138
 139	return 0;
 140}
 141
 142static struct video_device *cx23885_vdev_init(struct cx23885_dev *dev,
 143				    struct pci_dev *pci,
 144				    struct video_device *template,
 145				    char *type)
 146{
 147	struct video_device *vfd;
 148	dprintk(1, "%s()\n", __func__);
 149
 150	vfd = video_device_alloc();
 151	if (NULL == vfd)
 152		return NULL;
 153	*vfd = *template;
 154	vfd->v4l2_dev = &dev->v4l2_dev;
 155	vfd->release = video_device_release;
 156	vfd->lock = &dev->lock;
 157	snprintf(vfd->name, sizeof(vfd->name), "%s (%s)",
 158		 cx23885_boards[dev->board].name, type);
 159	video_set_drvdata(vfd, dev);
 160	return vfd;
 161}
 162
 163int cx23885_flatiron_write(struct cx23885_dev *dev, u8 reg, u8 data)
 164{
 165	/* 8 bit registers, 8 bit values */
 166	u8 buf[] = { reg, data };
 167
 168	struct i2c_msg msg = { .addr = 0x98 >> 1,
 169		.flags = 0, .buf = buf, .len = 2 };
 170
 171	return i2c_transfer(&dev->i2c_bus[2].i2c_adap, &msg, 1);
 172}
 173
 174u8 cx23885_flatiron_read(struct cx23885_dev *dev, u8 reg)
 175{
 176	/* 8 bit registers, 8 bit values */
 177	int ret;
 178	u8 b0[] = { reg };
 179	u8 b1[] = { 0 };
 180
 181	struct i2c_msg msg[] = {
 182		{ .addr = 0x98 >> 1, .flags = 0, .buf = b0, .len = 1 },
 183		{ .addr = 0x98 >> 1, .flags = I2C_M_RD, .buf = b1, .len = 1 }
 184	};
 185
 186	ret = i2c_transfer(&dev->i2c_bus[2].i2c_adap, &msg[0], 2);
 187	if (ret != 2)
 188		pr_err("%s() error\n", __func__);
 189
 190	return b1[0];
 191}
 192
 193static void cx23885_flatiron_dump(struct cx23885_dev *dev)
 194{
 195	int i;
 196	dprintk(1, "Flatiron dump\n");
 197	for (i = 0; i < 0x24; i++) {
 198		dprintk(1, "FI[%02x] = %02x\n", i,
 199			cx23885_flatiron_read(dev, i));
 200	}
 201}
 202
 203static int cx23885_flatiron_mux(struct cx23885_dev *dev, int input)
 204{
 205	u8 val;
 206	dprintk(1, "%s(input = %d)\n", __func__, input);
 207
 208	if (input == 1)
 209		val = cx23885_flatiron_read(dev, CH_PWR_CTRL1) & ~FLD_CH_SEL;
 210	else if (input == 2)
 211		val = cx23885_flatiron_read(dev, CH_PWR_CTRL1) | FLD_CH_SEL;
 212	else
 213		return -EINVAL;
 214
 215	val |= 0x20; /* Enable clock to delta-sigma and dec filter */
 216
 217	cx23885_flatiron_write(dev, CH_PWR_CTRL1, val);
 218
 219	/* Wake up */
 220	cx23885_flatiron_write(dev, CH_PWR_CTRL2, 0);
 221
 222	if (video_debug)
 223		cx23885_flatiron_dump(dev);
 224
 225	return 0;
 226}
 227
 228static int cx23885_video_mux(struct cx23885_dev *dev, unsigned int input)
 229{
 230	dprintk(1, "%s() video_mux: %d [vmux=%d, gpio=0x%x,0x%x,0x%x,0x%x]\n",
 231		__func__,
 232		input, INPUT(input)->vmux,
 233		INPUT(input)->gpio0, INPUT(input)->gpio1,
 234		INPUT(input)->gpio2, INPUT(input)->gpio3);
 235	dev->input = input;
 236
 237	if (dev->board == CX23885_BOARD_MYGICA_X8506 ||
 238		dev->board == CX23885_BOARD_MAGICPRO_PROHDTVE2 ||
 239		dev->board == CX23885_BOARD_MYGICA_X8507) {
 240		/* Select Analog TV */
 241		if (INPUT(input)->type == CX23885_VMUX_TELEVISION)
 242			cx23885_gpio_clear(dev, GPIO_0);
 243	}
 244
 245	/* Tell the internal A/V decoder */
 246	v4l2_subdev_call(dev->sd_cx25840, video, s_routing,
 247			INPUT(input)->vmux, 0, 0);
 248
 249	if ((dev->board == CX23885_BOARD_HAUPPAUGE_HVR1800) ||
 250		(dev->board == CX23885_BOARD_MPX885) ||
 251		(dev->board == CX23885_BOARD_HAUPPAUGE_HVR1250) ||
 252		(dev->board == CX23885_BOARD_HAUPPAUGE_IMPACTVCBE) ||
 253		(dev->board == CX23885_BOARD_HAUPPAUGE_HVR1255) ||
 254		(dev->board == CX23885_BOARD_HAUPPAUGE_HVR1255_22111) ||
 255		(dev->board == CX23885_BOARD_HAUPPAUGE_HVR1265_K4) ||
 256		(dev->board == CX23885_BOARD_HAUPPAUGE_QUADHD_ATSC) ||
 257		(dev->board == CX23885_BOARD_HAUPPAUGE_QUADHD_DVB) ||
 258		(dev->board == CX23885_BOARD_HAUPPAUGE_HVR1850) ||
 259		(dev->board == CX23885_BOARD_HAUPPAUGE_HVR5525) ||
 260		(dev->board == CX23885_BOARD_MYGICA_X8507) ||
 261		(dev->board == CX23885_BOARD_AVERMEDIA_HC81R) ||
 262		(dev->board == CX23885_BOARD_VIEWCAST_260E) ||
 263		(dev->board == CX23885_BOARD_VIEWCAST_460E) ||
 264		(dev->board == CX23885_BOARD_AVERMEDIA_CE310B) ||
 265		(dev->board == CX23885_BOARD_AVERMEDIA_H789C)) {
 266		/* Configure audio routing */
 267		v4l2_subdev_call(dev->sd_cx25840, audio, s_routing,
 268			INPUT(input)->amux, 0, 0);
 269
 270		if (INPUT(input)->amux == CX25840_AUDIO7)
 271			cx23885_flatiron_mux(dev, 1);
 272		else if (INPUT(input)->amux == CX25840_AUDIO6)
 273			cx23885_flatiron_mux(dev, 2);
 274	}
 275
 276	return 0;
 277}
 278
 279static int cx23885_audio_mux(struct cx23885_dev *dev, unsigned int input)
 280{
 281	dprintk(1, "%s(input=%d)\n", __func__, input);
 282
 283	/* The baseband video core of the cx23885 has two audio inputs.
 284	 * LR1 and LR2. In almost every single case so far only HVR1xxx
 285	 * cards we've only ever supported LR1. Time to support LR2,
 286	 * which is available via the optional white breakout header on
 287	 * the board.
 288	 * We'll use a could of existing enums in the card struct to allow
 289	 * devs to specify which baseband input they need, or just default
 290	 * to what we've always used.
 291	 */
 292	if (INPUT(input)->amux == CX25840_AUDIO7)
 293		cx23885_flatiron_mux(dev, 1);
 294	else if (INPUT(input)->amux == CX25840_AUDIO6)
 295		cx23885_flatiron_mux(dev, 2);
 296	else {
 297		/* Not specifically defined, assume the default. */
 298		cx23885_flatiron_mux(dev, 1);
 299	}
 300
 301	return 0;
 302}
 303
 304/* ------------------------------------------------------------------ */
 305static int cx23885_start_video_dma(struct cx23885_dev *dev,
 306			   struct cx23885_dmaqueue *q,
 307			   struct cx23885_buffer *buf)
 308{
 309	dprintk(1, "%s()\n", __func__);
 310
 311	/* Stop the dma/fifo before we tamper with it's risc programs */
 312	cx_clear(VID_A_DMA_CTL, 0x11);
 313
 314	/* setup fifo + format */
 315	cx23885_sram_channel_setup(dev, &dev->sram_channels[SRAM_CH01],
 316				buf->bpl, buf->risc.dma);
 317
 318	/* reset counter */
 319	cx_write(VID_A_GPCNT_CTL, 3);
 320	q->count = 0;
 321
 322	/* enable irq */
 323	cx23885_irq_add_enable(dev, 0x01);
 324	cx_set(VID_A_INT_MSK, 0x000011);
 325
 326	/* start dma */
 327	cx_set(DEV_CNTRL2, (1<<5));
 328	cx_set(VID_A_DMA_CTL, 0x11); /* FIFO and RISC enable */
 329
 330	return 0;
 331}
 332
 333static int queue_setup(struct vb2_queue *q,
 334			   unsigned int *num_buffers, unsigned int *num_planes,
 335			   unsigned int sizes[], struct device *alloc_devs[])
 336{
 337	struct cx23885_dev *dev = q->drv_priv;
 338
 339	*num_planes = 1;
 340	sizes[0] = (dev->fmt->depth * dev->width * dev->height) >> 3;
 341	return 0;
 342}
 343
 344static int buffer_prepare(struct vb2_buffer *vb)
 345{
 346	int ret;
 347	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
 348	struct cx23885_dev *dev = vb->vb2_queue->drv_priv;
 349	struct cx23885_buffer *buf =
 350		container_of(vbuf, struct cx23885_buffer, vb);
 351	u32 line0_offset, line1_offset;
 352	struct sg_table *sgt = vb2_dma_sg_plane_desc(vb, 0);
 353	int field_tff;
 354
 355	buf->bpl = (dev->width * dev->fmt->depth) >> 3;
 356
 357	if (vb2_plane_size(vb, 0) < dev->height * buf->bpl)
 358		return -EINVAL;
 359	vb2_set_plane_payload(vb, 0, dev->height * buf->bpl);
 360
 361	switch (dev->field) {
 362	case V4L2_FIELD_TOP:
 363		ret = cx23885_risc_buffer(dev->pci, &buf->risc,
 364				sgt->sgl, 0, UNSET,
 365				buf->bpl, 0, dev->height);
 366		break;
 367	case V4L2_FIELD_BOTTOM:
 368		ret = cx23885_risc_buffer(dev->pci, &buf->risc,
 369				sgt->sgl, UNSET, 0,
 370				buf->bpl, 0, dev->height);
 371		break;
 372	case V4L2_FIELD_INTERLACED:
 373		if (dev->tvnorm & V4L2_STD_525_60)
 374			/* NTSC or  */
 375			field_tff = 1;
 376		else
 377			field_tff = 0;
 378
 379		if (cx23885_boards[dev->board].force_bff)
 380			/* PAL / SECAM OR 888 in NTSC MODE */
 381			field_tff = 0;
 382
 383		if (field_tff) {
 384			/* cx25840 transmits NTSC bottom field first */
 385			dprintk(1, "%s() Creating TFF/NTSC risc\n",
 386					__func__);
 387			line0_offset = buf->bpl;
 388			line1_offset = 0;
 389		} else {
 390			/* All other formats are top field first */
 391			dprintk(1, "%s() Creating BFF/PAL/SECAM risc\n",
 392					__func__);
 393			line0_offset = 0;
 394			line1_offset = buf->bpl;
 395		}
 396		ret = cx23885_risc_buffer(dev->pci, &buf->risc,
 397				sgt->sgl, line0_offset,
 398				line1_offset,
 399				buf->bpl, buf->bpl,
 400				dev->height >> 1);
 401		break;
 402	case V4L2_FIELD_SEQ_TB:
 403		ret = cx23885_risc_buffer(dev->pci, &buf->risc,
 404				sgt->sgl,
 405				0, buf->bpl * (dev->height >> 1),
 406				buf->bpl, 0,
 407				dev->height >> 1);
 408		break;
 409	case V4L2_FIELD_SEQ_BT:
 410		ret = cx23885_risc_buffer(dev->pci, &buf->risc,
 411				sgt->sgl,
 412				buf->bpl * (dev->height >> 1), 0,
 413				buf->bpl, 0,
 414				dev->height >> 1);
 415		break;
 416	default:
 417		return -EINVAL; /* should not happen */
 418	}
 419	dprintk(2, "[%p/%d] buffer_init - %dx%d %dbpp 0x%08x - dma=0x%08lx\n",
 420		buf, buf->vb.vb2_buf.index,
 421		dev->width, dev->height, dev->fmt->depth, dev->fmt->fourcc,
 422		(unsigned long)buf->risc.dma);
 423	return ret;
 424}
 425
 426static void buffer_finish(struct vb2_buffer *vb)
 427{
 428	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
 429	struct cx23885_buffer *buf = container_of(vbuf,
 430		struct cx23885_buffer, vb);
 431
 432	cx23885_free_buffer(vb->vb2_queue->drv_priv, buf);
 433}
 434
 435/*
 436 * The risc program for each buffer works as follows: it starts with a simple
 437 * 'JUMP to addr + 12', which is effectively a NOP. Then the code to DMA the
 438 * buffer follows and at the end we have a JUMP back to the start + 12 (skipping
 439 * the initial JUMP).
 440 *
 441 * This is the risc program of the first buffer to be queued if the active list
 442 * is empty and it just keeps DMAing this buffer without generating any
 443 * interrupts.
 444 *
 445 * If a new buffer is added then the initial JUMP in the code for that buffer
 446 * will generate an interrupt which signals that the previous buffer has been
 447 * DMAed successfully and that it can be returned to userspace.
 448 *
 449 * It also sets the final jump of the previous buffer to the start of the new
 450 * buffer, thus chaining the new buffer into the DMA chain. This is a single
 451 * atomic u32 write, so there is no race condition.
 452 *
 453 * The end-result of all this that you only get an interrupt when a buffer
 454 * is ready, so the control flow is very easy.
 455 */
 456static void buffer_queue(struct vb2_buffer *vb)
 457{
 458	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
 459	struct cx23885_dev *dev = vb->vb2_queue->drv_priv;
 460	struct cx23885_buffer   *buf = container_of(vbuf,
 461		struct cx23885_buffer, vb);
 462	struct cx23885_buffer   *prev;
 463	struct cx23885_dmaqueue *q    = &dev->vidq;
 464	unsigned long flags;
 465
 466	/* add jump to start */
 467	buf->risc.cpu[1] = cpu_to_le32(buf->risc.dma + 12);
 468	buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_CNT_INC);
 469	buf->risc.jmp[1] = cpu_to_le32(buf->risc.dma + 12);
 470	buf->risc.jmp[2] = cpu_to_le32(0); /* bits 63-32 */
 471
 472	spin_lock_irqsave(&dev->slock, flags);
 473	if (list_empty(&q->active)) {
 474		list_add_tail(&buf->queue, &q->active);
 475		dprintk(2, "[%p/%d] buffer_queue - first active\n",
 476			buf, buf->vb.vb2_buf.index);
 477	} else {
 478		buf->risc.cpu[0] |= cpu_to_le32(RISC_IRQ1);
 479		prev = list_entry(q->active.prev, struct cx23885_buffer,
 480			queue);
 481		list_add_tail(&buf->queue, &q->active);
 482		prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
 483		dprintk(2, "[%p/%d] buffer_queue - append to active\n",
 484				buf, buf->vb.vb2_buf.index);
 485	}
 486	spin_unlock_irqrestore(&dev->slock, flags);
 487}
 488
 489static int cx23885_start_streaming(struct vb2_queue *q, unsigned int count)
 490{
 491	struct cx23885_dev *dev = q->drv_priv;
 492	struct cx23885_dmaqueue *dmaq = &dev->vidq;
 493	struct cx23885_buffer *buf = list_entry(dmaq->active.next,
 494			struct cx23885_buffer, queue);
 495
 496	cx23885_start_video_dma(dev, dmaq, buf);
 497	return 0;
 498}
 499
 500static void cx23885_stop_streaming(struct vb2_queue *q)
 501{
 502	struct cx23885_dev *dev = q->drv_priv;
 503	struct cx23885_dmaqueue *dmaq = &dev->vidq;
 504	unsigned long flags;
 505
 506	cx_clear(VID_A_DMA_CTL, 0x11);
 507	spin_lock_irqsave(&dev->slock, flags);
 508	while (!list_empty(&dmaq->active)) {
 509		struct cx23885_buffer *buf = list_entry(dmaq->active.next,
 510			struct cx23885_buffer, queue);
 511
 512		list_del(&buf->queue);
 513		vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
 514	}
 515	spin_unlock_irqrestore(&dev->slock, flags);
 516}
 517
 518static const struct vb2_ops cx23885_video_qops = {
 519	.queue_setup    = queue_setup,
 520	.buf_prepare  = buffer_prepare,
 521	.buf_finish = buffer_finish,
 522	.buf_queue    = buffer_queue,
 523	.start_streaming = cx23885_start_streaming,
 524	.stop_streaming = cx23885_stop_streaming,
 525};
 526
 527/* ------------------------------------------------------------------ */
 528/* VIDEO IOCTLS                                                       */
 529
 530static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
 531	struct v4l2_format *f)
 532{
 533	struct cx23885_dev *dev = video_drvdata(file);
 534
 535	f->fmt.pix.width        = dev->width;
 536	f->fmt.pix.height       = dev->height;
 537	f->fmt.pix.field        = dev->field;
 538	f->fmt.pix.pixelformat  = dev->fmt->fourcc;
 539	f->fmt.pix.bytesperline =
 540		(f->fmt.pix.width * dev->fmt->depth) >> 3;
 541	f->fmt.pix.sizeimage =
 542		f->fmt.pix.height * f->fmt.pix.bytesperline;
 543	f->fmt.pix.colorspace   = V4L2_COLORSPACE_SMPTE170M;
 544
 545	return 0;
 546}
 547
 548static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
 549	struct v4l2_format *f)
 550{
 551	struct cx23885_dev *dev = video_drvdata(file);
 552	struct cx23885_fmt *fmt;
 553	enum v4l2_field   field;
 554	unsigned int      maxw, maxh;
 555
 556	fmt = format_by_fourcc(f->fmt.pix.pixelformat);
 557	if (NULL == fmt)
 558		return -EINVAL;
 559
 560	field = f->fmt.pix.field;
 561	maxw  = 720;
 562	maxh  = norm_maxh(dev->tvnorm);
 563
 564	if (V4L2_FIELD_ANY == field) {
 565		field = (f->fmt.pix.height > maxh/2)
 566			? V4L2_FIELD_INTERLACED
 567			: V4L2_FIELD_BOTTOM;
 568	}
 569
 570	switch (field) {
 571	case V4L2_FIELD_TOP:
 572	case V4L2_FIELD_BOTTOM:
 573		maxh = maxh / 2;
 574		break;
 575	case V4L2_FIELD_INTERLACED:
 576	case V4L2_FIELD_SEQ_TB:
 577	case V4L2_FIELD_SEQ_BT:
 578		break;
 579	default:
 580		field = V4L2_FIELD_INTERLACED;
 581		break;
 582	}
 583
 584	f->fmt.pix.field = field;
 585	v4l_bound_align_image(&f->fmt.pix.width, 48, maxw, 2,
 586			      &f->fmt.pix.height, 32, maxh, 0, 0);
 587	f->fmt.pix.bytesperline =
 588		(f->fmt.pix.width * fmt->depth) >> 3;
 589	f->fmt.pix.sizeimage =
 590		f->fmt.pix.height * f->fmt.pix.bytesperline;
 591	f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
 592
 593	return 0;
 594}
 595
 596static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
 597	struct v4l2_format *f)
 598{
 599	struct cx23885_dev *dev = video_drvdata(file);
 600	struct v4l2_subdev_format format = {
 601		.which = V4L2_SUBDEV_FORMAT_ACTIVE,
 602	};
 603	int err;
 604
 605	dprintk(2, "%s()\n", __func__);
 606	err = vidioc_try_fmt_vid_cap(file, priv, f);
 607
 608	if (0 != err)
 609		return err;
 610
 611	if (vb2_is_busy(&dev->vb2_vidq) || vb2_is_busy(&dev->vb2_vbiq) ||
 612	    vb2_is_busy(&dev->vb2_mpegq))
 613		return -EBUSY;
 614
 615	dev->fmt        = format_by_fourcc(f->fmt.pix.pixelformat);
 616	dev->width      = f->fmt.pix.width;
 617	dev->height     = f->fmt.pix.height;
 618	dev->field	= f->fmt.pix.field;
 619	dprintk(2, "%s() width=%d height=%d field=%d\n", __func__,
 620		dev->width, dev->height, dev->field);
 621	v4l2_fill_mbus_format(&format.format, &f->fmt.pix, MEDIA_BUS_FMT_FIXED);
 622	call_all(dev, pad, set_fmt, NULL, &format);
 623	v4l2_fill_pix_format(&f->fmt.pix, &format.format);
 624	/* set_fmt overwrites f->fmt.pix.field, restore it */
 625	f->fmt.pix.field = dev->field;
 626	return 0;
 627}
 628
 629static int vidioc_querycap(struct file *file, void  *priv,
 630	struct v4l2_capability *cap)
 631{
 632	struct cx23885_dev *dev = video_drvdata(file);
 633
 634	strscpy(cap->driver, "cx23885", sizeof(cap->driver));
 635	strscpy(cap->card, cx23885_boards[dev->board].name,
 636		sizeof(cap->card));
 637	sprintf(cap->bus_info, "PCIe:%s", pci_name(dev->pci));
 638	cap->capabilities = V4L2_CAP_READWRITE | V4L2_CAP_STREAMING |
 639			    V4L2_CAP_AUDIO | V4L2_CAP_VBI_CAPTURE |
 640			    V4L2_CAP_VIDEO_CAPTURE |
 641			    V4L2_CAP_DEVICE_CAPS;
 642	switch (dev->board) { /* i2c device tuners */
 643	case CX23885_BOARD_HAUPPAUGE_HVR1265_K4:
 644	case CX23885_BOARD_HAUPPAUGE_HVR5525:
 645	case CX23885_BOARD_HAUPPAUGE_QUADHD_DVB:
 646	case CX23885_BOARD_HAUPPAUGE_QUADHD_ATSC:
 647		cap->capabilities |= V4L2_CAP_TUNER;
 648		break;
 649	default:
 650		if (dev->tuner_type != TUNER_ABSENT)
 651			cap->capabilities |= V4L2_CAP_TUNER;
 652		break;
 653	}
 654	return 0;
 655}
 656
 657static int vidioc_enum_fmt_vid_cap(struct file *file, void  *priv,
 658	struct v4l2_fmtdesc *f)
 659{
 660	if (unlikely(f->index >= ARRAY_SIZE(formats)))
 661		return -EINVAL;
 662
 663	f->pixelformat = formats[f->index].fourcc;
 664
 665	return 0;
 666}
 667
 668static int vidioc_g_pixelaspect(struct file *file, void *priv,
 669				int type, struct v4l2_fract *f)
 670{
 671	struct cx23885_dev *dev = video_drvdata(file);
 672	bool is_50hz = dev->tvnorm & V4L2_STD_625_50;
 673
 674	if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
 675		return -EINVAL;
 676
 677	f->numerator = is_50hz ? 54 : 11;
 678	f->denominator = is_50hz ? 59 : 10;
 679
 680	return 0;
 681}
 682
 683static int vidioc_g_selection(struct file *file, void *fh,
 684			      struct v4l2_selection *sel)
 685{
 686	struct cx23885_dev *dev = video_drvdata(file);
 687
 688	if (sel->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
 689		return -EINVAL;
 690
 691	switch (sel->target) {
 692	case V4L2_SEL_TGT_CROP_BOUNDS:
 693	case V4L2_SEL_TGT_CROP_DEFAULT:
 694		sel->r.top = 0;
 695		sel->r.left = 0;
 696		sel->r.width = 720;
 697		sel->r.height = norm_maxh(dev->tvnorm);
 698		break;
 699	default:
 700		return -EINVAL;
 701	}
 702	return 0;
 703}
 704
 705static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *id)
 706{
 707	struct cx23885_dev *dev = video_drvdata(file);
 708	dprintk(1, "%s()\n", __func__);
 709
 710	*id = dev->tvnorm;
 711	return 0;
 712}
 713
 714static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id tvnorms)
 715{
 716	struct cx23885_dev *dev = video_drvdata(file);
 717	dprintk(1, "%s()\n", __func__);
 718
 719	return cx23885_set_tvnorm(dev, tvnorms);
 720}
 721
 722int cx23885_enum_input(struct cx23885_dev *dev, struct v4l2_input *i)
 723{
 724	static const char *iname[] = {
 725		[CX23885_VMUX_COMPOSITE1] = "Composite1",
 726		[CX23885_VMUX_COMPOSITE2] = "Composite2",
 727		[CX23885_VMUX_COMPOSITE3] = "Composite3",
 728		[CX23885_VMUX_COMPOSITE4] = "Composite4",
 729		[CX23885_VMUX_SVIDEO]     = "S-Video",
 730		[CX23885_VMUX_COMPONENT]  = "Component",
 731		[CX23885_VMUX_TELEVISION] = "Television",
 732		[CX23885_VMUX_CABLE]      = "Cable TV",
 733		[CX23885_VMUX_DVB]        = "DVB",
 734		[CX23885_VMUX_DEBUG]      = "for debug only",
 735	};
 736	unsigned int n;
 737	dprintk(1, "%s()\n", __func__);
 738
 739	n = i->index;
 740	if (n >= MAX_CX23885_INPUT)
 741		return -EINVAL;
 742
 743	if (0 == INPUT(n)->type)
 744		return -EINVAL;
 745
 746	i->index = n;
 747	i->type  = V4L2_INPUT_TYPE_CAMERA;
 748	strscpy(i->name, iname[INPUT(n)->type], sizeof(i->name));
 749	i->std = CX23885_NORMS;
 750	if ((CX23885_VMUX_TELEVISION == INPUT(n)->type) ||
 751		(CX23885_VMUX_CABLE == INPUT(n)->type)) {
 752		i->type = V4L2_INPUT_TYPE_TUNER;
 753		i->audioset = 4;
 754	} else {
 755		/* Two selectable audio inputs for non-tv inputs */
 756		i->audioset = 3;
 757	}
 758
 759	if (dev->input == n) {
 760		/* enum'd input matches our configured input.
 761		 * Ask the video decoder to process the call
 762		 * and give it an oppertunity to update the
 763		 * status field.
 764		 */
 765		call_all(dev, video, g_input_status, &i->status);
 766	}
 767
 768	return 0;
 769}
 770
 771static int vidioc_enum_input(struct file *file, void *priv,
 772				struct v4l2_input *i)
 773{
 774	struct cx23885_dev *dev = video_drvdata(file);
 775	dprintk(1, "%s()\n", __func__);
 776	return cx23885_enum_input(dev, i);
 777}
 778
 779int cx23885_get_input(struct file *file, void *priv, unsigned int *i)
 780{
 781	struct cx23885_dev *dev = video_drvdata(file);
 782
 783	*i = dev->input;
 784	dprintk(1, "%s() returns %d\n", __func__, *i);
 785	return 0;
 786}
 787
 788static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
 789{
 790	return cx23885_get_input(file, priv, i);
 791}
 792
 793int cx23885_set_input(struct file *file, void *priv, unsigned int i)
 794{
 795	struct cx23885_dev *dev = video_drvdata(file);
 796
 797	dprintk(1, "%s(%d)\n", __func__, i);
 798
 799	if (i >= MAX_CX23885_INPUT) {
 800		dprintk(1, "%s() -EINVAL\n", __func__);
 801		return -EINVAL;
 802	}
 803
 804	if (INPUT(i)->type == 0)
 805		return -EINVAL;
 806
 807	cx23885_video_mux(dev, i);
 808
 809	/* By default establish the default audio input for the card also */
 810	/* Caller is free to use VIDIOC_S_AUDIO to override afterwards */
 811	cx23885_audio_mux(dev, i);
 812	return 0;
 813}
 814
 815static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
 816{
 817	return cx23885_set_input(file, priv, i);
 818}
 819
 820static int vidioc_log_status(struct file *file, void *priv)
 821{
 822	struct cx23885_dev *dev = video_drvdata(file);
 823
 824	call_all(dev, core, log_status);
 825	return 0;
 826}
 827
 828static int cx23885_query_audinput(struct file *file, void *priv,
 829	struct v4l2_audio *i)
 830{
 831	static const char *iname[] = {
 832		[0] = "Baseband L/R 1",
 833		[1] = "Baseband L/R 2",
 834		[2] = "TV",
 835	};
 836	unsigned int n;
 837	dprintk(1, "%s()\n", __func__);
 838
 839	n = i->index;
 840	if (n >= 3)
 841		return -EINVAL;
 842
 843	memset(i, 0, sizeof(*i));
 844	i->index = n;
 845	strscpy(i->name, iname[n], sizeof(i->name));
 846	i->capability = V4L2_AUDCAP_STEREO;
 847	return 0;
 848
 849}
 850
 851static int vidioc_enum_audinput(struct file *file, void *priv,
 852				struct v4l2_audio *i)
 853{
 854	return cx23885_query_audinput(file, priv, i);
 855}
 856
 857static int vidioc_g_audinput(struct file *file, void *priv,
 858	struct v4l2_audio *i)
 859{
 860	struct cx23885_dev *dev = video_drvdata(file);
 861
 862	if ((CX23885_VMUX_TELEVISION == INPUT(dev->input)->type) ||
 863		(CX23885_VMUX_CABLE == INPUT(dev->input)->type))
 864		i->index = 2;
 865	else
 866		i->index = dev->audinput;
 867	dprintk(1, "%s(input=%d)\n", __func__, i->index);
 868
 869	return cx23885_query_audinput(file, priv, i);
 870}
 871
 872static int vidioc_s_audinput(struct file *file, void *priv,
 873	const struct v4l2_audio *i)
 874{
 875	struct cx23885_dev *dev = video_drvdata(file);
 876
 877	if ((CX23885_VMUX_TELEVISION == INPUT(dev->input)->type) ||
 878		(CX23885_VMUX_CABLE == INPUT(dev->input)->type)) {
 879		return i->index != 2 ? -EINVAL : 0;
 880	}
 881	if (i->index > 1)
 882		return -EINVAL;
 883
 884	dprintk(1, "%s(%d)\n", __func__, i->index);
 885
 886	dev->audinput = i->index;
 887
 888	/* Skip the audio defaults from the cards struct, caller wants
 889	 * directly touch the audio mux hardware. */
 890	cx23885_flatiron_mux(dev, dev->audinput + 1);
 891	return 0;
 892}
 893
 894static int vidioc_g_tuner(struct file *file, void *priv,
 895				struct v4l2_tuner *t)
 896{
 897	struct cx23885_dev *dev = video_drvdata(file);
 898
 899	switch (dev->board) { /* i2c device tuners */
 900	case CX23885_BOARD_HAUPPAUGE_HVR1265_K4:
 901	case CX23885_BOARD_HAUPPAUGE_HVR5525:
 902	case CX23885_BOARD_HAUPPAUGE_QUADHD_DVB:
 903	case CX23885_BOARD_HAUPPAUGE_QUADHD_ATSC:
 904		break;
 905	default:
 906		if (dev->tuner_type == TUNER_ABSENT)
 907			return -EINVAL;
 908		break;
 909	}
 910	if (0 != t->index)
 911		return -EINVAL;
 912
 913	strscpy(t->name, "Television", sizeof(t->name));
 914
 915	call_all(dev, tuner, g_tuner, t);
 916	return 0;
 917}
 918
 919static int vidioc_s_tuner(struct file *file, void *priv,
 920				const struct v4l2_tuner *t)
 921{
 922	struct cx23885_dev *dev = video_drvdata(file);
 923
 924	switch (dev->board) { /* i2c device tuners */
 925	case CX23885_BOARD_HAUPPAUGE_HVR1265_K4:
 926	case CX23885_BOARD_HAUPPAUGE_HVR5525:
 927	case CX23885_BOARD_HAUPPAUGE_QUADHD_DVB:
 928	case CX23885_BOARD_HAUPPAUGE_QUADHD_ATSC:
 929		break;
 930	default:
 931		if (dev->tuner_type == TUNER_ABSENT)
 932			return -EINVAL;
 933		break;
 934	}
 935	if (0 != t->index)
 936		return -EINVAL;
 937	/* Update the A/V core */
 938	call_all(dev, tuner, s_tuner, t);
 939
 940	return 0;
 941}
 942
 943static int vidioc_g_frequency(struct file *file, void *priv,
 944				struct v4l2_frequency *f)
 945{
 946	struct cx23885_dev *dev = video_drvdata(file);
 947
 948	switch (dev->board) { /* i2c device tuners */
 949	case CX23885_BOARD_HAUPPAUGE_HVR1265_K4:
 950	case CX23885_BOARD_HAUPPAUGE_HVR5525:
 951	case CX23885_BOARD_HAUPPAUGE_QUADHD_DVB:
 952	case CX23885_BOARD_HAUPPAUGE_QUADHD_ATSC:
 953		break;
 954	default:
 955		if (dev->tuner_type == TUNER_ABSENT)
 956			return -EINVAL;
 957		break;
 958	}
 959	f->type = V4L2_TUNER_ANALOG_TV;
 960	f->frequency = dev->freq;
 961
 962	call_all(dev, tuner, g_frequency, f);
 963
 964	return 0;
 965}
 966
 967static int cx23885_set_freq(struct cx23885_dev *dev, const struct v4l2_frequency *f)
 968{
 969	struct v4l2_ctrl *mute;
 970	int old_mute_val = 1;
 971
 972	switch (dev->board) { /* i2c device tuners */
 973	case CX23885_BOARD_HAUPPAUGE_HVR1265_K4:
 974	case CX23885_BOARD_HAUPPAUGE_HVR5525:
 975	case CX23885_BOARD_HAUPPAUGE_QUADHD_DVB:
 976	case CX23885_BOARD_HAUPPAUGE_QUADHD_ATSC:
 977		break;
 978	default:
 979		if (dev->tuner_type == TUNER_ABSENT)
 980			return -EINVAL;
 981		break;
 982	}
 983	if (unlikely(f->tuner != 0))
 984		return -EINVAL;
 985
 986	dev->freq = f->frequency;
 987
 988	/* I need to mute audio here */
 989	mute = v4l2_ctrl_find(&dev->ctrl_handler, V4L2_CID_AUDIO_MUTE);
 990	if (mute) {
 991		old_mute_val = v4l2_ctrl_g_ctrl(mute);
 992		if (!old_mute_val)
 993			v4l2_ctrl_s_ctrl(mute, 1);
 994	}
 995
 996	call_all(dev, tuner, s_frequency, f);
 997
 998	/* When changing channels it is required to reset TVAUDIO */
 999	msleep(100);
1000
1001	/* I need to unmute audio here */
1002	if (old_mute_val == 0)
1003		v4l2_ctrl_s_ctrl(mute, old_mute_val);
1004
1005	return 0;
1006}
1007
1008static int cx23885_set_freq_via_ops(struct cx23885_dev *dev,
1009	const struct v4l2_frequency *f)
1010{
1011	struct v4l2_ctrl *mute;
1012	int old_mute_val = 1;
1013	struct vb2_dvb_frontend *vfe;
1014	struct dvb_frontend *fe;
1015
1016	struct analog_parameters params = {
1017		.mode      = V4L2_TUNER_ANALOG_TV,
1018		.audmode   = V4L2_TUNER_MODE_STEREO,
1019		.std       = dev->tvnorm,
1020		.frequency = f->frequency
1021	};
1022
1023	dev->freq = f->frequency;
1024
1025	/* I need to mute audio here */
1026	mute = v4l2_ctrl_find(&dev->ctrl_handler, V4L2_CID_AUDIO_MUTE);
1027	if (mute) {
1028		old_mute_val = v4l2_ctrl_g_ctrl(mute);
1029		if (!old_mute_val)
1030			v4l2_ctrl_s_ctrl(mute, 1);
1031	}
1032
1033	/* If HVR1850 */
1034	dprintk(1, "%s() frequency=%d tuner=%d std=0x%llx\n", __func__,
1035		params.frequency, f->tuner, params.std);
1036
1037	vfe = vb2_dvb_get_frontend(&dev->ts2.frontends, 1);
1038	if (!vfe) {
1039		return -EINVAL;
1040	}
1041
1042	fe = vfe->dvb.frontend;
1043
1044	if ((dev->board == CX23885_BOARD_HAUPPAUGE_HVR1850) ||
1045	    (dev->board == CX23885_BOARD_HAUPPAUGE_HVR1255) ||
1046	    (dev->board == CX23885_BOARD_HAUPPAUGE_HVR1255_22111) ||
1047	    (dev->board == CX23885_BOARD_HAUPPAUGE_HVR1265_K4) ||
1048	    (dev->board == CX23885_BOARD_HAUPPAUGE_HVR5525) ||
1049	    (dev->board == CX23885_BOARD_HAUPPAUGE_QUADHD_DVB) ||
1050	    (dev->board == CX23885_BOARD_HAUPPAUGE_QUADHD_ATSC))
1051		fe = &dev->ts1.analog_fe;
1052
1053	if (fe && fe->ops.tuner_ops.set_analog_params) {
1054		call_all(dev, video, s_std, dev->tvnorm);
1055		fe->ops.tuner_ops.set_analog_params(fe, &params);
1056	}
1057	else
1058		pr_err("%s() No analog tuner, aborting\n", __func__);
1059
1060	/* When changing channels it is required to reset TVAUDIO */
1061	msleep(100);
1062
1063	/* I need to unmute audio here */
1064	if (old_mute_val == 0)
1065		v4l2_ctrl_s_ctrl(mute, old_mute_val);
1066
1067	return 0;
1068}
1069
1070int cx23885_set_frequency(struct file *file, void *priv,
1071	const struct v4l2_frequency *f)
1072{
1073	struct cx23885_dev *dev = video_drvdata(file);
1074	int ret;
1075
1076	switch (dev->board) {
1077	case CX23885_BOARD_HAUPPAUGE_HVR1255:
1078	case CX23885_BOARD_HAUPPAUGE_HVR1255_22111:
1079	case CX23885_BOARD_HAUPPAUGE_HVR1265_K4:
1080	case CX23885_BOARD_HAUPPAUGE_HVR1850:
1081	case CX23885_BOARD_HAUPPAUGE_HVR5525:
1082	case CX23885_BOARD_HAUPPAUGE_QUADHD_DVB:
1083	case CX23885_BOARD_HAUPPAUGE_QUADHD_ATSC:
1084		ret = cx23885_set_freq_via_ops(dev, f);
1085		break;
1086	default:
1087		ret = cx23885_set_freq(dev, f);
1088	}
1089
1090	return ret;
1091}
1092
1093static int vidioc_s_frequency(struct file *file, void *priv,
1094	const struct v4l2_frequency *f)
1095{
1096	return cx23885_set_frequency(file, priv, f);
1097}
1098
1099/* ----------------------------------------------------------- */
1100
1101int cx23885_video_irq(struct cx23885_dev *dev, u32 status)
1102{
1103	u32 mask, count;
1104	int handled = 0;
1105
1106	mask   = cx_read(VID_A_INT_MSK);
1107	if (0 == (status & mask))
1108		return handled;
1109
1110	cx_write(VID_A_INT_STAT, status);
1111
1112	/* risc op code error, fifo overflow or line sync detection error */
1113	if ((status & VID_BC_MSK_OPC_ERR) ||
1114		(status & VID_BC_MSK_SYNC) ||
1115		(status & VID_BC_MSK_OF)) {
1116
1117		if (status & VID_BC_MSK_OPC_ERR) {
1118			dprintk(7, " (VID_BC_MSK_OPC_ERR 0x%08x)\n",
1119				VID_BC_MSK_OPC_ERR);
1120			pr_warn("%s: video risc op code error\n",
1121				dev->name);
1122			cx23885_sram_channel_dump(dev,
1123				&dev->sram_channels[SRAM_CH01]);
1124		}
1125
1126		if (status & VID_BC_MSK_SYNC)
1127			dprintk(7, " (VID_BC_MSK_SYNC 0x%08x) video lines miss-match\n",
1128				VID_BC_MSK_SYNC);
1129
1130		if (status & VID_BC_MSK_OF)
1131			dprintk(7, " (VID_BC_MSK_OF 0x%08x) fifo overflow\n",
1132				VID_BC_MSK_OF);
1133
1134	}
1135
1136	/* Video */
1137	if (status & VID_BC_MSK_RISCI1) {
1138		spin_lock(&dev->slock);
1139		count = cx_read(VID_A_GPCNT);
1140		cx23885_video_wakeup(dev, &dev->vidq, count);
1141		spin_unlock(&dev->slock);
1142		handled++;
1143	}
1144
1145	/* Allow the VBI framework to process it's payload */
1146	handled += cx23885_vbi_irq(dev, status);
1147
1148	return handled;
1149}
1150
1151/* ----------------------------------------------------------- */
1152/* exported stuff                                              */
1153
1154static const struct v4l2_file_operations video_fops = {
1155	.owner	       = THIS_MODULE,
1156	.open           = v4l2_fh_open,
1157	.release        = vb2_fop_release,
1158	.read           = vb2_fop_read,
1159	.poll		= vb2_fop_poll,
1160	.unlocked_ioctl = video_ioctl2,
1161	.mmap           = vb2_fop_mmap,
1162};
1163
1164static const struct v4l2_ioctl_ops video_ioctl_ops = {
1165	.vidioc_querycap      = vidioc_querycap,
1166	.vidioc_enum_fmt_vid_cap  = vidioc_enum_fmt_vid_cap,
1167	.vidioc_g_fmt_vid_cap     = vidioc_g_fmt_vid_cap,
1168	.vidioc_try_fmt_vid_cap   = vidioc_try_fmt_vid_cap,
1169	.vidioc_s_fmt_vid_cap     = vidioc_s_fmt_vid_cap,
1170	.vidioc_g_fmt_vbi_cap     = cx23885_vbi_fmt,
1171	.vidioc_try_fmt_vbi_cap   = cx23885_vbi_fmt,
1172	.vidioc_s_fmt_vbi_cap     = cx23885_vbi_fmt,
1173	.vidioc_reqbufs       = vb2_ioctl_reqbufs,
1174	.vidioc_prepare_buf   = vb2_ioctl_prepare_buf,
1175	.vidioc_querybuf      = vb2_ioctl_querybuf,
1176	.vidioc_qbuf          = vb2_ioctl_qbuf,
1177	.vidioc_dqbuf         = vb2_ioctl_dqbuf,
1178	.vidioc_streamon      = vb2_ioctl_streamon,
1179	.vidioc_streamoff     = vb2_ioctl_streamoff,
1180	.vidioc_g_pixelaspect = vidioc_g_pixelaspect,
1181	.vidioc_g_selection   = vidioc_g_selection,
1182	.vidioc_s_std         = vidioc_s_std,
1183	.vidioc_g_std         = vidioc_g_std,
1184	.vidioc_enum_input    = vidioc_enum_input,
1185	.vidioc_g_input       = vidioc_g_input,
1186	.vidioc_s_input       = vidioc_s_input,
1187	.vidioc_log_status    = vidioc_log_status,
1188	.vidioc_g_tuner       = vidioc_g_tuner,
1189	.vidioc_s_tuner       = vidioc_s_tuner,
1190	.vidioc_g_frequency   = vidioc_g_frequency,
1191	.vidioc_s_frequency   = vidioc_s_frequency,
1192#ifdef CONFIG_VIDEO_ADV_DEBUG
1193	.vidioc_g_chip_info   = cx23885_g_chip_info,
1194	.vidioc_g_register    = cx23885_g_register,
1195	.vidioc_s_register    = cx23885_s_register,
1196#endif
1197	.vidioc_enumaudio     = vidioc_enum_audinput,
1198	.vidioc_g_audio       = vidioc_g_audinput,
1199	.vidioc_s_audio       = vidioc_s_audinput,
1200	.vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1201	.vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1202};
1203
1204static struct video_device cx23885_vbi_template;
1205static struct video_device cx23885_video_template = {
1206	.name                 = "cx23885-video",
1207	.fops                 = &video_fops,
1208	.ioctl_ops	      = &video_ioctl_ops,
1209	.tvnorms              = CX23885_NORMS,
1210};
1211
1212void cx23885_video_unregister(struct cx23885_dev *dev)
1213{
1214	dprintk(1, "%s()\n", __func__);
1215	cx23885_irq_remove(dev, 0x01);
1216
1217	if (dev->vbi_dev) {
1218		if (video_is_registered(dev->vbi_dev))
1219			video_unregister_device(dev->vbi_dev);
1220		else
1221			video_device_release(dev->vbi_dev);
1222		dev->vbi_dev = NULL;
1223	}
1224	if (dev->video_dev) {
1225		if (video_is_registered(dev->video_dev))
1226			video_unregister_device(dev->video_dev);
1227		else
1228			video_device_release(dev->video_dev);
1229		dev->video_dev = NULL;
1230	}
1231
1232	if (dev->audio_dev)
1233		cx23885_audio_unregister(dev);
1234}
1235
1236int cx23885_video_register(struct cx23885_dev *dev)
1237{
1238	struct vb2_queue *q;
1239	int err;
1240
1241	dprintk(1, "%s()\n", __func__);
1242
1243	/* Initialize VBI template */
1244	cx23885_vbi_template = cx23885_video_template;
1245	strscpy(cx23885_vbi_template.name, "cx23885-vbi",
1246		sizeof(cx23885_vbi_template.name));
1247
1248	dev->tvnorm = V4L2_STD_NTSC_M;
1249	dev->fmt = format_by_fourcc(V4L2_PIX_FMT_YUYV);
1250	dev->field = V4L2_FIELD_INTERLACED;
1251	dev->width = 720;
1252	dev->height = norm_maxh(dev->tvnorm);
1253
1254	/* init video dma queues */
1255	INIT_LIST_HEAD(&dev->vidq.active);
1256
1257	/* init vbi dma queues */
1258	INIT_LIST_HEAD(&dev->vbiq.active);
1259
1260	cx23885_irq_add_enable(dev, 0x01);
1261
1262	if ((TUNER_ABSENT != dev->tuner_type) &&
1263			((dev->tuner_bus == 0) || (dev->tuner_bus == 1))) {
1264		struct v4l2_subdev *sd = NULL;
1265
1266		if (dev->tuner_addr)
1267			sd = v4l2_i2c_new_subdev(&dev->v4l2_dev,
1268				&dev->i2c_bus[dev->tuner_bus].i2c_adap,
1269				"tuner", dev->tuner_addr, NULL);
1270		else
1271			sd = v4l2_i2c_new_subdev(&dev->v4l2_dev,
1272				&dev->i2c_bus[dev->tuner_bus].i2c_adap,
1273				"tuner", 0, v4l2_i2c_tuner_addrs(ADDRS_TV));
1274		if (sd) {
1275			struct tuner_setup tun_setup;
1276
1277			memset(&tun_setup, 0, sizeof(tun_setup));
1278			tun_setup.mode_mask = T_ANALOG_TV;
1279			tun_setup.type = dev->tuner_type;
1280			tun_setup.addr = v4l2_i2c_subdev_addr(sd);
1281			tun_setup.tuner_callback = cx23885_tuner_callback;
1282
1283			v4l2_subdev_call(sd, tuner, s_type_addr, &tun_setup);
1284
1285			if ((dev->board == CX23885_BOARD_LEADTEK_WINFAST_PXTV1200) ||
1286			    (dev->board == CX23885_BOARD_LEADTEK_WINFAST_PXPVR2200)) {
1287				struct xc2028_ctrl ctrl = {
1288					.fname = XC2028_DEFAULT_FIRMWARE,
1289					.max_len = 64
1290				};
1291				struct v4l2_priv_tun_config cfg = {
1292					.tuner = dev->tuner_type,
1293					.priv = &ctrl
1294				};
1295				v4l2_subdev_call(sd, tuner, s_config, &cfg);
1296			}
1297
1298			if (dev->board == CX23885_BOARD_AVERMEDIA_HC81R) {
1299				struct xc2028_ctrl ctrl = {
1300					.fname = "xc3028L-v36.fw",
1301					.max_len = 64
1302				};
1303				struct v4l2_priv_tun_config cfg = {
1304					.tuner = dev->tuner_type,
1305					.priv = &ctrl
1306				};
1307				v4l2_subdev_call(sd, tuner, s_config, &cfg);
1308			}
1309		}
1310	}
1311
1312	/* initial device configuration */
1313	mutex_lock(&dev->lock);
1314	cx23885_set_tvnorm(dev, dev->tvnorm);
1315	cx23885_video_mux(dev, 0);
1316	cx23885_audio_mux(dev, 0);
1317	mutex_unlock(&dev->lock);
1318
1319	q = &dev->vb2_vidq;
1320	q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1321	q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF | VB2_READ;
1322	q->gfp_flags = GFP_DMA32;
1323	q->min_queued_buffers = 2;
1324	q->drv_priv = dev;
1325	q->buf_struct_size = sizeof(struct cx23885_buffer);
1326	q->ops = &cx23885_video_qops;
1327	q->mem_ops = &vb2_dma_sg_memops;
1328	q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1329	q->lock = &dev->lock;
1330	q->dev = &dev->pci->dev;
1331
1332	err = vb2_queue_init(q);
1333	if (err < 0)
1334		goto fail_unreg;
1335
1336	q = &dev->vb2_vbiq;
1337	q->type = V4L2_BUF_TYPE_VBI_CAPTURE;
1338	q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF | VB2_READ;
1339	q->gfp_flags = GFP_DMA32;
1340	q->min_queued_buffers = 2;
1341	q->drv_priv = dev;
1342	q->buf_struct_size = sizeof(struct cx23885_buffer);
1343	q->ops = &cx23885_vbi_qops;
1344	q->mem_ops = &vb2_dma_sg_memops;
1345	q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1346	q->lock = &dev->lock;
1347	q->dev = &dev->pci->dev;
1348
1349	err = vb2_queue_init(q);
1350	if (err < 0)
1351		goto fail_unreg;
1352
1353	/* register Video device */
1354	dev->video_dev = cx23885_vdev_init(dev, dev->pci,
1355		&cx23885_video_template, "video");
1356	if (!dev->video_dev) {
1357		err = -ENOMEM;
1358		goto fail_unreg;
1359	}
1360	dev->video_dev->queue = &dev->vb2_vidq;
1361	dev->video_dev->device_caps = V4L2_CAP_READWRITE | V4L2_CAP_STREAMING |
1362				      V4L2_CAP_AUDIO | V4L2_CAP_VIDEO_CAPTURE;
1363	switch (dev->board) { /* i2c device tuners */
1364	case CX23885_BOARD_HAUPPAUGE_HVR1265_K4:
1365	case CX23885_BOARD_HAUPPAUGE_HVR5525:
1366	case CX23885_BOARD_HAUPPAUGE_QUADHD_DVB:
1367	case CX23885_BOARD_HAUPPAUGE_QUADHD_ATSC:
1368		dev->video_dev->device_caps |= V4L2_CAP_TUNER;
1369		break;
1370	default:
1371		if (dev->tuner_type != TUNER_ABSENT)
1372			dev->video_dev->device_caps |= V4L2_CAP_TUNER;
1373	}
1374
1375	err = video_register_device(dev->video_dev, VFL_TYPE_VIDEO,
1376				    video_nr[dev->nr]);
1377	if (err < 0) {
1378		pr_info("%s: can't register video device\n",
1379			dev->name);
1380		goto fail_unreg;
1381	}
1382	pr_info("%s: registered device %s [v4l2]\n",
1383	       dev->name, video_device_node_name(dev->video_dev));
1384
1385	/* register VBI device */
1386	dev->vbi_dev = cx23885_vdev_init(dev, dev->pci,
1387		&cx23885_vbi_template, "vbi");
1388	if (!dev->vbi_dev) {
1389		err = -ENOMEM;
1390		goto fail_unreg;
1391	}
1392	dev->vbi_dev->queue = &dev->vb2_vbiq;
1393	dev->vbi_dev->device_caps = V4L2_CAP_READWRITE | V4L2_CAP_STREAMING |
1394				    V4L2_CAP_AUDIO | V4L2_CAP_VBI_CAPTURE;
1395	switch (dev->board) { /* i2c device tuners */
1396	case CX23885_BOARD_HAUPPAUGE_HVR1265_K4:
1397	case CX23885_BOARD_HAUPPAUGE_HVR5525:
1398	case CX23885_BOARD_HAUPPAUGE_QUADHD_DVB:
1399	case CX23885_BOARD_HAUPPAUGE_QUADHD_ATSC:
1400		dev->vbi_dev->device_caps |= V4L2_CAP_TUNER;
1401		break;
1402	default:
1403		if (dev->tuner_type != TUNER_ABSENT)
1404			dev->vbi_dev->device_caps |= V4L2_CAP_TUNER;
1405	}
1406	err = video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
1407				    vbi_nr[dev->nr]);
1408	if (err < 0) {
1409		pr_info("%s: can't register vbi device\n",
1410			dev->name);
1411		goto fail_unreg;
1412	}
1413	pr_info("%s: registered device %s\n",
1414	       dev->name, video_device_node_name(dev->vbi_dev));
1415
1416	/* Register ALSA audio device */
1417	dev->audio_dev = cx23885_audio_register(dev);
1418
1419	return 0;
1420
1421fail_unreg:
1422	cx23885_video_unregister(dev);
1423	return err;
1424}
~/ Read-only file preview.