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/delay.h>
17 #include <linux/errno.h>
19 #include <linux/kernel.h>
20 #include <linux/slab.h>
22 #include <linux/ioport.h>
23 #include <linux/init.h>
24 #include <linux/sched.h>
25 #include <linux/pci.h>
26 #include <linux/random.h>
27 #include <linux/version.h>
28 #include <linux/mutex.h>
29 #include <linux/videodev2.h>
30 #include <linux/dma-mapping.h>
31 #ifdef CONFIG_VIDEO_V4L1_COMPAT
32 /* Include V4L1 specific functions. Should be removed soon */
33 #include <linux/videodev.h>
35 #include <linux/interrupt.h>
36 #include <linux/kthread.h>
37 #include <linux/highmem.h>
38 #include <linux/freezer.h>
39 #include <media/videobuf-vmalloc.h>
40 #include <media/v4l2-device.h>
41 #include <media/v4l2-ioctl.h>
44 #define VIVI_MODULE_NAME "vivi"
46 /* Wake up at about 30 fps */
47 #define WAKE_NUMERATOR 30
48 #define WAKE_DENOMINATOR 1001
49 #define BUFFER_TIMEOUT msecs_to_jiffies(500) /* 0.5 seconds */
51 #define VIVI_MAJOR_VERSION 0
52 #define VIVI_MINOR_VERSION 6
53 #define VIVI_RELEASE 0
54 #define VIVI_VERSION \
55 KERNEL_VERSION(VIVI_MAJOR_VERSION, VIVI_MINOR_VERSION, VIVI_RELEASE)
57 MODULE_DESCRIPTION("Video Technology Magazine Virtual Video Capture Board");
58 MODULE_AUTHOR("Mauro Carvalho Chehab, Ted Walther and John Sokol");
59 MODULE_LICENSE("Dual BSD/GPL");
61 static unsigned video_nr
= -1;
62 module_param(video_nr
, uint
, 0644);
63 MODULE_PARM_DESC(video_nr
, "videoX start number, -1 is autodetect");
65 static unsigned n_devs
= 1;
66 module_param(n_devs
, uint
, 0644);
67 MODULE_PARM_DESC(n_devs
, "number of video devices to create");
69 static unsigned debug
;
70 module_param(debug
, uint
, 0644);
71 MODULE_PARM_DESC(debug
, "activates debug info");
73 static unsigned int vid_limit
= 16;
74 module_param(vid_limit
, uint
, 0644);
75 MODULE_PARM_DESC(vid_limit
, "capture memory limit in megabytes");
78 /* supported controls */
79 static struct v4l2_queryctrl vivi_qctrl
[] = {
81 .id
= V4L2_CID_AUDIO_VOLUME
,
86 .default_value
= 65535,
87 .flags
= V4L2_CTRL_FLAG_SLIDER
,
88 .type
= V4L2_CTRL_TYPE_INTEGER
,
90 .id
= V4L2_CID_BRIGHTNESS
,
91 .type
= V4L2_CTRL_TYPE_INTEGER
,
97 .flags
= V4L2_CTRL_FLAG_SLIDER
,
99 .id
= V4L2_CID_CONTRAST
,
100 .type
= V4L2_CTRL_TYPE_INTEGER
,
105 .default_value
= 0x10,
106 .flags
= V4L2_CTRL_FLAG_SLIDER
,
108 .id
= V4L2_CID_SATURATION
,
109 .type
= V4L2_CTRL_TYPE_INTEGER
,
110 .name
= "Saturation",
114 .default_value
= 127,
115 .flags
= V4L2_CTRL_FLAG_SLIDER
,
118 .type
= V4L2_CTRL_TYPE_INTEGER
,
124 .flags
= V4L2_CTRL_FLAG_SLIDER
,
128 #define dprintk(dev, level, fmt, arg...) \
129 v4l2_dbg(level, debug, &dev->v4l2_dev, fmt, ## arg)
131 /* ------------------------------------------------------------------
133 ------------------------------------------------------------------*/
137 u32 fourcc
; /* v4l2 format id */
141 static struct vivi_fmt formats
[] = {
143 .name
= "4:2:2, packed, YUYV",
144 .fourcc
= V4L2_PIX_FMT_YUYV
,
148 .name
= "4:2:2, packed, UYVY",
149 .fourcc
= V4L2_PIX_FMT_UYVY
,
153 .name
= "RGB565 (LE)",
154 .fourcc
= V4L2_PIX_FMT_RGB565
, /* gggbbbbb rrrrrggg */
158 .name
= "RGB565 (BE)",
159 .fourcc
= V4L2_PIX_FMT_RGB565X
, /* rrrrrggg gggbbbbb */
163 .name
= "RGB555 (LE)",
164 .fourcc
= V4L2_PIX_FMT_RGB555
, /* gggbbbbb arrrrrgg */
168 .name
= "RGB555 (BE)",
169 .fourcc
= V4L2_PIX_FMT_RGB555X
, /* arrrrrgg gggbbbbb */
174 static struct vivi_fmt
*get_format(struct v4l2_format
*f
)
176 struct vivi_fmt
*fmt
;
179 for (k
= 0; k
< ARRAY_SIZE(formats
); k
++) {
181 if (fmt
->fourcc
== f
->fmt
.pix
.pixelformat
)
185 if (k
== ARRAY_SIZE(formats
))
193 struct scatterlist
*sg
;
196 /* buffer for one video frame */
198 /* common v4l buffer stuff -- must be first */
199 struct videobuf_buffer vb
;
201 struct vivi_fmt
*fmt
;
204 struct vivi_dmaqueue
{
205 struct list_head active
;
207 /* thread for generating video stream*/
208 struct task_struct
*kthread
;
209 wait_queue_head_t wq
;
210 /* Counters to control fps rate */
215 static LIST_HEAD(vivi_devlist
);
218 struct list_head vivi_devlist
;
219 struct v4l2_device v4l2_dev
;
226 /* various device info */
227 struct video_device
*vfd
;
229 struct vivi_dmaqueue vidq
;
231 /* Several counters */
233 unsigned long jiffies
;
236 int mv_count
; /* Controls bars movement */
241 /* Control 'registers' */
242 int qctl_regs
[ARRAY_SIZE(vivi_qctrl
)];
246 struct vivi_dev
*dev
;
249 struct vivi_fmt
*fmt
;
250 unsigned int width
, height
;
251 struct videobuf_queue vb_vidq
;
253 enum v4l2_buf_type type
;
254 unsigned char bars
[8][3];
255 int input
; /* Input Number on bars */
258 /* ------------------------------------------------------------------
259 DMA and thread functions
260 ------------------------------------------------------------------*/
262 /* Bars and Colors should match positions */
276 #define COLOR_WHITE {204, 204, 204}
277 #define COLOR_AMBAR {208, 208, 0}
278 #define COLOR_CIAN { 0, 206, 206}
279 #define COLOR_GREEN { 0, 239, 0}
280 #define COLOR_MAGENTA {239, 0, 239}
281 #define COLOR_RED {205, 0, 0}
282 #define COLOR_BLUE { 0, 0, 255}
283 #define COLOR_BLACK { 0, 0, 0}
289 /* Maximum number of bars are 10 - otherwise, the input print code
290 should be modified */
291 static struct bar_std bars
[] = {
292 { /* Standard ITU-R color bar sequence */
339 #define NUM_INPUTS ARRAY_SIZE(bars)
341 #define TO_Y(r, g, b) \
342 (((16829 * r + 33039 * g + 6416 * b + 32768) >> 16) + 16)
343 /* RGB to V(Cr) Color transform */
344 #define TO_V(r, g, b) \
345 (((28784 * r - 24103 * g - 4681 * b + 32768) >> 16) + 128)
346 /* RGB to U(Cb) Color transform */
347 #define TO_U(r, g, b) \
348 (((-9714 * r - 19070 * g + 28784 * b + 32768) >> 16) + 128)
350 #define TSTAMP_MIN_Y 24
351 #define TSTAMP_MAX_Y (TSTAMP_MIN_Y + 15)
352 #define TSTAMP_INPUT_X 10
353 #define TSTAMP_MIN_X (54 + TSTAMP_INPUT_X)
355 static void gen_twopix(struct vivi_fh
*fh
, unsigned char *buf
, int colorpos
)
357 unsigned char r_y
, g_u
, b_v
;
361 r_y
= fh
->bars
[colorpos
][0]; /* R or precalculated Y */
362 g_u
= fh
->bars
[colorpos
][1]; /* G or precalculated U */
363 b_v
= fh
->bars
[colorpos
][2]; /* B or precalculated V */
365 for (color
= 0; color
< 4; color
++) {
368 switch (fh
->fmt
->fourcc
) {
369 case V4L2_PIX_FMT_YUYV
:
383 case V4L2_PIX_FMT_UYVY
:
397 case V4L2_PIX_FMT_RGB565
:
401 *p
= (g_u
<< 5) | b_v
;
405 *p
= (r_y
<< 3) | (g_u
>> 3);
409 case V4L2_PIX_FMT_RGB565X
:
413 *p
= (r_y
<< 3) | (g_u
>> 3);
417 *p
= (g_u
<< 5) | b_v
;
421 case V4L2_PIX_FMT_RGB555
:
425 *p
= (g_u
<< 5) | b_v
;
429 *p
= (r_y
<< 2) | (g_u
>> 3);
433 case V4L2_PIX_FMT_RGB555X
:
437 *p
= (r_y
<< 2) | (g_u
>> 3);
441 *p
= (g_u
<< 5) | b_v
;
449 static void gen_line(struct vivi_fh
*fh
, char *basep
, int inipos
, int wmax
,
450 int hmax
, int line
, int count
, char *timestr
)
457 /* We will just duplicate the second pixel at the packet */
460 /* Generate a standard color bar pattern */
461 for (w
= 0; w
< wmax
; w
++) {
462 int colorpos
= ((w
+ count
) * 8/(wmax
+ 1)) % 8;
464 gen_twopix(fh
, basep
+ pos
, colorpos
);
465 pos
+= 4; /* only 16 bpp supported for now */
468 /* Prints input entry number */
470 /* Checks if it is possible to input number */
471 if (TSTAMP_MAX_Y
>= hmax
)
474 if (TSTAMP_INPUT_X
+ strlen(timestr
) >= wmax
)
477 if (line
>= TSTAMP_MIN_Y
&& line
<= TSTAMP_MAX_Y
) {
478 chr
= rom8x16_bits
[fh
->input
* 16 + line
- TSTAMP_MIN_Y
];
479 pos
= TSTAMP_INPUT_X
;
480 for (i
= 0; i
< 7; i
++) {
481 /* Draw white font on black background */
482 if (chr
& 1 << (7 - i
))
483 gen_twopix(fh
, basep
+ pos
, WHITE
);
485 gen_twopix(fh
, basep
+ pos
, BLACK
);
490 /* Checks if it is possible to show timestamp */
491 if (TSTAMP_MIN_X
+ strlen(timestr
) >= wmax
)
494 /* Print stream time */
495 if (line
>= TSTAMP_MIN_Y
&& line
<= TSTAMP_MAX_Y
) {
497 for (s
= timestr
; *s
; s
++) {
498 chr
= rom8x16_bits
[(*s
-0x30)*16+line
-TSTAMP_MIN_Y
];
499 for (i
= 0; i
< 7; i
++) {
500 pos
= inipos
+ j
* 2;
501 /* Draw white font on black background */
502 if (chr
& 1 << (7 - i
))
503 gen_twopix(fh
, basep
+ pos
, WHITE
);
505 gen_twopix(fh
, basep
+ pos
, BLACK
);
515 static void vivi_fillbuff(struct vivi_fh
*fh
, struct vivi_buffer
*buf
)
517 struct vivi_dev
*dev
= fh
->dev
;
519 int hmax
= buf
->vb
.height
;
520 int wmax
= buf
->vb
.width
;
523 void *vbuf
= videobuf_to_vmalloc(&buf
->vb
);
528 tmpbuf
= kmalloc(wmax
* 2, GFP_ATOMIC
);
532 for (h
= 0; h
< hmax
; h
++) {
533 gen_line(fh
, tmpbuf
, 0, wmax
, hmax
, h
, dev
->mv_count
,
535 memcpy(vbuf
+ pos
, tmpbuf
, wmax
* 2);
543 /* Updates stream time */
545 dev
->ms
+= jiffies_to_msecs(jiffies
-dev
->jiffies
);
546 dev
->jiffies
= jiffies
;
547 if (dev
->ms
>= 1000) {
561 sprintf(dev
->timestr
, "%02d:%02d:%02d:%03d",
562 dev
->h
, dev
->m
, dev
->s
, dev
->ms
);
564 dprintk(dev
, 2, "vivifill at %s: Buffer 0x%08lx size= %d\n",
565 dev
->timestr
, (unsigned long)tmpbuf
, pos
);
567 /* Advice that buffer was filled */
568 buf
->vb
.field_count
++;
569 do_gettimeofday(&ts
);
571 buf
->vb
.state
= VIDEOBUF_DONE
;
574 static void vivi_thread_tick(struct vivi_fh
*fh
)
576 struct vivi_buffer
*buf
;
577 struct vivi_dev
*dev
= fh
->dev
;
578 struct vivi_dmaqueue
*dma_q
= &dev
->vidq
;
580 unsigned long flags
= 0;
582 dprintk(dev
, 1, "Thread tick\n");
584 spin_lock_irqsave(&dev
->slock
, flags
);
585 if (list_empty(&dma_q
->active
)) {
586 dprintk(dev
, 1, "No active queue to serve\n");
590 buf
= list_entry(dma_q
->active
.next
,
591 struct vivi_buffer
, vb
.queue
);
593 /* Nobody is waiting on this buffer, return */
594 if (!waitqueue_active(&buf
->vb
.done
))
597 list_del(&buf
->vb
.queue
);
599 do_gettimeofday(&buf
->vb
.ts
);
602 vivi_fillbuff(fh
, buf
);
603 dprintk(dev
, 1, "filled buffer %p\n", buf
);
605 wake_up(&buf
->vb
.done
);
606 dprintk(dev
, 2, "[%p/%d] wakeup\n", buf
, buf
->vb
. i
);
608 spin_unlock_irqrestore(&dev
->slock
, flags
);
612 #define frames_to_ms(frames) \
613 ((frames * WAKE_NUMERATOR * 1000) / WAKE_DENOMINATOR)
615 static void vivi_sleep(struct vivi_fh
*fh
)
617 struct vivi_dev
*dev
= fh
->dev
;
618 struct vivi_dmaqueue
*dma_q
= &dev
->vidq
;
620 DECLARE_WAITQUEUE(wait
, current
);
622 dprintk(dev
, 1, "%s dma_q=0x%08lx\n", __func__
,
623 (unsigned long)dma_q
);
625 add_wait_queue(&dma_q
->wq
, &wait
);
626 if (kthread_should_stop())
629 /* Calculate time to wake up */
630 timeout
= msecs_to_jiffies(frames_to_ms(1));
632 vivi_thread_tick(fh
);
634 schedule_timeout_interruptible(timeout
);
637 remove_wait_queue(&dma_q
->wq
, &wait
);
641 static int vivi_thread(void *data
)
643 struct vivi_fh
*fh
= data
;
644 struct vivi_dev
*dev
= fh
->dev
;
646 dprintk(dev
, 1, "thread started\n");
653 if (kthread_should_stop())
656 dprintk(dev
, 1, "thread: exit\n");
660 static int vivi_start_thread(struct vivi_fh
*fh
)
662 struct vivi_dev
*dev
= fh
->dev
;
663 struct vivi_dmaqueue
*dma_q
= &dev
->vidq
;
666 dma_q
->ini_jiffies
= jiffies
;
668 dprintk(dev
, 1, "%s\n", __func__
);
670 dma_q
->kthread
= kthread_run(vivi_thread
, fh
, "vivi");
672 if (IS_ERR(dma_q
->kthread
)) {
673 v4l2_err(&dev
->v4l2_dev
, "kernel_thread() failed\n");
674 return PTR_ERR(dma_q
->kthread
);
677 wake_up_interruptible(&dma_q
->wq
);
679 dprintk(dev
, 1, "returning from %s\n", __func__
);
683 static void vivi_stop_thread(struct vivi_dmaqueue
*dma_q
)
685 struct vivi_dev
*dev
= container_of(dma_q
, struct vivi_dev
, vidq
);
687 dprintk(dev
, 1, "%s\n", __func__
);
688 /* shutdown control thread */
689 if (dma_q
->kthread
) {
690 kthread_stop(dma_q
->kthread
);
691 dma_q
->kthread
= NULL
;
695 /* ------------------------------------------------------------------
697 ------------------------------------------------------------------*/
699 buffer_setup(struct videobuf_queue
*vq
, unsigned int *count
, unsigned int *size
)
701 struct vivi_fh
*fh
= vq
->priv_data
;
702 struct vivi_dev
*dev
= fh
->dev
;
704 *size
= fh
->width
*fh
->height
*2;
709 while (*size
* *count
> vid_limit
* 1024 * 1024)
712 dprintk(dev
, 1, "%s, count=%d, size=%d\n", __func__
,
718 static void free_buffer(struct videobuf_queue
*vq
, struct vivi_buffer
*buf
)
720 struct vivi_fh
*fh
= vq
->priv_data
;
721 struct vivi_dev
*dev
= fh
->dev
;
723 dprintk(dev
, 1, "%s, state: %i\n", __func__
, buf
->vb
.state
);
728 videobuf_vmalloc_free(&buf
->vb
);
729 dprintk(dev
, 1, "free_buffer: freed\n");
730 buf
->vb
.state
= VIDEOBUF_NEEDS_INIT
;
733 #define norm_maxw() 1024
734 #define norm_maxh() 768
736 buffer_prepare(struct videobuf_queue
*vq
, struct videobuf_buffer
*vb
,
737 enum v4l2_field field
)
739 struct vivi_fh
*fh
= vq
->priv_data
;
740 struct vivi_dev
*dev
= fh
->dev
;
741 struct vivi_buffer
*buf
= container_of(vb
, struct vivi_buffer
, vb
);
744 dprintk(dev
, 1, "%s, field=%d\n", __func__
, field
);
746 BUG_ON(NULL
== fh
->fmt
);
748 if (fh
->width
< 48 || fh
->width
> norm_maxw() ||
749 fh
->height
< 32 || fh
->height
> norm_maxh())
752 buf
->vb
.size
= fh
->width
*fh
->height
*2;
753 if (0 != buf
->vb
.baddr
&& buf
->vb
.bsize
< buf
->vb
.size
)
756 /* These properties only change when queue is idle, see s_fmt */
758 buf
->vb
.width
= fh
->width
;
759 buf
->vb
.height
= fh
->height
;
760 buf
->vb
.field
= field
;
762 if (VIDEOBUF_NEEDS_INIT
== buf
->vb
.state
) {
763 rc
= videobuf_iolock(vq
, &buf
->vb
, NULL
);
768 buf
->vb
.state
= VIDEOBUF_PREPARED
;
773 free_buffer(vq
, buf
);
778 buffer_queue(struct videobuf_queue
*vq
, struct videobuf_buffer
*vb
)
780 struct vivi_buffer
*buf
= container_of(vb
, struct vivi_buffer
, vb
);
781 struct vivi_fh
*fh
= vq
->priv_data
;
782 struct vivi_dev
*dev
= fh
->dev
;
783 struct vivi_dmaqueue
*vidq
= &dev
->vidq
;
785 dprintk(dev
, 1, "%s\n", __func__
);
787 buf
->vb
.state
= VIDEOBUF_QUEUED
;
788 list_add_tail(&buf
->vb
.queue
, &vidq
->active
);
791 static void buffer_release(struct videobuf_queue
*vq
,
792 struct videobuf_buffer
*vb
)
794 struct vivi_buffer
*buf
= container_of(vb
, struct vivi_buffer
, vb
);
795 struct vivi_fh
*fh
= vq
->priv_data
;
796 struct vivi_dev
*dev
= (struct vivi_dev
*)fh
->dev
;
798 dprintk(dev
, 1, "%s\n", __func__
);
800 free_buffer(vq
, buf
);
803 static struct videobuf_queue_ops vivi_video_qops
= {
804 .buf_setup
= buffer_setup
,
805 .buf_prepare
= buffer_prepare
,
806 .buf_queue
= buffer_queue
,
807 .buf_release
= buffer_release
,
810 /* ------------------------------------------------------------------
811 IOCTL vidioc handling
812 ------------------------------------------------------------------*/
813 static int vidioc_querycap(struct file
*file
, void *priv
,
814 struct v4l2_capability
*cap
)
816 struct vivi_fh
*fh
= priv
;
817 struct vivi_dev
*dev
= fh
->dev
;
819 strcpy(cap
->driver
, "vivi");
820 strcpy(cap
->card
, "vivi");
821 strlcpy(cap
->bus_info
, dev
->v4l2_dev
.name
, sizeof(cap
->bus_info
));
822 cap
->version
= VIVI_VERSION
;
823 cap
->capabilities
= V4L2_CAP_VIDEO_CAPTURE
|
829 static int vidioc_enum_fmt_vid_cap(struct file
*file
, void *priv
,
830 struct v4l2_fmtdesc
*f
)
832 struct vivi_fmt
*fmt
;
834 if (f
->index
>= ARRAY_SIZE(formats
))
837 fmt
= &formats
[f
->index
];
839 strlcpy(f
->description
, fmt
->name
, sizeof(f
->description
));
840 f
->pixelformat
= fmt
->fourcc
;
844 static int vidioc_g_fmt_vid_cap(struct file
*file
, void *priv
,
845 struct v4l2_format
*f
)
847 struct vivi_fh
*fh
= priv
;
849 f
->fmt
.pix
.width
= fh
->width
;
850 f
->fmt
.pix
.height
= fh
->height
;
851 f
->fmt
.pix
.field
= fh
->vb_vidq
.field
;
852 f
->fmt
.pix
.pixelformat
= fh
->fmt
->fourcc
;
853 f
->fmt
.pix
.bytesperline
=
854 (f
->fmt
.pix
.width
* fh
->fmt
->depth
) >> 3;
855 f
->fmt
.pix
.sizeimage
=
856 f
->fmt
.pix
.height
* f
->fmt
.pix
.bytesperline
;
861 static int vidioc_try_fmt_vid_cap(struct file
*file
, void *priv
,
862 struct v4l2_format
*f
)
864 struct vivi_fh
*fh
= priv
;
865 struct vivi_dev
*dev
= fh
->dev
;
866 struct vivi_fmt
*fmt
;
867 enum v4l2_field field
;
868 unsigned int maxw
, maxh
;
872 dprintk(dev
, 1, "Fourcc format (0x%08x) invalid.\n",
873 f
->fmt
.pix
.pixelformat
);
877 field
= f
->fmt
.pix
.field
;
879 if (field
== V4L2_FIELD_ANY
) {
880 field
= V4L2_FIELD_INTERLACED
;
881 } else if (V4L2_FIELD_INTERLACED
!= field
) {
882 dprintk(dev
, 1, "Field type invalid.\n");
889 f
->fmt
.pix
.field
= field
;
890 if (f
->fmt
.pix
.height
< 32)
891 f
->fmt
.pix
.height
= 32;
892 if (f
->fmt
.pix
.height
> maxh
)
893 f
->fmt
.pix
.height
= maxh
;
894 if (f
->fmt
.pix
.width
< 48)
895 f
->fmt
.pix
.width
= 48;
896 if (f
->fmt
.pix
.width
> maxw
)
897 f
->fmt
.pix
.width
= maxw
;
898 f
->fmt
.pix
.width
&= ~0x03;
899 f
->fmt
.pix
.bytesperline
=
900 (f
->fmt
.pix
.width
* fmt
->depth
) >> 3;
901 f
->fmt
.pix
.sizeimage
=
902 f
->fmt
.pix
.height
* f
->fmt
.pix
.bytesperline
;
907 /* precalculate color bar values to speed up rendering */
908 static void precalculate_bars(struct vivi_fh
*fh
)
910 struct vivi_dev
*dev
= fh
->dev
;
911 unsigned char r
, g
, b
;
914 fh
->input
= dev
->input
;
916 for (k
= 0; k
< 8; k
++) {
917 r
= bars
[fh
->input
].bar
[k
][0];
918 g
= bars
[fh
->input
].bar
[k
][1];
919 b
= bars
[fh
->input
].bar
[k
][2];
922 switch (fh
->fmt
->fourcc
) {
923 case V4L2_PIX_FMT_YUYV
:
924 case V4L2_PIX_FMT_UYVY
:
927 case V4L2_PIX_FMT_RGB565
:
928 case V4L2_PIX_FMT_RGB565X
:
933 case V4L2_PIX_FMT_RGB555
:
934 case V4L2_PIX_FMT_RGB555X
:
942 fh
->bars
[k
][0] = TO_Y(r
, g
, b
); /* Luma */
943 fh
->bars
[k
][1] = TO_U(r
, g
, b
); /* Cb */
944 fh
->bars
[k
][2] = TO_V(r
, g
, b
); /* Cr */
954 /*FIXME: This seems to be generic enough to be at videodev2 */
955 static int vidioc_s_fmt_vid_cap(struct file
*file
, void *priv
,
956 struct v4l2_format
*f
)
958 struct vivi_fh
*fh
= priv
;
959 struct videobuf_queue
*q
= &fh
->vb_vidq
;
961 int ret
= vidioc_try_fmt_vid_cap(file
, fh
, f
);
965 mutex_lock(&q
->vb_lock
);
967 if (videobuf_queue_is_busy(&fh
->vb_vidq
)) {
968 dprintk(fh
->dev
, 1, "%s queue busy\n", __func__
);
973 fh
->fmt
= get_format(f
);
974 fh
->width
= f
->fmt
.pix
.width
;
975 fh
->height
= f
->fmt
.pix
.height
;
976 fh
->vb_vidq
.field
= f
->fmt
.pix
.field
;
979 precalculate_bars(fh
);
983 mutex_unlock(&q
->vb_lock
);
988 static int vidioc_reqbufs(struct file
*file
, void *priv
,
989 struct v4l2_requestbuffers
*p
)
991 struct vivi_fh
*fh
= priv
;
993 return (videobuf_reqbufs(&fh
->vb_vidq
, p
));
996 static int vidioc_querybuf(struct file
*file
, void *priv
, struct v4l2_buffer
*p
)
998 struct vivi_fh
*fh
= priv
;
1000 return (videobuf_querybuf(&fh
->vb_vidq
, p
));
1003 static int vidioc_qbuf(struct file
*file
, void *priv
, struct v4l2_buffer
*p
)
1005 struct vivi_fh
*fh
= priv
;
1007 return (videobuf_qbuf(&fh
->vb_vidq
, p
));
1010 static int vidioc_dqbuf(struct file
*file
, void *priv
, struct v4l2_buffer
*p
)
1012 struct vivi_fh
*fh
= priv
;
1014 return (videobuf_dqbuf(&fh
->vb_vidq
, p
,
1015 file
->f_flags
& O_NONBLOCK
));
1018 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1019 static int vidiocgmbuf(struct file
*file
, void *priv
, struct video_mbuf
*mbuf
)
1021 struct vivi_fh
*fh
= priv
;
1023 return videobuf_cgmbuf(&fh
->vb_vidq
, mbuf
, 8);
1027 static int vidioc_streamon(struct file
*file
, void *priv
, enum v4l2_buf_type i
)
1029 struct vivi_fh
*fh
= priv
;
1031 if (fh
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1036 return videobuf_streamon(&fh
->vb_vidq
);
1039 static int vidioc_streamoff(struct file
*file
, void *priv
, enum v4l2_buf_type i
)
1041 struct vivi_fh
*fh
= priv
;
1043 if (fh
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1048 return videobuf_streamoff(&fh
->vb_vidq
);
1051 static int vidioc_s_std(struct file
*file
, void *priv
, v4l2_std_id
*i
)
1056 /* only one input in this sample driver */
1057 static int vidioc_enum_input(struct file
*file
, void *priv
,
1058 struct v4l2_input
*inp
)
1060 if (inp
->index
>= NUM_INPUTS
)
1063 inp
->type
= V4L2_INPUT_TYPE_CAMERA
;
1064 inp
->std
= V4L2_STD_525_60
;
1065 sprintf(inp
->name
, "Camera %u", inp
->index
);
1070 static int vidioc_g_input(struct file
*file
, void *priv
, unsigned int *i
)
1072 struct vivi_fh
*fh
= priv
;
1073 struct vivi_dev
*dev
= fh
->dev
;
1079 static int vidioc_s_input(struct file
*file
, void *priv
, unsigned int i
)
1081 struct vivi_fh
*fh
= priv
;
1082 struct vivi_dev
*dev
= fh
->dev
;
1084 if (i
>= NUM_INPUTS
)
1088 precalculate_bars(fh
);
1093 /* --- controls ---------------------------------------------- */
1094 static int vidioc_queryctrl(struct file
*file
, void *priv
,
1095 struct v4l2_queryctrl
*qc
)
1099 for (i
= 0; i
< ARRAY_SIZE(vivi_qctrl
); i
++)
1100 if (qc
->id
&& qc
->id
== vivi_qctrl
[i
].id
) {
1101 memcpy(qc
, &(vivi_qctrl
[i
]),
1109 static int vidioc_g_ctrl(struct file
*file
, void *priv
,
1110 struct v4l2_control
*ctrl
)
1112 struct vivi_fh
*fh
= priv
;
1113 struct vivi_dev
*dev
= fh
->dev
;
1116 for (i
= 0; i
< ARRAY_SIZE(vivi_qctrl
); i
++)
1117 if (ctrl
->id
== vivi_qctrl
[i
].id
) {
1118 ctrl
->value
= dev
->qctl_regs
[i
];
1124 static int vidioc_s_ctrl(struct file
*file
, void *priv
,
1125 struct v4l2_control
*ctrl
)
1127 struct vivi_fh
*fh
= priv
;
1128 struct vivi_dev
*dev
= fh
->dev
;
1131 for (i
= 0; i
< ARRAY_SIZE(vivi_qctrl
); i
++)
1132 if (ctrl
->id
== vivi_qctrl
[i
].id
) {
1133 if (ctrl
->value
< vivi_qctrl
[i
].minimum
||
1134 ctrl
->value
> vivi_qctrl
[i
].maximum
) {
1137 dev
->qctl_regs
[i
] = ctrl
->value
;
1143 /* ------------------------------------------------------------------
1144 File operations for the device
1145 ------------------------------------------------------------------*/
1147 static int vivi_open(struct file
*file
)
1149 struct vivi_dev
*dev
= video_drvdata(file
);
1150 struct vivi_fh
*fh
= NULL
;
1153 mutex_lock(&dev
->mutex
);
1156 if (dev
->users
> 1) {
1158 mutex_unlock(&dev
->mutex
);
1162 dprintk(dev
, 1, "open /dev/video%d type=%s users=%d\n", dev
->vfd
->num
,
1163 v4l2_type_names
[V4L2_BUF_TYPE_VIDEO_CAPTURE
], dev
->users
);
1165 /* allocate + initialize per filehandle data */
1166 fh
= kzalloc(sizeof(*fh
), GFP_KERNEL
);
1171 mutex_unlock(&dev
->mutex
);
1176 file
->private_data
= fh
;
1179 fh
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1180 fh
->fmt
= &formats
[0];
1184 /* Resets frame counters */
1190 dev
->jiffies
= jiffies
;
1191 sprintf(dev
->timestr
, "%02d:%02d:%02d:%03d",
1192 dev
->h
, dev
->m
, dev
->s
, dev
->ms
);
1194 videobuf_queue_vmalloc_init(&fh
->vb_vidq
, &vivi_video_qops
,
1195 NULL
, &dev
->slock
, fh
->type
, V4L2_FIELD_INTERLACED
,
1196 sizeof(struct vivi_buffer
), fh
);
1198 vivi_start_thread(fh
);
1204 vivi_read(struct file
*file
, char __user
*data
, size_t count
, loff_t
*ppos
)
1206 struct vivi_fh
*fh
= file
->private_data
;
1208 if (fh
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE
) {
1209 return videobuf_read_stream(&fh
->vb_vidq
, data
, count
, ppos
, 0,
1210 file
->f_flags
& O_NONBLOCK
);
1216 vivi_poll(struct file
*file
, struct poll_table_struct
*wait
)
1218 struct vivi_fh
*fh
= file
->private_data
;
1219 struct vivi_dev
*dev
= fh
->dev
;
1220 struct videobuf_queue
*q
= &fh
->vb_vidq
;
1222 dprintk(dev
, 1, "%s\n", __func__
);
1224 if (V4L2_BUF_TYPE_VIDEO_CAPTURE
!= fh
->type
)
1227 return videobuf_poll_stream(file
, q
, wait
);
1230 static int vivi_close(struct file
*file
)
1232 struct vivi_fh
*fh
= file
->private_data
;
1233 struct vivi_dev
*dev
= fh
->dev
;
1234 struct vivi_dmaqueue
*vidq
= &dev
->vidq
;
1236 int minor
= video_devdata(file
)->minor
;
1238 vivi_stop_thread(vidq
);
1239 videobuf_stop(&fh
->vb_vidq
);
1240 videobuf_mmap_free(&fh
->vb_vidq
);
1244 mutex_lock(&dev
->mutex
);
1246 mutex_unlock(&dev
->mutex
);
1248 dprintk(dev
, 1, "close called (minor=%d, users=%d)\n",
1254 static int vivi_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1256 struct vivi_fh
*fh
= file
->private_data
;
1257 struct vivi_dev
*dev
= fh
->dev
;
1260 dprintk(dev
, 1, "mmap called, vma=0x%08lx\n", (unsigned long)vma
);
1262 ret
= videobuf_mmap_mapper(&fh
->vb_vidq
, vma
);
1264 dprintk(dev
, 1, "vma start=0x%08lx, size=%ld, ret=%d\n",
1265 (unsigned long)vma
->vm_start
,
1266 (unsigned long)vma
->vm_end
-(unsigned long)vma
->vm_start
,
1272 static const struct v4l2_file_operations vivi_fops
= {
1273 .owner
= THIS_MODULE
,
1275 .release
= vivi_close
,
1278 .ioctl
= video_ioctl2
, /* V4L2 ioctl handler */
1282 static const struct v4l2_ioctl_ops vivi_ioctl_ops
= {
1283 .vidioc_querycap
= vidioc_querycap
,
1284 .vidioc_enum_fmt_vid_cap
= vidioc_enum_fmt_vid_cap
,
1285 .vidioc_g_fmt_vid_cap
= vidioc_g_fmt_vid_cap
,
1286 .vidioc_try_fmt_vid_cap
= vidioc_try_fmt_vid_cap
,
1287 .vidioc_s_fmt_vid_cap
= vidioc_s_fmt_vid_cap
,
1288 .vidioc_reqbufs
= vidioc_reqbufs
,
1289 .vidioc_querybuf
= vidioc_querybuf
,
1290 .vidioc_qbuf
= vidioc_qbuf
,
1291 .vidioc_dqbuf
= vidioc_dqbuf
,
1292 .vidioc_s_std
= vidioc_s_std
,
1293 .vidioc_enum_input
= vidioc_enum_input
,
1294 .vidioc_g_input
= vidioc_g_input
,
1295 .vidioc_s_input
= vidioc_s_input
,
1296 .vidioc_queryctrl
= vidioc_queryctrl
,
1297 .vidioc_g_ctrl
= vidioc_g_ctrl
,
1298 .vidioc_s_ctrl
= vidioc_s_ctrl
,
1299 .vidioc_streamon
= vidioc_streamon
,
1300 .vidioc_streamoff
= vidioc_streamoff
,
1301 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1302 .vidiocgmbuf
= vidiocgmbuf
,
1306 static struct video_device vivi_template
= {
1309 .ioctl_ops
= &vivi_ioctl_ops
,
1311 .release
= video_device_release
,
1313 .tvnorms
= V4L2_STD_525_60
,
1314 .current_norm
= V4L2_STD_NTSC_M
,
1317 /* -----------------------------------------------------------------
1318 Initialization and module stuff
1319 ------------------------------------------------------------------*/
1321 static int vivi_release(void)
1323 struct vivi_dev
*dev
;
1324 struct list_head
*list
;
1326 while (!list_empty(&vivi_devlist
)) {
1327 list
= vivi_devlist
.next
;
1329 dev
= list_entry(list
, struct vivi_dev
, vivi_devlist
);
1331 v4l2_info(&dev
->v4l2_dev
, "unregistering /dev/video%d\n",
1333 video_unregister_device(dev
->vfd
);
1334 v4l2_device_unregister(&dev
->v4l2_dev
);
1341 static int __init
vivi_create_instance(int inst
)
1343 struct vivi_dev
*dev
;
1344 struct video_device
*vfd
;
1347 dev
= kzalloc(sizeof(*dev
), GFP_KERNEL
);
1351 snprintf(dev
->v4l2_dev
.name
, sizeof(dev
->v4l2_dev
.name
),
1352 "%s-%03d", VIVI_MODULE_NAME
, inst
);
1353 ret
= v4l2_device_register(NULL
, &dev
->v4l2_dev
);
1357 /* init video dma queues */
1358 INIT_LIST_HEAD(&dev
->vidq
.active
);
1359 init_waitqueue_head(&dev
->vidq
.wq
);
1361 /* initialize locks */
1362 spin_lock_init(&dev
->slock
);
1363 mutex_init(&dev
->mutex
);
1366 vfd
= video_device_alloc();
1370 *vfd
= vivi_template
;
1372 ret
= video_register_device(vfd
, VFL_TYPE_GRABBER
, video_nr
);
1376 video_set_drvdata(vfd
, dev
);
1378 /* Set all controls to their default value. */
1379 for (i
= 0; i
< ARRAY_SIZE(vivi_qctrl
); i
++)
1380 dev
->qctl_regs
[i
] = vivi_qctrl
[i
].default_value
;
1382 /* Now that everything is fine, let's add it to device list */
1383 list_add_tail(&dev
->vivi_devlist
, &vivi_devlist
);
1385 snprintf(vfd
->name
, sizeof(vfd
->name
), "%s (%i)",
1386 vivi_template
.name
, vfd
->num
);
1392 v4l2_info(&dev
->v4l2_dev
, "V4L2 device registered as /dev/video%d\n",
1397 video_device_release(vfd
);
1399 v4l2_device_unregister(&dev
->v4l2_dev
);
1405 /* This routine allocates from 1 to n_devs virtual drivers.
1407 The real maximum number of virtual drivers will depend on how many drivers
1408 will succeed. This is limited to the maximum number of devices that
1409 videodev supports, which is equal to VIDEO_NUM_DEVICES.
1411 static int __init
vivi_init(void)
1418 for (i
= 0; i
< n_devs
; i
++) {
1419 ret
= vivi_create_instance(i
);
1421 /* If some instantiations succeeded, keep driver */
1429 printk(KERN_INFO
"Error %d while loading vivi driver\n", ret
);
1433 printk(KERN_INFO
"Video Technology Magazine Virtual Video "
1434 "Capture Board ver %u.%u.%u successfully loaded.\n",
1435 (VIVI_VERSION
>> 16) & 0xFF, (VIVI_VERSION
>> 8) & 0xFF,
1436 VIVI_VERSION
& 0xFF);
1438 /* n_devs will reflect the actual number of allocated devices */
1444 static void __exit
vivi_exit(void)
1449 module_init(vivi_init
);
1450 module_exit(vivi_exit
);