dvb_vb2.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  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 void _dmxdev_lock(struct vb2_queue *vq)
  84. {
  85. struct dvb_vb2_ctx *ctx = vb2_get_drv_priv(vq);
  86. mutex_lock(&ctx->mutex);
  87. dprintk(3, "[%s]\n", ctx->name);
  88. }
  89. static void _dmxdev_unlock(struct vb2_queue *vq)
  90. {
  91. struct dvb_vb2_ctx *ctx = vb2_get_drv_priv(vq);
  92. if (mutex_is_locked(&ctx->mutex))
  93. mutex_unlock(&ctx->mutex);
  94. dprintk(3, "[%s]\n", ctx->name);
  95. }
  96. static const struct vb2_ops dvb_vb2_qops = {
  97. .queue_setup = _queue_setup,
  98. .buf_prepare = _buffer_prepare,
  99. .buf_queue = _buffer_queue,
  100. .start_streaming = _start_streaming,
  101. .stop_streaming = _stop_streaming,
  102. .wait_prepare = _dmxdev_unlock,
  103. .wait_finish = _dmxdev_lock,
  104. };
  105. static void _fill_dmx_buffer(struct vb2_buffer *vb, void *pb)
  106. {
  107. struct dvb_vb2_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
  108. struct dmx_buffer *b = pb;
  109. b->index = vb->index;
  110. b->length = vb->planes[0].length;
  111. b->bytesused = vb->planes[0].bytesused;
  112. b->offset = vb->planes[0].m.offset;
  113. dprintk(3, "[%s]\n", ctx->name);
  114. }
  115. static int _fill_vb2_buffer(struct vb2_buffer *vb, struct vb2_plane *planes)
  116. {
  117. struct dvb_vb2_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
  118. planes[0].bytesused = 0;
  119. dprintk(3, "[%s]\n", ctx->name);
  120. return 0;
  121. }
  122. static const struct vb2_buf_ops dvb_vb2_buf_ops = {
  123. .fill_user_buffer = _fill_dmx_buffer,
  124. .fill_vb2_buffer = _fill_vb2_buffer,
  125. };
  126. /*
  127. * Videobuf operations
  128. */
  129. int dvb_vb2_init(struct dvb_vb2_ctx *ctx, const char *name, int nonblocking)
  130. {
  131. struct vb2_queue *q = &ctx->vb_q;
  132. int ret;
  133. memset(ctx, 0, sizeof(struct dvb_vb2_ctx));
  134. q->type = DVB_BUF_TYPE_CAPTURE;
  135. /**only mmap is supported currently*/
  136. q->io_modes = VB2_MMAP;
  137. q->drv_priv = ctx;
  138. q->buf_struct_size = sizeof(struct dvb_buffer);
  139. q->min_queued_buffers = 1;
  140. q->ops = &dvb_vb2_qops;
  141. q->mem_ops = &vb2_vmalloc_memops;
  142. q->buf_ops = &dvb_vb2_buf_ops;
  143. ret = vb2_core_queue_init(q);
  144. if (ret) {
  145. ctx->state = DVB_VB2_STATE_NONE;
  146. dprintk(1, "[%s] errno=%d\n", ctx->name, ret);
  147. return ret;
  148. }
  149. mutex_init(&ctx->mutex);
  150. spin_lock_init(&ctx->slock);
  151. INIT_LIST_HEAD(&ctx->dvb_q);
  152. strscpy(ctx->name, name, DVB_VB2_NAME_MAX);
  153. ctx->nonblocking = nonblocking;
  154. ctx->state = DVB_VB2_STATE_INIT;
  155. dprintk(3, "[%s]\n", ctx->name);
  156. return 0;
  157. }
  158. int dvb_vb2_release(struct dvb_vb2_ctx *ctx)
  159. {
  160. struct vb2_queue *q = (struct vb2_queue *)&ctx->vb_q;
  161. if (ctx->state & DVB_VB2_STATE_INIT)
  162. vb2_core_queue_release(q);
  163. ctx->state = DVB_VB2_STATE_NONE;
  164. dprintk(3, "[%s]\n", ctx->name);
  165. return 0;
  166. }
  167. int dvb_vb2_stream_on(struct dvb_vb2_ctx *ctx)
  168. {
  169. struct vb2_queue *q = &ctx->vb_q;
  170. int ret;
  171. ret = vb2_core_streamon(q, q->type);
  172. if (ret) {
  173. ctx->state = DVB_VB2_STATE_NONE;
  174. dprintk(1, "[%s] errno=%d\n", ctx->name, ret);
  175. return ret;
  176. }
  177. ctx->state |= DVB_VB2_STATE_STREAMON;
  178. dprintk(3, "[%s]\n", ctx->name);
  179. return 0;
  180. }
  181. int dvb_vb2_stream_off(struct dvb_vb2_ctx *ctx)
  182. {
  183. struct vb2_queue *q = (struct vb2_queue *)&ctx->vb_q;
  184. int ret;
  185. ctx->state &= ~DVB_VB2_STATE_STREAMON;
  186. ret = vb2_core_streamoff(q, q->type);
  187. if (ret) {
  188. ctx->state = DVB_VB2_STATE_NONE;
  189. dprintk(1, "[%s] errno=%d\n", ctx->name, ret);
  190. return ret;
  191. }
  192. dprintk(3, "[%s]\n", ctx->name);
  193. return 0;
  194. }
  195. int dvb_vb2_is_streaming(struct dvb_vb2_ctx *ctx)
  196. {
  197. return (ctx->state & DVB_VB2_STATE_STREAMON);
  198. }
  199. int dvb_vb2_fill_buffer(struct dvb_vb2_ctx *ctx,
  200. const unsigned char *src, int len,
  201. enum dmx_buffer_flags *buffer_flags)
  202. {
  203. unsigned long flags = 0;
  204. void *vbuf = NULL;
  205. int todo = len;
  206. unsigned char *psrc = (unsigned char *)src;
  207. int ll = 0;
  208. /*
  209. * normal case: This func is called twice from demux driver
  210. * one with valid src pointer, second time with NULL pointer
  211. */
  212. if (!src || !len)
  213. return 0;
  214. spin_lock_irqsave(&ctx->slock, flags);
  215. if (buffer_flags && *buffer_flags) {
  216. ctx->flags |= *buffer_flags;
  217. *buffer_flags = 0;
  218. }
  219. while (todo) {
  220. if (!ctx->buf) {
  221. if (list_empty(&ctx->dvb_q)) {
  222. dprintk(3, "[%s] Buffer overflow!!!\n",
  223. ctx->name);
  224. break;
  225. }
  226. ctx->buf = list_entry(ctx->dvb_q.next,
  227. struct dvb_buffer, list);
  228. ctx->remain = vb2_plane_size(&ctx->buf->vb, 0);
  229. ctx->offset = 0;
  230. }
  231. if (!dvb_vb2_is_streaming(ctx)) {
  232. vb2_buffer_done(&ctx->buf->vb, VB2_BUF_STATE_ERROR);
  233. list_del(&ctx->buf->list);
  234. ctx->buf = NULL;
  235. break;
  236. }
  237. /* Fill buffer */
  238. ll = min(todo, ctx->remain);
  239. vbuf = vb2_plane_vaddr(&ctx->buf->vb, 0);
  240. memcpy(vbuf + ctx->offset, psrc, ll);
  241. todo -= ll;
  242. psrc += ll;
  243. ctx->remain -= ll;
  244. ctx->offset += ll;
  245. if (ctx->remain == 0) {
  246. vb2_buffer_done(&ctx->buf->vb, VB2_BUF_STATE_DONE);
  247. list_del(&ctx->buf->list);
  248. ctx->buf = NULL;
  249. }
  250. }
  251. if (ctx->nonblocking && ctx->buf) {
  252. vb2_set_plane_payload(&ctx->buf->vb, 0, ll);
  253. vb2_buffer_done(&ctx->buf->vb, VB2_BUF_STATE_DONE);
  254. list_del(&ctx->buf->list);
  255. ctx->buf = NULL;
  256. }
  257. spin_unlock_irqrestore(&ctx->slock, flags);
  258. if (todo)
  259. dprintk(1, "[%s] %d bytes are dropped.\n", ctx->name, todo);
  260. else
  261. dprintk(3, "[%s]\n", ctx->name);
  262. dprintk(3, "[%s] %d bytes are copied\n", ctx->name, len - todo);
  263. return (len - todo);
  264. }
  265. int dvb_vb2_reqbufs(struct dvb_vb2_ctx *ctx, struct dmx_requestbuffers *req)
  266. {
  267. int ret;
  268. /* Adjust size to a sane value */
  269. if (req->size > DVB_V2_MAX_SIZE)
  270. req->size = DVB_V2_MAX_SIZE;
  271. /* FIXME: round req->size to a 188 or 204 multiple */
  272. ctx->buf_siz = req->size;
  273. ctx->buf_cnt = req->count;
  274. ret = vb2_core_reqbufs(&ctx->vb_q, VB2_MEMORY_MMAP, 0, &req->count);
  275. if (ret) {
  276. ctx->state = DVB_VB2_STATE_NONE;
  277. dprintk(1, "[%s] count=%d size=%d errno=%d\n", ctx->name,
  278. ctx->buf_cnt, ctx->buf_siz, ret);
  279. return ret;
  280. }
  281. ctx->state |= DVB_VB2_STATE_REQBUFS;
  282. dprintk(3, "[%s] count=%d size=%d\n", ctx->name,
  283. ctx->buf_cnt, ctx->buf_siz);
  284. return 0;
  285. }
  286. int dvb_vb2_querybuf(struct dvb_vb2_ctx *ctx, struct dmx_buffer *b)
  287. {
  288. struct vb2_queue *q = &ctx->vb_q;
  289. struct vb2_buffer *vb2 = vb2_get_buffer(q, b->index);
  290. if (!vb2) {
  291. dprintk(1, "[%s] invalid buffer index\n", ctx->name);
  292. return -EINVAL;
  293. }
  294. vb2_core_querybuf(&ctx->vb_q, vb2, b);
  295. dprintk(3, "[%s] index=%d\n", ctx->name, b->index);
  296. return 0;
  297. }
  298. int dvb_vb2_expbuf(struct dvb_vb2_ctx *ctx, struct dmx_exportbuffer *exp)
  299. {
  300. struct vb2_queue *q = &ctx->vb_q;
  301. struct vb2_buffer *vb2 = vb2_get_buffer(q, exp->index);
  302. int ret;
  303. if (!vb2) {
  304. dprintk(1, "[%s] invalid buffer index\n", ctx->name);
  305. return -EINVAL;
  306. }
  307. ret = vb2_core_expbuf(&ctx->vb_q, &exp->fd, q->type, vb2,
  308. 0, exp->flags);
  309. if (ret) {
  310. dprintk(1, "[%s] index=%d errno=%d\n", ctx->name,
  311. exp->index, ret);
  312. return ret;
  313. }
  314. dprintk(3, "[%s] index=%d fd=%d\n", ctx->name, exp->index, exp->fd);
  315. return 0;
  316. }
  317. int dvb_vb2_qbuf(struct dvb_vb2_ctx *ctx, struct dmx_buffer *b)
  318. {
  319. struct vb2_queue *q = &ctx->vb_q;
  320. struct vb2_buffer *vb2 = vb2_get_buffer(q, b->index);
  321. int ret;
  322. if (!vb2) {
  323. dprintk(1, "[%s] invalid buffer index\n", ctx->name);
  324. return -EINVAL;
  325. }
  326. ret = vb2_core_qbuf(&ctx->vb_q, vb2, b, NULL);
  327. if (ret) {
  328. dprintk(1, "[%s] index=%d errno=%d\n", ctx->name,
  329. b->index, ret);
  330. return ret;
  331. }
  332. dprintk(5, "[%s] index=%d\n", ctx->name, b->index);
  333. return 0;
  334. }
  335. int dvb_vb2_dqbuf(struct dvb_vb2_ctx *ctx, struct dmx_buffer *b)
  336. {
  337. unsigned long flags;
  338. int ret;
  339. ret = vb2_core_dqbuf(&ctx->vb_q, &b->index, b, ctx->nonblocking);
  340. if (ret) {
  341. dprintk(1, "[%s] errno=%d\n", ctx->name, ret);
  342. return ret;
  343. }
  344. spin_lock_irqsave(&ctx->slock, flags);
  345. b->count = ctx->count++;
  346. b->flags = ctx->flags;
  347. ctx->flags = 0;
  348. spin_unlock_irqrestore(&ctx->slock, flags);
  349. dprintk(5, "[%s] index=%d, count=%d, flags=%d\n",
  350. ctx->name, b->index, ctx->count, b->flags);
  351. return 0;
  352. }
  353. int dvb_vb2_mmap(struct dvb_vb2_ctx *ctx, struct vm_area_struct *vma)
  354. {
  355. int ret;
  356. ret = vb2_mmap(&ctx->vb_q, vma);
  357. if (ret) {
  358. dprintk(1, "[%s] errno=%d\n", ctx->name, ret);
  359. return ret;
  360. }
  361. dprintk(3, "[%s] ret=%d\n", ctx->name, ret);
  362. return 0;
  363. }
  364. __poll_t dvb_vb2_poll(struct dvb_vb2_ctx *ctx, struct file *file,
  365. poll_table *wait)
  366. {
  367. dprintk(3, "[%s]\n", ctx->name);
  368. return vb2_core_poll(&ctx->vb_q, file, wait);
  369. }