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/mutex.h>
29 #include <linux/videodev2.h>
30 #include <linux/dma-mapping.h>
31 #ifdef CONFIG_VIDEO_V4L1_COMPAT
32 /* Include V4L1 specific functions. Should be removed soon */
33 #include <linux/videodev.h>
35 #include <linux/interrupt.h>
36 #include <media/videobuf-vmalloc.h>
37 #include <media/v4l2-common.h>
38 #include <linux/kthread.h>
39 #include <linux/highmem.h>
40 #include <linux/freezer.h>
42 /* Wake up at about 30 fps */
43 #define WAKE_NUMERATOR 30
44 #define WAKE_DENOMINATOR 1001
45 #define BUFFER_TIMEOUT msecs_to_jiffies(500) /* 0.5 seconds */
47 /* These timers are for 1 fps - used only for testing */
48 //#define WAKE_DENOMINATOR 30 /* hack for testing purposes */
49 //#define BUFFER_TIMEOUT msecs_to_jiffies(5000) /* 5 seconds */
53 #define VIVI_MAJOR_VERSION 0
54 #define VIVI_MINOR_VERSION 4
55 #define VIVI_RELEASE 0
56 #define VIVI_VERSION KERNEL_VERSION(VIVI_MAJOR_VERSION, VIVI_MINOR_VERSION, VIVI_RELEASE)
58 /* Declare static vars that will be used as parameters */
59 static unsigned int vid_limit
= 16; /* Video memory limit, in Mb */
60 static struct video_device vivi
; /* Video device */
61 static int video_nr
= -1; /* /dev/videoN, -1 for autodetect */
63 /* supported controls */
64 static struct v4l2_queryctrl vivi_qctrl
[] = {
66 .id
= V4L2_CID_AUDIO_VOLUME
,
71 .default_value
= 65535,
73 .type
= V4L2_CTRL_TYPE_INTEGER
,
75 .id
= V4L2_CID_BRIGHTNESS
,
76 .type
= V4L2_CTRL_TYPE_INTEGER
,
84 .id
= V4L2_CID_CONTRAST
,
85 .type
= V4L2_CTRL_TYPE_INTEGER
,
90 .default_value
= 0x10,
93 .id
= V4L2_CID_SATURATION
,
94 .type
= V4L2_CTRL_TYPE_INTEGER
,
103 .type
= V4L2_CTRL_TYPE_INTEGER
,
113 static int qctl_regs
[ARRAY_SIZE(vivi_qctrl
)];
115 #define dprintk(level,fmt, arg...) \
117 if (vivi.debug >= (level)) \
118 printk(KERN_DEBUG "vivi: " fmt , ## arg); \
121 /* ------------------------------------------------------------------
123 ------------------------------------------------------------------*/
127 u32 fourcc
; /* v4l2 format id */
131 static struct vivi_fmt format
= {
132 .name
= "4:2:2, packed, YUYV",
133 .fourcc
= V4L2_PIX_FMT_YUYV
,
139 struct scatterlist
*sg
;
142 /* buffer for one video frame */
144 /* common v4l buffer stuff -- must be first */
145 struct videobuf_buffer vb
;
147 struct vivi_fmt
*fmt
;
150 struct vivi_dmaqueue
{
151 struct list_head active
;
152 struct list_head queued
;
153 struct timer_list timeout
;
155 /* thread for generating video stream*/
156 struct task_struct
*kthread
;
157 wait_queue_head_t wq
;
158 /* Counters to control fps rate */
163 static LIST_HEAD(vivi_devlist
);
166 struct list_head vivi_devlist
;
172 /* various device info */
173 struct video_device vfd
;
175 struct vivi_dmaqueue vidq
;
177 /* Several counters */
178 int h
,m
,s
,us
,jiffies
;
183 struct vivi_dev
*dev
;
186 struct vivi_fmt
*fmt
;
187 unsigned int width
,height
;
188 struct videobuf_queue vb_vidq
;
190 enum v4l2_buf_type type
;
193 /* ------------------------------------------------------------------
194 DMA and thread functions
195 ------------------------------------------------------------------*/
197 /* Bars and Colors should match positions */
209 static u8 bars
[8][3] = {
211 {204,204,204}, /* white */
212 {208,208, 0}, /* ambar */
213 { 0,206,206}, /* cyan */
214 { 0,239, 0}, /* green */
215 {239, 0,239}, /* magenta */
216 {205, 0, 0}, /* red */
217 { 0, 0,255}, /* blue */
221 #define TO_Y(r,g,b) (((16829*r +33039*g +6416*b + 32768)>>16)+16)
222 /* RGB to V(Cr) Color transform */
223 #define TO_V(r,g,b) (((28784*r -24103*g -4681*b + 32768)>>16)+128)
224 /* RGB to U(Cb) Color transform */
225 #define TO_U(r,g,b) (((-9714*r -19070*g +28784*b + 32768)>>16)+128)
227 #define TSTAMP_MIN_Y 24
228 #define TSTAMP_MAX_Y TSTAMP_MIN_Y+15
229 #define TSTAMP_MIN_X 64
231 static void gen_line(char *basep
,int inipos
,int wmax
,
232 int hmax
, int line
, int count
, char *timestr
)
234 int w
,i
,j
,pos
=inipos
,y
;
238 /* We will just duplicate the second pixel at the packet */
241 /* Generate a standard color bar pattern */
242 for (w
=0;w
<wmax
;w
++) {
243 int colorpos
=((w
+count
)*8/(wmax
+1)) % 8;
248 for (color
=0;color
<4;color
++) {
254 *p
=TO_Y(r
,g
,b
); /* Luminance */
257 *p
=TO_U(r
,g
,b
); /* Cb */
260 *p
=TO_V(r
,g
,b
); /* Cr */
267 /* Checks if it is possible to show timestamp */
268 if (TSTAMP_MAX_Y
>=hmax
)
270 if (TSTAMP_MIN_X
+strlen(timestr
)>=wmax
)
273 /* Print stream time */
274 if (line
>=TSTAMP_MIN_Y
&& line
<=TSTAMP_MAX_Y
) {
276 for (s
=timestr
;*s
;s
++) {
277 chr
=rom8x16_bits
[(*s
-0x30)*16+line
-TSTAMP_MIN_Y
];
279 if (chr
&1<<(7-i
)) { /* Font color*/
285 } else { /* Background color */
293 for (color
=0;color
<4;color
++) {
301 *p
=TO_Y(r
,g
,b
); /* Luminance */
304 *p
=TO_U(r
,g
,b
); /* Cb */
307 *p
=TO_V(r
,g
,b
); /* Cr */
321 static void vivi_fillbuff(struct vivi_dev
*dev
,struct vivi_buffer
*buf
)
324 int hmax
= buf
->vb
.height
;
325 int wmax
= buf
->vb
.width
;
327 char *tmpbuf
= kmalloc(wmax
*2,GFP_KERNEL
);
328 void *vbuf
=videobuf_to_vmalloc (&buf
->vb
);
329 /* FIXME: move to dev struct */
330 static int mv_count
=0;
335 for (h
=0;h
<hmax
;h
++) {
336 gen_line(tmpbuf
,0,wmax
,hmax
,h
,mv_count
,
338 /* FIXME: replacing to __copy_to_user */
339 if (copy_to_user(vbuf
+pos
,tmpbuf
,wmax
*2)!=0)
340 dprintk(2,"vivifill copy_to_user failed.\n");
348 /* Updates stream time */
350 dev
->us
+=jiffies_to_usecs(jiffies
-dev
->jiffies
);
351 dev
->jiffies
=jiffies
;
352 if (dev
->us
>=1000000) {
366 sprintf(dev
->timestr
,"%02d:%02d:%02d:%03d",
367 dev
->h
,dev
->m
,dev
->s
,(dev
->us
+500)/1000);
369 dprintk(2,"vivifill at %s: Buffer 0x%08lx size= %d\n",dev
->timestr
,
370 (unsigned long)tmpbuf
,pos
);
372 /* Advice that buffer was filled */
373 buf
->vb
.state
= VIDEOBUF_DONE
;
374 buf
->vb
.field_count
++;
375 do_gettimeofday(&ts
);
378 list_del(&buf
->vb
.queue
);
379 wake_up(&buf
->vb
.done
);
382 static int restart_video_queue(struct vivi_dmaqueue
*dma_q
);
384 static void vivi_thread_tick(struct vivi_dmaqueue
*dma_q
)
386 struct vivi_buffer
*buf
;
387 struct vivi_dev
*dev
= container_of(dma_q
,struct vivi_dev
,vidq
);
391 /* Announces videobuf that all went ok */
392 for (bc
= 0;; bc
++) {
393 if (list_empty(&dma_q
->active
)) {
394 dprintk(1,"No active queue to serve\n");
398 buf
= list_entry(dma_q
->active
.next
,
399 struct vivi_buffer
, vb
.queue
);
401 /* Nobody is waiting something to be done, just return */
402 if (!waitqueue_active(&buf
->vb
.done
)) {
403 mod_timer(&dma_q
->timeout
, jiffies
+BUFFER_TIMEOUT
);
407 do_gettimeofday(&buf
->vb
.ts
);
408 dprintk(2,"[%p/%d] wakeup\n",buf
,buf
->vb
.i
);
411 vivi_fillbuff(dev
,buf
);
413 if (list_empty(&dma_q
->active
)) {
414 del_timer(&dma_q
->timeout
);
416 mod_timer(&dma_q
->timeout
, jiffies
+BUFFER_TIMEOUT
);
420 dprintk(1,"%s: %d buffers handled (should be 1)\n",__FUNCTION__
,bc
);
423 static void vivi_sleep(struct vivi_dmaqueue
*dma_q
)
426 DECLARE_WAITQUEUE(wait
, current
);
428 dprintk(1,"%s dma_q=0x%08lx\n",__FUNCTION__
,(unsigned long)dma_q
);
430 add_wait_queue(&dma_q
->wq
, &wait
);
431 if (!kthread_should_stop()) {
434 /* Calculate time to wake up */
435 timeout
=dma_q
->ini_jiffies
+msecs_to_jiffies((dma_q
->frame
*WAKE_NUMERATOR
*1000)/WAKE_DENOMINATOR
)-jiffies
;
438 int old
=dma_q
->frame
;
439 dma_q
->frame
=(jiffies_to_msecs(jiffies
-dma_q
->ini_jiffies
)*WAKE_DENOMINATOR
)/(WAKE_NUMERATOR
*1000)+1;
441 timeout
=dma_q
->ini_jiffies
+msecs_to_jiffies((dma_q
->frame
*WAKE_NUMERATOR
*1000)/WAKE_DENOMINATOR
)-jiffies
;
443 dprintk(1,"underrun, losed %d frames. "
444 "Now, frame is %d. Waking on %d jiffies\n",
445 dma_q
->frame
-old
,dma_q
->frame
,timeout
);
447 dprintk(1,"will sleep for %i jiffies\n",timeout
);
449 vivi_thread_tick(dma_q
);
451 schedule_timeout_interruptible (timeout
);
454 remove_wait_queue(&dma_q
->wq
, &wait
);
458 static int vivi_thread(void *data
)
460 struct vivi_dmaqueue
*dma_q
=data
;
462 dprintk(1,"thread started\n");
464 mod_timer(&dma_q
->timeout
, jiffies
+BUFFER_TIMEOUT
);
470 if (kthread_should_stop())
473 dprintk(1, "thread: exit\n");
477 static int vivi_start_thread(struct vivi_dmaqueue
*dma_q
)
480 dma_q
->ini_jiffies
=jiffies
;
482 dprintk(1,"%s\n",__FUNCTION__
);
484 dma_q
->kthread
= kthread_run(vivi_thread
, dma_q
, "vivi");
486 if (IS_ERR(dma_q
->kthread
)) {
487 printk(KERN_ERR
"vivi: kernel_thread() failed\n");
488 return PTR_ERR(dma_q
->kthread
);
491 wake_up_interruptible(&dma_q
->wq
);
493 dprintk(1,"returning from %s\n",__FUNCTION__
);
497 static void vivi_stop_thread(struct vivi_dmaqueue
*dma_q
)
499 dprintk(1,"%s\n",__FUNCTION__
);
500 /* shutdown control thread */
501 if (dma_q
->kthread
) {
502 kthread_stop(dma_q
->kthread
);
507 static int restart_video_queue(struct vivi_dmaqueue
*dma_q
)
509 struct vivi_buffer
*buf
, *prev
;
511 dprintk(1,"%s dma_q=0x%08lx\n",__FUNCTION__
,(unsigned long)dma_q
);
513 if (!list_empty(&dma_q
->active
)) {
514 buf
= list_entry(dma_q
->active
.next
, struct vivi_buffer
, vb
.queue
);
515 dprintk(2,"restart_queue [%p/%d]: restart dma\n",
518 dprintk(1,"Restarting video dma\n");
519 vivi_stop_thread(dma_q
);
520 // vivi_start_thread(dma_q);
522 /* cancel all outstanding capture / vbi requests */
523 list_for_each_entry_safe(buf
, prev
, &dma_q
->active
, vb
.queue
) {
524 list_del(&buf
->vb
.queue
);
525 buf
->vb
.state
= VIDEOBUF_ERROR
;
526 wake_up(&buf
->vb
.done
);
528 mod_timer(&dma_q
->timeout
, jiffies
+BUFFER_TIMEOUT
);
535 if (list_empty(&dma_q
->queued
))
537 buf
= list_entry(dma_q
->queued
.next
, struct vivi_buffer
, vb
.queue
);
539 list_del(&buf
->vb
.queue
);
540 list_add_tail(&buf
->vb
.queue
,&dma_q
->active
);
542 dprintk(1,"Restarting video dma\n");
543 vivi_stop_thread(dma_q
);
544 vivi_start_thread(dma_q
);
546 buf
->vb
.state
= VIDEOBUF_ACTIVE
;
547 mod_timer(&dma_q
->timeout
, jiffies
+BUFFER_TIMEOUT
);
548 dprintk(2,"[%p/%d] restart_queue - first active\n",
551 } else if (prev
->vb
.width
== buf
->vb
.width
&&
552 prev
->vb
.height
== buf
->vb
.height
&&
553 prev
->fmt
== buf
->fmt
) {
554 list_del(&buf
->vb
.queue
);
555 list_add_tail(&buf
->vb
.queue
,&dma_q
->active
);
556 buf
->vb
.state
= VIDEOBUF_ACTIVE
;
557 dprintk(2,"[%p/%d] restart_queue - move to active\n",
566 static void vivi_vid_timeout(unsigned long data
)
568 struct vivi_dev
*dev
= (struct vivi_dev
*)data
;
569 struct vivi_dmaqueue
*vidq
= &dev
->vidq
;
570 struct vivi_buffer
*buf
;
572 while (!list_empty(&vidq
->active
)) {
573 buf
= list_entry(vidq
->active
.next
, struct vivi_buffer
, vb
.queue
);
574 list_del(&buf
->vb
.queue
);
575 buf
->vb
.state
= VIDEOBUF_ERROR
;
576 wake_up(&buf
->vb
.done
);
577 printk("vivi/0: [%p/%d] timeout\n", buf
, buf
->vb
.i
);
580 restart_video_queue(vidq
);
583 /* ------------------------------------------------------------------
585 ------------------------------------------------------------------*/
587 buffer_setup(struct videobuf_queue
*vq
, unsigned int *count
, unsigned int *size
)
589 struct vivi_fh
*fh
= vq
->priv_data
;
591 *size
= fh
->width
*fh
->height
*2;
596 while (*size
* *count
> vid_limit
* 1024 * 1024)
599 dprintk(1,"%s, count=%d, size=%d\n",__FUNCTION__
,*count
, *size
);
604 static void free_buffer(struct videobuf_queue
*vq
, struct vivi_buffer
*buf
)
606 dprintk(1,"%s\n",__FUNCTION__
);
611 videobuf_waiton(&buf
->vb
,0,0);
612 videobuf_vmalloc_free(&buf
->vb
);
613 buf
->vb
.state
= VIDEOBUF_NEEDS_INIT
;
616 #define norm_maxw() 1024
617 #define norm_maxh() 768
619 buffer_prepare(struct videobuf_queue
*vq
, struct videobuf_buffer
*vb
,
620 enum v4l2_field field
)
622 struct vivi_fh
*fh
= vq
->priv_data
;
623 struct vivi_buffer
*buf
= container_of(vb
,struct vivi_buffer
,vb
);
624 int rc
, init_buffer
= 0;
626 dprintk(1,"%s, field=%d\n",__FUNCTION__
,field
);
628 BUG_ON(NULL
== fh
->fmt
);
629 if (fh
->width
< 48 || fh
->width
> norm_maxw() ||
630 fh
->height
< 32 || fh
->height
> norm_maxh())
632 buf
->vb
.size
= fh
->width
*fh
->height
*2;
633 if (0 != buf
->vb
.baddr
&& buf
->vb
.bsize
< buf
->vb
.size
)
636 if (buf
->fmt
!= fh
->fmt
||
637 buf
->vb
.width
!= fh
->width
||
638 buf
->vb
.height
!= fh
->height
||
639 buf
->vb
.field
!= field
) {
641 buf
->vb
.width
= fh
->width
;
642 buf
->vb
.height
= fh
->height
;
643 buf
->vb
.field
= field
;
647 if (VIDEOBUF_NEEDS_INIT
== buf
->vb
.state
) {
648 if (0 != (rc
= videobuf_iolock(vq
,&buf
->vb
,NULL
)))
652 buf
->vb
.state
= VIDEOBUF_PREPARED
;
662 buffer_queue(struct videobuf_queue
*vq
, struct videobuf_buffer
*vb
)
664 struct vivi_buffer
*buf
= container_of(vb
,struct vivi_buffer
,vb
);
665 struct vivi_fh
*fh
= vq
->priv_data
;
666 struct vivi_dev
*dev
= fh
->dev
;
667 struct vivi_dmaqueue
*vidq
= &dev
->vidq
;
668 struct vivi_buffer
*prev
;
670 if (!list_empty(&vidq
->queued
)) {
671 dprintk(1,"adding vb queue=0x%08lx\n",(unsigned long)&buf
->vb
.queue
);
672 list_add_tail(&buf
->vb
.queue
,&vidq
->queued
);
673 buf
->vb
.state
= VIDEOBUF_QUEUED
;
674 dprintk(2,"[%p/%d] buffer_queue - append to queued\n",
676 } else if (list_empty(&vidq
->active
)) {
677 list_add_tail(&buf
->vb
.queue
,&vidq
->active
);
679 buf
->vb
.state
= VIDEOBUF_ACTIVE
;
680 mod_timer(&vidq
->timeout
, jiffies
+BUFFER_TIMEOUT
);
681 dprintk(2,"[%p/%d] buffer_queue - first active\n",
684 vivi_start_thread(vidq
);
686 prev
= list_entry(vidq
->active
.prev
, struct vivi_buffer
, vb
.queue
);
687 if (prev
->vb
.width
== buf
->vb
.width
&&
688 prev
->vb
.height
== buf
->vb
.height
&&
689 prev
->fmt
== buf
->fmt
) {
690 list_add_tail(&buf
->vb
.queue
,&vidq
->active
);
691 buf
->vb
.state
= VIDEOBUF_ACTIVE
;
692 dprintk(2,"[%p/%d] buffer_queue - append to active\n",
696 list_add_tail(&buf
->vb
.queue
,&vidq
->queued
);
697 buf
->vb
.state
= VIDEOBUF_QUEUED
;
698 dprintk(2,"[%p/%d] buffer_queue - first queued\n",
704 static void buffer_release(struct videobuf_queue
*vq
, struct videobuf_buffer
*vb
)
706 struct vivi_buffer
*buf
= container_of(vb
,struct vivi_buffer
,vb
);
707 struct vivi_fh
*fh
= vq
->priv_data
;
708 struct vivi_dev
*dev
= (struct vivi_dev
*)fh
->dev
;
709 struct vivi_dmaqueue
*vidq
= &dev
->vidq
;
711 dprintk(1,"%s\n",__FUNCTION__
);
713 vivi_stop_thread(vidq
);
718 static struct videobuf_queue_ops vivi_video_qops
= {
719 .buf_setup
= buffer_setup
,
720 .buf_prepare
= buffer_prepare
,
721 .buf_queue
= buffer_queue
,
722 .buf_release
= buffer_release
,
725 /* ------------------------------------------------------------------
726 IOCTL vidioc handling
727 ------------------------------------------------------------------*/
728 static int vidioc_querycap (struct file
*file
, void *priv
,
729 struct v4l2_capability
*cap
)
731 strcpy(cap
->driver
, "vivi");
732 strcpy(cap
->card
, "vivi");
733 cap
->version
= VIVI_VERSION
;
734 cap
->capabilities
= V4L2_CAP_VIDEO_CAPTURE
|
740 static int vidioc_enum_fmt_cap (struct file
*file
, void *priv
,
741 struct v4l2_fmtdesc
*f
)
746 strlcpy(f
->description
,format
.name
,sizeof(f
->description
));
747 f
->pixelformat
= format
.fourcc
;
751 static int vidioc_g_fmt_cap (struct file
*file
, void *priv
,
752 struct v4l2_format
*f
)
754 struct vivi_fh
*fh
=priv
;
756 f
->fmt
.pix
.width
= fh
->width
;
757 f
->fmt
.pix
.height
= fh
->height
;
758 f
->fmt
.pix
.field
= fh
->vb_vidq
.field
;
759 f
->fmt
.pix
.pixelformat
= fh
->fmt
->fourcc
;
760 f
->fmt
.pix
.bytesperline
=
761 (f
->fmt
.pix
.width
* fh
->fmt
->depth
) >> 3;
762 f
->fmt
.pix
.sizeimage
=
763 f
->fmt
.pix
.height
* f
->fmt
.pix
.bytesperline
;
768 static int vidioc_try_fmt_cap (struct file
*file
, void *priv
,
769 struct v4l2_format
*f
)
771 struct vivi_fmt
*fmt
;
772 enum v4l2_field field
;
773 unsigned int maxw
, maxh
;
775 if (format
.fourcc
!= f
->fmt
.pix
.pixelformat
) {
776 dprintk(1,"Fourcc format (0x%08x) invalid. Driver accepts "
777 "only 0x%08x\n",f
->fmt
.pix
.pixelformat
,format
.fourcc
);
782 field
= f
->fmt
.pix
.field
;
784 if (field
== V4L2_FIELD_ANY
) {
785 field
=V4L2_FIELD_INTERLACED
;
786 } else if (V4L2_FIELD_INTERLACED
!= field
) {
787 dprintk(1,"Field type invalid.\n");
794 f
->fmt
.pix
.field
= field
;
795 if (f
->fmt
.pix
.height
< 32)
796 f
->fmt
.pix
.height
= 32;
797 if (f
->fmt
.pix
.height
> maxh
)
798 f
->fmt
.pix
.height
= maxh
;
799 if (f
->fmt
.pix
.width
< 48)
800 f
->fmt
.pix
.width
= 48;
801 if (f
->fmt
.pix
.width
> maxw
)
802 f
->fmt
.pix
.width
= maxw
;
803 f
->fmt
.pix
.width
&= ~0x03;
804 f
->fmt
.pix
.bytesperline
=
805 (f
->fmt
.pix
.width
* fmt
->depth
) >> 3;
806 f
->fmt
.pix
.sizeimage
=
807 f
->fmt
.pix
.height
* f
->fmt
.pix
.bytesperline
;
812 /*FIXME: This seems to be generic enough to be at videodev2 */
813 static int vidioc_s_fmt_cap (struct file
*file
, void *priv
,
814 struct v4l2_format
*f
)
816 struct vivi_fh
*fh
=priv
;
817 int ret
= vidioc_try_fmt_cap(file
,fh
,f
);
822 fh
->width
= f
->fmt
.pix
.width
;
823 fh
->height
= f
->fmt
.pix
.height
;
824 fh
->vb_vidq
.field
= f
->fmt
.pix
.field
;
830 static int vidioc_reqbufs (struct file
*file
, void *priv
, struct v4l2_requestbuffers
*p
)
832 struct vivi_fh
*fh
=priv
;
834 return (videobuf_reqbufs(&fh
->vb_vidq
, p
));
837 static int vidioc_querybuf (struct file
*file
, void *priv
, struct v4l2_buffer
*p
)
839 struct vivi_fh
*fh
=priv
;
841 return (videobuf_querybuf(&fh
->vb_vidq
, p
));
844 static int vidioc_qbuf (struct file
*file
, void *priv
, struct v4l2_buffer
*p
)
846 struct vivi_fh
*fh
=priv
;
848 return (videobuf_qbuf(&fh
->vb_vidq
, p
));
851 static int vidioc_dqbuf (struct file
*file
, void *priv
, struct v4l2_buffer
*p
)
853 struct vivi_fh
*fh
=priv
;
855 return (videobuf_dqbuf(&fh
->vb_vidq
, p
,
856 file
->f_flags
& O_NONBLOCK
));
859 #ifdef CONFIG_VIDEO_V4L1_COMPAT
860 static int vidiocgmbuf (struct file
*file
, void *priv
, struct video_mbuf
*mbuf
)
862 struct vivi_fh
*fh
=priv
;
864 return videobuf_cgmbuf (&fh
->vb_vidq
, mbuf
, 8);
868 static int vidioc_streamon(struct file
*file
, void *priv
, enum v4l2_buf_type i
)
870 struct vivi_fh
*fh
=priv
;
872 if (fh
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
877 return videobuf_streamon(&fh
->vb_vidq
);
880 static int vidioc_streamoff(struct file
*file
, void *priv
, enum v4l2_buf_type i
)
882 struct vivi_fh
*fh
=priv
;
884 if (fh
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
889 return videobuf_streamoff(&fh
->vb_vidq
);
892 static int vidioc_s_std (struct file
*file
, void *priv
, v4l2_std_id
*i
)
897 /* only one input in this sample driver */
898 static int vidioc_enum_input (struct file
*file
, void *priv
,
899 struct v4l2_input
*inp
)
904 inp
->type
= V4L2_INPUT_TYPE_CAMERA
;
905 inp
->std
= V4L2_STD_NTSC_M
;
906 strcpy(inp
->name
,"Camera");
911 static int vidioc_g_input (struct file
*file
, void *priv
, unsigned int *i
)
917 static int vidioc_s_input (struct file
*file
, void *priv
, unsigned int i
)
925 /* --- controls ---------------------------------------------- */
926 static int vidioc_queryctrl (struct file
*file
, void *priv
,
927 struct v4l2_queryctrl
*qc
)
931 for (i
= 0; i
< ARRAY_SIZE(vivi_qctrl
); i
++)
932 if (qc
->id
&& qc
->id
== vivi_qctrl
[i
].id
) {
933 memcpy(qc
, &(vivi_qctrl
[i
]),
941 static int vidioc_g_ctrl (struct file
*file
, void *priv
,
942 struct v4l2_control
*ctrl
)
946 for (i
= 0; i
< ARRAY_SIZE(vivi_qctrl
); i
++)
947 if (ctrl
->id
== vivi_qctrl
[i
].id
) {
948 ctrl
->value
=qctl_regs
[i
];
954 static int vidioc_s_ctrl (struct file
*file
, void *priv
,
955 struct v4l2_control
*ctrl
)
959 for (i
= 0; i
< ARRAY_SIZE(vivi_qctrl
); i
++)
960 if (ctrl
->id
== vivi_qctrl
[i
].id
) {
962 vivi_qctrl
[i
].minimum
964 vivi_qctrl
[i
].maximum
) {
967 qctl_regs
[i
]=ctrl
->value
;
973 /* ------------------------------------------------------------------
974 File operations for the device
975 ------------------------------------------------------------------*/
977 #define line_buf_size(norm) (norm_maxw(norm)*(format.depth+7)/8)
979 static int vivi_open(struct inode
*inode
, struct file
*file
)
981 int minor
= iminor(inode
);
982 struct vivi_dev
*dev
;
986 printk(KERN_DEBUG
"vivi: open called (minor=%d)\n",minor
);
988 list_for_each_entry(dev
, &vivi_devlist
, vivi_devlist
)
989 if (dev
->vfd
.minor
== minor
)
996 /* If more than one user, mutex should be added */
999 dprintk(1, "open minor=%d type=%s users=%d\n", minor
,
1000 v4l2_type_names
[V4L2_BUF_TYPE_VIDEO_CAPTURE
], dev
->users
);
1002 /* allocate + initialize per filehandle data */
1003 fh
= kzalloc(sizeof(*fh
),GFP_KERNEL
);
1009 file
->private_data
= fh
;
1012 fh
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1017 /* Put all controls at a sane state */
1018 for (i
= 0; i
< ARRAY_SIZE(vivi_qctrl
); i
++)
1019 qctl_regs
[i
] =vivi_qctrl
[i
].default_value
;
1021 dprintk(1,"Open: fh=0x%08lx, dev=0x%08lx, dev->vidq=0x%08lx\n",
1022 (unsigned long)fh
,(unsigned long)dev
,(unsigned long)&dev
->vidq
);
1023 dprintk(1,"Open: list_empty queued=%d\n",list_empty(&dev
->vidq
.queued
));
1024 dprintk(1,"Open: list_empty active=%d\n",list_empty(&dev
->vidq
.active
));
1026 /* Resets frame counters */
1031 dev
->jiffies
=jiffies
;
1032 sprintf(dev
->timestr
,"%02d:%02d:%02d:%03d",
1033 dev
->h
,dev
->m
,dev
->s
,(dev
->us
+500)/1000);
1035 videobuf_queue_vmalloc_init(&fh
->vb_vidq
, &vivi_video_qops
,
1038 V4L2_FIELD_INTERLACED
,
1039 sizeof(struct vivi_buffer
),fh
);
1045 vivi_read(struct file
*file
, char __user
*data
, size_t count
, loff_t
*ppos
)
1047 struct vivi_fh
*fh
= file
->private_data
;
1049 if (fh
->type
==V4L2_BUF_TYPE_VIDEO_CAPTURE
) {
1050 return videobuf_read_stream(&fh
->vb_vidq
, data
, count
, ppos
, 0,
1051 file
->f_flags
& O_NONBLOCK
);
1057 vivi_poll(struct file
*file
, struct poll_table_struct
*wait
)
1059 struct vivi_fh
*fh
= file
->private_data
;
1060 struct videobuf_queue
*q
= &fh
->vb_vidq
;
1062 dprintk(1,"%s\n",__FUNCTION__
);
1064 if (V4L2_BUF_TYPE_VIDEO_CAPTURE
!= fh
->type
)
1067 return videobuf_poll_stream(file
, q
, wait
);
1070 static int vivi_release(struct inode
*inode
, struct file
*file
)
1072 struct vivi_fh
*fh
= file
->private_data
;
1073 struct vivi_dev
*dev
= fh
->dev
;
1074 struct vivi_dmaqueue
*vidq
= &dev
->vidq
;
1076 int minor
= iminor(inode
);
1078 vivi_stop_thread(vidq
);
1079 videobuf_stop(&fh
->vb_vidq
);
1080 videobuf_mmap_free(&fh
->vb_vidq
);
1086 printk(KERN_DEBUG
"vivi: close called (minor=%d, users=%d)\n",minor
,dev
->users
);
1092 vivi_mmap(struct file
*file
, struct vm_area_struct
* vma
)
1094 struct vivi_fh
*fh
= file
->private_data
;
1097 dprintk (1,"mmap called, vma=0x%08lx\n",(unsigned long)vma
);
1099 ret
=videobuf_mmap_mapper(&fh
->vb_vidq
, vma
);
1101 dprintk (1,"vma start=0x%08lx, size=%ld, ret=%d\n",
1102 (unsigned long)vma
->vm_start
,
1103 (unsigned long)vma
->vm_end
-(unsigned long)vma
->vm_start
,
1109 static const struct file_operations vivi_fops
= {
1110 .owner
= THIS_MODULE
,
1112 .release
= vivi_release
,
1115 .ioctl
= video_ioctl2
, /* V4L2 ioctl handler */
1117 .llseek
= no_llseek
,
1120 static struct video_device vivi
= {
1122 .type
= VID_TYPE_CAPTURE
,
1125 // .release = video_device_release,
1127 .vidioc_querycap
= vidioc_querycap
,
1128 .vidioc_enum_fmt_cap
= vidioc_enum_fmt_cap
,
1129 .vidioc_g_fmt_cap
= vidioc_g_fmt_cap
,
1130 .vidioc_try_fmt_cap
= vidioc_try_fmt_cap
,
1131 .vidioc_s_fmt_cap
= vidioc_s_fmt_cap
,
1132 .vidioc_reqbufs
= vidioc_reqbufs
,
1133 .vidioc_querybuf
= vidioc_querybuf
,
1134 .vidioc_qbuf
= vidioc_qbuf
,
1135 .vidioc_dqbuf
= vidioc_dqbuf
,
1136 .vidioc_s_std
= vidioc_s_std
,
1137 .vidioc_enum_input
= vidioc_enum_input
,
1138 .vidioc_g_input
= vidioc_g_input
,
1139 .vidioc_s_input
= vidioc_s_input
,
1140 .vidioc_queryctrl
= vidioc_queryctrl
,
1141 .vidioc_g_ctrl
= vidioc_g_ctrl
,
1142 .vidioc_s_ctrl
= vidioc_s_ctrl
,
1143 .vidioc_streamon
= vidioc_streamon
,
1144 .vidioc_streamoff
= vidioc_streamoff
,
1145 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1146 .vidiocgmbuf
= vidiocgmbuf
,
1148 .tvnorms
= V4L2_STD_NTSC_M
,
1149 .current_norm
= V4L2_STD_NTSC_M
,
1151 /* -----------------------------------------------------------------
1152 Initialization and module stuff
1153 ------------------------------------------------------------------*/
1155 static int __init
vivi_init(void)
1158 struct vivi_dev
*dev
;
1160 dev
= kzalloc(sizeof(*dev
),GFP_KERNEL
);
1163 list_add_tail(&dev
->vivi_devlist
,&vivi_devlist
);
1165 /* init video dma queues */
1166 INIT_LIST_HEAD(&dev
->vidq
.active
);
1167 INIT_LIST_HEAD(&dev
->vidq
.queued
);
1168 init_waitqueue_head(&dev
->vidq
.wq
);
1170 /* initialize locks */
1171 mutex_init(&dev
->lock
);
1173 dev
->vidq
.timeout
.function
= vivi_vid_timeout
;
1174 dev
->vidq
.timeout
.data
= (unsigned long)dev
;
1175 init_timer(&dev
->vidq
.timeout
);
1177 ret
= video_register_device(&vivi
, VFL_TYPE_GRABBER
, video_nr
);
1178 printk(KERN_INFO
"Video Technology Magazine Virtual Video Capture Board (Load status: %d)\n", ret
);
1182 static void __exit
vivi_exit(void)
1185 struct list_head
*list
;
1187 while (!list_empty(&vivi_devlist
)) {
1188 list
= vivi_devlist
.next
;
1190 h
= list_entry(list
, struct vivi_dev
, vivi_devlist
);
1193 video_unregister_device(&vivi
);
1196 module_init(vivi_init
);
1197 module_exit(vivi_exit
);
1199 MODULE_DESCRIPTION("Video Technology Magazine Virtual Video Capture Board");
1200 MODULE_AUTHOR("Mauro Carvalho Chehab, Ted Walther and John Sokol");
1201 MODULE_LICENSE("Dual BSD/GPL");
1203 module_param(video_nr
, int, 0);
1205 module_param_named(debug
,vivi
.debug
, int, 0644);
1206 MODULE_PARM_DESC(debug
,"activates debug info");
1208 module_param(vid_limit
,int,0644);
1209 MODULE_PARM_DESC(vid_limit
,"capture memory limit in megabytes");