const: mark struct vm_struct_operations
[linux-2.6.git] / drivers / media / video / zoran / zoran_driver.c
index ed8ac66..47137de 100644 (file)
@@ -49,6 +49,7 @@
 #include <linux/module.h>
 #include <linux/delay.h>
 #include <linux/slab.h>
+#include <linux/smp_lock.h>
 #include <linux/pci.h>
 #include <linux/vmalloc.h>
 #include <linux/wait.h>
 #include <linux/i2c-algo-bit.h>
 
 #include <linux/spinlock.h>
-#define     MAP_NR(x)       virt_to_page(x)
-#define     ZORAN_VID_TYPE  ( \
-                               VID_TYPE_CAPTURE | \
-                               VID_TYPE_OVERLAY | \
-                               VID_TYPE_CLIPPING | \
-                               VID_TYPE_FRAMERAM | \
-                               VID_TYPE_SCALES | \
-                               VID_TYPE_MJPEG_DECODER | \
-                               VID_TYPE_MJPEG_ENCODER \
-                            )
 
 #include <linux/videodev.h>
 #include <media/v4l2-common.h>
 #include <asm/uaccess.h>
 #include <linux/proc_fs.h>
 
-#include <linux/video_decoder.h>
-#include <linux/video_encoder.h>
 #include <linux/mutex.h>
 #include "zoran.h"
 #include "zoran_device.h"
 #include "zoran_card.h"
 
-       /* we declare some card type definitions here, they mean
-        * the same as the v4l1 ZORAN_VID_TYPE above, except it's v4l2 */
-#define ZORAN_V4L2_VID_FLAGS ( \
-                               V4L2_CAP_STREAMING |\
-                               V4L2_CAP_VIDEO_CAPTURE |\
-                               V4L2_CAP_VIDEO_OUTPUT |\
-                               V4L2_CAP_VIDEO_OVERLAY \
-                             )
-
-
-#if defined(CONFIG_VIDEO_V4L1_COMPAT)
-#define ZFMT(pal, fcc, cs) \
-       .palette = (pal), .fourcc = (fcc), .colorspace = (cs)
-#else
-#define ZFMT(pal, fcc, cs) \
-       .fourcc = (fcc), .colorspace = (cs)
-#endif
 
 const struct zoran_format zoran_formats[] = {
        {
                .name = "15-bit RGB LE",
-               ZFMT(VIDEO_PALETTE_RGB555,
-                    V4L2_PIX_FMT_RGB555, V4L2_COLORSPACE_SRGB),
+               .fourcc = V4L2_PIX_FMT_RGB555,
+               .colorspace = V4L2_COLORSPACE_SRGB,
                .depth = 15,
                .flags = ZORAN_FORMAT_CAPTURE |
                         ZORAN_FORMAT_OVERLAY,
@@ -116,16 +88,16 @@ const struct zoran_format zoran_formats[] = {
                           ZR36057_VFESPFR_LittleEndian,
        }, {
                .name = "15-bit RGB BE",
-               ZFMT(-1,
-                    V4L2_PIX_FMT_RGB555X, V4L2_COLORSPACE_SRGB),
+               .fourcc = V4L2_PIX_FMT_RGB555X,
+               .colorspace = V4L2_COLORSPACE_SRGB,
                .depth = 15,
                .flags = ZORAN_FORMAT_CAPTURE |
                         ZORAN_FORMAT_OVERLAY,
                .vfespfr = ZR36057_VFESPFR_RGB555|ZR36057_VFESPFR_ErrDif,
        }, {
                .name = "16-bit RGB LE",
-               ZFMT(VIDEO_PALETTE_RGB565,
-                    V4L2_PIX_FMT_RGB565, V4L2_COLORSPACE_SRGB),
+               .fourcc = V4L2_PIX_FMT_RGB565,
+               .colorspace = V4L2_COLORSPACE_SRGB,
                .depth = 16,
                .flags = ZORAN_FORMAT_CAPTURE |
                         ZORAN_FORMAT_OVERLAY,
@@ -133,56 +105,56 @@ const struct zoran_format zoran_formats[] = {
                           ZR36057_VFESPFR_LittleEndian,
        }, {
                .name = "16-bit RGB BE",
-               ZFMT(-1,
-                    V4L2_PIX_FMT_RGB565X, V4L2_COLORSPACE_SRGB),
+               .fourcc = V4L2_PIX_FMT_RGB565X,
+               .colorspace = V4L2_COLORSPACE_SRGB,
                .depth = 16,
                .flags = ZORAN_FORMAT_CAPTURE |
                         ZORAN_FORMAT_OVERLAY,
                .vfespfr = ZR36057_VFESPFR_RGB565|ZR36057_VFESPFR_ErrDif,
        }, {
                .name = "24-bit RGB",
-               ZFMT(VIDEO_PALETTE_RGB24,
-                    V4L2_PIX_FMT_BGR24, V4L2_COLORSPACE_SRGB),
+               .fourcc = V4L2_PIX_FMT_BGR24,
+               .colorspace = V4L2_COLORSPACE_SRGB,
                .depth = 24,
                .flags = ZORAN_FORMAT_CAPTURE |
                         ZORAN_FORMAT_OVERLAY,
                .vfespfr = ZR36057_VFESPFR_RGB888|ZR36057_VFESPFR_Pack24,
        }, {
                .name = "32-bit RGB LE",
-               ZFMT(VIDEO_PALETTE_RGB32,
-                    V4L2_PIX_FMT_BGR32, V4L2_COLORSPACE_SRGB),
+               .fourcc = V4L2_PIX_FMT_BGR32,
+               .colorspace = V4L2_COLORSPACE_SRGB,
                .depth = 32,
                .flags = ZORAN_FORMAT_CAPTURE |
                         ZORAN_FORMAT_OVERLAY,
                .vfespfr = ZR36057_VFESPFR_RGB888|ZR36057_VFESPFR_LittleEndian,
        }, {
                .name = "32-bit RGB BE",
-               ZFMT(-1,
-                    V4L2_PIX_FMT_RGB32, V4L2_COLORSPACE_SRGB),
+               .fourcc = V4L2_PIX_FMT_RGB32,
+               .colorspace = V4L2_COLORSPACE_SRGB,
                .depth = 32,
                .flags = ZORAN_FORMAT_CAPTURE |
                         ZORAN_FORMAT_OVERLAY,
                .vfespfr = ZR36057_VFESPFR_RGB888,
        }, {
                .name = "4:2:2, packed, YUYV",
-               ZFMT(VIDEO_PALETTE_YUV422,
-                    V4L2_PIX_FMT_YUYV, V4L2_COLORSPACE_SMPTE170M),
+               .fourcc = V4L2_PIX_FMT_YUYV,
+               .colorspace = V4L2_COLORSPACE_SMPTE170M,
                .depth = 16,
                .flags = ZORAN_FORMAT_CAPTURE |
                         ZORAN_FORMAT_OVERLAY,
                .vfespfr = ZR36057_VFESPFR_YUV422,
        }, {
                .name = "4:2:2, packed, UYVY",
-               ZFMT(VIDEO_PALETTE_UYVY,
-                    V4L2_PIX_FMT_UYVY, V4L2_COLORSPACE_SMPTE170M),
+               .fourcc = V4L2_PIX_FMT_UYVY,
+               .colorspace = V4L2_COLORSPACE_SMPTE170M,
                .depth = 16,
                .flags = ZORAN_FORMAT_CAPTURE |
                         ZORAN_FORMAT_OVERLAY,
                .vfespfr = ZR36057_VFESPFR_YUV422|ZR36057_VFESPFR_LittleEndian,
        }, {
                .name = "Hardware-encoded Motion-JPEG",
-               ZFMT(-1,
-                    V4L2_PIX_FMT_MJPEG, V4L2_COLORSPACE_SMPTE170M),
+               .fourcc = V4L2_PIX_FMT_MJPEG,
+               .colorspace = V4L2_COLORSPACE_SMPTE170M,
                .depth = 0,
                .flags = ZORAN_FORMAT_CAPTURE |
                         ZORAN_FORMAT_PLAYBACK |
@@ -191,10 +163,6 @@ const struct zoran_format zoran_formats[] = {
 };
 #define NUM_FORMATS ARRAY_SIZE(zoran_formats)
 
-static int lock_norm;  /* 0 = default 1 = Don't change TV standard (norm) */
-module_param(lock_norm, int, 0644);
-MODULE_PARM_DESC(lock_norm, "Prevent norm changes (1 = ignore, >1 = fail)");
-
        /* small helper function for calculating buffersizes for v4l2
         * we calculate the nearest higher power-of-two, which
         * will be the recommended buffersize */
@@ -219,8 +187,26 @@ zoran_v4l2_calc_bufsize (struct zoran_jpg_settings *settings)
 }
 
 /* forward references */
-static void v4l_fbuffer_free(struct file *file);
-static void jpg_fbuffer_free(struct file *file);
+static void v4l_fbuffer_free(struct zoran_fh *fh);
+static void jpg_fbuffer_free(struct zoran_fh *fh);
+
+/* Set mapping mode */
+static void map_mode_raw(struct zoran_fh *fh)
+{
+       fh->map_mode = ZORAN_MAP_MODE_RAW;
+       fh->buffers.buffer_size = v4l_bufsize;
+       fh->buffers.num_buffers = v4l_nbufs;
+}
+static void map_mode_jpg(struct zoran_fh *fh, int play)
+{
+       fh->map_mode = play ? ZORAN_MAP_MODE_JPG_PLAY : ZORAN_MAP_MODE_JPG_REC;
+       fh->buffers.buffer_size = jpg_bufsize;
+       fh->buffers.num_buffers = jpg_nbufs;
+}
+static inline const char *mode_name(enum zoran_map_mode mode)
+{
+       return mode == ZORAN_MAP_MODE_RAW ? "V4L" : "JPG";
+}
 
 /*
  *   Allocate the V4L grab buffers
@@ -228,75 +214,70 @@ static void jpg_fbuffer_free(struct file *file);
  *   These have to be pysically contiguous.
  */
 
-static int
-v4l_fbuffer_alloc (struct file *file)
+static int v4l_fbuffer_alloc(struct zoran_fh *fh)
 {
-       struct zoran_fh *fh = file->private_data;
        struct zoran *zr = fh->zr;
        int i, off;
        unsigned char *mem;
 
-       for (i = 0; i < fh->v4l_buffers.num_buffers; i++) {
-               if (fh->v4l_buffers.buffer[i].fbuffer)
+       for (i = 0; i < fh->buffers.num_buffers; i++) {
+               if (fh->buffers.buffer[i].v4l.fbuffer)
                        dprintk(2,
                                KERN_WARNING
-                               "%s: v4l_fbuffer_alloc() - buffer %d already allocated!?\n",
-                               ZR_DEVNAME(zr), i);
+                               "%s: %s - buffer %d already allocated!?\n",
+                               ZR_DEVNAME(zr), __func__, i);
 
                //udelay(20);
-               mem = kmalloc(fh->v4l_buffers.buffer_size, GFP_KERNEL);
+               mem = kmalloc(fh->buffers.buffer_size,
+                             GFP_KERNEL | __GFP_NOWARN);
                if (!mem) {
                        dprintk(1,
                                KERN_ERR
-                               "%s: v4l_fbuffer_alloc() - kmalloc for V4L buf %d failed\n",
-                               ZR_DEVNAME(zr), i);
-                       v4l_fbuffer_free(file);
+                               "%s: %s - kmalloc for V4L buf %d failed\n",
+                               ZR_DEVNAME(zr), __func__, i);
+                       v4l_fbuffer_free(fh);
                        return -ENOBUFS;
                }
-               fh->v4l_buffers.buffer[i].fbuffer = mem;
-               fh->v4l_buffers.buffer[i].fbuffer_phys =
-                   virt_to_phys(mem);
-               fh->v4l_buffers.buffer[i].fbuffer_bus =
-                   virt_to_bus(mem);
-               for (off = 0; off < fh->v4l_buffers.buffer_size;
+               fh->buffers.buffer[i].v4l.fbuffer = mem;
+               fh->buffers.buffer[i].v4l.fbuffer_phys = virt_to_phys(mem);
+               fh->buffers.buffer[i].v4l.fbuffer_bus = virt_to_bus(mem);
+               for (off = 0; off < fh->buffers.buffer_size;
                     off += PAGE_SIZE)
-                       SetPageReserved(MAP_NR(mem + off));
+                       SetPageReserved(virt_to_page(mem + off));
                dprintk(4,
                        KERN_INFO
-                       "%s: v4l_fbuffer_alloc() - V4L frame %d mem 0x%lx (bus: 0x%lx)\n",
-                       ZR_DEVNAME(zr), i, (unsigned long) mem,
-                       virt_to_bus(mem));
+                       "%s: %s - V4L frame %d mem 0x%lx (bus: 0x%llx)\n",
+                       ZR_DEVNAME(zr), __func__, i, (unsigned long) mem,
+                       (unsigned long long)virt_to_bus(mem));
        }
 
-       fh->v4l_buffers.allocated = 1;
+       fh->buffers.allocated = 1;
 
        return 0;
 }
 
 /* free the V4L grab buffers */
-static void
-v4l_fbuffer_free (struct file *file)
+static void v4l_fbuffer_free(struct zoran_fh *fh)
 {
-       struct zoran_fh *fh = file->private_data;
        struct zoran *zr = fh->zr;
        int i, off;
        unsigned char *mem;
 
-       dprintk(4, KERN_INFO "%s: v4l_fbuffer_free()\n", ZR_DEVNAME(zr));
+       dprintk(4, KERN_INFO "%s: %s\n", ZR_DEVNAME(zr), __func__);
 
-       for (i = 0; i < fh->v4l_buffers.num_buffers; i++) {
-               if (!fh->v4l_buffers.buffer[i].fbuffer)
+       for (i = 0; i < fh->buffers.num_buffers; i++) {
+               if (!fh->buffers.buffer[i].v4l.fbuffer)
                        continue;
 
-               mem = fh->v4l_buffers.buffer[i].fbuffer;
-               for (off = 0; off < fh->v4l_buffers.buffer_size;
+               mem = fh->buffers.buffer[i].v4l.fbuffer;
+               for (off = 0; off < fh->buffers.buffer_size;
                     off += PAGE_SIZE)
-                       ClearPageReserved(MAP_NR(mem + off));
-               kfree((void *) fh->v4l_buffers.buffer[i].fbuffer);
-               fh->v4l_buffers.buffer[i].fbuffer = NULL;
+                       ClearPageReserved(virt_to_page(mem + off));
+               kfree(fh->buffers.buffer[i].v4l.fbuffer);
+               fh->buffers.buffer[i].v4l.fbuffer = NULL;
        }
 
-       fh->v4l_buffers.allocated = 0;
+       fh->buffers.allocated = 0;
 }
 
 /*
@@ -327,156 +308,128 @@ v4l_fbuffer_free (struct file *file)
  *       and fragment buffers are not little-endian.
  */
 
-static int
-jpg_fbuffer_alloc (struct file *file)
+static int jpg_fbuffer_alloc(struct zoran_fh *fh)
 {
-       struct zoran_fh *fh = file->private_data;
        struct zoran *zr = fh->zr;
        int i, j, off;
-       unsigned long mem;
+       u8 *mem;
 
-       for (i = 0; i < fh->jpg_buffers.num_buffers; i++) {
-               if (fh->jpg_buffers.buffer[i].frag_tab)
+       for (i = 0; i < fh->buffers.num_buffers; i++) {
+               if (fh->buffers.buffer[i].jpg.frag_tab)
                        dprintk(2,
                                KERN_WARNING
-                               "%s: jpg_fbuffer_alloc() - buffer %d already allocated!?\n",
-                               ZR_DEVNAME(zr), i);
+                               "%s: %s - buffer %d already allocated!?\n",
+                               ZR_DEVNAME(zr), __func__, i);
 
                /* Allocate fragment table for this buffer */
 
-               mem = get_zeroed_page(GFP_KERNEL);
+               mem = (void *)get_zeroed_page(GFP_KERNEL);
                if (mem == 0) {
                        dprintk(1,
                                KERN_ERR
-                               "%s: jpg_fbuffer_alloc() - get_zeroed_page (frag_tab) failed for buffer %d\n",
-                               ZR_DEVNAME(zr), i);
-                       jpg_fbuffer_free(file);
+                               "%s: %s - get_zeroed_page (frag_tab) failed for buffer %d\n",
+                               ZR_DEVNAME(zr), __func__, i);
+                       jpg_fbuffer_free(fh);
                        return -ENOBUFS;
                }
-               fh->jpg_buffers.buffer[i].frag_tab = (__le32 *) mem;
-               fh->jpg_buffers.buffer[i].frag_tab_bus =
-                   virt_to_bus((void *) mem);
-
-               //if (alloc_contig) {
-               if (fh->jpg_buffers.need_contiguous) {
-                       mem =
-                           (unsigned long) kmalloc(fh->jpg_buffers.
-                                                   buffer_size,
-                                                   GFP_KERNEL);
-                       if (mem == 0) {
+               fh->buffers.buffer[i].jpg.frag_tab = (__le32 *)mem;
+               fh->buffers.buffer[i].jpg.frag_tab_bus = virt_to_bus(mem);
+
+               if (fh->buffers.need_contiguous) {
+                       mem = kmalloc(fh->buffers.buffer_size, GFP_KERNEL);
+                       if (mem == NULL) {
                                dprintk(1,
                                        KERN_ERR
-                                       "%s: jpg_fbuffer_alloc() - kmalloc failed for buffer %d\n",
-                                       ZR_DEVNAME(zr), i);
-                               jpg_fbuffer_free(file);
+                                       "%s: %s - kmalloc failed for buffer %d\n",
+                                       ZR_DEVNAME(zr), __func__, i);
+                               jpg_fbuffer_free(fh);
                                return -ENOBUFS;
                        }
-                       fh->jpg_buffers.buffer[i].frag_tab[0] =
-                           cpu_to_le32(virt_to_bus((void *) mem));
-                       fh->jpg_buffers.buffer[i].frag_tab[1] =
-                           cpu_to_le32(((fh->jpg_buffers.buffer_size / 4) << 1) | 1);
-                       for (off = 0; off < fh->jpg_buffers.buffer_size;
-                            off += PAGE_SIZE)
-                               SetPageReserved(MAP_NR(mem + off));
+                       fh->buffers.buffer[i].jpg.frag_tab[0] =
+                               cpu_to_le32(virt_to_bus(mem));
+                       fh->buffers.buffer[i].jpg.frag_tab[1] =
+                               cpu_to_le32((fh->buffers.buffer_size >> 1) | 1);
+                       for (off = 0; off < fh->buffers.buffer_size; off += PAGE_SIZE)
+                               SetPageReserved(virt_to_page(mem + off));
                } else {
                        /* jpg_bufsize is already page aligned */
-                       for (j = 0;
-                            j < fh->jpg_buffers.buffer_size / PAGE_SIZE;
-                            j++) {
-                               mem = get_zeroed_page(GFP_KERNEL);
-                               if (mem == 0) {
+                       for (j = 0; j < fh->buffers.buffer_size / PAGE_SIZE; j++) {
+                               mem = (void *)get_zeroed_page(GFP_KERNEL);
+                               if (mem == NULL) {
                                        dprintk(1,
                                                KERN_ERR
-                                               "%s: jpg_fbuffer_alloc() - get_zeroed_page failed for buffer %d\n",
-                                               ZR_DEVNAME(zr), i);
-                                       jpg_fbuffer_free(file);
+                                               "%s: %s - get_zeroed_page failed for buffer %d\n",
+                                               ZR_DEVNAME(zr), __func__, i);
+                                       jpg_fbuffer_free(fh);
                                        return -ENOBUFS;
                                }
 
-                               fh->jpg_buffers.buffer[i].frag_tab[2 * j] =
-                                   cpu_to_le32(virt_to_bus((void *) mem));
-                               fh->jpg_buffers.buffer[i].frag_tab[2 * j +
-                                                                  1] =
-                                   cpu_to_le32((PAGE_SIZE / 4) << 1);
-                               SetPageReserved(MAP_NR(mem));
+                               fh->buffers.buffer[i].jpg.frag_tab[2 * j] =
+                                       cpu_to_le32(virt_to_bus(mem));
+                               fh->buffers.buffer[i].jpg.frag_tab[2 * j + 1] =
+                                       cpu_to_le32((PAGE_SIZE >> 2) << 1);
+                               SetPageReserved(virt_to_page(mem));
                        }
 
-                       fh->jpg_buffers.buffer[i].frag_tab[2 * j - 1] |= cpu_to_le32(1);
+                       fh->buffers.buffer[i].jpg.frag_tab[2 * j - 1] |= cpu_to_le32(1);
                }
        }
 
        dprintk(4,
-               KERN_DEBUG "%s: jpg_fbuffer_alloc() - %d KB allocated\n",
-               ZR_DEVNAME(zr),
-               (fh->jpg_buffers.num_buffers *
-                fh->jpg_buffers.buffer_size) >> 10);
+               KERN_DEBUG "%s: %s - %d KB allocated\n",
+               ZR_DEVNAME(zr), __func__,
+               (fh->buffers.num_buffers * fh->buffers.buffer_size) >> 10);
 
-       fh->jpg_buffers.allocated = 1;
+       fh->buffers.allocated = 1;
 
        return 0;
 }
 
 /* free the MJPEG grab buffers */
-static void
-jpg_fbuffer_free (struct file *file)
+static void jpg_fbuffer_free(struct zoran_fh *fh)
 {
-       struct zoran_fh *fh = file->private_data;
        struct zoran *zr = fh->zr;
        int i, j, off;
        unsigned char *mem;
+       __le32 frag_tab;
+       struct zoran_buffer *buffer;
 
-       dprintk(4, KERN_DEBUG "%s: jpg_fbuffer_free()\n", ZR_DEVNAME(zr));
+       dprintk(4, KERN_DEBUG "%s: %s\n", ZR_DEVNAME(zr), __func__);
 
-       for (i = 0; i < fh->jpg_buffers.num_buffers; i++) {
-               if (!fh->jpg_buffers.buffer[i].frag_tab)
+       for (i = 0, buffer = &fh->buffers.buffer[0];
+            i < fh->buffers.num_buffers; i++, buffer++) {
+               if (!buffer->jpg.frag_tab)
                        continue;
 
-               //if (alloc_contig) {
-               if (fh->jpg_buffers.need_contiguous) {
-                       if (fh->jpg_buffers.buffer[i].frag_tab[0]) {
-                               mem = (unsigned char *) bus_to_virt(le32_to_cpu(
-                                       fh->jpg_buffers.buffer[i].frag_tab[0]));
-                               for (off = 0;
-                                    off < fh->jpg_buffers.buffer_size;
-                                    off += PAGE_SIZE)
-                                       ClearPageReserved(MAP_NR
-                                                         (mem + off));
+               if (fh->buffers.need_contiguous) {
+                       frag_tab = buffer->jpg.frag_tab[0];
+
+                       if (frag_tab) {
+                               mem = bus_to_virt(le32_to_cpu(frag_tab));
+                               for (off = 0; off < fh->buffers.buffer_size; off += PAGE_SIZE)
+                                       ClearPageReserved(virt_to_page(mem + off));
                                kfree(mem);
-                               fh->jpg_buffers.buffer[i].frag_tab[0] = 0;
-                               fh->jpg_buffers.buffer[i].frag_tab[1] = 0;
+                               buffer->jpg.frag_tab[0] = 0;
+                               buffer->jpg.frag_tab[1] = 0;
                        }
                } else {
-                       for (j = 0;
-                            j < fh->jpg_buffers.buffer_size / PAGE_SIZE;
-                            j++) {
-                               if (!fh->jpg_buffers.buffer[i].
-                                   frag_tab[2 * j])
+                       for (j = 0; j < fh->buffers.buffer_size / PAGE_SIZE; j++) {
+                               frag_tab = buffer->jpg.frag_tab[2 * j];
+
+                               if (!frag_tab)
                                        break;
-                               ClearPageReserved(MAP_NR
-                                                 (bus_to_virt
-                                                  (le32_to_cpu
-                                                   (fh->jpg_buffers.
-                                                    buffer[i].frag_tab[2 *
-                                                                      j]))));
-                               free_page((unsigned long)
-                                         bus_to_virt
-                                                 (le32_to_cpu
-                                                  (fh->jpg_buffers.
-                                                     buffer[i].
-                                                     frag_tab[2 * j])));
-                               fh->jpg_buffers.buffer[i].frag_tab[2 * j] =
-                                   0;
-                               fh->jpg_buffers.buffer[i].frag_tab[2 * j +
-                                                                  1] = 0;
+                               ClearPageReserved(virt_to_page(bus_to_virt(le32_to_cpu(frag_tab))));
+                               free_page((unsigned long)bus_to_virt(le32_to_cpu(frag_tab)));
+                               buffer->jpg.frag_tab[2 * j] = 0;
+                               buffer->jpg.frag_tab[2 * j + 1] = 0;
                        }
                }
 
-               free_page((unsigned long) fh->jpg_buffers.buffer[i].
-                         frag_tab);
-               fh->jpg_buffers.buffer[i].frag_tab = NULL;
+               free_page((unsigned long)buffer->jpg.frag_tab);
+               buffer->jpg.frag_tab = NULL;
        }
 
-       fh->jpg_buffers.allocated = 0;
+       fh->buffers.allocated = 0;
 }
 
 /*
@@ -484,12 +437,11 @@ jpg_fbuffer_free (struct file *file)
  */
 
 static int
-zoran_v4l_set_format (struct file               *file,
+zoran_v4l_set_format (struct zoran_fh           *fh,
                      int                        width,
                      int                        height,
                      const struct zoran_format *format)
 {
-       struct zoran_fh *fh = file->private_data;
        struct zoran *zr = fh->zr;
        int bpp;
 
@@ -499,19 +451,19 @@ zoran_v4l_set_format (struct file               *file,
            height > BUZ_MAX_HEIGHT || width > BUZ_MAX_WIDTH) {
                dprintk(1,
                        KERN_ERR
-                       "%s: v4l_set_format() - wrong frame size (%dx%d)\n",
-                       ZR_DEVNAME(zr), width, height);
+                       "%s: %s - wrong frame size (%dx%d)\n",
+                       ZR_DEVNAME(zr), __func__, width, height);
                return -EINVAL;
        }
 
        bpp = (format->depth + 7) / 8;
 
        /* Check against available buffer size */
-       if (height * width * bpp > fh->v4l_buffers.buffer_size) {
+       if (height * width * bpp > fh->buffers.buffer_size) {
                dprintk(1,
                        KERN_ERR
-                       "%s: v4l_set_format() - video buffer size (%d kB) is too small\n",
-                       ZR_DEVNAME(zr), fh->v4l_buffers.buffer_size >> 10);
+                       "%s: %s - video buffer size (%d kB) is too small\n",
+                       ZR_DEVNAME(zr), __func__, fh->buffers.buffer_size >> 10);
                return -EINVAL;
        }
 
@@ -520,8 +472,8 @@ zoran_v4l_set_format (struct file               *file,
        if ((bpp == 2 && (width & 1)) || (bpp == 3 && (width & 3))) {
                dprintk(1,
                        KERN_ERR
-                       "%s: v4l_set_format() - wrong frame alignment\n",
-                       ZR_DEVNAME(zr));
+                       "%s: %s - wrong frame alignment\n",
+                       ZR_DEVNAME(zr), __func__);
                return -EINVAL;
        }
 
@@ -533,43 +485,40 @@ zoran_v4l_set_format (struct file               *file,
        return 0;
 }
 
-static int
-zoran_v4l_queue_frame (struct file *file,
-                      int          num)
+static int zoran_v4l_queue_frame(struct zoran_fh *fh, int num)
 {
-       struct zoran_fh *fh = file->private_data;
        struct zoran *zr = fh->zr;
        unsigned long flags;
        int res = 0;
 
-       if (!fh->v4l_buffers.allocated) {
+       if (!fh->buffers.allocated) {
                dprintk(1,
                        KERN_ERR
-                       "%s: v4l_queue_frame() - buffers not yet allocated\n",
-                       ZR_DEVNAME(zr));
+                       "%s: %s - buffers not yet allocated\n",
+                       ZR_DEVNAME(zr), __func__);
                res = -ENOMEM;
        }
 
        /* No grabbing outside the buffer range! */
-       if (num >= fh->v4l_buffers.num_buffers || num < 0) {
+       if (num >= fh->buffers.num_buffers || num < 0) {
                dprintk(1,
                        KERN_ERR
-                       "%s: v4l_queue_frame() - buffer %d is out of range\n",
-                       ZR_DEVNAME(zr), num);
+                       "%s: %s - buffer %d is out of range\n",
+                       ZR_DEVNAME(zr), __func__, num);
                res = -EINVAL;
        }
 
        spin_lock_irqsave(&zr->spinlock, flags);
 
-       if (fh->v4l_buffers.active == ZORAN_FREE) {
+       if (fh->buffers.active == ZORAN_FREE) {
                if (zr->v4l_buffers.active == ZORAN_FREE) {
-                       zr->v4l_buffers = fh->v4l_buffers;
-                       fh->v4l_buffers.active = ZORAN_ACTIVE;
+                       zr->v4l_buffers = fh->buffers;
+                       fh->buffers.active = ZORAN_ACTIVE;
                } else {
                        dprintk(1,
                                KERN_ERR
-                               "%s: v4l_queue_frame() - another session is already capturing\n",
-                               ZR_DEVNAME(zr));
+                               "%s: %s - another session is already capturing\n",
+                               ZR_DEVNAME(zr), __func__);
                        res = -EBUSY;
                }
        }
@@ -580,7 +529,7 @@ zoran_v4l_queue_frame (struct file *file,
                default:
                case BUZ_STATE_PEND:
                        if (zr->v4l_buffers.active == ZORAN_FREE) {
-                               fh->v4l_buffers.active = ZORAN_FREE;
+                               fh->buffers.active = ZORAN_FREE;
                                zr->v4l_buffers.allocated = 0;
                        }
                        res = -EBUSY;   /* what are you doing? */
@@ -588,19 +537,17 @@ zoran_v4l_queue_frame (struct file *file,
                case BUZ_STATE_DONE:
                        dprintk(2,
                                KERN_WARNING
-                               "%s: v4l_queue_frame() - queueing buffer %d in state DONE!?\n",
-                               ZR_DEVNAME(zr), num);
+                               "%s: %s - queueing buffer %d in state DONE!?\n",
+                               ZR_DEVNAME(zr), __func__, num);
                case BUZ_STATE_USER:
                        /* since there is at least one unused buffer there's room for at least
                         * one more pend[] entry */
-                       zr->v4l_pend[zr->v4l_pend_head++ &
-                                       V4L_MASK_FRAME] = num;
+                       zr->v4l_pend[zr->v4l_pend_head++ & V4L_MASK_FRAME] = num;
                        zr->v4l_buffers.buffer[num].state = BUZ_STATE_PEND;
                        zr->v4l_buffers.buffer[num].bs.length =
                            fh->v4l_settings.bytesperline *
                            zr->v4l_settings.height;
-                       fh->v4l_buffers.buffer[num] =
-                           zr->v4l_buffers.buffer[num];
+                       fh->buffers.buffer[num] = zr->v4l_buffers.buffer[num];
                        break;
                }
        }
@@ -608,7 +555,7 @@ zoran_v4l_queue_frame (struct file *file,
        spin_unlock_irqrestore(&zr->spinlock, flags);
 
        if (!res && zr->v4l_buffers.active == ZORAN_FREE)
-               zr->v4l_buffers.active = fh->v4l_buffers.active;
+               zr->v4l_buffers.active = fh->buffers.active;
 
        return res;
 }
@@ -617,27 +564,24 @@ zoran_v4l_queue_frame (struct file *file,
  * Sync on a V4L buffer
  */
 
-static int
-v4l_sync (struct file *file,
-         int          frame)
+static int v4l_sync(struct zoran_fh *fh, int frame)
 {
-       struct zoran_fh *fh = file->private_data;
        struct zoran *zr = fh->zr;
        unsigned long flags;
 
-       if (fh->v4l_buffers.active == ZORAN_FREE) {
+       if (fh->buffers.active == ZORAN_FREE) {
                dprintk(1,
                        KERN_ERR
-                       "%s: v4l_sync() - no grab active for this session\n",
-                       ZR_DEVNAME(zr));
+                       "%s: %s - no grab active for this session\n",
+                       ZR_DEVNAME(zr), __func__);
                return -EINVAL;
        }
 
        /* check passed-in frame number */
-       if (frame >= fh->v4l_buffers.num_buffers || frame < 0) {
+       if (frame >= fh->buffers.num_buffers || frame < 0) {
                dprintk(1,
-                       KERN_ERR "%s: v4l_sync() - frame %d is invalid\n",
-                       ZR_DEVNAME(zr), frame);
+                       KERN_ERR "%s: %s - frame %d is invalid\n",
+                       ZR_DEVNAME(zr), __func__, frame);
                return -EINVAL;
        }
 
@@ -645,15 +589,14 @@ v4l_sync (struct file *file,
        if (zr->v4l_buffers.buffer[frame].state == BUZ_STATE_USER) {
                dprintk(1,
                        KERN_ERR
-                       "%s: v4l_sync() - attempt to sync on a buffer which was not queued?\n",
-                       ZR_DEVNAME(zr));
+                       "%s: %s - attempt to sync on a buffer which was not queued?\n",
+                       ZR_DEVNAME(zr), __func__);
                return -EPROTO;
        }
 
        /* wait on this buffer to get ready */
        if (!wait_event_interruptible_timeout(zr->v4l_capq,
-                               (zr->v4l_buffers.buffer[frame].state != BUZ_STATE_PEND),
-                               10*HZ))
+               (zr->v4l_buffers.buffer[frame].state != BUZ_STATE_PEND), 10*HZ))
                return -ETIME;
        if (signal_pending(current))
                return -ERESTARTSYS;
@@ -661,11 +604,11 @@ v4l_sync (struct file *file,
        /* buffer should now be in BUZ_STATE_DONE */
        if (zr->v4l_buffers.buffer[frame].state != BUZ_STATE_DONE)
                dprintk(2,
-                       KERN_ERR "%s: v4l_sync() - internal state error\n",
-                       ZR_DEVNAME(zr));
+                       KERN_ERR "%s: %s - internal state error\n",
+                       ZR_DEVNAME(zr), __func__);
 
        zr->v4l_buffers.buffer[frame].state = BUZ_STATE_USER;
-       fh->v4l_buffers.buffer[frame] = zr->v4l_buffers.buffer[frame];
+       fh->buffers.buffer[frame] = zr->v4l_buffers.buffer[frame];
 
        spin_lock_irqsave(&zr->spinlock, flags);
 
@@ -673,8 +616,7 @@ v4l_sync (struct file *file,
        if (zr->v4l_pend_tail == zr->v4l_pend_head) {
                zr36057_set_memgrab(zr, 0);
                if (zr->v4l_buffers.active == ZORAN_ACTIVE) {
-                       fh->v4l_buffers.active = zr->v4l_buffers.active =
-                           ZORAN_FREE;
+                       fh->buffers.active = zr->v4l_buffers.active = ZORAN_FREE;
                        zr->v4l_buffers.allocated = 0;
                }
        }
@@ -688,31 +630,28 @@ v4l_sync (struct file *file,
  *   Queue a MJPEG buffer for capture/playback
  */
 
-static int
-zoran_jpg_queue_frame (struct file          *file,
-                      int                   num,
-                      enum zoran_codec_mode mode)
+static int zoran_jpg_queue_frame(struct zoran_fh *fh, int num,
+                                enum zoran_codec_mode mode)
 {
-       struct zoran_fh *fh = file->private_data;
        struct zoran *zr = fh->zr;
        unsigned long flags;
        int res = 0;
 
        /* Check if buffers are allocated */
-       if (!fh->jpg_buffers.allocated) {
+       if (!fh->buffers.allocated) {
                dprintk(1,
                        KERN_ERR
-                       "%s: jpg_queue_frame() - buffers not yet allocated\n",
-                       ZR_DEVNAME(zr));
+                       "%s: %s - buffers not yet allocated\n",
+                       ZR_DEVNAME(zr), __func__);
                return -ENOMEM;
        }
 
        /* No grabbing outside the buffer range! */
-       if (num >= fh->jpg_buffers.num_buffers || num < 0) {
+       if (num >= fh->buffers.num_buffers || num < 0) {
                dprintk(1,
                        KERN_ERR
-                       "%s: jpg_queue_frame() - buffer %d out of range\n",
-                       ZR_DEVNAME(zr), num);
+                       "%s: %s - buffer %d out of range\n",
+                       ZR_DEVNAME(zr), __func__, num);
                return -EINVAL;
        }
 
@@ -723,20 +662,20 @@ zoran_jpg_queue_frame (struct file          *file,
                /* wrong codec mode active - invalid */
                dprintk(1,
                        KERN_ERR
-                       "%s: jpg_queue_frame() - codec in wrong mode\n",
-                       ZR_DEVNAME(zr));
+                       "%s: %s - codec in wrong mode\n",
+                       ZR_DEVNAME(zr), __func__);
                return -EINVAL;
        }
 
-       if (fh->jpg_buffers.active == ZORAN_FREE) {
+       if (fh->buffers.active == ZORAN_FREE) {
                if (zr->jpg_buffers.active == ZORAN_FREE) {
-                       zr->jpg_buffers = fh->jpg_buffers;
-                       fh->jpg_buffers.active = ZORAN_ACTIVE;
+                       zr->jpg_buffers = fh->buffers;
+                       fh->buffers.active = ZORAN_ACTIVE;
                } else {
                        dprintk(1,
                                KERN_ERR
-                               "%s: jpg_queue_frame() - another session is already capturing\n",
-                               ZR_DEVNAME(zr));
+                               "%s: %s - another session is already capturing\n",
+                               ZR_DEVNAME(zr), __func__);
                        res = -EBUSY;
                }
        }
@@ -753,23 +692,21 @@ zoran_jpg_queue_frame (struct file          *file,
                case BUZ_STATE_DONE:
                        dprintk(2,
                                KERN_WARNING
-                               "%s: jpg_queue_frame() - queing frame in BUZ_STATE_DONE state!?\n",
-                               ZR_DEVNAME(zr));
+                               "%s: %s - queing frame in BUZ_STATE_DONE state!?\n",
+                               ZR_DEVNAME(zr), __func__);
                case BUZ_STATE_USER:
                        /* since there is at least one unused buffer there's room for at
                         *least one more pend[] entry */
-                       zr->jpg_pend[zr->jpg_que_head++ & BUZ_MASK_FRAME] =
-                           num;
+                       zr->jpg_pend[zr->jpg_que_head++ & BUZ_MASK_FRAME] = num;
                        zr->jpg_buffers.buffer[num].state = BUZ_STATE_PEND;
-                       fh->jpg_buffers.buffer[num] =
-                           zr->jpg_buffers.buffer[num];
+                       fh->buffers.buffer[num] = zr->jpg_buffers.buffer[num];
                        zoran_feed_stat_com(zr);
                        break;
                default:
                case BUZ_STATE_DMA:
                case BUZ_STATE_PEND:
                        if (zr->jpg_buffers.active == ZORAN_FREE) {
-                               fh->jpg_buffers.active = ZORAN_FREE;
+                               fh->buffers.active = ZORAN_FREE;
                                zr->jpg_buffers.allocated = 0;
                        }
                        res = -EBUSY;   /* what are you doing? */
@@ -779,47 +716,41 @@ zoran_jpg_queue_frame (struct file          *file,
 
        spin_unlock_irqrestore(&zr->spinlock, flags);
 
-       if (!res && zr->jpg_buffers.active == ZORAN_FREE) {
-               zr->jpg_buffers.active = fh->jpg_buffers.active;
-       }
+       if (!res && zr->jpg_buffers.active == ZORAN_FREE)
+               zr->jpg_buffers.active = fh->buffers.active;
 
        return res;
 }
 
-static int
-jpg_qbuf (struct file          *file,
-         int                   frame,
-         enum zoran_codec_mode mode)
+static int jpg_qbuf(struct zoran_fh *fh, int frame, enum zoran_codec_mode mode)
 {
-       struct zoran_fh *fh = file->private_data;
        struct zoran *zr = fh->zr;
        int res = 0;
 
        /* Does the user want to stop streaming? */
        if (frame < 0) {
                if (zr->codec_mode == mode) {
-                       if (fh->jpg_buffers.active == ZORAN_FREE) {
+                       if (fh->buffers.active == ZORAN_FREE) {
                                dprintk(1,
                                        KERN_ERR
-                                       "%s: jpg_qbuf(-1) - session not active\n",
-                                       ZR_DEVNAME(zr));
+                                       "%s: %s(-1) - session not active\n",
+                                       ZR_DEVNAME(zr), __func__);
                                return -EINVAL;
                        }
-                       fh->jpg_buffers.active = zr->jpg_buffers.active =
-                           ZORAN_FREE;
+                       fh->buffers.active = zr->jpg_buffers.active = ZORAN_FREE;
                        zr->jpg_buffers.allocated = 0;
                        zr36057_enable_jpg(zr, BUZ_MODE_IDLE);
                        return 0;
                } else {
                        dprintk(1,
                                KERN_ERR
-                               "%s: jpg_qbuf() - stop streaming but not in streaming mode\n",
-                               ZR_DEVNAME(zr));
+                               "%s: %s - stop streaming but not in streaming mode\n",
+                               ZR_DEVNAME(zr), __func__);
                        return -EINVAL;
                }
        }
 
-       if ((res = zoran_jpg_queue_frame(file, frame, mode)))
+       if ((res = zoran_jpg_queue_frame(fh, frame, mode)))
                return res;
 
        /* Start the jpeg codec when the first frame is queued  */
@@ -833,28 +764,25 @@ jpg_qbuf (struct file          *file,
  *   Sync on a MJPEG buffer
  */
 
-static int
-jpg_sync (struct file       *file,
-         struct zoran_sync *bs)
+static int jpg_sync(struct zoran_fh *fh, struct zoran_sync *bs)
 {
-       struct zoran_fh *fh = file->private_data;
        struct zoran *zr = fh->zr;
        unsigned long flags;
        int frame;
 
-       if (fh->jpg_buffers.active == ZORAN_FREE) {
+       if (fh->buffers.active == ZORAN_FREE) {
                dprintk(1,
                        KERN_ERR
-                       "%s: jpg_sync() - capture is not currently active\n",
-                       ZR_DEVNAME(zr));
+                       "%s: %s - capture is not currently active\n",
+                       ZR_DEVNAME(zr), __func__);
                return -EINVAL;
        }
        if (zr->codec_mode != BUZ_MODE_MOTION_DECOMPRESS &&
            zr->codec_mode != BUZ_MODE_MOTION_COMPRESS) {
                dprintk(1,
                        KERN_ERR
-                       "%s: jpg_sync() - codec not in streaming mode\n",
-                       ZR_DEVNAME(zr));
+                       "%s: %s - codec not in streaming mode\n",
+                       ZR_DEVNAME(zr), __func__);
                return -EINVAL;
        }
        if (!wait_event_interruptible_timeout(zr->jpg_capq,
@@ -869,8 +797,8 @@ jpg_sync (struct file       *file,
                                           sizeof(isr), &isr);
                dprintk(1,
                        KERN_ERR
-                       "%s: jpg_sync() - timeout: codec isr=0x%02x\n",
-                       ZR_DEVNAME(zr), isr);
+                       "%s: %s - timeout: codec isr=0x%02x\n",
+                       ZR_DEVNAME(zr), __func__, isr);
 
                return -ETIME;
 
@@ -888,28 +816,26 @@ jpg_sync (struct file       *file,
        /* buffer should now be in BUZ_STATE_DONE */
        if (zr->jpg_buffers.buffer[frame].state != BUZ_STATE_DONE)
                dprintk(2,
-                       KERN_ERR "%s: jpg_sync() - internal state error\n",
-                       ZR_DEVNAME(zr));
+                       KERN_ERR "%s: %s - internal state error\n",
+                       ZR_DEVNAME(zr), __func__);
 
        *bs = zr->jpg_buffers.buffer[frame].bs;
        bs->frame = frame;
        zr->jpg_buffers.buffer[frame].state = BUZ_STATE_USER;
-       fh->jpg_buffers.buffer[frame] = zr->jpg_buffers.buffer[frame];
+       fh->buffers.buffer[frame] = zr->jpg_buffers.buffer[frame];
 
        spin_unlock_irqrestore(&zr->spinlock, flags);
 
        return 0;
 }
 
-static void
-zoran_open_init_session (struct file *file)
+static void zoran_open_init_session(struct zoran_fh *fh)
 {
        int i;
-       struct zoran_fh *fh = file->private_data;
        struct zoran *zr = fh->zr;
 
        /* Per default, map the V4L Buffers */
-       fh->map_mode = ZORAN_MAP_MODE_RAW;
+       map_mode_raw(fh);
 
        /* take over the card's current settings */
        fh->overlay_settings = zr->overlay_settings;
@@ -919,38 +845,21 @@ zoran_open_init_session (struct file *file)
 
        /* v4l settings */
        fh->v4l_settings = zr->v4l_settings;
-
-       /* v4l_buffers */
-       memset(&fh->v4l_buffers, 0, sizeof(struct zoran_v4l_struct));
-       for (i = 0; i < VIDEO_MAX_FRAME; i++) {
-               fh->v4l_buffers.buffer[i].state = BUZ_STATE_USER;       /* nothing going on */
-               fh->v4l_buffers.buffer[i].bs.frame = i;
-       }
-       fh->v4l_buffers.allocated = 0;
-       fh->v4l_buffers.active = ZORAN_FREE;
-       fh->v4l_buffers.buffer_size = v4l_bufsize;
-       fh->v4l_buffers.num_buffers = v4l_nbufs;
-
        /* jpg settings */
        fh->jpg_settings = zr->jpg_settings;
 
-       /* jpg_buffers */
-       memset(&fh->jpg_buffers, 0, sizeof(struct zoran_jpg_struct));
-       for (i = 0; i < BUZ_MAX_FRAME; i++) {
-               fh->jpg_buffers.buffer[i].state = BUZ_STATE_USER;       /* nothing going on */
-               fh->jpg_buffers.buffer[i].bs.frame = i;
+       /* buffers */
+       memset(&fh->buffers, 0, sizeof(fh->buffers));
+       for (i = 0; i < MAX_FRAME; i++) {
+               fh->buffers.buffer[i].state = BUZ_STATE_USER;   /* nothing going on */
+               fh->buffers.buffer[i].bs.frame = i;
        }
-       fh->jpg_buffers.need_contiguous = zr->jpg_buffers.need_contiguous;
-       fh->jpg_buffers.allocated = 0;
-       fh->jpg_buffers.active = ZORAN_FREE;
-       fh->jpg_buffers.buffer_size = jpg_bufsize;
-       fh->jpg_buffers.num_buffers = jpg_nbufs;
+       fh->buffers.allocated = 0;
+       fh->buffers.active = ZORAN_FREE;
 }
 
-static void
-zoran_close_end_session (struct file *file)
+static void zoran_close_end_session(struct zoran_fh *fh)
 {
-       struct zoran_fh *fh = file->private_data;
        struct zoran *zr = fh->zr;
 
        /* overlay */
@@ -962,33 +871,33 @@ zoran_close_end_session (struct file *file)
                zr->overlay_mask = NULL;
        }
 
-       /* v4l capture */
-       if (fh->v4l_buffers.active != ZORAN_FREE) {
-               unsigned long flags;
+       if (fh->map_mode == ZORAN_MAP_MODE_RAW) {
+               /* v4l capture */
+               if (fh->buffers.active != ZORAN_FREE) {
+                       unsigned long flags;
 
-               spin_lock_irqsave(&zr->spinlock, flags);
-               zr36057_set_memgrab(zr, 0);
-               zr->v4l_buffers.allocated = 0;
-               zr->v4l_buffers.active = fh->v4l_buffers.active =
-                   ZORAN_FREE;
-               spin_unlock_irqrestore(&zr->spinlock, flags);
-       }
+                       spin_lock_irqsave(&zr->spinlock, flags);
+                       zr36057_set_memgrab(zr, 0);
+                       zr->v4l_buffers.allocated = 0;
+                       zr->v4l_buffers.active = fh->buffers.active = ZORAN_FREE;
+                       spin_unlock_irqrestore(&zr->spinlock, flags);
+               }
 
-       /* v4l buffers */
-       if (fh->v4l_buffers.allocated)
-               v4l_fbuffer_free(file);
+               /* v4l buffers */
+               if (fh->buffers.allocated)
+                       v4l_fbuffer_free(fh);
+       } else {
+               /* jpg capture */
+               if (fh->buffers.active != ZORAN_FREE) {
+                       zr36057_enable_jpg(zr, BUZ_MODE_IDLE);
+                       zr->jpg_buffers.allocated = 0;
+                       zr->jpg_buffers.active = fh->buffers.active = ZORAN_FREE;
+               }
 
-       /* jpg capture */
-       if (fh->jpg_buffers.active != ZORAN_FREE) {
-               zr36057_enable_jpg(zr, BUZ_MODE_IDLE);
-               zr->jpg_buffers.allocated = 0;
-               zr->jpg_buffers.active = fh->jpg_buffers.active =
-                   ZORAN_FREE;
+               /* jpg buffers */
+               if (fh->buffers.allocated)
+                       jpg_fbuffer_free(fh);
        }
-
-       /* jpg buffers */
-       if (fh->jpg_buffers.allocated)
-               jpg_fbuffer_free(file);
 }
 
 /*
@@ -1001,15 +910,11 @@ static int zoran_open(struct file *file)
        struct zoran_fh *fh;
        int res, first_open = 0;
 
-       dprintk(2, KERN_INFO "%s: zoran_open(%s, pid=[%d]), users(-)=%d\n",
-               ZR_DEVNAME(zr), current->comm, task_pid_nr(current), zr->user + 1);
+       dprintk(2, KERN_INFO "%s: %s(%s, pid=[%d]), users(-)=%d\n",
+               ZR_DEVNAME(zr), __func__, current->comm, task_pid_nr(current), zr->user + 1);
 
        lock_kernel();
 
-       /* see fs/device.c - the kernel already locks during open(),
-        * so locking ourselves only causes deadlocks */
-       /*mutex_lock(&zr->resource_lock);*/
-
        if (zr->user >= 2048) {
                dprintk(1, KERN_ERR "%s: too many users (%d) on device\n",
                        ZR_DEVNAME(zr), zr->user);
@@ -1017,41 +922,15 @@ static int zoran_open(struct file *file)
                goto fail_unlock;
        }
 
-       if (!zr->decoder) {
-               dprintk(1,
-                       KERN_ERR "%s: no TV decoder loaded for device!\n",
-                       ZR_DEVNAME(zr));
-               res = -EIO;
-               goto fail_unlock;
-       }
-
-       if (!try_module_get(zr->decoder->driver->driver.owner)) {
-               dprintk(1,
-                       KERN_ERR
-                       "%s: failed to grab ownership of video decoder\n",
-                       ZR_DEVNAME(zr));
-               res = -EIO;
-               goto fail_unlock;
-       }
-       if (zr->encoder &&
-           !try_module_get(zr->encoder->driver->driver.owner)) {
-               dprintk(1,
-                       KERN_ERR
-                       "%s: failed to grab ownership of video encoder\n",
-                       ZR_DEVNAME(zr));
-               res = -EIO;
-               goto fail_decoder;
-       }
-
        /* now, create the open()-specific file_ops struct */
        fh = kzalloc(sizeof(struct zoran_fh), GFP_KERNEL);
        if (!fh) {
                dprintk(1,
                        KERN_ERR
-                       "%s: zoran_open() - allocation of zoran_fh failed\n",
-                       ZR_DEVNAME(zr));
+                       "%s: %s - allocation of zoran_fh failed\n",
+                       ZR_DEVNAME(zr), __func__);
                res = -ENOMEM;
-               goto fail_encoder;
+               goto fail_unlock;
        }
        /* used to be BUZ_MAX_WIDTH/HEIGHT, but that gives overflows
         * on norm-change! */
@@ -1060,8 +939,8 @@ static int zoran_open(struct file *file)
        if (!fh->overlay_mask) {
                dprintk(1,
                        KERN_ERR
-                       "%s: zoran_open() - allocation of overlay_mask failed\n",
-                       ZR_DEVNAME(zr));
+                       "%s: %s - allocation of overlay_mask failed\n",
+                       ZR_DEVNAME(zr), __func__);
                res = -ENOMEM;
                goto fail_fh;
        }
@@ -1083,18 +962,13 @@ static int zoran_open(struct file *file)
        /* set file_ops stuff */
        file->private_data = fh;
        fh->zr = zr;
-       zoran_open_init_session(file);
+       zoran_open_init_session(fh);
        unlock_kernel();
 
        return 0;
 
 fail_fh:
        kfree(fh);
-fail_encoder:
-       if (zr->encoder)
-               module_put(zr->encoder->driver->driver.owner);
-fail_decoder:
-       module_put(zr->decoder->driver->driver.owner);
 fail_unlock:
        unlock_kernel();
 
@@ -1110,14 +984,14 @@ zoran_close(struct file  *file)
        struct zoran_fh *fh = file->private_data;
        struct zoran *zr = fh->zr;
 
-       dprintk(2, KERN_INFO "%s: zoran_close(%s, pid=[%d]), users(+)=%d\n",
-               ZR_DEVNAME(zr), current->comm, task_pid_nr(current), zr->user - 1);
+       dprintk(2, KERN_INFO "%s: %s(%s, pid=[%d]), users(+)=%d\n",
+               ZR_DEVNAME(zr), __func__, current->comm, task_pid_nr(current), zr->user - 1);
 
        /* kernel locks (fs/device.c), so don't do that ourselves
         * (prevents deadlocks) */
        /*mutex_lock(&zr->resource_lock);*/
 
-       zoran_close_end_session(file);
+       zoran_close_end_session(fh);
 
        if (zr->user-- == 1) {  /* Last process */
                /* Clean up JPEG process */
@@ -1145,10 +1019,8 @@ zoran_close(struct file  *file)
                zoran_set_pci_master(zr, 0);
 
                if (!pass_through) {    /* Switch to color bar */
-                       struct v4l2_routing route = { 2, 0 };
-
-                       decoder_command(zr, VIDIOC_STREAMOFF, 0);
-                       encoder_command(zr, VIDIOC_INT_S_VIDEO_ROUTING, &route);
+                       decoder_call(zr, video, s_stream, 0);
+                       encoder_call(zr, video, s_routing, 2, 0, 0);
                }
        }
 
@@ -1156,14 +1028,7 @@ zoran_close(struct file  *file)
        kfree(fh->overlay_mask);
        kfree(fh);
 
-       /* release locks on the i2c modules */
-       module_put(zr->decoder->driver->driver.owner);
-       if (zr->encoder)
-               module_put(zr->encoder->driver->driver.owner);
-
-       /*mutex_unlock(&zr->resource_lock);*/
-
-       dprintk(4, KERN_INFO "%s: zoran_close() done\n", ZR_DEVNAME(zr));
+       dprintk(4, KERN_INFO "%s: %s done\n", ZR_DEVNAME(zr), __func__);
 
        return 0;
 }
@@ -1191,15 +1056,13 @@ zoran_write (struct file *file,
        return -EINVAL;
 }
 
-static int
-setup_fbuffer (struct file               *file,
+static int setup_fbuffer(struct zoran_fh *fh,
               void                      *base,
               const struct zoran_format *fmt,
               int                        width,
               int                        height,
               int                        bytesperline)
 {
-       struct zoran_fh *fh = file->private_data;
        struct zoran *zr = fh->zr;
 
        /* (Ronald) v4l/v4l2 guidelines */
@@ -1227,8 +1090,8 @@ setup_fbuffer (struct file               *file,
                 * friendly and silently do as if nothing went wrong */
                dprintk(3,
                        KERN_ERR
-                       "%s: setup_fbuffer() - forced overlay turnoff because framebuffer changed\n",
-                       ZR_DEVNAME(zr));
+                       "%s: %s - forced overlay turnoff because framebuffer changed\n",
+                       ZR_DEVNAME(zr), __func__);
                zr36057_overlay(zr, 0);
        }
 #endif
@@ -1236,31 +1099,31 @@ setup_fbuffer (struct file               *file,
        if (!(fmt->flags & ZORAN_FORMAT_OVERLAY)) {
                dprintk(1,
                        KERN_ERR
-                       "%s: setup_fbuffer() - no valid overlay format given\n",
-                       ZR_DEVNAME(zr));
+                       "%s: %s - no valid overlay format given\n",
+                       ZR_DEVNAME(zr), __func__);
                return -EINVAL;
        }
        if (height <= 0 || width <= 0 || bytesperline <= 0) {
                dprintk(1,
                        KERN_ERR
-                       "%s: setup_fbuffer() - invalid height/width/bpl value (%d|%d|%d)\n",
-                       ZR_DEVNAME(zr), width, height, bytesperline);
+                       "%s: %s - invalid height/width/bpl value (%d|%d|%d)\n",
+                       ZR_DEVNAME(zr), __func__, width, height, bytesperline);
                return -EINVAL;
        }
        if (bytesperline & 3) {
                dprintk(1,
                        KERN_ERR
-                       "%s: setup_fbuffer() - bytesperline (%d) must be 4-byte aligned\n",
-                       ZR_DEVNAME(zr), bytesperline);
+                       "%s: %s - bytesperline (%d) must be 4-byte aligned\n",
+                       ZR_DEVNAME(zr), __func__, bytesperline);
                return -EINVAL;
        }
 
-       zr->buffer.base = (void *) ((unsigned long) base & ~3);
-       zr->buffer.height = height;
-       zr->buffer.width = width;
-       zr->buffer.depth = fmt->depth;
+       zr->vbuf_base = (void *) ((unsigned long) base & ~3);
+       zr->vbuf_height = height;
+       zr->vbuf_width = width;
+       zr->vbuf_depth = fmt->depth;
        zr->overlay_settings.format = fmt;
-       zr->buffer.bytesperline = bytesperline;
+       zr->vbuf_bytesperline = bytesperline;
 
        /* The user should set new window parameters */
        zr->overlay_settings.is_set = 0;
@@ -1269,35 +1132,27 @@ setup_fbuffer (struct file               *file,
 }
 
 
-static int
-setup_window (struct file       *file,
-             int                x,
-             int                y,
-             int                width,
-             int                height,
-             struct video_clip __user *clips,
-             int                clipcount,
-             void              __user *bitmap)
+static int setup_window(struct zoran_fh *fh, int x, int y, int width, int height,
+       struct v4l2_clip __user *clips, int clipcount, void __user *bitmap)
 {
-       struct zoran_fh *fh = file->private_data;
        struct zoran *zr = fh->zr;
-       struct video_clip *vcp = NULL;
+       struct v4l2_clip *vcp = NULL;
        int on, end;
 
 
-       if (!zr->buffer.base) {
+       if (!zr->vbuf_base) {
                dprintk(1,
                        KERN_ERR
-                       "%s: setup_window() - frame buffer has to be set first\n",
-                       ZR_DEVNAME(zr));
+                       "%s: %s - frame buffer has to be set first\n",
+                       ZR_DEVNAME(zr), __func__);
                return -EINVAL;
        }
 
        if (!fh->overlay_settings.format) {
                dprintk(1,
                        KERN_ERR
-                       "%s: setup_window() - no overlay format set\n",
-                       ZR_DEVNAME(zr));
+                       "%s: %s - no overlay format set\n",
+                       ZR_DEVNAME(zr), __func__);
                return -EINVAL;
        }
 
@@ -1305,13 +1160,13 @@ setup_window (struct file       *file,
         * The video front end needs 4-byte alinged line sizes, we correct that
         * silently here if necessary
         */
-       if (zr->buffer.depth == 15 || zr->buffer.depth == 16) {
+       if (zr->vbuf_depth == 15 || zr->vbuf_depth == 16) {
                end = (x + width) & ~1; /* round down */
                x = (x + 1) & ~1;       /* round up */
                width = end - x;
        }
 
-       if (zr->buffer.depth == 24) {
+       if (zr->vbuf_depth == 24) {
                end = (x + width) & ~3; /* round down */
                x = (x + 3) & ~3;       /* round up */
                width = end - x;
@@ -1327,8 +1182,8 @@ setup_window (struct file       *file,
            width > BUZ_MAX_WIDTH || height > BUZ_MAX_HEIGHT) {
                dprintk(1,
                        KERN_ERR
-                       "%s: setup_window() - width = %d or height = %d invalid\n",
-                       ZR_DEVNAME(zr), width, height);
+                       "%s: %s - width = %d or height = %d invalid\n",
+                       ZR_DEVNAME(zr), __func__, width, height);
                return -EINVAL;
        }
 
@@ -1366,20 +1221,20 @@ setup_window (struct file       *file,
                }
        } else if (clipcount > 0) {
                /* write our own bitmap from the clips */
-               vcp = vmalloc(sizeof(struct video_clip) * (clipcount + 4));
+               vcp = vmalloc(sizeof(struct v4l2_clip) * (clipcount + 4));
                if (vcp == NULL) {
                        dprintk(1,
                                KERN_ERR
-                               "%s: setup_window() - Alloc of clip mask failed\n",
-                               ZR_DEVNAME(zr));
+                               "%s: %s - Alloc of clip mask failed\n",
+                               ZR_DEVNAME(zr), __func__);
                        return -ENOMEM;
                }
                if (copy_from_user
-                   (vcp, clips, sizeof(struct video_clip) * clipcount)) {
+                   (vcp, clips, sizeof(struct v4l2_clip) * clipcount)) {
                        vfree(vcp);
                        return -EFAULT;
                }
-               write_overlay_mask(file, vcp, clipcount);
+               write_overlay_mask(fh, vcp, clipcount);
                vfree(vcp);
        }
 
@@ -1395,11 +1250,8 @@ setup_window (struct file       *file,
        return wait_grab_pending(zr);
 }
 
-static int
-setup_overlay (struct file *file,
-              int          on)
+static int setup_overlay(struct zoran_fh *fh, int on)
 {
-       struct zoran_fh *fh = file->private_data;
        struct zoran *zr = fh->zr;
 
        /* If there is nothing to do, return immediatly */
@@ -1412,16 +1264,16 @@ setup_overlay (struct file *file,
            fh->overlay_active == ZORAN_FREE) {
                dprintk(1,
                        KERN_ERR
-                       "%s: setup_overlay() - overlay is already active for another session\n",
-                       ZR_DEVNAME(zr));
+                       "%s: %s - overlay is already active for another session\n",
+                       ZR_DEVNAME(zr), __func__);
                return -EBUSY;
        }
        if (!on && zr->overlay_active != ZORAN_FREE &&
            fh->overlay_active == ZORAN_FREE) {
                dprintk(1,
                        KERN_ERR
-                       "%s: setup_overlay() - you cannot cancel someone else's session\n",
-                       ZR_DEVNAME(zr));
+                       "%s: %s - you cannot cancel someone else's session\n",
+                       ZR_DEVNAME(zr), __func__);
                return -EPERM;
        }
 
@@ -1434,18 +1286,18 @@ setup_overlay (struct file *file,
                        zr36057_overlay(zr, 0);
                zr->overlay_mask = NULL;
        } else {
-               if (!zr->buffer.base || !fh->overlay_settings.is_set) {
+               if (!zr->vbuf_base || !fh->overlay_settings.is_set) {
                        dprintk(1,
                                KERN_ERR
-                               "%s: setup_overlay() - buffer or window not set\n",
-                               ZR_DEVNAME(zr));
+                               "%s: %s - buffer or window not set\n",
+                               ZR_DEVNAME(zr), __func__);
                        return -EINVAL;
                }
                if (!fh->overlay_settings.format) {
                        dprintk(1,
                                KERN_ERR
-                               "%s: setup_overlay() - no overlay format set\n",
-                               ZR_DEVNAME(zr));
+                               "%s: %s - no overlay format set\n",
+                               ZR_DEVNAME(zr), __func__);
                        return -EINVAL;
                }
                zr->overlay_active = fh->overlay_active = ZORAN_LOCKED;
@@ -1462,41 +1314,47 @@ setup_overlay (struct file *file,
        return wait_grab_pending(zr);
 }
 
-       /* get the status of a buffer in the clients buffer queue */
-static int
-zoran_v4l2_buffer_status (struct file        *file,
-                         struct v4l2_buffer *buf,
-                         int                 num)
+/* get the status of a buffer in the clients buffer queue */
+static int zoran_v4l2_buffer_status(struct zoran_fh *fh,
+                                   struct v4l2_buffer *buf, int num)
 {
-       struct zoran_fh *fh = file->private_data;
        struct zoran *zr = fh->zr;
+       unsigned long flags;
 
        buf->flags = V4L2_BUF_FLAG_MAPPED;
 
        switch (fh->map_mode) {
        case ZORAN_MAP_MODE_RAW:
-
                /* check range */
-               if (num < 0 || num >= fh->v4l_buffers.num_buffers ||
-                   !fh->v4l_buffers.allocated) {
+               if (num < 0 || num >= fh->buffers.num_buffers ||
+                   !fh->buffers.allocated) {
                        dprintk(1,
                                KERN_ERR
-                               "%s: v4l2_buffer_status() - wrong number or buffers not allocated\n",
-                               ZR_DEVNAME(zr));
+                               "%s: %s - wrong number or buffers not allocated\n",
+                               ZR_DEVNAME(zr), __func__);
                        return -EINVAL;
                }
 
+               spin_lock_irqsave(&zr->spinlock, flags);
+               dprintk(3,
+                       KERN_DEBUG
+                       "%s: %s() - raw active=%c, buffer %d: state=%c, map=%c\n",
+                       ZR_DEVNAME(zr), __func__,
+                       "FAL"[fh->buffers.active], num,
+                       "UPMD"[zr->v4l_buffers.buffer[num].state],
+                       fh->buffers.buffer[num].map ? 'Y' : 'N');
+               spin_unlock_irqrestore(&zr->spinlock, flags);
+
                buf->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
-               buf->length = fh->v4l_buffers.buffer_size;
+               buf->length = fh->buffers.buffer_size;
 
                /* get buffer */
-               buf->bytesused = fh->v4l_buffers.buffer[num].bs.length;
-               if (fh->v4l_buffers.buffer[num].state == BUZ_STATE_DONE ||
-                   fh->v4l_buffers.buffer[num].state == BUZ_STATE_USER) {
-                       buf->sequence = fh->v4l_buffers.buffer[num].bs.seq;
+               buf->bytesused = fh->buffers.buffer[num].bs.length;
+               if (fh->buffers.buffer[num].state == BUZ_STATE_DONE ||
+                   fh->buffers.buffer[num].state == BUZ_STATE_USER) {
+                       buf->sequence = fh->buffers.buffer[num].bs.seq;
                        buf->flags |= V4L2_BUF_FLAG_DONE;
-                       buf->timestamp =
-                           fh->v4l_buffers.buffer[num].bs.timestamp;
+                       buf->timestamp = fh->buffers.buffer[num].bs.timestamp;
                } else {
                        buf->flags |= V4L2_BUF_FLAG_QUEUED;
                }
@@ -1512,28 +1370,26 @@ zoran_v4l2_buffer_status (struct file        *file,
        case ZORAN_MAP_MODE_JPG_PLAY:
 
                /* check range */
-               if (num < 0 || num >= fh->jpg_buffers.num_buffers ||
-                   !fh->jpg_buffers.allocated) {
+               if (num < 0 || num >= fh->buffers.num_buffers ||
+                   !fh->buffers.allocated) {
                        dprintk(1,
                                KERN_ERR
-                               "%s: v4l2_buffer_status() - wrong number or buffers not allocated\n",
-                               ZR_DEVNAME(zr));
+                               "%s: %s - wrong number or buffers not allocated\n",
+                               ZR_DEVNAME(zr), __func__);
                        return -EINVAL;
                }
 
                buf->type = (fh->map_mode == ZORAN_MAP_MODE_JPG_REC) ?
                              V4L2_BUF_TYPE_VIDEO_CAPTURE :
                              V4L2_BUF_TYPE_VIDEO_OUTPUT;
-               buf->length = fh->jpg_buffers.buffer_size;
+               buf->length = fh->buffers.buffer_size;
 
                /* these variables are only written after frame has been captured */
-               if (fh->jpg_buffers.buffer[num].state == BUZ_STATE_DONE ||
-                   fh->jpg_buffers.buffer[num].state == BUZ_STATE_USER) {
-                       buf->sequence = fh->jpg_buffers.buffer[num].bs.seq;
-                       buf->timestamp =
-                           fh->jpg_buffers.buffer[num].bs.timestamp;
-                       buf->bytesused =
-                           fh->jpg_buffers.buffer[num].bs.length;
+               if (fh->buffers.buffer[num].state == BUZ_STATE_DONE ||
+                   fh->buffers.buffer[num].state == BUZ_STATE_USER) {
+                       buf->sequence = fh->buffers.buffer[num].bs.seq;
+                       buf->timestamp = fh->buffers.buffer[num].bs.timestamp;
+                       buf->bytesused = fh->buffers.buffer[num].bs.length;
                        buf->flags |= V4L2_BUF_FLAG_DONE;
                } else {
                        buf->flags |= V4L2_BUF_FLAG_QUEUED;
@@ -1541,14 +1397,11 @@ zoran_v4l2_buffer_status (struct file        *file,
 
                /* which fields are these? */
                if (fh->jpg_settings.TmpDcm != 1)
-                       buf->field =
-                           fh->jpg_settings.
-                           odd_even ? V4L2_FIELD_TOP : V4L2_FIELD_BOTTOM;
+                       buf->field = fh->jpg_settings.odd_even ?
+                               V4L2_FIELD_TOP : V4L2_FIELD_BOTTOM;
                else
-                       buf->field =
-                           fh->jpg_settings.
-                           odd_even ? V4L2_FIELD_SEQ_TB :
-                           V4L2_FIELD_SEQ_BT;
+                       buf->field = fh->jpg_settings.odd_even ?
+                               V4L2_FIELD_SEQ_TB : V4L2_FIELD_SEQ_BT;
 
                break;
 
@@ -1556,8 +1409,8 @@ zoran_v4l2_buffer_status (struct file        *file,
 
                dprintk(5,
                        KERN_ERR
-                       "%s: v4l2_buffer_status() - invalid buffer type|map_mode (%d|%d)\n",
-                       ZR_DEVNAME(zr), buf->type, fh->map_mode);
+                       "%s: %s - invalid buffer type|map_mode (%d|%d)\n",
+                       ZR_DEVNAME(zr), __func__, buf->type, fh->map_mode);
                return -EINVAL;
        }
 
@@ -1578,31 +1431,15 @@ zoran_set_norm (struct zoran *zr,
            zr->jpg_buffers.active != ZORAN_FREE) {
                dprintk(1,
                        KERN_WARNING
-                       "%s: set_norm() called while in playback/capture mode\n",
-                       ZR_DEVNAME(zr));
+                       "%s: %s called while in playback/capture mode\n",
+                       ZR_DEVNAME(zr), __func__);
                return -EBUSY;
        }
 
-       if (lock_norm && norm != zr->norm) {
-               if (lock_norm > 1) {
-                       dprintk(1,
-                               KERN_WARNING
-                               "%s: set_norm() - TV standard is locked, can not switch norm\n",
-                               ZR_DEVNAME(zr));
-                       return -EPERM;
-               } else {
-                       dprintk(1,
-                               KERN_WARNING
-                               "%s: set_norm() - TV standard is locked, norm was not changed\n",
-                               ZR_DEVNAME(zr));
-                       norm = zr->norm;
-               }
-       }
-
        if (!(norm & zr->card.norms)) {
                dprintk(1,
-                       KERN_ERR "%s: set_norm() - unsupported norm %llx\n",
-                       ZR_DEVNAME(zr), norm);
+                       KERN_ERR "%s: %s - unsupported norm %llx\n",
+                       ZR_DEVNAME(zr), __func__, norm);
                return -EINVAL;
        }
 
@@ -1610,20 +1447,20 @@ zoran_set_norm (struct zoran *zr,
                int status = 0;
                v4l2_std_id std = 0;
 
-               decoder_command(zr, VIDIOC_QUERYSTD, &std);
-               decoder_command(zr, VIDIOC_S_STD, &std);
+               decoder_call(zr, video, querystd, &std);
+               decoder_call(zr, core, s_std, std);
 
                /* let changes come into effect */
                ssleep(2);
 
-               decoder_command(zr, VIDIOC_INT_G_INPUT_STATUS, &status);
+               decoder_call(zr, video, g_input_status, &status);
                if (status & V4L2_IN_ST_NO_SIGNAL) {
                        dprintk(1,
                                KERN_ERR
-                               "%s: set_norm() - no norm detected\n",
-                               ZR_DEVNAME(zr));
+                               "%s: %s - no norm detected\n",
+                               ZR_DEVNAME(zr), __func__);
                        /* reset norm */
-                       decoder_command(zr, VIDIOC_S_STD, &zr->norm);
+                       decoder_call(zr, core, s_std, zr->norm);
                        return -EIO;
                }
 
@@ -1642,8 +1479,8 @@ zoran_set_norm (struct zoran *zr,
        if (on)
                zr36057_overlay(zr, 0);
 
-       decoder_command(zr, VIDIOC_S_STD, &norm);
-       encoder_command(zr, VIDIOC_INT_S_STD_OUTPUT, &norm);
+       decoder_call(zr, core, s_std, norm);
+       encoder_call(zr, video, s_std_output, norm);
 
        if (on)
                zr36057_overlay(zr, 1);
@@ -1658,8 +1495,6 @@ static int
 zoran_set_input (struct zoran *zr,
                 int           input)
 {
-       struct v4l2_routing route = { 0, 0 };
-
        if (input == zr->input) {
                return 0;
        }
@@ -1668,23 +1503,23 @@ zoran_set_input (struct zoran *zr,
            zr->jpg_buffers.active != ZORAN_FREE) {
                dprintk(1,
                        KERN_WARNING
-                       "%s: set_input() called while in playback/capture mode\n",
-                       ZR_DEVNAME(zr));
+                       "%s: %s called while in playback/capture mode\n",
+                       ZR_DEVNAME(zr), __func__);
                return -EBUSY;
        }
 
        if (input < 0 || input >= zr->card.inputs) {
                dprintk(1,
                        KERN_ERR
-                       "%s: set_input() - unnsupported input %d\n",
-                       ZR_DEVNAME(zr), input);
+                       "%s: %s - unnsupported input %d\n",
+                       ZR_DEVNAME(zr), __func__, input);
                return -EINVAL;
        }
 
-       route.input = zr->card.input[input].muxsel;
        zr->input = input;
 
-       decoder_command(zr, VIDIOC_INT_S_VIDEO_ROUTING, &route);
+       decoder_call(zr, video, s_routing,
+                       zr->card.input[input].muxsel, 0, 0);
 
        return 0;
 }
@@ -1831,7 +1666,7 @@ sparams_unlock_and_return:
 
                mutex_lock(&zr->resource_lock);
 
-               if (fh->jpg_buffers.allocated || fh->v4l_buffers.allocated) {
+               if (fh->buffers.allocated) {
                        dprintk(1,
                                KERN_ERR
                                "%s: BUZIOC_REQBUFS - buffers already allocated\n",
@@ -1840,17 +1675,17 @@ sparams_unlock_and_return:
                        goto jpgreqbuf_unlock_and_return;
                }
 
-               fh->jpg_buffers.num_buffers = breq->count;
-               fh->jpg_buffers.buffer_size = breq->size;
+               /* The next mmap will map the MJPEG buffers - could
+                * also be *_PLAY, but it doesn't matter here */
+               map_mode_jpg(fh, 0);
+               fh->buffers.num_buffers = breq->count;
+               fh->buffers.buffer_size = breq->size;
 
-               if (jpg_fbuffer_alloc(file)) {
+               if (jpg_fbuffer_alloc(fh)) {
                        res = -ENOMEM;
                        goto jpgreqbuf_unlock_and_return;
                }
 
-               /* The next mmap will map the MJPEG buffers - could
-                * also be *_PLAY, but it doesn't matter here */
-               fh->map_mode = ZORAN_MAP_MODE_JPG_REC;
 jpgreqbuf_unlock_and_return:
                mutex_unlock(&zr->resource_lock);
 
@@ -1865,7 +1700,7 @@ jpgreqbuf_unlock_and_return:
                        ZR_DEVNAME(zr), *frame);
 
                mutex_lock(&zr->resource_lock);
-               res = jpg_qbuf(file, *frame, BUZ_MODE_MOTION_COMPRESS);
+               res = jpg_qbuf(fh, *frame, BUZ_MODE_MOTION_COMPRESS);
                mutex_unlock(&zr->resource_lock);
 
                return res;
@@ -1879,7 +1714,7 @@ jpgreqbuf_unlock_and_return:
                        ZR_DEVNAME(zr), *frame);
 
                mutex_lock(&zr->resource_lock);
-               res = jpg_qbuf(file, *frame, BUZ_MODE_MOTION_DECOMPRESS);
+               res = jpg_qbuf(fh, *frame, BUZ_MODE_MOTION_DECOMPRESS);
                mutex_unlock(&zr->resource_lock);
 
                return res;
@@ -1893,7 +1728,15 @@ jpgreqbuf_unlock_and_return:
                dprintk(3, KERN_DEBUG "%s: BUZIOC_SYNC\n", ZR_DEVNAME(zr));
 
                mutex_lock(&zr->resource_lock);
-               res = jpg_sync(file, bsync);
+
+               if (fh->map_mode == ZORAN_MAP_MODE_RAW) {
+                       dprintk(2, KERN_WARNING
+                               "%s: %s - not in jpg capture mode\n",
+                               ZR_DEVNAME(zr), __func__);
+                       res = -EINVAL;
+               } else {
+                       res = jpg_sync(fh, bsync);
+               }
                mutex_unlock(&zr->resource_lock);
 
                return res;
@@ -1902,7 +1745,6 @@ jpgreqbuf_unlock_and_return:
        case BUZIOC_G_STATUS:
        {
                struct zoran_status *bstat = arg;
-               struct v4l2_routing route = { 0, 0 };
                int status = 0, res = 0;
                v4l2_std_id norm;
 
@@ -1916,8 +1758,6 @@ jpgreqbuf_unlock_and_return:
                        return -EINVAL;
                }
 
-               route.input = zr->card.input[bstat->input].muxsel;
-
                mutex_lock(&zr->resource_lock);
 
                if (zr->codec_mode != BUZ_MODE_IDLE) {
@@ -1929,18 +1769,19 @@ jpgreqbuf_unlock_and_return:
                        goto gstat_unlock_and_return;
                }
 
-               decoder_command(zr, VIDIOC_INT_S_VIDEO_ROUTING, &route);
+               decoder_call(zr, video, s_routing,
+                               zr->card.input[bstat->input].muxsel, 0, 0);
 
                /* sleep 1 second */
                ssleep(1);
 
                /* Get status of video decoder */
-               decoder_command(zr, VIDIOC_QUERYSTD, &norm);
-               decoder_command(zr, VIDIOC_INT_G_INPUT_STATUS, &status);
+               decoder_call(zr, video, querystd, &norm);
+               decoder_call(zr, video, g_input_status, &status);
 
                /* restore previous input and norm */
-               route.input = zr->card.input[zr->input].muxsel;
-               decoder_command(zr, VIDIOC_INT_S_VIDEO_ROUTING, &route);
+               decoder_call(zr, video, s_routing,
+                               zr->card.input[zr->input].muxsel, 0, 0);
 gstat_unlock_and_return:
                mutex_unlock(&zr->resource_lock);
 
@@ -1972,18 +1813,10 @@ static int zoran_vidiocgmbuf(struct file *file, void *__fh, struct video_mbuf *v
        struct zoran *zr = fh->zr;
        int i, res = 0;
 
-       vmbuf->size =
-           fh->v4l_buffers.num_buffers *
-           fh->v4l_buffers.buffer_size;
-       vmbuf->frames = fh->v4l_buffers.num_buffers;
-       for (i = 0; i < vmbuf->frames; i++) {
-               vmbuf->offsets[i] =
-                   i * fh->v4l_buffers.buffer_size;
-       }
 
        mutex_lock(&zr->resource_lock);
 
-       if (fh->jpg_buffers.allocated || fh->v4l_buffers.allocated) {
+       if (fh->buffers.allocated) {
                dprintk(1,
                        KERN_ERR
                        "%s: VIDIOCGMBUF - buffers already allocated\n",
@@ -1992,13 +1825,19 @@ static int zoran_vidiocgmbuf(struct file *file, void *__fh, struct video_mbuf *v
                goto v4l1reqbuf_unlock_and_return;
        }
 
-       if (v4l_fbuffer_alloc(file)) {
+       /* The next mmap will map the V4L buffers */
+       map_mode_raw(fh);
+
+       if (v4l_fbuffer_alloc(fh)) {
                res = -ENOMEM;
                goto v4l1reqbuf_unlock_and_return;
        }
 
-       /* The next mmap will map the V4L buffers */
-       fh->map_mode = ZORAN_MAP_MODE_RAW;
+       vmbuf->size = fh->buffers.num_buffers * fh->buffers.buffer_size;
+       vmbuf->frames = fh->buffers.num_buffers;
+       for (i = 0; i < vmbuf->frames; i++)
+               vmbuf->offsets[i] = i * fh->buffers.buffer_size;
+
 v4l1reqbuf_unlock_and_return:
        mutex_unlock(&zr->resource_lock);
 
@@ -2016,32 +1855,29 @@ static int zoran_querycap(struct file *file, void *__fh, struct v4l2_capability
        strncpy(cap->driver, "zoran", sizeof(cap->driver)-1);
        snprintf(cap->bus_info, sizeof(cap->bus_info), "PCI:%s",
                 pci_name(zr->pci_dev));
-       cap->version =
-           KERNEL_VERSION(MAJOR_VERSION, MINOR_VERSION,
+       cap->version = KERNEL_VERSION(MAJOR_VERSION, MINOR_VERSION,
                           RELEASE_VERSION);
-       cap->capabilities = ZORAN_V4L2_VID_FLAGS;
-
+       cap->capabilities = V4L2_CAP_STREAMING | V4L2_CAP_VIDEO_CAPTURE |
+                           V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_VIDEO_OVERLAY;
        return 0;
 }
 
 static int zoran_enum_fmt(struct zoran *zr, struct v4l2_fmtdesc *fmt, int flag)
 {
-       int num = -1, i;
+       unsigned int num, i;
 
-       for (i = 0; i < NUM_FORMATS; i++) {
-               if (zoran_formats[i].flags & flag)
-                       num++;
-               if (num == fmt->index)
-                       break;
+       for (num = i = 0; i < NUM_FORMATS; i++) {
+               if (zoran_formats[i].flags & flag && num++ == fmt->index) {
+                       strncpy(fmt->description, zoran_formats[i].name,
+                               sizeof(fmt->description) - 1);
+                       /* fmt struct pre-zeroed, so adding '\0' not neeed */
+                       fmt->pixelformat = zoran_formats[i].fourcc;
+                       if (zoran_formats[i].flags & ZORAN_FORMAT_COMPRESSED)
+                               fmt->flags |= V4L2_FMT_FLAG_COMPRESSED;
+                       return 0;
+               }
        }
-       if (fmt->index < 0 /* late, but not too late */  || i == NUM_FORMATS)
-               return -EINVAL;
-
-       strncpy(fmt->description, zoran_formats[i].name, sizeof(fmt->description)-1);
-       fmt->pixelformat = zoran_formats[i].fourcc;
-       if (zoran_formats[i].flags & ZORAN_FORMAT_COMPRESSED)
-               fmt->flags |= V4L2_FMT_FLAG_COMPRESSED;
-       return 0;
+       return -EINVAL;
 }
 
 static int zoran_enum_fmt_vid_cap(struct file *file, void *__fh,
@@ -2175,8 +2011,6 @@ static int zoran_try_fmt_vid_out(struct file *file, void *__fh,
        if (fmt->fmt.pix.pixelformat != V4L2_PIX_FMT_MJPEG)
                return -EINVAL;
 
-       fmt->fmt.pix.bytesperline = 0;
-
        mutex_lock(&zr->resource_lock);
        settings = fh->jpg_settings;
 
@@ -2201,6 +2035,14 @@ static int zoran_try_fmt_vid_out(struct file *file, void *__fh,
        else
                settings.field_per_buff = 1;
 
+       if (settings.HorDcm > 1) {
+               settings.img_x = (BUZ_MAX_WIDTH == 720) ? 8 : 0;
+               settings.img_width = (BUZ_MAX_WIDTH == 720) ? 704 : BUZ_MAX_WIDTH;
+       } else {
+               settings.img_x = 0;
+               settings.img_width = BUZ_MAX_WIDTH;
+       }
+
        /* check */
        res = zoran_check_jpg_settings(zr, &settings, 1);
        if (res)
@@ -2218,6 +2060,8 @@ static int zoran_try_fmt_vid_out(struct file *file, void *__fh,
                                V4L2_FIELD_TOP : V4L2_FIELD_BOTTOM);
 
        fmt->fmt.pix.sizeimage = zoran_v4l2_calc_bufsize(&settings);
+       fmt->fmt.pix.bytesperline = 0;
+       fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
 tryfmt_unlock_and_return:
        mutex_unlock(&zr->resource_lock);
        return res;
@@ -2245,16 +2089,10 @@ static int zoran_try_fmt_vid_cap(struct file *file, void *__fh,
                return -EINVAL;
        }
 
-       bpp = (zoran_formats[i].depth + 7) / 8;
-       fmt->fmt.pix.width &= ~((bpp == 2) ? 1 : 3);
-       if (fmt->fmt.pix.width > BUZ_MAX_WIDTH)
-               fmt->fmt.pix.width = BUZ_MAX_WIDTH;
-       if (fmt->fmt.pix.width < BUZ_MIN_WIDTH)
-               fmt->fmt.pix.width = BUZ_MIN_WIDTH;
-       if (fmt->fmt.pix.height > BUZ_MAX_HEIGHT)
-               fmt->fmt.pix.height = BUZ_MAX_HEIGHT;
-       if (fmt->fmt.pix.height < BUZ_MIN_HEIGHT)
-               fmt->fmt.pix.height = BUZ_MIN_HEIGHT;
+       bpp = DIV_ROUND_UP(zoran_formats[i].depth, 8);
+       v4l_bound_align_image(
+               &fmt->fmt.pix.width, BUZ_MIN_WIDTH, BUZ_MAX_WIDTH, bpp == 2 ? 1 : 2,
+               &fmt->fmt.pix.height, BUZ_MIN_HEIGHT, BUZ_MAX_HEIGHT, 0, 0);
        mutex_unlock(&zr->resource_lock);
 
        return 0;
@@ -2274,14 +2112,10 @@ static int zoran_s_fmt_vid_overlay(struct file *file, void *__fh,
                        fmt->fmt.win.clipcount,
                        fmt->fmt.win.bitmap);
        mutex_lock(&zr->resource_lock);
-       res = setup_window(file, fmt->fmt.win.w.left,
-                       fmt->fmt.win.w.top,
-                       fmt->fmt.win.w.width,
-                       fmt->fmt.win.w.height,
-                       (struct video_clip __user *)
-                       fmt->fmt.win.clips,
-                       fmt->fmt.win.clipcount,
-                       fmt->fmt.win.bitmap);
+       res = setup_window(fh, fmt->fmt.win.w.left, fmt->fmt.win.w.top,
+                          fmt->fmt.win.w.width, fmt->fmt.win.w.height,
+                          (struct v4l2_clip __user *)fmt->fmt.win.clips,
+                          fmt->fmt.win.clipcount, fmt->fmt.win.bitmap);
        mutex_unlock(&zr->resource_lock);
        return res;
 }
@@ -2304,15 +2138,15 @@ static int zoran_s_fmt_vid_out(struct file *file, void *__fh,
 
        mutex_lock(&zr->resource_lock);
 
-       settings = fh->jpg_settings;
-
-       if (fh->v4l_buffers.allocated || fh->jpg_buffers.allocated) {
+       if (fh->buffers.allocated) {
                dprintk(1, KERN_ERR "%s: VIDIOC_S_FMT - cannot change capture mode\n",
-                               ZR_DEVNAME(zr));
+                       ZR_DEVNAME(zr));
                res = -EBUSY;
                goto sfmtjpg_unlock_and_return;
        }
 
+       settings = fh->jpg_settings;
+
        /* we actually need to set 'real' parameters now */
        if (fmt->fmt.pix.height * 2 > BUZ_MAX_HEIGHT)
                settings.TmpDcm = 1;
@@ -2350,6 +2184,9 @@ static int zoran_s_fmt_vid_out(struct file *file, void *__fh,
        /* it's ok, so set them */
        fh->jpg_settings = settings;
 
+       map_mode_jpg(fh, fmt->type == V4L2_BUF_TYPE_VIDEO_OUTPUT);
+       fh->buffers.buffer_size = zoran_v4l2_calc_bufsize(&fh->jpg_settings);
+
        /* tell the user what we actually did */
        fmt->fmt.pix.width = settings.img_width / settings.HorDcm;
        fmt->fmt.pix.height = settings.img_height * 2 /
@@ -2360,15 +2197,10 @@ static int zoran_s_fmt_vid_out(struct file *file, void *__fh,
        else
                fmt->fmt.pix.field = (fh->jpg_settings.odd_even ?
                                V4L2_FIELD_TOP : V4L2_FIELD_BOTTOM);
-       fh->jpg_buffers.buffer_size = zoran_v4l2_calc_bufsize(&fh->jpg_settings);
        fmt->fmt.pix.bytesperline = 0;
-       fmt->fmt.pix.sizeimage = fh->jpg_buffers.buffer_size;
+       fmt->fmt.pix.sizeimage = fh->buffers.buffer_size;
        fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
 
-       /* we hereby abuse this variable to show that
-        * we're gonna do mjpeg capture */
-       fh->map_mode = (fmt->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) ?
-               ZORAN_MAP_MODE_JPG_REC : ZORAN_MAP_MODE_JPG_PLAY;
 sfmtjpg_unlock_and_return:
        mutex_unlock(&zr->resource_lock);
        return res;
@@ -2393,9 +2225,11 @@ static int zoran_s_fmt_vid_cap(struct file *file, void *__fh,
                        ZR_DEVNAME(zr), fmt->fmt.pix.pixelformat);
                return -EINVAL;
        }
+
        mutex_lock(&zr->resource_lock);
-       if (fh->jpg_buffers.allocated ||
-               (fh->v4l_buffers.allocated && fh->v4l_buffers.active != ZORAN_FREE)) {
+
+       if ((fh->map_mode != ZORAN_MAP_MODE_RAW && fh->buffers.allocated) ||
+           fh->buffers.active != ZORAN_FREE) {
                dprintk(1, KERN_ERR "%s: VIDIOC_S_FMT - cannot change capture mode\n",
                                ZR_DEVNAME(zr));
                res = -EBUSY;
@@ -2406,13 +2240,14 @@ static int zoran_s_fmt_vid_cap(struct file *file, void *__fh,
        if (fmt->fmt.pix.width > BUZ_MAX_WIDTH)
                fmt->fmt.pix.width = BUZ_MAX_WIDTH;
 
-       res = zoran_v4l_set_format(file, fmt->fmt.pix.width,
-                       fmt->fmt.pix.height, &zoran_formats[i]);
+       map_mode_raw(fh);
+
+       res = zoran_v4l_set_format(fh, fmt->fmt.pix.width, fmt->fmt.pix.height,
+                                  &zoran_formats[i]);
        if (res)
                goto sfmtv4l_unlock_and_return;
 
-       /* tell the user the
-        * results/missing stuff */
+       /* tell the user the results/missing stuff */
        fmt->fmt.pix.bytesperline = fh->v4l_settings.bytesperline;
        fmt->fmt.pix.sizeimage = fh->v4l_settings.height * fh->v4l_settings.bytesperline;
        fmt->fmt.pix.colorspace = fh->v4l_settings.format->colorspace;
@@ -2421,7 +2256,6 @@ static int zoran_s_fmt_vid_cap(struct file *file, void *__fh,
        else
                fmt->fmt.pix.field = V4L2_FIELD_TOP;
 
-       fh->map_mode = ZORAN_MAP_MODE_RAW;
 sfmtv4l_unlock_and_return:
        mutex_unlock(&zr->resource_lock);
        return res;
@@ -2435,12 +2269,12 @@ static int zoran_g_fbuf(struct file *file, void *__fh,
 
        memset(fb, 0, sizeof(*fb));
        mutex_lock(&zr->resource_lock);
-       fb->base = zr->buffer.base;
-       fb->fmt.width = zr->buffer.width;
-       fb->fmt.height = zr->buffer.height;
+       fb->base = zr->vbuf_base;
+       fb->fmt.width = zr->vbuf_width;
+       fb->fmt.height = zr->vbuf_height;
        if (zr->overlay_settings.format)
                fb->fmt.pixelformat = fh->overlay_settings.format->fourcc;
-       fb->fmt.bytesperline = zr->buffer.bytesperline;
+       fb->fmt.bytesperline = zr->vbuf_bytesperline;
        mutex_unlock(&zr->resource_lock);
        fb->fmt.colorspace = V4L2_COLORSPACE_SRGB;
        fb->fmt.field = V4L2_FIELD_INTERLACED;
@@ -2469,9 +2303,8 @@ static int zoran_s_fbuf(struct file *file, void *__fh,
        }
 
        mutex_lock(&zr->resource_lock);
-       res = setup_fbuffer(file, fb->base, &zoran_formats[i],
-                               fb->fmt.width, fb->fmt.height,
-                               fb->fmt.bytesperline);
+       res = setup_fbuffer(fh, fb->base, &zoran_formats[i], fb->fmt.width,
+                           fb->fmt.height, fb->fmt.bytesperline);
        mutex_unlock(&zr->resource_lock);
 
        return res;
@@ -2484,7 +2317,7 @@ static int zoran_overlay(struct file *file, void *__fh, unsigned int on)
        int res;
 
        mutex_lock(&zr->resource_lock);
-       res = setup_overlay(file, on);
+       res = setup_overlay(fh, on);
        mutex_unlock(&zr->resource_lock);
 
        return res;
@@ -2510,7 +2343,7 @@ static int zoran_reqbufs(struct file *file, void *__fh, struct v4l2_requestbuffe
                return zoran_streamoff(file, fh, req->type);
 
        mutex_lock(&zr->resource_lock);
-       if (fh->v4l_buffers.allocated || fh->jpg_buffers.allocated) {
+       if (fh->buffers.allocated) {
                dprintk(2,
                                KERN_ERR
                                "%s: VIDIOC_REQBUFS - buffers already allocated\n",
@@ -2520,46 +2353,38 @@ static int zoran_reqbufs(struct file *file, void *__fh, struct v4l2_requestbuffe
        }
 
        if (fh->map_mode == ZORAN_MAP_MODE_RAW &&
-                       req->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
-
+           req->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
                /* control user input */
                if (req->count < 2)
                        req->count = 2;
                if (req->count > v4l_nbufs)
                        req->count = v4l_nbufs;
-               fh->v4l_buffers.num_buffers = req->count;
 
-               if (v4l_fbuffer_alloc(file)) {
+               /* The next mmap will map the V4L buffers */
+               map_mode_raw(fh);
+               fh->buffers.num_buffers = req->count;
+
+               if (v4l_fbuffer_alloc(fh)) {
                        res = -ENOMEM;
                        goto v4l2reqbuf_unlock_and_return;
                }
-
-               /* The next mmap will map the V4L buffers */
-               fh->map_mode = ZORAN_MAP_MODE_RAW;
-
        } else if (fh->map_mode == ZORAN_MAP_MODE_JPG_REC ||
-                       fh->map_mode == ZORAN_MAP_MODE_JPG_PLAY) {
-
+                  fh->map_mode == ZORAN_MAP_MODE_JPG_PLAY) {
                /* we need to calculate size ourselves now */
                if (req->count < 4)
                        req->count = 4;
                if (req->count > jpg_nbufs)
                        req->count = jpg_nbufs;
-               fh->jpg_buffers.num_buffers = req->count;
-               fh->jpg_buffers.buffer_size =
-                       zoran_v4l2_calc_bufsize(&fh->jpg_settings);
 
-               if (jpg_fbuffer_alloc(file)) {
+               /* The next mmap will map the MJPEG buffers */
+               map_mode_jpg(fh, req->type == V4L2_BUF_TYPE_VIDEO_OUTPUT);
+               fh->buffers.num_buffers = req->count;
+               fh->buffers.buffer_size = zoran_v4l2_calc_bufsize(&fh->jpg_settings);
+
+               if (jpg_fbuffer_alloc(fh)) {
                        res = -ENOMEM;
                        goto v4l2reqbuf_unlock_and_return;
                }
-
-               /* The next mmap will map the MJPEG buffers */
-               if (req->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
-                       fh->map_mode = ZORAN_MAP_MODE_JPG_REC;
-               else
-                       fh->map_mode = ZORAN_MAP_MODE_JPG_PLAY;
-
        } else {
                dprintk(1,
                                KERN_ERR
@@ -2578,15 +2403,10 @@ static int zoran_querybuf(struct file *file, void *__fh, struct v4l2_buffer *buf
 {
        struct zoran_fh *fh = __fh;
        struct zoran *zr = fh->zr;
-       __u32 type = buf->type;
-       int index = buf->index, res;
-
-       memset(buf, 0, sizeof(*buf));
-       buf->type = type;
-       buf->index = index;
+       int res;
 
        mutex_lock(&zr->resource_lock);
-       res = zoran_v4l2_buffer_status(file, buf, buf->index);
+       res = zoran_v4l2_buffer_status(fh, buf, buf->index);
        mutex_unlock(&zr->resource_lock);
 
        return res;
@@ -2610,11 +2430,10 @@ static int zoran_qbuf(struct file *file, void *__fh, struct v4l2_buffer *buf)
                        goto qbuf_unlock_and_return;
                }
 
-               res = zoran_v4l_queue_frame(file, buf->index);
+               res = zoran_v4l_queue_frame(fh, buf->index);
                if (res)
                        goto qbuf_unlock_and_return;
-               if (!zr->v4l_memgrab_active &&
-                               fh->v4l_buffers.active == ZORAN_LOCKED)
+               if (!zr->v4l_memgrab_active && fh->buffers.active == ZORAN_LOCKED)
                        zr36057_set_memgrab(zr, 1);
                break;
 
@@ -2636,14 +2455,13 @@ static int zoran_qbuf(struct file *file, void *__fh, struct v4l2_buffer *buf)
                        goto qbuf_unlock_and_return;
                }
 
-               res = zoran_jpg_queue_frame(file, buf->index,
-                                       codec_mode);
+               res = zoran_jpg_queue_frame(fh, buf->index, codec_mode);
                if (res != 0)
                        goto qbuf_unlock_and_return;
                if (zr->codec_mode == BUZ_MODE_IDLE &&
-                               fh->jpg_buffers.active == ZORAN_LOCKED) {
+                   fh->buffers.active == ZORAN_LOCKED)
                        zr36057_enable_jpg(zr, codec_mode);
-               }
+
                break;
 
        default:
@@ -2683,11 +2501,11 @@ static int zoran_dqbuf(struct file *file, void *__fh, struct v4l2_buffer *buf)
                        res = -EAGAIN;
                        goto dqbuf_unlock_and_return;
                }
-               res = v4l_sync(file, num);
+               res = v4l_sync(fh, num);
                if (res)
                        goto dqbuf_unlock_and_return;
                zr->v4l_sync_tail++;
-               res = zoran_v4l2_buffer_status(file, buf, num);
+               res = zoran_v4l2_buffer_status(fh, buf, num);
                break;
 
        case ZORAN_MAP_MODE_JPG_REC:
@@ -2715,10 +2533,10 @@ static int zoran_dqbuf(struct file *file, void *__fh, struct v4l2_buffer *buf)
                        res = -EAGAIN;
                        goto dqbuf_unlock_and_return;
                }
-               res = jpg_sync(file, &bs);
+               res = jpg_sync(fh, &bs);
                if (res)
                        goto dqbuf_unlock_and_return;
-               res = zoran_v4l2_buffer_status(file, buf, bs.frame);
+               res = zoran_v4l2_buffer_status(fh, buf, bs.frame);
                break;
        }
 
@@ -2746,12 +2564,12 @@ static int zoran_streamon(struct file *file, void *__fh, enum v4l2_buf_type type
        switch (fh->map_mode) {
        case ZORAN_MAP_MODE_RAW:        /* raw capture */
                if (zr->v4l_buffers.active != ZORAN_ACTIVE ||
-                   fh->v4l_buffers.active != ZORAN_ACTIVE) {
+                   fh->buffers.active != ZORAN_ACTIVE) {
                        res = -EBUSY;
                        goto strmon_unlock_and_return;
                }
 
-               zr->v4l_buffers.active = fh->v4l_buffers.active = ZORAN_LOCKED;
+               zr->v4l_buffers.active = fh->buffers.active = ZORAN_LOCKED;
                zr->v4l_settings = fh->v4l_settings;
 
                zr->v4l_sync_tail = zr->v4l_pend_tail;
@@ -2765,12 +2583,12 @@ static int zoran_streamon(struct file *file, void *__fh, enum v4l2_buf_type type
        case ZORAN_MAP_MODE_JPG_PLAY:
                /* what is the codec mode right now? */
                if (zr->jpg_buffers.active != ZORAN_ACTIVE ||
-                   fh->jpg_buffers.active != ZORAN_ACTIVE) {
+                   fh->buffers.active != ZORAN_ACTIVE) {
                        res = -EBUSY;
                        goto strmon_unlock_and_return;
                }
 
-               zr->jpg_buffers.active = fh->jpg_buffers.active = ZORAN_LOCKED;
+               zr->jpg_buffers.active = fh->buffers.active = ZORAN_LOCKED;
 
                if (zr->jpg_que_head != zr->jpg_que_tail) {
                        /* Start the jpeg codec when the first frame is queued  */
@@ -2797,12 +2615,13 @@ static int zoran_streamoff(struct file *file, void *__fh, enum v4l2_buf_type typ
        struct zoran_fh *fh = __fh;
        struct zoran *zr = fh->zr;
        int i, res = 0;
+       unsigned long flags;
 
        mutex_lock(&zr->resource_lock);
 
        switch (fh->map_mode) {
        case ZORAN_MAP_MODE_RAW:        /* raw capture */
-               if (fh->v4l_buffers.active == ZORAN_FREE &&
+               if (fh->buffers.active == ZORAN_FREE &&
                    zr->v4l_buffers.active != ZORAN_FREE) {
                        res = -EPERM;   /* stay off other's settings! */
                        goto strmoff_unlock_and_return;
@@ -2810,30 +2629,30 @@ static int zoran_streamoff(struct file *file, void *__fh, enum v4l2_buf_type typ
                if (zr->v4l_buffers.active == ZORAN_FREE)
                        goto strmoff_unlock_and_return;
 
+               spin_lock_irqsave(&zr->spinlock, flags);
                /* unload capture */
                if (zr->v4l_memgrab_active) {
-                       unsigned long flags;
 
-                       spin_lock_irqsave(&zr->spinlock, flags);
                        zr36057_set_memgrab(zr, 0);
-                       spin_unlock_irqrestore(&zr->spinlock, flags);
                }
 
-               for (i = 0; i < fh->v4l_buffers.num_buffers; i++)
+               for (i = 0; i < fh->buffers.num_buffers; i++)
                        zr->v4l_buffers.buffer[i].state = BUZ_STATE_USER;
-               fh->v4l_buffers = zr->v4l_buffers;
+               fh->buffers = zr->v4l_buffers;
 
-               zr->v4l_buffers.active = fh->v4l_buffers.active = ZORAN_FREE;
+               zr->v4l_buffers.active = fh->buffers.active = ZORAN_FREE;
 
                zr->v4l_grab_seq = 0;
                zr->v4l_pend_head = zr->v4l_pend_tail = 0;
                zr->v4l_sync_tail = 0;
 
+               spin_unlock_irqrestore(&zr->spinlock, flags);
+
                break;
 
        case ZORAN_MAP_MODE_JPG_REC:
        case ZORAN_MAP_MODE_JPG_PLAY:
-               if (fh->jpg_buffers.active == ZORAN_FREE &&
+               if (fh->buffers.active == ZORAN_FREE &&
                    zr->jpg_buffers.active != ZORAN_FREE) {
                        res = -EPERM;   /* stay off other's settings! */
                        goto strmoff_unlock_and_return;
@@ -2841,7 +2660,7 @@ static int zoran_streamoff(struct file *file, void *__fh, enum v4l2_buf_type typ
                if (zr->jpg_buffers.active == ZORAN_FREE)
                        goto strmoff_unlock_and_return;
 
-               res = jpg_qbuf(file, -1,
+               res = jpg_qbuf(fh, -1,
                             (fh->map_mode == ZORAN_MAP_MODE_JPG_REC) ?
                             BUZ_MODE_MOTION_COMPRESS :
                             BUZ_MODE_MOTION_DECOMPRESS);
@@ -2872,7 +2691,7 @@ static int zoran_queryctrl(struct file *file, void *__fh,
            ctrl->id > V4L2_CID_HUE)
                return -EINVAL;
 
-       decoder_command(zr, VIDIOC_QUERYCTRL, ctrl);
+       decoder_call(zr, core, queryctrl, ctrl);
 
        return 0;
 }
@@ -2888,7 +2707,7 @@ static int zoran_g_ctrl(struct file *file, void *__fh, struct v4l2_control *ctrl
                return -EINVAL;
 
        mutex_lock(&zr->resource_lock);
-       decoder_command(zr, VIDIOC_G_CTRL, ctrl);
+       decoder_call(zr, core, g_ctrl, ctrl);
        mutex_unlock(&zr->resource_lock);
 
        return 0;
@@ -2905,7 +2724,7 @@ static int zoran_s_ctrl(struct file *file, void *__fh, struct v4l2_control *ctrl
                return -EINVAL;
 
        mutex_lock(&zr->resource_lock);
-       decoder_command(zr, VIDIOC_S_CTRL, ctrl);
+       decoder_call(zr, core, s_ctrl, ctrl);
        mutex_unlock(&zr->resource_lock);
 
        return 0;
@@ -2960,7 +2779,7 @@ static int zoran_enum_input(struct file *file, void *__fh,
 
        /* Get status of video decoder */
        mutex_lock(&zr->resource_lock);
-       decoder_command(zr, VIDIOC_INT_G_INPUT_STATUS, &inp->status);
+       decoder_call(zr, video, g_input_status, &inp->status);
        mutex_unlock(&zr->resource_lock);
        return 0;
 }
@@ -3102,7 +2921,7 @@ static int zoran_s_crop(struct file *file, void *__fh, struct v4l2_crop *crop)
 
        mutex_lock(&zr->resource_lock);
 
-       if (fh->jpg_buffers.allocated || fh->v4l_buffers.allocated) {
+       if (fh->buffers.allocated) {
                dprintk(1, KERN_ERR
                        "%s: VIDIOC_S_CROP - cannot change settings while active\n",
                        ZR_DEVNAME(zr));
@@ -3180,8 +2999,7 @@ static int zoran_s_jpegcomp(struct file *file, void *__fh,
 
        mutex_lock(&zr->resource_lock);
 
-       if (fh->v4l_buffers.active != ZORAN_FREE ||
-           fh->jpg_buffers.active != ZORAN_FREE) {
+       if (fh->buffers.active != ZORAN_FREE) {
                dprintk(1, KERN_WARNING
                        "%s: VIDIOC_S_JPEGCOMP called while in playback/capture mode\n",
                        ZR_DEVNAME(zr));
@@ -3192,14 +3010,14 @@ static int zoran_s_jpegcomp(struct file *file, void *__fh,
        res = zoran_check_jpg_settings(zr, &settings, 0);
        if (res)
                goto sjpegc_unlock_and_return;
-       if (!fh->jpg_buffers.allocated)
-               fh->jpg_buffers.buffer_size =
-                   zoran_v4l2_calc_bufsize(&fh->jpg_settings);
+       if (!fh->buffers.allocated)
+               fh->buffers.buffer_size =
+                       zoran_v4l2_calc_bufsize(&fh->jpg_settings);
        fh->jpg_settings.jpg_comp = *params = settings.jpg_comp;
 sjpegc_unlock_and_return:
        mutex_unlock(&zr->resource_lock);
 
-       return 0;
+       return res;
 }
 
 static unsigned int
@@ -3231,11 +3049,11 @@ zoran_poll (struct file *file,
                        KERN_DEBUG
                        "%s: %s() raw - active=%c, sync_tail=%lu/%c, pend_tail=%lu, pend_head=%lu\n",
                        ZR_DEVNAME(zr), __func__,
-                       "FAL"[fh->v4l_buffers.active], zr->v4l_sync_tail,
+                       "FAL"[fh->buffers.active], zr->v4l_sync_tail,
                        "UPMD"[zr->v4l_buffers.buffer[frame].state],
                        zr->v4l_pend_tail, zr->v4l_pend_head);
                /* Process is the one capturing? */
-               if (fh->v4l_buffers.active != ZORAN_FREE &&
+               if (fh->buffers.active != ZORAN_FREE &&
                    /* Buffer ready to DQBUF? */
                    zr->v4l_buffers.buffer[frame].state == BUZ_STATE_DONE)
                        res = POLLIN | POLLRDNORM;
@@ -3253,10 +3071,10 @@ zoran_poll (struct file *file,
                        KERN_DEBUG
                        "%s: %s() jpg - active=%c, que_tail=%lu/%c, que_head=%lu, dma=%lu/%lu\n",
                        ZR_DEVNAME(zr), __func__,
-                       "FAL"[fh->jpg_buffers.active], zr->jpg_que_tail,
+                       "FAL"[fh->buffers.active], zr->jpg_que_tail,
                        "UPMD"[zr->jpg_buffers.buffer[frame].state],
                        zr->jpg_que_head, zr->jpg_dma_tail, zr->jpg_dma_head);
-               if (fh->jpg_buffers.active != ZORAN_FREE &&
+               if (fh->buffers.active != ZORAN_FREE &&
                    zr->jpg_buffers.buffer[frame].state == BUZ_STATE_DONE) {
                        if (fh->map_mode == ZORAN_MAP_MODE_JPG_REC)
                                res = POLLIN | POLLRDNORM;
@@ -3270,8 +3088,8 @@ zoran_poll (struct file *file,
        default:
                dprintk(1,
                        KERN_ERR
-                       "%s: zoran_poll() - internal error, unknown map_mode=%d\n",
-                       ZR_DEVNAME(zr), fh->map_mode);
+                       "%s: %s - internal error, unknown map_mode=%d\n",
+                       ZR_DEVNAME(zr), __func__, fh->map_mode);
                res = POLLNVAL;
        }
 
@@ -3305,95 +3123,56 @@ static void
 zoran_vm_close (struct vm_area_struct *vma)
 {
        struct zoran_mapping *map = vma->vm_private_data;
-       struct file *file = map->file;
-       struct zoran_fh *fh = file->private_data;
+       struct zoran_fh *fh = map->file->private_data;
        struct zoran *zr = fh->zr;
        int i;
 
-       map->count--;
-       if (map->count == 0) {
-               switch (fh->map_mode) {
-               case ZORAN_MAP_MODE_JPG_REC:
-               case ZORAN_MAP_MODE_JPG_PLAY:
-
-                       dprintk(3, KERN_INFO "%s: munmap(MJPEG)\n",
-                               ZR_DEVNAME(zr));
-
-                       for (i = 0; i < fh->jpg_buffers.num_buffers; i++) {
-                               if (fh->jpg_buffers.buffer[i].map == map) {
-                                       fh->jpg_buffers.buffer[i].map =
-                                           NULL;
-                               }
-                       }
-                       kfree(map);
-
-                       for (i = 0; i < fh->jpg_buffers.num_buffers; i++)
-                               if (fh->jpg_buffers.buffer[i].map)
-                                       break;
-                       if (i == fh->jpg_buffers.num_buffers) {
-                               mutex_lock(&zr->resource_lock);
-
-                               if (fh->jpg_buffers.active != ZORAN_FREE) {
-                                       jpg_qbuf(file, -1, zr->codec_mode);
-                                       zr->jpg_buffers.allocated = 0;
-                                       zr->jpg_buffers.active =
-                                           fh->jpg_buffers.active =
-                                           ZORAN_FREE;
-                               }
-                               jpg_fbuffer_free(file);
-                               mutex_unlock(&zr->resource_lock);
-                       }
+       if (--map->count > 0)
+               return;
 
-                       break;
+       dprintk(3, KERN_INFO "%s: %s - munmap(%s)\n", ZR_DEVNAME(zr),
+               __func__, mode_name(fh->map_mode));
 
-               case ZORAN_MAP_MODE_RAW:
-
-                       dprintk(3, KERN_INFO "%s: munmap(V4L)\n",
-                               ZR_DEVNAME(zr));
+       for (i = 0; i < fh->buffers.num_buffers; i++) {
+               if (fh->buffers.buffer[i].map == map)
+                       fh->buffers.buffer[i].map = NULL;
+       }
+       kfree(map);
 
-                       for (i = 0; i < fh->v4l_buffers.num_buffers; i++) {
-                               if (fh->v4l_buffers.buffer[i].map == map) {
-                                       /* unqueue/unmap */
-                                       fh->v4l_buffers.buffer[i].map =
-                                           NULL;
-                               }
-                       }
-                       kfree(map);
+       /* Any buffers still mapped? */
+       for (i = 0; i < fh->buffers.num_buffers; i++)
+               if (fh->buffers.buffer[i].map)
+                       return;
 
-                       for (i = 0; i < fh->v4l_buffers.num_buffers; i++)
-                               if (fh->v4l_buffers.buffer[i].map)
-                                       break;
-                       if (i == fh->v4l_buffers.num_buffers) {
-                               mutex_lock(&zr->resource_lock);
-
-                               if (fh->v4l_buffers.active != ZORAN_FREE) {
-                                       unsigned long flags;
-
-                                       spin_lock_irqsave(&zr->spinlock, flags);
-                                       zr36057_set_memgrab(zr, 0);
-                                       zr->v4l_buffers.allocated = 0;
-                                       zr->v4l_buffers.active =
-                                           fh->v4l_buffers.active =
-                                           ZORAN_FREE;
-                                       spin_unlock_irqrestore(&zr->spinlock, flags);
-                               }
-                               v4l_fbuffer_free(file);
-                               mutex_unlock(&zr->resource_lock);
-                       }
+       dprintk(3, KERN_INFO "%s: %s - free %s buffers\n", ZR_DEVNAME(zr),
+               __func__, mode_name(fh->map_mode));
 
-                       break;
+       mutex_lock(&zr->resource_lock);
 
-               default:
-                       printk(KERN_ERR
-                              "%s: munmap() - internal error - unknown map mode %d\n",
-                              ZR_DEVNAME(zr), fh->map_mode);
-                       break;
+       if (fh->map_mode == ZORAN_MAP_MODE_RAW) {
+               if (fh->buffers.active != ZORAN_FREE) {
+                       unsigned long flags;
 
+                       spin_lock_irqsave(&zr->spinlock, flags);
+                       zr36057_set_memgrab(zr, 0);
+                       zr->v4l_buffers.allocated = 0;
+                       zr->v4l_buffers.active = fh->buffers.active = ZORAN_FREE;
+                       spin_unlock_irqrestore(&zr->spinlock, flags);
+               }
+               v4l_fbuffer_free(fh);
+       } else {
+               if (fh->buffers.active != ZORAN_FREE) {
+                       jpg_qbuf(fh, -1, zr->codec_mode);
+                       zr->jpg_buffers.allocated = 0;
+                       zr->jpg_buffers.active = fh->buffers.active = ZORAN_FREE;
                }
+               jpg_fbuffer_free(fh);
        }
+
+       mutex_unlock(&zr->resource_lock);
 }
 
-static struct vm_operations_struct zoran_vm_ops = {
+static const struct vm_operations_struct zoran_vm_ops = {
        .open = zoran_vm_open,
        .close = zoran_vm_close,
 };
@@ -3413,90 +3192,106 @@ zoran_mmap (struct file           *file,
        int res = 0;
 
        dprintk(3,
-               KERN_INFO "%s: mmap(%s) of 0x%08lx-0x%08lx (size=%lu)\n",
-               ZR_DEVNAME(zr),
-               fh->map_mode == ZORAN_MAP_MODE_RAW ? "V4L" : "MJPEG",
-               vma->vm_start, vma->vm_end, size);
+               KERN_INFO "%s: %s(%s) of 0x%08lx-0x%08lx (size=%lu)\n",
+               ZR_DEVNAME(zr), __func__,
+               mode_name(fh->map_mode), vma->vm_start, vma->vm_end, size);
 
        if (!(vma->vm_flags & VM_SHARED) || !(vma->vm_flags & VM_READ) ||
            !(vma->vm_flags & VM_WRITE)) {
                dprintk(1,
                        KERN_ERR
-                       "%s: mmap() - no MAP_SHARED/PROT_{READ,WRITE} given\n",
-                       ZR_DEVNAME(zr));
+                       "%s: %s - no MAP_SHARED/PROT_{READ,WRITE} given\n",
+                       ZR_DEVNAME(zr), __func__);
                return -EINVAL;
        }
 
-       switch (fh->map_mode) {
+       mutex_lock(&zr->resource_lock);
 
-       case ZORAN_MAP_MODE_JPG_REC:
-       case ZORAN_MAP_MODE_JPG_PLAY:
+       if (!fh->buffers.allocated) {
+               dprintk(1,
+                       KERN_ERR
+                       "%s: %s(%s) - buffers not yet allocated\n",
+                       ZR_DEVNAME(zr), __func__, mode_name(fh->map_mode));
+               res = -ENOMEM;
+               goto mmap_unlock_and_return;
+       }
 
-               /* lock */
-               mutex_lock(&zr->resource_lock);
+       first = offset / fh->buffers.buffer_size;
+       last = first - 1 + size / fh->buffers.buffer_size;
+       if (offset % fh->buffers.buffer_size != 0 ||
+           size % fh->buffers.buffer_size != 0 || first < 0 ||
+           last < 0 || first >= fh->buffers.num_buffers ||
+           last >= fh->buffers.buffer_size) {
+               dprintk(1,
+                       KERN_ERR
+                       "%s: %s(%s) - offset=%lu or size=%lu invalid for bufsize=%d and numbufs=%d\n",
+                       ZR_DEVNAME(zr), __func__, mode_name(fh->map_mode), offset, size,
+                       fh->buffers.buffer_size,
+                       fh->buffers.num_buffers);
+               res = -EINVAL;
+               goto mmap_unlock_and_return;
+       }
 
-               /* Map the MJPEG buffers */
-               if (!fh->jpg_buffers.allocated) {
+       /* Check if any buffers are already mapped */
+       for (i = first; i <= last; i++) {
+               if (fh->buffers.buffer[i].map) {
                        dprintk(1,
                                KERN_ERR
-                               "%s: zoran_mmap(MJPEG) - buffers not yet allocated\n",
-                               ZR_DEVNAME(zr));
-                       res = -ENOMEM;
-                       goto jpg_mmap_unlock_and_return;
+                               "%s: %s(%s) - buffer %d already mapped\n",
+                               ZR_DEVNAME(zr), __func__, mode_name(fh->map_mode), i);
+                       res = -EBUSY;
+                       goto mmap_unlock_and_return;
                }
+       }
 
-               first = offset / fh->jpg_buffers.buffer_size;
-               last = first - 1 + size / fh->jpg_buffers.buffer_size;
-               if (offset % fh->jpg_buffers.buffer_size != 0 ||
-                   size % fh->jpg_buffers.buffer_size != 0 || first < 0 ||
-                   last < 0 || first >= fh->jpg_buffers.num_buffers ||
-                   last >= fh->jpg_buffers.num_buffers) {
-                       dprintk(1,
-                               KERN_ERR
-                               "%s: mmap(MJPEG) - offset=%lu or size=%lu invalid for bufsize=%d and numbufs=%d\n",
-                               ZR_DEVNAME(zr), offset, size,
-                               fh->jpg_buffers.buffer_size,
-                               fh->jpg_buffers.num_buffers);
-                       res = -EINVAL;
-                       goto jpg_mmap_unlock_and_return;
-               }
+       /* map these buffers */
+       map = kmalloc(sizeof(struct zoran_mapping), GFP_KERNEL);
+       if (!map) {
+               res = -ENOMEM;
+               goto mmap_unlock_and_return;
+       }
+       map->file = file;
+       map->count = 1;
+
+       vma->vm_ops = &zoran_vm_ops;
+       vma->vm_flags |= VM_DONTEXPAND;
+       vma->vm_private_data = map;
+
+       if (fh->map_mode == ZORAN_MAP_MODE_RAW) {
                for (i = first; i <= last; i++) {
-                       if (fh->jpg_buffers.buffer[i].map) {
+                       todo = size;
+                       if (todo > fh->buffers.buffer_size)
+                               todo = fh->buffers.buffer_size;
+                       page = fh->buffers.buffer[i].v4l.fbuffer_phys;
+                       if (remap_pfn_range(vma, start, page >> PAGE_SHIFT,
+                                                       todo, PAGE_SHARED)) {
                                dprintk(1,
                                        KERN_ERR
-                                       "%s: mmap(MJPEG) - buffer %d already mapped\n",
-                                       ZR_DEVNAME(zr), i);
-                               res = -EBUSY;
-                               goto jpg_mmap_unlock_and_return;
+                                       "%s: %s(V4L) - remap_pfn_range failed\n",
+                                       ZR_DEVNAME(zr), __func__);
+                               res = -EAGAIN;
+                               goto mmap_unlock_and_return;
                        }
+                       size -= todo;
+                       start += todo;
+                       fh->buffers.buffer[i].map = map;
+                       if (size == 0)
+                               break;
                }
-
-               /* map these buffers (v4l_buffers[i]) */
-               map = kmalloc(sizeof(struct zoran_mapping), GFP_KERNEL);
-               if (!map) {
-                       res = -ENOMEM;
-                       goto jpg_mmap_unlock_and_return;
-               }
-               map->file = file;
-               map->count = 1;
-
-               vma->vm_ops = &zoran_vm_ops;
-               vma->vm_flags |= VM_DONTEXPAND;
-               vma->vm_private_data = map;
-
+       } else {
                for (i = first; i <= last; i++) {
                        for (j = 0;
-                            j < fh->jpg_buffers.buffer_size / PAGE_SIZE;
+                            j < fh->buffers.buffer_size / PAGE_SIZE;
                             j++) {
                                fraglen =
-                                   (le32_to_cpu(fh->jpg_buffers.buffer[i].
+                                   (le32_to_cpu(fh->buffers.buffer[i].jpg.
                                     frag_tab[2 * j + 1]) & ~1) << 1;
                                todo = size;
                                if (todo > fraglen)
                                        todo = fraglen;
                                pos =
-                                   le32_to_cpu(fh->jpg_buffers.
-                                   buffer[i].frag_tab[2 * j]);
+                                   le32_to_cpu(fh->buffers.
+                                   buffer[i].jpg.frag_tab[2 * j]);
                                /* should just be pos on i386 */
                                page = virt_to_phys(bus_to_virt(pos))
                                                                >> PAGE_SHIFT;
@@ -3504,115 +3299,29 @@ zoran_mmap (struct file           *file,
                                                        todo, PAGE_SHARED)) {
                                        dprintk(1,
                                                KERN_ERR
-                                               "%s: zoran_mmap(V4L) - remap_pfn_range failed\n",
-                                               ZR_DEVNAME(zr));
+                                               "%s: %s(V4L) - remap_pfn_range failed\n",
+                                               ZR_DEVNAME(zr), __func__);
                                        res = -EAGAIN;
-                                       goto jpg_mmap_unlock_and_return;
+                                       goto mmap_unlock_and_return;
                                }
                                size -= todo;
                                start += todo;
                                if (size == 0)
                                        break;
-                               if (le32_to_cpu(fh->jpg_buffers.buffer[i].
+                               if (le32_to_cpu(fh->buffers.buffer[i].jpg.
                                    frag_tab[2 * j + 1]) & 1)
                                        break;  /* was last fragment */
                        }
-                       fh->jpg_buffers.buffer[i].map = map;
+                       fh->buffers.buffer[i].map = map;
                        if (size == 0)
                                break;
 
                }
-       jpg_mmap_unlock_and_return:
-               mutex_unlock(&zr->resource_lock);
-
-               break;
-
-       case ZORAN_MAP_MODE_RAW:
-
-               mutex_lock(&zr->resource_lock);
-
-               /* Map the V4L buffers */
-               if (!fh->v4l_buffers.allocated) {
-                       dprintk(1,
-                               KERN_ERR
-                               "%s: zoran_mmap(V4L) - buffers not yet allocated\n",
-                               ZR_DEVNAME(zr));
-                       res = -ENOMEM;
-                       goto v4l_mmap_unlock_and_return;
-               }
-
-               first = offset / fh->v4l_buffers.buffer_size;
-               last = first - 1 + size / fh->v4l_buffers.buffer_size;
-               if (offset % fh->v4l_buffers.buffer_size != 0 ||
-                   size % fh->v4l_buffers.buffer_size != 0 || first < 0 ||
-                   last < 0 || first >= fh->v4l_buffers.num_buffers ||
-                   last >= fh->v4l_buffers.buffer_size) {
-                       dprintk(1,
-                               KERN_ERR
-                               "%s: mmap(V4L) - offset=%lu or size=%lu invalid for bufsize=%d and numbufs=%d\n",
-                               ZR_DEVNAME(zr), offset, size,
-                               fh->v4l_buffers.buffer_size,
-                               fh->v4l_buffers.num_buffers);
-                       res = -EINVAL;
-                       goto v4l_mmap_unlock_and_return;
-               }
-               for (i = first; i <= last; i++) {
-                       if (fh->v4l_buffers.buffer[i].map) {
-                               dprintk(1,
-                                       KERN_ERR
-                                       "%s: mmap(V4L) - buffer %d already mapped\n",
-                                       ZR_DEVNAME(zr), i);
-                               res = -EBUSY;
-                               goto v4l_mmap_unlock_and_return;
-                       }
-               }
-
-               /* map these buffers (v4l_buffers[i]) */
-               map = kmalloc(sizeof(struct zoran_mapping), GFP_KERNEL);
-               if (!map) {
-                       res = -ENOMEM;
-                       goto v4l_mmap_unlock_and_return;
-               }
-               map->file = file;
-               map->count = 1;
-
-               vma->vm_ops = &zoran_vm_ops;
-               vma->vm_flags |= VM_DONTEXPAND;
-               vma->vm_private_data = map;
-
-               for (i = first; i <= last; i++) {
-                       todo = size;
-                       if (todo > fh->v4l_buffers.buffer_size)
-                               todo = fh->v4l_buffers.buffer_size;
-                       page = fh->v4l_buffers.buffer[i].fbuffer_phys;
-                       if (remap_pfn_range(vma, start, page >> PAGE_SHIFT,
-                                                       todo, PAGE_SHARED)) {
-                               dprintk(1,
-                                       KERN_ERR
-                                       "%s: zoran_mmap(V4L)i - remap_pfn_range failed\n",
-                                       ZR_DEVNAME(zr));
-                               res = -EAGAIN;
-                               goto v4l_mmap_unlock_and_return;
-                       }
-                       size -= todo;
-                       start += todo;
-                       fh->v4l_buffers.buffer[i].map = map;
-                       if (size == 0)
-                               break;
-               }
-       v4l_mmap_unlock_and_return:
-               mutex_unlock(&zr->resource_lock);
-
-               break;
-
-       default:
-               dprintk(1,
-                       KERN_ERR
-                       "%s: zoran_mmap() - internal error - unknown map mode %d\n",
-                       ZR_DEVNAME(zr), fh->map_mode);
-               break;
        }
 
+mmap_unlock_and_return:
+       mutex_unlock(&zr->resource_lock);
+
        return 0;
 }