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 <media/video-buf.h>
30 #include <media/v4l2-common.h>
31 #include <linux/kthread.h>
32 #include <linux/highmem.h>
34 /* Wake up at about 30 fps */
35 #define WAKE_NUMERATOR 30
36 #define WAKE_DENOMINATOR 1001
37 #define BUFFER_TIMEOUT msecs_to_jiffies(500) /* 0.5 seconds */
39 /* These timers are for 1 fps - used only for testing */
40 //#define WAKE_DENOMINATOR 30 /* hack for testing purposes */
41 //#define BUFFER_TIMEOUT msecs_to_jiffies(5000) /* 5 seconds */
46 #define kzalloc(size, flags) \
48 void *__ret = kmalloc(size, flags); \
50 memset(__ret, 0, size); \
55 MODULE_DESCRIPTION("Video Technology Magazine Virtual Video Capture Board");
56 MODULE_AUTHOR("Ted Walther and John Sokol");
57 MODULE_LICENSE("Dual BSD/GPL");
59 #define VIVI_MAJOR_VERSION 0
60 #define VIVI_MINOR_VERSION 4
61 #define VIVI_RELEASE 0
62 #define VIVI_VERSION KERNEL_VERSION(VIVI_MAJOR_VERSION, VIVI_MINOR_VERSION, VIVI_RELEASE)
64 static int video_nr = -1; /* /dev/videoN, -1 for autodetect */
65 module_param(video_nr, int, 0);
68 module_param(debug, int, 0);
70 static unsigned int vid_limit = 16;
71 module_param(vid_limit,int,0644);
72 MODULE_PARM_DESC(vid_limit,"capture memory limit in megabytes");
74 /* supported controls */
75 static struct v4l2_queryctrl vivi_qctrl[] = {
77 .id = V4L2_CID_AUDIO_VOLUME,
82 .default_value = 65535,
84 .type = V4L2_CTRL_TYPE_INTEGER,
86 .id = V4L2_CID_BRIGHTNESS,
87 .type = V4L2_CTRL_TYPE_INTEGER,
95 .id = V4L2_CID_CONTRAST,
96 .type = V4L2_CTRL_TYPE_INTEGER,
101 .default_value = 0x10,
104 .id = V4L2_CID_SATURATION,
105 .type = V4L2_CTRL_TYPE_INTEGER,
106 .name = "Saturation",
110 .default_value = 127,
114 .type = V4L2_CTRL_TYPE_INTEGER,
124 static int qctl_regs[ARRAY_SIZE(vivi_qctrl)];
126 #define dprintk(level,fmt, arg...) \
128 if (debug >= (level)) \
129 printk(KERN_DEBUG "vivi: " fmt , ## arg); \
132 /* ------------------------------------------------------------------
134 ------------------------------------------------------------------*/
138 u32 fourcc; /* v4l2 format id */
142 static struct vivi_fmt format = {
143 .name = "4:2:2, packed, YUYV",
144 .fourcc = V4L2_PIX_FMT_YUYV,
150 struct scatterlist *sg;
153 /* buffer for one video frame */
155 /* common v4l buffer stuff -- must be first */
156 struct videobuf_buffer vb;
158 struct vivi_fmt *fmt;
160 struct sg_to_addr *to_addr;
163 struct vivi_dmaqueue {
164 struct list_head active;
165 struct list_head queued;
166 struct timer_list timeout;
168 /* thread for generating video stream*/
169 struct task_struct *kthread;
170 wait_queue_head_t wq;
171 /* Counters to control fps rate */
176 static LIST_HEAD(vivi_devlist);
179 struct list_head vivi_devlist;
181 struct semaphore lock;
185 /* various device info */
186 unsigned int resources;
187 struct video_device video_dev;
189 struct vivi_dmaqueue vidq;
191 /* Several counters */
192 int h,m,s,us,jiffies;
197 struct vivi_dev *dev;
200 struct vivi_fmt *fmt;
201 unsigned int width,height;
202 struct videobuf_queue vb_vidq;
204 enum v4l2_buf_type type;
207 /* ------------------------------------------------------------------
208 DMA and thread functions
209 ------------------------------------------------------------------*/
211 /* Bars and Colors should match positions */
223 static u8 bars[8][3] = {
225 {204,204,204}, /* white */
226 {208,208, 0}, /* ambar */
227 { 0,206,206}, /* cyan */
228 { 0,239, 0}, /* green */
229 {239, 0,239}, /* magenta */
230 {205, 0, 0}, /* red */
231 { 0, 0,255}, /* blue */
235 #define TO_Y(r,g,b) (((16829*r +33039*g +6416*b + 32768)>>16)+16)
236 /* RGB to V(Cr) Color transform */
237 #define TO_V(r,g,b) (((28784*r -24103*g -4681*b + 32768)>>16)+128)
238 /* RGB to U(Cb) Color transform */
239 #define TO_U(r,g,b) (((-9714*r -19070*g +28784*b + 32768)>>16)+128)
241 #define TSTAMP_MIN_Y 24
242 #define TSTAMP_MAX_Y TSTAMP_MIN_Y+15
243 #define TSTAMP_MIN_X 64
245 void prep_to_addr(struct sg_to_addr to_addr[],struct videobuf_buffer *vb)
249 for (i=0;i<vb->dma.nr_pages;i++) {
250 to_addr[i].sg=&vb->dma.sglist[i];
252 pos += vb->dma.sglist[i].length;
256 inline int get_addr_pos(int pos, int pages, struct sg_to_addr to_addr[])
258 int p1=0,p2=pages-1,p3=pages/2;
261 BUG_ON (pos>=to_addr[p2].pos+to_addr[p2].sg->length);
264 if (pos < to_addr[p3].pos) {
271 if (pos >= to_addr[p2].pos)
277 void gen_line(struct sg_to_addr to_addr[],int inipos,int pages,int wmax,
278 int hmax, int line, char *timestr)
280 int w,i,j,pos=inipos,pgpos,oldpg,y;
285 /* Get first addr pointed to pixel position */
286 oldpg=get_addr_pos(pos,pages,to_addr);
287 pg=pfn_to_page(to_addr[oldpg].sg->dma_address >> PAGE_SHIFT);
288 basep = kmap_atomic(pg, KM_BOUNCE_READ)+to_addr[oldpg].sg->offset;
290 /* We will just duplicate the second pixel at the packet */
293 /* Generate a standard color bar pattern */
294 for (w=0;w<wmax;w++) {
299 for (color=0;color<4;color++) {
300 pgpos=get_addr_pos(pos,pages,to_addr);
302 pg=pfn_to_page(to_addr[pgpos].sg->dma_address >> PAGE_SHIFT);
303 kunmap_atomic(basep, KM_BOUNCE_READ);
304 basep= kmap_atomic(pg, KM_BOUNCE_READ)+to_addr[pgpos].sg->offset;
307 p=basep+pos-to_addr[pgpos].pos;
312 *p=TO_Y(r,g,b); /* Luminance */
315 *p=TO_U(r,g,b); /* Cb */
318 *p=TO_V(r,g,b); /* Cr */
325 /* Checks if it is possible to show timestamp */
326 if (TSTAMP_MAX_Y>=hmax)
328 if (TSTAMP_MIN_X+strlen(timestr)>=wmax)
331 /* Print stream time */
332 if (line>=TSTAMP_MIN_Y && line<=TSTAMP_MAX_Y) {
334 for (s=timestr;*s;s++) {
335 chr=rom8x16_bits[(*s-0x30)*16+line-TSTAMP_MIN_Y];
337 if (chr&1<<(7-i)) { /* Font color*/
343 } else { /* Background color */
351 for (color=0;color<4;color++) {
352 pgpos=get_addr_pos(pos,pages,to_addr);
354 pg=pfn_to_page(to_addr[pgpos].
359 basep= kmap_atomic(pg,
361 to_addr[pgpos].sg->offset;
364 p=basep+pos-to_addr[pgpos].pos;
371 *p=TO_Y(r,g,b); /* Luminance */
374 *p=TO_U(r,g,b); /* Cb */
377 *p=TO_V(r,g,b); /* Cr */
389 kunmap_atomic(basep, KM_BOUNCE_READ);
391 static void vivi_fillbuff(struct vivi_dev *dev,struct vivi_buffer *buf)
394 int hmax = buf->vb.height;
395 int wmax = buf->vb.width;
396 struct videobuf_buffer *vb=&buf->vb;
397 struct sg_to_addr *to_addr=buf->to_addr;
400 /* Test if DMA mapping is ready */
401 if (!vb->dma.sglist[0].dma_address)
404 prep_to_addr(to_addr,vb);
406 /* Check if there is enough memory */
407 BUG_ON(buf->vb.dma.nr_pages << PAGE_SHIFT < (buf->vb.width*buf->vb.height)*2);
409 for (h=0;h<hmax;h++) {
410 gen_line(to_addr,pos,vb->dma.nr_pages,wmax,hmax,h,dev->timestr);
414 /* Updates stream time */
416 dev->us+=jiffies_to_usecs(jiffies-dev->jiffies);
417 dev->jiffies=jiffies;
418 if (dev->us>=1000000) {
432 sprintf(dev->timestr,"%02d:%02d:%02d:%03d",
433 dev->h,dev->m,dev->s,(dev->us+500)/1000);
435 dprintk(2,"vivifill at %s: Buffer 0x%08lx size= %d\n",dev->timestr,
436 (unsigned long)buf->vb.dma.vmalloc,pos);
438 /* Advice that buffer was filled */
439 buf->vb.state = STATE_DONE;
440 buf->vb.field_count++;
441 do_gettimeofday(&ts);
444 list_del(&buf->vb.queue);
445 wake_up(&buf->vb.done);
448 static int restart_video_queue(struct vivi_dmaqueue *dma_q);
450 static void vivi_thread_tick(struct vivi_dmaqueue *dma_q)
452 struct vivi_buffer *buf;
453 struct vivi_dev *dev= container_of(dma_q,struct vivi_dev,vidq);
457 /* Announces videobuf that all went ok */
458 for (bc = 0;; bc++) {
459 if (list_empty(&dma_q->active)) {
460 dprintk(1,"No active queue to serve\n");
464 buf = list_entry(dma_q->active.next,
465 struct vivi_buffer, vb.queue);
467 /* Nobody is waiting something to be done, just return */
468 if (!waitqueue_active(&buf->vb.done)) {
469 mod_timer(&dma_q->timeout, jiffies+BUFFER_TIMEOUT);
473 do_gettimeofday(&buf->vb.ts);
474 dprintk(2,"[%p/%d] wakeup\n",buf,buf->vb.i);
477 vivi_fillbuff(dev,buf);
479 if (list_empty(&dma_q->active)) {
480 del_timer(&dma_q->timeout);
482 mod_timer(&dma_q->timeout, jiffies+BUFFER_TIMEOUT);
485 dprintk(1,"%s: %d buffers handled (should be 1)\n",__FUNCTION__,bc);
488 void vivi_sleep(struct vivi_dmaqueue *dma_q)
491 DECLARE_WAITQUEUE(wait, current);
493 dprintk(1,"%s dma_q=0x%08lx\n",__FUNCTION__,(unsigned long)dma_q);
495 add_wait_queue(&dma_q->wq, &wait);
496 if (!kthread_should_stop()) {
499 /* Calculate time to wake up */
500 timeout=dma_q->ini_jiffies+msecs_to_jiffies((dma_q->frame*WAKE_NUMERATOR*1000)/WAKE_DENOMINATOR)-jiffies;
503 int old=dma_q->frame;
504 dma_q->frame=(jiffies_to_msecs(jiffies-dma_q->ini_jiffies)*WAKE_DENOMINATOR)/(WAKE_NUMERATOR*1000)+1;
506 timeout=dma_q->ini_jiffies+msecs_to_jiffies((dma_q->frame*WAKE_NUMERATOR*1000)/WAKE_DENOMINATOR)-jiffies;
508 dprintk(1,"underrun, losed %d frames. "
509 "Now, frame is %d. Waking on %d jiffies\n",
510 dma_q->frame-old,dma_q->frame,timeout);
512 dprintk(1,"will sleep for %i jiffies\n",timeout);
514 vivi_thread_tick(dma_q);
516 schedule_timeout_interruptible (timeout);
519 remove_wait_queue(&dma_q->wq, &wait);
523 int vivi_thread(void *data)
525 struct vivi_dmaqueue *dma_q=data;
527 dprintk(1,"thread started\n");
532 if (kthread_should_stop())
535 dprintk(1, "thread: exit\n");
539 int vivi_start_thread(struct vivi_dmaqueue *dma_q)
542 dma_q->ini_jiffies=jiffies;
544 dprintk(1,"%s\n",__FUNCTION__);
545 init_waitqueue_head(&dma_q->wq);
547 dma_q->kthread = kthread_run(vivi_thread, dma_q, "vivi");
549 if (dma_q->kthread == NULL) {
550 printk(KERN_ERR "vivi: kernel_thread() failed\n");
553 dprintk(1,"returning from %s\n",__FUNCTION__);
557 void vivi_stop_thread(struct vivi_dmaqueue *dma_q)
559 dprintk(1,"%s\n",__FUNCTION__);
560 /* shutdown control thread */
561 if (dma_q->kthread) {
562 kthread_stop(dma_q->kthread);
567 static int restart_video_queue(struct vivi_dmaqueue *dma_q)
569 struct vivi_buffer *buf, *prev;
570 struct list_head *item;
572 dprintk(1,"%s dma_q=0x%08lx\n",__FUNCTION__,(unsigned long)dma_q);
574 if (!list_empty(&dma_q->active)) {
575 buf = list_entry(dma_q->active.next, struct vivi_buffer, vb.queue);
576 dprintk(2,"restart_queue [%p/%d]: restart dma\n",
579 dprintk(1,"Restarting video dma\n");
580 vivi_stop_thread(dma_q);
581 // vivi_start_thread(dma_q);
583 /* cancel all outstanding capture / vbi requests */
584 list_for_each(item,&dma_q->active) {
585 buf = list_entry(item, struct vivi_buffer, vb.queue);
587 list_del(&buf->vb.queue);
588 buf->vb.state = STATE_ERROR;
589 wake_up(&buf->vb.done);
591 mod_timer(&dma_q->timeout, jiffies+BUFFER_TIMEOUT);
598 if (list_empty(&dma_q->queued))
600 buf = list_entry(dma_q->queued.next, struct vivi_buffer, vb.queue);
602 list_del(&buf->vb.queue);
603 list_add_tail(&buf->vb.queue,&dma_q->active);
605 dprintk(1,"Restarting video dma\n");
606 vivi_stop_thread(dma_q);
607 vivi_start_thread(dma_q);
609 buf->vb.state = STATE_ACTIVE;
610 mod_timer(&dma_q->timeout, jiffies+BUFFER_TIMEOUT);
611 dprintk(2,"[%p/%d] restart_queue - first active\n",
614 } else if (prev->vb.width == buf->vb.width &&
615 prev->vb.height == buf->vb.height &&
616 prev->fmt == buf->fmt) {
617 list_del(&buf->vb.queue);
618 list_add_tail(&buf->vb.queue,&dma_q->active);
619 buf->vb.state = STATE_ACTIVE;
620 dprintk(2,"[%p/%d] restart_queue - move to active\n",
629 static void vivi_vid_timeout(unsigned long data)
631 struct vivi_dev *dev = (struct vivi_dev*)data;
632 struct vivi_dmaqueue *vidq = &dev->vidq;
633 struct vivi_buffer *buf;
635 while (!list_empty(&vidq->active)) {
636 buf = list_entry(vidq->active.next, struct vivi_buffer, vb.queue);
637 list_del(&buf->vb.queue);
638 buf->vb.state = STATE_ERROR;
639 wake_up(&buf->vb.done);
640 printk("vivi/0: [%p/%d] timeout\n", buf, buf->vb.i);
643 restart_video_queue(vidq);
646 /* ------------------------------------------------------------------
648 ------------------------------------------------------------------*/
650 buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size)
652 struct vivi_fh *fh = vq->priv_data;
654 *size = fh->width*fh->height*2;
658 while (*size * *count > vid_limit * 1024 * 1024)
664 free_buffer(struct videobuf_queue *vq, struct vivi_buffer *buf)
666 dprintk(1,"%s\n",__FUNCTION__);
671 /*FIXME: Maybe a spinlock is required here */
675 videobuf_waiton(&buf->vb,0,0);
676 videobuf_dma_unmap(vq, &buf->vb.dma);
677 videobuf_dma_free(&buf->vb.dma);
678 buf->vb.state = STATE_NEEDS_INIT;
681 #define norm_maxw() 1024
682 #define norm_maxh() 768
684 buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
685 enum v4l2_field field)
687 struct vivi_fh *fh = vq->priv_data;
688 struct vivi_buffer *buf = container_of(vb,struct vivi_buffer,vb);
689 int rc, init_buffer = 0;
691 // dprintk(1,"%s, field=%d\n",__FUNCTION__,field);
693 BUG_ON(NULL == fh->fmt);
694 if (fh->width < 48 || fh->width > norm_maxw() ||
695 fh->height < 32 || fh->height > norm_maxh())
697 buf->vb.size = fh->width*fh->height*2;
698 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
701 if (buf->fmt != fh->fmt ||
702 buf->vb.width != fh->width ||
703 buf->vb.height != fh->height ||
704 buf->vb.field != field) {
706 buf->vb.width = fh->width;
707 buf->vb.height = fh->height;
708 buf->vb.field = field;
712 if (STATE_NEEDS_INIT == buf->vb.state) {
713 if (0 != (rc = videobuf_iolock(vq,&buf->vb,NULL)))
717 buf->vb.state = STATE_PREPARED;
719 if (NULL == (buf->to_addr = kmalloc(sizeof(*buf->to_addr) * vb->dma.nr_pages,GFP_KERNEL))) {
732 buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
734 struct vivi_buffer *buf = container_of(vb,struct vivi_buffer,vb);
735 struct vivi_fh *fh = vq->priv_data;
736 struct vivi_dev *dev = fh->dev;
737 struct vivi_dmaqueue *vidq = &dev->vidq;
738 struct vivi_buffer *prev;
740 if (!list_empty(&vidq->queued)) {
741 dprintk(1,"adding vb queue=0x%08lx\n",(unsigned long)&buf->vb.queue);
742 list_add_tail(&buf->vb.queue,&vidq->queued);
743 buf->vb.state = STATE_QUEUED;
744 dprintk(2,"[%p/%d] buffer_queue - append to queued\n",
746 } else if (list_empty(&vidq->active)) {
747 list_add_tail(&buf->vb.queue,&vidq->active);
749 buf->vb.state = STATE_ACTIVE;
750 mod_timer(&vidq->timeout, jiffies+BUFFER_TIMEOUT);
751 dprintk(2,"[%p/%d] buffer_queue - first active\n",
754 vivi_start_thread(vidq);
756 prev = list_entry(vidq->active.prev, struct vivi_buffer, vb.queue);
757 if (prev->vb.width == buf->vb.width &&
758 prev->vb.height == buf->vb.height &&
759 prev->fmt == buf->fmt) {
760 list_add_tail(&buf->vb.queue,&vidq->active);
761 buf->vb.state = STATE_ACTIVE;
762 dprintk(2,"[%p/%d] buffer_queue - append to active\n",
766 list_add_tail(&buf->vb.queue,&vidq->queued);
767 buf->vb.state = STATE_QUEUED;
768 dprintk(2,"[%p/%d] buffer_queue - first queued\n",
774 static void buffer_release(struct videobuf_queue *vq, struct videobuf_buffer *vb)
776 struct vivi_buffer *buf = container_of(vb,struct vivi_buffer,vb);
777 struct vivi_fh *fh = vq->priv_data;
778 struct vivi_dev *dev = (struct vivi_dev*)fh->dev;
779 struct vivi_dmaqueue *vidq = &dev->vidq;
781 dprintk(1,"%s\n",__FUNCTION__);
783 vivi_stop_thread(vidq);
788 int vivi_map_sg (void *dev, struct scatterlist *sg, int nents,
793 dprintk(1,"%s, number of pages=%d\n",__FUNCTION__,nents);
794 BUG_ON(direction == DMA_NONE);
796 for (i = 0; i < nents; i++ ) {
799 sg[i].dma_address = page_to_phys(sg[i].page) + sg[i].offset;
805 int vivi_unmap_sg(void *dev,struct scatterlist *sglist,int nr_pages,
808 dprintk(1,"%s\n",__FUNCTION__);
812 int vivi_dma_sync_sg(void *dev,struct scatterlist *sglist,int nr_pages,
815 // dprintk(1,"%s\n",__FUNCTION__);
817 // flush_write_buffers();
821 static struct videobuf_queue_ops vivi_video_qops = {
822 .buf_setup = buffer_setup,
823 .buf_prepare = buffer_prepare,
824 .buf_queue = buffer_queue,
825 .buf_release = buffer_release,
827 /* Non-pci handling routines */
828 .vb_map_sg = vivi_map_sg,
829 .vb_dma_sync_sg = vivi_dma_sync_sg,
830 .vb_unmap_sg = vivi_unmap_sg,
833 /* ------------------------------------------------------------------
835 ------------------------------------------------------------------*/
837 static int vivi_try_fmt(struct vivi_dev *dev, struct vivi_fh *fh,
838 struct v4l2_format *f)
840 struct vivi_fmt *fmt;
841 enum v4l2_field field;
842 unsigned int maxw, maxh;
844 if (format.fourcc != f->fmt.pix.pixelformat) {
845 dprintk(1,"Fourcc format invalid.\n");
850 field = f->fmt.pix.field;
852 if (field == V4L2_FIELD_ANY) {
853 // field=V4L2_FIELD_INTERLACED;
854 field=V4L2_FIELD_SEQ_TB;
855 } else if (V4L2_FIELD_INTERLACED != field) {
856 dprintk(1,"Field type invalid.\n");
863 f->fmt.pix.field = field;
864 if (f->fmt.pix.height < 32)
865 f->fmt.pix.height = 32;
866 if (f->fmt.pix.height > maxh)
867 f->fmt.pix.height = maxh;
868 if (f->fmt.pix.width < 48)
869 f->fmt.pix.width = 48;
870 if (f->fmt.pix.width > maxw)
871 f->fmt.pix.width = maxw;
872 f->fmt.pix.width &= ~0x03;
873 f->fmt.pix.bytesperline =
874 (f->fmt.pix.width * fmt->depth) >> 3;
875 f->fmt.pix.sizeimage =
876 f->fmt.pix.height * f->fmt.pix.bytesperline;
881 static int res_get(struct vivi_dev *dev, struct vivi_fh *fh)
885 if (dev->resources) {
886 /* no, someone else uses it */
890 /* it's free, grab it */
892 dprintk(1,"res: get\n");
897 static inline int res_locked(struct vivi_dev *dev)
899 return (dev->resources);
902 static void res_free(struct vivi_dev *dev, struct vivi_fh *fh)
906 dprintk(1,"res: put\n");
910 static int vivi_do_ioctl(struct inode *inode, struct file *file, unsigned int cmd, void *arg)
912 struct vivi_fh *fh = file->private_data;
913 struct vivi_dev *dev = fh->dev;
917 if (_IOC_DIR(cmd) & _IOC_WRITE)
918 v4l_printk_ioctl_arg("vivi(w)",cmd, arg);
919 else if (!_IOC_DIR(cmd) & _IOC_READ) {
920 v4l_print_ioctl("vivi", cmd);
925 /* --- capabilities ------------------------------------------ */
926 case VIDIOC_QUERYCAP:
928 struct v4l2_capability *cap = (struct v4l2_capability*)arg;
930 memset(cap, 0, sizeof(*cap));
932 strcpy(cap->driver, "vivi");
933 strcpy(cap->card, "vivi");
934 cap->version = VIVI_VERSION;
936 V4L2_CAP_VIDEO_CAPTURE |
941 /* --- capture ioctls ---------------------------------------- */
942 case VIDIOC_ENUM_FMT:
944 struct v4l2_fmtdesc *f = arg;
945 enum v4l2_buf_type type;
951 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
957 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
962 memset(f,0,sizeof(*f));
966 strlcpy(f->description,format.name,sizeof(f->description));
967 f->pixelformat = format.fourcc;
976 struct v4l2_format *f = (struct v4l2_format *)arg;
978 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
983 memset(&f->fmt.pix,0,sizeof(f->fmt.pix));
984 f->fmt.pix.width = fh->width;
985 f->fmt.pix.height = fh->height;
986 f->fmt.pix.field = fh->vb_vidq.field;
987 f->fmt.pix.pixelformat = fh->fmt->fourcc;
988 f->fmt.pix.bytesperline =
989 (f->fmt.pix.width * fh->fmt->depth) >> 3;
990 f->fmt.pix.sizeimage =
991 f->fmt.pix.height * f->fmt.pix.bytesperline;
996 struct v4l2_format *f = arg;
998 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
999 dprintk(1,"Only capture supported.\n");
1004 ret = vivi_try_fmt(dev,fh,f);
1009 fh->width = f->fmt.pix.width;
1010 fh->height = f->fmt.pix.height;
1011 fh->vb_vidq.field = f->fmt.pix.field;
1016 case VIDIOC_TRY_FMT:
1018 struct v4l2_format *f = arg;
1019 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1024 ret=vivi_try_fmt(dev,fh,f);
1027 case VIDIOC_REQBUFS:
1028 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1032 ret=videobuf_reqbufs(&fh->vb_vidq, arg);
1034 case VIDIOC_QUERYBUF:
1035 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1039 ret=videobuf_querybuf(&fh->vb_vidq, arg);
1042 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1046 ret=videobuf_qbuf(&fh->vb_vidq, arg);
1049 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1053 ret=videobuf_dqbuf(&fh->vb_vidq, arg,
1054 file->f_flags & O_NONBLOCK);
1057 /* --- streaming capture ------------------------------------- */
1060 struct video_mbuf *mbuf = arg;
1061 struct videobuf_queue *q=&fh->vb_vidq;
1062 struct v4l2_requestbuffers req;
1065 memset(&req,0,sizeof(req));
1068 req.memory = V4L2_MEMORY_MMAP;
1069 ret = videobuf_reqbufs(q,&req);
1072 memset(mbuf,0,sizeof(*mbuf));
1073 mbuf->frames = req.count;
1075 for (i = 0; i < mbuf->frames; i++) {
1076 mbuf->offsets[i] = q->bufs[i]->boff;
1077 mbuf->size += q->bufs[i]->bsize;
1082 case VIDIOC_STREAMON:
1084 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1086 if (!res_get(dev,fh))
1088 ret=videobuf_streamon(&fh->vb_vidq);
1091 case VIDIOC_STREAMOFF:
1093 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1097 ret = videobuf_streamoff(&fh->vb_vidq);
1103 /* ---------- tv norms ---------- */
1104 case VIDIOC_ENUMSTD:
1106 struct v4l2_standard *e = arg;
1112 ret = v4l2_video_std_construct(e, V4L2_STD_NTSC_M, "NTSC-M");
1114 /* Allows vivi to use different fps from video std */
1115 e->frameperiod.numerator = WAKE_NUMERATOR;
1116 e->frameperiod.denominator = WAKE_DENOMINATOR;
1122 v4l2_std_id *id = arg;
1124 *id = V4L2_STD_NTSC_M;
1131 /* ------ input switching ---------- */
1132 case VIDIOC_ENUMINPUT:
1133 { /* only one input in this sample driver */
1134 struct v4l2_input *inp = arg;
1136 if (inp->index != 0) {
1140 memset(inp, 0, sizeof(*inp));
1143 inp->type = V4L2_INPUT_TYPE_CAMERA;
1144 inp->std = V4L2_STD_NTSC_M;
1145 strcpy(inp->name,"Camera");
1148 case VIDIOC_G_INPUT:
1150 unsigned int *i = arg;
1155 case VIDIOC_S_INPUT:
1157 unsigned int *i = arg;
1164 /* --- controls ---------------------------------------------- */
1165 case VIDIOC_QUERYCTRL:
1167 struct v4l2_queryctrl *qc = arg;
1170 for (i = 0; i < ARRAY_SIZE(vivi_qctrl); i++)
1171 if (qc->id && qc->id == vivi_qctrl[i].id) {
1172 memcpy(qc, &(vivi_qctrl[i]),
1182 struct v4l2_control *ctrl = arg;
1185 for (i = 0; i < ARRAY_SIZE(vivi_qctrl); i++)
1186 if (ctrl->id == vivi_qctrl[i].id) {
1187 ctrl->value=qctl_regs[i];
1196 struct v4l2_control *ctrl = arg;
1198 for (i = 0; i < ARRAY_SIZE(vivi_qctrl); i++)
1199 if (ctrl->id == vivi_qctrl[i].id) {
1201 vivi_qctrl[i].minimum
1203 vivi_qctrl[i].maximum) {
1207 qctl_regs[i]=ctrl->value;
1214 ret=v4l_compat_translate_ioctl(inode,file,cmd,arg,vivi_do_ioctl);
1219 v4l_print_ioctl("vivi(err)", cmd);
1220 dprintk(1,"errcode=%d\n",ret);
1221 } else if (_IOC_DIR(cmd) & _IOC_READ)
1222 v4l_printk_ioctl_arg("vivi(r)",cmd, arg);
1228 static int vivi_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
1230 return video_usercopy(inode, file, cmd, arg, vivi_do_ioctl);
1233 /* ------------------------------------------------------------------
1234 File operations for the device
1235 ------------------------------------------------------------------*/
1237 #define line_buf_size(norm) (norm_maxw(norm)*(format.depth+7)/8)
1239 static int vivi_open(struct inode *inode, struct file *file)
1241 int minor = iminor(inode);
1242 struct vivi_dev *h,*dev = NULL;
1244 struct list_head *list;
1245 enum v4l2_buf_type type = 0;
1248 printk(KERN_DEBUG "vivi: open called (minor=%d)\n",minor);
1250 list_for_each(list,&vivi_devlist) {
1251 h = list_entry(list, struct vivi_dev, vivi_devlist);
1252 if (h->video_dev.minor == minor) {
1254 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1261 /* If more than one user, mutex should be added */
1264 dprintk(1,"open minor=%d type=%s users=%d\n",
1265 minor,v4l2_type_names[type],dev->users);
1267 /* allocate + initialize per filehandle data */
1268 fh = kzalloc(sizeof(*fh),GFP_KERNEL);
1274 file->private_data = fh;
1276 fh->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1281 /* Put all controls at a sane state */
1282 for (i = 0; i < ARRAY_SIZE(vivi_qctrl); i++)
1283 qctl_regs[i] =vivi_qctrl[i].default_value;
1285 dprintk(1,"Open: fh=0x%08lx, dev=0x%08lx, dev->vidq=0x%08lx\n",
1286 (unsigned long)fh,(unsigned long)dev,(unsigned long)&dev->vidq);
1287 dprintk(1,"Open: list_empty queued=%d\n",list_empty(&dev->vidq.queued));
1288 dprintk(1,"Open: list_empty active=%d\n",list_empty(&dev->vidq.active));
1290 /* Resets frame counters */
1295 dev->jiffies=jiffies;
1296 sprintf(dev->timestr,"%02d:%02d:%02d:%03d",
1297 dev->h,dev->m,dev->s,(dev->us+500)/1000);
1299 videobuf_queue_init(&fh->vb_vidq, &vivi_video_qops,
1302 V4L2_FIELD_INTERLACED,
1303 sizeof(struct vivi_buffer),fh);
1309 vivi_read(struct file *file, char __user *data, size_t count, loff_t *ppos)
1311 struct vivi_fh *fh = file->private_data;
1313 if (fh->type==V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1314 if (res_locked(fh->dev))
1316 return videobuf_read_one(&fh->vb_vidq, data, count, ppos,
1317 file->f_flags & O_NONBLOCK);
1323 vivi_poll(struct file *file, struct poll_table_struct *wait)
1325 struct vivi_fh *fh = file->private_data;
1326 struct vivi_buffer *buf;
1328 dprintk(1,"%s\n",__FUNCTION__);
1330 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != fh->type)
1333 if (res_get(fh->dev,fh)) {
1334 dprintk(1,"poll: mmap interface\n");
1335 /* streaming capture */
1336 if (list_empty(&fh->vb_vidq.stream))
1338 buf = list_entry(fh->vb_vidq.stream.next,struct vivi_buffer,vb.stream);
1340 dprintk(1,"poll: read() interface\n");
1341 /* read() capture */
1342 buf = (struct vivi_buffer*)fh->vb_vidq.read_buf;
1346 poll_wait(file, &buf->vb.done, wait);
1347 if (buf->vb.state == STATE_DONE ||
1348 buf->vb.state == STATE_ERROR)
1349 return POLLIN|POLLRDNORM;
1353 static int vivi_release(struct inode *inode, struct file *file)
1355 struct vivi_fh *fh = file->private_data;
1356 struct vivi_dev *dev = fh->dev;
1357 struct vivi_dmaqueue *vidq = &dev->vidq;
1359 int minor = iminor(inode);
1361 vivi_stop_thread(vidq);
1362 videobuf_mmap_free(&fh->vb_vidq);
1368 printk(KERN_DEBUG "vivi: close called (minor=%d, users=%d)\n",minor,dev->users);
1374 vivi_mmap(struct file *file, struct vm_area_struct * vma)
1376 struct vivi_fh *fh = file->private_data;
1379 dprintk (1,"mmap called, vma=0x%08lx\n",(unsigned long)vma);
1381 ret=videobuf_mmap_mapper(&fh->vb_vidq, vma);
1383 dprintk (1,"vma start=0x%08lx, size=%ld, ret=%d\n",
1384 (unsigned long)vma->vm_start,
1385 (unsigned long)vma->vm_end-(unsigned long)vma->vm_start,
1391 static struct file_operations vivi_fops = {
1392 .owner = THIS_MODULE,
1394 .release = vivi_release,
1397 .ioctl = vivi_ioctl,
1399 .llseek = no_llseek,
1402 static struct video_device vivi = {
1403 .name = "VTM Virtual Video Capture Board",
1404 .type = VID_TYPE_CAPTURE,
1408 // .release = video_device_release,
1410 /* ------------------------------------------------------------------
1411 Initialization and module stuff
1412 ------------------------------------------------------------------*/
1414 static int __init vivi_init(void)
1417 struct vivi_dev *dev;
1419 dev = kzalloc(sizeof(*dev),GFP_KERNEL);
1422 list_add_tail(&dev->vivi_devlist,&vivi_devlist);
1424 /* init video dma queues */
1425 INIT_LIST_HEAD(&dev->vidq.active);
1426 INIT_LIST_HEAD(&dev->vidq.queued);
1428 /* initialize locks */
1429 init_MUTEX(&dev->lock);
1431 dev->vidq.timeout.function = vivi_vid_timeout;
1432 dev->vidq.timeout.data = (unsigned long)dev;
1433 init_timer(&dev->vidq.timeout);
1435 ret = video_register_device(&vivi, VFL_TYPE_GRABBER, video_nr);
1436 printk(KERN_INFO "Video Technology Magazine Virtual Video Capture Board (Load status: %d)\n", ret);
1440 static void __exit vivi_exit(void)
1443 struct list_head *list;
1445 list_for_each(list,&vivi_devlist) {
1446 h = list_entry(list, struct vivi_dev, vivi_devlist);
1449 video_unregister_device(&vivi);
1452 module_init(vivi_init);
1453 module_exit(vivi_exit);