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/errno.h>
17 #include <linux/kernel.h>
18 #include <linux/init.h>
19 #include <linux/sched.h>
20 #include <linux/slab.h>
21 #include <linux/font.h>
22 #include <linux/version.h>
23 #include <linux/mutex.h>
24 #include <linux/videodev2.h>
25 #include <linux/kthread.h>
26 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 20)
27 #include <linux/freezer.h>
29 #include <media/videobuf-vmalloc.h>
30 #include <media/v4l2-device.h>
31 #include <media/v4l2-ioctl.h>
32 #include <media/v4l2-common.h>
34 #define VIVI_MODULE_NAME "vivi"
36 /* Wake up at about 30 fps */
37 #define WAKE_NUMERATOR 30
38 #define WAKE_DENOMINATOR 1001
39 #define BUFFER_TIMEOUT msecs_to_jiffies(500) /* 0.5 seconds */
41 #define MAX_WIDTH 1920
42 #define MAX_HEIGHT 1200
44 #define VIVI_MAJOR_VERSION 0
45 #define VIVI_MINOR_VERSION 7
46 #define VIVI_RELEASE 0
47 #define VIVI_VERSION \
48 KERNEL_VERSION(VIVI_MAJOR_VERSION, VIVI_MINOR_VERSION, VIVI_RELEASE)
50 MODULE_DESCRIPTION("Video Technology Magazine Virtual Video Capture Board");
51 MODULE_AUTHOR("Mauro Carvalho Chehab, Ted Walther and John Sokol");
52 MODULE_LICENSE("Dual BSD/GPL");
54 static unsigned video_nr
= -1;
55 module_param(video_nr
, uint
, 0644);
56 MODULE_PARM_DESC(video_nr
, "videoX start number, -1 is autodetect");
58 static unsigned n_devs
= 1;
59 module_param(n_devs
, uint
, 0644);
60 MODULE_PARM_DESC(n_devs
, "number of video devices to create");
62 static unsigned debug
;
63 module_param(debug
, uint
, 0644);
64 MODULE_PARM_DESC(debug
, "activates debug info");
66 static unsigned int vid_limit
= 16;
67 module_param(vid_limit
, uint
, 0644);
68 MODULE_PARM_DESC(vid_limit
, "capture memory limit in megabytes");
70 /* Global font descriptor */
71 static const u8
*font8x16
;
73 #define dprintk(dev, level, fmt, arg...) \
74 v4l2_dbg(level, debug, &dev->v4l2_dev, fmt, ## arg)
76 /* ------------------------------------------------------------------
78 ------------------------------------------------------------------*/
82 u32 fourcc
; /* v4l2 format id */
86 static struct vivi_fmt formats
[] = {
88 .name
= "4:2:2, packed, YUYV",
89 .fourcc
= V4L2_PIX_FMT_YUYV
,
93 .name
= "4:2:2, packed, UYVY",
94 .fourcc
= V4L2_PIX_FMT_UYVY
,
98 .name
= "RGB565 (LE)",
99 .fourcc
= V4L2_PIX_FMT_RGB565
, /* gggbbbbb rrrrrggg */
103 .name
= "RGB565 (BE)",
104 .fourcc
= V4L2_PIX_FMT_RGB565X
, /* rrrrrggg gggbbbbb */
108 .name
= "RGB555 (LE)",
109 .fourcc
= V4L2_PIX_FMT_RGB555
, /* gggbbbbb arrrrrgg */
113 .name
= "RGB555 (BE)",
114 .fourcc
= V4L2_PIX_FMT_RGB555X
, /* arrrrrgg gggbbbbb */
119 static struct vivi_fmt
*get_format(struct v4l2_format
*f
)
121 struct vivi_fmt
*fmt
;
124 for (k
= 0; k
< ARRAY_SIZE(formats
); k
++) {
126 if (fmt
->fourcc
== f
->fmt
.pix
.pixelformat
)
130 if (k
== ARRAY_SIZE(formats
))
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
;
149 struct vivi_dmaqueue
{
150 struct list_head active
;
152 /* thread for generating video stream*/
153 struct task_struct
*kthread
;
154 wait_queue_head_t wq
;
155 /* Counters to control fps rate */
160 static LIST_HEAD(vivi_devlist
);
163 struct list_head vivi_devlist
;
164 struct v4l2_device v4l2_dev
;
176 /* various device info */
177 struct video_device
*vfd
;
179 struct vivi_dmaqueue vidq
;
181 /* Several counters */
183 unsigned long jiffies
;
185 int mv_count
; /* Controls bars movement */
191 struct vivi_fmt
*fmt
;
192 unsigned int width
, height
;
193 struct videobuf_queue vb_vidq
;
195 unsigned long generating
;
197 u8 line
[MAX_WIDTH
* 4];
200 /* ------------------------------------------------------------------
201 DMA and thread functions
202 ------------------------------------------------------------------*/
204 /* Bars and Colors should match positions */
219 #define COLOR_WHITE {204, 204, 204}
220 #define COLOR_AMBER {208, 208, 0}
221 #define COLOR_CYAN { 0, 206, 206}
222 #define COLOR_GREEN { 0, 239, 0}
223 #define COLOR_MAGENTA {239, 0, 239}
224 #define COLOR_RED {205, 0, 0}
225 #define COLOR_BLUE { 0, 0, 255}
226 #define COLOR_BLACK { 0, 0, 0}
232 /* Maximum number of bars are 10 - otherwise, the input print code
233 should be modified */
234 static struct bar_std bars
[] = {
235 { /* Standard ITU-R color bar sequence */
236 { COLOR_WHITE
, COLOR_AMBER
, COLOR_CYAN
, COLOR_GREEN
,
237 COLOR_MAGENTA
, COLOR_RED
, COLOR_BLUE
, COLOR_BLACK
, COLOR_BLACK
}
239 { COLOR_WHITE
, COLOR_AMBER
, COLOR_BLACK
, COLOR_WHITE
,
240 COLOR_AMBER
, COLOR_BLACK
, COLOR_WHITE
, COLOR_AMBER
, COLOR_BLACK
}
242 { COLOR_WHITE
, COLOR_CYAN
, COLOR_BLACK
, COLOR_WHITE
,
243 COLOR_CYAN
, COLOR_BLACK
, COLOR_WHITE
, COLOR_CYAN
, COLOR_BLACK
}
245 { COLOR_WHITE
, COLOR_GREEN
, COLOR_BLACK
, COLOR_WHITE
,
246 COLOR_GREEN
, COLOR_BLACK
, COLOR_WHITE
, COLOR_GREEN
, COLOR_BLACK
}
250 #define NUM_INPUTS ARRAY_SIZE(bars)
252 #define TO_Y(r, g, b) \
253 (((16829 * r + 33039 * g + 6416 * b + 32768) >> 16) + 16)
254 /* RGB to V(Cr) Color transform */
255 #define TO_V(r, g, b) \
256 (((28784 * r - 24103 * g - 4681 * b + 32768) >> 16) + 128)
257 /* RGB to U(Cb) Color transform */
258 #define TO_U(r, g, b) \
259 (((-9714 * r - 19070 * g + 28784 * b + 32768) >> 16) + 128)
261 /* precalculate color bar values to speed up rendering */
262 static void precalculate_bars(struct vivi_dev
*dev
)
267 for (k
= 0; k
< 9; k
++) {
268 r
= bars
[dev
->input
].bar
[k
][0];
269 g
= bars
[dev
->input
].bar
[k
][1];
270 b
= bars
[dev
->input
].bar
[k
][2];
273 switch (dev
->fmt
->fourcc
) {
274 case V4L2_PIX_FMT_YUYV
:
275 case V4L2_PIX_FMT_UYVY
:
278 case V4L2_PIX_FMT_RGB565
:
279 case V4L2_PIX_FMT_RGB565X
:
284 case V4L2_PIX_FMT_RGB555
:
285 case V4L2_PIX_FMT_RGB555X
:
293 dev
->bars
[k
][0] = TO_Y(r
, g
, b
); /* Luma */
294 dev
->bars
[k
][1] = TO_U(r
, g
, b
); /* Cb */
295 dev
->bars
[k
][2] = TO_V(r
, g
, b
); /* Cr */
304 #define TSTAMP_MIN_Y 24
305 #define TSTAMP_MAX_Y (TSTAMP_MIN_Y + 15)
306 #define TSTAMP_INPUT_X 10
307 #define TSTAMP_MIN_X (54 + TSTAMP_INPUT_X)
309 static void gen_twopix(struct vivi_dev
*dev
, u8
*buf
, int colorpos
)
315 r_y
= dev
->bars
[colorpos
][0]; /* R or precalculated Y */
316 g_u
= dev
->bars
[colorpos
][1]; /* G or precalculated U */
317 b_v
= dev
->bars
[colorpos
][2]; /* B or precalculated V */
319 for (color
= 0; color
< 4; color
++) {
322 switch (dev
->fmt
->fourcc
) {
323 case V4L2_PIX_FMT_YUYV
:
337 case V4L2_PIX_FMT_UYVY
:
351 case V4L2_PIX_FMT_RGB565
:
355 *p
= (g_u
<< 5) | b_v
;
359 *p
= (r_y
<< 3) | (g_u
>> 3);
363 case V4L2_PIX_FMT_RGB565X
:
367 *p
= (r_y
<< 3) | (g_u
>> 3);
371 *p
= (g_u
<< 5) | b_v
;
375 case V4L2_PIX_FMT_RGB555
:
379 *p
= (g_u
<< 5) | b_v
;
383 *p
= (r_y
<< 2) | (g_u
>> 3);
387 case V4L2_PIX_FMT_RGB555X
:
391 *p
= (r_y
<< 2) | (g_u
>> 3);
395 *p
= (g_u
<< 5) | b_v
;
403 static void precalculate_line(struct vivi_dev
*dev
)
407 for (w
= 0; w
< dev
->width
* 2; w
+= 2) {
408 int colorpos
= (w
/ (dev
->width
/ 8) % 8);
410 gen_twopix(dev
, dev
->line
+ w
* 2, colorpos
);
414 static void gen_text(struct vivi_dev
*dev
, char *basep
,
415 int y
, int x
, char *text
)
419 /* Checks if it is possible to show string */
420 if (y
+ 16 >= dev
->height
|| x
+ strlen(text
) * 8 >= dev
->width
)
423 /* Print stream time */
424 for (line
= y
; line
< y
+ 16; line
++) {
426 char *pos
= basep
+ line
* dev
->width
* 2 + x
* 2;
429 for (s
= text
; *s
; s
++) {
430 u8 chr
= font8x16
[*s
* 16 + line
- y
];
433 for (i
= 0; i
< 7; i
++, j
++) {
434 /* Draw white font on black background */
435 if (chr
& (1 << (7 - i
)))
436 gen_twopix(dev
, pos
+ j
* 2, WHITE
);
438 gen_twopix(dev
, pos
+ j
* 2, TEXT_BLACK
);
444 static void vivi_fillbuff(struct vivi_dev
*dev
, struct vivi_buffer
*buf
)
446 int hmax
= buf
->vb
.height
;
447 int wmax
= buf
->vb
.width
;
449 void *vbuf
= videobuf_to_vmalloc(&buf
->vb
);
457 for (h
= 0; h
< hmax
; h
++)
458 memcpy(vbuf
+ h
* wmax
* 2, dev
->line
+ (dev
->mv_count
% wmax
) * 2, wmax
* 2);
460 /* Updates stream time */
462 dev
->ms
+= jiffies_to_msecs(jiffies
- dev
->jiffies
);
463 dev
->jiffies
= jiffies
;
465 snprintf(str
, sizeof(str
), " %02d:%02d:%02d:%03d ",
466 (ms
/ (60 * 60 * 1000)) % 24,
467 (ms
/ (60 * 1000)) % 60,
470 gen_text(dev
, vbuf
, line
++ * 16, 16, str
);
471 snprintf(str
, sizeof(str
), " %dx%d, input %d ",
472 dev
->width
, dev
->height
, dev
->input
);
473 gen_text(dev
, vbuf
, line
++ * 16, 16, str
);
475 snprintf(str
, sizeof(str
), " brightness %3d, contrast %3d, saturation %3d, hue %d ",
480 gen_text(dev
, vbuf
, line
++ * 16, 16, str
);
481 snprintf(str
, sizeof(str
), " volume %3d ", dev
->volume
);
482 gen_text(dev
, vbuf
, line
++ * 16, 16, str
);
486 /* Advice that buffer was filled */
487 buf
->vb
.field_count
++;
488 do_gettimeofday(&ts
);
490 buf
->vb
.state
= VIDEOBUF_DONE
;
493 static void vivi_thread_tick(struct vivi_dev
*dev
)
495 struct vivi_dmaqueue
*dma_q
= &dev
->vidq
;
496 struct vivi_buffer
*buf
;
497 unsigned long flags
= 0;
499 dprintk(dev
, 1, "Thread tick\n");
501 spin_lock_irqsave(&dev
->slock
, flags
);
502 if (list_empty(&dma_q
->active
)) {
503 dprintk(dev
, 1, "No active queue to serve\n");
507 buf
= list_entry(dma_q
->active
.next
,
508 struct vivi_buffer
, vb
.queue
);
510 /* Nobody is waiting on this buffer, return */
511 if (!waitqueue_active(&buf
->vb
.done
))
514 list_del(&buf
->vb
.queue
);
516 do_gettimeofday(&buf
->vb
.ts
);
519 vivi_fillbuff(dev
, buf
);
520 dprintk(dev
, 1, "filled buffer %p\n", buf
);
522 wake_up(&buf
->vb
.done
);
523 dprintk(dev
, 2, "[%p/%d] wakeup\n", buf
, buf
->vb
. i
);
525 spin_unlock_irqrestore(&dev
->slock
, flags
);
528 #define frames_to_ms(frames) \
529 ((frames * WAKE_NUMERATOR * 1000) / WAKE_DENOMINATOR)
531 static void vivi_sleep(struct vivi_dev
*dev
)
533 struct vivi_dmaqueue
*dma_q
= &dev
->vidq
;
535 DECLARE_WAITQUEUE(wait
, current
);
537 dprintk(dev
, 1, "%s dma_q=0x%08lx\n", __func__
,
538 (unsigned long)dma_q
);
540 add_wait_queue(&dma_q
->wq
, &wait
);
541 if (kthread_should_stop())
544 /* Calculate time to wake up */
545 timeout
= msecs_to_jiffies(frames_to_ms(1));
547 vivi_thread_tick(dev
);
549 schedule_timeout_interruptible(timeout
);
552 remove_wait_queue(&dma_q
->wq
, &wait
);
556 static int vivi_thread(void *data
)
558 struct vivi_dev
*dev
= data
;
560 dprintk(dev
, 1, "thread started\n");
567 if (kthread_should_stop())
570 dprintk(dev
, 1, "thread: exit\n");
574 static void vivi_start_generating(struct file
*file
)
576 struct vivi_dev
*dev
= video_drvdata(file
);
577 struct vivi_dmaqueue
*dma_q
= &dev
->vidq
;
579 dprintk(dev
, 1, "%s\n", __func__
);
581 if (test_and_set_bit(0, &dev
->generating
))
583 file
->private_data
= dev
;
585 /* Resets frame counters */
588 dev
->jiffies
= jiffies
;
591 dma_q
->ini_jiffies
= jiffies
;
592 dma_q
->kthread
= kthread_run(vivi_thread
, dev
, dev
->v4l2_dev
.name
);
594 if (IS_ERR(dma_q
->kthread
)) {
595 v4l2_err(&dev
->v4l2_dev
, "kernel_thread() failed\n");
596 clear_bit(0, &dev
->generating
);
600 wake_up_interruptible(&dma_q
->wq
);
602 dprintk(dev
, 1, "returning from %s\n", __func__
);
605 static void vivi_stop_generating(struct file
*file
)
607 struct vivi_dev
*dev
= video_drvdata(file
);
608 struct vivi_dmaqueue
*dma_q
= &dev
->vidq
;
610 dprintk(dev
, 1, "%s\n", __func__
);
612 if (!file
->private_data
)
614 if (!test_and_clear_bit(0, &dev
->generating
))
617 /* shutdown control thread */
618 if (dma_q
->kthread
) {
619 kthread_stop(dma_q
->kthread
);
620 dma_q
->kthread
= NULL
;
622 videobuf_stop(&dev
->vb_vidq
);
623 videobuf_mmap_free(&dev
->vb_vidq
);
626 static int vivi_is_generating(struct vivi_dev
*dev
)
628 return test_bit(0, &dev
->generating
);
631 /* ------------------------------------------------------------------
633 ------------------------------------------------------------------*/
635 buffer_setup(struct videobuf_queue
*vq
, unsigned int *count
, unsigned int *size
)
637 struct vivi_dev
*dev
= vq
->priv_data
;
639 *size
= dev
->width
* dev
->height
* 2;
644 while (*size
* *count
> vid_limit
* 1024 * 1024)
647 dprintk(dev
, 1, "%s, count=%d, size=%d\n", __func__
,
653 static void free_buffer(struct videobuf_queue
*vq
, struct vivi_buffer
*buf
)
655 struct vivi_dev
*dev
= vq
->priv_data
;
657 dprintk(dev
, 1, "%s, state: %i\n", __func__
, buf
->vb
.state
);
659 videobuf_vmalloc_free(&buf
->vb
);
660 dprintk(dev
, 1, "free_buffer: freed\n");
661 buf
->vb
.state
= VIDEOBUF_NEEDS_INIT
;
665 buffer_prepare(struct videobuf_queue
*vq
, struct videobuf_buffer
*vb
,
666 enum v4l2_field field
)
668 struct vivi_dev
*dev
= vq
->priv_data
;
669 struct vivi_buffer
*buf
= container_of(vb
, struct vivi_buffer
, vb
);
672 dprintk(dev
, 1, "%s, field=%d\n", __func__
, field
);
674 BUG_ON(NULL
== dev
->fmt
);
676 if (dev
->width
< 48 || dev
->width
> MAX_WIDTH
||
677 dev
->height
< 32 || dev
->height
> MAX_HEIGHT
)
680 buf
->vb
.size
= dev
->width
* dev
->height
* 2;
681 if (0 != buf
->vb
.baddr
&& buf
->vb
.bsize
< buf
->vb
.size
)
684 /* These properties only change when queue is idle, see s_fmt */
686 buf
->vb
.width
= dev
->width
;
687 buf
->vb
.height
= dev
->height
;
688 buf
->vb
.field
= field
;
690 precalculate_bars(dev
);
691 precalculate_line(dev
);
693 if (VIDEOBUF_NEEDS_INIT
== buf
->vb
.state
) {
694 rc
= videobuf_iolock(vq
, &buf
->vb
, NULL
);
699 buf
->vb
.state
= VIDEOBUF_PREPARED
;
703 free_buffer(vq
, buf
);
708 buffer_queue(struct videobuf_queue
*vq
, struct videobuf_buffer
*vb
)
710 struct vivi_dev
*dev
= vq
->priv_data
;
711 struct vivi_buffer
*buf
= container_of(vb
, struct vivi_buffer
, vb
);
712 struct vivi_dmaqueue
*vidq
= &dev
->vidq
;
714 dprintk(dev
, 1, "%s\n", __func__
);
716 buf
->vb
.state
= VIDEOBUF_QUEUED
;
717 list_add_tail(&buf
->vb
.queue
, &vidq
->active
);
720 static void buffer_release(struct videobuf_queue
*vq
,
721 struct videobuf_buffer
*vb
)
723 struct vivi_dev
*dev
= vq
->priv_data
;
724 struct vivi_buffer
*buf
= container_of(vb
, struct vivi_buffer
, vb
);
726 dprintk(dev
, 1, "%s\n", __func__
);
728 free_buffer(vq
, buf
);
731 static struct videobuf_queue_ops vivi_video_qops
= {
732 .buf_setup
= buffer_setup
,
733 .buf_prepare
= buffer_prepare
,
734 .buf_queue
= buffer_queue
,
735 .buf_release
= buffer_release
,
738 /* ------------------------------------------------------------------
739 IOCTL vidioc handling
740 ------------------------------------------------------------------*/
741 static int vidioc_querycap(struct file
*file
, void *priv
,
742 struct v4l2_capability
*cap
)
744 struct vivi_dev
*dev
= video_drvdata(file
);
746 strcpy(cap
->driver
, "vivi");
747 strcpy(cap
->card
, "vivi");
748 strlcpy(cap
->bus_info
, dev
->v4l2_dev
.name
, sizeof(cap
->bus_info
));
749 cap
->version
= VIVI_VERSION
;
750 cap
->capabilities
= V4L2_CAP_VIDEO_CAPTURE
| V4L2_CAP_STREAMING
| \
755 static int vidioc_enum_fmt_vid_cap(struct file
*file
, void *priv
,
756 struct v4l2_fmtdesc
*f
)
758 struct vivi_fmt
*fmt
;
760 if (f
->index
>= ARRAY_SIZE(formats
))
763 fmt
= &formats
[f
->index
];
765 strlcpy(f
->description
, fmt
->name
, sizeof(f
->description
));
766 f
->pixelformat
= fmt
->fourcc
;
770 static int vidioc_g_fmt_vid_cap(struct file
*file
, void *priv
,
771 struct v4l2_format
*f
)
773 struct vivi_dev
*dev
= video_drvdata(file
);
775 f
->fmt
.pix
.width
= dev
->width
;
776 f
->fmt
.pix
.height
= dev
->height
;
777 f
->fmt
.pix
.field
= dev
->vb_vidq
.field
;
778 f
->fmt
.pix
.pixelformat
= dev
->fmt
->fourcc
;
779 f
->fmt
.pix
.bytesperline
=
780 (f
->fmt
.pix
.width
* dev
->fmt
->depth
) >> 3;
781 f
->fmt
.pix
.sizeimage
=
782 f
->fmt
.pix
.height
* f
->fmt
.pix
.bytesperline
;
786 static int vidioc_try_fmt_vid_cap(struct file
*file
, void *priv
,
787 struct v4l2_format
*f
)
789 struct vivi_dev
*dev
= video_drvdata(file
);
790 struct vivi_fmt
*fmt
;
791 enum v4l2_field field
;
795 dprintk(dev
, 1, "Fourcc format (0x%08x) invalid.\n",
796 f
->fmt
.pix
.pixelformat
);
800 field
= f
->fmt
.pix
.field
;
802 if (field
== V4L2_FIELD_ANY
) {
803 field
= V4L2_FIELD_INTERLACED
;
804 } else if (V4L2_FIELD_INTERLACED
!= field
) {
805 dprintk(dev
, 1, "Field type invalid.\n");
809 f
->fmt
.pix
.field
= field
;
810 v4l_bound_align_image(&f
->fmt
.pix
.width
, 48, MAX_WIDTH
, 2,
811 &f
->fmt
.pix
.height
, 32, MAX_HEIGHT
, 0, 0);
812 f
->fmt
.pix
.bytesperline
=
813 (f
->fmt
.pix
.width
* fmt
->depth
) >> 3;
814 f
->fmt
.pix
.sizeimage
=
815 f
->fmt
.pix
.height
* f
->fmt
.pix
.bytesperline
;
819 static int vidioc_s_fmt_vid_cap(struct file
*file
, void *priv
,
820 struct v4l2_format
*f
)
822 struct vivi_dev
*dev
= video_drvdata(file
);
824 int ret
= vidioc_try_fmt_vid_cap(file
, priv
, f
);
828 if (vivi_is_generating(dev
)) {
829 dprintk(dev
, 1, "%s device busy\n", __func__
);
834 dev
->fmt
= get_format(f
);
835 dev
->width
= f
->fmt
.pix
.width
;
836 dev
->height
= f
->fmt
.pix
.height
;
837 dev
->vb_vidq
.field
= f
->fmt
.pix
.field
;
843 static int vidioc_reqbufs(struct file
*file
, void *priv
,
844 struct v4l2_requestbuffers
*p
)
846 struct vivi_dev
*dev
= video_drvdata(file
);
848 return videobuf_reqbufs(&dev
->vb_vidq
, p
);
851 static int vidioc_querybuf(struct file
*file
, void *priv
, struct v4l2_buffer
*p
)
853 struct vivi_dev
*dev
= video_drvdata(file
);
855 return videobuf_querybuf(&dev
->vb_vidq
, p
);
858 static int vidioc_qbuf(struct file
*file
, void *priv
, struct v4l2_buffer
*p
)
860 struct vivi_dev
*dev
= video_drvdata(file
);
862 return videobuf_qbuf(&dev
->vb_vidq
, p
);
865 static int vidioc_dqbuf(struct file
*file
, void *priv
, struct v4l2_buffer
*p
)
867 struct vivi_dev
*dev
= video_drvdata(file
);
869 return videobuf_dqbuf(&dev
->vb_vidq
, p
,
870 file
->f_flags
& O_NONBLOCK
);
873 #ifdef CONFIG_VIDEO_V4L1_COMPAT
874 static int vidiocgmbuf(struct file
*file
, void *priv
, struct video_mbuf
*mbuf
)
876 struct vivi_dev
*dev
= video_drvdata(file
);
878 return videobuf_cgmbuf(&dev
->vb_vidq
, mbuf
, 8);
882 static int vidioc_streamon(struct file
*file
, void *priv
, enum v4l2_buf_type i
)
884 struct vivi_dev
*dev
= video_drvdata(file
);
887 if (i
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
889 ret
= videobuf_streamon(&dev
->vb_vidq
);
893 vivi_start_generating(file
);
897 static int vidioc_streamoff(struct file
*file
, void *priv
, enum v4l2_buf_type i
)
899 struct vivi_dev
*dev
= video_drvdata(file
);
902 if (i
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
904 ret
= videobuf_streamoff(&dev
->vb_vidq
);
906 vivi_stop_generating(file
);
910 static int vidioc_s_std(struct file
*file
, void *priv
, v4l2_std_id
*i
)
915 /* only one input in this sample driver */
916 static int vidioc_enum_input(struct file
*file
, void *priv
,
917 struct v4l2_input
*inp
)
919 if (inp
->index
>= NUM_INPUTS
)
922 inp
->type
= V4L2_INPUT_TYPE_CAMERA
;
923 inp
->std
= V4L2_STD_525_60
;
924 sprintf(inp
->name
, "Camera %u", inp
->index
);
928 static int vidioc_g_input(struct file
*file
, void *priv
, unsigned int *i
)
930 struct vivi_dev
*dev
= video_drvdata(file
);
936 static int vidioc_s_input(struct file
*file
, void *priv
, unsigned int i
)
938 struct vivi_dev
*dev
= video_drvdata(file
);
944 precalculate_bars(dev
);
945 precalculate_line(dev
);
949 /* --- controls ---------------------------------------------- */
950 static int vidioc_queryctrl(struct file
*file
, void *priv
,
951 struct v4l2_queryctrl
*qc
)
954 case V4L2_CID_AUDIO_VOLUME
:
955 return v4l2_ctrl_query_fill(qc
, 0, 255, 1, 200);
956 case V4L2_CID_BRIGHTNESS
:
957 return v4l2_ctrl_query_fill(qc
, 0, 255, 1, 127);
958 case V4L2_CID_CONTRAST
:
959 return v4l2_ctrl_query_fill(qc
, 0, 255, 1, 16);
960 case V4L2_CID_SATURATION
:
961 return v4l2_ctrl_query_fill(qc
, 0, 255, 1, 127);
963 return v4l2_ctrl_query_fill(qc
, -128, 127, 1, 0);
968 static int vidioc_g_ctrl(struct file
*file
, void *priv
,
969 struct v4l2_control
*ctrl
)
971 struct vivi_dev
*dev
= video_drvdata(file
);
974 case V4L2_CID_AUDIO_VOLUME
:
975 ctrl
->value
= dev
->volume
;
977 case V4L2_CID_BRIGHTNESS
:
978 ctrl
->value
= dev
->brightness
;
980 case V4L2_CID_CONTRAST
:
981 ctrl
->value
= dev
->contrast
;
983 case V4L2_CID_SATURATION
:
984 ctrl
->value
= dev
->saturation
;
987 ctrl
->value
= dev
->hue
;
993 static int vidioc_s_ctrl(struct file
*file
, void *priv
,
994 struct v4l2_control
*ctrl
)
996 struct vivi_dev
*dev
= video_drvdata(file
);
997 struct v4l2_queryctrl qc
;
1001 err
= vidioc_queryctrl(file
, priv
, &qc
);
1004 if (ctrl
->value
< qc
.minimum
|| ctrl
->value
> qc
.maximum
)
1007 case V4L2_CID_AUDIO_VOLUME
:
1008 dev
->volume
= ctrl
->value
;
1010 case V4L2_CID_BRIGHTNESS
:
1011 dev
->brightness
= ctrl
->value
;
1013 case V4L2_CID_CONTRAST
:
1014 dev
->contrast
= ctrl
->value
;
1016 case V4L2_CID_SATURATION
:
1017 dev
->saturation
= ctrl
->value
;
1020 dev
->hue
= ctrl
->value
;
1026 /* ------------------------------------------------------------------
1027 File operations for the device
1028 ------------------------------------------------------------------*/
1031 vivi_read(struct file
*file
, char __user
*data
, size_t count
, loff_t
*ppos
)
1033 struct vivi_dev
*dev
= video_drvdata(file
);
1035 vivi_start_generating(file
);
1036 return videobuf_read_stream(&dev
->vb_vidq
, data
, count
, ppos
, 0,
1037 file
->f_flags
& O_NONBLOCK
);
1041 vivi_poll(struct file
*file
, struct poll_table_struct
*wait
)
1043 struct vivi_dev
*dev
= video_drvdata(file
);
1044 struct videobuf_queue
*q
= &dev
->vb_vidq
;
1046 dprintk(dev
, 1, "%s\n", __func__
);
1048 vivi_start_generating(file
);
1049 return videobuf_poll_stream(file
, q
, wait
);
1052 static int vivi_close(struct file
*file
)
1054 struct video_device
*vdev
= video_devdata(file
);
1055 struct vivi_dev
*dev
= video_drvdata(file
);
1057 vivi_stop_generating(file
);
1059 dprintk(dev
, 1, "close called (dev=%s)\n",
1060 video_device_node_name(vdev
));
1064 static int vivi_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1066 struct vivi_dev
*dev
= video_drvdata(file
);
1069 dprintk(dev
, 1, "mmap called, vma=0x%08lx\n", (unsigned long)vma
);
1071 ret
= videobuf_mmap_mapper(&dev
->vb_vidq
, vma
);
1073 dprintk(dev
, 1, "vma start=0x%08lx, size=%ld, ret=%d\n",
1074 (unsigned long)vma
->vm_start
,
1075 (unsigned long)vma
->vm_end
- (unsigned long)vma
->vm_start
,
1080 static const struct v4l2_file_operations vivi_fops
= {
1081 .owner
= THIS_MODULE
,
1082 .release
= vivi_close
,
1085 .unlocked_ioctl
= video_ioctl2
, /* V4L2 ioctl handler */
1089 static const struct v4l2_ioctl_ops vivi_ioctl_ops
= {
1090 .vidioc_querycap
= vidioc_querycap
,
1091 .vidioc_enum_fmt_vid_cap
= vidioc_enum_fmt_vid_cap
,
1092 .vidioc_g_fmt_vid_cap
= vidioc_g_fmt_vid_cap
,
1093 .vidioc_try_fmt_vid_cap
= vidioc_try_fmt_vid_cap
,
1094 .vidioc_s_fmt_vid_cap
= vidioc_s_fmt_vid_cap
,
1095 .vidioc_reqbufs
= vidioc_reqbufs
,
1096 .vidioc_querybuf
= vidioc_querybuf
,
1097 .vidioc_qbuf
= vidioc_qbuf
,
1098 .vidioc_dqbuf
= vidioc_dqbuf
,
1099 .vidioc_s_std
= vidioc_s_std
,
1100 .vidioc_enum_input
= vidioc_enum_input
,
1101 .vidioc_g_input
= vidioc_g_input
,
1102 .vidioc_s_input
= vidioc_s_input
,
1103 .vidioc_streamon
= vidioc_streamon
,
1104 .vidioc_streamoff
= vidioc_streamoff
,
1105 .vidioc_queryctrl
= vidioc_queryctrl
,
1106 .vidioc_g_ctrl
= vidioc_g_ctrl
,
1107 .vidioc_s_ctrl
= vidioc_s_ctrl
,
1108 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1109 .vidiocgmbuf
= vidiocgmbuf
,
1113 static struct video_device vivi_template
= {
1116 .ioctl_ops
= &vivi_ioctl_ops
,
1117 .release
= video_device_release
,
1119 .tvnorms
= V4L2_STD_525_60
,
1120 .current_norm
= V4L2_STD_NTSC_M
,
1123 /* -----------------------------------------------------------------
1124 Initialization and module stuff
1125 ------------------------------------------------------------------*/
1127 static int vivi_release(void)
1129 struct vivi_dev
*dev
;
1130 struct list_head
*list
;
1132 while (!list_empty(&vivi_devlist
)) {
1133 list
= vivi_devlist
.next
;
1135 dev
= list_entry(list
, struct vivi_dev
, vivi_devlist
);
1137 v4l2_info(&dev
->v4l2_dev
, "unregistering %s\n",
1138 video_device_node_name(dev
->vfd
));
1139 video_unregister_device(dev
->vfd
);
1140 v4l2_device_unregister(&dev
->v4l2_dev
);
1147 static int __init
vivi_create_instance(int inst
)
1149 struct vivi_dev
*dev
;
1150 struct video_device
*vfd
;
1153 dev
= kzalloc(sizeof(*dev
), GFP_KERNEL
);
1157 snprintf(dev
->v4l2_dev
.name
, sizeof(dev
->v4l2_dev
.name
),
1158 "%s-%03d", VIVI_MODULE_NAME
, inst
);
1159 ret
= v4l2_device_register(NULL
, &dev
->v4l2_dev
);
1163 dev
->fmt
= &formats
[0];
1167 dev
->brightness
= 127;
1169 dev
->saturation
= 127;
1172 /* initialize locks */
1173 spin_lock_init(&dev
->slock
);
1174 mutex_init(&dev
->mutex
);
1176 videobuf_queue_vmalloc_init(&dev
->vb_vidq
, &vivi_video_qops
,
1177 NULL
, &dev
->slock
, V4L2_BUF_TYPE_VIDEO_CAPTURE
,
1178 V4L2_FIELD_INTERLACED
,
1179 sizeof(struct vivi_buffer
), dev
, &dev
->mutex
);
1181 /* init video dma queues */
1182 INIT_LIST_HEAD(&dev
->vidq
.active
);
1183 init_waitqueue_head(&dev
->vidq
.wq
);
1186 vfd
= video_device_alloc();
1190 *vfd
= vivi_template
;
1192 vfd
->v4l2_dev
= &dev
->v4l2_dev
;
1193 vfd
->lock
= &dev
->mutex
;
1195 ret
= video_register_device(vfd
, VFL_TYPE_GRABBER
, video_nr
);
1199 video_set_drvdata(vfd
, dev
);
1201 /* Now that everything is fine, let's add it to device list */
1202 list_add_tail(&dev
->vivi_devlist
, &vivi_devlist
);
1208 v4l2_info(&dev
->v4l2_dev
, "V4L2 device registered as %s\n",
1209 video_device_node_name(vfd
));
1213 video_device_release(vfd
);
1215 v4l2_device_unregister(&dev
->v4l2_dev
);
1221 /* This routine allocates from 1 to n_devs virtual drivers.
1223 The real maximum number of virtual drivers will depend on how many drivers
1224 will succeed. This is limited to the maximum number of devices that
1225 videodev supports, which is equal to VIDEO_NUM_DEVICES.
1227 static int __init
vivi_init(void)
1229 const struct font_desc
*font
= find_font("VGA8x16");
1233 printk(KERN_ERR
"vivi: could not find font\n");
1236 font8x16
= font
->data
;
1241 for (i
= 0; i
< n_devs
; i
++) {
1242 ret
= vivi_create_instance(i
);
1244 /* If some instantiations succeeded, keep driver */
1252 printk(KERN_ERR
"vivi: error %d while loading driver\n", ret
);
1256 printk(KERN_INFO
"Video Technology Magazine Virtual Video "
1257 "Capture Board ver %u.%u.%u successfully loaded.\n",
1258 (VIVI_VERSION
>> 16) & 0xFF, (VIVI_VERSION
>> 8) & 0xFF,
1259 VIVI_VERSION
& 0xFF);
1261 /* n_devs will reflect the actual number of allocated devices */
1267 static void __exit
vivi_exit(void)
1272 module_init(vivi_init
);
1273 module_exit(vivi_exit
);