Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * SPDX-License-Identifier: GPL-2.0
0003  *
0004  * dvb-vb2.h - DVB driver helper framework for streaming I/O
0005  *
0006  * Copyright (C) 2015 Samsung Electronics
0007  *
0008  * Author: jh1009.sung@samsung.com
0009  *
0010  * This program is free software; you can redistribute it and/or modify
0011  * it under the terms of the GNU General Public License as published by
0012  * the Free Software Foundation.
0013  */
0014 
0015 #ifndef _DVB_VB2_H
0016 #define _DVB_VB2_H
0017 
0018 #include <linux/mutex.h>
0019 #include <linux/poll.h>
0020 #include <linux/dvb/dmx.h>
0021 #include <media/videobuf2-core.h>
0022 #include <media/videobuf2-dma-contig.h>
0023 #include <media/videobuf2-vmalloc.h>
0024 
0025 /**
0026  * enum dvb_buf_type - types of Digital TV memory-mapped buffers
0027  *
0028  * @DVB_BUF_TYPE_CAPTURE: buffer is filled by the Kernel,
0029  *            with a received Digital TV stream
0030  */
0031 enum dvb_buf_type {
0032     DVB_BUF_TYPE_CAPTURE        = 1,
0033 };
0034 
0035 /**
0036  * enum dvb_vb2_states - states to control VB2 state machine
0037  * @DVB_VB2_STATE_NONE:
0038  *  VB2 engine not initialized yet, init failed or VB2 was released.
0039  * @DVB_VB2_STATE_INIT:
0040  *  VB2 engine initialized.
0041  * @DVB_VB2_STATE_REQBUFS:
0042  *  Buffers were requested
0043  * @DVB_VB2_STATE_STREAMON:
0044  *  VB2 is streaming. Callers should not check it directly. Instead,
0045  *  they should use dvb_vb2_is_streaming().
0046  *
0047  * Note:
0048  *
0049  * Callers should not touch at the state machine directly. This
0050  * is handled inside dvb_vb2.c.
0051  */
0052 enum dvb_vb2_states {
0053     DVB_VB2_STATE_NONE  = 0x0,
0054     DVB_VB2_STATE_INIT  = 0x1,
0055     DVB_VB2_STATE_REQBUFS   = 0x2,
0056     DVB_VB2_STATE_STREAMON  = 0x4,
0057 };
0058 
0059 #define DVB_VB2_NAME_MAX (20)
0060 
0061 /**
0062  * struct dvb_buffer - video buffer information for v4l2.
0063  *
0064  * @vb:     embedded struct &vb2_buffer.
0065  * @list:   list of &struct dvb_buffer.
0066  */
0067 struct dvb_buffer {
0068     struct vb2_buffer   vb;
0069     struct list_head    list;
0070 };
0071 
0072 /**
0073  * struct dvb_vb2_ctx - control struct for VB2 handler
0074  * @vb_q:   pointer to &struct vb2_queue with videobuf2 queue.
0075  * @mutex:  mutex to serialize vb2 operations. Used by
0076  *      vb2 core %wait_prepare and %wait_finish operations.
0077  * @slock:  spin lock used to protect buffer filling at dvb_vb2.c.
0078  * @dvb_q:  List of buffers that are not filled yet.
0079  * @buf:    Pointer to the buffer that are currently being filled.
0080  * @offset: index to the next position at the @buf to be filled.
0081  * @remain: How many bytes are left to be filled at @buf.
0082  * @state:  bitmask of buffer states as defined by &enum dvb_vb2_states.
0083  * @buf_siz:    size of each VB2 buffer.
0084  * @buf_cnt:    number of VB2 buffers.
0085  * @nonblocking:
0086  *      If different than zero, device is operating on non-blocking
0087  *      mode.
0088  * @flags:  buffer flags as defined by &enum dmx_buffer_flags.
0089  *      Filled only at &DMX_DQBUF. &DMX_QBUF should zero this field.
0090  * @count:  monotonic counter for filled buffers. Helps to identify
0091  *      data stream loses. Filled only at &DMX_DQBUF. &DMX_QBUF should
0092  *      zero this field.
0093  *
0094  * @name:   name of the device type. Currently, it can either be
0095  *      "dvr" or "demux_filter".
0096  */
0097 struct dvb_vb2_ctx {
0098     struct vb2_queue    vb_q;
0099     struct mutex        mutex;
0100     spinlock_t      slock;
0101     struct list_head    dvb_q;
0102     struct dvb_buffer   *buf;
0103     int offset;
0104     int remain;
0105     int state;
0106     int buf_siz;
0107     int buf_cnt;
0108     int nonblocking;
0109 
0110     enum dmx_buffer_flags flags;
0111     u32 count;
0112 
0113     char    name[DVB_VB2_NAME_MAX + 1];
0114 };
0115 
0116 #ifndef CONFIG_DVB_MMAP
0117 static inline int dvb_vb2_init(struct dvb_vb2_ctx *ctx,
0118                    const char *name, int non_blocking)
0119 {
0120     return 0;
0121 };
0122 static inline int dvb_vb2_release(struct dvb_vb2_ctx *ctx)
0123 {
0124     return 0;
0125 };
0126 #define dvb_vb2_is_streaming(ctx) (0)
0127 #define dvb_vb2_fill_buffer(ctx, file, wait, flags) (0)
0128 
0129 static inline __poll_t dvb_vb2_poll(struct dvb_vb2_ctx *ctx,
0130                     struct file *file,
0131                     poll_table *wait)
0132 {
0133     return 0;
0134 }
0135 #else
0136 /**
0137  * dvb_vb2_init - initializes VB2 handler
0138  *
0139  * @ctx:    control struct for VB2 handler
0140  * @name:   name for the VB2 handler
0141  * @non_blocking:
0142  *      if not zero, it means that the device is at non-blocking mode
0143  */
0144 int dvb_vb2_init(struct dvb_vb2_ctx *ctx, const char *name, int non_blocking);
0145 
0146 /**
0147  * dvb_vb2_release - Releases the VB2 handler allocated resources and
0148  *  put @ctx at DVB_VB2_STATE_NONE state.
0149  * @ctx:    control struct for VB2 handler
0150  */
0151 int dvb_vb2_release(struct dvb_vb2_ctx *ctx);
0152 
0153 /**
0154  * dvb_vb2_is_streaming - checks if the VB2 handler is streaming
0155  * @ctx:    control struct for VB2 handler
0156  *
0157  * Return: 0 if not streaming, 1 otherwise.
0158  */
0159 int dvb_vb2_is_streaming(struct dvb_vb2_ctx *ctx);
0160 
0161 /**
0162  * dvb_vb2_fill_buffer - fills a VB2 buffer
0163  * @ctx:    control struct for VB2 handler
0164  * @src:    place where the data is stored
0165  * @len:    number of bytes to be copied from @src
0166  * @buffer_flags:
0167  *      pointer to buffer flags as defined by &enum dmx_buffer_flags.
0168  *      can be NULL.
0169  */
0170 int dvb_vb2_fill_buffer(struct dvb_vb2_ctx *ctx,
0171             const unsigned char *src, int len,
0172             enum dmx_buffer_flags *buffer_flags);
0173 
0174 /**
0175  * dvb_vb2_poll - Wrapper to vb2_core_streamon() for Digital TV
0176  *      buffer handling.
0177  *
0178  * @ctx:    control struct for VB2 handler
0179  * @file:   &struct file argument passed to the poll
0180  *      file operation handler.
0181  * @wait:   &poll_table wait argument passed to the poll
0182  *      file operation handler.
0183  *
0184  * Implements poll syscall() logic.
0185  */
0186 __poll_t dvb_vb2_poll(struct dvb_vb2_ctx *ctx, struct file *file,
0187               poll_table *wait);
0188 #endif
0189 
0190 /**
0191  * dvb_vb2_stream_on() - Wrapper to vb2_core_streamon() for Digital TV
0192  *  buffer handling.
0193  *
0194  * @ctx:    control struct for VB2 handler
0195  *
0196  * Starts dvb streaming
0197  */
0198 int dvb_vb2_stream_on(struct dvb_vb2_ctx *ctx);
0199 /**
0200  * dvb_vb2_stream_off() - Wrapper to vb2_core_streamoff() for Digital TV
0201  *  buffer handling.
0202  *
0203  * @ctx:    control struct for VB2 handler
0204  *
0205  * Stops dvb streaming
0206  */
0207 int dvb_vb2_stream_off(struct dvb_vb2_ctx *ctx);
0208 
0209 /**
0210  * dvb_vb2_reqbufs() - Wrapper to vb2_core_reqbufs() for Digital TV
0211  *  buffer handling.
0212  *
0213  * @ctx:    control struct for VB2 handler
0214  * @req:    &struct dmx_requestbuffers passed from userspace in
0215  *      order to handle &DMX_REQBUFS.
0216  *
0217  * Initiate streaming by requesting a number of buffers. Also used to
0218  * free previously requested buffers, is ``req->count`` is zero.
0219  */
0220 int dvb_vb2_reqbufs(struct dvb_vb2_ctx *ctx, struct dmx_requestbuffers *req);
0221 
0222 /**
0223  * dvb_vb2_querybuf() - Wrapper to vb2_core_querybuf() for Digital TV
0224  *  buffer handling.
0225  *
0226  * @ctx:    control struct for VB2 handler
0227  * @b:      &struct dmx_buffer passed from userspace in
0228  *      order to handle &DMX_QUERYBUF.
0229  *
0230  *
0231  */
0232 int dvb_vb2_querybuf(struct dvb_vb2_ctx *ctx, struct dmx_buffer *b);
0233 
0234 /**
0235  * dvb_vb2_expbuf() - Wrapper to vb2_core_expbuf() for Digital TV
0236  *  buffer handling.
0237  *
0238  * @ctx:    control struct for VB2 handler
0239  * @exp:    &struct dmx_exportbuffer passed from userspace in
0240  *      order to handle &DMX_EXPBUF.
0241  *
0242  * Export a buffer as a file descriptor.
0243  */
0244 int dvb_vb2_expbuf(struct dvb_vb2_ctx *ctx, struct dmx_exportbuffer *exp);
0245 
0246 /**
0247  * dvb_vb2_qbuf() - Wrapper to vb2_core_qbuf() for Digital TV buffer handling.
0248  *
0249  * @ctx:    control struct for VB2 handler
0250  * @b:      &struct dmx_buffer passed from userspace in
0251  *      order to handle &DMX_QBUF.
0252  *
0253  * Queue a Digital TV buffer as requested by userspace
0254  */
0255 int dvb_vb2_qbuf(struct dvb_vb2_ctx *ctx, struct dmx_buffer *b);
0256 
0257 /**
0258  * dvb_vb2_dqbuf() - Wrapper to vb2_core_dqbuf() for Digital TV
0259  *  buffer handling.
0260  *
0261  * @ctx:    control struct for VB2 handler
0262  * @b:      &struct dmx_buffer passed from userspace in
0263  *      order to handle &DMX_DQBUF.
0264  *
0265  * Dequeue a Digital TV buffer to the userspace
0266  */
0267 int dvb_vb2_dqbuf(struct dvb_vb2_ctx *ctx, struct dmx_buffer *b);
0268 
0269 /**
0270  * dvb_vb2_mmap() - Wrapper to vb2_mmap() for Digital TV buffer handling.
0271  *
0272  * @ctx:    control struct for VB2 handler
0273  * @vma:        pointer to &struct vm_area_struct with the vma passed
0274  *              to the mmap file operation handler in the driver.
0275  *
0276  * map Digital TV video buffers into application address space.
0277  */
0278 int dvb_vb2_mmap(struct dvb_vb2_ctx *ctx, struct vm_area_struct *vma);
0279 
0280 #endif /* _DVB_VB2_H */