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