472
479
dev->width, dev->height, dev->input);
473
480
gen_text(dev, vbuf, line++ * 16, 16, str);
482
mutex_lock(&dev->ctrl_handler.lock);
475
483
snprintf(str, sizeof(str), " brightness %3d, contrast %3d, saturation %3d, hue %d ",
480
gen_text(dev, vbuf, line++ * 16, 16, str);
481
snprintf(str, sizeof(str), " volume %3d ", dev->volume);
482
gen_text(dev, vbuf, line++ * 16, 16, str);
484
dev->brightness->cur.val,
485
dev->contrast->cur.val,
486
dev->saturation->cur.val,
488
gen_text(dev, vbuf, line++ * 16, 16, str);
489
snprintf(str, sizeof(str), " volume %3d ", dev->volume->cur.val);
490
gen_text(dev, vbuf, line++ * 16, 16, str);
491
snprintf(str, sizeof(str), " int32 %d, int64 %lld ",
493
dev->int64->cur.val64);
494
gen_text(dev, vbuf, line++ * 16, 16, str);
495
snprintf(str, sizeof(str), " boolean %d, menu %s, string \"%s\" ",
496
dev->boolean->cur.val,
497
dev->menu->qmenu[dev->menu->cur.val],
498
dev->string->cur.string);
499
mutex_unlock(&dev->ctrl_handler.lock);
500
gen_text(dev, vbuf, line++ * 16, 16, str);
501
if (dev->button_pressed) {
502
dev->button_pressed--;
503
snprintf(str, sizeof(str), " button pressed!");
504
gen_text(dev, vbuf, line++ * 16, 16, str);
484
507
dev->mv_count += 2;
486
/* Advice that buffer was filled */
487
buf->vb.field_count++;
509
buf->vb.v4l2_buf.field = dev->field;
511
buf->vb.v4l2_buf.sequence = dev->field_count >> 1;
488
512
do_gettimeofday(&ts);
490
buf->vb.state = VIDEOBUF_DONE;
513
buf->vb.v4l2_buf.timestamp = ts;
493
516
static void vivi_thread_tick(struct vivi_dev *dev)
594
606
if (IS_ERR(dma_q->kthread)) {
595
607
v4l2_err(&dev->v4l2_dev, "kernel_thread() failed\n");
596
clear_bit(0, &dev->generating);
608
return PTR_ERR(dma_q->kthread);
599
610
/* Wakes thread */
600
611
wake_up_interruptible(&dma_q->wq);
602
613
dprintk(dev, 1, "returning from %s\n", __func__);
605
static void vivi_stop_generating(struct file *file)
617
static void vivi_stop_generating(struct vivi_dev *dev)
607
struct vivi_dev *dev = video_drvdata(file);
608
619
struct vivi_dmaqueue *dma_q = &dev->vidq;
610
621
dprintk(dev, 1, "%s\n", __func__);
612
if (!file->private_data)
614
if (!test_and_clear_bit(0, &dev->generating))
617
623
/* shutdown control thread */
618
624
if (dma_q->kthread) {
619
625
kthread_stop(dma_q->kthread);
620
626
dma_q->kthread = NULL;
622
videobuf_stop(&dev->vb_vidq);
623
videobuf_mmap_free(&dev->vb_vidq);
626
static int vivi_is_generating(struct vivi_dev *dev)
628
return test_bit(0, &dev->generating);
630
* Typical driver might need to wait here until dma engine stops.
631
* In this case we can abort imiedetly, so it's just a noop.
634
/* Release all active buffers */
635
while (!list_empty(&dma_q->active)) {
636
struct vivi_buffer *buf;
637
buf = list_entry(dma_q->active.next, struct vivi_buffer, list);
638
list_del(&buf->list);
639
vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
640
dprintk(dev, 2, "[%p/%d] done\n", buf, buf->vb.v4l2_buf.index);
631
643
/* ------------------------------------------------------------------
632
644
Videobuf operations
633
645
------------------------------------------------------------------*/
635
buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size)
637
struct vivi_dev *dev = vq->priv_data;
639
*size = dev->width * dev->height * 2;
644
while (*size * *count > vid_limit * 1024 * 1024)
647
dprintk(dev, 1, "%s, count=%d, size=%d\n", __func__,
653
static void free_buffer(struct videobuf_queue *vq, struct vivi_buffer *buf)
655
struct vivi_dev *dev = vq->priv_data;
657
dprintk(dev, 1, "%s, state: %i\n", __func__, buf->vb.state);
659
videobuf_vmalloc_free(&buf->vb);
660
dprintk(dev, 1, "free_buffer: freed\n");
661
buf->vb.state = VIDEOBUF_NEEDS_INIT;
665
buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
666
enum v4l2_field field)
668
struct vivi_dev *dev = vq->priv_data;
646
static int queue_setup(struct vb2_queue *vq, unsigned int *nbuffers,
647
unsigned int *nplanes, unsigned long sizes[],
650
struct vivi_dev *dev = vb2_get_drv_priv(vq);
653
size = dev->width * dev->height * 2;
658
while (size * *nbuffers > vid_limit * 1024 * 1024)
666
* videobuf2-vmalloc allocator is context-less so no need to set
670
dprintk(dev, 1, "%s, count=%d, size=%ld\n", __func__,
676
static int buffer_init(struct vb2_buffer *vb)
678
struct vivi_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
680
BUG_ON(NULL == dev->fmt);
683
* This callback is called once per buffer, after its allocation.
685
* Vivi does not allow changing format during streaming, but it is
686
* possible to do so when streaming is paused (i.e. in streamoff state).
687
* Buffers however are not freed when going into streamoff and so
688
* buffer size verification has to be done in buffer_prepare, on each
690
* It would be best to move verification code here to buf_init and
697
static int buffer_prepare(struct vb2_buffer *vb)
699
struct vivi_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
669
700
struct vivi_buffer *buf = container_of(vb, struct vivi_buffer, vb);
672
dprintk(dev, 1, "%s, field=%d\n", __func__, field);
703
dprintk(dev, 1, "%s, field=%d\n", __func__, vb->v4l2_buf.field);
674
705
BUG_ON(NULL == dev->fmt);
708
* Theses properties only change when queue is idle, see s_fmt.
709
* The below checks should not be performed here, on each
710
* buffer_prepare (i.e. on each qbuf). Most of the code in this function
711
* should thus be moved to buffer_init and s_fmt.
676
713
if (dev->width < 48 || dev->width > MAX_WIDTH ||
677
714
dev->height < 32 || dev->height > MAX_HEIGHT)
680
buf->vb.size = dev->width * dev->height * 2;
681
if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
717
size = dev->width * dev->height * 2;
718
if (vb2_plane_size(vb, 0) < size) {
719
dprintk(dev, 1, "%s data will not fit into plane (%lu < %lu)\n",
720
__func__, vb2_plane_size(vb, 0), size);
684
/* These properties only change when queue is idle, see s_fmt */
686
buf->vb.width = dev->width;
687
buf->vb.height = dev->height;
688
buf->vb.field = field;
724
vb2_set_plane_payload(&buf->vb, 0, size);
690
728
precalculate_bars(dev);
691
729
precalculate_line(dev);
693
if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
694
rc = videobuf_iolock(vq, &buf->vb, NULL);
699
buf->vb.state = VIDEOBUF_PREPARED;
703
free_buffer(vq, buf);
708
buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
710
struct vivi_dev *dev = vq->priv_data;
734
static int buffer_finish(struct vb2_buffer *vb)
736
struct vivi_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
737
dprintk(dev, 1, "%s\n", __func__);
741
static void buffer_cleanup(struct vb2_buffer *vb)
743
struct vivi_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
744
dprintk(dev, 1, "%s\n", __func__);
748
static void buffer_queue(struct vb2_buffer *vb)
750
struct vivi_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
711
751
struct vivi_buffer *buf = container_of(vb, struct vivi_buffer, vb);
712
752
struct vivi_dmaqueue *vidq = &dev->vidq;
714
dprintk(dev, 1, "%s\n", __func__);
716
buf->vb.state = VIDEOBUF_QUEUED;
717
list_add_tail(&buf->vb.queue, &vidq->active);
720
static void buffer_release(struct videobuf_queue *vq,
721
struct videobuf_buffer *vb)
723
struct vivi_dev *dev = vq->priv_data;
724
struct vivi_buffer *buf = container_of(vb, struct vivi_buffer, vb);
726
dprintk(dev, 1, "%s\n", __func__);
728
free_buffer(vq, buf);
731
static struct videobuf_queue_ops vivi_video_qops = {
732
.buf_setup = buffer_setup,
733
.buf_prepare = buffer_prepare,
734
.buf_queue = buffer_queue,
735
.buf_release = buffer_release,
753
unsigned long flags = 0;
755
dprintk(dev, 1, "%s\n", __func__);
757
spin_lock_irqsave(&dev->slock, flags);
758
list_add_tail(&buf->list, &vidq->active);
759
spin_unlock_irqrestore(&dev->slock, flags);
762
static int start_streaming(struct vb2_queue *vq)
764
struct vivi_dev *dev = vb2_get_drv_priv(vq);
765
dprintk(dev, 1, "%s\n", __func__);
766
return vivi_start_generating(dev);
769
/* abort streaming and wait for last buffer */
770
static int stop_streaming(struct vb2_queue *vq)
772
struct vivi_dev *dev = vb2_get_drv_priv(vq);
773
dprintk(dev, 1, "%s\n", __func__);
774
vivi_stop_generating(dev);
778
static void vivi_lock(struct vb2_queue *vq)
780
struct vivi_dev *dev = vb2_get_drv_priv(vq);
781
mutex_lock(&dev->mutex);
784
static void vivi_unlock(struct vb2_queue *vq)
786
struct vivi_dev *dev = vb2_get_drv_priv(vq);
787
mutex_unlock(&dev->mutex);
791
static struct vb2_ops vivi_video_qops = {
792
.queue_setup = queue_setup,
793
.buf_init = buffer_init,
794
.buf_prepare = buffer_prepare,
795
.buf_finish = buffer_finish,
796
.buf_cleanup = buffer_cleanup,
797
.buf_queue = buffer_queue,
798
.start_streaming = start_streaming,
799
.stop_streaming = stop_streaming,
800
.wait_prepare = vivi_unlock,
801
.wait_finish = vivi_lock,
738
804
/* ------------------------------------------------------------------
820
886
struct v4l2_format *f)
822
888
struct vivi_dev *dev = video_drvdata(file);
889
struct vb2_queue *q = &dev->vb_vidq;
824
891
int ret = vidioc_try_fmt_vid_cap(file, priv, f);
828
if (vivi_is_generating(dev)) {
895
if (vb2_is_streaming(q)) {
829
896
dprintk(dev, 1, "%s device busy\n", __func__);
834
900
dev->fmt = get_format(f);
835
901
dev->width = f->fmt.pix.width;
836
902
dev->height = f->fmt.pix.height;
837
dev->vb_vidq.field = f->fmt.pix.field;
903
dev->field = f->fmt.pix.field;
843
908
static int vidioc_reqbufs(struct file *file, void *priv,
844
909
struct v4l2_requestbuffers *p)
846
911
struct vivi_dev *dev = video_drvdata(file);
848
return videobuf_reqbufs(&dev->vb_vidq, p);
912
return vb2_reqbufs(&dev->vb_vidq, p);
851
915
static int vidioc_querybuf(struct file *file, void *priv, struct v4l2_buffer *p)
853
917
struct vivi_dev *dev = video_drvdata(file);
855
return videobuf_querybuf(&dev->vb_vidq, p);
918
return vb2_querybuf(&dev->vb_vidq, p);
858
921
static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *p)
860
923
struct vivi_dev *dev = video_drvdata(file);
862
return videobuf_qbuf(&dev->vb_vidq, p);
924
return vb2_qbuf(&dev->vb_vidq, p);
865
927
static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
867
929
struct vivi_dev *dev = video_drvdata(file);
869
return videobuf_dqbuf(&dev->vb_vidq, p,
870
file->f_flags & O_NONBLOCK);
930
return vb2_dqbuf(&dev->vb_vidq, p, file->f_flags & O_NONBLOCK);
873
933
static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
875
935
struct vivi_dev *dev = video_drvdata(file);
878
if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
880
ret = videobuf_streamon(&dev->vb_vidq);
884
vivi_start_generating(file);
936
return vb2_streamon(&dev->vb_vidq, i);
888
939
static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
890
941
struct vivi_dev *dev = video_drvdata(file);
893
if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
895
ret = videobuf_streamoff(&dev->vb_vidq);
897
vivi_stop_generating(file);
942
return vb2_streamoff(&dev->vb_vidq, i);
901
945
static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *i)
940
984
/* --- controls ---------------------------------------------- */
941
static int vidioc_queryctrl(struct file *file, void *priv,
942
struct v4l2_queryctrl *qc)
945
case V4L2_CID_AUDIO_VOLUME:
946
return v4l2_ctrl_query_fill(qc, 0, 255, 1, 200);
947
case V4L2_CID_BRIGHTNESS:
948
return v4l2_ctrl_query_fill(qc, 0, 255, 1, 127);
949
case V4L2_CID_CONTRAST:
950
return v4l2_ctrl_query_fill(qc, 0, 255, 1, 16);
951
case V4L2_CID_SATURATION:
952
return v4l2_ctrl_query_fill(qc, 0, 255, 1, 127);
954
return v4l2_ctrl_query_fill(qc, -128, 127, 1, 0);
959
static int vidioc_g_ctrl(struct file *file, void *priv,
960
struct v4l2_control *ctrl)
962
struct vivi_dev *dev = video_drvdata(file);
965
case V4L2_CID_AUDIO_VOLUME:
966
ctrl->value = dev->volume;
968
case V4L2_CID_BRIGHTNESS:
969
ctrl->value = dev->brightness;
971
case V4L2_CID_CONTRAST:
972
ctrl->value = dev->contrast;
974
case V4L2_CID_SATURATION:
975
ctrl->value = dev->saturation;
978
ctrl->value = dev->hue;
984
static int vidioc_s_ctrl(struct file *file, void *priv,
985
struct v4l2_control *ctrl)
987
struct vivi_dev *dev = video_drvdata(file);
988
struct v4l2_queryctrl qc;
992
err = vidioc_queryctrl(file, priv, &qc);
995
if (ctrl->value < qc.minimum || ctrl->value > qc.maximum)
998
case V4L2_CID_AUDIO_VOLUME:
999
dev->volume = ctrl->value;
1001
case V4L2_CID_BRIGHTNESS:
1002
dev->brightness = ctrl->value;
1004
case V4L2_CID_CONTRAST:
1005
dev->contrast = ctrl->value;
1007
case V4L2_CID_SATURATION:
1008
dev->saturation = ctrl->value;
1011
dev->hue = ctrl->value;
986
static int vivi_s_ctrl(struct v4l2_ctrl *ctrl)
988
struct vivi_dev *dev = container_of(ctrl->handler, struct vivi_dev, ctrl_handler);
990
if (ctrl == dev->button)
991
dev->button_pressed = 30;
1017
995
/* ------------------------------------------------------------------
1151
1201
dev->fmt = &formats[0];
1152
1202
dev->width = 640;
1153
1203
dev->height = 480;
1155
dev->brightness = 127;
1157
dev->saturation = 127;
1204
hdl = &dev->ctrl_handler;
1205
v4l2_ctrl_handler_init(hdl, 11);
1206
dev->volume = v4l2_ctrl_new_std(hdl, &vivi_ctrl_ops,
1207
V4L2_CID_AUDIO_VOLUME, 0, 255, 1, 200);
1208
dev->brightness = v4l2_ctrl_new_std(hdl, &vivi_ctrl_ops,
1209
V4L2_CID_BRIGHTNESS, 0, 255, 1, 127);
1210
dev->contrast = v4l2_ctrl_new_std(hdl, &vivi_ctrl_ops,
1211
V4L2_CID_CONTRAST, 0, 255, 1, 16);
1212
dev->saturation = v4l2_ctrl_new_std(hdl, &vivi_ctrl_ops,
1213
V4L2_CID_SATURATION, 0, 255, 1, 127);
1214
dev->hue = v4l2_ctrl_new_std(hdl, &vivi_ctrl_ops,
1215
V4L2_CID_HUE, -128, 127, 1, 0);
1216
dev->button = v4l2_ctrl_new_custom(hdl, &vivi_ctrl_button, NULL);
1217
dev->int32 = v4l2_ctrl_new_custom(hdl, &vivi_ctrl_int32, NULL);
1218
dev->int64 = v4l2_ctrl_new_custom(hdl, &vivi_ctrl_int64, NULL);
1219
dev->boolean = v4l2_ctrl_new_custom(hdl, &vivi_ctrl_boolean, NULL);
1220
dev->menu = v4l2_ctrl_new_custom(hdl, &vivi_ctrl_menu, NULL);
1221
dev->string = v4l2_ctrl_new_custom(hdl, &vivi_ctrl_string, NULL);
1226
dev->v4l2_dev.ctrl_handler = hdl;
1160
1228
/* initialize locks */
1161
1229
spin_lock_init(&dev->slock);
1231
/* initialize queue */
1233
memset(q, 0, sizeof(dev->vb_vidq));
1234
q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1235
q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_READ;
1237
q->buf_struct_size = sizeof(struct vivi_buffer);
1238
q->ops = &vivi_video_qops;
1239
q->mem_ops = &vb2_vmalloc_memops;
1162
1243
mutex_init(&dev->mutex);
1164
videobuf_queue_vmalloc_init(&dev->vb_vidq, &vivi_video_qops,
1165
NULL, &dev->slock, V4L2_BUF_TYPE_VIDEO_CAPTURE,
1166
V4L2_FIELD_INTERLACED,
1167
sizeof(struct vivi_buffer), dev, &dev->mutex);
1169
1245
/* init video dma queues */
1170
1246
INIT_LIST_HEAD(&dev->vidq.active);
1171
1247
init_waitqueue_head(&dev->vidq.wq);