V4L/DVB (3344a): Conversions from kmalloc+memset to k(z|c)alloc
[pandora-kernel.git] / drivers / media / video / cx88 / cx88-video.c
index 5f58c10..073494c 100644 (file)
@@ -1,5 +1,4 @@
 /*
- * $Id: cx88-video.c,v 1.82 2005/07/22 05:13:34 mkrufky Exp $
  *
  * device driver for Conexant 2388x based TV cards
  * video4linux video interface
 #include <asm/div64.h>
 
 #include "cx88.h"
+#include <media/v4l2-common.h>
+
+/* Include V4L1 specific functions. Should be removed soon */
+#include <linux/videodev.h>
 
 MODULE_DESCRIPTION("v4l2 driver module for cx2388x based TV cards");
 MODULE_AUTHOR("Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]");
@@ -66,7 +69,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)
 
 /* ------------------------------------------------------------------ */
 
@@ -101,7 +104,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,
@@ -238,7 +241,7 @@ 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,
@@ -269,7 +272,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,
@@ -283,6 +286,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,
@@ -296,7 +300,7 @@ static struct cx88_ctrl cx8800_ctls[] = {
                        .minimum       = 0,
                        .maximum       = 0x3f,
                        .step          = 1,
-                       .default_value = 0,
+                       .default_value = 0x1f,
                        .type          = V4L2_CTRL_TYPE_INTEGER,
                },
                .reg                   = AUD_VOL_CTL,
@@ -326,22 +330,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);
+       down(&core->lock);
        if (dev->resources & bit) {
                /* no, someone else uses it */
-               up(&dev->lock);
+               up(&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);
+       up(&core->lock);
        return 1;
 }
 
@@ -360,27 +365,29 @@ 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)
 {
+       struct cx88_core *core = dev->core;
        if ((fh->resources & bits) != bits)
                BUG();
 
-       down(&dev->lock);
+       down(&core->lock);
        fh->resources  &= ~bits;
        dev->resources &= ~bits;
        dprintk(1,"res: put %d\n",bits);
-       up(&dev->lock);
+       up(&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);
@@ -413,9 +420,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 */
@@ -424,6 +431,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 */
@@ -431,7 +446,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;
 }
@@ -455,11 +470,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);
@@ -475,7 +491,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);
@@ -524,12 +540,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)
@@ -609,6 +626,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 */
@@ -701,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;
@@ -725,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;
@@ -755,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);
-               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;
 
@@ -857,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;
 }
 
@@ -870,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;
@@ -895,27 +918,32 @@ static int get_control(struct cx8800_dev *dev, struct v4l2_control *ctl)
                ctl->value = ((value + (c->off << c->shift)) & c->mask) >> c->shift;
                break;
        }
+       printk("get_control id=0x%X reg=0x%02x val=0x%02x (mask 0x%02x)%s\n",
+                                       ctl->id, c->reg, ctl->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;
@@ -925,55 +953,44 @@ static int set_control(struct cx8800_dev *dev, struct v4l2_control *ctl)
                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]" : "");
+       printk("set_control id=0x%X reg=0x%02x val=0x%02x (mask 0x%02x)%s\n",
+                                       ctl->id, 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,
-       };
-       static struct v4l2_control hue = {
-               .id    = V4L2_CID_HUE,
-               .value = 0x80,
-       };
-       static struct v4l2_control contrast = {
-               .id    = V4L2_CID_CONTRAST,
-               .value = 0x80,
-       };
-       static struct v4l2_control brightness = {
-               .id    = V4L2_CID_BRIGHTNESS,
-               .value = 0x80,
-       };
+       struct v4l2_control ctrl;
+       int i;
 
-       set_control(dev,&mute);
-       set_control(dev,&volume);
-       set_control(dev,&hue);
-       set_control(dev,&contrast);
-       set_control(dev,&brightness);
+       for (i = 0; i < CX8800_CTLS; i++) {
+               ctrl.id=cx8800_ctls[i].v.id;
+               ctrl.value=cx8800_ctls[i].v.default_value
+                               +cx8800_ctls[i].off;
+               set_control(core, &ctrl);
+       }
 }
 
 /* ------------------------------------------------------------------ */
@@ -1004,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:
        {
@@ -1016,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)
@@ -1099,14 +1118,16 @@ static int video_do_ioctl(struct inode *inode, struct file *file,
        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));
@@ -1116,12 +1137,126 @@ static int video_do_ioctl(struct inode *inode, struct file *file,
                        V4L2_CAP_READWRITE     |
                        V4L2_CAP_STREAMING     |
                        V4L2_CAP_VBI_CAPTURE   |
+                       V4L2_CAP_VIDEO_OVERLAY |
                        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;
+
+       dprintk( 1, "CORE IOCTL: 0x%x\n", cmd );
+       if (video_debug > 1)
+               v4l_print_ioctl(core->name,cmd);
+
+       switch (cmd) {
        /* ---------- tv norms ---------- */
        case VIDIOC_ENUMSTD:
        {
@@ -1156,9 +1291,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);
+               down(&core->lock);
+               cx88_set_tvnorm(core,&tvnorms[i]);
+               up(&core->lock);
                return 0;
        }
 
@@ -1199,7 +1334,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:
@@ -1208,55 +1343,15 @@ static int video_do_ioctl(struct inode *inode, struct file *file,
 
                if (*i >= 4)
                        return -EINVAL;
-               down(&dev->lock);
+               down(&core->lock);
                cx88_newstation(core);
-               video_mux(dev,*i);
-               up(&dev->lock);
+               video_mux(core,*i);
+               up(&core->lock);
                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);
-       }
-
        /* --- controls ---------------------------------------------- */
        case VIDIOC_QUERYCTRL:
        {
@@ -1277,9 +1372,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:
@@ -1300,7 +1395,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:
@@ -1323,10 +1418,11 @@ static int video_do_ioctl(struct inode *inode, struct file *file,
                if (UNSET == core->tuner_type)
                        return -EINVAL;
 
-               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(dev->core,VIDIOC_G_FREQUENCY,f);
+               cx88_call_i2c_clients(core,VIDIOC_G_FREQUENCY,f);
 
                return 0;
        }
@@ -1338,83 +1434,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;
+               down(&core->lock);
+               core->freq = f->frequency;
                cx88_newstation(core);
-               cx88_call_i2c_clients(dev->core,VIDIOC_S_FREQUENCY,f);
+               cx88_call_i2c_clients(core,VIDIOC_S_FREQUENCY,f);
 
                /* When changing channels it is required to reset TVAUDIO */
                msleep (10);
                cx88_set_tvaudio(core);
 
-               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;
-
-               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);
-
-               err = videobuf_streamoff(get_queue(fh));
-               if (err < 0)
-                       return err;
-               res_free(dev,fh,res);
+               up(&core->lock);
                return 0;
        }
 
        default:
                return v4l_compat_translate_ioctl(inode,file,cmd,arg,
-                                                 video_do_ioctl);
+                                                 driver_ioctl);
        }
        return 0;
 }
@@ -1435,7 +1474,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:
@@ -1443,7 +1482,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));
@@ -1460,8 +1499,9 @@ static int radio_do_ioctl(struct inode *inode, struct file *file,
 
                memset(t,0,sizeof(*t));
                strcpy(t->name, "Radio");
+               t->type = V4L2_TUNER_RADIO;
 
-               cx88_call_i2c_clients(dev->core,VIDIOC_G_TUNER,t);
+               cx88_call_i2c_clients(core,VIDIOC_G_TUNER,t);
                return 0;
        }
        case VIDIOC_ENUMINPUT:
@@ -1494,6 +1534,7 @@ static int radio_do_ioctl(struct inode *inode, struct file *file,
                *id = 0;
                return 0;
        }
+#ifdef HAVE_V4L1
        case VIDIOCSTUNER:
        {
                struct video_tuner *v = arg;
@@ -1501,9 +1542,10 @@ static int radio_do_ioctl(struct inode *inode, struct file *file,
                if (v->tuner) /* Only tuner 0 */
                        return -EINVAL;
 
-               cx88_call_i2c_clients(dev->core,VIDIOCSTUNER,v);
-                return 0;
+               cx88_call_i2c_clients(core,VIDIOCSTUNER,v);
+               return 0;
        }
+#endif
        case VIDIOC_S_TUNER:
        {
                struct v4l2_tuner *t = arg;
@@ -1511,7 +1553,7 @@ static int radio_do_ioctl(struct inode *inode, struct file *file,
                if (0 != t->index)
                        return -EINVAL;
 
-               cx88_call_i2c_clients(dev->core,VIDIOC_S_TUNER,t);
+               cx88_call_i2c_clients(core,VIDIOC_S_TUNER,t);
 
                return 0;
        }
@@ -1569,7 +1611,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);
@@ -1614,14 +1656,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);
        }
 
@@ -1629,7 +1671,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);
        }
 
@@ -1691,6 +1733,7 @@ static struct file_operations video_fops =
        .poll          = video_poll,
        .mmap          = video_mmap,
        .ioctl         = video_ioctl,
+       .compat_ioctl  = v4l_compat_ioctl32,
        .llseek        = no_llseek,
 };
 
@@ -1718,6 +1761,7 @@ static struct file_operations radio_fops =
        .open          = video_open,
        .release       = video_release,
        .ioctl         = radio_ioctl,
+       .compat_ioctl  = v4l_compat_ioctl32,
        .llseek        = no_llseek,
 };
 
@@ -1764,10 +1808,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;
@@ -1784,8 +1827,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));
@@ -1798,7 +1841,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;
 
@@ -1835,6 +1877,7 @@ static int __devinit cx8800_initdev(struct pci_dev *pci_dev,
                request_module("tuner");
        if (core->tda9887_conf)
                request_module("tda9887");
+
        /* register v4l devices */
        dev->video_dev = cx88_vdev_init(core,dev->pci,
                                        &cx8800_video_template,"video");
@@ -1878,11 +1921,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);
+       down(&core->lock);
+       cx88_set_tvnorm(core,tvnorms);
+       init_controls(core);
+       video_mux(core,0);
+       up(&core->lock);
 
        /* start tvaudio thread */
        if (core->tuner_type != TUNER_ABSENT)
@@ -1901,15 +1944,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 */
@@ -1921,7 +1965,7 @@ 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);
 }
 
@@ -1945,7 +1989,7 @@ static int cx8800_suspend(struct pci_dev *pci_dev, pm_message_t state)
        spin_unlock(&dev->slock);
 
        /* FIXME -- shutdown device */
-       cx88_shutdown(dev->core);
+       cx88_shutdown(core);
 
        pci_save_state(pci_dev);
        if (0 != pci_set_power_state(pci_dev, pci_choose_state(pci_dev, state))) {
@@ -1959,16 +2003,32 @@ static int cx8800_resume(struct pci_dev *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);
 
        /* FIXME: re-initialize hardware */
-       cx88_reset(dev->core);
+       cx88_reset(core);
 
        /* restart video+vbi capture */
        spin_lock(&dev->slock);
@@ -2030,6 +2090,8 @@ static void cx8800_fini(void)
 module_init(cx8800_init);
 module_exit(cx8800_fini);
 
+EXPORT_SYMBOL(cx88_do_ioctl);
+
 /* ----------------------------------------------------------- */
 /*
  * Local variables: