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  *  You should have received a copy of the GNU General Public License
18  *  along with this program; if not, write to the Free Software
19  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  */
21 
22 #include <linux/init.h>
23 #include <linux/list.h>
24 #include <linux/module.h>
25 #include <linux/moduleparam.h>
26 #include <linux/kmod.h>
27 #include <linux/kernel.h>
28 #include <linux/slab.h>
29 #include <linux/interrupt.h>
30 #include <linux/delay.h>
31 #include <linux/kthread.h>
32 #include <asm/div64.h>
33 
34 #include "cx23885.h"
35 #include "cx23885-video.h"
36 #include <media/v4l2-common.h>
37 #include <media/v4l2-ioctl.h>
38 #include "cx23885-ioctl.h"
39 #include "tuner-xc2028.h"
40 
41 #include <media/cx25840.h>
42 
43 MODULE_DESCRIPTION("v4l2 driver module for cx23885 based TV cards");
44 MODULE_AUTHOR("Steven Toth <stoth@linuxtv.org>");
45 MODULE_LICENSE("GPL");
46 
47 /* ------------------------------------------------------------------ */
48 
49 static unsigned int video_nr[] = {[0 ... (CX23885_MAXBOARDS - 1)] = UNSET };
50 static unsigned int vbi_nr[]   = {[0 ... (CX23885_MAXBOARDS - 1)] = UNSET };
51 static unsigned int radio_nr[] = {[0 ... (CX23885_MAXBOARDS - 1)] = UNSET };
52 
53 module_param_array(video_nr, int, NULL, 0444);
54 module_param_array(vbi_nr,   int, NULL, 0444);
55 module_param_array(radio_nr, int, NULL, 0444);
56 
57 MODULE_PARM_DESC(video_nr, "video device numbers");
58 MODULE_PARM_DESC(vbi_nr, "vbi device numbers");
59 MODULE_PARM_DESC(radio_nr, "radio device numbers");
60 
61 static unsigned int video_debug;
62 module_param(video_debug, int, 0644);
63 MODULE_PARM_DESC(video_debug, "enable debug messages [video]");
64 
65 static unsigned int irq_debug;
66 module_param(irq_debug, int, 0644);
67 MODULE_PARM_DESC(irq_debug, "enable debug messages [IRQ handler]");
68 
69 static unsigned int vid_limit = 16;
70 module_param(vid_limit, int, 0644);
71 MODULE_PARM_DESC(vid_limit, "capture memory limit in megabytes");
72 
73 #define dprintk(level, fmt, arg...)\
74 	do { if (video_debug >= level)\
75 		printk(KERN_DEBUG "%s: " fmt, dev->name, ## arg);\
76 	} while (0)
77 
78 /* ------------------------------------------------------------------- */
79 /* static data                                                         */
80 
81 #define FORMAT_FLAGS_PACKED       0x01
82 #if 0
83 static struct cx23885_fmt formats[] = {
84 	{
85 		.name     = "8 bpp, gray",
86 		.fourcc   = V4L2_PIX_FMT_GREY,
87 		.depth    = 8,
88 		.flags    = FORMAT_FLAGS_PACKED,
89 	}, {
90 		.name     = "15 bpp RGB, le",
91 		.fourcc   = V4L2_PIX_FMT_RGB555,
92 		.depth    = 16,
93 		.flags    = FORMAT_FLAGS_PACKED,
94 	}, {
95 		.name     = "15 bpp RGB, be",
96 		.fourcc   = V4L2_PIX_FMT_RGB555X,
97 		.depth    = 16,
98 		.flags    = FORMAT_FLAGS_PACKED,
99 	}, {
100 		.name     = "16 bpp RGB, le",
101 		.fourcc   = V4L2_PIX_FMT_RGB565,
102 		.depth    = 16,
103 		.flags    = FORMAT_FLAGS_PACKED,
104 	}, {
105 		.name     = "16 bpp RGB, be",
106 		.fourcc   = V4L2_PIX_FMT_RGB565X,
107 		.depth    = 16,
108 		.flags    = FORMAT_FLAGS_PACKED,
109 	}, {
110 		.name     = "24 bpp RGB, le",
111 		.fourcc   = V4L2_PIX_FMT_BGR24,
112 		.depth    = 24,
113 		.flags    = FORMAT_FLAGS_PACKED,
114 	}, {
115 		.name     = "32 bpp RGB, le",
116 		.fourcc   = V4L2_PIX_FMT_BGR32,
117 		.depth    = 32,
118 		.flags    = FORMAT_FLAGS_PACKED,
119 	}, {
120 		.name     = "32 bpp RGB, be",
121 		.fourcc   = V4L2_PIX_FMT_RGB32,
122 		.depth    = 32,
123 		.flags    = FORMAT_FLAGS_PACKED,
124 	}, {
125 		.name     = "4:2:2, packed, YUYV",
126 		.fourcc   = V4L2_PIX_FMT_YUYV,
127 		.depth    = 16,
128 		.flags    = FORMAT_FLAGS_PACKED,
129 	}, {
130 		.name     = "4:2:2, packed, UYVY",
131 		.fourcc   = V4L2_PIX_FMT_UYVY,
132 		.depth    = 16,
133 		.flags    = FORMAT_FLAGS_PACKED,
134 	},
135 };
136 #else
137 static struct cx23885_fmt formats[] = {
138 	{
139 #if 0
140 		.name     = "4:2:2, packed, UYVY",
141 		.fourcc   = V4L2_PIX_FMT_UYVY,
142 		.depth    = 16,
143 		.flags    = FORMAT_FLAGS_PACKED,
144 	}, {
145 #endif
146 		.name     = "4:2:2, packed, YUYV",
147 		.fourcc   = V4L2_PIX_FMT_YUYV,
148 		.depth    = 16,
149 		.flags    = FORMAT_FLAGS_PACKED,
150 	}
151 };
152 #endif
153 
154 static struct cx23885_fmt *format_by_fourcc(unsigned int fourcc)
155 {
156 	unsigned int i;
157 
158 	for (i = 0; i < ARRAY_SIZE(formats); i++)
159 		if (formats[i].fourcc == fourcc)
160 			return formats+i;
161 
162 	printk(KERN_ERR "%s(%c%c%c%c) NOT FOUND\n", __func__,
163 		(fourcc & 0xff),
164 		((fourcc >> 8) & 0xff),
165 		((fourcc >> 16) & 0xff),
166 		((fourcc >> 24) & 0xff)
167 		);
168 	return NULL;
169 }
170 
171 /* ------------------------------------------------------------------- */
172 
173 static const struct v4l2_queryctrl no_ctl = {
174 	.name  = "42",
175 	.flags = V4L2_CTRL_FLAG_DISABLED,
176 };
177 
178 static struct cx23885_ctrl cx23885_ctls[] = {
179 	/* --- video --- */
180 	{
181 		.v = {
182 			.id            = V4L2_CID_BRIGHTNESS,
183 			.name          = "Brightness",
184 			.minimum       = 0x00,
185 			.maximum       = 0xff,
186 			.step          = 1,
187 			.default_value = 0x7f,
188 			.type          = V4L2_CTRL_TYPE_INTEGER,
189 		},
190 		.off                   = 128,
191 		.reg                   = LUMA_CTRL,
192 		.mask                  = 0x00ff,
193 		.shift                 = 0,
194 	}, {
195 		.v = {
196 			.id            = V4L2_CID_CONTRAST,
197 			.name          = "Contrast",
198 			.minimum       = 0,
199 			.maximum       = 0x7f,
200 			.step          = 1,
201 			.default_value = 0x3f,
202 			.type          = V4L2_CTRL_TYPE_INTEGER,
203 		},
204 		.off                   = 0,
205 		.reg                   = LUMA_CTRL,
206 		.mask                  = 0xff00,
207 		.shift                 = 8,
208 	}, {
209 		.v = {
210 			.id            = V4L2_CID_HUE,
211 			.name          = "Hue",
212 			.minimum       = -127,
213 			.maximum       = 128,
214 			.step          = 1,
215 			.default_value = 0x0,
216 			.type          = V4L2_CTRL_TYPE_INTEGER,
217 		},
218 		.off                   = 128,
219 		.reg                   = CHROMA_CTRL,
220 		.mask                  = 0xff0000,
221 		.shift                 = 16,
222 	}, {
223 		/* strictly, this only describes only U saturation.
224 		 * V saturation is handled specially through code.
225 		 */
226 		.v = {
227 			.id            = V4L2_CID_SATURATION,
228 			.name          = "Saturation",
229 			.minimum       = 0,
230 			.maximum       = 0x7f,
231 			.step          = 1,
232 			.default_value = 0x3f,
233 			.type          = V4L2_CTRL_TYPE_INTEGER,
234 		},
235 		.off                   = 0,
236 		.reg                   = CHROMA_CTRL,
237 		.mask                  = 0x00ff,
238 		.shift                 = 0,
239 	}, {
240 	/* --- audio --- */
241 		.v = {
242 			.id            = V4L2_CID_AUDIO_MUTE,
243 			.name          = "Mute",
244 			.minimum       = 0,
245 			.maximum       = 1,
246 			.default_value = 1,
247 			.type          = V4L2_CTRL_TYPE_BOOLEAN,
248 		},
249 		.reg                   = PATH1_CTL1,
250 		.mask                  = (0x1f << 24),
251 		.shift                 = 24,
252 	}, {
253 		.v = {
254 			.id            = V4L2_CID_AUDIO_VOLUME,
255 			.name          = "Volume",
256 			.minimum       = 0,
257 			.maximum       = 65535,
258 			.step          = 65535 / 100,
259 			.default_value = 65535,
260 			.type          = V4L2_CTRL_TYPE_INTEGER,
261 		},
262 		.reg                   = PATH1_VOL_CTL,
263 		.mask                  = 0xff,
264 		.shift                 = 0,
265 	}
266 };
267 static const int CX23885_CTLS = ARRAY_SIZE(cx23885_ctls);
268 
269 /* Must be sorted from low to high control ID! */
270 static const u32 cx23885_user_ctrls[] = {
271 	V4L2_CID_USER_CLASS,
272 	V4L2_CID_BRIGHTNESS,
273 	V4L2_CID_CONTRAST,
274 	V4L2_CID_SATURATION,
275 	V4L2_CID_HUE,
276 	V4L2_CID_AUDIO_VOLUME,
277 	V4L2_CID_AUDIO_MUTE,
278 	0
279 };
280 
281 static const u32 *ctrl_classes[] = {
282 	cx23885_user_ctrls,
283 	NULL
284 };
285 
286 void cx23885_video_wakeup(struct cx23885_dev *dev,
287 	struct cx23885_dmaqueue *q, u32 count)
288 {
289 	struct cx23885_buffer *buf;
290 	int bc;
291 
292 	for (bc = 0;; bc++) {
293 		if (list_empty(&q->active))
294 			break;
295 		buf = list_entry(q->active.next,
296 				 struct cx23885_buffer, vb.queue);
297 
298 		/* count comes from the hw and is is 16bit wide --
299 		 * this trick handles wrap-arounds correctly for
300 		 * up to 32767 buffers in flight... */
301 		if ((s16) (count - buf->count) < 0)
302 			break;
303 
304 		v4l2_get_timestamp(&buf->vb.ts);
305 		dprintk(2, "[%p/%d] wakeup reg=%d buf=%d\n", buf, buf->vb.i,
306 			count, buf->count);
307 		buf->vb.state = VIDEOBUF_DONE;
308 		list_del(&buf->vb.queue);
309 		wake_up(&buf->vb.done);
310 	}
311 	if (list_empty(&q->active))
312 		del_timer(&q->timeout);
313 	else
314 		mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
315 	if (bc != 1)
316 		printk(KERN_ERR "%s: %d buffers handled (should be 1)\n",
317 			__func__, bc);
318 }
319 
320 int cx23885_set_tvnorm(struct cx23885_dev *dev, v4l2_std_id norm)
321 {
322 	dprintk(1, "%s(norm = 0x%08x) name: [%s]\n",
323 		__func__,
324 		(unsigned int)norm,
325 		v4l2_norm_to_name(norm));
326 
327 	dev->tvnorm = norm;
328 
329 	call_all(dev, video, s_std, norm);
330 
331 	return 0;
332 }
333 
334 static struct video_device *cx23885_vdev_init(struct cx23885_dev *dev,
335 				    struct pci_dev *pci,
336 				    struct video_device *template,
337 				    char *type)
338 {
339 	struct video_device *vfd;
340 	dprintk(1, "%s()\n", __func__);
341 
342 	vfd = video_device_alloc();
343 	if (NULL == vfd)
344 		return NULL;
345 	*vfd = *template;
346 	vfd->v4l2_dev = &dev->v4l2_dev;
347 	vfd->release = video_device_release;
348 	snprintf(vfd->name, sizeof(vfd->name), "%s (%s)",
349 		 cx23885_boards[dev->board].name, type);
350 	video_set_drvdata(vfd, dev);
351 	return vfd;
352 }
353 
354 static int cx23885_ctrl_query(struct v4l2_queryctrl *qctrl)
355 {
356 	int i;
357 
358 	if (qctrl->id < V4L2_CID_BASE ||
359 	    qctrl->id >= V4L2_CID_LASTP1)
360 		return -EINVAL;
361 	for (i = 0; i < CX23885_CTLS; i++)
362 		if (cx23885_ctls[i].v.id == qctrl->id)
363 			break;
364 	if (i == CX23885_CTLS) {
365 		*qctrl = no_ctl;
366 		return 0;
367 	}
368 	*qctrl = cx23885_ctls[i].v;
369 	return 0;
370 }
371 
372 /* ------------------------------------------------------------------- */
373 /* resource management                                                 */
374 
375 static int res_get(struct cx23885_dev *dev, struct cx23885_fh *fh,
376 	unsigned int bit)
377 {
378 	dprintk(1, "%s()\n", __func__);
379 	if (fh->resources & bit)
380 		/* have it already allocated */
381 		return 1;
382 
383 	/* is it free? */
384 	mutex_lock(&dev->lock);
385 	if (dev->resources & bit) {
386 		/* no, someone else uses it */
387 		mutex_unlock(&dev->lock);
388 		return 0;
389 	}
390 	/* it's free, grab it */
391 	fh->resources  |= bit;
392 	dev->resources |= bit;
393 	dprintk(1, "res: get %d\n", bit);
394 	mutex_unlock(&dev->lock);
395 	return 1;
396 }
397 
398 static int res_check(struct cx23885_fh *fh, unsigned int bit)
399 {
400 	return fh->resources & bit;
401 }
402 
403 static int res_locked(struct cx23885_dev *dev, unsigned int bit)
404 {
405 	return dev->resources & bit;
406 }
407 
408 static void res_free(struct cx23885_dev *dev, struct cx23885_fh *fh,
409 	unsigned int bits)
410 {
411 	BUG_ON((fh->resources & bits) != bits);
412 	dprintk(1, "%s()\n", __func__);
413 
414 	mutex_lock(&dev->lock);
415 	fh->resources  &= ~bits;
416 	dev->resources &= ~bits;
417 	dprintk(1, "res: put %d\n", bits);
418 	mutex_unlock(&dev->lock);
419 }
420 
421 int cx23885_flatiron_write(struct cx23885_dev *dev, u8 reg, u8 data)
422 {
423 	/* 8 bit registers, 8 bit values */
424 	u8 buf[] = { reg, data };
425 
426 	struct i2c_msg msg = { .addr = 0x98 >> 1,
427 		.flags = 0, .buf = buf, .len = 2 };
428 
429 	return i2c_transfer(&dev->i2c_bus[2].i2c_adap, &msg, 1);
430 }
431 
432 u8 cx23885_flatiron_read(struct cx23885_dev *dev, u8 reg)
433 {
434 	/* 8 bit registers, 8 bit values */
435 	int ret;
436 	u8 b0[] = { reg };
437 	u8 b1[] = { 0 };
438 
439 	struct i2c_msg msg[] = {
440 		{ .addr = 0x98 >> 1, .flags = 0, .buf = b0, .len = 1 },
441 		{ .addr = 0x98 >> 1, .flags = I2C_M_RD, .buf = b1, .len = 1 }
442 	};
443 
444 	ret = i2c_transfer(&dev->i2c_bus[2].i2c_adap, &msg[0], 2);
445 	if (ret != 2)
446 		printk(KERN_ERR "%s() error\n", __func__);
447 
448 	return b1[0];
449 }
450 
451 static void cx23885_flatiron_dump(struct cx23885_dev *dev)
452 {
453 	int i;
454 	dprintk(1, "Flatiron dump\n");
455 	for (i = 0; i < 0x24; i++) {
456 		dprintk(1, "FI[%02x] = %02x\n", i,
457 			cx23885_flatiron_read(dev, i));
458 	}
459 }
460 
461 static int cx23885_flatiron_mux(struct cx23885_dev *dev, int input)
462 {
463 	u8 val;
464 	dprintk(1, "%s(input = %d)\n", __func__, input);
465 
466 	if (input == 1)
467 		val = cx23885_flatiron_read(dev, CH_PWR_CTRL1) & ~FLD_CH_SEL;
468 	else if (input == 2)
469 		val = cx23885_flatiron_read(dev, CH_PWR_CTRL1) | FLD_CH_SEL;
470 	else
471 		return -EINVAL;
472 
473 	val |= 0x20; /* Enable clock to delta-sigma and dec filter */
474 
475 	cx23885_flatiron_write(dev, CH_PWR_CTRL1, val);
476 
477 	/* Wake up */
478 	cx23885_flatiron_write(dev, CH_PWR_CTRL2, 0);
479 
480 	if (video_debug)
481 		cx23885_flatiron_dump(dev);
482 
483 	return 0;
484 }
485 
486 static int cx23885_video_mux(struct cx23885_dev *dev, unsigned int input)
487 {
488 	dprintk(1, "%s() video_mux: %d [vmux=%d, gpio=0x%x,0x%x,0x%x,0x%x]\n",
489 		__func__,
490 		input, INPUT(input)->vmux,
491 		INPUT(input)->gpio0, INPUT(input)->gpio1,
492 		INPUT(input)->gpio2, INPUT(input)->gpio3);
493 	dev->input = input;
494 
495 	if (dev->board == CX23885_BOARD_MYGICA_X8506 ||
496 		dev->board == CX23885_BOARD_MAGICPRO_PROHDTVE2 ||
497 		dev->board == CX23885_BOARD_MYGICA_X8507) {
498 		/* Select Analog TV */
499 		if (INPUT(input)->type == CX23885_VMUX_TELEVISION)
500 			cx23885_gpio_clear(dev, GPIO_0);
501 	}
502 
503 	/* Tell the internal A/V decoder */
504 	v4l2_subdev_call(dev->sd_cx25840, video, s_routing,
505 			INPUT(input)->vmux, 0, 0);
506 
507 	if ((dev->board == CX23885_BOARD_HAUPPAUGE_HVR1800) ||
508 		(dev->board == CX23885_BOARD_MPX885) ||
509 		(dev->board == CX23885_BOARD_HAUPPAUGE_HVR1250) ||
510 		(dev->board == CX23885_BOARD_HAUPPAUGE_IMPACTVCBE) ||
511 		(dev->board == CX23885_BOARD_HAUPPAUGE_HVR1255) ||
512 		(dev->board == CX23885_BOARD_HAUPPAUGE_HVR1255_22111) ||
513 		(dev->board == CX23885_BOARD_HAUPPAUGE_HVR1850) ||
514 		(dev->board == CX23885_BOARD_MYGICA_X8507) ||
515 		(dev->board == CX23885_BOARD_AVERMEDIA_HC81R)) {
516 		/* Configure audio routing */
517 		v4l2_subdev_call(dev->sd_cx25840, audio, s_routing,
518 			INPUT(input)->amux, 0, 0);
519 
520 		if (INPUT(input)->amux == CX25840_AUDIO7)
521 			cx23885_flatiron_mux(dev, 1);
522 		else if (INPUT(input)->amux == CX25840_AUDIO6)
523 			cx23885_flatiron_mux(dev, 2);
524 	}
525 
526 	return 0;
527 }
528 
529 static int cx23885_audio_mux(struct cx23885_dev *dev, unsigned int input)
530 {
531 	dprintk(1, "%s(input=%d)\n", __func__, input);
532 
533 	/* The baseband video core of the cx23885 has two audio inputs.
534 	 * LR1 and LR2. In almost every single case so far only HVR1xxx
535 	 * cards we've only ever supported LR1. Time to support LR2,
536 	 * which is available via the optional white breakout header on
537 	 * the board.
538 	 * We'll use a could of existing enums in the card struct to allow
539 	 * devs to specify which baseband input they need, or just default
540 	 * to what we've always used.
541 	 */
542 	if (INPUT(input)->amux == CX25840_AUDIO7)
543 		cx23885_flatiron_mux(dev, 1);
544 	else if (INPUT(input)->amux == CX25840_AUDIO6)
545 		cx23885_flatiron_mux(dev, 2);
546 	else {
547 		/* Not specifically defined, assume the default. */
548 		cx23885_flatiron_mux(dev, 1);
549 	}
550 
551 	return 0;
552 }
553 
554 /* ------------------------------------------------------------------ */
555 static int cx23885_start_video_dma(struct cx23885_dev *dev,
556 			   struct cx23885_dmaqueue *q,
557 			   struct cx23885_buffer *buf)
558 {
559 	dprintk(1, "%s()\n", __func__);
560 
561 	/* Stop the dma/fifo before we tamper with it's risc programs */
562 	cx_clear(VID_A_DMA_CTL, 0x11);
563 
564 	/* setup fifo + format */
565 	cx23885_sram_channel_setup(dev, &dev->sram_channels[SRAM_CH01],
566 				buf->bpl, buf->risc.dma);
567 
568 	/* reset counter */
569 	cx_write(VID_A_GPCNT_CTL, 3);
570 	q->count = 1;
571 
572 	/* enable irq */
573 	cx23885_irq_add_enable(dev, 0x01);
574 	cx_set(VID_A_INT_MSK, 0x000011);
575 
576 	/* start dma */
577 	cx_set(DEV_CNTRL2, (1<<5));
578 	cx_set(VID_A_DMA_CTL, 0x11); /* FIFO and RISC enable */
579 
580 	return 0;
581 }
582 
583 
584 static int cx23885_restart_video_queue(struct cx23885_dev *dev,
585 			       struct cx23885_dmaqueue *q)
586 {
587 	struct cx23885_buffer *buf, *prev;
588 	struct list_head *item;
589 	dprintk(1, "%s()\n", __func__);
590 
591 	if (!list_empty(&q->active)) {
592 		buf = list_entry(q->active.next, struct cx23885_buffer,
593 			vb.queue);
594 		dprintk(2, "restart_queue [%p/%d]: restart dma\n",
595 			buf, buf->vb.i);
596 		cx23885_start_video_dma(dev, q, buf);
597 		list_for_each(item, &q->active) {
598 			buf = list_entry(item, struct cx23885_buffer,
599 				vb.queue);
600 			buf->count    = q->count++;
601 		}
602 		mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
603 		return 0;
604 	}
605 
606 	prev = NULL;
607 	for (;;) {
608 		if (list_empty(&q->queued))
609 			return 0;
610 		buf = list_entry(q->queued.next, struct cx23885_buffer,
611 			vb.queue);
612 		if (NULL == prev) {
613 			list_move_tail(&buf->vb.queue, &q->active);
614 			cx23885_start_video_dma(dev, q, buf);
615 			buf->vb.state = VIDEOBUF_ACTIVE;
616 			buf->count    = q->count++;
617 			mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
618 			dprintk(2, "[%p/%d] restart_queue - first active\n",
619 				buf, buf->vb.i);
620 
621 		} else if (prev->vb.width  == buf->vb.width  &&
622 			   prev->vb.height == buf->vb.height &&
623 			   prev->fmt       == buf->fmt) {
624 			list_move_tail(&buf->vb.queue, &q->active);
625 			buf->vb.state = VIDEOBUF_ACTIVE;
626 			buf->count    = q->count++;
627 			prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
628 			prev->risc.jmp[2] = cpu_to_le32(0); /* Bits 63 - 32 */
629 			dprintk(2, "[%p/%d] restart_queue - move to active\n",
630 				buf, buf->vb.i);
631 		} else {
632 			return 0;
633 		}
634 		prev = buf;
635 	}
636 }
637 
638 static int buffer_setup(struct videobuf_queue *q, unsigned int *count,
639 	unsigned int *size)
640 {
641 	struct cx23885_fh *fh = q->priv_data;
642 
643 	*size = fh->fmt->depth*fh->width*fh->height >> 3;
644 	if (0 == *count)
645 		*count = 32;
646 	if (*size * *count > vid_limit * 1024 * 1024)
647 		*count = (vid_limit * 1024 * 1024) / *size;
648 	return 0;
649 }
650 
651 static int buffer_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
652 	       enum v4l2_field field)
653 {
654 	struct cx23885_fh *fh  = q->priv_data;
655 	struct cx23885_dev *dev = fh->dev;
656 	struct cx23885_buffer *buf =
657 		container_of(vb, struct cx23885_buffer, vb);
658 	int rc, init_buffer = 0;
659 	u32 line0_offset, line1_offset;
660 	struct videobuf_dmabuf *dma = videobuf_to_dma(&buf->vb);
661 	int field_tff;
662 
663 	BUG_ON(NULL == fh->fmt);
664 	if (fh->width  < 48 || fh->width  > norm_maxw(dev->tvnorm) ||
665 	    fh->height < 32 || fh->height > norm_maxh(dev->tvnorm))
666 		return -EINVAL;
667 	buf->vb.size = (fh->width * fh->height * fh->fmt->depth) >> 3;
668 	if (0 != buf->vb.baddr  &&  buf->vb.bsize < buf->vb.size)
669 		return -EINVAL;
670 
671 	if (buf->fmt       != fh->fmt    ||
672 	    buf->vb.width  != fh->width  ||
673 	    buf->vb.height != fh->height ||
674 	    buf->vb.field  != field) {
675 		buf->fmt       = fh->fmt;
676 		buf->vb.width  = fh->width;
677 		buf->vb.height = fh->height;
678 		buf->vb.field  = field;
679 		init_buffer = 1;
680 	}
681 
682 	if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
683 		init_buffer = 1;
684 		rc = videobuf_iolock(q, &buf->vb, NULL);
685 		if (0 != rc)
686 			goto fail;
687 	}
688 
689 	if (init_buffer) {
690 		buf->bpl = buf->vb.width * buf->fmt->depth >> 3;
691 		switch (buf->vb.field) {
692 		case V4L2_FIELD_TOP:
693 			cx23885_risc_buffer(dev->pci, &buf->risc,
694 					 dma->sglist, 0, UNSET,
695 					 buf->bpl, 0, buf->vb.height);
696 			break;
697 		case V4L2_FIELD_BOTTOM:
698 			cx23885_risc_buffer(dev->pci, &buf->risc,
699 					 dma->sglist, UNSET, 0,
700 					 buf->bpl, 0, buf->vb.height);
701 			break;
702 		case V4L2_FIELD_INTERLACED:
703 			if (dev->tvnorm & V4L2_STD_NTSC)
704 				/* NTSC or  */
705 				field_tff = 1;
706 			else
707 				field_tff = 0;
708 
709 			if (cx23885_boards[dev->board].force_bff)
710 				/* PAL / SECAM OR 888 in NTSC MODE */
711 				field_tff = 0;
712 
713 			if (field_tff) {
714 				/* cx25840 transmits NTSC bottom field first */
715 				dprintk(1, "%s() Creating TFF/NTSC risc\n",
716 					__func__);
717 				line0_offset = buf->bpl;
718 				line1_offset = 0;
719 			} else {
720 				/* All other formats are top field first */
721 				dprintk(1, "%s() Creating BFF/PAL/SECAM risc\n",
722 					__func__);
723 				line0_offset = 0;
724 				line1_offset = buf->bpl;
725 			}
726 			cx23885_risc_buffer(dev->pci, &buf->risc,
727 					dma->sglist, line0_offset,
728 					line1_offset,
729 					buf->bpl, buf->bpl,
730 					buf->vb.height >> 1);
731 			break;
732 		case V4L2_FIELD_SEQ_TB:
733 			cx23885_risc_buffer(dev->pci, &buf->risc,
734 					 dma->sglist,
735 					 0, buf->bpl * (buf->vb.height >> 1),
736 					 buf->bpl, 0,
737 					 buf->vb.height >> 1);
738 			break;
739 		case V4L2_FIELD_SEQ_BT:
740 			cx23885_risc_buffer(dev->pci, &buf->risc,
741 					 dma->sglist,
742 					 buf->bpl * (buf->vb.height >> 1), 0,
743 					 buf->bpl, 0,
744 					 buf->vb.height >> 1);
745 			break;
746 		default:
747 			BUG();
748 		}
749 	}
750 	dprintk(2, "[%p/%d] buffer_prep - %dx%d %dbpp \"%s\" - dma=0x%08lx\n",
751 		buf, buf->vb.i,
752 		fh->width, fh->height, fh->fmt->depth, fh->fmt->name,
753 		(unsigned long)buf->risc.dma);
754 
755 	buf->vb.state = VIDEOBUF_PREPARED;
756 	return 0;
757 
758  fail:
759 	cx23885_free_buffer(q, buf);
760 	return rc;
761 }
762 
763 static void buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
764 {
765 	struct cx23885_buffer   *buf = container_of(vb,
766 		struct cx23885_buffer, vb);
767 	struct cx23885_buffer   *prev;
768 	struct cx23885_fh       *fh   = vq->priv_data;
769 	struct cx23885_dev      *dev  = fh->dev;
770 	struct cx23885_dmaqueue *q    = &dev->vidq;
771 
772 	/* add jump to stopper */
773 	buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_IRQ1 | RISC_CNT_INC);
774 	buf->risc.jmp[1] = cpu_to_le32(q->stopper.dma);
775 	buf->risc.jmp[2] = cpu_to_le32(0); /* bits 63-32 */
776 
777 	if (!list_empty(&q->queued)) {
778 		list_add_tail(&buf->vb.queue, &q->queued);
779 		buf->vb.state = VIDEOBUF_QUEUED;
780 		dprintk(2, "[%p/%d] buffer_queue - append to queued\n",
781 			buf, buf->vb.i);
782 
783 	} else if (list_empty(&q->active)) {
784 		list_add_tail(&buf->vb.queue, &q->active);
785 		cx23885_start_video_dma(dev, q, buf);
786 		buf->vb.state = VIDEOBUF_ACTIVE;
787 		buf->count    = q->count++;
788 		mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
789 		dprintk(2, "[%p/%d] buffer_queue - first active\n",
790 			buf, buf->vb.i);
791 
792 	} else {
793 		prev = list_entry(q->active.prev, struct cx23885_buffer,
794 			vb.queue);
795 		if (prev->vb.width  == buf->vb.width  &&
796 		    prev->vb.height == buf->vb.height &&
797 		    prev->fmt       == buf->fmt) {
798 			list_add_tail(&buf->vb.queue, &q->active);
799 			buf->vb.state = VIDEOBUF_ACTIVE;
800 			buf->count    = q->count++;
801 			prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
802 			/* 64 bit bits 63-32 */
803 			prev->risc.jmp[2] = cpu_to_le32(0);
804 			dprintk(2, "[%p/%d] buffer_queue - append to active\n",
805 				buf, buf->vb.i);
806 
807 		} else {
808 			list_add_tail(&buf->vb.queue, &q->queued);
809 			buf->vb.state = VIDEOBUF_QUEUED;
810 			dprintk(2, "[%p/%d] buffer_queue - first queued\n",
811 				buf, buf->vb.i);
812 		}
813 	}
814 }
815 
816 static void buffer_release(struct videobuf_queue *q,
817 	struct videobuf_buffer *vb)
818 {
819 	struct cx23885_buffer *buf = container_of(vb,
820 		struct cx23885_buffer, vb);
821 
822 	cx23885_free_buffer(q, buf);
823 }
824 
825 static struct videobuf_queue_ops cx23885_video_qops = {
826 	.buf_setup    = buffer_setup,
827 	.buf_prepare  = buffer_prepare,
828 	.buf_queue    = buffer_queue,
829 	.buf_release  = buffer_release,
830 };
831 
832 static struct videobuf_queue *get_queue(struct cx23885_fh *fh)
833 {
834 	switch (fh->type) {
835 	case V4L2_BUF_TYPE_VIDEO_CAPTURE:
836 		return &fh->vidq;
837 	case V4L2_BUF_TYPE_VBI_CAPTURE:
838 		return &fh->vbiq;
839 	default:
840 		BUG();
841 		return NULL;
842 	}
843 }
844 
845 static int get_resource(struct cx23885_fh *fh)
846 {
847 	switch (fh->type) {
848 	case V4L2_BUF_TYPE_VIDEO_CAPTURE:
849 		return RESOURCE_VIDEO;
850 	case V4L2_BUF_TYPE_VBI_CAPTURE:
851 		return RESOURCE_VBI;
852 	default:
853 		BUG();
854 		return 0;
855 	}
856 }
857 
858 static int video_open(struct file *file)
859 {
860 	struct video_device *vdev = video_devdata(file);
861 	struct cx23885_dev *dev = video_drvdata(file);
862 	struct cx23885_fh *fh;
863 	enum v4l2_buf_type type = 0;
864 	int radio = 0;
865 
866 	switch (vdev->vfl_type) {
867 	case VFL_TYPE_GRABBER:
868 		type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
869 		break;
870 	case VFL_TYPE_VBI:
871 		type = V4L2_BUF_TYPE_VBI_CAPTURE;
872 		break;
873 	case VFL_TYPE_RADIO:
874 		radio = 1;
875 		break;
876 	}
877 
878 	dprintk(1, "open dev=%s radio=%d type=%s\n",
879 		video_device_node_name(vdev), radio, v4l2_type_names[type]);
880 
881 	/* allocate + initialize per filehandle data */
882 	fh = kzalloc(sizeof(*fh), GFP_KERNEL);
883 	if (NULL == fh)
884 		return -ENOMEM;
885 
886 	file->private_data = fh;
887 	fh->dev      = dev;
888 	fh->radio    = radio;
889 	fh->type     = type;
890 	fh->width    = 320;
891 	fh->height   = 240;
892 	fh->fmt      = format_by_fourcc(V4L2_PIX_FMT_YUYV);
893 
894 	videobuf_queue_sg_init(&fh->vidq, &cx23885_video_qops,
895 			    &dev->pci->dev, &dev->slock,
896 			    V4L2_BUF_TYPE_VIDEO_CAPTURE,
897 			    V4L2_FIELD_INTERLACED,
898 			    sizeof(struct cx23885_buffer),
899 			    fh, NULL);
900 
901 	videobuf_queue_sg_init(&fh->vbiq, &cx23885_vbi_qops,
902 		&dev->pci->dev, &dev->slock,
903 		V4L2_BUF_TYPE_VBI_CAPTURE,
904 		V4L2_FIELD_SEQ_TB,
905 		sizeof(struct cx23885_buffer),
906 		fh, NULL);
907 
908 
909 	dprintk(1, "post videobuf_queue_init()\n");
910 
911 	return 0;
912 }
913 
914 static ssize_t video_read(struct file *file, char __user *data,
915 	size_t count, loff_t *ppos)
916 {
917 	struct cx23885_fh *fh = file->private_data;
918 
919 	switch (fh->type) {
920 	case V4L2_BUF_TYPE_VIDEO_CAPTURE:
921 		if (res_locked(fh->dev, RESOURCE_VIDEO))
922 			return -EBUSY;
923 		return videobuf_read_one(&fh->vidq, data, count, ppos,
924 					 file->f_flags & O_NONBLOCK);
925 	case V4L2_BUF_TYPE_VBI_CAPTURE:
926 		if (!res_get(fh->dev, fh, RESOURCE_VBI))
927 			return -EBUSY;
928 		return videobuf_read_stream(&fh->vbiq, data, count, ppos, 1,
929 					    file->f_flags & O_NONBLOCK);
930 	default:
931 		BUG();
932 		return 0;
933 	}
934 }
935 
936 static unsigned int video_poll(struct file *file,
937 	struct poll_table_struct *wait)
938 {
939 	struct cx23885_fh *fh = file->private_data;
940 	struct cx23885_buffer *buf;
941 	unsigned int rc = POLLERR;
942 
943 	if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type) {
944 		if (!res_get(fh->dev, fh, RESOURCE_VBI))
945 			return POLLERR;
946 		return videobuf_poll_stream(file, &fh->vbiq, wait);
947 	}
948 
949 	mutex_lock(&fh->vidq.vb_lock);
950 	if (res_check(fh, RESOURCE_VIDEO)) {
951 		/* streaming capture */
952 		if (list_empty(&fh->vidq.stream))
953 			goto done;
954 		buf = list_entry(fh->vidq.stream.next,
955 			struct cx23885_buffer, vb.stream);
956 	} else {
957 		/* read() capture */
958 		buf = (struct cx23885_buffer *)fh->vidq.read_buf;
959 		if (NULL == buf)
960 			goto done;
961 	}
962 	poll_wait(file, &buf->vb.done, wait);
963 	if (buf->vb.state == VIDEOBUF_DONE ||
964 	    buf->vb.state == VIDEOBUF_ERROR)
965 		rc =  POLLIN|POLLRDNORM;
966 	else
967 		rc = 0;
968 done:
969 	mutex_unlock(&fh->vidq.vb_lock);
970 	return rc;
971 }
972 
973 static int video_release(struct file *file)
974 {
975 	struct cx23885_fh *fh = file->private_data;
976 	struct cx23885_dev *dev = fh->dev;
977 
978 	/* turn off overlay */
979 	if (res_check(fh, RESOURCE_OVERLAY)) {
980 		/* FIXME */
981 		res_free(dev, fh, RESOURCE_OVERLAY);
982 	}
983 
984 	/* stop video capture */
985 	if (res_check(fh, RESOURCE_VIDEO)) {
986 		videobuf_queue_cancel(&fh->vidq);
987 		res_free(dev, fh, RESOURCE_VIDEO);
988 	}
989 	if (fh->vidq.read_buf) {
990 		buffer_release(&fh->vidq, fh->vidq.read_buf);
991 		kfree(fh->vidq.read_buf);
992 	}
993 
994 	/* stop vbi capture */
995 	if (res_check(fh, RESOURCE_VBI)) {
996 		if (fh->vbiq.streaming)
997 			videobuf_streamoff(&fh->vbiq);
998 		if (fh->vbiq.reading)
999 			videobuf_read_stop(&fh->vbiq);
1000 		res_free(dev, fh, RESOURCE_VBI);
1001 	}
1002 
1003 	videobuf_mmap_free(&fh->vidq);
1004 	videobuf_mmap_free(&fh->vbiq);
1005 
1006 	file->private_data = NULL;
1007 	kfree(fh);
1008 
1009 	/* We are not putting the tuner to sleep here on exit, because
1010 	 * we want to use the mpeg encoder in another session to capture
1011 	 * tuner video. Closing this will result in no video to the encoder.
1012 	 */
1013 
1014 	return 0;
1015 }
1016 
1017 static int video_mmap(struct file *file, struct vm_area_struct *vma)
1018 {
1019 	struct cx23885_fh *fh = file->private_data;
1020 
1021 	return videobuf_mmap_mapper(get_queue(fh), vma);
1022 }
1023 
1024 /* ------------------------------------------------------------------ */
1025 /* VIDEO CTRL IOCTLS                                                  */
1026 
1027 int cx23885_get_control(struct cx23885_dev *dev,
1028 	struct v4l2_control *ctl)
1029 {
1030 	dprintk(1, "%s() calling cx25840(VIDIOC_G_CTRL)\n", __func__);
1031 	call_all(dev, core, g_ctrl, ctl);
1032 	return 0;
1033 }
1034 
1035 int cx23885_set_control(struct cx23885_dev *dev,
1036 	struct v4l2_control *ctl)
1037 {
1038 	dprintk(1, "%s() calling cx25840(VIDIOC_S_CTRL)\n", __func__);
1039 	call_all(dev, core, s_ctrl, ctl);
1040 
1041 	return 0;
1042 }
1043 
1044 static void init_controls(struct cx23885_dev *dev)
1045 {
1046 	struct v4l2_control ctrl;
1047 	int i;
1048 
1049 	for (i = 0; i < CX23885_CTLS; i++) {
1050 		ctrl.id = cx23885_ctls[i].v.id;
1051 		ctrl.value = cx23885_ctls[i].v.default_value;
1052 
1053 		cx23885_set_control(dev, &ctrl);
1054 	}
1055 }
1056 
1057 /* ------------------------------------------------------------------ */
1058 /* VIDEO IOCTLS                                                       */
1059 
1060 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
1061 	struct v4l2_format *f)
1062 {
1063 	struct cx23885_fh *fh   = priv;
1064 
1065 	f->fmt.pix.width        = fh->width;
1066 	f->fmt.pix.height       = fh->height;
1067 	f->fmt.pix.field        = fh->vidq.field;
1068 	f->fmt.pix.pixelformat  = fh->fmt->fourcc;
1069 	f->fmt.pix.bytesperline =
1070 		(f->fmt.pix.width * fh->fmt->depth) >> 3;
1071 	f->fmt.pix.sizeimage =
1072 		f->fmt.pix.height * f->fmt.pix.bytesperline;
1073 
1074 	return 0;
1075 }
1076 
1077 static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
1078 	struct v4l2_format *f)
1079 {
1080 	struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1081 	struct cx23885_fmt *fmt;
1082 	enum v4l2_field   field;
1083 	unsigned int      maxw, maxh;
1084 
1085 	fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1086 	if (NULL == fmt)
1087 		return -EINVAL;
1088 
1089 	field = f->fmt.pix.field;
1090 	maxw  = norm_maxw(dev->tvnorm);
1091 	maxh  = norm_maxh(dev->tvnorm);
1092 
1093 	if (V4L2_FIELD_ANY == field) {
1094 		field = (f->fmt.pix.height > maxh/2)
1095 			? V4L2_FIELD_INTERLACED
1096 			: V4L2_FIELD_BOTTOM;
1097 	}
1098 
1099 	switch (field) {
1100 	case V4L2_FIELD_TOP:
1101 	case V4L2_FIELD_BOTTOM:
1102 		maxh = maxh / 2;
1103 		break;
1104 	case V4L2_FIELD_INTERLACED:
1105 		break;
1106 	default:
1107 		return -EINVAL;
1108 	}
1109 
1110 	f->fmt.pix.field = field;
1111 	v4l_bound_align_image(&f->fmt.pix.width, 48, maxw, 2,
1112 			      &f->fmt.pix.height, 32, maxh, 0, 0);
1113 	f->fmt.pix.bytesperline =
1114 		(f->fmt.pix.width * fmt->depth) >> 3;
1115 	f->fmt.pix.sizeimage =
1116 		f->fmt.pix.height * f->fmt.pix.bytesperline;
1117 
1118 	return 0;
1119 }
1120 
1121 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
1122 	struct v4l2_format *f)
1123 {
1124 	struct cx23885_fh *fh = priv;
1125 	struct cx23885_dev *dev  = ((struct cx23885_fh *)priv)->dev;
1126 	struct v4l2_mbus_framefmt mbus_fmt;
1127 	int err;
1128 
1129 	dprintk(2, "%s()\n", __func__);
1130 	err = vidioc_try_fmt_vid_cap(file, priv, f);
1131 
1132 	if (0 != err)
1133 		return err;
1134 	fh->fmt        = format_by_fourcc(f->fmt.pix.pixelformat);
1135 	fh->width      = f->fmt.pix.width;
1136 	fh->height     = f->fmt.pix.height;
1137 	fh->vidq.field = f->fmt.pix.field;
1138 	dprintk(2, "%s() width=%d height=%d field=%d\n", __func__,
1139 		fh->width, fh->height, fh->vidq.field);
1140 	v4l2_fill_mbus_format(&mbus_fmt, &f->fmt.pix, V4L2_MBUS_FMT_FIXED);
1141 	call_all(dev, video, s_mbus_fmt, &mbus_fmt);
1142 	v4l2_fill_pix_format(&f->fmt.pix, &mbus_fmt);
1143 	return 0;
1144 }
1145 
1146 static int vidioc_querycap(struct file *file, void  *priv,
1147 	struct v4l2_capability *cap)
1148 {
1149 	struct cx23885_dev *dev  = ((struct cx23885_fh *)priv)->dev;
1150 
1151 	strcpy(cap->driver, "cx23885");
1152 	strlcpy(cap->card, cx23885_boards[dev->board].name,
1153 		sizeof(cap->card));
1154 	sprintf(cap->bus_info, "PCIe:%s", pci_name(dev->pci));
1155 	cap->capabilities =
1156 		V4L2_CAP_VIDEO_CAPTURE |
1157 		V4L2_CAP_READWRITE     |
1158 		V4L2_CAP_STREAMING     |
1159 		V4L2_CAP_VBI_CAPTURE;
1160 	if (dev->tuner_type != TUNER_ABSENT)
1161 		cap->capabilities |= V4L2_CAP_TUNER;
1162 	return 0;
1163 }
1164 
1165 static int vidioc_enum_fmt_vid_cap(struct file *file, void  *priv,
1166 	struct v4l2_fmtdesc *f)
1167 {
1168 	if (unlikely(f->index >= ARRAY_SIZE(formats)))
1169 		return -EINVAL;
1170 
1171 	strlcpy(f->description, formats[f->index].name,
1172 		sizeof(f->description));
1173 	f->pixelformat = formats[f->index].fourcc;
1174 
1175 	return 0;
1176 }
1177 
1178 static int vidioc_reqbufs(struct file *file, void *priv,
1179 	struct v4l2_requestbuffers *p)
1180 {
1181 	struct cx23885_fh *fh = priv;
1182 	return videobuf_reqbufs(get_queue(fh), p);
1183 }
1184 
1185 static int vidioc_querybuf(struct file *file, void *priv,
1186 	struct v4l2_buffer *p)
1187 {
1188 	struct cx23885_fh *fh = priv;
1189 	return videobuf_querybuf(get_queue(fh), p);
1190 }
1191 
1192 static int vidioc_qbuf(struct file *file, void *priv,
1193 	struct v4l2_buffer *p)
1194 {
1195 	struct cx23885_fh *fh = priv;
1196 	return videobuf_qbuf(get_queue(fh), p);
1197 }
1198 
1199 static int vidioc_dqbuf(struct file *file, void *priv,
1200 	struct v4l2_buffer *p)
1201 {
1202 	struct cx23885_fh *fh = priv;
1203 	return videobuf_dqbuf(get_queue(fh), p,
1204 				file->f_flags & O_NONBLOCK);
1205 }
1206 
1207 static int vidioc_streamon(struct file *file, void *priv,
1208 	enum v4l2_buf_type i)
1209 {
1210 	struct cx23885_fh *fh = priv;
1211 	struct cx23885_dev *dev = fh->dev;
1212 	dprintk(1, "%s()\n", __func__);
1213 
1214 	if ((fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) &&
1215 		(fh->type != V4L2_BUF_TYPE_VBI_CAPTURE))
1216 		return -EINVAL;
1217 	if (unlikely(i != fh->type))
1218 		return -EINVAL;
1219 
1220 	if (unlikely(!res_get(dev, fh, get_resource(fh))))
1221 		return -EBUSY;
1222 
1223 	/* Don't start VBI streaming unless vida streaming
1224 	 * has already started.
1225 	 */
1226 	if ((fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) &&
1227 		((cx_read(VID_A_DMA_CTL) & 0x11) == 0))
1228 		return -EINVAL;
1229 
1230 	return videobuf_streamon(get_queue(fh));
1231 }
1232 
1233 static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
1234 {
1235 	struct cx23885_fh *fh = priv;
1236 	struct cx23885_dev *dev = fh->dev;
1237 	int err, res;
1238 	dprintk(1, "%s()\n", __func__);
1239 
1240 	if ((fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) &&
1241 		(fh->type != V4L2_BUF_TYPE_VBI_CAPTURE))
1242 		return -EINVAL;
1243 	if (i != fh->type)
1244 		return -EINVAL;
1245 
1246 	res = get_resource(fh);
1247 	err = videobuf_streamoff(get_queue(fh));
1248 	if (err < 0)
1249 		return err;
1250 	res_free(dev, fh, res);
1251 	return 0;
1252 }
1253 
1254 static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *id)
1255 {
1256 	struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1257 	dprintk(1, "%s()\n", __func__);
1258 
1259 	*id = dev->tvnorm;
1260 	return 0;
1261 }
1262 
1263 static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id tvnorms)
1264 {
1265 	struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1266 	dprintk(1, "%s()\n", __func__);
1267 
1268 	mutex_lock(&dev->lock);
1269 	cx23885_set_tvnorm(dev, tvnorms);
1270 	mutex_unlock(&dev->lock);
1271 
1272 	return 0;
1273 }
1274 
1275 int cx23885_enum_input(struct cx23885_dev *dev, struct v4l2_input *i)
1276 {
1277 	static const char *iname[] = {
1278 		[CX23885_VMUX_COMPOSITE1] = "Composite1",
1279 		[CX23885_VMUX_COMPOSITE2] = "Composite2",
1280 		[CX23885_VMUX_COMPOSITE3] = "Composite3",
1281 		[CX23885_VMUX_COMPOSITE4] = "Composite4",
1282 		[CX23885_VMUX_SVIDEO]     = "S-Video",
1283 		[CX23885_VMUX_COMPONENT]  = "Component",
1284 		[CX23885_VMUX_TELEVISION] = "Television",
1285 		[CX23885_VMUX_CABLE]      = "Cable TV",
1286 		[CX23885_VMUX_DVB]        = "DVB",
1287 		[CX23885_VMUX_DEBUG]      = "for debug only",
1288 	};
1289 	unsigned int n;
1290 	dprintk(1, "%s()\n", __func__);
1291 
1292 	n = i->index;
1293 	if (n >= MAX_CX23885_INPUT)
1294 		return -EINVAL;
1295 
1296 	if (0 == INPUT(n)->type)
1297 		return -EINVAL;
1298 
1299 	i->index = n;
1300 	i->type  = V4L2_INPUT_TYPE_CAMERA;
1301 	strcpy(i->name, iname[INPUT(n)->type]);
1302 	if ((CX23885_VMUX_TELEVISION == INPUT(n)->type) ||
1303 		(CX23885_VMUX_CABLE == INPUT(n)->type)) {
1304 		i->type = V4L2_INPUT_TYPE_TUNER;
1305 		i->std = CX23885_NORMS;
1306 	}
1307 
1308 	/* Two selectable audio inputs for non-tv inputs */
1309 	if (INPUT(n)->type != CX23885_VMUX_TELEVISION)
1310 		i->audioset = 0x3;
1311 
1312 	if (dev->input == n) {
1313 		/* enum'd input matches our configured input.
1314 		 * Ask the video decoder to process the call
1315 		 * and give it an oppertunity to update the
1316 		 * status field.
1317 		 */
1318 		call_all(dev, video, g_input_status, &i->status);
1319 	}
1320 
1321 	return 0;
1322 }
1323 
1324 static int vidioc_enum_input(struct file *file, void *priv,
1325 				struct v4l2_input *i)
1326 {
1327 	struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1328 	dprintk(1, "%s()\n", __func__);
1329 	return cx23885_enum_input(dev, i);
1330 }
1331 
1332 int cx23885_get_input(struct file *file, void *priv, unsigned int *i)
1333 {
1334 	struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1335 
1336 	*i = dev->input;
1337 	dprintk(1, "%s() returns %d\n", __func__, *i);
1338 	return 0;
1339 }
1340 
1341 static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1342 {
1343 	return cx23885_get_input(file, priv, i);
1344 }
1345 
1346 int cx23885_set_input(struct file *file, void *priv, unsigned int i)
1347 {
1348 	struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1349 
1350 	dprintk(1, "%s(%d)\n", __func__, i);
1351 
1352 	if (i >= MAX_CX23885_INPUT) {
1353 		dprintk(1, "%s() -EINVAL\n", __func__);
1354 		return -EINVAL;
1355 	}
1356 
1357 	if (INPUT(i)->type == 0)
1358 		return -EINVAL;
1359 
1360 	mutex_lock(&dev->lock);
1361 	cx23885_video_mux(dev, i);
1362 
1363 	/* By default establish the default audio input for the card also */
1364 	/* Caller is free to use VIDIOC_S_AUDIO to override afterwards */
1365 	cx23885_audio_mux(dev, i);
1366 	mutex_unlock(&dev->lock);
1367 	return 0;
1368 }
1369 
1370 static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1371 {
1372 	return cx23885_set_input(file, priv, i);
1373 }
1374 
1375 static int vidioc_log_status(struct file *file, void *priv)
1376 {
1377 	struct cx23885_fh  *fh  = priv;
1378 	struct cx23885_dev *dev = fh->dev;
1379 
1380 	printk(KERN_INFO
1381 		"%s/0: ============  START LOG STATUS  ============\n",
1382 		dev->name);
1383 	call_all(dev, core, log_status);
1384 	printk(KERN_INFO
1385 		"%s/0: =============  END LOG STATUS  =============\n",
1386 		dev->name);
1387 	return 0;
1388 }
1389 
1390 static int cx23885_query_audinput(struct file *file, void *priv,
1391 	struct v4l2_audio *i)
1392 {
1393 	struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1394 	static const char *iname[] = {
1395 		[0] = "Baseband L/R 1",
1396 		[1] = "Baseband L/R 2",
1397 	};
1398 	unsigned int n;
1399 	dprintk(1, "%s()\n", __func__);
1400 
1401 	n = i->index;
1402 	if (n >= 2)
1403 		return -EINVAL;
1404 
1405 	memset(i, 0, sizeof(*i));
1406 	i->index = n;
1407 	strcpy(i->name, iname[n]);
1408 	i->capability  = V4L2_AUDCAP_STEREO;
1409 	i->mode  = V4L2_AUDMODE_AVL;
1410 	return 0;
1411 
1412 }
1413 
1414 static int vidioc_enum_audinput(struct file *file, void *priv,
1415 				struct v4l2_audio *i)
1416 {
1417 	return cx23885_query_audinput(file, priv, i);
1418 }
1419 
1420 static int vidioc_g_audinput(struct file *file, void *priv,
1421 	struct v4l2_audio *i)
1422 {
1423 	struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1424 
1425 	i->index = dev->audinput;
1426 	dprintk(1, "%s(input=%d)\n", __func__, i->index);
1427 
1428 	return cx23885_query_audinput(file, priv, i);
1429 }
1430 
1431 static int vidioc_s_audinput(struct file *file, void *priv,
1432 	const struct v4l2_audio *i)
1433 {
1434 	struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1435 	if (i->index >= 2)
1436 		return -EINVAL;
1437 
1438 	dprintk(1, "%s(%d)\n", __func__, i->index);
1439 
1440 	dev->audinput = i->index;
1441 
1442 	/* Skip the audio defaults from the cards struct, caller wants
1443 	 * directly touch the audio mux hardware. */
1444 	cx23885_flatiron_mux(dev, dev->audinput + 1);
1445 	return 0;
1446 }
1447 
1448 static int vidioc_queryctrl(struct file *file, void *priv,
1449 				struct v4l2_queryctrl *qctrl)
1450 {
1451 	qctrl->id = v4l2_ctrl_next(ctrl_classes, qctrl->id);
1452 	if (unlikely(qctrl->id == 0))
1453 		return -EINVAL;
1454 	return cx23885_ctrl_query(qctrl);
1455 }
1456 
1457 static int vidioc_g_ctrl(struct file *file, void *priv,
1458 				struct v4l2_control *ctl)
1459 {
1460 	struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1461 
1462 	return cx23885_get_control(dev, ctl);
1463 }
1464 
1465 static int vidioc_s_ctrl(struct file *file, void *priv,
1466 				struct v4l2_control *ctl)
1467 {
1468 	struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1469 
1470 	return cx23885_set_control(dev, ctl);
1471 }
1472 
1473 static int vidioc_g_tuner(struct file *file, void *priv,
1474 				struct v4l2_tuner *t)
1475 {
1476 	struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1477 
1478 	if (dev->tuner_type == TUNER_ABSENT)
1479 		return -EINVAL;
1480 	if (0 != t->index)
1481 		return -EINVAL;
1482 
1483 	strcpy(t->name, "Television");
1484 
1485 	call_all(dev, tuner, g_tuner, t);
1486 	return 0;
1487 }
1488 
1489 static int vidioc_s_tuner(struct file *file, void *priv,
1490 				const struct v4l2_tuner *t)
1491 {
1492 	struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1493 
1494 	if (dev->tuner_type == TUNER_ABSENT)
1495 		return -EINVAL;
1496 	if (0 != t->index)
1497 		return -EINVAL;
1498 	/* Update the A/V core */
1499 	call_all(dev, tuner, s_tuner, t);
1500 
1501 	return 0;
1502 }
1503 
1504 static int vidioc_g_frequency(struct file *file, void *priv,
1505 				struct v4l2_frequency *f)
1506 {
1507 	struct cx23885_fh *fh = priv;
1508 	struct cx23885_dev *dev = fh->dev;
1509 
1510 	if (dev->tuner_type == TUNER_ABSENT)
1511 		return -EINVAL;
1512 
1513 	/* f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV; */
1514 	f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1515 	f->frequency = dev->freq;
1516 
1517 	call_all(dev, tuner, g_frequency, f);
1518 
1519 	return 0;
1520 }
1521 
1522 static int cx23885_set_freq(struct cx23885_dev *dev, const struct v4l2_frequency *f)
1523 {
1524 	struct v4l2_control ctrl;
1525 
1526 	if (dev->tuner_type == TUNER_ABSENT)
1527 		return -EINVAL;
1528 	if (unlikely(f->tuner != 0))
1529 		return -EINVAL;
1530 
1531 	mutex_lock(&dev->lock);
1532 	dev->freq = f->frequency;
1533 
1534 	/* I need to mute audio here */
1535 	ctrl.id = V4L2_CID_AUDIO_MUTE;
1536 	ctrl.value = 1;
1537 	cx23885_set_control(dev, &ctrl);
1538 
1539 	call_all(dev, tuner, s_frequency, f);
1540 
1541 	/* When changing channels it is required to reset TVAUDIO */
1542 	msleep(100);
1543 
1544 	/* I need to unmute audio here */
1545 	ctrl.value = 0;
1546 	cx23885_set_control(dev, &ctrl);
1547 
1548 	mutex_unlock(&dev->lock);
1549 
1550 	return 0;
1551 }
1552 
1553 static int cx23885_set_freq_via_ops(struct cx23885_dev *dev,
1554 	const struct v4l2_frequency *f)
1555 {
1556 	struct v4l2_control ctrl;
1557 	struct videobuf_dvb_frontend *vfe;
1558 	struct dvb_frontend *fe;
1559 
1560 	struct analog_parameters params = {
1561 		.mode      = V4L2_TUNER_ANALOG_TV,
1562 		.audmode   = V4L2_TUNER_MODE_STEREO,
1563 		.std       = dev->tvnorm,
1564 		.frequency = f->frequency
1565 	};
1566 
1567 	mutex_lock(&dev->lock);
1568 	dev->freq = f->frequency;
1569 
1570 	/* I need to mute audio here */
1571 	ctrl.id = V4L2_CID_AUDIO_MUTE;
1572 	ctrl.value = 1;
1573 	cx23885_set_control(dev, &ctrl);
1574 
1575 	/* If HVR1850 */
1576 	dprintk(1, "%s() frequency=%d tuner=%d std=0x%llx\n", __func__,
1577 		params.frequency, f->tuner, params.std);
1578 
1579 	vfe = videobuf_dvb_get_frontend(&dev->ts2.frontends, 1);
1580 	if (!vfe) {
1581 		mutex_unlock(&dev->lock);
1582 		return -EINVAL;
1583 	}
1584 
1585 	fe = vfe->dvb.frontend;
1586 
1587 	if ((dev->board == CX23885_BOARD_HAUPPAUGE_HVR1850) ||
1588 	    (dev->board == CX23885_BOARD_HAUPPAUGE_HVR1255) ||
1589 	    (dev->board == CX23885_BOARD_HAUPPAUGE_HVR1255_22111))
1590 		fe = &dev->ts1.analog_fe;
1591 
1592 	if (fe && fe->ops.tuner_ops.set_analog_params) {
1593 		call_all(dev, video, s_std, dev->tvnorm);
1594 		fe->ops.tuner_ops.set_analog_params(fe, &params);
1595 	}
1596 	else
1597 		printk(KERN_ERR "%s() No analog tuner, aborting\n", __func__);
1598 
1599 	/* When changing channels it is required to reset TVAUDIO */
1600 	msleep(100);
1601 
1602 	/* I need to unmute audio here */
1603 	ctrl.value = 0;
1604 	cx23885_set_control(dev, &ctrl);
1605 
1606 	mutex_unlock(&dev->lock);
1607 
1608 	return 0;
1609 }
1610 
1611 int cx23885_set_frequency(struct file *file, void *priv,
1612 	const struct v4l2_frequency *f)
1613 {
1614 	struct cx23885_fh *fh = priv;
1615 	struct cx23885_dev *dev = fh->dev;
1616 	int ret;
1617 
1618 	switch (dev->board) {
1619 	case CX23885_BOARD_HAUPPAUGE_HVR1255:
1620 	case CX23885_BOARD_HAUPPAUGE_HVR1255_22111:
1621 	case CX23885_BOARD_HAUPPAUGE_HVR1850:
1622 		ret = cx23885_set_freq_via_ops(dev, f);
1623 		break;
1624 	default:
1625 		ret = cx23885_set_freq(dev, f);
1626 	}
1627 
1628 	return ret;
1629 }
1630 
1631 static int vidioc_s_frequency(struct file *file, void *priv,
1632 	const struct v4l2_frequency *f)
1633 {
1634 	return cx23885_set_frequency(file, priv, f);
1635 }
1636 
1637 /* ----------------------------------------------------------- */
1638 
1639 static void cx23885_vid_timeout(unsigned long data)
1640 {
1641 	struct cx23885_dev *dev = (struct cx23885_dev *)data;
1642 	struct cx23885_dmaqueue *q = &dev->vidq;
1643 	struct cx23885_buffer *buf;
1644 	unsigned long flags;
1645 
1646 	spin_lock_irqsave(&dev->slock, flags);
1647 	while (!list_empty(&q->active)) {
1648 		buf = list_entry(q->active.next,
1649 			struct cx23885_buffer, vb.queue);
1650 		list_del(&buf->vb.queue);
1651 		buf->vb.state = VIDEOBUF_ERROR;
1652 		wake_up(&buf->vb.done);
1653 		printk(KERN_ERR "%s: [%p/%d] timeout - dma=0x%08lx\n",
1654 			dev->name, buf, buf->vb.i,
1655 			(unsigned long)buf->risc.dma);
1656 	}
1657 	cx23885_restart_video_queue(dev, q);
1658 	spin_unlock_irqrestore(&dev->slock, flags);
1659 }
1660 
1661 int cx23885_video_irq(struct cx23885_dev *dev, u32 status)
1662 {
1663 	u32 mask, count;
1664 	int handled = 0;
1665 
1666 	mask   = cx_read(VID_A_INT_MSK);
1667 	if (0 == (status & mask))
1668 		return handled;
1669 
1670 	cx_write(VID_A_INT_STAT, status);
1671 
1672 	/* risc op code error, fifo overflow or line sync detection error */
1673 	if ((status & VID_BC_MSK_OPC_ERR) ||
1674 		(status & VID_BC_MSK_SYNC) ||
1675 		(status & VID_BC_MSK_OF)) {
1676 
1677 		if (status & VID_BC_MSK_OPC_ERR) {
1678 			dprintk(7, " (VID_BC_MSK_OPC_ERR 0x%08x)\n",
1679 				VID_BC_MSK_OPC_ERR);
1680 			printk(KERN_WARNING "%s: video risc op code error\n",
1681 				dev->name);
1682 			cx23885_sram_channel_dump(dev,
1683 				&dev->sram_channels[SRAM_CH01]);
1684 		}
1685 
1686 		if (status & VID_BC_MSK_SYNC)
1687 			dprintk(7, " (VID_BC_MSK_SYNC 0x%08x) "
1688 				"video lines miss-match\n",
1689 				VID_BC_MSK_SYNC);
1690 
1691 		if (status & VID_BC_MSK_OF)
1692 			dprintk(7, " (VID_BC_MSK_OF 0x%08x) fifo overflow\n",
1693 				VID_BC_MSK_OF);
1694 
1695 	}
1696 
1697 	/* Video */
1698 	if (status & VID_BC_MSK_RISCI1) {
1699 		spin_lock(&dev->slock);
1700 		count = cx_read(VID_A_GPCNT);
1701 		cx23885_video_wakeup(dev, &dev->vidq, count);
1702 		spin_unlock(&dev->slock);
1703 		handled++;
1704 	}
1705 	if (status & VID_BC_MSK_RISCI2) {
1706 		dprintk(2, "stopper video\n");
1707 		spin_lock(&dev->slock);
1708 		cx23885_restart_video_queue(dev, &dev->vidq);
1709 		spin_unlock(&dev->slock);
1710 		handled++;
1711 	}
1712 
1713 	/* Allow the VBI framework to process it's payload */
1714 	handled += cx23885_vbi_irq(dev, status);
1715 
1716 	return handled;
1717 }
1718 
1719 /* ----------------------------------------------------------- */
1720 /* exported stuff                                              */
1721 
1722 static const struct v4l2_file_operations video_fops = {
1723 	.owner	       = THIS_MODULE,
1724 	.open	       = video_open,
1725 	.release       = video_release,
1726 	.read	       = video_read,
1727 	.poll          = video_poll,
1728 	.mmap	       = video_mmap,
1729 	.ioctl	       = video_ioctl2,
1730 };
1731 
1732 static const struct v4l2_ioctl_ops video_ioctl_ops = {
1733 	.vidioc_querycap      = vidioc_querycap,
1734 	.vidioc_enum_fmt_vid_cap  = vidioc_enum_fmt_vid_cap,
1735 	.vidioc_g_fmt_vid_cap     = vidioc_g_fmt_vid_cap,
1736 	.vidioc_try_fmt_vid_cap   = vidioc_try_fmt_vid_cap,
1737 	.vidioc_s_fmt_vid_cap     = vidioc_s_fmt_vid_cap,
1738 	.vidioc_g_fmt_vbi_cap     = cx23885_vbi_fmt,
1739 	.vidioc_try_fmt_vbi_cap   = cx23885_vbi_fmt,
1740 	.vidioc_s_fmt_vbi_cap     = cx23885_vbi_fmt,
1741 	.vidioc_reqbufs       = vidioc_reqbufs,
1742 	.vidioc_querybuf      = vidioc_querybuf,
1743 	.vidioc_qbuf          = vidioc_qbuf,
1744 	.vidioc_dqbuf         = vidioc_dqbuf,
1745 	.vidioc_s_std         = vidioc_s_std,
1746 	.vidioc_g_std         = vidioc_g_std,
1747 	.vidioc_enum_input    = vidioc_enum_input,
1748 	.vidioc_g_input       = vidioc_g_input,
1749 	.vidioc_s_input       = vidioc_s_input,
1750 	.vidioc_log_status    = vidioc_log_status,
1751 	.vidioc_queryctrl     = vidioc_queryctrl,
1752 	.vidioc_g_ctrl        = vidioc_g_ctrl,
1753 	.vidioc_s_ctrl        = vidioc_s_ctrl,
1754 	.vidioc_streamon      = vidioc_streamon,
1755 	.vidioc_streamoff     = vidioc_streamoff,
1756 	.vidioc_g_tuner       = vidioc_g_tuner,
1757 	.vidioc_s_tuner       = vidioc_s_tuner,
1758 	.vidioc_g_frequency   = vidioc_g_frequency,
1759 	.vidioc_s_frequency   = vidioc_s_frequency,
1760 #ifdef CONFIG_VIDEO_ADV_DEBUG
1761 	.vidioc_g_chip_info   = cx23885_g_chip_info,
1762 	.vidioc_g_register    = cx23885_g_register,
1763 	.vidioc_s_register    = cx23885_s_register,
1764 #endif
1765 	.vidioc_enumaudio     = vidioc_enum_audinput,
1766 	.vidioc_g_audio       = vidioc_g_audinput,
1767 	.vidioc_s_audio       = vidioc_s_audinput,
1768 };
1769 
1770 static struct video_device cx23885_vbi_template;
1771 static struct video_device cx23885_video_template = {
1772 	.name                 = "cx23885-video",
1773 	.fops                 = &video_fops,
1774 	.ioctl_ops 	      = &video_ioctl_ops,
1775 	.tvnorms              = CX23885_NORMS,
1776 };
1777 
1778 static const struct v4l2_file_operations radio_fops = {
1779 	.owner         = THIS_MODULE,
1780 	.open          = video_open,
1781 	.release       = video_release,
1782 	.ioctl         = video_ioctl2,
1783 };
1784 
1785 
1786 void cx23885_video_unregister(struct cx23885_dev *dev)
1787 {
1788 	dprintk(1, "%s()\n", __func__);
1789 	cx23885_irq_remove(dev, 0x01);
1790 
1791 	if (dev->vbi_dev) {
1792 		if (video_is_registered(dev->vbi_dev))
1793 			video_unregister_device(dev->vbi_dev);
1794 		else
1795 			video_device_release(dev->vbi_dev);
1796 		dev->vbi_dev = NULL;
1797 		btcx_riscmem_free(dev->pci, &dev->vbiq.stopper);
1798 	}
1799 	if (dev->video_dev) {
1800 		if (video_is_registered(dev->video_dev))
1801 			video_unregister_device(dev->video_dev);
1802 		else
1803 			video_device_release(dev->video_dev);
1804 		dev->video_dev = NULL;
1805 
1806 		btcx_riscmem_free(dev->pci, &dev->vidq.stopper);
1807 	}
1808 
1809 	if (dev->audio_dev)
1810 		cx23885_audio_unregister(dev);
1811 }
1812 
1813 int cx23885_video_register(struct cx23885_dev *dev)
1814 {
1815 	int err;
1816 
1817 	dprintk(1, "%s()\n", __func__);
1818 	spin_lock_init(&dev->slock);
1819 
1820 	/* Initialize VBI template */
1821 	cx23885_vbi_template = cx23885_video_template;
1822 	strcpy(cx23885_vbi_template.name, "cx23885-vbi");
1823 
1824 	dev->tvnorm = V4L2_STD_NTSC_M;
1825 
1826 	/* init video dma queues */
1827 	INIT_LIST_HEAD(&dev->vidq.active);
1828 	INIT_LIST_HEAD(&dev->vidq.queued);
1829 	dev->vidq.timeout.function = cx23885_vid_timeout;
1830 	dev->vidq.timeout.data = (unsigned long)dev;
1831 	init_timer(&dev->vidq.timeout);
1832 	cx23885_risc_stopper(dev->pci, &dev->vidq.stopper,
1833 		VID_A_DMA_CTL, 0x11, 0x00);
1834 
1835 	/* init vbi dma queues */
1836 	INIT_LIST_HEAD(&dev->vbiq.active);
1837 	INIT_LIST_HEAD(&dev->vbiq.queued);
1838 	dev->vbiq.timeout.function = cx23885_vbi_timeout;
1839 	dev->vbiq.timeout.data = (unsigned long)dev;
1840 	init_timer(&dev->vbiq.timeout);
1841 	cx23885_risc_stopper(dev->pci, &dev->vbiq.stopper,
1842 		VID_A_DMA_CTL, 0x22, 0x00);
1843 
1844 	cx23885_irq_add_enable(dev, 0x01);
1845 
1846 	if ((TUNER_ABSENT != dev->tuner_type) &&
1847 			((dev->tuner_bus == 0) || (dev->tuner_bus == 1))) {
1848 		struct v4l2_subdev *sd = NULL;
1849 
1850 		if (dev->tuner_addr)
1851 			sd = v4l2_i2c_new_subdev(&dev->v4l2_dev,
1852 				&dev->i2c_bus[dev->tuner_bus].i2c_adap,
1853 				"tuner", dev->tuner_addr, NULL);
1854 		else
1855 			sd = v4l2_i2c_new_subdev(&dev->v4l2_dev,
1856 				&dev->i2c_bus[dev->tuner_bus].i2c_adap,
1857 				"tuner", 0, v4l2_i2c_tuner_addrs(ADDRS_TV));
1858 		if (sd) {
1859 			struct tuner_setup tun_setup;
1860 
1861 			memset(&tun_setup, 0, sizeof(tun_setup));
1862 			tun_setup.mode_mask = T_ANALOG_TV;
1863 			tun_setup.type = dev->tuner_type;
1864 			tun_setup.addr = v4l2_i2c_subdev_addr(sd);
1865 			tun_setup.tuner_callback = cx23885_tuner_callback;
1866 
1867 			v4l2_subdev_call(sd, tuner, s_type_addr, &tun_setup);
1868 
1869 			if ((dev->board == CX23885_BOARD_LEADTEK_WINFAST_PXTV1200) ||
1870 			    (dev->board == CX23885_BOARD_LEADTEK_WINFAST_PXPVR2200)) {
1871 				struct xc2028_ctrl ctrl = {
1872 					.fname = XC2028_DEFAULT_FIRMWARE,
1873 					.max_len = 64
1874 				};
1875 				struct v4l2_priv_tun_config cfg = {
1876 					.tuner = dev->tuner_type,
1877 					.priv = &ctrl
1878 				};
1879 				v4l2_subdev_call(sd, tuner, s_config, &cfg);
1880 			}
1881 
1882 			if (dev->board == CX23885_BOARD_AVERMEDIA_HC81R) {
1883 				struct xc2028_ctrl ctrl = {
1884 					.fname = "xc3028L-v36.fw",
1885 					.max_len = 64
1886 				};
1887 				struct v4l2_priv_tun_config cfg = {
1888 					.tuner = dev->tuner_type,
1889 					.priv = &ctrl
1890 				};
1891 				v4l2_subdev_call(sd, tuner, s_config, &cfg);
1892 			}
1893 		}
1894 	}
1895 
1896 	/* register Video device */
1897 	dev->video_dev = cx23885_vdev_init(dev, dev->pci,
1898 		&cx23885_video_template, "video");
1899 	err = video_register_device(dev->video_dev, VFL_TYPE_GRABBER,
1900 				    video_nr[dev->nr]);
1901 	if (err < 0) {
1902 		printk(KERN_INFO "%s: can't register video device\n",
1903 			dev->name);
1904 		goto fail_unreg;
1905 	}
1906 	printk(KERN_INFO "%s: registered device %s [v4l2]\n",
1907 	       dev->name, video_device_node_name(dev->video_dev));
1908 
1909 	/* register VBI device */
1910 	dev->vbi_dev = cx23885_vdev_init(dev, dev->pci,
1911 		&cx23885_vbi_template, "vbi");
1912 	err = video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
1913 				    vbi_nr[dev->nr]);
1914 	if (err < 0) {
1915 		printk(KERN_INFO "%s: can't register vbi device\n",
1916 			dev->name);
1917 		goto fail_unreg;
1918 	}
1919 	printk(KERN_INFO "%s: registered device %s\n",
1920 	       dev->name, video_device_node_name(dev->vbi_dev));
1921 
1922 	/* Register ALSA audio device */
1923 	dev->audio_dev = cx23885_audio_register(dev);
1924 
1925 	/* initial device configuration */
1926 	mutex_lock(&dev->lock);
1927 	cx23885_set_tvnorm(dev, dev->tvnorm);
1928 	init_controls(dev);
1929 	cx23885_video_mux(dev, 0);
1930 	cx23885_audio_mux(dev, 0);
1931 	mutex_unlock(&dev->lock);
1932 
1933 	return 0;
1934 
1935 fail_unreg:
1936 	cx23885_video_unregister(dev);
1937 	return err;
1938 }
1939 
1940