dvb_vb2.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * dvb-vb2.c - dvb-vb2
  4. *
  5. * Copyright (C) 2015 Samsung Electronics
  6. *
  7. * Author: jh1009.sung@samsung.com
  8. */
  9. #include <linux/err.h>
  10. #include <linux/kernel.h>
  11. #include <linux/module.h>
  12. #include <linux/mm.h>
  13. #include <media/dvbdev.h>
  14. #include <media/dvb_vb2.h>
  15. #define DVB_V2_MAX_SIZE (4096 * 188)
  16. static int vb2_debug;
  17. module_param(vb2_debug, int, 0644);
  18. #define dprintk(level, fmt, arg...) \
  19. do { \
  20. if (vb2_debug >= level) \
  21. pr_info("vb2: %s: " fmt, __func__, ## arg); \
  22. } while (0)
  23. static int _queue_setup(struct vb2_queue *vq,
  24. unsigned int *nbuffers, unsigned int *nplanes,
  25. unsigned int sizes[], struct device *alloc_devs[])
  26. {
  27. struct dvb_vb2_ctx *ctx = vb2_get_drv_priv(vq);
  28. ctx->buf_cnt = *nbuffers;
  29. *nplanes = 1;
  30. sizes[0] = ctx->buf_siz;
  31. /*
  32. * videobuf2-vmalloc allocator is context-less so no need to set
  33. * alloc_ctxs array.
  34. */
  35. dprintk(3, "[%s] count=%d, size=%d\n", ctx->name,
  36. *nbuffers, sizes[0]);
  37. return 0;
  38. }
  39. static int _buffer_prepare(struct vb2_buffer *vb)
  40. {
  41. struct dvb_vb2_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
  42. unsigned long size = ctx->buf_siz;
  43. if (vb2_plane_size(vb, 0) < size) {
  44. dprintk(1, "[%s] data will not fit into plane (%lu < %lu)\n",
  45. ctx->name, vb2_plane_size(vb, 0), size);
  46. return -EINVAL;
  47. }
  48. vb2_set_plane_payload(vb, 0, size);
  49. dprintk(3, "[%s]\n", ctx->name);
  50. return 0;
  51. }
  52. static void _buffer_queue(struct vb2_buffer *vb)
  53. {
  54. struct dvb_vb2_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
  55. struct dvb_buffer *buf = container_of(vb, struct dvb_buffer, vb);
  56. unsigned long flags = 0;
  57. spin_lock_irqsave(&ctx->slock, flags);
  58. list_add_tail(&buf->list, &ctx->dvb_q);
  59. spin_unlock_irqrestore(&ctx->slock, flags);
  60. dprintk(3, "[%s]\n", ctx->name);
  61. }
  62. static int _start_streaming(struct vb2_queue *vq, unsigned int count)
  63. {
  64. struct dvb_vb2_ctx *ctx = vb2_get_drv_priv(vq);
  65. dprintk(3, "[%s] count=%d\n", ctx->name, count);
  66. return 0;
  67. }
  68. static void _stop_streaming(struct vb2_queue *vq)
  69. {
  70. struct dvb_vb2_ctx *ctx = vb2_get_drv_priv(vq);
  71. struct dvb_buffer *buf;
  72. unsigned long flags = 0;
  73. dprintk(3, "[%s]\n", ctx->name);
  74. spin_lock_irqsave(&ctx->slock, flags);
  75. while (!list_empty(&ctx->dvb_q)) {
  76. buf = list_entry(ctx->dvb_q.next,
  77. struct dvb_buffer, list);
  78. vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
  79. list_del(&buf->list);
  80. }
  81. spin_unlock_irqrestore(&ctx->slock, flags);
  82. }
  83. static const struct vb2_ops dvb_vb2_qops = {
  84. .queue_setup = _queue_setup,
  85. .buf_prepare = _buffer_prepare,
  86. .buf_queue = _buffer_queue,
  87. .start_streaming = _start_streaming,
  88. .stop_streaming = _stop_streaming,
  89. };
  90. static void _fill_dmx_buffer(struct vb2_buffer *vb, void *pb)
  91. {
  92. struct dvb_vb2_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
  93. struct dmx_buffer *b = pb;
  94. b->index = vb->index;
  95. b->length = vb->planes[0].length;
  96. b->bytesused = vb->planes[0].bytesused;
  97. b->offset = vb->planes[0].m.offset;
  98. dprintk(3, "[%s]\n", ctx->name);
  99. }
  100. static int _fill_vb2_buffer(struct vb2_buffer *vb, struct vb2_plane *planes)
  101. {
  102. struct dvb_vb2_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
  103. planes[0].bytesused = 0;
  104. dprintk(3, "[%s]\n", ctx->name);
  105. return 0;
  106. }
  107. static const struct vb2_buf_ops dvb_vb2_buf_ops = {
  108. .fill_user_buffer = _fill_dmx_buffer,
  109. .fill_vb2_buffer = _fill_vb2_buffer,
  110. };
  111. /*
  112. * vb2 operations
  113. */
  114. int dvb_vb2_init(struct dvb_vb2_ctx *ctx, const char *name,
  115. struct mutex *mutex, int nonblocking)
  116. {
  117. struct vb2_queue *q = &ctx->vb_q;
  118. int ret;
  119. memset(ctx, 0, sizeof(struct dvb_vb2_ctx));
  120. q->type = DVB_BUF_TYPE_CAPTURE;
  121. /**only mmap is supported currently*/
  122. q->io_modes = VB2_MMAP;
  123. q->drv_priv = ctx;
  124. q->buf_struct_size = sizeof(struct dvb_buffer);
  125. q->min_queued_buffers = 1;
  126. q->ops = &dvb_vb2_qops;
  127. q->mem_ops = &vb2_vmalloc_memops;
  128. q->buf_ops = &dvb_vb2_buf_ops;
  129. q->lock = mutex;
  130. spin_lock_init(&ctx->slock);
  131. INIT_LIST_HEAD(&ctx->dvb_q);
  132. strscpy(ctx->name, name, DVB_VB2_NAME_MAX);
  133. ctx->nonblocking = nonblocking;
  134. ctx->state = DVB_VB2_STATE_INIT;
  135. ret = vb2_core_queue_init(q);
  136. if (ret) {
  137. ctx->state = DVB_VB2_STATE_NONE;
  138. dprintk(1, "[%s] errno=%d\n", ctx->name, ret);
  139. return ret;
  140. }
  141. dprintk(3, "[%s]\n", ctx->name);
  142. return 0;
  143. }
  144. int dvb_vb2_release(struct dvb_vb2_ctx *ctx)
  145. {
  146. struct vb2_queue *q = (struct vb2_queue *)&ctx->vb_q;
  147. if (ctx->state & DVB_VB2_STATE_INIT)
  148. vb2_core_queue_release(q);
  149. ctx->state = DVB_VB2_STATE_NONE;
  150. dprintk(3, "[%s]\n", ctx->name);
  151. return 0;
  152. }
  153. int dvb_vb2_stream_on(struct dvb_vb2_ctx *ctx)
  154. {
  155. struct vb2_queue *q = &ctx->vb_q;
  156. int ret;
  157. ret = vb2_core_streamon(q, q->type);
  158. if (ret) {
  159. ctx->state = DVB_VB2_STATE_NONE;
  160. dprintk(1, "[%s] errno=%d\n", ctx->name, ret);
  161. return ret;
  162. }
  163. ctx->state |= DVB_VB2_STATE_STREAMON;
  164. dprintk(3, "[%s]\n", ctx->name);
  165. return 0;
  166. }
  167. int dvb_vb2_stream_off(struct dvb_vb2_ctx *ctx)
  168. {
  169. struct vb2_queue *q = (struct vb2_queue *)&ctx->vb_q;
  170. int ret;
  171. ctx->state &= ~DVB_VB2_STATE_STREAMON;
  172. ret = vb2_core_streamoff(q, q->type);
  173. if (ret) {
  174. ctx->state = DVB_VB2_STATE_NONE;
  175. dprintk(1, "[%s] errno=%d\n", ctx->name, ret);
  176. return ret;
  177. }
  178. dprintk(3, "[%s]\n", ctx->name);
  179. return 0;
  180. }
  181. int dvb_vb2_is_streaming(struct dvb_vb2_ctx *ctx)
  182. {
  183. return (ctx->state & DVB_VB2_STATE_STREAMON);
  184. }
  185. int dvb_vb2_fill_buffer(struct dvb_vb2_ctx *ctx,
  186. const unsigned char *src, int len,
  187. enum dmx_buffer_flags *buffer_flags,
  188. bool flush)
  189. {
  190. unsigned long flags = 0;
  191. void *vbuf = NULL;
  192. int todo = len;
  193. unsigned char *psrc = (unsigned char *)src;
  194. int ll = 0;
  195. /*
  196. * normal case: This func is called twice from demux driver
  197. * one with valid src pointer, second time with NULL pointer
  198. */
  199. if (!src || !len)
  200. return 0;
  201. spin_lock_irqsave(&ctx->slock, flags);
  202. if (buffer_flags && *buffer_flags) {
  203. ctx->flags |= *buffer_flags;
  204. *buffer_flags = 0;
  205. }
  206. while (todo) {
  207. if (!ctx->buf) {
  208. if (list_empty(&ctx->dvb_q)) {
  209. dprintk(3, "[%s] Buffer overflow!!!\n",
  210. ctx->name);
  211. break;
  212. }
  213. ctx->buf = list_entry(ctx->dvb_q.next,
  214. struct dvb_buffer, list);
  215. ctx->remain = vb2_plane_size(&ctx->buf->vb, 0);
  216. ctx->offset = 0;
  217. }
  218. if (!dvb_vb2_is_streaming(ctx)) {
  219. vb2_buffer_done(&ctx->buf->vb, VB2_BUF_STATE_ERROR);
  220. list_del(&ctx->buf->list);
  221. ctx->buf = NULL;
  222. break;
  223. }
  224. /* Fill buffer */
  225. ll = min(todo, ctx->remain);
  226. vbuf = vb2_plane_vaddr(&ctx->buf->vb, 0);
  227. memcpy(vbuf + ctx->offset, psrc, ll);
  228. todo -= ll;
  229. psrc += ll;
  230. ctx->remain -= ll;
  231. ctx->offset += ll;
  232. if (ctx->remain == 0) {
  233. vb2_buffer_done(&ctx->buf->vb, VB2_BUF_STATE_DONE);
  234. list_del(&ctx->buf->list);
  235. ctx->buf = NULL;
  236. }
  237. }
  238. if (flush && ctx->buf) {
  239. vb2_set_plane_payload(&ctx->buf->vb, 0, ll);
  240. vb2_buffer_done(&ctx->buf->vb, VB2_BUF_STATE_DONE);
  241. list_del(&ctx->buf->list);
  242. ctx->buf = NULL;
  243. }
  244. spin_unlock_irqrestore(&ctx->slock, flags);
  245. if (todo)
  246. dprintk(1, "[%s] %d bytes are dropped.\n", ctx->name, todo);
  247. else
  248. dprintk(3, "[%s]\n", ctx->name);
  249. dprintk(3, "[%s] %d bytes are copied\n", ctx->name, len - todo);
  250. return (len - todo);
  251. }
  252. int dvb_vb2_reqbufs(struct dvb_vb2_ctx *ctx, struct dmx_requestbuffers *req)
  253. {
  254. int ret;
  255. /* Adjust size to a sane value */
  256. if (req->size > DVB_V2_MAX_SIZE)
  257. req->size = DVB_V2_MAX_SIZE;
  258. /* FIXME: round req->size to a 188 or 204 multiple */
  259. ctx->buf_siz = req->size;
  260. ctx->buf_cnt = req->count;
  261. ret = vb2_core_reqbufs(&ctx->vb_q, VB2_MEMORY_MMAP, 0, &req->count);
  262. if (ret) {
  263. ctx->state = DVB_VB2_STATE_NONE;
  264. dprintk(1, "[%s] count=%d size=%d errno=%d\n", ctx->name,
  265. ctx->buf_cnt, ctx->buf_siz, ret);
  266. return ret;
  267. }
  268. ctx->state |= DVB_VB2_STATE_REQBUFS;
  269. dprintk(3, "[%s] count=%d size=%d\n", ctx->name,
  270. ctx->buf_cnt, ctx->buf_siz);
  271. return 0;
  272. }
  273. int dvb_vb2_querybuf(struct dvb_vb2_ctx *ctx, struct dmx_buffer *b)
  274. {
  275. struct vb2_queue *q = &ctx->vb_q;
  276. struct vb2_buffer *vb2 = vb2_get_buffer(q, b->index);
  277. if (!vb2) {
  278. dprintk(1, "[%s] invalid buffer index\n", ctx->name);
  279. return -EINVAL;
  280. }
  281. vb2_core_querybuf(&ctx->vb_q, vb2, b);
  282. dprintk(3, "[%s] index=%d\n", ctx->name, b->index);
  283. return 0;
  284. }
  285. int dvb_vb2_expbuf(struct dvb_vb2_ctx *ctx, struct dmx_exportbuffer *exp)
  286. {
  287. struct vb2_queue *q = &ctx->vb_q;
  288. struct vb2_buffer *vb2 = vb2_get_buffer(q, exp->index);
  289. int ret;
  290. if (!vb2) {
  291. dprintk(1, "[%s] invalid buffer index\n", ctx->name);
  292. return -EINVAL;
  293. }
  294. ret = vb2_core_expbuf(&ctx->vb_q, &exp->fd, q->type, vb2,
  295. 0, exp->flags);
  296. if (ret) {
  297. dprintk(1, "[%s] index=%d errno=%d\n", ctx->name,
  298. exp->index, ret);
  299. return ret;
  300. }
  301. dprintk(3, "[%s] index=%d fd=%d\n", ctx->name, exp->index, exp->fd);
  302. return 0;
  303. }
  304. int dvb_vb2_qbuf(struct dvb_vb2_ctx *ctx, struct dmx_buffer *b)
  305. {
  306. struct vb2_queue *q = &ctx->vb_q;
  307. struct vb2_buffer *vb2 = vb2_get_buffer(q, b->index);
  308. int ret;
  309. if (!vb2) {
  310. dprintk(1, "[%s] invalid buffer index\n", ctx->name);
  311. return -EINVAL;
  312. }
  313. ret = vb2_core_qbuf(&ctx->vb_q, vb2, b, NULL);
  314. if (ret) {
  315. dprintk(1, "[%s] index=%d errno=%d\n", ctx->name,
  316. b->index, ret);
  317. return ret;
  318. }
  319. dprintk(5, "[%s] index=%d\n", ctx->name, b->index);
  320. return 0;
  321. }
  322. int dvb_vb2_dqbuf(struct dvb_vb2_ctx *ctx, struct dmx_buffer *b)
  323. {
  324. unsigned long flags;
  325. int ret;
  326. ret = vb2_core_dqbuf(&ctx->vb_q, &b->index, b, ctx->nonblocking);
  327. if (ret) {
  328. dprintk(1, "[%s] errno=%d\n", ctx->name, ret);
  329. return ret;
  330. }
  331. spin_lock_irqsave(&ctx->slock, flags);
  332. b->count = ctx->count++;
  333. b->flags = ctx->flags;
  334. ctx->flags = 0;
  335. spin_unlock_irqrestore(&ctx->slock, flags);
  336. dprintk(5, "[%s] index=%d, count=%d, flags=%d\n",
  337. ctx->name, b->index, ctx->count, b->flags);
  338. return 0;
  339. }
  340. int dvb_vb2_mmap(struct dvb_vb2_ctx *ctx, struct vm_area_struct *vma)
  341. {
  342. int ret;
  343. ret = vb2_mmap(&ctx->vb_q, vma);
  344. if (ret) {
  345. dprintk(1, "[%s] errno=%d\n", ctx->name, ret);
  346. return ret;
  347. }
  348. dprintk(3, "[%s] ret=%d\n", ctx->name, ret);
  349. return 0;
  350. }
  351. __poll_t dvb_vb2_poll(struct dvb_vb2_ctx *ctx, struct file *file,
  352. poll_table *wait)
  353. {
  354. dprintk(3, "[%s]\n", ctx->name);
  355. return vb2_core_poll(&ctx->vb_q, file, wait);
  356. }