videobuf2-core.c 94 KB

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