foo
[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("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_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         hrtimer_cancel(&timer);
177         video_unregister_device(vdev);
178         v4l2_device_unregister(&v4l2_dev);
179         kfree(frame);
180 }
181
182 static int ioctl_querycap(struct file *file, void *fh, struct v4l2_capability *cap)
183 {
184         strcpy(cap->driver, KBUILD_MODNAME);
185         strcpy(cap->card, "Dummy V4L2 device");
186         strcpy(cap->bus_info, "nobus");
187         cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
188         cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
189         return 0;
190 }
191
192 static int ioctl_enum_fmt(struct file *file, void *fh, struct v4l2_fmtdesc *fdesc)
193 {
194         if(fdesc->index != 0 || fdesc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
195                 return -EINVAL;
196         }
197         fdesc->flags = 0;
198         strcpy(fdesc->description, "RGB24");
199         fdesc->pixelformat = V4L2_PIX_FMT_RGB24;
200         return 0;
201 }
202
203 static int ioctl_get_fmt(struct file *file, void *fh, struct v4l2_format *fmt)
204 {
205         if(fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
206                 return -EINVAL;
207         }
208         fmt->fmt.pix.width = width;
209         fmt->fmt.pix.height = height;
210         fmt->fmt.pix.pixelformat = V4L2_PIX_FMT_RGB24;
211         fmt->fmt.pix.field = V4L2_FIELD_NONE;
212         fmt->fmt.pix.bytesperline = 0;
213         fmt->fmt.pix.sizeimage = frame_size;
214         fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SRGB;
215         fmt->fmt.pix.priv = 0;
216         fmt->fmt.pix.flags = 0;
217         fmt->fmt.pix.ycbcr_enc = 0;
218         fmt->fmt.pix.quantization = V4L2_QUANTIZATION_FULL_RANGE;
219         fmt->fmt.pix.xfer_func = V4L2_XFER_FUNC_NONE;
220         return 0;
221 }
222
223 static int ioctl_set_fmt(struct file *file, void *fh, struct v4l2_format *fmt)
224 {
225         int res;
226
227         if(fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
228                 return -EINVAL;
229         }
230         if((res = update_frame(fmt->fmt.pix.width, fmt->fmt.pix.height)) != 0) {
231                 return res;
232         }
233         return ioctl_get_fmt(file, fh, fmt);
234 }
235
236 static int ioctl_try_fmt(struct file *file, void *fh, struct v4l2_format *fmt)
237 {
238         if(fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
239                 return -EINVAL;
240         }
241         if(fmt->fmt.pix.width <= 0)
242                 fmt->fmt.pix.width = width;
243         if(fmt->fmt.pix.height <= 0)
244                 fmt->fmt.pix.height = height;
245         fmt->fmt.pix.pixelformat = V4L2_PIX_FMT_RGB24;
246         fmt->fmt.pix.field = V4L2_FIELD_NONE;
247         fmt->fmt.pix.bytesperline = 0;
248         fmt->fmt.pix.sizeimage = frame_size;
249         fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SRGB;
250         fmt->fmt.pix.priv = 0;
251         fmt->fmt.pix.flags = 0;
252         fmt->fmt.pix.ycbcr_enc = 0;
253         fmt->fmt.pix.quantization = V4L2_QUANTIZATION_FULL_RANGE;
254         fmt->fmt.pix.xfer_func = V4L2_XFER_FUNC_NONE;
255         return 0;
256 }
257
258 static int ioctl_get_parm(struct file *file, void *fh, struct v4l2_streamparm *parm)
259 {
260         if(parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
261                 return -EINVAL;
262         }
263         parm->parm.capture.capability = 0;
264         parm->parm.capture.capturemode = 0;
265         parm->parm.capture.timeperframe.numerator = 1;
266         parm->parm.capture.timeperframe.denominator = framerate;
267         parm->parm.capture.extendedmode = 0;
268         parm->parm.capture.readbuffers = 1;
269         return 0;
270 }
271
272 static int ioctl_set_parm(struct file *file, void *fh, struct v4l2_streamparm *parm)
273 {
274         int new_framerate;
275         if(parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
276                 return -EINVAL;
277         }
278
279         new_framerate = parm->parm.capture.timeperframe.denominator / parm->parm.capture.timeperframe.numerator;
280         framerate = new_framerate < 1 ? 1 : (new_framerate > 60 ? 60 : new_framerate);
281
282         return ioctl_get_parm(file, fh, parm);
283 }
284
285 static int ioctl_queryctl(struct file *file, void *fh, struct v4l2_queryctrl *ctl)
286 {
287         return -EINVAL;
288 }
289
290 static int queue_setup(struct vb2_queue *vbq, unsigned int *nbuf,
291                 unsigned int *nplanes, unsigned int *sizes, void **alloc_ctx)
292 {
293         if(vbq->num_buffers + *nbuf < 2) {
294                 *nbuf = 2 - vbq->num_buffers;
295         }
296
297         *nplanes = 1;
298         sizes[0] = frame_size;
299         alloc_ctx[0] = 0;
300         return 0;
301 }
302
303 static int buf_prepare(struct vb2_buffer *vb)
304 {
305         if(vb2_plane_size(vb, 0) < frame_size) {
306                 printk(KERN_ALERT "vdummy: buffer too small\n");
307                 return -EINVAL;
308         }
309         return 0;
310 }
311
312 static void buf_queue(struct vb2_buffer *vb)
313 {
314         struct buffer *buf = (struct buffer*)((char*)vb - offsetof(struct buffer, vb));
315
316         spin_lock(&blist_lock);
317         if(buflist) {
318                 buflist_tail->next = buf;
319                 buflist_tail = buf;
320         } else {
321                 buflist = buflist_tail = buf;
322         }
323         buf->next = 0;
324         spin_unlock(&blist_lock);
325 }
326
327 static void clear_queue(enum vb2_buffer_state st)
328 {
329         spin_lock(&blist_lock);
330         while(buflist) {
331                 struct buffer *buf = buflist;
332                 buflist = buflist->next;
333                 vb2_buffer_done(&buf->vb.vb2_buf, st);
334         }
335         buflist = buflist_tail = 0;
336         spin_unlock(&blist_lock);
337 }
338
339 static int start_streaming(struct vb2_queue *vbq, unsigned int count)
340 {
341         ktime_t frame_interval;
342
343         streaming = 1;
344         seqno = 0;
345
346         /* calculate frame interval in nanoseconds */
347         if(framerate <= 1) {
348                 frame_interval = ktime_set(1, 0);
349         } else {
350                 frame_interval = ktime_set(0, 1000000000ul / (unsigned long)framerate);
351         }
352
353         hrtimer_start(&timer, frame_interval, HRTIMER_MODE_REL);
354         return 0;
355 }
356
357 static void stop_streaming(struct vb2_queue *vbq)
358 {
359         hrtimer_cancel(&timer);
360
361         streaming = 0;
362         clear_queue(VB2_BUF_STATE_ERROR);
363 }
364
365 static int update_frame(int xsz, int ysz)
366 {
367         unsigned char *tmp;
368         int i, j;
369         int new_fsz = xsz * ysz * 3;
370
371         if(xsz == width && ysz == height) {
372                 return 0;
373         }
374
375         printk(KERN_INFO "vdummy: update_frame(%d, %d)\n", xsz, ysz);
376
377         if(!frame || frame_size < new_fsz) {
378                 if(!(tmp = kmalloc(new_fsz, GFP_KERNEL))) {
379                         return -ENOMEM;
380                 }
381                 kfree(frame);
382                 frame_size = new_fsz;
383                 width = xsz;
384                 height = ysz;
385                 frame = tmp;
386         }
387
388         tmp = frame;
389         for(i=0; i<height; i++) {
390                 for(j=0; j<width; j++) {
391                         int val = i ^ j;
392                         *tmp++ = val;
393                         *tmp++ = (val << 1) & 0xff;
394                         *tmp++ = (val << 2) & 0xff;
395                 }
396         }
397
398         return 0;
399 }
400
401 static int gen_frame(void)
402 {
403         struct buffer *buf = 0;
404
405         spin_lock(&blist_lock);
406         if(buflist) {
407                 buf = buflist;
408                 buflist = buflist->next;
409                 if(!buflist) buflist_tail = 0;
410         }
411         spin_unlock(&blist_lock);
412
413         if(buf) {
414                 mutex_lock(&mutex);
415                 memcpy(vb2_plane_vaddr(&buf->vb.vb2_buf, 0), frame, frame_size);
416
417                 buf->vb.vb2_buf.timestamp = ktime_get_ns();
418                 printk(KERN_INFO "vdummy: copying frame: %d ts: %lu\n", seqno,
419                                 (unsigned long)buf->vb.vb2_buf.timestamp);
420                 buf->vb.sequence = seqno++;
421                 buf->vb.field = V4L2_FIELD_NONE;
422                 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_DONE);
423                 vb2_set_plane_payload(&buf->vb.vb2_buf, 0, frame_size);
424                 mutex_unlock(&mutex);
425         }
426         return 0;
427 }
428
429 enum hrtimer_restart timer_func(struct hrtimer *timer)
430 {
431         gen_frame();
432         return streaming ? HRTIMER_RESTART : HRTIMER_NORESTART;
433 }