V4L/DVB: vivi: clean up and a major overhaul
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / media / video / vivi.c
blobd5e30b85c5592db37eddc7b93b082e177f75ad6e
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 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the BSD Licence, GNU General Public License
12 * as published by the Free Software Foundation; either version 2 of the
13 * License, or (at your option) any later version
15 #include <linux/module.h>
16 #include <linux/errno.h>
17 #include <linux/kernel.h>
18 #include <linux/init.h>
19 #include <linux/sched.h>
20 #include <linux/font.h>
21 #include <linux/version.h>
22 #include <linux/mutex.h>
23 #include <linux/videodev2.h>
24 #include <linux/kthread.h>
25 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 20)
26 #include <linux/freezer.h>
27 #endif
28 #include <media/videobuf-vmalloc.h>
29 #include <media/v4l2-device.h>
30 #include <media/v4l2-ioctl.h>
31 #include <media/v4l2-common.h>
33 #define VIVI_MODULE_NAME "vivi"
35 /* Wake up at about 30 fps */
36 #define WAKE_NUMERATOR 30
37 #define WAKE_DENOMINATOR 1001
38 #define BUFFER_TIMEOUT msecs_to_jiffies(500) /* 0.5 seconds */
40 #define MAX_WIDTH 1920
41 #define MAX_HEIGHT 1200
43 #define VIVI_MAJOR_VERSION 0
44 #define VIVI_MINOR_VERSION 7
45 #define VIVI_RELEASE 0
46 #define VIVI_VERSION \
47 KERNEL_VERSION(VIVI_MAJOR_VERSION, VIVI_MINOR_VERSION, VIVI_RELEASE)
49 MODULE_DESCRIPTION("Video Technology Magazine Virtual Video Capture Board");
50 MODULE_AUTHOR("Mauro Carvalho Chehab, Ted Walther and John Sokol");
51 MODULE_LICENSE("Dual BSD/GPL");
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 /* ------------------------------------------------------------------
76 Basic structures
77 ------------------------------------------------------------------*/
79 struct vivi_fmt {
80 char *name;
81 u32 fourcc; /* v4l2 format id */
82 int depth;
85 static struct vivi_fmt formats[] = {
87 .name = "4:2:2, packed, YUYV",
88 .fourcc = V4L2_PIX_FMT_YUYV,
89 .depth = 16,
92 .name = "4:2:2, packed, UYVY",
93 .fourcc = V4L2_PIX_FMT_UYVY,
94 .depth = 16,
97 .name = "RGB565 (LE)",
98 .fourcc = V4L2_PIX_FMT_RGB565, /* gggbbbbb rrrrrggg */
99 .depth = 16,
102 .name = "RGB565 (BE)",
103 .fourcc = V4L2_PIX_FMT_RGB565X, /* rrrrrggg gggbbbbb */
104 .depth = 16,
107 .name = "RGB555 (LE)",
108 .fourcc = V4L2_PIX_FMT_RGB555, /* gggbbbbb arrrrrgg */
109 .depth = 16,
112 .name = "RGB555 (BE)",
113 .fourcc = V4L2_PIX_FMT_RGB555X, /* arrrrrgg gggbbbbb */
114 .depth = 16,
118 static struct vivi_fmt *get_format(struct v4l2_format *f)
120 struct vivi_fmt *fmt;
121 unsigned int k;
123 for (k = 0; k < ARRAY_SIZE(formats); k++) {
124 fmt = &formats[k];
125 if (fmt->fourcc == f->fmt.pix.pixelformat)
126 break;
129 if (k == ARRAY_SIZE(formats))
130 return NULL;
132 return &formats[k];
135 struct sg_to_addr {
136 int pos;
137 struct scatterlist *sg;
140 /* buffer for one video frame */
141 struct vivi_buffer {
142 /* common v4l buffer stuff -- must be first */
143 struct videobuf_buffer vb;
145 struct vivi_fmt *fmt;
148 struct vivi_dmaqueue {
149 struct list_head active;
151 /* thread for generating video stream*/
152 struct task_struct *kthread;
153 wait_queue_head_t wq;
154 /* Counters to control fps rate */
155 int frame;
156 int ini_jiffies;
159 static LIST_HEAD(vivi_devlist);
161 struct vivi_dev {
162 struct list_head vivi_devlist;
163 struct v4l2_device v4l2_dev;
165 /* controls */
166 int brightness;
167 int contrast;
168 int saturation;
169 int hue;
170 int volume;
172 spinlock_t slock;
173 struct mutex mutex;
175 /* various device info */
176 struct video_device *vfd;
178 struct vivi_dmaqueue vidq;
180 /* Several counters */
181 unsigned ms;
182 unsigned long jiffies;
184 int mv_count; /* Controls bars movement */
186 /* Input Number */
187 int input;
189 /* video capture */
190 struct vivi_fmt *fmt;
191 unsigned int width, height;
192 struct videobuf_queue vb_vidq;
194 unsigned long generating;
195 u8 bars[9][3];
196 u8 line[MAX_WIDTH * 4];
199 /* ------------------------------------------------------------------
200 DMA and thread functions
201 ------------------------------------------------------------------*/
203 /* Bars and Colors should match positions */
205 enum colors {
206 WHITE,
207 AMBER,
208 CYAN,
209 GREEN,
210 MAGENTA,
211 RED,
212 BLUE,
213 BLACK,
214 TEXT_BLACK,
217 /* R G B */
218 #define COLOR_WHITE {204, 204, 204}
219 #define COLOR_AMBER {208, 208, 0}
220 #define COLOR_CYAN { 0, 206, 206}
221 #define COLOR_GREEN { 0, 239, 0}
222 #define COLOR_MAGENTA {239, 0, 239}
223 #define COLOR_RED {205, 0, 0}
224 #define COLOR_BLUE { 0, 0, 255}
225 #define COLOR_BLACK { 0, 0, 0}
227 struct bar_std {
228 u8 bar[9][3];
231 /* Maximum number of bars are 10 - otherwise, the input print code
232 should be modified */
233 static struct bar_std bars[] = {
234 { /* Standard ITU-R color bar sequence */
235 { COLOR_WHITE, COLOR_AMBER, COLOR_CYAN, COLOR_GREEN,
236 COLOR_MAGENTA, COLOR_RED, COLOR_BLUE, COLOR_BLACK, COLOR_BLACK }
237 }, {
238 { COLOR_WHITE, COLOR_AMBER, COLOR_BLACK, COLOR_WHITE,
239 COLOR_AMBER, COLOR_BLACK, COLOR_WHITE, COLOR_AMBER, COLOR_BLACK }
240 }, {
241 { COLOR_WHITE, COLOR_CYAN, COLOR_BLACK, COLOR_WHITE,
242 COLOR_CYAN, COLOR_BLACK, COLOR_WHITE, COLOR_CYAN, COLOR_BLACK }
243 }, {
244 { COLOR_WHITE, COLOR_GREEN, COLOR_BLACK, COLOR_WHITE,
245 COLOR_GREEN, COLOR_BLACK, COLOR_WHITE, COLOR_GREEN, COLOR_BLACK }
249 #define NUM_INPUTS ARRAY_SIZE(bars)
251 #define TO_Y(r, g, b) \
252 (((16829 * r + 33039 * g + 6416 * b + 32768) >> 16) + 16)
253 /* RGB to V(Cr) Color transform */
254 #define TO_V(r, g, b) \
255 (((28784 * r - 24103 * g - 4681 * b + 32768) >> 16) + 128)
256 /* RGB to U(Cb) Color transform */
257 #define TO_U(r, g, b) \
258 (((-9714 * r - 19070 * g + 28784 * b + 32768) >> 16) + 128)
260 /* precalculate color bar values to speed up rendering */
261 static void precalculate_bars(struct vivi_dev *dev)
263 u8 r, g, b;
264 int k, is_yuv;
266 for (k = 0; k < 9; k++) {
267 r = bars[dev->input].bar[k][0];
268 g = bars[dev->input].bar[k][1];
269 b = bars[dev->input].bar[k][2];
270 is_yuv = 0;
272 switch (dev->fmt->fourcc) {
273 case V4L2_PIX_FMT_YUYV:
274 case V4L2_PIX_FMT_UYVY:
275 is_yuv = 1;
276 break;
277 case V4L2_PIX_FMT_RGB565:
278 case V4L2_PIX_FMT_RGB565X:
279 r >>= 3;
280 g >>= 2;
281 b >>= 3;
282 break;
283 case V4L2_PIX_FMT_RGB555:
284 case V4L2_PIX_FMT_RGB555X:
285 r >>= 3;
286 g >>= 3;
287 b >>= 3;
288 break;
291 if (is_yuv) {
292 dev->bars[k][0] = TO_Y(r, g, b); /* Luma */
293 dev->bars[k][1] = TO_U(r, g, b); /* Cb */
294 dev->bars[k][2] = TO_V(r, g, b); /* Cr */
295 } else {
296 dev->bars[k][0] = r;
297 dev->bars[k][1] = g;
298 dev->bars[k][2] = b;
303 #define TSTAMP_MIN_Y 24
304 #define TSTAMP_MAX_Y (TSTAMP_MIN_Y + 15)
305 #define TSTAMP_INPUT_X 10
306 #define TSTAMP_MIN_X (54 + TSTAMP_INPUT_X)
308 static void gen_twopix(struct vivi_dev *dev, u8 *buf, int colorpos)
310 u8 r_y, g_u, b_v;
311 int color;
312 u8 *p;
314 r_y = dev->bars[colorpos][0]; /* R or precalculated Y */
315 g_u = dev->bars[colorpos][1]; /* G or precalculated U */
316 b_v = dev->bars[colorpos][2]; /* B or precalculated V */
318 for (color = 0; color < 4; color++) {
319 p = buf + color;
321 switch (dev->fmt->fourcc) {
322 case V4L2_PIX_FMT_YUYV:
323 switch (color) {
324 case 0:
325 case 2:
326 *p = r_y;
327 break;
328 case 1:
329 *p = g_u;
330 break;
331 case 3:
332 *p = b_v;
333 break;
335 break;
336 case V4L2_PIX_FMT_UYVY:
337 switch (color) {
338 case 1:
339 case 3:
340 *p = r_y;
341 break;
342 case 0:
343 *p = g_u;
344 break;
345 case 2:
346 *p = b_v;
347 break;
349 break;
350 case V4L2_PIX_FMT_RGB565:
351 switch (color) {
352 case 0:
353 case 2:
354 *p = (g_u << 5) | b_v;
355 break;
356 case 1:
357 case 3:
358 *p = (r_y << 3) | (g_u >> 3);
359 break;
361 break;
362 case V4L2_PIX_FMT_RGB565X:
363 switch (color) {
364 case 0:
365 case 2:
366 *p = (r_y << 3) | (g_u >> 3);
367 break;
368 case 1:
369 case 3:
370 *p = (g_u << 5) | b_v;
371 break;
373 break;
374 case V4L2_PIX_FMT_RGB555:
375 switch (color) {
376 case 0:
377 case 2:
378 *p = (g_u << 5) | b_v;
379 break;
380 case 1:
381 case 3:
382 *p = (r_y << 2) | (g_u >> 3);
383 break;
385 break;
386 case V4L2_PIX_FMT_RGB555X:
387 switch (color) {
388 case 0:
389 case 2:
390 *p = (r_y << 2) | (g_u >> 3);
391 break;
392 case 1:
393 case 3:
394 *p = (g_u << 5) | b_v;
395 break;
397 break;
402 static void precalculate_line(struct vivi_dev *dev)
404 int w;
406 for (w = 0; w < dev->width * 2; w += 2) {
407 int colorpos = (w / (dev->width / 8) % 8);
409 gen_twopix(dev, dev->line + w * 2, colorpos);
413 static void gen_text(struct vivi_dev *dev, char *basep,
414 int y, int x, char *text)
416 int line;
418 /* Checks if it is possible to show string */
419 if (y + 16 >= dev->height || x + strlen(text) * 8 >= dev->width)
420 return;
422 /* Print stream time */
423 for (line = y; line < y + 16; line++) {
424 int j = 0;
425 char *pos = basep + line * dev->width * 2 + x * 2;
426 char *s;
428 for (s = text; *s; s++) {
429 u8 chr = font8x16[*s * 16 + line - y];
430 int i;
432 for (i = 0; i < 7; i++, j++) {
433 /* Draw white font on black background */
434 if (chr & (1 << (7 - i)))
435 gen_twopix(dev, pos + j * 2, WHITE);
436 else
437 gen_twopix(dev, pos + j * 2, TEXT_BLACK);
443 static void vivi_fillbuff(struct vivi_dev *dev, struct vivi_buffer *buf)
445 int hmax = buf->vb.height;
446 int wmax = buf->vb.width;
447 struct timeval ts;
448 void *vbuf = videobuf_to_vmalloc(&buf->vb);
449 unsigned ms;
450 char str[100];
451 int h, line = 1;
453 if (!vbuf)
454 return;
456 for (h = 0; h < hmax; h++)
457 memcpy(vbuf + h * wmax * 2, dev->line + (dev->mv_count % wmax) * 2, wmax * 2);
459 /* Updates stream time */
461 dev->ms += jiffies_to_msecs(jiffies - dev->jiffies);
462 dev->jiffies = jiffies;
463 ms = dev->ms;
464 snprintf(str, sizeof(str), " %02d:%02d:%02d:%03d ",
465 (ms / (60 * 60 * 1000)) % 24,
466 (ms / (60 * 1000)) % 60,
467 (ms / 1000) % 60,
468 ms % 1000);
469 gen_text(dev, vbuf, line++ * 16, 16, str);
470 snprintf(str, sizeof(str), " %dx%d, input %d ",
471 dev->width, dev->height, dev->input);
472 gen_text(dev, vbuf, line++ * 16, 16, str);
474 snprintf(str, sizeof(str), " brightness %3d, contrast %3d, saturation %3d, hue %d ",
475 dev->brightness,
476 dev->contrast,
477 dev->saturation,
478 dev->hue);
479 gen_text(dev, vbuf, line++ * 16, 16, str);
480 snprintf(str, sizeof(str), " volume %3d ", dev->volume);
481 gen_text(dev, vbuf, line++ * 16, 16, str);
483 dev->mv_count += 2;
485 /* Advice that buffer was filled */
486 buf->vb.field_count++;
487 do_gettimeofday(&ts);
488 buf->vb.ts = ts;
489 buf->vb.state = VIDEOBUF_DONE;
492 static void vivi_thread_tick(struct vivi_dev *dev)
494 struct vivi_dmaqueue *dma_q = &dev->vidq;
495 struct vivi_buffer *buf;
496 unsigned long flags = 0;
498 dprintk(dev, 1, "Thread tick\n");
500 spin_lock_irqsave(&dev->slock, flags);
501 if (list_empty(&dma_q->active)) {
502 dprintk(dev, 1, "No active queue to serve\n");
503 goto unlock;
506 buf = list_entry(dma_q->active.next,
507 struct vivi_buffer, vb.queue);
509 /* Nobody is waiting on this buffer, return */
510 if (!waitqueue_active(&buf->vb.done))
511 goto unlock;
513 list_del(&buf->vb.queue);
515 do_gettimeofday(&buf->vb.ts);
517 /* Fill buffer */
518 vivi_fillbuff(dev, buf);
519 dprintk(dev, 1, "filled buffer %p\n", buf);
521 wake_up(&buf->vb.done);
522 dprintk(dev, 2, "[%p/%d] wakeup\n", buf, buf->vb. i);
523 unlock:
524 spin_unlock_irqrestore(&dev->slock, flags);
527 #define frames_to_ms(frames) \
528 ((frames * WAKE_NUMERATOR * 1000) / WAKE_DENOMINATOR)
530 static void vivi_sleep(struct vivi_dev *dev)
532 struct vivi_dmaqueue *dma_q = &dev->vidq;
533 int timeout;
534 DECLARE_WAITQUEUE(wait, current);
536 dprintk(dev, 1, "%s dma_q=0x%08lx\n", __func__,
537 (unsigned long)dma_q);
539 add_wait_queue(&dma_q->wq, &wait);
540 if (kthread_should_stop())
541 goto stop_task;
543 /* Calculate time to wake up */
544 timeout = msecs_to_jiffies(frames_to_ms(1));
546 vivi_thread_tick(dev);
548 schedule_timeout_interruptible(timeout);
550 stop_task:
551 remove_wait_queue(&dma_q->wq, &wait);
552 try_to_freeze();
555 static int vivi_thread(void *data)
557 struct vivi_dev *dev = data;
559 dprintk(dev, 1, "thread started\n");
561 set_freezable();
563 for (;;) {
564 vivi_sleep(dev);
566 if (kthread_should_stop())
567 break;
569 dprintk(dev, 1, "thread: exit\n");
570 return 0;
573 static void vivi_start_generating(struct file *file)
575 struct vivi_dev *dev = video_drvdata(file);
576 struct vivi_dmaqueue *dma_q = &dev->vidq;
578 dprintk(dev, 1, "%s\n", __func__);
580 if (test_and_set_bit(0, &dev->generating))
581 return;
582 file->private_data = dev;
584 /* Resets frame counters */
585 dev->ms = 0;
586 dev->mv_count = 0;
587 dev->jiffies = jiffies;
589 dma_q->frame = 0;
590 dma_q->ini_jiffies = jiffies;
591 dma_q->kthread = kthread_run(vivi_thread, dev, dev->v4l2_dev.name);
593 if (IS_ERR(dma_q->kthread)) {
594 v4l2_err(&dev->v4l2_dev, "kernel_thread() failed\n");
595 clear_bit(0, &dev->generating);
596 return;
598 /* Wakes thread */
599 wake_up_interruptible(&dma_q->wq);
601 dprintk(dev, 1, "returning from %s\n", __func__);
604 static void vivi_stop_generating(struct file *file)
606 struct vivi_dev *dev = video_drvdata(file);
607 struct vivi_dmaqueue *dma_q = &dev->vidq;
609 dprintk(dev, 1, "%s\n", __func__);
611 if (!file->private_data)
612 return;
613 if (!test_and_clear_bit(0, &dev->generating))
614 return;
616 /* shutdown control thread */
617 if (dma_q->kthread) {
618 kthread_stop(dma_q->kthread);
619 dma_q->kthread = NULL;
621 videobuf_stop(&dev->vb_vidq);
622 videobuf_mmap_free(&dev->vb_vidq);
625 static int vivi_is_generating(struct vivi_dev *dev)
627 return test_bit(0, &dev->generating);
630 /* ------------------------------------------------------------------
631 Videobuf operations
632 ------------------------------------------------------------------*/
633 static int
634 buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size)
636 struct vivi_dev *dev = vq->priv_data;
638 *size = dev->width * dev->height * 2;
640 if (0 == *count)
641 *count = 32;
643 while (*size * *count > vid_limit * 1024 * 1024)
644 (*count)--;
646 dprintk(dev, 1, "%s, count=%d, size=%d\n", __func__,
647 *count, *size);
649 return 0;
652 static void free_buffer(struct videobuf_queue *vq, struct vivi_buffer *buf)
654 struct vivi_dev *dev = vq->priv_data;
656 dprintk(dev, 1, "%s, state: %i\n", __func__, buf->vb.state);
658 videobuf_vmalloc_free(&buf->vb);
659 dprintk(dev, 1, "free_buffer: freed\n");
660 buf->vb.state = VIDEOBUF_NEEDS_INIT;
663 static int
664 buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
665 enum v4l2_field field)
667 struct vivi_dev *dev = vq->priv_data;
668 struct vivi_buffer *buf = container_of(vb, struct vivi_buffer, vb);
669 int rc;
671 dprintk(dev, 1, "%s, field=%d\n", __func__, field);
673 BUG_ON(NULL == dev->fmt);
675 if (dev->width < 48 || dev->width > MAX_WIDTH ||
676 dev->height < 32 || dev->height > MAX_HEIGHT)
677 return -EINVAL;
679 buf->vb.size = dev->width * dev->height * 2;
680 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
681 return -EINVAL;
683 /* These properties only change when queue is idle, see s_fmt */
684 buf->fmt = dev->fmt;
685 buf->vb.width = dev->width;
686 buf->vb.height = dev->height;
687 buf->vb.field = field;
689 precalculate_bars(dev);
690 precalculate_line(dev);
692 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
693 rc = videobuf_iolock(vq, &buf->vb, NULL);
694 if (rc < 0)
695 goto fail;
698 buf->vb.state = VIDEOBUF_PREPARED;
699 return 0;
701 fail:
702 free_buffer(vq, buf);
703 return rc;
706 static void
707 buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
709 struct vivi_dev *dev = vq->priv_data;
710 struct vivi_buffer *buf = container_of(vb, struct vivi_buffer, vb);
711 struct vivi_dmaqueue *vidq = &dev->vidq;
713 dprintk(dev, 1, "%s\n", __func__);
715 buf->vb.state = VIDEOBUF_QUEUED;
716 list_add_tail(&buf->vb.queue, &vidq->active);
719 static void buffer_release(struct videobuf_queue *vq,
720 struct videobuf_buffer *vb)
722 struct vivi_dev *dev = vq->priv_data;
723 struct vivi_buffer *buf = container_of(vb, struct vivi_buffer, vb);
725 dprintk(dev, 1, "%s\n", __func__);
727 free_buffer(vq, buf);
730 static struct videobuf_queue_ops vivi_video_qops = {
731 .buf_setup = buffer_setup,
732 .buf_prepare = buffer_prepare,
733 .buf_queue = buffer_queue,
734 .buf_release = buffer_release,
737 /* ------------------------------------------------------------------
738 IOCTL vidioc handling
739 ------------------------------------------------------------------*/
740 static int vidioc_querycap(struct file *file, void *priv,
741 struct v4l2_capability *cap)
743 struct vivi_dev *dev = video_drvdata(file);
745 strcpy(cap->driver, "vivi");
746 strcpy(cap->card, "vivi");
747 strlcpy(cap->bus_info, dev->v4l2_dev.name, sizeof(cap->bus_info));
748 cap->version = VIVI_VERSION;
749 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING | \
750 V4L2_CAP_READWRITE;
751 return 0;
754 static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
755 struct v4l2_fmtdesc *f)
757 struct vivi_fmt *fmt;
759 if (f->index >= ARRAY_SIZE(formats))
760 return -EINVAL;
762 fmt = &formats[f->index];
764 strlcpy(f->description, fmt->name, sizeof(f->description));
765 f->pixelformat = fmt->fourcc;
766 return 0;
769 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
770 struct v4l2_format *f)
772 struct vivi_dev *dev = video_drvdata(file);
774 f->fmt.pix.width = dev->width;
775 f->fmt.pix.height = dev->height;
776 f->fmt.pix.field = dev->vb_vidq.field;
777 f->fmt.pix.pixelformat = dev->fmt->fourcc;
778 f->fmt.pix.bytesperline =
779 (f->fmt.pix.width * dev->fmt->depth) >> 3;
780 f->fmt.pix.sizeimage =
781 f->fmt.pix.height * f->fmt.pix.bytesperline;
782 return 0;
785 static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
786 struct v4l2_format *f)
788 struct vivi_dev *dev = video_drvdata(file);
789 struct vivi_fmt *fmt;
790 enum v4l2_field field;
792 fmt = get_format(f);
793 if (!fmt) {
794 dprintk(dev, 1, "Fourcc format (0x%08x) invalid.\n",
795 f->fmt.pix.pixelformat);
796 return -EINVAL;
799 field = f->fmt.pix.field;
801 if (field == V4L2_FIELD_ANY) {
802 field = V4L2_FIELD_INTERLACED;
803 } else if (V4L2_FIELD_INTERLACED != field) {
804 dprintk(dev, 1, "Field type invalid.\n");
805 return -EINVAL;
808 f->fmt.pix.field = field;
809 v4l_bound_align_image(&f->fmt.pix.width, 48, MAX_WIDTH, 2,
810 &f->fmt.pix.height, 32, MAX_HEIGHT, 0, 0);
811 f->fmt.pix.bytesperline =
812 (f->fmt.pix.width * fmt->depth) >> 3;
813 f->fmt.pix.sizeimage =
814 f->fmt.pix.height * f->fmt.pix.bytesperline;
815 return 0;
818 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
819 struct v4l2_format *f)
821 struct vivi_dev *dev = video_drvdata(file);
822 struct videobuf_queue *q = &dev->vb_vidq;
824 int ret = vidioc_try_fmt_vid_cap(file, priv, f);
825 if (ret < 0)
826 return ret;
828 mutex_lock(&q->vb_lock);
830 if (vivi_is_generating(dev)) {
831 dprintk(dev, 1, "%s device busy\n", __func__);
832 ret = -EBUSY;
833 goto out;
836 dev->fmt = get_format(f);
837 dev->width = f->fmt.pix.width;
838 dev->height = f->fmt.pix.height;
839 dev->vb_vidq.field = f->fmt.pix.field;
840 ret = 0;
841 out:
842 mutex_unlock(&q->vb_lock);
843 return ret;
846 static int vidioc_reqbufs(struct file *file, void *priv,
847 struct v4l2_requestbuffers *p)
849 struct vivi_dev *dev = video_drvdata(file);
851 return videobuf_reqbufs(&dev->vb_vidq, p);
854 static int vidioc_querybuf(struct file *file, void *priv, struct v4l2_buffer *p)
856 struct vivi_dev *dev = video_drvdata(file);
858 return videobuf_querybuf(&dev->vb_vidq, p);
861 static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *p)
863 struct vivi_dev *dev = video_drvdata(file);
865 return videobuf_qbuf(&dev->vb_vidq, p);
868 static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
870 struct vivi_dev *dev = video_drvdata(file);
872 return videobuf_dqbuf(&dev->vb_vidq, p,
873 file->f_flags & O_NONBLOCK);
876 #ifdef CONFIG_VIDEO_V4L1_COMPAT
877 static int vidiocgmbuf(struct file *file, void *priv, struct video_mbuf *mbuf)
879 struct vivi_dev *dev = video_drvdata(file);
881 return videobuf_cgmbuf(&dev->vb_vidq, mbuf, 8);
883 #endif
885 static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
887 struct vivi_dev *dev = video_drvdata(file);
888 int ret;
890 if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
891 return -EINVAL;
892 ret = videobuf_streamon(&dev->vb_vidq);
893 if (ret)
894 return ret;
896 vivi_start_generating(file);
897 return 0;
900 static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
902 struct vivi_dev *dev = video_drvdata(file);
903 int ret;
905 if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
906 return -EINVAL;
907 ret = videobuf_streamoff(&dev->vb_vidq);
908 if (!ret)
909 vivi_stop_generating(file);
910 return ret;
913 static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *i)
915 return 0;
918 /* only one input in this sample driver */
919 static int vidioc_enum_input(struct file *file, void *priv,
920 struct v4l2_input *inp)
922 if (inp->index >= NUM_INPUTS)
923 return -EINVAL;
925 inp->type = V4L2_INPUT_TYPE_CAMERA;
926 inp->std = V4L2_STD_525_60;
927 sprintf(inp->name, "Camera %u", inp->index);
928 return 0;
931 static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
933 struct vivi_dev *dev = video_drvdata(file);
935 *i = dev->input;
936 return 0;
939 static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
941 struct vivi_dev *dev = video_drvdata(file);
943 if (i >= NUM_INPUTS)
944 return -EINVAL;
946 dev->input = i;
947 precalculate_bars(dev);
948 precalculate_line(dev);
949 return 0;
952 /* --- controls ---------------------------------------------- */
953 static int vidioc_queryctrl(struct file *file, void *priv,
954 struct v4l2_queryctrl *qc)
956 switch (qc->id) {
957 case V4L2_CID_AUDIO_VOLUME:
958 return v4l2_ctrl_query_fill(qc, 0, 255, 1, 200);
959 case V4L2_CID_BRIGHTNESS:
960 return v4l2_ctrl_query_fill(qc, 0, 255, 1, 127);
961 case V4L2_CID_CONTRAST:
962 return v4l2_ctrl_query_fill(qc, 0, 255, 1, 16);
963 case V4L2_CID_SATURATION:
964 return v4l2_ctrl_query_fill(qc, 0, 255, 1, 127);
965 case V4L2_CID_HUE:
966 return v4l2_ctrl_query_fill(qc, -128, 127, 1, 0);
968 return -EINVAL;
971 static int vidioc_g_ctrl(struct file *file, void *priv,
972 struct v4l2_control *ctrl)
974 struct vivi_dev *dev = video_drvdata(file);
976 switch (ctrl->id) {
977 case V4L2_CID_AUDIO_VOLUME:
978 ctrl->value = dev->volume;
979 return 0;
980 case V4L2_CID_BRIGHTNESS:
981 ctrl->value = dev->brightness;
982 return 0;
983 case V4L2_CID_CONTRAST:
984 ctrl->value = dev->contrast;
985 return 0;
986 case V4L2_CID_SATURATION:
987 ctrl->value = dev->saturation;
988 return 0;
989 case V4L2_CID_HUE:
990 ctrl->value = dev->hue;
991 return 0;
993 return -EINVAL;
996 static int vidioc_s_ctrl(struct file *file, void *priv,
997 struct v4l2_control *ctrl)
999 struct vivi_dev *dev = video_drvdata(file);
1000 struct v4l2_queryctrl qc;
1001 int err;
1003 qc.id = ctrl->id;
1004 err = vidioc_queryctrl(file, priv, &qc);
1005 if (err < 0)
1006 return err;
1007 if (ctrl->value < qc.minimum || ctrl->value > qc.maximum)
1008 return -ERANGE;
1009 switch (ctrl->id) {
1010 case V4L2_CID_AUDIO_VOLUME:
1011 dev->volume = ctrl->value;
1012 return 0;
1013 case V4L2_CID_BRIGHTNESS:
1014 dev->brightness = ctrl->value;
1015 return 0;
1016 case V4L2_CID_CONTRAST:
1017 dev->contrast = ctrl->value;
1018 return 0;
1019 case V4L2_CID_SATURATION:
1020 dev->saturation = ctrl->value;
1021 return 0;
1022 case V4L2_CID_HUE:
1023 dev->hue = ctrl->value;
1024 return 0;
1026 return -EINVAL;
1029 /* ------------------------------------------------------------------
1030 File operations for the device
1031 ------------------------------------------------------------------*/
1033 static ssize_t
1034 vivi_read(struct file *file, char __user *data, size_t count, loff_t *ppos)
1036 struct vivi_dev *dev = video_drvdata(file);
1038 vivi_start_generating(file);
1039 return videobuf_read_stream(&dev->vb_vidq, data, count, ppos, 0,
1040 file->f_flags & O_NONBLOCK);
1043 static unsigned int
1044 vivi_poll(struct file *file, struct poll_table_struct *wait)
1046 struct vivi_dev *dev = video_drvdata(file);
1047 struct videobuf_queue *q = &dev->vb_vidq;
1049 dprintk(dev, 1, "%s\n", __func__);
1051 vivi_start_generating(file);
1052 return videobuf_poll_stream(file, q, wait);
1055 static int vivi_close(struct file *file)
1057 struct video_device *vdev = video_devdata(file);
1058 struct vivi_dev *dev = video_drvdata(file);
1060 vivi_stop_generating(file);
1062 dprintk(dev, 1, "close called (dev=%s)\n",
1063 video_device_node_name(vdev));
1064 return 0;
1067 static int vivi_mmap(struct file *file, struct vm_area_struct *vma)
1069 struct vivi_dev *dev = video_drvdata(file);
1070 int ret;
1072 dprintk(dev, 1, "mmap called, vma=0x%08lx\n", (unsigned long)vma);
1074 ret = videobuf_mmap_mapper(&dev->vb_vidq, vma);
1076 dprintk(dev, 1, "vma start=0x%08lx, size=%ld, ret=%d\n",
1077 (unsigned long)vma->vm_start,
1078 (unsigned long)vma->vm_end - (unsigned long)vma->vm_start,
1079 ret);
1080 return ret;
1083 static const struct v4l2_file_operations vivi_fops = {
1084 .owner = THIS_MODULE,
1085 .release = vivi_close,
1086 .read = vivi_read,
1087 .poll = vivi_poll,
1088 .ioctl = video_ioctl2, /* V4L2 ioctl handler */
1089 .mmap = vivi_mmap,
1092 static const struct v4l2_ioctl_ops vivi_ioctl_ops = {
1093 .vidioc_querycap = vidioc_querycap,
1094 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1095 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1096 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1097 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
1098 .vidioc_reqbufs = vidioc_reqbufs,
1099 .vidioc_querybuf = vidioc_querybuf,
1100 .vidioc_qbuf = vidioc_qbuf,
1101 .vidioc_dqbuf = vidioc_dqbuf,
1102 .vidioc_s_std = vidioc_s_std,
1103 .vidioc_enum_input = vidioc_enum_input,
1104 .vidioc_g_input = vidioc_g_input,
1105 .vidioc_s_input = vidioc_s_input,
1106 .vidioc_streamon = vidioc_streamon,
1107 .vidioc_streamoff = vidioc_streamoff,
1108 .vidioc_queryctrl = vidioc_queryctrl,
1109 .vidioc_g_ctrl = vidioc_g_ctrl,
1110 .vidioc_s_ctrl = vidioc_s_ctrl,
1111 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1112 .vidiocgmbuf = vidiocgmbuf,
1113 #endif
1116 static struct video_device vivi_template = {
1117 .name = "vivi",
1118 .fops = &vivi_fops,
1119 .ioctl_ops = &vivi_ioctl_ops,
1120 .release = video_device_release,
1122 .tvnorms = V4L2_STD_525_60,
1123 .current_norm = V4L2_STD_NTSC_M,
1126 /* -----------------------------------------------------------------
1127 Initialization and module stuff
1128 ------------------------------------------------------------------*/
1130 static int vivi_release(void)
1132 struct vivi_dev *dev;
1133 struct list_head *list;
1135 while (!list_empty(&vivi_devlist)) {
1136 list = vivi_devlist.next;
1137 list_del(list);
1138 dev = list_entry(list, struct vivi_dev, vivi_devlist);
1140 v4l2_info(&dev->v4l2_dev, "unregistering %s\n",
1141 video_device_node_name(dev->vfd));
1142 video_unregister_device(dev->vfd);
1143 v4l2_device_unregister(&dev->v4l2_dev);
1144 kfree(dev);
1147 return 0;
1150 static int __init vivi_create_instance(int inst)
1152 struct vivi_dev *dev;
1153 struct video_device *vfd;
1154 int ret;
1156 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1157 if (!dev)
1158 return -ENOMEM;
1160 snprintf(dev->v4l2_dev.name, sizeof(dev->v4l2_dev.name),
1161 "%s-%03d", VIVI_MODULE_NAME, inst);
1162 ret = v4l2_device_register(NULL, &dev->v4l2_dev);
1163 if (ret)
1164 goto free_dev;
1166 dev->fmt = &formats[0];
1167 dev->width = 640;
1168 dev->height = 480;
1169 dev->volume = 200;
1170 dev->brightness = 127;
1171 dev->contrast = 16;
1172 dev->saturation = 127;
1173 dev->hue = 0;
1175 videobuf_queue_vmalloc_init(&dev->vb_vidq, &vivi_video_qops,
1176 NULL, &dev->slock, V4L2_BUF_TYPE_VIDEO_CAPTURE,
1177 V4L2_FIELD_INTERLACED,
1178 sizeof(struct vivi_buffer), dev);
1180 /* init video dma queues */
1181 INIT_LIST_HEAD(&dev->vidq.active);
1182 init_waitqueue_head(&dev->vidq.wq);
1184 /* initialize locks */
1185 spin_lock_init(&dev->slock);
1186 mutex_init(&dev->mutex);
1188 ret = -ENOMEM;
1189 vfd = video_device_alloc();
1190 if (!vfd)
1191 goto unreg_dev;
1193 *vfd = vivi_template;
1194 vfd->debug = debug;
1195 vfd->v4l2_dev = &dev->v4l2_dev;
1197 ret = video_register_device(vfd, VFL_TYPE_GRABBER, video_nr);
1198 if (ret < 0)
1199 goto rel_vdev;
1201 video_set_drvdata(vfd, dev);
1203 /* Now that everything is fine, let's add it to device list */
1204 list_add_tail(&dev->vivi_devlist, &vivi_devlist);
1206 if (video_nr != -1)
1207 video_nr++;
1209 dev->vfd = vfd;
1210 v4l2_info(&dev->v4l2_dev, "V4L2 device registered as %s\n",
1211 video_device_node_name(vfd));
1212 return 0;
1214 rel_vdev:
1215 video_device_release(vfd);
1216 unreg_dev:
1217 v4l2_device_unregister(&dev->v4l2_dev);
1218 free_dev:
1219 kfree(dev);
1220 return ret;
1223 /* This routine allocates from 1 to n_devs virtual drivers.
1225 The real maximum number of virtual drivers will depend on how many drivers
1226 will succeed. This is limited to the maximum number of devices that
1227 videodev supports, which is equal to VIDEO_NUM_DEVICES.
1229 static int __init vivi_init(void)
1231 const struct font_desc *font = find_font("VGA8x16");
1232 int ret = 0, i;
1234 if (font == NULL) {
1235 printk(KERN_ERR "vivi: could not find font\n");
1236 return -ENODEV;
1238 font8x16 = font->data;
1240 if (n_devs <= 0)
1241 n_devs = 1;
1243 for (i = 0; i < n_devs; i++) {
1244 ret = vivi_create_instance(i);
1245 if (ret) {
1246 /* If some instantiations succeeded, keep driver */
1247 if (i)
1248 ret = 0;
1249 break;
1253 if (ret < 0) {
1254 printk(KERN_ERR "vivi: error %d while loading driver\n", ret);
1255 return ret;
1258 printk(KERN_INFO "Video Technology Magazine Virtual Video "
1259 "Capture Board ver %u.%u.%u successfully loaded.\n",
1260 (VIVI_VERSION >> 16) & 0xFF, (VIVI_VERSION >> 8) & 0xFF,
1261 VIVI_VERSION & 0xFF);
1263 /* n_devs will reflect the actual number of allocated devices */
1264 n_devs = i;
1266 return ret;
1269 static void __exit vivi_exit(void)
1271 vivi_release();
1274 module_init(vivi_init);
1275 module_exit(vivi_exit);