videobuf2-core.c 72 KB

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