videobuf2-core.c 97 KB

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