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_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_graph_walk_init(&graph, mdev);
150 mutex_unlock(&mdev->graph_mutex);
153 media_graph_walk_start(&graph, entity);
154 while ((entity = media_graph_walk_next(&graph))) {
155 if (entity == &video->video_dev.entity)
157 if (!is_media_entity_v4l2_video_device(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_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 whether 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_graph_walk_init(&pipe->graph, mdev);
306 media_graph_walk_start(&pipe->graph, entity);
307 while ((entity = media_graph_walk_next(&pipe->graph))) {
309 if (!is_media_entity_v4l2_subdev(entity))
311 subdev = media_entity_to_v4l2_subdev(entity);
312 ret = v4l2_subdev_call(subdev, video, s_stream, 1);
313 if (ret < 0 && ret != -ENOIOCTLCMD)
318 media_graph_walk_cleanup(&pipe->graph);
319 mutex_unlock(&mdev->graph_mutex);
324 * vpfe_pipeline_disable() - Disable streaming on a pipeline
325 * @vpfe_dev: vpfe device
326 * @pipe: VPFE pipeline
328 * Walk the entities chain starting at the pipeline output video node and stop
329 * all modules in the chain.
331 * Return 0 if all modules have been properly stopped, or -ETIMEDOUT if a module
334 static int vpfe_pipeline_disable(struct vpfe_pipeline *pipe)
336 struct media_entity *entity;
337 struct v4l2_subdev *subdev;
338 struct media_device *mdev;
341 if (pipe->state == VPFE_PIPELINE_STREAM_CONTINUOUS)
342 entity = vpfe_get_input_entity(pipe->outputs[0]);
344 entity = &pipe->inputs[0]->video_dev.entity;
346 mdev = entity->graph_obj.mdev;
347 mutex_lock(&mdev->graph_mutex);
348 media_graph_walk_start(&pipe->graph, entity);
350 while ((entity = media_graph_walk_next(&pipe->graph))) {
352 if (!is_media_entity_v4l2_subdev(entity))
354 subdev = media_entity_to_v4l2_subdev(entity);
355 ret = v4l2_subdev_call(subdev, video, s_stream, 0);
356 if (ret < 0 && ret != -ENOIOCTLCMD)
359 mutex_unlock(&mdev->graph_mutex);
361 media_graph_walk_cleanup(&pipe->graph);
362 return ret ? -ETIMEDOUT : 0;
366 * vpfe_pipeline_set_stream() - Enable/disable streaming on a pipeline
367 * @vpfe_dev: VPFE device
368 * @pipe: VPFE pipeline
369 * @state: Stream state (stopped or active)
371 * Set the pipeline to the given stream state.
373 * Return 0 if successful, or the return value of the failed video::s_stream
374 * operation otherwise.
376 static int vpfe_pipeline_set_stream(struct vpfe_pipeline *pipe,
377 enum vpfe_pipeline_stream_state state)
379 if (state == VPFE_PIPELINE_STREAM_STOPPED)
380 return vpfe_pipeline_disable(pipe);
382 return vpfe_pipeline_enable(pipe);
385 static int all_videos_stopped(struct vpfe_video_device *video)
387 struct vpfe_pipeline *pipe = &video->pipe;
390 for (i = 0; i < pipe->input_num; i++)
391 if (pipe->inputs[i]->started)
393 for (i = 0; i < pipe->output_num; i++)
394 if (pipe->outputs[i]->started)
400 * vpfe_open() - open video device
401 * @file: file pointer
403 * initialize media pipeline state, allocate memory for file handle
405 * Return 0 if successful, or the return -ENODEV otherwise.
407 static int vpfe_open(struct file *file)
409 struct vpfe_video_device *video = video_drvdata(file);
410 struct vpfe_fh *handle;
412 /* Allocate memory for the file handle object */
413 handle = kzalloc(sizeof(struct vpfe_fh), GFP_KERNEL);
418 v4l2_fh_init(&handle->vfh, &video->video_dev);
419 v4l2_fh_add(&handle->vfh);
421 mutex_lock(&video->lock);
422 /* If decoder is not initialized. initialize it */
423 if (!video->initialized && vpfe_update_pipe_state(video)) {
424 mutex_unlock(&video->lock);
425 v4l2_fh_del(&handle->vfh);
426 v4l2_fh_exit(&handle->vfh);
430 /* Increment device users counter */
432 /* Set io_allowed member to false */
433 handle->io_allowed = 0;
434 handle->video = video;
435 file->private_data = &handle->vfh;
436 mutex_unlock(&video->lock);
441 /* get the next buffer available from dma queue */
443 vpfe_video_get_next_buffer(struct vpfe_video_device *video)
445 video->cur_frm = video->next_frm =
446 list_entry(video->dma_queue.next,
447 struct vpfe_cap_buffer, list);
449 list_del(&video->next_frm->list);
450 video->next_frm->vb.vb2_buf.state = VB2_BUF_STATE_ACTIVE;
451 return vb2_dma_contig_plane_dma_addr(&video->next_frm->vb.vb2_buf, 0);
454 /* schedule the next buffer which is available on dma queue */
455 void vpfe_video_schedule_next_buffer(struct vpfe_video_device *video)
457 struct vpfe_device *vpfe_dev = video->vpfe_dev;
460 if (list_empty(&video->dma_queue))
463 video->next_frm = list_entry(video->dma_queue.next,
464 struct vpfe_cap_buffer, list);
466 if (video->pipe.state == VPFE_PIPELINE_STREAM_SINGLESHOT)
467 video->cur_frm = video->next_frm;
469 list_del(&video->next_frm->list);
470 video->next_frm->vb.vb2_buf.state = VB2_BUF_STATE_ACTIVE;
471 addr = vb2_dma_contig_plane_dma_addr(&video->next_frm->vb.vb2_buf, 0);
472 video->ops->queue(vpfe_dev, addr);
473 video->state = VPFE_VIDEO_BUFFER_QUEUED;
476 /* schedule the buffer for capturing bottom field */
477 void vpfe_video_schedule_bottom_field(struct vpfe_video_device *video)
479 struct vpfe_device *vpfe_dev = video->vpfe_dev;
482 addr = vb2_dma_contig_plane_dma_addr(&video->cur_frm->vb.vb2_buf, 0);
483 addr += video->field_off;
484 video->ops->queue(vpfe_dev, addr);
487 /* make buffer available for dequeue */
488 void vpfe_video_process_buffer_complete(struct vpfe_video_device *video)
490 struct vpfe_pipeline *pipe = &video->pipe;
492 video->cur_frm->vb.vb2_buf.timestamp = ktime_get_ns();
493 vb2_buffer_done(&video->cur_frm->vb.vb2_buf, VB2_BUF_STATE_DONE);
494 if (pipe->state == VPFE_PIPELINE_STREAM_CONTINUOUS)
495 video->cur_frm = video->next_frm;
498 /* vpfe_stop_capture() - stop streaming */
499 static void vpfe_stop_capture(struct vpfe_video_device *video)
501 struct vpfe_pipeline *pipe = &video->pipe;
505 if (video->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
507 if (all_videos_stopped(video))
508 vpfe_pipeline_set_stream(pipe,
509 VPFE_PIPELINE_STREAM_STOPPED);
513 * vpfe_release() - release video device
514 * @file: file pointer
516 * deletes buffer queue, frees the buffers and the vpfe file handle
520 static int vpfe_release(struct file *file)
522 struct vpfe_video_device *video = video_drvdata(file);
523 struct v4l2_fh *vfh = file->private_data;
524 struct vpfe_device *vpfe_dev = video->vpfe_dev;
525 struct vpfe_fh *fh = container_of(vfh, struct vpfe_fh, vfh);
527 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_release\n");
529 /* Get the device lock */
530 mutex_lock(&video->lock);
531 /* if this instance is doing IO */
532 if (fh->io_allowed) {
533 if (video->started) {
534 vpfe_stop_capture(video);
536 * mark pipe state as stopped in vpfe_release(),
537 * as app might call streamon() after streamoff()
538 * in which case driver has to start streaming.
540 video->pipe.state = VPFE_PIPELINE_STREAM_STOPPED;
541 vb2_streamoff(&video->buffer_queue,
542 video->buffer_queue.type);
545 /* Free buffers allocated */
546 vb2_queue_release(&video->buffer_queue);
548 /* Decrement device users counter */
550 v4l2_fh_del(&fh->vfh);
551 v4l2_fh_exit(&fh->vfh);
552 /* If this is the last file handle */
554 video->initialized = 0;
555 mutex_unlock(&video->lock);
556 file->private_data = NULL;
557 /* Free memory allocated to file handle object */
564 * vpfe_mmap() - It is used to map kernel space buffers
567 static int vpfe_mmap(struct file *file, struct vm_area_struct *vma)
569 struct vpfe_video_device *video = video_drvdata(file);
570 struct vpfe_device *vpfe_dev = video->vpfe_dev;
572 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_mmap\n");
573 return vb2_mmap(&video->buffer_queue, vma);
577 * vpfe_poll() - It is used for select/poll system call
579 static __poll_t vpfe_poll(struct file *file, poll_table *wait)
581 struct vpfe_video_device *video = video_drvdata(file);
582 struct vpfe_device *vpfe_dev = video->vpfe_dev;
584 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_poll\n");
586 return vb2_poll(&video->buffer_queue, file, wait);
590 /* vpfe capture driver file operations */
591 static const struct v4l2_file_operations vpfe_fops = {
592 .owner = THIS_MODULE,
594 .release = vpfe_release,
595 .unlocked_ioctl = video_ioctl2,
601 * vpfe_querycap() - query capabilities of video device
602 * @file: file pointer
603 * @priv: void pointer
604 * @cap: pointer to v4l2_capability structure
606 * fills v4l2 capabilities structure
610 static int vpfe_querycap(struct file *file, void *priv,
611 struct v4l2_capability *cap)
613 struct vpfe_video_device *video = video_drvdata(file);
614 struct vpfe_device *vpfe_dev = video->vpfe_dev;
616 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_querycap\n");
618 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
619 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
621 cap->device_caps = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_STREAMING;
622 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VIDEO_OUTPUT |
623 V4L2_CAP_STREAMING | V4L2_CAP_DEVICE_CAPS;
624 strlcpy(cap->driver, CAPTURE_DRV_NAME, sizeof(cap->driver));
625 strlcpy(cap->bus_info, "VPFE", sizeof(cap->bus_info));
626 strlcpy(cap->card, vpfe_dev->cfg->card_name, sizeof(cap->card));
632 * vpfe_g_fmt() - get the format which is active on video device
633 * @file: file pointer
634 * @priv: void pointer
635 * @fmt: pointer to v4l2_format structure
637 * fills v4l2 format structure with active format
641 static int vpfe_g_fmt(struct file *file, void *priv,
642 struct v4l2_format *fmt)
644 struct vpfe_video_device *video = video_drvdata(file);
645 struct vpfe_device *vpfe_dev = video->vpfe_dev;
647 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_g_fmt\n");
648 /* Fill in the information about format */
654 * vpfe_enum_fmt() - enum formats supported on media chain
655 * @file: file pointer
656 * @priv: void pointer
657 * @fmt: pointer to v4l2_fmtdesc structure
659 * fills v4l2_fmtdesc structure with output format set on adjacent subdev,
660 * only one format is enumearted as subdevs are already configured
662 * Return 0 if successful, error code otherwise
664 static int vpfe_enum_fmt(struct file *file, void *priv,
665 struct v4l2_fmtdesc *fmt)
667 struct vpfe_video_device *video = video_drvdata(file);
668 struct vpfe_device *vpfe_dev = video->vpfe_dev;
669 struct v4l2_subdev_format sd_fmt;
670 struct v4l2_mbus_framefmt mbus;
671 struct v4l2_subdev *subdev;
672 struct v4l2_format format;
673 struct media_pad *remote;
676 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_enum_fmt\n");
679 * since already subdev pad format is set,
680 * only one pixel format is available
682 if (fmt->index > 0) {
683 v4l2_err(&vpfe_dev->v4l2_dev, "Invalid index\n");
686 /* get the remote pad */
687 remote = media_entity_remote_pad(&video->pad);
689 v4l2_err(&vpfe_dev->v4l2_dev,
690 "invalid remote pad for video node\n");
693 /* get the remote subdev */
694 subdev = vpfe_video_remote_subdev(video, NULL);
696 v4l2_err(&vpfe_dev->v4l2_dev,
697 "invalid remote subdev for video node\n");
700 sd_fmt.pad = remote->index;
701 sd_fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
702 /* get output format of remote subdev */
703 ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &sd_fmt);
705 v4l2_err(&vpfe_dev->v4l2_dev,
706 "invalid remote subdev for video node\n");
709 /* convert to pix format */
710 mbus.code = sd_fmt.format.code;
711 mbus_to_pix(&mbus, &format.fmt.pix);
712 /* copy the result */
713 fmt->pixelformat = format.fmt.pix.pixelformat;
719 * vpfe_s_fmt() - set the format on video device
720 * @file: file pointer
721 * @priv: void pointer
722 * @fmt: pointer to v4l2_format structure
724 * validate and set the format on video device
726 * Return 0 on success, error code otherwise
728 static int vpfe_s_fmt(struct file *file, void *priv,
729 struct v4l2_format *fmt)
731 struct vpfe_video_device *video = video_drvdata(file);
732 struct vpfe_device *vpfe_dev = video->vpfe_dev;
733 struct v4l2_format format;
736 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_s_fmt\n");
737 /* If streaming is started, return error */
738 if (video->started) {
739 v4l2_err(&vpfe_dev->v4l2_dev, "Streaming is started\n");
742 /* get adjacent subdev's output pad format */
743 ret = __vpfe_video_get_format(video, &format);
752 * vpfe_try_fmt() - try the format on video device
753 * @file: file pointer
754 * @priv: void pointer
755 * @fmt: pointer to v4l2_format structure
757 * validate the format, update with correct format
758 * based on output format set on adjacent subdev
760 * Return 0 on success, error code otherwise
762 static int vpfe_try_fmt(struct file *file, void *priv,
763 struct v4l2_format *fmt)
765 struct vpfe_video_device *video = video_drvdata(file);
766 struct vpfe_device *vpfe_dev = video->vpfe_dev;
767 struct v4l2_format format;
770 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_try_fmt\n");
771 /* get adjacent subdev's output pad format */
772 ret = __vpfe_video_get_format(video, &format);
781 * vpfe_enum_input() - enum inputs supported on media chain
782 * @file: file pointer
783 * @priv: void pointer
784 * @fmt: pointer to v4l2_fmtdesc structure
786 * fills v4l2_input structure with input available on media chain,
787 * only one input is enumearted as media chain is setup by this time
789 * Return 0 if successful, -EINVAL is media chain is invalid
791 static int vpfe_enum_input(struct file *file, void *priv,
792 struct v4l2_input *inp)
794 struct vpfe_video_device *video = video_drvdata(file);
795 struct vpfe_ext_subdev_info *sdinfo = video->current_ext_subdev;
796 struct vpfe_device *vpfe_dev = video->vpfe_dev;
798 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_enum_input\n");
799 /* enumerate from the subdev user has chosen through mc */
800 if (inp->index < sdinfo->num_inputs) {
801 memcpy(inp, &sdinfo->inputs[inp->index],
802 sizeof(struct v4l2_input));
809 * vpfe_g_input() - get index of the input which is active
810 * @file: file pointer
811 * @priv: void pointer
812 * @index: pointer to unsigned int
814 * set index with input index which is active
816 static int vpfe_g_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;
821 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_g_input\n");
823 *index = video->current_input;
828 * vpfe_s_input() - set input which is pointed by input index
829 * @file: file pointer
830 * @priv: void pointer
831 * @index: pointer to unsigned int
833 * set input on external subdev
835 * Return 0 on success, error code otherwise
837 static int vpfe_s_input(struct file *file, void *priv, unsigned int index)
839 struct vpfe_video_device *video = video_drvdata(file);
840 struct vpfe_device *vpfe_dev = video->vpfe_dev;
841 struct vpfe_ext_subdev_info *sdinfo;
842 struct vpfe_route *route;
843 struct v4l2_input *inps;
849 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_s_input\n");
851 ret = mutex_lock_interruptible(&video->lock);
855 * If streaming is started return device busy
858 if (video->started) {
859 v4l2_err(&vpfe_dev->v4l2_dev, "Streaming is on\n");
864 sdinfo = video->current_ext_subdev;
865 if (!sdinfo->registered) {
869 if (vpfe_dev->cfg->setup_input &&
870 vpfe_dev->cfg->setup_input(sdinfo->grp_id) < 0) {
872 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev,
873 "couldn't setup input for %s\n",
874 sdinfo->module_name);
877 route = &sdinfo->routes[index];
878 if (route && sdinfo->can_route) {
879 input = route->input;
880 output = route->output;
881 ret = v4l2_device_call_until_err(&vpfe_dev->v4l2_dev,
882 sdinfo->grp_id, video,
883 s_routing, input, output, 0);
885 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev,
886 "s_input:error in setting input in decoder\n");
891 /* set standards set by subdev in video device */
892 for (i = 0; i < sdinfo->num_inputs; i++) {
893 inps = &sdinfo->inputs[i];
894 video->video_dev.tvnorms |= inps->std;
896 video->current_input = index;
898 mutex_unlock(&video->lock);
903 * vpfe_querystd() - query std which is being input on external subdev
904 * @file: file pointer
905 * @priv: void pointer
906 * @std_id: pointer to v4l2_std_id structure
908 * call external subdev through v4l2_device_call_until_err to
909 * get the std that is being active.
911 * Return 0 on success, error code otherwise
913 static int vpfe_querystd(struct file *file, void *priv, v4l2_std_id *std_id)
915 struct vpfe_video_device *video = video_drvdata(file);
916 struct vpfe_device *vpfe_dev = video->vpfe_dev;
917 struct vpfe_ext_subdev_info *sdinfo;
920 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_querystd\n");
922 ret = mutex_lock_interruptible(&video->lock);
923 sdinfo = video->current_ext_subdev;
926 /* Call querystd function of decoder device */
927 ret = v4l2_device_call_until_err(&vpfe_dev->v4l2_dev, sdinfo->grp_id,
928 video, querystd, std_id);
929 mutex_unlock(&video->lock);
934 * vpfe_s_std() - set std on external subdev
935 * @file: file pointer
936 * @priv: void pointer
937 * @std_id: pointer to v4l2_std_id structure
939 * set std pointed by std_id on external subdev by calling it using
940 * v4l2_device_call_until_err
942 * Return 0 on success, error code otherwise
944 static int vpfe_s_std(struct file *file, void *priv, v4l2_std_id std_id)
946 struct vpfe_video_device *video = video_drvdata(file);
947 struct vpfe_device *vpfe_dev = video->vpfe_dev;
948 struct vpfe_ext_subdev_info *sdinfo;
951 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_s_std\n");
953 /* Call decoder driver function to set the standard */
954 ret = mutex_lock_interruptible(&video->lock);
957 sdinfo = video->current_ext_subdev;
958 /* If streaming is started, return device busy error */
959 if (video->started) {
960 v4l2_err(&vpfe_dev->v4l2_dev, "streaming is started\n");
964 ret = v4l2_device_call_until_err(&vpfe_dev->v4l2_dev, sdinfo->grp_id,
965 video, s_std, std_id);
967 v4l2_err(&vpfe_dev->v4l2_dev, "Failed to set standard\n");
968 video->stdid = V4L2_STD_UNKNOWN;
971 video->stdid = std_id;
973 mutex_unlock(&video->lock);
977 static int vpfe_g_std(struct file *file, void *priv, v4l2_std_id *tvnorm)
979 struct vpfe_video_device *video = video_drvdata(file);
980 struct vpfe_device *vpfe_dev = video->vpfe_dev;
982 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_g_std\n");
983 *tvnorm = video->stdid;
988 * vpfe_enum_dv_timings() - enumerate dv_timings which are supported by
990 * @file: file pointer
991 * @priv: void pointer
992 * @timings: pointer to v4l2_enum_dv_timings structure
994 * enum dv_timings's which are supported by external subdev through
997 * Return 0 on success, error code otherwise
1000 vpfe_enum_dv_timings(struct file *file, void *fh,
1001 struct v4l2_enum_dv_timings *timings)
1003 struct vpfe_video_device *video = video_drvdata(file);
1004 struct vpfe_device *vpfe_dev = video->vpfe_dev;
1005 struct v4l2_subdev *subdev = video->current_ext_subdev->subdev;
1009 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_enum_dv_timings\n");
1010 return v4l2_subdev_call(subdev, pad, enum_dv_timings, timings);
1014 * vpfe_query_dv_timings() - query the dv_timings which is being input
1015 * to external subdev
1016 * @file: file pointer
1017 * @priv: void pointer
1018 * @timings: pointer to v4l2_dv_timings structure
1020 * get dv_timings which is being input on external subdev through
1023 * Return 0 on success, error code otherwise
1026 vpfe_query_dv_timings(struct file *file, void *fh,
1027 struct v4l2_dv_timings *timings)
1029 struct vpfe_video_device *video = video_drvdata(file);
1030 struct vpfe_device *vpfe_dev = video->vpfe_dev;
1031 struct v4l2_subdev *subdev = video->current_ext_subdev->subdev;
1033 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_query_dv_timings\n");
1034 return v4l2_subdev_call(subdev, video, query_dv_timings, timings);
1038 * vpfe_s_dv_timings() - set dv_timings on external subdev
1039 * @file: file pointer
1040 * @priv: void pointer
1041 * @timings: pointer to v4l2_dv_timings structure
1043 * set dv_timings pointed by timings on external subdev through
1044 * v4l2_device_call_until_err, this configures amplifier also
1046 * Return 0 on success, error code otherwise
1049 vpfe_s_dv_timings(struct file *file, void *fh,
1050 struct v4l2_dv_timings *timings)
1052 struct vpfe_video_device *video = video_drvdata(file);
1053 struct vpfe_device *vpfe_dev = video->vpfe_dev;
1055 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_s_dv_timings\n");
1057 video->stdid = V4L2_STD_UNKNOWN;
1058 return v4l2_device_call_until_err(&vpfe_dev->v4l2_dev,
1059 video->current_ext_subdev->grp_id,
1060 video, s_dv_timings, timings);
1064 * vpfe_g_dv_timings() - get dv_timings which is set on external subdev
1065 * @file: file pointer
1066 * @priv: void pointer
1067 * @timings: pointer to v4l2_dv_timings structure
1069 * get dv_timings which is set on external subdev through
1072 * Return 0 on success, error code otherwise
1075 vpfe_g_dv_timings(struct file *file, void *fh,
1076 struct v4l2_dv_timings *timings)
1078 struct vpfe_video_device *video = video_drvdata(file);
1079 struct vpfe_device *vpfe_dev = video->vpfe_dev;
1080 struct v4l2_subdev *subdev = video->current_ext_subdev->subdev;
1082 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_g_dv_timings\n");
1083 return v4l2_subdev_call(subdev, video, g_dv_timings, timings);
1087 * Videobuf operations
1090 * vpfe_buffer_queue_setup : Callback function for buffer setup.
1091 * @vq: vb2_queue ptr
1093 * @nbuffers: ptr to number of buffers requested by application
1094 * @nplanes:: contains number of distinct video planes needed to hold a frame
1095 * @sizes[]: contains the size (in bytes) of each plane.
1096 * @alloc_devs: ptr to allocation context
1098 * This callback function is called when reqbuf() is called to adjust
1099 * the buffer nbuffers and buffer size
1102 vpfe_buffer_queue_setup(struct vb2_queue *vq,
1103 unsigned int *nbuffers, unsigned int *nplanes,
1104 unsigned int sizes[], struct device *alloc_devs[])
1106 struct vpfe_fh *fh = vb2_get_drv_priv(vq);
1107 struct vpfe_video_device *video = fh->video;
1108 struct vpfe_device *vpfe_dev = video->vpfe_dev;
1111 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_buffer_queue_setup\n");
1112 size = video->fmt.fmt.pix.sizeimage;
1114 if (vq->num_buffers + *nbuffers < 3)
1115 *nbuffers = 3 - vq->num_buffers;
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 const 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,
1318 .wait_prepare = vb2_ops_wait_prepare,
1319 .wait_finish = vb2_ops_wait_finish,
1323 * vpfe_reqbufs() - supported REQBUF only once opening
1326 static int vpfe_reqbufs(struct file *file, void *priv,
1327 struct v4l2_requestbuffers *req_buf)
1329 struct vpfe_video_device *video = video_drvdata(file);
1330 struct vpfe_device *vpfe_dev = video->vpfe_dev;
1331 struct vpfe_fh *fh = file->private_data;
1332 struct vb2_queue *q;
1335 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_reqbufs\n");
1337 if (req_buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1338 req_buf->type != V4L2_BUF_TYPE_VIDEO_OUTPUT){
1339 v4l2_err(&vpfe_dev->v4l2_dev, "Invalid buffer type\n");
1343 ret = mutex_lock_interruptible(&video->lock);
1347 if (video->io_usrs != 0) {
1348 v4l2_err(&vpfe_dev->v4l2_dev, "Only one IO user allowed\n");
1352 video->memory = req_buf->memory;
1354 /* Initialize videobuf2 queue as per the buffer type */
1355 q = &video->buffer_queue;
1356 q->type = req_buf->type;
1357 q->io_modes = VB2_MMAP | VB2_USERPTR;
1359 q->min_buffers_needed = 1;
1360 q->ops = &video_qops;
1361 q->mem_ops = &vb2_dma_contig_memops;
1362 q->buf_struct_size = sizeof(struct vpfe_cap_buffer);
1363 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1364 q->dev = vpfe_dev->pdev;
1365 q->lock = &video->lock;
1367 ret = vb2_queue_init(q);
1369 v4l2_err(&vpfe_dev->v4l2_dev, "vb2_queue_init() failed\n");
1375 INIT_LIST_HEAD(&video->dma_queue);
1376 ret = vb2_reqbufs(&video->buffer_queue, req_buf);
1379 mutex_unlock(&video->lock);
1384 * vpfe_querybuf() - query buffers for exchange
1386 static int vpfe_querybuf(struct file *file, void *priv,
1387 struct v4l2_buffer *buf)
1389 struct vpfe_video_device *video = video_drvdata(file);
1390 struct vpfe_device *vpfe_dev = video->vpfe_dev;
1392 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_querybuf\n");
1394 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1395 buf->type != V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1396 v4l2_err(&vpfe_dev->v4l2_dev, "Invalid buf type\n");
1400 if (video->memory != V4L2_MEMORY_MMAP) {
1401 v4l2_err(&vpfe_dev->v4l2_dev, "Invalid memory\n");
1405 /* Call vb2_querybuf to get information */
1406 return vb2_querybuf(&video->buffer_queue, buf);
1410 * vpfe_qbuf() - queue buffers for capture or processing
1412 static int vpfe_qbuf(struct file *file, void *priv,
1413 struct v4l2_buffer *p)
1415 struct vpfe_video_device *video = video_drvdata(file);
1416 struct vpfe_device *vpfe_dev = video->vpfe_dev;
1417 struct vpfe_fh *fh = file->private_data;
1419 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_qbuf\n");
1421 if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1422 p->type != V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1423 v4l2_err(&vpfe_dev->v4l2_dev, "Invalid buf type\n");
1427 * If this file handle is not allowed to do IO,
1430 if (!fh->io_allowed) {
1431 v4l2_err(&vpfe_dev->v4l2_dev, "fh->io_allowed\n");
1435 return vb2_qbuf(&video->buffer_queue, p);
1439 * vpfe_dqbuf() - deque buffer which is done with processing
1441 static int vpfe_dqbuf(struct file *file, void *priv,
1442 struct v4l2_buffer *buf)
1444 struct vpfe_video_device *video = video_drvdata(file);
1445 struct vpfe_device *vpfe_dev = video->vpfe_dev;
1447 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_dqbuf\n");
1449 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1450 buf->type != V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1451 v4l2_err(&vpfe_dev->v4l2_dev, "Invalid buf type\n");
1455 return vb2_dqbuf(&video->buffer_queue,
1456 buf, (file->f_flags & O_NONBLOCK));
1460 * vpfe_streamon() - start streaming
1461 * @file: file pointer
1462 * @priv: void pointer
1463 * @buf_type: enum v4l2_buf_type
1465 * queue buffer onto hardware for capture/processing and
1466 * start all the subdevs which are in media chain
1468 * Return 0 on success, error code otherwise
1470 static int vpfe_streamon(struct file *file, void *priv,
1471 enum v4l2_buf_type buf_type)
1473 struct vpfe_video_device *video = video_drvdata(file);
1474 struct vpfe_device *vpfe_dev = video->vpfe_dev;
1475 struct vpfe_pipeline *pipe = &video->pipe;
1476 struct vpfe_fh *fh = file->private_data;
1479 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_streamon\n");
1481 if (buf_type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1482 buf_type != V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1483 v4l2_err(&vpfe_dev->v4l2_dev, "Invalid buf type\n");
1486 /* If file handle is not allowed IO, return error */
1487 if (!fh->io_allowed) {
1488 v4l2_err(&vpfe_dev->v4l2_dev, "fh->io_allowed\n");
1491 /* If buffer queue is empty, return error */
1492 if (list_empty(&video->buffer_queue.queued_list)) {
1493 v4l2_err(&vpfe_dev->v4l2_dev, "buffer queue is empty\n");
1496 /* Validate the pipeline */
1497 if (buf_type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1498 ret = vpfe_video_validate_pipeline(pipe);
1502 /* Call vb2_streamon to start streaming */
1503 return vb2_streamon(&video->buffer_queue, buf_type);
1507 * vpfe_streamoff() - stop streaming
1508 * @file: file pointer
1509 * @priv: void pointer
1510 * @buf_type: enum v4l2_buf_type
1512 * stop all the subdevs which are in media chain
1514 * Return 0 on success, error code otherwise
1516 static int vpfe_streamoff(struct file *file, void *priv,
1517 enum v4l2_buf_type buf_type)
1519 struct vpfe_video_device *video = video_drvdata(file);
1520 struct vpfe_device *vpfe_dev = video->vpfe_dev;
1521 struct vpfe_fh *fh = file->private_data;
1524 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_streamoff\n");
1526 if (buf_type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1527 buf_type != V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1528 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "Invalid buf type\n");
1532 /* If io is allowed for this file handle, return error */
1533 if (!fh->io_allowed) {
1534 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "fh->io_allowed\n");
1538 /* If streaming is not started, return error */
1539 if (!video->started) {
1540 v4l2_err(&vpfe_dev->v4l2_dev, "device is not started\n");
1544 ret = mutex_lock_interruptible(&video->lock);
1548 vpfe_stop_capture(video);
1549 ret = vb2_streamoff(&video->buffer_queue, buf_type);
1550 mutex_unlock(&video->lock);
1555 /* vpfe capture ioctl operations */
1556 static const struct v4l2_ioctl_ops vpfe_ioctl_ops = {
1557 .vidioc_querycap = vpfe_querycap,
1558 .vidioc_g_fmt_vid_cap = vpfe_g_fmt,
1559 .vidioc_s_fmt_vid_cap = vpfe_s_fmt,
1560 .vidioc_try_fmt_vid_cap = vpfe_try_fmt,
1561 .vidioc_enum_fmt_vid_cap = vpfe_enum_fmt,
1562 .vidioc_g_fmt_vid_out = vpfe_g_fmt,
1563 .vidioc_s_fmt_vid_out = vpfe_s_fmt,
1564 .vidioc_try_fmt_vid_out = vpfe_try_fmt,
1565 .vidioc_enum_fmt_vid_out = vpfe_enum_fmt,
1566 .vidioc_enum_input = vpfe_enum_input,
1567 .vidioc_g_input = vpfe_g_input,
1568 .vidioc_s_input = vpfe_s_input,
1569 .vidioc_querystd = vpfe_querystd,
1570 .vidioc_s_std = vpfe_s_std,
1571 .vidioc_g_std = vpfe_g_std,
1572 .vidioc_enum_dv_timings = vpfe_enum_dv_timings,
1573 .vidioc_query_dv_timings = vpfe_query_dv_timings,
1574 .vidioc_s_dv_timings = vpfe_s_dv_timings,
1575 .vidioc_g_dv_timings = vpfe_g_dv_timings,
1576 .vidioc_reqbufs = vpfe_reqbufs,
1577 .vidioc_querybuf = vpfe_querybuf,
1578 .vidioc_qbuf = vpfe_qbuf,
1579 .vidioc_dqbuf = vpfe_dqbuf,
1580 .vidioc_streamon = vpfe_streamon,
1581 .vidioc_streamoff = vpfe_streamoff,
1584 /* VPFE video init function */
1585 int vpfe_video_init(struct vpfe_video_device *video, const char *name)
1587 const char *direction;
1590 switch (video->type) {
1591 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1592 direction = "output";
1593 video->pad.flags = MEDIA_PAD_FL_SINK;
1594 video->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1597 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
1598 direction = "input";
1599 video->pad.flags = MEDIA_PAD_FL_SOURCE;
1600 video->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
1606 /* Initialize field of video device */
1607 mutex_init(&video->lock);
1608 video->video_dev.release = video_device_release;
1609 video->video_dev.fops = &vpfe_fops;
1610 video->video_dev.ioctl_ops = &vpfe_ioctl_ops;
1611 video->video_dev.minor = -1;
1612 video->video_dev.tvnorms = 0;
1613 video->video_dev.lock = &video->lock;
1614 snprintf(video->video_dev.name, sizeof(video->video_dev.name),
1615 "DAVINCI VIDEO %s %s", name, direction);
1617 spin_lock_init(&video->irqlock);
1618 spin_lock_init(&video->dma_queue_lock);
1619 ret = media_entity_pads_init(&video->video_dev.entity,
1624 video_set_drvdata(&video->video_dev, video);
1629 /* vpfe video device register function */
1630 int vpfe_video_register(struct vpfe_video_device *video,
1631 struct v4l2_device *vdev)
1635 video->video_dev.v4l2_dev = vdev;
1637 ret = video_register_device(&video->video_dev, VFL_TYPE_GRABBER, -1);
1639 pr_err("%s: could not register video device (%d)\n",
1644 /* vpfe video device unregister function */
1645 void vpfe_video_unregister(struct vpfe_video_device *video)
1647 if (video_is_registered(&video->video_dev)) {
1648 video_unregister_device(&video->video_dev);
1649 media_entity_cleanup(&video->video_dev.entity);