videobuf2-core.c 98 KB

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