#include <linux/module.h>
#include <linux/kernel.h>
-#include <linux/version.h>
+#include <linux/mutex.h>
#include <linux/fs.h>
#include <linux/slab.h>
#include <asm/uaccess.h>
#include <linux/videodev2.h>
+#include <media/videobuf2-v4l2.h>
+#include <media/videobuf2-vmalloc.h>
#include <media/v4l2-dev.h>
#include <media/v4l2-ioctl.h>
+#include <media/v4l2-device.h>
+#include <media/v4l2-ctrls.h>
+#include <media/v4l2-event.h>
+
+struct buffer {
+ struct vb2_buffer vb;
+ struct buffer *next;
+};
static int init(void);
static void shutdown(void);
-static int open(struct file *file);
-static int close(struct file *file);
-static ssize_t read(struct file *file, char *ubuf, size_t bufsz, loff_t *offs);
static int ioctl_querycap(struct file *file, void *fh, struct v4l2_capability *cap);
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_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 queue_setup(struct vb2_queue *vbq, unsigned int *nbuf,
+ unsigned int *nplanes, unsigned int *sizes, void **alloc_ctx);
+static int buf_prepare(struct vb2_buffer *vb);
+static void buf_queue(struct vb2_buffer *vb);
+static int start_streaming(struct vb2_queue *vbq, unsigned int count);
+static void stop_streaming(struct vb2_queue *vbq);
+
+
static int update_frame(int xsz, int ysz);
+static int gen_frame(void);
module_init(init);
MODULE_AUTHOR("John Tsiombikas");
MODULE_DESCRIPTION("v4l2 test module");
-#define DEVNAME "video42"
-
-struct video_device *vdev;
+static struct mutex mutex;
+static struct video_device *vdev;
+static struct v4l2_device v4l2_dev;
static struct v4l2_file_operations fops;
static struct v4l2_ioctl_ops iops;
+static struct vb2_queue vbq;
+static struct vb2_ops vbops;
static int width, height;
static unsigned char *frame;
static int frame_size;
+static int streaming;
+static spinlock_t blist_lock;
+static struct buffer *buflist, *buflist_tail;
+static int seqno;
+
static int init(void)
{
int res;
+ strcpy(v4l2_dev.name, "vdummy");
+ if((res = v4l2_device_register(0, &v4l2_dev)) != 0) {
+ return res;
+ }
+
+ mutex_init(&mutex);
+ spin_lock_init(&blist_lock);
+
+ vbops.queue_setup = queue_setup;
+ vbops.buf_prepare = buf_prepare;
+ vbops.buf_queue = buf_queue;
+ vbops.start_streaming = start_streaming;
+ vbops.stop_streaming = stop_streaming;
+ vbops.wait_prepare = vb2_ops_wait_prepare;
+ vbops.wait_finish = vb2_ops_wait_finish;
+
+ vbq.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
+ vbq.io_modes = VB2_MMAP | VB2_READ;
+ vbq.buf_struct_size = sizeof *buflist;
+ vbq.ops = &vbops;
+ vbq.mem_ops = &vb2_vmalloc_memops;
+ vbq.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
+ vbq.min_buffers_needed = 1;
+ vbq.lock = &mutex;
+ vbq.gfp_flags = GFP_KERNEL;
+
+ if((res = vb2_queue_init(&vbq)) != 0) {
+ printk(KERN_ALERT "vdummy: failed to initialize videobuf2 queue\n");
+ v4l2_device_unregister(&v4l2_dev);
+ return res;
+ }
+
if(!(vdev = video_device_alloc())) {
return -ENOMEM;
}
+ memset(vdev, 0, sizeof *vdev);
vdev->release = video_device_release;
- strcpy(vdev->name, "video test");
+ strcpy(vdev->name, KBUILD_MODNAME);
vdev->fops = &fops;
vdev->ioctl_ops = &iops;
vdev->vfl_type = VFL_TYPE_GRABBER;
-
- fops.open = open;
- fops.release = close;
- fops.read = read;
+ vdev->vfl_dir = VFL_DIR_RX;
+ vdev->v4l2_dev = &v4l2_dev;
+ vdev->lock = &mutex;
+ vdev->queue = &vbq;
+
+ fops.owner = THIS_MODULE;
+ fops.open = v4l2_fh_open;
+ fops.release = vb2_fop_release;
+ fops.read = vb2_fop_read;
+ fops.mmap = vb2_fop_mmap;
+ fops.poll = vb2_fop_poll;
fops.unlocked_ioctl = video_ioctl2;
iops.vidioc_querycap = ioctl_querycap;
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_g_parm = ioctl_get_parm;
+ iops.vidioc_s_parm = ioctl_set_parm;
iops.vidioc_queryctrl = ioctl_queryctl;
+ iops.vidioc_reqbufs = vb2_ioctl_reqbufs;
+ iops.vidioc_create_bufs = vb2_ioctl_create_bufs;
+ iops.vidioc_querybuf = vb2_ioctl_querybuf;
+ iops.vidioc_qbuf = vb2_ioctl_qbuf;
+ iops.vidioc_dqbuf = vb2_ioctl_dqbuf;
+ iops.vidioc_expbuf = vb2_ioctl_expbuf;
+ iops.vidioc_streamon = vb2_ioctl_streamon;
+ iops.vidioc_streamoff = vb2_ioctl_streamoff;
+ iops.vidioc_log_status = v4l2_ctrl_log_status;
+ iops.vidioc_subscribe_event = v4l2_ctrl_subscribe_event;
+ iops.vidioc_unsubscribe_event = v4l2_event_unsubscribe;
if((res = video_register_device(vdev, VFL_TYPE_GRABBER, -1)) != 0) {
+ printk(KERN_ALERT "vdummy: failed to register video device\n");
+ v4l2_device_unregister(&v4l2_dev);
video_device_release(vdev);
return res;
}
- return 0;
-}
-
-static void shutdown(void)
-{
- video_unregister_device(vdev);
- kfree(frame);
-}
-
-static int open(struct file *file)
-{
- int res;
- try_module_get(THIS_MODULE);
if((res = update_frame(640, 480)) != 0) {
+ video_unregister_device(vdev);
+ v4l2_device_unregister(&v4l2_dev);
+ video_device_release(vdev);
return res;
}
- return 0;
-}
-static int close(struct file *file)
-{
- module_put(THIS_MODULE);
+ printk(KERN_INFO "vdummy device initialized\n");
return 0;
}
-static ssize_t read(struct file *file, char *ubuf, size_t ubufsz, loff_t *offs)
+static void shutdown(void)
{
- int sz = frame_size - *offs;
-
- if(sz <= 0) return 0;
- if(ubufsz < sz) sz = ubufsz;
-
- if(copy_to_user(ubuf, frame + *offs, sz) != 0) {
- return -EFAULT;
- }
-
- *offs += sz;
- if(*offs >= frame_size) {
- *offs = 0;
- }
- return sz;
+ video_unregister_device(vdev);
+ v4l2_device_unregister(&v4l2_dev);
+ kfree(frame);
}
static int ioctl_querycap(struct file *file, void *fh, struct v4l2_capability *cap)
{
- strcpy(cap->driver, "vidtest");
- strcpy(cap->card, "fake");
+ strcpy(cap->driver, KBUILD_MODNAME);
+ strcpy(cap->card, "Test V4L2 device");
strcpy(cap->bus_info, "nobus");
- cap->version = 0;
- cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_READWRITE;
+ cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
+ cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
return 0;
}
return ioctl_get_fmt(file, fh, fmt);
}
+static int ioctl_get_parm(struct file *file, void *fh, struct v4l2_streamparm *parm)
+{
+ if(parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
+ return -EINVAL;
+ }
+ 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.extendedmode = 0;
+ parm->parm.capture.readbuffers = 1;
+ return 0;
+}
+
+static int ioctl_set_parm(struct file *file, void *fh, struct v4l2_streamparm *parm)
+{
+ if(parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
+ return -EINVAL;
+ }
+ return ioctl_get_parm(file, fh, parm);
+}
+
static int ioctl_queryctl(struct file *file, void *fh, struct v4l2_queryctrl *ctl)
{
return -EINVAL;
}
+static int queue_setup(struct vb2_queue *vbq, unsigned int *nbuf,
+ unsigned int *nplanes, unsigned int *sizes, void **alloc_ctx)
+{
+ if(vbq->num_buffers + *nbuf < 2) {
+ *nbuf = 2 - vbq->num_buffers;
+ }
+
+ *nplanes = 1;
+ sizes[0] = frame_size;
+ alloc_ctx[0] = 0;
+ return 0;
+}
+
+static int buf_prepare(struct vb2_buffer *vb)
+{
+ if(vb2_plane_size(vb, 0) < frame_size) {
+ printk(KERN_ALERT "vdummy: buffer too small\n");
+ return -EINVAL;
+ }
+ vb2_set_plane_payload(vb, 0, frame_size);
+ return 0;
+}
+
+static void buf_queue(struct vb2_buffer *vb)
+{
+ struct buffer *buf = (struct buffer*)((char*)vb - offsetof(struct buffer, vb));
+
+ spin_lock(&blist_lock);
+ if(buflist) {
+ buflist_tail->next = buf;
+ buflist_tail = buf;
+ } else {
+ buflist = buflist_tail = buf;
+ }
+ buf->next = 0;
+ spin_unlock(&blist_lock);
+}
+
+static void clear_queue(enum vb2_buffer_state st)
+{
+ spin_lock(&blist_lock);
+ while(buflist) {
+ struct buffer *buf = buflist;
+ buflist = buflist->next;
+ vb2_buffer_done(&buf->vb, st);
+ }
+ buflist = buflist_tail = 0;
+ spin_unlock(&blist_lock);
+}
+
+static int start_streaming(struct vb2_queue *vbq, unsigned int count)
+{
+ streaming = 1;
+ seqno = 0;
+
+ gen_frame();
+ return 0;
+}
+
+static void stop_streaming(struct vb2_queue *vbq)
+{
+ streaming = 0;
+ clear_queue(VB2_BUF_STATE_ERROR);
+}
+
static int update_frame(int xsz, int ysz)
{
unsigned char *tmp;
int i, j;
- int new_fsz = xsz * ysz;
+ int new_fsz = xsz * ysz * 3;
if(xsz == width && ysz == height) {
return 0;
}
+ printk(KERN_INFO "vdummy: update_frame(%d, %d)\n", xsz, ysz);
+
if(!frame || frame_size < new_fsz) {
if(!(tmp = kmalloc(new_fsz, GFP_KERNEL))) {
return -ENOMEM;
*tmp++ = (val << 2) & 0xff;
}
}
+
+ return 0;
+}
+
+static int gen_frame(void)
+{
+ struct buffer *buf = 0;
+
+ spin_lock(&blist_lock);
+ if(buflist) {
+ buf = buflist;
+ buflist = buflist->next;
+ if(!buflist) buflist_tail = 0;
+ }
+ spin_unlock(&blist_lock);
+
+ if(buf) {
+ struct vb2_v4l2_buffer *vvbuf;
+
+ memcpy(vb2_plane_vaddr(&buf->vb, 0), frame, frame_size);
+
+ vvbuf = to_vb2_v4l2_buffer(&buf->vb);
+ buf->vb.timestamp = ktime_get_ns();
+ vvbuf->sequence = seqno++;
+ vvbuf->field = V4L2_FIELD_NONE;
+ vb2_buffer_done(&buf->vb, VB2_BUF_STATE_DONE);
+ }
+
return 0;
}