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>
39 #include <linux/freezer.h>
41 /* Wake up at about 30 fps */
42 #define WAKE_NUMERATOR 30
43 #define WAKE_DENOMINATOR 1001
44 #define BUFFER_TIMEOUT msecs_to_jiffies(500) /* 0.5 seconds */
46 /* These timers are for 1 fps - used only for testing */
47 //#define WAKE_DENOMINATOR 30 /* hack for testing purposes */
48 //#define BUFFER_TIMEOUT msecs_to_jiffies(5000) /* 5 seconds */
52 #define VIVI_MAJOR_VERSION 0
53 #define VIVI_MINOR_VERSION 4
54 #define VIVI_RELEASE 0
55 #define VIVI_VERSION KERNEL_VERSION(VIVI_MAJOR_VERSION, VIVI_MINOR_VERSION, VIVI_RELEASE)
57 /* Declare static vars that will be used as parameters */
58 static unsigned int vid_limit
= 16; /* Video memory limit, in Mb */
59 static struct video_device vivi
; /* Video device */
60 static int video_nr
= -1; /* /dev/videoN, -1 for autodetect */
62 /* supported controls */
63 static struct v4l2_queryctrl vivi_qctrl
[] = {
65 .id
= V4L2_CID_AUDIO_VOLUME
,
70 .default_value
= 65535,
72 .type
= V4L2_CTRL_TYPE_INTEGER
,
74 .id
= V4L2_CID_BRIGHTNESS
,
75 .type
= V4L2_CTRL_TYPE_INTEGER
,
83 .id
= V4L2_CID_CONTRAST
,
84 .type
= V4L2_CTRL_TYPE_INTEGER
,
89 .default_value
= 0x10,
92 .id
= V4L2_CID_SATURATION
,
93 .type
= V4L2_CTRL_TYPE_INTEGER
,
102 .type
= V4L2_CTRL_TYPE_INTEGER
,
112 static int qctl_regs
[ARRAY_SIZE(vivi_qctrl
)];
114 #define dprintk(level,fmt, arg...) \
116 if (vivi.debug >= (level)) \
117 printk(KERN_DEBUG "vivi: " fmt , ## arg); \
120 /* ------------------------------------------------------------------
122 ------------------------------------------------------------------*/
126 u32 fourcc
; /* v4l2 format id */
130 static struct vivi_fmt format
= {
131 .name
= "4:2:2, packed, YUYV",
132 .fourcc
= V4L2_PIX_FMT_YUYV
,
138 struct scatterlist
*sg
;
141 /* buffer for one video frame */
143 /* common v4l buffer stuff -- must be first */
144 struct videobuf_buffer vb
;
146 struct vivi_fmt
*fmt
;
148 #ifdef CONFIG_VIVI_SCATTER
149 struct sg_to_addr
*to_addr
;
153 struct vivi_dmaqueue
{
154 struct list_head active
;
155 struct list_head queued
;
156 struct timer_list timeout
;
158 /* thread for generating video stream*/
159 struct task_struct
*kthread
;
160 wait_queue_head_t wq
;
161 /* Counters to control fps rate */
166 static LIST_HEAD(vivi_devlist
);
169 struct list_head vivi_devlist
;
171 struct semaphore lock
;
175 /* various device info */
176 unsigned int resources
;
177 struct video_device vfd
;
179 struct vivi_dmaqueue vidq
;
181 /* Several counters */
182 int h
,m
,s
,us
,jiffies
;
187 struct vivi_dev
*dev
;
190 struct vivi_fmt
*fmt
;
191 unsigned int width
,height
;
192 struct videobuf_queue vb_vidq
;
194 enum v4l2_buf_type type
;
197 /* ------------------------------------------------------------------
198 DMA and thread functions
199 ------------------------------------------------------------------*/
201 /* Bars and Colors should match positions */
213 static u8 bars
[8][3] = {
215 {204,204,204}, /* white */
216 {208,208, 0}, /* ambar */
217 { 0,206,206}, /* cyan */
218 { 0,239, 0}, /* green */
219 {239, 0,239}, /* magenta */
220 {205, 0, 0}, /* red */
221 { 0, 0,255}, /* blue */
225 #define TO_Y(r,g,b) (((16829*r +33039*g +6416*b + 32768)>>16)+16)
226 /* RGB to V(Cr) Color transform */
227 #define TO_V(r,g,b) (((28784*r -24103*g -4681*b + 32768)>>16)+128)
228 /* RGB to U(Cb) Color transform */
229 #define TO_U(r,g,b) (((-9714*r -19070*g +28784*b + 32768)>>16)+128)
231 #define TSTAMP_MIN_Y 24
232 #define TSTAMP_MAX_Y TSTAMP_MIN_Y+15
233 #define TSTAMP_MIN_X 64
235 #ifdef CONFIG_VIVI_SCATTER
236 static void prep_to_addr(struct sg_to_addr to_addr
[],
237 struct videobuf_buffer
*vb
)
241 for (i
=0;i
<vb
->dma
.nr_pages
;i
++) {
242 to_addr
[i
].sg
=&vb
->dma
.sglist
[i
];
244 pos
+= vb
->dma
.sglist
[i
].length
;
248 static int get_addr_pos(int pos
, int pages
, struct sg_to_addr to_addr
[])
250 int p1
=0,p2
=pages
-1,p3
=pages
/2;
253 BUG_ON (pos
>=to_addr
[p2
].pos
+to_addr
[p2
].sg
->length
);
256 if (pos
< to_addr
[p3
].pos
) {
263 if (pos
>= to_addr
[p2
].pos
)
270 #ifdef CONFIG_VIVI_SCATTER
271 static void gen_line(struct sg_to_addr to_addr
[],int inipos
,int pages
,int wmax
,
272 int hmax
, int line
, char *timestr
)
274 static void gen_line(char *basep
,int inipos
,int wmax
,
275 int hmax
, int line
, char *timestr
)
278 int w
,i
,j
,pos
=inipos
,y
;
281 #ifdef CONFIG_VIVI_SCATTER
289 spin_lock_init(&spinlock
);
291 /* Get first addr pointed to pixel position */
292 oldpg
=get_addr_pos(pos
,pages
,to_addr
);
293 pg
=pfn_to_page(sg_dma_address(to_addr
[oldpg
].sg
) >> PAGE_SHIFT
);
294 spin_lock_irqsave(&spinlock
,flags
);
295 basep
= kmap_atomic(pg
, KM_BOUNCE_READ
)+to_addr
[oldpg
].sg
->offset
;
298 /* We will just duplicate the second pixel at the packet */
301 /* Generate a standard color bar pattern */
302 for (w
=0;w
<wmax
;w
++) {
307 for (color
=0;color
<4;color
++) {
308 #ifdef CONFIG_VIVI_SCATTER
309 pgpos
=get_addr_pos(pos
,pages
,to_addr
);
311 pg
=pfn_to_page(sg_dma_address(to_addr
[pgpos
].sg
) >> PAGE_SHIFT
);
312 kunmap_atomic(basep
, KM_BOUNCE_READ
);
313 basep
= kmap_atomic(pg
, KM_BOUNCE_READ
)+to_addr
[pgpos
].sg
->offset
;
316 p
=basep
+pos
-to_addr
[pgpos
].pos
;
324 *p
=TO_Y(r
,g
,b
); /* Luminance */
327 *p
=TO_U(r
,g
,b
); /* Cb */
330 *p
=TO_V(r
,g
,b
); /* Cr */
337 /* Checks if it is possible to show timestamp */
338 if (TSTAMP_MAX_Y
>=hmax
)
340 if (TSTAMP_MIN_X
+strlen(timestr
)>=wmax
)
343 /* Print stream time */
344 if (line
>=TSTAMP_MIN_Y
&& line
<=TSTAMP_MAX_Y
) {
346 for (s
=timestr
;*s
;s
++) {
347 chr
=rom8x16_bits
[(*s
-0x30)*16+line
-TSTAMP_MIN_Y
];
349 if (chr
&1<<(7-i
)) { /* Font color*/
355 } else { /* Background color */
363 for (color
=0;color
<4;color
++) {
364 #ifdef CONFIG_VIVI_SCATTER
365 pgpos
=get_addr_pos(pos
,pages
,to_addr
);
367 pg
=pfn_to_page(sg_dma_address(
372 basep
= kmap_atomic(pg
,
374 to_addr
[pgpos
].sg
->offset
;
377 p
=basep
+pos
-to_addr
[pgpos
].pos
;
387 *p
=TO_Y(r
,g
,b
); /* Luminance */
390 *p
=TO_U(r
,g
,b
); /* Cb */
393 *p
=TO_V(r
,g
,b
); /* Cr */
405 #ifdef CONFIG_VIVI_SCATTER
406 kunmap_atomic(basep
, KM_BOUNCE_READ
);
407 spin_unlock_irqrestore(&spinlock
,flags
);
412 static void vivi_fillbuff(struct vivi_dev
*dev
,struct vivi_buffer
*buf
)
415 int hmax
= buf
->vb
.height
;
416 int wmax
= buf
->vb
.width
;
418 #ifdef CONFIG_VIVI_SCATTER
419 struct sg_to_addr
*to_addr
=buf
->to_addr
;
420 struct videobuf_buffer
*vb
=&buf
->vb
;
425 #ifdef CONFIG_VIVI_SCATTER
426 /* Test if DMA mapping is ready */
427 if (!sg_dma_address(&vb
->dma
.sglist
[0]))
430 prep_to_addr(to_addr
,vb
);
432 /* Check if there is enough memory */
433 BUG_ON(buf
->vb
.dma
.nr_pages
<< PAGE_SHIFT
< (buf
->vb
.width
*buf
->vb
.height
)*2);
435 if (buf
->vb
.dma
.varea
) {
436 tmpbuf
=kmalloc (wmax
*2, GFP_KERNEL
);
438 tmpbuf
=buf
->vb
.dma
.vmalloc
;
443 for (h
=0;h
<hmax
;h
++) {
444 #ifdef CONFIG_VIVI_SCATTER
445 gen_line(to_addr
,pos
,vb
->dma
.nr_pages
,wmax
,hmax
,h
,dev
->timestr
);
447 if (buf
->vb
.dma
.varea
) {
448 gen_line(tmpbuf
,0,wmax
,hmax
,h
,dev
->timestr
);
449 /* FIXME: replacing to __copy_to_user */
450 if (copy_to_user(buf
->vb
.dma
.varea
+pos
,tmpbuf
,wmax
*2)!=0)
451 dprintk(2,"vivifill copy_to_user failed.\n");
453 gen_line(tmpbuf
,pos
,wmax
,hmax
,h
,dev
->timestr
);
459 /* Updates stream time */
461 dev
->us
+=jiffies_to_usecs(jiffies
-dev
->jiffies
);
462 dev
->jiffies
=jiffies
;
463 if (dev
->us
>=1000000) {
477 sprintf(dev
->timestr
,"%02d:%02d:%02d:%03d",
478 dev
->h
,dev
->m
,dev
->s
,(dev
->us
+500)/1000);
480 dprintk(2,"vivifill at %s: Buffer 0x%08lx size= %d\n",dev
->timestr
,
481 (unsigned long)buf
->vb
.dma
.varea
,pos
);
483 /* Advice that buffer was filled */
484 buf
->vb
.state
= STATE_DONE
;
485 buf
->vb
.field_count
++;
486 do_gettimeofday(&ts
);
489 list_del(&buf
->vb
.queue
);
490 wake_up(&buf
->vb
.done
);
493 static int restart_video_queue(struct vivi_dmaqueue
*dma_q
);
495 static void vivi_thread_tick(struct vivi_dmaqueue
*dma_q
)
497 struct vivi_buffer
*buf
;
498 struct vivi_dev
*dev
= container_of(dma_q
,struct vivi_dev
,vidq
);
502 /* Announces videobuf that all went ok */
503 for (bc
= 0;; bc
++) {
504 if (list_empty(&dma_q
->active
)) {
505 dprintk(1,"No active queue to serve\n");
509 buf
= list_entry(dma_q
->active
.next
,
510 struct vivi_buffer
, vb
.queue
);
512 /* Nobody is waiting something to be done, just return */
513 if (!waitqueue_active(&buf
->vb
.done
)) {
514 mod_timer(&dma_q
->timeout
, jiffies
+BUFFER_TIMEOUT
);
518 do_gettimeofday(&buf
->vb
.ts
);
519 dprintk(2,"[%p/%d] wakeup\n",buf
,buf
->vb
.i
);
522 vivi_fillbuff(dev
,buf
);
524 if (list_empty(&dma_q
->active
)) {
525 del_timer(&dma_q
->timeout
);
527 mod_timer(&dma_q
->timeout
, jiffies
+BUFFER_TIMEOUT
);
531 dprintk(1,"%s: %d buffers handled (should be 1)\n",__FUNCTION__
,bc
);
534 static void vivi_sleep(struct vivi_dmaqueue
*dma_q
)
537 DECLARE_WAITQUEUE(wait
, current
);
539 dprintk(1,"%s dma_q=0x%08lx\n",__FUNCTION__
,(unsigned long)dma_q
);
541 add_wait_queue(&dma_q
->wq
, &wait
);
542 if (!kthread_should_stop()) {
545 /* Calculate time to wake up */
546 timeout
=dma_q
->ini_jiffies
+msecs_to_jiffies((dma_q
->frame
*WAKE_NUMERATOR
*1000)/WAKE_DENOMINATOR
)-jiffies
;
549 int old
=dma_q
->frame
;
550 dma_q
->frame
=(jiffies_to_msecs(jiffies
-dma_q
->ini_jiffies
)*WAKE_DENOMINATOR
)/(WAKE_NUMERATOR
*1000)+1;
552 timeout
=dma_q
->ini_jiffies
+msecs_to_jiffies((dma_q
->frame
*WAKE_NUMERATOR
*1000)/WAKE_DENOMINATOR
)-jiffies
;
554 dprintk(1,"underrun, losed %d frames. "
555 "Now, frame is %d. Waking on %d jiffies\n",
556 dma_q
->frame
-old
,dma_q
->frame
,timeout
);
558 dprintk(1,"will sleep for %i jiffies\n",timeout
);
560 vivi_thread_tick(dma_q
);
562 schedule_timeout_interruptible (timeout
);
565 remove_wait_queue(&dma_q
->wq
, &wait
);
569 static int vivi_thread(void *data
)
571 struct vivi_dmaqueue
*dma_q
=data
;
573 dprintk(1,"thread started\n");
575 mod_timer(&dma_q
->timeout
, jiffies
+BUFFER_TIMEOUT
);
580 if (kthread_should_stop())
583 dprintk(1, "thread: exit\n");
587 static int vivi_start_thread(struct vivi_dmaqueue
*dma_q
)
590 dma_q
->ini_jiffies
=jiffies
;
592 dprintk(1,"%s\n",__FUNCTION__
);
594 dma_q
->kthread
= kthread_run(vivi_thread
, dma_q
, "vivi");
596 if (IS_ERR(dma_q
->kthread
)) {
597 printk(KERN_ERR
"vivi: kernel_thread() failed\n");
598 return PTR_ERR(dma_q
->kthread
);
601 wake_up_interruptible(&dma_q
->wq
);
603 dprintk(1,"returning from %s\n",__FUNCTION__
);
607 static void vivi_stop_thread(struct vivi_dmaqueue
*dma_q
)
609 dprintk(1,"%s\n",__FUNCTION__
);
610 /* shutdown control thread */
611 if (dma_q
->kthread
) {
612 kthread_stop(dma_q
->kthread
);
617 static int restart_video_queue(struct vivi_dmaqueue
*dma_q
)
619 struct vivi_buffer
*buf
, *prev
;
620 struct list_head
*item
;
622 dprintk(1,"%s dma_q=0x%08lx\n",__FUNCTION__
,(unsigned long)dma_q
);
624 if (!list_empty(&dma_q
->active
)) {
625 buf
= list_entry(dma_q
->active
.next
, struct vivi_buffer
, vb
.queue
);
626 dprintk(2,"restart_queue [%p/%d]: restart dma\n",
629 dprintk(1,"Restarting video dma\n");
630 vivi_stop_thread(dma_q
);
631 // vivi_start_thread(dma_q);
633 /* cancel all outstanding capture / vbi requests */
634 list_for_each(item
,&dma_q
->active
) {
635 buf
= list_entry(item
, struct vivi_buffer
, vb
.queue
);
637 list_del(&buf
->vb
.queue
);
638 buf
->vb
.state
= STATE_ERROR
;
639 wake_up(&buf
->vb
.done
);
641 mod_timer(&dma_q
->timeout
, jiffies
+BUFFER_TIMEOUT
);
648 if (list_empty(&dma_q
->queued
))
650 buf
= list_entry(dma_q
->queued
.next
, struct vivi_buffer
, vb
.queue
);
652 list_del(&buf
->vb
.queue
);
653 list_add_tail(&buf
->vb
.queue
,&dma_q
->active
);
655 dprintk(1,"Restarting video dma\n");
656 vivi_stop_thread(dma_q
);
657 vivi_start_thread(dma_q
);
659 buf
->vb
.state
= STATE_ACTIVE
;
660 mod_timer(&dma_q
->timeout
, jiffies
+BUFFER_TIMEOUT
);
661 dprintk(2,"[%p/%d] restart_queue - first active\n",
664 } else if (prev
->vb
.width
== buf
->vb
.width
&&
665 prev
->vb
.height
== buf
->vb
.height
&&
666 prev
->fmt
== buf
->fmt
) {
667 list_del(&buf
->vb
.queue
);
668 list_add_tail(&buf
->vb
.queue
,&dma_q
->active
);
669 buf
->vb
.state
= STATE_ACTIVE
;
670 dprintk(2,"[%p/%d] restart_queue - move to active\n",
679 static void vivi_vid_timeout(unsigned long data
)
681 struct vivi_dev
*dev
= (struct vivi_dev
*)data
;
682 struct vivi_dmaqueue
*vidq
= &dev
->vidq
;
683 struct vivi_buffer
*buf
;
685 while (!list_empty(&vidq
->active
)) {
686 buf
= list_entry(vidq
->active
.next
, struct vivi_buffer
, vb
.queue
);
687 list_del(&buf
->vb
.queue
);
688 buf
->vb
.state
= STATE_ERROR
;
689 wake_up(&buf
->vb
.done
);
690 printk("vivi/0: [%p/%d] timeout\n", buf
, buf
->vb
.i
);
693 restart_video_queue(vidq
);
696 /* ------------------------------------------------------------------
698 ------------------------------------------------------------------*/
700 buffer_setup(struct videobuf_queue
*vq
, unsigned int *count
, unsigned int *size
)
702 struct vivi_fh
*fh
= vq
->priv_data
;
704 *size
= fh
->width
*fh
->height
*2;
708 while (*size
* *count
> vid_limit
* 1024 * 1024)
713 static void free_buffer(struct videobuf_queue
*vq
, struct vivi_buffer
*buf
)
715 dprintk(1,"%s\n",__FUNCTION__
);
720 #ifdef CONFIG_VIVI_SCATTER
721 /*FIXME: Maybe a spinlock is required here */
726 videobuf_waiton(&buf
->vb
,0,0);
727 videobuf_dma_unmap(vq
, &buf
->vb
.dma
);
728 videobuf_dma_free(&buf
->vb
.dma
);
729 buf
->vb
.state
= STATE_NEEDS_INIT
;
732 #define norm_maxw() 1024
733 #define norm_maxh() 768
735 buffer_prepare(struct videobuf_queue
*vq
, struct videobuf_buffer
*vb
,
736 enum v4l2_field field
)
738 struct vivi_fh
*fh
= vq
->priv_data
;
739 struct vivi_buffer
*buf
= container_of(vb
,struct vivi_buffer
,vb
);
740 int rc
, init_buffer
= 0;
742 // dprintk(1,"%s, field=%d\n",__FUNCTION__,field);
744 BUG_ON(NULL
== fh
->fmt
);
745 if (fh
->width
< 48 || fh
->width
> norm_maxw() ||
746 fh
->height
< 32 || fh
->height
> norm_maxh())
748 buf
->vb
.size
= fh
->width
*fh
->height
*2;
749 if (0 != buf
->vb
.baddr
&& buf
->vb
.bsize
< buf
->vb
.size
)
752 if (buf
->fmt
!= fh
->fmt
||
753 buf
->vb
.width
!= fh
->width
||
754 buf
->vb
.height
!= fh
->height
||
755 buf
->vb
.field
!= field
) {
757 buf
->vb
.width
= fh
->width
;
758 buf
->vb
.height
= fh
->height
;
759 buf
->vb
.field
= field
;
763 if (STATE_NEEDS_INIT
== buf
->vb
.state
) {
764 if (0 != (rc
= videobuf_iolock(vq
,&buf
->vb
,NULL
)))
768 buf
->vb
.state
= STATE_PREPARED
;
770 #ifdef CONFIG_VIVI_SCATTER
771 if (NULL
== (buf
->to_addr
= kmalloc(sizeof(*buf
->to_addr
) * vb
->dma
.nr_pages
,GFP_KERNEL
))) {
784 buffer_queue(struct videobuf_queue
*vq
, struct videobuf_buffer
*vb
)
786 struct vivi_buffer
*buf
= container_of(vb
,struct vivi_buffer
,vb
);
787 struct vivi_fh
*fh
= vq
->priv_data
;
788 struct vivi_dev
*dev
= fh
->dev
;
789 struct vivi_dmaqueue
*vidq
= &dev
->vidq
;
790 struct vivi_buffer
*prev
;
792 if (!list_empty(&vidq
->queued
)) {
793 dprintk(1,"adding vb queue=0x%08lx\n",(unsigned long)&buf
->vb
.queue
);
794 list_add_tail(&buf
->vb
.queue
,&vidq
->queued
);
795 buf
->vb
.state
= STATE_QUEUED
;
796 dprintk(2,"[%p/%d] buffer_queue - append to queued\n",
798 } else if (list_empty(&vidq
->active
)) {
799 list_add_tail(&buf
->vb
.queue
,&vidq
->active
);
801 buf
->vb
.state
= STATE_ACTIVE
;
802 mod_timer(&vidq
->timeout
, jiffies
+BUFFER_TIMEOUT
);
803 dprintk(2,"[%p/%d] buffer_queue - first active\n",
806 vivi_start_thread(vidq
);
808 prev
= list_entry(vidq
->active
.prev
, struct vivi_buffer
, vb
.queue
);
809 if (prev
->vb
.width
== buf
->vb
.width
&&
810 prev
->vb
.height
== buf
->vb
.height
&&
811 prev
->fmt
== buf
->fmt
) {
812 list_add_tail(&buf
->vb
.queue
,&vidq
->active
);
813 buf
->vb
.state
= STATE_ACTIVE
;
814 dprintk(2,"[%p/%d] buffer_queue - append to active\n",
818 list_add_tail(&buf
->vb
.queue
,&vidq
->queued
);
819 buf
->vb
.state
= STATE_QUEUED
;
820 dprintk(2,"[%p/%d] buffer_queue - first queued\n",
826 static void buffer_release(struct videobuf_queue
*vq
, struct videobuf_buffer
*vb
)
828 struct vivi_buffer
*buf
= container_of(vb
,struct vivi_buffer
,vb
);
829 struct vivi_fh
*fh
= vq
->priv_data
;
830 struct vivi_dev
*dev
= (struct vivi_dev
*)fh
->dev
;
831 struct vivi_dmaqueue
*vidq
= &dev
->vidq
;
833 dprintk(1,"%s\n",__FUNCTION__
);
835 vivi_stop_thread(vidq
);
840 #ifdef CONFIG_VIVI_SCATTER
841 static int vivi_map_sg(void *dev
, struct scatterlist
*sg
, int nents
,
846 dprintk(1,"%s, number of pages=%d\n",__FUNCTION__
,nents
);
847 BUG_ON(direction
== DMA_NONE
);
849 for (i
= 0; i
< nents
; i
++ ) {
852 sg_dma_address(&sg
[i
]) = page_to_phys(sg
[i
].page
) + sg
[i
].offset
;
858 static int vivi_unmap_sg(void *dev
,struct scatterlist
*sglist
,int nr_pages
,
861 dprintk(1,"%s\n",__FUNCTION__
);
865 static int vivi_dma_sync_sg(void *dev
,struct scatterlist
*sglist
, int nr_pages
,
868 // dprintk(1,"%s\n",__FUNCTION__);
870 // flush_write_buffers();
875 static struct videobuf_queue_ops vivi_video_qops
= {
876 .buf_setup
= buffer_setup
,
877 .buf_prepare
= buffer_prepare
,
878 .buf_queue
= buffer_queue
,
879 .buf_release
= buffer_release
,
881 /* Non-pci handling routines */
882 // .vb_map_sg = vivi_map_sg,
883 // .vb_dma_sync_sg = vivi_dma_sync_sg,
884 // .vb_unmap_sg = vivi_unmap_sg,
887 /* ------------------------------------------------------------------
889 ------------------------------------------------------------------*/
892 static int res_get(struct vivi_dev
*dev
, struct vivi_fh
*fh
)
896 if (dev
->resources
) {
897 /* no, someone else uses it */
901 /* it's free, grab it */
903 dprintk(1,"res: get\n");
908 static int res_locked(struct vivi_dev
*dev
)
910 return (dev
->resources
);
913 static void res_free(struct vivi_dev
*dev
, struct vivi_fh
*fh
)
917 dprintk(1,"res: put\n");
921 /* ------------------------------------------------------------------
922 IOCTL vidioc handling
923 ------------------------------------------------------------------*/
924 static int vidioc_querycap (struct file
*file
, void *priv
,
925 struct v4l2_capability
*cap
)
927 strcpy(cap
->driver
, "vivi");
928 strcpy(cap
->card
, "vivi");
929 cap
->version
= VIVI_VERSION
;
930 cap
->capabilities
= V4L2_CAP_VIDEO_CAPTURE
|
936 static int vidioc_enum_fmt_cap (struct file
*file
, void *priv
,
937 struct v4l2_fmtdesc
*f
)
942 strlcpy(f
->description
,format
.name
,sizeof(f
->description
));
943 f
->pixelformat
= format
.fourcc
;
947 static int vidioc_g_fmt_cap (struct file
*file
, void *priv
,
948 struct v4l2_format
*f
)
950 struct vivi_fh
*fh
=priv
;
952 f
->fmt
.pix
.width
= fh
->width
;
953 f
->fmt
.pix
.height
= fh
->height
;
954 f
->fmt
.pix
.field
= fh
->vb_vidq
.field
;
955 f
->fmt
.pix
.pixelformat
= fh
->fmt
->fourcc
;
956 f
->fmt
.pix
.bytesperline
=
957 (f
->fmt
.pix
.width
* fh
->fmt
->depth
) >> 3;
958 f
->fmt
.pix
.sizeimage
=
959 f
->fmt
.pix
.height
* f
->fmt
.pix
.bytesperline
;
964 static int vidioc_try_fmt_cap (struct file
*file
, void *priv
,
965 struct v4l2_format
*f
)
967 struct vivi_fmt
*fmt
;
968 enum v4l2_field field
;
969 unsigned int maxw
, maxh
;
971 if (format
.fourcc
!= f
->fmt
.pix
.pixelformat
) {
972 dprintk(1,"Fourcc format (0x%08x) invalid. Driver accepts "
973 "only 0x%08x\n",f
->fmt
.pix
.pixelformat
,format
.fourcc
);
978 field
= f
->fmt
.pix
.field
;
980 if (field
== V4L2_FIELD_ANY
) {
981 // field=V4L2_FIELD_INTERLACED;
982 field
=V4L2_FIELD_SEQ_TB
;
983 } else if (V4L2_FIELD_INTERLACED
!= field
) {
984 dprintk(1,"Field type invalid.\n");
991 f
->fmt
.pix
.field
= field
;
992 if (f
->fmt
.pix
.height
< 32)
993 f
->fmt
.pix
.height
= 32;
994 if (f
->fmt
.pix
.height
> maxh
)
995 f
->fmt
.pix
.height
= maxh
;
996 if (f
->fmt
.pix
.width
< 48)
997 f
->fmt
.pix
.width
= 48;
998 if (f
->fmt
.pix
.width
> maxw
)
999 f
->fmt
.pix
.width
= maxw
;
1000 f
->fmt
.pix
.width
&= ~0x03;
1001 f
->fmt
.pix
.bytesperline
=
1002 (f
->fmt
.pix
.width
* fmt
->depth
) >> 3;
1003 f
->fmt
.pix
.sizeimage
=
1004 f
->fmt
.pix
.height
* f
->fmt
.pix
.bytesperline
;
1009 /*FIXME: This seems to be generic enough to be at videodev2 */
1010 static int vidioc_s_fmt_cap (struct file
*file
, void *priv
,
1011 struct v4l2_format
*f
)
1013 struct vivi_fh
*fh
=priv
;
1014 int ret
= vidioc_try_fmt_cap(file
,fh
,f
);
1019 fh
->width
= f
->fmt
.pix
.width
;
1020 fh
->height
= f
->fmt
.pix
.height
;
1021 fh
->vb_vidq
.field
= f
->fmt
.pix
.field
;
1027 static int vidioc_reqbufs (struct file
*file
, void *priv
, struct v4l2_requestbuffers
*p
)
1029 struct vivi_fh
*fh
=priv
;
1031 return (videobuf_reqbufs(&fh
->vb_vidq
, p
));
1034 static int vidioc_querybuf (struct file
*file
, void *priv
, struct v4l2_buffer
*p
)
1036 struct vivi_fh
*fh
=priv
;
1038 return (videobuf_querybuf(&fh
->vb_vidq
, p
));
1041 static int vidioc_qbuf (struct file
*file
, void *priv
, struct v4l2_buffer
*p
)
1043 struct vivi_fh
*fh
=priv
;
1045 return (videobuf_qbuf(&fh
->vb_vidq
, p
));
1048 static int vidioc_dqbuf (struct file
*file
, void *priv
, struct v4l2_buffer
*p
)
1050 struct vivi_fh
*fh
=priv
;
1052 return (videobuf_dqbuf(&fh
->vb_vidq
, p
,
1053 file
->f_flags
& O_NONBLOCK
));
1056 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1057 static int vidiocgmbuf (struct file
*file
, void *priv
, struct video_mbuf
*mbuf
)
1059 struct vivi_fh
*fh
=priv
;
1060 struct videobuf_queue
*q
=&fh
->vb_vidq
;
1061 struct v4l2_requestbuffers req
;
1067 req
.memory
= V4L2_MEMORY_MMAP
;
1068 ret
= videobuf_reqbufs(q
,&req
);
1072 mbuf
->frames
= req
.count
;
1074 for (i
= 0; i
< mbuf
->frames
; i
++) {
1075 mbuf
->offsets
[i
] = q
->bufs
[i
]->boff
;
1076 mbuf
->size
+= q
->bufs
[i
]->bsize
;
1082 static int vidioc_streamon(struct file
*file
, void *priv
, enum v4l2_buf_type i
)
1084 struct vivi_fh
*fh
=priv
;
1085 struct vivi_dev
*dev
= fh
->dev
;
1087 if (fh
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1092 if (!res_get(dev
,fh
))
1094 return (videobuf_streamon(&fh
->vb_vidq
));
1097 static int vidioc_streamoff(struct file
*file
, void *priv
, enum v4l2_buf_type i
)
1099 struct vivi_fh
*fh
=priv
;
1100 struct vivi_dev
*dev
= fh
->dev
;
1102 if (fh
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1107 videobuf_streamoff(&fh
->vb_vidq
);
1113 static int vidioc_s_std (struct file
*file
, void *priv
, v4l2_std_id
*i
)
1118 /* only one input in this sample driver */
1119 static int vidioc_enum_input (struct file
*file
, void *priv
,
1120 struct v4l2_input
*inp
)
1122 if (inp
->index
!= 0)
1125 inp
->type
= V4L2_INPUT_TYPE_CAMERA
;
1126 inp
->std
= V4L2_STD_NTSC_M
;
1127 strcpy(inp
->name
,"Camera");
1132 static int vidioc_g_input (struct file
*file
, void *priv
, unsigned int *i
)
1138 static int vidioc_s_input (struct file
*file
, void *priv
, unsigned int i
)
1146 /* --- controls ---------------------------------------------- */
1147 static int vidioc_queryctrl (struct file
*file
, void *priv
,
1148 struct v4l2_queryctrl
*qc
)
1152 for (i
= 0; i
< ARRAY_SIZE(vivi_qctrl
); i
++)
1153 if (qc
->id
&& qc
->id
== vivi_qctrl
[i
].id
) {
1154 memcpy(qc
, &(vivi_qctrl
[i
]),
1162 static int vidioc_g_ctrl (struct file
*file
, void *priv
,
1163 struct v4l2_control
*ctrl
)
1167 for (i
= 0; i
< ARRAY_SIZE(vivi_qctrl
); i
++)
1168 if (ctrl
->id
== vivi_qctrl
[i
].id
) {
1169 ctrl
->value
=qctl_regs
[i
];
1175 static int vidioc_s_ctrl (struct file
*file
, void *priv
,
1176 struct v4l2_control
*ctrl
)
1180 for (i
= 0; i
< ARRAY_SIZE(vivi_qctrl
); i
++)
1181 if (ctrl
->id
== vivi_qctrl
[i
].id
) {
1183 vivi_qctrl
[i
].minimum
1185 vivi_qctrl
[i
].maximum
) {
1188 qctl_regs
[i
]=ctrl
->value
;
1194 /* ------------------------------------------------------------------
1195 File operations for the device
1196 ------------------------------------------------------------------*/
1198 #define line_buf_size(norm) (norm_maxw(norm)*(format.depth+7)/8)
1200 static int vivi_open(struct inode
*inode
, struct file
*file
)
1202 int minor
= iminor(inode
);
1203 struct vivi_dev
*h
,*dev
= NULL
;
1205 struct list_head
*list
;
1206 enum v4l2_buf_type type
= 0;
1209 printk(KERN_DEBUG
"vivi: open called (minor=%d)\n",minor
);
1211 list_for_each(list
,&vivi_devlist
) {
1212 h
= list_entry(list
, struct vivi_dev
, vivi_devlist
);
1213 if (h
->vfd
.minor
== minor
) {
1215 type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1223 /* If more than one user, mutex should be added */
1226 dprintk(1,"open minor=%d type=%s users=%d\n",
1227 minor
,v4l2_type_names
[type
],dev
->users
);
1229 /* allocate + initialize per filehandle data */
1230 fh
= kzalloc(sizeof(*fh
),GFP_KERNEL
);
1236 file
->private_data
= fh
;
1239 fh
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1244 /* Put all controls at a sane state */
1245 for (i
= 0; i
< ARRAY_SIZE(vivi_qctrl
); i
++)
1246 qctl_regs
[i
] =vivi_qctrl
[i
].default_value
;
1248 dprintk(1,"Open: fh=0x%08lx, dev=0x%08lx, dev->vidq=0x%08lx\n",
1249 (unsigned long)fh
,(unsigned long)dev
,(unsigned long)&dev
->vidq
);
1250 dprintk(1,"Open: list_empty queued=%d\n",list_empty(&dev
->vidq
.queued
));
1251 dprintk(1,"Open: list_empty active=%d\n",list_empty(&dev
->vidq
.active
));
1253 /* Resets frame counters */
1258 dev
->jiffies
=jiffies
;
1259 sprintf(dev
->timestr
,"%02d:%02d:%02d:%03d",
1260 dev
->h
,dev
->m
,dev
->s
,(dev
->us
+500)/1000);
1262 #ifdef CONFIG_VIVI_SCATTER
1263 videobuf_queue_init(&fh
->vb_vidq
,VIDEOBUF_DMA_SCATTER
, &vivi_video_qops
,
1266 V4L2_FIELD_INTERLACED
,
1267 sizeof(struct vivi_buffer
),fh
);
1269 videobuf_queue_init(&fh
->vb_vidq
, &vivi_video_qops
,
1272 V4L2_FIELD_INTERLACED
,
1273 sizeof(struct vivi_buffer
),fh
);
1280 vivi_read(struct file
*file
, char __user
*data
, size_t count
, loff_t
*ppos
)
1282 struct vivi_fh
*fh
= file
->private_data
;
1284 if (fh
->type
==V4L2_BUF_TYPE_VIDEO_CAPTURE
) {
1285 if (res_locked(fh
->dev
))
1287 return videobuf_read_one(&fh
->vb_vidq
, data
, count
, ppos
,
1288 file
->f_flags
& O_NONBLOCK
);
1294 vivi_poll(struct file
*file
, struct poll_table_struct
*wait
)
1296 struct vivi_fh
*fh
= file
->private_data
;
1297 struct vivi_buffer
*buf
;
1299 dprintk(1,"%s\n",__FUNCTION__
);
1301 if (V4L2_BUF_TYPE_VIDEO_CAPTURE
!= fh
->type
)
1304 if (res_get(fh
->dev
,fh
)) {
1305 dprintk(1,"poll: mmap interface\n");
1306 /* streaming capture */
1307 if (list_empty(&fh
->vb_vidq
.stream
))
1309 buf
= list_entry(fh
->vb_vidq
.stream
.next
,struct vivi_buffer
,vb
.stream
);
1311 dprintk(1,"poll: read() interface\n");
1312 /* read() capture */
1313 buf
= (struct vivi_buffer
*)fh
->vb_vidq
.read_buf
;
1317 poll_wait(file
, &buf
->vb
.done
, wait
);
1318 if (buf
->vb
.state
== STATE_DONE
||
1319 buf
->vb
.state
== STATE_ERROR
)
1320 return POLLIN
|POLLRDNORM
;
1324 static int vivi_release(struct inode
*inode
, struct file
*file
)
1326 struct vivi_fh
*fh
= file
->private_data
;
1327 struct vivi_dev
*dev
= fh
->dev
;
1328 struct vivi_dmaqueue
*vidq
= &dev
->vidq
;
1330 int minor
= iminor(inode
);
1332 vivi_stop_thread(vidq
);
1333 videobuf_mmap_free(&fh
->vb_vidq
);
1339 printk(KERN_DEBUG
"vivi: close called (minor=%d, users=%d)\n",minor
,dev
->users
);
1345 vivi_mmap(struct file
*file
, struct vm_area_struct
* vma
)
1347 struct vivi_fh
*fh
= file
->private_data
;
1350 dprintk (1,"mmap called, vma=0x%08lx\n",(unsigned long)vma
);
1352 ret
=videobuf_mmap_mapper(&fh
->vb_vidq
, vma
);
1354 dprintk (1,"vma start=0x%08lx, size=%ld, ret=%d\n",
1355 (unsigned long)vma
->vm_start
,
1356 (unsigned long)vma
->vm_end
-(unsigned long)vma
->vm_start
,
1362 static const struct file_operations vivi_fops
= {
1363 .owner
= THIS_MODULE
,
1365 .release
= vivi_release
,
1368 .ioctl
= video_ioctl2
, /* V4L2 ioctl handler */
1370 .llseek
= no_llseek
,
1373 static struct video_device vivi
= {
1375 .type
= VID_TYPE_CAPTURE
,
1379 // .release = video_device_release,
1381 .vidioc_querycap
= vidioc_querycap
,
1382 .vidioc_enum_fmt_cap
= vidioc_enum_fmt_cap
,
1383 .vidioc_g_fmt_cap
= vidioc_g_fmt_cap
,
1384 .vidioc_try_fmt_cap
= vidioc_try_fmt_cap
,
1385 .vidioc_s_fmt_cap
= vidioc_s_fmt_cap
,
1386 .vidioc_reqbufs
= vidioc_reqbufs
,
1387 .vidioc_querybuf
= vidioc_querybuf
,
1388 .vidioc_qbuf
= vidioc_qbuf
,
1389 .vidioc_dqbuf
= vidioc_dqbuf
,
1390 .vidioc_s_std
= vidioc_s_std
,
1391 .vidioc_enum_input
= vidioc_enum_input
,
1392 .vidioc_g_input
= vidioc_g_input
,
1393 .vidioc_s_input
= vidioc_s_input
,
1394 .vidioc_queryctrl
= vidioc_queryctrl
,
1395 .vidioc_g_ctrl
= vidioc_g_ctrl
,
1396 .vidioc_s_ctrl
= vidioc_s_ctrl
,
1397 .vidioc_streamon
= vidioc_streamon
,
1398 .vidioc_streamoff
= vidioc_streamoff
,
1399 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1400 .vidiocgmbuf
= vidiocgmbuf
,
1402 .tvnorms
= V4L2_STD_NTSC_M
,
1403 .current_norm
= V4L2_STD_NTSC_M
,
1405 /* -----------------------------------------------------------------
1406 Initialization and module stuff
1407 ------------------------------------------------------------------*/
1409 static int __init
vivi_init(void)
1412 struct vivi_dev
*dev
;
1414 dev
= kzalloc(sizeof(*dev
),GFP_KERNEL
);
1417 list_add_tail(&dev
->vivi_devlist
,&vivi_devlist
);
1419 /* init video dma queues */
1420 INIT_LIST_HEAD(&dev
->vidq
.active
);
1421 INIT_LIST_HEAD(&dev
->vidq
.queued
);
1422 init_waitqueue_head(&dev
->vidq
.wq
);
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 while (!list_empty(&vivi_devlist
)) {
1442 list
= vivi_devlist
.next
;
1444 h
= list_entry(list
, struct vivi_dev
, vivi_devlist
);
1447 video_unregister_device(&vivi
);
1450 module_init(vivi_init
);
1451 module_exit(vivi_exit
);
1453 MODULE_DESCRIPTION("Video Technology Magazine Virtual Video Capture Board");
1454 MODULE_AUTHOR("Mauro Carvalho Chehab, Ted Walther and John Sokol");
1455 MODULE_LICENSE("Dual BSD/GPL");
1457 module_param(video_nr
, int, 0);
1459 module_param_named(debug
,vivi
.debug
, int, 0644);
1460 MODULE_PARM_DESC(debug
,"activates debug info");
1462 module_param(vid_limit
,int,0644);
1463 MODULE_PARM_DESC(vid_limit
,"capture memory limit in megabytes");