V4L/DVB (13554a): v4l: Use the video_drvdata function in drivers
[safe/jmp/linux-2.6] / drivers / media / video / cx88 / cx88-blackbird.c
index a1be1e2..f0ef941 100644 (file)
@@ -3,7 +3,7 @@
  *  Support for a cx23416 mpeg encoder via cx2388x host port.
  *  "blackbird" reference design.
  *
- *    (c) 2004 Jelle Foks <jelle@foks.8m.com>
+ *    (c) 2004 Jelle Foks <jelle@foks.us>
  *    (c) 2004 Gerd Knorr <kraxel@bytesex.org>
  *
  *    (c) 2005-2006 Mauro Carvalho Chehab <mchehab@infradead.org>
  */
 
 #include <linux/module.h>
-#include <linux/moduleparam.h>
 #include <linux/init.h>
 #include <linux/fs.h>
 #include <linux/delay.h>
 #include <linux/device.h>
 #include <linux/firmware.h>
+#include <linux/smp_lock.h>
 #include <media/v4l2-common.h>
+#include <media/v4l2-ioctl.h>
 #include <media/cx2341x.h>
 
 #include "cx88.h"
 
 MODULE_DESCRIPTION("driver for cx2388x/cx23416 based mpeg encoder cards");
-MODULE_AUTHOR("Jelle Foks <jelle@foks.8m.com>, Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]");
+MODULE_AUTHOR("Jelle Foks <jelle@foks.us>, Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]");
 MODULE_LICENSE("GPL");
 
 static unsigned int mpegbufs = 32;
 module_param(mpegbufs,int,0644);
 MODULE_PARM_DESC(mpegbufs,"number of mpeg buffers, range 2-32");
 
-static unsigned int debug = 0;
+static unsigned int debug;
 module_param(debug,int,0644);
 MODULE_PARM_DESC(debug,"enable debug messages [blackbird]");
 
@@ -56,7 +57,7 @@ MODULE_PARM_DESC(debug,"enable debug messages [blackbird]");
 
 /* ------------------------------------------------------------------ */
 
-#define BLACKBIRD_FIRM_IMAGE_SIZE 256*1024
+#define BLACKBIRD_FIRM_IMAGE_SIZE 376836
 
 /* defines below are from ivtv-driver.h */
 
@@ -308,14 +309,14 @@ static int register_read(struct cx88_core *core, u32 address, u32 *value)
 
 /* ------------------------------------------------------------------ */
 
-static int blackbird_mbox_func(void *priv, int command, int in, int out, u32 data[CX2341X_MBOX_MAX_DATA])
+static int blackbird_mbox_func(void *priv, u32 command, int in, int out, u32 data[CX2341X_MBOX_MAX_DATA])
 {
        struct cx8802_dev *dev = priv;
        unsigned long timeout;
        u32 value, flag, retval;
        int i;
 
-       dprintk(1,"%s: 0x%X\n", __FUNCTION__, command);
+       dprintk(1,"%s: 0x%X\n", __func__, command);
 
        /* this may not be 100% safe if we can't read any memory location
           without side effects */
@@ -528,44 +529,6 @@ static void blackbird_codec_settings(struct cx8802_dev *dev)
        cx2341x_update(dev, blackbird_mbox_func, NULL, &dev->params);
 }
 
-static struct v4l2_mpeg_compression default_mpeg_params = {
-       .st_type          = V4L2_MPEG_PS_2,
-       .st_bitrate       = {
-               .mode     = V4L2_BITRATE_CBR,
-               .min      = 0,
-               .target   = 0,
-               .max      = 0
-       },
-       .ts_pid_pmt       = 16,
-       .ts_pid_audio     = 260,
-       .ts_pid_video     = 256,
-       .ts_pid_pcr       = 259,
-       .ps_size          = 0,
-       .au_type          = V4L2_MPEG_AU_2_II,
-       .au_bitrate       = {
-               .mode     = V4L2_BITRATE_CBR,
-               .min      = 224,
-               .target   = 224,
-               .max      = 224
-       },
-       .au_sample_rate    = 48000,
-       .au_pesid          = 0,
-       .vi_type           = V4L2_MPEG_VI_2,
-       .vi_aspect_ratio   = V4L2_MPEG_ASPECT_4_3,
-       .vi_bitrate        = {
-               .mode      = V4L2_BITRATE_CBR,
-               .min       = 4000,
-               .target    = 4500,
-               .max       = 6000
-       },
-       .vi_frame_rate     = 25,
-       .vi_frames_per_gop = 12,
-       .vi_bframes_count  = 2,
-       .vi_pesid          = 0,
-       .closed_gops       = 1,
-       .pulldown          = 0
-};
-
 static int blackbird_initialize_codec(struct cx8802_dev *dev)
 {
        struct cx88_core *core = dev->core;
@@ -575,19 +538,22 @@ static int blackbird_initialize_codec(struct cx8802_dev *dev)
        dprintk(1,"Initialize codec\n");
        retval = blackbird_api_cmd(dev, CX2341X_ENC_PING_FW, 0, 0); /* ping */
        if (retval < 0) {
+
+               dev->mpeg_active = 0;
+
                /* ping was not successful, reset and upload firmware */
                cx_write(MO_SRST_IO, 0); /* SYS_RSTO=0 */
-               msleep(1);
                cx_write(MO_SRST_IO, 1); /* SYS_RSTO=1 */
-               msleep(1);
                retval = blackbird_load_firmware(dev);
                if (retval < 0)
                        return retval;
 
-               dev->mailbox = blackbird_find_mailbox(dev);
-               if (dev->mailbox < 0)
+               retval = blackbird_find_mailbox(dev);
+               if (retval < 0)
                        return -1;
 
+               dev->mailbox = retval;
+
                retval = blackbird_api_cmd(dev, CX2341X_ENC_PING_FW, 0, 0); /* ping */
                if (retval < 0) {
                        dprintk(0, "ERROR: Firmware ping failed!\n");
@@ -601,7 +567,6 @@ static int blackbird_initialize_codec(struct cx8802_dev *dev)
                }
                dprintk(0, "Firmware version is 0x%08x\n", version);
        }
-       msleep(1);
 
        cx_write(MO_PINMUX_IO, 0x88); /* 656-8bit IO and enable MPEG parallel IO */
        cx_clear(MO_INPUT_FORMAT, 0x100); /* chroma subcarrier lock to normal? */
@@ -609,40 +574,68 @@ static int blackbird_initialize_codec(struct cx8802_dev *dev)
        cx_clear(MO_OUTPUT_FORMAT, 0x0008); /* Normal Y-limits to let the mpeg encoder sync */
 
        blackbird_codec_settings(dev);
-       msleep(1);
 
-       /* blackbird_api_cmd(dev, IVTV_API_ASSIGN_NUM_VSYNC_LINES, 4, 0, 0xef, 0xef);
-          blackbird_api_cmd(dev, IVTV_API_ASSIGN_NUM_VSYNC_LINES, 4, 0, 0xf0, 0xf0);
-          blackbird_api_cmd(dev, IVTV_API_ASSIGN_NUM_VSYNC_LINES, 4, 0, 0x180, 0x180); */
        blackbird_api_cmd(dev, CX2341X_ENC_SET_NUM_VSYNC_LINES, 2, 0,
                        BLACKBIRD_FIELD1_SAA7115,
                        BLACKBIRD_FIELD2_SAA7115
                );
 
-       /* blackbird_api_cmd(dev, IVTV_API_ASSIGN_PLACEHOLDER, 12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0); */
        blackbird_api_cmd(dev, CX2341X_ENC_SET_PLACEHOLDER, 12, 0,
                        BLACKBIRD_CUSTOM_EXTENSION_USR_DATA,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
 
-       /* initialize the video input */
-       blackbird_api_cmd(dev, CX2341X_ENC_INITIALIZE_INPUT, 0, 0);
+       return 0;
+}
 
-       msleep(1);
+static int blackbird_start_codec(struct file *file, void *priv)
+{
+       struct cx8802_dev *dev  = ((struct cx8802_fh *)priv)->dev;
+       struct cx88_core *core = dev->core;
+       /* start capturing to the host interface */
+       u32 reg;
 
-       blackbird_api_cmd(dev, CX2341X_ENC_MUTE_VIDEO, 1, 0, BLACKBIRD_UNMUTE);
-       msleep(1);
-       blackbird_api_cmd(dev, CX2341X_ENC_MUTE_AUDIO, 1, 0, BLACKBIRD_UNMUTE);
-       msleep(1);
+       int i;
+       int lastchange = -1;
+       int lastval = 0;
+
+       for (i = 0; (i < 10) && (i < (lastchange + 4)); i++) {
+               reg = cx_read(AUD_STATUS);
+
+               dprintk(1, "AUD_STATUS:%dL: 0x%x\n", i, reg);
+               if ((reg & 0x0F) != lastval) {
+                       lastval = reg & 0x0F;
+                       lastchange = i;
+               }
+               msleep(100);
+       }
+
+       /* unmute audio source */
+       cx_clear(AUD_VOL_CTL, (1 << 6));
+
+       blackbird_api_cmd(dev, CX2341X_ENC_REFRESH_INPUT, 0, 0);
+
+       /* initialize the video input */
+       blackbird_api_cmd(dev, CX2341X_ENC_INITIALIZE_INPUT, 0, 0);
 
        /* start capturing to the host interface */
-       /* blackbird_api_cmd(dev, CX2341X_ENC_START_CAPTURE, 2, 0, 0, 0x13); */
        blackbird_api_cmd(dev, CX2341X_ENC_START_CAPTURE, 2, 0,
                        BLACKBIRD_MPEG_CAPTURE,
                        BLACKBIRD_RAW_BITS_NONE
                );
-       msleep(10);
 
-       blackbird_api_cmd(dev, CX2341X_ENC_REFRESH_INPUT, 0,0);
+       dev->mpeg_active = 1;
+       return 0;
+}
+
+static int blackbird_stop_codec(struct cx8802_dev *dev)
+{
+       blackbird_api_cmd(dev, CX2341X_ENC_STOP_CAPTURE, 3, 0,
+                       BLACKBIRD_END_NOW,
+                       BLACKBIRD_MPEG_CAPTURE,
+                       BLACKBIRD_RAW_BITS_NONE
+               );
+
+       dev->mpeg_active = 0;
        return 0;
 }
 
@@ -704,7 +697,7 @@ static int blackbird_queryctrl(struct cx8802_dev *dev, struct v4l2_queryctrl *qc
                return -EINVAL;
 
        /* Standard V4L2 controls */
-       if (cx8800_ctrl_query(qctrl) == 0)
+       if (cx8800_ctrl_query(dev->core, qctrl) == 0)
                return 0;
 
        /* MPEG V4L2 controls */
@@ -724,7 +717,8 @@ static int vidioc_querymenu (struct file *file, void *priv,
 
        qctrl.id = qmenu->id;
        blackbird_queryctrl(dev, &qctrl);
-       return v4l2_ctrl_query_menu(qmenu, &qctrl, cx2341x_ctrl_get_menu(qmenu->id));
+       return v4l2_ctrl_query_menu(qmenu, &qctrl,
+                       cx2341x_ctrl_get_menu(&dev->params, qmenu->id));
 }
 
 static int vidioc_querycap (struct file *file, void  *priv,
@@ -734,37 +728,35 @@ static int vidioc_querycap (struct file *file, void  *priv,
        struct cx88_core  *core = dev->core;
 
        strcpy(cap->driver, "cx88_blackbird");
-       strlcpy(cap->card, cx88_boards[core->board].name,sizeof(cap->card));
+       strlcpy(cap->card, core->board.name, sizeof(cap->card));
        sprintf(cap->bus_info,"PCI:%s",pci_name(dev->pci));
        cap->version = CX88_VERSION_CODE;
        cap->capabilities =
                V4L2_CAP_VIDEO_CAPTURE |
                V4L2_CAP_READWRITE     |
                V4L2_CAP_STREAMING;
-       if (UNSET != core->tuner_type)
+       if (UNSET != core->board.tuner_type)
                cap->capabilities |= V4L2_CAP_TUNER;
        return 0;
 }
 
-static int vidioc_enum_fmt_cap (struct file *file, void  *priv,
+static int vidioc_enum_fmt_vid_cap (struct file *file, void  *priv,
                                        struct v4l2_fmtdesc *f)
 {
        if (f->index != 0)
                return -EINVAL;
 
        strlcpy(f->description, "MPEG", sizeof(f->description));
-       f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
        f->pixelformat = V4L2_PIX_FMT_MPEG;
        return 0;
 }
 
-static int vidioc_g_fmt_cap (struct file *file, void *priv,
+static int vidioc_g_fmt_vid_cap (struct file *file, void *priv,
                                        struct v4l2_format *f)
 {
        struct cx8802_fh  *fh   = priv;
        struct cx8802_dev *dev  = fh->dev;
 
-       f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
        f->fmt.pix.pixelformat  = V4L2_PIX_FMT_MPEG;
        f->fmt.pix.bytesperline = 0;
        f->fmt.pix.sizeimage    = dev->ts_packet_size * dev->ts_packet_count; /* 188 * 4 * 1024; */
@@ -777,13 +769,12 @@ static int vidioc_g_fmt_cap (struct file *file, void *priv,
        return 0;
 }
 
-static int vidioc_try_fmt_cap (struct file *file, void *priv,
+static int vidioc_try_fmt_vid_cap (struct file *file, void *priv,
                        struct v4l2_format *f)
 {
        struct cx8802_fh  *fh   = priv;
        struct cx8802_dev *dev  = fh->dev;
 
-       f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
        f->fmt.pix.pixelformat  = V4L2_PIX_FMT_MPEG;
        f->fmt.pix.bytesperline = 0;
        f->fmt.pix.sizeimage    = dev->ts_packet_size * dev->ts_packet_count; /* 188 * 4 * 1024; */;
@@ -793,14 +784,13 @@ static int vidioc_try_fmt_cap (struct file *file, void *priv,
        return 0;
 }
 
-static int vidioc_s_fmt_cap (struct file *file, void *priv,
+static int vidioc_s_fmt_vid_cap (struct file *file, void *priv,
                                        struct v4l2_format *f)
 {
        struct cx8802_fh  *fh   = priv;
        struct cx8802_dev *dev  = fh->dev;
        struct cx88_core  *core = dev->core;
 
-       f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
        f->fmt.pix.pixelformat  = V4L2_PIX_FMT_MPEG;
        f->fmt.pix.bytesperline = 0;
        f->fmt.pix.sizeimage    = dev->ts_packet_size * dev->ts_packet_count; /* 188 * 4 * 1024; */;
@@ -853,23 +843,6 @@ static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
        return videobuf_streamoff(&fh->mpegq);
 }
 
-static int vidioc_g_mpegcomp (struct file *file, void *fh,
-                             struct v4l2_mpeg_compression *f)
-{
-       printk(KERN_WARNING "VIDIOC_G_MPEGCOMP is obsolete. "
-                               "Replace with VIDIOC_G_EXT_CTRLS!");
-       memcpy(f,&default_mpeg_params,sizeof(*f));
-       return 0;
-}
-
-static int vidioc_s_mpegcomp (struct file *file, void *fh,
-                             struct v4l2_mpeg_compression *f)
-{
-       printk(KERN_WARNING "VIDIOC_S_MPEGCOMP is obsolete. "
-                               "Replace with VIDIOC_S_EXT_CTRLS!");
-       return 0;
-}
-
 static int vidioc_g_ext_ctrls (struct file *file, void *priv,
                               struct v4l2_ext_controls *f)
 {
@@ -877,7 +850,7 @@ static int vidioc_g_ext_ctrls (struct file *file, void *priv,
 
        if (f->ctrl_class != V4L2_CTRL_CLASS_MPEG)
                return -EINVAL;
-       return cx2341x_ext_ctrls(&dev->params, f, VIDIOC_G_EXT_CTRLS);
+       return cx2341x_ext_ctrls(&dev->params, 0, f, VIDIOC_G_EXT_CTRLS);
 }
 
 static int vidioc_s_ext_ctrls (struct file *file, void *priv,
@@ -889,8 +862,12 @@ static int vidioc_s_ext_ctrls (struct file *file, void *priv,
 
        if (f->ctrl_class != V4L2_CTRL_CLASS_MPEG)
                return -EINVAL;
+
+       if (dev->mpeg_active)
+               blackbird_stop_codec(dev);
+
        p = dev->params;
-       err = cx2341x_ext_ctrls(&p, f, VIDIOC_S_EXT_CTRLS);
+       err = cx2341x_ext_ctrls(&p, 0, f, VIDIOC_S_EXT_CTRLS);
        if (!err) {
                err = cx2341x_update(dev, blackbird_mbox_func, &dev->params, &p);
                dev->params = p;
@@ -908,7 +885,7 @@ static int vidioc_try_ext_ctrls (struct file *file, void *priv,
        if (f->ctrl_class != V4L2_CTRL_CLASS_MPEG)
                return -EINVAL;
        p = dev->params;
-       err = cx2341x_ext_ctrls(&p, f, VIDIOC_TRY_EXT_CTRLS);
+       err = cx2341x_ext_ctrls(&p, 0, f, VIDIOC_TRY_EXT_CTRLS);
 
        return err;
 }
@@ -920,10 +897,9 @@ static int vidioc_s_frequency (struct file *file, void *priv,
        struct cx8802_dev *dev  = fh->dev;
        struct cx88_core  *core = dev->core;
 
-       blackbird_api_cmd(fh->dev, CX2341X_ENC_STOP_CAPTURE, 3, 0,
-                               BLACKBIRD_END_NOW,
-                               BLACKBIRD_MPEG_CAPTURE,
-                               BLACKBIRD_RAW_BITS_NONE);
+       if (dev->mpeg_active)
+               blackbird_stop_codec(dev);
+
        cx88_set_freq (core,f);
        blackbird_initialize_codec(dev);
        cx88_set_scale(dev->core, dev->width, dev->height,
@@ -940,7 +916,7 @@ static int vidioc_log_status (struct file *file, void *priv)
        snprintf(name, sizeof(name), "%s/2", core->name);
        printk("%s/2: ============  START LOG STATUS  ============\n",
                core->name);
-       cx88_call_i2c_clients(core, VIDIOC_LOG_STATUS, NULL);
+       call_all(core, core, log_status);
        cx2341x_log_status(&dev->params, name);
        printk("%s/2: =============  END LOG STATUS  =============\n",
                core->name);
@@ -958,7 +934,7 @@ static int vidioc_queryctrl (struct file *file, void *priv,
        qctrl->id = v4l2_ctrl_next(ctrl_classes, qctrl->id);
        if (unlikely(qctrl->id == 0))
                return -EINVAL;
-       return cx8800_ctrl_query(qctrl);
+       return cx8800_ctrl_query(dev->core, qctrl);
 }
 
 static int vidioc_enum_input (struct file *file, void *priv,
@@ -990,12 +966,12 @@ static int vidioc_g_frequency (struct file *file, void *priv,
        struct cx8802_fh  *fh   = priv;
        struct cx88_core  *core = fh->dev->core;
 
-       if (unlikely(UNSET == core->tuner_type))
+       if (unlikely(UNSET == core->board.tuner_type))
                return -EINVAL;
 
        f->type = V4L2_TUNER_ANALOG_TV;
        f->frequency = core->freq;
-       cx88_call_i2c_clients(core,VIDIOC_G_FREQUENCY,f);
+       call_all(core, tuner, g_frequency, f);
 
        return 0;
 }
@@ -1028,7 +1004,9 @@ static int vidioc_g_tuner (struct file *file, void *priv,
        struct cx88_core  *core = ((struct cx8802_fh *)priv)->dev->core;
        u32 reg;
 
-       if (unlikely(UNSET == core->tuner_type))
+       if (unlikely(UNSET == core->board.tuner_type))
+               return -EINVAL;
+       if (0 != t->index)
                return -EINVAL;
 
        strcpy(t->name, "Television");
@@ -1047,7 +1025,7 @@ static int vidioc_s_tuner (struct file *file, void *priv,
 {
        struct cx88_core  *core = ((struct cx8802_fh *)priv)->dev->core;
 
-       if (UNSET == core->tuner_type)
+       if (UNSET == core->board.tuner_type)
                return -EINVAL;
        if (0 != t->index)
                return -EINVAL;
@@ -1068,34 +1046,33 @@ static int vidioc_s_std (struct file *file, void *priv, v4l2_std_id *id)
 
 /* FIXME: cx88_ioctl_hook not implemented */
 
-static int mpeg_open(struct inode *inode, struct file *file)
+static int mpeg_open(struct file *file)
 {
-       int minor = iminor(inode);
-       struct cx8802_dev *dev = NULL;
+       int minor = video_devdata(file)->minor;
+       struct cx8802_dev *dev = video_drvdata(file);
        struct cx8802_fh *fh;
        struct cx8802_driver *drv = NULL;
        int err;
 
-       dev = cx8802_get_device(inode);
+       dprintk( 1, "%s\n", __func__);
 
-       dprintk( 1, "%s\n", __FUNCTION__);
-
-       if (dev == NULL)
-               return -ENODEV;
+       lock_kernel();
 
        /* Make sure we can acquire the hardware */
        drv = cx8802_get_driver(dev, CX88_MPEG_BLACKBIRD);
        if (drv) {
                err = drv->request_acquire(drv);
                if(err != 0) {
-                       dprintk(1,"%s: Unable to acquire hardware, %d\n", __FUNCTION__, err);
+                       dprintk(1,"%s: Unable to acquire hardware, %d\n", __func__, err);
+                       unlock_kernel();
                        return err;
                }
        }
 
-       if (blackbird_initialize_codec(dev) < 0) {
+       if (!atomic_read(&dev->core->mpeg_users) && blackbird_initialize_codec(dev) < 0) {
                if (drv)
                        drv->request_release(drv);
+               unlock_kernel();
                return -EINVAL;
        }
        dprintk(1,"open minor=%d\n",minor);
@@ -1105,13 +1082,14 @@ static int mpeg_open(struct inode *inode, struct file *file)
        if (NULL == fh) {
                if (drv)
                        drv->request_release(drv);
+               unlock_kernel();
                return -ENOMEM;
        }
        file->private_data = fh;
        fh->dev      = dev;
 
-       videobuf_queue_init(&fh->mpegq, &blackbird_qops,
-                           dev->pci, &dev->slock,
+       videobuf_queue_sg_init(&fh->mpegq, &blackbird_qops,
+                           &dev->pci->dev, &dev->slock,
                            V4L2_BUF_TYPE_VIDEO_CAPTURE,
                            V4L2_FIELD_INTERLACED,
                            sizeof(struct cx88_buffer),
@@ -1120,43 +1098,37 @@ static int mpeg_open(struct inode *inode, struct file *file)
        /* FIXME: locking against other video device */
        cx88_set_scale(dev->core, dev->width, dev->height,
                        fh->mpegq.field);
+       unlock_kernel();
+
+       atomic_inc(&dev->core->mpeg_users);
 
        return 0;
 }
 
-static int mpeg_release(struct inode *inode, struct file *file)
+static int mpeg_release(struct file *file)
 {
        struct cx8802_fh  *fh  = file->private_data;
-       struct cx8802_dev *dev = NULL;
+       struct cx8802_dev *dev = fh->dev;
        struct cx8802_driver *drv = NULL;
 
-       /* blackbird_api_cmd(fh->dev, CX2341X_ENC_STOP_CAPTURE, 3, 0, BLACKBIRD_END_NOW, 0, 0x13); */
-       blackbird_api_cmd(fh->dev, CX2341X_ENC_STOP_CAPTURE, 3, 0,
-                       BLACKBIRD_END_NOW,
-                       BLACKBIRD_MPEG_CAPTURE,
-                       BLACKBIRD_RAW_BITS_NONE
-               );
+       if (dev->mpeg_active && atomic_read(&dev->core->mpeg_users) == 1)
+               blackbird_stop_codec(dev);
 
        cx8802_cancel_buffers(fh->dev);
        /* stop mpeg capture */
-       if (fh->mpegq.streaming)
-               videobuf_streamoff(&fh->mpegq);
-       if (fh->mpegq.reading)
-               videobuf_read_stop(&fh->mpegq);
+       videobuf_stop(&fh->mpegq);
 
        videobuf_mmap_free(&fh->mpegq);
        file->private_data = NULL;
        kfree(fh);
 
        /* Make sure we release the hardware */
-       dev = cx8802_get_device(inode);
-       if (dev == NULL)
-               return -ENODEV;
-
        drv = cx8802_get_driver(dev, CX88_MPEG_BLACKBIRD);
        if (drv)
                drv->request_release(drv);
 
+       atomic_dec(&dev->core->mpeg_users);
+
        return 0;
 }
 
@@ -1164,6 +1136,10 @@ static ssize_t
 mpeg_read(struct file *file, char __user *data, size_t count, loff_t *ppos)
 {
        struct cx8802_fh *fh = file->private_data;
+       struct cx8802_dev *dev = fh->dev;
+
+       if (!dev->mpeg_active)
+               blackbird_start_codec(file, fh);
 
        return videobuf_read_stream(&fh->mpegq, data, count, ppos, 0,
                                    file->f_flags & O_NONBLOCK);
@@ -1173,6 +1149,10 @@ static unsigned int
 mpeg_poll(struct file *file, struct poll_table_struct *wait)
 {
        struct cx8802_fh *fh = file->private_data;
+       struct cx8802_dev *dev = fh->dev;
+
+       if (!dev->mpeg_active)
+               blackbird_start_codec(file, fh);
 
        return videobuf_poll_stream(file, &fh->mpegq, wait);
 }
@@ -1185,7 +1165,7 @@ mpeg_mmap(struct file *file, struct vm_area_struct * vma)
        return videobuf_mmap_mapper(&fh->mpegq, vma);
 }
 
-static const struct file_operations mpeg_fops =
+static const struct v4l2_file_operations mpeg_fops =
 {
        .owner         = THIS_MODULE,
        .open          = mpeg_open,
@@ -1194,29 +1174,21 @@ static const struct file_operations mpeg_fops =
        .poll          = mpeg_poll,
        .mmap          = mpeg_mmap,
        .ioctl         = video_ioctl2,
-       .llseek        = no_llseek,
 };
 
-static struct video_device cx8802_mpeg_template =
-{
-       .name                 = "cx8802",
-       .type                 = VID_TYPE_CAPTURE|VID_TYPE_TUNER|VID_TYPE_SCALES|VID_TYPE_MPEG_ENCODER,
-       .fops                 = &mpeg_fops,
-       .minor                = -1,
+static const struct v4l2_ioctl_ops mpeg_ioctl_ops = {
        .vidioc_querymenu     = vidioc_querymenu,
        .vidioc_querycap      = vidioc_querycap,
-       .vidioc_enum_fmt_cap  = vidioc_enum_fmt_cap,
-       .vidioc_g_fmt_cap     = vidioc_g_fmt_cap,
-       .vidioc_try_fmt_cap   = vidioc_try_fmt_cap,
-       .vidioc_s_fmt_cap     = vidioc_s_fmt_cap,
+       .vidioc_enum_fmt_vid_cap  = vidioc_enum_fmt_vid_cap,
+       .vidioc_g_fmt_vid_cap     = vidioc_g_fmt_vid_cap,
+       .vidioc_try_fmt_vid_cap   = vidioc_try_fmt_vid_cap,
+       .vidioc_s_fmt_vid_cap     = vidioc_s_fmt_vid_cap,
        .vidioc_reqbufs       = vidioc_reqbufs,
        .vidioc_querybuf      = vidioc_querybuf,
        .vidioc_qbuf          = vidioc_qbuf,
        .vidioc_dqbuf         = vidioc_dqbuf,
        .vidioc_streamon      = vidioc_streamon,
        .vidioc_streamoff     = vidioc_streamoff,
-       .vidioc_g_mpegcomp    = vidioc_g_mpegcomp,
-       .vidioc_s_mpegcomp    = vidioc_s_mpegcomp,
        .vidioc_g_ext_ctrls   = vidioc_g_ext_ctrls,
        .vidioc_s_ext_ctrls   = vidioc_s_ext_ctrls,
        .vidioc_try_ext_ctrls = vidioc_try_ext_ctrls,
@@ -1232,8 +1204,15 @@ static struct video_device cx8802_mpeg_template =
        .vidioc_g_tuner       = vidioc_g_tuner,
        .vidioc_s_tuner       = vidioc_s_tuner,
        .vidioc_s_std         = vidioc_s_std,
+};
+
+static struct video_device cx8802_mpeg_template = {
+       .name                 = "cx8802",
+       .fops                 = &mpeg_fops,
+       .ioctl_ops            = &mpeg_ioctl_ops,
+       .minor                = -1,
        .tvnorms              = CX88_NORMS,
-       .current_norm         = V4L2_STD_NTSC_M,
+       .current_norm         = V4L2_STD_NTSC_M,
 };
 
 /* ------------------------------------------------------------------ */
@@ -1244,15 +1223,23 @@ static int cx8802_blackbird_advise_acquire(struct cx8802_driver *drv)
        struct cx88_core *core = drv->core;
        int err = 0;
 
-       switch (core->board) {
+       switch (core->boardnr) {
        case CX88_BOARD_HAUPPAUGE_HVR1300:
                /* By default, core setup will leave the cx22702 out of reset, on the bus.
                 * We left the hardware on power up with the cx22702 active.
                 * We're being given access to re-arrange the GPIOs.
                 * Take the bus off the cx22702 and put the cx23416 on it.
                 */
-               cx_clear(MO_GP0_IO, 0x00000080); /* cx22702 in reset */
-               cx_set(MO_GP0_IO,   0x00000004); /* Disable the cx22702 */
+               /* Toggle reset on cx22702 leaving i2c active */
+               cx_set(MO_GP0_IO, 0x00000080);
+               udelay(1000);
+               cx_clear(MO_GP0_IO, 0x00000080);
+               udelay(50);
+               cx_set(MO_GP0_IO, 0x00000080);
+               udelay(1000);
+               /* tri-state the cx22702 pins */
+               cx_set(MO_GP0_IO, 0x00000004);
+               udelay(1000);
                break;
        default:
                err = -ENODEV;
@@ -1266,7 +1253,7 @@ static int cx8802_blackbird_advise_release(struct cx8802_driver *drv)
        struct cx88_core *core = drv->core;
        int err = 0;
 
-       switch (core->board) {
+       switch (core->boardnr) {
        case CX88_BOARD_HAUPPAUGE_HVR1300:
                /* Exit leaving the cx23416 on the bus */
                break;
@@ -1279,7 +1266,7 @@ static int cx8802_blackbird_advise_release(struct cx8802_driver *drv)
 static void blackbird_unregister_video(struct cx8802_dev *dev)
 {
        if (dev->mpeg_dev) {
-               if (-1 != dev->mpeg_dev->minor)
+               if (video_is_registered(dev->mpeg_dev))
                        video_unregister_device(dev->mpeg_dev);
                else
                        video_device_release(dev->mpeg_dev);
@@ -1293,14 +1280,15 @@ static int blackbird_register_video(struct cx8802_dev *dev)
 
        dev->mpeg_dev = cx88_vdev_init(dev->core,dev->pci,
                                       &cx8802_mpeg_template,"mpeg");
+       video_set_drvdata(dev->mpeg_dev, dev);
        err = video_register_device(dev->mpeg_dev,VFL_TYPE_GRABBER, -1);
        if (err < 0) {
                printk(KERN_INFO "%s/2: can't register mpeg device\n",
                       dev->core->name);
                return err;
        }
-       printk(KERN_INFO "%s/2: registered device video%d [mpeg]\n",
-              dev->core->name,dev->mpeg_dev->minor & 0x1f);
+       printk(KERN_INFO "%s/2: registered device %s [mpeg]\n",
+              dev->core->name, video_device_node_name(dev->mpeg_dev));
        return 0;
 }
 
@@ -1312,15 +1300,15 @@ static int cx8802_blackbird_probe(struct cx8802_driver *drv)
        struct cx8802_dev *dev = core->dvbdev;
        int err;
 
-       dprintk( 1, "%s\n", __FUNCTION__);
+       dprintk( 1, "%s\n", __func__);
        dprintk( 1, " ->being probed by Card=%d Name=%s, PCI %02x:%02x\n",
-               core->board,
+               core->boardnr,
                core->name,
                core->pci_bus,
                core->pci_slot);
 
        err = -ENODEV;
-       if (!(cx88_boards[core->board].mpeg & CX88_MPEG_BLACKBIRD))
+       if (!(core->board.mpeg & CX88_MPEG_BLACKBIRD))
                goto fail_core;
 
        dev->width = 720;
@@ -1341,6 +1329,7 @@ static int cx8802_blackbird_probe(struct cx8802_driver *drv)
               core->name);
        host_setup(dev->core);
 
+       blackbird_initialize_codec(dev);
        blackbird_register_video(dev);
 
        /* initial device configuration: needed ? */
@@ -1373,7 +1362,7 @@ static struct cx8802_driver cx8802_blackbird_driver = {
        .advise_release = cx8802_blackbird_advise_release,
 };
 
-static int blackbird_init(void)
+static int __init blackbird_init(void)
 {
        printk(KERN_INFO "cx2388x blackbird driver version %d.%d.%d loaded\n",
               (CX88_VERSION_CODE >> 16) & 0xff,
@@ -1386,7 +1375,7 @@ static int blackbird_init(void)
        return cx8802_register_driver(&cx8802_blackbird_driver);
 }
 
-static void blackbird_fini(void)
+static void __exit blackbird_fini(void)
 {
        cx8802_unregister_driver(&cx8802_blackbird_driver);
 }