[media] v4l: vivi: port to videobuf2
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / media / video / vivi.c
blob9ed3831b34bd2e890ecd89c82e1c4c0e6745415f
1 /*
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 /* ------------------------------------------------------------------
78 Basic structures
79 ------------------------------------------------------------------*/
81 struct vivi_fmt {
82 char *name;
83 u32 fourcc; /* v4l2 format id */
84 int depth;
87 static struct vivi_fmt formats[] = {
89 .name = "4:2:2, packed, YUYV",
90 .fourcc = V4L2_PIX_FMT_YUYV,
91 .depth = 16,
94 .name = "4:2:2, packed, UYVY",
95 .fourcc = V4L2_PIX_FMT_UYVY,
96 .depth = 16,
99 .name = "RGB565 (LE)",
100 .fourcc = V4L2_PIX_FMT_RGB565, /* gggbbbbb rrrrrggg */
101 .depth = 16,
104 .name = "RGB565 (BE)",
105 .fourcc = V4L2_PIX_FMT_RGB565X, /* rrrrrggg gggbbbbb */
106 .depth = 16,
109 .name = "RGB555 (LE)",
110 .fourcc = V4L2_PIX_FMT_RGB555, /* gggbbbbb arrrrrgg */
111 .depth = 16,
114 .name = "RGB555 (BE)",
115 .fourcc = V4L2_PIX_FMT_RGB555X, /* arrrrrgg gggbbbbb */
116 .depth = 16,
120 static struct vivi_fmt *get_format(struct v4l2_format *f)
122 struct vivi_fmt *fmt;
123 unsigned int k;
125 for (k = 0; k < ARRAY_SIZE(formats); k++) {
126 fmt = &formats[k];
127 if (fmt->fourcc == f->fmt.pix.pixelformat)
128 break;
131 if (k == ARRAY_SIZE(formats))
132 return NULL;
134 return &formats[k];
137 /* buffer for one video frame */
138 struct vivi_buffer {
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 */
152 int frame;
153 int ini_jiffies;
156 static LIST_HEAD(vivi_devlist);
158 struct vivi_dev {
159 struct list_head vivi_devlist;
160 struct v4l2_device v4l2_dev;
162 /* controls */
163 int brightness;
164 int contrast;
165 int saturation;
166 int hue;
167 int volume;
169 spinlock_t slock;
170 struct mutex mutex;
172 /* various device info */
173 struct video_device *vfd;
175 struct vivi_dmaqueue vidq;
177 /* Several counters */
178 unsigned ms;
179 unsigned long jiffies;
181 int mv_count; /* Controls bars movement */
183 /* Input Number */
184 int input;
186 /* video capture */
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;
194 u8 bars[9][3];
195 u8 line[MAX_WIDTH * 4];
198 /* ------------------------------------------------------------------
199 DMA and thread functions
200 ------------------------------------------------------------------*/
202 /* Bars and Colors should match positions */
204 enum colors {
205 WHITE,
206 AMBER,
207 CYAN,
208 GREEN,
209 MAGENTA,
210 RED,
211 BLUE,
212 BLACK,
213 TEXT_BLACK,
216 /* R G B */
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}
226 struct bar_std {
227 u8 bar[9][3];
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 }
236 }, {
237 { COLOR_WHITE, COLOR_AMBER, COLOR_BLACK, COLOR_WHITE,
238 COLOR_AMBER, COLOR_BLACK, COLOR_WHITE, COLOR_AMBER, COLOR_BLACK }
239 }, {
240 { COLOR_WHITE, COLOR_CYAN, COLOR_BLACK, COLOR_WHITE,
241 COLOR_CYAN, COLOR_BLACK, COLOR_WHITE, COLOR_CYAN, COLOR_BLACK }
242 }, {
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)
262 u8 r, g, b;
263 int k, is_yuv;
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];
269 is_yuv = 0;
271 switch (dev->fmt->fourcc) {
272 case V4L2_PIX_FMT_YUYV:
273 case V4L2_PIX_FMT_UYVY:
274 is_yuv = 1;
275 break;
276 case V4L2_PIX_FMT_RGB565:
277 case V4L2_PIX_FMT_RGB565X:
278 r >>= 3;
279 g >>= 2;
280 b >>= 3;
281 break;
282 case V4L2_PIX_FMT_RGB555:
283 case V4L2_PIX_FMT_RGB555X:
284 r >>= 3;
285 g >>= 3;
286 b >>= 3;
287 break;
290 if (is_yuv) {
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 */
294 } else {
295 dev->bars[k][0] = r;
296 dev->bars[k][1] = g;
297 dev->bars[k][2] = b;
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)
309 u8 r_y, g_u, b_v;
310 int color;
311 u8 *p;
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++) {
318 p = buf + color;
320 switch (dev->fmt->fourcc) {
321 case V4L2_PIX_FMT_YUYV:
322 switch (color) {
323 case 0:
324 case 2:
325 *p = r_y;
326 break;
327 case 1:
328 *p = g_u;
329 break;
330 case 3:
331 *p = b_v;
332 break;
334 break;
335 case V4L2_PIX_FMT_UYVY:
336 switch (color) {
337 case 1:
338 case 3:
339 *p = r_y;
340 break;
341 case 0:
342 *p = g_u;
343 break;
344 case 2:
345 *p = b_v;
346 break;
348 break;
349 case V4L2_PIX_FMT_RGB565:
350 switch (color) {
351 case 0:
352 case 2:
353 *p = (g_u << 5) | b_v;
354 break;
355 case 1:
356 case 3:
357 *p = (r_y << 3) | (g_u >> 3);
358 break;
360 break;
361 case V4L2_PIX_FMT_RGB565X:
362 switch (color) {
363 case 0:
364 case 2:
365 *p = (r_y << 3) | (g_u >> 3);
366 break;
367 case 1:
368 case 3:
369 *p = (g_u << 5) | b_v;
370 break;
372 break;
373 case V4L2_PIX_FMT_RGB555:
374 switch (color) {
375 case 0:
376 case 2:
377 *p = (g_u << 5) | b_v;
378 break;
379 case 1:
380 case 3:
381 *p = (r_y << 2) | (g_u >> 3);
382 break;
384 break;
385 case V4L2_PIX_FMT_RGB555X:
386 switch (color) {
387 case 0:
388 case 2:
389 *p = (r_y << 2) | (g_u >> 3);
390 break;
391 case 1:
392 case 3:
393 *p = (g_u << 5) | b_v;
394 break;
396 break;
401 static void precalculate_line(struct vivi_dev *dev)
403 int w;
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)
415 int line;
417 /* Checks if it is possible to show string */
418 if (y + 16 >= dev->height || x + strlen(text) * 8 >= dev->width)
419 return;
421 /* Print stream time */
422 for (line = y; line < y + 16; line++) {
423 int j = 0;
424 char *pos = basep + line * dev->width * 2 + x * 2;
425 char *s;
427 for (s = text; *s; s++) {
428 u8 chr = font8x16[*s * 16 + line - y];
429 int i;
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);
435 else
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;
446 struct timeval ts;
447 void *vbuf = vb2_plane_vaddr(&buf->vb, 0);
448 unsigned ms;
449 char str[100];
450 int h, line = 1;
452 if (!vbuf)
453 return;
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;
462 ms = dev->ms;
463 snprintf(str, sizeof(str), " %02d:%02d:%02d:%03d ",
464 (ms / (60 * 60 * 1000)) % 24,
465 (ms / (60 * 1000)) % 60,
466 (ms / 1000) % 60,
467 ms % 1000);
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 ",
474 dev->brightness,
475 dev->contrast,
476 dev->saturation,
477 dev->hue);
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);
482 dev->mv_count += 2;
484 buf->vb.v4l2_buf.field = dev->field;
485 dev->field_count++;
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");
502 goto unlock;
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);
510 /* Fill buffer */
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);
516 unlock:
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;
526 int timeout;
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())
534 goto stop_task;
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);
543 stop_task:
544 remove_wait_queue(&dma_q->wq, &wait);
545 try_to_freeze();
548 static int vivi_thread(void *data)
550 struct vivi_dev *dev = data;
552 dprintk(dev, 1, "thread started\n");
554 set_freezable();
556 for (;;) {
557 vivi_sleep(dev);
559 if (kthread_should_stop())
560 break;
562 dprintk(dev, 1, "thread: exit\n");
563 return 0;
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 */
573 dev->ms = 0;
574 dev->mv_count = 0;
575 dev->jiffies = jiffies;
577 dma_q->frame = 0;
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);
585 /* Wakes thread */
586 wake_up_interruptible(&dma_q->wq);
588 dprintk(dev, 1, "returning from %s\n", __func__);
589 return 0;
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 /* ------------------------------------------------------------------
619 Videobuf operations
620 ------------------------------------------------------------------*/
621 static int queue_setup(struct vb2_queue *vq, unsigned int *nbuffers,
622 unsigned int *nplanes, unsigned long sizes[],
623 void *alloc_ctxs[])
625 struct vivi_dev *dev = vb2_get_drv_priv(vq);
626 unsigned long size;
628 size = dev->width * dev->height * 2;
630 if (0 == *nbuffers)
631 *nbuffers = 32;
633 while (size * *nbuffers > vid_limit * 1024 * 1024)
634 (*nbuffers)--;
636 *nplanes = 1;
638 sizes[0] = size;
641 * videobuf2-vmalloc allocator is context-less so no need to set
642 * alloc_ctxs array.
645 dprintk(dev, 1, "%s, count=%d, size=%ld\n", __func__,
646 *nbuffers, size);
648 return 0;
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
664 * qbuf.
665 * It would be best to move verification code here to buf_init and
666 * s_fmt though.
669 return 0;
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);
676 unsigned long size;
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)
690 return -EINVAL;
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);
696 return -EINVAL;
699 vb2_set_plane_payload(&buf->vb, 0, size);
701 buf->fmt = dev->fmt;
703 precalculate_bars(dev);
704 precalculate_line(dev);
706 return 0;
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__);
713 return 0;
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);
750 return 0;
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 | \
792 V4L2_CAP_READWRITE;
793 return 0;
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))
802 return -EINVAL;
804 fmt = &formats[f->index];
806 strlcpy(f->description, fmt->name, sizeof(f->description));
807 f->pixelformat = fmt->fourcc;
808 return 0;
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;
824 return 0;
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;
834 fmt = get_format(f);
835 if (!fmt) {
836 dprintk(dev, 1, "Fourcc format (0x%08x) invalid.\n",
837 f->fmt.pix.pixelformat);
838 return -EINVAL;
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");
847 return -EINVAL;
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;
857 return 0;
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);
867 if (ret < 0)
868 return ret;
870 if (vb2_is_streaming(q)) {
871 dprintk(dev, 1, "%s device busy\n", __func__);
872 return -EBUSY;
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;
880 return 0;
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)
922 return 0;
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)
930 return -EINVAL;
932 inp->type = V4L2_INPUT_TYPE_CAMERA;
933 inp->std = V4L2_STD_525_60;
934 sprintf(inp->name, "Camera %u", inp->index);
935 return 0;
938 static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
940 struct vivi_dev *dev = video_drvdata(file);
942 *i = dev->input;
943 return 0;
946 static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
948 struct vivi_dev *dev = video_drvdata(file);
950 if (i >= NUM_INPUTS)
951 return -EINVAL;
953 dev->input = i;
954 precalculate_bars(dev);
955 precalculate_line(dev);
956 return 0;
959 /* --- controls ---------------------------------------------- */
960 static int vidioc_queryctrl(struct file *file, void *priv,
961 struct v4l2_queryctrl *qc)
963 switch (qc->id) {
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);
972 case V4L2_CID_HUE:
973 return v4l2_ctrl_query_fill(qc, -128, 127, 1, 0);
975 return -EINVAL;
978 static int vidioc_g_ctrl(struct file *file, void *priv,
979 struct v4l2_control *ctrl)
981 struct vivi_dev *dev = video_drvdata(file);
983 switch (ctrl->id) {
984 case V4L2_CID_AUDIO_VOLUME:
985 ctrl->value = dev->volume;
986 return 0;
987 case V4L2_CID_BRIGHTNESS:
988 ctrl->value = dev->brightness;
989 return 0;
990 case V4L2_CID_CONTRAST:
991 ctrl->value = dev->contrast;
992 return 0;
993 case V4L2_CID_SATURATION:
994 ctrl->value = dev->saturation;
995 return 0;
996 case V4L2_CID_HUE:
997 ctrl->value = dev->hue;
998 return 0;
1000 return -EINVAL;
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;
1008 int err;
1010 qc.id = ctrl->id;
1011 err = vidioc_queryctrl(file, priv, &qc);
1012 if (err < 0)
1013 return err;
1014 if (ctrl->value < qc.minimum || ctrl->value > qc.maximum)
1015 return -ERANGE;
1016 switch (ctrl->id) {
1017 case V4L2_CID_AUDIO_VOLUME:
1018 dev->volume = ctrl->value;
1019 return 0;
1020 case V4L2_CID_BRIGHTNESS:
1021 dev->brightness = ctrl->value;
1022 return 0;
1023 case V4L2_CID_CONTRAST:
1024 dev->contrast = ctrl->value;
1025 return 0;
1026 case V4L2_CID_SATURATION:
1027 dev->saturation = ctrl->value;
1028 return 0;
1029 case V4L2_CID_HUE:
1030 dev->hue = ctrl->value;
1031 return 0;
1033 return -EINVAL;
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);
1045 dev->open_count++;
1046 return 0;
1049 static ssize_t
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);
1059 static unsigned int
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);
1079 return 0;
1082 static int vivi_mmap(struct file *file, struct vm_area_struct *vma)
1084 struct vivi_dev *dev = video_drvdata(file);
1085 int ret;
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,
1093 ret);
1094 return ret;
1097 static const struct v4l2_file_operations vivi_fops = {
1098 .owner = THIS_MODULE,
1099 .open = vivi_open,
1100 .release = vivi_close,
1101 .read = vivi_read,
1102 .poll = vivi_poll,
1103 .unlocked_ioctl = video_ioctl2, /* V4L2 ioctl handler */
1104 .mmap = vivi_mmap,
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 = {
1129 .name = "vivi",
1130 .fops = &vivi_fops,
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;
1149 list_del(list);
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);
1156 kfree(dev);
1159 return 0;
1162 static int __init vivi_create_instance(int inst)
1164 struct vivi_dev *dev;
1165 struct video_device *vfd;
1166 struct vb2_queue *q;
1167 int ret;
1169 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1170 if (!dev)
1171 return -ENOMEM;
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);
1176 if (ret)
1177 goto free_dev;
1179 dev->fmt = &formats[0];
1180 dev->width = 640;
1181 dev->height = 480;
1182 dev->volume = 200;
1183 dev->brightness = 127;
1184 dev->contrast = 16;
1185 dev->saturation = 127;
1186 dev->hue = 0;
1188 /* initialize locks */
1189 spin_lock_init(&dev->slock);
1191 /* initialize queue */
1192 q = &dev->vb_vidq;
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;
1196 q->drv_priv = dev;
1197 q->buf_struct_size = sizeof(struct vivi_buffer);
1198 q->ops = &vivi_video_qops;
1199 q->mem_ops = &vb2_vmalloc_memops;
1201 vb2_queue_init(q);
1203 mutex_init(&dev->mutex);
1205 /* init video dma queues */
1206 INIT_LIST_HEAD(&dev->vidq.active);
1207 init_waitqueue_head(&dev->vidq.wq);
1209 ret = -ENOMEM;
1210 vfd = video_device_alloc();
1211 if (!vfd)
1212 goto unreg_dev;
1214 *vfd = vivi_template;
1215 vfd->debug = debug;
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);
1225 if (ret < 0)
1226 goto rel_vdev;
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);
1233 if (video_nr != -1)
1234 video_nr++;
1236 dev->vfd = vfd;
1237 v4l2_info(&dev->v4l2_dev, "V4L2 device registered as %s\n",
1238 video_device_node_name(vfd));
1239 return 0;
1241 rel_vdev:
1242 video_device_release(vfd);
1243 unreg_dev:
1244 v4l2_device_unregister(&dev->v4l2_dev);
1245 free_dev:
1246 kfree(dev);
1247 return ret;
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");
1259 int ret = 0, i;
1261 if (font == NULL) {
1262 printk(KERN_ERR "vivi: could not find font\n");
1263 return -ENODEV;
1265 font8x16 = font->data;
1267 if (n_devs <= 0)
1268 n_devs = 1;
1270 for (i = 0; i < n_devs; i++) {
1271 ret = vivi_create_instance(i);
1272 if (ret) {
1273 /* If some instantiations succeeded, keep driver */
1274 if (i)
1275 ret = 0;
1276 break;
1280 if (ret < 0) {
1281 printk(KERN_ERR "vivi: error %d while loading driver\n", ret);
1282 return 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 */
1291 n_devs = i;
1293 return ret;
1296 static void __exit vivi_exit(void)
1298 vivi_release();
1301 module_init(vivi_init);
1302 module_exit(vivi_exit);