Merge branch 'master' of goat:git/vdummy
[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-vmalloc.h>
11 #include <media/v4l2-dev.h>
12 #include <media/v4l2-ioctl.h>
13 #include <media/v4l2-device.h>
14 #include <media/v4l2-ctrls.h>
15 #include <media/v4l2-event.h>
16
17 struct buffer {
18         struct vb2_v4l2_buffer vb;
19         struct buffer *next;
20 };
21
22 static int init(void);
23 static void shutdown(void);
24
25 static int ioctl_querycap(struct file *file, void *fh, struct v4l2_capability *cap);
26 static int ioctl_enum_fmt(struct file *file, void *fh, struct v4l2_fmtdesc *fdesc);
27 static int ioctl_enum_framesz(struct file *file, void *fh, struct v4l2_frmsizeenum *fsz);
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, struct device **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("Dummy V4L2 device");
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_enum_framesizes = ioctl_enum_framesz;
136         iops.vidioc_g_fmt_vid_cap = ioctl_get_fmt;
137         iops.vidioc_s_fmt_vid_cap = ioctl_set_fmt;
138         iops.vidioc_try_fmt_vid_cap = ioctl_try_fmt;
139         iops.vidioc_g_parm = ioctl_get_parm;
140         iops.vidioc_s_parm = ioctl_set_parm;
141         iops.vidioc_queryctrl = ioctl_queryctl;
142         iops.vidioc_reqbufs = vb2_ioctl_reqbufs;
143         iops.vidioc_create_bufs = vb2_ioctl_create_bufs;
144         iops.vidioc_querybuf = vb2_ioctl_querybuf;
145         iops.vidioc_qbuf = vb2_ioctl_qbuf;
146         iops.vidioc_dqbuf = vb2_ioctl_dqbuf;
147         iops.vidioc_expbuf = vb2_ioctl_expbuf;
148         iops.vidioc_streamon = vb2_ioctl_streamon;
149         iops.vidioc_streamoff = vb2_ioctl_streamoff;
150         iops.vidioc_log_status = v4l2_ctrl_log_status;
151         iops.vidioc_subscribe_event = v4l2_ctrl_subscribe_event;
152         iops.vidioc_unsubscribe_event = v4l2_event_unsubscribe;
153
154         if((res = video_register_device(vdev, VFL_TYPE_GRABBER, -1)) != 0) {
155                 printk(KERN_ALERT "vdummy: failed to register video device\n");
156                 v4l2_device_unregister(&v4l2_dev);
157                 video_device_release(vdev);
158                 return res;
159         }
160
161         hrtimer_init(&timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
162         timer.function = timer_func;
163
164         if((res = update_frame(640, 480)) != 0) {
165                 video_unregister_device(vdev);
166                 v4l2_device_unregister(&v4l2_dev);
167                 video_device_release(vdev);
168                 return res;
169         }
170
171         printk(KERN_INFO "vdummy: initialized\n");
172         return 0;
173 }
174
175 static void shutdown(void)
176 {
177         hrtimer_cancel(&timer);
178         video_unregister_device(vdev);
179         v4l2_device_unregister(&v4l2_dev);
180         kfree(frame);
181 }
182
183 static int ioctl_querycap(struct file *file, void *fh, struct v4l2_capability *cap)
184 {
185         strcpy(cap->driver, KBUILD_MODNAME);
186         strcpy(cap->card, "Dummy V4L2 device");
187         strcpy(cap->bus_info, "nobus");
188         cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
189         cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
190         return 0;
191 }
192
193 static int ioctl_enum_fmt(struct file *file, void *fh, struct v4l2_fmtdesc *fdesc)
194 {
195         if(fdesc->index != 0 || fdesc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
196                 return -EINVAL;
197         }
198         fdesc->flags = 0;
199         strcpy(fdesc->description, "RGB24");
200         fdesc->pixelformat = V4L2_PIX_FMT_RGB24;
201         return 0;
202 }
203
204 static int ioctl_enum_framesz(struct file *file, void *fh, struct v4l2_frmsizeenum *fsz)
205 {
206         if(fsz->index != 0) {
207                 return -EINVAL;
208         }
209         fsz->type = V4L2_FRMSIZE_TYPE_CONTINUOUS;
210         fsz->stepwise.min_width = fsz->stepwise.min_height = 1;
211         fsz->stepwise.max_width = fsz->stepwise.max_height = 8192;
212         fsz->stepwise.step_width = fsz->stepwise.step_height = 1;
213         return 0;
214 }
215
216 static int ioctl_get_fmt(struct file *file, void *fh, struct v4l2_format *fmt)
217 {
218         if(fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
219                 return -EINVAL;
220         }
221         fmt->fmt.pix.width = width;
222         fmt->fmt.pix.height = height;
223         fmt->fmt.pix.pixelformat = V4L2_PIX_FMT_RGB24;
224         fmt->fmt.pix.field = V4L2_FIELD_NONE;
225         fmt->fmt.pix.bytesperline = 0;
226         fmt->fmt.pix.sizeimage = frame_size;
227         fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SRGB;
228         fmt->fmt.pix.priv = 0;
229         fmt->fmt.pix.flags = 0;
230         fmt->fmt.pix.ycbcr_enc = 0;
231         fmt->fmt.pix.quantization = V4L2_QUANTIZATION_FULL_RANGE;
232         fmt->fmt.pix.xfer_func = V4L2_XFER_FUNC_NONE;
233         return 0;
234 }
235
236 static int ioctl_set_fmt(struct file *file, void *fh, struct v4l2_format *fmt)
237 {
238         int res;
239
240         if(fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
241                 return -EINVAL;
242         }
243         if((res = update_frame(fmt->fmt.pix.width, fmt->fmt.pix.height)) != 0) {
244                 return res;
245         }
246         return ioctl_get_fmt(file, fh, fmt);
247 }
248
249 static int ioctl_try_fmt(struct file *file, void *fh, struct v4l2_format *fmt)
250 {
251         if(fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
252                 return -EINVAL;
253         }
254         if(fmt->fmt.pix.width <= 0)
255                 fmt->fmt.pix.width = width;
256         if(fmt->fmt.pix.height <= 0)
257                 fmt->fmt.pix.height = height;
258         fmt->fmt.pix.pixelformat = V4L2_PIX_FMT_RGB24;
259         fmt->fmt.pix.field = V4L2_FIELD_NONE;
260         fmt->fmt.pix.bytesperline = 0;
261         fmt->fmt.pix.sizeimage = frame_size;
262         fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SRGB;
263         fmt->fmt.pix.priv = 0;
264         fmt->fmt.pix.flags = 0;
265         fmt->fmt.pix.ycbcr_enc = 0;
266         fmt->fmt.pix.quantization = V4L2_QUANTIZATION_FULL_RANGE;
267         fmt->fmt.pix.xfer_func = V4L2_XFER_FUNC_NONE;
268         return 0;
269 }
270
271 static int ioctl_get_parm(struct file *file, void *fh, struct v4l2_streamparm *parm)
272 {
273         if(parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
274                 return -EINVAL;
275         }
276         parm->parm.capture.capability = 0;
277         parm->parm.capture.capturemode = 0;
278         parm->parm.capture.timeperframe.numerator = 1;
279         parm->parm.capture.timeperframe.denominator = framerate;
280         parm->parm.capture.extendedmode = 0;
281         parm->parm.capture.readbuffers = 1;
282         return 0;
283 }
284
285 static int ioctl_set_parm(struct file *file, void *fh, struct v4l2_streamparm *parm)
286 {
287         int new_framerate;
288         if(parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
289                 return -EINVAL;
290         }
291
292         new_framerate = parm->parm.capture.timeperframe.denominator / parm->parm.capture.timeperframe.numerator;
293         framerate = new_framerate < 1 ? 1 : (new_framerate > 60 ? 60 : new_framerate);
294
295         return ioctl_get_parm(file, fh, parm);
296 }
297
298 static int ioctl_queryctl(struct file *file, void *fh, struct v4l2_queryctrl *ctl)
299 {
300         return -EINVAL;
301 }
302
303 static int queue_setup(struct vb2_queue *vbq, unsigned int *nbuf,
304                 unsigned int *nplanes, unsigned int *sizes, struct device **alloc_ctx)
305 {
306         if(vbq->num_buffers + *nbuf < 2) {
307                 *nbuf = 2 - vbq->num_buffers;
308         }
309
310         *nplanes = 1;
311         sizes[0] = frame_size;
312         alloc_ctx[0] = 0;
313         return 0;
314 }
315
316 static int buf_prepare(struct vb2_buffer *vb)
317 {
318         if(vb2_plane_size(vb, 0) < frame_size) {
319                 printk(KERN_ALERT "vdummy: buffer too small\n");
320                 return -EINVAL;
321         }
322         return 0;
323 }
324
325 static void buf_queue(struct vb2_buffer *vb)
326 {
327         struct buffer *buf = (struct buffer*)((char*)vb - offsetof(struct buffer, vb));
328
329         spin_lock(&blist_lock);
330         printk(KERN_INFO "vdummy: enqueue buffer\n");
331         if(buflist) {
332                 buflist_tail->next = buf;
333                 buflist_tail = buf;
334         } else {
335                 buflist = buflist_tail = buf;
336         }
337         buf->next = 0;
338         spin_unlock(&blist_lock);
339 }
340
341 static void clear_queue(enum vb2_buffer_state st)
342 {
343         struct buffer *list;
344
345         printk(KERN_INFO "vdummy: clear_queue\n");
346
347         spin_lock(&blist_lock);
348         list = buflist;
349         buflist = buflist_tail = 0;
350         spin_unlock(&blist_lock);
351
352         while(list) {
353                 struct buffer *buf = list;
354                 list = list->next;
355                 vb2_buffer_done(&buf->vb.vb2_buf, st);
356         }
357 }
358
359 static int start_streaming(struct vb2_queue *vbq, unsigned int count)
360 {
361         ktime_t frame_interval;
362
363         streaming = 1;
364         seqno = 0;
365
366         /* calculate frame interval in nanoseconds */
367         if(framerate <= 1) {
368                 frame_interval = ktime_set(1, 0);
369         } else {
370                 frame_interval = ktime_set(0, 1000000000ul / (unsigned long)framerate);
371         }
372
373         printk(KERN_INFO "vdummy: start streaming (interval: %ld ms)\n",
374                         (long)ktime_to_ms(frame_interval));
375         hrtimer_start(&timer, frame_interval, HRTIMER_MODE_REL);
376         return 0;
377 }
378
379 static void stop_streaming(struct vb2_queue *vbq)
380 {
381         printk(KERN_INFO "vdummy: stop streaming\n");
382         hrtimer_cancel(&timer);
383
384         streaming = 0;
385         clear_queue(VB2_BUF_STATE_ERROR);
386 }
387
388 static int update_frame(int xsz, int ysz)
389 {
390         unsigned char *tmp;
391         int i, j;
392         int new_fsz = xsz * ysz * 3;
393
394         if(xsz == width && ysz == height) {
395                 return 0;
396         }
397
398         printk(KERN_INFO "vdummy: update_frame(%d, %d)\n", xsz, ysz);
399
400         if(!frame || frame_size < new_fsz) {
401                 if(!(tmp = kmalloc(new_fsz, GFP_KERNEL))) {
402                         return -ENOMEM;
403                 }
404                 kfree(frame);
405                 frame_size = new_fsz;
406                 width = xsz;
407                 height = ysz;
408                 frame = tmp;
409         }
410
411         tmp = frame;
412         for(i=0; i<height; i++) {
413                 for(j=0; j<width; j++) {
414                         int val = i ^ j;
415                         *tmp++ = val;
416                         *tmp++ = (val << 1) & 0xff;
417                         *tmp++ = (val << 2) & 0xff;
418                 }
419         }
420
421         return 0;
422 }
423
424 static int gen_frame(void)
425 {
426         struct buffer *buf = 0;
427         unsigned long flags;
428
429         printk(KERN_INFO "vdummy: gen_frame\n");
430
431         spin_lock_irqsave(&blist_lock, flags);
432         if(buflist) {
433                 buf = buflist;
434                 buflist = buflist->next;
435                 if(!buflist) buflist_tail = 0;
436         }
437         spin_unlock_irqrestore(&blist_lock, flags);
438
439         if(buf) {
440                 memcpy(vb2_plane_vaddr(&buf->vb.vb2_buf, 0), frame, frame_size);
441
442                 buf->vb.vb2_buf.timestamp = ktime_get_ns();
443                 printk(KERN_INFO "vdummy: copying frame: %d ts: %lu\n", seqno,
444                                 (unsigned long)buf->vb.vb2_buf.timestamp);
445                 buf->vb.sequence = seqno++;
446                 buf->vb.field = V4L2_FIELD_NONE;
447                 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_DONE);
448                 vb2_set_plane_payload(&buf->vb.vb2_buf, 0, frame_size);
449         }
450         return 0;
451 }
452
453 enum hrtimer_restart timer_func(struct hrtimer *timer)
454 {
455         printk(KERN_INFO "vdummy: timer!\n");
456         //gen_frame();
457         return streaming ? HRTIMER_RESTART : HRTIMER_NORESTART;
458 }