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;
148 struct sg_to_addr *to_addr;
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;
169 struct semaphore lock;
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
233 static void prep_to_addr(struct sg_to_addr to_addr[],
234 struct videobuf_buffer *vb)
238 for (i=0;i<vb->dma.nr_pages;i++) {
239 to_addr[i].sg=&vb->dma.sglist[i];
241 pos += vb->dma.sglist[i].length;
245 static int get_addr_pos(int pos, int pages, struct sg_to_addr to_addr[])
247 int p1=0,p2=pages-1,p3=pages/2;
250 BUG_ON (pos>=to_addr[p2].pos+to_addr[p2].sg->length);
253 if (pos < to_addr[p3].pos) {
260 if (pos >= to_addr[p2].pos)
266 static void gen_line(struct sg_to_addr to_addr[],int inipos,int pages,int wmax,
267 int hmax, int line, char *timestr)
269 int w,i,j,pos=inipos,pgpos,oldpg,y;
276 spin_lock_init(&spinlock);
278 /* Get first addr pointed to pixel position */
279 oldpg=get_addr_pos(pos,pages,to_addr);
280 pg=pfn_to_page(sg_dma_address(to_addr[oldpg].sg) >> PAGE_SHIFT);
281 spin_lock_irqsave(&spinlock,flags);
282 basep = kmap_atomic(pg, KM_BOUNCE_READ)+to_addr[oldpg].sg->offset;
284 /* We will just duplicate the second pixel at the packet */
287 /* Generate a standard color bar pattern */
288 for (w=0;w<wmax;w++) {
293 for (color=0;color<4;color++) {
294 pgpos=get_addr_pos(pos,pages,to_addr);
296 pg=pfn_to_page(sg_dma_address(to_addr[pgpos].sg) >> PAGE_SHIFT);
297 kunmap_atomic(basep, KM_BOUNCE_READ);
298 basep= kmap_atomic(pg, KM_BOUNCE_READ)+to_addr[pgpos].sg->offset;
301 p=basep+pos-to_addr[pgpos].pos;
306 *p=TO_Y(r,g,b); /* Luminance */
309 *p=TO_U(r,g,b); /* Cb */
312 *p=TO_V(r,g,b); /* Cr */
319 /* Checks if it is possible to show timestamp */
320 if (TSTAMP_MAX_Y>=hmax)
322 if (TSTAMP_MIN_X+strlen(timestr)>=wmax)
325 /* Print stream time */
326 if (line>=TSTAMP_MIN_Y && line<=TSTAMP_MAX_Y) {
328 for (s=timestr;*s;s++) {
329 chr=rom8x16_bits[(*s-0x30)*16+line-TSTAMP_MIN_Y];
331 if (chr&1<<(7-i)) { /* Font color*/
337 } else { /* Background color */
345 for (color=0;color<4;color++) {
346 pgpos=get_addr_pos(pos,pages,to_addr);
348 pg=pfn_to_page(sg_dma_address(
353 basep= kmap_atomic(pg,
355 to_addr[pgpos].sg->offset;
358 p=basep+pos-to_addr[pgpos].pos;
365 *p=TO_Y(r,g,b); /* Luminance */
368 *p=TO_U(r,g,b); /* Cb */
371 *p=TO_V(r,g,b); /* Cr */
383 kunmap_atomic(basep, KM_BOUNCE_READ);
384 spin_unlock_irqrestore(&spinlock,flags);
387 static void vivi_fillbuff(struct vivi_dev *dev,struct vivi_buffer *buf)
390 int hmax = buf->vb.height;
391 int wmax = buf->vb.width;
392 struct videobuf_buffer *vb=&buf->vb;
393 struct sg_to_addr *to_addr=buf->to_addr;
396 /* Test if DMA mapping is ready */
397 if (!sg_dma_address(&vb->dma.sglist[0]))
400 prep_to_addr(to_addr,vb);
402 /* Check if there is enough memory */
403 BUG_ON(buf->vb.dma.nr_pages << PAGE_SHIFT < (buf->vb.width*buf->vb.height)*2);
405 for (h=0;h<hmax;h++) {
406 gen_line(to_addr,pos,vb->dma.nr_pages,wmax,hmax,h,dev->timestr);
410 /* Updates stream time */
412 dev->us+=jiffies_to_usecs(jiffies-dev->jiffies);
413 dev->jiffies=jiffies;
414 if (dev->us>=1000000) {
428 sprintf(dev->timestr,"%02d:%02d:%02d:%03d",
429 dev->h,dev->m,dev->s,(dev->us+500)/1000);
431 dprintk(2,"vivifill at %s: Buffer 0x%08lx size= %d\n",dev->timestr,
432 (unsigned long)buf->vb.dma.vmalloc,pos);
434 /* Advice that buffer was filled */
435 buf->vb.state = STATE_DONE;
436 buf->vb.field_count++;
437 do_gettimeofday(&ts);
440 list_del(&buf->vb.queue);
441 wake_up(&buf->vb.done);
444 static int restart_video_queue(struct vivi_dmaqueue *dma_q);
446 static void vivi_thread_tick(struct vivi_dmaqueue *dma_q)
448 struct vivi_buffer *buf;
449 struct vivi_dev *dev= container_of(dma_q,struct vivi_dev,vidq);
453 /* Announces videobuf that all went ok */
454 for (bc = 0;; bc++) {
455 if (list_empty(&dma_q->active)) {
456 dprintk(1,"No active queue to serve\n");
460 buf = list_entry(dma_q->active.next,
461 struct vivi_buffer, vb.queue);
463 /* Nobody is waiting something to be done, just return */
464 if (!waitqueue_active(&buf->vb.done)) {
465 mod_timer(&dma_q->timeout, jiffies+BUFFER_TIMEOUT);
469 do_gettimeofday(&buf->vb.ts);
470 dprintk(2,"[%p/%d] wakeup\n",buf,buf->vb.i);
473 vivi_fillbuff(dev,buf);
475 if (list_empty(&dma_q->active)) {
476 del_timer(&dma_q->timeout);
478 mod_timer(&dma_q->timeout, jiffies+BUFFER_TIMEOUT);
482 dprintk(1,"%s: %d buffers handled (should be 1)\n",__FUNCTION__,bc);
485 static void vivi_sleep(struct vivi_dmaqueue *dma_q)
488 DECLARE_WAITQUEUE(wait, current);
490 dprintk(1,"%s dma_q=0x%08lx\n",__FUNCTION__,(unsigned long)dma_q);
492 add_wait_queue(&dma_q->wq, &wait);
493 if (!kthread_should_stop()) {
496 /* Calculate time to wake up */
497 timeout=dma_q->ini_jiffies+msecs_to_jiffies((dma_q->frame*WAKE_NUMERATOR*1000)/WAKE_DENOMINATOR)-jiffies;
500 int old=dma_q->frame;
501 dma_q->frame=(jiffies_to_msecs(jiffies-dma_q->ini_jiffies)*WAKE_DENOMINATOR)/(WAKE_NUMERATOR*1000)+1;
503 timeout=dma_q->ini_jiffies+msecs_to_jiffies((dma_q->frame*WAKE_NUMERATOR*1000)/WAKE_DENOMINATOR)-jiffies;
505 dprintk(1,"underrun, losed %d frames. "
506 "Now, frame is %d. Waking on %d jiffies\n",
507 dma_q->frame-old,dma_q->frame,timeout);
509 dprintk(1,"will sleep for %i jiffies\n",timeout);
511 vivi_thread_tick(dma_q);
513 schedule_timeout_interruptible (timeout);
516 remove_wait_queue(&dma_q->wq, &wait);
520 static int vivi_thread(void *data)
522 struct vivi_dmaqueue *dma_q=data;
524 dprintk(1,"thread started\n");
526 mod_timer(&dma_q->timeout, jiffies+BUFFER_TIMEOUT);
531 if (kthread_should_stop())
534 dprintk(1, "thread: exit\n");
538 static int vivi_start_thread(struct vivi_dmaqueue *dma_q)
541 dma_q->ini_jiffies=jiffies;
543 dprintk(1,"%s\n",__FUNCTION__);
545 dma_q->kthread = kthread_run(vivi_thread, dma_q, "vivi");
547 if (IS_ERR(dma_q->kthread)) {
548 printk(KERN_ERR "vivi: kernel_thread() failed\n");
549 return PTR_ERR(dma_q->kthread);
552 wake_up_interruptible(&dma_q->wq);
554 dprintk(1,"returning from %s\n",__FUNCTION__);
558 static void vivi_stop_thread(struct vivi_dmaqueue *dma_q)
560 dprintk(1,"%s\n",__FUNCTION__);
561 /* shutdown control thread */
562 if (dma_q->kthread) {
563 kthread_stop(dma_q->kthread);
568 static int restart_video_queue(struct vivi_dmaqueue *dma_q)
570 struct vivi_buffer *buf, *prev;
571 struct list_head *item;
573 dprintk(1,"%s dma_q=0x%08lx\n",__FUNCTION__,(unsigned long)dma_q);
575 if (!list_empty(&dma_q->active)) {
576 buf = list_entry(dma_q->active.next, struct vivi_buffer, vb.queue);
577 dprintk(2,"restart_queue [%p/%d]: restart dma\n",
580 dprintk(1,"Restarting video dma\n");
581 vivi_stop_thread(dma_q);
582 // vivi_start_thread(dma_q);
584 /* cancel all outstanding capture / vbi requests */
585 list_for_each(item,&dma_q->active) {
586 buf = list_entry(item, struct vivi_buffer, vb.queue);
588 list_del(&buf->vb.queue);
589 buf->vb.state = STATE_ERROR;
590 wake_up(&buf->vb.done);
592 mod_timer(&dma_q->timeout, jiffies+BUFFER_TIMEOUT);
599 if (list_empty(&dma_q->queued))
601 buf = list_entry(dma_q->queued.next, struct vivi_buffer, vb.queue);
603 list_del(&buf->vb.queue);
604 list_add_tail(&buf->vb.queue,&dma_q->active);
606 dprintk(1,"Restarting video dma\n");
607 vivi_stop_thread(dma_q);
608 vivi_start_thread(dma_q);
610 buf->vb.state = STATE_ACTIVE;
611 mod_timer(&dma_q->timeout, jiffies+BUFFER_TIMEOUT);
612 dprintk(2,"[%p/%d] restart_queue - first active\n",
615 } else if (prev->vb.width == buf->vb.width &&
616 prev->vb.height == buf->vb.height &&
617 prev->fmt == buf->fmt) {
618 list_del(&buf->vb.queue);
619 list_add_tail(&buf->vb.queue,&dma_q->active);
620 buf->vb.state = STATE_ACTIVE;
621 dprintk(2,"[%p/%d] restart_queue - move to active\n",
630 static void vivi_vid_timeout(unsigned long data)
632 struct vivi_dev *dev = (struct vivi_dev*)data;
633 struct vivi_dmaqueue *vidq = &dev->vidq;
634 struct vivi_buffer *buf;
636 while (!list_empty(&vidq->active)) {
637 buf = list_entry(vidq->active.next, struct vivi_buffer, vb.queue);
638 list_del(&buf->vb.queue);
639 buf->vb.state = STATE_ERROR;
640 wake_up(&buf->vb.done);
641 printk("vivi/0: [%p/%d] timeout\n", buf, buf->vb.i);
644 restart_video_queue(vidq);
647 /* ------------------------------------------------------------------
649 ------------------------------------------------------------------*/
651 buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size)
653 struct vivi_fh *fh = vq->priv_data;
655 *size = fh->width*fh->height*2;
659 while (*size * *count > vid_limit * 1024 * 1024)
664 static void free_buffer(struct videobuf_queue *vq, struct vivi_buffer *buf)
666 dprintk(1,"%s\n",__FUNCTION__);
671 /*FIXME: Maybe a spinlock is required here */
675 videobuf_waiton(&buf->vb,0,0);
676 videobuf_dma_unmap(vq, &buf->vb.dma);
677 videobuf_dma_free(&buf->vb.dma);
678 buf->vb.state = STATE_NEEDS_INIT;
681 #define norm_maxw() 1024
682 #define norm_maxh() 768
684 buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
685 enum v4l2_field field)
687 struct vivi_fh *fh = vq->priv_data;
688 struct vivi_buffer *buf = container_of(vb,struct vivi_buffer,vb);
689 int rc, init_buffer = 0;
691 // dprintk(1,"%s, field=%d\n",__FUNCTION__,field);
693 BUG_ON(NULL == fh->fmt);
694 if (fh->width < 48 || fh->width > norm_maxw() ||
695 fh->height < 32 || fh->height > norm_maxh())
697 buf->vb.size = fh->width*fh->height*2;
698 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
701 if (buf->fmt != fh->fmt ||
702 buf->vb.width != fh->width ||
703 buf->vb.height != fh->height ||
704 buf->vb.field != field) {
706 buf->vb.width = fh->width;
707 buf->vb.height = fh->height;
708 buf->vb.field = field;
712 if (STATE_NEEDS_INIT == buf->vb.state) {
713 if (0 != (rc = videobuf_iolock(vq,&buf->vb,NULL)))
717 buf->vb.state = STATE_PREPARED;
719 if (NULL == (buf->to_addr = kmalloc(sizeof(*buf->to_addr) * vb->dma.nr_pages,GFP_KERNEL))) {
732 buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
734 struct vivi_buffer *buf = container_of(vb,struct vivi_buffer,vb);
735 struct vivi_fh *fh = vq->priv_data;
736 struct vivi_dev *dev = fh->dev;
737 struct vivi_dmaqueue *vidq = &dev->vidq;
738 struct vivi_buffer *prev;
740 if (!list_empty(&vidq->queued)) {
741 dprintk(1,"adding vb queue=0x%08lx\n",(unsigned long)&buf->vb.queue);
742 list_add_tail(&buf->vb.queue,&vidq->queued);
743 buf->vb.state = STATE_QUEUED;
744 dprintk(2,"[%p/%d] buffer_queue - append to queued\n",
746 } else if (list_empty(&vidq->active)) {
747 list_add_tail(&buf->vb.queue,&vidq->active);
749 buf->vb.state = STATE_ACTIVE;
750 mod_timer(&vidq->timeout, jiffies+BUFFER_TIMEOUT);
751 dprintk(2,"[%p/%d] buffer_queue - first active\n",
754 vivi_start_thread(vidq);
756 prev = list_entry(vidq->active.prev, struct vivi_buffer, vb.queue);
757 if (prev->vb.width == buf->vb.width &&
758 prev->vb.height == buf->vb.height &&
759 prev->fmt == buf->fmt) {
760 list_add_tail(&buf->vb.queue,&vidq->active);
761 buf->vb.state = STATE_ACTIVE;
762 dprintk(2,"[%p/%d] buffer_queue - append to active\n",
766 list_add_tail(&buf->vb.queue,&vidq->queued);
767 buf->vb.state = STATE_QUEUED;
768 dprintk(2,"[%p/%d] buffer_queue - first queued\n",
774 static void buffer_release(struct videobuf_queue *vq, struct videobuf_buffer *vb)
776 struct vivi_buffer *buf = container_of(vb,struct vivi_buffer,vb);
777 struct vivi_fh *fh = vq->priv_data;
778 struct vivi_dev *dev = (struct vivi_dev*)fh->dev;
779 struct vivi_dmaqueue *vidq = &dev->vidq;
781 dprintk(1,"%s\n",__FUNCTION__);
783 vivi_stop_thread(vidq);
788 static int vivi_map_sg(void *dev, struct scatterlist *sg, int nents,
793 dprintk(1,"%s, number of pages=%d\n",__FUNCTION__,nents);
794 BUG_ON(direction == DMA_NONE);
796 for (i = 0; i < nents; i++ ) {
799 sg_dma_address(&sg[i]) = page_to_phys(sg[i].page) + sg[i].offset;
805 static int vivi_unmap_sg(void *dev,struct scatterlist *sglist,int nr_pages,
808 dprintk(1,"%s\n",__FUNCTION__);
812 static int vivi_dma_sync_sg(void *dev,struct scatterlist *sglist, int nr_pages,
815 // dprintk(1,"%s\n",__FUNCTION__);
817 // flush_write_buffers();
821 static struct videobuf_queue_ops vivi_video_qops = {
822 .buf_setup = buffer_setup,
823 .buf_prepare = buffer_prepare,
824 .buf_queue = buffer_queue,
825 .buf_release = buffer_release,
827 /* Non-pci handling routines */
828 .vb_map_sg = vivi_map_sg,
829 .vb_dma_sync_sg = vivi_dma_sync_sg,
830 .vb_unmap_sg = vivi_unmap_sg,
833 /* ------------------------------------------------------------------
835 ------------------------------------------------------------------*/
838 static int res_get(struct vivi_dev *dev, struct vivi_fh *fh)
842 if (dev->resources) {
843 /* no, someone else uses it */
847 /* it's free, grab it */
849 dprintk(1,"res: get\n");
854 static int res_locked(struct vivi_dev *dev)
856 return (dev->resources);
859 static void res_free(struct vivi_dev *dev, struct vivi_fh *fh)
863 dprintk(1,"res: put\n");
867 /* ------------------------------------------------------------------
868 IOCTL vidioc handling
869 ------------------------------------------------------------------*/
870 static int vidioc_querycap (struct file *file, void *priv,
871 struct v4l2_capability *cap)
873 strcpy(cap->driver, "vivi");
874 strcpy(cap->card, "vivi");
875 cap->version = VIVI_VERSION;
876 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE |
882 static int vidioc_enum_fmt_cap (struct file *file, void *priv,
883 struct v4l2_fmtdesc *f)
888 strlcpy(f->description,format.name,sizeof(f->description));
889 f->pixelformat = format.fourcc;
893 static int vidioc_g_fmt_cap (struct file *file, void *priv,
894 struct v4l2_format *f)
896 struct vivi_fh *fh=priv;
898 f->fmt.pix.width = fh->width;
899 f->fmt.pix.height = fh->height;
900 f->fmt.pix.field = fh->vb_vidq.field;
901 f->fmt.pix.pixelformat = fh->fmt->fourcc;
902 f->fmt.pix.bytesperline =
903 (f->fmt.pix.width * fh->fmt->depth) >> 3;
904 f->fmt.pix.sizeimage =
905 f->fmt.pix.height * f->fmt.pix.bytesperline;
910 static int vidioc_try_fmt_cap (struct file *file, void *priv,
911 struct v4l2_format *f)
913 struct vivi_fmt *fmt;
914 enum v4l2_field field;
915 unsigned int maxw, maxh;
917 if (format.fourcc != f->fmt.pix.pixelformat) {
918 dprintk(1,"Fourcc format (0x%08x) invalid. Driver accepts "
919 "only 0x%08x\n",f->fmt.pix.pixelformat,format.fourcc);
924 field = f->fmt.pix.field;
926 if (field == V4L2_FIELD_ANY) {
927 // field=V4L2_FIELD_INTERLACED;
928 field=V4L2_FIELD_SEQ_TB;
929 } else if (V4L2_FIELD_INTERLACED != field) {
930 dprintk(1,"Field type invalid.\n");
937 f->fmt.pix.field = field;
938 if (f->fmt.pix.height < 32)
939 f->fmt.pix.height = 32;
940 if (f->fmt.pix.height > maxh)
941 f->fmt.pix.height = maxh;
942 if (f->fmt.pix.width < 48)
943 f->fmt.pix.width = 48;
944 if (f->fmt.pix.width > maxw)
945 f->fmt.pix.width = maxw;
946 f->fmt.pix.width &= ~0x03;
947 f->fmt.pix.bytesperline =
948 (f->fmt.pix.width * fmt->depth) >> 3;
949 f->fmt.pix.sizeimage =
950 f->fmt.pix.height * f->fmt.pix.bytesperline;
955 /*FIXME: This seems to be generic enough to be at videodev2 */
956 static int vidioc_s_fmt_cap (struct file *file, void *priv,
957 struct v4l2_format *f)
959 struct vivi_fh *fh=priv;
960 int ret = vidioc_try_fmt_cap(file,fh,f);
965 fh->width = f->fmt.pix.width;
966 fh->height = f->fmt.pix.height;
967 fh->vb_vidq.field = f->fmt.pix.field;
973 static int vidioc_reqbufs (struct file *file, void *priv, struct v4l2_requestbuffers *p)
975 struct vivi_fh *fh=priv;
977 return (videobuf_reqbufs(&fh->vb_vidq, p));
980 static int vidioc_querybuf (struct file *file, void *priv, struct v4l2_buffer *p)
982 struct vivi_fh *fh=priv;
984 return (videobuf_querybuf(&fh->vb_vidq, p));
987 static int vidioc_qbuf (struct file *file, void *priv, struct v4l2_buffer *p)
989 struct vivi_fh *fh=priv;
991 return (videobuf_qbuf(&fh->vb_vidq, p));
994 static int vidioc_dqbuf (struct file *file, void *priv, struct v4l2_buffer *p)
996 struct vivi_fh *fh=priv;
998 return (videobuf_dqbuf(&fh->vb_vidq, p,
999 file->f_flags & O_NONBLOCK));
1002 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1003 static int vidiocgmbuf (struct file *file, void *priv, struct video_mbuf *mbuf)
1005 struct vivi_fh *fh=priv;
1006 struct videobuf_queue *q=&fh->vb_vidq;
1007 struct v4l2_requestbuffers req;
1013 req.memory = V4L2_MEMORY_MMAP;
1014 ret = videobuf_reqbufs(q,&req);
1018 mbuf->frames = req.count;
1020 for (i = 0; i < mbuf->frames; i++) {
1021 mbuf->offsets[i] = q->bufs[i]->boff;
1022 mbuf->size += q->bufs[i]->bsize;
1028 static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
1030 struct vivi_fh *fh=priv;
1031 struct vivi_dev *dev = fh->dev;
1033 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1038 if (!res_get(dev,fh))
1040 return (videobuf_streamon(&fh->vb_vidq));
1043 static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
1045 struct vivi_fh *fh=priv;
1046 struct vivi_dev *dev = fh->dev;
1048 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1053 videobuf_streamoff(&fh->vb_vidq);
1059 static int vidioc_s_std (struct file *file, void *priv, v4l2_std_id *i)
1064 /* only one input in this sample driver */
1065 static int vidioc_enum_input (struct file *file, void *priv,
1066 struct v4l2_input *inp)
1068 if (inp->index != 0)
1071 inp->type = V4L2_INPUT_TYPE_CAMERA;
1072 inp->std = V4L2_STD_NTSC_M;
1073 strcpy(inp->name,"Camera");
1078 static int vidioc_g_input (struct file *file, void *priv, unsigned int *i)
1084 static int vidioc_s_input (struct file *file, void *priv, unsigned int i)
1092 /* --- controls ---------------------------------------------- */
1093 static int vidioc_queryctrl (struct file *file, void *priv,
1094 struct v4l2_queryctrl *qc)
1098 for (i = 0; i < ARRAY_SIZE(vivi_qctrl); i++)
1099 if (qc->id && qc->id == vivi_qctrl[i].id) {
1100 memcpy(qc, &(vivi_qctrl[i]),
1108 static int vidioc_g_ctrl (struct file *file, void *priv,
1109 struct v4l2_control *ctrl)
1113 for (i = 0; i < ARRAY_SIZE(vivi_qctrl); i++)
1114 if (ctrl->id == vivi_qctrl[i].id) {
1115 ctrl->value=qctl_regs[i];
1121 static int vidioc_s_ctrl (struct file *file, void *priv,
1122 struct v4l2_control *ctrl)
1126 for (i = 0; i < ARRAY_SIZE(vivi_qctrl); i++)
1127 if (ctrl->id == vivi_qctrl[i].id) {
1129 vivi_qctrl[i].minimum
1131 vivi_qctrl[i].maximum) {
1134 qctl_regs[i]=ctrl->value;
1140 /* ------------------------------------------------------------------
1141 File operations for the device
1142 ------------------------------------------------------------------*/
1144 #define line_buf_size(norm) (norm_maxw(norm)*(format.depth+7)/8)
1146 static int vivi_open(struct inode *inode, struct file *file)
1148 int minor = iminor(inode);
1149 struct vivi_dev *h,*dev = NULL;
1151 struct list_head *list;
1152 enum v4l2_buf_type type = 0;
1155 printk(KERN_DEBUG "vivi: open called (minor=%d)\n",minor);
1157 list_for_each(list,&vivi_devlist) {
1158 h = list_entry(list, struct vivi_dev, vivi_devlist);
1159 if (h->vfd.minor == minor) {
1161 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1169 /* If more than one user, mutex should be added */
1172 dprintk(1,"open minor=%d type=%s users=%d\n",
1173 minor,v4l2_type_names[type],dev->users);
1175 /* allocate + initialize per filehandle data */
1176 fh = kzalloc(sizeof(*fh),GFP_KERNEL);
1182 file->private_data = fh;
1185 fh->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1190 /* Put all controls at a sane state */
1191 for (i = 0; i < ARRAY_SIZE(vivi_qctrl); i++)
1192 qctl_regs[i] =vivi_qctrl[i].default_value;
1194 dprintk(1,"Open: fh=0x%08lx, dev=0x%08lx, dev->vidq=0x%08lx\n",
1195 (unsigned long)fh,(unsigned long)dev,(unsigned long)&dev->vidq);
1196 dprintk(1,"Open: list_empty queued=%d\n",list_empty(&dev->vidq.queued));
1197 dprintk(1,"Open: list_empty active=%d\n",list_empty(&dev->vidq.active));
1199 /* Resets frame counters */
1204 dev->jiffies=jiffies;
1205 sprintf(dev->timestr,"%02d:%02d:%02d:%03d",
1206 dev->h,dev->m,dev->s,(dev->us+500)/1000);
1208 videobuf_queue_init(&fh->vb_vidq, &vivi_video_qops,
1211 V4L2_FIELD_INTERLACED,
1212 sizeof(struct vivi_buffer),fh);
1218 vivi_read(struct file *file, char __user *data, size_t count, loff_t *ppos)
1220 struct vivi_fh *fh = file->private_data;
1222 if (fh->type==V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1223 if (res_locked(fh->dev))
1225 return videobuf_read_one(&fh->vb_vidq, data, count, ppos,
1226 file->f_flags & O_NONBLOCK);
1232 vivi_poll(struct file *file, struct poll_table_struct *wait)
1234 struct vivi_fh *fh = file->private_data;
1235 struct vivi_buffer *buf;
1237 dprintk(1,"%s\n",__FUNCTION__);
1239 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != fh->type)
1242 if (res_get(fh->dev,fh)) {
1243 dprintk(1,"poll: mmap interface\n");
1244 /* streaming capture */
1245 if (list_empty(&fh->vb_vidq.stream))
1247 buf = list_entry(fh->vb_vidq.stream.next,struct vivi_buffer,vb.stream);
1249 dprintk(1,"poll: read() interface\n");
1250 /* read() capture */
1251 buf = (struct vivi_buffer*)fh->vb_vidq.read_buf;
1255 poll_wait(file, &buf->vb.done, wait);
1256 if (buf->vb.state == STATE_DONE ||
1257 buf->vb.state == STATE_ERROR)
1258 return POLLIN|POLLRDNORM;
1262 static int vivi_release(struct inode *inode, struct file *file)
1264 struct vivi_fh *fh = file->private_data;
1265 struct vivi_dev *dev = fh->dev;
1266 struct vivi_dmaqueue *vidq = &dev->vidq;
1268 int minor = iminor(inode);
1270 vivi_stop_thread(vidq);
1271 videobuf_mmap_free(&fh->vb_vidq);
1277 printk(KERN_DEBUG "vivi: close called (minor=%d, users=%d)\n",minor,dev->users);
1283 vivi_mmap(struct file *file, struct vm_area_struct * vma)
1285 struct vivi_fh *fh = file->private_data;
1288 dprintk (1,"mmap called, vma=0x%08lx\n",(unsigned long)vma);
1290 ret=videobuf_mmap_mapper(&fh->vb_vidq, vma);
1292 dprintk (1,"vma start=0x%08lx, size=%ld, ret=%d\n",
1293 (unsigned long)vma->vm_start,
1294 (unsigned long)vma->vm_end-(unsigned long)vma->vm_start,
1300 static const struct file_operations vivi_fops = {
1301 .owner = THIS_MODULE,
1303 .release = vivi_release,
1306 .ioctl = video_ioctl2, /* V4L2 ioctl handler */
1308 .llseek = no_llseek,
1311 static struct video_device vivi = {
1313 .type = VID_TYPE_CAPTURE,
1317 // .release = video_device_release,
1319 .vidioc_querycap = vidioc_querycap,
1320 .vidioc_enum_fmt_cap = vidioc_enum_fmt_cap,
1321 .vidioc_g_fmt_cap = vidioc_g_fmt_cap,
1322 .vidioc_try_fmt_cap = vidioc_try_fmt_cap,
1323 .vidioc_s_fmt_cap = vidioc_s_fmt_cap,
1324 .vidioc_reqbufs = vidioc_reqbufs,
1325 .vidioc_querybuf = vidioc_querybuf,
1326 .vidioc_qbuf = vidioc_qbuf,
1327 .vidioc_dqbuf = vidioc_dqbuf,
1328 .vidioc_s_std = vidioc_s_std,
1329 .vidioc_enum_input = vidioc_enum_input,
1330 .vidioc_g_input = vidioc_g_input,
1331 .vidioc_s_input = vidioc_s_input,
1332 .vidioc_queryctrl = vidioc_queryctrl,
1333 .vidioc_g_ctrl = vidioc_g_ctrl,
1334 .vidioc_s_ctrl = vidioc_s_ctrl,
1335 .vidioc_streamon = vidioc_streamon,
1336 .vidioc_streamoff = vidioc_streamoff,
1337 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1338 .vidiocgmbuf = vidiocgmbuf,
1340 .tvnorms = V4L2_STD_NTSC_M,
1341 .current_norm = V4L2_STD_NTSC_M,
1343 /* -----------------------------------------------------------------
1344 Initialization and module stuff
1345 ------------------------------------------------------------------*/
1347 static int __init vivi_init(void)
1350 struct vivi_dev *dev;
1352 dev = kzalloc(sizeof(*dev),GFP_KERNEL);
1355 list_add_tail(&dev->vivi_devlist,&vivi_devlist);
1357 /* init video dma queues */
1358 INIT_LIST_HEAD(&dev->vidq.active);
1359 INIT_LIST_HEAD(&dev->vidq.queued);
1360 init_waitqueue_head(&dev->vidq.wq);
1362 /* initialize locks */
1363 init_MUTEX(&dev->lock);
1365 dev->vidq.timeout.function = vivi_vid_timeout;
1366 dev->vidq.timeout.data = (unsigned long)dev;
1367 init_timer(&dev->vidq.timeout);
1369 ret = video_register_device(&vivi, VFL_TYPE_GRABBER, video_nr);
1370 printk(KERN_INFO "Video Technology Magazine Virtual Video Capture Board (Load status: %d)\n", ret);
1374 static void __exit vivi_exit(void)
1377 struct list_head *list;
1379 while (!list_empty(&vivi_devlist)) {
1380 list = vivi_devlist.next;
1382 h = list_entry(list, struct vivi_dev, vivi_devlist);
1385 video_unregister_device(&vivi);
1388 module_init(vivi_init);
1389 module_exit(vivi_exit);
1391 MODULE_DESCRIPTION("Video Technology Magazine Virtual Video Capture Board");
1392 MODULE_AUTHOR("Mauro Carvalho Chehab, Ted Walther and John Sokol");
1393 MODULE_LICENSE("Dual BSD/GPL");
1395 module_param(video_nr, int, 0);
1397 module_param_named(debug,vivi.debug, int, 0644);
1398 MODULE_PARM_DESC(debug,"activates debug info");
1400 module_param(vid_limit,int,0644);
1401 MODULE_PARM_DESC(vid_limit,"capture memory limit in megabytes");