videobuf2-core.c 62 KB

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