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