conditional alloc_ctx type based on kernel version
[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 <linux/version.h>
9 #include <asm/uaccess.h>
10 #include <linux/videodev2.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_enum_framesz(struct file *file, void *fh, struct v4l2_frmsizeenum *fsz);
29 static int ioctl_get_fmt(struct file *file, void *fh, struct v4l2_format *fmt);
30 static int ioctl_set_fmt(struct file *file, void *fh, struct v4l2_format *fmt);
31 static int ioctl_try_fmt(struct file *file, void *fh, struct v4l2_format *fmt);
32 static int ioctl_get_parm(struct file *file, void *fh, struct v4l2_streamparm *fmt);
33 static int ioctl_set_parm(struct file *file, void *fh, struct v4l2_streamparm *fmt);
34 static int ioctl_queryctl(struct file *file, void *fh, struct v4l2_queryctrl *ctl);
35
36 /* TODO find out exact version where this switched over */
37 #if LINUX_VERSION_CODE < KERNEL_VERSION(4,7,0)
38 #define ALLOC_CTX_TYPE  void
39 #else
40 #define ALLOC_CTX_TYPE  struct device
41 #endif
42
43 static int queue_setup(struct vb2_queue *vbq, unsigned int *nbuf,
44                 unsigned int *nplanes, unsigned int *sizes, ALLOC_CTX_TYPE **alloc_ctx);
45 static int buf_prepare(struct vb2_buffer *vb);
46 static void buf_queue(struct vb2_buffer *vb);
47 static int start_streaming(struct vb2_queue *vbq, unsigned int count);
48 static void stop_streaming(struct vb2_queue *vbq);
49
50
51 static int update_frame(int xsz, int ysz);
52 static int gen_frame(void);
53 enum hrtimer_restart timer_func(struct hrtimer *timer);
54
55
56 module_init(init);
57 module_exit(shutdown);
58
59 MODULE_LICENSE("GPL");
60 MODULE_AUTHOR("John Tsiombikas");
61 MODULE_DESCRIPTION("Dummy V4L2 device");
62
63 static struct mutex mutex;
64 static struct video_device *vdev;
65
66 static struct v4l2_device v4l2_dev;
67 static struct v4l2_file_operations fops;
68 static struct v4l2_ioctl_ops iops;
69 static struct vb2_queue vbq;
70 static struct vb2_ops vbops;
71 static int width, height;
72 static unsigned char *frame;
73 static int frame_size;
74
75 static int streaming;
76 static spinlock_t blist_lock;
77 static struct buffer *buflist, *buflist_tail;
78 static int seqno;
79
80 static int framerate = 1;
81 static struct hrtimer timer;
82
83 static int init(void)
84 {
85         int res;
86
87         strcpy(v4l2_dev.name, "vdummy");
88         if((res = v4l2_device_register(0, &v4l2_dev)) != 0) {
89                 return res;
90         }
91
92         mutex_init(&mutex);
93         spin_lock_init(&blist_lock);
94
95         vbops.queue_setup = queue_setup;
96         vbops.buf_prepare = buf_prepare;
97         vbops.buf_queue = buf_queue;
98         vbops.start_streaming = start_streaming;
99         vbops.stop_streaming = stop_streaming;
100         vbops.wait_prepare = vb2_ops_wait_prepare;
101         vbops.wait_finish = vb2_ops_wait_finish;
102
103         vbq.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
104         vbq.io_modes = VB2_MMAP | VB2_READ;
105         vbq.buf_struct_size = sizeof *buflist;
106         vbq.ops = &vbops;
107         vbq.mem_ops = &vb2_vmalloc_memops;
108         vbq.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
109         vbq.min_buffers_needed = 2;
110         vbq.lock = &mutex;
111         vbq.gfp_flags = GFP_KERNEL;
112
113         if((res = vb2_queue_init(&vbq)) != 0) {
114                 printk(KERN_ALERT "vdummy: failed to initialize videobuf2 queue\n");
115                 v4l2_device_unregister(&v4l2_dev);
116                 return res;
117         }
118
119         if(!(vdev = video_device_alloc())) {
120                 return -ENOMEM;
121         }
122         memset(vdev, 0, sizeof *vdev);
123         vdev->release = video_device_release;
124         strcpy(vdev->name, KBUILD_MODNAME);
125         vdev->fops = &fops;
126         vdev->ioctl_ops = &iops;
127         vdev->vfl_type = VFL_TYPE_GRABBER;
128         vdev->vfl_dir = VFL_DIR_RX;
129         vdev->v4l2_dev = &v4l2_dev;
130         vdev->lock = &mutex;
131         vdev->queue = &vbq;
132
133         fops.owner = THIS_MODULE;
134         fops.open = v4l2_fh_open;
135         fops.release = vb2_fop_release;
136         fops.read = vb2_fop_read;
137         fops.mmap = vb2_fop_mmap;
138         fops.poll = vb2_fop_poll;
139         fops.unlocked_ioctl = video_ioctl2;
140
141         iops.vidioc_querycap = ioctl_querycap;
142         iops.vidioc_enum_fmt_vid_cap = ioctl_enum_fmt;
143         iops.vidioc_enum_framesizes = ioctl_enum_framesz;
144         iops.vidioc_g_fmt_vid_cap = ioctl_get_fmt;
145         iops.vidioc_s_fmt_vid_cap = ioctl_set_fmt;
146         iops.vidioc_try_fmt_vid_cap = ioctl_try_fmt;
147         iops.vidioc_g_parm = ioctl_get_parm;
148         iops.vidioc_s_parm = ioctl_set_parm;
149         iops.vidioc_queryctrl = ioctl_queryctl;
150         iops.vidioc_reqbufs = vb2_ioctl_reqbufs;
151         iops.vidioc_create_bufs = vb2_ioctl_create_bufs;
152         iops.vidioc_querybuf = vb2_ioctl_querybuf;
153         iops.vidioc_qbuf = vb2_ioctl_qbuf;
154         iops.vidioc_dqbuf = vb2_ioctl_dqbuf;
155         iops.vidioc_expbuf = vb2_ioctl_expbuf;
156         iops.vidioc_streamon = vb2_ioctl_streamon;
157         iops.vidioc_streamoff = vb2_ioctl_streamoff;
158         iops.vidioc_log_status = v4l2_ctrl_log_status;
159         iops.vidioc_subscribe_event = v4l2_ctrl_subscribe_event;
160         iops.vidioc_unsubscribe_event = v4l2_event_unsubscribe;
161
162         if((res = video_register_device(vdev, VFL_TYPE_GRABBER, -1)) != 0) {
163                 printk(KERN_ALERT "vdummy: failed to register video device\n");
164                 v4l2_device_unregister(&v4l2_dev);
165                 video_device_release(vdev);
166                 return res;
167         }
168
169         hrtimer_init(&timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
170         timer.function = timer_func;
171
172         if((res = update_frame(640, 480)) != 0) {
173                 video_unregister_device(vdev);
174                 v4l2_device_unregister(&v4l2_dev);
175                 video_device_release(vdev);
176                 return res;
177         }
178
179         printk(KERN_INFO "vdummy: initialized\n");
180         return 0;
181 }
182
183 static void shutdown(void)
184 {
185         hrtimer_cancel(&timer);
186         video_unregister_device(vdev);
187         v4l2_device_unregister(&v4l2_dev);
188         kfree(frame);
189 }
190
191 static int ioctl_querycap(struct file *file, void *fh, struct v4l2_capability *cap)
192 {
193         strcpy(cap->driver, KBUILD_MODNAME);
194         strcpy(cap->card, "Dummy V4L2 device");
195         strcpy(cap->bus_info, "nobus");
196         cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
197         cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
198         return 0;
199 }
200
201 static int ioctl_enum_fmt(struct file *file, void *fh, struct v4l2_fmtdesc *fdesc)
202 {
203         if(fdesc->index != 0 || fdesc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
204                 return -EINVAL;
205         }
206         fdesc->flags = 0;
207         strcpy(fdesc->description, "RGB24");
208         fdesc->pixelformat = V4L2_PIX_FMT_RGB24;
209         return 0;
210 }
211
212 static int ioctl_enum_framesz(struct file *file, void *fh, struct v4l2_frmsizeenum *fsz)
213 {
214         if(fsz->index != 0) {
215                 return -EINVAL;
216         }
217         fsz->type = V4L2_FRMSIZE_TYPE_CONTINUOUS;
218         fsz->stepwise.min_width = fsz->stepwise.min_height = 1;
219         fsz->stepwise.max_width = fsz->stepwise.max_height = 8192;
220         fsz->stepwise.step_width = fsz->stepwise.step_height = 1;
221         return 0;
222 }
223
224 static int ioctl_get_fmt(struct file *file, void *fh, struct v4l2_format *fmt)
225 {
226         if(fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
227                 return -EINVAL;
228         }
229         fmt->fmt.pix.width = width;
230         fmt->fmt.pix.height = height;
231         fmt->fmt.pix.pixelformat = V4L2_PIX_FMT_RGB24;
232         fmt->fmt.pix.field = V4L2_FIELD_NONE;
233         fmt->fmt.pix.bytesperline = 0;
234         fmt->fmt.pix.sizeimage = frame_size;
235         fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SRGB;
236         fmt->fmt.pix.priv = 0;
237         fmt->fmt.pix.flags = 0;
238         fmt->fmt.pix.ycbcr_enc = 0;
239         fmt->fmt.pix.quantization = V4L2_QUANTIZATION_FULL_RANGE;
240         fmt->fmt.pix.xfer_func = V4L2_XFER_FUNC_NONE;
241         return 0;
242 }
243
244 static int ioctl_set_fmt(struct file *file, void *fh, struct v4l2_format *fmt)
245 {
246         int res;
247
248         if(fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
249                 return -EINVAL;
250         }
251         if((res = update_frame(fmt->fmt.pix.width, fmt->fmt.pix.height)) != 0) {
252                 return res;
253         }
254         return ioctl_get_fmt(file, fh, fmt);
255 }
256
257 static int ioctl_try_fmt(struct file *file, void *fh, struct v4l2_format *fmt)
258 {
259         if(fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
260                 return -EINVAL;
261         }
262         if(fmt->fmt.pix.width <= 0)
263                 fmt->fmt.pix.width = width;
264         if(fmt->fmt.pix.height <= 0)
265                 fmt->fmt.pix.height = height;
266         fmt->fmt.pix.pixelformat = V4L2_PIX_FMT_RGB24;
267         fmt->fmt.pix.field = V4L2_FIELD_NONE;
268         fmt->fmt.pix.bytesperline = 0;
269         fmt->fmt.pix.sizeimage = frame_size;
270         fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SRGB;
271         fmt->fmt.pix.priv = 0;
272         fmt->fmt.pix.flags = 0;
273         fmt->fmt.pix.ycbcr_enc = 0;
274         fmt->fmt.pix.quantization = V4L2_QUANTIZATION_FULL_RANGE;
275         fmt->fmt.pix.xfer_func = V4L2_XFER_FUNC_NONE;
276         return 0;
277 }
278
279 static int ioctl_get_parm(struct file *file, void *fh, struct v4l2_streamparm *parm)
280 {
281         if(parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
282                 return -EINVAL;
283         }
284         parm->parm.capture.capability = 0;
285         parm->parm.capture.capturemode = 0;
286         parm->parm.capture.timeperframe.numerator = 1;
287         parm->parm.capture.timeperframe.denominator = framerate;
288         parm->parm.capture.extendedmode = 0;
289         parm->parm.capture.readbuffers = 1;
290         return 0;
291 }
292
293 static int ioctl_set_parm(struct file *file, void *fh, struct v4l2_streamparm *parm)
294 {
295         int new_framerate;
296         if(parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
297                 return -EINVAL;
298         }
299
300         new_framerate = parm->parm.capture.timeperframe.denominator / parm->parm.capture.timeperframe.numerator;
301         framerate = new_framerate < 1 ? 1 : (new_framerate > 60 ? 60 : new_framerate);
302
303         return ioctl_get_parm(file, fh, parm);
304 }
305
306 static int ioctl_queryctl(struct file *file, void *fh, struct v4l2_queryctrl *ctl)
307 {
308         return -EINVAL;
309 }
310
311 static int queue_setup(struct vb2_queue *vbq, unsigned int *nbuf,
312                 unsigned int *nplanes, unsigned int *sizes, ALLOC_CTX_TYPE **alloc_ctx)
313 {
314         if(vbq->num_buffers + *nbuf < 2) {
315                 *nbuf = 2 - vbq->num_buffers;
316         }
317
318         *nplanes = 1;
319         sizes[0] = frame_size;
320         alloc_ctx[0] = 0;
321         return 0;
322 }
323
324 static int buf_prepare(struct vb2_buffer *vb)
325 {
326         if(vb2_plane_size(vb, 0) < frame_size) {
327                 printk(KERN_ALERT "vdummy: buffer too small\n");
328                 return -EINVAL;
329         }
330         return 0;
331 }
332
333 static void buf_queue(struct vb2_buffer *vb)
334 {
335         struct buffer *buf = (struct buffer*)((char*)vb - offsetof(struct buffer, vb));
336
337         spin_lock(&blist_lock);
338         printk(KERN_INFO "vdummy: enqueue buffer\n");
339         if(buflist) {
340                 buflist_tail->next = buf;
341                 buflist_tail = buf;
342         } else {
343                 buflist = buflist_tail = buf;
344         }
345         buf->next = 0;
346         spin_unlock(&blist_lock);
347 }
348
349 static void clear_queue(enum vb2_buffer_state st)
350 {
351         struct buffer *list;
352
353         printk(KERN_INFO "vdummy: clear_queue\n");
354
355         spin_lock(&blist_lock);
356         list = buflist;
357         buflist = buflist_tail = 0;
358         spin_unlock(&blist_lock);
359
360         while(list) {
361                 struct buffer *buf = list;
362                 list = list->next;
363                 vb2_buffer_done(&buf->vb.vb2_buf, st);
364         }
365 }
366
367 static int start_streaming(struct vb2_queue *vbq, unsigned int count)
368 {
369         ktime_t frame_interval;
370
371         streaming = 1;
372         seqno = 0;
373
374         /* calculate frame interval in nanoseconds */
375         if(framerate <= 1) {
376                 frame_interval = ktime_set(1, 0);
377         } else {
378                 frame_interval = ktime_set(0, 1000000000ul / (unsigned long)framerate);
379         }
380
381         printk(KERN_INFO "vdummy: start streaming (interval: %ld ms)\n",
382                         (long)ktime_to_ms(frame_interval));
383         hrtimer_start(&timer, frame_interval, HRTIMER_MODE_REL);
384         return 0;
385 }
386
387 static void stop_streaming(struct vb2_queue *vbq)
388 {
389         printk(KERN_INFO "vdummy: stop streaming\n");
390         hrtimer_cancel(&timer);
391
392         streaming = 0;
393         clear_queue(VB2_BUF_STATE_ERROR);
394 }
395
396 static int update_frame(int xsz, int ysz)
397 {
398         unsigned char *tmp;
399         int i, j;
400         int new_fsz = xsz * ysz * 3;
401
402         if(xsz == width && ysz == height) {
403                 return 0;
404         }
405
406         printk(KERN_INFO "vdummy: update_frame(%d, %d)\n", xsz, ysz);
407
408         if(!frame || frame_size < new_fsz) {
409                 if(!(tmp = kmalloc(new_fsz, GFP_KERNEL))) {
410                         return -ENOMEM;
411                 }
412                 kfree(frame);
413                 frame_size = new_fsz;
414                 width = xsz;
415                 height = ysz;
416                 frame = tmp;
417         }
418
419         tmp = frame;
420         for(i=0; i<height; i++) {
421                 for(j=0; j<width; j++) {
422                         int val = i ^ j;
423                         *tmp++ = val;
424                         *tmp++ = (val << 1) & 0xff;
425                         *tmp++ = (val << 2) & 0xff;
426                 }
427         }
428
429         return 0;
430 }
431
432 static int gen_frame(void)
433 {
434         struct buffer *buf = 0;
435         unsigned long flags;
436
437         printk(KERN_INFO "vdummy: gen_frame\n");
438
439         spin_lock_irqsave(&blist_lock, flags);
440         if(buflist) {
441                 buf = buflist;
442                 buflist = buflist->next;
443                 if(!buflist) buflist_tail = 0;
444         }
445         spin_unlock_irqrestore(&blist_lock, flags);
446
447         if(buf) {
448                 memcpy(vb2_plane_vaddr(&buf->vb.vb2_buf, 0), frame, frame_size);
449
450                 buf->vb.vb2_buf.timestamp = ktime_get_ns();
451                 printk(KERN_INFO "vdummy: copying frame: %d ts: %lu\n", seqno,
452                                 (unsigned long)buf->vb.vb2_buf.timestamp);
453                 buf->vb.sequence = seqno++;
454                 buf->vb.field = V4L2_FIELD_NONE;
455                 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_DONE);
456                 vb2_set_plane_payload(&buf->vb.vb2_buf, 0, frame_size);
457         }
458         return 0;
459 }
460
461 enum hrtimer_restart timer_func(struct hrtimer *timer)
462 {
463         printk(KERN_INFO "vdummy: timer!\n");
464         //gen_frame();
465         return streaming ? HRTIMER_RESTART : HRTIMER_NORESTART;
466 }