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