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>
40 /* Wake up at about 30 fps */
41 #define WAKE_NUMERATOR 30
42 #define WAKE_DENOMINATOR 1001
43 #define BUFFER_TIMEOUT msecs_to_jiffies(500) /* 0.5 seconds */
45 /* These timers are for 1 fps - used only for testing */
46 //#define WAKE_DENOMINATOR 30 /* hack for testing purposes */
47 //#define BUFFER_TIMEOUT msecs_to_jiffies(5000) /* 5 seconds */
51 #define VIVI_MAJOR_VERSION 0
52 #define VIVI_MINOR_VERSION 4
53 #define VIVI_RELEASE 0
54 #define VIVI_VERSION KERNEL_VERSION(VIVI_MAJOR_VERSION, VIVI_MINOR_VERSION, VIVI_RELEASE)
56 /* Declare static vars that will be used as parameters */
57 static unsigned int vid_limit = 16; /* Video memory limit, in Mb */
58 static struct video_device vivi; /* Video device */
59 static int video_nr = -1; /* /dev/videoN, -1 for autodetect */
61 /* supported controls */
62 static struct v4l2_queryctrl vivi_qctrl[] = {
64 .id = V4L2_CID_AUDIO_VOLUME,
69 .default_value = 65535,
71 .type = V4L2_CTRL_TYPE_INTEGER,
73 .id = V4L2_CID_BRIGHTNESS,
74 .type = V4L2_CTRL_TYPE_INTEGER,
82 .id = V4L2_CID_CONTRAST,
83 .type = V4L2_CTRL_TYPE_INTEGER,
88 .default_value = 0x10,
91 .id = V4L2_CID_SATURATION,
92 .type = V4L2_CTRL_TYPE_INTEGER,
101 .type = V4L2_CTRL_TYPE_INTEGER,
111 static int qctl_regs[ARRAY_SIZE(vivi_qctrl)];
113 #define dprintk(level,fmt, arg...) \
115 if (vivi.debug >= (level)) \
116 printk(KERN_DEBUG "vivi: " fmt , ## arg); \
119 /* ------------------------------------------------------------------
121 ------------------------------------------------------------------*/
125 u32 fourcc; /* v4l2 format id */
129 static struct vivi_fmt format = {
130 .name = "4:2:2, packed, YUYV",
131 .fourcc = V4L2_PIX_FMT_YUYV,
137 struct scatterlist *sg;
140 /* buffer for one video frame */
142 /* common v4l buffer stuff -- must be first */
143 struct videobuf_buffer vb;
145 struct vivi_fmt *fmt;
147 struct sg_to_addr *to_addr;
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
232 static void prep_to_addr(struct sg_to_addr to_addr[],
233 struct videobuf_buffer *vb)
237 for (i=0;i<vb->dma.nr_pages;i++) {
238 to_addr[i].sg=&vb->dma.sglist[i];
240 pos += vb->dma.sglist[i].length;
244 static int get_addr_pos(int pos, int pages, struct sg_to_addr to_addr[])
246 int p1=0,p2=pages-1,p3=pages/2;
249 BUG_ON (pos>=to_addr[p2].pos+to_addr[p2].sg->length);
252 if (pos < to_addr[p3].pos) {
259 if (pos >= to_addr[p2].pos)
265 static void gen_line(struct sg_to_addr to_addr[],int inipos,int pages,int wmax,
266 int hmax, int line, char *timestr)
268 int w,i,j,pos=inipos,pgpos,oldpg,y;
273 /* Get first addr pointed to pixel position */
274 oldpg=get_addr_pos(pos,pages,to_addr);
275 pg=pfn_to_page(to_addr[oldpg].sg->dma_address >> PAGE_SHIFT);
276 basep = kmap_atomic(pg, KM_BOUNCE_READ)+to_addr[oldpg].sg->offset;
278 /* We will just duplicate the second pixel at the packet */
281 /* Generate a standard color bar pattern */
282 for (w=0;w<wmax;w++) {
287 for (color=0;color<4;color++) {
288 pgpos=get_addr_pos(pos,pages,to_addr);
290 pg=pfn_to_page(to_addr[pgpos].sg->dma_address >> PAGE_SHIFT);
291 kunmap_atomic(basep, KM_BOUNCE_READ);
292 basep= kmap_atomic(pg, KM_BOUNCE_READ)+to_addr[pgpos].sg->offset;
295 p=basep+pos-to_addr[pgpos].pos;
300 *p=TO_Y(r,g,b); /* Luminance */
303 *p=TO_U(r,g,b); /* Cb */
306 *p=TO_V(r,g,b); /* Cr */
313 /* Checks if it is possible to show timestamp */
314 if (TSTAMP_MAX_Y>=hmax)
316 if (TSTAMP_MIN_X+strlen(timestr)>=wmax)
319 /* Print stream time */
320 if (line>=TSTAMP_MIN_Y && line<=TSTAMP_MAX_Y) {
322 for (s=timestr;*s;s++) {
323 chr=rom8x16_bits[(*s-0x30)*16+line-TSTAMP_MIN_Y];
325 if (chr&1<<(7-i)) { /* Font color*/
331 } else { /* Background color */
339 for (color=0;color<4;color++) {
340 pgpos=get_addr_pos(pos,pages,to_addr);
342 pg=pfn_to_page(to_addr[pgpos].
347 basep= kmap_atomic(pg,
349 to_addr[pgpos].sg->offset;
352 p=basep+pos-to_addr[pgpos].pos;
359 *p=TO_Y(r,g,b); /* Luminance */
362 *p=TO_U(r,g,b); /* Cb */
365 *p=TO_V(r,g,b); /* Cr */
377 kunmap_atomic(basep, KM_BOUNCE_READ);
379 static void vivi_fillbuff(struct vivi_dev *dev,struct vivi_buffer *buf)
382 int hmax = buf->vb.height;
383 int wmax = buf->vb.width;
384 struct videobuf_buffer *vb=&buf->vb;
385 struct sg_to_addr *to_addr=buf->to_addr;
388 /* Test if DMA mapping is ready */
389 if (!vb->dma.sglist[0].dma_address)
392 prep_to_addr(to_addr,vb);
394 /* Check if there is enough memory */
395 BUG_ON(buf->vb.dma.nr_pages << PAGE_SHIFT < (buf->vb.width*buf->vb.height)*2);
397 for (h=0;h<hmax;h++) {
398 gen_line(to_addr,pos,vb->dma.nr_pages,wmax,hmax,h,dev->timestr);
402 /* Updates stream time */
404 dev->us+=jiffies_to_usecs(jiffies-dev->jiffies);
405 dev->jiffies=jiffies;
406 if (dev->us>=1000000) {
420 sprintf(dev->timestr,"%02d:%02d:%02d:%03d",
421 dev->h,dev->m,dev->s,(dev->us+500)/1000);
423 dprintk(2,"vivifill at %s: Buffer 0x%08lx size= %d\n",dev->timestr,
424 (unsigned long)buf->vb.dma.vmalloc,pos);
426 /* Advice that buffer was filled */
427 buf->vb.state = STATE_DONE;
428 buf->vb.field_count++;
429 do_gettimeofday(&ts);
432 list_del(&buf->vb.queue);
433 wake_up(&buf->vb.done);
436 static int restart_video_queue(struct vivi_dmaqueue *dma_q);
438 static void vivi_thread_tick(struct vivi_dmaqueue *dma_q)
440 struct vivi_buffer *buf;
441 struct vivi_dev *dev= container_of(dma_q,struct vivi_dev,vidq);
445 /* Announces videobuf that all went ok */
446 for (bc = 0;; bc++) {
447 if (list_empty(&dma_q->active)) {
448 dprintk(1,"No active queue to serve\n");
452 buf = list_entry(dma_q->active.next,
453 struct vivi_buffer, vb.queue);
455 /* Nobody is waiting something to be done, just return */
456 if (!waitqueue_active(&buf->vb.done)) {
457 mod_timer(&dma_q->timeout, jiffies+BUFFER_TIMEOUT);
461 do_gettimeofday(&buf->vb.ts);
462 dprintk(2,"[%p/%d] wakeup\n",buf,buf->vb.i);
465 vivi_fillbuff(dev,buf);
467 if (list_empty(&dma_q->active)) {
468 del_timer(&dma_q->timeout);
470 mod_timer(&dma_q->timeout, jiffies+BUFFER_TIMEOUT);
473 dprintk(1,"%s: %d buffers handled (should be 1)\n",__FUNCTION__,bc);
476 static void vivi_sleep(struct vivi_dmaqueue *dma_q)
479 DECLARE_WAITQUEUE(wait, current);
481 dprintk(1,"%s dma_q=0x%08lx\n",__FUNCTION__,(unsigned long)dma_q);
483 add_wait_queue(&dma_q->wq, &wait);
484 if (!kthread_should_stop()) {
487 /* Calculate time to wake up */
488 timeout=dma_q->ini_jiffies+msecs_to_jiffies((dma_q->frame*WAKE_NUMERATOR*1000)/WAKE_DENOMINATOR)-jiffies;
491 int old=dma_q->frame;
492 dma_q->frame=(jiffies_to_msecs(jiffies-dma_q->ini_jiffies)*WAKE_DENOMINATOR)/(WAKE_NUMERATOR*1000)+1;
494 timeout=dma_q->ini_jiffies+msecs_to_jiffies((dma_q->frame*WAKE_NUMERATOR*1000)/WAKE_DENOMINATOR)-jiffies;
496 dprintk(1,"underrun, losed %d frames. "
497 "Now, frame is %d. Waking on %d jiffies\n",
498 dma_q->frame-old,dma_q->frame,timeout);
500 dprintk(1,"will sleep for %i jiffies\n",timeout);
502 vivi_thread_tick(dma_q);
504 schedule_timeout_interruptible (timeout);
507 remove_wait_queue(&dma_q->wq, &wait);
511 static int vivi_thread(void *data)
513 struct vivi_dmaqueue *dma_q=data;
515 dprintk(1,"thread started\n");
520 if (kthread_should_stop())
523 dprintk(1, "thread: exit\n");
527 static int vivi_start_thread(struct vivi_dmaqueue *dma_q)
530 dma_q->ini_jiffies=jiffies;
532 dprintk(1,"%s\n",__FUNCTION__);
533 init_waitqueue_head(&dma_q->wq);
535 dma_q->kthread = kthread_run(vivi_thread, dma_q, "vivi");
537 if (dma_q->kthread == NULL) {
538 printk(KERN_ERR "vivi: kernel_thread() failed\n");
541 dprintk(1,"returning from %s\n",__FUNCTION__);
545 static void vivi_stop_thread(struct vivi_dmaqueue *dma_q)
547 dprintk(1,"%s\n",__FUNCTION__);
548 /* shutdown control thread */
549 if (dma_q->kthread) {
550 kthread_stop(dma_q->kthread);
555 static int restart_video_queue(struct vivi_dmaqueue *dma_q)
557 struct vivi_buffer *buf, *prev;
558 struct list_head *item;
560 dprintk(1,"%s dma_q=0x%08lx\n",__FUNCTION__,(unsigned long)dma_q);
562 if (!list_empty(&dma_q->active)) {
563 buf = list_entry(dma_q->active.next, struct vivi_buffer, vb.queue);
564 dprintk(2,"restart_queue [%p/%d]: restart dma\n",
567 dprintk(1,"Restarting video dma\n");
568 vivi_stop_thread(dma_q);
569 // vivi_start_thread(dma_q);
571 /* cancel all outstanding capture / vbi requests */
572 list_for_each(item,&dma_q->active) {
573 buf = list_entry(item, struct vivi_buffer, vb.queue);
575 list_del(&buf->vb.queue);
576 buf->vb.state = STATE_ERROR;
577 wake_up(&buf->vb.done);
579 mod_timer(&dma_q->timeout, jiffies+BUFFER_TIMEOUT);
586 if (list_empty(&dma_q->queued))
588 buf = list_entry(dma_q->queued.next, struct vivi_buffer, vb.queue);
590 list_del(&buf->vb.queue);
591 list_add_tail(&buf->vb.queue,&dma_q->active);
593 dprintk(1,"Restarting video dma\n");
594 vivi_stop_thread(dma_q);
595 vivi_start_thread(dma_q);
597 buf->vb.state = STATE_ACTIVE;
598 mod_timer(&dma_q->timeout, jiffies+BUFFER_TIMEOUT);
599 dprintk(2,"[%p/%d] restart_queue - first active\n",
602 } else if (prev->vb.width == buf->vb.width &&
603 prev->vb.height == buf->vb.height &&
604 prev->fmt == buf->fmt) {
605 list_del(&buf->vb.queue);
606 list_add_tail(&buf->vb.queue,&dma_q->active);
607 buf->vb.state = STATE_ACTIVE;
608 dprintk(2,"[%p/%d] restart_queue - move to active\n",
617 static void vivi_vid_timeout(unsigned long data)
619 struct vivi_dev *dev = (struct vivi_dev*)data;
620 struct vivi_dmaqueue *vidq = &dev->vidq;
621 struct vivi_buffer *buf;
623 while (!list_empty(&vidq->active)) {
624 buf = list_entry(vidq->active.next, struct vivi_buffer, vb.queue);
625 list_del(&buf->vb.queue);
626 buf->vb.state = STATE_ERROR;
627 wake_up(&buf->vb.done);
628 printk("vivi/0: [%p/%d] timeout\n", buf, buf->vb.i);
631 restart_video_queue(vidq);
634 /* ------------------------------------------------------------------
636 ------------------------------------------------------------------*/
638 buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size)
640 struct vivi_fh *fh = vq->priv_data;
642 *size = fh->width*fh->height*2;
646 while (*size * *count > vid_limit * 1024 * 1024)
651 static void free_buffer(struct videobuf_queue *vq, struct vivi_buffer *buf)
653 dprintk(1,"%s\n",__FUNCTION__);
658 /*FIXME: Maybe a spinlock is required here */
662 videobuf_waiton(&buf->vb,0,0);
663 videobuf_dma_unmap(vq, &buf->vb.dma);
664 videobuf_dma_free(&buf->vb.dma);
665 buf->vb.state = STATE_NEEDS_INIT;
668 #define norm_maxw() 1024
669 #define norm_maxh() 768
671 buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
672 enum v4l2_field field)
674 struct vivi_fh *fh = vq->priv_data;
675 struct vivi_buffer *buf = container_of(vb,struct vivi_buffer,vb);
676 int rc, init_buffer = 0;
678 // dprintk(1,"%s, field=%d\n",__FUNCTION__,field);
680 BUG_ON(NULL == fh->fmt);
681 if (fh->width < 48 || fh->width > norm_maxw() ||
682 fh->height < 32 || fh->height > norm_maxh())
684 buf->vb.size = fh->width*fh->height*2;
685 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
688 if (buf->fmt != fh->fmt ||
689 buf->vb.width != fh->width ||
690 buf->vb.height != fh->height ||
691 buf->vb.field != field) {
693 buf->vb.width = fh->width;
694 buf->vb.height = fh->height;
695 buf->vb.field = field;
699 if (STATE_NEEDS_INIT == buf->vb.state) {
700 if (0 != (rc = videobuf_iolock(vq,&buf->vb,NULL)))
704 buf->vb.state = STATE_PREPARED;
706 if (NULL == (buf->to_addr = kmalloc(sizeof(*buf->to_addr) * vb->dma.nr_pages,GFP_KERNEL))) {
719 buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
721 struct vivi_buffer *buf = container_of(vb,struct vivi_buffer,vb);
722 struct vivi_fh *fh = vq->priv_data;
723 struct vivi_dev *dev = fh->dev;
724 struct vivi_dmaqueue *vidq = &dev->vidq;
725 struct vivi_buffer *prev;
727 if (!list_empty(&vidq->queued)) {
728 dprintk(1,"adding vb queue=0x%08lx\n",(unsigned long)&buf->vb.queue);
729 list_add_tail(&buf->vb.queue,&vidq->queued);
730 buf->vb.state = STATE_QUEUED;
731 dprintk(2,"[%p/%d] buffer_queue - append to queued\n",
733 } else if (list_empty(&vidq->active)) {
734 list_add_tail(&buf->vb.queue,&vidq->active);
736 buf->vb.state = STATE_ACTIVE;
737 mod_timer(&vidq->timeout, jiffies+BUFFER_TIMEOUT);
738 dprintk(2,"[%p/%d] buffer_queue - first active\n",
741 vivi_start_thread(vidq);
743 prev = list_entry(vidq->active.prev, struct vivi_buffer, vb.queue);
744 if (prev->vb.width == buf->vb.width &&
745 prev->vb.height == buf->vb.height &&
746 prev->fmt == buf->fmt) {
747 list_add_tail(&buf->vb.queue,&vidq->active);
748 buf->vb.state = STATE_ACTIVE;
749 dprintk(2,"[%p/%d] buffer_queue - append to active\n",
753 list_add_tail(&buf->vb.queue,&vidq->queued);
754 buf->vb.state = STATE_QUEUED;
755 dprintk(2,"[%p/%d] buffer_queue - first queued\n",
761 static void buffer_release(struct videobuf_queue *vq, struct videobuf_buffer *vb)
763 struct vivi_buffer *buf = container_of(vb,struct vivi_buffer,vb);
764 struct vivi_fh *fh = vq->priv_data;
765 struct vivi_dev *dev = (struct vivi_dev*)fh->dev;
766 struct vivi_dmaqueue *vidq = &dev->vidq;
768 dprintk(1,"%s\n",__FUNCTION__);
770 vivi_stop_thread(vidq);
775 static int vivi_map_sg(void *dev, struct scatterlist *sg, int nents,
780 dprintk(1,"%s, number of pages=%d\n",__FUNCTION__,nents);
781 BUG_ON(direction == DMA_NONE);
783 for (i = 0; i < nents; i++ ) {
786 sg[i].dma_address = page_to_phys(sg[i].page) + sg[i].offset;
792 static int vivi_unmap_sg(void *dev,struct scatterlist *sglist,int nr_pages,
795 dprintk(1,"%s\n",__FUNCTION__);
799 static int vivi_dma_sync_sg(void *dev,struct scatterlist *sglist, int nr_pages,
802 // dprintk(1,"%s\n",__FUNCTION__);
804 // flush_write_buffers();
808 static struct videobuf_queue_ops vivi_video_qops = {
809 .buf_setup = buffer_setup,
810 .buf_prepare = buffer_prepare,
811 .buf_queue = buffer_queue,
812 .buf_release = buffer_release,
814 /* Non-pci handling routines */
815 .vb_map_sg = vivi_map_sg,
816 .vb_dma_sync_sg = vivi_dma_sync_sg,
817 .vb_unmap_sg = vivi_unmap_sg,
820 /* ------------------------------------------------------------------
822 ------------------------------------------------------------------*/
825 static int res_get(struct vivi_dev *dev, struct vivi_fh *fh)
829 if (dev->resources) {
830 /* no, someone else uses it */
834 /* it's free, grab it */
836 dprintk(1,"res: get\n");
841 static int res_locked(struct vivi_dev *dev)
843 return (dev->resources);
846 static void res_free(struct vivi_dev *dev, struct vivi_fh *fh)
850 dprintk(1,"res: put\n");
854 /* ------------------------------------------------------------------
855 IOCTL vidioc handling
856 ------------------------------------------------------------------*/
857 static int vidioc_querycap (struct file *file, void *priv,
858 struct v4l2_capability *cap)
860 strcpy(cap->driver, "vivi");
861 strcpy(cap->card, "vivi");
862 cap->version = VIVI_VERSION;
863 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE |
869 static int vidioc_enum_fmt_cap (struct file *file, void *priv,
870 struct v4l2_fmtdesc *f)
875 strlcpy(f->description,format.name,sizeof(f->description));
876 f->pixelformat = format.fourcc;
880 static int vidioc_g_fmt_cap (struct file *file, void *priv,
881 struct v4l2_format *f)
883 struct vivi_fh *fh=priv;
885 f->fmt.pix.width = fh->width;
886 f->fmt.pix.height = fh->height;
887 f->fmt.pix.field = fh->vb_vidq.field;
888 f->fmt.pix.pixelformat = fh->fmt->fourcc;
889 f->fmt.pix.bytesperline =
890 (f->fmt.pix.width * fh->fmt->depth) >> 3;
891 f->fmt.pix.sizeimage =
892 f->fmt.pix.height * f->fmt.pix.bytesperline;
897 static int vidioc_try_fmt_cap (struct file *file, void *priv,
898 struct v4l2_format *f)
900 struct vivi_fmt *fmt;
901 enum v4l2_field field;
902 unsigned int maxw, maxh;
904 if (format.fourcc != f->fmt.pix.pixelformat) {
905 dprintk(1,"Fourcc format (0x%08x) invalid. Driver accepts "
906 "only 0x%08x\n",f->fmt.pix.pixelformat,format.fourcc);
911 field = f->fmt.pix.field;
913 if (field == V4L2_FIELD_ANY) {
914 // field=V4L2_FIELD_INTERLACED;
915 field=V4L2_FIELD_SEQ_TB;
916 } else if (V4L2_FIELD_INTERLACED != field) {
917 dprintk(1,"Field type invalid.\n");
924 f->fmt.pix.field = field;
925 if (f->fmt.pix.height < 32)
926 f->fmt.pix.height = 32;
927 if (f->fmt.pix.height > maxh)
928 f->fmt.pix.height = maxh;
929 if (f->fmt.pix.width < 48)
930 f->fmt.pix.width = 48;
931 if (f->fmt.pix.width > maxw)
932 f->fmt.pix.width = maxw;
933 f->fmt.pix.width &= ~0x03;
934 f->fmt.pix.bytesperline =
935 (f->fmt.pix.width * fmt->depth) >> 3;
936 f->fmt.pix.sizeimage =
937 f->fmt.pix.height * f->fmt.pix.bytesperline;
942 /*FIXME: This seems to be generic enough to be at videodev2 */
943 static int vidioc_s_fmt_cap (struct file *file, void *priv,
944 struct v4l2_format *f)
946 struct vivi_fh *fh=priv;
947 int ret = vidioc_try_fmt_cap(file,fh,f);
952 fh->width = f->fmt.pix.width;
953 fh->height = f->fmt.pix.height;
954 fh->vb_vidq.field = f->fmt.pix.field;
960 static int vidioc_reqbufs (struct file *file, void *priv, struct v4l2_requestbuffers *p)
962 struct vivi_fh *fh=priv;
964 return (videobuf_reqbufs(&fh->vb_vidq, p));
967 static int vidioc_querybuf (struct file *file, void *priv, struct v4l2_buffer *p)
969 struct vivi_fh *fh=priv;
971 return (videobuf_querybuf(&fh->vb_vidq, p));
974 static int vidioc_qbuf (struct file *file, void *priv, struct v4l2_buffer *p)
976 struct vivi_fh *fh=priv;
978 return (videobuf_qbuf(&fh->vb_vidq, p));
981 static int vidioc_dqbuf (struct file *file, void *priv, struct v4l2_buffer *p)
983 struct vivi_fh *fh=priv;
985 return (videobuf_dqbuf(&fh->vb_vidq, p,
986 file->f_flags & O_NONBLOCK));
989 #ifdef CONFIG_VIDEO_V4L1_COMPAT
990 static int vidiocgmbuf (struct file *file, void *priv, struct video_mbuf *mbuf)
992 struct vivi_fh *fh=priv;
993 struct videobuf_queue *q=&fh->vb_vidq;
994 struct v4l2_requestbuffers req;
1000 req.memory = V4L2_MEMORY_MMAP;
1001 ret = videobuf_reqbufs(q,&req);
1005 mbuf->frames = req.count;
1007 for (i = 0; i < mbuf->frames; i++) {
1008 mbuf->offsets[i] = q->bufs[i]->boff;
1009 mbuf->size += q->bufs[i]->bsize;
1015 static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
1017 struct vivi_fh *fh=priv;
1018 struct vivi_dev *dev = fh->dev;
1020 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1025 if (!res_get(dev,fh))
1027 return (videobuf_streamon(&fh->vb_vidq));
1030 static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
1032 struct vivi_fh *fh=priv;
1033 struct vivi_dev *dev = fh->dev;
1035 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1040 videobuf_streamoff(&fh->vb_vidq);
1046 static struct v4l2_tvnorm tvnorms[] = {
1049 .id = V4L2_STD_NTSC_M,
1053 static int vidioc_s_std (struct file *file, void *priv, v4l2_std_id a)
1059 /* only one input in this sample driver */
1060 static int vidioc_enum_input (struct file *file, void *priv,
1061 struct v4l2_input *inp)
1063 if (inp->index != 0)
1066 inp->type = V4L2_INPUT_TYPE_CAMERA;
1067 inp->std = V4L2_STD_NTSC_M;
1068 strcpy(inp->name,"Camera");
1073 static int vidioc_g_input (struct file *file, void *priv, unsigned int *i)
1079 static int vidioc_s_input (struct file *file, void *priv, unsigned int i)
1087 /* --- controls ---------------------------------------------- */
1088 static int vidioc_queryctrl (struct file *file, void *priv,
1089 struct v4l2_queryctrl *qc)
1093 for (i = 0; i < ARRAY_SIZE(vivi_qctrl); i++)
1094 if (qc->id && qc->id == vivi_qctrl[i].id) {
1095 memcpy(qc, &(vivi_qctrl[i]),
1103 static int vidioc_g_ctrl (struct file *file, void *priv,
1104 struct v4l2_control *ctrl)
1108 for (i = 0; i < ARRAY_SIZE(vivi_qctrl); i++)
1109 if (ctrl->id == vivi_qctrl[i].id) {
1110 ctrl->value=qctl_regs[i];
1116 static int vidioc_s_ctrl (struct file *file, void *priv,
1117 struct v4l2_control *ctrl)
1121 for (i = 0; i < ARRAY_SIZE(vivi_qctrl); i++)
1122 if (ctrl->id == vivi_qctrl[i].id) {
1124 vivi_qctrl[i].minimum
1126 vivi_qctrl[i].maximum) {
1129 qctl_regs[i]=ctrl->value;
1135 /* ------------------------------------------------------------------
1136 File operations for the device
1137 ------------------------------------------------------------------*/
1139 #define line_buf_size(norm) (norm_maxw(norm)*(format.depth+7)/8)
1141 static int vivi_open(struct inode *inode, struct file *file)
1143 int minor = iminor(inode);
1144 struct vivi_dev *h,*dev = NULL;
1146 struct list_head *list;
1147 enum v4l2_buf_type type = 0;
1150 printk(KERN_DEBUG "vivi: open called (minor=%d)\n",minor);
1152 list_for_each(list,&vivi_devlist) {
1153 h = list_entry(list, struct vivi_dev, vivi_devlist);
1154 if (h->vfd.minor == minor) {
1156 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1164 /* If more than one user, mutex should be added */
1167 dprintk(1,"open minor=%d type=%s users=%d\n",
1168 minor,v4l2_type_names[type],dev->users);
1170 /* allocate + initialize per filehandle data */
1171 fh = kzalloc(sizeof(*fh),GFP_KERNEL);
1177 file->private_data = fh;
1180 fh->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1185 /* Put all controls at a sane state */
1186 for (i = 0; i < ARRAY_SIZE(vivi_qctrl); i++)
1187 qctl_regs[i] =vivi_qctrl[i].default_value;
1189 dprintk(1,"Open: fh=0x%08lx, dev=0x%08lx, dev->vidq=0x%08lx\n",
1190 (unsigned long)fh,(unsigned long)dev,(unsigned long)&dev->vidq);
1191 dprintk(1,"Open: list_empty queued=%d\n",list_empty(&dev->vidq.queued));
1192 dprintk(1,"Open: list_empty active=%d\n",list_empty(&dev->vidq.active));
1194 /* Resets frame counters */
1199 dev->jiffies=jiffies;
1200 sprintf(dev->timestr,"%02d:%02d:%02d:%03d",
1201 dev->h,dev->m,dev->s,(dev->us+500)/1000);
1203 videobuf_queue_init(&fh->vb_vidq, &vivi_video_qops,
1206 V4L2_FIELD_INTERLACED,
1207 sizeof(struct vivi_buffer),fh);
1213 vivi_read(struct file *file, char __user *data, size_t count, loff_t *ppos)
1215 struct vivi_fh *fh = file->private_data;
1217 if (fh->type==V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1218 if (res_locked(fh->dev))
1220 return videobuf_read_one(&fh->vb_vidq, data, count, ppos,
1221 file->f_flags & O_NONBLOCK);
1227 vivi_poll(struct file *file, struct poll_table_struct *wait)
1229 struct vivi_fh *fh = file->private_data;
1230 struct vivi_buffer *buf;
1232 dprintk(1,"%s\n",__FUNCTION__);
1234 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != fh->type)
1237 if (res_get(fh->dev,fh)) {
1238 dprintk(1,"poll: mmap interface\n");
1239 /* streaming capture */
1240 if (list_empty(&fh->vb_vidq.stream))
1242 buf = list_entry(fh->vb_vidq.stream.next,struct vivi_buffer,vb.stream);
1244 dprintk(1,"poll: read() interface\n");
1245 /* read() capture */
1246 buf = (struct vivi_buffer*)fh->vb_vidq.read_buf;
1250 poll_wait(file, &buf->vb.done, wait);
1251 if (buf->vb.state == STATE_DONE ||
1252 buf->vb.state == STATE_ERROR)
1253 return POLLIN|POLLRDNORM;
1257 static int vivi_release(struct inode *inode, struct file *file)
1259 struct vivi_fh *fh = file->private_data;
1260 struct vivi_dev *dev = fh->dev;
1261 struct vivi_dmaqueue *vidq = &dev->vidq;
1263 int minor = iminor(inode);
1265 vivi_stop_thread(vidq);
1266 videobuf_mmap_free(&fh->vb_vidq);
1272 printk(KERN_DEBUG "vivi: close called (minor=%d, users=%d)\n",minor,dev->users);
1278 vivi_mmap(struct file *file, struct vm_area_struct * vma)
1280 struct vivi_fh *fh = file->private_data;
1283 dprintk (1,"mmap called, vma=0x%08lx\n",(unsigned long)vma);
1285 ret=videobuf_mmap_mapper(&fh->vb_vidq, vma);
1287 dprintk (1,"vma start=0x%08lx, size=%ld, ret=%d\n",
1288 (unsigned long)vma->vm_start,
1289 (unsigned long)vma->vm_end-(unsigned long)vma->vm_start,
1295 static struct file_operations vivi_fops = {
1296 .owner = THIS_MODULE,
1298 .release = vivi_release,
1301 .ioctl = video_ioctl2, /* V4L2 ioctl handler */
1303 .llseek = no_llseek,
1306 static struct video_device vivi = {
1308 .type = VID_TYPE_CAPTURE,
1312 // .release = video_device_release,
1314 .vidioc_querycap = vidioc_querycap,
1315 .vidioc_enum_fmt_cap = vidioc_enum_fmt_cap,
1316 .vidioc_g_fmt_cap = vidioc_g_fmt_cap,
1317 .vidioc_try_fmt_cap = vidioc_try_fmt_cap,
1318 .vidioc_s_fmt_cap = vidioc_s_fmt_cap,
1319 .vidioc_reqbufs = vidioc_reqbufs,
1320 .vidioc_querybuf = vidioc_querybuf,
1321 .vidioc_qbuf = vidioc_qbuf,
1322 .vidioc_dqbuf = vidioc_dqbuf,
1323 .vidioc_s_std = vidioc_s_std,
1324 .vidioc_enum_input = vidioc_enum_input,
1325 .vidioc_g_input = vidioc_g_input,
1326 .vidioc_s_input = vidioc_s_input,
1327 .vidioc_queryctrl = vidioc_queryctrl,
1328 .vidioc_g_ctrl = vidioc_g_ctrl,
1329 .vidioc_s_ctrl = vidioc_s_ctrl,
1330 .vidioc_streamon = vidioc_streamon,
1331 .vidioc_streamoff = vidioc_streamoff,
1332 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1333 .vidiocgmbuf = vidiocgmbuf,
1336 .tvnormsize = ARRAY_SIZE(tvnorms),
1338 /* -----------------------------------------------------------------
1339 Initialization and module stuff
1340 ------------------------------------------------------------------*/
1342 static int __init vivi_init(void)
1345 struct vivi_dev *dev;
1347 dev = kzalloc(sizeof(*dev),GFP_KERNEL);
1350 list_add_tail(&dev->vivi_devlist,&vivi_devlist);
1352 /* init video dma queues */
1353 INIT_LIST_HEAD(&dev->vidq.active);
1354 INIT_LIST_HEAD(&dev->vidq.queued);
1356 /* initialize locks */
1357 init_MUTEX(&dev->lock);
1359 dev->vidq.timeout.function = vivi_vid_timeout;
1360 dev->vidq.timeout.data = (unsigned long)dev;
1361 init_timer(&dev->vidq.timeout);
1363 ret = video_register_device(&vivi, VFL_TYPE_GRABBER, video_nr);
1364 printk(KERN_INFO "Video Technology Magazine Virtual Video Capture Board (Load status: %d)\n", ret);
1368 static void __exit vivi_exit(void)
1371 struct list_head *list;
1373 list_for_each(list,&vivi_devlist) {
1374 h = list_entry(list, struct vivi_dev, vivi_devlist);
1377 video_unregister_device(&vivi);
1380 module_init(vivi_init);
1381 module_exit(vivi_exit);
1383 MODULE_DESCRIPTION("Video Technology Magazine Virtual Video Capture Board");
1384 MODULE_AUTHOR("Mauro Carvalho Chehab, Ted Walther and John Sokol");
1385 MODULE_LICENSE("Dual BSD/GPL");
1387 module_param(video_nr, int, 0);
1389 module_param_named(debug,vivi.debug, int, 0644);
1390 MODULE_PARM_DESC(debug,"activates debug info");
1392 module_param(vid_limit,int,0644);
1393 MODULE_PARM_DESC(vid_limit,"capture memory limit in megabytes");