videobuf2-core.c 66 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602
  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 <media/v4l2-mc.h>
  27. #include <trace/events/vb2.h>
  28. static int debug;
  29. module_param(debug, int, 0644);
  30. #define dprintk(level, fmt, arg...) \
  31. do { \
  32. if (debug >= level) \
  33. pr_info("vb2-core: %s: " fmt, __func__, ## arg); \
  34. } while (0)
  35. #ifdef CONFIG_VIDEO_ADV_DEBUG
  36. /*
  37. * If advanced debugging is on, then count how often each op is called
  38. * successfully, which can either be per-buffer or per-queue.
  39. *
  40. * This makes it easy to check that the 'init' and 'cleanup'
  41. * (and variations thereof) stay balanced.
  42. */
  43. #define log_memop(vb, op) \
  44. dprintk(2, "call_memop(%p, %d, %s)%s\n", \
  45. (vb)->vb2_queue, (vb)->index, #op, \
  46. (vb)->vb2_queue->mem_ops->op ? "" : " (nop)")
  47. #define call_memop(vb, op, args...) \
  48. ({ \
  49. struct vb2_queue *_q = (vb)->vb2_queue; \
  50. int err; \
  51. \
  52. log_memop(vb, op); \
  53. err = _q->mem_ops->op ? _q->mem_ops->op(args) : 0; \
  54. if (!err) \
  55. (vb)->cnt_mem_ ## op++; \
  56. err; \
  57. })
  58. #define call_ptr_memop(vb, op, args...) \
  59. ({ \
  60. struct vb2_queue *_q = (vb)->vb2_queue; \
  61. void *ptr; \
  62. \
  63. log_memop(vb, op); \
  64. ptr = _q->mem_ops->op ? _q->mem_ops->op(args) : NULL; \
  65. if (!IS_ERR_OR_NULL(ptr)) \
  66. (vb)->cnt_mem_ ## op++; \
  67. ptr; \
  68. })
  69. #define call_void_memop(vb, op, args...) \
  70. ({ \
  71. struct vb2_queue *_q = (vb)->vb2_queue; \
  72. \
  73. log_memop(vb, op); \
  74. if (_q->mem_ops->op) \
  75. _q->mem_ops->op(args); \
  76. (vb)->cnt_mem_ ## op++; \
  77. })
  78. #define log_qop(q, op) \
  79. dprintk(2, "call_qop(%p, %s)%s\n", q, #op, \
  80. (q)->ops->op ? "" : " (nop)")
  81. #define call_qop(q, op, args...) \
  82. ({ \
  83. int err; \
  84. \
  85. log_qop(q, op); \
  86. err = (q)->ops->op ? (q)->ops->op(args) : 0; \
  87. if (!err) \
  88. (q)->cnt_ ## op++; \
  89. err; \
  90. })
  91. #define call_void_qop(q, op, args...) \
  92. ({ \
  93. log_qop(q, op); \
  94. if ((q)->ops->op) \
  95. (q)->ops->op(args); \
  96. (q)->cnt_ ## op++; \
  97. })
  98. #define log_vb_qop(vb, op, args...) \
  99. dprintk(2, "call_vb_qop(%p, %d, %s)%s\n", \
  100. (vb)->vb2_queue, (vb)->index, #op, \
  101. (vb)->vb2_queue->ops->op ? "" : " (nop)")
  102. #define call_vb_qop(vb, op, args...) \
  103. ({ \
  104. int err; \
  105. \
  106. log_vb_qop(vb, op); \
  107. err = (vb)->vb2_queue->ops->op ? \
  108. (vb)->vb2_queue->ops->op(args) : 0; \
  109. if (!err) \
  110. (vb)->cnt_ ## op++; \
  111. err; \
  112. })
  113. #define call_void_vb_qop(vb, op, args...) \
  114. ({ \
  115. log_vb_qop(vb, op); \
  116. if ((vb)->vb2_queue->ops->op) \
  117. (vb)->vb2_queue->ops->op(args); \
  118. (vb)->cnt_ ## op++; \
  119. })
  120. #else
  121. #define call_memop(vb, op, args...) \
  122. ((vb)->vb2_queue->mem_ops->op ? \
  123. (vb)->vb2_queue->mem_ops->op(args) : 0)
  124. #define call_ptr_memop(vb, op, args...) \
  125. ((vb)->vb2_queue->mem_ops->op ? \
  126. (vb)->vb2_queue->mem_ops->op(args) : NULL)
  127. #define call_void_memop(vb, op, args...) \
  128. do { \
  129. if ((vb)->vb2_queue->mem_ops->op) \
  130. (vb)->vb2_queue->mem_ops->op(args); \
  131. } while (0)
  132. #define call_qop(q, op, args...) \
  133. ((q)->ops->op ? (q)->ops->op(args) : 0)
  134. #define call_void_qop(q, op, args...) \
  135. do { \
  136. if ((q)->ops->op) \
  137. (q)->ops->op(args); \
  138. } while (0)
  139. #define call_vb_qop(vb, op, args...) \
  140. ((vb)->vb2_queue->ops->op ? (vb)->vb2_queue->ops->op(args) : 0)
  141. #define call_void_vb_qop(vb, op, args...) \
  142. do { \
  143. if ((vb)->vb2_queue->ops->op) \
  144. (vb)->vb2_queue->ops->op(args); \
  145. } while (0)
  146. #endif
  147. #define call_bufop(q, op, args...) \
  148. ({ \
  149. int ret = 0; \
  150. if (q && q->buf_ops && q->buf_ops->op) \
  151. ret = q->buf_ops->op(args); \
  152. ret; \
  153. })
  154. #define call_void_bufop(q, op, args...) \
  155. ({ \
  156. if (q && q->buf_ops && q->buf_ops->op) \
  157. q->buf_ops->op(args); \
  158. })
  159. static void __vb2_queue_cancel(struct vb2_queue *q);
  160. static void __enqueue_in_driver(struct vb2_buffer *vb);
  161. /**
  162. * __vb2_buf_mem_alloc() - allocate video memory for the given buffer
  163. */
  164. static int __vb2_buf_mem_alloc(struct vb2_buffer *vb)
  165. {
  166. struct vb2_queue *q = vb->vb2_queue;
  167. enum dma_data_direction dma_dir =
  168. q->is_output ? DMA_TO_DEVICE : DMA_FROM_DEVICE;
  169. void *mem_priv;
  170. int plane;
  171. int ret = -ENOMEM;
  172. /*
  173. * Allocate memory for all planes in this buffer
  174. * NOTE: mmapped areas should be page aligned
  175. */
  176. for (plane = 0; plane < vb->num_planes; ++plane) {
  177. unsigned long size = PAGE_ALIGN(vb->planes[plane].length);
  178. mem_priv = call_ptr_memop(vb, alloc,
  179. q->alloc_devs[plane] ? : q->dev,
  180. q->dma_attrs, size, dma_dir, q->gfp_flags);
  181. if (IS_ERR(mem_priv)) {
  182. if (mem_priv)
  183. ret = PTR_ERR(mem_priv);
  184. goto free;
  185. }
  186. /* Associate allocator private data with this plane */
  187. vb->planes[plane].mem_priv = mem_priv;
  188. }
  189. return 0;
  190. free:
  191. /* Free already allocated memory if one of the allocations failed */
  192. for (; plane > 0; --plane) {
  193. call_void_memop(vb, put, vb->planes[plane - 1].mem_priv);
  194. vb->planes[plane - 1].mem_priv = NULL;
  195. }
  196. return ret;
  197. }
  198. /**
  199. * __vb2_buf_mem_free() - free memory of the given buffer
  200. */
  201. static void __vb2_buf_mem_free(struct vb2_buffer *vb)
  202. {
  203. unsigned int plane;
  204. for (plane = 0; plane < vb->num_planes; ++plane) {
  205. call_void_memop(vb, put, vb->planes[plane].mem_priv);
  206. vb->planes[plane].mem_priv = NULL;
  207. dprintk(3, "freed plane %d of buffer %d\n", plane, vb->index);
  208. }
  209. }
  210. /**
  211. * __vb2_buf_userptr_put() - release userspace memory associated with
  212. * a USERPTR buffer
  213. */
  214. static void __vb2_buf_userptr_put(struct vb2_buffer *vb)
  215. {
  216. unsigned int plane;
  217. for (plane = 0; plane < vb->num_planes; ++plane) {
  218. if (vb->planes[plane].mem_priv)
  219. call_void_memop(vb, put_userptr, vb->planes[plane].mem_priv);
  220. vb->planes[plane].mem_priv = NULL;
  221. }
  222. }
  223. /**
  224. * __vb2_plane_dmabuf_put() - release memory associated with
  225. * a DMABUF shared plane
  226. */
  227. static void __vb2_plane_dmabuf_put(struct vb2_buffer *vb, struct vb2_plane *p)
  228. {
  229. if (!p->mem_priv)
  230. return;
  231. if (p->dbuf_mapped)
  232. call_void_memop(vb, unmap_dmabuf, p->mem_priv);
  233. call_void_memop(vb, detach_dmabuf, p->mem_priv);
  234. dma_buf_put(p->dbuf);
  235. p->mem_priv = NULL;
  236. p->dbuf = NULL;
  237. p->dbuf_mapped = 0;
  238. }
  239. /**
  240. * __vb2_buf_dmabuf_put() - release memory associated with
  241. * a DMABUF shared buffer
  242. */
  243. static void __vb2_buf_dmabuf_put(struct vb2_buffer *vb)
  244. {
  245. unsigned int plane;
  246. for (plane = 0; plane < vb->num_planes; ++plane)
  247. __vb2_plane_dmabuf_put(vb, &vb->planes[plane]);
  248. }
  249. /**
  250. * __setup_offsets() - setup unique offsets ("cookies") for every plane in
  251. * the buffer.
  252. */
  253. static void __setup_offsets(struct vb2_buffer *vb)
  254. {
  255. struct vb2_queue *q = vb->vb2_queue;
  256. unsigned int plane;
  257. unsigned long off = 0;
  258. if (vb->index) {
  259. struct vb2_buffer *prev = q->bufs[vb->index - 1];
  260. struct vb2_plane *p = &prev->planes[prev->num_planes - 1];
  261. off = PAGE_ALIGN(p->m.offset + p->length);
  262. }
  263. for (plane = 0; plane < vb->num_planes; ++plane) {
  264. vb->planes[plane].m.offset = off;
  265. dprintk(3, "buffer %d, plane %d offset 0x%08lx\n",
  266. vb->index, plane, off);
  267. off += vb->planes[plane].length;
  268. off = PAGE_ALIGN(off);
  269. }
  270. }
  271. /**
  272. * __vb2_queue_alloc() - allocate videobuf buffer structures and (for MMAP type)
  273. * video buffer memory for all buffers/planes on the queue and initializes the
  274. * queue
  275. *
  276. * Returns the number of buffers successfully allocated.
  277. */
  278. static int __vb2_queue_alloc(struct vb2_queue *q, enum vb2_memory memory,
  279. unsigned int num_buffers, unsigned int num_planes,
  280. const unsigned plane_sizes[VB2_MAX_PLANES])
  281. {
  282. unsigned int buffer, plane;
  283. struct vb2_buffer *vb;
  284. int ret;
  285. for (buffer = 0; buffer < num_buffers; ++buffer) {
  286. /* Allocate videobuf buffer structures */
  287. vb = kzalloc(q->buf_struct_size, GFP_KERNEL);
  288. if (!vb) {
  289. dprintk(1, "memory alloc for buffer struct failed\n");
  290. break;
  291. }
  292. vb->state = VB2_BUF_STATE_DEQUEUED;
  293. vb->vb2_queue = q;
  294. vb->num_planes = num_planes;
  295. vb->index = q->num_buffers + buffer;
  296. vb->type = q->type;
  297. vb->memory = memory;
  298. for (plane = 0; plane < num_planes; ++plane) {
  299. vb->planes[plane].length = plane_sizes[plane];
  300. vb->planes[plane].min_length = plane_sizes[plane];
  301. }
  302. q->bufs[vb->index] = vb;
  303. /* Allocate video buffer memory for the MMAP type */
  304. if (memory == VB2_MEMORY_MMAP) {
  305. ret = __vb2_buf_mem_alloc(vb);
  306. if (ret) {
  307. dprintk(1, "failed allocating memory for buffer %d\n",
  308. buffer);
  309. q->bufs[vb->index] = NULL;
  310. kfree(vb);
  311. break;
  312. }
  313. __setup_offsets(vb);
  314. /*
  315. * Call the driver-provided buffer initialization
  316. * callback, if given. An error in initialization
  317. * results in queue setup failure.
  318. */
  319. ret = call_vb_qop(vb, buf_init, vb);
  320. if (ret) {
  321. dprintk(1, "buffer %d %p initialization failed\n",
  322. buffer, vb);
  323. __vb2_buf_mem_free(vb);
  324. q->bufs[vb->index] = NULL;
  325. kfree(vb);
  326. break;
  327. }
  328. }
  329. }
  330. dprintk(1, "allocated %d buffers, %d plane(s) each\n",
  331. buffer, num_planes);
  332. return buffer;
  333. }
  334. /**
  335. * __vb2_free_mem() - release all video buffer memory for a given queue
  336. */
  337. static void __vb2_free_mem(struct vb2_queue *q, unsigned int buffers)
  338. {
  339. unsigned int buffer;
  340. struct vb2_buffer *vb;
  341. for (buffer = q->num_buffers - buffers; buffer < q->num_buffers;
  342. ++buffer) {
  343. vb = q->bufs[buffer];
  344. if (!vb)
  345. continue;
  346. /* Free MMAP buffers or release USERPTR buffers */
  347. if (q->memory == VB2_MEMORY_MMAP)
  348. __vb2_buf_mem_free(vb);
  349. else if (q->memory == VB2_MEMORY_DMABUF)
  350. __vb2_buf_dmabuf_put(vb);
  351. else
  352. __vb2_buf_userptr_put(vb);
  353. }
  354. }
  355. /**
  356. * __vb2_queue_free() - free buffers at the end of the queue - video memory and
  357. * related information, if no buffers are left return the queue to an
  358. * uninitialized state. Might be called even if the queue has already been freed.
  359. */
  360. static int __vb2_queue_free(struct vb2_queue *q, unsigned int buffers)
  361. {
  362. unsigned int buffer;
  363. /*
  364. * Sanity check: when preparing a buffer the queue lock is released for
  365. * a short while (see __buf_prepare for the details), which would allow
  366. * a race with a reqbufs which can call this function. Removing the
  367. * buffers from underneath __buf_prepare is obviously a bad idea, so we
  368. * check if any of the buffers is in the state PREPARING, and if so we
  369. * just return -EAGAIN.
  370. */
  371. for (buffer = q->num_buffers - buffers; buffer < q->num_buffers;
  372. ++buffer) {
  373. if (q->bufs[buffer] == NULL)
  374. continue;
  375. if (q->bufs[buffer]->state == VB2_BUF_STATE_PREPARING) {
  376. dprintk(1, "preparing buffers, cannot free\n");
  377. return -EAGAIN;
  378. }
  379. }
  380. /* Call driver-provided cleanup function for each buffer, if provided */
  381. for (buffer = q->num_buffers - buffers; buffer < q->num_buffers;
  382. ++buffer) {
  383. struct vb2_buffer *vb = q->bufs[buffer];
  384. if (vb && vb->planes[0].mem_priv)
  385. call_void_vb_qop(vb, buf_cleanup, vb);
  386. }
  387. /* Release video buffer memory */
  388. __vb2_free_mem(q, buffers);
  389. #ifdef CONFIG_VIDEO_ADV_DEBUG
  390. /*
  391. * Check that all the calls were balances during the life-time of this
  392. * queue. If not (or if the debug level is 1 or up), then dump the
  393. * counters to the kernel log.
  394. */
  395. if (q->num_buffers) {
  396. bool unbalanced = q->cnt_start_streaming != q->cnt_stop_streaming ||
  397. q->cnt_wait_prepare != q->cnt_wait_finish;
  398. if (unbalanced || debug) {
  399. pr_info("vb2: counters for queue %p:%s\n", q,
  400. unbalanced ? " UNBALANCED!" : "");
  401. pr_info("vb2: setup: %u start_streaming: %u stop_streaming: %u\n",
  402. q->cnt_queue_setup, q->cnt_start_streaming,
  403. q->cnt_stop_streaming);
  404. pr_info("vb2: wait_prepare: %u wait_finish: %u\n",
  405. q->cnt_wait_prepare, q->cnt_wait_finish);
  406. }
  407. q->cnt_queue_setup = 0;
  408. q->cnt_wait_prepare = 0;
  409. q->cnt_wait_finish = 0;
  410. q->cnt_start_streaming = 0;
  411. q->cnt_stop_streaming = 0;
  412. }
  413. for (buffer = 0; buffer < q->num_buffers; ++buffer) {
  414. struct vb2_buffer *vb = q->bufs[buffer];
  415. bool unbalanced = vb->cnt_mem_alloc != vb->cnt_mem_put ||
  416. vb->cnt_mem_prepare != vb->cnt_mem_finish ||
  417. vb->cnt_mem_get_userptr != vb->cnt_mem_put_userptr ||
  418. vb->cnt_mem_attach_dmabuf != vb->cnt_mem_detach_dmabuf ||
  419. vb->cnt_mem_map_dmabuf != vb->cnt_mem_unmap_dmabuf ||
  420. vb->cnt_buf_queue != vb->cnt_buf_done ||
  421. vb->cnt_buf_prepare != vb->cnt_buf_finish ||
  422. vb->cnt_buf_init != vb->cnt_buf_cleanup;
  423. if (unbalanced || debug) {
  424. pr_info("vb2: counters for queue %p, buffer %d:%s\n",
  425. q, buffer, unbalanced ? " UNBALANCED!" : "");
  426. pr_info("vb2: buf_init: %u buf_cleanup: %u buf_prepare: %u buf_finish: %u\n",
  427. vb->cnt_buf_init, vb->cnt_buf_cleanup,
  428. vb->cnt_buf_prepare, vb->cnt_buf_finish);
  429. pr_info("vb2: buf_queue: %u buf_done: %u\n",
  430. vb->cnt_buf_queue, vb->cnt_buf_done);
  431. pr_info("vb2: alloc: %u put: %u prepare: %u finish: %u mmap: %u\n",
  432. vb->cnt_mem_alloc, vb->cnt_mem_put,
  433. vb->cnt_mem_prepare, vb->cnt_mem_finish,
  434. vb->cnt_mem_mmap);
  435. pr_info("vb2: get_userptr: %u put_userptr: %u\n",
  436. vb->cnt_mem_get_userptr, vb->cnt_mem_put_userptr);
  437. pr_info("vb2: attach_dmabuf: %u detach_dmabuf: %u map_dmabuf: %u unmap_dmabuf: %u\n",
  438. vb->cnt_mem_attach_dmabuf, vb->cnt_mem_detach_dmabuf,
  439. vb->cnt_mem_map_dmabuf, vb->cnt_mem_unmap_dmabuf);
  440. pr_info("vb2: get_dmabuf: %u num_users: %u vaddr: %u cookie: %u\n",
  441. vb->cnt_mem_get_dmabuf,
  442. vb->cnt_mem_num_users,
  443. vb->cnt_mem_vaddr,
  444. vb->cnt_mem_cookie);
  445. }
  446. }
  447. #endif
  448. /* Free videobuf buffers */
  449. for (buffer = q->num_buffers - buffers; buffer < q->num_buffers;
  450. ++buffer) {
  451. kfree(q->bufs[buffer]);
  452. q->bufs[buffer] = NULL;
  453. }
  454. q->num_buffers -= buffers;
  455. if (!q->num_buffers) {
  456. q->memory = 0;
  457. INIT_LIST_HEAD(&q->queued_list);
  458. }
  459. return 0;
  460. }
  461. bool vb2_buffer_in_use(struct vb2_queue *q, struct vb2_buffer *vb)
  462. {
  463. unsigned int plane;
  464. for (plane = 0; plane < vb->num_planes; ++plane) {
  465. void *mem_priv = vb->planes[plane].mem_priv;
  466. /*
  467. * If num_users() has not been provided, call_memop
  468. * will return 0, apparently nobody cares about this
  469. * case anyway. If num_users() returns more than 1,
  470. * we are not the only user of the plane's memory.
  471. */
  472. if (mem_priv && call_memop(vb, num_users, mem_priv) > 1)
  473. return true;
  474. }
  475. return false;
  476. }
  477. EXPORT_SYMBOL(vb2_buffer_in_use);
  478. /**
  479. * __buffers_in_use() - return true if any buffers on the queue are in use and
  480. * the queue cannot be freed (by the means of REQBUFS(0)) call
  481. */
  482. static bool __buffers_in_use(struct vb2_queue *q)
  483. {
  484. unsigned int buffer;
  485. for (buffer = 0; buffer < q->num_buffers; ++buffer) {
  486. if (vb2_buffer_in_use(q, q->bufs[buffer]))
  487. return true;
  488. }
  489. return false;
  490. }
  491. void vb2_core_querybuf(struct vb2_queue *q, unsigned int index, void *pb)
  492. {
  493. call_void_bufop(q, fill_user_buffer, q->bufs[index], pb);
  494. }
  495. EXPORT_SYMBOL_GPL(vb2_core_querybuf);
  496. /**
  497. * __verify_userptr_ops() - verify that all memory operations required for
  498. * USERPTR queue type have been provided
  499. */
  500. static int __verify_userptr_ops(struct vb2_queue *q)
  501. {
  502. if (!(q->io_modes & VB2_USERPTR) || !q->mem_ops->get_userptr ||
  503. !q->mem_ops->put_userptr)
  504. return -EINVAL;
  505. return 0;
  506. }
  507. /**
  508. * __verify_mmap_ops() - verify that all memory operations required for
  509. * MMAP queue type have been provided
  510. */
  511. static int __verify_mmap_ops(struct vb2_queue *q)
  512. {
  513. if (!(q->io_modes & VB2_MMAP) || !q->mem_ops->alloc ||
  514. !q->mem_ops->put || !q->mem_ops->mmap)
  515. return -EINVAL;
  516. return 0;
  517. }
  518. /**
  519. * __verify_dmabuf_ops() - verify that all memory operations required for
  520. * DMABUF queue type have been provided
  521. */
  522. static int __verify_dmabuf_ops(struct vb2_queue *q)
  523. {
  524. if (!(q->io_modes & VB2_DMABUF) || !q->mem_ops->attach_dmabuf ||
  525. !q->mem_ops->detach_dmabuf || !q->mem_ops->map_dmabuf ||
  526. !q->mem_ops->unmap_dmabuf)
  527. return -EINVAL;
  528. return 0;
  529. }
  530. int vb2_verify_memory_type(struct vb2_queue *q,
  531. enum vb2_memory memory, unsigned int type)
  532. {
  533. if (memory != VB2_MEMORY_MMAP && memory != VB2_MEMORY_USERPTR &&
  534. memory != VB2_MEMORY_DMABUF) {
  535. dprintk(1, "unsupported memory type\n");
  536. return -EINVAL;
  537. }
  538. if (type != q->type) {
  539. dprintk(1, "requested type is incorrect\n");
  540. return -EINVAL;
  541. }
  542. /*
  543. * Make sure all the required memory ops for given memory type
  544. * are available.
  545. */
  546. if (memory == VB2_MEMORY_MMAP && __verify_mmap_ops(q)) {
  547. dprintk(1, "MMAP for current setup unsupported\n");
  548. return -EINVAL;
  549. }
  550. if (memory == VB2_MEMORY_USERPTR && __verify_userptr_ops(q)) {
  551. dprintk(1, "USERPTR for current setup unsupported\n");
  552. return -EINVAL;
  553. }
  554. if (memory == VB2_MEMORY_DMABUF && __verify_dmabuf_ops(q)) {
  555. dprintk(1, "DMABUF for current setup unsupported\n");
  556. return -EINVAL;
  557. }
  558. /*
  559. * Place the busy tests at the end: -EBUSY can be ignored when
  560. * create_bufs is called with count == 0, but count == 0 should still
  561. * do the memory and type validation.
  562. */
  563. if (vb2_fileio_is_active(q)) {
  564. dprintk(1, "file io in progress\n");
  565. return -EBUSY;
  566. }
  567. return 0;
  568. }
  569. EXPORT_SYMBOL(vb2_verify_memory_type);
  570. int vb2_core_reqbufs(struct vb2_queue *q, enum vb2_memory memory,
  571. unsigned int *count)
  572. {
  573. unsigned int num_buffers, allocated_buffers, num_planes = 0;
  574. unsigned plane_sizes[VB2_MAX_PLANES] = { };
  575. int ret;
  576. if (q->streaming) {
  577. dprintk(1, "streaming active\n");
  578. return -EBUSY;
  579. }
  580. if (*count == 0 || q->num_buffers != 0 || q->memory != memory) {
  581. /*
  582. * We already have buffers allocated, so first check if they
  583. * are not in use and can be freed.
  584. */
  585. mutex_lock(&q->mmap_lock);
  586. if (q->memory == VB2_MEMORY_MMAP && __buffers_in_use(q)) {
  587. mutex_unlock(&q->mmap_lock);
  588. dprintk(1, "memory in use, cannot free\n");
  589. return -EBUSY;
  590. }
  591. /*
  592. * Call queue_cancel to clean up any buffers in the PREPARED or
  593. * QUEUED state which is possible if buffers were prepared or
  594. * queued without ever calling STREAMON.
  595. */
  596. __vb2_queue_cancel(q);
  597. ret = __vb2_queue_free(q, q->num_buffers);
  598. mutex_unlock(&q->mmap_lock);
  599. if (ret)
  600. return ret;
  601. /*
  602. * In case of REQBUFS(0) return immediately without calling
  603. * driver's queue_setup() callback and allocating resources.
  604. */
  605. if (*count == 0)
  606. return 0;
  607. }
  608. /*
  609. * Make sure the requested values and current defaults are sane.
  610. */
  611. num_buffers = min_t(unsigned int, *count, VB2_MAX_FRAME);
  612. num_buffers = max_t(unsigned int, num_buffers, q->min_buffers_needed);
  613. memset(q->alloc_devs, 0, sizeof(q->alloc_devs));
  614. q->memory = memory;
  615. /*
  616. * Ask the driver how many buffers and planes per buffer it requires.
  617. * Driver also sets the size and allocator context for each plane.
  618. */
  619. ret = call_qop(q, queue_setup, q, &num_buffers, &num_planes,
  620. plane_sizes, q->alloc_devs);
  621. if (ret)
  622. return ret;
  623. /* Finally, allocate buffers and video memory */
  624. allocated_buffers =
  625. __vb2_queue_alloc(q, memory, num_buffers, num_planes, plane_sizes);
  626. if (allocated_buffers == 0) {
  627. dprintk(1, "memory allocation failed\n");
  628. return -ENOMEM;
  629. }
  630. /*
  631. * There is no point in continuing if we can't allocate the minimum
  632. * number of buffers needed by this vb2_queue.
  633. */
  634. if (allocated_buffers < q->min_buffers_needed)
  635. ret = -ENOMEM;
  636. /*
  637. * Check if driver can handle the allocated number of buffers.
  638. */
  639. if (!ret && allocated_buffers < num_buffers) {
  640. num_buffers = allocated_buffers;
  641. /*
  642. * num_planes is set by the previous queue_setup(), but since it
  643. * signals to queue_setup() whether it is called from create_bufs()
  644. * vs reqbufs() we zero it here to signal that queue_setup() is
  645. * called for the reqbufs() case.
  646. */
  647. num_planes = 0;
  648. ret = call_qop(q, queue_setup, q, &num_buffers,
  649. &num_planes, plane_sizes, q->alloc_devs);
  650. if (!ret && allocated_buffers < num_buffers)
  651. ret = -ENOMEM;
  652. /*
  653. * Either the driver has accepted a smaller number of buffers,
  654. * or .queue_setup() returned an error
  655. */
  656. }
  657. mutex_lock(&q->mmap_lock);
  658. q->num_buffers = allocated_buffers;
  659. if (ret < 0) {
  660. /*
  661. * Note: __vb2_queue_free() will subtract 'allocated_buffers'
  662. * from q->num_buffers.
  663. */
  664. __vb2_queue_free(q, allocated_buffers);
  665. mutex_unlock(&q->mmap_lock);
  666. return ret;
  667. }
  668. mutex_unlock(&q->mmap_lock);
  669. /*
  670. * Return the number of successfully allocated buffers
  671. * to the userspace.
  672. */
  673. *count = allocated_buffers;
  674. q->waiting_for_buffers = !q->is_output;
  675. return 0;
  676. }
  677. EXPORT_SYMBOL_GPL(vb2_core_reqbufs);
  678. int vb2_core_create_bufs(struct vb2_queue *q, enum vb2_memory memory,
  679. unsigned int *count, unsigned requested_planes,
  680. const unsigned requested_sizes[])
  681. {
  682. unsigned int num_planes = 0, num_buffers, allocated_buffers;
  683. unsigned plane_sizes[VB2_MAX_PLANES] = { };
  684. int ret;
  685. if (q->num_buffers == VB2_MAX_FRAME) {
  686. dprintk(1, "maximum number of buffers already allocated\n");
  687. return -ENOBUFS;
  688. }
  689. if (!q->num_buffers) {
  690. memset(q->alloc_devs, 0, sizeof(q->alloc_devs));
  691. q->memory = memory;
  692. q->waiting_for_buffers = !q->is_output;
  693. }
  694. num_buffers = min(*count, VB2_MAX_FRAME - q->num_buffers);
  695. if (requested_planes && requested_sizes) {
  696. num_planes = requested_planes;
  697. memcpy(plane_sizes, requested_sizes, sizeof(plane_sizes));
  698. }
  699. /*
  700. * Ask the driver, whether the requested number of buffers, planes per
  701. * buffer and their sizes are acceptable
  702. */
  703. ret = call_qop(q, queue_setup, q, &num_buffers,
  704. &num_planes, plane_sizes, q->alloc_devs);
  705. if (ret)
  706. return ret;
  707. /* Finally, allocate buffers and video memory */
  708. allocated_buffers = __vb2_queue_alloc(q, memory, num_buffers,
  709. num_planes, plane_sizes);
  710. if (allocated_buffers == 0) {
  711. dprintk(1, "memory allocation failed\n");
  712. return -ENOMEM;
  713. }
  714. /*
  715. * Check if driver can handle the so far allocated number of buffers.
  716. */
  717. if (allocated_buffers < num_buffers) {
  718. num_buffers = allocated_buffers;
  719. /*
  720. * q->num_buffers contains the total number of buffers, that the
  721. * queue driver has set up
  722. */
  723. ret = call_qop(q, queue_setup, q, &num_buffers,
  724. &num_planes, plane_sizes, q->alloc_devs);
  725. if (!ret && allocated_buffers < num_buffers)
  726. ret = -ENOMEM;
  727. /*
  728. * Either the driver has accepted a smaller number of buffers,
  729. * or .queue_setup() returned an error
  730. */
  731. }
  732. mutex_lock(&q->mmap_lock);
  733. q->num_buffers += allocated_buffers;
  734. if (ret < 0) {
  735. /*
  736. * Note: __vb2_queue_free() will subtract 'allocated_buffers'
  737. * from q->num_buffers.
  738. */
  739. __vb2_queue_free(q, allocated_buffers);
  740. mutex_unlock(&q->mmap_lock);
  741. return -ENOMEM;
  742. }
  743. mutex_unlock(&q->mmap_lock);
  744. /*
  745. * Return the number of successfully allocated buffers
  746. * to the userspace.
  747. */
  748. *count = allocated_buffers;
  749. return 0;
  750. }
  751. EXPORT_SYMBOL_GPL(vb2_core_create_bufs);
  752. void *vb2_plane_vaddr(struct vb2_buffer *vb, unsigned int plane_no)
  753. {
  754. if (plane_no > vb->num_planes || !vb->planes[plane_no].mem_priv)
  755. return NULL;
  756. return call_ptr_memop(vb, vaddr, vb->planes[plane_no].mem_priv);
  757. }
  758. EXPORT_SYMBOL_GPL(vb2_plane_vaddr);
  759. void *vb2_plane_cookie(struct vb2_buffer *vb, unsigned int plane_no)
  760. {
  761. if (plane_no >= vb->num_planes || !vb->planes[plane_no].mem_priv)
  762. return NULL;
  763. return call_ptr_memop(vb, cookie, vb->planes[plane_no].mem_priv);
  764. }
  765. EXPORT_SYMBOL_GPL(vb2_plane_cookie);
  766. void vb2_buffer_done(struct vb2_buffer *vb, enum vb2_buffer_state state)
  767. {
  768. struct vb2_queue *q = vb->vb2_queue;
  769. unsigned long flags;
  770. unsigned int plane;
  771. if (WARN_ON(vb->state != VB2_BUF_STATE_ACTIVE))
  772. return;
  773. if (WARN_ON(state != VB2_BUF_STATE_DONE &&
  774. state != VB2_BUF_STATE_ERROR &&
  775. state != VB2_BUF_STATE_QUEUED &&
  776. state != VB2_BUF_STATE_REQUEUEING))
  777. state = VB2_BUF_STATE_ERROR;
  778. #ifdef CONFIG_VIDEO_ADV_DEBUG
  779. /*
  780. * Although this is not a callback, it still does have to balance
  781. * with the buf_queue op. So update this counter manually.
  782. */
  783. vb->cnt_buf_done++;
  784. #endif
  785. dprintk(4, "done processing on buffer %d, state: %d\n",
  786. vb->index, state);
  787. /* sync buffers */
  788. for (plane = 0; plane < vb->num_planes; ++plane)
  789. call_void_memop(vb, finish, vb->planes[plane].mem_priv);
  790. spin_lock_irqsave(&q->done_lock, flags);
  791. if (state == VB2_BUF_STATE_QUEUED ||
  792. state == VB2_BUF_STATE_REQUEUEING) {
  793. vb->state = VB2_BUF_STATE_QUEUED;
  794. } else {
  795. /* Add the buffer to the done buffers list */
  796. list_add_tail(&vb->done_entry, &q->done_list);
  797. vb->state = state;
  798. }
  799. atomic_dec(&q->owned_by_drv_count);
  800. spin_unlock_irqrestore(&q->done_lock, flags);
  801. trace_vb2_buf_done(q, vb);
  802. switch (state) {
  803. case VB2_BUF_STATE_QUEUED:
  804. return;
  805. case VB2_BUF_STATE_REQUEUEING:
  806. if (q->start_streaming_called)
  807. __enqueue_in_driver(vb);
  808. return;
  809. default:
  810. /* Inform any processes that may be waiting for buffers */
  811. wake_up(&q->done_wq);
  812. break;
  813. }
  814. }
  815. EXPORT_SYMBOL_GPL(vb2_buffer_done);
  816. void vb2_discard_done(struct vb2_queue *q)
  817. {
  818. struct vb2_buffer *vb;
  819. unsigned long flags;
  820. spin_lock_irqsave(&q->done_lock, flags);
  821. list_for_each_entry(vb, &q->done_list, done_entry)
  822. vb->state = VB2_BUF_STATE_ERROR;
  823. spin_unlock_irqrestore(&q->done_lock, flags);
  824. }
  825. EXPORT_SYMBOL_GPL(vb2_discard_done);
  826. /**
  827. * __qbuf_mmap() - handle qbuf of an MMAP buffer
  828. */
  829. static int __qbuf_mmap(struct vb2_buffer *vb, const void *pb)
  830. {
  831. int ret = 0;
  832. if (pb)
  833. ret = call_bufop(vb->vb2_queue, fill_vb2_buffer,
  834. vb, pb, vb->planes);
  835. return ret ? ret : call_vb_qop(vb, buf_prepare, vb);
  836. }
  837. /**
  838. * __qbuf_userptr() - handle qbuf of a USERPTR buffer
  839. */
  840. static int __qbuf_userptr(struct vb2_buffer *vb, const void *pb)
  841. {
  842. struct vb2_plane planes[VB2_MAX_PLANES];
  843. struct vb2_queue *q = vb->vb2_queue;
  844. void *mem_priv;
  845. unsigned int plane;
  846. int ret = 0;
  847. enum dma_data_direction dma_dir =
  848. q->is_output ? DMA_TO_DEVICE : DMA_FROM_DEVICE;
  849. bool reacquired = vb->planes[0].mem_priv == NULL;
  850. memset(planes, 0, sizeof(planes[0]) * vb->num_planes);
  851. /* Copy relevant information provided by the userspace */
  852. if (pb)
  853. ret = call_bufop(vb->vb2_queue, fill_vb2_buffer,
  854. vb, pb, planes);
  855. if (ret)
  856. return ret;
  857. for (plane = 0; plane < vb->num_planes; ++plane) {
  858. /* Skip the plane if already verified */
  859. if (vb->planes[plane].m.userptr &&
  860. vb->planes[plane].m.userptr == planes[plane].m.userptr
  861. && vb->planes[plane].length == planes[plane].length)
  862. continue;
  863. dprintk(3, "userspace address for plane %d changed, reacquiring memory\n",
  864. plane);
  865. /* Check if the provided plane buffer is large enough */
  866. if (planes[plane].length < vb->planes[plane].min_length) {
  867. dprintk(1, "provided buffer size %u is less than setup size %u for plane %d\n",
  868. planes[plane].length,
  869. vb->planes[plane].min_length,
  870. plane);
  871. ret = -EINVAL;
  872. goto err;
  873. }
  874. /* Release previously acquired memory if present */
  875. if (vb->planes[plane].mem_priv) {
  876. if (!reacquired) {
  877. reacquired = true;
  878. call_void_vb_qop(vb, buf_cleanup, vb);
  879. }
  880. call_void_memop(vb, put_userptr, vb->planes[plane].mem_priv);
  881. }
  882. vb->planes[plane].mem_priv = NULL;
  883. vb->planes[plane].bytesused = 0;
  884. vb->planes[plane].length = 0;
  885. vb->planes[plane].m.userptr = 0;
  886. vb->planes[plane].data_offset = 0;
  887. /* Acquire each plane's memory */
  888. mem_priv = call_ptr_memop(vb, get_userptr,
  889. q->alloc_devs[plane] ? : q->dev,
  890. planes[plane].m.userptr,
  891. planes[plane].length, dma_dir);
  892. if (IS_ERR(mem_priv)) {
  893. dprintk(1, "failed acquiring userspace memory for plane %d\n",
  894. plane);
  895. ret = PTR_ERR(mem_priv);
  896. goto err;
  897. }
  898. vb->planes[plane].mem_priv = mem_priv;
  899. }
  900. /*
  901. * Now that everything is in order, copy relevant information
  902. * provided by userspace.
  903. */
  904. for (plane = 0; plane < vb->num_planes; ++plane) {
  905. vb->planes[plane].bytesused = planes[plane].bytesused;
  906. vb->planes[plane].length = planes[plane].length;
  907. vb->planes[plane].m.userptr = planes[plane].m.userptr;
  908. vb->planes[plane].data_offset = planes[plane].data_offset;
  909. }
  910. if (reacquired) {
  911. /*
  912. * One or more planes changed, so we must call buf_init to do
  913. * the driver-specific initialization on the newly acquired
  914. * buffer, if provided.
  915. */
  916. ret = call_vb_qop(vb, buf_init, vb);
  917. if (ret) {
  918. dprintk(1, "buffer initialization failed\n");
  919. goto err;
  920. }
  921. }
  922. ret = call_vb_qop(vb, buf_prepare, vb);
  923. if (ret) {
  924. dprintk(1, "buffer preparation failed\n");
  925. call_void_vb_qop(vb, buf_cleanup, vb);
  926. goto err;
  927. }
  928. return 0;
  929. err:
  930. /* In case of errors, release planes that were already acquired */
  931. for (plane = 0; plane < vb->num_planes; ++plane) {
  932. if (vb->planes[plane].mem_priv)
  933. call_void_memop(vb, put_userptr,
  934. vb->planes[plane].mem_priv);
  935. vb->planes[plane].mem_priv = NULL;
  936. vb->planes[plane].m.userptr = 0;
  937. vb->planes[plane].length = 0;
  938. }
  939. return ret;
  940. }
  941. /**
  942. * __qbuf_dmabuf() - handle qbuf of a DMABUF buffer
  943. */
  944. static int __qbuf_dmabuf(struct vb2_buffer *vb, const void *pb)
  945. {
  946. struct vb2_plane planes[VB2_MAX_PLANES];
  947. struct vb2_queue *q = vb->vb2_queue;
  948. void *mem_priv;
  949. unsigned int plane;
  950. int ret = 0;
  951. enum dma_data_direction dma_dir =
  952. q->is_output ? DMA_TO_DEVICE : DMA_FROM_DEVICE;
  953. bool reacquired = vb->planes[0].mem_priv == NULL;
  954. memset(planes, 0, sizeof(planes[0]) * vb->num_planes);
  955. /* Copy relevant information provided by the userspace */
  956. if (pb)
  957. ret = call_bufop(vb->vb2_queue, fill_vb2_buffer,
  958. vb, pb, planes);
  959. if (ret)
  960. return ret;
  961. for (plane = 0; plane < vb->num_planes; ++plane) {
  962. struct dma_buf *dbuf = dma_buf_get(planes[plane].m.fd);
  963. if (IS_ERR_OR_NULL(dbuf)) {
  964. dprintk(1, "invalid dmabuf fd for plane %d\n",
  965. plane);
  966. ret = -EINVAL;
  967. goto err;
  968. }
  969. /* use DMABUF size if length is not provided */
  970. if (planes[plane].length == 0)
  971. planes[plane].length = dbuf->size;
  972. if (planes[plane].length < vb->planes[plane].min_length) {
  973. dprintk(1, "invalid dmabuf length %u for plane %d, minimum length %u\n",
  974. planes[plane].length, plane,
  975. vb->planes[plane].min_length);
  976. dma_buf_put(dbuf);
  977. ret = -EINVAL;
  978. goto err;
  979. }
  980. /* Skip the plane if already verified */
  981. if (dbuf == vb->planes[plane].dbuf &&
  982. vb->planes[plane].length == planes[plane].length) {
  983. dma_buf_put(dbuf);
  984. continue;
  985. }
  986. dprintk(1, "buffer for plane %d changed\n", plane);
  987. if (!reacquired) {
  988. reacquired = true;
  989. call_void_vb_qop(vb, buf_cleanup, vb);
  990. }
  991. /* Release previously acquired memory if present */
  992. __vb2_plane_dmabuf_put(vb, &vb->planes[plane]);
  993. vb->planes[plane].bytesused = 0;
  994. vb->planes[plane].length = 0;
  995. vb->planes[plane].m.fd = 0;
  996. vb->planes[plane].data_offset = 0;
  997. /* Acquire each plane's memory */
  998. mem_priv = call_ptr_memop(vb, attach_dmabuf,
  999. q->alloc_devs[plane] ? : q->dev,
  1000. dbuf, planes[plane].length, dma_dir);
  1001. if (IS_ERR(mem_priv)) {
  1002. dprintk(1, "failed to attach dmabuf\n");
  1003. ret = PTR_ERR(mem_priv);
  1004. dma_buf_put(dbuf);
  1005. goto err;
  1006. }
  1007. vb->planes[plane].dbuf = dbuf;
  1008. vb->planes[plane].mem_priv = mem_priv;
  1009. }
  1010. /*
  1011. * This pins the buffer(s) with dma_buf_map_attachment()). It's done
  1012. * here instead just before the DMA, while queueing the buffer(s) so
  1013. * userspace knows sooner rather than later if the dma-buf map fails.
  1014. */
  1015. for (plane = 0; plane < vb->num_planes; ++plane) {
  1016. ret = call_memop(vb, map_dmabuf, vb->planes[plane].mem_priv);
  1017. if (ret) {
  1018. dprintk(1, "failed to map dmabuf for plane %d\n",
  1019. plane);
  1020. goto err;
  1021. }
  1022. vb->planes[plane].dbuf_mapped = 1;
  1023. }
  1024. /*
  1025. * Now that everything is in order, copy relevant information
  1026. * provided by userspace.
  1027. */
  1028. for (plane = 0; plane < vb->num_planes; ++plane) {
  1029. vb->planes[plane].bytesused = planes[plane].bytesused;
  1030. vb->planes[plane].length = planes[plane].length;
  1031. vb->planes[plane].m.fd = planes[plane].m.fd;
  1032. vb->planes[plane].data_offset = planes[plane].data_offset;
  1033. }
  1034. if (reacquired) {
  1035. /*
  1036. * Call driver-specific initialization on the newly acquired buffer,
  1037. * if provided.
  1038. */
  1039. ret = call_vb_qop(vb, buf_init, vb);
  1040. if (ret) {
  1041. dprintk(1, "buffer initialization failed\n");
  1042. goto err;
  1043. }
  1044. }
  1045. ret = call_vb_qop(vb, buf_prepare, vb);
  1046. if (ret) {
  1047. dprintk(1, "buffer preparation failed\n");
  1048. call_void_vb_qop(vb, buf_cleanup, vb);
  1049. goto err;
  1050. }
  1051. return 0;
  1052. err:
  1053. /* In case of errors, release planes that were already acquired */
  1054. __vb2_buf_dmabuf_put(vb);
  1055. return ret;
  1056. }
  1057. /**
  1058. * __enqueue_in_driver() - enqueue a vb2_buffer in driver for processing
  1059. */
  1060. static void __enqueue_in_driver(struct vb2_buffer *vb)
  1061. {
  1062. struct vb2_queue *q = vb->vb2_queue;
  1063. unsigned int plane;
  1064. vb->state = VB2_BUF_STATE_ACTIVE;
  1065. atomic_inc(&q->owned_by_drv_count);
  1066. trace_vb2_buf_queue(q, vb);
  1067. /* sync buffers */
  1068. for (plane = 0; plane < vb->num_planes; ++plane)
  1069. call_void_memop(vb, prepare, vb->planes[plane].mem_priv);
  1070. call_void_vb_qop(vb, buf_queue, vb);
  1071. }
  1072. static int __buf_prepare(struct vb2_buffer *vb, const void *pb)
  1073. {
  1074. struct vb2_queue *q = vb->vb2_queue;
  1075. int ret;
  1076. if (q->error) {
  1077. dprintk(1, "fatal error occurred on queue\n");
  1078. return -EIO;
  1079. }
  1080. vb->state = VB2_BUF_STATE_PREPARING;
  1081. switch (q->memory) {
  1082. case VB2_MEMORY_MMAP:
  1083. ret = __qbuf_mmap(vb, pb);
  1084. break;
  1085. case VB2_MEMORY_USERPTR:
  1086. ret = __qbuf_userptr(vb, pb);
  1087. break;
  1088. case VB2_MEMORY_DMABUF:
  1089. ret = __qbuf_dmabuf(vb, pb);
  1090. break;
  1091. default:
  1092. WARN(1, "Invalid queue type\n");
  1093. ret = -EINVAL;
  1094. }
  1095. if (ret)
  1096. dprintk(1, "buffer preparation failed: %d\n", ret);
  1097. vb->state = ret ? VB2_BUF_STATE_DEQUEUED : VB2_BUF_STATE_PREPARED;
  1098. return ret;
  1099. }
  1100. int vb2_core_prepare_buf(struct vb2_queue *q, unsigned int index, void *pb)
  1101. {
  1102. struct vb2_buffer *vb;
  1103. int ret;
  1104. vb = q->bufs[index];
  1105. if (vb->state != VB2_BUF_STATE_DEQUEUED) {
  1106. dprintk(1, "invalid buffer state %d\n",
  1107. vb->state);
  1108. return -EINVAL;
  1109. }
  1110. ret = __buf_prepare(vb, pb);
  1111. if (ret)
  1112. return ret;
  1113. /* Fill buffer information for the userspace */
  1114. call_void_bufop(q, fill_user_buffer, vb, pb);
  1115. dprintk(1, "prepare of buffer %d succeeded\n", vb->index);
  1116. return ret;
  1117. }
  1118. EXPORT_SYMBOL_GPL(vb2_core_prepare_buf);
  1119. /**
  1120. * vb2_start_streaming() - Attempt to start streaming.
  1121. * @q: videobuf2 queue
  1122. *
  1123. * Attempt to start streaming. When this function is called there must be
  1124. * at least q->min_buffers_needed buffers queued up (i.e. the minimum
  1125. * number of buffers required for the DMA engine to function). If the
  1126. * @start_streaming op fails it is supposed to return all the driver-owned
  1127. * buffers back to vb2 in state QUEUED. Check if that happened and if
  1128. * not warn and reclaim them forcefully.
  1129. */
  1130. static int vb2_start_streaming(struct vb2_queue *q)
  1131. {
  1132. struct vb2_buffer *vb;
  1133. int ret;
  1134. /*
  1135. * If any buffers were queued before streamon,
  1136. * we can now pass them to driver for processing.
  1137. */
  1138. list_for_each_entry(vb, &q->queued_list, queued_entry)
  1139. __enqueue_in_driver(vb);
  1140. /* Tell the driver to start streaming */
  1141. q->start_streaming_called = 1;
  1142. ret = call_qop(q, start_streaming, q,
  1143. atomic_read(&q->owned_by_drv_count));
  1144. if (!ret)
  1145. return 0;
  1146. q->start_streaming_called = 0;
  1147. dprintk(1, "driver refused to start streaming\n");
  1148. /*
  1149. * If you see this warning, then the driver isn't cleaning up properly
  1150. * after a failed start_streaming(). See the start_streaming()
  1151. * documentation in videobuf2-core.h for more information how buffers
  1152. * should be returned to vb2 in start_streaming().
  1153. */
  1154. if (WARN_ON(atomic_read(&q->owned_by_drv_count))) {
  1155. unsigned i;
  1156. /*
  1157. * Forcefully reclaim buffers if the driver did not
  1158. * correctly return them to vb2.
  1159. */
  1160. for (i = 0; i < q->num_buffers; ++i) {
  1161. vb = q->bufs[i];
  1162. if (vb->state == VB2_BUF_STATE_ACTIVE)
  1163. vb2_buffer_done(vb, VB2_BUF_STATE_QUEUED);
  1164. }
  1165. /* Must be zero now */
  1166. WARN_ON(atomic_read(&q->owned_by_drv_count));
  1167. }
  1168. /*
  1169. * If done_list is not empty, then start_streaming() didn't call
  1170. * vb2_buffer_done(vb, VB2_BUF_STATE_QUEUED) but STATE_ERROR or
  1171. * STATE_DONE.
  1172. */
  1173. WARN_ON(!list_empty(&q->done_list));
  1174. return ret;
  1175. }
  1176. int vb2_core_qbuf(struct vb2_queue *q, unsigned int index, void *pb)
  1177. {
  1178. struct vb2_buffer *vb;
  1179. int ret;
  1180. vb = q->bufs[index];
  1181. switch (vb->state) {
  1182. case VB2_BUF_STATE_DEQUEUED:
  1183. ret = __buf_prepare(vb, pb);
  1184. if (ret)
  1185. return ret;
  1186. break;
  1187. case VB2_BUF_STATE_PREPARED:
  1188. break;
  1189. case VB2_BUF_STATE_PREPARING:
  1190. dprintk(1, "buffer still being prepared\n");
  1191. return -EINVAL;
  1192. default:
  1193. dprintk(1, "invalid buffer state %d\n", vb->state);
  1194. return -EINVAL;
  1195. }
  1196. /*
  1197. * Add to the queued buffers list, a buffer will stay on it until
  1198. * dequeued in dqbuf.
  1199. */
  1200. list_add_tail(&vb->queued_entry, &q->queued_list);
  1201. q->queued_count++;
  1202. q->waiting_for_buffers = false;
  1203. vb->state = VB2_BUF_STATE_QUEUED;
  1204. if (pb)
  1205. call_void_bufop(q, copy_timestamp, vb, pb);
  1206. trace_vb2_qbuf(q, vb);
  1207. /*
  1208. * If already streaming, give the buffer to driver for processing.
  1209. * If not, the buffer will be given to driver on next streamon.
  1210. */
  1211. if (q->start_streaming_called)
  1212. __enqueue_in_driver(vb);
  1213. /* Fill buffer information for the userspace */
  1214. if (pb)
  1215. call_void_bufop(q, fill_user_buffer, vb, pb);
  1216. /*
  1217. * If streamon has been called, and we haven't yet called
  1218. * start_streaming() since not enough buffers were queued, and
  1219. * we now have reached the minimum number of queued buffers,
  1220. * then we can finally call start_streaming().
  1221. */
  1222. if (q->streaming && !q->start_streaming_called &&
  1223. q->queued_count >= q->min_buffers_needed) {
  1224. ret = vb2_start_streaming(q);
  1225. if (ret)
  1226. return ret;
  1227. }
  1228. dprintk(1, "qbuf of buffer %d succeeded\n", vb->index);
  1229. return 0;
  1230. }
  1231. EXPORT_SYMBOL_GPL(vb2_core_qbuf);
  1232. /**
  1233. * __vb2_wait_for_done_vb() - wait for a buffer to become available
  1234. * for dequeuing
  1235. *
  1236. * Will sleep if required for nonblocking == false.
  1237. */
  1238. static int __vb2_wait_for_done_vb(struct vb2_queue *q, int nonblocking)
  1239. {
  1240. /*
  1241. * All operations on vb_done_list are performed under done_lock
  1242. * spinlock protection. However, buffers may be removed from
  1243. * it and returned to userspace only while holding both driver's
  1244. * lock and the done_lock spinlock. Thus we can be sure that as
  1245. * long as we hold the driver's lock, the list will remain not
  1246. * empty if list_empty() check succeeds.
  1247. */
  1248. for (;;) {
  1249. int ret;
  1250. if (!q->streaming) {
  1251. dprintk(1, "streaming off, will not wait for buffers\n");
  1252. return -EINVAL;
  1253. }
  1254. if (q->error) {
  1255. dprintk(1, "Queue in error state, will not wait for buffers\n");
  1256. return -EIO;
  1257. }
  1258. if (q->last_buffer_dequeued) {
  1259. dprintk(3, "last buffer dequeued already, will not wait for buffers\n");
  1260. return -EPIPE;
  1261. }
  1262. if (!list_empty(&q->done_list)) {
  1263. /*
  1264. * Found a buffer that we were waiting for.
  1265. */
  1266. break;
  1267. }
  1268. if (nonblocking) {
  1269. dprintk(1, "nonblocking and no buffers to dequeue, will not wait\n");
  1270. return -EAGAIN;
  1271. }
  1272. /*
  1273. * We are streaming and blocking, wait for another buffer to
  1274. * become ready or for streamoff. Driver's lock is released to
  1275. * allow streamoff or qbuf to be called while waiting.
  1276. */
  1277. call_void_qop(q, wait_prepare, q);
  1278. /*
  1279. * All locks have been released, it is safe to sleep now.
  1280. */
  1281. dprintk(3, "will sleep waiting for buffers\n");
  1282. ret = wait_event_interruptible(q->done_wq,
  1283. !list_empty(&q->done_list) || !q->streaming ||
  1284. q->error);
  1285. /*
  1286. * We need to reevaluate both conditions again after reacquiring
  1287. * the locks or return an error if one occurred.
  1288. */
  1289. call_void_qop(q, wait_finish, q);
  1290. if (ret) {
  1291. dprintk(1, "sleep was interrupted\n");
  1292. return ret;
  1293. }
  1294. }
  1295. return 0;
  1296. }
  1297. /**
  1298. * __vb2_get_done_vb() - get a buffer ready for dequeuing
  1299. *
  1300. * Will sleep if required for nonblocking == false.
  1301. */
  1302. static int __vb2_get_done_vb(struct vb2_queue *q, struct vb2_buffer **vb,
  1303. void *pb, int nonblocking)
  1304. {
  1305. unsigned long flags;
  1306. int ret = 0;
  1307. /*
  1308. * Wait for at least one buffer to become available on the done_list.
  1309. */
  1310. ret = __vb2_wait_for_done_vb(q, nonblocking);
  1311. if (ret)
  1312. return ret;
  1313. /*
  1314. * Driver's lock has been held since we last verified that done_list
  1315. * is not empty, so no need for another list_empty(done_list) check.
  1316. */
  1317. spin_lock_irqsave(&q->done_lock, flags);
  1318. *vb = list_first_entry(&q->done_list, struct vb2_buffer, done_entry);
  1319. /*
  1320. * Only remove the buffer from done_list if all planes can be
  1321. * handled. Some cases such as V4L2 file I/O and DVB have pb
  1322. * == NULL; skip the check then as there's nothing to verify.
  1323. */
  1324. if (pb)
  1325. ret = call_bufop(q, verify_planes_array, *vb, pb);
  1326. if (!ret)
  1327. list_del(&(*vb)->done_entry);
  1328. spin_unlock_irqrestore(&q->done_lock, flags);
  1329. return ret;
  1330. }
  1331. int vb2_wait_for_all_buffers(struct vb2_queue *q)
  1332. {
  1333. if (!q->streaming) {
  1334. dprintk(1, "streaming off, will not wait for buffers\n");
  1335. return -EINVAL;
  1336. }
  1337. if (q->start_streaming_called)
  1338. wait_event(q->done_wq, !atomic_read(&q->owned_by_drv_count));
  1339. return 0;
  1340. }
  1341. EXPORT_SYMBOL_GPL(vb2_wait_for_all_buffers);
  1342. /**
  1343. * __vb2_dqbuf() - bring back the buffer to the DEQUEUED state
  1344. */
  1345. static void __vb2_dqbuf(struct vb2_buffer *vb)
  1346. {
  1347. struct vb2_queue *q = vb->vb2_queue;
  1348. unsigned int i;
  1349. /* nothing to do if the buffer is already dequeued */
  1350. if (vb->state == VB2_BUF_STATE_DEQUEUED)
  1351. return;
  1352. vb->state = VB2_BUF_STATE_DEQUEUED;
  1353. /* unmap DMABUF buffer */
  1354. if (q->memory == VB2_MEMORY_DMABUF)
  1355. for (i = 0; i < vb->num_planes; ++i) {
  1356. if (!vb->planes[i].dbuf_mapped)
  1357. continue;
  1358. call_void_memop(vb, unmap_dmabuf, vb->planes[i].mem_priv);
  1359. vb->planes[i].dbuf_mapped = 0;
  1360. }
  1361. }
  1362. int vb2_core_dqbuf(struct vb2_queue *q, unsigned int *pindex, void *pb,
  1363. bool nonblocking)
  1364. {
  1365. struct vb2_buffer *vb = NULL;
  1366. int ret;
  1367. ret = __vb2_get_done_vb(q, &vb, pb, nonblocking);
  1368. if (ret < 0)
  1369. return ret;
  1370. switch (vb->state) {
  1371. case VB2_BUF_STATE_DONE:
  1372. dprintk(3, "returning done buffer\n");
  1373. break;
  1374. case VB2_BUF_STATE_ERROR:
  1375. dprintk(3, "returning done buffer with errors\n");
  1376. break;
  1377. default:
  1378. dprintk(1, "invalid buffer state\n");
  1379. return -EINVAL;
  1380. }
  1381. call_void_vb_qop(vb, buf_finish, vb);
  1382. if (pindex)
  1383. *pindex = vb->index;
  1384. /* Fill buffer information for the userspace */
  1385. if (pb)
  1386. call_void_bufop(q, fill_user_buffer, vb, pb);
  1387. /* Remove from videobuf queue */
  1388. list_del(&vb->queued_entry);
  1389. q->queued_count--;
  1390. trace_vb2_dqbuf(q, vb);
  1391. /* go back to dequeued state */
  1392. __vb2_dqbuf(vb);
  1393. dprintk(1, "dqbuf of buffer %d, with state %d\n",
  1394. vb->index, vb->state);
  1395. return 0;
  1396. }
  1397. EXPORT_SYMBOL_GPL(vb2_core_dqbuf);
  1398. /**
  1399. * __vb2_queue_cancel() - cancel and stop (pause) streaming
  1400. *
  1401. * Removes all queued buffers from driver's queue and all buffers queued by
  1402. * userspace from videobuf's queue. Returns to state after reqbufs.
  1403. */
  1404. static void __vb2_queue_cancel(struct vb2_queue *q)
  1405. {
  1406. unsigned int i;
  1407. /*
  1408. * Tell driver to stop all transactions and release all queued
  1409. * buffers.
  1410. */
  1411. if (q->start_streaming_called)
  1412. call_void_qop(q, stop_streaming, q);
  1413. /*
  1414. * If you see this warning, then the driver isn't cleaning up properly
  1415. * in stop_streaming(). See the stop_streaming() documentation in
  1416. * videobuf2-core.h for more information how buffers should be returned
  1417. * to vb2 in stop_streaming().
  1418. */
  1419. if (WARN_ON(atomic_read(&q->owned_by_drv_count))) {
  1420. for (i = 0; i < q->num_buffers; ++i)
  1421. if (q->bufs[i]->state == VB2_BUF_STATE_ACTIVE)
  1422. vb2_buffer_done(q->bufs[i], VB2_BUF_STATE_ERROR);
  1423. /* Must be zero now */
  1424. WARN_ON(atomic_read(&q->owned_by_drv_count));
  1425. }
  1426. q->streaming = 0;
  1427. q->start_streaming_called = 0;
  1428. q->queued_count = 0;
  1429. q->error = 0;
  1430. /*
  1431. * Remove all buffers from videobuf's list...
  1432. */
  1433. INIT_LIST_HEAD(&q->queued_list);
  1434. /*
  1435. * ...and done list; userspace will not receive any buffers it
  1436. * has not already dequeued before initiating cancel.
  1437. */
  1438. INIT_LIST_HEAD(&q->done_list);
  1439. atomic_set(&q->owned_by_drv_count, 0);
  1440. wake_up_all(&q->done_wq);
  1441. /*
  1442. * Reinitialize all buffers for next use.
  1443. * Make sure to call buf_finish for any queued buffers. Normally
  1444. * that's done in dqbuf, but that's not going to happen when we
  1445. * cancel the whole queue. Note: this code belongs here, not in
  1446. * __vb2_dqbuf() since in vb2_core_dqbuf() there is a critical
  1447. * call to __fill_user_buffer() after buf_finish(). That order can't
  1448. * be changed, so we can't move the buf_finish() to __vb2_dqbuf().
  1449. */
  1450. for (i = 0; i < q->num_buffers; ++i) {
  1451. struct vb2_buffer *vb = q->bufs[i];
  1452. if (vb->state != VB2_BUF_STATE_DEQUEUED) {
  1453. vb->state = VB2_BUF_STATE_PREPARED;
  1454. call_void_vb_qop(vb, buf_finish, vb);
  1455. }
  1456. __vb2_dqbuf(vb);
  1457. }
  1458. }
  1459. int vb2_core_streamon(struct vb2_queue *q, unsigned int type)
  1460. {
  1461. int ret;
  1462. if (type != q->type) {
  1463. dprintk(1, "invalid stream type\n");
  1464. return -EINVAL;
  1465. }
  1466. if (q->streaming) {
  1467. dprintk(3, "already streaming\n");
  1468. return 0;
  1469. }
  1470. if (!q->num_buffers) {
  1471. dprintk(1, "no buffers have been allocated\n");
  1472. return -EINVAL;
  1473. }
  1474. if (q->num_buffers < q->min_buffers_needed) {
  1475. dprintk(1, "need at least %u allocated buffers\n",
  1476. q->min_buffers_needed);
  1477. return -EINVAL;
  1478. }
  1479. /*
  1480. * Tell driver to start streaming provided sufficient buffers
  1481. * are available.
  1482. */
  1483. if (q->queued_count >= q->min_buffers_needed) {
  1484. ret = v4l_vb2q_enable_media_source(q);
  1485. if (ret)
  1486. return ret;
  1487. ret = vb2_start_streaming(q);
  1488. if (ret) {
  1489. __vb2_queue_cancel(q);
  1490. return ret;
  1491. }
  1492. }
  1493. q->streaming = 1;
  1494. dprintk(3, "successful\n");
  1495. return 0;
  1496. }
  1497. EXPORT_SYMBOL_GPL(vb2_core_streamon);
  1498. void vb2_queue_error(struct vb2_queue *q)
  1499. {
  1500. q->error = 1;
  1501. wake_up_all(&q->done_wq);
  1502. }
  1503. EXPORT_SYMBOL_GPL(vb2_queue_error);
  1504. int vb2_core_streamoff(struct vb2_queue *q, unsigned int type)
  1505. {
  1506. if (type != q->type) {
  1507. dprintk(1, "invalid stream type\n");
  1508. return -EINVAL;
  1509. }
  1510. /*
  1511. * Cancel will pause streaming and remove all buffers from the driver
  1512. * and videobuf, effectively returning control over them to userspace.
  1513. *
  1514. * Note that we do this even if q->streaming == 0: if you prepare or
  1515. * queue buffers, and then call streamoff without ever having called
  1516. * streamon, you would still expect those buffers to be returned to
  1517. * their normal dequeued state.
  1518. */
  1519. __vb2_queue_cancel(q);
  1520. q->waiting_for_buffers = !q->is_output;
  1521. q->last_buffer_dequeued = false;
  1522. dprintk(3, "successful\n");
  1523. return 0;
  1524. }
  1525. EXPORT_SYMBOL_GPL(vb2_core_streamoff);
  1526. /**
  1527. * __find_plane_by_offset() - find plane associated with the given offset off
  1528. */
  1529. static int __find_plane_by_offset(struct vb2_queue *q, unsigned long off,
  1530. unsigned int *_buffer, unsigned int *_plane)
  1531. {
  1532. struct vb2_buffer *vb;
  1533. unsigned int buffer, plane;
  1534. /*
  1535. * Go over all buffers and their planes, comparing the given offset
  1536. * with an offset assigned to each plane. If a match is found,
  1537. * return its buffer and plane numbers.
  1538. */
  1539. for (buffer = 0; buffer < q->num_buffers; ++buffer) {
  1540. vb = q->bufs[buffer];
  1541. for (plane = 0; plane < vb->num_planes; ++plane) {
  1542. if (vb->planes[plane].m.offset == off) {
  1543. *_buffer = buffer;
  1544. *_plane = plane;
  1545. return 0;
  1546. }
  1547. }
  1548. }
  1549. return -EINVAL;
  1550. }
  1551. int vb2_core_expbuf(struct vb2_queue *q, int *fd, unsigned int type,
  1552. unsigned int index, unsigned int plane, unsigned int flags)
  1553. {
  1554. struct vb2_buffer *vb = NULL;
  1555. struct vb2_plane *vb_plane;
  1556. int ret;
  1557. struct dma_buf *dbuf;
  1558. if (q->memory != VB2_MEMORY_MMAP) {
  1559. dprintk(1, "queue is not currently set up for mmap\n");
  1560. return -EINVAL;
  1561. }
  1562. if (!q->mem_ops->get_dmabuf) {
  1563. dprintk(1, "queue does not support DMA buffer exporting\n");
  1564. return -EINVAL;
  1565. }
  1566. if (flags & ~(O_CLOEXEC | O_ACCMODE)) {
  1567. dprintk(1, "queue does support only O_CLOEXEC and access mode flags\n");
  1568. return -EINVAL;
  1569. }
  1570. if (type != q->type) {
  1571. dprintk(1, "invalid buffer type\n");
  1572. return -EINVAL;
  1573. }
  1574. if (index >= q->num_buffers) {
  1575. dprintk(1, "buffer index out of range\n");
  1576. return -EINVAL;
  1577. }
  1578. vb = q->bufs[index];
  1579. if (plane >= vb->num_planes) {
  1580. dprintk(1, "buffer plane out of range\n");
  1581. return -EINVAL;
  1582. }
  1583. if (vb2_fileio_is_active(q)) {
  1584. dprintk(1, "expbuf: file io in progress\n");
  1585. return -EBUSY;
  1586. }
  1587. vb_plane = &vb->planes[plane];
  1588. dbuf = call_ptr_memop(vb, get_dmabuf, vb_plane->mem_priv,
  1589. flags & O_ACCMODE);
  1590. if (IS_ERR_OR_NULL(dbuf)) {
  1591. dprintk(1, "failed to export buffer %d, plane %d\n",
  1592. index, plane);
  1593. return -EINVAL;
  1594. }
  1595. ret = dma_buf_fd(dbuf, flags & ~O_ACCMODE);
  1596. if (ret < 0) {
  1597. dprintk(3, "buffer %d, plane %d failed to export (%d)\n",
  1598. index, plane, ret);
  1599. dma_buf_put(dbuf);
  1600. return ret;
  1601. }
  1602. dprintk(3, "buffer %d, plane %d exported as %d descriptor\n",
  1603. index, plane, ret);
  1604. *fd = ret;
  1605. return 0;
  1606. }
  1607. EXPORT_SYMBOL_GPL(vb2_core_expbuf);
  1608. int vb2_mmap(struct vb2_queue *q, struct vm_area_struct *vma)
  1609. {
  1610. unsigned long off = vma->vm_pgoff << PAGE_SHIFT;
  1611. struct vb2_buffer *vb;
  1612. unsigned int buffer = 0, plane = 0;
  1613. int ret;
  1614. unsigned long length;
  1615. if (q->memory != VB2_MEMORY_MMAP) {
  1616. dprintk(1, "queue is not currently set up for mmap\n");
  1617. return -EINVAL;
  1618. }
  1619. /*
  1620. * Check memory area access mode.
  1621. */
  1622. if (!(vma->vm_flags & VM_SHARED)) {
  1623. dprintk(1, "invalid vma flags, VM_SHARED needed\n");
  1624. return -EINVAL;
  1625. }
  1626. if (q->is_output) {
  1627. if (!(vma->vm_flags & VM_WRITE)) {
  1628. dprintk(1, "invalid vma flags, VM_WRITE needed\n");
  1629. return -EINVAL;
  1630. }
  1631. } else {
  1632. if (!(vma->vm_flags & VM_READ)) {
  1633. dprintk(1, "invalid vma flags, VM_READ needed\n");
  1634. return -EINVAL;
  1635. }
  1636. }
  1637. if (vb2_fileio_is_active(q)) {
  1638. dprintk(1, "mmap: file io in progress\n");
  1639. return -EBUSY;
  1640. }
  1641. /*
  1642. * Find the plane corresponding to the offset passed by userspace.
  1643. */
  1644. ret = __find_plane_by_offset(q, off, &buffer, &plane);
  1645. if (ret)
  1646. return ret;
  1647. vb = q->bufs[buffer];
  1648. /*
  1649. * MMAP requires page_aligned buffers.
  1650. * The buffer length was page_aligned at __vb2_buf_mem_alloc(),
  1651. * so, we need to do the same here.
  1652. */
  1653. length = PAGE_ALIGN(vb->planes[plane].length);
  1654. if (length < (vma->vm_end - vma->vm_start)) {
  1655. dprintk(1,
  1656. "MMAP invalid, as it would overflow buffer length\n");
  1657. return -EINVAL;
  1658. }
  1659. mutex_lock(&q->mmap_lock);
  1660. ret = call_memop(vb, mmap, vb->planes[plane].mem_priv, vma);
  1661. mutex_unlock(&q->mmap_lock);
  1662. if (ret)
  1663. return ret;
  1664. dprintk(3, "buffer %d, plane %d successfully mapped\n", buffer, plane);
  1665. return 0;
  1666. }
  1667. EXPORT_SYMBOL_GPL(vb2_mmap);
  1668. #ifndef CONFIG_MMU
  1669. unsigned long vb2_get_unmapped_area(struct vb2_queue *q,
  1670. unsigned long addr,
  1671. unsigned long len,
  1672. unsigned long pgoff,
  1673. unsigned long flags)
  1674. {
  1675. unsigned long off = pgoff << PAGE_SHIFT;
  1676. struct vb2_buffer *vb;
  1677. unsigned int buffer, plane;
  1678. void *vaddr;
  1679. int ret;
  1680. if (q->memory != VB2_MEMORY_MMAP) {
  1681. dprintk(1, "queue is not currently set up for mmap\n");
  1682. return -EINVAL;
  1683. }
  1684. /*
  1685. * Find the plane corresponding to the offset passed by userspace.
  1686. */
  1687. ret = __find_plane_by_offset(q, off, &buffer, &plane);
  1688. if (ret)
  1689. return ret;
  1690. vb = q->bufs[buffer];
  1691. vaddr = vb2_plane_vaddr(vb, plane);
  1692. return vaddr ? (unsigned long)vaddr : -EINVAL;
  1693. }
  1694. EXPORT_SYMBOL_GPL(vb2_get_unmapped_area);
  1695. #endif
  1696. int vb2_core_queue_init(struct vb2_queue *q)
  1697. {
  1698. /*
  1699. * Sanity check
  1700. */
  1701. if (WARN_ON(!q) ||
  1702. WARN_ON(!q->ops) ||
  1703. WARN_ON(!q->mem_ops) ||
  1704. WARN_ON(!q->type) ||
  1705. WARN_ON(!q->io_modes) ||
  1706. WARN_ON(!q->ops->queue_setup) ||
  1707. WARN_ON(!q->ops->buf_queue))
  1708. return -EINVAL;
  1709. INIT_LIST_HEAD(&q->queued_list);
  1710. INIT_LIST_HEAD(&q->done_list);
  1711. spin_lock_init(&q->done_lock);
  1712. mutex_init(&q->mmap_lock);
  1713. init_waitqueue_head(&q->done_wq);
  1714. if (q->buf_struct_size == 0)
  1715. q->buf_struct_size = sizeof(struct vb2_buffer);
  1716. return 0;
  1717. }
  1718. EXPORT_SYMBOL_GPL(vb2_core_queue_init);
  1719. static int __vb2_init_fileio(struct vb2_queue *q, int read);
  1720. static int __vb2_cleanup_fileio(struct vb2_queue *q);
  1721. void vb2_core_queue_release(struct vb2_queue *q)
  1722. {
  1723. __vb2_cleanup_fileio(q);
  1724. __vb2_queue_cancel(q);
  1725. mutex_lock(&q->mmap_lock);
  1726. __vb2_queue_free(q, q->num_buffers);
  1727. mutex_unlock(&q->mmap_lock);
  1728. }
  1729. EXPORT_SYMBOL_GPL(vb2_core_queue_release);
  1730. unsigned int vb2_core_poll(struct vb2_queue *q, struct file *file,
  1731. poll_table *wait)
  1732. {
  1733. unsigned long req_events = poll_requested_events(wait);
  1734. struct vb2_buffer *vb = NULL;
  1735. unsigned long flags;
  1736. if (!q->is_output && !(req_events & (POLLIN | POLLRDNORM)))
  1737. return 0;
  1738. if (q->is_output && !(req_events & (POLLOUT | POLLWRNORM)))
  1739. return 0;
  1740. /*
  1741. * Start file I/O emulator only if streaming API has not been used yet.
  1742. */
  1743. if (q->num_buffers == 0 && !vb2_fileio_is_active(q)) {
  1744. if (!q->is_output && (q->io_modes & VB2_READ) &&
  1745. (req_events & (POLLIN | POLLRDNORM))) {
  1746. if (__vb2_init_fileio(q, 1))
  1747. return POLLERR;
  1748. }
  1749. if (q->is_output && (q->io_modes & VB2_WRITE) &&
  1750. (req_events & (POLLOUT | POLLWRNORM))) {
  1751. if (__vb2_init_fileio(q, 0))
  1752. return POLLERR;
  1753. /*
  1754. * Write to OUTPUT queue can be done immediately.
  1755. */
  1756. return POLLOUT | POLLWRNORM;
  1757. }
  1758. }
  1759. /*
  1760. * There is nothing to wait for if the queue isn't streaming, or if the
  1761. * error flag is set.
  1762. */
  1763. if (!vb2_is_streaming(q) || q->error)
  1764. return POLLERR;
  1765. /*
  1766. * If this quirk is set and QBUF hasn't been called yet then
  1767. * return POLLERR as well. This only affects capture queues, output
  1768. * queues will always initialize waiting_for_buffers to false.
  1769. * This quirk is set by V4L2 for backwards compatibility reasons.
  1770. */
  1771. if (q->quirk_poll_must_check_waiting_for_buffers &&
  1772. q->waiting_for_buffers && (req_events & (POLLIN | POLLRDNORM)))
  1773. return POLLERR;
  1774. /*
  1775. * For output streams you can call write() as long as there are fewer
  1776. * buffers queued than there are buffers available.
  1777. */
  1778. if (q->is_output && q->fileio && q->queued_count < q->num_buffers)
  1779. return POLLOUT | POLLWRNORM;
  1780. if (list_empty(&q->done_list)) {
  1781. /*
  1782. * If the last buffer was dequeued from a capture queue,
  1783. * return immediately. DQBUF will return -EPIPE.
  1784. */
  1785. if (q->last_buffer_dequeued)
  1786. return POLLIN | POLLRDNORM;
  1787. poll_wait(file, &q->done_wq, wait);
  1788. }
  1789. /*
  1790. * Take first buffer available for dequeuing.
  1791. */
  1792. spin_lock_irqsave(&q->done_lock, flags);
  1793. if (!list_empty(&q->done_list))
  1794. vb = list_first_entry(&q->done_list, struct vb2_buffer,
  1795. done_entry);
  1796. spin_unlock_irqrestore(&q->done_lock, flags);
  1797. if (vb && (vb->state == VB2_BUF_STATE_DONE
  1798. || vb->state == VB2_BUF_STATE_ERROR)) {
  1799. return (q->is_output) ?
  1800. POLLOUT | POLLWRNORM :
  1801. POLLIN | POLLRDNORM;
  1802. }
  1803. return 0;
  1804. }
  1805. EXPORT_SYMBOL_GPL(vb2_core_poll);
  1806. /**
  1807. * struct vb2_fileio_buf - buffer context used by file io emulator
  1808. *
  1809. * vb2 provides a compatibility layer and emulator of file io (read and
  1810. * write) calls on top of streaming API. This structure is used for
  1811. * tracking context related to the buffers.
  1812. */
  1813. struct vb2_fileio_buf {
  1814. void *vaddr;
  1815. unsigned int size;
  1816. unsigned int pos;
  1817. unsigned int queued:1;
  1818. };
  1819. /**
  1820. * struct vb2_fileio_data - queue context used by file io emulator
  1821. *
  1822. * @cur_index: the index of the buffer currently being read from or
  1823. * written to. If equal to q->num_buffers then a new buffer
  1824. * must be dequeued.
  1825. * @initial_index: in the read() case all buffers are queued up immediately
  1826. * in __vb2_init_fileio() and __vb2_perform_fileio() just cycles
  1827. * buffers. However, in the write() case no buffers are initially
  1828. * queued, instead whenever a buffer is full it is queued up by
  1829. * __vb2_perform_fileio(). Only once all available buffers have
  1830. * been queued up will __vb2_perform_fileio() start to dequeue
  1831. * buffers. This means that initially __vb2_perform_fileio()
  1832. * needs to know what buffer index to use when it is queuing up
  1833. * the buffers for the first time. That initial index is stored
  1834. * in this field. Once it is equal to q->num_buffers all
  1835. * available buffers have been queued and __vb2_perform_fileio()
  1836. * should start the normal dequeue/queue cycle.
  1837. *
  1838. * vb2 provides a compatibility layer and emulator of file io (read and
  1839. * write) calls on top of streaming API. For proper operation it required
  1840. * this structure to save the driver state between each call of the read
  1841. * or write function.
  1842. */
  1843. struct vb2_fileio_data {
  1844. unsigned int count;
  1845. unsigned int type;
  1846. unsigned int memory;
  1847. struct vb2_fileio_buf bufs[VB2_MAX_FRAME];
  1848. unsigned int cur_index;
  1849. unsigned int initial_index;
  1850. unsigned int q_count;
  1851. unsigned int dq_count;
  1852. unsigned read_once:1;
  1853. unsigned write_immediately:1;
  1854. };
  1855. /**
  1856. * __vb2_init_fileio() - initialize file io emulator
  1857. * @q: videobuf2 queue
  1858. * @read: mode selector (1 means read, 0 means write)
  1859. */
  1860. static int __vb2_init_fileio(struct vb2_queue *q, int read)
  1861. {
  1862. struct vb2_fileio_data *fileio;
  1863. int i, ret;
  1864. unsigned int count = 0;
  1865. /*
  1866. * Sanity check
  1867. */
  1868. if (WARN_ON((read && !(q->io_modes & VB2_READ)) ||
  1869. (!read && !(q->io_modes & VB2_WRITE))))
  1870. return -EINVAL;
  1871. /*
  1872. * Check if device supports mapping buffers to kernel virtual space.
  1873. */
  1874. if (!q->mem_ops->vaddr)
  1875. return -EBUSY;
  1876. /*
  1877. * Check if streaming api has not been already activated.
  1878. */
  1879. if (q->streaming || q->num_buffers > 0)
  1880. return -EBUSY;
  1881. /*
  1882. * Start with count 1, driver can increase it in queue_setup()
  1883. */
  1884. count = 1;
  1885. dprintk(3, "setting up file io: mode %s, count %d, read_once %d, write_immediately %d\n",
  1886. (read) ? "read" : "write", count, q->fileio_read_once,
  1887. q->fileio_write_immediately);
  1888. fileio = kzalloc(sizeof(*fileio), GFP_KERNEL);
  1889. if (fileio == NULL)
  1890. return -ENOMEM;
  1891. fileio->read_once = q->fileio_read_once;
  1892. fileio->write_immediately = q->fileio_write_immediately;
  1893. /*
  1894. * Request buffers and use MMAP type to force driver
  1895. * to allocate buffers by itself.
  1896. */
  1897. fileio->count = count;
  1898. fileio->memory = VB2_MEMORY_MMAP;
  1899. fileio->type = q->type;
  1900. q->fileio = fileio;
  1901. ret = vb2_core_reqbufs(q, fileio->memory, &fileio->count);
  1902. if (ret)
  1903. goto err_kfree;
  1904. /*
  1905. * Check if plane_count is correct
  1906. * (multiplane buffers are not supported).
  1907. */
  1908. if (q->bufs[0]->num_planes != 1) {
  1909. ret = -EBUSY;
  1910. goto err_reqbufs;
  1911. }
  1912. /*
  1913. * Get kernel address of each buffer.
  1914. */
  1915. for (i = 0; i < q->num_buffers; i++) {
  1916. fileio->bufs[i].vaddr = vb2_plane_vaddr(q->bufs[i], 0);
  1917. if (fileio->bufs[i].vaddr == NULL) {
  1918. ret = -EINVAL;
  1919. goto err_reqbufs;
  1920. }
  1921. fileio->bufs[i].size = vb2_plane_size(q->bufs[i], 0);
  1922. }
  1923. /*
  1924. * Read mode requires pre queuing of all buffers.
  1925. */
  1926. if (read) {
  1927. /*
  1928. * Queue all buffers.
  1929. */
  1930. for (i = 0; i < q->num_buffers; i++) {
  1931. ret = vb2_core_qbuf(q, i, NULL);
  1932. if (ret)
  1933. goto err_reqbufs;
  1934. fileio->bufs[i].queued = 1;
  1935. }
  1936. /*
  1937. * All buffers have been queued, so mark that by setting
  1938. * initial_index to q->num_buffers
  1939. */
  1940. fileio->initial_index = q->num_buffers;
  1941. fileio->cur_index = q->num_buffers;
  1942. }
  1943. /*
  1944. * Start streaming.
  1945. */
  1946. ret = vb2_core_streamon(q, q->type);
  1947. if (ret)
  1948. goto err_reqbufs;
  1949. return ret;
  1950. err_reqbufs:
  1951. fileio->count = 0;
  1952. vb2_core_reqbufs(q, fileio->memory, &fileio->count);
  1953. err_kfree:
  1954. q->fileio = NULL;
  1955. kfree(fileio);
  1956. return ret;
  1957. }
  1958. /**
  1959. * __vb2_cleanup_fileio() - free resourced used by file io emulator
  1960. * @q: videobuf2 queue
  1961. */
  1962. static int __vb2_cleanup_fileio(struct vb2_queue *q)
  1963. {
  1964. struct vb2_fileio_data *fileio = q->fileio;
  1965. if (fileio) {
  1966. vb2_core_streamoff(q, q->type);
  1967. q->fileio = NULL;
  1968. fileio->count = 0;
  1969. vb2_core_reqbufs(q, fileio->memory, &fileio->count);
  1970. kfree(fileio);
  1971. dprintk(3, "file io emulator closed\n");
  1972. }
  1973. return 0;
  1974. }
  1975. /**
  1976. * __vb2_perform_fileio() - perform a single file io (read or write) operation
  1977. * @q: videobuf2 queue
  1978. * @data: pointed to target userspace buffer
  1979. * @count: number of bytes to read or write
  1980. * @ppos: file handle position tracking pointer
  1981. * @nonblock: mode selector (1 means blocking calls, 0 means nonblocking)
  1982. * @read: access mode selector (1 means read, 0 means write)
  1983. */
  1984. static size_t __vb2_perform_fileio(struct vb2_queue *q, char __user *data, size_t count,
  1985. loff_t *ppos, int nonblock, int read)
  1986. {
  1987. struct vb2_fileio_data *fileio;
  1988. struct vb2_fileio_buf *buf;
  1989. bool is_multiplanar = q->is_multiplanar;
  1990. /*
  1991. * When using write() to write data to an output video node the vb2 core
  1992. * should copy timestamps if V4L2_BUF_FLAG_TIMESTAMP_COPY is set. Nobody
  1993. * else is able to provide this information with the write() operation.
  1994. */
  1995. bool copy_timestamp = !read && q->copy_timestamp;
  1996. unsigned index;
  1997. int ret;
  1998. dprintk(3, "mode %s, offset %ld, count %zd, %sblocking\n",
  1999. read ? "read" : "write", (long)*ppos, count,
  2000. nonblock ? "non" : "");
  2001. if (!data)
  2002. return -EINVAL;
  2003. /*
  2004. * Initialize emulator on first call.
  2005. */
  2006. if (!vb2_fileio_is_active(q)) {
  2007. ret = __vb2_init_fileio(q, read);
  2008. dprintk(3, "vb2_init_fileio result: %d\n", ret);
  2009. if (ret)
  2010. return ret;
  2011. }
  2012. fileio = q->fileio;
  2013. /*
  2014. * Check if we need to dequeue the buffer.
  2015. */
  2016. index = fileio->cur_index;
  2017. if (index >= q->num_buffers) {
  2018. struct vb2_buffer *b;
  2019. /*
  2020. * Call vb2_dqbuf to get buffer back.
  2021. */
  2022. ret = vb2_core_dqbuf(q, &index, NULL, nonblock);
  2023. dprintk(5, "vb2_dqbuf result: %d\n", ret);
  2024. if (ret)
  2025. return ret;
  2026. fileio->dq_count += 1;
  2027. fileio->cur_index = index;
  2028. buf = &fileio->bufs[index];
  2029. b = q->bufs[index];
  2030. /*
  2031. * Get number of bytes filled by the driver
  2032. */
  2033. buf->pos = 0;
  2034. buf->queued = 0;
  2035. buf->size = read ? vb2_get_plane_payload(q->bufs[index], 0)
  2036. : vb2_plane_size(q->bufs[index], 0);
  2037. /* Compensate for data_offset on read in the multiplanar case. */
  2038. if (is_multiplanar && read &&
  2039. b->planes[0].data_offset < buf->size) {
  2040. buf->pos = b->planes[0].data_offset;
  2041. buf->size -= buf->pos;
  2042. }
  2043. } else {
  2044. buf = &fileio->bufs[index];
  2045. }
  2046. /*
  2047. * Limit count on last few bytes of the buffer.
  2048. */
  2049. if (buf->pos + count > buf->size) {
  2050. count = buf->size - buf->pos;
  2051. dprintk(5, "reducing read count: %zd\n", count);
  2052. }
  2053. /*
  2054. * Transfer data to userspace.
  2055. */
  2056. dprintk(3, "copying %zd bytes - buffer %d, offset %u\n",
  2057. count, index, buf->pos);
  2058. if (read)
  2059. ret = copy_to_user(data, buf->vaddr + buf->pos, count);
  2060. else
  2061. ret = copy_from_user(buf->vaddr + buf->pos, data, count);
  2062. if (ret) {
  2063. dprintk(3, "error copying data\n");
  2064. return -EFAULT;
  2065. }
  2066. /*
  2067. * Update counters.
  2068. */
  2069. buf->pos += count;
  2070. *ppos += count;
  2071. /*
  2072. * Queue next buffer if required.
  2073. */
  2074. if (buf->pos == buf->size || (!read && fileio->write_immediately)) {
  2075. struct vb2_buffer *b = q->bufs[index];
  2076. /*
  2077. * Check if this is the last buffer to read.
  2078. */
  2079. if (read && fileio->read_once && fileio->dq_count == 1) {
  2080. dprintk(3, "read limit reached\n");
  2081. return __vb2_cleanup_fileio(q);
  2082. }
  2083. /*
  2084. * Call vb2_qbuf and give buffer to the driver.
  2085. */
  2086. b->planes[0].bytesused = buf->pos;
  2087. if (copy_timestamp)
  2088. b->timestamp = ktime_get_ns();
  2089. ret = vb2_core_qbuf(q, index, NULL);
  2090. dprintk(5, "vb2_dbuf result: %d\n", ret);
  2091. if (ret)
  2092. return ret;
  2093. /*
  2094. * Buffer has been queued, update the status
  2095. */
  2096. buf->pos = 0;
  2097. buf->queued = 1;
  2098. buf->size = vb2_plane_size(q->bufs[index], 0);
  2099. fileio->q_count += 1;
  2100. /*
  2101. * If we are queuing up buffers for the first time, then
  2102. * increase initial_index by one.
  2103. */
  2104. if (fileio->initial_index < q->num_buffers)
  2105. fileio->initial_index++;
  2106. /*
  2107. * The next buffer to use is either a buffer that's going to be
  2108. * queued for the first time (initial_index < q->num_buffers)
  2109. * or it is equal to q->num_buffers, meaning that the next
  2110. * time we need to dequeue a buffer since we've now queued up
  2111. * all the 'first time' buffers.
  2112. */
  2113. fileio->cur_index = fileio->initial_index;
  2114. }
  2115. /*
  2116. * Return proper number of bytes processed.
  2117. */
  2118. if (ret == 0)
  2119. ret = count;
  2120. return ret;
  2121. }
  2122. size_t vb2_read(struct vb2_queue *q, char __user *data, size_t count,
  2123. loff_t *ppos, int nonblocking)
  2124. {
  2125. return __vb2_perform_fileio(q, data, count, ppos, nonblocking, 1);
  2126. }
  2127. EXPORT_SYMBOL_GPL(vb2_read);
  2128. size_t vb2_write(struct vb2_queue *q, const char __user *data, size_t count,
  2129. loff_t *ppos, int nonblocking)
  2130. {
  2131. return __vb2_perform_fileio(q, (char __user *) data, count,
  2132. ppos, nonblocking, 0);
  2133. }
  2134. EXPORT_SYMBOL_GPL(vb2_write);
  2135. struct vb2_threadio_data {
  2136. struct task_struct *thread;
  2137. vb2_thread_fnc fnc;
  2138. void *priv;
  2139. bool stop;
  2140. };
  2141. static int vb2_thread(void *data)
  2142. {
  2143. struct vb2_queue *q = data;
  2144. struct vb2_threadio_data *threadio = q->threadio;
  2145. bool copy_timestamp = false;
  2146. unsigned prequeue = 0;
  2147. unsigned index = 0;
  2148. int ret = 0;
  2149. if (q->is_output) {
  2150. prequeue = q->num_buffers;
  2151. copy_timestamp = q->copy_timestamp;
  2152. }
  2153. set_freezable();
  2154. for (;;) {
  2155. struct vb2_buffer *vb;
  2156. /*
  2157. * Call vb2_dqbuf to get buffer back.
  2158. */
  2159. if (prequeue) {
  2160. vb = q->bufs[index++];
  2161. prequeue--;
  2162. } else {
  2163. call_void_qop(q, wait_finish, q);
  2164. if (!threadio->stop)
  2165. ret = vb2_core_dqbuf(q, &index, NULL, 0);
  2166. call_void_qop(q, wait_prepare, q);
  2167. dprintk(5, "file io: vb2_dqbuf result: %d\n", ret);
  2168. if (!ret)
  2169. vb = q->bufs[index];
  2170. }
  2171. if (ret || threadio->stop)
  2172. break;
  2173. try_to_freeze();
  2174. if (vb->state != VB2_BUF_STATE_ERROR)
  2175. if (threadio->fnc(vb, threadio->priv))
  2176. break;
  2177. call_void_qop(q, wait_finish, q);
  2178. if (copy_timestamp)
  2179. vb->timestamp = ktime_get_ns();;
  2180. if (!threadio->stop)
  2181. ret = vb2_core_qbuf(q, vb->index, NULL);
  2182. call_void_qop(q, wait_prepare, q);
  2183. if (ret || threadio->stop)
  2184. break;
  2185. }
  2186. /* Hmm, linux becomes *very* unhappy without this ... */
  2187. while (!kthread_should_stop()) {
  2188. set_current_state(TASK_INTERRUPTIBLE);
  2189. schedule();
  2190. }
  2191. return 0;
  2192. }
  2193. /*
  2194. * This function should not be used for anything else but the videobuf2-dvb
  2195. * support. If you think you have another good use-case for this, then please
  2196. * contact the linux-media mailinglist first.
  2197. */
  2198. int vb2_thread_start(struct vb2_queue *q, vb2_thread_fnc fnc, void *priv,
  2199. const char *thread_name)
  2200. {
  2201. struct vb2_threadio_data *threadio;
  2202. int ret = 0;
  2203. if (q->threadio)
  2204. return -EBUSY;
  2205. if (vb2_is_busy(q))
  2206. return -EBUSY;
  2207. if (WARN_ON(q->fileio))
  2208. return -EBUSY;
  2209. threadio = kzalloc(sizeof(*threadio), GFP_KERNEL);
  2210. if (threadio == NULL)
  2211. return -ENOMEM;
  2212. threadio->fnc = fnc;
  2213. threadio->priv = priv;
  2214. ret = __vb2_init_fileio(q, !q->is_output);
  2215. dprintk(3, "file io: vb2_init_fileio result: %d\n", ret);
  2216. if (ret)
  2217. goto nomem;
  2218. q->threadio = threadio;
  2219. threadio->thread = kthread_run(vb2_thread, q, "vb2-%s", thread_name);
  2220. if (IS_ERR(threadio->thread)) {
  2221. ret = PTR_ERR(threadio->thread);
  2222. threadio->thread = NULL;
  2223. goto nothread;
  2224. }
  2225. return 0;
  2226. nothread:
  2227. __vb2_cleanup_fileio(q);
  2228. nomem:
  2229. kfree(threadio);
  2230. return ret;
  2231. }
  2232. EXPORT_SYMBOL_GPL(vb2_thread_start);
  2233. int vb2_thread_stop(struct vb2_queue *q)
  2234. {
  2235. struct vb2_threadio_data *threadio = q->threadio;
  2236. int err;
  2237. if (threadio == NULL)
  2238. return 0;
  2239. threadio->stop = true;
  2240. /* Wake up all pending sleeps in the thread */
  2241. vb2_queue_error(q);
  2242. err = kthread_stop(threadio->thread);
  2243. __vb2_cleanup_fileio(q);
  2244. threadio->thread = NULL;
  2245. kfree(threadio);
  2246. q->threadio = NULL;
  2247. return err;
  2248. }
  2249. EXPORT_SYMBOL_GPL(vb2_thread_stop);
  2250. MODULE_DESCRIPTION("Media buffer core framework");
  2251. MODULE_AUTHOR("Pawel Osciak <pawel@osciak.com>, Marek Szyprowski");
  2252. MODULE_LICENSE("GPL");