videobuf2-core.c 47 KB

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