tree-wide: fix assorted typos all over the place
[safe/jmp/linux-2.6] / drivers / media / video / cx88 / cx88-core.c
index b18e7a3..b354111 100644 (file)
@@ -5,6 +5,11 @@
  *
  * (c) 2003 Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]
  *
+ * (c) 2005-2006 Mauro Carvalho Chehab <mchehab@infradead.org>
+ *     - Multituner support
+ *     - video_ioctl2 conversion
+ *     - PAL/M fixes
+ *
  *  This program is free software; you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
  *  the Free Software Foundation; either version 2 of the License, or
@@ -23,7 +28,6 @@
 #include <linux/init.h>
 #include <linux/list.h>
 #include <linux/module.h>
-#include <linux/moduleparam.h>
 #include <linux/kernel.h>
 #include <linux/slab.h>
 #include <linux/kmod.h>
 #include <linux/pci.h>
 #include <linux/delay.h>
 #include <linux/videodev2.h>
+#include <linux/mutex.h>
 
 #include "cx88.h"
+#include <media/v4l2-common.h>
+#include <media/v4l2-ioctl.h>
 
 MODULE_DESCRIPTION("v4l2 driver module for cx2388x based TV cards");
 MODULE_AUTHOR("Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]");
@@ -41,31 +48,15 @@ MODULE_LICENSE("GPL");
 
 /* ------------------------------------------------------------------ */
 
-static unsigned int core_debug = 0;
+static unsigned int core_debug;
 module_param(core_debug,int,0644);
 MODULE_PARM_DESC(core_debug,"enable debug messages [core]");
 
-static unsigned int latency = UNSET;
-module_param(latency,int,0444);
-MODULE_PARM_DESC(latency,"pci latency timer");
-
-static unsigned int tuner[] = {[0 ... (CX88_MAXBOARDS - 1)] = UNSET };
-static unsigned int radio[] = {[0 ... (CX88_MAXBOARDS - 1)] = UNSET };
-static unsigned int card[]  = {[0 ... (CX88_MAXBOARDS - 1)] = UNSET };
-
-module_param_array(tuner, int, NULL, 0444);
-module_param_array(radio, int, NULL, 0444);
-module_param_array(card,  int, NULL, 0444);
-
-MODULE_PARM_DESC(tuner,"tuner type");
-MODULE_PARM_DESC(radio,"radio tuner type");
-MODULE_PARM_DESC(card,"card type");
-
-static unsigned int nicam = 0;
+static unsigned int nicam;
 module_param(nicam,int,0644);
 MODULE_PARM_DESC(nicam,"tv audio is nicam");
 
-static unsigned int nocomb = 0;
+static unsigned int nocomb;
 module_param(nocomb,int,0644);
 MODULE_PARM_DESC(nocomb,"disable comb filter");
 
@@ -74,71 +65,19 @@ MODULE_PARM_DESC(nocomb,"disable comb filter");
 
 static unsigned int cx88_devcount;
 static LIST_HEAD(cx88_devlist);
-static DECLARE_MUTEX(devlist);
+static DEFINE_MUTEX(devlist);
 
-/* ------------------------------------------------------------------ */
-/* debug help functions                                               */
-
-static const char *v4l1_ioctls[] = {
-       "0", "CGAP", "GCHAN", "SCHAN", "GTUNER", "STUNER", "GPICT", "SPICT",
-       "CCAPTURE", "GWIN", "SWIN", "GFBUF", "SFBUF", "KEY", "GFREQ",
-       "SFREQ", "GAUDIO", "SAUDIO", "SYNC", "MCAPTURE", "GMBUF", "GUNIT",
-       "GCAPTURE", "SCAPTURE", "SPLAYMODE", "SWRITEMODE", "GPLAYINFO",
-       "SMICROCODE", "GVBIFMT", "SVBIFMT" };
-#define V4L1_IOCTLS ARRAY_SIZE(v4l1_ioctls)
-
-static const char *v4l2_ioctls[] = {
-       "QUERYCAP", "1", "ENUM_PIXFMT", "ENUM_FBUFFMT", "G_FMT", "S_FMT",
-       "G_COMP", "S_COMP", "REQBUFS", "QUERYBUF", "G_FBUF", "S_FBUF",
-       "G_WIN", "S_WIN", "PREVIEW", "QBUF", "16", "DQBUF", "STREAMON",
-       "STREAMOFF", "G_PERF", "G_PARM", "S_PARM", "G_STD", "S_STD",
-       "ENUMSTD", "ENUMINPUT", "G_CTRL", "S_CTRL", "G_TUNER", "S_TUNER",
-       "G_FREQ", "S_FREQ", "G_AUDIO", "S_AUDIO", "35", "QUERYCTRL",
-       "QUERYMENU", "G_INPUT", "S_INPUT", "ENUMCVT", "41", "42", "43",
-       "44", "45",  "G_OUTPUT", "S_OUTPUT", "ENUMOUTPUT", "G_AUDOUT",
-       "S_AUDOUT", "ENUMFX", "G_EFFECT", "S_EFFECT", "G_MODULATOR",
-       "S_MODULATOR"
-};
-#define V4L2_IOCTLS ARRAY_SIZE(v4l2_ioctls)
-
-void cx88_print_ioctl(char *name, unsigned int cmd)
-{
-       char *dir;
-
-       switch (_IOC_DIR(cmd)) {
-       case _IOC_NONE:              dir = "--"; break;
-       case _IOC_READ:              dir = "r-"; break;
-       case _IOC_WRITE:             dir = "-w"; break;
-       case _IOC_READ | _IOC_WRITE: dir = "rw"; break;
-       default:                     dir = "??"; break;
-       }
-       switch (_IOC_TYPE(cmd)) {
-       case 'v':
-               printk(KERN_DEBUG "%s: ioctl 0x%08x (v4l1, %s, VIDIOC%s)\n",
-                      name, cmd, dir, (_IOC_NR(cmd) < V4L1_IOCTLS) ?
-                      v4l1_ioctls[_IOC_NR(cmd)] : "???");
-               break;
-       case 'V':
-               printk(KERN_DEBUG "%s: ioctl 0x%08x (v4l2, %s, VIDIOC_%s)\n",
-                      name, cmd, dir, (_IOC_NR(cmd) < V4L2_IOCTLS) ?
-                      v4l2_ioctls[_IOC_NR(cmd)] : "???");
-               break;
-       default:
-               printk(KERN_DEBUG "%s: ioctl 0x%08x (???, %s, #%d)\n",
-                      name, cmd, dir, _IOC_NR(cmd));
-       }
-}
-
-/* ------------------------------------------------------------------ */
 #define NO_SYNC_LINE (-1U)
 
-static u32* cx88_risc_field(u32 *rp, struct scatterlist *sglist,
+/* @lpi: lines per IRQ, or 0 to not generate irqs. Note: IRQ to be
+        generated _after_ lpi lines are transferred. */
+static __le32* cx88_risc_field(__le32 *rp, struct scatterlist *sglist,
                            unsigned int offset, u32 sync_line,
                            unsigned int bpl, unsigned int padding,
-                           unsigned int lines)
+                           unsigned int lines, unsigned int lpi)
 {
        struct scatterlist *sg;
-       unsigned int line,todo;
+       unsigned int line,todo,sol;
 
        /* sync instruction */
        if (sync_line != NO_SYNC_LINE)
@@ -151,15 +90,19 @@ static u32* cx88_risc_field(u32 *rp, struct scatterlist *sglist,
                        offset -= sg_dma_len(sg);
                        sg++;
                }
+               if (lpi && line>0 && !(line % lpi))
+                       sol = RISC_SOL | RISC_IRQ1 | RISC_CNT_INC;
+               else
+                       sol = RISC_SOL;
                if (bpl <= sg_dma_len(sg)-offset) {
                        /* fits into current chunk */
-                       *(rp++)=cpu_to_le32(RISC_WRITE|RISC_SOL|RISC_EOL|bpl);
+                       *(rp++)=cpu_to_le32(RISC_WRITE|sol|RISC_EOL|bpl);
                        *(rp++)=cpu_to_le32(sg_dma_address(sg)+offset);
                        offset+=bpl;
                } else {
-                       /* scanline needs to be splitted */
+                       /* scanline needs to be split */
                        todo = bpl;
-                       *(rp++)=cpu_to_le32(RISC_WRITE|RISC_SOL|
+                       *(rp++)=cpu_to_le32(RISC_WRITE|sol|
                                            (sg_dma_len(sg)-offset));
                        *(rp++)=cpu_to_le32(sg_dma_address(sg)+offset);
                        todo -= (sg_dma_len(sg)-offset);
@@ -188,7 +131,7 @@ int cx88_risc_buffer(struct pci_dev *pci, struct btcx_riscmem *risc,
                     unsigned int bpl, unsigned int padding, unsigned int lines)
 {
        u32 instructions,fields;
-       u32 *rp;
+       __le32 *rp;
        int rc;
 
        fields = 0;
@@ -198,9 +141,11 @@ int cx88_risc_buffer(struct pci_dev *pci, struct btcx_riscmem *risc,
                fields++;
 
        /* estimate risc mem: worst case is one write per page border +
-          one write per scan line + syncs + jump (all 2 dwords) */
-       instructions  = (bpl * lines * fields) / PAGE_SIZE + lines * fields;
-       instructions += 3 + 4;
+          one write per scan line + syncs + jump (all 2 dwords).  Padding
+          can cause next bpl to start close to a page border.  First DMA
+          region may be smaller than PAGE_SIZE */
+       instructions  = fields * (1 + ((bpl + padding) * lines) / PAGE_SIZE + lines);
+       instructions += 2;
        if ((rc = btcx_riscmem_alloc(pci,risc,instructions*8)) < 0)
                return rc;
 
@@ -208,46 +153,48 @@ int cx88_risc_buffer(struct pci_dev *pci, struct btcx_riscmem *risc,
        rp = risc->cpu;
        if (UNSET != top_offset)
                rp = cx88_risc_field(rp, sglist, top_offset, 0,
-                                    bpl, padding, lines);
+                                    bpl, padding, lines, 0);
        if (UNSET != bottom_offset)
                rp = cx88_risc_field(rp, sglist, bottom_offset, 0x200,
-                                    bpl, padding, lines);
+                                    bpl, padding, lines, 0);
 
        /* save pointer to jmp instruction address */
        risc->jmp = rp;
-       BUG_ON((risc->jmp - risc->cpu + 2) / 4 > risc->size);
+       BUG_ON((risc->jmp - risc->cpu + 2) * sizeof (*risc->cpu) > risc->size);
        return 0;
 }
 
 int cx88_risc_databuffer(struct pci_dev *pci, struct btcx_riscmem *risc,
                         struct scatterlist *sglist, unsigned int bpl,
-                        unsigned int lines)
+                        unsigned int lines, unsigned int lpi)
 {
        u32 instructions;
-       u32 *rp;
+       __le32 *rp;
        int rc;
 
        /* estimate risc mem: worst case is one write per page border +
-          one write per scan line + syncs + jump (all 2 dwords) */
-       instructions  = (bpl * lines) / PAGE_SIZE + lines;
-       instructions += 3 + 4;
+          one write per scan line + syncs + jump (all 2 dwords).  Here
+          there is no padding and no sync.  First DMA region may be smaller
+          than PAGE_SIZE */
+       instructions  = 1 + (bpl * lines) / PAGE_SIZE + lines;
+       instructions += 1;
        if ((rc = btcx_riscmem_alloc(pci,risc,instructions*8)) < 0)
                return rc;
 
        /* write risc instructions */
        rp = risc->cpu;
-       rp = cx88_risc_field(rp, sglist, 0, NO_SYNC_LINE, bpl, 0, lines);
+       rp = cx88_risc_field(rp, sglist, 0, NO_SYNC_LINE, bpl, 0, lines, lpi);
 
        /* save pointer to jmp instruction address */
        risc->jmp = rp;
-       BUG_ON((risc->jmp - risc->cpu + 2) / 4 > risc->size);
+       BUG_ON((risc->jmp - risc->cpu + 2) * sizeof (*risc->cpu) > risc->size);
        return 0;
 }
 
 int cx88_risc_stopper(struct pci_dev *pci, struct btcx_riscmem *risc,
                      u32 reg, u32 mask, u32 value)
 {
-       u32 *rp;
+       __le32 *rp;
        int rc;
 
        if ((rc = btcx_riscmem_alloc(pci, risc, 4*16)) < 0)
@@ -265,15 +212,16 @@ int cx88_risc_stopper(struct pci_dev *pci, struct btcx_riscmem *risc,
 }
 
 void
-cx88_free_buffer(struct pci_dev *pci, struct cx88_buffer *buf)
+cx88_free_buffer(struct videobuf_queue *q, struct cx88_buffer *buf)
 {
-       if (in_interrupt())
-               BUG();
+       struct videobuf_dmabuf *dma=videobuf_to_dma(&buf->vb);
+
+       BUG_ON(in_interrupt());
        videobuf_waiton(&buf->vb,0,0);
-       videobuf_dma_pci_unmap(pci, &buf->vb.dma);
-       videobuf_dma_free(&buf->vb.dma);
-       btcx_riscmem_free(pci, &buf->risc);
-       buf->vb.state = STATE_NEEDS_INIT;
+       videobuf_dma_unmap(q, dma);
+       videobuf_dma_free(dma);
+       btcx_riscmem_free(to_pci_dev(q->dev), &buf->risc);
+       buf->vb.state = VIDEOBUF_NEEDS_INIT;
 }
 
 /* ------------------------------------------------------------------ */
@@ -283,7 +231,7 @@ cx88_free_buffer(struct pci_dev *pci, struct cx88_buffer *buf)
  * can use the whole SDRAM for the DMA fifos.  To simplify things, we
  * use a static memory layout.  That surely will waste memory in case
  * we don't use all DMA channels at the same time (which will be the
- * case most of the time).  But that still gives us enougth FIFO space
+ * case most of the time).  But that still gives us enough FIFO space
  * to be able to deal with insane long pci latencies ...
  *
  * FIFO space allocations:
@@ -291,9 +239,10 @@ cx88_free_buffer(struct pci_dev *pci, struct cx88_buffer *buf)
  *    channel  22    (u video)  -  2.0k
  *    channel  23    (v video)  -  2.0k
  *    channel  24    (vbi)      -  4.0k
- *    channels 25+26 (audio)    -  0.5k
+ *    channels 25+26 (audio)    -  4.0k
  *    channel  28    (mpeg)     -  4.0k
- *    TOTAL                     = 25.5k
+ *    channel  27    (audio rds)-  3.0k
+ *    TOTAL                     = 29.0k
  *
  * Every channel has 160 bytes control data (64 bytes instruction
  * queue and 6 CDT entries), which is close to 2k total.
@@ -359,7 +308,7 @@ struct sram_channel cx88_sram_channels[] = {
                .ctrl_start = 0x180680,
                .cdt        = 0x180680 + 64,
                .fifo_start = 0x185400,
-               .fifo_size  = 0x000200,
+               .fifo_size  = 0x001000,
                .ptr1_reg   = MO_DMA25_PTR1,
                .ptr2_reg   = MO_DMA25_PTR2,
                .cnt1_reg   = MO_DMA25_CNT1,
@@ -371,7 +320,7 @@ struct sram_channel cx88_sram_channels[] = {
                .ctrl_start = 0x180720,
                .cdt        = 0x180680 + 64,  /* same as audio IN */
                .fifo_start = 0x185400,       /* same as audio IN */
-               .fifo_size  = 0x000200,       /* same as audio IN */
+               .fifo_size  = 0x001000,       /* same as audio IN */
                .ptr1_reg   = MO_DMA26_PTR1,
                .ptr2_reg   = MO_DMA26_PTR2,
                .cnt1_reg   = MO_DMA26_CNT1,
@@ -382,13 +331,25 @@ struct sram_channel cx88_sram_channels[] = {
                .cmds_start = 0x180200,
                .ctrl_start = 0x1807C0,
                .cdt        = 0x1807C0 + 64,
-               .fifo_start = 0x185600,
+               .fifo_start = 0x186400,
                .fifo_size  = 0x001000,
                .ptr1_reg   = MO_DMA28_PTR1,
                .ptr2_reg   = MO_DMA28_PTR2,
                .cnt1_reg   = MO_DMA28_CNT1,
                .cnt2_reg   = MO_DMA28_CNT2,
        },
+       [SRAM_CH27] = {
+               .name       = "audio rds",
+               .cmds_start = 0x1801C0,
+               .ctrl_start = 0x180860,
+               .cdt        = 0x180860 + 64,
+               .fifo_start = 0x187400,
+               .fifo_size  = 0x000C00,
+               .ptr1_reg   = MO_DMA27_PTR1,
+               .ptr2_reg   = MO_DMA27_PTR2,
+               .cnt1_reg   = MO_DMA27_CNT1,
+               .cnt2_reg   = MO_DMA27_CNT2,
+       },
 };
 
 int cx88_sram_channel_setup(struct cx88_core *core,
@@ -495,10 +456,13 @@ void cx88_sram_channel_dump(struct cx88_core *core,
                printk("%s:   cmds: %-12s: 0x%08x\n",
                       core->name,name[i],
                       cx_read(ch->cmds_start + 4*i));
-       for (i = 0; i < 4; i++) {
+       for (n = 1, i = 0; i < 4; i++) {
                risc = cx_read(ch->cmds_start + 4 * (i+11));
                printk("%s:   risc%d: ", core->name, i);
-               cx88_risc_decode(risc);
+               if (--n)
+                       printk("0x%08x [ arg #%d ]\n", risc, n);
+               else
+                       n = cx88_risc_decode(risc);
        }
        for (i = 0; i < 16; i += n) {
                risc = cx_read(ch->ctrl_start + 4 * i);
@@ -533,12 +497,12 @@ static char *cx88_pci_irqs[32] = {
 };
 
 void cx88_print_irqbits(char *name, char *tag, char **strings,
-                       u32 bits, u32 mask)
+                       int len, u32 bits, u32 mask)
 {
        unsigned int i;
 
        printk(KERN_DEBUG "%s: %s [0x%x]", name, tag, bits);
-       for (i = 0; i < 32; i++) {
+       for (i = 0; i < len; i++) {
                if (!(bits & (1 << i)))
                        continue;
                if (strings[i])
@@ -558,14 +522,14 @@ int cx88_core_irq(struct cx88_core *core, u32 status)
 {
        int handled = 0;
 
-       if (status & (1<<18)) {
+       if (status & PCI_INT_IR_SMPINT) {
                cx88_ir_irq(core);
                handled++;
        }
        if (!handled)
                cx88_print_irqbits(core->name, "irq pci",
-                                  cx88_pci_irqs, status,
-                                  core->pci_irqmask);
+                                  cx88_pci_irqs, ARRAY_SIZE(cx88_pci_irqs),
+                                  status, core->pci_irqmask);
        return handled;
 }
 
@@ -588,7 +552,7 @@ void cx88_wakeup(struct cx88_core *core,
                do_gettimeofday(&buf->vb.ts);
                dprintk(2,"[%p/%d] wakeup reg=%d buf=%d\n",buf,buf->vb.i,
                        count, buf->count);
-               buf->vb.state = STATE_DONE;
+               buf->vb.state = VIDEOBUF_DONE;
                list_del(&buf->vb.queue);
                wake_up(&buf->vb.done);
        }
@@ -598,7 +562,8 @@ void cx88_wakeup(struct cx88_core *core,
                mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
        }
        if (bc != 1)
-               printk("%s: %d buffers handled (should be 1)\n",__FUNCTION__,bc);
+               dprintk(2, "%s: %d buffers handled (should be 1)\n",
+                       __func__, bc);
 }
 
 void cx88_shutdown(struct cx88_core *core)
@@ -627,7 +592,7 @@ void cx88_shutdown(struct cx88_core *core)
 
 int cx88_reset(struct cx88_core *core)
 {
-       dprintk(1,"%s\n",__FUNCTION__);
+       dprintk(1,"%s\n",__func__);
        cx88_shutdown(core);
 
        /* clear irq status */
@@ -646,6 +611,7 @@ int cx88_reset(struct cx88_core *core)
        cx88_sram_channel_setup(core, &cx88_sram_channels[SRAM_CH25], 128, 0);
        cx88_sram_channel_setup(core, &cx88_sram_channels[SRAM_CH26], 128, 0);
        cx88_sram_channel_setup(core, &cx88_sram_channels[SRAM_CH28], 188*4, 0);
+       cx88_sram_channel_setup(core, &cx88_sram_channels[SRAM_CH27], 128, 0);
 
        /* misc init ... */
        cx_write(MO_INPUT_FORMAT, ((1 << 13) |   // agc enable
@@ -658,7 +624,7 @@ int cx88_reset(struct cx88_core *core)
        /* setup image format */
        cx_andor(MO_COLOR_CTRL, 0x4000, 0x4000);
 
-       /* setup FIFO Threshholds */
+       /* setup FIFO Thresholds */
        cx_write(MO_PDMA_STHRSH,   0x0807);
        cx_write(MO_PDMA_DTHRSH,   0x0807);
 
@@ -680,53 +646,55 @@ int cx88_reset(struct cx88_core *core)
 
 /* ------------------------------------------------------------------ */
 
-static unsigned int inline norm_swidth(struct cx88_tvnorm *norm)
+static unsigned int inline norm_swidth(v4l2_std_id norm)
 {
-       return (norm->id & V4L2_STD_625_50) ? 922 : 754;
+       return (norm & (V4L2_STD_MN & ~V4L2_STD_PAL_Nc)) ? 754 : 922;
 }
 
-static unsigned int inline norm_hdelay(struct cx88_tvnorm *norm)
+static unsigned int inline norm_hdelay(v4l2_std_id norm)
 {
-       return (norm->id & V4L2_STD_625_50) ? 186 : 135;
+       return (norm & (V4L2_STD_MN & ~V4L2_STD_PAL_Nc)) ? 135 : 186;
 }
 
-static unsigned int inline norm_vdelay(struct cx88_tvnorm *norm)
+static unsigned int inline norm_vdelay(v4l2_std_id norm)
 {
-       return (norm->id & V4L2_STD_625_50) ? 0x24 : 0x18;
+       return (norm & V4L2_STD_625_50) ? 0x24 : 0x18;
 }
 
-static unsigned int inline norm_fsc8(struct cx88_tvnorm *norm)
+static unsigned int inline norm_fsc8(v4l2_std_id norm)
 {
-       static const unsigned int ntsc = 28636360;
-       static const unsigned int pal  = 35468950;
-       static const unsigned int palm  = 28604892;
+       if (norm & V4L2_STD_PAL_M)
+               return 28604892;      // 3.575611 MHz
 
-       if (norm->id & V4L2_STD_PAL_M)
-               return palm;
+       if (norm & (V4L2_STD_PAL_Nc))
+               return 28656448;      // 3.582056 MHz
 
-       return (norm->id & V4L2_STD_625_50) ? pal : ntsc;
-}
+       if (norm & V4L2_STD_NTSC) // All NTSC/M and variants
+               return 28636360;      // 3.57954545 MHz +/- 10 Hz
 
-static unsigned int inline norm_notchfilter(struct cx88_tvnorm *norm)
-{
-       return (norm->id & V4L2_STD_625_50)
-               ? HLNotchFilter135PAL
-               : HLNotchFilter135NTSC;
+       /* SECAM have also different sub carrier for chroma,
+          but step_db and step_dr, at cx88_set_tvnorm already handles that.
+
+          The same FSC applies to PAL/BGDKIH, PAL/60, NTSC/4.43 and PAL/N
+        */
+
+       return 35468950;      // 4.43361875 MHz +/- 5 Hz
 }
 
-static unsigned int inline norm_htotal(struct cx88_tvnorm *norm)
+static unsigned int inline norm_htotal(v4l2_std_id norm)
 {
-       /* Should always be Line Draw Time / (4*FSC) */
 
-       if (norm->id & V4L2_STD_PAL_M)
-               return 909;
+       unsigned int fsc4=norm_fsc8(norm)/2;
 
-       return (norm->id & V4L2_STD_625_50) ? 1135 : 910;
+       /* returns 4*FSC / vtotal / frames per seconds */
+       return (norm & V4L2_STD_625_50) ?
+                               ((fsc4+312)/625+12)/25 :
+                               ((fsc4+262)/525*1001+15000)/30000;
 }
 
-static unsigned int inline norm_vbipack(struct cx88_tvnorm *norm)
+static unsigned int inline norm_vbipack(v4l2_std_id norm)
 {
-       return (norm->id & V4L2_STD_625_50) ? 511 : 288;
+       return (norm & V4L2_STD_625_50) ? 511 : 400;
 }
 
 int cx88_set_scale(struct cx88_core *core, unsigned int width, unsigned int height,
@@ -739,7 +707,7 @@ int cx88_set_scale(struct cx88_core *core, unsigned int width, unsigned int heig
        dprintk(1,"set_scale: %dx%d [%s%s,%s]\n", width, height,
                V4L2_FIELD_HAS_TOP(field)    ? "T" : "",
                V4L2_FIELD_HAS_BOTTOM(field) ? "B" : "",
-               core->tvnorm->name);
+               v4l2_norm_to_name(core->tvnorm));
        if (!V4L2_FIELD_HAS_BOTH(field))
                height *= 2;
 
@@ -748,7 +716,7 @@ int cx88_set_scale(struct cx88_core *core, unsigned int width, unsigned int heig
        value &= 0x3fe;
        cx_write(MO_HDELAY_EVEN,  value);
        cx_write(MO_HDELAY_ODD,   value);
-       dprintk(1,"set_scale: hdelay  0x%04x\n", value);
+       dprintk(1,"set_scale: hdelay  0x%04x (width %d)\n", value,swidth);
 
        value = (swidth * 4096 / width) - 4096;
        cx_write(MO_HSCALE_EVEN,  value);
@@ -776,11 +744,11 @@ int cx88_set_scale(struct cx88_core *core, unsigned int width, unsigned int heig
        // setup filters
        value = 0;
        value |= (1 << 19);        // CFILT (default)
-       if (core->tvnorm->id & V4L2_STD_SECAM) {
+       if (core->tvnorm & V4L2_STD_SECAM) {
                value |= (1 << 15);
                value |= (1 << 16);
        }
-       if (INPUT(core->input)->type == CX88_VMUX_SVIDEO)
+       if (INPUT(core->input).type == CX88_VMUX_SVIDEO)
                value |= (1 << 13) | (1 << 5);
        if (V4L2_FIELD_INTERLACED == field)
                value |= (1 << 3); // VINT (interlaced vertical scaling)
@@ -837,38 +805,79 @@ static int set_pll(struct cx88_core *core, int prescale, u32 ofreq)
        return -1;
 }
 
+int cx88_start_audio_dma(struct cx88_core *core)
+{
+       /* constant 128 made buzz in analog Nicam-stereo for bigger fifo_size */
+       int bpl = cx88_sram_channels[SRAM_CH25].fifo_size/4;
+
+       int rds_bpl = cx88_sram_channels[SRAM_CH27].fifo_size/AUD_RDS_LINES;
+
+       /* If downstream RISC is enabled, bail out; ALSA is managing DMA */
+       if (cx_read(MO_AUD_DMACNTRL) & 0x10)
+               return 0;
+
+       /* setup fifo + format */
+       cx88_sram_channel_setup(core, &cx88_sram_channels[SRAM_CH25], bpl, 0);
+       cx88_sram_channel_setup(core, &cx88_sram_channels[SRAM_CH26], bpl, 0);
+       cx88_sram_channel_setup(core, &cx88_sram_channels[SRAM_CH27],
+                               rds_bpl, 0);
+
+       cx_write(MO_AUDD_LNGTH, bpl); /* fifo bpl size */
+       cx_write(MO_AUDR_LNGTH, rds_bpl); /* fifo bpl size */
+
+       /* enable Up, Down and Audio RDS fifo */
+       cx_write(MO_AUD_DMACNTRL, 0x0007);
+
+       return 0;
+}
+
+int cx88_stop_audio_dma(struct cx88_core *core)
+{
+       /* If downstream RISC is enabled, bail out; ALSA is managing DMA */
+       if (cx_read(MO_AUD_DMACNTRL) & 0x10)
+               return 0;
+
+       /* stop dma */
+       cx_write(MO_AUD_DMACNTRL, 0x0000);
+
+       return 0;
+}
+
 static int set_tvaudio(struct cx88_core *core)
 {
-       struct cx88_tvnorm *norm = core->tvnorm;
+       v4l2_std_id norm = core->tvnorm;
 
-       if (CX88_VMUX_TELEVISION != INPUT(core->input)->type)
+       if (CX88_VMUX_TELEVISION != INPUT(core->input).type)
                return 0;
 
-       if (V4L2_STD_PAL_BG & norm->id) {
+       if (V4L2_STD_PAL_BG & norm) {
                core->tvaudio = WW_BG;
 
-       } else if (V4L2_STD_PAL_DK & norm->id) {
+       } else if (V4L2_STD_PAL_DK & norm) {
                core->tvaudio = WW_DK;
 
-       } else if (V4L2_STD_PAL_I & norm->id) {
+       } else if (V4L2_STD_PAL_I & norm) {
                core->tvaudio = WW_I;
 
-       } else if (V4L2_STD_SECAM_L & norm->id) {
+       } else if (V4L2_STD_SECAM_L & norm) {
                core->tvaudio = WW_L;
 
-       } else if (V4L2_STD_SECAM_DK & norm->id) {
+       } else if ((V4L2_STD_SECAM_B | V4L2_STD_SECAM_G | V4L2_STD_SECAM_H) & norm) {
+               core->tvaudio = WW_BG;
+
+       } else if (V4L2_STD_SECAM_DK & norm) {
                core->tvaudio = WW_DK;
 
-       } else if ((V4L2_STD_NTSC_M & norm->id) ||
-                  (V4L2_STD_PAL_M  & norm->id)) {
+       } else if ((V4L2_STD_NTSC_M & norm) ||
+                  (V4L2_STD_PAL_M  & norm)) {
                core->tvaudio = WW_BTSC;
 
-       } else if (V4L2_STD_NTSC_M_JP & norm->id) {
+       } else if (V4L2_STD_NTSC_M_JP & norm) {
                core->tvaudio = WW_EIAJ;
 
        } else {
                printk("%s/0: tvaudio support needs work for this tv norm [%s], sorry\n",
-                      core->name, norm->name);
+                      core->name, v4l2_norm_to_name(core->tvnorm));
                core->tvaudio = 0;
                return 0;
        }
@@ -877,10 +886,17 @@ static int set_tvaudio(struct cx88_core *core)
        cx88_set_tvaudio(core);
        /* cx88_set_stereo(dev,V4L2_TUNER_MODE_STEREO); */
 
+/*
+   This should be needed only on cx88-alsa. It seems that some cx88 chips have
+   bugs and does require DMA enabled for it to work.
+ */
+       cx88_start_audio_dma(core);
        return 0;
 }
 
-int cx88_set_tvnorm(struct cx88_core *core, struct cx88_tvnorm *norm)
+
+
+int cx88_set_tvnorm(struct cx88_core *core, v4l2_std_id norm)
 {
        u32 fsc8;
        u32 adc_clock;
@@ -888,6 +904,7 @@ int cx88_set_tvnorm(struct cx88_core *core, struct cx88_tvnorm *norm)
        u32 step_db,step_dr;
        u64 tmp64;
        u32 bdelay,agcdelay,htotal;
+       u32 cxiformat, cxoformat;
 
        core->tvnorm = norm;
        fsc8       = norm_fsc8(norm);
@@ -896,23 +913,54 @@ int cx88_set_tvnorm(struct cx88_core *core, struct cx88_tvnorm *norm)
        step_db    = fsc8;
        step_dr    = fsc8;
 
-       if (norm->id & V4L2_STD_SECAM) {
+       if (norm & V4L2_STD_NTSC_M_JP) {
+               cxiformat = VideoFormatNTSCJapan;
+               cxoformat = 0x181f0008;
+       } else if (norm & V4L2_STD_NTSC_443) {
+               cxiformat = VideoFormatNTSC443;
+               cxoformat = 0x181f0008;
+       } else if (norm & V4L2_STD_PAL_M) {
+               cxiformat = VideoFormatPALM;
+               cxoformat = 0x1c1f0008;
+       } else if (norm & V4L2_STD_PAL_N) {
+               cxiformat = VideoFormatPALN;
+               cxoformat = 0x1c1f0008;
+       } else if (norm & V4L2_STD_PAL_Nc) {
+               cxiformat = VideoFormatPALNC;
+               cxoformat = 0x1c1f0008;
+       } else if (norm & V4L2_STD_PAL_60) {
+               cxiformat = VideoFormatPAL60;
+               cxoformat = 0x181f0008;
+       } else if (norm & V4L2_STD_NTSC) {
+               cxiformat = VideoFormatNTSC;
+               cxoformat = 0x181f0008;
+       } else if (norm & V4L2_STD_SECAM) {
                step_db = 4250000 * 8;
                step_dr = 4406250 * 8;
+
+               cxiformat = VideoFormatSECAM;
+               cxoformat = 0x181f0008;
+       } else { /* PAL */
+               cxiformat = VideoFormatPAL;
+               cxoformat = 0x181f0008;
        }
 
        dprintk(1,"set_tvnorm: \"%s\" fsc8=%d adc=%d vdec=%d db/dr=%d/%d\n",
-               norm->name, fsc8, adc_clock, vdec_clock, step_db, step_dr);
+               v4l2_norm_to_name(core->tvnorm), fsc8, adc_clock, vdec_clock,
+               step_db, step_dr);
        set_pll(core,2,vdec_clock);
 
        dprintk(1,"set_tvnorm: MO_INPUT_FORMAT  0x%08x [old=0x%08x]\n",
-               norm->cxiformat, cx_read(MO_INPUT_FORMAT) & 0x0f);
-       cx_andor(MO_INPUT_FORMAT, 0xf, norm->cxiformat);
+               cxiformat, cx_read(MO_INPUT_FORMAT) & 0x0f);
+       /* Chroma AGC must be disabled if SECAM is used, we enable it
+          by default on PAL and NTSC */
+       cx_andor(MO_INPUT_FORMAT, 0x40f,
+                norm & V4L2_STD_SECAM ? cxiformat : cxiformat | 0x400);
 
        // FIXME: as-is from DScaler
        dprintk(1,"set_tvnorm: MO_OUTPUT_FORMAT 0x%08x [old=0x%08x]\n",
-               norm->cxoformat, cx_read(MO_OUTPUT_FORMAT));
-       cx_write(MO_OUTPUT_FORMAT, norm->cxoformat);
+               cxoformat, cx_read(MO_OUTPUT_FORMAT));
+       cx_write(MO_OUTPUT_FORMAT, cxoformat);
 
        // MO_SCONV_REG = adc clock / video dec clock * 2^17
        tmp64  = adc_clock * (u64)(1 << 17);
@@ -945,14 +993,14 @@ int cx88_set_tvnorm(struct cx88_core *core, struct cx88_tvnorm *norm)
        // htotal
        tmp64 = norm_htotal(norm) * (u64)vdec_clock;
        do_div(tmp64, fsc8);
-       htotal = (u32)tmp64 | (norm_notchfilter(norm) << 11);
+       htotal = (u32)tmp64 | (HLNotchFilter4xFsc << 11);
        dprintk(1,"set_tvnorm: MO_HTOTAL        0x%08x [old=0x%08x,htotal=%d]\n",
                htotal, cx_read(MO_HTOTAL), (u32)tmp64);
        cx_write(MO_HTOTAL, htotal);
 
-       // vbi stuff
-       cx_write(MO_VBI_PACKET, ((1 << 11) | /* (norm_vdelay(norm)   << 11) | */
-                                norm_vbipack(norm)));
+       // vbi stuff, set vbi offset to 10 (for 20 Clk*2 pixels), this makes
+       // the effective vbi offset ~244 samples, the same as the Bt8x8
+       cx_write(MO_VBI_PACKET, (10<<11) | norm_vbipack(norm));
 
        // this is needed as well to set all tvnorm parameter
        cx88_set_scale(core, 320, 240, V4L2_FIELD_INTERLACED);
@@ -961,7 +1009,7 @@ int cx88_set_tvnorm(struct cx88_core *core, struct cx88_tvnorm *norm)
        set_tvaudio(core);
 
        // tell i2c chips
-       cx88_call_i2c_clients(core,VIDIOC_S_STD,&norm->id);
+       call_all(core, core, s_std, norm);
 
        // done
        return 0;
@@ -969,61 +1017,6 @@ int cx88_set_tvnorm(struct cx88_core *core, struct cx88_tvnorm *norm)
 
 /* ------------------------------------------------------------------ */
 
-static int cx88_pci_quirks(char *name, struct pci_dev *pci)
-{
-       unsigned int lat = UNSET;
-       u8 ctrl = 0;
-       u8 value;
-
-       /* check pci quirks */
-       if (pci_pci_problems & PCIPCI_TRITON) {
-               printk(KERN_INFO "%s: quirk: PCIPCI_TRITON -- set TBFX\n",
-                      name);
-               ctrl |= CX88X_EN_TBFX;
-       }
-       if (pci_pci_problems & PCIPCI_NATOMA) {
-               printk(KERN_INFO "%s: quirk: PCIPCI_NATOMA -- set TBFX\n",
-                      name);
-               ctrl |= CX88X_EN_TBFX;
-       }
-       if (pci_pci_problems & PCIPCI_VIAETBF) {
-               printk(KERN_INFO "%s: quirk: PCIPCI_VIAETBF -- set TBFX\n",
-                      name);
-               ctrl |= CX88X_EN_TBFX;
-       }
-       if (pci_pci_problems & PCIPCI_VSFX) {
-               printk(KERN_INFO "%s: quirk: PCIPCI_VSFX -- set VSFX\n",
-                      name);
-               ctrl |= CX88X_EN_VSFX;
-       }
-#ifdef PCIPCI_ALIMAGIK
-       if (pci_pci_problems & PCIPCI_ALIMAGIK) {
-               printk(KERN_INFO "%s: quirk: PCIPCI_ALIMAGIK -- latency fixup\n",
-                      name);
-               lat = 0x0A;
-       }
-#endif
-
-       /* check insmod options */
-       if (UNSET != latency)
-               lat = latency;
-
-       /* apply stuff */
-       if (ctrl) {
-               pci_read_config_byte(pci, CX88X_DEVCTRL, &value);
-               value |= ctrl;
-               pci_write_config_byte(pci, CX88X_DEVCTRL, value);
-       }
-       if (UNSET != lat) {
-               printk(KERN_INFO "%s: setting pci latency timer to %d\n",
-                      name, latency);
-               pci_write_config_byte(pci, PCI_LATENCY_TIMER, latency);
-       }
-       return 0;
-}
-
-/* ------------------------------------------------------------------ */
-
 struct video_device *cx88_vdev_init(struct cx88_core *core,
                                    struct pci_dev *pci,
                                    struct video_device *template,
@@ -1035,126 +1028,42 @@ struct video_device *cx88_vdev_init(struct cx88_core *core,
        if (NULL == vfd)
                return NULL;
        *vfd = *template;
-       vfd->minor   = -1;
-       vfd->dev     = &pci->dev;
+       vfd->v4l2_dev = &core->v4l2_dev;
+       vfd->parent = &pci->dev;
        vfd->release = video_device_release;
        snprintf(vfd->name, sizeof(vfd->name), "%s %s (%s)",
-                core->name, type, cx88_boards[core->board].name);
+                core->name, type, core->board.name);
        return vfd;
 }
 
-static int get_ressources(struct cx88_core *core, struct pci_dev *pci)
-{
-       if (request_mem_region(pci_resource_start(pci,0),
-                              pci_resource_len(pci,0),
-                              core->name))
-               return 0;
-       printk(KERN_ERR "%s: can't get MMIO memory @ 0x%lx\n",
-              core->name,pci_resource_start(pci,0));
-       return -EBUSY;
-}
-
 struct cx88_core* cx88_core_get(struct pci_dev *pci)
 {
        struct cx88_core *core;
-       struct list_head *item;
-       int i;
 
-       down(&devlist);
-       list_for_each(item,&cx88_devlist) {
-               core = list_entry(item, struct cx88_core, devlist);
+       mutex_lock(&devlist);
+       list_for_each_entry(core, &cx88_devlist, devlist) {
                if (pci->bus->number != core->pci_bus)
                        continue;
                if (PCI_SLOT(pci->devfn) != core->pci_slot)
                        continue;
 
-               if (0 != get_ressources(core,pci))
-                       goto fail_unlock;
+               if (0 != cx88_get_resources(core, pci)) {
+                       mutex_unlock(&devlist);
+                       return NULL;
+               }
                atomic_inc(&core->refcount);
-               up(&devlist);
+               mutex_unlock(&devlist);
                return core;
        }
-       core = kmalloc(sizeof(*core),GFP_KERNEL);
-       if (NULL == core)
-               goto fail_unlock;
-
-       memset(core,0,sizeof(*core));
-       atomic_inc(&core->refcount);
-       core->pci_bus  = pci->bus->number;
-       core->pci_slot = PCI_SLOT(pci->devfn);
-       core->pci_irqmask = 0x00fc00;
-       init_MUTEX(&core->lock);
-
-       core->nr = cx88_devcount++;
-       sprintf(core->name,"cx88[%d]",core->nr);
-       if (0 != get_ressources(core,pci)) {
-               printk(KERN_ERR "CORE %s No more PCI ressources for "
-                       "subsystem: %04x:%04x, board: %s\n",
-                       core->name,pci->subsystem_vendor,
-                       pci->subsystem_device,
-                       cx88_boards[core->board].name);
-
-               cx88_devcount--;
-               goto fail_free;
-       }
-       list_add_tail(&core->devlist,&cx88_devlist);
-
-       /* PCI stuff */
-       cx88_pci_quirks(core->name, pci);
-       core->lmmio = ioremap(pci_resource_start(pci,0),
-                             pci_resource_len(pci,0));
-       core->bmmio = (u8 __iomem *)core->lmmio;
-
-       /* board config */
-       core->board = UNSET;
-       if (card[core->nr] < cx88_bcount)
-               core->board = card[core->nr];
-       for (i = 0; UNSET == core->board  &&  i < cx88_idcount; i++)
-               if (pci->subsystem_vendor == cx88_subids[i].subvendor &&
-                   pci->subsystem_device == cx88_subids[i].subdevice)
-                       core->board = cx88_subids[i].card;
-       if (UNSET == core->board) {
-               core->board = CX88_BOARD_UNKNOWN;
-               cx88_card_list(core,pci);
+
+       core = cx88_core_create(pci, cx88_devcount);
+       if (NULL != core) {
+               cx88_devcount++;
+               list_add_tail(&core->devlist, &cx88_devlist);
        }
-       printk(KERN_INFO "CORE %s: subsystem: %04x:%04x, board: %s [card=%d,%s]\n",
-               core->name,pci->subsystem_vendor,
-               pci->subsystem_device,cx88_boards[core->board].name,
-               core->board, card[core->nr] == core->board ?
-               "insmod option" : "autodetected");
-
-       core->tuner_type = tuner[core->nr];
-       core->radio_type = radio[core->nr];
-       if (UNSET == core->tuner_type)
-               core->tuner_type = cx88_boards[core->board].tuner_type;
-       if (UNSET == core->radio_type)
-               core->radio_type = cx88_boards[core->board].radio_type;
-       if (!core->tuner_addr)
-               core->tuner_addr = cx88_boards[core->board].tuner_addr;
-       if (!core->radio_addr)
-               core->radio_addr = cx88_boards[core->board].radio_addr;
-
-       printk(KERN_INFO "TV tuner %d at 0x%02x, Radio tuner %d at 0x%02x\n",
-               core->tuner_type, core->tuner_addr<<1,
-               core->radio_type, core->radio_addr<<1);
-
-       core->tda9887_conf = cx88_boards[core->board].tda9887_conf;
-
-       /* init hardware */
-       cx88_reset(core);
-       cx88_i2c_init(core,pci);
-       cx88_call_i2c_clients (core, TUNER_SET_STANDBY, NULL);
-       cx88_card_setup(core);
-       cx88_ir_init(core,pci);
-
-       up(&devlist);
-       return core;
 
-fail_free:
-       kfree(core);
-fail_unlock:
-       up(&devlist);
-       return NULL;
+       mutex_unlock(&devlist);
+       return core;
 }
 
 void cx88_core_put(struct cx88_core *core, struct pci_dev *pci)
@@ -1165,20 +1074,23 @@ void cx88_core_put(struct cx88_core *core, struct pci_dev *pci)
        if (!atomic_dec_and_test(&core->refcount))
                return;
 
-       down(&devlist);
+       mutex_lock(&devlist);
        cx88_ir_fini(core);
-       if (0 == core->i2c_rc)
-               i2c_bit_del_bus(&core->i2c_adap);
+       if (0 == core->i2c_rc) {
+               if (core->i2c_rtc)
+                       i2c_unregister_device(core->i2c_rtc);
+               i2c_del_adapter(&core->i2c_adap);
+       }
        list_del(&core->devlist);
        iounmap(core->lmmio);
        cx88_devcount--;
-       up(&devlist);
+       mutex_unlock(&devlist);
+       v4l2_device_unregister(&core->v4l2_dev);
        kfree(core);
 }
 
 /* ------------------------------------------------------------------ */
 
-EXPORT_SYMBOL(cx88_print_ioctl);
 EXPORT_SYMBOL(cx88_print_irqbits);
 
 EXPORT_SYMBOL(cx88_core_irq);
@@ -1202,6 +1114,9 @@ EXPORT_SYMBOL(cx88_vdev_init);
 EXPORT_SYMBOL(cx88_core_get);
 EXPORT_SYMBOL(cx88_core_put);
 
+EXPORT_SYMBOL(cx88_ir_start);
+EXPORT_SYMBOL(cx88_ir_stop);
+
 /*
  * Local variables:
  * c-basic-offset: 8