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;
274 /* Get first addr pointed to pixel position */
275 oldpg=get_addr_pos(pos,pages,to_addr);
276 pg=pfn_to_page(sg_dma_address(to_addr[oldpg].sg) >> PAGE_SHIFT);
277 basep = kmap_atomic(pg, KM_BOUNCE_READ)+to_addr[oldpg].sg->offset;
279 /* We will just duplicate the second pixel at the packet */
282 /* Generate a standard color bar pattern */
283 for (w=0;w<wmax;w++) {
288 for (color=0;color<4;color++) {
289 pgpos=get_addr_pos(pos,pages,to_addr);
291 pg=pfn_to_page(sg_dma_address(to_addr[pgpos].sg) >> PAGE_SHIFT);
292 kunmap_atomic(basep, KM_BOUNCE_READ);
293 basep= kmap_atomic(pg, KM_BOUNCE_READ)+to_addr[pgpos].sg->offset;
296 p=basep+pos-to_addr[pgpos].pos;
301 *p=TO_Y(r,g,b); /* Luminance */
304 *p=TO_U(r,g,b); /* Cb */
307 *p=TO_V(r,g,b); /* Cr */
314 /* Checks if it is possible to show timestamp */
315 if (TSTAMP_MAX_Y>=hmax)
317 if (TSTAMP_MIN_X+strlen(timestr)>=wmax)
320 /* Print stream time */
321 if (line>=TSTAMP_MIN_Y && line<=TSTAMP_MAX_Y) {
323 for (s=timestr;*s;s++) {
324 chr=rom8x16_bits[(*s-0x30)*16+line-TSTAMP_MIN_Y];
326 if (chr&1<<(7-i)) { /* Font color*/
332 } else { /* Background color */
340 for (color=0;color<4;color++) {
341 pgpos=get_addr_pos(pos,pages,to_addr);
343 pg=pfn_to_page(sg_dma_address(
348 basep= kmap_atomic(pg,
350 to_addr[pgpos].sg->offset;
353 p=basep+pos-to_addr[pgpos].pos;
360 *p=TO_Y(r,g,b); /* Luminance */
363 *p=TO_U(r,g,b); /* Cb */
366 *p=TO_V(r,g,b); /* Cr */
378 kunmap_atomic(basep, KM_BOUNCE_READ);
380 static void vivi_fillbuff(struct vivi_dev *dev,struct vivi_buffer *buf)
383 int hmax = buf->vb.height;
384 int wmax = buf->vb.width;
385 struct videobuf_buffer *vb=&buf->vb;
386 struct sg_to_addr *to_addr=buf->to_addr;
389 /* Test if DMA mapping is ready */
390 if (!sg_dma_address(&vb->dma.sglist[0]))
393 prep_to_addr(to_addr,vb);
395 /* Check if there is enough memory */
396 BUG_ON(buf->vb.dma.nr_pages << PAGE_SHIFT < (buf->vb.width*buf->vb.height)*2);
398 for (h=0;h<hmax;h++) {
399 gen_line(to_addr,pos,vb->dma.nr_pages,wmax,hmax,h,dev->timestr);
403 /* Updates stream time */
405 dev->us+=jiffies_to_usecs(jiffies-dev->jiffies);
406 dev->jiffies=jiffies;
407 if (dev->us>=1000000) {
421 sprintf(dev->timestr,"%02d:%02d:%02d:%03d",
422 dev->h,dev->m,dev->s,(dev->us+500)/1000);
424 dprintk(2,"vivifill at %s: Buffer 0x%08lx size= %d\n",dev->timestr,
425 (unsigned long)buf->vb.dma.vmalloc,pos);
427 /* Advice that buffer was filled */
428 buf->vb.state = STATE_DONE;
429 buf->vb.field_count++;
430 do_gettimeofday(&ts);
433 list_del(&buf->vb.queue);
434 wake_up(&buf->vb.done);
437 static int restart_video_queue(struct vivi_dmaqueue *dma_q);
439 static void vivi_thread_tick(struct vivi_dmaqueue *dma_q)
441 struct vivi_buffer *buf;
442 struct vivi_dev *dev= container_of(dma_q,struct vivi_dev,vidq);
446 /* Announces videobuf that all went ok */
447 for (bc = 0;; bc++) {
448 if (list_empty(&dma_q->active)) {
449 dprintk(1,"No active queue to serve\n");
453 buf = list_entry(dma_q->active.next,
454 struct vivi_buffer, vb.queue);
456 /* Nobody is waiting something to be done, just return */
457 if (!waitqueue_active(&buf->vb.done)) {
458 mod_timer(&dma_q->timeout, jiffies+BUFFER_TIMEOUT);
462 do_gettimeofday(&buf->vb.ts);
463 dprintk(2,"[%p/%d] wakeup\n",buf,buf->vb.i);
466 vivi_fillbuff(dev,buf);
468 if (list_empty(&dma_q->active)) {
469 del_timer(&dma_q->timeout);
471 mod_timer(&dma_q->timeout, jiffies+BUFFER_TIMEOUT);
474 dprintk(1,"%s: %d buffers handled (should be 1)\n",__FUNCTION__,bc);
477 static void vivi_sleep(struct vivi_dmaqueue *dma_q)
480 DECLARE_WAITQUEUE(wait, current);
482 dprintk(1,"%s dma_q=0x%08lx\n",__FUNCTION__,(unsigned long)dma_q);
484 add_wait_queue(&dma_q->wq, &wait);
485 if (!kthread_should_stop()) {
488 /* Calculate time to wake up */
489 timeout=dma_q->ini_jiffies+msecs_to_jiffies((dma_q->frame*WAKE_NUMERATOR*1000)/WAKE_DENOMINATOR)-jiffies;
492 int old=dma_q->frame;
493 dma_q->frame=(jiffies_to_msecs(jiffies-dma_q->ini_jiffies)*WAKE_DENOMINATOR)/(WAKE_NUMERATOR*1000)+1;
495 timeout=dma_q->ini_jiffies+msecs_to_jiffies((dma_q->frame*WAKE_NUMERATOR*1000)/WAKE_DENOMINATOR)-jiffies;
497 dprintk(1,"underrun, losed %d frames. "
498 "Now, frame is %d. Waking on %d jiffies\n",
499 dma_q->frame-old,dma_q->frame,timeout);
501 dprintk(1,"will sleep for %i jiffies\n",timeout);
503 vivi_thread_tick(dma_q);
505 schedule_timeout_interruptible (timeout);
508 remove_wait_queue(&dma_q->wq, &wait);
512 static int vivi_thread(void *data)
514 struct vivi_dmaqueue *dma_q=data;
516 dprintk(1,"thread started\n");
521 if (kthread_should_stop())
524 dprintk(1, "thread: exit\n");
528 static int vivi_start_thread(struct vivi_dmaqueue *dma_q)
531 dma_q->ini_jiffies=jiffies;
533 dprintk(1,"%s\n",__FUNCTION__);
534 init_waitqueue_head(&dma_q->wq);
536 dma_q->kthread = kthread_run(vivi_thread, dma_q, "vivi");
538 if (dma_q->kthread == NULL) {
539 printk(KERN_ERR "vivi: kernel_thread() failed\n");
542 dprintk(1,"returning from %s\n",__FUNCTION__);
546 static void vivi_stop_thread(struct vivi_dmaqueue *dma_q)
548 dprintk(1,"%s\n",__FUNCTION__);
549 /* shutdown control thread */
550 if (dma_q->kthread) {
551 kthread_stop(dma_q->kthread);
556 static int restart_video_queue(struct vivi_dmaqueue *dma_q)
558 struct vivi_buffer *buf, *prev;
559 struct list_head *item;
561 dprintk(1,"%s dma_q=0x%08lx\n",__FUNCTION__,(unsigned long)dma_q);
563 if (!list_empty(&dma_q->active)) {
564 buf = list_entry(dma_q->active.next, struct vivi_buffer, vb.queue);
565 dprintk(2,"restart_queue [%p/%d]: restart dma\n",
568 dprintk(1,"Restarting video dma\n");
569 vivi_stop_thread(dma_q);
570 // vivi_start_thread(dma_q);
572 /* cancel all outstanding capture / vbi requests */
573 list_for_each(item,&dma_q->active) {
574 buf = list_entry(item, struct vivi_buffer, vb.queue);
576 list_del(&buf->vb.queue);
577 buf->vb.state = STATE_ERROR;
578 wake_up(&buf->vb.done);
580 mod_timer(&dma_q->timeout, jiffies+BUFFER_TIMEOUT);
587 if (list_empty(&dma_q->queued))
589 buf = list_entry(dma_q->queued.next, struct vivi_buffer, vb.queue);
591 list_del(&buf->vb.queue);
592 list_add_tail(&buf->vb.queue,&dma_q->active);
594 dprintk(1,"Restarting video dma\n");
595 vivi_stop_thread(dma_q);
596 vivi_start_thread(dma_q);
598 buf->vb.state = STATE_ACTIVE;
599 mod_timer(&dma_q->timeout, jiffies+BUFFER_TIMEOUT);
600 dprintk(2,"[%p/%d] restart_queue - first active\n",
603 } else if (prev->vb.width == buf->vb.width &&
604 prev->vb.height == buf->vb.height &&
605 prev->fmt == buf->fmt) {
606 list_del(&buf->vb.queue);
607 list_add_tail(&buf->vb.queue,&dma_q->active);
608 buf->vb.state = STATE_ACTIVE;
609 dprintk(2,"[%p/%d] restart_queue - move to active\n",
618 static void vivi_vid_timeout(unsigned long data)
620 struct vivi_dev *dev = (struct vivi_dev*)data;
621 struct vivi_dmaqueue *vidq = &dev->vidq;
622 struct vivi_buffer *buf;
624 while (!list_empty(&vidq->active)) {
625 buf = list_entry(vidq->active.next, struct vivi_buffer, vb.queue);
626 list_del(&buf->vb.queue);
627 buf->vb.state = STATE_ERROR;
628 wake_up(&buf->vb.done);
629 printk("vivi/0: [%p/%d] timeout\n", buf, buf->vb.i);
632 restart_video_queue(vidq);
635 /* ------------------------------------------------------------------
637 ------------------------------------------------------------------*/
639 buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size)
641 struct vivi_fh *fh = vq->priv_data;
643 *size = fh->width*fh->height*2;
647 while (*size * *count > vid_limit * 1024 * 1024)
652 static void free_buffer(struct videobuf_queue *vq, struct vivi_buffer *buf)
654 dprintk(1,"%s\n",__FUNCTION__);
659 /*FIXME: Maybe a spinlock is required here */
663 videobuf_waiton(&buf->vb,0,0);
664 videobuf_dma_unmap(vq, &buf->vb.dma);
665 videobuf_dma_free(&buf->vb.dma);
666 buf->vb.state = STATE_NEEDS_INIT;
669 #define norm_maxw() 1024
670 #define norm_maxh() 768
672 buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
673 enum v4l2_field field)
675 struct vivi_fh *fh = vq->priv_data;
676 struct vivi_buffer *buf = container_of(vb,struct vivi_buffer,vb);
677 int rc, init_buffer = 0;
679 // dprintk(1,"%s, field=%d\n",__FUNCTION__,field);
681 BUG_ON(NULL == fh->fmt);
682 if (fh->width < 48 || fh->width > norm_maxw() ||
683 fh->height < 32 || fh->height > norm_maxh())
685 buf->vb.size = fh->width*fh->height*2;
686 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
689 if (buf->fmt != fh->fmt ||
690 buf->vb.width != fh->width ||
691 buf->vb.height != fh->height ||
692 buf->vb.field != field) {
694 buf->vb.width = fh->width;
695 buf->vb.height = fh->height;
696 buf->vb.field = field;
700 if (STATE_NEEDS_INIT == buf->vb.state) {
701 if (0 != (rc = videobuf_iolock(vq,&buf->vb,NULL)))
705 buf->vb.state = STATE_PREPARED;
707 if (NULL == (buf->to_addr = kmalloc(sizeof(*buf->to_addr) * vb->dma.nr_pages,GFP_KERNEL))) {
720 buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
722 struct vivi_buffer *buf = container_of(vb,struct vivi_buffer,vb);
723 struct vivi_fh *fh = vq->priv_data;
724 struct vivi_dev *dev = fh->dev;
725 struct vivi_dmaqueue *vidq = &dev->vidq;
726 struct vivi_buffer *prev;
728 if (!list_empty(&vidq->queued)) {
729 dprintk(1,"adding vb queue=0x%08lx\n",(unsigned long)&buf->vb.queue);
730 list_add_tail(&buf->vb.queue,&vidq->queued);
731 buf->vb.state = STATE_QUEUED;
732 dprintk(2,"[%p/%d] buffer_queue - append to queued\n",
734 } else if (list_empty(&vidq->active)) {
735 list_add_tail(&buf->vb.queue,&vidq->active);
737 buf->vb.state = STATE_ACTIVE;
738 mod_timer(&vidq->timeout, jiffies+BUFFER_TIMEOUT);
739 dprintk(2,"[%p/%d] buffer_queue - first active\n",
742 vivi_start_thread(vidq);
744 prev = list_entry(vidq->active.prev, struct vivi_buffer, vb.queue);
745 if (prev->vb.width == buf->vb.width &&
746 prev->vb.height == buf->vb.height &&
747 prev->fmt == buf->fmt) {
748 list_add_tail(&buf->vb.queue,&vidq->active);
749 buf->vb.state = STATE_ACTIVE;
750 dprintk(2,"[%p/%d] buffer_queue - append to active\n",
754 list_add_tail(&buf->vb.queue,&vidq->queued);
755 buf->vb.state = STATE_QUEUED;
756 dprintk(2,"[%p/%d] buffer_queue - first queued\n",
762 static void buffer_release(struct videobuf_queue *vq, struct videobuf_buffer *vb)
764 struct vivi_buffer *buf = container_of(vb,struct vivi_buffer,vb);
765 struct vivi_fh *fh = vq->priv_data;
766 struct vivi_dev *dev = (struct vivi_dev*)fh->dev;
767 struct vivi_dmaqueue *vidq = &dev->vidq;
769 dprintk(1,"%s\n",__FUNCTION__);
771 vivi_stop_thread(vidq);
776 static int vivi_map_sg(void *dev, struct scatterlist *sg, int nents,
781 dprintk(1,"%s, number of pages=%d\n",__FUNCTION__,nents);
782 BUG_ON(direction == DMA_NONE);
784 for (i = 0; i < nents; i++ ) {
787 sg_dma_address(&sg[i]) = page_to_phys(sg[i].page) + sg[i].offset;
793 static int vivi_unmap_sg(void *dev,struct scatterlist *sglist,int nr_pages,
796 dprintk(1,"%s\n",__FUNCTION__);
800 static int vivi_dma_sync_sg(void *dev,struct scatterlist *sglist, int nr_pages,
803 // dprintk(1,"%s\n",__FUNCTION__);
805 // flush_write_buffers();
809 static struct videobuf_queue_ops vivi_video_qops = {
810 .buf_setup = buffer_setup,
811 .buf_prepare = buffer_prepare,
812 .buf_queue = buffer_queue,
813 .buf_release = buffer_release,
815 /* Non-pci handling routines */
816 .vb_map_sg = vivi_map_sg,
817 .vb_dma_sync_sg = vivi_dma_sync_sg,
818 .vb_unmap_sg = vivi_unmap_sg,
821 /* ------------------------------------------------------------------
823 ------------------------------------------------------------------*/
826 static int res_get(struct vivi_dev *dev, struct vivi_fh *fh)
830 if (dev->resources) {
831 /* no, someone else uses it */
835 /* it's free, grab it */
837 dprintk(1,"res: get\n");
842 static int res_locked(struct vivi_dev *dev)
844 return (dev->resources);
847 static void res_free(struct vivi_dev *dev, struct vivi_fh *fh)
851 dprintk(1,"res: put\n");
855 /* ------------------------------------------------------------------
856 IOCTL vidioc handling
857 ------------------------------------------------------------------*/
858 static int vidioc_querycap (struct file *file, void *priv,
859 struct v4l2_capability *cap)
861 strcpy(cap->driver, "vivi");
862 strcpy(cap->card, "vivi");
863 cap->version = VIVI_VERSION;
864 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE |
870 static int vidioc_enum_fmt_cap (struct file *file, void *priv,
871 struct v4l2_fmtdesc *f)
876 strlcpy(f->description,format.name,sizeof(f->description));
877 f->pixelformat = format.fourcc;
881 static int vidioc_g_fmt_cap (struct file *file, void *priv,
882 struct v4l2_format *f)
884 struct vivi_fh *fh=priv;
886 f->fmt.pix.width = fh->width;
887 f->fmt.pix.height = fh->height;
888 f->fmt.pix.field = fh->vb_vidq.field;
889 f->fmt.pix.pixelformat = fh->fmt->fourcc;
890 f->fmt.pix.bytesperline =
891 (f->fmt.pix.width * fh->fmt->depth) >> 3;
892 f->fmt.pix.sizeimage =
893 f->fmt.pix.height * f->fmt.pix.bytesperline;
898 static int vidioc_try_fmt_cap (struct file *file, void *priv,
899 struct v4l2_format *f)
901 struct vivi_fmt *fmt;
902 enum v4l2_field field;
903 unsigned int maxw, maxh;
905 if (format.fourcc != f->fmt.pix.pixelformat) {
906 dprintk(1,"Fourcc format (0x%08x) invalid. Driver accepts "
907 "only 0x%08x\n",f->fmt.pix.pixelformat,format.fourcc);
912 field = f->fmt.pix.field;
914 if (field == V4L2_FIELD_ANY) {
915 // field=V4L2_FIELD_INTERLACED;
916 field=V4L2_FIELD_SEQ_TB;
917 } else if (V4L2_FIELD_INTERLACED != field) {
918 dprintk(1,"Field type invalid.\n");
925 f->fmt.pix.field = field;
926 if (f->fmt.pix.height < 32)
927 f->fmt.pix.height = 32;
928 if (f->fmt.pix.height > maxh)
929 f->fmt.pix.height = maxh;
930 if (f->fmt.pix.width < 48)
931 f->fmt.pix.width = 48;
932 if (f->fmt.pix.width > maxw)
933 f->fmt.pix.width = maxw;
934 f->fmt.pix.width &= ~0x03;
935 f->fmt.pix.bytesperline =
936 (f->fmt.pix.width * fmt->depth) >> 3;
937 f->fmt.pix.sizeimage =
938 f->fmt.pix.height * f->fmt.pix.bytesperline;
943 /*FIXME: This seems to be generic enough to be at videodev2 */
944 static int vidioc_s_fmt_cap (struct file *file, void *priv,
945 struct v4l2_format *f)
947 struct vivi_fh *fh=priv;
948 int ret = vidioc_try_fmt_cap(file,fh,f);
953 fh->width = f->fmt.pix.width;
954 fh->height = f->fmt.pix.height;
955 fh->vb_vidq.field = f->fmt.pix.field;
961 static int vidioc_reqbufs (struct file *file, void *priv, struct v4l2_requestbuffers *p)
963 struct vivi_fh *fh=priv;
965 return (videobuf_reqbufs(&fh->vb_vidq, p));
968 static int vidioc_querybuf (struct file *file, void *priv, struct v4l2_buffer *p)
970 struct vivi_fh *fh=priv;
972 return (videobuf_querybuf(&fh->vb_vidq, p));
975 static int vidioc_qbuf (struct file *file, void *priv, struct v4l2_buffer *p)
977 struct vivi_fh *fh=priv;
979 return (videobuf_qbuf(&fh->vb_vidq, p));
982 static int vidioc_dqbuf (struct file *file, void *priv, struct v4l2_buffer *p)
984 struct vivi_fh *fh=priv;
986 return (videobuf_dqbuf(&fh->vb_vidq, p,
987 file->f_flags & O_NONBLOCK));
990 #ifdef CONFIG_VIDEO_V4L1_COMPAT
991 static int vidiocgmbuf (struct file *file, void *priv, struct video_mbuf *mbuf)
993 struct vivi_fh *fh=priv;
994 struct videobuf_queue *q=&fh->vb_vidq;
995 struct v4l2_requestbuffers req;
1001 req.memory = V4L2_MEMORY_MMAP;
1002 ret = videobuf_reqbufs(q,&req);
1006 mbuf->frames = req.count;
1008 for (i = 0; i < mbuf->frames; i++) {
1009 mbuf->offsets[i] = q->bufs[i]->boff;
1010 mbuf->size += q->bufs[i]->bsize;
1016 static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
1018 struct vivi_fh *fh=priv;
1019 struct vivi_dev *dev = fh->dev;
1021 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1026 if (!res_get(dev,fh))
1028 return (videobuf_streamon(&fh->vb_vidq));
1031 static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
1033 struct vivi_fh *fh=priv;
1034 struct vivi_dev *dev = fh->dev;
1036 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1041 videobuf_streamoff(&fh->vb_vidq);
1047 static int vidioc_s_std (struct file *file, void *priv, v4l2_std_id *i)
1052 /* only one input in this sample driver */
1053 static int vidioc_enum_input (struct file *file, void *priv,
1054 struct v4l2_input *inp)
1056 if (inp->index != 0)
1059 inp->type = V4L2_INPUT_TYPE_CAMERA;
1060 inp->std = V4L2_STD_NTSC_M;
1061 strcpy(inp->name,"Camera");
1066 static int vidioc_g_input (struct file *file, void *priv, unsigned int *i)
1072 static int vidioc_s_input (struct file *file, void *priv, unsigned int i)
1080 /* --- controls ---------------------------------------------- */
1081 static int vidioc_queryctrl (struct file *file, void *priv,
1082 struct v4l2_queryctrl *qc)
1086 for (i = 0; i < ARRAY_SIZE(vivi_qctrl); i++)
1087 if (qc->id && qc->id == vivi_qctrl[i].id) {
1088 memcpy(qc, &(vivi_qctrl[i]),
1096 static int vidioc_g_ctrl (struct file *file, void *priv,
1097 struct v4l2_control *ctrl)
1101 for (i = 0; i < ARRAY_SIZE(vivi_qctrl); i++)
1102 if (ctrl->id == vivi_qctrl[i].id) {
1103 ctrl->value=qctl_regs[i];
1109 static int vidioc_s_ctrl (struct file *file, void *priv,
1110 struct v4l2_control *ctrl)
1114 for (i = 0; i < ARRAY_SIZE(vivi_qctrl); i++)
1115 if (ctrl->id == vivi_qctrl[i].id) {
1117 vivi_qctrl[i].minimum
1119 vivi_qctrl[i].maximum) {
1122 qctl_regs[i]=ctrl->value;
1128 /* ------------------------------------------------------------------
1129 File operations for the device
1130 ------------------------------------------------------------------*/
1132 #define line_buf_size(norm) (norm_maxw(norm)*(format.depth+7)/8)
1134 static int vivi_open(struct inode *inode, struct file *file)
1136 int minor = iminor(inode);
1137 struct vivi_dev *h,*dev = NULL;
1139 struct list_head *list;
1140 enum v4l2_buf_type type = 0;
1143 printk(KERN_DEBUG "vivi: open called (minor=%d)\n",minor);
1145 list_for_each(list,&vivi_devlist) {
1146 h = list_entry(list, struct vivi_dev, vivi_devlist);
1147 if (h->vfd.minor == minor) {
1149 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1157 /* If more than one user, mutex should be added */
1160 dprintk(1,"open minor=%d type=%s users=%d\n",
1161 minor,v4l2_type_names[type],dev->users);
1163 /* allocate + initialize per filehandle data */
1164 fh = kzalloc(sizeof(*fh),GFP_KERNEL);
1170 file->private_data = fh;
1173 fh->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1178 /* Put all controls at a sane state */
1179 for (i = 0; i < ARRAY_SIZE(vivi_qctrl); i++)
1180 qctl_regs[i] =vivi_qctrl[i].default_value;
1182 dprintk(1,"Open: fh=0x%08lx, dev=0x%08lx, dev->vidq=0x%08lx\n",
1183 (unsigned long)fh,(unsigned long)dev,(unsigned long)&dev->vidq);
1184 dprintk(1,"Open: list_empty queued=%d\n",list_empty(&dev->vidq.queued));
1185 dprintk(1,"Open: list_empty active=%d\n",list_empty(&dev->vidq.active));
1187 /* Resets frame counters */
1192 dev->jiffies=jiffies;
1193 sprintf(dev->timestr,"%02d:%02d:%02d:%03d",
1194 dev->h,dev->m,dev->s,(dev->us+500)/1000);
1196 videobuf_queue_init(&fh->vb_vidq, &vivi_video_qops,
1199 V4L2_FIELD_INTERLACED,
1200 sizeof(struct vivi_buffer),fh);
1206 vivi_read(struct file *file, char __user *data, size_t count, loff_t *ppos)
1208 struct vivi_fh *fh = file->private_data;
1210 if (fh->type==V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1211 if (res_locked(fh->dev))
1213 return videobuf_read_one(&fh->vb_vidq, data, count, ppos,
1214 file->f_flags & O_NONBLOCK);
1220 vivi_poll(struct file *file, struct poll_table_struct *wait)
1222 struct vivi_fh *fh = file->private_data;
1223 struct vivi_buffer *buf;
1225 dprintk(1,"%s\n",__FUNCTION__);
1227 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != fh->type)
1230 if (res_get(fh->dev,fh)) {
1231 dprintk(1,"poll: mmap interface\n");
1232 /* streaming capture */
1233 if (list_empty(&fh->vb_vidq.stream))
1235 buf = list_entry(fh->vb_vidq.stream.next,struct vivi_buffer,vb.stream);
1237 dprintk(1,"poll: read() interface\n");
1238 /* read() capture */
1239 buf = (struct vivi_buffer*)fh->vb_vidq.read_buf;
1243 poll_wait(file, &buf->vb.done, wait);
1244 if (buf->vb.state == STATE_DONE ||
1245 buf->vb.state == STATE_ERROR)
1246 return POLLIN|POLLRDNORM;
1250 static int vivi_release(struct inode *inode, struct file *file)
1252 struct vivi_fh *fh = file->private_data;
1253 struct vivi_dev *dev = fh->dev;
1254 struct vivi_dmaqueue *vidq = &dev->vidq;
1256 int minor = iminor(inode);
1258 vivi_stop_thread(vidq);
1259 videobuf_mmap_free(&fh->vb_vidq);
1265 printk(KERN_DEBUG "vivi: close called (minor=%d, users=%d)\n",minor,dev->users);
1271 vivi_mmap(struct file *file, struct vm_area_struct * vma)
1273 struct vivi_fh *fh = file->private_data;
1276 dprintk (1,"mmap called, vma=0x%08lx\n",(unsigned long)vma);
1278 ret=videobuf_mmap_mapper(&fh->vb_vidq, vma);
1280 dprintk (1,"vma start=0x%08lx, size=%ld, ret=%d\n",
1281 (unsigned long)vma->vm_start,
1282 (unsigned long)vma->vm_end-(unsigned long)vma->vm_start,
1288 static struct file_operations vivi_fops = {
1289 .owner = THIS_MODULE,
1291 .release = vivi_release,
1294 .ioctl = video_ioctl2, /* V4L2 ioctl handler */
1296 .llseek = no_llseek,
1299 static struct video_device vivi = {
1301 .type = VID_TYPE_CAPTURE,
1305 // .release = video_device_release,
1307 .vidioc_querycap = vidioc_querycap,
1308 .vidioc_enum_fmt_cap = vidioc_enum_fmt_cap,
1309 .vidioc_g_fmt_cap = vidioc_g_fmt_cap,
1310 .vidioc_try_fmt_cap = vidioc_try_fmt_cap,
1311 .vidioc_s_fmt_cap = vidioc_s_fmt_cap,
1312 .vidioc_reqbufs = vidioc_reqbufs,
1313 .vidioc_querybuf = vidioc_querybuf,
1314 .vidioc_qbuf = vidioc_qbuf,
1315 .vidioc_dqbuf = vidioc_dqbuf,
1316 .vidioc_s_std = vidioc_s_std,
1317 .vidioc_enum_input = vidioc_enum_input,
1318 .vidioc_g_input = vidioc_g_input,
1319 .vidioc_s_input = vidioc_s_input,
1320 .vidioc_queryctrl = vidioc_queryctrl,
1321 .vidioc_g_ctrl = vidioc_g_ctrl,
1322 .vidioc_s_ctrl = vidioc_s_ctrl,
1323 .vidioc_streamon = vidioc_streamon,
1324 .vidioc_streamoff = vidioc_streamoff,
1325 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1326 .vidiocgmbuf = vidiocgmbuf,
1328 .tvnorms = V4L2_STD_NTSC_M,
1329 .current_norm = V4L2_STD_NTSC_M,
1331 /* -----------------------------------------------------------------
1332 Initialization and module stuff
1333 ------------------------------------------------------------------*/
1335 static int __init vivi_init(void)
1338 struct vivi_dev *dev;
1340 dev = kzalloc(sizeof(*dev),GFP_KERNEL);
1343 list_add_tail(&dev->vivi_devlist,&vivi_devlist);
1345 /* init video dma queues */
1346 INIT_LIST_HEAD(&dev->vidq.active);
1347 INIT_LIST_HEAD(&dev->vidq.queued);
1349 /* initialize locks */
1350 init_MUTEX(&dev->lock);
1352 dev->vidq.timeout.function = vivi_vid_timeout;
1353 dev->vidq.timeout.data = (unsigned long)dev;
1354 init_timer(&dev->vidq.timeout);
1356 ret = video_register_device(&vivi, VFL_TYPE_GRABBER, video_nr);
1357 printk(KERN_INFO "Video Technology Magazine Virtual Video Capture Board (Load status: %d)\n", ret);
1361 static void __exit vivi_exit(void)
1364 struct list_head *list;
1366 list_for_each(list,&vivi_devlist) {
1367 h = list_entry(list, struct vivi_dev, vivi_devlist);
1370 video_unregister_device(&vivi);
1373 module_init(vivi_init);
1374 module_exit(vivi_exit);
1376 MODULE_DESCRIPTION("Video Technology Magazine Virtual Video Capture Board");
1377 MODULE_AUTHOR("Mauro Carvalho Chehab, Ted Walther and John Sokol");
1378 MODULE_LICENSE("Dual BSD/GPL");
1380 module_param(video_nr, int, 0);
1382 module_param_named(debug,vivi.debug, int, 0644);
1383 MODULE_PARM_DESC(debug,"activates debug info");
1385 module_param(vid_limit,int,0644);
1386 MODULE_PARM_DESC(vid_limit,"capture memory limit in megabytes");