fixed a couple of omissions, untested
[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 <asm/uaccess.h>
7 #include <linux/videodev2.h>
8 #include <media/videobuf2-v4l2.h>
9 #include <media/videobuf2-vmalloc.h>
10 #include <media/v4l2-dev.h>
11 #include <media/v4l2-ioctl.h>
12 #include <media/v4l2-device.h>
13 #include <media/v4l2-ctrls.h>
14 #include <media/v4l2-event.h>
15
16 struct buffer {
17         struct vb2_buffer vb;
18         struct buffer *next;
19 };
20
21 static int init(void);
22 static void shutdown(void);
23
24 static int ioctl_querycap(struct file *file, void *fh, struct v4l2_capability *cap);
25 static int ioctl_enum_fmt(struct file *file, void *fh, struct v4l2_fmtdesc *fdesc);
26 static int ioctl_get_fmt(struct file *file, void *fh, struct v4l2_format *fmt);
27 static int ioctl_set_fmt(struct file *file, void *fh, struct v4l2_format *fmt);
28 static int ioctl_get_parm(struct file *file, void *fh, struct v4l2_streamparm *fmt);
29 static int ioctl_set_parm(struct file *file, void *fh, struct v4l2_streamparm *fmt);
30 static int ioctl_queryctl(struct file *file, void *fh, struct v4l2_queryctrl *ctl);
31
32 static int queue_setup(struct vb2_queue *vbq, unsigned int *nbuf,
33                 unsigned int *nplanes, unsigned int *sizes, void **alloc_ctx);
34 static int buf_prepare(struct vb2_buffer *vb);
35 static void buf_queue(struct vb2_buffer *vb);
36 static int start_streaming(struct vb2_queue *vbq, unsigned int count);
37 static void stop_streaming(struct vb2_queue *vbq);
38
39
40 static int update_frame(int xsz, int ysz);
41 static int gen_frame(void);
42
43
44 module_init(init);
45 module_exit(shutdown);
46
47 MODULE_LICENSE("GPL");
48 MODULE_AUTHOR("John Tsiombikas");
49 MODULE_DESCRIPTION("v4l2 test module");
50
51 static struct mutex mutex;
52 static struct video_device *vdev;
53
54 static struct v4l2_device v4l2_dev;
55 static struct v4l2_file_operations fops;
56 static struct v4l2_ioctl_ops iops;
57 static struct vb2_queue vbq;
58 static struct vb2_ops vbops;
59 static int width, height;
60 static unsigned char *frame;
61 static int frame_size;
62
63 static int streaming;
64 static spinlock_t blist_lock;
65 static struct buffer *buflist, *buflist_tail;
66 static int seqno;
67
68
69 static int init(void)
70 {
71         int res;
72
73         strcpy(v4l2_dev.name, "vdummy");
74         if((res = v4l2_device_register(0, &v4l2_dev)) != 0) {
75                 return res;
76         }
77
78         mutex_init(&mutex);
79         spin_lock_init(&blist_lock);
80
81         vbops.queue_setup = queue_setup;
82         vbops.buf_prepare = buf_prepare;
83         vbops.buf_queue = buf_queue;
84         vbops.start_streaming = start_streaming;
85         vbops.stop_streaming = stop_streaming;
86         vbops.wait_prepare = vb2_ops_wait_prepare;
87         vbops.wait_finish = vb2_ops_wait_finish;
88
89         vbq.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
90         vbq.io_modes = VB2_MMAP | VB2_READ;
91         vbq.buf_struct_size = sizeof *buflist;
92         vbq.ops = &vbops;
93         vbq.mem_ops = &vb2_vmalloc_memops;
94         vbq.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
95         vbq.min_buffers_needed = 1;
96         vbq.lock = &mutex;
97         vbq.gfp_flags = GFP_KERNEL;
98
99         if((res = vb2_queue_init(&vbq)) != 0) {
100                 printk(KERN_ALERT "vdummy: failed to initialize videobuf2 queue\n");
101                 v4l2_device_unregister(&v4l2_dev);
102                 return res;
103         }
104
105         if(!(vdev = video_device_alloc())) {
106                 return -ENOMEM;
107         }
108         memset(vdev, 0, sizeof *vdev);
109         vdev->release = video_device_release;
110         strcpy(vdev->name, KBUILD_MODNAME);
111         vdev->fops = &fops;
112         vdev->ioctl_ops = &iops;
113         vdev->vfl_type = VFL_TYPE_GRABBER;
114         vdev->vfl_dir = VFL_DIR_RX;
115         vdev->v4l2_dev = &v4l2_dev;
116         vdev->lock = &mutex;
117         vdev->queue = &vbq;
118
119         fops.owner = THIS_MODULE;
120         fops.open = v4l2_fh_open;
121         fops.release = vb2_fop_release;
122         fops.read = vb2_fop_read;
123         fops.mmap = vb2_fop_mmap;
124         fops.poll = vb2_fop_poll;
125         fops.unlocked_ioctl = video_ioctl2;
126
127         iops.vidioc_querycap = ioctl_querycap;
128         iops.vidioc_enum_fmt_vid_cap = ioctl_enum_fmt;
129         iops.vidioc_g_fmt_vid_cap = ioctl_get_fmt;
130         iops.vidioc_s_fmt_vid_cap = ioctl_set_fmt;
131         iops.vidioc_g_parm = ioctl_get_parm;
132         iops.vidioc_s_parm = ioctl_set_parm;
133         iops.vidioc_queryctrl = ioctl_queryctl;
134         iops.vidioc_reqbufs = vb2_ioctl_reqbufs;
135         iops.vidioc_create_bufs = vb2_ioctl_create_bufs;
136         iops.vidioc_querybuf = vb2_ioctl_querybuf;
137         iops.vidioc_qbuf = vb2_ioctl_qbuf;
138         iops.vidioc_dqbuf = vb2_ioctl_dqbuf;
139         iops.vidioc_expbuf = vb2_ioctl_expbuf;
140         iops.vidioc_streamon = vb2_ioctl_streamon;
141         iops.vidioc_streamoff = vb2_ioctl_streamoff;
142         iops.vidioc_log_status = v4l2_ctrl_log_status;
143         iops.vidioc_subscribe_event = v4l2_ctrl_subscribe_event;
144         iops.vidioc_unsubscribe_event = v4l2_event_unsubscribe;
145
146         if((res = video_register_device(vdev, VFL_TYPE_GRABBER, -1)) != 0) {
147                 printk(KERN_ALERT "vdummy: failed to register video device\n");
148                 v4l2_device_unregister(&v4l2_dev);
149                 video_device_release(vdev);
150                 return res;
151         }
152
153         if((res = update_frame(640, 480)) != 0) {
154                 video_unregister_device(vdev);
155                 v4l2_device_unregister(&v4l2_dev);
156                 video_device_release(vdev);
157                 return res;
158         }
159
160         printk(KERN_INFO "vdummy device initialized\n");
161         return 0;
162 }
163
164 static void shutdown(void)
165 {
166         video_unregister_device(vdev);
167         v4l2_device_unregister(&v4l2_dev);
168         kfree(frame);
169 }
170
171 static int ioctl_querycap(struct file *file, void *fh, struct v4l2_capability *cap)
172 {
173         strcpy(cap->driver, KBUILD_MODNAME);
174         strcpy(cap->card, "dummy v4l2 dev");
175         strcpy(cap->bus_info, "nobus");
176         cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
177         cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
178         return 0;
179 }
180
181 static int ioctl_enum_fmt(struct file *file, void *fh, struct v4l2_fmtdesc *fdesc)
182 {
183         if(fdesc->index != 0 || fdesc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
184                 return -EINVAL;
185         }
186         fdesc->flags = 0;
187         strcpy(fdesc->description, "RGB24");
188         fdesc->pixelformat = V4L2_PIX_FMT_RGB24;
189         return 0;
190 }
191
192 static int ioctl_get_fmt(struct file *file, void *fh, struct v4l2_format *fmt)
193 {
194         if(fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
195                 return -EINVAL;
196         }
197         fmt->fmt.pix.width = width;
198         fmt->fmt.pix.height = height;
199         fmt->fmt.pix.pixelformat = V4L2_PIX_FMT_RGB24;
200         fmt->fmt.pix.field = V4L2_FIELD_NONE;
201         fmt->fmt.pix.bytesperline = 0;
202         fmt->fmt.pix.sizeimage = frame_size;
203         fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SRGB;
204         fmt->fmt.pix.priv = 0;
205         fmt->fmt.pix.flags = 0;
206         fmt->fmt.pix.ycbcr_enc = 0;
207         fmt->fmt.pix.quantization = V4L2_QUANTIZATION_FULL_RANGE;
208         fmt->fmt.pix.xfer_func = V4L2_XFER_FUNC_NONE;
209         return 0;
210 }
211
212 static int ioctl_set_fmt(struct file *file, void *fh, struct v4l2_format *fmt)
213 {
214         int res;
215
216         if(fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
217                 return -EINVAL;
218         }
219         if((res = update_frame(fmt->fmt.pix.width, fmt->fmt.pix.height)) != 0) {
220                 return res;
221         }
222         return ioctl_get_fmt(file, fh, fmt);
223 }
224
225 static int ioctl_get_parm(struct file *file, void *fh, struct v4l2_streamparm *parm)
226 {
227         if(parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
228                 return -EINVAL;
229         }
230         parm->parm.capture.capability = 0;
231         parm->parm.capture.capturemode = 0;
232         parm->parm.capture.timeperframe.numerator = 1;
233         parm->parm.capture.timeperframe.denominator = 30;
234         parm->parm.capture.extendedmode = 0;
235         parm->parm.capture.readbuffers = 1;
236         return 0;
237 }
238
239 static int ioctl_set_parm(struct file *file, void *fh, struct v4l2_streamparm *parm)
240 {
241         if(parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
242                 return -EINVAL;
243         }
244         return ioctl_get_parm(file, fh, parm);
245 }
246
247 static int ioctl_queryctl(struct file *file, void *fh, struct v4l2_queryctrl *ctl)
248 {
249         return -EINVAL;
250 }
251
252 static int queue_setup(struct vb2_queue *vbq, unsigned int *nbuf,
253                 unsigned int *nplanes, unsigned int *sizes, void **alloc_ctx)
254 {
255         if(vbq->num_buffers + *nbuf < 2) {
256                 *nbuf = 2 - vbq->num_buffers;
257         }
258
259         *nplanes = 1;
260         sizes[0] = frame_size;
261         alloc_ctx[0] = 0;
262         return 0;
263 }
264
265 static int buf_prepare(struct vb2_buffer *vb)
266 {
267         if(vb2_plane_size(vb, 0) < frame_size) {
268                 printk(KERN_ALERT "vdummy: buffer too small\n");
269                 return -EINVAL;
270         }
271         vb2_set_plane_payload(vb, 0, frame_size);
272         return 0;
273 }
274
275 static void buf_queue(struct vb2_buffer *vb)
276 {
277         struct buffer *buf = (struct buffer*)((char*)vb - offsetof(struct buffer, vb));
278
279         spin_lock(&blist_lock);
280         if(buflist) {
281                 buflist_tail->next = buf;
282                 buflist_tail = buf;
283         } else {
284                 buflist = buflist_tail = buf;
285         }
286         buf->next = 0;
287         spin_unlock(&blist_lock);
288 }
289
290 static void clear_queue(enum vb2_buffer_state st)
291 {
292         spin_lock(&blist_lock);
293         while(buflist) {
294                 struct buffer *buf = buflist;
295                 buflist = buflist->next;
296                 vb2_buffer_done(&buf->vb, st);
297         }
298         buflist = buflist_tail = 0;
299         spin_unlock(&blist_lock);
300 }
301
302 static int start_streaming(struct vb2_queue *vbq, unsigned int count)
303 {
304         streaming = 1;
305         seqno = 0;
306
307         gen_frame();
308         return 0;
309 }
310
311 static void stop_streaming(struct vb2_queue *vbq)
312 {
313         streaming = 0;
314         clear_queue(VB2_BUF_STATE_ERROR);
315 }
316
317 static int update_frame(int xsz, int ysz)
318 {
319         unsigned char *tmp;
320         int i, j;
321         int new_fsz = xsz * ysz * 3;
322
323         if(xsz == width && ysz == height) {
324                 return 0;
325         }
326
327         printk(KERN_INFO "vdummy: update_frame(%d, %d)\n", xsz, ysz);
328
329         if(!frame || frame_size < new_fsz) {
330                 if(!(tmp = kmalloc(new_fsz, GFP_KERNEL))) {
331                         return -ENOMEM;
332                 }
333                 kfree(frame);
334                 frame_size = new_fsz;
335                 width = xsz;
336                 height = ysz;
337                 frame = tmp;
338         }
339
340         tmp = frame;
341         for(i=0; i<height; i++) {
342                 for(j=0; j<width; j++) {
343                         int val = i ^ j;
344                         *tmp++ = val;
345                         *tmp++ = (val << 1) & 0xff;
346                         *tmp++ = (val << 2) & 0xff;
347                 }
348         }
349
350         return 0;
351 }
352
353 static int gen_frame(void)
354 {
355         struct buffer *buf = 0;
356
357         spin_lock(&blist_lock);
358         if(buflist) {
359                 buf = buflist;
360                 buflist = buflist->next;
361                 if(!buflist) buflist_tail = 0;
362         }
363         spin_unlock(&blist_lock);
364
365         if(buf) {
366                 memcpy(vb2_plane_vaddr(&buf->vb, 0), frame, frame_size);
367
368                 /*v4l2_get_timestamp(&buf->vb.v4l2_buf.timestamp);
369                 buf->vb.v4l2_buf.sequence = seqno++;
370                 buf->vb.v4l2_buf.field = V4L2_FIELD_NONE;*/
371                 vb2_buffer_done(&buf->vb, VB2_BUF_STATE_DONE);
372         }
373
374         return 0;
375 }