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 #include <linux/interrupt.h>
32 #include <linux/kthread.h>
33 #include <linux/highmem.h>
34 #include <linux/freezer.h>
35 #include <media/videobuf-vmalloc.h>
36 #include <media/v4l2-device.h>
37 #include <media/v4l2-ioctl.h>
40 #define VIVI_MODULE_NAME "vivi"
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 #define VIVI_MAJOR_VERSION 0
48 #define VIVI_MINOR_VERSION 6
49 #define VIVI_RELEASE 0
50 #define VIVI_VERSION \
51 KERNEL_VERSION(VIVI_MAJOR_VERSION, VIVI_MINOR_VERSION, VIVI_RELEASE)
53 MODULE_DESCRIPTION("Video Technology Magazine Virtual Video Capture Board");
54 MODULE_AUTHOR("Mauro Carvalho Chehab, Ted Walther and John Sokol");
55 MODULE_LICENSE("Dual BSD/GPL");
57 static unsigned video_nr
= -1;
58 module_param(video_nr
, uint
, 0644);
59 MODULE_PARM_DESC(video_nr
, "videoX start number, -1 is autodetect");
61 static unsigned n_devs
= 1;
62 module_param(n_devs
, uint
, 0644);
63 MODULE_PARM_DESC(n_devs
, "number of video devices to create");
65 static unsigned debug
;
66 module_param(debug
, uint
, 0644);
67 MODULE_PARM_DESC(debug
, "activates debug info");
69 static unsigned int vid_limit
= 16;
70 module_param(vid_limit
, uint
, 0644);
71 MODULE_PARM_DESC(vid_limit
, "capture memory limit in megabytes");
74 /* supported controls */
75 static struct v4l2_queryctrl vivi_qctrl
[] = {
77 .id
= V4L2_CID_AUDIO_VOLUME
,
82 .default_value
= 65535,
83 .flags
= V4L2_CTRL_FLAG_SLIDER
,
84 .type
= V4L2_CTRL_TYPE_INTEGER
,
86 .id
= V4L2_CID_BRIGHTNESS
,
87 .type
= V4L2_CTRL_TYPE_INTEGER
,
93 .flags
= V4L2_CTRL_FLAG_SLIDER
,
95 .id
= V4L2_CID_CONTRAST
,
96 .type
= V4L2_CTRL_TYPE_INTEGER
,
101 .default_value
= 0x10,
102 .flags
= V4L2_CTRL_FLAG_SLIDER
,
104 .id
= V4L2_CID_SATURATION
,
105 .type
= V4L2_CTRL_TYPE_INTEGER
,
106 .name
= "Saturation",
110 .default_value
= 127,
111 .flags
= V4L2_CTRL_FLAG_SLIDER
,
114 .type
= V4L2_CTRL_TYPE_INTEGER
,
120 .flags
= V4L2_CTRL_FLAG_SLIDER
,
124 #define dprintk(dev, level, fmt, arg...) \
125 v4l2_dbg(level, debug, &dev->v4l2_dev, fmt, ## arg)
127 /* ------------------------------------------------------------------
129 ------------------------------------------------------------------*/
133 u32 fourcc
; /* v4l2 format id */
137 static struct vivi_fmt formats
[] = {
139 .name
= "4:2:2, packed, YUYV",
140 .fourcc
= V4L2_PIX_FMT_YUYV
,
144 .name
= "4:2:2, packed, UYVY",
145 .fourcc
= V4L2_PIX_FMT_UYVY
,
149 .name
= "RGB565 (LE)",
150 .fourcc
= V4L2_PIX_FMT_RGB565
, /* gggbbbbb rrrrrggg */
154 .name
= "RGB565 (BE)",
155 .fourcc
= V4L2_PIX_FMT_RGB565X
, /* rrrrrggg gggbbbbb */
159 .name
= "RGB555 (LE)",
160 .fourcc
= V4L2_PIX_FMT_RGB555
, /* gggbbbbb arrrrrgg */
164 .name
= "RGB555 (BE)",
165 .fourcc
= V4L2_PIX_FMT_RGB555X
, /* arrrrrgg gggbbbbb */
170 static struct vivi_fmt
*get_format(struct v4l2_format
*f
)
172 struct vivi_fmt
*fmt
;
175 for (k
= 0; k
< ARRAY_SIZE(formats
); k
++) {
177 if (fmt
->fourcc
== f
->fmt
.pix
.pixelformat
)
181 if (k
== ARRAY_SIZE(formats
))
189 struct scatterlist
*sg
;
192 /* buffer for one video frame */
194 /* common v4l buffer stuff -- must be first */
195 struct videobuf_buffer vb
;
197 struct vivi_fmt
*fmt
;
200 struct vivi_dmaqueue
{
201 struct list_head active
;
203 /* thread for generating video stream*/
204 struct task_struct
*kthread
;
205 wait_queue_head_t wq
;
206 /* Counters to control fps rate */
211 static LIST_HEAD(vivi_devlist
);
214 struct list_head vivi_devlist
;
215 struct v4l2_device v4l2_dev
;
222 /* various device info */
223 struct video_device
*vfd
;
225 struct vivi_dmaqueue vidq
;
227 /* Several counters */
229 unsigned long jiffies
;
232 int mv_count
; /* Controls bars movement */
237 /* Control 'registers' */
238 int qctl_regs
[ARRAY_SIZE(vivi_qctrl
)];
242 struct vivi_dev
*dev
;
245 struct vivi_fmt
*fmt
;
246 unsigned int width
, height
;
247 struct videobuf_queue vb_vidq
;
249 enum v4l2_buf_type type
;
250 unsigned char bars
[8][3];
251 int input
; /* Input Number on bars */
254 /* ------------------------------------------------------------------
255 DMA and thread functions
256 ------------------------------------------------------------------*/
258 /* Bars and Colors should match positions */
272 #define COLOR_WHITE {204, 204, 204}
273 #define COLOR_AMBAR {208, 208, 0}
274 #define COLOR_CIAN { 0, 206, 206}
275 #define COLOR_GREEN { 0, 239, 0}
276 #define COLOR_MAGENTA {239, 0, 239}
277 #define COLOR_RED {205, 0, 0}
278 #define COLOR_BLUE { 0, 0, 255}
279 #define COLOR_BLACK { 0, 0, 0}
285 /* Maximum number of bars are 10 - otherwise, the input print code
286 should be modified */
287 static struct bar_std bars
[] = {
288 { /* Standard ITU-R color bar sequence */
335 #define NUM_INPUTS ARRAY_SIZE(bars)
337 #define TO_Y(r, g, b) \
338 (((16829 * r + 33039 * g + 6416 * b + 32768) >> 16) + 16)
339 /* RGB to V(Cr) Color transform */
340 #define TO_V(r, g, b) \
341 (((28784 * r - 24103 * g - 4681 * b + 32768) >> 16) + 128)
342 /* RGB to U(Cb) Color transform */
343 #define TO_U(r, g, b) \
344 (((-9714 * r - 19070 * g + 28784 * b + 32768) >> 16) + 128)
346 /* precalculate color bar values to speed up rendering */
347 static void precalculate_bars(struct vivi_fh
*fh
)
349 struct vivi_dev
*dev
= fh
->dev
;
350 unsigned char r
, g
, b
;
353 fh
->input
= dev
->input
;
355 for (k
= 0; k
< 8; k
++) {
356 r
= bars
[fh
->input
].bar
[k
][0];
357 g
= bars
[fh
->input
].bar
[k
][1];
358 b
= bars
[fh
->input
].bar
[k
][2];
361 switch (fh
->fmt
->fourcc
) {
362 case V4L2_PIX_FMT_YUYV
:
363 case V4L2_PIX_FMT_UYVY
:
366 case V4L2_PIX_FMT_RGB565
:
367 case V4L2_PIX_FMT_RGB565X
:
372 case V4L2_PIX_FMT_RGB555
:
373 case V4L2_PIX_FMT_RGB555X
:
381 fh
->bars
[k
][0] = TO_Y(r
, g
, b
); /* Luma */
382 fh
->bars
[k
][1] = TO_U(r
, g
, b
); /* Cb */
383 fh
->bars
[k
][2] = TO_V(r
, g
, b
); /* Cr */
393 #define TSTAMP_MIN_Y 24
394 #define TSTAMP_MAX_Y (TSTAMP_MIN_Y + 15)
395 #define TSTAMP_INPUT_X 10
396 #define TSTAMP_MIN_X (54 + TSTAMP_INPUT_X)
398 static void gen_twopix(struct vivi_fh
*fh
, unsigned char *buf
, int colorpos
)
400 unsigned char r_y
, g_u
, b_v
;
404 r_y
= fh
->bars
[colorpos
][0]; /* R or precalculated Y */
405 g_u
= fh
->bars
[colorpos
][1]; /* G or precalculated U */
406 b_v
= fh
->bars
[colorpos
][2]; /* B or precalculated V */
408 for (color
= 0; color
< 4; color
++) {
411 switch (fh
->fmt
->fourcc
) {
412 case V4L2_PIX_FMT_YUYV
:
426 case V4L2_PIX_FMT_UYVY
:
440 case V4L2_PIX_FMT_RGB565
:
444 *p
= (g_u
<< 5) | b_v
;
448 *p
= (r_y
<< 3) | (g_u
>> 3);
452 case V4L2_PIX_FMT_RGB565X
:
456 *p
= (r_y
<< 3) | (g_u
>> 3);
460 *p
= (g_u
<< 5) | b_v
;
464 case V4L2_PIX_FMT_RGB555
:
468 *p
= (g_u
<< 5) | b_v
;
472 *p
= (r_y
<< 2) | (g_u
>> 3);
476 case V4L2_PIX_FMT_RGB555X
:
480 *p
= (r_y
<< 2) | (g_u
>> 3);
484 *p
= (g_u
<< 5) | b_v
;
492 static void gen_line(struct vivi_fh
*fh
, char *basep
, int inipos
, int wmax
,
493 int hmax
, int line
, int count
, char *timestr
)
500 /* We will just duplicate the second pixel at the packet */
503 /* Generate a standard color bar pattern */
504 for (w
= 0; w
< wmax
; w
++) {
505 int colorpos
= ((w
+ count
) * 8/(wmax
+ 1)) % 8;
507 gen_twopix(fh
, basep
+ pos
, colorpos
);
508 pos
+= 4; /* only 16 bpp supported for now */
511 /* Prints input entry number */
513 /* Checks if it is possible to input number */
514 if (TSTAMP_MAX_Y
>= hmax
)
517 if (TSTAMP_INPUT_X
+ strlen(timestr
) >= wmax
)
520 if (line
>= TSTAMP_MIN_Y
&& line
<= TSTAMP_MAX_Y
) {
521 chr
= rom8x16_bits
[fh
->input
* 16 + line
- TSTAMP_MIN_Y
];
522 pos
= TSTAMP_INPUT_X
;
523 for (i
= 0; i
< 7; i
++) {
524 /* Draw white font on black background */
525 if (chr
& 1 << (7 - i
))
526 gen_twopix(fh
, basep
+ pos
, WHITE
);
528 gen_twopix(fh
, basep
+ pos
, BLACK
);
533 /* Checks if it is possible to show timestamp */
534 if (TSTAMP_MIN_X
+ strlen(timestr
) >= wmax
)
537 /* Print stream time */
538 if (line
>= TSTAMP_MIN_Y
&& line
<= TSTAMP_MAX_Y
) {
540 for (s
= timestr
; *s
; s
++) {
541 chr
= rom8x16_bits
[(*s
-0x30)*16+line
-TSTAMP_MIN_Y
];
542 for (i
= 0; i
< 7; i
++) {
543 pos
= inipos
+ j
* 2;
544 /* Draw white font on black background */
545 if (chr
& 1 << (7 - i
))
546 gen_twopix(fh
, basep
+ pos
, WHITE
);
548 gen_twopix(fh
, basep
+ pos
, BLACK
);
558 static void vivi_fillbuff(struct vivi_fh
*fh
, struct vivi_buffer
*buf
)
560 struct vivi_dev
*dev
= fh
->dev
;
562 int hmax
= buf
->vb
.height
;
563 int wmax
= buf
->vb
.width
;
566 void *vbuf
= videobuf_to_vmalloc(&buf
->vb
);
571 tmpbuf
= kmalloc(wmax
* 2, GFP_ATOMIC
);
575 for (h
= 0; h
< hmax
; h
++) {
576 gen_line(fh
, tmpbuf
, 0, wmax
, hmax
, h
, dev
->mv_count
,
578 memcpy(vbuf
+ pos
, tmpbuf
, wmax
* 2);
586 /* Updates stream time */
588 dev
->ms
+= jiffies_to_msecs(jiffies
-dev
->jiffies
);
589 dev
->jiffies
= jiffies
;
590 if (dev
->ms
>= 1000) {
604 sprintf(dev
->timestr
, "%02d:%02d:%02d:%03d",
605 dev
->h
, dev
->m
, dev
->s
, dev
->ms
);
607 dprintk(dev
, 2, "vivifill at %s: Buffer 0x%08lx size= %d\n",
608 dev
->timestr
, (unsigned long)tmpbuf
, pos
);
610 /* Advice that buffer was filled */
611 buf
->vb
.field_count
++;
612 do_gettimeofday(&ts
);
614 buf
->vb
.state
= VIDEOBUF_DONE
;
617 static void vivi_thread_tick(struct vivi_fh
*fh
)
619 struct vivi_buffer
*buf
;
620 struct vivi_dev
*dev
= fh
->dev
;
621 struct vivi_dmaqueue
*dma_q
= &dev
->vidq
;
623 unsigned long flags
= 0;
625 dprintk(dev
, 1, "Thread tick\n");
627 spin_lock_irqsave(&dev
->slock
, flags
);
628 if (list_empty(&dma_q
->active
)) {
629 dprintk(dev
, 1, "No active queue to serve\n");
633 buf
= list_entry(dma_q
->active
.next
,
634 struct vivi_buffer
, vb
.queue
);
636 /* Nobody is waiting on this buffer, return */
637 if (!waitqueue_active(&buf
->vb
.done
))
640 list_del(&buf
->vb
.queue
);
642 do_gettimeofday(&buf
->vb
.ts
);
645 vivi_fillbuff(fh
, buf
);
646 dprintk(dev
, 1, "filled buffer %p\n", buf
);
648 wake_up(&buf
->vb
.done
);
649 dprintk(dev
, 2, "[%p/%d] wakeup\n", buf
, buf
->vb
. i
);
651 spin_unlock_irqrestore(&dev
->slock
, flags
);
655 #define frames_to_ms(frames) \
656 ((frames * WAKE_NUMERATOR * 1000) / WAKE_DENOMINATOR)
658 static void vivi_sleep(struct vivi_fh
*fh
)
660 struct vivi_dev
*dev
= fh
->dev
;
661 struct vivi_dmaqueue
*dma_q
= &dev
->vidq
;
663 DECLARE_WAITQUEUE(wait
, current
);
665 dprintk(dev
, 1, "%s dma_q=0x%08lx\n", __func__
,
666 (unsigned long)dma_q
);
668 add_wait_queue(&dma_q
->wq
, &wait
);
669 if (kthread_should_stop())
672 /* Calculate time to wake up */
673 timeout
= msecs_to_jiffies(frames_to_ms(1));
675 vivi_thread_tick(fh
);
677 schedule_timeout_interruptible(timeout
);
680 remove_wait_queue(&dma_q
->wq
, &wait
);
684 static int vivi_thread(void *data
)
686 struct vivi_fh
*fh
= data
;
687 struct vivi_dev
*dev
= fh
->dev
;
689 dprintk(dev
, 1, "thread started\n");
696 if (kthread_should_stop())
699 dprintk(dev
, 1, "thread: exit\n");
703 static int vivi_start_thread(struct vivi_fh
*fh
)
705 struct vivi_dev
*dev
= fh
->dev
;
706 struct vivi_dmaqueue
*dma_q
= &dev
->vidq
;
709 dma_q
->ini_jiffies
= jiffies
;
711 dprintk(dev
, 1, "%s\n", __func__
);
713 dma_q
->kthread
= kthread_run(vivi_thread
, fh
, "vivi");
715 if (IS_ERR(dma_q
->kthread
)) {
716 v4l2_err(&dev
->v4l2_dev
, "kernel_thread() failed\n");
717 return PTR_ERR(dma_q
->kthread
);
720 wake_up_interruptible(&dma_q
->wq
);
722 dprintk(dev
, 1, "returning from %s\n", __func__
);
726 static void vivi_stop_thread(struct vivi_dmaqueue
*dma_q
)
728 struct vivi_dev
*dev
= container_of(dma_q
, struct vivi_dev
, vidq
);
730 dprintk(dev
, 1, "%s\n", __func__
);
731 /* shutdown control thread */
732 if (dma_q
->kthread
) {
733 kthread_stop(dma_q
->kthread
);
734 dma_q
->kthread
= NULL
;
738 /* ------------------------------------------------------------------
740 ------------------------------------------------------------------*/
742 buffer_setup(struct videobuf_queue
*vq
, unsigned int *count
, unsigned int *size
)
744 struct vivi_fh
*fh
= vq
->priv_data
;
745 struct vivi_dev
*dev
= fh
->dev
;
747 *size
= fh
->width
*fh
->height
*2;
752 while (*size
* *count
> vid_limit
* 1024 * 1024)
755 dprintk(dev
, 1, "%s, count=%d, size=%d\n", __func__
,
761 static void free_buffer(struct videobuf_queue
*vq
, struct vivi_buffer
*buf
)
763 struct vivi_fh
*fh
= vq
->priv_data
;
764 struct vivi_dev
*dev
= fh
->dev
;
766 dprintk(dev
, 1, "%s, state: %i\n", __func__
, buf
->vb
.state
);
771 videobuf_vmalloc_free(&buf
->vb
);
772 dprintk(dev
, 1, "free_buffer: freed\n");
773 buf
->vb
.state
= VIDEOBUF_NEEDS_INIT
;
776 #define norm_maxw() 1024
777 #define norm_maxh() 768
779 buffer_prepare(struct videobuf_queue
*vq
, struct videobuf_buffer
*vb
,
780 enum v4l2_field field
)
782 struct vivi_fh
*fh
= vq
->priv_data
;
783 struct vivi_dev
*dev
= fh
->dev
;
784 struct vivi_buffer
*buf
= container_of(vb
, struct vivi_buffer
, vb
);
787 dprintk(dev
, 1, "%s, field=%d\n", __func__
, field
);
789 BUG_ON(NULL
== fh
->fmt
);
791 if (fh
->width
< 48 || fh
->width
> norm_maxw() ||
792 fh
->height
< 32 || fh
->height
> norm_maxh())
795 buf
->vb
.size
= fh
->width
*fh
->height
*2;
796 if (0 != buf
->vb
.baddr
&& buf
->vb
.bsize
< buf
->vb
.size
)
799 /* These properties only change when queue is idle, see s_fmt */
801 buf
->vb
.width
= fh
->width
;
802 buf
->vb
.height
= fh
->height
;
803 buf
->vb
.field
= field
;
805 precalculate_bars(fh
);
807 if (VIDEOBUF_NEEDS_INIT
== buf
->vb
.state
) {
808 rc
= videobuf_iolock(vq
, &buf
->vb
, NULL
);
813 buf
->vb
.state
= VIDEOBUF_PREPARED
;
818 free_buffer(vq
, buf
);
823 buffer_queue(struct videobuf_queue
*vq
, struct videobuf_buffer
*vb
)
825 struct vivi_buffer
*buf
= container_of(vb
, struct vivi_buffer
, vb
);
826 struct vivi_fh
*fh
= vq
->priv_data
;
827 struct vivi_dev
*dev
= fh
->dev
;
828 struct vivi_dmaqueue
*vidq
= &dev
->vidq
;
830 dprintk(dev
, 1, "%s\n", __func__
);
832 buf
->vb
.state
= VIDEOBUF_QUEUED
;
833 list_add_tail(&buf
->vb
.queue
, &vidq
->active
);
836 static void buffer_release(struct videobuf_queue
*vq
,
837 struct videobuf_buffer
*vb
)
839 struct vivi_buffer
*buf
= container_of(vb
, struct vivi_buffer
, vb
);
840 struct vivi_fh
*fh
= vq
->priv_data
;
841 struct vivi_dev
*dev
= (struct vivi_dev
*)fh
->dev
;
843 dprintk(dev
, 1, "%s\n", __func__
);
845 free_buffer(vq
, buf
);
848 static struct videobuf_queue_ops vivi_video_qops
= {
849 .buf_setup
= buffer_setup
,
850 .buf_prepare
= buffer_prepare
,
851 .buf_queue
= buffer_queue
,
852 .buf_release
= buffer_release
,
855 /* ------------------------------------------------------------------
856 IOCTL vidioc handling
857 ------------------------------------------------------------------*/
858 static int vidioc_querycap(struct file
*file
, void *priv
,
859 struct v4l2_capability
*cap
)
861 struct vivi_fh
*fh
= priv
;
862 struct vivi_dev
*dev
= fh
->dev
;
864 strcpy(cap
->driver
, "vivi");
865 strcpy(cap
->card
, "vivi");
866 strlcpy(cap
->bus_info
, dev
->v4l2_dev
.name
, sizeof(cap
->bus_info
));
867 cap
->version
= VIVI_VERSION
;
868 cap
->capabilities
= V4L2_CAP_VIDEO_CAPTURE
|
874 static int vidioc_enum_fmt_vid_cap(struct file
*file
, void *priv
,
875 struct v4l2_fmtdesc
*f
)
877 struct vivi_fmt
*fmt
;
879 if (f
->index
>= ARRAY_SIZE(formats
))
882 fmt
= &formats
[f
->index
];
884 strlcpy(f
->description
, fmt
->name
, sizeof(f
->description
));
885 f
->pixelformat
= fmt
->fourcc
;
889 static int vidioc_g_fmt_vid_cap(struct file
*file
, void *priv
,
890 struct v4l2_format
*f
)
892 struct vivi_fh
*fh
= priv
;
894 f
->fmt
.pix
.width
= fh
->width
;
895 f
->fmt
.pix
.height
= fh
->height
;
896 f
->fmt
.pix
.field
= fh
->vb_vidq
.field
;
897 f
->fmt
.pix
.pixelformat
= fh
->fmt
->fourcc
;
898 f
->fmt
.pix
.bytesperline
=
899 (f
->fmt
.pix
.width
* fh
->fmt
->depth
) >> 3;
900 f
->fmt
.pix
.sizeimage
=
901 f
->fmt
.pix
.height
* f
->fmt
.pix
.bytesperline
;
906 static int vidioc_try_fmt_vid_cap(struct file
*file
, void *priv
,
907 struct v4l2_format
*f
)
909 struct vivi_fh
*fh
= priv
;
910 struct vivi_dev
*dev
= fh
->dev
;
911 struct vivi_fmt
*fmt
;
912 enum v4l2_field field
;
913 unsigned int maxw
, maxh
;
917 dprintk(dev
, 1, "Fourcc format (0x%08x) invalid.\n",
918 f
->fmt
.pix
.pixelformat
);
922 field
= f
->fmt
.pix
.field
;
924 if (field
== V4L2_FIELD_ANY
) {
925 field
= V4L2_FIELD_INTERLACED
;
926 } else if (V4L2_FIELD_INTERLACED
!= field
) {
927 dprintk(dev
, 1, "Field type invalid.\n");
934 f
->fmt
.pix
.field
= field
;
935 v4l_bound_align_image(&f
->fmt
.pix
.width
, 48, maxw
, 2,
936 &f
->fmt
.pix
.height
, 32, maxh
, 0, 0);
937 f
->fmt
.pix
.bytesperline
=
938 (f
->fmt
.pix
.width
* fmt
->depth
) >> 3;
939 f
->fmt
.pix
.sizeimage
=
940 f
->fmt
.pix
.height
* f
->fmt
.pix
.bytesperline
;
945 /*FIXME: This seems to be generic enough to be at videodev2 */
946 static int vidioc_s_fmt_vid_cap(struct file
*file
, void *priv
,
947 struct v4l2_format
*f
)
949 struct vivi_fh
*fh
= priv
;
950 struct videobuf_queue
*q
= &fh
->vb_vidq
;
952 int ret
= vidioc_try_fmt_vid_cap(file
, fh
, f
);
956 mutex_lock(&q
->vb_lock
);
958 if (videobuf_queue_is_busy(&fh
->vb_vidq
)) {
959 dprintk(fh
->dev
, 1, "%s queue busy\n", __func__
);
964 fh
->fmt
= get_format(f
);
965 fh
->width
= f
->fmt
.pix
.width
;
966 fh
->height
= f
->fmt
.pix
.height
;
967 fh
->vb_vidq
.field
= f
->fmt
.pix
.field
;
972 mutex_unlock(&q
->vb_lock
);
977 static int vidioc_reqbufs(struct file
*file
, void *priv
,
978 struct v4l2_requestbuffers
*p
)
980 struct vivi_fh
*fh
= priv
;
982 return (videobuf_reqbufs(&fh
->vb_vidq
, p
));
985 static int vidioc_querybuf(struct file
*file
, void *priv
, struct v4l2_buffer
*p
)
987 struct vivi_fh
*fh
= priv
;
989 return (videobuf_querybuf(&fh
->vb_vidq
, p
));
992 static int vidioc_qbuf(struct file
*file
, void *priv
, struct v4l2_buffer
*p
)
994 struct vivi_fh
*fh
= priv
;
996 return (videobuf_qbuf(&fh
->vb_vidq
, p
));
999 static int vidioc_dqbuf(struct file
*file
, void *priv
, struct v4l2_buffer
*p
)
1001 struct vivi_fh
*fh
= priv
;
1003 return (videobuf_dqbuf(&fh
->vb_vidq
, p
,
1004 file
->f_flags
& O_NONBLOCK
));
1007 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1008 static int vidiocgmbuf(struct file
*file
, void *priv
, struct video_mbuf
*mbuf
)
1010 struct vivi_fh
*fh
= priv
;
1012 return videobuf_cgmbuf(&fh
->vb_vidq
, mbuf
, 8);
1016 static int vidioc_streamon(struct file
*file
, void *priv
, enum v4l2_buf_type i
)
1018 struct vivi_fh
*fh
= priv
;
1020 if (fh
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1025 return videobuf_streamon(&fh
->vb_vidq
);
1028 static int vidioc_streamoff(struct file
*file
, void *priv
, enum v4l2_buf_type i
)
1030 struct vivi_fh
*fh
= priv
;
1032 if (fh
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1037 return videobuf_streamoff(&fh
->vb_vidq
);
1040 static int vidioc_s_std(struct file
*file
, void *priv
, v4l2_std_id
*i
)
1045 /* only one input in this sample driver */
1046 static int vidioc_enum_input(struct file
*file
, void *priv
,
1047 struct v4l2_input
*inp
)
1049 if (inp
->index
>= NUM_INPUTS
)
1052 inp
->type
= V4L2_INPUT_TYPE_CAMERA
;
1053 inp
->std
= V4L2_STD_525_60
;
1054 sprintf(inp
->name
, "Camera %u", inp
->index
);
1059 static int vidioc_g_input(struct file
*file
, void *priv
, unsigned int *i
)
1061 struct vivi_fh
*fh
= priv
;
1062 struct vivi_dev
*dev
= fh
->dev
;
1068 static int vidioc_s_input(struct file
*file
, void *priv
, unsigned int i
)
1070 struct vivi_fh
*fh
= priv
;
1071 struct vivi_dev
*dev
= fh
->dev
;
1073 if (i
>= NUM_INPUTS
)
1077 precalculate_bars(fh
);
1082 /* --- controls ---------------------------------------------- */
1083 static int vidioc_queryctrl(struct file
*file
, void *priv
,
1084 struct v4l2_queryctrl
*qc
)
1088 for (i
= 0; i
< ARRAY_SIZE(vivi_qctrl
); i
++)
1089 if (qc
->id
&& qc
->id
== vivi_qctrl
[i
].id
) {
1090 memcpy(qc
, &(vivi_qctrl
[i
]),
1098 static int vidioc_g_ctrl(struct file
*file
, void *priv
,
1099 struct v4l2_control
*ctrl
)
1101 struct vivi_fh
*fh
= priv
;
1102 struct vivi_dev
*dev
= fh
->dev
;
1105 for (i
= 0; i
< ARRAY_SIZE(vivi_qctrl
); i
++)
1106 if (ctrl
->id
== vivi_qctrl
[i
].id
) {
1107 ctrl
->value
= dev
->qctl_regs
[i
];
1113 static int vidioc_s_ctrl(struct file
*file
, void *priv
,
1114 struct v4l2_control
*ctrl
)
1116 struct vivi_fh
*fh
= priv
;
1117 struct vivi_dev
*dev
= fh
->dev
;
1120 for (i
= 0; i
< ARRAY_SIZE(vivi_qctrl
); i
++)
1121 if (ctrl
->id
== vivi_qctrl
[i
].id
) {
1122 if (ctrl
->value
< vivi_qctrl
[i
].minimum
||
1123 ctrl
->value
> vivi_qctrl
[i
].maximum
) {
1126 dev
->qctl_regs
[i
] = ctrl
->value
;
1132 /* ------------------------------------------------------------------
1133 File operations for the device
1134 ------------------------------------------------------------------*/
1136 static int vivi_open(struct file
*file
)
1138 struct vivi_dev
*dev
= video_drvdata(file
);
1139 struct vivi_fh
*fh
= NULL
;
1142 mutex_lock(&dev
->mutex
);
1145 if (dev
->users
> 1) {
1147 mutex_unlock(&dev
->mutex
);
1151 dprintk(dev
, 1, "open /dev/video%d type=%s users=%d\n", dev
->vfd
->num
,
1152 v4l2_type_names
[V4L2_BUF_TYPE_VIDEO_CAPTURE
], dev
->users
);
1154 /* allocate + initialize per filehandle data */
1155 fh
= kzalloc(sizeof(*fh
), GFP_KERNEL
);
1160 mutex_unlock(&dev
->mutex
);
1165 file
->private_data
= fh
;
1168 fh
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1169 fh
->fmt
= &formats
[0];
1173 /* Resets frame counters */
1179 dev
->jiffies
= jiffies
;
1180 sprintf(dev
->timestr
, "%02d:%02d:%02d:%03d",
1181 dev
->h
, dev
->m
, dev
->s
, dev
->ms
);
1183 videobuf_queue_vmalloc_init(&fh
->vb_vidq
, &vivi_video_qops
,
1184 NULL
, &dev
->slock
, fh
->type
, V4L2_FIELD_INTERLACED
,
1185 sizeof(struct vivi_buffer
), fh
);
1187 vivi_start_thread(fh
);
1193 vivi_read(struct file
*file
, char __user
*data
, size_t count
, loff_t
*ppos
)
1195 struct vivi_fh
*fh
= file
->private_data
;
1197 if (fh
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE
) {
1198 return videobuf_read_stream(&fh
->vb_vidq
, data
, count
, ppos
, 0,
1199 file
->f_flags
& O_NONBLOCK
);
1205 vivi_poll(struct file
*file
, struct poll_table_struct
*wait
)
1207 struct vivi_fh
*fh
= file
->private_data
;
1208 struct vivi_dev
*dev
= fh
->dev
;
1209 struct videobuf_queue
*q
= &fh
->vb_vidq
;
1211 dprintk(dev
, 1, "%s\n", __func__
);
1213 if (V4L2_BUF_TYPE_VIDEO_CAPTURE
!= fh
->type
)
1216 return videobuf_poll_stream(file
, q
, wait
);
1219 static int vivi_close(struct file
*file
)
1221 struct vivi_fh
*fh
= file
->private_data
;
1222 struct vivi_dev
*dev
= fh
->dev
;
1223 struct vivi_dmaqueue
*vidq
= &dev
->vidq
;
1225 int minor
= video_devdata(file
)->minor
;
1227 vivi_stop_thread(vidq
);
1228 videobuf_stop(&fh
->vb_vidq
);
1229 videobuf_mmap_free(&fh
->vb_vidq
);
1233 mutex_lock(&dev
->mutex
);
1235 mutex_unlock(&dev
->mutex
);
1237 dprintk(dev
, 1, "close called (minor=%d, users=%d)\n",
1243 static int vivi_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1245 struct vivi_fh
*fh
= file
->private_data
;
1246 struct vivi_dev
*dev
= fh
->dev
;
1249 dprintk(dev
, 1, "mmap called, vma=0x%08lx\n", (unsigned long)vma
);
1251 ret
= videobuf_mmap_mapper(&fh
->vb_vidq
, vma
);
1253 dprintk(dev
, 1, "vma start=0x%08lx, size=%ld, ret=%d\n",
1254 (unsigned long)vma
->vm_start
,
1255 (unsigned long)vma
->vm_end
-(unsigned long)vma
->vm_start
,
1261 static const struct v4l2_file_operations vivi_fops
= {
1262 .owner
= THIS_MODULE
,
1264 .release
= vivi_close
,
1267 .ioctl
= video_ioctl2
, /* V4L2 ioctl handler */
1271 static const struct v4l2_ioctl_ops vivi_ioctl_ops
= {
1272 .vidioc_querycap
= vidioc_querycap
,
1273 .vidioc_enum_fmt_vid_cap
= vidioc_enum_fmt_vid_cap
,
1274 .vidioc_g_fmt_vid_cap
= vidioc_g_fmt_vid_cap
,
1275 .vidioc_try_fmt_vid_cap
= vidioc_try_fmt_vid_cap
,
1276 .vidioc_s_fmt_vid_cap
= vidioc_s_fmt_vid_cap
,
1277 .vidioc_reqbufs
= vidioc_reqbufs
,
1278 .vidioc_querybuf
= vidioc_querybuf
,
1279 .vidioc_qbuf
= vidioc_qbuf
,
1280 .vidioc_dqbuf
= vidioc_dqbuf
,
1281 .vidioc_s_std
= vidioc_s_std
,
1282 .vidioc_enum_input
= vidioc_enum_input
,
1283 .vidioc_g_input
= vidioc_g_input
,
1284 .vidioc_s_input
= vidioc_s_input
,
1285 .vidioc_queryctrl
= vidioc_queryctrl
,
1286 .vidioc_g_ctrl
= vidioc_g_ctrl
,
1287 .vidioc_s_ctrl
= vidioc_s_ctrl
,
1288 .vidioc_streamon
= vidioc_streamon
,
1289 .vidioc_streamoff
= vidioc_streamoff
,
1290 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1291 .vidiocgmbuf
= vidiocgmbuf
,
1295 static struct video_device vivi_template
= {
1298 .ioctl_ops
= &vivi_ioctl_ops
,
1300 .release
= video_device_release
,
1302 .tvnorms
= V4L2_STD_525_60
,
1303 .current_norm
= V4L2_STD_NTSC_M
,
1306 /* -----------------------------------------------------------------
1307 Initialization and module stuff
1308 ------------------------------------------------------------------*/
1310 static int vivi_release(void)
1312 struct vivi_dev
*dev
;
1313 struct list_head
*list
;
1315 while (!list_empty(&vivi_devlist
)) {
1316 list
= vivi_devlist
.next
;
1318 dev
= list_entry(list
, struct vivi_dev
, vivi_devlist
);
1320 v4l2_info(&dev
->v4l2_dev
, "unregistering /dev/video%d\n",
1322 video_unregister_device(dev
->vfd
);
1323 v4l2_device_unregister(&dev
->v4l2_dev
);
1330 static int __init
vivi_create_instance(int inst
)
1332 struct vivi_dev
*dev
;
1333 struct video_device
*vfd
;
1336 dev
= kzalloc(sizeof(*dev
), GFP_KERNEL
);
1340 snprintf(dev
->v4l2_dev
.name
, sizeof(dev
->v4l2_dev
.name
),
1341 "%s-%03d", VIVI_MODULE_NAME
, inst
);
1342 ret
= v4l2_device_register(NULL
, &dev
->v4l2_dev
);
1346 /* init video dma queues */
1347 INIT_LIST_HEAD(&dev
->vidq
.active
);
1348 init_waitqueue_head(&dev
->vidq
.wq
);
1350 /* initialize locks */
1351 spin_lock_init(&dev
->slock
);
1352 mutex_init(&dev
->mutex
);
1355 vfd
= video_device_alloc();
1359 *vfd
= vivi_template
;
1362 ret
= video_register_device(vfd
, VFL_TYPE_GRABBER
, video_nr
);
1366 video_set_drvdata(vfd
, dev
);
1368 /* Set all controls to their default value. */
1369 for (i
= 0; i
< ARRAY_SIZE(vivi_qctrl
); i
++)
1370 dev
->qctl_regs
[i
] = vivi_qctrl
[i
].default_value
;
1372 /* Now that everything is fine, let's add it to device list */
1373 list_add_tail(&dev
->vivi_devlist
, &vivi_devlist
);
1375 snprintf(vfd
->name
, sizeof(vfd
->name
), "%s (%i)",
1376 vivi_template
.name
, vfd
->num
);
1382 v4l2_info(&dev
->v4l2_dev
, "V4L2 device registered as /dev/video%d\n",
1387 video_device_release(vfd
);
1389 v4l2_device_unregister(&dev
->v4l2_dev
);
1395 /* This routine allocates from 1 to n_devs virtual drivers.
1397 The real maximum number of virtual drivers will depend on how many drivers
1398 will succeed. This is limited to the maximum number of devices that
1399 videodev supports, which is equal to VIDEO_NUM_DEVICES.
1401 static int __init
vivi_init(void)
1408 for (i
= 0; i
< n_devs
; i
++) {
1409 ret
= vivi_create_instance(i
);
1411 /* If some instantiations succeeded, keep driver */
1419 printk(KERN_INFO
"Error %d while loading vivi driver\n", ret
);
1423 printk(KERN_INFO
"Video Technology Magazine Virtual Video "
1424 "Capture Board ver %u.%u.%u successfully loaded.\n",
1425 (VIVI_VERSION
>> 16) & 0xFF, (VIVI_VERSION
>> 8) & 0xFF,
1426 VIVI_VERSION
& 0xFF);
1428 /* n_devs will reflect the actual number of allocated devices */
1434 static void __exit
vivi_exit(void)
1439 module_init(vivi_init
);
1440 module_exit(vivi_exit
);