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