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_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_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);
426 v4l2_fh_del(&handle->vfh);
427 v4l2_fh_exit(&handle->vfh);
431 /* Increment device users counter */
433 /* Set io_allowed member to false */
434 handle->io_allowed = 0;
435 handle->video = video;
436 file->private_data = &handle->vfh;
437 mutex_unlock(&video->lock);
442 /* get the next buffer available from dma queue */
444 vpfe_video_get_next_buffer(struct vpfe_video_device *video)
446 video->cur_frm = video->next_frm =
447 list_entry(video->dma_queue.next,
448 struct vpfe_cap_buffer, list);
450 list_del(&video->next_frm->list);
451 video->next_frm->vb.vb2_buf.state = VB2_BUF_STATE_ACTIVE;
452 return vb2_dma_contig_plane_dma_addr(&video->next_frm->vb.vb2_buf, 0);
455 /* schedule the next buffer which is available on dma queue */
456 void vpfe_video_schedule_next_buffer(struct vpfe_video_device *video)
458 struct vpfe_device *vpfe_dev = video->vpfe_dev;
461 if (list_empty(&video->dma_queue))
464 video->next_frm = list_entry(video->dma_queue.next,
465 struct vpfe_cap_buffer, list);
467 if (video->pipe.state == VPFE_PIPELINE_STREAM_SINGLESHOT)
468 video->cur_frm = video->next_frm;
470 list_del(&video->next_frm->list);
471 video->next_frm->vb.vb2_buf.state = VB2_BUF_STATE_ACTIVE;
472 addr = vb2_dma_contig_plane_dma_addr(&video->next_frm->vb.vb2_buf, 0);
473 video->ops->queue(vpfe_dev, addr);
474 video->state = VPFE_VIDEO_BUFFER_QUEUED;
477 /* schedule the buffer for capturing bottom field */
478 void vpfe_video_schedule_bottom_field(struct vpfe_video_device *video)
480 struct vpfe_device *vpfe_dev = video->vpfe_dev;
483 addr = vb2_dma_contig_plane_dma_addr(&video->cur_frm->vb.vb2_buf, 0);
484 addr += video->field_off;
485 video->ops->queue(vpfe_dev, addr);
488 /* make buffer available for dequeue */
489 void vpfe_video_process_buffer_complete(struct vpfe_video_device *video)
491 struct vpfe_pipeline *pipe = &video->pipe;
493 video->cur_frm->vb.vb2_buf.timestamp = ktime_get_ns();
494 vb2_buffer_done(&video->cur_frm->vb.vb2_buf, VB2_BUF_STATE_DONE);
495 if (pipe->state == VPFE_PIPELINE_STREAM_CONTINUOUS)
496 video->cur_frm = video->next_frm;
499 /* vpfe_stop_capture() - stop streaming */
500 static void vpfe_stop_capture(struct vpfe_video_device *video)
502 struct vpfe_pipeline *pipe = &video->pipe;
506 if (video->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
508 if (all_videos_stopped(video))
509 vpfe_pipeline_set_stream(pipe,
510 VPFE_PIPELINE_STREAM_STOPPED);
514 * vpfe_release() - release video device
515 * @file: file pointer
517 * deletes buffer queue, frees the buffers and the vpfe file handle
521 static int vpfe_release(struct file *file)
523 struct vpfe_video_device *video = video_drvdata(file);
524 struct v4l2_fh *vfh = file->private_data;
525 struct vpfe_device *vpfe_dev = video->vpfe_dev;
526 struct vpfe_fh *fh = container_of(vfh, struct vpfe_fh, vfh);
528 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_release\n");
530 /* Get the device lock */
531 mutex_lock(&video->lock);
532 /* if this instance is doing IO */
533 if (fh->io_allowed) {
534 if (video->started) {
535 vpfe_stop_capture(video);
537 * mark pipe state as stopped in vpfe_release(),
538 * as app might call streamon() after streamoff()
539 * in which case driver has to start streaming.
541 video->pipe.state = VPFE_PIPELINE_STREAM_STOPPED;
542 vb2_streamoff(&video->buffer_queue,
543 video->buffer_queue.type);
546 /* Free buffers allocated */
547 vb2_queue_release(&video->buffer_queue);
549 /* Decrement device users counter */
551 v4l2_fh_del(&fh->vfh);
552 v4l2_fh_exit(&fh->vfh);
553 /* If this is the last file handle */
555 video->initialized = 0;
556 mutex_unlock(&video->lock);
557 file->private_data = NULL;
558 /* Free memory allocated to file handle object */
565 * vpfe_mmap() - It is used to map kernel space buffers
568 static int vpfe_mmap(struct file *file, struct vm_area_struct *vma)
570 struct vpfe_video_device *video = video_drvdata(file);
571 struct vpfe_device *vpfe_dev = video->vpfe_dev;
573 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_mmap\n");
574 return vb2_mmap(&video->buffer_queue, vma);
578 * vpfe_poll() - It is used for select/poll system call
580 static unsigned int vpfe_poll(struct file *file, poll_table *wait)
582 struct vpfe_video_device *video = video_drvdata(file);
583 struct vpfe_device *vpfe_dev = video->vpfe_dev;
585 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_poll\n");
587 return vb2_poll(&video->buffer_queue, file, wait);
591 /* vpfe capture driver file operations */
592 static const struct v4l2_file_operations vpfe_fops = {
593 .owner = THIS_MODULE,
595 .release = vpfe_release,
596 .unlocked_ioctl = video_ioctl2,
602 * vpfe_querycap() - query capabilities of video device
603 * @file: file pointer
604 * @priv: void pointer
605 * @cap: pointer to v4l2_capability structure
607 * fills v4l2 capabilities structure
611 static int vpfe_querycap(struct file *file, void *priv,
612 struct v4l2_capability *cap)
614 struct vpfe_video_device *video = video_drvdata(file);
615 struct vpfe_device *vpfe_dev = video->vpfe_dev;
617 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_querycap\n");
619 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
620 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
622 cap->device_caps = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_STREAMING;
623 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VIDEO_OUTPUT |
624 V4L2_CAP_STREAMING | V4L2_CAP_DEVICE_CAPS;
625 strlcpy(cap->driver, CAPTURE_DRV_NAME, sizeof(cap->driver));
626 strlcpy(cap->bus_info, "VPFE", sizeof(cap->bus_info));
627 strlcpy(cap->card, vpfe_dev->cfg->card_name, sizeof(cap->card));
633 * vpfe_g_fmt() - get the format which is active on video device
634 * @file: file pointer
635 * @priv: void pointer
636 * @fmt: pointer to v4l2_format structure
638 * fills v4l2 format structure with active format
642 static int vpfe_g_fmt(struct file *file, void *priv,
643 struct v4l2_format *fmt)
645 struct vpfe_video_device *video = video_drvdata(file);
646 struct vpfe_device *vpfe_dev = video->vpfe_dev;
648 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_g_fmt\n");
649 /* Fill in the information about format */
655 * vpfe_enum_fmt() - enum formats supported on media chain
656 * @file: file pointer
657 * @priv: void pointer
658 * @fmt: pointer to v4l2_fmtdesc structure
660 * fills v4l2_fmtdesc structure with output format set on adjacent subdev,
661 * only one format is enumearted as subdevs are already configured
663 * Return 0 if successful, error code otherwise
665 static int vpfe_enum_fmt(struct file *file, void *priv,
666 struct v4l2_fmtdesc *fmt)
668 struct vpfe_video_device *video = video_drvdata(file);
669 struct vpfe_device *vpfe_dev = video->vpfe_dev;
670 struct v4l2_subdev_format sd_fmt;
671 struct v4l2_mbus_framefmt mbus;
672 struct v4l2_subdev *subdev;
673 struct v4l2_format format;
674 struct media_pad *remote;
677 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_enum_fmt\n");
680 * since already subdev pad format is set,
681 * only one pixel format is available
683 if (fmt->index > 0) {
684 v4l2_err(&vpfe_dev->v4l2_dev, "Invalid index\n");
687 /* get the remote pad */
688 remote = media_entity_remote_pad(&video->pad);
690 v4l2_err(&vpfe_dev->v4l2_dev,
691 "invalid remote pad for video node\n");
694 /* get the remote subdev */
695 subdev = vpfe_video_remote_subdev(video, NULL);
697 v4l2_err(&vpfe_dev->v4l2_dev,
698 "invalid remote subdev for video node\n");
701 sd_fmt.pad = remote->index;
702 sd_fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
703 /* get output format of remote subdev */
704 ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &sd_fmt);
706 v4l2_err(&vpfe_dev->v4l2_dev,
707 "invalid remote subdev for video node\n");
710 /* convert to pix format */
711 mbus.code = sd_fmt.format.code;
712 mbus_to_pix(&mbus, &format.fmt.pix);
713 /* copy the result */
714 fmt->pixelformat = format.fmt.pix.pixelformat;
720 * vpfe_s_fmt() - set the format on video device
721 * @file: file pointer
722 * @priv: void pointer
723 * @fmt: pointer to v4l2_format structure
725 * validate and set the format on video device
727 * Return 0 on success, error code otherwise
729 static int vpfe_s_fmt(struct file *file, void *priv,
730 struct v4l2_format *fmt)
732 struct vpfe_video_device *video = video_drvdata(file);
733 struct vpfe_device *vpfe_dev = video->vpfe_dev;
734 struct v4l2_format format;
737 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_s_fmt\n");
738 /* If streaming is started, return error */
739 if (video->started) {
740 v4l2_err(&vpfe_dev->v4l2_dev, "Streaming is started\n");
743 /* get adjacent subdev's output pad format */
744 ret = __vpfe_video_get_format(video, &format);
753 * vpfe_try_fmt() - try the format on video device
754 * @file: file pointer
755 * @priv: void pointer
756 * @fmt: pointer to v4l2_format structure
758 * validate the format, update with correct format
759 * based on output format set on adjacent subdev
761 * Return 0 on success, error code otherwise
763 static int vpfe_try_fmt(struct file *file, void *priv,
764 struct v4l2_format *fmt)
766 struct vpfe_video_device *video = video_drvdata(file);
767 struct vpfe_device *vpfe_dev = video->vpfe_dev;
768 struct v4l2_format format;
771 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_try_fmt\n");
772 /* get adjacent subdev's output pad format */
773 ret = __vpfe_video_get_format(video, &format);
782 * vpfe_enum_input() - enum inputs supported on media chain
783 * @file: file pointer
784 * @priv: void pointer
785 * @fmt: pointer to v4l2_fmtdesc structure
787 * fills v4l2_input structure with input available on media chain,
788 * only one input is enumearted as media chain is setup by this time
790 * Return 0 if successful, -EINVAL is media chain is invalid
792 static int vpfe_enum_input(struct file *file, void *priv,
793 struct v4l2_input *inp)
795 struct vpfe_video_device *video = video_drvdata(file);
796 struct vpfe_ext_subdev_info *sdinfo = video->current_ext_subdev;
797 struct vpfe_device *vpfe_dev = video->vpfe_dev;
799 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_enum_input\n");
800 /* enumerate from the subdev user has chosen through mc */
801 if (inp->index < sdinfo->num_inputs) {
802 memcpy(inp, &sdinfo->inputs[inp->index],
803 sizeof(struct v4l2_input));
810 * vpfe_g_input() - get index of the input which is active
811 * @file: file pointer
812 * @priv: void pointer
813 * @index: pointer to unsigned int
815 * set index with input index which is active
817 static int vpfe_g_input(struct file *file, void *priv, unsigned int *index)
819 struct vpfe_video_device *video = video_drvdata(file);
820 struct vpfe_device *vpfe_dev = video->vpfe_dev;
822 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_g_input\n");
824 *index = video->current_input;
829 * vpfe_s_input() - set input which is pointed by input index
830 * @file: file pointer
831 * @priv: void pointer
832 * @index: pointer to unsigned int
834 * set input on external subdev
836 * Return 0 on success, error code otherwise
838 static int vpfe_s_input(struct file *file, void *priv, unsigned int index)
840 struct vpfe_video_device *video = video_drvdata(file);
841 struct vpfe_device *vpfe_dev = video->vpfe_dev;
842 struct vpfe_ext_subdev_info *sdinfo;
843 struct vpfe_route *route;
844 struct v4l2_input *inps;
850 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_s_input\n");
852 ret = mutex_lock_interruptible(&video->lock);
856 * If streaming is started return device busy
859 if (video->started) {
860 v4l2_err(&vpfe_dev->v4l2_dev, "Streaming is on\n");
865 sdinfo = video->current_ext_subdev;
866 if (!sdinfo->registered) {
870 if (vpfe_dev->cfg->setup_input &&
871 vpfe_dev->cfg->setup_input(sdinfo->grp_id) < 0) {
873 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev,
874 "couldn't setup input for %s\n",
875 sdinfo->module_name);
878 route = &sdinfo->routes[index];
879 if (route && sdinfo->can_route) {
880 input = route->input;
881 output = route->output;
882 ret = v4l2_device_call_until_err(&vpfe_dev->v4l2_dev,
883 sdinfo->grp_id, video,
884 s_routing, input, output, 0);
886 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev,
887 "s_input:error in setting input in decoder\n");
892 /* set standards set by subdev in video device */
893 for (i = 0; i < sdinfo->num_inputs; i++) {
894 inps = &sdinfo->inputs[i];
895 video->video_dev.tvnorms |= inps->std;
897 video->current_input = index;
899 mutex_unlock(&video->lock);
904 * vpfe_querystd() - query std which is being input on external subdev
905 * @file: file pointer
906 * @priv: void pointer
907 * @std_id: pointer to v4l2_std_id structure
909 * call external subdev through v4l2_device_call_until_err to
910 * get the std that is being active.
912 * Return 0 on success, error code otherwise
914 static int vpfe_querystd(struct file *file, void *priv, v4l2_std_id *std_id)
916 struct vpfe_video_device *video = video_drvdata(file);
917 struct vpfe_device *vpfe_dev = video->vpfe_dev;
918 struct vpfe_ext_subdev_info *sdinfo;
921 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_querystd\n");
923 ret = mutex_lock_interruptible(&video->lock);
924 sdinfo = video->current_ext_subdev;
927 /* Call querystd function of decoder device */
928 ret = v4l2_device_call_until_err(&vpfe_dev->v4l2_dev, sdinfo->grp_id,
929 video, querystd, std_id);
930 mutex_unlock(&video->lock);
935 * vpfe_s_std() - set std on external subdev
936 * @file: file pointer
937 * @priv: void pointer
938 * @std_id: pointer to v4l2_std_id structure
940 * set std pointed by std_id on external subdev by calling it using
941 * v4l2_device_call_until_err
943 * Return 0 on success, error code otherwise
945 static int vpfe_s_std(struct file *file, void *priv, v4l2_std_id std_id)
947 struct vpfe_video_device *video = video_drvdata(file);
948 struct vpfe_device *vpfe_dev = video->vpfe_dev;
949 struct vpfe_ext_subdev_info *sdinfo;
952 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_s_std\n");
954 /* Call decoder driver function to set the standard */
955 ret = mutex_lock_interruptible(&video->lock);
958 sdinfo = video->current_ext_subdev;
959 /* If streaming is started, return device busy error */
960 if (video->started) {
961 v4l2_err(&vpfe_dev->v4l2_dev, "streaming is started\n");
965 ret = v4l2_device_call_until_err(&vpfe_dev->v4l2_dev, sdinfo->grp_id,
966 video, s_std, std_id);
968 v4l2_err(&vpfe_dev->v4l2_dev, "Failed to set standard\n");
969 video->stdid = V4L2_STD_UNKNOWN;
972 video->stdid = std_id;
974 mutex_unlock(&video->lock);
978 static int vpfe_g_std(struct file *file, void *priv, v4l2_std_id *tvnorm)
980 struct vpfe_video_device *video = video_drvdata(file);
981 struct vpfe_device *vpfe_dev = video->vpfe_dev;
983 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_g_std\n");
984 *tvnorm = video->stdid;
989 * vpfe_enum_dv_timings() - enumerate dv_timings which are supported by
991 * @file: file pointer
992 * @priv: void pointer
993 * @timings: pointer to v4l2_enum_dv_timings structure
995 * enum dv_timings's which are supported by external subdev through
998 * Return 0 on success, error code otherwise
1001 vpfe_enum_dv_timings(struct file *file, void *fh,
1002 struct v4l2_enum_dv_timings *timings)
1004 struct vpfe_video_device *video = video_drvdata(file);
1005 struct vpfe_device *vpfe_dev = video->vpfe_dev;
1006 struct v4l2_subdev *subdev = video->current_ext_subdev->subdev;
1010 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_enum_dv_timings\n");
1011 return v4l2_subdev_call(subdev, pad, enum_dv_timings, timings);
1015 * vpfe_query_dv_timings() - query the dv_timings which is being input
1016 * to external subdev
1017 * @file: file pointer
1018 * @priv: void pointer
1019 * @timings: pointer to v4l2_dv_timings structure
1021 * get dv_timings which is being input on external subdev through
1024 * Return 0 on success, error code otherwise
1027 vpfe_query_dv_timings(struct file *file, void *fh,
1028 struct v4l2_dv_timings *timings)
1030 struct vpfe_video_device *video = video_drvdata(file);
1031 struct vpfe_device *vpfe_dev = video->vpfe_dev;
1032 struct v4l2_subdev *subdev = video->current_ext_subdev->subdev;
1034 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_query_dv_timings\n");
1035 return v4l2_subdev_call(subdev, video, query_dv_timings, timings);
1039 * vpfe_s_dv_timings() - set dv_timings on external subdev
1040 * @file: file pointer
1041 * @priv: void pointer
1042 * @timings: pointer to v4l2_dv_timings structure
1044 * set dv_timings pointed by timings on external subdev through
1045 * v4l2_device_call_until_err, this configures amplifier also
1047 * Return 0 on success, error code otherwise
1050 vpfe_s_dv_timings(struct file *file, void *fh,
1051 struct v4l2_dv_timings *timings)
1053 struct vpfe_video_device *video = video_drvdata(file);
1054 struct vpfe_device *vpfe_dev = video->vpfe_dev;
1056 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_s_dv_timings\n");
1058 video->stdid = V4L2_STD_UNKNOWN;
1059 return v4l2_device_call_until_err(&vpfe_dev->v4l2_dev,
1060 video->current_ext_subdev->grp_id,
1061 video, s_dv_timings, timings);
1065 * vpfe_g_dv_timings() - get dv_timings which is set on external subdev
1066 * @file: file pointer
1067 * @priv: void pointer
1068 * @timings: pointer to v4l2_dv_timings structure
1070 * get dv_timings which is set on external subdev through
1073 * Return 0 on success, error code otherwise
1076 vpfe_g_dv_timings(struct file *file, void *fh,
1077 struct v4l2_dv_timings *timings)
1079 struct vpfe_video_device *video = video_drvdata(file);
1080 struct vpfe_device *vpfe_dev = video->vpfe_dev;
1081 struct v4l2_subdev *subdev = video->current_ext_subdev->subdev;
1083 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_g_dv_timings\n");
1084 return v4l2_subdev_call(subdev, video, g_dv_timings, timings);
1088 * Videobuf operations
1091 * vpfe_buffer_queue_setup : Callback function for buffer setup.
1092 * @vq: vb2_queue ptr
1094 * @nbuffers: ptr to number of buffers requested by application
1095 * @nplanes:: contains number of distinct video planes needed to hold a frame
1096 * @sizes[]: contains the size (in bytes) of each plane.
1097 * @alloc_devs: ptr to allocation context
1099 * This callback function is called when reqbuf() is called to adjust
1100 * the buffer nbuffers and buffer size
1103 vpfe_buffer_queue_setup(struct vb2_queue *vq,
1104 unsigned int *nbuffers, unsigned int *nplanes,
1105 unsigned int sizes[], struct device *alloc_devs[])
1107 struct vpfe_fh *fh = vb2_get_drv_priv(vq);
1108 struct vpfe_video_device *video = fh->video;
1109 struct vpfe_device *vpfe_dev = video->vpfe_dev;
1112 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_buffer_queue_setup\n");
1113 size = video->fmt.fmt.pix.sizeimage;
1115 if (vq->num_buffers + *nbuffers < 3)
1116 *nbuffers = 3 - vq->num_buffers;
1120 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev,
1121 "nbuffers=%d, size=%lu\n", *nbuffers, size);
1126 * vpfe_buffer_prepare : callback function for buffer prepare
1127 * @vb: ptr to vb2_buffer
1129 * This is the callback function for buffer prepare when vb2_qbuf()
1130 * function is called. The buffer is prepared and user space virtual address
1131 * or user address is converted into physical address
1133 static int vpfe_buffer_prepare(struct vb2_buffer *vb)
1135 struct vpfe_fh *fh = vb2_get_drv_priv(vb->vb2_queue);
1136 struct vpfe_video_device *video = fh->video;
1137 struct vpfe_device *vpfe_dev = video->vpfe_dev;
1140 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_buffer_prepare\n");
1142 if (vb->state != VB2_BUF_STATE_ACTIVE &&
1143 vb->state != VB2_BUF_STATE_PREPARED)
1146 /* Initialize buffer */
1147 vb2_set_plane_payload(vb, 0, video->fmt.fmt.pix.sizeimage);
1148 if (vb2_plane_vaddr(vb, 0) &&
1149 vb2_get_plane_payload(vb, 0) > vb2_plane_size(vb, 0))
1152 addr = vb2_dma_contig_plane_dma_addr(vb, 0);
1153 /* Make sure user addresses are aligned to 32 bytes */
1154 if (!ALIGN(addr, 32))
1160 static void vpfe_buffer_queue(struct vb2_buffer *vb)
1162 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
1163 /* Get the file handle object and device object */
1164 struct vpfe_fh *fh = vb2_get_drv_priv(vb->vb2_queue);
1165 struct vpfe_video_device *video = fh->video;
1166 struct vpfe_device *vpfe_dev = video->vpfe_dev;
1167 struct vpfe_pipeline *pipe = &video->pipe;
1168 struct vpfe_cap_buffer *buf = container_of(vbuf,
1169 struct vpfe_cap_buffer, vb);
1170 unsigned long flags;
1171 unsigned long empty;
1174 spin_lock_irqsave(&video->dma_queue_lock, flags);
1175 empty = list_empty(&video->dma_queue);
1176 /* add the buffer to the DMA queue */
1177 list_add_tail(&buf->list, &video->dma_queue);
1178 spin_unlock_irqrestore(&video->dma_queue_lock, flags);
1179 /* this case happens in case of single shot */
1180 if (empty && video->started && pipe->state ==
1181 VPFE_PIPELINE_STREAM_SINGLESHOT &&
1182 video->state == VPFE_VIDEO_BUFFER_NOT_QUEUED) {
1183 spin_lock(&video->dma_queue_lock);
1184 addr = vpfe_video_get_next_buffer(video);
1185 video->ops->queue(vpfe_dev, addr);
1187 video->state = VPFE_VIDEO_BUFFER_QUEUED;
1188 spin_unlock(&video->dma_queue_lock);
1190 /* enable h/w each time in single shot */
1191 if (vpfe_video_is_pipe_ready(pipe))
1192 vpfe_pipeline_set_stream(pipe,
1193 VPFE_PIPELINE_STREAM_SINGLESHOT);
1197 /* vpfe_start_capture() - start streaming on all the subdevs */
1198 static int vpfe_start_capture(struct vpfe_video_device *video)
1200 struct vpfe_pipeline *pipe = &video->pipe;
1204 if (vpfe_video_is_pipe_ready(pipe))
1205 ret = vpfe_pipeline_set_stream(pipe, pipe->state);
1210 static int vpfe_start_streaming(struct vb2_queue *vq, unsigned int count)
1212 struct vpfe_fh *fh = vb2_get_drv_priv(vq);
1213 struct vpfe_video_device *video = fh->video;
1214 struct vpfe_device *vpfe_dev = video->vpfe_dev;
1218 ret = mutex_lock_interruptible(&video->lock);
1222 /* Get the next frame from the buffer queue */
1223 video->cur_frm = video->next_frm =
1224 list_entry(video->dma_queue.next, struct vpfe_cap_buffer, list);
1225 /* Remove buffer from the buffer queue */
1226 list_del(&video->cur_frm->list);
1227 /* Mark state of the current frame to active */
1228 video->cur_frm->vb.vb2_buf.state = VB2_BUF_STATE_ACTIVE;
1229 /* Initialize field_id and started member */
1230 video->field_id = 0;
1231 addr = vb2_dma_contig_plane_dma_addr(&video->cur_frm->vb.vb2_buf, 0);
1232 video->ops->queue(vpfe_dev, addr);
1233 video->state = VPFE_VIDEO_BUFFER_QUEUED;
1235 ret = vpfe_start_capture(video);
1237 struct vpfe_cap_buffer *buf, *tmp;
1239 vb2_buffer_done(&video->cur_frm->vb.vb2_buf,
1240 VB2_BUF_STATE_QUEUED);
1241 list_for_each_entry_safe(buf, tmp, &video->dma_queue, list) {
1242 list_del(&buf->list);
1243 vb2_buffer_done(&buf->vb.vb2_buf,
1244 VB2_BUF_STATE_QUEUED);
1249 mutex_unlock(&video->lock);
1253 mutex_unlock(&video->lock);
1255 ret = vb2_streamoff(&video->buffer_queue, video->buffer_queue.type);
1259 static int vpfe_buffer_init(struct vb2_buffer *vb)
1261 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
1262 struct vpfe_cap_buffer *buf = container_of(vbuf,
1263 struct vpfe_cap_buffer, vb);
1265 INIT_LIST_HEAD(&buf->list);
1269 /* abort streaming and wait for last buffer */
1270 static void vpfe_stop_streaming(struct vb2_queue *vq)
1272 struct vpfe_fh *fh = vb2_get_drv_priv(vq);
1273 struct vpfe_video_device *video = fh->video;
1275 /* release all active buffers */
1276 if (video->cur_frm == video->next_frm) {
1277 vb2_buffer_done(&video->cur_frm->vb.vb2_buf,
1278 VB2_BUF_STATE_ERROR);
1280 if (video->cur_frm != NULL)
1281 vb2_buffer_done(&video->cur_frm->vb.vb2_buf,
1282 VB2_BUF_STATE_ERROR);
1283 if (video->next_frm != NULL)
1284 vb2_buffer_done(&video->next_frm->vb.vb2_buf,
1285 VB2_BUF_STATE_ERROR);
1288 while (!list_empty(&video->dma_queue)) {
1289 video->next_frm = list_entry(video->dma_queue.next,
1290 struct vpfe_cap_buffer, list);
1291 list_del(&video->next_frm->list);
1292 vb2_buffer_done(&video->next_frm->vb.vb2_buf,
1293 VB2_BUF_STATE_ERROR);
1297 static void vpfe_buf_cleanup(struct vb2_buffer *vb)
1299 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
1300 struct vpfe_fh *fh = vb2_get_drv_priv(vb->vb2_queue);
1301 struct vpfe_video_device *video = fh->video;
1302 struct vpfe_device *vpfe_dev = video->vpfe_dev;
1303 struct vpfe_cap_buffer *buf = container_of(vbuf,
1304 struct vpfe_cap_buffer, vb);
1306 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_buf_cleanup\n");
1307 if (vb->state == VB2_BUF_STATE_ACTIVE)
1308 list_del_init(&buf->list);
1311 static struct vb2_ops video_qops = {
1312 .queue_setup = vpfe_buffer_queue_setup,
1313 .buf_init = vpfe_buffer_init,
1314 .buf_prepare = vpfe_buffer_prepare,
1315 .start_streaming = vpfe_start_streaming,
1316 .stop_streaming = vpfe_stop_streaming,
1317 .buf_cleanup = vpfe_buf_cleanup,
1318 .buf_queue = vpfe_buffer_queue,
1322 * vpfe_reqbufs() - supported REQBUF only once opening
1325 static int vpfe_reqbufs(struct file *file, void *priv,
1326 struct v4l2_requestbuffers *req_buf)
1328 struct vpfe_video_device *video = video_drvdata(file);
1329 struct vpfe_device *vpfe_dev = video->vpfe_dev;
1330 struct vpfe_fh *fh = file->private_data;
1331 struct vb2_queue *q;
1334 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_reqbufs\n");
1336 if (req_buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1337 req_buf->type != V4L2_BUF_TYPE_VIDEO_OUTPUT){
1338 v4l2_err(&vpfe_dev->v4l2_dev, "Invalid buffer type\n");
1342 ret = mutex_lock_interruptible(&video->lock);
1346 if (video->io_usrs != 0) {
1347 v4l2_err(&vpfe_dev->v4l2_dev, "Only one IO user allowed\n");
1351 video->memory = req_buf->memory;
1353 /* Initialize videobuf2 queue as per the buffer type */
1354 q = &video->buffer_queue;
1355 q->type = req_buf->type;
1356 q->io_modes = VB2_MMAP | VB2_USERPTR;
1358 q->min_buffers_needed = 1;
1359 q->ops = &video_qops;
1360 q->mem_ops = &vb2_dma_contig_memops;
1361 q->buf_struct_size = sizeof(struct vpfe_cap_buffer);
1362 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1363 q->dev = vpfe_dev->pdev;
1365 ret = vb2_queue_init(q);
1367 v4l2_err(&vpfe_dev->v4l2_dev, "vb2_queue_init() failed\n");
1373 INIT_LIST_HEAD(&video->dma_queue);
1374 ret = vb2_reqbufs(&video->buffer_queue, req_buf);
1377 mutex_unlock(&video->lock);
1382 * vpfe_querybuf() - query buffers for exchange
1384 static int vpfe_querybuf(struct file *file, void *priv,
1385 struct v4l2_buffer *buf)
1387 struct vpfe_video_device *video = video_drvdata(file);
1388 struct vpfe_device *vpfe_dev = video->vpfe_dev;
1390 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_querybuf\n");
1392 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1393 buf->type != V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1394 v4l2_err(&vpfe_dev->v4l2_dev, "Invalid buf type\n");
1398 if (video->memory != V4L2_MEMORY_MMAP) {
1399 v4l2_err(&vpfe_dev->v4l2_dev, "Invalid memory\n");
1403 /* Call vb2_querybuf to get information */
1404 return vb2_querybuf(&video->buffer_queue, buf);
1408 * vpfe_qbuf() - queue buffers for capture or processing
1410 static int vpfe_qbuf(struct file *file, void *priv,
1411 struct v4l2_buffer *p)
1413 struct vpfe_video_device *video = video_drvdata(file);
1414 struct vpfe_device *vpfe_dev = video->vpfe_dev;
1415 struct vpfe_fh *fh = file->private_data;
1417 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_qbuf\n");
1419 if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1420 p->type != V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1421 v4l2_err(&vpfe_dev->v4l2_dev, "Invalid buf type\n");
1425 * If this file handle is not allowed to do IO,
1428 if (!fh->io_allowed) {
1429 v4l2_err(&vpfe_dev->v4l2_dev, "fh->io_allowed\n");
1433 return vb2_qbuf(&video->buffer_queue, p);
1437 * vpfe_dqbuf() - deque buffer which is done with processing
1439 static int vpfe_dqbuf(struct file *file, void *priv,
1440 struct v4l2_buffer *buf)
1442 struct vpfe_video_device *video = video_drvdata(file);
1443 struct vpfe_device *vpfe_dev = video->vpfe_dev;
1445 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_dqbuf\n");
1447 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1448 buf->type != V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1449 v4l2_err(&vpfe_dev->v4l2_dev, "Invalid buf type\n");
1453 return vb2_dqbuf(&video->buffer_queue,
1454 buf, (file->f_flags & O_NONBLOCK));
1458 * vpfe_streamon() - start streaming
1459 * @file: file pointer
1460 * @priv: void pointer
1461 * @buf_type: enum v4l2_buf_type
1463 * queue buffer onto hardware for capture/processing and
1464 * start all the subdevs which are in media chain
1466 * Return 0 on success, error code otherwise
1468 static int vpfe_streamon(struct file *file, void *priv,
1469 enum v4l2_buf_type buf_type)
1471 struct vpfe_video_device *video = video_drvdata(file);
1472 struct vpfe_device *vpfe_dev = video->vpfe_dev;
1473 struct vpfe_pipeline *pipe = &video->pipe;
1474 struct vpfe_fh *fh = file->private_data;
1475 struct vpfe_ext_subdev_info *sdinfo;
1478 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_streamon\n");
1480 if (buf_type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1481 buf_type != V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1482 v4l2_err(&vpfe_dev->v4l2_dev, "Invalid buf type\n");
1485 /* If file handle is not allowed IO, return error */
1486 if (!fh->io_allowed) {
1487 v4l2_err(&vpfe_dev->v4l2_dev, "fh->io_allowed\n");
1490 sdinfo = video->current_ext_subdev;
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 video->video_dev.release = video_device_release;
1608 video->video_dev.fops = &vpfe_fops;
1609 video->video_dev.ioctl_ops = &vpfe_ioctl_ops;
1610 video->video_dev.minor = -1;
1611 video->video_dev.tvnorms = 0;
1612 snprintf(video->video_dev.name, sizeof(video->video_dev.name),
1613 "DAVINCI VIDEO %s %s", name, direction);
1615 spin_lock_init(&video->irqlock);
1616 spin_lock_init(&video->dma_queue_lock);
1617 mutex_init(&video->lock);
1618 ret = media_entity_pads_init(&video->video_dev.entity,
1623 video_set_drvdata(&video->video_dev, video);
1628 /* vpfe video device register function */
1629 int vpfe_video_register(struct vpfe_video_device *video,
1630 struct v4l2_device *vdev)
1634 video->video_dev.v4l2_dev = vdev;
1636 ret = video_register_device(&video->video_dev, VFL_TYPE_GRABBER, -1);
1638 pr_err("%s: could not register video device (%d)\n",
1643 /* vpfe video device unregister function */
1644 void vpfe_video_unregister(struct vpfe_video_device *video)
1646 if (video_is_registered(&video->video_dev)) {
1647 video_unregister_device(&video->video_dev);
1648 media_entity_cleanup(&video->video_dev.entity);