videobuf2-core.c 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112
  1. /*
  2. * videobuf2-core.c - video buffer 2 core framework
  3. *
  4. * Copyright (C) 2010 Samsung Electronics
  5. *
  6. * Author: Pawel Osciak <pawel@osciak.com>
  7. * Marek Szyprowski <m.szyprowski@samsung.com>
  8. *
  9. * The vb2_thread implementation was based on code from videobuf-dvb.c:
  10. * (c) 2004 Gerd Knorr <kraxel@bytesex.org> [SUSE Labs]
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation.
  15. */
  16. #include <linux/err.h>
  17. #include <linux/kernel.h>
  18. #include <linux/module.h>
  19. #include <linux/mm.h>
  20. #include <linux/poll.h>
  21. #include <linux/slab.h>
  22. #include <linux/sched.h>
  23. #include <linux/freezer.h>
  24. #include <linux/kthread.h>
  25. #include <media/videobuf2-core.h>
  26. #include <trace/events/vb2.h>
  27. #include "videobuf2-internal.h"
  28. int vb2_debug;
  29. EXPORT_SYMBOL_GPL(vb2_debug);
  30. module_param_named(debug, vb2_debug, int, 0644);
  31. static void __vb2_queue_cancel(struct vb2_queue *q);
  32. static void __enqueue_in_driver(struct vb2_buffer *vb);
  33. /**
  34. * __vb2_buf_mem_alloc() - allocate video memory for the given buffer
  35. */
  36. static int __vb2_buf_mem_alloc(struct vb2_buffer *vb)
  37. {
  38. struct vb2_queue *q = vb->vb2_queue;
  39. enum dma_data_direction dma_dir =
  40. q->is_output ? DMA_TO_DEVICE : DMA_FROM_DEVICE;
  41. void *mem_priv;
  42. int plane;
  43. /*
  44. * Allocate memory for all planes in this buffer
  45. * NOTE: mmapped areas should be page aligned
  46. */
  47. for (plane = 0; plane < vb->num_planes; ++plane) {
  48. unsigned long size = PAGE_ALIGN(q->plane_sizes[plane]);
  49. mem_priv = call_ptr_memop(vb, alloc, q->alloc_ctx[plane],
  50. size, dma_dir, q->gfp_flags);
  51. if (IS_ERR_OR_NULL(mem_priv))
  52. goto free;
  53. /* Associate allocator private data with this plane */
  54. vb->planes[plane].mem_priv = mem_priv;
  55. vb->planes[plane].length = q->plane_sizes[plane];
  56. }
  57. return 0;
  58. free:
  59. /* Free already allocated memory if one of the allocations failed */
  60. for (; plane > 0; --plane) {
  61. call_void_memop(vb, put, vb->planes[plane - 1].mem_priv);
  62. vb->planes[plane - 1].mem_priv = NULL;
  63. }
  64. return -ENOMEM;
  65. }
  66. /**
  67. * __vb2_buf_mem_free() - free memory of the given buffer
  68. */
  69. static void __vb2_buf_mem_free(struct vb2_buffer *vb)
  70. {
  71. unsigned int plane;
  72. for (plane = 0; plane < vb->num_planes; ++plane) {
  73. call_void_memop(vb, put, vb->planes[plane].mem_priv);
  74. vb->planes[plane].mem_priv = NULL;
  75. dprintk(3, "freed plane %d of buffer %d\n", plane, vb->index);
  76. }
  77. }
  78. /**
  79. * __vb2_buf_userptr_put() - release userspace memory associated with
  80. * a USERPTR buffer
  81. */
  82. static void __vb2_buf_userptr_put(struct vb2_buffer *vb)
  83. {
  84. unsigned int plane;
  85. for (plane = 0; plane < vb->num_planes; ++plane) {
  86. if (vb->planes[plane].mem_priv)
  87. call_void_memop(vb, put_userptr, vb->planes[plane].mem_priv);
  88. vb->planes[plane].mem_priv = NULL;
  89. }
  90. }
  91. /**
  92. * __vb2_plane_dmabuf_put() - release memory associated with
  93. * a DMABUF shared plane
  94. */
  95. static void __vb2_plane_dmabuf_put(struct vb2_buffer *vb, struct vb2_plane *p)
  96. {
  97. if (!p->mem_priv)
  98. return;
  99. if (p->dbuf_mapped)
  100. call_void_memop(vb, unmap_dmabuf, p->mem_priv);
  101. call_void_memop(vb, detach_dmabuf, p->mem_priv);
  102. dma_buf_put(p->dbuf);
  103. p->mem_priv = NULL;
  104. p->dbuf = NULL;
  105. p->dbuf_mapped = 0;
  106. }
  107. /**
  108. * __vb2_buf_dmabuf_put() - release memory associated with
  109. * a DMABUF shared buffer
  110. */
  111. static void __vb2_buf_dmabuf_put(struct vb2_buffer *vb)
  112. {
  113. unsigned int plane;
  114. for (plane = 0; plane < vb->num_planes; ++plane)
  115. __vb2_plane_dmabuf_put(vb, &vb->planes[plane]);
  116. }
  117. /**
  118. * __setup_lengths() - setup initial lengths for every plane in
  119. * every buffer on the queue
  120. */
  121. static void __setup_lengths(struct vb2_queue *q, unsigned int n)
  122. {
  123. unsigned int buffer, plane;
  124. struct vb2_buffer *vb;
  125. for (buffer = q->num_buffers; buffer < q->num_buffers + n; ++buffer) {
  126. vb = q->bufs[buffer];
  127. if (!vb)
  128. continue;
  129. for (plane = 0; plane < vb->num_planes; ++plane)
  130. vb->planes[plane].length = q->plane_sizes[plane];
  131. }
  132. }
  133. /**
  134. * __setup_offsets() - setup unique offsets ("cookies") for every plane in
  135. * every buffer on the queue
  136. */
  137. static void __setup_offsets(struct vb2_queue *q, unsigned int n)
  138. {
  139. unsigned int buffer, plane;
  140. struct vb2_buffer *vb;
  141. unsigned long off;
  142. if (q->num_buffers) {
  143. struct vb2_plane *p;
  144. vb = q->bufs[q->num_buffers - 1];
  145. p = &vb->planes[vb->num_planes - 1];
  146. off = PAGE_ALIGN(p->m.offset + p->length);
  147. } else {
  148. off = 0;
  149. }
  150. for (buffer = q->num_buffers; buffer < q->num_buffers + n; ++buffer) {
  151. vb = q->bufs[buffer];
  152. if (!vb)
  153. continue;
  154. for (plane = 0; plane < vb->num_planes; ++plane) {
  155. vb->planes[plane].m.offset = off;
  156. dprintk(3, "buffer %d, plane %d offset 0x%08lx\n",
  157. buffer, plane, off);
  158. off += vb->planes[plane].length;
  159. off = PAGE_ALIGN(off);
  160. }
  161. }
  162. }
  163. /**
  164. * __vb2_queue_alloc() - allocate videobuf buffer structures and (for MMAP type)
  165. * video buffer memory for all buffers/planes on the queue and initializes the
  166. * queue
  167. *
  168. * Returns the number of buffers successfully allocated.
  169. */
  170. static int __vb2_queue_alloc(struct vb2_queue *q, enum vb2_memory memory,
  171. unsigned int num_buffers, unsigned int num_planes)
  172. {
  173. unsigned int buffer;
  174. struct vb2_buffer *vb;
  175. int ret;
  176. /* Ensure that q->num_buffers+num_buffers is below VB2_MAX_FRAME */
  177. num_buffers = min_t(unsigned int, num_buffers,
  178. VB2_MAX_FRAME - q->num_buffers);
  179. for (buffer = 0; buffer < num_buffers; ++buffer) {
  180. /* Allocate videobuf buffer structures */
  181. vb = kzalloc(q->buf_struct_size, GFP_KERNEL);
  182. if (!vb) {
  183. dprintk(1, "memory alloc for buffer struct failed\n");
  184. break;
  185. }
  186. vb->state = VB2_BUF_STATE_DEQUEUED;
  187. vb->vb2_queue = q;
  188. vb->num_planes = num_planes;
  189. vb->index = q->num_buffers + buffer;
  190. vb->type = q->type;
  191. vb->memory = memory;
  192. /* Allocate video buffer memory for the MMAP type */
  193. if (memory == VB2_MEMORY_MMAP) {
  194. ret = __vb2_buf_mem_alloc(vb);
  195. if (ret) {
  196. dprintk(1, "failed allocating memory for "
  197. "buffer %d\n", buffer);
  198. kfree(vb);
  199. break;
  200. }
  201. /*
  202. * Call the driver-provided buffer initialization
  203. * callback, if given. An error in initialization
  204. * results in queue setup failure.
  205. */
  206. ret = call_vb_qop(vb, buf_init, vb);
  207. if (ret) {
  208. dprintk(1, "buffer %d %p initialization"
  209. " failed\n", buffer, vb);
  210. __vb2_buf_mem_free(vb);
  211. kfree(vb);
  212. break;
  213. }
  214. }
  215. q->bufs[q->num_buffers + buffer] = vb;
  216. }
  217. __setup_lengths(q, buffer);
  218. if (memory == VB2_MEMORY_MMAP)
  219. __setup_offsets(q, buffer);
  220. dprintk(1, "allocated %d buffers, %d plane(s) each\n",
  221. buffer, num_planes);
  222. return buffer;
  223. }
  224. /**
  225. * __vb2_free_mem() - release all video buffer memory for a given queue
  226. */
  227. static void __vb2_free_mem(struct vb2_queue *q, unsigned int buffers)
  228. {
  229. unsigned int buffer;
  230. struct vb2_buffer *vb;
  231. for (buffer = q->num_buffers - buffers; buffer < q->num_buffers;
  232. ++buffer) {
  233. vb = q->bufs[buffer];
  234. if (!vb)
  235. continue;
  236. /* Free MMAP buffers or release USERPTR buffers */
  237. if (q->memory == VB2_MEMORY_MMAP)
  238. __vb2_buf_mem_free(vb);
  239. else if (q->memory == VB2_MEMORY_DMABUF)
  240. __vb2_buf_dmabuf_put(vb);
  241. else
  242. __vb2_buf_userptr_put(vb);
  243. }
  244. }
  245. /**
  246. * __vb2_queue_free() - free buffers at the end of the queue - video memory and
  247. * related information, if no buffers are left return the queue to an
  248. * uninitialized state. Might be called even if the queue has already been freed.
  249. */
  250. static int __vb2_queue_free(struct vb2_queue *q, unsigned int buffers)
  251. {
  252. unsigned int buffer;
  253. /*
  254. * Sanity check: when preparing a buffer the queue lock is released for
  255. * a short while (see __buf_prepare for the details), which would allow
  256. * a race with a reqbufs which can call this function. Removing the
  257. * buffers from underneath __buf_prepare is obviously a bad idea, so we
  258. * check if any of the buffers is in the state PREPARING, and if so we
  259. * just return -EAGAIN.
  260. */
  261. for (buffer = q->num_buffers - buffers; buffer < q->num_buffers;
  262. ++buffer) {
  263. if (q->bufs[buffer] == NULL)
  264. continue;
  265. if (q->bufs[buffer]->state == VB2_BUF_STATE_PREPARING) {
  266. dprintk(1, "preparing buffers, cannot free\n");
  267. return -EAGAIN;
  268. }
  269. }
  270. /* Call driver-provided cleanup function for each buffer, if provided */
  271. for (buffer = q->num_buffers - buffers; buffer < q->num_buffers;
  272. ++buffer) {
  273. struct vb2_buffer *vb = q->bufs[buffer];
  274. if (vb && vb->planes[0].mem_priv)
  275. call_void_vb_qop(vb, buf_cleanup, vb);
  276. }
  277. /* Release video buffer memory */
  278. __vb2_free_mem(q, buffers);
  279. #ifdef CONFIG_VIDEO_ADV_DEBUG
  280. /*
  281. * Check that all the calls were balances during the life-time of this
  282. * queue. If not (or if the debug level is 1 or up), then dump the
  283. * counters to the kernel log.
  284. */
  285. if (q->num_buffers) {
  286. bool unbalanced = q->cnt_start_streaming != q->cnt_stop_streaming ||
  287. q->cnt_wait_prepare != q->cnt_wait_finish;
  288. if (unbalanced || vb2_debug) {
  289. pr_info("vb2: counters for queue %p:%s\n", q,
  290. unbalanced ? " UNBALANCED!" : "");
  291. pr_info("vb2: setup: %u start_streaming: %u stop_streaming: %u\n",
  292. q->cnt_queue_setup, q->cnt_start_streaming,
  293. q->cnt_stop_streaming);
  294. pr_info("vb2: wait_prepare: %u wait_finish: %u\n",
  295. q->cnt_wait_prepare, q->cnt_wait_finish);
  296. }
  297. q->cnt_queue_setup = 0;
  298. q->cnt_wait_prepare = 0;
  299. q->cnt_wait_finish = 0;
  300. q->cnt_start_streaming = 0;
  301. q->cnt_stop_streaming = 0;
  302. }
  303. for (buffer = 0; buffer < q->num_buffers; ++buffer) {
  304. struct vb2_buffer *vb = q->bufs[buffer];
  305. bool unbalanced = vb->cnt_mem_alloc != vb->cnt_mem_put ||
  306. vb->cnt_mem_prepare != vb->cnt_mem_finish ||
  307. vb->cnt_mem_get_userptr != vb->cnt_mem_put_userptr ||
  308. vb->cnt_mem_attach_dmabuf != vb->cnt_mem_detach_dmabuf ||
  309. vb->cnt_mem_map_dmabuf != vb->cnt_mem_unmap_dmabuf ||
  310. vb->cnt_buf_queue != vb->cnt_buf_done ||
  311. vb->cnt_buf_prepare != vb->cnt_buf_finish ||
  312. vb->cnt_buf_init != vb->cnt_buf_cleanup;
  313. if (unbalanced || vb2_debug) {
  314. pr_info("vb2: counters for queue %p, buffer %d:%s\n",
  315. q, buffer, unbalanced ? " UNBALANCED!" : "");
  316. pr_info("vb2: buf_init: %u buf_cleanup: %u buf_prepare: %u buf_finish: %u\n",
  317. vb->cnt_buf_init, vb->cnt_buf_cleanup,
  318. vb->cnt_buf_prepare, vb->cnt_buf_finish);
  319. pr_info("vb2: buf_queue: %u buf_done: %u\n",
  320. vb->cnt_buf_queue, vb->cnt_buf_done);
  321. pr_info("vb2: alloc: %u put: %u prepare: %u finish: %u mmap: %u\n",
  322. vb->cnt_mem_alloc, vb->cnt_mem_put,
  323. vb->cnt_mem_prepare, vb->cnt_mem_finish,
  324. vb->cnt_mem_mmap);
  325. pr_info("vb2: get_userptr: %u put_userptr: %u\n",
  326. vb->cnt_mem_get_userptr, vb->cnt_mem_put_userptr);
  327. pr_info("vb2: attach_dmabuf: %u detach_dmabuf: %u map_dmabuf: %u unmap_dmabuf: %u\n",
  328. vb->cnt_mem_attach_dmabuf, vb->cnt_mem_detach_dmabuf,
  329. vb->cnt_mem_map_dmabuf, vb->cnt_mem_unmap_dmabuf);
  330. pr_info("vb2: get_dmabuf: %u num_users: %u vaddr: %u cookie: %u\n",
  331. vb->cnt_mem_get_dmabuf,
  332. vb->cnt_mem_num_users,
  333. vb->cnt_mem_vaddr,
  334. vb->cnt_mem_cookie);
  335. }
  336. }
  337. #endif
  338. /* Free videobuf buffers */
  339. for (buffer = q->num_buffers - buffers; buffer < q->num_buffers;
  340. ++buffer) {
  341. kfree(q->bufs[buffer]);
  342. q->bufs[buffer] = NULL;
  343. }
  344. q->num_buffers -= buffers;
  345. if (!q->num_buffers) {
  346. q->memory = 0;
  347. INIT_LIST_HEAD(&q->queued_list);
  348. }
  349. return 0;
  350. }
  351. /**
  352. * vb2_buffer_in_use() - return true if the buffer is in use and
  353. * the queue cannot be freed (by the means of REQBUFS(0)) call
  354. */
  355. bool vb2_buffer_in_use(struct vb2_queue *q, struct vb2_buffer *vb)
  356. {
  357. unsigned int plane;
  358. for (plane = 0; plane < vb->num_planes; ++plane) {
  359. void *mem_priv = vb->planes[plane].mem_priv;
  360. /*
  361. * If num_users() has not been provided, call_memop
  362. * will return 0, apparently nobody cares about this
  363. * case anyway. If num_users() returns more than 1,
  364. * we are not the only user of the plane's memory.
  365. */
  366. if (mem_priv && call_memop(vb, num_users, mem_priv) > 1)
  367. return true;
  368. }
  369. return false;
  370. }
  371. EXPORT_SYMBOL(vb2_buffer_in_use);
  372. /**
  373. * __buffers_in_use() - return true if any buffers on the queue are in use and
  374. * the queue cannot be freed (by the means of REQBUFS(0)) call
  375. */
  376. static bool __buffers_in_use(struct vb2_queue *q)
  377. {
  378. unsigned int buffer;
  379. for (buffer = 0; buffer < q->num_buffers; ++buffer) {
  380. if (vb2_buffer_in_use(q, q->bufs[buffer]))
  381. return true;
  382. }
  383. return false;
  384. }
  385. /**
  386. * vb2_core_querybuf() - query video buffer information
  387. * @q: videobuf queue
  388. * @index: id number of the buffer
  389. * @pb: buffer struct passed from userspace
  390. *
  391. * Should be called from vidioc_querybuf ioctl handler in driver.
  392. * The passed buffer should have been verified.
  393. * This function fills the relevant information for the userspace.
  394. *
  395. * The return values from this function are intended to be directly returned
  396. * from vidioc_querybuf handler in driver.
  397. */
  398. int vb2_core_querybuf(struct vb2_queue *q, unsigned int index, void *pb)
  399. {
  400. return call_bufop(q, fill_user_buffer, q->bufs[index], pb);
  401. }
  402. EXPORT_SYMBOL_GPL(vb2_core_querybuf);
  403. /**
  404. * __verify_userptr_ops() - verify that all memory operations required for
  405. * USERPTR queue type have been provided
  406. */
  407. static int __verify_userptr_ops(struct vb2_queue *q)
  408. {
  409. if (!(q->io_modes & VB2_USERPTR) || !q->mem_ops->get_userptr ||
  410. !q->mem_ops->put_userptr)
  411. return -EINVAL;
  412. return 0;
  413. }
  414. /**
  415. * __verify_mmap_ops() - verify that all memory operations required for
  416. * MMAP queue type have been provided
  417. */
  418. static int __verify_mmap_ops(struct vb2_queue *q)
  419. {
  420. if (!(q->io_modes & VB2_MMAP) || !q->mem_ops->alloc ||
  421. !q->mem_ops->put || !q->mem_ops->mmap)
  422. return -EINVAL;
  423. return 0;
  424. }
  425. /**
  426. * __verify_dmabuf_ops() - verify that all memory operations required for
  427. * DMABUF queue type have been provided
  428. */
  429. static int __verify_dmabuf_ops(struct vb2_queue *q)
  430. {
  431. if (!(q->io_modes & VB2_DMABUF) || !q->mem_ops->attach_dmabuf ||
  432. !q->mem_ops->detach_dmabuf || !q->mem_ops->map_dmabuf ||
  433. !q->mem_ops->unmap_dmabuf)
  434. return -EINVAL;
  435. return 0;
  436. }
  437. /**
  438. * vb2_verify_memory_type() - Check whether the memory type and buffer type
  439. * passed to a buffer operation are compatible with the queue.
  440. */
  441. int vb2_verify_memory_type(struct vb2_queue *q,
  442. enum vb2_memory memory, unsigned int type)
  443. {
  444. if (memory != VB2_MEMORY_MMAP && memory != VB2_MEMORY_USERPTR &&
  445. memory != VB2_MEMORY_DMABUF) {
  446. dprintk(1, "unsupported memory type\n");
  447. return -EINVAL;
  448. }
  449. if (type != q->type) {
  450. dprintk(1, "requested type is incorrect\n");
  451. return -EINVAL;
  452. }
  453. /*
  454. * Make sure all the required memory ops for given memory type
  455. * are available.
  456. */
  457. if (memory == VB2_MEMORY_MMAP && __verify_mmap_ops(q)) {
  458. dprintk(1, "MMAP for current setup unsupported\n");
  459. return -EINVAL;
  460. }
  461. if (memory == VB2_MEMORY_USERPTR && __verify_userptr_ops(q)) {
  462. dprintk(1, "USERPTR for current setup unsupported\n");
  463. return -EINVAL;
  464. }
  465. if (memory == VB2_MEMORY_DMABUF && __verify_dmabuf_ops(q)) {
  466. dprintk(1, "DMABUF for current setup unsupported\n");
  467. return -EINVAL;
  468. }
  469. /*
  470. * Place the busy tests at the end: -EBUSY can be ignored when
  471. * create_bufs is called with count == 0, but count == 0 should still
  472. * do the memory and type validation.
  473. */
  474. if (vb2_fileio_is_active(q)) {
  475. dprintk(1, "file io in progress\n");
  476. return -EBUSY;
  477. }
  478. return 0;
  479. }
  480. EXPORT_SYMBOL(vb2_verify_memory_type);
  481. /**
  482. * vb2_core_reqbufs() - Initiate streaming
  483. * @q: videobuf2 queue
  484. * @memory: memory type
  485. * @count: requested buffer count
  486. *
  487. * Should be called from vidioc_reqbufs ioctl handler of a driver.
  488. * This function:
  489. * 1) verifies streaming parameters passed from the userspace,
  490. * 2) sets up the queue,
  491. * 3) negotiates number of buffers and planes per buffer with the driver
  492. * to be used during streaming,
  493. * 4) allocates internal buffer structures (struct vb2_buffer), according to
  494. * the agreed parameters,
  495. * 5) for MMAP memory type, allocates actual video memory, using the
  496. * memory handling/allocation routines provided during queue initialization
  497. *
  498. * If req->count is 0, all the memory will be freed instead.
  499. * If the queue has been allocated previously (by a previous vb2_reqbufs) call
  500. * and the queue is not busy, memory will be reallocated.
  501. *
  502. * The return values from this function are intended to be directly returned
  503. * from vidioc_reqbufs handler in driver.
  504. */
  505. int vb2_core_reqbufs(struct vb2_queue *q, enum vb2_memory memory,
  506. unsigned int *count)
  507. {
  508. unsigned int num_buffers, allocated_buffers, num_planes = 0;
  509. int ret;
  510. if (q->streaming) {
  511. dprintk(1, "streaming active\n");
  512. return -EBUSY;
  513. }
  514. if (*count == 0 || q->num_buffers != 0 || q->memory != memory) {
  515. /*
  516. * We already have buffers allocated, so first check if they
  517. * are not in use and can be freed.
  518. */
  519. mutex_lock(&q->mmap_lock);
  520. if (q->memory == VB2_MEMORY_MMAP && __buffers_in_use(q)) {
  521. mutex_unlock(&q->mmap_lock);
  522. dprintk(1, "memory in use, cannot free\n");
  523. return -EBUSY;
  524. }
  525. /*
  526. * Call queue_cancel to clean up any buffers in the PREPARED or
  527. * QUEUED state which is possible if buffers were prepared or
  528. * queued without ever calling STREAMON.
  529. */
  530. __vb2_queue_cancel(q);
  531. ret = __vb2_queue_free(q, q->num_buffers);
  532. mutex_unlock(&q->mmap_lock);
  533. if (ret)
  534. return ret;
  535. /*
  536. * In case of REQBUFS(0) return immediately without calling
  537. * driver's queue_setup() callback and allocating resources.
  538. */
  539. if (*count == 0)
  540. return 0;
  541. }
  542. /*
  543. * Make sure the requested values and current defaults are sane.
  544. */
  545. num_buffers = min_t(unsigned int, *count, VB2_MAX_FRAME);
  546. num_buffers = max_t(unsigned int, num_buffers, q->min_buffers_needed);
  547. memset(q->plane_sizes, 0, sizeof(q->plane_sizes));
  548. memset(q->alloc_ctx, 0, sizeof(q->alloc_ctx));
  549. q->memory = memory;
  550. /*
  551. * Ask the driver how many buffers and planes per buffer it requires.
  552. * Driver also sets the size and allocator context for each plane.
  553. */
  554. ret = call_qop(q, queue_setup, q, NULL, &num_buffers, &num_planes,
  555. q->plane_sizes, q->alloc_ctx);
  556. if (ret)
  557. return ret;
  558. /* Finally, allocate buffers and video memory */
  559. allocated_buffers =
  560. __vb2_queue_alloc(q, memory, num_buffers, num_planes);
  561. if (allocated_buffers == 0) {
  562. dprintk(1, "memory allocation failed\n");
  563. return -ENOMEM;
  564. }
  565. /*
  566. * There is no point in continuing if we can't allocate the minimum
  567. * number of buffers needed by this vb2_queue.
  568. */
  569. if (allocated_buffers < q->min_buffers_needed)
  570. ret = -ENOMEM;
  571. /*
  572. * Check if driver can handle the allocated number of buffers.
  573. */
  574. if (!ret && allocated_buffers < num_buffers) {
  575. num_buffers = allocated_buffers;
  576. ret = call_qop(q, queue_setup, q, NULL, &num_buffers,
  577. &num_planes, q->plane_sizes, q->alloc_ctx);
  578. if (!ret && allocated_buffers < num_buffers)
  579. ret = -ENOMEM;
  580. /*
  581. * Either the driver has accepted a smaller number of buffers,
  582. * or .queue_setup() returned an error
  583. */
  584. }
  585. mutex_lock(&q->mmap_lock);
  586. q->num_buffers = allocated_buffers;
  587. if (ret < 0) {
  588. /*
  589. * Note: __vb2_queue_free() will subtract 'allocated_buffers'
  590. * from q->num_buffers.
  591. */
  592. __vb2_queue_free(q, allocated_buffers);
  593. mutex_unlock(&q->mmap_lock);
  594. return ret;
  595. }
  596. mutex_unlock(&q->mmap_lock);
  597. /*
  598. * Return the number of successfully allocated buffers
  599. * to the userspace.
  600. */
  601. *count = allocated_buffers;
  602. q->waiting_for_buffers = !q->is_output;
  603. return 0;
  604. }
  605. EXPORT_SYMBOL_GPL(vb2_core_reqbufs);
  606. /**
  607. * vb2_core_create_bufs() - Allocate buffers and any required auxiliary structs
  608. * @q: videobuf2 queue
  609. * @memory: memory type
  610. * @count: requested buffer count
  611. * @parg: parameter passed to device driver
  612. *
  613. * Should be called from vidioc_create_bufs ioctl handler of a driver.
  614. * This function:
  615. * 1) verifies parameter sanity
  616. * 2) calls the .queue_setup() queue operation
  617. * 3) performs any necessary memory allocations
  618. *
  619. * The return values from this function are intended to be directly returned
  620. * from vidioc_create_bufs handler in driver.
  621. */
  622. int vb2_core_create_bufs(struct vb2_queue *q, enum vb2_memory memory,
  623. unsigned int *count, const void *parg)
  624. {
  625. unsigned int num_planes = 0, num_buffers, allocated_buffers;
  626. int ret;
  627. if (q->num_buffers == VB2_MAX_FRAME) {
  628. dprintk(1, "maximum number of buffers already allocated\n");
  629. return -ENOBUFS;
  630. }
  631. if (!q->num_buffers) {
  632. memset(q->plane_sizes, 0, sizeof(q->plane_sizes));
  633. memset(q->alloc_ctx, 0, sizeof(q->alloc_ctx));
  634. q->memory = memory;
  635. q->waiting_for_buffers = !q->is_output;
  636. }
  637. num_buffers = min(*count, VB2_MAX_FRAME - q->num_buffers);
  638. /*
  639. * Ask the driver, whether the requested number of buffers, planes per
  640. * buffer and their sizes are acceptable
  641. */
  642. ret = call_qop(q, queue_setup, q, parg, &num_buffers,
  643. &num_planes, q->plane_sizes, q->alloc_ctx);
  644. if (ret)
  645. return ret;
  646. /* Finally, allocate buffers and video memory */
  647. allocated_buffers = __vb2_queue_alloc(q, memory, num_buffers,
  648. num_planes);
  649. if (allocated_buffers == 0) {
  650. dprintk(1, "memory allocation failed\n");
  651. return -ENOMEM;
  652. }
  653. /*
  654. * Check if driver can handle the so far allocated number of buffers.
  655. */
  656. if (allocated_buffers < num_buffers) {
  657. num_buffers = allocated_buffers;
  658. /*
  659. * q->num_buffers contains the total number of buffers, that the
  660. * queue driver has set up
  661. */
  662. ret = call_qop(q, queue_setup, q, parg, &num_buffers,
  663. &num_planes, q->plane_sizes, q->alloc_ctx);
  664. if (!ret && allocated_buffers < num_buffers)
  665. ret = -ENOMEM;
  666. /*
  667. * Either the driver has accepted a smaller number of buffers,
  668. * or .queue_setup() returned an error
  669. */
  670. }
  671. mutex_lock(&q->mmap_lock);
  672. q->num_buffers += allocated_buffers;
  673. if (ret < 0) {
  674. /*
  675. * Note: __vb2_queue_free() will subtract 'allocated_buffers'
  676. * from q->num_buffers.
  677. */
  678. __vb2_queue_free(q, allocated_buffers);
  679. mutex_unlock(&q->mmap_lock);
  680. return -ENOMEM;
  681. }
  682. mutex_unlock(&q->mmap_lock);
  683. /*
  684. * Return the number of successfully allocated buffers
  685. * to the userspace.
  686. */
  687. *count = allocated_buffers;
  688. return 0;
  689. }
  690. EXPORT_SYMBOL_GPL(vb2_core_create_bufs);
  691. /**
  692. * vb2_plane_vaddr() - Return a kernel virtual address of a given plane
  693. * @vb: vb2_buffer to which the plane in question belongs to
  694. * @plane_no: plane number for which the address is to be returned
  695. *
  696. * This function returns a kernel virtual address of a given plane if
  697. * such a mapping exist, NULL otherwise.
  698. */
  699. void *vb2_plane_vaddr(struct vb2_buffer *vb, unsigned int plane_no)
  700. {
  701. if (plane_no >= vb->num_planes || !vb->planes[plane_no].mem_priv)
  702. return NULL;
  703. return call_ptr_memop(vb, vaddr, vb->planes[plane_no].mem_priv);
  704. }
  705. EXPORT_SYMBOL_GPL(vb2_plane_vaddr);
  706. /**
  707. * vb2_plane_cookie() - Return allocator specific cookie for the given plane
  708. * @vb: vb2_buffer to which the plane in question belongs to
  709. * @plane_no: plane number for which the cookie is to be returned
  710. *
  711. * This function returns an allocator specific cookie for a given plane if
  712. * available, NULL otherwise. The allocator should provide some simple static
  713. * inline function, which would convert this cookie to the allocator specific
  714. * type that can be used directly by the driver to access the buffer. This can
  715. * be for example physical address, pointer to scatter list or IOMMU mapping.
  716. */
  717. void *vb2_plane_cookie(struct vb2_buffer *vb, unsigned int plane_no)
  718. {
  719. if (plane_no >= vb->num_planes || !vb->planes[plane_no].mem_priv)
  720. return NULL;
  721. return call_ptr_memop(vb, cookie, vb->planes[plane_no].mem_priv);
  722. }
  723. EXPORT_SYMBOL_GPL(vb2_plane_cookie);
  724. /**
  725. * vb2_buffer_done() - inform videobuf that an operation on a buffer is finished
  726. * @vb: vb2_buffer returned from the driver
  727. * @state: either VB2_BUF_STATE_DONE if the operation finished successfully,
  728. * VB2_BUF_STATE_ERROR if the operation finished with an error or
  729. * VB2_BUF_STATE_QUEUED if the driver wants to requeue buffers.
  730. * If start_streaming fails then it should return buffers with state
  731. * VB2_BUF_STATE_QUEUED to put them back into the queue.
  732. *
  733. * This function should be called by the driver after a hardware operation on
  734. * a buffer is finished and the buffer may be returned to userspace. The driver
  735. * cannot use this buffer anymore until it is queued back to it by videobuf
  736. * by the means of buf_queue callback. Only buffers previously queued to the
  737. * driver by buf_queue can be passed to this function.
  738. *
  739. * While streaming a buffer can only be returned in state DONE or ERROR.
  740. * The start_streaming op can also return them in case the DMA engine cannot
  741. * be started for some reason. In that case the buffers should be returned with
  742. * state QUEUED.
  743. */
  744. void vb2_buffer_done(struct vb2_buffer *vb, enum vb2_buffer_state state)
  745. {
  746. struct vb2_queue *q = vb->vb2_queue;
  747. unsigned long flags;
  748. unsigned int plane;
  749. if (WARN_ON(vb->state != VB2_BUF_STATE_ACTIVE))
  750. return;
  751. if (WARN_ON(state != VB2_BUF_STATE_DONE &&
  752. state != VB2_BUF_STATE_ERROR &&
  753. state != VB2_BUF_STATE_QUEUED &&
  754. state != VB2_BUF_STATE_REQUEUEING))
  755. state = VB2_BUF_STATE_ERROR;
  756. #ifdef CONFIG_VIDEO_ADV_DEBUG
  757. /*
  758. * Although this is not a callback, it still does have to balance
  759. * with the buf_queue op. So update this counter manually.
  760. */
  761. vb->cnt_buf_done++;
  762. #endif
  763. dprintk(4, "done processing on buffer %d, state: %d\n",
  764. vb->index, state);
  765. /* sync buffers */
  766. for (plane = 0; plane < vb->num_planes; ++plane)
  767. call_void_memop(vb, finish, vb->planes[plane].mem_priv);
  768. spin_lock_irqsave(&q->done_lock, flags);
  769. if (state == VB2_BUF_STATE_QUEUED ||
  770. state == VB2_BUF_STATE_REQUEUEING) {
  771. vb->state = VB2_BUF_STATE_QUEUED;
  772. } else {
  773. /* Add the buffer to the done buffers list */
  774. list_add_tail(&vb->done_entry, &q->done_list);
  775. vb->state = state;
  776. }
  777. atomic_dec(&q->owned_by_drv_count);
  778. spin_unlock_irqrestore(&q->done_lock, flags);
  779. trace_vb2_buf_done(q, vb);
  780. switch (state) {
  781. case VB2_BUF_STATE_QUEUED:
  782. return;
  783. case VB2_BUF_STATE_REQUEUEING:
  784. if (q->start_streaming_called)
  785. __enqueue_in_driver(vb);
  786. return;
  787. default:
  788. /* Inform any processes that may be waiting for buffers */
  789. wake_up(&q->done_wq);
  790. break;
  791. }
  792. }
  793. EXPORT_SYMBOL_GPL(vb2_buffer_done);
  794. /**
  795. * vb2_discard_done() - discard all buffers marked as DONE
  796. * @q: videobuf2 queue
  797. *
  798. * This function is intended to be used with suspend/resume operations. It
  799. * discards all 'done' buffers as they would be too old to be requested after
  800. * resume.
  801. *
  802. * Drivers must stop the hardware and synchronize with interrupt handlers and/or
  803. * delayed works before calling this function to make sure no buffer will be
  804. * touched by the driver and/or hardware.
  805. */
  806. void vb2_discard_done(struct vb2_queue *q)
  807. {
  808. struct vb2_buffer *vb;
  809. unsigned long flags;
  810. spin_lock_irqsave(&q->done_lock, flags);
  811. list_for_each_entry(vb, &q->done_list, done_entry)
  812. vb->state = VB2_BUF_STATE_ERROR;
  813. spin_unlock_irqrestore(&q->done_lock, flags);
  814. }
  815. EXPORT_SYMBOL_GPL(vb2_discard_done);
  816. /**
  817. * __qbuf_mmap() - handle qbuf of an MMAP buffer
  818. */
  819. static int __qbuf_mmap(struct vb2_buffer *vb, const void *pb)
  820. {
  821. int ret = call_bufop(vb->vb2_queue, fill_vb2_buffer,
  822. vb, pb, vb->planes);
  823. return ret ? ret : call_vb_qop(vb, buf_prepare, vb);
  824. }
  825. /**
  826. * __qbuf_userptr() - handle qbuf of a USERPTR buffer
  827. */
  828. static int __qbuf_userptr(struct vb2_buffer *vb, const void *pb)
  829. {
  830. struct vb2_plane planes[VB2_MAX_PLANES];
  831. struct vb2_queue *q = vb->vb2_queue;
  832. void *mem_priv;
  833. unsigned int plane;
  834. int ret;
  835. enum dma_data_direction dma_dir =
  836. q->is_output ? DMA_TO_DEVICE : DMA_FROM_DEVICE;
  837. bool reacquired = vb->planes[0].mem_priv == NULL;
  838. memset(planes, 0, sizeof(planes[0]) * vb->num_planes);
  839. /* Copy relevant information provided by the userspace */
  840. ret = call_bufop(vb->vb2_queue, fill_vb2_buffer, vb, pb, planes);
  841. if (ret)
  842. return ret;
  843. for (plane = 0; plane < vb->num_planes; ++plane) {
  844. /* Skip the plane if already verified */
  845. if (vb->planes[plane].m.userptr &&
  846. vb->planes[plane].m.userptr == planes[plane].m.userptr
  847. && vb->planes[plane].length == planes[plane].length)
  848. continue;
  849. dprintk(3, "userspace address for plane %d changed, "
  850. "reacquiring memory\n", plane);
  851. /* Check if the provided plane buffer is large enough */
  852. if (planes[plane].length < q->plane_sizes[plane]) {
  853. dprintk(1, "provided buffer size %u is less than "
  854. "setup size %u for plane %d\n",
  855. planes[plane].length,
  856. q->plane_sizes[plane], plane);
  857. ret = -EINVAL;
  858. goto err;
  859. }
  860. /* Release previously acquired memory if present */
  861. if (vb->planes[plane].mem_priv) {
  862. if (!reacquired) {
  863. reacquired = true;
  864. call_void_vb_qop(vb, buf_cleanup, vb);
  865. }
  866. call_void_memop(vb, put_userptr, vb->planes[plane].mem_priv);
  867. }
  868. vb->planes[plane].mem_priv = NULL;
  869. vb->planes[plane].bytesused = 0;
  870. vb->planes[plane].length = 0;
  871. vb->planes[plane].m.userptr = 0;
  872. vb->planes[plane].data_offset = 0;
  873. /* Acquire each plane's memory */
  874. mem_priv = call_ptr_memop(vb, get_userptr, q->alloc_ctx[plane],
  875. planes[plane].m.userptr,
  876. planes[plane].length, dma_dir);
  877. if (IS_ERR_OR_NULL(mem_priv)) {
  878. dprintk(1, "failed acquiring userspace "
  879. "memory for plane %d\n", plane);
  880. ret = mem_priv ? PTR_ERR(mem_priv) : -EINVAL;
  881. goto err;
  882. }
  883. vb->planes[plane].mem_priv = mem_priv;
  884. }
  885. /*
  886. * Now that everything is in order, copy relevant information
  887. * provided by userspace.
  888. */
  889. for (plane = 0; plane < vb->num_planes; ++plane) {
  890. vb->planes[plane].bytesused = planes[plane].bytesused;
  891. vb->planes[plane].length = planes[plane].length;
  892. vb->planes[plane].m.userptr = planes[plane].m.userptr;
  893. vb->planes[plane].data_offset = planes[plane].data_offset;
  894. }
  895. if (reacquired) {
  896. /*
  897. * One or more planes changed, so we must call buf_init to do
  898. * the driver-specific initialization on the newly acquired
  899. * buffer, if provided.
  900. */
  901. ret = call_vb_qop(vb, buf_init, vb);
  902. if (ret) {
  903. dprintk(1, "buffer initialization failed\n");
  904. goto err;
  905. }
  906. }
  907. ret = call_vb_qop(vb, buf_prepare, vb);
  908. if (ret) {
  909. dprintk(1, "buffer preparation failed\n");
  910. call_void_vb_qop(vb, buf_cleanup, vb);
  911. goto err;
  912. }
  913. return 0;
  914. err:
  915. /* In case of errors, release planes that were already acquired */
  916. for (plane = 0; plane < vb->num_planes; ++plane) {
  917. if (vb->planes[plane].mem_priv)
  918. call_void_memop(vb, put_userptr,
  919. vb->planes[plane].mem_priv);
  920. vb->planes[plane].mem_priv = NULL;
  921. vb->planes[plane].m.userptr = 0;
  922. vb->planes[plane].length = 0;
  923. }
  924. return ret;
  925. }
  926. /**
  927. * __qbuf_dmabuf() - handle qbuf of a DMABUF buffer
  928. */
  929. static int __qbuf_dmabuf(struct vb2_buffer *vb, const void *pb)
  930. {
  931. struct vb2_plane planes[VB2_MAX_PLANES];
  932. struct vb2_queue *q = vb->vb2_queue;
  933. void *mem_priv;
  934. unsigned int plane;
  935. int ret;
  936. enum dma_data_direction dma_dir =
  937. q->is_output ? DMA_TO_DEVICE : DMA_FROM_DEVICE;
  938. bool reacquired = vb->planes[0].mem_priv == NULL;
  939. memset(planes, 0, sizeof(planes[0]) * vb->num_planes);
  940. /* Copy relevant information provided by the userspace */
  941. ret = call_bufop(vb->vb2_queue, fill_vb2_buffer, vb, pb, planes);
  942. if (ret)
  943. return ret;
  944. for (plane = 0; plane < vb->num_planes; ++plane) {
  945. struct dma_buf *dbuf = dma_buf_get(planes[plane].m.fd);
  946. if (IS_ERR_OR_NULL(dbuf)) {
  947. dprintk(1, "invalid dmabuf fd for plane %d\n",
  948. plane);
  949. ret = -EINVAL;
  950. goto err;
  951. }
  952. /* use DMABUF size if length is not provided */
  953. if (planes[plane].length == 0)
  954. planes[plane].length = dbuf->size;
  955. if (planes[plane].length < q->plane_sizes[plane]) {
  956. dprintk(1, "invalid dmabuf length for plane %d\n",
  957. plane);
  958. ret = -EINVAL;
  959. goto err;
  960. }
  961. /* Skip the plane if already verified */
  962. if (dbuf == vb->planes[plane].dbuf &&
  963. vb->planes[plane].length == planes[plane].length) {
  964. dma_buf_put(dbuf);
  965. continue;
  966. }
  967. dprintk(1, "buffer for plane %d changed\n", plane);
  968. if (!reacquired) {
  969. reacquired = true;
  970. call_void_vb_qop(vb, buf_cleanup, vb);
  971. }
  972. /* Release previously acquired memory if present */
  973. __vb2_plane_dmabuf_put(vb, &vb->planes[plane]);
  974. vb->planes[plane].bytesused = 0;
  975. vb->planes[plane].length = 0;
  976. vb->planes[plane].m.fd = 0;
  977. vb->planes[plane].data_offset = 0;
  978. /* Acquire each plane's memory */
  979. mem_priv = call_ptr_memop(vb, attach_dmabuf,
  980. q->alloc_ctx[plane], dbuf, planes[plane].length,
  981. dma_dir);
  982. if (IS_ERR(mem_priv)) {
  983. dprintk(1, "failed to attach dmabuf\n");
  984. ret = PTR_ERR(mem_priv);
  985. dma_buf_put(dbuf);
  986. goto err;
  987. }
  988. vb->planes[plane].dbuf = dbuf;
  989. vb->planes[plane].mem_priv = mem_priv;
  990. }
  991. /* TODO: This pins the buffer(s) with dma_buf_map_attachment()).. but
  992. * really we want to do this just before the DMA, not while queueing
  993. * the buffer(s)..
  994. */
  995. for (plane = 0; plane < vb->num_planes; ++plane) {
  996. ret = call_memop(vb, map_dmabuf, vb->planes[plane].mem_priv);
  997. if (ret) {
  998. dprintk(1, "failed to map dmabuf for plane %d\n",
  999. plane);
  1000. goto err;
  1001. }
  1002. vb->planes[plane].dbuf_mapped = 1;
  1003. }
  1004. /*
  1005. * Now that everything is in order, copy relevant information
  1006. * provided by userspace.
  1007. */
  1008. for (plane = 0; plane < vb->num_planes; ++plane) {
  1009. vb->planes[plane].bytesused = planes[plane].bytesused;
  1010. vb->planes[plane].length = planes[plane].length;
  1011. vb->planes[plane].m.fd = planes[plane].m.fd;
  1012. vb->planes[plane].data_offset = planes[plane].data_offset;
  1013. }
  1014. if (reacquired) {
  1015. /*
  1016. * Call driver-specific initialization on the newly acquired buffer,
  1017. * if provided.
  1018. */
  1019. ret = call_vb_qop(vb, buf_init, vb);
  1020. if (ret) {
  1021. dprintk(1, "buffer initialization failed\n");
  1022. goto err;
  1023. }
  1024. }
  1025. ret = call_vb_qop(vb, buf_prepare, vb);
  1026. if (ret) {
  1027. dprintk(1, "buffer preparation failed\n");
  1028. call_void_vb_qop(vb, buf_cleanup, vb);
  1029. goto err;
  1030. }
  1031. return 0;
  1032. err:
  1033. /* In case of errors, release planes that were already acquired */
  1034. __vb2_buf_dmabuf_put(vb);
  1035. return ret;
  1036. }
  1037. /**
  1038. * __enqueue_in_driver() - enqueue a vb2_buffer in driver for processing
  1039. */
  1040. static void __enqueue_in_driver(struct vb2_buffer *vb)
  1041. {
  1042. struct vb2_queue *q = vb->vb2_queue;
  1043. unsigned int plane;
  1044. vb->state = VB2_BUF_STATE_ACTIVE;
  1045. atomic_inc(&q->owned_by_drv_count);
  1046. trace_vb2_buf_queue(q, vb);
  1047. /* sync buffers */
  1048. for (plane = 0; plane < vb->num_planes; ++plane)
  1049. call_void_memop(vb, prepare, vb->planes[plane].mem_priv);
  1050. call_void_vb_qop(vb, buf_queue, vb);
  1051. }
  1052. static int __buf_prepare(struct vb2_buffer *vb, const void *pb)
  1053. {
  1054. struct vb2_queue *q = vb->vb2_queue;
  1055. int ret;
  1056. if (q->error) {
  1057. dprintk(1, "fatal error occurred on queue\n");
  1058. return -EIO;
  1059. }
  1060. vb->state = VB2_BUF_STATE_PREPARING;
  1061. switch (q->memory) {
  1062. case VB2_MEMORY_MMAP:
  1063. ret = __qbuf_mmap(vb, pb);
  1064. break;
  1065. case VB2_MEMORY_USERPTR:
  1066. ret = __qbuf_userptr(vb, pb);
  1067. break;
  1068. case VB2_MEMORY_DMABUF:
  1069. ret = __qbuf_dmabuf(vb, pb);
  1070. break;
  1071. default:
  1072. WARN(1, "Invalid queue type\n");
  1073. ret = -EINVAL;
  1074. }
  1075. if (ret)
  1076. dprintk(1, "buffer preparation failed: %d\n", ret);
  1077. vb->state = ret ? VB2_BUF_STATE_DEQUEUED : VB2_BUF_STATE_PREPARED;
  1078. return ret;
  1079. }
  1080. /**
  1081. * vb2_core_prepare_buf() - Pass ownership of a buffer from userspace
  1082. * to the kernel
  1083. * @q: videobuf2 queue
  1084. * @index: id number of the buffer
  1085. * @pb: buffer structure passed from userspace to vidioc_prepare_buf
  1086. * handler in driver
  1087. *
  1088. * Should be called from vidioc_prepare_buf ioctl handler of a driver.
  1089. * The passed buffer should have been verified.
  1090. * This function calls buf_prepare callback in the driver (if provided),
  1091. * in which driver-specific buffer initialization can be performed,
  1092. *
  1093. * The return values from this function are intended to be directly returned
  1094. * from vidioc_prepare_buf handler in driver.
  1095. */
  1096. int vb2_core_prepare_buf(struct vb2_queue *q, unsigned int index, void *pb)
  1097. {
  1098. struct vb2_buffer *vb;
  1099. int ret;
  1100. vb = q->bufs[index];
  1101. if (vb->state != VB2_BUF_STATE_DEQUEUED) {
  1102. dprintk(1, "invalid buffer state %d\n",
  1103. vb->state);
  1104. return -EINVAL;
  1105. }
  1106. ret = __buf_prepare(vb, pb);
  1107. if (ret)
  1108. return ret;
  1109. /* Fill buffer information for the userspace */
  1110. ret = call_bufop(q, fill_user_buffer, vb, pb);
  1111. if (ret)
  1112. return ret;
  1113. dprintk(1, "prepare of buffer %d succeeded\n", vb->index);
  1114. return ret;
  1115. }
  1116. EXPORT_SYMBOL_GPL(vb2_core_prepare_buf);
  1117. /**
  1118. * vb2_start_streaming() - Attempt to start streaming.
  1119. * @q: videobuf2 queue
  1120. *
  1121. * Attempt to start streaming. When this function is called there must be
  1122. * at least q->min_buffers_needed buffers queued up (i.e. the minimum
  1123. * number of buffers required for the DMA engine to function). If the
  1124. * @start_streaming op fails it is supposed to return all the driver-owned
  1125. * buffers back to vb2 in state QUEUED. Check if that happened and if
  1126. * not warn and reclaim them forcefully.
  1127. */
  1128. static int vb2_start_streaming(struct vb2_queue *q)
  1129. {
  1130. struct vb2_buffer *vb;
  1131. int ret;
  1132. /*
  1133. * If any buffers were queued before streamon,
  1134. * we can now pass them to driver for processing.
  1135. */
  1136. list_for_each_entry(vb, &q->queued_list, queued_entry)
  1137. __enqueue_in_driver(vb);
  1138. /* Tell the driver to start streaming */
  1139. q->start_streaming_called = 1;
  1140. ret = call_qop(q, start_streaming, q,
  1141. atomic_read(&q->owned_by_drv_count));
  1142. if (!ret)
  1143. return 0;
  1144. q->start_streaming_called = 0;
  1145. dprintk(1, "driver refused to start streaming\n");
  1146. /*
  1147. * If you see this warning, then the driver isn't cleaning up properly
  1148. * after a failed start_streaming(). See the start_streaming()
  1149. * documentation in videobuf2-core.h for more information how buffers
  1150. * should be returned to vb2 in start_streaming().
  1151. */
  1152. if (WARN_ON(atomic_read(&q->owned_by_drv_count))) {
  1153. unsigned i;
  1154. /*
  1155. * Forcefully reclaim buffers if the driver did not
  1156. * correctly return them to vb2.
  1157. */
  1158. for (i = 0; i < q->num_buffers; ++i) {
  1159. vb = q->bufs[i];
  1160. if (vb->state == VB2_BUF_STATE_ACTIVE)
  1161. vb2_buffer_done(vb, VB2_BUF_STATE_QUEUED);
  1162. }
  1163. /* Must be zero now */
  1164. WARN_ON(atomic_read(&q->owned_by_drv_count));
  1165. }
  1166. /*
  1167. * If done_list is not empty, then start_streaming() didn't call
  1168. * vb2_buffer_done(vb, VB2_BUF_STATE_QUEUED) but STATE_ERROR or
  1169. * STATE_DONE.
  1170. */
  1171. WARN_ON(!list_empty(&q->done_list));
  1172. return ret;
  1173. }
  1174. /**
  1175. * vb2_core_qbuf() - Queue a buffer from userspace
  1176. * @q: videobuf2 queue
  1177. * @index: id number of the buffer
  1178. * @pb: buffer structure passed from userspace to vidioc_qbuf handler
  1179. * in driver
  1180. *
  1181. * Should be called from vidioc_qbuf ioctl handler of a driver.
  1182. * The passed buffer should have been verified.
  1183. * This function:
  1184. * 1) if necessary, calls buf_prepare callback in the driver (if provided), in
  1185. * which driver-specific buffer initialization can be performed,
  1186. * 2) if streaming is on, queues the buffer in driver by the means of buf_queue
  1187. * callback for processing.
  1188. *
  1189. * The return values from this function are intended to be directly returned
  1190. * from vidioc_qbuf handler in driver.
  1191. */
  1192. int vb2_core_qbuf(struct vb2_queue *q, unsigned int index, void *pb)
  1193. {
  1194. struct vb2_buffer *vb;
  1195. int ret;
  1196. if (q->error) {
  1197. dprintk(1, "fatal error occurred on queue\n");
  1198. return -EIO;
  1199. }
  1200. vb = q->bufs[index];
  1201. switch (vb->state) {
  1202. case VB2_BUF_STATE_DEQUEUED:
  1203. ret = __buf_prepare(vb, pb);
  1204. if (ret)
  1205. return ret;
  1206. break;
  1207. case VB2_BUF_STATE_PREPARED:
  1208. break;
  1209. case VB2_BUF_STATE_PREPARING:
  1210. dprintk(1, "buffer still being prepared\n");
  1211. return -EINVAL;
  1212. default:
  1213. dprintk(1, "invalid buffer state %d\n", vb->state);
  1214. return -EINVAL;
  1215. }
  1216. /*
  1217. * Add to the queued buffers list, a buffer will stay on it until
  1218. * dequeued in dqbuf.
  1219. */
  1220. list_add_tail(&vb->queued_entry, &q->queued_list);
  1221. q->queued_count++;
  1222. q->waiting_for_buffers = false;
  1223. vb->state = VB2_BUF_STATE_QUEUED;
  1224. call_bufop(q, set_timestamp, vb, pb);
  1225. trace_vb2_qbuf(q, vb);
  1226. /*
  1227. * If already streaming, give the buffer to driver for processing.
  1228. * If not, the buffer will be given to driver on next streamon.
  1229. */
  1230. if (q->start_streaming_called)
  1231. __enqueue_in_driver(vb);
  1232. /* Fill buffer information for the userspace */
  1233. ret = call_bufop(q, fill_user_buffer, vb, pb);
  1234. if (ret)
  1235. return ret;
  1236. /*
  1237. * If streamon has been called, and we haven't yet called
  1238. * start_streaming() since not enough buffers were queued, and
  1239. * we now have reached the minimum number of queued buffers,
  1240. * then we can finally call start_streaming().
  1241. */
  1242. if (q->streaming && !q->start_streaming_called &&
  1243. q->queued_count >= q->min_buffers_needed) {
  1244. ret = vb2_start_streaming(q);
  1245. if (ret)
  1246. return ret;
  1247. }
  1248. dprintk(1, "qbuf of buffer %d succeeded\n", vb->index);
  1249. return 0;
  1250. }
  1251. EXPORT_SYMBOL_GPL(vb2_core_qbuf);
  1252. /**
  1253. * __vb2_wait_for_done_vb() - wait for a buffer to become available
  1254. * for dequeuing
  1255. *
  1256. * Will sleep if required for nonblocking == false.
  1257. */
  1258. static int __vb2_wait_for_done_vb(struct vb2_queue *q, int nonblocking)
  1259. {
  1260. /*
  1261. * All operations on vb_done_list are performed under done_lock
  1262. * spinlock protection. However, buffers may be removed from
  1263. * it and returned to userspace only while holding both driver's
  1264. * lock and the done_lock spinlock. Thus we can be sure that as
  1265. * long as we hold the driver's lock, the list will remain not
  1266. * empty if list_empty() check succeeds.
  1267. */
  1268. for (;;) {
  1269. int ret;
  1270. if (!q->streaming) {
  1271. dprintk(1, "streaming off, will not wait for buffers\n");
  1272. return -EINVAL;
  1273. }
  1274. if (q->error) {
  1275. dprintk(1, "Queue in error state, will not wait for buffers\n");
  1276. return -EIO;
  1277. }
  1278. if (q->last_buffer_dequeued) {
  1279. dprintk(3, "last buffer dequeued already, will not wait for buffers\n");
  1280. return -EPIPE;
  1281. }
  1282. if (!list_empty(&q->done_list)) {
  1283. /*
  1284. * Found a buffer that we were waiting for.
  1285. */
  1286. break;
  1287. }
  1288. if (nonblocking) {
  1289. dprintk(1, "nonblocking and no buffers to dequeue, "
  1290. "will not wait\n");
  1291. return -EAGAIN;
  1292. }
  1293. /*
  1294. * We are streaming and blocking, wait for another buffer to
  1295. * become ready or for streamoff. Driver's lock is released to
  1296. * allow streamoff or qbuf to be called while waiting.
  1297. */
  1298. call_void_qop(q, wait_prepare, q);
  1299. /*
  1300. * All locks have been released, it is safe to sleep now.
  1301. */
  1302. dprintk(3, "will sleep waiting for buffers\n");
  1303. ret = wait_event_interruptible(q->done_wq,
  1304. !list_empty(&q->done_list) || !q->streaming ||
  1305. q->error);
  1306. /*
  1307. * We need to reevaluate both conditions again after reacquiring
  1308. * the locks or return an error if one occurred.
  1309. */
  1310. call_void_qop(q, wait_finish, q);
  1311. if (ret) {
  1312. dprintk(1, "sleep was interrupted\n");
  1313. return ret;
  1314. }
  1315. }
  1316. return 0;
  1317. }
  1318. /**
  1319. * __vb2_get_done_vb() - get a buffer ready for dequeuing
  1320. *
  1321. * Will sleep if required for nonblocking == false.
  1322. */
  1323. static int __vb2_get_done_vb(struct vb2_queue *q, struct vb2_buffer **vb,
  1324. void *pb, int nonblocking)
  1325. {
  1326. unsigned long flags;
  1327. int ret = 0;
  1328. /*
  1329. * Wait for at least one buffer to become available on the done_list.
  1330. */
  1331. ret = __vb2_wait_for_done_vb(q, nonblocking);
  1332. if (ret)
  1333. return ret;
  1334. /*
  1335. * Driver's lock has been held since we last verified that done_list
  1336. * is not empty, so no need for another list_empty(done_list) check.
  1337. */
  1338. spin_lock_irqsave(&q->done_lock, flags);
  1339. *vb = list_first_entry(&q->done_list, struct vb2_buffer, done_entry);
  1340. /*
  1341. * Only remove the buffer from done_list if all planes can be
  1342. * handled. Some cases such as V4L2 file I/O and DVB have pb
  1343. * == NULL; skip the check then as there's nothing to verify.
  1344. */
  1345. if (pb)
  1346. ret = call_bufop(q, verify_planes_array, *vb, pb);
  1347. if (!ret)
  1348. list_del(&(*vb)->done_entry);
  1349. spin_unlock_irqrestore(&q->done_lock, flags);
  1350. return ret;
  1351. }
  1352. /**
  1353. * vb2_wait_for_all_buffers() - wait until all buffers are given back to vb2
  1354. * @q: videobuf2 queue
  1355. *
  1356. * This function will wait until all buffers that have been given to the driver
  1357. * by buf_queue() are given back to vb2 with vb2_buffer_done(). It doesn't call
  1358. * wait_prepare, wait_finish pair. It is intended to be called with all locks
  1359. * taken, for example from stop_streaming() callback.
  1360. */
  1361. int vb2_wait_for_all_buffers(struct vb2_queue *q)
  1362. {
  1363. if (!q->streaming) {
  1364. dprintk(1, "streaming off, will not wait for buffers\n");
  1365. return -EINVAL;
  1366. }
  1367. if (q->start_streaming_called)
  1368. wait_event(q->done_wq, !atomic_read(&q->owned_by_drv_count));
  1369. return 0;
  1370. }
  1371. EXPORT_SYMBOL_GPL(vb2_wait_for_all_buffers);
  1372. /**
  1373. * __vb2_dqbuf() - bring back the buffer to the DEQUEUED state
  1374. */
  1375. static void __vb2_dqbuf(struct vb2_buffer *vb)
  1376. {
  1377. struct vb2_queue *q = vb->vb2_queue;
  1378. unsigned int i;
  1379. /* nothing to do if the buffer is already dequeued */
  1380. if (vb->state == VB2_BUF_STATE_DEQUEUED)
  1381. return;
  1382. vb->state = VB2_BUF_STATE_DEQUEUED;
  1383. /* unmap DMABUF buffer */
  1384. if (q->memory == VB2_MEMORY_DMABUF)
  1385. for (i = 0; i < vb->num_planes; ++i) {
  1386. if (!vb->planes[i].dbuf_mapped)
  1387. continue;
  1388. call_void_memop(vb, unmap_dmabuf, vb->planes[i].mem_priv);
  1389. vb->planes[i].dbuf_mapped = 0;
  1390. }
  1391. }
  1392. /**
  1393. * vb2_dqbuf() - Dequeue a buffer to the userspace
  1394. * @q: videobuf2 queue
  1395. * @pb: buffer structure passed from userspace to vidioc_dqbuf handler
  1396. * in driver
  1397. * @nonblocking: if true, this call will not sleep waiting for a buffer if no
  1398. * buffers ready for dequeuing are present. Normally the driver
  1399. * would be passing (file->f_flags & O_NONBLOCK) here
  1400. *
  1401. * Should be called from vidioc_dqbuf ioctl handler of a driver.
  1402. * The passed buffer should have been verified.
  1403. * This function:
  1404. * 1) calls buf_finish callback in the driver (if provided), in which
  1405. * driver can perform any additional operations that may be required before
  1406. * returning the buffer to userspace, such as cache sync,
  1407. * 2) the buffer struct members are filled with relevant information for
  1408. * the userspace.
  1409. *
  1410. * The return values from this function are intended to be directly returned
  1411. * from vidioc_dqbuf handler in driver.
  1412. */
  1413. int vb2_core_dqbuf(struct vb2_queue *q, void *pb, bool nonblocking)
  1414. {
  1415. struct vb2_buffer *vb = NULL;
  1416. int ret;
  1417. ret = __vb2_get_done_vb(q, &vb, pb, nonblocking);
  1418. if (ret < 0)
  1419. return ret;
  1420. switch (vb->state) {
  1421. case VB2_BUF_STATE_DONE:
  1422. dprintk(3, "returning done buffer\n");
  1423. break;
  1424. case VB2_BUF_STATE_ERROR:
  1425. dprintk(3, "returning done buffer with errors\n");
  1426. break;
  1427. default:
  1428. dprintk(1, "invalid buffer state\n");
  1429. return -EINVAL;
  1430. }
  1431. call_void_vb_qop(vb, buf_finish, vb);
  1432. /* Fill buffer information for the userspace */
  1433. ret = call_bufop(q, fill_user_buffer, vb, pb);
  1434. if (ret)
  1435. return ret;
  1436. /* Remove from videobuf queue */
  1437. list_del(&vb->queued_entry);
  1438. q->queued_count--;
  1439. trace_vb2_dqbuf(q, vb);
  1440. /* go back to dequeued state */
  1441. __vb2_dqbuf(vb);
  1442. dprintk(1, "dqbuf of buffer %d, with state %d\n",
  1443. vb->index, vb->state);
  1444. return 0;
  1445. }
  1446. EXPORT_SYMBOL_GPL(vb2_core_dqbuf);
  1447. /**
  1448. * __vb2_queue_cancel() - cancel and stop (pause) streaming
  1449. *
  1450. * Removes all queued buffers from driver's queue and all buffers queued by
  1451. * userspace from videobuf's queue. Returns to state after reqbufs.
  1452. */
  1453. static void __vb2_queue_cancel(struct vb2_queue *q)
  1454. {
  1455. unsigned int i;
  1456. /*
  1457. * Tell driver to stop all transactions and release all queued
  1458. * buffers.
  1459. */
  1460. if (q->start_streaming_called)
  1461. call_void_qop(q, stop_streaming, q);
  1462. /*
  1463. * If you see this warning, then the driver isn't cleaning up properly
  1464. * in stop_streaming(). See the stop_streaming() documentation in
  1465. * videobuf2-core.h for more information how buffers should be returned
  1466. * to vb2 in stop_streaming().
  1467. */
  1468. if (WARN_ON(atomic_read(&q->owned_by_drv_count))) {
  1469. for (i = 0; i < q->num_buffers; ++i)
  1470. if (q->bufs[i]->state == VB2_BUF_STATE_ACTIVE)
  1471. vb2_buffer_done(q->bufs[i], VB2_BUF_STATE_ERROR);
  1472. /* Must be zero now */
  1473. WARN_ON(atomic_read(&q->owned_by_drv_count));
  1474. }
  1475. q->streaming = 0;
  1476. q->start_streaming_called = 0;
  1477. q->queued_count = 0;
  1478. q->error = 0;
  1479. /*
  1480. * Remove all buffers from videobuf's list...
  1481. */
  1482. INIT_LIST_HEAD(&q->queued_list);
  1483. /*
  1484. * ...and done list; userspace will not receive any buffers it
  1485. * has not already dequeued before initiating cancel.
  1486. */
  1487. INIT_LIST_HEAD(&q->done_list);
  1488. atomic_set(&q->owned_by_drv_count, 0);
  1489. wake_up_all(&q->done_wq);
  1490. /*
  1491. * Reinitialize all buffers for next use.
  1492. * Make sure to call buf_finish for any queued buffers. Normally
  1493. * that's done in dqbuf, but that's not going to happen when we
  1494. * cancel the whole queue. Note: this code belongs here, not in
  1495. * __vb2_dqbuf() since in vb2_internal_dqbuf() there is a critical
  1496. * call to __fill_v4l2_buffer() after buf_finish(). That order can't
  1497. * be changed, so we can't move the buf_finish() to __vb2_dqbuf().
  1498. */
  1499. for (i = 0; i < q->num_buffers; ++i) {
  1500. struct vb2_buffer *vb = q->bufs[i];
  1501. if (vb->state != VB2_BUF_STATE_DEQUEUED) {
  1502. vb->state = VB2_BUF_STATE_PREPARED;
  1503. call_void_vb_qop(vb, buf_finish, vb);
  1504. }
  1505. __vb2_dqbuf(vb);
  1506. }
  1507. }
  1508. int vb2_core_streamon(struct vb2_queue *q, unsigned int type)
  1509. {
  1510. int ret;
  1511. if (type != q->type) {
  1512. dprintk(1, "invalid stream type\n");
  1513. return -EINVAL;
  1514. }
  1515. if (q->streaming) {
  1516. dprintk(3, "already streaming\n");
  1517. return 0;
  1518. }
  1519. if (!q->num_buffers) {
  1520. dprintk(1, "no buffers have been allocated\n");
  1521. return -EINVAL;
  1522. }
  1523. if (q->num_buffers < q->min_buffers_needed) {
  1524. dprintk(1, "need at least %u allocated buffers\n",
  1525. q->min_buffers_needed);
  1526. return -EINVAL;
  1527. }
  1528. /*
  1529. * Tell driver to start streaming provided sufficient buffers
  1530. * are available.
  1531. */
  1532. if (q->queued_count >= q->min_buffers_needed) {
  1533. ret = vb2_start_streaming(q);
  1534. if (ret) {
  1535. __vb2_queue_cancel(q);
  1536. return ret;
  1537. }
  1538. }
  1539. q->streaming = 1;
  1540. dprintk(3, "successful\n");
  1541. return 0;
  1542. }
  1543. EXPORT_SYMBOL_GPL(vb2_core_streamon);
  1544. /**
  1545. * vb2_queue_error() - signal a fatal error on the queue
  1546. * @q: videobuf2 queue
  1547. *
  1548. * Flag that a fatal unrecoverable error has occurred and wake up all processes
  1549. * waiting on the queue. Polling will now set POLLERR and queuing and dequeuing
  1550. * buffers will return -EIO.
  1551. *
  1552. * The error flag will be cleared when cancelling the queue, either from
  1553. * vb2_streamoff or vb2_queue_release. Drivers should thus not call this
  1554. * function before starting the stream, otherwise the error flag will remain set
  1555. * until the queue is released when closing the device node.
  1556. */
  1557. void vb2_queue_error(struct vb2_queue *q)
  1558. {
  1559. q->error = 1;
  1560. wake_up_all(&q->done_wq);
  1561. }
  1562. EXPORT_SYMBOL_GPL(vb2_queue_error);
  1563. int vb2_core_streamoff(struct vb2_queue *q, unsigned int type)
  1564. {
  1565. if (type != q->type) {
  1566. dprintk(1, "invalid stream type\n");
  1567. return -EINVAL;
  1568. }
  1569. /*
  1570. * Cancel will pause streaming and remove all buffers from the driver
  1571. * and videobuf, effectively returning control over them to userspace.
  1572. *
  1573. * Note that we do this even if q->streaming == 0: if you prepare or
  1574. * queue buffers, and then call streamoff without ever having called
  1575. * streamon, you would still expect those buffers to be returned to
  1576. * their normal dequeued state.
  1577. */
  1578. __vb2_queue_cancel(q);
  1579. q->waiting_for_buffers = !q->is_output;
  1580. q->last_buffer_dequeued = false;
  1581. dprintk(3, "successful\n");
  1582. return 0;
  1583. }
  1584. EXPORT_SYMBOL_GPL(vb2_core_streamoff);
  1585. /**
  1586. * __find_plane_by_offset() - find plane associated with the given offset off
  1587. */
  1588. static int __find_plane_by_offset(struct vb2_queue *q, unsigned long off,
  1589. unsigned int *_buffer, unsigned int *_plane)
  1590. {
  1591. struct vb2_buffer *vb;
  1592. unsigned int buffer, plane;
  1593. /*
  1594. * Go over all buffers and their planes, comparing the given offset
  1595. * with an offset assigned to each plane. If a match is found,
  1596. * return its buffer and plane numbers.
  1597. */
  1598. for (buffer = 0; buffer < q->num_buffers; ++buffer) {
  1599. vb = q->bufs[buffer];
  1600. for (plane = 0; plane < vb->num_planes; ++plane) {
  1601. if (vb->planes[plane].m.offset == off) {
  1602. *_buffer = buffer;
  1603. *_plane = plane;
  1604. return 0;
  1605. }
  1606. }
  1607. }
  1608. return -EINVAL;
  1609. }
  1610. /**
  1611. * vb2_core_expbuf() - Export a buffer as a file descriptor
  1612. * @q: videobuf2 queue
  1613. * @fd: file descriptor associated with DMABUF (set by driver) *
  1614. * @type: buffer type
  1615. * @index: id number of the buffer
  1616. * @plane: index of the plane to be exported, 0 for single plane queues
  1617. * @flags: flags for newly created file, currently only O_CLOEXEC is
  1618. * supported, refer to manual of open syscall for more details
  1619. *
  1620. * The return values from this function are intended to be directly returned
  1621. * from vidioc_expbuf handler in driver.
  1622. */
  1623. int vb2_core_expbuf(struct vb2_queue *q, int *fd, unsigned int type,
  1624. unsigned int index, unsigned int plane, unsigned int flags)
  1625. {
  1626. struct vb2_buffer *vb = NULL;
  1627. struct vb2_plane *vb_plane;
  1628. int ret;
  1629. struct dma_buf *dbuf;
  1630. if (q->memory != VB2_MEMORY_MMAP) {
  1631. dprintk(1, "queue is not currently set up for mmap\n");
  1632. return -EINVAL;
  1633. }
  1634. if (!q->mem_ops->get_dmabuf) {
  1635. dprintk(1, "queue does not support DMA buffer exporting\n");
  1636. return -EINVAL;
  1637. }
  1638. if (flags & ~(O_CLOEXEC | O_ACCMODE)) {
  1639. dprintk(1, "queue does support only O_CLOEXEC and access mode flags\n");
  1640. return -EINVAL;
  1641. }
  1642. if (type != q->type) {
  1643. dprintk(1, "invalid buffer type\n");
  1644. return -EINVAL;
  1645. }
  1646. if (index >= q->num_buffers) {
  1647. dprintk(1, "buffer index out of range\n");
  1648. return -EINVAL;
  1649. }
  1650. vb = q->bufs[index];
  1651. if (plane >= vb->num_planes) {
  1652. dprintk(1, "buffer plane out of range\n");
  1653. return -EINVAL;
  1654. }
  1655. if (vb2_fileio_is_active(q)) {
  1656. dprintk(1, "expbuf: file io in progress\n");
  1657. return -EBUSY;
  1658. }
  1659. vb_plane = &vb->planes[plane];
  1660. dbuf = call_ptr_memop(vb, get_dmabuf, vb_plane->mem_priv,
  1661. flags & O_ACCMODE);
  1662. if (IS_ERR_OR_NULL(dbuf)) {
  1663. dprintk(1, "failed to export buffer %d, plane %d\n",
  1664. index, plane);
  1665. return -EINVAL;
  1666. }
  1667. ret = dma_buf_fd(dbuf, flags & ~O_ACCMODE);
  1668. if (ret < 0) {
  1669. dprintk(3, "buffer %d, plane %d failed to export (%d)\n",
  1670. index, plane, ret);
  1671. dma_buf_put(dbuf);
  1672. return ret;
  1673. }
  1674. dprintk(3, "buffer %d, plane %d exported as %d descriptor\n",
  1675. index, plane, ret);
  1676. *fd = ret;
  1677. return 0;
  1678. }
  1679. EXPORT_SYMBOL_GPL(vb2_core_expbuf);
  1680. /**
  1681. * vb2_mmap() - map video buffers into application address space
  1682. * @q: videobuf2 queue
  1683. * @vma: vma passed to the mmap file operation handler in the driver
  1684. *
  1685. * Should be called from mmap file operation handler of a driver.
  1686. * This function maps one plane of one of the available video buffers to
  1687. * userspace. To map whole video memory allocated on reqbufs, this function
  1688. * has to be called once per each plane per each buffer previously allocated.
  1689. *
  1690. * When the userspace application calls mmap, it passes to it an offset returned
  1691. * to it earlier by the means of vidioc_querybuf handler. That offset acts as
  1692. * a "cookie", which is then used to identify the plane to be mapped.
  1693. * This function finds a plane with a matching offset and a mapping is performed
  1694. * by the means of a provided memory operation.
  1695. *
  1696. * The return values from this function are intended to be directly returned
  1697. * from the mmap handler in driver.
  1698. */
  1699. int vb2_mmap(struct vb2_queue *q, struct vm_area_struct *vma)
  1700. {
  1701. unsigned long off = vma->vm_pgoff << PAGE_SHIFT;
  1702. struct vb2_buffer *vb;
  1703. unsigned int buffer = 0, plane = 0;
  1704. int ret;
  1705. unsigned long length;
  1706. if (q->memory != VB2_MEMORY_MMAP) {
  1707. dprintk(1, "queue is not currently set up for mmap\n");
  1708. return -EINVAL;
  1709. }
  1710. /*
  1711. * Check memory area access mode.
  1712. */
  1713. if (!(vma->vm_flags & VM_SHARED)) {
  1714. dprintk(1, "invalid vma flags, VM_SHARED needed\n");
  1715. return -EINVAL;
  1716. }
  1717. if (q->is_output) {
  1718. if (!(vma->vm_flags & VM_WRITE)) {
  1719. dprintk(1, "invalid vma flags, VM_WRITE needed\n");
  1720. return -EINVAL;
  1721. }
  1722. } else {
  1723. if (!(vma->vm_flags & VM_READ)) {
  1724. dprintk(1, "invalid vma flags, VM_READ needed\n");
  1725. return -EINVAL;
  1726. }
  1727. }
  1728. mutex_lock(&q->mmap_lock);
  1729. if (vb2_fileio_is_active(q)) {
  1730. dprintk(1, "mmap: file io in progress\n");
  1731. ret = -EBUSY;
  1732. goto unlock;
  1733. }
  1734. /*
  1735. * Find the plane corresponding to the offset passed by userspace.
  1736. */
  1737. ret = __find_plane_by_offset(q, off, &buffer, &plane);
  1738. if (ret)
  1739. goto unlock;
  1740. vb = q->bufs[buffer];
  1741. /*
  1742. * MMAP requires page_aligned buffers.
  1743. * The buffer length was page_aligned at __vb2_buf_mem_alloc(),
  1744. * so, we need to do the same here.
  1745. */
  1746. length = PAGE_ALIGN(vb->planes[plane].length);
  1747. if (length < (vma->vm_end - vma->vm_start)) {
  1748. dprintk(1,
  1749. "MMAP invalid, as it would overflow buffer length\n");
  1750. ret = -EINVAL;
  1751. goto unlock;
  1752. }
  1753. ret = call_memop(vb, mmap, vb->planes[plane].mem_priv, vma);
  1754. unlock:
  1755. mutex_unlock(&q->mmap_lock);
  1756. if (ret)
  1757. return ret;
  1758. dprintk(3, "buffer %d, plane %d successfully mapped\n", buffer, plane);
  1759. return 0;
  1760. }
  1761. EXPORT_SYMBOL_GPL(vb2_mmap);
  1762. #ifndef CONFIG_MMU
  1763. unsigned long vb2_get_unmapped_area(struct vb2_queue *q,
  1764. unsigned long addr,
  1765. unsigned long len,
  1766. unsigned long pgoff,
  1767. unsigned long flags)
  1768. {
  1769. unsigned long off = pgoff << PAGE_SHIFT;
  1770. struct vb2_buffer *vb;
  1771. unsigned int buffer, plane;
  1772. void *vaddr;
  1773. int ret;
  1774. if (q->memory != VB2_MEMORY_MMAP) {
  1775. dprintk(1, "queue is not currently set up for mmap\n");
  1776. return -EINVAL;
  1777. }
  1778. /*
  1779. * Find the plane corresponding to the offset passed by userspace.
  1780. */
  1781. ret = __find_plane_by_offset(q, off, &buffer, &plane);
  1782. if (ret)
  1783. return ret;
  1784. vb = q->bufs[buffer];
  1785. vaddr = vb2_plane_vaddr(vb, plane);
  1786. return vaddr ? (unsigned long)vaddr : -EINVAL;
  1787. }
  1788. EXPORT_SYMBOL_GPL(vb2_get_unmapped_area);
  1789. #endif
  1790. /**
  1791. * vb2_core_queue_init() - initialize a videobuf2 queue
  1792. * @q: videobuf2 queue; this structure should be allocated in driver
  1793. *
  1794. * The vb2_queue structure should be allocated by the driver. The driver is
  1795. * responsible of clearing it's content and setting initial values for some
  1796. * required entries before calling this function.
  1797. * q->ops, q->mem_ops, q->type and q->io_modes are mandatory. Please refer
  1798. * to the struct vb2_queue description in include/media/videobuf2-core.h
  1799. * for more information.
  1800. */
  1801. int vb2_core_queue_init(struct vb2_queue *q)
  1802. {
  1803. /*
  1804. * Sanity check
  1805. */
  1806. if (WARN_ON(!q) ||
  1807. WARN_ON(!q->ops) ||
  1808. WARN_ON(!q->mem_ops) ||
  1809. WARN_ON(!q->type) ||
  1810. WARN_ON(!q->io_modes) ||
  1811. WARN_ON(!q->ops->queue_setup) ||
  1812. WARN_ON(!q->ops->buf_queue))
  1813. return -EINVAL;
  1814. INIT_LIST_HEAD(&q->queued_list);
  1815. INIT_LIST_HEAD(&q->done_list);
  1816. spin_lock_init(&q->done_lock);
  1817. mutex_init(&q->mmap_lock);
  1818. init_waitqueue_head(&q->done_wq);
  1819. if (q->buf_struct_size == 0)
  1820. q->buf_struct_size = sizeof(struct vb2_buffer);
  1821. return 0;
  1822. }
  1823. EXPORT_SYMBOL_GPL(vb2_core_queue_init);
  1824. /**
  1825. * vb2_core_queue_release() - stop streaming, release the queue and free memory
  1826. * @q: videobuf2 queue
  1827. *
  1828. * This function stops streaming and performs necessary clean ups, including
  1829. * freeing video buffer memory. The driver is responsible for freeing
  1830. * the vb2_queue structure itself.
  1831. */
  1832. void vb2_core_queue_release(struct vb2_queue *q)
  1833. {
  1834. __vb2_queue_cancel(q);
  1835. mutex_lock(&q->mmap_lock);
  1836. __vb2_queue_free(q, q->num_buffers);
  1837. mutex_unlock(&q->mmap_lock);
  1838. }
  1839. EXPORT_SYMBOL_GPL(vb2_core_queue_release);
  1840. MODULE_DESCRIPTION("Driver helper framework for Video for Linux 2");
  1841. MODULE_AUTHOR("Pawel Osciak <pawel@osciak.com>, Marek Szyprowski");
  1842. MODULE_LICENSE("GPL");