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 MODULE_DESCRIPTION("Video Technology Magazine Virtual Video Capture Board");
52 MODULE_AUTHOR("Mauro Carvalho Chehab, Ted Walther and John Sokol");
53 MODULE_LICENSE("Dual BSD/GPL");
55 #define VIVI_MAJOR_VERSION 0
56 #define VIVI_MINOR_VERSION 4
57 #define VIVI_RELEASE 0
58 #define VIVI_VERSION KERNEL_VERSION(VIVI_MAJOR_VERSION, VIVI_MINOR_VERSION, VIVI_RELEASE)
60 static int video_nr = -1; /* /dev/videoN, -1 for autodetect */
61 module_param(video_nr, int, 0);
64 module_param(debug, int, 0);
66 static unsigned int vid_limit = 16;
67 module_param(vid_limit,int,0644);
68 MODULE_PARM_DESC(vid_limit,"capture memory limit in megabytes");
70 /* supported controls */
71 static struct v4l2_queryctrl vivi_qctrl[] = {
73 .id = V4L2_CID_AUDIO_VOLUME,
78 .default_value = 65535,
80 .type = V4L2_CTRL_TYPE_INTEGER,
82 .id = V4L2_CID_BRIGHTNESS,
83 .type = V4L2_CTRL_TYPE_INTEGER,
91 .id = V4L2_CID_CONTRAST,
92 .type = V4L2_CTRL_TYPE_INTEGER,
97 .default_value = 0x10,
100 .id = V4L2_CID_SATURATION,
101 .type = V4L2_CTRL_TYPE_INTEGER,
102 .name = "Saturation",
106 .default_value = 127,
110 .type = V4L2_CTRL_TYPE_INTEGER,
120 static int qctl_regs[ARRAY_SIZE(vivi_qctrl)];
122 #define dprintk(level,fmt, arg...) \
124 if (debug >= (level)) \
125 printk(KERN_DEBUG "vivi: " fmt , ## arg); \
128 /* ------------------------------------------------------------------
130 ------------------------------------------------------------------*/
134 u32 fourcc; /* v4l2 format id */
138 static struct vivi_fmt format = {
139 .name = "4:2:2, packed, YUYV",
140 .fourcc = V4L2_PIX_FMT_YUYV,
146 struct scatterlist *sg;
149 /* buffer for one video frame */
151 /* common v4l buffer stuff -- must be first */
152 struct videobuf_buffer vb;
154 struct vivi_fmt *fmt;
156 struct sg_to_addr *to_addr;
159 struct vivi_dmaqueue {
160 struct list_head active;
161 struct list_head queued;
162 struct timer_list timeout;
164 /* thread for generating video stream*/
165 struct task_struct *kthread;
166 wait_queue_head_t wq;
167 /* Counters to control fps rate */
172 static LIST_HEAD(vivi_devlist);
175 struct list_head vivi_devlist;
177 struct semaphore lock;
181 /* various device info */
182 unsigned int resources;
183 struct video_device video_dev;
185 struct vivi_dmaqueue vidq;
187 /* Several counters */
188 int h,m,s,us,jiffies;
193 struct vivi_dev *dev;
196 struct vivi_fmt *fmt;
197 unsigned int width,height;
198 struct videobuf_queue vb_vidq;
200 enum v4l2_buf_type type;
203 /* ------------------------------------------------------------------
204 DMA and thread functions
205 ------------------------------------------------------------------*/
207 /* Bars and Colors should match positions */
219 static u8 bars[8][3] = {
221 {204,204,204}, /* white */
222 {208,208, 0}, /* ambar */
223 { 0,206,206}, /* cyan */
224 { 0,239, 0}, /* green */
225 {239, 0,239}, /* magenta */
226 {205, 0, 0}, /* red */
227 { 0, 0,255}, /* blue */
231 #define TO_Y(r,g,b) (((16829*r +33039*g +6416*b + 32768)>>16)+16)
232 /* RGB to V(Cr) Color transform */
233 #define TO_V(r,g,b) (((28784*r -24103*g -4681*b + 32768)>>16)+128)
234 /* RGB to U(Cb) Color transform */
235 #define TO_U(r,g,b) (((-9714*r -19070*g +28784*b + 32768)>>16)+128)
237 #define TSTAMP_MIN_Y 24
238 #define TSTAMP_MAX_Y TSTAMP_MIN_Y+15
239 #define TSTAMP_MIN_X 64
241 static void prep_to_addr(struct sg_to_addr to_addr[],
242 struct videobuf_buffer *vb)
246 for (i=0;i<vb->dma.nr_pages;i++) {
247 to_addr[i].sg=&vb->dma.sglist[i];
249 pos += vb->dma.sglist[i].length;
253 static int get_addr_pos(int pos, int pages, struct sg_to_addr to_addr[])
255 int p1=0,p2=pages-1,p3=pages/2;
258 BUG_ON (pos>=to_addr[p2].pos+to_addr[p2].sg->length);
261 if (pos < to_addr[p3].pos) {
268 if (pos >= to_addr[p2].pos)
274 static void gen_line(struct sg_to_addr to_addr[],int inipos,int pages,int wmax,
275 int hmax, int line, char *timestr)
277 int w,i,j,pos=inipos,pgpos,oldpg,y;
282 /* Get first addr pointed to pixel position */
283 oldpg=get_addr_pos(pos,pages,to_addr);
284 pg=pfn_to_page(to_addr[oldpg].sg->dma_address >> PAGE_SHIFT);
285 basep = kmap_atomic(pg, KM_BOUNCE_READ)+to_addr[oldpg].sg->offset;
287 /* We will just duplicate the second pixel at the packet */
290 /* Generate a standard color bar pattern */
291 for (w=0;w<wmax;w++) {
296 for (color=0;color<4;color++) {
297 pgpos=get_addr_pos(pos,pages,to_addr);
299 pg=pfn_to_page(to_addr[pgpos].sg->dma_address >> PAGE_SHIFT);
300 kunmap_atomic(basep, KM_BOUNCE_READ);
301 basep= kmap_atomic(pg, KM_BOUNCE_READ)+to_addr[pgpos].sg->offset;
304 p=basep+pos-to_addr[pgpos].pos;
309 *p=TO_Y(r,g,b); /* Luminance */
312 *p=TO_U(r,g,b); /* Cb */
315 *p=TO_V(r,g,b); /* Cr */
322 /* Checks if it is possible to show timestamp */
323 if (TSTAMP_MAX_Y>=hmax)
325 if (TSTAMP_MIN_X+strlen(timestr)>=wmax)
328 /* Print stream time */
329 if (line>=TSTAMP_MIN_Y && line<=TSTAMP_MAX_Y) {
331 for (s=timestr;*s;s++) {
332 chr=rom8x16_bits[(*s-0x30)*16+line-TSTAMP_MIN_Y];
334 if (chr&1<<(7-i)) { /* Font color*/
340 } else { /* Background color */
348 for (color=0;color<4;color++) {
349 pgpos=get_addr_pos(pos,pages,to_addr);
351 pg=pfn_to_page(to_addr[pgpos].
356 basep= kmap_atomic(pg,
358 to_addr[pgpos].sg->offset;
361 p=basep+pos-to_addr[pgpos].pos;
368 *p=TO_Y(r,g,b); /* Luminance */
371 *p=TO_U(r,g,b); /* Cb */
374 *p=TO_V(r,g,b); /* Cr */
386 kunmap_atomic(basep, KM_BOUNCE_READ);
388 static void vivi_fillbuff(struct vivi_dev *dev,struct vivi_buffer *buf)
391 int hmax = buf->vb.height;
392 int wmax = buf->vb.width;
393 struct videobuf_buffer *vb=&buf->vb;
394 struct sg_to_addr *to_addr=buf->to_addr;
397 /* Test if DMA mapping is ready */
398 if (!vb->dma.sglist[0].dma_address)
401 prep_to_addr(to_addr,vb);
403 /* Check if there is enough memory */
404 BUG_ON(buf->vb.dma.nr_pages << PAGE_SHIFT < (buf->vb.width*buf->vb.height)*2);
406 for (h=0;h<hmax;h++) {
407 gen_line(to_addr,pos,vb->dma.nr_pages,wmax,hmax,h,dev->timestr);
411 /* Updates stream time */
413 dev->us+=jiffies_to_usecs(jiffies-dev->jiffies);
414 dev->jiffies=jiffies;
415 if (dev->us>=1000000) {
429 sprintf(dev->timestr,"%02d:%02d:%02d:%03d",
430 dev->h,dev->m,dev->s,(dev->us+500)/1000);
432 dprintk(2,"vivifill at %s: Buffer 0x%08lx size= %d\n",dev->timestr,
433 (unsigned long)buf->vb.dma.vmalloc,pos);
435 /* Advice that buffer was filled */
436 buf->vb.state = STATE_DONE;
437 buf->vb.field_count++;
438 do_gettimeofday(&ts);
441 list_del(&buf->vb.queue);
442 wake_up(&buf->vb.done);
445 static int restart_video_queue(struct vivi_dmaqueue *dma_q);
447 static void vivi_thread_tick(struct vivi_dmaqueue *dma_q)
449 struct vivi_buffer *buf;
450 struct vivi_dev *dev= container_of(dma_q,struct vivi_dev,vidq);
454 /* Announces videobuf that all went ok */
455 for (bc = 0;; bc++) {
456 if (list_empty(&dma_q->active)) {
457 dprintk(1,"No active queue to serve\n");
461 buf = list_entry(dma_q->active.next,
462 struct vivi_buffer, vb.queue);
464 /* Nobody is waiting something to be done, just return */
465 if (!waitqueue_active(&buf->vb.done)) {
466 mod_timer(&dma_q->timeout, jiffies+BUFFER_TIMEOUT);
470 do_gettimeofday(&buf->vb.ts);
471 dprintk(2,"[%p/%d] wakeup\n",buf,buf->vb.i);
474 vivi_fillbuff(dev,buf);
476 if (list_empty(&dma_q->active)) {
477 del_timer(&dma_q->timeout);
479 mod_timer(&dma_q->timeout, jiffies+BUFFER_TIMEOUT);
482 dprintk(1,"%s: %d buffers handled (should be 1)\n",__FUNCTION__,bc);
485 static void vivi_sleep(struct vivi_dmaqueue *dma_q)
488 DECLARE_WAITQUEUE(wait, current);
490 dprintk(1,"%s dma_q=0x%08lx\n",__FUNCTION__,(unsigned long)dma_q);
492 add_wait_queue(&dma_q->wq, &wait);
493 if (!kthread_should_stop()) {
496 /* Calculate time to wake up */
497 timeout=dma_q->ini_jiffies+msecs_to_jiffies((dma_q->frame*WAKE_NUMERATOR*1000)/WAKE_DENOMINATOR)-jiffies;
500 int old=dma_q->frame;
501 dma_q->frame=(jiffies_to_msecs(jiffies-dma_q->ini_jiffies)*WAKE_DENOMINATOR)/(WAKE_NUMERATOR*1000)+1;
503 timeout=dma_q->ini_jiffies+msecs_to_jiffies((dma_q->frame*WAKE_NUMERATOR*1000)/WAKE_DENOMINATOR)-jiffies;
505 dprintk(1,"underrun, losed %d frames. "
506 "Now, frame is %d. Waking on %d jiffies\n",
507 dma_q->frame-old,dma_q->frame,timeout);
509 dprintk(1,"will sleep for %i jiffies\n",timeout);
511 vivi_thread_tick(dma_q);
513 schedule_timeout_interruptible (timeout);
516 remove_wait_queue(&dma_q->wq, &wait);
520 static int vivi_thread(void *data)
522 struct vivi_dmaqueue *dma_q=data;
524 dprintk(1,"thread started\n");
529 if (kthread_should_stop())
532 dprintk(1, "thread: exit\n");
536 static int vivi_start_thread(struct vivi_dmaqueue *dma_q)
539 dma_q->ini_jiffies=jiffies;
541 dprintk(1,"%s\n",__FUNCTION__);
542 init_waitqueue_head(&dma_q->wq);
544 dma_q->kthread = kthread_run(vivi_thread, dma_q, "vivi");
546 if (dma_q->kthread == NULL) {
547 printk(KERN_ERR "vivi: kernel_thread() failed\n");
550 dprintk(1,"returning from %s\n",__FUNCTION__);
554 static void vivi_stop_thread(struct vivi_dmaqueue *dma_q)
556 dprintk(1,"%s\n",__FUNCTION__);
557 /* shutdown control thread */
558 if (dma_q->kthread) {
559 kthread_stop(dma_q->kthread);
564 static int restart_video_queue(struct vivi_dmaqueue *dma_q)
566 struct vivi_buffer *buf, *prev;
567 struct list_head *item;
569 dprintk(1,"%s dma_q=0x%08lx\n",__FUNCTION__,(unsigned long)dma_q);
571 if (!list_empty(&dma_q->active)) {
572 buf = list_entry(dma_q->active.next, struct vivi_buffer, vb.queue);
573 dprintk(2,"restart_queue [%p/%d]: restart dma\n",
576 dprintk(1,"Restarting video dma\n");
577 vivi_stop_thread(dma_q);
578 // vivi_start_thread(dma_q);
580 /* cancel all outstanding capture / vbi requests */
581 list_for_each(item,&dma_q->active) {
582 buf = list_entry(item, struct vivi_buffer, vb.queue);
584 list_del(&buf->vb.queue);
585 buf->vb.state = STATE_ERROR;
586 wake_up(&buf->vb.done);
588 mod_timer(&dma_q->timeout, jiffies+BUFFER_TIMEOUT);
595 if (list_empty(&dma_q->queued))
597 buf = list_entry(dma_q->queued.next, struct vivi_buffer, vb.queue);
599 list_del(&buf->vb.queue);
600 list_add_tail(&buf->vb.queue,&dma_q->active);
602 dprintk(1,"Restarting video dma\n");
603 vivi_stop_thread(dma_q);
604 vivi_start_thread(dma_q);
606 buf->vb.state = STATE_ACTIVE;
607 mod_timer(&dma_q->timeout, jiffies+BUFFER_TIMEOUT);
608 dprintk(2,"[%p/%d] restart_queue - first active\n",
611 } else if (prev->vb.width == buf->vb.width &&
612 prev->vb.height == buf->vb.height &&
613 prev->fmt == buf->fmt) {
614 list_del(&buf->vb.queue);
615 list_add_tail(&buf->vb.queue,&dma_q->active);
616 buf->vb.state = STATE_ACTIVE;
617 dprintk(2,"[%p/%d] restart_queue - move to active\n",
626 static void vivi_vid_timeout(unsigned long data)
628 struct vivi_dev *dev = (struct vivi_dev*)data;
629 struct vivi_dmaqueue *vidq = &dev->vidq;
630 struct vivi_buffer *buf;
632 while (!list_empty(&vidq->active)) {
633 buf = list_entry(vidq->active.next, struct vivi_buffer, vb.queue);
634 list_del(&buf->vb.queue);
635 buf->vb.state = STATE_ERROR;
636 wake_up(&buf->vb.done);
637 printk("vivi/0: [%p/%d] timeout\n", buf, buf->vb.i);
640 restart_video_queue(vidq);
643 /* ------------------------------------------------------------------
645 ------------------------------------------------------------------*/
647 buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size)
649 struct vivi_fh *fh = vq->priv_data;
651 *size = fh->width*fh->height*2;
655 while (*size * *count > vid_limit * 1024 * 1024)
660 static void free_buffer(struct videobuf_queue *vq, struct vivi_buffer *buf)
662 dprintk(1,"%s\n",__FUNCTION__);
667 /*FIXME: Maybe a spinlock is required here */
671 videobuf_waiton(&buf->vb,0,0);
672 videobuf_dma_unmap(vq, &buf->vb.dma);
673 videobuf_dma_free(&buf->vb.dma);
674 buf->vb.state = STATE_NEEDS_INIT;
677 #define norm_maxw() 1024
678 #define norm_maxh() 768
680 buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
681 enum v4l2_field field)
683 struct vivi_fh *fh = vq->priv_data;
684 struct vivi_buffer *buf = container_of(vb,struct vivi_buffer,vb);
685 int rc, init_buffer = 0;
687 // dprintk(1,"%s, field=%d\n",__FUNCTION__,field);
689 BUG_ON(NULL == fh->fmt);
690 if (fh->width < 48 || fh->width > norm_maxw() ||
691 fh->height < 32 || fh->height > norm_maxh())
693 buf->vb.size = fh->width*fh->height*2;
694 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
697 if (buf->fmt != fh->fmt ||
698 buf->vb.width != fh->width ||
699 buf->vb.height != fh->height ||
700 buf->vb.field != field) {
702 buf->vb.width = fh->width;
703 buf->vb.height = fh->height;
704 buf->vb.field = field;
708 if (STATE_NEEDS_INIT == buf->vb.state) {
709 if (0 != (rc = videobuf_iolock(vq,&buf->vb,NULL)))
713 buf->vb.state = STATE_PREPARED;
715 if (NULL == (buf->to_addr = kmalloc(sizeof(*buf->to_addr) * vb->dma.nr_pages,GFP_KERNEL))) {
728 buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
730 struct vivi_buffer *buf = container_of(vb,struct vivi_buffer,vb);
731 struct vivi_fh *fh = vq->priv_data;
732 struct vivi_dev *dev = fh->dev;
733 struct vivi_dmaqueue *vidq = &dev->vidq;
734 struct vivi_buffer *prev;
736 if (!list_empty(&vidq->queued)) {
737 dprintk(1,"adding vb queue=0x%08lx\n",(unsigned long)&buf->vb.queue);
738 list_add_tail(&buf->vb.queue,&vidq->queued);
739 buf->vb.state = STATE_QUEUED;
740 dprintk(2,"[%p/%d] buffer_queue - append to queued\n",
742 } else if (list_empty(&vidq->active)) {
743 list_add_tail(&buf->vb.queue,&vidq->active);
745 buf->vb.state = STATE_ACTIVE;
746 mod_timer(&vidq->timeout, jiffies+BUFFER_TIMEOUT);
747 dprintk(2,"[%p/%d] buffer_queue - first active\n",
750 vivi_start_thread(vidq);
752 prev = list_entry(vidq->active.prev, struct vivi_buffer, vb.queue);
753 if (prev->vb.width == buf->vb.width &&
754 prev->vb.height == buf->vb.height &&
755 prev->fmt == buf->fmt) {
756 list_add_tail(&buf->vb.queue,&vidq->active);
757 buf->vb.state = STATE_ACTIVE;
758 dprintk(2,"[%p/%d] buffer_queue - append to active\n",
762 list_add_tail(&buf->vb.queue,&vidq->queued);
763 buf->vb.state = STATE_QUEUED;
764 dprintk(2,"[%p/%d] buffer_queue - first queued\n",
770 static void buffer_release(struct videobuf_queue *vq, struct videobuf_buffer *vb)
772 struct vivi_buffer *buf = container_of(vb,struct vivi_buffer,vb);
773 struct vivi_fh *fh = vq->priv_data;
774 struct vivi_dev *dev = (struct vivi_dev*)fh->dev;
775 struct vivi_dmaqueue *vidq = &dev->vidq;
777 dprintk(1,"%s\n",__FUNCTION__);
779 vivi_stop_thread(vidq);
784 static int vivi_map_sg(void *dev, struct scatterlist *sg, int nents,
789 dprintk(1,"%s, number of pages=%d\n",__FUNCTION__,nents);
790 BUG_ON(direction == DMA_NONE);
792 for (i = 0; i < nents; i++ ) {
795 sg[i].dma_address = page_to_phys(sg[i].page) + sg[i].offset;
801 static int vivi_unmap_sg(void *dev,struct scatterlist *sglist,int nr_pages,
804 dprintk(1,"%s\n",__FUNCTION__);
808 static int vivi_dma_sync_sg(void *dev,struct scatterlist *sglist, int nr_pages,
811 // dprintk(1,"%s\n",__FUNCTION__);
813 // flush_write_buffers();
817 static struct videobuf_queue_ops vivi_video_qops = {
818 .buf_setup = buffer_setup,
819 .buf_prepare = buffer_prepare,
820 .buf_queue = buffer_queue,
821 .buf_release = buffer_release,
823 /* Non-pci handling routines */
824 .vb_map_sg = vivi_map_sg,
825 .vb_dma_sync_sg = vivi_dma_sync_sg,
826 .vb_unmap_sg = vivi_unmap_sg,
829 /* ------------------------------------------------------------------
831 ------------------------------------------------------------------*/
833 static int vivi_try_fmt(struct vivi_dev *dev, struct vivi_fh *fh,
834 struct v4l2_format *f)
836 struct vivi_fmt *fmt;
837 enum v4l2_field field;
838 unsigned int maxw, maxh;
840 if (format.fourcc != f->fmt.pix.pixelformat) {
841 dprintk(1,"Fourcc format invalid.\n");
846 field = f->fmt.pix.field;
848 if (field == V4L2_FIELD_ANY) {
849 // field=V4L2_FIELD_INTERLACED;
850 field=V4L2_FIELD_SEQ_TB;
851 } else if (V4L2_FIELD_INTERLACED != field) {
852 dprintk(1,"Field type invalid.\n");
859 f->fmt.pix.field = field;
860 if (f->fmt.pix.height < 32)
861 f->fmt.pix.height = 32;
862 if (f->fmt.pix.height > maxh)
863 f->fmt.pix.height = maxh;
864 if (f->fmt.pix.width < 48)
865 f->fmt.pix.width = 48;
866 if (f->fmt.pix.width > maxw)
867 f->fmt.pix.width = maxw;
868 f->fmt.pix.width &= ~0x03;
869 f->fmt.pix.bytesperline =
870 (f->fmt.pix.width * fmt->depth) >> 3;
871 f->fmt.pix.sizeimage =
872 f->fmt.pix.height * f->fmt.pix.bytesperline;
877 static int res_get(struct vivi_dev *dev, struct vivi_fh *fh)
881 if (dev->resources) {
882 /* no, someone else uses it */
886 /* it's free, grab it */
888 dprintk(1,"res: get\n");
893 static int res_locked(struct vivi_dev *dev)
895 return (dev->resources);
898 static void res_free(struct vivi_dev *dev, struct vivi_fh *fh)
902 dprintk(1,"res: put\n");
906 static int vivi_do_ioctl(struct inode *inode, struct file *file, unsigned int cmd, void *arg)
908 struct vivi_fh *fh = file->private_data;
909 struct vivi_dev *dev = fh->dev;
913 if (_IOC_DIR(cmd) & _IOC_WRITE)
914 v4l_printk_ioctl_arg("vivi(w)",cmd, arg);
915 else if (!_IOC_DIR(cmd) & _IOC_READ) {
916 v4l_print_ioctl("vivi", cmd);
921 /* --- capabilities ------------------------------------------ */
922 case VIDIOC_QUERYCAP:
924 struct v4l2_capability *cap = (struct v4l2_capability*)arg;
926 memset(cap, 0, sizeof(*cap));
928 strcpy(cap->driver, "vivi");
929 strcpy(cap->card, "vivi");
930 cap->version = VIVI_VERSION;
932 V4L2_CAP_VIDEO_CAPTURE |
937 /* --- capture ioctls ---------------------------------------- */
938 case VIDIOC_ENUM_FMT:
940 struct v4l2_fmtdesc *f = arg;
941 enum v4l2_buf_type type;
947 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
953 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
958 memset(f,0,sizeof(*f));
962 strlcpy(f->description,format.name,sizeof(f->description));
963 f->pixelformat = format.fourcc;
972 struct v4l2_format *f = (struct v4l2_format *)arg;
974 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
979 memset(&f->fmt.pix,0,sizeof(f->fmt.pix));
980 f->fmt.pix.width = fh->width;
981 f->fmt.pix.height = fh->height;
982 f->fmt.pix.field = fh->vb_vidq.field;
983 f->fmt.pix.pixelformat = fh->fmt->fourcc;
984 f->fmt.pix.bytesperline =
985 (f->fmt.pix.width * fh->fmt->depth) >> 3;
986 f->fmt.pix.sizeimage =
987 f->fmt.pix.height * f->fmt.pix.bytesperline;
992 struct v4l2_format *f = arg;
994 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
995 dprintk(1,"Only capture supported.\n");
1000 ret = vivi_try_fmt(dev,fh,f);
1005 fh->width = f->fmt.pix.width;
1006 fh->height = f->fmt.pix.height;
1007 fh->vb_vidq.field = f->fmt.pix.field;
1012 case VIDIOC_TRY_FMT:
1014 struct v4l2_format *f = arg;
1015 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1020 ret=vivi_try_fmt(dev,fh,f);
1023 case VIDIOC_REQBUFS:
1024 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1028 ret=videobuf_reqbufs(&fh->vb_vidq, arg);
1030 case VIDIOC_QUERYBUF:
1031 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1035 ret=videobuf_querybuf(&fh->vb_vidq, arg);
1038 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1042 ret=videobuf_qbuf(&fh->vb_vidq, arg);
1045 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1049 ret=videobuf_dqbuf(&fh->vb_vidq, arg,
1050 file->f_flags & O_NONBLOCK);
1053 /* --- streaming capture ------------------------------------- */
1056 struct video_mbuf *mbuf = arg;
1057 struct videobuf_queue *q=&fh->vb_vidq;
1058 struct v4l2_requestbuffers req;
1061 memset(&req,0,sizeof(req));
1064 req.memory = V4L2_MEMORY_MMAP;
1065 ret = videobuf_reqbufs(q,&req);
1068 memset(mbuf,0,sizeof(*mbuf));
1069 mbuf->frames = req.count;
1071 for (i = 0; i < mbuf->frames; i++) {
1072 mbuf->offsets[i] = q->bufs[i]->boff;
1073 mbuf->size += q->bufs[i]->bsize;
1078 case VIDIOC_STREAMON:
1080 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1082 if (!res_get(dev,fh))
1084 ret=videobuf_streamon(&fh->vb_vidq);
1087 case VIDIOC_STREAMOFF:
1089 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1093 ret = videobuf_streamoff(&fh->vb_vidq);
1099 /* ---------- tv norms ---------- */
1100 case VIDIOC_ENUMSTD:
1102 struct v4l2_standard *e = arg;
1108 ret = v4l2_video_std_construct(e, V4L2_STD_NTSC_M, "NTSC-M");
1110 /* Allows vivi to use different fps from video std */
1111 e->frameperiod.numerator = WAKE_NUMERATOR;
1112 e->frameperiod.denominator = WAKE_DENOMINATOR;
1118 v4l2_std_id *id = arg;
1120 *id = V4L2_STD_NTSC_M;
1127 /* ------ input switching ---------- */
1128 case VIDIOC_ENUMINPUT:
1129 { /* only one input in this sample driver */
1130 struct v4l2_input *inp = arg;
1132 if (inp->index != 0) {
1136 memset(inp, 0, sizeof(*inp));
1139 inp->type = V4L2_INPUT_TYPE_CAMERA;
1140 inp->std = V4L2_STD_NTSC_M;
1141 strcpy(inp->name,"Camera");
1144 case VIDIOC_G_INPUT:
1146 unsigned int *i = arg;
1151 case VIDIOC_S_INPUT:
1153 unsigned int *i = arg;
1160 /* --- controls ---------------------------------------------- */
1161 case VIDIOC_QUERYCTRL:
1163 struct v4l2_queryctrl *qc = arg;
1166 for (i = 0; i < ARRAY_SIZE(vivi_qctrl); i++)
1167 if (qc->id && qc->id == vivi_qctrl[i].id) {
1168 memcpy(qc, &(vivi_qctrl[i]),
1178 struct v4l2_control *ctrl = arg;
1181 for (i = 0; i < ARRAY_SIZE(vivi_qctrl); i++)
1182 if (ctrl->id == vivi_qctrl[i].id) {
1183 ctrl->value=qctl_regs[i];
1192 struct v4l2_control *ctrl = arg;
1194 for (i = 0; i < ARRAY_SIZE(vivi_qctrl); i++)
1195 if (ctrl->id == vivi_qctrl[i].id) {
1197 vivi_qctrl[i].minimum
1199 vivi_qctrl[i].maximum) {
1203 qctl_regs[i]=ctrl->value;
1210 ret=v4l_compat_translate_ioctl(inode,file,cmd,arg,vivi_do_ioctl);
1215 v4l_print_ioctl("vivi(err)", cmd);
1216 dprintk(1,"errcode=%d\n",ret);
1217 } else if (_IOC_DIR(cmd) & _IOC_READ)
1218 v4l_printk_ioctl_arg("vivi(r)",cmd, arg);
1224 static int vivi_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
1226 return video_usercopy(inode, file, cmd, arg, vivi_do_ioctl);
1229 /* ------------------------------------------------------------------
1230 File operations for the device
1231 ------------------------------------------------------------------*/
1233 #define line_buf_size(norm) (norm_maxw(norm)*(format.depth+7)/8)
1235 static int vivi_open(struct inode *inode, struct file *file)
1237 int minor = iminor(inode);
1238 struct vivi_dev *h,*dev = NULL;
1240 struct list_head *list;
1241 enum v4l2_buf_type type = 0;
1244 printk(KERN_DEBUG "vivi: open called (minor=%d)\n",minor);
1246 list_for_each(list,&vivi_devlist) {
1247 h = list_entry(list, struct vivi_dev, vivi_devlist);
1248 if (h->video_dev.minor == minor) {
1250 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1257 /* If more than one user, mutex should be added */
1260 dprintk(1,"open minor=%d type=%s users=%d\n",
1261 minor,v4l2_type_names[type],dev->users);
1263 /* allocate + initialize per filehandle data */
1264 fh = kzalloc(sizeof(*fh),GFP_KERNEL);
1270 file->private_data = fh;
1272 fh->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1277 /* Put all controls at a sane state */
1278 for (i = 0; i < ARRAY_SIZE(vivi_qctrl); i++)
1279 qctl_regs[i] =vivi_qctrl[i].default_value;
1281 dprintk(1,"Open: fh=0x%08lx, dev=0x%08lx, dev->vidq=0x%08lx\n",
1282 (unsigned long)fh,(unsigned long)dev,(unsigned long)&dev->vidq);
1283 dprintk(1,"Open: list_empty queued=%d\n",list_empty(&dev->vidq.queued));
1284 dprintk(1,"Open: list_empty active=%d\n",list_empty(&dev->vidq.active));
1286 /* Resets frame counters */
1291 dev->jiffies=jiffies;
1292 sprintf(dev->timestr,"%02d:%02d:%02d:%03d",
1293 dev->h,dev->m,dev->s,(dev->us+500)/1000);
1295 videobuf_queue_init(&fh->vb_vidq, &vivi_video_qops,
1298 V4L2_FIELD_INTERLACED,
1299 sizeof(struct vivi_buffer),fh);
1305 vivi_read(struct file *file, char __user *data, size_t count, loff_t *ppos)
1307 struct vivi_fh *fh = file->private_data;
1309 if (fh->type==V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1310 if (res_locked(fh->dev))
1312 return videobuf_read_one(&fh->vb_vidq, data, count, ppos,
1313 file->f_flags & O_NONBLOCK);
1319 vivi_poll(struct file *file, struct poll_table_struct *wait)
1321 struct vivi_fh *fh = file->private_data;
1322 struct vivi_buffer *buf;
1324 dprintk(1,"%s\n",__FUNCTION__);
1326 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != fh->type)
1329 if (res_get(fh->dev,fh)) {
1330 dprintk(1,"poll: mmap interface\n");
1331 /* streaming capture */
1332 if (list_empty(&fh->vb_vidq.stream))
1334 buf = list_entry(fh->vb_vidq.stream.next,struct vivi_buffer,vb.stream);
1336 dprintk(1,"poll: read() interface\n");
1337 /* read() capture */
1338 buf = (struct vivi_buffer*)fh->vb_vidq.read_buf;
1342 poll_wait(file, &buf->vb.done, wait);
1343 if (buf->vb.state == STATE_DONE ||
1344 buf->vb.state == STATE_ERROR)
1345 return POLLIN|POLLRDNORM;
1349 static int vivi_release(struct inode *inode, struct file *file)
1351 struct vivi_fh *fh = file->private_data;
1352 struct vivi_dev *dev = fh->dev;
1353 struct vivi_dmaqueue *vidq = &dev->vidq;
1355 int minor = iminor(inode);
1357 vivi_stop_thread(vidq);
1358 videobuf_mmap_free(&fh->vb_vidq);
1364 printk(KERN_DEBUG "vivi: close called (minor=%d, users=%d)\n",minor,dev->users);
1370 vivi_mmap(struct file *file, struct vm_area_struct * vma)
1372 struct vivi_fh *fh = file->private_data;
1375 dprintk (1,"mmap called, vma=0x%08lx\n",(unsigned long)vma);
1377 ret=videobuf_mmap_mapper(&fh->vb_vidq, vma);
1379 dprintk (1,"vma start=0x%08lx, size=%ld, ret=%d\n",
1380 (unsigned long)vma->vm_start,
1381 (unsigned long)vma->vm_end-(unsigned long)vma->vm_start,
1387 static struct file_operations vivi_fops = {
1388 .owner = THIS_MODULE,
1390 .release = vivi_release,
1393 .ioctl = vivi_ioctl,
1395 .llseek = no_llseek,
1398 static struct video_device vivi = {
1399 .name = "VTM Virtual Video Capture Board",
1400 .type = VID_TYPE_CAPTURE,
1404 // .release = video_device_release,
1406 /* ------------------------------------------------------------------
1407 Initialization and module stuff
1408 ------------------------------------------------------------------*/
1410 static int __init vivi_init(void)
1413 struct vivi_dev *dev;
1415 dev = kzalloc(sizeof(*dev),GFP_KERNEL);
1418 list_add_tail(&dev->vivi_devlist,&vivi_devlist);
1420 /* init video dma queues */
1421 INIT_LIST_HEAD(&dev->vidq.active);
1422 INIT_LIST_HEAD(&dev->vidq.queued);
1424 /* initialize locks */
1425 init_MUTEX(&dev->lock);
1427 dev->vidq.timeout.function = vivi_vid_timeout;
1428 dev->vidq.timeout.data = (unsigned long)dev;
1429 init_timer(&dev->vidq.timeout);
1431 ret = video_register_device(&vivi, VFL_TYPE_GRABBER, video_nr);
1432 printk(KERN_INFO "Video Technology Magazine Virtual Video Capture Board (Load status: %d)\n", ret);
1436 static void __exit vivi_exit(void)
1439 struct list_head *list;
1441 list_for_each(list,&vivi_devlist) {
1442 h = list_entry(list, struct vivi_dev, vivi_devlist);
1445 video_unregister_device(&vivi);
1448 module_init(vivi_init);
1449 module_exit(vivi_exit);