videobuf2-core.c 72 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764
  1. /*
  2. * videobuf2-core.c - V4L2 driver helper 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. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation.
  12. */
  13. #include <linux/err.h>
  14. #include <linux/kernel.h>
  15. #include <linux/module.h>
  16. #include <linux/mm.h>
  17. #include <linux/poll.h>
  18. #include <linux/slab.h>
  19. #include <linux/sched.h>
  20. #include <media/v4l2-dev.h>
  21. #include <media/v4l2-fh.h>
  22. #include <media/v4l2-event.h>
  23. #include <media/videobuf2-core.h>
  24. static int debug;
  25. module_param(debug, int, 0644);
  26. #define dprintk(level, fmt, arg...) \
  27. do { \
  28. if (debug >= level) \
  29. printk(KERN_DEBUG "vb2: " fmt, ## arg); \
  30. } while (0)
  31. #define call_memop(q, op, args...) \
  32. (((q)->mem_ops->op) ? \
  33. ((q)->mem_ops->op(args)) : 0)
  34. #define call_qop(q, op, args...) \
  35. (((q)->ops->op) ? ((q)->ops->op(args)) : 0)
  36. #define V4L2_BUFFER_MASK_FLAGS (V4L2_BUF_FLAG_MAPPED | V4L2_BUF_FLAG_QUEUED | \
  37. V4L2_BUF_FLAG_DONE | V4L2_BUF_FLAG_ERROR | \
  38. V4L2_BUF_FLAG_PREPARED | \
  39. V4L2_BUF_FLAG_TIMESTAMP_MASK)
  40. /**
  41. * __vb2_buf_mem_alloc() - allocate video memory for the given buffer
  42. */
  43. static int __vb2_buf_mem_alloc(struct vb2_buffer *vb)
  44. {
  45. struct vb2_queue *q = vb->vb2_queue;
  46. void *mem_priv;
  47. int plane;
  48. /*
  49. * Allocate memory for all planes in this buffer
  50. * NOTE: mmapped areas should be page aligned
  51. */
  52. for (plane = 0; plane < vb->num_planes; ++plane) {
  53. unsigned long size = PAGE_ALIGN(q->plane_sizes[plane]);
  54. mem_priv = call_memop(q, alloc, q->alloc_ctx[plane],
  55. size, q->gfp_flags);
  56. if (IS_ERR_OR_NULL(mem_priv))
  57. goto free;
  58. /* Associate allocator private data with this plane */
  59. vb->planes[plane].mem_priv = mem_priv;
  60. vb->v4l2_planes[plane].length = q->plane_sizes[plane];
  61. }
  62. return 0;
  63. free:
  64. /* Free already allocated memory if one of the allocations failed */
  65. for (; plane > 0; --plane) {
  66. call_memop(q, put, vb->planes[plane - 1].mem_priv);
  67. vb->planes[plane - 1].mem_priv = NULL;
  68. }
  69. return -ENOMEM;
  70. }
  71. /**
  72. * __vb2_buf_mem_free() - free memory of the given buffer
  73. */
  74. static void __vb2_buf_mem_free(struct vb2_buffer *vb)
  75. {
  76. struct vb2_queue *q = vb->vb2_queue;
  77. unsigned int plane;
  78. for (plane = 0; plane < vb->num_planes; ++plane) {
  79. call_memop(q, put, vb->planes[plane].mem_priv);
  80. vb->planes[plane].mem_priv = NULL;
  81. dprintk(3, "Freed plane %d of buffer %d\n", plane,
  82. vb->v4l2_buf.index);
  83. }
  84. }
  85. /**
  86. * __vb2_buf_userptr_put() - release userspace memory associated with
  87. * a USERPTR buffer
  88. */
  89. static void __vb2_buf_userptr_put(struct vb2_buffer *vb)
  90. {
  91. struct vb2_queue *q = vb->vb2_queue;
  92. unsigned int plane;
  93. for (plane = 0; plane < vb->num_planes; ++plane) {
  94. if (vb->planes[plane].mem_priv)
  95. call_memop(q, put_userptr, vb->planes[plane].mem_priv);
  96. vb->planes[plane].mem_priv = NULL;
  97. }
  98. }
  99. /**
  100. * __vb2_plane_dmabuf_put() - release memory associated with
  101. * a DMABUF shared plane
  102. */
  103. static void __vb2_plane_dmabuf_put(struct vb2_queue *q, struct vb2_plane *p)
  104. {
  105. if (!p->mem_priv)
  106. return;
  107. if (p->dbuf_mapped)
  108. call_memop(q, unmap_dmabuf, p->mem_priv);
  109. call_memop(q, detach_dmabuf, p->mem_priv);
  110. dma_buf_put(p->dbuf);
  111. memset(p, 0, sizeof(*p));
  112. }
  113. /**
  114. * __vb2_buf_dmabuf_put() - release memory associated with
  115. * a DMABUF shared buffer
  116. */
  117. static void __vb2_buf_dmabuf_put(struct vb2_buffer *vb)
  118. {
  119. struct vb2_queue *q = vb->vb2_queue;
  120. unsigned int plane;
  121. for (plane = 0; plane < vb->num_planes; ++plane)
  122. __vb2_plane_dmabuf_put(q, &vb->planes[plane]);
  123. }
  124. /**
  125. * __setup_offsets() - setup unique offsets ("cookies") for every plane in
  126. * every buffer on the queue
  127. */
  128. static void __setup_offsets(struct vb2_queue *q, unsigned int n)
  129. {
  130. unsigned int buffer, plane;
  131. struct vb2_buffer *vb;
  132. unsigned long off;
  133. if (q->num_buffers) {
  134. struct v4l2_plane *p;
  135. vb = q->bufs[q->num_buffers - 1];
  136. p = &vb->v4l2_planes[vb->num_planes - 1];
  137. off = PAGE_ALIGN(p->m.mem_offset + p->length);
  138. } else {
  139. off = 0;
  140. }
  141. for (buffer = q->num_buffers; buffer < q->num_buffers + n; ++buffer) {
  142. vb = q->bufs[buffer];
  143. if (!vb)
  144. continue;
  145. for (plane = 0; plane < vb->num_planes; ++plane) {
  146. vb->v4l2_planes[plane].length = q->plane_sizes[plane];
  147. vb->v4l2_planes[plane].m.mem_offset = off;
  148. dprintk(3, "Buffer %d, plane %d offset 0x%08lx\n",
  149. buffer, plane, off);
  150. off += vb->v4l2_planes[plane].length;
  151. off = PAGE_ALIGN(off);
  152. }
  153. }
  154. }
  155. /**
  156. * __vb2_queue_alloc() - allocate videobuf buffer structures and (for MMAP type)
  157. * video buffer memory for all buffers/planes on the queue and initializes the
  158. * queue
  159. *
  160. * Returns the number of buffers successfully allocated.
  161. */
  162. static int __vb2_queue_alloc(struct vb2_queue *q, enum v4l2_memory memory,
  163. unsigned int num_buffers, unsigned int num_planes)
  164. {
  165. unsigned int buffer;
  166. struct vb2_buffer *vb;
  167. int ret;
  168. for (buffer = 0; buffer < num_buffers; ++buffer) {
  169. /* Allocate videobuf buffer structures */
  170. vb = kzalloc(q->buf_struct_size, GFP_KERNEL);
  171. if (!vb) {
  172. dprintk(1, "Memory alloc for buffer struct failed\n");
  173. break;
  174. }
  175. /* Length stores number of planes for multiplanar buffers */
  176. if (V4L2_TYPE_IS_MULTIPLANAR(q->type))
  177. vb->v4l2_buf.length = num_planes;
  178. vb->state = VB2_BUF_STATE_DEQUEUED;
  179. vb->vb2_queue = q;
  180. vb->num_planes = num_planes;
  181. vb->v4l2_buf.index = q->num_buffers + buffer;
  182. vb->v4l2_buf.type = q->type;
  183. vb->v4l2_buf.memory = memory;
  184. /* Allocate video buffer memory for the MMAP type */
  185. if (memory == V4L2_MEMORY_MMAP) {
  186. ret = __vb2_buf_mem_alloc(vb);
  187. if (ret) {
  188. dprintk(1, "Failed allocating memory for "
  189. "buffer %d\n", buffer);
  190. kfree(vb);
  191. break;
  192. }
  193. /*
  194. * Call the driver-provided buffer initialization
  195. * callback, if given. An error in initialization
  196. * results in queue setup failure.
  197. */
  198. ret = call_qop(q, buf_init, vb);
  199. if (ret) {
  200. dprintk(1, "Buffer %d %p initialization"
  201. " failed\n", buffer, vb);
  202. __vb2_buf_mem_free(vb);
  203. kfree(vb);
  204. break;
  205. }
  206. }
  207. q->bufs[q->num_buffers + buffer] = vb;
  208. }
  209. if (memory == V4L2_MEMORY_MMAP)
  210. __setup_offsets(q, buffer);
  211. dprintk(1, "Allocated %d buffers, %d plane(s) each\n",
  212. buffer, num_planes);
  213. return buffer;
  214. }
  215. /**
  216. * __vb2_free_mem() - release all video buffer memory for a given queue
  217. */
  218. static void __vb2_free_mem(struct vb2_queue *q, unsigned int buffers)
  219. {
  220. unsigned int buffer;
  221. struct vb2_buffer *vb;
  222. for (buffer = q->num_buffers - buffers; buffer < q->num_buffers;
  223. ++buffer) {
  224. vb = q->bufs[buffer];
  225. if (!vb)
  226. continue;
  227. /* Free MMAP buffers or release USERPTR buffers */
  228. if (q->memory == V4L2_MEMORY_MMAP)
  229. __vb2_buf_mem_free(vb);
  230. else if (q->memory == V4L2_MEMORY_DMABUF)
  231. __vb2_buf_dmabuf_put(vb);
  232. else
  233. __vb2_buf_userptr_put(vb);
  234. }
  235. }
  236. /**
  237. * __vb2_queue_free() - free buffers at the end of the queue - video memory and
  238. * related information, if no buffers are left return the queue to an
  239. * uninitialized state. Might be called even if the queue has already been freed.
  240. */
  241. static void __vb2_queue_free(struct vb2_queue *q, unsigned int buffers)
  242. {
  243. unsigned int buffer;
  244. /* Call driver-provided cleanup function for each buffer, if provided */
  245. if (q->ops->buf_cleanup) {
  246. for (buffer = q->num_buffers - buffers; buffer < q->num_buffers;
  247. ++buffer) {
  248. if (NULL == q->bufs[buffer])
  249. continue;
  250. q->ops->buf_cleanup(q->bufs[buffer]);
  251. }
  252. }
  253. /* Release video buffer memory */
  254. __vb2_free_mem(q, buffers);
  255. /* Free videobuf buffers */
  256. for (buffer = q->num_buffers - buffers; buffer < q->num_buffers;
  257. ++buffer) {
  258. kfree(q->bufs[buffer]);
  259. q->bufs[buffer] = NULL;
  260. }
  261. q->num_buffers -= buffers;
  262. if (!q->num_buffers)
  263. q->memory = 0;
  264. INIT_LIST_HEAD(&q->queued_list);
  265. }
  266. /**
  267. * __verify_planes_array() - verify that the planes array passed in struct
  268. * v4l2_buffer from userspace can be safely used
  269. */
  270. static int __verify_planes_array(struct vb2_buffer *vb, const struct v4l2_buffer *b)
  271. {
  272. if (!V4L2_TYPE_IS_MULTIPLANAR(b->type))
  273. return 0;
  274. /* Is memory for copying plane information present? */
  275. if (NULL == b->m.planes) {
  276. dprintk(1, "Multi-planar buffer passed but "
  277. "planes array not provided\n");
  278. return -EINVAL;
  279. }
  280. if (b->length < vb->num_planes || b->length > VIDEO_MAX_PLANES) {
  281. dprintk(1, "Incorrect planes array length, "
  282. "expected %d, got %d\n", vb->num_planes, b->length);
  283. return -EINVAL;
  284. }
  285. return 0;
  286. }
  287. /**
  288. * __verify_length() - Verify that the bytesused value for each plane fits in
  289. * the plane length and that the data offset doesn't exceed the bytesused value.
  290. */
  291. static int __verify_length(struct vb2_buffer *vb, const struct v4l2_buffer *b)
  292. {
  293. unsigned int length;
  294. unsigned int plane;
  295. if (!V4L2_TYPE_IS_OUTPUT(b->type))
  296. return 0;
  297. if (V4L2_TYPE_IS_MULTIPLANAR(b->type)) {
  298. for (plane = 0; plane < vb->num_planes; ++plane) {
  299. length = (b->memory == V4L2_MEMORY_USERPTR)
  300. ? b->m.planes[plane].length
  301. : vb->v4l2_planes[plane].length;
  302. if (b->m.planes[plane].bytesused > length)
  303. return -EINVAL;
  304. if (b->m.planes[plane].data_offset > 0 &&
  305. b->m.planes[plane].data_offset >=
  306. b->m.planes[plane].bytesused)
  307. return -EINVAL;
  308. }
  309. } else {
  310. length = (b->memory == V4L2_MEMORY_USERPTR)
  311. ? b->length : vb->v4l2_planes[0].length;
  312. if (b->bytesused > length)
  313. return -EINVAL;
  314. }
  315. return 0;
  316. }
  317. /**
  318. * __buffer_in_use() - return true if the buffer is in use and
  319. * the queue cannot be freed (by the means of REQBUFS(0)) call
  320. */
  321. static bool __buffer_in_use(struct vb2_queue *q, struct vb2_buffer *vb)
  322. {
  323. unsigned int plane;
  324. for (plane = 0; plane < vb->num_planes; ++plane) {
  325. void *mem_priv = vb->planes[plane].mem_priv;
  326. /*
  327. * If num_users() has not been provided, call_memop
  328. * will return 0, apparently nobody cares about this
  329. * case anyway. If num_users() returns more than 1,
  330. * we are not the only user of the plane's memory.
  331. */
  332. if (mem_priv && call_memop(q, num_users, mem_priv) > 1)
  333. return true;
  334. }
  335. return false;
  336. }
  337. /**
  338. * __buffers_in_use() - return true if any buffers on the queue are in use and
  339. * the queue cannot be freed (by the means of REQBUFS(0)) call
  340. */
  341. static bool __buffers_in_use(struct vb2_queue *q)
  342. {
  343. unsigned int buffer;
  344. for (buffer = 0; buffer < q->num_buffers; ++buffer) {
  345. if (__buffer_in_use(q, q->bufs[buffer]))
  346. return true;
  347. }
  348. return false;
  349. }
  350. /**
  351. * __fill_v4l2_buffer() - fill in a struct v4l2_buffer with information to be
  352. * returned to userspace
  353. */
  354. static void __fill_v4l2_buffer(struct vb2_buffer *vb, struct v4l2_buffer *b)
  355. {
  356. struct vb2_queue *q = vb->vb2_queue;
  357. /* Copy back data such as timestamp, flags, etc. */
  358. memcpy(b, &vb->v4l2_buf, offsetof(struct v4l2_buffer, m));
  359. b->reserved2 = vb->v4l2_buf.reserved2;
  360. b->reserved = vb->v4l2_buf.reserved;
  361. if (V4L2_TYPE_IS_MULTIPLANAR(q->type)) {
  362. /*
  363. * Fill in plane-related data if userspace provided an array
  364. * for it. The caller has already verified memory and size.
  365. */
  366. b->length = vb->num_planes;
  367. memcpy(b->m.planes, vb->v4l2_planes,
  368. b->length * sizeof(struct v4l2_plane));
  369. } else {
  370. /*
  371. * We use length and offset in v4l2_planes array even for
  372. * single-planar buffers, but userspace does not.
  373. */
  374. b->length = vb->v4l2_planes[0].length;
  375. b->bytesused = vb->v4l2_planes[0].bytesused;
  376. if (q->memory == V4L2_MEMORY_MMAP)
  377. b->m.offset = vb->v4l2_planes[0].m.mem_offset;
  378. else if (q->memory == V4L2_MEMORY_USERPTR)
  379. b->m.userptr = vb->v4l2_planes[0].m.userptr;
  380. else if (q->memory == V4L2_MEMORY_DMABUF)
  381. b->m.fd = vb->v4l2_planes[0].m.fd;
  382. }
  383. /*
  384. * Clear any buffer state related flags.
  385. */
  386. b->flags &= ~V4L2_BUFFER_MASK_FLAGS;
  387. b->flags |= q->timestamp_type;
  388. switch (vb->state) {
  389. case VB2_BUF_STATE_QUEUED:
  390. case VB2_BUF_STATE_ACTIVE:
  391. b->flags |= V4L2_BUF_FLAG_QUEUED;
  392. break;
  393. case VB2_BUF_STATE_ERROR:
  394. b->flags |= V4L2_BUF_FLAG_ERROR;
  395. /* fall through */
  396. case VB2_BUF_STATE_DONE:
  397. b->flags |= V4L2_BUF_FLAG_DONE;
  398. break;
  399. case VB2_BUF_STATE_PREPARED:
  400. b->flags |= V4L2_BUF_FLAG_PREPARED;
  401. break;
  402. case VB2_BUF_STATE_DEQUEUED:
  403. /* nothing */
  404. break;
  405. }
  406. if (__buffer_in_use(q, vb))
  407. b->flags |= V4L2_BUF_FLAG_MAPPED;
  408. }
  409. /**
  410. * vb2_querybuf() - query video buffer information
  411. * @q: videobuf queue
  412. * @b: buffer struct passed from userspace to vidioc_querybuf handler
  413. * in driver
  414. *
  415. * Should be called from vidioc_querybuf ioctl handler in driver.
  416. * This function will verify the passed v4l2_buffer structure and fill the
  417. * relevant information for the userspace.
  418. *
  419. * The return values from this function are intended to be directly returned
  420. * from vidioc_querybuf handler in driver.
  421. */
  422. int vb2_querybuf(struct vb2_queue *q, struct v4l2_buffer *b)
  423. {
  424. struct vb2_buffer *vb;
  425. int ret;
  426. if (b->type != q->type) {
  427. dprintk(1, "querybuf: wrong buffer type\n");
  428. return -EINVAL;
  429. }
  430. if (b->index >= q->num_buffers) {
  431. dprintk(1, "querybuf: buffer index out of range\n");
  432. return -EINVAL;
  433. }
  434. vb = q->bufs[b->index];
  435. ret = __verify_planes_array(vb, b);
  436. if (!ret)
  437. __fill_v4l2_buffer(vb, b);
  438. return ret;
  439. }
  440. EXPORT_SYMBOL(vb2_querybuf);
  441. /**
  442. * __verify_userptr_ops() - verify that all memory operations required for
  443. * USERPTR queue type have been provided
  444. */
  445. static int __verify_userptr_ops(struct vb2_queue *q)
  446. {
  447. if (!(q->io_modes & VB2_USERPTR) || !q->mem_ops->get_userptr ||
  448. !q->mem_ops->put_userptr)
  449. return -EINVAL;
  450. return 0;
  451. }
  452. /**
  453. * __verify_mmap_ops() - verify that all memory operations required for
  454. * MMAP queue type have been provided
  455. */
  456. static int __verify_mmap_ops(struct vb2_queue *q)
  457. {
  458. if (!(q->io_modes & VB2_MMAP) || !q->mem_ops->alloc ||
  459. !q->mem_ops->put || !q->mem_ops->mmap)
  460. return -EINVAL;
  461. return 0;
  462. }
  463. /**
  464. * __verify_dmabuf_ops() - verify that all memory operations required for
  465. * DMABUF queue type have been provided
  466. */
  467. static int __verify_dmabuf_ops(struct vb2_queue *q)
  468. {
  469. if (!(q->io_modes & VB2_DMABUF) || !q->mem_ops->attach_dmabuf ||
  470. !q->mem_ops->detach_dmabuf || !q->mem_ops->map_dmabuf ||
  471. !q->mem_ops->unmap_dmabuf)
  472. return -EINVAL;
  473. return 0;
  474. }
  475. /**
  476. * __verify_memory_type() - Check whether the memory type and buffer type
  477. * passed to a buffer operation are compatible with the queue.
  478. */
  479. static int __verify_memory_type(struct vb2_queue *q,
  480. enum v4l2_memory memory, enum v4l2_buf_type type)
  481. {
  482. if (memory != V4L2_MEMORY_MMAP && memory != V4L2_MEMORY_USERPTR &&
  483. memory != V4L2_MEMORY_DMABUF) {
  484. dprintk(1, "reqbufs: unsupported memory type\n");
  485. return -EINVAL;
  486. }
  487. if (type != q->type) {
  488. dprintk(1, "reqbufs: requested type is incorrect\n");
  489. return -EINVAL;
  490. }
  491. /*
  492. * Make sure all the required memory ops for given memory type
  493. * are available.
  494. */
  495. if (memory == V4L2_MEMORY_MMAP && __verify_mmap_ops(q)) {
  496. dprintk(1, "reqbufs: MMAP for current setup unsupported\n");
  497. return -EINVAL;
  498. }
  499. if (memory == V4L2_MEMORY_USERPTR && __verify_userptr_ops(q)) {
  500. dprintk(1, "reqbufs: USERPTR for current setup unsupported\n");
  501. return -EINVAL;
  502. }
  503. if (memory == V4L2_MEMORY_DMABUF && __verify_dmabuf_ops(q)) {
  504. dprintk(1, "reqbufs: DMABUF for current setup unsupported\n");
  505. return -EINVAL;
  506. }
  507. /*
  508. * Place the busy tests at the end: -EBUSY can be ignored when
  509. * create_bufs is called with count == 0, but count == 0 should still
  510. * do the memory and type validation.
  511. */
  512. if (q->fileio) {
  513. dprintk(1, "reqbufs: file io in progress\n");
  514. return -EBUSY;
  515. }
  516. return 0;
  517. }
  518. /**
  519. * __reqbufs() - Initiate streaming
  520. * @q: videobuf2 queue
  521. * @req: struct passed from userspace to vidioc_reqbufs handler in driver
  522. *
  523. * Should be called from vidioc_reqbufs ioctl handler of a driver.
  524. * This function:
  525. * 1) verifies streaming parameters passed from the userspace,
  526. * 2) sets up the queue,
  527. * 3) negotiates number of buffers and planes per buffer with the driver
  528. * to be used during streaming,
  529. * 4) allocates internal buffer structures (struct vb2_buffer), according to
  530. * the agreed parameters,
  531. * 5) for MMAP memory type, allocates actual video memory, using the
  532. * memory handling/allocation routines provided during queue initialization
  533. *
  534. * If req->count is 0, all the memory will be freed instead.
  535. * If the queue has been allocated previously (by a previous vb2_reqbufs) call
  536. * and the queue is not busy, memory will be reallocated.
  537. *
  538. * The return values from this function are intended to be directly returned
  539. * from vidioc_reqbufs handler in driver.
  540. */
  541. static int __reqbufs(struct vb2_queue *q, struct v4l2_requestbuffers *req)
  542. {
  543. unsigned int num_buffers, allocated_buffers, num_planes = 0;
  544. int ret;
  545. if (q->streaming) {
  546. dprintk(1, "reqbufs: streaming active\n");
  547. return -EBUSY;
  548. }
  549. if (req->count == 0 || q->num_buffers != 0 || q->memory != req->memory) {
  550. /*
  551. * We already have buffers allocated, so first check if they
  552. * are not in use and can be freed.
  553. */
  554. if (q->memory == V4L2_MEMORY_MMAP && __buffers_in_use(q)) {
  555. dprintk(1, "reqbufs: memory in use, cannot free\n");
  556. return -EBUSY;
  557. }
  558. __vb2_queue_free(q, q->num_buffers);
  559. /*
  560. * In case of REQBUFS(0) return immediately without calling
  561. * driver's queue_setup() callback and allocating resources.
  562. */
  563. if (req->count == 0)
  564. return 0;
  565. }
  566. /*
  567. * Make sure the requested values and current defaults are sane.
  568. */
  569. num_buffers = min_t(unsigned int, req->count, VIDEO_MAX_FRAME);
  570. memset(q->plane_sizes, 0, sizeof(q->plane_sizes));
  571. memset(q->alloc_ctx, 0, sizeof(q->alloc_ctx));
  572. q->memory = req->memory;
  573. /*
  574. * Ask the driver how many buffers and planes per buffer it requires.
  575. * Driver also sets the size and allocator context for each plane.
  576. */
  577. ret = call_qop(q, queue_setup, q, NULL, &num_buffers, &num_planes,
  578. q->plane_sizes, q->alloc_ctx);
  579. if (ret)
  580. return ret;
  581. /* Finally, allocate buffers and video memory */
  582. ret = __vb2_queue_alloc(q, req->memory, num_buffers, num_planes);
  583. if (ret == 0) {
  584. dprintk(1, "Memory allocation failed\n");
  585. return -ENOMEM;
  586. }
  587. allocated_buffers = ret;
  588. /*
  589. * Check if driver can handle the allocated number of buffers.
  590. */
  591. if (allocated_buffers < num_buffers) {
  592. num_buffers = allocated_buffers;
  593. ret = call_qop(q, queue_setup, q, NULL, &num_buffers,
  594. &num_planes, q->plane_sizes, q->alloc_ctx);
  595. if (!ret && allocated_buffers < num_buffers)
  596. ret = -ENOMEM;
  597. /*
  598. * Either the driver has accepted a smaller number of buffers,
  599. * or .queue_setup() returned an error
  600. */
  601. }
  602. q->num_buffers = allocated_buffers;
  603. if (ret < 0) {
  604. __vb2_queue_free(q, allocated_buffers);
  605. return ret;
  606. }
  607. /*
  608. * Return the number of successfully allocated buffers
  609. * to the userspace.
  610. */
  611. req->count = allocated_buffers;
  612. return 0;
  613. }
  614. /**
  615. * vb2_reqbufs() - Wrapper for __reqbufs() that also verifies the memory and
  616. * type values.
  617. * @q: videobuf2 queue
  618. * @req: struct passed from userspace to vidioc_reqbufs handler in driver
  619. */
  620. int vb2_reqbufs(struct vb2_queue *q, struct v4l2_requestbuffers *req)
  621. {
  622. int ret = __verify_memory_type(q, req->memory, req->type);
  623. return ret ? ret : __reqbufs(q, req);
  624. }
  625. EXPORT_SYMBOL_GPL(vb2_reqbufs);
  626. /**
  627. * __create_bufs() - Allocate buffers and any required auxiliary structs
  628. * @q: videobuf2 queue
  629. * @create: creation parameters, passed from userspace to vidioc_create_bufs
  630. * handler in driver
  631. *
  632. * Should be called from vidioc_create_bufs ioctl handler of a driver.
  633. * This function:
  634. * 1) verifies parameter sanity
  635. * 2) calls the .queue_setup() queue operation
  636. * 3) performs any necessary memory allocations
  637. *
  638. * The return values from this function are intended to be directly returned
  639. * from vidioc_create_bufs handler in driver.
  640. */
  641. static int __create_bufs(struct vb2_queue *q, struct v4l2_create_buffers *create)
  642. {
  643. unsigned int num_planes = 0, num_buffers, allocated_buffers;
  644. int ret;
  645. if (q->num_buffers == VIDEO_MAX_FRAME) {
  646. dprintk(1, "%s(): maximum number of buffers already allocated\n",
  647. __func__);
  648. return -ENOBUFS;
  649. }
  650. if (!q->num_buffers) {
  651. memset(q->plane_sizes, 0, sizeof(q->plane_sizes));
  652. memset(q->alloc_ctx, 0, sizeof(q->alloc_ctx));
  653. q->memory = create->memory;
  654. }
  655. num_buffers = min(create->count, VIDEO_MAX_FRAME - q->num_buffers);
  656. /*
  657. * Ask the driver, whether the requested number of buffers, planes per
  658. * buffer and their sizes are acceptable
  659. */
  660. ret = call_qop(q, queue_setup, q, &create->format, &num_buffers,
  661. &num_planes, q->plane_sizes, q->alloc_ctx);
  662. if (ret)
  663. return ret;
  664. /* Finally, allocate buffers and video memory */
  665. ret = __vb2_queue_alloc(q, create->memory, num_buffers,
  666. num_planes);
  667. if (ret == 0) {
  668. dprintk(1, "Memory allocation failed\n");
  669. return -ENOMEM;
  670. }
  671. allocated_buffers = ret;
  672. /*
  673. * Check if driver can handle the so far allocated number of buffers.
  674. */
  675. if (ret < num_buffers) {
  676. num_buffers = ret;
  677. /*
  678. * q->num_buffers contains the total number of buffers, that the
  679. * queue driver has set up
  680. */
  681. ret = call_qop(q, queue_setup, q, &create->format, &num_buffers,
  682. &num_planes, q->plane_sizes, q->alloc_ctx);
  683. if (!ret && allocated_buffers < num_buffers)
  684. ret = -ENOMEM;
  685. /*
  686. * Either the driver has accepted a smaller number of buffers,
  687. * or .queue_setup() returned an error
  688. */
  689. }
  690. q->num_buffers += allocated_buffers;
  691. if (ret < 0) {
  692. __vb2_queue_free(q, allocated_buffers);
  693. return -ENOMEM;
  694. }
  695. /*
  696. * Return the number of successfully allocated buffers
  697. * to the userspace.
  698. */
  699. create->count = allocated_buffers;
  700. return 0;
  701. }
  702. /**
  703. * vb2_create_bufs() - Wrapper for __create_bufs() that also verifies the
  704. * memory and type values.
  705. * @q: videobuf2 queue
  706. * @create: creation parameters, passed from userspace to vidioc_create_bufs
  707. * handler in driver
  708. */
  709. int vb2_create_bufs(struct vb2_queue *q, struct v4l2_create_buffers *create)
  710. {
  711. int ret = __verify_memory_type(q, create->memory, create->format.type);
  712. create->index = q->num_buffers;
  713. if (create->count == 0)
  714. return ret != -EBUSY ? ret : 0;
  715. return ret ? ret : __create_bufs(q, create);
  716. }
  717. EXPORT_SYMBOL_GPL(vb2_create_bufs);
  718. /**
  719. * vb2_plane_vaddr() - Return a kernel virtual address of a given plane
  720. * @vb: vb2_buffer to which the plane in question belongs to
  721. * @plane_no: plane number for which the address is to be returned
  722. *
  723. * This function returns a kernel virtual address of a given plane if
  724. * such a mapping exist, NULL otherwise.
  725. */
  726. void *vb2_plane_vaddr(struct vb2_buffer *vb, unsigned int plane_no)
  727. {
  728. struct vb2_queue *q = vb->vb2_queue;
  729. if (plane_no > vb->num_planes || !vb->planes[plane_no].mem_priv)
  730. return NULL;
  731. return call_memop(q, vaddr, vb->planes[plane_no].mem_priv);
  732. }
  733. EXPORT_SYMBOL_GPL(vb2_plane_vaddr);
  734. /**
  735. * vb2_plane_cookie() - Return allocator specific cookie for the given plane
  736. * @vb: vb2_buffer to which the plane in question belongs to
  737. * @plane_no: plane number for which the cookie is to be returned
  738. *
  739. * This function returns an allocator specific cookie for a given plane if
  740. * available, NULL otherwise. The allocator should provide some simple static
  741. * inline function, which would convert this cookie to the allocator specific
  742. * type that can be used directly by the driver to access the buffer. This can
  743. * be for example physical address, pointer to scatter list or IOMMU mapping.
  744. */
  745. void *vb2_plane_cookie(struct vb2_buffer *vb, unsigned int plane_no)
  746. {
  747. struct vb2_queue *q = vb->vb2_queue;
  748. if (plane_no > vb->num_planes || !vb->planes[plane_no].mem_priv)
  749. return NULL;
  750. return call_memop(q, cookie, vb->planes[plane_no].mem_priv);
  751. }
  752. EXPORT_SYMBOL_GPL(vb2_plane_cookie);
  753. /**
  754. * vb2_buffer_done() - inform videobuf that an operation on a buffer is finished
  755. * @vb: vb2_buffer returned from the driver
  756. * @state: either VB2_BUF_STATE_DONE if the operation finished successfully
  757. * or VB2_BUF_STATE_ERROR if the operation finished with an error
  758. *
  759. * This function should be called by the driver after a hardware operation on
  760. * a buffer is finished and the buffer may be returned to userspace. The driver
  761. * cannot use this buffer anymore until it is queued back to it by videobuf
  762. * by the means of buf_queue callback. Only buffers previously queued to the
  763. * driver by buf_queue can be passed to this function.
  764. */
  765. void vb2_buffer_done(struct vb2_buffer *vb, enum vb2_buffer_state state)
  766. {
  767. struct vb2_queue *q = vb->vb2_queue;
  768. unsigned long flags;
  769. unsigned int plane;
  770. if (vb->state != VB2_BUF_STATE_ACTIVE)
  771. return;
  772. if (state != VB2_BUF_STATE_DONE && state != VB2_BUF_STATE_ERROR)
  773. return;
  774. dprintk(4, "Done processing on buffer %d, state: %d\n",
  775. vb->v4l2_buf.index, state);
  776. /* sync buffers */
  777. for (plane = 0; plane < vb->num_planes; ++plane)
  778. call_memop(q, finish, vb->planes[plane].mem_priv);
  779. /* Add the buffer to the done buffers list */
  780. spin_lock_irqsave(&q->done_lock, flags);
  781. vb->state = state;
  782. list_add_tail(&vb->done_entry, &q->done_list);
  783. atomic_dec(&q->queued_count);
  784. spin_unlock_irqrestore(&q->done_lock, flags);
  785. /* Inform any processes that may be waiting for buffers */
  786. wake_up(&q->done_wq);
  787. }
  788. EXPORT_SYMBOL_GPL(vb2_buffer_done);
  789. /**
  790. * __fill_vb2_buffer() - fill a vb2_buffer with information provided in a
  791. * v4l2_buffer by the userspace. The caller has already verified that struct
  792. * v4l2_buffer has a valid number of planes.
  793. */
  794. static void __fill_vb2_buffer(struct vb2_buffer *vb, const struct v4l2_buffer *b,
  795. struct v4l2_plane *v4l2_planes)
  796. {
  797. unsigned int plane;
  798. if (V4L2_TYPE_IS_MULTIPLANAR(b->type)) {
  799. /* Fill in driver-provided information for OUTPUT types */
  800. if (V4L2_TYPE_IS_OUTPUT(b->type)) {
  801. /*
  802. * Will have to go up to b->length when API starts
  803. * accepting variable number of planes.
  804. */
  805. for (plane = 0; plane < vb->num_planes; ++plane) {
  806. v4l2_planes[plane].bytesused =
  807. b->m.planes[plane].bytesused;
  808. v4l2_planes[plane].data_offset =
  809. b->m.planes[plane].data_offset;
  810. }
  811. }
  812. if (b->memory == V4L2_MEMORY_USERPTR) {
  813. for (plane = 0; plane < vb->num_planes; ++plane) {
  814. v4l2_planes[plane].m.userptr =
  815. b->m.planes[plane].m.userptr;
  816. v4l2_planes[plane].length =
  817. b->m.planes[plane].length;
  818. }
  819. }
  820. if (b->memory == V4L2_MEMORY_DMABUF) {
  821. for (plane = 0; plane < vb->num_planes; ++plane) {
  822. v4l2_planes[plane].m.fd =
  823. b->m.planes[plane].m.fd;
  824. v4l2_planes[plane].length =
  825. b->m.planes[plane].length;
  826. v4l2_planes[plane].data_offset =
  827. b->m.planes[plane].data_offset;
  828. }
  829. }
  830. } else {
  831. /*
  832. * Single-planar buffers do not use planes array,
  833. * so fill in relevant v4l2_buffer struct fields instead.
  834. * In videobuf we use our internal V4l2_planes struct for
  835. * single-planar buffers as well, for simplicity.
  836. */
  837. if (V4L2_TYPE_IS_OUTPUT(b->type)) {
  838. v4l2_planes[0].bytesused = b->bytesused;
  839. v4l2_planes[0].data_offset = 0;
  840. }
  841. if (b->memory == V4L2_MEMORY_USERPTR) {
  842. v4l2_planes[0].m.userptr = b->m.userptr;
  843. v4l2_planes[0].length = b->length;
  844. }
  845. if (b->memory == V4L2_MEMORY_DMABUF) {
  846. v4l2_planes[0].m.fd = b->m.fd;
  847. v4l2_planes[0].length = b->length;
  848. v4l2_planes[0].data_offset = 0;
  849. }
  850. }
  851. vb->v4l2_buf.field = b->field;
  852. vb->v4l2_buf.timestamp = b->timestamp;
  853. vb->v4l2_buf.flags = b->flags & ~V4L2_BUFFER_MASK_FLAGS;
  854. }
  855. /**
  856. * __qbuf_userptr() - handle qbuf of a USERPTR buffer
  857. */
  858. static int __qbuf_userptr(struct vb2_buffer *vb, const struct v4l2_buffer *b)
  859. {
  860. struct v4l2_plane planes[VIDEO_MAX_PLANES];
  861. struct vb2_queue *q = vb->vb2_queue;
  862. void *mem_priv;
  863. unsigned int plane;
  864. int ret;
  865. int write = !V4L2_TYPE_IS_OUTPUT(q->type);
  866. /* Copy relevant information provided by the userspace */
  867. __fill_vb2_buffer(vb, b, planes);
  868. for (plane = 0; plane < vb->num_planes; ++plane) {
  869. /* Skip the plane if already verified */
  870. if (vb->v4l2_planes[plane].m.userptr &&
  871. vb->v4l2_planes[plane].m.userptr == planes[plane].m.userptr
  872. && vb->v4l2_planes[plane].length == planes[plane].length)
  873. continue;
  874. dprintk(3, "qbuf: userspace address for plane %d changed, "
  875. "reacquiring memory\n", plane);
  876. /* Check if the provided plane buffer is large enough */
  877. if (planes[plane].length < q->plane_sizes[plane]) {
  878. dprintk(1, "qbuf: provided buffer size %u is less than "
  879. "setup size %u for plane %d\n",
  880. planes[plane].length,
  881. q->plane_sizes[plane], plane);
  882. ret = -EINVAL;
  883. goto err;
  884. }
  885. /* Release previously acquired memory if present */
  886. if (vb->planes[plane].mem_priv)
  887. call_memop(q, put_userptr, vb->planes[plane].mem_priv);
  888. vb->planes[plane].mem_priv = NULL;
  889. vb->v4l2_planes[plane].m.userptr = 0;
  890. vb->v4l2_planes[plane].length = 0;
  891. /* Acquire each plane's memory */
  892. mem_priv = call_memop(q, get_userptr, q->alloc_ctx[plane],
  893. planes[plane].m.userptr,
  894. planes[plane].length, write);
  895. if (IS_ERR_OR_NULL(mem_priv)) {
  896. dprintk(1, "qbuf: failed acquiring userspace "
  897. "memory for plane %d\n", plane);
  898. ret = mem_priv ? PTR_ERR(mem_priv) : -EINVAL;
  899. goto err;
  900. }
  901. vb->planes[plane].mem_priv = mem_priv;
  902. }
  903. /*
  904. * Call driver-specific initialization on the newly acquired buffer,
  905. * if provided.
  906. */
  907. ret = call_qop(q, buf_init, vb);
  908. if (ret) {
  909. dprintk(1, "qbuf: buffer initialization failed\n");
  910. goto err;
  911. }
  912. /*
  913. * Now that everything is in order, copy relevant information
  914. * provided by userspace.
  915. */
  916. for (plane = 0; plane < vb->num_planes; ++plane)
  917. vb->v4l2_planes[plane] = planes[plane];
  918. return 0;
  919. err:
  920. /* In case of errors, release planes that were already acquired */
  921. for (plane = 0; plane < vb->num_planes; ++plane) {
  922. if (vb->planes[plane].mem_priv)
  923. call_memop(q, put_userptr, vb->planes[plane].mem_priv);
  924. vb->planes[plane].mem_priv = NULL;
  925. vb->v4l2_planes[plane].m.userptr = 0;
  926. vb->v4l2_planes[plane].length = 0;
  927. }
  928. return ret;
  929. }
  930. /**
  931. * __qbuf_mmap() - handle qbuf of an MMAP buffer
  932. */
  933. static int __qbuf_mmap(struct vb2_buffer *vb, const struct v4l2_buffer *b)
  934. {
  935. __fill_vb2_buffer(vb, b, vb->v4l2_planes);
  936. return 0;
  937. }
  938. /**
  939. * __qbuf_dmabuf() - handle qbuf of a DMABUF buffer
  940. */
  941. static int __qbuf_dmabuf(struct vb2_buffer *vb, const struct v4l2_buffer *b)
  942. {
  943. struct v4l2_plane planes[VIDEO_MAX_PLANES];
  944. struct vb2_queue *q = vb->vb2_queue;
  945. void *mem_priv;
  946. unsigned int plane;
  947. int ret;
  948. int write = !V4L2_TYPE_IS_OUTPUT(q->type);
  949. /* Verify and copy relevant information provided by the userspace */
  950. __fill_vb2_buffer(vb, b, planes);
  951. for (plane = 0; plane < vb->num_planes; ++plane) {
  952. struct dma_buf *dbuf = dma_buf_get(planes[plane].m.fd);
  953. if (IS_ERR_OR_NULL(dbuf)) {
  954. dprintk(1, "qbuf: invalid dmabuf fd for plane %d\n",
  955. plane);
  956. ret = -EINVAL;
  957. goto err;
  958. }
  959. /* use DMABUF size if length is not provided */
  960. if (planes[plane].length == 0)
  961. planes[plane].length = dbuf->size;
  962. if (planes[plane].length < planes[plane].data_offset +
  963. q->plane_sizes[plane]) {
  964. ret = -EINVAL;
  965. goto err;
  966. }
  967. /* Skip the plane if already verified */
  968. if (dbuf == vb->planes[plane].dbuf &&
  969. vb->v4l2_planes[plane].length == planes[plane].length) {
  970. dma_buf_put(dbuf);
  971. continue;
  972. }
  973. dprintk(1, "qbuf: buffer for plane %d changed\n", plane);
  974. /* Release previously acquired memory if present */
  975. __vb2_plane_dmabuf_put(q, &vb->planes[plane]);
  976. memset(&vb->v4l2_planes[plane], 0, sizeof(struct v4l2_plane));
  977. /* Acquire each plane's memory */
  978. mem_priv = call_memop(q, attach_dmabuf, q->alloc_ctx[plane],
  979. dbuf, planes[plane].length, write);
  980. if (IS_ERR(mem_priv)) {
  981. dprintk(1, "qbuf: failed to attach dmabuf\n");
  982. ret = PTR_ERR(mem_priv);
  983. dma_buf_put(dbuf);
  984. goto err;
  985. }
  986. vb->planes[plane].dbuf = dbuf;
  987. vb->planes[plane].mem_priv = mem_priv;
  988. }
  989. /* TODO: This pins the buffer(s) with dma_buf_map_attachment()).. but
  990. * really we want to do this just before the DMA, not while queueing
  991. * the buffer(s)..
  992. */
  993. for (plane = 0; plane < vb->num_planes; ++plane) {
  994. ret = call_memop(q, map_dmabuf, vb->planes[plane].mem_priv);
  995. if (ret) {
  996. dprintk(1, "qbuf: failed to map dmabuf for plane %d\n",
  997. plane);
  998. goto err;
  999. }
  1000. vb->planes[plane].dbuf_mapped = 1;
  1001. }
  1002. /*
  1003. * Call driver-specific initialization on the newly acquired buffer,
  1004. * if provided.
  1005. */
  1006. ret = call_qop(q, buf_init, vb);
  1007. if (ret) {
  1008. dprintk(1, "qbuf: buffer initialization failed\n");
  1009. goto err;
  1010. }
  1011. /*
  1012. * Now that everything is in order, copy relevant information
  1013. * provided by userspace.
  1014. */
  1015. for (plane = 0; plane < vb->num_planes; ++plane)
  1016. vb->v4l2_planes[plane] = planes[plane];
  1017. return 0;
  1018. err:
  1019. /* In case of errors, release planes that were already acquired */
  1020. __vb2_buf_dmabuf_put(vb);
  1021. return ret;
  1022. }
  1023. /**
  1024. * __enqueue_in_driver() - enqueue a vb2_buffer in driver for processing
  1025. */
  1026. static void __enqueue_in_driver(struct vb2_buffer *vb)
  1027. {
  1028. struct vb2_queue *q = vb->vb2_queue;
  1029. unsigned int plane;
  1030. vb->state = VB2_BUF_STATE_ACTIVE;
  1031. atomic_inc(&q->queued_count);
  1032. /* sync buffers */
  1033. for (plane = 0; plane < vb->num_planes; ++plane)
  1034. call_memop(q, prepare, vb->planes[plane].mem_priv);
  1035. q->ops->buf_queue(vb);
  1036. }
  1037. static int __buf_prepare(struct vb2_buffer *vb, const struct v4l2_buffer *b)
  1038. {
  1039. struct vb2_queue *q = vb->vb2_queue;
  1040. int ret;
  1041. ret = __verify_length(vb, b);
  1042. if (ret < 0) {
  1043. dprintk(1, "%s(): plane parameters verification failed: %d\n",
  1044. __func__, ret);
  1045. return ret;
  1046. }
  1047. switch (q->memory) {
  1048. case V4L2_MEMORY_MMAP:
  1049. ret = __qbuf_mmap(vb, b);
  1050. break;
  1051. case V4L2_MEMORY_USERPTR:
  1052. ret = __qbuf_userptr(vb, b);
  1053. break;
  1054. case V4L2_MEMORY_DMABUF:
  1055. ret = __qbuf_dmabuf(vb, b);
  1056. break;
  1057. default:
  1058. WARN(1, "Invalid queue type\n");
  1059. ret = -EINVAL;
  1060. }
  1061. if (!ret)
  1062. ret = call_qop(q, buf_prepare, vb);
  1063. if (ret)
  1064. dprintk(1, "qbuf: buffer preparation failed: %d\n", ret);
  1065. else
  1066. vb->state = VB2_BUF_STATE_PREPARED;
  1067. return ret;
  1068. }
  1069. static int vb2_queue_or_prepare_buf(struct vb2_queue *q, struct v4l2_buffer *b,
  1070. const char *opname,
  1071. int (*handler)(struct vb2_queue *,
  1072. struct v4l2_buffer *,
  1073. struct vb2_buffer *))
  1074. {
  1075. struct rw_semaphore *mmap_sem = NULL;
  1076. struct vb2_buffer *vb;
  1077. int ret;
  1078. /*
  1079. * In case of user pointer buffers vb2 allocators need to get direct
  1080. * access to userspace pages. This requires getting the mmap semaphore
  1081. * for read access in the current process structure. The same semaphore
  1082. * is taken before calling mmap operation, while both qbuf/prepare_buf
  1083. * and mmap are called by the driver or v4l2 core with the driver's lock
  1084. * held. To avoid an AB-BA deadlock (mmap_sem then driver's lock in mmap
  1085. * and driver's lock then mmap_sem in qbuf/prepare_buf) the videobuf2
  1086. * core releases the driver's lock, takes mmap_sem and then takes the
  1087. * driver's lock again.
  1088. *
  1089. * To avoid racing with other vb2 calls, which might be called after
  1090. * releasing the driver's lock, this operation is performed at the
  1091. * beginning of qbuf/prepare_buf processing. This way the queue status
  1092. * is consistent after getting the driver's lock back.
  1093. */
  1094. if (q->memory == V4L2_MEMORY_USERPTR) {
  1095. mmap_sem = &current->mm->mmap_sem;
  1096. call_qop(q, wait_prepare, q);
  1097. down_read(mmap_sem);
  1098. call_qop(q, wait_finish, q);
  1099. }
  1100. if (q->fileio) {
  1101. dprintk(1, "%s(): file io in progress\n", opname);
  1102. ret = -EBUSY;
  1103. goto unlock;
  1104. }
  1105. if (b->type != q->type) {
  1106. dprintk(1, "%s(): invalid buffer type\n", opname);
  1107. ret = -EINVAL;
  1108. goto unlock;
  1109. }
  1110. if (b->index >= q->num_buffers) {
  1111. dprintk(1, "%s(): buffer index out of range\n", opname);
  1112. ret = -EINVAL;
  1113. goto unlock;
  1114. }
  1115. vb = q->bufs[b->index];
  1116. if (NULL == vb) {
  1117. /* Should never happen */
  1118. dprintk(1, "%s(): buffer is NULL\n", opname);
  1119. ret = -EINVAL;
  1120. goto unlock;
  1121. }
  1122. if (b->memory != q->memory) {
  1123. dprintk(1, "%s(): invalid memory type\n", opname);
  1124. ret = -EINVAL;
  1125. goto unlock;
  1126. }
  1127. ret = __verify_planes_array(vb, b);
  1128. if (ret)
  1129. goto unlock;
  1130. ret = handler(q, b, vb);
  1131. if (ret)
  1132. goto unlock;
  1133. /* Fill buffer information for the userspace */
  1134. __fill_v4l2_buffer(vb, b);
  1135. dprintk(1, "%s() of buffer %d succeeded\n", opname, vb->v4l2_buf.index);
  1136. unlock:
  1137. if (mmap_sem)
  1138. up_read(mmap_sem);
  1139. return ret;
  1140. }
  1141. static int __vb2_prepare_buf(struct vb2_queue *q, struct v4l2_buffer *b,
  1142. struct vb2_buffer *vb)
  1143. {
  1144. if (vb->state != VB2_BUF_STATE_DEQUEUED) {
  1145. dprintk(1, "%s(): invalid buffer state %d\n", __func__,
  1146. vb->state);
  1147. return -EINVAL;
  1148. }
  1149. return __buf_prepare(vb, b);
  1150. }
  1151. /**
  1152. * vb2_prepare_buf() - Pass ownership of a buffer from userspace to the kernel
  1153. * @q: videobuf2 queue
  1154. * @b: buffer structure passed from userspace to vidioc_prepare_buf
  1155. * handler in driver
  1156. *
  1157. * Should be called from vidioc_prepare_buf ioctl handler of a driver.
  1158. * This function:
  1159. * 1) verifies the passed buffer,
  1160. * 2) calls buf_prepare callback in the driver (if provided), in which
  1161. * driver-specific buffer initialization can be performed,
  1162. *
  1163. * The return values from this function are intended to be directly returned
  1164. * from vidioc_prepare_buf handler in driver.
  1165. */
  1166. int vb2_prepare_buf(struct vb2_queue *q, struct v4l2_buffer *b)
  1167. {
  1168. return vb2_queue_or_prepare_buf(q, b, "prepare_buf", __vb2_prepare_buf);
  1169. }
  1170. EXPORT_SYMBOL_GPL(vb2_prepare_buf);
  1171. static int __vb2_qbuf(struct vb2_queue *q, struct v4l2_buffer *b,
  1172. struct vb2_buffer *vb)
  1173. {
  1174. int ret;
  1175. switch (vb->state) {
  1176. case VB2_BUF_STATE_DEQUEUED:
  1177. ret = __buf_prepare(vb, b);
  1178. if (ret)
  1179. return ret;
  1180. case VB2_BUF_STATE_PREPARED:
  1181. break;
  1182. default:
  1183. dprintk(1, "qbuf: buffer already in use\n");
  1184. return -EINVAL;
  1185. }
  1186. /*
  1187. * Add to the queued buffers list, a buffer will stay on it until
  1188. * dequeued in dqbuf.
  1189. */
  1190. list_add_tail(&vb->queued_entry, &q->queued_list);
  1191. vb->state = VB2_BUF_STATE_QUEUED;
  1192. /*
  1193. * If already streaming, give the buffer to driver for processing.
  1194. * If not, the buffer will be given to driver on next streamon.
  1195. */
  1196. if (q->streaming)
  1197. __enqueue_in_driver(vb);
  1198. return 0;
  1199. }
  1200. /**
  1201. * vb2_qbuf() - Queue a buffer from userspace
  1202. * @q: videobuf2 queue
  1203. * @b: buffer structure passed from userspace to vidioc_qbuf handler
  1204. * in driver
  1205. *
  1206. * Should be called from vidioc_qbuf ioctl handler of a driver.
  1207. * This function:
  1208. * 1) verifies the passed buffer,
  1209. * 2) if necessary, calls buf_prepare callback in the driver (if provided), in
  1210. * which driver-specific buffer initialization can be performed,
  1211. * 3) if streaming is on, queues the buffer in driver by the means of buf_queue
  1212. * callback for processing.
  1213. *
  1214. * The return values from this function are intended to be directly returned
  1215. * from vidioc_qbuf handler in driver.
  1216. */
  1217. int vb2_qbuf(struct vb2_queue *q, struct v4l2_buffer *b)
  1218. {
  1219. return vb2_queue_or_prepare_buf(q, b, "qbuf", __vb2_qbuf);
  1220. }
  1221. EXPORT_SYMBOL_GPL(vb2_qbuf);
  1222. /**
  1223. * __vb2_wait_for_done_vb() - wait for a buffer to become available
  1224. * for dequeuing
  1225. *
  1226. * Will sleep if required for nonblocking == false.
  1227. */
  1228. static int __vb2_wait_for_done_vb(struct vb2_queue *q, int nonblocking)
  1229. {
  1230. /*
  1231. * All operations on vb_done_list are performed under done_lock
  1232. * spinlock protection. However, buffers may be removed from
  1233. * it and returned to userspace only while holding both driver's
  1234. * lock and the done_lock spinlock. Thus we can be sure that as
  1235. * long as we hold the driver's lock, the list will remain not
  1236. * empty if list_empty() check succeeds.
  1237. */
  1238. for (;;) {
  1239. int ret;
  1240. if (!q->streaming) {
  1241. dprintk(1, "Streaming off, will not wait for buffers\n");
  1242. return -EINVAL;
  1243. }
  1244. if (!list_empty(&q->done_list)) {
  1245. /*
  1246. * Found a buffer that we were waiting for.
  1247. */
  1248. break;
  1249. }
  1250. if (nonblocking) {
  1251. dprintk(1, "Nonblocking and no buffers to dequeue, "
  1252. "will not wait\n");
  1253. return -EAGAIN;
  1254. }
  1255. /*
  1256. * We are streaming and blocking, wait for another buffer to
  1257. * become ready or for streamoff. Driver's lock is released to
  1258. * allow streamoff or qbuf to be called while waiting.
  1259. */
  1260. call_qop(q, wait_prepare, q);
  1261. /*
  1262. * All locks have been released, it is safe to sleep now.
  1263. */
  1264. dprintk(3, "Will sleep waiting for buffers\n");
  1265. ret = wait_event_interruptible(q->done_wq,
  1266. !list_empty(&q->done_list) || !q->streaming);
  1267. /*
  1268. * We need to reevaluate both conditions again after reacquiring
  1269. * the locks or return an error if one occurred.
  1270. */
  1271. call_qop(q, wait_finish, q);
  1272. if (ret) {
  1273. dprintk(1, "Sleep was interrupted\n");
  1274. return ret;
  1275. }
  1276. }
  1277. return 0;
  1278. }
  1279. /**
  1280. * __vb2_get_done_vb() - get a buffer ready for dequeuing
  1281. *
  1282. * Will sleep if required for nonblocking == false.
  1283. */
  1284. static int __vb2_get_done_vb(struct vb2_queue *q, struct vb2_buffer **vb,
  1285. struct v4l2_buffer *b, int nonblocking)
  1286. {
  1287. unsigned long flags;
  1288. int ret;
  1289. /*
  1290. * Wait for at least one buffer to become available on the done_list.
  1291. */
  1292. ret = __vb2_wait_for_done_vb(q, nonblocking);
  1293. if (ret)
  1294. return ret;
  1295. /*
  1296. * Driver's lock has been held since we last verified that done_list
  1297. * is not empty, so no need for another list_empty(done_list) check.
  1298. */
  1299. spin_lock_irqsave(&q->done_lock, flags);
  1300. *vb = list_first_entry(&q->done_list, struct vb2_buffer, done_entry);
  1301. /*
  1302. * Only remove the buffer from done_list if v4l2_buffer can handle all
  1303. * the planes.
  1304. */
  1305. ret = __verify_planes_array(*vb, b);
  1306. if (!ret)
  1307. list_del(&(*vb)->done_entry);
  1308. spin_unlock_irqrestore(&q->done_lock, flags);
  1309. return ret;
  1310. }
  1311. /**
  1312. * vb2_wait_for_all_buffers() - wait until all buffers are given back to vb2
  1313. * @q: videobuf2 queue
  1314. *
  1315. * This function will wait until all buffers that have been given to the driver
  1316. * by buf_queue() are given back to vb2 with vb2_buffer_done(). It doesn't call
  1317. * wait_prepare, wait_finish pair. It is intended to be called with all locks
  1318. * taken, for example from stop_streaming() callback.
  1319. */
  1320. int vb2_wait_for_all_buffers(struct vb2_queue *q)
  1321. {
  1322. if (!q->streaming) {
  1323. dprintk(1, "Streaming off, will not wait for buffers\n");
  1324. return -EINVAL;
  1325. }
  1326. wait_event(q->done_wq, !atomic_read(&q->queued_count));
  1327. return 0;
  1328. }
  1329. EXPORT_SYMBOL_GPL(vb2_wait_for_all_buffers);
  1330. /**
  1331. * __vb2_dqbuf() - bring back the buffer to the DEQUEUED state
  1332. */
  1333. static void __vb2_dqbuf(struct vb2_buffer *vb)
  1334. {
  1335. struct vb2_queue *q = vb->vb2_queue;
  1336. unsigned int i;
  1337. /* nothing to do if the buffer is already dequeued */
  1338. if (vb->state == VB2_BUF_STATE_DEQUEUED)
  1339. return;
  1340. vb->state = VB2_BUF_STATE_DEQUEUED;
  1341. /* unmap DMABUF buffer */
  1342. if (q->memory == V4L2_MEMORY_DMABUF)
  1343. for (i = 0; i < vb->num_planes; ++i) {
  1344. if (!vb->planes[i].dbuf_mapped)
  1345. continue;
  1346. call_memop(q, unmap_dmabuf, vb->planes[i].mem_priv);
  1347. vb->planes[i].dbuf_mapped = 0;
  1348. }
  1349. }
  1350. /**
  1351. * vb2_dqbuf() - Dequeue a buffer to the userspace
  1352. * @q: videobuf2 queue
  1353. * @b: buffer structure passed from userspace to vidioc_dqbuf handler
  1354. * in driver
  1355. * @nonblocking: if true, this call will not sleep waiting for a buffer if no
  1356. * buffers ready for dequeuing are present. Normally the driver
  1357. * would be passing (file->f_flags & O_NONBLOCK) here
  1358. *
  1359. * Should be called from vidioc_dqbuf ioctl handler of a driver.
  1360. * This function:
  1361. * 1) verifies the passed buffer,
  1362. * 2) calls buf_finish callback in the driver (if provided), in which
  1363. * driver can perform any additional operations that may be required before
  1364. * returning the buffer to userspace, such as cache sync,
  1365. * 3) the buffer struct members are filled with relevant information for
  1366. * the userspace.
  1367. *
  1368. * The return values from this function are intended to be directly returned
  1369. * from vidioc_dqbuf handler in driver.
  1370. */
  1371. int vb2_dqbuf(struct vb2_queue *q, struct v4l2_buffer *b, bool nonblocking)
  1372. {
  1373. struct vb2_buffer *vb = NULL;
  1374. int ret;
  1375. if (q->fileio) {
  1376. dprintk(1, "dqbuf: file io in progress\n");
  1377. return -EBUSY;
  1378. }
  1379. if (b->type != q->type) {
  1380. dprintk(1, "dqbuf: invalid buffer type\n");
  1381. return -EINVAL;
  1382. }
  1383. ret = __vb2_get_done_vb(q, &vb, b, nonblocking);
  1384. if (ret < 0)
  1385. return ret;
  1386. ret = call_qop(q, buf_finish, vb);
  1387. if (ret) {
  1388. dprintk(1, "dqbuf: buffer finish failed\n");
  1389. return ret;
  1390. }
  1391. switch (vb->state) {
  1392. case VB2_BUF_STATE_DONE:
  1393. dprintk(3, "dqbuf: Returning done buffer\n");
  1394. break;
  1395. case VB2_BUF_STATE_ERROR:
  1396. dprintk(3, "dqbuf: Returning done buffer with errors\n");
  1397. break;
  1398. default:
  1399. dprintk(1, "dqbuf: Invalid buffer state\n");
  1400. return -EINVAL;
  1401. }
  1402. /* Fill buffer information for the userspace */
  1403. __fill_v4l2_buffer(vb, b);
  1404. /* Remove from videobuf queue */
  1405. list_del(&vb->queued_entry);
  1406. /* go back to dequeued state */
  1407. __vb2_dqbuf(vb);
  1408. dprintk(1, "dqbuf of buffer %d, with state %d\n",
  1409. vb->v4l2_buf.index, vb->state);
  1410. return 0;
  1411. }
  1412. EXPORT_SYMBOL_GPL(vb2_dqbuf);
  1413. /**
  1414. * __vb2_queue_cancel() - cancel and stop (pause) streaming
  1415. *
  1416. * Removes all queued buffers from driver's queue and all buffers queued by
  1417. * userspace from videobuf's queue. Returns to state after reqbufs.
  1418. */
  1419. static void __vb2_queue_cancel(struct vb2_queue *q)
  1420. {
  1421. unsigned int i;
  1422. /*
  1423. * Tell driver to stop all transactions and release all queued
  1424. * buffers.
  1425. */
  1426. if (q->streaming)
  1427. call_qop(q, stop_streaming, q);
  1428. q->streaming = 0;
  1429. /*
  1430. * Remove all buffers from videobuf's list...
  1431. */
  1432. INIT_LIST_HEAD(&q->queued_list);
  1433. /*
  1434. * ...and done list; userspace will not receive any buffers it
  1435. * has not already dequeued before initiating cancel.
  1436. */
  1437. INIT_LIST_HEAD(&q->done_list);
  1438. atomic_set(&q->queued_count, 0);
  1439. wake_up_all(&q->done_wq);
  1440. /*
  1441. * Reinitialize all buffers for next use.
  1442. */
  1443. for (i = 0; i < q->num_buffers; ++i)
  1444. __vb2_dqbuf(q->bufs[i]);
  1445. }
  1446. /**
  1447. * vb2_streamon - start streaming
  1448. * @q: videobuf2 queue
  1449. * @type: type argument passed from userspace to vidioc_streamon handler
  1450. *
  1451. * Should be called from vidioc_streamon handler of a driver.
  1452. * This function:
  1453. * 1) verifies current state
  1454. * 2) passes any previously queued buffers to the driver and starts streaming
  1455. *
  1456. * The return values from this function are intended to be directly returned
  1457. * from vidioc_streamon handler in the driver.
  1458. */
  1459. int vb2_streamon(struct vb2_queue *q, enum v4l2_buf_type type)
  1460. {
  1461. struct vb2_buffer *vb;
  1462. int ret;
  1463. if (q->fileio) {
  1464. dprintk(1, "streamon: file io in progress\n");
  1465. return -EBUSY;
  1466. }
  1467. if (type != q->type) {
  1468. dprintk(1, "streamon: invalid stream type\n");
  1469. return -EINVAL;
  1470. }
  1471. if (q->streaming) {
  1472. dprintk(1, "streamon: already streaming\n");
  1473. return -EBUSY;
  1474. }
  1475. /*
  1476. * If any buffers were queued before streamon,
  1477. * we can now pass them to driver for processing.
  1478. */
  1479. list_for_each_entry(vb, &q->queued_list, queued_entry)
  1480. __enqueue_in_driver(vb);
  1481. /*
  1482. * Let driver notice that streaming state has been enabled.
  1483. */
  1484. ret = call_qop(q, start_streaming, q, atomic_read(&q->queued_count));
  1485. if (ret) {
  1486. dprintk(1, "streamon: driver refused to start streaming\n");
  1487. __vb2_queue_cancel(q);
  1488. return ret;
  1489. }
  1490. q->streaming = 1;
  1491. dprintk(3, "Streamon successful\n");
  1492. return 0;
  1493. }
  1494. EXPORT_SYMBOL_GPL(vb2_streamon);
  1495. /**
  1496. * vb2_streamoff - stop streaming
  1497. * @q: videobuf2 queue
  1498. * @type: type argument passed from userspace to vidioc_streamoff handler
  1499. *
  1500. * Should be called from vidioc_streamoff handler of a driver.
  1501. * This function:
  1502. * 1) verifies current state,
  1503. * 2) stop streaming and dequeues any queued buffers, including those previously
  1504. * passed to the driver (after waiting for the driver to finish).
  1505. *
  1506. * This call can be used for pausing playback.
  1507. * The return values from this function are intended to be directly returned
  1508. * from vidioc_streamoff handler in the driver
  1509. */
  1510. int vb2_streamoff(struct vb2_queue *q, enum v4l2_buf_type type)
  1511. {
  1512. if (q->fileio) {
  1513. dprintk(1, "streamoff: file io in progress\n");
  1514. return -EBUSY;
  1515. }
  1516. if (type != q->type) {
  1517. dprintk(1, "streamoff: invalid stream type\n");
  1518. return -EINVAL;
  1519. }
  1520. if (!q->streaming) {
  1521. dprintk(1, "streamoff: not streaming\n");
  1522. return -EINVAL;
  1523. }
  1524. /*
  1525. * Cancel will pause streaming and remove all buffers from the driver
  1526. * and videobuf, effectively returning control over them to userspace.
  1527. */
  1528. __vb2_queue_cancel(q);
  1529. dprintk(3, "Streamoff successful\n");
  1530. return 0;
  1531. }
  1532. EXPORT_SYMBOL_GPL(vb2_streamoff);
  1533. /**
  1534. * __find_plane_by_offset() - find plane associated with the given offset off
  1535. */
  1536. static int __find_plane_by_offset(struct vb2_queue *q, unsigned long off,
  1537. unsigned int *_buffer, unsigned int *_plane)
  1538. {
  1539. struct vb2_buffer *vb;
  1540. unsigned int buffer, plane;
  1541. /*
  1542. * Go over all buffers and their planes, comparing the given offset
  1543. * with an offset assigned to each plane. If a match is found,
  1544. * return its buffer and plane numbers.
  1545. */
  1546. for (buffer = 0; buffer < q->num_buffers; ++buffer) {
  1547. vb = q->bufs[buffer];
  1548. for (plane = 0; plane < vb->num_planes; ++plane) {
  1549. if (vb->v4l2_planes[plane].m.mem_offset == off) {
  1550. *_buffer = buffer;
  1551. *_plane = plane;
  1552. return 0;
  1553. }
  1554. }
  1555. }
  1556. return -EINVAL;
  1557. }
  1558. /**
  1559. * vb2_expbuf() - Export a buffer as a file descriptor
  1560. * @q: videobuf2 queue
  1561. * @eb: export buffer structure passed from userspace to vidioc_expbuf
  1562. * handler in driver
  1563. *
  1564. * The return values from this function are intended to be directly returned
  1565. * from vidioc_expbuf handler in driver.
  1566. */
  1567. int vb2_expbuf(struct vb2_queue *q, struct v4l2_exportbuffer *eb)
  1568. {
  1569. struct vb2_buffer *vb = NULL;
  1570. struct vb2_plane *vb_plane;
  1571. int ret;
  1572. struct dma_buf *dbuf;
  1573. if (q->memory != V4L2_MEMORY_MMAP) {
  1574. dprintk(1, "Queue is not currently set up for mmap\n");
  1575. return -EINVAL;
  1576. }
  1577. if (!q->mem_ops->get_dmabuf) {
  1578. dprintk(1, "Queue does not support DMA buffer exporting\n");
  1579. return -EINVAL;
  1580. }
  1581. if (eb->flags & ~O_CLOEXEC) {
  1582. dprintk(1, "Queue does support only O_CLOEXEC flag\n");
  1583. return -EINVAL;
  1584. }
  1585. if (eb->type != q->type) {
  1586. dprintk(1, "qbuf: invalid buffer type\n");
  1587. return -EINVAL;
  1588. }
  1589. if (eb->index >= q->num_buffers) {
  1590. dprintk(1, "buffer index out of range\n");
  1591. return -EINVAL;
  1592. }
  1593. vb = q->bufs[eb->index];
  1594. if (eb->plane >= vb->num_planes) {
  1595. dprintk(1, "buffer plane out of range\n");
  1596. return -EINVAL;
  1597. }
  1598. vb_plane = &vb->planes[eb->plane];
  1599. dbuf = call_memop(q, get_dmabuf, vb_plane->mem_priv);
  1600. if (IS_ERR_OR_NULL(dbuf)) {
  1601. dprintk(1, "Failed to export buffer %d, plane %d\n",
  1602. eb->index, eb->plane);
  1603. return -EINVAL;
  1604. }
  1605. ret = dma_buf_fd(dbuf, eb->flags);
  1606. if (ret < 0) {
  1607. dprintk(3, "buffer %d, plane %d failed to export (%d)\n",
  1608. eb->index, eb->plane, ret);
  1609. dma_buf_put(dbuf);
  1610. return ret;
  1611. }
  1612. dprintk(3, "buffer %d, plane %d exported as %d descriptor\n",
  1613. eb->index, eb->plane, ret);
  1614. eb->fd = ret;
  1615. return 0;
  1616. }
  1617. EXPORT_SYMBOL_GPL(vb2_expbuf);
  1618. /**
  1619. * vb2_mmap() - map video buffers into application address space
  1620. * @q: videobuf2 queue
  1621. * @vma: vma passed to the mmap file operation handler in the driver
  1622. *
  1623. * Should be called from mmap file operation handler of a driver.
  1624. * This function maps one plane of one of the available video buffers to
  1625. * userspace. To map whole video memory allocated on reqbufs, this function
  1626. * has to be called once per each plane per each buffer previously allocated.
  1627. *
  1628. * When the userspace application calls mmap, it passes to it an offset returned
  1629. * to it earlier by the means of vidioc_querybuf handler. That offset acts as
  1630. * a "cookie", which is then used to identify the plane to be mapped.
  1631. * This function finds a plane with a matching offset and a mapping is performed
  1632. * by the means of a provided memory operation.
  1633. *
  1634. * The return values from this function are intended to be directly returned
  1635. * from the mmap handler in driver.
  1636. */
  1637. int vb2_mmap(struct vb2_queue *q, struct vm_area_struct *vma)
  1638. {
  1639. unsigned long off = vma->vm_pgoff << PAGE_SHIFT;
  1640. struct vb2_buffer *vb;
  1641. unsigned int buffer, plane;
  1642. int ret;
  1643. unsigned long length;
  1644. if (q->memory != V4L2_MEMORY_MMAP) {
  1645. dprintk(1, "Queue is not currently set up for mmap\n");
  1646. return -EINVAL;
  1647. }
  1648. /*
  1649. * Check memory area access mode.
  1650. */
  1651. if (!(vma->vm_flags & VM_SHARED)) {
  1652. dprintk(1, "Invalid vma flags, VM_SHARED needed\n");
  1653. return -EINVAL;
  1654. }
  1655. if (V4L2_TYPE_IS_OUTPUT(q->type)) {
  1656. if (!(vma->vm_flags & VM_WRITE)) {
  1657. dprintk(1, "Invalid vma flags, VM_WRITE needed\n");
  1658. return -EINVAL;
  1659. }
  1660. } else {
  1661. if (!(vma->vm_flags & VM_READ)) {
  1662. dprintk(1, "Invalid vma flags, VM_READ needed\n");
  1663. return -EINVAL;
  1664. }
  1665. }
  1666. /*
  1667. * Find the plane corresponding to the offset passed by userspace.
  1668. */
  1669. ret = __find_plane_by_offset(q, off, &buffer, &plane);
  1670. if (ret)
  1671. return ret;
  1672. vb = q->bufs[buffer];
  1673. /*
  1674. * MMAP requires page_aligned buffers.
  1675. * The buffer length was page_aligned at __vb2_buf_mem_alloc(),
  1676. * so, we need to do the same here.
  1677. */
  1678. length = PAGE_ALIGN(vb->v4l2_planes[plane].length);
  1679. if (length < (vma->vm_end - vma->vm_start)) {
  1680. dprintk(1,
  1681. "MMAP invalid, as it would overflow buffer length\n");
  1682. return -EINVAL;
  1683. }
  1684. ret = call_memop(q, mmap, vb->planes[plane].mem_priv, vma);
  1685. if (ret)
  1686. return ret;
  1687. dprintk(3, "Buffer %d, plane %d successfully mapped\n", buffer, plane);
  1688. return 0;
  1689. }
  1690. EXPORT_SYMBOL_GPL(vb2_mmap);
  1691. #ifndef CONFIG_MMU
  1692. unsigned long vb2_get_unmapped_area(struct vb2_queue *q,
  1693. unsigned long addr,
  1694. unsigned long len,
  1695. unsigned long pgoff,
  1696. unsigned long flags)
  1697. {
  1698. unsigned long off = pgoff << PAGE_SHIFT;
  1699. struct vb2_buffer *vb;
  1700. unsigned int buffer, plane;
  1701. int ret;
  1702. if (q->memory != V4L2_MEMORY_MMAP) {
  1703. dprintk(1, "Queue is not currently set up for mmap\n");
  1704. return -EINVAL;
  1705. }
  1706. /*
  1707. * Find the plane corresponding to the offset passed by userspace.
  1708. */
  1709. ret = __find_plane_by_offset(q, off, &buffer, &plane);
  1710. if (ret)
  1711. return ret;
  1712. vb = q->bufs[buffer];
  1713. return (unsigned long)vb2_plane_vaddr(vb, plane);
  1714. }
  1715. EXPORT_SYMBOL_GPL(vb2_get_unmapped_area);
  1716. #endif
  1717. static int __vb2_init_fileio(struct vb2_queue *q, int read);
  1718. static int __vb2_cleanup_fileio(struct vb2_queue *q);
  1719. /**
  1720. * vb2_poll() - implements poll userspace operation
  1721. * @q: videobuf2 queue
  1722. * @file: file argument passed to the poll file operation handler
  1723. * @wait: wait argument passed to the poll file operation handler
  1724. *
  1725. * This function implements poll file operation handler for a driver.
  1726. * For CAPTURE queues, if a buffer is ready to be dequeued, the userspace will
  1727. * be informed that the file descriptor of a video device is available for
  1728. * reading.
  1729. * For OUTPUT queues, if a buffer is ready to be dequeued, the file descriptor
  1730. * will be reported as available for writing.
  1731. *
  1732. * If the driver uses struct v4l2_fh, then vb2_poll() will also check for any
  1733. * pending events.
  1734. *
  1735. * The return values from this function are intended to be directly returned
  1736. * from poll handler in driver.
  1737. */
  1738. unsigned int vb2_poll(struct vb2_queue *q, struct file *file, poll_table *wait)
  1739. {
  1740. struct video_device *vfd = video_devdata(file);
  1741. unsigned long req_events = poll_requested_events(wait);
  1742. struct vb2_buffer *vb = NULL;
  1743. unsigned int res = 0;
  1744. unsigned long flags;
  1745. if (test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags)) {
  1746. struct v4l2_fh *fh = file->private_data;
  1747. if (v4l2_event_pending(fh))
  1748. res = POLLPRI;
  1749. else if (req_events & POLLPRI)
  1750. poll_wait(file, &fh->wait, wait);
  1751. }
  1752. if (!V4L2_TYPE_IS_OUTPUT(q->type) && !(req_events & (POLLIN | POLLRDNORM)))
  1753. return res;
  1754. if (V4L2_TYPE_IS_OUTPUT(q->type) && !(req_events & (POLLOUT | POLLWRNORM)))
  1755. return res;
  1756. /*
  1757. * Start file I/O emulator only if streaming API has not been used yet.
  1758. */
  1759. if (q->num_buffers == 0 && q->fileio == NULL) {
  1760. if (!V4L2_TYPE_IS_OUTPUT(q->type) && (q->io_modes & VB2_READ) &&
  1761. (req_events & (POLLIN | POLLRDNORM))) {
  1762. if (__vb2_init_fileio(q, 1))
  1763. return res | POLLERR;
  1764. }
  1765. if (V4L2_TYPE_IS_OUTPUT(q->type) && (q->io_modes & VB2_WRITE) &&
  1766. (req_events & (POLLOUT | POLLWRNORM))) {
  1767. if (__vb2_init_fileio(q, 0))
  1768. return res | POLLERR;
  1769. /*
  1770. * Write to OUTPUT queue can be done immediately.
  1771. */
  1772. return res | POLLOUT | POLLWRNORM;
  1773. }
  1774. }
  1775. /*
  1776. * There is nothing to wait for if no buffers have already been queued.
  1777. */
  1778. if (list_empty(&q->queued_list))
  1779. return res | POLLERR;
  1780. if (list_empty(&q->done_list))
  1781. poll_wait(file, &q->done_wq, wait);
  1782. /*
  1783. * Take first buffer available for dequeuing.
  1784. */
  1785. spin_lock_irqsave(&q->done_lock, flags);
  1786. if (!list_empty(&q->done_list))
  1787. vb = list_first_entry(&q->done_list, struct vb2_buffer,
  1788. done_entry);
  1789. spin_unlock_irqrestore(&q->done_lock, flags);
  1790. if (vb && (vb->state == VB2_BUF_STATE_DONE
  1791. || vb->state == VB2_BUF_STATE_ERROR)) {
  1792. return (V4L2_TYPE_IS_OUTPUT(q->type)) ?
  1793. res | POLLOUT | POLLWRNORM :
  1794. res | POLLIN | POLLRDNORM;
  1795. }
  1796. return res;
  1797. }
  1798. EXPORT_SYMBOL_GPL(vb2_poll);
  1799. /**
  1800. * vb2_queue_init() - initialize a videobuf2 queue
  1801. * @q: videobuf2 queue; this structure should be allocated in driver
  1802. *
  1803. * The vb2_queue structure should be allocated by the driver. The driver is
  1804. * responsible of clearing it's content and setting initial values for some
  1805. * required entries before calling this function.
  1806. * q->ops, q->mem_ops, q->type and q->io_modes are mandatory. Please refer
  1807. * to the struct vb2_queue description in include/media/videobuf2-core.h
  1808. * for more information.
  1809. */
  1810. int vb2_queue_init(struct vb2_queue *q)
  1811. {
  1812. /*
  1813. * Sanity check
  1814. */
  1815. if (WARN_ON(!q) ||
  1816. WARN_ON(!q->ops) ||
  1817. WARN_ON(!q->mem_ops) ||
  1818. WARN_ON(!q->type) ||
  1819. WARN_ON(!q->io_modes) ||
  1820. WARN_ON(!q->ops->queue_setup) ||
  1821. WARN_ON(!q->ops->buf_queue) ||
  1822. WARN_ON(q->timestamp_type & ~V4L2_BUF_FLAG_TIMESTAMP_MASK))
  1823. return -EINVAL;
  1824. /* Warn that the driver should choose an appropriate timestamp type */
  1825. WARN_ON(q->timestamp_type == V4L2_BUF_FLAG_TIMESTAMP_UNKNOWN);
  1826. INIT_LIST_HEAD(&q->queued_list);
  1827. INIT_LIST_HEAD(&q->done_list);
  1828. spin_lock_init(&q->done_lock);
  1829. init_waitqueue_head(&q->done_wq);
  1830. if (q->buf_struct_size == 0)
  1831. q->buf_struct_size = sizeof(struct vb2_buffer);
  1832. return 0;
  1833. }
  1834. EXPORT_SYMBOL_GPL(vb2_queue_init);
  1835. /**
  1836. * vb2_queue_release() - stop streaming, release the queue and free memory
  1837. * @q: videobuf2 queue
  1838. *
  1839. * This function stops streaming and performs necessary clean ups, including
  1840. * freeing video buffer memory. The driver is responsible for freeing
  1841. * the vb2_queue structure itself.
  1842. */
  1843. void vb2_queue_release(struct vb2_queue *q)
  1844. {
  1845. __vb2_cleanup_fileio(q);
  1846. __vb2_queue_cancel(q);
  1847. __vb2_queue_free(q, q->num_buffers);
  1848. }
  1849. EXPORT_SYMBOL_GPL(vb2_queue_release);
  1850. /**
  1851. * struct vb2_fileio_buf - buffer context used by file io emulator
  1852. *
  1853. * vb2 provides a compatibility layer and emulator of file io (read and
  1854. * write) calls on top of streaming API. This structure is used for
  1855. * tracking context related to the buffers.
  1856. */
  1857. struct vb2_fileio_buf {
  1858. void *vaddr;
  1859. unsigned int size;
  1860. unsigned int pos;
  1861. unsigned int queued:1;
  1862. };
  1863. /**
  1864. * struct vb2_fileio_data - queue context used by file io emulator
  1865. *
  1866. * vb2 provides a compatibility layer and emulator of file io (read and
  1867. * write) calls on top of streaming API. For proper operation it required
  1868. * this structure to save the driver state between each call of the read
  1869. * or write function.
  1870. */
  1871. struct vb2_fileio_data {
  1872. struct v4l2_requestbuffers req;
  1873. struct v4l2_buffer b;
  1874. struct vb2_fileio_buf bufs[VIDEO_MAX_FRAME];
  1875. unsigned int index;
  1876. unsigned int q_count;
  1877. unsigned int dq_count;
  1878. unsigned int flags;
  1879. };
  1880. /**
  1881. * __vb2_init_fileio() - initialize file io emulator
  1882. * @q: videobuf2 queue
  1883. * @read: mode selector (1 means read, 0 means write)
  1884. */
  1885. static int __vb2_init_fileio(struct vb2_queue *q, int read)
  1886. {
  1887. struct vb2_fileio_data *fileio;
  1888. int i, ret;
  1889. unsigned int count = 0;
  1890. /*
  1891. * Sanity check
  1892. */
  1893. if ((read && !(q->io_modes & VB2_READ)) ||
  1894. (!read && !(q->io_modes & VB2_WRITE)))
  1895. BUG();
  1896. /*
  1897. * Check if device supports mapping buffers to kernel virtual space.
  1898. */
  1899. if (!q->mem_ops->vaddr)
  1900. return -EBUSY;
  1901. /*
  1902. * Check if streaming api has not been already activated.
  1903. */
  1904. if (q->streaming || q->num_buffers > 0)
  1905. return -EBUSY;
  1906. /*
  1907. * Start with count 1, driver can increase it in queue_setup()
  1908. */
  1909. count = 1;
  1910. dprintk(3, "setting up file io: mode %s, count %d, flags %08x\n",
  1911. (read) ? "read" : "write", count, q->io_flags);
  1912. fileio = kzalloc(sizeof(struct vb2_fileio_data), GFP_KERNEL);
  1913. if (fileio == NULL)
  1914. return -ENOMEM;
  1915. fileio->flags = q->io_flags;
  1916. /*
  1917. * Request buffers and use MMAP type to force driver
  1918. * to allocate buffers by itself.
  1919. */
  1920. fileio->req.count = count;
  1921. fileio->req.memory = V4L2_MEMORY_MMAP;
  1922. fileio->req.type = q->type;
  1923. ret = vb2_reqbufs(q, &fileio->req);
  1924. if (ret)
  1925. goto err_kfree;
  1926. /*
  1927. * Check if plane_count is correct
  1928. * (multiplane buffers are not supported).
  1929. */
  1930. if (q->bufs[0]->num_planes != 1) {
  1931. ret = -EBUSY;
  1932. goto err_reqbufs;
  1933. }
  1934. /*
  1935. * Get kernel address of each buffer.
  1936. */
  1937. for (i = 0; i < q->num_buffers; i++) {
  1938. fileio->bufs[i].vaddr = vb2_plane_vaddr(q->bufs[i], 0);
  1939. if (fileio->bufs[i].vaddr == NULL) {
  1940. ret = -EINVAL;
  1941. goto err_reqbufs;
  1942. }
  1943. fileio->bufs[i].size = vb2_plane_size(q->bufs[i], 0);
  1944. }
  1945. /*
  1946. * Read mode requires pre queuing of all buffers.
  1947. */
  1948. if (read) {
  1949. /*
  1950. * Queue all buffers.
  1951. */
  1952. for (i = 0; i < q->num_buffers; i++) {
  1953. struct v4l2_buffer *b = &fileio->b;
  1954. memset(b, 0, sizeof(*b));
  1955. b->type = q->type;
  1956. b->memory = q->memory;
  1957. b->index = i;
  1958. ret = vb2_qbuf(q, b);
  1959. if (ret)
  1960. goto err_reqbufs;
  1961. fileio->bufs[i].queued = 1;
  1962. }
  1963. /*
  1964. * Start streaming.
  1965. */
  1966. ret = vb2_streamon(q, q->type);
  1967. if (ret)
  1968. goto err_reqbufs;
  1969. }
  1970. q->fileio = fileio;
  1971. return ret;
  1972. err_reqbufs:
  1973. fileio->req.count = 0;
  1974. vb2_reqbufs(q, &fileio->req);
  1975. err_kfree:
  1976. kfree(fileio);
  1977. return ret;
  1978. }
  1979. /**
  1980. * __vb2_cleanup_fileio() - free resourced used by file io emulator
  1981. * @q: videobuf2 queue
  1982. */
  1983. static int __vb2_cleanup_fileio(struct vb2_queue *q)
  1984. {
  1985. struct vb2_fileio_data *fileio = q->fileio;
  1986. if (fileio) {
  1987. /*
  1988. * Hack fileio context to enable direct calls to vb2 ioctl
  1989. * interface.
  1990. */
  1991. q->fileio = NULL;
  1992. vb2_streamoff(q, q->type);
  1993. fileio->req.count = 0;
  1994. vb2_reqbufs(q, &fileio->req);
  1995. kfree(fileio);
  1996. dprintk(3, "file io emulator closed\n");
  1997. }
  1998. return 0;
  1999. }
  2000. /**
  2001. * __vb2_perform_fileio() - perform a single file io (read or write) operation
  2002. * @q: videobuf2 queue
  2003. * @data: pointed to target userspace buffer
  2004. * @count: number of bytes to read or write
  2005. * @ppos: file handle position tracking pointer
  2006. * @nonblock: mode selector (1 means blocking calls, 0 means nonblocking)
  2007. * @read: access mode selector (1 means read, 0 means write)
  2008. */
  2009. static size_t __vb2_perform_fileio(struct vb2_queue *q, char __user *data, size_t count,
  2010. loff_t *ppos, int nonblock, int read)
  2011. {
  2012. struct vb2_fileio_data *fileio;
  2013. struct vb2_fileio_buf *buf;
  2014. int ret, index;
  2015. dprintk(3, "file io: mode %s, offset %ld, count %zd, %sblocking\n",
  2016. read ? "read" : "write", (long)*ppos, count,
  2017. nonblock ? "non" : "");
  2018. if (!data)
  2019. return -EINVAL;
  2020. /*
  2021. * Initialize emulator on first call.
  2022. */
  2023. if (!q->fileio) {
  2024. ret = __vb2_init_fileio(q, read);
  2025. dprintk(3, "file io: vb2_init_fileio result: %d\n", ret);
  2026. if (ret)
  2027. return ret;
  2028. }
  2029. fileio = q->fileio;
  2030. /*
  2031. * Hack fileio context to enable direct calls to vb2 ioctl interface.
  2032. * The pointer will be restored before returning from this function.
  2033. */
  2034. q->fileio = NULL;
  2035. index = fileio->index;
  2036. buf = &fileio->bufs[index];
  2037. /*
  2038. * Check if we need to dequeue the buffer.
  2039. */
  2040. if (buf->queued) {
  2041. struct vb2_buffer *vb;
  2042. /*
  2043. * Call vb2_dqbuf to get buffer back.
  2044. */
  2045. memset(&fileio->b, 0, sizeof(fileio->b));
  2046. fileio->b.type = q->type;
  2047. fileio->b.memory = q->memory;
  2048. fileio->b.index = index;
  2049. ret = vb2_dqbuf(q, &fileio->b, nonblock);
  2050. dprintk(5, "file io: vb2_dqbuf result: %d\n", ret);
  2051. if (ret)
  2052. goto end;
  2053. fileio->dq_count += 1;
  2054. /*
  2055. * Get number of bytes filled by the driver
  2056. */
  2057. vb = q->bufs[index];
  2058. buf->size = vb2_get_plane_payload(vb, 0);
  2059. buf->queued = 0;
  2060. }
  2061. /*
  2062. * Limit count on last few bytes of the buffer.
  2063. */
  2064. if (buf->pos + count > buf->size) {
  2065. count = buf->size - buf->pos;
  2066. dprintk(5, "reducing read count: %zd\n", count);
  2067. }
  2068. /*
  2069. * Transfer data to userspace.
  2070. */
  2071. dprintk(3, "file io: copying %zd bytes - buffer %d, offset %u\n",
  2072. count, index, buf->pos);
  2073. if (read)
  2074. ret = copy_to_user(data, buf->vaddr + buf->pos, count);
  2075. else
  2076. ret = copy_from_user(buf->vaddr + buf->pos, data, count);
  2077. if (ret) {
  2078. dprintk(3, "file io: error copying data\n");
  2079. ret = -EFAULT;
  2080. goto end;
  2081. }
  2082. /*
  2083. * Update counters.
  2084. */
  2085. buf->pos += count;
  2086. *ppos += count;
  2087. /*
  2088. * Queue next buffer if required.
  2089. */
  2090. if (buf->pos == buf->size ||
  2091. (!read && (fileio->flags & VB2_FILEIO_WRITE_IMMEDIATELY))) {
  2092. /*
  2093. * Check if this is the last buffer to read.
  2094. */
  2095. if (read && (fileio->flags & VB2_FILEIO_READ_ONCE) &&
  2096. fileio->dq_count == 1) {
  2097. dprintk(3, "file io: read limit reached\n");
  2098. /*
  2099. * Restore fileio pointer and release the context.
  2100. */
  2101. q->fileio = fileio;
  2102. return __vb2_cleanup_fileio(q);
  2103. }
  2104. /*
  2105. * Call vb2_qbuf and give buffer to the driver.
  2106. */
  2107. memset(&fileio->b, 0, sizeof(fileio->b));
  2108. fileio->b.type = q->type;
  2109. fileio->b.memory = q->memory;
  2110. fileio->b.index = index;
  2111. fileio->b.bytesused = buf->pos;
  2112. ret = vb2_qbuf(q, &fileio->b);
  2113. dprintk(5, "file io: vb2_dbuf result: %d\n", ret);
  2114. if (ret)
  2115. goto end;
  2116. /*
  2117. * Buffer has been queued, update the status
  2118. */
  2119. buf->pos = 0;
  2120. buf->queued = 1;
  2121. buf->size = q->bufs[0]->v4l2_planes[0].length;
  2122. fileio->q_count += 1;
  2123. /*
  2124. * Switch to the next buffer
  2125. */
  2126. fileio->index = (index + 1) % q->num_buffers;
  2127. /*
  2128. * Start streaming if required.
  2129. */
  2130. if (!read && !q->streaming) {
  2131. ret = vb2_streamon(q, q->type);
  2132. if (ret)
  2133. goto end;
  2134. }
  2135. }
  2136. /*
  2137. * Return proper number of bytes processed.
  2138. */
  2139. if (ret == 0)
  2140. ret = count;
  2141. end:
  2142. /*
  2143. * Restore the fileio context and block vb2 ioctl interface.
  2144. */
  2145. q->fileio = fileio;
  2146. return ret;
  2147. }
  2148. size_t vb2_read(struct vb2_queue *q, char __user *data, size_t count,
  2149. loff_t *ppos, int nonblocking)
  2150. {
  2151. return __vb2_perform_fileio(q, data, count, ppos, nonblocking, 1);
  2152. }
  2153. EXPORT_SYMBOL_GPL(vb2_read);
  2154. size_t vb2_write(struct vb2_queue *q, const char __user *data, size_t count,
  2155. loff_t *ppos, int nonblocking)
  2156. {
  2157. return __vb2_perform_fileio(q, (char __user *) data, count,
  2158. ppos, nonblocking, 0);
  2159. }
  2160. EXPORT_SYMBOL_GPL(vb2_write);
  2161. /*
  2162. * The following functions are not part of the vb2 core API, but are helper
  2163. * functions that plug into struct v4l2_ioctl_ops, struct v4l2_file_operations
  2164. * and struct vb2_ops.
  2165. * They contain boilerplate code that most if not all drivers have to do
  2166. * and so they simplify the driver code.
  2167. */
  2168. /* The queue is busy if there is a owner and you are not that owner. */
  2169. static inline bool vb2_queue_is_busy(struct video_device *vdev, struct file *file)
  2170. {
  2171. return vdev->queue->owner && vdev->queue->owner != file->private_data;
  2172. }
  2173. /* vb2 ioctl helpers */
  2174. int vb2_ioctl_reqbufs(struct file *file, void *priv,
  2175. struct v4l2_requestbuffers *p)
  2176. {
  2177. struct video_device *vdev = video_devdata(file);
  2178. int res = __verify_memory_type(vdev->queue, p->memory, p->type);
  2179. if (res)
  2180. return res;
  2181. if (vb2_queue_is_busy(vdev, file))
  2182. return -EBUSY;
  2183. res = __reqbufs(vdev->queue, p);
  2184. /* If count == 0, then the owner has released all buffers and he
  2185. is no longer owner of the queue. Otherwise we have a new owner. */
  2186. if (res == 0)
  2187. vdev->queue->owner = p->count ? file->private_data : NULL;
  2188. return res;
  2189. }
  2190. EXPORT_SYMBOL_GPL(vb2_ioctl_reqbufs);
  2191. int vb2_ioctl_create_bufs(struct file *file, void *priv,
  2192. struct v4l2_create_buffers *p)
  2193. {
  2194. struct video_device *vdev = video_devdata(file);
  2195. int res = __verify_memory_type(vdev->queue, p->memory, p->format.type);
  2196. p->index = vdev->queue->num_buffers;
  2197. /* If count == 0, then just check if memory and type are valid.
  2198. Any -EBUSY result from __verify_memory_type can be mapped to 0. */
  2199. if (p->count == 0)
  2200. return res != -EBUSY ? res : 0;
  2201. if (res)
  2202. return res;
  2203. if (vb2_queue_is_busy(vdev, file))
  2204. return -EBUSY;
  2205. res = __create_bufs(vdev->queue, p);
  2206. if (res == 0)
  2207. vdev->queue->owner = file->private_data;
  2208. return res;
  2209. }
  2210. EXPORT_SYMBOL_GPL(vb2_ioctl_create_bufs);
  2211. int vb2_ioctl_prepare_buf(struct file *file, void *priv,
  2212. struct v4l2_buffer *p)
  2213. {
  2214. struct video_device *vdev = video_devdata(file);
  2215. if (vb2_queue_is_busy(vdev, file))
  2216. return -EBUSY;
  2217. return vb2_prepare_buf(vdev->queue, p);
  2218. }
  2219. EXPORT_SYMBOL_GPL(vb2_ioctl_prepare_buf);
  2220. int vb2_ioctl_querybuf(struct file *file, void *priv, struct v4l2_buffer *p)
  2221. {
  2222. struct video_device *vdev = video_devdata(file);
  2223. /* No need to call vb2_queue_is_busy(), anyone can query buffers. */
  2224. return vb2_querybuf(vdev->queue, p);
  2225. }
  2226. EXPORT_SYMBOL_GPL(vb2_ioctl_querybuf);
  2227. int vb2_ioctl_qbuf(struct file *file, void *priv, struct v4l2_buffer *p)
  2228. {
  2229. struct video_device *vdev = video_devdata(file);
  2230. if (vb2_queue_is_busy(vdev, file))
  2231. return -EBUSY;
  2232. return vb2_qbuf(vdev->queue, p);
  2233. }
  2234. EXPORT_SYMBOL_GPL(vb2_ioctl_qbuf);
  2235. int vb2_ioctl_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
  2236. {
  2237. struct video_device *vdev = video_devdata(file);
  2238. if (vb2_queue_is_busy(vdev, file))
  2239. return -EBUSY;
  2240. return vb2_dqbuf(vdev->queue, p, file->f_flags & O_NONBLOCK);
  2241. }
  2242. EXPORT_SYMBOL_GPL(vb2_ioctl_dqbuf);
  2243. int vb2_ioctl_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
  2244. {
  2245. struct video_device *vdev = video_devdata(file);
  2246. if (vb2_queue_is_busy(vdev, file))
  2247. return -EBUSY;
  2248. return vb2_streamon(vdev->queue, i);
  2249. }
  2250. EXPORT_SYMBOL_GPL(vb2_ioctl_streamon);
  2251. int vb2_ioctl_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
  2252. {
  2253. struct video_device *vdev = video_devdata(file);
  2254. if (vb2_queue_is_busy(vdev, file))
  2255. return -EBUSY;
  2256. return vb2_streamoff(vdev->queue, i);
  2257. }
  2258. EXPORT_SYMBOL_GPL(vb2_ioctl_streamoff);
  2259. int vb2_ioctl_expbuf(struct file *file, void *priv, struct v4l2_exportbuffer *p)
  2260. {
  2261. struct video_device *vdev = video_devdata(file);
  2262. if (vb2_queue_is_busy(vdev, file))
  2263. return -EBUSY;
  2264. return vb2_expbuf(vdev->queue, p);
  2265. }
  2266. EXPORT_SYMBOL_GPL(vb2_ioctl_expbuf);
  2267. /* v4l2_file_operations helpers */
  2268. int vb2_fop_mmap(struct file *file, struct vm_area_struct *vma)
  2269. {
  2270. struct video_device *vdev = video_devdata(file);
  2271. struct mutex *lock = vdev->queue->lock ? vdev->queue->lock : vdev->lock;
  2272. int err;
  2273. if (lock && mutex_lock_interruptible(lock))
  2274. return -ERESTARTSYS;
  2275. err = vb2_mmap(vdev->queue, vma);
  2276. if (lock)
  2277. mutex_unlock(lock);
  2278. return err;
  2279. }
  2280. EXPORT_SYMBOL_GPL(vb2_fop_mmap);
  2281. int vb2_fop_release(struct file *file)
  2282. {
  2283. struct video_device *vdev = video_devdata(file);
  2284. if (file->private_data == vdev->queue->owner) {
  2285. vb2_queue_release(vdev->queue);
  2286. vdev->queue->owner = NULL;
  2287. }
  2288. return v4l2_fh_release(file);
  2289. }
  2290. EXPORT_SYMBOL_GPL(vb2_fop_release);
  2291. ssize_t vb2_fop_write(struct file *file, const char __user *buf,
  2292. size_t count, loff_t *ppos)
  2293. {
  2294. struct video_device *vdev = video_devdata(file);
  2295. struct mutex *lock = vdev->queue->lock ? vdev->queue->lock : vdev->lock;
  2296. int err = -EBUSY;
  2297. if (lock && mutex_lock_interruptible(lock))
  2298. return -ERESTARTSYS;
  2299. if (vb2_queue_is_busy(vdev, file))
  2300. goto exit;
  2301. err = vb2_write(vdev->queue, buf, count, ppos,
  2302. file->f_flags & O_NONBLOCK);
  2303. if (vdev->queue->fileio)
  2304. vdev->queue->owner = file->private_data;
  2305. exit:
  2306. if (lock)
  2307. mutex_unlock(lock);
  2308. return err;
  2309. }
  2310. EXPORT_SYMBOL_GPL(vb2_fop_write);
  2311. ssize_t vb2_fop_read(struct file *file, char __user *buf,
  2312. size_t count, loff_t *ppos)
  2313. {
  2314. struct video_device *vdev = video_devdata(file);
  2315. struct mutex *lock = vdev->queue->lock ? vdev->queue->lock : vdev->lock;
  2316. int err = -EBUSY;
  2317. if (lock && mutex_lock_interruptible(lock))
  2318. return -ERESTARTSYS;
  2319. if (vb2_queue_is_busy(vdev, file))
  2320. goto exit;
  2321. err = vb2_read(vdev->queue, buf, count, ppos,
  2322. file->f_flags & O_NONBLOCK);
  2323. if (vdev->queue->fileio)
  2324. vdev->queue->owner = file->private_data;
  2325. exit:
  2326. if (lock)
  2327. mutex_unlock(lock);
  2328. return err;
  2329. }
  2330. EXPORT_SYMBOL_GPL(vb2_fop_read);
  2331. unsigned int vb2_fop_poll(struct file *file, poll_table *wait)
  2332. {
  2333. struct video_device *vdev = video_devdata(file);
  2334. struct vb2_queue *q = vdev->queue;
  2335. struct mutex *lock = q->lock ? q->lock : vdev->lock;
  2336. unsigned long req_events = poll_requested_events(wait);
  2337. unsigned res;
  2338. void *fileio;
  2339. bool must_lock = false;
  2340. /* Try to be smart: only lock if polling might start fileio,
  2341. otherwise locking will only introduce unwanted delays. */
  2342. if (q->num_buffers == 0 && q->fileio == NULL) {
  2343. if (!V4L2_TYPE_IS_OUTPUT(q->type) && (q->io_modes & VB2_READ) &&
  2344. (req_events & (POLLIN | POLLRDNORM)))
  2345. must_lock = true;
  2346. else if (V4L2_TYPE_IS_OUTPUT(q->type) && (q->io_modes & VB2_WRITE) &&
  2347. (req_events & (POLLOUT | POLLWRNORM)))
  2348. must_lock = true;
  2349. }
  2350. /* If locking is needed, but this helper doesn't know how, then you
  2351. shouldn't be using this helper but you should write your own. */
  2352. WARN_ON(must_lock && !lock);
  2353. if (must_lock && lock && mutex_lock_interruptible(lock))
  2354. return POLLERR;
  2355. fileio = q->fileio;
  2356. res = vb2_poll(vdev->queue, file, wait);
  2357. /* If fileio was started, then we have a new queue owner. */
  2358. if (must_lock && !fileio && q->fileio)
  2359. q->owner = file->private_data;
  2360. if (must_lock && lock)
  2361. mutex_unlock(lock);
  2362. return res;
  2363. }
  2364. EXPORT_SYMBOL_GPL(vb2_fop_poll);
  2365. #ifndef CONFIG_MMU
  2366. unsigned long vb2_fop_get_unmapped_area(struct file *file, unsigned long addr,
  2367. unsigned long len, unsigned long pgoff, unsigned long flags)
  2368. {
  2369. struct video_device *vdev = video_devdata(file);
  2370. struct mutex *lock = vdev->queue->lock ? vdev->queue->lock : vdev->lock;
  2371. int ret;
  2372. if (lock && mutex_lock_interruptible(lock))
  2373. return -ERESTARTSYS;
  2374. ret = vb2_get_unmapped_area(vdev->queue, addr, len, pgoff, flags);
  2375. if (lock)
  2376. mutex_unlock(lock);
  2377. return ret;
  2378. }
  2379. EXPORT_SYMBOL_GPL(vb2_fop_get_unmapped_area);
  2380. #endif
  2381. /* vb2_ops helpers. Only use if vq->lock is non-NULL. */
  2382. void vb2_ops_wait_prepare(struct vb2_queue *vq)
  2383. {
  2384. mutex_unlock(vq->lock);
  2385. }
  2386. EXPORT_SYMBOL_GPL(vb2_ops_wait_prepare);
  2387. void vb2_ops_wait_finish(struct vb2_queue *vq)
  2388. {
  2389. mutex_lock(vq->lock);
  2390. }
  2391. EXPORT_SYMBOL_GPL(vb2_ops_wait_finish);
  2392. MODULE_DESCRIPTION("Driver helper framework for Video for Linux 2");
  2393. MODULE_AUTHOR("Pawel Osciak <pawel@osciak.com>, Marek Szyprowski");
  2394. MODULE_LICENSE("GPL");