videobuf2-core.c 46 KB

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