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