V4L/DVB (8571): gspca: Don't use CONFIG_VIDEO_ADV_DEBUG as a compile option.
[safe/jmp/linux-2.6] / drivers / media / video / soc_camera.c
index 904e9df..b6be5ee 100644 (file)
@@ -25,7 +25,9 @@
 #include <linux/vmalloc.h>
 
 #include <media/v4l2-common.h>
+#include <media/v4l2-ioctl.h>
 #include <media/v4l2-dev.h>
+#include <media/videobuf-core.h>
 #include <media/soc_camera.h>
 
 static LIST_HEAD(hosts);
@@ -38,13 +40,13 @@ format_by_fourcc(struct soc_camera_device *icd, unsigned int fourcc)
 {
        unsigned int i;
 
-       for (i = 0; i < icd->ops->num_formats; i++)
-               if (icd->ops->formats[i].fourcc == fourcc)
-                       return icd->ops->formats + i;
+       for (i = 0; i < icd->num_formats; i++)
+               if (icd->formats[i].fourcc == fourcc)
+                       return icd->formats + i;
        return NULL;
 }
 
-static int soc_camera_try_fmt_cap(struct file *file, void *priv,
+static int soc_camera_try_fmt_vid_cap(struct file *file, void *priv,
                                  struct v4l2_format *f)
 {
        struct soc_camera_file *icf = file->private_data;
@@ -75,12 +77,13 @@ static int soc_camera_try_fmt_cap(struct file *file, void *priv,
                return -EINVAL;
        }
 
-       /* limit to host capabilities */
-       ret = ici->try_fmt_cap(ici, f);
+       /* test physical bus parameters */
+       ret = ici->ops->try_bus_param(icd, f->fmt.pix.pixelformat);
+       if (ret)
+               return ret;
 
-       /* limit to sensor capabilities */
-       if (!ret)
-               ret = icd->ops->try_fmt_cap(icd, f);
+       /* limit format to hardware capabilities */
+       ret = ici->ops->try_fmt_cap(icd, f);
 
        /* calculate missing fields */
        f->fmt.pix.field = field;
@@ -136,15 +139,13 @@ static int soc_camera_reqbufs(struct file *file, void *priv,
 
        WARN_ON(priv != file->private_data);
 
-       dev_dbg(&icd->dev, "%s: %d\n", __FUNCTION__, p->memory);
+       dev_dbg(&icd->dev, "%s: %d\n", __func__, p->memory);
 
        ret = videobuf_reqbufs(&icf->vb_vidq, p);
        if (ret < 0)
                return ret;
 
-       return ici->reqbufs(icf, p);
-
-       return ret;
+       return ici->ops->reqbufs(icf, p);
 }
 
 static int soc_camera_querybuf(struct file *file, void *priv,
@@ -179,11 +180,9 @@ static int soc_camera_dqbuf(struct file *file, void *priv,
 
 static int soc_camera_open(struct inode *inode, struct file *file)
 {
-       struct video_device *vdev = video_devdata(file);
-       struct soc_camera_device *icd = container_of(vdev->dev,
-                                            struct soc_camera_device, dev);
-       struct soc_camera_host *ici =
-               to_soc_camera_host(icd->dev.parent);
+       struct video_device *vdev;
+       struct soc_camera_device *icd;
+       struct soc_camera_host *ici;
        struct soc_camera_file *icf;
        int ret;
 
@@ -191,7 +190,12 @@ static int soc_camera_open(struct inode *inode, struct file *file)
        if (!icf)
                return -ENOMEM;
 
-       icf->icd = icd;
+       /* Protect against icd->remove() until we module_get() both drivers. */
+       mutex_lock(&video_lock);
+
+       vdev = video_devdata(file);
+       icd = container_of(vdev->parent, struct soc_camera_device, dev);
+       ici = to_soc_camera_host(icd->dev.parent);
 
        if (!try_module_get(icd->ops->owner)) {
                dev_err(&icd->dev, "Couldn't lock sensor driver.\n");
@@ -199,26 +203,41 @@ static int soc_camera_open(struct inode *inode, struct file *file)
                goto emgd;
        }
 
-       if (!try_module_get(ici->owner)) {
+       if (!try_module_get(ici->ops->owner)) {
                dev_err(&icd->dev, "Couldn't lock capture bus driver.\n");
                ret = -EINVAL;
                goto emgi;
        }
 
+       icf->icd = icd;
+       icd->use_count++;
+
+       /* Now we really have to activate the camera */
+       if (icd->use_count == 1) {
+               ret = ici->ops->add(icd);
+               if (ret < 0) {
+                       dev_err(&icd->dev, "Couldn't activate the camera: %d\n", ret);
+                       icd->use_count--;
+                       goto eiciadd;
+               }
+       }
+
+       mutex_unlock(&video_lock);
+
        file->private_data = icf;
        dev_dbg(&icd->dev, "camera device open\n");
 
-       /* We must pass NULL as dev pointer, then all pci_* dma operations
-        * transform to normal dma_* ones. Do we need an irqlock? */
-       videobuf_queue_pci_init(&icf->vb_vidq, ici->vbq_ops, NULL, NULL,
-                               V4L2_BUF_TYPE_VIDEO_CAPTURE, V4L2_FIELD_NONE,
-                               ici->msize, icd);
+       ici->ops->init_videobuf(&icf->vb_vidq, icd);
 
        return 0;
 
+       /* All errors are entered with the video_lock held */
+eiciadd:
+       module_put(ici->ops->owner);
 emgi:
        module_put(icd->ops->owner);
 emgd:
+       mutex_unlock(&video_lock);
        vfree(icf);
        return ret;
 }
@@ -230,16 +249,22 @@ static int soc_camera_close(struct inode *inode, struct file *file)
        struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
        struct video_device *vdev = icd->vdev;
 
+       mutex_lock(&video_lock);
+       icd->use_count--;
+       if (!icd->use_count)
+               ici->ops->remove(icd);
        module_put(icd->ops->owner);
-       module_put(ici->owner);
-       vfree(file->private_data);
+       module_put(ici->ops->owner);
+       mutex_unlock(&video_lock);
 
-       dev_dbg(vdev->dev, "camera device close\n");
+       vfree(icf);
+
+       dev_dbg(vdev->parent, "camera device close\n");
 
        return 0;
 }
 
-static int soc_camera_read(struct file *file, char __user *buf,
+static ssize_t soc_camera_read(struct file *file, char __user *buf,
                           size_t count, loff_t *ppos)
 {
        struct soc_camera_file *icf = file->private_data;
@@ -247,7 +272,7 @@ static int soc_camera_read(struct file *file, char __user *buf,
        struct video_device *vdev = icd->vdev;
        int err = -EINVAL;
 
-       dev_err(vdev->dev, "camera device read not implemented\n");
+       dev_err(vdev->parent, "camera device read not implemented\n");
 
        return err;
 }
@@ -282,7 +307,7 @@ static unsigned int soc_camera_poll(struct file *file, poll_table *pt)
                return POLLERR;
        }
 
-       return ici->poll(file, pt);
+       return ici->ops->poll(file, pt);
 }
 
 
@@ -298,7 +323,7 @@ static struct file_operations soc_camera_fops = {
 };
 
 
-static int soc_camera_s_fmt_cap(struct file *file, void *priv,
+static int soc_camera_s_fmt_vid_cap(struct file *file, void *priv,
                                struct v4l2_format *f)
 {
        struct soc_camera_file *icf = file->private_data;
@@ -315,10 +340,10 @@ static int soc_camera_s_fmt_cap(struct file *file, void *priv,
        if (!data_fmt)
                return -EINVAL;
 
-       /* cached_datawidth may be further adjusted by the ici */
-       icd->cached_datawidth = data_fmt->depth;
+       /* buswidth may be further adjusted by the ici */
+       icd->buswidth = data_fmt->depth;
 
-       ret = soc_camera_try_fmt_cap(file, icf, f);
+       ret = soc_camera_try_fmt_vid_cap(file, icf, f);
        if (ret < 0)
                return ret;
 
@@ -326,25 +351,26 @@ static int soc_camera_s_fmt_cap(struct file *file, void *priv,
        rect.top        = icd->y_current;
        rect.width      = f->fmt.pix.width;
        rect.height     = f->fmt.pix.height;
-       ret = ici->set_capture_format(icd, f->fmt.pix.pixelformat, &rect);
+       ret = ici->ops->set_fmt_cap(icd, f->fmt.pix.pixelformat, &rect);
+       if (ret < 0)
+               return ret;
 
-       if (!ret) {
-               icd->current_fmt        = data_fmt;
-               icd->width              = rect.width;
-               icd->height             = rect.height;
-               icf->vb_vidq.field      = f->fmt.pix.field;
-               if (V4L2_BUF_TYPE_VIDEO_CAPTURE != f->type)
-                       dev_warn(&icd->dev, "Attention! Wrong buf-type %d\n",
-                                f->type);
-
-               dev_dbg(&icd->dev, "set width: %d height: %d\n",
-                      icd->width, icd->height);
-       }
+       icd->current_fmt        = data_fmt;
+       icd->width              = rect.width;
+       icd->height             = rect.height;
+       icf->vb_vidq.field      = f->fmt.pix.field;
+       if (V4L2_BUF_TYPE_VIDEO_CAPTURE != f->type)
+               dev_warn(&icd->dev, "Attention! Wrong buf-type %d\n",
+                        f->type);
 
-       return ret;
+       dev_dbg(&icd->dev, "set width: %d height: %d\n",
+               icd->width, icd->height);
+
+       /* set physical bus parameters */
+       return ici->ops->set_bus_param(icd, f->fmt.pix.pixelformat);
 }
 
-static int soc_camera_enum_fmt_cap(struct file *file, void  *priv,
+static int soc_camera_enum_fmt_vid_cap(struct file *file, void  *priv,
                                   struct v4l2_fmtdesc *f)
 {
        struct soc_camera_file *icf = file->private_data;
@@ -353,17 +379,17 @@ static int soc_camera_enum_fmt_cap(struct file *file, void  *priv,
 
        WARN_ON(priv != file->private_data);
 
-       if (f->index >= icd->ops->num_formats)
+       if (f->index >= icd->num_formats)
                return -EINVAL;
 
-       format = &icd->ops->formats[f->index];
+       format = &icd->formats[f->index];
 
        strlcpy(f->description, format->name, sizeof(f->description));
        f->pixelformat = format->fourcc;
        return 0;
 }
 
-static int soc_camera_g_fmt_cap(struct file *file, void *priv,
+static int soc_camera_g_fmt_vid_cap(struct file *file, void *priv,
                                struct v4l2_format *f)
 {
        struct soc_camera_file *icf = file->private_data;
@@ -395,7 +421,7 @@ static int soc_camera_querycap(struct file *file, void  *priv,
        WARN_ON(priv != file->private_data);
 
        strlcpy(cap->driver, ici->drv_name, sizeof(cap->driver));
-       return ici->querycap(ici, cap);
+       return ici->ops->querycap(ici, cap);
 }
 
 static int soc_camera_streamon(struct file *file, void *priv,
@@ -406,7 +432,7 @@ static int soc_camera_streamon(struct file *file, void *priv,
 
        WARN_ON(priv != file->private_data);
 
-       dev_dbg(&icd->dev, "%s\n", __FUNCTION__);
+       dev_dbg(&icd->dev, "%s\n", __func__);
 
        if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
                return -EINVAL;
@@ -425,7 +451,7 @@ static int soc_camera_streamoff(struct file *file, void *priv,
 
        WARN_ON(priv != file->private_data);
 
-       dev_dbg(&icd->dev, "%s\n", __FUNCTION__);
+       dev_dbg(&icd->dev, "%s\n", __func__);
 
        if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
                return -EINVAL;
@@ -548,7 +574,7 @@ static int soc_camera_s_crop(struct file *file, void *fh,
        if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
                return -EINVAL;
 
-       ret = ici->set_capture_format(icd, 0, &a->c);
+       ret = ici->ops->set_fmt_cap(icd, 0, &a->c);
        if (!ret) {
                icd->width      = a->c.width;
                icd->height     = a->c.height;
@@ -670,17 +696,17 @@ static int soc_camera_probe(struct device *dev)
                to_soc_camera_host(icd->dev.parent);
        int ret;
 
-       if (!icd->probe)
+       if (!icd->ops->probe)
                return -ENODEV;
 
-       ret = ici->add(icd);
+       /* We only call ->add() here to activate and probe the camera.
+        * We shall ->remove() and deactivate it immediately afterwards. */
+       ret = ici->ops->add(icd);
        if (ret < 0)
                return ret;
 
-       ret = icd->probe(icd);
-       if (ret < 0)
-               ici->remove(icd);
-       else {
+       ret = icd->ops->probe(icd);
+       if (ret >= 0) {
                const struct v4l2_queryctrl *qctrl;
 
                qctrl = soc_camera_find_qctrl(icd->ops, V4L2_CID_GAIN);
@@ -689,6 +715,7 @@ static int soc_camera_probe(struct device *dev)
                icd->exposure = qctrl ? qctrl->default_value :
                        (unsigned short)~0;
        }
+       ici->ops->remove(icd);
 
        return ret;
 }
@@ -698,13 +725,9 @@ static int soc_camera_probe(struct device *dev)
 static int soc_camera_remove(struct device *dev)
 {
        struct soc_camera_device *icd = to_soc_camera_dev(dev);
-       struct soc_camera_host *ici =
-               to_soc_camera_host(icd->dev.parent);
-
-       if (icd->remove)
-               icd->remove(icd);
 
-       ici->remove(icd);
+       if (icd->ops->remove)
+               icd->ops->remove(icd);
 
        return 0;
 }
@@ -721,30 +744,20 @@ static struct device_driver ic_drv = {
        .owner  = THIS_MODULE,
 };
 
-/*
- * Image capture host - this is a host device, not a bus device, so,
- * no bus reference, no probing.
- */
-static struct class soc_camera_host_class = {
-       .owner          = THIS_MODULE,
-       .name           = "camera_host",
-};
-
 static void dummy_release(struct device *dev)
 {
 }
 
-int soc_camera_host_register(struct soc_camera_host *ici, struct module *owner)
+int soc_camera_host_register(struct soc_camera_host *ici)
 {
        int ret;
        struct soc_camera_host *ix;
 
-       if (!ici->vbq_ops || !ici->add || !ici->remove || !owner)
+       if (!ici->ops->init_videobuf || !ici->ops->add || !ici->ops->remove)
                return -EINVAL;
 
        /* Number might be equal to the platform device ID */
        sprintf(ici->dev.bus_id, "camera_host%d", ici->nr);
-       ici->dev.class = &soc_camera_host_class;
 
        mutex_lock(&list_lock);
        list_for_each_entry(ix, &hosts, list) {
@@ -757,7 +770,6 @@ int soc_camera_host_register(struct soc_camera_host *ici, struct module *owner)
        list_add_tail(&ici->list, &hosts);
        mutex_unlock(&list_lock);
 
-       ici->owner = owner;
        ici->dev.release = dummy_release;
 
        ret = device_register(&ici->dev);
@@ -791,7 +803,7 @@ void soc_camera_host_unregister(struct soc_camera_host *ici)
                if (icd->dev.parent == &ici->dev) {
                        device_unregister(&icd->dev);
                        /* Not before device_unregister(), .remove
-                        * needs parent to call ici->remove() */
+                        * needs parent to call ici->ops->remove() */
                        icd->dev.parent = NULL;
                        memset(&icd->dev.kobj, 0, sizeof(icd->dev.kobj));
                }
@@ -834,9 +846,6 @@ int soc_camera_device_register(struct soc_camera_device *icd)
 
        icd->dev.release = dummy_release;
 
-       if (icd->ops->get_datawidth)
-               icd->cached_datawidth = icd->ops->get_datawidth(icd);
-
        return scan_add_device(icd);
 }
 EXPORT_SYMBOL(soc_camera_device_register);
@@ -853,6 +862,35 @@ void soc_camera_device_unregister(struct soc_camera_device *icd)
 }
 EXPORT_SYMBOL(soc_camera_device_unregister);
 
+static const struct v4l2_ioctl_ops soc_camera_ioctl_ops = {
+       .vidioc_querycap         = soc_camera_querycap,
+       .vidioc_g_fmt_vid_cap    = soc_camera_g_fmt_vid_cap,
+       .vidioc_enum_fmt_vid_cap = soc_camera_enum_fmt_vid_cap,
+       .vidioc_s_fmt_vid_cap    = soc_camera_s_fmt_vid_cap,
+       .vidioc_enum_input       = soc_camera_enum_input,
+       .vidioc_g_input          = soc_camera_g_input,
+       .vidioc_s_input          = soc_camera_s_input,
+       .vidioc_s_std            = soc_camera_s_std,
+       .vidioc_reqbufs          = soc_camera_reqbufs,
+       .vidioc_try_fmt_vid_cap  = soc_camera_try_fmt_vid_cap,
+       .vidioc_querybuf         = soc_camera_querybuf,
+       .vidioc_qbuf             = soc_camera_qbuf,
+       .vidioc_dqbuf            = soc_camera_dqbuf,
+       .vidioc_streamon         = soc_camera_streamon,
+       .vidioc_streamoff        = soc_camera_streamoff,
+       .vidioc_queryctrl        = soc_camera_queryctrl,
+       .vidioc_g_ctrl           = soc_camera_g_ctrl,
+       .vidioc_s_ctrl           = soc_camera_s_ctrl,
+       .vidioc_cropcap          = soc_camera_cropcap,
+       .vidioc_g_crop           = soc_camera_g_crop,
+       .vidioc_s_crop           = soc_camera_s_crop,
+       .vidioc_g_chip_ident     = soc_camera_g_chip_ident,
+#ifdef CONFIG_VIDEO_ADV_DEBUG
+       .vidioc_g_register       = soc_camera_g_register,
+       .vidioc_s_register       = soc_camera_s_register,
+#endif
+};
+
 int soc_camera_video_start(struct soc_camera_device *icd)
 {
        struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
@@ -869,45 +907,19 @@ int soc_camera_video_start(struct soc_camera_device *icd)
 
        strlcpy(vdev->name, ici->drv_name, sizeof(vdev->name));
        /* Maybe better &ici->dev */
-       vdev->dev               = &icd->dev;
-       vdev->type              = VID_TYPE_CAPTURE;
+       vdev->parent            = &icd->dev;
        vdev->current_norm      = V4L2_STD_UNKNOWN;
        vdev->fops              = &soc_camera_fops;
+       vdev->ioctl_ops         = &soc_camera_ioctl_ops;
        vdev->release           = video_device_release;
        vdev->minor             = -1;
        vdev->tvnorms           = V4L2_STD_UNKNOWN,
-       vdev->vidioc_querycap   = soc_camera_querycap;
-       vdev->vidioc_g_fmt_cap  = soc_camera_g_fmt_cap;
-       vdev->vidioc_enum_fmt_cap = soc_camera_enum_fmt_cap;
-       vdev->vidioc_s_fmt_cap  = soc_camera_s_fmt_cap;
-       vdev->vidioc_enum_input = soc_camera_enum_input;
-       vdev->vidioc_g_input    = soc_camera_g_input;
-       vdev->vidioc_s_input    = soc_camera_s_input;
-       vdev->vidioc_s_std      = soc_camera_s_std;
-       vdev->vidioc_reqbufs    = soc_camera_reqbufs;
-       vdev->vidioc_try_fmt_cap = soc_camera_try_fmt_cap;
-       vdev->vidioc_querybuf   = soc_camera_querybuf;
-       vdev->vidioc_qbuf       = soc_camera_qbuf;
-       vdev->vidioc_dqbuf      = soc_camera_dqbuf;
-       vdev->vidioc_streamon   = soc_camera_streamon;
-       vdev->vidioc_streamoff  = soc_camera_streamoff;
-       vdev->vidioc_queryctrl  = soc_camera_queryctrl;
-       vdev->vidioc_g_ctrl     = soc_camera_g_ctrl;
-       vdev->vidioc_s_ctrl     = soc_camera_s_ctrl;
-       vdev->vidioc_cropcap    = soc_camera_cropcap;
-       vdev->vidioc_g_crop     = soc_camera_g_crop;
-       vdev->vidioc_s_crop     = soc_camera_s_crop;
-       vdev->vidioc_g_chip_ident = soc_camera_g_chip_ident;
-#ifdef CONFIG_VIDEO_ADV_DEBUG
-       vdev->vidioc_g_register = soc_camera_g_register;
-       vdev->vidioc_s_register = soc_camera_s_register;
-#endif
 
-       icd->current_fmt = &icd->ops->formats[0];
+       icd->current_fmt = &icd->formats[0];
 
        err = video_register_device(vdev, VFL_TYPE_GRABBER, vdev->minor);
        if (err < 0) {
-               dev_err(vdev->dev, "video_register_device failed\n");
+               dev_err(vdev->parent, "video_register_device failed\n");
                goto evidregd;
        }
        icd->vdev = vdev;
@@ -925,7 +937,7 @@ void soc_camera_video_stop(struct soc_camera_device *icd)
 {
        struct video_device *vdev = icd->vdev;
 
-       dev_dbg(&icd->dev, "%s\n", __FUNCTION__);
+       dev_dbg(&icd->dev, "%s\n", __func__);
 
        if (!icd->dev.parent || !vdev)
                return;
@@ -945,14 +957,9 @@ static int __init soc_camera_init(void)
        ret = driver_register(&ic_drv);
        if (ret)
                goto edrvr;
-       ret = class_register(&soc_camera_host_class);
-       if (ret)
-               goto eclr;
 
        return 0;
 
-eclr:
-       driver_unregister(&ic_drv);
 edrvr:
        bus_unregister(&soc_camera_bus_type);
        return ret;
@@ -960,7 +967,6 @@ edrvr:
 
 static void __exit soc_camera_exit(void)
 {
-       class_unregister(&soc_camera_host_class);
        driver_unregister(&ic_drv);
        bus_unregister(&soc_camera_bus_type);
 }