Fedora kernel-2.6.17-1.2142_FC4 patched with stable patch-2.6.17.4-vs2.0.2-rc26.diff
[linux-2.6.git] / drivers / media / video / cx88 / cx88-video.c
index d1f5c92..694d1d8 100644 (file)
@@ -1,5 +1,4 @@
 /*
- * $Id: cx88-video.c,v 1.58 2005/03/07 15:58:05 kraxel Exp $
  *
  * device driver for Conexant 2388x based TV cards
  * video4linux video interface
 #include <asm/div64.h>
 
 #include "cx88.h"
+#include <media/v4l2-common.h>
+
+#ifdef CONFIG_VIDEO_V4L1_COMPAT
+/* Include V4L1 specific functions. Should be removed soon */
+#include <linux/videodev.h>
+#endif
 
 MODULE_DESCRIPTION("v4l2 driver module for cx2388x based TV cards");
 MODULE_AUTHOR("Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]");
@@ -66,7 +71,7 @@ module_param(vid_limit,int,0644);
 MODULE_PARM_DESC(vid_limit,"capture memory limit in megabytes");
 
 #define dprintk(level,fmt, arg...)     if (video_debug >= level) \
-       printk(KERN_DEBUG "%s/0: " fmt, dev->core->name , ## arg)
+       printk(KERN_DEBUG "%s/0: " fmt, core->name , ## arg)
 
 /* ------------------------------------------------------------------ */
 
@@ -86,13 +91,6 @@ static struct cx88_tvnorm tvnorms[] = {
                .id        = V4L2_STD_NTSC_M_JP,
                .cxiformat = VideoFormatNTSCJapan,
                .cxoformat = 0x181f0008,
-#if 0
-       },{
-               .name      = "NTSC-4.43",
-               .id        = FIXME,
-               .cxiformat = VideoFormatNTSC443,
-               .cxoformat = 0x181f0008,
-#endif
        },{
                .name      = "PAL-BG",
                .id        = V4L2_STD_PAL_BG,
@@ -108,7 +106,7 @@ static struct cx88_tvnorm tvnorms[] = {
                .id        = V4L2_STD_PAL_I,
                .cxiformat = VideoFormatPAL,
                .cxoformat = 0x181f0008,
-        },{
+       },{
                .name      = "PAL-M",
                .id        = V4L2_STD_PAL_M,
                .cxiformat = VideoFormatPALM,
@@ -231,7 +229,7 @@ static struct cx88_ctrl cx8800_ctls[] = {
                        .minimum       = 0x00,
                        .maximum       = 0xff,
                        .step          = 1,
-                       .default_value = 0,
+                       .default_value = 0x7f,
                        .type          = V4L2_CTRL_TYPE_INTEGER,
                },
                .off                   = 128,
@@ -245,9 +243,10 @@ static struct cx88_ctrl cx8800_ctls[] = {
                        .minimum       = 0,
                        .maximum       = 0xff,
                        .step          = 1,
-                       .default_value = 0,
+                       .default_value = 0x3f,
                        .type          = V4L2_CTRL_TYPE_INTEGER,
                },
+               .off                   = 0,
                .reg                   = MO_CONTR_BRIGHT,
                .mask                  = 0xff00,
                .shift                 = 8,
@@ -258,10 +257,10 @@ static struct cx88_ctrl cx8800_ctls[] = {
                        .minimum       = 0,
                        .maximum       = 0xff,
                        .step          = 1,
-                       .default_value = 0,
+                       .default_value = 0x7f,
                        .type          = V4L2_CTRL_TYPE_INTEGER,
                },
-               .off                   = 0,
+               .off                   = 128,
                .reg                   = MO_HUE,
                .mask                  = 0x00ff,
                .shift                 = 0,
@@ -275,7 +274,7 @@ static struct cx88_ctrl cx8800_ctls[] = {
                        .minimum       = 0,
                        .maximum       = 0xff,
                        .step          = 1,
-                       .default_value = 0,
+                       .default_value = 0x7f,
                        .type          = V4L2_CTRL_TYPE_INTEGER,
                },
                .off                   = 0,
@@ -289,6 +288,7 @@ static struct cx88_ctrl cx8800_ctls[] = {
                        .name          = "Mute",
                        .minimum       = 0,
                        .maximum       = 1,
+                       .default_value = 1,
                        .type          = V4L2_CTRL_TYPE_BOOLEAN,
                },
                .reg                   = AUD_VOL_CTL,
@@ -302,7 +302,7 @@ static struct cx88_ctrl cx8800_ctls[] = {
                        .minimum       = 0,
                        .maximum       = 0x3f,
                        .step          = 1,
-                       .default_value = 0,
+                       .default_value = 0x3f,
                        .type          = V4L2_CTRL_TYPE_INTEGER,
                },
                .reg                   = AUD_VOL_CTL,
@@ -332,22 +332,23 @@ static const int CX8800_CTLS = ARRAY_SIZE(cx8800_ctls);
 
 static int res_get(struct cx8800_dev *dev, struct cx8800_fh *fh, unsigned int bit)
 {
+       struct cx88_core *core = dev->core;
        if (fh->resources & bit)
                /* have it already allocated */
                return 1;
 
        /* is it free? */
-       down(&dev->lock);
+       mutex_lock(&core->lock);
        if (dev->resources & bit) {
                /* no, someone else uses it */
-               up(&dev->lock);
+               mutex_unlock(&core->lock);
                return 0;
        }
        /* it's free, grab it */
        fh->resources  |= bit;
        dev->resources |= bit;
        dprintk(1,"res: get %d\n",bit);
-       up(&dev->lock);
+       mutex_unlock(&core->lock);
        return 1;
 }
 
@@ -366,27 +367,28 @@ int res_locked(struct cx8800_dev *dev, unsigned int bit)
 static
 void res_free(struct cx8800_dev *dev, struct cx8800_fh *fh, unsigned int bits)
 {
-       if ((fh->resources & bits) != bits)
-               BUG();
+       struct cx88_core *core = dev->core;
+       BUG_ON((fh->resources & bits) != bits);
 
-       down(&dev->lock);
+       mutex_lock(&core->lock);
        fh->resources  &= ~bits;
        dev->resources &= ~bits;
        dprintk(1,"res: put %d\n",bits);
-       up(&dev->lock);
+       mutex_unlock(&core->lock);
 }
 
 /* ------------------------------------------------------------------ */
 
-static int video_mux(struct cx8800_dev *dev, unsigned int input)
+/* static int video_mux(struct cx8800_dev *dev, unsigned int input) */
+static int video_mux(struct cx88_core *core, unsigned int input)
 {
-       struct cx88_core *core = dev->core;
+       /* struct cx88_core *core = dev->core; */
 
        dprintk(1,"video_mux: %d [vmux=%d,gpio=0x%x,0x%x,0x%x,0x%x]\n",
                input, INPUT(input)->vmux,
                INPUT(input)->gpio0,INPUT(input)->gpio1,
                INPUT(input)->gpio2,INPUT(input)->gpio3);
-       dev->core->input = input;
+       core->input = input;
        cx_andor(MO_INPUT_FORMAT, 0x03 << 14, INPUT(input)->vmux << 14);
        cx_write(MO_GP3_IO, INPUT(input)->gpio3);
        cx_write(MO_GP0_IO, INPUT(input)->gpio0);
@@ -419,9 +421,9 @@ static int start_video_dma(struct cx8800_dev    *dev,
        struct cx88_core *core = dev->core;
 
        /* setup fifo + format */
-       cx88_sram_channel_setup(dev->core, &cx88_sram_channels[SRAM_CH21],
+       cx88_sram_channel_setup(core, &cx88_sram_channels[SRAM_CH21],
                                buf->bpl, buf->risc.dma);
-       cx88_set_scale(dev->core, buf->vb.width, buf->vb.height, buf->vb.field);
+       cx88_set_scale(core, buf->vb.width, buf->vb.height, buf->vb.field);
        cx_write(MO_COLOR_CTRL, buf->fmt->cxformat | ColorFormatGamma);
 
        /* reset counter */
@@ -430,6 +432,14 @@ static int start_video_dma(struct cx8800_dev    *dev,
 
        /* enable irqs */
        cx_set(MO_PCI_INTMSK, core->pci_irqmask | 0x01);
+
+       /* Enables corresponding bits at PCI_INT_STAT:
+               bits 0 to 4: video, audio, transport stream, VIP, Host
+               bit 7: timer
+               bits 8 and 9: DMA complete for: SRC, DST
+               bits 10 and 11: BERR signal asserted for RISC: RD, WR
+               bits 12 to 15: BERR signal asserted for: BRDG, SRC, DST, IPB
+        */
        cx_set(MO_VID_INTMSK, 0x0f0011);
 
        /* enable capture */
@@ -437,7 +447,7 @@ static int start_video_dma(struct cx8800_dev    *dev,
 
        /* start dma */
        cx_set(MO_DEV_CNTRL2, (1<<5));
-       cx_set(MO_VID_DMACNTRL, 0x11);
+       cx_set(MO_VID_DMACNTRL, 0x11); /* Planar Y and packed FIFO and RISC enable */
 
        return 0;
 }
@@ -461,11 +471,12 @@ static int stop_video_dma(struct cx8800_dev    *dev)
 static int restart_video_queue(struct cx8800_dev    *dev,
                               struct cx88_dmaqueue *q)
 {
+       struct cx88_core *core = dev->core;
        struct cx88_buffer *buf, *prev;
        struct list_head *item;
 
        if (!list_empty(&q->active)) {
-               buf = list_entry(q->active.next, struct cx88_buffer, vb.queue);
+               buf = list_entry(q->active.next, struct cx88_buffer, vb.queue);
                dprintk(2,"restart_queue [%p/%d]: restart dma\n",
                        buf, buf->vb.i);
                start_video_dma(dev, q, buf);
@@ -481,7 +492,7 @@ static int restart_video_queue(struct cx8800_dev    *dev,
        for (;;) {
                if (list_empty(&q->queued))
                        return 0;
-               buf = list_entry(q->queued.next, struct cx88_buffer, vb.queue);
+               buf = list_entry(q->queued.next, struct cx88_buffer, vb.queue);
                if (NULL == prev) {
                        list_del(&buf->vb.queue);
                        list_add_tail(&buf->vb.queue,&q->active);
@@ -530,12 +541,13 @@ buffer_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
 {
        struct cx8800_fh   *fh  = q->priv_data;
        struct cx8800_dev  *dev = fh->dev;
+       struct cx88_core *core = dev->core;
        struct cx88_buffer *buf = container_of(vb,struct cx88_buffer,vb);
        int rc, init_buffer = 0;
 
        BUG_ON(NULL == fh->fmt);
-       if (fh->width  < 48 || fh->width  > norm_maxw(dev->core->tvnorm) ||
-           fh->height < 32 || fh->height > norm_maxh(dev->core->tvnorm))
+       if (fh->width  < 48 || fh->width  > norm_maxw(core->tvnorm) ||
+           fh->height < 32 || fh->height > norm_maxh(core->tvnorm))
                return -EINVAL;
        buf->vb.size = (fh->width * fh->height * fh->fmt->depth) >> 3;
        if (0 != buf->vb.baddr  &&  buf->vb.bsize < buf->vb.size)
@@ -554,7 +566,7 @@ buffer_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
 
        if (STATE_NEEDS_INIT == buf->vb.state) {
                init_buffer = 1;
-               if (0 != (rc = videobuf_iolock(dev->pci,&buf->vb,NULL)))
+               if (0 != (rc = videobuf_iolock(q,&buf->vb,NULL)))
                        goto fail;
        }
 
@@ -604,7 +616,7 @@ buffer_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
        return 0;
 
  fail:
-       cx88_free_buffer(dev->pci,buf);
+       cx88_free_buffer(q,buf);
        return rc;
 }
 
@@ -615,6 +627,7 @@ buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
        struct cx88_buffer    *prev;
        struct cx8800_fh      *fh   = vq->priv_data;
        struct cx8800_dev     *dev  = fh->dev;
+       struct cx88_core      *core = dev->core;
        struct cx88_dmaqueue  *q    = &dev->vidq;
 
        /* add jump to stopper */
@@ -660,9 +673,8 @@ buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
 static void buffer_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
 {
        struct cx88_buffer *buf = container_of(vb,struct cx88_buffer,vb);
-       struct cx8800_fh   *fh  = q->priv_data;
 
-       cx88_free_buffer(fh->dev->pci,buf);
+       cx88_free_buffer(q,buf);
 }
 
 static struct videobuf_queue_ops cx8800_video_qops = {
@@ -674,231 +686,6 @@ static struct videobuf_queue_ops cx8800_video_qops = {
 
 /* ------------------------------------------------------------------ */
 
-#if 0 /* overlay support not finished yet */
-static u32* ov_risc_field(struct cx8800_dev *dev, struct cx8800_fh *fh,
-                         u32 *rp, struct btcx_skiplist *skips,
-                         u32 sync_line, int skip_even, int skip_odd)
-{
-       int line,maxy,start,end,skip,nskips;
-       u32 ri,ra;
-       u32 addr;
-
-       /* sync instruction */
-       *(rp++) = cpu_to_le32(RISC_RESYNC | sync_line);
-
-       addr  = (unsigned long)dev->fbuf.base;
-       addr += dev->fbuf.fmt.bytesperline * fh->win.w.top;
-       addr += (fh->fmt->depth >> 3)      * fh->win.w.left;
-
-       /* scan lines */
-       for (maxy = -1, line = 0; line < fh->win.w.height;
-            line++, addr += dev->fbuf.fmt.bytesperline) {
-               if ((line%2) == 0  &&  skip_even)
-                       continue;
-               if ((line%2) == 1  &&  skip_odd)
-                       continue;
-
-               /* calculate clipping */
-               if (line > maxy)
-                       btcx_calc_skips(line, fh->win.w.width, &maxy,
-                                       skips, &nskips, fh->clips, fh->nclips);
-
-               /* write out risc code */
-               for (start = 0, skip = 0; start < fh->win.w.width; start = end) {
-                       if (skip >= nskips) {
-                               ri  = RISC_WRITE;
-                               end = fh->win.w.width;
-                       } else if (start < skips[skip].start) {
-                               ri  = RISC_WRITE;
-                               end = skips[skip].start;
-                       } else {
-                               ri  = RISC_SKIP;
-                               end = skips[skip].end;
-                               skip++;
-                       }
-                       if (RISC_WRITE == ri)
-                               ra = addr + (fh->fmt->depth>>3)*start;
-                       else
-                               ra = 0;
-
-                       if (0 == start)
-                               ri |= RISC_SOL;
-                       if (fh->win.w.width == end)
-                               ri |= RISC_EOL;
-                       ri |= (fh->fmt->depth>>3) * (end-start);
-
-                       *(rp++)=cpu_to_le32(ri);
-                       if (0 != ra)
-                               *(rp++)=cpu_to_le32(ra);
-               }
-       }
-       kfree(skips);
-       return rp;
-}
-
-static int ov_risc_frame(struct cx8800_dev *dev, struct cx8800_fh *fh,
-                        struct cx88_buffer *buf)
-{
-       struct btcx_skiplist *skips;
-       u32 instructions,fields;
-       u32 *rp;
-       int rc;
-
-       /* skip list for window clipping */
-       if (NULL == (skips = kmalloc(sizeof(*skips) * fh->nclips,GFP_KERNEL)))
-               return -ENOMEM;
-
-       fields = 0;
-       if (V4L2_FIELD_HAS_TOP(fh->win.field))
-               fields++;
-       if (V4L2_FIELD_HAS_BOTTOM(fh->win.field))
-               fields++;
-
-        /* estimate risc mem: worst case is (clip+1) * lines instructions
-           + syncs + jump (all 2 dwords) */
-       instructions  = (fh->nclips+1) * fh->win.w.height;
-       instructions += 3 + 4;
-       if ((rc = btcx_riscmem_alloc(dev->pci,&buf->risc,instructions*8)) < 0) {
-               kfree(skips);
-               return rc;
-       }
-
-       /* write risc instructions */
-       rp = buf->risc.cpu;
-       switch (fh->win.field) {
-       case V4L2_FIELD_TOP:
-               rp = ov_risc_field(dev, fh, rp, skips, 0,     0, 0);
-               break;
-       case V4L2_FIELD_BOTTOM:
-               rp = ov_risc_field(dev, fh, rp, skips, 0x200, 0, 0);
-               break;
-       case V4L2_FIELD_INTERLACED:
-               rp = ov_risc_field(dev, fh, rp, skips, 0,     0, 1);
-               rp = ov_risc_field(dev, fh, rp, skips, 0x200, 1, 0);
-               break;
-       default:
-               BUG();
-       }
-
-       /* save pointer to jmp instruction address */
-       buf->risc.jmp = rp;
-       kfree(skips);
-       return 0;
-}
-
-static int verify_window(struct cx8800_dev *dev, struct v4l2_window *win)
-{
-       enum v4l2_field field;
-       int maxw, maxh;
-
-       if (NULL == dev->fbuf.base)
-               return -EINVAL;
-       if (win->w.width < 48 || win->w.height <  32)
-               return -EINVAL;
-       if (win->clipcount > 2048)
-               return -EINVAL;
-
-       field = win->field;
-       maxw  = norm_maxw(core->tvnorm);
-       maxh  = norm_maxh(core->tvnorm);
-
-       if (V4L2_FIELD_ANY == field) {
-                field = (win->w.height > maxh/2)
-                        ? V4L2_FIELD_INTERLACED
-                        : V4L2_FIELD_TOP;
-        }
-        switch (field) {
-        case V4L2_FIELD_TOP:
-        case V4L2_FIELD_BOTTOM:
-                maxh = maxh / 2;
-                break;
-        case V4L2_FIELD_INTERLACED:
-                break;
-        default:
-                return -EINVAL;
-        }
-
-       win->field = field;
-       if (win->w.width > maxw)
-               win->w.width = maxw;
-       if (win->w.height > maxh)
-               win->w.height = maxh;
-       return 0;
-}
-
-static int setup_window(struct cx8800_dev *dev, struct cx8800_fh *fh,
-                       struct v4l2_window *win)
-{
-       struct v4l2_clip *clips = NULL;
-       int n,size,retval = 0;
-
-       if (NULL == fh->fmt)
-               return -EINVAL;
-       retval = verify_window(dev,win);
-       if (0 != retval)
-               return retval;
-
-       /* copy clips  --  luckily v4l1 + v4l2 are binary
-          compatible here ...*/
-       n = win->clipcount;
-       size = sizeof(*clips)*(n+4);
-       clips = kmalloc(size,GFP_KERNEL);
-       if (NULL == clips)
-               return -ENOMEM;
-       if (n > 0) {
-               if (copy_from_user(clips,win->clips,sizeof(struct v4l2_clip)*n)) {
-                       kfree(clips);
-                       return -EFAULT;
-               }
-       }
-
-       /* clip against screen */
-       if (NULL != dev->fbuf.base)
-               n = btcx_screen_clips(dev->fbuf.fmt.width, dev->fbuf.fmt.height,
-                                     &win->w, clips, n);
-       btcx_sort_clips(clips,n);
-
-       /* 4-byte alignments */
-       switch (fh->fmt->depth) {
-       case 8:
-       case 24:
-               btcx_align(&win->w, clips, n, 3);
-               break;
-       case 16:
-               btcx_align(&win->w, clips, n, 1);
-               break;
-       case 32:
-               /* no alignment fixups needed */
-               break;
-       default:
-               BUG();
-       }
-
-       down(&fh->vidq.lock);
-       if (fh->clips)
-               kfree(fh->clips);
-       fh->clips    = clips;
-       fh->nclips   = n;
-       fh->win      = *win;
-#if 0
-       fh->ov.setup_ok = 1;
-#endif
-
-       /* update overlay if needed */
-       retval = 0;
-#if 0
-       if (check_btres(fh, RESOURCE_OVERLAY)) {
-               struct bttv_buffer *new;
-
-               new = videobuf_alloc(sizeof(*new));
-               bttv_overlay_risc(btv, &fh->ov, fh->ovfmt, new);
-               retval = bttv_switch_overlay(btv,fh,new);
-       }
-#endif
-       up(&fh->vidq.lock);
-       return retval;
-}
-#endif
 
 /* ------------------------------------------------------------------ */
 
@@ -932,6 +719,7 @@ static int video_open(struct inode *inode, struct file *file)
 {
        int minor = iminor(inode);
        struct cx8800_dev *h,*dev = NULL;
+       struct cx88_core *core;
        struct cx8800_fh *fh;
        struct list_head *list;
        enum v4l2_buf_type type = 0;
@@ -956,14 +744,15 @@ static int video_open(struct inode *inode, struct file *file)
        if (NULL == dev)
                return -ENODEV;
 
+       core = dev->core;
+
        dprintk(1,"open minor=%d radio=%d type=%s\n",
                minor,radio,v4l2_type_names[type]);
 
        /* allocate + initialize per filehandle data */
-       fh = kmalloc(sizeof(*fh),GFP_KERNEL);
+       fh = kzalloc(sizeof(*fh),GFP_KERNEL);
        if (NULL == fh)
                return -ENOMEM;
-       memset(fh,0,sizeof(*fh));
        file->private_data = fh;
        fh->dev      = dev;
        fh->radio    = radio;
@@ -986,24 +775,23 @@ static int video_open(struct inode *inode, struct file *file)
                            fh);
 
        if (fh->radio) {
-               struct cx88_core *core = dev->core;
                int board = core->board;
                dprintk(1,"video_open: setting radio device\n");
+               cx_write(MO_GP3_IO, cx88_boards[board].radio.gpio3);
                cx_write(MO_GP0_IO, cx88_boards[board].radio.gpio0);
                cx_write(MO_GP1_IO, cx88_boards[board].radio.gpio1);
                cx_write(MO_GP2_IO, cx88_boards[board].radio.gpio2);
-               cx_write(MO_GP3_IO, cx88_boards[board].radio.gpio3);
-               dev->core->tvaudio = WW_FM;
+               core->tvaudio = WW_FM;
                cx88_set_tvaudio(core);
                cx88_set_stereo(core,V4L2_TUNER_MODE_STEREO,1);
-               cx88_call_i2c_clients(dev->core,AUDC_SET_RADIO,NULL);
+               cx88_call_i2c_clients(core,AUDC_SET_RADIO,NULL);
        }
 
-        return 0;
+       return 0;
 }
 
 static ssize_t
-video_read(struct file *file, char *data, size_t count, loff_t *ppos)
+video_read(struct file *file, char __user *data, size_t count, loff_t *ppos)
 {
        struct cx8800_fh *fh = file->private_data;
 
@@ -1088,6 +876,9 @@ static int video_release(struct inode *inode, struct file *file)
        videobuf_mmap_free(&fh->vbiq);
        file->private_data = NULL;
        kfree(fh);
+
+       cx88_call_i2c_clients (dev->core, TUNER_SET_STANDBY, NULL);
+
        return 0;
 }
 
@@ -1101,9 +892,10 @@ video_mmap(struct file *file, struct vm_area_struct * vma)
 
 /* ------------------------------------------------------------------ */
 
-static int get_control(struct cx8800_dev *dev, struct v4l2_control *ctl)
+/* static int get_control(struct cx8800_dev *dev, struct v4l2_control *ctl) */
+static int get_control(struct cx88_core *core, struct v4l2_control *ctl)
 {
-       struct cx88_core *core = dev->core;
+       /* struct cx88_core *core = dev->core; */
        struct cx88_ctrl *c = NULL;
        u32 value;
        int i;
@@ -1117,7 +909,8 @@ static int get_control(struct cx8800_dev *dev, struct v4l2_control *ctl)
        value = c->sreg ? cx_sread(c->sreg) : cx_read(c->reg);
        switch (ctl->id) {
        case V4L2_CID_AUDIO_BALANCE:
-               ctl->value = (value & 0x40) ? (value & 0x3f) : (0x40 - (value & 0x3f));
+               ctl->value = ((value & 0x7f) < 0x40) ? ((value & 0x7f) + 0x40)
+                                       : (0x7f - (value & 0x7f));
                break;
        case V4L2_CID_AUDIO_VOLUME:
                ctl->value = 0x3f - (value & 0x3f);
@@ -1126,70 +919,78 @@ static int get_control(struct cx8800_dev *dev, struct v4l2_control *ctl)
                ctl->value = ((value + (c->off << c->shift)) & c->mask) >> c->shift;
                break;
        }
+       dprintk(1,"get_control id=0x%X(%s) ctrl=0x%02x, reg=0x%02x val=0x%02x (mask 0x%02x)%s\n",
+                               ctl->id, c->v.name, ctl->value, c->reg,
+                               value,c->mask, c->sreg ? " [shadowed]" : "");
        return 0;
 }
 
-static int set_control(struct cx8800_dev *dev, struct v4l2_control *ctl)
+/* static int set_control(struct cx8800_dev *dev, struct v4l2_control *ctl) */
+static int set_control(struct cx88_core *core, struct v4l2_control *ctl)
 {
-       struct cx88_core *core = dev->core;
+       /* struct cx88_core *core = dev->core; */
        struct cx88_ctrl *c = NULL;
-        u32 v_sat_value;
-       u32 value;
+       u32 value,mask;
        int i;
-
-       for (i = 0; i < CX8800_CTLS; i++)
-               if (cx8800_ctls[i].v.id == ctl->id)
+       for (i = 0; i < CX8800_CTLS; i++) {
+               if (cx8800_ctls[i].v.id == ctl->id) {
                        c = &cx8800_ctls[i];
+               }
+       }
        if (NULL == c)
                return -EINVAL;
 
        if (ctl->value < c->v.minimum)
-               return -ERANGE;
+               ctl->value = c->v.minimum;
        if (ctl->value > c->v.maximum)
-               return -ERANGE;
+               ctl->value = c->v.maximum;
+       mask=c->mask;
        switch (ctl->id) {
        case V4L2_CID_AUDIO_BALANCE:
-               value = (ctl->value < 0x40) ? (0x40 - ctl->value) : ctl->value;
+               value = (ctl->value < 0x40) ? (0x7f - ctl->value) : (ctl->value - 0x40);
                break;
        case V4L2_CID_AUDIO_VOLUME:
                value = 0x3f - (ctl->value & 0x3f);
                break;
        case V4L2_CID_SATURATION:
                /* special v_sat handling */
-               v_sat_value = ctl->value - (0x7f - 0x5a);
-               if (v_sat_value > 0xff)
-                       v_sat_value = 0xff;
-               if (v_sat_value < 0x00)
-                       v_sat_value = 0x00;
-               cx_andor(MO_UV_SATURATION, 0xff00, v_sat_value << 8);
-               /* fall through to default route for u_sat */
+
+               value = ((ctl->value - c->off) << c->shift) & c->mask;
+
+               if (core->tvnorm->id & V4L2_STD_SECAM) {
+                       /* For SECAM, both U and V sat should be equal */
+                       value=value<<8|value;
+               } else {
+                       /* Keeps U Saturation proportional to V Sat */
+                       value=(value*0x5a)/0x7f<<8|value;
+               }
+               mask=0xffff;
+               break;
        default:
                value = ((ctl->value - c->off) << c->shift) & c->mask;
                break;
        }
-       dprintk(1,"set_control id=0x%X reg=0x%x val=0x%x%s\n",
-               ctl->id, c->reg, value, c->sreg ? " [shadowed]" : "");
+       dprintk(1,"set_control id=0x%X(%s) ctrl=0x%02x, reg=0x%02x val=0x%02x (mask 0x%02x)%s\n",
+                               ctl->id, c->v.name, ctl->value, c->reg, value,
+                               mask, c->sreg ? " [shadowed]" : "");
        if (c->sreg) {
-               cx_sandor(c->sreg, c->reg, c->mask, value);
+               cx_sandor(c->sreg, c->reg, mask, value);
        } else {
-               cx_andor(c->reg, c->mask, value);
+               cx_andor(c->reg, mask, value);
        }
        return 0;
 }
 
-static void init_controls(struct cx8800_dev *dev)
+static void init_controls(struct cx88_core *core)
 {
-       static struct v4l2_control mute = {
-               .id    = V4L2_CID_AUDIO_MUTE,
-               .value = 1,
-       };
-       static struct v4l2_control volume = {
-               .id    = V4L2_CID_AUDIO_VOLUME,
-               .value = 0x3f,
-       };
+       struct v4l2_control ctrl;
+       int i;
 
-       set_control(dev,&mute);
-       set_control(dev,&volume);
+       for (i = 0; i < CX8800_CTLS; i++) {
+               ctrl.id=cx8800_ctls[i].v.id;
+               ctrl.value=cx8800_ctls[i].v.default_value;
+               set_control(core, &ctrl);
+       }
 }
 
 /* ------------------------------------------------------------------ */
@@ -1220,6 +1021,8 @@ static int cx8800_g_fmt(struct cx8800_dev *dev, struct cx8800_fh *fh,
 static int cx8800_try_fmt(struct cx8800_dev *dev, struct cx8800_fh *fh,
                          struct v4l2_format *f)
 {
+       struct cx88_core *core = dev->core;
+
        switch (f->type) {
        case V4L2_BUF_TYPE_VIDEO_CAPTURE:
        {
@@ -1232,8 +1035,8 @@ static int cx8800_try_fmt(struct cx8800_dev *dev, struct cx8800_fh *fh,
                        return -EINVAL;
 
                field = f->fmt.pix.field;
-               maxw  = norm_maxw(dev->core->tvnorm);
-               maxh  = norm_maxh(dev->core->tvnorm);
+               maxw  = norm_maxw(core->tvnorm);
+               maxh  = norm_maxh(core->tvnorm);
 
                if (V4L2_FIELD_ANY == field) {
                        field = (f->fmt.pix.height > maxh/2)
@@ -1312,20 +1115,19 @@ static int video_do_ioctl(struct inode *inode, struct file *file,
        struct cx8800_fh  *fh   = file->private_data;
        struct cx8800_dev *dev  = fh->dev;
        struct cx88_core  *core = dev->core;
-#if 0
-       unsigned long flags;
-#endif
        int err;
 
        if (video_debug > 1)
-               cx88_print_ioctl(core->name,cmd);
+               v4l_print_ioctl(core->name,cmd);
        switch (cmd) {
+
+       /* --- capabilities ------------------------------------------ */
        case VIDIOC_QUERYCAP:
        {
                struct v4l2_capability *cap = arg;
 
                memset(cap,0,sizeof(*cap));
-                strcpy(cap->driver, "cx8800");
+               strcpy(cap->driver, "cx8800");
                strlcpy(cap->card, cx88_boards[core->board].name,
                        sizeof(cap->card));
                sprintf(cap->bus_info,"PCI:%s",pci_name(dev->pci));
@@ -1335,15 +1137,134 @@ static int video_do_ioctl(struct inode *inode, struct file *file,
                        V4L2_CAP_READWRITE     |
                        V4L2_CAP_STREAMING     |
                        V4L2_CAP_VBI_CAPTURE   |
-#if 0
                        V4L2_CAP_VIDEO_OVERLAY |
-#endif
                        0;
                if (UNSET != core->tuner_type)
                        cap->capabilities |= V4L2_CAP_TUNER;
                return 0;
        }
 
+       /* --- capture ioctls ---------------------------------------- */
+       case VIDIOC_ENUM_FMT:
+       {
+               struct v4l2_fmtdesc *f = arg;
+               enum v4l2_buf_type type;
+               unsigned int index;
+
+               index = f->index;
+               type  = f->type;
+               switch (type) {
+               case V4L2_BUF_TYPE_VIDEO_CAPTURE:
+                       if (index >= ARRAY_SIZE(formats))
+                               return -EINVAL;
+                       memset(f,0,sizeof(*f));
+                       f->index = index;
+                       f->type  = type;
+                       strlcpy(f->description,formats[index].name,sizeof(f->description));
+                       f->pixelformat = formats[index].fourcc;
+                       break;
+               default:
+                       return -EINVAL;
+               }
+               return 0;
+       }
+       case VIDIOC_G_FMT:
+       {
+               struct v4l2_format *f = arg;
+               return cx8800_g_fmt(dev,fh,f);
+       }
+       case VIDIOC_S_FMT:
+       {
+               struct v4l2_format *f = arg;
+               return cx8800_s_fmt(dev,fh,f);
+       }
+       case VIDIOC_TRY_FMT:
+       {
+               struct v4l2_format *f = arg;
+               return cx8800_try_fmt(dev,fh,f);
+       }
+#ifdef HAVE_V4L1
+       /* --- streaming capture ------------------------------------- */
+       case VIDIOCGMBUF:
+       {
+               struct video_mbuf *mbuf = arg;
+               struct videobuf_queue *q;
+               struct v4l2_requestbuffers req;
+               unsigned int i;
+
+               q = get_queue(fh);
+               memset(&req,0,sizeof(req));
+               req.type   = q->type;
+               req.count  = 8;
+               req.memory = V4L2_MEMORY_MMAP;
+               err = videobuf_reqbufs(q,&req);
+               if (err < 0)
+                       return err;
+               memset(mbuf,0,sizeof(*mbuf));
+               mbuf->frames = req.count;
+               mbuf->size   = 0;
+               for (i = 0; i < mbuf->frames; i++) {
+                       mbuf->offsets[i]  = q->bufs[i]->boff;
+                       mbuf->size       += q->bufs[i]->bsize;
+               }
+               return 0;
+       }
+#endif
+       case VIDIOC_REQBUFS:
+               return videobuf_reqbufs(get_queue(fh), arg);
+
+       case VIDIOC_QUERYBUF:
+               return videobuf_querybuf(get_queue(fh), arg);
+
+       case VIDIOC_QBUF:
+               return videobuf_qbuf(get_queue(fh), arg);
+
+       case VIDIOC_DQBUF:
+               return videobuf_dqbuf(get_queue(fh), arg,
+                                       file->f_flags & O_NONBLOCK);
+
+       case VIDIOC_STREAMON:
+       {
+               int res = get_ressource(fh);
+
+               if (!res_get(dev,fh,res))
+                       return -EBUSY;
+               return videobuf_streamon(get_queue(fh));
+       }
+       case VIDIOC_STREAMOFF:
+       {
+               int res = get_ressource(fh);
+
+               err = videobuf_streamoff(get_queue(fh));
+               if (err < 0)
+                       return err;
+               res_free(dev,fh,res);
+               return 0;
+       }
+       default:
+               return cx88_do_ioctl( inode, file, fh->radio, core, cmd, arg, video_do_ioctl );
+       }
+       return 0;
+}
+
+int cx88_do_ioctl(struct inode *inode, struct file *file, int radio,
+                 struct cx88_core *core, unsigned int cmd, void *arg, v4l2_kioctl driver_ioctl)
+{
+       int err;
+
+       if (video_debug) {
+              if (video_debug > 1) {
+                      if (_IOC_DIR(cmd) & _IOC_WRITE)
+                              v4l_printk_ioctl_arg("cx88(w)",cmd, arg);
+                      else if (!_IOC_DIR(cmd) & _IOC_READ) {
+                              v4l_print_ioctl("cx88", cmd);
+                      }
+              } else
+                      v4l_print_ioctl(core->name,cmd);
+
+       }
+
+       switch (cmd) {
        /* ---------- tv norms ---------- */
        case VIDIOC_ENUMSTD:
        {
@@ -1378,9 +1299,9 @@ static int video_do_ioctl(struct inode *inode, struct file *file,
                if (i == ARRAY_SIZE(tvnorms))
                        return -EINVAL;
 
-               down(&dev->lock);
-               cx88_set_tvnorm(dev->core,&tvnorms[i]);
-               up(&dev->lock);
+               mutex_lock(&core->lock);
+               cx88_set_tvnorm(core,&tvnorms[i]);
+               mutex_unlock(&core->lock);
                return 0;
        }
 
@@ -1421,7 +1342,7 @@ static int video_do_ioctl(struct inode *inode, struct file *file,
        {
                unsigned int *i = arg;
 
-               *i = dev->core->input;
+               *i = core->input;
                return 0;
        }
        case VIDIOC_S_INPUT:
@@ -1430,84 +1351,14 @@ static int video_do_ioctl(struct inode *inode, struct file *file,
 
                if (*i >= 4)
                        return -EINVAL;
-               down(&dev->lock);
+               mutex_lock(&core->lock);
                cx88_newstation(core);
-               video_mux(dev,*i);
-               up(&dev->lock);
+               video_mux(core,*i);
+               mutex_unlock(&core->lock);
                return 0;
        }
 
 
-#if 0
-       /* needs review */
-       case VIDIOC_G_AUDIO:
-       {
-               struct v4l2_audio *a = arg;
-               unsigned int n = a->index;
-
-               memset(a,0,sizeof(*a));
-               a->index = n;
-               switch (n) {
-               case 0:
-                       if ((CX88_VMUX_TELEVISION == INPUT(n)->type)
-                           || (CX88_VMUX_CABLE == INPUT(n)->type)) {
-                               strcpy(a->name,"Television");
-                               // FIXME figure out if stereo received and set V4L2_AUDCAP_STEREO.
-                               return 0;
-                       }
-                       break;
-               case 1:
-                       if (CX88_BOARD_DVICO_FUSIONHDTV_3_GOLD == core->board) {
-                               strcpy(a->name,"Line In");
-                               a->capability = V4L2_AUDCAP_STEREO;
-                               return 0;
-                       }
-                       break;
-               }
-               // Audio input not available.
-               return -EINVAL;
-       }
-#endif
-
-       /* --- capture ioctls ---------------------------------------- */
-       case VIDIOC_ENUM_FMT:
-       {
-               struct v4l2_fmtdesc *f = arg;
-               enum v4l2_buf_type type;
-               unsigned int index;
-
-               index = f->index;
-               type  = f->type;
-               switch (type) {
-               case V4L2_BUF_TYPE_VIDEO_CAPTURE:
-                       if (index >= ARRAY_SIZE(formats))
-                               return -EINVAL;
-                       memset(f,0,sizeof(*f));
-                       f->index = index;
-                       f->type  = type;
-                       strlcpy(f->description,formats[index].name,sizeof(f->description));
-                       f->pixelformat = formats[index].fourcc;
-                       break;
-               default:
-                       return -EINVAL;
-               }
-               return 0;
-       }
-       case VIDIOC_G_FMT:
-       {
-               struct v4l2_format *f = arg;
-               return cx8800_g_fmt(dev,fh,f);
-       }
-       case VIDIOC_S_FMT:
-       {
-               struct v4l2_format *f = arg;
-               return cx8800_s_fmt(dev,fh,f);
-       }
-       case VIDIOC_TRY_FMT:
-       {
-               struct v4l2_format *f = arg;
-               return cx8800_try_fmt(dev,fh,f);
-       }
 
        /* --- controls ---------------------------------------------- */
        case VIDIOC_QUERYCTRL:
@@ -1529,9 +1380,9 @@ static int video_do_ioctl(struct inode *inode, struct file *file,
                return 0;
        }
        case VIDIOC_G_CTRL:
-               return get_control(dev,arg);
+               return get_control(core,arg);
        case VIDIOC_S_CTRL:
-               return set_control(dev,arg);
+               return set_control(core,arg);
 
        /* --- tuner ioctls ------------------------------------------ */
        case VIDIOC_G_TUNER:
@@ -1552,7 +1403,7 @@ static int video_do_ioctl(struct inode *inode, struct file *file,
 
                cx88_get_stereo(core ,t);
                reg = cx_read(MO_DEVICE_STATUS);
-                t->signal = (reg & (1<<5)) ? 0xffff : 0x0000;
+               t->signal = (reg & (1<<5)) ? 0xffff : 0x0000;
                return 0;
        }
        case VIDIOC_S_TUNER:
@@ -1570,13 +1421,17 @@ static int video_do_ioctl(struct inode *inode, struct file *file,
        {
                struct v4l2_frequency *f = arg;
 
+               memset(f,0,sizeof(*f));
+
                if (UNSET == core->tuner_type)
                        return -EINVAL;
-               if (f->tuner != 0)
-                       return -EINVAL;
-               memset(f,0,sizeof(*f));
-               f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
-               f->frequency = dev->freq;
+
+               /* f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV; */
+               f->type = radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
+               f->frequency = core->freq;
+
+               cx88_call_i2c_clients(core,VIDIOC_G_FREQUENCY,f);
+
                return 0;
        }
        case VIDIOC_S_FREQUENCY:
@@ -1587,82 +1442,26 @@ static int video_do_ioctl(struct inode *inode, struct file *file,
                        return -EINVAL;
                if (f->tuner != 0)
                        return -EINVAL;
-               if (0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV)
+               if (0 == radio && f->type != V4L2_TUNER_ANALOG_TV)
                        return -EINVAL;
-               if (1 == fh->radio && f->type != V4L2_TUNER_RADIO)
+               if (1 == radio && f->type != V4L2_TUNER_RADIO)
                        return -EINVAL;
-               down(&dev->lock);
-               dev->freq = f->frequency;
+               mutex_lock(&core->lock);
+               core->freq = f->frequency;
                cx88_newstation(core);
-#ifdef V4L2_I2C_CLIENTS
-               cx88_call_i2c_clients(dev->core,VIDIOC_S_FREQUENCY,f);
-#else
-               cx88_call_i2c_clients(dev->core,VIDIOCSFREQ,&dev->freq);
-#endif
-               up(&dev->lock);
-               return 0;
-       }
-
-       /* --- streaming capture ------------------------------------- */
-       case VIDIOCGMBUF:
-       {
-               struct video_mbuf *mbuf = arg;
-               struct videobuf_queue *q;
-               struct v4l2_requestbuffers req;
-               unsigned int i;
+               cx88_call_i2c_clients(core,VIDIOC_S_FREQUENCY,f);
 
-               q = get_queue(fh);
-               memset(&req,0,sizeof(req));
-               req.type   = q->type;
-               req.count  = 8;
-               req.memory = V4L2_MEMORY_MMAP;
-               err = videobuf_reqbufs(q,&req);
-               if (err < 0)
-                       return err;
-               memset(mbuf,0,sizeof(*mbuf));
-               mbuf->frames = req.count;
-               mbuf->size   = 0;
-               for (i = 0; i < mbuf->frames; i++) {
-                       mbuf->offsets[i]  = q->bufs[i]->boff;
-                       mbuf->size       += q->bufs[i]->bsize;
-               }
-               return 0;
-       }
-       case VIDIOC_REQBUFS:
-               return videobuf_reqbufs(get_queue(fh), arg);
-
-       case VIDIOC_QUERYBUF:
-               return videobuf_querybuf(get_queue(fh), arg);
-
-       case VIDIOC_QBUF:
-               return videobuf_qbuf(get_queue(fh), arg);
-
-       case VIDIOC_DQBUF:
-               return videobuf_dqbuf(get_queue(fh), arg,
-                                     file->f_flags & O_NONBLOCK);
-
-       case VIDIOC_STREAMON:
-       {
-               int res = get_ressource(fh);
-
-                if (!res_get(dev,fh,res))
-                       return -EBUSY;
-               return videobuf_streamon(get_queue(fh));
-       }
-       case VIDIOC_STREAMOFF:
-       {
-               int res = get_ressource(fh);
+               /* When changing channels it is required to reset TVAUDIO */
+               msleep (10);
+               cx88_set_tvaudio(core);
 
-               err = videobuf_streamoff(get_queue(fh));
-               if (err < 0)
-                       return err;
-               res_free(dev,fh,res);
+               mutex_unlock(&core->lock);
                return 0;
        }
 
        default:
                return v4l_compat_translate_ioctl(inode,file,cmd,arg,
-                                                 video_do_ioctl);
+                                                 driver_ioctl);
        }
        return 0;
 }
@@ -1670,7 +1469,19 @@ static int video_do_ioctl(struct inode *inode, struct file *file,
 static int video_ioctl(struct inode *inode, struct file *file,
                       unsigned int cmd, unsigned long arg)
 {
-       return video_usercopy(inode, file, cmd, arg, video_do_ioctl);
+       int retval;
+
+       retval=video_usercopy(inode, file, cmd, arg, video_do_ioctl);
+
+       if (video_debug > 1) {
+              if (retval < 0) {
+                      v4l_print_ioctl("cx88(err)", cmd);
+                      printk(KERN_DEBUG "cx88(err): errcode=%d\n",retval);
+              } else if (_IOC_DIR(cmd) & _IOC_READ)
+                      v4l_printk_ioctl_arg("cx88(r)",cmd, (void *)arg);
+       }
+
+       return retval;
 }
 
 /* ----------------------------------------------------------- */
@@ -1683,7 +1494,7 @@ static int radio_do_ioctl(struct inode *inode, struct file *file,
        struct cx88_core  *core = dev->core;
 
        if (video_debug > 1)
-               cx88_print_ioctl(core->name,cmd);
+               v4l_print_ioctl(core->name,cmd);
 
        switch (cmd) {
        case VIDIOC_QUERYCAP:
@@ -1691,7 +1502,7 @@ static int radio_do_ioctl(struct inode *inode, struct file *file,
                struct v4l2_capability *cap = arg;
 
                memset(cap,0,sizeof(*cap));
-                strcpy(cap->driver, "cx8800");
+               strcpy(cap->driver, "cx8800");
                strlcpy(cap->card, cx88_boards[core->board].name,
                        sizeof(cap->card));
                sprintf(cap->bus_info,"PCI:%s", pci_name(dev->pci));
@@ -1708,19 +1519,9 @@ static int radio_do_ioctl(struct inode *inode, struct file *file,
 
                memset(t,0,sizeof(*t));
                strcpy(t->name, "Radio");
-                t->rangelow  = (int)(65*16);
-                t->rangehigh = (int)(108*16);
-
-#ifdef V4L2_I2C_CLIENTS
-               cx88_call_i2c_clients(dev->core,VIDIOC_G_TUNER,t);
-#else
-               {
-                       struct video_tuner vt;
-                       memset(&vt,0,sizeof(vt));
-                       cx88_call_i2c_clients(dev,VIDIOCGTUNER,&vt);
-                       t->signal = vt.signal;
-               }
-#endif
+               t->type = V4L2_TUNER_RADIO;
+
+               cx88_call_i2c_clients(core,VIDIOC_G_TUNER,t);
                return 0;
        }
        case VIDIOC_ENUMINPUT:
@@ -1753,8 +1554,31 @@ static int radio_do_ioctl(struct inode *inode, struct file *file,
                *id = 0;
                return 0;
        }
-       case VIDIOC_S_AUDIO:
+#ifdef HAVE_V4L1
+       case VIDIOCSTUNER:
+       {
+               struct video_tuner *v = arg;
+
+               if (v->tuner) /* Only tuner 0 */
+                       return -EINVAL;
+
+               cx88_call_i2c_clients(core,VIDIOCSTUNER,v);
+               return 0;
+       }
+#endif
        case VIDIOC_S_TUNER:
+       {
+               struct v4l2_tuner *t = arg;
+
+               if (0 != t->index)
+                       return -EINVAL;
+
+               cx88_call_i2c_clients(core,VIDIOC_S_TUNER,t);
+
+               return 0;
+       }
+
+       case VIDIOC_S_AUDIO:
        case VIDIOC_S_INPUT:
        case VIDIOC_S_STD:
                return 0;
@@ -1807,7 +1631,7 @@ static void cx8800_vid_timeout(unsigned long data)
        struct cx88_buffer *buf;
        unsigned long flags;
 
-       cx88_sram_channel_dump(dev->core, &cx88_sram_channels[SRAM_CH21]);
+       cx88_sram_channel_dump(core, &cx88_sram_channels[SRAM_CH21]);
 
        cx_clear(MO_VID_DMACNTRL, 0x11);
        cx_clear(VID_CAPTURE_CONTROL, 0x06);
@@ -1825,6 +1649,14 @@ static void cx8800_vid_timeout(unsigned long data)
        spin_unlock_irqrestore(&dev->slock,flags);
 }
 
+static char *cx88_vid_irqs[32] = {
+       "y_risci1", "u_risci1", "v_risci1", "vbi_risc1",
+       "y_risci2", "u_risci2", "v_risci2", "vbi_risc2",
+       "y_oflow",  "u_oflow",  "v_oflow",  "vbi_oflow",
+       "y_sync",   "u_sync",   "v_sync",   "vbi_sync",
+       "opc_err",  "par_err",  "rip_err",  "pci_abort",
+};
+
 static void cx8800_vid_irq(struct cx8800_dev *dev)
 {
        struct cx88_core *core = dev->core;
@@ -1844,14 +1676,14 @@ static void cx8800_vid_irq(struct cx8800_dev *dev)
                printk(KERN_WARNING "%s/0: video risc op code error\n",core->name);
                cx_clear(MO_VID_DMACNTRL, 0x11);
                cx_clear(VID_CAPTURE_CONTROL, 0x06);
-               cx88_sram_channel_dump(dev->core, &cx88_sram_channels[SRAM_CH21]);
+               cx88_sram_channel_dump(core, &cx88_sram_channels[SRAM_CH21]);
        }
 
        /* risc1 y */
        if (status & 0x01) {
                spin_lock(&dev->slock);
                count = cx_read(MO_VIDY_GPCNT);
-               cx88_wakeup(dev->core, &dev->vidq, count);
+               cx88_wakeup(core, &dev->vidq, count);
                spin_unlock(&dev->slock);
        }
 
@@ -1859,7 +1691,7 @@ static void cx8800_vid_irq(struct cx8800_dev *dev)
        if (status & 0x08) {
                spin_lock(&dev->slock);
                count = cx_read(MO_VBI_GPCNT);
-               cx88_wakeup(dev->core, &dev->vbiq, count);
+               cx88_wakeup(core, &dev->vbiq, count);
                spin_unlock(&dev->slock);
        }
 
@@ -1921,6 +1753,7 @@ static struct file_operations video_fops =
        .poll          = video_poll,
        .mmap          = video_mmap,
        .ioctl         = video_ioctl,
+       .compat_ioctl  = v4l_compat_ioctl32,
        .llseek        = no_llseek,
 };
 
@@ -1948,6 +1781,7 @@ static struct file_operations radio_fops =
        .open          = video_open,
        .release       = video_release,
        .ioctl         = radio_ioctl,
+       .compat_ioctl  = v4l_compat_ioctl32,
        .llseek        = no_llseek,
 };
 
@@ -1994,10 +1828,9 @@ static int __devinit cx8800_initdev(struct pci_dev *pci_dev,
        struct cx88_core *core;
        int err;
 
-       dev = kmalloc(sizeof(*dev),GFP_KERNEL);
+       dev = kzalloc(sizeof(*dev),GFP_KERNEL);
        if (NULL == dev)
                return -ENOMEM;
-       memset(dev,0,sizeof(*dev));
 
        /* pci init */
        dev->pci = pci_dev;
@@ -2014,8 +1847,8 @@ static int __devinit cx8800_initdev(struct pci_dev *pci_dev,
 
        /* print pci info */
        pci_read_config_byte(pci_dev, PCI_CLASS_REVISION, &dev->pci_rev);
-        pci_read_config_byte(pci_dev, PCI_LATENCY_TIMER,  &dev->pci_lat);
-        printk(KERN_INFO "%s/0: found at %s, rev: %d, irq: %d, "
+       pci_read_config_byte(pci_dev, PCI_LATENCY_TIMER,  &dev->pci_lat);
+       printk(KERN_INFO "%s/0: found at %s, rev: %d, irq: %d, "
               "latency: %d, mmio: 0x%lx\n", core->name,
               pci_name(pci_dev), dev->pci_rev, pci_dev->irq,
               dev->pci_lat,pci_resource_start(pci_dev,0));
@@ -2028,7 +1861,6 @@ static int __devinit cx8800_initdev(struct pci_dev *pci_dev,
        }
 
        /* initialize driver struct */
-        init_MUTEX(&dev->lock);
        spin_lock_init(&dev->slock);
        core->tvnorm = tvnorms;
 
@@ -2065,10 +1897,6 @@ static int __devinit cx8800_initdev(struct pci_dev *pci_dev,
                request_module("tuner");
        if (core->tda9887_conf)
                request_module("tda9887");
-       if (core->tuner_type != UNSET)
-               cx88_call_i2c_clients(dev->core,TUNER_SET_TYPE,&core->tuner_type);
-       if (core->tda9887_conf)
-               cx88_call_i2c_clients(dev->core,TDA9887_SET_CONFIG,&core->tda9887_conf);
 
        /* register v4l devices */
        dev->video_dev = cx88_vdev_init(core,dev->pci,
@@ -2113,11 +1941,11 @@ static int __devinit cx8800_initdev(struct pci_dev *pci_dev,
        pci_set_drvdata(pci_dev,dev);
 
        /* initial device configuration */
-       down(&dev->lock);
-       init_controls(dev);
-       cx88_set_tvnorm(dev->core,tvnorms);
-       video_mux(dev,0);
-       up(&dev->lock);
+       mutex_lock(&core->lock);
+       cx88_set_tvnorm(core,tvnorms);
+       init_controls(core);
+       video_mux(core,0);
+       mutex_unlock(&core->lock);
 
        /* start tvaudio thread */
        if (core->tuner_type != TUNER_ABSENT)
@@ -2136,15 +1964,16 @@ fail_free:
 
 static void __devexit cx8800_finidev(struct pci_dev *pci_dev)
 {
-        struct cx8800_dev *dev = pci_get_drvdata(pci_dev);
+       struct cx8800_dev *dev = pci_get_drvdata(pci_dev);
+       struct cx88_core *core = dev->core;
 
        /* stop thread */
-       if (dev->core->kthread) {
-               kthread_stop(dev->core->kthread);
-               dev->core->kthread = NULL;
+       if (core->kthread) {
+               kthread_stop(core->kthread);
+               core->kthread = NULL;
        }
 
-       cx88_shutdown(dev->core); /* FIXME */
+       cx88_shutdown(core); /* FIXME */
        pci_disable_device(pci_dev);
 
        /* unregister stuff */
@@ -2156,13 +1985,13 @@ static void __devexit cx8800_finidev(struct pci_dev *pci_dev)
        /* free memory */
        btcx_riscmem_free(dev->pci,&dev->vidq.stopper);
        list_del(&dev->devlist);
-       cx88_core_put(dev->core,dev->pci);
+       cx88_core_put(core,dev->pci);
        kfree(dev);
 }
 
 static int cx8800_suspend(struct pci_dev *pci_dev, pm_message_t state)
 {
-        struct cx8800_dev *dev = pci_get_drvdata(pci_dev);
+       struct cx8800_dev *dev = pci_get_drvdata(pci_dev);
        struct cx88_core *core = dev->core;
 
        /* stop video+vbi capture */
@@ -2179,10 +2008,8 @@ static int cx8800_suspend(struct pci_dev *pci_dev, pm_message_t state)
        }
        spin_unlock(&dev->slock);
 
-#if 1
        /* FIXME -- shutdown device */
-       cx88_shutdown(dev->core);
-#endif
+       cx88_shutdown(core);
 
        pci_save_state(pci_dev);
        if (0 != pci_set_power_state(pci_dev, pci_choose_state(pci_dev, state))) {
@@ -2194,20 +2021,34 @@ static int cx8800_suspend(struct pci_dev *pci_dev, pm_message_t state)
 
 static int cx8800_resume(struct pci_dev *pci_dev)
 {
-        struct cx8800_dev *dev = pci_get_drvdata(pci_dev);
+       struct cx8800_dev *dev = pci_get_drvdata(pci_dev);
        struct cx88_core *core = dev->core;
+       int err;
 
        if (dev->state.disabled) {
-               pci_enable_device(pci_dev);
+               err=pci_enable_device(pci_dev);
+               if (err) {
+                       printk(KERN_ERR "%s: can't enable device\n",
+                                                      core->name);
+                       return err;
+               }
+
                dev->state.disabled = 0;
        }
-       pci_set_power_state(pci_dev, PCI_D0);
+       err= pci_set_power_state(pci_dev, PCI_D0);
+       if (err) {
+               printk(KERN_ERR "%s: can't enable device\n",
+                                      core->name);
+
+               pci_disable_device(pci_dev);
+               dev->state.disabled = 1;
+
+               return err;
+       }
        pci_restore_state(pci_dev);
 
-#if 1
        /* FIXME: re-initialize hardware */
-       cx88_reset(dev->core);
-#endif
+       cx88_reset(core);
 
        /* restart video+vbi capture */
        spin_lock(&dev->slock);
@@ -2230,8 +2071,8 @@ static struct pci_device_id cx8800_pci_tbl[] = {
        {
                .vendor       = 0x14f1,
                .device       = 0x8800,
-                .subvendor    = PCI_ANY_ID,
-                .subdevice    = PCI_ANY_ID,
+               .subvendor    = PCI_ANY_ID,
+               .subdevice    = PCI_ANY_ID,
        },{
                /* --- end of list --- */
        }
@@ -2239,10 +2080,10 @@ static struct pci_device_id cx8800_pci_tbl[] = {
 MODULE_DEVICE_TABLE(pci, cx8800_pci_tbl);
 
 static struct pci_driver cx8800_pci_driver = {
-        .name     = "cx8800",
-        .id_table = cx8800_pci_tbl,
-        .probe    = cx8800_initdev,
-        .remove   = __devexit_p(cx8800_finidev),
+       .name     = "cx8800",
+       .id_table = cx8800_pci_tbl,
+       .probe    = cx8800_initdev,
+       .remove   = __devexit_p(cx8800_finidev),
 
        .suspend  = cx8800_suspend,
        .resume   = cx8800_resume,
@@ -2269,9 +2110,12 @@ static void cx8800_fini(void)
 module_init(cx8800_init);
 module_exit(cx8800_fini);
 
+EXPORT_SYMBOL(cx88_do_ioctl);
+
 /* ----------------------------------------------------------- */
 /*
  * Local variables:
  * c-basic-offset: 8
  * End:
+ * 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
  */