Merge to Fedora kernel-2.6.18-1.2224_FC5 patched with stable patch-2.6.18.1-vs2.0...
[linux-2.6.git] / drivers / media / video / zoran_driver.c
index d6294bd..5f90db2 100644 (file)
@@ -44,7 +44,6 @@
  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  */
 
-#include <linux/config.h>
 #include <linux/version.h>
 #include <linux/init.h>
 #include <linux/module.h>
@@ -52,6 +51,8 @@
 #include <linux/slab.h>
 #include <linux/pci.h>
 #include <linux/vmalloc.h>
+#include <linux/wait.h>
+#include <linux/byteorder/generic.h>
 
 #include <linux/interrupt.h>
 #include <linux/i2c.h>
@@ -71,6 +72,7 @@
                             )
 
 #include <linux/videodev.h>
+#include <media/v4l2-common.h>
 #include "videocodec.h"
 
 #include <asm/io.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"
 
-#ifdef HAVE_V4L2
+#ifdef CONFIG_VIDEO_V4L2
        /* 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 ( \
@@ -91,7 +94,7 @@
                                V4L2_CAP_VIDEO_CAPTURE |\
                                V4L2_CAP_VIDEO_OUTPUT |\
                                V4L2_CAP_VIDEO_OVERLAY \
-                              )
+                             )
 #endif
 
 #include <asm/byteorder.h>
@@ -100,7 +103,7 @@ const struct zoran_format zoran_formats[] = {
        {
                .name = "15-bit RGB",
                .palette = VIDEO_PALETTE_RGB555,
-#ifdef HAVE_V4L2
+#ifdef CONFIG_VIDEO_V4L2
 #ifdef __LITTLE_ENDIAN
                .fourcc = V4L2_PIX_FMT_RGB555,
 #else
@@ -114,7 +117,7 @@ const struct zoran_format zoran_formats[] = {
        }, {
                .name = "16-bit RGB",
                .palette = VIDEO_PALETTE_RGB565,
-#ifdef HAVE_V4L2
+#ifdef CONFIG_VIDEO_V4L2
 #ifdef __LITTLE_ENDIAN
                .fourcc = V4L2_PIX_FMT_RGB565,
 #else
@@ -128,7 +131,7 @@ const struct zoran_format zoran_formats[] = {
        }, {
                .name = "24-bit RGB",
                .palette = VIDEO_PALETTE_RGB24,
-#ifdef HAVE_V4L2
+#ifdef CONFIG_VIDEO_V4L2
 #ifdef __LITTLE_ENDIAN
                .fourcc = V4L2_PIX_FMT_BGR24,
 #else
@@ -142,7 +145,7 @@ const struct zoran_format zoran_formats[] = {
        }, {
                .name = "32-bit RGB",
                .palette = VIDEO_PALETTE_RGB32,
-#ifdef HAVE_V4L2
+#ifdef CONFIG_VIDEO_V4L2
 #ifdef __LITTLE_ENDIAN
                .fourcc = V4L2_PIX_FMT_BGR32,
 #else
@@ -156,17 +159,17 @@ const struct zoran_format zoran_formats[] = {
        }, {
                .name = "4:2:2, packed, YUYV",
                .palette = VIDEO_PALETTE_YUV422,
-#ifdef HAVE_V4L2
+#ifdef CONFIG_VIDEO_V4L2
                .fourcc = V4L2_PIX_FMT_YUYV,
                .colorspace = V4L2_COLORSPACE_SMPTE170M,
 #endif
                .depth = 16,
                .flags = ZORAN_FORMAT_CAPTURE |
-                        ZORAN_FORMAT_OVERLAY,
+                        ZORAN_FORMAT_OVERLAY,
        }, {
                .name = "Hardware-encoded Motion-JPEG",
                .palette = -1,
-#ifdef HAVE_V4L2
+#ifdef CONFIG_VIDEO_V4L2
                .fourcc = V4L2_PIX_FMT_MJPEG,
                .colorspace = V4L2_COLORSPACE_SMPTE170M,
 #endif
@@ -176,7 +179,7 @@ const struct zoran_format zoran_formats[] = {
                         ZORAN_FORMAT_COMPRESSED,
        }
 };
-const int zoran_num_formats =
+static const int zoran_num_formats =
     (sizeof(zoran_formats) / sizeof(struct zoran_format));
 
 // RJ: Test only - want to test BUZ_USE_HIMEM even when CONFIG_BIGPHYS_AREA is defined
@@ -204,10 +207,10 @@ extern int jpg_bufsize;
 extern int pass_through;
 
 static int lock_norm = 0;      /* 1=Don't change TV standard (norm) */
-MODULE_PARM(lock_norm, "i");
+module_param(lock_norm, int, 0);
 MODULE_PARM_DESC(lock_norm, "Users can't change norm");
 
-#ifdef HAVE_V4L2
+#ifdef CONFIG_VIDEO_V4L2
        /* small helper function for calculating buffersizes for v4l2
         * we calculate the nearest higher power-of-two, which
         * will be the recommended buffersize */
@@ -265,7 +268,7 @@ get_high_mem (unsigned long size)
  * if more than one driver at a time has the idea to use this memory!!!!
  */
 
-       volatile unsigned char *mem;
+       volatile unsigned char __iomem *mem;
        unsigned char c;
        unsigned long hi_mem_ph;
        unsigned long i;
@@ -285,21 +288,21 @@ get_high_mem (unsigned long size)
        for (i = 0; i < size; i++) {
                /* Check if it is memory */
                c = i & 0xff;
-               mem[i] = c;
-               if (mem[i] != c)
+               writeb(c, mem + i);
+               if (readb(mem + i) != c)
                        break;
                c = 255 - c;
-               mem[i] = c;
-               if (mem[i] != c)
+               writeb(c, mem + i);
+               if (readb(mem + i) != c)
                        break;
-               mem[i] = 0;     /* zero out memory */
+               writeb(0, mem + i);     /* zero out memory */
 
                /* give the kernel air to breath */
                if ((i & 0x3ffff) == 0x3ffff)
                        schedule();
        }
 
-       iounmap((void *) mem);
+       iounmap(mem);
 
        if (i != size) {
                dprintk(1,
@@ -516,6 +519,16 @@ v4l_fbuffer_free (struct file *file)
  *       virtual addresses) and then again have to make a lot of efforts
  *       to get the physical address.
  *
+ *   Ben Capper:
+ *       On big-endian architectures (such as ppc) some extra steps
+ *       are needed. When reading and writing to the stat_com array
+ *       and fragment buffers, the device expects to see little-
+ *       endian values. The use of cpu_to_le32() and le32_to_cpu()
+ *       in this function (and one or two others in zoran_device.c)
+ *       ensure that these values are always stored in little-endian
+ *       form, regardless of architecture. The zr36057 does Very Bad
+ *       Things on big endian architectures if the stat_com array
+ *       and fragment buffers are not little-endian.
  */
 
 static int
@@ -569,9 +582,9 @@ jpg_fbuffer_alloc (struct file *file)
                                return -ENOBUFS;
                        }
                        fh->jpg_buffers.buffer[i].frag_tab[0] =
-                           virt_to_bus((void *) mem);
+                           cpu_to_le32(virt_to_bus((void *) mem));
                        fh->jpg_buffers.buffer[i].frag_tab[1] =
-                           ((fh->jpg_buffers.buffer_size / 4) << 1) | 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));
@@ -591,14 +604,14 @@ jpg_fbuffer_alloc (struct file *file)
                                }
 
                                fh->jpg_buffers.buffer[i].frag_tab[2 * j] =
-                                   virt_to_bus((void *) mem);
+                                   cpu_to_le32(virt_to_bus((void *) mem));
                                fh->jpg_buffers.buffer[i].frag_tab[2 * j +
                                                                   1] =
-                                   (PAGE_SIZE / 4) << 1;
+                                   cpu_to_le32((PAGE_SIZE / 4) << 1);
                                SetPageReserved(MAP_NR(mem));
                        }
 
-                       fh->jpg_buffers.buffer[i].frag_tab[2 * j - 1] |= 1;
+                       fh->jpg_buffers.buffer[i].frag_tab[2 * j - 1] |= cpu_to_le32(1);
                }
        }
 
@@ -631,19 +644,14 @@ jpg_fbuffer_free (struct file *file)
                //if (alloc_contig) {
                if (fh->jpg_buffers.need_contiguous) {
                        if (fh->jpg_buffers.buffer[i].frag_tab[0]) {
-                               mem =
-                                   (unsigned char *) bus_to_virt(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));
-                               kfree((void *) mem);
+                               kfree(mem);
                                fh->jpg_buffers.buffer[i].frag_tab[0] = 0;
                                fh->jpg_buffers.buffer[i].frag_tab[1] = 0;
                        }
@@ -656,13 +664,16 @@ jpg_fbuffer_free (struct file *file)
                                        break;
                                ClearPageReserved(MAP_NR
                                                  (bus_to_virt
-                                                  (fh->jpg_buffers.
-                                                   buffer[i].frag_tab[2 *
-                                                                      j])));
+                                                  (le32_to_cpu
+                                                   (fh->jpg_buffers.
+                                                    buffer[i].frag_tab[2 *
+                                                                      j]))));
                                free_page((unsigned long)
-                                         bus_to_virt(fh->jpg_buffers.
+                                         bus_to_virt
+                                                 (le32_to_cpu
+                                                  (fh->jpg_buffers.
                                                      buffer[i].
-                                                     frag_tab[2 * j]));
+                                                     frag_tab[2 * j])));
                                fh->jpg_buffers.buffer[i].frag_tab[2 * j] =
                                    0;
                                fh->jpg_buffers.buffer[i].frag_tab[2 * j +
@@ -909,12 +920,12 @@ v4l_sync (struct file *file,
        }
 
        /* wait on this buffer to get ready */
-       while (zr->v4l_buffers.buffer[frame].state == BUZ_STATE_PEND) {
-               if (!interruptible_sleep_on_timeout(&zr->v4l_capq, 10 * HZ))
-                       return -ETIME;
-               else if (signal_pending(current))
-                       return -ERESTARTSYS;
-       }
+       if (!wait_event_interruptible_timeout(zr->v4l_capq,
+                               (zr->v4l_buffers.buffer[frame].state != BUZ_STATE_PEND),
+                               10*HZ))
+               return -ETIME;
+       if (signal_pending(current))
+               return -ERESTARTSYS;
 
        /* buffer should now be in BUZ_STATE_DONE */
        if (zr->v4l_buffers.buffer[frame].state != BUZ_STATE_DONE)
@@ -986,8 +997,6 @@ zoran_jpg_queue_frame (struct file          *file,
                return -EINVAL;
        }
 
-       spin_lock_irqsave(&zr->spinlock, flags);
-
        if (fh->jpg_buffers.active == ZORAN_FREE) {
                if (zr->jpg_buffers.active == ZORAN_FREE) {
                        zr->jpg_buffers = fh->jpg_buffers;
@@ -1006,6 +1015,8 @@ zoran_jpg_queue_frame (struct file          *file,
                zr36057_enable_jpg(zr, mode);
        }
 
+       spin_lock_irqsave(&zr->spinlock, flags);
+
        if (!res) {
                switch (zr->jpg_buffers.buffer[num].state) {
                case BUZ_STATE_DONE:
@@ -1098,7 +1109,7 @@ jpg_sync (struct file       *file,
        struct zoran_fh *fh = file->private_data;
        struct zoran *zr = fh->zr;
        unsigned long flags;
-       int frame, timeout;
+       int frame;
 
        if (fh->jpg_buffers.active == ZORAN_FREE) {
                dprintk(1,
@@ -1115,29 +1126,26 @@ jpg_sync (struct file       *file,
                        ZR_DEVNAME(zr));
                return -EINVAL;
        }
-       while (zr->jpg_que_tail == zr->jpg_dma_tail) {
-               if (zr->jpg_dma_tail == zr->jpg_dma_head)
-                       break;
+       if (!wait_event_interruptible_timeout(zr->jpg_capq,
+                       (zr->jpg_que_tail != zr->jpg_dma_tail ||
+                        zr->jpg_dma_tail == zr->jpg_dma_head),
+                       10*HZ)) {
+               int isr;
 
-               timeout =
-                   interruptible_sleep_on_timeout(&zr->jpg_capq, 10 * HZ);
-               if (!timeout) {
-                       int isr;
-
-                       btand(~ZR36057_JMC_Go_en, ZR36057_JMC);
-                       udelay(1);
-                       zr->codec->control(zr->codec, CODEC_G_STATUS,
+               btand(~ZR36057_JMC_Go_en, ZR36057_JMC);
+               udelay(1);
+               zr->codec->control(zr->codec, CODEC_G_STATUS,
                                           sizeof(isr), &isr);
-                       dprintk(1,
-                               KERN_ERR
-                               "%s: jpg_sync() - timeout: codec isr=0x%02x\n",
-                               ZR_DEVNAME(zr), isr);
+               dprintk(1,
+                       KERN_ERR
+                       "%s: jpg_sync() - timeout: codec isr=0x%02x\n",
+                       ZR_DEVNAME(zr), isr);
 
-                       return -ETIME;
+               return -ETIME;
 
-               } else if (signal_pending(current))
-                       return -ERESTARTSYS;
        }
+       if (signal_pending(current))
+               return -ERESTARTSYS;
 
        spin_lock_irqsave(&zr->spinlock, flags);
 
@@ -1285,7 +1293,7 @@ zoran_open (struct inode *inode,
 
        /* see fs/device.c - the kernel already locks during open(),
         * so locking ourselves only causes deadlocks */
-       /*down(&zr->resource_lock);*/
+       /*mutex_lock(&zr->resource_lock);*/
 
        if (!zr->decoder) {
                dprintk(1,
@@ -1304,7 +1312,7 @@ zoran_open (struct inode *inode,
                res = -ENODEV;
                goto open_unlock_and_return;
        }
-       if (!try_module_get(zr->decoder->driver->owner)) {
+       if (!try_module_get(zr->decoder->driver->driver.owner)) {
                dprintk(1,
                        KERN_ERR
                        "%s: failed to grab ownership of i2c decoder\n",
@@ -1314,13 +1322,13 @@ zoran_open (struct inode *inode,
                goto open_unlock_and_return;
        }
        if (zr->encoder &&
-           !try_module_get(zr->encoder->driver->owner)) {
+           !try_module_get(zr->encoder->driver->driver.owner)) {
                dprintk(1,
                        KERN_ERR
                        "%s: failed to grab ownership of i2c encoder\n",
                        ZR_DEVNAME(zr));
                res = -EIO;
-               module_put(zr->decoder->driver->owner);
+               module_put(zr->decoder->driver->driver.owner);
                module_put(THIS_MODULE);
                goto open_unlock_and_return;
        }
@@ -1338,7 +1346,7 @@ zoran_open (struct inode *inode,
                ZR_DEVNAME(zr), current->comm, current->pid, zr->user);
 
        /* now, create the open()-specific file_ops struct */
-       fh = kmalloc(sizeof(struct zoran_fh), GFP_KERNEL);
+       fh = kzalloc(sizeof(struct zoran_fh), GFP_KERNEL);
        if (!fh) {
                dprintk(1,
                        KERN_ERR
@@ -1347,7 +1355,6 @@ zoran_open (struct inode *inode,
                res = -ENOMEM;
                goto open_unlock_and_return;
        }
-       memset(fh, 0, sizeof(struct zoran_fh));
        /* used to be BUZ_MAX_WIDTH/HEIGHT, but that gives overflows
         * on norm-change! */
        fh->overlay_mask =
@@ -1365,7 +1372,7 @@ zoran_open (struct inode *inode,
        if (zr->user++ == 0)
                first_open = 1;
 
-       /*up(&zr->resource_lock);*/
+       /*mutex_unlock(&zr->resource_lock);*/
 
        /* default setup - TODO: look at flags */
        if (first_open) {       /* First device open */
@@ -1386,16 +1393,16 @@ zoran_open (struct inode *inode,
 open_unlock_and_return:
        /* if we grabbed locks, release them accordingly */
        if (have_module_locks) {
-               module_put(zr->decoder->driver->owner);
+               module_put(zr->decoder->driver->driver.owner);
                if (zr->encoder) {
-                       module_put(zr->encoder->driver->owner);
+                       module_put(zr->encoder->driver->driver.owner);
                }
                module_put(THIS_MODULE);
        }
 
        /* if there's no device found, we didn't obtain the lock either */
        if (zr) {
-               /*up(&zr->resource_lock);*/
+               /*mutex_unlock(&zr->resource_lock);*/
        }
 
        return res;
@@ -1413,7 +1420,7 @@ zoran_close (struct inode *inode,
 
        /* kernel locks (fs/device.c), so don't do that ourselves
         * (prevents deadlocks) */
-       /*down(&zr->resource_lock);*/
+       /*mutex_lock(&zr->resource_lock);*/
 
        zoran_close_end_session(file);
 
@@ -1454,13 +1461,13 @@ zoran_close (struct inode *inode,
        kfree(fh);
 
        /* release locks on the i2c modules */
-       module_put(zr->decoder->driver->owner);
+       module_put(zr->decoder->driver->driver.owner);
        if (zr->encoder) {
-                module_put(zr->encoder->driver->owner);
+                module_put(zr->encoder->driver->driver.owner);
        }
        module_put(THIS_MODULE);
 
-       /*up(&zr->resource_lock);*/
+       /*mutex_unlock(&zr->resource_lock);*/
 
        dprintk(4, KERN_INFO "%s: zoran_close() done\n", ZR_DEVNAME(zr));
 
@@ -1754,7 +1761,7 @@ setup_overlay (struct file *file,
        return wait_grab_pending(zr);
 }
 
-#ifdef HAVE_V4L2
+#ifdef CONFIG_VIDEO_V4L2
        /* get the status of a buffer in the clients buffer queue */
 static int
 zoran_v4l2_buffer_status (struct file        *file,
@@ -1864,7 +1871,7 @@ zoran_v4l2_buffer_status (struct file        *file,
 
 static int
 zoran_set_norm (struct zoran *zr,
-               int           norm) /* VIDEO_MODE_* */
+               int           norm) /* VIDEO_MODE_* */
 {
        int norm_encoder, on;
 
@@ -1917,8 +1924,7 @@ zoran_set_norm (struct zoran *zr,
                decoder_command(zr, DECODER_SET_NORM, &norm);
 
                /* let changes come into effect */
-               current->state = TASK_UNINTERRUPTIBLE;
-               schedule_timeout(2 * HZ);
+               ssleep(2);
 
                decoder_command(zr, DECODER_GET_STATUS, &status);
                if (!(status & DECODER_STATUS_GOOD)) {
@@ -2000,12 +2006,14 @@ zoran_set_input (struct zoran *zr,
 
 static int
 zoran_do_ioctl (struct inode *inode,
-               struct file  *file,
-               unsigned int  cmd,
-               void         *arg)
+               struct file  *file,
+               unsigned int  cmd,
+               void         *arg)
 {
        struct zoran_fh *fh = file->private_data;
        struct zoran *zr = fh->zr;
+       /* CAREFUL: used in multiple places here */
+       struct zoran_jpg_settings settings;
 
        /* we might have older buffers lying around... We don't want
         * to wait, but we do want to try cleaning them up ASAP. So
@@ -2020,14 +2028,14 @@ zoran_do_ioctl (struct inode *inode,
         * but moving the free code outside the munmap() handler fixes
         * all this... If someone knows why, please explain me (Ronald)
         */
-       if (!down_trylock(&zr->resource_lock)) {
+       if (!!mutex_trylock(&zr->resource_lock)) {
                /* we obtained it! Let's try to free some things */
                if (fh->jpg_buffers.ready_to_be_freed)
                        jpg_fbuffer_free(file);
                if (fh->v4l_buffers.ready_to_be_freed)
                        v4l_fbuffer_free(file);
 
-               up(&zr->resource_lock);
+               mutex_unlock(&zr->resource_lock);
        }
 
        switch (cmd) {
@@ -2039,17 +2047,17 @@ zoran_do_ioctl (struct inode *inode,
                dprintk(3, KERN_DEBUG "%s: VIDIOCGCAP\n", ZR_DEVNAME(zr));
 
                memset(vcap, 0, sizeof(struct video_capability));
-               strncpy(vcap->name, ZR_DEVNAME(zr), sizeof(vcap->name));
+               strncpy(vcap->name, ZR_DEVNAME(zr), sizeof(vcap->name)-1);
                vcap->type = ZORAN_VID_TYPE;
 
                vcap->channels = zr->card.inputs;
                vcap->audios = 0;
-               down(&zr->resource_lock);
+               mutex_lock(&zr->resource_lock);
                vcap->maxwidth = BUZ_MAX_WIDTH;
                vcap->maxheight = BUZ_MAX_HEIGHT;
                vcap->minwidth = BUZ_MIN_WIDTH;
                vcap->minheight = BUZ_MIN_HEIGHT;
-               up(&zr->resource_lock);
+               mutex_unlock(&zr->resource_lock);
 
                return 0;
        }
@@ -2077,9 +2085,9 @@ zoran_do_ioctl (struct inode *inode,
                vchan->tuners = 0;
                vchan->flags = 0;
                vchan->type = VIDEO_TYPE_CAMERA;
-               down(&zr->resource_lock);
+               mutex_lock(&zr->resource_lock);
                vchan->norm = zr->norm;
-               up(&zr->resource_lock);
+               mutex_unlock(&zr->resource_lock);
                vchan->channel = channel;
 
                return 0;
@@ -2087,7 +2095,7 @@ zoran_do_ioctl (struct inode *inode,
                break;
 
                /* RJ: the documentation at http://roadrunner.swansea.linux.org.uk/v4lapi.shtml says:
-                * 
+                *
                 * * "The VIDIOCSCHAN ioctl takes an integer argument and switches the capture to this input."
                 * *                                 ^^^^^^^
                 * * The famos BTTV driver has it implemented with a struct video_channel argument
@@ -2106,7 +2114,7 @@ zoran_do_ioctl (struct inode *inode,
                        "%s: VIDIOCSCHAN - channel=%d, norm=%d\n",
                        ZR_DEVNAME(zr), vchan->channel, vchan->norm);
 
-               down(&zr->resource_lock);
+               mutex_lock(&zr->resource_lock);
                if ((res = zoran_set_input(zr, vchan->channel)))
                        goto schan_unlock_and_return;
                if ((res = zoran_set_norm(zr, vchan->norm)))
@@ -2115,7 +2123,7 @@ zoran_do_ioctl (struct inode *inode,
                /* Make sure the changes come into effect */
                res = wait_grab_pending(zr);
        schan_unlock_and_return:
-               up(&zr->resource_lock);
+               mutex_unlock(&zr->resource_lock);
                return res;
        }
                break;
@@ -2127,7 +2135,7 @@ zoran_do_ioctl (struct inode *inode,
                dprintk(3, KERN_DEBUG "%s: VIDIOCGPICT\n", ZR_DEVNAME(zr));
 
                memset(vpict, 0, sizeof(struct video_picture));
-               down(&zr->resource_lock);
+               mutex_lock(&zr->resource_lock);
                vpict->hue = zr->hue;
                vpict->brightness = zr->brightness;
                vpict->contrast = zr->contrast;
@@ -2138,7 +2146,7 @@ zoran_do_ioctl (struct inode *inode,
                } else {
                        vpict->depth = 0;
                }
-               up(&zr->resource_lock);
+               mutex_unlock(&zr->resource_lock);
 
                return 0;
        }
@@ -2173,7 +2181,7 @@ zoran_do_ioctl (struct inode *inode,
                        return -EINVAL;
                }
 
-               down(&zr->resource_lock);
+               mutex_lock(&zr->resource_lock);
 
                decoder_command(zr, DECODER_SET_PICTURE, vpict);
 
@@ -2184,7 +2192,7 @@ zoran_do_ioctl (struct inode *inode,
 
                fh->overlay_settings.format = &zoran_formats[i];
 
-               up(&zr->resource_lock);
+               mutex_unlock(&zr->resource_lock);
 
                return 0;
        }
@@ -2197,9 +2205,9 @@ zoran_do_ioctl (struct inode *inode,
                dprintk(3, KERN_DEBUG "%s: VIDIOCCAPTURE - on=%d\n",
                        ZR_DEVNAME(zr), *on);
 
-               down(&zr->resource_lock);
+               mutex_lock(&zr->resource_lock);
                res = setup_overlay(file, *on);
-               up(&zr->resource_lock);
+               mutex_unlock(&zr->resource_lock);
 
                return res;
        }
@@ -2212,12 +2220,12 @@ zoran_do_ioctl (struct inode *inode,
                dprintk(3, KERN_DEBUG "%s: VIDIOCGWIN\n", ZR_DEVNAME(zr));
 
                memset(vwin, 0, sizeof(struct video_window));
-               down(&zr->resource_lock);
+               mutex_lock(&zr->resource_lock);
                vwin->x = fh->overlay_settings.x;
                vwin->y = fh->overlay_settings.y;
                vwin->width = fh->overlay_settings.width;
                vwin->height = fh->overlay_settings.height;
-               up(&zr->resource_lock);
+               mutex_unlock(&zr->resource_lock);
                vwin->clipcount = 0;
                return 0;
        }
@@ -2234,12 +2242,12 @@ zoran_do_ioctl (struct inode *inode,
                        ZR_DEVNAME(zr), vwin->x, vwin->y, vwin->width,
                        vwin->height, vwin->clipcount);
 
-               down(&zr->resource_lock);
+               mutex_lock(&zr->resource_lock);
                res =
                    setup_window(file, vwin->x, vwin->y, vwin->width,
                                 vwin->height, vwin->clips,
                                 vwin->clipcount, NULL);
-               up(&zr->resource_lock);
+               mutex_unlock(&zr->resource_lock);
 
                return res;
        }
@@ -2251,9 +2259,9 @@ zoran_do_ioctl (struct inode *inode,
 
                dprintk(3, KERN_DEBUG "%s: VIDIOCGFBUF\n", ZR_DEVNAME(zr));
 
-               down(&zr->resource_lock);
+               mutex_lock(&zr->resource_lock);
                *vbuf = zr->buffer;
-               up(&zr->resource_lock);
+               mutex_unlock(&zr->resource_lock);
                return 0;
        }
                break;
@@ -2280,12 +2288,12 @@ zoran_do_ioctl (struct inode *inode,
                        return -EINVAL;
                }
 
-               down(&zr->resource_lock);
+               mutex_lock(&zr->resource_lock);
                res =
                    setup_fbuffer(file, vbuf->base, &zoran_formats[i],
                                  vbuf->width, vbuf->height,
                                  vbuf->bytesperline);
-               up(&zr->resource_lock);
+               mutex_unlock(&zr->resource_lock);
 
                return res;
        }
@@ -2298,9 +2306,9 @@ zoran_do_ioctl (struct inode *inode,
                dprintk(3, KERN_DEBUG "%s: VIDIOCSYNC - frame=%d\n",
                        ZR_DEVNAME(zr), *frame);
 
-               down(&zr->resource_lock);
+               mutex_lock(&zr->resource_lock);
                res = v4l_sync(file, *frame);
-               up(&zr->resource_lock);
+               mutex_unlock(&zr->resource_lock);
                if (!res)
                        zr->v4l_sync_tail++;
                return res;
@@ -2318,9 +2326,9 @@ zoran_do_ioctl (struct inode *inode,
                        ZR_DEVNAME(zr), vmap->frame, vmap->width, vmap->height,
                        vmap->format);
 
-               down(&zr->resource_lock);
+               mutex_lock(&zr->resource_lock);
                res = v4l_grab(file, vmap);
-               up(&zr->resource_lock);
+               mutex_unlock(&zr->resource_lock);
                return res;
        }
                break;
@@ -2341,7 +2349,7 @@ zoran_do_ioctl (struct inode *inode,
                            i * fh->v4l_buffers.buffer_size;
                }
 
-               down(&zr->resource_lock);
+               mutex_lock(&zr->resource_lock);
 
                if (fh->jpg_buffers.allocated || fh->v4l_buffers.allocated) {
                        dprintk(1,
@@ -2360,7 +2368,7 @@ zoran_do_ioctl (struct inode *inode,
                /* The next mmap will map the V4L buffers */
                fh->map_mode = ZORAN_MAP_MODE_RAW;
        v4l1reqbuf_unlock_and_return:
-               up(&zr->resource_lock);
+               mutex_unlock(&zr->resource_lock);
 
                return res;
        }
@@ -2414,7 +2422,7 @@ zoran_do_ioctl (struct inode *inode,
                bparams->major_version = MAJOR_VERSION;
                bparams->minor_version = MINOR_VERSION;
 
-               down(&zr->resource_lock);
+               mutex_lock(&zr->resource_lock);
 
                bparams->norm = zr->norm;
                bparams->input = zr->input;
@@ -2443,7 +2451,7 @@ zoran_do_ioctl (struct inode *inode,
                bparams->jpeg_markers =
                    fh->jpg_settings.jpg_comp.jpeg_markers;
 
-               up(&zr->resource_lock);
+               mutex_unlock(&zr->resource_lock);
 
                bparams->VFIFO_FB = 0;
 
@@ -2454,7 +2462,6 @@ zoran_do_ioctl (struct inode *inode,
        case BUZIOC_S_PARAMS:
        {
                struct zoran_params *bparams = arg;
-               struct zoran_jpg_settings settings;
                int res = 0;
 
                dprintk(3, KERN_DEBUG "%s: BUZIOC_S_PARAMS\n", ZR_DEVNAME(zr));
@@ -2480,7 +2487,7 @@ zoran_do_ioctl (struct inode *inode,
                       sizeof(bparams->COM_data));
                settings.jpg_comp.jpeg_markers = bparams->jpeg_markers;
 
-               down(&zr->resource_lock);
+               mutex_lock(&zr->resource_lock);
 
                if (zr->codec_mode != BUZ_MODE_IDLE) {
                        dprintk(1,
@@ -2500,7 +2507,7 @@ zoran_do_ioctl (struct inode *inode,
 
                fh->jpg_settings = settings;
        sparams_unlock_and_return:
-               up(&zr->resource_lock);
+               mutex_unlock(&zr->resource_lock);
 
                return res;
        }
@@ -2532,7 +2539,7 @@ zoran_do_ioctl (struct inode *inode,
                    breq->size > MAX_KMALLOC_MEM)
                        breq->size = MAX_KMALLOC_MEM;
 
-               down(&zr->resource_lock);
+               mutex_lock(&zr->resource_lock);
 
                if (fh->jpg_buffers.allocated || fh->v4l_buffers.allocated) {
                        dprintk(1,
@@ -2555,7 +2562,7 @@ zoran_do_ioctl (struct inode *inode,
                 * also be *_PLAY, but it doesn't matter here */
                fh->map_mode = ZORAN_MAP_MODE_JPG_REC;
        jpgreqbuf_unlock_and_return:
-               up(&zr->resource_lock);
+               mutex_unlock(&zr->resource_lock);
 
                return res;
        }
@@ -2568,9 +2575,9 @@ zoran_do_ioctl (struct inode *inode,
                dprintk(3, KERN_DEBUG "%s: BUZIOC_QBUF_CAPT - frame=%d\n",
                        ZR_DEVNAME(zr), *frame);
 
-               down(&zr->resource_lock);
+               mutex_lock(&zr->resource_lock);
                res = jpg_qbuf(file, *frame, BUZ_MODE_MOTION_COMPRESS);
-               up(&zr->resource_lock);
+               mutex_unlock(&zr->resource_lock);
 
                return res;
        }
@@ -2583,9 +2590,9 @@ zoran_do_ioctl (struct inode *inode,
                dprintk(3, KERN_DEBUG "%s: BUZIOC_QBUF_PLAY - frame=%d\n",
                        ZR_DEVNAME(zr), *frame);
 
-               down(&zr->resource_lock);
+               mutex_lock(&zr->resource_lock);
                res = jpg_qbuf(file, *frame, BUZ_MODE_MOTION_DECOMPRESS);
-               up(&zr->resource_lock);
+               mutex_unlock(&zr->resource_lock);
 
                return res;
        }
@@ -2598,9 +2605,9 @@ zoran_do_ioctl (struct inode *inode,
 
                dprintk(3, KERN_DEBUG "%s: BUZIOC_SYNC\n", ZR_DEVNAME(zr));
 
-               down(&zr->resource_lock);
+               mutex_lock(&zr->resource_lock);
                res = jpg_sync(file, bsync);
-               up(&zr->resource_lock);
+               mutex_unlock(&zr->resource_lock);
 
                return res;
        }
@@ -2624,7 +2631,7 @@ zoran_do_ioctl (struct inode *inode,
                input = zr->card.input[bstat->input].muxsel;
                norm = VIDEO_MODE_AUTO;
 
-               down(&zr->resource_lock);
+               mutex_lock(&zr->resource_lock);
 
                if (zr->codec_mode != BUZ_MODE_IDLE) {
                        dprintk(1,
@@ -2639,8 +2646,7 @@ zoran_do_ioctl (struct inode *inode,
                decoder_command(zr, DECODER_SET_NORM, &norm);
 
                /* sleep 1 second */
-               current->state = TASK_UNINTERRUPTIBLE;
-               schedule_timeout(1 * HZ);
+               ssleep(1);
 
                /* Get status of video decoder */
                decoder_command(zr, DECODER_GET_STATUS, &status);
@@ -2650,7 +2656,7 @@ zoran_do_ioctl (struct inode *inode,
                decoder_command(zr, DECODER_SET_INPUT, &input);
                decoder_command(zr, DECODER_SET_NORM, &zr->norm);
        gstat_unlock_and_return:
-               up(&zr->resource_lock);
+               mutex_unlock(&zr->resource_lock);
 
                if (!res) {
                        bstat->signal =
@@ -2670,7 +2676,7 @@ zoran_do_ioctl (struct inode *inode,
        }
                break;
 
-#ifdef HAVE_V4L2
+#ifdef CONFIG_VIDEO_V4L2
 
                /* The new video4linux2 capture interface - much nicer than video4linux1, since
                 * it allows for integrating the JPEG capturing calls inside standard v4l2
@@ -2683,10 +2689,10 @@ zoran_do_ioctl (struct inode *inode,
                dprintk(3, KERN_DEBUG "%s: VIDIOC_QUERYCAP\n", ZR_DEVNAME(zr));
 
                memset(cap, 0, sizeof(*cap));
-               strncpy(cap->card, ZR_DEVNAME(zr), sizeof(cap->card));
-               strncpy(cap->driver, "zoran", sizeof(cap->driver));
+               strncpy(cap->card, ZR_DEVNAME(zr), sizeof(cap->card)-1);
+               strncpy(cap->driver, "zoran", sizeof(cap->driver)-1);
                snprintf(cap->bus_info, sizeof(cap->bus_info), "PCI:%s",
-                        zr->pci_dev->slot_name);
+                        pci_name(zr->pci_dev));
                cap->version =
                    KERNEL_VERSION(MAJOR_VERSION, MINOR_VERSION,
                                   RELEASE_VERSION);
@@ -2736,7 +2742,7 @@ zoran_do_ioctl (struct inode *inode,
                memset(fmt, 0, sizeof(*fmt));
                fmt->index = index;
                fmt->type = type;
-               strncpy(fmt->description, zoran_formats[i].name, 31);
+               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;
@@ -2758,7 +2764,7 @@ zoran_do_ioctl (struct inode *inode,
                switch (fmt->type) {
                case V4L2_BUF_TYPE_VIDEO_OVERLAY:
 
-                       down(&zr->resource_lock);
+                       mutex_lock(&zr->resource_lock);
 
                        fmt->fmt.win.w.left = fh->overlay_settings.x;
                        fmt->fmt.win.w.top = fh->overlay_settings.y;
@@ -2771,14 +2777,14 @@ zoran_do_ioctl (struct inode *inode,
                        else
                                fmt->fmt.win.field = V4L2_FIELD_TOP;
 
-                       up(&zr->resource_lock);
+                       mutex_unlock(&zr->resource_lock);
 
                        break;
 
                case V4L2_BUF_TYPE_VIDEO_CAPTURE:
                case V4L2_BUF_TYPE_VIDEO_OUTPUT:
 
-                       down(&zr->resource_lock);
+                       mutex_lock(&zr->resource_lock);
 
                        if (fmt->type == V4L2_BUF_TYPE_VIDEO_CAPTURE &&
                            fh->map_mode == ZORAN_MAP_MODE_RAW) {
@@ -2832,7 +2838,7 @@ zoran_do_ioctl (struct inode *inode,
                                    V4L2_COLORSPACE_SMPTE170M;
                        }
 
-                       up(&zr->resource_lock);
+                       mutex_unlock(&zr->resource_lock);
 
                        break;
 
@@ -2865,7 +2871,7 @@ zoran_do_ioctl (struct inode *inode,
                                fmt->fmt.win.w.height,
                                fmt->fmt.win.clipcount,
                                fmt->fmt.win.bitmap);
-                       down(&zr->resource_lock);
+                       mutex_lock(&zr->resource_lock);
                        res =
                            setup_window(file, fmt->fmt.win.w.left,
                                         fmt->fmt.win.w.top,
@@ -2875,7 +2881,7 @@ zoran_do_ioctl (struct inode *inode,
                                           fmt->fmt.win.clips,
                                         fmt->fmt.win.clipcount,
                                         fmt->fmt.win.bitmap);
-                       up(&zr->resource_lock);
+                       mutex_unlock(&zr->resource_lock);
                        return res;
                        break;
 
@@ -2912,9 +2918,7 @@ zoran_do_ioctl (struct inode *inode,
                        }
 
                        if (fmt->fmt.pix.pixelformat == V4L2_PIX_FMT_MJPEG) {
-                               struct zoran_jpg_settings settings;
-
-                               down(&zr->resource_lock);
+                               mutex_lock(&zr->resource_lock);
 
                                settings = fh->jpg_settings;
 
@@ -2992,7 +2996,7 @@ zoran_do_ioctl (struct inode *inode,
                                    ZORAN_MAP_MODE_JPG_REC :
                                    ZORAN_MAP_MODE_JPG_PLAY;
                        sfmtjpg_unlock_and_return:
-                               up(&zr->resource_lock);
+                               mutex_unlock(&zr->resource_lock);
                        } else {
                                for (i = 0; i < zoran_num_formats; i++)
                                        if (fmt->fmt.pix.pixelformat ==
@@ -3007,7 +3011,7 @@ zoran_do_ioctl (struct inode *inode,
                                                (char *) &printformat);
                                        return -EINVAL;
                                }
-                               down(&zr->resource_lock);
+                               mutex_lock(&zr->resource_lock);
                                if (fh->jpg_buffers.allocated ||
                                    (fh->v4l_buffers.allocated &&
                                     fh->v4l_buffers.active !=
@@ -3049,7 +3053,7 @@ zoran_do_ioctl (struct inode *inode,
 
                                fh->map_mode = ZORAN_MAP_MODE_RAW;
                        sfmtv4l_unlock_and_return:
-                               up(&zr->resource_lock);
+                               mutex_unlock(&zr->resource_lock);
                        }
 
                        break;
@@ -3074,7 +3078,7 @@ zoran_do_ioctl (struct inode *inode,
                dprintk(3, KERN_DEBUG "%s: VIDIOC_G_FBUF\n", ZR_DEVNAME(zr));
 
                memset(fb, 0, sizeof(*fb));
-               down(&zr->resource_lock);
+               mutex_lock(&zr->resource_lock);
                fb->base = zr->buffer.base;
                fb->fmt.width = zr->buffer.width;
                fb->fmt.height = zr->buffer.height;
@@ -3083,7 +3087,7 @@ zoran_do_ioctl (struct inode *inode,
                                fh->overlay_settings.format->fourcc;
                }
                fb->fmt.bytesperline = zr->buffer.bytesperline;
-               up(&zr->resource_lock);
+               mutex_unlock(&zr->resource_lock);
                fb->fmt.colorspace = V4L2_COLORSPACE_SRGB;
                fb->fmt.field = V4L2_FIELD_INTERLACED;
                fb->flags = V4L2_FBUF_FLAG_OVERLAY;
@@ -3118,12 +3122,12 @@ zoran_do_ioctl (struct inode *inode,
                        return -EINVAL;
                }
 
-               down(&zr->resource_lock);
+               mutex_lock(&zr->resource_lock);
                res =
                    setup_fbuffer(file, fb->base, &zoran_formats[i],
                                  fb->fmt.width, fb->fmt.height,
                                  fb->fmt.bytesperline);
-               up(&zr->resource_lock);
+               mutex_unlock(&zr->resource_lock);
 
                return res;
        }
@@ -3136,9 +3140,9 @@ zoran_do_ioctl (struct inode *inode,
                dprintk(3, KERN_DEBUG "%s: VIDIOC_PREVIEW - on=%d\n",
                        ZR_DEVNAME(zr), *on);
 
-               down(&zr->resource_lock);
+               mutex_lock(&zr->resource_lock);
                res = setup_overlay(file, *on);
-               up(&zr->resource_lock);
+               mutex_unlock(&zr->resource_lock);
 
                return res;
        }
@@ -3160,7 +3164,7 @@ zoran_do_ioctl (struct inode *inode,
                        return -EINVAL;
                }
 
-               down(&zr->resource_lock);
+               mutex_lock(&zr->resource_lock);
 
                if (fh->v4l_buffers.allocated || fh->jpg_buffers.allocated) {
                        dprintk(1,
@@ -3221,7 +3225,7 @@ zoran_do_ioctl (struct inode *inode,
                        goto v4l2reqbuf_unlock_and_return;
                }
        v4l2reqbuf_unlock_and_return:
-               up(&zr->resource_lock);
+               mutex_unlock(&zr->resource_lock);
 
                return 0;
        }
@@ -3242,9 +3246,9 @@ zoran_do_ioctl (struct inode *inode,
                buf->type = type;
                buf->index = index;
 
-               down(&zr->resource_lock);
+               mutex_lock(&zr->resource_lock);
                res = zoran_v4l2_buffer_status(file, buf, buf->index);
-               up(&zr->resource_lock);
+               mutex_unlock(&zr->resource_lock);
 
                return res;
        }
@@ -3259,7 +3263,7 @@ zoran_do_ioctl (struct inode *inode,
                        KERN_DEBUG "%s: VIDIOC_QBUF - type=%d, index=%d\n",
                        ZR_DEVNAME(zr), buf->type, buf->index);
 
-               down(&zr->resource_lock);
+               mutex_lock(&zr->resource_lock);
 
                switch (fh->map_mode) {
                case ZORAN_MAP_MODE_RAW:
@@ -3319,7 +3323,7 @@ zoran_do_ioctl (struct inode *inode,
                        goto qbuf_unlock_and_return;
                }
        qbuf_unlock_and_return:
-               up(&zr->resource_lock);
+               mutex_unlock(&zr->resource_lock);
 
                return res;
        }
@@ -3333,7 +3337,7 @@ zoran_do_ioctl (struct inode *inode,
                dprintk(3, KERN_DEBUG "%s: VIDIOC_DQBUF - type=%d\n",
                        ZR_DEVNAME(zr), buf->type);
 
-               down(&zr->resource_lock);
+               mutex_lock(&zr->resource_lock);
 
                switch (fh->map_mode) {
                case ZORAN_MAP_MODE_RAW:
@@ -3407,7 +3411,7 @@ zoran_do_ioctl (struct inode *inode,
                        goto dqbuf_unlock_and_return;
                }
        dqbuf_unlock_and_return:
-               up(&zr->resource_lock);
+               mutex_unlock(&zr->resource_lock);
 
                return res;
        }
@@ -3419,7 +3423,7 @@ zoran_do_ioctl (struct inode *inode,
 
                dprintk(3, KERN_DEBUG "%s: VIDIOC_STREAMON\n", ZR_DEVNAME(zr));
 
-               down(&zr->resource_lock);
+               mutex_lock(&zr->resource_lock);
 
                switch (fh->map_mode) {
                case ZORAN_MAP_MODE_RAW:        /* raw capture */
@@ -3467,7 +3471,7 @@ zoran_do_ioctl (struct inode *inode,
                        goto strmon_unlock_and_return;
                }
        strmon_unlock_and_return:
-               up(&zr->resource_lock);
+               mutex_unlock(&zr->resource_lock);
 
                return res;
        }
@@ -3479,7 +3483,7 @@ zoran_do_ioctl (struct inode *inode,
 
                dprintk(3, KERN_DEBUG "%s: VIDIOC_STREAMOFF\n", ZR_DEVNAME(zr));
 
-               down(&zr->resource_lock);
+               mutex_lock(&zr->resource_lock);
 
                switch (fh->map_mode) {
                case ZORAN_MAP_MODE_RAW:        /* raw capture */
@@ -3537,7 +3541,7 @@ zoran_do_ioctl (struct inode *inode,
                        goto strmoff_unlock_and_return;
                }
        strmoff_unlock_and_return:
-               up(&zr->resource_lock);
+               mutex_unlock(&zr->resource_lock);
 
                return res;
        }
@@ -3562,16 +3566,16 @@ zoran_do_ioctl (struct inode *inode,
 
                switch (ctrl->id) {
                case V4L2_CID_BRIGHTNESS:
-                       strncpy(ctrl->name, "Brightness", 31);
+                       strncpy(ctrl->name, "Brightness", sizeof(ctrl->name)-1);
                        break;
                case V4L2_CID_CONTRAST:
-                       strncpy(ctrl->name, "Contrast", 31);
+                       strncpy(ctrl->name, "Contrast", sizeof(ctrl->name)-1);
                        break;
                case V4L2_CID_SATURATION:
-                       strncpy(ctrl->name, "Saturation", 31);
+                       strncpy(ctrl->name, "Saturation", sizeof(ctrl->name)-1);
                        break;
                case V4L2_CID_HUE:
-                       strncpy(ctrl->name, "Hue", 31);
+                       strncpy(ctrl->name, "Hue", sizeof(ctrl->name)-1);
                        break;
                }
 
@@ -3597,7 +3601,7 @@ zoran_do_ioctl (struct inode *inode,
                    ctrl->id > V4L2_CID_HUE)
                        return -EINVAL;
 
-               down(&zr->resource_lock);
+               mutex_lock(&zr->resource_lock);
                switch (ctrl->id) {
                case V4L2_CID_BRIGHTNESS:
                        ctrl->value = zr->brightness;
@@ -3612,7 +3616,7 @@ zoran_do_ioctl (struct inode *inode,
                        ctrl->value = zr->hue;
                        break;
                }
-               up(&zr->resource_lock);
+               mutex_unlock(&zr->resource_lock);
 
                return 0;
        }
@@ -3639,7 +3643,7 @@ zoran_do_ioctl (struct inode *inode,
                        return -EINVAL;
                }
 
-               down(&zr->resource_lock);
+               mutex_lock(&zr->resource_lock);
                switch (ctrl->id) {
                case V4L2_CID_BRIGHTNESS:
                        zr->brightness = ctrl->value;
@@ -3661,7 +3665,7 @@ zoran_do_ioctl (struct inode *inode,
 
                decoder_command(zr, DECODER_SET_PICTURE, &pict);
 
-               up(&zr->resource_lock);
+               mutex_unlock(&zr->resource_lock);
 
                return 0;
        }
@@ -3689,7 +3693,7 @@ zoran_do_ioctl (struct inode *inode,
                                        &caps);
                        if (caps.flags & VIDEO_DECODER_AUTO) {
                                std->id = V4L2_STD_ALL;
-                               strncpy(std->name, "Autodetect", 31);
+                               strncpy(std->name, "Autodetect", sizeof(std->name)-1);
                                return 0;
                        } else
                                return -EINVAL;
@@ -3697,21 +3701,21 @@ zoran_do_ioctl (struct inode *inode,
                switch (std->index) {
                case 0:
                        std->id = V4L2_STD_PAL;
-                       strncpy(std->name, "PAL", 31);
+                       strncpy(std->name, "PAL", sizeof(std->name)-1);
                        std->frameperiod.numerator = 1;
                        std->frameperiod.denominator = 25;
                        std->framelines = zr->card.tvn[0]->Ht;
                        break;
                case 1:
                        std->id = V4L2_STD_NTSC;
-                       strncpy(std->name, "NTSC", 31);
+                       strncpy(std->name, "NTSC", sizeof(std->name)-1);
                        std->frameperiod.numerator = 1001;
                        std->frameperiod.denominator = 30000;
                        std->framelines = zr->card.tvn[1]->Ht;
                        break;
                case 2:
                        std->id = V4L2_STD_SECAM;
-                       strncpy(std->name, "SECAM", 31);
+                       strncpy(std->name, "SECAM", sizeof(std->name)-1);
                        std->frameperiod.numerator = 1;
                        std->frameperiod.denominator = 25;
                        std->framelines = zr->card.tvn[2]->Ht;
@@ -3729,9 +3733,9 @@ zoran_do_ioctl (struct inode *inode,
 
                dprintk(3, KERN_DEBUG "%s: VIDIOC_G_STD\n", ZR_DEVNAME(zr));
 
-               down(&zr->resource_lock);
+               mutex_lock(&zr->resource_lock);
                norm = zr->norm;
-               up(&zr->resource_lock);
+               mutex_unlock(&zr->resource_lock);
 
                switch (norm) {
                case VIDEO_MODE_PAL:
@@ -3755,7 +3759,7 @@ zoran_do_ioctl (struct inode *inode,
                v4l2_std_id *std = arg;
 
                dprintk(3, KERN_DEBUG "%s: VIDIOC_S_STD - norm=0x%llx\n",
-                       ZR_DEVNAME(zr), *std);
+                       ZR_DEVNAME(zr), (unsigned long long)*std);
 
                if (*std == V4L2_STD_PAL)
                        norm = VIDEO_MODE_PAL;
@@ -3769,17 +3773,17 @@ zoran_do_ioctl (struct inode *inode,
                        dprintk(1,
                                KERN_ERR
                                "%s: VIDIOC_S_STD - invalid norm 0x%llx\n",
-                               ZR_DEVNAME(zr), *std);
+                               ZR_DEVNAME(zr), (unsigned long long)*std);
                        return -EINVAL;
                }
 
-               down(&zr->resource_lock);
+               mutex_lock(&zr->resource_lock);
                if ((res = zoran_set_norm(zr, norm)))
                        goto sstd_unlock_and_return;
 
                res = wait_grab_pending(zr);
        sstd_unlock_and_return:
-               up(&zr->resource_lock);
+               mutex_unlock(&zr->resource_lock);
                return res;
        }
                break;
@@ -3806,9 +3810,9 @@ zoran_do_ioctl (struct inode *inode,
                inp->std = V4L2_STD_ALL;
 
                /* Get status of video decoder */
-               down(&zr->resource_lock);
+               mutex_lock(&zr->resource_lock);
                decoder_command(zr, DECODER_GET_STATUS, &status);
-               up(&zr->resource_lock);
+               mutex_unlock(&zr->resource_lock);
 
                if (!(status & DECODER_STATUS_GOOD)) {
                        inp->status |= V4L2_IN_ST_NO_POWER;
@@ -3827,9 +3831,9 @@ zoran_do_ioctl (struct inode *inode,
 
                dprintk(3, KERN_DEBUG "%s: VIDIOC_G_INPUT\n", ZR_DEVNAME(zr));
 
-               down(&zr->resource_lock);
+               mutex_lock(&zr->resource_lock);
                *input = zr->input;
-               up(&zr->resource_lock);
+               mutex_unlock(&zr->resource_lock);
 
                return 0;
        }
@@ -3842,14 +3846,14 @@ zoran_do_ioctl (struct inode *inode,
                dprintk(3, KERN_DEBUG "%s: VIDIOC_S_INPUT - input=%d\n",
                        ZR_DEVNAME(zr), *input);
 
-               down(&zr->resource_lock);
+               mutex_lock(&zr->resource_lock);
                if ((res = zoran_set_input(zr, *input)))
                        goto sinput_unlock_and_return;
 
                /* Make sure the changes come into effect */
                res = wait_grab_pending(zr);
        sinput_unlock_and_return:
-               up(&zr->resource_lock);
+               mutex_unlock(&zr->resource_lock);
                return res;
        }
                break;
@@ -3867,7 +3871,7 @@ zoran_do_ioctl (struct inode *inode,
                memset(outp, 0, sizeof(*outp));
                outp->index = 0;
                outp->type = V4L2_OUTPUT_TYPE_ANALOGVGAOVERLAY;
-               strncpy(outp->name, "Autodetect", 31);
+               strncpy(outp->name, "Autodetect", sizeof(outp->name)-1);
 
                return 0;
        }
@@ -3911,7 +3915,7 @@ zoran_do_ioctl (struct inode *inode,
                memset(cropcap, 0, sizeof(*cropcap));
                cropcap->type = type;
 
-               down(&zr->resource_lock);
+               mutex_lock(&zr->resource_lock);
 
                if (cropcap->type != V4L2_BUF_TYPE_VIDEO_OUTPUT &&
                    (cropcap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
@@ -3931,7 +3935,7 @@ zoran_do_ioctl (struct inode *inode,
                cropcap->defrect.width = BUZ_MIN_WIDTH;
                cropcap->defrect.height = BUZ_MIN_HEIGHT;
        cropcap_unlock_and_return:
-               up(&zr->resource_lock);
+               mutex_unlock(&zr->resource_lock);
                return res;
        }
                break;
@@ -3947,7 +3951,7 @@ zoran_do_ioctl (struct inode *inode,
                memset(crop, 0, sizeof(*crop));
                crop->type = type;
 
-               down(&zr->resource_lock);
+               mutex_lock(&zr->resource_lock);
 
                if (crop->type != V4L2_BUF_TYPE_VIDEO_OUTPUT &&
                    (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
@@ -3966,7 +3970,7 @@ zoran_do_ioctl (struct inode *inode,
                crop->c.height = fh->jpg_settings.img_height;
 
        gcrop_unlock_and_return:
-               up(&zr->resource_lock);
+               mutex_unlock(&zr->resource_lock);
 
                return res;
        }
@@ -3976,7 +3980,8 @@ zoran_do_ioctl (struct inode *inode,
        {
                struct v4l2_crop *crop = arg;
                int res = 0;
-               struct zoran_jpg_settings settings = fh->jpg_settings;
+
+               settings = fh->jpg_settings;
 
                dprintk(3,
                        KERN_ERR
@@ -3984,7 +3989,7 @@ zoran_do_ioctl (struct inode *inode,
                        ZR_DEVNAME(zr), crop->type, crop->c.left, crop->c.top,
                        crop->c.width, crop->c.height);
 
-               down(&zr->resource_lock);
+               mutex_lock(&zr->resource_lock);
 
                if (fh->jpg_buffers.allocated || fh->v4l_buffers.allocated) {
                        dprintk(1,
@@ -4020,7 +4025,7 @@ zoran_do_ioctl (struct inode *inode,
                fh->jpg_settings = settings;
 
        scrop_unlock_and_return:
-               up(&zr->resource_lock);
+               mutex_unlock(&zr->resource_lock);
                return res;
        }
                break;
@@ -4034,7 +4039,7 @@ zoran_do_ioctl (struct inode *inode,
 
                memset(params, 0, sizeof(*params));
 
-               down(&zr->resource_lock);
+               mutex_lock(&zr->resource_lock);
 
                params->quality = fh->jpg_settings.jpg_comp.quality;
                params->APPn = fh->jpg_settings.jpg_comp.APPn;
@@ -4049,7 +4054,7 @@ zoran_do_ioctl (struct inode *inode,
                params->jpeg_markers =
                    fh->jpg_settings.jpg_comp.jpeg_markers;
 
-               up(&zr->resource_lock);
+               mutex_unlock(&zr->resource_lock);
 
                return 0;
        }
@@ -4058,9 +4063,10 @@ zoran_do_ioctl (struct inode *inode,
        case VIDIOC_S_JPEGCOMP:
        {
                struct v4l2_jpegcompression *params = arg;
-               struct zoran_jpg_settings settings = fh->jpg_settings;
                int res = 0;
 
+               settings = fh->jpg_settings;
+
                dprintk(3,
                        KERN_DEBUG
                        "%s: VIDIOC_S_JPEGCOMP - quality=%d, APPN=%d, APP_len=%d, COM_len=%d\n",
@@ -4069,7 +4075,7 @@ zoran_do_ioctl (struct inode *inode,
 
                settings.jpg_comp = *params;
 
-               down(&zr->resource_lock);
+               mutex_lock(&zr->resource_lock);
 
                if (fh->v4l_buffers.active != ZORAN_FREE ||
                    fh->jpg_buffers.active != ZORAN_FREE) {
@@ -4088,7 +4094,7 @@ zoran_do_ioctl (struct inode *inode,
                            zoran_v4l2_calc_bufsize(&fh->jpg_settings);
                fh->jpg_settings.jpg_comp = *params = settings.jpg_comp;
        sjpegc_unlock_and_return:
-               up(&zr->resource_lock);
+               mutex_unlock(&zr->resource_lock);
 
                return 0;
        }
@@ -4100,7 +4106,7 @@ zoran_do_ioctl (struct inode *inode,
 
                dprintk(3,
                        KERN_DEBUG "%s: VIDIOC_QUERY_STD - std=0x%llx\n",
-                       ZR_DEVNAME(zr), *std);
+                       ZR_DEVNAME(zr), (unsigned long long)*std);
 
                if (*std == V4L2_STD_ALL || *std == V4L2_STD_NTSC ||
                    *std == V4L2_STD_PAL || (*std == V4L2_STD_SECAM &&
@@ -4122,7 +4128,7 @@ zoran_do_ioctl (struct inode *inode,
 
                switch (fmt->type) {
                case V4L2_BUF_TYPE_VIDEO_OVERLAY:
-                       down(&zr->resource_lock);
+                       mutex_lock(&zr->resource_lock);
 
                        if (fmt->fmt.win.w.width > BUZ_MAX_WIDTH)
                                fmt->fmt.win.w.width = BUZ_MAX_WIDTH;
@@ -4133,7 +4139,7 @@ zoran_do_ioctl (struct inode *inode,
                        if (fmt->fmt.win.w.height < BUZ_MIN_HEIGHT)
                                fmt->fmt.win.w.height = BUZ_MIN_HEIGHT;
 
-                       up(&zr->resource_lock);
+                       mutex_unlock(&zr->resource_lock);
                        break;
 
                case V4L2_BUF_TYPE_VIDEO_CAPTURE:
@@ -4141,11 +4147,10 @@ zoran_do_ioctl (struct inode *inode,
                        if (fmt->fmt.pix.bytesperline > 0)
                                return -EINVAL;
 
-                       down(&zr->resource_lock);
+                       mutex_lock(&zr->resource_lock);
 
                        if (fmt->fmt.pix.pixelformat == V4L2_PIX_FMT_MJPEG) {
-                               struct zoran_jpg_settings settings =
-                                   fh->jpg_settings;
+                               settings = fh->jpg_settings;
 
                                /* we actually need to set 'real' parameters now */
                                if ((fmt->fmt.pix.height * 2) >
@@ -4225,7 +4230,7 @@ zoran_do_ioctl (struct inode *inode,
                                goto tryfmt_unlock_and_return;
                        }
                tryfmt_unlock_and_return:
-                       up(&zr->resource_lock);
+                       mutex_unlock(&zr->resource_lock);
 
                        return res;
                        break;
@@ -4276,7 +4281,7 @@ zoran_poll (struct file *file,
         * if no buffers queued or so, return POLLNVAL
         */
 
-       down(&zr->resource_lock);
+       mutex_lock(&zr->resource_lock);
 
        switch (fh->map_mode) {
        case ZORAN_MAP_MODE_RAW:
@@ -4325,7 +4330,7 @@ zoran_poll (struct file *file,
        }
 
 poll_unlock_and_return:
-       up(&zr->resource_lock);
+       mutex_unlock(&zr->resource_lock);
 
        return res;
 }
@@ -4381,7 +4386,7 @@ zoran_vm_close (struct vm_area_struct *vma)
                                if (fh->jpg_buffers.buffer[i].map)
                                        break;
                        if (i == fh->jpg_buffers.num_buffers) {
-                               down(&zr->resource_lock);
+                               mutex_lock(&zr->resource_lock);
 
                                if (fh->jpg_buffers.active != ZORAN_FREE) {
                                        jpg_qbuf(file, -1, zr->codec_mode);
@@ -4394,7 +4399,7 @@ zoran_vm_close (struct vm_area_struct *vma)
                                fh->jpg_buffers.allocated = 0;
                                fh->jpg_buffers.ready_to_be_freed = 1;
 
-                               up(&zr->resource_lock);
+                               mutex_unlock(&zr->resource_lock);
                        }
 
                        break;
@@ -4417,7 +4422,7 @@ zoran_vm_close (struct vm_area_struct *vma)
                                if (fh->v4l_buffers.buffer[i].map)
                                        break;
                        if (i == fh->v4l_buffers.num_buffers) {
-                               down(&zr->resource_lock);
+                               mutex_lock(&zr->resource_lock);
 
                                if (fh->v4l_buffers.active != ZORAN_FREE) {
                                        zr36057_set_memgrab(zr, 0);
@@ -4430,7 +4435,7 @@ zoran_vm_close (struct vm_area_struct *vma)
                                fh->v4l_buffers.allocated = 0;
                                fh->v4l_buffers.ready_to_be_freed = 1;
 
-                               up(&zr->resource_lock);
+                               mutex_unlock(&zr->resource_lock);
                        }
 
                        break;
@@ -4450,12 +4455,6 @@ static struct vm_operations_struct zoran_vm_ops = {
        .close = zoran_vm_close,
 };
 
-#if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
-#define zr_remap_page_range(a,b,c,d,e) remap_page_range(b,c,d,e)
-#else
-#define zr_remap_page_range(a,b,c,d,e) remap_page_range(a,b,c,d,e)
-#endif
-
 static int
 zoran_mmap (struct file           *file,
            struct vm_area_struct *vma)
@@ -4491,7 +4490,7 @@ zoran_mmap (struct file           *file,
        case ZORAN_MAP_MODE_JPG_PLAY:
 
                /* lock */
-               down(&zr->resource_lock);
+               mutex_lock(&zr->resource_lock);
 
                /* Map the MJPEG buffers */
                if (!fh->jpg_buffers.allocated) {
@@ -4547,20 +4546,22 @@ zoran_mmap (struct file           *file,
                             j < fh->jpg_buffers.buffer_size / PAGE_SIZE;
                             j++) {
                                fraglen =
-                                   (fh->jpg_buffers.buffer[i].
-                                    frag_tab[2 * j + 1] & ~1) << 1;
+                                   (le32_to_cpu(fh->jpg_buffers.buffer[i].
+                                    frag_tab[2 * j + 1]) & ~1) << 1;
                                todo = size;
                                if (todo > fraglen)
                                        todo = fraglen;
                                pos =
-                                   (unsigned long) fh->jpg_buffers.
-                                   buffer[i].frag_tab[2 * j];
-                               page = virt_to_phys(bus_to_virt(pos));  /* should just be pos on i386 */
-                               if (zr_remap_page_range
-                                   (vma, start, page, todo, PAGE_SHARED)) {
+                                   le32_to_cpu((unsigned long) fh->jpg_buffers.
+                                   buffer[i].frag_tab[2 * j]);
+                               /* should just be pos on i386 */
+                               page = virt_to_phys(bus_to_virt(pos))
+                                                               >> PAGE_SHIFT;
+                               if (remap_pfn_range(vma, start, page,
+                                                       todo, PAGE_SHARED)) {
                                        dprintk(1,
                                                KERN_ERR
-                                               "%s: zoran_mmap(V4L) - remap_page_range failed\n",
+                                               "%s: zoran_mmap(V4L) - remap_pfn_range failed\n",
                                                ZR_DEVNAME(zr));
                                        res = -EAGAIN;
                                        goto jpg_mmap_unlock_and_return;
@@ -4569,8 +4570,8 @@ zoran_mmap (struct file           *file,
                                start += todo;
                                if (size == 0)
                                        break;
-                               if (fh->jpg_buffers.buffer[i].
-                                   frag_tab[2 * j + 1] & 1)
+                               if (le32_to_cpu(fh->jpg_buffers.buffer[i].
+                                   frag_tab[2 * j + 1]) & 1)
                                        break;  /* was last fragment */
                        }
                        fh->jpg_buffers.buffer[i].map = map;
@@ -4579,13 +4580,13 @@ zoran_mmap (struct file           *file,
 
                }
        jpg_mmap_unlock_and_return:
-               up(&zr->resource_lock);
+               mutex_unlock(&zr->resource_lock);
 
                break;
 
        case ZORAN_MAP_MODE_RAW:
 
-               down(&zr->resource_lock);
+               mutex_lock(&zr->resource_lock);
 
                /* Map the V4L buffers */
                if (!fh->v4l_buffers.allocated) {
@@ -4641,11 +4642,11 @@ zoran_mmap (struct file           *file,
                        if (todo > fh->v4l_buffers.buffer_size)
                                todo = fh->v4l_buffers.buffer_size;
                        page = fh->v4l_buffers.buffer[i].fbuffer_phys;
-                       if (zr_remap_page_range
-                           (vma, start, page, todo, PAGE_SHARED)) {
+                       if (remap_pfn_range(vma, start, page >> PAGE_SHIFT,
+                                                       todo, PAGE_SHARED)) {
                                dprintk(1,
                                        KERN_ERR
-                                       "%s: zoran_mmap(V4L)i - remap_page_range failed\n",
+                                       "%s: zoran_mmap(V4L)i - remap_pfn_range failed\n",
                                        ZR_DEVNAME(zr));
                                res = -EAGAIN;
                                goto v4l_mmap_unlock_and_return;
@@ -4657,7 +4658,7 @@ zoran_mmap (struct file           *file,
                                break;
                }
        v4l_mmap_unlock_and_return:
-               up(&zr->resource_lock);
+               mutex_unlock(&zr->resource_lock);
 
                break;
 
@@ -4677,6 +4678,7 @@ static struct file_operations zoran_fops = {
        .open = zoran_open,
        .release = zoran_close,
        .ioctl = zoran_ioctl,
+       .compat_ioctl   = v4l_compat_ioctl32,
        .llseek = no_llseek,
        .read = zoran_read,
        .write = zoran_write,
@@ -4687,7 +4689,7 @@ static struct file_operations zoran_fops = {
 struct video_device zoran_template __devinitdata = {
        .name = ZORAN_NAME,
        .type = ZORAN_VID_TYPE,
-#ifdef HAVE_V4L2
+#ifdef CONFIG_VIDEO_V4L2
        .type2 = ZORAN_V4L2_VID_FLAGS,
 #endif
        .hardware = ZORAN_HARDWARE,
@@ -4695,3 +4697,4 @@ struct video_device zoran_template __devinitdata = {
        .release = &zoran_vdev_release,
        .minor = -1
 };
+