/*1* SPDX-License-Identifier: GPL-2.02*3* dvb-vb2.h - DVB driver helper framework for streaming I/O4*5* Copyright (C) 2015 Samsung Electronics6*7* Author: [email protected]8*9* This program is free software; you can redistribute it and/or modify10* it under the terms of the GNU General Public License as published by11* the Free Software Foundation.12*/1314#ifndef _DVB_VB2_H15#define _DVB_VB2_H1617#include <linux/mutex.h>18#include <linux/poll.h>19#include <linux/dvb/dmx.h>20#include <media/videobuf2-core.h>21#include <media/videobuf2-dma-contig.h>22#include <media/videobuf2-vmalloc.h>2324/**25* enum dvb_buf_type - types of Digital TV memory-mapped buffers26*27* @DVB_BUF_TYPE_CAPTURE: buffer is filled by the Kernel,28* with a received Digital TV stream29*/30enum dvb_buf_type {31DVB_BUF_TYPE_CAPTURE = 1,32};3334/**35* enum dvb_vb2_states - states to control VB2 state machine36* @DVB_VB2_STATE_NONE:37* VB2 engine not initialized yet, init failed or VB2 was released.38* @DVB_VB2_STATE_INIT:39* VB2 engine initialized.40* @DVB_VB2_STATE_REQBUFS:41* Buffers were requested42* @DVB_VB2_STATE_STREAMON:43* VB2 is streaming. Callers should not check it directly. Instead,44* they should use dvb_vb2_is_streaming().45*46* Note:47*48* Callers should not touch at the state machine directly. This49* is handled inside dvb_vb2.c.50*/51enum dvb_vb2_states {52DVB_VB2_STATE_NONE = 0x0,53DVB_VB2_STATE_INIT = 0x1,54DVB_VB2_STATE_REQBUFS = 0x2,55DVB_VB2_STATE_STREAMON = 0x4,56};5758#define DVB_VB2_NAME_MAX (20)5960/**61* struct dvb_buffer - video buffer information for v4l2.62*63* @vb: embedded struct &vb2_buffer.64* @list: list of &struct dvb_buffer.65*/66struct dvb_buffer {67struct vb2_buffer vb;68struct list_head list;69};7071/**72* struct dvb_vb2_ctx - control struct for VB2 handler73* @vb_q: pointer to &struct vb2_queue with videobuf2 queue.74* @mutex: mutex to serialize vb2 operations. Used by75* vb2 core %wait_prepare and %wait_finish operations.76* @slock: spin lock used to protect buffer filling at dvb_vb2.c.77* @dvb_q: List of buffers that are not filled yet.78* @buf: Pointer to the buffer that are currently being filled.79* @offset: index to the next position at the @buf to be filled.80* @remain: How many bytes are left to be filled at @buf.81* @state: bitmask of buffer states as defined by &enum dvb_vb2_states.82* @buf_siz: size of each VB2 buffer.83* @buf_cnt: number of VB2 buffers.84* @nonblocking:85* If different than zero, device is operating on non-blocking86* mode.87* @flags: buffer flags as defined by &enum dmx_buffer_flags.88* Filled only at &DMX_DQBUF. &DMX_QBUF should zero this field.89* @count: monotonic counter for filled buffers. Helps to identify90* data stream loses. Filled only at &DMX_DQBUF. &DMX_QBUF should91* zero this field.92*93* @name: name of the device type. Currently, it can either be94* "dvr" or "demux_filter".95*/96struct dvb_vb2_ctx {97struct vb2_queue vb_q;98struct mutex mutex;99spinlock_t slock;100struct list_head dvb_q;101struct dvb_buffer *buf;102int offset;103int remain;104int state;105int buf_siz;106int buf_cnt;107int nonblocking;108109enum dmx_buffer_flags flags;110u32 count;111112char name[DVB_VB2_NAME_MAX + 1];113};114115#ifndef CONFIG_DVB_MMAP116static inline int dvb_vb2_init(struct dvb_vb2_ctx *ctx,117const char *name, int non_blocking)118{119return 0;120};121static inline int dvb_vb2_release(struct dvb_vb2_ctx *ctx)122{123return 0;124};125#define dvb_vb2_is_streaming(ctx) (0)126#define dvb_vb2_fill_buffer(ctx, file, wait, flags) (0)127128static inline __poll_t dvb_vb2_poll(struct dvb_vb2_ctx *ctx,129struct file *file,130poll_table *wait)131{132return 0;133}134#else135/**136* dvb_vb2_init - initializes VB2 handler137*138* @ctx: control struct for VB2 handler139* @name: name for the VB2 handler140* @non_blocking:141* if not zero, it means that the device is at non-blocking mode142*/143int dvb_vb2_init(struct dvb_vb2_ctx *ctx, const char *name, int non_blocking);144145/**146* dvb_vb2_release - Releases the VB2 handler allocated resources and147* put @ctx at DVB_VB2_STATE_NONE state.148* @ctx: control struct for VB2 handler149*/150int dvb_vb2_release(struct dvb_vb2_ctx *ctx);151152/**153* dvb_vb2_is_streaming - checks if the VB2 handler is streaming154* @ctx: control struct for VB2 handler155*156* Return: 0 if not streaming, 1 otherwise.157*/158int dvb_vb2_is_streaming(struct dvb_vb2_ctx *ctx);159160/**161* dvb_vb2_fill_buffer - fills a VB2 buffer162* @ctx: control struct for VB2 handler163* @src: place where the data is stored164* @len: number of bytes to be copied from @src165* @buffer_flags:166* pointer to buffer flags as defined by &enum dmx_buffer_flags.167* can be NULL.168*/169int dvb_vb2_fill_buffer(struct dvb_vb2_ctx *ctx,170const unsigned char *src, int len,171enum dmx_buffer_flags *buffer_flags);172173/**174* dvb_vb2_poll - Wrapper to vb2_core_streamon() for Digital TV175* buffer handling.176*177* @ctx: control struct for VB2 handler178* @file: &struct file argument passed to the poll179* file operation handler.180* @wait: &poll_table wait argument passed to the poll181* file operation handler.182*183* Implements poll syscall() logic.184*/185__poll_t dvb_vb2_poll(struct dvb_vb2_ctx *ctx, struct file *file,186poll_table *wait);187#endif188189/**190* dvb_vb2_stream_on() - Wrapper to vb2_core_streamon() for Digital TV191* buffer handling.192*193* @ctx: control struct for VB2 handler194*195* Starts dvb streaming196*/197int dvb_vb2_stream_on(struct dvb_vb2_ctx *ctx);198/**199* dvb_vb2_stream_off() - Wrapper to vb2_core_streamoff() for Digital TV200* buffer handling.201*202* @ctx: control struct for VB2 handler203*204* Stops dvb streaming205*/206int dvb_vb2_stream_off(struct dvb_vb2_ctx *ctx);207208/**209* dvb_vb2_reqbufs() - Wrapper to vb2_core_reqbufs() for Digital TV210* buffer handling.211*212* @ctx: control struct for VB2 handler213* @req: &struct dmx_requestbuffers passed from userspace in214* order to handle &DMX_REQBUFS.215*216* Initiate streaming by requesting a number of buffers. Also used to217* free previously requested buffers, is ``req->count`` is zero.218*/219int dvb_vb2_reqbufs(struct dvb_vb2_ctx *ctx, struct dmx_requestbuffers *req);220221/**222* dvb_vb2_querybuf() - Wrapper to vb2_core_querybuf() for Digital TV223* buffer handling.224*225* @ctx: control struct for VB2 handler226* @b: &struct dmx_buffer passed from userspace in227* order to handle &DMX_QUERYBUF.228*229*230*/231int dvb_vb2_querybuf(struct dvb_vb2_ctx *ctx, struct dmx_buffer *b);232233/**234* dvb_vb2_expbuf() - Wrapper to vb2_core_expbuf() for Digital TV235* buffer handling.236*237* @ctx: control struct for VB2 handler238* @exp: &struct dmx_exportbuffer passed from userspace in239* order to handle &DMX_EXPBUF.240*241* Export a buffer as a file descriptor.242*/243int dvb_vb2_expbuf(struct dvb_vb2_ctx *ctx, struct dmx_exportbuffer *exp);244245/**246* dvb_vb2_qbuf() - Wrapper to vb2_core_qbuf() for Digital TV buffer handling.247*248* @ctx: control struct for VB2 handler249* @b: &struct dmx_buffer passed from userspace in250* order to handle &DMX_QBUF.251*252* Queue a Digital TV buffer as requested by userspace253*/254int dvb_vb2_qbuf(struct dvb_vb2_ctx *ctx, struct dmx_buffer *b);255256/**257* dvb_vb2_dqbuf() - Wrapper to vb2_core_dqbuf() for Digital TV258* buffer handling.259*260* @ctx: control struct for VB2 handler261* @b: &struct dmx_buffer passed from userspace in262* order to handle &DMX_DQBUF.263*264* Dequeue a Digital TV buffer to the userspace265*/266int dvb_vb2_dqbuf(struct dvb_vb2_ctx *ctx, struct dmx_buffer *b);267268/**269* dvb_vb2_mmap() - Wrapper to vb2_mmap() for Digital TV buffer handling.270*271* @ctx: control struct for VB2 handler272* @vma: pointer to &struct vm_area_struct with the vma passed273* to the mmap file operation handler in the driver.274*275* map Digital TV video buffers into application address space.276*/277int dvb_vb2_mmap(struct dvb_vb2_ctx *ctx, struct vm_area_struct *vma);278279#endif /* _DVB_VB2_H */280281282