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/video-buf.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;
331 if (buf->vb.dma.varea) {
332 tmpbuf=kmalloc (wmax*2, GFP_KERNEL);
334 tmpbuf=buf->vb.dma.vmalloc;
338 for (h=0;h<hmax;h++) {
339 if (buf->vb.dma.varea) {
340 gen_line(tmpbuf,0,wmax,hmax,h,dev->timestr);
341 /* FIXME: replacing to __copy_to_user */
342 if (copy_to_user(buf->vb.dma.varea+pos,tmpbuf,wmax*2)!=0)
343 dprintk(2,"vivifill copy_to_user failed.\n");
345 gen_line(tmpbuf,pos,wmax,hmax,h,dev->timestr);
350 /* Updates stream time */
352 dev->us+=jiffies_to_usecs(jiffies-dev->jiffies);
353 dev->jiffies=jiffies;
354 if (dev->us>=1000000) {
368 sprintf(dev->timestr,"%02d:%02d:%02d:%03d",
369 dev->h,dev->m,dev->s,(dev->us+500)/1000);
371 dprintk(2,"vivifill at %s: Buffer 0x%08lx size= %d\n",dev->timestr,
372 (unsigned long)buf->vb.dma.varea,pos);
374 /* Advice that buffer was filled */
375 buf->vb.state = STATE_DONE;
376 buf->vb.field_count++;
377 do_gettimeofday(&ts);
380 list_del(&buf->vb.queue);
381 wake_up(&buf->vb.done);
384 static int restart_video_queue(struct vivi_dmaqueue *dma_q);
386 static void vivi_thread_tick(struct vivi_dmaqueue *dma_q)
388 struct vivi_buffer *buf;
389 struct vivi_dev *dev= container_of(dma_q,struct vivi_dev,vidq);
393 /* Announces videobuf that all went ok */
394 for (bc = 0;; bc++) {
395 if (list_empty(&dma_q->active)) {
396 dprintk(1,"No active queue to serve\n");
400 buf = list_entry(dma_q->active.next,
401 struct vivi_buffer, vb.queue);
403 /* Nobody is waiting something to be done, just return */
404 if (!waitqueue_active(&buf->vb.done)) {
405 mod_timer(&dma_q->timeout, jiffies+BUFFER_TIMEOUT);
409 do_gettimeofday(&buf->vb.ts);
410 dprintk(2,"[%p/%d] wakeup\n",buf,buf->vb.i);
413 vivi_fillbuff(dev,buf);
415 if (list_empty(&dma_q->active)) {
416 del_timer(&dma_q->timeout);
418 mod_timer(&dma_q->timeout, jiffies+BUFFER_TIMEOUT);
422 dprintk(1,"%s: %d buffers handled (should be 1)\n",__FUNCTION__,bc);
425 static void vivi_sleep(struct vivi_dmaqueue *dma_q)
428 DECLARE_WAITQUEUE(wait, current);
430 dprintk(1,"%s dma_q=0x%08lx\n",__FUNCTION__,(unsigned long)dma_q);
432 add_wait_queue(&dma_q->wq, &wait);
433 if (!kthread_should_stop()) {
436 /* Calculate time to wake up */
437 timeout=dma_q->ini_jiffies+msecs_to_jiffies((dma_q->frame*WAKE_NUMERATOR*1000)/WAKE_DENOMINATOR)-jiffies;
440 int old=dma_q->frame;
441 dma_q->frame=(jiffies_to_msecs(jiffies-dma_q->ini_jiffies)*WAKE_DENOMINATOR)/(WAKE_NUMERATOR*1000)+1;
443 timeout=dma_q->ini_jiffies+msecs_to_jiffies((dma_q->frame*WAKE_NUMERATOR*1000)/WAKE_DENOMINATOR)-jiffies;
445 dprintk(1,"underrun, losed %d frames. "
446 "Now, frame is %d. Waking on %d jiffies\n",
447 dma_q->frame-old,dma_q->frame,timeout);
449 dprintk(1,"will sleep for %i jiffies\n",timeout);
451 vivi_thread_tick(dma_q);
453 schedule_timeout_interruptible (timeout);
456 remove_wait_queue(&dma_q->wq, &wait);
460 static int vivi_thread(void *data)
462 struct vivi_dmaqueue *dma_q=data;
464 dprintk(1,"thread started\n");
466 mod_timer(&dma_q->timeout, jiffies+BUFFER_TIMEOUT);
472 if (kthread_should_stop())
475 dprintk(1, "thread: exit\n");
479 static int vivi_start_thread(struct vivi_dmaqueue *dma_q)
482 dma_q->ini_jiffies=jiffies;
484 dprintk(1,"%s\n",__FUNCTION__);
486 dma_q->kthread = kthread_run(vivi_thread, dma_q, "vivi");
488 if (IS_ERR(dma_q->kthread)) {
489 printk(KERN_ERR "vivi: kernel_thread() failed\n");
490 return PTR_ERR(dma_q->kthread);
493 wake_up_interruptible(&dma_q->wq);
495 dprintk(1,"returning from %s\n",__FUNCTION__);
499 static void vivi_stop_thread(struct vivi_dmaqueue *dma_q)
501 dprintk(1,"%s\n",__FUNCTION__);
502 /* shutdown control thread */
503 if (dma_q->kthread) {
504 kthread_stop(dma_q->kthread);
509 static int restart_video_queue(struct vivi_dmaqueue *dma_q)
511 struct vivi_buffer *buf, *prev;
512 struct list_head *item;
514 dprintk(1,"%s dma_q=0x%08lx\n",__FUNCTION__,(unsigned long)dma_q);
516 if (!list_empty(&dma_q->active)) {
517 buf = list_entry(dma_q->active.next, struct vivi_buffer, vb.queue);
518 dprintk(2,"restart_queue [%p/%d]: restart dma\n",
521 dprintk(1,"Restarting video dma\n");
522 vivi_stop_thread(dma_q);
523 // vivi_start_thread(dma_q);
525 /* cancel all outstanding capture / vbi requests */
526 list_for_each(item,&dma_q->active) {
527 buf = list_entry(item, struct vivi_buffer, vb.queue);
529 list_del(&buf->vb.queue);
530 buf->vb.state = STATE_ERROR;
531 wake_up(&buf->vb.done);
533 mod_timer(&dma_q->timeout, jiffies+BUFFER_TIMEOUT);
540 if (list_empty(&dma_q->queued))
542 buf = list_entry(dma_q->queued.next, struct vivi_buffer, vb.queue);
544 list_del(&buf->vb.queue);
545 list_add_tail(&buf->vb.queue,&dma_q->active);
547 dprintk(1,"Restarting video dma\n");
548 vivi_stop_thread(dma_q);
549 vivi_start_thread(dma_q);
551 buf->vb.state = STATE_ACTIVE;
552 mod_timer(&dma_q->timeout, jiffies+BUFFER_TIMEOUT);
553 dprintk(2,"[%p/%d] restart_queue - first active\n",
556 } else if (prev->vb.width == buf->vb.width &&
557 prev->vb.height == buf->vb.height &&
558 prev->fmt == buf->fmt) {
559 list_del(&buf->vb.queue);
560 list_add_tail(&buf->vb.queue,&dma_q->active);
561 buf->vb.state = STATE_ACTIVE;
562 dprintk(2,"[%p/%d] restart_queue - move to active\n",
571 static void vivi_vid_timeout(unsigned long data)
573 struct vivi_dev *dev = (struct vivi_dev*)data;
574 struct vivi_dmaqueue *vidq = &dev->vidq;
575 struct vivi_buffer *buf;
577 while (!list_empty(&vidq->active)) {
578 buf = list_entry(vidq->active.next, struct vivi_buffer, vb.queue);
579 list_del(&buf->vb.queue);
580 buf->vb.state = STATE_ERROR;
581 wake_up(&buf->vb.done);
582 printk("vivi/0: [%p/%d] timeout\n", buf, buf->vb.i);
585 restart_video_queue(vidq);
588 /* ------------------------------------------------------------------
590 ------------------------------------------------------------------*/
592 buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size)
594 struct vivi_fh *fh = vq->priv_data;
596 *size = fh->width*fh->height*2;
600 while (*size * *count > vid_limit * 1024 * 1024)
605 static void free_buffer(struct videobuf_queue *vq, struct vivi_buffer *buf)
607 dprintk(1,"%s\n",__FUNCTION__);
613 videobuf_waiton(&buf->vb,0,0);
614 videobuf_dma_unmap(vq, &buf->vb.dma);
615 videobuf_dma_free(&buf->vb.dma);
616 buf->vb.state = STATE_NEEDS_INIT;
619 #define norm_maxw() 1024
620 #define norm_maxh() 768
622 buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
623 enum v4l2_field field)
625 struct vivi_fh *fh = vq->priv_data;
626 struct vivi_buffer *buf = container_of(vb,struct vivi_buffer,vb);
627 int rc, init_buffer = 0;
629 // dprintk(1,"%s, field=%d\n",__FUNCTION__,field);
631 BUG_ON(NULL == fh->fmt);
632 if (fh->width < 48 || fh->width > norm_maxw() ||
633 fh->height < 32 || fh->height > norm_maxh())
635 buf->vb.size = fh->width*fh->height*2;
636 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
639 if (buf->fmt != fh->fmt ||
640 buf->vb.width != fh->width ||
641 buf->vb.height != fh->height ||
642 buf->vb.field != field) {
644 buf->vb.width = fh->width;
645 buf->vb.height = fh->height;
646 buf->vb.field = field;
650 if (STATE_NEEDS_INIT == buf->vb.state) {
651 if (0 != (rc = videobuf_iolock(vq,&buf->vb,NULL)))
655 buf->vb.state = STATE_PREPARED;
665 buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
667 struct vivi_buffer *buf = container_of(vb,struct vivi_buffer,vb);
668 struct vivi_fh *fh = vq->priv_data;
669 struct vivi_dev *dev = fh->dev;
670 struct vivi_dmaqueue *vidq = &dev->vidq;
671 struct vivi_buffer *prev;
673 if (!list_empty(&vidq->queued)) {
674 dprintk(1,"adding vb queue=0x%08lx\n",(unsigned long)&buf->vb.queue);
675 list_add_tail(&buf->vb.queue,&vidq->queued);
676 buf->vb.state = STATE_QUEUED;
677 dprintk(2,"[%p/%d] buffer_queue - append to queued\n",
679 } else if (list_empty(&vidq->active)) {
680 list_add_tail(&buf->vb.queue,&vidq->active);
682 buf->vb.state = STATE_ACTIVE;
683 mod_timer(&vidq->timeout, jiffies+BUFFER_TIMEOUT);
684 dprintk(2,"[%p/%d] buffer_queue - first active\n",
687 vivi_start_thread(vidq);
689 prev = list_entry(vidq->active.prev, struct vivi_buffer, vb.queue);
690 if (prev->vb.width == buf->vb.width &&
691 prev->vb.height == buf->vb.height &&
692 prev->fmt == buf->fmt) {
693 list_add_tail(&buf->vb.queue,&vidq->active);
694 buf->vb.state = STATE_ACTIVE;
695 dprintk(2,"[%p/%d] buffer_queue - append to active\n",
699 list_add_tail(&buf->vb.queue,&vidq->queued);
700 buf->vb.state = STATE_QUEUED;
701 dprintk(2,"[%p/%d] buffer_queue - first queued\n",
707 static void buffer_release(struct videobuf_queue *vq, struct videobuf_buffer *vb)
709 struct vivi_buffer *buf = container_of(vb,struct vivi_buffer,vb);
710 struct vivi_fh *fh = vq->priv_data;
711 struct vivi_dev *dev = (struct vivi_dev*)fh->dev;
712 struct vivi_dmaqueue *vidq = &dev->vidq;
714 dprintk(1,"%s\n",__FUNCTION__);
716 vivi_stop_thread(vidq);
722 static struct videobuf_queue_ops vivi_video_qops = {
723 .buf_setup = buffer_setup,
724 .buf_prepare = buffer_prepare,
725 .buf_queue = buffer_queue,
726 .buf_release = buffer_release,
728 /* Non-pci handling routines */
729 // .vb_map_sg = vivi_map_sg,
730 // .vb_dma_sync_sg = vivi_dma_sync_sg,
731 // .vb_unmap_sg = vivi_unmap_sg,
734 /* ------------------------------------------------------------------
736 ------------------------------------------------------------------*/
739 static int res_get(struct vivi_dev *dev, struct vivi_fh *fh)
742 mutex_lock(&dev->lock);
743 if (dev->resources) {
744 /* no, someone else uses it */
745 mutex_unlock(&dev->lock);
748 /* it's free, grab it */
750 dprintk(1,"res: get\n");
751 mutex_unlock(&dev->lock);
755 static int res_locked(struct vivi_dev *dev)
757 return (dev->resources);
760 static void res_free(struct vivi_dev *dev, struct vivi_fh *fh)
762 mutex_lock(&dev->lock);
764 dprintk(1,"res: put\n");
765 mutex_lock(&dev->lock);
768 /* ------------------------------------------------------------------
769 IOCTL vidioc handling
770 ------------------------------------------------------------------*/
771 static int vidioc_querycap (struct file *file, void *priv,
772 struct v4l2_capability *cap)
774 strcpy(cap->driver, "vivi");
775 strcpy(cap->card, "vivi");
776 cap->version = VIVI_VERSION;
777 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE |
783 static int vidioc_enum_fmt_cap (struct file *file, void *priv,
784 struct v4l2_fmtdesc *f)
789 strlcpy(f->description,format.name,sizeof(f->description));
790 f->pixelformat = format.fourcc;
794 static int vidioc_g_fmt_cap (struct file *file, void *priv,
795 struct v4l2_format *f)
797 struct vivi_fh *fh=priv;
799 f->fmt.pix.width = fh->width;
800 f->fmt.pix.height = fh->height;
801 f->fmt.pix.field = fh->vb_vidq.field;
802 f->fmt.pix.pixelformat = fh->fmt->fourcc;
803 f->fmt.pix.bytesperline =
804 (f->fmt.pix.width * fh->fmt->depth) >> 3;
805 f->fmt.pix.sizeimage =
806 f->fmt.pix.height * f->fmt.pix.bytesperline;
811 static int vidioc_try_fmt_cap (struct file *file, void *priv,
812 struct v4l2_format *f)
814 struct vivi_fmt *fmt;
815 enum v4l2_field field;
816 unsigned int maxw, maxh;
818 if (format.fourcc != f->fmt.pix.pixelformat) {
819 dprintk(1,"Fourcc format (0x%08x) invalid. Driver accepts "
820 "only 0x%08x\n",f->fmt.pix.pixelformat,format.fourcc);
825 field = f->fmt.pix.field;
827 if (field == V4L2_FIELD_ANY) {
828 // field=V4L2_FIELD_INTERLACED;
829 field=V4L2_FIELD_SEQ_TB;
830 } else if (V4L2_FIELD_INTERLACED != field) {
831 dprintk(1,"Field type invalid.\n");
838 f->fmt.pix.field = field;
839 if (f->fmt.pix.height < 32)
840 f->fmt.pix.height = 32;
841 if (f->fmt.pix.height > maxh)
842 f->fmt.pix.height = maxh;
843 if (f->fmt.pix.width < 48)
844 f->fmt.pix.width = 48;
845 if (f->fmt.pix.width > maxw)
846 f->fmt.pix.width = maxw;
847 f->fmt.pix.width &= ~0x03;
848 f->fmt.pix.bytesperline =
849 (f->fmt.pix.width * fmt->depth) >> 3;
850 f->fmt.pix.sizeimage =
851 f->fmt.pix.height * f->fmt.pix.bytesperline;
856 /*FIXME: This seems to be generic enough to be at videodev2 */
857 static int vidioc_s_fmt_cap (struct file *file, void *priv,
858 struct v4l2_format *f)
860 struct vivi_fh *fh=priv;
861 int ret = vidioc_try_fmt_cap(file,fh,f);
866 fh->width = f->fmt.pix.width;
867 fh->height = f->fmt.pix.height;
868 fh->vb_vidq.field = f->fmt.pix.field;
874 static int vidioc_reqbufs (struct file *file, void *priv, struct v4l2_requestbuffers *p)
876 struct vivi_fh *fh=priv;
878 return (videobuf_reqbufs(&fh->vb_vidq, p));
881 static int vidioc_querybuf (struct file *file, void *priv, struct v4l2_buffer *p)
883 struct vivi_fh *fh=priv;
885 return (videobuf_querybuf(&fh->vb_vidq, p));
888 static int vidioc_qbuf (struct file *file, void *priv, struct v4l2_buffer *p)
890 struct vivi_fh *fh=priv;
892 return (videobuf_qbuf(&fh->vb_vidq, p));
895 static int vidioc_dqbuf (struct file *file, void *priv, struct v4l2_buffer *p)
897 struct vivi_fh *fh=priv;
899 return (videobuf_dqbuf(&fh->vb_vidq, p,
900 file->f_flags & O_NONBLOCK));
903 #ifdef CONFIG_VIDEO_V4L1_COMPAT
904 static int vidiocgmbuf (struct file *file, void *priv, struct video_mbuf *mbuf)
906 struct vivi_fh *fh=priv;
907 struct videobuf_queue *q=&fh->vb_vidq;
908 struct v4l2_requestbuffers req;
914 req.memory = V4L2_MEMORY_MMAP;
915 ret = videobuf_reqbufs(q,&req);
919 mbuf->frames = req.count;
921 for (i = 0; i < mbuf->frames; i++) {
922 mbuf->offsets[i] = q->bufs[i]->boff;
923 mbuf->size += q->bufs[i]->bsize;
929 static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
931 struct vivi_fh *fh=priv;
932 struct vivi_dev *dev = fh->dev;
934 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
939 if (!res_get(dev,fh))
941 return (videobuf_streamon(&fh->vb_vidq));
944 static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
946 struct vivi_fh *fh=priv;
947 struct vivi_dev *dev = fh->dev;
949 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
954 videobuf_streamoff(&fh->vb_vidq);
960 static int vidioc_s_std (struct file *file, void *priv, v4l2_std_id *i)
965 /* only one input in this sample driver */
966 static int vidioc_enum_input (struct file *file, void *priv,
967 struct v4l2_input *inp)
972 inp->type = V4L2_INPUT_TYPE_CAMERA;
973 inp->std = V4L2_STD_NTSC_M;
974 strcpy(inp->name,"Camera");
979 static int vidioc_g_input (struct file *file, void *priv, unsigned int *i)
985 static int vidioc_s_input (struct file *file, void *priv, unsigned int i)
993 /* --- controls ---------------------------------------------- */
994 static int vidioc_queryctrl (struct file *file, void *priv,
995 struct v4l2_queryctrl *qc)
999 for (i = 0; i < ARRAY_SIZE(vivi_qctrl); i++)
1000 if (qc->id && qc->id == vivi_qctrl[i].id) {
1001 memcpy(qc, &(vivi_qctrl[i]),
1009 static int vidioc_g_ctrl (struct file *file, void *priv,
1010 struct v4l2_control *ctrl)
1014 for (i = 0; i < ARRAY_SIZE(vivi_qctrl); i++)
1015 if (ctrl->id == vivi_qctrl[i].id) {
1016 ctrl->value=qctl_regs[i];
1022 static int vidioc_s_ctrl (struct file *file, void *priv,
1023 struct v4l2_control *ctrl)
1027 for (i = 0; i < ARRAY_SIZE(vivi_qctrl); i++)
1028 if (ctrl->id == vivi_qctrl[i].id) {
1030 vivi_qctrl[i].minimum
1032 vivi_qctrl[i].maximum) {
1035 qctl_regs[i]=ctrl->value;
1041 /* ------------------------------------------------------------------
1042 File operations for the device
1043 ------------------------------------------------------------------*/
1045 #define line_buf_size(norm) (norm_maxw(norm)*(format.depth+7)/8)
1047 static int vivi_open(struct inode *inode, struct file *file)
1049 int minor = iminor(inode);
1050 struct vivi_dev *h,*dev = NULL;
1052 struct list_head *list;
1053 enum v4l2_buf_type type = 0;
1056 printk(KERN_DEBUG "vivi: open called (minor=%d)\n",minor);
1058 list_for_each(list,&vivi_devlist) {
1059 h = list_entry(list, struct vivi_dev, vivi_devlist);
1060 if (h->vfd.minor == minor) {
1062 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1070 /* If more than one user, mutex should be added */
1073 dprintk(1,"open minor=%d type=%s users=%d\n",
1074 minor,v4l2_type_names[type],dev->users);
1076 /* allocate + initialize per filehandle data */
1077 fh = kzalloc(sizeof(*fh),GFP_KERNEL);
1083 file->private_data = fh;
1086 fh->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1091 /* Put all controls at a sane state */
1092 for (i = 0; i < ARRAY_SIZE(vivi_qctrl); i++)
1093 qctl_regs[i] =vivi_qctrl[i].default_value;
1095 dprintk(1,"Open: fh=0x%08lx, dev=0x%08lx, dev->vidq=0x%08lx\n",
1096 (unsigned long)fh,(unsigned long)dev,(unsigned long)&dev->vidq);
1097 dprintk(1,"Open: list_empty queued=%d\n",list_empty(&dev->vidq.queued));
1098 dprintk(1,"Open: list_empty active=%d\n",list_empty(&dev->vidq.active));
1100 /* Resets frame counters */
1105 dev->jiffies=jiffies;
1106 sprintf(dev->timestr,"%02d:%02d:%02d:%03d",
1107 dev->h,dev->m,dev->s,(dev->us+500)/1000);
1109 videobuf_queue_init(&fh->vb_vidq, &vivi_video_qops,
1112 V4L2_FIELD_INTERLACED,
1113 sizeof(struct vivi_buffer),fh);
1119 vivi_read(struct file *file, char __user *data, size_t count, loff_t *ppos)
1121 struct vivi_fh *fh = file->private_data;
1123 if (fh->type==V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1124 if (res_locked(fh->dev))
1126 return videobuf_read_one(&fh->vb_vidq, data, count, ppos,
1127 file->f_flags & O_NONBLOCK);
1133 vivi_poll(struct file *file, struct poll_table_struct *wait)
1135 struct vivi_fh *fh = file->private_data;
1136 struct vivi_buffer *buf;
1138 dprintk(1,"%s\n",__FUNCTION__);
1140 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != fh->type)
1143 if (res_get(fh->dev,fh)) {
1144 dprintk(1,"poll: mmap interface\n");
1145 /* streaming capture */
1146 if (list_empty(&fh->vb_vidq.stream))
1148 buf = list_entry(fh->vb_vidq.stream.next,struct vivi_buffer,vb.stream);
1150 dprintk(1,"poll: read() interface\n");
1151 /* read() capture */
1152 buf = (struct vivi_buffer*)fh->vb_vidq.read_buf;
1156 poll_wait(file, &buf->vb.done, wait);
1157 if (buf->vb.state == STATE_DONE ||
1158 buf->vb.state == STATE_ERROR)
1159 return POLLIN|POLLRDNORM;
1163 static int vivi_release(struct inode *inode, struct file *file)
1165 struct vivi_fh *fh = file->private_data;
1166 struct vivi_dev *dev = fh->dev;
1167 struct vivi_dmaqueue *vidq = &dev->vidq;
1169 int minor = iminor(inode);
1171 vivi_stop_thread(vidq);
1172 videobuf_mmap_free(&fh->vb_vidq);
1178 printk(KERN_DEBUG "vivi: close called (minor=%d, users=%d)\n",minor,dev->users);
1184 vivi_mmap(struct file *file, struct vm_area_struct * vma)
1186 struct vivi_fh *fh = file->private_data;
1189 dprintk (1,"mmap called, vma=0x%08lx\n",(unsigned long)vma);
1191 ret=videobuf_mmap_mapper(&fh->vb_vidq, vma);
1193 dprintk (1,"vma start=0x%08lx, size=%ld, ret=%d\n",
1194 (unsigned long)vma->vm_start,
1195 (unsigned long)vma->vm_end-(unsigned long)vma->vm_start,
1201 static const struct file_operations vivi_fops = {
1202 .owner = THIS_MODULE,
1204 .release = vivi_release,
1207 .ioctl = video_ioctl2, /* V4L2 ioctl handler */
1209 .llseek = no_llseek,
1212 static struct video_device vivi = {
1214 .type = VID_TYPE_CAPTURE,
1218 // .release = video_device_release,
1220 .vidioc_querycap = vidioc_querycap,
1221 .vidioc_enum_fmt_cap = vidioc_enum_fmt_cap,
1222 .vidioc_g_fmt_cap = vidioc_g_fmt_cap,
1223 .vidioc_try_fmt_cap = vidioc_try_fmt_cap,
1224 .vidioc_s_fmt_cap = vidioc_s_fmt_cap,
1225 .vidioc_reqbufs = vidioc_reqbufs,
1226 .vidioc_querybuf = vidioc_querybuf,
1227 .vidioc_qbuf = vidioc_qbuf,
1228 .vidioc_dqbuf = vidioc_dqbuf,
1229 .vidioc_s_std = vidioc_s_std,
1230 .vidioc_enum_input = vidioc_enum_input,
1231 .vidioc_g_input = vidioc_g_input,
1232 .vidioc_s_input = vidioc_s_input,
1233 .vidioc_queryctrl = vidioc_queryctrl,
1234 .vidioc_g_ctrl = vidioc_g_ctrl,
1235 .vidioc_s_ctrl = vidioc_s_ctrl,
1236 .vidioc_streamon = vidioc_streamon,
1237 .vidioc_streamoff = vidioc_streamoff,
1238 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1239 .vidiocgmbuf = vidiocgmbuf,
1241 .tvnorms = V4L2_STD_NTSC_M,
1242 .current_norm = V4L2_STD_NTSC_M,
1244 /* -----------------------------------------------------------------
1245 Initialization and module stuff
1246 ------------------------------------------------------------------*/
1248 static int __init vivi_init(void)
1251 struct vivi_dev *dev;
1253 dev = kzalloc(sizeof(*dev),GFP_KERNEL);
1256 list_add_tail(&dev->vivi_devlist,&vivi_devlist);
1258 /* init video dma queues */
1259 INIT_LIST_HEAD(&dev->vidq.active);
1260 INIT_LIST_HEAD(&dev->vidq.queued);
1261 init_waitqueue_head(&dev->vidq.wq);
1263 /* initialize locks */
1264 mutex_init(&dev->lock);
1266 dev->vidq.timeout.function = vivi_vid_timeout;
1267 dev->vidq.timeout.data = (unsigned long)dev;
1268 init_timer(&dev->vidq.timeout);
1270 ret = video_register_device(&vivi, VFL_TYPE_GRABBER, video_nr);
1271 printk(KERN_INFO "Video Technology Magazine Virtual Video Capture Board (Load status: %d)\n", ret);
1275 static void __exit vivi_exit(void)
1278 struct list_head *list;
1280 while (!list_empty(&vivi_devlist)) {
1281 list = vivi_devlist.next;
1283 h = list_entry(list, struct vivi_dev, vivi_devlist);
1286 video_unregister_device(&vivi);
1289 module_init(vivi_init);
1290 module_exit(vivi_exit);
1292 MODULE_DESCRIPTION("Video Technology Magazine Virtual Video Capture Board");
1293 MODULE_AUTHOR("Mauro Carvalho Chehab, Ted Walther and John Sokol");
1294 MODULE_LICENSE("Dual BSD/GPL");
1296 module_param(video_nr, int, 0);
1298 module_param_named(debug,vivi.debug, int, 0644);
1299 MODULE_PARM_DESC(debug,"activates debug info");
1301 module_param(vid_limit,int,0644);
1302 MODULE_PARM_DESC(vid_limit,"capture memory limit in megabytes");