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/interrupt.h>
30 #include <media/video-buf.h>
31 #include <media/v4l2-common.h>
32 #include <linux/kthread.h>
33 #include <linux/highmem.h>
35 /* Wake up at about 30 fps */
36 #define WAKE_NUMERATOR 30
37 #define WAKE_DENOMINATOR 1001
38 #define BUFFER_TIMEOUT msecs_to_jiffies(500) /* 0.5 seconds */
40 /* These timers are for 1 fps - used only for testing */
41 //#define WAKE_DENOMINATOR 30 /* hack for testing purposes */
42 //#define BUFFER_TIMEOUT msecs_to_jiffies(5000) /* 5 seconds */
47 #define kzalloc(size, flags) \
49 void *__ret = kmalloc(size, flags); \
51 memset(__ret, 0, size); \
56 MODULE_DESCRIPTION("Video Technology Magazine Virtual Video Capture Board");
57 MODULE_AUTHOR("Mauro Carvalho Chehab, Ted Walther and John Sokol");
58 MODULE_LICENSE("Dual BSD/GPL");
60 #define VIVI_MAJOR_VERSION 0
61 #define VIVI_MINOR_VERSION 4
62 #define VIVI_RELEASE 0
63 #define VIVI_VERSION KERNEL_VERSION(VIVI_MAJOR_VERSION, VIVI_MINOR_VERSION, VIVI_RELEASE)
65 static int video_nr = -1; /* /dev/videoN, -1 for autodetect */
66 module_param(video_nr, int, 0);
69 module_param(debug, int, 0);
71 static unsigned int vid_limit = 16;
72 module_param(vid_limit,int,0644);
73 MODULE_PARM_DESC(vid_limit,"capture memory limit in megabytes");
75 /* supported controls */
76 static struct v4l2_queryctrl vivi_qctrl[] = {
78 .id = V4L2_CID_AUDIO_VOLUME,
83 .default_value = 65535,
85 .type = V4L2_CTRL_TYPE_INTEGER,
87 .id = V4L2_CID_BRIGHTNESS,
88 .type = V4L2_CTRL_TYPE_INTEGER,
96 .id = V4L2_CID_CONTRAST,
97 .type = V4L2_CTRL_TYPE_INTEGER,
102 .default_value = 0x10,
105 .id = V4L2_CID_SATURATION,
106 .type = V4L2_CTRL_TYPE_INTEGER,
107 .name = "Saturation",
111 .default_value = 127,
115 .type = V4L2_CTRL_TYPE_INTEGER,
125 static int qctl_regs[ARRAY_SIZE(vivi_qctrl)];
127 #define dprintk(level,fmt, arg...) \
129 if (debug >= (level)) \
130 printk(KERN_DEBUG "vivi: " fmt , ## arg); \
133 /* ------------------------------------------------------------------
135 ------------------------------------------------------------------*/
139 u32 fourcc; /* v4l2 format id */
143 static struct vivi_fmt format = {
144 .name = "4:2:2, packed, YUYV",
145 .fourcc = V4L2_PIX_FMT_YUYV,
151 struct scatterlist *sg;
154 /* buffer for one video frame */
156 /* common v4l buffer stuff -- must be first */
157 struct videobuf_buffer vb;
159 struct vivi_fmt *fmt;
161 struct sg_to_addr *to_addr;
164 struct vivi_dmaqueue {
165 struct list_head active;
166 struct list_head queued;
167 struct timer_list timeout;
169 /* thread for generating video stream*/
170 struct task_struct *kthread;
171 wait_queue_head_t wq;
172 /* Counters to control fps rate */
177 static LIST_HEAD(vivi_devlist);
180 struct list_head vivi_devlist;
182 struct semaphore lock;
186 /* various device info */
187 unsigned int resources;
188 struct video_device video_dev;
190 struct vivi_dmaqueue vidq;
192 /* Several counters */
193 int h,m,s,us,jiffies;
198 struct vivi_dev *dev;
201 struct vivi_fmt *fmt;
202 unsigned int width,height;
203 struct videobuf_queue vb_vidq;
205 enum v4l2_buf_type type;
208 /* ------------------------------------------------------------------
209 DMA and thread functions
210 ------------------------------------------------------------------*/
212 /* Bars and Colors should match positions */
224 static u8 bars[8][3] = {
226 {204,204,204}, /* white */
227 {208,208, 0}, /* ambar */
228 { 0,206,206}, /* cyan */
229 { 0,239, 0}, /* green */
230 {239, 0,239}, /* magenta */
231 {205, 0, 0}, /* red */
232 { 0, 0,255}, /* blue */
236 #define TO_Y(r,g,b) (((16829*r +33039*g +6416*b + 32768)>>16)+16)
237 /* RGB to V(Cr) Color transform */
238 #define TO_V(r,g,b) (((28784*r -24103*g -4681*b + 32768)>>16)+128)
239 /* RGB to U(Cb) Color transform */
240 #define TO_U(r,g,b) (((-9714*r -19070*g +28784*b + 32768)>>16)+128)
242 #define TSTAMP_MIN_Y 24
243 #define TSTAMP_MAX_Y TSTAMP_MIN_Y+15
244 #define TSTAMP_MIN_X 64
246 void prep_to_addr(struct sg_to_addr to_addr[],struct videobuf_buffer *vb)
250 for (i=0;i<vb->dma.nr_pages;i++) {
251 to_addr[i].sg=&vb->dma.sglist[i];
253 pos += vb->dma.sglist[i].length;
257 inline int get_addr_pos(int pos, int pages, struct sg_to_addr to_addr[])
259 int p1=0,p2=pages-1,p3=pages/2;
262 BUG_ON (pos>=to_addr[p2].pos+to_addr[p2].sg->length);
265 if (pos < to_addr[p3].pos) {
272 if (pos >= to_addr[p2].pos)
278 void gen_line(struct sg_to_addr to_addr[],int inipos,int pages,int wmax,
279 int hmax, int line, char *timestr)
281 int w,i,j,pos=inipos,pgpos,oldpg,y;
286 /* Get first addr pointed to pixel position */
287 oldpg=get_addr_pos(pos,pages,to_addr);
288 pg=pfn_to_page(to_addr[oldpg].sg->dma_address >> PAGE_SHIFT);
289 basep = kmap_atomic(pg, KM_BOUNCE_READ)+to_addr[oldpg].sg->offset;
291 /* We will just duplicate the second pixel at the packet */
294 /* Generate a standard color bar pattern */
295 for (w=0;w<wmax;w++) {
300 for (color=0;color<4;color++) {
301 pgpos=get_addr_pos(pos,pages,to_addr);
303 pg=pfn_to_page(to_addr[pgpos].sg->dma_address >> PAGE_SHIFT);
304 kunmap_atomic(basep, KM_BOUNCE_READ);
305 basep= kmap_atomic(pg, KM_BOUNCE_READ)+to_addr[pgpos].sg->offset;
308 p=basep+pos-to_addr[pgpos].pos;
313 *p=TO_Y(r,g,b); /* Luminance */
316 *p=TO_U(r,g,b); /* Cb */
319 *p=TO_V(r,g,b); /* Cr */
326 /* Checks if it is possible to show timestamp */
327 if (TSTAMP_MAX_Y>=hmax)
329 if (TSTAMP_MIN_X+strlen(timestr)>=wmax)
332 /* Print stream time */
333 if (line>=TSTAMP_MIN_Y && line<=TSTAMP_MAX_Y) {
335 for (s=timestr;*s;s++) {
336 chr=rom8x16_bits[(*s-0x30)*16+line-TSTAMP_MIN_Y];
338 if (chr&1<<(7-i)) { /* Font color*/
344 } else { /* Background color */
352 for (color=0;color<4;color++) {
353 pgpos=get_addr_pos(pos,pages,to_addr);
355 pg=pfn_to_page(to_addr[pgpos].
360 basep= kmap_atomic(pg,
362 to_addr[pgpos].sg->offset;
365 p=basep+pos-to_addr[pgpos].pos;
372 *p=TO_Y(r,g,b); /* Luminance */
375 *p=TO_U(r,g,b); /* Cb */
378 *p=TO_V(r,g,b); /* Cr */
390 kunmap_atomic(basep, KM_BOUNCE_READ);
392 static void vivi_fillbuff(struct vivi_dev *dev,struct vivi_buffer *buf)
395 int hmax = buf->vb.height;
396 int wmax = buf->vb.width;
397 struct videobuf_buffer *vb=&buf->vb;
398 struct sg_to_addr *to_addr=buf->to_addr;
401 /* Test if DMA mapping is ready */
402 if (!vb->dma.sglist[0].dma_address)
405 prep_to_addr(to_addr,vb);
407 /* Check if there is enough memory */
408 BUG_ON(buf->vb.dma.nr_pages << PAGE_SHIFT < (buf->vb.width*buf->vb.height)*2);
410 for (h=0;h<hmax;h++) {
411 gen_line(to_addr,pos,vb->dma.nr_pages,wmax,hmax,h,dev->timestr);
415 /* Updates stream time */
417 dev->us+=jiffies_to_usecs(jiffies-dev->jiffies);
418 dev->jiffies=jiffies;
419 if (dev->us>=1000000) {
433 sprintf(dev->timestr,"%02d:%02d:%02d:%03d",
434 dev->h,dev->m,dev->s,(dev->us+500)/1000);
436 dprintk(2,"vivifill at %s: Buffer 0x%08lx size= %d\n",dev->timestr,
437 (unsigned long)buf->vb.dma.vmalloc,pos);
439 /* Advice that buffer was filled */
440 buf->vb.state = STATE_DONE;
441 buf->vb.field_count++;
442 do_gettimeofday(&ts);
445 list_del(&buf->vb.queue);
446 wake_up(&buf->vb.done);
449 static int restart_video_queue(struct vivi_dmaqueue *dma_q);
451 static void vivi_thread_tick(struct vivi_dmaqueue *dma_q)
453 struct vivi_buffer *buf;
454 struct vivi_dev *dev= container_of(dma_q,struct vivi_dev,vidq);
458 /* Announces videobuf that all went ok */
459 for (bc = 0;; bc++) {
460 if (list_empty(&dma_q->active)) {
461 dprintk(1,"No active queue to serve\n");
465 buf = list_entry(dma_q->active.next,
466 struct vivi_buffer, vb.queue);
468 /* Nobody is waiting something to be done, just return */
469 if (!waitqueue_active(&buf->vb.done)) {
470 mod_timer(&dma_q->timeout, jiffies+BUFFER_TIMEOUT);
474 do_gettimeofday(&buf->vb.ts);
475 dprintk(2,"[%p/%d] wakeup\n",buf,buf->vb.i);
478 vivi_fillbuff(dev,buf);
480 if (list_empty(&dma_q->active)) {
481 del_timer(&dma_q->timeout);
483 mod_timer(&dma_q->timeout, jiffies+BUFFER_TIMEOUT);
486 dprintk(1,"%s: %d buffers handled (should be 1)\n",__FUNCTION__,bc);
489 void vivi_sleep(struct vivi_dmaqueue *dma_q)
492 DECLARE_WAITQUEUE(wait, current);
494 dprintk(1,"%s dma_q=0x%08lx\n",__FUNCTION__,(unsigned long)dma_q);
496 add_wait_queue(&dma_q->wq, &wait);
497 if (!kthread_should_stop()) {
500 /* Calculate time to wake up */
501 timeout=dma_q->ini_jiffies+msecs_to_jiffies((dma_q->frame*WAKE_NUMERATOR*1000)/WAKE_DENOMINATOR)-jiffies;
504 int old=dma_q->frame;
505 dma_q->frame=(jiffies_to_msecs(jiffies-dma_q->ini_jiffies)*WAKE_DENOMINATOR)/(WAKE_NUMERATOR*1000)+1;
507 timeout=dma_q->ini_jiffies+msecs_to_jiffies((dma_q->frame*WAKE_NUMERATOR*1000)/WAKE_DENOMINATOR)-jiffies;
509 dprintk(1,"underrun, losed %d frames. "
510 "Now, frame is %d. Waking on %d jiffies\n",
511 dma_q->frame-old,dma_q->frame,timeout);
513 dprintk(1,"will sleep for %i jiffies\n",timeout);
515 vivi_thread_tick(dma_q);
517 schedule_timeout_interruptible (timeout);
520 remove_wait_queue(&dma_q->wq, &wait);
524 int vivi_thread(void *data)
526 struct vivi_dmaqueue *dma_q=data;
528 dprintk(1,"thread started\n");
533 if (kthread_should_stop())
536 dprintk(1, "thread: exit\n");
540 int vivi_start_thread(struct vivi_dmaqueue *dma_q)
543 dma_q->ini_jiffies=jiffies;
545 dprintk(1,"%s\n",__FUNCTION__);
546 init_waitqueue_head(&dma_q->wq);
548 dma_q->kthread = kthread_run(vivi_thread, dma_q, "vivi");
550 if (dma_q->kthread == NULL) {
551 printk(KERN_ERR "vivi: kernel_thread() failed\n");
554 dprintk(1,"returning from %s\n",__FUNCTION__);
558 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)
665 free_buffer(struct videobuf_queue *vq, struct vivi_buffer *buf)
667 dprintk(1,"%s\n",__FUNCTION__);
672 /*FIXME: Maybe a spinlock is required here */
676 videobuf_waiton(&buf->vb,0,0);
677 videobuf_dma_unmap(vq, &buf->vb.dma);
678 videobuf_dma_free(&buf->vb.dma);
679 buf->vb.state = STATE_NEEDS_INIT;
682 #define norm_maxw() 1024
683 #define norm_maxh() 768
685 buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
686 enum v4l2_field field)
688 struct vivi_fh *fh = vq->priv_data;
689 struct vivi_buffer *buf = container_of(vb,struct vivi_buffer,vb);
690 int rc, init_buffer = 0;
692 // dprintk(1,"%s, field=%d\n",__FUNCTION__,field);
694 BUG_ON(NULL == fh->fmt);
695 if (fh->width < 48 || fh->width > norm_maxw() ||
696 fh->height < 32 || fh->height > norm_maxh())
698 buf->vb.size = fh->width*fh->height*2;
699 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
702 if (buf->fmt != fh->fmt ||
703 buf->vb.width != fh->width ||
704 buf->vb.height != fh->height ||
705 buf->vb.field != field) {
707 buf->vb.width = fh->width;
708 buf->vb.height = fh->height;
709 buf->vb.field = field;
713 if (STATE_NEEDS_INIT == buf->vb.state) {
714 if (0 != (rc = videobuf_iolock(vq,&buf->vb,NULL)))
718 buf->vb.state = STATE_PREPARED;
720 if (NULL == (buf->to_addr = kmalloc(sizeof(*buf->to_addr) * vb->dma.nr_pages,GFP_KERNEL))) {
733 buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
735 struct vivi_buffer *buf = container_of(vb,struct vivi_buffer,vb);
736 struct vivi_fh *fh = vq->priv_data;
737 struct vivi_dev *dev = fh->dev;
738 struct vivi_dmaqueue *vidq = &dev->vidq;
739 struct vivi_buffer *prev;
741 if (!list_empty(&vidq->queued)) {
742 dprintk(1,"adding vb queue=0x%08lx\n",(unsigned long)&buf->vb.queue);
743 list_add_tail(&buf->vb.queue,&vidq->queued);
744 buf->vb.state = STATE_QUEUED;
745 dprintk(2,"[%p/%d] buffer_queue - append to queued\n",
747 } else if (list_empty(&vidq->active)) {
748 list_add_tail(&buf->vb.queue,&vidq->active);
750 buf->vb.state = STATE_ACTIVE;
751 mod_timer(&vidq->timeout, jiffies+BUFFER_TIMEOUT);
752 dprintk(2,"[%p/%d] buffer_queue - first active\n",
755 vivi_start_thread(vidq);
757 prev = list_entry(vidq->active.prev, struct vivi_buffer, vb.queue);
758 if (prev->vb.width == buf->vb.width &&
759 prev->vb.height == buf->vb.height &&
760 prev->fmt == buf->fmt) {
761 list_add_tail(&buf->vb.queue,&vidq->active);
762 buf->vb.state = STATE_ACTIVE;
763 dprintk(2,"[%p/%d] buffer_queue - append to active\n",
767 list_add_tail(&buf->vb.queue,&vidq->queued);
768 buf->vb.state = STATE_QUEUED;
769 dprintk(2,"[%p/%d] buffer_queue - first queued\n",
775 static void buffer_release(struct videobuf_queue *vq, struct videobuf_buffer *vb)
777 struct vivi_buffer *buf = container_of(vb,struct vivi_buffer,vb);
778 struct vivi_fh *fh = vq->priv_data;
779 struct vivi_dev *dev = (struct vivi_dev*)fh->dev;
780 struct vivi_dmaqueue *vidq = &dev->vidq;
782 dprintk(1,"%s\n",__FUNCTION__);
784 vivi_stop_thread(vidq);
789 int vivi_map_sg (void *dev, struct scatterlist *sg, int nents,
794 dprintk(1,"%s, number of pages=%d\n",__FUNCTION__,nents);
795 BUG_ON(direction == DMA_NONE);
797 for (i = 0; i < nents; i++ ) {
800 sg[i].dma_address = page_to_phys(sg[i].page) + sg[i].offset;
806 int vivi_unmap_sg(void *dev,struct scatterlist *sglist,int nr_pages,
809 dprintk(1,"%s\n",__FUNCTION__);
813 int vivi_dma_sync_sg(void *dev,struct scatterlist *sglist,int nr_pages,
816 // dprintk(1,"%s\n",__FUNCTION__);
818 // flush_write_buffers();
822 static struct videobuf_queue_ops vivi_video_qops = {
823 .buf_setup = buffer_setup,
824 .buf_prepare = buffer_prepare,
825 .buf_queue = buffer_queue,
826 .buf_release = buffer_release,
828 /* Non-pci handling routines */
829 .vb_map_sg = vivi_map_sg,
830 .vb_dma_sync_sg = vivi_dma_sync_sg,
831 .vb_unmap_sg = vivi_unmap_sg,
834 /* ------------------------------------------------------------------
836 ------------------------------------------------------------------*/
838 static int vivi_try_fmt(struct vivi_dev *dev, struct vivi_fh *fh,
839 struct v4l2_format *f)
841 struct vivi_fmt *fmt;
842 enum v4l2_field field;
843 unsigned int maxw, maxh;
845 if (format.fourcc != f->fmt.pix.pixelformat) {
846 dprintk(1,"Fourcc format invalid.\n");
851 field = f->fmt.pix.field;
853 if (field == V4L2_FIELD_ANY) {
854 // field=V4L2_FIELD_INTERLACED;
855 field=V4L2_FIELD_SEQ_TB;
856 } else if (V4L2_FIELD_INTERLACED != field) {
857 dprintk(1,"Field type invalid.\n");
864 f->fmt.pix.field = field;
865 if (f->fmt.pix.height < 32)
866 f->fmt.pix.height = 32;
867 if (f->fmt.pix.height > maxh)
868 f->fmt.pix.height = maxh;
869 if (f->fmt.pix.width < 48)
870 f->fmt.pix.width = 48;
871 if (f->fmt.pix.width > maxw)
872 f->fmt.pix.width = maxw;
873 f->fmt.pix.width &= ~0x03;
874 f->fmt.pix.bytesperline =
875 (f->fmt.pix.width * fmt->depth) >> 3;
876 f->fmt.pix.sizeimage =
877 f->fmt.pix.height * f->fmt.pix.bytesperline;
882 static int res_get(struct vivi_dev *dev, struct vivi_fh *fh)
886 if (dev->resources) {
887 /* no, someone else uses it */
891 /* it's free, grab it */
893 dprintk(1,"res: get\n");
898 static inline int res_locked(struct vivi_dev *dev)
900 return (dev->resources);
903 static void res_free(struct vivi_dev *dev, struct vivi_fh *fh)
907 dprintk(1,"res: put\n");
911 static int vivi_do_ioctl(struct inode *inode, struct file *file, unsigned int cmd, void *arg)
913 struct vivi_fh *fh = file->private_data;
914 struct vivi_dev *dev = fh->dev;
918 if (_IOC_DIR(cmd) & _IOC_WRITE)
919 v4l_printk_ioctl_arg("vivi(w)",cmd, arg);
920 else if (!_IOC_DIR(cmd) & _IOC_READ) {
921 v4l_print_ioctl("vivi", cmd);
926 /* --- capabilities ------------------------------------------ */
927 case VIDIOC_QUERYCAP:
929 struct v4l2_capability *cap = (struct v4l2_capability*)arg;
931 memset(cap, 0, sizeof(*cap));
933 strcpy(cap->driver, "vivi");
934 strcpy(cap->card, "vivi");
935 cap->version = VIVI_VERSION;
937 V4L2_CAP_VIDEO_CAPTURE |
942 /* --- capture ioctls ---------------------------------------- */
943 case VIDIOC_ENUM_FMT:
945 struct v4l2_fmtdesc *f = arg;
946 enum v4l2_buf_type type;
952 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
958 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
963 memset(f,0,sizeof(*f));
967 strlcpy(f->description,format.name,sizeof(f->description));
968 f->pixelformat = format.fourcc;
977 struct v4l2_format *f = (struct v4l2_format *)arg;
979 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
984 memset(&f->fmt.pix,0,sizeof(f->fmt.pix));
985 f->fmt.pix.width = fh->width;
986 f->fmt.pix.height = fh->height;
987 f->fmt.pix.field = fh->vb_vidq.field;
988 f->fmt.pix.pixelformat = fh->fmt->fourcc;
989 f->fmt.pix.bytesperline =
990 (f->fmt.pix.width * fh->fmt->depth) >> 3;
991 f->fmt.pix.sizeimage =
992 f->fmt.pix.height * f->fmt.pix.bytesperline;
997 struct v4l2_format *f = arg;
999 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1000 dprintk(1,"Only capture supported.\n");
1005 ret = vivi_try_fmt(dev,fh,f);
1010 fh->width = f->fmt.pix.width;
1011 fh->height = f->fmt.pix.height;
1012 fh->vb_vidq.field = f->fmt.pix.field;
1017 case VIDIOC_TRY_FMT:
1019 struct v4l2_format *f = arg;
1020 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1025 ret=vivi_try_fmt(dev,fh,f);
1028 case VIDIOC_REQBUFS:
1029 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1033 ret=videobuf_reqbufs(&fh->vb_vidq, arg);
1035 case VIDIOC_QUERYBUF:
1036 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1040 ret=videobuf_querybuf(&fh->vb_vidq, arg);
1043 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1047 ret=videobuf_qbuf(&fh->vb_vidq, arg);
1050 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1054 ret=videobuf_dqbuf(&fh->vb_vidq, arg,
1055 file->f_flags & O_NONBLOCK);
1058 /* --- streaming capture ------------------------------------- */
1061 struct video_mbuf *mbuf = arg;
1062 struct videobuf_queue *q=&fh->vb_vidq;
1063 struct v4l2_requestbuffers req;
1066 memset(&req,0,sizeof(req));
1069 req.memory = V4L2_MEMORY_MMAP;
1070 ret = videobuf_reqbufs(q,&req);
1073 memset(mbuf,0,sizeof(*mbuf));
1074 mbuf->frames = req.count;
1076 for (i = 0; i < mbuf->frames; i++) {
1077 mbuf->offsets[i] = q->bufs[i]->boff;
1078 mbuf->size += q->bufs[i]->bsize;
1083 case VIDIOC_STREAMON:
1085 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1087 if (!res_get(dev,fh))
1089 ret=videobuf_streamon(&fh->vb_vidq);
1092 case VIDIOC_STREAMOFF:
1094 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1098 ret = videobuf_streamoff(&fh->vb_vidq);
1104 /* ---------- tv norms ---------- */
1105 case VIDIOC_ENUMSTD:
1107 struct v4l2_standard *e = arg;
1113 ret = v4l2_video_std_construct(e, V4L2_STD_NTSC_M, "NTSC-M");
1115 /* Allows vivi to use different fps from video std */
1116 e->frameperiod.numerator = WAKE_NUMERATOR;
1117 e->frameperiod.denominator = WAKE_DENOMINATOR;
1123 v4l2_std_id *id = arg;
1125 *id = V4L2_STD_NTSC_M;
1132 /* ------ input switching ---------- */
1133 case VIDIOC_ENUMINPUT:
1134 { /* only one input in this sample driver */
1135 struct v4l2_input *inp = arg;
1137 if (inp->index != 0) {
1141 memset(inp, 0, sizeof(*inp));
1144 inp->type = V4L2_INPUT_TYPE_CAMERA;
1145 inp->std = V4L2_STD_NTSC_M;
1146 strcpy(inp->name,"Camera");
1149 case VIDIOC_G_INPUT:
1151 unsigned int *i = arg;
1156 case VIDIOC_S_INPUT:
1158 unsigned int *i = arg;
1165 /* --- controls ---------------------------------------------- */
1166 case VIDIOC_QUERYCTRL:
1168 struct v4l2_queryctrl *qc = arg;
1171 for (i = 0; i < ARRAY_SIZE(vivi_qctrl); i++)
1172 if (qc->id && qc->id == vivi_qctrl[i].id) {
1173 memcpy(qc, &(vivi_qctrl[i]),
1183 struct v4l2_control *ctrl = arg;
1186 for (i = 0; i < ARRAY_SIZE(vivi_qctrl); i++)
1187 if (ctrl->id == vivi_qctrl[i].id) {
1188 ctrl->value=qctl_regs[i];
1197 struct v4l2_control *ctrl = arg;
1199 for (i = 0; i < ARRAY_SIZE(vivi_qctrl); i++)
1200 if (ctrl->id == vivi_qctrl[i].id) {
1202 vivi_qctrl[i].minimum
1204 vivi_qctrl[i].maximum) {
1208 qctl_regs[i]=ctrl->value;
1215 ret=v4l_compat_translate_ioctl(inode,file,cmd,arg,vivi_do_ioctl);
1220 v4l_print_ioctl("vivi(err)", cmd);
1221 dprintk(1,"errcode=%d\n",ret);
1222 } else if (_IOC_DIR(cmd) & _IOC_READ)
1223 v4l_printk_ioctl_arg("vivi(r)",cmd, arg);
1229 static int vivi_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
1231 return video_usercopy(inode, file, cmd, arg, vivi_do_ioctl);
1234 /* ------------------------------------------------------------------
1235 File operations for the device
1236 ------------------------------------------------------------------*/
1238 #define line_buf_size(norm) (norm_maxw(norm)*(format.depth+7)/8)
1240 static int vivi_open(struct inode *inode, struct file *file)
1242 int minor = iminor(inode);
1243 struct vivi_dev *h,*dev = NULL;
1245 struct list_head *list;
1246 enum v4l2_buf_type type = 0;
1249 printk(KERN_DEBUG "vivi: open called (minor=%d)\n",minor);
1251 list_for_each(list,&vivi_devlist) {
1252 h = list_entry(list, struct vivi_dev, vivi_devlist);
1253 if (h->video_dev.minor == minor) {
1255 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1262 /* If more than one user, mutex should be added */
1265 dprintk(1,"open minor=%d type=%s users=%d\n",
1266 minor,v4l2_type_names[type],dev->users);
1268 /* allocate + initialize per filehandle data */
1269 fh = kzalloc(sizeof(*fh),GFP_KERNEL);
1275 file->private_data = fh;
1277 fh->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1282 /* Put all controls at a sane state */
1283 for (i = 0; i < ARRAY_SIZE(vivi_qctrl); i++)
1284 qctl_regs[i] =vivi_qctrl[i].default_value;
1286 dprintk(1,"Open: fh=0x%08lx, dev=0x%08lx, dev->vidq=0x%08lx\n",
1287 (unsigned long)fh,(unsigned long)dev,(unsigned long)&dev->vidq);
1288 dprintk(1,"Open: list_empty queued=%d\n",list_empty(&dev->vidq.queued));
1289 dprintk(1,"Open: list_empty active=%d\n",list_empty(&dev->vidq.active));
1291 /* Resets frame counters */
1296 dev->jiffies=jiffies;
1297 sprintf(dev->timestr,"%02d:%02d:%02d:%03d",
1298 dev->h,dev->m,dev->s,(dev->us+500)/1000);
1300 videobuf_queue_init(&fh->vb_vidq, &vivi_video_qops,
1303 V4L2_FIELD_INTERLACED,
1304 sizeof(struct vivi_buffer),fh);
1310 vivi_read(struct file *file, char __user *data, size_t count, loff_t *ppos)
1312 struct vivi_fh *fh = file->private_data;
1314 if (fh->type==V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1315 if (res_locked(fh->dev))
1317 return videobuf_read_one(&fh->vb_vidq, data, count, ppos,
1318 file->f_flags & O_NONBLOCK);
1324 vivi_poll(struct file *file, struct poll_table_struct *wait)
1326 struct vivi_fh *fh = file->private_data;
1327 struct vivi_buffer *buf;
1329 dprintk(1,"%s\n",__FUNCTION__);
1331 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != fh->type)
1334 if (res_get(fh->dev,fh)) {
1335 dprintk(1,"poll: mmap interface\n");
1336 /* streaming capture */
1337 if (list_empty(&fh->vb_vidq.stream))
1339 buf = list_entry(fh->vb_vidq.stream.next,struct vivi_buffer,vb.stream);
1341 dprintk(1,"poll: read() interface\n");
1342 /* read() capture */
1343 buf = (struct vivi_buffer*)fh->vb_vidq.read_buf;
1347 poll_wait(file, &buf->vb.done, wait);
1348 if (buf->vb.state == STATE_DONE ||
1349 buf->vb.state == STATE_ERROR)
1350 return POLLIN|POLLRDNORM;
1354 static int vivi_release(struct inode *inode, struct file *file)
1356 struct vivi_fh *fh = file->private_data;
1357 struct vivi_dev *dev = fh->dev;
1358 struct vivi_dmaqueue *vidq = &dev->vidq;
1360 int minor = iminor(inode);
1362 vivi_stop_thread(vidq);
1363 videobuf_mmap_free(&fh->vb_vidq);
1369 printk(KERN_DEBUG "vivi: close called (minor=%d, users=%d)\n",minor,dev->users);
1375 vivi_mmap(struct file *file, struct vm_area_struct * vma)
1377 struct vivi_fh *fh = file->private_data;
1380 dprintk (1,"mmap called, vma=0x%08lx\n",(unsigned long)vma);
1382 ret=videobuf_mmap_mapper(&fh->vb_vidq, vma);
1384 dprintk (1,"vma start=0x%08lx, size=%ld, ret=%d\n",
1385 (unsigned long)vma->vm_start,
1386 (unsigned long)vma->vm_end-(unsigned long)vma->vm_start,
1392 static struct file_operations vivi_fops = {
1393 .owner = THIS_MODULE,
1395 .release = vivi_release,
1398 .ioctl = vivi_ioctl,
1400 .llseek = no_llseek,
1403 static struct video_device vivi = {
1404 .name = "VTM Virtual Video Capture Board",
1405 .type = VID_TYPE_CAPTURE,
1409 // .release = video_device_release,
1411 /* ------------------------------------------------------------------
1412 Initialization and module stuff
1413 ------------------------------------------------------------------*/
1415 static int __init vivi_init(void)
1418 struct vivi_dev *dev;
1420 dev = kzalloc(sizeof(*dev),GFP_KERNEL);
1423 list_add_tail(&dev->vivi_devlist,&vivi_devlist);
1425 /* init video dma queues */
1426 INIT_LIST_HEAD(&dev->vidq.active);
1427 INIT_LIST_HEAD(&dev->vidq.queued);
1429 /* initialize locks */
1430 init_MUTEX(&dev->lock);
1432 dev->vidq.timeout.function = vivi_vid_timeout;
1433 dev->vidq.timeout.data = (unsigned long)dev;
1434 init_timer(&dev->vidq.timeout);
1436 ret = video_register_device(&vivi, VFL_TYPE_GRABBER, video_nr);
1437 printk(KERN_INFO "Video Technology Magazine Virtual Video Capture Board (Load status: %d)\n", ret);
1441 static void __exit vivi_exit(void)
1444 struct list_head *list;
1446 list_for_each(list,&vivi_devlist) {
1447 h = list_entry(list, struct vivi_dev, vivi_devlist);
1450 video_unregister_device(&vivi);
1453 module_init(vivi_init);
1454 module_exit(vivi_exit);