1 // SPDX-License-Identifier: GPL-2.0
5 * Copyright (C) 2015 Samsung Electronics
7 * Author: jh1009.sung@samsung.com
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.
14 #include <linux/err.h>
15 #include <linux/kernel.h>
16 #include <linux/module.h>
19 #include <media/dvbdev.h>
20 #include <media/dvb_vb2.h>
22 #define DVB_V2_MAX_SIZE (4096 * 188)
25 module_param(vb2_debug
, int, 0644);
27 #define dprintk(level, fmt, arg...) \
29 if (vb2_debug >= level) \
30 pr_info("vb2: %s: " fmt, __func__, ## arg); \
33 static int _queue_setup(struct vb2_queue
*vq
,
34 unsigned int *nbuffers
, unsigned int *nplanes
,
35 unsigned int sizes
[], struct device
*alloc_devs
[])
37 struct dvb_vb2_ctx
*ctx
= vb2_get_drv_priv(vq
);
39 ctx
->buf_cnt
= *nbuffers
;
41 sizes
[0] = ctx
->buf_siz
;
44 * videobuf2-vmalloc allocator is context-less so no need to set
48 dprintk(3, "[%s] count=%d, size=%d\n", ctx
->name
,
54 static int _buffer_prepare(struct vb2_buffer
*vb
)
56 struct dvb_vb2_ctx
*ctx
= vb2_get_drv_priv(vb
->vb2_queue
);
57 unsigned long size
= ctx
->buf_siz
;
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
);
65 vb2_set_plane_payload(vb
, 0, size
);
66 dprintk(3, "[%s]\n", ctx
->name
);
71 static void _buffer_queue(struct vb2_buffer
*vb
)
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;
77 spin_lock_irqsave(&ctx
->slock
, flags
);
78 list_add_tail(&buf
->list
, &ctx
->dvb_q
);
79 spin_unlock_irqrestore(&ctx
->slock
, flags
);
81 dprintk(3, "[%s]\n", ctx
->name
);
84 static int _start_streaming(struct vb2_queue
*vq
, unsigned int count
)
86 struct dvb_vb2_ctx
*ctx
= vb2_get_drv_priv(vq
);
88 dprintk(3, "[%s] count=%d\n", ctx
->name
, count
);
92 static void _stop_streaming(struct vb2_queue
*vq
)
94 struct dvb_vb2_ctx
*ctx
= vb2_get_drv_priv(vq
);
95 struct dvb_buffer
*buf
;
96 unsigned long flags
= 0;
98 dprintk(3, "[%s]\n", ctx
->name
);
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
);
107 spin_unlock_irqrestore(&ctx
->slock
, flags
);
110 static void _dmxdev_lock(struct vb2_queue
*vq
)
112 struct dvb_vb2_ctx
*ctx
= vb2_get_drv_priv(vq
);
114 mutex_lock(&ctx
->mutex
);
115 dprintk(3, "[%s]\n", ctx
->name
);
118 static void _dmxdev_unlock(struct vb2_queue
*vq
)
120 struct dvb_vb2_ctx
*ctx
= vb2_get_drv_priv(vq
);
122 if (mutex_is_locked(&ctx
->mutex
))
123 mutex_unlock(&ctx
->mutex
);
124 dprintk(3, "[%s]\n", ctx
->name
);
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
,
137 static void _fill_dmx_buffer(struct vb2_buffer
*vb
, void *pb
)
139 struct dvb_vb2_ctx
*ctx
= vb2_get_drv_priv(vb
->vb2_queue
);
140 struct dmx_buffer
*b
= pb
;
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
);
149 static int _fill_vb2_buffer(struct vb2_buffer
*vb
, struct vb2_plane
*planes
)
151 struct dvb_vb2_ctx
*ctx
= vb2_get_drv_priv(vb
->vb2_queue
);
153 planes
[0].bytesused
= 0;
154 dprintk(3, "[%s]\n", ctx
->name
);
159 static const struct vb2_buf_ops dvb_vb2_buf_ops
= {
160 .fill_user_buffer
= _fill_dmx_buffer
,
161 .fill_vb2_buffer
= _fill_vb2_buffer
,
165 * Videobuf operations
167 int dvb_vb2_init(struct dvb_vb2_ctx
*ctx
, const char *name
, int nonblocking
)
169 struct vb2_queue
*q
= &ctx
->vb_q
;
172 memset(ctx
, 0, sizeof(struct dvb_vb2_ctx
));
173 q
->type
= DVB_BUF_TYPE_CAPTURE
;
176 /**only mmap is supported currently*/
177 q
->io_modes
= VB2_MMAP
;
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
;
185 ret
= vb2_core_queue_init(q
);
187 ctx
->state
= DVB_VB2_STATE_NONE
;
188 dprintk(1, "[%s] errno=%d\n", ctx
->name
, ret
);
192 mutex_init(&ctx
->mutex
);
193 spin_lock_init(&ctx
->slock
);
194 INIT_LIST_HEAD(&ctx
->dvb_q
);
196 strscpy(ctx
->name
, name
, DVB_VB2_NAME_MAX
);
197 ctx
->nonblocking
= nonblocking
;
198 ctx
->state
= DVB_VB2_STATE_INIT
;
200 dprintk(3, "[%s]\n", ctx
->name
);
205 int dvb_vb2_release(struct dvb_vb2_ctx
*ctx
)
207 struct vb2_queue
*q
= (struct vb2_queue
*)&ctx
->vb_q
;
209 if (ctx
->state
& DVB_VB2_STATE_INIT
)
210 vb2_core_queue_release(q
);
212 ctx
->state
= DVB_VB2_STATE_NONE
;
213 dprintk(3, "[%s]\n", ctx
->name
);
218 int dvb_vb2_stream_on(struct dvb_vb2_ctx
*ctx
)
220 struct vb2_queue
*q
= &ctx
->vb_q
;
223 ret
= vb2_core_streamon(q
, q
->type
);
225 ctx
->state
= DVB_VB2_STATE_NONE
;
226 dprintk(1, "[%s] errno=%d\n", ctx
->name
, ret
);
229 ctx
->state
|= DVB_VB2_STATE_STREAMON
;
230 dprintk(3, "[%s]\n", ctx
->name
);
235 int dvb_vb2_stream_off(struct dvb_vb2_ctx
*ctx
)
237 struct vb2_queue
*q
= (struct vb2_queue
*)&ctx
->vb_q
;
240 ctx
->state
&= ~DVB_VB2_STATE_STREAMON
;
241 ret
= vb2_core_streamoff(q
, q
->type
);
243 ctx
->state
= DVB_VB2_STATE_NONE
;
244 dprintk(1, "[%s] errno=%d\n", ctx
->name
, ret
);
247 dprintk(3, "[%s]\n", ctx
->name
);
252 int dvb_vb2_is_streaming(struct dvb_vb2_ctx
*ctx
)
254 return (ctx
->state
& DVB_VB2_STATE_STREAMON
);
257 int dvb_vb2_fill_buffer(struct dvb_vb2_ctx
*ctx
,
258 const unsigned char *src
, int len
,
259 enum dmx_buffer_flags
*buffer_flags
)
261 unsigned long flags
= 0;
264 unsigned char *psrc
= (unsigned char *)src
;
268 * normal case: This func is called twice from demux driver
269 * one with valid src pointer, second time with NULL pointer
273 spin_lock_irqsave(&ctx
->slock
, flags
);
274 if (buffer_flags
&& *buffer_flags
) {
275 ctx
->flags
|= *buffer_flags
;
280 if (list_empty(&ctx
->dvb_q
)) {
281 dprintk(3, "[%s] Buffer overflow!!!\n",
286 ctx
->buf
= list_entry(ctx
->dvb_q
.next
,
287 struct dvb_buffer
, list
);
288 ctx
->remain
= vb2_plane_size(&ctx
->buf
->vb
, 0);
292 if (!dvb_vb2_is_streaming(ctx
)) {
293 vb2_buffer_done(&ctx
->buf
->vb
, VB2_BUF_STATE_ERROR
);
294 list_del(&ctx
->buf
->list
);
300 ll
= min(todo
, ctx
->remain
);
301 vbuf
= vb2_plane_vaddr(&ctx
->buf
->vb
, 0);
302 memcpy(vbuf
+ ctx
->offset
, psrc
, ll
);
309 if (ctx
->remain
== 0) {
310 vb2_buffer_done(&ctx
->buf
->vb
, VB2_BUF_STATE_DONE
);
311 list_del(&ctx
->buf
->list
);
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
);
322 spin_unlock_irqrestore(&ctx
->slock
, flags
);
325 dprintk(1, "[%s] %d bytes are dropped.\n", ctx
->name
, todo
);
327 dprintk(3, "[%s]\n", ctx
->name
);
329 dprintk(3, "[%s] %d bytes are copied\n", ctx
->name
, len
- todo
);
333 int dvb_vb2_reqbufs(struct dvb_vb2_ctx
*ctx
, struct dmx_requestbuffers
*req
)
337 /* Adjust size to a sane value */
338 if (req
->size
> DVB_V2_MAX_SIZE
)
339 req
->size
= DVB_V2_MAX_SIZE
;
341 /* FIXME: round req->size to a 188 or 204 multiple */
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
);
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
);
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
);
359 int dvb_vb2_querybuf(struct dvb_vb2_ctx
*ctx
, struct dmx_buffer
*b
)
361 vb2_core_querybuf(&ctx
->vb_q
, b
->index
, b
);
362 dprintk(3, "[%s] index=%d\n", ctx
->name
, b
->index
);
366 int dvb_vb2_expbuf(struct dvb_vb2_ctx
*ctx
, struct dmx_exportbuffer
*exp
)
368 struct vb2_queue
*q
= &ctx
->vb_q
;
371 ret
= vb2_core_expbuf(&ctx
->vb_q
, &exp
->fd
, q
->type
, exp
->index
,
374 dprintk(1, "[%s] index=%d errno=%d\n", ctx
->name
,
378 dprintk(3, "[%s] index=%d fd=%d\n", ctx
->name
, exp
->index
, exp
->fd
);
383 int dvb_vb2_qbuf(struct dvb_vb2_ctx
*ctx
, struct dmx_buffer
*b
)
387 ret
= vb2_core_qbuf(&ctx
->vb_q
, b
->index
, b
, NULL
);
389 dprintk(1, "[%s] index=%d errno=%d\n", ctx
->name
,
393 dprintk(5, "[%s] index=%d\n", ctx
->name
, b
->index
);
398 int dvb_vb2_dqbuf(struct dvb_vb2_ctx
*ctx
, struct dmx_buffer
*b
)
403 ret
= vb2_core_dqbuf(&ctx
->vb_q
, &b
->index
, b
, ctx
->nonblocking
);
405 dprintk(1, "[%s] errno=%d\n", ctx
->name
, ret
);
409 spin_lock_irqsave(&ctx
->slock
, flags
);
410 b
->count
= ctx
->count
++;
411 b
->flags
= ctx
->flags
;
413 spin_unlock_irqrestore(&ctx
->slock
, flags
);
415 dprintk(5, "[%s] index=%d, count=%d, flags=%d\n",
416 ctx
->name
, b
->index
, ctx
->count
, b
->flags
);
422 int dvb_vb2_mmap(struct dvb_vb2_ctx
*ctx
, struct vm_area_struct
*vma
)
426 ret
= vb2_mmap(&ctx
->vb_q
, vma
);
428 dprintk(1, "[%s] errno=%d\n", ctx
->name
, ret
);
431 dprintk(3, "[%s] ret=%d\n", ctx
->name
, ret
);
436 __poll_t
dvb_vb2_poll(struct dvb_vb2_ctx
*ctx
, struct file
*file
,
439 dprintk(3, "[%s]\n", ctx
->name
);
440 return vb2_core_poll(&ctx
->vb_q
, file
, wait
);