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/videodev2.h>
29 #include <linux/dma-mapping.h>
30 #ifdef CONFIG_VIDEO_V4L1_COMPAT
31 /* Include V4L1 specific functions. Should be removed soon */
32 #include <linux/videodev.h>
34 #include <linux/interrupt.h>
35 #include <media/video-buf.h>
36 #include <media/v4l2-common.h>
37 #include <linux/kthread.h>
38 #include <linux/highmem.h>
39 #include <linux/freezer.h>
41 /* Wake up at about 30 fps */
42 #define WAKE_NUMERATOR 30
43 #define WAKE_DENOMINATOR 1001
44 #define BUFFER_TIMEOUT msecs_to_jiffies(500) /* 0.5 seconds */
46 /* These timers are for 1 fps - used only for testing */
47 //#define WAKE_DENOMINATOR 30 /* hack for testing purposes */
48 //#define BUFFER_TIMEOUT msecs_to_jiffies(5000) /* 5 seconds */
52 #define VIVI_MAJOR_VERSION 0
53 #define VIVI_MINOR_VERSION 4
54 #define VIVI_RELEASE 0
55 #define VIVI_VERSION KERNEL_VERSION(VIVI_MAJOR_VERSION, VIVI_MINOR_VERSION, VIVI_RELEASE)
57 /* Declare static vars that will be used as parameters */
58 static unsigned int vid_limit = 16; /* Video memory limit, in Mb */
59 static struct video_device vivi; /* Video device */
60 static int video_nr = -1; /* /dev/videoN, -1 for autodetect */
62 /* supported controls */
63 static struct v4l2_queryctrl vivi_qctrl[] = {
65 .id = V4L2_CID_AUDIO_VOLUME,
70 .default_value = 65535,
72 .type = V4L2_CTRL_TYPE_INTEGER,
74 .id = V4L2_CID_BRIGHTNESS,
75 .type = V4L2_CTRL_TYPE_INTEGER,
83 .id = V4L2_CID_CONTRAST,
84 .type = V4L2_CTRL_TYPE_INTEGER,
89 .default_value = 0x10,
92 .id = V4L2_CID_SATURATION,
93 .type = V4L2_CTRL_TYPE_INTEGER,
102 .type = V4L2_CTRL_TYPE_INTEGER,
112 static int qctl_regs[ARRAY_SIZE(vivi_qctrl)];
114 #define dprintk(level,fmt, arg...) \
116 if (vivi.debug >= (level)) \
117 printk(KERN_DEBUG "vivi: " fmt , ## arg); \
120 /* ------------------------------------------------------------------
122 ------------------------------------------------------------------*/
126 u32 fourcc; /* v4l2 format id */
130 static struct vivi_fmt format = {
131 .name = "4:2:2, packed, YUYV",
132 .fourcc = V4L2_PIX_FMT_YUYV,
138 struct scatterlist *sg;
141 /* buffer for one video frame */
143 /* common v4l buffer stuff -- must be first */
144 struct videobuf_buffer vb;
146 struct vivi_fmt *fmt;
150 struct vivi_dmaqueue {
151 struct list_head active;
152 struct list_head queued;
153 struct timer_list timeout;
155 /* thread for generating video stream*/
156 struct task_struct *kthread;
157 wait_queue_head_t wq;
158 /* Counters to control fps rate */
163 static LIST_HEAD(vivi_devlist);
166 struct list_head vivi_devlist;
168 struct semaphore lock;
172 /* various device info */
173 unsigned int resources;
174 struct video_device vfd;
176 struct vivi_dmaqueue vidq;
178 /* Several counters */
179 int h,m,s,us,jiffies;
184 struct vivi_dev *dev;
187 struct vivi_fmt *fmt;
188 unsigned int width,height;
189 struct videobuf_queue vb_vidq;
191 enum v4l2_buf_type type;
194 /* ------------------------------------------------------------------
195 DMA and thread functions
196 ------------------------------------------------------------------*/
198 /* Bars and Colors should match positions */
210 static u8 bars[8][3] = {
212 {204,204,204}, /* white */
213 {208,208, 0}, /* ambar */
214 { 0,206,206}, /* cyan */
215 { 0,239, 0}, /* green */
216 {239, 0,239}, /* magenta */
217 {205, 0, 0}, /* red */
218 { 0, 0,255}, /* blue */
222 #define TO_Y(r,g,b) (((16829*r +33039*g +6416*b + 32768)>>16)+16)
223 /* RGB to V(Cr) Color transform */
224 #define TO_V(r,g,b) (((28784*r -24103*g -4681*b + 32768)>>16)+128)
225 /* RGB to U(Cb) Color transform */
226 #define TO_U(r,g,b) (((-9714*r -19070*g +28784*b + 32768)>>16)+128)
228 #define TSTAMP_MIN_Y 24
229 #define TSTAMP_MAX_Y TSTAMP_MIN_Y+15
230 #define TSTAMP_MIN_X 64
233 static void gen_line(char *basep,int inipos,int wmax,
234 int hmax, int line, char *timestr)
236 int w,i,j,pos=inipos,y;
240 /* We will just duplicate the second pixel at the packet */
243 /* Generate a standard color bar pattern */
244 for (w=0;w<wmax;w++) {
249 for (color=0;color<4;color++) {
255 *p=TO_Y(r,g,b); /* Luminance */
258 *p=TO_U(r,g,b); /* Cb */
261 *p=TO_V(r,g,b); /* Cr */
268 /* Checks if it is possible to show timestamp */
269 if (TSTAMP_MAX_Y>=hmax)
271 if (TSTAMP_MIN_X+strlen(timestr)>=wmax)
274 /* Print stream time */
275 if (line>=TSTAMP_MIN_Y && line<=TSTAMP_MAX_Y) {
277 for (s=timestr;*s;s++) {
278 chr=rom8x16_bits[(*s-0x30)*16+line-TSTAMP_MIN_Y];
280 if (chr&1<<(7-i)) { /* Font color*/
286 } else { /* Background color */
294 for (color=0;color<4;color++) {
302 *p=TO_Y(r,g,b); /* Luminance */
305 *p=TO_U(r,g,b); /* Cb */
308 *p=TO_V(r,g,b); /* Cr */
322 static void vivi_fillbuff(struct vivi_dev *dev,struct vivi_buffer *buf)
325 int hmax = buf->vb.height;
326 int wmax = buf->vb.width;
330 if (buf->vb.dma.varea) {
331 tmpbuf=kmalloc (wmax*2, GFP_KERNEL);
333 tmpbuf=buf->vb.dma.vmalloc;
337 for (h=0;h<hmax;h++) {
338 if (buf->vb.dma.varea) {
339 gen_line(tmpbuf,0,wmax,hmax,h,dev->timestr);
340 /* FIXME: replacing to __copy_to_user */
341 if (copy_to_user(buf->vb.dma.varea+pos,tmpbuf,wmax*2)!=0)
342 dprintk(2,"vivifill copy_to_user failed.\n");
344 gen_line(tmpbuf,pos,wmax,hmax,h,dev->timestr);
349 /* Updates stream time */
351 dev->us+=jiffies_to_usecs(jiffies-dev->jiffies);
352 dev->jiffies=jiffies;
353 if (dev->us>=1000000) {
367 sprintf(dev->timestr,"%02d:%02d:%02d:%03d",
368 dev->h,dev->m,dev->s,(dev->us+500)/1000);
370 dprintk(2,"vivifill at %s: Buffer 0x%08lx size= %d\n",dev->timestr,
371 (unsigned long)buf->vb.dma.varea,pos);
373 /* Advice that buffer was filled */
374 buf->vb.state = STATE_DONE;
375 buf->vb.field_count++;
376 do_gettimeofday(&ts);
379 list_del(&buf->vb.queue);
380 wake_up(&buf->vb.done);
383 static int restart_video_queue(struct vivi_dmaqueue *dma_q);
385 static void vivi_thread_tick(struct vivi_dmaqueue *dma_q)
387 struct vivi_buffer *buf;
388 struct vivi_dev *dev= container_of(dma_q,struct vivi_dev,vidq);
392 /* Announces videobuf that all went ok */
393 for (bc = 0;; bc++) {
394 if (list_empty(&dma_q->active)) {
395 dprintk(1,"No active queue to serve\n");
399 buf = list_entry(dma_q->active.next,
400 struct vivi_buffer, vb.queue);
402 /* Nobody is waiting something to be done, just return */
403 if (!waitqueue_active(&buf->vb.done)) {
404 mod_timer(&dma_q->timeout, jiffies+BUFFER_TIMEOUT);
408 do_gettimeofday(&buf->vb.ts);
409 dprintk(2,"[%p/%d] wakeup\n",buf,buf->vb.i);
412 vivi_fillbuff(dev,buf);
414 if (list_empty(&dma_q->active)) {
415 del_timer(&dma_q->timeout);
417 mod_timer(&dma_q->timeout, jiffies+BUFFER_TIMEOUT);
421 dprintk(1,"%s: %d buffers handled (should be 1)\n",__FUNCTION__,bc);
424 static void vivi_sleep(struct vivi_dmaqueue *dma_q)
427 DECLARE_WAITQUEUE(wait, current);
429 dprintk(1,"%s dma_q=0x%08lx\n",__FUNCTION__,(unsigned long)dma_q);
431 add_wait_queue(&dma_q->wq, &wait);
432 if (!kthread_should_stop()) {
435 /* Calculate time to wake up */
436 timeout=dma_q->ini_jiffies+msecs_to_jiffies((dma_q->frame*WAKE_NUMERATOR*1000)/WAKE_DENOMINATOR)-jiffies;
439 int old=dma_q->frame;
440 dma_q->frame=(jiffies_to_msecs(jiffies-dma_q->ini_jiffies)*WAKE_DENOMINATOR)/(WAKE_NUMERATOR*1000)+1;
442 timeout=dma_q->ini_jiffies+msecs_to_jiffies((dma_q->frame*WAKE_NUMERATOR*1000)/WAKE_DENOMINATOR)-jiffies;
444 dprintk(1,"underrun, losed %d frames. "
445 "Now, frame is %d. Waking on %d jiffies\n",
446 dma_q->frame-old,dma_q->frame,timeout);
448 dprintk(1,"will sleep for %i jiffies\n",timeout);
450 vivi_thread_tick(dma_q);
452 schedule_timeout_interruptible (timeout);
455 remove_wait_queue(&dma_q->wq, &wait);
459 static int vivi_thread(void *data)
461 struct vivi_dmaqueue *dma_q=data;
463 dprintk(1,"thread started\n");
465 mod_timer(&dma_q->timeout, jiffies+BUFFER_TIMEOUT);
471 if (kthread_should_stop())
474 dprintk(1, "thread: exit\n");
478 static int vivi_start_thread(struct vivi_dmaqueue *dma_q)
481 dma_q->ini_jiffies=jiffies;
483 dprintk(1,"%s\n",__FUNCTION__);
485 dma_q->kthread = kthread_run(vivi_thread, dma_q, "vivi");
487 if (IS_ERR(dma_q->kthread)) {
488 printk(KERN_ERR "vivi: kernel_thread() failed\n");
489 return PTR_ERR(dma_q->kthread);
492 wake_up_interruptible(&dma_q->wq);
494 dprintk(1,"returning from %s\n",__FUNCTION__);
498 static void vivi_stop_thread(struct vivi_dmaqueue *dma_q)
500 dprintk(1,"%s\n",__FUNCTION__);
501 /* shutdown control thread */
502 if (dma_q->kthread) {
503 kthread_stop(dma_q->kthread);
508 static int restart_video_queue(struct vivi_dmaqueue *dma_q)
510 struct vivi_buffer *buf, *prev;
511 struct list_head *item;
513 dprintk(1,"%s dma_q=0x%08lx\n",__FUNCTION__,(unsigned long)dma_q);
515 if (!list_empty(&dma_q->active)) {
516 buf = list_entry(dma_q->active.next, struct vivi_buffer, vb.queue);
517 dprintk(2,"restart_queue [%p/%d]: restart dma\n",
520 dprintk(1,"Restarting video dma\n");
521 vivi_stop_thread(dma_q);
522 // vivi_start_thread(dma_q);
524 /* cancel all outstanding capture / vbi requests */
525 list_for_each(item,&dma_q->active) {
526 buf = list_entry(item, struct vivi_buffer, vb.queue);
528 list_del(&buf->vb.queue);
529 buf->vb.state = STATE_ERROR;
530 wake_up(&buf->vb.done);
532 mod_timer(&dma_q->timeout, jiffies+BUFFER_TIMEOUT);
539 if (list_empty(&dma_q->queued))
541 buf = list_entry(dma_q->queued.next, struct vivi_buffer, vb.queue);
543 list_del(&buf->vb.queue);
544 list_add_tail(&buf->vb.queue,&dma_q->active);
546 dprintk(1,"Restarting video dma\n");
547 vivi_stop_thread(dma_q);
548 vivi_start_thread(dma_q);
550 buf->vb.state = STATE_ACTIVE;
551 mod_timer(&dma_q->timeout, jiffies+BUFFER_TIMEOUT);
552 dprintk(2,"[%p/%d] restart_queue - first active\n",
555 } else if (prev->vb.width == buf->vb.width &&
556 prev->vb.height == buf->vb.height &&
557 prev->fmt == buf->fmt) {
558 list_del(&buf->vb.queue);
559 list_add_tail(&buf->vb.queue,&dma_q->active);
560 buf->vb.state = STATE_ACTIVE;
561 dprintk(2,"[%p/%d] restart_queue - move to active\n",
570 static void vivi_vid_timeout(unsigned long data)
572 struct vivi_dev *dev = (struct vivi_dev*)data;
573 struct vivi_dmaqueue *vidq = &dev->vidq;
574 struct vivi_buffer *buf;
576 while (!list_empty(&vidq->active)) {
577 buf = list_entry(vidq->active.next, struct vivi_buffer, vb.queue);
578 list_del(&buf->vb.queue);
579 buf->vb.state = STATE_ERROR;
580 wake_up(&buf->vb.done);
581 printk("vivi/0: [%p/%d] timeout\n", buf, buf->vb.i);
584 restart_video_queue(vidq);
587 /* ------------------------------------------------------------------
589 ------------------------------------------------------------------*/
591 buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size)
593 struct vivi_fh *fh = vq->priv_data;
595 *size = fh->width*fh->height*2;
599 while (*size * *count > vid_limit * 1024 * 1024)
604 static void free_buffer(struct videobuf_queue *vq, struct vivi_buffer *buf)
606 dprintk(1,"%s\n",__FUNCTION__);
612 videobuf_waiton(&buf->vb,0,0);
613 videobuf_dma_unmap(vq, &buf->vb.dma);
614 videobuf_dma_free(&buf->vb.dma);
615 buf->vb.state = STATE_NEEDS_INIT;
618 #define norm_maxw() 1024
619 #define norm_maxh() 768
621 buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
622 enum v4l2_field field)
624 struct vivi_fh *fh = vq->priv_data;
625 struct vivi_buffer *buf = container_of(vb,struct vivi_buffer,vb);
626 int rc, init_buffer = 0;
628 // dprintk(1,"%s, field=%d\n",__FUNCTION__,field);
630 BUG_ON(NULL == fh->fmt);
631 if (fh->width < 48 || fh->width > norm_maxw() ||
632 fh->height < 32 || fh->height > norm_maxh())
634 buf->vb.size = fh->width*fh->height*2;
635 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
638 if (buf->fmt != fh->fmt ||
639 buf->vb.width != fh->width ||
640 buf->vb.height != fh->height ||
641 buf->vb.field != field) {
643 buf->vb.width = fh->width;
644 buf->vb.height = fh->height;
645 buf->vb.field = field;
649 if (STATE_NEEDS_INIT == buf->vb.state) {
650 if (0 != (rc = videobuf_iolock(vq,&buf->vb,NULL)))
654 buf->vb.state = STATE_PREPARED;
664 buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
666 struct vivi_buffer *buf = container_of(vb,struct vivi_buffer,vb);
667 struct vivi_fh *fh = vq->priv_data;
668 struct vivi_dev *dev = fh->dev;
669 struct vivi_dmaqueue *vidq = &dev->vidq;
670 struct vivi_buffer *prev;
672 if (!list_empty(&vidq->queued)) {
673 dprintk(1,"adding vb queue=0x%08lx\n",(unsigned long)&buf->vb.queue);
674 list_add_tail(&buf->vb.queue,&vidq->queued);
675 buf->vb.state = STATE_QUEUED;
676 dprintk(2,"[%p/%d] buffer_queue - append to queued\n",
678 } else if (list_empty(&vidq->active)) {
679 list_add_tail(&buf->vb.queue,&vidq->active);
681 buf->vb.state = STATE_ACTIVE;
682 mod_timer(&vidq->timeout, jiffies+BUFFER_TIMEOUT);
683 dprintk(2,"[%p/%d] buffer_queue - first active\n",
686 vivi_start_thread(vidq);
688 prev = list_entry(vidq->active.prev, struct vivi_buffer, vb.queue);
689 if (prev->vb.width == buf->vb.width &&
690 prev->vb.height == buf->vb.height &&
691 prev->fmt == buf->fmt) {
692 list_add_tail(&buf->vb.queue,&vidq->active);
693 buf->vb.state = STATE_ACTIVE;
694 dprintk(2,"[%p/%d] buffer_queue - append to active\n",
698 list_add_tail(&buf->vb.queue,&vidq->queued);
699 buf->vb.state = STATE_QUEUED;
700 dprintk(2,"[%p/%d] buffer_queue - first queued\n",
706 static void buffer_release(struct videobuf_queue *vq, struct videobuf_buffer *vb)
708 struct vivi_buffer *buf = container_of(vb,struct vivi_buffer,vb);
709 struct vivi_fh *fh = vq->priv_data;
710 struct vivi_dev *dev = (struct vivi_dev*)fh->dev;
711 struct vivi_dmaqueue *vidq = &dev->vidq;
713 dprintk(1,"%s\n",__FUNCTION__);
715 vivi_stop_thread(vidq);
721 static struct videobuf_queue_ops vivi_video_qops = {
722 .buf_setup = buffer_setup,
723 .buf_prepare = buffer_prepare,
724 .buf_queue = buffer_queue,
725 .buf_release = buffer_release,
727 /* Non-pci handling routines */
728 // .vb_map_sg = vivi_map_sg,
729 // .vb_dma_sync_sg = vivi_dma_sync_sg,
730 // .vb_unmap_sg = vivi_unmap_sg,
733 /* ------------------------------------------------------------------
735 ------------------------------------------------------------------*/
738 static int res_get(struct vivi_dev *dev, struct vivi_fh *fh)
742 if (dev->resources) {
743 /* no, someone else uses it */
747 /* it's free, grab it */
749 dprintk(1,"res: get\n");
754 static int res_locked(struct vivi_dev *dev)
756 return (dev->resources);
759 static void res_free(struct vivi_dev *dev, struct vivi_fh *fh)
763 dprintk(1,"res: put\n");
767 /* ------------------------------------------------------------------
768 IOCTL vidioc handling
769 ------------------------------------------------------------------*/
770 static int vidioc_querycap (struct file *file, void *priv,
771 struct v4l2_capability *cap)
773 strcpy(cap->driver, "vivi");
774 strcpy(cap->card, "vivi");
775 cap->version = VIVI_VERSION;
776 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE |
782 static int vidioc_enum_fmt_cap (struct file *file, void *priv,
783 struct v4l2_fmtdesc *f)
788 strlcpy(f->description,format.name,sizeof(f->description));
789 f->pixelformat = format.fourcc;
793 static int vidioc_g_fmt_cap (struct file *file, void *priv,
794 struct v4l2_format *f)
796 struct vivi_fh *fh=priv;
798 f->fmt.pix.width = fh->width;
799 f->fmt.pix.height = fh->height;
800 f->fmt.pix.field = fh->vb_vidq.field;
801 f->fmt.pix.pixelformat = fh->fmt->fourcc;
802 f->fmt.pix.bytesperline =
803 (f->fmt.pix.width * fh->fmt->depth) >> 3;
804 f->fmt.pix.sizeimage =
805 f->fmt.pix.height * f->fmt.pix.bytesperline;
810 static int vidioc_try_fmt_cap (struct file *file, void *priv,
811 struct v4l2_format *f)
813 struct vivi_fmt *fmt;
814 enum v4l2_field field;
815 unsigned int maxw, maxh;
817 if (format.fourcc != f->fmt.pix.pixelformat) {
818 dprintk(1,"Fourcc format (0x%08x) invalid. Driver accepts "
819 "only 0x%08x\n",f->fmt.pix.pixelformat,format.fourcc);
824 field = f->fmt.pix.field;
826 if (field == V4L2_FIELD_ANY) {
827 // field=V4L2_FIELD_INTERLACED;
828 field=V4L2_FIELD_SEQ_TB;
829 } else if (V4L2_FIELD_INTERLACED != field) {
830 dprintk(1,"Field type invalid.\n");
837 f->fmt.pix.field = field;
838 if (f->fmt.pix.height < 32)
839 f->fmt.pix.height = 32;
840 if (f->fmt.pix.height > maxh)
841 f->fmt.pix.height = maxh;
842 if (f->fmt.pix.width < 48)
843 f->fmt.pix.width = 48;
844 if (f->fmt.pix.width > maxw)
845 f->fmt.pix.width = maxw;
846 f->fmt.pix.width &= ~0x03;
847 f->fmt.pix.bytesperline =
848 (f->fmt.pix.width * fmt->depth) >> 3;
849 f->fmt.pix.sizeimage =
850 f->fmt.pix.height * f->fmt.pix.bytesperline;
855 /*FIXME: This seems to be generic enough to be at videodev2 */
856 static int vidioc_s_fmt_cap (struct file *file, void *priv,
857 struct v4l2_format *f)
859 struct vivi_fh *fh=priv;
860 int ret = vidioc_try_fmt_cap(file,fh,f);
865 fh->width = f->fmt.pix.width;
866 fh->height = f->fmt.pix.height;
867 fh->vb_vidq.field = f->fmt.pix.field;
873 static int vidioc_reqbufs (struct file *file, void *priv, struct v4l2_requestbuffers *p)
875 struct vivi_fh *fh=priv;
877 return (videobuf_reqbufs(&fh->vb_vidq, p));
880 static int vidioc_querybuf (struct file *file, void *priv, struct v4l2_buffer *p)
882 struct vivi_fh *fh=priv;
884 return (videobuf_querybuf(&fh->vb_vidq, p));
887 static int vidioc_qbuf (struct file *file, void *priv, struct v4l2_buffer *p)
889 struct vivi_fh *fh=priv;
891 return (videobuf_qbuf(&fh->vb_vidq, p));
894 static int vidioc_dqbuf (struct file *file, void *priv, struct v4l2_buffer *p)
896 struct vivi_fh *fh=priv;
898 return (videobuf_dqbuf(&fh->vb_vidq, p,
899 file->f_flags & O_NONBLOCK));
902 #ifdef CONFIG_VIDEO_V4L1_COMPAT
903 static int vidiocgmbuf (struct file *file, void *priv, struct video_mbuf *mbuf)
905 struct vivi_fh *fh=priv;
906 struct videobuf_queue *q=&fh->vb_vidq;
907 struct v4l2_requestbuffers req;
913 req.memory = V4L2_MEMORY_MMAP;
914 ret = videobuf_reqbufs(q,&req);
918 mbuf->frames = req.count;
920 for (i = 0; i < mbuf->frames; i++) {
921 mbuf->offsets[i] = q->bufs[i]->boff;
922 mbuf->size += q->bufs[i]->bsize;
928 static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
930 struct vivi_fh *fh=priv;
931 struct vivi_dev *dev = fh->dev;
933 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
938 if (!res_get(dev,fh))
940 return (videobuf_streamon(&fh->vb_vidq));
943 static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
945 struct vivi_fh *fh=priv;
946 struct vivi_dev *dev = fh->dev;
948 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
953 videobuf_streamoff(&fh->vb_vidq);
959 static int vidioc_s_std (struct file *file, void *priv, v4l2_std_id *i)
964 /* only one input in this sample driver */
965 static int vidioc_enum_input (struct file *file, void *priv,
966 struct v4l2_input *inp)
971 inp->type = V4L2_INPUT_TYPE_CAMERA;
972 inp->std = V4L2_STD_NTSC_M;
973 strcpy(inp->name,"Camera");
978 static int vidioc_g_input (struct file *file, void *priv, unsigned int *i)
984 static int vidioc_s_input (struct file *file, void *priv, unsigned int i)
992 /* --- controls ---------------------------------------------- */
993 static int vidioc_queryctrl (struct file *file, void *priv,
994 struct v4l2_queryctrl *qc)
998 for (i = 0; i < ARRAY_SIZE(vivi_qctrl); i++)
999 if (qc->id && qc->id == vivi_qctrl[i].id) {
1000 memcpy(qc, &(vivi_qctrl[i]),
1008 static int vidioc_g_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) {
1015 ctrl->value=qctl_regs[i];
1021 static int vidioc_s_ctrl (struct file *file, void *priv,
1022 struct v4l2_control *ctrl)
1026 for (i = 0; i < ARRAY_SIZE(vivi_qctrl); i++)
1027 if (ctrl->id == vivi_qctrl[i].id) {
1029 vivi_qctrl[i].minimum
1031 vivi_qctrl[i].maximum) {
1034 qctl_regs[i]=ctrl->value;
1040 /* ------------------------------------------------------------------
1041 File operations for the device
1042 ------------------------------------------------------------------*/
1044 #define line_buf_size(norm) (norm_maxw(norm)*(format.depth+7)/8)
1046 static int vivi_open(struct inode *inode, struct file *file)
1048 int minor = iminor(inode);
1049 struct vivi_dev *h,*dev = NULL;
1051 struct list_head *list;
1052 enum v4l2_buf_type type = 0;
1055 printk(KERN_DEBUG "vivi: open called (minor=%d)\n",minor);
1057 list_for_each(list,&vivi_devlist) {
1058 h = list_entry(list, struct vivi_dev, vivi_devlist);
1059 if (h->vfd.minor == minor) {
1061 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1069 /* If more than one user, mutex should be added */
1072 dprintk(1,"open minor=%d type=%s users=%d\n",
1073 minor,v4l2_type_names[type],dev->users);
1075 /* allocate + initialize per filehandle data */
1076 fh = kzalloc(sizeof(*fh),GFP_KERNEL);
1082 file->private_data = fh;
1085 fh->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1090 /* Put all controls at a sane state */
1091 for (i = 0; i < ARRAY_SIZE(vivi_qctrl); i++)
1092 qctl_regs[i] =vivi_qctrl[i].default_value;
1094 dprintk(1,"Open: fh=0x%08lx, dev=0x%08lx, dev->vidq=0x%08lx\n",
1095 (unsigned long)fh,(unsigned long)dev,(unsigned long)&dev->vidq);
1096 dprintk(1,"Open: list_empty queued=%d\n",list_empty(&dev->vidq.queued));
1097 dprintk(1,"Open: list_empty active=%d\n",list_empty(&dev->vidq.active));
1099 /* Resets frame counters */
1104 dev->jiffies=jiffies;
1105 sprintf(dev->timestr,"%02d:%02d:%02d:%03d",
1106 dev->h,dev->m,dev->s,(dev->us+500)/1000);
1108 videobuf_queue_init(&fh->vb_vidq, &vivi_video_qops,
1111 V4L2_FIELD_INTERLACED,
1112 sizeof(struct vivi_buffer),fh);
1118 vivi_read(struct file *file, char __user *data, size_t count, loff_t *ppos)
1120 struct vivi_fh *fh = file->private_data;
1122 if (fh->type==V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1123 if (res_locked(fh->dev))
1125 return videobuf_read_one(&fh->vb_vidq, data, count, ppos,
1126 file->f_flags & O_NONBLOCK);
1132 vivi_poll(struct file *file, struct poll_table_struct *wait)
1134 struct vivi_fh *fh = file->private_data;
1135 struct vivi_buffer *buf;
1137 dprintk(1,"%s\n",__FUNCTION__);
1139 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != fh->type)
1142 if (res_get(fh->dev,fh)) {
1143 dprintk(1,"poll: mmap interface\n");
1144 /* streaming capture */
1145 if (list_empty(&fh->vb_vidq.stream))
1147 buf = list_entry(fh->vb_vidq.stream.next,struct vivi_buffer,vb.stream);
1149 dprintk(1,"poll: read() interface\n");
1150 /* read() capture */
1151 buf = (struct vivi_buffer*)fh->vb_vidq.read_buf;
1155 poll_wait(file, &buf->vb.done, wait);
1156 if (buf->vb.state == STATE_DONE ||
1157 buf->vb.state == STATE_ERROR)
1158 return POLLIN|POLLRDNORM;
1162 static int vivi_release(struct inode *inode, struct file *file)
1164 struct vivi_fh *fh = file->private_data;
1165 struct vivi_dev *dev = fh->dev;
1166 struct vivi_dmaqueue *vidq = &dev->vidq;
1168 int minor = iminor(inode);
1170 vivi_stop_thread(vidq);
1171 videobuf_mmap_free(&fh->vb_vidq);
1177 printk(KERN_DEBUG "vivi: close called (minor=%d, users=%d)\n",minor,dev->users);
1183 vivi_mmap(struct file *file, struct vm_area_struct * vma)
1185 struct vivi_fh *fh = file->private_data;
1188 dprintk (1,"mmap called, vma=0x%08lx\n",(unsigned long)vma);
1190 ret=videobuf_mmap_mapper(&fh->vb_vidq, vma);
1192 dprintk (1,"vma start=0x%08lx, size=%ld, ret=%d\n",
1193 (unsigned long)vma->vm_start,
1194 (unsigned long)vma->vm_end-(unsigned long)vma->vm_start,
1200 static const struct file_operations vivi_fops = {
1201 .owner = THIS_MODULE,
1203 .release = vivi_release,
1206 .ioctl = video_ioctl2, /* V4L2 ioctl handler */
1208 .llseek = no_llseek,
1211 static struct video_device vivi = {
1213 .type = VID_TYPE_CAPTURE,
1217 // .release = video_device_release,
1219 .vidioc_querycap = vidioc_querycap,
1220 .vidioc_enum_fmt_cap = vidioc_enum_fmt_cap,
1221 .vidioc_g_fmt_cap = vidioc_g_fmt_cap,
1222 .vidioc_try_fmt_cap = vidioc_try_fmt_cap,
1223 .vidioc_s_fmt_cap = vidioc_s_fmt_cap,
1224 .vidioc_reqbufs = vidioc_reqbufs,
1225 .vidioc_querybuf = vidioc_querybuf,
1226 .vidioc_qbuf = vidioc_qbuf,
1227 .vidioc_dqbuf = vidioc_dqbuf,
1228 .vidioc_s_std = vidioc_s_std,
1229 .vidioc_enum_input = vidioc_enum_input,
1230 .vidioc_g_input = vidioc_g_input,
1231 .vidioc_s_input = vidioc_s_input,
1232 .vidioc_queryctrl = vidioc_queryctrl,
1233 .vidioc_g_ctrl = vidioc_g_ctrl,
1234 .vidioc_s_ctrl = vidioc_s_ctrl,
1235 .vidioc_streamon = vidioc_streamon,
1236 .vidioc_streamoff = vidioc_streamoff,
1237 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1238 .vidiocgmbuf = vidiocgmbuf,
1240 .tvnorms = V4L2_STD_NTSC_M,
1241 .current_norm = V4L2_STD_NTSC_M,
1243 /* -----------------------------------------------------------------
1244 Initialization and module stuff
1245 ------------------------------------------------------------------*/
1247 static int __init vivi_init(void)
1250 struct vivi_dev *dev;
1252 dev = kzalloc(sizeof(*dev),GFP_KERNEL);
1255 list_add_tail(&dev->vivi_devlist,&vivi_devlist);
1257 /* init video dma queues */
1258 INIT_LIST_HEAD(&dev->vidq.active);
1259 INIT_LIST_HEAD(&dev->vidq.queued);
1260 init_waitqueue_head(&dev->vidq.wq);
1262 /* initialize locks */
1263 init_MUTEX(&dev->lock);
1265 dev->vidq.timeout.function = vivi_vid_timeout;
1266 dev->vidq.timeout.data = (unsigned long)dev;
1267 init_timer(&dev->vidq.timeout);
1269 ret = video_register_device(&vivi, VFL_TYPE_GRABBER, video_nr);
1270 printk(KERN_INFO "Video Technology Magazine Virtual Video Capture Board (Load status: %d)\n", ret);
1274 static void __exit vivi_exit(void)
1277 struct list_head *list;
1279 while (!list_empty(&vivi_devlist)) {
1280 list = vivi_devlist.next;
1282 h = list_entry(list, struct vivi_dev, vivi_devlist);
1285 video_unregister_device(&vivi);
1288 module_init(vivi_init);
1289 module_exit(vivi_exit);
1291 MODULE_DESCRIPTION("Video Technology Magazine Virtual Video Capture Board");
1292 MODULE_AUTHOR("Mauro Carvalho Chehab, Ted Walther and John Sokol");
1293 MODULE_LICENSE("Dual BSD/GPL");
1295 module_param(video_nr, int, 0);
1297 module_param_named(debug,vivi.debug, int, 0644);
1298 MODULE_PARM_DESC(debug,"activates debug info");
1300 module_param(vid_limit,int,0644);
1301 MODULE_PARM_DESC(vid_limit,"capture memory limit in megabytes");