videobuf2-core.c 54 KB

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