]> git.karo-electronics.de Git - mv-sheeva.git/blobdiff - drivers/media/video/saa7164/saa7164-core.c
Merge tag 'v2.6.37' of git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[mv-sheeva.git] / drivers / media / video / saa7164 / saa7164-core.c
index e6aa0fbd1e910d36834be82fddcee2aaf6c8a926..e1bac5051460360202dcef7bf117aa0974d4f316 100644 (file)
@@ -1,7 +1,7 @@
 /*
  *  Driver for the NXP SAA7164 PCIe bridge
  *
- *  Copyright (c) 2009 Steven Toth <stoth@kernellabs.com>
+ *  Copyright (c) 2010 Steven Toth <stoth@kernellabs.com>
  *
  *  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
@@ -30,6 +30,9 @@
 #include <linux/delay.h>
 #include <asm/div64.h>
 
+#ifdef CONFIG_PROC_FS
+#include <linux/proc_fs.h>
+#endif
 #include "saa7164.h"
 
 MODULE_DESCRIPTION("Driver for NXP SAA7164 based TV cards");
@@ -49,14 +52,38 @@ unsigned int saa_debug;
 module_param_named(debug, saa_debug, int, 0644);
 MODULE_PARM_DESC(debug, "enable debug messages");
 
+unsigned int fw_debug;
+module_param(fw_debug, int, 0644);
+MODULE_PARM_DESC(fw_debug, "Firware debug level def:2");
+
+unsigned int encoder_buffers = SAA7164_MAX_ENCODER_BUFFERS;
+module_param(encoder_buffers, int, 0644);
+MODULE_PARM_DESC(encoder_buffers, "Total buffers in read queue 16-512 def:64");
+
+unsigned int vbi_buffers = SAA7164_MAX_VBI_BUFFERS;
+module_param(vbi_buffers, int, 0644);
+MODULE_PARM_DESC(vbi_buffers, "Total buffers in read queue 16-512 def:64");
+
 unsigned int waitsecs = 10;
 module_param(waitsecs, int, 0644);
-MODULE_PARM_DESC(debug, "timeout on firmware messages");
+MODULE_PARM_DESC(waitsecs, "timeout on firmware messages");
 
 static unsigned int card[]  = {[0 ... (SAA7164_MAXBOARDS - 1)] = UNSET };
 module_param_array(card,  int, NULL, 0444);
 MODULE_PARM_DESC(card, "card type");
 
+unsigned int print_histogram = 64;
+module_param(print_histogram, int, 0644);
+MODULE_PARM_DESC(print_histogram, "print histogram values once");
+
+unsigned int crc_checking = 1;
+module_param(crc_checking, int, 0644);
+MODULE_PARM_DESC(crc_checking, "enable crc sanity checking on buffers");
+
+unsigned int guard_checking = 1;
+module_param(guard_checking, int, 0644);
+MODULE_PARM_DESC(guard_checking, "enable dma sanity checking for buffer overruns");
+
 static unsigned int saa7164_devcount;
 
 static DEFINE_MUTEX(devlist);
@@ -64,6 +91,444 @@ LIST_HEAD(saa7164_devlist);
 
 #define INT_SIZE 16
 
+void saa7164_dumphex16FF(struct saa7164_dev *dev, u8 *buf, int len)
+{
+       int i;
+       u8 tmp[16];
+       memset(&tmp[0], 0xff, sizeof(tmp));
+
+       printk(KERN_INFO "--------------------> "
+               "00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f\n");
+
+       for (i = 0; i < len; i += 16) {
+               if (memcmp(&tmp, buf + i, sizeof(tmp)) != 0) {
+                       printk(KERN_INFO "         [0x%08x] "
+                               "%02x %02x %02x %02x %02x %02x %02x %02x "
+                               "%02x %02x %02x %02x %02x %02x %02x %02x\n", i,
+                       *(buf+i+0), *(buf+i+1), *(buf+i+2), *(buf+i+3),
+                       *(buf+i+4), *(buf+i+5), *(buf+i+6), *(buf+i+7),
+                       *(buf+i+8), *(buf+i+9), *(buf+i+10), *(buf+i+11),
+                       *(buf+i+12), *(buf+i+13), *(buf+i+14), *(buf+i+15));
+               }
+       }
+}
+
+static void saa7164_pack_verifier(struct saa7164_buffer *buf)
+{
+       u8 *p = (u8 *)buf->cpu;
+       int i;
+
+       for (i = 0; i < buf->actual_size; i += 2048) {
+
+               if ((*(p + i + 0) != 0x00) || (*(p + i + 1) != 0x00) ||
+                       (*(p + i + 2) != 0x01) || (*(p + i + 3) != 0xBA)) {
+                       printk(KERN_ERR "No pack at 0x%x\n", i);
+//                     saa7164_dumphex16FF(buf->port->dev, (p + i), 32);
+               }
+       }
+}
+
+#define FIXED_VIDEO_PID 0xf1
+#define FIXED_AUDIO_PID 0xf2
+
+static void saa7164_ts_verifier(struct saa7164_buffer *buf)
+{
+       struct saa7164_port *port = buf->port;
+       u32 i;
+       u8 cc, a;
+       u16 pid;
+       u8 __iomem *bufcpu = (u8 *)buf->cpu;
+
+       port->sync_errors = 0;
+       port->v_cc_errors = 0;
+       port->a_cc_errors = 0;
+
+       for (i = 0; i < buf->actual_size; i += 188) {
+               if (*(bufcpu + i) != 0x47)
+                       port->sync_errors++;
+
+               /* TODO: Query pid lower 8 bits, ignoring upper bits intensionally */
+               pid = ((*(bufcpu + i + 1) & 0x1f) << 8) | *(bufcpu + i + 2);
+               cc = *(bufcpu + i + 3) & 0x0f;
+
+               if (pid == FIXED_VIDEO_PID) {
+                       a = ((port->last_v_cc + 1) & 0x0f);
+                       if (a != cc) {
+                               printk(KERN_ERR "video cc last = %x current = %x i = %d\n",
+                                       port->last_v_cc, cc, i);
+                               port->v_cc_errors++;
+                       }
+
+                       port->last_v_cc = cc;
+               } else
+               if (pid == FIXED_AUDIO_PID) {
+                       a = ((port->last_a_cc + 1) & 0x0f);
+                       if (a != cc) {
+                               printk(KERN_ERR "audio cc last = %x current = %x i = %d\n",
+                                       port->last_a_cc, cc, i);
+                               port->a_cc_errors++;
+                       }
+
+                       port->last_a_cc = cc;
+               }
+
+       }
+
+       /* Only report errors if we've been through this function atleast
+        * once already and the cached cc values are primed. First time through
+        * always generates errors.
+        */
+       if (port->v_cc_errors && (port->done_first_interrupt > 1))
+               printk(KERN_ERR "video pid cc, %d errors\n", port->v_cc_errors);
+
+       if (port->a_cc_errors && (port->done_first_interrupt > 1))
+               printk(KERN_ERR "audio pid cc, %d errors\n", port->a_cc_errors);
+
+       if (port->sync_errors && (port->done_first_interrupt > 1))
+               printk(KERN_ERR "sync_errors = %d\n", port->sync_errors);
+
+       if (port->done_first_interrupt == 1)
+               port->done_first_interrupt++;
+}
+
+static void saa7164_histogram_reset(struct saa7164_histogram *hg, char *name)
+{
+       int i;
+
+       memset(hg, 0, sizeof(struct saa7164_histogram));
+       strcpy(hg->name, name);
+
+       /* First 30ms x 1ms */
+       for (i = 0; i < 30; i++) {
+               hg->counter1[0 + i].val = i;
+       }
+
+       /* 30 - 200ms x 10ms  */
+       for (i = 0; i < 18; i++) {
+               hg->counter1[30 + i].val = 30 + (i * 10);
+       }
+
+       /* 200 - 2000ms x 100ms  */
+       for (i = 0; i < 15; i++) {
+               hg->counter1[48 + i].val = 200 + (i * 200);
+       }
+
+       /* Catch all massive value (2secs) */
+       hg->counter1[55].val = 2000;
+
+       /* Catch all massive value (4secs) */
+       hg->counter1[56].val = 4000;
+
+       /* Catch all massive value (8secs) */
+       hg->counter1[57].val = 8000;
+
+       /* Catch all massive value (15secs) */
+       hg->counter1[58].val = 15000;
+
+       /* Catch all massive value (30secs) */
+       hg->counter1[59].val = 30000;
+
+       /* Catch all massive value (60secs) */
+       hg->counter1[60].val = 60000;
+
+       /* Catch all massive value (5mins) */
+       hg->counter1[61].val = 300000;
+
+       /* Catch all massive value (15mins) */
+       hg->counter1[62].val = 900000;
+
+       /* Catch all massive values (1hr) */
+       hg->counter1[63].val = 3600000;
+}
+
+void saa7164_histogram_update(struct saa7164_histogram *hg, u32 val)
+{
+       int i;
+       for (i = 0; i < 64; i++) {
+               if (val <= hg->counter1[i].val) {
+                       hg->counter1[i].count++;
+                       hg->counter1[i].update_time = jiffies;
+                       break;
+               }
+       }
+}
+
+static void saa7164_histogram_print(struct saa7164_port *port,
+       struct saa7164_histogram *hg)
+{
+       u32 entries = 0;
+       int i;
+
+       printk(KERN_ERR "Histogram named %s (ms, count, last_update_jiffy)\n", hg->name);
+       for (i = 0; i < 64; i++) {
+               if (hg->counter1[i].count == 0)
+                       continue;
+
+               printk(KERN_ERR " %4d %12d %Ld\n",
+                       hg->counter1[i].val,
+                       hg->counter1[i].count,
+                       hg->counter1[i].update_time);
+
+               entries++;
+       }
+       printk(KERN_ERR "Total: %d\n", entries);
+}
+
+static void saa7164_work_enchandler_helper(struct saa7164_port *port, int bufnr)
+{
+       struct saa7164_dev *dev = port->dev;
+       struct saa7164_buffer *buf = 0;
+       struct saa7164_user_buffer *ubuf = 0;
+       struct list_head *c, *n;
+       int i = 0;
+       u8 __iomem *p;
+
+       mutex_lock(&port->dmaqueue_lock);
+       list_for_each_safe(c, n, &port->dmaqueue.list) {
+
+               buf = list_entry(c, struct saa7164_buffer, list);
+               if (i++ > port->hwcfg.buffercount) {
+                       printk(KERN_ERR "%s() illegal i count %d\n",
+                               __func__, i);
+                       break;
+               }
+
+               if (buf->idx == bufnr) {
+
+                       /* Found the buffer, deal with it */
+                       dprintk(DBGLVL_IRQ, "%s() bufnr: %d\n", __func__, bufnr);
+
+                       if (crc_checking) {
+                               /* Throw a new checksum on the dma buffer */
+                               buf->crc = crc32(0, buf->cpu, buf->actual_size);
+                       }
+
+                       if (guard_checking) {
+                               p = (u8 *)buf->cpu;
+                               if ((*(p + buf->actual_size + 0) != 0xff) ||
+                                       (*(p + buf->actual_size + 1) != 0xff) ||
+                                       (*(p + buf->actual_size + 2) != 0xff) ||
+                                       (*(p + buf->actual_size + 3) != 0xff) ||
+                                       (*(p + buf->actual_size + 0x10) != 0xff) ||
+                                       (*(p + buf->actual_size + 0x11) != 0xff) ||
+                                       (*(p + buf->actual_size + 0x12) != 0xff) ||
+                                       (*(p + buf->actual_size + 0x13) != 0xff)) {
+                                               printk(KERN_ERR "%s() buf %p guard buffer breach\n",
+                                                       __func__, buf);
+//                                             saa7164_dumphex16FF(dev, (p + buf->actual_size) - 32 , 64);
+                               }
+                       }
+
+                       if ((port->nr != SAA7164_PORT_VBI1) && (port->nr != SAA7164_PORT_VBI2)) {
+                               /* Validate the incoming buffer content */
+                               if (port->encoder_params.stream_type == V4L2_MPEG_STREAM_TYPE_MPEG2_TS)
+                                       saa7164_ts_verifier(buf);
+                               else if (port->encoder_params.stream_type == V4L2_MPEG_STREAM_TYPE_MPEG2_PS)
+                                       saa7164_pack_verifier(buf);
+                       }
+
+                       /* find a free user buffer and clone to it */
+                       if (!list_empty(&port->list_buf_free.list)) {
+
+                               /* Pull the first buffer from the used list */
+                               ubuf = list_first_entry(&port->list_buf_free.list,
+                                       struct saa7164_user_buffer, list);
+
+                               if (buf->actual_size <= ubuf->actual_size) {
+
+                                       memcpy_fromio(ubuf->data, buf->cpu,
+                                               ubuf->actual_size);
+
+                                       if (crc_checking) {
+                                               /* Throw a new checksum on the read buffer */
+                                               ubuf->crc = crc32(0, ubuf->data, ubuf->actual_size);
+                                       }
+
+                                       /* Requeue the buffer on the free list */
+                                       ubuf->pos = 0;
+
+                                       list_move_tail(&ubuf->list,
+                                               &port->list_buf_used.list);
+
+                                       /* Flag any userland waiters */
+                                       wake_up_interruptible(&port->wait_read);
+
+                               } else {
+                                       printk(KERN_ERR "buf %p bufsize fails match\n", buf);
+                               }
+
+                       } else
+                               printk(KERN_ERR "encirq no free buffers, increase param encoder_buffers\n");
+
+                       /* Ensure offset into buffer remains 0, fill buffer
+                        * with known bad data. We check for this data at a later point
+                        * in time. */
+                       saa7164_buffer_zero_offsets(port, bufnr);
+                       memset_io(buf->cpu, 0xff, buf->pci_size);
+                       if (crc_checking) {
+                               /* Throw yet aanother new checksum on the dma buffer */
+                               buf->crc = crc32(0, buf->cpu, buf->actual_size);
+                       }
+
+                       break;
+               }
+       }
+       mutex_unlock(&port->dmaqueue_lock);
+}
+
+static void saa7164_work_enchandler(struct work_struct *w)
+{
+       struct saa7164_port *port =
+               container_of(w, struct saa7164_port, workenc);
+       struct saa7164_dev *dev = port->dev;
+
+       u32 wp, mcb, rp, cnt = 0;
+
+       port->last_svc_msecs_diff = port->last_svc_msecs;
+       port->last_svc_msecs = jiffies_to_msecs(jiffies);
+
+       port->last_svc_msecs_diff = port->last_svc_msecs -
+               port->last_svc_msecs_diff;
+
+       saa7164_histogram_update(&port->svc_interval,
+               port->last_svc_msecs_diff);
+
+       port->last_irq_svc_msecs_diff = port->last_svc_msecs -
+               port->last_irq_msecs;
+
+       saa7164_histogram_update(&port->irq_svc_interval,
+               port->last_irq_svc_msecs_diff);
+
+       dprintk(DBGLVL_IRQ,
+               "%s() %Ldms elapsed irq->deferred %Ldms wp: %d rp: %d\n",
+               __func__,
+               port->last_svc_msecs_diff,
+               port->last_irq_svc_msecs_diff,
+               port->last_svc_wp,
+               port->last_svc_rp
+               );
+
+       /* Current write position */
+       wp = saa7164_readl(port->bufcounter);
+       if (wp > (port->hwcfg.buffercount - 1)) {
+               printk(KERN_ERR "%s() illegal buf count %d\n", __func__, wp);
+               return;
+       }
+
+       /* Most current complete buffer */
+       if (wp == 0)
+               mcb = (port->hwcfg.buffercount - 1);
+       else
+               mcb = wp - 1;
+
+       while (1) {
+               if (port->done_first_interrupt == 0) {
+                       port->done_first_interrupt++;
+                       rp = mcb;
+               } else
+                       rp = (port->last_svc_rp + 1) % 8;
+
+               if ((rp < 0) || (rp > (port->hwcfg.buffercount - 1))) {
+                       printk(KERN_ERR "%s() illegal rp count %d\n", __func__, rp);
+                       break;
+               }
+
+               saa7164_work_enchandler_helper(port, rp);
+               port->last_svc_rp = rp;
+               cnt++;
+
+               if (rp == mcb)
+                       break;
+       }
+
+       /* TODO: Convert this into a /proc/saa7164 style readable file */
+       if (print_histogram == port->nr) {
+               saa7164_histogram_print(port, &port->irq_interval);
+               saa7164_histogram_print(port, &port->svc_interval);
+               saa7164_histogram_print(port, &port->irq_svc_interval);
+               saa7164_histogram_print(port, &port->read_interval);
+               saa7164_histogram_print(port, &port->poll_interval);
+               /* TODO: fix this to preserve any previous state */
+               print_histogram = 64 + port->nr;
+       }
+}
+
+static void saa7164_work_vbihandler(struct work_struct *w)
+{
+       struct saa7164_port *port =
+               container_of(w, struct saa7164_port, workenc);
+       struct saa7164_dev *dev = port->dev;
+
+       u32 wp, mcb, rp, cnt = 0;
+
+       port->last_svc_msecs_diff = port->last_svc_msecs;
+       port->last_svc_msecs = jiffies_to_msecs(jiffies);
+       port->last_svc_msecs_diff = port->last_svc_msecs -
+               port->last_svc_msecs_diff;
+
+       saa7164_histogram_update(&port->svc_interval,
+               port->last_svc_msecs_diff);
+
+       port->last_irq_svc_msecs_diff = port->last_svc_msecs -
+               port->last_irq_msecs;
+
+       saa7164_histogram_update(&port->irq_svc_interval,
+               port->last_irq_svc_msecs_diff);
+
+       dprintk(DBGLVL_IRQ,
+               "%s() %Ldms elapsed irq->deferred %Ldms wp: %d rp: %d\n",
+               __func__,
+               port->last_svc_msecs_diff,
+               port->last_irq_svc_msecs_diff,
+               port->last_svc_wp,
+               port->last_svc_rp
+               );
+
+       /* Current write position */
+       wp = saa7164_readl(port->bufcounter);
+       if (wp > (port->hwcfg.buffercount - 1)) {
+               printk(KERN_ERR "%s() illegal buf count %d\n", __func__, wp);
+               return;
+       }
+
+       /* Most current complete buffer */
+       if (wp == 0)
+               mcb = (port->hwcfg.buffercount - 1);
+       else
+               mcb = wp - 1;
+
+       while (1) {
+               if (port->done_first_interrupt == 0) {
+                       port->done_first_interrupt++;
+                       rp = mcb;
+               } else
+                       rp = (port->last_svc_rp + 1) % 8;
+
+               if ((rp < 0) || (rp > (port->hwcfg.buffercount - 1))) {
+                       printk(KERN_ERR "%s() illegal rp count %d\n", __func__, rp);
+                       break;
+               }
+
+               saa7164_work_enchandler_helper(port, rp);
+               port->last_svc_rp = rp;
+               cnt++;
+
+               if (rp == mcb)
+                       break;
+       }
+
+       /* TODO: Convert this into a /proc/saa7164 style readable file */
+       if (print_histogram == port->nr) {
+               saa7164_histogram_print(port, &port->irq_interval);
+               saa7164_histogram_print(port, &port->svc_interval);
+               saa7164_histogram_print(port, &port->irq_svc_interval);
+               saa7164_histogram_print(port, &port->read_interval);
+               saa7164_histogram_print(port, &port->poll_interval);
+               /* TODO: fix this to preserve any previous state */
+               print_histogram = 64 + port->nr;
+       }
+}
+
 static void saa7164_work_cmdhandler(struct work_struct *w)
 {
        struct saa7164_dev *dev = container_of(w, struct saa7164_dev, workcmd);
@@ -74,7 +539,7 @@ static void saa7164_work_cmdhandler(struct work_struct *w)
 
 static void saa7164_buffer_deliver(struct saa7164_buffer *buf)
 {
-       struct saa7164_tsport *port = buf->port;
+       struct saa7164_port *port = buf->port;
 
        /* Feed the transport payload into the kernel demux */
        dvb_dmx_swfilter_packets(&port->dvb.demux, (u8 *)buf->cpu,
@@ -82,7 +547,56 @@ static void saa7164_buffer_deliver(struct saa7164_buffer *buf)
 
 }
 
-static irqreturn_t saa7164_irq_ts(struct saa7164_tsport *port)
+static irqreturn_t saa7164_irq_vbi(struct saa7164_port *port)
+{
+       struct saa7164_dev *dev = port->dev;
+
+       /* Store old time */
+       port->last_irq_msecs_diff = port->last_irq_msecs;
+
+       /* Collect new stats */
+       port->last_irq_msecs = jiffies_to_msecs(jiffies);
+
+       /* Calculate stats */
+       port->last_irq_msecs_diff = port->last_irq_msecs -
+               port->last_irq_msecs_diff;
+
+       saa7164_histogram_update(&port->irq_interval,
+               port->last_irq_msecs_diff);
+
+       dprintk(DBGLVL_IRQ, "%s() %Ldms elapsed\n", __func__,
+               port->last_irq_msecs_diff);
+
+       /* Tis calls the vbi irq handler */
+       schedule_work(&port->workenc);
+       return 0;
+}
+
+static irqreturn_t saa7164_irq_encoder(struct saa7164_port *port)
+{
+       struct saa7164_dev *dev = port->dev;
+
+       /* Store old time */
+       port->last_irq_msecs_diff = port->last_irq_msecs;
+
+       /* Collect new stats */
+       port->last_irq_msecs = jiffies_to_msecs(jiffies);
+
+       /* Calculate stats */
+       port->last_irq_msecs_diff = port->last_irq_msecs -
+               port->last_irq_msecs_diff;
+
+       saa7164_histogram_update(&port->irq_interval,
+               port->last_irq_msecs_diff);
+
+       dprintk(DBGLVL_IRQ, "%s() %Ldms elapsed\n", __func__,
+               port->last_irq_msecs_diff);
+
+       schedule_work(&port->workenc);
+       return 0;
+}
+
+static irqreturn_t saa7164_irq_ts(struct saa7164_port *port)
 {
        struct saa7164_dev *dev = port->dev;
        struct saa7164_buffer *buf;
@@ -96,7 +610,7 @@ static irqreturn_t saa7164_irq_ts(struct saa7164_tsport *port)
 
        /* Find the previous buffer to the current write point */
        if (wp == 0)
-               rp = 7;
+               rp = (port->hwcfg.buffercount - 1);
        else
                rp = wp - 1;
 
@@ -107,7 +621,7 @@ static irqreturn_t saa7164_irq_ts(struct saa7164_tsport *port)
                if (i++ > port->hwcfg.buffercount)
                        BUG();
 
-               if (buf->nr == rp) {
+               if (buf->idx == rp) {
                        /* Found the buffer, deal with it */
                        dprintk(DBGLVL_IRQ, "%s() wp: %d processing: %d\n",
                                __func__, wp, rp);
@@ -123,6 +637,13 @@ static irqreturn_t saa7164_irq_ts(struct saa7164_tsport *port)
 static irqreturn_t saa7164_irq(int irq, void *dev_id)
 {
        struct saa7164_dev *dev = dev_id;
+       struct saa7164_port *porta = &dev->ports[SAA7164_PORT_TS1];
+       struct saa7164_port *portb = &dev->ports[SAA7164_PORT_TS2];
+       struct saa7164_port *portc = &dev->ports[SAA7164_PORT_ENC1];
+       struct saa7164_port *portd = &dev->ports[SAA7164_PORT_ENC2];
+       struct saa7164_port *porte = &dev->ports[SAA7164_PORT_VBI1];
+       struct saa7164_port *portf = &dev->ports[SAA7164_PORT_VBI2];
+
        u32 intid, intstat[INT_SIZE/4];
        int i, handled = 0, bit;
 
@@ -168,17 +689,35 @@ static irqreturn_t saa7164_irq(int irq, void *dev_id)
                                if (intid == dev->intfdesc.bInterruptId) {
                                        /* A response to an cmd/api call */
                                        schedule_work(&dev->workcmd);
-                               } else if (intid ==
-                                       dev->ts1.hwcfg.interruptid) {
+                               } else if (intid == porta->hwcfg.interruptid) {
 
                                        /* Transport path 1 */
-                                       saa7164_irq_ts(&dev->ts1);
+                                       saa7164_irq_ts(porta);
 
-                               } else if (intid ==
-                                       dev->ts2.hwcfg.interruptid) {
+                               } else if (intid == portb->hwcfg.interruptid) {
 
                                        /* Transport path 2 */
-                                       saa7164_irq_ts(&dev->ts2);
+                                       saa7164_irq_ts(portb);
+
+                               } else if (intid == portc->hwcfg.interruptid) {
+
+                                       /* Encoder path 1 */
+                                       saa7164_irq_encoder(portc);
+
+                               } else if (intid == portd->hwcfg.interruptid) {
+
+                                       /* Encoder path 2 */
+                                       saa7164_irq_encoder(portd);
+
+                               } else if (intid == porte->hwcfg.interruptid) {
+
+                                       /* VBI path 1 */
+                                       saa7164_irq_vbi(porte);
+
+                               } else if (intid == portf->hwcfg.interruptid) {
+
+                                       /* VBI path 2 */
+                                       saa7164_irq_vbi(portf);
 
                                } else {
                                        /* Find the function */
@@ -286,8 +825,8 @@ void saa7164_dumpregs(struct saa7164_dev *dev, u32 addr)
 
 static void saa7164_dump_hwdesc(struct saa7164_dev *dev)
 {
-       dprintk(1, "@0x%p hwdesc sizeof(tmComResHWDescr_t) = %d bytes\n",
-               &dev->hwdesc, (u32)sizeof(tmComResHWDescr_t));
+       dprintk(1, "@0x%p hwdesc sizeof(struct tmComResHWDescr) = %d bytes\n",
+               &dev->hwdesc, (u32)sizeof(struct tmComResHWDescr));
 
        dprintk(1, " .bLength = 0x%x\n", dev->hwdesc.bLength);
        dprintk(1, " .bDescriptorType = 0x%x\n", dev->hwdesc.bDescriptorType);
@@ -317,8 +856,8 @@ static void saa7164_dump_hwdesc(struct saa7164_dev *dev)
 static void saa7164_dump_intfdesc(struct saa7164_dev *dev)
 {
        dprintk(1, "@0x%p intfdesc "
-               "sizeof(tmComResInterfaceDescr_t) = %d bytes\n",
-               &dev->intfdesc, (u32)sizeof(tmComResInterfaceDescr_t));
+               "sizeof(struct tmComResInterfaceDescr) = %d bytes\n",
+               &dev->intfdesc, (u32)sizeof(struct tmComResInterfaceDescr));
 
        dprintk(1, " .bLength = 0x%x\n", dev->intfdesc.bLength);
        dprintk(1, " .bDescriptorType = 0x%x\n", dev->intfdesc.bDescriptorType);
@@ -338,8 +877,8 @@ static void saa7164_dump_intfdesc(struct saa7164_dev *dev)
 
 static void saa7164_dump_busdesc(struct saa7164_dev *dev)
 {
-       dprintk(1, "@0x%p busdesc sizeof(tmComResBusDescr_t) = %d bytes\n",
-               &dev->busdesc, (u32)sizeof(tmComResBusDescr_t));
+       dprintk(1, "@0x%p busdesc sizeof(struct tmComResBusDescr) = %d bytes\n",
+               &dev->busdesc, (u32)sizeof(struct tmComResBusDescr));
 
        dprintk(1, " .CommandRing   = 0x%016Lx\n", dev->busdesc.CommandRing);
        dprintk(1, " .ResponseRing  = 0x%016Lx\n", dev->busdesc.ResponseRing);
@@ -356,23 +895,23 @@ static void saa7164_dump_busdesc(struct saa7164_dev *dev)
  */
 static void saa7164_get_descriptors(struct saa7164_dev *dev)
 {
-       memcpy(&dev->hwdesc, dev->bmmio, sizeof(tmComResHWDescr_t));
-       memcpy(&dev->intfdesc, dev->bmmio + sizeof(tmComResHWDescr_t),
-               sizeof(tmComResInterfaceDescr_t));
-       memcpy(&dev->busdesc, dev->bmmio + dev->intfdesc.BARLocation,
-               sizeof(tmComResBusDescr_t));
-
-       if (dev->hwdesc.bLength != sizeof(tmComResHWDescr_t)) {
-               printk(KERN_ERR "Structure tmComResHWDescr_t is mangled\n");
+       memcpy_fromio(&dev->hwdesc, dev->bmmio, sizeof(struct tmComResHWDescr));
+       memcpy_fromio(&dev->intfdesc, dev->bmmio + sizeof(struct tmComResHWDescr),
+               sizeof(struct tmComResInterfaceDescr));
+       memcpy_fromio(&dev->busdesc, dev->bmmio + dev->intfdesc.BARLocation,
+               sizeof(struct tmComResBusDescr));
+
+       if (dev->hwdesc.bLength != sizeof(struct tmComResHWDescr)) {
+               printk(KERN_ERR "Structure struct tmComResHWDescr is mangled\n");
                printk(KERN_ERR "Need %x got %d\n", dev->hwdesc.bLength,
-                       (u32)sizeof(tmComResHWDescr_t));
+                       (u32)sizeof(struct tmComResHWDescr));
        } else
                saa7164_dump_hwdesc(dev);
 
-       if (dev->intfdesc.bLength != sizeof(tmComResInterfaceDescr_t)) {
-               printk(KERN_ERR "struct tmComResInterfaceDescr_t is mangled\n");
+       if (dev->intfdesc.bLength != sizeof(struct tmComResInterfaceDescr)) {
+               printk(KERN_ERR "struct struct tmComResInterfaceDescr is mangled\n");
                printk(KERN_ERR "Need %x got %d\n", dev->intfdesc.bLength,
-                       (u32)sizeof(tmComResInterfaceDescr_t));
+                       (u32)sizeof(struct tmComResInterfaceDescr));
        } else
                saa7164_dump_intfdesc(dev);
 
@@ -402,6 +941,58 @@ static int get_resources(struct saa7164_dev *dev)
        return -EBUSY;
 }
 
+static int saa7164_port_init(struct saa7164_dev *dev, int portnr)
+{
+       struct saa7164_port *port = 0;
+
+       if ((portnr < 0) || (portnr >= SAA7164_MAX_PORTS))
+               BUG();
+
+       port = &dev->ports[portnr];
+
+       port->dev = dev;
+       port->nr = portnr;
+
+       if ((portnr == SAA7164_PORT_TS1) || (portnr == SAA7164_PORT_TS2))
+               port->type = SAA7164_MPEG_DVB;
+       else
+       if ((portnr == SAA7164_PORT_ENC1) || (portnr == SAA7164_PORT_ENC2)) {
+               port->type = SAA7164_MPEG_ENCODER;
+
+               /* We need a deferred interrupt handler for cmd handling */
+               INIT_WORK(&port->workenc, saa7164_work_enchandler);
+       }
+       else
+       if ((portnr == SAA7164_PORT_VBI1) || (portnr == SAA7164_PORT_VBI2)) {
+               port->type = SAA7164_MPEG_VBI;
+
+               /* We need a deferred interrupt handler for cmd handling */
+               INIT_WORK(&port->workenc, saa7164_work_vbihandler);
+       } else
+               BUG();
+
+       /* Init all the critical resources */
+       mutex_init(&port->dvb.lock);
+       INIT_LIST_HEAD(&port->dmaqueue.list);
+       mutex_init(&port->dmaqueue_lock);
+
+       INIT_LIST_HEAD(&port->list_buf_used.list);
+       INIT_LIST_HEAD(&port->list_buf_free.list);
+       init_waitqueue_head(&port->wait_read);
+
+
+       saa7164_histogram_reset(&port->irq_interval, "irq intervals");
+       saa7164_histogram_reset(&port->svc_interval, "deferred intervals");
+       saa7164_histogram_reset(&port->irq_svc_interval,
+               "irq to deferred intervals");
+       saa7164_histogram_reset(&port->read_interval,
+               "encoder/vbi read() intervals");
+       saa7164_histogram_reset(&port->poll_interval,
+               "encoder/vbi poll() intervals");
+
+       return 0;
+}
+
 static int saa7164_dev_setup(struct saa7164_dev *dev)
 {
        int i;
@@ -443,23 +1034,13 @@ static int saa7164_dev_setup(struct saa7164_dev *dev)
        dev->i2c_bus[2].dev = dev;
        dev->i2c_bus[2].nr = 2;
 
-       /* Transport port A Defaults / setup */
-       dev->ts1.dev = dev;
-       dev->ts1.nr = 0;
-       mutex_init(&dev->ts1.dvb.lock);
-       INIT_LIST_HEAD(&dev->ts1.dmaqueue.list);
-       INIT_LIST_HEAD(&dev->ts1.dummy_dmaqueue.list);
-       mutex_init(&dev->ts1.dmaqueue_lock);
-       mutex_init(&dev->ts1.dummy_dmaqueue_lock);
-
-       /* Transport port B Defaults / setup */
-       dev->ts2.dev = dev;
-       dev->ts2.nr = 1;
-       mutex_init(&dev->ts2.dvb.lock);
-       INIT_LIST_HEAD(&dev->ts2.dmaqueue.list);
-       INIT_LIST_HEAD(&dev->ts2.dummy_dmaqueue.list);
-       mutex_init(&dev->ts2.dmaqueue_lock);
-       mutex_init(&dev->ts2.dummy_dmaqueue_lock);
+       /* Transport + Encoder ports 1, 2, 3, 4 - Defaults / setup */
+       saa7164_port_init(dev, SAA7164_PORT_TS1);
+       saa7164_port_init(dev, SAA7164_PORT_TS2);
+       saa7164_port_init(dev, SAA7164_PORT_ENC1);
+       saa7164_port_init(dev, SAA7164_PORT_ENC2);
+       saa7164_port_init(dev, SAA7164_PORT_VBI1);
+       saa7164_port_init(dev, SAA7164_PORT_VBI2);
 
        if (get_resources(dev) < 0) {
                printk(KERN_ERR "CORE %s No more PCIe resources for "
@@ -516,6 +1097,132 @@ static void saa7164_dev_unregister(struct saa7164_dev *dev)
        return;
 }
 
+#ifdef CONFIG_PROC_FS
+static int saa7164_proc_show(struct seq_file *m, void *v)
+{
+       struct saa7164_dev *dev;
+       struct tmComResBusInfo *b;
+       struct list_head *list;
+       int i, c;
+
+       if (saa7164_devcount == 0)
+               return 0;
+
+       list_for_each(list, &saa7164_devlist) {
+               dev = list_entry(list, struct saa7164_dev, devlist);
+               seq_printf(m, "%s = %p\n", dev->name, dev);
+
+               /* Lock the bus from any other access */
+               b = &dev->bus;
+               mutex_lock(&b->lock);
+
+               seq_printf(m, " .m_pdwSetWritePos = 0x%x (0x%08x)\n",
+                       b->m_dwSetReadPos, saa7164_readl(b->m_dwSetReadPos));
+
+               seq_printf(m, " .m_pdwSetReadPos  = 0x%x (0x%08x)\n",
+                       b->m_dwSetWritePos, saa7164_readl(b->m_dwSetWritePos));
+
+               seq_printf(m, " .m_pdwGetWritePos = 0x%x (0x%08x)\n",
+                       b->m_dwGetReadPos, saa7164_readl(b->m_dwGetReadPos));
+
+               seq_printf(m, " .m_pdwGetReadPos  = 0x%x (0x%08x)\n",
+                       b->m_dwGetWritePos, saa7164_readl(b->m_dwGetWritePos));
+               c = 0;
+               seq_printf(m, "\n  Set Ring:\n");
+               seq_printf(m, "\n addr  00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f\n");
+               for (i = 0; i < b->m_dwSizeSetRing; i++) {
+                       if (c == 0)
+                               seq_printf(m, " %04x:", i);
+
+                       seq_printf(m, " %02x", *(b->m_pdwSetRing + i));
+
+                       if (++c == 16) {
+                               seq_printf(m, "\n");
+                               c = 0;
+                       }
+               }
+
+               c = 0;
+               seq_printf(m, "\n  Get Ring:\n");
+               seq_printf(m, "\n addr  00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f\n");
+               for (i = 0; i < b->m_dwSizeGetRing; i++) {
+                       if (c == 0)
+                               seq_printf(m, " %04x:", i);
+
+                       seq_printf(m, " %02x", *(b->m_pdwGetRing + i));
+
+                       if (++c == 16) {
+                               seq_printf(m, "\n");
+                               c = 0;
+                       }
+               }
+
+               mutex_unlock(&b->lock);
+
+       }
+
+       return 0;
+}
+
+static int saa7164_proc_open(struct inode *inode, struct file *filp)
+{
+       return single_open(filp, saa7164_proc_show, NULL);
+}
+
+static struct file_operations saa7164_proc_fops = {
+       .open           = saa7164_proc_open,
+       .read           = seq_read,
+       .llseek         = seq_lseek,
+       .release        = single_release,
+};
+
+static int saa7164_proc_create(void)
+{
+       struct proc_dir_entry *pe;
+
+       pe = proc_create("saa7164", S_IRUGO, NULL, &saa7164_proc_fops);
+       if (!pe)
+               return -ENOMEM;
+
+       return 0;
+}
+#endif
+
+static int saa7164_thread_function(void *data)
+{
+       struct saa7164_dev *dev = data;
+       struct tmFwInfoStruct fwinfo;
+       u64 last_poll_time = 0;
+
+       dprintk(DBGLVL_THR, "thread started\n");
+
+       set_freezable();
+
+       while (1) {
+               msleep_interruptible(100);
+               if (kthread_should_stop())
+                       break;
+               try_to_freeze();
+
+               dprintk(DBGLVL_THR, "thread running\n");
+
+               /* Dump the firmware debug message to console */
+               /* Polling this costs us 1-2% of the arm CPU */
+               /* convert this into a respnde to interrupt 0x7a */
+               saa7164_api_collect_debug(dev);
+
+               /* Monitor CPU load every 1 second */
+               if ((last_poll_time + 1000 /* ms */) < jiffies_to_msecs(jiffies)) {
+                       saa7164_api_get_load_info(dev, &fwinfo);
+                       last_poll_time = jiffies_to_msecs(jiffies);
+               }
+
+       }
+
+       dprintk(DBGLVL_THR, "thread exiting\n");
+       return 0;
+}
+
 static int __devinit saa7164_initdev(struct pci_dev *pci_dev,
                                     const struct pci_device_id *pci_id)
 {
@@ -622,7 +1329,6 @@ static int __devinit saa7164_initdev(struct pci_dev *pci_dev,
                saa7164_gpio_setup(dev);
                saa7164_card_setup(dev);
 
-
                /* Parse the dynamic device configuration, find various
                 * media endpoints (MPEG, WMV, PS, TS) and cache their
                 * configuration details into the driver, so we can
@@ -633,7 +1339,7 @@ static int __devinit saa7164_initdev(struct pci_dev *pci_dev,
 
                /* Begin to create the video sub-systems and register funcs */
                if (saa7164_boards[dev->board].porta == SAA7164_MPEG_DVB) {
-                       if (saa7164_dvb_register(&dev->ts1) < 0) {
+                       if (saa7164_dvb_register(&dev->ports[SAA7164_PORT_TS1]) < 0) {
                                printk(KERN_ERR "%s() Failed to register "
                                        "dvb adapters on porta\n",
                                        __func__);
@@ -641,13 +1347,50 @@ static int __devinit saa7164_initdev(struct pci_dev *pci_dev,
                }
 
                if (saa7164_boards[dev->board].portb == SAA7164_MPEG_DVB) {
-                       if (saa7164_dvb_register(&dev->ts2) < 0) {
+                       if (saa7164_dvb_register(&dev->ports[SAA7164_PORT_TS2]) < 0) {
                                printk(KERN_ERR"%s() Failed to register "
                                        "dvb adapters on portb\n",
                                        __func__);
                        }
                }
 
+               if (saa7164_boards[dev->board].portc == SAA7164_MPEG_ENCODER) {
+                       if (saa7164_encoder_register(&dev->ports[SAA7164_PORT_ENC1]) < 0) {
+                               printk(KERN_ERR"%s() Failed to register "
+                                       "mpeg encoder\n", __func__);
+                       }
+               }
+
+               if (saa7164_boards[dev->board].portd == SAA7164_MPEG_ENCODER) {
+                       if (saa7164_encoder_register(&dev->ports[SAA7164_PORT_ENC2]) < 0) {
+                               printk(KERN_ERR"%s() Failed to register "
+                                       "mpeg encoder\n", __func__);
+                       }
+               }
+
+               if (saa7164_boards[dev->board].porte == SAA7164_MPEG_VBI) {
+                       if (saa7164_vbi_register(&dev->ports[SAA7164_PORT_VBI1]) < 0) {
+                               printk(KERN_ERR"%s() Failed to register "
+                                       "vbi device\n", __func__);
+                       }
+               }
+
+               if (saa7164_boards[dev->board].portf == SAA7164_MPEG_VBI) {
+                       if (saa7164_vbi_register(&dev->ports[SAA7164_PORT_VBI2]) < 0) {
+                               printk(KERN_ERR"%s() Failed to register "
+                                       "vbi device\n", __func__);
+                       }
+               }
+               saa7164_api_set_debug(dev, fw_debug);
+
+               if (fw_debug) {
+                       dev->kthread = kthread_run(saa7164_thread_function, dev,
+                               "saa7164 debug");
+                       if (!dev->kthread)
+                               printk(KERN_ERR "%s() Failed to create "
+                                       "debug kernel thread\n", __func__);
+               }
+
        } /* != BOARD_UNKNOWN */
        else
                printk(KERN_ERR "%s() Unsupported board detected, "
@@ -675,13 +1418,49 @@ static void __devexit saa7164_finidev(struct pci_dev *pci_dev)
 {
        struct saa7164_dev *dev = pci_get_drvdata(pci_dev);
 
+       if (dev->board != SAA7164_BOARD_UNKNOWN) {
+               if (fw_debug && dev->kthread) {
+                       kthread_stop(dev->kthread);
+                       dev->kthread = NULL;
+               }
+               if (dev->firmwareloaded)
+                       saa7164_api_set_debug(dev, 0x00);
+       }
+
+       saa7164_histogram_print(&dev->ports[SAA7164_PORT_ENC1],
+               &dev->ports[SAA7164_PORT_ENC1].irq_interval);
+       saa7164_histogram_print(&dev->ports[SAA7164_PORT_ENC1],
+               &dev->ports[SAA7164_PORT_ENC1].svc_interval);
+       saa7164_histogram_print(&dev->ports[SAA7164_PORT_ENC1],
+               &dev->ports[SAA7164_PORT_ENC1].irq_svc_interval);
+       saa7164_histogram_print(&dev->ports[SAA7164_PORT_ENC1],
+               &dev->ports[SAA7164_PORT_ENC1].read_interval);
+       saa7164_histogram_print(&dev->ports[SAA7164_PORT_ENC1],
+               &dev->ports[SAA7164_PORT_ENC1].poll_interval);
+       saa7164_histogram_print(&dev->ports[SAA7164_PORT_VBI1],
+               &dev->ports[SAA7164_PORT_VBI1].read_interval);
+       saa7164_histogram_print(&dev->ports[SAA7164_PORT_VBI2],
+               &dev->ports[SAA7164_PORT_VBI2].poll_interval);
+
        saa7164_shutdown(dev);
 
        if (saa7164_boards[dev->board].porta == SAA7164_MPEG_DVB)
-               saa7164_dvb_unregister(&dev->ts1);
+               saa7164_dvb_unregister(&dev->ports[SAA7164_PORT_TS1]);
 
        if (saa7164_boards[dev->board].portb == SAA7164_MPEG_DVB)
-               saa7164_dvb_unregister(&dev->ts2);
+               saa7164_dvb_unregister(&dev->ports[SAA7164_PORT_TS2]);
+
+       if (saa7164_boards[dev->board].portc == SAA7164_MPEG_ENCODER)
+               saa7164_encoder_unregister(&dev->ports[SAA7164_PORT_ENC1]);
+
+       if (saa7164_boards[dev->board].portd == SAA7164_MPEG_ENCODER)
+               saa7164_encoder_unregister(&dev->ports[SAA7164_PORT_ENC2]);
+
+       if (saa7164_boards[dev->board].porte == SAA7164_MPEG_VBI)
+               saa7164_vbi_unregister(&dev->ports[SAA7164_PORT_VBI1]);
+
+       if (saa7164_boards[dev->board].portf == SAA7164_MPEG_VBI)
+               saa7164_vbi_unregister(&dev->ports[SAA7164_PORT_VBI2]);
 
        saa7164_i2c_unregister(&dev->i2c_bus[0]);
        saa7164_i2c_unregister(&dev->i2c_bus[1]);
@@ -727,11 +1506,18 @@ static struct pci_driver saa7164_pci_driver = {
 static int __init saa7164_init(void)
 {
        printk(KERN_INFO "saa7164 driver loaded\n");
+
+#ifdef CONFIG_PROC_FS
+       saa7164_proc_create();
+#endif
        return pci_register_driver(&saa7164_pci_driver);
 }
 
 static void __exit saa7164_fini(void)
 {
+#ifdef CONFIG_PROC_FS
+       remove_proc_entry("saa7164", NULL);
+#endif
        pci_unregister_driver(&saa7164_pci_driver);
 }