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