2 * Virtual Video driver - This code emulates a real video device with v4l2 api
4 * Copyright (c) 2006 by:
5 * Mauro Carvalho Chehab <mchehab--a.t--infradead.org>
6 * Ted Walther <ted--a.t--enumera.com>
7 * John Sokol <sokol--a.t--videotechnology.com>
8 * http://v4l.videotechnology.com/
10 * 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
12 * as published by the Free Software Foundation; either version 2 of the
13 * License, or (at your option) any later version
15 #include <linux/module.h>
16 #include <linux/delay.h>
17 #include <linux/errno.h>
19 #include <linux/kernel.h>
20 #include <linux/slab.h>
22 #include <linux/ioport.h>
23 #include <linux/init.h>
24 #include <linux/sched.h>
25 #include <linux/pci.h>
26 #include <linux/random.h>
27 #include <linux/version.h>
28 #include <linux/mutex.h>
29 #include <linux/videodev2.h>
30 #include <linux/dma-mapping.h>
31 #ifdef CONFIG_VIDEO_V4L1_COMPAT
32 /* Include V4L1 specific functions. Should be removed soon */
33 #include <linux/videodev.h>
35 #include <linux/interrupt.h>
36 #include <media/videobuf-dma-sg.h>
37 #include <media/v4l2-common.h>
38 #include <linux/kthread.h>
39 #include <linux/highmem.h>
40 #include <linux/freezer.h>
42 /* Wake up at about 30 fps */
43 #define WAKE_NUMERATOR 30
44 #define WAKE_DENOMINATOR 1001
45 #define BUFFER_TIMEOUT msecs_to_jiffies(500) /* 0.5 seconds */
47 /* These timers are for 1 fps - used only for testing */
48 //#define WAKE_DENOMINATOR 30 /* hack for testing purposes */
49 //#define BUFFER_TIMEOUT msecs_to_jiffies(5000) /* 5 seconds */
53 #define VIVI_MAJOR_VERSION 0
54 #define VIVI_MINOR_VERSION 4
55 #define VIVI_RELEASE 0
56 #define VIVI_VERSION KERNEL_VERSION(VIVI_MAJOR_VERSION, VIVI_MINOR_VERSION, VIVI_RELEASE)
58 /* Declare static vars that will be used as parameters */
59 static unsigned int vid_limit = 16; /* Video memory limit, in Mb */
60 static struct video_device vivi; /* Video device */
61 static int video_nr = -1; /* /dev/videoN, -1 for autodetect */
63 /* supported controls */
64 static struct v4l2_queryctrl vivi_qctrl[] = {
66 .id = V4L2_CID_AUDIO_VOLUME,
71 .default_value = 65535,
73 .type = V4L2_CTRL_TYPE_INTEGER,
75 .id = V4L2_CID_BRIGHTNESS,
76 .type = V4L2_CTRL_TYPE_INTEGER,
84 .id = V4L2_CID_CONTRAST,
85 .type = V4L2_CTRL_TYPE_INTEGER,
90 .default_value = 0x10,
93 .id = V4L2_CID_SATURATION,
94 .type = V4L2_CTRL_TYPE_INTEGER,
103 .type = V4L2_CTRL_TYPE_INTEGER,
113 static int qctl_regs[ARRAY_SIZE(vivi_qctrl)];
115 #define dprintk(level,fmt, arg...) \
117 if (vivi.debug >= (level)) \
118 printk(KERN_DEBUG "vivi: " fmt , ## arg); \
121 /* ------------------------------------------------------------------
123 ------------------------------------------------------------------*/
127 u32 fourcc; /* v4l2 format id */
131 static struct vivi_fmt format = {
132 .name = "4:2:2, packed, YUYV",
133 .fourcc = V4L2_PIX_FMT_YUYV,
139 struct scatterlist *sg;
142 /* buffer for one video frame */
144 /* common v4l buffer stuff -- must be first */
145 struct videobuf_buffer vb;
147 struct vivi_fmt *fmt;
151 struct vivi_dmaqueue {
152 struct list_head active;
153 struct list_head queued;
154 struct timer_list timeout;
156 /* thread for generating video stream*/
157 struct task_struct *kthread;
158 wait_queue_head_t wq;
159 /* Counters to control fps rate */
164 static LIST_HEAD(vivi_devlist);
167 struct list_head vivi_devlist;
173 /* various device info */
174 unsigned int resources;
175 struct video_device vfd;
177 struct vivi_dmaqueue vidq;
179 /* Several counters */
180 int h,m,s,us,jiffies;
185 struct vivi_dev *dev;
188 struct vivi_fmt *fmt;
189 unsigned int width,height;
190 struct videobuf_queue vb_vidq;
192 enum v4l2_buf_type type;
195 /* ------------------------------------------------------------------
196 DMA and thread functions
197 ------------------------------------------------------------------*/
199 /* Bars and Colors should match positions */
211 static u8 bars[8][3] = {
213 {204,204,204}, /* white */
214 {208,208, 0}, /* ambar */
215 { 0,206,206}, /* cyan */
216 { 0,239, 0}, /* green */
217 {239, 0,239}, /* magenta */
218 {205, 0, 0}, /* red */
219 { 0, 0,255}, /* blue */
223 #define TO_Y(r,g,b) (((16829*r +33039*g +6416*b + 32768)>>16)+16)
224 /* RGB to V(Cr) Color transform */
225 #define TO_V(r,g,b) (((28784*r -24103*g -4681*b + 32768)>>16)+128)
226 /* RGB to U(Cb) Color transform */
227 #define TO_U(r,g,b) (((-9714*r -19070*g +28784*b + 32768)>>16)+128)
229 #define TSTAMP_MIN_Y 24
230 #define TSTAMP_MAX_Y TSTAMP_MIN_Y+15
231 #define TSTAMP_MIN_X 64
234 static void gen_line(char *basep,int inipos,int wmax,
235 int hmax, int line, char *timestr)
237 int w,i,j,pos=inipos,y;
241 /* We will just duplicate the second pixel at the packet */
244 /* Generate a standard color bar pattern */
245 for (w=0;w<wmax;w++) {
250 for (color=0;color<4;color++) {
256 *p=TO_Y(r,g,b); /* Luminance */
259 *p=TO_U(r,g,b); /* Cb */
262 *p=TO_V(r,g,b); /* Cr */
269 /* Checks if it is possible to show timestamp */
270 if (TSTAMP_MAX_Y>=hmax)
272 if (TSTAMP_MIN_X+strlen(timestr)>=wmax)
275 /* Print stream time */
276 if (line>=TSTAMP_MIN_Y && line<=TSTAMP_MAX_Y) {
278 for (s=timestr;*s;s++) {
279 chr=rom8x16_bits[(*s-0x30)*16+line-TSTAMP_MIN_Y];
281 if (chr&1<<(7-i)) { /* Font color*/
287 } else { /* Background color */
295 for (color=0;color<4;color++) {
303 *p=TO_Y(r,g,b); /* Luminance */
306 *p=TO_U(r,g,b); /* Cb */
309 *p=TO_V(r,g,b); /* Cr */
323 static void vivi_fillbuff(struct vivi_dev *dev,struct vivi_buffer *buf)
326 int hmax = buf->vb.height;
327 int wmax = buf->vb.width;
330 struct videobuf_dmabuf *dma=videobuf_to_dma(&buf->vb);
334 tmpbuf=kmalloc (wmax*2, GFP_KERNEL);
340 for (h=0;h<hmax;h++) {
342 gen_line(tmpbuf,0,wmax,hmax,h,dev->timestr);
343 /* FIXME: replacing to __copy_to_user */
344 if (copy_to_user(dma->varea+pos,tmpbuf,wmax*2)!=0)
345 dprintk(2,"vivifill copy_to_user failed.\n");
347 gen_line(tmpbuf,pos,wmax,hmax,h,dev->timestr);
352 /* Updates stream time */
354 dev->us+=jiffies_to_usecs(jiffies-dev->jiffies);
355 dev->jiffies=jiffies;
356 if (dev->us>=1000000) {
370 sprintf(dev->timestr,"%02d:%02d:%02d:%03d",
371 dev->h,dev->m,dev->s,(dev->us+500)/1000);
373 dprintk(2,"vivifill at %s: Buffer 0x%08lx size= %d\n",dev->timestr,
374 (unsigned long)dma->varea,pos);
376 /* Advice that buffer was filled */
377 buf->vb.state = STATE_DONE;
378 buf->vb.field_count++;
379 do_gettimeofday(&ts);
382 list_del(&buf->vb.queue);
383 wake_up(&buf->vb.done);
386 static int restart_video_queue(struct vivi_dmaqueue *dma_q);
388 static void vivi_thread_tick(struct vivi_dmaqueue *dma_q)
390 struct vivi_buffer *buf;
391 struct vivi_dev *dev= container_of(dma_q,struct vivi_dev,vidq);
395 /* Announces videobuf that all went ok */
396 for (bc = 0;; bc++) {
397 if (list_empty(&dma_q->active)) {
398 dprintk(1,"No active queue to serve\n");
402 buf = list_entry(dma_q->active.next,
403 struct vivi_buffer, vb.queue);
405 /* Nobody is waiting something to be done, just return */
406 if (!waitqueue_active(&buf->vb.done)) {
407 mod_timer(&dma_q->timeout, jiffies+BUFFER_TIMEOUT);
411 do_gettimeofday(&buf->vb.ts);
412 dprintk(2,"[%p/%d] wakeup\n",buf,buf->vb.i);
415 vivi_fillbuff(dev,buf);
417 if (list_empty(&dma_q->active)) {
418 del_timer(&dma_q->timeout);
420 mod_timer(&dma_q->timeout, jiffies+BUFFER_TIMEOUT);
424 dprintk(1,"%s: %d buffers handled (should be 1)\n",__FUNCTION__,bc);
427 static void vivi_sleep(struct vivi_dmaqueue *dma_q)
430 DECLARE_WAITQUEUE(wait, current);
432 dprintk(1,"%s dma_q=0x%08lx\n",__FUNCTION__,(unsigned long)dma_q);
434 add_wait_queue(&dma_q->wq, &wait);
435 if (!kthread_should_stop()) {
438 /* Calculate time to wake up */
439 timeout=dma_q->ini_jiffies+msecs_to_jiffies((dma_q->frame*WAKE_NUMERATOR*1000)/WAKE_DENOMINATOR)-jiffies;
442 int old=dma_q->frame;
443 dma_q->frame=(jiffies_to_msecs(jiffies-dma_q->ini_jiffies)*WAKE_DENOMINATOR)/(WAKE_NUMERATOR*1000)+1;
445 timeout=dma_q->ini_jiffies+msecs_to_jiffies((dma_q->frame*WAKE_NUMERATOR*1000)/WAKE_DENOMINATOR)-jiffies;
447 dprintk(1,"underrun, losed %d frames. "
448 "Now, frame is %d. Waking on %d jiffies\n",
449 dma_q->frame-old,dma_q->frame,timeout);
451 dprintk(1,"will sleep for %i jiffies\n",timeout);
453 vivi_thread_tick(dma_q);
455 schedule_timeout_interruptible (timeout);
458 remove_wait_queue(&dma_q->wq, &wait);
462 static int vivi_thread(void *data)
464 struct vivi_dmaqueue *dma_q=data;
466 dprintk(1,"thread started\n");
468 mod_timer(&dma_q->timeout, jiffies+BUFFER_TIMEOUT);
474 if (kthread_should_stop())
477 dprintk(1, "thread: exit\n");
481 static int vivi_start_thread(struct vivi_dmaqueue *dma_q)
484 dma_q->ini_jiffies=jiffies;
486 dprintk(1,"%s\n",__FUNCTION__);
488 dma_q->kthread = kthread_run(vivi_thread, dma_q, "vivi");
490 if (IS_ERR(dma_q->kthread)) {
491 printk(KERN_ERR "vivi: kernel_thread() failed\n");
492 return PTR_ERR(dma_q->kthread);
495 wake_up_interruptible(&dma_q->wq);
497 dprintk(1,"returning from %s\n",__FUNCTION__);
501 static void vivi_stop_thread(struct vivi_dmaqueue *dma_q)
503 dprintk(1,"%s\n",__FUNCTION__);
504 /* shutdown control thread */
505 if (dma_q->kthread) {
506 kthread_stop(dma_q->kthread);
511 static int restart_video_queue(struct vivi_dmaqueue *dma_q)
513 struct vivi_buffer *buf, *prev;
514 struct list_head *item;
516 dprintk(1,"%s dma_q=0x%08lx\n",__FUNCTION__,(unsigned long)dma_q);
518 if (!list_empty(&dma_q->active)) {
519 buf = list_entry(dma_q->active.next, struct vivi_buffer, vb.queue);
520 dprintk(2,"restart_queue [%p/%d]: restart dma\n",
523 dprintk(1,"Restarting video dma\n");
524 vivi_stop_thread(dma_q);
525 // vivi_start_thread(dma_q);
527 /* cancel all outstanding capture / vbi requests */
528 list_for_each(item,&dma_q->active) {
529 buf = list_entry(item, struct vivi_buffer, vb.queue);
531 list_del(&buf->vb.queue);
532 buf->vb.state = STATE_ERROR;
533 wake_up(&buf->vb.done);
535 mod_timer(&dma_q->timeout, jiffies+BUFFER_TIMEOUT);
542 if (list_empty(&dma_q->queued))
544 buf = list_entry(dma_q->queued.next, struct vivi_buffer, vb.queue);
546 list_del(&buf->vb.queue);
547 list_add_tail(&buf->vb.queue,&dma_q->active);
549 dprintk(1,"Restarting video dma\n");
550 vivi_stop_thread(dma_q);
551 vivi_start_thread(dma_q);
553 buf->vb.state = STATE_ACTIVE;
554 mod_timer(&dma_q->timeout, jiffies+BUFFER_TIMEOUT);
555 dprintk(2,"[%p/%d] restart_queue - first active\n",
558 } else if (prev->vb.width == buf->vb.width &&
559 prev->vb.height == buf->vb.height &&
560 prev->fmt == buf->fmt) {
561 list_del(&buf->vb.queue);
562 list_add_tail(&buf->vb.queue,&dma_q->active);
563 buf->vb.state = STATE_ACTIVE;
564 dprintk(2,"[%p/%d] restart_queue - move to active\n",
573 static void vivi_vid_timeout(unsigned long data)
575 struct vivi_dev *dev = (struct vivi_dev*)data;
576 struct vivi_dmaqueue *vidq = &dev->vidq;
577 struct vivi_buffer *buf;
579 while (!list_empty(&vidq->active)) {
580 buf = list_entry(vidq->active.next, struct vivi_buffer, vb.queue);
581 list_del(&buf->vb.queue);
582 buf->vb.state = STATE_ERROR;
583 wake_up(&buf->vb.done);
584 printk("vivi/0: [%p/%d] timeout\n", buf, buf->vb.i);
587 restart_video_queue(vidq);
590 /* ------------------------------------------------------------------
592 ------------------------------------------------------------------*/
594 buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size)
596 struct vivi_fh *fh = vq->priv_data;
598 *size = fh->width*fh->height*2;
603 while (*size * *count > vid_limit * 1024 * 1024)
606 dprintk(1,"%s, count=%d, size=%d\n",__FUNCTION__,*count, *size);
611 static void free_buffer(struct videobuf_queue *vq, struct vivi_buffer *buf)
613 struct videobuf_dmabuf *dma=videobuf_to_dma(&buf->vb);
615 dprintk(1,"%s\n",__FUNCTION__);
621 videobuf_waiton(&buf->vb,0,0);
622 videobuf_dma_unmap(vq, dma);
623 videobuf_dma_free(dma);
624 buf->vb.state = STATE_NEEDS_INIT;
627 #define norm_maxw() 1024
628 #define norm_maxh() 768
630 buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
631 enum v4l2_field field)
633 struct vivi_fh *fh = vq->priv_data;
634 struct vivi_buffer *buf = container_of(vb,struct vivi_buffer,vb);
635 int rc, init_buffer = 0;
637 dprintk(1,"%s, field=%d\n",__FUNCTION__,field);
639 BUG_ON(NULL == fh->fmt);
640 if (fh->width < 48 || fh->width > norm_maxw() ||
641 fh->height < 32 || fh->height > norm_maxh())
643 buf->vb.size = fh->width*fh->height*2;
644 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
647 if (buf->fmt != fh->fmt ||
648 buf->vb.width != fh->width ||
649 buf->vb.height != fh->height ||
650 buf->vb.field != field) {
652 buf->vb.width = fh->width;
653 buf->vb.height = fh->height;
654 buf->vb.field = field;
658 if (STATE_NEEDS_INIT == buf->vb.state) {
659 if (0 != (rc = videobuf_iolock(vq,&buf->vb,NULL)))
663 buf->vb.state = STATE_PREPARED;
673 buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
675 struct vivi_buffer *buf = container_of(vb,struct vivi_buffer,vb);
676 struct vivi_fh *fh = vq->priv_data;
677 struct vivi_dev *dev = fh->dev;
678 struct vivi_dmaqueue *vidq = &dev->vidq;
679 struct vivi_buffer *prev;
681 if (!list_empty(&vidq->queued)) {
682 dprintk(1,"adding vb queue=0x%08lx\n",(unsigned long)&buf->vb.queue);
683 list_add_tail(&buf->vb.queue,&vidq->queued);
684 buf->vb.state = STATE_QUEUED;
685 dprintk(2,"[%p/%d] buffer_queue - append to queued\n",
687 } else if (list_empty(&vidq->active)) {
688 list_add_tail(&buf->vb.queue,&vidq->active);
690 buf->vb.state = STATE_ACTIVE;
691 mod_timer(&vidq->timeout, jiffies+BUFFER_TIMEOUT);
692 dprintk(2,"[%p/%d] buffer_queue - first active\n",
695 vivi_start_thread(vidq);
697 prev = list_entry(vidq->active.prev, struct vivi_buffer, vb.queue);
698 if (prev->vb.width == buf->vb.width &&
699 prev->vb.height == buf->vb.height &&
700 prev->fmt == buf->fmt) {
701 list_add_tail(&buf->vb.queue,&vidq->active);
702 buf->vb.state = STATE_ACTIVE;
703 dprintk(2,"[%p/%d] buffer_queue - append to active\n",
707 list_add_tail(&buf->vb.queue,&vidq->queued);
708 buf->vb.state = STATE_QUEUED;
709 dprintk(2,"[%p/%d] buffer_queue - first queued\n",
715 static void buffer_release(struct videobuf_queue *vq, struct videobuf_buffer *vb)
717 struct vivi_buffer *buf = container_of(vb,struct vivi_buffer,vb);
718 struct vivi_fh *fh = vq->priv_data;
719 struct vivi_dev *dev = (struct vivi_dev*)fh->dev;
720 struct vivi_dmaqueue *vidq = &dev->vidq;
722 dprintk(1,"%s\n",__FUNCTION__);
724 vivi_stop_thread(vidq);
730 static struct videobuf_queue_ops vivi_video_qops = {
731 .buf_setup = buffer_setup,
732 .buf_prepare = buffer_prepare,
733 .buf_queue = buffer_queue,
734 .buf_release = buffer_release,
737 /* ------------------------------------------------------------------
739 ------------------------------------------------------------------*/
742 static int res_get(struct vivi_dev *dev, struct vivi_fh *fh)
745 mutex_lock(&dev->lock);
746 if (dev->resources) {
747 /* no, someone else uses it */
748 mutex_unlock(&dev->lock);
751 /* it's free, grab it */
753 dprintk(1,"res: get\n");
754 mutex_unlock(&dev->lock);
758 static int res_locked(struct vivi_dev *dev)
760 return (dev->resources);
763 static void res_free(struct vivi_dev *dev, struct vivi_fh *fh)
765 mutex_lock(&dev->lock);
767 dprintk(1,"res: put\n");
768 mutex_lock(&dev->lock);
771 /* ------------------------------------------------------------------
772 IOCTL vidioc handling
773 ------------------------------------------------------------------*/
774 static int vidioc_querycap (struct file *file, void *priv,
775 struct v4l2_capability *cap)
777 strcpy(cap->driver, "vivi");
778 strcpy(cap->card, "vivi");
779 cap->version = VIVI_VERSION;
780 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE |
786 static int vidioc_enum_fmt_cap (struct file *file, void *priv,
787 struct v4l2_fmtdesc *f)
792 strlcpy(f->description,format.name,sizeof(f->description));
793 f->pixelformat = format.fourcc;
797 static int vidioc_g_fmt_cap (struct file *file, void *priv,
798 struct v4l2_format *f)
800 struct vivi_fh *fh=priv;
802 f->fmt.pix.width = fh->width;
803 f->fmt.pix.height = fh->height;
804 f->fmt.pix.field = fh->vb_vidq.field;
805 f->fmt.pix.pixelformat = fh->fmt->fourcc;
806 f->fmt.pix.bytesperline =
807 (f->fmt.pix.width * fh->fmt->depth) >> 3;
808 f->fmt.pix.sizeimage =
809 f->fmt.pix.height * f->fmt.pix.bytesperline;
814 static int vidioc_try_fmt_cap (struct file *file, void *priv,
815 struct v4l2_format *f)
817 struct vivi_fmt *fmt;
818 enum v4l2_field field;
819 unsigned int maxw, maxh;
821 if (format.fourcc != f->fmt.pix.pixelformat) {
822 dprintk(1,"Fourcc format (0x%08x) invalid. Driver accepts "
823 "only 0x%08x\n",f->fmt.pix.pixelformat,format.fourcc);
828 field = f->fmt.pix.field;
830 if (field == V4L2_FIELD_ANY) {
831 // field=V4L2_FIELD_INTERLACED;
832 field=V4L2_FIELD_SEQ_TB;
833 } else if (V4L2_FIELD_INTERLACED != field) {
834 dprintk(1,"Field type invalid.\n");
841 f->fmt.pix.field = field;
842 if (f->fmt.pix.height < 32)
843 f->fmt.pix.height = 32;
844 if (f->fmt.pix.height > maxh)
845 f->fmt.pix.height = maxh;
846 if (f->fmt.pix.width < 48)
847 f->fmt.pix.width = 48;
848 if (f->fmt.pix.width > maxw)
849 f->fmt.pix.width = maxw;
850 f->fmt.pix.width &= ~0x03;
851 f->fmt.pix.bytesperline =
852 (f->fmt.pix.width * fmt->depth) >> 3;
853 f->fmt.pix.sizeimage =
854 f->fmt.pix.height * f->fmt.pix.bytesperline;
859 /*FIXME: This seems to be generic enough to be at videodev2 */
860 static int vidioc_s_fmt_cap (struct file *file, void *priv,
861 struct v4l2_format *f)
863 struct vivi_fh *fh=priv;
864 int ret = vidioc_try_fmt_cap(file,fh,f);
869 fh->width = f->fmt.pix.width;
870 fh->height = f->fmt.pix.height;
871 fh->vb_vidq.field = f->fmt.pix.field;
877 static int vidioc_reqbufs (struct file *file, void *priv, struct v4l2_requestbuffers *p)
879 struct vivi_fh *fh=priv;
881 return (videobuf_reqbufs(&fh->vb_vidq, p));
884 static int vidioc_querybuf (struct file *file, void *priv, struct v4l2_buffer *p)
886 struct vivi_fh *fh=priv;
888 return (videobuf_querybuf(&fh->vb_vidq, p));
891 static int vidioc_qbuf (struct file *file, void *priv, struct v4l2_buffer *p)
893 struct vivi_fh *fh=priv;
895 return (videobuf_qbuf(&fh->vb_vidq, p));
898 static int vidioc_dqbuf (struct file *file, void *priv, struct v4l2_buffer *p)
900 struct vivi_fh *fh=priv;
902 return (videobuf_dqbuf(&fh->vb_vidq, p,
903 file->f_flags & O_NONBLOCK));
906 #ifdef CONFIG_VIDEO_V4L1_COMPAT
907 static int vidiocgmbuf (struct file *file, void *priv, struct video_mbuf *mbuf)
909 struct vivi_fh *fh=priv;
911 return videobuf_cgmbuf (&fh->vb_vidq, mbuf, 8);
915 static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
917 struct vivi_fh *fh=priv;
918 struct vivi_dev *dev = fh->dev;
920 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
925 if (!res_get(dev,fh))
927 return (videobuf_streamon(&fh->vb_vidq));
930 static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
932 struct vivi_fh *fh=priv;
933 struct vivi_dev *dev = fh->dev;
935 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
940 videobuf_streamoff(&fh->vb_vidq);
946 static int vidioc_s_std (struct file *file, void *priv, v4l2_std_id *i)
951 /* only one input in this sample driver */
952 static int vidioc_enum_input (struct file *file, void *priv,
953 struct v4l2_input *inp)
958 inp->type = V4L2_INPUT_TYPE_CAMERA;
959 inp->std = V4L2_STD_NTSC_M;
960 strcpy(inp->name,"Camera");
965 static int vidioc_g_input (struct file *file, void *priv, unsigned int *i)
971 static int vidioc_s_input (struct file *file, void *priv, unsigned int i)
979 /* --- controls ---------------------------------------------- */
980 static int vidioc_queryctrl (struct file *file, void *priv,
981 struct v4l2_queryctrl *qc)
985 for (i = 0; i < ARRAY_SIZE(vivi_qctrl); i++)
986 if (qc->id && qc->id == vivi_qctrl[i].id) {
987 memcpy(qc, &(vivi_qctrl[i]),
995 static int vidioc_g_ctrl (struct file *file, void *priv,
996 struct v4l2_control *ctrl)
1000 for (i = 0; i < ARRAY_SIZE(vivi_qctrl); i++)
1001 if (ctrl->id == vivi_qctrl[i].id) {
1002 ctrl->value=qctl_regs[i];
1008 static int vidioc_s_ctrl (struct file *file, void *priv,
1009 struct v4l2_control *ctrl)
1013 for (i = 0; i < ARRAY_SIZE(vivi_qctrl); i++)
1014 if (ctrl->id == vivi_qctrl[i].id) {
1016 vivi_qctrl[i].minimum
1018 vivi_qctrl[i].maximum) {
1021 qctl_regs[i]=ctrl->value;
1027 /* ------------------------------------------------------------------
1028 File operations for the device
1029 ------------------------------------------------------------------*/
1031 #define line_buf_size(norm) (norm_maxw(norm)*(format.depth+7)/8)
1033 static int vivi_open(struct inode *inode, struct file *file)
1035 int minor = iminor(inode);
1036 struct vivi_dev *h,*dev = NULL;
1038 struct list_head *list;
1039 enum v4l2_buf_type type = 0;
1042 printk(KERN_DEBUG "vivi: open called (minor=%d)\n",minor);
1044 list_for_each(list,&vivi_devlist) {
1045 h = list_entry(list, struct vivi_dev, vivi_devlist);
1046 if (h->vfd.minor == minor) {
1048 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1056 /* If more than one user, mutex should be added */
1059 dprintk(1,"open minor=%d type=%s users=%d\n",
1060 minor,v4l2_type_names[type],dev->users);
1062 /* allocate + initialize per filehandle data */
1063 fh = kzalloc(sizeof(*fh),GFP_KERNEL);
1069 file->private_data = fh;
1072 fh->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1077 /* Put all controls at a sane state */
1078 for (i = 0; i < ARRAY_SIZE(vivi_qctrl); i++)
1079 qctl_regs[i] =vivi_qctrl[i].default_value;
1081 dprintk(1,"Open: fh=0x%08lx, dev=0x%08lx, dev->vidq=0x%08lx\n",
1082 (unsigned long)fh,(unsigned long)dev,(unsigned long)&dev->vidq);
1083 dprintk(1,"Open: list_empty queued=%d\n",list_empty(&dev->vidq.queued));
1084 dprintk(1,"Open: list_empty active=%d\n",list_empty(&dev->vidq.active));
1086 /* Resets frame counters */
1091 dev->jiffies=jiffies;
1092 sprintf(dev->timestr,"%02d:%02d:%02d:%03d",
1093 dev->h,dev->m,dev->s,(dev->us+500)/1000);
1095 videobuf_queue_pci_init(&fh->vb_vidq, &vivi_video_qops,
1098 V4L2_FIELD_INTERLACED,
1099 sizeof(struct vivi_buffer),fh);
1106 vivi_read(struct file *file, char __user *data, size_t count, loff_t *ppos)
1108 struct vivi_fh *fh = file->private_data;
1110 if (fh->type==V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1111 if (res_locked(fh->dev))
1113 return videobuf_read_stream(&fh->vb_vidq, data, count, ppos, 0,
1114 file->f_flags & O_NONBLOCK);
1120 vivi_poll(struct file *file, struct poll_table_struct *wait)
1122 struct vivi_fh *fh = file->private_data;
1123 struct vivi_buffer *buf;
1125 dprintk(1,"%s\n",__FUNCTION__);
1127 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != fh->type)
1130 if (res_get(fh->dev,fh)) {
1131 dprintk(1,"poll: mmap interface\n");
1132 /* streaming capture */
1133 if (list_empty(&fh->vb_vidq.stream))
1135 buf = list_entry(fh->vb_vidq.stream.next,struct vivi_buffer,vb.stream);
1137 dprintk(1,"poll: read() interface\n");
1138 /* read() capture */
1139 buf = (struct vivi_buffer*)fh->vb_vidq.read_buf;
1143 poll_wait(file, &buf->vb.done, wait);
1144 if (buf->vb.state == STATE_DONE ||
1145 buf->vb.state == STATE_ERROR)
1146 return POLLIN|POLLRDNORM;
1150 static int vivi_release(struct inode *inode, struct file *file)
1152 struct vivi_fh *fh = file->private_data;
1153 struct vivi_dev *dev = fh->dev;
1154 struct vivi_dmaqueue *vidq = &dev->vidq;
1156 int minor = iminor(inode);
1158 vivi_stop_thread(vidq);
1159 videobuf_mmap_free(&fh->vb_vidq);
1165 printk(KERN_DEBUG "vivi: close called (minor=%d, users=%d)\n",minor,dev->users);
1171 vivi_mmap(struct file *file, struct vm_area_struct * vma)
1173 struct vivi_fh *fh = file->private_data;
1176 dprintk (1,"mmap called, vma=0x%08lx\n",(unsigned long)vma);
1178 ret=videobuf_mmap_mapper(&fh->vb_vidq, vma);
1180 dprintk (1,"vma start=0x%08lx, size=%ld, ret=%d\n",
1181 (unsigned long)vma->vm_start,
1182 (unsigned long)vma->vm_end-(unsigned long)vma->vm_start,
1188 static const struct file_operations vivi_fops = {
1189 .owner = THIS_MODULE,
1191 .release = vivi_release,
1194 .ioctl = video_ioctl2, /* V4L2 ioctl handler */
1196 .llseek = no_llseek,
1199 static struct video_device vivi = {
1201 .type = VID_TYPE_CAPTURE,
1205 // .release = video_device_release,
1207 .vidioc_querycap = vidioc_querycap,
1208 .vidioc_enum_fmt_cap = vidioc_enum_fmt_cap,
1209 .vidioc_g_fmt_cap = vidioc_g_fmt_cap,
1210 .vidioc_try_fmt_cap = vidioc_try_fmt_cap,
1211 .vidioc_s_fmt_cap = vidioc_s_fmt_cap,
1212 .vidioc_reqbufs = vidioc_reqbufs,
1213 .vidioc_querybuf = vidioc_querybuf,
1214 .vidioc_qbuf = vidioc_qbuf,
1215 .vidioc_dqbuf = vidioc_dqbuf,
1216 .vidioc_s_std = vidioc_s_std,
1217 .vidioc_enum_input = vidioc_enum_input,
1218 .vidioc_g_input = vidioc_g_input,
1219 .vidioc_s_input = vidioc_s_input,
1220 .vidioc_queryctrl = vidioc_queryctrl,
1221 .vidioc_g_ctrl = vidioc_g_ctrl,
1222 .vidioc_s_ctrl = vidioc_s_ctrl,
1223 .vidioc_streamon = vidioc_streamon,
1224 .vidioc_streamoff = vidioc_streamoff,
1225 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1226 .vidiocgmbuf = vidiocgmbuf,
1228 .tvnorms = V4L2_STD_NTSC_M,
1229 .current_norm = V4L2_STD_NTSC_M,
1231 /* -----------------------------------------------------------------
1232 Initialization and module stuff
1233 ------------------------------------------------------------------*/
1235 static int __init vivi_init(void)
1238 struct vivi_dev *dev;
1240 dev = kzalloc(sizeof(*dev),GFP_KERNEL);
1243 list_add_tail(&dev->vivi_devlist,&vivi_devlist);
1245 /* init video dma queues */
1246 INIT_LIST_HEAD(&dev->vidq.active);
1247 INIT_LIST_HEAD(&dev->vidq.queued);
1248 init_waitqueue_head(&dev->vidq.wq);
1250 /* initialize locks */
1251 mutex_init(&dev->lock);
1253 dev->vidq.timeout.function = vivi_vid_timeout;
1254 dev->vidq.timeout.data = (unsigned long)dev;
1255 init_timer(&dev->vidq.timeout);
1257 ret = video_register_device(&vivi, VFL_TYPE_GRABBER, video_nr);
1258 printk(KERN_INFO "Video Technology Magazine Virtual Video Capture Board (Load status: %d)\n", ret);
1262 static void __exit vivi_exit(void)
1265 struct list_head *list;
1267 while (!list_empty(&vivi_devlist)) {
1268 list = vivi_devlist.next;
1270 h = list_entry(list, struct vivi_dev, vivi_devlist);
1273 video_unregister_device(&vivi);
1276 module_init(vivi_init);
1277 module_exit(vivi_exit);
1279 MODULE_DESCRIPTION("Video Technology Magazine Virtual Video Capture Board");
1280 MODULE_AUTHOR("Mauro Carvalho Chehab, Ted Walther and John Sokol");
1281 MODULE_LICENSE("Dual BSD/GPL");
1283 module_param(video_nr, int, 0);
1285 module_param_named(debug,vivi.debug, int, 0644);
1286 MODULE_PARM_DESC(debug,"activates debug info");
1288 module_param(vid_limit,int,0644);
1289 MODULE_PARM_DESC(vid_limit,"capture memory limit in megabytes");