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