Linux Audio

Check our new training course

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