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