videobuf2-core.c 63 KB

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