videobuf2-core.c 46 KB

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