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"
30 /* minimum number of buffers needed in cont-mode */
31 #define MIN_NUM_BUFFERS 3
35 /* get v4l2 subdev pointer to external subdev which is active */
36 static struct media_entity *vpfe_get_input_entity
37 (struct vpfe_video_device *video)
39 struct vpfe_device *vpfe_dev = video->vpfe_dev;
40 struct media_pad *remote;
42 remote = media_entity_remote_pad(&vpfe_dev->vpfe_isif.pads[0]);
44 pr_err("Invalid media connection to isif/ccdc\n");
47 return remote->entity;
50 /* updates external subdev(sensor/decoder) which is active */
51 static int vpfe_update_current_ext_subdev(struct vpfe_video_device *video)
53 struct vpfe_device *vpfe_dev = video->vpfe_dev;
54 struct vpfe_config *vpfe_cfg;
55 struct v4l2_subdev *subdev;
56 struct media_pad *remote;
59 remote = media_entity_remote_pad(&vpfe_dev->vpfe_isif.pads[0]);
61 pr_err("Invalid media connection to isif/ccdc\n");
65 subdev = media_entity_to_v4l2_subdev(remote->entity);
66 vpfe_cfg = vpfe_dev->pdev->platform_data;
67 for (i = 0; i < vpfe_cfg->num_subdevs; i++) {
68 if (!strcmp(vpfe_cfg->sub_devs[i].module_name, subdev->name)) {
69 video->current_ext_subdev = &vpfe_cfg->sub_devs[i];
74 /* if user not linked decoder/sensor to isif/ccdc */
75 if (i == vpfe_cfg->num_subdevs) {
76 pr_err("Invalid media chain connection to isif/ccdc\n");
79 /* find the v4l2 subdev pointer */
80 for (i = 0; i < vpfe_dev->num_ext_subdevs; i++) {
81 if (!strcmp(video->current_ext_subdev->module_name,
82 vpfe_dev->sd[i]->name))
83 video->current_ext_subdev->subdev = vpfe_dev->sd[i];
88 /* get the subdev which is connected to the output video node */
89 static struct v4l2_subdev *
90 vpfe_video_remote_subdev(struct vpfe_video_device *video, u32 *pad)
92 struct media_pad *remote = media_entity_remote_pad(&video->pad);
94 if (remote == NULL || remote->entity->type != MEDIA_ENT_T_V4L2_SUBDEV)
98 return media_entity_to_v4l2_subdev(remote->entity);
101 /* get the format set at output pad of the adjacent subdev */
103 __vpfe_video_get_format(struct vpfe_video_device *video,
104 struct v4l2_format *format)
106 struct v4l2_subdev_format fmt;
107 struct v4l2_subdev *subdev;
108 struct media_pad *remote;
112 subdev = vpfe_video_remote_subdev(video, &pad);
116 fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
117 remote = media_entity_remote_pad(&video->pad);
118 fmt.pad = remote->index;
120 ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &fmt);
121 if (ret == -ENOIOCTLCMD)
124 format->type = video->type;
125 /* convert mbus_format to v4l2_format */
126 v4l2_fill_pix_format(&format->fmt.pix, &fmt.format);
127 mbus_to_pix(&fmt.format, &format->fmt.pix);
132 /* make a note of pipeline details */
133 static void vpfe_prepare_pipeline(struct vpfe_video_device *video)
135 struct media_entity *entity = &video->video_dev.entity;
136 struct media_device *mdev = entity->parent;
137 struct vpfe_pipeline *pipe = &video->pipe;
138 struct vpfe_video_device *far_end = NULL;
139 struct media_entity_graph graph;
142 pipe->output_num = 0;
144 if (video->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
145 pipe->inputs[pipe->input_num++] = video;
147 pipe->outputs[pipe->output_num++] = video;
149 mutex_lock(&mdev->graph_mutex);
150 media_entity_graph_walk_start(&graph, entity);
151 while ((entity = media_entity_graph_walk_next(&graph))) {
152 if (entity == &video->video_dev.entity)
154 if (media_entity_type(entity) != MEDIA_ENT_T_DEVNODE)
156 far_end = to_vpfe_video(media_entity_to_video_device(entity));
157 if (far_end->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
158 pipe->inputs[pipe->input_num++] = far_end;
160 pipe->outputs[pipe->output_num++] = far_end;
162 mutex_unlock(&mdev->graph_mutex);
165 /* update pipe state selected by user */
166 static int vpfe_update_pipe_state(struct vpfe_video_device *video)
168 struct vpfe_pipeline *pipe = &video->pipe;
171 vpfe_prepare_pipeline(video);
173 /* Find out if there is any input video
174 if yes, it is single shot.
176 if (pipe->input_num == 0) {
177 pipe->state = VPFE_PIPELINE_STREAM_CONTINUOUS;
178 ret = vpfe_update_current_ext_subdev(video);
180 pr_err("Invalid external subdev\n");
184 pipe->state = VPFE_PIPELINE_STREAM_SINGLESHOT;
186 video->initialized = 1;
187 video->skip_frame_count = 1;
188 video->skip_frame_count_init = 1;
192 /* checks wether pipeline is ready for enabling */
193 int vpfe_video_is_pipe_ready(struct vpfe_pipeline *pipe)
197 for (i = 0; i < pipe->input_num; i++)
198 if (!pipe->inputs[i]->started ||
199 pipe->inputs[i]->state != VPFE_VIDEO_BUFFER_QUEUED)
201 for (i = 0; i < pipe->output_num; i++)
202 if (!pipe->outputs[i]->started ||
203 pipe->outputs[i]->state != VPFE_VIDEO_BUFFER_QUEUED)
209 * Validate a pipeline by checking both ends of all links for format
212 * Return 0 if all formats match, or -EPIPE if at least one link is found with
213 * different formats on its two ends.
215 static int vpfe_video_validate_pipeline(struct vpfe_pipeline *pipe)
217 struct v4l2_subdev_format fmt_source;
218 struct v4l2_subdev_format fmt_sink;
219 struct v4l2_subdev *subdev;
220 struct media_pad *pad;
224 * Should not matter if it is output[0] or 1 as
225 * the general ideas is to traverse backwards and
226 * the fact that the out video node always has the
227 * format of the connected pad.
229 subdev = vpfe_video_remote_subdev(pipe->outputs[0], NULL);
234 /* Retrieve the sink format */
235 pad = &subdev->entity.pads[0];
236 if (!(pad->flags & MEDIA_PAD_FL_SINK))
239 fmt_sink.which = V4L2_SUBDEV_FORMAT_ACTIVE;
240 fmt_sink.pad = pad->index;
241 ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL,
244 if (ret < 0 && ret != -ENOIOCTLCMD)
247 /* Retrieve the source format */
248 pad = media_entity_remote_pad(pad);
250 pad->entity->type != MEDIA_ENT_T_V4L2_SUBDEV)
253 subdev = media_entity_to_v4l2_subdev(pad->entity);
255 fmt_source.which = V4L2_SUBDEV_FORMAT_ACTIVE;
256 fmt_source.pad = pad->index;
257 ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &fmt_source);
258 if (ret < 0 && ret != -ENOIOCTLCMD)
261 /* Check if the two ends match */
262 if (fmt_source.format.code != fmt_sink.format.code ||
263 fmt_source.format.width != fmt_sink.format.width ||
264 fmt_source.format.height != fmt_sink.format.height)
271 * vpfe_pipeline_enable() - Enable streaming on a pipeline
272 * @vpfe_dev: vpfe device
273 * @pipe: vpfe pipeline
275 * Walk the entities chain starting at the pipeline output video node and start
276 * all modules in the chain in the given mode.
278 * Return 0 if successful, or the return value of the failed video::s_stream
279 * operation otherwise.
281 static int vpfe_pipeline_enable(struct vpfe_pipeline *pipe)
283 struct media_entity_graph graph;
284 struct media_entity *entity;
285 struct v4l2_subdev *subdev;
286 struct media_device *mdev;
289 if (pipe->state == VPFE_PIPELINE_STREAM_CONTINUOUS)
290 entity = vpfe_get_input_entity(pipe->outputs[0]);
292 entity = &pipe->inputs[0]->video_dev.entity;
294 mdev = entity->parent;
295 mutex_lock(&mdev->graph_mutex);
296 media_entity_graph_walk_start(&graph, entity);
297 while ((entity = media_entity_graph_walk_next(&graph))) {
299 if (media_entity_type(entity) == MEDIA_ENT_T_DEVNODE)
301 subdev = media_entity_to_v4l2_subdev(entity);
302 ret = v4l2_subdev_call(subdev, video, s_stream, 1);
303 if (ret < 0 && ret != -ENOIOCTLCMD)
306 mutex_unlock(&mdev->graph_mutex);
311 * vpfe_pipeline_disable() - Disable streaming on a pipeline
312 * @vpfe_dev: vpfe device
313 * @pipe: VPFE pipeline
315 * Walk the entities chain starting at the pipeline output video node and stop
316 * all modules in the chain.
318 * Return 0 if all modules have been properly stopped, or -ETIMEDOUT if a module
321 static int vpfe_pipeline_disable(struct vpfe_pipeline *pipe)
323 struct media_entity_graph graph;
324 struct media_entity *entity;
325 struct v4l2_subdev *subdev;
326 struct media_device *mdev;
329 if (pipe->state == VPFE_PIPELINE_STREAM_CONTINUOUS)
330 entity = vpfe_get_input_entity(pipe->outputs[0]);
332 entity = &pipe->inputs[0]->video_dev.entity;
334 mdev = entity->parent;
335 mutex_lock(&mdev->graph_mutex);
336 media_entity_graph_walk_start(&graph, entity);
338 while ((entity = media_entity_graph_walk_next(&graph))) {
340 if (media_entity_type(entity) == MEDIA_ENT_T_DEVNODE)
342 subdev = media_entity_to_v4l2_subdev(entity);
343 ret = v4l2_subdev_call(subdev, video, s_stream, 0);
344 if (ret < 0 && ret != -ENOIOCTLCMD)
347 mutex_unlock(&mdev->graph_mutex);
349 return (ret == 0) ? ret : -ETIMEDOUT ;
353 * vpfe_pipeline_set_stream() - Enable/disable streaming on a pipeline
354 * @vpfe_dev: VPFE device
355 * @pipe: VPFE pipeline
356 * @state: Stream state (stopped or active)
358 * Set the pipeline to the given stream state.
360 * Return 0 if successful, or the return value of the failed video::s_stream
361 * operation otherwise.
363 static int vpfe_pipeline_set_stream(struct vpfe_pipeline *pipe,
364 enum vpfe_pipeline_stream_state state)
366 if (state == VPFE_PIPELINE_STREAM_STOPPED)
367 return vpfe_pipeline_disable(pipe);
369 return vpfe_pipeline_enable(pipe);
372 static int all_videos_stopped(struct vpfe_video_device *video)
374 struct vpfe_pipeline *pipe = &video->pipe;
377 for (i = 0; i < pipe->input_num; i++)
378 if (pipe->inputs[i]->started)
380 for (i = 0; i < pipe->output_num; i++)
381 if (pipe->outputs[i]->started)
387 * vpfe_open() - open video device
388 * @file: file pointer
390 * initialize media pipeline state, allocate memory for file handle
392 * Return 0 if successful, or the return -ENODEV otherwise.
394 static int vpfe_open(struct file *file)
396 struct vpfe_video_device *video = video_drvdata(file);
397 struct vpfe_fh *handle;
399 /* Allocate memory for the file handle object */
400 handle = kzalloc(sizeof(struct vpfe_fh), GFP_KERNEL);
405 v4l2_fh_init(&handle->vfh, &video->video_dev);
406 v4l2_fh_add(&handle->vfh);
408 mutex_lock(&video->lock);
409 /* If decoder is not initialized. initialize it */
410 if (!video->initialized && vpfe_update_pipe_state(video)) {
411 mutex_unlock(&video->lock);
414 /* Increment device users counter */
416 /* Set io_allowed member to false */
417 handle->io_allowed = 0;
418 v4l2_prio_open(&video->prio, &handle->prio);
419 handle->video = video;
420 file->private_data = &handle->vfh;
421 mutex_unlock(&video->lock);
426 /* get the next buffer available from dma queue */
428 vpfe_video_get_next_buffer(struct vpfe_video_device *video)
430 video->cur_frm = video->next_frm =
431 list_entry(video->dma_queue.next,
432 struct vpfe_cap_buffer, list);
434 list_del(&video->next_frm->list);
435 video->next_frm->vb.state = VB2_BUF_STATE_ACTIVE;
436 return vb2_dma_contig_plane_dma_addr(&video->next_frm->vb, 0);
439 /* schedule the next buffer which is available on dma queue */
440 void vpfe_video_schedule_next_buffer(struct vpfe_video_device *video)
442 struct vpfe_device *vpfe_dev = video->vpfe_dev;
445 if (list_empty(&video->dma_queue))
448 video->next_frm = list_entry(video->dma_queue.next,
449 struct vpfe_cap_buffer, list);
451 if (VPFE_PIPELINE_STREAM_SINGLESHOT == video->pipe.state)
452 video->cur_frm = video->next_frm;
454 list_del(&video->next_frm->list);
455 video->next_frm->vb.state = VB2_BUF_STATE_ACTIVE;
456 addr = vb2_dma_contig_plane_dma_addr(&video->next_frm->vb, 0);
457 video->ops->queue(vpfe_dev, addr);
458 video->state = VPFE_VIDEO_BUFFER_QUEUED;
461 /* schedule the buffer for capturing bottom field */
462 void vpfe_video_schedule_bottom_field(struct vpfe_video_device *video)
464 struct vpfe_device *vpfe_dev = video->vpfe_dev;
467 addr = vb2_dma_contig_plane_dma_addr(&video->cur_frm->vb, 0);
468 addr += video->field_off;
469 video->ops->queue(vpfe_dev, addr);
472 /* make buffer available for dequeue */
473 void vpfe_video_process_buffer_complete(struct vpfe_video_device *video)
475 struct vpfe_pipeline *pipe = &video->pipe;
477 do_gettimeofday(&video->cur_frm->vb.v4l2_buf.timestamp);
478 vb2_buffer_done(&video->cur_frm->vb, VB2_BUF_STATE_DONE);
479 if (pipe->state == VPFE_PIPELINE_STREAM_CONTINUOUS)
480 video->cur_frm = video->next_frm;
483 /* vpfe_stop_capture() - stop streaming */
484 static void vpfe_stop_capture(struct vpfe_video_device *video)
486 struct vpfe_pipeline *pipe = &video->pipe;
490 if (video->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
492 if (all_videos_stopped(video))
493 vpfe_pipeline_set_stream(pipe,
494 VPFE_PIPELINE_STREAM_STOPPED);
498 * vpfe_release() - release video device
499 * @file: file pointer
501 * deletes buffer queue, frees the buffers and the vpfe file handle
505 static int vpfe_release(struct file *file)
507 struct vpfe_video_device *video = video_drvdata(file);
508 struct v4l2_fh *vfh = file->private_data;
509 struct vpfe_device *vpfe_dev = video->vpfe_dev;
510 struct vpfe_fh *fh = container_of(vfh, struct vpfe_fh, vfh);
512 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_release\n");
514 /* Get the device lock */
515 mutex_lock(&video->lock);
516 /* if this instance is doing IO */
517 if (fh->io_allowed) {
518 if (video->started) {
519 vpfe_stop_capture(video);
520 /* mark pipe state as stopped in vpfe_release(),
521 as app might call streamon() after streamoff()
522 in which case driver has to start streaming.
524 video->pipe.state = VPFE_PIPELINE_STREAM_STOPPED;
525 vb2_streamoff(&video->buffer_queue,
526 video->buffer_queue.type);
529 /* Free buffers allocated */
530 vb2_queue_release(&video->buffer_queue);
531 vb2_dma_contig_cleanup_ctx(video->alloc_ctx);
533 /* Decrement device users counter */
535 /* Close the priority */
536 v4l2_prio_close(&video->prio, fh->prio);
537 /* If this is the last file handle */
539 video->initialized = 0;
540 mutex_unlock(&video->lock);
541 file->private_data = NULL;
542 /* Free memory allocated to file handle object */
549 * vpfe_mmap() - It is used to map kernel space buffers
552 static int vpfe_mmap(struct file *file, struct vm_area_struct *vma)
554 struct vpfe_video_device *video = video_drvdata(file);
555 struct vpfe_device *vpfe_dev = video->vpfe_dev;
557 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_mmap\n");
558 return vb2_mmap(&video->buffer_queue, vma);
562 * vpfe_poll() - It is used for select/poll system call
564 static unsigned int vpfe_poll(struct file *file, poll_table *wait)
566 struct vpfe_video_device *video = video_drvdata(file);
567 struct vpfe_device *vpfe_dev = video->vpfe_dev;
569 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_poll\n");
571 return vb2_poll(&video->buffer_queue, file, wait);
575 /* vpfe capture driver file operations */
576 static const struct v4l2_file_operations vpfe_fops = {
577 .owner = THIS_MODULE,
579 .release = vpfe_release,
580 .unlocked_ioctl = video_ioctl2,
586 * vpfe_querycap() - query capabilities of video device
587 * @file: file pointer
588 * @priv: void pointer
589 * @cap: pointer to v4l2_capability structure
591 * fills v4l2 capabilities structure
595 static int vpfe_querycap(struct file *file, void *priv,
596 struct v4l2_capability *cap)
598 struct vpfe_video_device *video = video_drvdata(file);
599 struct vpfe_device *vpfe_dev = video->vpfe_dev;
601 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_querycap\n");
603 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
604 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
606 cap->capabilities = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_STREAMING;
607 cap->device_caps = cap->capabilities;
608 cap->version = VPFE_CAPTURE_VERSION_CODE;
609 strlcpy(cap->driver, CAPTURE_DRV_NAME, sizeof(cap->driver));
610 strlcpy(cap->bus_info, "VPFE", sizeof(cap->bus_info));
611 strlcpy(cap->card, vpfe_dev->cfg->card_name, sizeof(cap->card));
617 * vpfe_g_fmt() - get the format which is active on video device
618 * @file: file pointer
619 * @priv: void pointer
620 * @fmt: pointer to v4l2_format structure
622 * fills v4l2 format structure with active format
626 static int vpfe_g_fmt(struct file *file, void *priv,
627 struct v4l2_format *fmt)
629 struct vpfe_video_device *video = video_drvdata(file);
630 struct vpfe_device *vpfe_dev = video->vpfe_dev;
632 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_g_fmt\n");
633 /* Fill in the information about format */
639 * vpfe_enum_fmt() - enum formats supported on media chain
640 * @file: file pointer
641 * @priv: void pointer
642 * @fmt: pointer to v4l2_fmtdesc structure
644 * fills v4l2_fmtdesc structure with output format set on adjacent subdev,
645 * only one format is enumearted as subdevs are already configured
647 * Return 0 if successful, error code otherwise
649 static int vpfe_enum_fmt(struct file *file, void *priv,
650 struct v4l2_fmtdesc *fmt)
652 struct vpfe_video_device *video = video_drvdata(file);
653 struct vpfe_device *vpfe_dev = video->vpfe_dev;
654 struct v4l2_subdev_format sd_fmt;
655 struct v4l2_mbus_framefmt mbus;
656 struct v4l2_subdev *subdev;
657 struct v4l2_format format;
658 struct media_pad *remote;
661 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_enum_fmt\n");
663 /* since already subdev pad format is set,
664 only one pixel format is available */
665 if (fmt->index > 0) {
666 v4l2_err(&vpfe_dev->v4l2_dev, "Invalid index\n");
669 /* get the remote pad */
670 remote = media_entity_remote_pad(&video->pad);
671 if (remote == NULL) {
672 v4l2_err(&vpfe_dev->v4l2_dev,
673 "invalid remote pad for video node\n");
676 /* get the remote subdev */
677 subdev = vpfe_video_remote_subdev(video, NULL);
678 if (subdev == NULL) {
679 v4l2_err(&vpfe_dev->v4l2_dev,
680 "invalid remote subdev for video node\n");
683 sd_fmt.pad = remote->index;
684 sd_fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
685 /* get output format of remote subdev */
686 ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &sd_fmt);
688 v4l2_err(&vpfe_dev->v4l2_dev,
689 "invalid remote subdev for video node\n");
692 /* convert to pix format */
693 mbus.code = sd_fmt.format.code;
694 mbus_to_pix(&mbus, &format.fmt.pix);
695 /* copy the result */
696 fmt->pixelformat = format.fmt.pix.pixelformat;
702 * vpfe_s_fmt() - set the format on video device
703 * @file: file pointer
704 * @priv: void pointer
705 * @fmt: pointer to v4l2_format structure
707 * validate and set the format on video device
709 * Return 0 on success, error code otherwise
711 static int vpfe_s_fmt(struct file *file, void *priv,
712 struct v4l2_format *fmt)
714 struct vpfe_video_device *video = video_drvdata(file);
715 struct vpfe_device *vpfe_dev = video->vpfe_dev;
716 struct v4l2_format format;
719 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_s_fmt\n");
720 /* If streaming is started, return error */
721 if (video->started) {
722 v4l2_err(&vpfe_dev->v4l2_dev, "Streaming is started\n");
725 /* get adjacent subdev's output pad format */
726 ret = __vpfe_video_get_format(video, &format);
735 * vpfe_try_fmt() - try the format on video device
736 * @file: file pointer
737 * @priv: void pointer
738 * @fmt: pointer to v4l2_format structure
740 * validate the format, update with correct format
741 * based on output format set on adjacent subdev
743 * Return 0 on success, error code otherwise
745 static int vpfe_try_fmt(struct file *file, void *priv,
746 struct v4l2_format *fmt)
748 struct vpfe_video_device *video = video_drvdata(file);
749 struct vpfe_device *vpfe_dev = video->vpfe_dev;
750 struct v4l2_format format;
753 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_try_fmt\n");
754 /* get adjacent subdev's output pad format */
755 ret = __vpfe_video_get_format(video, &format);
764 * vpfe_enum_input() - enum inputs supported on media chain
765 * @file: file pointer
766 * @priv: void pointer
767 * @fmt: pointer to v4l2_fmtdesc structure
769 * fills v4l2_input structure with input available on media chain,
770 * only one input is enumearted as media chain is setup by this time
772 * Return 0 if successful, -EINVAL is media chain is invalid
774 static int vpfe_enum_input(struct file *file, void *priv,
775 struct v4l2_input *inp)
777 struct vpfe_video_device *video = video_drvdata(file);
778 struct vpfe_ext_subdev_info *sdinfo = video->current_ext_subdev;
779 struct vpfe_device *vpfe_dev = video->vpfe_dev;
781 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_enum_input\n");
782 /* enumerate from the subdev user has chosen through mc */
783 if (inp->index < sdinfo->num_inputs) {
784 memcpy(inp, &sdinfo->inputs[inp->index],
785 sizeof(struct v4l2_input));
792 * vpfe_g_input() - get index of the input which is active
793 * @file: file pointer
794 * @priv: void pointer
795 * @index: pointer to unsigned int
797 * set index with input index which is active
799 static int vpfe_g_input(struct file *file, void *priv, unsigned int *index)
801 struct vpfe_video_device *video = video_drvdata(file);
802 struct vpfe_device *vpfe_dev = video->vpfe_dev;
804 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_g_input\n");
806 *index = video->current_input;
811 * vpfe_s_input() - set input which is pointed by input index
812 * @file: file pointer
813 * @priv: void pointer
814 * @index: pointer to unsigned int
816 * set input on external subdev
818 * Return 0 on success, error code otherwise
820 static int vpfe_s_input(struct file *file, void *priv, unsigned int index)
822 struct vpfe_video_device *video = video_drvdata(file);
823 struct vpfe_device *vpfe_dev = video->vpfe_dev;
824 struct vpfe_ext_subdev_info *sdinfo;
825 struct vpfe_route *route;
826 struct v4l2_input *inps;
832 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_s_input\n");
834 ret = mutex_lock_interruptible(&video->lock);
838 * If streaming is started return device busy
841 if (video->started) {
842 v4l2_err(&vpfe_dev->v4l2_dev, "Streaming is on\n");
847 sdinfo = video->current_ext_subdev;
848 if (!sdinfo->registered) {
852 if (vpfe_dev->cfg->setup_input &&
853 vpfe_dev->cfg->setup_input(sdinfo->grp_id) < 0) {
855 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev,
856 "couldn't setup input for %s\n",
857 sdinfo->module_name);
860 route = &sdinfo->routes[index];
861 if (route && sdinfo->can_route) {
862 input = route->input;
863 output = route->output;
864 ret = v4l2_device_call_until_err(&vpfe_dev->v4l2_dev,
865 sdinfo->grp_id, video,
866 s_routing, input, output, 0);
868 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev,
869 "s_input:error in setting input in decoder\n");
874 /* set standards set by subdev in video device */
875 for (i = 0; i < sdinfo->num_inputs; i++) {
876 inps = &sdinfo->inputs[i];
877 video->video_dev.tvnorms |= inps->std;
879 video->current_input = index;
881 mutex_unlock(&video->lock);
886 * vpfe_querystd() - query std which is being input on external subdev
887 * @file: file pointer
888 * @priv: void pointer
889 * @std_id: pointer to v4l2_std_id structure
891 * call external subdev through v4l2_device_call_until_err to
892 * get the std that is being active.
894 * Return 0 on success, error code otherwise
896 static int vpfe_querystd(struct file *file, void *priv, v4l2_std_id *std_id)
898 struct vpfe_video_device *video = video_drvdata(file);
899 struct vpfe_device *vpfe_dev = video->vpfe_dev;
900 struct vpfe_ext_subdev_info *sdinfo;
903 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_querystd\n");
905 ret = mutex_lock_interruptible(&video->lock);
906 sdinfo = video->current_ext_subdev;
909 /* Call querystd function of decoder device */
910 ret = v4l2_device_call_until_err(&vpfe_dev->v4l2_dev, sdinfo->grp_id,
911 video, querystd, std_id);
912 mutex_unlock(&video->lock);
917 * vpfe_s_std() - set std on external subdev
918 * @file: file pointer
919 * @priv: void pointer
920 * @std_id: pointer to v4l2_std_id structure
922 * set std pointed by std_id on external subdev by calling it using
923 * v4l2_device_call_until_err
925 * Return 0 on success, error code otherwise
927 static int vpfe_s_std(struct file *file, void *priv, v4l2_std_id std_id)
929 struct vpfe_video_device *video = video_drvdata(file);
930 struct vpfe_device *vpfe_dev = video->vpfe_dev;
931 struct vpfe_ext_subdev_info *sdinfo;
934 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_s_std\n");
936 /* Call decoder driver function to set the standard */
937 ret = mutex_lock_interruptible(&video->lock);
940 sdinfo = video->current_ext_subdev;
941 /* If streaming is started, return device busy error */
942 if (video->started) {
943 v4l2_err(&vpfe_dev->v4l2_dev, "streaming is started\n");
947 ret = v4l2_device_call_until_err(&vpfe_dev->v4l2_dev, sdinfo->grp_id,
948 core, s_std, std_id);
950 v4l2_err(&vpfe_dev->v4l2_dev, "Failed to set standard\n");
951 video->stdid = V4L2_STD_UNKNOWN;
954 video->stdid = std_id;
956 mutex_unlock(&video->lock);
960 static int vpfe_g_std(struct file *file, void *priv, v4l2_std_id *tvnorm)
962 struct vpfe_video_device *video = video_drvdata(file);
963 struct vpfe_device *vpfe_dev = video->vpfe_dev;
965 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_g_std\n");
966 *tvnorm = video->stdid;
971 * vpfe_enum_dv_timings() - enumerate dv_timings which are supported by
973 * @file: file pointer
974 * @priv: void pointer
975 * @timings: pointer to v4l2_enum_dv_timings structure
977 * enum dv_timings's which are supported by external subdev through
980 * Return 0 on success, error code otherwise
983 vpfe_enum_dv_timings(struct file *file, void *fh,
984 struct v4l2_enum_dv_timings *timings)
986 struct vpfe_video_device *video = video_drvdata(file);
987 struct vpfe_device *vpfe_dev = video->vpfe_dev;
988 struct v4l2_subdev *subdev = video->current_ext_subdev->subdev;
990 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_enum_dv_timings\n");
991 return v4l2_subdev_call(subdev, video, enum_dv_timings, timings);
995 * vpfe_query_dv_timings() - query the dv_timings which is being input
997 * @file: file pointer
998 * @priv: void pointer
999 * @timings: pointer to v4l2_dv_timings structure
1001 * get dv_timings which is being input on external subdev through
1004 * Return 0 on success, error code otherwise
1007 vpfe_query_dv_timings(struct file *file, void *fh,
1008 struct v4l2_dv_timings *timings)
1010 struct vpfe_video_device *video = video_drvdata(file);
1011 struct vpfe_device *vpfe_dev = video->vpfe_dev;
1012 struct v4l2_subdev *subdev = video->current_ext_subdev->subdev;
1014 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_query_dv_timings\n");
1015 return v4l2_subdev_call(subdev, video, query_dv_timings, timings);
1019 * vpfe_s_dv_timings() - set dv_timings on external subdev
1020 * @file: file pointer
1021 * @priv: void pointer
1022 * @timings: pointer to v4l2_dv_timings structure
1024 * set dv_timings pointed by timings on external subdev through
1025 * v4l2_device_call_until_err, this configures amplifier also
1027 * Return 0 on success, error code otherwise
1030 vpfe_s_dv_timings(struct file *file, void *fh,
1031 struct v4l2_dv_timings *timings)
1033 struct vpfe_video_device *video = video_drvdata(file);
1034 struct vpfe_device *vpfe_dev = video->vpfe_dev;
1036 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_s_dv_timings\n");
1038 video->stdid = V4L2_STD_UNKNOWN;
1039 return v4l2_device_call_until_err(&vpfe_dev->v4l2_dev,
1040 video->current_ext_subdev->grp_id,
1041 video, s_dv_timings, timings);
1045 * vpfe_g_dv_timings() - get dv_timings which is set on external subdev
1046 * @file: file pointer
1047 * @priv: void pointer
1048 * @timings: pointer to v4l2_dv_timings structure
1050 * get dv_timings which is set on external subdev through
1053 * Return 0 on success, error code otherwise
1056 vpfe_g_dv_timings(struct file *file, void *fh,
1057 struct v4l2_dv_timings *timings)
1059 struct vpfe_video_device *video = video_drvdata(file);
1060 struct vpfe_device *vpfe_dev = video->vpfe_dev;
1061 struct v4l2_subdev *subdev = video->current_ext_subdev->subdev;
1063 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_g_dv_timings\n");
1064 return v4l2_subdev_call(subdev, video, g_dv_timings, timings);
1068 * Videobuf operations
1071 * vpfe_buffer_queue_setup : Callback function for buffer setup.
1072 * @vq: vb2_queue ptr
1074 * @nbuffers: ptr to number of buffers requested by application
1075 * @nplanes:: contains number of distinct video planes needed to hold a frame
1076 * @sizes[]: contains the size (in bytes) of each plane.
1077 * @alloc_ctxs: ptr to allocation context
1079 * This callback function is called when reqbuf() is called to adjust
1080 * the buffer nbuffers and buffer size
1083 vpfe_buffer_queue_setup(struct vb2_queue *vq, const struct v4l2_format *fmt,
1084 unsigned int *nbuffers, unsigned int *nplanes,
1085 unsigned int sizes[], void *alloc_ctxs[])
1087 struct vpfe_fh *fh = vb2_get_drv_priv(vq);
1088 struct vpfe_video_device *video = fh->video;
1089 struct vpfe_device *vpfe_dev = video->vpfe_dev;
1090 struct vpfe_pipeline *pipe = &video->pipe;
1093 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_buffer_queue_setup\n");
1094 size = video->fmt.fmt.pix.sizeimage;
1096 if (vpfe_dev->video_limit) {
1097 while (size * *nbuffers > vpfe_dev->video_limit)
1100 if (pipe->state == VPFE_PIPELINE_STREAM_CONTINUOUS) {
1101 if (*nbuffers < MIN_NUM_BUFFERS)
1102 *nbuffers = MIN_NUM_BUFFERS;
1106 alloc_ctxs[0] = video->alloc_ctx;
1107 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev,
1108 "nbuffers=%d, size=%lu\n", *nbuffers, size);
1113 * vpfe_buffer_prepare : callback function for buffer prepare
1114 * @vb: ptr to vb2_buffer
1116 * This is the callback function for buffer prepare when vb2_qbuf()
1117 * function is called. The buffer is prepared and user space virtual address
1118 * or user address is converted into physical address
1120 static int vpfe_buffer_prepare(struct vb2_buffer *vb)
1122 struct vpfe_fh *fh = vb2_get_drv_priv(vb->vb2_queue);
1123 struct vpfe_video_device *video = fh->video;
1124 struct vpfe_device *vpfe_dev = video->vpfe_dev;
1127 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_buffer_prepare\n");
1129 if (vb->state != VB2_BUF_STATE_ACTIVE &&
1130 vb->state != VB2_BUF_STATE_PREPARED)
1133 /* Initialize buffer */
1134 vb2_set_plane_payload(vb, 0, video->fmt.fmt.pix.sizeimage);
1135 if (vb2_plane_vaddr(vb, 0) &&
1136 vb2_get_plane_payload(vb, 0) > vb2_plane_size(vb, 0))
1139 addr = vb2_dma_contig_plane_dma_addr(vb, 0);
1140 /* Make sure user addresses are aligned to 32 bytes */
1141 if (!ALIGN(addr, 32))
1147 static void vpfe_buffer_queue(struct vb2_buffer *vb)
1149 /* Get the file handle object and device object */
1150 struct vpfe_fh *fh = vb2_get_drv_priv(vb->vb2_queue);
1151 struct vpfe_video_device *video = fh->video;
1152 struct vpfe_device *vpfe_dev = video->vpfe_dev;
1153 struct vpfe_pipeline *pipe = &video->pipe;
1154 struct vpfe_cap_buffer *buf = container_of(vb,
1155 struct vpfe_cap_buffer, vb);
1156 unsigned long flags;
1157 unsigned long empty;
1160 spin_lock_irqsave(&video->dma_queue_lock, flags);
1161 empty = list_empty(&video->dma_queue);
1162 /* add the buffer to the DMA queue */
1163 list_add_tail(&buf->list, &video->dma_queue);
1164 spin_unlock_irqrestore(&video->dma_queue_lock, flags);
1165 /* this case happens in case of single shot */
1166 if (empty && video->started && pipe->state ==
1167 VPFE_PIPELINE_STREAM_SINGLESHOT &&
1168 video->state == VPFE_VIDEO_BUFFER_NOT_QUEUED) {
1169 spin_lock(&video->dma_queue_lock);
1170 addr = vpfe_video_get_next_buffer(video);
1171 video->ops->queue(vpfe_dev, addr);
1173 video->state = VPFE_VIDEO_BUFFER_QUEUED;
1174 spin_unlock(&video->dma_queue_lock);
1176 /* enable h/w each time in single shot */
1177 if (vpfe_video_is_pipe_ready(pipe))
1178 vpfe_pipeline_set_stream(pipe,
1179 VPFE_PIPELINE_STREAM_SINGLESHOT);
1183 /* vpfe_start_capture() - start streaming on all the subdevs */
1184 static int vpfe_start_capture(struct vpfe_video_device *video)
1186 struct vpfe_pipeline *pipe = &video->pipe;
1190 if (vpfe_video_is_pipe_ready(pipe))
1191 ret = vpfe_pipeline_set_stream(pipe, pipe->state);
1196 static int vpfe_start_streaming(struct vb2_queue *vq, unsigned int count)
1198 struct vpfe_fh *fh = vb2_get_drv_priv(vq);
1199 struct vpfe_video_device *video = fh->video;
1200 struct vpfe_device *vpfe_dev = video->vpfe_dev;
1204 ret = mutex_lock_interruptible(&video->lock);
1208 /* Get the next frame from the buffer queue */
1209 video->cur_frm = video->next_frm =
1210 list_entry(video->dma_queue.next, struct vpfe_cap_buffer, list);
1211 /* Remove buffer from the buffer queue */
1212 list_del(&video->cur_frm->list);
1213 /* Mark state of the current frame to active */
1214 video->cur_frm->vb.state = VB2_BUF_STATE_ACTIVE;
1215 /* Initialize field_id and started member */
1216 video->field_id = 0;
1217 addr = vb2_dma_contig_plane_dma_addr(&video->cur_frm->vb, 0);
1218 video->ops->queue(vpfe_dev, addr);
1219 video->state = VPFE_VIDEO_BUFFER_QUEUED;
1221 ret = vpfe_start_capture(video);
1225 mutex_unlock(&video->lock);
1229 mutex_unlock(&video->lock);
1231 ret = vb2_streamoff(&video->buffer_queue, video->buffer_queue.type);
1235 static int vpfe_buffer_init(struct vb2_buffer *vb)
1237 struct vpfe_cap_buffer *buf = container_of(vb,
1238 struct vpfe_cap_buffer, vb);
1240 INIT_LIST_HEAD(&buf->list);
1244 /* abort streaming and wait for last buffer */
1245 static int vpfe_stop_streaming(struct vb2_queue *vq)
1247 struct vpfe_fh *fh = vb2_get_drv_priv(vq);
1248 struct vpfe_video_device *video = fh->video;
1250 if (!vb2_is_streaming(vq))
1252 /* release all active buffers */
1253 while (!list_empty(&video->dma_queue)) {
1254 video->next_frm = list_entry(video->dma_queue.next,
1255 struct vpfe_cap_buffer, list);
1256 list_del(&video->next_frm->list);
1257 vb2_buffer_done(&video->next_frm->vb, VB2_BUF_STATE_ERROR);
1262 static void vpfe_buf_cleanup(struct vb2_buffer *vb)
1264 struct vpfe_fh *fh = vb2_get_drv_priv(vb->vb2_queue);
1265 struct vpfe_video_device *video = fh->video;
1266 struct vpfe_device *vpfe_dev = video->vpfe_dev;
1267 struct vpfe_cap_buffer *buf = container_of(vb,
1268 struct vpfe_cap_buffer, vb);
1270 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_buf_cleanup\n");
1271 if (vb->state == VB2_BUF_STATE_ACTIVE)
1272 list_del_init(&buf->list);
1275 static struct vb2_ops video_qops = {
1276 .queue_setup = vpfe_buffer_queue_setup,
1277 .buf_init = vpfe_buffer_init,
1278 .buf_prepare = vpfe_buffer_prepare,
1279 .start_streaming = vpfe_start_streaming,
1280 .stop_streaming = vpfe_stop_streaming,
1281 .buf_cleanup = vpfe_buf_cleanup,
1282 .buf_queue = vpfe_buffer_queue,
1286 * vpfe_reqbufs() - supported REQBUF only once opening
1289 static int vpfe_reqbufs(struct file *file, void *priv,
1290 struct v4l2_requestbuffers *req_buf)
1292 struct vpfe_video_device *video = video_drvdata(file);
1293 struct vpfe_device *vpfe_dev = video->vpfe_dev;
1294 struct vpfe_fh *fh = file->private_data;
1295 struct vb2_queue *q;
1298 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_reqbufs\n");
1300 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != req_buf->type &&
1301 V4L2_BUF_TYPE_VIDEO_OUTPUT != req_buf->type) {
1302 v4l2_err(&vpfe_dev->v4l2_dev, "Invalid buffer type\n");
1306 ret = mutex_lock_interruptible(&video->lock);
1310 if (video->io_usrs != 0) {
1311 v4l2_err(&vpfe_dev->v4l2_dev, "Only one IO user allowed\n");
1315 video->memory = req_buf->memory;
1317 /* Initialize videobuf2 queue as per the buffer type */
1318 video->alloc_ctx = vb2_dma_contig_init_ctx(vpfe_dev->pdev);
1319 if (IS_ERR(video->alloc_ctx)) {
1320 v4l2_err(&vpfe_dev->v4l2_dev, "Failed to get the context\n");
1321 return PTR_ERR(video->alloc_ctx);
1324 q = &video->buffer_queue;
1325 q->type = req_buf->type;
1326 q->io_modes = VB2_MMAP | VB2_USERPTR;
1328 q->ops = &video_qops;
1329 q->mem_ops = &vb2_dma_contig_memops;
1330 q->buf_struct_size = sizeof(struct vpfe_cap_buffer);
1332 ret = vb2_queue_init(q);
1334 v4l2_err(&vpfe_dev->v4l2_dev, "vb2_queue_init() failed\n");
1335 vb2_dma_contig_cleanup_ctx(vpfe_dev->pdev);
1341 INIT_LIST_HEAD(&video->dma_queue);
1342 ret = vb2_reqbufs(&video->buffer_queue, req_buf);
1345 mutex_unlock(&video->lock);
1350 * vpfe_querybuf() - query buffers for exchange
1352 static int vpfe_querybuf(struct file *file, void *priv,
1353 struct v4l2_buffer *buf)
1355 struct vpfe_video_device *video = video_drvdata(file);
1356 struct vpfe_device *vpfe_dev = video->vpfe_dev;
1358 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_querybuf\n");
1360 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != buf->type &&
1361 V4L2_BUF_TYPE_VIDEO_OUTPUT != buf->type) {
1362 v4l2_err(&vpfe_dev->v4l2_dev, "Invalid buf type\n");
1366 if (video->memory != V4L2_MEMORY_MMAP) {
1367 v4l2_err(&vpfe_dev->v4l2_dev, "Invalid memory\n");
1371 /* Call vb2_querybuf to get information */
1372 return vb2_querybuf(&video->buffer_queue, buf);
1376 * vpfe_qbuf() - queue buffers for capture or processing
1378 static int vpfe_qbuf(struct file *file, void *priv,
1379 struct v4l2_buffer *p)
1381 struct vpfe_video_device *video = video_drvdata(file);
1382 struct vpfe_device *vpfe_dev = video->vpfe_dev;
1383 struct vpfe_fh *fh = file->private_data;
1385 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_qbuf\n");
1387 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != p->type &&
1388 V4L2_BUF_TYPE_VIDEO_OUTPUT != p->type) {
1389 v4l2_err(&vpfe_dev->v4l2_dev, "Invalid buf type\n");
1393 * If this file handle is not allowed to do IO,
1396 if (!fh->io_allowed) {
1397 v4l2_err(&vpfe_dev->v4l2_dev, "fh->io_allowed\n");
1401 return vb2_qbuf(&video->buffer_queue, p);
1405 * vpfe_dqbuf() - deque buffer which is done with processing
1407 static int vpfe_dqbuf(struct file *file, void *priv,
1408 struct v4l2_buffer *buf)
1410 struct vpfe_video_device *video = video_drvdata(file);
1411 struct vpfe_device *vpfe_dev = video->vpfe_dev;
1413 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_dqbuf\n");
1415 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != buf->type &&
1416 V4L2_BUF_TYPE_VIDEO_OUTPUT != buf->type) {
1417 v4l2_err(&vpfe_dev->v4l2_dev, "Invalid buf type\n");
1421 return vb2_dqbuf(&video->buffer_queue,
1422 buf, (file->f_flags & O_NONBLOCK));
1426 * vpfe_streamon() - start streaming
1427 * @file: file pointer
1428 * @priv: void pointer
1429 * @buf_type: enum v4l2_buf_type
1431 * queue buffer onto hardware for capture/processing and
1432 * start all the subdevs which are in media chain
1434 * Return 0 on success, error code otherwise
1436 static int vpfe_streamon(struct file *file, void *priv,
1437 enum v4l2_buf_type buf_type)
1439 struct vpfe_video_device *video = video_drvdata(file);
1440 struct vpfe_device *vpfe_dev = video->vpfe_dev;
1441 struct vpfe_pipeline *pipe = &video->pipe;
1442 struct vpfe_fh *fh = file->private_data;
1443 struct vpfe_ext_subdev_info *sdinfo;
1446 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_streamon\n");
1448 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != buf_type &&
1449 V4L2_BUF_TYPE_VIDEO_OUTPUT != buf_type) {
1450 v4l2_err(&vpfe_dev->v4l2_dev, "Invalid buf type\n");
1453 /* If file handle is not allowed IO, return error */
1454 if (!fh->io_allowed) {
1455 v4l2_err(&vpfe_dev->v4l2_dev, "fh->io_allowed\n");
1458 sdinfo = video->current_ext_subdev;
1459 /* If buffer queue is empty, return error */
1460 if (list_empty(&video->buffer_queue.queued_list)) {
1461 v4l2_err(&vpfe_dev->v4l2_dev, "buffer queue is empty\n");
1464 /* Validate the pipeline */
1465 if (V4L2_BUF_TYPE_VIDEO_CAPTURE == buf_type) {
1466 ret = vpfe_video_validate_pipeline(pipe);
1470 /* Call vb2_streamon to start streaming */
1471 return vb2_streamon(&video->buffer_queue, buf_type);
1475 * vpfe_streamoff() - stop streaming
1476 * @file: file pointer
1477 * @priv: void pointer
1478 * @buf_type: enum v4l2_buf_type
1480 * stop all the subdevs which are in media chain
1482 * Return 0 on success, error code otherwise
1484 static int vpfe_streamoff(struct file *file, void *priv,
1485 enum v4l2_buf_type buf_type)
1487 struct vpfe_video_device *video = video_drvdata(file);
1488 struct vpfe_device *vpfe_dev = video->vpfe_dev;
1489 struct vpfe_fh *fh = file->private_data;
1492 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_streamoff\n");
1494 if (buf_type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1495 buf_type != V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1496 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "Invalid buf type\n");
1500 /* If io is allowed for this file handle, return error */
1501 if (!fh->io_allowed) {
1502 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "fh->io_allowed\n");
1506 /* If streaming is not started, return error */
1507 if (!video->started) {
1508 v4l2_err(&vpfe_dev->v4l2_dev, "device is not started\n");
1512 ret = mutex_lock_interruptible(&video->lock);
1516 vpfe_stop_capture(video);
1517 ret = vb2_streamoff(&video->buffer_queue, buf_type);
1518 mutex_unlock(&video->lock);
1523 /* vpfe capture ioctl operations */
1524 static const struct v4l2_ioctl_ops vpfe_ioctl_ops = {
1525 .vidioc_querycap = vpfe_querycap,
1526 .vidioc_g_fmt_vid_cap = vpfe_g_fmt,
1527 .vidioc_s_fmt_vid_cap = vpfe_s_fmt,
1528 .vidioc_try_fmt_vid_cap = vpfe_try_fmt,
1529 .vidioc_enum_fmt_vid_cap = vpfe_enum_fmt,
1530 .vidioc_g_fmt_vid_out = vpfe_g_fmt,
1531 .vidioc_s_fmt_vid_out = vpfe_s_fmt,
1532 .vidioc_try_fmt_vid_out = vpfe_try_fmt,
1533 .vidioc_enum_fmt_vid_out = vpfe_enum_fmt,
1534 .vidioc_enum_input = vpfe_enum_input,
1535 .vidioc_g_input = vpfe_g_input,
1536 .vidioc_s_input = vpfe_s_input,
1537 .vidioc_querystd = vpfe_querystd,
1538 .vidioc_s_std = vpfe_s_std,
1539 .vidioc_g_std = vpfe_g_std,
1540 .vidioc_enum_dv_timings = vpfe_enum_dv_timings,
1541 .vidioc_query_dv_timings = vpfe_query_dv_timings,
1542 .vidioc_s_dv_timings = vpfe_s_dv_timings,
1543 .vidioc_g_dv_timings = vpfe_g_dv_timings,
1544 .vidioc_reqbufs = vpfe_reqbufs,
1545 .vidioc_querybuf = vpfe_querybuf,
1546 .vidioc_qbuf = vpfe_qbuf,
1547 .vidioc_dqbuf = vpfe_dqbuf,
1548 .vidioc_streamon = vpfe_streamon,
1549 .vidioc_streamoff = vpfe_streamoff,
1552 /* VPFE video init function */
1553 int vpfe_video_init(struct vpfe_video_device *video, const char *name)
1555 const char *direction;
1558 switch (video->type) {
1559 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1560 direction = "output";
1561 video->pad.flags = MEDIA_PAD_FL_SINK;
1562 video->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1565 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
1566 direction = "input";
1567 video->pad.flags = MEDIA_PAD_FL_SOURCE;
1568 video->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
1574 /* Initialize field of video device */
1575 video->video_dev.release = video_device_release;
1576 video->video_dev.fops = &vpfe_fops;
1577 video->video_dev.ioctl_ops = &vpfe_ioctl_ops;
1578 video->video_dev.minor = -1;
1579 video->video_dev.tvnorms = 0;
1580 snprintf(video->video_dev.name, sizeof(video->video_dev.name),
1581 "DAVINCI VIDEO %s %s", name, direction);
1583 /* Initialize prio member of device object */
1584 v4l2_prio_init(&video->prio);
1585 spin_lock_init(&video->irqlock);
1586 spin_lock_init(&video->dma_queue_lock);
1587 mutex_init(&video->lock);
1588 ret = media_entity_init(&video->video_dev.entity,
1593 video_set_drvdata(&video->video_dev, video);
1598 /* vpfe video device register function */
1599 int vpfe_video_register(struct vpfe_video_device *video,
1600 struct v4l2_device *vdev)
1604 video->video_dev.v4l2_dev = vdev;
1606 ret = video_register_device(&video->video_dev, VFL_TYPE_GRABBER, -1);
1608 pr_err("%s: could not register video device (%d)\n",
1613 /* vpfe video device unregister function */
1614 void vpfe_video_unregister(struct vpfe_video_device *video)
1616 if (video_is_registered(&video->video_dev)) {
1617 video_unregister_device(&video->video_dev);
1618 media_entity_cleanup(&video->video_dev.entity);