Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/ebiederm...
[deliverable/linux.git] / drivers / staging / media / davinci_vpfe / vpfe_video.c
CommitLineData
622897da
MH
1/*
2 * Copyright (C) 2012 Texas Instruments Inc
3 *
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.
7 *
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.
12 *
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
16 *
17 * Contributors:
18 * Manjunath Hadli <manjunath.hadli@ti.com>
19 * Prabhakar Lad <prabhakar.lad@ti.com>
20 */
21
22#include <linux/module.h>
23#include <linux/slab.h>
24
25#include <media/v4l2-ioctl.h>
26
27#include "vpfe.h"
28#include "vpfe_mc_capture.h"
29
622897da
MH
30static int debug;
31
32/* get v4l2 subdev pointer to external subdev which is active */
33static struct media_entity *vpfe_get_input_entity
34 (struct vpfe_video_device *video)
35{
36 struct vpfe_device *vpfe_dev = video->vpfe_dev;
37 struct media_pad *remote;
38
1bddf1b3 39 remote = media_entity_remote_pad(&vpfe_dev->vpfe_isif.pads[0]);
622897da
MH
40 if (remote == NULL) {
41 pr_err("Invalid media connection to isif/ccdc\n");
42 return NULL;
43 }
44 return remote->entity;
45}
46
47/* updates external subdev(sensor/decoder) which is active */
48static int vpfe_update_current_ext_subdev(struct vpfe_video_device *video)
49{
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;
54 int i;
55
1bddf1b3 56 remote = media_entity_remote_pad(&vpfe_dev->vpfe_isif.pads[0]);
622897da
MH
57 if (remote == NULL) {
58 pr_err("Invalid media connection to isif/ccdc\n");
59 return -EINVAL;
60 }
61
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];
67 break;
68 }
69 }
70
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");
74 return -EINVAL;
75 }
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];
81 }
82 return 0;
83}
84
85/* get the subdev which is connected to the output video node */
86static struct v4l2_subdev *
87vpfe_video_remote_subdev(struct vpfe_video_device *video, u32 *pad)
88{
1bddf1b3 89 struct media_pad *remote = media_entity_remote_pad(&video->pad);
622897da 90
14fae6fc 91 if (!remote || !is_media_entity_v4l2_subdev(remote->entity))
622897da
MH
92 return NULL;
93 if (pad)
94 *pad = remote->index;
95 return media_entity_to_v4l2_subdev(remote->entity);
96}
97
98/* get the format set at output pad of the adjacent subdev */
99static int
100__vpfe_video_get_format(struct vpfe_video_device *video,
101 struct v4l2_format *format)
102{
103 struct v4l2_subdev_format fmt;
104 struct v4l2_subdev *subdev;
105 struct media_pad *remote;
106 u32 pad;
107 int ret;
108
109 subdev = vpfe_video_remote_subdev(video, &pad);
110 if (subdev == NULL)
111 return -EINVAL;
112
113 fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
1bddf1b3 114 remote = media_entity_remote_pad(&video->pad);
622897da
MH
115 fmt.pad = remote->index;
116
117 ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &fmt);
118 if (ret == -ENOIOCTLCMD)
119 return -EINVAL;
120
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);
125
126 return 0;
127}
128
129/* make a note of pipeline details */
bb0faebd 130static int vpfe_prepare_pipeline(struct vpfe_video_device *video)
622897da 131{
bb0faebd 132 struct media_entity_graph graph;
622897da 133 struct media_entity *entity = &video->video_dev.entity;
d10c9894 134 struct media_device *mdev = entity->graph_obj.mdev;
622897da
MH
135 struct vpfe_pipeline *pipe = &video->pipe;
136 struct vpfe_video_device *far_end = NULL;
bb0faebd 137 int ret;
622897da
MH
138
139 pipe->input_num = 0;
140 pipe->output_num = 0;
141
142 if (video->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
143 pipe->inputs[pipe->input_num++] = video;
144 else
145 pipe->outputs[pipe->output_num++] = video;
146
147 mutex_lock(&mdev->graph_mutex);
bb0faebd
SA
148 ret = media_entity_graph_walk_init(&graph, entity->graph_obj.mdev);
149 if (ret) {
4f388a92 150 mutex_unlock(&mdev->graph_mutex);
bb0faebd
SA
151 return -ENOMEM;
152 }
622897da
MH
153 media_entity_graph_walk_start(&graph, entity);
154 while ((entity = media_entity_graph_walk_next(&graph))) {
155 if (entity == &video->video_dev.entity)
156 continue;
59ecd59d 157 if (!is_media_entity_v4l2_io(entity))
622897da
MH
158 continue;
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;
162 else
163 pipe->outputs[pipe->output_num++] = far_end;
164 }
bb0faebd 165 media_entity_graph_walk_cleanup(&graph);
622897da 166 mutex_unlock(&mdev->graph_mutex);
bb0faebd
SA
167
168 return 0;
622897da
MH
169}
170
171/* update pipe state selected by user */
172static int vpfe_update_pipe_state(struct vpfe_video_device *video)
173{
174 struct vpfe_pipeline *pipe = &video->pipe;
8bbb6568 175 int ret;
622897da 176
8bbb6568
HV
177 ret = vpfe_prepare_pipeline(video);
178 if (ret)
179 return ret;
622897da 180
9c5d8933
TF
181 /*
182 * Find out if there is any input video
183 * if yes, it is single shot.
184 */
622897da
MH
185 if (pipe->input_num == 0) {
186 pipe->state = VPFE_PIPELINE_STREAM_CONTINUOUS;
8bbb6568
HV
187 ret = vpfe_update_current_ext_subdev(video);
188 if (ret) {
622897da 189 pr_err("Invalid external subdev\n");
8bbb6568 190 return ret;
622897da
MH
191 }
192 } else {
193 pipe->state = VPFE_PIPELINE_STREAM_SINGLESHOT;
194 }
195 video->initialized = 1;
196 video->skip_frame_count = 1;
197 video->skip_frame_count_init = 1;
198 return 0;
199}
200
201/* checks wether pipeline is ready for enabling */
202int vpfe_video_is_pipe_ready(struct vpfe_pipeline *pipe)
203{
204 int i;
205
206 for (i = 0; i < pipe->input_num; i++)
207 if (!pipe->inputs[i]->started ||
208 pipe->inputs[i]->state != VPFE_VIDEO_BUFFER_QUEUED)
209 return 0;
210 for (i = 0; i < pipe->output_num; i++)
211 if (!pipe->outputs[i]->started ||
212 pipe->outputs[i]->state != VPFE_VIDEO_BUFFER_QUEUED)
213 return 0;
214 return 1;
215}
216
217/**
218 * Validate a pipeline by checking both ends of all links for format
219 * discrepancies.
220 *
221 * Return 0 if all formats match, or -EPIPE if at least one link is found with
222 * different formats on its two ends.
223 */
224static int vpfe_video_validate_pipeline(struct vpfe_pipeline *pipe)
225{
226 struct v4l2_subdev_format fmt_source;
227 struct v4l2_subdev_format fmt_sink;
228 struct v4l2_subdev *subdev;
229 struct media_pad *pad;
230 int ret;
231
232 /*
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.
237 */
238 subdev = vpfe_video_remote_subdev(pipe->outputs[0], NULL);
239 if (subdev == NULL)
240 return -EPIPE;
241
242 while (1) {
243 /* Retrieve the sink format */
244 pad = &subdev->entity.pads[0];
245 if (!(pad->flags & MEDIA_PAD_FL_SINK))
246 break;
247
248 fmt_sink.which = V4L2_SUBDEV_FORMAT_ACTIVE;
249 fmt_sink.pad = pad->index;
250 ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL,
251 &fmt_sink);
252
253 if (ret < 0 && ret != -ENOIOCTLCMD)
254 return -EPIPE;
255
256 /* Retrieve the source format */
1bddf1b3 257 pad = media_entity_remote_pad(pad);
14fae6fc 258 if (!pad || !is_media_entity_v4l2_subdev(pad->entity))
622897da
MH
259 break;
260
261 subdev = media_entity_to_v4l2_subdev(pad->entity);
262
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)
267 return -EPIPE;
268
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)
273 return -EPIPE;
274 }
275 return 0;
276}
277
278/*
279 * vpfe_pipeline_enable() - Enable streaming on a pipeline
280 * @vpfe_dev: vpfe device
281 * @pipe: vpfe pipeline
282 *
283 * Walk the entities chain starting at the pipeline output video node and start
284 * all modules in the chain in the given mode.
285 *
286 * Return 0 if successful, or the return value of the failed video::s_stream
287 * operation otherwise.
288 */
289static int vpfe_pipeline_enable(struct vpfe_pipeline *pipe)
290{
622897da
MH
291 struct media_entity *entity;
292 struct v4l2_subdev *subdev;
293 struct media_device *mdev;
bb0faebd 294 int ret;
622897da
MH
295
296 if (pipe->state == VPFE_PIPELINE_STREAM_CONTINUOUS)
297 entity = vpfe_get_input_entity(pipe->outputs[0]);
298 else
299 entity = &pipe->inputs[0]->video_dev.entity;
300
d10c9894 301 mdev = entity->graph_obj.mdev;
622897da 302 mutex_lock(&mdev->graph_mutex);
bb0faebd
SA
303 ret = media_entity_graph_walk_init(&pipe->graph,
304 entity->graph_obj.mdev);
305 if (ret)
306 goto out;
307 media_entity_graph_walk_start(&pipe->graph, entity);
308 while ((entity = media_entity_graph_walk_next(&pipe->graph))) {
622897da 309
59ecd59d 310 if (!is_media_entity_v4l2_subdev(entity))
622897da
MH
311 continue;
312 subdev = media_entity_to_v4l2_subdev(entity);
313 ret = v4l2_subdev_call(subdev, video, s_stream, 1);
314 if (ret < 0 && ret != -ENOIOCTLCMD)
315 break;
316 }
bb0faebd
SA
317out:
318 if (ret)
319 media_entity_graph_walk_cleanup(&pipe->graph);
622897da
MH
320 mutex_unlock(&mdev->graph_mutex);
321 return ret;
322}
323
324/*
325 * vpfe_pipeline_disable() - Disable streaming on a pipeline
326 * @vpfe_dev: vpfe device
327 * @pipe: VPFE pipeline
328 *
329 * Walk the entities chain starting at the pipeline output video node and stop
330 * all modules in the chain.
331 *
332 * Return 0 if all modules have been properly stopped, or -ETIMEDOUT if a module
333 * can't be stopped.
334 */
335static int vpfe_pipeline_disable(struct vpfe_pipeline *pipe)
336{
622897da
MH
337 struct media_entity *entity;
338 struct v4l2_subdev *subdev;
339 struct media_device *mdev;
340 int ret = 0;
341
342 if (pipe->state == VPFE_PIPELINE_STREAM_CONTINUOUS)
343 entity = vpfe_get_input_entity(pipe->outputs[0]);
344 else
345 entity = &pipe->inputs[0]->video_dev.entity;
346
d10c9894 347 mdev = entity->graph_obj.mdev;
622897da 348 mutex_lock(&mdev->graph_mutex);
bb0faebd 349 media_entity_graph_walk_start(&pipe->graph, entity);
622897da 350
bb0faebd 351 while ((entity = media_entity_graph_walk_next(&pipe->graph))) {
622897da 352
3efdf62c 353 if (!is_media_entity_v4l2_subdev(entity))
622897da
MH
354 continue;
355 subdev = media_entity_to_v4l2_subdev(entity);
356 ret = v4l2_subdev_call(subdev, video, s_stream, 0);
357 if (ret < 0 && ret != -ENOIOCTLCMD)
358 break;
359 }
360 mutex_unlock(&mdev->graph_mutex);
361
bb0faebd 362 media_entity_graph_walk_cleanup(&pipe->graph);
3e6e3b3d 363 return ret ? -ETIMEDOUT : 0;
622897da
MH
364}
365
366/*
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)
371 *
372 * Set the pipeline to the given stream state.
373 *
2f9e96c3 374 * Return 0 if successful, or the return value of the failed video::s_stream
622897da
MH
375 * operation otherwise.
376 */
377static int vpfe_pipeline_set_stream(struct vpfe_pipeline *pipe,
378 enum vpfe_pipeline_stream_state state)
379{
380 if (state == VPFE_PIPELINE_STREAM_STOPPED)
381 return vpfe_pipeline_disable(pipe);
382
383 return vpfe_pipeline_enable(pipe);
384}
385
386static int all_videos_stopped(struct vpfe_video_device *video)
387{
388 struct vpfe_pipeline *pipe = &video->pipe;
389 int i;
390
391 for (i = 0; i < pipe->input_num; i++)
392 if (pipe->inputs[i]->started)
393 return 0;
394 for (i = 0; i < pipe->output_num; i++)
395 if (pipe->outputs[i]->started)
396 return 0;
397 return 1;
398}
399
400/*
401 * vpfe_open() - open video device
402 * @file: file pointer
403 *
404 * initialize media pipeline state, allocate memory for file handle
405 *
406 * Return 0 if successful, or the return -ENODEV otherwise.
407 */
408static int vpfe_open(struct file *file)
409{
410 struct vpfe_video_device *video = video_drvdata(file);
411 struct vpfe_fh *handle;
412
413 /* Allocate memory for the file handle object */
414 handle = kzalloc(sizeof(struct vpfe_fh), GFP_KERNEL);
415
416 if (handle == NULL)
417 return -ENOMEM;
418
419 v4l2_fh_init(&handle->vfh, &video->video_dev);
420 v4l2_fh_add(&handle->vfh);
421
422 mutex_lock(&video->lock);
423 /* If decoder is not initialized. initialize it */
424 if (!video->initialized && vpfe_update_pipe_state(video)) {
425 mutex_unlock(&video->lock);
426 return -ENODEV;
427 }
428 /* Increment device users counter */
429 video->usrs++;
430 /* Set io_allowed member to false */
431 handle->io_allowed = 0;
622897da
MH
432 handle->video = video;
433 file->private_data = &handle->vfh;
434 mutex_unlock(&video->lock);
435
436 return 0;
437}
438
439/* get the next buffer available from dma queue */
440static unsigned long
441vpfe_video_get_next_buffer(struct vpfe_video_device *video)
442{
443 video->cur_frm = video->next_frm =
444 list_entry(video->dma_queue.next,
445 struct vpfe_cap_buffer, list);
446
447 list_del(&video->next_frm->list);
2d700715
JS
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);
622897da
MH
450}
451
452/* schedule the next buffer which is available on dma queue */
453void vpfe_video_schedule_next_buffer(struct vpfe_video_device *video)
454{
455 struct vpfe_device *vpfe_dev = video->vpfe_dev;
456 unsigned long addr;
457
458 if (list_empty(&video->dma_queue))
459 return;
460
461 video->next_frm = list_entry(video->dma_queue.next,
462 struct vpfe_cap_buffer, list);
463
b369a87c 464 if (video->pipe.state == VPFE_PIPELINE_STREAM_SINGLESHOT)
622897da
MH
465 video->cur_frm = video->next_frm;
466
467 list_del(&video->next_frm->list);
2d700715
JS
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);
622897da
MH
470 video->ops->queue(vpfe_dev, addr);
471 video->state = VPFE_VIDEO_BUFFER_QUEUED;
472}
473
474/* schedule the buffer for capturing bottom field */
475void vpfe_video_schedule_bottom_field(struct vpfe_video_device *video)
476{
477 struct vpfe_device *vpfe_dev = video->vpfe_dev;
478 unsigned long addr;
479
2d700715 480 addr = vb2_dma_contig_plane_dma_addr(&video->cur_frm->vb.vb2_buf, 0);
622897da
MH
481 addr += video->field_off;
482 video->ops->queue(vpfe_dev, addr);
483}
484
485/* make buffer available for dequeue */
486void vpfe_video_process_buffer_complete(struct vpfe_video_device *video)
487{
488 struct vpfe_pipeline *pipe = &video->pipe;
489
d6dd645e 490 video->cur_frm->vb.vb2_buf.timestamp = ktime_get_ns();
2d700715 491 vb2_buffer_done(&video->cur_frm->vb.vb2_buf, VB2_BUF_STATE_DONE);
622897da
MH
492 if (pipe->state == VPFE_PIPELINE_STREAM_CONTINUOUS)
493 video->cur_frm = video->next_frm;
494}
495
496/* vpfe_stop_capture() - stop streaming */
497static void vpfe_stop_capture(struct vpfe_video_device *video)
498{
499 struct vpfe_pipeline *pipe = &video->pipe;
500
501 video->started = 0;
502
503 if (video->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
504 return;
505 if (all_videos_stopped(video))
506 vpfe_pipeline_set_stream(pipe,
507 VPFE_PIPELINE_STREAM_STOPPED);
508}
509
510/*
511 * vpfe_release() - release video device
512 * @file: file pointer
513 *
514 * deletes buffer queue, frees the buffers and the vpfe file handle
515 *
516 * Return 0
517 */
518static int vpfe_release(struct file *file)
519{
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);
524
525 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_release\n");
526
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);
9c5d8933
TF
533 /*
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.
537 */
622897da
MH
538 video->pipe.state = VPFE_PIPELINE_STREAM_STOPPED;
539 vb2_streamoff(&video->buffer_queue,
540 video->buffer_queue.type);
541 }
542 video->io_usrs = 0;
543 /* Free buffers allocated */
544 vb2_queue_release(&video->buffer_queue);
545 vb2_dma_contig_cleanup_ctx(video->alloc_ctx);
546 }
547 /* Decrement device users counter */
548 video->usrs--;
933fd6e5
LP
549 v4l2_fh_del(&fh->vfh);
550 v4l2_fh_exit(&fh->vfh);
622897da
MH
551 /* If this is the last file handle */
552 if (!video->usrs)
553 video->initialized = 0;
554 mutex_unlock(&video->lock);
555 file->private_data = NULL;
556 /* Free memory allocated to file handle object */
557 v4l2_fh_del(vfh);
558 kzfree(fh);
559 return 0;
560}
561
562/*
563 * vpfe_mmap() - It is used to map kernel space buffers
564 * into user spaces
565 */
566static int vpfe_mmap(struct file *file, struct vm_area_struct *vma)
567{
568 struct vpfe_video_device *video = video_drvdata(file);
569 struct vpfe_device *vpfe_dev = video->vpfe_dev;
570
571 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_mmap\n");
572 return vb2_mmap(&video->buffer_queue, vma);
573}
574
575/*
576 * vpfe_poll() - It is used for select/poll system call
577 */
578static unsigned int vpfe_poll(struct file *file, poll_table *wait)
579{
580 struct vpfe_video_device *video = video_drvdata(file);
581 struct vpfe_device *vpfe_dev = video->vpfe_dev;
582
583 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_poll\n");
584 if (video->started)
585 return vb2_poll(&video->buffer_queue, file, wait);
586 return 0;
587}
588
589/* vpfe capture driver file operations */
590static const struct v4l2_file_operations vpfe_fops = {
591 .owner = THIS_MODULE,
592 .open = vpfe_open,
593 .release = vpfe_release,
594 .unlocked_ioctl = video_ioctl2,
595 .mmap = vpfe_mmap,
596 .poll = vpfe_poll
597};
598
599/*
600 * vpfe_querycap() - query capabilities of video device
601 * @file: file pointer
602 * @priv: void pointer
603 * @cap: pointer to v4l2_capability structure
604 *
605 * fills v4l2 capabilities structure
606 *
607 * Return 0
608 */
609static int vpfe_querycap(struct file *file, void *priv,
610 struct v4l2_capability *cap)
611{
612 struct vpfe_video_device *video = video_drvdata(file);
613 struct vpfe_device *vpfe_dev = video->vpfe_dev;
614
615 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_querycap\n");
616
617 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
57e774cc 618 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
622897da 619 else
57e774cc
HV
620 cap->device_caps = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_STREAMING;
621 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VIDEO_OUTPUT |
622 V4L2_CAP_STREAMING | V4L2_CAP_DEVICE_CAPS;
622897da
MH
623 strlcpy(cap->driver, CAPTURE_DRV_NAME, sizeof(cap->driver));
624 strlcpy(cap->bus_info, "VPFE", sizeof(cap->bus_info));
625 strlcpy(cap->card, vpfe_dev->cfg->card_name, sizeof(cap->card));
626
627 return 0;
628}
629
630/*
631 * vpfe_g_fmt() - get the format which is active on video device
632 * @file: file pointer
633 * @priv: void pointer
634 * @fmt: pointer to v4l2_format structure
635 *
636 * fills v4l2 format structure with active format
637 *
638 * Return 0
639 */
640static int vpfe_g_fmt(struct file *file, void *priv,
641 struct v4l2_format *fmt)
642{
643 struct vpfe_video_device *video = video_drvdata(file);
644 struct vpfe_device *vpfe_dev = video->vpfe_dev;
645
646 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_g_fmt\n");
647 /* Fill in the information about format */
648 *fmt = video->fmt;
649 return 0;
650}
651
652/*
653 * vpfe_enum_fmt() - enum formats supported on media chain
654 * @file: file pointer
655 * @priv: void pointer
656 * @fmt: pointer to v4l2_fmtdesc structure
657 *
658 * fills v4l2_fmtdesc structure with output format set on adjacent subdev,
659 * only one format is enumearted as subdevs are already configured
660 *
2f9e96c3 661 * Return 0 if successful, error code otherwise
622897da
MH
662 */
663static int vpfe_enum_fmt(struct file *file, void *priv,
664 struct v4l2_fmtdesc *fmt)
665{
666 struct vpfe_video_device *video = video_drvdata(file);
667 struct vpfe_device *vpfe_dev = video->vpfe_dev;
668 struct v4l2_subdev_format sd_fmt;
669 struct v4l2_mbus_framefmt mbus;
670 struct v4l2_subdev *subdev;
671 struct v4l2_format format;
672 struct media_pad *remote;
8bbb6568 673 int ret;
622897da
MH
674
675 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_enum_fmt\n");
676
9c5d8933
TF
677 /*
678 * since already subdev pad format is set,
679 * only one pixel format is available
680 */
622897da
MH
681 if (fmt->index > 0) {
682 v4l2_err(&vpfe_dev->v4l2_dev, "Invalid index\n");
683 return -EINVAL;
684 }
685 /* get the remote pad */
1bddf1b3 686 remote = media_entity_remote_pad(&video->pad);
622897da
MH
687 if (remote == NULL) {
688 v4l2_err(&vpfe_dev->v4l2_dev,
689 "invalid remote pad for video node\n");
690 return -EINVAL;
691 }
692 /* get the remote subdev */
693 subdev = vpfe_video_remote_subdev(video, NULL);
694 if (subdev == NULL) {
695 v4l2_err(&vpfe_dev->v4l2_dev,
696 "invalid remote subdev for video node\n");
697 return -EINVAL;
698 }
699 sd_fmt.pad = remote->index;
700 sd_fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
701 /* get output format of remote subdev */
8bbb6568
HV
702 ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &sd_fmt);
703 if (ret) {
622897da
MH
704 v4l2_err(&vpfe_dev->v4l2_dev,
705 "invalid remote subdev for video node\n");
8bbb6568 706 return ret;
622897da
MH
707 }
708 /* convert to pix format */
709 mbus.code = sd_fmt.format.code;
710 mbus_to_pix(&mbus, &format.fmt.pix);
711 /* copy the result */
712 fmt->pixelformat = format.fmt.pix.pixelformat;
713
714 return 0;
715}
716
717/*
718 * vpfe_s_fmt() - set the format on video device
719 * @file: file pointer
720 * @priv: void pointer
721 * @fmt: pointer to v4l2_format structure
722 *
723 * validate and set the format on video device
724 *
725 * Return 0 on success, error code otherwise
726 */
727static int vpfe_s_fmt(struct file *file, void *priv,
728 struct v4l2_format *fmt)
729{
730 struct vpfe_video_device *video = video_drvdata(file);
731 struct vpfe_device *vpfe_dev = video->vpfe_dev;
732 struct v4l2_format format;
8bbb6568 733 int ret;
622897da
MH
734
735 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_s_fmt\n");
736 /* If streaming is started, return error */
737 if (video->started) {
738 v4l2_err(&vpfe_dev->v4l2_dev, "Streaming is started\n");
739 return -EBUSY;
740 }
741 /* get adjacent subdev's output pad format */
8bbb6568
HV
742 ret = __vpfe_video_get_format(video, &format);
743 if (ret)
744 return ret;
622897da
MH
745 *fmt = format;
746 video->fmt = *fmt;
747 return 0;
748}
749
750/*
751 * vpfe_try_fmt() - try the format on video device
752 * @file: file pointer
753 * @priv: void pointer
754 * @fmt: pointer to v4l2_format structure
755 *
756 * validate the format, update with correct format
757 * based on output format set on adjacent subdev
758 *
759 * Return 0 on success, error code otherwise
760 */
761static int vpfe_try_fmt(struct file *file, void *priv,
762 struct v4l2_format *fmt)
763{
764 struct vpfe_video_device *video = video_drvdata(file);
765 struct vpfe_device *vpfe_dev = video->vpfe_dev;
766 struct v4l2_format format;
8bbb6568 767 int ret;
622897da
MH
768
769 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_try_fmt\n");
770 /* get adjacent subdev's output pad format */
8bbb6568
HV
771 ret = __vpfe_video_get_format(video, &format);
772 if (ret)
773 return ret;
622897da
MH
774
775 *fmt = format;
776 return 0;
777}
778
779/*
780 * vpfe_enum_input() - enum inputs supported on media chain
781 * @file: file pointer
782 * @priv: void pointer
783 * @fmt: pointer to v4l2_fmtdesc structure
784 *
785 * fills v4l2_input structure with input available on media chain,
786 * only one input is enumearted as media chain is setup by this time
787 *
2f9e96c3 788 * Return 0 if successful, -EINVAL is media chain is invalid
622897da
MH
789 */
790static int vpfe_enum_input(struct file *file, void *priv,
791 struct v4l2_input *inp)
792{
793 struct vpfe_video_device *video = video_drvdata(file);
794 struct vpfe_ext_subdev_info *sdinfo = video->current_ext_subdev;
795 struct vpfe_device *vpfe_dev = video->vpfe_dev;
796
797 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_enum_input\n");
2f9e96c3 798 /* enumerate from the subdev user has chosen through mc */
622897da
MH
799 if (inp->index < sdinfo->num_inputs) {
800 memcpy(inp, &sdinfo->inputs[inp->index],
801 sizeof(struct v4l2_input));
802 return 0;
803 }
804 return -EINVAL;
805}
806
807/*
808 * vpfe_g_input() - get index of the input which is active
809 * @file: file pointer
810 * @priv: void pointer
811 * @index: pointer to unsigned int
812 *
813 * set index with input index which is active
814 */
815static int vpfe_g_input(struct file *file, void *priv, unsigned int *index)
816{
817 struct vpfe_video_device *video = video_drvdata(file);
818 struct vpfe_device *vpfe_dev = video->vpfe_dev;
819
820 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_g_input\n");
821
822 *index = video->current_input;
823 return 0;
824}
825
826/*
827 * vpfe_s_input() - set input which is pointed by input index
828 * @file: file pointer
829 * @priv: void pointer
830 * @index: pointer to unsigned int
831 *
832 * set input on external subdev
833 *
834 * Return 0 on success, error code otherwise
835 */
836static int vpfe_s_input(struct file *file, void *priv, unsigned int index)
837{
838 struct vpfe_video_device *video = video_drvdata(file);
839 struct vpfe_device *vpfe_dev = video->vpfe_dev;
840 struct vpfe_ext_subdev_info *sdinfo;
841 struct vpfe_route *route;
842 struct v4l2_input *inps;
843 u32 output;
844 u32 input;
845 int ret;
846 int i;
847
848 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_s_input\n");
849
8bbb6568
HV
850 ret = mutex_lock_interruptible(&video->lock);
851 if (ret)
852 return ret;
622897da
MH
853 /*
854 * If streaming is started return device busy
855 * error
856 */
857 if (video->started) {
858 v4l2_err(&vpfe_dev->v4l2_dev, "Streaming is on\n");
859 ret = -EBUSY;
860 goto unlock_out;
861 }
862
863 sdinfo = video->current_ext_subdev;
864 if (!sdinfo->registered) {
865 ret = -EINVAL;
866 goto unlock_out;
867 }
868 if (vpfe_dev->cfg->setup_input &&
869 vpfe_dev->cfg->setup_input(sdinfo->grp_id) < 0) {
870 ret = -EFAULT;
871 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev,
872 "couldn't setup input for %s\n",
873 sdinfo->module_name);
874 goto unlock_out;
875 }
876 route = &sdinfo->routes[index];
877 if (route && sdinfo->can_route) {
878 input = route->input;
879 output = route->output;
880 ret = v4l2_device_call_until_err(&vpfe_dev->v4l2_dev,
881 sdinfo->grp_id, video,
882 s_routing, input, output, 0);
883 if (ret) {
884 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev,
885 "s_input:error in setting input in decoder\n");
886 ret = -EINVAL;
887 goto unlock_out;
888 }
889 }
890 /* set standards set by subdev in video device */
891 for (i = 0; i < sdinfo->num_inputs; i++) {
892 inps = &sdinfo->inputs[i];
893 video->video_dev.tvnorms |= inps->std;
894 }
895 video->current_input = index;
896unlock_out:
897 mutex_unlock(&video->lock);
898 return ret;
899}
900
901/*
902 * vpfe_querystd() - query std which is being input on external subdev
903 * @file: file pointer
904 * @priv: void pointer
905 * @std_id: pointer to v4l2_std_id structure
906 *
907 * call external subdev through v4l2_device_call_until_err to
908 * get the std that is being active.
909 *
910 * Return 0 on success, error code otherwise
911 */
912static int vpfe_querystd(struct file *file, void *priv, v4l2_std_id *std_id)
913{
914 struct vpfe_video_device *video = video_drvdata(file);
915 struct vpfe_device *vpfe_dev = video->vpfe_dev;
916 struct vpfe_ext_subdev_info *sdinfo;
917 int ret;
918
919 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_querystd\n");
920
921 ret = mutex_lock_interruptible(&video->lock);
922 sdinfo = video->current_ext_subdev;
923 if (ret)
924 return ret;
925 /* Call querystd function of decoder device */
926 ret = v4l2_device_call_until_err(&vpfe_dev->v4l2_dev, sdinfo->grp_id,
927 video, querystd, std_id);
928 mutex_unlock(&video->lock);
929 return ret;
930}
931
932/*
933 * vpfe_s_std() - set std on external subdev
934 * @file: file pointer
935 * @priv: void pointer
936 * @std_id: pointer to v4l2_std_id structure
937 *
938 * set std pointed by std_id on external subdev by calling it using
939 * v4l2_device_call_until_err
940 *
941 * Return 0 on success, error code otherwise
942 */
314527ac 943static int vpfe_s_std(struct file *file, void *priv, v4l2_std_id std_id)
622897da
MH
944{
945 struct vpfe_video_device *video = video_drvdata(file);
946 struct vpfe_device *vpfe_dev = video->vpfe_dev;
947 struct vpfe_ext_subdev_info *sdinfo;
948 int ret;
949
950 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_s_std\n");
951
952 /* Call decoder driver function to set the standard */
8bbb6568
HV
953 ret = mutex_lock_interruptible(&video->lock);
954 if (ret)
955 return ret;
622897da
MH
956 sdinfo = video->current_ext_subdev;
957 /* If streaming is started, return device busy error */
958 if (video->started) {
959 v4l2_err(&vpfe_dev->v4l2_dev, "streaming is started\n");
960 ret = -EBUSY;
961 goto unlock_out;
962 }
963 ret = v4l2_device_call_until_err(&vpfe_dev->v4l2_dev, sdinfo->grp_id,
8774bed9 964 video, s_std, std_id);
622897da
MH
965 if (ret < 0) {
966 v4l2_err(&vpfe_dev->v4l2_dev, "Failed to set standard\n");
967 video->stdid = V4L2_STD_UNKNOWN;
968 goto unlock_out;
969 }
314527ac 970 video->stdid = std_id;
622897da
MH
971unlock_out:
972 mutex_unlock(&video->lock);
973 return ret;
974}
975
976static int vpfe_g_std(struct file *file, void *priv, v4l2_std_id *tvnorm)
977{
978 struct vpfe_video_device *video = video_drvdata(file);
979 struct vpfe_device *vpfe_dev = video->vpfe_dev;
980
981 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_g_std\n");
982 *tvnorm = video->stdid;
983 return 0;
984}
985
986/*
987 * vpfe_enum_dv_timings() - enumerate dv_timings which are supported by
988 * to external subdev
989 * @file: file pointer
990 * @priv: void pointer
991 * @timings: pointer to v4l2_enum_dv_timings structure
992 *
993 * enum dv_timings's which are supported by external subdev through
994 * v4l2_subdev_call
995 *
996 * Return 0 on success, error code otherwise
997 */
998static int
999vpfe_enum_dv_timings(struct file *file, void *fh,
1000 struct v4l2_enum_dv_timings *timings)
1001{
1002 struct vpfe_video_device *video = video_drvdata(file);
1003 struct vpfe_device *vpfe_dev = video->vpfe_dev;
1004 struct v4l2_subdev *subdev = video->current_ext_subdev->subdev;
1005
0eda185c
LP
1006 timings->pad = 0;
1007
622897da 1008 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_enum_dv_timings\n");
0eda185c 1009 return v4l2_subdev_call(subdev, pad, enum_dv_timings, timings);
622897da
MH
1010}
1011
1012/*
1013 * vpfe_query_dv_timings() - query the dv_timings which is being input
1014 * to external subdev
1015 * @file: file pointer
1016 * @priv: void pointer
1017 * @timings: pointer to v4l2_dv_timings structure
1018 *
1019 * get dv_timings which is being input on external subdev through
1020 * v4l2_subdev_call
1021 *
1022 * Return 0 on success, error code otherwise
1023 */
1024static int
1025vpfe_query_dv_timings(struct file *file, void *fh,
1026 struct v4l2_dv_timings *timings)
1027{
1028 struct vpfe_video_device *video = video_drvdata(file);
1029 struct vpfe_device *vpfe_dev = video->vpfe_dev;
1030 struct v4l2_subdev *subdev = video->current_ext_subdev->subdev;
1031
1032 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_query_dv_timings\n");
1033 return v4l2_subdev_call(subdev, video, query_dv_timings, timings);
1034}
1035
1036/*
4bd3bb71 1037 * vpfe_s_dv_timings() - set dv_timings on external subdev
622897da
MH
1038 * @file: file pointer
1039 * @priv: void pointer
1040 * @timings: pointer to v4l2_dv_timings structure
1041 *
4bd3bb71 1042 * set dv_timings pointed by timings on external subdev through
622897da
MH
1043 * v4l2_device_call_until_err, this configures amplifier also
1044 *
1045 * Return 0 on success, error code otherwise
1046 */
1047static int
1048vpfe_s_dv_timings(struct file *file, void *fh,
1049 struct v4l2_dv_timings *timings)
1050{
1051 struct vpfe_video_device *video = video_drvdata(file);
1052 struct vpfe_device *vpfe_dev = video->vpfe_dev;
1053
1054 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_s_dv_timings\n");
1055
1056 video->stdid = V4L2_STD_UNKNOWN;
1057 return v4l2_device_call_until_err(&vpfe_dev->v4l2_dev,
1058 video->current_ext_subdev->grp_id,
1059 video, s_dv_timings, timings);
1060}
1061
1062/*
4bd3bb71 1063 * vpfe_g_dv_timings() - get dv_timings which is set on external subdev
622897da
MH
1064 * @file: file pointer
1065 * @priv: void pointer
1066 * @timings: pointer to v4l2_dv_timings structure
1067 *
4bd3bb71 1068 * get dv_timings which is set on external subdev through
622897da
MH
1069 * v4l2_subdev_call
1070 *
1071 * Return 0 on success, error code otherwise
1072 */
1073static int
1074vpfe_g_dv_timings(struct file *file, void *fh,
1075 struct v4l2_dv_timings *timings)
1076{
1077 struct vpfe_video_device *video = video_drvdata(file);
1078 struct vpfe_device *vpfe_dev = video->vpfe_dev;
1079 struct v4l2_subdev *subdev = video->current_ext_subdev->subdev;
1080
1081 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_g_dv_timings\n");
1082 return v4l2_subdev_call(subdev, video, g_dv_timings, timings);
1083}
1084
1085/*
1086 * Videobuf operations
1087 */
1088/*
1089 * vpfe_buffer_queue_setup : Callback function for buffer setup.
1090 * @vq: vb2_queue ptr
1091 * @fmt: v4l2 format
1092 * @nbuffers: ptr to number of buffers requested by application
1093 * @nplanes:: contains number of distinct video planes needed to hold a frame
1094 * @sizes[]: contains the size (in bytes) of each plane.
1095 * @alloc_ctxs: ptr to allocation context
1096 *
1097 * This callback function is called when reqbuf() is called to adjust
1098 * the buffer nbuffers and buffer size
1099 */
1100static int
df9ecb0c 1101vpfe_buffer_queue_setup(struct vb2_queue *vq,
622897da
MH
1102 unsigned int *nbuffers, unsigned int *nplanes,
1103 unsigned int sizes[], void *alloc_ctxs[])
1104{
1105 struct vpfe_fh *fh = vb2_get_drv_priv(vq);
1106 struct vpfe_video_device *video = fh->video;
1107 struct vpfe_device *vpfe_dev = video->vpfe_dev;
622897da
MH
1108 unsigned long size;
1109
1110 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_buffer_queue_setup\n");
1111 size = video->fmt.fmt.pix.sizeimage;
1112
171fe6d1
LP
1113 if (vq->num_buffers + *nbuffers < 3)
1114 *nbuffers = 3 - vq->num_buffers;
1115
622897da
MH
1116 *nplanes = 1;
1117 sizes[0] = size;
1118 alloc_ctxs[0] = video->alloc_ctx;
1119 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev,
1120 "nbuffers=%d, size=%lu\n", *nbuffers, size);
1121 return 0;
1122}
1123
1124/*
1125 * vpfe_buffer_prepare : callback function for buffer prepare
1126 * @vb: ptr to vb2_buffer
1127 *
1128 * This is the callback function for buffer prepare when vb2_qbuf()
1129 * function is called. The buffer is prepared and user space virtual address
1130 * or user address is converted into physical address
1131 */
1132static int vpfe_buffer_prepare(struct vb2_buffer *vb)
1133{
1134 struct vpfe_fh *fh = vb2_get_drv_priv(vb->vb2_queue);
1135 struct vpfe_video_device *video = fh->video;
1136 struct vpfe_device *vpfe_dev = video->vpfe_dev;
1137 unsigned long addr;
1138
1139 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_buffer_prepare\n");
1140
1141 if (vb->state != VB2_BUF_STATE_ACTIVE &&
1142 vb->state != VB2_BUF_STATE_PREPARED)
1143 return 0;
1144
1145 /* Initialize buffer */
1146 vb2_set_plane_payload(vb, 0, video->fmt.fmt.pix.sizeimage);
1147 if (vb2_plane_vaddr(vb, 0) &&
1148 vb2_get_plane_payload(vb, 0) > vb2_plane_size(vb, 0))
1149 return -EINVAL;
1150
1151 addr = vb2_dma_contig_plane_dma_addr(vb, 0);
1152 /* Make sure user addresses are aligned to 32 bytes */
1153 if (!ALIGN(addr, 32))
1154 return -EINVAL;
1155
1156 return 0;
1157}
1158
1159static void vpfe_buffer_queue(struct vb2_buffer *vb)
1160{
2d700715 1161 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
622897da
MH
1162 /* Get the file handle object and device object */
1163 struct vpfe_fh *fh = vb2_get_drv_priv(vb->vb2_queue);
1164 struct vpfe_video_device *video = fh->video;
1165 struct vpfe_device *vpfe_dev = video->vpfe_dev;
1166 struct vpfe_pipeline *pipe = &video->pipe;
2d700715 1167 struct vpfe_cap_buffer *buf = container_of(vbuf,
622897da
MH
1168 struct vpfe_cap_buffer, vb);
1169 unsigned long flags;
1170 unsigned long empty;
1171 unsigned long addr;
1172
1173 spin_lock_irqsave(&video->dma_queue_lock, flags);
1174 empty = list_empty(&video->dma_queue);
1175 /* add the buffer to the DMA queue */
1176 list_add_tail(&buf->list, &video->dma_queue);
1177 spin_unlock_irqrestore(&video->dma_queue_lock, flags);
1178 /* this case happens in case of single shot */
1179 if (empty && video->started && pipe->state ==
1180 VPFE_PIPELINE_STREAM_SINGLESHOT &&
1181 video->state == VPFE_VIDEO_BUFFER_NOT_QUEUED) {
1182 spin_lock(&video->dma_queue_lock);
1183 addr = vpfe_video_get_next_buffer(video);
1184 video->ops->queue(vpfe_dev, addr);
1185
1186 video->state = VPFE_VIDEO_BUFFER_QUEUED;
1187 spin_unlock(&video->dma_queue_lock);
1188
1189 /* enable h/w each time in single shot */
1190 if (vpfe_video_is_pipe_ready(pipe))
1191 vpfe_pipeline_set_stream(pipe,
1192 VPFE_PIPELINE_STREAM_SINGLESHOT);
1193 }
1194}
1195
1196/* vpfe_start_capture() - start streaming on all the subdevs */
1197static int vpfe_start_capture(struct vpfe_video_device *video)
1198{
1199 struct vpfe_pipeline *pipe = &video->pipe;
1200 int ret = 0;
1201
1202 video->started = 1;
1203 if (vpfe_video_is_pipe_ready(pipe))
1204 ret = vpfe_pipeline_set_stream(pipe, pipe->state);
1205
1206 return ret;
1207}
1208
1209static int vpfe_start_streaming(struct vb2_queue *vq, unsigned int count)
1210{
1211 struct vpfe_fh *fh = vb2_get_drv_priv(vq);
1212 struct vpfe_video_device *video = fh->video;
1213 struct vpfe_device *vpfe_dev = video->vpfe_dev;
1214 unsigned long addr;
1215 int ret;
1216
1217 ret = mutex_lock_interruptible(&video->lock);
1218 if (ret)
1219 goto streamoff;
1220
1221 /* Get the next frame from the buffer queue */
1222 video->cur_frm = video->next_frm =
1223 list_entry(video->dma_queue.next, struct vpfe_cap_buffer, list);
1224 /* Remove buffer from the buffer queue */
1225 list_del(&video->cur_frm->list);
1226 /* Mark state of the current frame to active */
2d700715 1227 video->cur_frm->vb.vb2_buf.state = VB2_BUF_STATE_ACTIVE;
622897da
MH
1228 /* Initialize field_id and started member */
1229 video->field_id = 0;
2d700715 1230 addr = vb2_dma_contig_plane_dma_addr(&video->cur_frm->vb.vb2_buf, 0);
622897da
MH
1231 video->ops->queue(vpfe_dev, addr);
1232 video->state = VPFE_VIDEO_BUFFER_QUEUED;
1233
1234 ret = vpfe_start_capture(video);
8f7402a3
LP
1235 if (ret) {
1236 struct vpfe_cap_buffer *buf, *tmp;
1237
2d700715
JS
1238 vb2_buffer_done(&video->cur_frm->vb.vb2_buf,
1239 VB2_BUF_STATE_QUEUED);
8f7402a3
LP
1240 list_for_each_entry_safe(buf, tmp, &video->dma_queue, list) {
1241 list_del(&buf->list);
2d700715
JS
1242 vb2_buffer_done(&buf->vb.vb2_buf,
1243 VB2_BUF_STATE_QUEUED);
8f7402a3 1244 }
622897da 1245 goto unlock_out;
8f7402a3 1246 }
622897da
MH
1247
1248 mutex_unlock(&video->lock);
1249
1250 return ret;
1251unlock_out:
1252 mutex_unlock(&video->lock);
1253streamoff:
1254 ret = vb2_streamoff(&video->buffer_queue, video->buffer_queue.type);
1255 return 0;
1256}
1257
1258static int vpfe_buffer_init(struct vb2_buffer *vb)
1259{
2d700715
JS
1260 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
1261 struct vpfe_cap_buffer *buf = container_of(vbuf,
622897da
MH
1262 struct vpfe_cap_buffer, vb);
1263
1264 INIT_LIST_HEAD(&buf->list);
1265 return 0;
1266}
1267
1268/* abort streaming and wait for last buffer */
e37559b2 1269static void vpfe_stop_streaming(struct vb2_queue *vq)
622897da
MH
1270{
1271 struct vpfe_fh *fh = vb2_get_drv_priv(vq);
1272 struct vpfe_video_device *video = fh->video;
1273
622897da 1274 /* release all active buffers */
d891ae5e 1275 if (video->cur_frm == video->next_frm) {
2d700715
JS
1276 vb2_buffer_done(&video->cur_frm->vb.vb2_buf,
1277 VB2_BUF_STATE_ERROR);
d891ae5e
LP
1278 } else {
1279 if (video->cur_frm != NULL)
2d700715 1280 vb2_buffer_done(&video->cur_frm->vb.vb2_buf,
d891ae5e
LP
1281 VB2_BUF_STATE_ERROR);
1282 if (video->next_frm != NULL)
2d700715 1283 vb2_buffer_done(&video->next_frm->vb.vb2_buf,
d891ae5e
LP
1284 VB2_BUF_STATE_ERROR);
1285 }
1286
622897da
MH
1287 while (!list_empty(&video->dma_queue)) {
1288 video->next_frm = list_entry(video->dma_queue.next,
1289 struct vpfe_cap_buffer, list);
1290 list_del(&video->next_frm->list);
2d700715
JS
1291 vb2_buffer_done(&video->next_frm->vb.vb2_buf,
1292 VB2_BUF_STATE_ERROR);
622897da 1293 }
622897da
MH
1294}
1295
1296static void vpfe_buf_cleanup(struct vb2_buffer *vb)
1297{
2d700715 1298 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
622897da
MH
1299 struct vpfe_fh *fh = vb2_get_drv_priv(vb->vb2_queue);
1300 struct vpfe_video_device *video = fh->video;
1301 struct vpfe_device *vpfe_dev = video->vpfe_dev;
2d700715 1302 struct vpfe_cap_buffer *buf = container_of(vbuf,
622897da
MH
1303 struct vpfe_cap_buffer, vb);
1304
1305 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_buf_cleanup\n");
1306 if (vb->state == VB2_BUF_STATE_ACTIVE)
1307 list_del_init(&buf->list);
1308}
1309
1310static struct vb2_ops video_qops = {
1311 .queue_setup = vpfe_buffer_queue_setup,
1312 .buf_init = vpfe_buffer_init,
1313 .buf_prepare = vpfe_buffer_prepare,
1314 .start_streaming = vpfe_start_streaming,
1315 .stop_streaming = vpfe_stop_streaming,
1316 .buf_cleanup = vpfe_buf_cleanup,
1317 .buf_queue = vpfe_buffer_queue,
1318};
1319
1320/*
1321 * vpfe_reqbufs() - supported REQBUF only once opening
1322 * the device.
1323 */
1324static int vpfe_reqbufs(struct file *file, void *priv,
1325 struct v4l2_requestbuffers *req_buf)
1326{
1327 struct vpfe_video_device *video = video_drvdata(file);
1328 struct vpfe_device *vpfe_dev = video->vpfe_dev;
1329 struct vpfe_fh *fh = file->private_data;
1330 struct vb2_queue *q;
1331 int ret;
1332
1333 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_reqbufs\n");
1334
b369a87c
TF
1335 if (req_buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1336 req_buf->type != V4L2_BUF_TYPE_VIDEO_OUTPUT){
622897da
MH
1337 v4l2_err(&vpfe_dev->v4l2_dev, "Invalid buffer type\n");
1338 return -EINVAL;
1339 }
1340
8bbb6568
HV
1341 ret = mutex_lock_interruptible(&video->lock);
1342 if (ret)
1343 return ret;
622897da
MH
1344
1345 if (video->io_usrs != 0) {
1346 v4l2_err(&vpfe_dev->v4l2_dev, "Only one IO user allowed\n");
1347 ret = -EBUSY;
1348 goto unlock_out;
1349 }
1350 video->memory = req_buf->memory;
1351
1352 /* Initialize videobuf2 queue as per the buffer type */
1353 video->alloc_ctx = vb2_dma_contig_init_ctx(vpfe_dev->pdev);
1354 if (IS_ERR(video->alloc_ctx)) {
1355 v4l2_err(&vpfe_dev->v4l2_dev, "Failed to get the context\n");
1356 return PTR_ERR(video->alloc_ctx);
1357 }
1358
1359 q = &video->buffer_queue;
1360 q->type = req_buf->type;
1361 q->io_modes = VB2_MMAP | VB2_USERPTR;
1362 q->drv_priv = fh;
b3379c62 1363 q->min_buffers_needed = 1;
622897da
MH
1364 q->ops = &video_qops;
1365 q->mem_ops = &vb2_dma_contig_memops;
1366 q->buf_struct_size = sizeof(struct vpfe_cap_buffer);
4f26aa17 1367 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
622897da 1368
8bbb6568
HV
1369 ret = vb2_queue_init(q);
1370 if (ret) {
622897da
MH
1371 v4l2_err(&vpfe_dev->v4l2_dev, "vb2_queue_init() failed\n");
1372 vb2_dma_contig_cleanup_ctx(vpfe_dev->pdev);
8bbb6568 1373 return ret;
622897da
MH
1374 }
1375
1376 fh->io_allowed = 1;
1377 video->io_usrs = 1;
1378 INIT_LIST_HEAD(&video->dma_queue);
1379 ret = vb2_reqbufs(&video->buffer_queue, req_buf);
1380
1381unlock_out:
1382 mutex_unlock(&video->lock);
1383 return ret;
1384}
1385
1386/*
1387 * vpfe_querybuf() - query buffers for exchange
1388 */
1389static int vpfe_querybuf(struct file *file, void *priv,
1390 struct v4l2_buffer *buf)
1391{
1392 struct vpfe_video_device *video = video_drvdata(file);
1393 struct vpfe_device *vpfe_dev = video->vpfe_dev;
1394
1395 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_querybuf\n");
1396
b369a87c
TF
1397 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1398 buf->type != V4L2_BUF_TYPE_VIDEO_OUTPUT) {
622897da
MH
1399 v4l2_err(&vpfe_dev->v4l2_dev, "Invalid buf type\n");
1400 return -EINVAL;
1401 }
1402
1403 if (video->memory != V4L2_MEMORY_MMAP) {
1404 v4l2_err(&vpfe_dev->v4l2_dev, "Invalid memory\n");
1405 return -EINVAL;
1406 }
1407
1408 /* Call vb2_querybuf to get information */
1409 return vb2_querybuf(&video->buffer_queue, buf);
1410}
1411
1412/*
1413 * vpfe_qbuf() - queue buffers for capture or processing
1414 */
1415static int vpfe_qbuf(struct file *file, void *priv,
1416 struct v4l2_buffer *p)
1417{
1418 struct vpfe_video_device *video = video_drvdata(file);
1419 struct vpfe_device *vpfe_dev = video->vpfe_dev;
1420 struct vpfe_fh *fh = file->private_data;
1421
1422 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_qbuf\n");
1423
b369a87c
TF
1424 if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1425 p->type != V4L2_BUF_TYPE_VIDEO_OUTPUT) {
622897da
MH
1426 v4l2_err(&vpfe_dev->v4l2_dev, "Invalid buf type\n");
1427 return -EINVAL;
1428 }
1429 /*
1430 * If this file handle is not allowed to do IO,
1431 * return error
1432 */
1433 if (!fh->io_allowed) {
1434 v4l2_err(&vpfe_dev->v4l2_dev, "fh->io_allowed\n");
1435 return -EACCES;
1436 }
1437
1438 return vb2_qbuf(&video->buffer_queue, p);
1439}
1440
1441/*
1442 * vpfe_dqbuf() - deque buffer which is done with processing
1443 */
1444static int vpfe_dqbuf(struct file *file, void *priv,
1445 struct v4l2_buffer *buf)
1446{
1447 struct vpfe_video_device *video = video_drvdata(file);
1448 struct vpfe_device *vpfe_dev = video->vpfe_dev;
1449
1450 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_dqbuf\n");
1451
b369a87c
TF
1452 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1453 buf->type != V4L2_BUF_TYPE_VIDEO_OUTPUT) {
622897da
MH
1454 v4l2_err(&vpfe_dev->v4l2_dev, "Invalid buf type\n");
1455 return -EINVAL;
1456 }
1457
1458 return vb2_dqbuf(&video->buffer_queue,
1459 buf, (file->f_flags & O_NONBLOCK));
1460}
1461
1462/*
4bd3bb71 1463 * vpfe_streamon() - start streaming
622897da
MH
1464 * @file: file pointer
1465 * @priv: void pointer
1466 * @buf_type: enum v4l2_buf_type
1467 *
1468 * queue buffer onto hardware for capture/processing and
1469 * start all the subdevs which are in media chain
1470 *
1471 * Return 0 on success, error code otherwise
1472 */
1473static int vpfe_streamon(struct file *file, void *priv,
1474 enum v4l2_buf_type buf_type)
1475{
1476 struct vpfe_video_device *video = video_drvdata(file);
1477 struct vpfe_device *vpfe_dev = video->vpfe_dev;
1478 struct vpfe_pipeline *pipe = &video->pipe;
1479 struct vpfe_fh *fh = file->private_data;
1480 struct vpfe_ext_subdev_info *sdinfo;
1481 int ret = -EINVAL;
1482
1483 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_streamon\n");
1484
b369a87c
TF
1485 if (buf_type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1486 buf_type != V4L2_BUF_TYPE_VIDEO_OUTPUT) {
622897da
MH
1487 v4l2_err(&vpfe_dev->v4l2_dev, "Invalid buf type\n");
1488 return ret;
1489 }
1490 /* If file handle is not allowed IO, return error */
1491 if (!fh->io_allowed) {
1492 v4l2_err(&vpfe_dev->v4l2_dev, "fh->io_allowed\n");
1493 return -EACCES;
1494 }
1495 sdinfo = video->current_ext_subdev;
1496 /* If buffer queue is empty, return error */
1497 if (list_empty(&video->buffer_queue.queued_list)) {
1498 v4l2_err(&vpfe_dev->v4l2_dev, "buffer queue is empty\n");
1499 return -EIO;
1500 }
1501 /* Validate the pipeline */
b369a87c 1502 if (buf_type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
622897da
MH
1503 ret = vpfe_video_validate_pipeline(pipe);
1504 if (ret < 0)
1505 return ret;
1506 }
1507 /* Call vb2_streamon to start streaming */
1508 return vb2_streamon(&video->buffer_queue, buf_type);
1509}
1510
1511/*
4bd3bb71 1512 * vpfe_streamoff() - stop streaming
622897da
MH
1513 * @file: file pointer
1514 * @priv: void pointer
1515 * @buf_type: enum v4l2_buf_type
1516 *
1517 * stop all the subdevs which are in media chain
1518 *
1519 * Return 0 on success, error code otherwise
1520 */
1521static int vpfe_streamoff(struct file *file, void *priv,
1522 enum v4l2_buf_type buf_type)
1523{
1524 struct vpfe_video_device *video = video_drvdata(file);
1525 struct vpfe_device *vpfe_dev = video->vpfe_dev;
1526 struct vpfe_fh *fh = file->private_data;
1527 int ret = 0;
1528
1529 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_streamoff\n");
1530
1531 if (buf_type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1532 buf_type != V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1533 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "Invalid buf type\n");
1534 return -EINVAL;
1535 }
1536
1537 /* If io is allowed for this file handle, return error */
1538 if (!fh->io_allowed) {
1539 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "fh->io_allowed\n");
1540 return -EACCES;
1541 }
1542
1543 /* If streaming is not started, return error */
1544 if (!video->started) {
1545 v4l2_err(&vpfe_dev->v4l2_dev, "device is not started\n");
1546 return -EINVAL;
1547 }
1548
8bbb6568
HV
1549 ret = mutex_lock_interruptible(&video->lock);
1550 if (ret)
1551 return ret;
622897da
MH
1552
1553 vpfe_stop_capture(video);
1554 ret = vb2_streamoff(&video->buffer_queue, buf_type);
1555 mutex_unlock(&video->lock);
1556
1557 return ret;
1558}
1559
1560/* vpfe capture ioctl operations */
1561static const struct v4l2_ioctl_ops vpfe_ioctl_ops = {
1562 .vidioc_querycap = vpfe_querycap,
1563 .vidioc_g_fmt_vid_cap = vpfe_g_fmt,
1564 .vidioc_s_fmt_vid_cap = vpfe_s_fmt,
1565 .vidioc_try_fmt_vid_cap = vpfe_try_fmt,
1566 .vidioc_enum_fmt_vid_cap = vpfe_enum_fmt,
1567 .vidioc_g_fmt_vid_out = vpfe_g_fmt,
1568 .vidioc_s_fmt_vid_out = vpfe_s_fmt,
1569 .vidioc_try_fmt_vid_out = vpfe_try_fmt,
1570 .vidioc_enum_fmt_vid_out = vpfe_enum_fmt,
1571 .vidioc_enum_input = vpfe_enum_input,
1572 .vidioc_g_input = vpfe_g_input,
1573 .vidioc_s_input = vpfe_s_input,
1574 .vidioc_querystd = vpfe_querystd,
1575 .vidioc_s_std = vpfe_s_std,
1576 .vidioc_g_std = vpfe_g_std,
1577 .vidioc_enum_dv_timings = vpfe_enum_dv_timings,
1578 .vidioc_query_dv_timings = vpfe_query_dv_timings,
1579 .vidioc_s_dv_timings = vpfe_s_dv_timings,
1580 .vidioc_g_dv_timings = vpfe_g_dv_timings,
1581 .vidioc_reqbufs = vpfe_reqbufs,
1582 .vidioc_querybuf = vpfe_querybuf,
1583 .vidioc_qbuf = vpfe_qbuf,
1584 .vidioc_dqbuf = vpfe_dqbuf,
1585 .vidioc_streamon = vpfe_streamon,
1586 .vidioc_streamoff = vpfe_streamoff,
1587};
1588
1589/* VPFE video init function */
1590int vpfe_video_init(struct vpfe_video_device *video, const char *name)
1591{
1592 const char *direction;
1593 int ret;
1594
1595 switch (video->type) {
1596 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1597 direction = "output";
1598 video->pad.flags = MEDIA_PAD_FL_SINK;
1599 video->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1600 break;
1601
1602 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
1603 direction = "input";
1604 video->pad.flags = MEDIA_PAD_FL_SOURCE;
1605 video->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
1606 break;
1607
1608 default:
1609 return -EINVAL;
1610 }
1611 /* Initialize field of video device */
1612 video->video_dev.release = video_device_release;
1613 video->video_dev.fops = &vpfe_fops;
1614 video->video_dev.ioctl_ops = &vpfe_ioctl_ops;
1615 video->video_dev.minor = -1;
1616 video->video_dev.tvnorms = 0;
1617 snprintf(video->video_dev.name, sizeof(video->video_dev.name),
1618 "DAVINCI VIDEO %s %s", name, direction);
1619
622897da
MH
1620 spin_lock_init(&video->irqlock);
1621 spin_lock_init(&video->dma_queue_lock);
1622 mutex_init(&video->lock);
ab22e77c 1623 ret = media_entity_pads_init(&video->video_dev.entity,
18095107 1624 1, &video->pad);
622897da
MH
1625 if (ret < 0)
1626 return ret;
1627
1628 video_set_drvdata(&video->video_dev, video);
1629
1630 return 0;
1631}
1632
1633/* vpfe video device register function */
1634int vpfe_video_register(struct vpfe_video_device *video,
1635 struct v4l2_device *vdev)
1636{
1637 int ret;
1638
1639 video->video_dev.v4l2_dev = vdev;
1640
1641 ret = video_register_device(&video->video_dev, VFL_TYPE_GRABBER, -1);
1642 if (ret < 0)
1643 pr_err("%s: could not register video device (%d)\n",
1644 __func__, ret);
1645 return ret;
1646}
1647
1648/* vpfe video device unregister function */
1649void vpfe_video_unregister(struct vpfe_video_device *video)
1650{
1651 if (video_is_registered(&video->video_dev)) {
622897da 1652 video_unregister_device(&video->video_dev);
7bb151b2 1653 media_entity_cleanup(&video->video_dev.entity);
622897da
MH
1654 }
1655}
This page took 0.531152 seconds and 5 git commands to generate.