videobuf2-core.c 46 KB

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