fedora core 6 1.2949 + vserver 2.2.0
[linux-2.6.git] / drivers / media / video / zoran_driver.c
index 485553b..862a984 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>
@@ -73,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 ( \
@@ -93,7 +94,7 @@
                                V4L2_CAP_VIDEO_CAPTURE |\
                                V4L2_CAP_VIDEO_OUTPUT |\
                                V4L2_CAP_VIDEO_OVERLAY \
-                              )
+                             )
 #endif
 
 #include <asm/byteorder.h>
@@ -102,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
@@ -116,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
@@ -130,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
@@ -144,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
@@ -158,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
@@ -209,7 +210,7 @@ static int lock_norm = 0;   /* 1=Don't change TV standard (norm) */
 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 */
@@ -669,7 +670,7 @@ jpg_fbuffer_free (struct file *file)
                                                                       j]))));
                                free_page((unsigned long)
                                          bus_to_virt
-                                                 (le32_to_cpu
+                                                 (le32_to_cpu
                                                   (fh->jpg_buffers.
                                                      buffer[i].
                                                      frag_tab[2 * j])));
@@ -1292,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,
@@ -1371,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 */
@@ -1401,7 +1402,7 @@ open_unlock_and_return:
 
        /* 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;
@@ -1419,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);
 
@@ -1466,7 +1467,7 @@ zoran_close (struct inode *inode,
        }
        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));
 
@@ -1511,6 +1512,13 @@ setup_fbuffer (struct file               *file,
        if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_RAWIO))
                return -EPERM;
 
+       /* Don't allow frame buffer overlay if PCI or AGP is buggy, or on
+          ALi Magik (that needs very low latency while the card needs a
+          higher value always) */
+
+       if (pci_pci_problems & (PCIPCI_FAIL | PCIAGP_FAIL | PCIPCI_ALIMAGIK))
+               return -ENXIO;
+
        /* we need a bytesperline value, even if not given */
        if (!bytesperline)
                bytesperline = width * ((fmt->depth + 7) & ~7) / 8;
@@ -1760,7 +1768,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,
@@ -1870,7 +1878,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;
 
@@ -2005,9 +2013,9 @@ 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;
@@ -2027,14 +2035,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) {
@@ -2046,17 +2054,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;
        }
@@ -2084,9 +2092,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;
@@ -2094,7 +2102,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
@@ -2113,7 +2121,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)))
@@ -2122,7 +2130,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;
@@ -2134,7 +2142,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;
@@ -2145,7 +2153,7 @@ zoran_do_ioctl (struct inode *inode,
                } else {
                        vpict->depth = 0;
                }
-               up(&zr->resource_lock);
+               mutex_unlock(&zr->resource_lock);
 
                return 0;
        }
@@ -2180,7 +2188,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);
 
@@ -2191,7 +2199,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;
        }
@@ -2204,9 +2212,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;
        }
@@ -2219,12 +2227,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;
        }
@@ -2241,12 +2249,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;
        }
@@ -2258,9 +2266,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;
@@ -2287,12 +2295,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;
        }
@@ -2305,9 +2313,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;
@@ -2325,9 +2333,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;
@@ -2348,7 +2356,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,
@@ -2367,7 +2375,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;
        }
@@ -2421,7 +2429,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;
@@ -2450,7 +2458,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;
 
@@ -2486,7 +2494,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,
@@ -2506,7 +2514,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;
        }
@@ -2538,7 +2546,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,
@@ -2561,7 +2569,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;
        }
@@ -2574,9 +2582,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;
        }
@@ -2589,9 +2597,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;
        }
@@ -2604,9 +2612,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;
        }
@@ -2630,7 +2638,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,
@@ -2655,7 +2663,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 =
@@ -2675,7 +2683,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
@@ -2688,8 +2696,8 @@ 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",
                         pci_name(zr->pci_dev));
                cap->version =
@@ -2741,7 +2749,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;
@@ -2763,7 +2771,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;
@@ -2776,14 +2784,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) {
@@ -2837,7 +2845,7 @@ zoran_do_ioctl (struct inode *inode,
                                    V4L2_COLORSPACE_SMPTE170M;
                        }
 
-                       up(&zr->resource_lock);
+                       mutex_unlock(&zr->resource_lock);
 
                        break;
 
@@ -2870,7 +2878,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,
@@ -2880,7 +2888,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;
 
@@ -2917,7 +2925,7 @@ zoran_do_ioctl (struct inode *inode,
                        }
 
                        if (fmt->fmt.pix.pixelformat == V4L2_PIX_FMT_MJPEG) {
-                               down(&zr->resource_lock);
+                               mutex_lock(&zr->resource_lock);
 
                                settings = fh->jpg_settings;
 
@@ -2995,7 +3003,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 ==
@@ -3010,7 +3018,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 !=
@@ -3052,7 +3060,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;
@@ -3077,7 +3085,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;
@@ -3086,7 +3094,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;
@@ -3121,12 +3129,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;
        }
@@ -3139,9 +3147,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;
        }
@@ -3163,7 +3171,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,
@@ -3224,7 +3232,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;
        }
@@ -3245,9 +3253,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;
        }
@@ -3262,7 +3270,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:
@@ -3322,7 +3330,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;
        }
@@ -3336,7 +3344,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:
@@ -3410,7 +3418,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;
        }
@@ -3422,7 +3430,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 */
@@ -3470,7 +3478,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;
        }
@@ -3482,7 +3490,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 */
@@ -3540,7 +3548,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;
        }
@@ -3565,16 +3573,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;
                }
 
@@ -3600,7 +3608,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;
@@ -3615,7 +3623,7 @@ zoran_do_ioctl (struct inode *inode,
                        ctrl->value = zr->hue;
                        break;
                }
-               up(&zr->resource_lock);
+               mutex_unlock(&zr->resource_lock);
 
                return 0;
        }
@@ -3642,7 +3650,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;
@@ -3664,7 +3672,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;
        }
@@ -3692,7 +3700,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;
@@ -3700,21 +3708,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;
@@ -3732,9 +3740,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:
@@ -3776,13 +3784,13 @@ zoran_do_ioctl (struct inode *inode,
                        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;
@@ -3809,9 +3817,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;
@@ -3830,9 +3838,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;
        }
@@ -3845,14 +3853,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;
@@ -3870,7 +3878,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;
        }
@@ -3914,7 +3922,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 ||
@@ -3934,7 +3942,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;
@@ -3950,7 +3958,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 ||
@@ -3969,7 +3977,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;
        }
@@ -3988,7 +3996,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,
@@ -4024,7 +4032,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;
@@ -4038,7 +4046,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;
@@ -4053,7 +4061,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;
        }
@@ -4074,7 +4082,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) {
@@ -4093,7 +4101,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;
        }
@@ -4127,7 +4135,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;
@@ -4138,7 +4146,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:
@@ -4146,7 +4154,7 @@ 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) {
                                settings = fh->jpg_settings;
@@ -4229,7 +4237,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;
@@ -4280,7 +4288,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:
@@ -4329,7 +4337,7 @@ zoran_poll (struct file *file,
        }
 
 poll_unlock_and_return:
-       up(&zr->resource_lock);
+       mutex_unlock(&zr->resource_lock);
 
        return res;
 }
@@ -4385,7 +4393,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);
@@ -4398,7 +4406,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;
@@ -4421,7 +4429,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);
@@ -4434,7 +4442,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;
@@ -4489,7 +4497,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) {
@@ -4579,13 +4587,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) {
@@ -4657,7 +4665,7 @@ zoran_mmap (struct file           *file,
                                break;
                }
        v4l_mmap_unlock_and_return:
-               up(&zr->resource_lock);
+               mutex_unlock(&zr->resource_lock);
 
                break;
 
@@ -4688,7 +4696,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,