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/mutex.h>
26 #include <linux/videodev2.h>
27 #include <linux/kthread.h>
28 #include <linux/freezer.h>
29 #include <media/videobuf2-vmalloc.h>
30 #include <media/v4l2-device.h>
31 #include <media/v4l2-ioctl.h>
32 #include <media/v4l2-ctrls.h>
33 #include <media/v4l2-fh.h>
34 #include <media/v4l2-common.h>
36 #define VIVI_MODULE_NAME "vivi"
38 /* Wake up at about 30 fps */
39 #define WAKE_NUMERATOR 30
40 #define WAKE_DENOMINATOR 1001
41 #define BUFFER_TIMEOUT msecs_to_jiffies(500) /* 0.5 seconds */
43 #define MAX_WIDTH 1920
44 #define MAX_HEIGHT 1200
46 #define VIVI_VERSION "0.8.1"
48 MODULE_DESCRIPTION("Video Technology Magazine Virtual Video Capture Board");
49 MODULE_AUTHOR("Mauro Carvalho Chehab, Ted Walther and John Sokol");
50 MODULE_LICENSE("Dual BSD/GPL");
51 MODULE_VERSION(VIVI_VERSION
);
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
))
135 /* buffer for one video frame */
137 /* common v4l buffer stuff -- must be first */
138 struct vb2_buffer vb
;
139 struct list_head list
;
140 struct vivi_fmt
*fmt
;
143 struct vivi_dmaqueue
{
144 struct list_head active
;
146 /* thread for generating video stream*/
147 struct task_struct
*kthread
;
148 wait_queue_head_t wq
;
149 /* Counters to control fps rate */
154 static LIST_HEAD(vivi_devlist
);
157 struct list_head vivi_devlist
;
158 struct v4l2_device v4l2_dev
;
159 struct v4l2_ctrl_handler ctrl_handler
;
162 struct v4l2_ctrl
*brightness
;
163 struct v4l2_ctrl
*contrast
;
164 struct v4l2_ctrl
*saturation
;
165 struct v4l2_ctrl
*hue
;
166 struct v4l2_ctrl
*volume
;
167 struct v4l2_ctrl
*button
;
168 struct v4l2_ctrl
*boolean
;
169 struct v4l2_ctrl
*int32
;
170 struct v4l2_ctrl
*int64
;
171 struct v4l2_ctrl
*menu
;
172 struct v4l2_ctrl
*string
;
177 /* various device info */
178 struct video_device
*vfd
;
180 struct vivi_dmaqueue vidq
;
182 /* Several counters */
184 unsigned long jiffies
;
185 unsigned button_pressed
;
187 int mv_count
; /* Controls bars movement */
193 struct vivi_fmt
*fmt
;
194 unsigned int width
, height
;
195 struct vb2_queue vb_vidq
;
196 enum v4l2_field field
;
197 unsigned int field_count
;
200 u8 line
[MAX_WIDTH
* 4];
203 /* ------------------------------------------------------------------
204 DMA and thread functions
205 ------------------------------------------------------------------*/
207 /* Bars and Colors should match positions */
222 #define COLOR_WHITE {204, 204, 204}
223 #define COLOR_AMBER {208, 208, 0}
224 #define COLOR_CYAN { 0, 206, 206}
225 #define COLOR_GREEN { 0, 239, 0}
226 #define COLOR_MAGENTA {239, 0, 239}
227 #define COLOR_RED {205, 0, 0}
228 #define COLOR_BLUE { 0, 0, 255}
229 #define COLOR_BLACK { 0, 0, 0}
235 /* Maximum number of bars are 10 - otherwise, the input print code
236 should be modified */
237 static struct bar_std bars
[] = {
238 { /* Standard ITU-R color bar sequence */
239 { COLOR_WHITE
, COLOR_AMBER
, COLOR_CYAN
, COLOR_GREEN
,
240 COLOR_MAGENTA
, COLOR_RED
, COLOR_BLUE
, COLOR_BLACK
, COLOR_BLACK
}
242 { COLOR_WHITE
, COLOR_AMBER
, COLOR_BLACK
, COLOR_WHITE
,
243 COLOR_AMBER
, COLOR_BLACK
, COLOR_WHITE
, COLOR_AMBER
, COLOR_BLACK
}
245 { COLOR_WHITE
, COLOR_CYAN
, COLOR_BLACK
, COLOR_WHITE
,
246 COLOR_CYAN
, COLOR_BLACK
, COLOR_WHITE
, COLOR_CYAN
, COLOR_BLACK
}
248 { COLOR_WHITE
, COLOR_GREEN
, COLOR_BLACK
, COLOR_WHITE
,
249 COLOR_GREEN
, COLOR_BLACK
, COLOR_WHITE
, COLOR_GREEN
, COLOR_BLACK
}
253 #define NUM_INPUTS ARRAY_SIZE(bars)
255 #define TO_Y(r, g, b) \
256 (((16829 * r + 33039 * g + 6416 * b + 32768) >> 16) + 16)
257 /* RGB to V(Cr) Color transform */
258 #define TO_V(r, g, b) \
259 (((28784 * r - 24103 * g - 4681 * b + 32768) >> 16) + 128)
260 /* RGB to U(Cb) Color transform */
261 #define TO_U(r, g, b) \
262 (((-9714 * r - 19070 * g + 28784 * b + 32768) >> 16) + 128)
264 /* precalculate color bar values to speed up rendering */
265 static void precalculate_bars(struct vivi_dev
*dev
)
270 for (k
= 0; k
< 9; k
++) {
271 r
= bars
[dev
->input
].bar
[k
][0];
272 g
= bars
[dev
->input
].bar
[k
][1];
273 b
= bars
[dev
->input
].bar
[k
][2];
276 switch (dev
->fmt
->fourcc
) {
277 case V4L2_PIX_FMT_YUYV
:
278 case V4L2_PIX_FMT_UYVY
:
281 case V4L2_PIX_FMT_RGB565
:
282 case V4L2_PIX_FMT_RGB565X
:
287 case V4L2_PIX_FMT_RGB555
:
288 case V4L2_PIX_FMT_RGB555X
:
296 dev
->bars
[k
][0] = TO_Y(r
, g
, b
); /* Luma */
297 dev
->bars
[k
][1] = TO_U(r
, g
, b
); /* Cb */
298 dev
->bars
[k
][2] = TO_V(r
, g
, b
); /* Cr */
307 #define TSTAMP_MIN_Y 24
308 #define TSTAMP_MAX_Y (TSTAMP_MIN_Y + 15)
309 #define TSTAMP_INPUT_X 10
310 #define TSTAMP_MIN_X (54 + TSTAMP_INPUT_X)
312 static void gen_twopix(struct vivi_dev
*dev
, u8
*buf
, int colorpos
)
318 r_y
= dev
->bars
[colorpos
][0]; /* R or precalculated Y */
319 g_u
= dev
->bars
[colorpos
][1]; /* G or precalculated U */
320 b_v
= dev
->bars
[colorpos
][2]; /* B or precalculated V */
322 for (color
= 0; color
< 4; color
++) {
325 switch (dev
->fmt
->fourcc
) {
326 case V4L2_PIX_FMT_YUYV
:
340 case V4L2_PIX_FMT_UYVY
:
354 case V4L2_PIX_FMT_RGB565
:
358 *p
= (g_u
<< 5) | b_v
;
362 *p
= (r_y
<< 3) | (g_u
>> 3);
366 case V4L2_PIX_FMT_RGB565X
:
370 *p
= (r_y
<< 3) | (g_u
>> 3);
374 *p
= (g_u
<< 5) | b_v
;
378 case V4L2_PIX_FMT_RGB555
:
382 *p
= (g_u
<< 5) | b_v
;
386 *p
= (r_y
<< 2) | (g_u
>> 3);
390 case V4L2_PIX_FMT_RGB555X
:
394 *p
= (r_y
<< 2) | (g_u
>> 3);
398 *p
= (g_u
<< 5) | b_v
;
406 static void precalculate_line(struct vivi_dev
*dev
)
410 for (w
= 0; w
< dev
->width
* 2; w
+= 2) {
411 int colorpos
= (w
/ (dev
->width
/ 8) % 8);
413 gen_twopix(dev
, dev
->line
+ w
* 2, colorpos
);
417 static void gen_text(struct vivi_dev
*dev
, char *basep
,
418 int y
, int x
, char *text
)
422 /* Checks if it is possible to show string */
423 if (y
+ 16 >= dev
->height
|| x
+ strlen(text
) * 8 >= dev
->width
)
426 /* Print stream time */
427 for (line
= y
; line
< y
+ 16; line
++) {
429 char *pos
= basep
+ line
* dev
->width
* 2 + x
* 2;
432 for (s
= text
; *s
; s
++) {
433 u8 chr
= font8x16
[*s
* 16 + line
- y
];
436 for (i
= 0; i
< 7; i
++, j
++) {
437 /* Draw white font on black background */
438 if (chr
& (1 << (7 - i
)))
439 gen_twopix(dev
, pos
+ j
* 2, WHITE
);
441 gen_twopix(dev
, pos
+ j
* 2, TEXT_BLACK
);
447 static void vivi_fillbuff(struct vivi_dev
*dev
, struct vivi_buffer
*buf
)
449 int wmax
= dev
->width
;
450 int hmax
= dev
->height
;
452 void *vbuf
= vb2_plane_vaddr(&buf
->vb
, 0);
460 for (h
= 0; h
< hmax
; h
++)
461 memcpy(vbuf
+ h
* wmax
* 2, dev
->line
+ (dev
->mv_count
% wmax
) * 2, wmax
* 2);
463 /* Updates stream time */
465 dev
->ms
+= jiffies_to_msecs(jiffies
- dev
->jiffies
);
466 dev
->jiffies
= jiffies
;
468 snprintf(str
, sizeof(str
), " %02d:%02d:%02d:%03d ",
469 (ms
/ (60 * 60 * 1000)) % 24,
470 (ms
/ (60 * 1000)) % 60,
473 gen_text(dev
, vbuf
, line
++ * 16, 16, str
);
474 snprintf(str
, sizeof(str
), " %dx%d, input %d ",
475 dev
->width
, dev
->height
, dev
->input
);
476 gen_text(dev
, vbuf
, line
++ * 16, 16, str
);
478 mutex_lock(&dev
->ctrl_handler
.lock
);
479 snprintf(str
, sizeof(str
), " brightness %3d, contrast %3d, saturation %3d, hue %d ",
480 dev
->brightness
->cur
.val
,
481 dev
->contrast
->cur
.val
,
482 dev
->saturation
->cur
.val
,
484 gen_text(dev
, vbuf
, line
++ * 16, 16, str
);
485 snprintf(str
, sizeof(str
), " volume %3d ", dev
->volume
->cur
.val
);
486 gen_text(dev
, vbuf
, line
++ * 16, 16, str
);
487 snprintf(str
, sizeof(str
), " int32 %d, int64 %lld ",
489 dev
->int64
->cur
.val64
);
490 gen_text(dev
, vbuf
, line
++ * 16, 16, str
);
491 snprintf(str
, sizeof(str
), " boolean %d, menu %s, string \"%s\" ",
492 dev
->boolean
->cur
.val
,
493 dev
->menu
->qmenu
[dev
->menu
->cur
.val
],
494 dev
->string
->cur
.string
);
495 mutex_unlock(&dev
->ctrl_handler
.lock
);
496 gen_text(dev
, vbuf
, line
++ * 16, 16, str
);
497 if (dev
->button_pressed
) {
498 dev
->button_pressed
--;
499 snprintf(str
, sizeof(str
), " button pressed!");
500 gen_text(dev
, vbuf
, line
++ * 16, 16, str
);
505 buf
->vb
.v4l2_buf
.field
= dev
->field
;
507 buf
->vb
.v4l2_buf
.sequence
= dev
->field_count
>> 1;
508 do_gettimeofday(&ts
);
509 buf
->vb
.v4l2_buf
.timestamp
= ts
;
512 static void vivi_thread_tick(struct vivi_dev
*dev
)
514 struct vivi_dmaqueue
*dma_q
= &dev
->vidq
;
515 struct vivi_buffer
*buf
;
516 unsigned long flags
= 0;
518 dprintk(dev
, 1, "Thread tick\n");
520 spin_lock_irqsave(&dev
->slock
, flags
);
521 if (list_empty(&dma_q
->active
)) {
522 dprintk(dev
, 1, "No active queue to serve\n");
526 buf
= list_entry(dma_q
->active
.next
, struct vivi_buffer
, list
);
527 list_del(&buf
->list
);
529 do_gettimeofday(&buf
->vb
.v4l2_buf
.timestamp
);
532 vivi_fillbuff(dev
, buf
);
533 dprintk(dev
, 1, "filled buffer %p\n", buf
);
535 vb2_buffer_done(&buf
->vb
, VB2_BUF_STATE_DONE
);
536 dprintk(dev
, 2, "[%p/%d] done\n", buf
, buf
->vb
.v4l2_buf
.index
);
538 spin_unlock_irqrestore(&dev
->slock
, flags
);
541 #define frames_to_ms(frames) \
542 ((frames * WAKE_NUMERATOR * 1000) / WAKE_DENOMINATOR)
544 static void vivi_sleep(struct vivi_dev
*dev
)
546 struct vivi_dmaqueue
*dma_q
= &dev
->vidq
;
548 DECLARE_WAITQUEUE(wait
, current
);
550 dprintk(dev
, 1, "%s dma_q=0x%08lx\n", __func__
,
551 (unsigned long)dma_q
);
553 add_wait_queue(&dma_q
->wq
, &wait
);
554 if (kthread_should_stop())
557 /* Calculate time to wake up */
558 timeout
= msecs_to_jiffies(frames_to_ms(1));
560 vivi_thread_tick(dev
);
562 schedule_timeout_interruptible(timeout
);
565 remove_wait_queue(&dma_q
->wq
, &wait
);
569 static int vivi_thread(void *data
)
571 struct vivi_dev
*dev
= data
;
573 dprintk(dev
, 1, "thread started\n");
580 if (kthread_should_stop())
583 dprintk(dev
, 1, "thread: exit\n");
587 static int vivi_start_generating(struct vivi_dev
*dev
)
589 struct vivi_dmaqueue
*dma_q
= &dev
->vidq
;
591 dprintk(dev
, 1, "%s\n", __func__
);
593 /* Resets frame counters */
596 dev
->jiffies
= jiffies
;
599 dma_q
->ini_jiffies
= jiffies
;
600 dma_q
->kthread
= kthread_run(vivi_thread
, dev
, dev
->v4l2_dev
.name
);
602 if (IS_ERR(dma_q
->kthread
)) {
603 v4l2_err(&dev
->v4l2_dev
, "kernel_thread() failed\n");
604 return PTR_ERR(dma_q
->kthread
);
607 wake_up_interruptible(&dma_q
->wq
);
609 dprintk(dev
, 1, "returning from %s\n", __func__
);
613 static void vivi_stop_generating(struct vivi_dev
*dev
)
615 struct vivi_dmaqueue
*dma_q
= &dev
->vidq
;
617 dprintk(dev
, 1, "%s\n", __func__
);
619 /* shutdown control thread */
620 if (dma_q
->kthread
) {
621 kthread_stop(dma_q
->kthread
);
622 dma_q
->kthread
= NULL
;
626 * Typical driver might need to wait here until dma engine stops.
627 * In this case we can abort imiedetly, so it's just a noop.
630 /* Release all active buffers */
631 while (!list_empty(&dma_q
->active
)) {
632 struct vivi_buffer
*buf
;
633 buf
= list_entry(dma_q
->active
.next
, struct vivi_buffer
, list
);
634 list_del(&buf
->list
);
635 vb2_buffer_done(&buf
->vb
, VB2_BUF_STATE_ERROR
);
636 dprintk(dev
, 2, "[%p/%d] done\n", buf
, buf
->vb
.v4l2_buf
.index
);
639 /* ------------------------------------------------------------------
641 ------------------------------------------------------------------*/
642 static int queue_setup(struct vb2_queue
*vq
, unsigned int *nbuffers
,
643 unsigned int *nplanes
, unsigned long sizes
[],
646 struct vivi_dev
*dev
= vb2_get_drv_priv(vq
);
649 size
= dev
->width
* dev
->height
* 2;
654 while (size
* *nbuffers
> vid_limit
* 1024 * 1024)
662 * videobuf2-vmalloc allocator is context-less so no need to set
666 dprintk(dev
, 1, "%s, count=%d, size=%ld\n", __func__
,
672 static int buffer_init(struct vb2_buffer
*vb
)
674 struct vivi_dev
*dev
= vb2_get_drv_priv(vb
->vb2_queue
);
676 BUG_ON(NULL
== dev
->fmt
);
679 * This callback is called once per buffer, after its allocation.
681 * Vivi does not allow changing format during streaming, but it is
682 * possible to do so when streaming is paused (i.e. in streamoff state).
683 * Buffers however are not freed when going into streamoff and so
684 * buffer size verification has to be done in buffer_prepare, on each
686 * It would be best to move verification code here to buf_init and
693 static int buffer_prepare(struct vb2_buffer
*vb
)
695 struct vivi_dev
*dev
= vb2_get_drv_priv(vb
->vb2_queue
);
696 struct vivi_buffer
*buf
= container_of(vb
, struct vivi_buffer
, vb
);
699 dprintk(dev
, 1, "%s, field=%d\n", __func__
, vb
->v4l2_buf
.field
);
701 BUG_ON(NULL
== dev
->fmt
);
704 * Theses properties only change when queue is idle, see s_fmt.
705 * The below checks should not be performed here, on each
706 * buffer_prepare (i.e. on each qbuf). Most of the code in this function
707 * should thus be moved to buffer_init and s_fmt.
709 if (dev
->width
< 48 || dev
->width
> MAX_WIDTH
||
710 dev
->height
< 32 || dev
->height
> MAX_HEIGHT
)
713 size
= dev
->width
* dev
->height
* 2;
714 if (vb2_plane_size(vb
, 0) < size
) {
715 dprintk(dev
, 1, "%s data will not fit into plane (%lu < %lu)\n",
716 __func__
, vb2_plane_size(vb
, 0), size
);
720 vb2_set_plane_payload(&buf
->vb
, 0, size
);
724 precalculate_bars(dev
);
725 precalculate_line(dev
);
730 static int buffer_finish(struct vb2_buffer
*vb
)
732 struct vivi_dev
*dev
= vb2_get_drv_priv(vb
->vb2_queue
);
733 dprintk(dev
, 1, "%s\n", __func__
);
737 static void buffer_cleanup(struct vb2_buffer
*vb
)
739 struct vivi_dev
*dev
= vb2_get_drv_priv(vb
->vb2_queue
);
740 dprintk(dev
, 1, "%s\n", __func__
);
744 static void buffer_queue(struct vb2_buffer
*vb
)
746 struct vivi_dev
*dev
= vb2_get_drv_priv(vb
->vb2_queue
);
747 struct vivi_buffer
*buf
= container_of(vb
, struct vivi_buffer
, vb
);
748 struct vivi_dmaqueue
*vidq
= &dev
->vidq
;
749 unsigned long flags
= 0;
751 dprintk(dev
, 1, "%s\n", __func__
);
753 spin_lock_irqsave(&dev
->slock
, flags
);
754 list_add_tail(&buf
->list
, &vidq
->active
);
755 spin_unlock_irqrestore(&dev
->slock
, flags
);
758 static int start_streaming(struct vb2_queue
*vq
)
760 struct vivi_dev
*dev
= vb2_get_drv_priv(vq
);
761 dprintk(dev
, 1, "%s\n", __func__
);
762 return vivi_start_generating(dev
);
765 /* abort streaming and wait for last buffer */
766 static int stop_streaming(struct vb2_queue
*vq
)
768 struct vivi_dev
*dev
= vb2_get_drv_priv(vq
);
769 dprintk(dev
, 1, "%s\n", __func__
);
770 vivi_stop_generating(dev
);
774 static void vivi_lock(struct vb2_queue
*vq
)
776 struct vivi_dev
*dev
= vb2_get_drv_priv(vq
);
777 mutex_lock(&dev
->mutex
);
780 static void vivi_unlock(struct vb2_queue
*vq
)
782 struct vivi_dev
*dev
= vb2_get_drv_priv(vq
);
783 mutex_unlock(&dev
->mutex
);
787 static struct vb2_ops vivi_video_qops
= {
788 .queue_setup
= queue_setup
,
789 .buf_init
= buffer_init
,
790 .buf_prepare
= buffer_prepare
,
791 .buf_finish
= buffer_finish
,
792 .buf_cleanup
= buffer_cleanup
,
793 .buf_queue
= buffer_queue
,
794 .start_streaming
= start_streaming
,
795 .stop_streaming
= stop_streaming
,
796 .wait_prepare
= vivi_unlock
,
797 .wait_finish
= vivi_lock
,
800 /* ------------------------------------------------------------------
801 IOCTL vidioc handling
802 ------------------------------------------------------------------*/
803 static int vidioc_querycap(struct file
*file
, void *priv
,
804 struct v4l2_capability
*cap
)
806 struct vivi_dev
*dev
= video_drvdata(file
);
808 strcpy(cap
->driver
, "vivi");
809 strcpy(cap
->card
, "vivi");
810 strlcpy(cap
->bus_info
, dev
->v4l2_dev
.name
, sizeof(cap
->bus_info
));
811 cap
->capabilities
= V4L2_CAP_VIDEO_CAPTURE
| V4L2_CAP_STREAMING
| \
816 static int vidioc_enum_fmt_vid_cap(struct file
*file
, void *priv
,
817 struct v4l2_fmtdesc
*f
)
819 struct vivi_fmt
*fmt
;
821 if (f
->index
>= ARRAY_SIZE(formats
))
824 fmt
= &formats
[f
->index
];
826 strlcpy(f
->description
, fmt
->name
, sizeof(f
->description
));
827 f
->pixelformat
= fmt
->fourcc
;
831 static int vidioc_g_fmt_vid_cap(struct file
*file
, void *priv
,
832 struct v4l2_format
*f
)
834 struct vivi_dev
*dev
= video_drvdata(file
);
836 f
->fmt
.pix
.width
= dev
->width
;
837 f
->fmt
.pix
.height
= dev
->height
;
838 f
->fmt
.pix
.field
= dev
->field
;
839 f
->fmt
.pix
.pixelformat
= dev
->fmt
->fourcc
;
840 f
->fmt
.pix
.bytesperline
=
841 (f
->fmt
.pix
.width
* dev
->fmt
->depth
) >> 3;
842 f
->fmt
.pix
.sizeimage
=
843 f
->fmt
.pix
.height
* f
->fmt
.pix
.bytesperline
;
847 static int vidioc_try_fmt_vid_cap(struct file
*file
, void *priv
,
848 struct v4l2_format
*f
)
850 struct vivi_dev
*dev
= video_drvdata(file
);
851 struct vivi_fmt
*fmt
;
852 enum v4l2_field field
;
856 dprintk(dev
, 1, "Fourcc format (0x%08x) invalid.\n",
857 f
->fmt
.pix
.pixelformat
);
861 field
= f
->fmt
.pix
.field
;
863 if (field
== V4L2_FIELD_ANY
) {
864 field
= V4L2_FIELD_INTERLACED
;
865 } else if (V4L2_FIELD_INTERLACED
!= field
) {
866 dprintk(dev
, 1, "Field type invalid.\n");
870 f
->fmt
.pix
.field
= field
;
871 v4l_bound_align_image(&f
->fmt
.pix
.width
, 48, MAX_WIDTH
, 2,
872 &f
->fmt
.pix
.height
, 32, MAX_HEIGHT
, 0, 0);
873 f
->fmt
.pix
.bytesperline
=
874 (f
->fmt
.pix
.width
* fmt
->depth
) >> 3;
875 f
->fmt
.pix
.sizeimage
=
876 f
->fmt
.pix
.height
* f
->fmt
.pix
.bytesperline
;
880 static int vidioc_s_fmt_vid_cap(struct file
*file
, void *priv
,
881 struct v4l2_format
*f
)
883 struct vivi_dev
*dev
= video_drvdata(file
);
884 struct vb2_queue
*q
= &dev
->vb_vidq
;
886 int ret
= vidioc_try_fmt_vid_cap(file
, priv
, f
);
890 if (vb2_is_streaming(q
)) {
891 dprintk(dev
, 1, "%s device busy\n", __func__
);
895 dev
->fmt
= get_format(f
);
896 dev
->width
= f
->fmt
.pix
.width
;
897 dev
->height
= f
->fmt
.pix
.height
;
898 dev
->field
= f
->fmt
.pix
.field
;
903 static int vidioc_reqbufs(struct file
*file
, void *priv
,
904 struct v4l2_requestbuffers
*p
)
906 struct vivi_dev
*dev
= video_drvdata(file
);
907 return vb2_reqbufs(&dev
->vb_vidq
, p
);
910 static int vidioc_querybuf(struct file
*file
, void *priv
, struct v4l2_buffer
*p
)
912 struct vivi_dev
*dev
= video_drvdata(file
);
913 return vb2_querybuf(&dev
->vb_vidq
, p
);
916 static int vidioc_qbuf(struct file
*file
, void *priv
, struct v4l2_buffer
*p
)
918 struct vivi_dev
*dev
= video_drvdata(file
);
919 return vb2_qbuf(&dev
->vb_vidq
, p
);
922 static int vidioc_dqbuf(struct file
*file
, void *priv
, struct v4l2_buffer
*p
)
924 struct vivi_dev
*dev
= video_drvdata(file
);
925 return vb2_dqbuf(&dev
->vb_vidq
, p
, file
->f_flags
& O_NONBLOCK
);
928 static int vidioc_streamon(struct file
*file
, void *priv
, enum v4l2_buf_type i
)
930 struct vivi_dev
*dev
= video_drvdata(file
);
931 return vb2_streamon(&dev
->vb_vidq
, i
);
934 static int vidioc_streamoff(struct file
*file
, void *priv
, enum v4l2_buf_type i
)
936 struct vivi_dev
*dev
= video_drvdata(file
);
937 return vb2_streamoff(&dev
->vb_vidq
, i
);
940 static int vidioc_s_std(struct file
*file
, void *priv
, v4l2_std_id
*i
)
945 /* only one input in this sample driver */
946 static int vidioc_enum_input(struct file
*file
, void *priv
,
947 struct v4l2_input
*inp
)
949 if (inp
->index
>= NUM_INPUTS
)
952 inp
->type
= V4L2_INPUT_TYPE_CAMERA
;
953 inp
->std
= V4L2_STD_525_60
;
954 sprintf(inp
->name
, "Camera %u", inp
->index
);
958 static int vidioc_g_input(struct file
*file
, void *priv
, unsigned int *i
)
960 struct vivi_dev
*dev
= video_drvdata(file
);
966 static int vidioc_s_input(struct file
*file
, void *priv
, unsigned int i
)
968 struct vivi_dev
*dev
= video_drvdata(file
);
974 precalculate_bars(dev
);
975 precalculate_line(dev
);
979 /* --- controls ---------------------------------------------- */
981 static int vivi_s_ctrl(struct v4l2_ctrl
*ctrl
)
983 struct vivi_dev
*dev
= container_of(ctrl
->handler
, struct vivi_dev
, ctrl_handler
);
985 if (ctrl
== dev
->button
)
986 dev
->button_pressed
= 30;
990 /* ------------------------------------------------------------------
991 File operations for the device
992 ------------------------------------------------------------------*/
995 vivi_read(struct file
*file
, char __user
*data
, size_t count
, loff_t
*ppos
)
997 struct vivi_dev
*dev
= video_drvdata(file
);
999 dprintk(dev
, 1, "read called\n");
1000 return vb2_read(&dev
->vb_vidq
, data
, count
, ppos
,
1001 file
->f_flags
& O_NONBLOCK
);
1005 vivi_poll(struct file
*file
, struct poll_table_struct
*wait
)
1007 struct vivi_dev
*dev
= video_drvdata(file
);
1008 struct vb2_queue
*q
= &dev
->vb_vidq
;
1010 dprintk(dev
, 1, "%s\n", __func__
);
1011 return vb2_poll(q
, file
, wait
);
1014 static int vivi_close(struct file
*file
)
1016 struct video_device
*vdev
= video_devdata(file
);
1017 struct vivi_dev
*dev
= video_drvdata(file
);
1019 dprintk(dev
, 1, "close called (dev=%s), file %p\n",
1020 video_device_node_name(vdev
), file
);
1022 if (v4l2_fh_is_singular_file(file
))
1023 vb2_queue_release(&dev
->vb_vidq
);
1024 return v4l2_fh_release(file
);
1027 static int vivi_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1029 struct vivi_dev
*dev
= video_drvdata(file
);
1032 dprintk(dev
, 1, "mmap called, vma=0x%08lx\n", (unsigned long)vma
);
1034 ret
= vb2_mmap(&dev
->vb_vidq
, vma
);
1035 dprintk(dev
, 1, "vma start=0x%08lx, size=%ld, ret=%d\n",
1036 (unsigned long)vma
->vm_start
,
1037 (unsigned long)vma
->vm_end
- (unsigned long)vma
->vm_start
,
1042 static const struct v4l2_ctrl_ops vivi_ctrl_ops
= {
1043 .s_ctrl
= vivi_s_ctrl
,
1046 #define VIVI_CID_CUSTOM_BASE (V4L2_CID_USER_BASE | 0xf000)
1048 static const struct v4l2_ctrl_config vivi_ctrl_button
= {
1049 .ops
= &vivi_ctrl_ops
,
1050 .id
= VIVI_CID_CUSTOM_BASE
+ 0,
1052 .type
= V4L2_CTRL_TYPE_BUTTON
,
1055 static const struct v4l2_ctrl_config vivi_ctrl_boolean
= {
1056 .ops
= &vivi_ctrl_ops
,
1057 .id
= VIVI_CID_CUSTOM_BASE
+ 1,
1059 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
1066 static const struct v4l2_ctrl_config vivi_ctrl_int32
= {
1067 .ops
= &vivi_ctrl_ops
,
1068 .id
= VIVI_CID_CUSTOM_BASE
+ 2,
1069 .name
= "Integer 32 Bits",
1070 .type
= V4L2_CTRL_TYPE_INTEGER
,
1076 static const struct v4l2_ctrl_config vivi_ctrl_int64
= {
1077 .ops
= &vivi_ctrl_ops
,
1078 .id
= VIVI_CID_CUSTOM_BASE
+ 3,
1079 .name
= "Integer 64 Bits",
1080 .type
= V4L2_CTRL_TYPE_INTEGER64
,
1083 static const char * const vivi_ctrl_menu_strings
[] = {
1084 "Menu Item 0 (Skipped)",
1086 "Menu Item 2 (Skipped)",
1089 "Menu Item 5 (Skipped)",
1093 static const struct v4l2_ctrl_config vivi_ctrl_menu
= {
1094 .ops
= &vivi_ctrl_ops
,
1095 .id
= VIVI_CID_CUSTOM_BASE
+ 4,
1097 .type
= V4L2_CTRL_TYPE_MENU
,
1101 .menu_skip_mask
= 0x04,
1102 .qmenu
= vivi_ctrl_menu_strings
,
1105 static const struct v4l2_ctrl_config vivi_ctrl_string
= {
1106 .ops
= &vivi_ctrl_ops
,
1107 .id
= VIVI_CID_CUSTOM_BASE
+ 5,
1109 .type
= V4L2_CTRL_TYPE_STRING
,
1115 static const struct v4l2_file_operations vivi_fops
= {
1116 .owner
= THIS_MODULE
,
1117 .open
= v4l2_fh_open
,
1118 .release
= vivi_close
,
1121 .unlocked_ioctl
= video_ioctl2
, /* V4L2 ioctl handler */
1125 static const struct v4l2_ioctl_ops vivi_ioctl_ops
= {
1126 .vidioc_querycap
= vidioc_querycap
,
1127 .vidioc_enum_fmt_vid_cap
= vidioc_enum_fmt_vid_cap
,
1128 .vidioc_g_fmt_vid_cap
= vidioc_g_fmt_vid_cap
,
1129 .vidioc_try_fmt_vid_cap
= vidioc_try_fmt_vid_cap
,
1130 .vidioc_s_fmt_vid_cap
= vidioc_s_fmt_vid_cap
,
1131 .vidioc_reqbufs
= vidioc_reqbufs
,
1132 .vidioc_querybuf
= vidioc_querybuf
,
1133 .vidioc_qbuf
= vidioc_qbuf
,
1134 .vidioc_dqbuf
= vidioc_dqbuf
,
1135 .vidioc_s_std
= vidioc_s_std
,
1136 .vidioc_enum_input
= vidioc_enum_input
,
1137 .vidioc_g_input
= vidioc_g_input
,
1138 .vidioc_s_input
= vidioc_s_input
,
1139 .vidioc_streamon
= vidioc_streamon
,
1140 .vidioc_streamoff
= vidioc_streamoff
,
1143 static struct video_device vivi_template
= {
1146 .ioctl_ops
= &vivi_ioctl_ops
,
1147 .release
= video_device_release
,
1149 .tvnorms
= V4L2_STD_525_60
,
1150 .current_norm
= V4L2_STD_NTSC_M
,
1153 /* -----------------------------------------------------------------
1154 Initialization and module stuff
1155 ------------------------------------------------------------------*/
1157 static int vivi_release(void)
1159 struct vivi_dev
*dev
;
1160 struct list_head
*list
;
1162 while (!list_empty(&vivi_devlist
)) {
1163 list
= vivi_devlist
.next
;
1165 dev
= list_entry(list
, struct vivi_dev
, vivi_devlist
);
1167 v4l2_info(&dev
->v4l2_dev
, "unregistering %s\n",
1168 video_device_node_name(dev
->vfd
));
1169 video_unregister_device(dev
->vfd
);
1170 v4l2_device_unregister(&dev
->v4l2_dev
);
1171 v4l2_ctrl_handler_free(&dev
->ctrl_handler
);
1178 static int __init
vivi_create_instance(int inst
)
1180 struct vivi_dev
*dev
;
1181 struct video_device
*vfd
;
1182 struct v4l2_ctrl_handler
*hdl
;
1183 struct vb2_queue
*q
;
1186 dev
= kzalloc(sizeof(*dev
), GFP_KERNEL
);
1190 snprintf(dev
->v4l2_dev
.name
, sizeof(dev
->v4l2_dev
.name
),
1191 "%s-%03d", VIVI_MODULE_NAME
, inst
);
1192 ret
= v4l2_device_register(NULL
, &dev
->v4l2_dev
);
1196 dev
->fmt
= &formats
[0];
1199 hdl
= &dev
->ctrl_handler
;
1200 v4l2_ctrl_handler_init(hdl
, 11);
1201 dev
->volume
= v4l2_ctrl_new_std(hdl
, &vivi_ctrl_ops
,
1202 V4L2_CID_AUDIO_VOLUME
, 0, 255, 1, 200);
1203 dev
->brightness
= v4l2_ctrl_new_std(hdl
, &vivi_ctrl_ops
,
1204 V4L2_CID_BRIGHTNESS
, 0, 255, 1, 127);
1205 dev
->contrast
= v4l2_ctrl_new_std(hdl
, &vivi_ctrl_ops
,
1206 V4L2_CID_CONTRAST
, 0, 255, 1, 16);
1207 dev
->saturation
= v4l2_ctrl_new_std(hdl
, &vivi_ctrl_ops
,
1208 V4L2_CID_SATURATION
, 0, 255, 1, 127);
1209 dev
->hue
= v4l2_ctrl_new_std(hdl
, &vivi_ctrl_ops
,
1210 V4L2_CID_HUE
, -128, 127, 1, 0);
1211 dev
->button
= v4l2_ctrl_new_custom(hdl
, &vivi_ctrl_button
, NULL
);
1212 dev
->int32
= v4l2_ctrl_new_custom(hdl
, &vivi_ctrl_int32
, NULL
);
1213 dev
->int64
= v4l2_ctrl_new_custom(hdl
, &vivi_ctrl_int64
, NULL
);
1214 dev
->boolean
= v4l2_ctrl_new_custom(hdl
, &vivi_ctrl_boolean
, NULL
);
1215 dev
->menu
= v4l2_ctrl_new_custom(hdl
, &vivi_ctrl_menu
, NULL
);
1216 dev
->string
= v4l2_ctrl_new_custom(hdl
, &vivi_ctrl_string
, NULL
);
1221 dev
->v4l2_dev
.ctrl_handler
= hdl
;
1223 /* initialize locks */
1224 spin_lock_init(&dev
->slock
);
1226 /* initialize queue */
1228 memset(q
, 0, sizeof(dev
->vb_vidq
));
1229 q
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1230 q
->io_modes
= VB2_MMAP
| VB2_USERPTR
| VB2_READ
;
1232 q
->buf_struct_size
= sizeof(struct vivi_buffer
);
1233 q
->ops
= &vivi_video_qops
;
1234 q
->mem_ops
= &vb2_vmalloc_memops
;
1238 mutex_init(&dev
->mutex
);
1240 /* init video dma queues */
1241 INIT_LIST_HEAD(&dev
->vidq
.active
);
1242 init_waitqueue_head(&dev
->vidq
.wq
);
1245 vfd
= video_device_alloc();
1249 *vfd
= vivi_template
;
1251 vfd
->v4l2_dev
= &dev
->v4l2_dev
;
1252 set_bit(V4L2_FL_USE_FH_PRIO
, &vfd
->flags
);
1255 * Provide a mutex to v4l2 core. It will be used to protect
1256 * all fops and v4l2 ioctls.
1258 vfd
->lock
= &dev
->mutex
;
1260 ret
= video_register_device(vfd
, VFL_TYPE_GRABBER
, video_nr
);
1264 video_set_drvdata(vfd
, dev
);
1266 /* Now that everything is fine, let's add it to device list */
1267 list_add_tail(&dev
->vivi_devlist
, &vivi_devlist
);
1273 v4l2_info(&dev
->v4l2_dev
, "V4L2 device registered as %s\n",
1274 video_device_node_name(vfd
));
1278 video_device_release(vfd
);
1280 v4l2_ctrl_handler_free(hdl
);
1281 v4l2_device_unregister(&dev
->v4l2_dev
);
1287 /* This routine allocates from 1 to n_devs virtual drivers.
1289 The real maximum number of virtual drivers will depend on how many drivers
1290 will succeed. This is limited to the maximum number of devices that
1291 videodev supports, which is equal to VIDEO_NUM_DEVICES.
1293 static int __init
vivi_init(void)
1295 const struct font_desc
*font
= find_font("VGA8x16");
1299 printk(KERN_ERR
"vivi: could not find font\n");
1302 font8x16
= font
->data
;
1307 for (i
= 0; i
< n_devs
; i
++) {
1308 ret
= vivi_create_instance(i
);
1310 /* If some instantiations succeeded, keep driver */
1318 printk(KERN_ERR
"vivi: error %d while loading driver\n", ret
);
1322 printk(KERN_INFO
"Video Technology Magazine Virtual Video "
1323 "Capture Board ver %s successfully loaded.\n",
1326 /* n_devs will reflect the actual number of allocated devices */
1332 static void __exit
vivi_exit(void)
1337 module_init(vivi_init
);
1338 module_exit(vivi_exit
);