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 || !is_media_entity_v4l2_subdev(remote->entity))
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 int vpfe_prepare_pipeline(struct vpfe_video_device *video)
132 struct media_entity_graph graph;
133 struct media_entity *entity = &video->video_dev.entity;
134 struct media_device *mdev = entity->graph_obj.mdev;
135 struct vpfe_pipeline *pipe = &video->pipe;
136 struct vpfe_video_device *far_end = NULL;
140 pipe->output_num = 0;
142 if (video->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
143 pipe->inputs[pipe->input_num++] = video;
145 pipe->outputs[pipe->output_num++] = video;
147 mutex_lock(&mdev->graph_mutex);
148 ret = media_entity_graph_walk_init(&graph, entity->graph_obj.mdev);
150 mutex_unlock(&mdev->graph_mutex);
153 media_entity_graph_walk_start(&graph, entity);
154 while ((entity = media_entity_graph_walk_next(&graph))) {
155 if (entity == &video->video_dev.entity)
157 if (!is_media_entity_v4l2_io(entity))
159 far_end = to_vpfe_video(media_entity_to_video_device(entity));
160 if (far_end->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
161 pipe->inputs[pipe->input_num++] = far_end;
163 pipe->outputs[pipe->output_num++] = far_end;
165 media_entity_graph_walk_cleanup(&graph);
166 mutex_unlock(&mdev->graph_mutex);
171 /* update pipe state selected by user */
172 static int vpfe_update_pipe_state(struct vpfe_video_device *video)
174 struct vpfe_pipeline *pipe = &video->pipe;
177 ret = vpfe_prepare_pipeline(video);
182 * Find out if there is any input video
183 * if yes, it is single shot.
185 if (pipe->input_num == 0) {
186 pipe->state = VPFE_PIPELINE_STREAM_CONTINUOUS;
187 ret = vpfe_update_current_ext_subdev(video);
189 pr_err("Invalid external subdev\n");
193 pipe->state = VPFE_PIPELINE_STREAM_SINGLESHOT;
195 video->initialized = 1;
196 video->skip_frame_count = 1;
197 video->skip_frame_count_init = 1;
201 /* checks wether pipeline is ready for enabling */
202 int vpfe_video_is_pipe_ready(struct vpfe_pipeline *pipe)
206 for (i = 0; i < pipe->input_num; i++)
207 if (!pipe->inputs[i]->started ||
208 pipe->inputs[i]->state != VPFE_VIDEO_BUFFER_QUEUED)
210 for (i = 0; i < pipe->output_num; i++)
211 if (!pipe->outputs[i]->started ||
212 pipe->outputs[i]->state != VPFE_VIDEO_BUFFER_QUEUED)
218 * Validate a pipeline by checking both ends of all links for format
221 * Return 0 if all formats match, or -EPIPE if at least one link is found with
222 * different formats on its two ends.
224 static int vpfe_video_validate_pipeline(struct vpfe_pipeline *pipe)
226 struct v4l2_subdev_format fmt_source;
227 struct v4l2_subdev_format fmt_sink;
228 struct v4l2_subdev *subdev;
229 struct media_pad *pad;
233 * Should not matter if it is output[0] or 1 as
234 * the general ideas is to traverse backwards and
235 * the fact that the out video node always has the
236 * format of the connected pad.
238 subdev = vpfe_video_remote_subdev(pipe->outputs[0], NULL);
243 /* Retrieve the sink format */
244 pad = &subdev->entity.pads[0];
245 if (!(pad->flags & MEDIA_PAD_FL_SINK))
248 fmt_sink.which = V4L2_SUBDEV_FORMAT_ACTIVE;
249 fmt_sink.pad = pad->index;
250 ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL,
253 if (ret < 0 && ret != -ENOIOCTLCMD)
256 /* Retrieve the source format */
257 pad = media_entity_remote_pad(pad);
258 if (!pad || !is_media_entity_v4l2_subdev(pad->entity))
261 subdev = media_entity_to_v4l2_subdev(pad->entity);
263 fmt_source.which = V4L2_SUBDEV_FORMAT_ACTIVE;
264 fmt_source.pad = pad->index;
265 ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &fmt_source);
266 if (ret < 0 && ret != -ENOIOCTLCMD)
269 /* Check if the two ends match */
270 if (fmt_source.format.code != fmt_sink.format.code ||
271 fmt_source.format.width != fmt_sink.format.width ||
272 fmt_source.format.height != fmt_sink.format.height)
279 * vpfe_pipeline_enable() - Enable streaming on a pipeline
280 * @vpfe_dev: vpfe device
281 * @pipe: vpfe pipeline
283 * Walk the entities chain starting at the pipeline output video node and start
284 * all modules in the chain in the given mode.
286 * Return 0 if successful, or the return value of the failed video::s_stream
287 * operation otherwise.
289 static int vpfe_pipeline_enable(struct vpfe_pipeline *pipe)
291 struct media_entity *entity;
292 struct v4l2_subdev *subdev;
293 struct media_device *mdev;
296 if (pipe->state == VPFE_PIPELINE_STREAM_CONTINUOUS)
297 entity = vpfe_get_input_entity(pipe->outputs[0]);
299 entity = &pipe->inputs[0]->video_dev.entity;
301 mdev = entity->graph_obj.mdev;
302 mutex_lock(&mdev->graph_mutex);
303 ret = media_entity_graph_walk_init(&pipe->graph,
304 entity->graph_obj.mdev);
307 media_entity_graph_walk_start(&pipe->graph, entity);
308 while ((entity = media_entity_graph_walk_next(&pipe->graph))) {
310 if (!is_media_entity_v4l2_subdev(entity))
312 subdev = media_entity_to_v4l2_subdev(entity);
313 ret = v4l2_subdev_call(subdev, video, s_stream, 1);
314 if (ret < 0 && ret != -ENOIOCTLCMD)
319 media_entity_graph_walk_cleanup(&pipe->graph);
320 mutex_unlock(&mdev->graph_mutex);
325 * vpfe_pipeline_disable() - Disable streaming on a pipeline
326 * @vpfe_dev: vpfe device
327 * @pipe: VPFE pipeline
329 * Walk the entities chain starting at the pipeline output video node and stop
330 * all modules in the chain.
332 * Return 0 if all modules have been properly stopped, or -ETIMEDOUT if a module
335 static int vpfe_pipeline_disable(struct vpfe_pipeline *pipe)
337 struct media_entity *entity;
338 struct v4l2_subdev *subdev;
339 struct media_device *mdev;
342 if (pipe->state == VPFE_PIPELINE_STREAM_CONTINUOUS)
343 entity = vpfe_get_input_entity(pipe->outputs[0]);
345 entity = &pipe->inputs[0]->video_dev.entity;
347 mdev = entity->graph_obj.mdev;
348 mutex_lock(&mdev->graph_mutex);
349 media_entity_graph_walk_start(&pipe->graph, entity);
351 while ((entity = media_entity_graph_walk_next(&pipe->graph))) {
353 if (!is_media_entity_v4l2_subdev(entity))
355 subdev = media_entity_to_v4l2_subdev(entity);
356 ret = v4l2_subdev_call(subdev, video, s_stream, 0);
357 if (ret < 0 && ret != -ENOIOCTLCMD)
360 mutex_unlock(&mdev->graph_mutex);
362 media_entity_graph_walk_cleanup(&pipe->graph);
363 return ret ? -ETIMEDOUT : 0;
367 * vpfe_pipeline_set_stream() - Enable/disable streaming on a pipeline
368 * @vpfe_dev: VPFE device
369 * @pipe: VPFE pipeline
370 * @state: Stream state (stopped or active)
372 * Set the pipeline to the given stream state.
374 * Return 0 if successful, or the return value of the failed video::s_stream
375 * operation otherwise.
377 static int vpfe_pipeline_set_stream(struct vpfe_pipeline *pipe,
378 enum vpfe_pipeline_stream_state state)
380 if (state == VPFE_PIPELINE_STREAM_STOPPED)
381 return vpfe_pipeline_disable(pipe);
383 return vpfe_pipeline_enable(pipe);
386 static int all_videos_stopped(struct vpfe_video_device *video)
388 struct vpfe_pipeline *pipe = &video->pipe;
391 for (i = 0; i < pipe->input_num; i++)
392 if (pipe->inputs[i]->started)
394 for (i = 0; i < pipe->output_num; i++)
395 if (pipe->outputs[i]->started)
401 * vpfe_open() - open video device
402 * @file: file pointer
404 * initialize media pipeline state, allocate memory for file handle
406 * Return 0 if successful, or the return -ENODEV otherwise.
408 static int vpfe_open(struct file *file)
410 struct vpfe_video_device *video = video_drvdata(file);
411 struct vpfe_fh *handle;
413 /* Allocate memory for the file handle object */
414 handle = kzalloc(sizeof(struct vpfe_fh), GFP_KERNEL);
419 v4l2_fh_init(&handle->vfh, &video->video_dev);
420 v4l2_fh_add(&handle->vfh);
422 mutex_lock(&video->lock);
423 /* If decoder is not initialized. initialize it */
424 if (!video->initialized && vpfe_update_pipe_state(video)) {
425 mutex_unlock(&video->lock);
428 /* Increment device users counter */
430 /* Set io_allowed member to false */
431 handle->io_allowed = 0;
432 handle->video = video;
433 file->private_data = &handle->vfh;
434 mutex_unlock(&video->lock);
439 /* get the next buffer available from dma queue */
441 vpfe_video_get_next_buffer(struct vpfe_video_device *video)
443 video->cur_frm = video->next_frm =
444 list_entry(video->dma_queue.next,
445 struct vpfe_cap_buffer, list);
447 list_del(&video->next_frm->list);
448 video->next_frm->vb.vb2_buf.state = VB2_BUF_STATE_ACTIVE;
449 return vb2_dma_contig_plane_dma_addr(&video->next_frm->vb.vb2_buf, 0);
452 /* schedule the next buffer which is available on dma queue */
453 void vpfe_video_schedule_next_buffer(struct vpfe_video_device *video)
455 struct vpfe_device *vpfe_dev = video->vpfe_dev;
458 if (list_empty(&video->dma_queue))
461 video->next_frm = list_entry(video->dma_queue.next,
462 struct vpfe_cap_buffer, list);
464 if (video->pipe.state == VPFE_PIPELINE_STREAM_SINGLESHOT)
465 video->cur_frm = video->next_frm;
467 list_del(&video->next_frm->list);
468 video->next_frm->vb.vb2_buf.state = VB2_BUF_STATE_ACTIVE;
469 addr = vb2_dma_contig_plane_dma_addr(&video->next_frm->vb.vb2_buf, 0);
470 video->ops->queue(vpfe_dev, addr);
471 video->state = VPFE_VIDEO_BUFFER_QUEUED;
474 /* schedule the buffer for capturing bottom field */
475 void vpfe_video_schedule_bottom_field(struct vpfe_video_device *video)
477 struct vpfe_device *vpfe_dev = video->vpfe_dev;
480 addr = vb2_dma_contig_plane_dma_addr(&video->cur_frm->vb.vb2_buf, 0);
481 addr += video->field_off;
482 video->ops->queue(vpfe_dev, addr);
485 /* make buffer available for dequeue */
486 void vpfe_video_process_buffer_complete(struct vpfe_video_device *video)
488 struct vpfe_pipeline *pipe = &video->pipe;
490 video->cur_frm->vb.vb2_buf.timestamp = ktime_get_ns();
491 vb2_buffer_done(&video->cur_frm->vb.vb2_buf, VB2_BUF_STATE_DONE);
492 if (pipe->state == VPFE_PIPELINE_STREAM_CONTINUOUS)
493 video->cur_frm = video->next_frm;
496 /* vpfe_stop_capture() - stop streaming */
497 static void vpfe_stop_capture(struct vpfe_video_device *video)
499 struct vpfe_pipeline *pipe = &video->pipe;
503 if (video->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
505 if (all_videos_stopped(video))
506 vpfe_pipeline_set_stream(pipe,
507 VPFE_PIPELINE_STREAM_STOPPED);
511 * vpfe_release() - release video device
512 * @file: file pointer
514 * deletes buffer queue, frees the buffers and the vpfe file handle
518 static int vpfe_release(struct file *file)
520 struct vpfe_video_device *video = video_drvdata(file);
521 struct v4l2_fh *vfh = file->private_data;
522 struct vpfe_device *vpfe_dev = video->vpfe_dev;
523 struct vpfe_fh *fh = container_of(vfh, struct vpfe_fh, vfh);
525 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_release\n");
527 /* Get the device lock */
528 mutex_lock(&video->lock);
529 /* if this instance is doing IO */
530 if (fh->io_allowed) {
531 if (video->started) {
532 vpfe_stop_capture(video);
534 * mark pipe state as stopped in vpfe_release(),
535 * as app might call streamon() after streamoff()
536 * in which case driver has to start streaming.
538 video->pipe.state = VPFE_PIPELINE_STREAM_STOPPED;
539 vb2_streamoff(&video->buffer_queue,
540 video->buffer_queue.type);
543 /* Free buffers allocated */
544 vb2_queue_release(&video->buffer_queue);
545 vb2_dma_contig_cleanup_ctx(video->alloc_ctx);
547 /* Decrement device users counter */
549 v4l2_fh_del(&fh->vfh);
550 v4l2_fh_exit(&fh->vfh);
551 /* If this is the last file handle */
553 video->initialized = 0;
554 mutex_unlock(&video->lock);
555 file->private_data = NULL;
556 /* Free memory allocated to file handle object */
563 * vpfe_mmap() - It is used to map kernel space buffers
566 static int vpfe_mmap(struct file *file, struct vm_area_struct *vma)
568 struct vpfe_video_device *video = video_drvdata(file);
569 struct vpfe_device *vpfe_dev = video->vpfe_dev;
571 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_mmap\n");
572 return vb2_mmap(&video->buffer_queue, vma);
576 * vpfe_poll() - It is used for select/poll system call
578 static unsigned int vpfe_poll(struct file *file, poll_table *wait)
580 struct vpfe_video_device *video = video_drvdata(file);
581 struct vpfe_device *vpfe_dev = video->vpfe_dev;
583 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_poll\n");
585 return vb2_poll(&video->buffer_queue, file, wait);
589 /* vpfe capture driver file operations */
590 static const struct v4l2_file_operations vpfe_fops = {
591 .owner = THIS_MODULE,
593 .release = vpfe_release,
594 .unlocked_ioctl = video_ioctl2,
600 * vpfe_querycap() - query capabilities of video device
601 * @file: file pointer
602 * @priv: void pointer
603 * @cap: pointer to v4l2_capability structure
605 * fills v4l2 capabilities structure
609 static int vpfe_querycap(struct file *file, void *priv,
610 struct v4l2_capability *cap)
612 struct vpfe_video_device *video = video_drvdata(file);
613 struct vpfe_device *vpfe_dev = video->vpfe_dev;
615 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_querycap\n");
617 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
618 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
620 cap->device_caps = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_STREAMING;
621 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VIDEO_OUTPUT |
622 V4L2_CAP_STREAMING | V4L2_CAP_DEVICE_CAPS;
623 strlcpy(cap->driver, CAPTURE_DRV_NAME, sizeof(cap->driver));
624 strlcpy(cap->bus_info, "VPFE", sizeof(cap->bus_info));
625 strlcpy(cap->card, vpfe_dev->cfg->card_name, sizeof(cap->card));
631 * vpfe_g_fmt() - get the format which is active on video device
632 * @file: file pointer
633 * @priv: void pointer
634 * @fmt: pointer to v4l2_format structure
636 * fills v4l2 format structure with active format
640 static int vpfe_g_fmt(struct file *file, void *priv,
641 struct v4l2_format *fmt)
643 struct vpfe_video_device *video = video_drvdata(file);
644 struct vpfe_device *vpfe_dev = video->vpfe_dev;
646 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_g_fmt\n");
647 /* Fill in the information about format */
653 * vpfe_enum_fmt() - enum formats supported on media chain
654 * @file: file pointer
655 * @priv: void pointer
656 * @fmt: pointer to v4l2_fmtdesc structure
658 * fills v4l2_fmtdesc structure with output format set on adjacent subdev,
659 * only one format is enumearted as subdevs are already configured
661 * Return 0 if successful, error code otherwise
663 static int vpfe_enum_fmt(struct file *file, void *priv,
664 struct v4l2_fmtdesc *fmt)
666 struct vpfe_video_device *video = video_drvdata(file);
667 struct vpfe_device *vpfe_dev = video->vpfe_dev;
668 struct v4l2_subdev_format sd_fmt;
669 struct v4l2_mbus_framefmt mbus;
670 struct v4l2_subdev *subdev;
671 struct v4l2_format format;
672 struct media_pad *remote;
675 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_enum_fmt\n");
678 * since already subdev pad format is set,
679 * only one pixel format is available
681 if (fmt->index > 0) {
682 v4l2_err(&vpfe_dev->v4l2_dev, "Invalid index\n");
685 /* get the remote pad */
686 remote = media_entity_remote_pad(&video->pad);
687 if (remote == NULL) {
688 v4l2_err(&vpfe_dev->v4l2_dev,
689 "invalid remote pad for video node\n");
692 /* get the remote subdev */
693 subdev = vpfe_video_remote_subdev(video, NULL);
694 if (subdev == NULL) {
695 v4l2_err(&vpfe_dev->v4l2_dev,
696 "invalid remote subdev for video node\n");
699 sd_fmt.pad = remote->index;
700 sd_fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
701 /* get output format of remote subdev */
702 ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &sd_fmt);
704 v4l2_err(&vpfe_dev->v4l2_dev,
705 "invalid remote subdev for video node\n");
708 /* convert to pix format */
709 mbus.code = sd_fmt.format.code;
710 mbus_to_pix(&mbus, &format.fmt.pix);
711 /* copy the result */
712 fmt->pixelformat = format.fmt.pix.pixelformat;
718 * vpfe_s_fmt() - set the format on video device
719 * @file: file pointer
720 * @priv: void pointer
721 * @fmt: pointer to v4l2_format structure
723 * validate and set the format on video device
725 * Return 0 on success, error code otherwise
727 static int vpfe_s_fmt(struct file *file, void *priv,
728 struct v4l2_format *fmt)
730 struct vpfe_video_device *video = video_drvdata(file);
731 struct vpfe_device *vpfe_dev = video->vpfe_dev;
732 struct v4l2_format format;
735 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_s_fmt\n");
736 /* If streaming is started, return error */
737 if (video->started) {
738 v4l2_err(&vpfe_dev->v4l2_dev, "Streaming is started\n");
741 /* get adjacent subdev's output pad format */
742 ret = __vpfe_video_get_format(video, &format);
751 * vpfe_try_fmt() - try the format on video device
752 * @file: file pointer
753 * @priv: void pointer
754 * @fmt: pointer to v4l2_format structure
756 * validate the format, update with correct format
757 * based on output format set on adjacent subdev
759 * Return 0 on success, error code otherwise
761 static int vpfe_try_fmt(struct file *file, void *priv,
762 struct v4l2_format *fmt)
764 struct vpfe_video_device *video = video_drvdata(file);
765 struct vpfe_device *vpfe_dev = video->vpfe_dev;
766 struct v4l2_format format;
769 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_try_fmt\n");
770 /* get adjacent subdev's output pad format */
771 ret = __vpfe_video_get_format(video, &format);
780 * vpfe_enum_input() - enum inputs supported on media chain
781 * @file: file pointer
782 * @priv: void pointer
783 * @fmt: pointer to v4l2_fmtdesc structure
785 * fills v4l2_input structure with input available on media chain,
786 * only one input is enumearted as media chain is setup by this time
788 * Return 0 if successful, -EINVAL is media chain is invalid
790 static int vpfe_enum_input(struct file *file, void *priv,
791 struct v4l2_input *inp)
793 struct vpfe_video_device *video = video_drvdata(file);
794 struct vpfe_ext_subdev_info *sdinfo = video->current_ext_subdev;
795 struct vpfe_device *vpfe_dev = video->vpfe_dev;
797 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_enum_input\n");
798 /* enumerate from the subdev user has chosen through mc */
799 if (inp->index < sdinfo->num_inputs) {
800 memcpy(inp, &sdinfo->inputs[inp->index],
801 sizeof(struct v4l2_input));
808 * vpfe_g_input() - get index of the input which is active
809 * @file: file pointer
810 * @priv: void pointer
811 * @index: pointer to unsigned int
813 * set index with input index which is active
815 static int vpfe_g_input(struct file *file, void *priv, unsigned int *index)
817 struct vpfe_video_device *video = video_drvdata(file);
818 struct vpfe_device *vpfe_dev = video->vpfe_dev;
820 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_g_input\n");
822 *index = video->current_input;
827 * vpfe_s_input() - set input which is pointed by input index
828 * @file: file pointer
829 * @priv: void pointer
830 * @index: pointer to unsigned int
832 * set input on external subdev
834 * Return 0 on success, error code otherwise
836 static int vpfe_s_input(struct file *file, void *priv, unsigned int index)
838 struct vpfe_video_device *video = video_drvdata(file);
839 struct vpfe_device *vpfe_dev = video->vpfe_dev;
840 struct vpfe_ext_subdev_info *sdinfo;
841 struct vpfe_route *route;
842 struct v4l2_input *inps;
848 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_s_input\n");
850 ret = mutex_lock_interruptible(&video->lock);
854 * If streaming is started return device busy
857 if (video->started) {
858 v4l2_err(&vpfe_dev->v4l2_dev, "Streaming is on\n");
863 sdinfo = video->current_ext_subdev;
864 if (!sdinfo->registered) {
868 if (vpfe_dev->cfg->setup_input &&
869 vpfe_dev->cfg->setup_input(sdinfo->grp_id) < 0) {
871 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev,
872 "couldn't setup input for %s\n",
873 sdinfo->module_name);
876 route = &sdinfo->routes[index];
877 if (route && sdinfo->can_route) {
878 input = route->input;
879 output = route->output;
880 ret = v4l2_device_call_until_err(&vpfe_dev->v4l2_dev,
881 sdinfo->grp_id, video,
882 s_routing, input, output, 0);
884 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev,
885 "s_input:error in setting input in decoder\n");
890 /* set standards set by subdev in video device */
891 for (i = 0; i < sdinfo->num_inputs; i++) {
892 inps = &sdinfo->inputs[i];
893 video->video_dev.tvnorms |= inps->std;
895 video->current_input = index;
897 mutex_unlock(&video->lock);
902 * vpfe_querystd() - query std which is being input on external subdev
903 * @file: file pointer
904 * @priv: void pointer
905 * @std_id: pointer to v4l2_std_id structure
907 * call external subdev through v4l2_device_call_until_err to
908 * get the std that is being active.
910 * Return 0 on success, error code otherwise
912 static int vpfe_querystd(struct file *file, void *priv, v4l2_std_id *std_id)
914 struct vpfe_video_device *video = video_drvdata(file);
915 struct vpfe_device *vpfe_dev = video->vpfe_dev;
916 struct vpfe_ext_subdev_info *sdinfo;
919 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_querystd\n");
921 ret = mutex_lock_interruptible(&video->lock);
922 sdinfo = video->current_ext_subdev;
925 /* Call querystd function of decoder device */
926 ret = v4l2_device_call_until_err(&vpfe_dev->v4l2_dev, sdinfo->grp_id,
927 video, querystd, std_id);
928 mutex_unlock(&video->lock);
933 * vpfe_s_std() - set std on external subdev
934 * @file: file pointer
935 * @priv: void pointer
936 * @std_id: pointer to v4l2_std_id structure
938 * set std pointed by std_id on external subdev by calling it using
939 * v4l2_device_call_until_err
941 * Return 0 on success, error code otherwise
943 static int vpfe_s_std(struct file *file, void *priv, v4l2_std_id std_id)
945 struct vpfe_video_device *video = video_drvdata(file);
946 struct vpfe_device *vpfe_dev = video->vpfe_dev;
947 struct vpfe_ext_subdev_info *sdinfo;
950 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_s_std\n");
952 /* Call decoder driver function to set the standard */
953 ret = mutex_lock_interruptible(&video->lock);
956 sdinfo = video->current_ext_subdev;
957 /* If streaming is started, return device busy error */
958 if (video->started) {
959 v4l2_err(&vpfe_dev->v4l2_dev, "streaming is started\n");
963 ret = v4l2_device_call_until_err(&vpfe_dev->v4l2_dev, sdinfo->grp_id,
964 video, s_std, std_id);
966 v4l2_err(&vpfe_dev->v4l2_dev, "Failed to set standard\n");
967 video->stdid = V4L2_STD_UNKNOWN;
970 video->stdid = std_id;
972 mutex_unlock(&video->lock);
976 static int vpfe_g_std(struct file *file, void *priv, v4l2_std_id *tvnorm)
978 struct vpfe_video_device *video = video_drvdata(file);
979 struct vpfe_device *vpfe_dev = video->vpfe_dev;
981 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_g_std\n");
982 *tvnorm = video->stdid;
987 * vpfe_enum_dv_timings() - enumerate dv_timings which are supported by
989 * @file: file pointer
990 * @priv: void pointer
991 * @timings: pointer to v4l2_enum_dv_timings structure
993 * enum dv_timings's which are supported by external subdev through
996 * Return 0 on success, error code otherwise
999 vpfe_enum_dv_timings(struct file *file, void *fh,
1000 struct v4l2_enum_dv_timings *timings)
1002 struct vpfe_video_device *video = video_drvdata(file);
1003 struct vpfe_device *vpfe_dev = video->vpfe_dev;
1004 struct v4l2_subdev *subdev = video->current_ext_subdev->subdev;
1008 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_enum_dv_timings\n");
1009 return v4l2_subdev_call(subdev, pad, enum_dv_timings, timings);
1013 * vpfe_query_dv_timings() - query the dv_timings which is being input
1014 * to external subdev
1015 * @file: file pointer
1016 * @priv: void pointer
1017 * @timings: pointer to v4l2_dv_timings structure
1019 * get dv_timings which is being input on external subdev through
1022 * Return 0 on success, error code otherwise
1025 vpfe_query_dv_timings(struct file *file, void *fh,
1026 struct v4l2_dv_timings *timings)
1028 struct vpfe_video_device *video = video_drvdata(file);
1029 struct vpfe_device *vpfe_dev = video->vpfe_dev;
1030 struct v4l2_subdev *subdev = video->current_ext_subdev->subdev;
1032 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_query_dv_timings\n");
1033 return v4l2_subdev_call(subdev, video, query_dv_timings, timings);
1037 * vpfe_s_dv_timings() - set dv_timings on external subdev
1038 * @file: file pointer
1039 * @priv: void pointer
1040 * @timings: pointer to v4l2_dv_timings structure
1042 * set dv_timings pointed by timings on external subdev through
1043 * v4l2_device_call_until_err, this configures amplifier also
1045 * Return 0 on success, error code otherwise
1048 vpfe_s_dv_timings(struct file *file, void *fh,
1049 struct v4l2_dv_timings *timings)
1051 struct vpfe_video_device *video = video_drvdata(file);
1052 struct vpfe_device *vpfe_dev = video->vpfe_dev;
1054 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_s_dv_timings\n");
1056 video->stdid = V4L2_STD_UNKNOWN;
1057 return v4l2_device_call_until_err(&vpfe_dev->v4l2_dev,
1058 video->current_ext_subdev->grp_id,
1059 video, s_dv_timings, timings);
1063 * vpfe_g_dv_timings() - get dv_timings which is set on external subdev
1064 * @file: file pointer
1065 * @priv: void pointer
1066 * @timings: pointer to v4l2_dv_timings structure
1068 * get dv_timings which is set on external subdev through
1071 * Return 0 on success, error code otherwise
1074 vpfe_g_dv_timings(struct file *file, void *fh,
1075 struct v4l2_dv_timings *timings)
1077 struct vpfe_video_device *video = video_drvdata(file);
1078 struct vpfe_device *vpfe_dev = video->vpfe_dev;
1079 struct v4l2_subdev *subdev = video->current_ext_subdev->subdev;
1081 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_g_dv_timings\n");
1082 return v4l2_subdev_call(subdev, video, g_dv_timings, timings);
1086 * Videobuf operations
1089 * vpfe_buffer_queue_setup : Callback function for buffer setup.
1090 * @vq: vb2_queue ptr
1092 * @nbuffers: ptr to number of buffers requested by application
1093 * @nplanes:: contains number of distinct video planes needed to hold a frame
1094 * @sizes[]: contains the size (in bytes) of each plane.
1095 * @alloc_ctxs: ptr to allocation context
1097 * This callback function is called when reqbuf() is called to adjust
1098 * the buffer nbuffers and buffer size
1101 vpfe_buffer_queue_setup(struct vb2_queue *vq,
1102 unsigned int *nbuffers, unsigned int *nplanes,
1103 unsigned int sizes[], void *alloc_ctxs[])
1105 struct vpfe_fh *fh = vb2_get_drv_priv(vq);
1106 struct vpfe_video_device *video = fh->video;
1107 struct vpfe_device *vpfe_dev = video->vpfe_dev;
1110 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_buffer_queue_setup\n");
1111 size = video->fmt.fmt.pix.sizeimage;
1113 if (vq->num_buffers + *nbuffers < 3)
1114 *nbuffers = 3 - vq->num_buffers;
1118 alloc_ctxs[0] = video->alloc_ctx;
1119 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev,
1120 "nbuffers=%d, size=%lu\n", *nbuffers, size);
1125 * vpfe_buffer_prepare : callback function for buffer prepare
1126 * @vb: ptr to vb2_buffer
1128 * This is the callback function for buffer prepare when vb2_qbuf()
1129 * function is called. The buffer is prepared and user space virtual address
1130 * or user address is converted into physical address
1132 static int vpfe_buffer_prepare(struct vb2_buffer *vb)
1134 struct vpfe_fh *fh = vb2_get_drv_priv(vb->vb2_queue);
1135 struct vpfe_video_device *video = fh->video;
1136 struct vpfe_device *vpfe_dev = video->vpfe_dev;
1139 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_buffer_prepare\n");
1141 if (vb->state != VB2_BUF_STATE_ACTIVE &&
1142 vb->state != VB2_BUF_STATE_PREPARED)
1145 /* Initialize buffer */
1146 vb2_set_plane_payload(vb, 0, video->fmt.fmt.pix.sizeimage);
1147 if (vb2_plane_vaddr(vb, 0) &&
1148 vb2_get_plane_payload(vb, 0) > vb2_plane_size(vb, 0))
1151 addr = vb2_dma_contig_plane_dma_addr(vb, 0);
1152 /* Make sure user addresses are aligned to 32 bytes */
1153 if (!ALIGN(addr, 32))
1159 static void vpfe_buffer_queue(struct vb2_buffer *vb)
1161 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
1162 /* Get the file handle object and device object */
1163 struct vpfe_fh *fh = vb2_get_drv_priv(vb->vb2_queue);
1164 struct vpfe_video_device *video = fh->video;
1165 struct vpfe_device *vpfe_dev = video->vpfe_dev;
1166 struct vpfe_pipeline *pipe = &video->pipe;
1167 struct vpfe_cap_buffer *buf = container_of(vbuf,
1168 struct vpfe_cap_buffer, vb);
1169 unsigned long flags;
1170 unsigned long empty;
1173 spin_lock_irqsave(&video->dma_queue_lock, flags);
1174 empty = list_empty(&video->dma_queue);
1175 /* add the buffer to the DMA queue */
1176 list_add_tail(&buf->list, &video->dma_queue);
1177 spin_unlock_irqrestore(&video->dma_queue_lock, flags);
1178 /* this case happens in case of single shot */
1179 if (empty && video->started && pipe->state ==
1180 VPFE_PIPELINE_STREAM_SINGLESHOT &&
1181 video->state == VPFE_VIDEO_BUFFER_NOT_QUEUED) {
1182 spin_lock(&video->dma_queue_lock);
1183 addr = vpfe_video_get_next_buffer(video);
1184 video->ops->queue(vpfe_dev, addr);
1186 video->state = VPFE_VIDEO_BUFFER_QUEUED;
1187 spin_unlock(&video->dma_queue_lock);
1189 /* enable h/w each time in single shot */
1190 if (vpfe_video_is_pipe_ready(pipe))
1191 vpfe_pipeline_set_stream(pipe,
1192 VPFE_PIPELINE_STREAM_SINGLESHOT);
1196 /* vpfe_start_capture() - start streaming on all the subdevs */
1197 static int vpfe_start_capture(struct vpfe_video_device *video)
1199 struct vpfe_pipeline *pipe = &video->pipe;
1203 if (vpfe_video_is_pipe_ready(pipe))
1204 ret = vpfe_pipeline_set_stream(pipe, pipe->state);
1209 static int vpfe_start_streaming(struct vb2_queue *vq, unsigned int count)
1211 struct vpfe_fh *fh = vb2_get_drv_priv(vq);
1212 struct vpfe_video_device *video = fh->video;
1213 struct vpfe_device *vpfe_dev = video->vpfe_dev;
1217 ret = mutex_lock_interruptible(&video->lock);
1221 /* Get the next frame from the buffer queue */
1222 video->cur_frm = video->next_frm =
1223 list_entry(video->dma_queue.next, struct vpfe_cap_buffer, list);
1224 /* Remove buffer from the buffer queue */
1225 list_del(&video->cur_frm->list);
1226 /* Mark state of the current frame to active */
1227 video->cur_frm->vb.vb2_buf.state = VB2_BUF_STATE_ACTIVE;
1228 /* Initialize field_id and started member */
1229 video->field_id = 0;
1230 addr = vb2_dma_contig_plane_dma_addr(&video->cur_frm->vb.vb2_buf, 0);
1231 video->ops->queue(vpfe_dev, addr);
1232 video->state = VPFE_VIDEO_BUFFER_QUEUED;
1234 ret = vpfe_start_capture(video);
1236 struct vpfe_cap_buffer *buf, *tmp;
1238 vb2_buffer_done(&video->cur_frm->vb.vb2_buf,
1239 VB2_BUF_STATE_QUEUED);
1240 list_for_each_entry_safe(buf, tmp, &video->dma_queue, list) {
1241 list_del(&buf->list);
1242 vb2_buffer_done(&buf->vb.vb2_buf,
1243 VB2_BUF_STATE_QUEUED);
1248 mutex_unlock(&video->lock);
1252 mutex_unlock(&video->lock);
1254 ret = vb2_streamoff(&video->buffer_queue, video->buffer_queue.type);
1258 static int vpfe_buffer_init(struct vb2_buffer *vb)
1260 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
1261 struct vpfe_cap_buffer *buf = container_of(vbuf,
1262 struct vpfe_cap_buffer, vb);
1264 INIT_LIST_HEAD(&buf->list);
1268 /* abort streaming and wait for last buffer */
1269 static void vpfe_stop_streaming(struct vb2_queue *vq)
1271 struct vpfe_fh *fh = vb2_get_drv_priv(vq);
1272 struct vpfe_video_device *video = fh->video;
1274 /* release all active buffers */
1275 if (video->cur_frm == video->next_frm) {
1276 vb2_buffer_done(&video->cur_frm->vb.vb2_buf,
1277 VB2_BUF_STATE_ERROR);
1279 if (video->cur_frm != NULL)
1280 vb2_buffer_done(&video->cur_frm->vb.vb2_buf,
1281 VB2_BUF_STATE_ERROR);
1282 if (video->next_frm != NULL)
1283 vb2_buffer_done(&video->next_frm->vb.vb2_buf,
1284 VB2_BUF_STATE_ERROR);
1287 while (!list_empty(&video->dma_queue)) {
1288 video->next_frm = list_entry(video->dma_queue.next,
1289 struct vpfe_cap_buffer, list);
1290 list_del(&video->next_frm->list);
1291 vb2_buffer_done(&video->next_frm->vb.vb2_buf,
1292 VB2_BUF_STATE_ERROR);
1296 static void vpfe_buf_cleanup(struct vb2_buffer *vb)
1298 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
1299 struct vpfe_fh *fh = vb2_get_drv_priv(vb->vb2_queue);
1300 struct vpfe_video_device *video = fh->video;
1301 struct vpfe_device *vpfe_dev = video->vpfe_dev;
1302 struct vpfe_cap_buffer *buf = container_of(vbuf,
1303 struct vpfe_cap_buffer, vb);
1305 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_buf_cleanup\n");
1306 if (vb->state == VB2_BUF_STATE_ACTIVE)
1307 list_del_init(&buf->list);
1310 static struct vb2_ops video_qops = {
1311 .queue_setup = vpfe_buffer_queue_setup,
1312 .buf_init = vpfe_buffer_init,
1313 .buf_prepare = vpfe_buffer_prepare,
1314 .start_streaming = vpfe_start_streaming,
1315 .stop_streaming = vpfe_stop_streaming,
1316 .buf_cleanup = vpfe_buf_cleanup,
1317 .buf_queue = vpfe_buffer_queue,
1321 * vpfe_reqbufs() - supported REQBUF only once opening
1324 static int vpfe_reqbufs(struct file *file, void *priv,
1325 struct v4l2_requestbuffers *req_buf)
1327 struct vpfe_video_device *video = video_drvdata(file);
1328 struct vpfe_device *vpfe_dev = video->vpfe_dev;
1329 struct vpfe_fh *fh = file->private_data;
1330 struct vb2_queue *q;
1333 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_reqbufs\n");
1335 if (req_buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1336 req_buf->type != V4L2_BUF_TYPE_VIDEO_OUTPUT){
1337 v4l2_err(&vpfe_dev->v4l2_dev, "Invalid buffer type\n");
1341 ret = mutex_lock_interruptible(&video->lock);
1345 if (video->io_usrs != 0) {
1346 v4l2_err(&vpfe_dev->v4l2_dev, "Only one IO user allowed\n");
1350 video->memory = req_buf->memory;
1352 /* Initialize videobuf2 queue as per the buffer type */
1353 video->alloc_ctx = vb2_dma_contig_init_ctx(vpfe_dev->pdev);
1354 if (IS_ERR(video->alloc_ctx)) {
1355 v4l2_err(&vpfe_dev->v4l2_dev, "Failed to get the context\n");
1356 return PTR_ERR(video->alloc_ctx);
1359 q = &video->buffer_queue;
1360 q->type = req_buf->type;
1361 q->io_modes = VB2_MMAP | VB2_USERPTR;
1363 q->min_buffers_needed = 1;
1364 q->ops = &video_qops;
1365 q->mem_ops = &vb2_dma_contig_memops;
1366 q->buf_struct_size = sizeof(struct vpfe_cap_buffer);
1367 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1369 ret = vb2_queue_init(q);
1371 v4l2_err(&vpfe_dev->v4l2_dev, "vb2_queue_init() failed\n");
1372 vb2_dma_contig_cleanup_ctx(vpfe_dev->pdev);
1378 INIT_LIST_HEAD(&video->dma_queue);
1379 ret = vb2_reqbufs(&video->buffer_queue, req_buf);
1382 mutex_unlock(&video->lock);
1387 * vpfe_querybuf() - query buffers for exchange
1389 static int vpfe_querybuf(struct file *file, void *priv,
1390 struct v4l2_buffer *buf)
1392 struct vpfe_video_device *video = video_drvdata(file);
1393 struct vpfe_device *vpfe_dev = video->vpfe_dev;
1395 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_querybuf\n");
1397 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1398 buf->type != V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1399 v4l2_err(&vpfe_dev->v4l2_dev, "Invalid buf type\n");
1403 if (video->memory != V4L2_MEMORY_MMAP) {
1404 v4l2_err(&vpfe_dev->v4l2_dev, "Invalid memory\n");
1408 /* Call vb2_querybuf to get information */
1409 return vb2_querybuf(&video->buffer_queue, buf);
1413 * vpfe_qbuf() - queue buffers for capture or processing
1415 static int vpfe_qbuf(struct file *file, void *priv,
1416 struct v4l2_buffer *p)
1418 struct vpfe_video_device *video = video_drvdata(file);
1419 struct vpfe_device *vpfe_dev = video->vpfe_dev;
1420 struct vpfe_fh *fh = file->private_data;
1422 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_qbuf\n");
1424 if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1425 p->type != V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1426 v4l2_err(&vpfe_dev->v4l2_dev, "Invalid buf type\n");
1430 * If this file handle is not allowed to do IO,
1433 if (!fh->io_allowed) {
1434 v4l2_err(&vpfe_dev->v4l2_dev, "fh->io_allowed\n");
1438 return vb2_qbuf(&video->buffer_queue, p);
1442 * vpfe_dqbuf() - deque buffer which is done with processing
1444 static int vpfe_dqbuf(struct file *file, void *priv,
1445 struct v4l2_buffer *buf)
1447 struct vpfe_video_device *video = video_drvdata(file);
1448 struct vpfe_device *vpfe_dev = video->vpfe_dev;
1450 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_dqbuf\n");
1452 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1453 buf->type != V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1454 v4l2_err(&vpfe_dev->v4l2_dev, "Invalid buf type\n");
1458 return vb2_dqbuf(&video->buffer_queue,
1459 buf, (file->f_flags & O_NONBLOCK));
1463 * vpfe_streamon() - start streaming
1464 * @file: file pointer
1465 * @priv: void pointer
1466 * @buf_type: enum v4l2_buf_type
1468 * queue buffer onto hardware for capture/processing and
1469 * start all the subdevs which are in media chain
1471 * Return 0 on success, error code otherwise
1473 static int vpfe_streamon(struct file *file, void *priv,
1474 enum v4l2_buf_type buf_type)
1476 struct vpfe_video_device *video = video_drvdata(file);
1477 struct vpfe_device *vpfe_dev = video->vpfe_dev;
1478 struct vpfe_pipeline *pipe = &video->pipe;
1479 struct vpfe_fh *fh = file->private_data;
1480 struct vpfe_ext_subdev_info *sdinfo;
1483 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_streamon\n");
1485 if (buf_type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1486 buf_type != V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1487 v4l2_err(&vpfe_dev->v4l2_dev, "Invalid buf type\n");
1490 /* If file handle is not allowed IO, return error */
1491 if (!fh->io_allowed) {
1492 v4l2_err(&vpfe_dev->v4l2_dev, "fh->io_allowed\n");
1495 sdinfo = video->current_ext_subdev;
1496 /* If buffer queue is empty, return error */
1497 if (list_empty(&video->buffer_queue.queued_list)) {
1498 v4l2_err(&vpfe_dev->v4l2_dev, "buffer queue is empty\n");
1501 /* Validate the pipeline */
1502 if (buf_type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1503 ret = vpfe_video_validate_pipeline(pipe);
1507 /* Call vb2_streamon to start streaming */
1508 return vb2_streamon(&video->buffer_queue, buf_type);
1512 * vpfe_streamoff() - stop streaming
1513 * @file: file pointer
1514 * @priv: void pointer
1515 * @buf_type: enum v4l2_buf_type
1517 * stop all the subdevs which are in media chain
1519 * Return 0 on success, error code otherwise
1521 static int vpfe_streamoff(struct file *file, void *priv,
1522 enum v4l2_buf_type buf_type)
1524 struct vpfe_video_device *video = video_drvdata(file);
1525 struct vpfe_device *vpfe_dev = video->vpfe_dev;
1526 struct vpfe_fh *fh = file->private_data;
1529 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_streamoff\n");
1531 if (buf_type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1532 buf_type != V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1533 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "Invalid buf type\n");
1537 /* If io is allowed for this file handle, return error */
1538 if (!fh->io_allowed) {
1539 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "fh->io_allowed\n");
1543 /* If streaming is not started, return error */
1544 if (!video->started) {
1545 v4l2_err(&vpfe_dev->v4l2_dev, "device is not started\n");
1549 ret = mutex_lock_interruptible(&video->lock);
1553 vpfe_stop_capture(video);
1554 ret = vb2_streamoff(&video->buffer_queue, buf_type);
1555 mutex_unlock(&video->lock);
1560 /* vpfe capture ioctl operations */
1561 static const struct v4l2_ioctl_ops vpfe_ioctl_ops = {
1562 .vidioc_querycap = vpfe_querycap,
1563 .vidioc_g_fmt_vid_cap = vpfe_g_fmt,
1564 .vidioc_s_fmt_vid_cap = vpfe_s_fmt,
1565 .vidioc_try_fmt_vid_cap = vpfe_try_fmt,
1566 .vidioc_enum_fmt_vid_cap = vpfe_enum_fmt,
1567 .vidioc_g_fmt_vid_out = vpfe_g_fmt,
1568 .vidioc_s_fmt_vid_out = vpfe_s_fmt,
1569 .vidioc_try_fmt_vid_out = vpfe_try_fmt,
1570 .vidioc_enum_fmt_vid_out = vpfe_enum_fmt,
1571 .vidioc_enum_input = vpfe_enum_input,
1572 .vidioc_g_input = vpfe_g_input,
1573 .vidioc_s_input = vpfe_s_input,
1574 .vidioc_querystd = vpfe_querystd,
1575 .vidioc_s_std = vpfe_s_std,
1576 .vidioc_g_std = vpfe_g_std,
1577 .vidioc_enum_dv_timings = vpfe_enum_dv_timings,
1578 .vidioc_query_dv_timings = vpfe_query_dv_timings,
1579 .vidioc_s_dv_timings = vpfe_s_dv_timings,
1580 .vidioc_g_dv_timings = vpfe_g_dv_timings,
1581 .vidioc_reqbufs = vpfe_reqbufs,
1582 .vidioc_querybuf = vpfe_querybuf,
1583 .vidioc_qbuf = vpfe_qbuf,
1584 .vidioc_dqbuf = vpfe_dqbuf,
1585 .vidioc_streamon = vpfe_streamon,
1586 .vidioc_streamoff = vpfe_streamoff,
1589 /* VPFE video init function */
1590 int vpfe_video_init(struct vpfe_video_device *video, const char *name)
1592 const char *direction;
1595 switch (video->type) {
1596 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1597 direction = "output";
1598 video->pad.flags = MEDIA_PAD_FL_SINK;
1599 video->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1602 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
1603 direction = "input";
1604 video->pad.flags = MEDIA_PAD_FL_SOURCE;
1605 video->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
1611 /* Initialize field of video device */
1612 video->video_dev.release = video_device_release;
1613 video->video_dev.fops = &vpfe_fops;
1614 video->video_dev.ioctl_ops = &vpfe_ioctl_ops;
1615 video->video_dev.minor = -1;
1616 video->video_dev.tvnorms = 0;
1617 snprintf(video->video_dev.name, sizeof(video->video_dev.name),
1618 "DAVINCI VIDEO %s %s", name, direction);
1620 spin_lock_init(&video->irqlock);
1621 spin_lock_init(&video->dma_queue_lock);
1622 mutex_init(&video->lock);
1623 ret = media_entity_pads_init(&video->video_dev.entity,
1628 video_set_drvdata(&video->video_dev, video);
1633 /* vpfe video device register function */
1634 int vpfe_video_register(struct vpfe_video_device *video,
1635 struct v4l2_device *vdev)
1639 video->video_dev.v4l2_dev = vdev;
1641 ret = video_register_device(&video->video_dev, VFL_TYPE_GRABBER, -1);
1643 pr_err("%s: could not register video device (%d)\n",
1648 /* vpfe video device unregister function */
1649 void vpfe_video_unregister(struct vpfe_video_device *video)
1651 if (video_is_registered(&video->video_dev)) {
1652 video_unregister_device(&video->video_dev);
1653 media_entity_cleanup(&video->video_dev.entity);