fedora core 6 1.2949 + vserver 2.2.0
[linux-2.6.git] / drivers / media / common / saa7146_video.c
index 9329975..7e0cedc 100644 (file)
@@ -1,9 +1,9 @@
 #include <media/saa7146_vv.h>
 
-static int memory = 32;
+static int max_memory = 32;
 
-MODULE_PARM(memory,"i");
-MODULE_PARM_DESC(memory, "maximum memory usage for capture buffers (default: 32Mb)");
+module_param(max_memory, int, 0644);
+MODULE_PARM_DESC(max_memory, "maximum memory usage for capture buffers (default: 32Mb)");
 
 #define IS_CAPTURE_ACTIVE(fh) \
        (((vv->video_status & STATUS_CAPTURE) != 0) && (vv->video_fh == fh))
@@ -14,57 +14,64 @@ MODULE_PARM_DESC(memory, "maximum memory usage for capture buffers (default: 32M
 /* format descriptions for capture and preview */
 static struct saa7146_format formats[] = {
        {
-               .name           = "RGB-8 (3-3-2)",
+               .name           = "RGB-8 (3-3-2)",
                .pixelformat    = V4L2_PIX_FMT_RGB332,
-               .trans          = RGB08_COMPOSED,
+               .trans          = RGB08_COMPOSED,
                .depth          = 8,
                .flags          = 0,
        }, {
-               .name           = "RGB-16 (5/B-6/G-5/R)",
+               .name           = "RGB-16 (5/B-6/G-5/R)",
                .pixelformat    = V4L2_PIX_FMT_RGB565,
-               .trans          = RGB16_COMPOSED,
+               .trans          = RGB16_COMPOSED,
                .depth          = 16,
                .flags          = 0,
        }, {
-               .name           = "RGB-24 (B-G-R)",
+               .name           = "RGB-24 (B-G-R)",
                .pixelformat    = V4L2_PIX_FMT_BGR24,
-               .trans          = RGB24_COMPOSED,
+               .trans          = RGB24_COMPOSED,
                .depth          = 24,
                .flags          = 0,
        }, {
-               .name           = "RGB-32 (B-G-R)",
+               .name           = "RGB-32 (B-G-R)",
                .pixelformat    = V4L2_PIX_FMT_BGR32,
-               .trans          = RGB32_COMPOSED,
+               .trans          = RGB32_COMPOSED,
                .depth          = 32,
                .flags          = 0,
        }, {
-               .name           = "Greyscale-8",
+               .name           = "RGB-32 (R-G-B)",
+               .pixelformat    = V4L2_PIX_FMT_RGB32,
+               .trans          = RGB32_COMPOSED,
+               .depth          = 32,
+               .flags          = 0,
+               .swap           = 0x2,
+       }, {
+               .name           = "Greyscale-8",
                .pixelformat    = V4L2_PIX_FMT_GREY,
-               .trans          = Y8,
+               .trans          = Y8,
                .depth          = 8,
                .flags          = 0,
        }, {
-               .name           = "YUV 4:2:2 planar (Y-Cb-Cr)",
+               .name           = "YUV 4:2:2 planar (Y-Cb-Cr)",
                .pixelformat    = V4L2_PIX_FMT_YUV422P,
-               .trans          = YUV422_DECOMPOSED,
+               .trans          = YUV422_DECOMPOSED,
                .depth          = 16,
                .flags          = FORMAT_BYTE_SWAP|FORMAT_IS_PLANAR,
        }, {
-               .name           = "YVU 4:2:0 planar (Y-Cb-Cr)",
+               .name           = "YVU 4:2:0 planar (Y-Cb-Cr)",
                .pixelformat    = V4L2_PIX_FMT_YVU420,
-               .trans          = YUV420_DECOMPOSED,
+               .trans          = YUV420_DECOMPOSED,
                .depth          = 12,
                .flags          = FORMAT_BYTE_SWAP|FORMAT_IS_PLANAR,
        }, {
-               .name           = "YUV 4:2:0 planar (Y-Cb-Cr)",
+               .name           = "YUV 4:2:0 planar (Y-Cb-Cr)",
                .pixelformat    = V4L2_PIX_FMT_YUV420,
-               .trans          = YUV420_DECOMPOSED,
+               .trans          = YUV420_DECOMPOSED,
                .depth          = 12,
                .flags          = FORMAT_IS_PLANAR,
        }, {
-               .name           = "YUV 4:2:2 (U-Y-V-Y)",
+               .name           = "YUV 4:2:2 (U-Y-V-Y)",
                .pixelformat    = V4L2_PIX_FMT_UYVY,
-               .trans          = YUV422_COMPOSED,
+               .trans          = YUV422_COMPOSED,
                .depth          = 16,
                .flags          = 0,
        }
@@ -73,19 +80,19 @@ static struct saa7146_format formats[] = {
 /* unfortunately, the saa7146 contains a bug which prevents it from doing on-the-fly byte swaps.
    due to this, it's impossible to provide additional *packed* formats, which are simply byte swapped
    (like V4L2_PIX_FMT_YUYV) ... 8-( */
-   
+
 static int NUM_FORMATS = sizeof(formats)/sizeof(struct saa7146_format);
 
 struct saa7146_format* format_by_fourcc(struct saa7146_dev *dev, int fourcc)
 {
        int i, j = NUM_FORMATS;
-       
+
        for (i = 0; i < j; i++) {
                if (formats[i].pixelformat == fourcc) {
                        return formats+i;
                }
        }
-       
+
        DEB_D(("unknown pixelformat:'%4.4s'\n",(char *)&fourcc));
        return NULL;
 }
@@ -143,23 +150,23 @@ static int try_win(struct saa7146_dev *dev, struct v4l2_window *win)
        maxh  = vv->standard->v_max_out;
 
        if (V4L2_FIELD_ANY == field) {
-                field = (win->w.height > maxh/2)
-                        ? V4L2_FIELD_INTERLACED
-                        : V4L2_FIELD_TOP;
-               }
-        switch (field) {
-        case V4L2_FIELD_TOP:
-        case V4L2_FIELD_BOTTOM:
-        case V4L2_FIELD_ALTERNATE:
-                maxh = maxh / 2;
-                break;
-        case V4L2_FIELD_INTERLACED:
-                break;
-        default: {
+               field = (win->w.height > maxh/2)
+                       ? V4L2_FIELD_INTERLACED
+                       : V4L2_FIELD_TOP;
+               }
+       switch (field) {
+       case V4L2_FIELD_TOP:
+       case V4L2_FIELD_BOTTOM:
+       case V4L2_FIELD_ALTERNATE:
+               maxh = maxh / 2;
+               break;
+       case V4L2_FIELD_INTERLACED:
+               break;
+       default: {
                DEB_D(("no known field mode '%d'.\n",field));
-                return -EINVAL;
+               return -EINVAL;
+       }
        }
-        }
 
        win->field = field;
        if (win->w.width > maxw)
@@ -175,7 +182,7 @@ static int try_fmt(struct saa7146_fh *fh, struct v4l2_format *f)
        struct saa7146_dev *dev = fh->dev;
        struct saa7146_vv *vv = dev->vv_data;
        int err;
-       
+
        switch (f->type) {
        case V4L2_BUF_TYPE_VIDEO_CAPTURE:
        {
@@ -194,7 +201,7 @@ static int try_fmt(struct saa7146_fh *fh, struct v4l2_format *f)
                field = f->fmt.pix.field;
                maxw  = vv->standard->h_max_out;
                maxh  = vv->standard->v_max_out;
-               
+
                if (V4L2_FIELD_ANY == field) {
                        field = (f->fmt.pix.height > maxh/2)
                                ? V4L2_FIELD_INTERLACED
@@ -230,10 +237,10 @@ static int try_fmt(struct saa7146_fh *fh, struct v4l2_format *f)
 
                if (f->fmt.pix.bytesperline < calc_bpl)
                        f->fmt.pix.bytesperline = calc_bpl;
-                       
+
                if (f->fmt.pix.bytesperline > (2*PAGE_SIZE * fmt->depth)/8) /* arbitrary constraint */
                        f->fmt.pix.bytesperline = calc_bpl;
-                       
+
                f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * f->fmt.pix.height;
                DEB_D(("w:%d, h:%d, bytesperline:%d, sizeimage:%d\n",f->fmt.pix.width,f->fmt.pix.height,f->fmt.pix.bytesperline,f->fmt.pix.sizeimage));
 
@@ -270,36 +277,36 @@ int saa7146_start_preview(struct saa7146_fh *fh)
        if (IS_CAPTURE_ACTIVE(fh) != 0) {
                DEB_D(("streaming capture is active.\n"));
                return -EBUSY;
-               }
+       }
 
        /* check if overlay is running */
-       if (IS_OVERLAY_ACTIVE(fh) != 0) {               
+       if (IS_OVERLAY_ACTIVE(fh) != 0) {
                if (vv->video_fh == fh) {
-               DEB_D(("overlay is already active.\n"));
-               return 0;
-       }
+                       DEB_D(("overlay is already active.\n"));
+                       return 0;
+               }
                DEB_D(("overlay is already active in another open.\n"));
                return -EBUSY;
        }
-       
+
        if (0 == saa7146_res_get(fh, RESOURCE_DMA1_HPS|RESOURCE_DMA2_CLP)) {
                DEB_D(("cannot get necessary overlay resources\n"));
                return -EBUSY;
-       }       
-       
+       }
+
        err = try_win(dev,&fh->ov.win);
        if (0 != err) {
                saa7146_res_free(vv->video_fh, RESOURCE_DMA1_HPS|RESOURCE_DMA2_CLP);
                return -EBUSY;
        }
-       
+
        vv->ov_data = &fh->ov;
 
        DEB_D(("%dx%d+%d+%d %s field=%s\n",
                fh->ov.win.w.width,fh->ov.win.w.height,
                fh->ov.win.w.left,fh->ov.win.w.top,
                vv->ov_fmt->name,v4l2_field_names[fh->ov.win.field]));
-       
+
        if (0 != (ret = saa7146_enable_overlay(fh))) {
                DEB_D(("enabling overlay failed: %d\n",ret));
                saa7146_res_free(vv->video_fh, RESOURCE_DMA1_HPS|RESOURCE_DMA2_CLP);
@@ -311,6 +318,7 @@ int saa7146_start_preview(struct saa7146_fh *fh)
 
        return 0;
 }
+EXPORT_SYMBOL_GPL(saa7146_start_preview);
 
 int saa7146_stop_preview(struct saa7146_fh *fh)
 {
@@ -326,7 +334,7 @@ int saa7146_stop_preview(struct saa7146_fh *fh)
        }
 
        /* check if overlay is running at all */
-       if ((vv->video_status & STATUS_OVERLAY) == 0) {         
+       if ((vv->video_status & STATUS_OVERLAY) == 0) {
                DEB_D(("no active overlay.\n"));
                return 0;
        }
@@ -340,11 +348,12 @@ int saa7146_stop_preview(struct saa7146_fh *fh)
        vv->video_fh = NULL;
 
        saa7146_disable_overlay(fh);
-       
+
        saa7146_res_free(fh, RESOURCE_DMA1_HPS|RESOURCE_DMA2_CLP);
 
        return 0;
 }
+EXPORT_SYMBOL_GPL(saa7146_stop_preview);
 
 static int s_fmt(struct saa7146_fh *fh, struct v4l2_format *f)
 {
@@ -352,7 +361,7 @@ static int s_fmt(struct saa7146_fh *fh, struct v4l2_format *f)
        struct saa7146_vv *vv = dev->vv_data;
 
        int err;
-       
+
        switch (f->type) {
        case V4L2_BUF_TYPE_VIDEO_CAPTURE:
                DEB_EE(("V4L2_BUF_TYPE_VIDEO_CAPTURE: dev:%p, fh:%p\n",dev,fh));
@@ -371,25 +380,25 @@ static int s_fmt(struct saa7146_fh *fh, struct v4l2_format *f)
                err = try_win(dev,&f->fmt.win);
                if (0 != err)
                        return err;
-               down(&dev->lock);
+               mutex_lock(&dev->lock);
                fh->ov.win    = f->fmt.win;
                fh->ov.nclips = f->fmt.win.clipcount;
                if (fh->ov.nclips > 16)
                        fh->ov.nclips = 16;
                if (copy_from_user(fh->ov.clips,f->fmt.win.clips,sizeof(struct v4l2_clip)*fh->ov.nclips)) {
-                       up(&dev->lock);
+                       mutex_unlock(&dev->lock);
                        return -EFAULT;
                }
-               
+
                /* fh->ov.fh is used to indicate that we have valid overlay informations, too */
                fh->ov.fh = fh;
 
-               up(&dev->lock);
+               mutex_unlock(&dev->lock);
 
                /* check if our current overlay is active */
                if (IS_OVERLAY_ACTIVE(fh) != 0) {
-                               saa7146_stop_preview(fh);
-                               saa7146_start_preview(fh);
+                       saa7146_stop_preview(fh);
+                       saa7146_start_preview(fh);
                }
                return 0;
        default:
@@ -447,7 +456,7 @@ static int NUM_CONTROLS = sizeof(controls)/sizeof(struct v4l2_queryctrl);
 static struct v4l2_queryctrl* ctrl_by_id(int id)
 {
        int i;
-       
+
        for (i = 0; i < NUM_CONTROLS; i++)
                if (controls[i].id == id)
                        return controls+i;
@@ -508,8 +517,8 @@ static int set_control(struct saa7146_fh *fh, struct v4l2_control *c)
                DEB_D(("unknown control %d\n",c->id));
                return -EINVAL;
        }
-       
-       down(&dev->lock);
+
+       mutex_lock(&dev->lock);
 
        switch (ctrl->type) {
        case V4L2_CTRL_TYPE_BOOLEAN:
@@ -553,7 +562,7 @@ static int set_control(struct saa7146_fh *fh, struct v4l2_control *c)
                /* fixme: we can support changing VFLIP and HFLIP here... */
                if (IS_CAPTURE_ACTIVE(fh) != 0) {
                        DEB_D(("V4L2_CID_HFLIP while active capture.\n"));
-                       up(&dev->lock);
+                       mutex_unlock(&dev->lock);
                        return -EINVAL;
                }
                vv->hflip = c->value;
@@ -561,7 +570,7 @@ static int set_control(struct saa7146_fh *fh, struct v4l2_control *c)
        case V4L2_CID_VFLIP:
                if (IS_CAPTURE_ACTIVE(fh) != 0) {
                        DEB_D(("V4L2_CID_VFLIP while active capture.\n"));
-                       up(&dev->lock);
+                       mutex_unlock(&dev->lock);
                        return -EINVAL;
                }
                vv->vflip = c->value;
@@ -570,11 +579,11 @@ static int set_control(struct saa7146_fh *fh, struct v4l2_control *c)
                return -EINVAL;
        }
        }
-       up(&dev->lock);
-       
+       mutex_unlock(&dev->lock);
+
        if (IS_OVERLAY_ACTIVE(fh) != 0) {
-                               saa7146_stop_preview(fh);
-                               saa7146_start_preview(fh);
+               saa7146_stop_preview(fh);
+               saa7146_start_preview(fh);
        }
        return 0;
 }
@@ -626,7 +635,7 @@ static int saa7146_pgtable_build(struct saa7146_dev *dev, struct saa7146_buf *bu
                                return -1;
                        }
                }
-               
+
                ptr1 = pt1->cpu;
                ptr2 = pt2->cpu;
                ptr3 = pt3->cpu;
@@ -634,7 +643,7 @@ static int saa7146_pgtable_build(struct saa7146_dev *dev, struct saa7146_buf *bu
                /* walk all pages, copy all page addresses to ptr1 */
                for (i = 0; i < length; i++, list++) {
                        for (p = 0; p * 4096 < list->length; p++, ptr1++) {
-                               *ptr1 = sg_dma_address(list) - list->offset;
+                               *ptr1 = cpu_to_le32(sg_dma_address(list) - list->offset);
                        }
                }
 /*
@@ -642,14 +651,14 @@ static int saa7146_pgtable_build(struct saa7146_dev *dev, struct saa7146_buf *bu
                for(j=0;j<40;j++) {
                        printk("ptr1 %d: 0x%08x\n",j,ptr1[j]);
                }
-*/             
+*/
 
                /* if we have a user buffer, the first page may not be
                   aligned to a page boundary. */
                pt1->offset = buf->vb.dma.sglist->offset;
                pt2->offset = pt1->offset+o1;
                pt3->offset = pt1->offset+o2;
-               
+
                /* create video-dma2 page table */
                ptr1 = pt1->cpu;
                for(i = m1; i <= m2 ; i++, ptr2++) {
@@ -668,7 +677,7 @@ static int saa7146_pgtable_build(struct saa7146_dev *dev, struct saa7146_buf *bu
                for(;i<1024;i++,ptr3++) {
                        *ptr3 = fill;
                }
-               /* finally: finish up video-dma1 page table */          
+               /* finally: finish up video-dma1 page table */
                ptr1 = pt1->cpu+m1;
                fill = pt1->cpu[m1];
                for(i=m1;i<1024;i++,ptr1++) {
@@ -687,7 +696,7 @@ static int saa7146_pgtable_build(struct saa7146_dev *dev, struct saa7146_buf *bu
                for(j=0;j<40;j++) {
                        printk("ptr3 %d: 0x%08x\n",j,ptr3[j]);
                }
-*/                             
+*/
        } else {
                struct saa7146_pgtable *pt = &buf->pt[0];
                return saa7146_pgtable_build_single(pci, pt, list, length);
@@ -712,9 +721,9 @@ static int video_begin(struct saa7146_fh *fh)
 
        if ((vv->video_status & STATUS_CAPTURE) != 0) {
                if (vv->video_fh == fh) {
-               DEB_S(("already capturing.\n"));
+                       DEB_S(("already capturing.\n"));
                        return 0;
-       }
+               }
                DEB_S(("already capturing in another open.\n"));
                return -EBUSY;
        }
@@ -728,7 +737,7 @@ static int video_begin(struct saa7146_fh *fh)
                        return err;
                }
        }
-       
+
        fmt = format_by_fourcc(dev,fh->video_fmt.pixelformat);
        /* we need to have a valid format set here */
        BUG_ON(NULL == fmt);
@@ -753,7 +762,7 @@ static int video_begin(struct saa7146_fh *fh)
        saa7146_write(dev, MC2, MASK_27 );
 
        /* enable rps0 irqs */
-       IER_ENABLE(dev, MASK_27);
+       SAA7146_IER_ENABLE(dev, MASK_27);
 
        vv->video_fh = fh;
        vv->video_status = STATUS_CAPTURE;
@@ -798,13 +807,13 @@ static int video_end(struct saa7146_fh *fh, struct file *file)
        saa7146_write(dev, MC1, MASK_28);
 
        /* disable rps0 irqs */
-       IER_DISABLE(dev, MASK_27);
+       SAA7146_IER_DISABLE(dev, MASK_27);
 
        /* shut down all used video dma transfers */
        saa7146_write(dev, MC1, dmas);
 
        spin_unlock_irqrestore(&dev->slock, flags);
-       
+
        vv->video_fh = NULL;
        vv->video_status = 0;
 
@@ -813,7 +822,7 @@ static int video_end(struct saa7146_fh *fh, struct file *file)
        if (vv->ov_suspend != NULL) {
                saa7146_start_preview(vv->ov_suspend);
                vv->ov_suspend = NULL;
-}
+       }
 
        return 0;
 }
@@ -840,7 +849,7 @@ int saa7146_video_do_ioctl(struct inode *inode, struct file *file, unsigned int
                if( cmd == dev->ext_vv_data->ioctls[ee].cmd )
                        break;
        }
-       
+
        if( 0 != (dev->ext_vv_data->ioctls[ee].flags & SAA7146_EXCLUSIVE) ) {
                DEB_D(("extension handles ioctl exclusive.\n"));
                result = dev->ext_vv_data->ioctl(fh, cmd, arg);
@@ -853,7 +862,7 @@ int saa7146_video_do_ioctl(struct inode *inode, struct file *file, unsigned int
                        return result;
                }
        }
-       
+
        /* fixme: add handle "after" case (is it still needed?) */
 
        switch (fh->type) {
@@ -873,21 +882,21 @@ int saa7146_video_do_ioctl(struct inode *inode, struct file *file, unsigned int
        }
 
        switch (cmd) {
-       case VIDIOC_QUERYCAP:
+       case VIDIOC_QUERYCAP:
        {
                struct v4l2_capability *cap = arg;
                memset(cap,0,sizeof(*cap));
 
                DEB_EE(("VIDIOC_QUERYCAP\n"));
-               
-                strcpy(cap->driver, "saa7146 v4l2");
+
+               strcpy(cap->driver, "saa7146 v4l2");
                strlcpy(cap->card, dev->ext->name, sizeof(cap->card));
-               sprintf(cap->bus_info,"PCI:%s",dev->pci->slot_name);
+               sprintf(cap->bus_info,"PCI:%s", pci_name(dev->pci));
                cap->version = SAA7146_VERSION_CODE;
                cap->capabilities =
                        V4L2_CAP_VIDEO_CAPTURE |
                        V4L2_CAP_VIDEO_OVERLAY |
-                       V4L2_CAP_READWRITE | 
+                       V4L2_CAP_READWRITE |
                        V4L2_CAP_STREAMING;
                cap->capabilities |= dev->ext_vv_data->capabilities;
                return 0;
@@ -918,21 +927,21 @@ int saa7146_video_do_ioctl(struct inode *inode, struct file *file, unsigned int
                if (NULL == fmt) {
                        return -EINVAL;
                }
-               
+
                /* planar formats are not allowed for overlay video, clipping and video dma would clash */
                if (0 != (fmt->flags & FORMAT_IS_PLANAR)) {
                        DEB_S(("planar pixelformat '%4.4s' not allowed for overlay\n",(char *)&fmt->pixelformat));
                }
 
                /* check if overlay is running */
-               if (IS_OVERLAY_ACTIVE(fh) != 0) {               
+               if (IS_OVERLAY_ACTIVE(fh) != 0) {
                        if (vv->video_fh != fh) {
                                DEB_D(("refusing to change framebuffer informations while overlay is active in another open.\n"));
                                return -EBUSY;
-               }
+                       }
                }
 
-               down(&dev->lock);
+               mutex_lock(&dev->lock);
 
                /* ok, accept it */
                vv->ov_fb = *fb;
@@ -941,7 +950,7 @@ int saa7146_video_do_ioctl(struct inode *inode, struct file *file, unsigned int
                        vv->ov_fb.fmt.bytesperline =
                                vv->ov_fb.fmt.width*fmt->depth/8;
 
-               up(&dev->lock);
+               mutex_unlock(&dev->lock);
 
                return 0;
        }
@@ -964,7 +973,7 @@ int saa7146_video_do_ioctl(struct inode *inode, struct file *file, unsigned int
                        break;
                }
                default:
-                       return -EINVAL; 
+                       return -EINVAL;
                }
 
                DEB_EE(("VIDIOC_ENUM_FMT: type:%d, index:%d\n",f->type,f->index));
@@ -980,12 +989,12 @@ int saa7146_video_do_ioctl(struct inode *inode, struct file *file, unsigned int
                    (c->id <  V4L2_CID_PRIVATE_BASE ||
                     c->id >= V4L2_CID_PRIVATE_LASTP1))
                        return -EINVAL;
-                       
+
                ctrl = ctrl_by_id(c->id);
                if( NULL == ctrl ) {
                        return -EINVAL;
 /*
-                       c->flags = V4L2_CTRL_FLAG_DISABLED;     
+                       c->flags = V4L2_CTRL_FLAG_DISABLED;
                        return 0;
 */
                }
@@ -999,28 +1008,24 @@ int saa7146_video_do_ioctl(struct inode *inode, struct file *file, unsigned int
                return get_control(fh,arg);
        }
        case VIDIOC_S_CTRL:
-
-
-
-
        {
                DEB_EE(("VIDIOC_S_CTRL\n"));
                err = set_control(fh,arg);
                return err;
        }
-        case VIDIOC_G_PARM:
-        {
-                struct v4l2_streamparm *parm = arg;
+       case VIDIOC_G_PARM:
+       {
+               struct v4l2_streamparm *parm = arg;
                if( parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ) {
                        return -EINVAL;
                }
-                memset(&parm->parm.capture,0,sizeof(struct v4l2_captureparm));
+               memset(&parm->parm.capture,0,sizeof(struct v4l2_captureparm));
                parm->parm.capture.readbuffers = 1;
                // fixme: only for PAL!
                parm->parm.capture.timeperframe.numerator = 1;
                parm->parm.capture.timeperframe.denominator = 25;
-                return 0;
-        }
+               return 0;
+       }
        case VIDIOC_G_FMT:
        {
                struct v4l2_format *f = arg;
@@ -1066,7 +1071,7 @@ int saa7146_video_do_ioctl(struct inode *inode, struct file *file, unsigned int
                v4l2_std_id *id = arg;
                int found = 0;
                int i, err;
-                                               
+
                DEB_EE(("VIDIOC_S_STD\n"));
 
                if ((vv->video_status & STATUS_CAPTURE) == STATUS_CAPTURE) {
@@ -1083,8 +1088,8 @@ int saa7146_video_do_ioctl(struct inode *inode, struct file *file, unsigned int
                        }
                }
 
-               down(&dev->lock);
-               
+               mutex_lock(&dev->lock);
+
                for(i = 0; i < dev->ext_vv_data->num_stds; i++)
                        if (*id & dev->ext_vv_data->stds[i].id)
                                break;
@@ -1095,7 +1100,7 @@ int saa7146_video_do_ioctl(struct inode *inode, struct file *file, unsigned int
                        found = 1;
                }
 
-               up(&dev->lock);
+               mutex_unlock(&dev->lock);
 
                if (vv->ov_suspend != NULL) {
                        saa7146_start_preview(vv->ov_suspend);
@@ -1111,10 +1116,6 @@ int saa7146_video_do_ioctl(struct inode *inode, struct file *file, unsigned int
                return 0;
        }
        case VIDIOC_OVERLAY:
-
-
-
-
        {
                int on = *(int *)arg;
                int err = 0;
@@ -1130,7 +1131,7 @@ int saa7146_video_do_ioctl(struct inode *inode, struct file *file, unsigned int
        case VIDIOC_REQBUFS: {
                struct v4l2_requestbuffers *req = arg;
                DEB_D(("VIDIOC_REQBUFS, type:%d\n",req->type));
-               return videobuf_reqbufs(file,q,req);
+               return videobuf_reqbufs(q,req);
        }
        case VIDIOC_QUERYBUF: {
                struct v4l2_buffer *buf = arg;
@@ -1140,14 +1141,14 @@ int saa7146_video_do_ioctl(struct inode *inode, struct file *file, unsigned int
        case VIDIOC_QBUF: {
                struct v4l2_buffer *buf = arg;
                int ret = 0;
-               ret = videobuf_qbuf(file,q,buf);
+               ret = videobuf_qbuf(q,buf);
                DEB_D(("VIDIOC_QBUF: ret:%d, index:%d\n",ret,buf->index));
                return ret;
        }
        case VIDIOC_DQBUF: {
                struct v4l2_buffer *buf = arg;
                int ret = 0;
-               ret = videobuf_dqbuf(file,q,buf);
+               ret = videobuf_dqbuf(q,buf,file->f_flags & O_NONBLOCK);
                DEB_D(("VIDIOC_DQBUF: ret:%d, index:%d\n",ret,buf->index));
                return ret;
        }
@@ -1157,9 +1158,9 @@ int saa7146_video_do_ioctl(struct inode *inode, struct file *file, unsigned int
 
                err = video_begin(fh);
                if( 0 != err) {
-                               return err;
-                       }
-               err = videobuf_streamon(file,q);
+                       return err;
+               }
+               err = videobuf_streamon(q);
                return err;
        }
        case VIDIOC_STREAMOFF: {
@@ -1180,15 +1181,16 @@ int saa7146_video_do_ioctl(struct inode *inode, struct file *file, unsigned int
                        return -EBUSY;
                }
 
-               err = videobuf_streamoff(file,q);
+               err = videobuf_streamoff(q);
                if (0 != err) {
                        DEB_D(("warning: videobuf_streamoff() failed.\n"));
-               video_end(fh, file);
+                       video_end(fh, file);
                } else {
                        err = video_end(fh, file);
                }
                return err;
        }
+#ifdef CONFIG_VIDEO_V4L1_COMPAT
        case VIDIOCGMBUF:
        {
                struct video_mbuf *mbuf = arg;
@@ -1196,17 +1198,17 @@ int saa7146_video_do_ioctl(struct inode *inode, struct file *file, unsigned int
                int i;
 
                /* fixme: number of capture buffers and sizes for v4l apps */
-               int gbuffers = 2; 
+               int gbuffers = 2;
                int gbufsize = 768*576*4;
-               
+
                DEB_D(("VIDIOCGMBUF \n"));
 
                q = &fh->video_q;
-               down(&q->lock);
-               err = videobuf_mmap_setup(file,q,gbuffers,gbufsize,
+               mutex_lock(&q->lock);
+               err = videobuf_mmap_setup(q,gbuffers,gbufsize,
                                          V4L2_MEMORY_MMAP);
                if (err < 0) {
-                       up(&q->lock);
+                       mutex_unlock(&q->lock);
                        return err;
                }
                memset(mbuf,0,sizeof(*mbuf));
@@ -1214,9 +1216,10 @@ int saa7146_video_do_ioctl(struct inode *inode, struct file *file, unsigned int
                mbuf->size   = gbuffers * gbufsize;
                for (i = 0; i < gbuffers; i++)
                        mbuf->offsets[i] = i * gbufsize;
-               up(&q->lock);
+               mutex_unlock(&q->lock);
                return 0;
        }
+#endif
        default:
                return v4l_compat_translate_ioctl(inode,file,cmd,arg,
                                                  saa7146_video_do_ioctl);
@@ -1235,13 +1238,15 @@ static int buffer_activate (struct saa7146_dev *dev,
 
        buf->vb.state = STATE_ACTIVE;
        saa7146_set_capture(dev,buf,next);
-       
+
        mod_timer(&vv->video_q.timeout, jiffies+BUFFER_TIMEOUT);
        return 0;
 }
 
-static int buffer_prepare(struct file *file, struct videobuf_buffer *vb, enum v4l2_field field)
+static int buffer_prepare(struct videobuf_queue *q,
+                         struct videobuf_buffer *vb, enum v4l2_field field)
 {
+       struct file *file = q->priv_data;
        struct saa7146_fh *fh = file->private_data;
        struct saa7146_dev *dev = fh->dev;
        struct saa7146_vv *vv = dev->vv_data;
@@ -1264,7 +1269,7 @@ static int buffer_prepare(struct file *file, struct videobuf_buffer *vb, enum v4
                DEB_D(("size mismatch.\n"));
                return -EINVAL;
        }
-       
+
        DEB_CAP(("buffer_prepare [size=%dx%d,bytes=%d,fields=%s]\n",
                fh->video_fmt.width,fh->video_fmt.height,size,v4l2_field_names[fh->video_fmt.field]));
        if (buf->vb.width  != fh->video_fmt.width  ||
@@ -1274,12 +1279,12 @@ static int buffer_prepare(struct file *file, struct videobuf_buffer *vb, enum v4
            buf->vb.field  != field      ||
            buf->vb.field  != fh->video_fmt.field  ||
            buf->fmt       != &fh->video_fmt) {
-               saa7146_dma_free(dev,buf);
+               saa7146_dma_free(dev,q,buf);
        }
 
        if (STATE_NEEDS_INIT == buf->vb.state) {
                struct saa7146_format *sfmt;
-               
+
                buf->vb.bytesperline  = fh->video_fmt.bytesperline;
                buf->vb.width  = fh->video_fmt.width;
                buf->vb.height = fh->video_fmt.height;
@@ -1287,9 +1292,9 @@ static int buffer_prepare(struct file *file, struct videobuf_buffer *vb, enum v4
                buf->vb.field  = field;
                buf->fmt       = &fh->video_fmt;
                buf->vb.field  = fh->video_fmt.field;
-               
+
                sfmt = format_by_fourcc(dev,buf->fmt->pixelformat);
-                       
+
                if( 0 != IS_PLANAR(sfmt->trans)) {
                        saa7146_pgtable_free(dev->pci, &buf->pt[0]);
                        saa7146_pgtable_free(dev->pci, &buf->pt[1]);
@@ -1302,8 +1307,8 @@ static int buffer_prepare(struct file *file, struct videobuf_buffer *vb, enum v4
                        saa7146_pgtable_free(dev->pci, &buf->pt[0]);
                        saa7146_pgtable_alloc(dev->pci, &buf->pt[0]);
                }
-               
-               err = videobuf_iolock(dev->pci,&buf->vb, &vv->ov_fb);
+
+               err = videobuf_iolock(q,&buf->vb, &vv->ov_fb);
                if (err)
                        goto oops;
                err = saa7146_pgtable_build(dev,buf);
@@ -1317,13 +1322,14 @@ static int buffer_prepare(struct file *file, struct videobuf_buffer *vb, enum v4
 
  oops:
        DEB_D(("error out.\n"));
-       saa7146_dma_free(dev,buf);
+       saa7146_dma_free(dev,q,buf);
 
        return err;
 }
 
-static int buffer_setup(struct file *file, unsigned int *count, unsigned int *size)
+static int buffer_setup(struct videobuf_queue *q, unsigned int *count, unsigned int *size)
 {
+       struct file *file = q->priv_data;
        struct saa7146_fh *fh = file->private_data;
 
        if (0 == *count || *count > MAX_SAA7146_CAPTURE_BUFFERS)
@@ -1331,36 +1337,37 @@ static int buffer_setup(struct file *file, unsigned int *count, unsigned int *si
 
        *size = fh->video_fmt.sizeimage;
 
-       /* check if we exceed the "memory" parameter */
-       if( (*count * *size) > (memory*1048576) ) {
-               *count = (memory*1048576) / *size;
+       /* check if we exceed the "max_memory" parameter */
+       if( (*count * *size) > (max_memory*1048576) ) {
+               *count = (max_memory*1048576) / *size;
        }
-       
+
        DEB_CAP(("%d buffers, %d bytes each.\n",*count,*size));
 
        return 0;
 }
 
-static void buffer_queue(struct file *file, struct videobuf_buffer *vb)
+static void buffer_queue(struct videobuf_queue *q, struct videobuf_buffer *vb)
 {
+       struct file *file = q->priv_data;
        struct saa7146_fh *fh = file->private_data;
        struct saa7146_dev *dev = fh->dev;
        struct saa7146_vv *vv = dev->vv_data;
        struct saa7146_buf *buf = (struct saa7146_buf *)vb;
-       
+
        DEB_CAP(("vbuf:%p\n",vb));
        saa7146_buffer_queue(fh->dev,&vv->video_q,buf);
 }
 
-
-static void buffer_release(struct file *file, struct videobuf_buffer *vb)
+static void buffer_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
 {
+       struct file *file = q->priv_data;
        struct saa7146_fh *fh = file->private_data;
        struct saa7146_dev *dev = fh->dev;
        struct saa7146_buf *buf = (struct saa7146_buf *)vb;
-       
+
        DEB_CAP(("vbuf:%p\n",vb));
-       saa7146_dma_free(dev,buf);
+       saa7146_dma_free(dev,q,buf);
 }
 
 static struct videobuf_queue_ops video_qops = {
@@ -1375,7 +1382,7 @@ static struct videobuf_queue_ops video_qops = {
 
 static void video_init(struct saa7146_dev *dev, struct saa7146_vv *vv)
 {
-        INIT_LIST_HEAD(&vv->video_q.queue);
+       INIT_LIST_HEAD(&vv->video_q.queue);
 
        init_timer(&vv->video_q.timeout);
        vv->video_q.timeout.function = saa7146_buffer_timeout;
@@ -1408,9 +1415,10 @@ static int video_open(struct saa7146_dev *dev, struct file *file)
                            dev->pci, &dev->slock,
                            V4L2_BUF_TYPE_VIDEO_CAPTURE,
                            V4L2_FIELD_INTERLACED,
-                           sizeof(struct saa7146_buf));
+                           sizeof(struct saa7146_buf),
+                           file);
 
-       init_MUTEX(&fh->video_q.lock);
+       mutex_init(&fh->video_q.lock);
 
        return 0;
 }
@@ -1421,13 +1429,13 @@ static void video_close(struct saa7146_dev *dev, struct file *file)
        struct saa7146_fh *fh = (struct saa7146_fh *)file->private_data;
        struct saa7146_vv *vv = dev->vv_data;
        int err;
-       
+
        if (IS_CAPTURE_ACTIVE(fh) != 0) {
-               err = video_end(fh, file);              
+               err = video_end(fh, file);
        } else if (IS_OVERLAY_ACTIVE(fh) != 0) {
                err = saa7146_stop_preview(fh);
        }
-       
+
        /* hmm, why is this function declared void? */
        /* return err */
 }
@@ -1437,7 +1445,7 @@ static void video_irq_done(struct saa7146_dev *dev, unsigned long st)
 {
        struct saa7146_vv *vv = dev->vv_data;
        struct saa7146_dmaqueue *q = &vv->video_q;
-       
+
        spin_lock(&dev->slock);
        DEB_CAP(("called.\n"));
 
@@ -1450,7 +1458,7 @@ static void video_irq_done(struct saa7146_dev *dev, unsigned long st)
        spin_unlock(&dev->slock);
 }
 
-static ssize_t video_read(struct file *file, char *data, size_t count, loff_t *ppos)
+static ssize_t video_read(struct file *file, char __user *data, size_t count, loff_t *ppos)
 {
        struct saa7146_fh *fh = file->private_data;
        struct saa7146_dev *dev = fh->dev;
@@ -1460,11 +1468,11 @@ static ssize_t video_read(struct file *file, char *data, size_t count, loff_t *p
        DEB_EE(("called.\n"));
 
        if ((vv->video_status & STATUS_CAPTURE) != 0) {
-       /* fixme: should we allow read() captures while streaming capture? */
+               /* fixme: should we allow read() captures while streaming capture? */
                if (vv->video_fh == fh) {
-               DEB_S(("already capturing.\n"));
-               return -EBUSY;
-       }
+                       DEB_S(("already capturing.\n"));
+                       return -EBUSY;
+               }
                DEB_S(("already capturing in another open.\n"));
                return -EBUSY;
        }
@@ -1474,9 +1482,10 @@ static ssize_t video_read(struct file *file, char *data, size_t count, loff_t *p
                goto out;
        }
 
-       ret = videobuf_read_one(file,&fh->video_q , data, count, ppos);
+       ret = videobuf_read_one(&fh->video_q , data, count, ppos,
+                               file->f_flags & O_NONBLOCK);
        if (ret != 0) {
-       video_end(fh, file);
+               video_end(fh, file);
        } else {
                ret = video_end(fh, file);
        }
@@ -1486,7 +1495,7 @@ out:
                saa7146_start_preview(vv->ov_suspend);
                vv->ov_suspend = NULL;
        }
-       
+
        return ret;
 }