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