fedora core 6 1.2949 + vserver 2.2.0
[linux-2.6.git] / drivers / media / video / cx88 / cx88-blackbird.c
index ef56a5d..1c04516 100644 (file)
@@ -1,5 +1,4 @@
 /*
- * $Id: cx88-blackbird.c,v 1.17 2004/11/07 13:17:15 kraxel Exp $
  *
  *  Support for a cx23416 mpeg encoder via cx2388x host port.
  *  "blackbird" reference design.
  */
 
 #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 <media/v4l2-common.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.8m.com>, Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]");
 MODULE_LICENSE("GPL");
 
-static unsigned int mpegbufs = 8;
+static unsigned int mpegbufs = 32;
 module_param(mpegbufs,int,0644);
 MODULE_PARM_DESC(mpegbufs,"number of mpeg buffers, range 2-32");
 
@@ -49,48 +50,121 @@ 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 OLD_BLACKBIRD_FIRM_IMAGE_SIZE 262144
+#define     BLACKBIRD_FIRM_IMAGE_SIZE 376836
 
 /* defines below are from ivtv-driver.h */
 
 #define IVTV_CMD_HW_BLOCKS_RST 0xFFFFFFFF
 
-/*Firmware API commands*/
-#define IVTV_API_ENC_PING_FW 0x00000080
-#define IVTV_API_ENC_GETVER 0x000000C4
-#define IVTV_API_ENC_HALT_FW 0x000000C3
-#define IVTV_API_STD_TIMEOUT 0x00010000 /*units??*/
-//#define IVTV_API_ASSIGN_PGM_INDEX_INFO 0x000000c7
-#define IVTV_API_ASSIGN_STREAM_TYPE 0x000000b9
-#define IVTV_API_ASSIGN_OUTPUT_PORT 0x000000bb
-#define IVTV_API_ASSIGN_FRAMERATE 0x0000008f
-#define IVTV_API_ASSIGN_FRAME_SIZE 0x00000091
-#define IVTV_API_ASSIGN_ASPECT_RATIO 0x00000099
-#define IVTV_API_ASSIGN_BITRATES 0x00000095
-#define IVTV_API_ASSIGN_GOP_PROPERTIES 0x00000097
-#define IVTV_API_ASSIGN_3_2_PULLDOWN 0x000000b1
-#define IVTV_API_ASSIGN_GOP_CLOSURE 0x000000c5
-#define IVTV_API_ASSIGN_AUDIO_PROPERTIES 0x000000bd
-#define IVTV_API_ASSIGN_DNR_FILTER_MODE 0x0000009b
-#define IVTV_API_ASSIGN_DNR_FILTER_PROPS 0x0000009d
-#define IVTV_API_ASSIGN_CORING_LEVELS 0x0000009f
-#define IVTV_API_ASSIGN_SPATIAL_FILTER_TYPE 0x000000a1
-#define IVTV_API_ASSIGN_FRAME_DROP_RATE 0x000000d0
-#define IVTV_API_ASSIGN_PLACEHOLDER 0x000000d8
-#define IVTV_API_MUTE_VIDEO 0x000000d9
-#define IVTV_API_MUTE_AUDIO 0x000000da
-#define IVTV_API_INITIALIZE_INPUT 0x000000cd
-#define IVTV_API_REFRESH_INPUT 0x000000d3
-#define IVTV_API_ASSIGN_NUM_VSYNC_LINES 0x000000d6
-#define IVTV_API_BEGIN_CAPTURE 0x00000081
-//#define IVTV_API_PAUSE_ENCODER 0x000000d2
-//#define IVTV_API_EVENT_NOTIFICATION 0x000000d5
-#define IVTV_API_END_CAPTURE 0x00000082
+/* Firmware API commands */
+#define IVTV_API_STD_TIMEOUT 500
+
+enum blackbird_capture_type {
+       BLACKBIRD_MPEG_CAPTURE,
+       BLACKBIRD_RAW_CAPTURE,
+       BLACKBIRD_RAW_PASSTHRU_CAPTURE
+};
+enum blackbird_capture_bits {
+       BLACKBIRD_RAW_BITS_NONE             = 0x00,
+       BLACKBIRD_RAW_BITS_YUV_CAPTURE      = 0x01,
+       BLACKBIRD_RAW_BITS_PCM_CAPTURE      = 0x02,
+       BLACKBIRD_RAW_BITS_VBI_CAPTURE      = 0x04,
+       BLACKBIRD_RAW_BITS_PASSTHRU_CAPTURE = 0x08,
+       BLACKBIRD_RAW_BITS_TO_HOST_CAPTURE  = 0x10
+};
+enum blackbird_capture_end {
+       BLACKBIRD_END_AT_GOP, /* stop at the end of gop, generate irq */
+       BLACKBIRD_END_NOW, /* stop immediately, no irq */
+};
+enum blackbird_framerate {
+       BLACKBIRD_FRAMERATE_NTSC_30, /* NTSC: 30fps */
+       BLACKBIRD_FRAMERATE_PAL_25   /* PAL: 25fps */
+};
+enum blackbird_stream_port {
+       BLACKBIRD_OUTPUT_PORT_MEMORY,
+       BLACKBIRD_OUTPUT_PORT_STREAMING,
+       BLACKBIRD_OUTPUT_PORT_SERIAL
+};
+enum blackbird_data_xfer_status {
+       BLACKBIRD_MORE_BUFFERS_FOLLOW,
+       BLACKBIRD_LAST_BUFFER,
+};
+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,
+};
+enum blackbird_vbi_mode_bits {
+       BLACKBIRD_VBI_BITS_SLICED,
+       BLACKBIRD_VBI_BITS_RAW,
+};
+enum blackbird_vbi_insertion_bits {
+       BLACKBIRD_VBI_BITS_INSERT_IN_XTENSION_USR_DATA,
+       BLACKBIRD_VBI_BITS_INSERT_IN_PRIVATE_PACKETS = 0x1 << 1,
+       BLACKBIRD_VBI_BITS_SEPARATE_STREAM = 0x2 << 1,
+       BLACKBIRD_VBI_BITS_SEPARATE_STREAM_USR_DATA = 0x4 << 1,
+       BLACKBIRD_VBI_BITS_SEPARATE_STREAM_PRV_DATA = 0x5 << 1,
+};
+enum blackbird_dma_unit {
+       BLACKBIRD_DMA_BYTES,
+       BLACKBIRD_DMA_FRAMES,
+};
+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,
+};
+enum blackbird_pause {
+       BLACKBIRD_PAUSE_ENCODING,
+       BLACKBIRD_RESUME_ENCODING,
+};
+enum blackbird_copyright {
+       BLACKBIRD_COPYRIGHT_OFF,
+       BLACKBIRD_COPYRIGHT_ON,
+};
+enum blackbird_notification_type {
+       BLACKBIRD_NOTIFICATION_REFRESH,
+};
+enum blackbird_notification_status {
+       BLACKBIRD_NOTIFICATION_OFF,
+       BLACKBIRD_NOTIFICATION_ON,
+};
+enum blackbird_notification_mailbox {
+       BLACKBIRD_NOTIFICATION_NO_MAILBOX = -1,
+};
+enum blackbird_field1_lines {
+       BLACKBIRD_FIELD1_SAA7114 = 0x00EF, /* 239 */
+       BLACKBIRD_FIELD1_SAA7115 = 0x00F0, /* 240 */
+       BLACKBIRD_FIELD1_MICRONAS = 0x0105, /* 261 */
+};
+enum blackbird_field2_lines {
+       BLACKBIRD_FIELD2_SAA7114 = 0x00EF, /* 239 */
+       BLACKBIRD_FIELD2_SAA7115 = 0x00F0, /* 240 */
+       BLACKBIRD_FIELD2_MICRONAS = 0x0106, /* 262 */
+};
+enum blackbird_custom_data_type {
+       BLACKBIRD_CUSTOM_EXTENSION_USR_DATA,
+       BLACKBIRD_CUSTOM_PRIVATE_PACKET,
+};
+enum blackbird_mute {
+       BLACKBIRD_UNMUTE,
+       BLACKBIRD_MUTE,
+};
+enum blackbird_mute_video_mask {
+       BLACKBIRD_MUTE_VIDEO_V_MASK = 0x0000FF00,
+       BLACKBIRD_MUTE_VIDEO_U_MASK = 0x00FF0000,
+       BLACKBIRD_MUTE_VIDEO_Y_MASK = 0xFF000000,
+};
+enum blackbird_mute_video_shift {
+       BLACKBIRD_MUTE_VIDEO_V_SHIFT = 8,
+       BLACKBIRD_MUTE_VIDEO_U_SHIFT = 16,
+       BLACKBIRD_MUTE_VIDEO_Y_SHIFT = 24,
+};
 
 /* Registers */
 #define IVTV_REG_ENC_SDRAM_REFRESH (0x07F8 /*| IVTV_REG_OFFSET*/)
@@ -170,7 +244,7 @@ static int memory_write(struct cx88_core *core, u32 address, u32 value)
 
 static int memory_read(struct cx88_core *core, u32 address, u32 *value)
 {
-        int retval;
+       int retval;
        u32 val;
 
        /* Warning: address is dword address (4 bytes) */
@@ -207,10 +281,6 @@ static int register_write(struct cx88_core *core, u32 address, u32 value)
        cx_read(P1_RADDR0);
 
        return wait_ready_gpio0_bit1(core,1);
-#if 0
-       udelay(1000); /* without this, things don't go right (subsequent memory_write()'s don't get through */
-       /* ? would this be safe here? set_current_state(TASK_INTERRUPTIBLE); schedule_timeout(1); */
-#endif
 }
 
 
@@ -236,15 +306,12 @@ 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, int 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);
 
@@ -268,12 +335,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 */
@@ -283,7 +349,7 @@ static int blackbird_api_cmd(struct cx8802_dev *dev, u32 command,
        timeout = jiffies + msecs_to_jiffies(10);
        for (;;) {
                memory_read(dev->core, dev->mailbox, &flag);
-               if (0 == (flag & 4))
+               if (0 != (flag & 4))
                        break;
                if (time_after(jiffies,timeout)) {
                        dprintk(0, "ERROR: API Mailbox timeout\n");
@@ -293,12 +359,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);
@@ -307,7 +371,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)
 {
@@ -316,7 +402,7 @@ static int blackbird_find_mailbox(struct cx8802_dev *dev)
        u32 value;
        int i;
 
-       for (i = 0; i < BLACKBIRD_FIRM_IMAGE_SIZE; i++) {
+       for (i = 0; i < dev->fw_size; i++) {
                memory_read(dev->core, i, &value);
                if (value == signature[signaturecnt])
                        signaturecnt++;
@@ -324,7 +410,7 @@ static int blackbird_find_mailbox(struct cx8802_dev *dev)
                        signaturecnt = 0;
                if (4 == signaturecnt) {
                        dprintk(1, "Mailbox signature found\n");
-                       return i;
+                       return i+1;
                }
        }
        dprintk(0, "Mailbox signature values not found!\n");
@@ -343,33 +429,40 @@ static int blackbird_load_firmware(struct cx8802_dev *dev)
        u32 *dataptr;
 
        retval  = register_write(dev->core, IVTV_REG_VPU, 0xFFFFFFED);
-        retval |= register_write(dev->core, IVTV_REG_HW_BLOCKS, IVTV_CMD_HW_BLOCKS_RST);
-        retval |= register_write(dev->core, IVTV_REG_ENC_SDRAM_REFRESH, 0x80000640);
-        retval |= register_write(dev->core, IVTV_REG_ENC_SDRAM_PRECHARGE, 0x1A);
+       retval |= register_write(dev->core, IVTV_REG_HW_BLOCKS, IVTV_CMD_HW_BLOCKS_RST);
+       retval |= register_write(dev->core, IVTV_REG_ENC_SDRAM_REFRESH, 0x80000640);
+       retval |= register_write(dev->core, IVTV_REG_ENC_SDRAM_PRECHARGE, 0x1A);
        msleep(1);
-        retval |= register_write(dev->core, IVTV_REG_APU, 0);
+       retval |= register_write(dev->core, IVTV_REG_APU, 0);
 
        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;
        }
 
-       if (firmware->size != BLACKBIRD_FIRM_IMAGE_SIZE) {
-               dprintk(0, "ERROR: Firmware size mismatch (have %zd, expected %d)\n",
-                       firmware->size, BLACKBIRD_FIRM_IMAGE_SIZE);
+       if ((firmware->size != BLACKBIRD_FIRM_IMAGE_SIZE) &&
+           (firmware->size != OLD_BLACKBIRD_FIRM_IMAGE_SIZE)) {
+               dprintk(0, "ERROR: Firmware size mismatch (have %zd, expected %d or %d)\n",
+                       firmware->size, BLACKBIRD_FIRM_IMAGE_SIZE,
+                       OLD_BLACKBIRD_FIRM_IMAGE_SIZE);
+               release_firmware(firmware);
                return -1;
        }
+       dev->fw_size = firmware->size;
 
        if (0 != memcmp(firmware->data, magic, 8)) {
                dprintk(0, "ERROR: Firmware magic mismatch, wrong file?\n");
+               release_firmware(firmware);
                return -1;
        }
 
@@ -390,87 +483,90 @@ 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);
        dprintk(0, "Firmware upload successful.\n");
 
-        retval |= register_write(dev->core, IVTV_REG_HW_BLOCKS, IVTV_CMD_HW_BLOCKS_RST);
-        retval |= register_read(dev->core, IVTV_REG_SPU, &value);
-        retval |= register_write(dev->core, IVTV_REG_SPU, value & 0xFFFFFFFE);
+       retval |= register_write(dev->core, IVTV_REG_HW_BLOCKS, IVTV_CMD_HW_BLOCKS_RST);
+       retval |= register_read(dev->core, IVTV_REG_SPU, &value);
+       retval |= register_write(dev->core, IVTV_REG_SPU, value & 0xFFFFFFFE);
        msleep(1);
 
        retval |= register_read(dev->core, IVTV_REG_VPU, &value);
-        retval |= register_write(dev->core, IVTV_REG_VPU, value & 0xFFFFFFE8);
+       retval |= register_write(dev->core, IVTV_REG_VPU, value & 0xFFFFFFE8);
 
        if (retval < 0)
                dprintk(0, "Error with register_write\n");
        return 0;
 }
 
+/**
+ Settings used by the windows tv app for PVR2000:
+=================================================================================================================
+Profile | Codec | Resolution | CBR/VBR | Video Qlty   | V. Bitrate | Frmrate | Audio Codec | A. Bitrate | A. Mode
+-----------------------------------------------------------------------------------------------------------------
+MPEG-1  | MPEG1 | 352x288PAL | (CBR)   | 1000:Optimal | 2000 Kbps  | 25fps   | MPG1 Layer2 | 224kbps    | Stereo
+MPEG-2  | MPEG2 | 720x576PAL | VBR     | 600 :Good    | 4000 Kbps  | 25fps   | MPG1 Layer2 | 224kbps    | Stereo
+VCD     | MPEG1 | 352x288PAL | (CBR)   | 1000:Optimal | 1150 Kbps  | 25fps   | MPG1 Layer2 | 224kbps    | Stereo
+DVD     | MPEG2 | 720x576PAL | VBR     | 600 :Good    | 6000 Kbps  | 25fps   | MPG1 Layer2 | 224kbps    | Stereo
+DB* DVD | MPEG2 | 720x576PAL | CBR     | 600 :Good    | 6000 Kbps  | 25fps   | MPG1 Layer2 | 224kbps    | Stereo
+=================================================================================================================
+*DB: "DirectBurn"
+*/
+
 static void blackbird_codec_settings(struct cx8802_dev *dev)
 {
-       int bitrate_mode = 1;
-       int bitrate = 7500000;
-       int bitrate_peak = 7500000;
-
-       /* assign stream type */
-        blackbird_api_cmd(dev, IVTV_API_ASSIGN_STREAM_TYPE, 1, 0, 0); /* program stream */
-        //blackbird_api_cmd(dev, IVTV_API_ASSIGN_STREAM_TYPE, 1, 0, 2); /* MPEG1 stream */
-        //blackbird_api_cmd(dev, IVTV_API_ASSIGN_STREAM_TYPE, 1, 0, 3); /* PES A/V */
-        //blackbird_api_cmd(dev, IVTV_API_ASSIGN_STREAM_TYPE, 1, 0, 10); /* DVD stream */
-
-        /* assign output port */
-        blackbird_api_cmd(dev, IVTV_API_ASSIGN_OUTPUT_PORT, 1, 0, 1); /* 1 = Host */
+       /* assign frame size */
+       blackbird_api_cmd(dev, CX2341X_ENC_SET_FRAME_SIZE, 2, 0,
+                               dev->height, dev->width);
 
-        /* assign framerate */
-        blackbird_api_cmd(dev, IVTV_API_ASSIGN_FRAMERATE, 1, 0, 0);
+       dev->params.width = dev->width;
+       dev->params.height = dev->height;
+       dev->params.is_50hz = (dev->core->tvnorm->id & V4L2_STD_625_50) != 0;
 
-        /* assign frame size */
-        blackbird_api_cmd(dev, IVTV_API_ASSIGN_FRAME_SIZE, 2, 0, 480, 720);
-
-        /* assign aspect ratio */
-        blackbird_api_cmd(dev, IVTV_API_ASSIGN_ASPECT_RATIO, 1, 0, 2);
-
-        /* assign bitrates */
-        blackbird_api_cmd(dev, IVTV_API_ASSIGN_BITRATES, 5, 0,
-                        bitrate_mode,         /* mode */
-                        bitrate,              /* bps */
-                        bitrate_peak / 400,   /* peak/400 */
-                        0, 0x70);             /* encoding buffer, ckennedy */
-
-        /* assign gop properties */
-        blackbird_api_cmd(dev, IVTV_API_ASSIGN_GOP_PROPERTIES, 2, 0, 15, 3);
-        //blackbird_api_cmd(dev, IVTV_API_ASSIGN_GOP_PROPERTIES, 2, 0, 2, 1);
-
-        /* assign 3 2 pulldown */
-        blackbird_api_cmd(dev, IVTV_API_ASSIGN_3_2_PULLDOWN, 1, 0, 0);
-
-        /* note: it's not necessary to set the samplerate, the mpeg encoder seems to autodetect/adjust */
-       blackbird_api_cmd(dev, IVTV_API_ASSIGN_AUDIO_PROPERTIES, 1, 0, (2<<2) | (8<<4));
-
-       /* assign gop closure */
-        blackbird_api_cmd(dev, IVTV_API_ASSIGN_GOP_CLOSURE, 1, 0, 0);
-
-        /* assign audio properties */
-        blackbird_api_cmd(dev, IVTV_API_ASSIGN_AUDIO_PROPERTIES, 1, 0, 0 | (2 << 2) | (14 << 4));
-
-        /* assign dnr filter mode */
-        blackbird_api_cmd(dev, IVTV_API_ASSIGN_DNR_FILTER_MODE, 2, 0, 0, 0);
-
-        /* assign dnr filter props*/
-        blackbird_api_cmd(dev, IVTV_API_ASSIGN_DNR_FILTER_PROPS, 2, 0, 0, 0);
-
-        /* assign coring levels (luma_h, luma_l, chroma_h, chroma_l) */
-        blackbird_api_cmd(dev, IVTV_API_ASSIGN_CORING_LEVELS, 4, 0, 0, 255, 0, 255);
-
-       /* assign spatial filter type: luma_t: 1 = horiz_only, chroma_t: 1 = horiz_only */
-        blackbird_api_cmd(dev, IVTV_API_ASSIGN_SPATIAL_FILTER_TYPE, 2, 0, 1, 1);
-
-        /* assign frame drop rate */
-        blackbird_api_cmd(dev, IVTV_API_ASSIGN_FRAME_DROP_RATE, 1, 0, 0);
+       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;
@@ -478,7 +574,7 @@ static int blackbird_initialize_codec(struct cx8802_dev *dev)
        int retval;
 
        dprintk(1,"Initialize codec\n");
-       retval = blackbird_api_cmd(dev, IVTV_API_ENC_PING_FW, 0, 0); /* ping */
+       retval = blackbird_api_cmd(dev, CX2341X_ENC_PING_FW, 0, 0); /* ping */
        if (retval < 0) {
                /* ping was not successful, reset and upload firmware */
                cx_write(MO_SRST_IO, 0); /* SYS_RSTO=0 */
@@ -493,13 +589,13 @@ static int blackbird_initialize_codec(struct cx8802_dev *dev)
                if (dev->mailbox < 0)
                        return -1;
 
-               retval = blackbird_api_cmd(dev, IVTV_API_ENC_PING_FW, 0, 0); /* ping */
+               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, IVTV_API_ENC_GETVER, 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;
@@ -513,31 +609,41 @@ static int blackbird_initialize_codec(struct cx8802_dev *dev)
        cx_write(MO_VBOS_CONTROL, 0x84A00); /* no 656 mode, 8-bit pixels, disable VBI */
        cx_clear(MO_OUTPUT_FORMAT, 0x0008); /* Normal Y-limits to let the mpeg encoder sync */
 
-#if 0 /* FIXME */
-       set_scale(dev, 720, 480, V4L2_FIELD_INTERLACED);
-#endif
        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, IVTV_API_ASSIGN_PLACEHOLDER, 12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
+       /* 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);
 
-       blackbird_api_cmd(dev, IVTV_API_INITIALIZE_INPUT, 0, 0); /* initialize the video input */
+       /* initialize the video input */
+       blackbird_api_cmd(dev, CX2341X_ENC_INITIALIZE_INPUT, 0, 0);
 
        msleep(1);
 
-        blackbird_api_cmd(dev, IVTV_API_MUTE_VIDEO, 1, 0, 0);
+       blackbird_api_cmd(dev, CX2341X_ENC_MUTE_VIDEO, 1, 0, BLACKBIRD_UNMUTE);
        msleep(1);
-        blackbird_api_cmd(dev, IVTV_API_MUTE_AUDIO, 1, 0, 0);
+       blackbird_api_cmd(dev, CX2341X_ENC_MUTE_AUDIO, 1, 0, BLACKBIRD_UNMUTE);
        msleep(1);
 
-       blackbird_api_cmd(dev, IVTV_API_BEGIN_CAPTURE, 2, 0, 0, 0x13); /* start capturing to the host interface */
-       //blackbird_api_cmd(dev, IVTV_API_BEGIN_CAPTURE, 2, 0, 0, 0); /* start capturing to the host interface */
-       msleep(1);
+       /* 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, IVTV_API_REFRESH_INPUT, 0,0);
+       blackbird_api_cmd(dev, CX2341X_ENC_REFRESH_INPUT, 0,0);
        return 0;
 }
 
@@ -548,16 +654,11 @@ static int bb_buf_setup(struct videobuf_queue *q,
 {
        struct cx8802_fh *fh = q->priv_data;
 
-       fh->dev->ts_packet_size  = 512;
-       fh->dev->ts_packet_count = 100;
+       fh->dev->ts_packet_size  = 188 * 4; /* was: 512 */
+       fh->dev->ts_packet_count = mpegbufs; /* was: 100 */
 
        *size = fh->dev->ts_packet_size * fh->dev->ts_packet_count;
-       if (0 == *count)
-               *count = mpegbufs;
-       if (*count < 2)
-               *count = 2;
-       if (*count > 32)
-               *count = 32;
+       *count = fh->dev->ts_packet_count;
        return 0;
 }
 
@@ -566,7 +667,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);
+       return cx8802_buf_prepare(q, fh->dev, (struct cx88_buffer*)vb, field);
 }
 
 static void
@@ -579,8 +680,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 = {
@@ -592,17 +692,72 @@ static struct videobuf_queue_ops blackbird_qops = {
 
 /* ------------------------------------------------------------------ */
 
+static const u32 *ctrl_classes[] = {
+       cx88_user_ctrls,
+       cx2341x_mpeg_ctrls,
+       NULL
+};
+
+static int blackbird_queryctrl(struct cx8802_dev *dev, struct v4l2_queryctrl *qctrl)
+{
+       qctrl->id = v4l2_ctrl_next(ctrl_classes, qctrl->id);
+       if (qctrl->id == 0)
+               return -EINVAL;
+
+       /* Standard V4L2 controls */
+       if (cx8800_ctrl_query(qctrl) == 0)
+               return 0;
+
+       /* MPEG V4L2 controls */
+       if (cx2341x_ctrl_query(&dev->params, qctrl))
+               qctrl->flags |= V4L2_CTRL_FLAG_DISABLED;
+       return 0;
+}
+
+static int blackbird_querymenu(struct cx8802_dev *dev, struct v4l2_querymenu *qmenu)
+{
+       struct v4l2_queryctrl qctrl;
+
+       qctrl.id = qmenu->id;
+       blackbird_queryctrl(dev, &qctrl);
+       return v4l2_ctrl_query_menu(qmenu, &qctrl, cx2341x_ctrl_get_menu(qmenu->id));
+}
+
+/* ------------------------------------------------------------------ */
+
 static int mpeg_do_ioctl(struct inode *inode, struct file *file,
                         unsigned int cmd, void *arg)
 {
        struct cx8802_fh  *fh  = file->private_data;
        struct cx8802_dev *dev = fh->dev;
+       struct cx88_core  *core = dev->core;
 
        if (debug > 1)
-               cx88_print_ioctl(dev->core->name,cmd);
+               v4l_print_ioctl(core->name,cmd);
 
        switch (cmd) {
 
+       /* --- capabilities ------------------------------------------ */
+       case VIDIOC_QUERYCAP:
+       {
+               struct v4l2_capability *cap = arg;
+
+               memset(cap,0,sizeof(*cap));
+               strcpy(cap->driver, "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;
+
+               return 0;
+       }
+
        /* --- capture ioctls ---------------------------------------- */
        case VIDIOC_ENUM_FMT:
        {
@@ -615,24 +770,53 @@ static int mpeg_do_ioctl(struct inode *inode, struct file *file,
 
                memset(f,0,sizeof(*f));
                f->index = index;
-               strlcpy(f->description, "MPEG TS", sizeof(f->description));
+               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:
-       case VIDIOC_S_FMT:
-       case VIDIOC_TRY_FMT:
        {
-               /* FIXME -- quick'n'dirty for exactly one size ... */
                struct v4l2_format *f = arg;
 
                memset(f,0,sizeof(*f));
                f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
-               f->fmt.pix.width        = 720;
-               f->fmt.pix.height       = 576;
                f->fmt.pix.pixelformat  = V4L2_PIX_FMT_MPEG;
-               f->fmt.pix.sizeimage    = 1024 * 512 /* FIXME: BUFFER_SIZE */;
+               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;
        }
 
        /* --- streaming capture ------------------------------------- */
@@ -655,68 +839,194 @@ static int mpeg_do_ioctl(struct inode *inode, struct file *file,
        case VIDIOC_STREAMOFF:
                return videobuf_streamoff(&fh->mpegq);
 
+       /* --- mpeg compression -------------------------------------- */
+       case VIDIOC_G_MPEGCOMP:
+       {
+               struct v4l2_mpeg_compression *f = arg;
+
+               printk(KERN_WARNING "VIDIOC_G_MPEGCOMP is obsolete. "
+                                   "Replace with VIDIOC_G_EXT_CTRLS!");
+               memcpy(f,&default_mpeg_params,sizeof(*f));
+               return 0;
+       }
+       case VIDIOC_S_MPEGCOMP:
+               printk(KERN_WARNING "VIDIOC_S_MPEGCOMP is obsolete. "
+                                   "Replace with VIDIOC_S_EXT_CTRLS!");
+               return 0;
+       case VIDIOC_G_EXT_CTRLS:
+       {
+               struct v4l2_ext_controls *f = arg;
+
+               if (f->ctrl_class != V4L2_CTRL_CLASS_MPEG)
+                       return -EINVAL;
+               return cx2341x_ext_ctrls(&dev->params, f, cmd);
+       }
+       case VIDIOC_S_EXT_CTRLS:
+       case VIDIOC_TRY_EXT_CTRLS:
+       {
+               struct v4l2_ext_controls *f = arg;
+               struct cx2341x_mpeg_params p;
+               int err;
+
+               if (f->ctrl_class != V4L2_CTRL_CLASS_MPEG)
+                       return -EINVAL;
+               p = dev->params;
+               err = cx2341x_ext_ctrls(&p, f, cmd);
+               if (err == 0 && cmd == VIDIOC_S_EXT_CTRLS) {
+                       err = cx2341x_update(dev, blackbird_mbox_func, &dev->params, &p);
+                       dev->params = p;
+               }
+               return err;
+       }
+       case VIDIOC_S_FREQUENCY:
+       {
+               blackbird_api_cmd(fh->dev, CX2341X_ENC_STOP_CAPTURE, 3, 0,
+                                 BLACKBIRD_END_NOW,
+                                 BLACKBIRD_MPEG_CAPTURE,
+                                 BLACKBIRD_RAW_BITS_NONE);
+
+               cx88_do_ioctl(inode, file, 0, dev->core, cmd, arg, cx88_ioctl_hook);
+
+               blackbird_initialize_codec(dev);
+               cx88_set_scale(dev->core, dev->width, dev->height,
+                              fh->mpegq.field);
+               return 0;
+       }
+       case VIDIOC_LOG_STATUS:
+       {
+               char name[32 + 2];
+
+               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);
+               cx2341x_log_status(&dev->params, name);
+               printk("%s/2: =============  END LOG STATUS  =============\n",
+                      core->name);
+               return 0;
+       }
+       case VIDIOC_QUERYMENU:
+               return blackbird_querymenu(dev, arg);
+       case VIDIOC_QUERYCTRL:
+       {
+               struct v4l2_queryctrl *c = arg;
+
+               if (blackbird_queryctrl(dev, c) == 0)
+                       return 0;
+               return cx88_do_ioctl(inode, file, 0, dev->core, cmd, arg, mpeg_do_ioctl);
+       }
+
        default:
-               return -EINVAL;
+               return cx88_do_ioctl(inode, file, 0, dev->core, cmd, arg, cx88_ioctl_hook);
        }
        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 unsigned int mpeg_translate_ioctl(unsigned int cmd)
+{
+       return cmd;
+}
+
 static int mpeg_ioctl(struct inode *inode, struct file *file,
-                      unsigned int cmd, unsigned long arg)
+                       unsigned int cmd, unsigned long arg)
 {
-       return video_usercopy(inode, file, cmd, arg, mpeg_do_ioctl);
+       cmd = cx88_ioctl_translator( cmd );
+       return video_usercopy(inode, file, cmd, arg, cx88_ioctl_hook);
 }
 
 static int mpeg_open(struct inode *inode, struct file *file)
 {
        int minor = iminor(inode);
-       struct cx8802_dev *h,*dev = NULL;
+       struct cx8802_dev *dev = NULL;
        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;
-       }
-       if (NULL == dev)
+       dev = cx8802_get_device(inode);
+
+       dprintk( 1, "%s\n", __FUNCTION__);
+
+       if (dev == NULL)
                return -ENODEV;
 
-       if (blackbird_initialize_codec(dev) < 0)
+       /* 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);
+                       return err;
+               }
+       }
+
+       if (blackbird_initialize_codec(dev) < 0) {
+               if (drv)
+                       drv->request_release(drv);
                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);
                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,
                            V4L2_BUF_TYPE_VIDEO_CAPTURE,
-                           V4L2_FIELD_TOP,
+                           V4L2_FIELD_INTERLACED,
                            sizeof(struct cx88_buffer),
                            fh);
+
+       /* FIXME: locking against other video device */
+       cx88_set_scale(dev->core, dev->width, dev->height,
+                       fh->mpegq.field);
+
        return 0;
 }
 
 static int mpeg_release(struct inode *inode, struct file *file)
 {
        struct cx8802_fh  *fh  = file->private_data;
+       struct cx8802_dev *dev = NULL;
+       struct cx8802_driver *drv = NULL;
 
-       blackbird_api_cmd(fh->dev, IVTV_API_END_CAPTURE, 3, 0, 1, 0, 0x13);
+       /* 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
+               );
 
+       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_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);
+
        return 0;
 }
 
@@ -768,6 +1078,44 @@ static struct video_device cx8802_mpeg_template =
 
 /* ------------------------------------------------------------------ */
 
+/* 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->board) {
+       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 */
+               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->board) {
+       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) {
@@ -798,81 +1146,64 @@ static int blackbird_register_video(struct cx8802_dev *dev)
 
 /* ----------------------------------------------------------- */
 
-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", __FUNCTION__);
+       dprintk( 1, " ->being probed by Card=%d Name=%s, PCI %02x:%02x\n",
+               core->board,
+               core->name,
+               core->pci_bus,
+               core->pci_slot);
 
        err = -ENODEV;
-       if (!cx88_boards[core->board].blackbird)
+       if (!(cx88_boards[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;
+       cx2341x_fill_defaults(&dev->params);
+       dev->params.port = CX2341X_PORT_STREAMING;
 
-       err = cx8802_init_common(dev);
-       if (0 != err)
-               goto fail_free;
+       if (core->tvnorm->id & V4L2_STD_525_60) {
+               dev->height = 480;
+       } else {
+               dev->height = 576;
+       }
 
        /* 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_register_video(dev);
+
+       /* initial device configuration: needed ? */
+
        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);
+       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)
@@ -885,20 +1216,26 @@ static int blackbird_init(void)
        printk(KERN_INFO "cx2388x: snapshot date %04d-%02d-%02d\n",
               SNAPSHOT/10000, (SNAPSHOT/100)%100, SNAPSHOT%100);
 #endif
-       return pci_module_init(&blackbird_pci_driver);
+       cx88_ioctl_hook = mpeg_do_ioctl;
+       cx88_ioctl_translator = mpeg_translate_ioctl;
+       return cx8802_register_driver(&cx8802_blackbird_driver);
 }
 
 static void 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);
+
 /* ----------------------------------------------------------- */
 /*
  * Local variables:
  * c-basic-offset: 8
  * End:
+ * kate: eol "unix"; indent-width 3; remove-trailing-space on; replace-trailing-space-save on; tab-width 8; replace-tabs off; space-indent off; mixed-indent off
  */