forked from ~ljy/RK356X_SDK_RELEASE

hc
2024-01-04 1543e317f1da31b75942316931e8f491a8920811
kernel/drivers/media/platform/via-camera.c
....@@ -1,8 +1,8 @@
1
+// SPDX-License-Identifier: GPL-2.0-only
12 /*
23 * Driver for the VIA Chrome integrated camera controller.
34 *
45 * Copyright 2009,2010 Jonathan Corbet <corbet@lwn.net>
5
- * Distributable under the terms of the GNU General Public License, version 2
66 *
77 * This work was supported by the One Laptop Per Child project
88 */
....@@ -18,9 +18,10 @@
1818 #include <media/v4l2-device.h>
1919 #include <media/v4l2-ioctl.h>
2020 #include <media/v4l2-ctrls.h>
21
+#include <media/v4l2-event.h>
2122 #include <media/v4l2-image-sizes.h>
2223 #include <media/i2c/ov7670.h>
23
-#include <media/videobuf-dma-sg.h>
24
+#include <media/videobuf2-dma-sg.h>
2425 #include <linux/delay.h>
2526 #include <linux/dma-mapping.h>
2627 #include <linux/pm_qos.h>
....@@ -84,16 +85,11 @@
8485 * live in frame buffer memory, so we don't call them "DMA".
8586 */
8687 unsigned int cb_offsets[3]; /* offsets into fb mem */
87
- u8 __iomem *cb_addrs[3]; /* Kernel-space addresses */
88
+ u8 __iomem *cb_addrs[3]; /* Kernel-space addresses */
8889 int n_cap_bufs; /* How many are we using? */
89
- int next_buf;
90
- struct videobuf_queue vb_queue;
91
- struct list_head buffer_queue; /* prot. by reg_lock */
92
- /*
93
- * User tracking.
94
- */
95
- int users;
96
- struct file *owner;
90
+ struct vb2_queue vq;
91
+ struct list_head buffer_queue;
92
+ u32 sequence;
9793 /*
9894 * Video format information. sensor_format is kept in a form
9995 * that we can use to pass to the sensor. We always run the
....@@ -104,6 +100,13 @@
104100 struct v4l2_pix_format sensor_format;
105101 struct v4l2_pix_format user_format;
106102 u32 mbus_code;
103
+};
104
+
105
+/* buffer for one video frame */
106
+struct via_buffer {
107
+ /* common v4l buffer stuff -- must be first */
108
+ struct vb2_v4l2_buffer vbuf;
109
+ struct list_head queue;
107110 };
108111
109112 /*
....@@ -142,13 +145,11 @@
142145 * now this information must be managed at this level too.
143146 */
144147 static struct via_format {
145
- __u8 *desc;
146148 __u32 pixelformat;
147149 int bpp; /* Bytes per pixel */
148150 u32 mbus_code;
149151 } via_formats[] = {
150152 {
151
- .desc = "YUYV 4:2:2",
152153 .pixelformat = V4L2_PIX_FMT_YUYV,
153154 .mbus_code = MEDIA_BUS_FMT_YUYV8_2X8,
154155 .bpp = 2,
....@@ -324,28 +325,15 @@
324325 }
325326
326327 /*
327
- * Find the next videobuf buffer which has somebody waiting on it.
328
+ * Find the next buffer which has somebody waiting on it.
328329 */
329
-static struct videobuf_buffer *viacam_next_buffer(struct via_camera *cam)
330
+static struct via_buffer *viacam_next_buffer(struct via_camera *cam)
330331 {
331
- unsigned long flags;
332
- struct videobuf_buffer *buf = NULL;
333
-
334
- spin_lock_irqsave(&cam->viadev->reg_lock, flags);
335332 if (cam->opstate != S_RUNNING)
336
- goto out;
333
+ return NULL;
337334 if (list_empty(&cam->buffer_queue))
338
- goto out;
339
- buf = list_entry(cam->buffer_queue.next, struct videobuf_buffer, queue);
340
- if (!waitqueue_active(&buf->done)) {/* Nobody waiting */
341
- buf = NULL;
342
- goto out;
343
- }
344
- list_del(&buf->queue);
345
- buf->state = VIDEOBUF_ACTIVE;
346
-out:
347
- spin_unlock_irqrestore(&cam->viadev->reg_lock, flags);
348
- return buf;
335
+ return NULL;
336
+ return list_entry(cam->buffer_queue.next, struct via_buffer, queue);
349337 }
350338
351339 /*
....@@ -353,11 +341,12 @@
353341 */
354342 static irqreturn_t viacam_irq(int irq, void *data)
355343 {
356
- int bufn;
357
- struct videobuf_buffer *vb;
358344 struct via_camera *cam = data;
359
- struct videobuf_dmabuf *vdma;
345
+ struct via_buffer *vb;
346
+ int bufn;
347
+ struct sg_table *sgt;
360348
349
+ mutex_lock(&cam->lock);
361350 /*
362351 * If there is no place to put the data frame, don't bother
363352 * with anything else.
....@@ -375,12 +364,15 @@
375364 /*
376365 * Copy over the data and let any waiters know.
377366 */
378
- vdma = videobuf_to_dma(vb);
379
- viafb_dma_copy_out_sg(cam->cb_offsets[bufn], vdma->sglist, vdma->sglen);
380
- vb->state = VIDEOBUF_DONE;
381
- vb->size = cam->user_format.sizeimage;
382
- wake_up(&vb->done);
367
+ sgt = vb2_dma_sg_plane_desc(&vb->vbuf.vb2_buf, 0);
368
+ vb->vbuf.vb2_buf.timestamp = ktime_get_ns();
369
+ viafb_dma_copy_out_sg(cam->cb_offsets[bufn], sgt->sgl, sgt->nents);
370
+ vb->vbuf.sequence = cam->sequence++;
371
+ vb->vbuf.field = V4L2_FIELD_NONE;
372
+ list_del(&vb->queue);
373
+ vb2_buffer_done(&vb->vbuf.vb2_buf, VB2_BUF_STATE_DONE);
383374 done:
375
+ mutex_unlock(&cam->lock);
384376 return IRQ_HANDLED;
385377 }
386378
....@@ -556,7 +548,6 @@
556548 static void viacam_start_engine(struct via_camera *cam)
557549 {
558550 spin_lock_irq(&cam->viadev->reg_lock);
559
- cam->next_buf = 0;
560551 viacam_write_reg_mask(cam, VCR_CAPINTC, VCR_CI_ENABLE, VCR_CI_ENABLE);
561552 viacam_int_enable(cam);
562553 (void) viacam_read_reg(cam, VCR_CAPINTC); /* Force post */
....@@ -577,81 +568,117 @@
577568
578569
579570 /* --------------------------------------------------------------------------*/
580
-/* Videobuf callback ops */
571
+/* vb2 callback ops */
581572
582
-/*
583
- * buffer_setup. The purpose of this one would appear to be to tell
584
- * videobuf how big a single image is. It's also evidently up to us
585
- * to put some sort of limit on the maximum number of buffers allowed.
586
- */
587
-static int viacam_vb_buf_setup(struct videobuf_queue *q,
588
- unsigned int *count, unsigned int *size)
573
+static struct via_buffer *vb2_to_via_buffer(struct vb2_buffer *vb)
589574 {
590
- struct via_camera *cam = q->priv_data;
575
+ struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
591576
592
- *size = cam->user_format.sizeimage;
593
- if (*count == 0 || *count > 6) /* Arbitrary number */
594
- *count = 6;
595
- return 0;
577
+ return container_of(vbuf, struct via_buffer, vbuf);
596578 }
597579
598
-/*
599
- * Prepare a buffer.
600
- */
601
-static int viacam_vb_buf_prepare(struct videobuf_queue *q,
602
- struct videobuf_buffer *vb, enum v4l2_field field)
580
+static void viacam_vb2_queue(struct vb2_buffer *vb)
603581 {
604
- struct via_camera *cam = q->priv_data;
582
+ struct via_camera *cam = vb2_get_drv_priv(vb->vb2_queue);
583
+ struct via_buffer *via = vb2_to_via_buffer(vb);
605584
606
- vb->size = cam->user_format.sizeimage;
607
- vb->width = cam->user_format.width; /* bytesperline???? */
608
- vb->height = cam->user_format.height;
609
- vb->field = field;
610
- if (vb->state == VIDEOBUF_NEEDS_INIT) {
611
- int ret = videobuf_iolock(q, vb, NULL);
612
- if (ret)
613
- return ret;
585
+ list_add_tail(&via->queue, &cam->buffer_queue);
586
+}
587
+
588
+static int viacam_vb2_prepare(struct vb2_buffer *vb)
589
+{
590
+ struct via_camera *cam = vb2_get_drv_priv(vb->vb2_queue);
591
+
592
+ if (vb2_plane_size(vb, 0) < cam->user_format.sizeimage) {
593
+ cam_dbg(cam,
594
+ "Plane size too small (%lu < %u)\n",
595
+ vb2_plane_size(vb, 0),
596
+ cam->user_format.sizeimage);
597
+ return -EINVAL;
614598 }
615
- vb->state = VIDEOBUF_PREPARED;
599
+
600
+ vb2_set_plane_payload(vb, 0, cam->user_format.sizeimage);
601
+
616602 return 0;
617603 }
618604
619
-/*
620
- * We've got a buffer to put data into.
621
- *
622
- * FIXME: check for a running engine and valid buffers?
623
- */
624
-static void viacam_vb_buf_queue(struct videobuf_queue *q,
625
- struct videobuf_buffer *vb)
605
+static int viacam_vb2_queue_setup(struct vb2_queue *vq,
606
+ unsigned int *nbufs,
607
+ unsigned int *num_planes, unsigned int sizes[],
608
+ struct device *alloc_devs[])
626609 {
627
- struct via_camera *cam = q->priv_data;
610
+ struct via_camera *cam = vb2_get_drv_priv(vq);
611
+ int size = cam->user_format.sizeimage;
628612
613
+ if (*num_planes)
614
+ return sizes[0] < size ? -EINVAL : 0;
615
+
616
+ *num_planes = 1;
617
+ sizes[0] = size;
618
+ return 0;
619
+}
620
+
621
+static int viacam_vb2_start_streaming(struct vb2_queue *vq, unsigned int count)
622
+{
623
+ struct via_camera *cam = vb2_get_drv_priv(vq);
624
+ struct via_buffer *buf, *tmp;
625
+ int ret = 0;
626
+
627
+ if (cam->opstate != S_IDLE) {
628
+ ret = -EBUSY;
629
+ goto out;
630
+ }
629631 /*
630
- * Note that videobuf holds the lock when it calls
631
- * us, so we need not (indeed, cannot) take it here.
632
+ * Configure things if need be.
632633 */
633
- vb->state = VIDEOBUF_QUEUED;
634
- list_add_tail(&vb->queue, &cam->buffer_queue);
634
+ if (test_bit(CF_CONFIG_NEEDED, &cam->flags)) {
635
+ ret = viacam_configure_sensor(cam);
636
+ if (ret)
637
+ goto out;
638
+ ret = viacam_config_controller(cam);
639
+ if (ret)
640
+ goto out;
641
+ }
642
+ cam->sequence = 0;
643
+ /*
644
+ * If the CPU goes into C3, the DMA transfer gets corrupted and
645
+ * users start filing unsightly bug reports. Put in a "latency"
646
+ * requirement which will keep the CPU out of the deeper sleep
647
+ * states.
648
+ */
649
+ cpu_latency_qos_add_request(&cam->qos_request, 50);
650
+ viacam_start_engine(cam);
651
+ return 0;
652
+out:
653
+ list_for_each_entry_safe(buf, tmp, &cam->buffer_queue, queue) {
654
+ list_del(&buf->queue);
655
+ vb2_buffer_done(&buf->vbuf.vb2_buf, VB2_BUF_STATE_QUEUED);
656
+ }
657
+ return ret;
635658 }
636659
637
-/*
638
- * Free a buffer.
639
- */
640
-static void viacam_vb_buf_release(struct videobuf_queue *q,
641
- struct videobuf_buffer *vb)
660
+static void viacam_vb2_stop_streaming(struct vb2_queue *vq)
642661 {
643
- struct via_camera *cam = q->priv_data;
662
+ struct via_camera *cam = vb2_get_drv_priv(vq);
663
+ struct via_buffer *buf, *tmp;
644664
645
- videobuf_dma_unmap(&cam->platdev->dev, videobuf_to_dma(vb));
646
- videobuf_dma_free(videobuf_to_dma(vb));
647
- vb->state = VIDEOBUF_NEEDS_INIT;
665
+ cpu_latency_qos_remove_request(&cam->qos_request);
666
+ viacam_stop_engine(cam);
667
+
668
+ list_for_each_entry_safe(buf, tmp, &cam->buffer_queue, queue) {
669
+ list_del(&buf->queue);
670
+ vb2_buffer_done(&buf->vbuf.vb2_buf, VB2_BUF_STATE_ERROR);
671
+ }
648672 }
649673
650
-static const struct videobuf_queue_ops viacam_vb_ops = {
651
- .buf_setup = viacam_vb_buf_setup,
652
- .buf_prepare = viacam_vb_buf_prepare,
653
- .buf_queue = viacam_vb_buf_queue,
654
- .buf_release = viacam_vb_buf_release,
674
+static const struct vb2_ops viacam_vb2_ops = {
675
+ .queue_setup = viacam_vb2_queue_setup,
676
+ .buf_queue = viacam_vb2_queue,
677
+ .buf_prepare = viacam_vb2_prepare,
678
+ .start_streaming = viacam_vb2_start_streaming,
679
+ .stop_streaming = viacam_vb2_stop_streaming,
680
+ .wait_prepare = vb2_ops_wait_prepare,
681
+ .wait_finish = vb2_ops_wait_finish,
655682 };
656683
657684 /* --------------------------------------------------------------------------*/
....@@ -660,62 +687,43 @@
660687 static int viacam_open(struct file *filp)
661688 {
662689 struct via_camera *cam = video_drvdata(filp);
690
+ int ret;
663691
664
- filp->private_data = cam;
665692 /*
666693 * Note the new user. If this is the first one, we'll also
667694 * need to power up the sensor.
668695 */
669696 mutex_lock(&cam->lock);
670
- if (cam->users == 0) {
671
- int ret = viafb_request_dma();
697
+ ret = v4l2_fh_open(filp);
698
+ if (ret)
699
+ goto out;
700
+ if (v4l2_fh_is_singular_file(filp)) {
701
+ ret = viafb_request_dma();
672702
673703 if (ret) {
674
- mutex_unlock(&cam->lock);
675
- return ret;
704
+ v4l2_fh_release(filp);
705
+ goto out;
676706 }
677707 via_sensor_power_up(cam);
678708 set_bit(CF_CONFIG_NEEDED, &cam->flags);
679
- /*
680
- * Hook into videobuf. Evidently this cannot fail.
681
- */
682
- videobuf_queue_sg_init(&cam->vb_queue, &viacam_vb_ops,
683
- &cam->platdev->dev, &cam->viadev->reg_lock,
684
- V4L2_BUF_TYPE_VIDEO_CAPTURE, V4L2_FIELD_NONE,
685
- sizeof(struct videobuf_buffer), cam, NULL);
686709 }
687
- (cam->users)++;
710
+out:
688711 mutex_unlock(&cam->lock);
689
- return 0;
712
+ return ret;
690713 }
691714
692715 static int viacam_release(struct file *filp)
693716 {
694717 struct via_camera *cam = video_drvdata(filp);
718
+ bool last_open;
695719
696720 mutex_lock(&cam->lock);
697
- (cam->users)--;
698
- /*
699
- * If the "owner" is closing, shut down any ongoing
700
- * operations.
701
- */
702
- if (filp == cam->owner) {
703
- videobuf_stop(&cam->vb_queue);
704
- /*
705
- * We don't hold the spinlock here, but, if release()
706
- * is being called by the owner, nobody else will
707
- * be changing the state. And an extra stop would
708
- * not hurt anyway.
709
- */
710
- if (cam->opstate != S_IDLE)
711
- viacam_stop_engine(cam);
712
- cam->owner = NULL;
713
- }
721
+ last_open = v4l2_fh_is_singular_file(filp);
722
+ _vb2_fop_release(filp, NULL);
714723 /*
715724 * Last one out needs to turn out the lights.
716725 */
717
- if (cam->users == 0) {
718
- videobuf_mmap_free(&cam->vb_queue);
726
+ if (last_open) {
719727 via_sensor_power_down(cam);
720728 viafb_release_dma();
721729 }
....@@ -723,77 +731,14 @@
723731 return 0;
724732 }
725733
726
-/*
727
- * Read a frame from the device.
728
- */
729
-static ssize_t viacam_read(struct file *filp, char __user *buffer,
730
- size_t len, loff_t *pos)
731
-{
732
- struct via_camera *cam = video_drvdata(filp);
733
- int ret;
734
-
735
- mutex_lock(&cam->lock);
736
- /*
737
- * Enforce the V4l2 "only one owner gets to read data" rule.
738
- */
739
- if (cam->owner && cam->owner != filp) {
740
- ret = -EBUSY;
741
- goto out_unlock;
742
- }
743
- cam->owner = filp;
744
- /*
745
- * Do we need to configure the hardware?
746
- */
747
- if (test_bit(CF_CONFIG_NEEDED, &cam->flags)) {
748
- ret = viacam_configure_sensor(cam);
749
- if (!ret)
750
- ret = viacam_config_controller(cam);
751
- if (ret)
752
- goto out_unlock;
753
- }
754
- /*
755
- * Fire up the capture engine, then have videobuf do
756
- * the heavy lifting. Someday it would be good to avoid
757
- * stopping and restarting the engine each time.
758
- */
759
- INIT_LIST_HEAD(&cam->buffer_queue);
760
- viacam_start_engine(cam);
761
- ret = videobuf_read_stream(&cam->vb_queue, buffer, len, pos, 0,
762
- filp->f_flags & O_NONBLOCK);
763
- viacam_stop_engine(cam);
764
- /* videobuf_stop() ?? */
765
-
766
-out_unlock:
767
- mutex_unlock(&cam->lock);
768
- return ret;
769
-}
770
-
771
-
772
-static __poll_t viacam_poll(struct file *filp, struct poll_table_struct *pt)
773
-{
774
- struct via_camera *cam = video_drvdata(filp);
775
-
776
- return videobuf_poll_stream(filp, &cam->vb_queue, pt);
777
-}
778
-
779
-
780
-static int viacam_mmap(struct file *filp, struct vm_area_struct *vma)
781
-{
782
- struct via_camera *cam = video_drvdata(filp);
783
-
784
- return videobuf_mmap_mapper(&cam->vb_queue, vma);
785
-}
786
-
787
-
788
-
789734 static const struct v4l2_file_operations viacam_fops = {
790735 .owner = THIS_MODULE,
791736 .open = viacam_open,
792737 .release = viacam_release,
793
- .read = viacam_read,
794
- .poll = viacam_poll,
795
- .mmap = viacam_mmap,
796
- .unlocked_ioctl = video_ioctl2,
738
+ .read = vb2_fop_read,
739
+ .poll = vb2_fop_poll,
740
+ .mmap = vb2_fop_mmap,
741
+ .unlocked_ioctl = video_ioctl2,
797742 };
798743
799744 /*----------------------------------------------------------------------------*/
....@@ -811,8 +756,7 @@
811756 return -EINVAL;
812757
813758 input->type = V4L2_INPUT_TYPE_CAMERA;
814
- input->std = V4L2_STD_ALL; /* Not sure what should go here */
815
- strcpy(input->name, "Camera");
759
+ strscpy(input->name, "Camera", sizeof(input->name));
816760 return 0;
817761 }
818762
....@@ -829,17 +773,6 @@
829773 return 0;
830774 }
831775
832
-static int viacam_s_std(struct file *filp, void *priv, v4l2_std_id std)
833
-{
834
- return 0;
835
-}
836
-
837
-static int viacam_g_std(struct file *filp, void *priv, v4l2_std_id *std)
838
-{
839
- *std = V4L2_STD_NTSC_M;
840
- return 0;
841
-}
842
-
843776 /*
844777 * Video format stuff. Here is our default format until
845778 * user space messes with things.
....@@ -851,6 +784,7 @@
851784 .field = V4L2_FIELD_NONE,
852785 .bytesperline = VGA_WIDTH * 2,
853786 .sizeimage = VGA_WIDTH * VGA_HEIGHT * 2,
787
+ .colorspace = V4L2_COLORSPACE_SRGB,
854788 };
855789
856790 static const u32 via_def_mbus_code = MEDIA_BUS_FMT_YUYV8_2X8;
....@@ -860,8 +794,6 @@
860794 {
861795 if (fmt->index >= N_VIA_FMTS)
862796 return -EINVAL;
863
- strlcpy(fmt->description, via_formats[fmt->index].desc,
864
- sizeof(fmt->description));
865797 fmt->pixelformat = via_formats[fmt->index].pixelformat;
866798 return 0;
867799 }
....@@ -897,6 +829,10 @@
897829 userfmt->field = sensorfmt->field;
898830 userfmt->bytesperline = 2 * userfmt->width;
899831 userfmt->sizeimage = userfmt->bytesperline * userfmt->height;
832
+ userfmt->colorspace = sensorfmt->colorspace;
833
+ userfmt->ycbcr_enc = sensorfmt->ycbcr_enc;
834
+ userfmt->quantization = sensorfmt->quantization;
835
+ userfmt->xfer_func = sensorfmt->xfer_func;
900836 }
901837
902838
....@@ -927,32 +863,26 @@
927863 static int viacam_try_fmt_vid_cap(struct file *filp, void *priv,
928864 struct v4l2_format *fmt)
929865 {
930
- struct via_camera *cam = priv;
866
+ struct via_camera *cam = video_drvdata(filp);
931867 struct v4l2_format sfmt;
932
- int ret;
933868
934
- mutex_lock(&cam->lock);
935
- ret = viacam_do_try_fmt(cam, &fmt->fmt.pix, &sfmt.fmt.pix);
936
- mutex_unlock(&cam->lock);
937
- return ret;
869
+ return viacam_do_try_fmt(cam, &fmt->fmt.pix, &sfmt.fmt.pix);
938870 }
939871
940872
941873 static int viacam_g_fmt_vid_cap(struct file *filp, void *priv,
942874 struct v4l2_format *fmt)
943875 {
944
- struct via_camera *cam = priv;
876
+ struct via_camera *cam = video_drvdata(filp);
945877
946
- mutex_lock(&cam->lock);
947878 fmt->fmt.pix = cam->user_format;
948
- mutex_unlock(&cam->lock);
949879 return 0;
950880 }
951881
952882 static int viacam_s_fmt_vid_cap(struct file *filp, void *priv,
953883 struct v4l2_format *fmt)
954884 {
955
- struct via_camera *cam = priv;
885
+ struct via_camera *cam = video_drvdata(filp);
956886 int ret;
957887 struct v4l2_format sfmt;
958888 struct via_format *f = via_find_format(fmt->fmt.pix.pixelformat);
....@@ -961,18 +891,15 @@
961891 * Camera must be idle or we can't mess with the
962892 * video setup.
963893 */
964
- mutex_lock(&cam->lock);
965
- if (cam->opstate != S_IDLE) {
966
- ret = -EBUSY;
967
- goto out;
968
- }
894
+ if (cam->opstate != S_IDLE)
895
+ return -EBUSY;
969896 /*
970897 * Let the sensor code look over and tweak the
971898 * requested formatting.
972899 */
973900 ret = viacam_do_try_fmt(cam, &fmt->fmt.pix, &sfmt.fmt.pix);
974901 if (ret)
975
- goto out;
902
+ return ret;
976903 /*
977904 * OK, let's commit to the new format.
978905 */
....@@ -982,130 +909,16 @@
982909 ret = viacam_configure_sensor(cam);
983910 if (!ret)
984911 ret = viacam_config_controller(cam);
985
-out:
986
- mutex_unlock(&cam->lock);
987912 return ret;
988913 }
989914
990915 static int viacam_querycap(struct file *filp, void *priv,
991916 struct v4l2_capability *cap)
992917 {
993
- strcpy(cap->driver, "via-camera");
994
- strcpy(cap->card, "via-camera");
995
- cap->device_caps = V4L2_CAP_VIDEO_CAPTURE |
996
- V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
997
- cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
918
+ strscpy(cap->driver, "via-camera", sizeof(cap->driver));
919
+ strscpy(cap->card, "via-camera", sizeof(cap->card));
920
+ strscpy(cap->bus_info, "platform:via-camera", sizeof(cap->bus_info));
998921 return 0;
999
-}
1000
-
1001
-/*
1002
- * Streaming operations - pure videobuf stuff.
1003
- */
1004
-static int viacam_reqbufs(struct file *filp, void *priv,
1005
- struct v4l2_requestbuffers *rb)
1006
-{
1007
- struct via_camera *cam = priv;
1008
-
1009
- return videobuf_reqbufs(&cam->vb_queue, rb);
1010
-}
1011
-
1012
-static int viacam_querybuf(struct file *filp, void *priv,
1013
- struct v4l2_buffer *buf)
1014
-{
1015
- struct via_camera *cam = priv;
1016
-
1017
- return videobuf_querybuf(&cam->vb_queue, buf);
1018
-}
1019
-
1020
-static int viacam_qbuf(struct file *filp, void *priv, struct v4l2_buffer *buf)
1021
-{
1022
- struct via_camera *cam = priv;
1023
-
1024
- return videobuf_qbuf(&cam->vb_queue, buf);
1025
-}
1026
-
1027
-static int viacam_dqbuf(struct file *filp, void *priv, struct v4l2_buffer *buf)
1028
-{
1029
- struct via_camera *cam = priv;
1030
-
1031
- return videobuf_dqbuf(&cam->vb_queue, buf, filp->f_flags & O_NONBLOCK);
1032
-}
1033
-
1034
-static int viacam_streamon(struct file *filp, void *priv, enum v4l2_buf_type t)
1035
-{
1036
- struct via_camera *cam = priv;
1037
- int ret = 0;
1038
-
1039
- if (t != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1040
- return -EINVAL;
1041
-
1042
- mutex_lock(&cam->lock);
1043
- if (cam->opstate != S_IDLE) {
1044
- ret = -EBUSY;
1045
- goto out;
1046
- }
1047
- /*
1048
- * Enforce the V4l2 "only one owner gets to read data" rule.
1049
- */
1050
- if (cam->owner && cam->owner != filp) {
1051
- ret = -EBUSY;
1052
- goto out;
1053
- }
1054
- cam->owner = filp;
1055
- /*
1056
- * Configure things if need be.
1057
- */
1058
- if (test_bit(CF_CONFIG_NEEDED, &cam->flags)) {
1059
- ret = viacam_configure_sensor(cam);
1060
- if (ret)
1061
- goto out;
1062
- ret = viacam_config_controller(cam);
1063
- if (ret)
1064
- goto out;
1065
- }
1066
- /*
1067
- * If the CPU goes into C3, the DMA transfer gets corrupted and
1068
- * users start filing unsightly bug reports. Put in a "latency"
1069
- * requirement which will keep the CPU out of the deeper sleep
1070
- * states.
1071
- */
1072
- pm_qos_add_request(&cam->qos_request, PM_QOS_CPU_DMA_LATENCY, 50);
1073
- /*
1074
- * Fire things up.
1075
- */
1076
- INIT_LIST_HEAD(&cam->buffer_queue);
1077
- ret = videobuf_streamon(&cam->vb_queue);
1078
- if (!ret)
1079
- viacam_start_engine(cam);
1080
-out:
1081
- mutex_unlock(&cam->lock);
1082
- return ret;
1083
-}
1084
-
1085
-static int viacam_streamoff(struct file *filp, void *priv, enum v4l2_buf_type t)
1086
-{
1087
- struct via_camera *cam = priv;
1088
- int ret;
1089
-
1090
- if (t != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1091
- return -EINVAL;
1092
- mutex_lock(&cam->lock);
1093
- if (cam->opstate != S_RUNNING) {
1094
- ret = -EINVAL;
1095
- goto out;
1096
- }
1097
- pm_qos_remove_request(&cam->qos_request);
1098
- viacam_stop_engine(cam);
1099
- /*
1100
- * Videobuf will recycle all of the outstanding buffers, but
1101
- * we should be sure we don't retain any references to
1102
- * any of them.
1103
- */
1104
- ret = videobuf_streamoff(&cam->vb_queue);
1105
- INIT_LIST_HEAD(&cam->buffer_queue);
1106
-out:
1107
- mutex_unlock(&cam->lock);
1108
- return ret;
1109922 }
1110923
1111924 /* G/S_PARM */
....@@ -1113,33 +926,30 @@
1113926 static int viacam_g_parm(struct file *filp, void *priv,
1114927 struct v4l2_streamparm *parm)
1115928 {
1116
- struct via_camera *cam = priv;
1117
- int ret;
929
+ struct via_camera *cam = video_drvdata(filp);
1118930
1119
- mutex_lock(&cam->lock);
1120
- ret = v4l2_g_parm_cap(video_devdata(filp), cam->sensor, parm);
1121
- mutex_unlock(&cam->lock);
1122
- parm->parm.capture.readbuffers = cam->n_cap_bufs;
1123
- return ret;
931
+ return v4l2_g_parm_cap(video_devdata(filp), cam->sensor, parm);
1124932 }
1125933
1126934 static int viacam_s_parm(struct file *filp, void *priv,
1127935 struct v4l2_streamparm *parm)
1128936 {
1129
- struct via_camera *cam = priv;
1130
- int ret;
937
+ struct via_camera *cam = video_drvdata(filp);
1131938
1132
- mutex_lock(&cam->lock);
1133
- ret = v4l2_s_parm_cap(video_devdata(filp), cam->sensor, parm);
1134
- mutex_unlock(&cam->lock);
1135
- parm->parm.capture.readbuffers = cam->n_cap_bufs;
1136
- return ret;
939
+ return v4l2_s_parm_cap(video_devdata(filp), cam->sensor, parm);
1137940 }
1138941
1139942 static int viacam_enum_framesizes(struct file *filp, void *priv,
1140943 struct v4l2_frmsizeenum *sizes)
1141944 {
945
+ unsigned int i;
946
+
1142947 if (sizes->index != 0)
948
+ return -EINVAL;
949
+ for (i = 0; i < N_VIA_FMTS; i++)
950
+ if (sizes->pixel_format == via_formats[i].pixelformat)
951
+ break;
952
+ if (i >= N_VIA_FMTS)
1143953 return -EINVAL;
1144954 sizes->type = V4L2_FRMSIZE_TYPE_CONTINUOUS;
1145955 sizes->stepwise.min_width = QCIF_WIDTH;
....@@ -1153,7 +963,7 @@
1153963 static int viacam_enum_frameintervals(struct file *filp, void *priv,
1154964 struct v4l2_frmivalenum *interval)
1155965 {
1156
- struct via_camera *cam = priv;
966
+ struct via_camera *cam = video_drvdata(filp);
1157967 struct v4l2_subdev_frame_interval_enum fie = {
1158968 .index = interval->index,
1159969 .code = cam->mbus_code,
....@@ -1161,11 +971,18 @@
1161971 .height = cam->sensor_format.height,
1162972 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
1163973 };
974
+ unsigned int i;
1164975 int ret;
1165976
1166
- mutex_lock(&cam->lock);
977
+ for (i = 0; i < N_VIA_FMTS; i++)
978
+ if (interval->pixel_format == via_formats[i].pixelformat)
979
+ break;
980
+ if (i >= N_VIA_FMTS)
981
+ return -EINVAL;
982
+ if (interval->width < QCIF_WIDTH || interval->width > VGA_WIDTH ||
983
+ interval->height < QCIF_HEIGHT || interval->height > VGA_HEIGHT)
984
+ return -EINVAL;
1167985 ret = sensor_call(cam, pad, enum_frame_interval, NULL, &fie);
1168
- mutex_unlock(&cam->lock);
1169986 if (ret)
1170987 return ret;
1171988 interval->type = V4L2_FRMIVAL_TYPE_DISCRETE;
....@@ -1173,29 +990,30 @@
1173990 return 0;
1174991 }
1175992
1176
-
1177
-
1178993 static const struct v4l2_ioctl_ops viacam_ioctl_ops = {
1179994 .vidioc_enum_input = viacam_enum_input,
1180995 .vidioc_g_input = viacam_g_input,
1181996 .vidioc_s_input = viacam_s_input,
1182
- .vidioc_s_std = viacam_s_std,
1183
- .vidioc_g_std = viacam_g_std,
1184997 .vidioc_enum_fmt_vid_cap = viacam_enum_fmt_vid_cap,
1185998 .vidioc_try_fmt_vid_cap = viacam_try_fmt_vid_cap,
1186999 .vidioc_g_fmt_vid_cap = viacam_g_fmt_vid_cap,
11871000 .vidioc_s_fmt_vid_cap = viacam_s_fmt_vid_cap,
11881001 .vidioc_querycap = viacam_querycap,
1189
- .vidioc_reqbufs = viacam_reqbufs,
1190
- .vidioc_querybuf = viacam_querybuf,
1191
- .vidioc_qbuf = viacam_qbuf,
1192
- .vidioc_dqbuf = viacam_dqbuf,
1193
- .vidioc_streamon = viacam_streamon,
1194
- .vidioc_streamoff = viacam_streamoff,
1002
+ .vidioc_reqbufs = vb2_ioctl_reqbufs,
1003
+ .vidioc_create_bufs = vb2_ioctl_create_bufs,
1004
+ .vidioc_querybuf = vb2_ioctl_querybuf,
1005
+ .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
1006
+ .vidioc_qbuf = vb2_ioctl_qbuf,
1007
+ .vidioc_dqbuf = vb2_ioctl_dqbuf,
1008
+ .vidioc_expbuf = vb2_ioctl_expbuf,
1009
+ .vidioc_streamon = vb2_ioctl_streamon,
1010
+ .vidioc_streamoff = vb2_ioctl_streamoff,
11951011 .vidioc_g_parm = viacam_g_parm,
11961012 .vidioc_s_parm = viacam_s_parm,
11971013 .vidioc_enum_framesizes = viacam_enum_framesizes,
11981014 .vidioc_enum_frameintervals = viacam_enum_frameintervals,
1015
+ .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1016
+ .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
11991017 };
12001018
12011019 /*----------------------------------------------------------------------------*/
....@@ -1233,7 +1051,7 @@
12331051 /*
12341052 * Make sure the sensor's power state is correct
12351053 */
1236
- if (cam->users > 0)
1054
+ if (!list_empty(&cam->vdev.fh_list))
12371055 via_sensor_power_up(cam);
12381056 else
12391057 via_sensor_power_down(cam);
....@@ -1267,10 +1085,11 @@
12671085 static const struct video_device viacam_v4l_template = {
12681086 .name = "via-camera",
12691087 .minor = -1,
1270
- .tvnorms = V4L2_STD_NTSC_M,
12711088 .fops = &viacam_fops,
12721089 .ioctl_ops = &viacam_ioctl_ops,
12731090 .release = video_device_release_empty, /* Check this */
1091
+ .device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_READWRITE |
1092
+ V4L2_CAP_STREAMING,
12741093 };
12751094
12761095 /*
....@@ -1317,6 +1136,7 @@
13171136 int ret;
13181137 struct i2c_adapter *sensor_adapter;
13191138 struct viafb_dev *viadev = pdev->dev.platform_data;
1139
+ struct vb2_queue *vq;
13201140 struct i2c_board_info ov7670_info = {
13211141 .type = "ov7670",
13221142 .addr = 0x42 >> 1,
....@@ -1360,8 +1180,6 @@
13601180 via_cam_info = cam;
13611181 cam->platdev = pdev;
13621182 cam->viadev = viadev;
1363
- cam->users = 0;
1364
- cam->owner = NULL;
13651183 cam->opstate = S_IDLE;
13661184 cam->user_format = cam->sensor_format = viacam_def_pix_format;
13671185 mutex_init(&cam->lock);
....@@ -1422,15 +1240,31 @@
14221240 viacam_irq, IRQF_SHARED, "via-camera", cam);
14231241 if (ret)
14241242 goto out_power_down;
1243
+
1244
+ vq = &cam->vq;
1245
+ vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1246
+ vq->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF | VB2_READ;
1247
+ vq->drv_priv = cam;
1248
+ vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1249
+ vq->buf_struct_size = sizeof(struct via_buffer);
1250
+ vq->dev = cam->v4l2_dev.dev;
1251
+
1252
+ vq->ops = &viacam_vb2_ops;
1253
+ vq->mem_ops = &vb2_dma_sg_memops;
1254
+ vq->lock = &cam->lock;
1255
+
1256
+ ret = vb2_queue_init(vq);
14251257 /*
14261258 * Tell V4l2 that we exist.
14271259 */
14281260 cam->vdev = viacam_v4l_template;
14291261 cam->vdev.v4l2_dev = &cam->v4l2_dev;
1430
- ret = video_register_device(&cam->vdev, VFL_TYPE_GRABBER, -1);
1262
+ cam->vdev.lock = &cam->lock;
1263
+ cam->vdev.queue = vq;
1264
+ video_set_drvdata(&cam->vdev, cam);
1265
+ ret = video_register_device(&cam->vdev, VFL_TYPE_VIDEO, -1);
14311266 if (ret)
14321267 goto out_irq;
1433
- video_set_drvdata(&cam->vdev, cam);
14341268
14351269 #ifdef CONFIG_PM
14361270 /*
....@@ -1464,6 +1298,9 @@
14641298
14651299 video_unregister_device(&cam->vdev);
14661300 v4l2_device_unregister(&cam->v4l2_dev);
1301
+#ifdef CONFIG_PM
1302
+ viafb_pm_unregister(&viacam_pm_hooks);
1303
+#endif
14671304 free_irq(viadev->pdev->irq, cam);
14681305 via_sensor_power_release(cam);
14691306 v4l2_ctrl_handler_free(&cam->ctrl_handler);