videobuf2-core.c 66.4 KB
Newer Older
1
/*
2
 * videobuf2-core.c - video buffer 2 core framework
3 4 5
 *
 * Copyright (C) 2010 Samsung Electronics
 *
6
 * Author: Pawel Osciak <pawel@osciak.com>
7 8
 *	   Marek Szyprowski <m.szyprowski@samsung.com>
 *
H
Hans Verkuil 已提交
9 10 11
 * The vb2_thread implementation was based on code from videobuf-dvb.c:
 *	(c) 2004 Gerd Knorr <kraxel@bytesex.org> [SUSE Labs]
 *
12 13 14 15 16
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation.
 */

17 18
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

19 20 21 22 23 24 25
#include <linux/err.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/mm.h>
#include <linux/poll.h>
#include <linux/slab.h>
#include <linux/sched.h>
H
Hans Verkuil 已提交
26 27
#include <linux/freezer.h>
#include <linux/kthread.h>
28

29
#include <media/videobuf2-core.h>
30
#include <media/v4l2-mc.h>
31

32
#include <trace/events/vb2.h>
33

34 35
static int debug;
module_param(debug, int, 0644);
36

37 38 39 40
#define dprintk(level, fmt, arg...)				\
	do {							\
		if (debug >= level)				\
			pr_info("%s: " fmt, __func__, ## arg);	\
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
	} while (0)

#ifdef CONFIG_VIDEO_ADV_DEBUG

/*
 * If advanced debugging is on, then count how often each op is called
 * successfully, which can either be per-buffer or per-queue.
 *
 * This makes it easy to check that the 'init' and 'cleanup'
 * (and variations thereof) stay balanced.
 */

#define log_memop(vb, op)						\
	dprintk(2, "call_memop(%p, %d, %s)%s\n",			\
		(vb)->vb2_queue, (vb)->index, #op,			\
		(vb)->vb2_queue->mem_ops->op ? "" : " (nop)")

#define call_memop(vb, op, args...)					\
({									\
	struct vb2_queue *_q = (vb)->vb2_queue;				\
	int err;							\
									\
	log_memop(vb, op);						\
	err = _q->mem_ops->op ? _q->mem_ops->op(args) : 0;		\
	if (!err)							\
		(vb)->cnt_mem_ ## op++;					\
	err;								\
})

#define call_ptr_memop(vb, op, args...)					\
({									\
	struct vb2_queue *_q = (vb)->vb2_queue;				\
	void *ptr;							\
									\
	log_memop(vb, op);						\
	ptr = _q->mem_ops->op ? _q->mem_ops->op(args) : NULL;		\
	if (!IS_ERR_OR_NULL(ptr))					\
		(vb)->cnt_mem_ ## op++;					\
	ptr;								\
})

#define call_void_memop(vb, op, args...)				\
({									\
	struct vb2_queue *_q = (vb)->vb2_queue;				\
									\
	log_memop(vb, op);						\
	if (_q->mem_ops->op)						\
		_q->mem_ops->op(args);					\
	(vb)->cnt_mem_ ## op++;						\
})

#define log_qop(q, op)							\
	dprintk(2, "call_qop(%p, %s)%s\n", q, #op,			\
		(q)->ops->op ? "" : " (nop)")

#define call_qop(q, op, args...)					\
({									\
	int err;							\
									\
	log_qop(q, op);							\
	err = (q)->ops->op ? (q)->ops->op(args) : 0;			\
	if (!err)							\
		(q)->cnt_ ## op++;					\
	err;								\
})

#define call_void_qop(q, op, args...)					\
({									\
	log_qop(q, op);							\
	if ((q)->ops->op)						\
		(q)->ops->op(args);					\
	(q)->cnt_ ## op++;						\
})

#define log_vb_qop(vb, op, args...)					\
	dprintk(2, "call_vb_qop(%p, %d, %s)%s\n",			\
		(vb)->vb2_queue, (vb)->index, #op,			\
		(vb)->vb2_queue->ops->op ? "" : " (nop)")

#define call_vb_qop(vb, op, args...)					\
({									\
	int err;							\
									\
	log_vb_qop(vb, op);						\
	err = (vb)->vb2_queue->ops->op ?				\
		(vb)->vb2_queue->ops->op(args) : 0;			\
	if (!err)							\
		(vb)->cnt_ ## op++;					\
	err;								\
})

#define call_void_vb_qop(vb, op, args...)				\
({									\
	log_vb_qop(vb, op);						\
	if ((vb)->vb2_queue->ops->op)					\
		(vb)->vb2_queue->ops->op(args);				\
	(vb)->cnt_ ## op++;						\
})

#else

#define call_memop(vb, op, args...)					\
	((vb)->vb2_queue->mem_ops->op ?					\
		(vb)->vb2_queue->mem_ops->op(args) : 0)

#define call_ptr_memop(vb, op, args...)					\
	((vb)->vb2_queue->mem_ops->op ?					\
		(vb)->vb2_queue->mem_ops->op(args) : NULL)

#define call_void_memop(vb, op, args...)				\
	do {								\
		if ((vb)->vb2_queue->mem_ops->op)			\
			(vb)->vb2_queue->mem_ops->op(args);		\
	} while (0)

#define call_qop(q, op, args...)					\
	((q)->ops->op ? (q)->ops->op(args) : 0)

#define call_void_qop(q, op, args...)					\
	do {								\
		if ((q)->ops->op)					\
			(q)->ops->op(args);				\
	} while (0)

#define call_vb_qop(vb, op, args...)					\
	((vb)->vb2_queue->ops->op ? (vb)->vb2_queue->ops->op(args) : 0)

#define call_void_vb_qop(vb, op, args...)				\
	do {								\
		if ((vb)->vb2_queue->ops->op)				\
			(vb)->vb2_queue->ops->op(args);			\
	} while (0)

#endif

#define call_bufop(q, op, args...)					\
({									\
	int ret = 0;							\
	if (q && q->buf_ops && q->buf_ops->op)				\
		ret = q->buf_ops->op(args);				\
	ret;								\
})
183

184 185 186 187 188 189
#define call_void_bufop(q, op, args...)					\
({									\
	if (q && q->buf_ops && q->buf_ops->op)				\
		q->buf_ops->op(args);					\
})

190
static void __vb2_queue_cancel(struct vb2_queue *q);
191
static void __enqueue_in_driver(struct vb2_buffer *vb);
192

193
/*
194 195
 * __vb2_buf_mem_alloc() - allocate video memory for the given buffer
 */
196
static int __vb2_buf_mem_alloc(struct vb2_buffer *vb)
197 198 199 200
{
	struct vb2_queue *q = vb->vb2_queue;
	void *mem_priv;
	int plane;
201
	int ret = -ENOMEM;
202

203 204 205 206
	/*
	 * Allocate memory for all planes in this buffer
	 * NOTE: mmapped areas should be page aligned
	 */
207
	for (plane = 0; plane < vb->num_planes; ++plane) {
208
		unsigned long size = PAGE_ALIGN(vb->planes[plane].length);
209

210
		mem_priv = call_ptr_memop(vb, alloc,
211
				q->alloc_devs[plane] ? : q->dev,
212
				q->dma_attrs, size, q->dma_dir, q->gfp_flags);
213
		if (IS_ERR_OR_NULL(mem_priv)) {
214 215
			if (mem_priv)
				ret = PTR_ERR(mem_priv);
216
			goto free;
217
		}
218 219 220 221 222 223 224 225

		/* Associate allocator private data with this plane */
		vb->planes[plane].mem_priv = mem_priv;
	}

	return 0;
free:
	/* Free already allocated memory if one of the allocations failed */
226
	for (; plane > 0; --plane) {
227
		call_void_memop(vb, put, vb->planes[plane - 1].mem_priv);
228 229
		vb->planes[plane - 1].mem_priv = NULL;
	}
230

231
	return ret;
232 233
}

234
/*
235 236 237 238 239 240 241
 * __vb2_buf_mem_free() - free memory of the given buffer
 */
static void __vb2_buf_mem_free(struct vb2_buffer *vb)
{
	unsigned int plane;

	for (plane = 0; plane < vb->num_planes; ++plane) {
242
		call_void_memop(vb, put, vb->planes[plane].mem_priv);
243
		vb->planes[plane].mem_priv = NULL;
244
		dprintk(3, "freed plane %d of buffer %d\n", plane, vb->index);
245 246 247
	}
}

248
/*
249 250 251 252 253 254 255 256
 * __vb2_buf_userptr_put() - release userspace memory associated with
 * a USERPTR buffer
 */
static void __vb2_buf_userptr_put(struct vb2_buffer *vb)
{
	unsigned int plane;

	for (plane = 0; plane < vb->num_planes; ++plane) {
257
		if (vb->planes[plane].mem_priv)
258
			call_void_memop(vb, put_userptr, vb->planes[plane].mem_priv);
259
		vb->planes[plane].mem_priv = NULL;
260 261 262
	}
}

263
/*
264 265 266
 * __vb2_plane_dmabuf_put() - release memory associated with
 * a DMABUF shared plane
 */
267
static void __vb2_plane_dmabuf_put(struct vb2_buffer *vb, struct vb2_plane *p)
268 269 270 271 272
{
	if (!p->mem_priv)
		return;

	if (p->dbuf_mapped)
273
		call_void_memop(vb, unmap_dmabuf, p->mem_priv);
274

275
	call_void_memop(vb, detach_dmabuf, p->mem_priv);
276
	dma_buf_put(p->dbuf);
277 278 279
	p->mem_priv = NULL;
	p->dbuf = NULL;
	p->dbuf_mapped = 0;
280 281
}

282
/*
283 284 285 286 287 288 289 290
 * __vb2_buf_dmabuf_put() - release memory associated with
 * a DMABUF shared buffer
 */
static void __vb2_buf_dmabuf_put(struct vb2_buffer *vb)
{
	unsigned int plane;

	for (plane = 0; plane < vb->num_planes; ++plane)
291
		__vb2_plane_dmabuf_put(vb, &vb->planes[plane]);
292 293
}

294
/*
295
 * __setup_offsets() - setup unique offsets ("cookies") for every plane in
296
 * the buffer.
297
 */
298
static void __setup_offsets(struct vb2_buffer *vb)
299
{
300 301 302 303 304 305 306
	struct vb2_queue *q = vb->vb2_queue;
	unsigned int plane;
	unsigned long off = 0;

	if (vb->index) {
		struct vb2_buffer *prev = q->bufs[vb->index - 1];
		struct vb2_plane *p = &prev->planes[prev->num_planes - 1];
307

308
		off = PAGE_ALIGN(p->m.offset + p->length);
309 310
	}

311 312
	for (plane = 0; plane < vb->num_planes; ++plane) {
		vb->planes[plane].m.offset = off;
313

314 315
		dprintk(3, "buffer %d, plane %d offset 0x%08lx\n",
				vb->index, plane, off);
316

317 318
		off += vb->planes[plane].length;
		off = PAGE_ALIGN(off);
319 320 321
	}
}

322
/*
323 324 325 326 327 328
 * __vb2_queue_alloc() - allocate videobuf buffer structures and (for MMAP type)
 * video buffer memory for all buffers/planes on the queue and initializes the
 * queue
 *
 * Returns the number of buffers successfully allocated.
 */
329
static int __vb2_queue_alloc(struct vb2_queue *q, enum vb2_memory memory,
330 331
			     unsigned int num_buffers, unsigned int num_planes,
			     const unsigned plane_sizes[VB2_MAX_PLANES])
332
{
333
	unsigned int buffer, plane;
334 335 336
	struct vb2_buffer *vb;
	int ret;

337 338 339 340
	/* Ensure that q->num_buffers+num_buffers is below VB2_MAX_FRAME */
	num_buffers = min_t(unsigned int, num_buffers,
			    VB2_MAX_FRAME - q->num_buffers);

341 342 343 344
	for (buffer = 0; buffer < num_buffers; ++buffer) {
		/* Allocate videobuf buffer structures */
		vb = kzalloc(q->buf_struct_size, GFP_KERNEL);
		if (!vb) {
345
			dprintk(1, "memory alloc for buffer struct failed\n");
346 347 348 349 350 351
			break;
		}

		vb->state = VB2_BUF_STATE_DEQUEUED;
		vb->vb2_queue = q;
		vb->num_planes = num_planes;
352 353 354
		vb->index = q->num_buffers + buffer;
		vb->type = q->type;
		vb->memory = memory;
355 356 357 358
		for (plane = 0; plane < num_planes; ++plane) {
			vb->planes[plane].length = plane_sizes[plane];
			vb->planes[plane].min_length = plane_sizes[plane];
		}
359
		q->bufs[vb->index] = vb;
360 361

		/* Allocate video buffer memory for the MMAP type */
362
		if (memory == VB2_MEMORY_MMAP) {
363
			ret = __vb2_buf_mem_alloc(vb);
364
			if (ret) {
365 366
				dprintk(1, "failed allocating memory for buffer %d\n",
					buffer);
367
				q->bufs[vb->index] = NULL;
368
				kfree(vb);
369 370
				break;
			}
371
			__setup_offsets(vb);
372 373 374 375 376
			/*
			 * Call the driver-provided buffer initialization
			 * callback, if given. An error in initialization
			 * results in queue setup failure.
			 */
377
			ret = call_vb_qop(vb, buf_init, vb);
378
			if (ret) {
379 380
				dprintk(1, "buffer %d %p initialization failed\n",
					buffer, vb);
381
				__vb2_buf_mem_free(vb);
382
				q->bufs[vb->index] = NULL;
383 384 385 386 387 388
				kfree(vb);
				break;
			}
		}
	}

389
	dprintk(1, "allocated %d buffers, %d plane(s) each\n",
390
			buffer, num_planes);
391 392 393 394

	return buffer;
}

395
/*
396 397
 * __vb2_free_mem() - release all video buffer memory for a given queue
 */
398
static void __vb2_free_mem(struct vb2_queue *q, unsigned int buffers)
399 400 401 402
{
	unsigned int buffer;
	struct vb2_buffer *vb;

403 404
	for (buffer = q->num_buffers - buffers; buffer < q->num_buffers;
	     ++buffer) {
405 406 407 408 409
		vb = q->bufs[buffer];
		if (!vb)
			continue;

		/* Free MMAP buffers or release USERPTR buffers */
410
		if (q->memory == VB2_MEMORY_MMAP)
411
			__vb2_buf_mem_free(vb);
412
		else if (q->memory == VB2_MEMORY_DMABUF)
413
			__vb2_buf_dmabuf_put(vb);
414 415 416 417 418
		else
			__vb2_buf_userptr_put(vb);
	}
}

419
/*
420 421 422
 * __vb2_queue_free() - free buffers at the end of the queue - video memory and
 * related information, if no buffers are left return the queue to an
 * uninitialized state. Might be called even if the queue has already been freed.
423
 */
424
static int __vb2_queue_free(struct vb2_queue *q, unsigned int buffers)
425 426 427
{
	unsigned int buffer;

428 429 430 431 432 433 434 435 436 437 438 439 440
	/*
	 * Sanity check: when preparing a buffer the queue lock is released for
	 * a short while (see __buf_prepare for the details), which would allow
	 * a race with a reqbufs which can call this function. Removing the
	 * buffers from underneath __buf_prepare is obviously a bad idea, so we
	 * check if any of the buffers is in the state PREPARING, and if so we
	 * just return -EAGAIN.
	 */
	for (buffer = q->num_buffers - buffers; buffer < q->num_buffers;
	     ++buffer) {
		if (q->bufs[buffer] == NULL)
			continue;
		if (q->bufs[buffer]->state == VB2_BUF_STATE_PREPARING) {
H
Hans Verkuil 已提交
441
			dprintk(1, "preparing buffers, cannot free\n");
442 443 444 445
			return -EAGAIN;
		}
	}

446
	/* Call driver-provided cleanup function for each buffer, if provided */
447 448
	for (buffer = q->num_buffers - buffers; buffer < q->num_buffers;
	     ++buffer) {
449 450 451
		struct vb2_buffer *vb = q->bufs[buffer];

		if (vb && vb->planes[0].mem_priv)
452
			call_void_vb_qop(vb, buf_cleanup, vb);
453 454 455
	}

	/* Release video buffer memory */
456
	__vb2_free_mem(q, buffers);
457

458 459 460 461 462 463 464 465 466 467
#ifdef CONFIG_VIDEO_ADV_DEBUG
	/*
	 * Check that all the calls were balances during the life-time of this
	 * queue. If not (or if the debug level is 1 or up), then dump the
	 * counters to the kernel log.
	 */
	if (q->num_buffers) {
		bool unbalanced = q->cnt_start_streaming != q->cnt_stop_streaming ||
				  q->cnt_wait_prepare != q->cnt_wait_finish;

468
		if (unbalanced || debug) {
469
			pr_info("counters for queue %p:%s\n", q,
470
				unbalanced ? " UNBALANCED!" : "");
471
			pr_info("     setup: %u start_streaming: %u stop_streaming: %u\n",
472 473
				q->cnt_queue_setup, q->cnt_start_streaming,
				q->cnt_stop_streaming);
474
			pr_info("     wait_prepare: %u wait_finish: %u\n",
475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493
				q->cnt_wait_prepare, q->cnt_wait_finish);
		}
		q->cnt_queue_setup = 0;
		q->cnt_wait_prepare = 0;
		q->cnt_wait_finish = 0;
		q->cnt_start_streaming = 0;
		q->cnt_stop_streaming = 0;
	}
	for (buffer = 0; buffer < q->num_buffers; ++buffer) {
		struct vb2_buffer *vb = q->bufs[buffer];
		bool unbalanced = vb->cnt_mem_alloc != vb->cnt_mem_put ||
				  vb->cnt_mem_prepare != vb->cnt_mem_finish ||
				  vb->cnt_mem_get_userptr != vb->cnt_mem_put_userptr ||
				  vb->cnt_mem_attach_dmabuf != vb->cnt_mem_detach_dmabuf ||
				  vb->cnt_mem_map_dmabuf != vb->cnt_mem_unmap_dmabuf ||
				  vb->cnt_buf_queue != vb->cnt_buf_done ||
				  vb->cnt_buf_prepare != vb->cnt_buf_finish ||
				  vb->cnt_buf_init != vb->cnt_buf_cleanup;

494
		if (unbalanced || debug) {
495
			pr_info("   counters for queue %p, buffer %d:%s\n",
496
				q, buffer, unbalanced ? " UNBALANCED!" : "");
497
			pr_info("     buf_init: %u buf_cleanup: %u buf_prepare: %u buf_finish: %u\n",
498 499
				vb->cnt_buf_init, vb->cnt_buf_cleanup,
				vb->cnt_buf_prepare, vb->cnt_buf_finish);
500
			pr_info("     buf_queue: %u buf_done: %u\n",
501
				vb->cnt_buf_queue, vb->cnt_buf_done);
502
			pr_info("     alloc: %u put: %u prepare: %u finish: %u mmap: %u\n",
503 504 505
				vb->cnt_mem_alloc, vb->cnt_mem_put,
				vb->cnt_mem_prepare, vb->cnt_mem_finish,
				vb->cnt_mem_mmap);
506
			pr_info("     get_userptr: %u put_userptr: %u\n",
507
				vb->cnt_mem_get_userptr, vb->cnt_mem_put_userptr);
508
			pr_info("     attach_dmabuf: %u detach_dmabuf: %u map_dmabuf: %u unmap_dmabuf: %u\n",
509 510
				vb->cnt_mem_attach_dmabuf, vb->cnt_mem_detach_dmabuf,
				vb->cnt_mem_map_dmabuf, vb->cnt_mem_unmap_dmabuf);
511
			pr_info("     get_dmabuf: %u num_users: %u vaddr: %u cookie: %u\n",
512 513 514 515 516 517 518 519
				vb->cnt_mem_get_dmabuf,
				vb->cnt_mem_num_users,
				vb->cnt_mem_vaddr,
				vb->cnt_mem_cookie);
		}
	}
#endif

520
	/* Free videobuf buffers */
521 522
	for (buffer = q->num_buffers - buffers; buffer < q->num_buffers;
	     ++buffer) {
523 524 525 526
		kfree(q->bufs[buffer]);
		q->bufs[buffer] = NULL;
	}

527
	q->num_buffers -= buffers;
528
	if (!q->num_buffers) {
529
		q->memory = VB2_MEMORY_UNKNOWN;
530 531
		INIT_LIST_HEAD(&q->queued_list);
	}
532
	return 0;
533 534
}

535
bool vb2_buffer_in_use(struct vb2_queue *q, struct vb2_buffer *vb)
536 537 538
{
	unsigned int plane;
	for (plane = 0; plane < vb->num_planes; ++plane) {
539
		void *mem_priv = vb->planes[plane].mem_priv;
540 541 542 543 544 545
		/*
		 * If num_users() has not been provided, call_memop
		 * will return 0, apparently nobody cares about this
		 * case anyway. If num_users() returns more than 1,
		 * we are not the only user of the plane's memory.
		 */
546
		if (mem_priv && call_memop(vb, num_users, mem_priv) > 1)
547 548 549 550
			return true;
	}
	return false;
}
551
EXPORT_SYMBOL(vb2_buffer_in_use);
552

553
/*
554 555 556 557 558 559 560
 * __buffers_in_use() - return true if any buffers on the queue are in use and
 * the queue cannot be freed (by the means of REQBUFS(0)) call
 */
static bool __buffers_in_use(struct vb2_queue *q)
{
	unsigned int buffer;
	for (buffer = 0; buffer < q->num_buffers; ++buffer) {
561
		if (vb2_buffer_in_use(q, q->bufs[buffer]))
562 563 564 565 566
			return true;
	}
	return false;
}

567
void vb2_core_querybuf(struct vb2_queue *q, unsigned int index, void *pb)
568
{
569
	call_void_bufop(q, fill_user_buffer, q->bufs[index], pb);
570
}
571
EXPORT_SYMBOL_GPL(vb2_core_querybuf);
572

573
/*
574 575 576 577 578 579 580 581 582 583 584 585
 * __verify_userptr_ops() - verify that all memory operations required for
 * USERPTR queue type have been provided
 */
static int __verify_userptr_ops(struct vb2_queue *q)
{
	if (!(q->io_modes & VB2_USERPTR) || !q->mem_ops->get_userptr ||
	    !q->mem_ops->put_userptr)
		return -EINVAL;

	return 0;
}

586
/*
587 588 589 590 591 592 593 594 595 596 597 598
 * __verify_mmap_ops() - verify that all memory operations required for
 * MMAP queue type have been provided
 */
static int __verify_mmap_ops(struct vb2_queue *q)
{
	if (!(q->io_modes & VB2_MMAP) || !q->mem_ops->alloc ||
	    !q->mem_ops->put || !q->mem_ops->mmap)
		return -EINVAL;

	return 0;
}

599
/*
600 601 602 603 604 605 606 607 608 609 610 611 612
 * __verify_dmabuf_ops() - verify that all memory operations required for
 * DMABUF queue type have been provided
 */
static int __verify_dmabuf_ops(struct vb2_queue *q)
{
	if (!(q->io_modes & VB2_DMABUF) || !q->mem_ops->attach_dmabuf ||
	    !q->mem_ops->detach_dmabuf  || !q->mem_ops->map_dmabuf ||
	    !q->mem_ops->unmap_dmabuf)
		return -EINVAL;

	return 0;
}

613
int vb2_verify_memory_type(struct vb2_queue *q,
614
		enum vb2_memory memory, unsigned int type)
615
{
616 617
	if (memory != VB2_MEMORY_MMAP && memory != VB2_MEMORY_USERPTR &&
	    memory != VB2_MEMORY_DMABUF) {
H
Hans Verkuil 已提交
618
		dprintk(1, "unsupported memory type\n");
619 620 621 622
		return -EINVAL;
	}

	if (type != q->type) {
H
Hans Verkuil 已提交
623
		dprintk(1, "requested type is incorrect\n");
624 625 626 627 628 629 630
		return -EINVAL;
	}

	/*
	 * Make sure all the required memory ops for given memory type
	 * are available.
	 */
631
	if (memory == VB2_MEMORY_MMAP && __verify_mmap_ops(q)) {
H
Hans Verkuil 已提交
632
		dprintk(1, "MMAP for current setup unsupported\n");
633 634 635
		return -EINVAL;
	}

636
	if (memory == VB2_MEMORY_USERPTR && __verify_userptr_ops(q)) {
H
Hans Verkuil 已提交
637
		dprintk(1, "USERPTR for current setup unsupported\n");
638 639 640
		return -EINVAL;
	}

641
	if (memory == VB2_MEMORY_DMABUF && __verify_dmabuf_ops(q)) {
H
Hans Verkuil 已提交
642
		dprintk(1, "DMABUF for current setup unsupported\n");
643 644 645
		return -EINVAL;
	}

646 647 648 649 650
	/*
	 * Place the busy tests at the end: -EBUSY can be ignored when
	 * create_bufs is called with count == 0, but count == 0 should still
	 * do the memory and type validation.
	 */
651
	if (vb2_fileio_is_active(q)) {
H
Hans Verkuil 已提交
652
		dprintk(1, "file io in progress\n");
653 654 655 656
		return -EBUSY;
	}
	return 0;
}
657
EXPORT_SYMBOL(vb2_verify_memory_type);
658

659
int vb2_core_reqbufs(struct vb2_queue *q, enum vb2_memory memory,
660
		unsigned int *count)
661
{
662
	unsigned int num_buffers, allocated_buffers, num_planes = 0;
663
	unsigned plane_sizes[VB2_MAX_PLANES] = { };
664
	int ret;
665 666

	if (q->streaming) {
H
Hans Verkuil 已提交
667
		dprintk(1, "streaming active\n");
668 669 670
		return -EBUSY;
	}

671 672
	if (*count == 0 || q->num_buffers != 0 ||
	    (q->memory != VB2_MEMORY_UNKNOWN && q->memory != memory)) {
673 674 675 676
		/*
		 * We already have buffers allocated, so first check if they
		 * are not in use and can be freed.
		 */
677
		mutex_lock(&q->mmap_lock);
678
		if (q->memory == VB2_MEMORY_MMAP && __buffers_in_use(q)) {
679
			mutex_unlock(&q->mmap_lock);
H
Hans Verkuil 已提交
680
			dprintk(1, "memory in use, cannot free\n");
681 682 683
			return -EBUSY;
		}

684 685 686 687 688 689
		/*
		 * Call queue_cancel to clean up any buffers in the PREPARED or
		 * QUEUED state which is possible if buffers were prepared or
		 * queued without ever calling STREAMON.
		 */
		__vb2_queue_cancel(q);
690
		ret = __vb2_queue_free(q, q->num_buffers);
691
		mutex_unlock(&q->mmap_lock);
692 693
		if (ret)
			return ret;
694 695 696 697 698

		/*
		 * In case of REQBUFS(0) return immediately without calling
		 * driver's queue_setup() callback and allocating resources.
		 */
699
		if (*count == 0)
700
			return 0;
701 702 703 704 705
	}

	/*
	 * Make sure the requested values and current defaults are sane.
	 */
706 707 708
	WARN_ON(q->min_buffers_needed > VB2_MAX_FRAME);
	num_buffers = max_t(unsigned int, *count, q->min_buffers_needed);
	num_buffers = min_t(unsigned int, num_buffers, VB2_MAX_FRAME);
709
	memset(q->alloc_devs, 0, sizeof(q->alloc_devs));
710
	q->memory = memory;
711 712 713 714 715

	/*
	 * Ask the driver how many buffers and planes per buffer it requires.
	 * Driver also sets the size and allocator context for each plane.
	 */
716
	ret = call_qop(q, queue_setup, q, &num_buffers, &num_planes,
717
		       plane_sizes, q->alloc_devs);
718
	if (ret)
719 720 721
		return ret;

	/* Finally, allocate buffers and video memory */
722
	allocated_buffers =
723
		__vb2_queue_alloc(q, memory, num_buffers, num_planes, plane_sizes);
724
	if (allocated_buffers == 0) {
725
		dprintk(1, "memory allocation failed\n");
726
		return -ENOMEM;
727 728
	}

729 730 731 732 733 734 735
	/*
	 * There is no point in continuing if we can't allocate the minimum
	 * number of buffers needed by this vb2_queue.
	 */
	if (allocated_buffers < q->min_buffers_needed)
		ret = -ENOMEM;

736 737 738
	/*
	 * Check if driver can handle the allocated number of buffers.
	 */
739
	if (!ret && allocated_buffers < num_buffers) {
740
		num_buffers = allocated_buffers;
741 742 743 744 745 746 747
		/*
		 * num_planes is set by the previous queue_setup(), but since it
		 * signals to queue_setup() whether it is called from create_bufs()
		 * vs reqbufs() we zero it here to signal that queue_setup() is
		 * called for the reqbufs() case.
		 */
		num_planes = 0;
748

749
		ret = call_qop(q, queue_setup, q, &num_buffers,
750
			       &num_planes, plane_sizes, q->alloc_devs);
751

752
		if (!ret && allocated_buffers < num_buffers)
753 754 755
			ret = -ENOMEM;

		/*
756 757
		 * Either the driver has accepted a smaller number of buffers,
		 * or .queue_setup() returned an error
758
		 */
759 760
	}

761
	mutex_lock(&q->mmap_lock);
762 763 764
	q->num_buffers = allocated_buffers;

	if (ret < 0) {
765 766 767 768
		/*
		 * Note: __vb2_queue_free() will subtract 'allocated_buffers'
		 * from q->num_buffers.
		 */
769
		__vb2_queue_free(q, allocated_buffers);
770
		mutex_unlock(&q->mmap_lock);
771
		return ret;
772
	}
773
	mutex_unlock(&q->mmap_lock);
774 775 776 777 778

	/*
	 * Return the number of successfully allocated buffers
	 * to the userspace.
	 */
779
	*count = allocated_buffers;
780
	q->waiting_for_buffers = !q->is_output;
781 782 783

	return 0;
}
784
EXPORT_SYMBOL_GPL(vb2_core_reqbufs);
785

786
int vb2_core_create_bufs(struct vb2_queue *q, enum vb2_memory memory,
787 788
		unsigned int *count, unsigned requested_planes,
		const unsigned requested_sizes[])
789 790
{
	unsigned int num_planes = 0, num_buffers, allocated_buffers;
791
	unsigned plane_sizes[VB2_MAX_PLANES] = { };
792
	int ret;
793

794
	if (q->num_buffers == VB2_MAX_FRAME) {
H
Hans Verkuil 已提交
795
		dprintk(1, "maximum number of buffers already allocated\n");
796 797 798 799
		return -ENOBUFS;
	}

	if (!q->num_buffers) {
800
		memset(q->alloc_devs, 0, sizeof(q->alloc_devs));
801
		q->memory = memory;
802
		q->waiting_for_buffers = !q->is_output;
803 804
	}

805
	num_buffers = min(*count, VB2_MAX_FRAME - q->num_buffers);
806

807 808
	if (requested_planes && requested_sizes) {
		num_planes = requested_planes;
809
		memcpy(plane_sizes, requested_sizes, sizeof(plane_sizes));
810 811
	}

812 813 814 815
	/*
	 * Ask the driver, whether the requested number of buffers, planes per
	 * buffer and their sizes are acceptable
	 */
816
	ret = call_qop(q, queue_setup, q, &num_buffers,
817
		       &num_planes, plane_sizes, q->alloc_devs);
818
	if (ret)
819 820 821
		return ret;

	/* Finally, allocate buffers and video memory */
822
	allocated_buffers = __vb2_queue_alloc(q, memory, num_buffers,
823
				num_planes, plane_sizes);
824
	if (allocated_buffers == 0) {
825
		dprintk(1, "memory allocation failed\n");
826
		return -ENOMEM;
827 828 829 830 831
	}

	/*
	 * Check if driver can handle the so far allocated number of buffers.
	 */
832 833
	if (allocated_buffers < num_buffers) {
		num_buffers = allocated_buffers;
834 835 836 837 838

		/*
		 * q->num_buffers contains the total number of buffers, that the
		 * queue driver has set up
		 */
839
		ret = call_qop(q, queue_setup, q, &num_buffers,
840
			       &num_planes, plane_sizes, q->alloc_devs);
841 842 843 844 845 846 847 848 849 850

		if (!ret && allocated_buffers < num_buffers)
			ret = -ENOMEM;

		/*
		 * Either the driver has accepted a smaller number of buffers,
		 * or .queue_setup() returned an error
		 */
	}

851
	mutex_lock(&q->mmap_lock);
852 853 854
	q->num_buffers += allocated_buffers;

	if (ret < 0) {
855 856 857 858
		/*
		 * Note: __vb2_queue_free() will subtract 'allocated_buffers'
		 * from q->num_buffers.
		 */
859
		__vb2_queue_free(q, allocated_buffers);
860
		mutex_unlock(&q->mmap_lock);
861
		return -ENOMEM;
862
	}
863
	mutex_unlock(&q->mmap_lock);
864 865 866 867 868

	/*
	 * Return the number of successfully allocated buffers
	 * to the userspace.
	 */
869
	*count = allocated_buffers;
870 871 872

	return 0;
}
873
EXPORT_SYMBOL_GPL(vb2_core_create_bufs);
874

875 876
void *vb2_plane_vaddr(struct vb2_buffer *vb, unsigned int plane_no)
{
877
	if (plane_no >= vb->num_planes || !vb->planes[plane_no].mem_priv)
878 879
		return NULL;

880
	return call_ptr_memop(vb, vaddr, vb->planes[plane_no].mem_priv);
881 882 883 884 885 886

}
EXPORT_SYMBOL_GPL(vb2_plane_vaddr);

void *vb2_plane_cookie(struct vb2_buffer *vb, unsigned int plane_no)
{
887
	if (plane_no >= vb->num_planes || !vb->planes[plane_no].mem_priv)
888 889
		return NULL;

890
	return call_ptr_memop(vb, cookie, vb->planes[plane_no].mem_priv);
891 892 893 894 895 896 897
}
EXPORT_SYMBOL_GPL(vb2_plane_cookie);

void vb2_buffer_done(struct vb2_buffer *vb, enum vb2_buffer_state state)
{
	struct vb2_queue *q = vb->vb2_queue;
	unsigned long flags;
898
	unsigned int plane;
899

900
	if (WARN_ON(vb->state != VB2_BUF_STATE_ACTIVE))
901 902
		return;

903 904
	if (WARN_ON(state != VB2_BUF_STATE_DONE &&
		    state != VB2_BUF_STATE_ERROR &&
905 906
		    state != VB2_BUF_STATE_QUEUED &&
		    state != VB2_BUF_STATE_REQUEUEING))
907
		state = VB2_BUF_STATE_ERROR;
908

909 910 911 912 913 914 915
#ifdef CONFIG_VIDEO_ADV_DEBUG
	/*
	 * Although this is not a callback, it still does have to balance
	 * with the buf_queue op. So update this counter manually.
	 */
	vb->cnt_buf_done++;
#endif
916
	dprintk(4, "done processing on buffer %d, state: %d\n",
917
			vb->index, state);
918

919 920 921 922 923 924
	if (state != VB2_BUF_STATE_QUEUED &&
	    state != VB2_BUF_STATE_REQUEUEING) {
		/* sync buffers */
		for (plane = 0; plane < vb->num_planes; ++plane)
			call_void_memop(vb, finish, vb->planes[plane].mem_priv);
	}
925

926
	spin_lock_irqsave(&q->done_lock, flags);
927 928 929 930 931
	if (state == VB2_BUF_STATE_QUEUED ||
	    state == VB2_BUF_STATE_REQUEUEING) {
		vb->state = VB2_BUF_STATE_QUEUED;
	} else {
		/* Add the buffer to the done buffers list */
932
		list_add_tail(&vb->done_entry, &q->done_list);
933 934
		vb->state = state;
	}
935
	atomic_dec(&q->owned_by_drv_count);
936 937
	spin_unlock_irqrestore(&q->done_lock, flags);

938 939
	trace_vb2_buf_done(q, vb);

940 941 942 943
	switch (state) {
	case VB2_BUF_STATE_QUEUED:
		return;
	case VB2_BUF_STATE_REQUEUEING:
944 945
		if (q->start_streaming_called)
			__enqueue_in_driver(vb);
946
		return;
947 948 949 950
	default:
		/* Inform any processes that may be waiting for buffers */
		wake_up(&q->done_wq);
		break;
951
	}
952 953 954
}
EXPORT_SYMBOL_GPL(vb2_buffer_done);

955 956 957 958 959 960 961 962 963 964 965 966
void vb2_discard_done(struct vb2_queue *q)
{
	struct vb2_buffer *vb;
	unsigned long flags;

	spin_lock_irqsave(&q->done_lock, flags);
	list_for_each_entry(vb, &q->done_list, done_entry)
		vb->state = VB2_BUF_STATE_ERROR;
	spin_unlock_irqrestore(&q->done_lock, flags);
}
EXPORT_SYMBOL_GPL(vb2_discard_done);

967
/*
968
 * __prepare_mmap() - prepare an MMAP buffer
969
 */
970
static int __prepare_mmap(struct vb2_buffer *vb, const void *pb)
971
{
972 973 974 975 976
	int ret = 0;

	if (pb)
		ret = call_bufop(vb->vb2_queue, fill_vb2_buffer,
				 vb, pb, vb->planes);
977
	return ret ? ret : call_vb_qop(vb, buf_prepare, vb);
978 979
}

980
/*
981
 * __prepare_userptr() - prepare a USERPTR buffer
982
 */
983
static int __prepare_userptr(struct vb2_buffer *vb, const void *pb)
984
{
985
	struct vb2_plane planes[VB2_MAX_PLANES];
986 987 988
	struct vb2_queue *q = vb->vb2_queue;
	void *mem_priv;
	unsigned int plane;
989
	int ret = 0;
990
	bool reacquired = vb->planes[0].mem_priv == NULL;
991

992
	memset(planes, 0, sizeof(planes[0]) * vb->num_planes);
993
	/* Copy relevant information provided by the userspace */
994
	if (pb) {
995 996
		ret = call_bufop(vb->vb2_queue, fill_vb2_buffer,
				 vb, pb, planes);
997 998 999
		if (ret)
			return ret;
	}
1000 1001 1002

	for (plane = 0; plane < vb->num_planes; ++plane) {
		/* Skip the plane if already verified */
1003 1004 1005
		if (vb->planes[plane].m.userptr &&
			vb->planes[plane].m.userptr == planes[plane].m.userptr
			&& vb->planes[plane].length == planes[plane].length)
1006 1007
			continue;

1008 1009
		dprintk(3, "userspace address for plane %d changed, reacquiring memory\n",
			plane);
1010

1011
		/* Check if the provided plane buffer is large enough */
1012
		if (planes[plane].length < vb->planes[plane].min_length) {
1013
			dprintk(1, "provided buffer size %u is less than setup size %u for plane %d\n",
1014
						planes[plane].length,
1015 1016
						vb->planes[plane].min_length,
						plane);
1017
			ret = -EINVAL;
1018 1019 1020
			goto err;
		}

1021
		/* Release previously acquired memory if present */
1022 1023 1024
		if (vb->planes[plane].mem_priv) {
			if (!reacquired) {
				reacquired = true;
1025
				call_void_vb_qop(vb, buf_cleanup, vb);
1026
			}
1027
			call_void_memop(vb, put_userptr, vb->planes[plane].mem_priv);
1028
		}
1029 1030

		vb->planes[plane].mem_priv = NULL;
1031 1032 1033 1034
		vb->planes[plane].bytesused = 0;
		vb->planes[plane].length = 0;
		vb->planes[plane].m.userptr = 0;
		vb->planes[plane].data_offset = 0;
1035 1036

		/* Acquire each plane's memory */
1037
		mem_priv = call_ptr_memop(vb, get_userptr,
1038
				q->alloc_devs[plane] ? : q->dev,
1039
				planes[plane].m.userptr,
1040
				planes[plane].length, q->dma_dir);
1041
		if (IS_ERR(mem_priv)) {
1042 1043
			dprintk(1, "failed acquiring userspace memory for plane %d\n",
				plane);
1044
			ret = PTR_ERR(mem_priv);
1045
			goto err;
1046
		}
1047
		vb->planes[plane].mem_priv = mem_priv;
1048 1049 1050 1051 1052 1053
	}

	/*
	 * Now that everything is in order, copy relevant information
	 * provided by userspace.
	 */
1054 1055 1056 1057 1058 1059
	for (plane = 0; plane < vb->num_planes; ++plane) {
		vb->planes[plane].bytesused = planes[plane].bytesused;
		vb->planes[plane].length = planes[plane].length;
		vb->planes[plane].m.userptr = planes[plane].m.userptr;
		vb->planes[plane].data_offset = planes[plane].data_offset;
	}
1060

1061 1062 1063 1064 1065 1066 1067 1068
	if (reacquired) {
		/*
		 * One or more planes changed, so we must call buf_init to do
		 * the driver-specific initialization on the newly acquired
		 * buffer, if provided.
		 */
		ret = call_vb_qop(vb, buf_init, vb);
		if (ret) {
H
Hans Verkuil 已提交
1069
			dprintk(1, "buffer initialization failed\n");
1070 1071 1072 1073 1074 1075
			goto err;
		}
	}

	ret = call_vb_qop(vb, buf_prepare, vb);
	if (ret) {
H
Hans Verkuil 已提交
1076
		dprintk(1, "buffer preparation failed\n");
1077
		call_void_vb_qop(vb, buf_cleanup, vb);
1078 1079 1080
		goto err;
	}

1081 1082 1083
	return 0;
err:
	/* In case of errors, release planes that were already acquired */
1084 1085
	for (plane = 0; plane < vb->num_planes; ++plane) {
		if (vb->planes[plane].mem_priv)
1086 1087
			call_void_memop(vb, put_userptr,
				vb->planes[plane].mem_priv);
1088
		vb->planes[plane].mem_priv = NULL;
1089 1090
		vb->planes[plane].m.userptr = 0;
		vb->planes[plane].length = 0;
1091 1092 1093 1094 1095
	}

	return ret;
}

1096
/*
1097
 * __prepare_dmabuf() - prepare a DMABUF buffer
1098
 */
1099
static int __prepare_dmabuf(struct vb2_buffer *vb, const void *pb)
1100
{
1101
	struct vb2_plane planes[VB2_MAX_PLANES];
1102 1103 1104
	struct vb2_queue *q = vb->vb2_queue;
	void *mem_priv;
	unsigned int plane;
1105
	int ret = 0;
1106
	bool reacquired = vb->planes[0].mem_priv == NULL;
1107

1108
	memset(planes, 0, sizeof(planes[0]) * vb->num_planes);
1109
	/* Copy relevant information provided by the userspace */
1110
	if (pb) {
1111 1112
		ret = call_bufop(vb->vb2_queue, fill_vb2_buffer,
				 vb, pb, planes);
1113 1114 1115
		if (ret)
			return ret;
	}
1116 1117 1118 1119 1120

	for (plane = 0; plane < vb->num_planes; ++plane) {
		struct dma_buf *dbuf = dma_buf_get(planes[plane].m.fd);

		if (IS_ERR_OR_NULL(dbuf)) {
H
Hans Verkuil 已提交
1121
			dprintk(1, "invalid dmabuf fd for plane %d\n",
1122 1123 1124 1125 1126 1127 1128 1129 1130
				plane);
			ret = -EINVAL;
			goto err;
		}

		/* use DMABUF size if length is not provided */
		if (planes[plane].length == 0)
			planes[plane].length = dbuf->size;

1131
		if (planes[plane].length < vb->planes[plane].min_length) {
1132
			dprintk(1, "invalid dmabuf length %u for plane %d, minimum length %u\n",
1133 1134
				planes[plane].length, plane,
				vb->planes[plane].min_length);
1135
			dma_buf_put(dbuf);
1136 1137 1138 1139 1140 1141
			ret = -EINVAL;
			goto err;
		}

		/* Skip the plane if already verified */
		if (dbuf == vb->planes[plane].dbuf &&
1142
			vb->planes[plane].length == planes[plane].length) {
1143 1144 1145 1146
			dma_buf_put(dbuf);
			continue;
		}

1147
		dprintk(3, "buffer for plane %d changed\n", plane);
1148

1149 1150
		if (!reacquired) {
			reacquired = true;
1151
			call_void_vb_qop(vb, buf_cleanup, vb);
1152 1153
		}

1154
		/* Release previously acquired memory if present */
1155
		__vb2_plane_dmabuf_put(vb, &vb->planes[plane]);
1156 1157 1158 1159
		vb->planes[plane].bytesused = 0;
		vb->planes[plane].length = 0;
		vb->planes[plane].m.fd = 0;
		vb->planes[plane].data_offset = 0;
1160 1161

		/* Acquire each plane's memory */
1162
		mem_priv = call_ptr_memop(vb, attach_dmabuf,
1163
				q->alloc_devs[plane] ? : q->dev,
1164
				dbuf, planes[plane].length, q->dma_dir);
1165
		if (IS_ERR(mem_priv)) {
H
Hans Verkuil 已提交
1166
			dprintk(1, "failed to attach dmabuf\n");
1167 1168 1169 1170 1171 1172 1173 1174 1175
			ret = PTR_ERR(mem_priv);
			dma_buf_put(dbuf);
			goto err;
		}

		vb->planes[plane].dbuf = dbuf;
		vb->planes[plane].mem_priv = mem_priv;
	}

1176 1177 1178 1179
	/*
	 * This pins the buffer(s) with dma_buf_map_attachment()). It's done
	 * here instead just before the DMA, while queueing the buffer(s) so
	 * userspace knows sooner rather than later if the dma-buf map fails.
1180 1181
	 */
	for (plane = 0; plane < vb->num_planes; ++plane) {
1182
		ret = call_memop(vb, map_dmabuf, vb->planes[plane].mem_priv);
1183
		if (ret) {
H
Hans Verkuil 已提交
1184
			dprintk(1, "failed to map dmabuf for plane %d\n",
1185 1186 1187 1188 1189 1190 1191 1192 1193 1194
				plane);
			goto err;
		}
		vb->planes[plane].dbuf_mapped = 1;
	}

	/*
	 * Now that everything is in order, copy relevant information
	 * provided by userspace.
	 */
1195 1196 1197 1198 1199 1200
	for (plane = 0; plane < vb->num_planes; ++plane) {
		vb->planes[plane].bytesused = planes[plane].bytesused;
		vb->planes[plane].length = planes[plane].length;
		vb->planes[plane].m.fd = planes[plane].m.fd;
		vb->planes[plane].data_offset = planes[plane].data_offset;
	}
1201

1202 1203 1204 1205 1206 1207 1208
	if (reacquired) {
		/*
		 * Call driver-specific initialization on the newly acquired buffer,
		 * if provided.
		 */
		ret = call_vb_qop(vb, buf_init, vb);
		if (ret) {
H
Hans Verkuil 已提交
1209
			dprintk(1, "buffer initialization failed\n");
1210 1211 1212 1213 1214 1215
			goto err;
		}
	}

	ret = call_vb_qop(vb, buf_prepare, vb);
	if (ret) {
H
Hans Verkuil 已提交
1216
		dprintk(1, "buffer preparation failed\n");
1217
		call_void_vb_qop(vb, buf_cleanup, vb);
1218 1219 1220
		goto err;
	}

1221 1222 1223 1224 1225 1226 1227 1228
	return 0;
err:
	/* In case of errors, release planes that were already acquired */
	__vb2_buf_dmabuf_put(vb);

	return ret;
}

1229
/*
1230 1231 1232 1233 1234 1235 1236
 * __enqueue_in_driver() - enqueue a vb2_buffer in driver for processing
 */
static void __enqueue_in_driver(struct vb2_buffer *vb)
{
	struct vb2_queue *q = vb->vb2_queue;

	vb->state = VB2_BUF_STATE_ACTIVE;
1237
	atomic_inc(&q->owned_by_drv_count);
1238

1239 1240
	trace_vb2_buf_queue(q, vb);

1241
	call_void_vb_qop(vb, buf_queue, vb);
1242 1243
}

1244
static int __buf_prepare(struct vb2_buffer *vb, const void *pb)
1245 1246
{
	struct vb2_queue *q = vb->vb2_queue;
1247
	unsigned int plane;
1248 1249
	int ret;

1250 1251 1252 1253 1254
	if (q->error) {
		dprintk(1, "fatal error occurred on queue\n");
		return -EIO;
	}

1255
	vb->state = VB2_BUF_STATE_PREPARING;
1256

1257
	switch (q->memory) {
1258
	case VB2_MEMORY_MMAP:
1259
		ret = __prepare_mmap(vb, pb);
1260
		break;
1261
	case VB2_MEMORY_USERPTR:
1262
		ret = __prepare_userptr(vb, pb);
1263
		break;
1264
	case VB2_MEMORY_DMABUF:
1265
		ret = __prepare_dmabuf(vb, pb);
1266
		break;
1267 1268 1269 1270 1271
	default:
		WARN(1, "Invalid queue type\n");
		ret = -EINVAL;
	}

1272
	if (ret) {
H
Hans Verkuil 已提交
1273
		dprintk(1, "buffer preparation failed: %d\n", ret);
1274 1275 1276
		vb->state = VB2_BUF_STATE_DEQUEUED;
		return ret;
	}
1277

1278 1279 1280 1281 1282 1283 1284
	/* sync buffers */
	for (plane = 0; plane < vb->num_planes; ++plane)
		call_void_memop(vb, prepare, vb->planes[plane].mem_priv);

	vb->state = VB2_BUF_STATE_PREPARED;

	return 0;
1285 1286
}

1287
int vb2_core_prepare_buf(struct vb2_queue *q, unsigned int index, void *pb)
1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303
{
	struct vb2_buffer *vb;
	int ret;

	vb = q->bufs[index];
	if (vb->state != VB2_BUF_STATE_DEQUEUED) {
		dprintk(1, "invalid buffer state %d\n",
			vb->state);
		return -EINVAL;
	}

	ret = __buf_prepare(vb, pb);
	if (ret)
		return ret;

	/* Fill buffer information for the userspace */
1304
	call_void_bufop(q, fill_user_buffer, vb, pb);
1305

1306
	dprintk(2, "prepare of buffer %d succeeded\n", vb->index);
1307 1308 1309

	return ret;
}
1310
EXPORT_SYMBOL_GPL(vb2_core_prepare_buf);
1311

1312
/*
1313 1314 1315
 * vb2_start_streaming() - Attempt to start streaming.
 * @q:		videobuf2 queue
 *
1316 1317 1318 1319 1320 1321
 * Attempt to start streaming. When this function is called there must be
 * at least q->min_buffers_needed buffers queued up (i.e. the minimum
 * number of buffers required for the DMA engine to function). If the
 * @start_streaming op fails it is supposed to return all the driver-owned
 * buffers back to vb2 in state QUEUED. Check if that happened and if
 * not warn and reclaim them forcefully.
1322 1323 1324
 */
static int vb2_start_streaming(struct vb2_queue *q)
{
1325
	struct vb2_buffer *vb;
1326 1327 1328
	int ret;

	/*
1329 1330
	 * If any buffers were queued before streamon,
	 * we can now pass them to driver for processing.
1331
	 */
1332 1333 1334 1335
	list_for_each_entry(vb, &q->queued_list, queued_entry)
		__enqueue_in_driver(vb);

	/* Tell the driver to start streaming */
1336
	q->start_streaming_called = 1;
1337 1338 1339
	ret = call_qop(q, start_streaming, q,
		       atomic_read(&q->owned_by_drv_count));
	if (!ret)
1340
		return 0;
1341

1342 1343
	q->start_streaming_called = 0;

H
Hans Verkuil 已提交
1344
	dprintk(1, "driver refused to start streaming\n");
1345 1346 1347
	/*
	 * If you see this warning, then the driver isn't cleaning up properly
	 * after a failed start_streaming(). See the start_streaming()
1348
	 * documentation in videobuf2-core.h for more information how buffers
1349 1350
	 * should be returned to vb2 in start_streaming().
	 */
1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364
	if (WARN_ON(atomic_read(&q->owned_by_drv_count))) {
		unsigned i;

		/*
		 * Forcefully reclaim buffers if the driver did not
		 * correctly return them to vb2.
		 */
		for (i = 0; i < q->num_buffers; ++i) {
			vb = q->bufs[i];
			if (vb->state == VB2_BUF_STATE_ACTIVE)
				vb2_buffer_done(vb, VB2_BUF_STATE_QUEUED);
		}
		/* Must be zero now */
		WARN_ON(atomic_read(&q->owned_by_drv_count));
1365
	}
1366 1367 1368 1369 1370 1371
	/*
	 * If done_list is not empty, then start_streaming() didn't call
	 * vb2_buffer_done(vb, VB2_BUF_STATE_QUEUED) but STATE_ERROR or
	 * STATE_DONE.
	 */
	WARN_ON(!list_empty(&q->done_list));
1372 1373 1374
	return ret;
}

1375
int vb2_core_qbuf(struct vb2_queue *q, unsigned int index, void *pb)
1376
{
1377
	struct vb2_buffer *vb;
1378
	int ret;
1379

1380 1381 1382 1383 1384
	if (q->error) {
		dprintk(1, "fatal error occurred on queue\n");
		return -EIO;
	}

1385
	vb = q->bufs[index];
1386

1387 1388
	switch (vb->state) {
	case VB2_BUF_STATE_DEQUEUED:
1389
		ret = __buf_prepare(vb, pb);
1390
		if (ret)
1391
			return ret;
1392
		break;
1393 1394
	case VB2_BUF_STATE_PREPARED:
		break;
1395
	case VB2_BUF_STATE_PREPARING:
H
Hans Verkuil 已提交
1396
		dprintk(1, "buffer still being prepared\n");
1397
		return -EINVAL;
1398
	default:
H
Hans Verkuil 已提交
1399
		dprintk(1, "invalid buffer state %d\n", vb->state);
1400
		return -EINVAL;
1401 1402 1403 1404 1405 1406 1407
	}

	/*
	 * Add to the queued buffers list, a buffer will stay on it until
	 * dequeued in dqbuf.
	 */
	list_add_tail(&vb->queued_entry, &q->queued_list);
1408
	q->queued_count++;
1409
	q->waiting_for_buffers = false;
1410
	vb->state = VB2_BUF_STATE_QUEUED;
1411

1412 1413
	if (pb)
		call_void_bufop(q, copy_timestamp, vb, pb);
1414

1415 1416
	trace_vb2_qbuf(q, vb);

1417 1418 1419 1420
	/*
	 * If already streaming, give the buffer to driver for processing.
	 * If not, the buffer will be given to driver on next streamon.
	 */
1421
	if (q->start_streaming_called)
1422 1423
		__enqueue_in_driver(vb);

1424
	/* Fill buffer information for the userspace */
1425 1426
	if (pb)
		call_void_bufop(q, fill_user_buffer, vb, pb);
1427

1428 1429 1430 1431 1432 1433 1434 1435
	/*
	 * If streamon has been called, and we haven't yet called
	 * start_streaming() since not enough buffers were queued, and
	 * we now have reached the minimum number of queued buffers,
	 * then we can finally call start_streaming().
	 */
	if (q->streaming && !q->start_streaming_called &&
	    q->queued_count >= q->min_buffers_needed) {
1436 1437 1438 1439 1440
		ret = vb2_start_streaming(q);
		if (ret)
			return ret;
	}

1441
	dprintk(2, "qbuf of buffer %d succeeded\n", vb->index);
1442
	return 0;
1443
}
1444
EXPORT_SYMBOL_GPL(vb2_core_qbuf);
1445

1446
/*
1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466
 * __vb2_wait_for_done_vb() - wait for a buffer to become available
 * for dequeuing
 *
 * Will sleep if required for nonblocking == false.
 */
static int __vb2_wait_for_done_vb(struct vb2_queue *q, int nonblocking)
{
	/*
	 * All operations on vb_done_list are performed under done_lock
	 * spinlock protection. However, buffers may be removed from
	 * it and returned to userspace only while holding both driver's
	 * lock and the done_lock spinlock. Thus we can be sure that as
	 * long as we hold the driver's lock, the list will remain not
	 * empty if list_empty() check succeeds.
	 */

	for (;;) {
		int ret;

		if (!q->streaming) {
1467
			dprintk(1, "streaming off, will not wait for buffers\n");
1468 1469 1470
			return -EINVAL;
		}

1471 1472 1473 1474 1475
		if (q->error) {
			dprintk(1, "Queue in error state, will not wait for buffers\n");
			return -EIO;
		}

1476 1477 1478 1479 1480
		if (q->last_buffer_dequeued) {
			dprintk(3, "last buffer dequeued already, will not wait for buffers\n");
			return -EPIPE;
		}

1481 1482 1483 1484 1485 1486 1487 1488
		if (!list_empty(&q->done_list)) {
			/*
			 * Found a buffer that we were waiting for.
			 */
			break;
		}

		if (nonblocking) {
1489
			dprintk(3, "nonblocking and no buffers to dequeue, will not wait\n");
1490 1491 1492 1493 1494 1495 1496 1497
			return -EAGAIN;
		}

		/*
		 * We are streaming and blocking, wait for another buffer to
		 * become ready or for streamoff. Driver's lock is released to
		 * allow streamoff or qbuf to be called while waiting.
		 */
1498
		call_void_qop(q, wait_prepare, q);
1499 1500 1501 1502

		/*
		 * All locks have been released, it is safe to sleep now.
		 */
1503
		dprintk(3, "will sleep waiting for buffers\n");
1504
		ret = wait_event_interruptible(q->done_wq,
1505 1506
				!list_empty(&q->done_list) || !q->streaming ||
				q->error);
1507 1508 1509 1510 1511

		/*
		 * We need to reevaluate both conditions again after reacquiring
		 * the locks or return an error if one occurred.
		 */
1512
		call_void_qop(q, wait_finish, q);
1513
		if (ret) {
1514
			dprintk(1, "sleep was interrupted\n");
1515
			return ret;
1516
		}
1517 1518 1519 1520
	}
	return 0;
}

1521
/*
1522 1523 1524 1525 1526
 * __vb2_get_done_vb() - get a buffer ready for dequeuing
 *
 * Will sleep if required for nonblocking == false.
 */
static int __vb2_get_done_vb(struct vb2_queue *q, struct vb2_buffer **vb,
1527
			     void *pb, int nonblocking)
1528 1529
{
	unsigned long flags;
1530
	int ret = 0;
1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544

	/*
	 * Wait for at least one buffer to become available on the done_list.
	 */
	ret = __vb2_wait_for_done_vb(q, nonblocking);
	if (ret)
		return ret;

	/*
	 * Driver's lock has been held since we last verified that done_list
	 * is not empty, so no need for another list_empty(done_list) check.
	 */
	spin_lock_irqsave(&q->done_lock, flags);
	*vb = list_first_entry(&q->done_list, struct vb2_buffer, done_entry);
1545
	/*
1546 1547 1548
	 * Only remove the buffer from done_list if all planes can be
	 * handled. Some cases such as V4L2 file I/O and DVB have pb
	 * == NULL; skip the check then as there's nothing to verify.
1549
	 */
1550 1551
	if (pb)
		ret = call_bufop(q, verify_planes_array, *vb, pb);
1552 1553
	if (!ret)
		list_del(&(*vb)->done_entry);
1554 1555
	spin_unlock_irqrestore(&q->done_lock, flags);

1556
	return ret;
1557 1558 1559 1560 1561
}

int vb2_wait_for_all_buffers(struct vb2_queue *q)
{
	if (!q->streaming) {
1562
		dprintk(1, "streaming off, will not wait for buffers\n");
1563 1564 1565
		return -EINVAL;
	}

1566
	if (q->start_streaming_called)
1567
		wait_event(q->done_wq, !atomic_read(&q->owned_by_drv_count));
1568 1569 1570 1571
	return 0;
}
EXPORT_SYMBOL_GPL(vb2_wait_for_all_buffers);

1572
/*
1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586
 * __vb2_dqbuf() - bring back the buffer to the DEQUEUED state
 */
static void __vb2_dqbuf(struct vb2_buffer *vb)
{
	struct vb2_queue *q = vb->vb2_queue;
	unsigned int i;

	/* nothing to do if the buffer is already dequeued */
	if (vb->state == VB2_BUF_STATE_DEQUEUED)
		return;

	vb->state = VB2_BUF_STATE_DEQUEUED;

	/* unmap DMABUF buffer */
1587
	if (q->memory == VB2_MEMORY_DMABUF)
1588 1589 1590
		for (i = 0; i < vb->num_planes; ++i) {
			if (!vb->planes[i].dbuf_mapped)
				continue;
1591
			call_void_memop(vb, unmap_dmabuf, vb->planes[i].mem_priv);
1592 1593 1594 1595
			vb->planes[i].dbuf_mapped = 0;
		}
}

1596 1597
int vb2_core_dqbuf(struct vb2_queue *q, unsigned int *pindex, void *pb,
		   bool nonblocking)
1598 1599 1600 1601
{
	struct vb2_buffer *vb = NULL;
	int ret;

1602
	ret = __vb2_get_done_vb(q, &vb, pb, nonblocking);
1603
	if (ret < 0)
1604 1605 1606 1607
		return ret;

	switch (vb->state) {
	case VB2_BUF_STATE_DONE:
1608
		dprintk(3, "returning done buffer\n");
1609 1610
		break;
	case VB2_BUF_STATE_ERROR:
1611
		dprintk(3, "returning done buffer with errors\n");
1612 1613
		break;
	default:
1614
		dprintk(1, "invalid buffer state\n");
1615 1616 1617
		return -EINVAL;
	}

1618
	call_void_vb_qop(vb, buf_finish, vb);
1619

1620 1621 1622
	if (pindex)
		*pindex = vb->index;

1623
	/* Fill buffer information for the userspace */
1624 1625
	if (pb)
		call_void_bufop(q, fill_user_buffer, vb, pb);
1626

1627 1628
	/* Remove from videobuf queue */
	list_del(&vb->queued_entry);
1629
	q->queued_count--;
1630 1631 1632

	trace_vb2_dqbuf(q, vb);

1633 1634
	/* go back to dequeued state */
	__vb2_dqbuf(vb);
1635

1636
	dprintk(2, "dqbuf of buffer %d, with state %d\n",
1637
			vb->index, vb->state);
1638 1639

	return 0;
1640 1641

}
1642
EXPORT_SYMBOL_GPL(vb2_core_dqbuf);
1643

1644
/*
1645 1646 1647 1648 1649 1650
 * __vb2_queue_cancel() - cancel and stop (pause) streaming
 *
 * Removes all queued buffers from driver's queue and all buffers queued by
 * userspace from videobuf's queue. Returns to state after reqbufs.
 */
static void __vb2_queue_cancel(struct vb2_queue *q)
1651
{
1652
	unsigned int i;
1653 1654 1655 1656 1657

	/*
	 * Tell driver to stop all transactions and release all queued
	 * buffers.
	 */
1658
	if (q->start_streaming_called)
1659
		call_void_qop(q, stop_streaming, q);
1660

1661 1662 1663
	/*
	 * If you see this warning, then the driver isn't cleaning up properly
	 * in stop_streaming(). See the stop_streaming() documentation in
1664
	 * videobuf2-core.h for more information how buffers should be returned
1665 1666
	 * to vb2 in stop_streaming().
	 */
1667 1668
	if (WARN_ON(atomic_read(&q->owned_by_drv_count))) {
		for (i = 0; i < q->num_buffers; ++i)
1669 1670 1671
			if (q->bufs[i]->state == VB2_BUF_STATE_ACTIVE) {
				pr_warn("driver bug: stop_streaming operation is leaving buf %p in active state\n",
					q->bufs[i]);
1672
				vb2_buffer_done(q->bufs[i], VB2_BUF_STATE_ERROR);
1673
			}
1674 1675 1676
		/* Must be zero now */
		WARN_ON(atomic_read(&q->owned_by_drv_count));
	}
1677

1678 1679 1680
	q->streaming = 0;
	q->start_streaming_called = 0;
	q->queued_count = 0;
1681
	q->error = 0;
1682

1683 1684 1685 1686 1687 1688 1689 1690 1691
	/*
	 * Remove all buffers from videobuf's list...
	 */
	INIT_LIST_HEAD(&q->queued_list);
	/*
	 * ...and done list; userspace will not receive any buffers it
	 * has not already dequeued before initiating cancel.
	 */
	INIT_LIST_HEAD(&q->done_list);
1692
	atomic_set(&q->owned_by_drv_count, 0);
1693 1694 1695 1696
	wake_up_all(&q->done_wq);

	/*
	 * Reinitialize all buffers for next use.
1697 1698 1699
	 * Make sure to call buf_finish for any queued buffers. Normally
	 * that's done in dqbuf, but that's not going to happen when we
	 * cancel the whole queue. Note: this code belongs here, not in
R
Ricardo Ribalda 已提交
1700
	 * __vb2_dqbuf() since in vb2_core_dqbuf() there is a critical
1701
	 * call to __fill_user_buffer() after buf_finish(). That order can't
1702
	 * be changed, so we can't move the buf_finish() to __vb2_dqbuf().
1703
	 */
1704 1705 1706
	for (i = 0; i < q->num_buffers; ++i) {
		struct vb2_buffer *vb = q->bufs[i];

1707 1708 1709 1710 1711 1712 1713 1714 1715
		if (vb->state == VB2_BUF_STATE_PREPARED ||
		    vb->state == VB2_BUF_STATE_QUEUED) {
			unsigned int plane;

			for (plane = 0; plane < vb->num_planes; ++plane)
				call_void_memop(vb, finish,
						vb->planes[plane].mem_priv);
		}

1716 1717
		if (vb->state != VB2_BUF_STATE_DEQUEUED) {
			vb->state = VB2_BUF_STATE_PREPARED;
1718
			call_void_vb_qop(vb, buf_finish, vb);
1719 1720 1721
		}
		__vb2_dqbuf(vb);
	}
1722 1723
}

1724
int vb2_core_streamon(struct vb2_queue *q, unsigned int type)
1725
{
1726
	int ret;
1727 1728

	if (type != q->type) {
H
Hans Verkuil 已提交
1729
		dprintk(1, "invalid stream type\n");
1730 1731 1732 1733
		return -EINVAL;
	}

	if (q->streaming) {
H
Hans Verkuil 已提交
1734
		dprintk(3, "already streaming\n");
1735
		return 0;
1736 1737
	}

1738
	if (!q->num_buffers) {
H
Hans Verkuil 已提交
1739
		dprintk(1, "no buffers have been allocated\n");
1740 1741 1742
		return -EINVAL;
	}

1743
	if (q->num_buffers < q->min_buffers_needed) {
H
Hans Verkuil 已提交
1744
		dprintk(1, "need at least %u allocated buffers\n",
1745 1746 1747
				q->min_buffers_needed);
		return -EINVAL;
	}
1748

1749
	/*
1750 1751
	 * Tell driver to start streaming provided sufficient buffers
	 * are available.
1752
	 */
1753
	if (q->queued_count >= q->min_buffers_needed) {
1754 1755 1756
		ret = v4l_vb2q_enable_media_source(q);
		if (ret)
			return ret;
1757 1758 1759 1760 1761
		ret = vb2_start_streaming(q);
		if (ret) {
			__vb2_queue_cancel(q);
			return ret;
		}
1762 1763 1764
	}

	q->streaming = 1;
1765

H
Hans Verkuil 已提交
1766
	dprintk(3, "successful\n");
1767 1768
	return 0;
}
1769
EXPORT_SYMBOL_GPL(vb2_core_streamon);
1770

1771 1772 1773 1774 1775 1776 1777 1778
void vb2_queue_error(struct vb2_queue *q)
{
	q->error = 1;

	wake_up_all(&q->done_wq);
}
EXPORT_SYMBOL_GPL(vb2_queue_error);

1779
int vb2_core_streamoff(struct vb2_queue *q, unsigned int type)
1780
{
1781
	if (type != q->type) {
H
Hans Verkuil 已提交
1782
		dprintk(1, "invalid stream type\n");
1783 1784 1785 1786 1787 1788
		return -EINVAL;
	}

	/*
	 * Cancel will pause streaming and remove all buffers from the driver
	 * and videobuf, effectively returning control over them to userspace.
1789 1790 1791 1792 1793
	 *
	 * Note that we do this even if q->streaming == 0: if you prepare or
	 * queue buffers, and then call streamoff without ever having called
	 * streamon, you would still expect those buffers to be returned to
	 * their normal dequeued state.
1794 1795
	 */
	__vb2_queue_cancel(q);
1796
	q->waiting_for_buffers = !q->is_output;
1797
	q->last_buffer_dequeued = false;
1798

H
Hans Verkuil 已提交
1799
	dprintk(3, "successful\n");
1800 1801
	return 0;
}
1802
EXPORT_SYMBOL_GPL(vb2_core_streamoff);
1803

1804
/*
1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821
 * __find_plane_by_offset() - find plane associated with the given offset off
 */
static int __find_plane_by_offset(struct vb2_queue *q, unsigned long off,
			unsigned int *_buffer, unsigned int *_plane)
{
	struct vb2_buffer *vb;
	unsigned int buffer, plane;

	/*
	 * Go over all buffers and their planes, comparing the given offset
	 * with an offset assigned to each plane. If a match is found,
	 * return its buffer and plane numbers.
	 */
	for (buffer = 0; buffer < q->num_buffers; ++buffer) {
		vb = q->bufs[buffer];

		for (plane = 0; plane < vb->num_planes; ++plane) {
1822
			if (vb->planes[plane].m.offset == off) {
1823 1824 1825 1826 1827 1828 1829 1830 1831 1832
				*_buffer = buffer;
				*_plane = plane;
				return 0;
			}
		}
	}

	return -EINVAL;
}

1833
int vb2_core_expbuf(struct vb2_queue *q, int *fd, unsigned int type,
1834
		unsigned int index, unsigned int plane, unsigned int flags)
1835 1836 1837 1838 1839 1840
{
	struct vb2_buffer *vb = NULL;
	struct vb2_plane *vb_plane;
	int ret;
	struct dma_buf *dbuf;

1841
	if (q->memory != VB2_MEMORY_MMAP) {
1842
		dprintk(1, "queue is not currently set up for mmap\n");
1843 1844 1845 1846
		return -EINVAL;
	}

	if (!q->mem_ops->get_dmabuf) {
1847
		dprintk(1, "queue does not support DMA buffer exporting\n");
1848 1849 1850
		return -EINVAL;
	}

1851
	if (flags & ~(O_CLOEXEC | O_ACCMODE)) {
1852
		dprintk(1, "queue does support only O_CLOEXEC and access mode flags\n");
1853 1854 1855
		return -EINVAL;
	}

1856
	if (type != q->type) {
H
Hans Verkuil 已提交
1857
		dprintk(1, "invalid buffer type\n");
1858 1859 1860
		return -EINVAL;
	}

1861
	if (index >= q->num_buffers) {
1862 1863 1864 1865
		dprintk(1, "buffer index out of range\n");
		return -EINVAL;
	}

1866
	vb = q->bufs[index];
1867

1868
	if (plane >= vb->num_planes) {
1869 1870 1871 1872
		dprintk(1, "buffer plane out of range\n");
		return -EINVAL;
	}

1873 1874 1875 1876 1877
	if (vb2_fileio_is_active(q)) {
		dprintk(1, "expbuf: file io in progress\n");
		return -EBUSY;
	}

1878
	vb_plane = &vb->planes[plane];
1879

1880 1881
	dbuf = call_ptr_memop(vb, get_dmabuf, vb_plane->mem_priv,
				flags & O_ACCMODE);
1882
	if (IS_ERR_OR_NULL(dbuf)) {
1883
		dprintk(1, "failed to export buffer %d, plane %d\n",
1884
			index, plane);
1885 1886 1887
		return -EINVAL;
	}

1888
	ret = dma_buf_fd(dbuf, flags & ~O_ACCMODE);
1889 1890
	if (ret < 0) {
		dprintk(3, "buffer %d, plane %d failed to export (%d)\n",
1891
			index, plane, ret);
1892 1893 1894 1895 1896
		dma_buf_put(dbuf);
		return ret;
	}

	dprintk(3, "buffer %d, plane %d exported as %d descriptor\n",
1897 1898
		index, plane, ret);
	*fd = ret;
1899 1900 1901

	return 0;
}
1902
EXPORT_SYMBOL_GPL(vb2_core_expbuf);
1903

1904 1905 1906 1907
int vb2_mmap(struct vb2_queue *q, struct vm_area_struct *vma)
{
	unsigned long off = vma->vm_pgoff << PAGE_SHIFT;
	struct vb2_buffer *vb;
H
Hans Verkuil 已提交
1908
	unsigned int buffer = 0, plane = 0;
1909
	int ret;
1910
	unsigned long length;
1911

1912
	if (q->memory != VB2_MEMORY_MMAP) {
1913
		dprintk(1, "queue is not currently set up for mmap\n");
1914 1915 1916 1917 1918 1919 1920
		return -EINVAL;
	}

	/*
	 * Check memory area access mode.
	 */
	if (!(vma->vm_flags & VM_SHARED)) {
1921
		dprintk(1, "invalid vma flags, VM_SHARED needed\n");
1922 1923
		return -EINVAL;
	}
1924
	if (q->is_output) {
1925
		if (!(vma->vm_flags & VM_WRITE)) {
1926
			dprintk(1, "invalid vma flags, VM_WRITE needed\n");
1927 1928 1929 1930
			return -EINVAL;
		}
	} else {
		if (!(vma->vm_flags & VM_READ)) {
1931
			dprintk(1, "invalid vma flags, VM_READ needed\n");
1932 1933 1934
			return -EINVAL;
		}
	}
1935 1936 1937 1938
	if (vb2_fileio_is_active(q)) {
		dprintk(1, "mmap: file io in progress\n");
		return -EBUSY;
	}
1939 1940 1941 1942 1943 1944 1945 1946 1947 1948

	/*
	 * Find the plane corresponding to the offset passed by userspace.
	 */
	ret = __find_plane_by_offset(q, off, &buffer, &plane);
	if (ret)
		return ret;

	vb = q->bufs[buffer];

1949 1950 1951 1952 1953
	/*
	 * MMAP requires page_aligned buffers.
	 * The buffer length was page_aligned at __vb2_buf_mem_alloc(),
	 * so, we need to do the same here.
	 */
1954
	length = PAGE_ALIGN(vb->planes[plane].length);
1955 1956 1957
	if (length < (vma->vm_end - vma->vm_start)) {
		dprintk(1,
			"MMAP invalid, as it would overflow buffer length\n");
1958 1959 1960
		return -EINVAL;
	}

1961
	mutex_lock(&q->mmap_lock);
1962
	ret = call_memop(vb, mmap, vb->planes[plane].mem_priv, vma);
1963
	mutex_unlock(&q->mmap_lock);
1964
	if (ret)
1965 1966
		return ret;

1967
	dprintk(3, "buffer %d, plane %d successfully mapped\n", buffer, plane);
1968 1969 1970 1971
	return 0;
}
EXPORT_SYMBOL_GPL(vb2_mmap);

1972 1973 1974 1975 1976 1977 1978 1979 1980 1981
#ifndef CONFIG_MMU
unsigned long vb2_get_unmapped_area(struct vb2_queue *q,
				    unsigned long addr,
				    unsigned long len,
				    unsigned long pgoff,
				    unsigned long flags)
{
	unsigned long off = pgoff << PAGE_SHIFT;
	struct vb2_buffer *vb;
	unsigned int buffer, plane;
1982
	void *vaddr;
1983 1984
	int ret;

1985
	if (q->memory != VB2_MEMORY_MMAP) {
1986
		dprintk(1, "queue is not currently set up for mmap\n");
1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998
		return -EINVAL;
	}

	/*
	 * Find the plane corresponding to the offset passed by userspace.
	 */
	ret = __find_plane_by_offset(q, off, &buffer, &plane);
	if (ret)
		return ret;

	vb = q->bufs[buffer];

1999 2000
	vaddr = vb2_plane_vaddr(vb, plane);
	return vaddr ? (unsigned long)vaddr : -EINVAL;
2001 2002 2003 2004
}
EXPORT_SYMBOL_GPL(vb2_get_unmapped_area);
#endif

2005
int vb2_core_queue_init(struct vb2_queue *q)
2006
{
2007 2008 2009 2010 2011 2012 2013 2014 2015
	/*
	 * Sanity check
	 */
	if (WARN_ON(!q)			  ||
	    WARN_ON(!q->ops)		  ||
	    WARN_ON(!q->mem_ops)	  ||
	    WARN_ON(!q->type)		  ||
	    WARN_ON(!q->io_modes)	  ||
	    WARN_ON(!q->ops->queue_setup) ||
2016 2017 2018 2019 2020 2021 2022 2023 2024
	    WARN_ON(!q->ops->buf_queue))
		return -EINVAL;

	INIT_LIST_HEAD(&q->queued_list);
	INIT_LIST_HEAD(&q->done_list);
	spin_lock_init(&q->done_lock);
	mutex_init(&q->mmap_lock);
	init_waitqueue_head(&q->done_wq);

2025 2026
	q->memory = VB2_MEMORY_UNKNOWN;

2027 2028 2029
	if (q->buf_struct_size == 0)
		q->buf_struct_size = sizeof(struct vb2_buffer);

2030 2031 2032 2033 2034
	if (q->bidirectional)
		q->dma_dir = DMA_BIDIRECTIONAL;
	else
		q->dma_dir = q->is_output ? DMA_TO_DEVICE : DMA_FROM_DEVICE;

2035 2036
	return 0;
}
2037
EXPORT_SYMBOL_GPL(vb2_core_queue_init);
2038

2039 2040
static int __vb2_init_fileio(struct vb2_queue *q, int read);
static int __vb2_cleanup_fileio(struct vb2_queue *q);
2041
void vb2_core_queue_release(struct vb2_queue *q)
2042
{
2043
	__vb2_cleanup_fileio(q);
2044
	__vb2_queue_cancel(q);
2045
	mutex_lock(&q->mmap_lock);
2046
	__vb2_queue_free(q, q->num_buffers);
2047
	mutex_unlock(&q->mmap_lock);
2048
}
2049
EXPORT_SYMBOL_GPL(vb2_core_queue_release);
2050

A
Al Viro 已提交
2051
__poll_t vb2_core_poll(struct vb2_queue *q, struct file *file,
2052 2053
		poll_table *wait)
{
A
Al Viro 已提交
2054
	__poll_t req_events = poll_requested_events(wait);
2055 2056 2057
	struct vb2_buffer *vb = NULL;
	unsigned long flags;

2058
	if (!q->is_output && !(req_events & (EPOLLIN | EPOLLRDNORM)))
2059
		return 0;
2060
	if (q->is_output && !(req_events & (EPOLLOUT | EPOLLWRNORM)))
2061 2062 2063 2064 2065 2066 2067
		return 0;

	/*
	 * Start file I/O emulator only if streaming API has not been used yet.
	 */
	if (q->num_buffers == 0 && !vb2_fileio_is_active(q)) {
		if (!q->is_output && (q->io_modes & VB2_READ) &&
2068
				(req_events & (EPOLLIN | EPOLLRDNORM))) {
2069
			if (__vb2_init_fileio(q, 1))
2070
				return EPOLLERR;
2071 2072
		}
		if (q->is_output && (q->io_modes & VB2_WRITE) &&
2073
				(req_events & (EPOLLOUT | EPOLLWRNORM))) {
2074
			if (__vb2_init_fileio(q, 0))
2075
				return EPOLLERR;
2076 2077 2078
			/*
			 * Write to OUTPUT queue can be done immediately.
			 */
2079
			return EPOLLOUT | EPOLLWRNORM;
2080 2081 2082 2083 2084 2085 2086 2087
		}
	}

	/*
	 * There is nothing to wait for if the queue isn't streaming, or if the
	 * error flag is set.
	 */
	if (!vb2_is_streaming(q) || q->error)
2088
		return EPOLLERR;
2089

2090 2091
	/*
	 * If this quirk is set and QBUF hasn't been called yet then
2092
	 * return EPOLLERR as well. This only affects capture queues, output
2093 2094 2095 2096
	 * queues will always initialize waiting_for_buffers to false.
	 * This quirk is set by V4L2 for backwards compatibility reasons.
	 */
	if (q->quirk_poll_must_check_waiting_for_buffers &&
2097 2098
	    q->waiting_for_buffers && (req_events & (EPOLLIN | EPOLLRDNORM)))
		return EPOLLERR;
2099

2100 2101 2102 2103 2104
	/*
	 * For output streams you can call write() as long as there are fewer
	 * buffers queued than there are buffers available.
	 */
	if (q->is_output && q->fileio && q->queued_count < q->num_buffers)
2105
		return EPOLLOUT | EPOLLWRNORM;
2106 2107 2108 2109 2110 2111 2112

	if (list_empty(&q->done_list)) {
		/*
		 * If the last buffer was dequeued from a capture queue,
		 * return immediately. DQBUF will return -EPIPE.
		 */
		if (q->last_buffer_dequeued)
2113
			return EPOLLIN | EPOLLRDNORM;
2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129

		poll_wait(file, &q->done_wq, wait);
	}

	/*
	 * Take first buffer available for dequeuing.
	 */
	spin_lock_irqsave(&q->done_lock, flags);
	if (!list_empty(&q->done_list))
		vb = list_first_entry(&q->done_list, struct vb2_buffer,
					done_entry);
	spin_unlock_irqrestore(&q->done_lock, flags);

	if (vb && (vb->state == VB2_BUF_STATE_DONE
			|| vb->state == VB2_BUF_STATE_ERROR)) {
		return (q->is_output) ?
2130 2131
				EPOLLOUT | EPOLLWRNORM :
				EPOLLIN | EPOLLRDNORM;
2132 2133 2134 2135 2136
	}
	return 0;
}
EXPORT_SYMBOL_GPL(vb2_core_poll);

2137
/*
2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150
 * struct vb2_fileio_buf - buffer context used by file io emulator
 *
 * vb2 provides a compatibility layer and emulator of file io (read and
 * write) calls on top of streaming API. This structure is used for
 * tracking context related to the buffers.
 */
struct vb2_fileio_buf {
	void *vaddr;
	unsigned int size;
	unsigned int pos;
	unsigned int queued:1;
};

2151
/*
2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187
 * struct vb2_fileio_data - queue context used by file io emulator
 *
 * @cur_index:	the index of the buffer currently being read from or
 *		written to. If equal to q->num_buffers then a new buffer
 *		must be dequeued.
 * @initial_index: in the read() case all buffers are queued up immediately
 *		in __vb2_init_fileio() and __vb2_perform_fileio() just cycles
 *		buffers. However, in the write() case no buffers are initially
 *		queued, instead whenever a buffer is full it is queued up by
 *		__vb2_perform_fileio(). Only once all available buffers have
 *		been queued up will __vb2_perform_fileio() start to dequeue
 *		buffers. This means that initially __vb2_perform_fileio()
 *		needs to know what buffer index to use when it is queuing up
 *		the buffers for the first time. That initial index is stored
 *		in this field. Once it is equal to q->num_buffers all
 *		available buffers have been queued and __vb2_perform_fileio()
 *		should start the normal dequeue/queue cycle.
 *
 * vb2 provides a compatibility layer and emulator of file io (read and
 * write) calls on top of streaming API. For proper operation it required
 * this structure to save the driver state between each call of the read
 * or write function.
 */
struct vb2_fileio_data {
	unsigned int count;
	unsigned int type;
	unsigned int memory;
	struct vb2_fileio_buf bufs[VB2_MAX_FRAME];
	unsigned int cur_index;
	unsigned int initial_index;
	unsigned int q_count;
	unsigned int dq_count;
	unsigned read_once:1;
	unsigned write_immediately:1;
};

2188
/*
2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226
 * __vb2_init_fileio() - initialize file io emulator
 * @q:		videobuf2 queue
 * @read:	mode selector (1 means read, 0 means write)
 */
static int __vb2_init_fileio(struct vb2_queue *q, int read)
{
	struct vb2_fileio_data *fileio;
	int i, ret;
	unsigned int count = 0;

	/*
	 * Sanity check
	 */
	if (WARN_ON((read && !(q->io_modes & VB2_READ)) ||
		    (!read && !(q->io_modes & VB2_WRITE))))
		return -EINVAL;

	/*
	 * Check if device supports mapping buffers to kernel virtual space.
	 */
	if (!q->mem_ops->vaddr)
		return -EBUSY;

	/*
	 * Check if streaming api has not been already activated.
	 */
	if (q->streaming || q->num_buffers > 0)
		return -EBUSY;

	/*
	 * Start with count 1, driver can increase it in queue_setup()
	 */
	count = 1;

	dprintk(3, "setting up file io: mode %s, count %d, read_once %d, write_immediately %d\n",
		(read) ? "read" : "write", count, q->fileio_read_once,
		q->fileio_write_immediately);

2227
	fileio = kzalloc(sizeof(*fileio), GFP_KERNEL);
2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274
	if (fileio == NULL)
		return -ENOMEM;

	fileio->read_once = q->fileio_read_once;
	fileio->write_immediately = q->fileio_write_immediately;

	/*
	 * Request buffers and use MMAP type to force driver
	 * to allocate buffers by itself.
	 */
	fileio->count = count;
	fileio->memory = VB2_MEMORY_MMAP;
	fileio->type = q->type;
	q->fileio = fileio;
	ret = vb2_core_reqbufs(q, fileio->memory, &fileio->count);
	if (ret)
		goto err_kfree;

	/*
	 * Check if plane_count is correct
	 * (multiplane buffers are not supported).
	 */
	if (q->bufs[0]->num_planes != 1) {
		ret = -EBUSY;
		goto err_reqbufs;
	}

	/*
	 * Get kernel address of each buffer.
	 */
	for (i = 0; i < q->num_buffers; i++) {
		fileio->bufs[i].vaddr = vb2_plane_vaddr(q->bufs[i], 0);
		if (fileio->bufs[i].vaddr == NULL) {
			ret = -EINVAL;
			goto err_reqbufs;
		}
		fileio->bufs[i].size = vb2_plane_size(q->bufs[i], 0);
	}

	/*
	 * Read mode requires pre queuing of all buffers.
	 */
	if (read) {
		/*
		 * Queue all buffers.
		 */
		for (i = 0; i < q->num_buffers; i++) {
2275
			ret = vb2_core_qbuf(q, i, NULL);
2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306
			if (ret)
				goto err_reqbufs;
			fileio->bufs[i].queued = 1;
		}
		/*
		 * All buffers have been queued, so mark that by setting
		 * initial_index to q->num_buffers
		 */
		fileio->initial_index = q->num_buffers;
		fileio->cur_index = q->num_buffers;
	}

	/*
	 * Start streaming.
	 */
	ret = vb2_core_streamon(q, q->type);
	if (ret)
		goto err_reqbufs;

	return ret;

err_reqbufs:
	fileio->count = 0;
	vb2_core_reqbufs(q, fileio->memory, &fileio->count);

err_kfree:
	q->fileio = NULL;
	kfree(fileio);
	return ret;
}

2307
/*
2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325
 * __vb2_cleanup_fileio() - free resourced used by file io emulator
 * @q:		videobuf2 queue
 */
static int __vb2_cleanup_fileio(struct vb2_queue *q)
{
	struct vb2_fileio_data *fileio = q->fileio;

	if (fileio) {
		vb2_core_streamoff(q, q->type);
		q->fileio = NULL;
		fileio->count = 0;
		vb2_core_reqbufs(q, fileio->memory, &fileio->count);
		kfree(fileio);
		dprintk(3, "file io emulator closed\n");
	}
	return 0;
}

2326
/*
2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346
 * __vb2_perform_fileio() - perform a single file io (read or write) operation
 * @q:		videobuf2 queue
 * @data:	pointed to target userspace buffer
 * @count:	number of bytes to read or write
 * @ppos:	file handle position tracking pointer
 * @nonblock:	mode selector (1 means blocking calls, 0 means nonblocking)
 * @read:	access mode selector (1 means read, 0 means write)
 */
static size_t __vb2_perform_fileio(struct vb2_queue *q, char __user *data, size_t count,
		loff_t *ppos, int nonblock, int read)
{
	struct vb2_fileio_data *fileio;
	struct vb2_fileio_buf *buf;
	bool is_multiplanar = q->is_multiplanar;
	/*
	 * When using write() to write data to an output video node the vb2 core
	 * should copy timestamps if V4L2_BUF_FLAG_TIMESTAMP_COPY is set. Nobody
	 * else is able to provide this information with the write() operation.
	 */
	bool copy_timestamp = !read && q->copy_timestamp;
2347 2348
	unsigned index;
	int ret;
2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372

	dprintk(3, "mode %s, offset %ld, count %zd, %sblocking\n",
		read ? "read" : "write", (long)*ppos, count,
		nonblock ? "non" : "");

	if (!data)
		return -EINVAL;

	/*
	 * Initialize emulator on first call.
	 */
	if (!vb2_fileio_is_active(q)) {
		ret = __vb2_init_fileio(q, read);
		dprintk(3, "vb2_init_fileio result: %d\n", ret);
		if (ret)
			return ret;
	}
	fileio = q->fileio;

	/*
	 * Check if we need to dequeue the buffer.
	 */
	index = fileio->cur_index;
	if (index >= q->num_buffers) {
2373
		struct vb2_buffer *b;
2374 2375 2376 2377

		/*
		 * Call vb2_dqbuf to get buffer back.
		 */
2378
		ret = vb2_core_dqbuf(q, &index, NULL, nonblock);
2379 2380 2381 2382 2383
		dprintk(5, "vb2_dqbuf result: %d\n", ret);
		if (ret)
			return ret;
		fileio->dq_count += 1;

2384
		fileio->cur_index = index;
2385
		buf = &fileio->bufs[index];
2386
		b = q->bufs[index];
2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436

		/*
		 * Get number of bytes filled by the driver
		 */
		buf->pos = 0;
		buf->queued = 0;
		buf->size = read ? vb2_get_plane_payload(q->bufs[index], 0)
				 : vb2_plane_size(q->bufs[index], 0);
		/* Compensate for data_offset on read in the multiplanar case. */
		if (is_multiplanar && read &&
				b->planes[0].data_offset < buf->size) {
			buf->pos = b->planes[0].data_offset;
			buf->size -= buf->pos;
		}
	} else {
		buf = &fileio->bufs[index];
	}

	/*
	 * Limit count on last few bytes of the buffer.
	 */
	if (buf->pos + count > buf->size) {
		count = buf->size - buf->pos;
		dprintk(5, "reducing read count: %zd\n", count);
	}

	/*
	 * Transfer data to userspace.
	 */
	dprintk(3, "copying %zd bytes - buffer %d, offset %u\n",
		count, index, buf->pos);
	if (read)
		ret = copy_to_user(data, buf->vaddr + buf->pos, count);
	else
		ret = copy_from_user(buf->vaddr + buf->pos, data, count);
	if (ret) {
		dprintk(3, "error copying data\n");
		return -EFAULT;
	}

	/*
	 * Update counters.
	 */
	buf->pos += count;
	*ppos += count;

	/*
	 * Queue next buffer if required.
	 */
	if (buf->pos == buf->size || (!read && fileio->write_immediately)) {
2437
		struct vb2_buffer *b = q->bufs[index];
2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453

		/*
		 * Check if this is the last buffer to read.
		 */
		if (read && fileio->read_once && fileio->dq_count == 1) {
			dprintk(3, "read limit reached\n");
			return __vb2_cleanup_fileio(q);
		}

		/*
		 * Call vb2_qbuf and give buffer to the driver.
		 */
		b->planes[0].bytesused = buf->pos;

		if (copy_timestamp)
			b->timestamp = ktime_get_ns();
2454
		ret = vb2_core_qbuf(q, index, NULL);
2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516
		dprintk(5, "vb2_dbuf result: %d\n", ret);
		if (ret)
			return ret;

		/*
		 * Buffer has been queued, update the status
		 */
		buf->pos = 0;
		buf->queued = 1;
		buf->size = vb2_plane_size(q->bufs[index], 0);
		fileio->q_count += 1;
		/*
		 * If we are queuing up buffers for the first time, then
		 * increase initial_index by one.
		 */
		if (fileio->initial_index < q->num_buffers)
			fileio->initial_index++;
		/*
		 * The next buffer to use is either a buffer that's going to be
		 * queued for the first time (initial_index < q->num_buffers)
		 * or it is equal to q->num_buffers, meaning that the next
		 * time we need to dequeue a buffer since we've now queued up
		 * all the 'first time' buffers.
		 */
		fileio->cur_index = fileio->initial_index;
	}

	/*
	 * Return proper number of bytes processed.
	 */
	if (ret == 0)
		ret = count;
	return ret;
}

size_t vb2_read(struct vb2_queue *q, char __user *data, size_t count,
		loff_t *ppos, int nonblocking)
{
	return __vb2_perform_fileio(q, data, count, ppos, nonblocking, 1);
}
EXPORT_SYMBOL_GPL(vb2_read);

size_t vb2_write(struct vb2_queue *q, const char __user *data, size_t count,
		loff_t *ppos, int nonblocking)
{
	return __vb2_perform_fileio(q, (char __user *) data, count,
							ppos, nonblocking, 0);
}
EXPORT_SYMBOL_GPL(vb2_write);

struct vb2_threadio_data {
	struct task_struct *thread;
	vb2_thread_fnc fnc;
	void *priv;
	bool stop;
};

static int vb2_thread(void *data)
{
	struct vb2_queue *q = data;
	struct vb2_threadio_data *threadio = q->threadio;
	bool copy_timestamp = false;
2517 2518
	unsigned prequeue = 0;
	unsigned index = 0;
2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534
	int ret = 0;

	if (q->is_output) {
		prequeue = q->num_buffers;
		copy_timestamp = q->copy_timestamp;
	}

	set_freezable();

	for (;;) {
		struct vb2_buffer *vb;

		/*
		 * Call vb2_dqbuf to get buffer back.
		 */
		if (prequeue) {
2535
			vb = q->bufs[index++];
2536 2537 2538 2539
			prequeue--;
		} else {
			call_void_qop(q, wait_finish, q);
			if (!threadio->stop)
2540
				ret = vb2_core_dqbuf(q, &index, NULL, 0);
2541 2542
			call_void_qop(q, wait_prepare, q);
			dprintk(5, "file io: vb2_dqbuf result: %d\n", ret);
2543 2544
			if (!ret)
				vb = q->bufs[index];
2545 2546 2547 2548 2549
		}
		if (ret || threadio->stop)
			break;
		try_to_freeze();

2550
		if (vb->state != VB2_BUF_STATE_ERROR)
2551 2552 2553 2554
			if (threadio->fnc(vb, threadio->priv))
				break;
		call_void_qop(q, wait_finish, q);
		if (copy_timestamp)
F
Fengguang Wu 已提交
2555
			vb->timestamp = ktime_get_ns();
2556
		if (!threadio->stop)
2557
			ret = vb2_core_qbuf(q, vb->index, NULL);
2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634
		call_void_qop(q, wait_prepare, q);
		if (ret || threadio->stop)
			break;
	}

	/* Hmm, linux becomes *very* unhappy without this ... */
	while (!kthread_should_stop()) {
		set_current_state(TASK_INTERRUPTIBLE);
		schedule();
	}
	return 0;
}

/*
 * This function should not be used for anything else but the videobuf2-dvb
 * support. If you think you have another good use-case for this, then please
 * contact the linux-media mailinglist first.
 */
int vb2_thread_start(struct vb2_queue *q, vb2_thread_fnc fnc, void *priv,
		     const char *thread_name)
{
	struct vb2_threadio_data *threadio;
	int ret = 0;

	if (q->threadio)
		return -EBUSY;
	if (vb2_is_busy(q))
		return -EBUSY;
	if (WARN_ON(q->fileio))
		return -EBUSY;

	threadio = kzalloc(sizeof(*threadio), GFP_KERNEL);
	if (threadio == NULL)
		return -ENOMEM;
	threadio->fnc = fnc;
	threadio->priv = priv;

	ret = __vb2_init_fileio(q, !q->is_output);
	dprintk(3, "file io: vb2_init_fileio result: %d\n", ret);
	if (ret)
		goto nomem;
	q->threadio = threadio;
	threadio->thread = kthread_run(vb2_thread, q, "vb2-%s", thread_name);
	if (IS_ERR(threadio->thread)) {
		ret = PTR_ERR(threadio->thread);
		threadio->thread = NULL;
		goto nothread;
	}
	return 0;

nothread:
	__vb2_cleanup_fileio(q);
nomem:
	kfree(threadio);
	return ret;
}
EXPORT_SYMBOL_GPL(vb2_thread_start);

int vb2_thread_stop(struct vb2_queue *q)
{
	struct vb2_threadio_data *threadio = q->threadio;
	int err;

	if (threadio == NULL)
		return 0;
	threadio->stop = true;
	/* Wake up all pending sleeps in the thread */
	vb2_queue_error(q);
	err = kthread_stop(threadio->thread);
	__vb2_cleanup_fileio(q);
	threadio->thread = NULL;
	kfree(threadio);
	q->threadio = NULL;
	return err;
}
EXPORT_SYMBOL_GPL(vb2_thread_stop);

2635
MODULE_DESCRIPTION("Media buffer core framework");
2636
MODULE_AUTHOR("Pawel Osciak <pawel@osciak.com>, Marek Szyprowski");
2637
MODULE_LICENSE("GPL");