2 * Copyright (C) 2012 Texas Instruments Inc
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.
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.
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
18 * Manjunath Hadli <manjunath.hadli@ti.com>
19 * Prabhakar Lad <prabhakar.lad@ti.com>
22 #include <linux/module.h>
23 #include <linux/slab.h>
25 #include <media/v4l2-ioctl.h>
28 #include "vpfe_mc_capture.h"
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)
36 struct vpfe_device *vpfe_dev = video->vpfe_dev;
37 struct media_pad *remote;
39 remote = media_entity_remote_pad(&vpfe_dev->vpfe_isif.pads[0]);
41 pr_err("Invalid media connection to isif/ccdc\n");
44 return remote->entity;
47 /* updates external subdev(sensor/decoder) which is active */
48 static int vpfe_update_current_ext_subdev(struct vpfe_video_device *video)
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;
56 remote = media_entity_remote_pad(&vpfe_dev->vpfe_isif.pads[0]);
58 pr_err("Invalid media connection to isif/ccdc\n");
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];
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");
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];
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)
89 struct media_pad *remote = media_entity_remote_pad(&video->pad);
91 if (remote == NULL || remote->entity->type != MEDIA_ENT_T_V4L2_SUBDEV)
95 return media_entity_to_v4l2_subdev(remote->entity);
98 /* get the format set at output pad of the adjacent subdev */
100 __vpfe_video_get_format(struct vpfe_video_device *video,
101 struct v4l2_format *format)
103 struct v4l2_subdev_format fmt;
104 struct v4l2_subdev *subdev;
105 struct media_pad *remote;
109 subdev = vpfe_video_remote_subdev(video, &pad);
113 fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
114 remote = media_entity_remote_pad(&video->pad);
115 fmt.pad = remote->index;
117 ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &fmt);
118 if (ret == -ENOIOCTLCMD)
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);
129 /* make a note of pipeline details */
130 static void vpfe_prepare_pipeline(struct vpfe_video_device *video)
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;
139 pipe->output_num = 0;
141 if (video->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
142 pipe->inputs[pipe->input_num++] = video;
144 pipe->outputs[pipe->output_num++] = video;
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)
151 if (media_entity_type(entity) != MEDIA_ENT_T_DEVNODE)
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;
157 pipe->outputs[pipe->output_num++] = far_end;
159 mutex_unlock(&mdev->graph_mutex);
162 /* update pipe state selected by user */
163 static int vpfe_update_pipe_state(struct vpfe_video_device *video)
165 struct vpfe_pipeline *pipe = &video->pipe;
168 vpfe_prepare_pipeline(video);
170 /* Find out if there is any input video
171 if yes, it is single shot.
173 if (pipe->input_num == 0) {
174 pipe->state = VPFE_PIPELINE_STREAM_CONTINUOUS;
175 ret = vpfe_update_current_ext_subdev(video);
177 pr_err("Invalid external subdev\n");
181 pipe->state = VPFE_PIPELINE_STREAM_SINGLESHOT;
183 video->initialized = 1;
184 video->skip_frame_count = 1;
185 video->skip_frame_count_init = 1;
189 /* checks wether pipeline is ready for enabling */
190 int vpfe_video_is_pipe_ready(struct vpfe_pipeline *pipe)
194 for (i = 0; i < pipe->input_num; i++)
195 if (!pipe->inputs[i]->started ||
196 pipe->inputs[i]->state != VPFE_VIDEO_BUFFER_QUEUED)
198 for (i = 0; i < pipe->output_num; i++)
199 if (!pipe->outputs[i]->started ||
200 pipe->outputs[i]->state != VPFE_VIDEO_BUFFER_QUEUED)
206 * Validate a pipeline by checking both ends of all links for format
209 * Return 0 if all formats match, or -EPIPE if at least one link is found with
210 * different formats on its two ends.
212 static int vpfe_video_validate_pipeline(struct vpfe_pipeline *pipe)
214 struct v4l2_subdev_format fmt_source;
215 struct v4l2_subdev_format fmt_sink;
216 struct v4l2_subdev *subdev;
217 struct media_pad *pad;
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.
226 subdev = vpfe_video_remote_subdev(pipe->outputs[0], NULL);
231 /* Retrieve the sink format */
232 pad = &subdev->entity.pads[0];
233 if (!(pad->flags & MEDIA_PAD_FL_SINK))
236 fmt_sink.which = V4L2_SUBDEV_FORMAT_ACTIVE;
237 fmt_sink.pad = pad->index;
238 ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL,
241 if (ret < 0 && ret != -ENOIOCTLCMD)
244 /* Retrieve the source format */
245 pad = media_entity_remote_pad(pad);
247 pad->entity->type != MEDIA_ENT_T_V4L2_SUBDEV)
250 subdev = media_entity_to_v4l2_subdev(pad->entity);
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)
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)
268 * vpfe_pipeline_enable() - Enable streaming on a pipeline
269 * @vpfe_dev: vpfe device
270 * @pipe: vpfe pipeline
272 * Walk the entities chain starting at the pipeline output video node and start
273 * all modules in the chain in the given mode.
275 * Return 0 if successful, or the return value of the failed video::s_stream
276 * operation otherwise.
278 static int vpfe_pipeline_enable(struct vpfe_pipeline *pipe)
280 struct media_entity_graph graph;
281 struct media_entity *entity;
282 struct v4l2_subdev *subdev;
283 struct media_device *mdev;
286 if (pipe->state == VPFE_PIPELINE_STREAM_CONTINUOUS)
287 entity = vpfe_get_input_entity(pipe->outputs[0]);
289 entity = &pipe->inputs[0]->video_dev.entity;
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))) {
296 if (media_entity_type(entity) == MEDIA_ENT_T_DEVNODE)
298 subdev = media_entity_to_v4l2_subdev(entity);
299 ret = v4l2_subdev_call(subdev, video, s_stream, 1);
300 if (ret < 0 && ret != -ENOIOCTLCMD)
303 mutex_unlock(&mdev->graph_mutex);
308 * vpfe_pipeline_disable() - Disable streaming on a pipeline
309 * @vpfe_dev: vpfe device
310 * @pipe: VPFE pipeline
312 * Walk the entities chain starting at the pipeline output video node and stop
313 * all modules in the chain.
315 * Return 0 if all modules have been properly stopped, or -ETIMEDOUT if a module
318 static int vpfe_pipeline_disable(struct vpfe_pipeline *pipe)
320 struct media_entity_graph graph;
321 struct media_entity *entity;
322 struct v4l2_subdev *subdev;
323 struct media_device *mdev;
326 if (pipe->state == VPFE_PIPELINE_STREAM_CONTINUOUS)
327 entity = vpfe_get_input_entity(pipe->outputs[0]);
329 entity = &pipe->inputs[0]->video_dev.entity;
331 mdev = entity->parent;
332 mutex_lock(&mdev->graph_mutex);
333 media_entity_graph_walk_start(&graph, entity);
335 while ((entity = media_entity_graph_walk_next(&graph))) {
337 if (media_entity_type(entity) == MEDIA_ENT_T_DEVNODE)
339 subdev = media_entity_to_v4l2_subdev(entity);
340 ret = v4l2_subdev_call(subdev, video, s_stream, 0);
341 if (ret < 0 && ret != -ENOIOCTLCMD)
344 mutex_unlock(&mdev->graph_mutex);
346 return ret ? -ETIMEDOUT : 0;
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)
355 * Set the pipeline to the given stream state.
357 * Return 0 if successful, or the return value of the failed video::s_stream
358 * operation otherwise.
360 static int vpfe_pipeline_set_stream(struct vpfe_pipeline *pipe,
361 enum vpfe_pipeline_stream_state state)
363 if (state == VPFE_PIPELINE_STREAM_STOPPED)
364 return vpfe_pipeline_disable(pipe);
366 return vpfe_pipeline_enable(pipe);
369 static int all_videos_stopped(struct vpfe_video_device *video)
371 struct vpfe_pipeline *pipe = &video->pipe;
374 for (i = 0; i < pipe->input_num; i++)
375 if (pipe->inputs[i]->started)
377 for (i = 0; i < pipe->output_num; i++)
378 if (pipe->outputs[i]->started)
384 * vpfe_open() - open video device
385 * @file: file pointer
387 * initialize media pipeline state, allocate memory for file handle
389 * Return 0 if successful, or the return -ENODEV otherwise.
391 static int vpfe_open(struct file *file)
393 struct vpfe_video_device *video = video_drvdata(file);
394 struct vpfe_fh *handle;
396 /* Allocate memory for the file handle object */
397 handle = kzalloc(sizeof(struct vpfe_fh), GFP_KERNEL);
402 v4l2_fh_init(&handle->vfh, &video->video_dev);
403 v4l2_fh_add(&handle->vfh);
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);
411 /* Increment device users counter */
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);
422 /* get the next buffer available from dma queue */
424 vpfe_video_get_next_buffer(struct vpfe_video_device *video)
426 video->cur_frm = video->next_frm =
427 list_entry(video->dma_queue.next,
428 struct vpfe_cap_buffer, list);
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);
435 /* schedule the next buffer which is available on dma queue */
436 void vpfe_video_schedule_next_buffer(struct vpfe_video_device *video)
438 struct vpfe_device *vpfe_dev = video->vpfe_dev;
441 if (list_empty(&video->dma_queue))
444 video->next_frm = list_entry(video->dma_queue.next,
445 struct vpfe_cap_buffer, list);
447 if (VPFE_PIPELINE_STREAM_SINGLESHOT == video->pipe.state)
448 video->cur_frm = video->next_frm;
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;
457 /* schedule the buffer for capturing bottom field */
458 void vpfe_video_schedule_bottom_field(struct vpfe_video_device *video)
460 struct vpfe_device *vpfe_dev = video->vpfe_dev;
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);
468 /* make buffer available for dequeue */
469 void vpfe_video_process_buffer_complete(struct vpfe_video_device *video)
471 struct vpfe_pipeline *pipe = &video->pipe;
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;
479 /* vpfe_stop_capture() - stop streaming */
480 static void vpfe_stop_capture(struct vpfe_video_device *video)
482 struct vpfe_pipeline *pipe = &video->pipe;
486 if (video->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
488 if (all_videos_stopped(video))
489 vpfe_pipeline_set_stream(pipe,
490 VPFE_PIPELINE_STREAM_STOPPED);
494 * vpfe_release() - release video device
495 * @file: file pointer
497 * deletes buffer queue, frees the buffers and the vpfe file handle
501 static int vpfe_release(struct file *file)
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);
508 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_release\n");
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.
520 video->pipe.state = VPFE_PIPELINE_STREAM_STOPPED;
521 vb2_streamoff(&video->buffer_queue,
522 video->buffer_queue.type);
525 /* Free buffers allocated */
526 vb2_queue_release(&video->buffer_queue);
527 vb2_dma_contig_cleanup_ctx(video->alloc_ctx);
529 /* Decrement device users counter */
531 v4l2_fh_del(&fh->vfh);
532 v4l2_fh_exit(&fh->vfh);
533 /* If this is the last file handle */
535 video->initialized = 0;
536 mutex_unlock(&video->lock);
537 file->private_data = NULL;
538 /* Free memory allocated to file handle object */
545 * vpfe_mmap() - It is used to map kernel space buffers
548 static int vpfe_mmap(struct file *file, struct vm_area_struct *vma)
550 struct vpfe_video_device *video = video_drvdata(file);
551 struct vpfe_device *vpfe_dev = video->vpfe_dev;
553 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_mmap\n");
554 return vb2_mmap(&video->buffer_queue, vma);
558 * vpfe_poll() - It is used for select/poll system call
560 static unsigned int vpfe_poll(struct file *file, poll_table *wait)
562 struct vpfe_video_device *video = video_drvdata(file);
563 struct vpfe_device *vpfe_dev = video->vpfe_dev;
565 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_poll\n");
567 return vb2_poll(&video->buffer_queue, file, wait);
571 /* vpfe capture driver file operations */
572 static const struct v4l2_file_operations vpfe_fops = {
573 .owner = THIS_MODULE,
575 .release = vpfe_release,
576 .unlocked_ioctl = video_ioctl2,
582 * vpfe_querycap() - query capabilities of video device
583 * @file: file pointer
584 * @priv: void pointer
585 * @cap: pointer to v4l2_capability structure
587 * fills v4l2 capabilities structure
591 static int vpfe_querycap(struct file *file, void *priv,
592 struct v4l2_capability *cap)
594 struct vpfe_video_device *video = video_drvdata(file);
595 struct vpfe_device *vpfe_dev = video->vpfe_dev;
597 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_querycap\n");
599 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
600 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
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));
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
618 * fills v4l2 format structure with active format
622 static int vpfe_g_fmt(struct file *file, void *priv,
623 struct v4l2_format *fmt)
625 struct vpfe_video_device *video = video_drvdata(file);
626 struct vpfe_device *vpfe_dev = video->vpfe_dev;
628 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_g_fmt\n");
629 /* Fill in the information about format */
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
640 * fills v4l2_fmtdesc structure with output format set on adjacent subdev,
641 * only one format is enumearted as subdevs are already configured
643 * Return 0 if successful, error code otherwise
645 static int vpfe_enum_fmt(struct file *file, void *priv,
646 struct v4l2_fmtdesc *fmt)
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;
657 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_enum_fmt\n");
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");
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");
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");
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);
684 v4l2_err(&vpfe_dev->v4l2_dev,
685 "invalid remote subdev for video node\n");
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;
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
703 * validate and set the format on video device
705 * Return 0 on success, error code otherwise
707 static int vpfe_s_fmt(struct file *file, void *priv,
708 struct v4l2_format *fmt)
710 struct vpfe_video_device *video = video_drvdata(file);
711 struct vpfe_device *vpfe_dev = video->vpfe_dev;
712 struct v4l2_format format;
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");
721 /* get adjacent subdev's output pad format */
722 ret = __vpfe_video_get_format(video, &format);
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
736 * validate the format, update with correct format
737 * based on output format set on adjacent subdev
739 * Return 0 on success, error code otherwise
741 static int vpfe_try_fmt(struct file *file, void *priv,
742 struct v4l2_format *fmt)
744 struct vpfe_video_device *video = video_drvdata(file);
745 struct vpfe_device *vpfe_dev = video->vpfe_dev;
746 struct v4l2_format format;
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);
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
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
768 * Return 0 if successful, -EINVAL is media chain is invalid
770 static int vpfe_enum_input(struct file *file, void *priv,
771 struct v4l2_input *inp)
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;
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));
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
793 * set index with input index which is active
795 static int vpfe_g_input(struct file *file, void *priv, unsigned int *index)
797 struct vpfe_video_device *video = video_drvdata(file);
798 struct vpfe_device *vpfe_dev = video->vpfe_dev;
800 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_g_input\n");
802 *index = video->current_input;
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
812 * set input on external subdev
814 * Return 0 on success, error code otherwise
816 static int vpfe_s_input(struct file *file, void *priv, unsigned int index)
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;
828 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_s_input\n");
830 ret = mutex_lock_interruptible(&video->lock);
834 * If streaming is started return device busy
837 if (video->started) {
838 v4l2_err(&vpfe_dev->v4l2_dev, "Streaming is on\n");
843 sdinfo = video->current_ext_subdev;
844 if (!sdinfo->registered) {
848 if (vpfe_dev->cfg->setup_input &&
849 vpfe_dev->cfg->setup_input(sdinfo->grp_id) < 0) {
851 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev,
852 "couldn't setup input for %s\n",
853 sdinfo->module_name);
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);
864 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev,
865 "s_input:error in setting input in decoder\n");
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;
875 video->current_input = index;
877 mutex_unlock(&video->lock);
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
887 * call external subdev through v4l2_device_call_until_err to
888 * get the std that is being active.
890 * Return 0 on success, error code otherwise
892 static int vpfe_querystd(struct file *file, void *priv, v4l2_std_id *std_id)
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;
899 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_querystd\n");
901 ret = mutex_lock_interruptible(&video->lock);
902 sdinfo = video->current_ext_subdev;
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);
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
918 * set std pointed by std_id on external subdev by calling it using
919 * v4l2_device_call_until_err
921 * Return 0 on success, error code otherwise
923 static int vpfe_s_std(struct file *file, void *priv, v4l2_std_id std_id)
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;
930 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_s_std\n");
932 /* Call decoder driver function to set the standard */
933 ret = mutex_lock_interruptible(&video->lock);
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");
943 ret = v4l2_device_call_until_err(&vpfe_dev->v4l2_dev, sdinfo->grp_id,
944 video, s_std, std_id);
946 v4l2_err(&vpfe_dev->v4l2_dev, "Failed to set standard\n");
947 video->stdid = V4L2_STD_UNKNOWN;
950 video->stdid = std_id;
952 mutex_unlock(&video->lock);
956 static int vpfe_g_std(struct file *file, void *priv, v4l2_std_id *tvnorm)
958 struct vpfe_video_device *video = video_drvdata(file);
959 struct vpfe_device *vpfe_dev = video->vpfe_dev;
961 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_g_std\n");
962 *tvnorm = video->stdid;
967 * vpfe_enum_dv_timings() - enumerate dv_timings which are supported by
969 * @file: file pointer
970 * @priv: void pointer
971 * @timings: pointer to v4l2_enum_dv_timings structure
973 * enum dv_timings's which are supported by external subdev through
976 * Return 0 on success, error code otherwise
979 vpfe_enum_dv_timings(struct file *file, void *fh,
980 struct v4l2_enum_dv_timings *timings)
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;
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);
993 * vpfe_query_dv_timings() - query the dv_timings which is being input
995 * @file: file pointer
996 * @priv: void pointer
997 * @timings: pointer to v4l2_dv_timings structure
999 * get dv_timings which is being input on external subdev through
1002 * Return 0 on success, error code otherwise
1005 vpfe_query_dv_timings(struct file *file, void *fh,
1006 struct v4l2_dv_timings *timings)
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;
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);
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
1022 * set dv_timings pointed by timings on external subdev through
1023 * v4l2_device_call_until_err, this configures amplifier also
1025 * Return 0 on success, error code otherwise
1028 vpfe_s_dv_timings(struct file *file, void *fh,
1029 struct v4l2_dv_timings *timings)
1031 struct vpfe_video_device *video = video_drvdata(file);
1032 struct vpfe_device *vpfe_dev = video->vpfe_dev;
1034 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_s_dv_timings\n");
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);
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
1048 * get dv_timings which is set on external subdev through
1051 * Return 0 on success, error code otherwise
1054 vpfe_g_dv_timings(struct file *file, void *fh,
1055 struct v4l2_dv_timings *timings)
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;
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);
1066 * Videobuf operations
1069 * vpfe_buffer_queue_setup : Callback function for buffer setup.
1070 * @vq: vb2_queue ptr
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
1077 * This callback function is called when reqbuf() is called to adjust
1078 * the buffer nbuffers and buffer size
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[])
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;
1090 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_buffer_queue_setup\n");
1091 size = video->fmt.fmt.pix.sizeimage;
1093 if (vq->num_buffers + *nbuffers < 3)
1094 *nbuffers = 3 - vq->num_buffers;
1098 alloc_ctxs[0] = video->alloc_ctx;
1099 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev,
1100 "nbuffers=%d, size=%lu\n", *nbuffers, size);
1105 * vpfe_buffer_prepare : callback function for buffer prepare
1106 * @vb: ptr to vb2_buffer
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
1112 static int vpfe_buffer_prepare(struct vb2_buffer *vb)
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;
1119 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_buffer_prepare\n");
1121 if (vb->state != VB2_BUF_STATE_ACTIVE &&
1122 vb->state != VB2_BUF_STATE_PREPARED)
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))
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))
1139 static void vpfe_buffer_queue(struct vb2_buffer *vb)
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;
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);
1165 video->state = VPFE_VIDEO_BUFFER_QUEUED;
1166 spin_unlock(&video->dma_queue_lock);
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);
1175 /* vpfe_start_capture() - start streaming on all the subdevs */
1176 static int vpfe_start_capture(struct vpfe_video_device *video)
1178 struct vpfe_pipeline *pipe = &video->pipe;
1182 if (vpfe_video_is_pipe_ready(pipe))
1183 ret = vpfe_pipeline_set_stream(pipe, pipe->state);
1188 static int vpfe_start_streaming(struct vb2_queue *vq, unsigned int count)
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;
1196 ret = mutex_lock_interruptible(&video->lock);
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;
1213 ret = vpfe_start_capture(video);
1215 struct vpfe_cap_buffer *buf, *tmp;
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);
1225 mutex_unlock(&video->lock);
1229 mutex_unlock(&video->lock);
1231 ret = vb2_streamoff(&video->buffer_queue, video->buffer_queue.type);
1235 static int vpfe_buffer_init(struct vb2_buffer *vb)
1237 struct vpfe_cap_buffer *buf = container_of(vb,
1238 struct vpfe_cap_buffer, vb);
1240 INIT_LIST_HEAD(&buf->list);
1244 /* abort streaming and wait for last buffer */
1245 static void vpfe_stop_streaming(struct vb2_queue *vq)
1247 struct vpfe_fh *fh = vb2_get_drv_priv(vq);
1248 struct vpfe_video_device *video = fh->video;
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);
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);
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);
1270 static void vpfe_buf_cleanup(struct vb2_buffer *vb)
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);
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);
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,
1294 * vpfe_reqbufs() - supported REQBUF only once opening
1297 static int vpfe_reqbufs(struct file *file, void *priv,
1298 struct v4l2_requestbuffers *req_buf)
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;
1306 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_reqbufs\n");
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");
1314 ret = mutex_lock_interruptible(&video->lock);
1318 if (video->io_usrs != 0) {
1319 v4l2_err(&vpfe_dev->v4l2_dev, "Only one IO user allowed\n");
1323 video->memory = req_buf->memory;
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);
1332 q = &video->buffer_queue;
1333 q->type = req_buf->type;
1334 q->io_modes = VB2_MMAP | VB2_USERPTR;
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;
1342 ret = vb2_queue_init(q);
1344 v4l2_err(&vpfe_dev->v4l2_dev, "vb2_queue_init() failed\n");
1345 vb2_dma_contig_cleanup_ctx(vpfe_dev->pdev);
1351 INIT_LIST_HEAD(&video->dma_queue);
1352 ret = vb2_reqbufs(&video->buffer_queue, req_buf);
1355 mutex_unlock(&video->lock);
1360 * vpfe_querybuf() - query buffers for exchange
1362 static int vpfe_querybuf(struct file *file, void *priv,
1363 struct v4l2_buffer *buf)
1365 struct vpfe_video_device *video = video_drvdata(file);
1366 struct vpfe_device *vpfe_dev = video->vpfe_dev;
1368 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_querybuf\n");
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");
1376 if (video->memory != V4L2_MEMORY_MMAP) {
1377 v4l2_err(&vpfe_dev->v4l2_dev, "Invalid memory\n");
1381 /* Call vb2_querybuf to get information */
1382 return vb2_querybuf(&video->buffer_queue, buf);
1386 * vpfe_qbuf() - queue buffers for capture or processing
1388 static int vpfe_qbuf(struct file *file, void *priv,
1389 struct v4l2_buffer *p)
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;
1395 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_qbuf\n");
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");
1403 * If this file handle is not allowed to do IO,
1406 if (!fh->io_allowed) {
1407 v4l2_err(&vpfe_dev->v4l2_dev, "fh->io_allowed\n");
1411 return vb2_qbuf(&video->buffer_queue, p);
1415 * vpfe_dqbuf() - deque buffer which is done with processing
1417 static int vpfe_dqbuf(struct file *file, void *priv,
1418 struct v4l2_buffer *buf)
1420 struct vpfe_video_device *video = video_drvdata(file);
1421 struct vpfe_device *vpfe_dev = video->vpfe_dev;
1423 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_dqbuf\n");
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");
1431 return vb2_dqbuf(&video->buffer_queue,
1432 buf, (file->f_flags & O_NONBLOCK));
1436 * vpfe_streamon() - start streaming
1437 * @file: file pointer
1438 * @priv: void pointer
1439 * @buf_type: enum v4l2_buf_type
1441 * queue buffer onto hardware for capture/processing and
1442 * start all the subdevs which are in media chain
1444 * Return 0 on success, error code otherwise
1446 static int vpfe_streamon(struct file *file, void *priv,
1447 enum v4l2_buf_type buf_type)
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;
1456 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_streamon\n");
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");
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");
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");
1474 /* Validate the pipeline */
1475 if (V4L2_BUF_TYPE_VIDEO_CAPTURE == buf_type) {
1476 ret = vpfe_video_validate_pipeline(pipe);
1480 /* Call vb2_streamon to start streaming */
1481 return vb2_streamon(&video->buffer_queue, buf_type);
1485 * vpfe_streamoff() - stop streaming
1486 * @file: file pointer
1487 * @priv: void pointer
1488 * @buf_type: enum v4l2_buf_type
1490 * stop all the subdevs which are in media chain
1492 * Return 0 on success, error code otherwise
1494 static int vpfe_streamoff(struct file *file, void *priv,
1495 enum v4l2_buf_type buf_type)
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;
1502 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_streamoff\n");
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");
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");
1516 /* If streaming is not started, return error */
1517 if (!video->started) {
1518 v4l2_err(&vpfe_dev->v4l2_dev, "device is not started\n");
1522 ret = mutex_lock_interruptible(&video->lock);
1526 vpfe_stop_capture(video);
1527 ret = vb2_streamoff(&video->buffer_queue, buf_type);
1528 mutex_unlock(&video->lock);
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,
1562 /* VPFE video init function */
1563 int vpfe_video_init(struct vpfe_video_device *video, const char *name)
1565 const char *direction;
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;
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;
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);
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,
1601 video_set_drvdata(&video->video_dev, video);
1606 /* vpfe video device register function */
1607 int vpfe_video_register(struct vpfe_video_device *video,
1608 struct v4l2_device *vdev)
1612 video->video_dev.v4l2_dev = vdev;
1614 ret = video_register_device(&video->video_dev, VFL_TYPE_GRABBER, -1);
1616 pr_err("%s: could not register video device (%d)\n",
1621 /* vpfe video device unregister function */
1622 void vpfe_video_unregister(struct vpfe_video_device *video)
1624 if (video_is_registered(&video->video_dev)) {
1625 video_unregister_device(&video->video_dev);
1626 media_entity_cleanup(&video->video_dev.entity);