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