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. * @mutex: mutex to serialize vb2 operations. Used by
  69. * vb2 core %wait_prepare and %wait_finish operations.
  70. * @slock: spin lock used to protect buffer filling at dvb_vb2.c.
  71. * @dvb_q: List of buffers that are not filled yet.
  72. * @buf: Pointer to the buffer that are currently being filled.
  73. * @offset: index to the next position at the @buf to be filled.
  74. * @remain: How many bytes are left to be filled at @buf.
  75. * @state: bitmask of buffer states as defined by &enum dvb_vb2_states.
  76. * @buf_siz: size of each VB2 buffer.
  77. * @buf_cnt: number of VB2 buffers.
  78. * @nonblocking:
  79. * If different than zero, device is operating on non-blocking
  80. * mode.
  81. * @flags: buffer flags as defined by &enum dmx_buffer_flags.
  82. * Filled only at &DMX_DQBUF. &DMX_QBUF should zero this field.
  83. * @count: monotonic counter for filled buffers. Helps to identify
  84. * data stream loses. Filled only at &DMX_DQBUF. &DMX_QBUF should
  85. * zero this field.
  86. *
  87. * @name: name of the device type. Currently, it can either be
  88. * "dvr" or "demux_filter".
  89. */
  90. struct dvb_vb2_ctx {
  91. struct vb2_queue vb_q;
  92. struct mutex mutex;
  93. spinlock_t slock;
  94. struct list_head dvb_q;
  95. struct dvb_buffer *buf;
  96. int offset;
  97. int remain;
  98. int state;
  99. int buf_siz;
  100. int buf_cnt;
  101. int nonblocking;
  102. enum dmx_buffer_flags flags;
  103. u32 count;
  104. char name[DVB_VB2_NAME_MAX + 1];
  105. };
  106. #ifndef CONFIG_DVB_MMAP
  107. static inline int dvb_vb2_init(struct dvb_vb2_ctx *ctx,
  108. const char *name, int non_blocking)
  109. {
  110. return 0;
  111. };
  112. static inline int dvb_vb2_release(struct dvb_vb2_ctx *ctx)
  113. {
  114. return 0;
  115. };
  116. #define dvb_vb2_is_streaming(ctx) (0)
  117. #define dvb_vb2_fill_buffer(ctx, file, wait, flags) (0)
  118. static inline __poll_t dvb_vb2_poll(struct dvb_vb2_ctx *ctx,
  119. struct file *file,
  120. poll_table *wait)
  121. {
  122. return 0;
  123. }
  124. #else
  125. /**
  126. * dvb_vb2_init - initializes VB2 handler
  127. *
  128. * @ctx: control struct for VB2 handler
  129. * @name: name for the VB2 handler
  130. * @non_blocking:
  131. * if not zero, it means that the device is at non-blocking mode
  132. */
  133. int dvb_vb2_init(struct dvb_vb2_ctx *ctx, const char *name, int non_blocking);
  134. /**
  135. * dvb_vb2_release - Releases the VB2 handler allocated resources and
  136. * put @ctx at DVB_VB2_STATE_NONE state.
  137. * @ctx: control struct for VB2 handler
  138. */
  139. int dvb_vb2_release(struct dvb_vb2_ctx *ctx);
  140. /**
  141. * dvb_vb2_is_streaming - checks if the VB2 handler is streaming
  142. * @ctx: control struct for VB2 handler
  143. *
  144. * Return: 0 if not streaming, 1 otherwise.
  145. */
  146. int dvb_vb2_is_streaming(struct dvb_vb2_ctx *ctx);
  147. /**
  148. * dvb_vb2_fill_buffer - fills a VB2 buffer
  149. * @ctx: control struct for VB2 handler
  150. * @src: place where the data is stored
  151. * @len: number of bytes to be copied from @src
  152. * @buffer_flags:
  153. * pointer to buffer flags as defined by &enum dmx_buffer_flags.
  154. * can be NULL.
  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. /**
  160. * dvb_vb2_poll - Wrapper to vb2_core_streamon() for Digital TV
  161. * buffer handling.
  162. *
  163. * @ctx: control struct for VB2 handler
  164. * @file: &struct file argument passed to the poll
  165. * file operation handler.
  166. * @wait: &poll_table wait argument passed to the poll
  167. * file operation handler.
  168. *
  169. * Implements poll syscall() logic.
  170. */
  171. __poll_t dvb_vb2_poll(struct dvb_vb2_ctx *ctx, struct file *file,
  172. poll_table *wait);
  173. #endif
  174. /**
  175. * dvb_vb2_stream_on() - Wrapper to vb2_core_streamon() for Digital TV
  176. * buffer handling.
  177. *
  178. * @ctx: control struct for VB2 handler
  179. *
  180. * Starts dvb streaming
  181. */
  182. int dvb_vb2_stream_on(struct dvb_vb2_ctx *ctx);
  183. /**
  184. * dvb_vb2_stream_off() - Wrapper to vb2_core_streamoff() for Digital TV
  185. * buffer handling.
  186. *
  187. * @ctx: control struct for VB2 handler
  188. *
  189. * Stops dvb streaming
  190. */
  191. int dvb_vb2_stream_off(struct dvb_vb2_ctx *ctx);
  192. /**
  193. * dvb_vb2_reqbufs() - Wrapper to vb2_core_reqbufs() for Digital TV
  194. * buffer handling.
  195. *
  196. * @ctx: control struct for VB2 handler
  197. * @req: &struct dmx_requestbuffers passed from userspace in
  198. * order to handle &DMX_REQBUFS.
  199. *
  200. * Initiate streaming by requesting a number of buffers. Also used to
  201. * free previously requested buffers, is ``req->count`` is zero.
  202. */
  203. int dvb_vb2_reqbufs(struct dvb_vb2_ctx *ctx, struct dmx_requestbuffers *req);
  204. /**
  205. * dvb_vb2_querybuf() - Wrapper to vb2_core_querybuf() for Digital TV
  206. * buffer handling.
  207. *
  208. * @ctx: control struct for VB2 handler
  209. * @b: &struct dmx_buffer passed from userspace in
  210. * order to handle &DMX_QUERYBUF.
  211. *
  212. *
  213. */
  214. int dvb_vb2_querybuf(struct dvb_vb2_ctx *ctx, struct dmx_buffer *b);
  215. /**
  216. * dvb_vb2_expbuf() - Wrapper to vb2_core_expbuf() for Digital TV
  217. * buffer handling.
  218. *
  219. * @ctx: control struct for VB2 handler
  220. * @exp: &struct dmx_exportbuffer passed from userspace in
  221. * order to handle &DMX_EXPBUF.
  222. *
  223. * Export a buffer as a file descriptor.
  224. */
  225. int dvb_vb2_expbuf(struct dvb_vb2_ctx *ctx, struct dmx_exportbuffer *exp);
  226. /**
  227. * dvb_vb2_qbuf() - Wrapper to vb2_core_qbuf() for Digital TV buffer handling.
  228. *
  229. * @ctx: control struct for VB2 handler
  230. * @b: &struct dmx_buffer passed from userspace in
  231. * order to handle &DMX_QBUF.
  232. *
  233. * Queue a Digital TV buffer as requested by userspace
  234. */
  235. int dvb_vb2_qbuf(struct dvb_vb2_ctx *ctx, struct dmx_buffer *b);
  236. /**
  237. * dvb_vb2_dqbuf() - Wrapper to vb2_core_dqbuf() for Digital TV
  238. * buffer handling.
  239. *
  240. * @ctx: control struct for VB2 handler
  241. * @b: &struct dmx_buffer passed from userspace in
  242. * order to handle &DMX_DQBUF.
  243. *
  244. * Dequeue a Digital TV buffer to the userspace
  245. */
  246. int dvb_vb2_dqbuf(struct dvb_vb2_ctx *ctx, struct dmx_buffer *b);
  247. /**
  248. * dvb_vb2_mmap() - Wrapper to vb2_mmap() for Digital TV buffer handling.
  249. *
  250. * @ctx: control struct for VB2 handler
  251. * @vma: pointer to &struct vm_area_struct with the vma passed
  252. * to the mmap file operation handler in the driver.
  253. *
  254. * map Digital TV video buffers into application address space.
  255. */
  256. int dvb_vb2_mmap(struct dvb_vb2_ctx *ctx, struct vm_area_struct *vma);
  257. #endif /* _DVB_VB2_H */