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-vmalloc.h>
11 #include <media/v4l2-dev.h>
12 #include <media/v4l2-ioctl.h>
13 #include <media/v4l2-device.h>
14 #include <media/v4l2-ctrls.h>
15 #include <media/v4l2-event.h>
18 struct vb2_v4l2_buffer vb;
22 static int init(void);
23 static void shutdown(void);
25 static int ioctl_querycap(struct file *file, void *fh, struct v4l2_capability *cap);
26 static int ioctl_enum_fmt(struct file *file, void *fh, struct v4l2_fmtdesc *fdesc);
27 static int ioctl_enum_framesz(struct file *file, void *fh, struct v4l2_frmsizeenum *fsz);
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, struct device **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("Dummy V4L2 device");
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_enum_framesizes = ioctl_enum_framesz;
136 iops.vidioc_g_fmt_vid_cap = ioctl_get_fmt;
137 iops.vidioc_s_fmt_vid_cap = ioctl_set_fmt;
138 iops.vidioc_try_fmt_vid_cap = ioctl_try_fmt;
139 iops.vidioc_g_parm = ioctl_get_parm;
140 iops.vidioc_s_parm = ioctl_set_parm;
141 iops.vidioc_queryctrl = ioctl_queryctl;
142 iops.vidioc_reqbufs = vb2_ioctl_reqbufs;
143 iops.vidioc_create_bufs = vb2_ioctl_create_bufs;
144 iops.vidioc_querybuf = vb2_ioctl_querybuf;
145 iops.vidioc_qbuf = vb2_ioctl_qbuf;
146 iops.vidioc_dqbuf = vb2_ioctl_dqbuf;
147 iops.vidioc_expbuf = vb2_ioctl_expbuf;
148 iops.vidioc_streamon = vb2_ioctl_streamon;
149 iops.vidioc_streamoff = vb2_ioctl_streamoff;
150 iops.vidioc_log_status = v4l2_ctrl_log_status;
151 iops.vidioc_subscribe_event = v4l2_ctrl_subscribe_event;
152 iops.vidioc_unsubscribe_event = v4l2_event_unsubscribe;
154 if((res = video_register_device(vdev, VFL_TYPE_GRABBER, -1)) != 0) {
155 printk(KERN_ALERT "vdummy: failed to register video device\n");
156 v4l2_device_unregister(&v4l2_dev);
157 video_device_release(vdev);
161 hrtimer_init(&timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
162 timer.function = timer_func;
164 if((res = update_frame(640, 480)) != 0) {
165 video_unregister_device(vdev);
166 v4l2_device_unregister(&v4l2_dev);
167 video_device_release(vdev);
171 printk(KERN_INFO "vdummy: initialized\n");
175 static void shutdown(void)
177 hrtimer_cancel(&timer);
178 video_unregister_device(vdev);
179 v4l2_device_unregister(&v4l2_dev);
183 static int ioctl_querycap(struct file *file, void *fh, struct v4l2_capability *cap)
185 strcpy(cap->driver, KBUILD_MODNAME);
186 strcpy(cap->card, "Dummy V4L2 device");
187 strcpy(cap->bus_info, "nobus");
188 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
189 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
193 static int ioctl_enum_fmt(struct file *file, void *fh, struct v4l2_fmtdesc *fdesc)
195 if(fdesc->index != 0 || fdesc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
199 strcpy(fdesc->description, "RGB24");
200 fdesc->pixelformat = V4L2_PIX_FMT_RGB24;
204 static int ioctl_enum_framesz(struct file *file, void *fh, struct v4l2_frmsizeenum *fsz)
206 if(fsz->index != 0) {
209 fsz->type = V4L2_FRMSIZE_TYPE_CONTINUOUS;
210 fsz->stepwise.min_width = fsz->stepwise.min_height = 1;
211 fsz->stepwise.max_width = fsz->stepwise.max_height = 8192;
212 fsz->stepwise.step_width = fsz->stepwise.step_height = 1;
216 static int ioctl_get_fmt(struct file *file, void *fh, struct v4l2_format *fmt)
218 if(fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
221 fmt->fmt.pix.width = width;
222 fmt->fmt.pix.height = height;
223 fmt->fmt.pix.pixelformat = V4L2_PIX_FMT_RGB24;
224 fmt->fmt.pix.field = V4L2_FIELD_NONE;
225 fmt->fmt.pix.bytesperline = 0;
226 fmt->fmt.pix.sizeimage = frame_size;
227 fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SRGB;
228 fmt->fmt.pix.priv = 0;
229 fmt->fmt.pix.flags = 0;
230 fmt->fmt.pix.ycbcr_enc = 0;
231 fmt->fmt.pix.quantization = V4L2_QUANTIZATION_FULL_RANGE;
232 fmt->fmt.pix.xfer_func = V4L2_XFER_FUNC_NONE;
236 static int ioctl_set_fmt(struct file *file, void *fh, struct v4l2_format *fmt)
240 if(fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
243 if((res = update_frame(fmt->fmt.pix.width, fmt->fmt.pix.height)) != 0) {
246 return ioctl_get_fmt(file, fh, fmt);
249 static int ioctl_try_fmt(struct file *file, void *fh, struct v4l2_format *fmt)
251 if(fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
254 if(fmt->fmt.pix.width <= 0)
255 fmt->fmt.pix.width = width;
256 if(fmt->fmt.pix.height <= 0)
257 fmt->fmt.pix.height = height;
258 fmt->fmt.pix.pixelformat = V4L2_PIX_FMT_RGB24;
259 fmt->fmt.pix.field = V4L2_FIELD_NONE;
260 fmt->fmt.pix.bytesperline = 0;
261 fmt->fmt.pix.sizeimage = frame_size;
262 fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SRGB;
263 fmt->fmt.pix.priv = 0;
264 fmt->fmt.pix.flags = 0;
265 fmt->fmt.pix.ycbcr_enc = 0;
266 fmt->fmt.pix.quantization = V4L2_QUANTIZATION_FULL_RANGE;
267 fmt->fmt.pix.xfer_func = V4L2_XFER_FUNC_NONE;
271 static int ioctl_get_parm(struct file *file, void *fh, struct v4l2_streamparm *parm)
273 if(parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
276 parm->parm.capture.capability = 0;
277 parm->parm.capture.capturemode = 0;
278 parm->parm.capture.timeperframe.numerator = 1;
279 parm->parm.capture.timeperframe.denominator = framerate;
280 parm->parm.capture.extendedmode = 0;
281 parm->parm.capture.readbuffers = 1;
285 static int ioctl_set_parm(struct file *file, void *fh, struct v4l2_streamparm *parm)
288 if(parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
292 new_framerate = parm->parm.capture.timeperframe.denominator / parm->parm.capture.timeperframe.numerator;
293 framerate = new_framerate < 1 ? 1 : (new_framerate > 60 ? 60 : new_framerate);
295 return ioctl_get_parm(file, fh, parm);
298 static int ioctl_queryctl(struct file *file, void *fh, struct v4l2_queryctrl *ctl)
303 static int queue_setup(struct vb2_queue *vbq, unsigned int *nbuf,
304 unsigned int *nplanes, unsigned int *sizes, struct device **alloc_ctx)
306 if(vbq->num_buffers + *nbuf < 2) {
307 *nbuf = 2 - vbq->num_buffers;
311 sizes[0] = frame_size;
316 static int buf_prepare(struct vb2_buffer *vb)
318 if(vb2_plane_size(vb, 0) < frame_size) {
319 printk(KERN_ALERT "vdummy: buffer too small\n");
325 static void buf_queue(struct vb2_buffer *vb)
327 struct buffer *buf = (struct buffer*)((char*)vb - offsetof(struct buffer, vb));
329 spin_lock(&blist_lock);
330 printk(KERN_INFO "vdummy: enqueue buffer\n");
332 buflist_tail->next = buf;
335 buflist = buflist_tail = buf;
338 spin_unlock(&blist_lock);
341 static void clear_queue(enum vb2_buffer_state st)
345 printk(KERN_INFO "vdummy: clear_queue\n");
347 spin_lock(&blist_lock);
349 buflist = buflist_tail = 0;
350 spin_unlock(&blist_lock);
353 struct buffer *buf = list;
355 vb2_buffer_done(&buf->vb.vb2_buf, st);
359 static int start_streaming(struct vb2_queue *vbq, unsigned int count)
361 ktime_t frame_interval;
366 /* calculate frame interval in nanoseconds */
368 frame_interval = ktime_set(1, 0);
370 frame_interval = ktime_set(0, 1000000000ul / (unsigned long)framerate);
373 printk(KERN_INFO "vdummy: start streaming (interval: %ld ms)\n",
374 (long)ktime_to_ms(frame_interval));
375 hrtimer_start(&timer, frame_interval, HRTIMER_MODE_REL);
379 static void stop_streaming(struct vb2_queue *vbq)
381 printk(KERN_INFO "vdummy: stop streaming\n");
382 hrtimer_cancel(&timer);
385 clear_queue(VB2_BUF_STATE_ERROR);
388 static int update_frame(int xsz, int ysz)
392 int new_fsz = xsz * ysz * 3;
394 if(xsz == width && ysz == height) {
398 printk(KERN_INFO "vdummy: update_frame(%d, %d)\n", xsz, ysz);
400 if(!frame || frame_size < new_fsz) {
401 if(!(tmp = kmalloc(new_fsz, GFP_KERNEL))) {
405 frame_size = new_fsz;
412 for(i=0; i<height; i++) {
413 for(j=0; j<width; j++) {
416 *tmp++ = (val << 1) & 0xff;
417 *tmp++ = (val << 2) & 0xff;
424 static int gen_frame(void)
426 struct buffer *buf = 0;
429 printk(KERN_INFO "vdummy: gen_frame\n");
431 spin_lock_irqsave(&blist_lock, flags);
434 buflist = buflist->next;
435 if(!buflist) buflist_tail = 0;
437 spin_unlock_irqrestore(&blist_lock, flags);
440 memcpy(vb2_plane_vaddr(&buf->vb.vb2_buf, 0), frame, frame_size);
442 buf->vb.vb2_buf.timestamp = ktime_get_ns();
443 printk(KERN_INFO "vdummy: copying frame: %d ts: %lu\n", seqno,
444 (unsigned long)buf->vb.vb2_buf.timestamp);
445 buf->vb.sequence = seqno++;
446 buf->vb.field = V4L2_FIELD_NONE;
447 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_DONE);
448 vb2_set_plane_payload(&buf->vb.vb2_buf, 0, frame_size);
453 enum hrtimer_restart timer_func(struct hrtimer *timer)
455 printk(KERN_INFO "vdummy: timer!\n");
457 return streaming ? HRTIMER_RESTART : HRTIMER_NORESTART;