videobuf2-core.c 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804
  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) {
  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. /*
  435. * Make sure the requested values and current defaults are sane.
  436. */
  437. num_buffers = min_t(unsigned int, req->count, VIDEO_MAX_FRAME);
  438. memset(plane_sizes, 0, sizeof(plane_sizes));
  439. memset(q->alloc_ctx, 0, sizeof(q->alloc_ctx));
  440. /*
  441. * Ask the driver how many buffers and planes per buffer it requires.
  442. * Driver also sets the size and allocator context for each plane.
  443. */
  444. ret = call_qop(q, queue_setup, q, &num_buffers, &num_planes,
  445. plane_sizes, q->alloc_ctx);
  446. if (ret)
  447. return ret;
  448. /* Finally, allocate buffers and video memory */
  449. ret = __vb2_queue_alloc(q, req->memory, num_buffers, num_planes,
  450. plane_sizes);
  451. if (ret < 0) {
  452. dprintk(1, "Memory allocation failed with error: %d\n", ret);
  453. return ret;
  454. }
  455. /*
  456. * Check if driver can handle the allocated number of buffers.
  457. */
  458. if (ret < num_buffers) {
  459. unsigned int orig_num_buffers;
  460. orig_num_buffers = num_buffers = ret;
  461. ret = call_qop(q, queue_setup, q, &num_buffers, &num_planes,
  462. plane_sizes, q->alloc_ctx);
  463. if (ret)
  464. goto free_mem;
  465. if (orig_num_buffers < num_buffers) {
  466. ret = -ENOMEM;
  467. goto free_mem;
  468. }
  469. /*
  470. * Ok, driver accepted smaller number of buffers.
  471. */
  472. ret = num_buffers;
  473. }
  474. q->memory = req->memory;
  475. /*
  476. * Return the number of successfully allocated buffers
  477. * to the userspace.
  478. */
  479. req->count = ret;
  480. return 0;
  481. free_mem:
  482. __vb2_queue_free(q);
  483. return ret;
  484. }
  485. EXPORT_SYMBOL_GPL(vb2_reqbufs);
  486. /**
  487. * vb2_plane_vaddr() - Return a kernel virtual address of a given plane
  488. * @vb: vb2_buffer to which the plane in question belongs to
  489. * @plane_no: plane number for which the address is to be returned
  490. *
  491. * This function returns a kernel virtual address of a given plane if
  492. * such a mapping exist, NULL otherwise.
  493. */
  494. void *vb2_plane_vaddr(struct vb2_buffer *vb, unsigned int plane_no)
  495. {
  496. struct vb2_queue *q = vb->vb2_queue;
  497. if (plane_no > vb->num_planes)
  498. return NULL;
  499. return call_memop(q, plane_no, vaddr, vb->planes[plane_no].mem_priv);
  500. }
  501. EXPORT_SYMBOL_GPL(vb2_plane_vaddr);
  502. /**
  503. * vb2_plane_cookie() - Return allocator specific cookie for the given plane
  504. * @vb: vb2_buffer to which the plane in question belongs to
  505. * @plane_no: plane number for which the cookie is to be returned
  506. *
  507. * This function returns an allocator specific cookie for a given plane if
  508. * available, NULL otherwise. The allocator should provide some simple static
  509. * inline function, which would convert this cookie to the allocator specific
  510. * type that can be used directly by the driver to access the buffer. This can
  511. * be for example physical address, pointer to scatter list or IOMMU mapping.
  512. */
  513. void *vb2_plane_cookie(struct vb2_buffer *vb, unsigned int plane_no)
  514. {
  515. struct vb2_queue *q = vb->vb2_queue;
  516. if (plane_no > vb->num_planes)
  517. return NULL;
  518. return call_memop(q, plane_no, cookie, vb->planes[plane_no].mem_priv);
  519. }
  520. EXPORT_SYMBOL_GPL(vb2_plane_cookie);
  521. /**
  522. * vb2_buffer_done() - inform videobuf that an operation on a buffer is finished
  523. * @vb: vb2_buffer returned from the driver
  524. * @state: either VB2_BUF_STATE_DONE if the operation finished successfully
  525. * or VB2_BUF_STATE_ERROR if the operation finished with an error
  526. *
  527. * This function should be called by the driver after a hardware operation on
  528. * a buffer is finished and the buffer may be returned to userspace. The driver
  529. * cannot use this buffer anymore until it is queued back to it by videobuf
  530. * by the means of buf_queue callback. Only buffers previously queued to the
  531. * driver by buf_queue can be passed to this function.
  532. */
  533. void vb2_buffer_done(struct vb2_buffer *vb, enum vb2_buffer_state state)
  534. {
  535. struct vb2_queue *q = vb->vb2_queue;
  536. unsigned long flags;
  537. if (vb->state != VB2_BUF_STATE_ACTIVE)
  538. return;
  539. if (state != VB2_BUF_STATE_DONE && state != VB2_BUF_STATE_ERROR)
  540. return;
  541. dprintk(4, "Done processing on buffer %d, state: %d\n",
  542. vb->v4l2_buf.index, vb->state);
  543. /* Add the buffer to the done buffers list */
  544. spin_lock_irqsave(&q->done_lock, flags);
  545. vb->state = state;
  546. list_add_tail(&vb->done_entry, &q->done_list);
  547. atomic_dec(&q->queued_count);
  548. spin_unlock_irqrestore(&q->done_lock, flags);
  549. /* Inform any processes that may be waiting for buffers */
  550. wake_up(&q->done_wq);
  551. }
  552. EXPORT_SYMBOL_GPL(vb2_buffer_done);
  553. /**
  554. * __fill_vb2_buffer() - fill a vb2_buffer with information provided in
  555. * a v4l2_buffer by the userspace
  556. */
  557. static int __fill_vb2_buffer(struct vb2_buffer *vb, struct v4l2_buffer *b,
  558. struct v4l2_plane *v4l2_planes)
  559. {
  560. unsigned int plane;
  561. int ret;
  562. if (V4L2_TYPE_IS_MULTIPLANAR(b->type)) {
  563. /*
  564. * Verify that the userspace gave us a valid array for
  565. * plane information.
  566. */
  567. ret = __verify_planes_array(vb, b);
  568. if (ret)
  569. return ret;
  570. /* Fill in driver-provided information for OUTPUT types */
  571. if (V4L2_TYPE_IS_OUTPUT(b->type)) {
  572. /*
  573. * Will have to go up to b->length when API starts
  574. * accepting variable number of planes.
  575. */
  576. for (plane = 0; plane < vb->num_planes; ++plane) {
  577. v4l2_planes[plane].bytesused =
  578. b->m.planes[plane].bytesused;
  579. v4l2_planes[plane].data_offset =
  580. b->m.planes[plane].data_offset;
  581. }
  582. }
  583. if (b->memory == V4L2_MEMORY_USERPTR) {
  584. for (plane = 0; plane < vb->num_planes; ++plane) {
  585. v4l2_planes[plane].m.userptr =
  586. b->m.planes[plane].m.userptr;
  587. v4l2_planes[plane].length =
  588. b->m.planes[plane].length;
  589. }
  590. }
  591. } else {
  592. /*
  593. * Single-planar buffers do not use planes array,
  594. * so fill in relevant v4l2_buffer struct fields instead.
  595. * In videobuf we use our internal V4l2_planes struct for
  596. * single-planar buffers as well, for simplicity.
  597. */
  598. if (V4L2_TYPE_IS_OUTPUT(b->type))
  599. v4l2_planes[0].bytesused = b->bytesused;
  600. if (b->memory == V4L2_MEMORY_USERPTR) {
  601. v4l2_planes[0].m.userptr = b->m.userptr;
  602. v4l2_planes[0].length = b->length;
  603. }
  604. }
  605. vb->v4l2_buf.field = b->field;
  606. vb->v4l2_buf.timestamp = b->timestamp;
  607. return 0;
  608. }
  609. /**
  610. * __qbuf_userptr() - handle qbuf of a USERPTR buffer
  611. */
  612. static int __qbuf_userptr(struct vb2_buffer *vb, struct v4l2_buffer *b)
  613. {
  614. struct v4l2_plane planes[VIDEO_MAX_PLANES];
  615. struct vb2_queue *q = vb->vb2_queue;
  616. void *mem_priv;
  617. unsigned int plane;
  618. int ret;
  619. int write = !V4L2_TYPE_IS_OUTPUT(q->type);
  620. /* Verify and copy relevant information provided by the userspace */
  621. ret = __fill_vb2_buffer(vb, b, planes);
  622. if (ret)
  623. return ret;
  624. for (plane = 0; plane < vb->num_planes; ++plane) {
  625. /* Skip the plane if already verified */
  626. if (vb->v4l2_planes[plane].m.userptr == planes[plane].m.userptr
  627. && vb->v4l2_planes[plane].length == planes[plane].length)
  628. continue;
  629. dprintk(3, "qbuf: userspace address for plane %d changed, "
  630. "reacquiring memory\n", plane);
  631. /* Release previously acquired memory if present */
  632. if (vb->planes[plane].mem_priv)
  633. call_memop(q, plane, put_userptr,
  634. vb->planes[plane].mem_priv);
  635. vb->planes[plane].mem_priv = NULL;
  636. /* Acquire each plane's memory */
  637. if (q->mem_ops->get_userptr) {
  638. mem_priv = q->mem_ops->get_userptr(q->alloc_ctx[plane],
  639. planes[plane].m.userptr,
  640. planes[plane].length,
  641. write);
  642. if (IS_ERR(mem_priv)) {
  643. dprintk(1, "qbuf: failed acquiring userspace "
  644. "memory for plane %d\n", plane);
  645. ret = PTR_ERR(mem_priv);
  646. goto err;
  647. }
  648. vb->planes[plane].mem_priv = mem_priv;
  649. }
  650. }
  651. /*
  652. * Call driver-specific initialization on the newly acquired buffer,
  653. * if provided.
  654. */
  655. ret = call_qop(q, buf_init, vb);
  656. if (ret) {
  657. dprintk(1, "qbuf: buffer initialization failed\n");
  658. goto err;
  659. }
  660. /*
  661. * Now that everything is in order, copy relevant information
  662. * provided by userspace.
  663. */
  664. for (plane = 0; plane < vb->num_planes; ++plane)
  665. vb->v4l2_planes[plane] = planes[plane];
  666. return 0;
  667. err:
  668. /* In case of errors, release planes that were already acquired */
  669. for (; plane > 0; --plane) {
  670. call_memop(q, plane, put_userptr,
  671. vb->planes[plane - 1].mem_priv);
  672. vb->planes[plane - 1].mem_priv = NULL;
  673. }
  674. return ret;
  675. }
  676. /**
  677. * __qbuf_mmap() - handle qbuf of an MMAP buffer
  678. */
  679. static int __qbuf_mmap(struct vb2_buffer *vb, struct v4l2_buffer *b)
  680. {
  681. return __fill_vb2_buffer(vb, b, vb->v4l2_planes);
  682. }
  683. /**
  684. * __enqueue_in_driver() - enqueue a vb2_buffer in driver for processing
  685. */
  686. static void __enqueue_in_driver(struct vb2_buffer *vb)
  687. {
  688. struct vb2_queue *q = vb->vb2_queue;
  689. vb->state = VB2_BUF_STATE_ACTIVE;
  690. atomic_inc(&q->queued_count);
  691. q->ops->buf_queue(vb);
  692. }
  693. /**
  694. * vb2_qbuf() - Queue a buffer from userspace
  695. * @q: videobuf2 queue
  696. * @b: buffer structure passed from userspace to vidioc_qbuf handler
  697. * in driver
  698. *
  699. * Should be called from vidioc_qbuf ioctl handler of a driver.
  700. * This function:
  701. * 1) verifies the passed buffer,
  702. * 2) calls buf_prepare callback in the driver (if provided), in which
  703. * driver-specific buffer initialization can be performed,
  704. * 3) if streaming is on, queues the buffer in driver by the means of buf_queue
  705. * callback for processing.
  706. *
  707. * The return values from this function are intended to be directly returned
  708. * from vidioc_qbuf handler in driver.
  709. */
  710. int vb2_qbuf(struct vb2_queue *q, struct v4l2_buffer *b)
  711. {
  712. struct vb2_buffer *vb;
  713. int ret = 0;
  714. if (q->fileio) {
  715. dprintk(1, "qbuf: file io in progress\n");
  716. return -EBUSY;
  717. }
  718. if (b->type != q->type) {
  719. dprintk(1, "qbuf: invalid buffer type\n");
  720. return -EINVAL;
  721. }
  722. if (b->index >= q->num_buffers) {
  723. dprintk(1, "qbuf: buffer index out of range\n");
  724. return -EINVAL;
  725. }
  726. vb = q->bufs[b->index];
  727. if (NULL == vb) {
  728. /* Should never happen */
  729. dprintk(1, "qbuf: buffer is NULL\n");
  730. return -EINVAL;
  731. }
  732. if (b->memory != q->memory) {
  733. dprintk(1, "qbuf: invalid memory type\n");
  734. return -EINVAL;
  735. }
  736. if (vb->state != VB2_BUF_STATE_DEQUEUED) {
  737. dprintk(1, "qbuf: buffer already in use\n");
  738. return -EINVAL;
  739. }
  740. if (q->memory == V4L2_MEMORY_MMAP)
  741. ret = __qbuf_mmap(vb, b);
  742. else if (q->memory == V4L2_MEMORY_USERPTR)
  743. ret = __qbuf_userptr(vb, b);
  744. else {
  745. WARN(1, "Invalid queue type\n");
  746. return -EINVAL;
  747. }
  748. if (ret)
  749. return ret;
  750. ret = call_qop(q, buf_prepare, vb);
  751. if (ret) {
  752. dprintk(1, "qbuf: buffer preparation failed\n");
  753. return ret;
  754. }
  755. /*
  756. * Add to the queued buffers list, a buffer will stay on it until
  757. * dequeued in dqbuf.
  758. */
  759. list_add_tail(&vb->queued_entry, &q->queued_list);
  760. vb->state = VB2_BUF_STATE_QUEUED;
  761. /*
  762. * If already streaming, give the buffer to driver for processing.
  763. * If not, the buffer will be given to driver on next streamon.
  764. */
  765. if (q->streaming)
  766. __enqueue_in_driver(vb);
  767. dprintk(1, "qbuf of buffer %d succeeded\n", vb->v4l2_buf.index);
  768. return 0;
  769. }
  770. EXPORT_SYMBOL_GPL(vb2_qbuf);
  771. /**
  772. * __vb2_wait_for_done_vb() - wait for a buffer to become available
  773. * for dequeuing
  774. *
  775. * Will sleep if required for nonblocking == false.
  776. */
  777. static int __vb2_wait_for_done_vb(struct vb2_queue *q, int nonblocking)
  778. {
  779. /*
  780. * All operations on vb_done_list are performed under done_lock
  781. * spinlock protection. However, buffers may be removed from
  782. * it and returned to userspace only while holding both driver's
  783. * lock and the done_lock spinlock. Thus we can be sure that as
  784. * long as we hold the driver's lock, the list will remain not
  785. * empty if list_empty() check succeeds.
  786. */
  787. for (;;) {
  788. int ret;
  789. if (!q->streaming) {
  790. dprintk(1, "Streaming off, will not wait for buffers\n");
  791. return -EINVAL;
  792. }
  793. if (!list_empty(&q->done_list)) {
  794. /*
  795. * Found a buffer that we were waiting for.
  796. */
  797. break;
  798. }
  799. if (nonblocking) {
  800. dprintk(1, "Nonblocking and no buffers to dequeue, "
  801. "will not wait\n");
  802. return -EAGAIN;
  803. }
  804. /*
  805. * We are streaming and blocking, wait for another buffer to
  806. * become ready or for streamoff. Driver's lock is released to
  807. * allow streamoff or qbuf to be called while waiting.
  808. */
  809. call_qop(q, wait_prepare, q);
  810. /*
  811. * All locks have been released, it is safe to sleep now.
  812. */
  813. dprintk(3, "Will sleep waiting for buffers\n");
  814. ret = wait_event_interruptible(q->done_wq,
  815. !list_empty(&q->done_list) || !q->streaming);
  816. /*
  817. * We need to reevaluate both conditions again after reacquiring
  818. * the locks or return an error if one occurred.
  819. */
  820. call_qop(q, wait_finish, q);
  821. if (ret)
  822. return ret;
  823. }
  824. return 0;
  825. }
  826. /**
  827. * __vb2_get_done_vb() - get a buffer ready for dequeuing
  828. *
  829. * Will sleep if required for nonblocking == false.
  830. */
  831. static int __vb2_get_done_vb(struct vb2_queue *q, struct vb2_buffer **vb,
  832. int nonblocking)
  833. {
  834. unsigned long flags;
  835. int ret;
  836. /*
  837. * Wait for at least one buffer to become available on the done_list.
  838. */
  839. ret = __vb2_wait_for_done_vb(q, nonblocking);
  840. if (ret)
  841. return ret;
  842. /*
  843. * Driver's lock has been held since we last verified that done_list
  844. * is not empty, so no need for another list_empty(done_list) check.
  845. */
  846. spin_lock_irqsave(&q->done_lock, flags);
  847. *vb = list_first_entry(&q->done_list, struct vb2_buffer, done_entry);
  848. list_del(&(*vb)->done_entry);
  849. spin_unlock_irqrestore(&q->done_lock, flags);
  850. return 0;
  851. }
  852. /**
  853. * vb2_wait_for_all_buffers() - wait until all buffers are given back to vb2
  854. * @q: videobuf2 queue
  855. *
  856. * This function will wait until all buffers that have been given to the driver
  857. * by buf_queue() are given back to vb2 with vb2_buffer_done(). It doesn't call
  858. * wait_prepare, wait_finish pair. It is intended to be called with all locks
  859. * taken, for example from stop_streaming() callback.
  860. */
  861. int vb2_wait_for_all_buffers(struct vb2_queue *q)
  862. {
  863. if (!q->streaming) {
  864. dprintk(1, "Streaming off, will not wait for buffers\n");
  865. return -EINVAL;
  866. }
  867. wait_event(q->done_wq, !atomic_read(&q->queued_count));
  868. return 0;
  869. }
  870. EXPORT_SYMBOL_GPL(vb2_wait_for_all_buffers);
  871. /**
  872. * vb2_dqbuf() - Dequeue a buffer to the userspace
  873. * @q: videobuf2 queue
  874. * @b: buffer structure passed from userspace to vidioc_dqbuf handler
  875. * in driver
  876. * @nonblocking: if true, this call will not sleep waiting for a buffer if no
  877. * buffers ready for dequeuing are present. Normally the driver
  878. * would be passing (file->f_flags & O_NONBLOCK) here
  879. *
  880. * Should be called from vidioc_dqbuf ioctl handler of a driver.
  881. * This function:
  882. * 1) verifies the passed buffer,
  883. * 2) calls buf_finish callback in the driver (if provided), in which
  884. * driver can perform any additional operations that may be required before
  885. * returning the buffer to userspace, such as cache sync,
  886. * 3) the buffer struct members are filled with relevant information for
  887. * the userspace.
  888. *
  889. * The return values from this function are intended to be directly returned
  890. * from vidioc_dqbuf handler in driver.
  891. */
  892. int vb2_dqbuf(struct vb2_queue *q, struct v4l2_buffer *b, bool nonblocking)
  893. {
  894. struct vb2_buffer *vb = NULL;
  895. int ret;
  896. if (q->fileio) {
  897. dprintk(1, "dqbuf: file io in progress\n");
  898. return -EBUSY;
  899. }
  900. if (b->type != q->type) {
  901. dprintk(1, "dqbuf: invalid buffer type\n");
  902. return -EINVAL;
  903. }
  904. ret = __vb2_get_done_vb(q, &vb, nonblocking);
  905. if (ret < 0) {
  906. dprintk(1, "dqbuf: error getting next done buffer\n");
  907. return ret;
  908. }
  909. ret = call_qop(q, buf_finish, vb);
  910. if (ret) {
  911. dprintk(1, "dqbuf: buffer finish failed\n");
  912. return ret;
  913. }
  914. switch (vb->state) {
  915. case VB2_BUF_STATE_DONE:
  916. dprintk(3, "dqbuf: Returning done buffer\n");
  917. break;
  918. case VB2_BUF_STATE_ERROR:
  919. dprintk(3, "dqbuf: Returning done buffer with errors\n");
  920. break;
  921. default:
  922. dprintk(1, "dqbuf: Invalid buffer state\n");
  923. return -EINVAL;
  924. }
  925. /* Fill buffer information for the userspace */
  926. __fill_v4l2_buffer(vb, b);
  927. /* Remove from videobuf queue */
  928. list_del(&vb->queued_entry);
  929. dprintk(1, "dqbuf of buffer %d, with state %d\n",
  930. vb->v4l2_buf.index, vb->state);
  931. vb->state = VB2_BUF_STATE_DEQUEUED;
  932. return 0;
  933. }
  934. EXPORT_SYMBOL_GPL(vb2_dqbuf);
  935. /**
  936. * vb2_streamon - start streaming
  937. * @q: videobuf2 queue
  938. * @type: type argument passed from userspace to vidioc_streamon handler
  939. *
  940. * Should be called from vidioc_streamon handler of a driver.
  941. * This function:
  942. * 1) verifies current state
  943. * 2) starts streaming and passes any previously queued buffers to the driver
  944. *
  945. * The return values from this function are intended to be directly returned
  946. * from vidioc_streamon handler in the driver.
  947. */
  948. int vb2_streamon(struct vb2_queue *q, enum v4l2_buf_type type)
  949. {
  950. struct vb2_buffer *vb;
  951. if (q->fileio) {
  952. dprintk(1, "streamon: file io in progress\n");
  953. return -EBUSY;
  954. }
  955. if (type != q->type) {
  956. dprintk(1, "streamon: invalid stream type\n");
  957. return -EINVAL;
  958. }
  959. if (q->streaming) {
  960. dprintk(1, "streamon: already streaming\n");
  961. return -EBUSY;
  962. }
  963. /*
  964. * Cannot start streaming on an OUTPUT device if no buffers have
  965. * been queued yet.
  966. */
  967. if (V4L2_TYPE_IS_OUTPUT(q->type)) {
  968. if (list_empty(&q->queued_list)) {
  969. dprintk(1, "streamon: no output buffers queued\n");
  970. return -EINVAL;
  971. }
  972. }
  973. q->streaming = 1;
  974. /*
  975. * Let driver notice that streaming state has been enabled.
  976. */
  977. call_qop(q, start_streaming, q);
  978. /*
  979. * If any buffers were queued before streamon,
  980. * we can now pass them to driver for processing.
  981. */
  982. list_for_each_entry(vb, &q->queued_list, queued_entry)
  983. __enqueue_in_driver(vb);
  984. dprintk(3, "Streamon successful\n");
  985. return 0;
  986. }
  987. EXPORT_SYMBOL_GPL(vb2_streamon);
  988. /**
  989. * __vb2_queue_cancel() - cancel and stop (pause) streaming
  990. *
  991. * Removes all queued buffers from driver's queue and all buffers queued by
  992. * userspace from videobuf's queue. Returns to state after reqbufs.
  993. */
  994. static void __vb2_queue_cancel(struct vb2_queue *q)
  995. {
  996. unsigned int i;
  997. /*
  998. * Tell driver to stop all transactions and release all queued
  999. * buffers.
  1000. */
  1001. if (q->streaming)
  1002. call_qop(q, stop_streaming, q);
  1003. q->streaming = 0;
  1004. /*
  1005. * Remove all buffers from videobuf's list...
  1006. */
  1007. INIT_LIST_HEAD(&q->queued_list);
  1008. /*
  1009. * ...and done list; userspace will not receive any buffers it
  1010. * has not already dequeued before initiating cancel.
  1011. */
  1012. INIT_LIST_HEAD(&q->done_list);
  1013. wake_up_all(&q->done_wq);
  1014. /*
  1015. * Reinitialize all buffers for next use.
  1016. */
  1017. for (i = 0; i < q->num_buffers; ++i)
  1018. q->bufs[i]->state = VB2_BUF_STATE_DEQUEUED;
  1019. }
  1020. /**
  1021. * vb2_streamoff - stop streaming
  1022. * @q: videobuf2 queue
  1023. * @type: type argument passed from userspace to vidioc_streamoff handler
  1024. *
  1025. * Should be called from vidioc_streamoff handler of a driver.
  1026. * This function:
  1027. * 1) verifies current state,
  1028. * 2) stop streaming and dequeues any queued buffers, including those previously
  1029. * passed to the driver (after waiting for the driver to finish).
  1030. *
  1031. * This call can be used for pausing playback.
  1032. * The return values from this function are intended to be directly returned
  1033. * from vidioc_streamoff handler in the driver
  1034. */
  1035. int vb2_streamoff(struct vb2_queue *q, enum v4l2_buf_type type)
  1036. {
  1037. if (q->fileio) {
  1038. dprintk(1, "streamoff: file io in progress\n");
  1039. return -EBUSY;
  1040. }
  1041. if (type != q->type) {
  1042. dprintk(1, "streamoff: invalid stream type\n");
  1043. return -EINVAL;
  1044. }
  1045. if (!q->streaming) {
  1046. dprintk(1, "streamoff: not streaming\n");
  1047. return -EINVAL;
  1048. }
  1049. /*
  1050. * Cancel will pause streaming and remove all buffers from the driver
  1051. * and videobuf, effectively returning control over them to userspace.
  1052. */
  1053. __vb2_queue_cancel(q);
  1054. dprintk(3, "Streamoff successful\n");
  1055. return 0;
  1056. }
  1057. EXPORT_SYMBOL_GPL(vb2_streamoff);
  1058. /**
  1059. * __find_plane_by_offset() - find plane associated with the given offset off
  1060. */
  1061. static int __find_plane_by_offset(struct vb2_queue *q, unsigned long off,
  1062. unsigned int *_buffer, unsigned int *_plane)
  1063. {
  1064. struct vb2_buffer *vb;
  1065. unsigned int buffer, plane;
  1066. /*
  1067. * Go over all buffers and their planes, comparing the given offset
  1068. * with an offset assigned to each plane. If a match is found,
  1069. * return its buffer and plane numbers.
  1070. */
  1071. for (buffer = 0; buffer < q->num_buffers; ++buffer) {
  1072. vb = q->bufs[buffer];
  1073. for (plane = 0; plane < vb->num_planes; ++plane) {
  1074. if (vb->v4l2_planes[plane].m.mem_offset == off) {
  1075. *_buffer = buffer;
  1076. *_plane = plane;
  1077. return 0;
  1078. }
  1079. }
  1080. }
  1081. return -EINVAL;
  1082. }
  1083. /**
  1084. * vb2_mmap() - map video buffers into application address space
  1085. * @q: videobuf2 queue
  1086. * @vma: vma passed to the mmap file operation handler in the driver
  1087. *
  1088. * Should be called from mmap file operation handler of a driver.
  1089. * This function maps one plane of one of the available video buffers to
  1090. * userspace. To map whole video memory allocated on reqbufs, this function
  1091. * has to be called once per each plane per each buffer previously allocated.
  1092. *
  1093. * When the userspace application calls mmap, it passes to it an offset returned
  1094. * to it earlier by the means of vidioc_querybuf handler. That offset acts as
  1095. * a "cookie", which is then used to identify the plane to be mapped.
  1096. * This function finds a plane with a matching offset and a mapping is performed
  1097. * by the means of a provided memory operation.
  1098. *
  1099. * The return values from this function are intended to be directly returned
  1100. * from the mmap handler in driver.
  1101. */
  1102. int vb2_mmap(struct vb2_queue *q, struct vm_area_struct *vma)
  1103. {
  1104. unsigned long off = vma->vm_pgoff << PAGE_SHIFT;
  1105. struct vb2_plane *vb_plane;
  1106. struct vb2_buffer *vb;
  1107. unsigned int buffer, plane;
  1108. int ret;
  1109. if (q->memory != V4L2_MEMORY_MMAP) {
  1110. dprintk(1, "Queue is not currently set up for mmap\n");
  1111. return -EINVAL;
  1112. }
  1113. /*
  1114. * Check memory area access mode.
  1115. */
  1116. if (!(vma->vm_flags & VM_SHARED)) {
  1117. dprintk(1, "Invalid vma flags, VM_SHARED needed\n");
  1118. return -EINVAL;
  1119. }
  1120. if (V4L2_TYPE_IS_OUTPUT(q->type)) {
  1121. if (!(vma->vm_flags & VM_WRITE)) {
  1122. dprintk(1, "Invalid vma flags, VM_WRITE needed\n");
  1123. return -EINVAL;
  1124. }
  1125. } else {
  1126. if (!(vma->vm_flags & VM_READ)) {
  1127. dprintk(1, "Invalid vma flags, VM_READ needed\n");
  1128. return -EINVAL;
  1129. }
  1130. }
  1131. /*
  1132. * Find the plane corresponding to the offset passed by userspace.
  1133. */
  1134. ret = __find_plane_by_offset(q, off, &buffer, &plane);
  1135. if (ret)
  1136. return ret;
  1137. vb = q->bufs[buffer];
  1138. vb_plane = &vb->planes[plane];
  1139. ret = q->mem_ops->mmap(vb_plane->mem_priv, vma);
  1140. if (ret)
  1141. return ret;
  1142. vb_plane->mapped = 1;
  1143. vb->num_planes_mapped++;
  1144. dprintk(3, "Buffer %d, plane %d successfully mapped\n", buffer, plane);
  1145. return 0;
  1146. }
  1147. EXPORT_SYMBOL_GPL(vb2_mmap);
  1148. static int __vb2_init_fileio(struct vb2_queue *q, int read);
  1149. static int __vb2_cleanup_fileio(struct vb2_queue *q);
  1150. /**
  1151. * vb2_poll() - implements poll userspace operation
  1152. * @q: videobuf2 queue
  1153. * @file: file argument passed to the poll file operation handler
  1154. * @wait: wait argument passed to the poll file operation handler
  1155. *
  1156. * This function implements poll file operation handler for a driver.
  1157. * For CAPTURE queues, if a buffer is ready to be dequeued, the userspace will
  1158. * be informed that the file descriptor of a video device is available for
  1159. * reading.
  1160. * For OUTPUT queues, if a buffer is ready to be dequeued, the file descriptor
  1161. * will be reported as available for writing.
  1162. *
  1163. * The return values from this function are intended to be directly returned
  1164. * from poll handler in driver.
  1165. */
  1166. unsigned int vb2_poll(struct vb2_queue *q, struct file *file, poll_table *wait)
  1167. {
  1168. unsigned long flags;
  1169. unsigned int ret;
  1170. struct vb2_buffer *vb = NULL;
  1171. /*
  1172. * Start file io emulator if streaming api has not been used yet.
  1173. */
  1174. if (q->num_buffers == 0 && q->fileio == NULL) {
  1175. if (!V4L2_TYPE_IS_OUTPUT(q->type) && (q->io_modes & VB2_READ)) {
  1176. ret = __vb2_init_fileio(q, 1);
  1177. if (ret)
  1178. return ret;
  1179. }
  1180. if (V4L2_TYPE_IS_OUTPUT(q->type) && (q->io_modes & VB2_WRITE)) {
  1181. ret = __vb2_init_fileio(q, 0);
  1182. if (ret)
  1183. return ret;
  1184. /*
  1185. * Write to OUTPUT queue can be done immediately.
  1186. */
  1187. return POLLOUT | POLLWRNORM;
  1188. }
  1189. }
  1190. /*
  1191. * There is nothing to wait for if no buffers have already been queued.
  1192. */
  1193. if (list_empty(&q->queued_list))
  1194. return POLLERR;
  1195. poll_wait(file, &q->done_wq, wait);
  1196. /*
  1197. * Take first buffer available for dequeuing.
  1198. */
  1199. spin_lock_irqsave(&q->done_lock, flags);
  1200. if (!list_empty(&q->done_list))
  1201. vb = list_first_entry(&q->done_list, struct vb2_buffer,
  1202. done_entry);
  1203. spin_unlock_irqrestore(&q->done_lock, flags);
  1204. if (vb && (vb->state == VB2_BUF_STATE_DONE
  1205. || vb->state == VB2_BUF_STATE_ERROR)) {
  1206. return (V4L2_TYPE_IS_OUTPUT(q->type)) ? POLLOUT | POLLWRNORM :
  1207. POLLIN | POLLRDNORM;
  1208. }
  1209. return 0;
  1210. }
  1211. EXPORT_SYMBOL_GPL(vb2_poll);
  1212. /**
  1213. * vb2_queue_init() - initialize a videobuf2 queue
  1214. * @q: videobuf2 queue; this structure should be allocated in driver
  1215. *
  1216. * The vb2_queue structure should be allocated by the driver. The driver is
  1217. * responsible of clearing it's content and setting initial values for some
  1218. * required entries before calling this function.
  1219. * q->ops, q->mem_ops, q->type and q->io_modes are mandatory. Please refer
  1220. * to the struct vb2_queue description in include/media/videobuf2-core.h
  1221. * for more information.
  1222. */
  1223. int vb2_queue_init(struct vb2_queue *q)
  1224. {
  1225. BUG_ON(!q);
  1226. BUG_ON(!q->ops);
  1227. BUG_ON(!q->mem_ops);
  1228. BUG_ON(!q->type);
  1229. BUG_ON(!q->io_modes);
  1230. BUG_ON(!q->ops->queue_setup);
  1231. BUG_ON(!q->ops->buf_queue);
  1232. INIT_LIST_HEAD(&q->queued_list);
  1233. INIT_LIST_HEAD(&q->done_list);
  1234. spin_lock_init(&q->done_lock);
  1235. init_waitqueue_head(&q->done_wq);
  1236. if (q->buf_struct_size == 0)
  1237. q->buf_struct_size = sizeof(struct vb2_buffer);
  1238. return 0;
  1239. }
  1240. EXPORT_SYMBOL_GPL(vb2_queue_init);
  1241. /**
  1242. * vb2_queue_release() - stop streaming, release the queue and free memory
  1243. * @q: videobuf2 queue
  1244. *
  1245. * This function stops streaming and performs necessary clean ups, including
  1246. * freeing video buffer memory. The driver is responsible for freeing
  1247. * the vb2_queue structure itself.
  1248. */
  1249. void vb2_queue_release(struct vb2_queue *q)
  1250. {
  1251. __vb2_cleanup_fileio(q);
  1252. __vb2_queue_cancel(q);
  1253. __vb2_queue_free(q);
  1254. }
  1255. EXPORT_SYMBOL_GPL(vb2_queue_release);
  1256. /**
  1257. * struct vb2_fileio_buf - buffer context used by file io emulator
  1258. *
  1259. * vb2 provides a compatibility layer and emulator of file io (read and
  1260. * write) calls on top of streaming API. This structure is used for
  1261. * tracking context related to the buffers.
  1262. */
  1263. struct vb2_fileio_buf {
  1264. void *vaddr;
  1265. unsigned int size;
  1266. unsigned int pos;
  1267. unsigned int queued:1;
  1268. };
  1269. /**
  1270. * struct vb2_fileio_data - queue context used by file io emulator
  1271. *
  1272. * vb2 provides a compatibility layer and emulator of file io (read and
  1273. * write) calls on top of streaming API. For proper operation it required
  1274. * this structure to save the driver state between each call of the read
  1275. * or write function.
  1276. */
  1277. struct vb2_fileio_data {
  1278. struct v4l2_requestbuffers req;
  1279. struct v4l2_buffer b;
  1280. struct vb2_fileio_buf bufs[VIDEO_MAX_FRAME];
  1281. unsigned int index;
  1282. unsigned int q_count;
  1283. unsigned int dq_count;
  1284. unsigned int flags;
  1285. };
  1286. /**
  1287. * __vb2_init_fileio() - initialize file io emulator
  1288. * @q: videobuf2 queue
  1289. * @read: mode selector (1 means read, 0 means write)
  1290. */
  1291. static int __vb2_init_fileio(struct vb2_queue *q, int read)
  1292. {
  1293. struct vb2_fileio_data *fileio;
  1294. int i, ret;
  1295. unsigned int count = 0;
  1296. /*
  1297. * Sanity check
  1298. */
  1299. if ((read && !(q->io_modes & VB2_READ)) ||
  1300. (!read && !(q->io_modes & VB2_WRITE)))
  1301. BUG();
  1302. /*
  1303. * Check if device supports mapping buffers to kernel virtual space.
  1304. */
  1305. if (!q->mem_ops->vaddr)
  1306. return -EBUSY;
  1307. /*
  1308. * Check if streaming api has not been already activated.
  1309. */
  1310. if (q->streaming || q->num_buffers > 0)
  1311. return -EBUSY;
  1312. /*
  1313. * Start with count 1, driver can increase it in queue_setup()
  1314. */
  1315. count = 1;
  1316. dprintk(3, "setting up file io: mode %s, count %d, flags %08x\n",
  1317. (read) ? "read" : "write", count, q->io_flags);
  1318. fileio = kzalloc(sizeof(struct vb2_fileio_data), GFP_KERNEL);
  1319. if (fileio == NULL)
  1320. return -ENOMEM;
  1321. fileio->flags = q->io_flags;
  1322. /*
  1323. * Request buffers and use MMAP type to force driver
  1324. * to allocate buffers by itself.
  1325. */
  1326. fileio->req.count = count;
  1327. fileio->req.memory = V4L2_MEMORY_MMAP;
  1328. fileio->req.type = q->type;
  1329. ret = vb2_reqbufs(q, &fileio->req);
  1330. if (ret)
  1331. goto err_kfree;
  1332. /*
  1333. * Check if plane_count is correct
  1334. * (multiplane buffers are not supported).
  1335. */
  1336. if (q->bufs[0]->num_planes != 1) {
  1337. fileio->req.count = 0;
  1338. ret = -EBUSY;
  1339. goto err_reqbufs;
  1340. }
  1341. /*
  1342. * Get kernel address of each buffer.
  1343. */
  1344. for (i = 0; i < q->num_buffers; i++) {
  1345. fileio->bufs[i].vaddr = vb2_plane_vaddr(q->bufs[i], 0);
  1346. if (fileio->bufs[i].vaddr == NULL)
  1347. goto err_reqbufs;
  1348. fileio->bufs[i].size = vb2_plane_size(q->bufs[i], 0);
  1349. }
  1350. /*
  1351. * Read mode requires pre queuing of all buffers.
  1352. */
  1353. if (read) {
  1354. /*
  1355. * Queue all buffers.
  1356. */
  1357. for (i = 0; i < q->num_buffers; i++) {
  1358. struct v4l2_buffer *b = &fileio->b;
  1359. memset(b, 0, sizeof(*b));
  1360. b->type = q->type;
  1361. b->memory = q->memory;
  1362. b->index = i;
  1363. ret = vb2_qbuf(q, b);
  1364. if (ret)
  1365. goto err_reqbufs;
  1366. fileio->bufs[i].queued = 1;
  1367. }
  1368. /*
  1369. * Start streaming.
  1370. */
  1371. ret = vb2_streamon(q, q->type);
  1372. if (ret)
  1373. goto err_reqbufs;
  1374. }
  1375. q->fileio = fileio;
  1376. return ret;
  1377. err_reqbufs:
  1378. vb2_reqbufs(q, &fileio->req);
  1379. err_kfree:
  1380. kfree(fileio);
  1381. return ret;
  1382. }
  1383. /**
  1384. * __vb2_cleanup_fileio() - free resourced used by file io emulator
  1385. * @q: videobuf2 queue
  1386. */
  1387. static int __vb2_cleanup_fileio(struct vb2_queue *q)
  1388. {
  1389. struct vb2_fileio_data *fileio = q->fileio;
  1390. if (fileio) {
  1391. /*
  1392. * Hack fileio context to enable direct calls to vb2 ioctl
  1393. * interface.
  1394. */
  1395. q->fileio = NULL;
  1396. vb2_streamoff(q, q->type);
  1397. fileio->req.count = 0;
  1398. vb2_reqbufs(q, &fileio->req);
  1399. kfree(fileio);
  1400. dprintk(3, "file io emulator closed\n");
  1401. }
  1402. return 0;
  1403. }
  1404. /**
  1405. * __vb2_perform_fileio() - perform a single file io (read or write) operation
  1406. * @q: videobuf2 queue
  1407. * @data: pointed to target userspace buffer
  1408. * @count: number of bytes to read or write
  1409. * @ppos: file handle position tracking pointer
  1410. * @nonblock: mode selector (1 means blocking calls, 0 means nonblocking)
  1411. * @read: access mode selector (1 means read, 0 means write)
  1412. */
  1413. static size_t __vb2_perform_fileio(struct vb2_queue *q, char __user *data, size_t count,
  1414. loff_t *ppos, int nonblock, int read)
  1415. {
  1416. struct vb2_fileio_data *fileio;
  1417. struct vb2_fileio_buf *buf;
  1418. int ret, index;
  1419. dprintk(3, "file io: mode %s, offset %ld, count %zd, %sblocking\n",
  1420. read ? "read" : "write", (long)*ppos, count,
  1421. nonblock ? "non" : "");
  1422. if (!data)
  1423. return -EINVAL;
  1424. /*
  1425. * Initialize emulator on first call.
  1426. */
  1427. if (!q->fileio) {
  1428. ret = __vb2_init_fileio(q, read);
  1429. dprintk(3, "file io: vb2_init_fileio result: %d\n", ret);
  1430. if (ret)
  1431. return ret;
  1432. }
  1433. fileio = q->fileio;
  1434. /*
  1435. * Hack fileio context to enable direct calls to vb2 ioctl interface.
  1436. * The pointer will be restored before returning from this function.
  1437. */
  1438. q->fileio = NULL;
  1439. index = fileio->index;
  1440. buf = &fileio->bufs[index];
  1441. /*
  1442. * Check if we need to dequeue the buffer.
  1443. */
  1444. if (buf->queued) {
  1445. struct vb2_buffer *vb;
  1446. /*
  1447. * Call vb2_dqbuf to get buffer back.
  1448. */
  1449. memset(&fileio->b, 0, sizeof(fileio->b));
  1450. fileio->b.type = q->type;
  1451. fileio->b.memory = q->memory;
  1452. fileio->b.index = index;
  1453. ret = vb2_dqbuf(q, &fileio->b, nonblock);
  1454. dprintk(5, "file io: vb2_dqbuf result: %d\n", ret);
  1455. if (ret)
  1456. goto end;
  1457. fileio->dq_count += 1;
  1458. /*
  1459. * Get number of bytes filled by the driver
  1460. */
  1461. vb = q->bufs[index];
  1462. buf->size = vb2_get_plane_payload(vb, 0);
  1463. buf->queued = 0;
  1464. }
  1465. /*
  1466. * Limit count on last few bytes of the buffer.
  1467. */
  1468. if (buf->pos + count > buf->size) {
  1469. count = buf->size - buf->pos;
  1470. dprintk(5, "reducing read count: %zd\n", count);
  1471. }
  1472. /*
  1473. * Transfer data to userspace.
  1474. */
  1475. dprintk(3, "file io: copying %zd bytes - buffer %d, offset %u\n",
  1476. count, index, buf->pos);
  1477. if (read)
  1478. ret = copy_to_user(data, buf->vaddr + buf->pos, count);
  1479. else
  1480. ret = copy_from_user(buf->vaddr + buf->pos, data, count);
  1481. if (ret) {
  1482. dprintk(3, "file io: error copying data\n");
  1483. ret = -EFAULT;
  1484. goto end;
  1485. }
  1486. /*
  1487. * Update counters.
  1488. */
  1489. buf->pos += count;
  1490. *ppos += count;
  1491. /*
  1492. * Queue next buffer if required.
  1493. */
  1494. if (buf->pos == buf->size ||
  1495. (!read && (fileio->flags & VB2_FILEIO_WRITE_IMMEDIATELY))) {
  1496. /*
  1497. * Check if this is the last buffer to read.
  1498. */
  1499. if (read && (fileio->flags & VB2_FILEIO_READ_ONCE) &&
  1500. fileio->dq_count == 1) {
  1501. dprintk(3, "file io: read limit reached\n");
  1502. /*
  1503. * Restore fileio pointer and release the context.
  1504. */
  1505. q->fileio = fileio;
  1506. return __vb2_cleanup_fileio(q);
  1507. }
  1508. /*
  1509. * Call vb2_qbuf and give buffer to the driver.
  1510. */
  1511. memset(&fileio->b, 0, sizeof(fileio->b));
  1512. fileio->b.type = q->type;
  1513. fileio->b.memory = q->memory;
  1514. fileio->b.index = index;
  1515. fileio->b.bytesused = buf->pos;
  1516. ret = vb2_qbuf(q, &fileio->b);
  1517. dprintk(5, "file io: vb2_dbuf result: %d\n", ret);
  1518. if (ret)
  1519. goto end;
  1520. /*
  1521. * Buffer has been queued, update the status
  1522. */
  1523. buf->pos = 0;
  1524. buf->queued = 1;
  1525. buf->size = q->bufs[0]->v4l2_planes[0].length;
  1526. fileio->q_count += 1;
  1527. /*
  1528. * Switch to the next buffer
  1529. */
  1530. fileio->index = (index + 1) % q->num_buffers;
  1531. /*
  1532. * Start streaming if required.
  1533. */
  1534. if (!read && !q->streaming) {
  1535. ret = vb2_streamon(q, q->type);
  1536. if (ret)
  1537. goto end;
  1538. }
  1539. }
  1540. /*
  1541. * Return proper number of bytes processed.
  1542. */
  1543. if (ret == 0)
  1544. ret = count;
  1545. end:
  1546. /*
  1547. * Restore the fileio context and block vb2 ioctl interface.
  1548. */
  1549. q->fileio = fileio;
  1550. return ret;
  1551. }
  1552. size_t vb2_read(struct vb2_queue *q, char __user *data, size_t count,
  1553. loff_t *ppos, int nonblocking)
  1554. {
  1555. return __vb2_perform_fileio(q, data, count, ppos, nonblocking, 1);
  1556. }
  1557. EXPORT_SYMBOL_GPL(vb2_read);
  1558. size_t vb2_write(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, 0);
  1562. }
  1563. EXPORT_SYMBOL_GPL(vb2_write);
  1564. MODULE_DESCRIPTION("Driver helper framework for Video for Linux 2");
  1565. MODULE_AUTHOR("Pawel Osciak, Marek Szyprowski");
  1566. MODULE_LICENSE("GPL");