V4L/DVB: gscpa_sonixb: limit ov7630 max framerate at 640x480
[safe/jmp/linux-2.6] / drivers / media / video / v4l2-dev.c
index 1219721..7090699 100644 (file)
@@ -66,7 +66,49 @@ static struct device_attribute video_device_attrs[] = {
  */
 static struct video_device *video_device[VIDEO_NUM_DEVICES];
 static DEFINE_MUTEX(videodev_lock);
-static DECLARE_BITMAP(video_nums[VFL_TYPE_MAX], VIDEO_NUM_DEVICES);
+static DECLARE_BITMAP(devnode_nums[VFL_TYPE_MAX], VIDEO_NUM_DEVICES);
+
+/* Device node utility functions */
+
+/* Note: these utility functions all assume that vfl_type is in the range
+   [0, VFL_TYPE_MAX-1]. */
+
+#ifdef CONFIG_VIDEO_FIXED_MINOR_RANGES
+/* Return the bitmap corresponding to vfl_type. */
+static inline unsigned long *devnode_bits(int vfl_type)
+{
+       /* Any types not assigned to fixed minor ranges must be mapped to
+          one single bitmap for the purposes of finding a free node number
+          since all those unassigned types use the same minor range. */
+       int idx = (vfl_type > VFL_TYPE_VTX) ? VFL_TYPE_MAX - 1 : vfl_type;
+
+       return devnode_nums[idx];
+}
+#else
+/* Return the bitmap corresponding to vfl_type. */
+static inline unsigned long *devnode_bits(int vfl_type)
+{
+       return devnode_nums[vfl_type];
+}
+#endif
+
+/* Mark device node number vdev->num as used */
+static inline void devnode_set(struct video_device *vdev)
+{
+       set_bit(vdev->num, devnode_bits(vdev->vfl_type));
+}
+
+/* Mark device node number vdev->num as unused */
+static inline void devnode_clear(struct video_device *vdev)
+{
+       clear_bit(vdev->num, devnode_bits(vdev->vfl_type));
+}
+
+/* Try to find a free device node number in the range [from, to> */
+static inline int devnode_find(struct video_device *vdev, int from, int to)
+{
+       return find_next_zero_bit(devnode_bits(vdev->vfl_type), to, from);
+}
 
 struct video_device *video_device_alloc(void)
 {
@@ -119,8 +161,8 @@ static void v4l2_device_release(struct device *cd)
           the release() callback. */
        vdev->cdev = NULL;
 
-       /* Mark minor as free */
-       clear_bit(vdev->num, video_nums[vdev->vfl_type]);
+       /* Mark device node number as free */
+       devnode_clear(vdev);
 
        mutex_unlock(&videodev_lock);
 
@@ -147,7 +189,7 @@ static ssize_t v4l2_read(struct file *filp, char __user *buf,
 
        if (!vdev->fops->read)
                return -EINVAL;
-       if (video_is_unregistered(vdev))
+       if (!video_is_registered(vdev))
                return -EIO;
        return vdev->fops->read(filp, buf, sz, off);
 }
@@ -159,7 +201,7 @@ static ssize_t v4l2_write(struct file *filp, const char __user *buf,
 
        if (!vdev->fops->write)
                return -EINVAL;
-       if (video_is_unregistered(vdev))
+       if (!video_is_registered(vdev))
                return -EIO;
        return vdev->fops->write(filp, buf, sz, off);
 }
@@ -168,7 +210,7 @@ static unsigned int v4l2_poll(struct file *filp, struct poll_table_struct *poll)
 {
        struct video_device *vdev = video_devdata(filp);
 
-       if (!vdev->fops->poll || video_is_unregistered(vdev))
+       if (!vdev->fops->poll || !video_is_registered(vdev))
                return DEFAULT_POLLMASK;
        return vdev->fops->poll(filp, poll);
 }
@@ -208,7 +250,7 @@ static unsigned long v4l2_get_unmapped_area(struct file *filp,
 
        if (!vdev->fops->get_unmapped_area)
                return -ENOSYS;
-       if (video_is_unregistered(vdev))
+       if (!video_is_registered(vdev))
                return -ENODEV;
        return vdev->fops->get_unmapped_area(filp, addr, len, pgoff, flags);
 }
@@ -218,8 +260,7 @@ static int v4l2_mmap(struct file *filp, struct vm_area_struct *vm)
 {
        struct video_device *vdev = video_devdata(filp);
 
-       if (!vdev->fops->mmap ||
-           video_is_unregistered(vdev))
+       if (!vdev->fops->mmap || !video_is_registered(vdev))
                return -ENODEV;
        return vdev->fops->mmap(filp, vm);
 }
@@ -235,7 +276,7 @@ static int v4l2_open(struct inode *inode, struct file *filp)
        vdev = video_devdata(filp);
        /* return ENODEV if the video device has been removed
           already or if it is not registered anymore. */
-       if (vdev == NULL || video_is_unregistered(vdev)) {
+       if (vdev == NULL || !video_is_registered(vdev)) {
                mutex_unlock(&videodev_lock);
                return -ENODEV;
        }
@@ -338,13 +379,16 @@ static int get_index(struct video_device *vdev)
  *     video_register_device - register video4linux devices
  *     @vdev: video device structure we want to register
  *     @type: type of device to register
- *     @nr:   which device number (0 == /dev/video0, 1 == /dev/video1, ...
+ *     @nr:   which device node number (0 == /dev/video0, 1 == /dev/video1, ...
  *             -1 == first free)
+ *     @warn_if_nr_in_use: warn if the desired device node number
+ *            was already in use and another number was chosen instead.
  *
- *     The registration code assigns minor numbers based on the type
- *     requested. -ENFILE is returned in all the device slots for this
- *     category are full. If not then the minor field is set and the
- *     driver initialize function is called (if non %NULL).
+ *     The registration code assigns minor numbers and device node numbers
+ *     based on the requested type and registers the new device node with
+ *     the kernel.
+ *     An error is returned if no free minor or device node number could be
+ *     found, or if the registration of the device node failed.
  *
  *     Zero is returned on success.
  *
@@ -358,7 +402,8 @@ static int get_index(struct video_device *vdev)
  *
  *     %VFL_TYPE_RADIO - A radio card
  */
-int video_register_device(struct video_device *vdev, int type, int nr)
+static int __video_register_device(struct video_device *vdev, int type, int nr,
+               int warn_if_nr_in_use)
 {
        int i = 0;
        int ret;
@@ -401,7 +446,7 @@ int video_register_device(struct video_device *vdev, int type, int nr)
        if (vdev->v4l2_dev && vdev->v4l2_dev->dev)
                vdev->parent = vdev->v4l2_dev->dev;
 
-       /* Part 2: find a free minor, kernel number and device index. */
+       /* Part 2: find a free minor, device node number and device index. */
 #ifdef CONFIG_VIDEO_FIXED_MINOR_RANGES
        /* Keep the ranges for the first four types for historical
         * reasons.
@@ -432,21 +477,22 @@ int video_register_device(struct video_device *vdev, int type, int nr)
        }
 #endif
 
-       /* Pick a minor number */
+       /* Pick a device node number */
        mutex_lock(&videodev_lock);
-       nr = find_next_zero_bit(video_nums[type], minor_cnt, nr == -1 ? 0 : nr);
+       nr = devnode_find(vdev, nr == -1 ? 0 : nr, minor_cnt);
        if (nr == minor_cnt)
-               nr = find_first_zero_bit(video_nums[type], minor_cnt);
+               nr = devnode_find(vdev, 0, minor_cnt);
        if (nr == minor_cnt) {
-               printk(KERN_ERR "could not get a free kernel number\n");
+               printk(KERN_ERR "could not get a free device node number\n");
                mutex_unlock(&videodev_lock);
                return -ENFILE;
        }
 #ifdef CONFIG_VIDEO_FIXED_MINOR_RANGES
-       /* 1-on-1 mapping of kernel number to minor number */
+       /* 1-on-1 mapping of device node number to minor number */
        i = nr;
 #else
-       /* The kernel number and minor numbers are independent */
+       /* The device node number and minor numbers are independent, so
+          we just find the first free minor number. */
        for (i = 0; i < VIDEO_NUM_DEVICES; i++)
                if (video_device[i] == NULL)
                        break;
@@ -458,7 +504,8 @@ int video_register_device(struct video_device *vdev, int type, int nr)
 #endif
        vdev->minor = i + minor_offset;
        vdev->num = nr;
-       set_bit(nr, video_nums[type]);
+       devnode_set(vdev);
+
        /* Should not happen since we thought this minor was free */
        WARN_ON(video_device[vdev->minor] != NULL);
        vdev->index = get_index(vdev);
@@ -492,7 +539,7 @@ int video_register_device(struct video_device *vdev, int type, int nr)
        vdev->dev.devt = MKDEV(VIDEO_MAJOR, vdev->minor);
        if (vdev->parent)
                vdev->dev.parent = vdev->parent;
-       dev_set_name(&vdev->dev, "%s%d", name_base, nr);
+       dev_set_name(&vdev->dev, "%s%d", name_base, vdev->num);
        ret = device_register(&vdev->dev);
        if (ret < 0) {
                printk(KERN_ERR "%s: device_register failed\n", __func__);
@@ -502,7 +549,12 @@ int video_register_device(struct video_device *vdev, int type, int nr)
           reference to the device goes away. */
        vdev->dev.release = v4l2_device_release;
 
+       if (nr != -1 && nr != vdev->num && warn_if_nr_in_use)
+               printk(KERN_WARNING "%s: requested %s%d, got %s\n", __func__,
+                       name_base, nr, video_device_node_name(vdev));
+
        /* Part 5: Activate this minor. The char device can now be used. */
+       set_bit(V4L2_FL_REGISTERED, &vdev->flags);
        mutex_lock(&videodev_lock);
        video_device[vdev->minor] = vdev;
        mutex_unlock(&videodev_lock);
@@ -512,14 +564,25 @@ cleanup:
        mutex_lock(&videodev_lock);
        if (vdev->cdev)
                cdev_del(vdev->cdev);
-       clear_bit(vdev->num, video_nums[type]);
+       devnode_clear(vdev);
        mutex_unlock(&videodev_lock);
        /* Mark this video device as never having been registered. */
        vdev->minor = -1;
        return ret;
 }
+
+int video_register_device(struct video_device *vdev, int type, int nr)
+{
+       return __video_register_device(vdev, type, nr, 1);
+}
 EXPORT_SYMBOL(video_register_device);
 
+int video_register_device_no_warn(struct video_device *vdev, int type, int nr)
+{
+       return __video_register_device(vdev, type, nr, 0);
+}
+EXPORT_SYMBOL(video_register_device_no_warn);
+
 /**
  *     video_unregister_device - unregister a video4linux device
  *     @vdev: the device to unregister
@@ -530,11 +593,11 @@ EXPORT_SYMBOL(video_register_device);
 void video_unregister_device(struct video_device *vdev)
 {
        /* Check if vdev was ever registered at all */
-       if (!vdev || vdev->minor < 0)
+       if (!vdev || !video_is_registered(vdev))
                return;
 
        mutex_lock(&videodev_lock);
-       set_bit(V4L2_FL_UNREGISTERED, &vdev->flags);
+       clear_bit(V4L2_FL_REGISTERED, &vdev->flags);
        mutex_unlock(&videodev_lock);
        device_unregister(&vdev->dev);
 }