• Home
  • History
  • Annotate
  • Line#
  • Navigate
  • Raw
  • Download
  • only in /netgear-R7000-V1.0.7.12_1.2.5/components/opensource/linux/linux-2.6.36/drivers/media/video/cx88/
1/*
2 *
3 * device driver for Conexant 2388x based TV cards
4 * video4linux video interface
5 *
6 * (c) 2003-04 Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]
7 *
8 * (c) 2005-2006 Mauro Carvalho Chehab <mchehab@infradead.org>
9 *	- Multituner support
10 *	- video_ioctl2 conversion
11 *	- PAL/M fixes
12 *
13 *  This program is free software; you can redistribute it and/or modify
14 *  it under the terms of the GNU General Public License as published by
15 *  the Free Software Foundation; either version 2 of the License, or
16 *  (at your option) any later version.
17 *
18 *  This program is distributed in the hope that it will be useful,
19 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
20 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21 *  GNU General Public License for more details.
22 *
23 *  You should have received a copy of the GNU General Public License
24 *  along with this program; if not, write to the Free Software
25 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 */
27
28#include <linux/init.h>
29#include <linux/list.h>
30#include <linux/module.h>
31#include <linux/kmod.h>
32#include <linux/kernel.h>
33#include <linux/slab.h>
34#include <linux/smp_lock.h>
35#include <linux/interrupt.h>
36#include <linux/dma-mapping.h>
37#include <linux/delay.h>
38#include <linux/kthread.h>
39#include <asm/div64.h>
40
41#include "cx88.h"
42#include <media/v4l2-common.h>
43#include <media/v4l2-ioctl.h>
44
45MODULE_DESCRIPTION("v4l2 driver module for cx2388x based TV cards");
46MODULE_AUTHOR("Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]");
47MODULE_LICENSE("GPL");
48
49/* ------------------------------------------------------------------ */
50
51static unsigned int video_nr[] = {[0 ... (CX88_MAXBOARDS - 1)] = UNSET };
52static unsigned int vbi_nr[]   = {[0 ... (CX88_MAXBOARDS - 1)] = UNSET };
53static unsigned int radio_nr[] = {[0 ... (CX88_MAXBOARDS - 1)] = UNSET };
54
55module_param_array(video_nr, int, NULL, 0444);
56module_param_array(vbi_nr,   int, NULL, 0444);
57module_param_array(radio_nr, int, NULL, 0444);
58
59MODULE_PARM_DESC(video_nr,"video device numbers");
60MODULE_PARM_DESC(vbi_nr,"vbi device numbers");
61MODULE_PARM_DESC(radio_nr,"radio device numbers");
62
63static unsigned int video_debug;
64module_param(video_debug,int,0644);
65MODULE_PARM_DESC(video_debug,"enable debug messages [video]");
66
67static unsigned int irq_debug;
68module_param(irq_debug,int,0644);
69MODULE_PARM_DESC(irq_debug,"enable debug messages [IRQ handler]");
70
71static unsigned int vid_limit = 16;
72module_param(vid_limit,int,0644);
73MODULE_PARM_DESC(vid_limit,"capture memory limit in megabytes");
74
75#define dprintk(level,fmt, arg...)	if (video_debug >= level) \
76	printk(KERN_DEBUG "%s/0: " fmt, core->name , ## arg)
77
78/* ------------------------------------------------------------------- */
79/* static data                                                         */
80
81static struct cx8800_fmt formats[] = {
82	{
83		.name     = "8 bpp, gray",
84		.fourcc   = V4L2_PIX_FMT_GREY,
85		.cxformat = ColorFormatY8,
86		.depth    = 8,
87		.flags    = FORMAT_FLAGS_PACKED,
88	},{
89		.name     = "15 bpp RGB, le",
90		.fourcc   = V4L2_PIX_FMT_RGB555,
91		.cxformat = ColorFormatRGB15,
92		.depth    = 16,
93		.flags    = FORMAT_FLAGS_PACKED,
94	},{
95		.name     = "15 bpp RGB, be",
96		.fourcc   = V4L2_PIX_FMT_RGB555X,
97		.cxformat = ColorFormatRGB15 | ColorFormatBSWAP,
98		.depth    = 16,
99		.flags    = FORMAT_FLAGS_PACKED,
100	},{
101		.name     = "16 bpp RGB, le",
102		.fourcc   = V4L2_PIX_FMT_RGB565,
103		.cxformat = ColorFormatRGB16,
104		.depth    = 16,
105		.flags    = FORMAT_FLAGS_PACKED,
106	},{
107		.name     = "16 bpp RGB, be",
108		.fourcc   = V4L2_PIX_FMT_RGB565X,
109		.cxformat = ColorFormatRGB16 | ColorFormatBSWAP,
110		.depth    = 16,
111		.flags    = FORMAT_FLAGS_PACKED,
112	},{
113		.name     = "24 bpp RGB, le",
114		.fourcc   = V4L2_PIX_FMT_BGR24,
115		.cxformat = ColorFormatRGB24,
116		.depth    = 24,
117		.flags    = FORMAT_FLAGS_PACKED,
118	},{
119		.name     = "32 bpp RGB, le",
120		.fourcc   = V4L2_PIX_FMT_BGR32,
121		.cxformat = ColorFormatRGB32,
122		.depth    = 32,
123		.flags    = FORMAT_FLAGS_PACKED,
124	},{
125		.name     = "32 bpp RGB, be",
126		.fourcc   = V4L2_PIX_FMT_RGB32,
127		.cxformat = ColorFormatRGB32 | ColorFormatBSWAP | ColorFormatWSWAP,
128		.depth    = 32,
129		.flags    = FORMAT_FLAGS_PACKED,
130	},{
131		.name     = "4:2:2, packed, YUYV",
132		.fourcc   = V4L2_PIX_FMT_YUYV,
133		.cxformat = ColorFormatYUY2,
134		.depth    = 16,
135		.flags    = FORMAT_FLAGS_PACKED,
136	},{
137		.name     = "4:2:2, packed, UYVY",
138		.fourcc   = V4L2_PIX_FMT_UYVY,
139		.cxformat = ColorFormatYUY2 | ColorFormatBSWAP,
140		.depth    = 16,
141		.flags    = FORMAT_FLAGS_PACKED,
142	},
143};
144
145static struct cx8800_fmt* format_by_fourcc(unsigned int fourcc)
146{
147	unsigned int i;
148
149	for (i = 0; i < ARRAY_SIZE(formats); i++)
150		if (formats[i].fourcc == fourcc)
151			return formats+i;
152	return NULL;
153}
154
155/* ------------------------------------------------------------------- */
156
157static const struct v4l2_queryctrl no_ctl = {
158	.name  = "42",
159	.flags = V4L2_CTRL_FLAG_DISABLED,
160};
161
162static struct cx88_ctrl cx8800_ctls[] = {
163	/* --- video --- */
164	{
165		.v = {
166			.id            = V4L2_CID_BRIGHTNESS,
167			.name          = "Brightness",
168			.minimum       = 0x00,
169			.maximum       = 0xff,
170			.step          = 1,
171			.default_value = 0x7f,
172			.type          = V4L2_CTRL_TYPE_INTEGER,
173		},
174		.off                   = 128,
175		.reg                   = MO_CONTR_BRIGHT,
176		.mask                  = 0x00ff,
177		.shift                 = 0,
178	},{
179		.v = {
180			.id            = V4L2_CID_CONTRAST,
181			.name          = "Contrast",
182			.minimum       = 0,
183			.maximum       = 0xff,
184			.step          = 1,
185			.default_value = 0x3f,
186			.type          = V4L2_CTRL_TYPE_INTEGER,
187		},
188		.off                   = 0,
189		.reg                   = MO_CONTR_BRIGHT,
190		.mask                  = 0xff00,
191		.shift                 = 8,
192	},{
193		.v = {
194			.id            = V4L2_CID_HUE,
195			.name          = "Hue",
196			.minimum       = 0,
197			.maximum       = 0xff,
198			.step          = 1,
199			.default_value = 0x7f,
200			.type          = V4L2_CTRL_TYPE_INTEGER,
201		},
202		.off                   = 128,
203		.reg                   = MO_HUE,
204		.mask                  = 0x00ff,
205		.shift                 = 0,
206	},{
207		/* strictly, this only describes only U saturation.
208		 * V saturation is handled specially through code.
209		 */
210		.v = {
211			.id            = V4L2_CID_SATURATION,
212			.name          = "Saturation",
213			.minimum       = 0,
214			.maximum       = 0xff,
215			.step          = 1,
216			.default_value = 0x7f,
217			.type          = V4L2_CTRL_TYPE_INTEGER,
218		},
219		.off                   = 0,
220		.reg                   = MO_UV_SATURATION,
221		.mask                  = 0x00ff,
222		.shift                 = 0,
223	},{
224		.v = {
225			.id            = V4L2_CID_CHROMA_AGC,
226			.name          = "Chroma AGC",
227			.minimum       = 0,
228			.maximum       = 1,
229			.default_value = 0x1,
230			.type          = V4L2_CTRL_TYPE_BOOLEAN,
231		},
232		.reg                   = MO_INPUT_FORMAT,
233		.mask                  = 1 << 10,
234		.shift                 = 10,
235	}, {
236		.v = {
237			.id            = V4L2_CID_COLOR_KILLER,
238			.name          = "Color killer",
239			.minimum       = 0,
240			.maximum       = 1,
241			.default_value = 0x1,
242			.type          = V4L2_CTRL_TYPE_BOOLEAN,
243		},
244		.reg                   = MO_INPUT_FORMAT,
245		.mask                  = 1 << 9,
246		.shift                 = 9,
247	}, {
248	/* --- audio --- */
249		.v = {
250			.id            = V4L2_CID_AUDIO_MUTE,
251			.name          = "Mute",
252			.minimum       = 0,
253			.maximum       = 1,
254			.default_value = 1,
255			.type          = V4L2_CTRL_TYPE_BOOLEAN,
256		},
257		.reg                   = AUD_VOL_CTL,
258		.sreg                  = SHADOW_AUD_VOL_CTL,
259		.mask                  = (1 << 6),
260		.shift                 = 6,
261	},{
262		.v = {
263			.id            = V4L2_CID_AUDIO_VOLUME,
264			.name          = "Volume",
265			.minimum       = 0,
266			.maximum       = 0x3f,
267			.step          = 1,
268			.default_value = 0x3f,
269			.type          = V4L2_CTRL_TYPE_INTEGER,
270		},
271		.reg                   = AUD_VOL_CTL,
272		.sreg                  = SHADOW_AUD_VOL_CTL,
273		.mask                  = 0x3f,
274		.shift                 = 0,
275	},{
276		.v = {
277			.id            = V4L2_CID_AUDIO_BALANCE,
278			.name          = "Balance",
279			.minimum       = 0,
280			.maximum       = 0x7f,
281			.step          = 1,
282			.default_value = 0x40,
283			.type          = V4L2_CTRL_TYPE_INTEGER,
284		},
285		.reg                   = AUD_BAL_CTL,
286		.sreg                  = SHADOW_AUD_BAL_CTL,
287		.mask                  = 0x7f,
288		.shift                 = 0,
289	}
290};
291static const int CX8800_CTLS = ARRAY_SIZE(cx8800_ctls);
292
293/* Must be sorted from low to high control ID! */
294const u32 cx88_user_ctrls[] = {
295	V4L2_CID_USER_CLASS,
296	V4L2_CID_BRIGHTNESS,
297	V4L2_CID_CONTRAST,
298	V4L2_CID_SATURATION,
299	V4L2_CID_HUE,
300	V4L2_CID_AUDIO_VOLUME,
301	V4L2_CID_AUDIO_BALANCE,
302	V4L2_CID_AUDIO_MUTE,
303	V4L2_CID_CHROMA_AGC,
304	V4L2_CID_COLOR_KILLER,
305	0
306};
307EXPORT_SYMBOL(cx88_user_ctrls);
308
309static const u32 *ctrl_classes[] = {
310	cx88_user_ctrls,
311	NULL
312};
313
314int cx8800_ctrl_query(struct cx88_core *core, struct v4l2_queryctrl *qctrl)
315{
316	int i;
317
318	if (qctrl->id < V4L2_CID_BASE ||
319	    qctrl->id >= V4L2_CID_LASTP1)
320		return -EINVAL;
321	for (i = 0; i < CX8800_CTLS; i++)
322		if (cx8800_ctls[i].v.id == qctrl->id)
323			break;
324	if (i == CX8800_CTLS) {
325		*qctrl = no_ctl;
326		return 0;
327	}
328	*qctrl = cx8800_ctls[i].v;
329	/* Report chroma AGC as inactive when SECAM is selected */
330	if (cx8800_ctls[i].v.id == V4L2_CID_CHROMA_AGC &&
331	    core->tvnorm & V4L2_STD_SECAM)
332		qctrl->flags |= V4L2_CTRL_FLAG_INACTIVE;
333
334	return 0;
335}
336EXPORT_SYMBOL(cx8800_ctrl_query);
337
338/* ------------------------------------------------------------------- */
339/* resource management                                                 */
340
341static int res_get(struct cx8800_dev *dev, struct cx8800_fh *fh, unsigned int bit)
342{
343	struct cx88_core *core = dev->core;
344	if (fh->resources & bit)
345		/* have it already allocated */
346		return 1;
347
348	/* is it free? */
349	mutex_lock(&core->lock);
350	if (dev->resources & bit) {
351		/* no, someone else uses it */
352		mutex_unlock(&core->lock);
353		return 0;
354	}
355	/* it's free, grab it */
356	fh->resources  |= bit;
357	dev->resources |= bit;
358	dprintk(1,"res: get %d\n",bit);
359	mutex_unlock(&core->lock);
360	return 1;
361}
362
363static
364int res_check(struct cx8800_fh *fh, unsigned int bit)
365{
366	return (fh->resources & bit);
367}
368
369static
370int res_locked(struct cx8800_dev *dev, unsigned int bit)
371{
372	return (dev->resources & bit);
373}
374
375static
376void res_free(struct cx8800_dev *dev, struct cx8800_fh *fh, unsigned int bits)
377{
378	struct cx88_core *core = dev->core;
379	BUG_ON((fh->resources & bits) != bits);
380
381	mutex_lock(&core->lock);
382	fh->resources  &= ~bits;
383	dev->resources &= ~bits;
384	dprintk(1,"res: put %d\n",bits);
385	mutex_unlock(&core->lock);
386}
387
388/* ------------------------------------------------------------------ */
389
390int cx88_video_mux(struct cx88_core *core, unsigned int input)
391{
392	/* struct cx88_core *core = dev->core; */
393
394	dprintk(1,"video_mux: %d [vmux=%d,gpio=0x%x,0x%x,0x%x,0x%x]\n",
395		input, INPUT(input).vmux,
396		INPUT(input).gpio0,INPUT(input).gpio1,
397		INPUT(input).gpio2,INPUT(input).gpio3);
398	core->input = input;
399	cx_andor(MO_INPUT_FORMAT, 0x03 << 14, INPUT(input).vmux << 14);
400	cx_write(MO_GP3_IO, INPUT(input).gpio3);
401	cx_write(MO_GP0_IO, INPUT(input).gpio0);
402	cx_write(MO_GP1_IO, INPUT(input).gpio1);
403	cx_write(MO_GP2_IO, INPUT(input).gpio2);
404
405	switch (INPUT(input).type) {
406	case CX88_VMUX_SVIDEO:
407		cx_set(MO_AFECFG_IO,    0x00000001);
408		cx_set(MO_INPUT_FORMAT, 0x00010010);
409		cx_set(MO_FILTER_EVEN,  0x00002020);
410		cx_set(MO_FILTER_ODD,   0x00002020);
411		break;
412	default:
413		cx_clear(MO_AFECFG_IO,    0x00000001);
414		cx_clear(MO_INPUT_FORMAT, 0x00010010);
415		cx_clear(MO_FILTER_EVEN,  0x00002020);
416		cx_clear(MO_FILTER_ODD,   0x00002020);
417		break;
418	}
419
420	/* if there are audioroutes defined, we have an external
421	   ADC to deal with audio */
422	if (INPUT(input).audioroute) {
423		/* The wm8775 module has the "2" route hardwired into
424		   the initialization. Some boards may use different
425		   routes for different inputs. HVR-1300 surely does */
426		if (core->board.audio_chip &&
427		    core->board.audio_chip == V4L2_IDENT_WM8775) {
428			call_all(core, audio, s_routing,
429				 INPUT(input).audioroute, 0, 0);
430		}
431		/* cx2388's C-ADC is connected to the tuner only.
432		   When used with S-Video, that ADC is busy dealing with
433		   chroma, so an external must be used for baseband audio */
434		if (INPUT(input).type != CX88_VMUX_TELEVISION &&
435		    INPUT(input).type != CX88_VMUX_CABLE) {
436			/* "I2S ADC mode" */
437			core->tvaudio = WW_I2SADC;
438			cx88_set_tvaudio(core);
439		} else {
440			/* Normal mode */
441			cx_write(AUD_I2SCNTL, 0x0);
442			cx_clear(AUD_CTL, EN_I2SIN_ENABLE);
443		}
444	}
445
446	return 0;
447}
448EXPORT_SYMBOL(cx88_video_mux);
449
450/* ------------------------------------------------------------------ */
451
452static int start_video_dma(struct cx8800_dev    *dev,
453			   struct cx88_dmaqueue *q,
454			   struct cx88_buffer   *buf)
455{
456	struct cx88_core *core = dev->core;
457
458	/* setup fifo + format */
459	cx88_sram_channel_setup(core, &cx88_sram_channels[SRAM_CH21],
460				buf->bpl, buf->risc.dma);
461	cx88_set_scale(core, buf->vb.width, buf->vb.height, buf->vb.field);
462	cx_write(MO_COLOR_CTRL, buf->fmt->cxformat | ColorFormatGamma);
463
464	/* reset counter */
465	cx_write(MO_VIDY_GPCNTRL,GP_COUNT_CONTROL_RESET);
466	q->count = 1;
467
468	/* enable irqs */
469	cx_set(MO_PCI_INTMSK, core->pci_irqmask | PCI_INT_VIDINT);
470
471	/* Enables corresponding bits at PCI_INT_STAT:
472		bits 0 to 4: video, audio, transport stream, VIP, Host
473		bit 7: timer
474		bits 8 and 9: DMA complete for: SRC, DST
475		bits 10 and 11: BERR signal asserted for RISC: RD, WR
476		bits 12 to 15: BERR signal asserted for: BRDG, SRC, DST, IPB
477	 */
478	cx_set(MO_VID_INTMSK, 0x0f0011);
479
480	/* enable capture */
481	cx_set(VID_CAPTURE_CONTROL,0x06);
482
483	/* start dma */
484	cx_set(MO_DEV_CNTRL2, (1<<5));
485	cx_set(MO_VID_DMACNTRL, 0x11); /* Planar Y and packed FIFO and RISC enable */
486
487	return 0;
488}
489
490#ifdef CONFIG_PM
491static int stop_video_dma(struct cx8800_dev    *dev)
492{
493	struct cx88_core *core = dev->core;
494
495	/* stop dma */
496	cx_clear(MO_VID_DMACNTRL, 0x11);
497
498	/* disable capture */
499	cx_clear(VID_CAPTURE_CONTROL,0x06);
500
501	/* disable irqs */
502	cx_clear(MO_PCI_INTMSK, PCI_INT_VIDINT);
503	cx_clear(MO_VID_INTMSK, 0x0f0011);
504	return 0;
505}
506#endif
507
508static int restart_video_queue(struct cx8800_dev    *dev,
509			       struct cx88_dmaqueue *q)
510{
511	struct cx88_core *core = dev->core;
512	struct cx88_buffer *buf, *prev;
513
514	if (!list_empty(&q->active)) {
515		buf = list_entry(q->active.next, struct cx88_buffer, vb.queue);
516		dprintk(2,"restart_queue [%p/%d]: restart dma\n",
517			buf, buf->vb.i);
518		start_video_dma(dev, q, buf);
519		list_for_each_entry(buf, &q->active, vb.queue)
520			buf->count = q->count++;
521		mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
522		return 0;
523	}
524
525	prev = NULL;
526	for (;;) {
527		if (list_empty(&q->queued))
528			return 0;
529		buf = list_entry(q->queued.next, struct cx88_buffer, vb.queue);
530		if (NULL == prev) {
531			list_move_tail(&buf->vb.queue, &q->active);
532			start_video_dma(dev, q, buf);
533			buf->vb.state = VIDEOBUF_ACTIVE;
534			buf->count    = q->count++;
535			mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
536			dprintk(2,"[%p/%d] restart_queue - first active\n",
537				buf,buf->vb.i);
538
539		} else if (prev->vb.width  == buf->vb.width  &&
540			   prev->vb.height == buf->vb.height &&
541			   prev->fmt       == buf->fmt) {
542			list_move_tail(&buf->vb.queue, &q->active);
543			buf->vb.state = VIDEOBUF_ACTIVE;
544			buf->count    = q->count++;
545			prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
546			dprintk(2,"[%p/%d] restart_queue - move to active\n",
547				buf,buf->vb.i);
548		} else {
549			return 0;
550		}
551		prev = buf;
552	}
553}
554
555/* ------------------------------------------------------------------ */
556
557static int
558buffer_setup(struct videobuf_queue *q, unsigned int *count, unsigned int *size)
559{
560	struct cx8800_fh *fh = q->priv_data;
561
562	*size = fh->fmt->depth*fh->width*fh->height >> 3;
563	if (0 == *count)
564		*count = 32;
565	if (*size * *count > vid_limit * 1024 * 1024)
566		*count = (vid_limit * 1024 * 1024) / *size;
567	return 0;
568}
569
570static int
571buffer_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
572	       enum v4l2_field field)
573{
574	struct cx8800_fh   *fh  = q->priv_data;
575	struct cx8800_dev  *dev = fh->dev;
576	struct cx88_core *core = dev->core;
577	struct cx88_buffer *buf = container_of(vb,struct cx88_buffer,vb);
578	struct videobuf_dmabuf *dma=videobuf_to_dma(&buf->vb);
579	int rc, init_buffer = 0;
580
581	BUG_ON(NULL == fh->fmt);
582	if (fh->width  < 48 || fh->width  > norm_maxw(core->tvnorm) ||
583	    fh->height < 32 || fh->height > norm_maxh(core->tvnorm))
584		return -EINVAL;
585	buf->vb.size = (fh->width * fh->height * fh->fmt->depth) >> 3;
586	if (0 != buf->vb.baddr  &&  buf->vb.bsize < buf->vb.size)
587		return -EINVAL;
588
589	if (buf->fmt       != fh->fmt    ||
590	    buf->vb.width  != fh->width  ||
591	    buf->vb.height != fh->height ||
592	    buf->vb.field  != field) {
593		buf->fmt       = fh->fmt;
594		buf->vb.width  = fh->width;
595		buf->vb.height = fh->height;
596		buf->vb.field  = field;
597		init_buffer = 1;
598	}
599
600	if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
601		init_buffer = 1;
602		if (0 != (rc = videobuf_iolock(q,&buf->vb,NULL)))
603			goto fail;
604	}
605
606	if (init_buffer) {
607		buf->bpl = buf->vb.width * buf->fmt->depth >> 3;
608		switch (buf->vb.field) {
609		case V4L2_FIELD_TOP:
610			cx88_risc_buffer(dev->pci, &buf->risc,
611					 dma->sglist, 0, UNSET,
612					 buf->bpl, 0, buf->vb.height);
613			break;
614		case V4L2_FIELD_BOTTOM:
615			cx88_risc_buffer(dev->pci, &buf->risc,
616					 dma->sglist, UNSET, 0,
617					 buf->bpl, 0, buf->vb.height);
618			break;
619		case V4L2_FIELD_INTERLACED:
620			cx88_risc_buffer(dev->pci, &buf->risc,
621					 dma->sglist, 0, buf->bpl,
622					 buf->bpl, buf->bpl,
623					 buf->vb.height >> 1);
624			break;
625		case V4L2_FIELD_SEQ_TB:
626			cx88_risc_buffer(dev->pci, &buf->risc,
627					 dma->sglist,
628					 0, buf->bpl * (buf->vb.height >> 1),
629					 buf->bpl, 0,
630					 buf->vb.height >> 1);
631			break;
632		case V4L2_FIELD_SEQ_BT:
633			cx88_risc_buffer(dev->pci, &buf->risc,
634					 dma->sglist,
635					 buf->bpl * (buf->vb.height >> 1), 0,
636					 buf->bpl, 0,
637					 buf->vb.height >> 1);
638			break;
639		default:
640			BUG();
641		}
642	}
643	dprintk(2,"[%p/%d] buffer_prepare - %dx%d %dbpp \"%s\" - dma=0x%08lx\n",
644		buf, buf->vb.i,
645		fh->width, fh->height, fh->fmt->depth, fh->fmt->name,
646		(unsigned long)buf->risc.dma);
647
648	buf->vb.state = VIDEOBUF_PREPARED;
649	return 0;
650
651 fail:
652	cx88_free_buffer(q,buf);
653	return rc;
654}
655
656static void
657buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
658{
659	struct cx88_buffer    *buf = container_of(vb,struct cx88_buffer,vb);
660	struct cx88_buffer    *prev;
661	struct cx8800_fh      *fh   = vq->priv_data;
662	struct cx8800_dev     *dev  = fh->dev;
663	struct cx88_core      *core = dev->core;
664	struct cx88_dmaqueue  *q    = &dev->vidq;
665
666	/* add jump to stopper */
667	buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_IRQ1 | RISC_CNT_INC);
668	buf->risc.jmp[1] = cpu_to_le32(q->stopper.dma);
669
670	if (!list_empty(&q->queued)) {
671		list_add_tail(&buf->vb.queue,&q->queued);
672		buf->vb.state = VIDEOBUF_QUEUED;
673		dprintk(2,"[%p/%d] buffer_queue - append to queued\n",
674			buf, buf->vb.i);
675
676	} else if (list_empty(&q->active)) {
677		list_add_tail(&buf->vb.queue,&q->active);
678		start_video_dma(dev, q, buf);
679		buf->vb.state = VIDEOBUF_ACTIVE;
680		buf->count    = q->count++;
681		mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
682		dprintk(2,"[%p/%d] buffer_queue - first active\n",
683			buf, buf->vb.i);
684
685	} else {
686		prev = list_entry(q->active.prev, struct cx88_buffer, vb.queue);
687		if (prev->vb.width  == buf->vb.width  &&
688		    prev->vb.height == buf->vb.height &&
689		    prev->fmt       == buf->fmt) {
690			list_add_tail(&buf->vb.queue,&q->active);
691			buf->vb.state = VIDEOBUF_ACTIVE;
692			buf->count    = q->count++;
693			prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
694			dprintk(2,"[%p/%d] buffer_queue - append to active\n",
695				buf, buf->vb.i);
696
697		} else {
698			list_add_tail(&buf->vb.queue,&q->queued);
699			buf->vb.state = VIDEOBUF_QUEUED;
700			dprintk(2,"[%p/%d] buffer_queue - first queued\n",
701				buf, buf->vb.i);
702		}
703	}
704}
705
706static void buffer_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
707{
708	struct cx88_buffer *buf = container_of(vb,struct cx88_buffer,vb);
709
710	cx88_free_buffer(q,buf);
711}
712
713static struct videobuf_queue_ops cx8800_video_qops = {
714	.buf_setup    = buffer_setup,
715	.buf_prepare  = buffer_prepare,
716	.buf_queue    = buffer_queue,
717	.buf_release  = buffer_release,
718};
719
720/* ------------------------------------------------------------------ */
721
722
723/* ------------------------------------------------------------------ */
724
725static struct videobuf_queue* get_queue(struct cx8800_fh *fh)
726{
727	switch (fh->type) {
728	case V4L2_BUF_TYPE_VIDEO_CAPTURE:
729		return &fh->vidq;
730	case V4L2_BUF_TYPE_VBI_CAPTURE:
731		return &fh->vbiq;
732	default:
733		BUG();
734		return NULL;
735	}
736}
737
738static int get_ressource(struct cx8800_fh *fh)
739{
740	switch (fh->type) {
741	case V4L2_BUF_TYPE_VIDEO_CAPTURE:
742		return RESOURCE_VIDEO;
743	case V4L2_BUF_TYPE_VBI_CAPTURE:
744		return RESOURCE_VBI;
745	default:
746		BUG();
747		return 0;
748	}
749}
750
751static int video_open(struct file *file)
752{
753	struct video_device *vdev = video_devdata(file);
754	struct cx8800_dev *dev = video_drvdata(file);
755	struct cx88_core *core;
756	struct cx8800_fh *fh;
757	enum v4l2_buf_type type = 0;
758	int radio = 0;
759
760	switch (vdev->vfl_type) {
761	case VFL_TYPE_GRABBER:
762		type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
763		break;
764	case VFL_TYPE_VBI:
765		type = V4L2_BUF_TYPE_VBI_CAPTURE;
766		break;
767	case VFL_TYPE_RADIO:
768		radio = 1;
769		break;
770	}
771
772	lock_kernel();
773
774	core = dev->core;
775
776	dprintk(1, "open dev=%s radio=%d type=%s\n",
777		video_device_node_name(vdev), radio, v4l2_type_names[type]);
778
779	/* allocate + initialize per filehandle data */
780	fh = kzalloc(sizeof(*fh),GFP_KERNEL);
781	if (NULL == fh) {
782		unlock_kernel();
783		return -ENOMEM;
784	}
785	file->private_data = fh;
786	fh->dev      = dev;
787	fh->radio    = radio;
788	fh->type     = type;
789	fh->width    = 320;
790	fh->height   = 240;
791	fh->fmt      = format_by_fourcc(V4L2_PIX_FMT_BGR24);
792
793	videobuf_queue_sg_init(&fh->vidq, &cx8800_video_qops,
794			    &dev->pci->dev, &dev->slock,
795			    V4L2_BUF_TYPE_VIDEO_CAPTURE,
796			    V4L2_FIELD_INTERLACED,
797			    sizeof(struct cx88_buffer),
798			    fh);
799	videobuf_queue_sg_init(&fh->vbiq, &cx8800_vbi_qops,
800			    &dev->pci->dev, &dev->slock,
801			    V4L2_BUF_TYPE_VBI_CAPTURE,
802			    V4L2_FIELD_SEQ_TB,
803			    sizeof(struct cx88_buffer),
804			    fh);
805
806	if (fh->radio) {
807		dprintk(1,"video_open: setting radio device\n");
808		cx_write(MO_GP3_IO, core->board.radio.gpio3);
809		cx_write(MO_GP0_IO, core->board.radio.gpio0);
810		cx_write(MO_GP1_IO, core->board.radio.gpio1);
811		cx_write(MO_GP2_IO, core->board.radio.gpio2);
812		if (core->board.radio.audioroute) {
813			if(core->board.audio_chip &&
814				core->board.audio_chip == V4L2_IDENT_WM8775) {
815				call_all(core, audio, s_routing,
816					core->board.radio.audioroute, 0, 0);
817			}
818			/* "I2S ADC mode" */
819			core->tvaudio = WW_I2SADC;
820			cx88_set_tvaudio(core);
821		} else {
822			/* FM Mode */
823			core->tvaudio = WW_FM;
824			cx88_set_tvaudio(core);
825			cx88_set_stereo(core,V4L2_TUNER_MODE_STEREO,1);
826		}
827		call_all(core, tuner, s_radio);
828	}
829	unlock_kernel();
830
831	atomic_inc(&core->users);
832
833	return 0;
834}
835
836static ssize_t
837video_read(struct file *file, char __user *data, size_t count, loff_t *ppos)
838{
839	struct cx8800_fh *fh = file->private_data;
840
841	switch (fh->type) {
842	case V4L2_BUF_TYPE_VIDEO_CAPTURE:
843		if (res_locked(fh->dev,RESOURCE_VIDEO))
844			return -EBUSY;
845		return videobuf_read_one(&fh->vidq, data, count, ppos,
846					 file->f_flags & O_NONBLOCK);
847	case V4L2_BUF_TYPE_VBI_CAPTURE:
848		if (!res_get(fh->dev,fh,RESOURCE_VBI))
849			return -EBUSY;
850		return videobuf_read_stream(&fh->vbiq, data, count, ppos, 1,
851					    file->f_flags & O_NONBLOCK);
852	default:
853		BUG();
854		return 0;
855	}
856}
857
858static unsigned int
859video_poll(struct file *file, struct poll_table_struct *wait)
860{
861	struct cx8800_fh *fh = file->private_data;
862	struct cx88_buffer *buf;
863	unsigned int rc = POLLERR;
864
865	if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type) {
866		if (!res_get(fh->dev,fh,RESOURCE_VBI))
867			return POLLERR;
868		return videobuf_poll_stream(file, &fh->vbiq, wait);
869	}
870
871	mutex_lock(&fh->vidq.vb_lock);
872	if (res_check(fh,RESOURCE_VIDEO)) {
873		/* streaming capture */
874		if (list_empty(&fh->vidq.stream))
875			goto done;
876		buf = list_entry(fh->vidq.stream.next,struct cx88_buffer,vb.stream);
877	} else {
878		/* read() capture */
879		buf = (struct cx88_buffer*)fh->vidq.read_buf;
880		if (NULL == buf)
881			goto done;
882	}
883	poll_wait(file, &buf->vb.done, wait);
884	if (buf->vb.state == VIDEOBUF_DONE ||
885	    buf->vb.state == VIDEOBUF_ERROR)
886		rc = POLLIN|POLLRDNORM;
887	else
888		rc = 0;
889done:
890	mutex_unlock(&fh->vidq.vb_lock);
891	return rc;
892}
893
894static int video_release(struct file *file)
895{
896	struct cx8800_fh  *fh  = file->private_data;
897	struct cx8800_dev *dev = fh->dev;
898
899	/* turn off overlay */
900	if (res_check(fh, RESOURCE_OVERLAY)) {
901		res_free(dev,fh,RESOURCE_OVERLAY);
902	}
903
904	/* stop video capture */
905	if (res_check(fh, RESOURCE_VIDEO)) {
906		videobuf_queue_cancel(&fh->vidq);
907		res_free(dev,fh,RESOURCE_VIDEO);
908	}
909	if (fh->vidq.read_buf) {
910		buffer_release(&fh->vidq,fh->vidq.read_buf);
911		kfree(fh->vidq.read_buf);
912	}
913
914	/* stop vbi capture */
915	if (res_check(fh, RESOURCE_VBI)) {
916		videobuf_stop(&fh->vbiq);
917		res_free(dev,fh,RESOURCE_VBI);
918	}
919
920	videobuf_mmap_free(&fh->vidq);
921	videobuf_mmap_free(&fh->vbiq);
922	file->private_data = NULL;
923	kfree(fh);
924
925	mutex_lock(&dev->core->lock);
926	if(atomic_dec_and_test(&dev->core->users))
927		call_all(dev->core, core, s_power, 0);
928	mutex_unlock(&dev->core->lock);
929
930	return 0;
931}
932
933static int
934video_mmap(struct file *file, struct vm_area_struct * vma)
935{
936	struct cx8800_fh *fh = file->private_data;
937
938	return videobuf_mmap_mapper(get_queue(fh), vma);
939}
940
941/* ------------------------------------------------------------------ */
942/* VIDEO CTRL IOCTLS                                                  */
943
944int cx88_get_control (struct cx88_core  *core, struct v4l2_control *ctl)
945{
946	struct cx88_ctrl  *c    = NULL;
947	u32 value;
948	int i;
949
950	for (i = 0; i < CX8800_CTLS; i++)
951		if (cx8800_ctls[i].v.id == ctl->id)
952			c = &cx8800_ctls[i];
953	if (unlikely(NULL == c))
954		return -EINVAL;
955
956	value = c->sreg ? cx_sread(c->sreg) : cx_read(c->reg);
957	switch (ctl->id) {
958	case V4L2_CID_AUDIO_BALANCE:
959		ctl->value = ((value & 0x7f) < 0x40) ? ((value & 0x7f) + 0x40)
960					: (0x7f - (value & 0x7f));
961		break;
962	case V4L2_CID_AUDIO_VOLUME:
963		ctl->value = 0x3f - (value & 0x3f);
964		break;
965	default:
966		ctl->value = ((value + (c->off << c->shift)) & c->mask) >> c->shift;
967		break;
968	}
969	dprintk(1,"get_control id=0x%X(%s) ctrl=0x%02x, reg=0x%02x val=0x%02x (mask 0x%02x)%s\n",
970				ctl->id, c->v.name, ctl->value, c->reg,
971				value,c->mask, c->sreg ? " [shadowed]" : "");
972	return 0;
973}
974EXPORT_SYMBOL(cx88_get_control);
975
976int cx88_set_control(struct cx88_core *core, struct v4l2_control *ctl)
977{
978	struct cx88_ctrl *c = NULL;
979	u32 value,mask;
980	int i;
981
982	for (i = 0; i < CX8800_CTLS; i++) {
983		if (cx8800_ctls[i].v.id == ctl->id) {
984			c = &cx8800_ctls[i];
985		}
986	}
987	if (unlikely(NULL == c))
988		return -EINVAL;
989
990	if (ctl->value < c->v.minimum)
991		ctl->value = c->v.minimum;
992	if (ctl->value > c->v.maximum)
993		ctl->value = c->v.maximum;
994	mask=c->mask;
995	switch (ctl->id) {
996	case V4L2_CID_AUDIO_BALANCE:
997		value = (ctl->value < 0x40) ? (0x7f - ctl->value) : (ctl->value - 0x40);
998		break;
999	case V4L2_CID_AUDIO_VOLUME:
1000		value = 0x3f - (ctl->value & 0x3f);
1001		break;
1002	case V4L2_CID_SATURATION:
1003		/* special v_sat handling */
1004
1005		value = ((ctl->value - c->off) << c->shift) & c->mask;
1006
1007		if (core->tvnorm & V4L2_STD_SECAM) {
1008			/* For SECAM, both U and V sat should be equal */
1009			value=value<<8|value;
1010		} else {
1011			/* Keeps U Saturation proportional to V Sat */
1012			value=(value*0x5a)/0x7f<<8|value;
1013		}
1014		mask=0xffff;
1015		break;
1016	case V4L2_CID_CHROMA_AGC:
1017		/* Do not allow chroma AGC to be enabled for SECAM */
1018		value = ((ctl->value - c->off) << c->shift) & c->mask;
1019		if (core->tvnorm & V4L2_STD_SECAM && value)
1020			return -EINVAL;
1021		break;
1022	default:
1023		value = ((ctl->value - c->off) << c->shift) & c->mask;
1024		break;
1025	}
1026	dprintk(1,"set_control id=0x%X(%s) ctrl=0x%02x, reg=0x%02x val=0x%02x (mask 0x%02x)%s\n",
1027				ctl->id, c->v.name, ctl->value, c->reg, value,
1028				mask, c->sreg ? " [shadowed]" : "");
1029	if (c->sreg) {
1030		cx_sandor(c->sreg, c->reg, mask, value);
1031	} else {
1032		cx_andor(c->reg, mask, value);
1033	}
1034	return 0;
1035}
1036EXPORT_SYMBOL(cx88_set_control);
1037
1038static void init_controls(struct cx88_core *core)
1039{
1040	struct v4l2_control ctrl;
1041	int i;
1042
1043	for (i = 0; i < CX8800_CTLS; i++) {
1044		ctrl.id=cx8800_ctls[i].v.id;
1045		ctrl.value=cx8800_ctls[i].v.default_value;
1046
1047		cx88_set_control(core, &ctrl);
1048	}
1049}
1050
1051/* ------------------------------------------------------------------ */
1052/* VIDEO IOCTLS                                                       */
1053
1054static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
1055					struct v4l2_format *f)
1056{
1057	struct cx8800_fh  *fh   = priv;
1058
1059	f->fmt.pix.width        = fh->width;
1060	f->fmt.pix.height       = fh->height;
1061	f->fmt.pix.field        = fh->vidq.field;
1062	f->fmt.pix.pixelformat  = fh->fmt->fourcc;
1063	f->fmt.pix.bytesperline =
1064		(f->fmt.pix.width * fh->fmt->depth) >> 3;
1065	f->fmt.pix.sizeimage =
1066		f->fmt.pix.height * f->fmt.pix.bytesperline;
1067	return 0;
1068}
1069
1070static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
1071			struct v4l2_format *f)
1072{
1073	struct cx88_core  *core = ((struct cx8800_fh *)priv)->dev->core;
1074	struct cx8800_fmt *fmt;
1075	enum v4l2_field   field;
1076	unsigned int      maxw, maxh;
1077
1078	fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1079	if (NULL == fmt)
1080		return -EINVAL;
1081
1082	field = f->fmt.pix.field;
1083	maxw  = norm_maxw(core->tvnorm);
1084	maxh  = norm_maxh(core->tvnorm);
1085
1086	if (V4L2_FIELD_ANY == field) {
1087		field = (f->fmt.pix.height > maxh/2)
1088			? V4L2_FIELD_INTERLACED
1089			: V4L2_FIELD_BOTTOM;
1090	}
1091
1092	switch (field) {
1093	case V4L2_FIELD_TOP:
1094	case V4L2_FIELD_BOTTOM:
1095		maxh = maxh / 2;
1096		break;
1097	case V4L2_FIELD_INTERLACED:
1098		break;
1099	default:
1100		return -EINVAL;
1101	}
1102
1103	f->fmt.pix.field = field;
1104	v4l_bound_align_image(&f->fmt.pix.width, 48, maxw, 2,
1105			      &f->fmt.pix.height, 32, maxh, 0, 0);
1106	f->fmt.pix.bytesperline =
1107		(f->fmt.pix.width * fmt->depth) >> 3;
1108	f->fmt.pix.sizeimage =
1109		f->fmt.pix.height * f->fmt.pix.bytesperline;
1110
1111	return 0;
1112}
1113
1114static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
1115					struct v4l2_format *f)
1116{
1117	struct cx8800_fh  *fh   = priv;
1118	int err = vidioc_try_fmt_vid_cap (file,priv,f);
1119
1120	if (0 != err)
1121		return err;
1122	fh->fmt        = format_by_fourcc(f->fmt.pix.pixelformat);
1123	fh->width      = f->fmt.pix.width;
1124	fh->height     = f->fmt.pix.height;
1125	fh->vidq.field = f->fmt.pix.field;
1126	return 0;
1127}
1128
1129static int vidioc_querycap (struct file *file, void  *priv,
1130					struct v4l2_capability *cap)
1131{
1132	struct cx8800_dev *dev  = ((struct cx8800_fh *)priv)->dev;
1133	struct cx88_core  *core = dev->core;
1134
1135	strcpy(cap->driver, "cx8800");
1136	strlcpy(cap->card, core->board.name, sizeof(cap->card));
1137	sprintf(cap->bus_info,"PCI:%s",pci_name(dev->pci));
1138	cap->version = CX88_VERSION_CODE;
1139	cap->capabilities =
1140		V4L2_CAP_VIDEO_CAPTURE |
1141		V4L2_CAP_READWRITE     |
1142		V4L2_CAP_STREAMING     |
1143		V4L2_CAP_VBI_CAPTURE;
1144	if (UNSET != core->board.tuner_type)
1145		cap->capabilities |= V4L2_CAP_TUNER;
1146	return 0;
1147}
1148
1149static int vidioc_enum_fmt_vid_cap (struct file *file, void  *priv,
1150					struct v4l2_fmtdesc *f)
1151{
1152	if (unlikely(f->index >= ARRAY_SIZE(formats)))
1153		return -EINVAL;
1154
1155	strlcpy(f->description,formats[f->index].name,sizeof(f->description));
1156	f->pixelformat = formats[f->index].fourcc;
1157
1158	return 0;
1159}
1160
1161#ifdef CONFIG_VIDEO_V4L1_COMPAT
1162static int vidiocgmbuf (struct file *file, void *priv, struct video_mbuf *mbuf)
1163{
1164	struct cx8800_fh           *fh  = priv;
1165
1166	return videobuf_cgmbuf (get_queue(fh), mbuf, 8);
1167}
1168#endif
1169
1170static int vidioc_reqbufs (struct file *file, void *priv, struct v4l2_requestbuffers *p)
1171{
1172	struct cx8800_fh  *fh   = priv;
1173	return (videobuf_reqbufs(get_queue(fh), p));
1174}
1175
1176static int vidioc_querybuf (struct file *file, void *priv, struct v4l2_buffer *p)
1177{
1178	struct cx8800_fh  *fh   = priv;
1179	return (videobuf_querybuf(get_queue(fh), p));
1180}
1181
1182static int vidioc_qbuf (struct file *file, void *priv, struct v4l2_buffer *p)
1183{
1184	struct cx8800_fh  *fh   = priv;
1185	return (videobuf_qbuf(get_queue(fh), p));
1186}
1187
1188static int vidioc_dqbuf (struct file *file, void *priv, struct v4l2_buffer *p)
1189{
1190	struct cx8800_fh  *fh   = priv;
1191	return (videobuf_dqbuf(get_queue(fh), p,
1192				file->f_flags & O_NONBLOCK));
1193}
1194
1195static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
1196{
1197	struct cx8800_fh  *fh   = priv;
1198	struct cx8800_dev *dev  = fh->dev;
1199
1200	/* We should remember that this driver also supports teletext,  */
1201	/* so we have to test if the v4l2_buf_type is VBI capture data. */
1202	if (unlikely((fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) &&
1203		     (fh->type != V4L2_BUF_TYPE_VBI_CAPTURE)))
1204		return -EINVAL;
1205
1206	if (unlikely(i != fh->type))
1207		return -EINVAL;
1208
1209	if (unlikely(!res_get(dev,fh,get_ressource(fh))))
1210		return -EBUSY;
1211	return videobuf_streamon(get_queue(fh));
1212}
1213
1214static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
1215{
1216	struct cx8800_fh  *fh   = priv;
1217	struct cx8800_dev *dev  = fh->dev;
1218	int               err, res;
1219
1220	if ((fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) &&
1221	    (fh->type != V4L2_BUF_TYPE_VBI_CAPTURE))
1222		return -EINVAL;
1223
1224	if (i != fh->type)
1225		return -EINVAL;
1226
1227	res = get_ressource(fh);
1228	err = videobuf_streamoff(get_queue(fh));
1229	if (err < 0)
1230		return err;
1231	res_free(dev,fh,res);
1232	return 0;
1233}
1234
1235static int vidioc_s_std (struct file *file, void *priv, v4l2_std_id *tvnorms)
1236{
1237	struct cx88_core  *core = ((struct cx8800_fh *)priv)->dev->core;
1238
1239	mutex_lock(&core->lock);
1240	cx88_set_tvnorm(core,*tvnorms);
1241	mutex_unlock(&core->lock);
1242
1243	return 0;
1244}
1245
1246/* only one input in this sample driver */
1247int cx88_enum_input (struct cx88_core  *core,struct v4l2_input *i)
1248{
1249	static const char *iname[] = {
1250		[ CX88_VMUX_COMPOSITE1 ] = "Composite1",
1251		[ CX88_VMUX_COMPOSITE2 ] = "Composite2",
1252		[ CX88_VMUX_COMPOSITE3 ] = "Composite3",
1253		[ CX88_VMUX_COMPOSITE4 ] = "Composite4",
1254		[ CX88_VMUX_SVIDEO     ] = "S-Video",
1255		[ CX88_VMUX_TELEVISION ] = "Television",
1256		[ CX88_VMUX_CABLE      ] = "Cable TV",
1257		[ CX88_VMUX_DVB        ] = "DVB",
1258		[ CX88_VMUX_DEBUG      ] = "for debug only",
1259	};
1260	unsigned int n = i->index;
1261
1262	if (n >= 4)
1263		return -EINVAL;
1264	if (0 == INPUT(n).type)
1265		return -EINVAL;
1266	i->type  = V4L2_INPUT_TYPE_CAMERA;
1267	strcpy(i->name,iname[INPUT(n).type]);
1268	if ((CX88_VMUX_TELEVISION == INPUT(n).type) ||
1269	    (CX88_VMUX_CABLE      == INPUT(n).type))
1270		i->type = V4L2_INPUT_TYPE_TUNER;
1271		i->std = CX88_NORMS;
1272	return 0;
1273}
1274EXPORT_SYMBOL(cx88_enum_input);
1275
1276static int vidioc_enum_input (struct file *file, void *priv,
1277				struct v4l2_input *i)
1278{
1279	struct cx88_core  *core = ((struct cx8800_fh *)priv)->dev->core;
1280	return cx88_enum_input (core,i);
1281}
1282
1283static int vidioc_g_input (struct file *file, void *priv, unsigned int *i)
1284{
1285	struct cx88_core  *core = ((struct cx8800_fh *)priv)->dev->core;
1286
1287	*i = core->input;
1288	return 0;
1289}
1290
1291static int vidioc_s_input (struct file *file, void *priv, unsigned int i)
1292{
1293	struct cx88_core  *core = ((struct cx8800_fh *)priv)->dev->core;
1294
1295	if (i >= 4)
1296		return -EINVAL;
1297
1298	mutex_lock(&core->lock);
1299	cx88_newstation(core);
1300	cx88_video_mux(core,i);
1301	mutex_unlock(&core->lock);
1302	return 0;
1303}
1304
1305
1306
1307static int vidioc_queryctrl (struct file *file, void *priv,
1308				struct v4l2_queryctrl *qctrl)
1309{
1310	struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1311
1312	qctrl->id = v4l2_ctrl_next(ctrl_classes, qctrl->id);
1313	if (unlikely(qctrl->id == 0))
1314		return -EINVAL;
1315	return cx8800_ctrl_query(core, qctrl);
1316}
1317
1318static int vidioc_g_ctrl (struct file *file, void *priv,
1319				struct v4l2_control *ctl)
1320{
1321	struct cx88_core  *core = ((struct cx8800_fh *)priv)->dev->core;
1322	return
1323		cx88_get_control(core,ctl);
1324}
1325
1326static int vidioc_s_ctrl (struct file *file, void *priv,
1327				struct v4l2_control *ctl)
1328{
1329	struct cx88_core  *core = ((struct cx8800_fh *)priv)->dev->core;
1330	return
1331		cx88_set_control(core,ctl);
1332}
1333
1334static int vidioc_g_tuner (struct file *file, void *priv,
1335				struct v4l2_tuner *t)
1336{
1337	struct cx88_core  *core = ((struct cx8800_fh *)priv)->dev->core;
1338	u32 reg;
1339
1340	if (unlikely(UNSET == core->board.tuner_type))
1341		return -EINVAL;
1342	if (0 != t->index)
1343		return -EINVAL;
1344
1345	strcpy(t->name, "Television");
1346	t->type       = V4L2_TUNER_ANALOG_TV;
1347	t->capability = V4L2_TUNER_CAP_NORM;
1348	t->rangehigh  = 0xffffffffUL;
1349
1350	cx88_get_stereo(core ,t);
1351	reg = cx_read(MO_DEVICE_STATUS);
1352	t->signal = (reg & (1<<5)) ? 0xffff : 0x0000;
1353	return 0;
1354}
1355
1356static int vidioc_s_tuner (struct file *file, void *priv,
1357				struct v4l2_tuner *t)
1358{
1359	struct cx88_core  *core = ((struct cx8800_fh *)priv)->dev->core;
1360
1361	if (UNSET == core->board.tuner_type)
1362		return -EINVAL;
1363	if (0 != t->index)
1364		return -EINVAL;
1365
1366	cx88_set_stereo(core, t->audmode, 1);
1367	return 0;
1368}
1369
1370static int vidioc_g_frequency (struct file *file, void *priv,
1371				struct v4l2_frequency *f)
1372{
1373	struct cx8800_fh  *fh   = priv;
1374	struct cx88_core  *core = fh->dev->core;
1375
1376	if (unlikely(UNSET == core->board.tuner_type))
1377		return -EINVAL;
1378
1379	/* f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV; */
1380	f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1381	f->frequency = core->freq;
1382
1383	call_all(core, tuner, g_frequency, f);
1384
1385	return 0;
1386}
1387
1388int cx88_set_freq (struct cx88_core  *core,
1389				struct v4l2_frequency *f)
1390{
1391	if (unlikely(UNSET == core->board.tuner_type))
1392		return -EINVAL;
1393	if (unlikely(f->tuner != 0))
1394		return -EINVAL;
1395
1396	mutex_lock(&core->lock);
1397	core->freq = f->frequency;
1398	cx88_newstation(core);
1399	call_all(core, tuner, s_frequency, f);
1400
1401	/* When changing channels it is required to reset TVAUDIO */
1402	msleep (10);
1403	cx88_set_tvaudio(core);
1404
1405	mutex_unlock(&core->lock);
1406
1407	return 0;
1408}
1409EXPORT_SYMBOL(cx88_set_freq);
1410
1411static int vidioc_s_frequency (struct file *file, void *priv,
1412				struct v4l2_frequency *f)
1413{
1414	struct cx8800_fh  *fh   = priv;
1415	struct cx88_core  *core = fh->dev->core;
1416
1417	if (unlikely(0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV))
1418		return -EINVAL;
1419	if (unlikely(1 == fh->radio && f->type != V4L2_TUNER_RADIO))
1420		return -EINVAL;
1421
1422	return
1423		cx88_set_freq (core,f);
1424}
1425
1426#ifdef CONFIG_VIDEO_ADV_DEBUG
1427static int vidioc_g_register (struct file *file, void *fh,
1428				struct v4l2_dbg_register *reg)
1429{
1430	struct cx88_core *core = ((struct cx8800_fh*)fh)->dev->core;
1431
1432	if (!v4l2_chip_match_host(&reg->match))
1433		return -EINVAL;
1434	/* cx2388x has a 24-bit register space */
1435	reg->val = cx_read(reg->reg & 0xffffff);
1436	reg->size = 4;
1437	return 0;
1438}
1439
1440static int vidioc_s_register (struct file *file, void *fh,
1441				struct v4l2_dbg_register *reg)
1442{
1443	struct cx88_core *core = ((struct cx8800_fh*)fh)->dev->core;
1444
1445	if (!v4l2_chip_match_host(&reg->match))
1446		return -EINVAL;
1447	cx_write(reg->reg & 0xffffff, reg->val);
1448	return 0;
1449}
1450#endif
1451
1452/* ----------------------------------------------------------- */
1453/* RADIO ESPECIFIC IOCTLS                                      */
1454/* ----------------------------------------------------------- */
1455
1456static int radio_querycap (struct file *file, void  *priv,
1457					struct v4l2_capability *cap)
1458{
1459	struct cx8800_dev *dev  = ((struct cx8800_fh *)priv)->dev;
1460	struct cx88_core  *core = dev->core;
1461
1462	strcpy(cap->driver, "cx8800");
1463	strlcpy(cap->card, core->board.name, sizeof(cap->card));
1464	sprintf(cap->bus_info,"PCI:%s", pci_name(dev->pci));
1465	cap->version = CX88_VERSION_CODE;
1466	cap->capabilities = V4L2_CAP_TUNER;
1467	return 0;
1468}
1469
1470static int radio_g_tuner (struct file *file, void *priv,
1471				struct v4l2_tuner *t)
1472{
1473	struct cx88_core  *core = ((struct cx8800_fh *)priv)->dev->core;
1474
1475	if (unlikely(t->index > 0))
1476		return -EINVAL;
1477
1478	strcpy(t->name, "Radio");
1479	t->type = V4L2_TUNER_RADIO;
1480
1481	call_all(core, tuner, g_tuner, t);
1482	return 0;
1483}
1484
1485static int radio_enum_input (struct file *file, void *priv,
1486				struct v4l2_input *i)
1487{
1488	if (i->index != 0)
1489		return -EINVAL;
1490	strcpy(i->name,"Radio");
1491	i->type = V4L2_INPUT_TYPE_TUNER;
1492
1493	return 0;
1494}
1495
1496static int radio_g_audio (struct file *file, void *priv, struct v4l2_audio *a)
1497{
1498	if (unlikely(a->index))
1499		return -EINVAL;
1500
1501	strcpy(a->name,"Radio");
1502	return 0;
1503}
1504
1505
1506static int radio_s_tuner (struct file *file, void *priv,
1507				struct v4l2_tuner *t)
1508{
1509	struct cx88_core  *core = ((struct cx8800_fh *)priv)->dev->core;
1510
1511	if (0 != t->index)
1512		return -EINVAL;
1513
1514	call_all(core, tuner, s_tuner, t);
1515
1516	return 0;
1517}
1518
1519static int radio_s_audio (struct file *file, void *fh,
1520			  struct v4l2_audio *a)
1521{
1522	return 0;
1523}
1524
1525static int radio_s_input (struct file *file, void *fh, unsigned int i)
1526{
1527	return 0;
1528}
1529
1530static int radio_queryctrl (struct file *file, void *priv,
1531			    struct v4l2_queryctrl *c)
1532{
1533	int i;
1534
1535	if (c->id <  V4L2_CID_BASE ||
1536		c->id >= V4L2_CID_LASTP1)
1537		return -EINVAL;
1538	if (c->id == V4L2_CID_AUDIO_MUTE) {
1539		for (i = 0; i < CX8800_CTLS; i++) {
1540			if (cx8800_ctls[i].v.id == c->id)
1541				break;
1542		}
1543		if (i == CX8800_CTLS)
1544			return -EINVAL;
1545		*c = cx8800_ctls[i].v;
1546	} else
1547		*c = no_ctl;
1548	return 0;
1549}
1550
1551/* ----------------------------------------------------------- */
1552
1553static void cx8800_vid_timeout(unsigned long data)
1554{
1555	struct cx8800_dev *dev = (struct cx8800_dev*)data;
1556	struct cx88_core *core = dev->core;
1557	struct cx88_dmaqueue *q = &dev->vidq;
1558	struct cx88_buffer *buf;
1559	unsigned long flags;
1560
1561	cx88_sram_channel_dump(core, &cx88_sram_channels[SRAM_CH21]);
1562
1563	cx_clear(MO_VID_DMACNTRL, 0x11);
1564	cx_clear(VID_CAPTURE_CONTROL, 0x06);
1565
1566	spin_lock_irqsave(&dev->slock,flags);
1567	while (!list_empty(&q->active)) {
1568		buf = list_entry(q->active.next, struct cx88_buffer, vb.queue);
1569		list_del(&buf->vb.queue);
1570		buf->vb.state = VIDEOBUF_ERROR;
1571		wake_up(&buf->vb.done);
1572		printk("%s/0: [%p/%d] timeout - dma=0x%08lx\n", core->name,
1573		       buf, buf->vb.i, (unsigned long)buf->risc.dma);
1574	}
1575	restart_video_queue(dev,q);
1576	spin_unlock_irqrestore(&dev->slock,flags);
1577}
1578
1579static char *cx88_vid_irqs[32] = {
1580	"y_risci1", "u_risci1", "v_risci1", "vbi_risc1",
1581	"y_risci2", "u_risci2", "v_risci2", "vbi_risc2",
1582	"y_oflow",  "u_oflow",  "v_oflow",  "vbi_oflow",
1583	"y_sync",   "u_sync",   "v_sync",   "vbi_sync",
1584	"opc_err",  "par_err",  "rip_err",  "pci_abort",
1585};
1586
1587static void cx8800_vid_irq(struct cx8800_dev *dev)
1588{
1589	struct cx88_core *core = dev->core;
1590	u32 status, mask, count;
1591
1592	status = cx_read(MO_VID_INTSTAT);
1593	mask   = cx_read(MO_VID_INTMSK);
1594	if (0 == (status & mask))
1595		return;
1596	cx_write(MO_VID_INTSTAT, status);
1597	if (irq_debug  ||  (status & mask & ~0xff))
1598		cx88_print_irqbits(core->name, "irq vid",
1599				   cx88_vid_irqs, ARRAY_SIZE(cx88_vid_irqs),
1600				   status, mask);
1601
1602	/* risc op code error */
1603	if (status & (1 << 16)) {
1604		printk(KERN_WARNING "%s/0: video risc op code error\n",core->name);
1605		cx_clear(MO_VID_DMACNTRL, 0x11);
1606		cx_clear(VID_CAPTURE_CONTROL, 0x06);
1607		cx88_sram_channel_dump(core, &cx88_sram_channels[SRAM_CH21]);
1608	}
1609
1610	/* risc1 y */
1611	if (status & 0x01) {
1612		spin_lock(&dev->slock);
1613		count = cx_read(MO_VIDY_GPCNT);
1614		cx88_wakeup(core, &dev->vidq, count);
1615		spin_unlock(&dev->slock);
1616	}
1617
1618	/* risc1 vbi */
1619	if (status & 0x08) {
1620		spin_lock(&dev->slock);
1621		count = cx_read(MO_VBI_GPCNT);
1622		cx88_wakeup(core, &dev->vbiq, count);
1623		spin_unlock(&dev->slock);
1624	}
1625
1626	/* risc2 y */
1627	if (status & 0x10) {
1628		dprintk(2,"stopper video\n");
1629		spin_lock(&dev->slock);
1630		restart_video_queue(dev,&dev->vidq);
1631		spin_unlock(&dev->slock);
1632	}
1633
1634	/* risc2 vbi */
1635	if (status & 0x80) {
1636		dprintk(2,"stopper vbi\n");
1637		spin_lock(&dev->slock);
1638		cx8800_restart_vbi_queue(dev,&dev->vbiq);
1639		spin_unlock(&dev->slock);
1640	}
1641}
1642
1643static irqreturn_t cx8800_irq(int irq, void *dev_id)
1644{
1645	struct cx8800_dev *dev = dev_id;
1646	struct cx88_core *core = dev->core;
1647	u32 status;
1648	int loop, handled = 0;
1649
1650	for (loop = 0; loop < 10; loop++) {
1651		status = cx_read(MO_PCI_INTSTAT) &
1652			(core->pci_irqmask | PCI_INT_VIDINT);
1653		if (0 == status)
1654			goto out;
1655		cx_write(MO_PCI_INTSTAT, status);
1656		handled = 1;
1657
1658		if (status & core->pci_irqmask)
1659			cx88_core_irq(core,status);
1660		if (status & PCI_INT_VIDINT)
1661			cx8800_vid_irq(dev);
1662	};
1663	if (10 == loop) {
1664		printk(KERN_WARNING "%s/0: irq loop -- clearing mask\n",
1665		       core->name);
1666		cx_write(MO_PCI_INTMSK,0);
1667	}
1668
1669 out:
1670	return IRQ_RETVAL(handled);
1671}
1672
1673/* ----------------------------------------------------------- */
1674/* exported stuff                                              */
1675
1676static const struct v4l2_file_operations video_fops =
1677{
1678	.owner	       = THIS_MODULE,
1679	.open	       = video_open,
1680	.release       = video_release,
1681	.read	       = video_read,
1682	.poll          = video_poll,
1683	.mmap	       = video_mmap,
1684	.ioctl	       = video_ioctl2,
1685};
1686
1687static const struct v4l2_ioctl_ops video_ioctl_ops = {
1688	.vidioc_querycap      = vidioc_querycap,
1689	.vidioc_enum_fmt_vid_cap  = vidioc_enum_fmt_vid_cap,
1690	.vidioc_g_fmt_vid_cap     = vidioc_g_fmt_vid_cap,
1691	.vidioc_try_fmt_vid_cap   = vidioc_try_fmt_vid_cap,
1692	.vidioc_s_fmt_vid_cap     = vidioc_s_fmt_vid_cap,
1693	.vidioc_g_fmt_vbi_cap     = cx8800_vbi_fmt,
1694	.vidioc_try_fmt_vbi_cap   = cx8800_vbi_fmt,
1695	.vidioc_s_fmt_vbi_cap     = cx8800_vbi_fmt,
1696	.vidioc_reqbufs       = vidioc_reqbufs,
1697	.vidioc_querybuf      = vidioc_querybuf,
1698	.vidioc_qbuf          = vidioc_qbuf,
1699	.vidioc_dqbuf         = vidioc_dqbuf,
1700	.vidioc_s_std         = vidioc_s_std,
1701	.vidioc_enum_input    = vidioc_enum_input,
1702	.vidioc_g_input       = vidioc_g_input,
1703	.vidioc_s_input       = vidioc_s_input,
1704	.vidioc_queryctrl     = vidioc_queryctrl,
1705	.vidioc_g_ctrl        = vidioc_g_ctrl,
1706	.vidioc_s_ctrl        = vidioc_s_ctrl,
1707	.vidioc_streamon      = vidioc_streamon,
1708	.vidioc_streamoff     = vidioc_streamoff,
1709#ifdef CONFIG_VIDEO_V4L1_COMPAT
1710	.vidiocgmbuf          = vidiocgmbuf,
1711#endif
1712	.vidioc_g_tuner       = vidioc_g_tuner,
1713	.vidioc_s_tuner       = vidioc_s_tuner,
1714	.vidioc_g_frequency   = vidioc_g_frequency,
1715	.vidioc_s_frequency   = vidioc_s_frequency,
1716#ifdef CONFIG_VIDEO_ADV_DEBUG
1717	.vidioc_g_register    = vidioc_g_register,
1718	.vidioc_s_register    = vidioc_s_register,
1719#endif
1720};
1721
1722static struct video_device cx8800_vbi_template;
1723
1724static struct video_device cx8800_video_template = {
1725	.name                 = "cx8800-video",
1726	.fops                 = &video_fops,
1727	.ioctl_ops 	      = &video_ioctl_ops,
1728	.tvnorms              = CX88_NORMS,
1729	.current_norm         = V4L2_STD_NTSC_M,
1730};
1731
1732static const struct v4l2_file_operations radio_fops =
1733{
1734	.owner         = THIS_MODULE,
1735	.open          = video_open,
1736	.release       = video_release,
1737	.ioctl         = video_ioctl2,
1738};
1739
1740static const struct v4l2_ioctl_ops radio_ioctl_ops = {
1741	.vidioc_querycap      = radio_querycap,
1742	.vidioc_g_tuner       = radio_g_tuner,
1743	.vidioc_enum_input    = radio_enum_input,
1744	.vidioc_g_audio       = radio_g_audio,
1745	.vidioc_s_tuner       = radio_s_tuner,
1746	.vidioc_s_audio       = radio_s_audio,
1747	.vidioc_s_input       = radio_s_input,
1748	.vidioc_queryctrl     = radio_queryctrl,
1749	.vidioc_g_ctrl        = vidioc_g_ctrl,
1750	.vidioc_s_ctrl        = vidioc_s_ctrl,
1751	.vidioc_g_frequency   = vidioc_g_frequency,
1752	.vidioc_s_frequency   = vidioc_s_frequency,
1753#ifdef CONFIG_VIDEO_ADV_DEBUG
1754	.vidioc_g_register    = vidioc_g_register,
1755	.vidioc_s_register    = vidioc_s_register,
1756#endif
1757};
1758
1759static struct video_device cx8800_radio_template = {
1760	.name                 = "cx8800-radio",
1761	.fops                 = &radio_fops,
1762	.ioctl_ops 	      = &radio_ioctl_ops,
1763};
1764
1765/* ----------------------------------------------------------- */
1766
1767static void cx8800_unregister_video(struct cx8800_dev *dev)
1768{
1769	if (dev->radio_dev) {
1770		if (video_is_registered(dev->radio_dev))
1771			video_unregister_device(dev->radio_dev);
1772		else
1773			video_device_release(dev->radio_dev);
1774		dev->radio_dev = NULL;
1775	}
1776	if (dev->vbi_dev) {
1777		if (video_is_registered(dev->vbi_dev))
1778			video_unregister_device(dev->vbi_dev);
1779		else
1780			video_device_release(dev->vbi_dev);
1781		dev->vbi_dev = NULL;
1782	}
1783	if (dev->video_dev) {
1784		if (video_is_registered(dev->video_dev))
1785			video_unregister_device(dev->video_dev);
1786		else
1787			video_device_release(dev->video_dev);
1788		dev->video_dev = NULL;
1789	}
1790}
1791
1792static int __devinit cx8800_initdev(struct pci_dev *pci_dev,
1793				    const struct pci_device_id *pci_id)
1794{
1795	struct cx8800_dev *dev;
1796	struct cx88_core *core;
1797
1798	int err;
1799
1800	dev = kzalloc(sizeof(*dev),GFP_KERNEL);
1801	if (NULL == dev)
1802		return -ENOMEM;
1803
1804	/* pci init */
1805	dev->pci = pci_dev;
1806	if (pci_enable_device(pci_dev)) {
1807		err = -EIO;
1808		goto fail_free;
1809	}
1810	core = cx88_core_get(dev->pci);
1811	if (NULL == core) {
1812		err = -EINVAL;
1813		goto fail_free;
1814	}
1815	dev->core = core;
1816
1817	/* print pci info */
1818	pci_read_config_byte(pci_dev, PCI_CLASS_REVISION, &dev->pci_rev);
1819	pci_read_config_byte(pci_dev, PCI_LATENCY_TIMER,  &dev->pci_lat);
1820	printk(KERN_INFO "%s/0: found at %s, rev: %d, irq: %d, "
1821	       "latency: %d, mmio: 0x%llx\n", core->name,
1822	       pci_name(pci_dev), dev->pci_rev, pci_dev->irq,
1823	       dev->pci_lat,(unsigned long long)pci_resource_start(pci_dev,0));
1824
1825	pci_set_master(pci_dev);
1826	if (!pci_dma_supported(pci_dev,DMA_BIT_MASK(32))) {
1827		printk("%s/0: Oops: no 32bit PCI DMA ???\n",core->name);
1828		err = -EIO;
1829		goto fail_core;
1830	}
1831
1832	/* Initialize VBI template */
1833	memcpy( &cx8800_vbi_template, &cx8800_video_template,
1834		sizeof(cx8800_vbi_template) );
1835	strcpy(cx8800_vbi_template.name,"cx8800-vbi");
1836
1837	/* initialize driver struct */
1838	spin_lock_init(&dev->slock);
1839	core->tvnorm = cx8800_video_template.current_norm;
1840
1841	/* init video dma queues */
1842	INIT_LIST_HEAD(&dev->vidq.active);
1843	INIT_LIST_HEAD(&dev->vidq.queued);
1844	dev->vidq.timeout.function = cx8800_vid_timeout;
1845	dev->vidq.timeout.data     = (unsigned long)dev;
1846	init_timer(&dev->vidq.timeout);
1847	cx88_risc_stopper(dev->pci,&dev->vidq.stopper,
1848			  MO_VID_DMACNTRL,0x11,0x00);
1849
1850	/* init vbi dma queues */
1851	INIT_LIST_HEAD(&dev->vbiq.active);
1852	INIT_LIST_HEAD(&dev->vbiq.queued);
1853	dev->vbiq.timeout.function = cx8800_vbi_timeout;
1854	dev->vbiq.timeout.data     = (unsigned long)dev;
1855	init_timer(&dev->vbiq.timeout);
1856	cx88_risc_stopper(dev->pci,&dev->vbiq.stopper,
1857			  MO_VID_DMACNTRL,0x88,0x00);
1858
1859	/* get irq */
1860	err = request_irq(pci_dev->irq, cx8800_irq,
1861			  IRQF_SHARED | IRQF_DISABLED, core->name, dev);
1862	if (err < 0) {
1863		printk(KERN_ERR "%s/0: can't get IRQ %d\n",
1864		       core->name,pci_dev->irq);
1865		goto fail_core;
1866	}
1867	cx_set(MO_PCI_INTMSK, core->pci_irqmask);
1868
1869	/* load and configure helper modules */
1870
1871	if (core->board.audio_chip == V4L2_IDENT_WM8775)
1872		v4l2_i2c_new_subdev(&core->v4l2_dev, &core->i2c_adap,
1873				"wm8775", "wm8775", 0x36 >> 1, NULL);
1874
1875	if (core->board.audio_chip == V4L2_IDENT_TVAUDIO) {
1876		/* This probes for a tda9874 as is used on some
1877		   Pixelview Ultra boards. */
1878		v4l2_i2c_new_subdev(&core->v4l2_dev,
1879				&core->i2c_adap,
1880				"tvaudio", "tvaudio", 0, I2C_ADDRS(0xb0 >> 1));
1881	}
1882
1883	switch (core->boardnr) {
1884	case CX88_BOARD_DVICO_FUSIONHDTV_5_GOLD:
1885	case CX88_BOARD_DVICO_FUSIONHDTV_7_GOLD: {
1886		static struct i2c_board_info rtc_info = {
1887			I2C_BOARD_INFO("isl1208", 0x6f)
1888		};
1889
1890		request_module("rtc-isl1208");
1891		core->i2c_rtc = i2c_new_device(&core->i2c_adap, &rtc_info);
1892	}
1893		/* break intentionally omitted */
1894	case CX88_BOARD_DVICO_FUSIONHDTV_5_PCI_NANO:
1895		request_module("ir-kbd-i2c");
1896	}
1897
1898	/* register v4l devices */
1899	dev->video_dev = cx88_vdev_init(core,dev->pci,
1900					&cx8800_video_template,"video");
1901	video_set_drvdata(dev->video_dev, dev);
1902	err = video_register_device(dev->video_dev,VFL_TYPE_GRABBER,
1903				    video_nr[core->nr]);
1904	if (err < 0) {
1905		printk(KERN_ERR "%s/0: can't register video device\n",
1906		       core->name);
1907		goto fail_unreg;
1908	}
1909	printk(KERN_INFO "%s/0: registered device %s [v4l2]\n",
1910	       core->name, video_device_node_name(dev->video_dev));
1911
1912	dev->vbi_dev = cx88_vdev_init(core,dev->pci,&cx8800_vbi_template,"vbi");
1913	video_set_drvdata(dev->vbi_dev, dev);
1914	err = video_register_device(dev->vbi_dev,VFL_TYPE_VBI,
1915				    vbi_nr[core->nr]);
1916	if (err < 0) {
1917		printk(KERN_ERR "%s/0: can't register vbi device\n",
1918		       core->name);
1919		goto fail_unreg;
1920	}
1921	printk(KERN_INFO "%s/0: registered device %s\n",
1922	       core->name, video_device_node_name(dev->vbi_dev));
1923
1924	if (core->board.radio.type == CX88_RADIO) {
1925		dev->radio_dev = cx88_vdev_init(core,dev->pci,
1926						&cx8800_radio_template,"radio");
1927		video_set_drvdata(dev->radio_dev, dev);
1928		err = video_register_device(dev->radio_dev,VFL_TYPE_RADIO,
1929					    radio_nr[core->nr]);
1930		if (err < 0) {
1931			printk(KERN_ERR "%s/0: can't register radio device\n",
1932			       core->name);
1933			goto fail_unreg;
1934		}
1935		printk(KERN_INFO "%s/0: registered device %s\n",
1936		       core->name, video_device_node_name(dev->radio_dev));
1937	}
1938
1939	/* everything worked */
1940	pci_set_drvdata(pci_dev,dev);
1941
1942	/* initial device configuration */
1943	mutex_lock(&core->lock);
1944	cx88_set_tvnorm(core,core->tvnorm);
1945	init_controls(core);
1946	cx88_video_mux(core,0);
1947	mutex_unlock(&core->lock);
1948
1949	/* start tvaudio thread */
1950	if (core->board.tuner_type != TUNER_ABSENT) {
1951		core->kthread = kthread_run(cx88_audio_thread, core, "cx88 tvaudio");
1952		if (IS_ERR(core->kthread)) {
1953			err = PTR_ERR(core->kthread);
1954			printk(KERN_ERR "%s/0: failed to create cx88 audio thread, err=%d\n",
1955			       core->name, err);
1956		}
1957	}
1958	return 0;
1959
1960fail_unreg:
1961	cx8800_unregister_video(dev);
1962	free_irq(pci_dev->irq, dev);
1963fail_core:
1964	cx88_core_put(core,dev->pci);
1965fail_free:
1966	kfree(dev);
1967	return err;
1968}
1969
1970static void __devexit cx8800_finidev(struct pci_dev *pci_dev)
1971{
1972	struct cx8800_dev *dev = pci_get_drvdata(pci_dev);
1973	struct cx88_core *core = dev->core;
1974
1975	/* stop thread */
1976	if (core->kthread) {
1977		kthread_stop(core->kthread);
1978		core->kthread = NULL;
1979	}
1980
1981	if (core->ir)
1982		cx88_ir_stop(core);
1983
1984	cx88_shutdown(core);
1985	pci_disable_device(pci_dev);
1986
1987	/* unregister stuff */
1988
1989	free_irq(pci_dev->irq, dev);
1990	cx8800_unregister_video(dev);
1991	pci_set_drvdata(pci_dev, NULL);
1992
1993	/* free memory */
1994	btcx_riscmem_free(dev->pci,&dev->vidq.stopper);
1995	cx88_core_put(core,dev->pci);
1996	kfree(dev);
1997}
1998
1999#ifdef CONFIG_PM
2000static int cx8800_suspend(struct pci_dev *pci_dev, pm_message_t state)
2001{
2002	struct cx8800_dev *dev = pci_get_drvdata(pci_dev);
2003	struct cx88_core *core = dev->core;
2004
2005	/* stop video+vbi capture */
2006	spin_lock(&dev->slock);
2007	if (!list_empty(&dev->vidq.active)) {
2008		printk("%s/0: suspend video\n", core->name);
2009		stop_video_dma(dev);
2010		del_timer(&dev->vidq.timeout);
2011	}
2012	if (!list_empty(&dev->vbiq.active)) {
2013		printk("%s/0: suspend vbi\n", core->name);
2014		cx8800_stop_vbi_dma(dev);
2015		del_timer(&dev->vbiq.timeout);
2016	}
2017	spin_unlock(&dev->slock);
2018
2019	if (core->ir)
2020		cx88_ir_stop(core);
2021	cx88_shutdown(core);
2022
2023	pci_save_state(pci_dev);
2024	if (0 != pci_set_power_state(pci_dev, pci_choose_state(pci_dev, state))) {
2025		pci_disable_device(pci_dev);
2026		dev->state.disabled = 1;
2027	}
2028	return 0;
2029}
2030
2031static int cx8800_resume(struct pci_dev *pci_dev)
2032{
2033	struct cx8800_dev *dev = pci_get_drvdata(pci_dev);
2034	struct cx88_core *core = dev->core;
2035	int err;
2036
2037	if (dev->state.disabled) {
2038		err=pci_enable_device(pci_dev);
2039		if (err) {
2040			printk(KERN_ERR "%s/0: can't enable device\n",
2041			       core->name);
2042			return err;
2043		}
2044
2045		dev->state.disabled = 0;
2046	}
2047	err= pci_set_power_state(pci_dev, PCI_D0);
2048	if (err) {
2049		printk(KERN_ERR "%s/0: can't set power state\n", core->name);
2050		pci_disable_device(pci_dev);
2051		dev->state.disabled = 1;
2052
2053		return err;
2054	}
2055	pci_restore_state(pci_dev);
2056
2057	cx88_reset(core);
2058	if (core->ir)
2059		cx88_ir_start(core);
2060
2061	cx_set(MO_PCI_INTMSK, core->pci_irqmask);
2062
2063	/* restart video+vbi capture */
2064	spin_lock(&dev->slock);
2065	if (!list_empty(&dev->vidq.active)) {
2066		printk("%s/0: resume video\n", core->name);
2067		restart_video_queue(dev,&dev->vidq);
2068	}
2069	if (!list_empty(&dev->vbiq.active)) {
2070		printk("%s/0: resume vbi\n", core->name);
2071		cx8800_restart_vbi_queue(dev,&dev->vbiq);
2072	}
2073	spin_unlock(&dev->slock);
2074
2075	return 0;
2076}
2077#endif
2078
2079/* ----------------------------------------------------------- */
2080
2081static struct pci_device_id cx8800_pci_tbl[] = {
2082	{
2083		.vendor       = 0x14f1,
2084		.device       = 0x8800,
2085		.subvendor    = PCI_ANY_ID,
2086		.subdevice    = PCI_ANY_ID,
2087	},{
2088		/* --- end of list --- */
2089	}
2090};
2091MODULE_DEVICE_TABLE(pci, cx8800_pci_tbl);
2092
2093static struct pci_driver cx8800_pci_driver = {
2094	.name     = "cx8800",
2095	.id_table = cx8800_pci_tbl,
2096	.probe    = cx8800_initdev,
2097	.remove   = __devexit_p(cx8800_finidev),
2098#ifdef CONFIG_PM
2099	.suspend  = cx8800_suspend,
2100	.resume   = cx8800_resume,
2101#endif
2102};
2103
2104static int __init cx8800_init(void)
2105{
2106	printk(KERN_INFO "cx88/0: cx2388x v4l2 driver version %d.%d.%d loaded\n",
2107	       (CX88_VERSION_CODE >> 16) & 0xff,
2108	       (CX88_VERSION_CODE >>  8) & 0xff,
2109	       CX88_VERSION_CODE & 0xff);
2110#ifdef SNAPSHOT
2111	printk(KERN_INFO "cx2388x: snapshot date %04d-%02d-%02d\n",
2112	       SNAPSHOT/10000, (SNAPSHOT/100)%100, SNAPSHOT%100);
2113#endif
2114	return pci_register_driver(&cx8800_pci_driver);
2115}
2116
2117static void __exit cx8800_fini(void)
2118{
2119	pci_unregister_driver(&cx8800_pci_driver);
2120}
2121
2122module_init(cx8800_init);
2123module_exit(cx8800_fini);
2124
2125/* ----------------------------------------------------------- */
2126/*
2127 * Local variables:
2128 * c-basic-offset: 8
2129 * End:
2130 * kate: eol "unix"; indent-width 3; remove-trailing-space on; replace-trailing-space-save on; tab-width 8; replace-tabs off; space-indent off; mixed-indent off
2131 */
2132