videobuf2-core.c 72 KB

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