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 #ifdef CONFIG_VIDEO_V4L1_COMPAT
30 /* Include V4L1 specific functions. Should be removed soon */
31 #include <linux/videodev.h>
33 #include <linux/interrupt.h>
34 #include <media/video-buf.h>
35 #include <media/v4l2-common.h>
36 #include <linux/kthread.h>
37 #include <linux/highmem.h>
39 /* Wake up at about 30 fps */
40 #define WAKE_NUMERATOR 30
41 #define WAKE_DENOMINATOR 1001
42 #define BUFFER_TIMEOUT msecs_to_jiffies(500) /* 0.5 seconds */
44 /* These timers are for 1 fps - used only for testing */
45 //#define WAKE_DENOMINATOR 30 /* hack for testing purposes */
46 //#define BUFFER_TIMEOUT msecs_to_jiffies(5000) /* 5 seconds */
51 #define kzalloc(size, flags) \
53 void *__ret = kmalloc(size, flags); \
55 memset(__ret, 0, size); \
60 MODULE_DESCRIPTION("Video Technology Magazine Virtual Video Capture Board");
61 MODULE_AUTHOR("Mauro Carvalho Chehab, Ted Walther and John Sokol");
62 MODULE_LICENSE("Dual BSD/GPL");
64 #define VIVI_MAJOR_VERSION 0
65 #define VIVI_MINOR_VERSION 4
66 #define VIVI_RELEASE 0
67 #define VIVI_VERSION KERNEL_VERSION(VIVI_MAJOR_VERSION, VIVI_MINOR_VERSION, VIVI_RELEASE)
69 static int video_nr = -1; /* /dev/videoN, -1 for autodetect */
70 module_param(video_nr, int, 0);
73 module_param(debug, int, 0);
75 static unsigned int vid_limit = 16;
76 module_param(vid_limit,int,0644);
77 MODULE_PARM_DESC(vid_limit,"capture memory limit in megabytes");
79 /* supported controls */
80 static struct v4l2_queryctrl vivi_qctrl[] = {
82 .id = V4L2_CID_AUDIO_VOLUME,
87 .default_value = 65535,
89 .type = V4L2_CTRL_TYPE_INTEGER,
91 .id = V4L2_CID_BRIGHTNESS,
92 .type = V4L2_CTRL_TYPE_INTEGER,
100 .id = V4L2_CID_CONTRAST,
101 .type = V4L2_CTRL_TYPE_INTEGER,
106 .default_value = 0x10,
109 .id = V4L2_CID_SATURATION,
110 .type = V4L2_CTRL_TYPE_INTEGER,
111 .name = "Saturation",
115 .default_value = 127,
119 .type = V4L2_CTRL_TYPE_INTEGER,
129 static int qctl_regs[ARRAY_SIZE(vivi_qctrl)];
131 #define dprintk(level,fmt, arg...) \
133 if (debug >= (level)) \
134 printk(KERN_DEBUG "vivi: " fmt , ## arg); \
137 /* ------------------------------------------------------------------
139 ------------------------------------------------------------------*/
143 u32 fourcc; /* v4l2 format id */
147 static struct vivi_fmt format = {
148 .name = "4:2:2, packed, YUYV",
149 .fourcc = V4L2_PIX_FMT_YUYV,
155 struct scatterlist *sg;
158 /* buffer for one video frame */
160 /* common v4l buffer stuff -- must be first */
161 struct videobuf_buffer vb;
163 struct vivi_fmt *fmt;
165 struct sg_to_addr *to_addr;
168 struct vivi_dmaqueue {
169 struct list_head active;
170 struct list_head queued;
171 struct timer_list timeout;
173 /* thread for generating video stream*/
174 struct task_struct *kthread;
175 wait_queue_head_t wq;
176 /* Counters to control fps rate */
181 static LIST_HEAD(vivi_devlist);
184 struct list_head vivi_devlist;
186 struct semaphore lock;
190 /* various device info */
191 unsigned int resources;
192 struct video_device video_dev;
194 struct vivi_dmaqueue vidq;
196 /* Several counters */
197 int h,m,s,us,jiffies;
202 struct vivi_dev *dev;
205 struct vivi_fmt *fmt;
206 unsigned int width,height;
207 struct videobuf_queue vb_vidq;
209 enum v4l2_buf_type type;
212 /* ------------------------------------------------------------------
213 DMA and thread functions
214 ------------------------------------------------------------------*/
216 /* Bars and Colors should match positions */
228 static u8 bars[8][3] = {
230 {204,204,204}, /* white */
231 {208,208, 0}, /* ambar */
232 { 0,206,206}, /* cyan */
233 { 0,239, 0}, /* green */
234 {239, 0,239}, /* magenta */
235 {205, 0, 0}, /* red */
236 { 0, 0,255}, /* blue */
240 #define TO_Y(r,g,b) (((16829*r +33039*g +6416*b + 32768)>>16)+16)
241 /* RGB to V(Cr) Color transform */
242 #define TO_V(r,g,b) (((28784*r -24103*g -4681*b + 32768)>>16)+128)
243 /* RGB to U(Cb) Color transform */
244 #define TO_U(r,g,b) (((-9714*r -19070*g +28784*b + 32768)>>16)+128)
246 #define TSTAMP_MIN_Y 24
247 #define TSTAMP_MAX_Y TSTAMP_MIN_Y+15
248 #define TSTAMP_MIN_X 64
250 void prep_to_addr(struct sg_to_addr to_addr[],struct videobuf_buffer *vb)
254 for (i=0;i<vb->dma.nr_pages;i++) {
255 to_addr[i].sg=&vb->dma.sglist[i];
257 pos += vb->dma.sglist[i].length;
261 inline int get_addr_pos(int pos, int pages, struct sg_to_addr to_addr[])
263 int p1=0,p2=pages-1,p3=pages/2;
266 BUG_ON (pos>=to_addr[p2].pos+to_addr[p2].sg->length);
269 if (pos < to_addr[p3].pos) {
276 if (pos >= to_addr[p2].pos)
282 void gen_line(struct sg_to_addr to_addr[],int inipos,int pages,int wmax,
283 int hmax, int line, char *timestr)
285 int w,i,j,pos=inipos,pgpos,oldpg,y;
290 /* Get first addr pointed to pixel position */
291 oldpg=get_addr_pos(pos,pages,to_addr);
292 pg=pfn_to_page(to_addr[oldpg].sg->dma_address >> PAGE_SHIFT);
293 basep = kmap_atomic(pg, KM_BOUNCE_READ)+to_addr[oldpg].sg->offset;
295 /* We will just duplicate the second pixel at the packet */
298 /* Generate a standard color bar pattern */
299 for (w=0;w<wmax;w++) {
304 for (color=0;color<4;color++) {
305 pgpos=get_addr_pos(pos,pages,to_addr);
307 pg=pfn_to_page(to_addr[pgpos].sg->dma_address >> PAGE_SHIFT);
308 kunmap_atomic(basep, KM_BOUNCE_READ);
309 basep= kmap_atomic(pg, KM_BOUNCE_READ)+to_addr[pgpos].sg->offset;
312 p=basep+pos-to_addr[pgpos].pos;
317 *p=TO_Y(r,g,b); /* Luminance */
320 *p=TO_U(r,g,b); /* Cb */
323 *p=TO_V(r,g,b); /* Cr */
330 /* Checks if it is possible to show timestamp */
331 if (TSTAMP_MAX_Y>=hmax)
333 if (TSTAMP_MIN_X+strlen(timestr)>=wmax)
336 /* Print stream time */
337 if (line>=TSTAMP_MIN_Y && line<=TSTAMP_MAX_Y) {
339 for (s=timestr;*s;s++) {
340 chr=rom8x16_bits[(*s-0x30)*16+line-TSTAMP_MIN_Y];
342 if (chr&1<<(7-i)) { /* Font color*/
348 } else { /* Background color */
356 for (color=0;color<4;color++) {
357 pgpos=get_addr_pos(pos,pages,to_addr);
359 pg=pfn_to_page(to_addr[pgpos].
364 basep= kmap_atomic(pg,
366 to_addr[pgpos].sg->offset;
369 p=basep+pos-to_addr[pgpos].pos;
376 *p=TO_Y(r,g,b); /* Luminance */
379 *p=TO_U(r,g,b); /* Cb */
382 *p=TO_V(r,g,b); /* Cr */
394 kunmap_atomic(basep, KM_BOUNCE_READ);
396 static void vivi_fillbuff(struct vivi_dev *dev,struct vivi_buffer *buf)
399 int hmax = buf->vb.height;
400 int wmax = buf->vb.width;
401 struct videobuf_buffer *vb=&buf->vb;
402 struct sg_to_addr *to_addr=buf->to_addr;
405 /* Test if DMA mapping is ready */
406 if (!vb->dma.sglist[0].dma_address)
409 prep_to_addr(to_addr,vb);
411 /* Check if there is enough memory */
412 BUG_ON(buf->vb.dma.nr_pages << PAGE_SHIFT < (buf->vb.width*buf->vb.height)*2);
414 for (h=0;h<hmax;h++) {
415 gen_line(to_addr,pos,vb->dma.nr_pages,wmax,hmax,h,dev->timestr);
419 /* Updates stream time */
421 dev->us+=jiffies_to_usecs(jiffies-dev->jiffies);
422 dev->jiffies=jiffies;
423 if (dev->us>=1000000) {
437 sprintf(dev->timestr,"%02d:%02d:%02d:%03d",
438 dev->h,dev->m,dev->s,(dev->us+500)/1000);
440 dprintk(2,"vivifill at %s: Buffer 0x%08lx size= %d\n",dev->timestr,
441 (unsigned long)buf->vb.dma.vmalloc,pos);
443 /* Advice that buffer was filled */
444 buf->vb.state = STATE_DONE;
445 buf->vb.field_count++;
446 do_gettimeofday(&ts);
449 list_del(&buf->vb.queue);
450 wake_up(&buf->vb.done);
453 static int restart_video_queue(struct vivi_dmaqueue *dma_q);
455 static void vivi_thread_tick(struct vivi_dmaqueue *dma_q)
457 struct vivi_buffer *buf;
458 struct vivi_dev *dev= container_of(dma_q,struct vivi_dev,vidq);
462 /* Announces videobuf that all went ok */
463 for (bc = 0;; bc++) {
464 if (list_empty(&dma_q->active)) {
465 dprintk(1,"No active queue to serve\n");
469 buf = list_entry(dma_q->active.next,
470 struct vivi_buffer, vb.queue);
472 /* Nobody is waiting something to be done, just return */
473 if (!waitqueue_active(&buf->vb.done)) {
474 mod_timer(&dma_q->timeout, jiffies+BUFFER_TIMEOUT);
478 do_gettimeofday(&buf->vb.ts);
479 dprintk(2,"[%p/%d] wakeup\n",buf,buf->vb.i);
482 vivi_fillbuff(dev,buf);
484 if (list_empty(&dma_q->active)) {
485 del_timer(&dma_q->timeout);
487 mod_timer(&dma_q->timeout, jiffies+BUFFER_TIMEOUT);
490 dprintk(1,"%s: %d buffers handled (should be 1)\n",__FUNCTION__,bc);
493 void vivi_sleep(struct vivi_dmaqueue *dma_q)
496 DECLARE_WAITQUEUE(wait, current);
498 dprintk(1,"%s dma_q=0x%08lx\n",__FUNCTION__,(unsigned long)dma_q);
500 add_wait_queue(&dma_q->wq, &wait);
501 if (!kthread_should_stop()) {
504 /* Calculate time to wake up */
505 timeout=dma_q->ini_jiffies+msecs_to_jiffies((dma_q->frame*WAKE_NUMERATOR*1000)/WAKE_DENOMINATOR)-jiffies;
508 int old=dma_q->frame;
509 dma_q->frame=(jiffies_to_msecs(jiffies-dma_q->ini_jiffies)*WAKE_DENOMINATOR)/(WAKE_NUMERATOR*1000)+1;
511 timeout=dma_q->ini_jiffies+msecs_to_jiffies((dma_q->frame*WAKE_NUMERATOR*1000)/WAKE_DENOMINATOR)-jiffies;
513 dprintk(1,"underrun, losed %d frames. "
514 "Now, frame is %d. Waking on %d jiffies\n",
515 dma_q->frame-old,dma_q->frame,timeout);
517 dprintk(1,"will sleep for %i jiffies\n",timeout);
519 vivi_thread_tick(dma_q);
521 schedule_timeout_interruptible (timeout);
524 remove_wait_queue(&dma_q->wq, &wait);
528 int vivi_thread(void *data)
530 struct vivi_dmaqueue *dma_q=data;
532 dprintk(1,"thread started\n");
537 if (kthread_should_stop())
540 dprintk(1, "thread: exit\n");
544 int vivi_start_thread(struct vivi_dmaqueue *dma_q)
547 dma_q->ini_jiffies=jiffies;
549 dprintk(1,"%s\n",__FUNCTION__);
550 init_waitqueue_head(&dma_q->wq);
552 dma_q->kthread = kthread_run(vivi_thread, dma_q, "vivi");
554 if (dma_q->kthread == NULL) {
555 printk(KERN_ERR "vivi: kernel_thread() failed\n");
558 dprintk(1,"returning from %s\n",__FUNCTION__);
562 void vivi_stop_thread(struct vivi_dmaqueue *dma_q)
564 dprintk(1,"%s\n",__FUNCTION__);
565 /* shutdown control thread */
566 if (dma_q->kthread) {
567 kthread_stop(dma_q->kthread);
572 static int restart_video_queue(struct vivi_dmaqueue *dma_q)
574 struct vivi_buffer *buf, *prev;
575 struct list_head *item;
577 dprintk(1,"%s dma_q=0x%08lx\n",__FUNCTION__,(unsigned long)dma_q);
579 if (!list_empty(&dma_q->active)) {
580 buf = list_entry(dma_q->active.next, struct vivi_buffer, vb.queue);
581 dprintk(2,"restart_queue [%p/%d]: restart dma\n",
584 dprintk(1,"Restarting video dma\n");
585 vivi_stop_thread(dma_q);
586 // vivi_start_thread(dma_q);
588 /* cancel all outstanding capture / vbi requests */
589 list_for_each(item,&dma_q->active) {
590 buf = list_entry(item, struct vivi_buffer, vb.queue);
592 list_del(&buf->vb.queue);
593 buf->vb.state = STATE_ERROR;
594 wake_up(&buf->vb.done);
596 mod_timer(&dma_q->timeout, jiffies+BUFFER_TIMEOUT);
603 if (list_empty(&dma_q->queued))
605 buf = list_entry(dma_q->queued.next, struct vivi_buffer, vb.queue);
607 list_del(&buf->vb.queue);
608 list_add_tail(&buf->vb.queue,&dma_q->active);
610 dprintk(1,"Restarting video dma\n");
611 vivi_stop_thread(dma_q);
612 vivi_start_thread(dma_q);
614 buf->vb.state = STATE_ACTIVE;
615 mod_timer(&dma_q->timeout, jiffies+BUFFER_TIMEOUT);
616 dprintk(2,"[%p/%d] restart_queue - first active\n",
619 } else if (prev->vb.width == buf->vb.width &&
620 prev->vb.height == buf->vb.height &&
621 prev->fmt == buf->fmt) {
622 list_del(&buf->vb.queue);
623 list_add_tail(&buf->vb.queue,&dma_q->active);
624 buf->vb.state = STATE_ACTIVE;
625 dprintk(2,"[%p/%d] restart_queue - move to active\n",
634 static void vivi_vid_timeout(unsigned long data)
636 struct vivi_dev *dev = (struct vivi_dev*)data;
637 struct vivi_dmaqueue *vidq = &dev->vidq;
638 struct vivi_buffer *buf;
640 while (!list_empty(&vidq->active)) {
641 buf = list_entry(vidq->active.next, struct vivi_buffer, vb.queue);
642 list_del(&buf->vb.queue);
643 buf->vb.state = STATE_ERROR;
644 wake_up(&buf->vb.done);
645 printk("vivi/0: [%p/%d] timeout\n", buf, buf->vb.i);
648 restart_video_queue(vidq);
651 /* ------------------------------------------------------------------
653 ------------------------------------------------------------------*/
655 buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size)
657 struct vivi_fh *fh = vq->priv_data;
659 *size = fh->width*fh->height*2;
663 while (*size * *count > vid_limit * 1024 * 1024)
669 free_buffer(struct videobuf_queue *vq, struct vivi_buffer *buf)
671 dprintk(1,"%s\n",__FUNCTION__);
676 /*FIXME: Maybe a spinlock is required here */
680 videobuf_waiton(&buf->vb,0,0);
681 videobuf_dma_unmap(vq, &buf->vb.dma);
682 videobuf_dma_free(&buf->vb.dma);
683 buf->vb.state = STATE_NEEDS_INIT;
686 #define norm_maxw() 1024
687 #define norm_maxh() 768
689 buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
690 enum v4l2_field field)
692 struct vivi_fh *fh = vq->priv_data;
693 struct vivi_buffer *buf = container_of(vb,struct vivi_buffer,vb);
694 int rc, init_buffer = 0;
696 // dprintk(1,"%s, field=%d\n",__FUNCTION__,field);
698 BUG_ON(NULL == fh->fmt);
699 if (fh->width < 48 || fh->width > norm_maxw() ||
700 fh->height < 32 || fh->height > norm_maxh())
702 buf->vb.size = fh->width*fh->height*2;
703 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
706 if (buf->fmt != fh->fmt ||
707 buf->vb.width != fh->width ||
708 buf->vb.height != fh->height ||
709 buf->vb.field != field) {
711 buf->vb.width = fh->width;
712 buf->vb.height = fh->height;
713 buf->vb.field = field;
717 if (STATE_NEEDS_INIT == buf->vb.state) {
718 if (0 != (rc = videobuf_iolock(vq,&buf->vb,NULL)))
722 buf->vb.state = STATE_PREPARED;
724 if (NULL == (buf->to_addr = kmalloc(sizeof(*buf->to_addr) * vb->dma.nr_pages,GFP_KERNEL))) {
737 buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
739 struct vivi_buffer *buf = container_of(vb,struct vivi_buffer,vb);
740 struct vivi_fh *fh = vq->priv_data;
741 struct vivi_dev *dev = fh->dev;
742 struct vivi_dmaqueue *vidq = &dev->vidq;
743 struct vivi_buffer *prev;
745 if (!list_empty(&vidq->queued)) {
746 dprintk(1,"adding vb queue=0x%08lx\n",(unsigned long)&buf->vb.queue);
747 list_add_tail(&buf->vb.queue,&vidq->queued);
748 buf->vb.state = STATE_QUEUED;
749 dprintk(2,"[%p/%d] buffer_queue - append to queued\n",
751 } else if (list_empty(&vidq->active)) {
752 list_add_tail(&buf->vb.queue,&vidq->active);
754 buf->vb.state = STATE_ACTIVE;
755 mod_timer(&vidq->timeout, jiffies+BUFFER_TIMEOUT);
756 dprintk(2,"[%p/%d] buffer_queue - first active\n",
759 vivi_start_thread(vidq);
761 prev = list_entry(vidq->active.prev, struct vivi_buffer, vb.queue);
762 if (prev->vb.width == buf->vb.width &&
763 prev->vb.height == buf->vb.height &&
764 prev->fmt == buf->fmt) {
765 list_add_tail(&buf->vb.queue,&vidq->active);
766 buf->vb.state = STATE_ACTIVE;
767 dprintk(2,"[%p/%d] buffer_queue - append to active\n",
771 list_add_tail(&buf->vb.queue,&vidq->queued);
772 buf->vb.state = STATE_QUEUED;
773 dprintk(2,"[%p/%d] buffer_queue - first queued\n",
779 static void buffer_release(struct videobuf_queue *vq, struct videobuf_buffer *vb)
781 struct vivi_buffer *buf = container_of(vb,struct vivi_buffer,vb);
782 struct vivi_fh *fh = vq->priv_data;
783 struct vivi_dev *dev = (struct vivi_dev*)fh->dev;
784 struct vivi_dmaqueue *vidq = &dev->vidq;
786 dprintk(1,"%s\n",__FUNCTION__);
788 vivi_stop_thread(vidq);
793 int vivi_map_sg (void *dev, struct scatterlist *sg, int nents,
798 dprintk(1,"%s, number of pages=%d\n",__FUNCTION__,nents);
799 BUG_ON(direction == DMA_NONE);
801 for (i = 0; i < nents; i++ ) {
804 sg[i].dma_address = page_to_phys(sg[i].page) + sg[i].offset;
810 int vivi_unmap_sg(void *dev,struct scatterlist *sglist,int nr_pages,
813 dprintk(1,"%s\n",__FUNCTION__);
817 int vivi_dma_sync_sg(void *dev,struct scatterlist *sglist,int nr_pages,
820 // dprintk(1,"%s\n",__FUNCTION__);
822 // flush_write_buffers();
826 static struct videobuf_queue_ops vivi_video_qops = {
827 .buf_setup = buffer_setup,
828 .buf_prepare = buffer_prepare,
829 .buf_queue = buffer_queue,
830 .buf_release = buffer_release,
832 /* Non-pci handling routines */
833 .vb_map_sg = vivi_map_sg,
834 .vb_dma_sync_sg = vivi_dma_sync_sg,
835 .vb_unmap_sg = vivi_unmap_sg,
838 /* ------------------------------------------------------------------
840 ------------------------------------------------------------------*/
842 static int vivi_try_fmt(struct vivi_dev *dev, struct vivi_fh *fh,
843 struct v4l2_format *f)
845 struct vivi_fmt *fmt;
846 enum v4l2_field field;
847 unsigned int maxw, maxh;
849 if (format.fourcc != f->fmt.pix.pixelformat) {
850 dprintk(1,"Fourcc format invalid.\n");
855 field = f->fmt.pix.field;
857 if (field == V4L2_FIELD_ANY) {
858 // field=V4L2_FIELD_INTERLACED;
859 field=V4L2_FIELD_SEQ_TB;
860 } else if (V4L2_FIELD_INTERLACED != field) {
861 dprintk(1,"Field type invalid.\n");
868 f->fmt.pix.field = field;
869 if (f->fmt.pix.height < 32)
870 f->fmt.pix.height = 32;
871 if (f->fmt.pix.height > maxh)
872 f->fmt.pix.height = maxh;
873 if (f->fmt.pix.width < 48)
874 f->fmt.pix.width = 48;
875 if (f->fmt.pix.width > maxw)
876 f->fmt.pix.width = maxw;
877 f->fmt.pix.width &= ~0x03;
878 f->fmt.pix.bytesperline =
879 (f->fmt.pix.width * fmt->depth) >> 3;
880 f->fmt.pix.sizeimage =
881 f->fmt.pix.height * f->fmt.pix.bytesperline;
886 static int res_get(struct vivi_dev *dev, struct vivi_fh *fh)
890 if (dev->resources) {
891 /* no, someone else uses it */
895 /* it's free, grab it */
897 dprintk(1,"res: get\n");
902 static inline int res_locked(struct vivi_dev *dev)
904 return (dev->resources);
907 static void res_free(struct vivi_dev *dev, struct vivi_fh *fh)
911 dprintk(1,"res: put\n");
915 static int vivi_do_ioctl(struct inode *inode, struct file *file, unsigned int cmd, void *arg)
917 struct vivi_fh *fh = file->private_data;
918 struct vivi_dev *dev = fh->dev;
922 if (_IOC_DIR(cmd) & _IOC_WRITE)
923 v4l_printk_ioctl_arg("vivi(w)",cmd, arg);
924 else if (!_IOC_DIR(cmd) & _IOC_READ) {
925 v4l_print_ioctl("vivi", cmd);
930 /* --- capabilities ------------------------------------------ */
931 case VIDIOC_QUERYCAP:
933 struct v4l2_capability *cap = (struct v4l2_capability*)arg;
935 memset(cap, 0, sizeof(*cap));
937 strcpy(cap->driver, "vivi");
938 strcpy(cap->card, "vivi");
939 cap->version = VIVI_VERSION;
941 V4L2_CAP_VIDEO_CAPTURE |
946 /* --- capture ioctls ---------------------------------------- */
947 case VIDIOC_ENUM_FMT:
949 struct v4l2_fmtdesc *f = arg;
950 enum v4l2_buf_type type;
956 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
962 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
967 memset(f,0,sizeof(*f));
971 strlcpy(f->description,format.name,sizeof(f->description));
972 f->pixelformat = format.fourcc;
981 struct v4l2_format *f = (struct v4l2_format *)arg;
983 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
988 memset(&f->fmt.pix,0,sizeof(f->fmt.pix));
989 f->fmt.pix.width = fh->width;
990 f->fmt.pix.height = fh->height;
991 f->fmt.pix.field = fh->vb_vidq.field;
992 f->fmt.pix.pixelformat = fh->fmt->fourcc;
993 f->fmt.pix.bytesperline =
994 (f->fmt.pix.width * fh->fmt->depth) >> 3;
995 f->fmt.pix.sizeimage =
996 f->fmt.pix.height * f->fmt.pix.bytesperline;
1001 struct v4l2_format *f = arg;
1003 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1004 dprintk(1,"Only capture supported.\n");
1009 ret = vivi_try_fmt(dev,fh,f);
1014 fh->width = f->fmt.pix.width;
1015 fh->height = f->fmt.pix.height;
1016 fh->vb_vidq.field = f->fmt.pix.field;
1021 case VIDIOC_TRY_FMT:
1023 struct v4l2_format *f = arg;
1024 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1029 ret=vivi_try_fmt(dev,fh,f);
1032 case VIDIOC_REQBUFS:
1033 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1037 ret=videobuf_reqbufs(&fh->vb_vidq, arg);
1039 case VIDIOC_QUERYBUF:
1040 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1044 ret=videobuf_querybuf(&fh->vb_vidq, arg);
1047 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1051 ret=videobuf_qbuf(&fh->vb_vidq, arg);
1054 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1058 ret=videobuf_dqbuf(&fh->vb_vidq, arg,
1059 file->f_flags & O_NONBLOCK);
1062 /* --- streaming capture ------------------------------------- */
1065 struct video_mbuf *mbuf = arg;
1066 struct videobuf_queue *q=&fh->vb_vidq;
1067 struct v4l2_requestbuffers req;
1070 memset(&req,0,sizeof(req));
1073 req.memory = V4L2_MEMORY_MMAP;
1074 ret = videobuf_reqbufs(q,&req);
1077 memset(mbuf,0,sizeof(*mbuf));
1078 mbuf->frames = req.count;
1080 for (i = 0; i < mbuf->frames; i++) {
1081 mbuf->offsets[i] = q->bufs[i]->boff;
1082 mbuf->size += q->bufs[i]->bsize;
1087 case VIDIOC_STREAMON:
1089 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1091 if (!res_get(dev,fh))
1093 ret=videobuf_streamon(&fh->vb_vidq);
1096 case VIDIOC_STREAMOFF:
1098 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1102 ret = videobuf_streamoff(&fh->vb_vidq);
1108 /* ---------- tv norms ---------- */
1109 case VIDIOC_ENUMSTD:
1111 struct v4l2_standard *e = arg;
1117 ret = v4l2_video_std_construct(e, V4L2_STD_NTSC_M, "NTSC-M");
1119 /* Allows vivi to use different fps from video std */
1120 e->frameperiod.numerator = WAKE_NUMERATOR;
1121 e->frameperiod.denominator = WAKE_DENOMINATOR;
1127 v4l2_std_id *id = arg;
1129 *id = V4L2_STD_NTSC_M;
1136 /* ------ input switching ---------- */
1137 case VIDIOC_ENUMINPUT:
1138 { /* only one input in this sample driver */
1139 struct v4l2_input *inp = arg;
1141 if (inp->index != 0) {
1145 memset(inp, 0, sizeof(*inp));
1148 inp->type = V4L2_INPUT_TYPE_CAMERA;
1149 inp->std = V4L2_STD_NTSC_M;
1150 strcpy(inp->name,"Camera");
1153 case VIDIOC_G_INPUT:
1155 unsigned int *i = arg;
1160 case VIDIOC_S_INPUT:
1162 unsigned int *i = arg;
1169 /* --- controls ---------------------------------------------- */
1170 case VIDIOC_QUERYCTRL:
1172 struct v4l2_queryctrl *qc = arg;
1175 for (i = 0; i < ARRAY_SIZE(vivi_qctrl); i++)
1176 if (qc->id && qc->id == vivi_qctrl[i].id) {
1177 memcpy(qc, &(vivi_qctrl[i]),
1187 struct v4l2_control *ctrl = arg;
1190 for (i = 0; i < ARRAY_SIZE(vivi_qctrl); i++)
1191 if (ctrl->id == vivi_qctrl[i].id) {
1192 ctrl->value=qctl_regs[i];
1201 struct v4l2_control *ctrl = arg;
1203 for (i = 0; i < ARRAY_SIZE(vivi_qctrl); i++)
1204 if (ctrl->id == vivi_qctrl[i].id) {
1206 vivi_qctrl[i].minimum
1208 vivi_qctrl[i].maximum) {
1212 qctl_regs[i]=ctrl->value;
1219 ret=v4l_compat_translate_ioctl(inode,file,cmd,arg,vivi_do_ioctl);
1224 v4l_print_ioctl("vivi(err)", cmd);
1225 dprintk(1,"errcode=%d\n",ret);
1226 } else if (_IOC_DIR(cmd) & _IOC_READ)
1227 v4l_printk_ioctl_arg("vivi(r)",cmd, arg);
1233 static int vivi_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
1235 return video_usercopy(inode, file, cmd, arg, vivi_do_ioctl);
1238 /* ------------------------------------------------------------------
1239 File operations for the device
1240 ------------------------------------------------------------------*/
1242 #define line_buf_size(norm) (norm_maxw(norm)*(format.depth+7)/8)
1244 static int vivi_open(struct inode *inode, struct file *file)
1246 int minor = iminor(inode);
1247 struct vivi_dev *h,*dev = NULL;
1249 struct list_head *list;
1250 enum v4l2_buf_type type = 0;
1253 printk(KERN_DEBUG "vivi: open called (minor=%d)\n",minor);
1255 list_for_each(list,&vivi_devlist) {
1256 h = list_entry(list, struct vivi_dev, vivi_devlist);
1257 if (h->video_dev.minor == minor) {
1259 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1266 /* If more than one user, mutex should be added */
1269 dprintk(1,"open minor=%d type=%s users=%d\n",
1270 minor,v4l2_type_names[type],dev->users);
1272 /* allocate + initialize per filehandle data */
1273 fh = kzalloc(sizeof(*fh),GFP_KERNEL);
1279 file->private_data = fh;
1281 fh->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1286 /* Put all controls at a sane state */
1287 for (i = 0; i < ARRAY_SIZE(vivi_qctrl); i++)
1288 qctl_regs[i] =vivi_qctrl[i].default_value;
1290 dprintk(1,"Open: fh=0x%08lx, dev=0x%08lx, dev->vidq=0x%08lx\n",
1291 (unsigned long)fh,(unsigned long)dev,(unsigned long)&dev->vidq);
1292 dprintk(1,"Open: list_empty queued=%d\n",list_empty(&dev->vidq.queued));
1293 dprintk(1,"Open: list_empty active=%d\n",list_empty(&dev->vidq.active));
1295 /* Resets frame counters */
1300 dev->jiffies=jiffies;
1301 sprintf(dev->timestr,"%02d:%02d:%02d:%03d",
1302 dev->h,dev->m,dev->s,(dev->us+500)/1000);
1304 videobuf_queue_init(&fh->vb_vidq, &vivi_video_qops,
1307 V4L2_FIELD_INTERLACED,
1308 sizeof(struct vivi_buffer),fh);
1314 vivi_read(struct file *file, char __user *data, size_t count, loff_t *ppos)
1316 struct vivi_fh *fh = file->private_data;
1318 if (fh->type==V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1319 if (res_locked(fh->dev))
1321 return videobuf_read_one(&fh->vb_vidq, data, count, ppos,
1322 file->f_flags & O_NONBLOCK);
1328 vivi_poll(struct file *file, struct poll_table_struct *wait)
1330 struct vivi_fh *fh = file->private_data;
1331 struct vivi_buffer *buf;
1333 dprintk(1,"%s\n",__FUNCTION__);
1335 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != fh->type)
1338 if (res_get(fh->dev,fh)) {
1339 dprintk(1,"poll: mmap interface\n");
1340 /* streaming capture */
1341 if (list_empty(&fh->vb_vidq.stream))
1343 buf = list_entry(fh->vb_vidq.stream.next,struct vivi_buffer,vb.stream);
1345 dprintk(1,"poll: read() interface\n");
1346 /* read() capture */
1347 buf = (struct vivi_buffer*)fh->vb_vidq.read_buf;
1351 poll_wait(file, &buf->vb.done, wait);
1352 if (buf->vb.state == STATE_DONE ||
1353 buf->vb.state == STATE_ERROR)
1354 return POLLIN|POLLRDNORM;
1358 static int vivi_release(struct inode *inode, struct file *file)
1360 struct vivi_fh *fh = file->private_data;
1361 struct vivi_dev *dev = fh->dev;
1362 struct vivi_dmaqueue *vidq = &dev->vidq;
1364 int minor = iminor(inode);
1366 vivi_stop_thread(vidq);
1367 videobuf_mmap_free(&fh->vb_vidq);
1373 printk(KERN_DEBUG "vivi: close called (minor=%d, users=%d)\n",minor,dev->users);
1379 vivi_mmap(struct file *file, struct vm_area_struct * vma)
1381 struct vivi_fh *fh = file->private_data;
1384 dprintk (1,"mmap called, vma=0x%08lx\n",(unsigned long)vma);
1386 ret=videobuf_mmap_mapper(&fh->vb_vidq, vma);
1388 dprintk (1,"vma start=0x%08lx, size=%ld, ret=%d\n",
1389 (unsigned long)vma->vm_start,
1390 (unsigned long)vma->vm_end-(unsigned long)vma->vm_start,
1396 static struct file_operations vivi_fops = {
1397 .owner = THIS_MODULE,
1399 .release = vivi_release,
1402 .ioctl = vivi_ioctl,
1404 .llseek = no_llseek,
1407 static struct video_device vivi = {
1408 .name = "VTM Virtual Video Capture Board",
1409 .type = VID_TYPE_CAPTURE,
1413 // .release = video_device_release,
1415 /* ------------------------------------------------------------------
1416 Initialization and module stuff
1417 ------------------------------------------------------------------*/
1419 static int __init vivi_init(void)
1422 struct vivi_dev *dev;
1424 dev = kzalloc(sizeof(*dev),GFP_KERNEL);
1427 list_add_tail(&dev->vivi_devlist,&vivi_devlist);
1429 /* init video dma queues */
1430 INIT_LIST_HEAD(&dev->vidq.active);
1431 INIT_LIST_HEAD(&dev->vidq.queued);
1433 /* initialize locks */
1434 init_MUTEX(&dev->lock);
1436 dev->vidq.timeout.function = vivi_vid_timeout;
1437 dev->vidq.timeout.data = (unsigned long)dev;
1438 init_timer(&dev->vidq.timeout);
1440 ret = video_register_device(&vivi, VFL_TYPE_GRABBER, video_nr);
1441 printk(KERN_INFO "Video Technology Magazine Virtual Video Capture Board (Load status: %d)\n", ret);
1445 static void __exit vivi_exit(void)
1448 struct list_head *list;
1450 list_for_each(list,&vivi_devlist) {
1451 h = list_entry(list, struct vivi_dev, vivi_devlist);
1454 video_unregister_device(&vivi);
1457 module_init(vivi_init);
1458 module_exit(vivi_exit);