V4L/DVB (13554a): v4l: Use the video_drvdata function in drivers
[safe/jmp/linux-2.6] / drivers / media / video / cx88 / cx88-blackbird.c
index 99bfa32..f0ef941 100644 (file)
@@ -3,9 +3,12 @@
  *  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>
+ *        - video_ioctl2 conversion
+ *
  *  Includes parts from the ivtv driver( http://ivtv.sourceforge.net/),
  *
  *  This program is free software; you can redistribute it and/or modify
  */
 
 #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>");
-MODULE_AUTHOR("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]");
 
 #define dprintk(level,fmt, arg...)     if (debug >= level) \
        printk(KERN_DEBUG "%s/2-bb: " fmt, dev->core->name , ## arg)
 
-static LIST_HEAD(cx8802_devlist);
 
 /* ------------------------------------------------------------------ */
 
-#define BLACKBIRD_FIRM_ENC_FILENAME "blackbird-fw-enc.bin"
-#define BLACKBIRD_FIRM_IMAGE_SIZE 256*1024
+#define BLACKBIRD_FIRM_IMAGE_SIZE 376836
 
 /* defines below are from ivtv-driver.h */
 
@@ -63,8 +66,6 @@ static LIST_HEAD(cx8802_devlist);
 /* Firmware API commands */
 #define IVTV_API_STD_TIMEOUT 500
 
-#define BLACKBIRD_API_PING               0x80
-#define BLACKBIRD_API_BEGIN_CAPTURE      0x81
 enum blackbird_capture_type {
        BLACKBIRD_MPEG_CAPTURE,
        BLACKBIRD_RAW_CAPTURE,
@@ -78,205 +79,29 @@ enum blackbird_capture_bits {
        BLACKBIRD_RAW_BITS_PASSTHRU_CAPTURE = 0x08,
        BLACKBIRD_RAW_BITS_TO_HOST_CAPTURE  = 0x10
 };
-#define BLACKBIRD_API_END_CAPTURE        0x82
 enum blackbird_capture_end {
        BLACKBIRD_END_AT_GOP, /* stop at the end of gop, generate irq */
        BLACKBIRD_END_NOW, /* stop immediately, no irq */
 };
-#define BLACKBIRD_API_SET_AUDIO_ID       0x89
-#define BLACKBIRD_API_SET_VIDEO_ID       0x8B
-#define BLACKBIRD_API_SET_PCR_ID         0x8D
-#define BLACKBIRD_API_SET_FRAMERATE      0x8F
 enum blackbird_framerate {
        BLACKBIRD_FRAMERATE_NTSC_30, /* NTSC: 30fps */
        BLACKBIRD_FRAMERATE_PAL_25   /* PAL: 25fps */
 };
-#define BLACKBIRD_API_SET_RESOLUTION     0x91
-#define BLACKBIRD_API_SET_VIDEO_BITRATE  0x95
-enum blackbird_video_bitrate_type {
-       BLACKBIRD_VIDEO_VBR,
-       BLACKBIRD_VIDEO_CBR
-};
-#define BLACKBIRD_PEAK_RATE_DIVISOR 400
-enum blackbird_mux_rate {
-       BLACKBIRD_MUX_RATE_DEFAULT,
-        /* dvd mux rate: multiply by 400 to get the actual rate */
-       BLACKBIRD_MUX_RATE_DVD = 25200
-};
-#define BLACKBIRD_API_SET_GOP_STRUCTURE  0x97
-#define BLACKBIRD_API_SET_ASPECT_RATIO   0x99
-enum blackbird_aspect_ratio {
-       BLACKBIRD_ASPECT_RATIO_FORBIDDEN,
-       BLACKBIRD_ASPECT_RATIO_1_1_SQUARE,
-       BLACKBIRD_ASPECT_RATIO_4_3,
-       BLACKBIRD_ASPECT_RATIO_16_9,
-       BLACKBIRD_ASPECT_RATIO_221_100,
-       BLACKBIRD_ASPECT_RATIO_RESERVED
-};
-#define BLACKBIRD_API_SET_DNR_MODE       0x9B
-enum blackbird_dnr_bits {
-       BLACKBIRD_DNR_BITS_MANUAL,
-       BLACKBIRD_DNR_BITS_AUTO_SPATIAL,
-       BLACKBIRD_DNR_BITS_AUTO_TEMPORAL,
-       BLACKBIRD_DNR_BITS_AUTO
-};
-enum blackbird_median_filter {
-       BLACKBIRD_MEDIAN_FILTER_DISABLED,
-       BLACKBIRD_MEDIAN_FILTER_HORIZONTAL,
-       BLACKBIRD_MEDIAN_FILTER_VERTICAL,
-       BLACKBIRD_MEDIAN_FILTER_HV,
-       BLACKBIRD_MEDIAN_FILTER_DIAGONAL
-};
-#define BLACKBIRD_API_SET_MANUAL_DNR     0x9D
-#define BLACKBIRD_API_SET_DNR_MEDIAN     0x9F
-#define BLACKBIRD_API_SET_SPATIAL_FILTER 0xA1
-enum blackbird_spatial_filter_luma {
-       BLACKBIRD_SPATIAL_FILTER_LUMA_DISABLED,
-       BLACKBIRD_SPATIAL_FILTER_LUMA_1D_HORIZ,
-       BLACKBIRD_SPATIAL_FILTER_LUMA_1D_VERT,
-       BLACKBIRD_SPATIAL_FILTER_LUMA_2D_HV, /* separable, default */
-       BLACKBIRD_SPATIAL_FILTER_LUMA_2D_SYMM /* symmetric non-separable */
-};
-enum blackbird_spatial_filter_chroma {
-       BLACKBIRD_SPATIAL_FILTER_CHROMA_DISABLED,
-       BLACKBIRD_SPATIAL_FILTER_CHROMA_1D_HORIZ /* default */
-};
-#define BLACKBIRD_API_SET_3_2_PULLDOWN   0xB1
-enum blackbird_pulldown {
-       BLACKBIRD_3_2_PULLDOWN_DISABLED,
-       BLACKBIRD_3_2_PULLDOWN_ENABLED
-};
-#define BLACKBIRD_API_SET_VBI_LINE_NO    0xB7
-enum blackbird_vbi_line_bits {
-       BLACKBIRD_VBI_LINE_BITS_TOP_FIELD,
-       BLACKBIRD_VBI_LINE_BITS_BOT_FIELD = (1 << 31),
-       BLACKBIRD_VBI_LINE_BITS_ALL_LINES = 0xFFFFFFFF
-};
-enum blackbird_vbi_line {
-       BLACKBIRD_VBI_LINE_DISABLED,
-       BLACKBIRD_VBI_LINE_ENABLED
-};
-enum blackbird_vbi_slicing {
-       BLACKBIRD_VBI_SLICING_NONE,
-       BLACKBIRD_VBI_SLICING_CLOSED_CAPTION
-};
-#define BLACKBIRD_API_SET_STREAM_TYPE    0xB9
-enum blackbird_stream_type {
-       BLACKBIRD_STREAM_PROGRAM,
-       BLACKBIRD_STREAM_TRANSPORT,
-       BLACKBIRD_STREAM_MPEG1,
-       BLACKBIRD_STREAM_PES_AV,
-       BLACKBIRD_STREAM_UNKNOWN4,
-       BLACKBIRD_STREAM_PES_VIDEO,
-       BLACKBIRD_STREAM_UNKNOWN6,
-       BLACKBIRD_STREAM_PES_AUDIO,
-       BLACKBIRD_STREAM_UNKNOWN8,
-       BLACKBIRD_STREAM_UNKNOWN9, /* audio/pcm ? */
-       BLACKBIRD_STREAM_DVD,
-       BLACKBIRD_STREAM_VCD,
-       BLACKBIRD_STREAM_UNKNOWN12 /* svcd/xvcd ? */
-};
-#define BLACKBIRD_API_SET_OUTPUT_PORT    0xBB
 enum blackbird_stream_port {
        BLACKBIRD_OUTPUT_PORT_MEMORY,
        BLACKBIRD_OUTPUT_PORT_STREAMING,
        BLACKBIRD_OUTPUT_PORT_SERIAL
 };
-#define BLACKBIRD_API_SET_AUDIO_PARAMS   0xBD
-enum blackbird_audio_bits_sample_rate {
-       BLACKBIRD_AUDIO_BITS_44100HZ,
-       BLACKBIRD_AUDIO_BITS_48000HZ,
-       BLACKBIRD_AUDIO_BITS_32000HZ,
-       BLACKBIRD_AUDIO_BITS_RESERVED_HZ,
-};
-enum blackbird_audio_bits_encoding {
-       BLACKBIRD_AUDIO_BITS_LAYER_1 = 0x1 << 2,
-       BLACKBIRD_AUDIO_BITS_LAYER_2 = 0x2 << 2,
-};
-enum blackbird_audio_bits_bitrate_layer_1 {
-       BLACKBIRD_AUDIO_BITS_LAYER_1_FREE_FORMAT,
-       BLACKBIRD_AUDIO_BITS_LAYER_1_32  = 0x01 << 4,
-       BLACKBIRD_AUDIO_BITS_LAYER_1_64  = 0x02 << 4,
-       BLACKBIRD_AUDIO_BITS_LAYER_1_96  = 0x03 << 4,
-       BLACKBIRD_AUDIO_BITS_LAYER_1_128 = 0x04 << 4,
-       BLACKBIRD_AUDIO_BITS_LAYER_1_160 = 0x05 << 4,
-       BLACKBIRD_AUDIO_BITS_LAYER_1_192 = 0x06 << 4,
-       BLACKBIRD_AUDIO_BITS_LAYER_1_224 = 0x07 << 4,
-       BLACKBIRD_AUDIO_BITS_LAYER_1_256 = 0x08 << 4,
-       BLACKBIRD_AUDIO_BITS_LAYER_1_288 = 0x09 << 4,
-       BLACKBIRD_AUDIO_BITS_LAYER_1_320 = 0x0A << 4,
-       BLACKBIRD_AUDIO_BITS_LAYER_1_352 = 0x0B << 4,
-       BLACKBIRD_AUDIO_BITS_LAYER_1_384 = 0x0C << 4,
-       BLACKBIRD_AUDIO_BITS_LAYER_1_416 = 0x0D << 4,
-       BLACKBIRD_AUDIO_BITS_LAYER_1_448 = 0x0E << 4,
-};
-enum blackbird_audio_bits_bitrate_layer_2 {
-       BLACKBIRD_AUDIO_BITS_LAYER_2_FREE_FORMAT,
-       BLACKBIRD_AUDIO_BITS_LAYER_2_32  = 0x01 << 4,
-       BLACKBIRD_AUDIO_BITS_LAYER_2_48  = 0x02 << 4,
-       BLACKBIRD_AUDIO_BITS_LAYER_2_56  = 0x03 << 4,
-       BLACKBIRD_AUDIO_BITS_LAYER_2_64  = 0x04 << 4,
-       BLACKBIRD_AUDIO_BITS_LAYER_2_80  = 0x05 << 4,
-       BLACKBIRD_AUDIO_BITS_LAYER_2_96  = 0x06 << 4,
-       BLACKBIRD_AUDIO_BITS_LAYER_2_112 = 0x07 << 4,
-       BLACKBIRD_AUDIO_BITS_LAYER_2_128 = 0x08 << 4,
-       BLACKBIRD_AUDIO_BITS_LAYER_2_160 = 0x09 << 4,
-       BLACKBIRD_AUDIO_BITS_LAYER_2_192 = 0x0A << 4,
-       BLACKBIRD_AUDIO_BITS_LAYER_2_224 = 0x0B << 4,
-       BLACKBIRD_AUDIO_BITS_LAYER_2_256 = 0x0C << 4,
-       BLACKBIRD_AUDIO_BITS_LAYER_2_320 = 0x0D << 4,
-       BLACKBIRD_AUDIO_BITS_LAYER_2_384 = 0x0E << 4,
-};
-enum blackbird_audio_bits_mode {
-       BLACKBIRD_AUDIO_BITS_STEREO,
-       BLACKBIRD_AUDIO_BITS_JOINT_STEREO = 0x1 << 8,
-       BLACKBIRD_AUDIO_BITS_DUAL         = 0x2 << 8,
-       BLACKBIRD_AUDIO_BITS_MONO         = 0x3 << 8,
-};
-enum blackbird_audio_bits_mode_extension {
-       BLACKBIRD_AUDIO_BITS_BOUND_4,
-       BLACKBIRD_AUDIO_BITS_BOUND_8  = 0x1 << 10,
-       BLACKBIRD_AUDIO_BITS_BOUND_12 = 0x2 << 10,
-       BLACKBIRD_AUDIO_BITS_BOUND_16 = 0x3 << 10,
-};
-enum blackbird_audio_bits_emphasis {
-       BLACKBIRD_AUDIO_BITS_EMPHASIS_NONE,
-       BLACKBIRD_AUDIO_BITS_EMPHASIS_50_15     = 0x1 << 12,
-       BLACKBIRD_AUDIO_BITS_EMPHASIS_RESERVED  = 0x2 << 12,
-       BLACKBIRD_AUDIO_BITS_EMPHASIS_CCITT_J17 = 0x3 << 12,
-};
-enum blackbird_audio_bits_crc {
-       BLACKBIRD_AUDIO_BITS_CRC_OFF,
-       BLACKBIRD_AUDIO_BITS_CRC_ON = 0x1 << 14,
-};
-enum blackbird_audio_bits_copyright {
-       BLACKBIRD_AUDIO_BITS_COPYRIGHT_OFF,
-       BLACKBIRD_AUDIO_BITS_COPYRIGHT_ON = 0x1 << 15,
-};
-enum blackbird_audio_bits_original {
-       BLACKBIRD_AUDIO_BITS_COPY,
-       BLACKBIRD_AUDIO_BITS_ORIGINAL = 0x1 << 16,
-};
-#define BLACKBIRD_API_HALT               0xC3
-#define BLACKBIRD_API_GET_VERSION        0xC4
-#define BLACKBIRD_API_SET_GOP_CLOSURE    0xC5
-enum blackbird_gop_closure {
-       BLACKBIRD_GOP_CLOSURE_OFF,
-       BLACKBIRD_GOP_CLOSURE_ON,
-};
-#define BLACKBIRD_API_DATA_XFER_STATUS   0xC6
 enum blackbird_data_xfer_status {
        BLACKBIRD_MORE_BUFFERS_FOLLOW,
        BLACKBIRD_LAST_BUFFER,
 };
-#define BLACKBIRD_API_PROGRAM_INDEX_INFO 0xC7
 enum blackbird_picture_mask {
        BLACKBIRD_PICTURE_MASK_NONE,
        BLACKBIRD_PICTURE_MASK_I_FRAMES,
        BLACKBIRD_PICTURE_MASK_I_P_FRAMES = 0x3,
        BLACKBIRD_PICTURE_MASK_ALL_FRAMES = 0x7,
 };
-#define BLACKBIRD_API_SET_VBI_PARAMS     0xC8
 enum blackbird_vbi_mode_bits {
        BLACKBIRD_VBI_BITS_SLICED,
        BLACKBIRD_VBI_BITS_RAW,
@@ -288,33 +113,23 @@ enum blackbird_vbi_insertion_bits {
        BLACKBIRD_VBI_BITS_SEPARATE_STREAM_USR_DATA = 0x4 << 1,
        BLACKBIRD_VBI_BITS_SEPARATE_STREAM_PRV_DATA = 0x5 << 1,
 };
-#define BLACKBIRD_API_SET_DMA_BLOCK_SIZE 0xC9
 enum blackbird_dma_unit {
        BLACKBIRD_DMA_BYTES,
        BLACKBIRD_DMA_FRAMES,
 };
-#define BLACKBIRD_API_DMA_TRANSFER_INFO  0xCA
-#define BLACKBIRD_API_DMA_TRANSFER_STAT  0xCB
 enum blackbird_dma_transfer_status_bits {
        BLACKBIRD_DMA_TRANSFER_BITS_DONE = 0x01,
        BLACKBIRD_DMA_TRANSFER_BITS_ERROR = 0x04,
        BLACKBIRD_DMA_TRANSFER_BITS_LL_ERROR = 0x10,
 };
-#define BLACKBIRD_API_SET_DMA2HOST_ADDR  0xCC
-#define BLACKBIRD_API_INIT_VIDEO_INPUT   0xCD
-#define BLACKBIRD_API_SET_FRAMESKIP      0xD0
-#define BLACKBIRD_API_PAUSE              0xD2
 enum blackbird_pause {
        BLACKBIRD_PAUSE_ENCODING,
        BLACKBIRD_RESUME_ENCODING,
 };
-#define BLACKBIRD_API_REFRESH_INPUT      0xD3
-#define BLACKBIRD_API_SET_COPYRIGHT      0xD4
 enum blackbird_copyright {
        BLACKBIRD_COPYRIGHT_OFF,
        BLACKBIRD_COPYRIGHT_ON,
 };
-#define BLACKBIRD_API_SET_NOTIFICATION   0xD5
 enum blackbird_notification_type {
        BLACKBIRD_NOTIFICATION_REFRESH,
 };
@@ -325,7 +140,6 @@ enum blackbird_notification_status {
 enum blackbird_notification_mailbox {
        BLACKBIRD_NOTIFICATION_NO_MAILBOX = -1,
 };
-#define BLACKBIRD_API_SET_CAPTURE_LINES  0xD6
 enum blackbird_field1_lines {
        BLACKBIRD_FIELD1_SAA7114 = 0x00EF, /* 239 */
        BLACKBIRD_FIELD1_SAA7115 = 0x00F0, /* 240 */
@@ -336,12 +150,10 @@ enum blackbird_field2_lines {
        BLACKBIRD_FIELD2_SAA7115 = 0x00F0, /* 240 */
        BLACKBIRD_FIELD2_MICRONAS = 0x0106, /* 262 */
 };
-#define BLACKBIRD_API_SET_CUSTOM_DATA    0xD7
 enum blackbird_custom_data_type {
        BLACKBIRD_CUSTOM_EXTENSION_USR_DATA,
        BLACKBIRD_CUSTOM_PRIVATE_PACKET,
 };
-#define BLACKBIRD_API_MUTE_VIDEO         0xD9
 enum blackbird_mute {
        BLACKBIRD_UNMUTE,
        BLACKBIRD_MUTE,
@@ -356,7 +168,6 @@ enum blackbird_mute_video_shift {
        BLACKBIRD_MUTE_VIDEO_U_SHIFT = 16,
        BLACKBIRD_MUTE_VIDEO_Y_SHIFT = 24,
 };
-#define BLACKBIRD_API_MUTE_AUDIO         0xDA
 
 /* Registers */
 #define IVTV_REG_ENC_SDRAM_REFRESH (0x07F8 /*| IVTV_REG_OFFSET*/)
@@ -498,17 +309,14 @@ static int register_read(struct cx88_core *core, u32 address, u32 *value)
 
 /* ------------------------------------------------------------------ */
 
-/* We don't need to call the API often, so using just one mailbox will probably suffice */
-static int blackbird_api_cmd(struct cx8802_dev *dev, u32 command,
-                            u32 inputcnt, u32 outputcnt, ...)
+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;
-       va_list args;
-       va_start(args, outputcnt);
 
-       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 */
@@ -530,12 +338,11 @@ static int blackbird_api_cmd(struct cx8802_dev *dev, u32 command,
        /* write command + args + fill remaining with zeros */
        memory_write(dev->core, dev->mailbox + 1, command); /* command code */
        memory_write(dev->core, dev->mailbox + 3, IVTV_API_STD_TIMEOUT); /* timeout */
-       for (i = 0; i < inputcnt ; i++) {
-               value = va_arg(args, int);
-               memory_write(dev->core, dev->mailbox + 4 + i, value);
-               dprintk(1, "API Input %d = %d\n", i, value);
+       for (i = 0; i < in; i++) {
+               memory_write(dev->core, dev->mailbox + 4 + i, data[i]);
+               dprintk(1, "API Input %d = %d\n", i, data[i]);
        }
-       for (; i < 16 ; i++)
+       for (; i < CX2341X_MBOX_MAX_DATA; i++)
                memory_write(dev->core, dev->mailbox + 4 + i, 0);
 
        flag |= 3; /* tell 'em we're done writing */
@@ -555,12 +362,10 @@ static int blackbird_api_cmd(struct cx8802_dev *dev, u32 command,
        }
 
        /* read output values */
-       for (i = 0; i < outputcnt ; i++) {
-               int *vptr = va_arg(args, int *);
-               memory_read(dev->core, dev->mailbox + 4 + i, vptr);
-               dprintk(1, "API Output %d = %d\n", i, *vptr);
+       for (i = 0; i < out; i++) {
+               memory_read(dev->core, dev->mailbox + 4 + i, data + i);
+               dprintk(1, "API Output %d = %d\n", i, data[i]);
        }
-       va_end(args);
 
        memory_read(dev->core, dev->mailbox + 2, &retval);
        dprintk(1, "API result = %d\n",retval);
@@ -569,7 +374,29 @@ static int blackbird_api_cmd(struct cx8802_dev *dev, u32 command,
        memory_write(dev->core, dev->mailbox, flag);
        return retval;
 }
+/* ------------------------------------------------------------------ */
+
+/* We don't need to call the API often, so using just one mailbox will probably suffice */
+static int blackbird_api_cmd(struct cx8802_dev *dev, u32 command,
+                            u32 inputcnt, u32 outputcnt, ...)
+{
+       u32 data[CX2341X_MBOX_MAX_DATA];
+       va_list vargs;
+       int i, err;
 
+       va_start(vargs, outputcnt);
+
+       for (i = 0; i < inputcnt; i++) {
+               data[i] = va_arg(vargs, int);
+       }
+       err = blackbird_mbox_func(dev, command, inputcnt, outputcnt, data);
+       for (i = 0; i < outputcnt; i++) {
+               int *vptr = va_arg(vargs, int *);
+               *vptr = data[i];
+       }
+       va_end(vargs);
+       return err;
+}
 
 static int blackbird_find_mailbox(struct cx8802_dev *dev)
 {
@@ -614,13 +441,13 @@ static int blackbird_load_firmware(struct cx8802_dev *dev)
        if (retval < 0)
                dprintk(0, "Error with register_write\n");
 
-       retval = request_firmware(&firmware, BLACKBIRD_FIRM_ENC_FILENAME,
+       retval = request_firmware(&firmware, CX2341X_FIRM_ENC_FILENAME,
                                  &dev->pci->dev);
 
 
        if (retval != 0) {
                dprintk(0, "ERROR: Hotplug firmware request failed (%s).\n",
-                       BLACKBIRD_FIRM_ENC_FILENAME);
+                       CX2341X_FIRM_ENC_FILENAME);
                dprintk(0, "Please fix your hotplug setup, the board will "
                        "not work without firmware loaded!\n");
                return -1;
@@ -629,11 +456,13 @@ static int blackbird_load_firmware(struct cx8802_dev *dev)
        if (firmware->size != BLACKBIRD_FIRM_IMAGE_SIZE) {
                dprintk(0, "ERROR: Firmware size mismatch (have %zd, expected %d)\n",
                        firmware->size, BLACKBIRD_FIRM_IMAGE_SIZE);
+               release_firmware(firmware);
                return -1;
        }
 
        if (0 != memcmp(firmware->data, magic, 8)) {
                dprintk(0, "ERROR: Firmware magic mismatch, wrong file?\n");
+               release_firmware(firmware);
                return -1;
        }
 
@@ -654,6 +483,7 @@ static int blackbird_load_firmware(struct cx8802_dev *dev)
        }
        if (checksum) {
                dprintk(0, "ERROR: Firmware load failed (checksum mismatch).\n");
+               release_firmware(firmware);
                return -1;
        }
        release_firmware(firmware);
@@ -686,559 +516,17 @@ DB* DVD | MPEG2 | 720x576PAL | CBR     | 600 :Good    | 6000 Kbps  | 25fps   | M
 *DB: "DirectBurn"
 */
 
-static struct blackbird_dnr default_dnr_params = {
-       .mode     = BLACKBIRD_DNR_BITS_MANUAL,
-       .type     = BLACKBIRD_MEDIAN_FILTER_DISABLED,
-       .spatial  = 0,
-       .temporal = 0
-};
-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    = 44100,
-       .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 = 15,
-       .vi_bframes_count  = 2,
-       .vi_pesid          = 0,
-       .closed_gops       = 0,
-       .pulldown          = 0
-};
-
-static enum blackbird_stream_type mpeg_stream_types[] = {
-       [V4L2_MPEG_SS_1]   = BLACKBIRD_STREAM_MPEG1,
-       [V4L2_MPEG_PS_2]   = BLACKBIRD_STREAM_PROGRAM,
-       [V4L2_MPEG_TS_2]   = BLACKBIRD_STREAM_TRANSPORT,
-       [V4L2_MPEG_PS_DVD] = BLACKBIRD_STREAM_DVD,
-};
-static enum blackbird_aspect_ratio mpeg_stream_ratios[] = {
-       [V4L2_MPEG_ASPECT_SQUARE] = BLACKBIRD_ASPECT_RATIO_1_1_SQUARE,
-       [V4L2_MPEG_ASPECT_4_3]    = BLACKBIRD_ASPECT_RATIO_4_3,
-       [V4L2_MPEG_ASPECT_16_9]   = BLACKBIRD_ASPECT_RATIO_16_9,
-       [V4L2_MPEG_ASPECT_1_221]  = BLACKBIRD_ASPECT_RATIO_221_100,
-};
-static enum blackbird_video_bitrate_type mpeg_video_bitrates[] = {
-       [V4L2_BITRATE_NONE] = BLACKBIRD_VIDEO_CBR,
-       [V4L2_BITRATE_CBR]  = BLACKBIRD_VIDEO_CBR,
-       [V4L2_BITRATE_VBR]  = BLACKBIRD_VIDEO_VBR,
-};
-/* find the best layer I/II bitrate to fit a given numeric value */
-struct bitrate_bits {
-       u32 bits; /* layer bits for the best fit */
-       u32 rate; /* actual numeric value for the layer best fit */
-};
-struct bitrate_approximation {
-       u32                 target;   /* numeric value of the rate we want */
-       struct bitrate_bits layer[2];
-};
-static struct bitrate_approximation mpeg_audio_bitrates[] = {
-       /* target  layer[0].bits           layer[0].rate       layer[1].bits           layer[1].rate */
-       {   0, { {                                0,   0, }, {                                0,   0, }, }, },
-       {  32, { { BLACKBIRD_AUDIO_BITS_LAYER_1_32 ,  32, }, { BLACKBIRD_AUDIO_BITS_LAYER_2_32 ,  32, }, }, },
-       {  48, { { BLACKBIRD_AUDIO_BITS_LAYER_1_64 ,  64, }, { BLACKBIRD_AUDIO_BITS_LAYER_2_48 ,  48, }, }, },
-       {  56, { { BLACKBIRD_AUDIO_BITS_LAYER_1_64 ,  64, }, { BLACKBIRD_AUDIO_BITS_LAYER_2_56 ,  56, }, }, },
-       {  64, { { BLACKBIRD_AUDIO_BITS_LAYER_1_64 ,  64, }, { BLACKBIRD_AUDIO_BITS_LAYER_2_64 ,  64, }, }, },
-       {  80, { { BLACKBIRD_AUDIO_BITS_LAYER_1_96 ,  96, }, { BLACKBIRD_AUDIO_BITS_LAYER_2_80 ,  80, }, }, },
-       {  96, { { BLACKBIRD_AUDIO_BITS_LAYER_1_96 ,  96, }, { BLACKBIRD_AUDIO_BITS_LAYER_2_96 ,  96, }, }, },
-       { 112, { { BLACKBIRD_AUDIO_BITS_LAYER_1_128, 128, }, { BLACKBIRD_AUDIO_BITS_LAYER_2_112, 112, }, }, },
-       { 128, { { BLACKBIRD_AUDIO_BITS_LAYER_1_128, 128, }, { BLACKBIRD_AUDIO_BITS_LAYER_2_128, 128, }, }, },
-       { 160, { { BLACKBIRD_AUDIO_BITS_LAYER_1_160, 160, }, { BLACKBIRD_AUDIO_BITS_LAYER_2_160, 160, }, }, },
-       { 192, { { BLACKBIRD_AUDIO_BITS_LAYER_1_192, 192, }, { BLACKBIRD_AUDIO_BITS_LAYER_2_192, 192, }, }, },
-       { 224, { { BLACKBIRD_AUDIO_BITS_LAYER_1_224, 224, }, { BLACKBIRD_AUDIO_BITS_LAYER_2_224, 224, }, }, },
-       { 256, { { BLACKBIRD_AUDIO_BITS_LAYER_1_256, 256, }, { BLACKBIRD_AUDIO_BITS_LAYER_2_256, 256, }, }, },
-       { 288, { { BLACKBIRD_AUDIO_BITS_LAYER_1_288, 288, }, { BLACKBIRD_AUDIO_BITS_LAYER_2_320, 320, }, }, },
-       { 320, { { BLACKBIRD_AUDIO_BITS_LAYER_1_320, 320, }, { BLACKBIRD_AUDIO_BITS_LAYER_2_320, 320, }, }, },
-       { 352, { { BLACKBIRD_AUDIO_BITS_LAYER_1_352, 352, }, { BLACKBIRD_AUDIO_BITS_LAYER_2_384, 384, }, }, },
-       { 384, { { BLACKBIRD_AUDIO_BITS_LAYER_1_384, 384, }, { BLACKBIRD_AUDIO_BITS_LAYER_2_384, 384, }, }, },
-       { 416, { { BLACKBIRD_AUDIO_BITS_LAYER_1_416, 416, }, { BLACKBIRD_AUDIO_BITS_LAYER_2_384, 384, }, }, },
-       { 448, { { BLACKBIRD_AUDIO_BITS_LAYER_1_448, 448, }, { BLACKBIRD_AUDIO_BITS_LAYER_2_384, 384, }, }, },
-};
-static const int BITRATES_SIZE = ARRAY_SIZE(mpeg_audio_bitrates);
-
-static void blackbird_set_default_params(struct cx8802_dev *dev)
-{
-       struct v4l2_mpeg_compression *params = &dev->params;
-       u32 au_params;
-
-       /* assign stream type */
-       if( params->st_type >= ARRAY_SIZE(mpeg_stream_types) )
-               params->st_type = V4L2_MPEG_PS_2;
-       if( params->st_type == V4L2_MPEG_SS_1 )
-               params->vi_type = V4L2_MPEG_VI_1;
-       else
-               params->vi_type = V4L2_MPEG_VI_2;
-       blackbird_api_cmd(dev, BLACKBIRD_API_SET_STREAM_TYPE, 1, 0, mpeg_stream_types[params->st_type]);
-
-       /* assign framerate */
-       if( params->vi_frame_rate <= 25 )
-       {
-               params->vi_frame_rate = 25;
-               blackbird_api_cmd(dev, BLACKBIRD_API_SET_FRAMERATE, 1, 0, BLACKBIRD_FRAMERATE_PAL_25);
-       }
-       else
-       {
-               params->vi_frame_rate = 30;
-               blackbird_api_cmd(dev, BLACKBIRD_API_SET_FRAMERATE, 1, 0, BLACKBIRD_FRAMERATE_NTSC_30);
-       }
-
-       /* assign aspect ratio */
-       if( params->vi_aspect_ratio >= ARRAY_SIZE(mpeg_stream_ratios) )
-               params->vi_aspect_ratio = V4L2_MPEG_ASPECT_4_3;
-       blackbird_api_cmd(dev, BLACKBIRD_API_SET_ASPECT_RATIO, 1, 0, mpeg_stream_ratios[params->vi_aspect_ratio]);
-
-       /* assign gop properties */
-       blackbird_api_cmd(dev, BLACKBIRD_API_SET_GOP_STRUCTURE, 2, 0, params->vi_frames_per_gop, params->vi_bframes_count+1);
-
-       /* assign gop closure */
-       blackbird_api_cmd(dev, BLACKBIRD_API_SET_GOP_CLOSURE, 1, 0, params->closed_gops);
-
-       /* assign 3 2 pulldown */
-       blackbird_api_cmd(dev, BLACKBIRD_API_SET_3_2_PULLDOWN, 1, 0, params->pulldown);
-
-       /* make sure the params are within bounds */
-       if( params->st_bitrate.mode >= ARRAY_SIZE(mpeg_video_bitrates) )
-               params->vi_bitrate.mode = V4L2_BITRATE_NONE;
-       if( params->vi_bitrate.mode >= ARRAY_SIZE(mpeg_video_bitrates) )
-               params->vi_bitrate.mode = V4L2_BITRATE_NONE;
-       if( params->au_bitrate.mode >= ARRAY_SIZE(mpeg_video_bitrates) )
-               params->au_bitrate.mode = V4L2_BITRATE_NONE;
-
-       /* assign audio properties */
-       /* note: it's not necessary to set the samplerate, the mpeg encoder seems to autodetect/adjust */
-       au_params = BLACKBIRD_AUDIO_BITS_STEREO |
-                       /* BLACKBIRD_AUDIO_BITS_BOUND_4 | */
-                       BLACKBIRD_AUDIO_BITS_EMPHASIS_NONE |
-                       BLACKBIRD_AUDIO_BITS_CRC_OFF |
-                       BLACKBIRD_AUDIO_BITS_COPYRIGHT_OFF |
-                       BLACKBIRD_AUDIO_BITS_COPY |
-                       0;
-       if( params->au_sample_rate <= 32000 )
-       {
-               params->au_sample_rate = 32000;
-               au_params |= BLACKBIRD_AUDIO_BITS_32000HZ;
-       }
-       else if( params->au_sample_rate <= 44100 )
-       {
-               params->au_sample_rate = 44100;
-               au_params |= BLACKBIRD_AUDIO_BITS_44100HZ;
-       }
-       else
-       {
-               params->au_sample_rate = 48000;
-               au_params |= BLACKBIRD_AUDIO_BITS_48000HZ;
-       }
-       if( params->au_type == V4L2_MPEG_AU_2_I )
-       {
-               au_params |= BLACKBIRD_AUDIO_BITS_LAYER_1;
-       }
-       else
-       {
-               /* TODO: try to handle the other formats more gracefully */
-               params->au_type = V4L2_MPEG_AU_2_II;
-               au_params |= BLACKBIRD_AUDIO_BITS_LAYER_2;
-       }
-       if( params->au_bitrate.mode )
-       {
-               int layer;
-
-               if( params->au_bitrate.mode == V4L2_BITRATE_CBR )
-                       params->au_bitrate.max = params->vi_bitrate.target;
-               else
-                       params->au_bitrate.target = params->vi_bitrate.max;
-
-               layer = params->au_type;
-               if( params->au_bitrate.target == 0 )
-               {
-                       /* TODO: use the minimum possible bitrate instead of 0 ? */
-                       au_params |= 0;
-               }
-               else if( params->au_bitrate.target >=
-                        mpeg_audio_bitrates[BITRATES_SIZE-1].layer[layer].rate )
-               {
-                       /* clamp the bitrate to the max supported by the standard */
-                       params->au_bitrate.target = mpeg_audio_bitrates[BITRATES_SIZE-1].layer[layer].rate;
-                       params->au_bitrate.max = params->au_bitrate.target;
-                       au_params |= mpeg_audio_bitrates[BITRATES_SIZE-1].layer[layer].bits;
-               }
-               else
-               {
-                       /* round up to the nearest supported bitrate */
-                       int i;
-                       for(i = 1; i < BITRATES_SIZE; i++)
-                       {
-                               if( params->au_bitrate.target > mpeg_audio_bitrates[i-1].layer[layer].rate &&
-                                   params->au_bitrate.target <= mpeg_audio_bitrates[i].layer[layer].rate )
-                               {
-                                       params->au_bitrate.target = mpeg_audio_bitrates[i].layer[layer].rate;
-                                       params->au_bitrate.max = params->au_bitrate.target;
-                                       au_params |= mpeg_audio_bitrates[i].layer[layer].bits;
-                                       break;
-                               }
-                       }
-               }
-       }
-       else
-       {
-               /* TODO: ??? */
-               params->au_bitrate.target = params->au_bitrate.max = 0;
-               au_params |= 0;
-       }
-       blackbird_api_cmd(dev, BLACKBIRD_API_SET_AUDIO_PARAMS, 1, 0, au_params );
-
-       /* assign bitrates */
-       if( params->vi_bitrate.mode )
-       {
-               /* bitrate is set, let's figure out the cbr/vbr mess */
-               if( params->vi_bitrate.max < params->vi_bitrate.target )
-               {
-                       if( params->vi_bitrate.mode == V4L2_BITRATE_CBR )
-                               params->vi_bitrate.max = params->vi_bitrate.target;
-                       else
-                               params->vi_bitrate.target = params->vi_bitrate.max;
-               }
-       }
-       else
-       {
-               if( params->st_bitrate.max < params->st_bitrate.target )
-               {
-                       if( params->st_bitrate.mode == V4L2_BITRATE_VBR )
-                               params->st_bitrate.target = params->st_bitrate.max;
-                       else
-                               params->st_bitrate.max = params->st_bitrate.target;
-               }
-               /* calculate vi_bitrate = st_bitrate - au_bitrate */
-               params->vi_bitrate.max = params->st_bitrate.max - params->au_bitrate.max;
-               params->vi_bitrate.target = params->st_bitrate.target - params->au_bitrate.target;
-       }
-       blackbird_api_cmd(dev, BLACKBIRD_API_SET_VIDEO_BITRATE, 4, 0,
-                               mpeg_video_bitrates[params->vi_bitrate.mode],
-                               params->vi_bitrate.target * 1000, /* kbps -> bps */
-                               params->vi_bitrate.max * 1000 / BLACKBIRD_PEAK_RATE_DIVISOR, /* peak/400 */
-                               BLACKBIRD_MUX_RATE_DEFAULT /*, 0x70*/); /* encoding buffer, ckennedy */
-
-       /* TODO: implement the stream ID stuff:
-               ts_pid_pmt, ts_pid_audio, ts_pid_video, ts_pid_pcr,
-               ps_size, au_pesid, vi_pesid
-       */
-}
-#define CHECK_PARAM( name ) ( dev->params.name != params->name )
-#define IF_PARAM( name ) if( CHECK_PARAM( name ) )
-#define UPDATE_PARAM( name ) dev->params.name = params->name
-void blackbird_set_params(struct cx8802_dev *dev, struct v4l2_mpeg_compression *params)
-{
-       u32 au_params;
-
-       /* assign stream type */
-       if( params->st_type >= ARRAY_SIZE(mpeg_stream_types) )
-               params->st_type = V4L2_MPEG_PS_2;
-       if( params->st_type == V4L2_MPEG_SS_1 )
-               params->vi_type = V4L2_MPEG_VI_1;
-       else
-               params->vi_type = V4L2_MPEG_VI_2;
-       if( CHECK_PARAM( st_type ) || CHECK_PARAM( vi_type ) )
-       {
-               UPDATE_PARAM( st_type );
-               UPDATE_PARAM( vi_type );
-               blackbird_api_cmd(dev, BLACKBIRD_API_SET_STREAM_TYPE, 1, 0, mpeg_stream_types[params->st_type]);
-       }
-
-       /* assign framerate */
-       if( params->vi_frame_rate <= 25 )
-               params->vi_frame_rate = 25;
-       else
-               params->vi_frame_rate = 30;
-       IF_PARAM( vi_frame_rate )
-       {
-               UPDATE_PARAM( vi_frame_rate );
-               if( params->vi_frame_rate == 25 )
-                       blackbird_api_cmd(dev, BLACKBIRD_API_SET_FRAMERATE, 1, 0, BLACKBIRD_FRAMERATE_PAL_25);
-               else
-                       blackbird_api_cmd(dev, BLACKBIRD_API_SET_FRAMERATE, 1, 0, BLACKBIRD_FRAMERATE_NTSC_30);
-       }
-
-       /* assign aspect ratio */
-       if( params->vi_aspect_ratio >= ARRAY_SIZE(mpeg_stream_ratios) )
-               params->vi_aspect_ratio = V4L2_MPEG_ASPECT_4_3;
-       IF_PARAM( vi_aspect_ratio )
-       {
-               UPDATE_PARAM( vi_aspect_ratio );
-               blackbird_api_cmd(dev, BLACKBIRD_API_SET_ASPECT_RATIO, 1, 0, mpeg_stream_ratios[params->vi_aspect_ratio]);
-       }
-
-       /* assign gop properties */
-       if( CHECK_PARAM( vi_frames_per_gop ) || CHECK_PARAM( vi_bframes_count ) )
-       {
-               UPDATE_PARAM( vi_frames_per_gop );
-               UPDATE_PARAM( vi_bframes_count );
-               blackbird_api_cmd(dev, BLACKBIRD_API_SET_GOP_STRUCTURE, 2, 0, params->vi_frames_per_gop, params->vi_bframes_count+1);
-       }
-
-       /* assign gop closure */
-       IF_PARAM( closed_gops )
-       {
-               UPDATE_PARAM( closed_gops );
-               blackbird_api_cmd(dev, BLACKBIRD_API_SET_GOP_CLOSURE, 1, 0, params->closed_gops);
-       }
-
-       /* assign 3 2 pulldown */
-       IF_PARAM( pulldown )
-       {
-               UPDATE_PARAM( pulldown );
-               blackbird_api_cmd(dev, BLACKBIRD_API_SET_3_2_PULLDOWN, 1, 0, params->pulldown);
-       }
-
-       /* make sure the params are within bounds */
-       if( params->st_bitrate.mode >= ARRAY_SIZE(mpeg_video_bitrates) )
-               params->vi_bitrate.mode = V4L2_BITRATE_NONE;
-       if( params->vi_bitrate.mode >= ARRAY_SIZE(mpeg_video_bitrates) )
-               params->vi_bitrate.mode = V4L2_BITRATE_NONE;
-       if( params->au_bitrate.mode >= ARRAY_SIZE(mpeg_video_bitrates) )
-               params->au_bitrate.mode = V4L2_BITRATE_NONE;
-
-       /* assign audio properties */
-       /* note: it's not necessary to set the samplerate, the mpeg encoder seems to autodetect/adjust */
-       au_params = BLACKBIRD_AUDIO_BITS_STEREO |
-                       /* BLACKBIRD_AUDIO_BITS_BOUND_4 | */
-       BLACKBIRD_AUDIO_BITS_EMPHASIS_NONE |
-               BLACKBIRD_AUDIO_BITS_CRC_OFF |
-               BLACKBIRD_AUDIO_BITS_COPYRIGHT_OFF |
-               BLACKBIRD_AUDIO_BITS_COPY |
-               0;
-       if( params->au_sample_rate < 32000 )
-       {
-               params->au_sample_rate = 32000;
-               au_params |= BLACKBIRD_AUDIO_BITS_32000HZ;
-       }
-       else if( params->au_sample_rate < 44100 )
-       {
-               params->au_sample_rate = 44100;
-               au_params |= BLACKBIRD_AUDIO_BITS_44100HZ;
-       }
-       else
-       {
-               params->au_sample_rate = 48000;
-               au_params |= BLACKBIRD_AUDIO_BITS_48000HZ;
-       }
-       if( params->au_type == V4L2_MPEG_AU_2_I )
-       {
-               au_params |= BLACKBIRD_AUDIO_BITS_LAYER_1;
-       }
-       else
-       {
-               /* TODO: try to handle the other formats more gracefully */
-               params->au_type = V4L2_MPEG_AU_2_II;
-               au_params |= BLACKBIRD_AUDIO_BITS_LAYER_2;
-       }
-       if( params->au_bitrate.mode )
-       {
-               int layer;
-
-               if( params->au_bitrate.mode == V4L2_BITRATE_CBR )
-                       params->au_bitrate.max = params->vi_bitrate.target;
-               else
-                       params->au_bitrate.target = params->vi_bitrate.max;
-
-               layer = params->au_type;
-               if( params->au_bitrate.target == 0 )
-               {
-                       /* TODO: use the minimum possible bitrate instead of 0 ? */
-                       au_params |= 0;
-               }
-               else if( params->au_bitrate.target >=
-                        mpeg_audio_bitrates[BITRATES_SIZE-1].layer[layer].rate )
-               {
-                       /* clamp the bitrate to the max supported by the standard */
-                       params->au_bitrate.target = mpeg_audio_bitrates[BITRATES_SIZE-1].layer[layer].rate;
-                       params->au_bitrate.max = params->au_bitrate.target;
-                       au_params |= mpeg_audio_bitrates[BITRATES_SIZE-1].layer[layer].bits;
-               }
-               else
-               {
-                       /* round up to the nearest supported bitrate */
-                       int i;
-                       for(i = 1; i < BITRATES_SIZE; i++)
-                       {
-                               if( params->au_bitrate.target > mpeg_audio_bitrates[i-1].layer[layer].rate &&
-                                   params->au_bitrate.target <= mpeg_audio_bitrates[i].layer[layer].rate )
-                               {
-                                       params->au_bitrate.target = mpeg_audio_bitrates[i].layer[layer].rate;
-                                       params->au_bitrate.max = params->au_bitrate.target;
-                                       au_params |= mpeg_audio_bitrates[i].layer[layer].bits;
-                                       break;
-                               }
-                       }
-               }
-       }
-       else
-       {
-               /* TODO: ??? */
-               params->au_bitrate.target = params->au_bitrate.max = 0;
-               au_params |= 0;
-       }
-       if( CHECK_PARAM( au_type ) || CHECK_PARAM( au_sample_rate )
-               || CHECK_PARAM( au_bitrate.mode ) || CHECK_PARAM( au_bitrate.max )
-               || CHECK_PARAM( au_bitrate.target )
-       )
-       {
-               UPDATE_PARAM( au_type );
-               UPDATE_PARAM( au_sample_rate );
-               UPDATE_PARAM( au_bitrate );
-               blackbird_api_cmd(dev, BLACKBIRD_API_SET_AUDIO_PARAMS, 1, 0, au_params );
-       }
-
-       /* assign bitrates */
-       if( params->vi_bitrate.mode )
-       {
-               /* bitrate is set, let's figure out the cbr/vbr mess */
-               if( params->vi_bitrate.max < params->vi_bitrate.target )
-               {
-                       if( params->vi_bitrate.mode == V4L2_BITRATE_CBR )
-                               params->vi_bitrate.max = params->vi_bitrate.target;
-                       else
-                               params->vi_bitrate.target = params->vi_bitrate.max;
-               }
-       }
-       else
-       {
-               if( params->st_bitrate.max < params->st_bitrate.target )
-               {
-                       if( params->st_bitrate.mode == V4L2_BITRATE_VBR )
-                               params->st_bitrate.target = params->st_bitrate.max;
-                       else
-                               params->st_bitrate.max = params->st_bitrate.target;
-               }
-               /* calculate vi_bitrate = st_bitrate - au_bitrate */
-               params->vi_bitrate.max = params->st_bitrate.max - params->au_bitrate.max;
-               params->vi_bitrate.target = params->st_bitrate.target - params->au_bitrate.target;
-       }
-       UPDATE_PARAM( st_bitrate );
-       if( CHECK_PARAM( vi_bitrate.mode ) || CHECK_PARAM( vi_bitrate.max )
-               || CHECK_PARAM( vi_bitrate.target )
-       )
-       {
-               UPDATE_PARAM( vi_bitrate );
-               blackbird_api_cmd(dev, BLACKBIRD_API_SET_VIDEO_BITRATE, 4, 0,
-                               mpeg_video_bitrates[params->vi_bitrate.mode],
-                               params->vi_bitrate.target * 1000, /* kbps -> bps */
-                               params->vi_bitrate.max * 1000 / BLACKBIRD_PEAK_RATE_DIVISOR, /* peak/400 */
-                               BLACKBIRD_MUX_RATE_DEFAULT /*, 0x70*/); /* encoding buffer, ckennedy */
-       }
-
-       /* TODO: implement the stream ID stuff:
-               ts_pid_pmt, ts_pid_audio, ts_pid_video, ts_pid_pcr,
-               ps_size, au_pesid, vi_pesid
-       */
-       UPDATE_PARAM( ts_pid_pmt );
-       UPDATE_PARAM( ts_pid_audio );
-       UPDATE_PARAM( ts_pid_video );
-       UPDATE_PARAM( ts_pid_pcr );
-       UPDATE_PARAM( ps_size );
-       UPDATE_PARAM( au_pesid );
-       UPDATE_PARAM( vi_pesid );
-}
-
-static void blackbird_set_default_dnr_params(struct cx8802_dev *dev)
-{
-       /* assign dnr filter mode */
-       if( dev->dnr_params.mode > BLACKBIRD_DNR_BITS_AUTO )
-               dev->dnr_params.mode = BLACKBIRD_DNR_BITS_MANUAL;
-       if( dev->dnr_params.type > BLACKBIRD_MEDIAN_FILTER_DIAGONAL )
-               dev->dnr_params.type = BLACKBIRD_MEDIAN_FILTER_DISABLED;
-       blackbird_api_cmd(dev, BLACKBIRD_API_SET_DNR_MODE, 2, 0,
-                               dev->dnr_params.mode,
-                               dev->dnr_params.type
-                       );
-
-       /* assign dnr filter props*/
-       if( dev->dnr_params.spatial > 15 )
-               dev->dnr_params.spatial = 15;
-       if( dev->dnr_params.temporal > 31 )
-               dev->dnr_params.temporal = 31;
-       blackbird_api_cmd(dev, BLACKBIRD_API_SET_MANUAL_DNR, 2, 0,
-                               dev->dnr_params.spatial,
-                               dev->dnr_params.temporal
-                       );
-}
-#define CHECK_DNR_PARAM( name ) ( dev->dnr_params.name != dnr_params->name )
-#define UPDATE_DNR_PARAM( name ) dev->dnr_params.name = dnr_params->name
-void blackbird_set_dnr_params(struct cx8802_dev *dev, struct blackbird_dnr* dnr_params)
-{
-       /* assign dnr filter mode */
-       /* clamp values */
-       if( dnr_params->mode > BLACKBIRD_DNR_BITS_AUTO )
-               dnr_params->mode = BLACKBIRD_DNR_BITS_MANUAL;
-       if( dnr_params->type > BLACKBIRD_MEDIAN_FILTER_DIAGONAL )
-               dnr_params->type = BLACKBIRD_MEDIAN_FILTER_DISABLED;
-       /* check if the params actually changed */
-       if( CHECK_DNR_PARAM( mode ) || CHECK_DNR_PARAM( type ) )
-       {
-               UPDATE_DNR_PARAM( mode );
-               UPDATE_DNR_PARAM( type );
-               blackbird_api_cmd(dev, BLACKBIRD_API_SET_DNR_MODE, 2, 0, dnr_params->mode, dnr_params->type);
-       }
-
-       /* assign dnr filter props*/
-       if( dnr_params->spatial > 15 )
-               dnr_params->spatial = 15;
-       if( dnr_params->temporal > 31 )
-               dnr_params->temporal = 31;
-       if( CHECK_DNR_PARAM( spatial ) || CHECK_DNR_PARAM( temporal ) )
-       {
-               UPDATE_DNR_PARAM( spatial );
-               UPDATE_DNR_PARAM( temporal );
-               blackbird_api_cmd(dev, BLACKBIRD_API_SET_MANUAL_DNR, 2, 0, dnr_params->spatial, dnr_params->temporal);
-       }
-}
-
 static void blackbird_codec_settings(struct cx8802_dev *dev)
 {
-
-       /* assign output port */
-       blackbird_api_cmd(dev, BLACKBIRD_API_SET_OUTPUT_PORT, 1, 0, BLACKBIRD_OUTPUT_PORT_STREAMING); /* Host */
-
        /* assign frame size */
-       blackbird_api_cmd(dev, BLACKBIRD_API_SET_RESOLUTION, 2, 0,
+       blackbird_api_cmd(dev, CX2341X_ENC_SET_FRAME_SIZE, 2, 0,
                                dev->height, dev->width);
 
-       /* assign coring levels (luma_h, luma_l, chroma_h, chroma_l) */
-       blackbird_api_cmd(dev, BLACKBIRD_API_SET_DNR_MEDIAN, 4, 0, 0, 255, 0, 255);
+       dev->params.width = dev->width;
+       dev->params.height = dev->height;
+       dev->params.is_50hz = (dev->core->tvnorm & V4L2_STD_625_50) != 0;
 
-       /* assign spatial filter type: luma_t: horiz_only, chroma_t: horiz_only */
-       blackbird_api_cmd(dev, BLACKBIRD_API_SET_SPATIAL_FILTER, 2, 0,
-                               BLACKBIRD_SPATIAL_FILTER_LUMA_1D_HORIZ,
-                               BLACKBIRD_SPATIAL_FILTER_CHROMA_1D_HORIZ
-                       );
-
-       /* assign frame drop rate */
-       /* blackbird_api_cmd(dev, IVTV_API_ASSIGN_FRAME_DROP_RATE, 1, 0, 0); */
-
-       blackbird_set_default_params(dev);
-       blackbird_set_default_dnr_params(dev);
+       cx2341x_update(dev, blackbird_mbox_func, NULL, &dev->params);
 }
 
 static int blackbird_initialize_codec(struct cx8802_dev *dev)
@@ -1248,35 +536,37 @@ static int blackbird_initialize_codec(struct cx8802_dev *dev)
        int retval;
 
        dprintk(1,"Initialize codec\n");
-       retval = blackbird_api_cmd(dev, BLACKBIRD_API_PING, 0, 0); /* ping */
+       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;
 
-               retval = blackbird_api_cmd(dev, BLACKBIRD_API_PING, 0, 0); /* ping */
+               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");
                        return -1;
                }
 
-               retval = blackbird_api_cmd(dev, BLACKBIRD_API_GET_VERSION, 0, 1, &version);
+               retval = blackbird_api_cmd(dev, CX2341X_ENC_GET_VERSION, 0, 1, &version);
                if (retval < 0) {
                        dprintk(0, "ERROR: Firmware get encoder version failed!\n");
                        return -1;
                }
                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? */
@@ -1284,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, BLACKBIRD_API_SET_CAPTURE_LINES, 2, 0,
+       blackbird_api_cmd(dev, CX2341X_ENC_SET_NUM_VSYNC_LINES, 2, 0,
                        BLACKBIRD_FIELD1_SAA7115,
-                       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, BLACKBIRD_API_SET_CUSTOM_DATA, 12, 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, BLACKBIRD_API_INIT_VIDEO_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, BLACKBIRD_API_MUTE_VIDEO, 1, 0, BLACKBIRD_UNMUTE);
-       msleep(1);
-       blackbird_api_cmd(dev, BLACKBIRD_API_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, BLACKBIRD_API_BEGIN_CAPTURE, 2, 0, 0, 0x13); */
-       blackbird_api_cmd(dev, BLACKBIRD_API_BEGIN_CAPTURE, 2, 0,
+       blackbird_api_cmd(dev, CX2341X_ENC_START_CAPTURE, 2, 0,
                        BLACKBIRD_MPEG_CAPTURE,
                        BLACKBIRD_RAW_BITS_NONE
                );
-       msleep(10);
 
-       blackbird_api_cmd(dev, BLACKBIRD_API_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;
 }
 
@@ -1341,7 +659,7 @@ bb_buf_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
               enum v4l2_field field)
 {
        struct cx8802_fh *fh = q->priv_data;
-       return cx8802_buf_prepare(fh->dev, (struct cx88_buffer*)vb, field);
+       return cx8802_buf_prepare(q, fh->dev, (struct cx88_buffer*)vb, field);
 }
 
 static void
@@ -1354,8 +672,7 @@ bb_buf_queue(struct videobuf_queue *q, struct videobuf_buffer *vb)
 static void
 bb_buf_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
 {
-       struct cx8802_fh *fh = q->priv_data;
-       cx88_free_buffer(fh->dev->pci, (struct cx88_buffer*)vb);
+       cx88_free_buffer(q, (struct cx88_buffer*)vb);
 }
 
 static struct videobuf_queue_ops blackbird_qops = {
@@ -1367,187 +684,412 @@ static struct videobuf_queue_ops blackbird_qops = {
 
 /* ------------------------------------------------------------------ */
 
-static int mpeg_do_ioctl(struct inode *inode, struct file *file,
-                        unsigned int cmd, void *arg)
+static const u32 *ctrl_classes[] = {
+       cx88_user_ctrls,
+       cx2341x_mpeg_ctrls,
+       NULL
+};
+
+static int blackbird_queryctrl(struct cx8802_dev *dev, struct v4l2_queryctrl *qctrl)
 {
-       struct cx8802_fh  *fh  = file->private_data;
-       struct cx8802_dev *dev = fh->dev;
+       qctrl->id = v4l2_ctrl_next(ctrl_classes, qctrl->id);
+       if (qctrl->id == 0)
+               return -EINVAL;
+
+       /* Standard V4L2 controls */
+       if (cx8800_ctrl_query(dev->core, qctrl) == 0)
+               return 0;
+
+       /* MPEG V4L2 controls */
+       if (cx2341x_ctrl_query(&dev->params, qctrl))
+               qctrl->flags |= V4L2_CTRL_FLAG_DISABLED;
+       return 0;
+}
+
+/* ------------------------------------------------------------------ */
+/* IOCTL Handlers                                                     */
+
+static int vidioc_querymenu (struct file *file, void *priv,
+                               struct v4l2_querymenu *qmenu)
+{
+       struct cx8802_dev *dev  = ((struct cx8802_fh *)priv)->dev;
+       struct v4l2_queryctrl qctrl;
+
+       qctrl.id = qmenu->id;
+       blackbird_queryctrl(dev, &qctrl);
+       return v4l2_ctrl_query_menu(qmenu, &qctrl,
+                       cx2341x_ctrl_get_menu(&dev->params, qmenu->id));
+}
+
+static int vidioc_querycap (struct file *file, void  *priv,
+                                       struct v4l2_capability *cap)
+{
+       struct cx8802_dev *dev  = ((struct cx8802_fh *)priv)->dev;
        struct cx88_core  *core = dev->core;
 
-       if (debug > 1)
-               cx88_print_ioctl(core->name,cmd);
-
-       switch (cmd) {
-
-       /* --- capabilities ------------------------------------------ */
-       case VIDIOC_QUERYCAP:
-       {
-               struct v4l2_capability *cap = arg;
-
-               memset(cap,0,sizeof(*cap));
-               strcpy(cap->driver, "cx88_blackbird");
-               strlcpy(cap->card, cx88_boards[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     |
-                       0;
-               if (UNSET != core->tuner_type)
-                       cap->capabilities |= V4L2_CAP_TUNER;
+       strcpy(cap->driver, "cx88_blackbird");
+       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->board.tuner_type)
+               cap->capabilities |= V4L2_CAP_TUNER;
+       return 0;
+}
 
-               return 0;
-       }
+static int vidioc_enum_fmt_vid_cap (struct file *file, void  *priv,
+                                       struct v4l2_fmtdesc *f)
+{
+       if (f->index != 0)
+               return -EINVAL;
 
-       /* --- capture ioctls ---------------------------------------- */
-       case VIDIOC_ENUM_FMT:
-       {
-               struct v4l2_fmtdesc *f = arg;
-               int index;
-
-               index = f->index;
-               if (index != 0)
-                       return -EINVAL;
-
-               memset(f,0,sizeof(*f));
-               f->index = index;
-               strlcpy(f->description, "MPEG", sizeof(f->description));
-               f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
-               f->pixelformat = V4L2_PIX_FMT_MPEG;
-               return 0;
-       }
-       case VIDIOC_G_FMT:
-       {
-               struct v4l2_format *f = arg;
-
-               memset(f,0,sizeof(*f));
-               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; */
-               f->fmt.pix.colorspace   = 0;
-               f->fmt.pix.width        = dev->width;
-               f->fmt.pix.height       = dev->height;
-               f->fmt.pix.field        = fh->mpegq.field;
-               dprintk(0,"VIDIOC_G_FMT: w: %d, h: %d, f: %d\n",
-                       dev->width, dev->height, fh->mpegq.field );
-               return 0;
-       }
-       case VIDIOC_TRY_FMT:
-       {
-               struct v4l2_format *f = arg;
-
-               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; */;
-               f->fmt.pix.colorspace   = 0;
-               dprintk(0,"VIDIOC_TRY_FMT: w: %d, h: %d, f: %d\n",
-                       dev->width, dev->height, fh->mpegq.field );
-               return 0;
-       }
-       case VIDIOC_S_FMT:
-       {
-               struct v4l2_format *f = arg;
-
-               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; */;
-               f->fmt.pix.colorspace   = 0;
-               dprintk(0,"VIDIOC_S_FMT: w: %d, h: %d, f: %d\n",
-                       f->fmt.pix.width, f->fmt.pix.height, f->fmt.pix.field );
-               return 0;
-       }
+       strlcpy(f->description, "MPEG", sizeof(f->description));
+       f->pixelformat = V4L2_PIX_FMT_MPEG;
+       return 0;
+}
 
-       /* --- streaming capture ------------------------------------- */
-       case VIDIOC_REQBUFS:
-               return videobuf_reqbufs(&fh->mpegq, arg);
+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->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; */
+       f->fmt.pix.colorspace   = 0;
+       f->fmt.pix.width        = dev->width;
+       f->fmt.pix.height       = dev->height;
+       f->fmt.pix.field        = fh->mpegq.field;
+       dprintk(0,"VIDIOC_G_FMT: w: %d, h: %d, f: %d\n",
+               dev->width, dev->height, fh->mpegq.field );
+       return 0;
+}
 
-       case VIDIOC_QUERYBUF:
-               return videobuf_querybuf(&fh->mpegq, arg);
+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->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; */;
+       f->fmt.pix.colorspace   = 0;
+       dprintk(0,"VIDIOC_TRY_FMT: w: %d, h: %d, f: %d\n",
+               dev->width, dev->height, fh->mpegq.field );
+       return 0;
+}
 
-       case VIDIOC_QBUF:
-               return videobuf_qbuf(&fh->mpegq, arg);
+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;
 
-       case VIDIOC_DQBUF:
-               return videobuf_dqbuf(&fh->mpegq, arg,
-                                     file->f_flags & O_NONBLOCK);
+       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; */;
+       f->fmt.pix.colorspace   = 0;
+       dev->width              = f->fmt.pix.width;
+       dev->height             = f->fmt.pix.height;
+       fh->mpegq.field         = f->fmt.pix.field;
+       cx88_set_scale(core, f->fmt.pix.width, f->fmt.pix.height, f->fmt.pix.field);
+       blackbird_api_cmd(dev, CX2341X_ENC_SET_FRAME_SIZE, 2, 0,
+                               f->fmt.pix.height, f->fmt.pix.width);
+       dprintk(0,"VIDIOC_S_FMT: w: %d, h: %d, f: %d\n",
+               f->fmt.pix.width, f->fmt.pix.height, f->fmt.pix.field );
+       return 0;
+}
 
-       case VIDIOC_STREAMON:
-               return videobuf_streamon(&fh->mpegq);
+static int vidioc_reqbufs (struct file *file, void *priv, struct v4l2_requestbuffers *p)
+{
+       struct cx8802_fh  *fh   = priv;
+       return (videobuf_reqbufs(&fh->mpegq, p));
+}
 
-       case VIDIOC_STREAMOFF:
-               return videobuf_streamoff(&fh->mpegq);
+static int vidioc_querybuf (struct file *file, void *priv, struct v4l2_buffer *p)
+{
+       struct cx8802_fh  *fh   = priv;
+       return (videobuf_querybuf(&fh->mpegq, p));
+}
 
-       /* --- mpeg compression -------------------------------------- */
-       case VIDIOC_G_MPEGCOMP:
-       {
-               struct v4l2_mpeg_compression *f = arg;
+static int vidioc_qbuf (struct file *file, void *priv, struct v4l2_buffer *p)
+{
+       struct cx8802_fh  *fh   = priv;
+       return (videobuf_qbuf(&fh->mpegq, p));
+}
 
-               memcpy(f,&dev->params,sizeof(*f));
-               return 0;
+static int vidioc_dqbuf (struct file *file, void *priv, struct v4l2_buffer *p)
+{
+       struct cx8802_fh  *fh   = priv;
+       return (videobuf_dqbuf(&fh->mpegq, p,
+                               file->f_flags & O_NONBLOCK));
+}
+
+static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
+{
+       struct cx8802_fh  *fh   = priv;
+       return videobuf_streamon(&fh->mpegq);
+}
+
+static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
+{
+       struct cx8802_fh  *fh   = priv;
+       return videobuf_streamoff(&fh->mpegq);
+}
+
+static int vidioc_g_ext_ctrls (struct file *file, void *priv,
+                              struct v4l2_ext_controls *f)
+{
+       struct cx8802_dev *dev  = ((struct cx8802_fh *)priv)->dev;
+
+       if (f->ctrl_class != V4L2_CTRL_CLASS_MPEG)
+               return -EINVAL;
+       return cx2341x_ext_ctrls(&dev->params, 0, f, VIDIOC_G_EXT_CTRLS);
+}
+
+static int vidioc_s_ext_ctrls (struct file *file, void *priv,
+                              struct v4l2_ext_controls *f)
+{
+       struct cx8802_dev *dev  = ((struct cx8802_fh *)priv)->dev;
+       struct cx2341x_mpeg_params p;
+       int err;
+
+       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, 0, f, VIDIOC_S_EXT_CTRLS);
+       if (!err) {
+               err = cx2341x_update(dev, blackbird_mbox_func, &dev->params, &p);
+               dev->params = p;
        }
-       case VIDIOC_S_MPEGCOMP:
-       {
-               struct v4l2_mpeg_compression *f = arg;
+       return err;
+}
+
+static int vidioc_try_ext_ctrls (struct file *file, void *priv,
+                              struct v4l2_ext_controls *f)
+{
+       struct cx8802_dev *dev  = ((struct cx8802_fh *)priv)->dev;
+       struct cx2341x_mpeg_params p;
+       int err;
 
-               blackbird_set_params(dev, f);
+       if (f->ctrl_class != V4L2_CTRL_CLASS_MPEG)
+               return -EINVAL;
+       p = dev->params;
+       err = cx2341x_ext_ctrls(&p, 0, f, VIDIOC_TRY_EXT_CTRLS);
+
+       return err;
+}
+
+static int vidioc_s_frequency (struct file *file, void *priv,
+                               struct v4l2_frequency *f)
+{
+       struct cx8802_fh  *fh   = priv;
+       struct cx8802_dev *dev  = fh->dev;
+       struct cx88_core  *core = dev->core;
+
+       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,
+                       fh->mpegq.field);
+       return 0;
+}
+
+static int vidioc_log_status (struct file *file, void *priv)
+{
+       struct cx8802_dev *dev  = ((struct cx8802_fh *)priv)->dev;
+       struct cx88_core  *core = dev->core;
+       char name[32 + 2];
+
+       snprintf(name, sizeof(name), "%s/2", core->name);
+       printk("%s/2: ============  START LOG STATUS  ============\n",
+               core->name);
+       call_all(core, core, log_status);
+       cx2341x_log_status(&dev->params, name);
+       printk("%s/2: =============  END LOG STATUS  =============\n",
+               core->name);
+       return 0;
+}
+
+static int vidioc_queryctrl (struct file *file, void *priv,
+                               struct v4l2_queryctrl *qctrl)
+{
+       struct cx8802_dev *dev  = ((struct cx8802_fh *)priv)->dev;
+
+       if (blackbird_queryctrl(dev, qctrl) == 0)
                return 0;
-       }
 
-       default:
-               return cx88_do_ioctl( inode, file, 0, dev->core, cmd, arg, cx88_ioctl_hook );
-       }
+       qctrl->id = v4l2_ctrl_next(ctrl_classes, qctrl->id);
+       if (unlikely(qctrl->id == 0))
+               return -EINVAL;
+       return cx8800_ctrl_query(dev->core, qctrl);
+}
+
+static int vidioc_enum_input (struct file *file, void *priv,
+                               struct v4l2_input *i)
+{
+       struct cx88_core  *core = ((struct cx8802_fh *)priv)->dev->core;
+       return cx88_enum_input (core,i);
+}
+
+static int vidioc_g_ctrl (struct file *file, void *priv,
+                               struct v4l2_control *ctl)
+{
+       struct cx88_core  *core = ((struct cx8802_fh *)priv)->dev->core;
+       return
+               cx88_get_control(core,ctl);
+}
+
+static int vidioc_s_ctrl (struct file *file, void *priv,
+                               struct v4l2_control *ctl)
+{
+       struct cx88_core  *core = ((struct cx8802_fh *)priv)->dev->core;
+       return
+               cx88_set_control(core,ctl);
+}
+
+static int vidioc_g_frequency (struct file *file, void *priv,
+                               struct v4l2_frequency *f)
+{
+       struct cx8802_fh  *fh   = priv;
+       struct cx88_core  *core = fh->dev->core;
+
+       if (unlikely(UNSET == core->board.tuner_type))
+               return -EINVAL;
+
+       f->type = V4L2_TUNER_ANALOG_TV;
+       f->frequency = core->freq;
+       call_all(core, tuner, g_frequency, f);
+
        return 0;
 }
 
-int (*cx88_ioctl_hook)(struct inode *inode, struct file *file,
-                       unsigned int cmd, void *arg);
-unsigned int (*cx88_ioctl_translator)(unsigned int cmd);
+static int vidioc_g_input (struct file *file, void *priv, unsigned int *i)
+{
+       struct cx88_core  *core = ((struct cx8802_fh *)priv)->dev->core;
 
-static unsigned int mpeg_translate_ioctl(unsigned int cmd)
+       *i = core->input;
+       return 0;
+}
+
+static int vidioc_s_input (struct file *file, void *priv, unsigned int i)
 {
-       return cmd;
+       struct cx88_core  *core = ((struct cx8802_fh *)priv)->dev->core;
+
+       if (i >= 4)
+               return -EINVAL;
+
+       mutex_lock(&core->lock);
+       cx88_newstation(core);
+       cx88_video_mux(core,i);
+       mutex_unlock(&core->lock);
+       return 0;
 }
 
-static int mpeg_ioctl(struct inode *inode, struct file *file,
-                       unsigned int cmd, unsigned long arg)
+static int vidioc_g_tuner (struct file *file, void *priv,
+                               struct v4l2_tuner *t)
 {
-       cmd = cx88_ioctl_translator( cmd );
-       return video_usercopy(inode, file, cmd, arg, cx88_ioctl_hook);
+       struct cx88_core  *core = ((struct cx8802_fh *)priv)->dev->core;
+       u32 reg;
+
+       if (unlikely(UNSET == core->board.tuner_type))
+               return -EINVAL;
+       if (0 != t->index)
+               return -EINVAL;
+
+       strcpy(t->name, "Television");
+       t->type       = V4L2_TUNER_ANALOG_TV;
+       t->capability = V4L2_TUNER_CAP_NORM;
+       t->rangehigh  = 0xffffffffUL;
+
+       cx88_get_stereo(core ,t);
+       reg = cx_read(MO_DEVICE_STATUS);
+       t->signal = (reg & (1<<5)) ? 0xffff : 0x0000;
+       return 0;
 }
 
-static int mpeg_open(struct inode *inode, struct file *file)
+static int vidioc_s_tuner (struct file *file, void *priv,
+                               struct v4l2_tuner *t)
 {
-       int minor = iminor(inode);
-       struct cx8802_dev *h,*dev = NULL;
+       struct cx88_core  *core = ((struct cx8802_fh *)priv)->dev->core;
+
+       if (UNSET == core->board.tuner_type)
+               return -EINVAL;
+       if (0 != t->index)
+               return -EINVAL;
+
+       cx88_set_stereo(core, t->audmode, 1);
+       return 0;
+}
+
+static int vidioc_s_std (struct file *file, void *priv, v4l2_std_id *id)
+{
+       struct cx88_core  *core = ((struct cx8802_fh *)priv)->dev->core;
+
+       mutex_lock(&core->lock);
+       cx88_set_tvnorm(core,*id);
+       mutex_unlock(&core->lock);
+       return 0;
+}
+
+/* FIXME: cx88_ioctl_hook not implemented */
+
+static int mpeg_open(struct file *file)
+{
+       int minor = video_devdata(file)->minor;
+       struct cx8802_dev *dev = video_drvdata(file);
        struct cx8802_fh *fh;
-       struct list_head *list;
+       struct cx8802_driver *drv = NULL;
+       int err;
 
-       list_for_each(list,&cx8802_devlist) {
-               h = list_entry(list, struct cx8802_dev, devlist);
-               if (h->mpeg_dev->minor == minor)
-                       dev = h;
+       dprintk( 1, "%s\n", __func__);
+
+       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", __func__, err);
+                       unlock_kernel();
+                       return err;
+               }
        }
-       if (NULL == dev)
-               return -ENODEV;
 
-       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);
 
        /* allocate + initialize per filehandle data */
-       fh = kmalloc(sizeof(*fh),GFP_KERNEL);
-       if (NULL == fh)
+       fh = kzalloc(sizeof(*fh),GFP_KERNEL);
+       if (NULL == fh) {
+               if (drv)
+                       drv->request_release(drv);
+               unlock_kernel();
                return -ENOMEM;
-       memset(fh,0,sizeof(*fh));
+       }
        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),
@@ -1556,30 +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 = fh->dev;
+       struct cx8802_driver *drv = NULL;
 
-       /* blackbird_api_cmd(fh->dev, BLACKBIRD_API_END_CAPTURE, 3, 0, BLACKBIRD_END_NOW, 0, 0x13); */
-       blackbird_api_cmd(fh->dev, BLACKBIRD_API_END_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 */
+       drv = cx8802_get_driver(dev, CX88_MPEG_BLACKBIRD);
+       if (drv)
+               drv->request_release(drv);
+
+       atomic_dec(&dev->core->mpeg_users);
+
        return 0;
 }
 
@@ -1587,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);
@@ -1596,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);
 }
@@ -1608,7 +1165,7 @@ mpeg_mmap(struct file *file, struct vm_area_struct * vma)
        return videobuf_mmap_mapper(&fh->mpegq, vma);
 }
 
-static struct file_operations mpeg_fops =
+static const struct v4l2_file_operations mpeg_fops =
 {
        .owner         = THIS_MODULE,
        .open          = mpeg_open,
@@ -1616,25 +1173,100 @@ static struct file_operations mpeg_fops =
        .read          = mpeg_read,
        .poll          = mpeg_poll,
        .mmap          = mpeg_mmap,
-       .ioctl         = mpeg_ioctl,
-       .llseek        = no_llseek,
+       .ioctl         = video_ioctl2,
 };
 
-static struct video_device cx8802_mpeg_template =
-{
-       .name          = "cx8802",
-       .type          = VID_TYPE_CAPTURE|VID_TYPE_TUNER|VID_TYPE_SCALES|VID_TYPE_MPEG_ENCODER,
-       .hardware      = 0,
-       .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_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_ext_ctrls   = vidioc_g_ext_ctrls,
+       .vidioc_s_ext_ctrls   = vidioc_s_ext_ctrls,
+       .vidioc_try_ext_ctrls = vidioc_try_ext_ctrls,
+       .vidioc_s_frequency   = vidioc_s_frequency,
+       .vidioc_log_status    = vidioc_log_status,
+       .vidioc_queryctrl     = vidioc_queryctrl,
+       .vidioc_enum_input    = vidioc_enum_input,
+       .vidioc_g_ctrl        = vidioc_g_ctrl,
+       .vidioc_s_ctrl        = vidioc_s_ctrl,
+       .vidioc_g_frequency   = vidioc_g_frequency,
+       .vidioc_g_input       = vidioc_g_input,
+       .vidioc_s_input       = vidioc_s_input,
+       .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,
 };
 
 /* ------------------------------------------------------------------ */
 
+/* The CX8802 MPEG API will call this when we can use the hardware */
+static int cx8802_blackbird_advise_acquire(struct cx8802_driver *drv)
+{
+       struct cx88_core *core = drv->core;
+       int err = 0;
+
+       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.
+                */
+               /* 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;
+       }
+       return err;
+}
+
+/* The CX8802 MPEG API will call this when we need to release the hardware */
+static int cx8802_blackbird_advise_release(struct cx8802_driver *drv)
+{
+       struct cx88_core *core = drv->core;
+       int err = 0;
+
+       switch (core->boardnr) {
+       case CX88_BOARD_HAUPPAUGE_HVR1300:
+               /* Exit leaving the cx23416 on the bus */
+               break;
+       default:
+               err = -ENODEV;
+       }
+       return err;
+}
+
 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);
@@ -1648,118 +1280,89 @@ 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;
 }
 
 /* ----------------------------------------------------------- */
 
-static int __devinit blackbird_probe(struct pci_dev *pci_dev,
-                                    const struct pci_device_id *pci_id)
+static int cx8802_blackbird_probe(struct cx8802_driver *drv)
 {
-       struct cx8802_dev *dev;
-       struct cx88_core  *core;
+       struct cx88_core *core = drv->core;
+       struct cx8802_dev *dev = core->dvbdev;
        int err;
 
-       /* general setup */
-       core = cx88_core_get(pci_dev);
-       if (NULL == core)
-               return -EINVAL;
+       dprintk( 1, "%s\n", __func__);
+       dprintk( 1, " ->being probed by Card=%d Name=%s, PCI %02x:%02x\n",
+               core->boardnr,
+               core->name,
+               core->pci_bus,
+               core->pci_slot);
 
        err = -ENODEV;
-       if (!cx88_boards[core->board].blackbird)
+       if (!(core->board.mpeg & CX88_MPEG_BLACKBIRD))
                goto fail_core;
 
-       err = -ENOMEM;
-       dev = kmalloc(sizeof(*dev),GFP_KERNEL);
-       if (NULL == dev)
-               goto fail_core;
-       memset(dev,0,sizeof(*dev));
-       dev->pci = pci_dev;
-       dev->core = core;
        dev->width = 720;
        dev->height = 576;
-       memcpy(&dev->params,&default_mpeg_params,sizeof(default_mpeg_params));
-       memcpy(&dev->dnr_params,&default_dnr_params,sizeof(default_dnr_params));
+       cx2341x_fill_defaults(&dev->params);
+       dev->params.port = CX2341X_PORT_STREAMING;
 
-       if (core->board == CX88_BOARD_HAUPPAUGE_ROSLYN) {
-
-               if (core->tuner_formats & V4L2_STD_525_60) {
-                       dev->height = 480;
-                       dev->params.vi_frame_rate = 30;
-               } else {
-                       dev->height = 576;
-                       dev->params.vi_frame_rate = 25;
-               }
+       cx8802_mpeg_template.current_norm = core->tvnorm;
 
+       if (core->tvnorm & V4L2_STD_525_60) {
+               dev->height = 480;
+       } else {
+               dev->height = 576;
        }
 
-       err = cx8802_init_common(dev);
-       if (0 != err)
-               goto fail_free;
-
        /* blackbird stuff */
        printk("%s/2: cx23416 based mpeg encoder (blackbird reference design)\n",
               core->name);
        host_setup(dev->core);
 
-       list_add_tail(&dev->devlist,&cx8802_devlist);
+       blackbird_initialize_codec(dev);
        blackbird_register_video(dev);
 
        /* initial device configuration: needed ? */
+       mutex_lock(&dev->core->lock);
+//     init_controls(core);
+       cx88_set_tvnorm(core,core->tvnorm);
+       cx88_video_mux(core,0);
+       mutex_unlock(&dev->core->lock);
 
        return 0;
 
- fail_free:
-       kfree(dev);
  fail_core:
-       cx88_core_put(core,pci_dev);
        return err;
 }
 
-static void __devexit blackbird_remove(struct pci_dev *pci_dev)
+static int cx8802_blackbird_remove(struct cx8802_driver *drv)
 {
-       struct cx8802_dev *dev = pci_get_drvdata(pci_dev);
-
        /* blackbird */
-       blackbird_unregister_video(dev);
-       list_del(&dev->devlist);
+       blackbird_unregister_video(drv->core->dvbdev);
 
-       /* common */
-       cx8802_fini_common(dev);
-       cx88_core_put(dev->core,dev->pci);
-       kfree(dev);
+       return 0;
 }
 
-static struct pci_device_id cx8802_pci_tbl[] = {
-       {
-               .vendor       = 0x14f1,
-               .device       = 0x8802,
-               .subvendor    = PCI_ANY_ID,
-               .subdevice    = PCI_ANY_ID,
-       },{
-               /* --- end of list --- */
-       }
-};
-MODULE_DEVICE_TABLE(pci, cx8802_pci_tbl);
-
-static struct pci_driver blackbird_pci_driver = {
-       .name     = "cx88-blackbird",
-       .id_table = cx8802_pci_tbl,
-       .probe    = blackbird_probe,
-       .remove   = __devexit_p(blackbird_remove),
-       .suspend  = cx8802_suspend_common,
-       .resume   = cx8802_resume_common,
+static struct cx8802_driver cx8802_blackbird_driver = {
+       .type_id        = CX88_MPEG_BLACKBIRD,
+       .hw_access      = CX8802_DRVCTL_SHARED,
+       .probe          = cx8802_blackbird_probe,
+       .remove         = cx8802_blackbird_remove,
+       .advise_acquire = cx8802_blackbird_advise_acquire,
+       .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,
@@ -1769,23 +1372,19 @@ static int blackbird_init(void)
        printk(KERN_INFO "cx2388x: snapshot date %04d-%02d-%02d\n",
               SNAPSHOT/10000, (SNAPSHOT/100)%100, SNAPSHOT%100);
 #endif
-       cx88_ioctl_hook = mpeg_do_ioctl;
-       cx88_ioctl_translator = mpeg_translate_ioctl;
-       return pci_register_driver(&blackbird_pci_driver);
+       return cx8802_register_driver(&cx8802_blackbird_driver);
 }
 
-static void blackbird_fini(void)
+static void __exit blackbird_fini(void)
 {
-       pci_unregister_driver(&blackbird_pci_driver);
+       cx8802_unregister_driver(&cx8802_blackbird_driver);
 }
 
 module_init(blackbird_init);
 module_exit(blackbird_fini);
 
-EXPORT_SYMBOL(cx88_ioctl_hook);
-EXPORT_SYMBOL(cx88_ioctl_translator);
-EXPORT_SYMBOL(blackbird_set_params);
-EXPORT_SYMBOL(blackbird_set_dnr_params);
+module_param_named(video_debug,cx8802_mpeg_template.debug, int, 0644);
+MODULE_PARM_DESC(debug,"enable debug messages [video]");
 
 /* ----------------------------------------------------------- */
 /*