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