dvb_vb2.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /*
  2. * SPDX-License-Identifier: GPL-2.0
  3. *
  4. * dvb-vb2.h - DVB driver helper framework for streaming I/O
  5. *
  6. * Copyright (C) 2015 Samsung Electronics
  7. *
  8. * Author: jh1009.sung@samsung.com
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation.
  13. */
  14. #ifndef _DVB_VB2_H
  15. #define _DVB_VB2_H
  16. #include <linux/mutex.h>
  17. #include <linux/poll.h>
  18. #include <linux/dvb/dmx.h>
  19. #include <media/videobuf2-core.h>
  20. #include <media/videobuf2-dma-contig.h>
  21. #include <media/videobuf2-vmalloc.h>
  22. /**
  23. * enum dvb_buf_type - types of Digital TV memory-mapped buffers
  24. *
  25. * @DVB_BUF_TYPE_CAPTURE: buffer is filled by the Kernel,
  26. * with a received Digital TV stream
  27. */
  28. enum dvb_buf_type {
  29. DVB_BUF_TYPE_CAPTURE = 1,
  30. };
  31. /**
  32. * enum dvb_vb2_states - states to control VB2 state machine
  33. * @DVB_VB2_STATE_NONE:
  34. * VB2 engine not initialized yet, init failed or VB2 was released.
  35. * @DVB_VB2_STATE_INIT:
  36. * VB2 engine initialized.
  37. * @DVB_VB2_STATE_REQBUFS:
  38. * Buffers were requested
  39. * @DVB_VB2_STATE_STREAMON:
  40. * VB2 is streaming. Callers should not check it directly. Instead,
  41. * they should use dvb_vb2_is_streaming().
  42. *
  43. * Note:
  44. *
  45. * Callers should not touch at the state machine directly. This
  46. * is handled inside dvb_vb2.c.
  47. */
  48. enum dvb_vb2_states {
  49. DVB_VB2_STATE_NONE = 0x0,
  50. DVB_VB2_STATE_INIT = 0x1,
  51. DVB_VB2_STATE_REQBUFS = 0x2,
  52. DVB_VB2_STATE_STREAMON = 0x4,
  53. };
  54. #define DVB_VB2_NAME_MAX (20)
  55. /**
  56. * struct dvb_buffer - video buffer information for v4l2.
  57. *
  58. * @vb: embedded struct &vb2_buffer.
  59. * @list: list of &struct dvb_buffer.
  60. */
  61. struct dvb_buffer {
  62. struct vb2_buffer vb;
  63. struct list_head list;
  64. };
  65. /**
  66. * struct dvb_vb2_ctx - control struct for VB2 handler
  67. * @vb_q: pointer to &struct vb2_queue with videobuf2 queue.
  68. * @slock: spin lock used to protect buffer filling at dvb_vb2.c.
  69. * @dvb_q: List of buffers that are not filled yet.
  70. * @buf: Pointer to the buffer that are currently being filled.
  71. * @offset: index to the next position at the @buf to be filled.
  72. * @remain: How many bytes are left to be filled at @buf.
  73. * @state: bitmask of buffer states as defined by &enum dvb_vb2_states.
  74. * @buf_siz: size of each VB2 buffer.
  75. * @buf_cnt: number of VB2 buffers.
  76. * @nonblocking:
  77. * If different than zero, device is operating on non-blocking
  78. * mode.
  79. * @flags: buffer flags as defined by &enum dmx_buffer_flags.
  80. * Filled only at &DMX_DQBUF. &DMX_QBUF should zero this field.
  81. * @count: monotonic counter for filled buffers. Helps to identify
  82. * data stream loses. Filled only at &DMX_DQBUF. &DMX_QBUF should
  83. * zero this field.
  84. *
  85. * @name: name of the device type. Currently, it can either be
  86. * "dvr" or "demux_filter".
  87. */
  88. struct dvb_vb2_ctx {
  89. struct vb2_queue vb_q;
  90. spinlock_t slock;
  91. struct list_head dvb_q;
  92. struct dvb_buffer *buf;
  93. int offset;
  94. int remain;
  95. int state;
  96. int buf_siz;
  97. int buf_cnt;
  98. int nonblocking;
  99. enum dmx_buffer_flags flags;
  100. u32 count;
  101. char name[DVB_VB2_NAME_MAX + 1];
  102. };
  103. #ifndef CONFIG_DVB_MMAP
  104. static inline int dvb_vb2_init(struct dvb_vb2_ctx *ctx, const char *name,
  105. struct mutex *mutex, int non_blocking)
  106. {
  107. return 0;
  108. };
  109. static inline int dvb_vb2_release(struct dvb_vb2_ctx *ctx)
  110. {
  111. return 0;
  112. };
  113. #define dvb_vb2_is_streaming(ctx) (0)
  114. #define dvb_vb2_fill_buffer(ctx, file, wait, flags, flush) (0)
  115. static inline __poll_t dvb_vb2_poll(struct dvb_vb2_ctx *ctx,
  116. struct file *file,
  117. poll_table *wait)
  118. {
  119. return 0;
  120. }
  121. #else
  122. /**
  123. * dvb_vb2_init - initializes VB2 handler
  124. *
  125. * @ctx: control struct for VB2 handler
  126. * @name: name for the VB2 handler
  127. * @mutex: pointer to the mutex that serializes vb2 ioctls
  128. * @non_blocking:
  129. * if not zero, it means that the device is at non-blocking mode
  130. */
  131. int dvb_vb2_init(struct dvb_vb2_ctx *ctx, const char *name,
  132. struct mutex *mutex, int non_blocking);
  133. /**
  134. * dvb_vb2_release - Releases the VB2 handler allocated resources and
  135. * put @ctx at DVB_VB2_STATE_NONE state.
  136. * @ctx: control struct for VB2 handler
  137. */
  138. int dvb_vb2_release(struct dvb_vb2_ctx *ctx);
  139. /**
  140. * dvb_vb2_is_streaming - checks if the VB2 handler is streaming
  141. * @ctx: control struct for VB2 handler
  142. *
  143. * Return: 0 if not streaming, 1 otherwise.
  144. */
  145. int dvb_vb2_is_streaming(struct dvb_vb2_ctx *ctx);
  146. /**
  147. * dvb_vb2_fill_buffer - fills a VB2 buffer
  148. * @ctx: control struct for VB2 handler
  149. * @src: place where the data is stored
  150. * @len: number of bytes to be copied from @src
  151. * @buffer_flags:
  152. * pointer to buffer flags as defined by &enum dmx_buffer_flags.
  153. * can be NULL.
  154. * @flush: flush the buffer, even if it isn't full.
  155. */
  156. int dvb_vb2_fill_buffer(struct dvb_vb2_ctx *ctx,
  157. const unsigned char *src, int len,
  158. enum dmx_buffer_flags *buffer_flags,
  159. bool flush);
  160. /**
  161. * dvb_vb2_poll - Wrapper to vb2_core_streamon() for Digital TV
  162. * buffer handling.
  163. *
  164. * @ctx: control struct for VB2 handler
  165. * @file: &struct file argument passed to the poll
  166. * file operation handler.
  167. * @wait: &poll_table wait argument passed to the poll
  168. * file operation handler.
  169. *
  170. * Implements poll syscall() logic.
  171. */
  172. __poll_t dvb_vb2_poll(struct dvb_vb2_ctx *ctx, struct file *file,
  173. poll_table *wait);
  174. #endif
  175. /**
  176. * dvb_vb2_stream_on() - Wrapper to vb2_core_streamon() for Digital TV
  177. * buffer handling.
  178. *
  179. * @ctx: control struct for VB2 handler
  180. *
  181. * Starts dvb streaming
  182. */
  183. int dvb_vb2_stream_on(struct dvb_vb2_ctx *ctx);
  184. /**
  185. * dvb_vb2_stream_off() - Wrapper to vb2_core_streamoff() for Digital TV
  186. * buffer handling.
  187. *
  188. * @ctx: control struct for VB2 handler
  189. *
  190. * Stops dvb streaming
  191. */
  192. int dvb_vb2_stream_off(struct dvb_vb2_ctx *ctx);
  193. /**
  194. * dvb_vb2_reqbufs() - Wrapper to vb2_core_reqbufs() for Digital TV
  195. * buffer handling.
  196. *
  197. * @ctx: control struct for VB2 handler
  198. * @req: &struct dmx_requestbuffers passed from userspace in
  199. * order to handle &DMX_REQBUFS.
  200. *
  201. * Initiate streaming by requesting a number of buffers. Also used to
  202. * free previously requested buffers, is ``req->count`` is zero.
  203. */
  204. int dvb_vb2_reqbufs(struct dvb_vb2_ctx *ctx, struct dmx_requestbuffers *req);
  205. /**
  206. * dvb_vb2_querybuf() - Wrapper to vb2_core_querybuf() for Digital TV
  207. * buffer handling.
  208. *
  209. * @ctx: control struct for VB2 handler
  210. * @b: &struct dmx_buffer passed from userspace in
  211. * order to handle &DMX_QUERYBUF.
  212. *
  213. *
  214. */
  215. int dvb_vb2_querybuf(struct dvb_vb2_ctx *ctx, struct dmx_buffer *b);
  216. /**
  217. * dvb_vb2_expbuf() - Wrapper to vb2_core_expbuf() for Digital TV
  218. * buffer handling.
  219. *
  220. * @ctx: control struct for VB2 handler
  221. * @exp: &struct dmx_exportbuffer passed from userspace in
  222. * order to handle &DMX_EXPBUF.
  223. *
  224. * Export a buffer as a file descriptor.
  225. */
  226. int dvb_vb2_expbuf(struct dvb_vb2_ctx *ctx, struct dmx_exportbuffer *exp);
  227. /**
  228. * dvb_vb2_qbuf() - Wrapper to vb2_core_qbuf() for Digital TV buffer handling.
  229. *
  230. * @ctx: control struct for VB2 handler
  231. * @b: &struct dmx_buffer passed from userspace in
  232. * order to handle &DMX_QBUF.
  233. *
  234. * Queue a Digital TV buffer as requested by userspace
  235. */
  236. int dvb_vb2_qbuf(struct dvb_vb2_ctx *ctx, struct dmx_buffer *b);
  237. /**
  238. * dvb_vb2_dqbuf() - Wrapper to vb2_core_dqbuf() for Digital TV
  239. * buffer handling.
  240. *
  241. * @ctx: control struct for VB2 handler
  242. * @b: &struct dmx_buffer passed from userspace in
  243. * order to handle &DMX_DQBUF.
  244. *
  245. * Dequeue a Digital TV buffer to the userspace
  246. */
  247. int dvb_vb2_dqbuf(struct dvb_vb2_ctx *ctx, struct dmx_buffer *b);
  248. /**
  249. * dvb_vb2_mmap() - Wrapper to vb2_mmap() for Digital TV buffer handling.
  250. *
  251. * @ctx: control struct for VB2 handler
  252. * @vma: pointer to &struct vm_area_struct with the vma passed
  253. * to the mmap file operation handler in the driver.
  254. *
  255. * map Digital TV video buffers into application address space.
  256. */
  257. int dvb_vb2_mmap(struct dvb_vb2_ctx *ctx, struct vm_area_struct *vma);
  258. #endif /* _DVB_VB2_H */