Update to 3.4-final.
[linux-flexiantxendom0-3.2.10.git] / drivers / media / video / s5p-fimc / fimc-capture.c
index a9e9653..7e9b2c6 100644 (file)
@@ -246,28 +246,37 @@ int fimc_capture_resume(struct fimc_dev *fimc)
 
 }
 
-static unsigned int get_plane_size(struct fimc_frame *fr, unsigned int plane)
-{
-       if (!fr || plane >= fr->fmt->memplanes)
-               return 0;
-       return fr->f_width * fr->f_height * fr->fmt->depth[plane] / 8;
-}
-
-static int queue_setup(struct vb2_queue *vq,  const struct v4l2_format *pfmt,
+static int queue_setup(struct vb2_queue *vq, const struct v4l2_format *pfmt,
                       unsigned int *num_buffers, unsigned int *num_planes,
                       unsigned int sizes[], void *allocators[])
 {
+       const struct v4l2_pix_format_mplane *pixm = NULL;
        struct fimc_ctx *ctx = vq->drv_priv;
-       struct fimc_fmt *fmt = ctx->d_frame.fmt;
+       struct fimc_frame *frame = &ctx->d_frame;
+       struct fimc_fmt *fmt = frame->fmt;
+       unsigned long wh;
        int i;
 
-       if (!fmt)
+       if (pfmt) {
+               pixm = &pfmt->fmt.pix_mp;
+               fmt = fimc_find_format(&pixm->pixelformat, NULL,
+                                      FMT_FLAGS_CAM | FMT_FLAGS_M2M, -1);
+               wh = pixm->width * pixm->height;
+       } else {
+               wh = frame->f_width * frame->f_height;
+       }
+
+       if (fmt == NULL)
                return -EINVAL;
 
        *num_planes = fmt->memplanes;
 
        for (i = 0; i < fmt->memplanes; i++) {
-               sizes[i] = get_plane_size(&ctx->d_frame, i);
+               unsigned int size = (wh * fmt->depth[i]) / 8;
+               if (pixm)
+                       sizes[i] = max(size, pixm->plane_fmt[i].sizeimage);
+               else
+                       sizes[i] = size;
                allocators[i] = ctx->fimc_dev->alloc_ctx;
        }
 
@@ -1019,52 +1028,117 @@ static int fimc_cap_dqbuf(struct file *file, void *priv,
        return vb2_dqbuf(&fimc->vid_cap.vbq, buf, file->f_flags & O_NONBLOCK);
 }
 
-static int fimc_cap_cropcap(struct file *file, void *fh,
-                           struct v4l2_cropcap *cr)
+static int fimc_cap_create_bufs(struct file *file, void *priv,
+                               struct v4l2_create_buffers *create)
 {
        struct fimc_dev *fimc = video_drvdata(file);
-       struct fimc_frame *f = &fimc->vid_cap.ctx->s_frame;
 
-       if (cr->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
-               return -EINVAL;
+       return vb2_create_bufs(&fimc->vid_cap.vbq, create);
+}
 
-       cr->bounds.left         = 0;
-       cr->bounds.top          = 0;
-       cr->bounds.width        = f->o_width;
-       cr->bounds.height       = f->o_height;
-       cr->defrect             = cr->bounds;
+static int fimc_cap_prepare_buf(struct file *file, void *priv,
+                               struct v4l2_buffer *b)
+{
+       struct fimc_dev *fimc = video_drvdata(file);
 
-       return 0;
+       return vb2_prepare_buf(&fimc->vid_cap.vbq, b);
 }
 
-static int fimc_cap_g_crop(struct file *file, void *fh, struct v4l2_crop *cr)
+static int fimc_cap_g_selection(struct file *file, void *fh,
+                               struct v4l2_selection *s)
 {
        struct fimc_dev *fimc = video_drvdata(file);
-       struct fimc_frame *f = &fimc->vid_cap.ctx->s_frame;
+       struct fimc_ctx *ctx = fimc->vid_cap.ctx;
+       struct fimc_frame *f = &ctx->s_frame;
 
-       cr->c.left      = f->offs_h;
-       cr->c.top       = f->offs_v;
-       cr->c.width     = f->width;
-       cr->c.height    = f->height;
+       if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
+               return -EINVAL;
 
-       return 0;
+       switch (s->target) {
+       case V4L2_SEL_TGT_COMPOSE_DEFAULT:
+       case V4L2_SEL_TGT_COMPOSE_BOUNDS:
+               f = &ctx->d_frame;
+       case V4L2_SEL_TGT_CROP_BOUNDS:
+       case V4L2_SEL_TGT_CROP_DEFAULT:
+               s->r.left = 0;
+               s->r.top = 0;
+               s->r.width = f->o_width;
+               s->r.height = f->o_height;
+               return 0;
+
+       case V4L2_SEL_TGT_COMPOSE_ACTIVE:
+               f = &ctx->d_frame;
+       case V4L2_SEL_TGT_CROP_ACTIVE:
+               s->r.left = f->offs_h;
+               s->r.top = f->offs_v;
+               s->r.width = f->width;
+               s->r.height = f->height;
+               return 0;
+       }
+
+       return -EINVAL;
+}
+
+/* Return 1 if rectangle a is enclosed in rectangle b, or 0 otherwise. */
+int enclosed_rectangle(struct v4l2_rect *a, struct v4l2_rect *b)
+{
+       if (a->left < b->left || a->top < b->top)
+               return 0;
+       if (a->left + a->width > b->left + b->width)
+               return 0;
+       if (a->top + a->height > b->top + b->height)
+               return 0;
+
+       return 1;
 }
 
-static int fimc_cap_s_crop(struct file *file, void *fh, struct v4l2_crop *cr)
+static int fimc_cap_s_selection(struct file *file, void *fh,
+                               struct v4l2_selection *s)
 {
        struct fimc_dev *fimc = video_drvdata(file);
        struct fimc_ctx *ctx = fimc->vid_cap.ctx;
-       struct fimc_frame *ff;
+       struct v4l2_rect rect = s->r;
+       struct fimc_frame *f;
        unsigned long flags;
+       unsigned int pad;
 
-       fimc_capture_try_crop(ctx, &cr->c, FIMC_SD_PAD_SINK);
-       ff = &ctx->s_frame;
+       if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
+               return -EINVAL;
+
+       switch (s->target) {
+       case V4L2_SEL_TGT_COMPOSE_DEFAULT:
+       case V4L2_SEL_TGT_COMPOSE_BOUNDS:
+       case V4L2_SEL_TGT_COMPOSE_ACTIVE:
+               f = &ctx->d_frame;
+               pad = FIMC_SD_PAD_SOURCE;
+               break;
+       case V4L2_SEL_TGT_CROP_BOUNDS:
+       case V4L2_SEL_TGT_CROP_DEFAULT:
+       case V4L2_SEL_TGT_CROP_ACTIVE:
+               f = &ctx->s_frame;
+               pad = FIMC_SD_PAD_SINK;
+               break;
+       default:
+               return -EINVAL;
+       }
+
+       fimc_capture_try_crop(ctx, &rect, pad);
 
+       if (s->flags & V4L2_SEL_FLAG_LE &&
+           !enclosed_rectangle(&rect, &s->r))
+               return -ERANGE;
+
+       if (s->flags & V4L2_SEL_FLAG_GE &&
+           !enclosed_rectangle(&s->r, &rect))
+               return -ERANGE;
+
+       s->r = rect;
        spin_lock_irqsave(&fimc->slock, flags);
-       set_frame_crop(ff, cr->c.left, cr->c.top, cr->c.width, cr->c.height);
-       set_bit(ST_CAPT_APPLY_CFG, &fimc->state);
+       set_frame_crop(f, s->r.left, s->r.top, s->r.width,
+                      s->r.height);
        spin_unlock_irqrestore(&fimc->slock, flags);
 
+       set_bit(ST_CAPT_APPLY_CFG, &fimc->state);
        return 0;
 }
 
@@ -1082,12 +1156,14 @@ static const struct v4l2_ioctl_ops fimc_capture_ioctl_ops = {
        .vidioc_qbuf                    = fimc_cap_qbuf,
        .vidioc_dqbuf                   = fimc_cap_dqbuf,
 
+       .vidioc_prepare_buf             = fimc_cap_prepare_buf,
+       .vidioc_create_bufs             = fimc_cap_create_bufs,
+
        .vidioc_streamon                = fimc_cap_streamon,
        .vidioc_streamoff               = fimc_cap_streamoff,
 
-       .vidioc_g_crop                  = fimc_cap_g_crop,
-       .vidioc_s_crop                  = fimc_cap_s_crop,
-       .vidioc_cropcap                 = fimc_cap_cropcap,
+       .vidioc_g_selection             = fimc_cap_g_selection,
+       .vidioc_s_selection             = fimc_cap_s_selection,
 
        .vidioc_enum_input              = fimc_cap_enum_input,
        .vidioc_s_input                 = fimc_cap_s_input,
@@ -1316,7 +1392,7 @@ static int fimc_subdev_set_crop(struct v4l2_subdev *sd,
        fimc_capture_try_crop(ctx, r, crop->pad);
 
        if (crop->which == V4L2_SUBDEV_FORMAT_TRY) {
-               mutex_lock(&fimc->lock);
+               mutex_unlock(&fimc->lock);
                *v4l2_subdev_get_try_crop(fh, crop->pad) = *r;
                return 0;
        }