videobuf2-core.c 53 KB

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