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/font.h>
21 #include <linux/version.h>
22 #include <linux/mutex.h>
23 #include <linux/videodev2.h>
24 #include <linux/kthread.h>
25 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 20)
26 #include <linux/freezer.h>
28 #include <media/videobuf-vmalloc.h>
29 #include <media/v4l2-device.h>
30 #include <media/v4l2-ioctl.h>
31 #include <media/v4l2-common.h>
33 #define VIVI_MODULE_NAME "vivi"
35 /* Wake up at about 30 fps */
36 #define WAKE_NUMERATOR 30
37 #define WAKE_DENOMINATOR 1001
38 #define BUFFER_TIMEOUT msecs_to_jiffies(500) /* 0.5 seconds */
40 #define MAX_WIDTH 1920
41 #define MAX_HEIGHT 1200
43 #define VIVI_MAJOR_VERSION 0
44 #define VIVI_MINOR_VERSION 7
45 #define VIVI_RELEASE 0
46 #define VIVI_VERSION \
47 KERNEL_VERSION(VIVI_MAJOR_VERSION, VIVI_MINOR_VERSION, VIVI_RELEASE)
49 MODULE_DESCRIPTION("Video Technology Magazine Virtual Video Capture Board");
50 MODULE_AUTHOR("Mauro Carvalho Chehab, Ted Walther and John Sokol");
51 MODULE_LICENSE("Dual BSD/GPL");
53 static unsigned video_nr
= -1;
54 module_param(video_nr
, uint
, 0644);
55 MODULE_PARM_DESC(video_nr
, "videoX start number, -1 is autodetect");
57 static unsigned n_devs
= 1;
58 module_param(n_devs
, uint
, 0644);
59 MODULE_PARM_DESC(n_devs
, "number of video devices to create");
61 static unsigned debug
;
62 module_param(debug
, uint
, 0644);
63 MODULE_PARM_DESC(debug
, "activates debug info");
65 static unsigned int vid_limit
= 16;
66 module_param(vid_limit
, uint
, 0644);
67 MODULE_PARM_DESC(vid_limit
, "capture memory limit in megabytes");
69 /* Global font descriptor */
70 static const u8
*font8x16
;
72 #define dprintk(dev, level, fmt, arg...) \
73 v4l2_dbg(level, debug, &dev->v4l2_dev, fmt, ## arg)
75 /* ------------------------------------------------------------------
77 ------------------------------------------------------------------*/
81 u32 fourcc
; /* v4l2 format id */
85 static struct vivi_fmt formats
[] = {
87 .name
= "4:2:2, packed, YUYV",
88 .fourcc
= V4L2_PIX_FMT_YUYV
,
92 .name
= "4:2:2, packed, UYVY",
93 .fourcc
= V4L2_PIX_FMT_UYVY
,
97 .name
= "RGB565 (LE)",
98 .fourcc
= V4L2_PIX_FMT_RGB565
, /* gggbbbbb rrrrrggg */
102 .name
= "RGB565 (BE)",
103 .fourcc
= V4L2_PIX_FMT_RGB565X
, /* rrrrrggg gggbbbbb */
107 .name
= "RGB555 (LE)",
108 .fourcc
= V4L2_PIX_FMT_RGB555
, /* gggbbbbb arrrrrgg */
112 .name
= "RGB555 (BE)",
113 .fourcc
= V4L2_PIX_FMT_RGB555X
, /* arrrrrgg gggbbbbb */
118 static struct vivi_fmt
*get_format(struct v4l2_format
*f
)
120 struct vivi_fmt
*fmt
;
123 for (k
= 0; k
< ARRAY_SIZE(formats
); k
++) {
125 if (fmt
->fourcc
== f
->fmt
.pix
.pixelformat
)
129 if (k
== ARRAY_SIZE(formats
))
137 struct scatterlist
*sg
;
140 /* buffer for one video frame */
142 /* common v4l buffer stuff -- must be first */
143 struct videobuf_buffer vb
;
145 struct vivi_fmt
*fmt
;
148 struct vivi_dmaqueue
{
149 struct list_head active
;
151 /* thread for generating video stream*/
152 struct task_struct
*kthread
;
153 wait_queue_head_t wq
;
154 /* Counters to control fps rate */
159 static LIST_HEAD(vivi_devlist
);
162 struct list_head vivi_devlist
;
163 struct v4l2_device v4l2_dev
;
175 /* various device info */
176 struct video_device
*vfd
;
178 struct vivi_dmaqueue vidq
;
180 /* Several counters */
182 unsigned long jiffies
;
184 int mv_count
; /* Controls bars movement */
190 struct vivi_fmt
*fmt
;
191 unsigned int width
, height
;
192 struct videobuf_queue vb_vidq
;
194 unsigned long generating
;
196 u8 line
[MAX_WIDTH
* 4];
199 /* ------------------------------------------------------------------
200 DMA and thread functions
201 ------------------------------------------------------------------*/
203 /* Bars and Colors should match positions */
218 #define COLOR_WHITE {204, 204, 204}
219 #define COLOR_AMBER {208, 208, 0}
220 #define COLOR_CYAN { 0, 206, 206}
221 #define COLOR_GREEN { 0, 239, 0}
222 #define COLOR_MAGENTA {239, 0, 239}
223 #define COLOR_RED {205, 0, 0}
224 #define COLOR_BLUE { 0, 0, 255}
225 #define COLOR_BLACK { 0, 0, 0}
231 /* Maximum number of bars are 10 - otherwise, the input print code
232 should be modified */
233 static struct bar_std bars
[] = {
234 { /* Standard ITU-R color bar sequence */
235 { COLOR_WHITE
, COLOR_AMBER
, COLOR_CYAN
, COLOR_GREEN
,
236 COLOR_MAGENTA
, COLOR_RED
, COLOR_BLUE
, COLOR_BLACK
, COLOR_BLACK
}
238 { COLOR_WHITE
, COLOR_AMBER
, COLOR_BLACK
, COLOR_WHITE
,
239 COLOR_AMBER
, COLOR_BLACK
, COLOR_WHITE
, COLOR_AMBER
, COLOR_BLACK
}
241 { COLOR_WHITE
, COLOR_CYAN
, COLOR_BLACK
, COLOR_WHITE
,
242 COLOR_CYAN
, COLOR_BLACK
, COLOR_WHITE
, COLOR_CYAN
, COLOR_BLACK
}
244 { COLOR_WHITE
, COLOR_GREEN
, COLOR_BLACK
, COLOR_WHITE
,
245 COLOR_GREEN
, COLOR_BLACK
, COLOR_WHITE
, COLOR_GREEN
, COLOR_BLACK
}
249 #define NUM_INPUTS ARRAY_SIZE(bars)
251 #define TO_Y(r, g, b) \
252 (((16829 * r + 33039 * g + 6416 * b + 32768) >> 16) + 16)
253 /* RGB to V(Cr) Color transform */
254 #define TO_V(r, g, b) \
255 (((28784 * r - 24103 * g - 4681 * b + 32768) >> 16) + 128)
256 /* RGB to U(Cb) Color transform */
257 #define TO_U(r, g, b) \
258 (((-9714 * r - 19070 * g + 28784 * b + 32768) >> 16) + 128)
260 /* precalculate color bar values to speed up rendering */
261 static void precalculate_bars(struct vivi_dev
*dev
)
266 for (k
= 0; k
< 9; k
++) {
267 r
= bars
[dev
->input
].bar
[k
][0];
268 g
= bars
[dev
->input
].bar
[k
][1];
269 b
= bars
[dev
->input
].bar
[k
][2];
272 switch (dev
->fmt
->fourcc
) {
273 case V4L2_PIX_FMT_YUYV
:
274 case V4L2_PIX_FMT_UYVY
:
277 case V4L2_PIX_FMT_RGB565
:
278 case V4L2_PIX_FMT_RGB565X
:
283 case V4L2_PIX_FMT_RGB555
:
284 case V4L2_PIX_FMT_RGB555X
:
292 dev
->bars
[k
][0] = TO_Y(r
, g
, b
); /* Luma */
293 dev
->bars
[k
][1] = TO_U(r
, g
, b
); /* Cb */
294 dev
->bars
[k
][2] = TO_V(r
, g
, b
); /* Cr */
303 #define TSTAMP_MIN_Y 24
304 #define TSTAMP_MAX_Y (TSTAMP_MIN_Y + 15)
305 #define TSTAMP_INPUT_X 10
306 #define TSTAMP_MIN_X (54 + TSTAMP_INPUT_X)
308 static void gen_twopix(struct vivi_dev
*dev
, u8
*buf
, int colorpos
)
314 r_y
= dev
->bars
[colorpos
][0]; /* R or precalculated Y */
315 g_u
= dev
->bars
[colorpos
][1]; /* G or precalculated U */
316 b_v
= dev
->bars
[colorpos
][2]; /* B or precalculated V */
318 for (color
= 0; color
< 4; color
++) {
321 switch (dev
->fmt
->fourcc
) {
322 case V4L2_PIX_FMT_YUYV
:
336 case V4L2_PIX_FMT_UYVY
:
350 case V4L2_PIX_FMT_RGB565
:
354 *p
= (g_u
<< 5) | b_v
;
358 *p
= (r_y
<< 3) | (g_u
>> 3);
362 case V4L2_PIX_FMT_RGB565X
:
366 *p
= (r_y
<< 3) | (g_u
>> 3);
370 *p
= (g_u
<< 5) | b_v
;
374 case V4L2_PIX_FMT_RGB555
:
378 *p
= (g_u
<< 5) | b_v
;
382 *p
= (r_y
<< 2) | (g_u
>> 3);
386 case V4L2_PIX_FMT_RGB555X
:
390 *p
= (r_y
<< 2) | (g_u
>> 3);
394 *p
= (g_u
<< 5) | b_v
;
402 static void precalculate_line(struct vivi_dev
*dev
)
406 for (w
= 0; w
< dev
->width
* 2; w
+= 2) {
407 int colorpos
= (w
/ (dev
->width
/ 8) % 8);
409 gen_twopix(dev
, dev
->line
+ w
* 2, colorpos
);
413 static void gen_text(struct vivi_dev
*dev
, char *basep
,
414 int y
, int x
, char *text
)
418 /* Checks if it is possible to show string */
419 if (y
+ 16 >= dev
->height
|| x
+ strlen(text
) * 8 >= dev
->width
)
422 /* Print stream time */
423 for (line
= y
; line
< y
+ 16; line
++) {
425 char *pos
= basep
+ line
* dev
->width
* 2 + x
* 2;
428 for (s
= text
; *s
; s
++) {
429 u8 chr
= font8x16
[*s
* 16 + line
- y
];
432 for (i
= 0; i
< 7; i
++, j
++) {
433 /* Draw white font on black background */
434 if (chr
& (1 << (7 - i
)))
435 gen_twopix(dev
, pos
+ j
* 2, WHITE
);
437 gen_twopix(dev
, pos
+ j
* 2, TEXT_BLACK
);
443 static void vivi_fillbuff(struct vivi_dev
*dev
, struct vivi_buffer
*buf
)
445 int hmax
= buf
->vb
.height
;
446 int wmax
= buf
->vb
.width
;
448 void *vbuf
= videobuf_to_vmalloc(&buf
->vb
);
456 for (h
= 0; h
< hmax
; h
++)
457 memcpy(vbuf
+ h
* wmax
* 2, dev
->line
+ (dev
->mv_count
% wmax
) * 2, wmax
* 2);
459 /* Updates stream time */
461 dev
->ms
+= jiffies_to_msecs(jiffies
- dev
->jiffies
);
462 dev
->jiffies
= jiffies
;
464 snprintf(str
, sizeof(str
), " %02d:%02d:%02d:%03d ",
465 (ms
/ (60 * 60 * 1000)) % 24,
466 (ms
/ (60 * 1000)) % 60,
469 gen_text(dev
, vbuf
, line
++ * 16, 16, str
);
470 snprintf(str
, sizeof(str
), " %dx%d, input %d ",
471 dev
->width
, dev
->height
, dev
->input
);
472 gen_text(dev
, vbuf
, line
++ * 16, 16, str
);
474 snprintf(str
, sizeof(str
), " brightness %3d, contrast %3d, saturation %3d, hue %d ",
479 gen_text(dev
, vbuf
, line
++ * 16, 16, str
);
480 snprintf(str
, sizeof(str
), " volume %3d ", dev
->volume
);
481 gen_text(dev
, vbuf
, line
++ * 16, 16, str
);
485 /* Advice that buffer was filled */
486 buf
->vb
.field_count
++;
487 do_gettimeofday(&ts
);
489 buf
->vb
.state
= VIDEOBUF_DONE
;
492 static void vivi_thread_tick(struct vivi_dev
*dev
)
494 struct vivi_dmaqueue
*dma_q
= &dev
->vidq
;
495 struct vivi_buffer
*buf
;
496 unsigned long flags
= 0;
498 dprintk(dev
, 1, "Thread tick\n");
500 spin_lock_irqsave(&dev
->slock
, flags
);
501 if (list_empty(&dma_q
->active
)) {
502 dprintk(dev
, 1, "No active queue to serve\n");
506 buf
= list_entry(dma_q
->active
.next
,
507 struct vivi_buffer
, vb
.queue
);
509 /* Nobody is waiting on this buffer, return */
510 if (!waitqueue_active(&buf
->vb
.done
))
513 list_del(&buf
->vb
.queue
);
515 do_gettimeofday(&buf
->vb
.ts
);
518 vivi_fillbuff(dev
, buf
);
519 dprintk(dev
, 1, "filled buffer %p\n", buf
);
521 wake_up(&buf
->vb
.done
);
522 dprintk(dev
, 2, "[%p/%d] wakeup\n", buf
, buf
->vb
. i
);
524 spin_unlock_irqrestore(&dev
->slock
, flags
);
527 #define frames_to_ms(frames) \
528 ((frames * WAKE_NUMERATOR * 1000) / WAKE_DENOMINATOR)
530 static void vivi_sleep(struct vivi_dev
*dev
)
532 struct vivi_dmaqueue
*dma_q
= &dev
->vidq
;
534 DECLARE_WAITQUEUE(wait
, current
);
536 dprintk(dev
, 1, "%s dma_q=0x%08lx\n", __func__
,
537 (unsigned long)dma_q
);
539 add_wait_queue(&dma_q
->wq
, &wait
);
540 if (kthread_should_stop())
543 /* Calculate time to wake up */
544 timeout
= msecs_to_jiffies(frames_to_ms(1));
546 vivi_thread_tick(dev
);
548 schedule_timeout_interruptible(timeout
);
551 remove_wait_queue(&dma_q
->wq
, &wait
);
555 static int vivi_thread(void *data
)
557 struct vivi_dev
*dev
= data
;
559 dprintk(dev
, 1, "thread started\n");
566 if (kthread_should_stop())
569 dprintk(dev
, 1, "thread: exit\n");
573 static void vivi_start_generating(struct file
*file
)
575 struct vivi_dev
*dev
= video_drvdata(file
);
576 struct vivi_dmaqueue
*dma_q
= &dev
->vidq
;
578 dprintk(dev
, 1, "%s\n", __func__
);
580 if (test_and_set_bit(0, &dev
->generating
))
582 file
->private_data
= dev
;
584 /* Resets frame counters */
587 dev
->jiffies
= jiffies
;
590 dma_q
->ini_jiffies
= jiffies
;
591 dma_q
->kthread
= kthread_run(vivi_thread
, dev
, dev
->v4l2_dev
.name
);
593 if (IS_ERR(dma_q
->kthread
)) {
594 v4l2_err(&dev
->v4l2_dev
, "kernel_thread() failed\n");
595 clear_bit(0, &dev
->generating
);
599 wake_up_interruptible(&dma_q
->wq
);
601 dprintk(dev
, 1, "returning from %s\n", __func__
);
604 static void vivi_stop_generating(struct file
*file
)
606 struct vivi_dev
*dev
= video_drvdata(file
);
607 struct vivi_dmaqueue
*dma_q
= &dev
->vidq
;
609 dprintk(dev
, 1, "%s\n", __func__
);
611 if (!file
->private_data
)
613 if (!test_and_clear_bit(0, &dev
->generating
))
616 /* shutdown control thread */
617 if (dma_q
->kthread
) {
618 kthread_stop(dma_q
->kthread
);
619 dma_q
->kthread
= NULL
;
621 videobuf_stop(&dev
->vb_vidq
);
622 videobuf_mmap_free(&dev
->vb_vidq
);
625 static int vivi_is_generating(struct vivi_dev
*dev
)
627 return test_bit(0, &dev
->generating
);
630 /* ------------------------------------------------------------------
632 ------------------------------------------------------------------*/
634 buffer_setup(struct videobuf_queue
*vq
, unsigned int *count
, unsigned int *size
)
636 struct vivi_dev
*dev
= vq
->priv_data
;
638 *size
= dev
->width
* dev
->height
* 2;
643 while (*size
* *count
> vid_limit
* 1024 * 1024)
646 dprintk(dev
, 1, "%s, count=%d, size=%d\n", __func__
,
652 static void free_buffer(struct videobuf_queue
*vq
, struct vivi_buffer
*buf
)
654 struct vivi_dev
*dev
= vq
->priv_data
;
656 dprintk(dev
, 1, "%s, state: %i\n", __func__
, buf
->vb
.state
);
658 videobuf_vmalloc_free(&buf
->vb
);
659 dprintk(dev
, 1, "free_buffer: freed\n");
660 buf
->vb
.state
= VIDEOBUF_NEEDS_INIT
;
664 buffer_prepare(struct videobuf_queue
*vq
, struct videobuf_buffer
*vb
,
665 enum v4l2_field field
)
667 struct vivi_dev
*dev
= vq
->priv_data
;
668 struct vivi_buffer
*buf
= container_of(vb
, struct vivi_buffer
, vb
);
671 dprintk(dev
, 1, "%s, field=%d\n", __func__
, field
);
673 BUG_ON(NULL
== dev
->fmt
);
675 if (dev
->width
< 48 || dev
->width
> MAX_WIDTH
||
676 dev
->height
< 32 || dev
->height
> MAX_HEIGHT
)
679 buf
->vb
.size
= dev
->width
* dev
->height
* 2;
680 if (0 != buf
->vb
.baddr
&& buf
->vb
.bsize
< buf
->vb
.size
)
683 /* These properties only change when queue is idle, see s_fmt */
685 buf
->vb
.width
= dev
->width
;
686 buf
->vb
.height
= dev
->height
;
687 buf
->vb
.field
= field
;
689 precalculate_bars(dev
);
690 precalculate_line(dev
);
692 if (VIDEOBUF_NEEDS_INIT
== buf
->vb
.state
) {
693 rc
= videobuf_iolock(vq
, &buf
->vb
, NULL
);
698 buf
->vb
.state
= VIDEOBUF_PREPARED
;
702 free_buffer(vq
, buf
);
707 buffer_queue(struct videobuf_queue
*vq
, struct videobuf_buffer
*vb
)
709 struct vivi_dev
*dev
= vq
->priv_data
;
710 struct vivi_buffer
*buf
= container_of(vb
, struct vivi_buffer
, vb
);
711 struct vivi_dmaqueue
*vidq
= &dev
->vidq
;
713 dprintk(dev
, 1, "%s\n", __func__
);
715 buf
->vb
.state
= VIDEOBUF_QUEUED
;
716 list_add_tail(&buf
->vb
.queue
, &vidq
->active
);
719 static void buffer_release(struct videobuf_queue
*vq
,
720 struct videobuf_buffer
*vb
)
722 struct vivi_dev
*dev
= vq
->priv_data
;
723 struct vivi_buffer
*buf
= container_of(vb
, struct vivi_buffer
, vb
);
725 dprintk(dev
, 1, "%s\n", __func__
);
727 free_buffer(vq
, buf
);
730 static struct videobuf_queue_ops vivi_video_qops
= {
731 .buf_setup
= buffer_setup
,
732 .buf_prepare
= buffer_prepare
,
733 .buf_queue
= buffer_queue
,
734 .buf_release
= buffer_release
,
737 /* ------------------------------------------------------------------
738 IOCTL vidioc handling
739 ------------------------------------------------------------------*/
740 static int vidioc_querycap(struct file
*file
, void *priv
,
741 struct v4l2_capability
*cap
)
743 struct vivi_dev
*dev
= video_drvdata(file
);
745 strcpy(cap
->driver
, "vivi");
746 strcpy(cap
->card
, "vivi");
747 strlcpy(cap
->bus_info
, dev
->v4l2_dev
.name
, sizeof(cap
->bus_info
));
748 cap
->version
= VIVI_VERSION
;
749 cap
->capabilities
= V4L2_CAP_VIDEO_CAPTURE
| V4L2_CAP_STREAMING
| \
754 static int vidioc_enum_fmt_vid_cap(struct file
*file
, void *priv
,
755 struct v4l2_fmtdesc
*f
)
757 struct vivi_fmt
*fmt
;
759 if (f
->index
>= ARRAY_SIZE(formats
))
762 fmt
= &formats
[f
->index
];
764 strlcpy(f
->description
, fmt
->name
, sizeof(f
->description
));
765 f
->pixelformat
= fmt
->fourcc
;
769 static int vidioc_g_fmt_vid_cap(struct file
*file
, void *priv
,
770 struct v4l2_format
*f
)
772 struct vivi_dev
*dev
= video_drvdata(file
);
774 f
->fmt
.pix
.width
= dev
->width
;
775 f
->fmt
.pix
.height
= dev
->height
;
776 f
->fmt
.pix
.field
= dev
->vb_vidq
.field
;
777 f
->fmt
.pix
.pixelformat
= dev
->fmt
->fourcc
;
778 f
->fmt
.pix
.bytesperline
=
779 (f
->fmt
.pix
.width
* dev
->fmt
->depth
) >> 3;
780 f
->fmt
.pix
.sizeimage
=
781 f
->fmt
.pix
.height
* f
->fmt
.pix
.bytesperline
;
785 static int vidioc_try_fmt_vid_cap(struct file
*file
, void *priv
,
786 struct v4l2_format
*f
)
788 struct vivi_dev
*dev
= video_drvdata(file
);
789 struct vivi_fmt
*fmt
;
790 enum v4l2_field field
;
794 dprintk(dev
, 1, "Fourcc format (0x%08x) invalid.\n",
795 f
->fmt
.pix
.pixelformat
);
799 field
= f
->fmt
.pix
.field
;
801 if (field
== V4L2_FIELD_ANY
) {
802 field
= V4L2_FIELD_INTERLACED
;
803 } else if (V4L2_FIELD_INTERLACED
!= field
) {
804 dprintk(dev
, 1, "Field type invalid.\n");
808 f
->fmt
.pix
.field
= field
;
809 v4l_bound_align_image(&f
->fmt
.pix
.width
, 48, MAX_WIDTH
, 2,
810 &f
->fmt
.pix
.height
, 32, MAX_HEIGHT
, 0, 0);
811 f
->fmt
.pix
.bytesperline
=
812 (f
->fmt
.pix
.width
* fmt
->depth
) >> 3;
813 f
->fmt
.pix
.sizeimage
=
814 f
->fmt
.pix
.height
* f
->fmt
.pix
.bytesperline
;
818 static int vidioc_s_fmt_vid_cap(struct file
*file
, void *priv
,
819 struct v4l2_format
*f
)
821 struct vivi_dev
*dev
= video_drvdata(file
);
822 struct videobuf_queue
*q
= &dev
->vb_vidq
;
824 int ret
= vidioc_try_fmt_vid_cap(file
, priv
, f
);
828 mutex_lock(&q
->vb_lock
);
830 if (vivi_is_generating(dev
)) {
831 dprintk(dev
, 1, "%s device busy\n", __func__
);
836 dev
->fmt
= get_format(f
);
837 dev
->width
= f
->fmt
.pix
.width
;
838 dev
->height
= f
->fmt
.pix
.height
;
839 dev
->vb_vidq
.field
= f
->fmt
.pix
.field
;
842 mutex_unlock(&q
->vb_lock
);
846 static int vidioc_reqbufs(struct file
*file
, void *priv
,
847 struct v4l2_requestbuffers
*p
)
849 struct vivi_dev
*dev
= video_drvdata(file
);
851 return videobuf_reqbufs(&dev
->vb_vidq
, p
);
854 static int vidioc_querybuf(struct file
*file
, void *priv
, struct v4l2_buffer
*p
)
856 struct vivi_dev
*dev
= video_drvdata(file
);
858 return videobuf_querybuf(&dev
->vb_vidq
, p
);
861 static int vidioc_qbuf(struct file
*file
, void *priv
, struct v4l2_buffer
*p
)
863 struct vivi_dev
*dev
= video_drvdata(file
);
865 return videobuf_qbuf(&dev
->vb_vidq
, p
);
868 static int vidioc_dqbuf(struct file
*file
, void *priv
, struct v4l2_buffer
*p
)
870 struct vivi_dev
*dev
= video_drvdata(file
);
872 return videobuf_dqbuf(&dev
->vb_vidq
, p
,
873 file
->f_flags
& O_NONBLOCK
);
876 #ifdef CONFIG_VIDEO_V4L1_COMPAT
877 static int vidiocgmbuf(struct file
*file
, void *priv
, struct video_mbuf
*mbuf
)
879 struct vivi_dev
*dev
= video_drvdata(file
);
881 return videobuf_cgmbuf(&dev
->vb_vidq
, mbuf
, 8);
885 static int vidioc_streamon(struct file
*file
, void *priv
, enum v4l2_buf_type i
)
887 struct vivi_dev
*dev
= video_drvdata(file
);
890 if (i
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
892 ret
= videobuf_streamon(&dev
->vb_vidq
);
896 vivi_start_generating(file
);
900 static int vidioc_streamoff(struct file
*file
, void *priv
, enum v4l2_buf_type i
)
902 struct vivi_dev
*dev
= video_drvdata(file
);
905 if (i
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
907 ret
= videobuf_streamoff(&dev
->vb_vidq
);
909 vivi_stop_generating(file
);
913 static int vidioc_s_std(struct file
*file
, void *priv
, v4l2_std_id
*i
)
918 /* only one input in this sample driver */
919 static int vidioc_enum_input(struct file
*file
, void *priv
,
920 struct v4l2_input
*inp
)
922 if (inp
->index
>= NUM_INPUTS
)
925 inp
->type
= V4L2_INPUT_TYPE_CAMERA
;
926 inp
->std
= V4L2_STD_525_60
;
927 sprintf(inp
->name
, "Camera %u", inp
->index
);
931 static int vidioc_g_input(struct file
*file
, void *priv
, unsigned int *i
)
933 struct vivi_dev
*dev
= video_drvdata(file
);
939 static int vidioc_s_input(struct file
*file
, void *priv
, unsigned int i
)
941 struct vivi_dev
*dev
= video_drvdata(file
);
947 precalculate_bars(dev
);
948 precalculate_line(dev
);
952 /* --- controls ---------------------------------------------- */
953 static int vidioc_queryctrl(struct file
*file
, void *priv
,
954 struct v4l2_queryctrl
*qc
)
957 case V4L2_CID_AUDIO_VOLUME
:
958 return v4l2_ctrl_query_fill(qc
, 0, 255, 1, 200);
959 case V4L2_CID_BRIGHTNESS
:
960 return v4l2_ctrl_query_fill(qc
, 0, 255, 1, 127);
961 case V4L2_CID_CONTRAST
:
962 return v4l2_ctrl_query_fill(qc
, 0, 255, 1, 16);
963 case V4L2_CID_SATURATION
:
964 return v4l2_ctrl_query_fill(qc
, 0, 255, 1, 127);
966 return v4l2_ctrl_query_fill(qc
, -128, 127, 1, 0);
971 static int vidioc_g_ctrl(struct file
*file
, void *priv
,
972 struct v4l2_control
*ctrl
)
974 struct vivi_dev
*dev
= video_drvdata(file
);
977 case V4L2_CID_AUDIO_VOLUME
:
978 ctrl
->value
= dev
->volume
;
980 case V4L2_CID_BRIGHTNESS
:
981 ctrl
->value
= dev
->brightness
;
983 case V4L2_CID_CONTRAST
:
984 ctrl
->value
= dev
->contrast
;
986 case V4L2_CID_SATURATION
:
987 ctrl
->value
= dev
->saturation
;
990 ctrl
->value
= dev
->hue
;
996 static int vidioc_s_ctrl(struct file
*file
, void *priv
,
997 struct v4l2_control
*ctrl
)
999 struct vivi_dev
*dev
= video_drvdata(file
);
1000 struct v4l2_queryctrl qc
;
1004 err
= vidioc_queryctrl(file
, priv
, &qc
);
1007 if (ctrl
->value
< qc
.minimum
|| ctrl
->value
> qc
.maximum
)
1010 case V4L2_CID_AUDIO_VOLUME
:
1011 dev
->volume
= ctrl
->value
;
1013 case V4L2_CID_BRIGHTNESS
:
1014 dev
->brightness
= ctrl
->value
;
1016 case V4L2_CID_CONTRAST
:
1017 dev
->contrast
= ctrl
->value
;
1019 case V4L2_CID_SATURATION
:
1020 dev
->saturation
= ctrl
->value
;
1023 dev
->hue
= ctrl
->value
;
1029 /* ------------------------------------------------------------------
1030 File operations for the device
1031 ------------------------------------------------------------------*/
1034 vivi_read(struct file
*file
, char __user
*data
, size_t count
, loff_t
*ppos
)
1036 struct vivi_dev
*dev
= video_drvdata(file
);
1038 vivi_start_generating(file
);
1039 return videobuf_read_stream(&dev
->vb_vidq
, data
, count
, ppos
, 0,
1040 file
->f_flags
& O_NONBLOCK
);
1044 vivi_poll(struct file
*file
, struct poll_table_struct
*wait
)
1046 struct vivi_dev
*dev
= video_drvdata(file
);
1047 struct videobuf_queue
*q
= &dev
->vb_vidq
;
1049 dprintk(dev
, 1, "%s\n", __func__
);
1051 vivi_start_generating(file
);
1052 return videobuf_poll_stream(file
, q
, wait
);
1055 static int vivi_close(struct file
*file
)
1057 struct video_device
*vdev
= video_devdata(file
);
1058 struct vivi_dev
*dev
= video_drvdata(file
);
1060 vivi_stop_generating(file
);
1062 dprintk(dev
, 1, "close called (dev=%s)\n",
1063 video_device_node_name(vdev
));
1067 static int vivi_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1069 struct vivi_dev
*dev
= video_drvdata(file
);
1072 dprintk(dev
, 1, "mmap called, vma=0x%08lx\n", (unsigned long)vma
);
1074 ret
= videobuf_mmap_mapper(&dev
->vb_vidq
, vma
);
1076 dprintk(dev
, 1, "vma start=0x%08lx, size=%ld, ret=%d\n",
1077 (unsigned long)vma
->vm_start
,
1078 (unsigned long)vma
->vm_end
- (unsigned long)vma
->vm_start
,
1083 static const struct v4l2_file_operations vivi_fops
= {
1084 .owner
= THIS_MODULE
,
1085 .release
= vivi_close
,
1088 .ioctl
= video_ioctl2
, /* V4L2 ioctl handler */
1092 static const struct v4l2_ioctl_ops vivi_ioctl_ops
= {
1093 .vidioc_querycap
= vidioc_querycap
,
1094 .vidioc_enum_fmt_vid_cap
= vidioc_enum_fmt_vid_cap
,
1095 .vidioc_g_fmt_vid_cap
= vidioc_g_fmt_vid_cap
,
1096 .vidioc_try_fmt_vid_cap
= vidioc_try_fmt_vid_cap
,
1097 .vidioc_s_fmt_vid_cap
= vidioc_s_fmt_vid_cap
,
1098 .vidioc_reqbufs
= vidioc_reqbufs
,
1099 .vidioc_querybuf
= vidioc_querybuf
,
1100 .vidioc_qbuf
= vidioc_qbuf
,
1101 .vidioc_dqbuf
= vidioc_dqbuf
,
1102 .vidioc_s_std
= vidioc_s_std
,
1103 .vidioc_enum_input
= vidioc_enum_input
,
1104 .vidioc_g_input
= vidioc_g_input
,
1105 .vidioc_s_input
= vidioc_s_input
,
1106 .vidioc_streamon
= vidioc_streamon
,
1107 .vidioc_streamoff
= vidioc_streamoff
,
1108 .vidioc_queryctrl
= vidioc_queryctrl
,
1109 .vidioc_g_ctrl
= vidioc_g_ctrl
,
1110 .vidioc_s_ctrl
= vidioc_s_ctrl
,
1111 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1112 .vidiocgmbuf
= vidiocgmbuf
,
1116 static struct video_device vivi_template
= {
1119 .ioctl_ops
= &vivi_ioctl_ops
,
1120 .release
= video_device_release
,
1122 .tvnorms
= V4L2_STD_525_60
,
1123 .current_norm
= V4L2_STD_NTSC_M
,
1126 /* -----------------------------------------------------------------
1127 Initialization and module stuff
1128 ------------------------------------------------------------------*/
1130 static int vivi_release(void)
1132 struct vivi_dev
*dev
;
1133 struct list_head
*list
;
1135 while (!list_empty(&vivi_devlist
)) {
1136 list
= vivi_devlist
.next
;
1138 dev
= list_entry(list
, struct vivi_dev
, vivi_devlist
);
1140 v4l2_info(&dev
->v4l2_dev
, "unregistering %s\n",
1141 video_device_node_name(dev
->vfd
));
1142 video_unregister_device(dev
->vfd
);
1143 v4l2_device_unregister(&dev
->v4l2_dev
);
1150 static int __init
vivi_create_instance(int inst
)
1152 struct vivi_dev
*dev
;
1153 struct video_device
*vfd
;
1156 dev
= kzalloc(sizeof(*dev
), GFP_KERNEL
);
1160 snprintf(dev
->v4l2_dev
.name
, sizeof(dev
->v4l2_dev
.name
),
1161 "%s-%03d", VIVI_MODULE_NAME
, inst
);
1162 ret
= v4l2_device_register(NULL
, &dev
->v4l2_dev
);
1166 dev
->fmt
= &formats
[0];
1170 dev
->brightness
= 127;
1172 dev
->saturation
= 127;
1175 videobuf_queue_vmalloc_init(&dev
->vb_vidq
, &vivi_video_qops
,
1176 NULL
, &dev
->slock
, V4L2_BUF_TYPE_VIDEO_CAPTURE
,
1177 V4L2_FIELD_INTERLACED
,
1178 sizeof(struct vivi_buffer
), dev
);
1180 /* init video dma queues */
1181 INIT_LIST_HEAD(&dev
->vidq
.active
);
1182 init_waitqueue_head(&dev
->vidq
.wq
);
1184 /* initialize locks */
1185 spin_lock_init(&dev
->slock
);
1186 mutex_init(&dev
->mutex
);
1189 vfd
= video_device_alloc();
1193 *vfd
= vivi_template
;
1195 vfd
->v4l2_dev
= &dev
->v4l2_dev
;
1197 ret
= video_register_device(vfd
, VFL_TYPE_GRABBER
, video_nr
);
1201 video_set_drvdata(vfd
, dev
);
1203 /* Now that everything is fine, let's add it to device list */
1204 list_add_tail(&dev
->vivi_devlist
, &vivi_devlist
);
1210 v4l2_info(&dev
->v4l2_dev
, "V4L2 device registered as %s\n",
1211 video_device_node_name(vfd
));
1215 video_device_release(vfd
);
1217 v4l2_device_unregister(&dev
->v4l2_dev
);
1223 /* This routine allocates from 1 to n_devs virtual drivers.
1225 The real maximum number of virtual drivers will depend on how many drivers
1226 will succeed. This is limited to the maximum number of devices that
1227 videodev supports, which is equal to VIDEO_NUM_DEVICES.
1229 static int __init
vivi_init(void)
1231 const struct font_desc
*font
= find_font("VGA8x16");
1235 printk(KERN_ERR
"vivi: could not find font\n");
1238 font8x16
= font
->data
;
1243 for (i
= 0; i
< n_devs
; i
++) {
1244 ret
= vivi_create_instance(i
);
1246 /* If some instantiations succeeded, keep driver */
1254 printk(KERN_ERR
"vivi: error %d while loading driver\n", ret
);
1258 printk(KERN_INFO
"Video Technology Magazine Virtual Video "
1259 "Capture Board ver %u.%u.%u successfully loaded.\n",
1260 (VIVI_VERSION
>> 16) & 0xFF, (VIVI_VERSION
>> 8) & 0xFF,
1261 VIVI_VERSION
& 0xFF);
1263 /* n_devs will reflect the actual number of allocated devices */
1269 static void __exit
vivi_exit(void)
1274 module_init(vivi_init
);
1275 module_exit(vivi_exit
);