xref: /openbmc/linux/drivers/media/dvb-core/dvb_vb2.c (revision 7b361cf0)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * dvb-vb2.c - dvb-vb2
4  *
5  * Copyright (C) 2015 Samsung Electronics
6  *
7  * Author: jh1009.sung@samsung.com
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation.
12  */
13 
14 #include <linux/err.h>
15 #include <linux/kernel.h>
16 #include <linux/module.h>
17 #include <linux/mm.h>
18 
19 #include "dvbdev.h"
20 #include "dvb_vb2.h"
21 
22 #define DVB_V2_MAX_SIZE		(4096 * 188)
23 
24 static int vb2_debug;
25 module_param(vb2_debug, int, 0644);
26 
27 #define dprintk(level, fmt, arg...)					      \
28 	do {								      \
29 		if (vb2_debug >= level)					      \
30 			pr_info("vb2: %s: " fmt, __func__, ## arg); \
31 	} while (0)
32 
33 static int _queue_setup(struct vb2_queue *vq,
34 			unsigned int *nbuffers, unsigned int *nplanes,
35 			unsigned int sizes[], struct device *alloc_devs[])
36 {
37 	struct dvb_vb2_ctx *ctx = vb2_get_drv_priv(vq);
38 
39 	ctx->buf_cnt = *nbuffers;
40 	*nplanes = 1;
41 	sizes[0] = ctx->buf_siz;
42 
43 	/*
44 	 * videobuf2-vmalloc allocator is context-less so no need to set
45 	 * alloc_ctxs array.
46 	 */
47 
48 	dprintk(3, "[%s] count=%d, size=%d\n", ctx->name,
49 		*nbuffers, sizes[0]);
50 
51 	return 0;
52 }
53 
54 static int _buffer_prepare(struct vb2_buffer *vb)
55 {
56 	struct dvb_vb2_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
57 	unsigned long size = ctx->buf_siz;
58 
59 	if (vb2_plane_size(vb, 0) < size) {
60 		dprintk(1, "[%s] data will not fit into plane (%lu < %lu)\n",
61 			ctx->name, vb2_plane_size(vb, 0), size);
62 		return -EINVAL;
63 	}
64 
65 	vb2_set_plane_payload(vb, 0, size);
66 	dprintk(3, "[%s]\n", ctx->name);
67 
68 	return 0;
69 }
70 
71 static void _buffer_queue(struct vb2_buffer *vb)
72 {
73 	struct dvb_vb2_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
74 	struct dvb_buffer *buf = container_of(vb, struct dvb_buffer, vb);
75 	unsigned long flags = 0;
76 
77 	spin_lock_irqsave(&ctx->slock, flags);
78 	list_add_tail(&buf->list, &ctx->dvb_q);
79 	spin_unlock_irqrestore(&ctx->slock, flags);
80 
81 	dprintk(3, "[%s]\n", ctx->name);
82 }
83 
84 static int _start_streaming(struct vb2_queue *vq, unsigned int count)
85 {
86 	struct dvb_vb2_ctx *ctx = vb2_get_drv_priv(vq);
87 
88 	dprintk(3, "[%s] count=%d\n", ctx->name, count);
89 	return 0;
90 }
91 
92 static void _stop_streaming(struct vb2_queue *vq)
93 {
94 	struct dvb_vb2_ctx *ctx = vb2_get_drv_priv(vq);
95 	struct dvb_buffer *buf;
96 	unsigned long flags = 0;
97 
98 	dprintk(3, "[%s]\n", ctx->name);
99 
100 	spin_lock_irqsave(&ctx->slock, flags);
101 	while (!list_empty(&ctx->dvb_q)) {
102 		buf = list_entry(ctx->dvb_q.next,
103 				 struct dvb_buffer, list);
104 		vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
105 		list_del(&buf->list);
106 	}
107 	spin_unlock_irqrestore(&ctx->slock, flags);
108 }
109 
110 static void _dmxdev_lock(struct vb2_queue *vq)
111 {
112 	struct dvb_vb2_ctx *ctx = vb2_get_drv_priv(vq);
113 
114 	mutex_lock(&ctx->mutex);
115 	dprintk(3, "[%s]\n", ctx->name);
116 }
117 
118 static void _dmxdev_unlock(struct vb2_queue *vq)
119 {
120 	struct dvb_vb2_ctx *ctx = vb2_get_drv_priv(vq);
121 
122 	if (mutex_is_locked(&ctx->mutex))
123 		mutex_unlock(&ctx->mutex);
124 	dprintk(3, "[%s]\n", ctx->name);
125 }
126 
127 static const struct vb2_ops dvb_vb2_qops = {
128 	.queue_setup		= _queue_setup,
129 	.buf_prepare		= _buffer_prepare,
130 	.buf_queue		= _buffer_queue,
131 	.start_streaming	= _start_streaming,
132 	.stop_streaming		= _stop_streaming,
133 	.wait_prepare		= _dmxdev_unlock,
134 	.wait_finish		= _dmxdev_lock,
135 };
136 
137 static void _fill_dmx_buffer(struct vb2_buffer *vb, void *pb)
138 {
139 	struct dvb_vb2_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
140 	struct dmx_buffer *b = pb;
141 
142 	b->index = vb->index;
143 	b->length = vb->planes[0].length;
144 	b->bytesused = vb->planes[0].bytesused;
145 	b->offset = vb->planes[0].m.offset;
146 	memset(b->reserved, 0, sizeof(b->reserved));
147 	dprintk(3, "[%s]\n", ctx->name);
148 }
149 
150 static int _fill_vb2_buffer(struct vb2_buffer *vb,
151 			    const void *pb, struct vb2_plane *planes)
152 {
153 	struct dvb_vb2_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
154 
155 	planes[0].bytesused = 0;
156 	dprintk(3, "[%s]\n", ctx->name);
157 
158 	return 0;
159 }
160 
161 static const struct vb2_buf_ops dvb_vb2_buf_ops = {
162 	.fill_user_buffer	= _fill_dmx_buffer,
163 	.fill_vb2_buffer	= _fill_vb2_buffer,
164 };
165 
166 /*
167  * Videobuf operations
168  */
169 int dvb_vb2_init(struct dvb_vb2_ctx *ctx, const char *name, int nonblocking)
170 {
171 	struct vb2_queue *q = &ctx->vb_q;
172 	int ret;
173 
174 	memset(ctx, 0, sizeof(struct dvb_vb2_ctx));
175 	q->type = DVB_BUF_TYPE_CAPTURE;
176 	/**capture type*/
177 	q->is_output = 0;
178 	/**only mmap is supported currently*/
179 	q->io_modes = VB2_MMAP;
180 	q->drv_priv = ctx;
181 	q->buf_struct_size = sizeof(struct dvb_buffer);
182 	q->min_buffers_needed = 1;
183 	q->ops = &dvb_vb2_qops;
184 	q->mem_ops = &vb2_vmalloc_memops;
185 	q->buf_ops = &dvb_vb2_buf_ops;
186 	q->num_buffers = 0;
187 	ret = vb2_core_queue_init(q);
188 	if (ret) {
189 		ctx->state = DVB_VB2_STATE_NONE;
190 		dprintk(1, "[%s] errno=%d\n", ctx->name, ret);
191 		return ret;
192 	}
193 
194 	mutex_init(&ctx->mutex);
195 	spin_lock_init(&ctx->slock);
196 	INIT_LIST_HEAD(&ctx->dvb_q);
197 
198 	strncpy(ctx->name, name, DVB_VB2_NAME_MAX);
199 	ctx->nonblocking = nonblocking;
200 	ctx->state = DVB_VB2_STATE_INIT;
201 
202 	dprintk(3, "[%s]\n", ctx->name);
203 
204 	return 0;
205 }
206 
207 int dvb_vb2_release(struct dvb_vb2_ctx *ctx)
208 {
209 	struct vb2_queue *q = (struct vb2_queue *)&ctx->vb_q;
210 
211 	if (ctx->state & DVB_VB2_STATE_INIT)
212 		vb2_core_queue_release(q);
213 
214 	ctx->state = DVB_VB2_STATE_NONE;
215 	dprintk(3, "[%s]\n", ctx->name);
216 
217 	return 0;
218 }
219 
220 int dvb_vb2_stream_on(struct dvb_vb2_ctx *ctx)
221 {
222 	struct vb2_queue *q = &ctx->vb_q;
223 	int ret;
224 
225 	ret = vb2_core_streamon(q, q->type);
226 	if (ret) {
227 		ctx->state = DVB_VB2_STATE_NONE;
228 		dprintk(1, "[%s] errno=%d\n", ctx->name, ret);
229 		return ret;
230 	}
231 	ctx->state |= DVB_VB2_STATE_STREAMON;
232 	dprintk(3, "[%s]\n", ctx->name);
233 
234 	return 0;
235 }
236 
237 int dvb_vb2_stream_off(struct dvb_vb2_ctx *ctx)
238 {
239 	struct vb2_queue *q = (struct vb2_queue *)&ctx->vb_q;
240 	int ret;
241 
242 	ctx->state &= ~DVB_VB2_STATE_STREAMON;
243 	ret = vb2_core_streamoff(q, q->type);
244 	if (ret) {
245 		ctx->state = DVB_VB2_STATE_NONE;
246 		dprintk(1, "[%s] errno=%d\n", ctx->name, ret);
247 		return ret;
248 	}
249 	dprintk(3, "[%s]\n", ctx->name);
250 
251 	return 0;
252 }
253 
254 int dvb_vb2_is_streaming(struct dvb_vb2_ctx *ctx)
255 {
256 	return (ctx->state & DVB_VB2_STATE_STREAMON);
257 }
258 
259 int dvb_vb2_fill_buffer(struct dvb_vb2_ctx *ctx,
260 			const unsigned char *src, int len)
261 {
262 	unsigned long flags = 0;
263 	void *vbuf = NULL;
264 	int todo = len;
265 	unsigned char *psrc = (unsigned char *)src;
266 	int ll = 0;
267 
268 	dprintk(3, "[%s] %d bytes are rcvd\n", ctx->name, len);
269 	if (!src) {
270 		dprintk(3, "[%s]:NULL pointer src\n", ctx->name);
271 		/**normal case: This func is called twice from demux driver
272 		 * once with valid src pointer, second time with NULL pointer
273 		 */
274 		return 0;
275 	}
276 	spin_lock_irqsave(&ctx->slock, flags);
277 	while (todo) {
278 		if (!ctx->buf) {
279 			if (list_empty(&ctx->dvb_q)) {
280 				dprintk(3, "[%s] Buffer overflow!!!\n",
281 					ctx->name);
282 				break;
283 			}
284 
285 			ctx->buf = list_entry(ctx->dvb_q.next,
286 					      struct dvb_buffer, list);
287 			ctx->remain = vb2_plane_size(&ctx->buf->vb, 0);
288 			ctx->offset = 0;
289 		}
290 
291 		if (!dvb_vb2_is_streaming(ctx)) {
292 			vb2_buffer_done(&ctx->buf->vb, VB2_BUF_STATE_ERROR);
293 			list_del(&ctx->buf->list);
294 			ctx->buf = NULL;
295 			break;
296 		}
297 
298 		/* Fill buffer */
299 		ll = min(todo, ctx->remain);
300 		vbuf = vb2_plane_vaddr(&ctx->buf->vb, 0);
301 		memcpy(vbuf + ctx->offset, psrc, ll);
302 		todo -= ll;
303 		psrc += ll;
304 
305 		ctx->remain -= ll;
306 		ctx->offset += ll;
307 
308 		if (ctx->remain == 0) {
309 			vb2_buffer_done(&ctx->buf->vb, VB2_BUF_STATE_DONE);
310 			list_del(&ctx->buf->list);
311 			ctx->buf = NULL;
312 		}
313 	}
314 
315 	if (ctx->nonblocking && ctx->buf) {
316 		vb2_set_plane_payload(&ctx->buf->vb, 0, ll);
317 		vb2_buffer_done(&ctx->buf->vb, VB2_BUF_STATE_DONE);
318 		list_del(&ctx->buf->list);
319 		ctx->buf = NULL;
320 	}
321 	spin_unlock_irqrestore(&ctx->slock, flags);
322 
323 	if (todo)
324 		dprintk(1, "[%s] %d bytes are dropped.\n", ctx->name, todo);
325 	else
326 		dprintk(3, "[%s]\n", ctx->name);
327 
328 	dprintk(3, "[%s] %d bytes are copied\n", ctx->name, len - todo);
329 	return (len - todo);
330 }
331 
332 int dvb_vb2_reqbufs(struct dvb_vb2_ctx *ctx, struct dmx_requestbuffers *req)
333 {
334 	int ret;
335 
336 	/* Adjust size to a sane value */
337 	if (req->size > DVB_V2_MAX_SIZE)
338 		req->size = DVB_V2_MAX_SIZE;
339 
340 	/* FIXME: round req->size to a 188 or 204 multiple */
341 
342 	ctx->buf_siz = req->size;
343 	ctx->buf_cnt = req->count;
344 	ret = vb2_core_reqbufs(&ctx->vb_q, VB2_MEMORY_MMAP, &req->count);
345 	if (ret) {
346 		ctx->state = DVB_VB2_STATE_NONE;
347 		dprintk(1, "[%s] count=%d size=%d errno=%d\n", ctx->name,
348 			ctx->buf_cnt, ctx->buf_siz, ret);
349 		return ret;
350 	}
351 	ctx->state |= DVB_VB2_STATE_REQBUFS;
352 	dprintk(3, "[%s] count=%d size=%d\n", ctx->name,
353 		ctx->buf_cnt, ctx->buf_siz);
354 
355 	return 0;
356 }
357 
358 int dvb_vb2_querybuf(struct dvb_vb2_ctx *ctx, struct dmx_buffer *b)
359 {
360 	vb2_core_querybuf(&ctx->vb_q, b->index, b);
361 	dprintk(3, "[%s] index=%d\n", ctx->name, b->index);
362 	return 0;
363 }
364 
365 int dvb_vb2_expbuf(struct dvb_vb2_ctx *ctx, struct dmx_exportbuffer *exp)
366 {
367 	struct vb2_queue *q = &ctx->vb_q;
368 	int ret;
369 
370 	ret = vb2_core_expbuf(&ctx->vb_q, &exp->fd, q->type, exp->index,
371 			      0, exp->flags);
372 	if (ret) {
373 		dprintk(1, "[%s] index=%d errno=%d\n", ctx->name,
374 			exp->index, ret);
375 		return ret;
376 	}
377 	dprintk(3, "[%s] index=%d fd=%d\n", ctx->name, exp->index, exp->fd);
378 
379 	return 0;
380 }
381 
382 int dvb_vb2_qbuf(struct dvb_vb2_ctx *ctx, struct dmx_buffer *b)
383 {
384 	int ret;
385 
386 	ret = vb2_core_qbuf(&ctx->vb_q, b->index, b);
387 	if (ret) {
388 		dprintk(1, "[%s] index=%d errno=%d\n", ctx->name,
389 			b->index, ret);
390 		return ret;
391 	}
392 	dprintk(5, "[%s] index=%d\n", ctx->name, b->index);
393 
394 	return 0;
395 }
396 
397 int dvb_vb2_dqbuf(struct dvb_vb2_ctx *ctx, struct dmx_buffer *b)
398 {
399 	int ret;
400 
401 	ret = vb2_core_dqbuf(&ctx->vb_q, &b->index, b, ctx->nonblocking);
402 	if (ret) {
403 		dprintk(1, "[%s] errno=%d\n", ctx->name, ret);
404 		return ret;
405 	}
406 	dprintk(5, "[%s] index=%d\n", ctx->name, b->index);
407 
408 	return 0;
409 }
410 
411 int dvb_vb2_mmap(struct dvb_vb2_ctx *ctx, struct vm_area_struct *vma)
412 {
413 	int ret;
414 
415 	ret = vb2_mmap(&ctx->vb_q, vma);
416 	if (ret) {
417 		dprintk(1, "[%s] errno=%d\n", ctx->name, ret);
418 		return ret;
419 	}
420 	dprintk(3, "[%s] ret=%d\n", ctx->name, ret);
421 
422 	return 0;
423 }
424 
425 unsigned int dvb_vb2_poll(struct dvb_vb2_ctx *ctx, struct file *file,
426 			  poll_table *wait)
427 {
428 	dprintk(3, "[%s]\n", ctx->name);
429 	return vb2_core_poll(&ctx->vb_q, file, wait);
430 }
431 
432