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