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 * Conversion to videobuf2 by Pawel Osciak & Marek Szyprowski
11 * Copyright (c) 2010 Samsung Electronics
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the BSD Licence, GNU General Public License
15 * as published by the Free Software Foundation; either version 2 of the
16 * License, or (at your option) any later version
18 #include <linux/module.h>
19 #include <linux/errno.h>
20 #include <linux/kernel.h>
21 #include <linux/init.h>
22 #include <linux/sched.h>
23 #include <linux/slab.h>
24 #include <linux/font.h>
25 #include <linux/version.h>
26 #include <linux/mutex.h>
27 #include <linux/videodev2.h>
28 #include <linux/kthread.h>
29 #include <linux/freezer.h>
30 #include <media/videobuf2-vmalloc.h>
31 #include <media/v4l2-device.h>
32 #include <media/v4l2-ioctl.h>
33 #include <media/v4l2-common.h>
35 #define VIVI_MODULE_NAME "vivi"
37 /* Wake up at about 30 fps */
38 #define WAKE_NUMERATOR 30
39 #define WAKE_DENOMINATOR 1001
40 #define BUFFER_TIMEOUT msecs_to_jiffies(500) /* 0.5 seconds */
42 #define MAX_WIDTH 1920
43 #define MAX_HEIGHT 1200
45 #define VIVI_MAJOR_VERSION 0
46 #define VIVI_MINOR_VERSION 8
47 #define VIVI_RELEASE 0
48 #define VIVI_VERSION \
49 KERNEL_VERSION(VIVI_MAJOR_VERSION, VIVI_MINOR_VERSION, VIVI_RELEASE)
51 MODULE_DESCRIPTION("Video Technology Magazine Virtual Video Capture Board");
52 MODULE_AUTHOR("Mauro Carvalho Chehab, Ted Walther and John Sokol");
53 MODULE_LICENSE("Dual BSD/GPL");
55 static unsigned video_nr
= -1;
56 module_param(video_nr
, uint
, 0644);
57 MODULE_PARM_DESC(video_nr
, "videoX start number, -1 is autodetect");
59 static unsigned n_devs
= 1;
60 module_param(n_devs
, uint
, 0644);
61 MODULE_PARM_DESC(n_devs
, "number of video devices to create");
63 static unsigned debug
;
64 module_param(debug
, uint
, 0644);
65 MODULE_PARM_DESC(debug
, "activates debug info");
67 static unsigned int vid_limit
= 16;
68 module_param(vid_limit
, uint
, 0644);
69 MODULE_PARM_DESC(vid_limit
, "capture memory limit in megabytes");
71 /* Global font descriptor */
72 static const u8
*font8x16
;
74 #define dprintk(dev, level, fmt, arg...) \
75 v4l2_dbg(level, debug, &dev->v4l2_dev, fmt, ## arg)
77 /* ------------------------------------------------------------------
79 ------------------------------------------------------------------*/
83 u32 fourcc
; /* v4l2 format id */
87 static struct vivi_fmt formats
[] = {
89 .name
= "4:2:2, packed, YUYV",
90 .fourcc
= V4L2_PIX_FMT_YUYV
,
94 .name
= "4:2:2, packed, UYVY",
95 .fourcc
= V4L2_PIX_FMT_UYVY
,
99 .name
= "RGB565 (LE)",
100 .fourcc
= V4L2_PIX_FMT_RGB565
, /* gggbbbbb rrrrrggg */
104 .name
= "RGB565 (BE)",
105 .fourcc
= V4L2_PIX_FMT_RGB565X
, /* rrrrrggg gggbbbbb */
109 .name
= "RGB555 (LE)",
110 .fourcc
= V4L2_PIX_FMT_RGB555
, /* gggbbbbb arrrrrgg */
114 .name
= "RGB555 (BE)",
115 .fourcc
= V4L2_PIX_FMT_RGB555X
, /* arrrrrgg gggbbbbb */
120 static struct vivi_fmt
*get_format(struct v4l2_format
*f
)
122 struct vivi_fmt
*fmt
;
125 for (k
= 0; k
< ARRAY_SIZE(formats
); k
++) {
127 if (fmt
->fourcc
== f
->fmt
.pix
.pixelformat
)
131 if (k
== ARRAY_SIZE(formats
))
137 /* buffer for one video frame */
139 /* common v4l buffer stuff -- must be first */
140 struct vb2_buffer vb
;
141 struct list_head list
;
142 struct vivi_fmt
*fmt
;
145 struct vivi_dmaqueue
{
146 struct list_head active
;
148 /* thread for generating video stream*/
149 struct task_struct
*kthread
;
150 wait_queue_head_t wq
;
151 /* Counters to control fps rate */
156 static LIST_HEAD(vivi_devlist
);
159 struct list_head vivi_devlist
;
160 struct v4l2_device v4l2_dev
;
172 /* various device info */
173 struct video_device
*vfd
;
175 struct vivi_dmaqueue vidq
;
177 /* Several counters */
179 unsigned long jiffies
;
181 int mv_count
; /* Controls bars movement */
187 struct vivi_fmt
*fmt
;
188 unsigned int width
, height
;
189 struct vb2_queue vb_vidq
;
190 enum v4l2_field field
;
191 unsigned int field_count
;
193 unsigned int open_count
;
195 u8 line
[MAX_WIDTH
* 4];
198 /* ------------------------------------------------------------------
199 DMA and thread functions
200 ------------------------------------------------------------------*/
202 /* Bars and Colors should match positions */
217 #define COLOR_WHITE {204, 204, 204}
218 #define COLOR_AMBER {208, 208, 0}
219 #define COLOR_CYAN { 0, 206, 206}
220 #define COLOR_GREEN { 0, 239, 0}
221 #define COLOR_MAGENTA {239, 0, 239}
222 #define COLOR_RED {205, 0, 0}
223 #define COLOR_BLUE { 0, 0, 255}
224 #define COLOR_BLACK { 0, 0, 0}
230 /* Maximum number of bars are 10 - otherwise, the input print code
231 should be modified */
232 static struct bar_std bars
[] = {
233 { /* Standard ITU-R color bar sequence */
234 { COLOR_WHITE
, COLOR_AMBER
, COLOR_CYAN
, COLOR_GREEN
,
235 COLOR_MAGENTA
, COLOR_RED
, COLOR_BLUE
, COLOR_BLACK
, COLOR_BLACK
}
237 { COLOR_WHITE
, COLOR_AMBER
, COLOR_BLACK
, COLOR_WHITE
,
238 COLOR_AMBER
, COLOR_BLACK
, COLOR_WHITE
, COLOR_AMBER
, COLOR_BLACK
}
240 { COLOR_WHITE
, COLOR_CYAN
, COLOR_BLACK
, COLOR_WHITE
,
241 COLOR_CYAN
, COLOR_BLACK
, COLOR_WHITE
, COLOR_CYAN
, COLOR_BLACK
}
243 { COLOR_WHITE
, COLOR_GREEN
, COLOR_BLACK
, COLOR_WHITE
,
244 COLOR_GREEN
, COLOR_BLACK
, COLOR_WHITE
, COLOR_GREEN
, COLOR_BLACK
}
248 #define NUM_INPUTS ARRAY_SIZE(bars)
250 #define TO_Y(r, g, b) \
251 (((16829 * r + 33039 * g + 6416 * b + 32768) >> 16) + 16)
252 /* RGB to V(Cr) Color transform */
253 #define TO_V(r, g, b) \
254 (((28784 * r - 24103 * g - 4681 * b + 32768) >> 16) + 128)
255 /* RGB to U(Cb) Color transform */
256 #define TO_U(r, g, b) \
257 (((-9714 * r - 19070 * g + 28784 * b + 32768) >> 16) + 128)
259 /* precalculate color bar values to speed up rendering */
260 static void precalculate_bars(struct vivi_dev
*dev
)
265 for (k
= 0; k
< 9; k
++) {
266 r
= bars
[dev
->input
].bar
[k
][0];
267 g
= bars
[dev
->input
].bar
[k
][1];
268 b
= bars
[dev
->input
].bar
[k
][2];
271 switch (dev
->fmt
->fourcc
) {
272 case V4L2_PIX_FMT_YUYV
:
273 case V4L2_PIX_FMT_UYVY
:
276 case V4L2_PIX_FMT_RGB565
:
277 case V4L2_PIX_FMT_RGB565X
:
282 case V4L2_PIX_FMT_RGB555
:
283 case V4L2_PIX_FMT_RGB555X
:
291 dev
->bars
[k
][0] = TO_Y(r
, g
, b
); /* Luma */
292 dev
->bars
[k
][1] = TO_U(r
, g
, b
); /* Cb */
293 dev
->bars
[k
][2] = TO_V(r
, g
, b
); /* Cr */
302 #define TSTAMP_MIN_Y 24
303 #define TSTAMP_MAX_Y (TSTAMP_MIN_Y + 15)
304 #define TSTAMP_INPUT_X 10
305 #define TSTAMP_MIN_X (54 + TSTAMP_INPUT_X)
307 static void gen_twopix(struct vivi_dev
*dev
, u8
*buf
, int colorpos
)
313 r_y
= dev
->bars
[colorpos
][0]; /* R or precalculated Y */
314 g_u
= dev
->bars
[colorpos
][1]; /* G or precalculated U */
315 b_v
= dev
->bars
[colorpos
][2]; /* B or precalculated V */
317 for (color
= 0; color
< 4; color
++) {
320 switch (dev
->fmt
->fourcc
) {
321 case V4L2_PIX_FMT_YUYV
:
335 case V4L2_PIX_FMT_UYVY
:
349 case V4L2_PIX_FMT_RGB565
:
353 *p
= (g_u
<< 5) | b_v
;
357 *p
= (r_y
<< 3) | (g_u
>> 3);
361 case V4L2_PIX_FMT_RGB565X
:
365 *p
= (r_y
<< 3) | (g_u
>> 3);
369 *p
= (g_u
<< 5) | b_v
;
373 case V4L2_PIX_FMT_RGB555
:
377 *p
= (g_u
<< 5) | b_v
;
381 *p
= (r_y
<< 2) | (g_u
>> 3);
385 case V4L2_PIX_FMT_RGB555X
:
389 *p
= (r_y
<< 2) | (g_u
>> 3);
393 *p
= (g_u
<< 5) | b_v
;
401 static void precalculate_line(struct vivi_dev
*dev
)
405 for (w
= 0; w
< dev
->width
* 2; w
+= 2) {
406 int colorpos
= (w
/ (dev
->width
/ 8) % 8);
408 gen_twopix(dev
, dev
->line
+ w
* 2, colorpos
);
412 static void gen_text(struct vivi_dev
*dev
, char *basep
,
413 int y
, int x
, char *text
)
417 /* Checks if it is possible to show string */
418 if (y
+ 16 >= dev
->height
|| x
+ strlen(text
) * 8 >= dev
->width
)
421 /* Print stream time */
422 for (line
= y
; line
< y
+ 16; line
++) {
424 char *pos
= basep
+ line
* dev
->width
* 2 + x
* 2;
427 for (s
= text
; *s
; s
++) {
428 u8 chr
= font8x16
[*s
* 16 + line
- y
];
431 for (i
= 0; i
< 7; i
++, j
++) {
432 /* Draw white font on black background */
433 if (chr
& (1 << (7 - i
)))
434 gen_twopix(dev
, pos
+ j
* 2, WHITE
);
436 gen_twopix(dev
, pos
+ j
* 2, TEXT_BLACK
);
442 static void vivi_fillbuff(struct vivi_dev
*dev
, struct vivi_buffer
*buf
)
444 int wmax
= dev
->width
;
445 int hmax
= dev
->height
;
447 void *vbuf
= vb2_plane_vaddr(&buf
->vb
, 0);
455 for (h
= 0; h
< hmax
; h
++)
456 memcpy(vbuf
+ h
* wmax
* 2, dev
->line
+ (dev
->mv_count
% wmax
) * 2, wmax
* 2);
458 /* Updates stream time */
460 dev
->ms
+= jiffies_to_msecs(jiffies
- dev
->jiffies
);
461 dev
->jiffies
= jiffies
;
463 snprintf(str
, sizeof(str
), " %02d:%02d:%02d:%03d ",
464 (ms
/ (60 * 60 * 1000)) % 24,
465 (ms
/ (60 * 1000)) % 60,
468 gen_text(dev
, vbuf
, line
++ * 16, 16, str
);
469 snprintf(str
, sizeof(str
), " %dx%d, input %d ",
470 dev
->width
, dev
->height
, dev
->input
);
471 gen_text(dev
, vbuf
, line
++ * 16, 16, str
);
473 snprintf(str
, sizeof(str
), " brightness %3d, contrast %3d, saturation %3d, hue %d ",
478 gen_text(dev
, vbuf
, line
++ * 16, 16, str
);
479 snprintf(str
, sizeof(str
), " volume %3d ", dev
->volume
);
480 gen_text(dev
, vbuf
, line
++ * 16, 16, str
);
484 buf
->vb
.v4l2_buf
.field
= dev
->field
;
486 buf
->vb
.v4l2_buf
.sequence
= dev
->field_count
>> 1;
487 do_gettimeofday(&ts
);
488 buf
->vb
.v4l2_buf
.timestamp
= ts
;
491 static void vivi_thread_tick(struct vivi_dev
*dev
)
493 struct vivi_dmaqueue
*dma_q
= &dev
->vidq
;
494 struct vivi_buffer
*buf
;
495 unsigned long flags
= 0;
497 dprintk(dev
, 1, "Thread tick\n");
499 spin_lock_irqsave(&dev
->slock
, flags
);
500 if (list_empty(&dma_q
->active
)) {
501 dprintk(dev
, 1, "No active queue to serve\n");
505 buf
= list_entry(dma_q
->active
.next
, struct vivi_buffer
, list
);
506 list_del(&buf
->list
);
508 do_gettimeofday(&buf
->vb
.v4l2_buf
.timestamp
);
511 vivi_fillbuff(dev
, buf
);
512 dprintk(dev
, 1, "filled buffer %p\n", buf
);
514 vb2_buffer_done(&buf
->vb
, VB2_BUF_STATE_DONE
);
515 dprintk(dev
, 2, "[%p/%d] done\n", buf
, buf
->vb
.v4l2_buf
.index
);
517 spin_unlock_irqrestore(&dev
->slock
, flags
);
520 #define frames_to_ms(frames) \
521 ((frames * WAKE_NUMERATOR * 1000) / WAKE_DENOMINATOR)
523 static void vivi_sleep(struct vivi_dev
*dev
)
525 struct vivi_dmaqueue
*dma_q
= &dev
->vidq
;
527 DECLARE_WAITQUEUE(wait
, current
);
529 dprintk(dev
, 1, "%s dma_q=0x%08lx\n", __func__
,
530 (unsigned long)dma_q
);
532 add_wait_queue(&dma_q
->wq
, &wait
);
533 if (kthread_should_stop())
536 /* Calculate time to wake up */
537 timeout
= msecs_to_jiffies(frames_to_ms(1));
539 vivi_thread_tick(dev
);
541 schedule_timeout_interruptible(timeout
);
544 remove_wait_queue(&dma_q
->wq
, &wait
);
548 static int vivi_thread(void *data
)
550 struct vivi_dev
*dev
= data
;
552 dprintk(dev
, 1, "thread started\n");
559 if (kthread_should_stop())
562 dprintk(dev
, 1, "thread: exit\n");
566 static int vivi_start_generating(struct vivi_dev
*dev
)
568 struct vivi_dmaqueue
*dma_q
= &dev
->vidq
;
570 dprintk(dev
, 1, "%s\n", __func__
);
572 /* Resets frame counters */
575 dev
->jiffies
= jiffies
;
578 dma_q
->ini_jiffies
= jiffies
;
579 dma_q
->kthread
= kthread_run(vivi_thread
, dev
, dev
->v4l2_dev
.name
);
581 if (IS_ERR(dma_q
->kthread
)) {
582 v4l2_err(&dev
->v4l2_dev
, "kernel_thread() failed\n");
583 return PTR_ERR(dma_q
->kthread
);
586 wake_up_interruptible(&dma_q
->wq
);
588 dprintk(dev
, 1, "returning from %s\n", __func__
);
592 static void vivi_stop_generating(struct vivi_dev
*dev
)
594 struct vivi_dmaqueue
*dma_q
= &dev
->vidq
;
596 dprintk(dev
, 1, "%s\n", __func__
);
598 /* shutdown control thread */
599 if (dma_q
->kthread
) {
600 kthread_stop(dma_q
->kthread
);
601 dma_q
->kthread
= NULL
;
605 * Typical driver might need to wait here until dma engine stops.
606 * In this case we can abort imiedetly, so it's just a noop.
609 /* Release all active buffers */
610 while (!list_empty(&dma_q
->active
)) {
611 struct vivi_buffer
*buf
;
612 buf
= list_entry(dma_q
->active
.next
, struct vivi_buffer
, list
);
613 list_del(&buf
->list
);
614 vb2_buffer_done(&buf
->vb
, VB2_BUF_STATE_ERROR
);
615 dprintk(dev
, 2, "[%p/%d] done\n", buf
, buf
->vb
.v4l2_buf
.index
);
618 /* ------------------------------------------------------------------
620 ------------------------------------------------------------------*/
621 static int queue_setup(struct vb2_queue
*vq
, unsigned int *nbuffers
,
622 unsigned int *nplanes
, unsigned long sizes
[],
625 struct vivi_dev
*dev
= vb2_get_drv_priv(vq
);
628 size
= dev
->width
* dev
->height
* 2;
633 while (size
* *nbuffers
> vid_limit
* 1024 * 1024)
641 * videobuf2-vmalloc allocator is context-less so no need to set
645 dprintk(dev
, 1, "%s, count=%d, size=%ld\n", __func__
,
651 static int buffer_init(struct vb2_buffer
*vb
)
653 struct vivi_dev
*dev
= vb2_get_drv_priv(vb
->vb2_queue
);
655 BUG_ON(NULL
== dev
->fmt
);
658 * This callback is called once per buffer, after its allocation.
660 * Vivi does not allow changing format during streaming, but it is
661 * possible to do so when streaming is paused (i.e. in streamoff state).
662 * Buffers however are not freed when going into streamoff and so
663 * buffer size verification has to be done in buffer_prepare, on each
665 * It would be best to move verification code here to buf_init and
672 static int buffer_prepare(struct vb2_buffer
*vb
)
674 struct vivi_dev
*dev
= vb2_get_drv_priv(vb
->vb2_queue
);
675 struct vivi_buffer
*buf
= container_of(vb
, struct vivi_buffer
, vb
);
678 dprintk(dev
, 1, "%s, field=%d\n", __func__
, vb
->v4l2_buf
.field
);
680 BUG_ON(NULL
== dev
->fmt
);
683 * Theses properties only change when queue is idle, see s_fmt.
684 * The below checks should not be performed here, on each
685 * buffer_prepare (i.e. on each qbuf). Most of the code in this function
686 * should thus be moved to buffer_init and s_fmt.
688 if (dev
->width
< 48 || dev
->width
> MAX_WIDTH
||
689 dev
->height
< 32 || dev
->height
> MAX_HEIGHT
)
692 size
= dev
->width
* dev
->height
* 2;
693 if (vb2_plane_size(vb
, 0) < size
) {
694 dprintk(dev
, 1, "%s data will not fit into plane (%lu < %lu)\n",
695 __func__
, vb2_plane_size(vb
, 0), size
);
699 vb2_set_plane_payload(&buf
->vb
, 0, size
);
703 precalculate_bars(dev
);
704 precalculate_line(dev
);
709 static int buffer_finish(struct vb2_buffer
*vb
)
711 struct vivi_dev
*dev
= vb2_get_drv_priv(vb
->vb2_queue
);
712 dprintk(dev
, 1, "%s\n", __func__
);
716 static void buffer_cleanup(struct vb2_buffer
*vb
)
718 struct vivi_dev
*dev
= vb2_get_drv_priv(vb
->vb2_queue
);
719 dprintk(dev
, 1, "%s\n", __func__
);
723 static void buffer_queue(struct vb2_buffer
*vb
)
725 struct vivi_dev
*dev
= vb2_get_drv_priv(vb
->vb2_queue
);
726 struct vivi_buffer
*buf
= container_of(vb
, struct vivi_buffer
, vb
);
727 struct vivi_dmaqueue
*vidq
= &dev
->vidq
;
728 unsigned long flags
= 0;
730 dprintk(dev
, 1, "%s\n", __func__
);
732 spin_lock_irqsave(&dev
->slock
, flags
);
733 list_add_tail(&buf
->list
, &vidq
->active
);
734 spin_unlock_irqrestore(&dev
->slock
, flags
);
737 static int start_streaming(struct vb2_queue
*vq
)
739 struct vivi_dev
*dev
= vb2_get_drv_priv(vq
);
740 dprintk(dev
, 1, "%s\n", __func__
);
741 return vivi_start_generating(dev
);
744 /* abort streaming and wait for last buffer */
745 static int stop_streaming(struct vb2_queue
*vq
)
747 struct vivi_dev
*dev
= vb2_get_drv_priv(vq
);
748 dprintk(dev
, 1, "%s\n", __func__
);
749 vivi_stop_generating(dev
);
753 static void vivi_lock(struct vb2_queue
*vq
)
755 struct vivi_dev
*dev
= vb2_get_drv_priv(vq
);
756 mutex_lock(&dev
->mutex
);
759 static void vivi_unlock(struct vb2_queue
*vq
)
761 struct vivi_dev
*dev
= vb2_get_drv_priv(vq
);
762 mutex_unlock(&dev
->mutex
);
766 static struct vb2_ops vivi_video_qops
= {
767 .queue_setup
= queue_setup
,
768 .buf_init
= buffer_init
,
769 .buf_prepare
= buffer_prepare
,
770 .buf_finish
= buffer_finish
,
771 .buf_cleanup
= buffer_cleanup
,
772 .buf_queue
= buffer_queue
,
773 .start_streaming
= start_streaming
,
774 .stop_streaming
= stop_streaming
,
775 .wait_prepare
= vivi_unlock
,
776 .wait_finish
= vivi_lock
,
779 /* ------------------------------------------------------------------
780 IOCTL vidioc handling
781 ------------------------------------------------------------------*/
782 static int vidioc_querycap(struct file
*file
, void *priv
,
783 struct v4l2_capability
*cap
)
785 struct vivi_dev
*dev
= video_drvdata(file
);
787 strcpy(cap
->driver
, "vivi");
788 strcpy(cap
->card
, "vivi");
789 strlcpy(cap
->bus_info
, dev
->v4l2_dev
.name
, sizeof(cap
->bus_info
));
790 cap
->version
= VIVI_VERSION
;
791 cap
->capabilities
= V4L2_CAP_VIDEO_CAPTURE
| V4L2_CAP_STREAMING
| \
796 static int vidioc_enum_fmt_vid_cap(struct file
*file
, void *priv
,
797 struct v4l2_fmtdesc
*f
)
799 struct vivi_fmt
*fmt
;
801 if (f
->index
>= ARRAY_SIZE(formats
))
804 fmt
= &formats
[f
->index
];
806 strlcpy(f
->description
, fmt
->name
, sizeof(f
->description
));
807 f
->pixelformat
= fmt
->fourcc
;
811 static int vidioc_g_fmt_vid_cap(struct file
*file
, void *priv
,
812 struct v4l2_format
*f
)
814 struct vivi_dev
*dev
= video_drvdata(file
);
816 f
->fmt
.pix
.width
= dev
->width
;
817 f
->fmt
.pix
.height
= dev
->height
;
818 f
->fmt
.pix
.field
= dev
->field
;
819 f
->fmt
.pix
.pixelformat
= dev
->fmt
->fourcc
;
820 f
->fmt
.pix
.bytesperline
=
821 (f
->fmt
.pix
.width
* dev
->fmt
->depth
) >> 3;
822 f
->fmt
.pix
.sizeimage
=
823 f
->fmt
.pix
.height
* f
->fmt
.pix
.bytesperline
;
827 static int vidioc_try_fmt_vid_cap(struct file
*file
, void *priv
,
828 struct v4l2_format
*f
)
830 struct vivi_dev
*dev
= video_drvdata(file
);
831 struct vivi_fmt
*fmt
;
832 enum v4l2_field field
;
836 dprintk(dev
, 1, "Fourcc format (0x%08x) invalid.\n",
837 f
->fmt
.pix
.pixelformat
);
841 field
= f
->fmt
.pix
.field
;
843 if (field
== V4L2_FIELD_ANY
) {
844 field
= V4L2_FIELD_INTERLACED
;
845 } else if (V4L2_FIELD_INTERLACED
!= field
) {
846 dprintk(dev
, 1, "Field type invalid.\n");
850 f
->fmt
.pix
.field
= field
;
851 v4l_bound_align_image(&f
->fmt
.pix
.width
, 48, MAX_WIDTH
, 2,
852 &f
->fmt
.pix
.height
, 32, MAX_HEIGHT
, 0, 0);
853 f
->fmt
.pix
.bytesperline
=
854 (f
->fmt
.pix
.width
* fmt
->depth
) >> 3;
855 f
->fmt
.pix
.sizeimage
=
856 f
->fmt
.pix
.height
* f
->fmt
.pix
.bytesperline
;
860 static int vidioc_s_fmt_vid_cap(struct file
*file
, void *priv
,
861 struct v4l2_format
*f
)
863 struct vivi_dev
*dev
= video_drvdata(file
);
864 struct vb2_queue
*q
= &dev
->vb_vidq
;
866 int ret
= vidioc_try_fmt_vid_cap(file
, priv
, f
);
870 if (vb2_is_streaming(q
)) {
871 dprintk(dev
, 1, "%s device busy\n", __func__
);
875 dev
->fmt
= get_format(f
);
876 dev
->width
= f
->fmt
.pix
.width
;
877 dev
->height
= f
->fmt
.pix
.height
;
878 dev
->field
= f
->fmt
.pix
.field
;
883 static int vidioc_reqbufs(struct file
*file
, void *priv
,
884 struct v4l2_requestbuffers
*p
)
886 struct vivi_dev
*dev
= video_drvdata(file
);
887 return vb2_reqbufs(&dev
->vb_vidq
, p
);
890 static int vidioc_querybuf(struct file
*file
, void *priv
, struct v4l2_buffer
*p
)
892 struct vivi_dev
*dev
= video_drvdata(file
);
893 return vb2_querybuf(&dev
->vb_vidq
, p
);
896 static int vidioc_qbuf(struct file
*file
, void *priv
, struct v4l2_buffer
*p
)
898 struct vivi_dev
*dev
= video_drvdata(file
);
899 return vb2_qbuf(&dev
->vb_vidq
, p
);
902 static int vidioc_dqbuf(struct file
*file
, void *priv
, struct v4l2_buffer
*p
)
904 struct vivi_dev
*dev
= video_drvdata(file
);
905 return vb2_dqbuf(&dev
->vb_vidq
, p
, file
->f_flags
& O_NONBLOCK
);
908 static int vidioc_streamon(struct file
*file
, void *priv
, enum v4l2_buf_type i
)
910 struct vivi_dev
*dev
= video_drvdata(file
);
911 return vb2_streamon(&dev
->vb_vidq
, i
);
914 static int vidioc_streamoff(struct file
*file
, void *priv
, enum v4l2_buf_type i
)
916 struct vivi_dev
*dev
= video_drvdata(file
);
917 return vb2_streamoff(&dev
->vb_vidq
, i
);
920 static int vidioc_s_std(struct file
*file
, void *priv
, v4l2_std_id
*i
)
925 /* only one input in this sample driver */
926 static int vidioc_enum_input(struct file
*file
, void *priv
,
927 struct v4l2_input
*inp
)
929 if (inp
->index
>= NUM_INPUTS
)
932 inp
->type
= V4L2_INPUT_TYPE_CAMERA
;
933 inp
->std
= V4L2_STD_525_60
;
934 sprintf(inp
->name
, "Camera %u", inp
->index
);
938 static int vidioc_g_input(struct file
*file
, void *priv
, unsigned int *i
)
940 struct vivi_dev
*dev
= video_drvdata(file
);
946 static int vidioc_s_input(struct file
*file
, void *priv
, unsigned int i
)
948 struct vivi_dev
*dev
= video_drvdata(file
);
954 precalculate_bars(dev
);
955 precalculate_line(dev
);
959 /* --- controls ---------------------------------------------- */
960 static int vidioc_queryctrl(struct file
*file
, void *priv
,
961 struct v4l2_queryctrl
*qc
)
964 case V4L2_CID_AUDIO_VOLUME
:
965 return v4l2_ctrl_query_fill(qc
, 0, 255, 1, 200);
966 case V4L2_CID_BRIGHTNESS
:
967 return v4l2_ctrl_query_fill(qc
, 0, 255, 1, 127);
968 case V4L2_CID_CONTRAST
:
969 return v4l2_ctrl_query_fill(qc
, 0, 255, 1, 16);
970 case V4L2_CID_SATURATION
:
971 return v4l2_ctrl_query_fill(qc
, 0, 255, 1, 127);
973 return v4l2_ctrl_query_fill(qc
, -128, 127, 1, 0);
978 static int vidioc_g_ctrl(struct file
*file
, void *priv
,
979 struct v4l2_control
*ctrl
)
981 struct vivi_dev
*dev
= video_drvdata(file
);
984 case V4L2_CID_AUDIO_VOLUME
:
985 ctrl
->value
= dev
->volume
;
987 case V4L2_CID_BRIGHTNESS
:
988 ctrl
->value
= dev
->brightness
;
990 case V4L2_CID_CONTRAST
:
991 ctrl
->value
= dev
->contrast
;
993 case V4L2_CID_SATURATION
:
994 ctrl
->value
= dev
->saturation
;
997 ctrl
->value
= dev
->hue
;
1003 static int vidioc_s_ctrl(struct file
*file
, void *priv
,
1004 struct v4l2_control
*ctrl
)
1006 struct vivi_dev
*dev
= video_drvdata(file
);
1007 struct v4l2_queryctrl qc
;
1011 err
= vidioc_queryctrl(file
, priv
, &qc
);
1014 if (ctrl
->value
< qc
.minimum
|| ctrl
->value
> qc
.maximum
)
1017 case V4L2_CID_AUDIO_VOLUME
:
1018 dev
->volume
= ctrl
->value
;
1020 case V4L2_CID_BRIGHTNESS
:
1021 dev
->brightness
= ctrl
->value
;
1023 case V4L2_CID_CONTRAST
:
1024 dev
->contrast
= ctrl
->value
;
1026 case V4L2_CID_SATURATION
:
1027 dev
->saturation
= ctrl
->value
;
1030 dev
->hue
= ctrl
->value
;
1036 /* ------------------------------------------------------------------
1037 File operations for the device
1038 ------------------------------------------------------------------*/
1040 static int vivi_open(struct file
*file
)
1042 struct vivi_dev
*dev
= video_drvdata(file
);
1044 dprintk(dev
, 1, "%s, %p\n", __func__
, file
);
1050 vivi_read(struct file
*file
, char __user
*data
, size_t count
, loff_t
*ppos
)
1052 struct vivi_dev
*dev
= video_drvdata(file
);
1054 dprintk(dev
, 1, "read called\n");
1055 return vb2_read(&dev
->vb_vidq
, data
, count
, ppos
,
1056 file
->f_flags
& O_NONBLOCK
);
1060 vivi_poll(struct file
*file
, struct poll_table_struct
*wait
)
1062 struct vivi_dev
*dev
= video_drvdata(file
);
1063 struct vb2_queue
*q
= &dev
->vb_vidq
;
1065 dprintk(dev
, 1, "%s\n", __func__
);
1066 return vb2_poll(q
, file
, wait
);
1069 static int vivi_close(struct file
*file
)
1071 struct video_device
*vdev
= video_devdata(file
);
1072 struct vivi_dev
*dev
= video_drvdata(file
);
1074 dprintk(dev
, 1, "close called (dev=%s), file %p\n",
1075 video_device_node_name(vdev
), file
);
1077 if (--dev
->open_count
== 0)
1078 vb2_queue_release(&dev
->vb_vidq
);
1082 static int vivi_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1084 struct vivi_dev
*dev
= video_drvdata(file
);
1087 dprintk(dev
, 1, "mmap called, vma=0x%08lx\n", (unsigned long)vma
);
1089 ret
= vb2_mmap(&dev
->vb_vidq
, vma
);
1090 dprintk(dev
, 1, "vma start=0x%08lx, size=%ld, ret=%d\n",
1091 (unsigned long)vma
->vm_start
,
1092 (unsigned long)vma
->vm_end
- (unsigned long)vma
->vm_start
,
1097 static const struct v4l2_file_operations vivi_fops
= {
1098 .owner
= THIS_MODULE
,
1100 .release
= vivi_close
,
1103 .unlocked_ioctl
= video_ioctl2
, /* V4L2 ioctl handler */
1107 static const struct v4l2_ioctl_ops vivi_ioctl_ops
= {
1108 .vidioc_querycap
= vidioc_querycap
,
1109 .vidioc_enum_fmt_vid_cap
= vidioc_enum_fmt_vid_cap
,
1110 .vidioc_g_fmt_vid_cap
= vidioc_g_fmt_vid_cap
,
1111 .vidioc_try_fmt_vid_cap
= vidioc_try_fmt_vid_cap
,
1112 .vidioc_s_fmt_vid_cap
= vidioc_s_fmt_vid_cap
,
1113 .vidioc_reqbufs
= vidioc_reqbufs
,
1114 .vidioc_querybuf
= vidioc_querybuf
,
1115 .vidioc_qbuf
= vidioc_qbuf
,
1116 .vidioc_dqbuf
= vidioc_dqbuf
,
1117 .vidioc_s_std
= vidioc_s_std
,
1118 .vidioc_enum_input
= vidioc_enum_input
,
1119 .vidioc_g_input
= vidioc_g_input
,
1120 .vidioc_s_input
= vidioc_s_input
,
1121 .vidioc_streamon
= vidioc_streamon
,
1122 .vidioc_streamoff
= vidioc_streamoff
,
1123 .vidioc_queryctrl
= vidioc_queryctrl
,
1124 .vidioc_g_ctrl
= vidioc_g_ctrl
,
1125 .vidioc_s_ctrl
= vidioc_s_ctrl
,
1128 static struct video_device vivi_template
= {
1131 .ioctl_ops
= &vivi_ioctl_ops
,
1132 .release
= video_device_release
,
1134 .tvnorms
= V4L2_STD_525_60
,
1135 .current_norm
= V4L2_STD_NTSC_M
,
1138 /* -----------------------------------------------------------------
1139 Initialization and module stuff
1140 ------------------------------------------------------------------*/
1142 static int vivi_release(void)
1144 struct vivi_dev
*dev
;
1145 struct list_head
*list
;
1147 while (!list_empty(&vivi_devlist
)) {
1148 list
= vivi_devlist
.next
;
1150 dev
= list_entry(list
, struct vivi_dev
, vivi_devlist
);
1152 v4l2_info(&dev
->v4l2_dev
, "unregistering %s\n",
1153 video_device_node_name(dev
->vfd
));
1154 video_unregister_device(dev
->vfd
);
1155 v4l2_device_unregister(&dev
->v4l2_dev
);
1162 static int __init
vivi_create_instance(int inst
)
1164 struct vivi_dev
*dev
;
1165 struct video_device
*vfd
;
1166 struct vb2_queue
*q
;
1169 dev
= kzalloc(sizeof(*dev
), GFP_KERNEL
);
1173 snprintf(dev
->v4l2_dev
.name
, sizeof(dev
->v4l2_dev
.name
),
1174 "%s-%03d", VIVI_MODULE_NAME
, inst
);
1175 ret
= v4l2_device_register(NULL
, &dev
->v4l2_dev
);
1179 dev
->fmt
= &formats
[0];
1183 dev
->brightness
= 127;
1185 dev
->saturation
= 127;
1188 /* initialize locks */
1189 spin_lock_init(&dev
->slock
);
1191 /* initialize queue */
1193 memset(q
, 0, sizeof(dev
->vb_vidq
));
1194 q
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1195 q
->io_modes
= VB2_MMAP
| VB2_USERPTR
| VB2_READ
;
1197 q
->buf_struct_size
= sizeof(struct vivi_buffer
);
1198 q
->ops
= &vivi_video_qops
;
1199 q
->mem_ops
= &vb2_vmalloc_memops
;
1203 mutex_init(&dev
->mutex
);
1205 /* init video dma queues */
1206 INIT_LIST_HEAD(&dev
->vidq
.active
);
1207 init_waitqueue_head(&dev
->vidq
.wq
);
1210 vfd
= video_device_alloc();
1214 *vfd
= vivi_template
;
1216 vfd
->v4l2_dev
= &dev
->v4l2_dev
;
1219 * Provide a mutex to v4l2 core. It will be used to protect
1220 * all fops and v4l2 ioctls.
1222 vfd
->lock
= &dev
->mutex
;
1224 ret
= video_register_device(vfd
, VFL_TYPE_GRABBER
, video_nr
);
1228 video_set_drvdata(vfd
, dev
);
1230 /* Now that everything is fine, let's add it to device list */
1231 list_add_tail(&dev
->vivi_devlist
, &vivi_devlist
);
1237 v4l2_info(&dev
->v4l2_dev
, "V4L2 device registered as %s\n",
1238 video_device_node_name(vfd
));
1242 video_device_release(vfd
);
1244 v4l2_device_unregister(&dev
->v4l2_dev
);
1250 /* This routine allocates from 1 to n_devs virtual drivers.
1252 The real maximum number of virtual drivers will depend on how many drivers
1253 will succeed. This is limited to the maximum number of devices that
1254 videodev supports, which is equal to VIDEO_NUM_DEVICES.
1256 static int __init
vivi_init(void)
1258 const struct font_desc
*font
= find_font("VGA8x16");
1262 printk(KERN_ERR
"vivi: could not find font\n");
1265 font8x16
= font
->data
;
1270 for (i
= 0; i
< n_devs
; i
++) {
1271 ret
= vivi_create_instance(i
);
1273 /* If some instantiations succeeded, keep driver */
1281 printk(KERN_ERR
"vivi: error %d while loading driver\n", ret
);
1285 printk(KERN_INFO
"Video Technology Magazine Virtual Video "
1286 "Capture Board ver %u.%u.%u successfully loaded.\n",
1287 (VIVI_VERSION
>> 16) & 0xFF, (VIVI_VERSION
>> 8) & 0xFF,
1288 VIVI_VERSION
& 0xFF);
1290 /* n_devs will reflect the actual number of allocated devices */
1296 static void __exit
vivi_exit(void)
1301 module_init(vivi_init
);
1302 module_exit(vivi_exit
);