xref: /openbmc/linux/drivers/media/dvb-core/dvb_vb2.c (revision 7b6c96d5)
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 	struct dvb_buffer *buf;
93 	unsigned long flags = 0;
94 
95 	dprintk(3, "[%s]\n", ctx->name);
96 
97 	spin_lock_irqsave(&ctx->slock, flags);
98 	while (!list_empty(&ctx->dvb_q)) {
99 		buf = list_entry(ctx->dvb_q.next,
100 				 struct dvb_buffer, list);
101 		vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
102 		list_del(&buf->list);
103 	}
104 	spin_unlock_irqrestore(&ctx->slock, flags);
105 }
106 
107 static void _dmxdev_lock(struct vb2_queue *vq)
108 {
109 	struct dvb_vb2_ctx *ctx = vb2_get_drv_priv(vq);
110 
111 	mutex_lock(&ctx->mutex);
112 	dprintk(3, "[%s]\n", ctx->name);
113 }
114 
115 static void _dmxdev_unlock(struct vb2_queue *vq)
116 {
117 	struct dvb_vb2_ctx *ctx = vb2_get_drv_priv(vq);
118 
119 	if (mutex_is_locked(&ctx->mutex))
120 		mutex_unlock(&ctx->mutex);
121 	dprintk(3, "[%s]\n", ctx->name);
122 }
123 
124 static const struct vb2_ops dvb_vb2_qops = {
125 	.queue_setup		= _queue_setup,
126 	.buf_prepare		= _buffer_prepare,
127 	.buf_queue		= _buffer_queue,
128 	.start_streaming	= _start_streaming,
129 	.stop_streaming		= _stop_streaming,
130 	.wait_prepare		= _dmxdev_unlock,
131 	.wait_finish		= _dmxdev_lock,
132 };
133 
134 static void _fill_dmx_buffer(struct vb2_buffer *vb, void *pb)
135 {
136 	struct dvb_vb2_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
137 	struct dmx_buffer *b = pb;
138 
139 	b->index = vb->index;
140 	b->length = vb->planes[0].length;
141 	b->bytesused = vb->planes[0].bytesused;
142 	b->offset = vb->planes[0].m.offset;
143 	memset(b->reserved, 0, sizeof(b->reserved));
144 	dprintk(3, "[%s]\n", ctx->name);
145 }
146 
147 static int _fill_vb2_buffer(struct vb2_buffer *vb,
148 			    const void *pb, struct vb2_plane *planes)
149 {
150 	struct dvb_vb2_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
151 
152 	planes[0].bytesused = 0;
153 	dprintk(3, "[%s]\n", ctx->name);
154 
155 	return 0;
156 }
157 
158 static const struct vb2_buf_ops dvb_vb2_buf_ops = {
159 	.fill_user_buffer	= _fill_dmx_buffer,
160 	.fill_vb2_buffer	= _fill_vb2_buffer,
161 };
162 
163 /*
164  * Videobuf operations
165  */
166 int dvb_vb2_init(struct dvb_vb2_ctx *ctx, const char *name, int nonblocking)
167 {
168 	struct vb2_queue *q = &ctx->vb_q;
169 	int ret;
170 
171 	memset(ctx, 0, sizeof(struct dvb_vb2_ctx));
172 	q->type = DVB_BUF_TYPE_CAPTURE;
173 	/**capture type*/
174 	q->is_output = 0;
175 	/**only mmap is supported currently*/
176 	q->io_modes = VB2_MMAP;
177 	q->drv_priv = ctx;
178 	q->buf_struct_size = sizeof(struct dvb_buffer);
179 	q->min_buffers_needed = 1;
180 	q->ops = &dvb_vb2_qops;
181 	q->mem_ops = &vb2_vmalloc_memops;
182 	q->buf_ops = &dvb_vb2_buf_ops;
183 	q->num_buffers = 0;
184 	ret = vb2_core_queue_init(q);
185 	if (ret) {
186 		ctx->state = DVB_VB2_STATE_NONE;
187 		dprintk(1, "[%s] errno=%d\n", ctx->name, ret);
188 		return ret;
189 	}
190 
191 	mutex_init(&ctx->mutex);
192 	spin_lock_init(&ctx->slock);
193 	INIT_LIST_HEAD(&ctx->dvb_q);
194 
195 	strncpy(ctx->name, name, DVB_VB2_NAME_MAX);
196 	ctx->nonblocking = nonblocking;
197 	ctx->state = DVB_VB2_STATE_INIT;
198 
199 	dprintk(3, "[%s]\n", ctx->name);
200 
201 	return 0;
202 }
203 
204 int dvb_vb2_release(struct dvb_vb2_ctx *ctx)
205 {
206 	struct vb2_queue *q = (struct vb2_queue *)&ctx->vb_q;
207 
208 	if (ctx->state & DVB_VB2_STATE_INIT)
209 		vb2_core_queue_release(q);
210 
211 	ctx->state = DVB_VB2_STATE_NONE;
212 	dprintk(3, "[%s]\n", ctx->name);
213 
214 	return 0;
215 }
216 
217 int dvb_vb2_stream_on(struct dvb_vb2_ctx *ctx)
218 {
219 	struct vb2_queue *q = &ctx->vb_q;
220 	int ret;
221 
222 	ret = vb2_core_streamon(q, q->type);
223 	if (ret) {
224 		ctx->state = DVB_VB2_STATE_NONE;
225 		dprintk(1, "[%s] errno=%d\n", ctx->name, ret);
226 		return ret;
227 	}
228 	ctx->state |= DVB_VB2_STATE_STREAMON;
229 	dprintk(3, "[%s]\n", ctx->name);
230 
231 	return 0;
232 }
233 
234 int dvb_vb2_stream_off(struct dvb_vb2_ctx *ctx)
235 {
236 	struct vb2_queue *q = (struct vb2_queue *)&ctx->vb_q;
237 	int ret;
238 
239 	ctx->state &= ~DVB_VB2_STATE_STREAMON;
240 	ret = vb2_core_streamoff(q, q->type);
241 	if (ret) {
242 		ctx->state = DVB_VB2_STATE_NONE;
243 		dprintk(1, "[%s] errno=%d\n", ctx->name, ret);
244 		return ret;
245 	}
246 	dprintk(3, "[%s]\n", ctx->name);
247 
248 	return 0;
249 }
250 
251 int dvb_vb2_is_streaming(struct dvb_vb2_ctx *ctx)
252 {
253 	return (ctx->state & DVB_VB2_STATE_STREAMON);
254 }
255 
256 int dvb_vb2_fill_buffer(struct dvb_vb2_ctx *ctx,
257 			const unsigned char *src, int len)
258 {
259 	unsigned long flags = 0;
260 	void *vbuf = NULL;
261 	int todo = len;
262 	unsigned char *psrc = (unsigned char *)src;
263 	int ll = 0;
264 
265 	dprintk(3, "[%s] %d bytes are rcvd\n", ctx->name, len);
266 	if (!src) {
267 		dprintk(3, "[%s]:NULL pointer src\n", ctx->name);
268 		/**normal case: This func is called twice from demux driver
269 		 * once with valid src pointer, second time with NULL pointer
270 		 */
271 		return 0;
272 	}
273 	spin_lock_irqsave(&ctx->slock, flags);
274 	while (todo) {
275 		if (!ctx->buf) {
276 			if (list_empty(&ctx->dvb_q)) {
277 				dprintk(3, "[%s] Buffer overflow!!!\n",
278 					ctx->name);
279 				break;
280 			}
281 
282 			ctx->buf = list_entry(ctx->dvb_q.next,
283 					      struct dvb_buffer, list);
284 			ctx->remain = vb2_plane_size(&ctx->buf->vb, 0);
285 			ctx->offset = 0;
286 		}
287 
288 		if (!dvb_vb2_is_streaming(ctx)) {
289 			vb2_buffer_done(&ctx->buf->vb, VB2_BUF_STATE_ERROR);
290 			list_del(&ctx->buf->list);
291 			ctx->buf = NULL;
292 			break;
293 		}
294 
295 		/* Fill buffer */
296 		ll = min(todo, ctx->remain);
297 		vbuf = vb2_plane_vaddr(&ctx->buf->vb, 0);
298 		memcpy(vbuf + ctx->offset, psrc, ll);
299 		todo -= ll;
300 		psrc += ll;
301 
302 		ctx->remain -= ll;
303 		ctx->offset += ll;
304 
305 		if (ctx->remain == 0) {
306 			vb2_buffer_done(&ctx->buf->vb, VB2_BUF_STATE_DONE);
307 			list_del(&ctx->buf->list);
308 			ctx->buf = NULL;
309 		}
310 	}
311 
312 	if (ctx->nonblocking && ctx->buf) {
313 		vb2_set_plane_payload(&ctx->buf->vb, 0, ll);
314 		vb2_buffer_done(&ctx->buf->vb, VB2_BUF_STATE_DONE);
315 		list_del(&ctx->buf->list);
316 		ctx->buf = NULL;
317 	}
318 	spin_unlock_irqrestore(&ctx->slock, flags);
319 
320 	if (todo)
321 		dprintk(1, "[%s] %d bytes are dropped.\n", ctx->name, todo);
322 	else
323 		dprintk(3, "[%s]\n", ctx->name);
324 
325 	dprintk(3, "[%s] %d bytes are copied\n", ctx->name, len - todo);
326 	return (len - todo);
327 }
328 
329 int dvb_vb2_reqbufs(struct dvb_vb2_ctx *ctx, struct dmx_requestbuffers *req)
330 {
331 	int ret;
332 
333 	ctx->buf_siz = req->size;
334 	ctx->buf_cnt = req->count;
335 	ret = vb2_core_reqbufs(&ctx->vb_q, VB2_MEMORY_MMAP, &req->count);
336 	if (ret) {
337 		ctx->state = DVB_VB2_STATE_NONE;
338 		dprintk(1, "[%s] count=%d size=%d errno=%d\n", ctx->name,
339 			ctx->buf_cnt, ctx->buf_siz, ret);
340 		return ret;
341 	}
342 	ctx->state |= DVB_VB2_STATE_REQBUFS;
343 	dprintk(3, "[%s] count=%d size=%d\n", ctx->name,
344 		ctx->buf_cnt, ctx->buf_siz);
345 
346 	return 0;
347 }
348 
349 int dvb_vb2_querybuf(struct dvb_vb2_ctx *ctx, struct dmx_buffer *b)
350 {
351 	vb2_core_querybuf(&ctx->vb_q, b->index, b);
352 	dprintk(3, "[%s] index=%d\n", ctx->name, b->index);
353 	return 0;
354 }
355 
356 int dvb_vb2_expbuf(struct dvb_vb2_ctx *ctx, struct dmx_exportbuffer *exp)
357 {
358 	struct vb2_queue *q = &ctx->vb_q;
359 	int ret;
360 
361 	ret = vb2_core_expbuf(&ctx->vb_q, &exp->fd, q->type, exp->index,
362 			      0, exp->flags);
363 	if (ret) {
364 		dprintk(1, "[%s] index=%d errno=%d\n", ctx->name,
365 			exp->index, ret);
366 		return ret;
367 	}
368 	dprintk(3, "[%s] index=%d fd=%d\n", ctx->name, exp->index, exp->fd);
369 
370 	return 0;
371 }
372 
373 int dvb_vb2_qbuf(struct dvb_vb2_ctx *ctx, struct dmx_buffer *b)
374 {
375 	int ret;
376 
377 	ret = vb2_core_qbuf(&ctx->vb_q, b->index, b);
378 	if (ret) {
379 		dprintk(1, "[%s] index=%d errno=%d\n", ctx->name,
380 			b->index, ret);
381 		return ret;
382 	}
383 	dprintk(5, "[%s] index=%d\n", ctx->name, b->index);
384 
385 	return 0;
386 }
387 
388 int dvb_vb2_dqbuf(struct dvb_vb2_ctx *ctx, struct dmx_buffer *b)
389 {
390 	int ret;
391 
392 	ret = vb2_core_dqbuf(&ctx->vb_q, &b->index, b, ctx->nonblocking);
393 	if (ret) {
394 		dprintk(1, "[%s] errno=%d\n", ctx->name, ret);
395 		return ret;
396 	}
397 	dprintk(5, "[%s] index=%d\n", ctx->name, b->index);
398 
399 	return 0;
400 }
401 
402 int dvb_vb2_mmap(struct dvb_vb2_ctx *ctx, struct vm_area_struct *vma)
403 {
404 	int ret;
405 
406 	ret = vb2_mmap(&ctx->vb_q, vma);
407 	if (ret) {
408 		dprintk(1, "[%s] errno=%d\n", ctx->name, ret);
409 		return ret;
410 	}
411 	dprintk(3, "[%s] ret=%d\n", ctx->name, ret);
412 
413 	return 0;
414 }
415 
416 unsigned int dvb_vb2_poll(struct dvb_vb2_ctx *ctx, struct file *file,
417 			  poll_table *wait)
418 {
419 	dprintk(3, "[%s]\n", ctx->name);
420 	return vb2_core_poll(&ctx->vb_q, file, wait);
421 }
422 
423