videobuf2-core.c 55 KB

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