xref: /openbmc/linux/drivers/media/dvb-core/dvb_vb2.c (revision 57868acc)
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 static int vb2_debug;
22 module_param(vb2_debug, int, 0644);
23 
24 #define dprintk(level, fmt, arg...)					      \
25 	do {								      \
26 		if (vb2_debug >= level)					      \
27 			pr_info("vb2: %s: " fmt, __func__, ## arg); \
28 	} while (0)
29 
30 static int _queue_setup(struct vb2_queue *vq,
31 			unsigned int *nbuffers, unsigned int *nplanes,
32 			unsigned int sizes[], struct device *alloc_devs[])
33 {
34 	struct dvb_vb2_ctx *ctx = vb2_get_drv_priv(vq);
35 
36 	*nbuffers = ctx->buf_cnt;
37 	*nplanes = 1;
38 	sizes[0] = ctx->buf_siz;
39 
40 	/*
41 	 * videobuf2-vmalloc allocator is context-less so no need to set
42 	 * alloc_ctxs array.
43 	 */
44 
45 	dprintk(3, "[%s] count=%d, size=%d\n", ctx->name,
46 		*nbuffers, sizes[0]);
47 
48 	return 0;
49 }
50 
51 static int _buffer_prepare(struct vb2_buffer *vb)
52 {
53 	struct dvb_vb2_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
54 	unsigned long size = ctx->buf_siz;
55 
56 	if (vb2_plane_size(vb, 0) < size) {
57 		dprintk(1, "[%s] data will not fit into plane (%lu < %lu)\n",
58 			ctx->name, vb2_plane_size(vb, 0), size);
59 		return -EINVAL;
60 	}
61 
62 	vb2_set_plane_payload(vb, 0, size);
63 	dprintk(3, "[%s]\n", ctx->name);
64 
65 	return 0;
66 }
67 
68 static void _buffer_queue(struct vb2_buffer *vb)
69 {
70 	struct dvb_vb2_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
71 	struct dvb_buffer *buf = container_of(vb, struct dvb_buffer, vb);
72 	unsigned long flags = 0;
73 
74 	spin_lock_irqsave(&ctx->slock, flags);
75 	list_add_tail(&buf->list, &ctx->dvb_q);
76 	spin_unlock_irqrestore(&ctx->slock, flags);
77 
78 	dprintk(3, "[%s]\n", ctx->name);
79 }
80 
81 static int _start_streaming(struct vb2_queue *vq, unsigned int count)
82 {
83 	struct dvb_vb2_ctx *ctx = vb2_get_drv_priv(vq);
84 
85 	dprintk(3, "[%s] count=%d\n", ctx->name, count);
86 	return 0;
87 }
88 
89 static void _stop_streaming(struct vb2_queue *vq)
90 {
91 	struct dvb_vb2_ctx *ctx = vb2_get_drv_priv(vq);
92 
93 	dprintk(3, "[%s]\n", ctx->name);
94 }
95 
96 static void _dmxdev_lock(struct vb2_queue *vq)
97 {
98 	struct dvb_vb2_ctx *ctx = vb2_get_drv_priv(vq);
99 
100 	mutex_lock(&ctx->mutex);
101 	dprintk(3, "[%s]\n", ctx->name);
102 }
103 
104 static void _dmxdev_unlock(struct vb2_queue *vq)
105 {
106 	struct dvb_vb2_ctx *ctx = vb2_get_drv_priv(vq);
107 
108 	if (mutex_is_locked(&ctx->mutex))
109 		mutex_unlock(&ctx->mutex);
110 	dprintk(3, "[%s]\n", ctx->name);
111 }
112 
113 static const struct vb2_ops dvb_vb2_qops = {
114 	.queue_setup		= _queue_setup,
115 	.buf_prepare		= _buffer_prepare,
116 	.buf_queue		= _buffer_queue,
117 	.start_streaming	= _start_streaming,
118 	.stop_streaming		= _stop_streaming,
119 	.wait_prepare		= _dmxdev_unlock,
120 	.wait_finish		= _dmxdev_lock,
121 };
122 
123 static void _fill_dmx_buffer(struct vb2_buffer *vb, void *pb)
124 {
125 	struct dvb_vb2_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
126 	struct dmx_buffer *b = pb;
127 
128 	b->index = vb->index;
129 	b->length = vb->planes[0].length;
130 	b->bytesused = vb->planes[0].bytesused;
131 	b->offset = vb->planes[0].m.offset;
132 	memset(b->reserved, 0, sizeof(b->reserved));
133 	dprintk(3, "[%s]\n", ctx->name);
134 }
135 
136 static int _fill_vb2_buffer(struct vb2_buffer *vb,
137 			    const void *pb, struct vb2_plane *planes)
138 {
139 	struct dvb_vb2_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
140 
141 	planes[0].bytesused = 0;
142 	dprintk(3, "[%s]\n", ctx->name);
143 
144 	return 0;
145 }
146 
147 static const struct vb2_buf_ops dvb_vb2_buf_ops = {
148 	.fill_user_buffer	= _fill_dmx_buffer,
149 	.fill_vb2_buffer	= _fill_vb2_buffer,
150 };
151 
152 /*
153  * Videobuf operations
154  */
155 int dvb_vb2_init(struct dvb_vb2_ctx *ctx, const char *name, int nonblocking)
156 {
157 	struct vb2_queue *q = &ctx->vb_q;
158 	int ret;
159 
160 	memset(ctx, 0, sizeof(struct dvb_vb2_ctx));
161 	q->type = DVB_BUF_TYPE_CAPTURE;
162 	/**capture type*/
163 	q->is_output = 0;
164 	/**only mmap is supported currently*/
165 	q->io_modes = VB2_MMAP;
166 	q->drv_priv = ctx;
167 	q->buf_struct_size = sizeof(struct dvb_buffer);
168 	q->min_buffers_needed = 1;
169 	q->ops = &dvb_vb2_qops;
170 	q->mem_ops = &vb2_vmalloc_memops;
171 	q->buf_ops = &dvb_vb2_buf_ops;
172 	q->num_buffers = 0;
173 	ret = vb2_core_queue_init(q);
174 	if (ret) {
175 		ctx->state = DVB_VB2_STATE_NONE;
176 		dprintk(1, "[%s] errno=%d\n", ctx->name, ret);
177 		return ret;
178 	}
179 
180 	mutex_init(&ctx->mutex);
181 	spin_lock_init(&ctx->slock);
182 	INIT_LIST_HEAD(&ctx->dvb_q);
183 
184 	strncpy(ctx->name, name, DVB_VB2_NAME_MAX);
185 	ctx->nonblocking = nonblocking;
186 	ctx->state = DVB_VB2_STATE_INIT;
187 
188 	dprintk(3, "[%s]\n", ctx->name);
189 
190 	return 0;
191 }
192 
193 int dvb_vb2_release(struct dvb_vb2_ctx *ctx)
194 {
195 	struct vb2_queue *q = (struct vb2_queue *)&ctx->vb_q;
196 
197 	if (ctx->state & DVB_VB2_STATE_INIT)
198 		vb2_core_queue_release(q);
199 
200 	ctx->state = DVB_VB2_STATE_NONE;
201 	dprintk(3, "[%s]\n", ctx->name);
202 
203 	return 0;
204 }
205 
206 int dvb_vb2_stream_on(struct dvb_vb2_ctx *ctx)
207 {
208 	struct vb2_queue *q = &ctx->vb_q;
209 	int ret;
210 
211 	ret = vb2_core_streamon(q, q->type);
212 	if (ret) {
213 		ctx->state = DVB_VB2_STATE_NONE;
214 		dprintk(1, "[%s] errno=%d\n", ctx->name, ret);
215 		return ret;
216 	}
217 	ctx->state |= DVB_VB2_STATE_STREAMON;
218 	dprintk(3, "[%s]\n", ctx->name);
219 
220 	return 0;
221 }
222 
223 int dvb_vb2_stream_off(struct dvb_vb2_ctx *ctx)
224 {
225 	struct vb2_queue *q = (struct vb2_queue *)&ctx->vb_q;
226 	int ret;
227 	unsigned long flags = 0;
228 
229 	ctx->state &= ~DVB_VB2_STATE_STREAMON;
230 	spin_lock_irqsave(&ctx->slock, flags);
231 	while (!list_empty(&ctx->dvb_q)) {
232 		struct dvb_buffer       *buf;
233 
234 		buf = list_entry(ctx->dvb_q.next,
235 				 struct dvb_buffer, list);
236 		list_del(&buf->list);
237 		spin_unlock_irqrestore(&ctx->slock, flags);
238 		vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
239 		spin_lock_irqsave(&ctx->slock, flags);
240 	}
241 	spin_unlock_irqrestore(&ctx->slock, flags);
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 	while (todo) {
276 		if (!ctx->buf) {
277 			spin_lock_irqsave(&ctx->slock, flags);
278 			if (list_empty(&ctx->dvb_q)) {
279 				spin_unlock_irqrestore(&ctx->slock, flags);
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 			list_del(&ctx->buf->list);
288 			spin_unlock_irqrestore(&ctx->slock, flags);
289 			ctx->remain = vb2_plane_size(&ctx->buf->vb, 0);
290 			ctx->offset = 0;
291 		}
292 
293 		if (!dvb_vb2_is_streaming(ctx)) {
294 			vb2_buffer_done(&ctx->buf->vb, VB2_BUF_STATE_ERROR);
295 			ctx->buf = NULL;
296 			break;
297 		}
298 
299 		/* Fill buffer */
300 		ll = min(todo, ctx->remain);
301 		vbuf = vb2_plane_vaddr(&ctx->buf->vb, 0);
302 		memcpy(vbuf + ctx->offset, psrc, ll);
303 		todo -= ll;
304 		psrc += ll;
305 
306 		ctx->remain -= ll;
307 		ctx->offset += ll;
308 
309 		if (ctx->remain == 0) {
310 			vb2_buffer_done(&ctx->buf->vb, VB2_BUF_STATE_DONE);
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 		ctx->buf = NULL;
319 	}
320 
321 	if (todo)
322 		dprintk(1, "[%s] %d bytes are dropped.\n", ctx->name, todo);
323 	else
324 		dprintk(3, "[%s]\n", ctx->name);
325 
326 	dprintk(3, "[%s] %d bytes are copied\n", ctx->name, len - todo);
327 	return (len - todo);
328 }
329 
330 int dvb_vb2_reqbufs(struct dvb_vb2_ctx *ctx, struct dmx_requestbuffers *req)
331 {
332 	int ret;
333 
334 	ctx->buf_siz = req->size;
335 	ctx->buf_cnt = req->count;
336 	ret = vb2_core_reqbufs(&ctx->vb_q, VB2_MEMORY_MMAP, &req->count);
337 	if (ret) {
338 		ctx->state = DVB_VB2_STATE_NONE;
339 		dprintk(1, "[%s] count=%d size=%d errno=%d\n", ctx->name,
340 			ctx->buf_cnt, ctx->buf_siz, ret);
341 		return ret;
342 	}
343 	ctx->state |= DVB_VB2_STATE_REQBUFS;
344 	dprintk(3, "[%s] count=%d size=%d\n", ctx->name,
345 		ctx->buf_cnt, ctx->buf_siz);
346 
347 	return 0;
348 }
349 
350 int dvb_vb2_querybuf(struct dvb_vb2_ctx *ctx, struct dmx_buffer *b)
351 {
352 	vb2_core_querybuf(&ctx->vb_q, b->index, b);
353 	dprintk(3, "[%s] index=%d\n", ctx->name, b->index);
354 	return 0;
355 }
356 
357 int dvb_vb2_expbuf(struct dvb_vb2_ctx *ctx, struct dmx_exportbuffer *exp)
358 {
359 	struct vb2_queue *q = &ctx->vb_q;
360 	int ret;
361 
362 	ret = vb2_core_expbuf(&ctx->vb_q, &exp->fd, q->type, exp->index,
363 			      0, exp->flags);
364 	if (ret) {
365 		dprintk(1, "[%s] index=%d errno=%d\n", ctx->name,
366 			exp->index, ret);
367 		return ret;
368 	}
369 	dprintk(3, "[%s] index=%d fd=%d\n", ctx->name, exp->index, exp->fd);
370 
371 	return 0;
372 }
373 
374 int dvb_vb2_qbuf(struct dvb_vb2_ctx *ctx, struct dmx_buffer *b)
375 {
376 	int ret;
377 
378 	ret = vb2_core_qbuf(&ctx->vb_q, b->index, b);
379 	if (ret) {
380 		dprintk(1, "[%s] index=%d errno=%d\n", ctx->name,
381 			b->index, ret);
382 		return ret;
383 	}
384 	dprintk(5, "[%s] index=%d\n", ctx->name, b->index);
385 
386 	return 0;
387 }
388 
389 int dvb_vb2_dqbuf(struct dvb_vb2_ctx *ctx, struct dmx_buffer *b)
390 {
391 	int ret;
392 
393 	ret = vb2_core_dqbuf(&ctx->vb_q, &b->index, b, ctx->nonblocking);
394 	if (ret) {
395 		dprintk(1, "[%s] errno=%d\n", ctx->name, ret);
396 		return ret;
397 	}
398 	dprintk(5, "[%s] index=%d\n", ctx->name, b->index);
399 
400 	return 0;
401 }
402 
403 int dvb_vb2_mmap(struct dvb_vb2_ctx *ctx, struct vm_area_struct *vma)
404 {
405 	int ret;
406 
407 	ret = vb2_mmap(&ctx->vb_q, vma);
408 	if (ret) {
409 		dprintk(1, "[%s] errno=%d\n", ctx->name, ret);
410 		return ret;
411 	}
412 	dprintk(3, "[%s] ret=%d\n", ctx->name, ret);
413 
414 	return 0;
415 }
416 
417 unsigned int dvb_vb2_poll(struct dvb_vb2_ctx *ctx, struct file *file,
418 			  poll_table *wait)
419 {
420 	dprintk(3, "[%s]\n", ctx->name);
421 	return vb2_core_poll(&ctx->vb_q, file, wait);
422 }
423 
424