Merge git://git.linux-nfs.org/pub/linux/nfs-2.6
[linux-2.6] / drivers / media / video / cx88 / cx88-video.c
1
2 /*
3  *
4  * device driver for Conexant 2388x based TV cards
5  * video4linux video interface
6  *
7  * (c) 2003-04 Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]
8  *
9  * (c) 2005-2006 Mauro Carvalho Chehab <mchehab@infradead.org>
10  *      - Multituner support
11  *      - video_ioctl2 conversion
12  *      - PAL/M fixes
13  *
14  *  This program is free software; you can redistribute it and/or modify
15  *  it under the terms of the GNU General Public License as published by
16  *  the Free Software Foundation; either version 2 of the License, or
17  *  (at your option) any later version.
18  *
19  *  This program is distributed in the hope that it will be useful,
20  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
21  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  *  GNU General Public License for more details.
23  *
24  *  You should have received a copy of the GNU General Public License
25  *  along with this program; if not, write to the Free Software
26  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27  */
28
29 #include <linux/init.h>
30 #include <linux/list.h>
31 #include <linux/module.h>
32 #include <linux/moduleparam.h>
33 #include <linux/kmod.h>
34 #include <linux/kernel.h>
35 #include <linux/slab.h>
36 #include <linux/interrupt.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
44 #ifdef CONFIG_VIDEO_V4L1_COMPAT
45 /* Include V4L1 specific functions. Should be removed soon */
46 #include <linux/videodev.h>
47 #endif
48
49 MODULE_DESCRIPTION("v4l2 driver module for cx2388x based TV cards");
50 MODULE_AUTHOR("Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]");
51 MODULE_LICENSE("GPL");
52
53 /* ------------------------------------------------------------------ */
54
55 static unsigned int video_nr[] = {[0 ... (CX88_MAXBOARDS - 1)] = UNSET };
56 static unsigned int vbi_nr[]   = {[0 ... (CX88_MAXBOARDS - 1)] = UNSET };
57 static unsigned int radio_nr[] = {[0 ... (CX88_MAXBOARDS - 1)] = UNSET };
58
59 module_param_array(video_nr, int, NULL, 0444);
60 module_param_array(vbi_nr,   int, NULL, 0444);
61 module_param_array(radio_nr, int, NULL, 0444);
62
63 MODULE_PARM_DESC(video_nr,"video device numbers");
64 MODULE_PARM_DESC(vbi_nr,"vbi device numbers");
65 MODULE_PARM_DESC(radio_nr,"radio device numbers");
66
67 static unsigned int video_debug = 0;
68 module_param(video_debug,int,0644);
69 MODULE_PARM_DESC(video_debug,"enable debug messages [video]");
70
71 static unsigned int irq_debug = 0;
72 module_param(irq_debug,int,0644);
73 MODULE_PARM_DESC(irq_debug,"enable debug messages [IRQ handler]");
74
75 static unsigned int vid_limit = 16;
76 module_param(vid_limit,int,0644);
77 MODULE_PARM_DESC(vid_limit,"capture memory limit in megabytes");
78
79 #define dprintk(level,fmt, arg...)      if (video_debug >= level) \
80         printk(KERN_DEBUG "%s/0: " fmt, core->name , ## arg)
81
82 /* ------------------------------------------------------------------ */
83
84 static LIST_HEAD(cx8800_devlist);
85
86 /* ------------------------------------------------------------------- */
87 /* static data                                                         */
88
89 static struct cx8800_fmt formats[] = {
90         {
91                 .name     = "8 bpp, gray",
92                 .fourcc   = V4L2_PIX_FMT_GREY,
93                 .cxformat = ColorFormatY8,
94                 .depth    = 8,
95                 .flags    = FORMAT_FLAGS_PACKED,
96         },{
97                 .name     = "15 bpp RGB, le",
98                 .fourcc   = V4L2_PIX_FMT_RGB555,
99                 .cxformat = ColorFormatRGB15,
100                 .depth    = 16,
101                 .flags    = FORMAT_FLAGS_PACKED,
102         },{
103                 .name     = "15 bpp RGB, be",
104                 .fourcc   = V4L2_PIX_FMT_RGB555X,
105                 .cxformat = ColorFormatRGB15 | ColorFormatBSWAP,
106                 .depth    = 16,
107                 .flags    = FORMAT_FLAGS_PACKED,
108         },{
109                 .name     = "16 bpp RGB, le",
110                 .fourcc   = V4L2_PIX_FMT_RGB565,
111                 .cxformat = ColorFormatRGB16,
112                 .depth    = 16,
113                 .flags    = FORMAT_FLAGS_PACKED,
114         },{
115                 .name     = "16 bpp RGB, be",
116                 .fourcc   = V4L2_PIX_FMT_RGB565X,
117                 .cxformat = ColorFormatRGB16 | ColorFormatBSWAP,
118                 .depth    = 16,
119                 .flags    = FORMAT_FLAGS_PACKED,
120         },{
121                 .name     = "24 bpp RGB, le",
122                 .fourcc   = V4L2_PIX_FMT_BGR24,
123                 .cxformat = ColorFormatRGB24,
124                 .depth    = 24,
125                 .flags    = FORMAT_FLAGS_PACKED,
126         },{
127                 .name     = "32 bpp RGB, le",
128                 .fourcc   = V4L2_PIX_FMT_BGR32,
129                 .cxformat = ColorFormatRGB32,
130                 .depth    = 32,
131                 .flags    = FORMAT_FLAGS_PACKED,
132         },{
133                 .name     = "32 bpp RGB, be",
134                 .fourcc   = V4L2_PIX_FMT_RGB32,
135                 .cxformat = ColorFormatRGB32 | ColorFormatBSWAP | ColorFormatWSWAP,
136                 .depth    = 32,
137                 .flags    = FORMAT_FLAGS_PACKED,
138         },{
139                 .name     = "4:2:2, packed, YUYV",
140                 .fourcc   = V4L2_PIX_FMT_YUYV,
141                 .cxformat = ColorFormatYUY2,
142                 .depth    = 16,
143                 .flags    = FORMAT_FLAGS_PACKED,
144         },{
145                 .name     = "4:2:2, packed, UYVY",
146                 .fourcc   = V4L2_PIX_FMT_UYVY,
147                 .cxformat = ColorFormatYUY2 | ColorFormatBSWAP,
148                 .depth    = 16,
149                 .flags    = FORMAT_FLAGS_PACKED,
150         },
151 };
152
153 static struct cx8800_fmt* format_by_fourcc(unsigned int fourcc)
154 {
155         unsigned int i;
156
157         for (i = 0; i < ARRAY_SIZE(formats); i++)
158                 if (formats[i].fourcc == fourcc)
159                         return formats+i;
160         return NULL;
161 }
162
163 /* ------------------------------------------------------------------- */
164
165 static const struct v4l2_queryctrl no_ctl = {
166         .name  = "42",
167         .flags = V4L2_CTRL_FLAG_DISABLED,
168 };
169
170 static struct cx88_ctrl cx8800_ctls[] = {
171         /* --- video --- */
172         {
173                 .v = {
174                         .id            = V4L2_CID_BRIGHTNESS,
175                         .name          = "Brightness",
176                         .minimum       = 0x00,
177                         .maximum       = 0xff,
178                         .step          = 1,
179                         .default_value = 0x7f,
180                         .type          = V4L2_CTRL_TYPE_INTEGER,
181                 },
182                 .off                   = 128,
183                 .reg                   = MO_CONTR_BRIGHT,
184                 .mask                  = 0x00ff,
185                 .shift                 = 0,
186         },{
187                 .v = {
188                         .id            = V4L2_CID_CONTRAST,
189                         .name          = "Contrast",
190                         .minimum       = 0,
191                         .maximum       = 0xff,
192                         .step          = 1,
193                         .default_value = 0x3f,
194                         .type          = V4L2_CTRL_TYPE_INTEGER,
195                 },
196                 .off                   = 0,
197                 .reg                   = MO_CONTR_BRIGHT,
198                 .mask                  = 0xff00,
199                 .shift                 = 8,
200         },{
201                 .v = {
202                         .id            = V4L2_CID_HUE,
203                         .name          = "Hue",
204                         .minimum       = 0,
205                         .maximum       = 0xff,
206                         .step          = 1,
207                         .default_value = 0x7f,
208                         .type          = V4L2_CTRL_TYPE_INTEGER,
209                 },
210                 .off                   = 128,
211                 .reg                   = MO_HUE,
212                 .mask                  = 0x00ff,
213                 .shift                 = 0,
214         },{
215                 /* strictly, this only describes only U saturation.
216                  * V saturation is handled specially through code.
217                  */
218                 .v = {
219                         .id            = V4L2_CID_SATURATION,
220                         .name          = "Saturation",
221                         .minimum       = 0,
222                         .maximum       = 0xff,
223                         .step          = 1,
224                         .default_value = 0x7f,
225                         .type          = V4L2_CTRL_TYPE_INTEGER,
226                 },
227                 .off                   = 0,
228                 .reg                   = MO_UV_SATURATION,
229                 .mask                  = 0x00ff,
230                 .shift                 = 0,
231         },{
232         /* --- audio --- */
233                 .v = {
234                         .id            = V4L2_CID_AUDIO_MUTE,
235                         .name          = "Mute",
236                         .minimum       = 0,
237                         .maximum       = 1,
238                         .default_value = 1,
239                         .type          = V4L2_CTRL_TYPE_BOOLEAN,
240                 },
241                 .reg                   = AUD_VOL_CTL,
242                 .sreg                  = SHADOW_AUD_VOL_CTL,
243                 .mask                  = (1 << 6),
244                 .shift                 = 6,
245         },{
246                 .v = {
247                         .id            = V4L2_CID_AUDIO_VOLUME,
248                         .name          = "Volume",
249                         .minimum       = 0,
250                         .maximum       = 0x3f,
251                         .step          = 1,
252                         .default_value = 0x3f,
253                         .type          = V4L2_CTRL_TYPE_INTEGER,
254                 },
255                 .reg                   = AUD_VOL_CTL,
256                 .sreg                  = SHADOW_AUD_VOL_CTL,
257                 .mask                  = 0x3f,
258                 .shift                 = 0,
259         },{
260                 .v = {
261                         .id            = V4L2_CID_AUDIO_BALANCE,
262                         .name          = "Balance",
263                         .minimum       = 0,
264                         .maximum       = 0x7f,
265                         .step          = 1,
266                         .default_value = 0x40,
267                         .type          = V4L2_CTRL_TYPE_INTEGER,
268                 },
269                 .reg                   = AUD_BAL_CTL,
270                 .sreg                  = SHADOW_AUD_BAL_CTL,
271                 .mask                  = 0x7f,
272                 .shift                 = 0,
273         }
274 };
275 static const int CX8800_CTLS = ARRAY_SIZE(cx8800_ctls);
276
277 const u32 cx88_user_ctrls[] = {
278         V4L2_CID_USER_CLASS,
279         V4L2_CID_BRIGHTNESS,
280         V4L2_CID_CONTRAST,
281         V4L2_CID_SATURATION,
282         V4L2_CID_HUE,
283         V4L2_CID_AUDIO_VOLUME,
284         V4L2_CID_AUDIO_BALANCE,
285         V4L2_CID_AUDIO_MUTE,
286         0
287 };
288 EXPORT_SYMBOL(cx88_user_ctrls);
289
290 static const u32 *ctrl_classes[] = {
291         cx88_user_ctrls,
292         NULL
293 };
294
295 int cx8800_ctrl_query(struct v4l2_queryctrl *qctrl)
296 {
297         int i;
298
299         if (qctrl->id < V4L2_CID_BASE ||
300             qctrl->id >= V4L2_CID_LASTP1)
301                 return -EINVAL;
302         for (i = 0; i < CX8800_CTLS; i++)
303                 if (cx8800_ctls[i].v.id == qctrl->id)
304                         break;
305         if (i == CX8800_CTLS) {
306                 *qctrl = no_ctl;
307                 return 0;
308         }
309         *qctrl = cx8800_ctls[i].v;
310         return 0;
311 }
312 EXPORT_SYMBOL(cx8800_ctrl_query);
313
314 /* ------------------------------------------------------------------- */
315 /* resource management                                                 */
316
317 static int res_get(struct cx8800_dev *dev, struct cx8800_fh *fh, unsigned int bit)
318 {
319         struct cx88_core *core = dev->core;
320         if (fh->resources & bit)
321                 /* have it already allocated */
322                 return 1;
323
324         /* is it free? */
325         mutex_lock(&core->lock);
326         if (dev->resources & bit) {
327                 /* no, someone else uses it */
328                 mutex_unlock(&core->lock);
329                 return 0;
330         }
331         /* it's free, grab it */
332         fh->resources  |= bit;
333         dev->resources |= bit;
334         dprintk(1,"res: get %d\n",bit);
335         mutex_unlock(&core->lock);
336         return 1;
337 }
338
339 static
340 int res_check(struct cx8800_fh *fh, unsigned int bit)
341 {
342         return (fh->resources & bit);
343 }
344
345 static
346 int res_locked(struct cx8800_dev *dev, unsigned int bit)
347 {
348         return (dev->resources & bit);
349 }
350
351 static
352 void res_free(struct cx8800_dev *dev, struct cx8800_fh *fh, unsigned int bits)
353 {
354         struct cx88_core *core = dev->core;
355         BUG_ON((fh->resources & bits) != bits);
356
357         mutex_lock(&core->lock);
358         fh->resources  &= ~bits;
359         dev->resources &= ~bits;
360         dprintk(1,"res: put %d\n",bits);
361         mutex_unlock(&core->lock);
362 }
363
364 /* ------------------------------------------------------------------ */
365
366 int cx88_video_mux(struct cx88_core *core, unsigned int input)
367 {
368         /* struct cx88_core *core = dev->core; */
369
370         dprintk(1,"video_mux: %d [vmux=%d,gpio=0x%x,0x%x,0x%x,0x%x]\n",
371                 input, INPUT(input)->vmux,
372                 INPUT(input)->gpio0,INPUT(input)->gpio1,
373                 INPUT(input)->gpio2,INPUT(input)->gpio3);
374         core->input = input;
375         cx_andor(MO_INPUT_FORMAT, 0x03 << 14, INPUT(input)->vmux << 14);
376         cx_write(MO_GP3_IO, INPUT(input)->gpio3);
377         cx_write(MO_GP0_IO, INPUT(input)->gpio0);
378         cx_write(MO_GP1_IO, INPUT(input)->gpio1);
379         cx_write(MO_GP2_IO, INPUT(input)->gpio2);
380
381         switch (INPUT(input)->type) {
382         case CX88_VMUX_SVIDEO:
383                 cx_set(MO_AFECFG_IO,    0x00000001);
384                 cx_set(MO_INPUT_FORMAT, 0x00010010);
385                 cx_set(MO_FILTER_EVEN,  0x00002020);
386                 cx_set(MO_FILTER_ODD,   0x00002020);
387                 break;
388         default:
389                 cx_clear(MO_AFECFG_IO,    0x00000001);
390                 cx_clear(MO_INPUT_FORMAT, 0x00010010);
391                 cx_clear(MO_FILTER_EVEN,  0x00002020);
392                 cx_clear(MO_FILTER_ODD,   0x00002020);
393                 break;
394         }
395
396         if (cx88_boards[core->board].mpeg & CX88_MPEG_BLACKBIRD) {
397                 /* sets sound input from external adc */
398                 if (INPUT(input)->extadc)
399                         cx_set(AUD_CTL, EN_I2SIN_ENABLE);
400                 else
401                         cx_clear(AUD_CTL, EN_I2SIN_ENABLE);
402         }
403         return 0;
404 }
405 EXPORT_SYMBOL(cx88_video_mux);
406
407 /* ------------------------------------------------------------------ */
408
409 static int start_video_dma(struct cx8800_dev    *dev,
410                            struct cx88_dmaqueue *q,
411                            struct cx88_buffer   *buf)
412 {
413         struct cx88_core *core = dev->core;
414
415         /* setup fifo + format */
416         cx88_sram_channel_setup(core, &cx88_sram_channels[SRAM_CH21],
417                                 buf->bpl, buf->risc.dma);
418         cx88_set_scale(core, buf->vb.width, buf->vb.height, buf->vb.field);
419         cx_write(MO_COLOR_CTRL, buf->fmt->cxformat | ColorFormatGamma);
420
421         /* reset counter */
422         cx_write(MO_VIDY_GPCNTRL,GP_COUNT_CONTROL_RESET);
423         q->count = 1;
424
425         /* enable irqs */
426         cx_set(MO_PCI_INTMSK, core->pci_irqmask | 0x01);
427
428         /* Enables corresponding bits at PCI_INT_STAT:
429                 bits 0 to 4: video, audio, transport stream, VIP, Host
430                 bit 7: timer
431                 bits 8 and 9: DMA complete for: SRC, DST
432                 bits 10 and 11: BERR signal asserted for RISC: RD, WR
433                 bits 12 to 15: BERR signal asserted for: BRDG, SRC, DST, IPB
434          */
435         cx_set(MO_VID_INTMSK, 0x0f0011);
436
437         /* enable capture */
438         cx_set(VID_CAPTURE_CONTROL,0x06);
439
440         /* start dma */
441         cx_set(MO_DEV_CNTRL2, (1<<5));
442         cx_set(MO_VID_DMACNTRL, 0x11); /* Planar Y and packed FIFO and RISC enable */
443
444         return 0;
445 }
446
447 #ifdef CONFIG_PM
448 static int stop_video_dma(struct cx8800_dev    *dev)
449 {
450         struct cx88_core *core = dev->core;
451
452         /* stop dma */
453         cx_clear(MO_VID_DMACNTRL, 0x11);
454
455         /* disable capture */
456         cx_clear(VID_CAPTURE_CONTROL,0x06);
457
458         /* disable irqs */
459         cx_clear(MO_PCI_INTMSK, 0x000001);
460         cx_clear(MO_VID_INTMSK, 0x0f0011);
461         return 0;
462 }
463 #endif
464
465 static int restart_video_queue(struct cx8800_dev    *dev,
466                                struct cx88_dmaqueue *q)
467 {
468         struct cx88_core *core = dev->core;
469         struct cx88_buffer *buf, *prev;
470         struct list_head *item;
471
472         if (!list_empty(&q->active)) {
473                 buf = list_entry(q->active.next, struct cx88_buffer, vb.queue);
474                 dprintk(2,"restart_queue [%p/%d]: restart dma\n",
475                         buf, buf->vb.i);
476                 start_video_dma(dev, q, buf);
477                 list_for_each(item,&q->active) {
478                         buf = list_entry(item, struct cx88_buffer, vb.queue);
479                         buf->count    = q->count++;
480                 }
481                 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
482                 return 0;
483         }
484
485         prev = NULL;
486         for (;;) {
487                 if (list_empty(&q->queued))
488                         return 0;
489                 buf = list_entry(q->queued.next, struct cx88_buffer, vb.queue);
490                 if (NULL == prev) {
491                         list_move_tail(&buf->vb.queue, &q->active);
492                         start_video_dma(dev, q, buf);
493                         buf->vb.state = STATE_ACTIVE;
494                         buf->count    = q->count++;
495                         mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
496                         dprintk(2,"[%p/%d] restart_queue - first active\n",
497                                 buf,buf->vb.i);
498
499                 } else if (prev->vb.width  == buf->vb.width  &&
500                            prev->vb.height == buf->vb.height &&
501                            prev->fmt       == buf->fmt) {
502                         list_move_tail(&buf->vb.queue, &q->active);
503                         buf->vb.state = STATE_ACTIVE;
504                         buf->count    = q->count++;
505                         prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
506                         dprintk(2,"[%p/%d] restart_queue - move to active\n",
507                                 buf,buf->vb.i);
508                 } else {
509                         return 0;
510                 }
511                 prev = buf;
512         }
513 }
514
515 /* ------------------------------------------------------------------ */
516
517 static int
518 buffer_setup(struct videobuf_queue *q, unsigned int *count, unsigned int *size)
519 {
520         struct cx8800_fh *fh = q->priv_data;
521
522         *size = fh->fmt->depth*fh->width*fh->height >> 3;
523         if (0 == *count)
524                 *count = 32;
525         while (*size * *count > vid_limit * 1024 * 1024)
526                 (*count)--;
527         return 0;
528 }
529
530 static int
531 buffer_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
532                enum v4l2_field field)
533 {
534         struct cx8800_fh   *fh  = q->priv_data;
535         struct cx8800_dev  *dev = fh->dev;
536         struct cx88_core *core = dev->core;
537         struct cx88_buffer *buf = container_of(vb,struct cx88_buffer,vb);
538         int rc, init_buffer = 0;
539
540         BUG_ON(NULL == fh->fmt);
541         if (fh->width  < 48 || fh->width  > norm_maxw(core->tvnorm) ||
542             fh->height < 32 || fh->height > norm_maxh(core->tvnorm))
543                 return -EINVAL;
544         buf->vb.size = (fh->width * fh->height * fh->fmt->depth) >> 3;
545         if (0 != buf->vb.baddr  &&  buf->vb.bsize < buf->vb.size)
546                 return -EINVAL;
547
548         if (buf->fmt       != fh->fmt    ||
549             buf->vb.width  != fh->width  ||
550             buf->vb.height != fh->height ||
551             buf->vb.field  != field) {
552                 buf->fmt       = fh->fmt;
553                 buf->vb.width  = fh->width;
554                 buf->vb.height = fh->height;
555                 buf->vb.field  = field;
556                 init_buffer = 1;
557         }
558
559         if (STATE_NEEDS_INIT == buf->vb.state) {
560                 init_buffer = 1;
561                 if (0 != (rc = videobuf_iolock(q,&buf->vb,NULL)))
562                         goto fail;
563         }
564
565         if (init_buffer) {
566                 buf->bpl = buf->vb.width * buf->fmt->depth >> 3;
567                 switch (buf->vb.field) {
568                 case V4L2_FIELD_TOP:
569                         cx88_risc_buffer(dev->pci, &buf->risc,
570                                          buf->vb.dma.sglist, 0, UNSET,
571                                          buf->bpl, 0, buf->vb.height);
572                         break;
573                 case V4L2_FIELD_BOTTOM:
574                         cx88_risc_buffer(dev->pci, &buf->risc,
575                                          buf->vb.dma.sglist, UNSET, 0,
576                                          buf->bpl, 0, buf->vb.height);
577                         break;
578                 case V4L2_FIELD_INTERLACED:
579                         cx88_risc_buffer(dev->pci, &buf->risc,
580                                          buf->vb.dma.sglist, 0, buf->bpl,
581                                          buf->bpl, buf->bpl,
582                                          buf->vb.height >> 1);
583                         break;
584                 case V4L2_FIELD_SEQ_TB:
585                         cx88_risc_buffer(dev->pci, &buf->risc,
586                                          buf->vb.dma.sglist,
587                                          0, buf->bpl * (buf->vb.height >> 1),
588                                          buf->bpl, 0,
589                                          buf->vb.height >> 1);
590                         break;
591                 case V4L2_FIELD_SEQ_BT:
592                         cx88_risc_buffer(dev->pci, &buf->risc,
593                                          buf->vb.dma.sglist,
594                                          buf->bpl * (buf->vb.height >> 1), 0,
595                                          buf->bpl, 0,
596                                          buf->vb.height >> 1);
597                         break;
598                 default:
599                         BUG();
600                 }
601         }
602         dprintk(2,"[%p/%d] buffer_prepare - %dx%d %dbpp \"%s\" - dma=0x%08lx\n",
603                 buf, buf->vb.i,
604                 fh->width, fh->height, fh->fmt->depth, fh->fmt->name,
605                 (unsigned long)buf->risc.dma);
606
607         buf->vb.state = STATE_PREPARED;
608         return 0;
609
610  fail:
611         cx88_free_buffer(q,buf);
612         return rc;
613 }
614
615 static void
616 buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
617 {
618         struct cx88_buffer    *buf = container_of(vb,struct cx88_buffer,vb);
619         struct cx88_buffer    *prev;
620         struct cx8800_fh      *fh   = vq->priv_data;
621         struct cx8800_dev     *dev  = fh->dev;
622         struct cx88_core      *core = dev->core;
623         struct cx88_dmaqueue  *q    = &dev->vidq;
624
625         /* add jump to stopper */
626         buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_IRQ1 | RISC_CNT_INC);
627         buf->risc.jmp[1] = cpu_to_le32(q->stopper.dma);
628
629         if (!list_empty(&q->queued)) {
630                 list_add_tail(&buf->vb.queue,&q->queued);
631                 buf->vb.state = STATE_QUEUED;
632                 dprintk(2,"[%p/%d] buffer_queue - append to queued\n",
633                         buf, buf->vb.i);
634
635         } else if (list_empty(&q->active)) {
636                 list_add_tail(&buf->vb.queue,&q->active);
637                 start_video_dma(dev, q, buf);
638                 buf->vb.state = STATE_ACTIVE;
639                 buf->count    = q->count++;
640                 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
641                 dprintk(2,"[%p/%d] buffer_queue - first active\n",
642                         buf, buf->vb.i);
643
644         } else {
645                 prev = list_entry(q->active.prev, struct cx88_buffer, vb.queue);
646                 if (prev->vb.width  == buf->vb.width  &&
647                     prev->vb.height == buf->vb.height &&
648                     prev->fmt       == buf->fmt) {
649                         list_add_tail(&buf->vb.queue,&q->active);
650                         buf->vb.state = STATE_ACTIVE;
651                         buf->count    = q->count++;
652                         prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
653                         dprintk(2,"[%p/%d] buffer_queue - append to active\n",
654                                 buf, buf->vb.i);
655
656                 } else {
657                         list_add_tail(&buf->vb.queue,&q->queued);
658                         buf->vb.state = STATE_QUEUED;
659                         dprintk(2,"[%p/%d] buffer_queue - first queued\n",
660                                 buf, buf->vb.i);
661                 }
662         }
663 }
664
665 static void buffer_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
666 {
667         struct cx88_buffer *buf = container_of(vb,struct cx88_buffer,vb);
668
669         cx88_free_buffer(q,buf);
670 }
671
672 static struct videobuf_queue_ops cx8800_video_qops = {
673         .buf_setup    = buffer_setup,
674         .buf_prepare  = buffer_prepare,
675         .buf_queue    = buffer_queue,
676         .buf_release  = buffer_release,
677 };
678
679 /* ------------------------------------------------------------------ */
680
681
682 /* ------------------------------------------------------------------ */
683
684 static struct videobuf_queue* get_queue(struct cx8800_fh *fh)
685 {
686         switch (fh->type) {
687         case V4L2_BUF_TYPE_VIDEO_CAPTURE:
688                 return &fh->vidq;
689         case V4L2_BUF_TYPE_VBI_CAPTURE:
690                 return &fh->vbiq;
691         default:
692                 BUG();
693                 return NULL;
694         }
695 }
696
697 static int get_ressource(struct cx8800_fh *fh)
698 {
699         switch (fh->type) {
700         case V4L2_BUF_TYPE_VIDEO_CAPTURE:
701                 return RESOURCE_VIDEO;
702         case V4L2_BUF_TYPE_VBI_CAPTURE:
703                 return RESOURCE_VBI;
704         default:
705                 BUG();
706                 return 0;
707         }
708 }
709
710 static int video_open(struct inode *inode, struct file *file)
711 {
712         int minor = iminor(inode);
713         struct cx8800_dev *h,*dev = NULL;
714         struct cx88_core *core;
715         struct cx8800_fh *fh;
716         struct list_head *list;
717         enum v4l2_buf_type type = 0;
718         int radio = 0;
719
720         list_for_each(list,&cx8800_devlist) {
721                 h = list_entry(list, struct cx8800_dev, devlist);
722                 if (h->video_dev->minor == minor) {
723                         dev  = h;
724                         type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
725                 }
726                 if (h->vbi_dev->minor == minor) {
727                         dev  = h;
728                         type = V4L2_BUF_TYPE_VBI_CAPTURE;
729                 }
730                 if (h->radio_dev &&
731                     h->radio_dev->minor == minor) {
732                         radio = 1;
733                         dev   = h;
734                 }
735         }
736         if (NULL == dev)
737                 return -ENODEV;
738
739         core = dev->core;
740
741         dprintk(1,"open minor=%d radio=%d type=%s\n",
742                 minor,radio,v4l2_type_names[type]);
743
744         /* allocate + initialize per filehandle data */
745         fh = kzalloc(sizeof(*fh),GFP_KERNEL);
746         if (NULL == fh)
747                 return -ENOMEM;
748         file->private_data = fh;
749         fh->dev      = dev;
750         fh->radio    = radio;
751         fh->type     = type;
752         fh->width    = 320;
753         fh->height   = 240;
754         fh->fmt      = format_by_fourcc(V4L2_PIX_FMT_BGR24);
755
756         videobuf_queue_init(&fh->vidq, &cx8800_video_qops,
757                             dev->pci, &dev->slock,
758                             V4L2_BUF_TYPE_VIDEO_CAPTURE,
759                             V4L2_FIELD_INTERLACED,
760                             sizeof(struct cx88_buffer),
761                             fh);
762         videobuf_queue_init(&fh->vbiq, &cx8800_vbi_qops,
763                             dev->pci, &dev->slock,
764                             V4L2_BUF_TYPE_VBI_CAPTURE,
765                             V4L2_FIELD_SEQ_TB,
766                             sizeof(struct cx88_buffer),
767                             fh);
768
769         if (fh->radio) {
770                 int board = core->board;
771                 dprintk(1,"video_open: setting radio device\n");
772                 cx_write(MO_GP3_IO, cx88_boards[board].radio.gpio3);
773                 cx_write(MO_GP0_IO, cx88_boards[board].radio.gpio0);
774                 cx_write(MO_GP1_IO, cx88_boards[board].radio.gpio1);
775                 cx_write(MO_GP2_IO, cx88_boards[board].radio.gpio2);
776                 core->tvaudio = WW_FM;
777                 cx88_set_tvaudio(core);
778                 cx88_set_stereo(core,V4L2_TUNER_MODE_STEREO,1);
779                 cx88_call_i2c_clients(core,AUDC_SET_RADIO,NULL);
780         }
781
782         return 0;
783 }
784
785 static ssize_t
786 video_read(struct file *file, char __user *data, size_t count, loff_t *ppos)
787 {
788         struct cx8800_fh *fh = file->private_data;
789
790         switch (fh->type) {
791         case V4L2_BUF_TYPE_VIDEO_CAPTURE:
792                 if (res_locked(fh->dev,RESOURCE_VIDEO))
793                         return -EBUSY;
794                 return videobuf_read_one(&fh->vidq, data, count, ppos,
795                                          file->f_flags & O_NONBLOCK);
796         case V4L2_BUF_TYPE_VBI_CAPTURE:
797                 if (!res_get(fh->dev,fh,RESOURCE_VBI))
798                         return -EBUSY;
799                 return videobuf_read_stream(&fh->vbiq, data, count, ppos, 1,
800                                             file->f_flags & O_NONBLOCK);
801         default:
802                 BUG();
803                 return 0;
804         }
805 }
806
807 static unsigned int
808 video_poll(struct file *file, struct poll_table_struct *wait)
809 {
810         struct cx8800_fh *fh = file->private_data;
811         struct cx88_buffer *buf;
812
813         if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type) {
814                 if (!res_get(fh->dev,fh,RESOURCE_VBI))
815                         return POLLERR;
816                 return videobuf_poll_stream(file, &fh->vbiq, wait);
817         }
818
819         if (res_check(fh,RESOURCE_VIDEO)) {
820                 /* streaming capture */
821                 if (list_empty(&fh->vidq.stream))
822                         return POLLERR;
823                 buf = list_entry(fh->vidq.stream.next,struct cx88_buffer,vb.stream);
824         } else {
825                 /* read() capture */
826                 buf = (struct cx88_buffer*)fh->vidq.read_buf;
827                 if (NULL == buf)
828                         return POLLERR;
829         }
830         poll_wait(file, &buf->vb.done, wait);
831         if (buf->vb.state == STATE_DONE ||
832             buf->vb.state == STATE_ERROR)
833                 return POLLIN|POLLRDNORM;
834         return 0;
835 }
836
837 static int video_release(struct inode *inode, struct file *file)
838 {
839         struct cx8800_fh  *fh  = file->private_data;
840         struct cx8800_dev *dev = fh->dev;
841
842         /* turn off overlay */
843         if (res_check(fh, RESOURCE_OVERLAY)) {
844                 /* FIXME */
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
877 static int
878 video_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
888 int 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 }
918 EXPORT_SYMBOL(cx88_get_control);
919
920 int 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 }
974 EXPORT_SYMBOL(cx88_set_control);
975
976 static 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
992 static 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
1008 static 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
1059 static 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
1074 static 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
1095 static 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
1108 static 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
1135 static 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
1141 static 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
1147 static 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
1153 static 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
1160 static 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
1175 static 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
1194 static 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 */
1206 int 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 }
1236 EXPORT_SYMBOL(cx88_enum_input);
1237
1238 static 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
1245 static 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
1253 static 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
1269 static 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
1278 static 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
1286 static 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
1294 static 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
1316 static 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
1330 static 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
1348 int 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 }
1369 EXPORT_SYMBOL(cx88_set_freq);
1370
1371 static 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
1387 static 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
1399 static 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
1415 static 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
1430 static 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
1445 static 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
1456 static 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 /* FIXME: Should add a standard for radio */
1467
1468 static int radio_s_tuner (struct file *file, void *priv,
1469                                 struct v4l2_tuner *t)
1470 {
1471         struct cx88_core  *core = ((struct cx8800_fh *)priv)->dev->core;
1472
1473         if (0 != t->index)
1474                 return -EINVAL;
1475
1476         cx88_call_i2c_clients(core,VIDIOC_S_TUNER,t);
1477
1478         return 0;
1479 }
1480
1481 static int radio_s_audio (struct file *file, void *fh,
1482                           struct v4l2_audio *a)
1483 {
1484         return 0;
1485 }
1486
1487 static int radio_s_input (struct file *file, void *fh, unsigned int i)
1488 {
1489         return 0;
1490 }
1491
1492 static int radio_queryctrl (struct file *file, void *priv,
1493                             struct v4l2_queryctrl *c)
1494 {
1495         int i;
1496
1497         if (c->id <  V4L2_CID_BASE ||
1498                 c->id >= V4L2_CID_LASTP1)
1499                 return -EINVAL;
1500         if (c->id == V4L2_CID_AUDIO_MUTE) {
1501                 for (i = 0; i < CX8800_CTLS; i++)
1502                         if (cx8800_ctls[i].v.id == c->id)
1503                                 break;
1504                 *c = cx8800_ctls[i].v;
1505         } else
1506                 *c = no_ctl;
1507         return 0;
1508 }
1509
1510 /* ----------------------------------------------------------- */
1511
1512 static void cx8800_vid_timeout(unsigned long data)
1513 {
1514         struct cx8800_dev *dev = (struct cx8800_dev*)data;
1515         struct cx88_core *core = dev->core;
1516         struct cx88_dmaqueue *q = &dev->vidq;
1517         struct cx88_buffer *buf;
1518         unsigned long flags;
1519
1520         cx88_sram_channel_dump(core, &cx88_sram_channels[SRAM_CH21]);
1521
1522         cx_clear(MO_VID_DMACNTRL, 0x11);
1523         cx_clear(VID_CAPTURE_CONTROL, 0x06);
1524
1525         spin_lock_irqsave(&dev->slock,flags);
1526         while (!list_empty(&q->active)) {
1527                 buf = list_entry(q->active.next, struct cx88_buffer, vb.queue);
1528                 list_del(&buf->vb.queue);
1529                 buf->vb.state = STATE_ERROR;
1530                 wake_up(&buf->vb.done);
1531                 printk("%s/0: [%p/%d] timeout - dma=0x%08lx\n", core->name,
1532                        buf, buf->vb.i, (unsigned long)buf->risc.dma);
1533         }
1534         restart_video_queue(dev,q);
1535         spin_unlock_irqrestore(&dev->slock,flags);
1536 }
1537
1538 static char *cx88_vid_irqs[32] = {
1539         "y_risci1", "u_risci1", "v_risci1", "vbi_risc1",
1540         "y_risci2", "u_risci2", "v_risci2", "vbi_risc2",
1541         "y_oflow",  "u_oflow",  "v_oflow",  "vbi_oflow",
1542         "y_sync",   "u_sync",   "v_sync",   "vbi_sync",
1543         "opc_err",  "par_err",  "rip_err",  "pci_abort",
1544 };
1545
1546 static void cx8800_vid_irq(struct cx8800_dev *dev)
1547 {
1548         struct cx88_core *core = dev->core;
1549         u32 status, mask, count;
1550
1551         status = cx_read(MO_VID_INTSTAT);
1552         mask   = cx_read(MO_VID_INTMSK);
1553         if (0 == (status & mask))
1554                 return;
1555         cx_write(MO_VID_INTSTAT, status);
1556         if (irq_debug  ||  (status & mask & ~0xff))
1557                 cx88_print_irqbits(core->name, "irq vid",
1558                                    cx88_vid_irqs, ARRAY_SIZE(cx88_vid_irqs),
1559                                    status, mask);
1560
1561         /* risc op code error */
1562         if (status & (1 << 16)) {
1563                 printk(KERN_WARNING "%s/0: video risc op code error\n",core->name);
1564                 cx_clear(MO_VID_DMACNTRL, 0x11);
1565                 cx_clear(VID_CAPTURE_CONTROL, 0x06);
1566                 cx88_sram_channel_dump(core, &cx88_sram_channels[SRAM_CH21]);
1567         }
1568
1569         /* risc1 y */
1570         if (status & 0x01) {
1571                 spin_lock(&dev->slock);
1572                 count = cx_read(MO_VIDY_GPCNT);
1573                 cx88_wakeup(core, &dev->vidq, count);
1574                 spin_unlock(&dev->slock);
1575         }
1576
1577         /* risc1 vbi */
1578         if (status & 0x08) {
1579                 spin_lock(&dev->slock);
1580                 count = cx_read(MO_VBI_GPCNT);
1581                 cx88_wakeup(core, &dev->vbiq, count);
1582                 spin_unlock(&dev->slock);
1583         }
1584
1585         /* risc2 y */
1586         if (status & 0x10) {
1587                 dprintk(2,"stopper video\n");
1588                 spin_lock(&dev->slock);
1589                 restart_video_queue(dev,&dev->vidq);
1590                 spin_unlock(&dev->slock);
1591         }
1592
1593         /* risc2 vbi */
1594         if (status & 0x80) {
1595                 dprintk(2,"stopper vbi\n");
1596                 spin_lock(&dev->slock);
1597                 cx8800_restart_vbi_queue(dev,&dev->vbiq);
1598                 spin_unlock(&dev->slock);
1599         }
1600 }
1601
1602 static irqreturn_t cx8800_irq(int irq, void *dev_id)
1603 {
1604         struct cx8800_dev *dev = dev_id;
1605         struct cx88_core *core = dev->core;
1606         u32 status;
1607         int loop, handled = 0;
1608
1609         for (loop = 0; loop < 10; loop++) {
1610                 status = cx_read(MO_PCI_INTSTAT) & (core->pci_irqmask | 0x01);
1611                 if (0 == status)
1612                         goto out;
1613                 cx_write(MO_PCI_INTSTAT, status);
1614                 handled = 1;
1615
1616                 if (status & core->pci_irqmask)
1617                         cx88_core_irq(core,status);
1618                 if (status & 0x01)
1619                         cx8800_vid_irq(dev);
1620         };
1621         if (10 == loop) {
1622                 printk(KERN_WARNING "%s/0: irq loop -- clearing mask\n",
1623                        core->name);
1624                 cx_write(MO_PCI_INTMSK,0);
1625         }
1626
1627  out:
1628         return IRQ_RETVAL(handled);
1629 }
1630
1631 /* ----------------------------------------------------------- */
1632 /* exported stuff                                              */
1633
1634 static const struct file_operations video_fops =
1635 {
1636         .owner         = THIS_MODULE,
1637         .open          = video_open,
1638         .release       = video_release,
1639         .read          = video_read,
1640         .poll          = video_poll,
1641         .mmap          = video_mmap,
1642         .ioctl         = video_ioctl2,
1643         .compat_ioctl  = v4l_compat_ioctl32,
1644         .llseek        = no_llseek,
1645 };
1646
1647 static struct video_device cx8800_vbi_template;
1648 static struct video_device cx8800_video_template =
1649 {
1650         .name                 = "cx8800-video",
1651         .type                 = VID_TYPE_CAPTURE|VID_TYPE_TUNER|VID_TYPE_SCALES,
1652         .fops                 = &video_fops,
1653         .minor                = -1,
1654         .vidioc_querycap      = vidioc_querycap,
1655         .vidioc_enum_fmt_cap  = vidioc_enum_fmt_cap,
1656         .vidioc_g_fmt_cap     = vidioc_g_fmt_cap,
1657         .vidioc_try_fmt_cap   = vidioc_try_fmt_cap,
1658         .vidioc_s_fmt_cap     = vidioc_s_fmt_cap,
1659         .vidioc_g_fmt_vbi     = cx8800_vbi_fmt,
1660         .vidioc_try_fmt_vbi   = cx8800_vbi_fmt,
1661         .vidioc_s_fmt_vbi     = cx8800_vbi_fmt,
1662         .vidioc_reqbufs       = vidioc_reqbufs,
1663         .vidioc_querybuf      = vidioc_querybuf,
1664         .vidioc_qbuf          = vidioc_qbuf,
1665         .vidioc_dqbuf         = vidioc_dqbuf,
1666         .vidioc_s_std         = vidioc_s_std,
1667         .vidioc_enum_input    = vidioc_enum_input,
1668         .vidioc_g_input       = vidioc_g_input,
1669         .vidioc_s_input       = vidioc_s_input,
1670         .vidioc_queryctrl     = vidioc_queryctrl,
1671         .vidioc_g_ctrl        = vidioc_g_ctrl,
1672         .vidioc_s_ctrl        = vidioc_s_ctrl,
1673         .vidioc_streamon      = vidioc_streamon,
1674         .vidioc_streamoff     = vidioc_streamoff,
1675 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1676         .vidiocgmbuf          = vidiocgmbuf,
1677 #endif
1678         .vidioc_g_tuner       = vidioc_g_tuner,
1679         .vidioc_s_tuner       = vidioc_s_tuner,
1680         .vidioc_g_frequency   = vidioc_g_frequency,
1681         .vidioc_s_frequency   = vidioc_s_frequency,
1682 #ifdef CONFIG_VIDEO_ADV_DEBUG
1683         .vidioc_g_register    = vidioc_g_register,
1684         .vidioc_s_register    = vidioc_s_register,
1685 #endif
1686         .tvnorms              = CX88_NORMS,
1687         .current_norm         = V4L2_STD_NTSC_M,
1688 };
1689
1690 static const struct file_operations radio_fops =
1691 {
1692         .owner         = THIS_MODULE,
1693         .open          = video_open,
1694         .release       = video_release,
1695         .ioctl         = video_ioctl2,
1696         .compat_ioctl  = v4l_compat_ioctl32,
1697         .llseek        = no_llseek,
1698 };
1699
1700 static struct video_device cx8800_radio_template =
1701 {
1702         .name                 = "cx8800-radio",
1703         .type                 = VID_TYPE_TUNER,
1704         .hardware             = 0,
1705         .fops                 = &radio_fops,
1706         .minor                = -1,
1707         .vidioc_querycap      = radio_querycap,
1708         .vidioc_g_tuner       = radio_g_tuner,
1709         .vidioc_enum_input    = radio_enum_input,
1710         .vidioc_g_audio       = radio_g_audio,
1711         .vidioc_s_tuner       = radio_s_tuner,
1712         .vidioc_s_audio       = radio_s_audio,
1713         .vidioc_s_input       = radio_s_input,
1714         .vidioc_queryctrl     = radio_queryctrl,
1715         .vidioc_g_ctrl        = vidioc_g_ctrl,
1716         .vidioc_s_ctrl        = vidioc_s_ctrl,
1717         .vidioc_g_frequency   = vidioc_g_frequency,
1718         .vidioc_s_frequency   = vidioc_s_frequency,
1719 };
1720
1721 /* ----------------------------------------------------------- */
1722
1723 static void cx8800_unregister_video(struct cx8800_dev *dev)
1724 {
1725         if (dev->radio_dev) {
1726                 if (-1 != dev->radio_dev->minor)
1727                         video_unregister_device(dev->radio_dev);
1728                 else
1729                         video_device_release(dev->radio_dev);
1730                 dev->radio_dev = NULL;
1731         }
1732         if (dev->vbi_dev) {
1733                 if (-1 != dev->vbi_dev->minor)
1734                         video_unregister_device(dev->vbi_dev);
1735                 else
1736                         video_device_release(dev->vbi_dev);
1737                 dev->vbi_dev = NULL;
1738         }
1739         if (dev->video_dev) {
1740                 if (-1 != dev->video_dev->minor)
1741                         video_unregister_device(dev->video_dev);
1742                 else
1743                         video_device_release(dev->video_dev);
1744                 dev->video_dev = NULL;
1745         }
1746 }
1747
1748 static int __devinit cx8800_initdev(struct pci_dev *pci_dev,
1749                                     const struct pci_device_id *pci_id)
1750 {
1751         struct cx8800_dev *dev;
1752         struct cx88_core *core;
1753
1754         int err;
1755
1756         dev = kzalloc(sizeof(*dev),GFP_KERNEL);
1757         if (NULL == dev)
1758                 return -ENOMEM;
1759
1760         /* pci init */
1761         dev->pci = pci_dev;
1762         if (pci_enable_device(pci_dev)) {
1763                 err = -EIO;
1764                 goto fail_free;
1765         }
1766         core = cx88_core_get(dev->pci);
1767         if (NULL == core) {
1768                 err = -EINVAL;
1769                 goto fail_free;
1770         }
1771         dev->core = core;
1772
1773         /* print pci info */
1774         pci_read_config_byte(pci_dev, PCI_CLASS_REVISION, &dev->pci_rev);
1775         pci_read_config_byte(pci_dev, PCI_LATENCY_TIMER,  &dev->pci_lat);
1776         printk(KERN_INFO "%s/0: found at %s, rev: %d, irq: %d, "
1777                "latency: %d, mmio: 0x%llx\n", core->name,
1778                pci_name(pci_dev), dev->pci_rev, pci_dev->irq,
1779                dev->pci_lat,(unsigned long long)pci_resource_start(pci_dev,0));
1780
1781         pci_set_master(pci_dev);
1782         if (!pci_dma_supported(pci_dev,DMA_32BIT_MASK)) {
1783                 printk("%s/0: Oops: no 32bit PCI DMA ???\n",core->name);
1784                 err = -EIO;
1785                 goto fail_core;
1786         }
1787
1788         /* Initialize VBI template */
1789         memcpy( &cx8800_vbi_template, &cx8800_video_template,
1790                 sizeof(cx8800_vbi_template) );
1791         strcpy(cx8800_vbi_template.name,"cx8800-vbi");
1792         cx8800_vbi_template.type = VID_TYPE_TELETEXT|VID_TYPE_TUNER;
1793
1794         /* initialize driver struct */
1795         spin_lock_init(&dev->slock);
1796         core->tvnorm = cx8800_video_template.current_norm;
1797
1798         /* init video dma queues */
1799         INIT_LIST_HEAD(&dev->vidq.active);
1800         INIT_LIST_HEAD(&dev->vidq.queued);
1801         dev->vidq.timeout.function = cx8800_vid_timeout;
1802         dev->vidq.timeout.data     = (unsigned long)dev;
1803         init_timer(&dev->vidq.timeout);
1804         cx88_risc_stopper(dev->pci,&dev->vidq.stopper,
1805                           MO_VID_DMACNTRL,0x11,0x00);
1806
1807         /* init vbi dma queues */
1808         INIT_LIST_HEAD(&dev->vbiq.active);
1809         INIT_LIST_HEAD(&dev->vbiq.queued);
1810         dev->vbiq.timeout.function = cx8800_vbi_timeout;
1811         dev->vbiq.timeout.data     = (unsigned long)dev;
1812         init_timer(&dev->vbiq.timeout);
1813         cx88_risc_stopper(dev->pci,&dev->vbiq.stopper,
1814                           MO_VID_DMACNTRL,0x88,0x00);
1815
1816         /* get irq */
1817         err = request_irq(pci_dev->irq, cx8800_irq,
1818                           IRQF_SHARED | IRQF_DISABLED, core->name, dev);
1819         if (err < 0) {
1820                 printk(KERN_ERR "%s: can't get IRQ %d\n",
1821                        core->name,pci_dev->irq);
1822                 goto fail_core;
1823         }
1824         cx_set(MO_PCI_INTMSK, core->pci_irqmask);
1825
1826         /* load and configure helper modules */
1827         if (TUNER_ABSENT != core->tuner_type)
1828                 request_module("tuner");
1829
1830         if (cx88_boards[ core->board ].audio_chip == AUDIO_CHIP_WM8775)
1831                 request_module("wm8775");
1832
1833         /* register v4l devices */
1834         dev->video_dev = cx88_vdev_init(core,dev->pci,
1835                                         &cx8800_video_template,"video");
1836         err = video_register_device(dev->video_dev,VFL_TYPE_GRABBER,
1837                                     video_nr[core->nr]);
1838         if (err < 0) {
1839                 printk(KERN_INFO "%s: can't register video device\n",
1840                        core->name);
1841                 goto fail_unreg;
1842         }
1843         printk(KERN_INFO "%s/0: registered device video%d [v4l2]\n",
1844                core->name,dev->video_dev->minor & 0x1f);
1845
1846         dev->vbi_dev = cx88_vdev_init(core,dev->pci,&cx8800_vbi_template,"vbi");
1847         err = video_register_device(dev->vbi_dev,VFL_TYPE_VBI,
1848                                     vbi_nr[core->nr]);
1849         if (err < 0) {
1850                 printk(KERN_INFO "%s/0: can't register vbi device\n",
1851                        core->name);
1852                 goto fail_unreg;
1853         }
1854         printk(KERN_INFO "%s/0: registered device vbi%d\n",
1855                core->name,dev->vbi_dev->minor & 0x1f);
1856
1857         if (core->has_radio) {
1858                 dev->radio_dev = cx88_vdev_init(core,dev->pci,
1859                                                 &cx8800_radio_template,"radio");
1860                 err = video_register_device(dev->radio_dev,VFL_TYPE_RADIO,
1861                                             radio_nr[core->nr]);
1862                 if (err < 0) {
1863                         printk(KERN_INFO "%s/0: can't register radio device\n",
1864                                core->name);
1865                         goto fail_unreg;
1866                 }
1867                 printk(KERN_INFO "%s/0: registered device radio%d\n",
1868                        core->name,dev->radio_dev->minor & 0x1f);
1869         }
1870
1871         /* everything worked */
1872         list_add_tail(&dev->devlist,&cx8800_devlist);
1873         pci_set_drvdata(pci_dev,dev);
1874
1875         /* initial device configuration */
1876         mutex_lock(&core->lock);
1877         cx88_set_tvnorm(core,core->tvnorm);
1878         init_controls(core);
1879         cx88_video_mux(core,0);
1880         mutex_unlock(&core->lock);
1881
1882         /* start tvaudio thread */
1883         if (core->tuner_type != TUNER_ABSENT)
1884                 core->kthread = kthread_run(cx88_audio_thread, core, "cx88 tvaudio");
1885         return 0;
1886
1887 fail_unreg:
1888         cx8800_unregister_video(dev);
1889         free_irq(pci_dev->irq, dev);
1890 fail_core:
1891         cx88_core_put(core,dev->pci);
1892 fail_free:
1893         kfree(dev);
1894         return err;
1895 }
1896
1897 static void __devexit cx8800_finidev(struct pci_dev *pci_dev)
1898 {
1899         struct cx8800_dev *dev = pci_get_drvdata(pci_dev);
1900         struct cx88_core *core = dev->core;
1901
1902         /* stop thread */
1903         if (core->kthread) {
1904                 kthread_stop(core->kthread);
1905                 core->kthread = NULL;
1906         }
1907
1908         cx88_shutdown(core); /* FIXME */
1909         pci_disable_device(pci_dev);
1910
1911         /* unregister stuff */
1912
1913         free_irq(pci_dev->irq, dev);
1914         cx8800_unregister_video(dev);
1915         pci_set_drvdata(pci_dev, NULL);
1916
1917         /* free memory */
1918         btcx_riscmem_free(dev->pci,&dev->vidq.stopper);
1919         list_del(&dev->devlist);
1920         cx88_core_put(core,dev->pci);
1921         kfree(dev);
1922 }
1923
1924 #ifdef CONFIG_PM
1925 static int cx8800_suspend(struct pci_dev *pci_dev, pm_message_t state)
1926 {
1927         struct cx8800_dev *dev = pci_get_drvdata(pci_dev);
1928         struct cx88_core *core = dev->core;
1929
1930         /* stop video+vbi capture */
1931         spin_lock(&dev->slock);
1932         if (!list_empty(&dev->vidq.active)) {
1933                 printk("%s: suspend video\n", core->name);
1934                 stop_video_dma(dev);
1935                 del_timer(&dev->vidq.timeout);
1936         }
1937         if (!list_empty(&dev->vbiq.active)) {
1938                 printk("%s: suspend vbi\n", core->name);
1939                 cx8800_stop_vbi_dma(dev);
1940                 del_timer(&dev->vbiq.timeout);
1941         }
1942         spin_unlock(&dev->slock);
1943
1944         /* FIXME -- shutdown device */
1945         cx88_shutdown(core);
1946
1947         pci_save_state(pci_dev);
1948         if (0 != pci_set_power_state(pci_dev, pci_choose_state(pci_dev, state))) {
1949                 pci_disable_device(pci_dev);
1950                 dev->state.disabled = 1;
1951         }
1952         return 0;
1953 }
1954
1955 static int cx8800_resume(struct pci_dev *pci_dev)
1956 {
1957         struct cx8800_dev *dev = pci_get_drvdata(pci_dev);
1958         struct cx88_core *core = dev->core;
1959         int err;
1960
1961         if (dev->state.disabled) {
1962                 err=pci_enable_device(pci_dev);
1963                 if (err) {
1964                         printk(KERN_ERR "%s: can't enable device\n",
1965                                                        core->name);
1966                         return err;
1967                 }
1968
1969                 dev->state.disabled = 0;
1970         }
1971         err= pci_set_power_state(pci_dev, PCI_D0);
1972         if (err) {
1973                 printk(KERN_ERR "%s: can't enable device\n",
1974                                        core->name);
1975
1976                 pci_disable_device(pci_dev);
1977                 dev->state.disabled = 1;
1978
1979                 return err;
1980         }
1981         pci_restore_state(pci_dev);
1982
1983         /* FIXME: re-initialize hardware */
1984         cx88_reset(core);
1985
1986         /* restart video+vbi capture */
1987         spin_lock(&dev->slock);
1988         if (!list_empty(&dev->vidq.active)) {
1989                 printk("%s: resume video\n", core->name);
1990                 restart_video_queue(dev,&dev->vidq);
1991         }
1992         if (!list_empty(&dev->vbiq.active)) {
1993                 printk("%s: resume vbi\n", core->name);
1994                 cx8800_restart_vbi_queue(dev,&dev->vbiq);
1995         }
1996         spin_unlock(&dev->slock);
1997
1998         return 0;
1999 }
2000 #endif
2001
2002 /* ----------------------------------------------------------- */
2003
2004 static struct pci_device_id cx8800_pci_tbl[] = {
2005         {
2006                 .vendor       = 0x14f1,
2007                 .device       = 0x8800,
2008                 .subvendor    = PCI_ANY_ID,
2009                 .subdevice    = PCI_ANY_ID,
2010         },{
2011                 /* --- end of list --- */
2012         }
2013 };
2014 MODULE_DEVICE_TABLE(pci, cx8800_pci_tbl);
2015
2016 static struct pci_driver cx8800_pci_driver = {
2017         .name     = "cx8800",
2018         .id_table = cx8800_pci_tbl,
2019         .probe    = cx8800_initdev,
2020         .remove   = __devexit_p(cx8800_finidev),
2021 #ifdef CONFIG_PM
2022         .suspend  = cx8800_suspend,
2023         .resume   = cx8800_resume,
2024 #endif
2025 };
2026
2027 static int cx8800_init(void)
2028 {
2029         printk(KERN_INFO "cx2388x v4l2 driver version %d.%d.%d loaded\n",
2030                (CX88_VERSION_CODE >> 16) & 0xff,
2031                (CX88_VERSION_CODE >>  8) & 0xff,
2032                CX88_VERSION_CODE & 0xff);
2033 #ifdef SNAPSHOT
2034         printk(KERN_INFO "cx2388x: snapshot date %04d-%02d-%02d\n",
2035                SNAPSHOT/10000, (SNAPSHOT/100)%100, SNAPSHOT%100);
2036 #endif
2037         return pci_register_driver(&cx8800_pci_driver);
2038 }
2039
2040 static void cx8800_fini(void)
2041 {
2042         pci_unregister_driver(&cx8800_pci_driver);
2043 }
2044
2045 module_init(cx8800_init);
2046 module_exit(cx8800_fini);
2047
2048 /* ----------------------------------------------------------- */
2049 /*
2050  * Local variables:
2051  * c-basic-offset: 8
2052  * End:
2053  * 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
2054  */