aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/media/video/marvell-ccic
diff options
context:
space:
mode:
authorJonathan Corbet <corbet@lwn.net>2011-06-20 15:14:36 -0400
committerMauro Carvalho Chehab <mchehab@redhat.com>2011-07-27 16:53:11 -0400
commitb5210fd2c737dd178ff0401a0050dc62148fed60 (patch)
tree7482902c13724d7f6667ee1c250dba63b9c9893e /drivers/media/video/marvell-ccic
parent3751e32961f22e256de85ebd585597ce5875037a (diff)
[media] marvell-cam: convert to videobuf2
This is a basic, naive conversion to the videobuf2 infrastructure, removing a lot of code in the process. For now, we're using vmalloc, which is suboptimal, but it does match what the cafe driver did before. In the cafe case, it may have to stay that way just because memory is too tight to do direct streaming; mmp-camera will be able to do better. Signed-off-by: Jonathan Corbet <corbet@lwn.net> Signed-off-by: Mauro Carvalho Chehab <mchehab@redhat.com>
Diffstat (limited to 'drivers/media/video/marvell-ccic')
-rw-r--r--drivers/media/video/marvell-ccic/Kconfig2
-rw-r--r--drivers/media/video/marvell-ccic/mcam-core.c579
-rw-r--r--drivers/media/video/marvell-ccic/mcam-core.h26
3 files changed, 196 insertions, 411 deletions
diff --git a/drivers/media/video/marvell-ccic/Kconfig b/drivers/media/video/marvell-ccic/Kconfig
index b4f72605997c..eb535b10000c 100644
--- a/drivers/media/video/marvell-ccic/Kconfig
+++ b/drivers/media/video/marvell-ccic/Kconfig
@@ -2,6 +2,7 @@ config VIDEO_CAFE_CCIC
2 tristate "Marvell 88ALP01 (Cafe) CMOS Camera Controller support" 2 tristate "Marvell 88ALP01 (Cafe) CMOS Camera Controller support"
3 depends on PCI && I2C && VIDEO_V4L2 3 depends on PCI && I2C && VIDEO_V4L2
4 select VIDEO_OV7670 4 select VIDEO_OV7670
5 select VIDEOBUF2_VMALLOC
5 ---help--- 6 ---help---
6 This is a video4linux2 driver for the Marvell 88ALP01 integrated 7 This is a video4linux2 driver for the Marvell 88ALP01 integrated
7 CMOS camera controller. This is the controller found on first- 8 CMOS camera controller. This is the controller found on first-
@@ -12,6 +13,7 @@ config VIDEO_MMP_CAMERA
12 depends on ARCH_MMP && I2C && VIDEO_V4L2 13 depends on ARCH_MMP && I2C && VIDEO_V4L2
13 select VIDEO_OV7670 14 select VIDEO_OV7670
14 select I2C_GPIO 15 select I2C_GPIO
16 select VIDEOBUF2_VMALLOC
15 ---help--- 17 ---help---
16 This is a Video4Linux2 driver for the integrated camera 18 This is a Video4Linux2 driver for the integrated camera
17 controller found on Marvell Armada 610 application 19 controller found on Marvell Armada 610 application
diff --git a/drivers/media/video/marvell-ccic/mcam-core.c b/drivers/media/video/marvell-ccic/mcam-core.c
index 3e6a5e8b0cd1..055d843f99df 100644
--- a/drivers/media/video/marvell-ccic/mcam-core.c
+++ b/drivers/media/video/marvell-ccic/mcam-core.c
@@ -17,6 +17,7 @@
17#include <media/v4l2-ioctl.h> 17#include <media/v4l2-ioctl.h>
18#include <media/v4l2-chip-ident.h> 18#include <media/v4l2-chip-ident.h>
19#include <media/ov7670.h> 19#include <media/ov7670.h>
20#include <media/videobuf2-vmalloc.h>
20#include <linux/device.h> 21#include <linux/device.h>
21#include <linux/wait.h> 22#include <linux/wait.h>
22#include <linux/list.h> 23#include <linux/list.h>
@@ -149,7 +150,6 @@ static void mcam_reset_buffers(struct mcam_camera *cam)
149 cam->next_buf = -1; 150 cam->next_buf = -1;
150 for (i = 0; i < cam->nbufs; i++) 151 for (i = 0; i < cam->nbufs; i++)
151 clear_bit(i, &cam->flags); 152 clear_bit(i, &cam->flags);
152 cam->specframes = 0;
153} 153}
154 154
155static inline int mcam_needs_config(struct mcam_camera *cam) 155static inline int mcam_needs_config(struct mcam_camera *cam)
@@ -165,6 +165,21 @@ static void mcam_set_config_needed(struct mcam_camera *cam, int needed)
165 clear_bit(CF_CONFIG_NEEDED, &cam->flags); 165 clear_bit(CF_CONFIG_NEEDED, &cam->flags);
166} 166}
167 167
168/*
169 * Our buffer type for working with videobuf2. Note that the vb2
170 * developers have decreed that struct vb2_buffer must be at the
171 * beginning of this structure.
172 */
173struct mcam_vb_buffer {
174 struct vb2_buffer vb_buf;
175 struct list_head queue;
176};
177
178static inline struct mcam_vb_buffer *vb_to_mvb(struct vb2_buffer *vb)
179{
180 return container_of(vb, struct mcam_vb_buffer, vb_buf);
181}
182
168 183
169/* 184/*
170 * Debugging and related. 185 * Debugging and related.
@@ -339,9 +354,7 @@ static void mcam_ctlr_stop_dma(struct mcam_camera *cam)
339 spin_lock_irqsave(&cam->dev_lock, flags); 354 spin_lock_irqsave(&cam->dev_lock, flags);
340 mcam_ctlr_stop(cam); 355 mcam_ctlr_stop(cam);
341 spin_unlock_irqrestore(&cam->dev_lock, flags); 356 spin_unlock_irqrestore(&cam->dev_lock, flags);
342 mdelay(1); 357 msleep(10);
343 wait_event_timeout(cam->iowait,
344 !test_bit(CF_DMA_ACTIVE, &cam->flags), HZ);
345 if (test_bit(CF_DMA_ACTIVE, &cam->flags)) 358 if (test_bit(CF_DMA_ACTIVE, &cam->flags))
346 cam_err(cam, "Timeout waiting for DMA to end\n"); 359 cam_err(cam, "Timeout waiting for DMA to end\n");
347 /* This would be bad news - what now? */ 360 /* This would be bad news - what now? */
@@ -524,44 +537,11 @@ static void mcam_free_dma_bufs(struct mcam_camera *cam)
524 537
525 538
526 539
527
528
529/* ----------------------------------------------------------------------- */ 540/* ----------------------------------------------------------------------- */
530/* 541/*
531 * Here starts the V4L2 interface code. 542 * Here starts the V4L2 interface code.
532 */ 543 */
533 544
534/*
535 * Read an image from the device.
536 */
537static ssize_t mcam_deliver_buffer(struct mcam_camera *cam,
538 char __user *buffer, size_t len, loff_t *pos)
539{
540 int bufno;
541 unsigned long flags;
542
543 spin_lock_irqsave(&cam->dev_lock, flags);
544 if (cam->next_buf < 0) {
545 cam_err(cam, "deliver_buffer: No next buffer\n");
546 spin_unlock_irqrestore(&cam->dev_lock, flags);
547 return -EIO;
548 }
549 bufno = cam->next_buf;
550 clear_bit(bufno, &cam->flags);
551 if (++(cam->next_buf) >= cam->nbufs)
552 cam->next_buf = 0;
553 if (!test_bit(cam->next_buf, &cam->flags))
554 cam->next_buf = -1;
555 cam->specframes = 0;
556 spin_unlock_irqrestore(&cam->dev_lock, flags);
557
558 if (len > cam->pix_format.sizeimage)
559 len = cam->pix_format.sizeimage;
560 if (copy_to_user(buffer, cam->dma_bufs[bufno], len))
561 return -EFAULT;
562 (*pos) += len;
563 return len;
564}
565 545
566/* 546/*
567 * Get everything ready, and start grabbing frames. 547 * Get everything ready, and start grabbing frames.
@@ -598,75 +578,138 @@ static int mcam_read_setup(struct mcam_camera *cam, enum mcam_state state)
598 return 0; 578 return 0;
599} 579}
600 580
581/* ----------------------------------------------------------------------- */
582/*
583 * Videobuf2 interface code.
584 */
601 585
602static ssize_t mcam_v4l_read(struct file *filp, 586static int mcam_vb_queue_setup(struct vb2_queue *vq, unsigned int *nbufs,
603 char __user *buffer, size_t len, loff_t *pos) 587 unsigned int *num_planes, unsigned long sizes[],
588 void *alloc_ctxs[])
604{ 589{
605 struct mcam_camera *cam = filp->private_data; 590 struct mcam_camera *cam = vb2_get_drv_priv(vq);
606 int ret = 0; 591
592 sizes[0] = cam->pix_format.sizeimage;
593 *num_planes = 1; /* Someday we have to support planar formats... */
594 if (*nbufs < 2 || *nbufs > 32)
595 *nbufs = 6; /* semi-arbitrary numbers */
596 return 0;
597}
598
599static int mcam_vb_buf_init(struct vb2_buffer *vb)
600{
601 struct mcam_vb_buffer *mvb = vb_to_mvb(vb);
602
603 INIT_LIST_HEAD(&mvb->queue);
604 return 0;
605}
606
607static void mcam_vb_buf_queue(struct vb2_buffer *vb)
608{
609 struct mcam_vb_buffer *mvb = vb_to_mvb(vb);
610 struct mcam_camera *cam = vb2_get_drv_priv(vb->vb2_queue);
611 unsigned long flags;
612
613 spin_lock_irqsave(&cam->dev_lock, flags);
614 list_add(&cam->buffers, &mvb->queue);
615 spin_unlock_irqrestore(&cam->dev_lock, flags);
616}
617
618/*
619 * vb2 uses these to release the mutex when waiting in dqbuf. I'm
620 * not actually sure we need to do this (I'm not sure that vb2_dqbuf() needs
621 * to be called with the mutex held), but better safe than sorry.
622 */
623static void mcam_vb_wait_prepare(struct vb2_queue *vq)
624{
625 struct mcam_camera *cam = vb2_get_drv_priv(vq);
626
627 mutex_unlock(&cam->s_mutex);
628}
629
630static void mcam_vb_wait_finish(struct vb2_queue *vq)
631{
632 struct mcam_camera *cam = vb2_get_drv_priv(vq);
607 633
608 /*
609 * Perhaps we're in speculative read mode and already
610 * have data?
611 */
612 mutex_lock(&cam->s_mutex); 634 mutex_lock(&cam->s_mutex);
613 if (cam->state == S_SPECREAD) { 635}
614 if (cam->next_buf >= 0) {
615 ret = mcam_deliver_buffer(cam, buffer, len, pos);
616 if (ret != 0)
617 goto out_unlock;
618 }
619 } else if (cam->state == S_FLAKED || cam->state == S_NOTREADY) {
620 ret = -EIO;
621 goto out_unlock;
622 } else if (cam->state != S_IDLE) {
623 ret = -EBUSY;
624 goto out_unlock;
625 }
626 636
627 /* 637/*
628 * v4l2: multiple processes can open the device, but only 638 * These need to be called with the mutex held from vb2
629 * one gets to grab data from it. 639 */
630 */ 640static int mcam_vb_start_streaming(struct vb2_queue *vq)
631 if (cam->owner && cam->owner != filp) { 641{
632 ret = -EBUSY; 642 struct mcam_camera *cam = vb2_get_drv_priv(vq);
633 goto out_unlock; 643 int ret = -EINVAL;
634 }
635 cam->owner = filp;
636 644
637 /* 645 if (cam->state == S_IDLE) {
638 * Do setup if need be. 646 cam->sequence = 0;
639 */ 647 ret = mcam_read_setup(cam, S_STREAMING);
640 if (cam->state != S_SPECREAD) {
641 ret = mcam_read_setup(cam, S_SINGLEREAD);
642 if (ret)
643 goto out_unlock;
644 }
645 /*
646 * Wait for something to happen. This should probably
647 * be interruptible (FIXME).
648 */
649 wait_event_timeout(cam->iowait, cam->next_buf >= 0, HZ);
650 if (cam->next_buf < 0) {
651 cam_err(cam, "read() operation timed out\n");
652 mcam_ctlr_stop_dma(cam);
653 ret = -EIO;
654 goto out_unlock;
655 } 648 }
649 return ret;
650}
651
652static int mcam_vb_stop_streaming(struct vb2_queue *vq)
653{
654 struct mcam_camera *cam = vb2_get_drv_priv(vq);
655 unsigned long flags;
656
657 if (cam->state != S_STREAMING)
658 return -EINVAL;
659 mcam_ctlr_stop_dma(cam);
656 /* 660 /*
657 * Give them their data and we should be done. 661 * VB2 reclaims the buffers, so we need to forget
662 * about them.
658 */ 663 */
659 ret = mcam_deliver_buffer(cam, buffer, len, pos); 664 spin_lock_irqsave(&cam->dev_lock, flags);
660 665 INIT_LIST_HEAD(&cam->buffers);
661out_unlock: 666 spin_unlock_irqrestore(&cam->dev_lock, flags);
662 mutex_unlock(&cam->s_mutex); 667 return 0;
663 return ret;
664} 668}
665 669
666 670
671static const struct vb2_ops mcam_vb2_ops = {
672 .queue_setup = mcam_vb_queue_setup,
673 .buf_init = mcam_vb_buf_init,
674 .buf_queue = mcam_vb_buf_queue,
675 .start_streaming = mcam_vb_start_streaming,
676 .stop_streaming = mcam_vb_stop_streaming,
677 .wait_prepare = mcam_vb_wait_prepare,
678 .wait_finish = mcam_vb_wait_finish,
679};
667 680
681static int mcam_setup_vb2(struct mcam_camera *cam)
682{
683 struct vb2_queue *vq = &cam->vb_queue;
668 684
685 memset(vq, 0, sizeof(*vq));
686 vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
687 vq->io_modes = VB2_MMAP; /* Add userptr */
688 vq->drv_priv = cam;
689 vq->ops = &mcam_vb2_ops;
690 vq->mem_ops = &vb2_vmalloc_memops;
691 vq->buf_struct_size = sizeof(struct mcam_vb_buffer);
669 692
693 return vb2_queue_init(vq);
694}
695
696static void mcam_cleanup_vb2(struct mcam_camera *cam)
697{
698 vb2_queue_release(&cam->vb_queue);
699}
700
701static ssize_t mcam_v4l_read(struct file *filp,
702 char __user *buffer, size_t len, loff_t *pos)
703{
704 struct mcam_camera *cam = filp->private_data;
705 int ret;
706
707 mutex_lock(&cam->s_mutex);
708 ret = vb2_read(&cam->vb_queue, buffer, len, pos,
709 filp->f_flags & O_NONBLOCK);
710 mutex_unlock(&cam->s_mutex);
711 return ret;
712}
670 713
671 714
672 715
@@ -674,26 +717,15 @@ out_unlock:
674 * Streaming I/O support. 717 * Streaming I/O support.
675 */ 718 */
676 719
677
678
679static int mcam_vidioc_streamon(struct file *filp, void *priv, 720static int mcam_vidioc_streamon(struct file *filp, void *priv,
680 enum v4l2_buf_type type) 721 enum v4l2_buf_type type)
681{ 722{
682 struct mcam_camera *cam = filp->private_data; 723 struct mcam_camera *cam = filp->private_data;
683 int ret = -EINVAL; 724 int ret;
684 725
685 if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
686 goto out;
687 mutex_lock(&cam->s_mutex); 726 mutex_lock(&cam->s_mutex);
688 if (cam->state != S_IDLE || cam->n_sbufs == 0) 727 ret = vb2_streamon(&cam->vb_queue, type);
689 goto out_unlock;
690
691 cam->sequence = 0;
692 ret = mcam_read_setup(cam, S_STREAMING);
693
694out_unlock:
695 mutex_unlock(&cam->s_mutex); 728 mutex_unlock(&cam->s_mutex);
696out:
697 return ret; 729 return ret;
698} 730}
699 731
@@ -702,137 +734,23 @@ static int mcam_vidioc_streamoff(struct file *filp, void *priv,
702 enum v4l2_buf_type type) 734 enum v4l2_buf_type type)
703{ 735{
704 struct mcam_camera *cam = filp->private_data; 736 struct mcam_camera *cam = filp->private_data;
705 int ret = -EINVAL; 737 int ret;
706 738
707 if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
708 goto out;
709 mutex_lock(&cam->s_mutex); 739 mutex_lock(&cam->s_mutex);
710 if (cam->state != S_STREAMING) 740 ret = vb2_streamoff(&cam->vb_queue, type);
711 goto out_unlock;
712
713 mcam_ctlr_stop_dma(cam);
714 ret = 0;
715
716out_unlock:
717 mutex_unlock(&cam->s_mutex); 741 mutex_unlock(&cam->s_mutex);
718out:
719 return ret; 742 return ret;
720} 743}
721 744
722 745
723
724static int mcam_setup_siobuf(struct mcam_camera *cam, int index)
725{
726 struct mcam_sio_buffer *buf = cam->sb_bufs + index;
727
728 INIT_LIST_HEAD(&buf->list);
729 buf->v4lbuf.length = PAGE_ALIGN(cam->pix_format.sizeimage);
730 buf->buffer = vmalloc_user(buf->v4lbuf.length);
731 if (buf->buffer == NULL)
732 return -ENOMEM;
733 buf->mapcount = 0;
734 buf->cam = cam;
735
736 buf->v4lbuf.index = index;
737 buf->v4lbuf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
738 buf->v4lbuf.field = V4L2_FIELD_NONE;
739 buf->v4lbuf.memory = V4L2_MEMORY_MMAP;
740 /*
741 * Offset: must be 32-bit even on a 64-bit system. videobuf-dma-sg
742 * just uses the length times the index, but the spec warns
743 * against doing just that - vma merging problems. So we
744 * leave a gap between each pair of buffers.
745 */
746 buf->v4lbuf.m.offset = 2*index*buf->v4lbuf.length;
747 return 0;
748}
749
750static int mcam_free_sio_buffers(struct mcam_camera *cam)
751{
752 int i;
753
754 /*
755 * If any buffers are mapped, we cannot free them at all.
756 */
757 for (i = 0; i < cam->n_sbufs; i++)
758 if (cam->sb_bufs[i].mapcount > 0)
759 return -EBUSY;
760 /*
761 * OK, let's do it.
762 */
763 for (i = 0; i < cam->n_sbufs; i++)
764 vfree(cam->sb_bufs[i].buffer);
765 cam->n_sbufs = 0;
766 kfree(cam->sb_bufs);
767 cam->sb_bufs = NULL;
768 INIT_LIST_HEAD(&cam->sb_avail);
769 INIT_LIST_HEAD(&cam->sb_full);
770 return 0;
771}
772
773
774
775static int mcam_vidioc_reqbufs(struct file *filp, void *priv, 746static int mcam_vidioc_reqbufs(struct file *filp, void *priv,
776 struct v4l2_requestbuffers *req) 747 struct v4l2_requestbuffers *req)
777{ 748{
778 struct mcam_camera *cam = filp->private_data; 749 struct mcam_camera *cam = filp->private_data;
779 int ret = 0; /* Silence warning */ 750 int ret;
780 751
781 /*
782 * Make sure it's something we can do. User pointers could be
783 * implemented without great pain, but that's not been done yet.
784 */
785 if (req->memory != V4L2_MEMORY_MMAP)
786 return -EINVAL;
787 /*
788 * If they ask for zero buffers, they really want us to stop streaming
789 * (if it's happening) and free everything. Should we check owner?
790 */
791 mutex_lock(&cam->s_mutex); 752 mutex_lock(&cam->s_mutex);
792 if (req->count == 0) { 753 ret = vb2_reqbufs(&cam->vb_queue, req);
793 if (cam->state == S_STREAMING)
794 mcam_ctlr_stop_dma(cam);
795 ret = mcam_free_sio_buffers(cam);
796 goto out;
797 }
798 /*
799 * Device needs to be idle and working. We *could* try to do the
800 * right thing in S_SPECREAD by shutting things down, but it
801 * probably doesn't matter.
802 */
803 if (cam->state != S_IDLE || (cam->owner && cam->owner != filp)) {
804 ret = -EBUSY;
805 goto out;
806 }
807 cam->owner = filp;
808
809 if (req->count < min_buffers)
810 req->count = min_buffers;
811 else if (req->count > max_buffers)
812 req->count = max_buffers;
813 if (cam->n_sbufs > 0) {
814 ret = mcam_free_sio_buffers(cam);
815 if (ret)
816 goto out;
817 }
818
819 cam->sb_bufs = kzalloc(req->count*sizeof(struct mcam_sio_buffer),
820 GFP_KERNEL);
821 if (cam->sb_bufs == NULL) {
822 ret = -ENOMEM;
823 goto out;
824 }
825 for (cam->n_sbufs = 0; cam->n_sbufs < req->count; (cam->n_sbufs++)) {
826 ret = mcam_setup_siobuf(cam, cam->n_sbufs);
827 if (ret)
828 break;
829 }
830
831 if (cam->n_sbufs == 0) /* no luck at all - ret already set */
832 kfree(cam->sb_bufs);
833 req->count = cam->n_sbufs; /* In case of partial success */
834
835out:
836 mutex_unlock(&cam->s_mutex); 754 mutex_unlock(&cam->s_mutex);
837 return ret; 755 return ret;
838} 756}
@@ -842,14 +760,10 @@ static int mcam_vidioc_querybuf(struct file *filp, void *priv,
842 struct v4l2_buffer *buf) 760 struct v4l2_buffer *buf)
843{ 761{
844 struct mcam_camera *cam = filp->private_data; 762 struct mcam_camera *cam = filp->private_data;
845 int ret = -EINVAL; 763 int ret;
846 764
847 mutex_lock(&cam->s_mutex); 765 mutex_lock(&cam->s_mutex);
848 if (buf->index >= cam->n_sbufs) 766 ret = vb2_querybuf(&cam->vb_queue, buf);
849 goto out;
850 *buf = cam->sb_bufs[buf->index].v4lbuf;
851 ret = 0;
852out:
853 mutex_unlock(&cam->s_mutex); 767 mutex_unlock(&cam->s_mutex);
854 return ret; 768 return ret;
855} 769}
@@ -858,29 +772,10 @@ static int mcam_vidioc_qbuf(struct file *filp, void *priv,
858 struct v4l2_buffer *buf) 772 struct v4l2_buffer *buf)
859{ 773{
860 struct mcam_camera *cam = filp->private_data; 774 struct mcam_camera *cam = filp->private_data;
861 struct mcam_sio_buffer *sbuf; 775 int ret;
862 int ret = -EINVAL;
863 unsigned long flags;
864 776
865 mutex_lock(&cam->s_mutex); 777 mutex_lock(&cam->s_mutex);
866 if (buf->index >= cam->n_sbufs) 778 ret = vb2_qbuf(&cam->vb_queue, buf);
867 goto out;
868 sbuf = cam->sb_bufs + buf->index;
869 if (sbuf->v4lbuf.flags & V4L2_BUF_FLAG_QUEUED) {
870 ret = 0; /* Already queued?? */
871 goto out;
872 }
873 if (sbuf->v4lbuf.flags & V4L2_BUF_FLAG_DONE) {
874 /* Spec doesn't say anything, seems appropriate tho */
875 ret = -EBUSY;
876 goto out;
877 }
878 sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_QUEUED;
879 spin_lock_irqsave(&cam->dev_lock, flags);
880 list_add(&sbuf->list, &cam->sb_avail);
881 spin_unlock_irqrestore(&cam->dev_lock, flags);
882 ret = 0;
883out:
884 mutex_unlock(&cam->s_mutex); 779 mutex_unlock(&cam->s_mutex);
885 return ret; 780 return ret;
886} 781}
@@ -889,111 +784,22 @@ static int mcam_vidioc_dqbuf(struct file *filp, void *priv,
889 struct v4l2_buffer *buf) 784 struct v4l2_buffer *buf)
890{ 785{
891 struct mcam_camera *cam = filp->private_data; 786 struct mcam_camera *cam = filp->private_data;
892 struct mcam_sio_buffer *sbuf; 787 int ret;
893 int ret = -EINVAL;
894 unsigned long flags;
895 788
896 mutex_lock(&cam->s_mutex); 789 mutex_lock(&cam->s_mutex);
897 if (cam->state != S_STREAMING) 790 ret = vb2_dqbuf(&cam->vb_queue, buf, filp->f_flags & O_NONBLOCK);
898 goto out_unlock;
899 if (list_empty(&cam->sb_full) && filp->f_flags & O_NONBLOCK) {
900 ret = -EAGAIN;
901 goto out_unlock;
902 }
903
904 while (list_empty(&cam->sb_full) && cam->state == S_STREAMING) {
905 mutex_unlock(&cam->s_mutex);
906 if (wait_event_interruptible(cam->iowait,
907 !list_empty(&cam->sb_full))) {
908 ret = -ERESTARTSYS;
909 goto out;
910 }
911 mutex_lock(&cam->s_mutex);
912 }
913
914 if (cam->state != S_STREAMING)
915 ret = -EINTR;
916 else {
917 spin_lock_irqsave(&cam->dev_lock, flags);
918 /* Should probably recheck !list_empty() here */
919 sbuf = list_entry(cam->sb_full.next,
920 struct mcam_sio_buffer, list);
921 list_del_init(&sbuf->list);
922 spin_unlock_irqrestore(&cam->dev_lock, flags);
923 sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_DONE;
924 *buf = sbuf->v4lbuf;
925 ret = 0;
926 }
927
928out_unlock:
929 mutex_unlock(&cam->s_mutex); 791 mutex_unlock(&cam->s_mutex);
930out:
931 return ret; 792 return ret;
932} 793}
933 794
934 795
935
936static void mcam_v4l_vm_open(struct vm_area_struct *vma)
937{
938 struct mcam_sio_buffer *sbuf = vma->vm_private_data;
939 /*
940 * Locking: done under mmap_sem, so we don't need to
941 * go back to the camera lock here.
942 */
943 sbuf->mapcount++;
944}
945
946
947static void mcam_v4l_vm_close(struct vm_area_struct *vma)
948{
949 struct mcam_sio_buffer *sbuf = vma->vm_private_data;
950
951 mutex_lock(&sbuf->cam->s_mutex);
952 sbuf->mapcount--;
953 /* Docs say we should stop I/O too... */
954 if (sbuf->mapcount == 0)
955 sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_MAPPED;
956 mutex_unlock(&sbuf->cam->s_mutex);
957}
958
959static const struct vm_operations_struct mcam_v4l_vm_ops = {
960 .open = mcam_v4l_vm_open,
961 .close = mcam_v4l_vm_close
962};
963
964
965static int mcam_v4l_mmap(struct file *filp, struct vm_area_struct *vma) 796static int mcam_v4l_mmap(struct file *filp, struct vm_area_struct *vma)
966{ 797{
967 struct mcam_camera *cam = filp->private_data; 798 struct mcam_camera *cam = filp->private_data;
968 unsigned long offset = vma->vm_pgoff << PAGE_SHIFT; 799 int ret;
969 int ret = -EINVAL;
970 int i;
971 struct mcam_sio_buffer *sbuf = NULL;
972 800
973 if (!(vma->vm_flags & VM_WRITE) || !(vma->vm_flags & VM_SHARED))
974 return -EINVAL;
975 /*
976 * Find the buffer they are looking for.
977 */
978 mutex_lock(&cam->s_mutex); 801 mutex_lock(&cam->s_mutex);
979 for (i = 0; i < cam->n_sbufs; i++) 802 ret = vb2_mmap(&cam->vb_queue, vma);
980 if (cam->sb_bufs[i].v4lbuf.m.offset == offset) {
981 sbuf = cam->sb_bufs + i;
982 break;
983 }
984 if (sbuf == NULL)
985 goto out;
986
987 ret = remap_vmalloc_range(vma, sbuf->buffer, 0);
988 if (ret)
989 goto out;
990 vma->vm_flags |= VM_DONTEXPAND;
991 vma->vm_private_data = sbuf;
992 vma->vm_ops = &mcam_v4l_vm_ops;
993 sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_MAPPED;
994 mcam_v4l_vm_open(vma);
995 ret = 0;
996out:
997 mutex_unlock(&cam->s_mutex); 803 mutex_unlock(&cam->s_mutex);
998 return ret; 804 return ret;
999} 805}
@@ -1003,19 +809,23 @@ out:
1003static int mcam_v4l_open(struct file *filp) 809static int mcam_v4l_open(struct file *filp)
1004{ 810{
1005 struct mcam_camera *cam = video_drvdata(filp); 811 struct mcam_camera *cam = video_drvdata(filp);
812 int ret = 0;
1006 813
1007 filp->private_data = cam; 814 filp->private_data = cam;
1008 815
1009 mutex_lock(&cam->s_mutex); 816 mutex_lock(&cam->s_mutex);
1010 if (cam->users == 0) { 817 if (cam->users == 0) {
818 ret = mcam_setup_vb2(cam);
819 if (ret)
820 goto out;
1011 mcam_ctlr_power_up(cam); 821 mcam_ctlr_power_up(cam);
1012 __mcam_cam_reset(cam); 822 __mcam_cam_reset(cam);
1013 mcam_set_config_needed(cam, 1); 823 mcam_set_config_needed(cam, 1);
1014 /* FIXME make sure this is complete */
1015 } 824 }
1016 (cam->users)++; 825 (cam->users)++;
826out:
1017 mutex_unlock(&cam->s_mutex); 827 mutex_unlock(&cam->s_mutex);
1018 return 0; 828 return ret;
1019} 829}
1020 830
1021 831
@@ -1027,10 +837,10 @@ static int mcam_v4l_release(struct file *filp)
1027 (cam->users)--; 837 (cam->users)--;
1028 if (filp == cam->owner) { 838 if (filp == cam->owner) {
1029 mcam_ctlr_stop_dma(cam); 839 mcam_ctlr_stop_dma(cam);
1030 mcam_free_sio_buffers(cam);
1031 cam->owner = NULL; 840 cam->owner = NULL;
1032 } 841 }
1033 if (cam->users == 0) { 842 if (cam->users == 0) {
843 mcam_cleanup_vb2(cam);
1034 mcam_ctlr_power_down(cam); 844 mcam_ctlr_power_down(cam);
1035 if (alloc_bufs_at_read) 845 if (alloc_bufs_at_read)
1036 mcam_free_dma_bufs(cam); 846 mcam_free_dma_bufs(cam);
@@ -1045,11 +855,12 @@ static unsigned int mcam_v4l_poll(struct file *filp,
1045 struct poll_table_struct *pt) 855 struct poll_table_struct *pt)
1046{ 856{
1047 struct mcam_camera *cam = filp->private_data; 857 struct mcam_camera *cam = filp->private_data;
858 int ret;
1048 859
1049 poll_wait(filp, &cam->iowait, pt); 860 mutex_lock(&cam->s_mutex);
1050 if (cam->next_buf >= 0) 861 ret = vb2_poll(&cam->vb_queue, filp, pt);
1051 return POLLIN | POLLRDNORM; 862 mutex_unlock(&cam->s_mutex);
1052 return 0; 863 return ret;
1053} 864}
1054 865
1055 866
@@ -1093,9 +904,6 @@ static int mcam_vidioc_s_ctrl(struct file *filp, void *priv,
1093} 904}
1094 905
1095 906
1096
1097
1098
1099static int mcam_vidioc_querycap(struct file *file, void *priv, 907static int mcam_vidioc_querycap(struct file *file, void *priv,
1100 struct v4l2_capability *cap) 908 struct v4l2_capability *cap)
1101{ 909{
@@ -1166,7 +974,7 @@ static int mcam_vidioc_s_fmt_vid_cap(struct file *filp, void *priv,
1166 * Can't do anything if the device is not idle 974 * Can't do anything if the device is not idle
1167 * Also can't if there are streaming buffers in place. 975 * Also can't if there are streaming buffers in place.
1168 */ 976 */
1169 if (cam->state != S_IDLE || cam->n_sbufs > 0) 977 if (cam->state != S_IDLE || cam->vb_queue.num_buffers > 0)
1170 return -EBUSY; 978 return -EBUSY;
1171 979
1172 f = mcam_find_format(fmt->fmt.pix.pixelformat); 980 f = mcam_find_format(fmt->fmt.pix.pixelformat);
@@ -1416,39 +1224,39 @@ static void mcam_frame_tasklet(unsigned long data)
1416 struct mcam_camera *cam = (struct mcam_camera *) data; 1224 struct mcam_camera *cam = (struct mcam_camera *) data;
1417 int i; 1225 int i;
1418 unsigned long flags; 1226 unsigned long flags;
1419 struct mcam_sio_buffer *sbuf; 1227 struct mcam_vb_buffer *buf;
1420 1228
1421 spin_lock_irqsave(&cam->dev_lock, flags); 1229 spin_lock_irqsave(&cam->dev_lock, flags);
1422 for (i = 0; i < cam->nbufs; i++) { 1230 for (i = 0; i < cam->nbufs; i++) {
1423 int bufno = cam->next_buf; 1231 int bufno = cam->next_buf;
1424 if (bufno < 0) { /* "will never happen" */ 1232
1425 cam_err(cam, "No valid bufs in tasklet!\n"); 1233 if (cam->state != S_STREAMING || bufno < 0)
1426 break; 1234 break; /* I/O got stopped */
1427 }
1428 if (++(cam->next_buf) >= cam->nbufs) 1235 if (++(cam->next_buf) >= cam->nbufs)
1429 cam->next_buf = 0; 1236 cam->next_buf = 0;
1430 if (!test_bit(bufno, &cam->flags)) 1237 if (!test_bit(bufno, &cam->flags))
1431 continue; 1238 continue;
1432 if (list_empty(&cam->sb_avail)) 1239 if (list_empty(&cam->buffers))
1433 break; /* Leave it valid, hope for better later */ 1240 break; /* Leave it valid, hope for better later */
1434 clear_bit(bufno, &cam->flags); 1241 clear_bit(bufno, &cam->flags);
1435 sbuf = list_entry(cam->sb_avail.next, 1242 buf = list_first_entry(&cam->buffers, struct mcam_vb_buffer,
1436 struct mcam_sio_buffer, list); 1243 queue);
1244 list_del_init(&buf->queue);
1437 /* 1245 /*
1438 * Drop the lock during the big copy. This *should* be safe... 1246 * Drop the lock during the big copy. This *should* be safe...
1439 */ 1247 */
1440 spin_unlock_irqrestore(&cam->dev_lock, flags); 1248 spin_unlock_irqrestore(&cam->dev_lock, flags);
1441 memcpy(sbuf->buffer, cam->dma_bufs[bufno], 1249 memcpy(vb2_plane_vaddr(&buf->vb_buf, 0), cam->dma_bufs[bufno],
1442 cam->pix_format.sizeimage); 1250 cam->pix_format.sizeimage);
1443 sbuf->v4lbuf.bytesused = cam->pix_format.sizeimage; 1251 buf->vb_buf.v4l2_buf.bytesused = cam->pix_format.sizeimage;
1444 sbuf->v4lbuf.sequence = cam->buf_seq[bufno]; 1252 buf->vb_buf.v4l2_buf.sequence = cam->buf_seq[bufno];
1445 sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_QUEUED; 1253 buf->vb_buf.v4l2_buf.flags &= ~V4L2_BUF_FLAG_QUEUED;
1446 sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_DONE; 1254 buf->vb_buf.v4l2_buf.flags |= V4L2_BUF_FLAG_DONE;
1255 vb2_set_plane_payload(&buf->vb_buf, 0,
1256 cam->pix_format.sizeimage);
1257 vb2_buffer_done(&buf->vb_buf, VB2_BUF_STATE_DONE);
1447 spin_lock_irqsave(&cam->dev_lock, flags); 1258 spin_lock_irqsave(&cam->dev_lock, flags);
1448 list_move_tail(&sbuf->list, &cam->sb_full);
1449 } 1259 }
1450 if (!list_empty(&cam->sb_full))
1451 wake_up(&cam->iowait);
1452 spin_unlock_irqrestore(&cam->dev_lock, flags); 1260 spin_unlock_irqrestore(&cam->dev_lock, flags);
1453} 1261}
1454 1262
@@ -1469,27 +1277,6 @@ static void mcam_frame_complete(struct mcam_camera *cam, int frame)
1469 1277
1470 switch (cam->state) { 1278 switch (cam->state) {
1471 /* 1279 /*
1472 * If in single read mode, try going speculative.
1473 */
1474 case S_SINGLEREAD:
1475 cam->state = S_SPECREAD;
1476 cam->specframes = 0;
1477 wake_up(&cam->iowait);
1478 break;
1479
1480 /*
1481 * If we are already doing speculative reads, and nobody is
1482 * reading them, just stop.
1483 */
1484 case S_SPECREAD:
1485 if (++(cam->specframes) >= cam->nbufs) {
1486 mcam_ctlr_stop(cam);
1487 mcam_ctlr_irq_disable(cam);
1488 cam->state = S_IDLE;
1489 }
1490 wake_up(&cam->iowait);
1491 break;
1492 /*
1493 * For the streaming case, we defer the real work to the 1280 * For the streaming case, we defer the real work to the
1494 * camera tasklet. 1281 * camera tasklet.
1495 * 1282 *
@@ -1570,12 +1357,10 @@ int mccic_register(struct mcam_camera *cam)
1570 mutex_init(&cam->s_mutex); 1357 mutex_init(&cam->s_mutex);
1571 cam->state = S_NOTREADY; 1358 cam->state = S_NOTREADY;
1572 mcam_set_config_needed(cam, 1); 1359 mcam_set_config_needed(cam, 1);
1573 init_waitqueue_head(&cam->iowait);
1574 cam->pix_format = mcam_def_pix_format; 1360 cam->pix_format = mcam_def_pix_format;
1575 cam->mbus_code = mcam_def_mbus_code; 1361 cam->mbus_code = mcam_def_mbus_code;
1576 INIT_LIST_HEAD(&cam->dev_list); 1362 INIT_LIST_HEAD(&cam->dev_list);
1577 INIT_LIST_HEAD(&cam->sb_avail); 1363 INIT_LIST_HEAD(&cam->buffers);
1578 INIT_LIST_HEAD(&cam->sb_full);
1579 tasklet_init(&cam->s_tasklet, mcam_frame_tasklet, (unsigned long) cam); 1364 tasklet_init(&cam->s_tasklet, mcam_frame_tasklet, (unsigned long) cam);
1580 1365
1581 mcam_ctlr_init(cam); 1366 mcam_ctlr_init(cam);
@@ -1638,10 +1423,8 @@ void mccic_shutdown(struct mcam_camera *cam)
1638 cam_warn(cam, "Removing a device with users!\n"); 1423 cam_warn(cam, "Removing a device with users!\n");
1639 mcam_ctlr_power_down(cam); 1424 mcam_ctlr_power_down(cam);
1640 } 1425 }
1426 vb2_queue_release(&cam->vb_queue);
1641 mcam_free_dma_bufs(cam); 1427 mcam_free_dma_bufs(cam);
1642 if (cam->n_sbufs > 0)
1643 /* What if they are still mapped? Shouldn't be, but... */
1644 mcam_free_sio_buffers(cam);
1645 video_unregister_device(&cam->vdev); 1428 video_unregister_device(&cam->vdev);
1646 v4l2_device_unregister(&cam->v4l2_dev); 1429 v4l2_device_unregister(&cam->v4l2_dev);
1647} 1430}
@@ -1674,9 +1457,7 @@ int mccic_resume(struct mcam_camera *cam)
1674 mutex_unlock(&cam->s_mutex); 1457 mutex_unlock(&cam->s_mutex);
1675 1458
1676 set_bit(CF_CONFIG_NEEDED, &cam->flags); 1459 set_bit(CF_CONFIG_NEEDED, &cam->flags);
1677 if (cam->state == S_SPECREAD) 1460 if (cam->state == S_STREAMING)
1678 cam->state = S_IDLE; /* Don't bother restarting */
1679 else if (cam->state == S_SINGLEREAD || cam->state == S_STREAMING)
1680 ret = mcam_read_setup(cam, cam->state); 1461 ret = mcam_read_setup(cam, cam->state);
1681 return ret; 1462 return ret;
1682} 1463}
diff --git a/drivers/media/video/marvell-ccic/mcam-core.h b/drivers/media/video/marvell-ccic/mcam-core.h
index 5effa8203412..f40450cf72a0 100644
--- a/drivers/media/video/marvell-ccic/mcam-core.h
+++ b/drivers/media/video/marvell-ccic/mcam-core.h
@@ -3,6 +3,13 @@
3 * 3 *
4 * Copyright 2011 Jonathan Corbet corbet@lwn.net 4 * Copyright 2011 Jonathan Corbet corbet@lwn.net
5 */ 5 */
6#ifndef _MCAM_CORE_H
7#define _MCAM_CORE_H
8
9#include <linux/list.h>
10#include <media/v4l2-common.h>
11#include <media/v4l2-dev.h>
12#include <media/videobuf2-core.h>
6 13
7/* 14/*
8 * Tracking of streaming I/O buffers. 15 * Tracking of streaming I/O buffers.
@@ -20,8 +27,6 @@ enum mcam_state {
20 S_NOTREADY, /* Not yet initialized */ 27 S_NOTREADY, /* Not yet initialized */
21 S_IDLE, /* Just hanging around */ 28 S_IDLE, /* Just hanging around */
22 S_FLAKED, /* Some sort of problem */ 29 S_FLAKED, /* Some sort of problem */
23 S_SINGLEREAD, /* In read() */
24 S_SPECREAD, /* Speculative read (for future read()) */
25 S_STREAMING /* Streaming data */ 30 S_STREAMING /* Streaming data */
26}; 31};
27#define MAX_DMA_BUFS 3 32#define MAX_DMA_BUFS 3
@@ -70,21 +75,19 @@ struct mcam_camera {
70 75
71 struct list_head dev_list; /* link to other devices */ 76 struct list_head dev_list; /* link to other devices */
72 77
78 /* Videobuf2 stuff */
79 struct vb2_queue vb_queue;
80 struct list_head buffers; /* Available frames */
81
73 /* DMA buffers */ 82 /* DMA buffers */
74 unsigned int nbufs; /* How many are alloc'd */ 83 unsigned int nbufs; /* How many are alloc'd */
75 int next_buf; /* Next to consume (dev_lock) */ 84 int next_buf; /* Next to consume (dev_lock) */
76 unsigned int dma_buf_size; /* allocated size */ 85 unsigned int dma_buf_size; /* allocated size */
77 void *dma_bufs[MAX_DMA_BUFS]; /* Internal buffer addresses */ 86 void *dma_bufs[MAX_DMA_BUFS]; /* Internal buffer addresses */
78 dma_addr_t dma_handles[MAX_DMA_BUFS]; /* Buffer bus addresses */ 87 dma_addr_t dma_handles[MAX_DMA_BUFS]; /* Buffer bus addresses */
79 unsigned int specframes; /* Unconsumed spec frames (dev_lock) */
80 unsigned int sequence; /* Frame sequence number */ 88 unsigned int sequence; /* Frame sequence number */
81 unsigned int buf_seq[MAX_DMA_BUFS]; /* Sequence for individual buffers */ 89 unsigned int buf_seq[MAX_DMA_BUFS]; /* Sequence for individual bufs */
82 90
83 /* Streaming buffers */
84 unsigned int n_sbufs; /* How many we have */
85 struct mcam_sio_buffer *sb_bufs; /* The array of housekeeping structs */
86 struct list_head sb_avail; /* Available for data (we own) (dev_lock) */
87 struct list_head sb_full; /* With data (user space owns) (dev_lock) */
88 struct tasklet_struct s_tasklet; 91 struct tasklet_struct s_tasklet;
89 92
90 /* Current operating parameters */ 93 /* Current operating parameters */
@@ -94,9 +97,6 @@ struct mcam_camera {
94 97
95 /* Locks */ 98 /* Locks */
96 struct mutex s_mutex; /* Access to this structure */ 99 struct mutex s_mutex; /* Access to this structure */
97
98 /* Misc */
99 wait_queue_head_t iowait; /* Waiting on frame data */
100}; 100};
101 101
102 102
@@ -257,3 +257,5 @@ int mccic_resume(struct mcam_camera *cam);
257 */ 257 */
258#define VGA_WIDTH 640 258#define VGA_WIDTH 640
259#define VGA_HEIGHT 480 259#define VGA_HEIGHT 480
260
261#endif /* _MCAM_CORE_H */