xref: /linux/drivers/media/platform/qcom/camss/camss-video.c (revision 3fd6c59042dbba50391e30862beac979491145fe)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * camss-video.c
4  *
5  * Qualcomm MSM Camera Subsystem - V4L2 device node
6  *
7  * Copyright (c) 2013-2015, The Linux Foundation. All rights reserved.
8  * Copyright (C) 2015-2018 Linaro Ltd.
9  */
10 #include <linux/slab.h>
11 #include <media/media-entity.h>
12 #include <media/v4l2-dev.h>
13 #include <media/v4l2-device.h>
14 #include <media/v4l2-ioctl.h>
15 #include <media/v4l2-mc.h>
16 #include <media/videobuf2-dma-sg.h>
17 
18 #include "camss-video.h"
19 #include "camss.h"
20 
21 #define CAMSS_FRAME_MIN_WIDTH		1
22 #define CAMSS_FRAME_MAX_WIDTH		8191
23 #define CAMSS_FRAME_MIN_HEIGHT		1
24 #define CAMSS_FRAME_MAX_HEIGHT_RDI	8191
25 #define CAMSS_FRAME_MAX_HEIGHT_PIX	4096
26 
27 /* -----------------------------------------------------------------------------
28  * Helper functions
29  */
30 
31 /*
32  * video_mbus_to_pix_mp - Convert v4l2_mbus_framefmt to v4l2_pix_format_mplane
33  * @mbus: v4l2_mbus_framefmt format (input)
34  * @pix: v4l2_pix_format_mplane format (output)
35  * @f: a pointer to formats array element to be used for the conversion
36  * @alignment: bytesperline alignment value
37  *
38  * Fill the output pix structure with information from the input mbus format.
39  *
40  * Return 0 on success or a negative error code otherwise
41  */
video_mbus_to_pix_mp(const struct v4l2_mbus_framefmt * mbus,struct v4l2_pix_format_mplane * pix,const struct camss_format_info * f,unsigned int alignment)42 static int video_mbus_to_pix_mp(const struct v4l2_mbus_framefmt *mbus,
43 				struct v4l2_pix_format_mplane *pix,
44 				const struct camss_format_info *f,
45 				unsigned int alignment)
46 {
47 	unsigned int i;
48 	u32 bytesperline;
49 
50 	memset(pix, 0, sizeof(*pix));
51 	v4l2_fill_pix_format_mplane(pix, mbus);
52 	pix->pixelformat = f->pixelformat;
53 	pix->num_planes = f->planes;
54 	for (i = 0; i < pix->num_planes; i++) {
55 		bytesperline = pix->width / f->hsub[i].numerator *
56 			f->hsub[i].denominator * f->bpp[i] / 8;
57 		bytesperline = ALIGN(bytesperline, alignment);
58 		pix->plane_fmt[i].bytesperline = bytesperline;
59 		pix->plane_fmt[i].sizeimage = pix->height /
60 				f->vsub[i].numerator * f->vsub[i].denominator *
61 				bytesperline;
62 	}
63 
64 	return 0;
65 }
66 
video_remote_subdev(struct camss_video * video,u32 * pad)67 static struct v4l2_subdev *video_remote_subdev(struct camss_video *video,
68 					       u32 *pad)
69 {
70 	struct media_pad *remote;
71 
72 	remote = media_pad_remote_pad_first(&video->pad);
73 
74 	if (!remote || !is_media_entity_v4l2_subdev(remote->entity))
75 		return NULL;
76 
77 	if (pad)
78 		*pad = remote->index;
79 
80 	return media_entity_to_v4l2_subdev(remote->entity);
81 }
82 
video_get_subdev_format(struct camss_video * video,struct v4l2_format * format)83 static int video_get_subdev_format(struct camss_video *video,
84 				   struct v4l2_format *format)
85 {
86 	struct v4l2_subdev_format fmt = {
87 		.which = V4L2_SUBDEV_FORMAT_ACTIVE,
88 	};
89 	struct v4l2_subdev *subdev;
90 	u32 pad;
91 	int ret;
92 
93 	subdev = video_remote_subdev(video, &pad);
94 	if (subdev == NULL)
95 		return -EPIPE;
96 
97 	fmt.pad = pad;
98 
99 	ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &fmt);
100 	if (ret)
101 		return ret;
102 
103 	ret = camss_format_find_format(fmt.format.code, format->fmt.pix_mp.pixelformat,
104 				       video->formats, video->nformats);
105 	if (ret < 0)
106 		return ret;
107 
108 	format->type = video->type;
109 
110 	return video_mbus_to_pix_mp(&fmt.format, &format->fmt.pix_mp,
111 				    &video->formats[ret], video->bpl_alignment);
112 }
113 
114 /* -----------------------------------------------------------------------------
115  * Video queue operations
116  */
117 
video_queue_setup(struct vb2_queue * q,unsigned int * num_buffers,unsigned int * num_planes,unsigned int sizes[],struct device * alloc_devs[])118 static int video_queue_setup(struct vb2_queue *q,
119 	unsigned int *num_buffers, unsigned int *num_planes,
120 	unsigned int sizes[], struct device *alloc_devs[])
121 {
122 	struct camss_video *video = vb2_get_drv_priv(q);
123 	const struct v4l2_pix_format_mplane *format =
124 						&video->active_fmt.fmt.pix_mp;
125 	unsigned int i;
126 
127 	if (*num_planes) {
128 		if (*num_planes != format->num_planes)
129 			return -EINVAL;
130 
131 		for (i = 0; i < *num_planes; i++)
132 			if (sizes[i] < format->plane_fmt[i].sizeimage)
133 				return -EINVAL;
134 
135 		return 0;
136 	}
137 
138 	*num_planes = format->num_planes;
139 
140 	for (i = 0; i < *num_planes; i++)
141 		sizes[i] = format->plane_fmt[i].sizeimage;
142 
143 	return 0;
144 }
145 
video_buf_init(struct vb2_buffer * vb)146 static int video_buf_init(struct vb2_buffer *vb)
147 {
148 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
149 	struct camss_video *video = vb2_get_drv_priv(vb->vb2_queue);
150 	struct camss_buffer *buffer = container_of(vbuf, struct camss_buffer,
151 						   vb);
152 	const struct v4l2_pix_format_mplane *format =
153 						&video->active_fmt.fmt.pix_mp;
154 	struct sg_table *sgt;
155 	unsigned int i;
156 
157 	for (i = 0; i < format->num_planes; i++) {
158 		sgt = vb2_dma_sg_plane_desc(vb, i);
159 		if (!sgt)
160 			return -EFAULT;
161 
162 		buffer->addr[i] = sg_dma_address(sgt->sgl);
163 	}
164 
165 	if (format->pixelformat == V4L2_PIX_FMT_NV12 ||
166 			format->pixelformat == V4L2_PIX_FMT_NV21 ||
167 			format->pixelformat == V4L2_PIX_FMT_NV16 ||
168 			format->pixelformat == V4L2_PIX_FMT_NV61)
169 		buffer->addr[1] = buffer->addr[0] +
170 				format->plane_fmt[0].bytesperline *
171 				format->height;
172 
173 	return 0;
174 }
175 
video_buf_prepare(struct vb2_buffer * vb)176 static int video_buf_prepare(struct vb2_buffer *vb)
177 {
178 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
179 	struct camss_video *video = vb2_get_drv_priv(vb->vb2_queue);
180 	const struct v4l2_pix_format_mplane *format =
181 						&video->active_fmt.fmt.pix_mp;
182 	unsigned int i;
183 
184 	for (i = 0; i < format->num_planes; i++) {
185 		if (format->plane_fmt[i].sizeimage > vb2_plane_size(vb, i))
186 			return -EINVAL;
187 
188 		vb2_set_plane_payload(vb, i, format->plane_fmt[i].sizeimage);
189 	}
190 
191 	vbuf->field = V4L2_FIELD_NONE;
192 
193 	return 0;
194 }
195 
video_buf_queue(struct vb2_buffer * vb)196 static void video_buf_queue(struct vb2_buffer *vb)
197 {
198 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
199 	struct camss_video *video = vb2_get_drv_priv(vb->vb2_queue);
200 	struct camss_buffer *buffer = container_of(vbuf, struct camss_buffer,
201 						   vb);
202 
203 	video->ops->queue_buffer(video, buffer);
204 }
205 
video_check_format(struct camss_video * video)206 static int video_check_format(struct camss_video *video)
207 {
208 	struct v4l2_pix_format_mplane *pix = &video->active_fmt.fmt.pix_mp;
209 	struct v4l2_format format;
210 	struct v4l2_pix_format_mplane *sd_pix = &format.fmt.pix_mp;
211 	int ret;
212 
213 	sd_pix->pixelformat = pix->pixelformat;
214 	ret = video_get_subdev_format(video, &format);
215 	if (ret < 0)
216 		return ret;
217 
218 	if (pix->pixelformat != sd_pix->pixelformat ||
219 	    pix->height != sd_pix->height ||
220 	    pix->width != sd_pix->width ||
221 	    pix->num_planes != sd_pix->num_planes ||
222 	    pix->field != format.fmt.pix_mp.field)
223 		return -EPIPE;
224 
225 	return 0;
226 }
227 
video_start_streaming(struct vb2_queue * q,unsigned int count)228 static int video_start_streaming(struct vb2_queue *q, unsigned int count)
229 {
230 	struct camss_video *video = vb2_get_drv_priv(q);
231 	struct video_device *vdev = &video->vdev;
232 	struct media_entity *entity;
233 	struct media_pad *pad;
234 	struct v4l2_subdev *subdev;
235 	int ret;
236 
237 	ret = video_device_pipeline_alloc_start(vdev);
238 	if (ret < 0) {
239 		dev_err(video->camss->dev, "Failed to start media pipeline: %d\n", ret);
240 		goto flush_buffers;
241 	}
242 
243 	ret = video_check_format(video);
244 	if (ret < 0)
245 		goto error;
246 
247 	entity = &vdev->entity;
248 	while (1) {
249 		pad = &entity->pads[0];
250 		if (!(pad->flags & MEDIA_PAD_FL_SINK))
251 			break;
252 
253 		pad = media_pad_remote_pad_first(pad);
254 		if (!pad || !is_media_entity_v4l2_subdev(pad->entity))
255 			break;
256 
257 		entity = pad->entity;
258 		subdev = media_entity_to_v4l2_subdev(entity);
259 
260 		ret = v4l2_subdev_call(subdev, video, s_stream, 1);
261 		if (ret < 0 && ret != -ENOIOCTLCMD)
262 			goto error;
263 	}
264 
265 	return 0;
266 
267 error:
268 	video_device_pipeline_stop(vdev);
269 
270 flush_buffers:
271 	video->ops->flush_buffers(video, VB2_BUF_STATE_QUEUED);
272 
273 	return ret;
274 }
275 
video_stop_streaming(struct vb2_queue * q)276 static void video_stop_streaming(struct vb2_queue *q)
277 {
278 	struct camss_video *video = vb2_get_drv_priv(q);
279 	struct video_device *vdev = &video->vdev;
280 	struct media_entity *entity;
281 	struct media_pad *pad;
282 	struct v4l2_subdev *subdev;
283 	int ret;
284 
285 	entity = &vdev->entity;
286 	while (1) {
287 		pad = &entity->pads[0];
288 		if (!(pad->flags & MEDIA_PAD_FL_SINK))
289 			break;
290 
291 		pad = media_pad_remote_pad_first(pad);
292 		if (!pad || !is_media_entity_v4l2_subdev(pad->entity))
293 			break;
294 
295 		entity = pad->entity;
296 		subdev = media_entity_to_v4l2_subdev(entity);
297 
298 		ret = v4l2_subdev_call(subdev, video, s_stream, 0);
299 
300 		if (ret) {
301 			dev_err(video->camss->dev, "Video pipeline stop failed: %d\n", ret);
302 			return;
303 		}
304 	}
305 
306 	video_device_pipeline_stop(vdev);
307 
308 	video->ops->flush_buffers(video, VB2_BUF_STATE_ERROR);
309 }
310 
311 static const struct vb2_ops msm_video_vb2_q_ops = {
312 	.queue_setup     = video_queue_setup,
313 	.buf_init        = video_buf_init,
314 	.buf_prepare     = video_buf_prepare,
315 	.buf_queue       = video_buf_queue,
316 	.start_streaming = video_start_streaming,
317 	.stop_streaming  = video_stop_streaming,
318 };
319 
320 /* -----------------------------------------------------------------------------
321  * V4L2 ioctls
322  */
323 
video_querycap(struct file * file,void * fh,struct v4l2_capability * cap)324 static int video_querycap(struct file *file, void *fh,
325 			  struct v4l2_capability *cap)
326 {
327 	strscpy(cap->driver, "qcom-camss", sizeof(cap->driver));
328 	strscpy(cap->card, "Qualcomm Camera Subsystem", sizeof(cap->card));
329 
330 	return 0;
331 }
332 
video_enum_fmt(struct file * file,void * fh,struct v4l2_fmtdesc * f)333 static int video_enum_fmt(struct file *file, void *fh, struct v4l2_fmtdesc *f)
334 {
335 	struct camss_video *video = video_drvdata(file);
336 	int i, j, k;
337 	u32 mcode = f->mbus_code;
338 
339 	if (f->type != video->type)
340 		return -EINVAL;
341 
342 	if (f->index >= video->nformats)
343 		return -EINVAL;
344 
345 	/*
346 	 * Find index "i" of "k"th unique pixelformat in formats array.
347 	 *
348 	 * If f->mbus_code passed to video_enum_fmt() is not zero, a device
349 	 * with V4L2_CAP_IO_MC capability restricts enumeration to only the
350 	 * pixel formats that can be produced from that media bus code.
351 	 * This is implemented by skipping video->formats[] entries with
352 	 * code != f->mbus_code (if f->mbus_code is not zero).
353 	 * If the f->mbus_code passed to video_enum_fmt() is not supported,
354 	 * -EINVAL is returned.
355 	 * If f->mbus_code is zero, all the pixel formats are enumerated.
356 	 */
357 	k = -1;
358 	for (i = 0; i < video->nformats; i++) {
359 		if (mcode != 0 && video->formats[i].code != mcode)
360 			continue;
361 
362 		for (j = 0; j < i; j++) {
363 			if (mcode != 0 && video->formats[j].code != mcode)
364 				continue;
365 			if (video->formats[i].pixelformat ==
366 					video->formats[j].pixelformat)
367 				break;
368 		}
369 
370 		if (j == i)
371 			k++;
372 
373 		if (k == f->index)
374 			break;
375 	}
376 
377 	if (k == -1 || k < f->index)
378 		/*
379 		 * All the unique pixel formats matching the arguments
380 		 * have been enumerated (k >= 0 and f->index > 0), or
381 		 * no pixel formats match the non-zero f->mbus_code (k == -1).
382 		 */
383 		return -EINVAL;
384 
385 	f->pixelformat = video->formats[i].pixelformat;
386 
387 	return 0;
388 }
389 
video_enum_framesizes(struct file * file,void * fh,struct v4l2_frmsizeenum * fsize)390 static int video_enum_framesizes(struct file *file, void *fh,
391 				 struct v4l2_frmsizeenum *fsize)
392 {
393 	struct camss_video *video = video_drvdata(file);
394 	int i;
395 
396 	if (fsize->index)
397 		return -EINVAL;
398 
399 	/* Only accept pixel format present in the formats[] table */
400 	for (i = 0; i < video->nformats; i++) {
401 		if (video->formats[i].pixelformat == fsize->pixel_format)
402 			break;
403 	}
404 
405 	if (i == video->nformats)
406 		return -EINVAL;
407 
408 	fsize->type = V4L2_FRMSIZE_TYPE_CONTINUOUS;
409 	fsize->stepwise.min_width = CAMSS_FRAME_MIN_WIDTH;
410 	fsize->stepwise.max_width = CAMSS_FRAME_MAX_WIDTH;
411 	fsize->stepwise.min_height = CAMSS_FRAME_MIN_HEIGHT;
412 	fsize->stepwise.max_height = (video->line_based) ?
413 		CAMSS_FRAME_MAX_HEIGHT_PIX : CAMSS_FRAME_MAX_HEIGHT_RDI;
414 	fsize->stepwise.step_width = 1;
415 	fsize->stepwise.step_height = 1;
416 
417 	return 0;
418 }
419 
video_g_fmt(struct file * file,void * fh,struct v4l2_format * f)420 static int video_g_fmt(struct file *file, void *fh, struct v4l2_format *f)
421 {
422 	struct camss_video *video = video_drvdata(file);
423 
424 	*f = video->active_fmt;
425 
426 	return 0;
427 }
428 
__video_try_fmt(struct camss_video * video,struct v4l2_format * f)429 static int __video_try_fmt(struct camss_video *video, struct v4l2_format *f)
430 {
431 	struct v4l2_pix_format_mplane *pix_mp;
432 	const struct camss_format_info *fi;
433 	struct v4l2_plane_pix_format *p;
434 	u32 bytesperline[3] = { 0 };
435 	u32 sizeimage[3] = { 0 };
436 	u32 width, height;
437 	u32 bpl, lines;
438 	int i, j;
439 
440 	pix_mp = &f->fmt.pix_mp;
441 
442 	if (video->line_based)
443 		for (i = 0; i < pix_mp->num_planes && i < 3; i++) {
444 			p = &pix_mp->plane_fmt[i];
445 			bytesperline[i] = clamp_t(u32, p->bytesperline,
446 						  1, 65528);
447 			sizeimage[i] = clamp_t(u32, p->sizeimage,
448 					       bytesperline[i],
449 					       bytesperline[i] * CAMSS_FRAME_MAX_HEIGHT_PIX);
450 		}
451 
452 	for (j = 0; j < video->nformats; j++)
453 		if (pix_mp->pixelformat == video->formats[j].pixelformat)
454 			break;
455 
456 	if (j == video->nformats)
457 		j = 0; /* default format */
458 
459 	fi = &video->formats[j];
460 	width = pix_mp->width;
461 	height = pix_mp->height;
462 
463 	memset(pix_mp, 0, sizeof(*pix_mp));
464 
465 	pix_mp->pixelformat = fi->pixelformat;
466 	pix_mp->width = clamp_t(u32, width, 1, CAMSS_FRAME_MAX_WIDTH);
467 	pix_mp->height = clamp_t(u32, height, 1, CAMSS_FRAME_MAX_HEIGHT_RDI);
468 	pix_mp->num_planes = fi->planes;
469 	for (i = 0; i < pix_mp->num_planes; i++) {
470 		bpl = pix_mp->width / fi->hsub[i].numerator *
471 			fi->hsub[i].denominator * fi->bpp[i] / 8;
472 		bpl = ALIGN(bpl, video->bpl_alignment);
473 		pix_mp->plane_fmt[i].bytesperline = bpl;
474 		pix_mp->plane_fmt[i].sizeimage = pix_mp->height /
475 			fi->vsub[i].numerator * fi->vsub[i].denominator * bpl;
476 	}
477 
478 	pix_mp->field = V4L2_FIELD_NONE;
479 	pix_mp->colorspace = V4L2_COLORSPACE_SRGB;
480 	pix_mp->flags = 0;
481 	pix_mp->ycbcr_enc = V4L2_MAP_YCBCR_ENC_DEFAULT(pix_mp->colorspace);
482 	pix_mp->quantization = V4L2_MAP_QUANTIZATION_DEFAULT(true,
483 					pix_mp->colorspace, pix_mp->ycbcr_enc);
484 	pix_mp->xfer_func = V4L2_MAP_XFER_FUNC_DEFAULT(pix_mp->colorspace);
485 
486 	if (video->line_based)
487 		for (i = 0; i < pix_mp->num_planes; i++) {
488 			p = &pix_mp->plane_fmt[i];
489 			p->bytesperline = clamp_t(u32, p->bytesperline,
490 						  1, 65528);
491 			p->sizeimage = clamp_t(u32, p->sizeimage,
492 					       p->bytesperline,
493 					       p->bytesperline * CAMSS_FRAME_MAX_HEIGHT_PIX);
494 			lines = p->sizeimage / p->bytesperline;
495 
496 			if (p->bytesperline < bytesperline[i])
497 				p->bytesperline = ALIGN(bytesperline[i], 8);
498 
499 			if (p->sizeimage < p->bytesperline * lines)
500 				p->sizeimage = p->bytesperline * lines;
501 
502 			if (p->sizeimage < sizeimage[i])
503 				p->sizeimage = sizeimage[i];
504 		}
505 
506 	return 0;
507 }
508 
video_try_fmt(struct file * file,void * fh,struct v4l2_format * f)509 static int video_try_fmt(struct file *file, void *fh, struct v4l2_format *f)
510 {
511 	struct camss_video *video = video_drvdata(file);
512 
513 	return __video_try_fmt(video, f);
514 }
515 
video_s_fmt(struct file * file,void * fh,struct v4l2_format * f)516 static int video_s_fmt(struct file *file, void *fh, struct v4l2_format *f)
517 {
518 	struct camss_video *video = video_drvdata(file);
519 	int ret;
520 
521 	if (vb2_is_busy(&video->vb2_q))
522 		return -EBUSY;
523 
524 	ret = __video_try_fmt(video, f);
525 	if (ret < 0)
526 		return ret;
527 
528 	video->active_fmt = *f;
529 
530 	return 0;
531 }
532 
video_enum_input(struct file * file,void * fh,struct v4l2_input * input)533 static int video_enum_input(struct file *file, void *fh,
534 			    struct v4l2_input *input)
535 {
536 	if (input->index > 0)
537 		return -EINVAL;
538 
539 	strscpy(input->name, "camera", sizeof(input->name));
540 	input->type = V4L2_INPUT_TYPE_CAMERA;
541 
542 	return 0;
543 }
544 
video_g_input(struct file * file,void * fh,unsigned int * input)545 static int video_g_input(struct file *file, void *fh, unsigned int *input)
546 {
547 	*input = 0;
548 
549 	return 0;
550 }
551 
video_s_input(struct file * file,void * fh,unsigned int input)552 static int video_s_input(struct file *file, void *fh, unsigned int input)
553 {
554 	return input == 0 ? 0 : -EINVAL;
555 }
556 
557 static const struct v4l2_ioctl_ops msm_vid_ioctl_ops = {
558 	.vidioc_querycap		= video_querycap,
559 	.vidioc_enum_fmt_vid_cap	= video_enum_fmt,
560 	.vidioc_enum_framesizes		= video_enum_framesizes,
561 	.vidioc_g_fmt_vid_cap_mplane	= video_g_fmt,
562 	.vidioc_s_fmt_vid_cap_mplane	= video_s_fmt,
563 	.vidioc_try_fmt_vid_cap_mplane	= video_try_fmt,
564 	.vidioc_reqbufs			= vb2_ioctl_reqbufs,
565 	.vidioc_querybuf		= vb2_ioctl_querybuf,
566 	.vidioc_qbuf			= vb2_ioctl_qbuf,
567 	.vidioc_expbuf			= vb2_ioctl_expbuf,
568 	.vidioc_dqbuf			= vb2_ioctl_dqbuf,
569 	.vidioc_create_bufs		= vb2_ioctl_create_bufs,
570 	.vidioc_prepare_buf		= vb2_ioctl_prepare_buf,
571 	.vidioc_streamon		= vb2_ioctl_streamon,
572 	.vidioc_streamoff		= vb2_ioctl_streamoff,
573 	.vidioc_enum_input		= video_enum_input,
574 	.vidioc_g_input			= video_g_input,
575 	.vidioc_s_input			= video_s_input,
576 };
577 
578 /* -----------------------------------------------------------------------------
579  * V4L2 file operations
580  */
581 
video_open(struct file * file)582 static int video_open(struct file *file)
583 {
584 	struct video_device *vdev = video_devdata(file);
585 	struct camss_video *video = video_drvdata(file);
586 	struct v4l2_fh *vfh;
587 	int ret;
588 
589 	mutex_lock(&video->lock);
590 
591 	vfh = kzalloc(sizeof(*vfh), GFP_KERNEL);
592 	if (vfh == NULL) {
593 		ret = -ENOMEM;
594 		goto error_alloc;
595 	}
596 
597 	v4l2_fh_init(vfh, vdev);
598 	v4l2_fh_add(vfh);
599 
600 	file->private_data = vfh;
601 
602 	ret = v4l2_pipeline_pm_get(&vdev->entity);
603 	if (ret < 0) {
604 		dev_err(video->camss->dev, "Failed to power up pipeline: %d\n",
605 			ret);
606 		goto error_pm_use;
607 	}
608 
609 	mutex_unlock(&video->lock);
610 
611 	return 0;
612 
613 error_pm_use:
614 	v4l2_fh_release(file);
615 
616 error_alloc:
617 	mutex_unlock(&video->lock);
618 
619 	return ret;
620 }
621 
video_release(struct file * file)622 static int video_release(struct file *file)
623 {
624 	struct video_device *vdev = video_devdata(file);
625 
626 	vb2_fop_release(file);
627 
628 	v4l2_pipeline_pm_put(&vdev->entity);
629 
630 	file->private_data = NULL;
631 
632 	return 0;
633 }
634 
635 static const struct v4l2_file_operations msm_vid_fops = {
636 	.owner          = THIS_MODULE,
637 	.unlocked_ioctl = video_ioctl2,
638 	.open           = video_open,
639 	.release        = video_release,
640 	.poll           = vb2_fop_poll,
641 	.mmap		= vb2_fop_mmap,
642 	.read		= vb2_fop_read,
643 };
644 
645 /* -----------------------------------------------------------------------------
646  * CAMSS video core
647  */
648 
msm_video_release(struct video_device * vdev)649 static void msm_video_release(struct video_device *vdev)
650 {
651 	struct camss_video *video = video_get_drvdata(vdev);
652 
653 	media_entity_cleanup(&vdev->entity);
654 
655 	mutex_destroy(&video->q_lock);
656 	mutex_destroy(&video->lock);
657 
658 	if (atomic_dec_and_test(&video->camss->ref_count))
659 		camss_delete(video->camss);
660 }
661 
662 /*
663  * msm_video_init_format - Helper function to initialize format
664  * @video: struct camss_video
665  *
666  * Initialize pad format with default value.
667  *
668  * Return 0 on success or a negative error code otherwise
669  */
msm_video_init_format(struct camss_video * video)670 static int msm_video_init_format(struct camss_video *video)
671 {
672 	int ret;
673 	struct v4l2_format format = {
674 		.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE,
675 		.fmt.pix_mp = {
676 			.width = 1920,
677 			.height = 1080,
678 			.pixelformat = video->formats[0].pixelformat,
679 		},
680 	};
681 
682 	ret = __video_try_fmt(video, &format);
683 	if (ret < 0)
684 		return ret;
685 
686 	video->active_fmt = format;
687 
688 	return 0;
689 }
690 
691 /*
692  * msm_video_register - Register a video device node
693  * @video: struct camss_video
694  * @v4l2_dev: V4L2 device
695  * @name: name to be used for the video device node
696  *
697  * Initialize and register a video device node to a V4L2 device. Also
698  * initialize the vb2 queue.
699  *
700  * Return 0 on success or a negative error code otherwise
701  */
702 
msm_video_register(struct camss_video * video,struct v4l2_device * v4l2_dev,const char * name)703 int msm_video_register(struct camss_video *video, struct v4l2_device *v4l2_dev,
704 		       const char *name)
705 {
706 	struct media_pad *pad = &video->pad;
707 	struct video_device *vdev;
708 	struct vb2_queue *q;
709 	int ret;
710 
711 	vdev = &video->vdev;
712 
713 	mutex_init(&video->q_lock);
714 
715 	q = &video->vb2_q;
716 	q->drv_priv = video;
717 	q->mem_ops = &vb2_dma_sg_memops;
718 	q->ops = &msm_video_vb2_q_ops;
719 	q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
720 	q->io_modes = VB2_DMABUF | VB2_MMAP | VB2_READ;
721 	q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
722 	q->buf_struct_size = sizeof(struct camss_buffer);
723 	q->dev = video->camss->dev;
724 	q->lock = &video->q_lock;
725 	ret = vb2_queue_init(q);
726 	if (ret < 0) {
727 		dev_err(v4l2_dev->dev, "Failed to init vb2 queue: %d\n", ret);
728 		goto error_vb2_init;
729 	}
730 
731 	pad->flags = MEDIA_PAD_FL_SINK;
732 	ret = media_entity_pads_init(&vdev->entity, 1, pad);
733 	if (ret < 0) {
734 		dev_err(v4l2_dev->dev, "Failed to init video entity: %d\n",
735 			ret);
736 		goto error_vb2_init;
737 	}
738 
739 	mutex_init(&video->lock);
740 
741 	ret = msm_video_init_format(video);
742 	if (ret < 0) {
743 		dev_err(v4l2_dev->dev, "Failed to init format: %d\n", ret);
744 		goto error_video_register;
745 	}
746 
747 	vdev->fops = &msm_vid_fops;
748 	vdev->device_caps = V4L2_CAP_VIDEO_CAPTURE_MPLANE | V4L2_CAP_STREAMING
749 			  | V4L2_CAP_READWRITE | V4L2_CAP_IO_MC;
750 	vdev->ioctl_ops = &msm_vid_ioctl_ops;
751 	vdev->release = msm_video_release;
752 	vdev->v4l2_dev = v4l2_dev;
753 	vdev->vfl_dir = VFL_DIR_RX;
754 	vdev->queue = &video->vb2_q;
755 	vdev->lock = &video->lock;
756 	strscpy(vdev->name, name, sizeof(vdev->name));
757 
758 	ret = video_register_device(vdev, VFL_TYPE_VIDEO, -1);
759 	if (ret < 0) {
760 		dev_err(v4l2_dev->dev, "Failed to register video device: %d\n",
761 			ret);
762 		goto error_video_register;
763 	}
764 
765 	video_set_drvdata(vdev, video);
766 	atomic_inc(&video->camss->ref_count);
767 
768 	return 0;
769 
770 error_video_register:
771 	media_entity_cleanup(&vdev->entity);
772 	mutex_destroy(&video->lock);
773 error_vb2_init:
774 	mutex_destroy(&video->q_lock);
775 
776 	return ret;
777 }
778 
msm_video_unregister(struct camss_video * video)779 void msm_video_unregister(struct camss_video *video)
780 {
781 	atomic_inc(&video->camss->ref_count);
782 	vb2_video_unregister_device(&video->vdev);
783 	atomic_dec(&video->camss->ref_count);
784 }
785