#include <linux/mutex.h>
#include <linux/fs.h>
#include <linux/slab.h>
+#include <linux/hrtimer.h>
+#include <linux/ktime.h>
#include <asm/uaccess.h>
#include <linux/videodev2.h>
#include <media/videobuf2-v4l2.h>
static int ioctl_enum_fmt(struct file *file, void *fh, struct v4l2_fmtdesc *fdesc);
static int ioctl_get_fmt(struct file *file, void *fh, struct v4l2_format *fmt);
static int ioctl_set_fmt(struct file *file, void *fh, struct v4l2_format *fmt);
+static int ioctl_try_fmt(struct file *file, void *fh, struct v4l2_format *fmt);
static int ioctl_get_parm(struct file *file, void *fh, struct v4l2_streamparm *fmt);
static int ioctl_set_parm(struct file *file, void *fh, struct v4l2_streamparm *fmt);
static int ioctl_queryctl(struct file *file, void *fh, struct v4l2_queryctrl *ctl);
static int update_frame(int xsz, int ysz);
static int gen_frame(void);
+enum hrtimer_restart timer_func(struct hrtimer *timer);
module_init(init);
static struct buffer *buflist, *buflist_tail;
static int seqno;
+static int framerate = 1;
+static struct hrtimer timer;
static int init(void)
{
vbq.ops = &vbops;
vbq.mem_ops = &vb2_vmalloc_memops;
vbq.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
- vbq.min_buffers_needed = 1;
+ vbq.min_buffers_needed = 2;
vbq.lock = &mutex;
vbq.gfp_flags = GFP_KERNEL;
iops.vidioc_enum_fmt_vid_cap = ioctl_enum_fmt;
iops.vidioc_g_fmt_vid_cap = ioctl_get_fmt;
iops.vidioc_s_fmt_vid_cap = ioctl_set_fmt;
+ iops.vidioc_try_fmt_vid_cap = ioctl_try_fmt;
iops.vidioc_g_parm = ioctl_get_parm;
iops.vidioc_s_parm = ioctl_set_parm;
iops.vidioc_queryctrl = ioctl_queryctl;
return res;
}
+ hrtimer_init(&timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
+ timer.function = timer_func;
+
if((res = update_frame(640, 480)) != 0) {
video_unregister_device(vdev);
v4l2_device_unregister(&v4l2_dev);
return ioctl_get_fmt(file, fh, fmt);
}
+static int ioctl_try_fmt(struct file *file, void *fh, struct v4l2_format *fmt)
+{
+ if(fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
+ return -EINVAL;
+ }
+ if(fmt->fmt.pix.width <= 0)
+ fmt->fmt.pix.width = width;
+ if(fmt->fmt.pix.height <= 0)
+ fmt->fmt.pix.height = height;
+ fmt->fmt.pix.pixelformat = V4L2_PIX_FMT_RGB24;
+ fmt->fmt.pix.field = V4L2_FIELD_NONE;
+ fmt->fmt.pix.bytesperline = 0;
+ fmt->fmt.pix.sizeimage = frame_size;
+ fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SRGB;
+ fmt->fmt.pix.priv = 0;
+ fmt->fmt.pix.flags = 0;
+ fmt->fmt.pix.ycbcr_enc = 0;
+ fmt->fmt.pix.quantization = V4L2_QUANTIZATION_FULL_RANGE;
+ fmt->fmt.pix.xfer_func = V4L2_XFER_FUNC_NONE;
+ return 0;
+}
+
static int ioctl_get_parm(struct file *file, void *fh, struct v4l2_streamparm *parm)
{
if(parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
parm->parm.capture.capability = 0;
parm->parm.capture.capturemode = 0;
parm->parm.capture.timeperframe.numerator = 1;
- parm->parm.capture.timeperframe.denominator = 30;
+ parm->parm.capture.timeperframe.denominator = framerate;
parm->parm.capture.extendedmode = 0;
parm->parm.capture.readbuffers = 1;
return 0;
static int ioctl_set_parm(struct file *file, void *fh, struct v4l2_streamparm *parm)
{
+ int new_framerate;
if(parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
return -EINVAL;
}
+
+ new_framerate = parm->parm.capture.timeperframe.denominator / parm->parm.capture.timeperframe.numerator;
+ framerate = new_framerate < 1 ? 1 : (new_framerate > 60 ? 60 : new_framerate);
+
return ioctl_get_parm(file, fh, parm);
}
static int start_streaming(struct vb2_queue *vbq, unsigned int count)
{
+ ktime_t frame_interval;
+
streaming = 1;
seqno = 0;
- gen_frame();
+ /* calculate frame interval in nanoseconds */
+ if(framerate <= 1) {
+ frame_interval = ktime_set(1, 0);
+ } else {
+ frame_interval = ktime_set(0, 1000000000ul / (unsigned long)framerate);
+ }
+
+ hrtimer_start(&timer, frame_interval, HRTIMER_MODE_REL);
return 0;
}
static void stop_streaming(struct vb2_queue *vbq)
{
+ while(hrtimer_try_to_cancel(&timer) == -1);
+
streaming = 0;
clear_queue(VB2_BUF_STATE_ERROR);
}
spin_unlock(&blist_lock);
if(buf) {
+ printk(KERN_INFO "vdummy: copying frame: %d\n", seqno);
+
memcpy(vb2_plane_vaddr(&buf->vb.vb2_buf, 0), frame, frame_size);
buf->vb.vb2_buf.timestamp = ktime_get_ns();
vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_DONE);
vb2_set_plane_payload(&buf->vb.vb2_buf, 0, frame_size);
}
-
return 0;
}
+
+enum hrtimer_restart timer_func(struct hrtimer *timer)
+{
+ gen_frame();
+ return streaming ? HRTIMER_RESTART : HRTIMER_NORESTART;
+}