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 whether pipeline is ready for enabling */
202 int vpfe_video_is_pipe_ready(struct vpfe_pipeline
*pipe
)
206 for (i
= 0; i
< pipe
->input_num
; i
++)
207 if (!pipe
->inputs
[i
]->started
||
208 pipe
->inputs
[i
]->state
!= VPFE_VIDEO_BUFFER_QUEUED
)
210 for (i
= 0; i
< pipe
->output_num
; i
++)
211 if (!pipe
->outputs
[i
]->started
||
212 pipe
->outputs
[i
]->state
!= VPFE_VIDEO_BUFFER_QUEUED
)
218 * Validate a pipeline by checking both ends of all links for format
221 * Return 0 if all formats match, or -EPIPE if at least one link is found with
222 * different formats on its two ends.
224 static int vpfe_video_validate_pipeline(struct vpfe_pipeline
*pipe
)
226 struct v4l2_subdev_format fmt_source
;
227 struct v4l2_subdev_format fmt_sink
;
228 struct v4l2_subdev
*subdev
;
229 struct media_pad
*pad
;
233 * Should not matter if it is output[0] or 1 as
234 * the general ideas is to traverse backwards and
235 * the fact that the out video node always has the
236 * format of the connected pad.
238 subdev
= vpfe_video_remote_subdev(pipe
->outputs
[0], NULL
);
243 /* Retrieve the sink format */
244 pad
= &subdev
->entity
.pads
[0];
245 if (!(pad
->flags
& MEDIA_PAD_FL_SINK
))
248 fmt_sink
.which
= V4L2_SUBDEV_FORMAT_ACTIVE
;
249 fmt_sink
.pad
= pad
->index
;
250 ret
= v4l2_subdev_call(subdev
, pad
, get_fmt
, NULL
,
253 if (ret
< 0 && ret
!= -ENOIOCTLCMD
)
256 /* Retrieve the source format */
257 pad
= media_entity_remote_pad(pad
);
258 if (!pad
|| !is_media_entity_v4l2_subdev(pad
->entity
))
261 subdev
= media_entity_to_v4l2_subdev(pad
->entity
);
263 fmt_source
.which
= V4L2_SUBDEV_FORMAT_ACTIVE
;
264 fmt_source
.pad
= pad
->index
;
265 ret
= v4l2_subdev_call(subdev
, pad
, get_fmt
, NULL
, &fmt_source
);
266 if (ret
< 0 && ret
!= -ENOIOCTLCMD
)
269 /* Check if the two ends match */
270 if (fmt_source
.format
.code
!= fmt_sink
.format
.code
||
271 fmt_source
.format
.width
!= fmt_sink
.format
.width
||
272 fmt_source
.format
.height
!= fmt_sink
.format
.height
)
279 * vpfe_pipeline_enable() - Enable streaming on a pipeline
280 * @vpfe_dev: vpfe device
281 * @pipe: vpfe pipeline
283 * Walk the entities chain starting at the pipeline output video node and start
284 * all modules in the chain in the given mode.
286 * Return 0 if successful, or the return value of the failed video::s_stream
287 * operation otherwise.
289 static int vpfe_pipeline_enable(struct vpfe_pipeline
*pipe
)
291 struct media_entity
*entity
;
292 struct v4l2_subdev
*subdev
;
293 struct media_device
*mdev
;
296 if (pipe
->state
== VPFE_PIPELINE_STREAM_CONTINUOUS
)
297 entity
= vpfe_get_input_entity(pipe
->outputs
[0]);
299 entity
= &pipe
->inputs
[0]->video_dev
.entity
;
301 mdev
= entity
->graph_obj
.mdev
;
302 mutex_lock(&mdev
->graph_mutex
);
303 ret
= media_entity_graph_walk_init(&pipe
->graph
,
304 entity
->graph_obj
.mdev
);
307 media_entity_graph_walk_start(&pipe
->graph
, entity
);
308 while ((entity
= media_entity_graph_walk_next(&pipe
->graph
))) {
310 if (!is_media_entity_v4l2_subdev(entity
))
312 subdev
= media_entity_to_v4l2_subdev(entity
);
313 ret
= v4l2_subdev_call(subdev
, video
, s_stream
, 1);
314 if (ret
< 0 && ret
!= -ENOIOCTLCMD
)
319 media_entity_graph_walk_cleanup(&pipe
->graph
);
320 mutex_unlock(&mdev
->graph_mutex
);
325 * vpfe_pipeline_disable() - Disable streaming on a pipeline
326 * @vpfe_dev: vpfe device
327 * @pipe: VPFE pipeline
329 * Walk the entities chain starting at the pipeline output video node and stop
330 * all modules in the chain.
332 * Return 0 if all modules have been properly stopped, or -ETIMEDOUT if a module
335 static int vpfe_pipeline_disable(struct vpfe_pipeline
*pipe
)
337 struct media_entity
*entity
;
338 struct v4l2_subdev
*subdev
;
339 struct media_device
*mdev
;
342 if (pipe
->state
== VPFE_PIPELINE_STREAM_CONTINUOUS
)
343 entity
= vpfe_get_input_entity(pipe
->outputs
[0]);
345 entity
= &pipe
->inputs
[0]->video_dev
.entity
;
347 mdev
= entity
->graph_obj
.mdev
;
348 mutex_lock(&mdev
->graph_mutex
);
349 media_entity_graph_walk_start(&pipe
->graph
, entity
);
351 while ((entity
= media_entity_graph_walk_next(&pipe
->graph
))) {
353 if (!is_media_entity_v4l2_subdev(entity
))
355 subdev
= media_entity_to_v4l2_subdev(entity
);
356 ret
= v4l2_subdev_call(subdev
, video
, s_stream
, 0);
357 if (ret
< 0 && ret
!= -ENOIOCTLCMD
)
360 mutex_unlock(&mdev
->graph_mutex
);
362 media_entity_graph_walk_cleanup(&pipe
->graph
);
363 return ret
? -ETIMEDOUT
: 0;
367 * vpfe_pipeline_set_stream() - Enable/disable streaming on a pipeline
368 * @vpfe_dev: VPFE device
369 * @pipe: VPFE pipeline
370 * @state: Stream state (stopped or active)
372 * Set the pipeline to the given stream state.
374 * Return 0 if successful, or the return value of the failed video::s_stream
375 * operation otherwise.
377 static int vpfe_pipeline_set_stream(struct vpfe_pipeline
*pipe
,
378 enum vpfe_pipeline_stream_state state
)
380 if (state
== VPFE_PIPELINE_STREAM_STOPPED
)
381 return vpfe_pipeline_disable(pipe
);
383 return vpfe_pipeline_enable(pipe
);
386 static int all_videos_stopped(struct vpfe_video_device
*video
)
388 struct vpfe_pipeline
*pipe
= &video
->pipe
;
391 for (i
= 0; i
< pipe
->input_num
; i
++)
392 if (pipe
->inputs
[i
]->started
)
394 for (i
= 0; i
< pipe
->output_num
; i
++)
395 if (pipe
->outputs
[i
]->started
)
401 * vpfe_open() - open video device
402 * @file: file pointer
404 * initialize media pipeline state, allocate memory for file handle
406 * Return 0 if successful, or the return -ENODEV otherwise.
408 static int vpfe_open(struct file
*file
)
410 struct vpfe_video_device
*video
= video_drvdata(file
);
411 struct vpfe_fh
*handle
;
413 /* Allocate memory for the file handle object */
414 handle
= kzalloc(sizeof(struct vpfe_fh
), GFP_KERNEL
);
419 v4l2_fh_init(&handle
->vfh
, &video
->video_dev
);
420 v4l2_fh_add(&handle
->vfh
);
422 mutex_lock(&video
->lock
);
423 /* If decoder is not initialized. initialize it */
424 if (!video
->initialized
&& vpfe_update_pipe_state(video
)) {
425 mutex_unlock(&video
->lock
);
428 /* Increment device users counter */
430 /* Set io_allowed member to false */
431 handle
->io_allowed
= 0;
432 handle
->video
= video
;
433 file
->private_data
= &handle
->vfh
;
434 mutex_unlock(&video
->lock
);
439 /* get the next buffer available from dma queue */
441 vpfe_video_get_next_buffer(struct vpfe_video_device
*video
)
443 video
->cur_frm
= video
->next_frm
=
444 list_entry(video
->dma_queue
.next
,
445 struct vpfe_cap_buffer
, list
);
447 list_del(&video
->next_frm
->list
);
448 video
->next_frm
->vb
.vb2_buf
.state
= VB2_BUF_STATE_ACTIVE
;
449 return vb2_dma_contig_plane_dma_addr(&video
->next_frm
->vb
.vb2_buf
, 0);
452 /* schedule the next buffer which is available on dma queue */
453 void vpfe_video_schedule_next_buffer(struct vpfe_video_device
*video
)
455 struct vpfe_device
*vpfe_dev
= video
->vpfe_dev
;
458 if (list_empty(&video
->dma_queue
))
461 video
->next_frm
= list_entry(video
->dma_queue
.next
,
462 struct vpfe_cap_buffer
, list
);
464 if (video
->pipe
.state
== VPFE_PIPELINE_STREAM_SINGLESHOT
)
465 video
->cur_frm
= video
->next_frm
;
467 list_del(&video
->next_frm
->list
);
468 video
->next_frm
->vb
.vb2_buf
.state
= VB2_BUF_STATE_ACTIVE
;
469 addr
= vb2_dma_contig_plane_dma_addr(&video
->next_frm
->vb
.vb2_buf
, 0);
470 video
->ops
->queue(vpfe_dev
, addr
);
471 video
->state
= VPFE_VIDEO_BUFFER_QUEUED
;
474 /* schedule the buffer for capturing bottom field */
475 void vpfe_video_schedule_bottom_field(struct vpfe_video_device
*video
)
477 struct vpfe_device
*vpfe_dev
= video
->vpfe_dev
;
480 addr
= vb2_dma_contig_plane_dma_addr(&video
->cur_frm
->vb
.vb2_buf
, 0);
481 addr
+= video
->field_off
;
482 video
->ops
->queue(vpfe_dev
, addr
);
485 /* make buffer available for dequeue */
486 void vpfe_video_process_buffer_complete(struct vpfe_video_device
*video
)
488 struct vpfe_pipeline
*pipe
= &video
->pipe
;
490 video
->cur_frm
->vb
.vb2_buf
.timestamp
= ktime_get_ns();
491 vb2_buffer_done(&video
->cur_frm
->vb
.vb2_buf
, VB2_BUF_STATE_DONE
);
492 if (pipe
->state
== VPFE_PIPELINE_STREAM_CONTINUOUS
)
493 video
->cur_frm
= video
->next_frm
;
496 /* vpfe_stop_capture() - stop streaming */
497 static void vpfe_stop_capture(struct vpfe_video_device
*video
)
499 struct vpfe_pipeline
*pipe
= &video
->pipe
;
503 if (video
->type
== V4L2_BUF_TYPE_VIDEO_OUTPUT
)
505 if (all_videos_stopped(video
))
506 vpfe_pipeline_set_stream(pipe
,
507 VPFE_PIPELINE_STREAM_STOPPED
);
511 * vpfe_release() - release video device
512 * @file: file pointer
514 * deletes buffer queue, frees the buffers and the vpfe file handle
518 static int vpfe_release(struct file
*file
)
520 struct vpfe_video_device
*video
= video_drvdata(file
);
521 struct v4l2_fh
*vfh
= file
->private_data
;
522 struct vpfe_device
*vpfe_dev
= video
->vpfe_dev
;
523 struct vpfe_fh
*fh
= container_of(vfh
, struct vpfe_fh
, vfh
);
525 v4l2_dbg(1, debug
, &vpfe_dev
->v4l2_dev
, "vpfe_release\n");
527 /* Get the device lock */
528 mutex_lock(&video
->lock
);
529 /* if this instance is doing IO */
530 if (fh
->io_allowed
) {
531 if (video
->started
) {
532 vpfe_stop_capture(video
);
534 * mark pipe state as stopped in vpfe_release(),
535 * as app might call streamon() after streamoff()
536 * in which case driver has to start streaming.
538 video
->pipe
.state
= VPFE_PIPELINE_STREAM_STOPPED
;
539 vb2_streamoff(&video
->buffer_queue
,
540 video
->buffer_queue
.type
);
543 /* Free buffers allocated */
544 vb2_queue_release(&video
->buffer_queue
);
546 /* Decrement device users counter */
548 v4l2_fh_del(&fh
->vfh
);
549 v4l2_fh_exit(&fh
->vfh
);
550 /* If this is the last file handle */
552 video
->initialized
= 0;
553 mutex_unlock(&video
->lock
);
554 file
->private_data
= NULL
;
555 /* Free memory allocated to file handle object */
562 * vpfe_mmap() - It is used to map kernel space buffers
565 static int vpfe_mmap(struct file
*file
, struct vm_area_struct
*vma
)
567 struct vpfe_video_device
*video
= video_drvdata(file
);
568 struct vpfe_device
*vpfe_dev
= video
->vpfe_dev
;
570 v4l2_dbg(1, debug
, &vpfe_dev
->v4l2_dev
, "vpfe_mmap\n");
571 return vb2_mmap(&video
->buffer_queue
, vma
);
575 * vpfe_poll() - It is used for select/poll system call
577 static unsigned int vpfe_poll(struct file
*file
, poll_table
*wait
)
579 struct vpfe_video_device
*video
= video_drvdata(file
);
580 struct vpfe_device
*vpfe_dev
= video
->vpfe_dev
;
582 v4l2_dbg(1, debug
, &vpfe_dev
->v4l2_dev
, "vpfe_poll\n");
584 return vb2_poll(&video
->buffer_queue
, file
, wait
);
588 /* vpfe capture driver file operations */
589 static const struct v4l2_file_operations vpfe_fops
= {
590 .owner
= THIS_MODULE
,
592 .release
= vpfe_release
,
593 .unlocked_ioctl
= video_ioctl2
,
599 * vpfe_querycap() - query capabilities of video device
600 * @file: file pointer
601 * @priv: void pointer
602 * @cap: pointer to v4l2_capability structure
604 * fills v4l2 capabilities structure
608 static int vpfe_querycap(struct file
*file
, void *priv
,
609 struct v4l2_capability
*cap
)
611 struct vpfe_video_device
*video
= video_drvdata(file
);
612 struct vpfe_device
*vpfe_dev
= video
->vpfe_dev
;
614 v4l2_dbg(1, debug
, &vpfe_dev
->v4l2_dev
, "vpfe_querycap\n");
616 if (video
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE
)
617 cap
->device_caps
= V4L2_CAP_VIDEO_CAPTURE
| V4L2_CAP_STREAMING
;
619 cap
->device_caps
= V4L2_CAP_VIDEO_OUTPUT
| V4L2_CAP_STREAMING
;
620 cap
->capabilities
= V4L2_CAP_VIDEO_CAPTURE
| V4L2_CAP_VIDEO_OUTPUT
|
621 V4L2_CAP_STREAMING
| V4L2_CAP_DEVICE_CAPS
;
622 strlcpy(cap
->driver
, CAPTURE_DRV_NAME
, sizeof(cap
->driver
));
623 strlcpy(cap
->bus_info
, "VPFE", sizeof(cap
->bus_info
));
624 strlcpy(cap
->card
, vpfe_dev
->cfg
->card_name
, sizeof(cap
->card
));
630 * vpfe_g_fmt() - get the format which is active on video device
631 * @file: file pointer
632 * @priv: void pointer
633 * @fmt: pointer to v4l2_format structure
635 * fills v4l2 format structure with active format
639 static int vpfe_g_fmt(struct file
*file
, void *priv
,
640 struct v4l2_format
*fmt
)
642 struct vpfe_video_device
*video
= video_drvdata(file
);
643 struct vpfe_device
*vpfe_dev
= video
->vpfe_dev
;
645 v4l2_dbg(1, debug
, &vpfe_dev
->v4l2_dev
, "vpfe_g_fmt\n");
646 /* Fill in the information about format */
652 * vpfe_enum_fmt() - enum formats supported on media chain
653 * @file: file pointer
654 * @priv: void pointer
655 * @fmt: pointer to v4l2_fmtdesc structure
657 * fills v4l2_fmtdesc structure with output format set on adjacent subdev,
658 * only one format is enumearted as subdevs are already configured
660 * Return 0 if successful, error code otherwise
662 static int vpfe_enum_fmt(struct file
*file
, void *priv
,
663 struct v4l2_fmtdesc
*fmt
)
665 struct vpfe_video_device
*video
= video_drvdata(file
);
666 struct vpfe_device
*vpfe_dev
= video
->vpfe_dev
;
667 struct v4l2_subdev_format sd_fmt
;
668 struct v4l2_mbus_framefmt mbus
;
669 struct v4l2_subdev
*subdev
;
670 struct v4l2_format format
;
671 struct media_pad
*remote
;
674 v4l2_dbg(1, debug
, &vpfe_dev
->v4l2_dev
, "vpfe_enum_fmt\n");
677 * since already subdev pad format is set,
678 * only one pixel format is available
680 if (fmt
->index
> 0) {
681 v4l2_err(&vpfe_dev
->v4l2_dev
, "Invalid index\n");
684 /* get the remote pad */
685 remote
= media_entity_remote_pad(&video
->pad
);
687 v4l2_err(&vpfe_dev
->v4l2_dev
,
688 "invalid remote pad for video node\n");
691 /* get the remote subdev */
692 subdev
= vpfe_video_remote_subdev(video
, NULL
);
694 v4l2_err(&vpfe_dev
->v4l2_dev
,
695 "invalid remote subdev for video node\n");
698 sd_fmt
.pad
= remote
->index
;
699 sd_fmt
.which
= V4L2_SUBDEV_FORMAT_ACTIVE
;
700 /* get output format of remote subdev */
701 ret
= v4l2_subdev_call(subdev
, pad
, get_fmt
, NULL
, &sd_fmt
);
703 v4l2_err(&vpfe_dev
->v4l2_dev
,
704 "invalid remote subdev for video node\n");
707 /* convert to pix format */
708 mbus
.code
= sd_fmt
.format
.code
;
709 mbus_to_pix(&mbus
, &format
.fmt
.pix
);
710 /* copy the result */
711 fmt
->pixelformat
= format
.fmt
.pix
.pixelformat
;
717 * vpfe_s_fmt() - set the format on video device
718 * @file: file pointer
719 * @priv: void pointer
720 * @fmt: pointer to v4l2_format structure
722 * validate and set the format on video device
724 * Return 0 on success, error code otherwise
726 static int vpfe_s_fmt(struct file
*file
, void *priv
,
727 struct v4l2_format
*fmt
)
729 struct vpfe_video_device
*video
= video_drvdata(file
);
730 struct vpfe_device
*vpfe_dev
= video
->vpfe_dev
;
731 struct v4l2_format format
;
734 v4l2_dbg(1, debug
, &vpfe_dev
->v4l2_dev
, "vpfe_s_fmt\n");
735 /* If streaming is started, return error */
736 if (video
->started
) {
737 v4l2_err(&vpfe_dev
->v4l2_dev
, "Streaming is started\n");
740 /* get adjacent subdev's output pad format */
741 ret
= __vpfe_video_get_format(video
, &format
);
750 * vpfe_try_fmt() - try the format on video device
751 * @file: file pointer
752 * @priv: void pointer
753 * @fmt: pointer to v4l2_format structure
755 * validate the format, update with correct format
756 * based on output format set on adjacent subdev
758 * Return 0 on success, error code otherwise
760 static int vpfe_try_fmt(struct file
*file
, void *priv
,
761 struct v4l2_format
*fmt
)
763 struct vpfe_video_device
*video
= video_drvdata(file
);
764 struct vpfe_device
*vpfe_dev
= video
->vpfe_dev
;
765 struct v4l2_format format
;
768 v4l2_dbg(1, debug
, &vpfe_dev
->v4l2_dev
, "vpfe_try_fmt\n");
769 /* get adjacent subdev's output pad format */
770 ret
= __vpfe_video_get_format(video
, &format
);
779 * vpfe_enum_input() - enum inputs supported on media chain
780 * @file: file pointer
781 * @priv: void pointer
782 * @fmt: pointer to v4l2_fmtdesc structure
784 * fills v4l2_input structure with input available on media chain,
785 * only one input is enumearted as media chain is setup by this time
787 * Return 0 if successful, -EINVAL is media chain is invalid
789 static int vpfe_enum_input(struct file
*file
, void *priv
,
790 struct v4l2_input
*inp
)
792 struct vpfe_video_device
*video
= video_drvdata(file
);
793 struct vpfe_ext_subdev_info
*sdinfo
= video
->current_ext_subdev
;
794 struct vpfe_device
*vpfe_dev
= video
->vpfe_dev
;
796 v4l2_dbg(1, debug
, &vpfe_dev
->v4l2_dev
, "vpfe_enum_input\n");
797 /* enumerate from the subdev user has chosen through mc */
798 if (inp
->index
< sdinfo
->num_inputs
) {
799 memcpy(inp
, &sdinfo
->inputs
[inp
->index
],
800 sizeof(struct v4l2_input
));
807 * vpfe_g_input() - get index of the input which is active
808 * @file: file pointer
809 * @priv: void pointer
810 * @index: pointer to unsigned int
812 * set index with input index which is active
814 static int vpfe_g_input(struct file
*file
, void *priv
, unsigned int *index
)
816 struct vpfe_video_device
*video
= video_drvdata(file
);
817 struct vpfe_device
*vpfe_dev
= video
->vpfe_dev
;
819 v4l2_dbg(1, debug
, &vpfe_dev
->v4l2_dev
, "vpfe_g_input\n");
821 *index
= video
->current_input
;
826 * vpfe_s_input() - set input which is pointed by input index
827 * @file: file pointer
828 * @priv: void pointer
829 * @index: pointer to unsigned int
831 * set input on external subdev
833 * Return 0 on success, error code otherwise
835 static int vpfe_s_input(struct file
*file
, void *priv
, unsigned int index
)
837 struct vpfe_video_device
*video
= video_drvdata(file
);
838 struct vpfe_device
*vpfe_dev
= video
->vpfe_dev
;
839 struct vpfe_ext_subdev_info
*sdinfo
;
840 struct vpfe_route
*route
;
841 struct v4l2_input
*inps
;
847 v4l2_dbg(1, debug
, &vpfe_dev
->v4l2_dev
, "vpfe_s_input\n");
849 ret
= mutex_lock_interruptible(&video
->lock
);
853 * If streaming is started return device busy
856 if (video
->started
) {
857 v4l2_err(&vpfe_dev
->v4l2_dev
, "Streaming is on\n");
862 sdinfo
= video
->current_ext_subdev
;
863 if (!sdinfo
->registered
) {
867 if (vpfe_dev
->cfg
->setup_input
&&
868 vpfe_dev
->cfg
->setup_input(sdinfo
->grp_id
) < 0) {
870 v4l2_dbg(1, debug
, &vpfe_dev
->v4l2_dev
,
871 "couldn't setup input for %s\n",
872 sdinfo
->module_name
);
875 route
= &sdinfo
->routes
[index
];
876 if (route
&& sdinfo
->can_route
) {
877 input
= route
->input
;
878 output
= route
->output
;
879 ret
= v4l2_device_call_until_err(&vpfe_dev
->v4l2_dev
,
880 sdinfo
->grp_id
, video
,
881 s_routing
, input
, output
, 0);
883 v4l2_dbg(1, debug
, &vpfe_dev
->v4l2_dev
,
884 "s_input:error in setting input in decoder\n");
889 /* set standards set by subdev in video device */
890 for (i
= 0; i
< sdinfo
->num_inputs
; i
++) {
891 inps
= &sdinfo
->inputs
[i
];
892 video
->video_dev
.tvnorms
|= inps
->std
;
894 video
->current_input
= index
;
896 mutex_unlock(&video
->lock
);
901 * vpfe_querystd() - query std which is being input on external subdev
902 * @file: file pointer
903 * @priv: void pointer
904 * @std_id: pointer to v4l2_std_id structure
906 * call external subdev through v4l2_device_call_until_err to
907 * get the std that is being active.
909 * Return 0 on success, error code otherwise
911 static int vpfe_querystd(struct file
*file
, void *priv
, v4l2_std_id
*std_id
)
913 struct vpfe_video_device
*video
= video_drvdata(file
);
914 struct vpfe_device
*vpfe_dev
= video
->vpfe_dev
;
915 struct vpfe_ext_subdev_info
*sdinfo
;
918 v4l2_dbg(1, debug
, &vpfe_dev
->v4l2_dev
, "vpfe_querystd\n");
920 ret
= mutex_lock_interruptible(&video
->lock
);
921 sdinfo
= video
->current_ext_subdev
;
924 /* Call querystd function of decoder device */
925 ret
= v4l2_device_call_until_err(&vpfe_dev
->v4l2_dev
, sdinfo
->grp_id
,
926 video
, querystd
, std_id
);
927 mutex_unlock(&video
->lock
);
932 * vpfe_s_std() - set std on external subdev
933 * @file: file pointer
934 * @priv: void pointer
935 * @std_id: pointer to v4l2_std_id structure
937 * set std pointed by std_id on external subdev by calling it using
938 * v4l2_device_call_until_err
940 * Return 0 on success, error code otherwise
942 static int vpfe_s_std(struct file
*file
, void *priv
, v4l2_std_id std_id
)
944 struct vpfe_video_device
*video
= video_drvdata(file
);
945 struct vpfe_device
*vpfe_dev
= video
->vpfe_dev
;
946 struct vpfe_ext_subdev_info
*sdinfo
;
949 v4l2_dbg(1, debug
, &vpfe_dev
->v4l2_dev
, "vpfe_s_std\n");
951 /* Call decoder driver function to set the standard */
952 ret
= mutex_lock_interruptible(&video
->lock
);
955 sdinfo
= video
->current_ext_subdev
;
956 /* If streaming is started, return device busy error */
957 if (video
->started
) {
958 v4l2_err(&vpfe_dev
->v4l2_dev
, "streaming is started\n");
962 ret
= v4l2_device_call_until_err(&vpfe_dev
->v4l2_dev
, sdinfo
->grp_id
,
963 video
, s_std
, std_id
);
965 v4l2_err(&vpfe_dev
->v4l2_dev
, "Failed to set standard\n");
966 video
->stdid
= V4L2_STD_UNKNOWN
;
969 video
->stdid
= std_id
;
971 mutex_unlock(&video
->lock
);
975 static int vpfe_g_std(struct file
*file
, void *priv
, v4l2_std_id
*tvnorm
)
977 struct vpfe_video_device
*video
= video_drvdata(file
);
978 struct vpfe_device
*vpfe_dev
= video
->vpfe_dev
;
980 v4l2_dbg(1, debug
, &vpfe_dev
->v4l2_dev
, "vpfe_g_std\n");
981 *tvnorm
= video
->stdid
;
986 * vpfe_enum_dv_timings() - enumerate dv_timings which are supported by
988 * @file: file pointer
989 * @priv: void pointer
990 * @timings: pointer to v4l2_enum_dv_timings structure
992 * enum dv_timings's which are supported by external subdev through
995 * Return 0 on success, error code otherwise
998 vpfe_enum_dv_timings(struct file
*file
, void *fh
,
999 struct v4l2_enum_dv_timings
*timings
)
1001 struct vpfe_video_device
*video
= video_drvdata(file
);
1002 struct vpfe_device
*vpfe_dev
= video
->vpfe_dev
;
1003 struct v4l2_subdev
*subdev
= video
->current_ext_subdev
->subdev
;
1007 v4l2_dbg(1, debug
, &vpfe_dev
->v4l2_dev
, "vpfe_enum_dv_timings\n");
1008 return v4l2_subdev_call(subdev
, pad
, enum_dv_timings
, timings
);
1012 * vpfe_query_dv_timings() - query the dv_timings which is being input
1013 * to external subdev
1014 * @file: file pointer
1015 * @priv: void pointer
1016 * @timings: pointer to v4l2_dv_timings structure
1018 * get dv_timings which is being input on external subdev through
1021 * Return 0 on success, error code otherwise
1024 vpfe_query_dv_timings(struct file
*file
, void *fh
,
1025 struct v4l2_dv_timings
*timings
)
1027 struct vpfe_video_device
*video
= video_drvdata(file
);
1028 struct vpfe_device
*vpfe_dev
= video
->vpfe_dev
;
1029 struct v4l2_subdev
*subdev
= video
->current_ext_subdev
->subdev
;
1031 v4l2_dbg(1, debug
, &vpfe_dev
->v4l2_dev
, "vpfe_query_dv_timings\n");
1032 return v4l2_subdev_call(subdev
, video
, query_dv_timings
, timings
);
1036 * vpfe_s_dv_timings() - set dv_timings on external subdev
1037 * @file: file pointer
1038 * @priv: void pointer
1039 * @timings: pointer to v4l2_dv_timings structure
1041 * set dv_timings pointed by timings on external subdev through
1042 * v4l2_device_call_until_err, this configures amplifier also
1044 * Return 0 on success, error code otherwise
1047 vpfe_s_dv_timings(struct file
*file
, void *fh
,
1048 struct v4l2_dv_timings
*timings
)
1050 struct vpfe_video_device
*video
= video_drvdata(file
);
1051 struct vpfe_device
*vpfe_dev
= video
->vpfe_dev
;
1053 v4l2_dbg(1, debug
, &vpfe_dev
->v4l2_dev
, "vpfe_s_dv_timings\n");
1055 video
->stdid
= V4L2_STD_UNKNOWN
;
1056 return v4l2_device_call_until_err(&vpfe_dev
->v4l2_dev
,
1057 video
->current_ext_subdev
->grp_id
,
1058 video
, s_dv_timings
, timings
);
1062 * vpfe_g_dv_timings() - get dv_timings which is set on external subdev
1063 * @file: file pointer
1064 * @priv: void pointer
1065 * @timings: pointer to v4l2_dv_timings structure
1067 * get dv_timings which is set on external subdev through
1070 * Return 0 on success, error code otherwise
1073 vpfe_g_dv_timings(struct file
*file
, void *fh
,
1074 struct v4l2_dv_timings
*timings
)
1076 struct vpfe_video_device
*video
= video_drvdata(file
);
1077 struct vpfe_device
*vpfe_dev
= video
->vpfe_dev
;
1078 struct v4l2_subdev
*subdev
= video
->current_ext_subdev
->subdev
;
1080 v4l2_dbg(1, debug
, &vpfe_dev
->v4l2_dev
, "vpfe_g_dv_timings\n");
1081 return v4l2_subdev_call(subdev
, video
, g_dv_timings
, timings
);
1085 * Videobuf operations
1088 * vpfe_buffer_queue_setup : Callback function for buffer setup.
1089 * @vq: vb2_queue ptr
1091 * @nbuffers: ptr to number of buffers requested by application
1092 * @nplanes:: contains number of distinct video planes needed to hold a frame
1093 * @sizes[]: contains the size (in bytes) of each plane.
1094 * @alloc_devs: ptr to allocation context
1096 * This callback function is called when reqbuf() is called to adjust
1097 * the buffer nbuffers and buffer size
1100 vpfe_buffer_queue_setup(struct vb2_queue
*vq
,
1101 unsigned int *nbuffers
, unsigned int *nplanes
,
1102 unsigned int sizes
[], struct device
*alloc_devs
[])
1104 struct vpfe_fh
*fh
= vb2_get_drv_priv(vq
);
1105 struct vpfe_video_device
*video
= fh
->video
;
1106 struct vpfe_device
*vpfe_dev
= video
->vpfe_dev
;
1109 v4l2_dbg(1, debug
, &vpfe_dev
->v4l2_dev
, "vpfe_buffer_queue_setup\n");
1110 size
= video
->fmt
.fmt
.pix
.sizeimage
;
1112 if (vq
->num_buffers
+ *nbuffers
< 3)
1113 *nbuffers
= 3 - vq
->num_buffers
;
1117 v4l2_dbg(1, debug
, &vpfe_dev
->v4l2_dev
,
1118 "nbuffers=%d, size=%lu\n", *nbuffers
, size
);
1123 * vpfe_buffer_prepare : callback function for buffer prepare
1124 * @vb: ptr to vb2_buffer
1126 * This is the callback function for buffer prepare when vb2_qbuf()
1127 * function is called. The buffer is prepared and user space virtual address
1128 * or user address is converted into physical address
1130 static int vpfe_buffer_prepare(struct vb2_buffer
*vb
)
1132 struct vpfe_fh
*fh
= vb2_get_drv_priv(vb
->vb2_queue
);
1133 struct vpfe_video_device
*video
= fh
->video
;
1134 struct vpfe_device
*vpfe_dev
= video
->vpfe_dev
;
1137 v4l2_dbg(1, debug
, &vpfe_dev
->v4l2_dev
, "vpfe_buffer_prepare\n");
1139 if (vb
->state
!= VB2_BUF_STATE_ACTIVE
&&
1140 vb
->state
!= VB2_BUF_STATE_PREPARED
)
1143 /* Initialize buffer */
1144 vb2_set_plane_payload(vb
, 0, video
->fmt
.fmt
.pix
.sizeimage
);
1145 if (vb2_plane_vaddr(vb
, 0) &&
1146 vb2_get_plane_payload(vb
, 0) > vb2_plane_size(vb
, 0))
1149 addr
= vb2_dma_contig_plane_dma_addr(vb
, 0);
1150 /* Make sure user addresses are aligned to 32 bytes */
1151 if (!ALIGN(addr
, 32))
1157 static void vpfe_buffer_queue(struct vb2_buffer
*vb
)
1159 struct vb2_v4l2_buffer
*vbuf
= to_vb2_v4l2_buffer(vb
);
1160 /* Get the file handle object and device object */
1161 struct vpfe_fh
*fh
= vb2_get_drv_priv(vb
->vb2_queue
);
1162 struct vpfe_video_device
*video
= fh
->video
;
1163 struct vpfe_device
*vpfe_dev
= video
->vpfe_dev
;
1164 struct vpfe_pipeline
*pipe
= &video
->pipe
;
1165 struct vpfe_cap_buffer
*buf
= container_of(vbuf
,
1166 struct vpfe_cap_buffer
, vb
);
1167 unsigned long flags
;
1168 unsigned long empty
;
1171 spin_lock_irqsave(&video
->dma_queue_lock
, flags
);
1172 empty
= list_empty(&video
->dma_queue
);
1173 /* add the buffer to the DMA queue */
1174 list_add_tail(&buf
->list
, &video
->dma_queue
);
1175 spin_unlock_irqrestore(&video
->dma_queue_lock
, flags
);
1176 /* this case happens in case of single shot */
1177 if (empty
&& video
->started
&& pipe
->state
==
1178 VPFE_PIPELINE_STREAM_SINGLESHOT
&&
1179 video
->state
== VPFE_VIDEO_BUFFER_NOT_QUEUED
) {
1180 spin_lock(&video
->dma_queue_lock
);
1181 addr
= vpfe_video_get_next_buffer(video
);
1182 video
->ops
->queue(vpfe_dev
, addr
);
1184 video
->state
= VPFE_VIDEO_BUFFER_QUEUED
;
1185 spin_unlock(&video
->dma_queue_lock
);
1187 /* enable h/w each time in single shot */
1188 if (vpfe_video_is_pipe_ready(pipe
))
1189 vpfe_pipeline_set_stream(pipe
,
1190 VPFE_PIPELINE_STREAM_SINGLESHOT
);
1194 /* vpfe_start_capture() - start streaming on all the subdevs */
1195 static int vpfe_start_capture(struct vpfe_video_device
*video
)
1197 struct vpfe_pipeline
*pipe
= &video
->pipe
;
1201 if (vpfe_video_is_pipe_ready(pipe
))
1202 ret
= vpfe_pipeline_set_stream(pipe
, pipe
->state
);
1207 static int vpfe_start_streaming(struct vb2_queue
*vq
, unsigned int count
)
1209 struct vpfe_fh
*fh
= vb2_get_drv_priv(vq
);
1210 struct vpfe_video_device
*video
= fh
->video
;
1211 struct vpfe_device
*vpfe_dev
= video
->vpfe_dev
;
1215 ret
= mutex_lock_interruptible(&video
->lock
);
1219 /* Get the next frame from the buffer queue */
1220 video
->cur_frm
= video
->next_frm
=
1221 list_entry(video
->dma_queue
.next
, struct vpfe_cap_buffer
, list
);
1222 /* Remove buffer from the buffer queue */
1223 list_del(&video
->cur_frm
->list
);
1224 /* Mark state of the current frame to active */
1225 video
->cur_frm
->vb
.vb2_buf
.state
= VB2_BUF_STATE_ACTIVE
;
1226 /* Initialize field_id and started member */
1227 video
->field_id
= 0;
1228 addr
= vb2_dma_contig_plane_dma_addr(&video
->cur_frm
->vb
.vb2_buf
, 0);
1229 video
->ops
->queue(vpfe_dev
, addr
);
1230 video
->state
= VPFE_VIDEO_BUFFER_QUEUED
;
1232 ret
= vpfe_start_capture(video
);
1234 struct vpfe_cap_buffer
*buf
, *tmp
;
1236 vb2_buffer_done(&video
->cur_frm
->vb
.vb2_buf
,
1237 VB2_BUF_STATE_QUEUED
);
1238 list_for_each_entry_safe(buf
, tmp
, &video
->dma_queue
, list
) {
1239 list_del(&buf
->list
);
1240 vb2_buffer_done(&buf
->vb
.vb2_buf
,
1241 VB2_BUF_STATE_QUEUED
);
1246 mutex_unlock(&video
->lock
);
1250 mutex_unlock(&video
->lock
);
1252 ret
= vb2_streamoff(&video
->buffer_queue
, video
->buffer_queue
.type
);
1256 static int vpfe_buffer_init(struct vb2_buffer
*vb
)
1258 struct vb2_v4l2_buffer
*vbuf
= to_vb2_v4l2_buffer(vb
);
1259 struct vpfe_cap_buffer
*buf
= container_of(vbuf
,
1260 struct vpfe_cap_buffer
, vb
);
1262 INIT_LIST_HEAD(&buf
->list
);
1266 /* abort streaming and wait for last buffer */
1267 static void vpfe_stop_streaming(struct vb2_queue
*vq
)
1269 struct vpfe_fh
*fh
= vb2_get_drv_priv(vq
);
1270 struct vpfe_video_device
*video
= fh
->video
;
1272 /* release all active buffers */
1273 if (video
->cur_frm
== video
->next_frm
) {
1274 vb2_buffer_done(&video
->cur_frm
->vb
.vb2_buf
,
1275 VB2_BUF_STATE_ERROR
);
1277 if (video
->cur_frm
!= NULL
)
1278 vb2_buffer_done(&video
->cur_frm
->vb
.vb2_buf
,
1279 VB2_BUF_STATE_ERROR
);
1280 if (video
->next_frm
!= NULL
)
1281 vb2_buffer_done(&video
->next_frm
->vb
.vb2_buf
,
1282 VB2_BUF_STATE_ERROR
);
1285 while (!list_empty(&video
->dma_queue
)) {
1286 video
->next_frm
= list_entry(video
->dma_queue
.next
,
1287 struct vpfe_cap_buffer
, list
);
1288 list_del(&video
->next_frm
->list
);
1289 vb2_buffer_done(&video
->next_frm
->vb
.vb2_buf
,
1290 VB2_BUF_STATE_ERROR
);
1294 static void vpfe_buf_cleanup(struct vb2_buffer
*vb
)
1296 struct vb2_v4l2_buffer
*vbuf
= to_vb2_v4l2_buffer(vb
);
1297 struct vpfe_fh
*fh
= vb2_get_drv_priv(vb
->vb2_queue
);
1298 struct vpfe_video_device
*video
= fh
->video
;
1299 struct vpfe_device
*vpfe_dev
= video
->vpfe_dev
;
1300 struct vpfe_cap_buffer
*buf
= container_of(vbuf
,
1301 struct vpfe_cap_buffer
, vb
);
1303 v4l2_dbg(1, debug
, &vpfe_dev
->v4l2_dev
, "vpfe_buf_cleanup\n");
1304 if (vb
->state
== VB2_BUF_STATE_ACTIVE
)
1305 list_del_init(&buf
->list
);
1308 static struct vb2_ops video_qops
= {
1309 .queue_setup
= vpfe_buffer_queue_setup
,
1310 .buf_init
= vpfe_buffer_init
,
1311 .buf_prepare
= vpfe_buffer_prepare
,
1312 .start_streaming
= vpfe_start_streaming
,
1313 .stop_streaming
= vpfe_stop_streaming
,
1314 .buf_cleanup
= vpfe_buf_cleanup
,
1315 .buf_queue
= vpfe_buffer_queue
,
1319 * vpfe_reqbufs() - supported REQBUF only once opening
1322 static int vpfe_reqbufs(struct file
*file
, void *priv
,
1323 struct v4l2_requestbuffers
*req_buf
)
1325 struct vpfe_video_device
*video
= video_drvdata(file
);
1326 struct vpfe_device
*vpfe_dev
= video
->vpfe_dev
;
1327 struct vpfe_fh
*fh
= file
->private_data
;
1328 struct vb2_queue
*q
;
1331 v4l2_dbg(1, debug
, &vpfe_dev
->v4l2_dev
, "vpfe_reqbufs\n");
1333 if (req_buf
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
&&
1334 req_buf
->type
!= V4L2_BUF_TYPE_VIDEO_OUTPUT
){
1335 v4l2_err(&vpfe_dev
->v4l2_dev
, "Invalid buffer type\n");
1339 ret
= mutex_lock_interruptible(&video
->lock
);
1343 if (video
->io_usrs
!= 0) {
1344 v4l2_err(&vpfe_dev
->v4l2_dev
, "Only one IO user allowed\n");
1348 video
->memory
= req_buf
->memory
;
1350 /* Initialize videobuf2 queue as per the buffer type */
1351 q
= &video
->buffer_queue
;
1352 q
->type
= req_buf
->type
;
1353 q
->io_modes
= VB2_MMAP
| VB2_USERPTR
;
1355 q
->min_buffers_needed
= 1;
1356 q
->ops
= &video_qops
;
1357 q
->mem_ops
= &vb2_dma_contig_memops
;
1358 q
->buf_struct_size
= sizeof(struct vpfe_cap_buffer
);
1359 q
->timestamp_flags
= V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC
;
1360 q
->dev
= vpfe_dev
->pdev
;
1362 ret
= vb2_queue_init(q
);
1364 v4l2_err(&vpfe_dev
->v4l2_dev
, "vb2_queue_init() failed\n");
1370 INIT_LIST_HEAD(&video
->dma_queue
);
1371 ret
= vb2_reqbufs(&video
->buffer_queue
, req_buf
);
1374 mutex_unlock(&video
->lock
);
1379 * vpfe_querybuf() - query buffers for exchange
1381 static int vpfe_querybuf(struct file
*file
, void *priv
,
1382 struct v4l2_buffer
*buf
)
1384 struct vpfe_video_device
*video
= video_drvdata(file
);
1385 struct vpfe_device
*vpfe_dev
= video
->vpfe_dev
;
1387 v4l2_dbg(1, debug
, &vpfe_dev
->v4l2_dev
, "vpfe_querybuf\n");
1389 if (buf
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
&&
1390 buf
->type
!= V4L2_BUF_TYPE_VIDEO_OUTPUT
) {
1391 v4l2_err(&vpfe_dev
->v4l2_dev
, "Invalid buf type\n");
1395 if (video
->memory
!= V4L2_MEMORY_MMAP
) {
1396 v4l2_err(&vpfe_dev
->v4l2_dev
, "Invalid memory\n");
1400 /* Call vb2_querybuf to get information */
1401 return vb2_querybuf(&video
->buffer_queue
, buf
);
1405 * vpfe_qbuf() - queue buffers for capture or processing
1407 static int vpfe_qbuf(struct file
*file
, void *priv
,
1408 struct v4l2_buffer
*p
)
1410 struct vpfe_video_device
*video
= video_drvdata(file
);
1411 struct vpfe_device
*vpfe_dev
= video
->vpfe_dev
;
1412 struct vpfe_fh
*fh
= file
->private_data
;
1414 v4l2_dbg(1, debug
, &vpfe_dev
->v4l2_dev
, "vpfe_qbuf\n");
1416 if (p
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
&&
1417 p
->type
!= V4L2_BUF_TYPE_VIDEO_OUTPUT
) {
1418 v4l2_err(&vpfe_dev
->v4l2_dev
, "Invalid buf type\n");
1422 * If this file handle is not allowed to do IO,
1425 if (!fh
->io_allowed
) {
1426 v4l2_err(&vpfe_dev
->v4l2_dev
, "fh->io_allowed\n");
1430 return vb2_qbuf(&video
->buffer_queue
, p
);
1434 * vpfe_dqbuf() - deque buffer which is done with processing
1436 static int vpfe_dqbuf(struct file
*file
, void *priv
,
1437 struct v4l2_buffer
*buf
)
1439 struct vpfe_video_device
*video
= video_drvdata(file
);
1440 struct vpfe_device
*vpfe_dev
= video
->vpfe_dev
;
1442 v4l2_dbg(1, debug
, &vpfe_dev
->v4l2_dev
, "vpfe_dqbuf\n");
1444 if (buf
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
&&
1445 buf
->type
!= V4L2_BUF_TYPE_VIDEO_OUTPUT
) {
1446 v4l2_err(&vpfe_dev
->v4l2_dev
, "Invalid buf type\n");
1450 return vb2_dqbuf(&video
->buffer_queue
,
1451 buf
, (file
->f_flags
& O_NONBLOCK
));
1455 * vpfe_streamon() - start streaming
1456 * @file: file pointer
1457 * @priv: void pointer
1458 * @buf_type: enum v4l2_buf_type
1460 * queue buffer onto hardware for capture/processing and
1461 * start all the subdevs which are in media chain
1463 * Return 0 on success, error code otherwise
1465 static int vpfe_streamon(struct file
*file
, void *priv
,
1466 enum v4l2_buf_type buf_type
)
1468 struct vpfe_video_device
*video
= video_drvdata(file
);
1469 struct vpfe_device
*vpfe_dev
= video
->vpfe_dev
;
1470 struct vpfe_pipeline
*pipe
= &video
->pipe
;
1471 struct vpfe_fh
*fh
= file
->private_data
;
1472 struct vpfe_ext_subdev_info
*sdinfo
;
1475 v4l2_dbg(1, debug
, &vpfe_dev
->v4l2_dev
, "vpfe_streamon\n");
1477 if (buf_type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
&&
1478 buf_type
!= V4L2_BUF_TYPE_VIDEO_OUTPUT
) {
1479 v4l2_err(&vpfe_dev
->v4l2_dev
, "Invalid buf type\n");
1482 /* If file handle is not allowed IO, return error */
1483 if (!fh
->io_allowed
) {
1484 v4l2_err(&vpfe_dev
->v4l2_dev
, "fh->io_allowed\n");
1487 sdinfo
= video
->current_ext_subdev
;
1488 /* If buffer queue is empty, return error */
1489 if (list_empty(&video
->buffer_queue
.queued_list
)) {
1490 v4l2_err(&vpfe_dev
->v4l2_dev
, "buffer queue is empty\n");
1493 /* Validate the pipeline */
1494 if (buf_type
== V4L2_BUF_TYPE_VIDEO_CAPTURE
) {
1495 ret
= vpfe_video_validate_pipeline(pipe
);
1499 /* Call vb2_streamon to start streaming */
1500 return vb2_streamon(&video
->buffer_queue
, buf_type
);
1504 * vpfe_streamoff() - stop streaming
1505 * @file: file pointer
1506 * @priv: void pointer
1507 * @buf_type: enum v4l2_buf_type
1509 * stop all the subdevs which are in media chain
1511 * Return 0 on success, error code otherwise
1513 static int vpfe_streamoff(struct file
*file
, void *priv
,
1514 enum v4l2_buf_type buf_type
)
1516 struct vpfe_video_device
*video
= video_drvdata(file
);
1517 struct vpfe_device
*vpfe_dev
= video
->vpfe_dev
;
1518 struct vpfe_fh
*fh
= file
->private_data
;
1521 v4l2_dbg(1, debug
, &vpfe_dev
->v4l2_dev
, "vpfe_streamoff\n");
1523 if (buf_type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
&&
1524 buf_type
!= V4L2_BUF_TYPE_VIDEO_OUTPUT
) {
1525 v4l2_dbg(1, debug
, &vpfe_dev
->v4l2_dev
, "Invalid buf type\n");
1529 /* If io is allowed for this file handle, return error */
1530 if (!fh
->io_allowed
) {
1531 v4l2_dbg(1, debug
, &vpfe_dev
->v4l2_dev
, "fh->io_allowed\n");
1535 /* If streaming is not started, return error */
1536 if (!video
->started
) {
1537 v4l2_err(&vpfe_dev
->v4l2_dev
, "device is not started\n");
1541 ret
= mutex_lock_interruptible(&video
->lock
);
1545 vpfe_stop_capture(video
);
1546 ret
= vb2_streamoff(&video
->buffer_queue
, buf_type
);
1547 mutex_unlock(&video
->lock
);
1552 /* vpfe capture ioctl operations */
1553 static const struct v4l2_ioctl_ops vpfe_ioctl_ops
= {
1554 .vidioc_querycap
= vpfe_querycap
,
1555 .vidioc_g_fmt_vid_cap
= vpfe_g_fmt
,
1556 .vidioc_s_fmt_vid_cap
= vpfe_s_fmt
,
1557 .vidioc_try_fmt_vid_cap
= vpfe_try_fmt
,
1558 .vidioc_enum_fmt_vid_cap
= vpfe_enum_fmt
,
1559 .vidioc_g_fmt_vid_out
= vpfe_g_fmt
,
1560 .vidioc_s_fmt_vid_out
= vpfe_s_fmt
,
1561 .vidioc_try_fmt_vid_out
= vpfe_try_fmt
,
1562 .vidioc_enum_fmt_vid_out
= vpfe_enum_fmt
,
1563 .vidioc_enum_input
= vpfe_enum_input
,
1564 .vidioc_g_input
= vpfe_g_input
,
1565 .vidioc_s_input
= vpfe_s_input
,
1566 .vidioc_querystd
= vpfe_querystd
,
1567 .vidioc_s_std
= vpfe_s_std
,
1568 .vidioc_g_std
= vpfe_g_std
,
1569 .vidioc_enum_dv_timings
= vpfe_enum_dv_timings
,
1570 .vidioc_query_dv_timings
= vpfe_query_dv_timings
,
1571 .vidioc_s_dv_timings
= vpfe_s_dv_timings
,
1572 .vidioc_g_dv_timings
= vpfe_g_dv_timings
,
1573 .vidioc_reqbufs
= vpfe_reqbufs
,
1574 .vidioc_querybuf
= vpfe_querybuf
,
1575 .vidioc_qbuf
= vpfe_qbuf
,
1576 .vidioc_dqbuf
= vpfe_dqbuf
,
1577 .vidioc_streamon
= vpfe_streamon
,
1578 .vidioc_streamoff
= vpfe_streamoff
,
1581 /* VPFE video init function */
1582 int vpfe_video_init(struct vpfe_video_device
*video
, const char *name
)
1584 const char *direction
;
1587 switch (video
->type
) {
1588 case V4L2_BUF_TYPE_VIDEO_CAPTURE
:
1589 direction
= "output";
1590 video
->pad
.flags
= MEDIA_PAD_FL_SINK
;
1591 video
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1594 case V4L2_BUF_TYPE_VIDEO_OUTPUT
:
1595 direction
= "input";
1596 video
->pad
.flags
= MEDIA_PAD_FL_SOURCE
;
1597 video
->type
= V4L2_BUF_TYPE_VIDEO_OUTPUT
;
1603 /* Initialize field of video device */
1604 video
->video_dev
.release
= video_device_release
;
1605 video
->video_dev
.fops
= &vpfe_fops
;
1606 video
->video_dev
.ioctl_ops
= &vpfe_ioctl_ops
;
1607 video
->video_dev
.minor
= -1;
1608 video
->video_dev
.tvnorms
= 0;
1609 snprintf(video
->video_dev
.name
, sizeof(video
->video_dev
.name
),
1610 "DAVINCI VIDEO %s %s", name
, direction
);
1612 spin_lock_init(&video
->irqlock
);
1613 spin_lock_init(&video
->dma_queue_lock
);
1614 mutex_init(&video
->lock
);
1615 ret
= media_entity_pads_init(&video
->video_dev
.entity
,
1620 video_set_drvdata(&video
->video_dev
, video
);
1625 /* vpfe video device register function */
1626 int vpfe_video_register(struct vpfe_video_device
*video
,
1627 struct v4l2_device
*vdev
)
1631 video
->video_dev
.v4l2_dev
= vdev
;
1633 ret
= video_register_device(&video
->video_dev
, VFL_TYPE_GRABBER
, -1);
1635 pr_err("%s: could not register video device (%d)\n",
1640 /* vpfe video device unregister function */
1641 void vpfe_video_unregister(struct vpfe_video_device
*video
)
1643 if (video_is_registered(&video
->video_dev
)) {
1644 video_unregister_device(&video
->video_dev
);
1645 media_entity_cleanup(&video
->video_dev
.entity
);