videobuf2-core.c 96 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521
  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. * The vb2_thread implementation was based on code from videobuf-dvb.c:
  10. * (c) 2004 Gerd Knorr <kraxel@bytesex.org> [SUSE Labs]
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation.
  15. */
  16. #include <linux/err.h>
  17. #include <linux/kernel.h>
  18. #include <linux/module.h>
  19. #include <linux/mm.h>
  20. #include <linux/poll.h>
  21. #include <linux/slab.h>
  22. #include <linux/sched.h>
  23. #include <linux/freezer.h>
  24. #include <linux/kthread.h>
  25. #include <media/v4l2-dev.h>
  26. #include <media/v4l2-fh.h>
  27. #include <media/v4l2-event.h>
  28. #include <media/v4l2-common.h>
  29. #include <media/videobuf2-core.h>
  30. static int debug;
  31. module_param(debug, int, 0644);
  32. #define dprintk(level, fmt, arg...) \
  33. do { \
  34. if (debug >= level) \
  35. pr_info("vb2: %s: " fmt, __func__, ## arg); \
  36. } while (0)
  37. #ifdef CONFIG_VIDEO_ADV_DEBUG
  38. /*
  39. * If advanced debugging is on, then count how often each op is called
  40. * successfully, which can either be per-buffer or per-queue.
  41. *
  42. * This makes it easy to check that the 'init' and 'cleanup'
  43. * (and variations thereof) stay balanced.
  44. */
  45. #define log_memop(vb, op) \
  46. dprintk(2, "call_memop(%p, %d, %s)%s\n", \
  47. (vb)->vb2_queue, (vb)->v4l2_buf.index, #op, \
  48. (vb)->vb2_queue->mem_ops->op ? "" : " (nop)")
  49. #define call_memop(vb, op, args...) \
  50. ({ \
  51. struct vb2_queue *_q = (vb)->vb2_queue; \
  52. int err; \
  53. \
  54. log_memop(vb, op); \
  55. err = _q->mem_ops->op ? _q->mem_ops->op(args) : 0; \
  56. if (!err) \
  57. (vb)->cnt_mem_ ## op++; \
  58. err; \
  59. })
  60. #define call_ptr_memop(vb, op, args...) \
  61. ({ \
  62. struct vb2_queue *_q = (vb)->vb2_queue; \
  63. void *ptr; \
  64. \
  65. log_memop(vb, op); \
  66. ptr = _q->mem_ops->op ? _q->mem_ops->op(args) : NULL; \
  67. if (!IS_ERR_OR_NULL(ptr)) \
  68. (vb)->cnt_mem_ ## op++; \
  69. ptr; \
  70. })
  71. #define call_void_memop(vb, op, args...) \
  72. ({ \
  73. struct vb2_queue *_q = (vb)->vb2_queue; \
  74. \
  75. log_memop(vb, op); \
  76. if (_q->mem_ops->op) \
  77. _q->mem_ops->op(args); \
  78. (vb)->cnt_mem_ ## op++; \
  79. })
  80. #define log_qop(q, op) \
  81. dprintk(2, "call_qop(%p, %s)%s\n", q, #op, \
  82. (q)->ops->op ? "" : " (nop)")
  83. #define call_qop(q, op, args...) \
  84. ({ \
  85. int err; \
  86. \
  87. log_qop(q, op); \
  88. err = (q)->ops->op ? (q)->ops->op(args) : 0; \
  89. if (!err) \
  90. (q)->cnt_ ## op++; \
  91. err; \
  92. })
  93. #define call_void_qop(q, op, args...) \
  94. ({ \
  95. log_qop(q, op); \
  96. if ((q)->ops->op) \
  97. (q)->ops->op(args); \
  98. (q)->cnt_ ## op++; \
  99. })
  100. #define log_vb_qop(vb, op, args...) \
  101. dprintk(2, "call_vb_qop(%p, %d, %s)%s\n", \
  102. (vb)->vb2_queue, (vb)->v4l2_buf.index, #op, \
  103. (vb)->vb2_queue->ops->op ? "" : " (nop)")
  104. #define call_vb_qop(vb, op, args...) \
  105. ({ \
  106. int err; \
  107. \
  108. log_vb_qop(vb, op); \
  109. err = (vb)->vb2_queue->ops->op ? \
  110. (vb)->vb2_queue->ops->op(args) : 0; \
  111. if (!err) \
  112. (vb)->cnt_ ## op++; \
  113. err; \
  114. })
  115. #define call_void_vb_qop(vb, op, args...) \
  116. ({ \
  117. log_vb_qop(vb, op); \
  118. if ((vb)->vb2_queue->ops->op) \
  119. (vb)->vb2_queue->ops->op(args); \
  120. (vb)->cnt_ ## op++; \
  121. })
  122. #else
  123. #define call_memop(vb, op, args...) \
  124. ((vb)->vb2_queue->mem_ops->op ? \
  125. (vb)->vb2_queue->mem_ops->op(args) : 0)
  126. #define call_ptr_memop(vb, op, args...) \
  127. ((vb)->vb2_queue->mem_ops->op ? \
  128. (vb)->vb2_queue->mem_ops->op(args) : NULL)
  129. #define call_void_memop(vb, op, args...) \
  130. do { \
  131. if ((vb)->vb2_queue->mem_ops->op) \
  132. (vb)->vb2_queue->mem_ops->op(args); \
  133. } while (0)
  134. #define call_qop(q, op, args...) \
  135. ((q)->ops->op ? (q)->ops->op(args) : 0)
  136. #define call_void_qop(q, op, args...) \
  137. do { \
  138. if ((q)->ops->op) \
  139. (q)->ops->op(args); \
  140. } while (0)
  141. #define call_vb_qop(vb, op, args...) \
  142. ((vb)->vb2_queue->ops->op ? (vb)->vb2_queue->ops->op(args) : 0)
  143. #define call_void_vb_qop(vb, op, args...) \
  144. do { \
  145. if ((vb)->vb2_queue->ops->op) \
  146. (vb)->vb2_queue->ops->op(args); \
  147. } while (0)
  148. #endif
  149. /* Flags that are set by the vb2 core */
  150. #define V4L2_BUFFER_MASK_FLAGS (V4L2_BUF_FLAG_MAPPED | V4L2_BUF_FLAG_QUEUED | \
  151. V4L2_BUF_FLAG_DONE | V4L2_BUF_FLAG_ERROR | \
  152. V4L2_BUF_FLAG_PREPARED | \
  153. V4L2_BUF_FLAG_TIMESTAMP_MASK)
  154. /* Output buffer flags that should be passed on to the driver */
  155. #define V4L2_BUFFER_OUT_FLAGS (V4L2_BUF_FLAG_PFRAME | V4L2_BUF_FLAG_BFRAME | \
  156. V4L2_BUF_FLAG_KEYFRAME | V4L2_BUF_FLAG_TIMECODE)
  157. static void __vb2_queue_cancel(struct vb2_queue *q);
  158. /**
  159. * __vb2_buf_mem_alloc() - allocate video memory for the given buffer
  160. */
  161. static int __vb2_buf_mem_alloc(struct vb2_buffer *vb)
  162. {
  163. struct vb2_queue *q = vb->vb2_queue;
  164. void *mem_priv;
  165. int plane;
  166. /*
  167. * Allocate memory for all planes in this buffer
  168. * NOTE: mmapped areas should be page aligned
  169. */
  170. for (plane = 0; plane < vb->num_planes; ++plane) {
  171. unsigned long size = PAGE_ALIGN(q->plane_sizes[plane]);
  172. mem_priv = call_ptr_memop(vb, alloc, q->alloc_ctx[plane],
  173. size, q->gfp_flags);
  174. if (IS_ERR_OR_NULL(mem_priv))
  175. goto free;
  176. /* Associate allocator private data with this plane */
  177. vb->planes[plane].mem_priv = mem_priv;
  178. vb->v4l2_planes[plane].length = q->plane_sizes[plane];
  179. }
  180. return 0;
  181. free:
  182. /* Free already allocated memory if one of the allocations failed */
  183. for (; plane > 0; --plane) {
  184. call_void_memop(vb, put, vb->planes[plane - 1].mem_priv);
  185. vb->planes[plane - 1].mem_priv = NULL;
  186. }
  187. return -ENOMEM;
  188. }
  189. /**
  190. * __vb2_buf_mem_free() - free memory of the given buffer
  191. */
  192. static void __vb2_buf_mem_free(struct vb2_buffer *vb)
  193. {
  194. unsigned int plane;
  195. for (plane = 0; plane < vb->num_planes; ++plane) {
  196. call_void_memop(vb, put, vb->planes[plane].mem_priv);
  197. vb->planes[plane].mem_priv = NULL;
  198. dprintk(3, "freed plane %d of buffer %d\n", plane,
  199. vb->v4l2_buf.index);
  200. }
  201. }
  202. /**
  203. * __vb2_buf_userptr_put() - release userspace memory associated with
  204. * a USERPTR buffer
  205. */
  206. static void __vb2_buf_userptr_put(struct vb2_buffer *vb)
  207. {
  208. unsigned int plane;
  209. for (plane = 0; plane < vb->num_planes; ++plane) {
  210. if (vb->planes[plane].mem_priv)
  211. call_void_memop(vb, put_userptr, vb->planes[plane].mem_priv);
  212. vb->planes[plane].mem_priv = NULL;
  213. }
  214. }
  215. /**
  216. * __vb2_plane_dmabuf_put() - release memory associated with
  217. * a DMABUF shared plane
  218. */
  219. static void __vb2_plane_dmabuf_put(struct vb2_buffer *vb, struct vb2_plane *p)
  220. {
  221. if (!p->mem_priv)
  222. return;
  223. if (p->dbuf_mapped)
  224. call_void_memop(vb, unmap_dmabuf, p->mem_priv);
  225. call_void_memop(vb, detach_dmabuf, p->mem_priv);
  226. dma_buf_put(p->dbuf);
  227. memset(p, 0, sizeof(*p));
  228. }
  229. /**
  230. * __vb2_buf_dmabuf_put() - release memory associated with
  231. * a DMABUF shared buffer
  232. */
  233. static void __vb2_buf_dmabuf_put(struct vb2_buffer *vb)
  234. {
  235. unsigned int plane;
  236. for (plane = 0; plane < vb->num_planes; ++plane)
  237. __vb2_plane_dmabuf_put(vb, &vb->planes[plane]);
  238. }
  239. /**
  240. * __setup_lengths() - setup initial lengths for every plane in
  241. * every buffer on the queue
  242. */
  243. static void __setup_lengths(struct vb2_queue *q, unsigned int n)
  244. {
  245. unsigned int buffer, plane;
  246. struct vb2_buffer *vb;
  247. for (buffer = q->num_buffers; buffer < q->num_buffers + n; ++buffer) {
  248. vb = q->bufs[buffer];
  249. if (!vb)
  250. continue;
  251. for (plane = 0; plane < vb->num_planes; ++plane)
  252. vb->v4l2_planes[plane].length = q->plane_sizes[plane];
  253. }
  254. }
  255. /**
  256. * __setup_offsets() - setup unique offsets ("cookies") for every plane in
  257. * every buffer on the queue
  258. */
  259. static void __setup_offsets(struct vb2_queue *q, unsigned int n)
  260. {
  261. unsigned int buffer, plane;
  262. struct vb2_buffer *vb;
  263. unsigned long off;
  264. if (q->num_buffers) {
  265. struct v4l2_plane *p;
  266. vb = q->bufs[q->num_buffers - 1];
  267. p = &vb->v4l2_planes[vb->num_planes - 1];
  268. off = PAGE_ALIGN(p->m.mem_offset + p->length);
  269. } else {
  270. off = 0;
  271. }
  272. for (buffer = q->num_buffers; buffer < q->num_buffers + n; ++buffer) {
  273. vb = q->bufs[buffer];
  274. if (!vb)
  275. continue;
  276. for (plane = 0; plane < vb->num_planes; ++plane) {
  277. vb->v4l2_planes[plane].m.mem_offset = off;
  278. dprintk(3, "buffer %d, plane %d offset 0x%08lx\n",
  279. buffer, plane, off);
  280. off += vb->v4l2_planes[plane].length;
  281. off = PAGE_ALIGN(off);
  282. }
  283. }
  284. }
  285. /**
  286. * __vb2_queue_alloc() - allocate videobuf buffer structures and (for MMAP type)
  287. * video buffer memory for all buffers/planes on the queue and initializes the
  288. * queue
  289. *
  290. * Returns the number of buffers successfully allocated.
  291. */
  292. static int __vb2_queue_alloc(struct vb2_queue *q, enum v4l2_memory memory,
  293. unsigned int num_buffers, unsigned int num_planes)
  294. {
  295. unsigned int buffer;
  296. struct vb2_buffer *vb;
  297. int ret;
  298. for (buffer = 0; buffer < num_buffers; ++buffer) {
  299. /* Allocate videobuf buffer structures */
  300. vb = kzalloc(q->buf_struct_size, GFP_KERNEL);
  301. if (!vb) {
  302. dprintk(1, "memory alloc for buffer struct failed\n");
  303. break;
  304. }
  305. /* Length stores number of planes for multiplanar buffers */
  306. if (V4L2_TYPE_IS_MULTIPLANAR(q->type))
  307. vb->v4l2_buf.length = num_planes;
  308. vb->state = VB2_BUF_STATE_DEQUEUED;
  309. vb->vb2_queue = q;
  310. vb->num_planes = num_planes;
  311. vb->v4l2_buf.index = q->num_buffers + buffer;
  312. vb->v4l2_buf.type = q->type;
  313. vb->v4l2_buf.memory = memory;
  314. /* Allocate video buffer memory for the MMAP type */
  315. if (memory == V4L2_MEMORY_MMAP) {
  316. ret = __vb2_buf_mem_alloc(vb);
  317. if (ret) {
  318. dprintk(1, "failed allocating memory for "
  319. "buffer %d\n", buffer);
  320. kfree(vb);
  321. break;
  322. }
  323. /*
  324. * Call the driver-provided buffer initialization
  325. * callback, if given. An error in initialization
  326. * results in queue setup failure.
  327. */
  328. ret = call_vb_qop(vb, buf_init, vb);
  329. if (ret) {
  330. dprintk(1, "buffer %d %p initialization"
  331. " failed\n", buffer, vb);
  332. __vb2_buf_mem_free(vb);
  333. kfree(vb);
  334. break;
  335. }
  336. }
  337. q->bufs[q->num_buffers + buffer] = vb;
  338. }
  339. __setup_lengths(q, buffer);
  340. if (memory == V4L2_MEMORY_MMAP)
  341. __setup_offsets(q, buffer);
  342. dprintk(1, "allocated %d buffers, %d plane(s) each\n",
  343. buffer, num_planes);
  344. return buffer;
  345. }
  346. /**
  347. * __vb2_free_mem() - release all video buffer memory for a given queue
  348. */
  349. static void __vb2_free_mem(struct vb2_queue *q, unsigned int buffers)
  350. {
  351. unsigned int buffer;
  352. struct vb2_buffer *vb;
  353. for (buffer = q->num_buffers - buffers; buffer < q->num_buffers;
  354. ++buffer) {
  355. vb = q->bufs[buffer];
  356. if (!vb)
  357. continue;
  358. /* Free MMAP buffers or release USERPTR buffers */
  359. if (q->memory == V4L2_MEMORY_MMAP)
  360. __vb2_buf_mem_free(vb);
  361. else if (q->memory == V4L2_MEMORY_DMABUF)
  362. __vb2_buf_dmabuf_put(vb);
  363. else
  364. __vb2_buf_userptr_put(vb);
  365. }
  366. }
  367. /**
  368. * __vb2_queue_free() - free buffers at the end of the queue - video memory and
  369. * related information, if no buffers are left return the queue to an
  370. * uninitialized state. Might be called even if the queue has already been freed.
  371. */
  372. static int __vb2_queue_free(struct vb2_queue *q, unsigned int buffers)
  373. {
  374. unsigned int buffer;
  375. /*
  376. * Sanity check: when preparing a buffer the queue lock is released for
  377. * a short while (see __buf_prepare for the details), which would allow
  378. * a race with a reqbufs which can call this function. Removing the
  379. * buffers from underneath __buf_prepare is obviously a bad idea, so we
  380. * check if any of the buffers is in the state PREPARING, and if so we
  381. * just return -EAGAIN.
  382. */
  383. for (buffer = q->num_buffers - buffers; buffer < q->num_buffers;
  384. ++buffer) {
  385. if (q->bufs[buffer] == NULL)
  386. continue;
  387. if (q->bufs[buffer]->state == VB2_BUF_STATE_PREPARING) {
  388. dprintk(1, "preparing buffers, cannot free\n");
  389. return -EAGAIN;
  390. }
  391. }
  392. /* Call driver-provided cleanup function for each buffer, if provided */
  393. for (buffer = q->num_buffers - buffers; buffer < q->num_buffers;
  394. ++buffer) {
  395. struct vb2_buffer *vb = q->bufs[buffer];
  396. if (vb && vb->planes[0].mem_priv)
  397. call_void_vb_qop(vb, buf_cleanup, vb);
  398. }
  399. /* Release video buffer memory */
  400. __vb2_free_mem(q, buffers);
  401. #ifdef CONFIG_VIDEO_ADV_DEBUG
  402. /*
  403. * Check that all the calls were balances during the life-time of this
  404. * queue. If not (or if the debug level is 1 or up), then dump the
  405. * counters to the kernel log.
  406. */
  407. if (q->num_buffers) {
  408. bool unbalanced = q->cnt_start_streaming != q->cnt_stop_streaming ||
  409. q->cnt_wait_prepare != q->cnt_wait_finish;
  410. if (unbalanced || debug) {
  411. pr_info("vb2: counters for queue %p:%s\n", q,
  412. unbalanced ? " UNBALANCED!" : "");
  413. pr_info("vb2: setup: %u start_streaming: %u stop_streaming: %u\n",
  414. q->cnt_queue_setup, q->cnt_start_streaming,
  415. q->cnt_stop_streaming);
  416. pr_info("vb2: wait_prepare: %u wait_finish: %u\n",
  417. q->cnt_wait_prepare, q->cnt_wait_finish);
  418. }
  419. q->cnt_queue_setup = 0;
  420. q->cnt_wait_prepare = 0;
  421. q->cnt_wait_finish = 0;
  422. q->cnt_start_streaming = 0;
  423. q->cnt_stop_streaming = 0;
  424. }
  425. for (buffer = 0; buffer < q->num_buffers; ++buffer) {
  426. struct vb2_buffer *vb = q->bufs[buffer];
  427. bool unbalanced = vb->cnt_mem_alloc != vb->cnt_mem_put ||
  428. vb->cnt_mem_prepare != vb->cnt_mem_finish ||
  429. vb->cnt_mem_get_userptr != vb->cnt_mem_put_userptr ||
  430. vb->cnt_mem_attach_dmabuf != vb->cnt_mem_detach_dmabuf ||
  431. vb->cnt_mem_map_dmabuf != vb->cnt_mem_unmap_dmabuf ||
  432. vb->cnt_buf_queue != vb->cnt_buf_done ||
  433. vb->cnt_buf_prepare != vb->cnt_buf_finish ||
  434. vb->cnt_buf_init != vb->cnt_buf_cleanup;
  435. if (unbalanced || debug) {
  436. pr_info("vb2: counters for queue %p, buffer %d:%s\n",
  437. q, buffer, unbalanced ? " UNBALANCED!" : "");
  438. pr_info("vb2: buf_init: %u buf_cleanup: %u buf_prepare: %u buf_finish: %u\n",
  439. vb->cnt_buf_init, vb->cnt_buf_cleanup,
  440. vb->cnt_buf_prepare, vb->cnt_buf_finish);
  441. pr_info("vb2: buf_queue: %u buf_done: %u\n",
  442. vb->cnt_buf_queue, vb->cnt_buf_done);
  443. pr_info("vb2: alloc: %u put: %u prepare: %u finish: %u mmap: %u\n",
  444. vb->cnt_mem_alloc, vb->cnt_mem_put,
  445. vb->cnt_mem_prepare, vb->cnt_mem_finish,
  446. vb->cnt_mem_mmap);
  447. pr_info("vb2: get_userptr: %u put_userptr: %u\n",
  448. vb->cnt_mem_get_userptr, vb->cnt_mem_put_userptr);
  449. pr_info("vb2: attach_dmabuf: %u detach_dmabuf: %u map_dmabuf: %u unmap_dmabuf: %u\n",
  450. vb->cnt_mem_attach_dmabuf, vb->cnt_mem_detach_dmabuf,
  451. vb->cnt_mem_map_dmabuf, vb->cnt_mem_unmap_dmabuf);
  452. pr_info("vb2: get_dmabuf: %u num_users: %u vaddr: %u cookie: %u\n",
  453. vb->cnt_mem_get_dmabuf,
  454. vb->cnt_mem_num_users,
  455. vb->cnt_mem_vaddr,
  456. vb->cnt_mem_cookie);
  457. }
  458. }
  459. #endif
  460. /* Free videobuf buffers */
  461. for (buffer = q->num_buffers - buffers; buffer < q->num_buffers;
  462. ++buffer) {
  463. kfree(q->bufs[buffer]);
  464. q->bufs[buffer] = NULL;
  465. }
  466. q->num_buffers -= buffers;
  467. if (!q->num_buffers) {
  468. q->memory = 0;
  469. INIT_LIST_HEAD(&q->queued_list);
  470. }
  471. return 0;
  472. }
  473. /**
  474. * __verify_planes_array() - verify that the planes array passed in struct
  475. * v4l2_buffer from userspace can be safely used
  476. */
  477. static int __verify_planes_array(struct vb2_buffer *vb, const struct v4l2_buffer *b)
  478. {
  479. if (!V4L2_TYPE_IS_MULTIPLANAR(b->type))
  480. return 0;
  481. /* Is memory for copying plane information present? */
  482. if (NULL == b->m.planes) {
  483. dprintk(1, "multi-planar buffer passed but "
  484. "planes array not provided\n");
  485. return -EINVAL;
  486. }
  487. if (b->length < vb->num_planes || b->length > VIDEO_MAX_PLANES) {
  488. dprintk(1, "incorrect planes array length, "
  489. "expected %d, got %d\n", vb->num_planes, b->length);
  490. return -EINVAL;
  491. }
  492. return 0;
  493. }
  494. /**
  495. * __verify_length() - Verify that the bytesused value for each plane fits in
  496. * the plane length and that the data offset doesn't exceed the bytesused value.
  497. */
  498. static int __verify_length(struct vb2_buffer *vb, const struct v4l2_buffer *b)
  499. {
  500. unsigned int length;
  501. unsigned int bytesused;
  502. unsigned int plane;
  503. if (!V4L2_TYPE_IS_OUTPUT(b->type))
  504. return 0;
  505. if (V4L2_TYPE_IS_MULTIPLANAR(b->type)) {
  506. for (plane = 0; plane < vb->num_planes; ++plane) {
  507. length = (b->memory == V4L2_MEMORY_USERPTR ||
  508. b->memory == V4L2_MEMORY_DMABUF)
  509. ? b->m.planes[plane].length
  510. : vb->v4l2_planes[plane].length;
  511. bytesused = b->m.planes[plane].bytesused
  512. ? b->m.planes[plane].bytesused : length;
  513. if (b->m.planes[plane].bytesused > length)
  514. return -EINVAL;
  515. if (b->m.planes[plane].data_offset > 0 &&
  516. b->m.planes[plane].data_offset >= bytesused)
  517. return -EINVAL;
  518. }
  519. } else {
  520. length = (b->memory == V4L2_MEMORY_USERPTR)
  521. ? b->length : vb->v4l2_planes[0].length;
  522. bytesused = b->bytesused ? b->bytesused : length;
  523. if (b->bytesused > length)
  524. return -EINVAL;
  525. }
  526. return 0;
  527. }
  528. /**
  529. * __buffer_in_use() - return true if the buffer is in use and
  530. * the queue cannot be freed (by the means of REQBUFS(0)) call
  531. */
  532. static bool __buffer_in_use(struct vb2_queue *q, struct vb2_buffer *vb)
  533. {
  534. unsigned int plane;
  535. for (plane = 0; plane < vb->num_planes; ++plane) {
  536. void *mem_priv = vb->planes[plane].mem_priv;
  537. /*
  538. * If num_users() has not been provided, call_memop
  539. * will return 0, apparently nobody cares about this
  540. * case anyway. If num_users() returns more than 1,
  541. * we are not the only user of the plane's memory.
  542. */
  543. if (mem_priv && call_memop(vb, num_users, mem_priv) > 1)
  544. return true;
  545. }
  546. return false;
  547. }
  548. /**
  549. * __buffers_in_use() - return true if any buffers on the queue are in use and
  550. * the queue cannot be freed (by the means of REQBUFS(0)) call
  551. */
  552. static bool __buffers_in_use(struct vb2_queue *q)
  553. {
  554. unsigned int buffer;
  555. for (buffer = 0; buffer < q->num_buffers; ++buffer) {
  556. if (__buffer_in_use(q, q->bufs[buffer]))
  557. return true;
  558. }
  559. return false;
  560. }
  561. /**
  562. * __fill_v4l2_buffer() - fill in a struct v4l2_buffer with information to be
  563. * returned to userspace
  564. */
  565. static void __fill_v4l2_buffer(struct vb2_buffer *vb, struct v4l2_buffer *b)
  566. {
  567. struct vb2_queue *q = vb->vb2_queue;
  568. /* Copy back data such as timestamp, flags, etc. */
  569. memcpy(b, &vb->v4l2_buf, offsetof(struct v4l2_buffer, m));
  570. b->reserved2 = vb->v4l2_buf.reserved2;
  571. b->reserved = vb->v4l2_buf.reserved;
  572. if (V4L2_TYPE_IS_MULTIPLANAR(q->type)) {
  573. /*
  574. * Fill in plane-related data if userspace provided an array
  575. * for it. The caller has already verified memory and size.
  576. */
  577. b->length = vb->num_planes;
  578. memcpy(b->m.planes, vb->v4l2_planes,
  579. b->length * sizeof(struct v4l2_plane));
  580. } else {
  581. /*
  582. * We use length and offset in v4l2_planes array even for
  583. * single-planar buffers, but userspace does not.
  584. */
  585. b->length = vb->v4l2_planes[0].length;
  586. b->bytesused = vb->v4l2_planes[0].bytesused;
  587. if (q->memory == V4L2_MEMORY_MMAP)
  588. b->m.offset = vb->v4l2_planes[0].m.mem_offset;
  589. else if (q->memory == V4L2_MEMORY_USERPTR)
  590. b->m.userptr = vb->v4l2_planes[0].m.userptr;
  591. else if (q->memory == V4L2_MEMORY_DMABUF)
  592. b->m.fd = vb->v4l2_planes[0].m.fd;
  593. }
  594. /*
  595. * Clear any buffer state related flags.
  596. */
  597. b->flags &= ~V4L2_BUFFER_MASK_FLAGS;
  598. b->flags |= q->timestamp_flags & V4L2_BUF_FLAG_TIMESTAMP_MASK;
  599. if ((q->timestamp_flags & V4L2_BUF_FLAG_TIMESTAMP_MASK) !=
  600. V4L2_BUF_FLAG_TIMESTAMP_COPY) {
  601. /*
  602. * For non-COPY timestamps, drop timestamp source bits
  603. * and obtain the timestamp source from the queue.
  604. */
  605. b->flags &= ~V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
  606. b->flags |= q->timestamp_flags & V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
  607. }
  608. switch (vb->state) {
  609. case VB2_BUF_STATE_QUEUED:
  610. case VB2_BUF_STATE_ACTIVE:
  611. b->flags |= V4L2_BUF_FLAG_QUEUED;
  612. break;
  613. case VB2_BUF_STATE_ERROR:
  614. b->flags |= V4L2_BUF_FLAG_ERROR;
  615. /* fall through */
  616. case VB2_BUF_STATE_DONE:
  617. b->flags |= V4L2_BUF_FLAG_DONE;
  618. break;
  619. case VB2_BUF_STATE_PREPARED:
  620. b->flags |= V4L2_BUF_FLAG_PREPARED;
  621. break;
  622. case VB2_BUF_STATE_PREPARING:
  623. case VB2_BUF_STATE_DEQUEUED:
  624. /* nothing */
  625. break;
  626. }
  627. if (__buffer_in_use(q, vb))
  628. b->flags |= V4L2_BUF_FLAG_MAPPED;
  629. }
  630. /**
  631. * vb2_querybuf() - query video buffer information
  632. * @q: videobuf queue
  633. * @b: buffer struct passed from userspace to vidioc_querybuf handler
  634. * in driver
  635. *
  636. * Should be called from vidioc_querybuf ioctl handler in driver.
  637. * This function will verify the passed v4l2_buffer structure and fill the
  638. * relevant information for the userspace.
  639. *
  640. * The return values from this function are intended to be directly returned
  641. * from vidioc_querybuf handler in driver.
  642. */
  643. int vb2_querybuf(struct vb2_queue *q, struct v4l2_buffer *b)
  644. {
  645. struct vb2_buffer *vb;
  646. int ret;
  647. if (b->type != q->type) {
  648. dprintk(1, "wrong buffer type\n");
  649. return -EINVAL;
  650. }
  651. if (b->index >= q->num_buffers) {
  652. dprintk(1, "buffer index out of range\n");
  653. return -EINVAL;
  654. }
  655. vb = q->bufs[b->index];
  656. ret = __verify_planes_array(vb, b);
  657. if (!ret)
  658. __fill_v4l2_buffer(vb, b);
  659. return ret;
  660. }
  661. EXPORT_SYMBOL(vb2_querybuf);
  662. /**
  663. * __verify_userptr_ops() - verify that all memory operations required for
  664. * USERPTR queue type have been provided
  665. */
  666. static int __verify_userptr_ops(struct vb2_queue *q)
  667. {
  668. if (!(q->io_modes & VB2_USERPTR) || !q->mem_ops->get_userptr ||
  669. !q->mem_ops->put_userptr)
  670. return -EINVAL;
  671. return 0;
  672. }
  673. /**
  674. * __verify_mmap_ops() - verify that all memory operations required for
  675. * MMAP queue type have been provided
  676. */
  677. static int __verify_mmap_ops(struct vb2_queue *q)
  678. {
  679. if (!(q->io_modes & VB2_MMAP) || !q->mem_ops->alloc ||
  680. !q->mem_ops->put || !q->mem_ops->mmap)
  681. return -EINVAL;
  682. return 0;
  683. }
  684. /**
  685. * __verify_dmabuf_ops() - verify that all memory operations required for
  686. * DMABUF queue type have been provided
  687. */
  688. static int __verify_dmabuf_ops(struct vb2_queue *q)
  689. {
  690. if (!(q->io_modes & VB2_DMABUF) || !q->mem_ops->attach_dmabuf ||
  691. !q->mem_ops->detach_dmabuf || !q->mem_ops->map_dmabuf ||
  692. !q->mem_ops->unmap_dmabuf)
  693. return -EINVAL;
  694. return 0;
  695. }
  696. /**
  697. * __verify_memory_type() - Check whether the memory type and buffer type
  698. * passed to a buffer operation are compatible with the queue.
  699. */
  700. static int __verify_memory_type(struct vb2_queue *q,
  701. enum v4l2_memory memory, enum v4l2_buf_type type)
  702. {
  703. if (memory != V4L2_MEMORY_MMAP && memory != V4L2_MEMORY_USERPTR &&
  704. memory != V4L2_MEMORY_DMABUF) {
  705. dprintk(1, "unsupported memory type\n");
  706. return -EINVAL;
  707. }
  708. if (type != q->type) {
  709. dprintk(1, "requested type is incorrect\n");
  710. return -EINVAL;
  711. }
  712. /*
  713. * Make sure all the required memory ops for given memory type
  714. * are available.
  715. */
  716. if (memory == V4L2_MEMORY_MMAP && __verify_mmap_ops(q)) {
  717. dprintk(1, "MMAP for current setup unsupported\n");
  718. return -EINVAL;
  719. }
  720. if (memory == V4L2_MEMORY_USERPTR && __verify_userptr_ops(q)) {
  721. dprintk(1, "USERPTR for current setup unsupported\n");
  722. return -EINVAL;
  723. }
  724. if (memory == V4L2_MEMORY_DMABUF && __verify_dmabuf_ops(q)) {
  725. dprintk(1, "DMABUF for current setup unsupported\n");
  726. return -EINVAL;
  727. }
  728. /*
  729. * Place the busy tests at the end: -EBUSY can be ignored when
  730. * create_bufs is called with count == 0, but count == 0 should still
  731. * do the memory and type validation.
  732. */
  733. if (vb2_fileio_is_active(q)) {
  734. dprintk(1, "file io in progress\n");
  735. return -EBUSY;
  736. }
  737. return 0;
  738. }
  739. /**
  740. * __reqbufs() - Initiate streaming
  741. * @q: videobuf2 queue
  742. * @req: struct passed from userspace to vidioc_reqbufs handler in driver
  743. *
  744. * Should be called from vidioc_reqbufs ioctl handler of a driver.
  745. * This function:
  746. * 1) verifies streaming parameters passed from the userspace,
  747. * 2) sets up the queue,
  748. * 3) negotiates number of buffers and planes per buffer with the driver
  749. * to be used during streaming,
  750. * 4) allocates internal buffer structures (struct vb2_buffer), according to
  751. * the agreed parameters,
  752. * 5) for MMAP memory type, allocates actual video memory, using the
  753. * memory handling/allocation routines provided during queue initialization
  754. *
  755. * If req->count is 0, all the memory will be freed instead.
  756. * If the queue has been allocated previously (by a previous vb2_reqbufs) call
  757. * and the queue is not busy, memory will be reallocated.
  758. *
  759. * The return values from this function are intended to be directly returned
  760. * from vidioc_reqbufs handler in driver.
  761. */
  762. static int __reqbufs(struct vb2_queue *q, struct v4l2_requestbuffers *req)
  763. {
  764. unsigned int num_buffers, allocated_buffers, num_planes = 0;
  765. int ret;
  766. if (q->streaming) {
  767. dprintk(1, "streaming active\n");
  768. return -EBUSY;
  769. }
  770. if (req->count == 0 || q->num_buffers != 0 || q->memory != req->memory) {
  771. /*
  772. * We already have buffers allocated, so first check if they
  773. * are not in use and can be freed.
  774. */
  775. mutex_lock(&q->mmap_lock);
  776. if (q->memory == V4L2_MEMORY_MMAP && __buffers_in_use(q)) {
  777. mutex_unlock(&q->mmap_lock);
  778. dprintk(1, "memory in use, cannot free\n");
  779. return -EBUSY;
  780. }
  781. /*
  782. * Call queue_cancel to clean up any buffers in the PREPARED or
  783. * QUEUED state which is possible if buffers were prepared or
  784. * queued without ever calling STREAMON.
  785. */
  786. __vb2_queue_cancel(q);
  787. ret = __vb2_queue_free(q, q->num_buffers);
  788. mutex_unlock(&q->mmap_lock);
  789. if (ret)
  790. return ret;
  791. /*
  792. * In case of REQBUFS(0) return immediately without calling
  793. * driver's queue_setup() callback and allocating resources.
  794. */
  795. if (req->count == 0)
  796. return 0;
  797. }
  798. /*
  799. * Make sure the requested values and current defaults are sane.
  800. */
  801. num_buffers = min_t(unsigned int, req->count, VIDEO_MAX_FRAME);
  802. num_buffers = max_t(unsigned int, num_buffers, q->min_buffers_needed);
  803. memset(q->plane_sizes, 0, sizeof(q->plane_sizes));
  804. memset(q->alloc_ctx, 0, sizeof(q->alloc_ctx));
  805. q->memory = req->memory;
  806. /*
  807. * Ask the driver how many buffers and planes per buffer it requires.
  808. * Driver also sets the size and allocator context for each plane.
  809. */
  810. ret = call_qop(q, queue_setup, q, NULL, &num_buffers, &num_planes,
  811. q->plane_sizes, q->alloc_ctx);
  812. if (ret)
  813. return ret;
  814. /* Finally, allocate buffers and video memory */
  815. allocated_buffers = __vb2_queue_alloc(q, req->memory, num_buffers, num_planes);
  816. if (allocated_buffers == 0) {
  817. dprintk(1, "memory allocation failed\n");
  818. return -ENOMEM;
  819. }
  820. /*
  821. * There is no point in continuing if we can't allocate the minimum
  822. * number of buffers needed by this vb2_queue.
  823. */
  824. if (allocated_buffers < q->min_buffers_needed)
  825. ret = -ENOMEM;
  826. /*
  827. * Check if driver can handle the allocated number of buffers.
  828. */
  829. if (!ret && allocated_buffers < num_buffers) {
  830. num_buffers = allocated_buffers;
  831. ret = call_qop(q, queue_setup, q, NULL, &num_buffers,
  832. &num_planes, q->plane_sizes, q->alloc_ctx);
  833. if (!ret && allocated_buffers < num_buffers)
  834. ret = -ENOMEM;
  835. /*
  836. * Either the driver has accepted a smaller number of buffers,
  837. * or .queue_setup() returned an error
  838. */
  839. }
  840. mutex_lock(&q->mmap_lock);
  841. q->num_buffers = allocated_buffers;
  842. if (ret < 0) {
  843. /*
  844. * Note: __vb2_queue_free() will subtract 'allocated_buffers'
  845. * from q->num_buffers.
  846. */
  847. __vb2_queue_free(q, allocated_buffers);
  848. mutex_unlock(&q->mmap_lock);
  849. return ret;
  850. }
  851. mutex_unlock(&q->mmap_lock);
  852. /*
  853. * Return the number of successfully allocated buffers
  854. * to the userspace.
  855. */
  856. req->count = allocated_buffers;
  857. q->waiting_for_buffers = !V4L2_TYPE_IS_OUTPUT(q->type);
  858. return 0;
  859. }
  860. /**
  861. * vb2_reqbufs() - Wrapper for __reqbufs() that also verifies the memory and
  862. * type values.
  863. * @q: videobuf2 queue
  864. * @req: struct passed from userspace to vidioc_reqbufs handler in driver
  865. */
  866. int vb2_reqbufs(struct vb2_queue *q, struct v4l2_requestbuffers *req)
  867. {
  868. int ret = __verify_memory_type(q, req->memory, req->type);
  869. return ret ? ret : __reqbufs(q, req);
  870. }
  871. EXPORT_SYMBOL_GPL(vb2_reqbufs);
  872. /**
  873. * __create_bufs() - Allocate buffers and any required auxiliary structs
  874. * @q: videobuf2 queue
  875. * @create: creation parameters, passed from userspace to vidioc_create_bufs
  876. * handler in driver
  877. *
  878. * Should be called from vidioc_create_bufs ioctl handler of a driver.
  879. * This function:
  880. * 1) verifies parameter sanity
  881. * 2) calls the .queue_setup() queue operation
  882. * 3) performs any necessary memory allocations
  883. *
  884. * The return values from this function are intended to be directly returned
  885. * from vidioc_create_bufs handler in driver.
  886. */
  887. static int __create_bufs(struct vb2_queue *q, struct v4l2_create_buffers *create)
  888. {
  889. unsigned int num_planes = 0, num_buffers, allocated_buffers;
  890. int ret;
  891. if (q->num_buffers == VIDEO_MAX_FRAME) {
  892. dprintk(1, "maximum number of buffers already allocated\n");
  893. return -ENOBUFS;
  894. }
  895. if (!q->num_buffers) {
  896. memset(q->plane_sizes, 0, sizeof(q->plane_sizes));
  897. memset(q->alloc_ctx, 0, sizeof(q->alloc_ctx));
  898. q->memory = create->memory;
  899. q->waiting_for_buffers = !V4L2_TYPE_IS_OUTPUT(q->type);
  900. }
  901. num_buffers = min(create->count, VIDEO_MAX_FRAME - q->num_buffers);
  902. /*
  903. * Ask the driver, whether the requested number of buffers, planes per
  904. * buffer and their sizes are acceptable
  905. */
  906. ret = call_qop(q, queue_setup, q, &create->format, &num_buffers,
  907. &num_planes, q->plane_sizes, q->alloc_ctx);
  908. if (ret)
  909. return ret;
  910. /* Finally, allocate buffers and video memory */
  911. allocated_buffers = __vb2_queue_alloc(q, create->memory, num_buffers,
  912. num_planes);
  913. if (allocated_buffers == 0) {
  914. dprintk(1, "memory allocation failed\n");
  915. return -ENOMEM;
  916. }
  917. /*
  918. * Check if driver can handle the so far allocated number of buffers.
  919. */
  920. if (allocated_buffers < num_buffers) {
  921. num_buffers = allocated_buffers;
  922. /*
  923. * q->num_buffers contains the total number of buffers, that the
  924. * queue driver has set up
  925. */
  926. ret = call_qop(q, queue_setup, q, &create->format, &num_buffers,
  927. &num_planes, q->plane_sizes, q->alloc_ctx);
  928. if (!ret && allocated_buffers < num_buffers)
  929. ret = -ENOMEM;
  930. /*
  931. * Either the driver has accepted a smaller number of buffers,
  932. * or .queue_setup() returned an error
  933. */
  934. }
  935. mutex_lock(&q->mmap_lock);
  936. q->num_buffers += allocated_buffers;
  937. if (ret < 0) {
  938. /*
  939. * Note: __vb2_queue_free() will subtract 'allocated_buffers'
  940. * from q->num_buffers.
  941. */
  942. __vb2_queue_free(q, allocated_buffers);
  943. mutex_unlock(&q->mmap_lock);
  944. return -ENOMEM;
  945. }
  946. mutex_unlock(&q->mmap_lock);
  947. /*
  948. * Return the number of successfully allocated buffers
  949. * to the userspace.
  950. */
  951. create->count = allocated_buffers;
  952. return 0;
  953. }
  954. /**
  955. * vb2_create_bufs() - Wrapper for __create_bufs() that also verifies the
  956. * memory and type values.
  957. * @q: videobuf2 queue
  958. * @create: creation parameters, passed from userspace to vidioc_create_bufs
  959. * handler in driver
  960. */
  961. int vb2_create_bufs(struct vb2_queue *q, struct v4l2_create_buffers *create)
  962. {
  963. int ret = __verify_memory_type(q, create->memory, create->format.type);
  964. create->index = q->num_buffers;
  965. if (create->count == 0)
  966. return ret != -EBUSY ? ret : 0;
  967. return ret ? ret : __create_bufs(q, create);
  968. }
  969. EXPORT_SYMBOL_GPL(vb2_create_bufs);
  970. /**
  971. * vb2_plane_vaddr() - Return a kernel virtual address of a given plane
  972. * @vb: vb2_buffer to which the plane in question belongs to
  973. * @plane_no: plane number for which the address is to be returned
  974. *
  975. * This function returns a kernel virtual address of a given plane if
  976. * such a mapping exist, NULL otherwise.
  977. */
  978. void *vb2_plane_vaddr(struct vb2_buffer *vb, unsigned int plane_no)
  979. {
  980. if (plane_no > vb->num_planes || !vb->planes[plane_no].mem_priv)
  981. return NULL;
  982. return call_ptr_memop(vb, vaddr, vb->planes[plane_no].mem_priv);
  983. }
  984. EXPORT_SYMBOL_GPL(vb2_plane_vaddr);
  985. /**
  986. * vb2_plane_cookie() - Return allocator specific cookie for the given plane
  987. * @vb: vb2_buffer to which the plane in question belongs to
  988. * @plane_no: plane number for which the cookie is to be returned
  989. *
  990. * This function returns an allocator specific cookie for a given plane if
  991. * available, NULL otherwise. The allocator should provide some simple static
  992. * inline function, which would convert this cookie to the allocator specific
  993. * type that can be used directly by the driver to access the buffer. This can
  994. * be for example physical address, pointer to scatter list or IOMMU mapping.
  995. */
  996. void *vb2_plane_cookie(struct vb2_buffer *vb, unsigned int plane_no)
  997. {
  998. if (plane_no >= vb->num_planes || !vb->planes[plane_no].mem_priv)
  999. return NULL;
  1000. return call_ptr_memop(vb, cookie, vb->planes[plane_no].mem_priv);
  1001. }
  1002. EXPORT_SYMBOL_GPL(vb2_plane_cookie);
  1003. /**
  1004. * vb2_buffer_done() - inform videobuf that an operation on a buffer is finished
  1005. * @vb: vb2_buffer returned from the driver
  1006. * @state: either VB2_BUF_STATE_DONE if the operation finished successfully
  1007. * or VB2_BUF_STATE_ERROR if the operation finished with an error.
  1008. * If start_streaming fails then it should return buffers with state
  1009. * VB2_BUF_STATE_QUEUED to put them back into the queue.
  1010. *
  1011. * This function should be called by the driver after a hardware operation on
  1012. * a buffer is finished and the buffer may be returned to userspace. The driver
  1013. * cannot use this buffer anymore until it is queued back to it by videobuf
  1014. * by the means of buf_queue callback. Only buffers previously queued to the
  1015. * driver by buf_queue can be passed to this function.
  1016. *
  1017. * While streaming a buffer can only be returned in state DONE or ERROR.
  1018. * The start_streaming op can also return them in case the DMA engine cannot
  1019. * be started for some reason. In that case the buffers should be returned with
  1020. * state QUEUED.
  1021. */
  1022. void vb2_buffer_done(struct vb2_buffer *vb, enum vb2_buffer_state state)
  1023. {
  1024. struct vb2_queue *q = vb->vb2_queue;
  1025. unsigned long flags;
  1026. unsigned int plane;
  1027. if (WARN_ON(vb->state != VB2_BUF_STATE_ACTIVE))
  1028. return;
  1029. if (WARN_ON(state != VB2_BUF_STATE_DONE &&
  1030. state != VB2_BUF_STATE_ERROR &&
  1031. state != VB2_BUF_STATE_QUEUED))
  1032. state = VB2_BUF_STATE_ERROR;
  1033. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1034. /*
  1035. * Although this is not a callback, it still does have to balance
  1036. * with the buf_queue op. So update this counter manually.
  1037. */
  1038. vb->cnt_buf_done++;
  1039. #endif
  1040. dprintk(4, "done processing on buffer %d, state: %d\n",
  1041. vb->v4l2_buf.index, state);
  1042. /* sync buffers */
  1043. for (plane = 0; plane < vb->num_planes; ++plane)
  1044. call_void_memop(vb, finish, vb->planes[plane].mem_priv);
  1045. /* Add the buffer to the done buffers list */
  1046. spin_lock_irqsave(&q->done_lock, flags);
  1047. vb->state = state;
  1048. if (state != VB2_BUF_STATE_QUEUED)
  1049. list_add_tail(&vb->done_entry, &q->done_list);
  1050. atomic_dec(&q->owned_by_drv_count);
  1051. spin_unlock_irqrestore(&q->done_lock, flags);
  1052. if (state == VB2_BUF_STATE_QUEUED)
  1053. return;
  1054. /* Inform any processes that may be waiting for buffers */
  1055. wake_up(&q->done_wq);
  1056. }
  1057. EXPORT_SYMBOL_GPL(vb2_buffer_done);
  1058. /**
  1059. * vb2_discard_done() - discard all buffers marked as DONE
  1060. * @q: videobuf2 queue
  1061. *
  1062. * This function is intended to be used with suspend/resume operations. It
  1063. * discards all 'done' buffers as they would be too old to be requested after
  1064. * resume.
  1065. *
  1066. * Drivers must stop the hardware and synchronize with interrupt handlers and/or
  1067. * delayed works before calling this function to make sure no buffer will be
  1068. * touched by the driver and/or hardware.
  1069. */
  1070. void vb2_discard_done(struct vb2_queue *q)
  1071. {
  1072. struct vb2_buffer *vb;
  1073. unsigned long flags;
  1074. spin_lock_irqsave(&q->done_lock, flags);
  1075. list_for_each_entry(vb, &q->done_list, done_entry)
  1076. vb->state = VB2_BUF_STATE_ERROR;
  1077. spin_unlock_irqrestore(&q->done_lock, flags);
  1078. }
  1079. EXPORT_SYMBOL_GPL(vb2_discard_done);
  1080. /**
  1081. * __fill_vb2_buffer() - fill a vb2_buffer with information provided in a
  1082. * v4l2_buffer by the userspace. The caller has already verified that struct
  1083. * v4l2_buffer has a valid number of planes.
  1084. */
  1085. static void __fill_vb2_buffer(struct vb2_buffer *vb, const struct v4l2_buffer *b,
  1086. struct v4l2_plane *v4l2_planes)
  1087. {
  1088. unsigned int plane;
  1089. if (V4L2_TYPE_IS_MULTIPLANAR(b->type)) {
  1090. if (b->memory == V4L2_MEMORY_USERPTR) {
  1091. for (plane = 0; plane < vb->num_planes; ++plane) {
  1092. v4l2_planes[plane].m.userptr =
  1093. b->m.planes[plane].m.userptr;
  1094. v4l2_planes[plane].length =
  1095. b->m.planes[plane].length;
  1096. }
  1097. }
  1098. if (b->memory == V4L2_MEMORY_DMABUF) {
  1099. for (plane = 0; plane < vb->num_planes; ++plane) {
  1100. v4l2_planes[plane].m.fd =
  1101. b->m.planes[plane].m.fd;
  1102. v4l2_planes[plane].length =
  1103. b->m.planes[plane].length;
  1104. }
  1105. }
  1106. /* Fill in driver-provided information for OUTPUT types */
  1107. if (V4L2_TYPE_IS_OUTPUT(b->type)) {
  1108. /*
  1109. * Will have to go up to b->length when API starts
  1110. * accepting variable number of planes.
  1111. *
  1112. * If bytesused == 0 for the output buffer, then fall
  1113. * back to the full buffer size. In that case
  1114. * userspace clearly never bothered to set it and
  1115. * it's a safe assumption that they really meant to
  1116. * use the full plane sizes.
  1117. */
  1118. for (plane = 0; plane < vb->num_planes; ++plane) {
  1119. struct v4l2_plane *pdst = &v4l2_planes[plane];
  1120. struct v4l2_plane *psrc = &b->m.planes[plane];
  1121. pdst->bytesused = psrc->bytesused ?
  1122. psrc->bytesused : pdst->length;
  1123. pdst->data_offset = psrc->data_offset;
  1124. }
  1125. }
  1126. } else {
  1127. /*
  1128. * Single-planar buffers do not use planes array,
  1129. * so fill in relevant v4l2_buffer struct fields instead.
  1130. * In videobuf we use our internal V4l2_planes struct for
  1131. * single-planar buffers as well, for simplicity.
  1132. *
  1133. * If bytesused == 0 for the output buffer, then fall back
  1134. * to the full buffer size as that's a sensible default.
  1135. */
  1136. if (b->memory == V4L2_MEMORY_USERPTR) {
  1137. v4l2_planes[0].m.userptr = b->m.userptr;
  1138. v4l2_planes[0].length = b->length;
  1139. }
  1140. if (b->memory == V4L2_MEMORY_DMABUF) {
  1141. v4l2_planes[0].m.fd = b->m.fd;
  1142. v4l2_planes[0].length = b->length;
  1143. }
  1144. if (V4L2_TYPE_IS_OUTPUT(b->type))
  1145. v4l2_planes[0].bytesused = b->bytesused ?
  1146. b->bytesused : v4l2_planes[0].length;
  1147. else
  1148. v4l2_planes[0].bytesused = 0;
  1149. }
  1150. /* Zero flags that the vb2 core handles */
  1151. vb->v4l2_buf.flags = b->flags & ~V4L2_BUFFER_MASK_FLAGS;
  1152. if ((vb->vb2_queue->timestamp_flags & V4L2_BUF_FLAG_TIMESTAMP_MASK) !=
  1153. V4L2_BUF_FLAG_TIMESTAMP_COPY || !V4L2_TYPE_IS_OUTPUT(b->type)) {
  1154. /*
  1155. * Non-COPY timestamps and non-OUTPUT queues will get
  1156. * their timestamp and timestamp source flags from the
  1157. * queue.
  1158. */
  1159. vb->v4l2_buf.flags &= ~V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
  1160. }
  1161. if (V4L2_TYPE_IS_OUTPUT(b->type)) {
  1162. /*
  1163. * For output buffers mask out the timecode flag:
  1164. * this will be handled later in vb2_internal_qbuf().
  1165. * The 'field' is valid metadata for this output buffer
  1166. * and so that needs to be copied here.
  1167. */
  1168. vb->v4l2_buf.flags &= ~V4L2_BUF_FLAG_TIMECODE;
  1169. vb->v4l2_buf.field = b->field;
  1170. } else {
  1171. /* Zero any output buffer flags as this is a capture buffer */
  1172. vb->v4l2_buf.flags &= ~V4L2_BUFFER_OUT_FLAGS;
  1173. }
  1174. }
  1175. /**
  1176. * __qbuf_mmap() - handle qbuf of an MMAP buffer
  1177. */
  1178. static int __qbuf_mmap(struct vb2_buffer *vb, const struct v4l2_buffer *b)
  1179. {
  1180. __fill_vb2_buffer(vb, b, vb->v4l2_planes);
  1181. return call_vb_qop(vb, buf_prepare, vb);
  1182. }
  1183. /**
  1184. * __qbuf_userptr() - handle qbuf of a USERPTR buffer
  1185. */
  1186. static int __qbuf_userptr(struct vb2_buffer *vb, const struct v4l2_buffer *b)
  1187. {
  1188. struct v4l2_plane planes[VIDEO_MAX_PLANES];
  1189. struct vb2_queue *q = vb->vb2_queue;
  1190. void *mem_priv;
  1191. unsigned int plane;
  1192. int ret;
  1193. int write = !V4L2_TYPE_IS_OUTPUT(q->type);
  1194. bool reacquired = vb->planes[0].mem_priv == NULL;
  1195. memset(planes, 0, sizeof(planes[0]) * vb->num_planes);
  1196. /* Copy relevant information provided by the userspace */
  1197. __fill_vb2_buffer(vb, b, planes);
  1198. for (plane = 0; plane < vb->num_planes; ++plane) {
  1199. /* Skip the plane if already verified */
  1200. if (vb->v4l2_planes[plane].m.userptr &&
  1201. vb->v4l2_planes[plane].m.userptr == planes[plane].m.userptr
  1202. && vb->v4l2_planes[plane].length == planes[plane].length)
  1203. continue;
  1204. dprintk(3, "userspace address for plane %d changed, "
  1205. "reacquiring memory\n", plane);
  1206. /* Check if the provided plane buffer is large enough */
  1207. if (planes[plane].length < q->plane_sizes[plane]) {
  1208. dprintk(1, "provided buffer size %u is less than "
  1209. "setup size %u for plane %d\n",
  1210. planes[plane].length,
  1211. q->plane_sizes[plane], plane);
  1212. ret = -EINVAL;
  1213. goto err;
  1214. }
  1215. /* Release previously acquired memory if present */
  1216. if (vb->planes[plane].mem_priv) {
  1217. if (!reacquired) {
  1218. reacquired = true;
  1219. call_void_vb_qop(vb, buf_cleanup, vb);
  1220. }
  1221. call_void_memop(vb, put_userptr, vb->planes[plane].mem_priv);
  1222. }
  1223. vb->planes[plane].mem_priv = NULL;
  1224. memset(&vb->v4l2_planes[plane], 0, sizeof(struct v4l2_plane));
  1225. /* Acquire each plane's memory */
  1226. mem_priv = call_ptr_memop(vb, get_userptr, q->alloc_ctx[plane],
  1227. planes[plane].m.userptr,
  1228. planes[plane].length, write);
  1229. if (IS_ERR_OR_NULL(mem_priv)) {
  1230. dprintk(1, "failed acquiring userspace "
  1231. "memory for plane %d\n", plane);
  1232. ret = mem_priv ? PTR_ERR(mem_priv) : -EINVAL;
  1233. goto err;
  1234. }
  1235. vb->planes[plane].mem_priv = mem_priv;
  1236. }
  1237. /*
  1238. * Now that everything is in order, copy relevant information
  1239. * provided by userspace.
  1240. */
  1241. for (plane = 0; plane < vb->num_planes; ++plane)
  1242. vb->v4l2_planes[plane] = planes[plane];
  1243. if (reacquired) {
  1244. /*
  1245. * One or more planes changed, so we must call buf_init to do
  1246. * the driver-specific initialization on the newly acquired
  1247. * buffer, if provided.
  1248. */
  1249. ret = call_vb_qop(vb, buf_init, vb);
  1250. if (ret) {
  1251. dprintk(1, "buffer initialization failed\n");
  1252. goto err;
  1253. }
  1254. }
  1255. ret = call_vb_qop(vb, buf_prepare, vb);
  1256. if (ret) {
  1257. dprintk(1, "buffer preparation failed\n");
  1258. call_void_vb_qop(vb, buf_cleanup, vb);
  1259. goto err;
  1260. }
  1261. return 0;
  1262. err:
  1263. /* In case of errors, release planes that were already acquired */
  1264. for (plane = 0; plane < vb->num_planes; ++plane) {
  1265. if (vb->planes[plane].mem_priv)
  1266. call_void_memop(vb, put_userptr, vb->planes[plane].mem_priv);
  1267. vb->planes[plane].mem_priv = NULL;
  1268. vb->v4l2_planes[plane].m.userptr = 0;
  1269. vb->v4l2_planes[plane].length = 0;
  1270. }
  1271. return ret;
  1272. }
  1273. /**
  1274. * __qbuf_dmabuf() - handle qbuf of a DMABUF buffer
  1275. */
  1276. static int __qbuf_dmabuf(struct vb2_buffer *vb, const struct v4l2_buffer *b)
  1277. {
  1278. struct v4l2_plane planes[VIDEO_MAX_PLANES];
  1279. struct vb2_queue *q = vb->vb2_queue;
  1280. void *mem_priv;
  1281. unsigned int plane;
  1282. int ret;
  1283. int write = !V4L2_TYPE_IS_OUTPUT(q->type);
  1284. bool reacquired = vb->planes[0].mem_priv == NULL;
  1285. memset(planes, 0, sizeof(planes[0]) * vb->num_planes);
  1286. /* Copy relevant information provided by the userspace */
  1287. __fill_vb2_buffer(vb, b, planes);
  1288. for (plane = 0; plane < vb->num_planes; ++plane) {
  1289. struct dma_buf *dbuf = dma_buf_get(planes[plane].m.fd);
  1290. if (IS_ERR_OR_NULL(dbuf)) {
  1291. dprintk(1, "invalid dmabuf fd for plane %d\n",
  1292. plane);
  1293. ret = -EINVAL;
  1294. goto err;
  1295. }
  1296. /* use DMABUF size if length is not provided */
  1297. if (planes[plane].length == 0)
  1298. planes[plane].length = dbuf->size;
  1299. if (planes[plane].length < q->plane_sizes[plane]) {
  1300. dprintk(1, "invalid dmabuf length for plane %d\n",
  1301. plane);
  1302. ret = -EINVAL;
  1303. goto err;
  1304. }
  1305. /* Skip the plane if already verified */
  1306. if (dbuf == vb->planes[plane].dbuf &&
  1307. vb->v4l2_planes[plane].length == planes[plane].length) {
  1308. dma_buf_put(dbuf);
  1309. continue;
  1310. }
  1311. dprintk(1, "buffer for plane %d changed\n", plane);
  1312. if (!reacquired) {
  1313. reacquired = true;
  1314. call_void_vb_qop(vb, buf_cleanup, vb);
  1315. }
  1316. /* Release previously acquired memory if present */
  1317. __vb2_plane_dmabuf_put(vb, &vb->planes[plane]);
  1318. memset(&vb->v4l2_planes[plane], 0, sizeof(struct v4l2_plane));
  1319. /* Acquire each plane's memory */
  1320. mem_priv = call_ptr_memop(vb, attach_dmabuf, q->alloc_ctx[plane],
  1321. dbuf, planes[plane].length, write);
  1322. if (IS_ERR(mem_priv)) {
  1323. dprintk(1, "failed to attach dmabuf\n");
  1324. ret = PTR_ERR(mem_priv);
  1325. dma_buf_put(dbuf);
  1326. goto err;
  1327. }
  1328. vb->planes[plane].dbuf = dbuf;
  1329. vb->planes[plane].mem_priv = mem_priv;
  1330. }
  1331. /* TODO: This pins the buffer(s) with dma_buf_map_attachment()).. but
  1332. * really we want to do this just before the DMA, not while queueing
  1333. * the buffer(s)..
  1334. */
  1335. for (plane = 0; plane < vb->num_planes; ++plane) {
  1336. ret = call_memop(vb, map_dmabuf, vb->planes[plane].mem_priv);
  1337. if (ret) {
  1338. dprintk(1, "failed to map dmabuf for plane %d\n",
  1339. plane);
  1340. goto err;
  1341. }
  1342. vb->planes[plane].dbuf_mapped = 1;
  1343. }
  1344. /*
  1345. * Now that everything is in order, copy relevant information
  1346. * provided by userspace.
  1347. */
  1348. for (plane = 0; plane < vb->num_planes; ++plane)
  1349. vb->v4l2_planes[plane] = planes[plane];
  1350. if (reacquired) {
  1351. /*
  1352. * Call driver-specific initialization on the newly acquired buffer,
  1353. * if provided.
  1354. */
  1355. ret = call_vb_qop(vb, buf_init, vb);
  1356. if (ret) {
  1357. dprintk(1, "buffer initialization failed\n");
  1358. goto err;
  1359. }
  1360. }
  1361. ret = call_vb_qop(vb, buf_prepare, vb);
  1362. if (ret) {
  1363. dprintk(1, "buffer preparation failed\n");
  1364. call_void_vb_qop(vb, buf_cleanup, vb);
  1365. goto err;
  1366. }
  1367. return 0;
  1368. err:
  1369. /* In case of errors, release planes that were already acquired */
  1370. __vb2_buf_dmabuf_put(vb);
  1371. return ret;
  1372. }
  1373. /**
  1374. * __enqueue_in_driver() - enqueue a vb2_buffer in driver for processing
  1375. */
  1376. static void __enqueue_in_driver(struct vb2_buffer *vb)
  1377. {
  1378. struct vb2_queue *q = vb->vb2_queue;
  1379. unsigned int plane;
  1380. vb->state = VB2_BUF_STATE_ACTIVE;
  1381. atomic_inc(&q->owned_by_drv_count);
  1382. /* sync buffers */
  1383. for (plane = 0; plane < vb->num_planes; ++plane)
  1384. call_void_memop(vb, prepare, vb->planes[plane].mem_priv);
  1385. call_void_vb_qop(vb, buf_queue, vb);
  1386. }
  1387. static int __buf_prepare(struct vb2_buffer *vb, const struct v4l2_buffer *b)
  1388. {
  1389. struct vb2_queue *q = vb->vb2_queue;
  1390. int ret;
  1391. ret = __verify_length(vb, b);
  1392. if (ret < 0) {
  1393. dprintk(1, "plane parameters verification failed: %d\n", ret);
  1394. return ret;
  1395. }
  1396. if (b->field == V4L2_FIELD_ALTERNATE && V4L2_TYPE_IS_OUTPUT(q->type)) {
  1397. /*
  1398. * If the format's field is ALTERNATE, then the buffer's field
  1399. * should be either TOP or BOTTOM, not ALTERNATE since that
  1400. * makes no sense. The driver has to know whether the
  1401. * buffer represents a top or a bottom field in order to
  1402. * program any DMA correctly. Using ALTERNATE is wrong, since
  1403. * that just says that it is either a top or a bottom field,
  1404. * but not which of the two it is.
  1405. */
  1406. dprintk(1, "the field is incorrectly set to ALTERNATE for an output buffer\n");
  1407. return -EINVAL;
  1408. }
  1409. if (q->error) {
  1410. dprintk(1, "fatal error occurred on queue\n");
  1411. return -EIO;
  1412. }
  1413. vb->state = VB2_BUF_STATE_PREPARING;
  1414. vb->v4l2_buf.timestamp.tv_sec = 0;
  1415. vb->v4l2_buf.timestamp.tv_usec = 0;
  1416. vb->v4l2_buf.sequence = 0;
  1417. switch (q->memory) {
  1418. case V4L2_MEMORY_MMAP:
  1419. ret = __qbuf_mmap(vb, b);
  1420. break;
  1421. case V4L2_MEMORY_USERPTR:
  1422. down_read(&current->mm->mmap_sem);
  1423. ret = __qbuf_userptr(vb, b);
  1424. up_read(&current->mm->mmap_sem);
  1425. break;
  1426. case V4L2_MEMORY_DMABUF:
  1427. ret = __qbuf_dmabuf(vb, b);
  1428. break;
  1429. default:
  1430. WARN(1, "Invalid queue type\n");
  1431. ret = -EINVAL;
  1432. }
  1433. if (ret)
  1434. dprintk(1, "buffer preparation failed: %d\n", ret);
  1435. vb->state = ret ? VB2_BUF_STATE_DEQUEUED : VB2_BUF_STATE_PREPARED;
  1436. return ret;
  1437. }
  1438. static int vb2_queue_or_prepare_buf(struct vb2_queue *q, struct v4l2_buffer *b,
  1439. const char *opname)
  1440. {
  1441. if (b->type != q->type) {
  1442. dprintk(1, "%s: invalid buffer type\n", opname);
  1443. return -EINVAL;
  1444. }
  1445. if (b->index >= q->num_buffers) {
  1446. dprintk(1, "%s: buffer index out of range\n", opname);
  1447. return -EINVAL;
  1448. }
  1449. if (q->bufs[b->index] == NULL) {
  1450. /* Should never happen */
  1451. dprintk(1, "%s: buffer is NULL\n", opname);
  1452. return -EINVAL;
  1453. }
  1454. if (b->memory != q->memory) {
  1455. dprintk(1, "%s: invalid memory type\n", opname);
  1456. return -EINVAL;
  1457. }
  1458. return __verify_planes_array(q->bufs[b->index], b);
  1459. }
  1460. /**
  1461. * vb2_prepare_buf() - Pass ownership of a buffer from userspace to the kernel
  1462. * @q: videobuf2 queue
  1463. * @b: buffer structure passed from userspace to vidioc_prepare_buf
  1464. * handler in driver
  1465. *
  1466. * Should be called from vidioc_prepare_buf ioctl handler of a driver.
  1467. * This function:
  1468. * 1) verifies the passed buffer,
  1469. * 2) calls buf_prepare callback in the driver (if provided), in which
  1470. * driver-specific buffer initialization can be performed,
  1471. *
  1472. * The return values from this function are intended to be directly returned
  1473. * from vidioc_prepare_buf handler in driver.
  1474. */
  1475. int vb2_prepare_buf(struct vb2_queue *q, struct v4l2_buffer *b)
  1476. {
  1477. struct vb2_buffer *vb;
  1478. int ret;
  1479. if (vb2_fileio_is_active(q)) {
  1480. dprintk(1, "file io in progress\n");
  1481. return -EBUSY;
  1482. }
  1483. ret = vb2_queue_or_prepare_buf(q, b, "prepare_buf");
  1484. if (ret)
  1485. return ret;
  1486. vb = q->bufs[b->index];
  1487. if (vb->state != VB2_BUF_STATE_DEQUEUED) {
  1488. dprintk(1, "invalid buffer state %d\n",
  1489. vb->state);
  1490. return -EINVAL;
  1491. }
  1492. ret = __buf_prepare(vb, b);
  1493. if (!ret) {
  1494. /* Fill buffer information for the userspace */
  1495. __fill_v4l2_buffer(vb, b);
  1496. dprintk(1, "prepare of buffer %d succeeded\n", vb->v4l2_buf.index);
  1497. }
  1498. return ret;
  1499. }
  1500. EXPORT_SYMBOL_GPL(vb2_prepare_buf);
  1501. /**
  1502. * vb2_start_streaming() - Attempt to start streaming.
  1503. * @q: videobuf2 queue
  1504. *
  1505. * Attempt to start streaming. When this function is called there must be
  1506. * at least q->min_buffers_needed buffers queued up (i.e. the minimum
  1507. * number of buffers required for the DMA engine to function). If the
  1508. * @start_streaming op fails it is supposed to return all the driver-owned
  1509. * buffers back to vb2 in state QUEUED. Check if that happened and if
  1510. * not warn and reclaim them forcefully.
  1511. */
  1512. static int vb2_start_streaming(struct vb2_queue *q)
  1513. {
  1514. struct vb2_buffer *vb;
  1515. int ret;
  1516. /*
  1517. * If any buffers were queued before streamon,
  1518. * we can now pass them to driver for processing.
  1519. */
  1520. list_for_each_entry(vb, &q->queued_list, queued_entry)
  1521. __enqueue_in_driver(vb);
  1522. /* Tell the driver to start streaming */
  1523. q->start_streaming_called = 1;
  1524. ret = call_qop(q, start_streaming, q,
  1525. atomic_read(&q->owned_by_drv_count));
  1526. if (!ret)
  1527. return 0;
  1528. q->start_streaming_called = 0;
  1529. dprintk(1, "driver refused to start streaming\n");
  1530. /*
  1531. * If you see this warning, then the driver isn't cleaning up properly
  1532. * after a failed start_streaming(). See the start_streaming()
  1533. * documentation in videobuf2-core.h for more information how buffers
  1534. * should be returned to vb2 in start_streaming().
  1535. */
  1536. if (WARN_ON(atomic_read(&q->owned_by_drv_count))) {
  1537. unsigned i;
  1538. /*
  1539. * Forcefully reclaim buffers if the driver did not
  1540. * correctly return them to vb2.
  1541. */
  1542. for (i = 0; i < q->num_buffers; ++i) {
  1543. vb = q->bufs[i];
  1544. if (vb->state == VB2_BUF_STATE_ACTIVE)
  1545. vb2_buffer_done(vb, VB2_BUF_STATE_QUEUED);
  1546. }
  1547. /* Must be zero now */
  1548. WARN_ON(atomic_read(&q->owned_by_drv_count));
  1549. }
  1550. /*
  1551. * If done_list is not empty, then start_streaming() didn't call
  1552. * vb2_buffer_done(vb, VB2_BUF_STATE_QUEUED) but STATE_ERROR or
  1553. * STATE_DONE.
  1554. */
  1555. WARN_ON(!list_empty(&q->done_list));
  1556. return ret;
  1557. }
  1558. static int vb2_internal_qbuf(struct vb2_queue *q, struct v4l2_buffer *b)
  1559. {
  1560. int ret = vb2_queue_or_prepare_buf(q, b, "qbuf");
  1561. struct vb2_buffer *vb;
  1562. if (ret)
  1563. return ret;
  1564. vb = q->bufs[b->index];
  1565. switch (vb->state) {
  1566. case VB2_BUF_STATE_DEQUEUED:
  1567. ret = __buf_prepare(vb, b);
  1568. if (ret)
  1569. return ret;
  1570. break;
  1571. case VB2_BUF_STATE_PREPARED:
  1572. break;
  1573. case VB2_BUF_STATE_PREPARING:
  1574. dprintk(1, "buffer still being prepared\n");
  1575. return -EINVAL;
  1576. default:
  1577. dprintk(1, "invalid buffer state %d\n", vb->state);
  1578. return -EINVAL;
  1579. }
  1580. /*
  1581. * Add to the queued buffers list, a buffer will stay on it until
  1582. * dequeued in dqbuf.
  1583. */
  1584. list_add_tail(&vb->queued_entry, &q->queued_list);
  1585. q->queued_count++;
  1586. q->waiting_for_buffers = false;
  1587. vb->state = VB2_BUF_STATE_QUEUED;
  1588. if (V4L2_TYPE_IS_OUTPUT(q->type)) {
  1589. /*
  1590. * For output buffers copy the timestamp if needed,
  1591. * and the timecode field and flag if needed.
  1592. */
  1593. if ((q->timestamp_flags & V4L2_BUF_FLAG_TIMESTAMP_MASK) ==
  1594. V4L2_BUF_FLAG_TIMESTAMP_COPY)
  1595. vb->v4l2_buf.timestamp = b->timestamp;
  1596. vb->v4l2_buf.flags |= b->flags & V4L2_BUF_FLAG_TIMECODE;
  1597. if (b->flags & V4L2_BUF_FLAG_TIMECODE)
  1598. vb->v4l2_buf.timecode = b->timecode;
  1599. }
  1600. /*
  1601. * If already streaming, give the buffer to driver for processing.
  1602. * If not, the buffer will be given to driver on next streamon.
  1603. */
  1604. if (q->start_streaming_called)
  1605. __enqueue_in_driver(vb);
  1606. /* Fill buffer information for the userspace */
  1607. __fill_v4l2_buffer(vb, b);
  1608. /*
  1609. * If streamon has been called, and we haven't yet called
  1610. * start_streaming() since not enough buffers were queued, and
  1611. * we now have reached the minimum number of queued buffers,
  1612. * then we can finally call start_streaming().
  1613. */
  1614. if (q->streaming && !q->start_streaming_called &&
  1615. q->queued_count >= q->min_buffers_needed) {
  1616. ret = vb2_start_streaming(q);
  1617. if (ret)
  1618. return ret;
  1619. }
  1620. dprintk(1, "qbuf of buffer %d succeeded\n", vb->v4l2_buf.index);
  1621. return 0;
  1622. }
  1623. /**
  1624. * vb2_qbuf() - Queue a buffer from userspace
  1625. * @q: videobuf2 queue
  1626. * @b: buffer structure passed from userspace to vidioc_qbuf handler
  1627. * in driver
  1628. *
  1629. * Should be called from vidioc_qbuf ioctl handler of a driver.
  1630. * This function:
  1631. * 1) verifies the passed buffer,
  1632. * 2) if necessary, calls buf_prepare callback in the driver (if provided), in
  1633. * which driver-specific buffer initialization can be performed,
  1634. * 3) if streaming is on, queues the buffer in driver by the means of buf_queue
  1635. * callback for processing.
  1636. *
  1637. * The return values from this function are intended to be directly returned
  1638. * from vidioc_qbuf handler in driver.
  1639. */
  1640. int vb2_qbuf(struct vb2_queue *q, struct v4l2_buffer *b)
  1641. {
  1642. if (vb2_fileio_is_active(q)) {
  1643. dprintk(1, "file io in progress\n");
  1644. return -EBUSY;
  1645. }
  1646. return vb2_internal_qbuf(q, b);
  1647. }
  1648. EXPORT_SYMBOL_GPL(vb2_qbuf);
  1649. /**
  1650. * __vb2_wait_for_done_vb() - wait for a buffer to become available
  1651. * for dequeuing
  1652. *
  1653. * Will sleep if required for nonblocking == false.
  1654. */
  1655. static int __vb2_wait_for_done_vb(struct vb2_queue *q, int nonblocking)
  1656. {
  1657. /*
  1658. * All operations on vb_done_list are performed under done_lock
  1659. * spinlock protection. However, buffers may be removed from
  1660. * it and returned to userspace only while holding both driver's
  1661. * lock and the done_lock spinlock. Thus we can be sure that as
  1662. * long as we hold the driver's lock, the list will remain not
  1663. * empty if list_empty() check succeeds.
  1664. */
  1665. for (;;) {
  1666. int ret;
  1667. if (!q->streaming) {
  1668. dprintk(1, "streaming off, will not wait for buffers\n");
  1669. return -EINVAL;
  1670. }
  1671. if (q->error) {
  1672. dprintk(1, "Queue in error state, will not wait for buffers\n");
  1673. return -EIO;
  1674. }
  1675. if (!list_empty(&q->done_list)) {
  1676. /*
  1677. * Found a buffer that we were waiting for.
  1678. */
  1679. break;
  1680. }
  1681. if (nonblocking) {
  1682. dprintk(1, "nonblocking and no buffers to dequeue, "
  1683. "will not wait\n");
  1684. return -EAGAIN;
  1685. }
  1686. /*
  1687. * We are streaming and blocking, wait for another buffer to
  1688. * become ready or for streamoff. Driver's lock is released to
  1689. * allow streamoff or qbuf to be called while waiting.
  1690. */
  1691. call_void_qop(q, wait_prepare, q);
  1692. /*
  1693. * All locks have been released, it is safe to sleep now.
  1694. */
  1695. dprintk(3, "will sleep waiting for buffers\n");
  1696. ret = wait_event_interruptible(q->done_wq,
  1697. !list_empty(&q->done_list) || !q->streaming ||
  1698. q->error);
  1699. /*
  1700. * We need to reevaluate both conditions again after reacquiring
  1701. * the locks or return an error if one occurred.
  1702. */
  1703. call_void_qop(q, wait_finish, q);
  1704. if (ret) {
  1705. dprintk(1, "sleep was interrupted\n");
  1706. return ret;
  1707. }
  1708. }
  1709. return 0;
  1710. }
  1711. /**
  1712. * __vb2_get_done_vb() - get a buffer ready for dequeuing
  1713. *
  1714. * Will sleep if required for nonblocking == false.
  1715. */
  1716. static int __vb2_get_done_vb(struct vb2_queue *q, struct vb2_buffer **vb,
  1717. struct v4l2_buffer *b, int nonblocking)
  1718. {
  1719. unsigned long flags;
  1720. int ret;
  1721. /*
  1722. * Wait for at least one buffer to become available on the done_list.
  1723. */
  1724. ret = __vb2_wait_for_done_vb(q, nonblocking);
  1725. if (ret)
  1726. return ret;
  1727. /*
  1728. * Driver's lock has been held since we last verified that done_list
  1729. * is not empty, so no need for another list_empty(done_list) check.
  1730. */
  1731. spin_lock_irqsave(&q->done_lock, flags);
  1732. *vb = list_first_entry(&q->done_list, struct vb2_buffer, done_entry);
  1733. /*
  1734. * Only remove the buffer from done_list if v4l2_buffer can handle all
  1735. * the planes.
  1736. */
  1737. ret = __verify_planes_array(*vb, b);
  1738. if (!ret)
  1739. list_del(&(*vb)->done_entry);
  1740. spin_unlock_irqrestore(&q->done_lock, flags);
  1741. return ret;
  1742. }
  1743. /**
  1744. * vb2_wait_for_all_buffers() - wait until all buffers are given back to vb2
  1745. * @q: videobuf2 queue
  1746. *
  1747. * This function will wait until all buffers that have been given to the driver
  1748. * by buf_queue() are given back to vb2 with vb2_buffer_done(). It doesn't call
  1749. * wait_prepare, wait_finish pair. It is intended to be called with all locks
  1750. * taken, for example from stop_streaming() callback.
  1751. */
  1752. int vb2_wait_for_all_buffers(struct vb2_queue *q)
  1753. {
  1754. if (!q->streaming) {
  1755. dprintk(1, "streaming off, will not wait for buffers\n");
  1756. return -EINVAL;
  1757. }
  1758. if (q->start_streaming_called)
  1759. wait_event(q->done_wq, !atomic_read(&q->owned_by_drv_count));
  1760. return 0;
  1761. }
  1762. EXPORT_SYMBOL_GPL(vb2_wait_for_all_buffers);
  1763. /**
  1764. * __vb2_dqbuf() - bring back the buffer to the DEQUEUED state
  1765. */
  1766. static void __vb2_dqbuf(struct vb2_buffer *vb)
  1767. {
  1768. struct vb2_queue *q = vb->vb2_queue;
  1769. unsigned int i;
  1770. /* nothing to do if the buffer is already dequeued */
  1771. if (vb->state == VB2_BUF_STATE_DEQUEUED)
  1772. return;
  1773. vb->state = VB2_BUF_STATE_DEQUEUED;
  1774. /* unmap DMABUF buffer */
  1775. if (q->memory == V4L2_MEMORY_DMABUF)
  1776. for (i = 0; i < vb->num_planes; ++i) {
  1777. if (!vb->planes[i].dbuf_mapped)
  1778. continue;
  1779. call_void_memop(vb, unmap_dmabuf, vb->planes[i].mem_priv);
  1780. vb->planes[i].dbuf_mapped = 0;
  1781. }
  1782. }
  1783. static int vb2_internal_dqbuf(struct vb2_queue *q, struct v4l2_buffer *b, bool nonblocking)
  1784. {
  1785. struct vb2_buffer *vb = NULL;
  1786. int ret;
  1787. if (b->type != q->type) {
  1788. dprintk(1, "invalid buffer type\n");
  1789. return -EINVAL;
  1790. }
  1791. ret = __vb2_get_done_vb(q, &vb, b, nonblocking);
  1792. if (ret < 0)
  1793. return ret;
  1794. switch (vb->state) {
  1795. case VB2_BUF_STATE_DONE:
  1796. dprintk(3, "returning done buffer\n");
  1797. break;
  1798. case VB2_BUF_STATE_ERROR:
  1799. dprintk(3, "returning done buffer with errors\n");
  1800. break;
  1801. default:
  1802. dprintk(1, "invalid buffer state\n");
  1803. return -EINVAL;
  1804. }
  1805. call_void_vb_qop(vb, buf_finish, vb);
  1806. /* Fill buffer information for the userspace */
  1807. __fill_v4l2_buffer(vb, b);
  1808. /* Remove from videobuf queue */
  1809. list_del(&vb->queued_entry);
  1810. q->queued_count--;
  1811. /* go back to dequeued state */
  1812. __vb2_dqbuf(vb);
  1813. dprintk(1, "dqbuf of buffer %d, with state %d\n",
  1814. vb->v4l2_buf.index, vb->state);
  1815. return 0;
  1816. }
  1817. /**
  1818. * vb2_dqbuf() - Dequeue a buffer to the userspace
  1819. * @q: videobuf2 queue
  1820. * @b: buffer structure passed from userspace to vidioc_dqbuf handler
  1821. * in driver
  1822. * @nonblocking: if true, this call will not sleep waiting for a buffer if no
  1823. * buffers ready for dequeuing are present. Normally the driver
  1824. * would be passing (file->f_flags & O_NONBLOCK) here
  1825. *
  1826. * Should be called from vidioc_dqbuf ioctl handler of a driver.
  1827. * This function:
  1828. * 1) verifies the passed buffer,
  1829. * 2) calls buf_finish callback in the driver (if provided), in which
  1830. * driver can perform any additional operations that may be required before
  1831. * returning the buffer to userspace, such as cache sync,
  1832. * 3) the buffer struct members are filled with relevant information for
  1833. * the userspace.
  1834. *
  1835. * The return values from this function are intended to be directly returned
  1836. * from vidioc_dqbuf handler in driver.
  1837. */
  1838. int vb2_dqbuf(struct vb2_queue *q, struct v4l2_buffer *b, bool nonblocking)
  1839. {
  1840. if (vb2_fileio_is_active(q)) {
  1841. dprintk(1, "file io in progress\n");
  1842. return -EBUSY;
  1843. }
  1844. return vb2_internal_dqbuf(q, b, nonblocking);
  1845. }
  1846. EXPORT_SYMBOL_GPL(vb2_dqbuf);
  1847. /**
  1848. * __vb2_queue_cancel() - cancel and stop (pause) streaming
  1849. *
  1850. * Removes all queued buffers from driver's queue and all buffers queued by
  1851. * userspace from videobuf's queue. Returns to state after reqbufs.
  1852. */
  1853. static void __vb2_queue_cancel(struct vb2_queue *q)
  1854. {
  1855. unsigned int i;
  1856. /*
  1857. * Tell driver to stop all transactions and release all queued
  1858. * buffers.
  1859. */
  1860. if (q->start_streaming_called)
  1861. call_void_qop(q, stop_streaming, q);
  1862. /*
  1863. * If you see this warning, then the driver isn't cleaning up properly
  1864. * in stop_streaming(). See the stop_streaming() documentation in
  1865. * videobuf2-core.h for more information how buffers should be returned
  1866. * to vb2 in stop_streaming().
  1867. */
  1868. if (WARN_ON(atomic_read(&q->owned_by_drv_count))) {
  1869. for (i = 0; i < q->num_buffers; ++i)
  1870. if (q->bufs[i]->state == VB2_BUF_STATE_ACTIVE)
  1871. vb2_buffer_done(q->bufs[i], VB2_BUF_STATE_ERROR);
  1872. /* Must be zero now */
  1873. WARN_ON(atomic_read(&q->owned_by_drv_count));
  1874. }
  1875. q->streaming = 0;
  1876. q->start_streaming_called = 0;
  1877. q->queued_count = 0;
  1878. q->error = 0;
  1879. /*
  1880. * Remove all buffers from videobuf's list...
  1881. */
  1882. INIT_LIST_HEAD(&q->queued_list);
  1883. /*
  1884. * ...and done list; userspace will not receive any buffers it
  1885. * has not already dequeued before initiating cancel.
  1886. */
  1887. INIT_LIST_HEAD(&q->done_list);
  1888. atomic_set(&q->owned_by_drv_count, 0);
  1889. wake_up_all(&q->done_wq);
  1890. /*
  1891. * Reinitialize all buffers for next use.
  1892. * Make sure to call buf_finish for any queued buffers. Normally
  1893. * that's done in dqbuf, but that's not going to happen when we
  1894. * cancel the whole queue. Note: this code belongs here, not in
  1895. * __vb2_dqbuf() since in vb2_internal_dqbuf() there is a critical
  1896. * call to __fill_v4l2_buffer() after buf_finish(). That order can't
  1897. * be changed, so we can't move the buf_finish() to __vb2_dqbuf().
  1898. */
  1899. for (i = 0; i < q->num_buffers; ++i) {
  1900. struct vb2_buffer *vb = q->bufs[i];
  1901. if (vb->state != VB2_BUF_STATE_DEQUEUED) {
  1902. vb->state = VB2_BUF_STATE_PREPARED;
  1903. call_void_vb_qop(vb, buf_finish, vb);
  1904. }
  1905. __vb2_dqbuf(vb);
  1906. }
  1907. }
  1908. static int vb2_internal_streamon(struct vb2_queue *q, enum v4l2_buf_type type)
  1909. {
  1910. int ret;
  1911. if (type != q->type) {
  1912. dprintk(1, "invalid stream type\n");
  1913. return -EINVAL;
  1914. }
  1915. if (q->streaming) {
  1916. dprintk(3, "already streaming\n");
  1917. return 0;
  1918. }
  1919. if (!q->num_buffers) {
  1920. dprintk(1, "no buffers have been allocated\n");
  1921. return -EINVAL;
  1922. }
  1923. if (q->num_buffers < q->min_buffers_needed) {
  1924. dprintk(1, "need at least %u allocated buffers\n",
  1925. q->min_buffers_needed);
  1926. return -EINVAL;
  1927. }
  1928. /*
  1929. * Tell driver to start streaming provided sufficient buffers
  1930. * are available.
  1931. */
  1932. if (q->queued_count >= q->min_buffers_needed) {
  1933. ret = vb2_start_streaming(q);
  1934. if (ret) {
  1935. __vb2_queue_cancel(q);
  1936. return ret;
  1937. }
  1938. }
  1939. q->streaming = 1;
  1940. dprintk(3, "successful\n");
  1941. return 0;
  1942. }
  1943. /**
  1944. * vb2_queue_error() - signal a fatal error on the queue
  1945. * @q: videobuf2 queue
  1946. *
  1947. * Flag that a fatal unrecoverable error has occurred and wake up all processes
  1948. * waiting on the queue. Polling will now set POLLERR and queuing and dequeuing
  1949. * buffers will return -EIO.
  1950. *
  1951. * The error flag will be cleared when cancelling the queue, either from
  1952. * vb2_streamoff or vb2_queue_release. Drivers should thus not call this
  1953. * function before starting the stream, otherwise the error flag will remain set
  1954. * until the queue is released when closing the device node.
  1955. */
  1956. void vb2_queue_error(struct vb2_queue *q)
  1957. {
  1958. q->error = 1;
  1959. wake_up_all(&q->done_wq);
  1960. }
  1961. EXPORT_SYMBOL_GPL(vb2_queue_error);
  1962. /**
  1963. * vb2_streamon - start streaming
  1964. * @q: videobuf2 queue
  1965. * @type: type argument passed from userspace to vidioc_streamon handler
  1966. *
  1967. * Should be called from vidioc_streamon handler of a driver.
  1968. * This function:
  1969. * 1) verifies current state
  1970. * 2) passes any previously queued buffers to the driver and starts streaming
  1971. *
  1972. * The return values from this function are intended to be directly returned
  1973. * from vidioc_streamon handler in the driver.
  1974. */
  1975. int vb2_streamon(struct vb2_queue *q, enum v4l2_buf_type type)
  1976. {
  1977. if (vb2_fileio_is_active(q)) {
  1978. dprintk(1, "file io in progress\n");
  1979. return -EBUSY;
  1980. }
  1981. return vb2_internal_streamon(q, type);
  1982. }
  1983. EXPORT_SYMBOL_GPL(vb2_streamon);
  1984. static int vb2_internal_streamoff(struct vb2_queue *q, enum v4l2_buf_type type)
  1985. {
  1986. if (type != q->type) {
  1987. dprintk(1, "invalid stream type\n");
  1988. return -EINVAL;
  1989. }
  1990. /*
  1991. * Cancel will pause streaming and remove all buffers from the driver
  1992. * and videobuf, effectively returning control over them to userspace.
  1993. *
  1994. * Note that we do this even if q->streaming == 0: if you prepare or
  1995. * queue buffers, and then call streamoff without ever having called
  1996. * streamon, you would still expect those buffers to be returned to
  1997. * their normal dequeued state.
  1998. */
  1999. __vb2_queue_cancel(q);
  2000. q->waiting_for_buffers = !V4L2_TYPE_IS_OUTPUT(q->type);
  2001. dprintk(3, "successful\n");
  2002. return 0;
  2003. }
  2004. /**
  2005. * vb2_streamoff - stop streaming
  2006. * @q: videobuf2 queue
  2007. * @type: type argument passed from userspace to vidioc_streamoff handler
  2008. *
  2009. * Should be called from vidioc_streamoff handler of a driver.
  2010. * This function:
  2011. * 1) verifies current state,
  2012. * 2) stop streaming and dequeues any queued buffers, including those previously
  2013. * passed to the driver (after waiting for the driver to finish).
  2014. *
  2015. * This call can be used for pausing playback.
  2016. * The return values from this function are intended to be directly returned
  2017. * from vidioc_streamoff handler in the driver
  2018. */
  2019. int vb2_streamoff(struct vb2_queue *q, enum v4l2_buf_type type)
  2020. {
  2021. if (vb2_fileio_is_active(q)) {
  2022. dprintk(1, "file io in progress\n");
  2023. return -EBUSY;
  2024. }
  2025. return vb2_internal_streamoff(q, type);
  2026. }
  2027. EXPORT_SYMBOL_GPL(vb2_streamoff);
  2028. /**
  2029. * __find_plane_by_offset() - find plane associated with the given offset off
  2030. */
  2031. static int __find_plane_by_offset(struct vb2_queue *q, unsigned long off,
  2032. unsigned int *_buffer, unsigned int *_plane)
  2033. {
  2034. struct vb2_buffer *vb;
  2035. unsigned int buffer, plane;
  2036. /*
  2037. * Go over all buffers and their planes, comparing the given offset
  2038. * with an offset assigned to each plane. If a match is found,
  2039. * return its buffer and plane numbers.
  2040. */
  2041. for (buffer = 0; buffer < q->num_buffers; ++buffer) {
  2042. vb = q->bufs[buffer];
  2043. for (plane = 0; plane < vb->num_planes; ++plane) {
  2044. if (vb->v4l2_planes[plane].m.mem_offset == off) {
  2045. *_buffer = buffer;
  2046. *_plane = plane;
  2047. return 0;
  2048. }
  2049. }
  2050. }
  2051. return -EINVAL;
  2052. }
  2053. /**
  2054. * vb2_expbuf() - Export a buffer as a file descriptor
  2055. * @q: videobuf2 queue
  2056. * @eb: export buffer structure passed from userspace to vidioc_expbuf
  2057. * handler in driver
  2058. *
  2059. * The return values from this function are intended to be directly returned
  2060. * from vidioc_expbuf handler in driver.
  2061. */
  2062. int vb2_expbuf(struct vb2_queue *q, struct v4l2_exportbuffer *eb)
  2063. {
  2064. struct vb2_buffer *vb = NULL;
  2065. struct vb2_plane *vb_plane;
  2066. int ret;
  2067. struct dma_buf *dbuf;
  2068. if (q->memory != V4L2_MEMORY_MMAP) {
  2069. dprintk(1, "queue is not currently set up for mmap\n");
  2070. return -EINVAL;
  2071. }
  2072. if (!q->mem_ops->get_dmabuf) {
  2073. dprintk(1, "queue does not support DMA buffer exporting\n");
  2074. return -EINVAL;
  2075. }
  2076. if (eb->flags & ~(O_CLOEXEC | O_ACCMODE)) {
  2077. dprintk(1, "queue does support only O_CLOEXEC and access mode flags\n");
  2078. return -EINVAL;
  2079. }
  2080. if (eb->type != q->type) {
  2081. dprintk(1, "invalid buffer type\n");
  2082. return -EINVAL;
  2083. }
  2084. if (eb->index >= q->num_buffers) {
  2085. dprintk(1, "buffer index out of range\n");
  2086. return -EINVAL;
  2087. }
  2088. vb = q->bufs[eb->index];
  2089. if (eb->plane >= vb->num_planes) {
  2090. dprintk(1, "buffer plane out of range\n");
  2091. return -EINVAL;
  2092. }
  2093. if (vb2_fileio_is_active(q)) {
  2094. dprintk(1, "expbuf: file io in progress\n");
  2095. return -EBUSY;
  2096. }
  2097. vb_plane = &vb->planes[eb->plane];
  2098. dbuf = call_ptr_memop(vb, get_dmabuf, vb_plane->mem_priv, eb->flags & O_ACCMODE);
  2099. if (IS_ERR_OR_NULL(dbuf)) {
  2100. dprintk(1, "failed to export buffer %d, plane %d\n",
  2101. eb->index, eb->plane);
  2102. return -EINVAL;
  2103. }
  2104. ret = dma_buf_fd(dbuf, eb->flags & ~O_ACCMODE);
  2105. if (ret < 0) {
  2106. dprintk(3, "buffer %d, plane %d failed to export (%d)\n",
  2107. eb->index, eb->plane, ret);
  2108. dma_buf_put(dbuf);
  2109. return ret;
  2110. }
  2111. dprintk(3, "buffer %d, plane %d exported as %d descriptor\n",
  2112. eb->index, eb->plane, ret);
  2113. eb->fd = ret;
  2114. return 0;
  2115. }
  2116. EXPORT_SYMBOL_GPL(vb2_expbuf);
  2117. /**
  2118. * vb2_mmap() - map video buffers into application address space
  2119. * @q: videobuf2 queue
  2120. * @vma: vma passed to the mmap file operation handler in the driver
  2121. *
  2122. * Should be called from mmap file operation handler of a driver.
  2123. * This function maps one plane of one of the available video buffers to
  2124. * userspace. To map whole video memory allocated on reqbufs, this function
  2125. * has to be called once per each plane per each buffer previously allocated.
  2126. *
  2127. * When the userspace application calls mmap, it passes to it an offset returned
  2128. * to it earlier by the means of vidioc_querybuf handler. That offset acts as
  2129. * a "cookie", which is then used to identify the plane to be mapped.
  2130. * This function finds a plane with a matching offset and a mapping is performed
  2131. * by the means of a provided memory operation.
  2132. *
  2133. * The return values from this function are intended to be directly returned
  2134. * from the mmap handler in driver.
  2135. */
  2136. int vb2_mmap(struct vb2_queue *q, struct vm_area_struct *vma)
  2137. {
  2138. unsigned long off = vma->vm_pgoff << PAGE_SHIFT;
  2139. struct vb2_buffer *vb;
  2140. unsigned int buffer = 0, plane = 0;
  2141. int ret;
  2142. unsigned long length;
  2143. if (q->memory != V4L2_MEMORY_MMAP) {
  2144. dprintk(1, "queue is not currently set up for mmap\n");
  2145. return -EINVAL;
  2146. }
  2147. /*
  2148. * Check memory area access mode.
  2149. */
  2150. if (!(vma->vm_flags & VM_SHARED)) {
  2151. dprintk(1, "invalid vma flags, VM_SHARED needed\n");
  2152. return -EINVAL;
  2153. }
  2154. if (V4L2_TYPE_IS_OUTPUT(q->type)) {
  2155. if (!(vma->vm_flags & VM_WRITE)) {
  2156. dprintk(1, "invalid vma flags, VM_WRITE needed\n");
  2157. return -EINVAL;
  2158. }
  2159. } else {
  2160. if (!(vma->vm_flags & VM_READ)) {
  2161. dprintk(1, "invalid vma flags, VM_READ needed\n");
  2162. return -EINVAL;
  2163. }
  2164. }
  2165. if (vb2_fileio_is_active(q)) {
  2166. dprintk(1, "mmap: file io in progress\n");
  2167. return -EBUSY;
  2168. }
  2169. /*
  2170. * Find the plane corresponding to the offset passed by userspace.
  2171. */
  2172. ret = __find_plane_by_offset(q, off, &buffer, &plane);
  2173. if (ret)
  2174. return ret;
  2175. vb = q->bufs[buffer];
  2176. /*
  2177. * MMAP requires page_aligned buffers.
  2178. * The buffer length was page_aligned at __vb2_buf_mem_alloc(),
  2179. * so, we need to do the same here.
  2180. */
  2181. length = PAGE_ALIGN(vb->v4l2_planes[plane].length);
  2182. if (length < (vma->vm_end - vma->vm_start)) {
  2183. dprintk(1,
  2184. "MMAP invalid, as it would overflow buffer length\n");
  2185. return -EINVAL;
  2186. }
  2187. mutex_lock(&q->mmap_lock);
  2188. ret = call_memop(vb, mmap, vb->planes[plane].mem_priv, vma);
  2189. mutex_unlock(&q->mmap_lock);
  2190. if (ret)
  2191. return ret;
  2192. dprintk(3, "buffer %d, plane %d successfully mapped\n", buffer, plane);
  2193. return 0;
  2194. }
  2195. EXPORT_SYMBOL_GPL(vb2_mmap);
  2196. #ifndef CONFIG_MMU
  2197. unsigned long vb2_get_unmapped_area(struct vb2_queue *q,
  2198. unsigned long addr,
  2199. unsigned long len,
  2200. unsigned long pgoff,
  2201. unsigned long flags)
  2202. {
  2203. unsigned long off = pgoff << PAGE_SHIFT;
  2204. struct vb2_buffer *vb;
  2205. unsigned int buffer, plane;
  2206. void *vaddr;
  2207. int ret;
  2208. if (q->memory != V4L2_MEMORY_MMAP) {
  2209. dprintk(1, "queue is not currently set up for mmap\n");
  2210. return -EINVAL;
  2211. }
  2212. /*
  2213. * Find the plane corresponding to the offset passed by userspace.
  2214. */
  2215. ret = __find_plane_by_offset(q, off, &buffer, &plane);
  2216. if (ret)
  2217. return ret;
  2218. vb = q->bufs[buffer];
  2219. vaddr = vb2_plane_vaddr(vb, plane);
  2220. return vaddr ? (unsigned long)vaddr : -EINVAL;
  2221. }
  2222. EXPORT_SYMBOL_GPL(vb2_get_unmapped_area);
  2223. #endif
  2224. static int __vb2_init_fileio(struct vb2_queue *q, int read);
  2225. static int __vb2_cleanup_fileio(struct vb2_queue *q);
  2226. /**
  2227. * vb2_poll() - implements poll userspace operation
  2228. * @q: videobuf2 queue
  2229. * @file: file argument passed to the poll file operation handler
  2230. * @wait: wait argument passed to the poll file operation handler
  2231. *
  2232. * This function implements poll file operation handler for a driver.
  2233. * For CAPTURE queues, if a buffer is ready to be dequeued, the userspace will
  2234. * be informed that the file descriptor of a video device is available for
  2235. * reading.
  2236. * For OUTPUT queues, if a buffer is ready to be dequeued, the file descriptor
  2237. * will be reported as available for writing.
  2238. *
  2239. * If the driver uses struct v4l2_fh, then vb2_poll() will also check for any
  2240. * pending events.
  2241. *
  2242. * The return values from this function are intended to be directly returned
  2243. * from poll handler in driver.
  2244. */
  2245. unsigned int vb2_poll(struct vb2_queue *q, struct file *file, poll_table *wait)
  2246. {
  2247. struct video_device *vfd = video_devdata(file);
  2248. unsigned long req_events = poll_requested_events(wait);
  2249. struct vb2_buffer *vb = NULL;
  2250. unsigned int res = 0;
  2251. unsigned long flags;
  2252. if (test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags)) {
  2253. struct v4l2_fh *fh = file->private_data;
  2254. if (v4l2_event_pending(fh))
  2255. res = POLLPRI;
  2256. else if (req_events & POLLPRI)
  2257. poll_wait(file, &fh->wait, wait);
  2258. }
  2259. if (!V4L2_TYPE_IS_OUTPUT(q->type) && !(req_events & (POLLIN | POLLRDNORM)))
  2260. return res;
  2261. if (V4L2_TYPE_IS_OUTPUT(q->type) && !(req_events & (POLLOUT | POLLWRNORM)))
  2262. return res;
  2263. /*
  2264. * Start file I/O emulator only if streaming API has not been used yet.
  2265. */
  2266. if (q->num_buffers == 0 && !vb2_fileio_is_active(q)) {
  2267. if (!V4L2_TYPE_IS_OUTPUT(q->type) && (q->io_modes & VB2_READ) &&
  2268. (req_events & (POLLIN | POLLRDNORM))) {
  2269. if (__vb2_init_fileio(q, 1))
  2270. return res | POLLERR;
  2271. }
  2272. if (V4L2_TYPE_IS_OUTPUT(q->type) && (q->io_modes & VB2_WRITE) &&
  2273. (req_events & (POLLOUT | POLLWRNORM))) {
  2274. if (__vb2_init_fileio(q, 0))
  2275. return res | POLLERR;
  2276. /*
  2277. * Write to OUTPUT queue can be done immediately.
  2278. */
  2279. return res | POLLOUT | POLLWRNORM;
  2280. }
  2281. }
  2282. /*
  2283. * There is nothing to wait for if the queue isn't streaming, or if the
  2284. * error flag is set.
  2285. */
  2286. if (!vb2_is_streaming(q) || q->error)
  2287. return res | POLLERR;
  2288. /*
  2289. * For compatibility with vb1: if QBUF hasn't been called yet, then
  2290. * return POLLERR as well. This only affects capture queues, output
  2291. * queues will always initialize waiting_for_buffers to false.
  2292. */
  2293. if (q->waiting_for_buffers)
  2294. return res | POLLERR;
  2295. /*
  2296. * For output streams you can write as long as there are fewer buffers
  2297. * queued than there are buffers available.
  2298. */
  2299. if (V4L2_TYPE_IS_OUTPUT(q->type) && q->queued_count < q->num_buffers)
  2300. return res | POLLOUT | POLLWRNORM;
  2301. if (list_empty(&q->done_list))
  2302. poll_wait(file, &q->done_wq, wait);
  2303. /*
  2304. * Take first buffer available for dequeuing.
  2305. */
  2306. spin_lock_irqsave(&q->done_lock, flags);
  2307. if (!list_empty(&q->done_list))
  2308. vb = list_first_entry(&q->done_list, struct vb2_buffer,
  2309. done_entry);
  2310. spin_unlock_irqrestore(&q->done_lock, flags);
  2311. if (vb && (vb->state == VB2_BUF_STATE_DONE
  2312. || vb->state == VB2_BUF_STATE_ERROR)) {
  2313. return (V4L2_TYPE_IS_OUTPUT(q->type)) ?
  2314. res | POLLOUT | POLLWRNORM :
  2315. res | POLLIN | POLLRDNORM;
  2316. }
  2317. return res;
  2318. }
  2319. EXPORT_SYMBOL_GPL(vb2_poll);
  2320. /**
  2321. * vb2_queue_init() - initialize a videobuf2 queue
  2322. * @q: videobuf2 queue; this structure should be allocated in driver
  2323. *
  2324. * The vb2_queue structure should be allocated by the driver. The driver is
  2325. * responsible of clearing it's content and setting initial values for some
  2326. * required entries before calling this function.
  2327. * q->ops, q->mem_ops, q->type and q->io_modes are mandatory. Please refer
  2328. * to the struct vb2_queue description in include/media/videobuf2-core.h
  2329. * for more information.
  2330. */
  2331. int vb2_queue_init(struct vb2_queue *q)
  2332. {
  2333. /*
  2334. * Sanity check
  2335. */
  2336. if (WARN_ON(!q) ||
  2337. WARN_ON(!q->ops) ||
  2338. WARN_ON(!q->mem_ops) ||
  2339. WARN_ON(!q->type) ||
  2340. WARN_ON(!q->io_modes) ||
  2341. WARN_ON(!q->ops->queue_setup) ||
  2342. WARN_ON(!q->ops->buf_queue) ||
  2343. WARN_ON(q->timestamp_flags &
  2344. ~(V4L2_BUF_FLAG_TIMESTAMP_MASK |
  2345. V4L2_BUF_FLAG_TSTAMP_SRC_MASK)))
  2346. return -EINVAL;
  2347. /* Warn that the driver should choose an appropriate timestamp type */
  2348. WARN_ON((q->timestamp_flags & V4L2_BUF_FLAG_TIMESTAMP_MASK) ==
  2349. V4L2_BUF_FLAG_TIMESTAMP_UNKNOWN);
  2350. INIT_LIST_HEAD(&q->queued_list);
  2351. INIT_LIST_HEAD(&q->done_list);
  2352. spin_lock_init(&q->done_lock);
  2353. mutex_init(&q->mmap_lock);
  2354. init_waitqueue_head(&q->done_wq);
  2355. if (q->buf_struct_size == 0)
  2356. q->buf_struct_size = sizeof(struct vb2_buffer);
  2357. return 0;
  2358. }
  2359. EXPORT_SYMBOL_GPL(vb2_queue_init);
  2360. /**
  2361. * vb2_queue_release() - stop streaming, release the queue and free memory
  2362. * @q: videobuf2 queue
  2363. *
  2364. * This function stops streaming and performs necessary clean ups, including
  2365. * freeing video buffer memory. The driver is responsible for freeing
  2366. * the vb2_queue structure itself.
  2367. */
  2368. void vb2_queue_release(struct vb2_queue *q)
  2369. {
  2370. __vb2_cleanup_fileio(q);
  2371. __vb2_queue_cancel(q);
  2372. mutex_lock(&q->mmap_lock);
  2373. __vb2_queue_free(q, q->num_buffers);
  2374. mutex_unlock(&q->mmap_lock);
  2375. }
  2376. EXPORT_SYMBOL_GPL(vb2_queue_release);
  2377. /**
  2378. * struct vb2_fileio_buf - buffer context used by file io emulator
  2379. *
  2380. * vb2 provides a compatibility layer and emulator of file io (read and
  2381. * write) calls on top of streaming API. This structure is used for
  2382. * tracking context related to the buffers.
  2383. */
  2384. struct vb2_fileio_buf {
  2385. void *vaddr;
  2386. unsigned int size;
  2387. unsigned int pos;
  2388. unsigned int queued:1;
  2389. };
  2390. /**
  2391. * struct vb2_fileio_data - queue context used by file io emulator
  2392. *
  2393. * @cur_index: the index of the buffer currently being read from or
  2394. * written to. If equal to q->num_buffers then a new buffer
  2395. * must be dequeued.
  2396. * @initial_index: in the read() case all buffers are queued up immediately
  2397. * in __vb2_init_fileio() and __vb2_perform_fileio() just cycles
  2398. * buffers. However, in the write() case no buffers are initially
  2399. * queued, instead whenever a buffer is full it is queued up by
  2400. * __vb2_perform_fileio(). Only once all available buffers have
  2401. * been queued up will __vb2_perform_fileio() start to dequeue
  2402. * buffers. This means that initially __vb2_perform_fileio()
  2403. * needs to know what buffer index to use when it is queuing up
  2404. * the buffers for the first time. That initial index is stored
  2405. * in this field. Once it is equal to q->num_buffers all
  2406. * available buffers have been queued and __vb2_perform_fileio()
  2407. * should start the normal dequeue/queue cycle.
  2408. *
  2409. * vb2 provides a compatibility layer and emulator of file io (read and
  2410. * write) calls on top of streaming API. For proper operation it required
  2411. * this structure to save the driver state between each call of the read
  2412. * or write function.
  2413. */
  2414. struct vb2_fileio_data {
  2415. struct v4l2_requestbuffers req;
  2416. struct v4l2_plane p;
  2417. struct v4l2_buffer b;
  2418. struct vb2_fileio_buf bufs[VIDEO_MAX_FRAME];
  2419. unsigned int cur_index;
  2420. unsigned int initial_index;
  2421. unsigned int q_count;
  2422. unsigned int dq_count;
  2423. unsigned int flags;
  2424. };
  2425. /**
  2426. * __vb2_init_fileio() - initialize file io emulator
  2427. * @q: videobuf2 queue
  2428. * @read: mode selector (1 means read, 0 means write)
  2429. */
  2430. static int __vb2_init_fileio(struct vb2_queue *q, int read)
  2431. {
  2432. struct vb2_fileio_data *fileio;
  2433. int i, ret;
  2434. unsigned int count = 0;
  2435. /*
  2436. * Sanity check
  2437. */
  2438. if (WARN_ON((read && !(q->io_modes & VB2_READ)) ||
  2439. (!read && !(q->io_modes & VB2_WRITE))))
  2440. return -EINVAL;
  2441. /*
  2442. * Check if device supports mapping buffers to kernel virtual space.
  2443. */
  2444. if (!q->mem_ops->vaddr)
  2445. return -EBUSY;
  2446. /*
  2447. * Check if streaming api has not been already activated.
  2448. */
  2449. if (q->streaming || q->num_buffers > 0)
  2450. return -EBUSY;
  2451. /*
  2452. * Start with count 1, driver can increase it in queue_setup()
  2453. */
  2454. count = 1;
  2455. dprintk(3, "setting up file io: mode %s, count %d, flags %08x\n",
  2456. (read) ? "read" : "write", count, q->io_flags);
  2457. fileio = kzalloc(sizeof(struct vb2_fileio_data), GFP_KERNEL);
  2458. if (fileio == NULL)
  2459. return -ENOMEM;
  2460. fileio->flags = q->io_flags;
  2461. /*
  2462. * Request buffers and use MMAP type to force driver
  2463. * to allocate buffers by itself.
  2464. */
  2465. fileio->req.count = count;
  2466. fileio->req.memory = V4L2_MEMORY_MMAP;
  2467. fileio->req.type = q->type;
  2468. q->fileio = fileio;
  2469. ret = __reqbufs(q, &fileio->req);
  2470. if (ret)
  2471. goto err_kfree;
  2472. /*
  2473. * Check if plane_count is correct
  2474. * (multiplane buffers are not supported).
  2475. */
  2476. if (q->bufs[0]->num_planes != 1) {
  2477. ret = -EBUSY;
  2478. goto err_reqbufs;
  2479. }
  2480. /*
  2481. * Get kernel address of each buffer.
  2482. */
  2483. for (i = 0; i < q->num_buffers; i++) {
  2484. fileio->bufs[i].vaddr = vb2_plane_vaddr(q->bufs[i], 0);
  2485. if (fileio->bufs[i].vaddr == NULL) {
  2486. ret = -EINVAL;
  2487. goto err_reqbufs;
  2488. }
  2489. fileio->bufs[i].size = vb2_plane_size(q->bufs[i], 0);
  2490. }
  2491. /*
  2492. * Read mode requires pre queuing of all buffers.
  2493. */
  2494. if (read) {
  2495. bool is_multiplanar = V4L2_TYPE_IS_MULTIPLANAR(q->type);
  2496. /*
  2497. * Queue all buffers.
  2498. */
  2499. for (i = 0; i < q->num_buffers; i++) {
  2500. struct v4l2_buffer *b = &fileio->b;
  2501. memset(b, 0, sizeof(*b));
  2502. b->type = q->type;
  2503. if (is_multiplanar) {
  2504. memset(&fileio->p, 0, sizeof(fileio->p));
  2505. b->m.planes = &fileio->p;
  2506. b->length = 1;
  2507. }
  2508. b->memory = q->memory;
  2509. b->index = i;
  2510. ret = vb2_internal_qbuf(q, b);
  2511. if (ret)
  2512. goto err_reqbufs;
  2513. fileio->bufs[i].queued = 1;
  2514. }
  2515. /*
  2516. * All buffers have been queued, so mark that by setting
  2517. * initial_index to q->num_buffers
  2518. */
  2519. fileio->initial_index = q->num_buffers;
  2520. fileio->cur_index = q->num_buffers;
  2521. }
  2522. /*
  2523. * Start streaming.
  2524. */
  2525. ret = vb2_internal_streamon(q, q->type);
  2526. if (ret)
  2527. goto err_reqbufs;
  2528. return ret;
  2529. err_reqbufs:
  2530. fileio->req.count = 0;
  2531. __reqbufs(q, &fileio->req);
  2532. err_kfree:
  2533. q->fileio = NULL;
  2534. kfree(fileio);
  2535. return ret;
  2536. }
  2537. /**
  2538. * __vb2_cleanup_fileio() - free resourced used by file io emulator
  2539. * @q: videobuf2 queue
  2540. */
  2541. static int __vb2_cleanup_fileio(struct vb2_queue *q)
  2542. {
  2543. struct vb2_fileio_data *fileio = q->fileio;
  2544. if (fileio) {
  2545. vb2_internal_streamoff(q, q->type);
  2546. q->fileio = NULL;
  2547. fileio->req.count = 0;
  2548. vb2_reqbufs(q, &fileio->req);
  2549. kfree(fileio);
  2550. dprintk(3, "file io emulator closed\n");
  2551. }
  2552. return 0;
  2553. }
  2554. /**
  2555. * __vb2_perform_fileio() - perform a single file io (read or write) operation
  2556. * @q: videobuf2 queue
  2557. * @data: pointed to target userspace buffer
  2558. * @count: number of bytes to read or write
  2559. * @ppos: file handle position tracking pointer
  2560. * @nonblock: mode selector (1 means blocking calls, 0 means nonblocking)
  2561. * @read: access mode selector (1 means read, 0 means write)
  2562. */
  2563. static size_t __vb2_perform_fileio(struct vb2_queue *q, char __user *data, size_t count,
  2564. loff_t *ppos, int nonblock, int read)
  2565. {
  2566. struct vb2_fileio_data *fileio;
  2567. struct vb2_fileio_buf *buf;
  2568. bool is_multiplanar = V4L2_TYPE_IS_MULTIPLANAR(q->type);
  2569. /*
  2570. * When using write() to write data to an output video node the vb2 core
  2571. * should set timestamps if V4L2_BUF_FLAG_TIMESTAMP_COPY is set. Nobody
  2572. * else is able to provide this information with the write() operation.
  2573. */
  2574. bool set_timestamp = !read &&
  2575. (q->timestamp_flags & V4L2_BUF_FLAG_TIMESTAMP_MASK) ==
  2576. V4L2_BUF_FLAG_TIMESTAMP_COPY;
  2577. int ret, index;
  2578. dprintk(3, "mode %s, offset %ld, count %zd, %sblocking\n",
  2579. read ? "read" : "write", (long)*ppos, count,
  2580. nonblock ? "non" : "");
  2581. if (!data)
  2582. return -EINVAL;
  2583. /*
  2584. * Initialize emulator on first call.
  2585. */
  2586. if (!vb2_fileio_is_active(q)) {
  2587. ret = __vb2_init_fileio(q, read);
  2588. dprintk(3, "vb2_init_fileio result: %d\n", ret);
  2589. if (ret)
  2590. return ret;
  2591. }
  2592. fileio = q->fileio;
  2593. /*
  2594. * Check if we need to dequeue the buffer.
  2595. */
  2596. index = fileio->cur_index;
  2597. if (index >= q->num_buffers) {
  2598. /*
  2599. * Call vb2_dqbuf to get buffer back.
  2600. */
  2601. memset(&fileio->b, 0, sizeof(fileio->b));
  2602. fileio->b.type = q->type;
  2603. fileio->b.memory = q->memory;
  2604. if (is_multiplanar) {
  2605. memset(&fileio->p, 0, sizeof(fileio->p));
  2606. fileio->b.m.planes = &fileio->p;
  2607. fileio->b.length = 1;
  2608. }
  2609. ret = vb2_internal_dqbuf(q, &fileio->b, nonblock);
  2610. dprintk(5, "vb2_dqbuf result: %d\n", ret);
  2611. if (ret)
  2612. return ret;
  2613. fileio->dq_count += 1;
  2614. fileio->cur_index = index = fileio->b.index;
  2615. buf = &fileio->bufs[index];
  2616. /*
  2617. * Get number of bytes filled by the driver
  2618. */
  2619. buf->pos = 0;
  2620. buf->queued = 0;
  2621. buf->size = read ? vb2_get_plane_payload(q->bufs[index], 0)
  2622. : vb2_plane_size(q->bufs[index], 0);
  2623. /* Compensate for data_offset on read in the multiplanar case. */
  2624. if (is_multiplanar && read &&
  2625. fileio->b.m.planes[0].data_offset < buf->size) {
  2626. buf->pos = fileio->b.m.planes[0].data_offset;
  2627. buf->size -= buf->pos;
  2628. }
  2629. } else {
  2630. buf = &fileio->bufs[index];
  2631. }
  2632. /*
  2633. * Limit count on last few bytes of the buffer.
  2634. */
  2635. if (buf->pos + count > buf->size) {
  2636. count = buf->size - buf->pos;
  2637. dprintk(5, "reducing read count: %zd\n", count);
  2638. }
  2639. /*
  2640. * Transfer data to userspace.
  2641. */
  2642. dprintk(3, "copying %zd bytes - buffer %d, offset %u\n",
  2643. count, index, buf->pos);
  2644. if (read)
  2645. ret = copy_to_user(data, buf->vaddr + buf->pos, count);
  2646. else
  2647. ret = copy_from_user(buf->vaddr + buf->pos, data, count);
  2648. if (ret) {
  2649. dprintk(3, "error copying data\n");
  2650. return -EFAULT;
  2651. }
  2652. /*
  2653. * Update counters.
  2654. */
  2655. buf->pos += count;
  2656. *ppos += count;
  2657. /*
  2658. * Queue next buffer if required.
  2659. */
  2660. if (buf->pos == buf->size ||
  2661. (!read && (fileio->flags & VB2_FILEIO_WRITE_IMMEDIATELY))) {
  2662. /*
  2663. * Check if this is the last buffer to read.
  2664. */
  2665. if (read && (fileio->flags & VB2_FILEIO_READ_ONCE) &&
  2666. fileio->dq_count == 1) {
  2667. dprintk(3, "read limit reached\n");
  2668. return __vb2_cleanup_fileio(q);
  2669. }
  2670. /*
  2671. * Call vb2_qbuf and give buffer to the driver.
  2672. */
  2673. memset(&fileio->b, 0, sizeof(fileio->b));
  2674. fileio->b.type = q->type;
  2675. fileio->b.memory = q->memory;
  2676. fileio->b.index = index;
  2677. fileio->b.bytesused = buf->pos;
  2678. if (is_multiplanar) {
  2679. memset(&fileio->p, 0, sizeof(fileio->p));
  2680. fileio->p.bytesused = buf->pos;
  2681. fileio->b.m.planes = &fileio->p;
  2682. fileio->b.length = 1;
  2683. }
  2684. if (set_timestamp)
  2685. v4l2_get_timestamp(&fileio->b.timestamp);
  2686. ret = vb2_internal_qbuf(q, &fileio->b);
  2687. dprintk(5, "vb2_dbuf result: %d\n", ret);
  2688. if (ret)
  2689. return ret;
  2690. /*
  2691. * Buffer has been queued, update the status
  2692. */
  2693. buf->pos = 0;
  2694. buf->queued = 1;
  2695. buf->size = vb2_plane_size(q->bufs[index], 0);
  2696. fileio->q_count += 1;
  2697. /*
  2698. * If we are queuing up buffers for the first time, then
  2699. * increase initial_index by one.
  2700. */
  2701. if (fileio->initial_index < q->num_buffers)
  2702. fileio->initial_index++;
  2703. /*
  2704. * The next buffer to use is either a buffer that's going to be
  2705. * queued for the first time (initial_index < q->num_buffers)
  2706. * or it is equal to q->num_buffers, meaning that the next
  2707. * time we need to dequeue a buffer since we've now queued up
  2708. * all the 'first time' buffers.
  2709. */
  2710. fileio->cur_index = fileio->initial_index;
  2711. }
  2712. /*
  2713. * Return proper number of bytes processed.
  2714. */
  2715. if (ret == 0)
  2716. ret = count;
  2717. return ret;
  2718. }
  2719. size_t vb2_read(struct vb2_queue *q, char __user *data, size_t count,
  2720. loff_t *ppos, int nonblocking)
  2721. {
  2722. return __vb2_perform_fileio(q, data, count, ppos, nonblocking, 1);
  2723. }
  2724. EXPORT_SYMBOL_GPL(vb2_read);
  2725. size_t vb2_write(struct vb2_queue *q, const char __user *data, size_t count,
  2726. loff_t *ppos, int nonblocking)
  2727. {
  2728. return __vb2_perform_fileio(q, (char __user *) data, count,
  2729. ppos, nonblocking, 0);
  2730. }
  2731. EXPORT_SYMBOL_GPL(vb2_write);
  2732. struct vb2_threadio_data {
  2733. struct task_struct *thread;
  2734. vb2_thread_fnc fnc;
  2735. void *priv;
  2736. bool stop;
  2737. };
  2738. static int vb2_thread(void *data)
  2739. {
  2740. struct vb2_queue *q = data;
  2741. struct vb2_threadio_data *threadio = q->threadio;
  2742. struct vb2_fileio_data *fileio = q->fileio;
  2743. bool set_timestamp = false;
  2744. int prequeue = 0;
  2745. int index = 0;
  2746. int ret = 0;
  2747. if (V4L2_TYPE_IS_OUTPUT(q->type)) {
  2748. prequeue = q->num_buffers;
  2749. set_timestamp =
  2750. (q->timestamp_flags & V4L2_BUF_FLAG_TIMESTAMP_MASK) ==
  2751. V4L2_BUF_FLAG_TIMESTAMP_COPY;
  2752. }
  2753. set_freezable();
  2754. for (;;) {
  2755. struct vb2_buffer *vb;
  2756. /*
  2757. * Call vb2_dqbuf to get buffer back.
  2758. */
  2759. memset(&fileio->b, 0, sizeof(fileio->b));
  2760. fileio->b.type = q->type;
  2761. fileio->b.memory = q->memory;
  2762. if (prequeue) {
  2763. fileio->b.index = index++;
  2764. prequeue--;
  2765. } else {
  2766. call_void_qop(q, wait_finish, q);
  2767. ret = vb2_internal_dqbuf(q, &fileio->b, 0);
  2768. call_void_qop(q, wait_prepare, q);
  2769. dprintk(5, "file io: vb2_dqbuf result: %d\n", ret);
  2770. }
  2771. if (threadio->stop)
  2772. break;
  2773. if (ret)
  2774. break;
  2775. try_to_freeze();
  2776. vb = q->bufs[fileio->b.index];
  2777. if (!(fileio->b.flags & V4L2_BUF_FLAG_ERROR))
  2778. ret = threadio->fnc(vb, threadio->priv);
  2779. if (ret)
  2780. break;
  2781. call_void_qop(q, wait_finish, q);
  2782. if (set_timestamp)
  2783. v4l2_get_timestamp(&fileio->b.timestamp);
  2784. ret = vb2_internal_qbuf(q, &fileio->b);
  2785. call_void_qop(q, wait_prepare, q);
  2786. if (ret)
  2787. break;
  2788. }
  2789. /* Hmm, linux becomes *very* unhappy without this ... */
  2790. while (!kthread_should_stop()) {
  2791. set_current_state(TASK_INTERRUPTIBLE);
  2792. schedule();
  2793. }
  2794. return 0;
  2795. }
  2796. /*
  2797. * This function should not be used for anything else but the videobuf2-dvb
  2798. * support. If you think you have another good use-case for this, then please
  2799. * contact the linux-media mailinglist first.
  2800. */
  2801. int vb2_thread_start(struct vb2_queue *q, vb2_thread_fnc fnc, void *priv,
  2802. const char *thread_name)
  2803. {
  2804. struct vb2_threadio_data *threadio;
  2805. int ret = 0;
  2806. if (q->threadio)
  2807. return -EBUSY;
  2808. if (vb2_is_busy(q))
  2809. return -EBUSY;
  2810. if (WARN_ON(q->fileio))
  2811. return -EBUSY;
  2812. threadio = kzalloc(sizeof(*threadio), GFP_KERNEL);
  2813. if (threadio == NULL)
  2814. return -ENOMEM;
  2815. threadio->fnc = fnc;
  2816. threadio->priv = priv;
  2817. ret = __vb2_init_fileio(q, !V4L2_TYPE_IS_OUTPUT(q->type));
  2818. dprintk(3, "file io: vb2_init_fileio result: %d\n", ret);
  2819. if (ret)
  2820. goto nomem;
  2821. q->threadio = threadio;
  2822. threadio->thread = kthread_run(vb2_thread, q, "vb2-%s", thread_name);
  2823. if (IS_ERR(threadio->thread)) {
  2824. ret = PTR_ERR(threadio->thread);
  2825. threadio->thread = NULL;
  2826. goto nothread;
  2827. }
  2828. return 0;
  2829. nothread:
  2830. __vb2_cleanup_fileio(q);
  2831. nomem:
  2832. kfree(threadio);
  2833. return ret;
  2834. }
  2835. EXPORT_SYMBOL_GPL(vb2_thread_start);
  2836. int vb2_thread_stop(struct vb2_queue *q)
  2837. {
  2838. struct vb2_threadio_data *threadio = q->threadio;
  2839. struct vb2_fileio_data *fileio = q->fileio;
  2840. int err;
  2841. if (threadio == NULL)
  2842. return 0;
  2843. call_void_qop(q, wait_finish, q);
  2844. threadio->stop = true;
  2845. vb2_internal_streamoff(q, q->type);
  2846. call_void_qop(q, wait_prepare, q);
  2847. q->fileio = NULL;
  2848. fileio->req.count = 0;
  2849. vb2_reqbufs(q, &fileio->req);
  2850. kfree(fileio);
  2851. err = kthread_stop(threadio->thread);
  2852. threadio->thread = NULL;
  2853. kfree(threadio);
  2854. q->fileio = NULL;
  2855. q->threadio = NULL;
  2856. return err;
  2857. }
  2858. EXPORT_SYMBOL_GPL(vb2_thread_stop);
  2859. /*
  2860. * The following functions are not part of the vb2 core API, but are helper
  2861. * functions that plug into struct v4l2_ioctl_ops, struct v4l2_file_operations
  2862. * and struct vb2_ops.
  2863. * They contain boilerplate code that most if not all drivers have to do
  2864. * and so they simplify the driver code.
  2865. */
  2866. /* The queue is busy if there is a owner and you are not that owner. */
  2867. static inline bool vb2_queue_is_busy(struct video_device *vdev, struct file *file)
  2868. {
  2869. return vdev->queue->owner && vdev->queue->owner != file->private_data;
  2870. }
  2871. /* vb2 ioctl helpers */
  2872. int vb2_ioctl_reqbufs(struct file *file, void *priv,
  2873. struct v4l2_requestbuffers *p)
  2874. {
  2875. struct video_device *vdev = video_devdata(file);
  2876. int res = __verify_memory_type(vdev->queue, p->memory, p->type);
  2877. if (res)
  2878. return res;
  2879. if (vb2_queue_is_busy(vdev, file))
  2880. return -EBUSY;
  2881. res = __reqbufs(vdev->queue, p);
  2882. /* If count == 0, then the owner has released all buffers and he
  2883. is no longer owner of the queue. Otherwise we have a new owner. */
  2884. if (res == 0)
  2885. vdev->queue->owner = p->count ? file->private_data : NULL;
  2886. return res;
  2887. }
  2888. EXPORT_SYMBOL_GPL(vb2_ioctl_reqbufs);
  2889. int vb2_ioctl_create_bufs(struct file *file, void *priv,
  2890. struct v4l2_create_buffers *p)
  2891. {
  2892. struct video_device *vdev = video_devdata(file);
  2893. int res = __verify_memory_type(vdev->queue, p->memory, p->format.type);
  2894. p->index = vdev->queue->num_buffers;
  2895. /* If count == 0, then just check if memory and type are valid.
  2896. Any -EBUSY result from __verify_memory_type can be mapped to 0. */
  2897. if (p->count == 0)
  2898. return res != -EBUSY ? res : 0;
  2899. if (res)
  2900. return res;
  2901. if (vb2_queue_is_busy(vdev, file))
  2902. return -EBUSY;
  2903. res = __create_bufs(vdev->queue, p);
  2904. if (res == 0)
  2905. vdev->queue->owner = file->private_data;
  2906. return res;
  2907. }
  2908. EXPORT_SYMBOL_GPL(vb2_ioctl_create_bufs);
  2909. int vb2_ioctl_prepare_buf(struct file *file, void *priv,
  2910. struct v4l2_buffer *p)
  2911. {
  2912. struct video_device *vdev = video_devdata(file);
  2913. if (vb2_queue_is_busy(vdev, file))
  2914. return -EBUSY;
  2915. return vb2_prepare_buf(vdev->queue, p);
  2916. }
  2917. EXPORT_SYMBOL_GPL(vb2_ioctl_prepare_buf);
  2918. int vb2_ioctl_querybuf(struct file *file, void *priv, struct v4l2_buffer *p)
  2919. {
  2920. struct video_device *vdev = video_devdata(file);
  2921. /* No need to call vb2_queue_is_busy(), anyone can query buffers. */
  2922. return vb2_querybuf(vdev->queue, p);
  2923. }
  2924. EXPORT_SYMBOL_GPL(vb2_ioctl_querybuf);
  2925. int vb2_ioctl_qbuf(struct file *file, void *priv, struct v4l2_buffer *p)
  2926. {
  2927. struct video_device *vdev = video_devdata(file);
  2928. if (vb2_queue_is_busy(vdev, file))
  2929. return -EBUSY;
  2930. return vb2_qbuf(vdev->queue, p);
  2931. }
  2932. EXPORT_SYMBOL_GPL(vb2_ioctl_qbuf);
  2933. int vb2_ioctl_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
  2934. {
  2935. struct video_device *vdev = video_devdata(file);
  2936. if (vb2_queue_is_busy(vdev, file))
  2937. return -EBUSY;
  2938. return vb2_dqbuf(vdev->queue, p, file->f_flags & O_NONBLOCK);
  2939. }
  2940. EXPORT_SYMBOL_GPL(vb2_ioctl_dqbuf);
  2941. int vb2_ioctl_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
  2942. {
  2943. struct video_device *vdev = video_devdata(file);
  2944. if (vb2_queue_is_busy(vdev, file))
  2945. return -EBUSY;
  2946. return vb2_streamon(vdev->queue, i);
  2947. }
  2948. EXPORT_SYMBOL_GPL(vb2_ioctl_streamon);
  2949. int vb2_ioctl_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
  2950. {
  2951. struct video_device *vdev = video_devdata(file);
  2952. if (vb2_queue_is_busy(vdev, file))
  2953. return -EBUSY;
  2954. return vb2_streamoff(vdev->queue, i);
  2955. }
  2956. EXPORT_SYMBOL_GPL(vb2_ioctl_streamoff);
  2957. int vb2_ioctl_expbuf(struct file *file, void *priv, struct v4l2_exportbuffer *p)
  2958. {
  2959. struct video_device *vdev = video_devdata(file);
  2960. if (vb2_queue_is_busy(vdev, file))
  2961. return -EBUSY;
  2962. return vb2_expbuf(vdev->queue, p);
  2963. }
  2964. EXPORT_SYMBOL_GPL(vb2_ioctl_expbuf);
  2965. /* v4l2_file_operations helpers */
  2966. int vb2_fop_mmap(struct file *file, struct vm_area_struct *vma)
  2967. {
  2968. struct video_device *vdev = video_devdata(file);
  2969. return vb2_mmap(vdev->queue, vma);
  2970. }
  2971. EXPORT_SYMBOL_GPL(vb2_fop_mmap);
  2972. int _vb2_fop_release(struct file *file, struct mutex *lock)
  2973. {
  2974. struct video_device *vdev = video_devdata(file);
  2975. if (file->private_data == vdev->queue->owner) {
  2976. if (lock)
  2977. mutex_lock(lock);
  2978. vb2_queue_release(vdev->queue);
  2979. vdev->queue->owner = NULL;
  2980. if (lock)
  2981. mutex_unlock(lock);
  2982. }
  2983. return v4l2_fh_release(file);
  2984. }
  2985. EXPORT_SYMBOL_GPL(_vb2_fop_release);
  2986. int vb2_fop_release(struct file *file)
  2987. {
  2988. struct video_device *vdev = video_devdata(file);
  2989. struct mutex *lock = vdev->queue->lock ? vdev->queue->lock : vdev->lock;
  2990. return _vb2_fop_release(file, lock);
  2991. }
  2992. EXPORT_SYMBOL_GPL(vb2_fop_release);
  2993. ssize_t vb2_fop_write(struct file *file, const char __user *buf,
  2994. size_t count, loff_t *ppos)
  2995. {
  2996. struct video_device *vdev = video_devdata(file);
  2997. struct mutex *lock = vdev->queue->lock ? vdev->queue->lock : vdev->lock;
  2998. int err = -EBUSY;
  2999. if (lock && mutex_lock_interruptible(lock))
  3000. return -ERESTARTSYS;
  3001. if (vb2_queue_is_busy(vdev, file))
  3002. goto exit;
  3003. err = vb2_write(vdev->queue, buf, count, ppos,
  3004. file->f_flags & O_NONBLOCK);
  3005. if (vdev->queue->fileio)
  3006. vdev->queue->owner = file->private_data;
  3007. exit:
  3008. if (lock)
  3009. mutex_unlock(lock);
  3010. return err;
  3011. }
  3012. EXPORT_SYMBOL_GPL(vb2_fop_write);
  3013. ssize_t vb2_fop_read(struct file *file, char __user *buf,
  3014. size_t count, loff_t *ppos)
  3015. {
  3016. struct video_device *vdev = video_devdata(file);
  3017. struct mutex *lock = vdev->queue->lock ? vdev->queue->lock : vdev->lock;
  3018. int err = -EBUSY;
  3019. if (lock && mutex_lock_interruptible(lock))
  3020. return -ERESTARTSYS;
  3021. if (vb2_queue_is_busy(vdev, file))
  3022. goto exit;
  3023. err = vb2_read(vdev->queue, buf, count, ppos,
  3024. file->f_flags & O_NONBLOCK);
  3025. if (vdev->queue->fileio)
  3026. vdev->queue->owner = file->private_data;
  3027. exit:
  3028. if (lock)
  3029. mutex_unlock(lock);
  3030. return err;
  3031. }
  3032. EXPORT_SYMBOL_GPL(vb2_fop_read);
  3033. unsigned int vb2_fop_poll(struct file *file, poll_table *wait)
  3034. {
  3035. struct video_device *vdev = video_devdata(file);
  3036. struct vb2_queue *q = vdev->queue;
  3037. struct mutex *lock = q->lock ? q->lock : vdev->lock;
  3038. unsigned long req_events = poll_requested_events(wait);
  3039. unsigned res;
  3040. void *fileio;
  3041. bool must_lock = false;
  3042. /* Try to be smart: only lock if polling might start fileio,
  3043. otherwise locking will only introduce unwanted delays. */
  3044. if (q->num_buffers == 0 && !vb2_fileio_is_active(q)) {
  3045. if (!V4L2_TYPE_IS_OUTPUT(q->type) && (q->io_modes & VB2_READ) &&
  3046. (req_events & (POLLIN | POLLRDNORM)))
  3047. must_lock = true;
  3048. else if (V4L2_TYPE_IS_OUTPUT(q->type) && (q->io_modes & VB2_WRITE) &&
  3049. (req_events & (POLLOUT | POLLWRNORM)))
  3050. must_lock = true;
  3051. }
  3052. /* If locking is needed, but this helper doesn't know how, then you
  3053. shouldn't be using this helper but you should write your own. */
  3054. WARN_ON(must_lock && !lock);
  3055. if (must_lock && lock && mutex_lock_interruptible(lock))
  3056. return POLLERR;
  3057. fileio = q->fileio;
  3058. res = vb2_poll(vdev->queue, file, wait);
  3059. /* If fileio was started, then we have a new queue owner. */
  3060. if (must_lock && !fileio && q->fileio)
  3061. q->owner = file->private_data;
  3062. if (must_lock && lock)
  3063. mutex_unlock(lock);
  3064. return res;
  3065. }
  3066. EXPORT_SYMBOL_GPL(vb2_fop_poll);
  3067. #ifndef CONFIG_MMU
  3068. unsigned long vb2_fop_get_unmapped_area(struct file *file, unsigned long addr,
  3069. unsigned long len, unsigned long pgoff, unsigned long flags)
  3070. {
  3071. struct video_device *vdev = video_devdata(file);
  3072. return vb2_get_unmapped_area(vdev->queue, addr, len, pgoff, flags);
  3073. }
  3074. EXPORT_SYMBOL_GPL(vb2_fop_get_unmapped_area);
  3075. #endif
  3076. /* vb2_ops helpers. Only use if vq->lock is non-NULL. */
  3077. void vb2_ops_wait_prepare(struct vb2_queue *vq)
  3078. {
  3079. mutex_unlock(vq->lock);
  3080. }
  3081. EXPORT_SYMBOL_GPL(vb2_ops_wait_prepare);
  3082. void vb2_ops_wait_finish(struct vb2_queue *vq)
  3083. {
  3084. mutex_lock(vq->lock);
  3085. }
  3086. EXPORT_SYMBOL_GPL(vb2_ops_wait_finish);
  3087. MODULE_DESCRIPTION("Driver helper framework for Video for Linux 2");
  3088. MODULE_AUTHOR("Pawel Osciak <pawel@osciak.com>, Marek Szyprowski");
  3089. MODULE_LICENSE("GPL");