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