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-vmalloc.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 */
62 static int n_devs = 1; /* Number of virtual devices */
64 /* supported controls */
65 static struct v4l2_queryctrl vivi_qctrl[] = {
67 .id = V4L2_CID_AUDIO_VOLUME,
72 .default_value = 65535,
74 .type = V4L2_CTRL_TYPE_INTEGER,
76 .id = V4L2_CID_BRIGHTNESS,
77 .type = V4L2_CTRL_TYPE_INTEGER,
85 .id = V4L2_CID_CONTRAST,
86 .type = V4L2_CTRL_TYPE_INTEGER,
91 .default_value = 0x10,
94 .id = V4L2_CID_SATURATION,
95 .type = V4L2_CTRL_TYPE_INTEGER,
100 .default_value = 127,
104 .type = V4L2_CTRL_TYPE_INTEGER,
114 static int qctl_regs[ARRAY_SIZE(vivi_qctrl)];
116 #define dprintk(level,fmt, arg...) \
118 if (vivi.debug >= (level)) \
119 printk(KERN_DEBUG "vivi: " fmt , ## arg); \
122 /* ------------------------------------------------------------------
124 ------------------------------------------------------------------*/
128 u32 fourcc; /* v4l2 format id */
132 static struct vivi_fmt format = {
133 .name = "4:2:2, packed, YUYV",
134 .fourcc = V4L2_PIX_FMT_YUYV,
140 struct scatterlist *sg;
143 /* buffer for one video frame */
145 /* common v4l buffer stuff -- must be first */
146 struct videobuf_buffer vb;
148 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 struct video_device *vfd;
176 struct vivi_dmaqueue vidq;
178 /* Several counters */
179 int h,m,s,us,jiffies;
182 int mv_count; /* Controls bars movement */
186 struct vivi_dev *dev;
189 struct vivi_fmt *fmt;
190 unsigned int width,height;
191 struct videobuf_queue vb_vidq;
193 enum v4l2_buf_type type;
196 /* ------------------------------------------------------------------
197 DMA and thread functions
198 ------------------------------------------------------------------*/
200 /* Bars and Colors should match positions */
212 static u8 bars[8][3] = {
214 {204,204,204}, /* white */
215 {208,208, 0}, /* ambar */
216 { 0,206,206}, /* cyan */
217 { 0,239, 0}, /* green */
218 {239, 0,239}, /* magenta */
219 {205, 0, 0}, /* red */
220 { 0, 0,255}, /* blue */
224 #define TO_Y(r,g,b) (((16829*r +33039*g +6416*b + 32768)>>16)+16)
225 /* RGB to V(Cr) Color transform */
226 #define TO_V(r,g,b) (((28784*r -24103*g -4681*b + 32768)>>16)+128)
227 /* RGB to U(Cb) Color transform */
228 #define TO_U(r,g,b) (((-9714*r -19070*g +28784*b + 32768)>>16)+128)
230 #define TSTAMP_MIN_Y 24
231 #define TSTAMP_MAX_Y TSTAMP_MIN_Y+15
232 #define TSTAMP_MIN_X 64
234 static void gen_line(char *basep,int inipos,int wmax,
235 int hmax, int line, int count, 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++) {
246 int colorpos=((w+count)*8/(wmax+1)) % 8;
251 for (color=0;color<4;color++) {
257 *p=TO_Y(r,g,b); /* Luminance */
260 *p=TO_U(r,g,b); /* Cb */
263 *p=TO_V(r,g,b); /* Cr */
270 /* Checks if it is possible to show timestamp */
271 if (TSTAMP_MAX_Y>=hmax)
273 if (TSTAMP_MIN_X+strlen(timestr)>=wmax)
276 /* Print stream time */
277 if (line>=TSTAMP_MIN_Y && line<=TSTAMP_MAX_Y) {
279 for (s=timestr;*s;s++) {
280 chr=rom8x16_bits[(*s-0x30)*16+line-TSTAMP_MIN_Y];
282 if (chr&1<<(7-i)) { /* Font color*/
288 } else { /* Background color */
296 for (color=0;color<4;color++) {
304 *p=TO_Y(r,g,b); /* Luminance */
307 *p=TO_U(r,g,b); /* Cb */
310 *p=TO_V(r,g,b); /* Cr */
324 static void vivi_fillbuff(struct vivi_dev *dev,struct vivi_buffer *buf)
327 int hmax = buf->vb.height;
328 int wmax = buf->vb.width;
330 char *tmpbuf = kmalloc(wmax*2,GFP_KERNEL);
331 void *vbuf=videobuf_to_vmalloc (&buf->vb);
336 for (h=0;h<hmax;h++) {
337 gen_line(tmpbuf, 0, wmax, hmax, h, dev->mv_count,
339 /* FIXME: replacing to __copy_to_user */
340 if (copy_to_user(vbuf+pos,tmpbuf,wmax*2)!=0)
341 dprintk(2,"vivifill copy_to_user failed.\n");
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)tmpbuf,pos);
373 /* Advice that buffer was filled */
374 buf->vb.state = VIDEOBUF_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;
512 dprintk(1,"%s dma_q=0x%08lx\n",__FUNCTION__,(unsigned long)dma_q);
514 if (!list_empty(&dma_q->active)) {
515 buf = list_entry(dma_q->active.next, struct vivi_buffer, vb.queue);
516 dprintk(2,"restart_queue [%p/%d]: restart dma\n",
519 dprintk(1,"Restarting video dma\n");
520 vivi_stop_thread(dma_q);
521 // vivi_start_thread(dma_q);
523 /* cancel all outstanding capture / vbi requests */
524 list_for_each_entry_safe(buf, prev, &dma_q->active, vb.queue) {
525 list_del(&buf->vb.queue);
526 buf->vb.state = VIDEOBUF_ERROR;
527 wake_up(&buf->vb.done);
529 mod_timer(&dma_q->timeout, jiffies+BUFFER_TIMEOUT);
536 if (list_empty(&dma_q->queued))
538 buf = list_entry(dma_q->queued.next, struct vivi_buffer, vb.queue);
540 list_del(&buf->vb.queue);
541 list_add_tail(&buf->vb.queue,&dma_q->active);
543 dprintk(1,"Restarting video dma\n");
544 vivi_stop_thread(dma_q);
545 vivi_start_thread(dma_q);
547 buf->vb.state = VIDEOBUF_ACTIVE;
548 mod_timer(&dma_q->timeout, jiffies+BUFFER_TIMEOUT);
549 dprintk(2,"[%p/%d] restart_queue - first active\n",
552 } else if (prev->vb.width == buf->vb.width &&
553 prev->vb.height == buf->vb.height &&
554 prev->fmt == buf->fmt) {
555 list_del(&buf->vb.queue);
556 list_add_tail(&buf->vb.queue,&dma_q->active);
557 buf->vb.state = VIDEOBUF_ACTIVE;
558 dprintk(2,"[%p/%d] restart_queue - move to active\n",
567 static void vivi_vid_timeout(unsigned long data)
569 struct vivi_dev *dev = (struct vivi_dev*)data;
570 struct vivi_dmaqueue *vidq = &dev->vidq;
571 struct vivi_buffer *buf;
573 while (!list_empty(&vidq->active)) {
574 buf = list_entry(vidq->active.next, struct vivi_buffer, vb.queue);
575 list_del(&buf->vb.queue);
576 buf->vb.state = VIDEOBUF_ERROR;
577 wake_up(&buf->vb.done);
578 printk("vivi/0: [%p/%d] timeout\n", buf, buf->vb.i);
581 restart_video_queue(vidq);
584 /* ------------------------------------------------------------------
586 ------------------------------------------------------------------*/
588 buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size)
590 struct vivi_fh *fh = vq->priv_data;
592 *size = fh->width*fh->height*2;
597 while (*size * *count > vid_limit * 1024 * 1024)
600 dprintk(1,"%s, count=%d, size=%d\n",__FUNCTION__,*count, *size);
605 static void free_buffer(struct videobuf_queue *vq, struct vivi_buffer *buf)
607 dprintk(1,"%s\n",__FUNCTION__);
612 videobuf_waiton(&buf->vb,0,0);
613 videobuf_vmalloc_free(&buf->vb);
614 buf->vb.state = VIDEOBUF_NEEDS_INIT;
617 #define norm_maxw() 1024
618 #define norm_maxh() 768
620 buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
621 enum v4l2_field field)
623 struct vivi_fh *fh = vq->priv_data;
624 struct vivi_buffer *buf = container_of(vb,struct vivi_buffer,vb);
625 int rc, init_buffer = 0;
627 dprintk(1,"%s, field=%d\n",__FUNCTION__,field);
629 BUG_ON(NULL == fh->fmt);
630 if (fh->width < 48 || fh->width > norm_maxw() ||
631 fh->height < 32 || fh->height > norm_maxh())
633 buf->vb.size = fh->width*fh->height*2;
634 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
637 if (buf->fmt != fh->fmt ||
638 buf->vb.width != fh->width ||
639 buf->vb.height != fh->height ||
640 buf->vb.field != field) {
642 buf->vb.width = fh->width;
643 buf->vb.height = fh->height;
644 buf->vb.field = field;
648 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
649 if (0 != (rc = videobuf_iolock(vq,&buf->vb,NULL)))
653 buf->vb.state = VIDEOBUF_PREPARED;
663 buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
665 struct vivi_buffer *buf = container_of(vb,struct vivi_buffer,vb);
666 struct vivi_fh *fh = vq->priv_data;
667 struct vivi_dev *dev = fh->dev;
668 struct vivi_dmaqueue *vidq = &dev->vidq;
669 struct vivi_buffer *prev;
671 if (!list_empty(&vidq->queued)) {
672 dprintk(1,"adding vb queue=0x%08lx\n",(unsigned long)&buf->vb.queue);
673 list_add_tail(&buf->vb.queue,&vidq->queued);
674 buf->vb.state = VIDEOBUF_QUEUED;
675 dprintk(2,"[%p/%d] buffer_queue - append to queued\n",
677 } else if (list_empty(&vidq->active)) {
678 list_add_tail(&buf->vb.queue,&vidq->active);
680 buf->vb.state = VIDEOBUF_ACTIVE;
681 mod_timer(&vidq->timeout, jiffies+BUFFER_TIMEOUT);
682 dprintk(2,"[%p/%d] buffer_queue - first active\n",
685 vivi_start_thread(vidq);
687 prev = list_entry(vidq->active.prev, struct vivi_buffer, vb.queue);
688 if (prev->vb.width == buf->vb.width &&
689 prev->vb.height == buf->vb.height &&
690 prev->fmt == buf->fmt) {
691 list_add_tail(&buf->vb.queue,&vidq->active);
692 buf->vb.state = VIDEOBUF_ACTIVE;
693 dprintk(2,"[%p/%d] buffer_queue - append to active\n",
697 list_add_tail(&buf->vb.queue,&vidq->queued);
698 buf->vb.state = VIDEOBUF_QUEUED;
699 dprintk(2,"[%p/%d] buffer_queue - first queued\n",
705 static void buffer_release(struct videobuf_queue *vq, struct videobuf_buffer *vb)
707 struct vivi_buffer *buf = container_of(vb,struct vivi_buffer,vb);
708 struct vivi_fh *fh = vq->priv_data;
709 struct vivi_dev *dev = (struct vivi_dev*)fh->dev;
710 struct vivi_dmaqueue *vidq = &dev->vidq;
712 dprintk(1,"%s\n",__FUNCTION__);
714 vivi_stop_thread(vidq);
719 static struct videobuf_queue_ops vivi_video_qops = {
720 .buf_setup = buffer_setup,
721 .buf_prepare = buffer_prepare,
722 .buf_queue = buffer_queue,
723 .buf_release = buffer_release,
726 /* ------------------------------------------------------------------
727 IOCTL vidioc handling
728 ------------------------------------------------------------------*/
729 static int vidioc_querycap (struct file *file, void *priv,
730 struct v4l2_capability *cap)
732 strcpy(cap->driver, "vivi");
733 strcpy(cap->card, "vivi");
734 cap->version = VIVI_VERSION;
735 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE |
741 static int vidioc_enum_fmt_cap (struct file *file, void *priv,
742 struct v4l2_fmtdesc *f)
747 strlcpy(f->description,format.name,sizeof(f->description));
748 f->pixelformat = format.fourcc;
752 static int vidioc_g_fmt_cap (struct file *file, void *priv,
753 struct v4l2_format *f)
755 struct vivi_fh *fh=priv;
757 f->fmt.pix.width = fh->width;
758 f->fmt.pix.height = fh->height;
759 f->fmt.pix.field = fh->vb_vidq.field;
760 f->fmt.pix.pixelformat = fh->fmt->fourcc;
761 f->fmt.pix.bytesperline =
762 (f->fmt.pix.width * fh->fmt->depth) >> 3;
763 f->fmt.pix.sizeimage =
764 f->fmt.pix.height * f->fmt.pix.bytesperline;
769 static int vidioc_try_fmt_cap (struct file *file, void *priv,
770 struct v4l2_format *f)
772 struct vivi_fmt *fmt;
773 enum v4l2_field field;
774 unsigned int maxw, maxh;
776 if (format.fourcc != f->fmt.pix.pixelformat) {
777 dprintk(1,"Fourcc format (0x%08x) invalid. Driver accepts "
778 "only 0x%08x\n",f->fmt.pix.pixelformat,format.fourcc);
783 field = f->fmt.pix.field;
785 if (field == V4L2_FIELD_ANY) {
786 field=V4L2_FIELD_INTERLACED;
787 } else if (V4L2_FIELD_INTERLACED != field) {
788 dprintk(1,"Field type invalid.\n");
795 f->fmt.pix.field = field;
796 if (f->fmt.pix.height < 32)
797 f->fmt.pix.height = 32;
798 if (f->fmt.pix.height > maxh)
799 f->fmt.pix.height = maxh;
800 if (f->fmt.pix.width < 48)
801 f->fmt.pix.width = 48;
802 if (f->fmt.pix.width > maxw)
803 f->fmt.pix.width = maxw;
804 f->fmt.pix.width &= ~0x03;
805 f->fmt.pix.bytesperline =
806 (f->fmt.pix.width * fmt->depth) >> 3;
807 f->fmt.pix.sizeimage =
808 f->fmt.pix.height * f->fmt.pix.bytesperline;
813 /*FIXME: This seems to be generic enough to be at videodev2 */
814 static int vidioc_s_fmt_cap (struct file *file, void *priv,
815 struct v4l2_format *f)
817 struct vivi_fh *fh=priv;
818 int ret = vidioc_try_fmt_cap(file,fh,f);
823 fh->width = f->fmt.pix.width;
824 fh->height = f->fmt.pix.height;
825 fh->vb_vidq.field = f->fmt.pix.field;
831 static int vidioc_reqbufs (struct file *file, void *priv, struct v4l2_requestbuffers *p)
833 struct vivi_fh *fh=priv;
835 return (videobuf_reqbufs(&fh->vb_vidq, p));
838 static int vidioc_querybuf (struct file *file, void *priv, struct v4l2_buffer *p)
840 struct vivi_fh *fh=priv;
842 return (videobuf_querybuf(&fh->vb_vidq, p));
845 static int vidioc_qbuf (struct file *file, void *priv, struct v4l2_buffer *p)
847 struct vivi_fh *fh=priv;
849 return (videobuf_qbuf(&fh->vb_vidq, p));
852 static int vidioc_dqbuf (struct file *file, void *priv, struct v4l2_buffer *p)
854 struct vivi_fh *fh=priv;
856 return (videobuf_dqbuf(&fh->vb_vidq, p,
857 file->f_flags & O_NONBLOCK));
860 #ifdef CONFIG_VIDEO_V4L1_COMPAT
861 static int vidiocgmbuf (struct file *file, void *priv, struct video_mbuf *mbuf)
863 struct vivi_fh *fh=priv;
865 return videobuf_cgmbuf (&fh->vb_vidq, mbuf, 8);
869 static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
871 struct vivi_fh *fh=priv;
873 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
878 return videobuf_streamon(&fh->vb_vidq);
881 static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
883 struct vivi_fh *fh=priv;
885 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
890 return videobuf_streamoff(&fh->vb_vidq);
893 static int vidioc_s_std (struct file *file, void *priv, v4l2_std_id *i)
898 /* only one input in this sample driver */
899 static int vidioc_enum_input (struct file *file, void *priv,
900 struct v4l2_input *inp)
905 inp->type = V4L2_INPUT_TYPE_CAMERA;
906 inp->std = V4L2_STD_NTSC_M;
907 strcpy(inp->name,"Camera");
912 static int vidioc_g_input (struct file *file, void *priv, unsigned int *i)
918 static int vidioc_s_input (struct file *file, void *priv, unsigned int i)
926 /* --- controls ---------------------------------------------- */
927 static int vidioc_queryctrl (struct file *file, void *priv,
928 struct v4l2_queryctrl *qc)
932 for (i = 0; i < ARRAY_SIZE(vivi_qctrl); i++)
933 if (qc->id && qc->id == vivi_qctrl[i].id) {
934 memcpy(qc, &(vivi_qctrl[i]),
942 static int vidioc_g_ctrl (struct file *file, void *priv,
943 struct v4l2_control *ctrl)
947 for (i = 0; i < ARRAY_SIZE(vivi_qctrl); i++)
948 if (ctrl->id == vivi_qctrl[i].id) {
949 ctrl->value=qctl_regs[i];
955 static int vidioc_s_ctrl (struct file *file, void *priv,
956 struct v4l2_control *ctrl)
960 for (i = 0; i < ARRAY_SIZE(vivi_qctrl); i++)
961 if (ctrl->id == vivi_qctrl[i].id) {
963 vivi_qctrl[i].minimum
965 vivi_qctrl[i].maximum) {
968 qctl_regs[i]=ctrl->value;
974 /* ------------------------------------------------------------------
975 File operations for the device
976 ------------------------------------------------------------------*/
978 #define line_buf_size(norm) (norm_maxw(norm)*(format.depth+7)/8)
980 static int vivi_open(struct inode *inode, struct file *file)
982 int minor = iminor(inode);
983 struct vivi_dev *dev;
987 printk(KERN_DEBUG "vivi: open called (minor=%d)\n",minor);
989 list_for_each_entry(dev, &vivi_devlist, vivi_devlist)
990 if (dev->vfd->minor == minor)
997 /* If more than one user, mutex should be added */
1000 dprintk(1, "open minor=%d type=%s users=%d\n", minor,
1001 v4l2_type_names[V4L2_BUF_TYPE_VIDEO_CAPTURE], dev->users);
1003 /* allocate + initialize per filehandle data */
1004 fh = kzalloc(sizeof(*fh),GFP_KERNEL);
1010 file->private_data = fh;
1013 fh->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1018 /* Put all controls at a sane state */
1019 for (i = 0; i < ARRAY_SIZE(vivi_qctrl); i++)
1020 qctl_regs[i] =vivi_qctrl[i].default_value;
1022 dprintk(1,"Open: fh=0x%08lx, dev=0x%08lx, dev->vidq=0x%08lx\n",
1023 (unsigned long)fh,(unsigned long)dev,(unsigned long)&dev->vidq);
1024 dprintk(1,"Open: list_empty queued=%d\n",list_empty(&dev->vidq.queued));
1025 dprintk(1,"Open: list_empty active=%d\n",list_empty(&dev->vidq.active));
1027 /* Resets frame counters */
1032 dev->jiffies=jiffies;
1033 sprintf(dev->timestr,"%02d:%02d:%02d:%03d",
1034 dev->h,dev->m,dev->s,(dev->us+500)/1000);
1036 videobuf_queue_vmalloc_init(&fh->vb_vidq, &vivi_video_qops,
1039 V4L2_FIELD_INTERLACED,
1040 sizeof(struct vivi_buffer),fh);
1046 vivi_read(struct file *file, char __user *data, size_t count, loff_t *ppos)
1048 struct vivi_fh *fh = file->private_data;
1050 if (fh->type==V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1051 return videobuf_read_stream(&fh->vb_vidq, data, count, ppos, 0,
1052 file->f_flags & O_NONBLOCK);
1058 vivi_poll(struct file *file, struct poll_table_struct *wait)
1060 struct vivi_fh *fh = file->private_data;
1061 struct videobuf_queue *q = &fh->vb_vidq;
1063 dprintk(1,"%s\n",__FUNCTION__);
1065 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != fh->type)
1068 return videobuf_poll_stream(file, q, wait);
1071 static int vivi_close(struct inode *inode, struct file *file)
1073 struct vivi_fh *fh = file->private_data;
1074 struct vivi_dev *dev = fh->dev;
1075 struct vivi_dmaqueue *vidq = &dev->vidq;
1077 int minor = iminor(inode);
1079 vivi_stop_thread(vidq);
1080 videobuf_stop(&fh->vb_vidq);
1081 videobuf_mmap_free(&fh->vb_vidq);
1087 printk(KERN_DEBUG "vivi: close called (minor=%d, users=%d)\n",minor,dev->users);
1092 static int vivi_release(void)
1094 struct vivi_dev *dev;
1095 struct list_head *list;
1097 while (!list_empty(&vivi_devlist)) {
1098 list = vivi_devlist.next;
1100 dev = list_entry(list, struct vivi_dev, vivi_devlist);
1102 if (-1 != dev->vfd->minor)
1103 video_unregister_device(dev->vfd);
1105 video_device_release(dev->vfd);
1114 vivi_mmap(struct file *file, struct vm_area_struct * vma)
1116 struct vivi_fh *fh = file->private_data;
1119 dprintk (1,"mmap called, vma=0x%08lx\n",(unsigned long)vma);
1121 ret=videobuf_mmap_mapper(&fh->vb_vidq, vma);
1123 dprintk (1,"vma start=0x%08lx, size=%ld, ret=%d\n",
1124 (unsigned long)vma->vm_start,
1125 (unsigned long)vma->vm_end-(unsigned long)vma->vm_start,
1131 static const struct file_operations vivi_fops = {
1132 .owner = THIS_MODULE,
1134 .release = vivi_close,
1137 .ioctl = video_ioctl2, /* V4L2 ioctl handler */
1139 .llseek = no_llseek,
1142 static struct video_device vivi_template = {
1144 .type = VID_TYPE_CAPTURE,
1147 .release = video_device_release,
1149 .vidioc_querycap = vidioc_querycap,
1150 .vidioc_enum_fmt_cap = vidioc_enum_fmt_cap,
1151 .vidioc_g_fmt_cap = vidioc_g_fmt_cap,
1152 .vidioc_try_fmt_cap = vidioc_try_fmt_cap,
1153 .vidioc_s_fmt_cap = vidioc_s_fmt_cap,
1154 .vidioc_reqbufs = vidioc_reqbufs,
1155 .vidioc_querybuf = vidioc_querybuf,
1156 .vidioc_qbuf = vidioc_qbuf,
1157 .vidioc_dqbuf = vidioc_dqbuf,
1158 .vidioc_s_std = vidioc_s_std,
1159 .vidioc_enum_input = vidioc_enum_input,
1160 .vidioc_g_input = vidioc_g_input,
1161 .vidioc_s_input = vidioc_s_input,
1162 .vidioc_queryctrl = vidioc_queryctrl,
1163 .vidioc_g_ctrl = vidioc_g_ctrl,
1164 .vidioc_s_ctrl = vidioc_s_ctrl,
1165 .vidioc_streamon = vidioc_streamon,
1166 .vidioc_streamoff = vidioc_streamoff,
1167 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1168 .vidiocgmbuf = vidiocgmbuf,
1170 .tvnorms = V4L2_STD_NTSC_M,
1171 .current_norm = V4L2_STD_NTSC_M,
1173 /* -----------------------------------------------------------------
1174 Initialization and module stuff
1175 ------------------------------------------------------------------*/
1177 static int __init vivi_init(void)
1179 int ret = -ENOMEM, i;
1180 struct vivi_dev *dev;
1181 struct video_device *vfd;
1183 for (i = 0; i < n_devs; i++) {
1184 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1188 list_add_tail(&dev->vivi_devlist, &vivi_devlist);
1190 /* init video dma queues */
1191 INIT_LIST_HEAD(&dev->vidq.active);
1192 INIT_LIST_HEAD(&dev->vidq.queued);
1193 init_waitqueue_head(&dev->vidq.wq);
1195 /* initialize locks */
1196 mutex_init(&dev->lock);
1198 dev->vidq.timeout.function = vivi_vid_timeout;
1199 dev->vidq.timeout.data = (unsigned long)dev;
1200 init_timer(&dev->vidq.timeout);
1202 vfd = video_device_alloc();
1206 *vfd = vivi_template;
1208 ret = video_register_device(vfd, VFL_TYPE_GRABBER, video_nr);
1212 snprintf(vfd->name, sizeof(vfd->name), "%s (%i)",
1213 vivi_template.name, vfd->minor);
1223 printk(KERN_INFO "Error %d while loading vivi driver\n", ret);
1225 printk(KERN_INFO "Video Technology Magazine Virtual Video "
1226 "Capture Board successfully loaded.\n");
1230 static void __exit vivi_exit(void)
1235 module_init(vivi_init);
1236 module_exit(vivi_exit);
1238 MODULE_DESCRIPTION("Video Technology Magazine Virtual Video Capture Board");
1239 MODULE_AUTHOR("Mauro Carvalho Chehab, Ted Walther and John Sokol");
1240 MODULE_LICENSE("Dual BSD/GPL");
1242 module_param(video_nr, int, 0);
1243 MODULE_PARM_DESC(video_nr, "video iminor start number");
1245 module_param(n_devs, int, 0);
1246 MODULE_PARM_DESC(n_devs, "number of video devices to create");
1248 module_param_named(debug, vivi.debug, int, 0644);
1249 MODULE_PARM_DESC(debug, "activates debug info");
1251 module_param(vid_limit, int, 0644);
1252 MODULE_PARM_DESC(vid_limit, "capture memory limit in megabytes");