V4L/DVB: gspca_sonixb: Add support for camera button
[safe/jmp/linux-2.6] / drivers / media / video / vivi.c
index 625e966..cdbe703 100644 (file)
 #include <linux/mutex.h>
 #include <linux/videodev2.h>
 #include <linux/dma-mapping.h>
-#ifdef CONFIG_VIDEO_V4L1_COMPAT
-/* Include V4L1 specific functions. Should be removed soon */
-#include <linux/videodev.h>
-#endif
 #include <linux/interrupt.h>
 #include <linux/kthread.h>
 #include <linux/highmem.h>
@@ -84,7 +80,7 @@ static struct v4l2_queryctrl vivi_qctrl[] = {
                .maximum       = 65535,
                .step          = 65535/100,
                .default_value = 65535,
-               .flags         = 0,
+               .flags         = V4L2_CTRL_FLAG_SLIDER,
                .type          = V4L2_CTRL_TYPE_INTEGER,
        }, {
                .id            = V4L2_CID_BRIGHTNESS,
@@ -94,7 +90,7 @@ static struct v4l2_queryctrl vivi_qctrl[] = {
                .maximum       = 255,
                .step          = 1,
                .default_value = 127,
-               .flags         = 0,
+               .flags         = V4L2_CTRL_FLAG_SLIDER,
        }, {
                .id            = V4L2_CID_CONTRAST,
                .type          = V4L2_CTRL_TYPE_INTEGER,
@@ -103,7 +99,7 @@ static struct v4l2_queryctrl vivi_qctrl[] = {
                .maximum       = 255,
                .step          = 0x1,
                .default_value = 0x10,
-               .flags         = 0,
+               .flags         = V4L2_CTRL_FLAG_SLIDER,
        }, {
                .id            = V4L2_CID_SATURATION,
                .type          = V4L2_CTRL_TYPE_INTEGER,
@@ -112,7 +108,7 @@ static struct v4l2_queryctrl vivi_qctrl[] = {
                .maximum       = 255,
                .step          = 0x1,
                .default_value = 127,
-               .flags         = 0,
+               .flags         = V4L2_CTRL_FLAG_SLIDER,
        }, {
                .id            = V4L2_CID_HUE,
                .type          = V4L2_CTRL_TYPE_INTEGER,
@@ -121,12 +117,10 @@ static struct v4l2_queryctrl vivi_qctrl[] = {
                .maximum       = 127,
                .step          = 0x1,
                .default_value = 0,
-               .flags         = 0,
+               .flags         = V4L2_CTRL_FLAG_SLIDER,
        }
 };
 
-static int qctl_regs[ARRAY_SIZE(vivi_qctrl)];
-
 #define dprintk(dev, level, fmt, arg...) \
        v4l2_dbg(level, debug, &dev->v4l2_dev, fmt, ## arg)
 
@@ -239,6 +233,9 @@ struct vivi_dev {
 
        /* Input Number */
        int                        input;
+
+       /* Control 'registers' */
+       int                        qctl_regs[ARRAY_SIZE(vivi_qctrl)];
 };
 
 struct vivi_fh {
@@ -346,6 +343,53 @@ static struct bar_std bars[] = {
 #define TO_U(r, g, b) \
        (((-9714 * r - 19070 * g + 28784 * b + 32768) >> 16) + 128)
 
+/* precalculate color bar values to speed up rendering */
+static void precalculate_bars(struct vivi_fh *fh)
+{
+       struct vivi_dev *dev = fh->dev;
+       unsigned char r, g, b;
+       int k, is_yuv;
+
+       fh->input = dev->input;
+
+       for (k = 0; k < 8; k++) {
+               r = bars[fh->input].bar[k][0];
+               g = bars[fh->input].bar[k][1];
+               b = bars[fh->input].bar[k][2];
+               is_yuv = 0;
+
+               switch (fh->fmt->fourcc) {
+               case V4L2_PIX_FMT_YUYV:
+               case V4L2_PIX_FMT_UYVY:
+                       is_yuv = 1;
+                       break;
+               case V4L2_PIX_FMT_RGB565:
+               case V4L2_PIX_FMT_RGB565X:
+                       r >>= 3;
+                       g >>= 2;
+                       b >>= 3;
+                       break;
+               case V4L2_PIX_FMT_RGB555:
+               case V4L2_PIX_FMT_RGB555X:
+                       r >>= 3;
+                       g >>= 3;
+                       b >>= 3;
+                       break;
+               }
+
+               if (is_yuv) {
+                       fh->bars[k][0] = TO_Y(r, g, b); /* Luma */
+                       fh->bars[k][1] = TO_U(r, g, b); /* Cb */
+                       fh->bars[k][2] = TO_V(r, g, b); /* Cr */
+               } else {
+                       fh->bars[k][0] = r;
+                       fh->bars[k][1] = g;
+                       fh->bars[k][2] = b;
+               }
+       }
+
+}
+
 #define TSTAMP_MIN_Y   24
 #define TSTAMP_MAX_Y   (TSTAMP_MIN_Y + 15)
 #define TSTAMP_INPUT_X 10
@@ -758,6 +802,8 @@ buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
        buf->vb.height = fh->height;
        buf->vb.field  = field;
 
+       precalculate_bars(fh);
+
        if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
                rc = videobuf_iolock(vq, &buf->vb, NULL);
                if (rc < 0)
@@ -886,15 +932,8 @@ static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
        maxh  = norm_maxh();
 
        f->fmt.pix.field = field;
-       if (f->fmt.pix.height < 32)
-               f->fmt.pix.height = 32;
-       if (f->fmt.pix.height > maxh)
-               f->fmt.pix.height = maxh;
-       if (f->fmt.pix.width < 48)
-               f->fmt.pix.width = 48;
-       if (f->fmt.pix.width > maxw)
-               f->fmt.pix.width = maxw;
-       f->fmt.pix.width &= ~0x03;
+       v4l_bound_align_image(&f->fmt.pix.width, 48, maxw, 2,
+                             &f->fmt.pix.height, 32, maxh, 0, 0);
        f->fmt.pix.bytesperline =
                (f->fmt.pix.width * fmt->depth) >> 3;
        f->fmt.pix.sizeimage =
@@ -903,53 +942,6 @@ static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
        return 0;
 }
 
-/* precalculate color bar values to speed up rendering */
-static void precalculate_bars(struct vivi_fh *fh)
-{
-       struct vivi_dev *dev = fh->dev;
-       unsigned char r, g, b;
-       int k, is_yuv;
-
-       fh->input = dev->input;
-
-       for (k = 0; k < 8; k++) {
-               r = bars[fh->input].bar[k][0];
-               g = bars[fh->input].bar[k][1];
-               b = bars[fh->input].bar[k][2];
-               is_yuv = 0;
-
-               switch (fh->fmt->fourcc) {
-               case V4L2_PIX_FMT_YUYV:
-               case V4L2_PIX_FMT_UYVY:
-                       is_yuv = 1;
-                       break;
-               case V4L2_PIX_FMT_RGB565:
-               case V4L2_PIX_FMT_RGB565X:
-                       r >>= 3;
-                       g >>= 2;
-                       b >>= 3;
-                       break;
-               case V4L2_PIX_FMT_RGB555:
-               case V4L2_PIX_FMT_RGB555X:
-                       r >>= 3;
-                       g >>= 3;
-                       b >>= 3;
-                       break;
-               }
-
-               if (is_yuv) {
-                       fh->bars[k][0] = TO_Y(r, g, b); /* Luma */
-                       fh->bars[k][1] = TO_U(r, g, b); /* Cb */
-                       fh->bars[k][2] = TO_V(r, g, b); /* Cr */
-               } else {
-                       fh->bars[k][0] = r;
-                       fh->bars[k][1] = g;
-                       fh->bars[k][2] = b;
-               }
-       }
-
-}
-
 /*FIXME: This seems to be generic enough to be at videodev2 */
 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
                                        struct v4l2_format *f)
@@ -975,8 +967,6 @@ static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
        fh->vb_vidq.field = f->fmt.pix.field;
        fh->type          = f->type;
 
-       precalculate_bars(fh);
-
        ret = 0;
 out:
        mutex_unlock(&q->vb_lock);
@@ -1108,12 +1098,14 @@ static int vidioc_queryctrl(struct file *file, void *priv,
 static int vidioc_g_ctrl(struct file *file, void *priv,
                         struct v4l2_control *ctrl)
 {
+       struct vivi_fh *fh = priv;
+       struct vivi_dev *dev = fh->dev;
        int i;
 
        for (i = 0; i < ARRAY_SIZE(vivi_qctrl); i++)
                if (ctrl->id == vivi_qctrl[i].id) {
-                       ctrl->value = qctl_regs[i];
-                       return (0);
+                       ctrl->value = dev->qctl_regs[i];
+                       return 0;
                }
 
        return -EINVAL;
@@ -1121,16 +1113,18 @@ static int vidioc_g_ctrl(struct file *file, void *priv,
 static int vidioc_s_ctrl(struct file *file, void *priv,
                                struct v4l2_control *ctrl)
 {
+       struct vivi_fh *fh = priv;
+       struct vivi_dev *dev = fh->dev;
        int i;
 
        for (i = 0; i < ARRAY_SIZE(vivi_qctrl); i++)
                if (ctrl->id == vivi_qctrl[i].id) {
-                       if (ctrl->value < vivi_qctrl[i].minimum
-                           || ctrl->value > vivi_qctrl[i].maximum) {
-                                       return (-ERANGE);
-                               }
-                       qctl_regs[i] = ctrl->value;
-                       return (0);
+                       if (ctrl->value < vivi_qctrl[i].minimum ||
+                           ctrl->value > vivi_qctrl[i].maximum) {
+                               return -ERANGE;
+                       }
+                       dev->qctl_regs[i] = ctrl->value;
+                       return 0;
                }
        return -EINVAL;
 }
@@ -1143,7 +1137,6 @@ static int vivi_open(struct file *file)
 {
        struct vivi_dev *dev = video_drvdata(file);
        struct vivi_fh *fh = NULL;
-       int i;
        int retval = 0;
 
        mutex_lock(&dev->mutex);
@@ -1155,7 +1148,8 @@ static int vivi_open(struct file *file)
                return -EBUSY;
        }
 
-       dprintk(dev, 1, "open /dev/video%d type=%s users=%d\n", dev->vfd->num,
+       dprintk(dev, 1, "open %s type=%s users=%d\n",
+               video_device_node_name(dev->vfd),
                v4l2_type_names[V4L2_BUF_TYPE_VIDEO_CAPTURE], dev->users);
 
        /* allocate + initialize per filehandle data */
@@ -1177,10 +1171,6 @@ static int vivi_open(struct file *file)
        fh->width    = 640;
        fh->height   = 480;
 
-       /* Put all controls at a sane state */
-       for (i = 0; i < ARRAY_SIZE(vivi_qctrl); i++)
-               qctl_regs[i] = vivi_qctrl[i].default_value;
-
        /* Resets frame counters */
        dev->h = 0;
        dev->m = 0;
@@ -1232,8 +1222,7 @@ static int vivi_close(struct file *file)
        struct vivi_fh         *fh = file->private_data;
        struct vivi_dev *dev       = fh->dev;
        struct vivi_dmaqueue *vidq = &dev->vidq;
-
-       int minor = video_devdata(file)->minor;
+       struct video_device  *vdev = video_devdata(file);
 
        vivi_stop_thread(vidq);
        videobuf_stop(&fh->vb_vidq);
@@ -1245,8 +1234,8 @@ static int vivi_close(struct file *file)
        dev->users--;
        mutex_unlock(&dev->mutex);
 
-       dprintk(dev, 1, "close called (minor=%d, users=%d)\n",
-               minor, dev->users);
+       dprintk(dev, 1, "close called (dev=%s, users=%d)\n",
+               video_device_node_name(vdev), dev->users);
 
        return 0;
 }
@@ -1307,7 +1296,6 @@ static struct video_device vivi_template = {
        .name           = "vivi",
        .fops           = &vivi_fops,
        .ioctl_ops      = &vivi_ioctl_ops,
-       .minor          = -1,
        .release        = video_device_release,
 
        .tvnorms              = V4L2_STD_525_60,
@@ -1328,8 +1316,8 @@ static int vivi_release(void)
                list_del(list);
                dev = list_entry(list, struct vivi_dev, vivi_devlist);
 
-               v4l2_info(&dev->v4l2_dev, "unregistering /dev/video%d\n",
-                       dev->vfd->num);
+               v4l2_info(&dev->v4l2_dev, "unregistering %s\n",
+                       video_device_node_name(dev->vfd));
                video_unregister_device(dev->vfd);
                v4l2_device_unregister(&dev->v4l2_dev);
                kfree(dev);
@@ -1338,18 +1326,18 @@ static int vivi_release(void)
        return 0;
 }
 
-static int __init vivi_create_instance(int i)
+static int __init vivi_create_instance(int inst)
 {
        struct vivi_dev *dev;
        struct video_device *vfd;
-       int ret;
+       int ret, i;
 
        dev = kzalloc(sizeof(*dev), GFP_KERNEL);
        if (!dev)
                return -ENOMEM;
 
        snprintf(dev->v4l2_dev.name, sizeof(dev->v4l2_dev.name),
-                       "%s-%03d", VIVI_MODULE_NAME, i);
+                       "%s-%03d", VIVI_MODULE_NAME, inst);
        ret = v4l2_device_register(NULL, &dev->v4l2_dev);
        if (ret)
                goto free_dev;
@@ -1368,6 +1356,7 @@ static int __init vivi_create_instance(int i)
                goto unreg_dev;
 
        *vfd = vivi_template;
+       vfd->debug = debug;
 
        ret = video_register_device(vfd, VFL_TYPE_GRABBER, video_nr);
        if (ret < 0)
@@ -1375,18 +1364,19 @@ static int __init vivi_create_instance(int i)
 
        video_set_drvdata(vfd, dev);
 
+       /* Set all controls to their default value. */
+       for (i = 0; i < ARRAY_SIZE(vivi_qctrl); i++)
+               dev->qctl_regs[i] = vivi_qctrl[i].default_value;
+
        /* Now that everything is fine, let's add it to device list */
        list_add_tail(&dev->vivi_devlist, &vivi_devlist);
 
-       snprintf(vfd->name, sizeof(vfd->name), "%s (%i)",
-                       vivi_template.name, vfd->num);
-
-       if (video_nr >= 0)
+       if (video_nr != -1)
                video_nr++;
 
        dev->vfd = vfd;
-       v4l2_info(&dev->v4l2_dev, "V4L2 device registered as /dev/video%d\n",
-                       vfd->num);
+       v4l2_info(&dev->v4l2_dev, "V4L2 device registered as %s\n",
+                 video_device_node_name(vfd));
        return 0;
 
 rel_vdev:
@@ -1406,7 +1396,7 @@ free_dev:
  */
 static int __init vivi_init(void)
 {
-       int ret, i;
+       int ret = 0, i;
 
        if (n_devs <= 0)
                n_devs = 1;