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 */
52 #define kzalloc(size, flags) \
54 void *__ret = kmalloc(size, flags); \
56 memset(__ret, 0, size); \
61 MODULE_DESCRIPTION("Video Technology Magazine Virtual Video Capture Board");
62 MODULE_AUTHOR("Mauro Carvalho Chehab, Ted Walther and John Sokol");
63 MODULE_LICENSE("Dual BSD/GPL");
65 #define VIVI_MAJOR_VERSION 0
66 #define VIVI_MINOR_VERSION 4
67 #define VIVI_RELEASE 0
68 #define VIVI_VERSION KERNEL_VERSION(VIVI_MAJOR_VERSION, VIVI_MINOR_VERSION, VIVI_RELEASE)
70 static int video_nr = -1; /* /dev/videoN, -1 for autodetect */
71 module_param(video_nr, int, 0);
74 module_param(debug, int, 0);
76 static unsigned int vid_limit = 16;
77 module_param(vid_limit,int,0644);
78 MODULE_PARM_DESC(vid_limit,"capture memory limit in megabytes");
80 /* supported controls */
81 static struct v4l2_queryctrl vivi_qctrl[] = {
83 .id = V4L2_CID_AUDIO_VOLUME,
88 .default_value = 65535,
90 .type = V4L2_CTRL_TYPE_INTEGER,
92 .id = V4L2_CID_BRIGHTNESS,
93 .type = V4L2_CTRL_TYPE_INTEGER,
101 .id = V4L2_CID_CONTRAST,
102 .type = V4L2_CTRL_TYPE_INTEGER,
107 .default_value = 0x10,
110 .id = V4L2_CID_SATURATION,
111 .type = V4L2_CTRL_TYPE_INTEGER,
112 .name = "Saturation",
116 .default_value = 127,
120 .type = V4L2_CTRL_TYPE_INTEGER,
130 static int qctl_regs[ARRAY_SIZE(vivi_qctrl)];
132 #define dprintk(level,fmt, arg...) \
134 if (debug >= (level)) \
135 printk(KERN_DEBUG "vivi: " fmt , ## arg); \
138 /* ------------------------------------------------------------------
140 ------------------------------------------------------------------*/
144 u32 fourcc; /* v4l2 format id */
148 static struct vivi_fmt format = {
149 .name = "4:2:2, packed, YUYV",
150 .fourcc = V4L2_PIX_FMT_YUYV,
156 struct scatterlist *sg;
159 /* buffer for one video frame */
161 /* common v4l buffer stuff -- must be first */
162 struct videobuf_buffer vb;
164 struct vivi_fmt *fmt;
166 struct sg_to_addr *to_addr;
169 struct vivi_dmaqueue {
170 struct list_head active;
171 struct list_head queued;
172 struct timer_list timeout;
174 /* thread for generating video stream*/
175 struct task_struct *kthread;
176 wait_queue_head_t wq;
177 /* Counters to control fps rate */
182 static LIST_HEAD(vivi_devlist);
185 struct list_head vivi_devlist;
187 struct semaphore lock;
191 /* various device info */
192 unsigned int resources;
193 struct video_device video_dev;
195 struct vivi_dmaqueue vidq;
197 /* Several counters */
198 int h,m,s,us,jiffies;
203 struct vivi_dev *dev;
206 struct vivi_fmt *fmt;
207 unsigned int width,height;
208 struct videobuf_queue vb_vidq;
210 enum v4l2_buf_type type;
213 /* ------------------------------------------------------------------
214 DMA and thread functions
215 ------------------------------------------------------------------*/
217 /* Bars and Colors should match positions */
229 static u8 bars[8][3] = {
231 {204,204,204}, /* white */
232 {208,208, 0}, /* ambar */
233 { 0,206,206}, /* cyan */
234 { 0,239, 0}, /* green */
235 {239, 0,239}, /* magenta */
236 {205, 0, 0}, /* red */
237 { 0, 0,255}, /* blue */
241 #define TO_Y(r,g,b) (((16829*r +33039*g +6416*b + 32768)>>16)+16)
242 /* RGB to V(Cr) Color transform */
243 #define TO_V(r,g,b) (((28784*r -24103*g -4681*b + 32768)>>16)+128)
244 /* RGB to U(Cb) Color transform */
245 #define TO_U(r,g,b) (((-9714*r -19070*g +28784*b + 32768)>>16)+128)
247 #define TSTAMP_MIN_Y 24
248 #define TSTAMP_MAX_Y TSTAMP_MIN_Y+15
249 #define TSTAMP_MIN_X 64
251 void prep_to_addr(struct sg_to_addr to_addr[],struct videobuf_buffer *vb)
255 for (i=0;i<vb->dma.nr_pages;i++) {
256 to_addr[i].sg=&vb->dma.sglist[i];
258 pos += vb->dma.sglist[i].length;
262 inline int get_addr_pos(int pos, int pages, struct sg_to_addr to_addr[])
264 int p1=0,p2=pages-1,p3=pages/2;
267 BUG_ON (pos>=to_addr[p2].pos+to_addr[p2].sg->length);
270 if (pos < to_addr[p3].pos) {
277 if (pos >= to_addr[p2].pos)
283 void gen_line(struct sg_to_addr to_addr[],int inipos,int pages,int wmax,
284 int hmax, int line, char *timestr)
286 int w,i,j,pos=inipos,pgpos,oldpg,y;
291 /* Get first addr pointed to pixel position */
292 oldpg=get_addr_pos(pos,pages,to_addr);
293 pg=pfn_to_page(to_addr[oldpg].sg->dma_address >> PAGE_SHIFT);
294 basep = kmap_atomic(pg, KM_BOUNCE_READ)+to_addr[oldpg].sg->offset;
296 /* We will just duplicate the second pixel at the packet */
299 /* Generate a standard color bar pattern */
300 for (w=0;w<wmax;w++) {
305 for (color=0;color<4;color++) {
306 pgpos=get_addr_pos(pos,pages,to_addr);
308 pg=pfn_to_page(to_addr[pgpos].sg->dma_address >> PAGE_SHIFT);
309 kunmap_atomic(basep, KM_BOUNCE_READ);
310 basep= kmap_atomic(pg, KM_BOUNCE_READ)+to_addr[pgpos].sg->offset;
313 p=basep+pos-to_addr[pgpos].pos;
318 *p=TO_Y(r,g,b); /* Luminance */
321 *p=TO_U(r,g,b); /* Cb */
324 *p=TO_V(r,g,b); /* Cr */
331 /* Checks if it is possible to show timestamp */
332 if (TSTAMP_MAX_Y>=hmax)
334 if (TSTAMP_MIN_X+strlen(timestr)>=wmax)
337 /* Print stream time */
338 if (line>=TSTAMP_MIN_Y && line<=TSTAMP_MAX_Y) {
340 for (s=timestr;*s;s++) {
341 chr=rom8x16_bits[(*s-0x30)*16+line-TSTAMP_MIN_Y];
343 if (chr&1<<(7-i)) { /* Font color*/
349 } else { /* Background color */
357 for (color=0;color<4;color++) {
358 pgpos=get_addr_pos(pos,pages,to_addr);
360 pg=pfn_to_page(to_addr[pgpos].
365 basep= kmap_atomic(pg,
367 to_addr[pgpos].sg->offset;
370 p=basep+pos-to_addr[pgpos].pos;
377 *p=TO_Y(r,g,b); /* Luminance */
380 *p=TO_U(r,g,b); /* Cb */
383 *p=TO_V(r,g,b); /* Cr */
395 kunmap_atomic(basep, KM_BOUNCE_READ);
397 static void vivi_fillbuff(struct vivi_dev *dev,struct vivi_buffer *buf)
400 int hmax = buf->vb.height;
401 int wmax = buf->vb.width;
402 struct videobuf_buffer *vb=&buf->vb;
403 struct sg_to_addr *to_addr=buf->to_addr;
406 /* Test if DMA mapping is ready */
407 if (!vb->dma.sglist[0].dma_address)
410 prep_to_addr(to_addr,vb);
412 /* Check if there is enough memory */
413 BUG_ON(buf->vb.dma.nr_pages << PAGE_SHIFT < (buf->vb.width*buf->vb.height)*2);
415 for (h=0;h<hmax;h++) {
416 gen_line(to_addr,pos,vb->dma.nr_pages,wmax,hmax,h,dev->timestr);
420 /* Updates stream time */
422 dev->us+=jiffies_to_usecs(jiffies-dev->jiffies);
423 dev->jiffies=jiffies;
424 if (dev->us>=1000000) {
438 sprintf(dev->timestr,"%02d:%02d:%02d:%03d",
439 dev->h,dev->m,dev->s,(dev->us+500)/1000);
441 dprintk(2,"vivifill at %s: Buffer 0x%08lx size= %d\n",dev->timestr,
442 (unsigned long)buf->vb.dma.vmalloc,pos);
444 /* Advice that buffer was filled */
445 buf->vb.state = STATE_DONE;
446 buf->vb.field_count++;
447 do_gettimeofday(&ts);
450 list_del(&buf->vb.queue);
451 wake_up(&buf->vb.done);
454 static int restart_video_queue(struct vivi_dmaqueue *dma_q);
456 static void vivi_thread_tick(struct vivi_dmaqueue *dma_q)
458 struct vivi_buffer *buf;
459 struct vivi_dev *dev= container_of(dma_q,struct vivi_dev,vidq);
463 /* Announces videobuf that all went ok */
464 for (bc = 0;; bc++) {
465 if (list_empty(&dma_q->active)) {
466 dprintk(1,"No active queue to serve\n");
470 buf = list_entry(dma_q->active.next,
471 struct vivi_buffer, vb.queue);
473 /* Nobody is waiting something to be done, just return */
474 if (!waitqueue_active(&buf->vb.done)) {
475 mod_timer(&dma_q->timeout, jiffies+BUFFER_TIMEOUT);
479 do_gettimeofday(&buf->vb.ts);
480 dprintk(2,"[%p/%d] wakeup\n",buf,buf->vb.i);
483 vivi_fillbuff(dev,buf);
485 if (list_empty(&dma_q->active)) {
486 del_timer(&dma_q->timeout);
488 mod_timer(&dma_q->timeout, jiffies+BUFFER_TIMEOUT);
491 dprintk(1,"%s: %d buffers handled (should be 1)\n",__FUNCTION__,bc);
494 void vivi_sleep(struct vivi_dmaqueue *dma_q)
497 DECLARE_WAITQUEUE(wait, current);
499 dprintk(1,"%s dma_q=0x%08lx\n",__FUNCTION__,(unsigned long)dma_q);
501 add_wait_queue(&dma_q->wq, &wait);
502 if (!kthread_should_stop()) {
505 /* Calculate time to wake up */
506 timeout=dma_q->ini_jiffies+msecs_to_jiffies((dma_q->frame*WAKE_NUMERATOR*1000)/WAKE_DENOMINATOR)-jiffies;
509 int old=dma_q->frame;
510 dma_q->frame=(jiffies_to_msecs(jiffies-dma_q->ini_jiffies)*WAKE_DENOMINATOR)/(WAKE_NUMERATOR*1000)+1;
512 timeout=dma_q->ini_jiffies+msecs_to_jiffies((dma_q->frame*WAKE_NUMERATOR*1000)/WAKE_DENOMINATOR)-jiffies;
514 dprintk(1,"underrun, losed %d frames. "
515 "Now, frame is %d. Waking on %d jiffies\n",
516 dma_q->frame-old,dma_q->frame,timeout);
518 dprintk(1,"will sleep for %i jiffies\n",timeout);
520 vivi_thread_tick(dma_q);
522 schedule_timeout_interruptible (timeout);
525 remove_wait_queue(&dma_q->wq, &wait);
529 int vivi_thread(void *data)
531 struct vivi_dmaqueue *dma_q=data;
533 dprintk(1,"thread started\n");
538 if (kthread_should_stop())
541 dprintk(1, "thread: exit\n");
545 int vivi_start_thread(struct vivi_dmaqueue *dma_q)
548 dma_q->ini_jiffies=jiffies;
550 dprintk(1,"%s\n",__FUNCTION__);
551 init_waitqueue_head(&dma_q->wq);
553 dma_q->kthread = kthread_run(vivi_thread, dma_q, "vivi");
555 if (dma_q->kthread == NULL) {
556 printk(KERN_ERR "vivi: kernel_thread() failed\n");
559 dprintk(1,"returning from %s\n",__FUNCTION__);
563 void vivi_stop_thread(struct vivi_dmaqueue *dma_q)
565 dprintk(1,"%s\n",__FUNCTION__);
566 /* shutdown control thread */
567 if (dma_q->kthread) {
568 kthread_stop(dma_q->kthread);
573 static int restart_video_queue(struct vivi_dmaqueue *dma_q)
575 struct vivi_buffer *buf, *prev;
576 struct list_head *item;
578 dprintk(1,"%s dma_q=0x%08lx\n",__FUNCTION__,(unsigned long)dma_q);
580 if (!list_empty(&dma_q->active)) {
581 buf = list_entry(dma_q->active.next, struct vivi_buffer, vb.queue);
582 dprintk(2,"restart_queue [%p/%d]: restart dma\n",
585 dprintk(1,"Restarting video dma\n");
586 vivi_stop_thread(dma_q);
587 // vivi_start_thread(dma_q);
589 /* cancel all outstanding capture / vbi requests */
590 list_for_each(item,&dma_q->active) {
591 buf = list_entry(item, struct vivi_buffer, vb.queue);
593 list_del(&buf->vb.queue);
594 buf->vb.state = STATE_ERROR;
595 wake_up(&buf->vb.done);
597 mod_timer(&dma_q->timeout, jiffies+BUFFER_TIMEOUT);
604 if (list_empty(&dma_q->queued))
606 buf = list_entry(dma_q->queued.next, struct vivi_buffer, vb.queue);
608 list_del(&buf->vb.queue);
609 list_add_tail(&buf->vb.queue,&dma_q->active);
611 dprintk(1,"Restarting video dma\n");
612 vivi_stop_thread(dma_q);
613 vivi_start_thread(dma_q);
615 buf->vb.state = STATE_ACTIVE;
616 mod_timer(&dma_q->timeout, jiffies+BUFFER_TIMEOUT);
617 dprintk(2,"[%p/%d] restart_queue - first active\n",
620 } else if (prev->vb.width == buf->vb.width &&
621 prev->vb.height == buf->vb.height &&
622 prev->fmt == buf->fmt) {
623 list_del(&buf->vb.queue);
624 list_add_tail(&buf->vb.queue,&dma_q->active);
625 buf->vb.state = STATE_ACTIVE;
626 dprintk(2,"[%p/%d] restart_queue - move to active\n",
635 static void vivi_vid_timeout(unsigned long data)
637 struct vivi_dev *dev = (struct vivi_dev*)data;
638 struct vivi_dmaqueue *vidq = &dev->vidq;
639 struct vivi_buffer *buf;
641 while (!list_empty(&vidq->active)) {
642 buf = list_entry(vidq->active.next, struct vivi_buffer, vb.queue);
643 list_del(&buf->vb.queue);
644 buf->vb.state = STATE_ERROR;
645 wake_up(&buf->vb.done);
646 printk("vivi/0: [%p/%d] timeout\n", buf, buf->vb.i);
649 restart_video_queue(vidq);
652 /* ------------------------------------------------------------------
654 ------------------------------------------------------------------*/
656 buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size)
658 struct vivi_fh *fh = vq->priv_data;
660 *size = fh->width*fh->height*2;
664 while (*size * *count > vid_limit * 1024 * 1024)
670 free_buffer(struct videobuf_queue *vq, struct vivi_buffer *buf)
672 dprintk(1,"%s\n",__FUNCTION__);
677 /*FIXME: Maybe a spinlock is required here */
681 videobuf_waiton(&buf->vb,0,0);
682 videobuf_dma_unmap(vq, &buf->vb.dma);
683 videobuf_dma_free(&buf->vb.dma);
684 buf->vb.state = STATE_NEEDS_INIT;
687 #define norm_maxw() 1024
688 #define norm_maxh() 768
690 buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
691 enum v4l2_field field)
693 struct vivi_fh *fh = vq->priv_data;
694 struct vivi_buffer *buf = container_of(vb,struct vivi_buffer,vb);
695 int rc, init_buffer = 0;
697 // dprintk(1,"%s, field=%d\n",__FUNCTION__,field);
699 BUG_ON(NULL == fh->fmt);
700 if (fh->width < 48 || fh->width > norm_maxw() ||
701 fh->height < 32 || fh->height > norm_maxh())
703 buf->vb.size = fh->width*fh->height*2;
704 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
707 if (buf->fmt != fh->fmt ||
708 buf->vb.width != fh->width ||
709 buf->vb.height != fh->height ||
710 buf->vb.field != field) {
712 buf->vb.width = fh->width;
713 buf->vb.height = fh->height;
714 buf->vb.field = field;
718 if (STATE_NEEDS_INIT == buf->vb.state) {
719 if (0 != (rc = videobuf_iolock(vq,&buf->vb,NULL)))
723 buf->vb.state = STATE_PREPARED;
725 if (NULL == (buf->to_addr = kmalloc(sizeof(*buf->to_addr) * vb->dma.nr_pages,GFP_KERNEL))) {
738 buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
740 struct vivi_buffer *buf = container_of(vb,struct vivi_buffer,vb);
741 struct vivi_fh *fh = vq->priv_data;
742 struct vivi_dev *dev = fh->dev;
743 struct vivi_dmaqueue *vidq = &dev->vidq;
744 struct vivi_buffer *prev;
746 if (!list_empty(&vidq->queued)) {
747 dprintk(1,"adding vb queue=0x%08lx\n",(unsigned long)&buf->vb.queue);
748 list_add_tail(&buf->vb.queue,&vidq->queued);
749 buf->vb.state = STATE_QUEUED;
750 dprintk(2,"[%p/%d] buffer_queue - append to queued\n",
752 } else if (list_empty(&vidq->active)) {
753 list_add_tail(&buf->vb.queue,&vidq->active);
755 buf->vb.state = STATE_ACTIVE;
756 mod_timer(&vidq->timeout, jiffies+BUFFER_TIMEOUT);
757 dprintk(2,"[%p/%d] buffer_queue - first active\n",
760 vivi_start_thread(vidq);
762 prev = list_entry(vidq->active.prev, struct vivi_buffer, vb.queue);
763 if (prev->vb.width == buf->vb.width &&
764 prev->vb.height == buf->vb.height &&
765 prev->fmt == buf->fmt) {
766 list_add_tail(&buf->vb.queue,&vidq->active);
767 buf->vb.state = STATE_ACTIVE;
768 dprintk(2,"[%p/%d] buffer_queue - append to active\n",
772 list_add_tail(&buf->vb.queue,&vidq->queued);
773 buf->vb.state = STATE_QUEUED;
774 dprintk(2,"[%p/%d] buffer_queue - first queued\n",
780 static void buffer_release(struct videobuf_queue *vq, struct videobuf_buffer *vb)
782 struct vivi_buffer *buf = container_of(vb,struct vivi_buffer,vb);
783 struct vivi_fh *fh = vq->priv_data;
784 struct vivi_dev *dev = (struct vivi_dev*)fh->dev;
785 struct vivi_dmaqueue *vidq = &dev->vidq;
787 dprintk(1,"%s\n",__FUNCTION__);
789 vivi_stop_thread(vidq);
794 int vivi_map_sg (void *dev, struct scatterlist *sg, int nents,
799 dprintk(1,"%s, number of pages=%d\n",__FUNCTION__,nents);
800 BUG_ON(direction == DMA_NONE);
802 for (i = 0; i < nents; i++ ) {
805 sg[i].dma_address = page_to_phys(sg[i].page) + sg[i].offset;
811 int vivi_unmap_sg(void *dev,struct scatterlist *sglist,int nr_pages,
814 dprintk(1,"%s\n",__FUNCTION__);
818 int vivi_dma_sync_sg(void *dev,struct scatterlist *sglist,int nr_pages,
821 // dprintk(1,"%s\n",__FUNCTION__);
823 // flush_write_buffers();
827 static struct videobuf_queue_ops vivi_video_qops = {
828 .buf_setup = buffer_setup,
829 .buf_prepare = buffer_prepare,
830 .buf_queue = buffer_queue,
831 .buf_release = buffer_release,
833 /* Non-pci handling routines */
834 .vb_map_sg = vivi_map_sg,
835 .vb_dma_sync_sg = vivi_dma_sync_sg,
836 .vb_unmap_sg = vivi_unmap_sg,
839 /* ------------------------------------------------------------------
841 ------------------------------------------------------------------*/
843 static int vivi_try_fmt(struct vivi_dev *dev, struct vivi_fh *fh,
844 struct v4l2_format *f)
846 struct vivi_fmt *fmt;
847 enum v4l2_field field;
848 unsigned int maxw, maxh;
850 if (format.fourcc != f->fmt.pix.pixelformat) {
851 dprintk(1,"Fourcc format invalid.\n");
856 field = f->fmt.pix.field;
858 if (field == V4L2_FIELD_ANY) {
859 // field=V4L2_FIELD_INTERLACED;
860 field=V4L2_FIELD_SEQ_TB;
861 } else if (V4L2_FIELD_INTERLACED != field) {
862 dprintk(1,"Field type invalid.\n");
869 f->fmt.pix.field = field;
870 if (f->fmt.pix.height < 32)
871 f->fmt.pix.height = 32;
872 if (f->fmt.pix.height > maxh)
873 f->fmt.pix.height = maxh;
874 if (f->fmt.pix.width < 48)
875 f->fmt.pix.width = 48;
876 if (f->fmt.pix.width > maxw)
877 f->fmt.pix.width = maxw;
878 f->fmt.pix.width &= ~0x03;
879 f->fmt.pix.bytesperline =
880 (f->fmt.pix.width * fmt->depth) >> 3;
881 f->fmt.pix.sizeimage =
882 f->fmt.pix.height * f->fmt.pix.bytesperline;
887 static int res_get(struct vivi_dev *dev, struct vivi_fh *fh)
891 if (dev->resources) {
892 /* no, someone else uses it */
896 /* it's free, grab it */
898 dprintk(1,"res: get\n");
903 static inline int res_locked(struct vivi_dev *dev)
905 return (dev->resources);
908 static void res_free(struct vivi_dev *dev, struct vivi_fh *fh)
912 dprintk(1,"res: put\n");
916 static int vivi_do_ioctl(struct inode *inode, struct file *file, unsigned int cmd, void *arg)
918 struct vivi_fh *fh = file->private_data;
919 struct vivi_dev *dev = fh->dev;
923 if (_IOC_DIR(cmd) & _IOC_WRITE)
924 v4l_printk_ioctl_arg("vivi(w)",cmd, arg);
925 else if (!_IOC_DIR(cmd) & _IOC_READ) {
926 v4l_print_ioctl("vivi", cmd);
931 /* --- capabilities ------------------------------------------ */
932 case VIDIOC_QUERYCAP:
934 struct v4l2_capability *cap = (struct v4l2_capability*)arg;
936 memset(cap, 0, sizeof(*cap));
938 strcpy(cap->driver, "vivi");
939 strcpy(cap->card, "vivi");
940 cap->version = VIVI_VERSION;
942 V4L2_CAP_VIDEO_CAPTURE |
947 /* --- capture ioctls ---------------------------------------- */
948 case VIDIOC_ENUM_FMT:
950 struct v4l2_fmtdesc *f = arg;
951 enum v4l2_buf_type type;
957 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
963 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
968 memset(f,0,sizeof(*f));
972 strlcpy(f->description,format.name,sizeof(f->description));
973 f->pixelformat = format.fourcc;
982 struct v4l2_format *f = (struct v4l2_format *)arg;
984 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
989 memset(&f->fmt.pix,0,sizeof(f->fmt.pix));
990 f->fmt.pix.width = fh->width;
991 f->fmt.pix.height = fh->height;
992 f->fmt.pix.field = fh->vb_vidq.field;
993 f->fmt.pix.pixelformat = fh->fmt->fourcc;
994 f->fmt.pix.bytesperline =
995 (f->fmt.pix.width * fh->fmt->depth) >> 3;
996 f->fmt.pix.sizeimage =
997 f->fmt.pix.height * f->fmt.pix.bytesperline;
1002 struct v4l2_format *f = arg;
1004 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1005 dprintk(1,"Only capture supported.\n");
1010 ret = vivi_try_fmt(dev,fh,f);
1015 fh->width = f->fmt.pix.width;
1016 fh->height = f->fmt.pix.height;
1017 fh->vb_vidq.field = f->fmt.pix.field;
1022 case VIDIOC_TRY_FMT:
1024 struct v4l2_format *f = arg;
1025 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1030 ret=vivi_try_fmt(dev,fh,f);
1033 case VIDIOC_REQBUFS:
1034 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1038 ret=videobuf_reqbufs(&fh->vb_vidq, arg);
1040 case VIDIOC_QUERYBUF:
1041 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1045 ret=videobuf_querybuf(&fh->vb_vidq, arg);
1048 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1052 ret=videobuf_qbuf(&fh->vb_vidq, arg);
1055 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1059 ret=videobuf_dqbuf(&fh->vb_vidq, arg,
1060 file->f_flags & O_NONBLOCK);
1063 /* --- streaming capture ------------------------------------- */
1066 struct video_mbuf *mbuf = arg;
1067 struct videobuf_queue *q=&fh->vb_vidq;
1068 struct v4l2_requestbuffers req;
1071 memset(&req,0,sizeof(req));
1074 req.memory = V4L2_MEMORY_MMAP;
1075 ret = videobuf_reqbufs(q,&req);
1078 memset(mbuf,0,sizeof(*mbuf));
1079 mbuf->frames = req.count;
1081 for (i = 0; i < mbuf->frames; i++) {
1082 mbuf->offsets[i] = q->bufs[i]->boff;
1083 mbuf->size += q->bufs[i]->bsize;
1088 case VIDIOC_STREAMON:
1090 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1092 if (!res_get(dev,fh))
1094 ret=videobuf_streamon(&fh->vb_vidq);
1097 case VIDIOC_STREAMOFF:
1099 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1103 ret = videobuf_streamoff(&fh->vb_vidq);
1109 /* ---------- tv norms ---------- */
1110 case VIDIOC_ENUMSTD:
1112 struct v4l2_standard *e = arg;
1118 ret = v4l2_video_std_construct(e, V4L2_STD_NTSC_M, "NTSC-M");
1120 /* Allows vivi to use different fps from video std */
1121 e->frameperiod.numerator = WAKE_NUMERATOR;
1122 e->frameperiod.denominator = WAKE_DENOMINATOR;
1128 v4l2_std_id *id = arg;
1130 *id = V4L2_STD_NTSC_M;
1137 /* ------ input switching ---------- */
1138 case VIDIOC_ENUMINPUT:
1139 { /* only one input in this sample driver */
1140 struct v4l2_input *inp = arg;
1142 if (inp->index != 0) {
1146 memset(inp, 0, sizeof(*inp));
1149 inp->type = V4L2_INPUT_TYPE_CAMERA;
1150 inp->std = V4L2_STD_NTSC_M;
1151 strcpy(inp->name,"Camera");
1154 case VIDIOC_G_INPUT:
1156 unsigned int *i = arg;
1161 case VIDIOC_S_INPUT:
1163 unsigned int *i = arg;
1170 /* --- controls ---------------------------------------------- */
1171 case VIDIOC_QUERYCTRL:
1173 struct v4l2_queryctrl *qc = arg;
1176 for (i = 0; i < ARRAY_SIZE(vivi_qctrl); i++)
1177 if (qc->id && qc->id == vivi_qctrl[i].id) {
1178 memcpy(qc, &(vivi_qctrl[i]),
1188 struct v4l2_control *ctrl = arg;
1191 for (i = 0; i < ARRAY_SIZE(vivi_qctrl); i++)
1192 if (ctrl->id == vivi_qctrl[i].id) {
1193 ctrl->value=qctl_regs[i];
1202 struct v4l2_control *ctrl = arg;
1204 for (i = 0; i < ARRAY_SIZE(vivi_qctrl); i++)
1205 if (ctrl->id == vivi_qctrl[i].id) {
1207 vivi_qctrl[i].minimum
1209 vivi_qctrl[i].maximum) {
1213 qctl_regs[i]=ctrl->value;
1220 ret=v4l_compat_translate_ioctl(inode,file,cmd,arg,vivi_do_ioctl);
1225 v4l_print_ioctl("vivi(err)", cmd);
1226 dprintk(1,"errcode=%d\n",ret);
1227 } else if (_IOC_DIR(cmd) & _IOC_READ)
1228 v4l_printk_ioctl_arg("vivi(r)",cmd, arg);
1234 static int vivi_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
1236 return video_usercopy(inode, file, cmd, arg, vivi_do_ioctl);
1239 /* ------------------------------------------------------------------
1240 File operations for the device
1241 ------------------------------------------------------------------*/
1243 #define line_buf_size(norm) (norm_maxw(norm)*(format.depth+7)/8)
1245 static int vivi_open(struct inode *inode, struct file *file)
1247 int minor = iminor(inode);
1248 struct vivi_dev *h,*dev = NULL;
1250 struct list_head *list;
1251 enum v4l2_buf_type type = 0;
1254 printk(KERN_DEBUG "vivi: open called (minor=%d)\n",minor);
1256 list_for_each(list,&vivi_devlist) {
1257 h = list_entry(list, struct vivi_dev, vivi_devlist);
1258 if (h->video_dev.minor == minor) {
1260 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1267 /* If more than one user, mutex should be added */
1270 dprintk(1,"open minor=%d type=%s users=%d\n",
1271 minor,v4l2_type_names[type],dev->users);
1273 /* allocate + initialize per filehandle data */
1274 fh = kzalloc(sizeof(*fh),GFP_KERNEL);
1280 file->private_data = fh;
1282 fh->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1287 /* Put all controls at a sane state */
1288 for (i = 0; i < ARRAY_SIZE(vivi_qctrl); i++)
1289 qctl_regs[i] =vivi_qctrl[i].default_value;
1291 dprintk(1,"Open: fh=0x%08lx, dev=0x%08lx, dev->vidq=0x%08lx\n",
1292 (unsigned long)fh,(unsigned long)dev,(unsigned long)&dev->vidq);
1293 dprintk(1,"Open: list_empty queued=%d\n",list_empty(&dev->vidq.queued));
1294 dprintk(1,"Open: list_empty active=%d\n",list_empty(&dev->vidq.active));
1296 /* Resets frame counters */
1301 dev->jiffies=jiffies;
1302 sprintf(dev->timestr,"%02d:%02d:%02d:%03d",
1303 dev->h,dev->m,dev->s,(dev->us+500)/1000);
1305 videobuf_queue_init(&fh->vb_vidq, &vivi_video_qops,
1308 V4L2_FIELD_INTERLACED,
1309 sizeof(struct vivi_buffer),fh);
1315 vivi_read(struct file *file, char __user *data, size_t count, loff_t *ppos)
1317 struct vivi_fh *fh = file->private_data;
1319 if (fh->type==V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1320 if (res_locked(fh->dev))
1322 return videobuf_read_one(&fh->vb_vidq, data, count, ppos,
1323 file->f_flags & O_NONBLOCK);
1329 vivi_poll(struct file *file, struct poll_table_struct *wait)
1331 struct vivi_fh *fh = file->private_data;
1332 struct vivi_buffer *buf;
1334 dprintk(1,"%s\n",__FUNCTION__);
1336 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != fh->type)
1339 if (res_get(fh->dev,fh)) {
1340 dprintk(1,"poll: mmap interface\n");
1341 /* streaming capture */
1342 if (list_empty(&fh->vb_vidq.stream))
1344 buf = list_entry(fh->vb_vidq.stream.next,struct vivi_buffer,vb.stream);
1346 dprintk(1,"poll: read() interface\n");
1347 /* read() capture */
1348 buf = (struct vivi_buffer*)fh->vb_vidq.read_buf;
1352 poll_wait(file, &buf->vb.done, wait);
1353 if (buf->vb.state == STATE_DONE ||
1354 buf->vb.state == STATE_ERROR)
1355 return POLLIN|POLLRDNORM;
1359 static int vivi_release(struct inode *inode, struct file *file)
1361 struct vivi_fh *fh = file->private_data;
1362 struct vivi_dev *dev = fh->dev;
1363 struct vivi_dmaqueue *vidq = &dev->vidq;
1365 int minor = iminor(inode);
1367 vivi_stop_thread(vidq);
1368 videobuf_mmap_free(&fh->vb_vidq);
1374 printk(KERN_DEBUG "vivi: close called (minor=%d, users=%d)\n",minor,dev->users);
1380 vivi_mmap(struct file *file, struct vm_area_struct * vma)
1382 struct vivi_fh *fh = file->private_data;
1385 dprintk (1,"mmap called, vma=0x%08lx\n",(unsigned long)vma);
1387 ret=videobuf_mmap_mapper(&fh->vb_vidq, vma);
1389 dprintk (1,"vma start=0x%08lx, size=%ld, ret=%d\n",
1390 (unsigned long)vma->vm_start,
1391 (unsigned long)vma->vm_end-(unsigned long)vma->vm_start,
1397 static struct file_operations vivi_fops = {
1398 .owner = THIS_MODULE,
1400 .release = vivi_release,
1403 .ioctl = vivi_ioctl,
1405 .llseek = no_llseek,
1408 static struct video_device vivi = {
1409 .name = "VTM Virtual Video Capture Board",
1410 .type = VID_TYPE_CAPTURE,
1414 // .release = video_device_release,
1416 /* ------------------------------------------------------------------
1417 Initialization and module stuff
1418 ------------------------------------------------------------------*/
1420 static int __init vivi_init(void)
1423 struct vivi_dev *dev;
1425 dev = kzalloc(sizeof(*dev),GFP_KERNEL);
1428 list_add_tail(&dev->vivi_devlist,&vivi_devlist);
1430 /* init video dma queues */
1431 INIT_LIST_HEAD(&dev->vidq.active);
1432 INIT_LIST_HEAD(&dev->vidq.queued);
1434 /* initialize locks */
1435 init_MUTEX(&dev->lock);
1437 dev->vidq.timeout.function = vivi_vid_timeout;
1438 dev->vidq.timeout.data = (unsigned long)dev;
1439 init_timer(&dev->vidq.timeout);
1441 ret = video_register_device(&vivi, VFL_TYPE_GRABBER, video_nr);
1442 printk(KERN_INFO "Video Technology Magazine Virtual Video Capture Board (Load status: %d)\n", ret);
1446 static void __exit vivi_exit(void)
1449 struct list_head *list;
1451 list_for_each(list,&vivi_devlist) {
1452 h = list_entry(list, struct vivi_dev, vivi_devlist);
1455 video_unregister_device(&vivi);
1458 module_init(vivi_init);
1459 module_exit(vivi_exit);