GNU Linux-libre 4.14.251-gnu1
[releases.git] / drivers / media / platform / vimc / vimc-capture.c
1 /*
2  * vimc-capture.c Virtual Media Controller Driver
3  *
4  * Copyright (C) 2015-2017 Helen Koike <helen.fornazier@gmail.com>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  */
17
18 #include <linux/component.h>
19 #include <linux/module.h>
20 #include <linux/platform_device.h>
21 #include <media/v4l2-ioctl.h>
22 #include <media/videobuf2-core.h>
23 #include <media/videobuf2-vmalloc.h>
24
25 #include "vimc-common.h"
26 #include "vimc-streamer.h"
27
28 #define VIMC_CAP_DRV_NAME "vimc-capture"
29
30 struct vimc_cap_device {
31         struct vimc_ent_device ved;
32         struct video_device vdev;
33         struct device *dev;
34         struct v4l2_pix_format format;
35         struct vb2_queue queue;
36         struct list_head buf_list;
37         /*
38          * NOTE: in a real driver, a spin lock must be used to access the
39          * queue because the frames are generated from a hardware interruption
40          * and the isr is not allowed to sleep.
41          * Even if it is not necessary a spinlock in the vimc driver, we
42          * use it here as a code reference
43          */
44         spinlock_t qlock;
45         struct mutex lock;
46         u32 sequence;
47         struct vimc_stream stream;
48 };
49
50 static const struct v4l2_pix_format fmt_default = {
51         .width = 640,
52         .height = 480,
53         .pixelformat = V4L2_PIX_FMT_RGB24,
54         .field = V4L2_FIELD_NONE,
55         .colorspace = V4L2_COLORSPACE_DEFAULT,
56 };
57
58 struct vimc_cap_buffer {
59         /*
60          * struct vb2_v4l2_buffer must be the first element
61          * the videobuf2 framework will allocate this struct based on
62          * buf_struct_size and use the first sizeof(struct vb2_buffer) bytes of
63          * memory as a vb2_buffer
64          */
65         struct vb2_v4l2_buffer vb2;
66         struct list_head list;
67 };
68
69 static int vimc_cap_querycap(struct file *file, void *priv,
70                              struct v4l2_capability *cap)
71 {
72         struct vimc_cap_device *vcap = video_drvdata(file);
73
74         strlcpy(cap->driver, KBUILD_MODNAME, sizeof(cap->driver));
75         strlcpy(cap->card, KBUILD_MODNAME, sizeof(cap->card));
76         snprintf(cap->bus_info, sizeof(cap->bus_info),
77                  "platform:%s", vcap->vdev.v4l2_dev->name);
78
79         return 0;
80 }
81
82 static void vimc_cap_get_format(struct vimc_ent_device *ved,
83                                 struct v4l2_pix_format *fmt)
84 {
85         struct vimc_cap_device *vcap = container_of(ved, struct vimc_cap_device,
86                                                     ved);
87
88         *fmt = vcap->format;
89 }
90
91 static int vimc_cap_g_fmt_vid_cap(struct file *file, void *priv,
92                                   struct v4l2_format *f)
93 {
94         struct vimc_cap_device *vcap = video_drvdata(file);
95
96         f->fmt.pix = vcap->format;
97
98         return 0;
99 }
100
101 static int vimc_cap_try_fmt_vid_cap(struct file *file, void *priv,
102                                     struct v4l2_format *f)
103 {
104         struct v4l2_pix_format *format = &f->fmt.pix;
105         const struct vimc_pix_map *vpix;
106
107         format->width = clamp_t(u32, format->width, VIMC_FRAME_MIN_WIDTH,
108                                 VIMC_FRAME_MAX_WIDTH) & ~1;
109         format->height = clamp_t(u32, format->height, VIMC_FRAME_MIN_HEIGHT,
110                                  VIMC_FRAME_MAX_HEIGHT) & ~1;
111
112         /* Don't accept a pixelformat that is not on the table */
113         vpix = vimc_pix_map_by_pixelformat(format->pixelformat);
114         if (!vpix) {
115                 format->pixelformat = fmt_default.pixelformat;
116                 vpix = vimc_pix_map_by_pixelformat(format->pixelformat);
117         }
118         /* TODO: Add support for custom bytesperline values */
119         format->bytesperline = format->width * vpix->bpp;
120         format->sizeimage = format->bytesperline * format->height;
121
122         if (format->field == V4L2_FIELD_ANY)
123                 format->field = fmt_default.field;
124
125         vimc_colorimetry_clamp(format);
126
127         return 0;
128 }
129
130 static int vimc_cap_s_fmt_vid_cap(struct file *file, void *priv,
131                                   struct v4l2_format *f)
132 {
133         struct vimc_cap_device *vcap = video_drvdata(file);
134         int ret;
135
136         /* Do not change the format while stream is on */
137         if (vb2_is_busy(&vcap->queue))
138                 return -EBUSY;
139
140         ret = vimc_cap_try_fmt_vid_cap(file, priv, f);
141         if (ret)
142                 return ret;
143
144         dev_dbg(vcap->dev, "%s: format update: "
145                 "old:%dx%d (0x%x, %d, %d, %d, %d) "
146                 "new:%dx%d (0x%x, %d, %d, %d, %d)\n", vcap->vdev.name,
147                 /* old */
148                 vcap->format.width, vcap->format.height,
149                 vcap->format.pixelformat, vcap->format.colorspace,
150                 vcap->format.quantization, vcap->format.xfer_func,
151                 vcap->format.ycbcr_enc,
152                 /* new */
153                 f->fmt.pix.width, f->fmt.pix.height,
154                 f->fmt.pix.pixelformat, f->fmt.pix.colorspace,
155                 f->fmt.pix.quantization, f->fmt.pix.xfer_func,
156                 f->fmt.pix.ycbcr_enc);
157
158         vcap->format = f->fmt.pix;
159
160         return 0;
161 }
162
163 static int vimc_cap_enum_fmt_vid_cap(struct file *file, void *priv,
164                                      struct v4l2_fmtdesc *f)
165 {
166         const struct vimc_pix_map *vpix = vimc_pix_map_by_index(f->index);
167
168         if (!vpix)
169                 return -EINVAL;
170
171         f->pixelformat = vpix->pixelformat;
172
173         return 0;
174 }
175
176 static int vimc_cap_enum_framesizes(struct file *file, void *fh,
177                                     struct v4l2_frmsizeenum *fsize)
178 {
179         const struct vimc_pix_map *vpix;
180
181         if (fsize->index)
182                 return -EINVAL;
183
184         /* Only accept code in the pix map table */
185         vpix = vimc_pix_map_by_code(fsize->pixel_format);
186         if (!vpix)
187                 return -EINVAL;
188
189         fsize->type = V4L2_FRMSIZE_TYPE_CONTINUOUS;
190         fsize->stepwise.min_width = VIMC_FRAME_MIN_WIDTH;
191         fsize->stepwise.max_width = VIMC_FRAME_MAX_WIDTH;
192         fsize->stepwise.min_height = VIMC_FRAME_MIN_HEIGHT;
193         fsize->stepwise.max_height = VIMC_FRAME_MAX_HEIGHT;
194         fsize->stepwise.step_width = 2;
195         fsize->stepwise.step_height = 2;
196
197         return 0;
198 }
199
200 static const struct v4l2_file_operations vimc_cap_fops = {
201         .owner          = THIS_MODULE,
202         .open           = v4l2_fh_open,
203         .release        = vb2_fop_release,
204         .read           = vb2_fop_read,
205         .poll           = vb2_fop_poll,
206         .unlocked_ioctl = video_ioctl2,
207         .mmap           = vb2_fop_mmap,
208 };
209
210 static const struct v4l2_ioctl_ops vimc_cap_ioctl_ops = {
211         .vidioc_querycap = vimc_cap_querycap,
212
213         .vidioc_g_fmt_vid_cap = vimc_cap_g_fmt_vid_cap,
214         .vidioc_s_fmt_vid_cap = vimc_cap_s_fmt_vid_cap,
215         .vidioc_try_fmt_vid_cap = vimc_cap_try_fmt_vid_cap,
216         .vidioc_enum_fmt_vid_cap = vimc_cap_enum_fmt_vid_cap,
217         .vidioc_enum_framesizes = vimc_cap_enum_framesizes,
218
219         .vidioc_reqbufs = vb2_ioctl_reqbufs,
220         .vidioc_create_bufs = vb2_ioctl_create_bufs,
221         .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
222         .vidioc_querybuf = vb2_ioctl_querybuf,
223         .vidioc_qbuf = vb2_ioctl_qbuf,
224         .vidioc_dqbuf = vb2_ioctl_dqbuf,
225         .vidioc_expbuf = vb2_ioctl_expbuf,
226         .vidioc_streamon = vb2_ioctl_streamon,
227         .vidioc_streamoff = vb2_ioctl_streamoff,
228 };
229
230 static void vimc_cap_return_all_buffers(struct vimc_cap_device *vcap,
231                                         enum vb2_buffer_state state)
232 {
233         struct vimc_cap_buffer *vbuf, *node;
234
235         spin_lock(&vcap->qlock);
236
237         list_for_each_entry_safe(vbuf, node, &vcap->buf_list, list) {
238                 list_del(&vbuf->list);
239                 vb2_buffer_done(&vbuf->vb2.vb2_buf, state);
240         }
241
242         spin_unlock(&vcap->qlock);
243 }
244
245 static int vimc_cap_start_streaming(struct vb2_queue *vq, unsigned int count)
246 {
247         struct vimc_cap_device *vcap = vb2_get_drv_priv(vq);
248         struct media_entity *entity = &vcap->vdev.entity;
249         int ret;
250
251         vcap->sequence = 0;
252
253         /* Start the media pipeline */
254         ret = media_pipeline_start(entity, &vcap->stream.pipe);
255         if (ret) {
256                 vimc_cap_return_all_buffers(vcap, VB2_BUF_STATE_QUEUED);
257                 return ret;
258         }
259
260         ret = vimc_streamer_s_stream(&vcap->stream, &vcap->ved, 1);
261         if (ret) {
262                 media_pipeline_stop(entity);
263                 vimc_cap_return_all_buffers(vcap, VB2_BUF_STATE_QUEUED);
264                 return ret;
265         }
266
267         return 0;
268 }
269
270 /*
271  * Stop the stream engine. Any remaining buffers in the stream queue are
272  * dequeued and passed on to the vb2 framework marked as STATE_ERROR.
273  */
274 static void vimc_cap_stop_streaming(struct vb2_queue *vq)
275 {
276         struct vimc_cap_device *vcap = vb2_get_drv_priv(vq);
277
278         vimc_streamer_s_stream(&vcap->stream, &vcap->ved, 0);
279
280         /* Stop the media pipeline */
281         media_pipeline_stop(&vcap->vdev.entity);
282
283         /* Release all active buffers */
284         vimc_cap_return_all_buffers(vcap, VB2_BUF_STATE_ERROR);
285 }
286
287 static void vimc_cap_buf_queue(struct vb2_buffer *vb2_buf)
288 {
289         struct vimc_cap_device *vcap = vb2_get_drv_priv(vb2_buf->vb2_queue);
290         struct vimc_cap_buffer *buf = container_of(vb2_buf,
291                                                    struct vimc_cap_buffer,
292                                                    vb2.vb2_buf);
293
294         spin_lock(&vcap->qlock);
295         list_add_tail(&buf->list, &vcap->buf_list);
296         spin_unlock(&vcap->qlock);
297 }
298
299 static int vimc_cap_queue_setup(struct vb2_queue *vq, unsigned int *nbuffers,
300                                 unsigned int *nplanes, unsigned int sizes[],
301                                 struct device *alloc_devs[])
302 {
303         struct vimc_cap_device *vcap = vb2_get_drv_priv(vq);
304
305         if (*nplanes)
306                 return sizes[0] < vcap->format.sizeimage ? -EINVAL : 0;
307         /* We don't support multiplanes for now */
308         *nplanes = 1;
309         sizes[0] = vcap->format.sizeimage;
310
311         return 0;
312 }
313
314 static int vimc_cap_buffer_prepare(struct vb2_buffer *vb)
315 {
316         struct vimc_cap_device *vcap = vb2_get_drv_priv(vb->vb2_queue);
317         unsigned long size = vcap->format.sizeimage;
318
319         if (vb2_plane_size(vb, 0) < size) {
320                 dev_err(vcap->dev, "%s: buffer too small (%lu < %lu)\n",
321                         vcap->vdev.name, vb2_plane_size(vb, 0), size);
322                 return -EINVAL;
323         }
324         return 0;
325 }
326
327 static const struct vb2_ops vimc_cap_qops = {
328         .start_streaming        = vimc_cap_start_streaming,
329         .stop_streaming         = vimc_cap_stop_streaming,
330         .buf_queue              = vimc_cap_buf_queue,
331         .queue_setup            = vimc_cap_queue_setup,
332         .buf_prepare            = vimc_cap_buffer_prepare,
333         /*
334          * Since q->lock is set we can use the standard
335          * vb2_ops_wait_prepare/finish helper functions.
336          */
337         .wait_prepare           = vb2_ops_wait_prepare,
338         .wait_finish            = vb2_ops_wait_finish,
339 };
340
341 static const struct media_entity_operations vimc_cap_mops = {
342         .link_validate          = vimc_link_validate,
343 };
344
345 static void vimc_cap_comp_unbind(struct device *comp, struct device *master,
346                                  void *master_data)
347 {
348         struct vimc_ent_device *ved = dev_get_drvdata(comp);
349         struct vimc_cap_device *vcap = container_of(ved, struct vimc_cap_device,
350                                                     ved);
351
352         vb2_queue_release(&vcap->queue);
353         media_entity_cleanup(ved->ent);
354         video_unregister_device(&vcap->vdev);
355         vimc_pads_cleanup(vcap->ved.pads);
356         kfree(vcap);
357 }
358
359 static void *vimc_cap_process_frame(struct vimc_ent_device *ved,
360                                     const void *frame)
361 {
362         struct vimc_cap_device *vcap = container_of(ved, struct vimc_cap_device,
363                                                     ved);
364         struct vimc_cap_buffer *vimc_buf;
365         void *vbuf;
366
367         spin_lock(&vcap->qlock);
368
369         /* Get the first entry of the list */
370         vimc_buf = list_first_entry_or_null(&vcap->buf_list,
371                                             typeof(*vimc_buf), list);
372         if (!vimc_buf) {
373                 spin_unlock(&vcap->qlock);
374                 return ERR_PTR(-EAGAIN);
375         }
376
377         /* Remove this entry from the list */
378         list_del(&vimc_buf->list);
379
380         spin_unlock(&vcap->qlock);
381
382         /* Fill the buffer */
383         vimc_buf->vb2.vb2_buf.timestamp = ktime_get_ns();
384         vimc_buf->vb2.sequence = vcap->sequence++;
385         vimc_buf->vb2.field = vcap->format.field;
386
387         vbuf = vb2_plane_vaddr(&vimc_buf->vb2.vb2_buf, 0);
388
389         memcpy(vbuf, frame, vcap->format.sizeimage);
390
391         /* Set it as ready */
392         vb2_set_plane_payload(&vimc_buf->vb2.vb2_buf, 0,
393                               vcap->format.sizeimage);
394         vb2_buffer_done(&vimc_buf->vb2.vb2_buf, VB2_BUF_STATE_DONE);
395         return NULL;
396 }
397
398 static int vimc_cap_comp_bind(struct device *comp, struct device *master,
399                               void *master_data)
400 {
401         struct v4l2_device *v4l2_dev = master_data;
402         struct vimc_platform_data *pdata = comp->platform_data;
403         const struct vimc_pix_map *vpix;
404         struct vimc_cap_device *vcap;
405         struct video_device *vdev;
406         struct vb2_queue *q;
407         int ret;
408
409         /* Allocate the vimc_cap_device struct */
410         vcap = kzalloc(sizeof(*vcap), GFP_KERNEL);
411         if (!vcap)
412                 return -ENOMEM;
413
414         /* Allocate the pads */
415         vcap->ved.pads =
416                 vimc_pads_init(1, (const unsigned long[1]) {MEDIA_PAD_FL_SINK});
417         if (IS_ERR(vcap->ved.pads)) {
418                 ret = PTR_ERR(vcap->ved.pads);
419                 goto err_free_vcap;
420         }
421
422         /* Initialize the media entity */
423         vcap->vdev.entity.name = pdata->entity_name;
424         vcap->vdev.entity.function = MEDIA_ENT_F_IO_V4L;
425         ret = media_entity_pads_init(&vcap->vdev.entity,
426                                      1, vcap->ved.pads);
427         if (ret)
428                 goto err_clean_pads;
429
430         /* Initialize the lock */
431         mutex_init(&vcap->lock);
432
433         /* Initialize the vb2 queue */
434         q = &vcap->queue;
435         q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
436         q->io_modes = VB2_MMAP | VB2_DMABUF;
437         q->drv_priv = vcap;
438         q->buf_struct_size = sizeof(struct vimc_cap_buffer);
439         q->ops = &vimc_cap_qops;
440         q->mem_ops = &vb2_vmalloc_memops;
441         q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
442         q->min_buffers_needed = 2;
443         q->lock = &vcap->lock;
444
445         ret = vb2_queue_init(q);
446         if (ret) {
447                 dev_err(comp, "%s: vb2 queue init failed (err=%d)\n",
448                         pdata->entity_name, ret);
449                 goto err_clean_m_ent;
450         }
451
452         /* Initialize buffer list and its lock */
453         INIT_LIST_HEAD(&vcap->buf_list);
454         spin_lock_init(&vcap->qlock);
455
456         /* Set default frame format */
457         vcap->format = fmt_default;
458         vpix = vimc_pix_map_by_pixelformat(vcap->format.pixelformat);
459         vcap->format.bytesperline = vcap->format.width * vpix->bpp;
460         vcap->format.sizeimage = vcap->format.bytesperline *
461                                  vcap->format.height;
462
463         /* Fill the vimc_ent_device struct */
464         vcap->ved.ent = &vcap->vdev.entity;
465         vcap->ved.process_frame = vimc_cap_process_frame;
466         vcap->ved.vdev_get_format = vimc_cap_get_format;
467         dev_set_drvdata(comp, &vcap->ved);
468         vcap->dev = comp;
469
470         /* Initialize the video_device struct */
471         vdev = &vcap->vdev;
472         vdev->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
473         vdev->entity.ops = &vimc_cap_mops;
474         vdev->release = video_device_release_empty;
475         vdev->fops = &vimc_cap_fops;
476         vdev->ioctl_ops = &vimc_cap_ioctl_ops;
477         vdev->lock = &vcap->lock;
478         vdev->queue = q;
479         vdev->v4l2_dev = v4l2_dev;
480         vdev->vfl_dir = VFL_DIR_RX;
481         strlcpy(vdev->name, pdata->entity_name, sizeof(vdev->name));
482         video_set_drvdata(vdev, &vcap->ved);
483
484         /* Register the video_device with the v4l2 and the media framework */
485         ret = video_register_device(vdev, VFL_TYPE_GRABBER, -1);
486         if (ret) {
487                 dev_err(comp, "%s: video register failed (err=%d)\n",
488                         vcap->vdev.name, ret);
489                 goto err_release_queue;
490         }
491
492         return 0;
493
494 err_release_queue:
495         vb2_queue_release(q);
496 err_clean_m_ent:
497         media_entity_cleanup(&vcap->vdev.entity);
498 err_clean_pads:
499         vimc_pads_cleanup(vcap->ved.pads);
500 err_free_vcap:
501         kfree(vcap);
502
503         return ret;
504 }
505
506 static const struct component_ops vimc_cap_comp_ops = {
507         .bind = vimc_cap_comp_bind,
508         .unbind = vimc_cap_comp_unbind,
509 };
510
511 static int vimc_cap_probe(struct platform_device *pdev)
512 {
513         return component_add(&pdev->dev, &vimc_cap_comp_ops);
514 }
515
516 static int vimc_cap_remove(struct platform_device *pdev)
517 {
518         component_del(&pdev->dev, &vimc_cap_comp_ops);
519
520         return 0;
521 }
522
523 static const struct platform_device_id vimc_cap_driver_ids[] = {
524         {
525                 .name           = VIMC_CAP_DRV_NAME,
526         },
527         { }
528 };
529
530 static struct platform_driver vimc_cap_pdrv = {
531         .probe          = vimc_cap_probe,
532         .remove         = vimc_cap_remove,
533         .id_table       = vimc_cap_driver_ids,
534         .driver         = {
535                 .name   = VIMC_CAP_DRV_NAME,
536         },
537 };
538
539 module_platform_driver(vimc_cap_pdrv);
540
541 MODULE_DEVICE_TABLE(platform, vimc_cap_driver_ids);
542
543 MODULE_DESCRIPTION("Virtual Media Controller Driver (VIMC) Capture");
544 MODULE_AUTHOR("Helen Mae Koike Fornazier <helen.fornazier@gmail.com>");
545 MODULE_LICENSE("GPL");