0xf9, 0xfa
 };
 
+static inline struct s5p_jpeg_ctx *fh_to_ctx(struct v4l2_fh *fh)
+{
+       return container_of(fh, struct s5p_jpeg_ctx, fh);
+}
+
 static inline void jpeg_set_qtbl(void __iomem *regs, const unsigned char *qtbl,
                   unsigned long tab, int len)
 {
        struct video_device *vfd = video_devdata(file);
        struct s5p_jpeg_ctx *ctx;
        struct s5p_jpeg_fmt *out_fmt;
+       int ret = 0;
 
        ctx = kzalloc(sizeof *ctx, GFP_KERNEL);
        if (!ctx)
                return -ENOMEM;
 
-       file->private_data = ctx;
+       v4l2_fh_init(&ctx->fh, vfd);
+       file->private_data = &ctx->fh;
+       v4l2_fh_add(&ctx->fh);
+
        ctx->jpeg = jpeg;
        if (vfd == jpeg->vfd_encoder) {
                ctx->mode = S5P_JPEG_ENCODE;
 
        ctx->m2m_ctx = v4l2_m2m_ctx_init(jpeg->m2m_dev, ctx, queue_init);
        if (IS_ERR(ctx->m2m_ctx)) {
-               int err = PTR_ERR(ctx->m2m_ctx);
-               kfree(ctx);
-               return err;
+               ret = PTR_ERR(ctx->m2m_ctx);
+               goto error;
        }
 
        ctx->out_q.fmt = out_fmt;
        ctx->cap_q.fmt = s5p_jpeg_find_format(ctx->mode, V4L2_PIX_FMT_YUYV);
-
        return 0;
+
+error:
+       v4l2_fh_del(&ctx->fh);
+       v4l2_fh_exit(&ctx->fh);
+       kfree(ctx);
+       return ret;
 }
 
 static int s5p_jpeg_release(struct file *file)
 {
-       struct s5p_jpeg_ctx *ctx = file->private_data;
+       struct s5p_jpeg_ctx *ctx = fh_to_ctx(file->private_data);
 
        v4l2_m2m_ctx_release(ctx->m2m_ctx);
+       v4l2_fh_del(&ctx->fh);
+       v4l2_fh_exit(&ctx->fh);
        kfree(ctx);
 
        return 0;
 static unsigned int s5p_jpeg_poll(struct file *file,
                                 struct poll_table_struct *wait)
 {
-       struct s5p_jpeg_ctx *ctx = file->private_data;
+       struct s5p_jpeg_ctx *ctx = fh_to_ctx(file->private_data);
 
        return v4l2_m2m_poll(file, ctx->m2m_ctx, wait);
 }
 
 static int s5p_jpeg_mmap(struct file *file, struct vm_area_struct *vma)
 {
-       struct s5p_jpeg_ctx *ctx = file->private_data;
+       struct s5p_jpeg_ctx *ctx = fh_to_ctx(file->private_data);
 
        return v4l2_m2m_mmap(file, ctx->m2m_ctx, vma);
 }
 static int s5p_jpeg_querycap(struct file *file, void *priv,
                           struct v4l2_capability *cap)
 {
-       struct s5p_jpeg_ctx *ctx = priv;
+       struct s5p_jpeg_ctx *ctx = fh_to_ctx(priv);
 
        if (ctx->mode == S5P_JPEG_ENCODE) {
                strlcpy(cap->driver, S5P_JPEG_M2M_NAME " encoder",
 static int s5p_jpeg_enum_fmt_vid_cap(struct file *file, void *priv,
                                   struct v4l2_fmtdesc *f)
 {
-       struct s5p_jpeg_ctx *ctx;
-
-       ctx = priv;
+       struct s5p_jpeg_ctx *ctx = fh_to_ctx(priv);
 
        if (ctx->mode == S5P_JPEG_ENCODE)
                return enum_fmt(formats_enc, NUM_FORMATS_ENC, f,
 static int s5p_jpeg_enum_fmt_vid_out(struct file *file, void *priv,
                                   struct v4l2_fmtdesc *f)
 {
-       struct s5p_jpeg_ctx *ctx;
-
-       ctx = priv;
+       struct s5p_jpeg_ctx *ctx = fh_to_ctx(priv);
 
        if (ctx->mode == S5P_JPEG_ENCODE)
                return enum_fmt(formats_enc, NUM_FORMATS_ENC, f,
        struct vb2_queue *vq;
        struct s5p_jpeg_q_data *q_data = NULL;
        struct v4l2_pix_format *pix = &f->fmt.pix;
-       struct s5p_jpeg_ctx *ct = priv;
+       struct s5p_jpeg_ctx *ct = fh_to_ctx(priv);
 
        vq = v4l2_m2m_get_vq(ct->m2m_ctx, f->type);
        if (!vq)
 static int s5p_jpeg_try_fmt_vid_cap(struct file *file, void *priv,
                                  struct v4l2_format *f)
 {
+       struct s5p_jpeg_ctx *ctx = fh_to_ctx(priv);
        struct s5p_jpeg_fmt *fmt;
-       struct s5p_jpeg_ctx *ctx = priv;
 
        fmt = s5p_jpeg_find_format(ctx->mode, f->fmt.pix.pixelformat);
        if (!fmt || !(fmt->types & MEM2MEM_CAPTURE)) {
 static int s5p_jpeg_try_fmt_vid_out(struct file *file, void *priv,
                                  struct v4l2_format *f)
 {
+       struct s5p_jpeg_ctx *ctx = fh_to_ctx(priv);
        struct s5p_jpeg_fmt *fmt;
-       struct s5p_jpeg_ctx *ctx = priv;
 
        fmt = s5p_jpeg_find_format(ctx->mode, f->fmt.pix.pixelformat);
        if (!fmt || !(fmt->types & MEM2MEM_OUTPUT)) {
        if (ret)
                return ret;
 
-       return s5p_jpeg_s_fmt(priv, f);
+       return s5p_jpeg_s_fmt(fh_to_ctx(priv), f);
 }
 
 static int s5p_jpeg_s_fmt_vid_out(struct file *file, void *priv,
        if (ret)
                return ret;
 
-       return s5p_jpeg_s_fmt(priv, f);
+       return s5p_jpeg_s_fmt(fh_to_ctx(priv), f);
 }
 
 static int s5p_jpeg_reqbufs(struct file *file, void *priv,
                          struct v4l2_requestbuffers *reqbufs)
 {
-       struct s5p_jpeg_ctx *ctx = priv;
+       struct s5p_jpeg_ctx *ctx = fh_to_ctx(priv);
 
        return v4l2_m2m_reqbufs(file, ctx->m2m_ctx, reqbufs);
 }
 static int s5p_jpeg_querybuf(struct file *file, void *priv,
                           struct v4l2_buffer *buf)
 {
-       struct s5p_jpeg_ctx *ctx = priv;
+       struct s5p_jpeg_ctx *ctx = fh_to_ctx(priv);
 
        return v4l2_m2m_querybuf(file, ctx->m2m_ctx, buf);
 }
 
 static int s5p_jpeg_qbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
 {
-       struct s5p_jpeg_ctx *ctx = priv;
+       struct s5p_jpeg_ctx *ctx = fh_to_ctx(priv);
 
        return v4l2_m2m_qbuf(file, ctx->m2m_ctx, buf);
 }
 static int s5p_jpeg_dqbuf(struct file *file, void *priv,
                          struct v4l2_buffer *buf)
 {
-       struct s5p_jpeg_ctx *ctx = priv;
+       struct s5p_jpeg_ctx *ctx = fh_to_ctx(priv);
 
        return v4l2_m2m_dqbuf(file, ctx->m2m_ctx, buf);
 }
 static int s5p_jpeg_streamon(struct file *file, void *priv,
                           enum v4l2_buf_type type)
 {
-       struct s5p_jpeg_ctx *ctx = priv;
+       struct s5p_jpeg_ctx *ctx = fh_to_ctx(priv);
 
        return v4l2_m2m_streamon(file, ctx->m2m_ctx, type);
 }
 static int s5p_jpeg_streamoff(struct file *file, void *priv,
                            enum v4l2_buf_type type)
 {
-       struct s5p_jpeg_ctx *ctx = priv;
+       struct s5p_jpeg_ctx *ctx = fh_to_ctx(priv);
 
        return v4l2_m2m_streamoff(file, ctx->m2m_ctx, type);
 }
 int s5p_jpeg_g_selection(struct file *file, void *priv,
                         struct v4l2_selection *s)
 {
-       struct s5p_jpeg_ctx *ctx = priv;
+       struct s5p_jpeg_ctx *ctx = fh_to_ctx(priv);
 
        if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT &&
            s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)