Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * dvb-vb2.c - dvb-vb2
0004  *
0005  * Copyright (C) 2015 Samsung Electronics
0006  *
0007  * Author: jh1009.sung@samsung.com
0008  */
0009 
0010 #include <linux/err.h>
0011 #include <linux/kernel.h>
0012 #include <linux/module.h>
0013 #include <linux/mm.h>
0014 
0015 #include <media/dvbdev.h>
0016 #include <media/dvb_vb2.h>
0017 
0018 #define DVB_V2_MAX_SIZE     (4096 * 188)
0019 
0020 static int vb2_debug;
0021 module_param(vb2_debug, int, 0644);
0022 
0023 #define dprintk(level, fmt, arg...)                       \
0024     do {                                      \
0025         if (vb2_debug >= level)                       \
0026             pr_info("vb2: %s: " fmt, __func__, ## arg); \
0027     } while (0)
0028 
0029 static int _queue_setup(struct vb2_queue *vq,
0030             unsigned int *nbuffers, unsigned int *nplanes,
0031             unsigned int sizes[], struct device *alloc_devs[])
0032 {
0033     struct dvb_vb2_ctx *ctx = vb2_get_drv_priv(vq);
0034 
0035     ctx->buf_cnt = *nbuffers;
0036     *nplanes = 1;
0037     sizes[0] = ctx->buf_siz;
0038 
0039     /*
0040      * videobuf2-vmalloc allocator is context-less so no need to set
0041      * alloc_ctxs array.
0042      */
0043 
0044     dprintk(3, "[%s] count=%d, size=%d\n", ctx->name,
0045         *nbuffers, sizes[0]);
0046 
0047     return 0;
0048 }
0049 
0050 static int _buffer_prepare(struct vb2_buffer *vb)
0051 {
0052     struct dvb_vb2_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
0053     unsigned long size = ctx->buf_siz;
0054 
0055     if (vb2_plane_size(vb, 0) < size) {
0056         dprintk(1, "[%s] data will not fit into plane (%lu < %lu)\n",
0057             ctx->name, vb2_plane_size(vb, 0), size);
0058         return -EINVAL;
0059     }
0060 
0061     vb2_set_plane_payload(vb, 0, size);
0062     dprintk(3, "[%s]\n", ctx->name);
0063 
0064     return 0;
0065 }
0066 
0067 static void _buffer_queue(struct vb2_buffer *vb)
0068 {
0069     struct dvb_vb2_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
0070     struct dvb_buffer *buf = container_of(vb, struct dvb_buffer, vb);
0071     unsigned long flags = 0;
0072 
0073     spin_lock_irqsave(&ctx->slock, flags);
0074     list_add_tail(&buf->list, &ctx->dvb_q);
0075     spin_unlock_irqrestore(&ctx->slock, flags);
0076 
0077     dprintk(3, "[%s]\n", ctx->name);
0078 }
0079 
0080 static int _start_streaming(struct vb2_queue *vq, unsigned int count)
0081 {
0082     struct dvb_vb2_ctx *ctx = vb2_get_drv_priv(vq);
0083 
0084     dprintk(3, "[%s] count=%d\n", ctx->name, count);
0085     return 0;
0086 }
0087 
0088 static void _stop_streaming(struct vb2_queue *vq)
0089 {
0090     struct dvb_vb2_ctx *ctx = vb2_get_drv_priv(vq);
0091     struct dvb_buffer *buf;
0092     unsigned long flags = 0;
0093 
0094     dprintk(3, "[%s]\n", ctx->name);
0095 
0096     spin_lock_irqsave(&ctx->slock, flags);
0097     while (!list_empty(&ctx->dvb_q)) {
0098         buf = list_entry(ctx->dvb_q.next,
0099                  struct dvb_buffer, list);
0100         vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
0101         list_del(&buf->list);
0102     }
0103     spin_unlock_irqrestore(&ctx->slock, flags);
0104 }
0105 
0106 static void _dmxdev_lock(struct vb2_queue *vq)
0107 {
0108     struct dvb_vb2_ctx *ctx = vb2_get_drv_priv(vq);
0109 
0110     mutex_lock(&ctx->mutex);
0111     dprintk(3, "[%s]\n", ctx->name);
0112 }
0113 
0114 static void _dmxdev_unlock(struct vb2_queue *vq)
0115 {
0116     struct dvb_vb2_ctx *ctx = vb2_get_drv_priv(vq);
0117 
0118     if (mutex_is_locked(&ctx->mutex))
0119         mutex_unlock(&ctx->mutex);
0120     dprintk(3, "[%s]\n", ctx->name);
0121 }
0122 
0123 static const struct vb2_ops dvb_vb2_qops = {
0124     .queue_setup        = _queue_setup,
0125     .buf_prepare        = _buffer_prepare,
0126     .buf_queue      = _buffer_queue,
0127     .start_streaming    = _start_streaming,
0128     .stop_streaming     = _stop_streaming,
0129     .wait_prepare       = _dmxdev_unlock,
0130     .wait_finish        = _dmxdev_lock,
0131 };
0132 
0133 static void _fill_dmx_buffer(struct vb2_buffer *vb, void *pb)
0134 {
0135     struct dvb_vb2_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
0136     struct dmx_buffer *b = pb;
0137 
0138     b->index = vb->index;
0139     b->length = vb->planes[0].length;
0140     b->bytesused = vb->planes[0].bytesused;
0141     b->offset = vb->planes[0].m.offset;
0142     dprintk(3, "[%s]\n", ctx->name);
0143 }
0144 
0145 static int _fill_vb2_buffer(struct vb2_buffer *vb, struct vb2_plane *planes)
0146 {
0147     struct dvb_vb2_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
0148 
0149     planes[0].bytesused = 0;
0150     dprintk(3, "[%s]\n", ctx->name);
0151 
0152     return 0;
0153 }
0154 
0155 static const struct vb2_buf_ops dvb_vb2_buf_ops = {
0156     .fill_user_buffer   = _fill_dmx_buffer,
0157     .fill_vb2_buffer    = _fill_vb2_buffer,
0158 };
0159 
0160 /*
0161  * Videobuf operations
0162  */
0163 int dvb_vb2_init(struct dvb_vb2_ctx *ctx, const char *name, int nonblocking)
0164 {
0165     struct vb2_queue *q = &ctx->vb_q;
0166     int ret;
0167 
0168     memset(ctx, 0, sizeof(struct dvb_vb2_ctx));
0169     q->type = DVB_BUF_TYPE_CAPTURE;
0170     /**capture type*/
0171     q->is_output = 0;
0172     /**only mmap is supported currently*/
0173     q->io_modes = VB2_MMAP;
0174     q->drv_priv = ctx;
0175     q->buf_struct_size = sizeof(struct dvb_buffer);
0176     q->min_buffers_needed = 1;
0177     q->ops = &dvb_vb2_qops;
0178     q->mem_ops = &vb2_vmalloc_memops;
0179     q->buf_ops = &dvb_vb2_buf_ops;
0180     q->num_buffers = 0;
0181     ret = vb2_core_queue_init(q);
0182     if (ret) {
0183         ctx->state = DVB_VB2_STATE_NONE;
0184         dprintk(1, "[%s] errno=%d\n", ctx->name, ret);
0185         return ret;
0186     }
0187 
0188     mutex_init(&ctx->mutex);
0189     spin_lock_init(&ctx->slock);
0190     INIT_LIST_HEAD(&ctx->dvb_q);
0191 
0192     strscpy(ctx->name, name, DVB_VB2_NAME_MAX);
0193     ctx->nonblocking = nonblocking;
0194     ctx->state = DVB_VB2_STATE_INIT;
0195 
0196     dprintk(3, "[%s]\n", ctx->name);
0197 
0198     return 0;
0199 }
0200 
0201 int dvb_vb2_release(struct dvb_vb2_ctx *ctx)
0202 {
0203     struct vb2_queue *q = (struct vb2_queue *)&ctx->vb_q;
0204 
0205     if (ctx->state & DVB_VB2_STATE_INIT)
0206         vb2_core_queue_release(q);
0207 
0208     ctx->state = DVB_VB2_STATE_NONE;
0209     dprintk(3, "[%s]\n", ctx->name);
0210 
0211     return 0;
0212 }
0213 
0214 int dvb_vb2_stream_on(struct dvb_vb2_ctx *ctx)
0215 {
0216     struct vb2_queue *q = &ctx->vb_q;
0217     int ret;
0218 
0219     ret = vb2_core_streamon(q, q->type);
0220     if (ret) {
0221         ctx->state = DVB_VB2_STATE_NONE;
0222         dprintk(1, "[%s] errno=%d\n", ctx->name, ret);
0223         return ret;
0224     }
0225     ctx->state |= DVB_VB2_STATE_STREAMON;
0226     dprintk(3, "[%s]\n", ctx->name);
0227 
0228     return 0;
0229 }
0230 
0231 int dvb_vb2_stream_off(struct dvb_vb2_ctx *ctx)
0232 {
0233     struct vb2_queue *q = (struct vb2_queue *)&ctx->vb_q;
0234     int ret;
0235 
0236     ctx->state &= ~DVB_VB2_STATE_STREAMON;
0237     ret = vb2_core_streamoff(q, q->type);
0238     if (ret) {
0239         ctx->state = DVB_VB2_STATE_NONE;
0240         dprintk(1, "[%s] errno=%d\n", ctx->name, ret);
0241         return ret;
0242     }
0243     dprintk(3, "[%s]\n", ctx->name);
0244 
0245     return 0;
0246 }
0247 
0248 int dvb_vb2_is_streaming(struct dvb_vb2_ctx *ctx)
0249 {
0250     return (ctx->state & DVB_VB2_STATE_STREAMON);
0251 }
0252 
0253 int dvb_vb2_fill_buffer(struct dvb_vb2_ctx *ctx,
0254             const unsigned char *src, int len,
0255             enum dmx_buffer_flags *buffer_flags)
0256 {
0257     unsigned long flags = 0;
0258     void *vbuf = NULL;
0259     int todo = len;
0260     unsigned char *psrc = (unsigned char *)src;
0261     int ll = 0;
0262 
0263     /*
0264      * normal case: This func is called twice from demux driver
0265      * one with valid src pointer, second time with NULL pointer
0266      */
0267     if (!src || !len)
0268         return 0;
0269     spin_lock_irqsave(&ctx->slock, flags);
0270     if (buffer_flags && *buffer_flags) {
0271         ctx->flags |= *buffer_flags;
0272         *buffer_flags = 0;
0273     }
0274     while (todo) {
0275         if (!ctx->buf) {
0276             if (list_empty(&ctx->dvb_q)) {
0277                 dprintk(3, "[%s] Buffer overflow!!!\n",
0278                     ctx->name);
0279                 break;
0280             }
0281 
0282             ctx->buf = list_entry(ctx->dvb_q.next,
0283                           struct dvb_buffer, list);
0284             ctx->remain = vb2_plane_size(&ctx->buf->vb, 0);
0285             ctx->offset = 0;
0286         }
0287 
0288         if (!dvb_vb2_is_streaming(ctx)) {
0289             vb2_buffer_done(&ctx->buf->vb, VB2_BUF_STATE_ERROR);
0290             list_del(&ctx->buf->list);
0291             ctx->buf = NULL;
0292             break;
0293         }
0294 
0295         /* Fill buffer */
0296         ll = min(todo, ctx->remain);
0297         vbuf = vb2_plane_vaddr(&ctx->buf->vb, 0);
0298         memcpy(vbuf + ctx->offset, psrc, ll);
0299         todo -= ll;
0300         psrc += ll;
0301 
0302         ctx->remain -= ll;
0303         ctx->offset += ll;
0304 
0305         if (ctx->remain == 0) {
0306             vb2_buffer_done(&ctx->buf->vb, VB2_BUF_STATE_DONE);
0307             list_del(&ctx->buf->list);
0308             ctx->buf = NULL;
0309         }
0310     }
0311 
0312     if (ctx->nonblocking && ctx->buf) {
0313         vb2_set_plane_payload(&ctx->buf->vb, 0, ll);
0314         vb2_buffer_done(&ctx->buf->vb, VB2_BUF_STATE_DONE);
0315         list_del(&ctx->buf->list);
0316         ctx->buf = NULL;
0317     }
0318     spin_unlock_irqrestore(&ctx->slock, flags);
0319 
0320     if (todo)
0321         dprintk(1, "[%s] %d bytes are dropped.\n", ctx->name, todo);
0322     else
0323         dprintk(3, "[%s]\n", ctx->name);
0324 
0325     dprintk(3, "[%s] %d bytes are copied\n", ctx->name, len - todo);
0326     return (len - todo);
0327 }
0328 
0329 int dvb_vb2_reqbufs(struct dvb_vb2_ctx *ctx, struct dmx_requestbuffers *req)
0330 {
0331     int ret;
0332 
0333     /* Adjust size to a sane value */
0334     if (req->size > DVB_V2_MAX_SIZE)
0335         req->size = DVB_V2_MAX_SIZE;
0336 
0337     /* FIXME: round req->size to a 188 or 204 multiple */
0338 
0339     ctx->buf_siz = req->size;
0340     ctx->buf_cnt = req->count;
0341     ret = vb2_core_reqbufs(&ctx->vb_q, VB2_MEMORY_MMAP, 0, &req->count);
0342     if (ret) {
0343         ctx->state = DVB_VB2_STATE_NONE;
0344         dprintk(1, "[%s] count=%d size=%d errno=%d\n", ctx->name,
0345             ctx->buf_cnt, ctx->buf_siz, ret);
0346         return ret;
0347     }
0348     ctx->state |= DVB_VB2_STATE_REQBUFS;
0349     dprintk(3, "[%s] count=%d size=%d\n", ctx->name,
0350         ctx->buf_cnt, ctx->buf_siz);
0351 
0352     return 0;
0353 }
0354 
0355 int dvb_vb2_querybuf(struct dvb_vb2_ctx *ctx, struct dmx_buffer *b)
0356 {
0357     struct vb2_queue *q = &ctx->vb_q;
0358 
0359     if (b->index >= q->num_buffers) {
0360         dprintk(1, "[%s] buffer index out of range\n", ctx->name);
0361         return -EINVAL;
0362     }
0363     vb2_core_querybuf(&ctx->vb_q, b->index, b);
0364     dprintk(3, "[%s] index=%d\n", ctx->name, b->index);
0365     return 0;
0366 }
0367 
0368 int dvb_vb2_expbuf(struct dvb_vb2_ctx *ctx, struct dmx_exportbuffer *exp)
0369 {
0370     struct vb2_queue *q = &ctx->vb_q;
0371     int ret;
0372 
0373     ret = vb2_core_expbuf(&ctx->vb_q, &exp->fd, q->type, exp->index,
0374                   0, exp->flags);
0375     if (ret) {
0376         dprintk(1, "[%s] index=%d errno=%d\n", ctx->name,
0377             exp->index, ret);
0378         return ret;
0379     }
0380     dprintk(3, "[%s] index=%d fd=%d\n", ctx->name, exp->index, exp->fd);
0381 
0382     return 0;
0383 }
0384 
0385 int dvb_vb2_qbuf(struct dvb_vb2_ctx *ctx, struct dmx_buffer *b)
0386 {
0387     struct vb2_queue *q = &ctx->vb_q;
0388     int ret;
0389 
0390     if (b->index >= q->num_buffers) {
0391         dprintk(1, "[%s] buffer index out of range\n", ctx->name);
0392         return -EINVAL;
0393     }
0394     ret = vb2_core_qbuf(&ctx->vb_q, b->index, b, NULL);
0395     if (ret) {
0396         dprintk(1, "[%s] index=%d errno=%d\n", ctx->name,
0397             b->index, ret);
0398         return ret;
0399     }
0400     dprintk(5, "[%s] index=%d\n", ctx->name, b->index);
0401 
0402     return 0;
0403 }
0404 
0405 int dvb_vb2_dqbuf(struct dvb_vb2_ctx *ctx, struct dmx_buffer *b)
0406 {
0407     unsigned long flags;
0408     int ret;
0409 
0410     ret = vb2_core_dqbuf(&ctx->vb_q, &b->index, b, ctx->nonblocking);
0411     if (ret) {
0412         dprintk(1, "[%s] errno=%d\n", ctx->name, ret);
0413         return ret;
0414     }
0415 
0416     spin_lock_irqsave(&ctx->slock, flags);
0417     b->count = ctx->count++;
0418     b->flags = ctx->flags;
0419     ctx->flags = 0;
0420     spin_unlock_irqrestore(&ctx->slock, flags);
0421 
0422     dprintk(5, "[%s] index=%d, count=%d, flags=%d\n",
0423         ctx->name, b->index, ctx->count, b->flags);
0424 
0425 
0426     return 0;
0427 }
0428 
0429 int dvb_vb2_mmap(struct dvb_vb2_ctx *ctx, struct vm_area_struct *vma)
0430 {
0431     int ret;
0432 
0433     ret = vb2_mmap(&ctx->vb_q, vma);
0434     if (ret) {
0435         dprintk(1, "[%s] errno=%d\n", ctx->name, ret);
0436         return ret;
0437     }
0438     dprintk(3, "[%s] ret=%d\n", ctx->name, ret);
0439 
0440     return 0;
0441 }
0442 
0443 __poll_t dvb_vb2_poll(struct dvb_vb2_ctx *ctx, struct file *file,
0444               poll_table *wait)
0445 {
0446     dprintk(3, "[%s]\n", ctx->name);
0447     return vb2_core_poll(&ctx->vb_q, file, wait);
0448 }
0449