]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/staging/media/davinci_vpfe/vpfe_video.c
Merge tag 'v4.2-rc3' into next
[karo-tx-linux.git] / drivers / staging / media / davinci_vpfe / vpfe_video.c
1 /*
2  * Copyright (C) 2012 Texas Instruments Inc
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation version 2.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16  *
17  * Contributors:
18  *      Manjunath Hadli <manjunath.hadli@ti.com>
19  *      Prabhakar Lad <prabhakar.lad@ti.com>
20  */
21
22 #include <linux/module.h>
23 #include <linux/slab.h>
24
25 #include <media/v4l2-ioctl.h>
26
27 #include "vpfe.h"
28 #include "vpfe_mc_capture.h"
29
30 static int debug;
31
32 /* get v4l2 subdev pointer to external subdev which is active */
33 static struct media_entity *vpfe_get_input_entity
34                         (struct vpfe_video_device *video)
35 {
36         struct vpfe_device *vpfe_dev = video->vpfe_dev;
37         struct media_pad *remote;
38
39         remote = media_entity_remote_pad(&vpfe_dev->vpfe_isif.pads[0]);
40         if (remote == NULL) {
41                 pr_err("Invalid media connection to isif/ccdc\n");
42                 return NULL;
43         }
44         return remote->entity;
45 }
46
47 /* updates external subdev(sensor/decoder) which is active */
48 static int vpfe_update_current_ext_subdev(struct vpfe_video_device *video)
49 {
50         struct vpfe_device *vpfe_dev = video->vpfe_dev;
51         struct vpfe_config *vpfe_cfg;
52         struct v4l2_subdev *subdev;
53         struct media_pad *remote;
54         int i;
55
56         remote = media_entity_remote_pad(&vpfe_dev->vpfe_isif.pads[0]);
57         if (remote == NULL) {
58                 pr_err("Invalid media connection to isif/ccdc\n");
59                 return -EINVAL;
60         }
61
62         subdev = media_entity_to_v4l2_subdev(remote->entity);
63         vpfe_cfg = vpfe_dev->pdev->platform_data;
64         for (i = 0; i < vpfe_cfg->num_subdevs; i++) {
65                 if (!strcmp(vpfe_cfg->sub_devs[i].module_name, subdev->name)) {
66                         video->current_ext_subdev = &vpfe_cfg->sub_devs[i];
67                         break;
68                 }
69         }
70
71         /* if user not linked decoder/sensor to isif/ccdc */
72         if (i == vpfe_cfg->num_subdevs) {
73                 pr_err("Invalid media chain connection to isif/ccdc\n");
74                 return -EINVAL;
75         }
76         /* find the v4l2 subdev pointer */
77         for (i = 0; i < vpfe_dev->num_ext_subdevs; i++) {
78                 if (!strcmp(video->current_ext_subdev->module_name,
79                         vpfe_dev->sd[i]->name))
80                         video->current_ext_subdev->subdev = vpfe_dev->sd[i];
81         }
82         return 0;
83 }
84
85 /* get the subdev which is connected to the output video node */
86 static struct v4l2_subdev *
87 vpfe_video_remote_subdev(struct vpfe_video_device *video, u32 *pad)
88 {
89         struct media_pad *remote = media_entity_remote_pad(&video->pad);
90
91         if (remote == NULL || remote->entity->type != MEDIA_ENT_T_V4L2_SUBDEV)
92                 return NULL;
93         if (pad)
94                 *pad = remote->index;
95         return media_entity_to_v4l2_subdev(remote->entity);
96 }
97
98 /* get the format set at output pad of the adjacent subdev */
99 static int
100 __vpfe_video_get_format(struct vpfe_video_device *video,
101                         struct v4l2_format *format)
102 {
103         struct v4l2_subdev_format fmt;
104         struct v4l2_subdev *subdev;
105         struct media_pad *remote;
106         u32 pad;
107         int ret;
108
109         subdev = vpfe_video_remote_subdev(video, &pad);
110         if (subdev == NULL)
111                 return -EINVAL;
112
113         fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
114         remote = media_entity_remote_pad(&video->pad);
115         fmt.pad = remote->index;
116
117         ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &fmt);
118         if (ret == -ENOIOCTLCMD)
119                 return -EINVAL;
120
121         format->type = video->type;
122         /* convert mbus_format to v4l2_format */
123         v4l2_fill_pix_format(&format->fmt.pix, &fmt.format);
124         mbus_to_pix(&fmt.format, &format->fmt.pix);
125
126         return 0;
127 }
128
129 /* make a note of pipeline details */
130 static void vpfe_prepare_pipeline(struct vpfe_video_device *video)
131 {
132         struct media_entity *entity = &video->video_dev.entity;
133         struct media_device *mdev = entity->parent;
134         struct vpfe_pipeline *pipe = &video->pipe;
135         struct vpfe_video_device *far_end = NULL;
136         struct media_entity_graph graph;
137
138         pipe->input_num = 0;
139         pipe->output_num = 0;
140
141         if (video->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
142                 pipe->inputs[pipe->input_num++] = video;
143         else
144                 pipe->outputs[pipe->output_num++] = video;
145
146         mutex_lock(&mdev->graph_mutex);
147         media_entity_graph_walk_start(&graph, entity);
148         while ((entity = media_entity_graph_walk_next(&graph))) {
149                 if (entity == &video->video_dev.entity)
150                         continue;
151                 if (media_entity_type(entity) != MEDIA_ENT_T_DEVNODE)
152                         continue;
153                 far_end = to_vpfe_video(media_entity_to_video_device(entity));
154                 if (far_end->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
155                         pipe->inputs[pipe->input_num++] = far_end;
156                 else
157                         pipe->outputs[pipe->output_num++] = far_end;
158         }
159         mutex_unlock(&mdev->graph_mutex);
160 }
161
162 /* update pipe state selected by user */
163 static int vpfe_update_pipe_state(struct vpfe_video_device *video)
164 {
165         struct vpfe_pipeline *pipe = &video->pipe;
166         int ret;
167
168         vpfe_prepare_pipeline(video);
169
170         /* Find out if there is any input video
171           if yes, it is single shot.
172         */
173         if (pipe->input_num == 0) {
174                 pipe->state = VPFE_PIPELINE_STREAM_CONTINUOUS;
175                 ret = vpfe_update_current_ext_subdev(video);
176                 if (ret) {
177                         pr_err("Invalid external subdev\n");
178                         return ret;
179                 }
180         } else {
181                 pipe->state = VPFE_PIPELINE_STREAM_SINGLESHOT;
182         }
183         video->initialized = 1;
184         video->skip_frame_count = 1;
185         video->skip_frame_count_init = 1;
186         return 0;
187 }
188
189 /* checks wether pipeline is ready for enabling */
190 int vpfe_video_is_pipe_ready(struct vpfe_pipeline *pipe)
191 {
192         int i;
193
194         for (i = 0; i < pipe->input_num; i++)
195                 if (!pipe->inputs[i]->started ||
196                         pipe->inputs[i]->state != VPFE_VIDEO_BUFFER_QUEUED)
197                         return 0;
198         for (i = 0; i < pipe->output_num; i++)
199                 if (!pipe->outputs[i]->started ||
200                         pipe->outputs[i]->state != VPFE_VIDEO_BUFFER_QUEUED)
201                         return 0;
202         return 1;
203 }
204
205 /**
206  * Validate a pipeline by checking both ends of all links for format
207  * discrepancies.
208  *
209  * Return 0 if all formats match, or -EPIPE if at least one link is found with
210  * different formats on its two ends.
211  */
212 static int vpfe_video_validate_pipeline(struct vpfe_pipeline *pipe)
213 {
214         struct v4l2_subdev_format fmt_source;
215         struct v4l2_subdev_format fmt_sink;
216         struct v4l2_subdev *subdev;
217         struct media_pad *pad;
218         int ret;
219
220         /*
221          * Should not matter if it is output[0] or 1 as
222          * the general ideas is to traverse backwards and
223          * the fact that the out video node always has the
224          * format of the connected pad.
225          */
226         subdev = vpfe_video_remote_subdev(pipe->outputs[0], NULL);
227         if (subdev == NULL)
228                 return -EPIPE;
229
230         while (1) {
231                 /* Retrieve the sink format */
232                 pad = &subdev->entity.pads[0];
233                 if (!(pad->flags & MEDIA_PAD_FL_SINK))
234                         break;
235
236                 fmt_sink.which = V4L2_SUBDEV_FORMAT_ACTIVE;
237                 fmt_sink.pad = pad->index;
238                 ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL,
239                                        &fmt_sink);
240
241                 if (ret < 0 && ret != -ENOIOCTLCMD)
242                         return -EPIPE;
243
244                 /* Retrieve the source format */
245                 pad = media_entity_remote_pad(pad);
246                 if (pad == NULL ||
247                         pad->entity->type != MEDIA_ENT_T_V4L2_SUBDEV)
248                         break;
249
250                 subdev = media_entity_to_v4l2_subdev(pad->entity);
251
252                 fmt_source.which = V4L2_SUBDEV_FORMAT_ACTIVE;
253                 fmt_source.pad = pad->index;
254                 ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &fmt_source);
255                 if (ret < 0 && ret != -ENOIOCTLCMD)
256                         return -EPIPE;
257
258                 /* Check if the two ends match */
259                 if (fmt_source.format.code != fmt_sink.format.code ||
260                     fmt_source.format.width != fmt_sink.format.width ||
261                     fmt_source.format.height != fmt_sink.format.height)
262                         return -EPIPE;
263         }
264         return 0;
265 }
266
267 /*
268  * vpfe_pipeline_enable() - Enable streaming on a pipeline
269  * @vpfe_dev: vpfe device
270  * @pipe: vpfe pipeline
271  *
272  * Walk the entities chain starting at the pipeline output video node and start
273  * all modules in the chain in the given mode.
274  *
275  * Return 0 if successful, or the return value of the failed video::s_stream
276  * operation otherwise.
277  */
278 static int vpfe_pipeline_enable(struct vpfe_pipeline *pipe)
279 {
280         struct media_entity_graph graph;
281         struct media_entity *entity;
282         struct v4l2_subdev *subdev;
283         struct media_device *mdev;
284         int ret = 0;
285
286         if (pipe->state == VPFE_PIPELINE_STREAM_CONTINUOUS)
287                 entity = vpfe_get_input_entity(pipe->outputs[0]);
288         else
289                 entity = &pipe->inputs[0]->video_dev.entity;
290
291         mdev = entity->parent;
292         mutex_lock(&mdev->graph_mutex);
293         media_entity_graph_walk_start(&graph, entity);
294         while ((entity = media_entity_graph_walk_next(&graph))) {
295
296                 if (media_entity_type(entity) == MEDIA_ENT_T_DEVNODE)
297                         continue;
298                 subdev = media_entity_to_v4l2_subdev(entity);
299                 ret = v4l2_subdev_call(subdev, video, s_stream, 1);
300                 if (ret < 0 && ret != -ENOIOCTLCMD)
301                         break;
302         }
303         mutex_unlock(&mdev->graph_mutex);
304         return ret;
305 }
306
307 /*
308  * vpfe_pipeline_disable() - Disable streaming on a pipeline
309  * @vpfe_dev: vpfe device
310  * @pipe: VPFE pipeline
311  *
312  * Walk the entities chain starting at the pipeline output video node and stop
313  * all modules in the chain.
314  *
315  * Return 0 if all modules have been properly stopped, or -ETIMEDOUT if a module
316  * can't be stopped.
317  */
318 static int vpfe_pipeline_disable(struct vpfe_pipeline *pipe)
319 {
320         struct media_entity_graph graph;
321         struct media_entity *entity;
322         struct v4l2_subdev *subdev;
323         struct media_device *mdev;
324         int ret = 0;
325
326         if (pipe->state == VPFE_PIPELINE_STREAM_CONTINUOUS)
327                 entity = vpfe_get_input_entity(pipe->outputs[0]);
328         else
329                 entity = &pipe->inputs[0]->video_dev.entity;
330
331         mdev = entity->parent;
332         mutex_lock(&mdev->graph_mutex);
333         media_entity_graph_walk_start(&graph, entity);
334
335         while ((entity = media_entity_graph_walk_next(&graph))) {
336
337                 if (media_entity_type(entity) == MEDIA_ENT_T_DEVNODE)
338                         continue;
339                 subdev = media_entity_to_v4l2_subdev(entity);
340                 ret = v4l2_subdev_call(subdev, video, s_stream, 0);
341                 if (ret < 0 && ret != -ENOIOCTLCMD)
342                         break;
343         }
344         mutex_unlock(&mdev->graph_mutex);
345
346         return ret ? -ETIMEDOUT : 0;
347 }
348
349 /*
350  * vpfe_pipeline_set_stream() - Enable/disable streaming on a pipeline
351  * @vpfe_dev: VPFE device
352  * @pipe: VPFE pipeline
353  * @state: Stream state (stopped or active)
354  *
355  * Set the pipeline to the given stream state.
356  *
357  * Return 0 if successful, or the return value of the failed video::s_stream
358  * operation otherwise.
359  */
360 static int vpfe_pipeline_set_stream(struct vpfe_pipeline *pipe,
361                             enum vpfe_pipeline_stream_state state)
362 {
363         if (state == VPFE_PIPELINE_STREAM_STOPPED)
364                 return vpfe_pipeline_disable(pipe);
365
366         return vpfe_pipeline_enable(pipe);
367 }
368
369 static int all_videos_stopped(struct vpfe_video_device *video)
370 {
371         struct vpfe_pipeline *pipe = &video->pipe;
372         int i;
373
374         for (i = 0; i < pipe->input_num; i++)
375                 if (pipe->inputs[i]->started)
376                         return 0;
377         for (i = 0; i < pipe->output_num; i++)
378                 if (pipe->outputs[i]->started)
379                         return 0;
380         return 1;
381 }
382
383 /*
384  * vpfe_open() - open video device
385  * @file: file pointer
386  *
387  * initialize media pipeline state, allocate memory for file handle
388  *
389  * Return 0 if successful, or the return -ENODEV otherwise.
390  */
391 static int vpfe_open(struct file *file)
392 {
393         struct vpfe_video_device *video = video_drvdata(file);
394         struct vpfe_fh *handle;
395
396         /* Allocate memory for the file handle object */
397         handle = kzalloc(sizeof(struct vpfe_fh), GFP_KERNEL);
398
399         if (handle == NULL)
400                 return -ENOMEM;
401
402         v4l2_fh_init(&handle->vfh, &video->video_dev);
403         v4l2_fh_add(&handle->vfh);
404
405         mutex_lock(&video->lock);
406         /* If decoder is not initialized. initialize it */
407         if (!video->initialized && vpfe_update_pipe_state(video)) {
408                 mutex_unlock(&video->lock);
409                 return -ENODEV;
410         }
411         /* Increment device users counter */
412         video->usrs++;
413         /* Set io_allowed member to false */
414         handle->io_allowed = 0;
415         handle->video = video;
416         file->private_data = &handle->vfh;
417         mutex_unlock(&video->lock);
418
419         return 0;
420 }
421
422 /* get the next buffer available from dma queue */
423 static unsigned long
424 vpfe_video_get_next_buffer(struct vpfe_video_device *video)
425 {
426         video->cur_frm = video->next_frm =
427                 list_entry(video->dma_queue.next,
428                            struct vpfe_cap_buffer, list);
429
430         list_del(&video->next_frm->list);
431         video->next_frm->vb.state = VB2_BUF_STATE_ACTIVE;
432         return vb2_dma_contig_plane_dma_addr(&video->next_frm->vb, 0);
433 }
434
435 /* schedule the next buffer which is available on dma queue */
436 void vpfe_video_schedule_next_buffer(struct vpfe_video_device *video)
437 {
438         struct vpfe_device *vpfe_dev = video->vpfe_dev;
439         unsigned long addr;
440
441         if (list_empty(&video->dma_queue))
442                 return;
443
444         video->next_frm = list_entry(video->dma_queue.next,
445                                         struct vpfe_cap_buffer, list);
446
447         if (VPFE_PIPELINE_STREAM_SINGLESHOT == video->pipe.state)
448                 video->cur_frm = video->next_frm;
449
450         list_del(&video->next_frm->list);
451         video->next_frm->vb.state = VB2_BUF_STATE_ACTIVE;
452         addr = vb2_dma_contig_plane_dma_addr(&video->next_frm->vb, 0);
453         video->ops->queue(vpfe_dev, addr);
454         video->state = VPFE_VIDEO_BUFFER_QUEUED;
455 }
456
457 /* schedule the buffer for capturing bottom field */
458 void vpfe_video_schedule_bottom_field(struct vpfe_video_device *video)
459 {
460         struct vpfe_device *vpfe_dev = video->vpfe_dev;
461         unsigned long addr;
462
463         addr = vb2_dma_contig_plane_dma_addr(&video->cur_frm->vb, 0);
464         addr += video->field_off;
465         video->ops->queue(vpfe_dev, addr);
466 }
467
468 /* make buffer available for dequeue */
469 void vpfe_video_process_buffer_complete(struct vpfe_video_device *video)
470 {
471         struct vpfe_pipeline *pipe = &video->pipe;
472
473         v4l2_get_timestamp(&video->cur_frm->vb.v4l2_buf.timestamp);
474         vb2_buffer_done(&video->cur_frm->vb, VB2_BUF_STATE_DONE);
475         if (pipe->state == VPFE_PIPELINE_STREAM_CONTINUOUS)
476                 video->cur_frm = video->next_frm;
477 }
478
479 /* vpfe_stop_capture() - stop streaming */
480 static void vpfe_stop_capture(struct vpfe_video_device *video)
481 {
482         struct vpfe_pipeline *pipe = &video->pipe;
483
484         video->started = 0;
485
486         if (video->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
487                 return;
488         if (all_videos_stopped(video))
489                 vpfe_pipeline_set_stream(pipe,
490                                          VPFE_PIPELINE_STREAM_STOPPED);
491 }
492
493 /*
494  * vpfe_release() - release video device
495  * @file: file pointer
496  *
497  * deletes buffer queue, frees the buffers and the vpfe file handle
498  *
499  * Return 0
500  */
501 static int vpfe_release(struct file *file)
502 {
503         struct vpfe_video_device *video = video_drvdata(file);
504         struct v4l2_fh *vfh = file->private_data;
505         struct vpfe_device *vpfe_dev = video->vpfe_dev;
506         struct vpfe_fh *fh = container_of(vfh, struct vpfe_fh, vfh);
507
508         v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_release\n");
509
510         /* Get the device lock */
511         mutex_lock(&video->lock);
512         /* if this instance is doing IO */
513         if (fh->io_allowed) {
514                 if (video->started) {
515                         vpfe_stop_capture(video);
516                         /* mark pipe state as stopped in vpfe_release(),
517                            as app might call streamon() after streamoff()
518                            in which case driver has to start streaming.
519                         */
520                         video->pipe.state = VPFE_PIPELINE_STREAM_STOPPED;
521                         vb2_streamoff(&video->buffer_queue,
522                                       video->buffer_queue.type);
523                 }
524                 video->io_usrs = 0;
525                 /* Free buffers allocated */
526                 vb2_queue_release(&video->buffer_queue);
527                 vb2_dma_contig_cleanup_ctx(video->alloc_ctx);
528         }
529         /* Decrement device users counter */
530         video->usrs--;
531         v4l2_fh_del(&fh->vfh);
532         v4l2_fh_exit(&fh->vfh);
533         /* If this is the last file handle */
534         if (!video->usrs)
535                 video->initialized = 0;
536         mutex_unlock(&video->lock);
537         file->private_data = NULL;
538         /* Free memory allocated to file handle object */
539         v4l2_fh_del(vfh);
540         kzfree(fh);
541         return 0;
542 }
543
544 /*
545  * vpfe_mmap() - It is used to map kernel space buffers
546  * into user spaces
547  */
548 static int vpfe_mmap(struct file *file, struct vm_area_struct *vma)
549 {
550         struct vpfe_video_device *video = video_drvdata(file);
551         struct vpfe_device *vpfe_dev = video->vpfe_dev;
552
553         v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_mmap\n");
554         return vb2_mmap(&video->buffer_queue, vma);
555 }
556
557 /*
558  * vpfe_poll() - It is used for select/poll system call
559  */
560 static unsigned int vpfe_poll(struct file *file, poll_table *wait)
561 {
562         struct vpfe_video_device *video = video_drvdata(file);
563         struct vpfe_device *vpfe_dev = video->vpfe_dev;
564
565         v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_poll\n");
566         if (video->started)
567                 return vb2_poll(&video->buffer_queue, file, wait);
568         return 0;
569 }
570
571 /* vpfe capture driver file operations */
572 static const struct v4l2_file_operations vpfe_fops = {
573         .owner = THIS_MODULE,
574         .open = vpfe_open,
575         .release = vpfe_release,
576         .unlocked_ioctl = video_ioctl2,
577         .mmap = vpfe_mmap,
578         .poll = vpfe_poll
579 };
580
581 /*
582  * vpfe_querycap() - query capabilities of video device
583  * @file: file pointer
584  * @priv: void pointer
585  * @cap: pointer to v4l2_capability structure
586  *
587  * fills v4l2 capabilities structure
588  *
589  * Return 0
590  */
591 static int vpfe_querycap(struct file *file, void  *priv,
592                                struct v4l2_capability *cap)
593 {
594         struct vpfe_video_device *video = video_drvdata(file);
595         struct vpfe_device *vpfe_dev = video->vpfe_dev;
596
597         v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_querycap\n");
598
599         if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
600                 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
601         else
602                 cap->device_caps = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_STREAMING;
603         cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VIDEO_OUTPUT |
604                             V4L2_CAP_STREAMING | V4L2_CAP_DEVICE_CAPS;
605         strlcpy(cap->driver, CAPTURE_DRV_NAME, sizeof(cap->driver));
606         strlcpy(cap->bus_info, "VPFE", sizeof(cap->bus_info));
607         strlcpy(cap->card, vpfe_dev->cfg->card_name, sizeof(cap->card));
608
609         return 0;
610 }
611
612 /*
613  * vpfe_g_fmt() - get the format which is active on video device
614  * @file: file pointer
615  * @priv: void pointer
616  * @fmt: pointer to v4l2_format structure
617  *
618  * fills v4l2 format structure with active format
619  *
620  * Return 0
621  */
622 static int vpfe_g_fmt(struct file *file, void *priv,
623                                 struct v4l2_format *fmt)
624 {
625         struct vpfe_video_device *video = video_drvdata(file);
626         struct vpfe_device *vpfe_dev = video->vpfe_dev;
627
628         v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_g_fmt\n");
629         /* Fill in the information about format */
630         *fmt = video->fmt;
631         return 0;
632 }
633
634 /*
635  * vpfe_enum_fmt() - enum formats supported on media chain
636  * @file: file pointer
637  * @priv: void pointer
638  * @fmt: pointer to v4l2_fmtdesc structure
639  *
640  * fills v4l2_fmtdesc structure with output format set on adjacent subdev,
641  * only one format is enumearted as subdevs are already configured
642  *
643  * Return 0 if successful, error code otherwise
644  */
645 static int vpfe_enum_fmt(struct file *file, void  *priv,
646                                    struct v4l2_fmtdesc *fmt)
647 {
648         struct vpfe_video_device *video = video_drvdata(file);
649         struct vpfe_device *vpfe_dev = video->vpfe_dev;
650         struct v4l2_subdev_format sd_fmt;
651         struct v4l2_mbus_framefmt mbus;
652         struct v4l2_subdev *subdev;
653         struct v4l2_format format;
654         struct media_pad *remote;
655         int ret;
656
657         v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_enum_fmt\n");
658
659         /* since already subdev pad format is set,
660         only one pixel format is available */
661         if (fmt->index > 0) {
662                 v4l2_err(&vpfe_dev->v4l2_dev, "Invalid index\n");
663                 return -EINVAL;
664         }
665         /* get the remote pad */
666         remote = media_entity_remote_pad(&video->pad);
667         if (remote == NULL) {
668                 v4l2_err(&vpfe_dev->v4l2_dev,
669                          "invalid remote pad for video node\n");
670                 return -EINVAL;
671         }
672         /* get the remote subdev */
673         subdev = vpfe_video_remote_subdev(video, NULL);
674         if (subdev == NULL) {
675                 v4l2_err(&vpfe_dev->v4l2_dev,
676                          "invalid remote subdev for video node\n");
677                 return -EINVAL;
678         }
679         sd_fmt.pad = remote->index;
680         sd_fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
681         /* get output format of remote subdev */
682         ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &sd_fmt);
683         if (ret) {
684                 v4l2_err(&vpfe_dev->v4l2_dev,
685                          "invalid remote subdev for video node\n");
686                 return ret;
687         }
688         /* convert to pix format */
689         mbus.code = sd_fmt.format.code;
690         mbus_to_pix(&mbus, &format.fmt.pix);
691         /* copy the result */
692         fmt->pixelformat = format.fmt.pix.pixelformat;
693
694         return 0;
695 }
696
697 /*
698  * vpfe_s_fmt() - set the format on video device
699  * @file: file pointer
700  * @priv: void pointer
701  * @fmt: pointer to v4l2_format structure
702  *
703  * validate and set the format on video device
704  *
705  * Return 0 on success, error code otherwise
706  */
707 static int vpfe_s_fmt(struct file *file, void *priv,
708                                 struct v4l2_format *fmt)
709 {
710         struct vpfe_video_device *video = video_drvdata(file);
711         struct vpfe_device *vpfe_dev = video->vpfe_dev;
712         struct v4l2_format format;
713         int ret;
714
715         v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_s_fmt\n");
716         /* If streaming is started, return error */
717         if (video->started) {
718                 v4l2_err(&vpfe_dev->v4l2_dev, "Streaming is started\n");
719                 return -EBUSY;
720         }
721         /* get adjacent subdev's output pad format */
722         ret = __vpfe_video_get_format(video, &format);
723         if (ret)
724                 return ret;
725         *fmt = format;
726         video->fmt = *fmt;
727         return 0;
728 }
729
730 /*
731  * vpfe_try_fmt() - try the format on video device
732  * @file: file pointer
733  * @priv: void pointer
734  * @fmt: pointer to v4l2_format structure
735  *
736  * validate the format, update with correct format
737  * based on output format set on adjacent subdev
738  *
739  * Return 0 on success, error code otherwise
740  */
741 static int vpfe_try_fmt(struct file *file, void *priv,
742                                   struct v4l2_format *fmt)
743 {
744         struct vpfe_video_device *video = video_drvdata(file);
745         struct vpfe_device *vpfe_dev = video->vpfe_dev;
746         struct v4l2_format format;
747         int ret;
748
749         v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_try_fmt\n");
750         /* get adjacent subdev's output pad format */
751         ret = __vpfe_video_get_format(video, &format);
752         if (ret)
753                 return ret;
754
755         *fmt = format;
756         return 0;
757 }
758
759 /*
760  * vpfe_enum_input() - enum inputs supported on media chain
761  * @file: file pointer
762  * @priv: void pointer
763  * @fmt: pointer to v4l2_fmtdesc structure
764  *
765  * fills v4l2_input structure with input available on media chain,
766  * only one input is enumearted as media chain is setup by this time
767  *
768  * Return 0 if successful, -EINVAL is media chain is invalid
769  */
770 static int vpfe_enum_input(struct file *file, void *priv,
771                                  struct v4l2_input *inp)
772 {
773         struct vpfe_video_device *video = video_drvdata(file);
774         struct vpfe_ext_subdev_info *sdinfo = video->current_ext_subdev;
775         struct vpfe_device *vpfe_dev = video->vpfe_dev;
776
777         v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_enum_input\n");
778         /* enumerate from the subdev user has chosen through mc */
779         if (inp->index < sdinfo->num_inputs) {
780                 memcpy(inp, &sdinfo->inputs[inp->index],
781                        sizeof(struct v4l2_input));
782                 return 0;
783         }
784         return -EINVAL;
785 }
786
787 /*
788  * vpfe_g_input() - get index of the input which is active
789  * @file: file pointer
790  * @priv: void pointer
791  * @index: pointer to unsigned int
792  *
793  * set index with input index which is active
794  */
795 static int vpfe_g_input(struct file *file, void *priv, unsigned int *index)
796 {
797         struct vpfe_video_device *video = video_drvdata(file);
798         struct vpfe_device *vpfe_dev = video->vpfe_dev;
799
800         v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_g_input\n");
801
802         *index = video->current_input;
803         return 0;
804 }
805
806 /*
807  * vpfe_s_input() - set input which is pointed by input index
808  * @file: file pointer
809  * @priv: void pointer
810  * @index: pointer to unsigned int
811  *
812  * set input on external subdev
813  *
814  * Return 0 on success, error code otherwise
815  */
816 static int vpfe_s_input(struct file *file, void *priv, unsigned int index)
817 {
818         struct vpfe_video_device *video = video_drvdata(file);
819         struct vpfe_device *vpfe_dev = video->vpfe_dev;
820         struct vpfe_ext_subdev_info *sdinfo;
821         struct vpfe_route *route;
822         struct v4l2_input *inps;
823         u32 output;
824         u32 input;
825         int ret;
826         int i;
827
828         v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_s_input\n");
829
830         ret = mutex_lock_interruptible(&video->lock);
831         if (ret)
832                 return ret;
833         /*
834          * If streaming is started return device busy
835          * error
836          */
837         if (video->started) {
838                 v4l2_err(&vpfe_dev->v4l2_dev, "Streaming is on\n");
839                 ret = -EBUSY;
840                 goto unlock_out;
841         }
842
843         sdinfo = video->current_ext_subdev;
844         if (!sdinfo->registered) {
845                 ret = -EINVAL;
846                 goto unlock_out;
847         }
848         if (vpfe_dev->cfg->setup_input &&
849                 vpfe_dev->cfg->setup_input(sdinfo->grp_id) < 0) {
850                 ret = -EFAULT;
851                 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev,
852                           "couldn't setup input for %s\n",
853                           sdinfo->module_name);
854                 goto unlock_out;
855         }
856         route = &sdinfo->routes[index];
857         if (route && sdinfo->can_route) {
858                 input = route->input;
859                 output = route->output;
860                 ret = v4l2_device_call_until_err(&vpfe_dev->v4l2_dev,
861                                                  sdinfo->grp_id, video,
862                                                  s_routing, input, output, 0);
863                 if (ret) {
864                         v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev,
865                                 "s_input:error in setting input in decoder\n");
866                         ret = -EINVAL;
867                         goto unlock_out;
868                 }
869         }
870         /* set standards set by subdev in video device */
871         for (i = 0; i < sdinfo->num_inputs; i++) {
872                 inps = &sdinfo->inputs[i];
873                 video->video_dev.tvnorms |= inps->std;
874         }
875         video->current_input = index;
876 unlock_out:
877         mutex_unlock(&video->lock);
878         return ret;
879 }
880
881 /*
882  * vpfe_querystd() - query std which is being input on external subdev
883  * @file: file pointer
884  * @priv: void pointer
885  * @std_id: pointer to v4l2_std_id structure
886  *
887  * call external subdev through v4l2_device_call_until_err to
888  * get the std that is being active.
889  *
890  * Return 0 on success, error code otherwise
891  */
892 static int vpfe_querystd(struct file *file, void *priv, v4l2_std_id *std_id)
893 {
894         struct vpfe_video_device *video = video_drvdata(file);
895         struct vpfe_device *vpfe_dev = video->vpfe_dev;
896         struct vpfe_ext_subdev_info *sdinfo;
897         int ret;
898
899         v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_querystd\n");
900
901         ret = mutex_lock_interruptible(&video->lock);
902         sdinfo = video->current_ext_subdev;
903         if (ret)
904                 return ret;
905         /* Call querystd function of decoder device */
906         ret = v4l2_device_call_until_err(&vpfe_dev->v4l2_dev, sdinfo->grp_id,
907                                          video, querystd, std_id);
908         mutex_unlock(&video->lock);
909         return ret;
910 }
911
912 /*
913  * vpfe_s_std() - set std on external subdev
914  * @file: file pointer
915  * @priv: void pointer
916  * @std_id: pointer to v4l2_std_id structure
917  *
918  * set std pointed by std_id on external subdev by calling it using
919  * v4l2_device_call_until_err
920  *
921  * Return 0 on success, error code otherwise
922  */
923 static int vpfe_s_std(struct file *file, void *priv, v4l2_std_id std_id)
924 {
925         struct vpfe_video_device *video = video_drvdata(file);
926         struct vpfe_device *vpfe_dev = video->vpfe_dev;
927         struct vpfe_ext_subdev_info *sdinfo;
928         int ret;
929
930         v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_s_std\n");
931
932         /* Call decoder driver function to set the standard */
933         ret = mutex_lock_interruptible(&video->lock);
934         if (ret)
935                 return ret;
936         sdinfo = video->current_ext_subdev;
937         /* If streaming is started, return device busy error */
938         if (video->started) {
939                 v4l2_err(&vpfe_dev->v4l2_dev, "streaming is started\n");
940                 ret = -EBUSY;
941                 goto unlock_out;
942         }
943         ret = v4l2_device_call_until_err(&vpfe_dev->v4l2_dev, sdinfo->grp_id,
944                                          video, s_std, std_id);
945         if (ret < 0) {
946                 v4l2_err(&vpfe_dev->v4l2_dev, "Failed to set standard\n");
947                 video->stdid = V4L2_STD_UNKNOWN;
948                 goto unlock_out;
949         }
950         video->stdid = std_id;
951 unlock_out:
952         mutex_unlock(&video->lock);
953         return ret;
954 }
955
956 static int vpfe_g_std(struct file *file, void *priv, v4l2_std_id *tvnorm)
957 {
958         struct vpfe_video_device *video = video_drvdata(file);
959         struct vpfe_device *vpfe_dev = video->vpfe_dev;
960
961         v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_g_std\n");
962         *tvnorm = video->stdid;
963         return 0;
964 }
965
966 /*
967  * vpfe_enum_dv_timings() - enumerate dv_timings which are supported by
968  *                      to external subdev
969  * @file: file pointer
970  * @priv: void pointer
971  * @timings: pointer to v4l2_enum_dv_timings structure
972  *
973  * enum dv_timings's which are supported by external subdev through
974  * v4l2_subdev_call
975  *
976  * Return 0 on success, error code otherwise
977  */
978 static int
979 vpfe_enum_dv_timings(struct file *file, void *fh,
980                   struct v4l2_enum_dv_timings *timings)
981 {
982         struct vpfe_video_device *video = video_drvdata(file);
983         struct vpfe_device *vpfe_dev = video->vpfe_dev;
984         struct v4l2_subdev *subdev = video->current_ext_subdev->subdev;
985
986         timings->pad = 0;
987
988         v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_enum_dv_timings\n");
989         return v4l2_subdev_call(subdev, pad, enum_dv_timings, timings);
990 }
991
992 /*
993  * vpfe_query_dv_timings() - query the dv_timings which is being input
994  *                      to external subdev
995  * @file: file pointer
996  * @priv: void pointer
997  * @timings: pointer to v4l2_dv_timings structure
998  *
999  * get dv_timings which is being input on external subdev through
1000  * v4l2_subdev_call
1001  *
1002  * Return 0 on success, error code otherwise
1003  */
1004 static int
1005 vpfe_query_dv_timings(struct file *file, void *fh,
1006                    struct v4l2_dv_timings *timings)
1007 {
1008         struct vpfe_video_device *video = video_drvdata(file);
1009         struct vpfe_device *vpfe_dev = video->vpfe_dev;
1010         struct v4l2_subdev *subdev = video->current_ext_subdev->subdev;
1011
1012         v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_query_dv_timings\n");
1013         return v4l2_subdev_call(subdev, video, query_dv_timings, timings);
1014 }
1015
1016 /*
1017  * vpfe_s_dv_timings() - set dv_timings on external subdev
1018  * @file: file pointer
1019  * @priv: void pointer
1020  * @timings: pointer to v4l2_dv_timings structure
1021  *
1022  * set dv_timings pointed by timings on external subdev through
1023  * v4l2_device_call_until_err, this configures amplifier also
1024  *
1025  * Return 0 on success, error code otherwise
1026  */
1027 static int
1028 vpfe_s_dv_timings(struct file *file, void *fh,
1029                   struct v4l2_dv_timings *timings)
1030 {
1031         struct vpfe_video_device *video = video_drvdata(file);
1032         struct vpfe_device *vpfe_dev = video->vpfe_dev;
1033
1034         v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_s_dv_timings\n");
1035
1036         video->stdid = V4L2_STD_UNKNOWN;
1037         return v4l2_device_call_until_err(&vpfe_dev->v4l2_dev,
1038                                           video->current_ext_subdev->grp_id,
1039                                           video, s_dv_timings, timings);
1040 }
1041
1042 /*
1043  * vpfe_g_dv_timings() - get dv_timings which is set on external subdev
1044  * @file: file pointer
1045  * @priv: void pointer
1046  * @timings: pointer to v4l2_dv_timings structure
1047  *
1048  * get dv_timings which is set on external subdev through
1049  * v4l2_subdev_call
1050  *
1051  * Return 0 on success, error code otherwise
1052  */
1053 static int
1054 vpfe_g_dv_timings(struct file *file, void *fh,
1055               struct v4l2_dv_timings *timings)
1056 {
1057         struct vpfe_video_device *video = video_drvdata(file);
1058         struct vpfe_device *vpfe_dev = video->vpfe_dev;
1059         struct v4l2_subdev *subdev = video->current_ext_subdev->subdev;
1060
1061         v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_g_dv_timings\n");
1062         return v4l2_subdev_call(subdev, video, g_dv_timings, timings);
1063 }
1064
1065 /*
1066  *  Videobuf operations
1067  */
1068 /*
1069  * vpfe_buffer_queue_setup : Callback function for buffer setup.
1070  * @vq: vb2_queue ptr
1071  * @fmt: v4l2 format
1072  * @nbuffers: ptr to number of buffers requested by application
1073  * @nplanes:: contains number of distinct video planes needed to hold a frame
1074  * @sizes[]: contains the size (in bytes) of each plane.
1075  * @alloc_ctxs: ptr to allocation context
1076  *
1077  * This callback function is called when reqbuf() is called to adjust
1078  * the buffer nbuffers and buffer size
1079  */
1080 static int
1081 vpfe_buffer_queue_setup(struct vb2_queue *vq, const struct v4l2_format *fmt,
1082                         unsigned int *nbuffers, unsigned int *nplanes,
1083                         unsigned int sizes[], void *alloc_ctxs[])
1084 {
1085         struct vpfe_fh *fh = vb2_get_drv_priv(vq);
1086         struct vpfe_video_device *video = fh->video;
1087         struct vpfe_device *vpfe_dev = video->vpfe_dev;
1088         unsigned long size;
1089
1090         v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_buffer_queue_setup\n");
1091         size = video->fmt.fmt.pix.sizeimage;
1092
1093         if (vq->num_buffers + *nbuffers < 3)
1094                 *nbuffers = 3 - vq->num_buffers;
1095
1096         *nplanes = 1;
1097         sizes[0] = size;
1098         alloc_ctxs[0] = video->alloc_ctx;
1099         v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev,
1100                  "nbuffers=%d, size=%lu\n", *nbuffers, size);
1101         return 0;
1102 }
1103
1104 /*
1105  * vpfe_buffer_prepare : callback function for buffer prepare
1106  * @vb: ptr to vb2_buffer
1107  *
1108  * This is the callback function for buffer prepare when vb2_qbuf()
1109  * function is called. The buffer is prepared and user space virtual address
1110  * or user address is converted into  physical address
1111  */
1112 static int vpfe_buffer_prepare(struct vb2_buffer *vb)
1113 {
1114         struct vpfe_fh *fh = vb2_get_drv_priv(vb->vb2_queue);
1115         struct vpfe_video_device *video = fh->video;
1116         struct vpfe_device *vpfe_dev = video->vpfe_dev;
1117         unsigned long addr;
1118
1119         v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_buffer_prepare\n");
1120
1121         if (vb->state != VB2_BUF_STATE_ACTIVE &&
1122             vb->state != VB2_BUF_STATE_PREPARED)
1123                 return 0;
1124
1125         /* Initialize buffer */
1126         vb2_set_plane_payload(vb, 0, video->fmt.fmt.pix.sizeimage);
1127         if (vb2_plane_vaddr(vb, 0) &&
1128                 vb2_get_plane_payload(vb, 0) > vb2_plane_size(vb, 0))
1129                         return -EINVAL;
1130
1131         addr = vb2_dma_contig_plane_dma_addr(vb, 0);
1132         /* Make sure user addresses are aligned to 32 bytes */
1133         if (!ALIGN(addr, 32))
1134                 return -EINVAL;
1135
1136         return 0;
1137 }
1138
1139 static void vpfe_buffer_queue(struct vb2_buffer *vb)
1140 {
1141         /* Get the file handle object and device object */
1142         struct vpfe_fh *fh = vb2_get_drv_priv(vb->vb2_queue);
1143         struct vpfe_video_device *video = fh->video;
1144         struct vpfe_device *vpfe_dev = video->vpfe_dev;
1145         struct vpfe_pipeline *pipe = &video->pipe;
1146         struct vpfe_cap_buffer *buf = container_of(vb,
1147                                 struct vpfe_cap_buffer, vb);
1148         unsigned long flags;
1149         unsigned long empty;
1150         unsigned long addr;
1151
1152         spin_lock_irqsave(&video->dma_queue_lock, flags);
1153         empty = list_empty(&video->dma_queue);
1154         /* add the buffer to the DMA queue */
1155         list_add_tail(&buf->list, &video->dma_queue);
1156         spin_unlock_irqrestore(&video->dma_queue_lock, flags);
1157         /* this case happens in case of single shot */
1158         if (empty && video->started && pipe->state ==
1159                 VPFE_PIPELINE_STREAM_SINGLESHOT &&
1160                 video->state == VPFE_VIDEO_BUFFER_NOT_QUEUED) {
1161                 spin_lock(&video->dma_queue_lock);
1162                 addr = vpfe_video_get_next_buffer(video);
1163                 video->ops->queue(vpfe_dev, addr);
1164
1165                 video->state = VPFE_VIDEO_BUFFER_QUEUED;
1166                 spin_unlock(&video->dma_queue_lock);
1167
1168                 /* enable h/w each time in single shot */
1169                 if (vpfe_video_is_pipe_ready(pipe))
1170                         vpfe_pipeline_set_stream(pipe,
1171                                         VPFE_PIPELINE_STREAM_SINGLESHOT);
1172         }
1173 }
1174
1175 /* vpfe_start_capture() - start streaming on all the subdevs */
1176 static int vpfe_start_capture(struct vpfe_video_device *video)
1177 {
1178         struct vpfe_pipeline *pipe = &video->pipe;
1179         int ret = 0;
1180
1181         video->started = 1;
1182         if (vpfe_video_is_pipe_ready(pipe))
1183                 ret = vpfe_pipeline_set_stream(pipe, pipe->state);
1184
1185         return ret;
1186 }
1187
1188 static int vpfe_start_streaming(struct vb2_queue *vq, unsigned int count)
1189 {
1190         struct vpfe_fh *fh = vb2_get_drv_priv(vq);
1191         struct vpfe_video_device *video = fh->video;
1192         struct vpfe_device *vpfe_dev = video->vpfe_dev;
1193         unsigned long addr;
1194         int ret;
1195
1196         ret = mutex_lock_interruptible(&video->lock);
1197         if (ret)
1198                 goto streamoff;
1199
1200         /* Get the next frame from the buffer queue */
1201         video->cur_frm = video->next_frm =
1202                 list_entry(video->dma_queue.next, struct vpfe_cap_buffer, list);
1203         /* Remove buffer from the buffer queue */
1204         list_del(&video->cur_frm->list);
1205         /* Mark state of the current frame to active */
1206         video->cur_frm->vb.state = VB2_BUF_STATE_ACTIVE;
1207         /* Initialize field_id and started member */
1208         video->field_id = 0;
1209         addr = vb2_dma_contig_plane_dma_addr(&video->cur_frm->vb, 0);
1210         video->ops->queue(vpfe_dev, addr);
1211         video->state = VPFE_VIDEO_BUFFER_QUEUED;
1212
1213         ret = vpfe_start_capture(video);
1214         if (ret) {
1215                 struct vpfe_cap_buffer *buf, *tmp;
1216
1217                 vb2_buffer_done(&video->cur_frm->vb, VB2_BUF_STATE_QUEUED);
1218                 list_for_each_entry_safe(buf, tmp, &video->dma_queue, list) {
1219                         list_del(&buf->list);
1220                         vb2_buffer_done(&buf->vb, VB2_BUF_STATE_QUEUED);
1221                 }
1222                 goto unlock_out;
1223         }
1224
1225         mutex_unlock(&video->lock);
1226
1227         return ret;
1228 unlock_out:
1229         mutex_unlock(&video->lock);
1230 streamoff:
1231         ret = vb2_streamoff(&video->buffer_queue, video->buffer_queue.type);
1232         return 0;
1233 }
1234
1235 static int vpfe_buffer_init(struct vb2_buffer *vb)
1236 {
1237         struct vpfe_cap_buffer *buf = container_of(vb,
1238                                                    struct vpfe_cap_buffer, vb);
1239
1240         INIT_LIST_HEAD(&buf->list);
1241         return 0;
1242 }
1243
1244 /* abort streaming and wait for last buffer */
1245 static void vpfe_stop_streaming(struct vb2_queue *vq)
1246 {
1247         struct vpfe_fh *fh = vb2_get_drv_priv(vq);
1248         struct vpfe_video_device *video = fh->video;
1249
1250         /* release all active buffers */
1251         if (video->cur_frm == video->next_frm) {
1252                 vb2_buffer_done(&video->cur_frm->vb, VB2_BUF_STATE_ERROR);
1253         } else {
1254                 if (video->cur_frm != NULL)
1255                         vb2_buffer_done(&video->cur_frm->vb,
1256                                         VB2_BUF_STATE_ERROR);
1257                 if (video->next_frm != NULL)
1258                         vb2_buffer_done(&video->next_frm->vb,
1259                                         VB2_BUF_STATE_ERROR);
1260         }
1261
1262         while (!list_empty(&video->dma_queue)) {
1263                 video->next_frm = list_entry(video->dma_queue.next,
1264                                                 struct vpfe_cap_buffer, list);
1265                 list_del(&video->next_frm->list);
1266                 vb2_buffer_done(&video->next_frm->vb, VB2_BUF_STATE_ERROR);
1267         }
1268 }
1269
1270 static void vpfe_buf_cleanup(struct vb2_buffer *vb)
1271 {
1272         struct vpfe_fh *fh = vb2_get_drv_priv(vb->vb2_queue);
1273         struct vpfe_video_device *video = fh->video;
1274         struct vpfe_device *vpfe_dev = video->vpfe_dev;
1275         struct vpfe_cap_buffer *buf = container_of(vb,
1276                                         struct vpfe_cap_buffer, vb);
1277
1278         v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_buf_cleanup\n");
1279         if (vb->state == VB2_BUF_STATE_ACTIVE)
1280                 list_del_init(&buf->list);
1281 }
1282
1283 static struct vb2_ops video_qops = {
1284         .queue_setup            = vpfe_buffer_queue_setup,
1285         .buf_init               = vpfe_buffer_init,
1286         .buf_prepare            = vpfe_buffer_prepare,
1287         .start_streaming        = vpfe_start_streaming,
1288         .stop_streaming         = vpfe_stop_streaming,
1289         .buf_cleanup            = vpfe_buf_cleanup,
1290         .buf_queue              = vpfe_buffer_queue,
1291 };
1292
1293 /*
1294  * vpfe_reqbufs() - supported REQBUF only once opening
1295  * the device.
1296  */
1297 static int vpfe_reqbufs(struct file *file, void *priv,
1298                         struct v4l2_requestbuffers *req_buf)
1299 {
1300         struct vpfe_video_device *video = video_drvdata(file);
1301         struct vpfe_device *vpfe_dev = video->vpfe_dev;
1302         struct vpfe_fh *fh = file->private_data;
1303         struct vb2_queue *q;
1304         int ret;
1305
1306         v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_reqbufs\n");
1307
1308         if (V4L2_BUF_TYPE_VIDEO_CAPTURE != req_buf->type &&
1309             V4L2_BUF_TYPE_VIDEO_OUTPUT != req_buf->type) {
1310                 v4l2_err(&vpfe_dev->v4l2_dev, "Invalid buffer type\n");
1311                 return -EINVAL;
1312         }
1313
1314         ret = mutex_lock_interruptible(&video->lock);
1315         if (ret)
1316                 return ret;
1317
1318         if (video->io_usrs != 0) {
1319                 v4l2_err(&vpfe_dev->v4l2_dev, "Only one IO user allowed\n");
1320                 ret = -EBUSY;
1321                 goto unlock_out;
1322         }
1323         video->memory = req_buf->memory;
1324
1325         /* Initialize videobuf2 queue as per the buffer type */
1326         video->alloc_ctx = vb2_dma_contig_init_ctx(vpfe_dev->pdev);
1327         if (IS_ERR(video->alloc_ctx)) {
1328                 v4l2_err(&vpfe_dev->v4l2_dev, "Failed to get the context\n");
1329                 return PTR_ERR(video->alloc_ctx);
1330         }
1331
1332         q = &video->buffer_queue;
1333         q->type = req_buf->type;
1334         q->io_modes = VB2_MMAP | VB2_USERPTR;
1335         q->drv_priv = fh;
1336         q->min_buffers_needed = 1;
1337         q->ops = &video_qops;
1338         q->mem_ops = &vb2_dma_contig_memops;
1339         q->buf_struct_size = sizeof(struct vpfe_cap_buffer);
1340         q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1341
1342         ret = vb2_queue_init(q);
1343         if (ret) {
1344                 v4l2_err(&vpfe_dev->v4l2_dev, "vb2_queue_init() failed\n");
1345                 vb2_dma_contig_cleanup_ctx(vpfe_dev->pdev);
1346                 return ret;
1347         }
1348
1349         fh->io_allowed = 1;
1350         video->io_usrs = 1;
1351         INIT_LIST_HEAD(&video->dma_queue);
1352         ret = vb2_reqbufs(&video->buffer_queue, req_buf);
1353
1354 unlock_out:
1355         mutex_unlock(&video->lock);
1356         return ret;
1357 }
1358
1359 /*
1360  * vpfe_querybuf() - query buffers for exchange
1361  */
1362 static int vpfe_querybuf(struct file *file, void *priv,
1363                          struct v4l2_buffer *buf)
1364 {
1365         struct vpfe_video_device *video = video_drvdata(file);
1366         struct vpfe_device *vpfe_dev = video->vpfe_dev;
1367
1368         v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_querybuf\n");
1369
1370         if (V4L2_BUF_TYPE_VIDEO_CAPTURE != buf->type &&
1371             V4L2_BUF_TYPE_VIDEO_OUTPUT != buf->type) {
1372                 v4l2_err(&vpfe_dev->v4l2_dev, "Invalid buf type\n");
1373                 return  -EINVAL;
1374         }
1375
1376         if (video->memory != V4L2_MEMORY_MMAP) {
1377                 v4l2_err(&vpfe_dev->v4l2_dev, "Invalid memory\n");
1378                 return -EINVAL;
1379         }
1380
1381         /* Call vb2_querybuf to get information */
1382         return vb2_querybuf(&video->buffer_queue, buf);
1383 }
1384
1385 /*
1386  * vpfe_qbuf() - queue buffers for capture or processing
1387  */
1388 static int vpfe_qbuf(struct file *file, void *priv,
1389                      struct v4l2_buffer *p)
1390 {
1391         struct vpfe_video_device *video = video_drvdata(file);
1392         struct vpfe_device *vpfe_dev = video->vpfe_dev;
1393         struct vpfe_fh *fh = file->private_data;
1394
1395         v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_qbuf\n");
1396
1397         if (V4L2_BUF_TYPE_VIDEO_CAPTURE != p->type &&
1398             V4L2_BUF_TYPE_VIDEO_OUTPUT != p->type) {
1399                 v4l2_err(&vpfe_dev->v4l2_dev, "Invalid buf type\n");
1400                 return -EINVAL;
1401         }
1402         /*
1403          * If this file handle is not allowed to do IO,
1404          * return error
1405          */
1406         if (!fh->io_allowed) {
1407                 v4l2_err(&vpfe_dev->v4l2_dev, "fh->io_allowed\n");
1408                 return -EACCES;
1409         }
1410
1411         return vb2_qbuf(&video->buffer_queue, p);
1412 }
1413
1414 /*
1415  * vpfe_dqbuf() - deque buffer which is done with processing
1416  */
1417 static int vpfe_dqbuf(struct file *file, void *priv,
1418                       struct v4l2_buffer *buf)
1419 {
1420         struct vpfe_video_device *video = video_drvdata(file);
1421         struct vpfe_device *vpfe_dev = video->vpfe_dev;
1422
1423         v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_dqbuf\n");
1424
1425         if (V4L2_BUF_TYPE_VIDEO_CAPTURE != buf->type &&
1426             V4L2_BUF_TYPE_VIDEO_OUTPUT != buf->type) {
1427                 v4l2_err(&vpfe_dev->v4l2_dev, "Invalid buf type\n");
1428                 return -EINVAL;
1429         }
1430
1431         return vb2_dqbuf(&video->buffer_queue,
1432                          buf, (file->f_flags & O_NONBLOCK));
1433 }
1434
1435 /*
1436  * vpfe_streamon() - start streaming
1437  * @file: file pointer
1438  * @priv: void pointer
1439  * @buf_type: enum v4l2_buf_type
1440  *
1441  * queue buffer onto hardware for capture/processing and
1442  * start all the subdevs which are in media chain
1443  *
1444  * Return 0 on success, error code otherwise
1445  */
1446 static int vpfe_streamon(struct file *file, void *priv,
1447                          enum v4l2_buf_type buf_type)
1448 {
1449         struct vpfe_video_device *video = video_drvdata(file);
1450         struct vpfe_device *vpfe_dev = video->vpfe_dev;
1451         struct vpfe_pipeline *pipe = &video->pipe;
1452         struct vpfe_fh *fh = file->private_data;
1453         struct vpfe_ext_subdev_info *sdinfo;
1454         int ret = -EINVAL;
1455
1456         v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_streamon\n");
1457
1458         if (V4L2_BUF_TYPE_VIDEO_CAPTURE != buf_type &&
1459             V4L2_BUF_TYPE_VIDEO_OUTPUT != buf_type) {
1460                 v4l2_err(&vpfe_dev->v4l2_dev, "Invalid buf type\n");
1461                 return ret;
1462         }
1463         /* If file handle is not allowed IO, return error */
1464         if (!fh->io_allowed) {
1465                 v4l2_err(&vpfe_dev->v4l2_dev, "fh->io_allowed\n");
1466                 return -EACCES;
1467         }
1468         sdinfo = video->current_ext_subdev;
1469         /* If buffer queue is empty, return error */
1470         if (list_empty(&video->buffer_queue.queued_list)) {
1471                 v4l2_err(&vpfe_dev->v4l2_dev, "buffer queue is empty\n");
1472                 return -EIO;
1473         }
1474         /* Validate the pipeline */
1475         if (V4L2_BUF_TYPE_VIDEO_CAPTURE == buf_type) {
1476                 ret = vpfe_video_validate_pipeline(pipe);
1477                 if (ret < 0)
1478                         return ret;
1479         }
1480         /* Call vb2_streamon to start streaming */
1481         return vb2_streamon(&video->buffer_queue, buf_type);
1482 }
1483
1484 /*
1485  * vpfe_streamoff() - stop streaming
1486  * @file: file pointer
1487  * @priv: void pointer
1488  * @buf_type: enum v4l2_buf_type
1489  *
1490  * stop all the subdevs which are in media chain
1491  *
1492  * Return 0 on success, error code otherwise
1493  */
1494 static int vpfe_streamoff(struct file *file, void *priv,
1495                           enum v4l2_buf_type buf_type)
1496 {
1497         struct vpfe_video_device *video = video_drvdata(file);
1498         struct vpfe_device *vpfe_dev = video->vpfe_dev;
1499         struct vpfe_fh *fh = file->private_data;
1500         int ret = 0;
1501
1502         v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_streamoff\n");
1503
1504         if (buf_type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1505             buf_type != V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1506                 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "Invalid buf type\n");
1507                 return -EINVAL;
1508         }
1509
1510         /* If io is allowed for this file handle, return error */
1511         if (!fh->io_allowed) {
1512                 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "fh->io_allowed\n");
1513                 return -EACCES;
1514         }
1515
1516         /* If streaming is not started, return error */
1517         if (!video->started) {
1518                 v4l2_err(&vpfe_dev->v4l2_dev, "device is not started\n");
1519                 return -EINVAL;
1520         }
1521
1522         ret = mutex_lock_interruptible(&video->lock);
1523         if (ret)
1524                 return ret;
1525
1526         vpfe_stop_capture(video);
1527         ret = vb2_streamoff(&video->buffer_queue, buf_type);
1528         mutex_unlock(&video->lock);
1529
1530         return ret;
1531 }
1532
1533 /* vpfe capture ioctl operations */
1534 static const struct v4l2_ioctl_ops vpfe_ioctl_ops = {
1535         .vidioc_querycap         = vpfe_querycap,
1536         .vidioc_g_fmt_vid_cap    = vpfe_g_fmt,
1537         .vidioc_s_fmt_vid_cap    = vpfe_s_fmt,
1538         .vidioc_try_fmt_vid_cap  = vpfe_try_fmt,
1539         .vidioc_enum_fmt_vid_cap = vpfe_enum_fmt,
1540         .vidioc_g_fmt_vid_out    = vpfe_g_fmt,
1541         .vidioc_s_fmt_vid_out    = vpfe_s_fmt,
1542         .vidioc_try_fmt_vid_out  = vpfe_try_fmt,
1543         .vidioc_enum_fmt_vid_out = vpfe_enum_fmt,
1544         .vidioc_enum_input       = vpfe_enum_input,
1545         .vidioc_g_input          = vpfe_g_input,
1546         .vidioc_s_input          = vpfe_s_input,
1547         .vidioc_querystd         = vpfe_querystd,
1548         .vidioc_s_std            = vpfe_s_std,
1549         .vidioc_g_std            = vpfe_g_std,
1550         .vidioc_enum_dv_timings  = vpfe_enum_dv_timings,
1551         .vidioc_query_dv_timings = vpfe_query_dv_timings,
1552         .vidioc_s_dv_timings     = vpfe_s_dv_timings,
1553         .vidioc_g_dv_timings     = vpfe_g_dv_timings,
1554         .vidioc_reqbufs          = vpfe_reqbufs,
1555         .vidioc_querybuf         = vpfe_querybuf,
1556         .vidioc_qbuf             = vpfe_qbuf,
1557         .vidioc_dqbuf            = vpfe_dqbuf,
1558         .vidioc_streamon         = vpfe_streamon,
1559         .vidioc_streamoff        = vpfe_streamoff,
1560 };
1561
1562 /* VPFE video init function */
1563 int vpfe_video_init(struct vpfe_video_device *video, const char *name)
1564 {
1565         const char *direction;
1566         int ret;
1567
1568         switch (video->type) {
1569         case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1570                 direction = "output";
1571                 video->pad.flags = MEDIA_PAD_FL_SINK;
1572                 video->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1573                 break;
1574
1575         case V4L2_BUF_TYPE_VIDEO_OUTPUT:
1576                 direction = "input";
1577                 video->pad.flags = MEDIA_PAD_FL_SOURCE;
1578                 video->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
1579                 break;
1580
1581         default:
1582                 return -EINVAL;
1583         }
1584         /* Initialize field of video device */
1585         video->video_dev.release = video_device_release;
1586         video->video_dev.fops = &vpfe_fops;
1587         video->video_dev.ioctl_ops = &vpfe_ioctl_ops;
1588         video->video_dev.minor = -1;
1589         video->video_dev.tvnorms = 0;
1590         snprintf(video->video_dev.name, sizeof(video->video_dev.name),
1591                  "DAVINCI VIDEO %s %s", name, direction);
1592
1593         spin_lock_init(&video->irqlock);
1594         spin_lock_init(&video->dma_queue_lock);
1595         mutex_init(&video->lock);
1596         ret = media_entity_init(&video->video_dev.entity,
1597                                 1, &video->pad, 0);
1598         if (ret < 0)
1599                 return ret;
1600
1601         video_set_drvdata(&video->video_dev, video);
1602
1603         return 0;
1604 }
1605
1606 /* vpfe video device register function */
1607 int vpfe_video_register(struct vpfe_video_device *video,
1608                         struct v4l2_device *vdev)
1609 {
1610         int ret;
1611
1612         video->video_dev.v4l2_dev = vdev;
1613
1614         ret = video_register_device(&video->video_dev, VFL_TYPE_GRABBER, -1);
1615         if (ret < 0)
1616                 pr_err("%s: could not register video device (%d)\n",
1617                        __func__, ret);
1618         return ret;
1619 }
1620
1621 /* vpfe video device unregister function */
1622 void vpfe_video_unregister(struct vpfe_video_device *video)
1623 {
1624         if (video_is_registered(&video->video_dev)) {
1625                 video_unregister_device(&video->video_dev);
1626                 media_entity_cleanup(&video->video_dev.entity);
1627         }
1628 }