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;
176 if (vpfe_prepare_pipeline(video))
177 return vpfe_prepare_pipeline(video);
180 * Find out if there is any input video
181 * if yes, it is single shot.
183 if (pipe->input_num == 0) {
184 pipe->state = VPFE_PIPELINE_STREAM_CONTINUOUS;
185 if (vpfe_update_current_ext_subdev(video)) {
186 pr_err("Invalid external subdev\n");
187 return vpfe_update_current_ext_subdev(video);
190 pipe->state = VPFE_PIPELINE_STREAM_SINGLESHOT;
192 video->initialized = 1;
193 video->skip_frame_count = 1;
194 video->skip_frame_count_init = 1;
198 /* checks wether pipeline is ready for enabling */
199 int vpfe_video_is_pipe_ready(struct vpfe_pipeline *pipe)
203 for (i = 0; i < pipe->input_num; i++)
204 if (!pipe->inputs[i]->started ||
205 pipe->inputs[i]->state != VPFE_VIDEO_BUFFER_QUEUED)
207 for (i = 0; i < pipe->output_num; i++)
208 if (!pipe->outputs[i]->started ||
209 pipe->outputs[i]->state != VPFE_VIDEO_BUFFER_QUEUED)
215 * Validate a pipeline by checking both ends of all links for format
218 * Return 0 if all formats match, or -EPIPE if at least one link is found with
219 * different formats on its two ends.
221 static int vpfe_video_validate_pipeline(struct vpfe_pipeline *pipe)
223 struct v4l2_subdev_format fmt_source;
224 struct v4l2_subdev_format fmt_sink;
225 struct v4l2_subdev *subdev;
226 struct media_pad *pad;
230 * Should not matter if it is output[0] or 1 as
231 * the general ideas is to traverse backwards and
232 * the fact that the out video node always has the
233 * format of the connected pad.
235 subdev = vpfe_video_remote_subdev(pipe->outputs[0], NULL);
240 /* Retrieve the sink format */
241 pad = &subdev->entity.pads[0];
242 if (!(pad->flags & MEDIA_PAD_FL_SINK))
245 fmt_sink.which = V4L2_SUBDEV_FORMAT_ACTIVE;
246 fmt_sink.pad = pad->index;
247 ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL,
250 if (ret < 0 && ret != -ENOIOCTLCMD)
253 /* Retrieve the source format */
254 pad = media_entity_remote_pad(pad);
255 if (!pad || !is_media_entity_v4l2_subdev(pad->entity))
258 subdev = media_entity_to_v4l2_subdev(pad->entity);
260 fmt_source.which = V4L2_SUBDEV_FORMAT_ACTIVE;
261 fmt_source.pad = pad->index;
262 ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &fmt_source);
263 if (ret < 0 && ret != -ENOIOCTLCMD)
266 /* Check if the two ends match */
267 if (fmt_source.format.code != fmt_sink.format.code ||
268 fmt_source.format.width != fmt_sink.format.width ||
269 fmt_source.format.height != fmt_sink.format.height)
276 * vpfe_pipeline_enable() - Enable streaming on a pipeline
277 * @vpfe_dev: vpfe device
278 * @pipe: vpfe pipeline
280 * Walk the entities chain starting at the pipeline output video node and start
281 * all modules in the chain in the given mode.
283 * Return 0 if successful, or the return value of the failed video::s_stream
284 * operation otherwise.
286 static int vpfe_pipeline_enable(struct vpfe_pipeline *pipe)
288 struct media_entity *entity;
289 struct v4l2_subdev *subdev;
290 struct media_device *mdev;
293 if (pipe->state == VPFE_PIPELINE_STREAM_CONTINUOUS)
294 entity = vpfe_get_input_entity(pipe->outputs[0]);
296 entity = &pipe->inputs[0]->video_dev.entity;
298 mdev = entity->graph_obj.mdev;
299 mutex_lock(&mdev->graph_mutex);
300 ret = media_entity_graph_walk_init(&pipe->graph,
301 entity->graph_obj.mdev);
304 media_entity_graph_walk_start(&pipe->graph, entity);
305 while ((entity = media_entity_graph_walk_next(&pipe->graph))) {
307 if (!is_media_entity_v4l2_subdev(entity))
309 subdev = media_entity_to_v4l2_subdev(entity);
310 ret = v4l2_subdev_call(subdev, video, s_stream, 1);
311 if (ret < 0 && ret != -ENOIOCTLCMD)
316 media_entity_graph_walk_cleanup(&pipe->graph);
317 mutex_unlock(&mdev->graph_mutex);
322 * vpfe_pipeline_disable() - Disable streaming on a pipeline
323 * @vpfe_dev: vpfe device
324 * @pipe: VPFE pipeline
326 * Walk the entities chain starting at the pipeline output video node and stop
327 * all modules in the chain.
329 * Return 0 if all modules have been properly stopped, or -ETIMEDOUT if a module
332 static int vpfe_pipeline_disable(struct vpfe_pipeline *pipe)
334 struct media_entity *entity;
335 struct v4l2_subdev *subdev;
336 struct media_device *mdev;
339 if (pipe->state == VPFE_PIPELINE_STREAM_CONTINUOUS)
340 entity = vpfe_get_input_entity(pipe->outputs[0]);
342 entity = &pipe->inputs[0]->video_dev.entity;
344 mdev = entity->graph_obj.mdev;
345 mutex_lock(&mdev->graph_mutex);
346 media_entity_graph_walk_start(&pipe->graph, entity);
348 while ((entity = media_entity_graph_walk_next(&pipe->graph))) {
350 if (!is_media_entity_v4l2_subdev(entity))
352 subdev = media_entity_to_v4l2_subdev(entity);
353 ret = v4l2_subdev_call(subdev, video, s_stream, 0);
354 if (ret < 0 && ret != -ENOIOCTLCMD)
357 mutex_unlock(&mdev->graph_mutex);
359 media_entity_graph_walk_cleanup(&pipe->graph);
360 return ret ? -ETIMEDOUT : 0;
364 * vpfe_pipeline_set_stream() - Enable/disable streaming on a pipeline
365 * @vpfe_dev: VPFE device
366 * @pipe: VPFE pipeline
367 * @state: Stream state (stopped or active)
369 * Set the pipeline to the given stream state.
371 * Return 0 if successful, or the return value of the failed video::s_stream
372 * operation otherwise.
374 static int vpfe_pipeline_set_stream(struct vpfe_pipeline *pipe,
375 enum vpfe_pipeline_stream_state state)
377 if (state == VPFE_PIPELINE_STREAM_STOPPED)
378 return vpfe_pipeline_disable(pipe);
380 return vpfe_pipeline_enable(pipe);
383 static int all_videos_stopped(struct vpfe_video_device *video)
385 struct vpfe_pipeline *pipe = &video->pipe;
388 for (i = 0; i < pipe->input_num; i++)
389 if (pipe->inputs[i]->started)
391 for (i = 0; i < pipe->output_num; i++)
392 if (pipe->outputs[i]->started)
398 * vpfe_open() - open video device
399 * @file: file pointer
401 * initialize media pipeline state, allocate memory for file handle
403 * Return 0 if successful, or the return -ENODEV otherwise.
405 static int vpfe_open(struct file *file)
407 struct vpfe_video_device *video = video_drvdata(file);
408 struct vpfe_fh *handle;
410 /* Allocate memory for the file handle object */
411 handle = kzalloc(sizeof(struct vpfe_fh), GFP_KERNEL);
416 v4l2_fh_init(&handle->vfh, &video->video_dev);
417 v4l2_fh_add(&handle->vfh);
419 mutex_lock(&video->lock);
420 /* If decoder is not initialized. initialize it */
421 if (!video->initialized && vpfe_update_pipe_state(video)) {
422 mutex_unlock(&video->lock);
425 /* Increment device users counter */
427 /* Set io_allowed member to false */
428 handle->io_allowed = 0;
429 handle->video = video;
430 file->private_data = &handle->vfh;
431 mutex_unlock(&video->lock);
436 /* get the next buffer available from dma queue */
438 vpfe_video_get_next_buffer(struct vpfe_video_device *video)
440 video->cur_frm = video->next_frm =
441 list_entry(video->dma_queue.next,
442 struct vpfe_cap_buffer, list);
444 list_del(&video->next_frm->list);
445 video->next_frm->vb.vb2_buf.state = VB2_BUF_STATE_ACTIVE;
446 return vb2_dma_contig_plane_dma_addr(&video->next_frm->vb.vb2_buf, 0);
449 /* schedule the next buffer which is available on dma queue */
450 void vpfe_video_schedule_next_buffer(struct vpfe_video_device *video)
452 struct vpfe_device *vpfe_dev = video->vpfe_dev;
455 if (list_empty(&video->dma_queue))
458 video->next_frm = list_entry(video->dma_queue.next,
459 struct vpfe_cap_buffer, list);
461 if (video->pipe.state == VPFE_PIPELINE_STREAM_SINGLESHOT)
462 video->cur_frm = video->next_frm;
464 list_del(&video->next_frm->list);
465 video->next_frm->vb.vb2_buf.state = VB2_BUF_STATE_ACTIVE;
466 addr = vb2_dma_contig_plane_dma_addr(&video->next_frm->vb.vb2_buf, 0);
467 video->ops->queue(vpfe_dev, addr);
468 video->state = VPFE_VIDEO_BUFFER_QUEUED;
471 /* schedule the buffer for capturing bottom field */
472 void vpfe_video_schedule_bottom_field(struct vpfe_video_device *video)
474 struct vpfe_device *vpfe_dev = video->vpfe_dev;
477 addr = vb2_dma_contig_plane_dma_addr(&video->cur_frm->vb.vb2_buf, 0);
478 addr += video->field_off;
479 video->ops->queue(vpfe_dev, addr);
482 /* make buffer available for dequeue */
483 void vpfe_video_process_buffer_complete(struct vpfe_video_device *video)
485 struct vpfe_pipeline *pipe = &video->pipe;
487 video->cur_frm->vb.vb2_buf.timestamp = ktime_get_ns();
488 vb2_buffer_done(&video->cur_frm->vb.vb2_buf, VB2_BUF_STATE_DONE);
489 if (pipe->state == VPFE_PIPELINE_STREAM_CONTINUOUS)
490 video->cur_frm = video->next_frm;
493 /* vpfe_stop_capture() - stop streaming */
494 static void vpfe_stop_capture(struct vpfe_video_device *video)
496 struct vpfe_pipeline *pipe = &video->pipe;
500 if (video->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
502 if (all_videos_stopped(video))
503 vpfe_pipeline_set_stream(pipe,
504 VPFE_PIPELINE_STREAM_STOPPED);
508 * vpfe_release() - release video device
509 * @file: file pointer
511 * deletes buffer queue, frees the buffers and the vpfe file handle
515 static int vpfe_release(struct file *file)
517 struct vpfe_video_device *video = video_drvdata(file);
518 struct v4l2_fh *vfh = file->private_data;
519 struct vpfe_device *vpfe_dev = video->vpfe_dev;
520 struct vpfe_fh *fh = container_of(vfh, struct vpfe_fh, vfh);
522 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_release\n");
524 /* Get the device lock */
525 mutex_lock(&video->lock);
526 /* if this instance is doing IO */
527 if (fh->io_allowed) {
528 if (video->started) {
529 vpfe_stop_capture(video);
531 * mark pipe state as stopped in vpfe_release(),
532 * as app might call streamon() after streamoff()
533 * in which case driver has to start streaming.
535 video->pipe.state = VPFE_PIPELINE_STREAM_STOPPED;
536 vb2_streamoff(&video->buffer_queue,
537 video->buffer_queue.type);
540 /* Free buffers allocated */
541 vb2_queue_release(&video->buffer_queue);
542 vb2_dma_contig_cleanup_ctx(video->alloc_ctx);
544 /* Decrement device users counter */
546 v4l2_fh_del(&fh->vfh);
547 v4l2_fh_exit(&fh->vfh);
548 /* If this is the last file handle */
550 video->initialized = 0;
551 mutex_unlock(&video->lock);
552 file->private_data = NULL;
553 /* Free memory allocated to file handle object */
560 * vpfe_mmap() - It is used to map kernel space buffers
563 static int vpfe_mmap(struct file *file, struct vm_area_struct *vma)
565 struct vpfe_video_device *video = video_drvdata(file);
566 struct vpfe_device *vpfe_dev = video->vpfe_dev;
568 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_mmap\n");
569 return vb2_mmap(&video->buffer_queue, vma);
573 * vpfe_poll() - It is used for select/poll system call
575 static unsigned int vpfe_poll(struct file *file, poll_table *wait)
577 struct vpfe_video_device *video = video_drvdata(file);
578 struct vpfe_device *vpfe_dev = video->vpfe_dev;
580 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_poll\n");
582 return vb2_poll(&video->buffer_queue, file, wait);
586 /* vpfe capture driver file operations */
587 static const struct v4l2_file_operations vpfe_fops = {
588 .owner = THIS_MODULE,
590 .release = vpfe_release,
591 .unlocked_ioctl = video_ioctl2,
597 * vpfe_querycap() - query capabilities of video device
598 * @file: file pointer
599 * @priv: void pointer
600 * @cap: pointer to v4l2_capability structure
602 * fills v4l2 capabilities structure
606 static int vpfe_querycap(struct file *file, void *priv,
607 struct v4l2_capability *cap)
609 struct vpfe_video_device *video = video_drvdata(file);
610 struct vpfe_device *vpfe_dev = video->vpfe_dev;
612 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_querycap\n");
614 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
615 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
617 cap->device_caps = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_STREAMING;
618 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VIDEO_OUTPUT |
619 V4L2_CAP_STREAMING | V4L2_CAP_DEVICE_CAPS;
620 strlcpy(cap->driver, CAPTURE_DRV_NAME, sizeof(cap->driver));
621 strlcpy(cap->bus_info, "VPFE", sizeof(cap->bus_info));
622 strlcpy(cap->card, vpfe_dev->cfg->card_name, sizeof(cap->card));
628 * vpfe_g_fmt() - get the format which is active on video device
629 * @file: file pointer
630 * @priv: void pointer
631 * @fmt: pointer to v4l2_format structure
633 * fills v4l2 format structure with active format
637 static int vpfe_g_fmt(struct file *file, void *priv,
638 struct v4l2_format *fmt)
640 struct vpfe_video_device *video = video_drvdata(file);
641 struct vpfe_device *vpfe_dev = video->vpfe_dev;
643 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_g_fmt\n");
644 /* Fill in the information about format */
650 * vpfe_enum_fmt() - enum formats supported on media chain
651 * @file: file pointer
652 * @priv: void pointer
653 * @fmt: pointer to v4l2_fmtdesc structure
655 * fills v4l2_fmtdesc structure with output format set on adjacent subdev,
656 * only one format is enumearted as subdevs are already configured
658 * Return 0 if successful, error code otherwise
660 static int vpfe_enum_fmt(struct file *file, void *priv,
661 struct v4l2_fmtdesc *fmt)
663 struct vpfe_video_device *video = video_drvdata(file);
664 struct vpfe_device *vpfe_dev = video->vpfe_dev;
665 struct v4l2_subdev_format sd_fmt;
666 struct v4l2_mbus_framefmt mbus;
667 struct v4l2_subdev *subdev;
668 struct v4l2_format format;
669 struct media_pad *remote;
671 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_enum_fmt\n");
674 * since already subdev pad format is set,
675 * only one pixel format is available
677 if (fmt->index > 0) {
678 v4l2_err(&vpfe_dev->v4l2_dev, "Invalid index\n");
681 /* get the remote pad */
682 remote = media_entity_remote_pad(&video->pad);
683 if (remote == NULL) {
684 v4l2_err(&vpfe_dev->v4l2_dev,
685 "invalid remote pad for video node\n");
688 /* get the remote subdev */
689 subdev = vpfe_video_remote_subdev(video, NULL);
690 if (subdev == NULL) {
691 v4l2_err(&vpfe_dev->v4l2_dev,
692 "invalid remote subdev for video node\n");
695 sd_fmt.pad = remote->index;
696 sd_fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
697 /* get output format of remote subdev */
698 if (v4l2_subdev_call(subdev, pad, get_fmt, NULL, &sd_fmt)) {
699 v4l2_err(&vpfe_dev->v4l2_dev,
700 "invalid remote subdev for video node\n");
701 return v4l2_subdev_call(subdev, pad, get_fmt, NULL, &sd_fmt);
703 /* convert to pix format */
704 mbus.code = sd_fmt.format.code;
705 mbus_to_pix(&mbus, &format.fmt.pix);
706 /* copy the result */
707 fmt->pixelformat = format.fmt.pix.pixelformat;
713 * vpfe_s_fmt() - set the format on video device
714 * @file: file pointer
715 * @priv: void pointer
716 * @fmt: pointer to v4l2_format structure
718 * validate and set the format on video device
720 * Return 0 on success, error code otherwise
722 static int vpfe_s_fmt(struct file *file, void *priv,
723 struct v4l2_format *fmt)
725 struct vpfe_video_device *video = video_drvdata(file);
726 struct vpfe_device *vpfe_dev = video->vpfe_dev;
727 struct v4l2_format format;
729 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_s_fmt\n");
730 /* If streaming is started, return error */
731 if (video->started) {
732 v4l2_err(&vpfe_dev->v4l2_dev, "Streaming is started\n");
735 /* get adjacent subdev's output pad format */
736 if (__vpfe_video_get_format(video, &format))
737 return __vpfe_video_get_format(video, &format);
744 * vpfe_try_fmt() - try the format on video device
745 * @file: file pointer
746 * @priv: void pointer
747 * @fmt: pointer to v4l2_format structure
749 * validate the format, update with correct format
750 * based on output format set on adjacent subdev
752 * Return 0 on success, error code otherwise
754 static int vpfe_try_fmt(struct file *file, void *priv,
755 struct v4l2_format *fmt)
757 struct vpfe_video_device *video = video_drvdata(file);
758 struct vpfe_device *vpfe_dev = video->vpfe_dev;
759 struct v4l2_format format;
761 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_try_fmt\n");
762 /* get adjacent subdev's output pad format */
763 if (__vpfe_video_get_format(video, &format))
764 return __vpfe_video_get_format(video, &format);
771 * vpfe_enum_input() - enum inputs supported on media chain
772 * @file: file pointer
773 * @priv: void pointer
774 * @fmt: pointer to v4l2_fmtdesc structure
776 * fills v4l2_input structure with input available on media chain,
777 * only one input is enumearted as media chain is setup by this time
779 * Return 0 if successful, -EINVAL is media chain is invalid
781 static int vpfe_enum_input(struct file *file, void *priv,
782 struct v4l2_input *inp)
784 struct vpfe_video_device *video = video_drvdata(file);
785 struct vpfe_ext_subdev_info *sdinfo = video->current_ext_subdev;
786 struct vpfe_device *vpfe_dev = video->vpfe_dev;
788 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_enum_input\n");
789 /* enumerate from the subdev user has chosen through mc */
790 if (inp->index < sdinfo->num_inputs) {
791 memcpy(inp, &sdinfo->inputs[inp->index],
792 sizeof(struct v4l2_input));
799 * vpfe_g_input() - get index of the input which is active
800 * @file: file pointer
801 * @priv: void pointer
802 * @index: pointer to unsigned int
804 * set index with input index which is active
806 static int vpfe_g_input(struct file *file, void *priv, unsigned int *index)
808 struct vpfe_video_device *video = video_drvdata(file);
809 struct vpfe_device *vpfe_dev = video->vpfe_dev;
811 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_g_input\n");
813 *index = video->current_input;
818 * vpfe_s_input() - set input which is pointed by input index
819 * @file: file pointer
820 * @priv: void pointer
821 * @index: pointer to unsigned int
823 * set input on external subdev
825 * Return 0 on success, error code otherwise
827 static int vpfe_s_input(struct file *file, void *priv, unsigned int index)
829 struct vpfe_video_device *video = video_drvdata(file);
830 struct vpfe_device *vpfe_dev = video->vpfe_dev;
831 struct vpfe_ext_subdev_info *sdinfo;
832 struct vpfe_route *route;
833 struct v4l2_input *inps;
839 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_s_input\n");
841 if (mutex_lock_interruptible(&video->lock))
842 return mutex_lock_interruptible(&video->lock);
844 * If streaming is started return device busy
847 if (video->started) {
848 v4l2_err(&vpfe_dev->v4l2_dev, "Streaming is on\n");
853 sdinfo = video->current_ext_subdev;
854 if (!sdinfo->registered) {
858 if (vpfe_dev->cfg->setup_input &&
859 vpfe_dev->cfg->setup_input(sdinfo->grp_id) < 0) {
861 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev,
862 "couldn't setup input for %s\n",
863 sdinfo->module_name);
866 route = &sdinfo->routes[index];
867 if (route && sdinfo->can_route) {
868 input = route->input;
869 output = route->output;
870 ret = v4l2_device_call_until_err(&vpfe_dev->v4l2_dev,
871 sdinfo->grp_id, video,
872 s_routing, input, output, 0);
874 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev,
875 "s_input:error in setting input in decoder\n");
880 /* set standards set by subdev in video device */
881 for (i = 0; i < sdinfo->num_inputs; i++) {
882 inps = &sdinfo->inputs[i];
883 video->video_dev.tvnorms |= inps->std;
885 video->current_input = index;
887 mutex_unlock(&video->lock);
892 * vpfe_querystd() - query std which is being input on external subdev
893 * @file: file pointer
894 * @priv: void pointer
895 * @std_id: pointer to v4l2_std_id structure
897 * call external subdev through v4l2_device_call_until_err to
898 * get the std that is being active.
900 * Return 0 on success, error code otherwise
902 static int vpfe_querystd(struct file *file, void *priv, v4l2_std_id *std_id)
904 struct vpfe_video_device *video = video_drvdata(file);
905 struct vpfe_device *vpfe_dev = video->vpfe_dev;
906 struct vpfe_ext_subdev_info *sdinfo;
909 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_querystd\n");
911 ret = mutex_lock_interruptible(&video->lock);
912 sdinfo = video->current_ext_subdev;
915 /* Call querystd function of decoder device */
916 ret = v4l2_device_call_until_err(&vpfe_dev->v4l2_dev, sdinfo->grp_id,
917 video, querystd, std_id);
918 mutex_unlock(&video->lock);
923 * vpfe_s_std() - set std on external subdev
924 * @file: file pointer
925 * @priv: void pointer
926 * @std_id: pointer to v4l2_std_id structure
928 * set std pointed by std_id on external subdev by calling it using
929 * v4l2_device_call_until_err
931 * Return 0 on success, error code otherwise
933 static int vpfe_s_std(struct file *file, void *priv, v4l2_std_id std_id)
935 struct vpfe_video_device *video = video_drvdata(file);
936 struct vpfe_device *vpfe_dev = video->vpfe_dev;
937 struct vpfe_ext_subdev_info *sdinfo;
940 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_s_std\n");
942 /* Call decoder driver function to set the standard */
943 if (mutex_lock_interruptible(&video->lock))
944 return mutex_lock_interruptible(&video->lock);
945 sdinfo = video->current_ext_subdev;
946 /* If streaming is started, return device busy error */
947 if (video->started) {
948 v4l2_err(&vpfe_dev->v4l2_dev, "streaming is started\n");
952 ret = v4l2_device_call_until_err(&vpfe_dev->v4l2_dev, sdinfo->grp_id,
953 video, s_std, std_id);
955 v4l2_err(&vpfe_dev->v4l2_dev, "Failed to set standard\n");
956 video->stdid = V4L2_STD_UNKNOWN;
959 video->stdid = std_id;
961 mutex_unlock(&video->lock);
965 static int vpfe_g_std(struct file *file, void *priv, v4l2_std_id *tvnorm)
967 struct vpfe_video_device *video = video_drvdata(file);
968 struct vpfe_device *vpfe_dev = video->vpfe_dev;
970 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_g_std\n");
971 *tvnorm = video->stdid;
976 * vpfe_enum_dv_timings() - enumerate dv_timings which are supported by
978 * @file: file pointer
979 * @priv: void pointer
980 * @timings: pointer to v4l2_enum_dv_timings structure
982 * enum dv_timings's which are supported by external subdev through
985 * Return 0 on success, error code otherwise
988 vpfe_enum_dv_timings(struct file *file, void *fh,
989 struct v4l2_enum_dv_timings *timings)
991 struct vpfe_video_device *video = video_drvdata(file);
992 struct vpfe_device *vpfe_dev = video->vpfe_dev;
993 struct v4l2_subdev *subdev = video->current_ext_subdev->subdev;
997 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_enum_dv_timings\n");
998 return v4l2_subdev_call(subdev, pad, enum_dv_timings, timings);
1002 * vpfe_query_dv_timings() - query the dv_timings which is being input
1003 * to external subdev
1004 * @file: file pointer
1005 * @priv: void pointer
1006 * @timings: pointer to v4l2_dv_timings structure
1008 * get dv_timings which is being input on external subdev through
1011 * Return 0 on success, error code otherwise
1014 vpfe_query_dv_timings(struct file *file, void *fh,
1015 struct v4l2_dv_timings *timings)
1017 struct vpfe_video_device *video = video_drvdata(file);
1018 struct vpfe_device *vpfe_dev = video->vpfe_dev;
1019 struct v4l2_subdev *subdev = video->current_ext_subdev->subdev;
1021 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_query_dv_timings\n");
1022 return v4l2_subdev_call(subdev, video, query_dv_timings, timings);
1026 * vpfe_s_dv_timings() - set dv_timings on external subdev
1027 * @file: file pointer
1028 * @priv: void pointer
1029 * @timings: pointer to v4l2_dv_timings structure
1031 * set dv_timings pointed by timings on external subdev through
1032 * v4l2_device_call_until_err, this configures amplifier also
1034 * Return 0 on success, error code otherwise
1037 vpfe_s_dv_timings(struct file *file, void *fh,
1038 struct v4l2_dv_timings *timings)
1040 struct vpfe_video_device *video = video_drvdata(file);
1041 struct vpfe_device *vpfe_dev = video->vpfe_dev;
1043 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_s_dv_timings\n");
1045 video->stdid = V4L2_STD_UNKNOWN;
1046 return v4l2_device_call_until_err(&vpfe_dev->v4l2_dev,
1047 video->current_ext_subdev->grp_id,
1048 video, s_dv_timings, timings);
1052 * vpfe_g_dv_timings() - get dv_timings which is set on external subdev
1053 * @file: file pointer
1054 * @priv: void pointer
1055 * @timings: pointer to v4l2_dv_timings structure
1057 * get dv_timings which is set on external subdev through
1060 * Return 0 on success, error code otherwise
1063 vpfe_g_dv_timings(struct file *file, void *fh,
1064 struct v4l2_dv_timings *timings)
1066 struct vpfe_video_device *video = video_drvdata(file);
1067 struct vpfe_device *vpfe_dev = video->vpfe_dev;
1068 struct v4l2_subdev *subdev = video->current_ext_subdev->subdev;
1070 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_g_dv_timings\n");
1071 return v4l2_subdev_call(subdev, video, g_dv_timings, timings);
1075 * Videobuf operations
1078 * vpfe_buffer_queue_setup : Callback function for buffer setup.
1079 * @vq: vb2_queue ptr
1081 * @nbuffers: ptr to number of buffers requested by application
1082 * @nplanes:: contains number of distinct video planes needed to hold a frame
1083 * @sizes[]: contains the size (in bytes) of each plane.
1084 * @alloc_ctxs: ptr to allocation context
1086 * This callback function is called when reqbuf() is called to adjust
1087 * the buffer nbuffers and buffer size
1090 vpfe_buffer_queue_setup(struct vb2_queue *vq,
1091 unsigned int *nbuffers, unsigned int *nplanes,
1092 unsigned int sizes[], void *alloc_ctxs[])
1094 struct vpfe_fh *fh = vb2_get_drv_priv(vq);
1095 struct vpfe_video_device *video = fh->video;
1096 struct vpfe_device *vpfe_dev = video->vpfe_dev;
1099 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_buffer_queue_setup\n");
1100 size = video->fmt.fmt.pix.sizeimage;
1102 if (vq->num_buffers + *nbuffers < 3)
1103 *nbuffers = 3 - vq->num_buffers;
1107 alloc_ctxs[0] = video->alloc_ctx;
1108 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev,
1109 "nbuffers=%d, size=%lu\n", *nbuffers, size);
1114 * vpfe_buffer_prepare : callback function for buffer prepare
1115 * @vb: ptr to vb2_buffer
1117 * This is the callback function for buffer prepare when vb2_qbuf()
1118 * function is called. The buffer is prepared and user space virtual address
1119 * or user address is converted into physical address
1121 static int vpfe_buffer_prepare(struct vb2_buffer *vb)
1123 struct vpfe_fh *fh = vb2_get_drv_priv(vb->vb2_queue);
1124 struct vpfe_video_device *video = fh->video;
1125 struct vpfe_device *vpfe_dev = video->vpfe_dev;
1128 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_buffer_prepare\n");
1130 if (vb->state != VB2_BUF_STATE_ACTIVE &&
1131 vb->state != VB2_BUF_STATE_PREPARED)
1134 /* Initialize buffer */
1135 vb2_set_plane_payload(vb, 0, video->fmt.fmt.pix.sizeimage);
1136 if (vb2_plane_vaddr(vb, 0) &&
1137 vb2_get_plane_payload(vb, 0) > vb2_plane_size(vb, 0))
1140 addr = vb2_dma_contig_plane_dma_addr(vb, 0);
1141 /* Make sure user addresses are aligned to 32 bytes */
1142 if (!ALIGN(addr, 32))
1148 static void vpfe_buffer_queue(struct vb2_buffer *vb)
1150 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
1151 /* Get the file handle object and device object */
1152 struct vpfe_fh *fh = vb2_get_drv_priv(vb->vb2_queue);
1153 struct vpfe_video_device *video = fh->video;
1154 struct vpfe_device *vpfe_dev = video->vpfe_dev;
1155 struct vpfe_pipeline *pipe = &video->pipe;
1156 struct vpfe_cap_buffer *buf = container_of(vbuf,
1157 struct vpfe_cap_buffer, vb);
1158 unsigned long flags;
1159 unsigned long empty;
1162 spin_lock_irqsave(&video->dma_queue_lock, flags);
1163 empty = list_empty(&video->dma_queue);
1164 /* add the buffer to the DMA queue */
1165 list_add_tail(&buf->list, &video->dma_queue);
1166 spin_unlock_irqrestore(&video->dma_queue_lock, flags);
1167 /* this case happens in case of single shot */
1168 if (empty && video->started && pipe->state ==
1169 VPFE_PIPELINE_STREAM_SINGLESHOT &&
1170 video->state == VPFE_VIDEO_BUFFER_NOT_QUEUED) {
1171 spin_lock(&video->dma_queue_lock);
1172 addr = vpfe_video_get_next_buffer(video);
1173 video->ops->queue(vpfe_dev, addr);
1175 video->state = VPFE_VIDEO_BUFFER_QUEUED;
1176 spin_unlock(&video->dma_queue_lock);
1178 /* enable h/w each time in single shot */
1179 if (vpfe_video_is_pipe_ready(pipe))
1180 vpfe_pipeline_set_stream(pipe,
1181 VPFE_PIPELINE_STREAM_SINGLESHOT);
1185 /* vpfe_start_capture() - start streaming on all the subdevs */
1186 static int vpfe_start_capture(struct vpfe_video_device *video)
1188 struct vpfe_pipeline *pipe = &video->pipe;
1192 if (vpfe_video_is_pipe_ready(pipe))
1193 ret = vpfe_pipeline_set_stream(pipe, pipe->state);
1198 static int vpfe_start_streaming(struct vb2_queue *vq, unsigned int count)
1200 struct vpfe_fh *fh = vb2_get_drv_priv(vq);
1201 struct vpfe_video_device *video = fh->video;
1202 struct vpfe_device *vpfe_dev = video->vpfe_dev;
1206 ret = mutex_lock_interruptible(&video->lock);
1210 /* Get the next frame from the buffer queue */
1211 video->cur_frm = video->next_frm =
1212 list_entry(video->dma_queue.next, struct vpfe_cap_buffer, list);
1213 /* Remove buffer from the buffer queue */
1214 list_del(&video->cur_frm->list);
1215 /* Mark state of the current frame to active */
1216 video->cur_frm->vb.vb2_buf.state = VB2_BUF_STATE_ACTIVE;
1217 /* Initialize field_id and started member */
1218 video->field_id = 0;
1219 addr = vb2_dma_contig_plane_dma_addr(&video->cur_frm->vb.vb2_buf, 0);
1220 video->ops->queue(vpfe_dev, addr);
1221 video->state = VPFE_VIDEO_BUFFER_QUEUED;
1223 ret = vpfe_start_capture(video);
1225 struct vpfe_cap_buffer *buf, *tmp;
1227 vb2_buffer_done(&video->cur_frm->vb.vb2_buf,
1228 VB2_BUF_STATE_QUEUED);
1229 list_for_each_entry_safe(buf, tmp, &video->dma_queue, list) {
1230 list_del(&buf->list);
1231 vb2_buffer_done(&buf->vb.vb2_buf,
1232 VB2_BUF_STATE_QUEUED);
1237 mutex_unlock(&video->lock);
1241 mutex_unlock(&video->lock);
1243 ret = vb2_streamoff(&video->buffer_queue, video->buffer_queue.type);
1247 static int vpfe_buffer_init(struct vb2_buffer *vb)
1249 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
1250 struct vpfe_cap_buffer *buf = container_of(vbuf,
1251 struct vpfe_cap_buffer, vb);
1253 INIT_LIST_HEAD(&buf->list);
1257 /* abort streaming and wait for last buffer */
1258 static void vpfe_stop_streaming(struct vb2_queue *vq)
1260 struct vpfe_fh *fh = vb2_get_drv_priv(vq);
1261 struct vpfe_video_device *video = fh->video;
1263 /* release all active buffers */
1264 if (video->cur_frm == video->next_frm) {
1265 vb2_buffer_done(&video->cur_frm->vb.vb2_buf,
1266 VB2_BUF_STATE_ERROR);
1268 if (video->cur_frm != NULL)
1269 vb2_buffer_done(&video->cur_frm->vb.vb2_buf,
1270 VB2_BUF_STATE_ERROR);
1271 if (video->next_frm != NULL)
1272 vb2_buffer_done(&video->next_frm->vb.vb2_buf,
1273 VB2_BUF_STATE_ERROR);
1276 while (!list_empty(&video->dma_queue)) {
1277 video->next_frm = list_entry(video->dma_queue.next,
1278 struct vpfe_cap_buffer, list);
1279 list_del(&video->next_frm->list);
1280 vb2_buffer_done(&video->next_frm->vb.vb2_buf,
1281 VB2_BUF_STATE_ERROR);
1285 static void vpfe_buf_cleanup(struct vb2_buffer *vb)
1287 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
1288 struct vpfe_fh *fh = vb2_get_drv_priv(vb->vb2_queue);
1289 struct vpfe_video_device *video = fh->video;
1290 struct vpfe_device *vpfe_dev = video->vpfe_dev;
1291 struct vpfe_cap_buffer *buf = container_of(vbuf,
1292 struct vpfe_cap_buffer, vb);
1294 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_buf_cleanup\n");
1295 if (vb->state == VB2_BUF_STATE_ACTIVE)
1296 list_del_init(&buf->list);
1299 static struct vb2_ops video_qops = {
1300 .queue_setup = vpfe_buffer_queue_setup,
1301 .buf_init = vpfe_buffer_init,
1302 .buf_prepare = vpfe_buffer_prepare,
1303 .start_streaming = vpfe_start_streaming,
1304 .stop_streaming = vpfe_stop_streaming,
1305 .buf_cleanup = vpfe_buf_cleanup,
1306 .buf_queue = vpfe_buffer_queue,
1310 * vpfe_reqbufs() - supported REQBUF only once opening
1313 static int vpfe_reqbufs(struct file *file, void *priv,
1314 struct v4l2_requestbuffers *req_buf)
1316 struct vpfe_video_device *video = video_drvdata(file);
1317 struct vpfe_device *vpfe_dev = video->vpfe_dev;
1318 struct vpfe_fh *fh = file->private_data;
1319 struct vb2_queue *q;
1322 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_reqbufs\n");
1324 if (req_buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1325 req_buf->type != V4L2_BUF_TYPE_VIDEO_OUTPUT){
1326 v4l2_err(&vpfe_dev->v4l2_dev, "Invalid buffer type\n");
1330 if (mutex_lock_interruptible(&video->lock))
1331 return mutex_lock_interruptible(&video->lock);
1333 if (video->io_usrs != 0) {
1334 v4l2_err(&vpfe_dev->v4l2_dev, "Only one IO user allowed\n");
1338 video->memory = req_buf->memory;
1340 /* Initialize videobuf2 queue as per the buffer type */
1341 video->alloc_ctx = vb2_dma_contig_init_ctx(vpfe_dev->pdev);
1342 if (IS_ERR(video->alloc_ctx)) {
1343 v4l2_err(&vpfe_dev->v4l2_dev, "Failed to get the context\n");
1344 return PTR_ERR(video->alloc_ctx);
1347 q = &video->buffer_queue;
1348 q->type = req_buf->type;
1349 q->io_modes = VB2_MMAP | VB2_USERPTR;
1351 q->min_buffers_needed = 1;
1352 q->ops = &video_qops;
1353 q->mem_ops = &vb2_dma_contig_memops;
1354 q->buf_struct_size = sizeof(struct vpfe_cap_buffer);
1355 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1357 if (vb2_queue_init(q)) {
1358 v4l2_err(&vpfe_dev->v4l2_dev, "vb2_queue_init() failed\n");
1359 vb2_dma_contig_cleanup_ctx(vpfe_dev->pdev);
1360 return vb2_queue_init(q);
1365 INIT_LIST_HEAD(&video->dma_queue);
1366 ret = vb2_reqbufs(&video->buffer_queue, req_buf);
1369 mutex_unlock(&video->lock);
1374 * vpfe_querybuf() - query buffers for exchange
1376 static int vpfe_querybuf(struct file *file, void *priv,
1377 struct v4l2_buffer *buf)
1379 struct vpfe_video_device *video = video_drvdata(file);
1380 struct vpfe_device *vpfe_dev = video->vpfe_dev;
1382 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_querybuf\n");
1384 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1385 buf->type != V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1386 v4l2_err(&vpfe_dev->v4l2_dev, "Invalid buf type\n");
1390 if (video->memory != V4L2_MEMORY_MMAP) {
1391 v4l2_err(&vpfe_dev->v4l2_dev, "Invalid memory\n");
1395 /* Call vb2_querybuf to get information */
1396 return vb2_querybuf(&video->buffer_queue, buf);
1400 * vpfe_qbuf() - queue buffers for capture or processing
1402 static int vpfe_qbuf(struct file *file, void *priv,
1403 struct v4l2_buffer *p)
1405 struct vpfe_video_device *video = video_drvdata(file);
1406 struct vpfe_device *vpfe_dev = video->vpfe_dev;
1407 struct vpfe_fh *fh = file->private_data;
1409 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_qbuf\n");
1411 if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1412 p->type != V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1413 v4l2_err(&vpfe_dev->v4l2_dev, "Invalid buf type\n");
1417 * If this file handle is not allowed to do IO,
1420 if (!fh->io_allowed) {
1421 v4l2_err(&vpfe_dev->v4l2_dev, "fh->io_allowed\n");
1425 return vb2_qbuf(&video->buffer_queue, p);
1429 * vpfe_dqbuf() - deque buffer which is done with processing
1431 static int vpfe_dqbuf(struct file *file, void *priv,
1432 struct v4l2_buffer *buf)
1434 struct vpfe_video_device *video = video_drvdata(file);
1435 struct vpfe_device *vpfe_dev = video->vpfe_dev;
1437 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_dqbuf\n");
1439 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1440 buf->type != V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1441 v4l2_err(&vpfe_dev->v4l2_dev, "Invalid buf type\n");
1445 return vb2_dqbuf(&video->buffer_queue,
1446 buf, (file->f_flags & O_NONBLOCK));
1450 * vpfe_streamon() - start streaming
1451 * @file: file pointer
1452 * @priv: void pointer
1453 * @buf_type: enum v4l2_buf_type
1455 * queue buffer onto hardware for capture/processing and
1456 * start all the subdevs which are in media chain
1458 * Return 0 on success, error code otherwise
1460 static int vpfe_streamon(struct file *file, void *priv,
1461 enum v4l2_buf_type buf_type)
1463 struct vpfe_video_device *video = video_drvdata(file);
1464 struct vpfe_device *vpfe_dev = video->vpfe_dev;
1465 struct vpfe_pipeline *pipe = &video->pipe;
1466 struct vpfe_fh *fh = file->private_data;
1467 struct vpfe_ext_subdev_info *sdinfo;
1470 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_streamon\n");
1472 if (buf_type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1473 buf_type != V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1474 v4l2_err(&vpfe_dev->v4l2_dev, "Invalid buf type\n");
1477 /* If file handle is not allowed IO, return error */
1478 if (!fh->io_allowed) {
1479 v4l2_err(&vpfe_dev->v4l2_dev, "fh->io_allowed\n");
1482 sdinfo = video->current_ext_subdev;
1483 /* If buffer queue is empty, return error */
1484 if (list_empty(&video->buffer_queue.queued_list)) {
1485 v4l2_err(&vpfe_dev->v4l2_dev, "buffer queue is empty\n");
1488 /* Validate the pipeline */
1489 if (buf_type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1490 ret = vpfe_video_validate_pipeline(pipe);
1494 /* Call vb2_streamon to start streaming */
1495 return vb2_streamon(&video->buffer_queue, buf_type);
1499 * vpfe_streamoff() - stop streaming
1500 * @file: file pointer
1501 * @priv: void pointer
1502 * @buf_type: enum v4l2_buf_type
1504 * stop all the subdevs which are in media chain
1506 * Return 0 on success, error code otherwise
1508 static int vpfe_streamoff(struct file *file, void *priv,
1509 enum v4l2_buf_type buf_type)
1511 struct vpfe_video_device *video = video_drvdata(file);
1512 struct vpfe_device *vpfe_dev = video->vpfe_dev;
1513 struct vpfe_fh *fh = file->private_data;
1516 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_streamoff\n");
1518 if (buf_type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1519 buf_type != V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1520 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "Invalid buf type\n");
1524 /* If io is allowed for this file handle, return error */
1525 if (!fh->io_allowed) {
1526 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "fh->io_allowed\n");
1530 /* If streaming is not started, return error */
1531 if (!video->started) {
1532 v4l2_err(&vpfe_dev->v4l2_dev, "device is not started\n");
1536 if (mutex_lock_interruptible(&video->lock))
1537 return mutex_lock_interruptible(&video->lock);
1539 vpfe_stop_capture(video);
1540 ret = vb2_streamoff(&video->buffer_queue, buf_type);
1541 mutex_unlock(&video->lock);
1546 /* vpfe capture ioctl operations */
1547 static const struct v4l2_ioctl_ops vpfe_ioctl_ops = {
1548 .vidioc_querycap = vpfe_querycap,
1549 .vidioc_g_fmt_vid_cap = vpfe_g_fmt,
1550 .vidioc_s_fmt_vid_cap = vpfe_s_fmt,
1551 .vidioc_try_fmt_vid_cap = vpfe_try_fmt,
1552 .vidioc_enum_fmt_vid_cap = vpfe_enum_fmt,
1553 .vidioc_g_fmt_vid_out = vpfe_g_fmt,
1554 .vidioc_s_fmt_vid_out = vpfe_s_fmt,
1555 .vidioc_try_fmt_vid_out = vpfe_try_fmt,
1556 .vidioc_enum_fmt_vid_out = vpfe_enum_fmt,
1557 .vidioc_enum_input = vpfe_enum_input,
1558 .vidioc_g_input = vpfe_g_input,
1559 .vidioc_s_input = vpfe_s_input,
1560 .vidioc_querystd = vpfe_querystd,
1561 .vidioc_s_std = vpfe_s_std,
1562 .vidioc_g_std = vpfe_g_std,
1563 .vidioc_enum_dv_timings = vpfe_enum_dv_timings,
1564 .vidioc_query_dv_timings = vpfe_query_dv_timings,
1565 .vidioc_s_dv_timings = vpfe_s_dv_timings,
1566 .vidioc_g_dv_timings = vpfe_g_dv_timings,
1567 .vidioc_reqbufs = vpfe_reqbufs,
1568 .vidioc_querybuf = vpfe_querybuf,
1569 .vidioc_qbuf = vpfe_qbuf,
1570 .vidioc_dqbuf = vpfe_dqbuf,
1571 .vidioc_streamon = vpfe_streamon,
1572 .vidioc_streamoff = vpfe_streamoff,
1575 /* VPFE video init function */
1576 int vpfe_video_init(struct vpfe_video_device *video, const char *name)
1578 const char *direction;
1581 switch (video->type) {
1582 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1583 direction = "output";
1584 video->pad.flags = MEDIA_PAD_FL_SINK;
1585 video->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1588 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
1589 direction = "input";
1590 video->pad.flags = MEDIA_PAD_FL_SOURCE;
1591 video->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
1597 /* Initialize field of video device */
1598 video->video_dev.release = video_device_release;
1599 video->video_dev.fops = &vpfe_fops;
1600 video->video_dev.ioctl_ops = &vpfe_ioctl_ops;
1601 video->video_dev.minor = -1;
1602 video->video_dev.tvnorms = 0;
1603 snprintf(video->video_dev.name, sizeof(video->video_dev.name),
1604 "DAVINCI VIDEO %s %s", name, direction);
1606 spin_lock_init(&video->irqlock);
1607 spin_lock_init(&video->dma_queue_lock);
1608 mutex_init(&video->lock);
1609 ret = media_entity_pads_init(&video->video_dev.entity,
1614 video_set_drvdata(&video->video_dev, video);
1619 /* vpfe video device register function */
1620 int vpfe_video_register(struct vpfe_video_device *video,
1621 struct v4l2_device *vdev)
1625 video->video_dev.v4l2_dev = vdev;
1627 ret = video_register_device(&video->video_dev, VFL_TYPE_GRABBER, -1);
1629 pr_err("%s: could not register video device (%d)\n",
1634 /* vpfe video device unregister function */
1635 void vpfe_video_unregister(struct vpfe_video_device *video)
1637 if (video_is_registered(&video->video_dev)) {
1638 video_unregister_device(&video->video_dev);
1639 media_entity_cleanup(&video->video_dev.entity);