videobuf2-core.c 73 KB

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