videobuf2-core.c 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120
  1. /*
  2. * videobuf2-core.c - V4L2 driver helper framework
  3. *
  4. * Copyright (C) 2010 Samsung Electronics
  5. *
  6. * Author: Pawel Osciak <pawel@osciak.com>
  7. * Marek Szyprowski <m.szyprowski@samsung.com>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation.
  12. */
  13. #include <linux/err.h>
  14. #include <linux/kernel.h>
  15. #include <linux/module.h>
  16. #include <linux/mm.h>
  17. #include <linux/poll.h>
  18. #include <linux/slab.h>
  19. #include <linux/sched.h>
  20. #include <media/v4l2-dev.h>
  21. #include <media/v4l2-fh.h>
  22. #include <media/v4l2-event.h>
  23. #include <media/videobuf2-core.h>
  24. static int debug;
  25. module_param(debug, int, 0644);
  26. #define dprintk(level, fmt, arg...) \
  27. do { \
  28. if (debug >= level) \
  29. printk(KERN_DEBUG "vb2: " fmt, ## arg); \
  30. } while (0)
  31. #define call_memop(q, op, args...) \
  32. (((q)->mem_ops->op) ? \
  33. ((q)->mem_ops->op(args)) : 0)
  34. #define call_qop(q, op, args...) \
  35. (((q)->ops->op) ? ((q)->ops->op(args)) : 0)
  36. #define V4L2_BUFFER_STATE_FLAGS (V4L2_BUF_FLAG_MAPPED | V4L2_BUF_FLAG_QUEUED | \
  37. V4L2_BUF_FLAG_DONE | V4L2_BUF_FLAG_ERROR | \
  38. V4L2_BUF_FLAG_PREPARED)
  39. /**
  40. * __vb2_buf_mem_alloc() - allocate video memory for the given buffer
  41. */
  42. static int __vb2_buf_mem_alloc(struct vb2_buffer *vb)
  43. {
  44. struct vb2_queue *q = vb->vb2_queue;
  45. void *mem_priv;
  46. int plane;
  47. /* Allocate memory for all planes in this buffer */
  48. for (plane = 0; plane < vb->num_planes; ++plane) {
  49. mem_priv = call_memop(q, alloc, q->alloc_ctx[plane],
  50. q->plane_sizes[plane]);
  51. if (IS_ERR_OR_NULL(mem_priv))
  52. goto free;
  53. /* Associate allocator private data with this plane */
  54. vb->planes[plane].mem_priv = mem_priv;
  55. vb->v4l2_planes[plane].length = q->plane_sizes[plane];
  56. }
  57. return 0;
  58. free:
  59. /* Free already allocated memory if one of the allocations failed */
  60. for (; plane > 0; --plane) {
  61. call_memop(q, put, vb->planes[plane - 1].mem_priv);
  62. vb->planes[plane - 1].mem_priv = NULL;
  63. }
  64. return -ENOMEM;
  65. }
  66. /**
  67. * __vb2_buf_mem_free() - free memory of the given buffer
  68. */
  69. static void __vb2_buf_mem_free(struct vb2_buffer *vb)
  70. {
  71. struct vb2_queue *q = vb->vb2_queue;
  72. unsigned int plane;
  73. for (plane = 0; plane < vb->num_planes; ++plane) {
  74. call_memop(q, put, vb->planes[plane].mem_priv);
  75. vb->planes[plane].mem_priv = NULL;
  76. dprintk(3, "Freed plane %d of buffer %d\n", plane,
  77. vb->v4l2_buf.index);
  78. }
  79. }
  80. /**
  81. * __vb2_buf_userptr_put() - release userspace memory associated with
  82. * a USERPTR buffer
  83. */
  84. static void __vb2_buf_userptr_put(struct vb2_buffer *vb)
  85. {
  86. struct vb2_queue *q = vb->vb2_queue;
  87. unsigned int plane;
  88. for (plane = 0; plane < vb->num_planes; ++plane) {
  89. if (vb->planes[plane].mem_priv)
  90. call_memop(q, put_userptr, vb->planes[plane].mem_priv);
  91. vb->planes[plane].mem_priv = NULL;
  92. }
  93. }
  94. /**
  95. * __setup_offsets() - setup unique offsets ("cookies") for every plane in
  96. * every buffer on the queue
  97. */
  98. static void __setup_offsets(struct vb2_queue *q, unsigned int n)
  99. {
  100. unsigned int buffer, plane;
  101. struct vb2_buffer *vb;
  102. unsigned long off;
  103. if (q->num_buffers) {
  104. struct v4l2_plane *p;
  105. vb = q->bufs[q->num_buffers - 1];
  106. p = &vb->v4l2_planes[vb->num_planes - 1];
  107. off = PAGE_ALIGN(p->m.mem_offset + p->length);
  108. } else {
  109. off = 0;
  110. }
  111. for (buffer = q->num_buffers; buffer < q->num_buffers + n; ++buffer) {
  112. vb = q->bufs[buffer];
  113. if (!vb)
  114. continue;
  115. for (plane = 0; plane < vb->num_planes; ++plane) {
  116. vb->v4l2_planes[plane].length = q->plane_sizes[plane];
  117. vb->v4l2_planes[plane].m.mem_offset = off;
  118. dprintk(3, "Buffer %d, plane %d offset 0x%08lx\n",
  119. buffer, plane, off);
  120. off += vb->v4l2_planes[plane].length;
  121. off = PAGE_ALIGN(off);
  122. }
  123. }
  124. }
  125. /**
  126. * __vb2_queue_alloc() - allocate videobuf buffer structures and (for MMAP type)
  127. * video buffer memory for all buffers/planes on the queue and initializes the
  128. * queue
  129. *
  130. * Returns the number of buffers successfully allocated.
  131. */
  132. static int __vb2_queue_alloc(struct vb2_queue *q, enum v4l2_memory memory,
  133. unsigned int num_buffers, unsigned int num_planes)
  134. {
  135. unsigned int buffer;
  136. struct vb2_buffer *vb;
  137. int ret;
  138. for (buffer = 0; buffer < num_buffers; ++buffer) {
  139. /* Allocate videobuf buffer structures */
  140. vb = kzalloc(q->buf_struct_size, GFP_KERNEL);
  141. if (!vb) {
  142. dprintk(1, "Memory alloc for buffer struct failed\n");
  143. break;
  144. }
  145. /* Length stores number of planes for multiplanar buffers */
  146. if (V4L2_TYPE_IS_MULTIPLANAR(q->type))
  147. vb->v4l2_buf.length = num_planes;
  148. vb->state = VB2_BUF_STATE_DEQUEUED;
  149. vb->vb2_queue = q;
  150. vb->num_planes = num_planes;
  151. vb->v4l2_buf.index = q->num_buffers + buffer;
  152. vb->v4l2_buf.type = q->type;
  153. vb->v4l2_buf.memory = memory;
  154. /* Allocate video buffer memory for the MMAP type */
  155. if (memory == V4L2_MEMORY_MMAP) {
  156. ret = __vb2_buf_mem_alloc(vb);
  157. if (ret) {
  158. dprintk(1, "Failed allocating memory for "
  159. "buffer %d\n", buffer);
  160. kfree(vb);
  161. break;
  162. }
  163. /*
  164. * Call the driver-provided buffer initialization
  165. * callback, if given. An error in initialization
  166. * results in queue setup failure.
  167. */
  168. ret = call_qop(q, buf_init, vb);
  169. if (ret) {
  170. dprintk(1, "Buffer %d %p initialization"
  171. " failed\n", buffer, vb);
  172. __vb2_buf_mem_free(vb);
  173. kfree(vb);
  174. break;
  175. }
  176. }
  177. q->bufs[q->num_buffers + buffer] = vb;
  178. }
  179. __setup_offsets(q, buffer);
  180. dprintk(1, "Allocated %d buffers, %d plane(s) each\n",
  181. buffer, num_planes);
  182. return buffer;
  183. }
  184. /**
  185. * __vb2_free_mem() - release all video buffer memory for a given queue
  186. */
  187. static void __vb2_free_mem(struct vb2_queue *q, unsigned int buffers)
  188. {
  189. unsigned int buffer;
  190. struct vb2_buffer *vb;
  191. for (buffer = q->num_buffers - buffers; buffer < q->num_buffers;
  192. ++buffer) {
  193. vb = q->bufs[buffer];
  194. if (!vb)
  195. continue;
  196. /* Free MMAP buffers or release USERPTR buffers */
  197. if (q->memory == V4L2_MEMORY_MMAP)
  198. __vb2_buf_mem_free(vb);
  199. else
  200. __vb2_buf_userptr_put(vb);
  201. }
  202. }
  203. /**
  204. * __vb2_queue_free() - free buffers at the end of the queue - video memory and
  205. * related information, if no buffers are left return the queue to an
  206. * uninitialized state. Might be called even if the queue has already been freed.
  207. */
  208. static void __vb2_queue_free(struct vb2_queue *q, unsigned int buffers)
  209. {
  210. unsigned int buffer;
  211. /* Call driver-provided cleanup function for each buffer, if provided */
  212. if (q->ops->buf_cleanup) {
  213. for (buffer = q->num_buffers - buffers; buffer < q->num_buffers;
  214. ++buffer) {
  215. if (NULL == q->bufs[buffer])
  216. continue;
  217. q->ops->buf_cleanup(q->bufs[buffer]);
  218. }
  219. }
  220. /* Release video buffer memory */
  221. __vb2_free_mem(q, buffers);
  222. /* Free videobuf buffers */
  223. for (buffer = q->num_buffers - buffers; buffer < q->num_buffers;
  224. ++buffer) {
  225. kfree(q->bufs[buffer]);
  226. q->bufs[buffer] = NULL;
  227. }
  228. q->num_buffers -= buffers;
  229. if (!q->num_buffers)
  230. q->memory = 0;
  231. INIT_LIST_HEAD(&q->queued_list);
  232. }
  233. /**
  234. * __verify_planes_array() - verify that the planes array passed in struct
  235. * v4l2_buffer from userspace can be safely used
  236. */
  237. static int __verify_planes_array(struct vb2_buffer *vb, const struct v4l2_buffer *b)
  238. {
  239. /* Is memory for copying plane information present? */
  240. if (NULL == b->m.planes) {
  241. dprintk(1, "Multi-planar buffer passed but "
  242. "planes array not provided\n");
  243. return -EINVAL;
  244. }
  245. if (b->length < vb->num_planes || b->length > VIDEO_MAX_PLANES) {
  246. dprintk(1, "Incorrect planes array length, "
  247. "expected %d, got %d\n", vb->num_planes, b->length);
  248. return -EINVAL;
  249. }
  250. return 0;
  251. }
  252. /**
  253. * __buffer_in_use() - return true if the buffer is in use and
  254. * the queue cannot be freed (by the means of REQBUFS(0)) call
  255. */
  256. static bool __buffer_in_use(struct vb2_queue *q, struct vb2_buffer *vb)
  257. {
  258. unsigned int plane;
  259. for (plane = 0; plane < vb->num_planes; ++plane) {
  260. void *mem_priv = vb->planes[plane].mem_priv;
  261. /*
  262. * If num_users() has not been provided, call_memop
  263. * will return 0, apparently nobody cares about this
  264. * case anyway. If num_users() returns more than 1,
  265. * we are not the only user of the plane's memory.
  266. */
  267. if (mem_priv && call_memop(q, num_users, mem_priv) > 1)
  268. return true;
  269. }
  270. return false;
  271. }
  272. /**
  273. * __buffers_in_use() - return true if any buffers on the queue are in use and
  274. * the queue cannot be freed (by the means of REQBUFS(0)) call
  275. */
  276. static bool __buffers_in_use(struct vb2_queue *q)
  277. {
  278. unsigned int buffer;
  279. for (buffer = 0; buffer < q->num_buffers; ++buffer) {
  280. if (__buffer_in_use(q, q->bufs[buffer]))
  281. return true;
  282. }
  283. return false;
  284. }
  285. /**
  286. * __fill_v4l2_buffer() - fill in a struct v4l2_buffer with information to be
  287. * returned to userspace
  288. */
  289. static int __fill_v4l2_buffer(struct vb2_buffer *vb, struct v4l2_buffer *b)
  290. {
  291. struct vb2_queue *q = vb->vb2_queue;
  292. int ret;
  293. /* Copy back data such as timestamp, flags, input, etc. */
  294. memcpy(b, &vb->v4l2_buf, offsetof(struct v4l2_buffer, m));
  295. b->input = vb->v4l2_buf.input;
  296. b->reserved = vb->v4l2_buf.reserved;
  297. if (V4L2_TYPE_IS_MULTIPLANAR(q->type)) {
  298. ret = __verify_planes_array(vb, b);
  299. if (ret)
  300. return ret;
  301. /*
  302. * Fill in plane-related data if userspace provided an array
  303. * for it. The memory and size is verified above.
  304. */
  305. memcpy(b->m.planes, vb->v4l2_planes,
  306. b->length * sizeof(struct v4l2_plane));
  307. } else {
  308. /*
  309. * We use length and offset in v4l2_planes array even for
  310. * single-planar buffers, but userspace does not.
  311. */
  312. b->length = vb->v4l2_planes[0].length;
  313. b->bytesused = vb->v4l2_planes[0].bytesused;
  314. if (q->memory == V4L2_MEMORY_MMAP)
  315. b->m.offset = vb->v4l2_planes[0].m.mem_offset;
  316. else if (q->memory == V4L2_MEMORY_USERPTR)
  317. b->m.userptr = vb->v4l2_planes[0].m.userptr;
  318. }
  319. /*
  320. * Clear any buffer state related flags.
  321. */
  322. b->flags &= ~V4L2_BUFFER_STATE_FLAGS;
  323. switch (vb->state) {
  324. case VB2_BUF_STATE_QUEUED:
  325. case VB2_BUF_STATE_ACTIVE:
  326. b->flags |= V4L2_BUF_FLAG_QUEUED;
  327. break;
  328. case VB2_BUF_STATE_ERROR:
  329. b->flags |= V4L2_BUF_FLAG_ERROR;
  330. /* fall through */
  331. case VB2_BUF_STATE_DONE:
  332. b->flags |= V4L2_BUF_FLAG_DONE;
  333. break;
  334. case VB2_BUF_STATE_PREPARED:
  335. b->flags |= V4L2_BUF_FLAG_PREPARED;
  336. break;
  337. case VB2_BUF_STATE_DEQUEUED:
  338. /* nothing */
  339. break;
  340. }
  341. if (__buffer_in_use(q, vb))
  342. b->flags |= V4L2_BUF_FLAG_MAPPED;
  343. return 0;
  344. }
  345. /**
  346. * vb2_querybuf() - query video buffer information
  347. * @q: videobuf queue
  348. * @b: buffer struct passed from userspace to vidioc_querybuf handler
  349. * in driver
  350. *
  351. * Should be called from vidioc_querybuf ioctl handler in driver.
  352. * This function will verify the passed v4l2_buffer structure and fill the
  353. * relevant information for the userspace.
  354. *
  355. * The return values from this function are intended to be directly returned
  356. * from vidioc_querybuf handler in driver.
  357. */
  358. int vb2_querybuf(struct vb2_queue *q, struct v4l2_buffer *b)
  359. {
  360. struct vb2_buffer *vb;
  361. if (b->type != q->type) {
  362. dprintk(1, "querybuf: wrong buffer type\n");
  363. return -EINVAL;
  364. }
  365. if (b->index >= q->num_buffers) {
  366. dprintk(1, "querybuf: buffer index out of range\n");
  367. return -EINVAL;
  368. }
  369. vb = q->bufs[b->index];
  370. return __fill_v4l2_buffer(vb, b);
  371. }
  372. EXPORT_SYMBOL(vb2_querybuf);
  373. /**
  374. * __verify_userptr_ops() - verify that all memory operations required for
  375. * USERPTR queue type have been provided
  376. */
  377. static int __verify_userptr_ops(struct vb2_queue *q)
  378. {
  379. if (!(q->io_modes & VB2_USERPTR) || !q->mem_ops->get_userptr ||
  380. !q->mem_ops->put_userptr)
  381. return -EINVAL;
  382. return 0;
  383. }
  384. /**
  385. * __verify_mmap_ops() - verify that all memory operations required for
  386. * MMAP queue type have been provided
  387. */
  388. static int __verify_mmap_ops(struct vb2_queue *q)
  389. {
  390. if (!(q->io_modes & VB2_MMAP) || !q->mem_ops->alloc ||
  391. !q->mem_ops->put || !q->mem_ops->mmap)
  392. return -EINVAL;
  393. return 0;
  394. }
  395. /**
  396. * vb2_reqbufs() - Initiate streaming
  397. * @q: videobuf2 queue
  398. * @req: struct passed from userspace to vidioc_reqbufs handler in driver
  399. *
  400. * Should be called from vidioc_reqbufs ioctl handler of a driver.
  401. * This function:
  402. * 1) verifies streaming parameters passed from the userspace,
  403. * 2) sets up the queue,
  404. * 3) negotiates number of buffers and planes per buffer with the driver
  405. * to be used during streaming,
  406. * 4) allocates internal buffer structures (struct vb2_buffer), according to
  407. * the agreed parameters,
  408. * 5) for MMAP memory type, allocates actual video memory, using the
  409. * memory handling/allocation routines provided during queue initialization
  410. *
  411. * If req->count is 0, all the memory will be freed instead.
  412. * If the queue has been allocated previously (by a previous vb2_reqbufs) call
  413. * and the queue is not busy, memory will be reallocated.
  414. *
  415. * The return values from this function are intended to be directly returned
  416. * from vidioc_reqbufs handler in driver.
  417. */
  418. int vb2_reqbufs(struct vb2_queue *q, struct v4l2_requestbuffers *req)
  419. {
  420. unsigned int num_buffers, allocated_buffers, num_planes = 0;
  421. int ret = 0;
  422. if (q->fileio) {
  423. dprintk(1, "reqbufs: file io in progress\n");
  424. return -EBUSY;
  425. }
  426. if (req->memory != V4L2_MEMORY_MMAP
  427. && req->memory != V4L2_MEMORY_USERPTR) {
  428. dprintk(1, "reqbufs: unsupported memory type\n");
  429. return -EINVAL;
  430. }
  431. if (req->type != q->type) {
  432. dprintk(1, "reqbufs: requested type is incorrect\n");
  433. return -EINVAL;
  434. }
  435. if (q->streaming) {
  436. dprintk(1, "reqbufs: streaming active\n");
  437. return -EBUSY;
  438. }
  439. /*
  440. * Make sure all the required memory ops for given memory type
  441. * are available.
  442. */
  443. if (req->memory == V4L2_MEMORY_MMAP && __verify_mmap_ops(q)) {
  444. dprintk(1, "reqbufs: MMAP for current setup unsupported\n");
  445. return -EINVAL;
  446. }
  447. if (req->memory == V4L2_MEMORY_USERPTR && __verify_userptr_ops(q)) {
  448. dprintk(1, "reqbufs: USERPTR for current setup unsupported\n");
  449. return -EINVAL;
  450. }
  451. if (req->count == 0 || q->num_buffers != 0 || q->memory != req->memory) {
  452. /*
  453. * We already have buffers allocated, so first check if they
  454. * are not in use and can be freed.
  455. */
  456. if (q->memory == V4L2_MEMORY_MMAP && __buffers_in_use(q)) {
  457. dprintk(1, "reqbufs: memory in use, cannot free\n");
  458. return -EBUSY;
  459. }
  460. __vb2_queue_free(q, q->num_buffers);
  461. /*
  462. * In case of REQBUFS(0) return immediately without calling
  463. * driver's queue_setup() callback and allocating resources.
  464. */
  465. if (req->count == 0)
  466. return 0;
  467. }
  468. /*
  469. * Make sure the requested values and current defaults are sane.
  470. */
  471. num_buffers = min_t(unsigned int, req->count, VIDEO_MAX_FRAME);
  472. memset(q->plane_sizes, 0, sizeof(q->plane_sizes));
  473. memset(q->alloc_ctx, 0, sizeof(q->alloc_ctx));
  474. q->memory = req->memory;
  475. /*
  476. * Ask the driver how many buffers and planes per buffer it requires.
  477. * Driver also sets the size and allocator context for each plane.
  478. */
  479. ret = call_qop(q, queue_setup, q, NULL, &num_buffers, &num_planes,
  480. q->plane_sizes, q->alloc_ctx);
  481. if (ret)
  482. return ret;
  483. /* Finally, allocate buffers and video memory */
  484. ret = __vb2_queue_alloc(q, req->memory, num_buffers, num_planes);
  485. if (ret == 0) {
  486. dprintk(1, "Memory allocation failed\n");
  487. return -ENOMEM;
  488. }
  489. allocated_buffers = ret;
  490. /*
  491. * Check if driver can handle the allocated number of buffers.
  492. */
  493. if (allocated_buffers < num_buffers) {
  494. num_buffers = allocated_buffers;
  495. ret = call_qop(q, queue_setup, q, NULL, &num_buffers,
  496. &num_planes, q->plane_sizes, q->alloc_ctx);
  497. if (!ret && allocated_buffers < num_buffers)
  498. ret = -ENOMEM;
  499. /*
  500. * Either the driver has accepted a smaller number of buffers,
  501. * or .queue_setup() returned an error
  502. */
  503. }
  504. q->num_buffers = allocated_buffers;
  505. if (ret < 0) {
  506. __vb2_queue_free(q, allocated_buffers);
  507. return ret;
  508. }
  509. /*
  510. * Return the number of successfully allocated buffers
  511. * to the userspace.
  512. */
  513. req->count = allocated_buffers;
  514. return 0;
  515. }
  516. EXPORT_SYMBOL_GPL(vb2_reqbufs);
  517. /**
  518. * vb2_create_bufs() - Allocate buffers and any required auxiliary structs
  519. * @q: videobuf2 queue
  520. * @create: creation parameters, passed from userspace to vidioc_create_bufs
  521. * handler in driver
  522. *
  523. * Should be called from vidioc_create_bufs ioctl handler of a driver.
  524. * This function:
  525. * 1) verifies parameter sanity
  526. * 2) calls the .queue_setup() queue operation
  527. * 3) performs any necessary memory allocations
  528. *
  529. * The return values from this function are intended to be directly returned
  530. * from vidioc_create_bufs handler in driver.
  531. */
  532. int vb2_create_bufs(struct vb2_queue *q, struct v4l2_create_buffers *create)
  533. {
  534. unsigned int num_planes = 0, num_buffers, allocated_buffers;
  535. int ret = 0;
  536. if (q->fileio) {
  537. dprintk(1, "%s(): file io in progress\n", __func__);
  538. return -EBUSY;
  539. }
  540. if (create->memory != V4L2_MEMORY_MMAP
  541. && create->memory != V4L2_MEMORY_USERPTR) {
  542. dprintk(1, "%s(): unsupported memory type\n", __func__);
  543. return -EINVAL;
  544. }
  545. if (create->format.type != q->type) {
  546. dprintk(1, "%s(): requested type is incorrect\n", __func__);
  547. return -EINVAL;
  548. }
  549. /*
  550. * Make sure all the required memory ops for given memory type
  551. * are available.
  552. */
  553. if (create->memory == V4L2_MEMORY_MMAP && __verify_mmap_ops(q)) {
  554. dprintk(1, "%s(): MMAP for current setup unsupported\n", __func__);
  555. return -EINVAL;
  556. }
  557. if (create->memory == V4L2_MEMORY_USERPTR && __verify_userptr_ops(q)) {
  558. dprintk(1, "%s(): USERPTR for current setup unsupported\n", __func__);
  559. return -EINVAL;
  560. }
  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. create->index = q->num_buffers;
  567. if (!q->num_buffers) {
  568. memset(q->plane_sizes, 0, sizeof(q->plane_sizes));
  569. memset(q->alloc_ctx, 0, sizeof(q->alloc_ctx));
  570. q->memory = create->memory;
  571. }
  572. num_buffers = min(create->count, VIDEO_MAX_FRAME - q->num_buffers);
  573. /*
  574. * Ask the driver, whether the requested number of buffers, planes per
  575. * buffer and their sizes are acceptable
  576. */
  577. ret = call_qop(q, queue_setup, q, &create->format, &num_buffers,
  578. &num_planes, q->plane_sizes, q->alloc_ctx);
  579. if (ret)
  580. return ret;
  581. /* Finally, allocate buffers and video memory */
  582. ret = __vb2_queue_alloc(q, create->memory, num_buffers,
  583. num_planes);
  584. if (ret < 0) {
  585. dprintk(1, "Memory allocation failed with error: %d\n", ret);
  586. return ret;
  587. }
  588. allocated_buffers = ret;
  589. /*
  590. * Check if driver can handle the so far allocated number of buffers.
  591. */
  592. if (ret < num_buffers) {
  593. num_buffers = ret;
  594. /*
  595. * q->num_buffers contains the total number of buffers, that the
  596. * queue driver has set up
  597. */
  598. ret = call_qop(q, queue_setup, q, &create->format, &num_buffers,
  599. &num_planes, q->plane_sizes, q->alloc_ctx);
  600. if (!ret && allocated_buffers < num_buffers)
  601. ret = -ENOMEM;
  602. /*
  603. * Either the driver has accepted a smaller number of buffers,
  604. * or .queue_setup() returned an error
  605. */
  606. }
  607. q->num_buffers += allocated_buffers;
  608. if (ret < 0) {
  609. __vb2_queue_free(q, allocated_buffers);
  610. return ret;
  611. }
  612. /*
  613. * Return the number of successfully allocated buffers
  614. * to the userspace.
  615. */
  616. create->count = allocated_buffers;
  617. return 0;
  618. }
  619. EXPORT_SYMBOL_GPL(vb2_create_bufs);
  620. /**
  621. * vb2_plane_vaddr() - Return a kernel virtual address of a given plane
  622. * @vb: vb2_buffer to which the plane in question belongs to
  623. * @plane_no: plane number for which the address is to be returned
  624. *
  625. * This function returns a kernel virtual address of a given plane if
  626. * such a mapping exist, NULL otherwise.
  627. */
  628. void *vb2_plane_vaddr(struct vb2_buffer *vb, unsigned int plane_no)
  629. {
  630. struct vb2_queue *q = vb->vb2_queue;
  631. if (plane_no > vb->num_planes || !vb->planes[plane_no].mem_priv)
  632. return NULL;
  633. return call_memop(q, vaddr, vb->planes[plane_no].mem_priv);
  634. }
  635. EXPORT_SYMBOL_GPL(vb2_plane_vaddr);
  636. /**
  637. * vb2_plane_cookie() - Return allocator specific cookie for the given plane
  638. * @vb: vb2_buffer to which the plane in question belongs to
  639. * @plane_no: plane number for which the cookie is to be returned
  640. *
  641. * This function returns an allocator specific cookie for a given plane if
  642. * available, NULL otherwise. The allocator should provide some simple static
  643. * inline function, which would convert this cookie to the allocator specific
  644. * type that can be used directly by the driver to access the buffer. This can
  645. * be for example physical address, pointer to scatter list or IOMMU mapping.
  646. */
  647. void *vb2_plane_cookie(struct vb2_buffer *vb, unsigned int plane_no)
  648. {
  649. struct vb2_queue *q = vb->vb2_queue;
  650. if (plane_no > vb->num_planes || !vb->planes[plane_no].mem_priv)
  651. return NULL;
  652. return call_memop(q, cookie, vb->planes[plane_no].mem_priv);
  653. }
  654. EXPORT_SYMBOL_GPL(vb2_plane_cookie);
  655. /**
  656. * vb2_buffer_done() - inform videobuf that an operation on a buffer is finished
  657. * @vb: vb2_buffer returned from the driver
  658. * @state: either VB2_BUF_STATE_DONE if the operation finished successfully
  659. * or VB2_BUF_STATE_ERROR if the operation finished with an error
  660. *
  661. * This function should be called by the driver after a hardware operation on
  662. * a buffer is finished and the buffer may be returned to userspace. The driver
  663. * cannot use this buffer anymore until it is queued back to it by videobuf
  664. * by the means of buf_queue callback. Only buffers previously queued to the
  665. * driver by buf_queue can be passed to this function.
  666. */
  667. void vb2_buffer_done(struct vb2_buffer *vb, enum vb2_buffer_state state)
  668. {
  669. struct vb2_queue *q = vb->vb2_queue;
  670. unsigned long flags;
  671. if (vb->state != VB2_BUF_STATE_ACTIVE)
  672. return;
  673. if (state != VB2_BUF_STATE_DONE && state != VB2_BUF_STATE_ERROR)
  674. return;
  675. dprintk(4, "Done processing on buffer %d, state: %d\n",
  676. vb->v4l2_buf.index, vb->state);
  677. /* Add the buffer to the done buffers list */
  678. spin_lock_irqsave(&q->done_lock, flags);
  679. vb->state = state;
  680. list_add_tail(&vb->done_entry, &q->done_list);
  681. atomic_dec(&q->queued_count);
  682. spin_unlock_irqrestore(&q->done_lock, flags);
  683. /* Inform any processes that may be waiting for buffers */
  684. wake_up(&q->done_wq);
  685. }
  686. EXPORT_SYMBOL_GPL(vb2_buffer_done);
  687. /**
  688. * __fill_vb2_buffer() - fill a vb2_buffer with information provided in
  689. * a v4l2_buffer by the userspace
  690. */
  691. static int __fill_vb2_buffer(struct vb2_buffer *vb, const struct v4l2_buffer *b,
  692. struct v4l2_plane *v4l2_planes)
  693. {
  694. unsigned int plane;
  695. int ret;
  696. if (V4L2_TYPE_IS_MULTIPLANAR(b->type)) {
  697. /*
  698. * Verify that the userspace gave us a valid array for
  699. * plane information.
  700. */
  701. ret = __verify_planes_array(vb, b);
  702. if (ret)
  703. return ret;
  704. /* Fill in driver-provided information for OUTPUT types */
  705. if (V4L2_TYPE_IS_OUTPUT(b->type)) {
  706. /*
  707. * Will have to go up to b->length when API starts
  708. * accepting variable number of planes.
  709. */
  710. for (plane = 0; plane < vb->num_planes; ++plane) {
  711. v4l2_planes[plane].bytesused =
  712. b->m.planes[plane].bytesused;
  713. v4l2_planes[plane].data_offset =
  714. b->m.planes[plane].data_offset;
  715. }
  716. }
  717. if (b->memory == V4L2_MEMORY_USERPTR) {
  718. for (plane = 0; plane < vb->num_planes; ++plane) {
  719. v4l2_planes[plane].m.userptr =
  720. b->m.planes[plane].m.userptr;
  721. v4l2_planes[plane].length =
  722. b->m.planes[plane].length;
  723. }
  724. }
  725. } else {
  726. /*
  727. * Single-planar buffers do not use planes array,
  728. * so fill in relevant v4l2_buffer struct fields instead.
  729. * In videobuf we use our internal V4l2_planes struct for
  730. * single-planar buffers as well, for simplicity.
  731. */
  732. if (V4L2_TYPE_IS_OUTPUT(b->type))
  733. v4l2_planes[0].bytesused = b->bytesused;
  734. if (b->memory == V4L2_MEMORY_USERPTR) {
  735. v4l2_planes[0].m.userptr = b->m.userptr;
  736. v4l2_planes[0].length = b->length;
  737. }
  738. }
  739. vb->v4l2_buf.field = b->field;
  740. vb->v4l2_buf.timestamp = b->timestamp;
  741. vb->v4l2_buf.input = b->input;
  742. vb->v4l2_buf.flags = b->flags & ~V4L2_BUFFER_STATE_FLAGS;
  743. return 0;
  744. }
  745. /**
  746. * __qbuf_userptr() - handle qbuf of a USERPTR buffer
  747. */
  748. static int __qbuf_userptr(struct vb2_buffer *vb, const struct v4l2_buffer *b)
  749. {
  750. struct v4l2_plane planes[VIDEO_MAX_PLANES];
  751. struct vb2_queue *q = vb->vb2_queue;
  752. void *mem_priv;
  753. unsigned int plane;
  754. int ret;
  755. int write = !V4L2_TYPE_IS_OUTPUT(q->type);
  756. /* Verify and copy relevant information provided by the userspace */
  757. ret = __fill_vb2_buffer(vb, b, planes);
  758. if (ret)
  759. return ret;
  760. for (plane = 0; plane < vb->num_planes; ++plane) {
  761. /* Skip the plane if already verified */
  762. if (vb->v4l2_planes[plane].m.userptr &&
  763. vb->v4l2_planes[plane].m.userptr == planes[plane].m.userptr
  764. && vb->v4l2_planes[plane].length == planes[plane].length)
  765. continue;
  766. dprintk(3, "qbuf: userspace address for plane %d changed, "
  767. "reacquiring memory\n", plane);
  768. /* Check if the provided plane buffer is large enough */
  769. if (planes[plane].length < q->plane_sizes[plane]) {
  770. ret = -EINVAL;
  771. goto err;
  772. }
  773. /* Release previously acquired memory if present */
  774. if (vb->planes[plane].mem_priv)
  775. call_memop(q, put_userptr, vb->planes[plane].mem_priv);
  776. vb->planes[plane].mem_priv = NULL;
  777. vb->v4l2_planes[plane].m.userptr = 0;
  778. vb->v4l2_planes[plane].length = 0;
  779. /* Acquire each plane's memory */
  780. mem_priv = call_memop(q, get_userptr, q->alloc_ctx[plane],
  781. planes[plane].m.userptr,
  782. planes[plane].length, write);
  783. if (IS_ERR_OR_NULL(mem_priv)) {
  784. dprintk(1, "qbuf: failed acquiring userspace "
  785. "memory for plane %d\n", plane);
  786. ret = mem_priv ? PTR_ERR(mem_priv) : -EINVAL;
  787. goto err;
  788. }
  789. vb->planes[plane].mem_priv = mem_priv;
  790. }
  791. /*
  792. * Call driver-specific initialization on the newly acquired buffer,
  793. * if provided.
  794. */
  795. ret = call_qop(q, buf_init, vb);
  796. if (ret) {
  797. dprintk(1, "qbuf: buffer initialization failed\n");
  798. goto err;
  799. }
  800. /*
  801. * Now that everything is in order, copy relevant information
  802. * provided by userspace.
  803. */
  804. for (plane = 0; plane < vb->num_planes; ++plane)
  805. vb->v4l2_planes[plane] = planes[plane];
  806. return 0;
  807. err:
  808. /* In case of errors, release planes that were already acquired */
  809. for (plane = 0; plane < vb->num_planes; ++plane) {
  810. if (vb->planes[plane].mem_priv)
  811. call_memop(q, put_userptr, vb->planes[plane].mem_priv);
  812. vb->planes[plane].mem_priv = NULL;
  813. vb->v4l2_planes[plane].m.userptr = 0;
  814. vb->v4l2_planes[plane].length = 0;
  815. }
  816. return ret;
  817. }
  818. /**
  819. * __qbuf_mmap() - handle qbuf of an MMAP buffer
  820. */
  821. static int __qbuf_mmap(struct vb2_buffer *vb, const struct v4l2_buffer *b)
  822. {
  823. return __fill_vb2_buffer(vb, b, vb->v4l2_planes);
  824. }
  825. /**
  826. * __enqueue_in_driver() - enqueue a vb2_buffer in driver for processing
  827. */
  828. static void __enqueue_in_driver(struct vb2_buffer *vb)
  829. {
  830. struct vb2_queue *q = vb->vb2_queue;
  831. vb->state = VB2_BUF_STATE_ACTIVE;
  832. atomic_inc(&q->queued_count);
  833. q->ops->buf_queue(vb);
  834. }
  835. static int __buf_prepare(struct vb2_buffer *vb, const struct v4l2_buffer *b)
  836. {
  837. struct vb2_queue *q = vb->vb2_queue;
  838. int ret;
  839. switch (q->memory) {
  840. case V4L2_MEMORY_MMAP:
  841. ret = __qbuf_mmap(vb, b);
  842. break;
  843. case V4L2_MEMORY_USERPTR:
  844. ret = __qbuf_userptr(vb, b);
  845. break;
  846. default:
  847. WARN(1, "Invalid queue type\n");
  848. ret = -EINVAL;
  849. }
  850. if (!ret)
  851. ret = call_qop(q, buf_prepare, vb);
  852. if (ret)
  853. dprintk(1, "qbuf: buffer preparation failed: %d\n", ret);
  854. else
  855. vb->state = VB2_BUF_STATE_PREPARED;
  856. return ret;
  857. }
  858. /**
  859. * vb2_prepare_buf() - Pass ownership of a buffer from userspace to the kernel
  860. * @q: videobuf2 queue
  861. * @b: buffer structure passed from userspace to vidioc_prepare_buf
  862. * handler in driver
  863. *
  864. * Should be called from vidioc_prepare_buf ioctl handler of a driver.
  865. * This function:
  866. * 1) verifies the passed buffer,
  867. * 2) calls buf_prepare callback in the driver (if provided), in which
  868. * driver-specific buffer initialization can be performed,
  869. *
  870. * The return values from this function are intended to be directly returned
  871. * from vidioc_prepare_buf handler in driver.
  872. */
  873. int vb2_prepare_buf(struct vb2_queue *q, struct v4l2_buffer *b)
  874. {
  875. struct vb2_buffer *vb;
  876. int ret;
  877. if (q->fileio) {
  878. dprintk(1, "%s(): file io in progress\n", __func__);
  879. return -EBUSY;
  880. }
  881. if (b->type != q->type) {
  882. dprintk(1, "%s(): invalid buffer type\n", __func__);
  883. return -EINVAL;
  884. }
  885. if (b->index >= q->num_buffers) {
  886. dprintk(1, "%s(): buffer index out of range\n", __func__);
  887. return -EINVAL;
  888. }
  889. vb = q->bufs[b->index];
  890. if (NULL == vb) {
  891. /* Should never happen */
  892. dprintk(1, "%s(): buffer is NULL\n", __func__);
  893. return -EINVAL;
  894. }
  895. if (b->memory != q->memory) {
  896. dprintk(1, "%s(): invalid memory type\n", __func__);
  897. return -EINVAL;
  898. }
  899. if (vb->state != VB2_BUF_STATE_DEQUEUED) {
  900. dprintk(1, "%s(): invalid buffer state %d\n", __func__, vb->state);
  901. return -EINVAL;
  902. }
  903. ret = __buf_prepare(vb, b);
  904. if (ret < 0)
  905. return ret;
  906. __fill_v4l2_buffer(vb, b);
  907. return 0;
  908. }
  909. EXPORT_SYMBOL_GPL(vb2_prepare_buf);
  910. /**
  911. * vb2_qbuf() - Queue a buffer from userspace
  912. * @q: videobuf2 queue
  913. * @b: buffer structure passed from userspace to vidioc_qbuf handler
  914. * in driver
  915. *
  916. * Should be called from vidioc_qbuf ioctl handler of a driver.
  917. * This function:
  918. * 1) verifies the passed buffer,
  919. * 2) if necessary, calls buf_prepare callback in the driver (if provided), in
  920. * which driver-specific buffer initialization can be performed,
  921. * 3) if streaming is on, queues the buffer in driver by the means of buf_queue
  922. * callback for processing.
  923. *
  924. * The return values from this function are intended to be directly returned
  925. * from vidioc_qbuf handler in driver.
  926. */
  927. int vb2_qbuf(struct vb2_queue *q, struct v4l2_buffer *b)
  928. {
  929. struct rw_semaphore *mmap_sem = NULL;
  930. struct vb2_buffer *vb;
  931. int ret = 0;
  932. /*
  933. * In case of user pointer buffers vb2 allocator needs to get direct
  934. * access to userspace pages. This requires getting read access on
  935. * mmap semaphore in the current process structure. The same
  936. * semaphore is taken before calling mmap operation, while both mmap
  937. * and qbuf are called by the driver or v4l2 core with driver's lock
  938. * held. To avoid a AB-BA deadlock (mmap_sem then driver's lock in
  939. * mmap and driver's lock then mmap_sem in qbuf) the videobuf2 core
  940. * release driver's lock, takes mmap_sem and then takes again driver's
  941. * lock.
  942. *
  943. * To avoid race with other vb2 calls, which might be called after
  944. * releasing driver's lock, this operation is performed at the
  945. * beggining of qbuf processing. This way the queue status is
  946. * consistent after getting driver's lock back.
  947. */
  948. if (q->memory == V4L2_MEMORY_USERPTR) {
  949. mmap_sem = &current->mm->mmap_sem;
  950. call_qop(q, wait_prepare, q);
  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. * If the driver uses struct v4l2_fh, then vb2_poll() will also check for any
  1426. * pending events.
  1427. *
  1428. * The return values from this function are intended to be directly returned
  1429. * from poll handler in driver.
  1430. */
  1431. unsigned int vb2_poll(struct vb2_queue *q, struct file *file, poll_table *wait)
  1432. {
  1433. struct video_device *vfd = video_devdata(file);
  1434. unsigned long req_events = poll_requested_events(wait);
  1435. struct vb2_buffer *vb = NULL;
  1436. unsigned int res = 0;
  1437. unsigned long flags;
  1438. if (test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags)) {
  1439. struct v4l2_fh *fh = file->private_data;
  1440. if (v4l2_event_pending(fh))
  1441. res = POLLPRI;
  1442. else if (req_events & POLLPRI)
  1443. poll_wait(file, &fh->wait, wait);
  1444. }
  1445. /*
  1446. * Start file I/O emulator only if streaming API has not been used yet.
  1447. */
  1448. if (q->num_buffers == 0 && q->fileio == NULL) {
  1449. if (!V4L2_TYPE_IS_OUTPUT(q->type) && (q->io_modes & VB2_READ) &&
  1450. (req_events & (POLLIN | POLLRDNORM))) {
  1451. if (__vb2_init_fileio(q, 1))
  1452. return res | POLLERR;
  1453. }
  1454. if (V4L2_TYPE_IS_OUTPUT(q->type) && (q->io_modes & VB2_WRITE) &&
  1455. (req_events & (POLLOUT | POLLWRNORM))) {
  1456. if (__vb2_init_fileio(q, 0))
  1457. return res | POLLERR;
  1458. /*
  1459. * Write to OUTPUT queue can be done immediately.
  1460. */
  1461. return res | POLLOUT | POLLWRNORM;
  1462. }
  1463. }
  1464. /*
  1465. * There is nothing to wait for if no buffers have already been queued.
  1466. */
  1467. if (list_empty(&q->queued_list))
  1468. return res | POLLERR;
  1469. poll_wait(file, &q->done_wq, wait);
  1470. /*
  1471. * Take first buffer available for dequeuing.
  1472. */
  1473. spin_lock_irqsave(&q->done_lock, flags);
  1474. if (!list_empty(&q->done_list))
  1475. vb = list_first_entry(&q->done_list, struct vb2_buffer,
  1476. done_entry);
  1477. spin_unlock_irqrestore(&q->done_lock, flags);
  1478. if (vb && (vb->state == VB2_BUF_STATE_DONE
  1479. || vb->state == VB2_BUF_STATE_ERROR)) {
  1480. return (V4L2_TYPE_IS_OUTPUT(q->type)) ?
  1481. res | POLLOUT | POLLWRNORM :
  1482. res | POLLIN | POLLRDNORM;
  1483. }
  1484. return res;
  1485. }
  1486. EXPORT_SYMBOL_GPL(vb2_poll);
  1487. /**
  1488. * vb2_queue_init() - initialize a videobuf2 queue
  1489. * @q: videobuf2 queue; this structure should be allocated in driver
  1490. *
  1491. * The vb2_queue structure should be allocated by the driver. The driver is
  1492. * responsible of clearing it's content and setting initial values for some
  1493. * required entries before calling this function.
  1494. * q->ops, q->mem_ops, q->type and q->io_modes are mandatory. Please refer
  1495. * to the struct vb2_queue description in include/media/videobuf2-core.h
  1496. * for more information.
  1497. */
  1498. int vb2_queue_init(struct vb2_queue *q)
  1499. {
  1500. BUG_ON(!q);
  1501. BUG_ON(!q->ops);
  1502. BUG_ON(!q->mem_ops);
  1503. BUG_ON(!q->type);
  1504. BUG_ON(!q->io_modes);
  1505. BUG_ON(!q->ops->queue_setup);
  1506. BUG_ON(!q->ops->buf_queue);
  1507. INIT_LIST_HEAD(&q->queued_list);
  1508. INIT_LIST_HEAD(&q->done_list);
  1509. spin_lock_init(&q->done_lock);
  1510. init_waitqueue_head(&q->done_wq);
  1511. if (q->buf_struct_size == 0)
  1512. q->buf_struct_size = sizeof(struct vb2_buffer);
  1513. return 0;
  1514. }
  1515. EXPORT_SYMBOL_GPL(vb2_queue_init);
  1516. /**
  1517. * vb2_queue_release() - stop streaming, release the queue and free memory
  1518. * @q: videobuf2 queue
  1519. *
  1520. * This function stops streaming and performs necessary clean ups, including
  1521. * freeing video buffer memory. The driver is responsible for freeing
  1522. * the vb2_queue structure itself.
  1523. */
  1524. void vb2_queue_release(struct vb2_queue *q)
  1525. {
  1526. __vb2_cleanup_fileio(q);
  1527. __vb2_queue_cancel(q);
  1528. __vb2_queue_free(q, q->num_buffers);
  1529. }
  1530. EXPORT_SYMBOL_GPL(vb2_queue_release);
  1531. /**
  1532. * struct vb2_fileio_buf - buffer 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. This structure is used for
  1536. * tracking context related to the buffers.
  1537. */
  1538. struct vb2_fileio_buf {
  1539. void *vaddr;
  1540. unsigned int size;
  1541. unsigned int pos;
  1542. unsigned int queued:1;
  1543. };
  1544. /**
  1545. * struct vb2_fileio_data - queue context used by file io emulator
  1546. *
  1547. * vb2 provides a compatibility layer and emulator of file io (read and
  1548. * write) calls on top of streaming API. For proper operation it required
  1549. * this structure to save the driver state between each call of the read
  1550. * or write function.
  1551. */
  1552. struct vb2_fileio_data {
  1553. struct v4l2_requestbuffers req;
  1554. struct v4l2_buffer b;
  1555. struct vb2_fileio_buf bufs[VIDEO_MAX_FRAME];
  1556. unsigned int index;
  1557. unsigned int q_count;
  1558. unsigned int dq_count;
  1559. unsigned int flags;
  1560. };
  1561. /**
  1562. * __vb2_init_fileio() - initialize file io emulator
  1563. * @q: videobuf2 queue
  1564. * @read: mode selector (1 means read, 0 means write)
  1565. */
  1566. static int __vb2_init_fileio(struct vb2_queue *q, int read)
  1567. {
  1568. struct vb2_fileio_data *fileio;
  1569. int i, ret;
  1570. unsigned int count = 0;
  1571. /*
  1572. * Sanity check
  1573. */
  1574. if ((read && !(q->io_modes & VB2_READ)) ||
  1575. (!read && !(q->io_modes & VB2_WRITE)))
  1576. BUG();
  1577. /*
  1578. * Check if device supports mapping buffers to kernel virtual space.
  1579. */
  1580. if (!q->mem_ops->vaddr)
  1581. return -EBUSY;
  1582. /*
  1583. * Check if streaming api has not been already activated.
  1584. */
  1585. if (q->streaming || q->num_buffers > 0)
  1586. return -EBUSY;
  1587. /*
  1588. * Start with count 1, driver can increase it in queue_setup()
  1589. */
  1590. count = 1;
  1591. dprintk(3, "setting up file io: mode %s, count %d, flags %08x\n",
  1592. (read) ? "read" : "write", count, q->io_flags);
  1593. fileio = kzalloc(sizeof(struct vb2_fileio_data), GFP_KERNEL);
  1594. if (fileio == NULL)
  1595. return -ENOMEM;
  1596. fileio->flags = q->io_flags;
  1597. /*
  1598. * Request buffers and use MMAP type to force driver
  1599. * to allocate buffers by itself.
  1600. */
  1601. fileio->req.count = count;
  1602. fileio->req.memory = V4L2_MEMORY_MMAP;
  1603. fileio->req.type = q->type;
  1604. ret = vb2_reqbufs(q, &fileio->req);
  1605. if (ret)
  1606. goto err_kfree;
  1607. /*
  1608. * Check if plane_count is correct
  1609. * (multiplane buffers are not supported).
  1610. */
  1611. if (q->bufs[0]->num_planes != 1) {
  1612. ret = -EBUSY;
  1613. goto err_reqbufs;
  1614. }
  1615. /*
  1616. * Get kernel address of each buffer.
  1617. */
  1618. for (i = 0; i < q->num_buffers; i++) {
  1619. fileio->bufs[i].vaddr = vb2_plane_vaddr(q->bufs[i], 0);
  1620. if (fileio->bufs[i].vaddr == NULL)
  1621. goto err_reqbufs;
  1622. fileio->bufs[i].size = vb2_plane_size(q->bufs[i], 0);
  1623. }
  1624. /*
  1625. * Read mode requires pre queuing of all buffers.
  1626. */
  1627. if (read) {
  1628. /*
  1629. * Queue all buffers.
  1630. */
  1631. for (i = 0; i < q->num_buffers; i++) {
  1632. struct v4l2_buffer *b = &fileio->b;
  1633. memset(b, 0, sizeof(*b));
  1634. b->type = q->type;
  1635. b->memory = q->memory;
  1636. b->index = i;
  1637. ret = vb2_qbuf(q, b);
  1638. if (ret)
  1639. goto err_reqbufs;
  1640. fileio->bufs[i].queued = 1;
  1641. }
  1642. /*
  1643. * Start streaming.
  1644. */
  1645. ret = vb2_streamon(q, q->type);
  1646. if (ret)
  1647. goto err_reqbufs;
  1648. }
  1649. q->fileio = fileio;
  1650. return ret;
  1651. err_reqbufs:
  1652. fileio->req.count = 0;
  1653. vb2_reqbufs(q, &fileio->req);
  1654. err_kfree:
  1655. kfree(fileio);
  1656. return ret;
  1657. }
  1658. /**
  1659. * __vb2_cleanup_fileio() - free resourced used by file io emulator
  1660. * @q: videobuf2 queue
  1661. */
  1662. static int __vb2_cleanup_fileio(struct vb2_queue *q)
  1663. {
  1664. struct vb2_fileio_data *fileio = q->fileio;
  1665. if (fileio) {
  1666. /*
  1667. * Hack fileio context to enable direct calls to vb2 ioctl
  1668. * interface.
  1669. */
  1670. q->fileio = NULL;
  1671. vb2_streamoff(q, q->type);
  1672. fileio->req.count = 0;
  1673. vb2_reqbufs(q, &fileio->req);
  1674. kfree(fileio);
  1675. dprintk(3, "file io emulator closed\n");
  1676. }
  1677. return 0;
  1678. }
  1679. /**
  1680. * __vb2_perform_fileio() - perform a single file io (read or write) operation
  1681. * @q: videobuf2 queue
  1682. * @data: pointed to target userspace buffer
  1683. * @count: number of bytes to read or write
  1684. * @ppos: file handle position tracking pointer
  1685. * @nonblock: mode selector (1 means blocking calls, 0 means nonblocking)
  1686. * @read: access mode selector (1 means read, 0 means write)
  1687. */
  1688. static size_t __vb2_perform_fileio(struct vb2_queue *q, char __user *data, size_t count,
  1689. loff_t *ppos, int nonblock, int read)
  1690. {
  1691. struct vb2_fileio_data *fileio;
  1692. struct vb2_fileio_buf *buf;
  1693. int ret, index;
  1694. dprintk(3, "file io: mode %s, offset %ld, count %zd, %sblocking\n",
  1695. read ? "read" : "write", (long)*ppos, count,
  1696. nonblock ? "non" : "");
  1697. if (!data)
  1698. return -EINVAL;
  1699. /*
  1700. * Initialize emulator on first call.
  1701. */
  1702. if (!q->fileio) {
  1703. ret = __vb2_init_fileio(q, read);
  1704. dprintk(3, "file io: vb2_init_fileio result: %d\n", ret);
  1705. if (ret)
  1706. return ret;
  1707. }
  1708. fileio = q->fileio;
  1709. /*
  1710. * Hack fileio context to enable direct calls to vb2 ioctl interface.
  1711. * The pointer will be restored before returning from this function.
  1712. */
  1713. q->fileio = NULL;
  1714. index = fileio->index;
  1715. buf = &fileio->bufs[index];
  1716. /*
  1717. * Check if we need to dequeue the buffer.
  1718. */
  1719. if (buf->queued) {
  1720. struct vb2_buffer *vb;
  1721. /*
  1722. * Call vb2_dqbuf to get buffer back.
  1723. */
  1724. memset(&fileio->b, 0, sizeof(fileio->b));
  1725. fileio->b.type = q->type;
  1726. fileio->b.memory = q->memory;
  1727. fileio->b.index = index;
  1728. ret = vb2_dqbuf(q, &fileio->b, nonblock);
  1729. dprintk(5, "file io: vb2_dqbuf result: %d\n", ret);
  1730. if (ret)
  1731. goto end;
  1732. fileio->dq_count += 1;
  1733. /*
  1734. * Get number of bytes filled by the driver
  1735. */
  1736. vb = q->bufs[index];
  1737. buf->size = vb2_get_plane_payload(vb, 0);
  1738. buf->queued = 0;
  1739. }
  1740. /*
  1741. * Limit count on last few bytes of the buffer.
  1742. */
  1743. if (buf->pos + count > buf->size) {
  1744. count = buf->size - buf->pos;
  1745. dprintk(5, "reducing read count: %zd\n", count);
  1746. }
  1747. /*
  1748. * Transfer data to userspace.
  1749. */
  1750. dprintk(3, "file io: copying %zd bytes - buffer %d, offset %u\n",
  1751. count, index, buf->pos);
  1752. if (read)
  1753. ret = copy_to_user(data, buf->vaddr + buf->pos, count);
  1754. else
  1755. ret = copy_from_user(buf->vaddr + buf->pos, data, count);
  1756. if (ret) {
  1757. dprintk(3, "file io: error copying data\n");
  1758. ret = -EFAULT;
  1759. goto end;
  1760. }
  1761. /*
  1762. * Update counters.
  1763. */
  1764. buf->pos += count;
  1765. *ppos += count;
  1766. /*
  1767. * Queue next buffer if required.
  1768. */
  1769. if (buf->pos == buf->size ||
  1770. (!read && (fileio->flags & VB2_FILEIO_WRITE_IMMEDIATELY))) {
  1771. /*
  1772. * Check if this is the last buffer to read.
  1773. */
  1774. if (read && (fileio->flags & VB2_FILEIO_READ_ONCE) &&
  1775. fileio->dq_count == 1) {
  1776. dprintk(3, "file io: read limit reached\n");
  1777. /*
  1778. * Restore fileio pointer and release the context.
  1779. */
  1780. q->fileio = fileio;
  1781. return __vb2_cleanup_fileio(q);
  1782. }
  1783. /*
  1784. * Call vb2_qbuf and give buffer to the driver.
  1785. */
  1786. memset(&fileio->b, 0, sizeof(fileio->b));
  1787. fileio->b.type = q->type;
  1788. fileio->b.memory = q->memory;
  1789. fileio->b.index = index;
  1790. fileio->b.bytesused = buf->pos;
  1791. ret = vb2_qbuf(q, &fileio->b);
  1792. dprintk(5, "file io: vb2_dbuf result: %d\n", ret);
  1793. if (ret)
  1794. goto end;
  1795. /*
  1796. * Buffer has been queued, update the status
  1797. */
  1798. buf->pos = 0;
  1799. buf->queued = 1;
  1800. buf->size = q->bufs[0]->v4l2_planes[0].length;
  1801. fileio->q_count += 1;
  1802. /*
  1803. * Switch to the next buffer
  1804. */
  1805. fileio->index = (index + 1) % q->num_buffers;
  1806. /*
  1807. * Start streaming if required.
  1808. */
  1809. if (!read && !q->streaming) {
  1810. ret = vb2_streamon(q, q->type);
  1811. if (ret)
  1812. goto end;
  1813. }
  1814. }
  1815. /*
  1816. * Return proper number of bytes processed.
  1817. */
  1818. if (ret == 0)
  1819. ret = count;
  1820. end:
  1821. /*
  1822. * Restore the fileio context and block vb2 ioctl interface.
  1823. */
  1824. q->fileio = fileio;
  1825. return ret;
  1826. }
  1827. size_t vb2_read(struct vb2_queue *q, char __user *data, size_t count,
  1828. loff_t *ppos, int nonblocking)
  1829. {
  1830. return __vb2_perform_fileio(q, data, count, ppos, nonblocking, 1);
  1831. }
  1832. EXPORT_SYMBOL_GPL(vb2_read);
  1833. size_t vb2_write(struct vb2_queue *q, char __user *data, size_t count,
  1834. loff_t *ppos, int nonblocking)
  1835. {
  1836. return __vb2_perform_fileio(q, data, count, ppos, nonblocking, 0);
  1837. }
  1838. EXPORT_SYMBOL_GPL(vb2_write);
  1839. MODULE_DESCRIPTION("Driver helper framework for Video for Linux 2");
  1840. MODULE_AUTHOR("Pawel Osciak <pawel@osciak.com>, Marek Szyprowski");
  1841. MODULE_LICENSE("GPL");