1 #include <linux/module.h>
2 #include <linux/kernel.h>
3 #include <linux/mutex.h>
5 #include <linux/slab.h>
6 #include <linux/hrtimer.h>
7 #include <linux/ktime.h>
8 #include <asm/uaccess.h>
9 #include <linux/videodev2.h>
10 #include <media/videobuf2-v4l2.h>
11 #include <media/videobuf2-vmalloc.h>
12 #include <media/v4l2-dev.h>
13 #include <media/v4l2-ioctl.h>
14 #include <media/v4l2-device.h>
15 #include <media/v4l2-ctrls.h>
16 #include <media/v4l2-event.h>
19 struct vb2_v4l2_buffer vb;
23 static int init(void);
24 static void shutdown(void);
26 static int ioctl_querycap(struct file *file, void *fh, struct v4l2_capability *cap);
27 static int ioctl_enum_fmt(struct file *file, void *fh, struct v4l2_fmtdesc *fdesc);
28 static int ioctl_get_fmt(struct file *file, void *fh, struct v4l2_format *fmt);
29 static int ioctl_set_fmt(struct file *file, void *fh, struct v4l2_format *fmt);
30 static int ioctl_try_fmt(struct file *file, void *fh, struct v4l2_format *fmt);
31 static int ioctl_get_parm(struct file *file, void *fh, struct v4l2_streamparm *fmt);
32 static int ioctl_set_parm(struct file *file, void *fh, struct v4l2_streamparm *fmt);
33 static int ioctl_queryctl(struct file *file, void *fh, struct v4l2_queryctrl *ctl);
35 static int queue_setup(struct vb2_queue *vbq, unsigned int *nbuf,
36 unsigned int *nplanes, unsigned int *sizes, void **alloc_ctx);
37 static int buf_prepare(struct vb2_buffer *vb);
38 static void buf_queue(struct vb2_buffer *vb);
39 static int start_streaming(struct vb2_queue *vbq, unsigned int count);
40 static void stop_streaming(struct vb2_queue *vbq);
43 static int update_frame(int xsz, int ysz);
44 static int gen_frame(void);
45 enum hrtimer_restart timer_func(struct hrtimer *timer);
49 module_exit(shutdown);
51 MODULE_LICENSE("GPL");
52 MODULE_AUTHOR("John Tsiombikas");
53 MODULE_DESCRIPTION("v4l2 test module");
55 static struct mutex mutex;
56 static struct video_device *vdev;
58 static struct v4l2_device v4l2_dev;
59 static struct v4l2_file_operations fops;
60 static struct v4l2_ioctl_ops iops;
61 static struct vb2_queue vbq;
62 static struct vb2_ops vbops;
63 static int width, height;
64 static unsigned char *frame;
65 static int frame_size;
68 static spinlock_t blist_lock;
69 static struct buffer *buflist, *buflist_tail;
72 static int framerate = 1;
73 static struct hrtimer timer;
79 strcpy(v4l2_dev.name, "vdummy");
80 if((res = v4l2_device_register(0, &v4l2_dev)) != 0) {
85 spin_lock_init(&blist_lock);
87 vbops.queue_setup = queue_setup;
88 vbops.buf_prepare = buf_prepare;
89 vbops.buf_queue = buf_queue;
90 vbops.start_streaming = start_streaming;
91 vbops.stop_streaming = stop_streaming;
92 vbops.wait_prepare = vb2_ops_wait_prepare;
93 vbops.wait_finish = vb2_ops_wait_finish;
95 vbq.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
96 vbq.io_modes = VB2_MMAP | VB2_READ;
97 vbq.buf_struct_size = sizeof *buflist;
99 vbq.mem_ops = &vb2_vmalloc_memops;
100 vbq.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
101 vbq.min_buffers_needed = 2;
103 vbq.gfp_flags = GFP_KERNEL;
105 if((res = vb2_queue_init(&vbq)) != 0) {
106 printk(KERN_ALERT "vdummy: failed to initialize videobuf2 queue\n");
107 v4l2_device_unregister(&v4l2_dev);
111 if(!(vdev = video_device_alloc())) {
114 memset(vdev, 0, sizeof *vdev);
115 vdev->release = video_device_release;
116 strcpy(vdev->name, KBUILD_MODNAME);
118 vdev->ioctl_ops = &iops;
119 vdev->vfl_type = VFL_TYPE_GRABBER;
120 vdev->vfl_dir = VFL_DIR_RX;
121 vdev->v4l2_dev = &v4l2_dev;
125 fops.owner = THIS_MODULE;
126 fops.open = v4l2_fh_open;
127 fops.release = vb2_fop_release;
128 fops.read = vb2_fop_read;
129 fops.mmap = vb2_fop_mmap;
130 fops.poll = vb2_fop_poll;
131 fops.unlocked_ioctl = video_ioctl2;
133 iops.vidioc_querycap = ioctl_querycap;
134 iops.vidioc_enum_fmt_vid_cap = ioctl_enum_fmt;
135 iops.vidioc_g_fmt_vid_cap = ioctl_get_fmt;
136 iops.vidioc_s_fmt_vid_cap = ioctl_set_fmt;
137 iops.vidioc_try_fmt_vid_cap = ioctl_try_fmt;
138 iops.vidioc_g_parm = ioctl_get_parm;
139 iops.vidioc_s_parm = ioctl_set_parm;
140 iops.vidioc_queryctrl = ioctl_queryctl;
141 iops.vidioc_reqbufs = vb2_ioctl_reqbufs;
142 iops.vidioc_create_bufs = vb2_ioctl_create_bufs;
143 iops.vidioc_querybuf = vb2_ioctl_querybuf;
144 iops.vidioc_qbuf = vb2_ioctl_qbuf;
145 iops.vidioc_dqbuf = vb2_ioctl_dqbuf;
146 iops.vidioc_expbuf = vb2_ioctl_expbuf;
147 iops.vidioc_streamon = vb2_ioctl_streamon;
148 iops.vidioc_streamoff = vb2_ioctl_streamoff;
149 iops.vidioc_log_status = v4l2_ctrl_log_status;
150 iops.vidioc_subscribe_event = v4l2_ctrl_subscribe_event;
151 iops.vidioc_unsubscribe_event = v4l2_event_unsubscribe;
153 if((res = video_register_device(vdev, VFL_TYPE_GRABBER, -1)) != 0) {
154 printk(KERN_ALERT "vdummy: failed to register video device\n");
155 v4l2_device_unregister(&v4l2_dev);
156 video_device_release(vdev);
160 hrtimer_init(&timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
161 timer.function = timer_func;
163 if((res = update_frame(640, 480)) != 0) {
164 video_unregister_device(vdev);
165 v4l2_device_unregister(&v4l2_dev);
166 video_device_release(vdev);
170 printk(KERN_INFO "vdummy device initialized\n");
174 static void shutdown(void)
176 video_unregister_device(vdev);
177 v4l2_device_unregister(&v4l2_dev);
181 static int ioctl_querycap(struct file *file, void *fh, struct v4l2_capability *cap)
183 strcpy(cap->driver, KBUILD_MODNAME);
184 strcpy(cap->card, "Test V4L2 device");
185 strcpy(cap->bus_info, "nobus");
186 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
187 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
191 static int ioctl_enum_fmt(struct file *file, void *fh, struct v4l2_fmtdesc *fdesc)
193 if(fdesc->index != 0 || fdesc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
197 strcpy(fdesc->description, "RGB24");
198 fdesc->pixelformat = V4L2_PIX_FMT_RGB24;
202 static int ioctl_get_fmt(struct file *file, void *fh, struct v4l2_format *fmt)
204 if(fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
207 fmt->fmt.pix.width = width;
208 fmt->fmt.pix.height = height;
209 fmt->fmt.pix.pixelformat = V4L2_PIX_FMT_RGB24;
210 fmt->fmt.pix.field = V4L2_FIELD_NONE;
211 fmt->fmt.pix.bytesperline = 0;
212 fmt->fmt.pix.sizeimage = frame_size;
213 fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SRGB;
214 fmt->fmt.pix.priv = 0;
215 fmt->fmt.pix.flags = 0;
216 fmt->fmt.pix.ycbcr_enc = 0;
217 fmt->fmt.pix.quantization = V4L2_QUANTIZATION_FULL_RANGE;
218 fmt->fmt.pix.xfer_func = V4L2_XFER_FUNC_NONE;
222 static int ioctl_set_fmt(struct file *file, void *fh, struct v4l2_format *fmt)
226 if(fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
229 if((res = update_frame(fmt->fmt.pix.width, fmt->fmt.pix.height)) != 0) {
232 return ioctl_get_fmt(file, fh, fmt);
235 static int ioctl_try_fmt(struct file *file, void *fh, struct v4l2_format *fmt)
237 if(fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
240 if(fmt->fmt.pix.width <= 0)
241 fmt->fmt.pix.width = width;
242 if(fmt->fmt.pix.height <= 0)
243 fmt->fmt.pix.height = height;
244 fmt->fmt.pix.pixelformat = V4L2_PIX_FMT_RGB24;
245 fmt->fmt.pix.field = V4L2_FIELD_NONE;
246 fmt->fmt.pix.bytesperline = 0;
247 fmt->fmt.pix.sizeimage = frame_size;
248 fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SRGB;
249 fmt->fmt.pix.priv = 0;
250 fmt->fmt.pix.flags = 0;
251 fmt->fmt.pix.ycbcr_enc = 0;
252 fmt->fmt.pix.quantization = V4L2_QUANTIZATION_FULL_RANGE;
253 fmt->fmt.pix.xfer_func = V4L2_XFER_FUNC_NONE;
257 static int ioctl_get_parm(struct file *file, void *fh, struct v4l2_streamparm *parm)
259 if(parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
262 parm->parm.capture.capability = 0;
263 parm->parm.capture.capturemode = 0;
264 parm->parm.capture.timeperframe.numerator = 1;
265 parm->parm.capture.timeperframe.denominator = framerate;
266 parm->parm.capture.extendedmode = 0;
267 parm->parm.capture.readbuffers = 1;
271 static int ioctl_set_parm(struct file *file, void *fh, struct v4l2_streamparm *parm)
274 if(parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
278 new_framerate = parm->parm.capture.timeperframe.denominator / parm->parm.capture.timeperframe.numerator;
279 framerate = new_framerate < 1 ? 1 : (new_framerate > 60 ? 60 : new_framerate);
281 return ioctl_get_parm(file, fh, parm);
284 static int ioctl_queryctl(struct file *file, void *fh, struct v4l2_queryctrl *ctl)
289 static int queue_setup(struct vb2_queue *vbq, unsigned int *nbuf,
290 unsigned int *nplanes, unsigned int *sizes, void **alloc_ctx)
292 if(vbq->num_buffers + *nbuf < 2) {
293 *nbuf = 2 - vbq->num_buffers;
297 sizes[0] = frame_size;
302 static int buf_prepare(struct vb2_buffer *vb)
304 if(vb2_plane_size(vb, 0) < frame_size) {
305 printk(KERN_ALERT "vdummy: buffer too small\n");
311 static void buf_queue(struct vb2_buffer *vb)
313 struct buffer *buf = (struct buffer*)((char*)vb - offsetof(struct buffer, vb));
315 spin_lock(&blist_lock);
317 buflist_tail->next = buf;
320 buflist = buflist_tail = buf;
323 spin_unlock(&blist_lock);
326 static void clear_queue(enum vb2_buffer_state st)
328 spin_lock(&blist_lock);
330 struct buffer *buf = buflist;
331 buflist = buflist->next;
332 vb2_buffer_done(&buf->vb.vb2_buf, st);
334 buflist = buflist_tail = 0;
335 spin_unlock(&blist_lock);
338 static int start_streaming(struct vb2_queue *vbq, unsigned int count)
340 ktime_t frame_interval;
345 /* calculate frame interval in nanoseconds */
347 frame_interval = ktime_set(1, 0);
349 frame_interval = ktime_set(0, 1000000000ul / (unsigned long)framerate);
352 hrtimer_start(&timer, frame_interval, HRTIMER_MODE_REL);
356 static void stop_streaming(struct vb2_queue *vbq)
358 while(hrtimer_try_to_cancel(&timer) == -1);
361 clear_queue(VB2_BUF_STATE_ERROR);
364 static int update_frame(int xsz, int ysz)
368 int new_fsz = xsz * ysz * 3;
370 if(xsz == width && ysz == height) {
374 printk(KERN_INFO "vdummy: update_frame(%d, %d)\n", xsz, ysz);
376 if(!frame || frame_size < new_fsz) {
377 if(!(tmp = kmalloc(new_fsz, GFP_KERNEL))) {
381 frame_size = new_fsz;
388 for(i=0; i<height; i++) {
389 for(j=0; j<width; j++) {
392 *tmp++ = (val << 1) & 0xff;
393 *tmp++ = (val << 2) & 0xff;
400 static int gen_frame(void)
402 struct buffer *buf = 0;
404 spin_lock(&blist_lock);
407 buflist = buflist->next;
408 if(!buflist) buflist_tail = 0;
410 spin_unlock(&blist_lock);
413 printk(KERN_INFO "vdummy: copying frame: %d\n", seqno);
415 memcpy(vb2_plane_vaddr(&buf->vb.vb2_buf, 0), frame, frame_size);
417 buf->vb.vb2_buf.timestamp = ktime_get_ns();
418 buf->vb.sequence = seqno++;
419 buf->vb.field = V4L2_FIELD_NONE;
420 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_DONE);
421 vb2_set_plane_payload(&buf->vb.vb2_buf, 0, frame_size);
426 enum hrtimer_restart timer_func(struct hrtimer *timer)
429 return streaming ? HRTIMER_RESTART : HRTIMER_NORESTART;