videobuf2-core.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405
  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 (req->memory != V4L2_MEMORY_MMAP
  393. && req->memory != V4L2_MEMORY_USERPTR) {
  394. dprintk(1, "reqbufs: unsupported memory type\n");
  395. return -EINVAL;
  396. }
  397. if (req->type != q->type) {
  398. dprintk(1, "reqbufs: requested type is incorrect\n");
  399. return -EINVAL;
  400. }
  401. if (q->streaming) {
  402. dprintk(1, "reqbufs: streaming active\n");
  403. return -EBUSY;
  404. }
  405. /*
  406. * Make sure all the required memory ops for given memory type
  407. * are available.
  408. */
  409. if (req->memory == V4L2_MEMORY_MMAP && __verify_mmap_ops(q)) {
  410. dprintk(1, "reqbufs: MMAP for current setup unsupported\n");
  411. return -EINVAL;
  412. }
  413. if (req->memory == V4L2_MEMORY_USERPTR && __verify_userptr_ops(q)) {
  414. dprintk(1, "reqbufs: USERPTR for current setup unsupported\n");
  415. return -EINVAL;
  416. }
  417. if (req->count == 0 || q->num_buffers != 0) {
  418. /*
  419. * We already have buffers allocated, so first check if they
  420. * are not in use and can be freed.
  421. */
  422. if (q->memory == V4L2_MEMORY_MMAP && __buffers_in_use(q)) {
  423. dprintk(1, "reqbufs: memory in use, cannot free\n");
  424. return -EBUSY;
  425. }
  426. ret = __vb2_queue_free(q);
  427. if (ret != 0)
  428. return ret;
  429. }
  430. /*
  431. * Make sure the requested values and current defaults are sane.
  432. */
  433. num_buffers = min_t(unsigned int, req->count, VIDEO_MAX_FRAME);
  434. memset(plane_sizes, 0, sizeof(plane_sizes));
  435. memset(q->alloc_ctx, 0, sizeof(q->alloc_ctx));
  436. /*
  437. * Ask the driver how many buffers and planes per buffer it requires.
  438. * Driver also sets the size and allocator context for each plane.
  439. */
  440. ret = call_qop(q, queue_setup, q, &num_buffers, &num_planes,
  441. plane_sizes, q->alloc_ctx);
  442. if (ret)
  443. return ret;
  444. /* Finally, allocate buffers and video memory */
  445. ret = __vb2_queue_alloc(q, req->memory, num_buffers, num_planes,
  446. plane_sizes);
  447. if (ret < 0) {
  448. dprintk(1, "Memory allocation failed with error: %d\n", ret);
  449. return ret;
  450. }
  451. /*
  452. * Check if driver can handle the allocated number of buffers.
  453. */
  454. if (ret < num_buffers) {
  455. unsigned int orig_num_buffers;
  456. orig_num_buffers = num_buffers = ret;
  457. ret = call_qop(q, queue_setup, q, &num_buffers, &num_planes,
  458. plane_sizes, q->alloc_ctx);
  459. if (ret)
  460. goto free_mem;
  461. if (orig_num_buffers < num_buffers) {
  462. ret = -ENOMEM;
  463. goto free_mem;
  464. }
  465. /*
  466. * Ok, driver accepted smaller number of buffers.
  467. */
  468. ret = num_buffers;
  469. }
  470. q->memory = req->memory;
  471. /*
  472. * Return the number of successfully allocated buffers
  473. * to the userspace.
  474. */
  475. req->count = ret;
  476. return 0;
  477. free_mem:
  478. __vb2_queue_free(q);
  479. return ret;
  480. }
  481. EXPORT_SYMBOL_GPL(vb2_reqbufs);
  482. /**
  483. * vb2_plane_vaddr() - Return a kernel virtual address of a given plane
  484. * @vb: vb2_buffer to which the plane in question belongs to
  485. * @plane_no: plane number for which the address is to be returned
  486. *
  487. * This function returns a kernel virtual address of a given plane if
  488. * such a mapping exist, NULL otherwise.
  489. */
  490. void *vb2_plane_vaddr(struct vb2_buffer *vb, unsigned int plane_no)
  491. {
  492. struct vb2_queue *q = vb->vb2_queue;
  493. if (plane_no > vb->num_planes)
  494. return NULL;
  495. return call_memop(q, plane_no, vaddr, vb->planes[plane_no].mem_priv);
  496. }
  497. EXPORT_SYMBOL_GPL(vb2_plane_vaddr);
  498. /**
  499. * vb2_plane_cookie() - Return allocator specific cookie for the given plane
  500. * @vb: vb2_buffer to which the plane in question belongs to
  501. * @plane_no: plane number for which the cookie is to be returned
  502. *
  503. * This function returns an allocator specific cookie for a given plane if
  504. * available, NULL otherwise. The allocator should provide some simple static
  505. * inline function, which would convert this cookie to the allocator specific
  506. * type that can be used directly by the driver to access the buffer. This can
  507. * be for example physical address, pointer to scatter list or IOMMU mapping.
  508. */
  509. void *vb2_plane_cookie(struct vb2_buffer *vb, unsigned int plane_no)
  510. {
  511. struct vb2_queue *q = vb->vb2_queue;
  512. if (plane_no > vb->num_planes)
  513. return NULL;
  514. return call_memop(q, plane_no, cookie, vb->planes[plane_no].mem_priv);
  515. }
  516. EXPORT_SYMBOL_GPL(vb2_plane_cookie);
  517. /**
  518. * vb2_buffer_done() - inform videobuf that an operation on a buffer is finished
  519. * @vb: vb2_buffer returned from the driver
  520. * @state: either VB2_BUF_STATE_DONE if the operation finished successfully
  521. * or VB2_BUF_STATE_ERROR if the operation finished with an error
  522. *
  523. * This function should be called by the driver after a hardware operation on
  524. * a buffer is finished and the buffer may be returned to userspace. The driver
  525. * cannot use this buffer anymore until it is queued back to it by videobuf
  526. * by the means of buf_queue callback. Only buffers previously queued to the
  527. * driver by buf_queue can be passed to this function.
  528. */
  529. void vb2_buffer_done(struct vb2_buffer *vb, enum vb2_buffer_state state)
  530. {
  531. struct vb2_queue *q = vb->vb2_queue;
  532. unsigned long flags;
  533. if (vb->state != VB2_BUF_STATE_ACTIVE)
  534. return;
  535. if (state != VB2_BUF_STATE_DONE && state != VB2_BUF_STATE_ERROR)
  536. return;
  537. dprintk(4, "Done processing on buffer %d, state: %d\n",
  538. vb->v4l2_buf.index, vb->state);
  539. /* Add the buffer to the done buffers list */
  540. spin_lock_irqsave(&q->done_lock, flags);
  541. vb->state = state;
  542. list_add_tail(&vb->done_entry, &q->done_list);
  543. atomic_dec(&q->queued_count);
  544. spin_unlock_irqrestore(&q->done_lock, flags);
  545. /* Inform any processes that may be waiting for buffers */
  546. wake_up(&q->done_wq);
  547. }
  548. EXPORT_SYMBOL_GPL(vb2_buffer_done);
  549. /**
  550. * __fill_vb2_buffer() - fill a vb2_buffer with information provided in
  551. * a v4l2_buffer by the userspace
  552. */
  553. static int __fill_vb2_buffer(struct vb2_buffer *vb, struct v4l2_buffer *b,
  554. struct v4l2_plane *v4l2_planes)
  555. {
  556. unsigned int plane;
  557. int ret;
  558. if (V4L2_TYPE_IS_MULTIPLANAR(b->type)) {
  559. /*
  560. * Verify that the userspace gave us a valid array for
  561. * plane information.
  562. */
  563. ret = __verify_planes_array(vb, b);
  564. if (ret)
  565. return ret;
  566. /* Fill in driver-provided information for OUTPUT types */
  567. if (V4L2_TYPE_IS_OUTPUT(b->type)) {
  568. /*
  569. * Will have to go up to b->length when API starts
  570. * accepting variable number of planes.
  571. */
  572. for (plane = 0; plane < vb->num_planes; ++plane) {
  573. v4l2_planes[plane].bytesused =
  574. b->m.planes[plane].bytesused;
  575. v4l2_planes[plane].data_offset =
  576. b->m.planes[plane].data_offset;
  577. }
  578. }
  579. if (b->memory == V4L2_MEMORY_USERPTR) {
  580. for (plane = 0; plane < vb->num_planes; ++plane) {
  581. v4l2_planes[plane].m.userptr =
  582. b->m.planes[plane].m.userptr;
  583. v4l2_planes[plane].length =
  584. b->m.planes[plane].length;
  585. }
  586. }
  587. } else {
  588. /*
  589. * Single-planar buffers do not use planes array,
  590. * so fill in relevant v4l2_buffer struct fields instead.
  591. * In videobuf we use our internal V4l2_planes struct for
  592. * single-planar buffers as well, for simplicity.
  593. */
  594. if (V4L2_TYPE_IS_OUTPUT(b->type))
  595. v4l2_planes[0].bytesused = b->bytesused;
  596. if (b->memory == V4L2_MEMORY_USERPTR) {
  597. v4l2_planes[0].m.userptr = b->m.userptr;
  598. v4l2_planes[0].length = b->length;
  599. }
  600. }
  601. vb->v4l2_buf.field = b->field;
  602. vb->v4l2_buf.timestamp = b->timestamp;
  603. return 0;
  604. }
  605. /**
  606. * __qbuf_userptr() - handle qbuf of a USERPTR buffer
  607. */
  608. static int __qbuf_userptr(struct vb2_buffer *vb, struct v4l2_buffer *b)
  609. {
  610. struct v4l2_plane planes[VIDEO_MAX_PLANES];
  611. struct vb2_queue *q = vb->vb2_queue;
  612. void *mem_priv;
  613. unsigned int plane;
  614. int ret;
  615. int write = !V4L2_TYPE_IS_OUTPUT(q->type);
  616. /* Verify and copy relevant information provided by the userspace */
  617. ret = __fill_vb2_buffer(vb, b, planes);
  618. if (ret)
  619. return ret;
  620. for (plane = 0; plane < vb->num_planes; ++plane) {
  621. /* Skip the plane if already verified */
  622. if (vb->v4l2_planes[plane].m.userptr == planes[plane].m.userptr
  623. && vb->v4l2_planes[plane].length == planes[plane].length)
  624. continue;
  625. dprintk(3, "qbuf: userspace address for plane %d changed, "
  626. "reacquiring memory\n", plane);
  627. /* Release previously acquired memory if present */
  628. if (vb->planes[plane].mem_priv)
  629. call_memop(q, plane, put_userptr,
  630. vb->planes[plane].mem_priv);
  631. vb->planes[plane].mem_priv = NULL;
  632. /* Acquire each plane's memory */
  633. if (q->mem_ops->get_userptr) {
  634. mem_priv = q->mem_ops->get_userptr(q->alloc_ctx[plane],
  635. planes[plane].m.userptr,
  636. planes[plane].length,
  637. write);
  638. if (IS_ERR(mem_priv)) {
  639. dprintk(1, "qbuf: failed acquiring userspace "
  640. "memory for plane %d\n", plane);
  641. ret = PTR_ERR(mem_priv);
  642. goto err;
  643. }
  644. vb->planes[plane].mem_priv = mem_priv;
  645. }
  646. }
  647. /*
  648. * Call driver-specific initialization on the newly acquired buffer,
  649. * if provided.
  650. */
  651. ret = call_qop(q, buf_init, vb);
  652. if (ret) {
  653. dprintk(1, "qbuf: buffer initialization failed\n");
  654. goto err;
  655. }
  656. /*
  657. * Now that everything is in order, copy relevant information
  658. * provided by userspace.
  659. */
  660. for (plane = 0; plane < vb->num_planes; ++plane)
  661. vb->v4l2_planes[plane] = planes[plane];
  662. return 0;
  663. err:
  664. /* In case of errors, release planes that were already acquired */
  665. for (; plane > 0; --plane) {
  666. call_memop(q, plane, put_userptr,
  667. vb->planes[plane - 1].mem_priv);
  668. vb->planes[plane - 1].mem_priv = NULL;
  669. }
  670. return ret;
  671. }
  672. /**
  673. * __qbuf_mmap() - handle qbuf of an MMAP buffer
  674. */
  675. static int __qbuf_mmap(struct vb2_buffer *vb, struct v4l2_buffer *b)
  676. {
  677. return __fill_vb2_buffer(vb, b, vb->v4l2_planes);
  678. }
  679. /**
  680. * __enqueue_in_driver() - enqueue a vb2_buffer in driver for processing
  681. */
  682. static void __enqueue_in_driver(struct vb2_buffer *vb)
  683. {
  684. struct vb2_queue *q = vb->vb2_queue;
  685. vb->state = VB2_BUF_STATE_ACTIVE;
  686. atomic_inc(&q->queued_count);
  687. q->ops->buf_queue(vb);
  688. }
  689. /**
  690. * vb2_qbuf() - Queue a buffer from userspace
  691. * @q: videobuf2 queue
  692. * @b: buffer structure passed from userspace to vidioc_qbuf handler
  693. * in driver
  694. *
  695. * Should be called from vidioc_qbuf ioctl handler of a driver.
  696. * This function:
  697. * 1) verifies the passed buffer,
  698. * 2) calls buf_prepare callback in the driver (if provided), in which
  699. * driver-specific buffer initialization can be performed,
  700. * 3) if streaming is on, queues the buffer in driver by the means of buf_queue
  701. * callback for processing.
  702. *
  703. * The return values from this function are intended to be directly returned
  704. * from vidioc_qbuf handler in driver.
  705. */
  706. int vb2_qbuf(struct vb2_queue *q, struct v4l2_buffer *b)
  707. {
  708. struct vb2_buffer *vb;
  709. int ret = 0;
  710. if (b->type != q->type) {
  711. dprintk(1, "qbuf: invalid buffer type\n");
  712. return -EINVAL;
  713. }
  714. if (b->index >= q->num_buffers) {
  715. dprintk(1, "qbuf: buffer index out of range\n");
  716. return -EINVAL;
  717. }
  718. vb = q->bufs[b->index];
  719. if (NULL == vb) {
  720. /* Should never happen */
  721. dprintk(1, "qbuf: buffer is NULL\n");
  722. return -EINVAL;
  723. }
  724. if (b->memory != q->memory) {
  725. dprintk(1, "qbuf: invalid memory type\n");
  726. return -EINVAL;
  727. }
  728. if (vb->state != VB2_BUF_STATE_DEQUEUED) {
  729. dprintk(1, "qbuf: buffer already in use\n");
  730. return -EINVAL;
  731. }
  732. if (q->memory == V4L2_MEMORY_MMAP)
  733. ret = __qbuf_mmap(vb, b);
  734. else if (q->memory == V4L2_MEMORY_USERPTR)
  735. ret = __qbuf_userptr(vb, b);
  736. else {
  737. WARN(1, "Invalid queue type\n");
  738. return -EINVAL;
  739. }
  740. if (ret)
  741. return ret;
  742. ret = call_qop(q, buf_prepare, vb);
  743. if (ret) {
  744. dprintk(1, "qbuf: buffer preparation failed\n");
  745. return ret;
  746. }
  747. /*
  748. * Add to the queued buffers list, a buffer will stay on it until
  749. * dequeued in dqbuf.
  750. */
  751. list_add_tail(&vb->queued_entry, &q->queued_list);
  752. vb->state = VB2_BUF_STATE_QUEUED;
  753. /*
  754. * If already streaming, give the buffer to driver for processing.
  755. * If not, the buffer will be given to driver on next streamon.
  756. */
  757. if (q->streaming)
  758. __enqueue_in_driver(vb);
  759. dprintk(1, "qbuf of buffer %d succeeded\n", vb->v4l2_buf.index);
  760. return 0;
  761. }
  762. EXPORT_SYMBOL_GPL(vb2_qbuf);
  763. /**
  764. * __vb2_wait_for_done_vb() - wait for a buffer to become available
  765. * for dequeuing
  766. *
  767. * Will sleep if required for nonblocking == false.
  768. */
  769. static int __vb2_wait_for_done_vb(struct vb2_queue *q, int nonblocking)
  770. {
  771. /*
  772. * All operations on vb_done_list are performed under done_lock
  773. * spinlock protection. However, buffers may be removed from
  774. * it and returned to userspace only while holding both driver's
  775. * lock and the done_lock spinlock. Thus we can be sure that as
  776. * long as we hold the driver's lock, the list will remain not
  777. * empty if list_empty() check succeeds.
  778. */
  779. for (;;) {
  780. int ret;
  781. if (!q->streaming) {
  782. dprintk(1, "Streaming off, will not wait for buffers\n");
  783. return -EINVAL;
  784. }
  785. if (!list_empty(&q->done_list)) {
  786. /*
  787. * Found a buffer that we were waiting for.
  788. */
  789. break;
  790. }
  791. if (nonblocking) {
  792. dprintk(1, "Nonblocking and no buffers to dequeue, "
  793. "will not wait\n");
  794. return -EAGAIN;
  795. }
  796. /*
  797. * We are streaming and blocking, wait for another buffer to
  798. * become ready or for streamoff. Driver's lock is released to
  799. * allow streamoff or qbuf to be called while waiting.
  800. */
  801. call_qop(q, wait_prepare, q);
  802. /*
  803. * All locks have been released, it is safe to sleep now.
  804. */
  805. dprintk(3, "Will sleep waiting for buffers\n");
  806. ret = wait_event_interruptible(q->done_wq,
  807. !list_empty(&q->done_list) || !q->streaming);
  808. /*
  809. * We need to reevaluate both conditions again after reacquiring
  810. * the locks or return an error if one occurred.
  811. */
  812. call_qop(q, wait_finish, q);
  813. if (ret)
  814. return ret;
  815. }
  816. return 0;
  817. }
  818. /**
  819. * __vb2_get_done_vb() - get a buffer ready for dequeuing
  820. *
  821. * Will sleep if required for nonblocking == false.
  822. */
  823. static int __vb2_get_done_vb(struct vb2_queue *q, struct vb2_buffer **vb,
  824. int nonblocking)
  825. {
  826. unsigned long flags;
  827. int ret;
  828. /*
  829. * Wait for at least one buffer to become available on the done_list.
  830. */
  831. ret = __vb2_wait_for_done_vb(q, nonblocking);
  832. if (ret)
  833. return ret;
  834. /*
  835. * Driver's lock has been held since we last verified that done_list
  836. * is not empty, so no need for another list_empty(done_list) check.
  837. */
  838. spin_lock_irqsave(&q->done_lock, flags);
  839. *vb = list_first_entry(&q->done_list, struct vb2_buffer, done_entry);
  840. list_del(&(*vb)->done_entry);
  841. spin_unlock_irqrestore(&q->done_lock, flags);
  842. return 0;
  843. }
  844. /**
  845. * vb2_wait_for_all_buffers() - wait until all buffers are given back to vb2
  846. * @q: videobuf2 queue
  847. *
  848. * This function will wait until all buffers that have been given to the driver
  849. * by buf_queue() are given back to vb2 with vb2_buffer_done(). It doesn't call
  850. * wait_prepare, wait_finish pair. It is intended to be called with all locks
  851. * taken, for example from stop_streaming() callback.
  852. */
  853. int vb2_wait_for_all_buffers(struct vb2_queue *q)
  854. {
  855. if (!q->streaming) {
  856. dprintk(1, "Streaming off, will not wait for buffers\n");
  857. return -EINVAL;
  858. }
  859. wait_event(q->done_wq, !atomic_read(&q->queued_count));
  860. return 0;
  861. }
  862. EXPORT_SYMBOL_GPL(vb2_wait_for_all_buffers);
  863. /**
  864. * vb2_dqbuf() - Dequeue a buffer to the userspace
  865. * @q: videobuf2 queue
  866. * @b: buffer structure passed from userspace to vidioc_dqbuf handler
  867. * in driver
  868. * @nonblocking: if true, this call will not sleep waiting for a buffer if no
  869. * buffers ready for dequeuing are present. Normally the driver
  870. * would be passing (file->f_flags & O_NONBLOCK) here
  871. *
  872. * Should be called from vidioc_dqbuf ioctl handler of a driver.
  873. * This function:
  874. * 1) verifies the passed buffer,
  875. * 2) calls buf_finish callback in the driver (if provided), in which
  876. * driver can perform any additional operations that may be required before
  877. * returning the buffer to userspace, such as cache sync,
  878. * 3) the buffer struct members are filled with relevant information for
  879. * the userspace.
  880. *
  881. * The return values from this function are intended to be directly returned
  882. * from vidioc_dqbuf handler in driver.
  883. */
  884. int vb2_dqbuf(struct vb2_queue *q, struct v4l2_buffer *b, bool nonblocking)
  885. {
  886. struct vb2_buffer *vb = NULL;
  887. int ret;
  888. if (b->type != q->type) {
  889. dprintk(1, "dqbuf: invalid buffer type\n");
  890. return -EINVAL;
  891. }
  892. ret = __vb2_get_done_vb(q, &vb, nonblocking);
  893. if (ret < 0) {
  894. dprintk(1, "dqbuf: error getting next done buffer\n");
  895. return ret;
  896. }
  897. ret = call_qop(q, buf_finish, vb);
  898. if (ret) {
  899. dprintk(1, "dqbuf: buffer finish failed\n");
  900. return ret;
  901. }
  902. switch (vb->state) {
  903. case VB2_BUF_STATE_DONE:
  904. dprintk(3, "dqbuf: Returning done buffer\n");
  905. break;
  906. case VB2_BUF_STATE_ERROR:
  907. dprintk(3, "dqbuf: Returning done buffer with errors\n");
  908. break;
  909. default:
  910. dprintk(1, "dqbuf: Invalid buffer state\n");
  911. return -EINVAL;
  912. }
  913. /* Fill buffer information for the userspace */
  914. __fill_v4l2_buffer(vb, b);
  915. /* Remove from videobuf queue */
  916. list_del(&vb->queued_entry);
  917. dprintk(1, "dqbuf of buffer %d, with state %d\n",
  918. vb->v4l2_buf.index, vb->state);
  919. vb->state = VB2_BUF_STATE_DEQUEUED;
  920. return 0;
  921. }
  922. EXPORT_SYMBOL_GPL(vb2_dqbuf);
  923. /**
  924. * vb2_streamon - start streaming
  925. * @q: videobuf2 queue
  926. * @type: type argument passed from userspace to vidioc_streamon handler
  927. *
  928. * Should be called from vidioc_streamon handler of a driver.
  929. * This function:
  930. * 1) verifies current state
  931. * 2) starts streaming and passes any previously queued buffers to the driver
  932. *
  933. * The return values from this function are intended to be directly returned
  934. * from vidioc_streamon handler in the driver.
  935. */
  936. int vb2_streamon(struct vb2_queue *q, enum v4l2_buf_type type)
  937. {
  938. struct vb2_buffer *vb;
  939. if (type != q->type) {
  940. dprintk(1, "streamon: invalid stream type\n");
  941. return -EINVAL;
  942. }
  943. if (q->streaming) {
  944. dprintk(1, "streamon: already streaming\n");
  945. return -EBUSY;
  946. }
  947. /*
  948. * Cannot start streaming on an OUTPUT device if no buffers have
  949. * been queued yet.
  950. */
  951. if (V4L2_TYPE_IS_OUTPUT(q->type)) {
  952. if (list_empty(&q->queued_list)) {
  953. dprintk(1, "streamon: no output buffers queued\n");
  954. return -EINVAL;
  955. }
  956. }
  957. q->streaming = 1;
  958. /*
  959. * Let driver notice that streaming state has been enabled.
  960. */
  961. call_qop(q, start_streaming, q);
  962. /*
  963. * If any buffers were queued before streamon,
  964. * we can now pass them to driver for processing.
  965. */
  966. list_for_each_entry(vb, &q->queued_list, queued_entry)
  967. __enqueue_in_driver(vb);
  968. dprintk(3, "Streamon successful\n");
  969. return 0;
  970. }
  971. EXPORT_SYMBOL_GPL(vb2_streamon);
  972. /**
  973. * __vb2_queue_cancel() - cancel and stop (pause) streaming
  974. *
  975. * Removes all queued buffers from driver's queue and all buffers queued by
  976. * userspace from videobuf's queue. Returns to state after reqbufs.
  977. */
  978. static void __vb2_queue_cancel(struct vb2_queue *q)
  979. {
  980. unsigned int i;
  981. /*
  982. * Tell driver to stop all transactions and release all queued
  983. * buffers.
  984. */
  985. if (q->streaming)
  986. call_qop(q, stop_streaming, q);
  987. q->streaming = 0;
  988. /*
  989. * Remove all buffers from videobuf's list...
  990. */
  991. INIT_LIST_HEAD(&q->queued_list);
  992. /*
  993. * ...and done list; userspace will not receive any buffers it
  994. * has not already dequeued before initiating cancel.
  995. */
  996. INIT_LIST_HEAD(&q->done_list);
  997. wake_up_all(&q->done_wq);
  998. /*
  999. * Reinitialize all buffers for next use.
  1000. */
  1001. for (i = 0; i < q->num_buffers; ++i)
  1002. q->bufs[i]->state = VB2_BUF_STATE_DEQUEUED;
  1003. }
  1004. /**
  1005. * vb2_streamoff - stop streaming
  1006. * @q: videobuf2 queue
  1007. * @type: type argument passed from userspace to vidioc_streamoff handler
  1008. *
  1009. * Should be called from vidioc_streamoff handler of a driver.
  1010. * This function:
  1011. * 1) verifies current state,
  1012. * 2) stop streaming and dequeues any queued buffers, including those previously
  1013. * passed to the driver (after waiting for the driver to finish).
  1014. *
  1015. * This call can be used for pausing playback.
  1016. * The return values from this function are intended to be directly returned
  1017. * from vidioc_streamoff handler in the driver
  1018. */
  1019. int vb2_streamoff(struct vb2_queue *q, enum v4l2_buf_type type)
  1020. {
  1021. if (type != q->type) {
  1022. dprintk(1, "streamoff: invalid stream type\n");
  1023. return -EINVAL;
  1024. }
  1025. if (!q->streaming) {
  1026. dprintk(1, "streamoff: not streaming\n");
  1027. return -EINVAL;
  1028. }
  1029. /*
  1030. * Cancel will pause streaming and remove all buffers from the driver
  1031. * and videobuf, effectively returning control over them to userspace.
  1032. */
  1033. __vb2_queue_cancel(q);
  1034. dprintk(3, "Streamoff successful\n");
  1035. return 0;
  1036. }
  1037. EXPORT_SYMBOL_GPL(vb2_streamoff);
  1038. /**
  1039. * __find_plane_by_offset() - find plane associated with the given offset off
  1040. */
  1041. static int __find_plane_by_offset(struct vb2_queue *q, unsigned long off,
  1042. unsigned int *_buffer, unsigned int *_plane)
  1043. {
  1044. struct vb2_buffer *vb;
  1045. unsigned int buffer, plane;
  1046. /*
  1047. * Go over all buffers and their planes, comparing the given offset
  1048. * with an offset assigned to each plane. If a match is found,
  1049. * return its buffer and plane numbers.
  1050. */
  1051. for (buffer = 0; buffer < q->num_buffers; ++buffer) {
  1052. vb = q->bufs[buffer];
  1053. for (plane = 0; plane < vb->num_planes; ++plane) {
  1054. if (vb->v4l2_planes[plane].m.mem_offset == off) {
  1055. *_buffer = buffer;
  1056. *_plane = plane;
  1057. return 0;
  1058. }
  1059. }
  1060. }
  1061. return -EINVAL;
  1062. }
  1063. /**
  1064. * vb2_mmap() - map video buffers into application address space
  1065. * @q: videobuf2 queue
  1066. * @vma: vma passed to the mmap file operation handler in the driver
  1067. *
  1068. * Should be called from mmap file operation handler of a driver.
  1069. * This function maps one plane of one of the available video buffers to
  1070. * userspace. To map whole video memory allocated on reqbufs, this function
  1071. * has to be called once per each plane per each buffer previously allocated.
  1072. *
  1073. * When the userspace application calls mmap, it passes to it an offset returned
  1074. * to it earlier by the means of vidioc_querybuf handler. That offset acts as
  1075. * a "cookie", which is then used to identify the plane to be mapped.
  1076. * This function finds a plane with a matching offset and a mapping is performed
  1077. * by the means of a provided memory operation.
  1078. *
  1079. * The return values from this function are intended to be directly returned
  1080. * from the mmap handler in driver.
  1081. */
  1082. int vb2_mmap(struct vb2_queue *q, struct vm_area_struct *vma)
  1083. {
  1084. unsigned long off = vma->vm_pgoff << PAGE_SHIFT;
  1085. struct vb2_plane *vb_plane;
  1086. struct vb2_buffer *vb;
  1087. unsigned int buffer, plane;
  1088. int ret;
  1089. if (q->memory != V4L2_MEMORY_MMAP) {
  1090. dprintk(1, "Queue is not currently set up for mmap\n");
  1091. return -EINVAL;
  1092. }
  1093. /*
  1094. * Check memory area access mode.
  1095. */
  1096. if (!(vma->vm_flags & VM_SHARED)) {
  1097. dprintk(1, "Invalid vma flags, VM_SHARED needed\n");
  1098. return -EINVAL;
  1099. }
  1100. if (V4L2_TYPE_IS_OUTPUT(q->type)) {
  1101. if (!(vma->vm_flags & VM_WRITE)) {
  1102. dprintk(1, "Invalid vma flags, VM_WRITE needed\n");
  1103. return -EINVAL;
  1104. }
  1105. } else {
  1106. if (!(vma->vm_flags & VM_READ)) {
  1107. dprintk(1, "Invalid vma flags, VM_READ needed\n");
  1108. return -EINVAL;
  1109. }
  1110. }
  1111. /*
  1112. * Find the plane corresponding to the offset passed by userspace.
  1113. */
  1114. ret = __find_plane_by_offset(q, off, &buffer, &plane);
  1115. if (ret)
  1116. return ret;
  1117. vb = q->bufs[buffer];
  1118. vb_plane = &vb->planes[plane];
  1119. ret = q->mem_ops->mmap(vb_plane->mem_priv, vma);
  1120. if (ret)
  1121. return ret;
  1122. vb_plane->mapped = 1;
  1123. vb->num_planes_mapped++;
  1124. dprintk(3, "Buffer %d, plane %d successfully mapped\n", buffer, plane);
  1125. return 0;
  1126. }
  1127. EXPORT_SYMBOL_GPL(vb2_mmap);
  1128. /**
  1129. * vb2_poll() - implements poll userspace operation
  1130. * @q: videobuf2 queue
  1131. * @file: file argument passed to the poll file operation handler
  1132. * @wait: wait argument passed to the poll file operation handler
  1133. *
  1134. * This function implements poll file operation handler for a driver.
  1135. * For CAPTURE queues, if a buffer is ready to be dequeued, the userspace will
  1136. * be informed that the file descriptor of a video device is available for
  1137. * reading.
  1138. * For OUTPUT queues, if a buffer is ready to be dequeued, the file descriptor
  1139. * will be reported as available for writing.
  1140. *
  1141. * The return values from this function are intended to be directly returned
  1142. * from poll handler in driver.
  1143. */
  1144. unsigned int vb2_poll(struct vb2_queue *q, struct file *file, poll_table *wait)
  1145. {
  1146. unsigned long flags;
  1147. struct vb2_buffer *vb = NULL;
  1148. /*
  1149. * There is nothing to wait for if no buffers have already been queued.
  1150. */
  1151. if (list_empty(&q->queued_list))
  1152. return POLLERR;
  1153. poll_wait(file, &q->done_wq, wait);
  1154. /*
  1155. * Take first buffer available for dequeuing.
  1156. */
  1157. spin_lock_irqsave(&q->done_lock, flags);
  1158. if (!list_empty(&q->done_list))
  1159. vb = list_first_entry(&q->done_list, struct vb2_buffer,
  1160. done_entry);
  1161. spin_unlock_irqrestore(&q->done_lock, flags);
  1162. if (vb && (vb->state == VB2_BUF_STATE_DONE
  1163. || vb->state == VB2_BUF_STATE_ERROR)) {
  1164. return (V4L2_TYPE_IS_OUTPUT(q->type)) ? POLLOUT | POLLWRNORM :
  1165. POLLIN | POLLRDNORM;
  1166. }
  1167. return 0;
  1168. }
  1169. EXPORT_SYMBOL_GPL(vb2_poll);
  1170. /**
  1171. * vb2_queue_init() - initialize a videobuf2 queue
  1172. * @q: videobuf2 queue; this structure should be allocated in driver
  1173. *
  1174. * The vb2_queue structure should be allocated by the driver. The driver is
  1175. * responsible of clearing it's content and setting initial values for some
  1176. * required entries before calling this function.
  1177. * q->ops, q->mem_ops, q->type and q->io_modes are mandatory. Please refer
  1178. * to the struct vb2_queue description in include/media/videobuf2-core.h
  1179. * for more information.
  1180. */
  1181. int vb2_queue_init(struct vb2_queue *q)
  1182. {
  1183. BUG_ON(!q);
  1184. BUG_ON(!q->ops);
  1185. BUG_ON(!q->mem_ops);
  1186. BUG_ON(!q->type);
  1187. BUG_ON(!q->io_modes);
  1188. BUG_ON(!q->ops->queue_setup);
  1189. BUG_ON(!q->ops->buf_queue);
  1190. INIT_LIST_HEAD(&q->queued_list);
  1191. INIT_LIST_HEAD(&q->done_list);
  1192. spin_lock_init(&q->done_lock);
  1193. init_waitqueue_head(&q->done_wq);
  1194. if (q->buf_struct_size == 0)
  1195. q->buf_struct_size = sizeof(struct vb2_buffer);
  1196. return 0;
  1197. }
  1198. EXPORT_SYMBOL_GPL(vb2_queue_init);
  1199. /**
  1200. * vb2_queue_release() - stop streaming, release the queue and free memory
  1201. * @q: videobuf2 queue
  1202. *
  1203. * This function stops streaming and performs necessary clean ups, including
  1204. * freeing video buffer memory. The driver is responsible for freeing
  1205. * the vb2_queue structure itself.
  1206. */
  1207. void vb2_queue_release(struct vb2_queue *q)
  1208. {
  1209. __vb2_queue_cancel(q);
  1210. __vb2_queue_free(q);
  1211. }
  1212. EXPORT_SYMBOL_GPL(vb2_queue_release);
  1213. MODULE_DESCRIPTION("Driver helper framework for Video for Linux 2");
  1214. MODULE_AUTHOR("Pawel Osciak, Marek Szyprowski");
  1215. MODULE_LICENSE("GPL");