1 #include <linux/module.h>
2 #include <linux/kernel.h>
3 #include <linux/mutex.h>
5 #include <linux/slab.h>
6 #include <asm/uaccess.h>
7 #include <linux/videodev2.h>
8 #include <media/videobuf2-vmalloc.h>
9 #include <media/v4l2-dev.h>
10 #include <media/v4l2-ioctl.h>
11 #include <media/v4l2-device.h>
18 static int init(void);
19 static void shutdown(void);
21 static int ioctl_querycap(struct file *file, void *fh, struct v4l2_capability *cap);
22 static int ioctl_enum_fmt(struct file *file, void *fh, struct v4l2_fmtdesc *fdesc);
23 static int ioctl_get_fmt(struct file *file, void *fh, struct v4l2_format *fmt);
24 static int ioctl_set_fmt(struct file *file, void *fh, struct v4l2_format *fmt);
25 static int ioctl_get_parm(struct file *file, void *fh, struct v4l2_streamparm *fmt);
26 static int ioctl_set_parm(struct file *file, void *fh, struct v4l2_streamparm *fmt);
27 static int ioctl_queryctl(struct file *file, void *fh, struct v4l2_queryctrl *ctl);
29 static int queue_setup(struct vb2_queue *vbq, unsigned int *nbuf,
30 unsigned int *nplanes, unsigned int *sizes, void **alloc_ctx);
31 static int buf_prepare(struct vb2_buffer *vb);
32 static void buf_queue(struct vb2_buffer *vb);
33 static int start_streaming(struct vb2_queue *vbq, unsigned int count);
34 static void stop_streaming(struct vb2_queue *vbq);
37 static int update_frame(int xsz, int ysz);
38 static int gen_frame(void);
42 module_exit(shutdown);
44 MODULE_LICENSE("GPL");
45 MODULE_AUTHOR("John Tsiombikas");
46 MODULE_DESCRIPTION("v4l2 test module");
48 static struct mutex mutex;
49 static struct video_device *vdev;
51 static struct v4l2_device v4l2_dev;
52 static struct v4l2_file_operations fops;
53 static struct v4l2_ioctl_ops iops;
54 static struct vb2_queue vbq;
55 static struct vb2_ops vbops;
56 static int width, height;
57 static unsigned char *frame;
58 static int frame_size;
61 static spinlock_t blist_lock;
62 static struct buffer *buflist, *buflist_tail;
70 strcpy(v4l2_dev.name, "vdummy");
71 if((res = v4l2_device_register(0, &v4l2_dev)) != 0) {
77 vbops.queue_setup = queue_setup;
78 vbops.buf_prepare = buf_prepare;
79 vbops.buf_queue = buf_queue;
80 vbops.start_streaming = start_streaming;
81 vbops.stop_streaming = stop_streaming;
82 vbops.wait_prepare = vb2_ops_wait_prepare;
83 vbops.wait_finish = vb2_ops_wait_finish;
85 vbq.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
86 vbq.io_modes = VB2_MMAP | VB2_READ;
87 vbq.buf_struct_size = sizeof *buflist;
89 vbq.mem_ops = &vb2_vmalloc_memops;
90 vbq.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
91 vbq.min_buffers_needed = 1;
93 vbq.gfp_flags = GFP_KERNEL;
95 if((res = vb2_queue_init(&vbq)) != 0) {
96 printk(KERN_ALERT "vdummy: failed to initialize videobuf2 queue\n");
97 v4l2_device_unregister(&v4l2_dev);
101 if(!(vdev = video_device_alloc())) {
104 vdev->release = video_device_release;
105 strcpy(vdev->name, KBUILD_MODNAME);
107 vdev->ioctl_ops = &iops;
108 vdev->vfl_type = VFL_TYPE_GRABBER;
109 vdev->v4l2_dev = &v4l2_dev;
111 fops.owner = THIS_MODULE;
112 fops.open = v4l2_fh_open;
113 fops.release = vb2_fop_release;
114 fops.read = vb2_fop_read;
115 fops.mmap = vb2_fop_mmap;
116 fops.poll = vb2_fop_poll;
117 fops.unlocked_ioctl = video_ioctl2;
119 iops.vidioc_querycap = ioctl_querycap;
120 iops.vidioc_enum_fmt_vid_cap = ioctl_enum_fmt;
121 iops.vidioc_g_fmt_vid_cap = ioctl_get_fmt;
122 iops.vidioc_s_fmt_vid_cap = ioctl_set_fmt;
123 iops.vidioc_g_parm = ioctl_get_parm;
124 iops.vidioc_s_parm = ioctl_set_parm;
125 iops.vidioc_queryctrl = ioctl_queryctl;
126 iops.vidioc_reqbufs = vb2_ioctl_reqbufs;
127 iops.vidioc_create_bufs = vb2_ioctl_create_bufs;
128 iops.vidioc_querybuf = vb2_ioctl_querybuf;
129 iops.vidioc_qbuf = vb2_ioctl_qbuf;
130 iops.vidioc_dqbuf = vb2_ioctl_dqbuf;
131 iops.vidioc_expbuf = vb2_ioctl_expbuf;
132 iops.vidioc_streamon = vb2_ioctl_streamon;
133 iops.vidioc_streamoff = vb2_ioctl_streamoff;
135 if((res = video_register_device(vdev, VFL_TYPE_GRABBER, -1)) != 0) {
136 printk(KERN_ALERT "vdummy: failed to register video device\n");
137 v4l2_device_unregister(&v4l2_dev);
138 video_device_release(vdev);
142 if((res = update_frame(640, 480)) != 0) {
143 video_unregister_device(vdev);
144 v4l2_device_unregister(&v4l2_dev);
145 video_device_release(vdev);
149 printk(KERN_INFO "vdummy device initialized\n");
153 static void shutdown(void)
155 video_unregister_device(vdev);
156 v4l2_device_unregister(&v4l2_dev);
160 static int ioctl_querycap(struct file *file, void *fh, struct v4l2_capability *cap)
162 strcpy(cap->driver, KBUILD_MODNAME);
163 strcpy(cap->card, "dummy v4l2 dev");
164 strcpy(cap->bus_info, "nobus");
165 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
166 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
170 static int ioctl_enum_fmt(struct file *file, void *fh, struct v4l2_fmtdesc *fdesc)
172 if(fdesc->index != 0 || fdesc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
176 strcpy(fdesc->description, "RGB24");
177 fdesc->pixelformat = V4L2_PIX_FMT_RGB24;
181 static int ioctl_get_fmt(struct file *file, void *fh, struct v4l2_format *fmt)
183 if(fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
186 fmt->fmt.pix.width = width;
187 fmt->fmt.pix.height = height;
188 fmt->fmt.pix.pixelformat = V4L2_PIX_FMT_RGB24;
189 fmt->fmt.pix.field = V4L2_FIELD_NONE;
190 fmt->fmt.pix.bytesperline = 0;
191 fmt->fmt.pix.sizeimage = frame_size;
192 fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SRGB;
193 fmt->fmt.pix.priv = 0;
194 fmt->fmt.pix.flags = 0;
195 fmt->fmt.pix.ycbcr_enc = 0;
196 fmt->fmt.pix.quantization = V4L2_QUANTIZATION_FULL_RANGE;
197 fmt->fmt.pix.xfer_func = V4L2_XFER_FUNC_NONE;
201 static int ioctl_set_fmt(struct file *file, void *fh, struct v4l2_format *fmt)
205 if(fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
208 if((res = update_frame(fmt->fmt.pix.width, fmt->fmt.pix.height)) != 0) {
211 return ioctl_get_fmt(file, fh, fmt);
214 static int ioctl_get_parm(struct file *file, void *fh, struct v4l2_streamparm *parm)
216 if(parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
219 parm->parm.capture.capability = 0;
220 parm->parm.capture.capturemode = 0;
221 parm->parm.capture.timeperframe.numerator = 1;
222 parm->parm.capture.timeperframe.denominator = 30;
223 parm->parm.capture.extendedmode = 0;
224 parm->parm.capture.readbuffers = 1;
228 static int ioctl_set_parm(struct file *file, void *fh, struct v4l2_streamparm *parm)
230 if(parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
233 return ioctl_get_parm(file, fh, parm);
236 static int ioctl_queryctl(struct file *file, void *fh, struct v4l2_queryctrl *ctl)
241 static int queue_setup(struct vb2_queue *vbq, unsigned int *nbuf,
242 unsigned int *nplanes, unsigned int *sizes, void **alloc_ctx)
244 if(vbq->num_buffers + *nbuf < 2) {
245 *nbuf = 2 - vbq->num_buffers;
249 sizes[0] = frame_size;
254 static int buf_prepare(struct vb2_buffer *vb)
256 if(vb2_plane_size(vb, 0) < frame_size) {
257 printk(KERN_ALERT "vdummy: buffer too small\n");
260 vb2_set_plane_payload(vb, 0, frame_size);
264 static void buf_queue(struct vb2_buffer *vb)
266 struct buffer *buf = (struct buffer*)((char*)vb - offsetof(struct buffer, vb));
268 spin_lock(&blist_lock);
270 buflist_tail->next = buf;
273 buflist = buflist_tail = buf;
276 spin_unlock(&blist_lock);
279 static void clear_queue(enum vb2_buffer_state st)
281 spin_lock(&blist_lock);
283 struct buffer *buf = buflist;
284 buflist = buflist->next;
285 vb2_buffer_done(&buf->vb, st);
287 buflist = buflist_tail = 0;
288 spin_unlock(&blist_lock);
291 static int start_streaming(struct vb2_queue *vbq, unsigned int count)
300 static void stop_streaming(struct vb2_queue *vbq)
303 clear_queue(VB2_BUF_STATE_ERROR);
306 static int update_frame(int xsz, int ysz)
310 int new_fsz = xsz * ysz * 3;
312 if(xsz == width && ysz == height) {
316 printk(KERN_INFO "vdummy: update_frame(%d, %d)\n", xsz, ysz);
318 if(!frame || frame_size < new_fsz) {
319 if(!(tmp = kmalloc(new_fsz, GFP_KERNEL))) {
323 frame_size = new_fsz;
330 for(i=0; i<height; i++) {
331 for(j=0; j<width; j++) {
334 *tmp++ = (val << 1) & 0xff;
335 *tmp++ = (val << 2) & 0xff;
342 static int gen_frame(void)
344 struct buffer *buf = 0;
346 spin_lock(&blist_lock);
349 buflist = buflist->next;
350 if(!buflist) buflist_tail = 0;
352 spin_unlock(&blist_lock);
355 memcpy(vb2_plane_vaddr(&buf->vb, 0), frame, frame_size);
357 /*v4l2_get_timestamp(&buf->vb.v4l2_buf.timestamp);
358 buf->vb.v4l2_buf.sequence = seqno++;
359 buf->vb.v4l2_buf.field = V4L2_FIELD_NONE;*/
360 vb2_buffer_done(&buf->vb, VB2_BUF_STATE_DONE);