aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--drivers/media/video/Kconfig2
-rw-r--r--drivers/media/video/vivi.c357
2 files changed, 200 insertions, 159 deletions
diff --git a/drivers/media/video/Kconfig b/drivers/media/video/Kconfig
index f86e407c2218..d40a8fc01bfd 100644
--- a/drivers/media/video/Kconfig
+++ b/drivers/media/video/Kconfig
@@ -549,7 +549,7 @@ config VIDEO_VIVI
549 depends on VIDEO_DEV && VIDEO_V4L2 && !SPARC32 && !SPARC64 549 depends on VIDEO_DEV && VIDEO_V4L2 && !SPARC32 && !SPARC64
550 depends on FRAMEBUFFER_CONSOLE || STI_CONSOLE 550 depends on FRAMEBUFFER_CONSOLE || STI_CONSOLE
551 select FONT_8x16 551 select FONT_8x16
552 select VIDEOBUF_VMALLOC 552 select VIDEOBUF2_VMALLOC
553 default n 553 default n
554 ---help--- 554 ---help---
555 Enables a virtual video driver. This device shows a color bar 555 Enables a virtual video driver. This device shows a color bar
diff --git a/drivers/media/video/vivi.c b/drivers/media/video/vivi.c
index c49c39386bd0..9ed3831b34bd 100644
--- a/drivers/media/video/vivi.c
+++ b/drivers/media/video/vivi.c
@@ -7,6 +7,9 @@
7 * John Sokol <sokol--a.t--videotechnology.com> 7 * John Sokol <sokol--a.t--videotechnology.com>
8 * http://v4l.videotechnology.com/ 8 * http://v4l.videotechnology.com/
9 * 9 *
10 * Conversion to videobuf2 by Pawel Osciak & Marek Szyprowski
11 * Copyright (c) 2010 Samsung Electronics
12 *
10 * This program is free software; you can redistribute it and/or modify 13 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the BSD Licence, GNU General Public License 14 * it under the terms of the BSD Licence, GNU General Public License
12 * as published by the Free Software Foundation; either version 2 of the 15 * as published by the Free Software Foundation; either version 2 of the
@@ -23,10 +26,8 @@
23#include <linux/mutex.h> 26#include <linux/mutex.h>
24#include <linux/videodev2.h> 27#include <linux/videodev2.h>
25#include <linux/kthread.h> 28#include <linux/kthread.h>
26#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 20)
27#include <linux/freezer.h> 29#include <linux/freezer.h>
28#endif 30#include <media/videobuf2-vmalloc.h>
29#include <media/videobuf-vmalloc.h>
30#include <media/v4l2-device.h> 31#include <media/v4l2-device.h>
31#include <media/v4l2-ioctl.h> 32#include <media/v4l2-ioctl.h>
32#include <media/v4l2-common.h> 33#include <media/v4l2-common.h>
@@ -42,7 +43,7 @@
42#define MAX_HEIGHT 1200 43#define MAX_HEIGHT 1200
43 44
44#define VIVI_MAJOR_VERSION 0 45#define VIVI_MAJOR_VERSION 0
45#define VIVI_MINOR_VERSION 7 46#define VIVI_MINOR_VERSION 8
46#define VIVI_RELEASE 0 47#define VIVI_RELEASE 0
47#define VIVI_VERSION \ 48#define VIVI_VERSION \
48 KERNEL_VERSION(VIVI_MAJOR_VERSION, VIVI_MINOR_VERSION, VIVI_RELEASE) 49 KERNEL_VERSION(VIVI_MAJOR_VERSION, VIVI_MINOR_VERSION, VIVI_RELEASE)
@@ -133,16 +134,11 @@ static struct vivi_fmt *get_format(struct v4l2_format *f)
133 return &formats[k]; 134 return &formats[k];
134} 135}
135 136
136struct sg_to_addr {
137 int pos;
138 struct scatterlist *sg;
139};
140
141/* buffer for one video frame */ 137/* buffer for one video frame */
142struct vivi_buffer { 138struct vivi_buffer {
143 /* common v4l buffer stuff -- must be first */ 139 /* common v4l buffer stuff -- must be first */
144 struct videobuf_buffer vb; 140 struct vb2_buffer vb;
145 141 struct list_head list;
146 struct vivi_fmt *fmt; 142 struct vivi_fmt *fmt;
147}; 143};
148 144
@@ -190,9 +186,11 @@ struct vivi_dev {
190 /* video capture */ 186 /* video capture */
191 struct vivi_fmt *fmt; 187 struct vivi_fmt *fmt;
192 unsigned int width, height; 188 unsigned int width, height;
193 struct videobuf_queue vb_vidq; 189 struct vb2_queue vb_vidq;
190 enum v4l2_field field;
191 unsigned int field_count;
194 192
195 unsigned long generating; 193 unsigned int open_count;
196 u8 bars[9][3]; 194 u8 bars[9][3];
197 u8 line[MAX_WIDTH * 4]; 195 u8 line[MAX_WIDTH * 4];
198}; 196};
@@ -443,10 +441,10 @@ static void gen_text(struct vivi_dev *dev, char *basep,
443 441
444static void vivi_fillbuff(struct vivi_dev *dev, struct vivi_buffer *buf) 442static void vivi_fillbuff(struct vivi_dev *dev, struct vivi_buffer *buf)
445{ 443{
446 int hmax = buf->vb.height; 444 int wmax = dev->width;
447 int wmax = buf->vb.width; 445 int hmax = dev->height;
448 struct timeval ts; 446 struct timeval ts;
449 void *vbuf = videobuf_to_vmalloc(&buf->vb); 447 void *vbuf = vb2_plane_vaddr(&buf->vb, 0);
450 unsigned ms; 448 unsigned ms;
451 char str[100]; 449 char str[100];
452 int h, line = 1; 450 int h, line = 1;
@@ -483,11 +481,11 @@ static void vivi_fillbuff(struct vivi_dev *dev, struct vivi_buffer *buf)
483 481
484 dev->mv_count += 2; 482 dev->mv_count += 2;
485 483
486 /* Advice that buffer was filled */ 484 buf->vb.v4l2_buf.field = dev->field;
487 buf->vb.field_count++; 485 dev->field_count++;
486 buf->vb.v4l2_buf.sequence = dev->field_count >> 1;
488 do_gettimeofday(&ts); 487 do_gettimeofday(&ts);
489 buf->vb.ts = ts; 488 buf->vb.v4l2_buf.timestamp = ts;
490 buf->vb.state = VIDEOBUF_DONE;
491} 489}
492 490
493static void vivi_thread_tick(struct vivi_dev *dev) 491static void vivi_thread_tick(struct vivi_dev *dev)
@@ -504,23 +502,17 @@ static void vivi_thread_tick(struct vivi_dev *dev)
504 goto unlock; 502 goto unlock;
505 } 503 }
506 504
507 buf = list_entry(dma_q->active.next, 505 buf = list_entry(dma_q->active.next, struct vivi_buffer, list);
508 struct vivi_buffer, vb.queue); 506 list_del(&buf->list);
509
510 /* Nobody is waiting on this buffer, return */
511 if (!waitqueue_active(&buf->vb.done))
512 goto unlock;
513 507
514 list_del(&buf->vb.queue); 508 do_gettimeofday(&buf->vb.v4l2_buf.timestamp);
515
516 do_gettimeofday(&buf->vb.ts);
517 509
518 /* Fill buffer */ 510 /* Fill buffer */
519 vivi_fillbuff(dev, buf); 511 vivi_fillbuff(dev, buf);
520 dprintk(dev, 1, "filled buffer %p\n", buf); 512 dprintk(dev, 1, "filled buffer %p\n", buf);
521 513
522 wake_up(&buf->vb.done); 514 vb2_buffer_done(&buf->vb, VB2_BUF_STATE_DONE);
523 dprintk(dev, 2, "[%p/%d] wakeup\n", buf, buf->vb. i); 515 dprintk(dev, 2, "[%p/%d] done\n", buf, buf->vb.v4l2_buf.index);
524unlock: 516unlock:
525 spin_unlock_irqrestore(&dev->slock, flags); 517 spin_unlock_irqrestore(&dev->slock, flags);
526} 518}
@@ -571,17 +563,12 @@ static int vivi_thread(void *data)
571 return 0; 563 return 0;
572} 564}
573 565
574static void vivi_start_generating(struct file *file) 566static int vivi_start_generating(struct vivi_dev *dev)
575{ 567{
576 struct vivi_dev *dev = video_drvdata(file);
577 struct vivi_dmaqueue *dma_q = &dev->vidq; 568 struct vivi_dmaqueue *dma_q = &dev->vidq;
578 569
579 dprintk(dev, 1, "%s\n", __func__); 570 dprintk(dev, 1, "%s\n", __func__);
580 571
581 if (test_and_set_bit(0, &dev->generating))
582 return;
583 file->private_data = dev;
584
585 /* Resets frame counters */ 572 /* Resets frame counters */
586 dev->ms = 0; 573 dev->ms = 0;
587 dev->mv_count = 0; 574 dev->mv_count = 0;
@@ -593,146 +580,200 @@ static void vivi_start_generating(struct file *file)
593 580
594 if (IS_ERR(dma_q->kthread)) { 581 if (IS_ERR(dma_q->kthread)) {
595 v4l2_err(&dev->v4l2_dev, "kernel_thread() failed\n"); 582 v4l2_err(&dev->v4l2_dev, "kernel_thread() failed\n");
596 clear_bit(0, &dev->generating); 583 return PTR_ERR(dma_q->kthread);
597 return;
598 } 584 }
599 /* Wakes thread */ 585 /* Wakes thread */
600 wake_up_interruptible(&dma_q->wq); 586 wake_up_interruptible(&dma_q->wq);
601 587
602 dprintk(dev, 1, "returning from %s\n", __func__); 588 dprintk(dev, 1, "returning from %s\n", __func__);
589 return 0;
603} 590}
604 591
605static void vivi_stop_generating(struct file *file) 592static void vivi_stop_generating(struct vivi_dev *dev)
606{ 593{
607 struct vivi_dev *dev = video_drvdata(file);
608 struct vivi_dmaqueue *dma_q = &dev->vidq; 594 struct vivi_dmaqueue *dma_q = &dev->vidq;
609 595
610 dprintk(dev, 1, "%s\n", __func__); 596 dprintk(dev, 1, "%s\n", __func__);
611 597
612 if (!file->private_data)
613 return;
614 if (!test_and_clear_bit(0, &dev->generating))
615 return;
616
617 /* shutdown control thread */ 598 /* shutdown control thread */
618 if (dma_q->kthread) { 599 if (dma_q->kthread) {
619 kthread_stop(dma_q->kthread); 600 kthread_stop(dma_q->kthread);
620 dma_q->kthread = NULL; 601 dma_q->kthread = NULL;
621 } 602 }
622 videobuf_stop(&dev->vb_vidq);
623 videobuf_mmap_free(&dev->vb_vidq);
624}
625 603
626static int vivi_is_generating(struct vivi_dev *dev) 604 /*
627{ 605 * Typical driver might need to wait here until dma engine stops.
628 return test_bit(0, &dev->generating); 606 * In this case we can abort imiedetly, so it's just a noop.
607 */
608
609 /* Release all active buffers */
610 while (!list_empty(&dma_q->active)) {
611 struct vivi_buffer *buf;
612 buf = list_entry(dma_q->active.next, struct vivi_buffer, list);
613 list_del(&buf->list);
614 vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
615 dprintk(dev, 2, "[%p/%d] done\n", buf, buf->vb.v4l2_buf.index);
616 }
629} 617}
630
631/* ------------------------------------------------------------------ 618/* ------------------------------------------------------------------
632 Videobuf operations 619 Videobuf operations
633 ------------------------------------------------------------------*/ 620 ------------------------------------------------------------------*/
634static int 621static int queue_setup(struct vb2_queue *vq, unsigned int *nbuffers,
635buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size) 622 unsigned int *nplanes, unsigned long sizes[],
623 void *alloc_ctxs[])
636{ 624{
637 struct vivi_dev *dev = vq->priv_data; 625 struct vivi_dev *dev = vb2_get_drv_priv(vq);
626 unsigned long size;
627
628 size = dev->width * dev->height * 2;
638 629
639 *size = dev->width * dev->height * 2; 630 if (0 == *nbuffers)
631 *nbuffers = 32;
640 632
641 if (0 == *count) 633 while (size * *nbuffers > vid_limit * 1024 * 1024)
642 *count = 32; 634 (*nbuffers)--;
643 635
644 while (*size * *count > vid_limit * 1024 * 1024) 636 *nplanes = 1;
645 (*count)--;
646 637
647 dprintk(dev, 1, "%s, count=%d, size=%d\n", __func__, 638 sizes[0] = size;
648 *count, *size); 639
640 /*
641 * videobuf2-vmalloc allocator is context-less so no need to set
642 * alloc_ctxs array.
643 */
644
645 dprintk(dev, 1, "%s, count=%d, size=%ld\n", __func__,
646 *nbuffers, size);
649 647
650 return 0; 648 return 0;
651} 649}
652 650
653static void free_buffer(struct videobuf_queue *vq, struct vivi_buffer *buf) 651static int buffer_init(struct vb2_buffer *vb)
654{ 652{
655 struct vivi_dev *dev = vq->priv_data; 653 struct vivi_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
656 654
657 dprintk(dev, 1, "%s, state: %i\n", __func__, buf->vb.state); 655 BUG_ON(NULL == dev->fmt);
658 656
659 videobuf_vmalloc_free(&buf->vb); 657 /*
660 dprintk(dev, 1, "free_buffer: freed\n"); 658 * This callback is called once per buffer, after its allocation.
661 buf->vb.state = VIDEOBUF_NEEDS_INIT; 659 *
660 * Vivi does not allow changing format during streaming, but it is
661 * possible to do so when streaming is paused (i.e. in streamoff state).
662 * Buffers however are not freed when going into streamoff and so
663 * buffer size verification has to be done in buffer_prepare, on each
664 * qbuf.
665 * It would be best to move verification code here to buf_init and
666 * s_fmt though.
667 */
668
669 return 0;
662} 670}
663 671
664static int 672static int buffer_prepare(struct vb2_buffer *vb)
665buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
666 enum v4l2_field field)
667{ 673{
668 struct vivi_dev *dev = vq->priv_data; 674 struct vivi_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
669 struct vivi_buffer *buf = container_of(vb, struct vivi_buffer, vb); 675 struct vivi_buffer *buf = container_of(vb, struct vivi_buffer, vb);
670 int rc; 676 unsigned long size;
671 677
672 dprintk(dev, 1, "%s, field=%d\n", __func__, field); 678 dprintk(dev, 1, "%s, field=%d\n", __func__, vb->v4l2_buf.field);
673 679
674 BUG_ON(NULL == dev->fmt); 680 BUG_ON(NULL == dev->fmt);
675 681
682 /*
683 * Theses properties only change when queue is idle, see s_fmt.
684 * The below checks should not be performed here, on each
685 * buffer_prepare (i.e. on each qbuf). Most of the code in this function
686 * should thus be moved to buffer_init and s_fmt.
687 */
676 if (dev->width < 48 || dev->width > MAX_WIDTH || 688 if (dev->width < 48 || dev->width > MAX_WIDTH ||
677 dev->height < 32 || dev->height > MAX_HEIGHT) 689 dev->height < 32 || dev->height > MAX_HEIGHT)
678 return -EINVAL; 690 return -EINVAL;
679 691
680 buf->vb.size = dev->width * dev->height * 2; 692 size = dev->width * dev->height * 2;
681 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size) 693 if (vb2_plane_size(vb, 0) < size) {
694 dprintk(dev, 1, "%s data will not fit into plane (%lu < %lu)\n",
695 __func__, vb2_plane_size(vb, 0), size);
682 return -EINVAL; 696 return -EINVAL;
697 }
698
699 vb2_set_plane_payload(&buf->vb, 0, size);
683 700
684 /* These properties only change when queue is idle, see s_fmt */ 701 buf->fmt = dev->fmt;
685 buf->fmt = dev->fmt;
686 buf->vb.width = dev->width;
687 buf->vb.height = dev->height;
688 buf->vb.field = field;
689 702
690 precalculate_bars(dev); 703 precalculate_bars(dev);
691 precalculate_line(dev); 704 precalculate_line(dev);
692 705
693 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) { 706 return 0;
694 rc = videobuf_iolock(vq, &buf->vb, NULL); 707}
695 if (rc < 0)
696 goto fail;
697 }
698 708
699 buf->vb.state = VIDEOBUF_PREPARED; 709static int buffer_finish(struct vb2_buffer *vb)
710{
711 struct vivi_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
712 dprintk(dev, 1, "%s\n", __func__);
700 return 0; 713 return 0;
714}
715
716static void buffer_cleanup(struct vb2_buffer *vb)
717{
718 struct vivi_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
719 dprintk(dev, 1, "%s\n", __func__);
701 720
702fail:
703 free_buffer(vq, buf);
704 return rc;
705} 721}
706 722
707static void 723static void buffer_queue(struct vb2_buffer *vb)
708buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
709{ 724{
710 struct vivi_dev *dev = vq->priv_data; 725 struct vivi_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
711 struct vivi_buffer *buf = container_of(vb, struct vivi_buffer, vb); 726 struct vivi_buffer *buf = container_of(vb, struct vivi_buffer, vb);
712 struct vivi_dmaqueue *vidq = &dev->vidq; 727 struct vivi_dmaqueue *vidq = &dev->vidq;
728 unsigned long flags = 0;
713 729
714 dprintk(dev, 1, "%s\n", __func__); 730 dprintk(dev, 1, "%s\n", __func__);
715 731
716 buf->vb.state = VIDEOBUF_QUEUED; 732 spin_lock_irqsave(&dev->slock, flags);
717 list_add_tail(&buf->vb.queue, &vidq->active); 733 list_add_tail(&buf->list, &vidq->active);
734 spin_unlock_irqrestore(&dev->slock, flags);
718} 735}
719 736
720static void buffer_release(struct videobuf_queue *vq, 737static int start_streaming(struct vb2_queue *vq)
721 struct videobuf_buffer *vb)
722{ 738{
723 struct vivi_dev *dev = vq->priv_data; 739 struct vivi_dev *dev = vb2_get_drv_priv(vq);
724 struct vivi_buffer *buf = container_of(vb, struct vivi_buffer, vb); 740 dprintk(dev, 1, "%s\n", __func__);
741 return vivi_start_generating(dev);
742}
725 743
744/* abort streaming and wait for last buffer */
745static int stop_streaming(struct vb2_queue *vq)
746{
747 struct vivi_dev *dev = vb2_get_drv_priv(vq);
726 dprintk(dev, 1, "%s\n", __func__); 748 dprintk(dev, 1, "%s\n", __func__);
749 vivi_stop_generating(dev);
750 return 0;
751}
752
753static void vivi_lock(struct vb2_queue *vq)
754{
755 struct vivi_dev *dev = vb2_get_drv_priv(vq);
756 mutex_lock(&dev->mutex);
757}
727 758
728 free_buffer(vq, buf); 759static void vivi_unlock(struct vb2_queue *vq)
760{
761 struct vivi_dev *dev = vb2_get_drv_priv(vq);
762 mutex_unlock(&dev->mutex);
729} 763}
730 764
731static struct videobuf_queue_ops vivi_video_qops = { 765
732 .buf_setup = buffer_setup, 766static struct vb2_ops vivi_video_qops = {
733 .buf_prepare = buffer_prepare, 767 .queue_setup = queue_setup,
734 .buf_queue = buffer_queue, 768 .buf_init = buffer_init,
735 .buf_release = buffer_release, 769 .buf_prepare = buffer_prepare,
770 .buf_finish = buffer_finish,
771 .buf_cleanup = buffer_cleanup,
772 .buf_queue = buffer_queue,
773 .start_streaming = start_streaming,
774 .stop_streaming = stop_streaming,
775 .wait_prepare = vivi_unlock,
776 .wait_finish = vivi_lock,
736}; 777};
737 778
738/* ------------------------------------------------------------------ 779/* ------------------------------------------------------------------
@@ -774,7 +815,7 @@ static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
774 815
775 f->fmt.pix.width = dev->width; 816 f->fmt.pix.width = dev->width;
776 f->fmt.pix.height = dev->height; 817 f->fmt.pix.height = dev->height;
777 f->fmt.pix.field = dev->vb_vidq.field; 818 f->fmt.pix.field = dev->field;
778 f->fmt.pix.pixelformat = dev->fmt->fourcc; 819 f->fmt.pix.pixelformat = dev->fmt->fourcc;
779 f->fmt.pix.bytesperline = 820 f->fmt.pix.bytesperline =
780 (f->fmt.pix.width * dev->fmt->depth) >> 3; 821 (f->fmt.pix.width * dev->fmt->depth) >> 3;
@@ -820,82 +861,60 @@ static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
820 struct v4l2_format *f) 861 struct v4l2_format *f)
821{ 862{
822 struct vivi_dev *dev = video_drvdata(file); 863 struct vivi_dev *dev = video_drvdata(file);
864 struct vb2_queue *q = &dev->vb_vidq;
823 865
824 int ret = vidioc_try_fmt_vid_cap(file, priv, f); 866 int ret = vidioc_try_fmt_vid_cap(file, priv, f);
825 if (ret < 0) 867 if (ret < 0)
826 return ret; 868 return ret;
827 869
828 if (vivi_is_generating(dev)) { 870 if (vb2_is_streaming(q)) {
829 dprintk(dev, 1, "%s device busy\n", __func__); 871 dprintk(dev, 1, "%s device busy\n", __func__);
830 ret = -EBUSY; 872 return -EBUSY;
831 goto out;
832 } 873 }
833 874
834 dev->fmt = get_format(f); 875 dev->fmt = get_format(f);
835 dev->width = f->fmt.pix.width; 876 dev->width = f->fmt.pix.width;
836 dev->height = f->fmt.pix.height; 877 dev->height = f->fmt.pix.height;
837 dev->vb_vidq.field = f->fmt.pix.field; 878 dev->field = f->fmt.pix.field;
838 ret = 0; 879
839out: 880 return 0;
840 return ret;
841} 881}
842 882
843static int vidioc_reqbufs(struct file *file, void *priv, 883static int vidioc_reqbufs(struct file *file, void *priv,
844 struct v4l2_requestbuffers *p) 884 struct v4l2_requestbuffers *p)
845{ 885{
846 struct vivi_dev *dev = video_drvdata(file); 886 struct vivi_dev *dev = video_drvdata(file);
847 887 return vb2_reqbufs(&dev->vb_vidq, p);
848 return videobuf_reqbufs(&dev->vb_vidq, p);
849} 888}
850 889
851static int vidioc_querybuf(struct file *file, void *priv, struct v4l2_buffer *p) 890static int vidioc_querybuf(struct file *file, void *priv, struct v4l2_buffer *p)
852{ 891{
853 struct vivi_dev *dev = video_drvdata(file); 892 struct vivi_dev *dev = video_drvdata(file);
854 893 return vb2_querybuf(&dev->vb_vidq, p);
855 return videobuf_querybuf(&dev->vb_vidq, p);
856} 894}
857 895
858static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *p) 896static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *p)
859{ 897{
860 struct vivi_dev *dev = video_drvdata(file); 898 struct vivi_dev *dev = video_drvdata(file);
861 899 return vb2_qbuf(&dev->vb_vidq, p);
862 return videobuf_qbuf(&dev->vb_vidq, p);
863} 900}
864 901
865static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p) 902static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
866{ 903{
867 struct vivi_dev *dev = video_drvdata(file); 904 struct vivi_dev *dev = video_drvdata(file);
868 905 return vb2_dqbuf(&dev->vb_vidq, p, file->f_flags & O_NONBLOCK);
869 return videobuf_dqbuf(&dev->vb_vidq, p,
870 file->f_flags & O_NONBLOCK);
871} 906}
872 907
873static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i) 908static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
874{ 909{
875 struct vivi_dev *dev = video_drvdata(file); 910 struct vivi_dev *dev = video_drvdata(file);
876 int ret; 911 return vb2_streamon(&dev->vb_vidq, i);
877
878 if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
879 return -EINVAL;
880 ret = videobuf_streamon(&dev->vb_vidq);
881 if (ret)
882 return ret;
883
884 vivi_start_generating(file);
885 return 0;
886} 912}
887 913
888static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i) 914static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
889{ 915{
890 struct vivi_dev *dev = video_drvdata(file); 916 struct vivi_dev *dev = video_drvdata(file);
891 int ret; 917 return vb2_streamoff(&dev->vb_vidq, i);
892
893 if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
894 return -EINVAL;
895 ret = videobuf_streamoff(&dev->vb_vidq);
896 if (!ret)
897 vivi_stop_generating(file);
898 return ret;
899} 918}
900 919
901static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *i) 920static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *i)
@@ -1018,26 +1037,33 @@ static int vidioc_s_ctrl(struct file *file, void *priv,
1018 File operations for the device 1037 File operations for the device
1019 ------------------------------------------------------------------*/ 1038 ------------------------------------------------------------------*/
1020 1039
1040static int vivi_open(struct file *file)
1041{
1042 struct vivi_dev *dev = video_drvdata(file);
1043
1044 dprintk(dev, 1, "%s, %p\n", __func__, file);
1045 dev->open_count++;
1046 return 0;
1047}
1048
1021static ssize_t 1049static ssize_t
1022vivi_read(struct file *file, char __user *data, size_t count, loff_t *ppos) 1050vivi_read(struct file *file, char __user *data, size_t count, loff_t *ppos)
1023{ 1051{
1024 struct vivi_dev *dev = video_drvdata(file); 1052 struct vivi_dev *dev = video_drvdata(file);
1025 1053
1026 vivi_start_generating(file); 1054 dprintk(dev, 1, "read called\n");
1027 return videobuf_read_stream(&dev->vb_vidq, data, count, ppos, 0, 1055 return vb2_read(&dev->vb_vidq, data, count, ppos,
1028 file->f_flags & O_NONBLOCK); 1056 file->f_flags & O_NONBLOCK);
1029} 1057}
1030 1058
1031static unsigned int 1059static unsigned int
1032vivi_poll(struct file *file, struct poll_table_struct *wait) 1060vivi_poll(struct file *file, struct poll_table_struct *wait)
1033{ 1061{
1034 struct vivi_dev *dev = video_drvdata(file); 1062 struct vivi_dev *dev = video_drvdata(file);
1035 struct videobuf_queue *q = &dev->vb_vidq; 1063 struct vb2_queue *q = &dev->vb_vidq;
1036 1064
1037 dprintk(dev, 1, "%s\n", __func__); 1065 dprintk(dev, 1, "%s\n", __func__);
1038 1066 return vb2_poll(q, file, wait);
1039 vivi_start_generating(file);
1040 return videobuf_poll_stream(file, q, wait);
1041} 1067}
1042 1068
1043static int vivi_close(struct file *file) 1069static int vivi_close(struct file *file)
@@ -1045,10 +1071,11 @@ static int vivi_close(struct file *file)
1045 struct video_device *vdev = video_devdata(file); 1071 struct video_device *vdev = video_devdata(file);
1046 struct vivi_dev *dev = video_drvdata(file); 1072 struct vivi_dev *dev = video_drvdata(file);
1047 1073
1048 vivi_stop_generating(file); 1074 dprintk(dev, 1, "close called (dev=%s), file %p\n",
1075 video_device_node_name(vdev), file);
1049 1076
1050 dprintk(dev, 1, "close called (dev=%s)\n", 1077 if (--dev->open_count == 0)
1051 video_device_node_name(vdev)); 1078 vb2_queue_release(&dev->vb_vidq);
1052 return 0; 1079 return 0;
1053} 1080}
1054 1081
@@ -1059,8 +1086,7 @@ static int vivi_mmap(struct file *file, struct vm_area_struct *vma)
1059 1086
1060 dprintk(dev, 1, "mmap called, vma=0x%08lx\n", (unsigned long)vma); 1087 dprintk(dev, 1, "mmap called, vma=0x%08lx\n", (unsigned long)vma);
1061 1088
1062 ret = videobuf_mmap_mapper(&dev->vb_vidq, vma); 1089 ret = vb2_mmap(&dev->vb_vidq, vma);
1063
1064 dprintk(dev, 1, "vma start=0x%08lx, size=%ld, ret=%d\n", 1090 dprintk(dev, 1, "vma start=0x%08lx, size=%ld, ret=%d\n",
1065 (unsigned long)vma->vm_start, 1091 (unsigned long)vma->vm_start,
1066 (unsigned long)vma->vm_end - (unsigned long)vma->vm_start, 1092 (unsigned long)vma->vm_end - (unsigned long)vma->vm_start,
@@ -1070,6 +1096,7 @@ static int vivi_mmap(struct file *file, struct vm_area_struct *vma)
1070 1096
1071static const struct v4l2_file_operations vivi_fops = { 1097static const struct v4l2_file_operations vivi_fops = {
1072 .owner = THIS_MODULE, 1098 .owner = THIS_MODULE,
1099 .open = vivi_open,
1073 .release = vivi_close, 1100 .release = vivi_close,
1074 .read = vivi_read, 1101 .read = vivi_read,
1075 .poll = vivi_poll, 1102 .poll = vivi_poll,
@@ -1136,6 +1163,7 @@ static int __init vivi_create_instance(int inst)
1136{ 1163{
1137 struct vivi_dev *dev; 1164 struct vivi_dev *dev;
1138 struct video_device *vfd; 1165 struct video_device *vfd;
1166 struct vb2_queue *q;
1139 int ret; 1167 int ret;
1140 1168
1141 dev = kzalloc(sizeof(*dev), GFP_KERNEL); 1169 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
@@ -1159,12 +1187,20 @@ static int __init vivi_create_instance(int inst)
1159 1187
1160 /* initialize locks */ 1188 /* initialize locks */
1161 spin_lock_init(&dev->slock); 1189 spin_lock_init(&dev->slock);
1162 mutex_init(&dev->mutex);
1163 1190
1164 videobuf_queue_vmalloc_init(&dev->vb_vidq, &vivi_video_qops, 1191 /* initialize queue */
1165 NULL, &dev->slock, V4L2_BUF_TYPE_VIDEO_CAPTURE, 1192 q = &dev->vb_vidq;
1166 V4L2_FIELD_INTERLACED, 1193 memset(q, 0, sizeof(dev->vb_vidq));
1167 sizeof(struct vivi_buffer), dev, &dev->mutex); 1194 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1195 q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_READ;
1196 q->drv_priv = dev;
1197 q->buf_struct_size = sizeof(struct vivi_buffer);
1198 q->ops = &vivi_video_qops;
1199 q->mem_ops = &vb2_vmalloc_memops;
1200
1201 vb2_queue_init(q);
1202
1203 mutex_init(&dev->mutex);
1168 1204
1169 /* init video dma queues */ 1205 /* init video dma queues */
1170 INIT_LIST_HEAD(&dev->vidq.active); 1206 INIT_LIST_HEAD(&dev->vidq.active);
@@ -1178,6 +1214,11 @@ static int __init vivi_create_instance(int inst)
1178 *vfd = vivi_template; 1214 *vfd = vivi_template;
1179 vfd->debug = debug; 1215 vfd->debug = debug;
1180 vfd->v4l2_dev = &dev->v4l2_dev; 1216 vfd->v4l2_dev = &dev->v4l2_dev;
1217
1218 /*
1219 * Provide a mutex to v4l2 core. It will be used to protect
1220 * all fops and v4l2 ioctls.
1221 */
1181 vfd->lock = &dev->mutex; 1222 vfd->lock = &dev->mutex;
1182 1223
1183 ret = video_register_device(vfd, VFL_TYPE_GRABBER, video_nr); 1224 ret = video_register_device(vfd, VFL_TYPE_GRABBER, video_nr);