d107e59d1c9ba326fc39c6efa81731400b02c1cf
[vdummy] / vdummy.c
1 #include <linux/module.h>
2 #include <linux/kernel.h>
3 #include <linux/mutex.h>
4 #include <linux/fs.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-v4l2.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>
17
18 struct buffer {
19         struct vb2_v4l2_buffer vb;
20         struct buffer *next;
21 };
22
23 static int init(void);
24 static void shutdown(void);
25
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_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);
34
35 static int queue_setup(struct vb2_queue *vbq, unsigned int *nbuf,
36                 unsigned int *nplanes, unsigned int *sizes, void **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);
41
42
43 static int update_frame(int xsz, int ysz);
44 static int gen_frame(void);
45 enum hrtimer_restart timer_func(struct hrtimer *timer);
46
47
48 module_init(init);
49 module_exit(shutdown);
50
51 MODULE_LICENSE("GPL");
52 MODULE_AUTHOR("John Tsiombikas");
53 MODULE_DESCRIPTION("v4l2 test module");
54
55 static struct mutex mutex;
56 static struct video_device *vdev;
57
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;
66
67 static int streaming;
68 static spinlock_t blist_lock;
69 static struct buffer *buflist, *buflist_tail;
70 static int seqno;
71
72 static int framerate = 1;
73 static struct hrtimer timer;
74
75 static int init(void)
76 {
77         int res;
78
79         strcpy(v4l2_dev.name, "vdummy");
80         if((res = v4l2_device_register(0, &v4l2_dev)) != 0) {
81                 return res;
82         }
83
84         mutex_init(&mutex);
85         spin_lock_init(&blist_lock);
86
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;
94
95         vbq.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
96         vbq.io_modes = VB2_MMAP | VB2_READ;
97         vbq.buf_struct_size = sizeof *buflist;
98         vbq.ops = &vbops;
99         vbq.mem_ops = &vb2_vmalloc_memops;
100         vbq.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
101         vbq.min_buffers_needed = 2;
102         vbq.lock = &mutex;
103         vbq.gfp_flags = GFP_KERNEL;
104
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);
108                 return res;
109         }
110
111         if(!(vdev = video_device_alloc())) {
112                 return -ENOMEM;
113         }
114         memset(vdev, 0, sizeof *vdev);
115         vdev->release = video_device_release;
116         strcpy(vdev->name, KBUILD_MODNAME);
117         vdev->fops = &fops;
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;
122         vdev->lock = &mutex;
123         vdev->queue = &vbq;
124
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;
132
133         iops.vidioc_querycap = ioctl_querycap;
134         iops.vidioc_enum_fmt_vid_cap = ioctl_enum_fmt;
135         iops.vidioc_g_fmt_vid_cap = ioctl_get_fmt;
136         iops.vidioc_s_fmt_vid_cap = ioctl_set_fmt;
137         iops.vidioc_try_fmt_vid_cap = ioctl_try_fmt;
138         iops.vidioc_g_parm = ioctl_get_parm;
139         iops.vidioc_s_parm = ioctl_set_parm;
140         iops.vidioc_queryctrl = ioctl_queryctl;
141         iops.vidioc_reqbufs = vb2_ioctl_reqbufs;
142         iops.vidioc_create_bufs = vb2_ioctl_create_bufs;
143         iops.vidioc_querybuf = vb2_ioctl_querybuf;
144         iops.vidioc_qbuf = vb2_ioctl_qbuf;
145         iops.vidioc_dqbuf = vb2_ioctl_dqbuf;
146         iops.vidioc_expbuf = vb2_ioctl_expbuf;
147         iops.vidioc_streamon = vb2_ioctl_streamon;
148         iops.vidioc_streamoff = vb2_ioctl_streamoff;
149         iops.vidioc_log_status = v4l2_ctrl_log_status;
150         iops.vidioc_subscribe_event = v4l2_ctrl_subscribe_event;
151         iops.vidioc_unsubscribe_event = v4l2_event_unsubscribe;
152
153         if((res = video_register_device(vdev, VFL_TYPE_GRABBER, -1)) != 0) {
154                 printk(KERN_ALERT "vdummy: failed to register video device\n");
155                 v4l2_device_unregister(&v4l2_dev);
156                 video_device_release(vdev);
157                 return res;
158         }
159
160         hrtimer_init(&timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
161         timer.function = timer_func;
162
163         if((res = update_frame(640, 480)) != 0) {
164                 video_unregister_device(vdev);
165                 v4l2_device_unregister(&v4l2_dev);
166                 video_device_release(vdev);
167                 return res;
168         }
169
170         printk(KERN_INFO "vdummy device initialized\n");
171         return 0;
172 }
173
174 static void shutdown(void)
175 {
176         video_unregister_device(vdev);
177         v4l2_device_unregister(&v4l2_dev);
178         kfree(frame);
179 }
180
181 static int ioctl_querycap(struct file *file, void *fh, struct v4l2_capability *cap)
182 {
183         strcpy(cap->driver, KBUILD_MODNAME);
184         strcpy(cap->card, "Test V4L2 device");
185         strcpy(cap->bus_info, "nobus");
186         cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
187         cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
188         return 0;
189 }
190
191 static int ioctl_enum_fmt(struct file *file, void *fh, struct v4l2_fmtdesc *fdesc)
192 {
193         if(fdesc->index != 0 || fdesc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
194                 return -EINVAL;
195         }
196         fdesc->flags = 0;
197         strcpy(fdesc->description, "RGB24");
198         fdesc->pixelformat = V4L2_PIX_FMT_RGB24;
199         return 0;
200 }
201
202 static int ioctl_get_fmt(struct file *file, void *fh, struct v4l2_format *fmt)
203 {
204         if(fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
205                 return -EINVAL;
206         }
207         fmt->fmt.pix.width = width;
208         fmt->fmt.pix.height = height;
209         fmt->fmt.pix.pixelformat = V4L2_PIX_FMT_RGB24;
210         fmt->fmt.pix.field = V4L2_FIELD_NONE;
211         fmt->fmt.pix.bytesperline = 0;
212         fmt->fmt.pix.sizeimage = frame_size;
213         fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SRGB;
214         fmt->fmt.pix.priv = 0;
215         fmt->fmt.pix.flags = 0;
216         fmt->fmt.pix.ycbcr_enc = 0;
217         fmt->fmt.pix.quantization = V4L2_QUANTIZATION_FULL_RANGE;
218         fmt->fmt.pix.xfer_func = V4L2_XFER_FUNC_NONE;
219         return 0;
220 }
221
222 static int ioctl_set_fmt(struct file *file, void *fh, struct v4l2_format *fmt)
223 {
224         int res;
225
226         if(fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
227                 return -EINVAL;
228         }
229         if((res = update_frame(fmt->fmt.pix.width, fmt->fmt.pix.height)) != 0) {
230                 return res;
231         }
232         return ioctl_get_fmt(file, fh, fmt);
233 }
234
235 static int ioctl_try_fmt(struct file *file, void *fh, struct v4l2_format *fmt)
236 {
237         if(fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
238                 return -EINVAL;
239         }
240         if(fmt->fmt.pix.width <= 0)
241                 fmt->fmt.pix.width = width;
242         if(fmt->fmt.pix.height <= 0)
243                 fmt->fmt.pix.height = height;
244         fmt->fmt.pix.pixelformat = V4L2_PIX_FMT_RGB24;
245         fmt->fmt.pix.field = V4L2_FIELD_NONE;
246         fmt->fmt.pix.bytesperline = 0;
247         fmt->fmt.pix.sizeimage = frame_size;
248         fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SRGB;
249         fmt->fmt.pix.priv = 0;
250         fmt->fmt.pix.flags = 0;
251         fmt->fmt.pix.ycbcr_enc = 0;
252         fmt->fmt.pix.quantization = V4L2_QUANTIZATION_FULL_RANGE;
253         fmt->fmt.pix.xfer_func = V4L2_XFER_FUNC_NONE;
254         return 0;
255 }
256
257 static int ioctl_get_parm(struct file *file, void *fh, struct v4l2_streamparm *parm)
258 {
259         if(parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
260                 return -EINVAL;
261         }
262         parm->parm.capture.capability = 0;
263         parm->parm.capture.capturemode = 0;
264         parm->parm.capture.timeperframe.numerator = 1;
265         parm->parm.capture.timeperframe.denominator = framerate;
266         parm->parm.capture.extendedmode = 0;
267         parm->parm.capture.readbuffers = 1;
268         return 0;
269 }
270
271 static int ioctl_set_parm(struct file *file, void *fh, struct v4l2_streamparm *parm)
272 {
273         int new_framerate;
274         if(parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
275                 return -EINVAL;
276         }
277
278         new_framerate = parm->parm.capture.timeperframe.denominator / parm->parm.capture.timeperframe.numerator;
279         framerate = new_framerate < 1 ? 1 : (new_framerate > 60 ? 60 : new_framerate);
280
281         return ioctl_get_parm(file, fh, parm);
282 }
283
284 static int ioctl_queryctl(struct file *file, void *fh, struct v4l2_queryctrl *ctl)
285 {
286         return -EINVAL;
287 }
288
289 static int queue_setup(struct vb2_queue *vbq, unsigned int *nbuf,
290                 unsigned int *nplanes, unsigned int *sizes, void **alloc_ctx)
291 {
292         if(vbq->num_buffers + *nbuf < 2) {
293                 *nbuf = 2 - vbq->num_buffers;
294         }
295
296         *nplanes = 1;
297         sizes[0] = frame_size;
298         alloc_ctx[0] = 0;
299         return 0;
300 }
301
302 static int buf_prepare(struct vb2_buffer *vb)
303 {
304         if(vb2_plane_size(vb, 0) < frame_size) {
305                 printk(KERN_ALERT "vdummy: buffer too small\n");
306                 return -EINVAL;
307         }
308         return 0;
309 }
310
311 static void buf_queue(struct vb2_buffer *vb)
312 {
313         struct buffer *buf = (struct buffer*)((char*)vb - offsetof(struct buffer, vb));
314
315         spin_lock(&blist_lock);
316         if(buflist) {
317                 buflist_tail->next = buf;
318                 buflist_tail = buf;
319         } else {
320                 buflist = buflist_tail = buf;
321         }
322         buf->next = 0;
323         spin_unlock(&blist_lock);
324 }
325
326 static void clear_queue(enum vb2_buffer_state st)
327 {
328         spin_lock(&blist_lock);
329         while(buflist) {
330                 struct buffer *buf = buflist;
331                 buflist = buflist->next;
332                 vb2_buffer_done(&buf->vb.vb2_buf, st);
333         }
334         buflist = buflist_tail = 0;
335         spin_unlock(&blist_lock);
336 }
337
338 static int start_streaming(struct vb2_queue *vbq, unsigned int count)
339 {
340         ktime_t frame_interval;
341
342         streaming = 1;
343         seqno = 0;
344
345         /* calculate frame interval in nanoseconds */
346         if(framerate <= 1) {
347                 frame_interval = ktime_set(1, 0);
348         } else {
349                 frame_interval = ktime_set(0, 1000000000ul / (unsigned long)framerate);
350         }
351
352         hrtimer_start(&timer, frame_interval, HRTIMER_MODE_REL);
353         return 0;
354 }
355
356 static void stop_streaming(struct vb2_queue *vbq)
357 {
358         while(hrtimer_try_to_cancel(&timer) == -1);
359
360         streaming = 0;
361         clear_queue(VB2_BUF_STATE_ERROR);
362 }
363
364 static int update_frame(int xsz, int ysz)
365 {
366         unsigned char *tmp;
367         int i, j;
368         int new_fsz = xsz * ysz * 3;
369
370         if(xsz == width && ysz == height) {
371                 return 0;
372         }
373
374         printk(KERN_INFO "vdummy: update_frame(%d, %d)\n", xsz, ysz);
375
376         if(!frame || frame_size < new_fsz) {
377                 if(!(tmp = kmalloc(new_fsz, GFP_KERNEL))) {
378                         return -ENOMEM;
379                 }
380                 kfree(frame);
381                 frame_size = new_fsz;
382                 width = xsz;
383                 height = ysz;
384                 frame = tmp;
385         }
386
387         tmp = frame;
388         for(i=0; i<height; i++) {
389                 for(j=0; j<width; j++) {
390                         int val = i ^ j;
391                         *tmp++ = val;
392                         *tmp++ = (val << 1) & 0xff;
393                         *tmp++ = (val << 2) & 0xff;
394                 }
395         }
396
397         return 0;
398 }
399
400 static int gen_frame(void)
401 {
402         struct buffer *buf = 0;
403
404         spin_lock(&blist_lock);
405         if(buflist) {
406                 buf = buflist;
407                 buflist = buflist->next;
408                 if(!buflist) buflist_tail = 0;
409         }
410         spin_unlock(&blist_lock);
411
412         if(buf) {
413                 printk(KERN_INFO "vdummy: copying frame: %d\n", seqno);
414
415                 memcpy(vb2_plane_vaddr(&buf->vb.vb2_buf, 0), frame, frame_size);
416
417                 buf->vb.vb2_buf.timestamp = ktime_get_ns();
418                 buf->vb.sequence = seqno++;
419                 buf->vb.field = V4L2_FIELD_NONE;
420                 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_DONE);
421                 vb2_set_plane_payload(&buf->vb.vb2_buf, 0, frame_size);
422         }
423         return 0;
424 }
425
426 enum hrtimer_restart timer_func(struct hrtimer *timer)
427 {
428         gen_frame();
429         return streaming ? HRTIMER_RESTART : HRTIMER_NORESTART;
430 }