Fedora kernel-2.6.17-1.2142_FC4 patched with stable patch-2.6.17.4-vs2.0.2-rc26.diff
[linux-2.6.git] / drivers / media / common / saa7146_fops.c
index cb826c9..523ab38 100644 (file)
@@ -1,7 +1,6 @@
 #include <media/saa7146_vv.h>
-#include <linux/version.h>
 
-#define BOARD_CAN_DO_VBI(dev)   (dev->revision != 0 && dev->vv_data->vbi_minor != -1) 
+#define BOARD_CAN_DO_VBI(dev)   (dev->revision != 0 && dev->vv_data->vbi_minor != -1)
 
 /****************************************************************************/
 /* resource management functions, shamelessly stolen from saa7134 driver */
@@ -18,18 +17,18 @@ int saa7146_res_get(struct saa7146_fh *fh, unsigned int bit)
        }
 
        /* is it free? */
-       down(&dev->lock);
+       mutex_lock(&dev->lock);
        if (vv->resources & bit) {
                DEB_D(("locked! vv->resources:0x%02x, we want:0x%02x\n",vv->resources,bit));
                /* no, someone else uses it */
-               up(&dev->lock);
+               mutex_unlock(&dev->lock);
                return 0;
        }
        /* it's free, grab it */
        fh->resources  |= bit;
        vv->resources |= bit;
        DEB_D(("res: get 0x%02x, cur:0x%02x\n",bit,vv->resources));
-       up(&dev->lock);
+       mutex_unlock(&dev->lock);
        return 1;
 }
 
@@ -38,29 +37,28 @@ void saa7146_res_free(struct saa7146_fh *fh, unsigned int bits)
        struct saa7146_dev *dev = fh->dev;
        struct saa7146_vv *vv = dev->vv_data;
 
-       if ((fh->resources & bits) != bits)
-               BUG();
+       BUG_ON((fh->resources & bits) != bits);
 
-       down(&dev->lock);
+       mutex_lock(&dev->lock);
        fh->resources  &= ~bits;
        vv->resources &= ~bits;
        DEB_D(("res: put 0x%02x, cur:0x%02x\n",bits,vv->resources));
-       up(&dev->lock);
+       mutex_unlock(&dev->lock);
 }
 
 
 /********************************************************************************/
 /* common dma functions */
 
-void saa7146_dma_free(struct saa7146_dev *dev,struct saa7146_buf *buf)
+void saa7146_dma_free(struct saa7146_dev *dev,struct videobuf_queue *q,
+                                               struct saa7146_buf *buf)
 {
        DEB_EE(("dev:%p, buf:%p\n",dev,buf));
 
-       if (in_interrupt())
-               BUG();
+       BUG_ON(in_interrupt());
 
        videobuf_waiton(&buf->vb,0,0);
-       videobuf_dma_pci_unmap(dev->pci, &buf->vb.dma);
+       videobuf_dma_unmap(q, &buf->vb.dma);
        videobuf_dma_free(&buf->vb.dma);
        buf->vb.state = STATE_NEEDS_INIT;
 }
@@ -103,9 +101,9 @@ void saa7146_buffer_finish(struct saa7146_dev *dev,
        /* finish current buffer */
        if (NULL == q->curr) {
                DEB_D(("aiii. no current buffer\n"));
-               return; 
+               return;
        }
-                       
+
        q->curr->vb.state = state;
        do_gettimeofday(&q->curr->vb.ts);
        wake_up(&q->curr->vb.done);
@@ -144,13 +142,13 @@ void saa7146_buffer_next(struct saa7146_dev *dev,
                        // fixme: fix this for vflip != 0
 
                        saa7146_write(dev, PROT_ADDR1, 0);
-                       saa7146_write(dev, MC2, (MASK_02|MASK_18));             
+                       saa7146_write(dev, MC2, (MASK_02|MASK_18));
 
                        /* write the address of the rps-program */
                        saa7146_write(dev, RPS_ADDR0, dev->d_rps0.dma_handle);
                        /* turn on rps */
                        saa7146_write(dev, MC1, (MASK_12 | MASK_28));
-                               
+
 /*
                        printk("vdma%d.base_even:     0x%08x\n", 1,saa7146_read(dev,BASE_EVEN1));
                        printk("vdma%d.base_odd:      0x%08x\n", 1,saa7146_read(dev,BASE_ODD1));
@@ -205,7 +203,7 @@ static int fops_open(struct inode *inode, struct file *file)
 
        DEB_EE(("inode:%p, file:%p, minor:%d\n",inode,file,minor));
 
-       if (down_interruptible(&saa7146_devices_lock))
+       if (mutex_lock_interruptible(&saa7146_devices_lock))
                return -ERESTARTSYS;
 
        list_for_each(list,&saa7146_devices) {
@@ -240,21 +238,23 @@ static int fops_open(struct inode *inode, struct file *file)
        }
 
        /* allocate per open data */
-       fh = kmalloc(sizeof(*fh),GFP_KERNEL);
+       fh = kzalloc(sizeof(*fh),GFP_KERNEL);
        if (NULL == fh) {
                DEB_S(("cannot allocate memory for per open data.\n"));
                result = -ENOMEM;
                goto out;
        }
-       memset(fh,0,sizeof(*fh));
-       
+
        file->private_data = fh;
        fh->dev = dev;
        fh->type = type;
 
        if( fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
                DEB_S(("initializing vbi...\n"));
-               result = saa7146_vbi_uops.open(dev,file);
+               if (dev->ext_vv_data->capabilities & V4L2_CAP_VBI_CAPTURE)
+                       result = saa7146_vbi_uops.open(dev,file);
+               if (dev->ext_vv_data->vbi_fops.open)
+                       dev->ext_vv_data->vbi_fops.open(inode, file);
        } else {
                DEB_S(("initializing video...\n"));
                result = saa7146_video_uops.open(dev,file);
@@ -275,8 +275,8 @@ out:
                kfree(fh);
                file->private_data = NULL;
        }
-       up(&saa7146_devices_lock);
-        return result;
+       mutex_unlock(&saa7146_devices_lock);
+       return result;
 }
 
 static int fops_release(struct inode *inode, struct file *file)
@@ -286,11 +286,14 @@ static int fops_release(struct inode *inode, struct file *file)
 
        DEB_EE(("inode:%p, file:%p\n",inode,file));
 
-       if (down_interruptible(&saa7146_devices_lock))
+       if (mutex_lock_interruptible(&saa7146_devices_lock))
                return -ERESTARTSYS;
 
        if( fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
-               saa7146_vbi_uops.release(dev,file);
+               if (dev->ext_vv_data->capabilities & V4L2_CAP_VBI_CAPTURE)
+                       saa7146_vbi_uops.release(dev,file);
+               if (dev->ext_vv_data->vbi_fops.release)
+                       dev->ext_vv_data->vbi_fops.release(inode, file);
        } else {
                saa7146_video_uops.release(dev,file);
        }
@@ -299,7 +302,7 @@ static int fops_release(struct inode *inode, struct file *file)
        file->private_data = NULL;
        kfree(fh);
 
-       up(&saa7146_devices_lock);
+       mutex_unlock(&saa7146_devices_lock);
 
        return 0;
 }
@@ -333,6 +336,7 @@ static int fops_mmap(struct file *file, struct vm_area_struct * vma)
                BUG();
                return 0;
        }
+
        return videobuf_mmap_mapper(q,vma);
 }
 
@@ -382,7 +386,10 @@ static ssize_t fops_read(struct file *file, char __user *data, size_t count, lof
                }
        case V4L2_BUF_TYPE_VBI_CAPTURE: {
 //             DEB_EE(("V4L2_BUF_TYPE_VBI_CAPTURE: file:%p, data:%p, count:%lu\n", file, data, (unsigned long)count));
-               return saa7146_vbi_uops.read(file,data,count,ppos);
+               if (fh->dev->ext_vv_data->capabilities & V4L2_CAP_VBI_CAPTURE)
+                       return saa7146_vbi_uops.read(file,data,count,ppos);
+               else
+                       return -EINVAL;
                }
                break;
        default:
@@ -391,22 +398,41 @@ static ssize_t fops_read(struct file *file, char __user *data, size_t count, lof
        }
 }
 
+static ssize_t fops_write(struct file *file, const char __user *data, size_t count, loff_t *ppos)
+{
+       struct saa7146_fh *fh = file->private_data;
+
+       switch (fh->type) {
+       case V4L2_BUF_TYPE_VIDEO_CAPTURE:
+               return -EINVAL;
+       case V4L2_BUF_TYPE_VBI_CAPTURE:
+               if (fh->dev->ext_vv_data->vbi_fops.write)
+                       return fh->dev->ext_vv_data->vbi_fops.write(file, data, count, ppos);
+               else
+                       return -EINVAL;
+       default:
+               BUG();
+               return -EINVAL;
+       }
+}
+
 static struct file_operations video_fops =
 {
        .owner          = THIS_MODULE,
        .open           = fops_open,
        .release        = fops_release,
        .read           = fops_read,
+       .write          = fops_write,
        .poll           = fops_poll,
        .mmap           = fops_mmap,
        .ioctl          = fops_ioctl,
        .llseek         = no_llseek,
 };
 
-void vv_callback(struct saa7146_dev *dev, unsigned long status)
+static void vv_callback(struct saa7146_dev *dev, unsigned long status)
 {
        u32 isr = status;
-       
+
        DEB_INT(("dev:%p, isr:0x%08x\n",dev,(u32)status));
 
        if (0 != (isr & (MASK_27))) {
@@ -436,12 +462,11 @@ static struct video_device device_template =
 
 int saa7146_vv_init(struct saa7146_dev* dev, struct saa7146_ext_vv *ext_vv)
 {
-       struct saa7146_vv *vv = kmalloc (sizeof(struct saa7146_vv),GFP_KERNEL);
+       struct saa7146_vv *vv = kzalloc (sizeof(struct saa7146_vv),GFP_KERNEL);
        if( NULL == vv ) {
                ERR(("out of memory. aborting.\n"));
                return -1;
        }
-       memset(vv, 0x0, sizeof(*vv));
 
        DEB_EE(("dev:%p\n",dev));
 
@@ -455,11 +480,11 @@ int saa7146_vv_init(struct saa7146_dev* dev, struct saa7146_ext_vv *ext_vv)
           handle different devices that might need different
           configuration data) */
        dev->ext_vv_data = ext_vv;
-       
+
        vv->video_minor = -1;
        vv->vbi_minor = -1;
 
-       vv->d_clipping.cpu_addr = pci_alloc_consistent(dev->pci, SAA7146_CLIPPING_MEM, &vv->d_clipping.dma_handle);     
+       vv->d_clipping.cpu_addr = pci_alloc_consistent(dev->pci, SAA7146_CLIPPING_MEM, &vv->d_clipping.dma_handle);
        if( NULL == vv->d_clipping.cpu_addr ) {
                ERR(("out of memory. aborting.\n"));
                kfree(vv);
@@ -468,8 +493,9 @@ int saa7146_vv_init(struct saa7146_dev* dev, struct saa7146_ext_vv *ext_vv)
        memset(vv->d_clipping.cpu_addr, 0x0, SAA7146_CLIPPING_MEM);
 
        saa7146_video_uops.init(dev,vv);
-       saa7146_vbi_uops.init(dev,vv);
-       
+       if (dev->ext_vv_data->capabilities & V4L2_CAP_VBI_CAPTURE)
+               saa7146_vbi_uops.init(dev,vv);
+
        dev->vv_data = vv;
        dev->vv_callback = &vv_callback;
 
@@ -481,12 +507,12 @@ int saa7146_vv_release(struct saa7146_dev* dev)
        struct saa7146_vv *vv = dev->vv_data;
 
        DEB_EE(("dev:%p\n",dev));
+
        pci_free_consistent(dev->pci, SAA7146_RPS_MEM, vv->d_clipping.cpu_addr, vv->d_clipping.dma_handle);
-       kfree(vv);
+       kfree(vv);
        dev->vv_data = NULL;
        dev->vv_callback = NULL;
-       
+
        return 0;
 }
 
@@ -499,7 +525,7 @@ int saa7146_register_device(struct video_device **vid, struct saa7146_dev* dev,
        DEB_EE(("dev:%p, name:'%s', type:%d\n",dev,name,type));
 
        // released by vfd->release
-       vfd = video_device_alloc();
+       vfd = video_device_alloc();
        if (vfd == NULL)
                return -ENOMEM;
 
@@ -531,7 +557,7 @@ int saa7146_register_device(struct video_device **vid, struct saa7146_dev* dev,
 int saa7146_unregister_device(struct video_device **vid, struct saa7146_dev* dev)
 {
        struct saa7146_vv *vv = dev->vv_data;
-       
+
        DEB_EE(("dev:%p\n",dev));
 
        if( VFL_TYPE_GRABBER == (*vid)->type ) {