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-event.h>
35 #include <media/v4l2-common.h>
37 #define VIVI_MODULE_NAME "vivi"
39 /* Wake up at about 30 fps */
40 #define WAKE_NUMERATOR 30
41 #define WAKE_DENOMINATOR 1001
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 */
86 static struct vivi_fmt formats[] = {
88 .name = "4:2:2, packed, YUYV",
89 .fourcc = V4L2_PIX_FMT_YUYV,
94 .name = "4:2:2, packed, UYVY",
95 .fourcc = V4L2_PIX_FMT_UYVY,
100 .name = "4:2:2, packed, YVYU",
101 .fourcc = V4L2_PIX_FMT_YVYU,
106 .name = "4:2:2, packed, VYUY",
107 .fourcc = V4L2_PIX_FMT_VYUY,
112 .name = "RGB565 (LE)",
113 .fourcc = V4L2_PIX_FMT_RGB565, /* gggbbbbb rrrrrggg */
117 .name = "RGB565 (BE)",
118 .fourcc = V4L2_PIX_FMT_RGB565X, /* rrrrrggg gggbbbbb */
122 .name = "RGB555 (LE)",
123 .fourcc = V4L2_PIX_FMT_RGB555, /* gggbbbbb arrrrrgg */
127 .name = "RGB555 (BE)",
128 .fourcc = V4L2_PIX_FMT_RGB555X, /* arrrrrgg gggbbbbb */
132 .name = "RGB24 (LE)",
133 .fourcc = V4L2_PIX_FMT_RGB24, /* rgb */
137 .name = "RGB24 (BE)",
138 .fourcc = V4L2_PIX_FMT_BGR24, /* bgr */
142 .name = "RGB32 (LE)",
143 .fourcc = V4L2_PIX_FMT_RGB32, /* argb */
147 .name = "RGB32 (BE)",
148 .fourcc = V4L2_PIX_FMT_BGR32, /* bgra */
153 static struct vivi_fmt *get_format(struct v4l2_format *f)
155 struct vivi_fmt *fmt;
158 for (k = 0; k < ARRAY_SIZE(formats); k++) {
160 if (fmt->fourcc == f->fmt.pix.pixelformat)
164 if (k == ARRAY_SIZE(formats))
170 /* buffer for one video frame */
172 /* common v4l buffer stuff -- must be first */
173 struct vb2_buffer vb;
174 struct list_head list;
175 struct vivi_fmt *fmt;
178 struct vivi_dmaqueue {
179 struct list_head active;
181 /* thread for generating video stream*/
182 struct task_struct *kthread;
183 wait_queue_head_t wq;
184 /* Counters to control fps rate */
189 static LIST_HEAD(vivi_devlist);
192 struct list_head vivi_devlist;
193 struct v4l2_device v4l2_dev;
194 struct v4l2_ctrl_handler ctrl_handler;
195 struct video_device vdev;
198 struct v4l2_ctrl *brightness;
199 struct v4l2_ctrl *contrast;
200 struct v4l2_ctrl *saturation;
201 struct v4l2_ctrl *hue;
203 /* autogain/gain cluster */
204 struct v4l2_ctrl *autogain;
205 struct v4l2_ctrl *gain;
207 struct v4l2_ctrl *volume;
208 struct v4l2_ctrl *alpha;
209 struct v4l2_ctrl *button;
210 struct v4l2_ctrl *boolean;
211 struct v4l2_ctrl *int32;
212 struct v4l2_ctrl *int64;
213 struct v4l2_ctrl *menu;
214 struct v4l2_ctrl *string;
215 struct v4l2_ctrl *bitmask;
216 struct v4l2_ctrl *int_menu;
221 struct vivi_dmaqueue vidq;
223 /* Several counters */
225 unsigned long jiffies;
226 unsigned button_pressed;
228 int mv_count; /* Controls bars movement */
234 struct vivi_fmt *fmt;
235 unsigned int width, height;
236 struct vb2_queue vb_vidq;
237 unsigned int field_count;
240 u8 line[MAX_WIDTH * 8];
241 unsigned int pixelsize;
245 /* ------------------------------------------------------------------
246 DMA and thread functions
247 ------------------------------------------------------------------*/
249 /* Bars and Colors should match positions */
264 #define COLOR_WHITE {204, 204, 204}
265 #define COLOR_AMBER {208, 208, 0}
266 #define COLOR_CYAN { 0, 206, 206}
267 #define COLOR_GREEN { 0, 239, 0}
268 #define COLOR_MAGENTA {239, 0, 239}
269 #define COLOR_RED {205, 0, 0}
270 #define COLOR_BLUE { 0, 0, 255}
271 #define COLOR_BLACK { 0, 0, 0}
277 /* Maximum number of bars are 10 - otherwise, the input print code
278 should be modified */
279 static struct bar_std bars[] = {
280 { /* Standard ITU-R color bar sequence */
281 { COLOR_WHITE, COLOR_AMBER, COLOR_CYAN, COLOR_GREEN,
282 COLOR_MAGENTA, COLOR_RED, COLOR_BLUE, COLOR_BLACK, COLOR_BLACK }
284 { COLOR_WHITE, COLOR_AMBER, COLOR_BLACK, COLOR_WHITE,
285 COLOR_AMBER, COLOR_BLACK, COLOR_WHITE, COLOR_AMBER, COLOR_BLACK }
287 { COLOR_WHITE, COLOR_CYAN, COLOR_BLACK, COLOR_WHITE,
288 COLOR_CYAN, COLOR_BLACK, COLOR_WHITE, COLOR_CYAN, COLOR_BLACK }
290 { COLOR_WHITE, COLOR_GREEN, COLOR_BLACK, COLOR_WHITE,
291 COLOR_GREEN, COLOR_BLACK, COLOR_WHITE, COLOR_GREEN, COLOR_BLACK }
295 #define NUM_INPUTS ARRAY_SIZE(bars)
297 #define TO_Y(r, g, b) \
298 (((16829 * r + 33039 * g + 6416 * b + 32768) >> 16) + 16)
299 /* RGB to V(Cr) Color transform */
300 #define TO_V(r, g, b) \
301 (((28784 * r - 24103 * g - 4681 * b + 32768) >> 16) + 128)
302 /* RGB to U(Cb) Color transform */
303 #define TO_U(r, g, b) \
304 (((-9714 * r - 19070 * g + 28784 * b + 32768) >> 16) + 128)
306 /* precalculate color bar values to speed up rendering */
307 static void precalculate_bars(struct vivi_dev *dev)
312 for (k = 0; k < 9; k++) {
313 r = bars[dev->input].bar[k][0];
314 g = bars[dev->input].bar[k][1];
315 b = bars[dev->input].bar[k][2];
316 is_yuv = dev->fmt->is_yuv;
318 switch (dev->fmt->fourcc) {
319 case V4L2_PIX_FMT_RGB565:
320 case V4L2_PIX_FMT_RGB565X:
325 case V4L2_PIX_FMT_RGB555:
326 case V4L2_PIX_FMT_RGB555X:
331 case V4L2_PIX_FMT_YUYV:
332 case V4L2_PIX_FMT_UYVY:
333 case V4L2_PIX_FMT_YVYU:
334 case V4L2_PIX_FMT_VYUY:
335 case V4L2_PIX_FMT_RGB24:
336 case V4L2_PIX_FMT_BGR24:
337 case V4L2_PIX_FMT_RGB32:
338 case V4L2_PIX_FMT_BGR32:
343 dev->bars[k][0] = TO_Y(r, g, b); /* Luma */
344 dev->bars[k][1] = TO_U(r, g, b); /* Cb */
345 dev->bars[k][2] = TO_V(r, g, b); /* Cr */
354 /* 'odd' is true for pixels 1, 3, 5, etc. and false for pixels 0, 2, 4, etc. */
355 static void gen_twopix(struct vivi_dev *dev, u8 *buf, int colorpos, bool odd)
358 u8 alpha = dev->alpha_component;
362 r_y = dev->bars[colorpos][0]; /* R or precalculated Y */
363 g_u = dev->bars[colorpos][1]; /* G or precalculated U */
364 b_v = dev->bars[colorpos][2]; /* B or precalculated V */
366 for (color = 0; color < dev->pixelsize; color++) {
369 switch (dev->fmt->fourcc) {
370 case V4L2_PIX_FMT_YUYV:
376 *p = odd ? b_v : g_u;
380 case V4L2_PIX_FMT_UYVY:
383 *p = odd ? b_v : g_u;
390 case V4L2_PIX_FMT_YVYU:
396 *p = odd ? g_u : b_v;
400 case V4L2_PIX_FMT_VYUY:
403 *p = odd ? g_u : b_v;
410 case V4L2_PIX_FMT_RGB565:
413 *p = (g_u << 5) | b_v;
416 *p = (r_y << 3) | (g_u >> 3);
420 case V4L2_PIX_FMT_RGB565X:
423 *p = (r_y << 3) | (g_u >> 3);
426 *p = (g_u << 5) | b_v;
430 case V4L2_PIX_FMT_RGB555:
433 *p = (g_u << 5) | b_v;
436 *p = (alpha & 0x80) | (r_y << 2) | (g_u >> 3);
440 case V4L2_PIX_FMT_RGB555X:
443 *p = (alpha & 0x80) | (r_y << 2) | (g_u >> 3);
446 *p = (g_u << 5) | b_v;
450 case V4L2_PIX_FMT_RGB24:
463 case V4L2_PIX_FMT_BGR24:
476 case V4L2_PIX_FMT_RGB32:
492 case V4L2_PIX_FMT_BGR32:
512 static void precalculate_line(struct vivi_dev *dev)
516 for (w = 0; w < dev->width * 2; w++) {
517 int colorpos = w / (dev->width / 8) % 8;
519 gen_twopix(dev, dev->line + w * dev->pixelsize, colorpos, w & 1);
523 static void gen_text(struct vivi_dev *dev, char *basep,
524 int y, int x, char *text)
528 /* Checks if it is possible to show string */
529 if (y + 16 >= dev->height || x + strlen(text) * 8 >= dev->width)
532 /* Print stream time */
533 for (line = y; line < y + 16; line++) {
535 char *pos = basep + line * dev->width * dev->pixelsize + x * dev->pixelsize;
538 for (s = text; *s; s++) {
539 u8 chr = font8x16[*s * 16 + line - y];
542 for (i = 0; i < 7; i++, j++) {
543 /* Draw white font on black background */
544 if (chr & (1 << (7 - i)))
545 gen_twopix(dev, pos + j * dev->pixelsize, WHITE, (x+y) & 1);
547 gen_twopix(dev, pos + j * dev->pixelsize, TEXT_BLACK, (x+y) & 1);
553 static void vivi_fillbuff(struct vivi_dev *dev, struct vivi_buffer *buf)
555 int wmax = dev->width;
556 int hmax = dev->height;
558 void *vbuf = vb2_plane_vaddr(&buf->vb, 0);
567 for (h = 0; h < hmax; h++)
568 memcpy(vbuf + h * wmax * dev->pixelsize,
569 dev->line + (dev->mv_count % wmax) * dev->pixelsize,
570 wmax * dev->pixelsize);
572 /* Updates stream time */
574 dev->ms += jiffies_to_msecs(jiffies - dev->jiffies);
575 dev->jiffies = jiffies;
577 snprintf(str, sizeof(str), " %02d:%02d:%02d:%03d ",
578 (ms / (60 * 60 * 1000)) % 24,
579 (ms / (60 * 1000)) % 60,
582 gen_text(dev, vbuf, line++ * 16, 16, str);
583 snprintf(str, sizeof(str), " %dx%d, input %d ",
584 dev->width, dev->height, dev->input);
585 gen_text(dev, vbuf, line++ * 16, 16, str);
587 gain = v4l2_ctrl_g_ctrl(dev->gain);
588 mutex_lock(dev->ctrl_handler.lock);
589 snprintf(str, sizeof(str), " brightness %3d, contrast %3d, saturation %3d, hue %d ",
590 dev->brightness->cur.val,
591 dev->contrast->cur.val,
592 dev->saturation->cur.val,
594 gen_text(dev, vbuf, line++ * 16, 16, str);
595 snprintf(str, sizeof(str), " autogain %d, gain %3d, volume %3d, alpha 0x%02x ",
596 dev->autogain->cur.val, gain, dev->volume->cur.val,
597 dev->alpha->cur.val);
598 gen_text(dev, vbuf, line++ * 16, 16, str);
599 snprintf(str, sizeof(str), " int32 %d, int64 %lld, bitmask %08x ",
601 dev->int64->cur.val64,
602 dev->bitmask->cur.val);
603 gen_text(dev, vbuf, line++ * 16, 16, str);
604 snprintf(str, sizeof(str), " boolean %d, menu %s, string \"%s\" ",
605 dev->boolean->cur.val,
606 dev->menu->qmenu[dev->menu->cur.val],
607 dev->string->cur.string);
608 gen_text(dev, vbuf, line++ * 16, 16, str);
609 snprintf(str, sizeof(str), " integer_menu %lld, value %d ",
610 dev->int_menu->qmenu_int[dev->int_menu->cur.val],
611 dev->int_menu->cur.val);
612 gen_text(dev, vbuf, line++ * 16, 16, str);
613 mutex_unlock(dev->ctrl_handler.lock);
614 if (dev->button_pressed) {
615 dev->button_pressed--;
616 snprintf(str, sizeof(str), " button pressed!");
617 gen_text(dev, vbuf, line++ * 16, 16, str);
622 buf->vb.v4l2_buf.field = V4L2_FIELD_INTERLACED;
624 buf->vb.v4l2_buf.sequence = dev->field_count >> 1;
625 do_gettimeofday(&ts);
626 buf->vb.v4l2_buf.timestamp = ts;
629 static void vivi_thread_tick(struct vivi_dev *dev)
631 struct vivi_dmaqueue *dma_q = &dev->vidq;
632 struct vivi_buffer *buf;
633 unsigned long flags = 0;
635 dprintk(dev, 1, "Thread tick\n");
637 spin_lock_irqsave(&dev->slock, flags);
638 if (list_empty(&dma_q->active)) {
639 dprintk(dev, 1, "No active queue to serve\n");
640 spin_unlock_irqrestore(&dev->slock, flags);
644 buf = list_entry(dma_q->active.next, struct vivi_buffer, list);
645 list_del(&buf->list);
646 spin_unlock_irqrestore(&dev->slock, flags);
648 do_gettimeofday(&buf->vb.v4l2_buf.timestamp);
651 vivi_fillbuff(dev, buf);
652 dprintk(dev, 1, "filled buffer %p\n", buf);
654 vb2_buffer_done(&buf->vb, VB2_BUF_STATE_DONE);
655 dprintk(dev, 2, "[%p/%d] done\n", buf, buf->vb.v4l2_buf.index);
658 #define frames_to_ms(frames) \
659 ((frames * WAKE_NUMERATOR * 1000) / WAKE_DENOMINATOR)
661 static void vivi_sleep(struct vivi_dev *dev)
663 struct vivi_dmaqueue *dma_q = &dev->vidq;
665 DECLARE_WAITQUEUE(wait, current);
667 dprintk(dev, 1, "%s dma_q=0x%08lx\n", __func__,
668 (unsigned long)dma_q);
670 add_wait_queue(&dma_q->wq, &wait);
671 if (kthread_should_stop())
674 /* Calculate time to wake up */
675 timeout = msecs_to_jiffies(frames_to_ms(1));
677 vivi_thread_tick(dev);
679 schedule_timeout_interruptible(timeout);
682 remove_wait_queue(&dma_q->wq, &wait);
686 static int vivi_thread(void *data)
688 struct vivi_dev *dev = data;
690 dprintk(dev, 1, "thread started\n");
697 if (kthread_should_stop())
700 dprintk(dev, 1, "thread: exit\n");
704 static int vivi_start_generating(struct vivi_dev *dev)
706 struct vivi_dmaqueue *dma_q = &dev->vidq;
708 dprintk(dev, 1, "%s\n", __func__);
710 /* Resets frame counters */
713 dev->jiffies = jiffies;
716 dma_q->ini_jiffies = jiffies;
717 dma_q->kthread = kthread_run(vivi_thread, dev, dev->v4l2_dev.name);
719 if (IS_ERR(dma_q->kthread)) {
720 v4l2_err(&dev->v4l2_dev, "kernel_thread() failed\n");
721 return PTR_ERR(dma_q->kthread);
724 wake_up_interruptible(&dma_q->wq);
726 dprintk(dev, 1, "returning from %s\n", __func__);
730 static void vivi_stop_generating(struct vivi_dev *dev)
732 struct vivi_dmaqueue *dma_q = &dev->vidq;
734 dprintk(dev, 1, "%s\n", __func__);
736 /* shutdown control thread */
737 if (dma_q->kthread) {
738 kthread_stop(dma_q->kthread);
739 dma_q->kthread = NULL;
743 * Typical driver might need to wait here until dma engine stops.
744 * In this case we can abort imiedetly, so it's just a noop.
747 /* Release all active buffers */
748 while (!list_empty(&dma_q->active)) {
749 struct vivi_buffer *buf;
750 buf = list_entry(dma_q->active.next, struct vivi_buffer, list);
751 list_del(&buf->list);
752 vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
753 dprintk(dev, 2, "[%p/%d] done\n", buf, buf->vb.v4l2_buf.index);
756 /* ------------------------------------------------------------------
758 ------------------------------------------------------------------*/
759 static int queue_setup(struct vb2_queue *vq, const struct v4l2_format *fmt,
760 unsigned int *nbuffers, unsigned int *nplanes,
761 unsigned int sizes[], void *alloc_ctxs[])
763 struct vivi_dev *dev = vb2_get_drv_priv(vq);
767 size = fmt->fmt.pix.sizeimage;
769 size = dev->width * dev->height * dev->pixelsize;
777 while (size * *nbuffers > vid_limit * 1024 * 1024)
785 * videobuf2-vmalloc allocator is context-less so no need to set
789 dprintk(dev, 1, "%s, count=%d, size=%ld\n", __func__,
795 static int buffer_prepare(struct vb2_buffer *vb)
797 struct vivi_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
798 struct vivi_buffer *buf = container_of(vb, struct vivi_buffer, vb);
801 dprintk(dev, 1, "%s, field=%d\n", __func__, vb->v4l2_buf.field);
803 BUG_ON(NULL == dev->fmt);
806 * Theses properties only change when queue is idle, see s_fmt.
807 * The below checks should not be performed here, on each
808 * buffer_prepare (i.e. on each qbuf). Most of the code in this function
809 * should thus be moved to buffer_init and s_fmt.
811 if (dev->width < 48 || dev->width > MAX_WIDTH ||
812 dev->height < 32 || dev->height > MAX_HEIGHT)
815 size = dev->width * dev->height * dev->pixelsize;
816 if (vb2_plane_size(vb, 0) < size) {
817 dprintk(dev, 1, "%s data will not fit into plane (%lu < %lu)\n",
818 __func__, vb2_plane_size(vb, 0), size);
822 vb2_set_plane_payload(&buf->vb, 0, size);
826 precalculate_bars(dev);
827 precalculate_line(dev);
832 static void buffer_queue(struct vb2_buffer *vb)
834 struct vivi_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
835 struct vivi_buffer *buf = container_of(vb, struct vivi_buffer, vb);
836 struct vivi_dmaqueue *vidq = &dev->vidq;
837 unsigned long flags = 0;
839 dprintk(dev, 1, "%s\n", __func__);
841 spin_lock_irqsave(&dev->slock, flags);
842 list_add_tail(&buf->list, &vidq->active);
843 spin_unlock_irqrestore(&dev->slock, flags);
846 static int start_streaming(struct vb2_queue *vq, unsigned int count)
848 struct vivi_dev *dev = vb2_get_drv_priv(vq);
849 dprintk(dev, 1, "%s\n", __func__);
850 return vivi_start_generating(dev);
853 /* abort streaming and wait for last buffer */
854 static int stop_streaming(struct vb2_queue *vq)
856 struct vivi_dev *dev = vb2_get_drv_priv(vq);
857 dprintk(dev, 1, "%s\n", __func__);
858 vivi_stop_generating(dev);
862 static void vivi_lock(struct vb2_queue *vq)
864 struct vivi_dev *dev = vb2_get_drv_priv(vq);
865 mutex_lock(&dev->mutex);
868 static void vivi_unlock(struct vb2_queue *vq)
870 struct vivi_dev *dev = vb2_get_drv_priv(vq);
871 mutex_unlock(&dev->mutex);
875 static struct vb2_ops vivi_video_qops = {
876 .queue_setup = queue_setup,
877 .buf_prepare = buffer_prepare,
878 .buf_queue = buffer_queue,
879 .start_streaming = start_streaming,
880 .stop_streaming = stop_streaming,
881 .wait_prepare = vivi_unlock,
882 .wait_finish = vivi_lock,
885 /* ------------------------------------------------------------------
886 IOCTL vidioc handling
887 ------------------------------------------------------------------*/
888 static int vidioc_querycap(struct file *file, void *priv,
889 struct v4l2_capability *cap)
891 struct vivi_dev *dev = video_drvdata(file);
893 strcpy(cap->driver, "vivi");
894 strcpy(cap->card, "vivi");
895 snprintf(cap->bus_info, sizeof(cap->bus_info),
896 "platform:%s", dev->v4l2_dev.name);
897 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING |
899 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
903 static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
904 struct v4l2_fmtdesc *f)
906 struct vivi_fmt *fmt;
908 if (f->index >= ARRAY_SIZE(formats))
911 fmt = &formats[f->index];
913 strlcpy(f->description, fmt->name, sizeof(f->description));
914 f->pixelformat = fmt->fourcc;
918 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
919 struct v4l2_format *f)
921 struct vivi_dev *dev = video_drvdata(file);
923 f->fmt.pix.width = dev->width;
924 f->fmt.pix.height = dev->height;
925 f->fmt.pix.field = V4L2_FIELD_INTERLACED;
926 f->fmt.pix.pixelformat = dev->fmt->fourcc;
927 f->fmt.pix.bytesperline =
928 (f->fmt.pix.width * dev->fmt->depth) >> 3;
929 f->fmt.pix.sizeimage =
930 f->fmt.pix.height * f->fmt.pix.bytesperline;
931 if (dev->fmt->is_yuv)
932 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
934 f->fmt.pix.colorspace = V4L2_COLORSPACE_SRGB;
938 static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
939 struct v4l2_format *f)
941 struct vivi_dev *dev = video_drvdata(file);
942 struct vivi_fmt *fmt;
946 dprintk(dev, 1, "Fourcc format (0x%08x) unknown.\n",
947 f->fmt.pix.pixelformat);
948 f->fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
952 f->fmt.pix.field = V4L2_FIELD_INTERLACED;
953 v4l_bound_align_image(&f->fmt.pix.width, 48, MAX_WIDTH, 2,
954 &f->fmt.pix.height, 32, MAX_HEIGHT, 0, 0);
955 f->fmt.pix.bytesperline =
956 (f->fmt.pix.width * fmt->depth) >> 3;
957 f->fmt.pix.sizeimage =
958 f->fmt.pix.height * f->fmt.pix.bytesperline;
960 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
962 f->fmt.pix.colorspace = V4L2_COLORSPACE_SRGB;
967 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
968 struct v4l2_format *f)
970 struct vivi_dev *dev = video_drvdata(file);
971 struct vb2_queue *q = &dev->vb_vidq;
973 int ret = vidioc_try_fmt_vid_cap(file, priv, f);
977 if (vb2_is_busy(q)) {
978 dprintk(dev, 1, "%s device busy\n", __func__);
982 dev->fmt = get_format(f);
983 dev->pixelsize = dev->fmt->depth / 8;
984 dev->width = f->fmt.pix.width;
985 dev->height = f->fmt.pix.height;
990 static int vidioc_enum_framesizes(struct file *file, void *fh,
991 struct v4l2_frmsizeenum *fsize)
993 static const struct v4l2_frmsize_stepwise sizes = {
1001 for (i = 0; i < ARRAY_SIZE(formats); i++)
1002 if (formats[i].fourcc == fsize->pixel_format)
1004 if (i == ARRAY_SIZE(formats))
1006 fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE;
1007 fsize->stepwise = sizes;
1011 /* only one input in this sample driver */
1012 static int vidioc_enum_input(struct file *file, void *priv,
1013 struct v4l2_input *inp)
1015 if (inp->index >= NUM_INPUTS)
1018 inp->type = V4L2_INPUT_TYPE_CAMERA;
1019 sprintf(inp->name, "Camera %u", inp->index);
1023 static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1025 struct vivi_dev *dev = video_drvdata(file);
1031 static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1033 struct vivi_dev *dev = video_drvdata(file);
1035 if (i >= NUM_INPUTS)
1038 if (i == dev->input)
1042 precalculate_bars(dev);
1043 precalculate_line(dev);
1047 /* --- controls ---------------------------------------------- */
1049 static int vivi_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
1051 struct vivi_dev *dev = container_of(ctrl->handler, struct vivi_dev, ctrl_handler);
1053 if (ctrl == dev->autogain)
1054 dev->gain->val = jiffies & 0xff;
1058 static int vivi_s_ctrl(struct v4l2_ctrl *ctrl)
1060 struct vivi_dev *dev = container_of(ctrl->handler, struct vivi_dev, ctrl_handler);
1063 case V4L2_CID_ALPHA_COMPONENT:
1064 dev->alpha_component = ctrl->val;
1067 if (ctrl == dev->button)
1068 dev->button_pressed = 30;
1074 /* ------------------------------------------------------------------
1075 File operations for the device
1076 ------------------------------------------------------------------*/
1078 static const struct v4l2_ctrl_ops vivi_ctrl_ops = {
1079 .g_volatile_ctrl = vivi_g_volatile_ctrl,
1080 .s_ctrl = vivi_s_ctrl,
1083 #define VIVI_CID_CUSTOM_BASE (V4L2_CID_USER_BASE | 0xf000)
1085 static const struct v4l2_ctrl_config vivi_ctrl_button = {
1086 .ops = &vivi_ctrl_ops,
1087 .id = VIVI_CID_CUSTOM_BASE + 0,
1089 .type = V4L2_CTRL_TYPE_BUTTON,
1092 static const struct v4l2_ctrl_config vivi_ctrl_boolean = {
1093 .ops = &vivi_ctrl_ops,
1094 .id = VIVI_CID_CUSTOM_BASE + 1,
1096 .type = V4L2_CTRL_TYPE_BOOLEAN,
1103 static const struct v4l2_ctrl_config vivi_ctrl_int32 = {
1104 .ops = &vivi_ctrl_ops,
1105 .id = VIVI_CID_CUSTOM_BASE + 2,
1106 .name = "Integer 32 Bits",
1107 .type = V4L2_CTRL_TYPE_INTEGER,
1113 static const struct v4l2_ctrl_config vivi_ctrl_int64 = {
1114 .ops = &vivi_ctrl_ops,
1115 .id = VIVI_CID_CUSTOM_BASE + 3,
1116 .name = "Integer 64 Bits",
1117 .type = V4L2_CTRL_TYPE_INTEGER64,
1120 static const char * const vivi_ctrl_menu_strings[] = {
1121 "Menu Item 0 (Skipped)",
1123 "Menu Item 2 (Skipped)",
1126 "Menu Item 5 (Skipped)",
1130 static const struct v4l2_ctrl_config vivi_ctrl_menu = {
1131 .ops = &vivi_ctrl_ops,
1132 .id = VIVI_CID_CUSTOM_BASE + 4,
1134 .type = V4L2_CTRL_TYPE_MENU,
1138 .menu_skip_mask = 0x04,
1139 .qmenu = vivi_ctrl_menu_strings,
1142 static const struct v4l2_ctrl_config vivi_ctrl_string = {
1143 .ops = &vivi_ctrl_ops,
1144 .id = VIVI_CID_CUSTOM_BASE + 5,
1146 .type = V4L2_CTRL_TYPE_STRING,
1152 static const struct v4l2_ctrl_config vivi_ctrl_bitmask = {
1153 .ops = &vivi_ctrl_ops,
1154 .id = VIVI_CID_CUSTOM_BASE + 6,
1156 .type = V4L2_CTRL_TYPE_BITMASK,
1163 static const s64 vivi_ctrl_int_menu_values[] = {
1164 1, 1, 2, 3, 5, 8, 13, 21, 42,
1167 static const struct v4l2_ctrl_config vivi_ctrl_int_menu = {
1168 .ops = &vivi_ctrl_ops,
1169 .id = VIVI_CID_CUSTOM_BASE + 7,
1170 .name = "Integer menu",
1171 .type = V4L2_CTRL_TYPE_INTEGER_MENU,
1175 .menu_skip_mask = 0x02,
1176 .qmenu_int = vivi_ctrl_int_menu_values,
1179 static const struct v4l2_file_operations vivi_fops = {
1180 .owner = THIS_MODULE,
1181 .open = v4l2_fh_open,
1182 .release = vb2_fop_release,
1183 .read = vb2_fop_read,
1184 .poll = vb2_fop_poll,
1185 .unlocked_ioctl = video_ioctl2, /* V4L2 ioctl handler */
1186 .mmap = vb2_fop_mmap,
1189 static const struct v4l2_ioctl_ops vivi_ioctl_ops = {
1190 .vidioc_querycap = vidioc_querycap,
1191 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1192 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1193 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1194 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
1195 .vidioc_enum_framesizes = vidioc_enum_framesizes,
1196 .vidioc_reqbufs = vb2_ioctl_reqbufs,
1197 .vidioc_create_bufs = vb2_ioctl_create_bufs,
1198 .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
1199 .vidioc_querybuf = vb2_ioctl_querybuf,
1200 .vidioc_qbuf = vb2_ioctl_qbuf,
1201 .vidioc_dqbuf = vb2_ioctl_dqbuf,
1202 .vidioc_enum_input = vidioc_enum_input,
1203 .vidioc_g_input = vidioc_g_input,
1204 .vidioc_s_input = vidioc_s_input,
1205 .vidioc_streamon = vb2_ioctl_streamon,
1206 .vidioc_streamoff = vb2_ioctl_streamoff,
1207 .vidioc_log_status = v4l2_ctrl_log_status,
1208 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1209 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1212 static struct video_device vivi_template = {
1215 .ioctl_ops = &vivi_ioctl_ops,
1216 .release = video_device_release_empty,
1219 /* -----------------------------------------------------------------
1220 Initialization and module stuff
1221 ------------------------------------------------------------------*/
1223 static int vivi_release(void)
1225 struct vivi_dev *dev;
1226 struct list_head *list;
1228 while (!list_empty(&vivi_devlist)) {
1229 list = vivi_devlist.next;
1231 dev = list_entry(list, struct vivi_dev, vivi_devlist);
1233 v4l2_info(&dev->v4l2_dev, "unregistering %s\n",
1234 video_device_node_name(&dev->vdev));
1235 video_unregister_device(&dev->vdev);
1236 v4l2_device_unregister(&dev->v4l2_dev);
1237 v4l2_ctrl_handler_free(&dev->ctrl_handler);
1244 static int __init vivi_create_instance(int inst)
1246 struct vivi_dev *dev;
1247 struct video_device *vfd;
1248 struct v4l2_ctrl_handler *hdl;
1249 struct vb2_queue *q;
1252 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1256 snprintf(dev->v4l2_dev.name, sizeof(dev->v4l2_dev.name),
1257 "%s-%03d", VIVI_MODULE_NAME, inst);
1258 ret = v4l2_device_register(NULL, &dev->v4l2_dev);
1262 dev->fmt = &formats[0];
1265 dev->pixelsize = dev->fmt->depth / 8;
1266 hdl = &dev->ctrl_handler;
1267 v4l2_ctrl_handler_init(hdl, 11);
1268 dev->volume = v4l2_ctrl_new_std(hdl, &vivi_ctrl_ops,
1269 V4L2_CID_AUDIO_VOLUME, 0, 255, 1, 200);
1270 dev->brightness = v4l2_ctrl_new_std(hdl, &vivi_ctrl_ops,
1271 V4L2_CID_BRIGHTNESS, 0, 255, 1, 127);
1272 dev->contrast = v4l2_ctrl_new_std(hdl, &vivi_ctrl_ops,
1273 V4L2_CID_CONTRAST, 0, 255, 1, 16);
1274 dev->saturation = v4l2_ctrl_new_std(hdl, &vivi_ctrl_ops,
1275 V4L2_CID_SATURATION, 0, 255, 1, 127);
1276 dev->hue = v4l2_ctrl_new_std(hdl, &vivi_ctrl_ops,
1277 V4L2_CID_HUE, -128, 127, 1, 0);
1278 dev->autogain = v4l2_ctrl_new_std(hdl, &vivi_ctrl_ops,
1279 V4L2_CID_AUTOGAIN, 0, 1, 1, 1);
1280 dev->gain = v4l2_ctrl_new_std(hdl, &vivi_ctrl_ops,
1281 V4L2_CID_GAIN, 0, 255, 1, 100);
1282 dev->alpha = v4l2_ctrl_new_std(hdl, &vivi_ctrl_ops,
1283 V4L2_CID_ALPHA_COMPONENT, 0, 255, 1, 0);
1284 dev->button = v4l2_ctrl_new_custom(hdl, &vivi_ctrl_button, NULL);
1285 dev->int32 = v4l2_ctrl_new_custom(hdl, &vivi_ctrl_int32, NULL);
1286 dev->int64 = v4l2_ctrl_new_custom(hdl, &vivi_ctrl_int64, NULL);
1287 dev->boolean = v4l2_ctrl_new_custom(hdl, &vivi_ctrl_boolean, NULL);
1288 dev->menu = v4l2_ctrl_new_custom(hdl, &vivi_ctrl_menu, NULL);
1289 dev->string = v4l2_ctrl_new_custom(hdl, &vivi_ctrl_string, NULL);
1290 dev->bitmask = v4l2_ctrl_new_custom(hdl, &vivi_ctrl_bitmask, NULL);
1291 dev->int_menu = v4l2_ctrl_new_custom(hdl, &vivi_ctrl_int_menu, NULL);
1296 v4l2_ctrl_auto_cluster(2, &dev->autogain, 0, true);
1297 dev->v4l2_dev.ctrl_handler = hdl;
1299 /* initialize locks */
1300 spin_lock_init(&dev->slock);
1302 /* initialize queue */
1304 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1305 q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF | VB2_READ;
1307 q->buf_struct_size = sizeof(struct vivi_buffer);
1308 q->ops = &vivi_video_qops;
1309 q->mem_ops = &vb2_vmalloc_memops;
1311 ret = vb2_queue_init(q);
1315 mutex_init(&dev->mutex);
1317 /* init video dma queues */
1318 INIT_LIST_HEAD(&dev->vidq.active);
1319 init_waitqueue_head(&dev->vidq.wq);
1322 *vfd = vivi_template;
1324 vfd->v4l2_dev = &dev->v4l2_dev;
1326 set_bit(V4L2_FL_USE_FH_PRIO, &vfd->flags);
1329 * Provide a mutex to v4l2 core. It will be used to protect
1330 * all fops and v4l2 ioctls.
1332 vfd->lock = &dev->mutex;
1333 video_set_drvdata(vfd, dev);
1335 ret = video_register_device(vfd, VFL_TYPE_GRABBER, video_nr);
1339 /* Now that everything is fine, let's add it to device list */
1340 list_add_tail(&dev->vivi_devlist, &vivi_devlist);
1342 v4l2_info(&dev->v4l2_dev, "V4L2 device registered as %s\n",
1343 video_device_node_name(vfd));
1347 v4l2_ctrl_handler_free(hdl);
1348 v4l2_device_unregister(&dev->v4l2_dev);
1354 /* This routine allocates from 1 to n_devs virtual drivers.
1356 The real maximum number of virtual drivers will depend on how many drivers
1357 will succeed. This is limited to the maximum number of devices that
1358 videodev supports, which is equal to VIDEO_NUM_DEVICES.
1360 static int __init vivi_init(void)
1362 const struct font_desc *font = find_font("VGA8x16");
1366 printk(KERN_ERR "vivi: could not find font\n");
1369 font8x16 = font->data;
1374 for (i = 0; i < n_devs; i++) {
1375 ret = vivi_create_instance(i);
1377 /* If some instantiations succeeded, keep driver */
1385 printk(KERN_ERR "vivi: error %d while loading driver\n", ret);
1389 printk(KERN_INFO "Video Technology Magazine Virtual Video "
1390 "Capture Board ver %s successfully loaded.\n",
1393 /* n_devs will reflect the actual number of allocated devices */
1399 static void __exit vivi_exit(void)
1404 module_init(vivi_init);
1405 module_exit(vivi_exit);