1 #include <linux/module.h>
2 #include <linux/kernel.h>
3 #include <linux/mutex.h>
5 #include <linux/slab.h>
6 #include <asm/uaccess.h>
7 #include <linux/videodev2.h>
8 #include <media/videobuf2-v4l2.h>
9 #include <media/videobuf2-vmalloc.h>
10 #include <media/v4l2-dev.h>
11 #include <media/v4l2-ioctl.h>
12 #include <media/v4l2-device.h>
13 #include <media/v4l2-ctrls.h>
14 #include <media/v4l2-event.h>
21 static int init(void);
22 static void shutdown(void);
24 static int ioctl_querycap(struct file *file, void *fh, struct v4l2_capability *cap);
25 static int ioctl_enum_fmt(struct file *file, void *fh, struct v4l2_fmtdesc *fdesc);
26 static int ioctl_get_fmt(struct file *file, void *fh, struct v4l2_format *fmt);
27 static int ioctl_set_fmt(struct file *file, void *fh, struct v4l2_format *fmt);
28 static int ioctl_get_parm(struct file *file, void *fh, struct v4l2_streamparm *fmt);
29 static int ioctl_set_parm(struct file *file, void *fh, struct v4l2_streamparm *fmt);
30 static int ioctl_queryctl(struct file *file, void *fh, struct v4l2_queryctrl *ctl);
32 static int queue_setup(struct vb2_queue *vbq, unsigned int *nbuf,
33 unsigned int *nplanes, unsigned int *sizes, void **alloc_ctx);
34 static int buf_prepare(struct vb2_buffer *vb);
35 static void buf_queue(struct vb2_buffer *vb);
36 static int start_streaming(struct vb2_queue *vbq, unsigned int count);
37 static void stop_streaming(struct vb2_queue *vbq);
40 static int update_frame(int xsz, int ysz);
41 static int gen_frame(void);
45 module_exit(shutdown);
47 MODULE_LICENSE("GPL");
48 MODULE_AUTHOR("John Tsiombikas");
49 MODULE_DESCRIPTION("v4l2 test module");
51 static struct mutex mutex;
52 static struct video_device *vdev;
54 static struct v4l2_device v4l2_dev;
55 static struct v4l2_file_operations fops;
56 static struct v4l2_ioctl_ops iops;
57 static struct vb2_queue vbq;
58 static struct vb2_ops vbops;
59 static int width, height;
60 static unsigned char *frame;
61 static int frame_size;
64 static spinlock_t blist_lock;
65 static struct buffer *buflist, *buflist_tail;
73 strcpy(v4l2_dev.name, "vdummy");
74 if((res = v4l2_device_register(0, &v4l2_dev)) != 0) {
79 spin_lock_init(&blist_lock);
81 vbops.queue_setup = queue_setup;
82 vbops.buf_prepare = buf_prepare;
83 vbops.buf_queue = buf_queue;
84 vbops.start_streaming = start_streaming;
85 vbops.stop_streaming = stop_streaming;
86 vbops.wait_prepare = vb2_ops_wait_prepare;
87 vbops.wait_finish = vb2_ops_wait_finish;
89 vbq.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
90 vbq.io_modes = VB2_MMAP | VB2_READ;
91 vbq.buf_struct_size = sizeof *buflist;
93 vbq.mem_ops = &vb2_vmalloc_memops;
94 vbq.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
95 vbq.min_buffers_needed = 1;
97 vbq.gfp_flags = GFP_KERNEL;
99 if((res = vb2_queue_init(&vbq)) != 0) {
100 printk(KERN_ALERT "vdummy: failed to initialize videobuf2 queue\n");
101 v4l2_device_unregister(&v4l2_dev);
105 if(!(vdev = video_device_alloc())) {
108 memset(vdev, 0, sizeof *vdev);
109 vdev->release = video_device_release;
110 strcpy(vdev->name, KBUILD_MODNAME);
112 vdev->ioctl_ops = &iops;
113 vdev->vfl_type = VFL_TYPE_GRABBER;
114 vdev->vfl_dir = VFL_DIR_RX;
115 vdev->v4l2_dev = &v4l2_dev;
119 fops.owner = THIS_MODULE;
120 fops.open = v4l2_fh_open;
121 fops.release = vb2_fop_release;
122 fops.read = vb2_fop_read;
123 fops.mmap = vb2_fop_mmap;
124 fops.poll = vb2_fop_poll;
125 fops.unlocked_ioctl = video_ioctl2;
127 iops.vidioc_querycap = ioctl_querycap;
128 iops.vidioc_enum_fmt_vid_cap = ioctl_enum_fmt;
129 iops.vidioc_g_fmt_vid_cap = ioctl_get_fmt;
130 iops.vidioc_s_fmt_vid_cap = ioctl_set_fmt;
131 iops.vidioc_g_parm = ioctl_get_parm;
132 iops.vidioc_s_parm = ioctl_set_parm;
133 iops.vidioc_queryctrl = ioctl_queryctl;
134 iops.vidioc_reqbufs = vb2_ioctl_reqbufs;
135 iops.vidioc_create_bufs = vb2_ioctl_create_bufs;
136 iops.vidioc_querybuf = vb2_ioctl_querybuf;
137 iops.vidioc_qbuf = vb2_ioctl_qbuf;
138 iops.vidioc_dqbuf = vb2_ioctl_dqbuf;
139 iops.vidioc_expbuf = vb2_ioctl_expbuf;
140 iops.vidioc_streamon = vb2_ioctl_streamon;
141 iops.vidioc_streamoff = vb2_ioctl_streamoff;
142 iops.vidioc_log_status = v4l2_ctrl_log_status;
143 iops.vidioc_subscribe_event = v4l2_ctrl_subscribe_event;
144 iops.vidioc_unsubscribe_event = v4l2_event_unsubscribe;
146 if((res = video_register_device(vdev, VFL_TYPE_GRABBER, -1)) != 0) {
147 printk(KERN_ALERT "vdummy: failed to register video device\n");
148 v4l2_device_unregister(&v4l2_dev);
149 video_device_release(vdev);
153 if((res = update_frame(640, 480)) != 0) {
154 video_unregister_device(vdev);
155 v4l2_device_unregister(&v4l2_dev);
156 video_device_release(vdev);
160 printk(KERN_INFO "vdummy device initialized\n");
164 static void shutdown(void)
166 video_unregister_device(vdev);
167 v4l2_device_unregister(&v4l2_dev);
171 static int ioctl_querycap(struct file *file, void *fh, struct v4l2_capability *cap)
173 strcpy(cap->driver, KBUILD_MODNAME);
174 strcpy(cap->card, "dummy v4l2 dev");
175 strcpy(cap->bus_info, "nobus");
176 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
177 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
181 static int ioctl_enum_fmt(struct file *file, void *fh, struct v4l2_fmtdesc *fdesc)
183 if(fdesc->index != 0 || fdesc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
187 strcpy(fdesc->description, "RGB24");
188 fdesc->pixelformat = V4L2_PIX_FMT_RGB24;
192 static int ioctl_get_fmt(struct file *file, void *fh, struct v4l2_format *fmt)
194 if(fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
197 fmt->fmt.pix.width = width;
198 fmt->fmt.pix.height = height;
199 fmt->fmt.pix.pixelformat = V4L2_PIX_FMT_RGB24;
200 fmt->fmt.pix.field = V4L2_FIELD_NONE;
201 fmt->fmt.pix.bytesperline = 0;
202 fmt->fmt.pix.sizeimage = frame_size;
203 fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SRGB;
204 fmt->fmt.pix.priv = 0;
205 fmt->fmt.pix.flags = 0;
206 fmt->fmt.pix.ycbcr_enc = 0;
207 fmt->fmt.pix.quantization = V4L2_QUANTIZATION_FULL_RANGE;
208 fmt->fmt.pix.xfer_func = V4L2_XFER_FUNC_NONE;
212 static int ioctl_set_fmt(struct file *file, void *fh, struct v4l2_format *fmt)
216 if(fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
219 if((res = update_frame(fmt->fmt.pix.width, fmt->fmt.pix.height)) != 0) {
222 return ioctl_get_fmt(file, fh, fmt);
225 static int ioctl_get_parm(struct file *file, void *fh, struct v4l2_streamparm *parm)
227 if(parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
230 parm->parm.capture.capability = 0;
231 parm->parm.capture.capturemode = 0;
232 parm->parm.capture.timeperframe.numerator = 1;
233 parm->parm.capture.timeperframe.denominator = 30;
234 parm->parm.capture.extendedmode = 0;
235 parm->parm.capture.readbuffers = 1;
239 static int ioctl_set_parm(struct file *file, void *fh, struct v4l2_streamparm *parm)
241 if(parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
244 return ioctl_get_parm(file, fh, parm);
247 static int ioctl_queryctl(struct file *file, void *fh, struct v4l2_queryctrl *ctl)
252 static int queue_setup(struct vb2_queue *vbq, unsigned int *nbuf,
253 unsigned int *nplanes, unsigned int *sizes, void **alloc_ctx)
255 if(vbq->num_buffers + *nbuf < 2) {
256 *nbuf = 2 - vbq->num_buffers;
260 sizes[0] = frame_size;
265 static int buf_prepare(struct vb2_buffer *vb)
267 if(vb2_plane_size(vb, 0) < frame_size) {
268 printk(KERN_ALERT "vdummy: buffer too small\n");
271 vb2_set_plane_payload(vb, 0, frame_size);
275 static void buf_queue(struct vb2_buffer *vb)
277 struct buffer *buf = (struct buffer*)((char*)vb - offsetof(struct buffer, vb));
279 spin_lock(&blist_lock);
281 buflist_tail->next = buf;
284 buflist = buflist_tail = buf;
287 spin_unlock(&blist_lock);
290 static void clear_queue(enum vb2_buffer_state st)
292 spin_lock(&blist_lock);
294 struct buffer *buf = buflist;
295 buflist = buflist->next;
296 vb2_buffer_done(&buf->vb, st);
298 buflist = buflist_tail = 0;
299 spin_unlock(&blist_lock);
302 static int start_streaming(struct vb2_queue *vbq, unsigned int count)
311 static void stop_streaming(struct vb2_queue *vbq)
314 clear_queue(VB2_BUF_STATE_ERROR);
317 static int update_frame(int xsz, int ysz)
321 int new_fsz = xsz * ysz * 3;
323 if(xsz == width && ysz == height) {
327 printk(KERN_INFO "vdummy: update_frame(%d, %d)\n", xsz, ysz);
329 if(!frame || frame_size < new_fsz) {
330 if(!(tmp = kmalloc(new_fsz, GFP_KERNEL))) {
334 frame_size = new_fsz;
341 for(i=0; i<height; i++) {
342 for(j=0; j<width; j++) {
345 *tmp++ = (val << 1) & 0xff;
346 *tmp++ = (val << 2) & 0xff;
353 static int gen_frame(void)
355 struct buffer *buf = 0;
357 spin_lock(&blist_lock);
360 buflist = buflist->next;
361 if(!buflist) buflist_tail = 0;
363 spin_unlock(&blist_lock);
366 memcpy(vb2_plane_vaddr(&buf->vb, 0), frame, frame_size);
368 /*v4l2_get_timestamp(&buf->vb.v4l2_buf.timestamp);
369 buf->vb.v4l2_buf.sequence = seqno++;
370 buf->vb.v4l2_buf.field = V4L2_FIELD_NONE;*/
371 vb2_buffer_done(&buf->vb, VB2_BUF_STATE_DONE);