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;
167 /* autogain/gain cluster */
168 struct v4l2_ctrl *autogain;
169 struct v4l2_ctrl *gain;
171 struct v4l2_ctrl *volume;
172 struct v4l2_ctrl *button;
173 struct v4l2_ctrl *boolean;
174 struct v4l2_ctrl *int32;
175 struct v4l2_ctrl *int64;
176 struct v4l2_ctrl *menu;
177 struct v4l2_ctrl *string;
182 /* various device info */
183 struct video_device *vfd;
185 struct vivi_dmaqueue vidq;
187 /* Several counters */
189 unsigned long jiffies;
190 unsigned button_pressed;
192 int mv_count; /* Controls bars movement */
198 struct vivi_fmt *fmt;
199 unsigned int width, height;
200 struct vb2_queue vb_vidq;
201 enum v4l2_field field;
202 unsigned int field_count;
205 u8 line[MAX_WIDTH * 4];
208 /* ------------------------------------------------------------------
209 DMA and thread functions
210 ------------------------------------------------------------------*/
212 /* Bars and Colors should match positions */
227 #define COLOR_WHITE {204, 204, 204}
228 #define COLOR_AMBER {208, 208, 0}
229 #define COLOR_CYAN { 0, 206, 206}
230 #define COLOR_GREEN { 0, 239, 0}
231 #define COLOR_MAGENTA {239, 0, 239}
232 #define COLOR_RED {205, 0, 0}
233 #define COLOR_BLUE { 0, 0, 255}
234 #define COLOR_BLACK { 0, 0, 0}
240 /* Maximum number of bars are 10 - otherwise, the input print code
241 should be modified */
242 static struct bar_std bars[] = {
243 { /* Standard ITU-R color bar sequence */
244 { COLOR_WHITE, COLOR_AMBER, COLOR_CYAN, COLOR_GREEN,
245 COLOR_MAGENTA, COLOR_RED, COLOR_BLUE, COLOR_BLACK, COLOR_BLACK }
247 { COLOR_WHITE, COLOR_AMBER, COLOR_BLACK, COLOR_WHITE,
248 COLOR_AMBER, COLOR_BLACK, COLOR_WHITE, COLOR_AMBER, COLOR_BLACK }
250 { COLOR_WHITE, COLOR_CYAN, COLOR_BLACK, COLOR_WHITE,
251 COLOR_CYAN, COLOR_BLACK, COLOR_WHITE, COLOR_CYAN, COLOR_BLACK }
253 { COLOR_WHITE, COLOR_GREEN, COLOR_BLACK, COLOR_WHITE,
254 COLOR_GREEN, COLOR_BLACK, COLOR_WHITE, COLOR_GREEN, COLOR_BLACK }
258 #define NUM_INPUTS ARRAY_SIZE(bars)
260 #define TO_Y(r, g, b) \
261 (((16829 * r + 33039 * g + 6416 * b + 32768) >> 16) + 16)
262 /* RGB to V(Cr) Color transform */
263 #define TO_V(r, g, b) \
264 (((28784 * r - 24103 * g - 4681 * b + 32768) >> 16) + 128)
265 /* RGB to U(Cb) Color transform */
266 #define TO_U(r, g, b) \
267 (((-9714 * r - 19070 * g + 28784 * b + 32768) >> 16) + 128)
269 /* precalculate color bar values to speed up rendering */
270 static void precalculate_bars(struct vivi_dev *dev)
275 for (k = 0; k < 9; k++) {
276 r = bars[dev->input].bar[k][0];
277 g = bars[dev->input].bar[k][1];
278 b = bars[dev->input].bar[k][2];
281 switch (dev->fmt->fourcc) {
282 case V4L2_PIX_FMT_YUYV:
283 case V4L2_PIX_FMT_UYVY:
286 case V4L2_PIX_FMT_RGB565:
287 case V4L2_PIX_FMT_RGB565X:
292 case V4L2_PIX_FMT_RGB555:
293 case V4L2_PIX_FMT_RGB555X:
301 dev->bars[k][0] = TO_Y(r, g, b); /* Luma */
302 dev->bars[k][1] = TO_U(r, g, b); /* Cb */
303 dev->bars[k][2] = TO_V(r, g, b); /* Cr */
312 #define TSTAMP_MIN_Y 24
313 #define TSTAMP_MAX_Y (TSTAMP_MIN_Y + 15)
314 #define TSTAMP_INPUT_X 10
315 #define TSTAMP_MIN_X (54 + TSTAMP_INPUT_X)
317 static void gen_twopix(struct vivi_dev *dev, u8 *buf, int colorpos)
323 r_y = dev->bars[colorpos][0]; /* R or precalculated Y */
324 g_u = dev->bars[colorpos][1]; /* G or precalculated U */
325 b_v = dev->bars[colorpos][2]; /* B or precalculated V */
327 for (color = 0; color < 4; color++) {
330 switch (dev->fmt->fourcc) {
331 case V4L2_PIX_FMT_YUYV:
345 case V4L2_PIX_FMT_UYVY:
359 case V4L2_PIX_FMT_RGB565:
363 *p = (g_u << 5) | b_v;
367 *p = (r_y << 3) | (g_u >> 3);
371 case V4L2_PIX_FMT_RGB565X:
375 *p = (r_y << 3) | (g_u >> 3);
379 *p = (g_u << 5) | b_v;
383 case V4L2_PIX_FMT_RGB555:
387 *p = (g_u << 5) | b_v;
391 *p = (r_y << 2) | (g_u >> 3);
395 case V4L2_PIX_FMT_RGB555X:
399 *p = (r_y << 2) | (g_u >> 3);
403 *p = (g_u << 5) | b_v;
411 static void precalculate_line(struct vivi_dev *dev)
415 for (w = 0; w < dev->width * 2; w += 2) {
416 int colorpos = (w / (dev->width / 8) % 8);
418 gen_twopix(dev, dev->line + w * 2, colorpos);
422 static void gen_text(struct vivi_dev *dev, char *basep,
423 int y, int x, char *text)
427 /* Checks if it is possible to show string */
428 if (y + 16 >= dev->height || x + strlen(text) * 8 >= dev->width)
431 /* Print stream time */
432 for (line = y; line < y + 16; line++) {
434 char *pos = basep + line * dev->width * 2 + x * 2;
437 for (s = text; *s; s++) {
438 u8 chr = font8x16[*s * 16 + line - y];
441 for (i = 0; i < 7; i++, j++) {
442 /* Draw white font on black background */
443 if (chr & (1 << (7 - i)))
444 gen_twopix(dev, pos + j * 2, WHITE);
446 gen_twopix(dev, pos + j * 2, TEXT_BLACK);
452 static void vivi_fillbuff(struct vivi_dev *dev, struct vivi_buffer *buf)
454 int wmax = dev->width;
455 int hmax = dev->height;
457 void *vbuf = vb2_plane_vaddr(&buf->vb, 0);
466 for (h = 0; h < hmax; h++)
467 memcpy(vbuf + h * wmax * 2, dev->line + (dev->mv_count % wmax) * 2, wmax * 2);
469 /* Updates stream time */
471 dev->ms += jiffies_to_msecs(jiffies - dev->jiffies);
472 dev->jiffies = jiffies;
474 snprintf(str, sizeof(str), " %02d:%02d:%02d:%03d ",
475 (ms / (60 * 60 * 1000)) % 24,
476 (ms / (60 * 1000)) % 60,
479 gen_text(dev, vbuf, line++ * 16, 16, str);
480 snprintf(str, sizeof(str), " %dx%d, input %d ",
481 dev->width, dev->height, dev->input);
482 gen_text(dev, vbuf, line++ * 16, 16, str);
484 gain = v4l2_ctrl_g_ctrl(dev->gain);
485 mutex_lock(&dev->ctrl_handler.lock);
486 snprintf(str, sizeof(str), " brightness %3d, contrast %3d, saturation %3d, hue %d ",
487 dev->brightness->cur.val,
488 dev->contrast->cur.val,
489 dev->saturation->cur.val,
491 gen_text(dev, vbuf, line++ * 16, 16, str);
492 snprintf(str, sizeof(str), " autogain %d, gain %3d, volume %3d ",
493 dev->autogain->cur.val, gain, dev->volume->cur.val);
494 gen_text(dev, vbuf, line++ * 16, 16, str);
495 snprintf(str, sizeof(str), " int32 %d, int64 %lld ",
497 dev->int64->cur.val64);
498 gen_text(dev, vbuf, line++ * 16, 16, str);
499 snprintf(str, sizeof(str), " boolean %d, menu %s, string \"%s\" ",
500 dev->boolean->cur.val,
501 dev->menu->qmenu[dev->menu->cur.val],
502 dev->string->cur.string);
503 mutex_unlock(&dev->ctrl_handler.lock);
504 gen_text(dev, vbuf, line++ * 16, 16, str);
505 if (dev->button_pressed) {
506 dev->button_pressed--;
507 snprintf(str, sizeof(str), " button pressed!");
508 gen_text(dev, vbuf, line++ * 16, 16, str);
513 buf->vb.v4l2_buf.field = dev->field;
515 buf->vb.v4l2_buf.sequence = dev->field_count >> 1;
516 do_gettimeofday(&ts);
517 buf->vb.v4l2_buf.timestamp = ts;
520 static void vivi_thread_tick(struct vivi_dev *dev)
522 struct vivi_dmaqueue *dma_q = &dev->vidq;
523 struct vivi_buffer *buf;
524 unsigned long flags = 0;
526 dprintk(dev, 1, "Thread tick\n");
528 spin_lock_irqsave(&dev->slock, flags);
529 if (list_empty(&dma_q->active)) {
530 dprintk(dev, 1, "No active queue to serve\n");
534 buf = list_entry(dma_q->active.next, struct vivi_buffer, list);
535 list_del(&buf->list);
537 do_gettimeofday(&buf->vb.v4l2_buf.timestamp);
540 vivi_fillbuff(dev, buf);
541 dprintk(dev, 1, "filled buffer %p\n", buf);
543 vb2_buffer_done(&buf->vb, VB2_BUF_STATE_DONE);
544 dprintk(dev, 2, "[%p/%d] done\n", buf, buf->vb.v4l2_buf.index);
546 spin_unlock_irqrestore(&dev->slock, flags);
549 #define frames_to_ms(frames) \
550 ((frames * WAKE_NUMERATOR * 1000) / WAKE_DENOMINATOR)
552 static void vivi_sleep(struct vivi_dev *dev)
554 struct vivi_dmaqueue *dma_q = &dev->vidq;
556 DECLARE_WAITQUEUE(wait, current);
558 dprintk(dev, 1, "%s dma_q=0x%08lx\n", __func__,
559 (unsigned long)dma_q);
561 add_wait_queue(&dma_q->wq, &wait);
562 if (kthread_should_stop())
565 /* Calculate time to wake up */
566 timeout = msecs_to_jiffies(frames_to_ms(1));
568 vivi_thread_tick(dev);
570 schedule_timeout_interruptible(timeout);
573 remove_wait_queue(&dma_q->wq, &wait);
577 static int vivi_thread(void *data)
579 struct vivi_dev *dev = data;
581 dprintk(dev, 1, "thread started\n");
588 if (kthread_should_stop())
591 dprintk(dev, 1, "thread: exit\n");
595 static int vivi_start_generating(struct vivi_dev *dev)
597 struct vivi_dmaqueue *dma_q = &dev->vidq;
599 dprintk(dev, 1, "%s\n", __func__);
601 /* Resets frame counters */
604 dev->jiffies = jiffies;
607 dma_q->ini_jiffies = jiffies;
608 dma_q->kthread = kthread_run(vivi_thread, dev, dev->v4l2_dev.name);
610 if (IS_ERR(dma_q->kthread)) {
611 v4l2_err(&dev->v4l2_dev, "kernel_thread() failed\n");
612 return PTR_ERR(dma_q->kthread);
615 wake_up_interruptible(&dma_q->wq);
617 dprintk(dev, 1, "returning from %s\n", __func__);
621 static void vivi_stop_generating(struct vivi_dev *dev)
623 struct vivi_dmaqueue *dma_q = &dev->vidq;
625 dprintk(dev, 1, "%s\n", __func__);
627 /* shutdown control thread */
628 if (dma_q->kthread) {
629 kthread_stop(dma_q->kthread);
630 dma_q->kthread = NULL;
634 * Typical driver might need to wait here until dma engine stops.
635 * In this case we can abort imiedetly, so it's just a noop.
638 /* Release all active buffers */
639 while (!list_empty(&dma_q->active)) {
640 struct vivi_buffer *buf;
641 buf = list_entry(dma_q->active.next, struct vivi_buffer, list);
642 list_del(&buf->list);
643 vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
644 dprintk(dev, 2, "[%p/%d] done\n", buf, buf->vb.v4l2_buf.index);
647 /* ------------------------------------------------------------------
649 ------------------------------------------------------------------*/
650 static int queue_setup(struct vb2_queue *vq, unsigned int *nbuffers,
651 unsigned int *nplanes, unsigned long sizes[],
654 struct vivi_dev *dev = vb2_get_drv_priv(vq);
657 size = dev->width * dev->height * 2;
662 while (size * *nbuffers > vid_limit * 1024 * 1024)
670 * videobuf2-vmalloc allocator is context-less so no need to set
674 dprintk(dev, 1, "%s, count=%d, size=%ld\n", __func__,
680 static int buffer_init(struct vb2_buffer *vb)
682 struct vivi_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
684 BUG_ON(NULL == dev->fmt);
687 * This callback is called once per buffer, after its allocation.
689 * Vivi does not allow changing format during streaming, but it is
690 * possible to do so when streaming is paused (i.e. in streamoff state).
691 * Buffers however are not freed when going into streamoff and so
692 * buffer size verification has to be done in buffer_prepare, on each
694 * It would be best to move verification code here to buf_init and
701 static int buffer_prepare(struct vb2_buffer *vb)
703 struct vivi_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
704 struct vivi_buffer *buf = container_of(vb, struct vivi_buffer, vb);
707 dprintk(dev, 1, "%s, field=%d\n", __func__, vb->v4l2_buf.field);
709 BUG_ON(NULL == dev->fmt);
712 * Theses properties only change when queue is idle, see s_fmt.
713 * The below checks should not be performed here, on each
714 * buffer_prepare (i.e. on each qbuf). Most of the code in this function
715 * should thus be moved to buffer_init and s_fmt.
717 if (dev->width < 48 || dev->width > MAX_WIDTH ||
718 dev->height < 32 || dev->height > MAX_HEIGHT)
721 size = dev->width * dev->height * 2;
722 if (vb2_plane_size(vb, 0) < size) {
723 dprintk(dev, 1, "%s data will not fit into plane (%lu < %lu)\n",
724 __func__, vb2_plane_size(vb, 0), size);
728 vb2_set_plane_payload(&buf->vb, 0, size);
732 precalculate_bars(dev);
733 precalculate_line(dev);
738 static int buffer_finish(struct vb2_buffer *vb)
740 struct vivi_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
741 dprintk(dev, 1, "%s\n", __func__);
745 static void buffer_cleanup(struct vb2_buffer *vb)
747 struct vivi_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
748 dprintk(dev, 1, "%s\n", __func__);
752 static void buffer_queue(struct vb2_buffer *vb)
754 struct vivi_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
755 struct vivi_buffer *buf = container_of(vb, struct vivi_buffer, vb);
756 struct vivi_dmaqueue *vidq = &dev->vidq;
757 unsigned long flags = 0;
759 dprintk(dev, 1, "%s\n", __func__);
761 spin_lock_irqsave(&dev->slock, flags);
762 list_add_tail(&buf->list, &vidq->active);
763 spin_unlock_irqrestore(&dev->slock, flags);
766 static int start_streaming(struct vb2_queue *vq)
768 struct vivi_dev *dev = vb2_get_drv_priv(vq);
769 dprintk(dev, 1, "%s\n", __func__);
770 return vivi_start_generating(dev);
773 /* abort streaming and wait for last buffer */
774 static int stop_streaming(struct vb2_queue *vq)
776 struct vivi_dev *dev = vb2_get_drv_priv(vq);
777 dprintk(dev, 1, "%s\n", __func__);
778 vivi_stop_generating(dev);
782 static void vivi_lock(struct vb2_queue *vq)
784 struct vivi_dev *dev = vb2_get_drv_priv(vq);
785 mutex_lock(&dev->mutex);
788 static void vivi_unlock(struct vb2_queue *vq)
790 struct vivi_dev *dev = vb2_get_drv_priv(vq);
791 mutex_unlock(&dev->mutex);
795 static struct vb2_ops vivi_video_qops = {
796 .queue_setup = queue_setup,
797 .buf_init = buffer_init,
798 .buf_prepare = buffer_prepare,
799 .buf_finish = buffer_finish,
800 .buf_cleanup = buffer_cleanup,
801 .buf_queue = buffer_queue,
802 .start_streaming = start_streaming,
803 .stop_streaming = stop_streaming,
804 .wait_prepare = vivi_unlock,
805 .wait_finish = vivi_lock,
808 /* ------------------------------------------------------------------
809 IOCTL vidioc handling
810 ------------------------------------------------------------------*/
811 static int vidioc_querycap(struct file *file, void *priv,
812 struct v4l2_capability *cap)
814 struct vivi_dev *dev = video_drvdata(file);
816 strcpy(cap->driver, "vivi");
817 strcpy(cap->card, "vivi");
818 strlcpy(cap->bus_info, dev->v4l2_dev.name, sizeof(cap->bus_info));
819 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING | \
824 static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
825 struct v4l2_fmtdesc *f)
827 struct vivi_fmt *fmt;
829 if (f->index >= ARRAY_SIZE(formats))
832 fmt = &formats[f->index];
834 strlcpy(f->description, fmt->name, sizeof(f->description));
835 f->pixelformat = fmt->fourcc;
839 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
840 struct v4l2_format *f)
842 struct vivi_dev *dev = video_drvdata(file);
844 f->fmt.pix.width = dev->width;
845 f->fmt.pix.height = dev->height;
846 f->fmt.pix.field = dev->field;
847 f->fmt.pix.pixelformat = dev->fmt->fourcc;
848 f->fmt.pix.bytesperline =
849 (f->fmt.pix.width * dev->fmt->depth) >> 3;
850 f->fmt.pix.sizeimage =
851 f->fmt.pix.height * f->fmt.pix.bytesperline;
855 static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
856 struct v4l2_format *f)
858 struct vivi_dev *dev = video_drvdata(file);
859 struct vivi_fmt *fmt;
860 enum v4l2_field field;
864 dprintk(dev, 1, "Fourcc format (0x%08x) invalid.\n",
865 f->fmt.pix.pixelformat);
869 field = f->fmt.pix.field;
871 if (field == V4L2_FIELD_ANY) {
872 field = V4L2_FIELD_INTERLACED;
873 } else if (V4L2_FIELD_INTERLACED != field) {
874 dprintk(dev, 1, "Field type invalid.\n");
878 f->fmt.pix.field = field;
879 v4l_bound_align_image(&f->fmt.pix.width, 48, MAX_WIDTH, 2,
880 &f->fmt.pix.height, 32, MAX_HEIGHT, 0, 0);
881 f->fmt.pix.bytesperline =
882 (f->fmt.pix.width * fmt->depth) >> 3;
883 f->fmt.pix.sizeimage =
884 f->fmt.pix.height * f->fmt.pix.bytesperline;
888 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
889 struct v4l2_format *f)
891 struct vivi_dev *dev = video_drvdata(file);
892 struct vb2_queue *q = &dev->vb_vidq;
894 int ret = vidioc_try_fmt_vid_cap(file, priv, f);
898 if (vb2_is_streaming(q)) {
899 dprintk(dev, 1, "%s device busy\n", __func__);
903 dev->fmt = get_format(f);
904 dev->width = f->fmt.pix.width;
905 dev->height = f->fmt.pix.height;
906 dev->field = f->fmt.pix.field;
911 static int vidioc_reqbufs(struct file *file, void *priv,
912 struct v4l2_requestbuffers *p)
914 struct vivi_dev *dev = video_drvdata(file);
915 return vb2_reqbufs(&dev->vb_vidq, p);
918 static int vidioc_querybuf(struct file *file, void *priv, struct v4l2_buffer *p)
920 struct vivi_dev *dev = video_drvdata(file);
921 return vb2_querybuf(&dev->vb_vidq, p);
924 static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *p)
926 struct vivi_dev *dev = video_drvdata(file);
927 return vb2_qbuf(&dev->vb_vidq, p);
930 static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
932 struct vivi_dev *dev = video_drvdata(file);
933 return vb2_dqbuf(&dev->vb_vidq, p, file->f_flags & O_NONBLOCK);
936 static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
938 struct vivi_dev *dev = video_drvdata(file);
939 return vb2_streamon(&dev->vb_vidq, i);
942 static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
944 struct vivi_dev *dev = video_drvdata(file);
945 return vb2_streamoff(&dev->vb_vidq, i);
948 static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *i)
953 /* only one input in this sample driver */
954 static int vidioc_enum_input(struct file *file, void *priv,
955 struct v4l2_input *inp)
957 if (inp->index >= NUM_INPUTS)
960 inp->type = V4L2_INPUT_TYPE_CAMERA;
961 inp->std = V4L2_STD_525_60;
962 sprintf(inp->name, "Camera %u", inp->index);
966 static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
968 struct vivi_dev *dev = video_drvdata(file);
974 static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
976 struct vivi_dev *dev = video_drvdata(file);
982 precalculate_bars(dev);
983 precalculate_line(dev);
987 /* --- controls ---------------------------------------------- */
989 static int vivi_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
991 struct vivi_dev *dev = container_of(ctrl->handler, struct vivi_dev, ctrl_handler);
993 if (ctrl == dev->autogain)
994 dev->gain->val = jiffies & 0xff;
998 static int vivi_s_ctrl(struct v4l2_ctrl *ctrl)
1000 struct vivi_dev *dev = container_of(ctrl->handler, struct vivi_dev, ctrl_handler);
1002 if (ctrl == dev->button)
1003 dev->button_pressed = 30;
1007 /* ------------------------------------------------------------------
1008 File operations for the device
1009 ------------------------------------------------------------------*/
1012 vivi_read(struct file *file, char __user *data, size_t count, loff_t *ppos)
1014 struct vivi_dev *dev = video_drvdata(file);
1016 dprintk(dev, 1, "read called\n");
1017 return vb2_read(&dev->vb_vidq, data, count, ppos,
1018 file->f_flags & O_NONBLOCK);
1022 vivi_poll(struct file *file, struct poll_table_struct *wait)
1024 struct vivi_dev *dev = video_drvdata(file);
1025 struct vb2_queue *q = &dev->vb_vidq;
1027 dprintk(dev, 1, "%s\n", __func__);
1028 return vb2_poll(q, file, wait);
1031 static int vivi_close(struct file *file)
1033 struct video_device *vdev = video_devdata(file);
1034 struct vivi_dev *dev = video_drvdata(file);
1036 dprintk(dev, 1, "close called (dev=%s), file %p\n",
1037 video_device_node_name(vdev), file);
1039 if (v4l2_fh_is_singular_file(file))
1040 vb2_queue_release(&dev->vb_vidq);
1041 return v4l2_fh_release(file);
1044 static int vivi_mmap(struct file *file, struct vm_area_struct *vma)
1046 struct vivi_dev *dev = video_drvdata(file);
1049 dprintk(dev, 1, "mmap called, vma=0x%08lx\n", (unsigned long)vma);
1051 ret = vb2_mmap(&dev->vb_vidq, vma);
1052 dprintk(dev, 1, "vma start=0x%08lx, size=%ld, ret=%d\n",
1053 (unsigned long)vma->vm_start,
1054 (unsigned long)vma->vm_end - (unsigned long)vma->vm_start,
1059 static const struct v4l2_ctrl_ops vivi_ctrl_ops = {
1060 .g_volatile_ctrl = vivi_g_volatile_ctrl,
1061 .s_ctrl = vivi_s_ctrl,
1064 #define VIVI_CID_CUSTOM_BASE (V4L2_CID_USER_BASE | 0xf000)
1066 static const struct v4l2_ctrl_config vivi_ctrl_button = {
1067 .ops = &vivi_ctrl_ops,
1068 .id = VIVI_CID_CUSTOM_BASE + 0,
1070 .type = V4L2_CTRL_TYPE_BUTTON,
1073 static const struct v4l2_ctrl_config vivi_ctrl_boolean = {
1074 .ops = &vivi_ctrl_ops,
1075 .id = VIVI_CID_CUSTOM_BASE + 1,
1077 .type = V4L2_CTRL_TYPE_BOOLEAN,
1084 static const struct v4l2_ctrl_config vivi_ctrl_int32 = {
1085 .ops = &vivi_ctrl_ops,
1086 .id = VIVI_CID_CUSTOM_BASE + 2,
1087 .name = "Integer 32 Bits",
1088 .type = V4L2_CTRL_TYPE_INTEGER,
1094 static const struct v4l2_ctrl_config vivi_ctrl_int64 = {
1095 .ops = &vivi_ctrl_ops,
1096 .id = VIVI_CID_CUSTOM_BASE + 3,
1097 .name = "Integer 64 Bits",
1098 .type = V4L2_CTRL_TYPE_INTEGER64,
1101 static const char * const vivi_ctrl_menu_strings[] = {
1102 "Menu Item 0 (Skipped)",
1104 "Menu Item 2 (Skipped)",
1107 "Menu Item 5 (Skipped)",
1111 static const struct v4l2_ctrl_config vivi_ctrl_menu = {
1112 .ops = &vivi_ctrl_ops,
1113 .id = VIVI_CID_CUSTOM_BASE + 4,
1115 .type = V4L2_CTRL_TYPE_MENU,
1119 .menu_skip_mask = 0x04,
1120 .qmenu = vivi_ctrl_menu_strings,
1123 static const struct v4l2_ctrl_config vivi_ctrl_string = {
1124 .ops = &vivi_ctrl_ops,
1125 .id = VIVI_CID_CUSTOM_BASE + 5,
1127 .type = V4L2_CTRL_TYPE_STRING,
1133 static const struct v4l2_file_operations vivi_fops = {
1134 .owner = THIS_MODULE,
1135 .open = v4l2_fh_open,
1136 .release = vivi_close,
1139 .unlocked_ioctl = video_ioctl2, /* V4L2 ioctl handler */
1143 static const struct v4l2_ioctl_ops vivi_ioctl_ops = {
1144 .vidioc_querycap = vidioc_querycap,
1145 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1146 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1147 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1148 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
1149 .vidioc_reqbufs = vidioc_reqbufs,
1150 .vidioc_querybuf = vidioc_querybuf,
1151 .vidioc_qbuf = vidioc_qbuf,
1152 .vidioc_dqbuf = vidioc_dqbuf,
1153 .vidioc_s_std = vidioc_s_std,
1154 .vidioc_enum_input = vidioc_enum_input,
1155 .vidioc_g_input = vidioc_g_input,
1156 .vidioc_s_input = vidioc_s_input,
1157 .vidioc_streamon = vidioc_streamon,
1158 .vidioc_streamoff = vidioc_streamoff,
1161 static struct video_device vivi_template = {
1164 .ioctl_ops = &vivi_ioctl_ops,
1165 .release = video_device_release,
1167 .tvnorms = V4L2_STD_525_60,
1168 .current_norm = V4L2_STD_NTSC_M,
1171 /* -----------------------------------------------------------------
1172 Initialization and module stuff
1173 ------------------------------------------------------------------*/
1175 static int vivi_release(void)
1177 struct vivi_dev *dev;
1178 struct list_head *list;
1180 while (!list_empty(&vivi_devlist)) {
1181 list = vivi_devlist.next;
1183 dev = list_entry(list, struct vivi_dev, vivi_devlist);
1185 v4l2_info(&dev->v4l2_dev, "unregistering %s\n",
1186 video_device_node_name(dev->vfd));
1187 video_unregister_device(dev->vfd);
1188 v4l2_device_unregister(&dev->v4l2_dev);
1189 v4l2_ctrl_handler_free(&dev->ctrl_handler);
1196 static int __init vivi_create_instance(int inst)
1198 struct vivi_dev *dev;
1199 struct video_device *vfd;
1200 struct v4l2_ctrl_handler *hdl;
1201 struct vb2_queue *q;
1204 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1208 snprintf(dev->v4l2_dev.name, sizeof(dev->v4l2_dev.name),
1209 "%s-%03d", VIVI_MODULE_NAME, inst);
1210 ret = v4l2_device_register(NULL, &dev->v4l2_dev);
1214 dev->fmt = &formats[0];
1217 hdl = &dev->ctrl_handler;
1218 v4l2_ctrl_handler_init(hdl, 11);
1219 dev->volume = v4l2_ctrl_new_std(hdl, &vivi_ctrl_ops,
1220 V4L2_CID_AUDIO_VOLUME, 0, 255, 1, 200);
1221 dev->brightness = v4l2_ctrl_new_std(hdl, &vivi_ctrl_ops,
1222 V4L2_CID_BRIGHTNESS, 0, 255, 1, 127);
1223 dev->contrast = v4l2_ctrl_new_std(hdl, &vivi_ctrl_ops,
1224 V4L2_CID_CONTRAST, 0, 255, 1, 16);
1225 dev->saturation = v4l2_ctrl_new_std(hdl, &vivi_ctrl_ops,
1226 V4L2_CID_SATURATION, 0, 255, 1, 127);
1227 dev->hue = v4l2_ctrl_new_std(hdl, &vivi_ctrl_ops,
1228 V4L2_CID_HUE, -128, 127, 1, 0);
1229 dev->autogain = v4l2_ctrl_new_std(hdl, &vivi_ctrl_ops,
1230 V4L2_CID_AUTOGAIN, 0, 1, 1, 1);
1231 dev->gain = v4l2_ctrl_new_std(hdl, &vivi_ctrl_ops,
1232 V4L2_CID_GAIN, 0, 255, 1, 100);
1233 dev->button = v4l2_ctrl_new_custom(hdl, &vivi_ctrl_button, NULL);
1234 dev->int32 = v4l2_ctrl_new_custom(hdl, &vivi_ctrl_int32, NULL);
1235 dev->int64 = v4l2_ctrl_new_custom(hdl, &vivi_ctrl_int64, NULL);
1236 dev->boolean = v4l2_ctrl_new_custom(hdl, &vivi_ctrl_boolean, NULL);
1237 dev->menu = v4l2_ctrl_new_custom(hdl, &vivi_ctrl_menu, NULL);
1238 dev->string = v4l2_ctrl_new_custom(hdl, &vivi_ctrl_string, NULL);
1243 v4l2_ctrl_auto_cluster(2, &dev->autogain, 0, true);
1244 dev->v4l2_dev.ctrl_handler = hdl;
1246 /* initialize locks */
1247 spin_lock_init(&dev->slock);
1249 /* initialize queue */
1251 memset(q, 0, sizeof(dev->vb_vidq));
1252 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1253 q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_READ;
1255 q->buf_struct_size = sizeof(struct vivi_buffer);
1256 q->ops = &vivi_video_qops;
1257 q->mem_ops = &vb2_vmalloc_memops;
1261 mutex_init(&dev->mutex);
1263 /* init video dma queues */
1264 INIT_LIST_HEAD(&dev->vidq.active);
1265 init_waitqueue_head(&dev->vidq.wq);
1268 vfd = video_device_alloc();
1272 *vfd = vivi_template;
1274 vfd->v4l2_dev = &dev->v4l2_dev;
1275 set_bit(V4L2_FL_USE_FH_PRIO, &vfd->flags);
1278 * Provide a mutex to v4l2 core. It will be used to protect
1279 * all fops and v4l2 ioctls.
1281 vfd->lock = &dev->mutex;
1283 ret = video_register_device(vfd, VFL_TYPE_GRABBER, video_nr);
1287 video_set_drvdata(vfd, dev);
1289 /* Now that everything is fine, let's add it to device list */
1290 list_add_tail(&dev->vivi_devlist, &vivi_devlist);
1296 v4l2_info(&dev->v4l2_dev, "V4L2 device registered as %s\n",
1297 video_device_node_name(vfd));
1301 video_device_release(vfd);
1303 v4l2_ctrl_handler_free(hdl);
1304 v4l2_device_unregister(&dev->v4l2_dev);
1310 /* This routine allocates from 1 to n_devs virtual drivers.
1312 The real maximum number of virtual drivers will depend on how many drivers
1313 will succeed. This is limited to the maximum number of devices that
1314 videodev supports, which is equal to VIDEO_NUM_DEVICES.
1316 static int __init vivi_init(void)
1318 const struct font_desc *font = find_font("VGA8x16");
1322 printk(KERN_ERR "vivi: could not find font\n");
1325 font8x16 = font->data;
1330 for (i = 0; i < n_devs; i++) {
1331 ret = vivi_create_instance(i);
1333 /* If some instantiations succeeded, keep driver */
1341 printk(KERN_ERR "vivi: error %d while loading driver\n", ret);
1345 printk(KERN_INFO "Video Technology Magazine Virtual Video "
1346 "Capture Board ver %s successfully loaded.\n",
1349 /* n_devs will reflect the actual number of allocated devices */
1355 static void __exit vivi_exit(void)
1360 module_init(vivi_init);
1361 module_exit(vivi_exit);