• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * TI OMAP4 ISS V4L2 Driver - Generic video node
3  *
4  * Copyright (C) 2012 Texas Instruments, Inc.
5  *
6  * Author: Sergio Aguirre <sergio.a.aguirre@gmail.com>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  */
13 
14 #include <asm/cacheflush.h>
15 #include <linux/clk.h>
16 #include <linux/mm.h>
17 #include <linux/pagemap.h>
18 #include <linux/sched.h>
19 #include <linux/slab.h>
20 #include <linux/vmalloc.h>
21 #include <linux/module.h>
22 
23 #include <media/v4l2-dev.h>
24 #include <media/v4l2-ioctl.h>
25 #include <media/v4l2-mc.h>
26 
27 #include "iss_video.h"
28 #include "iss.h"
29 
30 /* -----------------------------------------------------------------------------
31  * Helper functions
32  */
33 
34 static struct iss_format_info formats[] = {
35 	{ MEDIA_BUS_FMT_Y8_1X8, MEDIA_BUS_FMT_Y8_1X8,
36 	  MEDIA_BUS_FMT_Y8_1X8, MEDIA_BUS_FMT_Y8_1X8,
37 	  V4L2_PIX_FMT_GREY, 8, "Greyscale 8 bpp", },
38 	{ MEDIA_BUS_FMT_Y10_1X10, MEDIA_BUS_FMT_Y10_1X10,
39 	  MEDIA_BUS_FMT_Y10_1X10, MEDIA_BUS_FMT_Y8_1X8,
40 	  V4L2_PIX_FMT_Y10, 10, "Greyscale 10 bpp", },
41 	{ MEDIA_BUS_FMT_Y12_1X12, MEDIA_BUS_FMT_Y10_1X10,
42 	  MEDIA_BUS_FMT_Y12_1X12, MEDIA_BUS_FMT_Y8_1X8,
43 	  V4L2_PIX_FMT_Y12, 12, "Greyscale 12 bpp", },
44 	{ MEDIA_BUS_FMT_SBGGR8_1X8, MEDIA_BUS_FMT_SBGGR8_1X8,
45 	  MEDIA_BUS_FMT_SBGGR8_1X8, MEDIA_BUS_FMT_SBGGR8_1X8,
46 	  V4L2_PIX_FMT_SBGGR8, 8, "BGGR Bayer 8 bpp", },
47 	{ MEDIA_BUS_FMT_SGBRG8_1X8, MEDIA_BUS_FMT_SGBRG8_1X8,
48 	  MEDIA_BUS_FMT_SGBRG8_1X8, MEDIA_BUS_FMT_SGBRG8_1X8,
49 	  V4L2_PIX_FMT_SGBRG8, 8, "GBRG Bayer 8 bpp", },
50 	{ MEDIA_BUS_FMT_SGRBG8_1X8, MEDIA_BUS_FMT_SGRBG8_1X8,
51 	  MEDIA_BUS_FMT_SGRBG8_1X8, MEDIA_BUS_FMT_SGRBG8_1X8,
52 	  V4L2_PIX_FMT_SGRBG8, 8, "GRBG Bayer 8 bpp", },
53 	{ MEDIA_BUS_FMT_SRGGB8_1X8, MEDIA_BUS_FMT_SRGGB8_1X8,
54 	  MEDIA_BUS_FMT_SRGGB8_1X8, MEDIA_BUS_FMT_SRGGB8_1X8,
55 	  V4L2_PIX_FMT_SRGGB8, 8, "RGGB Bayer 8 bpp", },
56 	{ MEDIA_BUS_FMT_SGRBG10_DPCM8_1X8, MEDIA_BUS_FMT_SGRBG10_DPCM8_1X8,
57 	  MEDIA_BUS_FMT_SGRBG10_1X10, 0,
58 	  V4L2_PIX_FMT_SGRBG10DPCM8, 8, "GRBG Bayer 10 bpp DPCM8",  },
59 	{ MEDIA_BUS_FMT_SBGGR10_1X10, MEDIA_BUS_FMT_SBGGR10_1X10,
60 	  MEDIA_BUS_FMT_SBGGR10_1X10, MEDIA_BUS_FMT_SBGGR8_1X8,
61 	  V4L2_PIX_FMT_SBGGR10, 10, "BGGR Bayer 10 bpp", },
62 	{ MEDIA_BUS_FMT_SGBRG10_1X10, MEDIA_BUS_FMT_SGBRG10_1X10,
63 	  MEDIA_BUS_FMT_SGBRG10_1X10, MEDIA_BUS_FMT_SGBRG8_1X8,
64 	  V4L2_PIX_FMT_SGBRG10, 10, "GBRG Bayer 10 bpp", },
65 	{ MEDIA_BUS_FMT_SGRBG10_1X10, MEDIA_BUS_FMT_SGRBG10_1X10,
66 	  MEDIA_BUS_FMT_SGRBG10_1X10, MEDIA_BUS_FMT_SGRBG8_1X8,
67 	  V4L2_PIX_FMT_SGRBG10, 10, "GRBG Bayer 10 bpp", },
68 	{ MEDIA_BUS_FMT_SRGGB10_1X10, MEDIA_BUS_FMT_SRGGB10_1X10,
69 	  MEDIA_BUS_FMT_SRGGB10_1X10, MEDIA_BUS_FMT_SRGGB8_1X8,
70 	  V4L2_PIX_FMT_SRGGB10, 10, "RGGB Bayer 10 bpp", },
71 	{ MEDIA_BUS_FMT_SBGGR12_1X12, MEDIA_BUS_FMT_SBGGR10_1X10,
72 	  MEDIA_BUS_FMT_SBGGR12_1X12, MEDIA_BUS_FMT_SBGGR8_1X8,
73 	  V4L2_PIX_FMT_SBGGR12, 12, "BGGR Bayer 12 bpp", },
74 	{ MEDIA_BUS_FMT_SGBRG12_1X12, MEDIA_BUS_FMT_SGBRG10_1X10,
75 	  MEDIA_BUS_FMT_SGBRG12_1X12, MEDIA_BUS_FMT_SGBRG8_1X8,
76 	  V4L2_PIX_FMT_SGBRG12, 12, "GBRG Bayer 12 bpp", },
77 	{ MEDIA_BUS_FMT_SGRBG12_1X12, MEDIA_BUS_FMT_SGRBG10_1X10,
78 	  MEDIA_BUS_FMT_SGRBG12_1X12, MEDIA_BUS_FMT_SGRBG8_1X8,
79 	  V4L2_PIX_FMT_SGRBG12, 12, "GRBG Bayer 12 bpp", },
80 	{ MEDIA_BUS_FMT_SRGGB12_1X12, MEDIA_BUS_FMT_SRGGB10_1X10,
81 	  MEDIA_BUS_FMT_SRGGB12_1X12, MEDIA_BUS_FMT_SRGGB8_1X8,
82 	  V4L2_PIX_FMT_SRGGB12, 12, "RGGB Bayer 12 bpp", },
83 	{ MEDIA_BUS_FMT_UYVY8_1X16, MEDIA_BUS_FMT_UYVY8_1X16,
84 	  MEDIA_BUS_FMT_UYVY8_1X16, 0,
85 	  V4L2_PIX_FMT_UYVY, 16, "YUV 4:2:2 (UYVY)", },
86 	{ MEDIA_BUS_FMT_YUYV8_1X16, MEDIA_BUS_FMT_YUYV8_1X16,
87 	  MEDIA_BUS_FMT_YUYV8_1X16, 0,
88 	  V4L2_PIX_FMT_YUYV, 16, "YUV 4:2:2 (YUYV)", },
89 	{ MEDIA_BUS_FMT_YUYV8_1_5X8, MEDIA_BUS_FMT_YUYV8_1_5X8,
90 	  MEDIA_BUS_FMT_YUYV8_1_5X8, 0,
91 	  V4L2_PIX_FMT_NV12, 8, "YUV 4:2:0 (NV12)", },
92 };
93 
94 const struct iss_format_info *
omap4iss_video_format_info(u32 code)95 omap4iss_video_format_info(u32 code)
96 {
97 	unsigned int i;
98 
99 	for (i = 0; i < ARRAY_SIZE(formats); ++i) {
100 		if (formats[i].code == code)
101 			return &formats[i];
102 	}
103 
104 	return NULL;
105 }
106 
107 /*
108  * iss_video_mbus_to_pix - Convert v4l2_mbus_framefmt to v4l2_pix_format
109  * @video: ISS video instance
110  * @mbus: v4l2_mbus_framefmt format (input)
111  * @pix: v4l2_pix_format format (output)
112  *
113  * Fill the output pix structure with information from the input mbus format.
114  * The bytesperline and sizeimage fields are computed from the requested bytes
115  * per line value in the pix format and information from the video instance.
116  *
117  * Return the number of padding bytes at end of line.
118  */
iss_video_mbus_to_pix(const struct iss_video * video,const struct v4l2_mbus_framefmt * mbus,struct v4l2_pix_format * pix)119 static unsigned int iss_video_mbus_to_pix(const struct iss_video *video,
120 					  const struct v4l2_mbus_framefmt *mbus,
121 					  struct v4l2_pix_format *pix)
122 {
123 	unsigned int bpl = pix->bytesperline;
124 	unsigned int min_bpl;
125 	unsigned int i;
126 
127 	memset(pix, 0, sizeof(*pix));
128 	pix->width = mbus->width;
129 	pix->height = mbus->height;
130 
131 	/* Skip the last format in the loop so that it will be selected if no
132 	 * match is found.
133 	 */
134 	for (i = 0; i < ARRAY_SIZE(formats) - 1; ++i) {
135 		if (formats[i].code == mbus->code)
136 			break;
137 	}
138 
139 	min_bpl = pix->width * ALIGN(formats[i].bpp, 8) / 8;
140 
141 	/* Clamp the requested bytes per line value. If the maximum bytes per
142 	 * line value is zero, the module doesn't support user configurable line
143 	 * sizes. Override the requested value with the minimum in that case.
144 	 */
145 	if (video->bpl_max)
146 		bpl = clamp(bpl, min_bpl, video->bpl_max);
147 	else
148 		bpl = min_bpl;
149 
150 	if (!video->bpl_zero_padding || bpl != min_bpl)
151 		bpl = ALIGN(bpl, video->bpl_alignment);
152 
153 	pix->pixelformat = formats[i].pixelformat;
154 	pix->bytesperline = bpl;
155 	pix->sizeimage = pix->bytesperline * pix->height;
156 	pix->colorspace = mbus->colorspace;
157 	pix->field = mbus->field;
158 
159 	/* FIXME: Special case for NV12! We should make this nicer... */
160 	if (pix->pixelformat == V4L2_PIX_FMT_NV12)
161 		pix->sizeimage += (pix->bytesperline * pix->height) / 2;
162 
163 	return bpl - min_bpl;
164 }
165 
iss_video_pix_to_mbus(const struct v4l2_pix_format * pix,struct v4l2_mbus_framefmt * mbus)166 static void iss_video_pix_to_mbus(const struct v4l2_pix_format *pix,
167 				  struct v4l2_mbus_framefmt *mbus)
168 {
169 	unsigned int i;
170 
171 	memset(mbus, 0, sizeof(*mbus));
172 	mbus->width = pix->width;
173 	mbus->height = pix->height;
174 
175 	/* Skip the last format in the loop so that it will be selected if no
176 	 * match is found.
177 	 */
178 	for (i = 0; i < ARRAY_SIZE(formats) - 1; ++i) {
179 		if (formats[i].pixelformat == pix->pixelformat)
180 			break;
181 	}
182 
183 	mbus->code = formats[i].code;
184 	mbus->colorspace = pix->colorspace;
185 	mbus->field = pix->field;
186 }
187 
188 static struct v4l2_subdev *
iss_video_remote_subdev(struct iss_video * video,u32 * pad)189 iss_video_remote_subdev(struct iss_video *video, u32 *pad)
190 {
191 	struct media_pad *remote;
192 
193 	remote = media_entity_remote_pad(&video->pad);
194 
195 	if (!remote || !is_media_entity_v4l2_subdev(remote->entity))
196 		return NULL;
197 
198 	if (pad)
199 		*pad = remote->index;
200 
201 	return media_entity_to_v4l2_subdev(remote->entity);
202 }
203 
204 /* Return a pointer to the ISS video instance at the far end of the pipeline. */
205 static struct iss_video *
iss_video_far_end(struct iss_video * video)206 iss_video_far_end(struct iss_video *video)
207 {
208 	struct media_entity_graph graph;
209 	struct media_entity *entity = &video->video.entity;
210 	struct media_device *mdev = entity->graph_obj.mdev;
211 	struct iss_video *far_end = NULL;
212 
213 	mutex_lock(&mdev->graph_mutex);
214 
215 	if (media_entity_graph_walk_init(&graph, mdev)) {
216 		mutex_unlock(&mdev->graph_mutex);
217 		return NULL;
218 	}
219 
220 	media_entity_graph_walk_start(&graph, entity);
221 
222 	while ((entity = media_entity_graph_walk_next(&graph))) {
223 		if (entity == &video->video.entity)
224 			continue;
225 
226 		if (!is_media_entity_v4l2_video_device(entity))
227 			continue;
228 
229 		far_end = to_iss_video(media_entity_to_video_device(entity));
230 		if (far_end->type != video->type)
231 			break;
232 
233 		far_end = NULL;
234 	}
235 
236 	mutex_unlock(&mdev->graph_mutex);
237 
238 	media_entity_graph_walk_cleanup(&graph);
239 
240 	return far_end;
241 }
242 
243 static int
__iss_video_get_format(struct iss_video * video,struct v4l2_mbus_framefmt * format)244 __iss_video_get_format(struct iss_video *video,
245 		       struct v4l2_mbus_framefmt *format)
246 {
247 	struct v4l2_subdev_format fmt;
248 	struct v4l2_subdev *subdev;
249 	u32 pad;
250 	int ret;
251 
252 	subdev = iss_video_remote_subdev(video, &pad);
253 	if (!subdev)
254 		return -EINVAL;
255 
256 	memset(&fmt, 0, sizeof(fmt));
257 	fmt.pad = pad;
258 	fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
259 
260 	mutex_lock(&video->mutex);
261 	ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &fmt);
262 	mutex_unlock(&video->mutex);
263 
264 	if (ret)
265 		return ret;
266 
267 	*format = fmt.format;
268 	return 0;
269 }
270 
271 static int
iss_video_check_format(struct iss_video * video,struct iss_video_fh * vfh)272 iss_video_check_format(struct iss_video *video, struct iss_video_fh *vfh)
273 {
274 	struct v4l2_mbus_framefmt format;
275 	struct v4l2_pix_format pixfmt;
276 	int ret;
277 
278 	ret = __iss_video_get_format(video, &format);
279 	if (ret < 0)
280 		return ret;
281 
282 	pixfmt.bytesperline = 0;
283 	ret = iss_video_mbus_to_pix(video, &format, &pixfmt);
284 
285 	if (vfh->format.fmt.pix.pixelformat != pixfmt.pixelformat ||
286 	    vfh->format.fmt.pix.height != pixfmt.height ||
287 	    vfh->format.fmt.pix.width != pixfmt.width ||
288 	    vfh->format.fmt.pix.bytesperline != pixfmt.bytesperline ||
289 	    vfh->format.fmt.pix.sizeimage != pixfmt.sizeimage)
290 		return -EINVAL;
291 
292 	return ret;
293 }
294 
295 /* -----------------------------------------------------------------------------
296  * Video queue operations
297  */
298 
iss_video_queue_setup(struct vb2_queue * vq,unsigned int * count,unsigned int * num_planes,unsigned int sizes[],struct device * alloc_devs[])299 static int iss_video_queue_setup(struct vb2_queue *vq,
300 				 unsigned int *count, unsigned int *num_planes,
301 				 unsigned int sizes[], struct device *alloc_devs[])
302 {
303 	struct iss_video_fh *vfh = vb2_get_drv_priv(vq);
304 	struct iss_video *video = vfh->video;
305 
306 	/* Revisit multi-planar support for NV12 */
307 	*num_planes = 1;
308 
309 	sizes[0] = vfh->format.fmt.pix.sizeimage;
310 	if (sizes[0] == 0)
311 		return -EINVAL;
312 
313 	*count = min(*count, video->capture_mem / PAGE_ALIGN(sizes[0]));
314 
315 	return 0;
316 }
317 
iss_video_buf_cleanup(struct vb2_buffer * vb)318 static void iss_video_buf_cleanup(struct vb2_buffer *vb)
319 {
320 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
321 	struct iss_buffer *buffer = container_of(vbuf, struct iss_buffer, vb);
322 
323 	if (buffer->iss_addr)
324 		buffer->iss_addr = 0;
325 }
326 
iss_video_buf_prepare(struct vb2_buffer * vb)327 static int iss_video_buf_prepare(struct vb2_buffer *vb)
328 {
329 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
330 	struct iss_video_fh *vfh = vb2_get_drv_priv(vb->vb2_queue);
331 	struct iss_buffer *buffer = container_of(vbuf, struct iss_buffer, vb);
332 	struct iss_video *video = vfh->video;
333 	unsigned long size = vfh->format.fmt.pix.sizeimage;
334 	dma_addr_t addr;
335 
336 	if (vb2_plane_size(vb, 0) < size)
337 		return -ENOBUFS;
338 
339 	addr = vb2_dma_contig_plane_dma_addr(vb, 0);
340 	if (!IS_ALIGNED(addr, 32)) {
341 		dev_dbg(video->iss->dev,
342 			"Buffer address must be aligned to 32 bytes boundary.\n");
343 		return -EINVAL;
344 	}
345 
346 	vb2_set_plane_payload(vb, 0, size);
347 	buffer->iss_addr = addr;
348 	return 0;
349 }
350 
iss_video_buf_queue(struct vb2_buffer * vb)351 static void iss_video_buf_queue(struct vb2_buffer *vb)
352 {
353 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
354 	struct iss_video_fh *vfh = vb2_get_drv_priv(vb->vb2_queue);
355 	struct iss_video *video = vfh->video;
356 	struct iss_buffer *buffer = container_of(vbuf, struct iss_buffer, vb);
357 	struct iss_pipeline *pipe = to_iss_pipeline(&video->video.entity);
358 	unsigned long flags;
359 	bool empty;
360 
361 	spin_lock_irqsave(&video->qlock, flags);
362 
363 	/* Mark the buffer is faulty and give it back to the queue immediately
364 	 * if the video node has registered an error. vb2 will perform the same
365 	 * check when preparing the buffer, but that is inherently racy, so we
366 	 * need to handle the race condition with an authoritative check here.
367 	 */
368 	if (unlikely(video->error)) {
369 		vb2_buffer_done(vb, VB2_BUF_STATE_ERROR);
370 		spin_unlock_irqrestore(&video->qlock, flags);
371 		return;
372 	}
373 
374 	empty = list_empty(&video->dmaqueue);
375 	list_add_tail(&buffer->list, &video->dmaqueue);
376 
377 	spin_unlock_irqrestore(&video->qlock, flags);
378 
379 	if (empty) {
380 		enum iss_pipeline_state state;
381 		unsigned int start;
382 
383 		if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
384 			state = ISS_PIPELINE_QUEUE_OUTPUT;
385 		else
386 			state = ISS_PIPELINE_QUEUE_INPUT;
387 
388 		spin_lock_irqsave(&pipe->lock, flags);
389 		pipe->state |= state;
390 		video->ops->queue(video, buffer);
391 		video->dmaqueue_flags |= ISS_VIDEO_DMAQUEUE_QUEUED;
392 
393 		start = iss_pipeline_ready(pipe);
394 		if (start)
395 			pipe->state |= ISS_PIPELINE_STREAM;
396 		spin_unlock_irqrestore(&pipe->lock, flags);
397 
398 		if (start)
399 			omap4iss_pipeline_set_stream(pipe,
400 						ISS_PIPELINE_STREAM_SINGLESHOT);
401 	}
402 }
403 
404 static const struct vb2_ops iss_video_vb2ops = {
405 	.queue_setup	= iss_video_queue_setup,
406 	.buf_prepare	= iss_video_buf_prepare,
407 	.buf_queue	= iss_video_buf_queue,
408 	.buf_cleanup	= iss_video_buf_cleanup,
409 };
410 
411 /*
412  * omap4iss_video_buffer_next - Complete the current buffer and return the next
413  * @video: ISS video object
414  *
415  * Remove the current video buffer from the DMA queue and fill its timestamp,
416  * field count and state fields before waking up its completion handler.
417  *
418  * For capture video nodes, the buffer state is set to VB2_BUF_STATE_DONE if no
419  * error has been flagged in the pipeline, or to VB2_BUF_STATE_ERROR otherwise.
420  *
421  * The DMA queue is expected to contain at least one buffer.
422  *
423  * Return a pointer to the next buffer in the DMA queue, or NULL if the queue is
424  * empty.
425  */
omap4iss_video_buffer_next(struct iss_video * video)426 struct iss_buffer *omap4iss_video_buffer_next(struct iss_video *video)
427 {
428 	struct iss_pipeline *pipe = to_iss_pipeline(&video->video.entity);
429 	enum iss_pipeline_state state;
430 	struct iss_buffer *buf;
431 	unsigned long flags;
432 
433 	spin_lock_irqsave(&video->qlock, flags);
434 	if (WARN_ON(list_empty(&video->dmaqueue))) {
435 		spin_unlock_irqrestore(&video->qlock, flags);
436 		return NULL;
437 	}
438 
439 	buf = list_first_entry(&video->dmaqueue, struct iss_buffer,
440 			       list);
441 	list_del(&buf->list);
442 	spin_unlock_irqrestore(&video->qlock, flags);
443 
444 	buf->vb.vb2_buf.timestamp = ktime_get_ns();
445 
446 	/* Do frame number propagation only if this is the output video node.
447 	 * Frame number either comes from the CSI receivers or it gets
448 	 * incremented here if H3A is not active.
449 	 * Note: There is no guarantee that the output buffer will finish
450 	 * first, so the input number might lag behind by 1 in some cases.
451 	 */
452 	if (video == pipe->output && !pipe->do_propagation)
453 		buf->vb.sequence =
454 			atomic_inc_return(&pipe->frame_number);
455 	else
456 		buf->vb.sequence = atomic_read(&pipe->frame_number);
457 
458 	vb2_buffer_done(&buf->vb.vb2_buf, pipe->error ?
459 			VB2_BUF_STATE_ERROR : VB2_BUF_STATE_DONE);
460 	pipe->error = false;
461 
462 	spin_lock_irqsave(&video->qlock, flags);
463 	if (list_empty(&video->dmaqueue)) {
464 		spin_unlock_irqrestore(&video->qlock, flags);
465 		if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
466 			state = ISS_PIPELINE_QUEUE_OUTPUT
467 			      | ISS_PIPELINE_STREAM;
468 		else
469 			state = ISS_PIPELINE_QUEUE_INPUT
470 			      | ISS_PIPELINE_STREAM;
471 
472 		spin_lock_irqsave(&pipe->lock, flags);
473 		pipe->state &= ~state;
474 		if (video->pipe.stream_state == ISS_PIPELINE_STREAM_CONTINUOUS)
475 			video->dmaqueue_flags |= ISS_VIDEO_DMAQUEUE_UNDERRUN;
476 		spin_unlock_irqrestore(&pipe->lock, flags);
477 		return NULL;
478 	}
479 
480 	if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && pipe->input) {
481 		spin_lock(&pipe->lock);
482 		pipe->state &= ~ISS_PIPELINE_STREAM;
483 		spin_unlock(&pipe->lock);
484 	}
485 
486 	buf = list_first_entry(&video->dmaqueue, struct iss_buffer,
487 			       list);
488 	spin_unlock_irqrestore(&video->qlock, flags);
489 	buf->vb.vb2_buf.state = VB2_BUF_STATE_ACTIVE;
490 	return buf;
491 }
492 
493 /*
494  * omap4iss_video_cancel_stream - Cancel stream on a video node
495  * @video: ISS video object
496  *
497  * Cancelling a stream mark all buffers on the video node as erroneous and makes
498  * sure no new buffer can be queued.
499  */
omap4iss_video_cancel_stream(struct iss_video * video)500 void omap4iss_video_cancel_stream(struct iss_video *video)
501 {
502 	unsigned long flags;
503 
504 	spin_lock_irqsave(&video->qlock, flags);
505 
506 	while (!list_empty(&video->dmaqueue)) {
507 		struct iss_buffer *buf;
508 
509 		buf = list_first_entry(&video->dmaqueue, struct iss_buffer,
510 				       list);
511 		list_del(&buf->list);
512 		vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
513 	}
514 
515 	vb2_queue_error(video->queue);
516 	video->error = true;
517 
518 	spin_unlock_irqrestore(&video->qlock, flags);
519 }
520 
521 /* -----------------------------------------------------------------------------
522  * V4L2 ioctls
523  */
524 
525 static int
iss_video_querycap(struct file * file,void * fh,struct v4l2_capability * cap)526 iss_video_querycap(struct file *file, void *fh, struct v4l2_capability *cap)
527 {
528 	struct iss_video *video = video_drvdata(file);
529 
530 	strlcpy(cap->driver, ISS_VIDEO_DRIVER_NAME, sizeof(cap->driver));
531 	strlcpy(cap->card, video->video.name, sizeof(cap->card));
532 	strlcpy(cap->bus_info, "media", sizeof(cap->bus_info));
533 
534 	if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
535 		cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
536 	else
537 		cap->device_caps = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_STREAMING;
538 
539 	cap->capabilities = V4L2_CAP_DEVICE_CAPS | V4L2_CAP_STREAMING
540 			  | V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VIDEO_OUTPUT;
541 
542 	return 0;
543 }
544 
545 static int
iss_video_enum_format(struct file * file,void * fh,struct v4l2_fmtdesc * f)546 iss_video_enum_format(struct file *file, void *fh, struct v4l2_fmtdesc *f)
547 {
548 	struct iss_video *video = video_drvdata(file);
549 	struct v4l2_mbus_framefmt format;
550 	unsigned int index = f->index;
551 	unsigned int i;
552 	int ret;
553 
554 	if (f->type != video->type)
555 		return -EINVAL;
556 
557 	ret = __iss_video_get_format(video, &format);
558 	if (ret < 0)
559 		return ret;
560 
561 	for (i = 0; i < ARRAY_SIZE(formats); ++i) {
562 		const struct iss_format_info *info = &formats[i];
563 
564 		if (format.code != info->code)
565 			continue;
566 
567 		if (index == 0) {
568 			f->pixelformat = info->pixelformat;
569 			strlcpy(f->description, info->description,
570 				sizeof(f->description));
571 			return 0;
572 		}
573 
574 		index--;
575 	}
576 
577 	return -EINVAL;
578 }
579 
580 static int
iss_video_get_format(struct file * file,void * fh,struct v4l2_format * format)581 iss_video_get_format(struct file *file, void *fh, struct v4l2_format *format)
582 {
583 	struct iss_video_fh *vfh = to_iss_video_fh(fh);
584 	struct iss_video *video = video_drvdata(file);
585 
586 	if (format->type != video->type)
587 		return -EINVAL;
588 
589 	mutex_lock(&video->mutex);
590 	*format = vfh->format;
591 	mutex_unlock(&video->mutex);
592 
593 	return 0;
594 }
595 
596 static int
iss_video_set_format(struct file * file,void * fh,struct v4l2_format * format)597 iss_video_set_format(struct file *file, void *fh, struct v4l2_format *format)
598 {
599 	struct iss_video_fh *vfh = to_iss_video_fh(fh);
600 	struct iss_video *video = video_drvdata(file);
601 	struct v4l2_mbus_framefmt fmt;
602 
603 	if (format->type != video->type)
604 		return -EINVAL;
605 
606 	mutex_lock(&video->mutex);
607 
608 	/* Fill the bytesperline and sizeimage fields by converting to media bus
609 	 * format and back to pixel format.
610 	 */
611 	iss_video_pix_to_mbus(&format->fmt.pix, &fmt);
612 	iss_video_mbus_to_pix(video, &fmt, &format->fmt.pix);
613 
614 	vfh->format = *format;
615 
616 	mutex_unlock(&video->mutex);
617 	return 0;
618 }
619 
620 static int
iss_video_try_format(struct file * file,void * fh,struct v4l2_format * format)621 iss_video_try_format(struct file *file, void *fh, struct v4l2_format *format)
622 {
623 	struct iss_video *video = video_drvdata(file);
624 	struct v4l2_subdev_format fmt;
625 	struct v4l2_subdev *subdev;
626 	u32 pad;
627 	int ret;
628 
629 	if (format->type != video->type)
630 		return -EINVAL;
631 
632 	subdev = iss_video_remote_subdev(video, &pad);
633 	if (!subdev)
634 		return -EINVAL;
635 
636 	iss_video_pix_to_mbus(&format->fmt.pix, &fmt.format);
637 
638 	fmt.pad = pad;
639 	fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
640 	ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &fmt);
641 	if (ret)
642 		return ret;
643 
644 	iss_video_mbus_to_pix(video, &fmt.format, &format->fmt.pix);
645 	return 0;
646 }
647 
648 static int
iss_video_get_selection(struct file * file,void * fh,struct v4l2_selection * sel)649 iss_video_get_selection(struct file *file, void *fh, struct v4l2_selection *sel)
650 {
651 	struct iss_video *video = video_drvdata(file);
652 	struct v4l2_subdev_format format;
653 	struct v4l2_subdev *subdev;
654 	struct v4l2_subdev_selection sdsel = {
655 		.which = V4L2_SUBDEV_FORMAT_ACTIVE,
656 		.target = sel->target,
657 	};
658 	u32 pad;
659 	int ret;
660 
661 	switch (sel->target) {
662 	case V4L2_SEL_TGT_CROP:
663 	case V4L2_SEL_TGT_CROP_BOUNDS:
664 	case V4L2_SEL_TGT_CROP_DEFAULT:
665 		if (video->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
666 			return -EINVAL;
667 		break;
668 	case V4L2_SEL_TGT_COMPOSE:
669 	case V4L2_SEL_TGT_COMPOSE_BOUNDS:
670 	case V4L2_SEL_TGT_COMPOSE_DEFAULT:
671 		if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
672 			return -EINVAL;
673 		break;
674 	default:
675 		return -EINVAL;
676 	}
677 	subdev = iss_video_remote_subdev(video, &pad);
678 	if (subdev == NULL)
679 		return -EINVAL;
680 
681 	/* Try the get selection operation first and fallback to get format if not
682 	 * implemented.
683 	 */
684 	sdsel.pad = pad;
685 	ret = v4l2_subdev_call(subdev, pad, get_selection, NULL, &sdsel);
686 	if (!ret)
687 		sel->r = sdsel.r;
688 	if (ret != -ENOIOCTLCMD)
689 		return ret;
690 
691 	format.pad = pad;
692 	format.which = V4L2_SUBDEV_FORMAT_ACTIVE;
693 	ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &format);
694 	if (ret < 0)
695 		return ret == -ENOIOCTLCMD ? -ENOTTY : ret;
696 
697 	sel->r.left = 0;
698 	sel->r.top = 0;
699 	sel->r.width = format.format.width;
700 	sel->r.height = format.format.height;
701 
702 	return 0;
703 }
704 
705 static int
iss_video_set_selection(struct file * file,void * fh,struct v4l2_selection * sel)706 iss_video_set_selection(struct file *file, void *fh, struct v4l2_selection *sel)
707 {
708 	struct iss_video *video = video_drvdata(file);
709 	struct v4l2_subdev *subdev;
710 	struct v4l2_subdev_selection sdsel = {
711 		.which = V4L2_SUBDEV_FORMAT_ACTIVE,
712 		.target = sel->target,
713 		.flags = sel->flags,
714 		.r = sel->r,
715 	};
716 	u32 pad;
717 	int ret;
718 
719 	switch (sel->target) {
720 	case V4L2_SEL_TGT_CROP:
721 		if (video->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
722 			return -EINVAL;
723 		break;
724 	case V4L2_SEL_TGT_COMPOSE:
725 		if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
726 			return -EINVAL;
727 		break;
728 	default:
729 		return -EINVAL;
730 	}
731 	subdev = iss_video_remote_subdev(video, &pad);
732 	if (subdev == NULL)
733 		return -EINVAL;
734 
735 	sdsel.pad = pad;
736 	mutex_lock(&video->mutex);
737 	ret = v4l2_subdev_call(subdev, pad, set_selection, NULL, &sdsel);
738 	mutex_unlock(&video->mutex);
739 	if (!ret)
740 		sel->r = sdsel.r;
741 
742 	return ret == -ENOIOCTLCMD ? -ENOTTY : ret;
743 }
744 
745 static int
iss_video_get_param(struct file * file,void * fh,struct v4l2_streamparm * a)746 iss_video_get_param(struct file *file, void *fh, struct v4l2_streamparm *a)
747 {
748 	struct iss_video_fh *vfh = to_iss_video_fh(fh);
749 	struct iss_video *video = video_drvdata(file);
750 
751 	if (video->type != V4L2_BUF_TYPE_VIDEO_OUTPUT ||
752 	    video->type != a->type)
753 		return -EINVAL;
754 
755 	memset(a, 0, sizeof(*a));
756 	a->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
757 	a->parm.output.capability = V4L2_CAP_TIMEPERFRAME;
758 	a->parm.output.timeperframe = vfh->timeperframe;
759 
760 	return 0;
761 }
762 
763 static int
iss_video_set_param(struct file * file,void * fh,struct v4l2_streamparm * a)764 iss_video_set_param(struct file *file, void *fh, struct v4l2_streamparm *a)
765 {
766 	struct iss_video_fh *vfh = to_iss_video_fh(fh);
767 	struct iss_video *video = video_drvdata(file);
768 
769 	if (video->type != V4L2_BUF_TYPE_VIDEO_OUTPUT ||
770 	    video->type != a->type)
771 		return -EINVAL;
772 
773 	if (a->parm.output.timeperframe.denominator == 0)
774 		a->parm.output.timeperframe.denominator = 1;
775 
776 	vfh->timeperframe = a->parm.output.timeperframe;
777 
778 	return 0;
779 }
780 
781 static int
iss_video_reqbufs(struct file * file,void * fh,struct v4l2_requestbuffers * rb)782 iss_video_reqbufs(struct file *file, void *fh, struct v4l2_requestbuffers *rb)
783 {
784 	struct iss_video_fh *vfh = to_iss_video_fh(fh);
785 
786 	return vb2_reqbufs(&vfh->queue, rb);
787 }
788 
789 static int
iss_video_querybuf(struct file * file,void * fh,struct v4l2_buffer * b)790 iss_video_querybuf(struct file *file, void *fh, struct v4l2_buffer *b)
791 {
792 	struct iss_video_fh *vfh = to_iss_video_fh(fh);
793 
794 	return vb2_querybuf(&vfh->queue, b);
795 }
796 
797 static int
iss_video_qbuf(struct file * file,void * fh,struct v4l2_buffer * b)798 iss_video_qbuf(struct file *file, void *fh, struct v4l2_buffer *b)
799 {
800 	struct iss_video_fh *vfh = to_iss_video_fh(fh);
801 
802 	return vb2_qbuf(&vfh->queue, b);
803 }
804 
805 static int
iss_video_expbuf(struct file * file,void * fh,struct v4l2_exportbuffer * e)806 iss_video_expbuf(struct file *file, void *fh, struct v4l2_exportbuffer *e)
807 {
808 	struct iss_video_fh *vfh = to_iss_video_fh(fh);
809 
810 	return vb2_expbuf(&vfh->queue, e);
811 }
812 
813 static int
iss_video_dqbuf(struct file * file,void * fh,struct v4l2_buffer * b)814 iss_video_dqbuf(struct file *file, void *fh, struct v4l2_buffer *b)
815 {
816 	struct iss_video_fh *vfh = to_iss_video_fh(fh);
817 
818 	return vb2_dqbuf(&vfh->queue, b, file->f_flags & O_NONBLOCK);
819 }
820 
821 /*
822  * Stream management
823  *
824  * Every ISS pipeline has a single input and a single output. The input can be
825  * either a sensor or a video node. The output is always a video node.
826  *
827  * As every pipeline has an output video node, the ISS video objects at the
828  * pipeline output stores the pipeline state. It tracks the streaming state of
829  * both the input and output, as well as the availability of buffers.
830  *
831  * In sensor-to-memory mode, frames are always available at the pipeline input.
832  * Starting the sensor usually requires I2C transfers and must be done in
833  * interruptible context. The pipeline is started and stopped synchronously
834  * to the stream on/off commands. All modules in the pipeline will get their
835  * subdev set stream handler called. The module at the end of the pipeline must
836  * delay starting the hardware until buffers are available at its output.
837  *
838  * In memory-to-memory mode, starting/stopping the stream requires
839  * synchronization between the input and output. ISS modules can't be stopped
840  * in the middle of a frame, and at least some of the modules seem to become
841  * busy as soon as they're started, even if they don't receive a frame start
842  * event. For that reason frames need to be processed in single-shot mode. The
843  * driver needs to wait until a frame is completely processed and written to
844  * memory before restarting the pipeline for the next frame. Pipelined
845  * processing might be possible but requires more testing.
846  *
847  * Stream start must be delayed until buffers are available at both the input
848  * and output. The pipeline must be started in the videobuf queue callback with
849  * the buffers queue spinlock held. The modules subdev set stream operation must
850  * not sleep.
851  */
852 static int
iss_video_streamon(struct file * file,void * fh,enum v4l2_buf_type type)853 iss_video_streamon(struct file *file, void *fh, enum v4l2_buf_type type)
854 {
855 	struct iss_video_fh *vfh = to_iss_video_fh(fh);
856 	struct iss_video *video = video_drvdata(file);
857 	struct media_entity_graph graph;
858 	struct media_entity *entity = &video->video.entity;
859 	enum iss_pipeline_state state;
860 	struct iss_pipeline *pipe;
861 	struct iss_video *far_end;
862 	unsigned long flags;
863 	int ret;
864 
865 	if (type != video->type)
866 		return -EINVAL;
867 
868 	mutex_lock(&video->stream_lock);
869 
870 	/* Start streaming on the pipeline. No link touching an entity in the
871 	 * pipeline can be activated or deactivated once streaming is started.
872 	 */
873 	pipe = entity->pipe
874 	     ? to_iss_pipeline(entity) : &video->pipe;
875 	pipe->external = NULL;
876 	pipe->external_rate = 0;
877 	pipe->external_bpp = 0;
878 
879 	ret = media_entity_enum_init(&pipe->ent_enum, entity->graph_obj.mdev);
880 	if (ret)
881 		goto err_graph_walk_init;
882 
883 	ret = media_entity_graph_walk_init(&graph, entity->graph_obj.mdev);
884 	if (ret)
885 		goto err_graph_walk_init;
886 
887 	if (video->iss->pdata->set_constraints)
888 		video->iss->pdata->set_constraints(video->iss, true);
889 
890 	ret = media_entity_pipeline_start(entity, &pipe->pipe);
891 	if (ret < 0)
892 		goto err_media_entity_pipeline_start;
893 
894 	media_entity_graph_walk_start(&graph, entity);
895 	while ((entity = media_entity_graph_walk_next(&graph)))
896 		media_entity_enum_set(&pipe->ent_enum, entity);
897 
898 	/* Verify that the currently configured format matches the output of
899 	 * the connected subdev.
900 	 */
901 	ret = iss_video_check_format(video, vfh);
902 	if (ret < 0)
903 		goto err_iss_video_check_format;
904 
905 	video->bpl_padding = ret;
906 	video->bpl_value = vfh->format.fmt.pix.bytesperline;
907 
908 	/* Find the ISS video node connected at the far end of the pipeline and
909 	 * update the pipeline.
910 	 */
911 	far_end = iss_video_far_end(video);
912 
913 	if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
914 		state = ISS_PIPELINE_STREAM_OUTPUT | ISS_PIPELINE_IDLE_OUTPUT;
915 		pipe->input = far_end;
916 		pipe->output = video;
917 	} else {
918 		if (!far_end) {
919 			ret = -EPIPE;
920 			goto err_iss_video_check_format;
921 		}
922 
923 		state = ISS_PIPELINE_STREAM_INPUT | ISS_PIPELINE_IDLE_INPUT;
924 		pipe->input = video;
925 		pipe->output = far_end;
926 	}
927 
928 	spin_lock_irqsave(&pipe->lock, flags);
929 	pipe->state &= ~ISS_PIPELINE_STREAM;
930 	pipe->state |= state;
931 	spin_unlock_irqrestore(&pipe->lock, flags);
932 
933 	/* Set the maximum time per frame as the value requested by userspace.
934 	 * This is a soft limit that can be overridden if the hardware doesn't
935 	 * support the request limit.
936 	 */
937 	if (video->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
938 		pipe->max_timeperframe = vfh->timeperframe;
939 
940 	video->queue = &vfh->queue;
941 	INIT_LIST_HEAD(&video->dmaqueue);
942 	video->error = false;
943 	atomic_set(&pipe->frame_number, -1);
944 
945 	ret = vb2_streamon(&vfh->queue, type);
946 	if (ret < 0)
947 		goto err_iss_video_check_format;
948 
949 	/* In sensor-to-memory mode, the stream can be started synchronously
950 	 * to the stream on command. In memory-to-memory mode, it will be
951 	 * started when buffers are queued on both the input and output.
952 	 */
953 	if (!pipe->input) {
954 		unsigned long flags;
955 
956 		ret = omap4iss_pipeline_set_stream(pipe,
957 					      ISS_PIPELINE_STREAM_CONTINUOUS);
958 		if (ret < 0)
959 			goto err_omap4iss_set_stream;
960 		spin_lock_irqsave(&video->qlock, flags);
961 		if (list_empty(&video->dmaqueue))
962 			video->dmaqueue_flags |= ISS_VIDEO_DMAQUEUE_UNDERRUN;
963 		spin_unlock_irqrestore(&video->qlock, flags);
964 	}
965 
966 	media_entity_graph_walk_cleanup(&graph);
967 
968 	mutex_unlock(&video->stream_lock);
969 
970 	return 0;
971 
972 err_omap4iss_set_stream:
973 	vb2_streamoff(&vfh->queue, type);
974 err_iss_video_check_format:
975 	media_entity_pipeline_stop(&video->video.entity);
976 err_media_entity_pipeline_start:
977 	if (video->iss->pdata->set_constraints)
978 		video->iss->pdata->set_constraints(video->iss, false);
979 	video->queue = NULL;
980 
981 	media_entity_graph_walk_cleanup(&graph);
982 
983 err_graph_walk_init:
984 	media_entity_enum_cleanup(&pipe->ent_enum);
985 
986 	mutex_unlock(&video->stream_lock);
987 
988 	return ret;
989 }
990 
991 static int
iss_video_streamoff(struct file * file,void * fh,enum v4l2_buf_type type)992 iss_video_streamoff(struct file *file, void *fh, enum v4l2_buf_type type)
993 {
994 	struct iss_video_fh *vfh = to_iss_video_fh(fh);
995 	struct iss_video *video = video_drvdata(file);
996 	struct iss_pipeline *pipe = to_iss_pipeline(&video->video.entity);
997 	enum iss_pipeline_state state;
998 	unsigned long flags;
999 
1000 	if (type != video->type)
1001 		return -EINVAL;
1002 
1003 	mutex_lock(&video->stream_lock);
1004 
1005 	if (!vb2_is_streaming(&vfh->queue))
1006 		goto done;
1007 
1008 	/* Update the pipeline state. */
1009 	if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1010 		state = ISS_PIPELINE_STREAM_OUTPUT
1011 		      | ISS_PIPELINE_QUEUE_OUTPUT;
1012 	else
1013 		state = ISS_PIPELINE_STREAM_INPUT
1014 		      | ISS_PIPELINE_QUEUE_INPUT;
1015 
1016 	spin_lock_irqsave(&pipe->lock, flags);
1017 	pipe->state &= ~state;
1018 	spin_unlock_irqrestore(&pipe->lock, flags);
1019 
1020 	/* Stop the stream. */
1021 	omap4iss_pipeline_set_stream(pipe, ISS_PIPELINE_STREAM_STOPPED);
1022 	vb2_streamoff(&vfh->queue, type);
1023 	video->queue = NULL;
1024 
1025 	media_entity_enum_cleanup(&pipe->ent_enum);
1026 
1027 	if (video->iss->pdata->set_constraints)
1028 		video->iss->pdata->set_constraints(video->iss, false);
1029 	media_entity_pipeline_stop(&video->video.entity);
1030 
1031 done:
1032 	mutex_unlock(&video->stream_lock);
1033 	return 0;
1034 }
1035 
1036 static int
iss_video_enum_input(struct file * file,void * fh,struct v4l2_input * input)1037 iss_video_enum_input(struct file *file, void *fh, struct v4l2_input *input)
1038 {
1039 	if (input->index > 0)
1040 		return -EINVAL;
1041 
1042 	strlcpy(input->name, "camera", sizeof(input->name));
1043 	input->type = V4L2_INPUT_TYPE_CAMERA;
1044 
1045 	return 0;
1046 }
1047 
1048 static int
iss_video_g_input(struct file * file,void * fh,unsigned int * input)1049 iss_video_g_input(struct file *file, void *fh, unsigned int *input)
1050 {
1051 	*input = 0;
1052 
1053 	return 0;
1054 }
1055 
1056 static int
iss_video_s_input(struct file * file,void * fh,unsigned int input)1057 iss_video_s_input(struct file *file, void *fh, unsigned int input)
1058 {
1059 	return input == 0 ? 0 : -EINVAL;
1060 }
1061 
1062 static const struct v4l2_ioctl_ops iss_video_ioctl_ops = {
1063 	.vidioc_querycap		= iss_video_querycap,
1064 	.vidioc_enum_fmt_vid_cap        = iss_video_enum_format,
1065 	.vidioc_g_fmt_vid_cap		= iss_video_get_format,
1066 	.vidioc_s_fmt_vid_cap		= iss_video_set_format,
1067 	.vidioc_try_fmt_vid_cap		= iss_video_try_format,
1068 	.vidioc_g_fmt_vid_out		= iss_video_get_format,
1069 	.vidioc_s_fmt_vid_out		= iss_video_set_format,
1070 	.vidioc_try_fmt_vid_out		= iss_video_try_format,
1071 	.vidioc_g_selection		= iss_video_get_selection,
1072 	.vidioc_s_selection		= iss_video_set_selection,
1073 	.vidioc_g_parm			= iss_video_get_param,
1074 	.vidioc_s_parm			= iss_video_set_param,
1075 	.vidioc_reqbufs			= iss_video_reqbufs,
1076 	.vidioc_querybuf		= iss_video_querybuf,
1077 	.vidioc_qbuf			= iss_video_qbuf,
1078 	.vidioc_expbuf			= iss_video_expbuf,
1079 	.vidioc_dqbuf			= iss_video_dqbuf,
1080 	.vidioc_streamon		= iss_video_streamon,
1081 	.vidioc_streamoff		= iss_video_streamoff,
1082 	.vidioc_enum_input		= iss_video_enum_input,
1083 	.vidioc_g_input			= iss_video_g_input,
1084 	.vidioc_s_input			= iss_video_s_input,
1085 };
1086 
1087 /* -----------------------------------------------------------------------------
1088  * V4L2 file operations
1089  */
1090 
iss_video_open(struct file * file)1091 static int iss_video_open(struct file *file)
1092 {
1093 	struct iss_video *video = video_drvdata(file);
1094 	struct iss_video_fh *handle;
1095 	struct vb2_queue *q;
1096 	int ret = 0;
1097 
1098 	handle = kzalloc(sizeof(*handle), GFP_KERNEL);
1099 	if (!handle)
1100 		return -ENOMEM;
1101 
1102 	v4l2_fh_init(&handle->vfh, &video->video);
1103 	v4l2_fh_add(&handle->vfh);
1104 
1105 	/* If this is the first user, initialise the pipeline. */
1106 	if (!omap4iss_get(video->iss)) {
1107 		ret = -EBUSY;
1108 		goto done;
1109 	}
1110 
1111 	ret = v4l2_pipeline_pm_use(&video->video.entity, 1);
1112 	if (ret < 0) {
1113 		omap4iss_put(video->iss);
1114 		goto done;
1115 	}
1116 
1117 	q = &handle->queue;
1118 
1119 	q->type = video->type;
1120 	q->io_modes = VB2_MMAP | VB2_DMABUF;
1121 	q->drv_priv = handle;
1122 	q->ops = &iss_video_vb2ops;
1123 	q->mem_ops = &vb2_dma_contig_memops;
1124 	q->buf_struct_size = sizeof(struct iss_buffer);
1125 	q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1126 	q->dev = video->iss->dev;
1127 
1128 	ret = vb2_queue_init(q);
1129 	if (ret) {
1130 		omap4iss_put(video->iss);
1131 		goto done;
1132 	}
1133 
1134 	memset(&handle->format, 0, sizeof(handle->format));
1135 	handle->format.type = video->type;
1136 	handle->timeperframe.denominator = 1;
1137 
1138 	handle->video = video;
1139 	file->private_data = &handle->vfh;
1140 
1141 done:
1142 	if (ret < 0) {
1143 		v4l2_fh_del(&handle->vfh);
1144 		kfree(handle);
1145 	}
1146 
1147 	return ret;
1148 }
1149 
iss_video_release(struct file * file)1150 static int iss_video_release(struct file *file)
1151 {
1152 	struct iss_video *video = video_drvdata(file);
1153 	struct v4l2_fh *vfh = file->private_data;
1154 	struct iss_video_fh *handle = to_iss_video_fh(vfh);
1155 
1156 	/* Disable streaming and free the buffers queue resources. */
1157 	iss_video_streamoff(file, vfh, video->type);
1158 
1159 	v4l2_pipeline_pm_use(&video->video.entity, 0);
1160 
1161 	/* Release the videobuf2 queue */
1162 	vb2_queue_release(&handle->queue);
1163 
1164 	v4l2_fh_del(vfh);
1165 	kfree(handle);
1166 	file->private_data = NULL;
1167 
1168 	omap4iss_put(video->iss);
1169 
1170 	return 0;
1171 }
1172 
iss_video_poll(struct file * file,poll_table * wait)1173 static unsigned int iss_video_poll(struct file *file, poll_table *wait)
1174 {
1175 	struct iss_video_fh *vfh = to_iss_video_fh(file->private_data);
1176 
1177 	return vb2_poll(&vfh->queue, file, wait);
1178 }
1179 
iss_video_mmap(struct file * file,struct vm_area_struct * vma)1180 static int iss_video_mmap(struct file *file, struct vm_area_struct *vma)
1181 {
1182 	struct iss_video_fh *vfh = to_iss_video_fh(file->private_data);
1183 
1184 	return vb2_mmap(&vfh->queue, vma);
1185 }
1186 
1187 static struct v4l2_file_operations iss_video_fops = {
1188 	.owner = THIS_MODULE,
1189 	.unlocked_ioctl = video_ioctl2,
1190 	.open = iss_video_open,
1191 	.release = iss_video_release,
1192 	.poll = iss_video_poll,
1193 	.mmap = iss_video_mmap,
1194 };
1195 
1196 /* -----------------------------------------------------------------------------
1197  * ISS video core
1198  */
1199 
1200 static const struct iss_video_operations iss_video_dummy_ops = {
1201 };
1202 
omap4iss_video_init(struct iss_video * video,const char * name)1203 int omap4iss_video_init(struct iss_video *video, const char *name)
1204 {
1205 	const char *direction;
1206 	int ret;
1207 
1208 	switch (video->type) {
1209 	case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1210 		direction = "output";
1211 		video->pad.flags = MEDIA_PAD_FL_SINK;
1212 		break;
1213 	case V4L2_BUF_TYPE_VIDEO_OUTPUT:
1214 		direction = "input";
1215 		video->pad.flags = MEDIA_PAD_FL_SOURCE;
1216 		break;
1217 
1218 	default:
1219 		return -EINVAL;
1220 	}
1221 
1222 	ret = media_entity_pads_init(&video->video.entity, 1, &video->pad);
1223 	if (ret < 0)
1224 		return ret;
1225 
1226 	spin_lock_init(&video->qlock);
1227 	mutex_init(&video->mutex);
1228 	atomic_set(&video->active, 0);
1229 
1230 	spin_lock_init(&video->pipe.lock);
1231 	mutex_init(&video->stream_lock);
1232 
1233 	/* Initialize the video device. */
1234 	if (!video->ops)
1235 		video->ops = &iss_video_dummy_ops;
1236 
1237 	video->video.fops = &iss_video_fops;
1238 	snprintf(video->video.name, sizeof(video->video.name),
1239 		 "OMAP4 ISS %s %s", name, direction);
1240 	video->video.vfl_type = VFL_TYPE_GRABBER;
1241 	video->video.release = video_device_release_empty;
1242 	video->video.ioctl_ops = &iss_video_ioctl_ops;
1243 	video->pipe.stream_state = ISS_PIPELINE_STREAM_STOPPED;
1244 
1245 	video_set_drvdata(&video->video, video);
1246 
1247 	return 0;
1248 }
1249 
omap4iss_video_cleanup(struct iss_video * video)1250 void omap4iss_video_cleanup(struct iss_video *video)
1251 {
1252 	media_entity_cleanup(&video->video.entity);
1253 	mutex_destroy(&video->stream_lock);
1254 	mutex_destroy(&video->mutex);
1255 }
1256 
omap4iss_video_register(struct iss_video * video,struct v4l2_device * vdev)1257 int omap4iss_video_register(struct iss_video *video, struct v4l2_device *vdev)
1258 {
1259 	int ret;
1260 
1261 	video->video.v4l2_dev = vdev;
1262 
1263 	ret = video_register_device(&video->video, VFL_TYPE_GRABBER, -1);
1264 	if (ret < 0)
1265 		dev_err(video->iss->dev,
1266 			"could not register video device (%d)\n", ret);
1267 
1268 	return ret;
1269 }
1270 
omap4iss_video_unregister(struct iss_video * video)1271 void omap4iss_video_unregister(struct iss_video *video)
1272 {
1273 	video_unregister_device(&video->video);
1274 }
1275