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 <linux/version.h>
9 #include <asm/uaccess.h>
10 #include <linux/videodev2.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_enum_framesz(struct file *file, void *fh, struct v4l2_frmsizeenum *fsz);
29 static int ioctl_get_fmt(struct file *file, void *fh, struct v4l2_format *fmt);
30 static int ioctl_set_fmt(struct file *file, void *fh, struct v4l2_format *fmt);
31 static int ioctl_try_fmt(struct file *file, void *fh, struct v4l2_format *fmt);
32 static int ioctl_get_parm(struct file *file, void *fh, struct v4l2_streamparm *fmt);
33 static int ioctl_set_parm(struct file *file, void *fh, struct v4l2_streamparm *fmt);
34 static int ioctl_queryctl(struct file *file, void *fh, struct v4l2_queryctrl *ctl);
36 /* TODO find out exact version where this switched over */
37 #if LINUX_VERSION_CODE < KERNEL_VERSION(4,7,0)
38 #define ALLOC_CTX_TYPE void
40 #define ALLOC_CTX_TYPE struct device
43 static int queue_setup(struct vb2_queue *vbq, unsigned int *nbuf,
44 unsigned int *nplanes, unsigned int *sizes, ALLOC_CTX_TYPE **alloc_ctx);
45 static int buf_prepare(struct vb2_buffer *vb);
46 static void buf_queue(struct vb2_buffer *vb);
47 static int start_streaming(struct vb2_queue *vbq, unsigned int count);
48 static void stop_streaming(struct vb2_queue *vbq);
51 static int update_frame(int xsz, int ysz);
52 static int gen_frame(void);
53 enum hrtimer_restart timer_func(struct hrtimer *timer);
57 module_exit(shutdown);
59 MODULE_LICENSE("GPL");
60 MODULE_AUTHOR("John Tsiombikas");
61 MODULE_DESCRIPTION("Dummy V4L2 device");
63 static struct mutex mutex;
64 static struct video_device *vdev;
66 static struct v4l2_device v4l2_dev;
67 static struct v4l2_file_operations fops;
68 static struct v4l2_ioctl_ops iops;
69 static struct vb2_queue vbq;
70 static struct vb2_ops vbops;
71 static int width, height;
72 static unsigned char *frame;
73 static int frame_size;
76 static spinlock_t blist_lock;
77 static struct buffer *buflist, *buflist_tail;
80 static int framerate = 1;
81 static struct hrtimer timer;
87 strcpy(v4l2_dev.name, "vdummy");
88 if((res = v4l2_device_register(0, &v4l2_dev)) != 0) {
93 spin_lock_init(&blist_lock);
95 vbops.queue_setup = queue_setup;
96 vbops.buf_prepare = buf_prepare;
97 vbops.buf_queue = buf_queue;
98 vbops.start_streaming = start_streaming;
99 vbops.stop_streaming = stop_streaming;
100 vbops.wait_prepare = vb2_ops_wait_prepare;
101 vbops.wait_finish = vb2_ops_wait_finish;
103 vbq.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
104 vbq.io_modes = VB2_MMAP | VB2_READ;
105 vbq.buf_struct_size = sizeof *buflist;
107 vbq.mem_ops = &vb2_vmalloc_memops;
108 vbq.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
109 vbq.min_buffers_needed = 2;
111 vbq.gfp_flags = GFP_KERNEL;
113 if((res = vb2_queue_init(&vbq)) != 0) {
114 printk(KERN_ALERT "vdummy: failed to initialize videobuf2 queue\n");
115 v4l2_device_unregister(&v4l2_dev);
119 if(!(vdev = video_device_alloc())) {
122 memset(vdev, 0, sizeof *vdev);
123 vdev->release = video_device_release;
124 strcpy(vdev->name, KBUILD_MODNAME);
126 vdev->ioctl_ops = &iops;
127 vdev->vfl_type = VFL_TYPE_GRABBER;
128 vdev->vfl_dir = VFL_DIR_RX;
129 vdev->v4l2_dev = &v4l2_dev;
133 fops.owner = THIS_MODULE;
134 fops.open = v4l2_fh_open;
135 fops.release = vb2_fop_release;
136 fops.read = vb2_fop_read;
137 fops.mmap = vb2_fop_mmap;
138 fops.poll = vb2_fop_poll;
139 fops.unlocked_ioctl = video_ioctl2;
141 iops.vidioc_querycap = ioctl_querycap;
142 iops.vidioc_enum_fmt_vid_cap = ioctl_enum_fmt;
143 iops.vidioc_enum_framesizes = ioctl_enum_framesz;
144 iops.vidioc_g_fmt_vid_cap = ioctl_get_fmt;
145 iops.vidioc_s_fmt_vid_cap = ioctl_set_fmt;
146 iops.vidioc_try_fmt_vid_cap = ioctl_try_fmt;
147 iops.vidioc_g_parm = ioctl_get_parm;
148 iops.vidioc_s_parm = ioctl_set_parm;
149 iops.vidioc_queryctrl = ioctl_queryctl;
150 iops.vidioc_reqbufs = vb2_ioctl_reqbufs;
151 iops.vidioc_create_bufs = vb2_ioctl_create_bufs;
152 iops.vidioc_querybuf = vb2_ioctl_querybuf;
153 iops.vidioc_qbuf = vb2_ioctl_qbuf;
154 iops.vidioc_dqbuf = vb2_ioctl_dqbuf;
155 iops.vidioc_expbuf = vb2_ioctl_expbuf;
156 iops.vidioc_streamon = vb2_ioctl_streamon;
157 iops.vidioc_streamoff = vb2_ioctl_streamoff;
158 iops.vidioc_log_status = v4l2_ctrl_log_status;
159 iops.vidioc_subscribe_event = v4l2_ctrl_subscribe_event;
160 iops.vidioc_unsubscribe_event = v4l2_event_unsubscribe;
162 if((res = video_register_device(vdev, VFL_TYPE_GRABBER, -1)) != 0) {
163 printk(KERN_ALERT "vdummy: failed to register video device\n");
164 v4l2_device_unregister(&v4l2_dev);
165 video_device_release(vdev);
169 hrtimer_init(&timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
170 timer.function = timer_func;
172 if((res = update_frame(640, 480)) != 0) {
173 video_unregister_device(vdev);
174 v4l2_device_unregister(&v4l2_dev);
175 video_device_release(vdev);
179 printk(KERN_INFO "vdummy: initialized\n");
183 static void shutdown(void)
185 hrtimer_cancel(&timer);
186 video_unregister_device(vdev);
187 v4l2_device_unregister(&v4l2_dev);
191 static int ioctl_querycap(struct file *file, void *fh, struct v4l2_capability *cap)
193 strcpy(cap->driver, KBUILD_MODNAME);
194 strcpy(cap->card, "Dummy V4L2 device");
195 strcpy(cap->bus_info, "nobus");
196 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
197 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
201 static int ioctl_enum_fmt(struct file *file, void *fh, struct v4l2_fmtdesc *fdesc)
203 if(fdesc->index != 0 || fdesc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
207 strcpy(fdesc->description, "RGB24");
208 fdesc->pixelformat = V4L2_PIX_FMT_RGB24;
212 static int ioctl_enum_framesz(struct file *file, void *fh, struct v4l2_frmsizeenum *fsz)
214 if(fsz->index != 0) {
217 fsz->type = V4L2_FRMSIZE_TYPE_CONTINUOUS;
218 fsz->stepwise.min_width = fsz->stepwise.min_height = 1;
219 fsz->stepwise.max_width = fsz->stepwise.max_height = 8192;
220 fsz->stepwise.step_width = fsz->stepwise.step_height = 1;
224 static int ioctl_get_fmt(struct file *file, void *fh, struct v4l2_format *fmt)
226 if(fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
229 fmt->fmt.pix.width = width;
230 fmt->fmt.pix.height = height;
231 fmt->fmt.pix.pixelformat = V4L2_PIX_FMT_RGB24;
232 fmt->fmt.pix.field = V4L2_FIELD_NONE;
233 fmt->fmt.pix.bytesperline = 0;
234 fmt->fmt.pix.sizeimage = frame_size;
235 fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SRGB;
236 fmt->fmt.pix.priv = 0;
237 fmt->fmt.pix.flags = 0;
238 fmt->fmt.pix.ycbcr_enc = 0;
239 fmt->fmt.pix.quantization = V4L2_QUANTIZATION_FULL_RANGE;
240 fmt->fmt.pix.xfer_func = V4L2_XFER_FUNC_NONE;
244 static int ioctl_set_fmt(struct file *file, void *fh, struct v4l2_format *fmt)
248 if(fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
251 if((res = update_frame(fmt->fmt.pix.width, fmt->fmt.pix.height)) != 0) {
254 return ioctl_get_fmt(file, fh, fmt);
257 static int ioctl_try_fmt(struct file *file, void *fh, struct v4l2_format *fmt)
259 if(fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
262 if(fmt->fmt.pix.width <= 0)
263 fmt->fmt.pix.width = width;
264 if(fmt->fmt.pix.height <= 0)
265 fmt->fmt.pix.height = height;
266 fmt->fmt.pix.pixelformat = V4L2_PIX_FMT_RGB24;
267 fmt->fmt.pix.field = V4L2_FIELD_NONE;
268 fmt->fmt.pix.bytesperline = 0;
269 fmt->fmt.pix.sizeimage = frame_size;
270 fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SRGB;
271 fmt->fmt.pix.priv = 0;
272 fmt->fmt.pix.flags = 0;
273 fmt->fmt.pix.ycbcr_enc = 0;
274 fmt->fmt.pix.quantization = V4L2_QUANTIZATION_FULL_RANGE;
275 fmt->fmt.pix.xfer_func = V4L2_XFER_FUNC_NONE;
279 static int ioctl_get_parm(struct file *file, void *fh, struct v4l2_streamparm *parm)
281 if(parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
284 parm->parm.capture.capability = 0;
285 parm->parm.capture.capturemode = 0;
286 parm->parm.capture.timeperframe.numerator = 1;
287 parm->parm.capture.timeperframe.denominator = framerate;
288 parm->parm.capture.extendedmode = 0;
289 parm->parm.capture.readbuffers = 1;
293 static int ioctl_set_parm(struct file *file, void *fh, struct v4l2_streamparm *parm)
296 if(parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
300 new_framerate = parm->parm.capture.timeperframe.denominator / parm->parm.capture.timeperframe.numerator;
301 framerate = new_framerate < 1 ? 1 : (new_framerate > 60 ? 60 : new_framerate);
303 return ioctl_get_parm(file, fh, parm);
306 static int ioctl_queryctl(struct file *file, void *fh, struct v4l2_queryctrl *ctl)
311 static int queue_setup(struct vb2_queue *vbq, unsigned int *nbuf,
312 unsigned int *nplanes, unsigned int *sizes, ALLOC_CTX_TYPE **alloc_ctx)
314 if(vbq->num_buffers + *nbuf < 2) {
315 *nbuf = 2 - vbq->num_buffers;
319 sizes[0] = frame_size;
324 static int buf_prepare(struct vb2_buffer *vb)
326 if(vb2_plane_size(vb, 0) < frame_size) {
327 printk(KERN_ALERT "vdummy: buffer too small\n");
333 static void buf_queue(struct vb2_buffer *vb)
335 struct buffer *buf = (struct buffer*)((char*)vb - offsetof(struct buffer, vb));
337 spin_lock(&blist_lock);
338 printk(KERN_INFO "vdummy: enqueue buffer\n");
340 buflist_tail->next = buf;
343 buflist = buflist_tail = buf;
346 spin_unlock(&blist_lock);
349 static void clear_queue(enum vb2_buffer_state st)
353 printk(KERN_INFO "vdummy: clear_queue\n");
355 spin_lock(&blist_lock);
357 buflist = buflist_tail = 0;
358 spin_unlock(&blist_lock);
361 struct buffer *buf = list;
363 vb2_buffer_done(&buf->vb.vb2_buf, st);
367 static int start_streaming(struct vb2_queue *vbq, unsigned int count)
369 ktime_t frame_interval;
374 /* calculate frame interval in nanoseconds */
376 frame_interval = ktime_set(1, 0);
378 frame_interval = ktime_set(0, 1000000000ul / (unsigned long)framerate);
381 printk(KERN_INFO "vdummy: start streaming (interval: %ld ms)\n",
382 (long)ktime_to_ms(frame_interval));
383 hrtimer_start(&timer, frame_interval, HRTIMER_MODE_REL);
387 static void stop_streaming(struct vb2_queue *vbq)
389 printk(KERN_INFO "vdummy: stop streaming\n");
390 hrtimer_cancel(&timer);
393 clear_queue(VB2_BUF_STATE_ERROR);
396 static int update_frame(int xsz, int ysz)
400 int new_fsz = xsz * ysz * 3;
402 if(xsz == width && ysz == height) {
406 printk(KERN_INFO "vdummy: update_frame(%d, %d)\n", xsz, ysz);
408 if(!frame || frame_size < new_fsz) {
409 if(!(tmp = kmalloc(new_fsz, GFP_KERNEL))) {
413 frame_size = new_fsz;
420 for(i=0; i<height; i++) {
421 for(j=0; j<width; j++) {
424 *tmp++ = (val << 1) & 0xff;
425 *tmp++ = (val << 2) & 0xff;
432 static int gen_frame(void)
434 struct buffer *buf = 0;
437 printk(KERN_INFO "vdummy: gen_frame\n");
439 spin_lock_irqsave(&blist_lock, flags);
442 buflist = buflist->next;
443 if(!buflist) buflist_tail = 0;
445 spin_unlock_irqrestore(&blist_lock, flags);
448 memcpy(vb2_plane_vaddr(&buf->vb.vb2_buf, 0), frame, frame_size);
450 buf->vb.vb2_buf.timestamp = ktime_get_ns();
451 printk(KERN_INFO "vdummy: copying frame: %d ts: %lu\n", seqno,
452 (unsigned long)buf->vb.vb2_buf.timestamp);
453 buf->vb.sequence = seqno++;
454 buf->vb.field = V4L2_FIELD_NONE;
455 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_DONE);
456 vb2_set_plane_payload(&buf->vb.vb2_buf, 0, frame_size);
461 enum hrtimer_restart timer_func(struct hrtimer *timer)
463 printk(KERN_INFO "vdummy: timer!\n");
465 return streaming ? HRTIMER_RESTART : HRTIMER_NORESTART;