dvb_vb2.c 10.0 KB

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