cx23885-video.c revision 22b4e64f0a119e94090ef45285a5c311f1f6855f
1/*
2 *  Driver for the Conexant CX23885 PCIe bridge
3 *
4 *  Copyright (c) 2007 Steven Toth <stoth@hauppauge.com>
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 <media/v4l2-common.h>
36
37#ifdef CONFIG_VIDEO_V4L1_COMPAT
38/* Include V4L1 specific functions. Should be removed soon */
39#include <linux/videodev.h>
40#endif
41
42MODULE_DESCRIPTION("v4l2 driver module for cx23885 based TV cards");
43MODULE_AUTHOR("Steven Toth <stoth@hauppauge.com>");
44MODULE_LICENSE("GPL");
45
46/* ------------------------------------------------------------------ */
47
48static unsigned int video_nr[] = {[0 ... (CX23885_MAXBOARDS - 1)] = UNSET };
49static unsigned int vbi_nr[]   = {[0 ... (CX23885_MAXBOARDS - 1)] = UNSET };
50static unsigned int radio_nr[] = {[0 ... (CX23885_MAXBOARDS - 1)] = UNSET };
51
52module_param_array(video_nr, int, NULL, 0444);
53module_param_array(vbi_nr,   int, NULL, 0444);
54module_param_array(radio_nr, int, NULL, 0444);
55
56MODULE_PARM_DESC(video_nr, "video device numbers");
57MODULE_PARM_DESC(vbi_nr, "vbi device numbers");
58MODULE_PARM_DESC(radio_nr, "radio device numbers");
59
60static unsigned int video_debug;
61module_param(video_debug, int, 0644);
62MODULE_PARM_DESC(video_debug, "enable debug messages [video]");
63
64static unsigned int irq_debug;
65module_param(irq_debug, int, 0644);
66MODULE_PARM_DESC(irq_debug, "enable debug messages [IRQ handler]");
67
68static unsigned int vid_limit = 16;
69module_param(vid_limit, int, 0644);
70MODULE_PARM_DESC(vid_limit, "capture memory limit in megabytes");
71
72#define dprintk(level, fmt, arg...)\
73	do { if (video_debug >= level)\
74		printk(KERN_DEBUG "%s/0: " fmt, dev->name, ## arg);\
75	} while (0)
76
77/* ------------------------------------------------------------------- */
78/* static data                                                         */
79
80#define FORMAT_FLAGS_PACKED       0x01
81
82static struct cx23885_fmt formats[] = {
83	{
84		.name     = "8 bpp, gray",
85		.fourcc   = V4L2_PIX_FMT_GREY,
86		.depth    = 8,
87		.flags    = FORMAT_FLAGS_PACKED,
88	}, {
89		.name     = "15 bpp RGB, le",
90		.fourcc   = V4L2_PIX_FMT_RGB555,
91		.depth    = 16,
92		.flags    = FORMAT_FLAGS_PACKED,
93	}, {
94		.name     = "15 bpp RGB, be",
95		.fourcc   = V4L2_PIX_FMT_RGB555X,
96		.depth    = 16,
97		.flags    = FORMAT_FLAGS_PACKED,
98	}, {
99		.name     = "16 bpp RGB, le",
100		.fourcc   = V4L2_PIX_FMT_RGB565,
101		.depth    = 16,
102		.flags    = FORMAT_FLAGS_PACKED,
103	}, {
104		.name     = "16 bpp RGB, be",
105		.fourcc   = V4L2_PIX_FMT_RGB565X,
106		.depth    = 16,
107		.flags    = FORMAT_FLAGS_PACKED,
108	}, {
109		.name     = "24 bpp RGB, le",
110		.fourcc   = V4L2_PIX_FMT_BGR24,
111		.depth    = 24,
112		.flags    = FORMAT_FLAGS_PACKED,
113	}, {
114		.name     = "32 bpp RGB, le",
115		.fourcc   = V4L2_PIX_FMT_BGR32,
116		.depth    = 32,
117		.flags    = FORMAT_FLAGS_PACKED,
118	}, {
119		.name     = "32 bpp RGB, be",
120		.fourcc   = V4L2_PIX_FMT_RGB32,
121		.depth    = 32,
122		.flags    = FORMAT_FLAGS_PACKED,
123	}, {
124		.name     = "4:2:2, packed, YUYV",
125		.fourcc   = V4L2_PIX_FMT_YUYV,
126		.depth    = 16,
127		.flags    = FORMAT_FLAGS_PACKED,
128	}, {
129		.name     = "4:2:2, packed, UYVY",
130		.fourcc   = V4L2_PIX_FMT_UYVY,
131		.depth    = 16,
132		.flags    = FORMAT_FLAGS_PACKED,
133	},
134};
135
136static struct cx23885_fmt *format_by_fourcc(unsigned int fourcc)
137{
138	unsigned int i;
139
140	for (i = 0; i < ARRAY_SIZE(formats); i++)
141		if (formats[i].fourcc == fourcc)
142			return formats+i;
143
144	printk(KERN_ERR "%s(0x%08x) NOT FOUND\n", __func__, fourcc);
145	return NULL;
146}
147
148/* ------------------------------------------------------------------- */
149
150static const struct v4l2_queryctrl no_ctl = {
151	.name  = "42",
152	.flags = V4L2_CTRL_FLAG_DISABLED,
153};
154
155static struct cx23885_ctrl cx23885_ctls[] = {
156	/* --- video --- */
157	{
158		.v = {
159			.id            = V4L2_CID_BRIGHTNESS,
160			.name          = "Brightness",
161			.minimum       = 0x00,
162			.maximum       = 0xff,
163			.step          = 1,
164			.default_value = 0x7f,
165			.type          = V4L2_CTRL_TYPE_INTEGER,
166		},
167		.off                   = 128,
168		.reg                   = LUMA_CTRL,
169		.mask                  = 0x00ff,
170		.shift                 = 0,
171	}, {
172		.v = {
173			.id            = V4L2_CID_CONTRAST,
174			.name          = "Contrast",
175			.minimum       = 0,
176			.maximum       = 0xff,
177			.step          = 1,
178			.default_value = 0x3f,
179			.type          = V4L2_CTRL_TYPE_INTEGER,
180		},
181		.off                   = 0,
182		.reg                   = LUMA_CTRL,
183		.mask                  = 0xff00,
184		.shift                 = 8,
185	}, {
186		.v = {
187			.id            = V4L2_CID_HUE,
188			.name          = "Hue",
189			.minimum       = 0,
190			.maximum       = 0xff,
191			.step          = 1,
192			.default_value = 0x7f,
193			.type          = V4L2_CTRL_TYPE_INTEGER,
194		},
195		.off                   = 128,
196		.reg                   = CHROMA_CTRL,
197		.mask                  = 0xff0000,
198		.shift                 = 16,
199	}, {
200		/* strictly, this only describes only U saturation.
201		 * V saturation is handled specially through code.
202		 */
203		.v = {
204			.id            = V4L2_CID_SATURATION,
205			.name          = "Saturation",
206			.minimum       = 0,
207			.maximum       = 0xff,
208			.step          = 1,
209			.default_value = 0x7f,
210			.type          = V4L2_CTRL_TYPE_INTEGER,
211		},
212		.off                   = 0,
213		.reg                   = CHROMA_CTRL,
214		.mask                  = 0x00ff,
215		.shift                 = 0,
216	}, {
217	/* --- audio --- */
218		.v = {
219			.id            = V4L2_CID_AUDIO_MUTE,
220			.name          = "Mute",
221			.minimum       = 0,
222			.maximum       = 1,
223			.default_value = 1,
224			.type          = V4L2_CTRL_TYPE_BOOLEAN,
225		},
226		.reg                   = PATH1_CTL1,
227		.mask                  = (0x1f << 24),
228		.shift                 = 24,
229	}, {
230		.v = {
231			.id            = V4L2_CID_AUDIO_VOLUME,
232			.name          = "Volume",
233			.minimum       = 0,
234			.maximum       = 0x3f,
235			.step          = 1,
236			.default_value = 0x3f,
237			.type          = V4L2_CTRL_TYPE_INTEGER,
238		},
239		.reg                   = PATH1_VOL_CTL,
240		.mask                  = 0xff,
241		.shift                 = 0,
242	}
243};
244static const int CX23885_CTLS = ARRAY_SIZE(cx23885_ctls);
245
246const u32 cx23885_user_ctrls[] = {
247	V4L2_CID_USER_CLASS,
248	V4L2_CID_BRIGHTNESS,
249	V4L2_CID_CONTRAST,
250	V4L2_CID_SATURATION,
251	V4L2_CID_HUE,
252	V4L2_CID_AUDIO_VOLUME,
253	V4L2_CID_AUDIO_MUTE,
254	0
255};
256EXPORT_SYMBOL(cx23885_user_ctrls);
257
258static const u32 *ctrl_classes[] = {
259	cx23885_user_ctrls,
260	NULL
261};
262
263void cx23885_video_wakeup(struct cx23885_dev *dev,
264		 struct cx23885_dmaqueue *q, u32 count)
265{
266	struct cx23885_buffer *buf;
267	int bc;
268
269	for (bc = 0;; bc++) {
270		if (list_empty(&q->active))
271			break;
272		buf = list_entry(q->active.next,
273				 struct cx23885_buffer, vb.queue);
274
275		/* count comes from the hw and is is 16bit wide --
276		 * this trick handles wrap-arounds correctly for
277		 * up to 32767 buffers in flight... */
278		if ((s16) (count - buf->count) < 0)
279			break;
280
281		do_gettimeofday(&buf->vb.ts);
282		dprintk(2, "[%p/%d] wakeup reg=%d buf=%d\n", buf, buf->vb.i,
283			count, buf->count);
284		buf->vb.state = VIDEOBUF_DONE;
285		list_del(&buf->vb.queue);
286		wake_up(&buf->vb.done);
287	}
288	if (list_empty(&q->active)) {
289		del_timer(&q->timeout);
290	} else {
291		mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
292	}
293	if (bc != 1)
294		printk(KERN_ERR "%s: %d buffers handled (should be 1)\n",
295			__func__, bc);
296}
297
298int cx23885_set_tvnorm(struct cx23885_dev *dev, v4l2_std_id norm)
299{
300	dprintk(1, "%s(norm = 0x%08x) name: [%s]\n",
301		__func__,
302		(unsigned int)norm,
303		v4l2_norm_to_name(norm));
304
305	dev->tvnorm = norm;
306
307	/* Tell the analog tuner/demods */
308	cx23885_call_i2c_clients(&dev->i2c_bus[1], VIDIOC_S_STD, &norm);
309
310	/* Tell the internal A/V decoder */
311	cx23885_call_i2c_clients(&dev->i2c_bus[2], VIDIOC_S_STD, &norm);
312
313	return 0;
314}
315
316struct video_device *cx23885_vdev_init(struct cx23885_dev *dev,
317				    struct pci_dev *pci,
318				    struct video_device *template,
319				    char *type)
320{
321	struct video_device *vfd;
322	dprintk(1, "%s()\n", __func__);
323
324	vfd = video_device_alloc();
325	if (NULL == vfd)
326		return NULL;
327	*vfd = *template;
328	vfd->minor   = -1;
329	vfd->dev     = &pci->dev;
330	vfd->release = video_device_release;
331	snprintf(vfd->name, sizeof(vfd->name), "%s %s (%s)",
332		 dev->name, type, cx23885_boards[dev->board].name);
333	return vfd;
334}
335
336int cx23885_ctrl_query(struct v4l2_queryctrl *qctrl)
337{
338	int i;
339
340	if (qctrl->id < V4L2_CID_BASE ||
341	    qctrl->id >= V4L2_CID_LASTP1)
342		return -EINVAL;
343	for (i = 0; i < CX23885_CTLS; i++)
344		if (cx23885_ctls[i].v.id == qctrl->id)
345			break;
346	if (i == CX23885_CTLS) {
347		*qctrl = no_ctl;
348		return 0;
349	}
350	*qctrl = cx23885_ctls[i].v;
351	return 0;
352}
353EXPORT_SYMBOL(cx23885_ctrl_query);
354
355/* ------------------------------------------------------------------- */
356/* resource management                                                 */
357
358static int res_get(struct cx23885_dev *dev, struct cx23885_fh *fh,
359	unsigned int bit)
360{
361	dprintk(1, "%s()\n", __func__);
362	if (fh->resources & bit)
363		/* have it already allocated */
364		return 1;
365
366	/* is it free? */
367	mutex_lock(&dev->lock);
368	if (dev->resources & bit) {
369		/* no, someone else uses it */
370		mutex_unlock(&dev->lock);
371		return 0;
372	}
373	/* it's free, grab it */
374	fh->resources  |= bit;
375	dev->resources |= bit;
376	dprintk(1, "res: get %d\n", bit);
377	mutex_unlock(&dev->lock);
378	return 1;
379}
380
381static int res_check(struct cx23885_fh *fh, unsigned int bit)
382{
383	return (fh->resources & bit);
384}
385
386static int res_locked(struct cx23885_dev *dev, unsigned int bit)
387{
388	return (dev->resources & bit);
389}
390
391static void res_free(struct cx23885_dev *dev, struct cx23885_fh *fh,
392	unsigned int bits)
393{
394	BUG_ON((fh->resources & bits) != bits);
395	dprintk(1, "%s()\n", __func__);
396
397	mutex_lock(&dev->lock);
398	fh->resources  &= ~bits;
399	dev->resources &= ~bits;
400	dprintk(1, "res: put %d\n", bits);
401	mutex_unlock(&dev->lock);
402}
403
404int cx23885_video_mux(struct cx23885_dev *dev, unsigned int input)
405{
406	struct v4l2_routing route;
407	memset(&route, 0, sizeof(route));
408
409	dprintk(1, "%s() video_mux: %d [vmux=%d, gpio=0x%x,0x%x,0x%x,0x%x]\n",
410		__func__,
411		input, INPUT(input)->vmux,
412		INPUT(input)->gpio0, INPUT(input)->gpio1,
413		INPUT(input)->gpio2, INPUT(input)->gpio3);
414	dev->input = input;
415
416	route.input = INPUT(input)->vmux;
417
418	/* Tell the internal A/V decoder */
419	cx23885_call_i2c_clients(&dev->i2c_bus[2],
420		VIDIOC_INT_S_VIDEO_ROUTING, &route);
421
422	return 0;
423}
424EXPORT_SYMBOL(cx23885_video_mux);
425
426/* ------------------------------------------------------------------ */
427int cx23885_set_scale(struct cx23885_dev *dev, unsigned int width,
428	unsigned int height, enum v4l2_field field)
429{
430	dprintk(1, "%s()\n", __func__);
431	return 0;
432}
433
434static int cx23885_start_video_dma(struct cx23885_dev *dev,
435			   struct cx23885_dmaqueue *q,
436			   struct cx23885_buffer *buf)
437{
438	dprintk(1, "%s()\n", __func__);
439
440	/* setup fifo + format */
441	cx23885_sram_channel_setup(dev, &dev->sram_channels[SRAM_CH01],
442				buf->bpl, buf->risc.dma);
443	cx23885_set_scale(dev, buf->vb.width, buf->vb.height, buf->vb.field);
444
445	/* reset counter */
446	cx_write(VID_A_GPCNT_CTL, 3);
447	q->count = 1;
448
449	/* enable irq */
450	cx_set(PCI_INT_MSK, cx_read(PCI_INT_MSK) | 0x01);
451	cx_set(VID_A_INT_MSK, 0x000011);
452
453	/* start dma */
454	cx_set(DEV_CNTRL2, (1<<5));
455	cx_set(VID_A_DMA_CTL, 0x11); /* FIFO and RISC enable */
456
457	return 0;
458}
459
460
461static int cx23885_restart_video_queue(struct cx23885_dev *dev,
462			       struct cx23885_dmaqueue *q)
463{
464	struct cx23885_buffer *buf, *prev;
465	struct list_head *item;
466	dprintk(1, "%s()\n", __func__);
467
468	if (!list_empty(&q->active)) {
469		buf = list_entry(q->active.next, struct cx23885_buffer,
470			vb.queue);
471		dprintk(2, "restart_queue [%p/%d]: restart dma\n",
472			buf, buf->vb.i);
473		cx23885_start_video_dma(dev, q, buf);
474		list_for_each(item, &q->active) {
475			buf = list_entry(item, struct cx23885_buffer,
476				vb.queue);
477			buf->count    = q->count++;
478		}
479		mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
480		return 0;
481	}
482
483	prev = NULL;
484	for (;;) {
485		if (list_empty(&q->queued))
486			return 0;
487		buf = list_entry(q->queued.next, struct cx23885_buffer,
488			vb.queue);
489		if (NULL == prev) {
490			list_move_tail(&buf->vb.queue, &q->active);
491			cx23885_start_video_dma(dev, q, buf);
492			buf->vb.state = VIDEOBUF_ACTIVE;
493			buf->count    = q->count++;
494			mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
495			dprintk(2, "[%p/%d] restart_queue - first active\n",
496				buf, buf->vb.i);
497
498		} else if (prev->vb.width  == buf->vb.width  &&
499			   prev->vb.height == buf->vb.height &&
500			   prev->fmt       == buf->fmt) {
501			list_move_tail(&buf->vb.queue, &q->active);
502			buf->vb.state = VIDEOBUF_ACTIVE;
503			buf->count    = q->count++;
504			prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
505			prev->risc.jmp[2] = cpu_to_le32(0); /* Bits 63 - 32 */
506			dprintk(2, "[%p/%d] restart_queue - move to active\n",
507				buf, buf->vb.i);
508		} else {
509			return 0;
510		}
511		prev = buf;
512	}
513}
514
515static int buffer_setup(struct videobuf_queue *q, unsigned int *count,
516	unsigned int *size)
517{
518	struct cx23885_fh *fh = q->priv_data;
519
520	*size = fh->fmt->depth*fh->width*fh->height >> 3;
521	if (0 == *count)
522		*count = 32;
523	while (*size * *count > vid_limit * 1024 * 1024)
524		(*count)--;
525	return 0;
526}
527
528static int buffer_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
529	       enum v4l2_field field)
530{
531	struct cx23885_fh *fh  = q->priv_data;
532	struct cx23885_dev *dev = fh->dev;
533	struct cx23885_buffer *buf =
534		container_of(vb, struct cx23885_buffer, vb);
535	int rc, init_buffer = 0;
536	u32 line0_offset, line1_offset;
537	struct videobuf_dmabuf *dma = videobuf_to_dma(&buf->vb);
538
539	BUG_ON(NULL == fh->fmt);
540	if (fh->width  < 48 || fh->width  > norm_maxw(dev->tvnorm) ||
541	    fh->height < 32 || fh->height > norm_maxh(dev->tvnorm))
542		return -EINVAL;
543	buf->vb.size = (fh->width * fh->height * fh->fmt->depth) >> 3;
544	if (0 != buf->vb.baddr  &&  buf->vb.bsize < buf->vb.size)
545		return -EINVAL;
546
547	if (buf->fmt       != fh->fmt    ||
548	    buf->vb.width  != fh->width  ||
549	    buf->vb.height != fh->height ||
550	    buf->vb.field  != field) {
551		buf->fmt       = fh->fmt;
552		buf->vb.width  = fh->width;
553		buf->vb.height = fh->height;
554		buf->vb.field  = field;
555		init_buffer = 1;
556	}
557
558	if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
559		init_buffer = 1;
560		rc = videobuf_iolock(q, &buf->vb, NULL);
561		if (0 != rc)
562			goto fail;
563	}
564
565	if (init_buffer) {
566		buf->bpl = buf->vb.width * buf->fmt->depth >> 3;
567		switch (buf->vb.field) {
568		case V4L2_FIELD_TOP:
569			cx23885_risc_buffer(dev->pci, &buf->risc,
570					 dma->sglist, 0, UNSET,
571					 buf->bpl, 0, buf->vb.height);
572			break;
573		case V4L2_FIELD_BOTTOM:
574			cx23885_risc_buffer(dev->pci, &buf->risc,
575					 dma->sglist, UNSET, 0,
576					 buf->bpl, 0, buf->vb.height);
577			break;
578		case V4L2_FIELD_INTERLACED:
579			if (dev->tvnorm & V4L2_STD_NTSC) {
580				/* cx25840 transmits NTSC bottom field first */
581				dprintk(1, "%s() Creating NTSC risc\n",
582					__func__);
583				line0_offset = buf->bpl;
584				line1_offset = 0;
585			} else {
586				/* All other formats are top field first */
587				dprintk(1, "%s() Creating PAL/SECAM risc\n",
588					__func__);
589				line0_offset = 0;
590				line1_offset = buf->bpl;
591			}
592			cx23885_risc_buffer(dev->pci, &buf->risc,
593					dma->sglist, line0_offset,
594					line1_offset,
595					buf->bpl, buf->bpl,
596					buf->vb.height >> 1);
597			break;
598		case V4L2_FIELD_SEQ_TB:
599			cx23885_risc_buffer(dev->pci, &buf->risc,
600					 dma->sglist,
601					 0, buf->bpl * (buf->vb.height >> 1),
602					 buf->bpl, 0,
603					 buf->vb.height >> 1);
604			break;
605		case V4L2_FIELD_SEQ_BT:
606			cx23885_risc_buffer(dev->pci, &buf->risc,
607					 dma->sglist,
608					 buf->bpl * (buf->vb.height >> 1), 0,
609					 buf->bpl, 0,
610					 buf->vb.height >> 1);
611			break;
612		default:
613			BUG();
614		}
615	}
616	dprintk(2, "[%p/%d] buffer_prep - %dx%d %dbpp \"%s\" - dma=0x%08lx\n",
617		buf, buf->vb.i,
618		fh->width, fh->height, fh->fmt->depth, fh->fmt->name,
619		(unsigned long)buf->risc.dma);
620
621	buf->vb.state = VIDEOBUF_PREPARED;
622	return 0;
623
624 fail:
625	cx23885_free_buffer(q, buf);
626	return rc;
627}
628
629static void buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
630{
631	struct cx23885_buffer   *buf = container_of(vb,
632		struct cx23885_buffer, vb);
633	struct cx23885_buffer   *prev;
634	struct cx23885_fh       *fh   = vq->priv_data;
635	struct cx23885_dev      *dev  = fh->dev;
636	struct cx23885_dmaqueue *q    = &dev->vidq;
637
638	/* add jump to stopper */
639	buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_IRQ1 | RISC_CNT_INC);
640	buf->risc.jmp[1] = cpu_to_le32(q->stopper.dma);
641	buf->risc.jmp[2] = cpu_to_le32(0); /* bits 63-32 */
642
643	if (!list_empty(&q->queued)) {
644		list_add_tail(&buf->vb.queue, &q->queued);
645		buf->vb.state = VIDEOBUF_QUEUED;
646		dprintk(2, "[%p/%d] buffer_queue - append to queued\n",
647			buf, buf->vb.i);
648
649	} else if (list_empty(&q->active)) {
650		list_add_tail(&buf->vb.queue, &q->active);
651		cx23885_start_video_dma(dev, q, buf);
652		buf->vb.state = VIDEOBUF_ACTIVE;
653		buf->count    = q->count++;
654		mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
655		dprintk(2, "[%p/%d] buffer_queue - first active\n",
656			buf, buf->vb.i);
657
658	} else {
659		prev = list_entry(q->active.prev, struct cx23885_buffer,
660			vb.queue);
661		if (prev->vb.width  == buf->vb.width  &&
662		    prev->vb.height == buf->vb.height &&
663		    prev->fmt       == buf->fmt) {
664			list_add_tail(&buf->vb.queue, &q->active);
665			buf->vb.state = VIDEOBUF_ACTIVE;
666			buf->count    = q->count++;
667			prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
668			/* 64 bit bits 63-32 */
669			prev->risc.jmp[2] = cpu_to_le32(0);
670			dprintk(2, "[%p/%d] buffer_queue - append to active\n",
671				buf, buf->vb.i);
672
673		} else {
674			list_add_tail(&buf->vb.queue, &q->queued);
675			buf->vb.state = VIDEOBUF_QUEUED;
676			dprintk(2, "[%p/%d] buffer_queue - first queued\n",
677				buf, buf->vb.i);
678		}
679	}
680}
681
682static void buffer_release(struct videobuf_queue *q,
683	struct videobuf_buffer *vb)
684{
685	struct cx23885_buffer *buf = container_of(vb,
686		struct cx23885_buffer, vb);
687
688	cx23885_free_buffer(q, buf);
689}
690
691static struct videobuf_queue_ops cx23885_video_qops = {
692	.buf_setup    = buffer_setup,
693	.buf_prepare  = buffer_prepare,
694	.buf_queue    = buffer_queue,
695	.buf_release  = buffer_release,
696};
697
698static struct videobuf_queue *get_queue(struct cx23885_fh *fh)
699{
700	switch (fh->type) {
701	case V4L2_BUF_TYPE_VIDEO_CAPTURE:
702		return &fh->vidq;
703	case V4L2_BUF_TYPE_VBI_CAPTURE:
704		return &fh->vbiq;
705	default:
706		BUG();
707		return NULL;
708	}
709}
710
711static int get_resource(struct cx23885_fh *fh)
712{
713	switch (fh->type) {
714	case V4L2_BUF_TYPE_VIDEO_CAPTURE:
715		return RESOURCE_VIDEO;
716	case V4L2_BUF_TYPE_VBI_CAPTURE:
717		return RESOURCE_VBI;
718	default:
719		BUG();
720		return 0;
721	}
722}
723
724static int video_open(struct inode *inode, struct file *file)
725{
726	int minor = iminor(inode);
727	struct cx23885_dev *h, *dev = NULL;
728	struct cx23885_fh *fh;
729	struct list_head *list;
730	enum v4l2_buf_type type = 0;
731	int radio = 0;
732
733	list_for_each(list, &cx23885_devlist) {
734		h = list_entry(list, struct cx23885_dev, devlist);
735		if (h->video_dev->minor == minor) {
736			dev  = h;
737			type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
738		}
739		if (h->vbi_dev &&
740		   h->vbi_dev->minor == minor) {
741			dev  = h;
742			type = V4L2_BUF_TYPE_VBI_CAPTURE;
743		}
744		if (h->radio_dev &&
745		    h->radio_dev->minor == minor) {
746			radio = 1;
747			dev   = h;
748		}
749	}
750	if (NULL == dev)
751		return -ENODEV;
752
753	dprintk(1, "open minor=%d radio=%d type=%s\n",
754		minor, radio, v4l2_type_names[type]);
755
756	/* allocate + initialize per filehandle data */
757	fh = kzalloc(sizeof(*fh), GFP_KERNEL);
758	if (NULL == fh)
759		return -ENOMEM;
760	file->private_data = fh;
761	fh->dev      = dev;
762	fh->radio    = radio;
763	fh->type     = type;
764	fh->width    = 320;
765	fh->height   = 240;
766	fh->fmt      = format_by_fourcc(V4L2_PIX_FMT_BGR24);
767
768	videobuf_queue_sg_init(&fh->vidq, &cx23885_video_qops,
769			    &dev->pci->dev, &dev->slock,
770			    V4L2_BUF_TYPE_VIDEO_CAPTURE,
771			    V4L2_FIELD_INTERLACED,
772			    sizeof(struct cx23885_buffer),
773			    fh);
774
775	dprintk(1, "post videobuf_queue_init()\n");
776
777
778	return 0;
779}
780
781static ssize_t video_read(struct file *file, char __user *data,
782	size_t count, loff_t *ppos)
783{
784	struct cx23885_fh *fh = file->private_data;
785
786	switch (fh->type) {
787	case V4L2_BUF_TYPE_VIDEO_CAPTURE:
788		if (res_locked(fh->dev, RESOURCE_VIDEO))
789			return -EBUSY;
790		return videobuf_read_one(&fh->vidq, data, count, ppos,
791					 file->f_flags & O_NONBLOCK);
792	case V4L2_BUF_TYPE_VBI_CAPTURE:
793		if (!res_get(fh->dev, fh, RESOURCE_VBI))
794			return -EBUSY;
795		return videobuf_read_stream(&fh->vbiq, data, count, ppos, 1,
796					    file->f_flags & O_NONBLOCK);
797	default:
798		BUG();
799		return 0;
800	}
801}
802
803static unsigned int video_poll(struct file *file,
804	struct poll_table_struct *wait)
805{
806	struct cx23885_fh *fh = file->private_data;
807	struct cx23885_buffer *buf;
808
809	if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type) {
810		if (!res_get(fh->dev, fh, RESOURCE_VBI))
811			return POLLERR;
812		return videobuf_poll_stream(file, &fh->vbiq, wait);
813	}
814
815	if (res_check(fh, RESOURCE_VIDEO)) {
816		/* streaming capture */
817		if (list_empty(&fh->vidq.stream))
818			return POLLERR;
819		buf = list_entry(fh->vidq.stream.next,
820			struct cx23885_buffer, vb.stream);
821	} else {
822		/* read() capture */
823		buf = (struct cx23885_buffer *)fh->vidq.read_buf;
824		if (NULL == buf)
825			return POLLERR;
826	}
827	poll_wait(file, &buf->vb.done, wait);
828	if (buf->vb.state == VIDEOBUF_DONE ||
829	    buf->vb.state == VIDEOBUF_ERROR)
830		return POLLIN|POLLRDNORM;
831	return 0;
832}
833
834static int video_release(struct inode *inode, struct file *file)
835{
836	struct cx23885_fh *fh = file->private_data;
837	struct cx23885_dev *dev = fh->dev;
838
839	/* turn off overlay */
840	if (res_check(fh, RESOURCE_OVERLAY)) {
841		/* FIXME */
842		res_free(dev, fh, RESOURCE_OVERLAY);
843	}
844
845	/* stop video capture */
846	if (res_check(fh, RESOURCE_VIDEO)) {
847		videobuf_queue_cancel(&fh->vidq);
848		res_free(dev, fh, RESOURCE_VIDEO);
849	}
850	if (fh->vidq.read_buf) {
851		buffer_release(&fh->vidq, fh->vidq.read_buf);
852		kfree(fh->vidq.read_buf);
853	}
854
855	/* stop vbi capture */
856	if (res_check(fh, RESOURCE_VBI)) {
857		if (fh->vbiq.streaming)
858			videobuf_streamoff(&fh->vbiq);
859		if (fh->vbiq.reading)
860			videobuf_read_stop(&fh->vbiq);
861		res_free(dev, fh, RESOURCE_VBI);
862	}
863
864	videobuf_mmap_free(&fh->vidq);
865	file->private_data = NULL;
866	kfree(fh);
867
868	/* We are not putting the tuner to sleep here on exit, because
869	 * we want to use the mpeg encoder in another session to capture
870	 * tuner video. Closing this will result in no video to the encoder.
871	 */
872
873	return 0;
874}
875
876static int video_mmap(struct file *file, struct vm_area_struct *vma)
877{
878	struct cx23885_fh *fh = file->private_data;
879
880	return videobuf_mmap_mapper(get_queue(fh), vma);
881}
882
883/* ------------------------------------------------------------------ */
884/* VIDEO CTRL IOCTLS                                                  */
885
886int cx23885_get_control(struct cx23885_dev *dev, struct v4l2_control *ctl)
887{
888	dprintk(1, "%s() calling cx25840(VIDIOC_G_CTRL)\n", __func__);
889	cx23885_call_i2c_clients(&dev->i2c_bus[2], VIDIOC_G_CTRL, ctl);
890	return 0;
891}
892EXPORT_SYMBOL(cx23885_get_control);
893
894int cx23885_set_control(struct cx23885_dev *dev, struct v4l2_control *ctl)
895{
896	dprintk(1, "%s() calling cx25840(VIDIOC_S_CTRL)"
897		" (disabled - no action)\n", __func__);
898	return 0;
899}
900EXPORT_SYMBOL(cx23885_set_control);
901
902static void init_controls(struct cx23885_dev *dev)
903{
904	struct v4l2_control ctrl;
905	int i;
906
907	for (i = 0; i < CX23885_CTLS; i++) {
908		ctrl.id = cx23885_ctls[i].v.id;
909		ctrl.value = cx23885_ctls[i].v.default_value;
910
911		cx23885_set_control(dev, &ctrl);
912	}
913}
914
915/* ------------------------------------------------------------------ */
916/* VIDEO IOCTLS                                                       */
917
918static int vidioc_g_fmt_cap(struct file *file, void *priv,
919	struct v4l2_format *f)
920{
921	struct cx23885_fh *fh   = priv;
922
923	f->fmt.pix.width        = fh->width;
924	f->fmt.pix.height       = fh->height;
925	f->fmt.pix.field        = fh->vidq.field;
926	f->fmt.pix.pixelformat  = fh->fmt->fourcc;
927	f->fmt.pix.bytesperline =
928		(f->fmt.pix.width * fh->fmt->depth) >> 3;
929	f->fmt.pix.sizeimage =
930		f->fmt.pix.height * f->fmt.pix.bytesperline;
931
932	return 0;
933}
934
935static int vidioc_try_fmt_cap(struct file *file, void *priv,
936	struct v4l2_format *f)
937{
938	struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
939	struct cx23885_fmt *fmt;
940	enum v4l2_field   field;
941	unsigned int      maxw, maxh;
942
943	fmt = format_by_fourcc(f->fmt.pix.pixelformat);
944	if (NULL == fmt)
945		return -EINVAL;
946
947	field = f->fmt.pix.field;
948	maxw  = norm_maxw(dev->tvnorm);
949	maxh  = norm_maxh(dev->tvnorm);
950
951	if (V4L2_FIELD_ANY == field) {
952		field = (f->fmt.pix.height > maxh/2)
953			? V4L2_FIELD_INTERLACED
954			: V4L2_FIELD_BOTTOM;
955	}
956
957	switch (field) {
958	case V4L2_FIELD_TOP:
959	case V4L2_FIELD_BOTTOM:
960		maxh = maxh / 2;
961		break;
962	case V4L2_FIELD_INTERLACED:
963		break;
964	default:
965		return -EINVAL;
966	}
967
968	f->fmt.pix.field = field;
969	if (f->fmt.pix.height < 32)
970		f->fmt.pix.height = 32;
971	if (f->fmt.pix.height > maxh)
972		f->fmt.pix.height = maxh;
973	if (f->fmt.pix.width < 48)
974		f->fmt.pix.width = 48;
975	if (f->fmt.pix.width > maxw)
976		f->fmt.pix.width = maxw;
977	f->fmt.pix.width &= ~0x03;
978	f->fmt.pix.bytesperline =
979		(f->fmt.pix.width * fmt->depth) >> 3;
980	f->fmt.pix.sizeimage =
981		f->fmt.pix.height * f->fmt.pix.bytesperline;
982
983	return 0;
984}
985
986static int vidioc_s_fmt_cap(struct file *file, void *priv,
987	struct v4l2_format *f)
988{
989	struct cx23885_fh *fh = priv;
990	struct cx23885_dev *dev  = ((struct cx23885_fh *)priv)->dev;
991	int err;
992
993	dprintk(2, "%s()\n", __func__);
994	err = vidioc_try_fmt_cap(file, priv, f);
995
996	if (0 != err)
997		return err;
998	fh->fmt        = format_by_fourcc(f->fmt.pix.pixelformat);
999	fh->width      = f->fmt.pix.width;
1000	fh->height     = f->fmt.pix.height;
1001	fh->vidq.field = f->fmt.pix.field;
1002	dprintk(2, "%s() width=%d height=%d field=%d\n", __func__,
1003		fh->width, fh->height, fh->vidq.field);
1004	cx23885_call_i2c_clients(&dev->i2c_bus[2], VIDIOC_S_FMT, f);
1005	return 0;
1006}
1007
1008static int vidioc_querycap(struct file *file, void  *priv,
1009	struct v4l2_capability *cap)
1010{
1011	struct cx23885_dev *dev  = ((struct cx23885_fh *)priv)->dev;
1012
1013	strcpy(cap->driver, "cx23885");
1014	strlcpy(cap->card, cx23885_boards[dev->board].name,
1015		sizeof(cap->card));
1016	sprintf(cap->bus_info, "PCIe:%s", pci_name(dev->pci));
1017	cap->version = CX23885_VERSION_CODE;
1018	cap->capabilities =
1019		V4L2_CAP_VIDEO_CAPTURE |
1020		V4L2_CAP_READWRITE     |
1021		V4L2_CAP_STREAMING     |
1022		V4L2_CAP_VBI_CAPTURE;
1023	if (UNSET != dev->tuner_type)
1024		cap->capabilities |= V4L2_CAP_TUNER;
1025	return 0;
1026}
1027
1028static int vidioc_enum_fmt_cap(struct file *file, void  *priv,
1029	struct v4l2_fmtdesc *f)
1030{
1031	if (unlikely(f->index >= ARRAY_SIZE(formats)))
1032		return -EINVAL;
1033
1034	strlcpy(f->description, formats[f->index].name,
1035		sizeof(f->description));
1036	f->pixelformat = formats[f->index].fourcc;
1037
1038	return 0;
1039}
1040
1041#ifdef CONFIG_VIDEO_V4L1_COMPAT
1042static int vidiocgmbuf(struct file *file, void *priv,
1043	struct video_mbuf *mbuf)
1044{
1045	struct cx23885_fh *fh = priv;
1046	struct videobuf_queue *q;
1047	struct v4l2_requestbuffers req;
1048	unsigned int i;
1049	int err;
1050
1051	q = get_queue(fh);
1052	memset(&req, 0, sizeof(req));
1053	req.type   = q->type;
1054	req.count  = 8;
1055	req.memory = V4L2_MEMORY_MMAP;
1056	err = videobuf_reqbufs(q, &req);
1057	if (err < 0)
1058		return err;
1059
1060	mbuf->frames = req.count;
1061	mbuf->size   = 0;
1062	for (i = 0; i < mbuf->frames; i++) {
1063		mbuf->offsets[i]  = q->bufs[i]->boff;
1064		mbuf->size       += q->bufs[i]->bsize;
1065	}
1066	return 0;
1067}
1068#endif
1069
1070static int vidioc_reqbufs(struct file *file, void *priv,
1071	struct v4l2_requestbuffers *p)
1072{
1073	struct cx23885_fh *fh = priv;
1074	return (videobuf_reqbufs(get_queue(fh), p));
1075}
1076
1077static int vidioc_querybuf(struct file *file, void *priv,
1078	struct v4l2_buffer *p)
1079{
1080	struct cx23885_fh *fh = priv;
1081	return (videobuf_querybuf(get_queue(fh), p));
1082}
1083
1084static int vidioc_qbuf(struct file *file, void *priv,
1085	struct v4l2_buffer *p)
1086{
1087	struct cx23885_fh *fh = priv;
1088	return (videobuf_qbuf(get_queue(fh), p));
1089}
1090
1091static int vidioc_dqbuf(struct file *file, void *priv,
1092	struct v4l2_buffer *p)
1093{
1094	struct cx23885_fh *fh = priv;
1095	return (videobuf_dqbuf(get_queue(fh), p,
1096				file->f_flags & O_NONBLOCK));
1097}
1098
1099static int vidioc_streamon(struct file *file, void *priv,
1100	enum v4l2_buf_type i)
1101{
1102	struct cx23885_fh *fh = priv;
1103	struct cx23885_dev *dev = fh->dev;
1104	dprintk(1, "%s()\n", __func__);
1105
1106	if (unlikely(fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE))
1107		return -EINVAL;
1108	if (unlikely(i != fh->type))
1109		return -EINVAL;
1110
1111	if (unlikely(!res_get(dev, fh, get_resource(fh))))
1112		return -EBUSY;
1113	return videobuf_streamon(get_queue(fh));
1114}
1115
1116static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
1117{
1118	struct cx23885_fh *fh = priv;
1119	struct cx23885_dev *dev = fh->dev;
1120	int err, res;
1121	dprintk(1, "%s()\n", __func__);
1122
1123	if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1124		return -EINVAL;
1125	if (i != fh->type)
1126		return -EINVAL;
1127
1128	res = get_resource(fh);
1129	err = videobuf_streamoff(get_queue(fh));
1130	if (err < 0)
1131		return err;
1132	res_free(dev, fh, res);
1133	return 0;
1134}
1135
1136static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *tvnorms)
1137{
1138	struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1139	dprintk(1, "%s()\n", __func__);
1140
1141	mutex_lock(&dev->lock);
1142	cx23885_set_tvnorm(dev, *tvnorms);
1143	mutex_unlock(&dev->lock);
1144
1145	return 0;
1146}
1147
1148int cx23885_enum_input(struct cx23885_dev *dev, struct v4l2_input *i)
1149{
1150	static const char *iname[] = {
1151		[CX23885_VMUX_COMPOSITE1] = "Composite1",
1152		[CX23885_VMUX_COMPOSITE2] = "Composite2",
1153		[CX23885_VMUX_COMPOSITE3] = "Composite3",
1154		[CX23885_VMUX_COMPOSITE4] = "Composite4",
1155		[CX23885_VMUX_SVIDEO]     = "S-Video",
1156		[CX23885_VMUX_TELEVISION] = "Television",
1157		[CX23885_VMUX_CABLE]      = "Cable TV",
1158		[CX23885_VMUX_DVB]        = "DVB",
1159		[CX23885_VMUX_DEBUG]      = "for debug only",
1160	};
1161	unsigned int n;
1162	dprintk(1, "%s()\n", __func__);
1163
1164	n = i->index;
1165	if (n >= 4)
1166		return -EINVAL;
1167
1168	if (0 == INPUT(n)->type)
1169		return -EINVAL;
1170
1171	memset(i, 0, sizeof(*i));
1172	i->index = n;
1173	i->type  = V4L2_INPUT_TYPE_CAMERA;
1174	strcpy(i->name, iname[INPUT(n)->type]);
1175	if ((CX23885_VMUX_TELEVISION == INPUT(n)->type) ||
1176		(CX23885_VMUX_CABLE == INPUT(n)->type))
1177		i->type = V4L2_INPUT_TYPE_TUNER;
1178		i->std = CX23885_NORMS;
1179	return 0;
1180}
1181EXPORT_SYMBOL(cx23885_enum_input);
1182
1183static int vidioc_enum_input(struct file *file, void *priv,
1184				struct v4l2_input *i)
1185{
1186	struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1187	dprintk(1, "%s()\n", __func__);
1188	return cx23885_enum_input(dev, i);
1189}
1190
1191static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1192{
1193	struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1194
1195	*i = dev->input;
1196	dprintk(1, "%s() returns %d\n", __func__, *i);
1197	return 0;
1198}
1199
1200static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1201{
1202	struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1203
1204	dprintk(1, "%s(%d)\n", __func__, i);
1205
1206	if (i >= 4) {
1207		dprintk(1, "%s() -EINVAL\n", __func__);
1208		return -EINVAL;
1209	}
1210
1211	mutex_lock(&dev->lock);
1212	cx23885_video_mux(dev, i);
1213	mutex_unlock(&dev->lock);
1214	return 0;
1215}
1216
1217static int vidioc_queryctrl(struct file *file, void *priv,
1218				struct v4l2_queryctrl *qctrl)
1219{
1220	qctrl->id = v4l2_ctrl_next(ctrl_classes, qctrl->id);
1221	if (unlikely(qctrl->id == 0))
1222		return -EINVAL;
1223	return cx23885_ctrl_query(qctrl);
1224}
1225
1226static int vidioc_g_ctrl(struct file *file, void *priv,
1227				struct v4l2_control *ctl)
1228{
1229	struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1230
1231	return cx23885_get_control(dev, ctl);
1232}
1233
1234static int vidioc_s_ctrl(struct file *file, void *priv,
1235				struct v4l2_control *ctl)
1236{
1237	struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1238
1239	return cx23885_set_control(dev, ctl);
1240}
1241
1242static int vidioc_g_tuner(struct file *file, void *priv,
1243				struct v4l2_tuner *t)
1244{
1245	struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1246
1247	if (unlikely(UNSET == dev->tuner_type))
1248		return -EINVAL;
1249	if (0 != t->index)
1250		return -EINVAL;
1251
1252	strcpy(t->name, "Television");
1253	t->type       = V4L2_TUNER_ANALOG_TV;
1254	t->capability = V4L2_TUNER_CAP_NORM;
1255	t->rangehigh  = 0xffffffffUL;
1256	t->signal = 0xffff ; /* LOCKED */
1257	return 0;
1258}
1259
1260static int vidioc_s_tuner(struct file *file, void *priv,
1261				struct v4l2_tuner *t)
1262{
1263	struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1264
1265	if (UNSET == dev->tuner_type)
1266		return -EINVAL;
1267	if (0 != t->index)
1268		return -EINVAL;
1269	return 0;
1270}
1271
1272static int vidioc_g_frequency(struct file *file, void *priv,
1273				struct v4l2_frequency *f)
1274{
1275	struct cx23885_fh *fh = priv;
1276	struct cx23885_dev *dev = fh->dev;
1277
1278	if (unlikely(UNSET == dev->tuner_type))
1279		return -EINVAL;
1280
1281	/* f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV; */
1282	f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1283	f->frequency = dev->freq;
1284
1285	cx23885_call_i2c_clients(&dev->i2c_bus[1], VIDIOC_G_FREQUENCY, f);
1286
1287	return 0;
1288}
1289
1290int cx23885_set_freq(struct cx23885_dev *dev, struct v4l2_frequency *f)
1291{
1292	if (unlikely(UNSET == dev->tuner_type))
1293		return -EINVAL;
1294	if (unlikely(f->tuner != 0))
1295		return -EINVAL;
1296
1297	mutex_lock(&dev->lock);
1298	dev->freq = f->frequency;
1299
1300	cx23885_call_i2c_clients(&dev->i2c_bus[1], VIDIOC_S_FREQUENCY, f);
1301
1302	/* When changing channels it is required to reset TVAUDIO */
1303	msleep(10);
1304
1305	mutex_unlock(&dev->lock);
1306
1307	return 0;
1308}
1309EXPORT_SYMBOL(cx23885_set_freq);
1310
1311static int vidioc_s_frequency(struct file *file, void *priv,
1312				struct v4l2_frequency *f)
1313{
1314	struct cx23885_fh *fh = priv;
1315	struct cx23885_dev *dev = fh->dev;
1316
1317	if (unlikely(0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV))
1318		return -EINVAL;
1319	if (unlikely(1 == fh->radio && f->type != V4L2_TUNER_RADIO))
1320		return -EINVAL;
1321
1322	return
1323		cx23885_set_freq(dev, f);
1324}
1325
1326#ifdef CONFIG_VIDEO_ADV_DEBUG
1327static int vidioc_g_register(struct file *file, void *fh,
1328				struct v4l2_register *reg)
1329{
1330	struct cx23885_dev *dev = ((struct cx23885_fh *)fh)->dev;
1331
1332	if (!v4l2_chip_match_host(reg->match_type, reg->match_chip))
1333		return -EINVAL;
1334
1335	cx23885_call_i2c_clients(&dev->i2c_bus[2], VIDIOC_DBG_G_REGISTER, reg);
1336
1337	return 0;
1338}
1339
1340static int vidioc_s_register(struct file *file, void *fh,
1341				struct v4l2_register *reg)
1342{
1343	struct cx23885_dev *dev = ((struct cx23885_fh *)fh)->dev;
1344
1345	if (!v4l2_chip_match_host(reg->match_type, reg->match_chip))
1346		return -EINVAL;
1347
1348	cx23885_call_i2c_clients(&dev->i2c_bus[2], VIDIOC_DBG_S_REGISTER, reg);
1349
1350	return 0;
1351}
1352#endif
1353
1354/* ----------------------------------------------------------- */
1355
1356static void cx23885_vid_timeout(unsigned long data)
1357{
1358	struct cx23885_dev *dev = (struct cx23885_dev *)data;
1359	struct cx23885_dmaqueue *q = &dev->vidq;
1360	struct cx23885_buffer *buf;
1361	unsigned long flags;
1362
1363	cx23885_sram_channel_dump(dev, &dev->sram_channels[SRAM_CH01]);
1364
1365	cx_clear(VID_A_DMA_CTL, 0x11);
1366
1367	spin_lock_irqsave(&dev->slock, flags);
1368	while (!list_empty(&q->active)) {
1369		buf = list_entry(q->active.next,
1370			struct cx23885_buffer, vb.queue);
1371		list_del(&buf->vb.queue);
1372		buf->vb.state = VIDEOBUF_ERROR;
1373		wake_up(&buf->vb.done);
1374		printk(KERN_ERR "%s/0: [%p/%d] timeout - dma=0x%08lx\n",
1375			dev->name, buf, buf->vb.i,
1376			(unsigned long)buf->risc.dma);
1377	}
1378	cx23885_restart_video_queue(dev, q);
1379	spin_unlock_irqrestore(&dev->slock, flags);
1380}
1381
1382int cx23885_video_irq(struct cx23885_dev *dev, u32 status)
1383{
1384	u32 mask, count;
1385	int handled = 0;
1386
1387	mask   = cx_read(VID_A_INT_MSK);
1388	if (0 == (status & mask))
1389		return handled;
1390	cx_write(VID_A_INT_STAT, status);
1391
1392	dprintk(2, "%s() status = 0x%08x\n", __func__, status);
1393	/* risc op code error */
1394	if (status & (1 << 16)) {
1395		printk(KERN_WARNING "%s/0: video risc op code error\n",
1396			dev->name);
1397		cx_clear(VID_A_DMA_CTL, 0x11);
1398		cx23885_sram_channel_dump(dev, &dev->sram_channels[SRAM_CH01]);
1399	}
1400
1401	/* risc1 y */
1402	if (status & 0x01) {
1403		spin_lock(&dev->slock);
1404		count = cx_read(VID_A_GPCNT);
1405		cx23885_video_wakeup(dev, &dev->vidq, count);
1406		spin_unlock(&dev->slock);
1407		handled++;
1408	}
1409	/* risc2 y */
1410	if (status & 0x10) {
1411		dprintk(2, "stopper video\n");
1412		spin_lock(&dev->slock);
1413		cx23885_restart_video_queue(dev, &dev->vidq);
1414		spin_unlock(&dev->slock);
1415		handled++;
1416	}
1417
1418	return handled;
1419}
1420
1421/* ----------------------------------------------------------- */
1422/* exported stuff                                              */
1423
1424static const struct file_operations video_fops = {
1425	.owner	       = THIS_MODULE,
1426	.open	       = video_open,
1427	.release       = video_release,
1428	.read	       = video_read,
1429	.poll          = video_poll,
1430	.mmap	       = video_mmap,
1431	.ioctl	       = video_ioctl2,
1432	.compat_ioctl  = v4l_compat_ioctl32,
1433	.llseek        = no_llseek,
1434};
1435
1436static struct video_device cx23885_vbi_template;
1437static struct video_device cx23885_video_template = {
1438	.name                 = "cx23885-video",
1439	.type                 = VID_TYPE_CAPTURE|VID_TYPE_TUNER|VID_TYPE_SCALES,
1440	.fops                 = &video_fops,
1441	.minor                = -1,
1442	.vidioc_querycap      = vidioc_querycap,
1443	.vidioc_enum_fmt_cap  = vidioc_enum_fmt_cap,
1444	.vidioc_g_fmt_cap     = vidioc_g_fmt_cap,
1445	.vidioc_try_fmt_cap   = vidioc_try_fmt_cap,
1446	.vidioc_s_fmt_cap     = vidioc_s_fmt_cap,
1447	.vidioc_g_fmt_vbi     = cx23885_vbi_fmt,
1448	.vidioc_try_fmt_vbi   = cx23885_vbi_fmt,
1449	.vidioc_s_fmt_vbi     = cx23885_vbi_fmt,
1450	.vidioc_reqbufs       = vidioc_reqbufs,
1451	.vidioc_querybuf      = vidioc_querybuf,
1452	.vidioc_qbuf          = vidioc_qbuf,
1453	.vidioc_dqbuf         = vidioc_dqbuf,
1454	.vidioc_s_std         = vidioc_s_std,
1455	.vidioc_enum_input    = vidioc_enum_input,
1456	.vidioc_g_input       = vidioc_g_input,
1457	.vidioc_s_input       = vidioc_s_input,
1458	.vidioc_queryctrl     = vidioc_queryctrl,
1459	.vidioc_g_ctrl        = vidioc_g_ctrl,
1460	.vidioc_s_ctrl        = vidioc_s_ctrl,
1461	.vidioc_streamon      = vidioc_streamon,
1462	.vidioc_streamoff     = vidioc_streamoff,
1463#ifdef CONFIG_VIDEO_V4L1_COMPAT
1464	.vidiocgmbuf          = vidiocgmbuf,
1465#endif
1466	.vidioc_g_tuner       = vidioc_g_tuner,
1467	.vidioc_s_tuner       = vidioc_s_tuner,
1468	.vidioc_g_frequency   = vidioc_g_frequency,
1469	.vidioc_s_frequency   = vidioc_s_frequency,
1470#ifdef CONFIG_VIDEO_ADV_DEBUG
1471	.vidioc_g_register    = vidioc_g_register,
1472	.vidioc_s_register    = vidioc_s_register,
1473#endif
1474	.tvnorms              = CX23885_NORMS,
1475	.current_norm         = V4L2_STD_NTSC_M,
1476};
1477
1478static const struct file_operations radio_fops = {
1479	.owner         = THIS_MODULE,
1480	.open          = video_open,
1481	.release       = video_release,
1482	.ioctl         = video_ioctl2,
1483	.compat_ioctl  = v4l_compat_ioctl32,
1484	.llseek        = no_llseek,
1485};
1486
1487
1488void cx23885_video_unregister(struct cx23885_dev *dev)
1489{
1490	dprintk(1, "%s()\n", __func__);
1491	cx_clear(PCI_INT_MSK, 1);
1492
1493	if (dev->video_dev) {
1494		if (-1 != dev->video_dev->minor)
1495			video_unregister_device(dev->video_dev);
1496		else
1497			video_device_release(dev->video_dev);
1498		dev->video_dev = NULL;
1499
1500		btcx_riscmem_free(dev->pci, &dev->vidq.stopper);
1501	}
1502}
1503
1504int cx23885_video_register(struct cx23885_dev *dev)
1505{
1506	int err;
1507
1508	dprintk(1, "%s()\n", __func__);
1509	spin_lock_init(&dev->slock);
1510
1511	/* Initialize VBI template */
1512	memcpy(&cx23885_vbi_template, &cx23885_video_template,
1513		sizeof(cx23885_vbi_template));
1514	strcpy(cx23885_vbi_template.name, "cx23885-vbi");
1515	cx23885_vbi_template.type = VID_TYPE_TELETEXT|VID_TYPE_TUNER;
1516
1517	dev->tvnorm = cx23885_video_template.current_norm;
1518
1519	/* init video dma queues */
1520	INIT_LIST_HEAD(&dev->vidq.active);
1521	INIT_LIST_HEAD(&dev->vidq.queued);
1522	dev->vidq.timeout.function = cx23885_vid_timeout;
1523	dev->vidq.timeout.data = (unsigned long)dev;
1524	init_timer(&dev->vidq.timeout);
1525	cx23885_risc_stopper(dev->pci, &dev->vidq.stopper,
1526		VID_A_DMA_CTL, 0x11, 0x00);
1527
1528	/* Don't enable VBI yet */
1529	cx_set(PCI_INT_MSK, 1);
1530
1531
1532	/* register v4l devices */
1533	dev->video_dev = cx23885_vdev_init(dev, dev->pci,
1534		&cx23885_video_template, "video");
1535	err = video_register_device(dev->video_dev, VFL_TYPE_GRABBER,
1536				    video_nr[dev->nr]);
1537	if (err < 0) {
1538		printk(KERN_INFO "%s: can't register video device\n",
1539			dev->name);
1540		goto fail_unreg;
1541	}
1542	printk(KERN_INFO "%s/0: registered device video%d [v4l2]\n",
1543	       dev->name, dev->video_dev->minor & 0x1f);
1544	/* initial device configuration */
1545	mutex_lock(&dev->lock);
1546	cx23885_set_tvnorm(dev, dev->tvnorm);
1547	init_controls(dev);
1548	cx23885_video_mux(dev, 0);
1549	mutex_unlock(&dev->lock);
1550
1551	return 0;
1552
1553fail_unreg:
1554	cx23885_video_unregister(dev);
1555	return err;
1556}
1557
1558