videobuf2-core.c 62 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383
  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. * __verify_memory_type() - Check whether the memory type and buffer type
  397. * passed to a buffer operation are compatible with the queue.
  398. */
  399. static int __verify_memory_type(struct vb2_queue *q,
  400. enum v4l2_memory memory, enum v4l2_buf_type type)
  401. {
  402. if (memory != V4L2_MEMORY_MMAP && memory != V4L2_MEMORY_USERPTR) {
  403. dprintk(1, "reqbufs: unsupported memory type\n");
  404. return -EINVAL;
  405. }
  406. if (type != q->type) {
  407. dprintk(1, "reqbufs: requested type is incorrect\n");
  408. return -EINVAL;
  409. }
  410. /*
  411. * Make sure all the required memory ops for given memory type
  412. * are available.
  413. */
  414. if (memory == V4L2_MEMORY_MMAP && __verify_mmap_ops(q)) {
  415. dprintk(1, "reqbufs: MMAP for current setup unsupported\n");
  416. return -EINVAL;
  417. }
  418. if (memory == V4L2_MEMORY_USERPTR && __verify_userptr_ops(q)) {
  419. dprintk(1, "reqbufs: USERPTR for current setup unsupported\n");
  420. return -EINVAL;
  421. }
  422. /*
  423. * Place the busy tests at the end: -EBUSY can be ignored when
  424. * create_bufs is called with count == 0, but count == 0 should still
  425. * do the memory and type validation.
  426. */
  427. if (q->fileio) {
  428. dprintk(1, "reqbufs: file io in progress\n");
  429. return -EBUSY;
  430. }
  431. return 0;
  432. }
  433. /**
  434. * __reqbufs() - Initiate streaming
  435. * @q: videobuf2 queue
  436. * @req: struct passed from userspace to vidioc_reqbufs handler in driver
  437. *
  438. * Should be called from vidioc_reqbufs ioctl handler of a driver.
  439. * This function:
  440. * 1) verifies streaming parameters passed from the userspace,
  441. * 2) sets up the queue,
  442. * 3) negotiates number of buffers and planes per buffer with the driver
  443. * to be used during streaming,
  444. * 4) allocates internal buffer structures (struct vb2_buffer), according to
  445. * the agreed parameters,
  446. * 5) for MMAP memory type, allocates actual video memory, using the
  447. * memory handling/allocation routines provided during queue initialization
  448. *
  449. * If req->count is 0, all the memory will be freed instead.
  450. * If the queue has been allocated previously (by a previous vb2_reqbufs) call
  451. * and the queue is not busy, memory will be reallocated.
  452. *
  453. * The return values from this function are intended to be directly returned
  454. * from vidioc_reqbufs handler in driver.
  455. */
  456. static int __reqbufs(struct vb2_queue *q, struct v4l2_requestbuffers *req)
  457. {
  458. unsigned int num_buffers, allocated_buffers, num_planes = 0;
  459. int ret;
  460. if (q->streaming) {
  461. dprintk(1, "reqbufs: streaming active\n");
  462. return -EBUSY;
  463. }
  464. if (req->count == 0 || q->num_buffers != 0 || q->memory != req->memory) {
  465. /*
  466. * We already have buffers allocated, so first check if they
  467. * are not in use and can be freed.
  468. */
  469. if (q->memory == V4L2_MEMORY_MMAP && __buffers_in_use(q)) {
  470. dprintk(1, "reqbufs: memory in use, cannot free\n");
  471. return -EBUSY;
  472. }
  473. __vb2_queue_free(q, q->num_buffers);
  474. /*
  475. * In case of REQBUFS(0) return immediately without calling
  476. * driver's queue_setup() callback and allocating resources.
  477. */
  478. if (req->count == 0)
  479. return 0;
  480. }
  481. /*
  482. * Make sure the requested values and current defaults are sane.
  483. */
  484. num_buffers = min_t(unsigned int, req->count, VIDEO_MAX_FRAME);
  485. memset(q->plane_sizes, 0, sizeof(q->plane_sizes));
  486. memset(q->alloc_ctx, 0, sizeof(q->alloc_ctx));
  487. q->memory = req->memory;
  488. /*
  489. * Ask the driver how many buffers and planes per buffer it requires.
  490. * Driver also sets the size and allocator context for each plane.
  491. */
  492. ret = call_qop(q, queue_setup, q, NULL, &num_buffers, &num_planes,
  493. q->plane_sizes, q->alloc_ctx);
  494. if (ret)
  495. return ret;
  496. /* Finally, allocate buffers and video memory */
  497. ret = __vb2_queue_alloc(q, req->memory, num_buffers, num_planes);
  498. if (ret == 0) {
  499. dprintk(1, "Memory allocation failed\n");
  500. return -ENOMEM;
  501. }
  502. allocated_buffers = ret;
  503. /*
  504. * Check if driver can handle the allocated number of buffers.
  505. */
  506. if (allocated_buffers < num_buffers) {
  507. num_buffers = allocated_buffers;
  508. ret = call_qop(q, queue_setup, q, NULL, &num_buffers,
  509. &num_planes, q->plane_sizes, q->alloc_ctx);
  510. if (!ret && allocated_buffers < num_buffers)
  511. ret = -ENOMEM;
  512. /*
  513. * Either the driver has accepted a smaller number of buffers,
  514. * or .queue_setup() returned an error
  515. */
  516. }
  517. q->num_buffers = allocated_buffers;
  518. if (ret < 0) {
  519. __vb2_queue_free(q, allocated_buffers);
  520. return ret;
  521. }
  522. /*
  523. * Return the number of successfully allocated buffers
  524. * to the userspace.
  525. */
  526. req->count = allocated_buffers;
  527. return 0;
  528. }
  529. /**
  530. * vb2_reqbufs() - Wrapper for __reqbufs() that also verifies the memory and
  531. * type values.
  532. * @q: videobuf2 queue
  533. * @req: struct passed from userspace to vidioc_reqbufs handler in driver
  534. */
  535. int vb2_reqbufs(struct vb2_queue *q, struct v4l2_requestbuffers *req)
  536. {
  537. int ret = __verify_memory_type(q, req->memory, req->type);
  538. return ret ? ret : __reqbufs(q, req);
  539. }
  540. EXPORT_SYMBOL_GPL(vb2_reqbufs);
  541. /**
  542. * __create_bufs() - Allocate buffers and any required auxiliary structs
  543. * @q: videobuf2 queue
  544. * @create: creation parameters, passed from userspace to vidioc_create_bufs
  545. * handler in driver
  546. *
  547. * Should be called from vidioc_create_bufs ioctl handler of a driver.
  548. * This function:
  549. * 1) verifies parameter sanity
  550. * 2) calls the .queue_setup() queue operation
  551. * 3) performs any necessary memory allocations
  552. *
  553. * The return values from this function are intended to be directly returned
  554. * from vidioc_create_bufs handler in driver.
  555. */
  556. static int __create_bufs(struct vb2_queue *q, struct v4l2_create_buffers *create)
  557. {
  558. unsigned int num_planes = 0, num_buffers, allocated_buffers;
  559. int ret;
  560. if (q->num_buffers == VIDEO_MAX_FRAME) {
  561. dprintk(1, "%s(): maximum number of buffers already allocated\n",
  562. __func__);
  563. return -ENOBUFS;
  564. }
  565. if (!q->num_buffers) {
  566. memset(q->plane_sizes, 0, sizeof(q->plane_sizes));
  567. memset(q->alloc_ctx, 0, sizeof(q->alloc_ctx));
  568. q->memory = create->memory;
  569. }
  570. num_buffers = min(create->count, VIDEO_MAX_FRAME - q->num_buffers);
  571. /*
  572. * Ask the driver, whether the requested number of buffers, planes per
  573. * buffer and their sizes are acceptable
  574. */
  575. ret = call_qop(q, queue_setup, q, &create->format, &num_buffers,
  576. &num_planes, q->plane_sizes, q->alloc_ctx);
  577. if (ret)
  578. return ret;
  579. /* Finally, allocate buffers and video memory */
  580. ret = __vb2_queue_alloc(q, create->memory, num_buffers,
  581. num_planes);
  582. if (ret == 0) {
  583. dprintk(1, "Memory allocation failed\n");
  584. return -ENOMEM;
  585. }
  586. allocated_buffers = ret;
  587. /*
  588. * Check if driver can handle the so far allocated number of buffers.
  589. */
  590. if (ret < num_buffers) {
  591. num_buffers = ret;
  592. /*
  593. * q->num_buffers contains the total number of buffers, that the
  594. * queue driver has set up
  595. */
  596. ret = call_qop(q, queue_setup, q, &create->format, &num_buffers,
  597. &num_planes, q->plane_sizes, q->alloc_ctx);
  598. if (!ret && allocated_buffers < num_buffers)
  599. ret = -ENOMEM;
  600. /*
  601. * Either the driver has accepted a smaller number of buffers,
  602. * or .queue_setup() returned an error
  603. */
  604. }
  605. q->num_buffers += allocated_buffers;
  606. if (ret < 0) {
  607. __vb2_queue_free(q, allocated_buffers);
  608. return -ENOMEM;
  609. }
  610. /*
  611. * Return the number of successfully allocated buffers
  612. * to the userspace.
  613. */
  614. create->count = allocated_buffers;
  615. return 0;
  616. }
  617. /**
  618. * vb2_create_bufs() - Wrapper for __create_bufs() that also verifies the
  619. * memory and type values.
  620. * @q: videobuf2 queue
  621. * @create: creation parameters, passed from userspace to vidioc_create_bufs
  622. * handler in driver
  623. */
  624. int vb2_create_bufs(struct vb2_queue *q, struct v4l2_create_buffers *create)
  625. {
  626. int ret = __verify_memory_type(q, create->memory, create->format.type);
  627. create->index = q->num_buffers;
  628. if (create->count == 0)
  629. return ret != -EBUSY ? ret : 0;
  630. return ret ? ret : __create_bufs(q, create);
  631. }
  632. EXPORT_SYMBOL_GPL(vb2_create_bufs);
  633. /**
  634. * vb2_plane_vaddr() - Return a kernel virtual address of a given plane
  635. * @vb: vb2_buffer to which the plane in question belongs to
  636. * @plane_no: plane number for which the address is to be returned
  637. *
  638. * This function returns a kernel virtual address of a given plane if
  639. * such a mapping exist, NULL otherwise.
  640. */
  641. void *vb2_plane_vaddr(struct vb2_buffer *vb, unsigned int plane_no)
  642. {
  643. struct vb2_queue *q = vb->vb2_queue;
  644. if (plane_no > vb->num_planes || !vb->planes[plane_no].mem_priv)
  645. return NULL;
  646. return call_memop(q, vaddr, vb->planes[plane_no].mem_priv);
  647. }
  648. EXPORT_SYMBOL_GPL(vb2_plane_vaddr);
  649. /**
  650. * vb2_plane_cookie() - Return allocator specific cookie for the given plane
  651. * @vb: vb2_buffer to which the plane in question belongs to
  652. * @plane_no: plane number for which the cookie is to be returned
  653. *
  654. * This function returns an allocator specific cookie for a given plane if
  655. * available, NULL otherwise. The allocator should provide some simple static
  656. * inline function, which would convert this cookie to the allocator specific
  657. * type that can be used directly by the driver to access the buffer. This can
  658. * be for example physical address, pointer to scatter list or IOMMU mapping.
  659. */
  660. void *vb2_plane_cookie(struct vb2_buffer *vb, unsigned int plane_no)
  661. {
  662. struct vb2_queue *q = vb->vb2_queue;
  663. if (plane_no > vb->num_planes || !vb->planes[plane_no].mem_priv)
  664. return NULL;
  665. return call_memop(q, cookie, vb->planes[plane_no].mem_priv);
  666. }
  667. EXPORT_SYMBOL_GPL(vb2_plane_cookie);
  668. /**
  669. * vb2_buffer_done() - inform videobuf that an operation on a buffer is finished
  670. * @vb: vb2_buffer returned from the driver
  671. * @state: either VB2_BUF_STATE_DONE if the operation finished successfully
  672. * or VB2_BUF_STATE_ERROR if the operation finished with an error
  673. *
  674. * This function should be called by the driver after a hardware operation on
  675. * a buffer is finished and the buffer may be returned to userspace. The driver
  676. * cannot use this buffer anymore until it is queued back to it by videobuf
  677. * by the means of buf_queue callback. Only buffers previously queued to the
  678. * driver by buf_queue can be passed to this function.
  679. */
  680. void vb2_buffer_done(struct vb2_buffer *vb, enum vb2_buffer_state state)
  681. {
  682. struct vb2_queue *q = vb->vb2_queue;
  683. unsigned long flags;
  684. if (vb->state != VB2_BUF_STATE_ACTIVE)
  685. return;
  686. if (state != VB2_BUF_STATE_DONE && state != VB2_BUF_STATE_ERROR)
  687. return;
  688. dprintk(4, "Done processing on buffer %d, state: %d\n",
  689. vb->v4l2_buf.index, vb->state);
  690. /* Add the buffer to the done buffers list */
  691. spin_lock_irqsave(&q->done_lock, flags);
  692. vb->state = state;
  693. list_add_tail(&vb->done_entry, &q->done_list);
  694. atomic_dec(&q->queued_count);
  695. spin_unlock_irqrestore(&q->done_lock, flags);
  696. /* Inform any processes that may be waiting for buffers */
  697. wake_up(&q->done_wq);
  698. }
  699. EXPORT_SYMBOL_GPL(vb2_buffer_done);
  700. /**
  701. * __fill_vb2_buffer() - fill a vb2_buffer with information provided in
  702. * a v4l2_buffer by the userspace
  703. */
  704. static int __fill_vb2_buffer(struct vb2_buffer *vb, const struct v4l2_buffer *b,
  705. struct v4l2_plane *v4l2_planes)
  706. {
  707. unsigned int plane;
  708. int ret;
  709. if (V4L2_TYPE_IS_MULTIPLANAR(b->type)) {
  710. /*
  711. * Verify that the userspace gave us a valid array for
  712. * plane information.
  713. */
  714. ret = __verify_planes_array(vb, b);
  715. if (ret)
  716. return ret;
  717. /* Fill in driver-provided information for OUTPUT types */
  718. if (V4L2_TYPE_IS_OUTPUT(b->type)) {
  719. /*
  720. * Will have to go up to b->length when API starts
  721. * accepting variable number of planes.
  722. */
  723. for (plane = 0; plane < vb->num_planes; ++plane) {
  724. v4l2_planes[plane].bytesused =
  725. b->m.planes[plane].bytesused;
  726. v4l2_planes[plane].data_offset =
  727. b->m.planes[plane].data_offset;
  728. }
  729. }
  730. if (b->memory == V4L2_MEMORY_USERPTR) {
  731. for (plane = 0; plane < vb->num_planes; ++plane) {
  732. v4l2_planes[plane].m.userptr =
  733. b->m.planes[plane].m.userptr;
  734. v4l2_planes[plane].length =
  735. b->m.planes[plane].length;
  736. }
  737. }
  738. } else {
  739. /*
  740. * Single-planar buffers do not use planes array,
  741. * so fill in relevant v4l2_buffer struct fields instead.
  742. * In videobuf we use our internal V4l2_planes struct for
  743. * single-planar buffers as well, for simplicity.
  744. */
  745. if (V4L2_TYPE_IS_OUTPUT(b->type))
  746. v4l2_planes[0].bytesused = b->bytesused;
  747. if (b->memory == V4L2_MEMORY_USERPTR) {
  748. v4l2_planes[0].m.userptr = b->m.userptr;
  749. v4l2_planes[0].length = b->length;
  750. }
  751. }
  752. vb->v4l2_buf.field = b->field;
  753. vb->v4l2_buf.timestamp = b->timestamp;
  754. vb->v4l2_buf.flags = b->flags & ~V4L2_BUFFER_STATE_FLAGS;
  755. return 0;
  756. }
  757. /**
  758. * __qbuf_userptr() - handle qbuf of a USERPTR buffer
  759. */
  760. static int __qbuf_userptr(struct vb2_buffer *vb, const struct v4l2_buffer *b)
  761. {
  762. struct v4l2_plane planes[VIDEO_MAX_PLANES];
  763. struct vb2_queue *q = vb->vb2_queue;
  764. void *mem_priv;
  765. unsigned int plane;
  766. int ret;
  767. int write = !V4L2_TYPE_IS_OUTPUT(q->type);
  768. /* Verify and copy relevant information provided by the userspace */
  769. ret = __fill_vb2_buffer(vb, b, planes);
  770. if (ret)
  771. return ret;
  772. for (plane = 0; plane < vb->num_planes; ++plane) {
  773. /* Skip the plane if already verified */
  774. if (vb->v4l2_planes[plane].m.userptr &&
  775. vb->v4l2_planes[plane].m.userptr == planes[plane].m.userptr
  776. && vb->v4l2_planes[plane].length == planes[plane].length)
  777. continue;
  778. dprintk(3, "qbuf: userspace address for plane %d changed, "
  779. "reacquiring memory\n", plane);
  780. /* Check if the provided plane buffer is large enough */
  781. if (planes[plane].length < q->plane_sizes[plane]) {
  782. ret = -EINVAL;
  783. goto err;
  784. }
  785. /* Release previously acquired memory if present */
  786. if (vb->planes[plane].mem_priv)
  787. call_memop(q, put_userptr, vb->planes[plane].mem_priv);
  788. vb->planes[plane].mem_priv = NULL;
  789. vb->v4l2_planes[plane].m.userptr = 0;
  790. vb->v4l2_planes[plane].length = 0;
  791. /* Acquire each plane's memory */
  792. mem_priv = call_memop(q, get_userptr, q->alloc_ctx[plane],
  793. planes[plane].m.userptr,
  794. planes[plane].length, write);
  795. if (IS_ERR_OR_NULL(mem_priv)) {
  796. dprintk(1, "qbuf: failed acquiring userspace "
  797. "memory for plane %d\n", plane);
  798. ret = mem_priv ? PTR_ERR(mem_priv) : -EINVAL;
  799. goto err;
  800. }
  801. vb->planes[plane].mem_priv = mem_priv;
  802. }
  803. /*
  804. * Call driver-specific initialization on the newly acquired buffer,
  805. * if provided.
  806. */
  807. ret = call_qop(q, buf_init, vb);
  808. if (ret) {
  809. dprintk(1, "qbuf: buffer initialization failed\n");
  810. goto err;
  811. }
  812. /*
  813. * Now that everything is in order, copy relevant information
  814. * provided by userspace.
  815. */
  816. for (plane = 0; plane < vb->num_planes; ++plane)
  817. vb->v4l2_planes[plane] = planes[plane];
  818. return 0;
  819. err:
  820. /* In case of errors, release planes that were already acquired */
  821. for (plane = 0; plane < vb->num_planes; ++plane) {
  822. if (vb->planes[plane].mem_priv)
  823. call_memop(q, put_userptr, vb->planes[plane].mem_priv);
  824. vb->planes[plane].mem_priv = NULL;
  825. vb->v4l2_planes[plane].m.userptr = 0;
  826. vb->v4l2_planes[plane].length = 0;
  827. }
  828. return ret;
  829. }
  830. /**
  831. * __qbuf_mmap() - handle qbuf of an MMAP buffer
  832. */
  833. static int __qbuf_mmap(struct vb2_buffer *vb, const struct v4l2_buffer *b)
  834. {
  835. return __fill_vb2_buffer(vb, b, vb->v4l2_planes);
  836. }
  837. /**
  838. * __enqueue_in_driver() - enqueue a vb2_buffer in driver for processing
  839. */
  840. static void __enqueue_in_driver(struct vb2_buffer *vb)
  841. {
  842. struct vb2_queue *q = vb->vb2_queue;
  843. vb->state = VB2_BUF_STATE_ACTIVE;
  844. atomic_inc(&q->queued_count);
  845. q->ops->buf_queue(vb);
  846. }
  847. static int __buf_prepare(struct vb2_buffer *vb, const struct v4l2_buffer *b)
  848. {
  849. struct vb2_queue *q = vb->vb2_queue;
  850. int ret;
  851. switch (q->memory) {
  852. case V4L2_MEMORY_MMAP:
  853. ret = __qbuf_mmap(vb, b);
  854. break;
  855. case V4L2_MEMORY_USERPTR:
  856. ret = __qbuf_userptr(vb, b);
  857. break;
  858. default:
  859. WARN(1, "Invalid queue type\n");
  860. ret = -EINVAL;
  861. }
  862. if (!ret)
  863. ret = call_qop(q, buf_prepare, vb);
  864. if (ret)
  865. dprintk(1, "qbuf: buffer preparation failed: %d\n", ret);
  866. else
  867. vb->state = VB2_BUF_STATE_PREPARED;
  868. return ret;
  869. }
  870. /**
  871. * vb2_prepare_buf() - Pass ownership of a buffer from userspace to the kernel
  872. * @q: videobuf2 queue
  873. * @b: buffer structure passed from userspace to vidioc_prepare_buf
  874. * handler in driver
  875. *
  876. * Should be called from vidioc_prepare_buf ioctl handler of a driver.
  877. * This function:
  878. * 1) verifies the passed buffer,
  879. * 2) calls buf_prepare callback in the driver (if provided), in which
  880. * driver-specific buffer initialization can be performed,
  881. *
  882. * The return values from this function are intended to be directly returned
  883. * from vidioc_prepare_buf handler in driver.
  884. */
  885. int vb2_prepare_buf(struct vb2_queue *q, struct v4l2_buffer *b)
  886. {
  887. struct vb2_buffer *vb;
  888. int ret;
  889. if (q->fileio) {
  890. dprintk(1, "%s(): file io in progress\n", __func__);
  891. return -EBUSY;
  892. }
  893. if (b->type != q->type) {
  894. dprintk(1, "%s(): invalid buffer type\n", __func__);
  895. return -EINVAL;
  896. }
  897. if (b->index >= q->num_buffers) {
  898. dprintk(1, "%s(): buffer index out of range\n", __func__);
  899. return -EINVAL;
  900. }
  901. vb = q->bufs[b->index];
  902. if (NULL == vb) {
  903. /* Should never happen */
  904. dprintk(1, "%s(): buffer is NULL\n", __func__);
  905. return -EINVAL;
  906. }
  907. if (b->memory != q->memory) {
  908. dprintk(1, "%s(): invalid memory type\n", __func__);
  909. return -EINVAL;
  910. }
  911. if (vb->state != VB2_BUF_STATE_DEQUEUED) {
  912. dprintk(1, "%s(): invalid buffer state %d\n", __func__, vb->state);
  913. return -EINVAL;
  914. }
  915. ret = __buf_prepare(vb, b);
  916. if (ret < 0)
  917. return ret;
  918. __fill_v4l2_buffer(vb, b);
  919. return 0;
  920. }
  921. EXPORT_SYMBOL_GPL(vb2_prepare_buf);
  922. /**
  923. * vb2_qbuf() - Queue a buffer from userspace
  924. * @q: videobuf2 queue
  925. * @b: buffer structure passed from userspace to vidioc_qbuf handler
  926. * in driver
  927. *
  928. * Should be called from vidioc_qbuf ioctl handler of a driver.
  929. * This function:
  930. * 1) verifies the passed buffer,
  931. * 2) if necessary, calls buf_prepare callback in the driver (if provided), in
  932. * which driver-specific buffer initialization can be performed,
  933. * 3) if streaming is on, queues the buffer in driver by the means of buf_queue
  934. * callback for processing.
  935. *
  936. * The return values from this function are intended to be directly returned
  937. * from vidioc_qbuf handler in driver.
  938. */
  939. int vb2_qbuf(struct vb2_queue *q, struct v4l2_buffer *b)
  940. {
  941. struct rw_semaphore *mmap_sem = NULL;
  942. struct vb2_buffer *vb;
  943. int ret = 0;
  944. /*
  945. * In case of user pointer buffers vb2 allocator needs to get direct
  946. * access to userspace pages. This requires getting read access on
  947. * mmap semaphore in the current process structure. The same
  948. * semaphore is taken before calling mmap operation, while both mmap
  949. * and qbuf are called by the driver or v4l2 core with driver's lock
  950. * held. To avoid a AB-BA deadlock (mmap_sem then driver's lock in
  951. * mmap and driver's lock then mmap_sem in qbuf) the videobuf2 core
  952. * release driver's lock, takes mmap_sem and then takes again driver's
  953. * lock.
  954. *
  955. * To avoid race with other vb2 calls, which might be called after
  956. * releasing driver's lock, this operation is performed at the
  957. * beggining of qbuf processing. This way the queue status is
  958. * consistent after getting driver's lock back.
  959. */
  960. if (q->memory == V4L2_MEMORY_USERPTR) {
  961. mmap_sem = &current->mm->mmap_sem;
  962. call_qop(q, wait_prepare, q);
  963. down_read(mmap_sem);
  964. call_qop(q, wait_finish, q);
  965. }
  966. if (q->fileio) {
  967. dprintk(1, "qbuf: file io in progress\n");
  968. ret = -EBUSY;
  969. goto unlock;
  970. }
  971. if (b->type != q->type) {
  972. dprintk(1, "qbuf: invalid buffer type\n");
  973. ret = -EINVAL;
  974. goto unlock;
  975. }
  976. if (b->index >= q->num_buffers) {
  977. dprintk(1, "qbuf: buffer index out of range\n");
  978. ret = -EINVAL;
  979. goto unlock;
  980. }
  981. vb = q->bufs[b->index];
  982. if (NULL == vb) {
  983. /* Should never happen */
  984. dprintk(1, "qbuf: buffer is NULL\n");
  985. ret = -EINVAL;
  986. goto unlock;
  987. }
  988. if (b->memory != q->memory) {
  989. dprintk(1, "qbuf: invalid memory type\n");
  990. ret = -EINVAL;
  991. goto unlock;
  992. }
  993. switch (vb->state) {
  994. case VB2_BUF_STATE_DEQUEUED:
  995. ret = __buf_prepare(vb, b);
  996. if (ret)
  997. goto unlock;
  998. case VB2_BUF_STATE_PREPARED:
  999. break;
  1000. default:
  1001. dprintk(1, "qbuf: buffer already in use\n");
  1002. ret = -EINVAL;
  1003. goto unlock;
  1004. }
  1005. /*
  1006. * Add to the queued buffers list, a buffer will stay on it until
  1007. * dequeued in dqbuf.
  1008. */
  1009. list_add_tail(&vb->queued_entry, &q->queued_list);
  1010. vb->state = VB2_BUF_STATE_QUEUED;
  1011. /*
  1012. * If already streaming, give the buffer to driver for processing.
  1013. * If not, the buffer will be given to driver on next streamon.
  1014. */
  1015. if (q->streaming)
  1016. __enqueue_in_driver(vb);
  1017. /* Fill buffer information for the userspace */
  1018. __fill_v4l2_buffer(vb, b);
  1019. dprintk(1, "qbuf of buffer %d succeeded\n", vb->v4l2_buf.index);
  1020. unlock:
  1021. if (mmap_sem)
  1022. up_read(mmap_sem);
  1023. return ret;
  1024. }
  1025. EXPORT_SYMBOL_GPL(vb2_qbuf);
  1026. /**
  1027. * __vb2_wait_for_done_vb() - wait for a buffer to become available
  1028. * for dequeuing
  1029. *
  1030. * Will sleep if required for nonblocking == false.
  1031. */
  1032. static int __vb2_wait_for_done_vb(struct vb2_queue *q, int nonblocking)
  1033. {
  1034. /*
  1035. * All operations on vb_done_list are performed under done_lock
  1036. * spinlock protection. However, buffers may be removed from
  1037. * it and returned to userspace only while holding both driver's
  1038. * lock and the done_lock spinlock. Thus we can be sure that as
  1039. * long as we hold the driver's lock, the list will remain not
  1040. * empty if list_empty() check succeeds.
  1041. */
  1042. for (;;) {
  1043. int ret;
  1044. if (!q->streaming) {
  1045. dprintk(1, "Streaming off, will not wait for buffers\n");
  1046. return -EINVAL;
  1047. }
  1048. if (!list_empty(&q->done_list)) {
  1049. /*
  1050. * Found a buffer that we were waiting for.
  1051. */
  1052. break;
  1053. }
  1054. if (nonblocking) {
  1055. dprintk(1, "Nonblocking and no buffers to dequeue, "
  1056. "will not wait\n");
  1057. return -EAGAIN;
  1058. }
  1059. /*
  1060. * We are streaming and blocking, wait for another buffer to
  1061. * become ready or for streamoff. Driver's lock is released to
  1062. * allow streamoff or qbuf to be called while waiting.
  1063. */
  1064. call_qop(q, wait_prepare, q);
  1065. /*
  1066. * All locks have been released, it is safe to sleep now.
  1067. */
  1068. dprintk(3, "Will sleep waiting for buffers\n");
  1069. ret = wait_event_interruptible(q->done_wq,
  1070. !list_empty(&q->done_list) || !q->streaming);
  1071. /*
  1072. * We need to reevaluate both conditions again after reacquiring
  1073. * the locks or return an error if one occurred.
  1074. */
  1075. call_qop(q, wait_finish, q);
  1076. if (ret)
  1077. return ret;
  1078. }
  1079. return 0;
  1080. }
  1081. /**
  1082. * __vb2_get_done_vb() - get a buffer ready for dequeuing
  1083. *
  1084. * Will sleep if required for nonblocking == false.
  1085. */
  1086. static int __vb2_get_done_vb(struct vb2_queue *q, struct vb2_buffer **vb,
  1087. int nonblocking)
  1088. {
  1089. unsigned long flags;
  1090. int ret;
  1091. /*
  1092. * Wait for at least one buffer to become available on the done_list.
  1093. */
  1094. ret = __vb2_wait_for_done_vb(q, nonblocking);
  1095. if (ret)
  1096. return ret;
  1097. /*
  1098. * Driver's lock has been held since we last verified that done_list
  1099. * is not empty, so no need for another list_empty(done_list) check.
  1100. */
  1101. spin_lock_irqsave(&q->done_lock, flags);
  1102. *vb = list_first_entry(&q->done_list, struct vb2_buffer, done_entry);
  1103. list_del(&(*vb)->done_entry);
  1104. spin_unlock_irqrestore(&q->done_lock, flags);
  1105. return 0;
  1106. }
  1107. /**
  1108. * vb2_wait_for_all_buffers() - wait until all buffers are given back to vb2
  1109. * @q: videobuf2 queue
  1110. *
  1111. * This function will wait until all buffers that have been given to the driver
  1112. * by buf_queue() are given back to vb2 with vb2_buffer_done(). It doesn't call
  1113. * wait_prepare, wait_finish pair. It is intended to be called with all locks
  1114. * taken, for example from stop_streaming() callback.
  1115. */
  1116. int vb2_wait_for_all_buffers(struct vb2_queue *q)
  1117. {
  1118. if (!q->streaming) {
  1119. dprintk(1, "Streaming off, will not wait for buffers\n");
  1120. return -EINVAL;
  1121. }
  1122. wait_event(q->done_wq, !atomic_read(&q->queued_count));
  1123. return 0;
  1124. }
  1125. EXPORT_SYMBOL_GPL(vb2_wait_for_all_buffers);
  1126. /**
  1127. * vb2_dqbuf() - Dequeue a buffer to the userspace
  1128. * @q: videobuf2 queue
  1129. * @b: buffer structure passed from userspace to vidioc_dqbuf handler
  1130. * in driver
  1131. * @nonblocking: if true, this call will not sleep waiting for a buffer if no
  1132. * buffers ready for dequeuing are present. Normally the driver
  1133. * would be passing (file->f_flags & O_NONBLOCK) here
  1134. *
  1135. * Should be called from vidioc_dqbuf ioctl handler of a driver.
  1136. * This function:
  1137. * 1) verifies the passed buffer,
  1138. * 2) calls buf_finish callback in the driver (if provided), in which
  1139. * driver can perform any additional operations that may be required before
  1140. * returning the buffer to userspace, such as cache sync,
  1141. * 3) the buffer struct members are filled with relevant information for
  1142. * the userspace.
  1143. *
  1144. * The return values from this function are intended to be directly returned
  1145. * from vidioc_dqbuf handler in driver.
  1146. */
  1147. int vb2_dqbuf(struct vb2_queue *q, struct v4l2_buffer *b, bool nonblocking)
  1148. {
  1149. struct vb2_buffer *vb = NULL;
  1150. int ret;
  1151. if (q->fileio) {
  1152. dprintk(1, "dqbuf: file io in progress\n");
  1153. return -EBUSY;
  1154. }
  1155. if (b->type != q->type) {
  1156. dprintk(1, "dqbuf: invalid buffer type\n");
  1157. return -EINVAL;
  1158. }
  1159. ret = __vb2_get_done_vb(q, &vb, nonblocking);
  1160. if (ret < 0) {
  1161. dprintk(1, "dqbuf: error getting next done buffer\n");
  1162. return ret;
  1163. }
  1164. ret = call_qop(q, buf_finish, vb);
  1165. if (ret) {
  1166. dprintk(1, "dqbuf: buffer finish failed\n");
  1167. return ret;
  1168. }
  1169. switch (vb->state) {
  1170. case VB2_BUF_STATE_DONE:
  1171. dprintk(3, "dqbuf: Returning done buffer\n");
  1172. break;
  1173. case VB2_BUF_STATE_ERROR:
  1174. dprintk(3, "dqbuf: Returning done buffer with errors\n");
  1175. break;
  1176. default:
  1177. dprintk(1, "dqbuf: Invalid buffer state\n");
  1178. return -EINVAL;
  1179. }
  1180. /* Fill buffer information for the userspace */
  1181. __fill_v4l2_buffer(vb, b);
  1182. /* Remove from videobuf queue */
  1183. list_del(&vb->queued_entry);
  1184. dprintk(1, "dqbuf of buffer %d, with state %d\n",
  1185. vb->v4l2_buf.index, vb->state);
  1186. vb->state = VB2_BUF_STATE_DEQUEUED;
  1187. return 0;
  1188. }
  1189. EXPORT_SYMBOL_GPL(vb2_dqbuf);
  1190. /**
  1191. * __vb2_queue_cancel() - cancel and stop (pause) streaming
  1192. *
  1193. * Removes all queued buffers from driver's queue and all buffers queued by
  1194. * userspace from videobuf's queue. Returns to state after reqbufs.
  1195. */
  1196. static void __vb2_queue_cancel(struct vb2_queue *q)
  1197. {
  1198. unsigned int i;
  1199. /*
  1200. * Tell driver to stop all transactions and release all queued
  1201. * buffers.
  1202. */
  1203. if (q->streaming)
  1204. call_qop(q, stop_streaming, q);
  1205. q->streaming = 0;
  1206. /*
  1207. * Remove all buffers from videobuf's list...
  1208. */
  1209. INIT_LIST_HEAD(&q->queued_list);
  1210. /*
  1211. * ...and done list; userspace will not receive any buffers it
  1212. * has not already dequeued before initiating cancel.
  1213. */
  1214. INIT_LIST_HEAD(&q->done_list);
  1215. atomic_set(&q->queued_count, 0);
  1216. wake_up_all(&q->done_wq);
  1217. /*
  1218. * Reinitialize all buffers for next use.
  1219. */
  1220. for (i = 0; i < q->num_buffers; ++i)
  1221. q->bufs[i]->state = VB2_BUF_STATE_DEQUEUED;
  1222. }
  1223. /**
  1224. * vb2_streamon - start streaming
  1225. * @q: videobuf2 queue
  1226. * @type: type argument passed from userspace to vidioc_streamon handler
  1227. *
  1228. * Should be called from vidioc_streamon handler of a driver.
  1229. * This function:
  1230. * 1) verifies current state
  1231. * 2) passes any previously queued buffers to the driver and starts streaming
  1232. *
  1233. * The return values from this function are intended to be directly returned
  1234. * from vidioc_streamon handler in the driver.
  1235. */
  1236. int vb2_streamon(struct vb2_queue *q, enum v4l2_buf_type type)
  1237. {
  1238. struct vb2_buffer *vb;
  1239. int ret;
  1240. if (q->fileio) {
  1241. dprintk(1, "streamon: file io in progress\n");
  1242. return -EBUSY;
  1243. }
  1244. if (type != q->type) {
  1245. dprintk(1, "streamon: invalid stream type\n");
  1246. return -EINVAL;
  1247. }
  1248. if (q->streaming) {
  1249. dprintk(1, "streamon: already streaming\n");
  1250. return -EBUSY;
  1251. }
  1252. /*
  1253. * If any buffers were queued before streamon,
  1254. * we can now pass them to driver for processing.
  1255. */
  1256. list_for_each_entry(vb, &q->queued_list, queued_entry)
  1257. __enqueue_in_driver(vb);
  1258. /*
  1259. * Let driver notice that streaming state has been enabled.
  1260. */
  1261. ret = call_qop(q, start_streaming, q, atomic_read(&q->queued_count));
  1262. if (ret) {
  1263. dprintk(1, "streamon: driver refused to start streaming\n");
  1264. __vb2_queue_cancel(q);
  1265. return ret;
  1266. }
  1267. q->streaming = 1;
  1268. dprintk(3, "Streamon successful\n");
  1269. return 0;
  1270. }
  1271. EXPORT_SYMBOL_GPL(vb2_streamon);
  1272. /**
  1273. * vb2_streamoff - stop streaming
  1274. * @q: videobuf2 queue
  1275. * @type: type argument passed from userspace to vidioc_streamoff handler
  1276. *
  1277. * Should be called from vidioc_streamoff handler of a driver.
  1278. * This function:
  1279. * 1) verifies current state,
  1280. * 2) stop streaming and dequeues any queued buffers, including those previously
  1281. * passed to the driver (after waiting for the driver to finish).
  1282. *
  1283. * This call can be used for pausing playback.
  1284. * The return values from this function are intended to be directly returned
  1285. * from vidioc_streamoff handler in the driver
  1286. */
  1287. int vb2_streamoff(struct vb2_queue *q, enum v4l2_buf_type type)
  1288. {
  1289. if (q->fileio) {
  1290. dprintk(1, "streamoff: file io in progress\n");
  1291. return -EBUSY;
  1292. }
  1293. if (type != q->type) {
  1294. dprintk(1, "streamoff: invalid stream type\n");
  1295. return -EINVAL;
  1296. }
  1297. if (!q->streaming) {
  1298. dprintk(1, "streamoff: not streaming\n");
  1299. return -EINVAL;
  1300. }
  1301. /*
  1302. * Cancel will pause streaming and remove all buffers from the driver
  1303. * and videobuf, effectively returning control over them to userspace.
  1304. */
  1305. __vb2_queue_cancel(q);
  1306. dprintk(3, "Streamoff successful\n");
  1307. return 0;
  1308. }
  1309. EXPORT_SYMBOL_GPL(vb2_streamoff);
  1310. /**
  1311. * __find_plane_by_offset() - find plane associated with the given offset off
  1312. */
  1313. static int __find_plane_by_offset(struct vb2_queue *q, unsigned long off,
  1314. unsigned int *_buffer, unsigned int *_plane)
  1315. {
  1316. struct vb2_buffer *vb;
  1317. unsigned int buffer, plane;
  1318. /*
  1319. * Go over all buffers and their planes, comparing the given offset
  1320. * with an offset assigned to each plane. If a match is found,
  1321. * return its buffer and plane numbers.
  1322. */
  1323. for (buffer = 0; buffer < q->num_buffers; ++buffer) {
  1324. vb = q->bufs[buffer];
  1325. for (plane = 0; plane < vb->num_planes; ++plane) {
  1326. if (vb->v4l2_planes[plane].m.mem_offset == off) {
  1327. *_buffer = buffer;
  1328. *_plane = plane;
  1329. return 0;
  1330. }
  1331. }
  1332. }
  1333. return -EINVAL;
  1334. }
  1335. /**
  1336. * vb2_mmap() - map video buffers into application address space
  1337. * @q: videobuf2 queue
  1338. * @vma: vma passed to the mmap file operation handler in the driver
  1339. *
  1340. * Should be called from mmap file operation handler of a driver.
  1341. * This function maps one plane of one of the available video buffers to
  1342. * userspace. To map whole video memory allocated on reqbufs, this function
  1343. * has to be called once per each plane per each buffer previously allocated.
  1344. *
  1345. * When the userspace application calls mmap, it passes to it an offset returned
  1346. * to it earlier by the means of vidioc_querybuf handler. That offset acts as
  1347. * a "cookie", which is then used to identify the plane to be mapped.
  1348. * This function finds a plane with a matching offset and a mapping is performed
  1349. * by the means of a provided memory operation.
  1350. *
  1351. * The return values from this function are intended to be directly returned
  1352. * from the mmap handler in driver.
  1353. */
  1354. int vb2_mmap(struct vb2_queue *q, struct vm_area_struct *vma)
  1355. {
  1356. unsigned long off = vma->vm_pgoff << PAGE_SHIFT;
  1357. struct vb2_buffer *vb;
  1358. unsigned int buffer, plane;
  1359. int ret;
  1360. if (q->memory != V4L2_MEMORY_MMAP) {
  1361. dprintk(1, "Queue is not currently set up for mmap\n");
  1362. return -EINVAL;
  1363. }
  1364. /*
  1365. * Check memory area access mode.
  1366. */
  1367. if (!(vma->vm_flags & VM_SHARED)) {
  1368. dprintk(1, "Invalid vma flags, VM_SHARED needed\n");
  1369. return -EINVAL;
  1370. }
  1371. if (V4L2_TYPE_IS_OUTPUT(q->type)) {
  1372. if (!(vma->vm_flags & VM_WRITE)) {
  1373. dprintk(1, "Invalid vma flags, VM_WRITE needed\n");
  1374. return -EINVAL;
  1375. }
  1376. } else {
  1377. if (!(vma->vm_flags & VM_READ)) {
  1378. dprintk(1, "Invalid vma flags, VM_READ needed\n");
  1379. return -EINVAL;
  1380. }
  1381. }
  1382. /*
  1383. * Find the plane corresponding to the offset passed by userspace.
  1384. */
  1385. ret = __find_plane_by_offset(q, off, &buffer, &plane);
  1386. if (ret)
  1387. return ret;
  1388. vb = q->bufs[buffer];
  1389. ret = call_memop(q, mmap, vb->planes[plane].mem_priv, vma);
  1390. if (ret)
  1391. return ret;
  1392. dprintk(3, "Buffer %d, plane %d successfully mapped\n", buffer, plane);
  1393. return 0;
  1394. }
  1395. EXPORT_SYMBOL_GPL(vb2_mmap);
  1396. #ifndef CONFIG_MMU
  1397. unsigned long vb2_get_unmapped_area(struct vb2_queue *q,
  1398. unsigned long addr,
  1399. unsigned long len,
  1400. unsigned long pgoff,
  1401. unsigned long flags)
  1402. {
  1403. unsigned long off = pgoff << PAGE_SHIFT;
  1404. struct vb2_buffer *vb;
  1405. unsigned int buffer, plane;
  1406. int ret;
  1407. if (q->memory != V4L2_MEMORY_MMAP) {
  1408. dprintk(1, "Queue is not currently set up for mmap\n");
  1409. return -EINVAL;
  1410. }
  1411. /*
  1412. * Find the plane corresponding to the offset passed by userspace.
  1413. */
  1414. ret = __find_plane_by_offset(q, off, &buffer, &plane);
  1415. if (ret)
  1416. return ret;
  1417. vb = q->bufs[buffer];
  1418. return (unsigned long)vb2_plane_vaddr(vb, plane);
  1419. }
  1420. EXPORT_SYMBOL_GPL(vb2_get_unmapped_area);
  1421. #endif
  1422. static int __vb2_init_fileio(struct vb2_queue *q, int read);
  1423. static int __vb2_cleanup_fileio(struct vb2_queue *q);
  1424. /**
  1425. * vb2_poll() - implements poll userspace operation
  1426. * @q: videobuf2 queue
  1427. * @file: file argument passed to the poll file operation handler
  1428. * @wait: wait argument passed to the poll file operation handler
  1429. *
  1430. * This function implements poll file operation handler for a driver.
  1431. * For CAPTURE queues, if a buffer is ready to be dequeued, the userspace will
  1432. * be informed that the file descriptor of a video device is available for
  1433. * reading.
  1434. * For OUTPUT queues, if a buffer is ready to be dequeued, the file descriptor
  1435. * will be reported as available for writing.
  1436. *
  1437. * If the driver uses struct v4l2_fh, then vb2_poll() will also check for any
  1438. * pending events.
  1439. *
  1440. * The return values from this function are intended to be directly returned
  1441. * from poll handler in driver.
  1442. */
  1443. unsigned int vb2_poll(struct vb2_queue *q, struct file *file, poll_table *wait)
  1444. {
  1445. struct video_device *vfd = video_devdata(file);
  1446. unsigned long req_events = poll_requested_events(wait);
  1447. struct vb2_buffer *vb = NULL;
  1448. unsigned int res = 0;
  1449. unsigned long flags;
  1450. if (test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags)) {
  1451. struct v4l2_fh *fh = file->private_data;
  1452. if (v4l2_event_pending(fh))
  1453. res = POLLPRI;
  1454. else if (req_events & POLLPRI)
  1455. poll_wait(file, &fh->wait, wait);
  1456. }
  1457. /*
  1458. * Start file I/O emulator only if streaming API has not been used yet.
  1459. */
  1460. if (q->num_buffers == 0 && q->fileio == NULL) {
  1461. if (!V4L2_TYPE_IS_OUTPUT(q->type) && (q->io_modes & VB2_READ) &&
  1462. (req_events & (POLLIN | POLLRDNORM))) {
  1463. if (__vb2_init_fileio(q, 1))
  1464. return res | POLLERR;
  1465. }
  1466. if (V4L2_TYPE_IS_OUTPUT(q->type) && (q->io_modes & VB2_WRITE) &&
  1467. (req_events & (POLLOUT | POLLWRNORM))) {
  1468. if (__vb2_init_fileio(q, 0))
  1469. return res | POLLERR;
  1470. /*
  1471. * Write to OUTPUT queue can be done immediately.
  1472. */
  1473. return res | POLLOUT | POLLWRNORM;
  1474. }
  1475. }
  1476. /*
  1477. * There is nothing to wait for if no buffers have already been queued.
  1478. */
  1479. if (list_empty(&q->queued_list))
  1480. return res | POLLERR;
  1481. poll_wait(file, &q->done_wq, wait);
  1482. /*
  1483. * Take first buffer available for dequeuing.
  1484. */
  1485. spin_lock_irqsave(&q->done_lock, flags);
  1486. if (!list_empty(&q->done_list))
  1487. vb = list_first_entry(&q->done_list, struct vb2_buffer,
  1488. done_entry);
  1489. spin_unlock_irqrestore(&q->done_lock, flags);
  1490. if (vb && (vb->state == VB2_BUF_STATE_DONE
  1491. || vb->state == VB2_BUF_STATE_ERROR)) {
  1492. return (V4L2_TYPE_IS_OUTPUT(q->type)) ?
  1493. res | POLLOUT | POLLWRNORM :
  1494. res | POLLIN | POLLRDNORM;
  1495. }
  1496. return res;
  1497. }
  1498. EXPORT_SYMBOL_GPL(vb2_poll);
  1499. /**
  1500. * vb2_queue_init() - initialize a videobuf2 queue
  1501. * @q: videobuf2 queue; this structure should be allocated in driver
  1502. *
  1503. * The vb2_queue structure should be allocated by the driver. The driver is
  1504. * responsible of clearing it's content and setting initial values for some
  1505. * required entries before calling this function.
  1506. * q->ops, q->mem_ops, q->type and q->io_modes are mandatory. Please refer
  1507. * to the struct vb2_queue description in include/media/videobuf2-core.h
  1508. * for more information.
  1509. */
  1510. int vb2_queue_init(struct vb2_queue *q)
  1511. {
  1512. /*
  1513. * Sanity check
  1514. */
  1515. if (WARN_ON(!q) ||
  1516. WARN_ON(!q->ops) ||
  1517. WARN_ON(!q->mem_ops) ||
  1518. WARN_ON(!q->type) ||
  1519. WARN_ON(!q->io_modes) ||
  1520. WARN_ON(!q->ops->queue_setup) ||
  1521. WARN_ON(!q->ops->buf_queue))
  1522. return -EINVAL;
  1523. INIT_LIST_HEAD(&q->queued_list);
  1524. INIT_LIST_HEAD(&q->done_list);
  1525. spin_lock_init(&q->done_lock);
  1526. init_waitqueue_head(&q->done_wq);
  1527. if (q->buf_struct_size == 0)
  1528. q->buf_struct_size = sizeof(struct vb2_buffer);
  1529. return 0;
  1530. }
  1531. EXPORT_SYMBOL_GPL(vb2_queue_init);
  1532. /**
  1533. * vb2_queue_release() - stop streaming, release the queue and free memory
  1534. * @q: videobuf2 queue
  1535. *
  1536. * This function stops streaming and performs necessary clean ups, including
  1537. * freeing video buffer memory. The driver is responsible for freeing
  1538. * the vb2_queue structure itself.
  1539. */
  1540. void vb2_queue_release(struct vb2_queue *q)
  1541. {
  1542. __vb2_cleanup_fileio(q);
  1543. __vb2_queue_cancel(q);
  1544. __vb2_queue_free(q, q->num_buffers);
  1545. }
  1546. EXPORT_SYMBOL_GPL(vb2_queue_release);
  1547. /**
  1548. * struct vb2_fileio_buf - buffer context used by file io emulator
  1549. *
  1550. * vb2 provides a compatibility layer and emulator of file io (read and
  1551. * write) calls on top of streaming API. This structure is used for
  1552. * tracking context related to the buffers.
  1553. */
  1554. struct vb2_fileio_buf {
  1555. void *vaddr;
  1556. unsigned int size;
  1557. unsigned int pos;
  1558. unsigned int queued:1;
  1559. };
  1560. /**
  1561. * struct vb2_fileio_data - queue context used by file io emulator
  1562. *
  1563. * vb2 provides a compatibility layer and emulator of file io (read and
  1564. * write) calls on top of streaming API. For proper operation it required
  1565. * this structure to save the driver state between each call of the read
  1566. * or write function.
  1567. */
  1568. struct vb2_fileio_data {
  1569. struct v4l2_requestbuffers req;
  1570. struct v4l2_buffer b;
  1571. struct vb2_fileio_buf bufs[VIDEO_MAX_FRAME];
  1572. unsigned int index;
  1573. unsigned int q_count;
  1574. unsigned int dq_count;
  1575. unsigned int flags;
  1576. };
  1577. /**
  1578. * __vb2_init_fileio() - initialize file io emulator
  1579. * @q: videobuf2 queue
  1580. * @read: mode selector (1 means read, 0 means write)
  1581. */
  1582. static int __vb2_init_fileio(struct vb2_queue *q, int read)
  1583. {
  1584. struct vb2_fileio_data *fileio;
  1585. int i, ret;
  1586. unsigned int count = 0;
  1587. /*
  1588. * Sanity check
  1589. */
  1590. if ((read && !(q->io_modes & VB2_READ)) ||
  1591. (!read && !(q->io_modes & VB2_WRITE)))
  1592. BUG();
  1593. /*
  1594. * Check if device supports mapping buffers to kernel virtual space.
  1595. */
  1596. if (!q->mem_ops->vaddr)
  1597. return -EBUSY;
  1598. /*
  1599. * Check if streaming api has not been already activated.
  1600. */
  1601. if (q->streaming || q->num_buffers > 0)
  1602. return -EBUSY;
  1603. /*
  1604. * Start with count 1, driver can increase it in queue_setup()
  1605. */
  1606. count = 1;
  1607. dprintk(3, "setting up file io: mode %s, count %d, flags %08x\n",
  1608. (read) ? "read" : "write", count, q->io_flags);
  1609. fileio = kzalloc(sizeof(struct vb2_fileio_data), GFP_KERNEL);
  1610. if (fileio == NULL)
  1611. return -ENOMEM;
  1612. fileio->flags = q->io_flags;
  1613. /*
  1614. * Request buffers and use MMAP type to force driver
  1615. * to allocate buffers by itself.
  1616. */
  1617. fileio->req.count = count;
  1618. fileio->req.memory = V4L2_MEMORY_MMAP;
  1619. fileio->req.type = q->type;
  1620. ret = vb2_reqbufs(q, &fileio->req);
  1621. if (ret)
  1622. goto err_kfree;
  1623. /*
  1624. * Check if plane_count is correct
  1625. * (multiplane buffers are not supported).
  1626. */
  1627. if (q->bufs[0]->num_planes != 1) {
  1628. ret = -EBUSY;
  1629. goto err_reqbufs;
  1630. }
  1631. /*
  1632. * Get kernel address of each buffer.
  1633. */
  1634. for (i = 0; i < q->num_buffers; i++) {
  1635. fileio->bufs[i].vaddr = vb2_plane_vaddr(q->bufs[i], 0);
  1636. if (fileio->bufs[i].vaddr == NULL)
  1637. goto err_reqbufs;
  1638. fileio->bufs[i].size = vb2_plane_size(q->bufs[i], 0);
  1639. }
  1640. /*
  1641. * Read mode requires pre queuing of all buffers.
  1642. */
  1643. if (read) {
  1644. /*
  1645. * Queue all buffers.
  1646. */
  1647. for (i = 0; i < q->num_buffers; i++) {
  1648. struct v4l2_buffer *b = &fileio->b;
  1649. memset(b, 0, sizeof(*b));
  1650. b->type = q->type;
  1651. b->memory = q->memory;
  1652. b->index = i;
  1653. ret = vb2_qbuf(q, b);
  1654. if (ret)
  1655. goto err_reqbufs;
  1656. fileio->bufs[i].queued = 1;
  1657. }
  1658. /*
  1659. * Start streaming.
  1660. */
  1661. ret = vb2_streamon(q, q->type);
  1662. if (ret)
  1663. goto err_reqbufs;
  1664. }
  1665. q->fileio = fileio;
  1666. return ret;
  1667. err_reqbufs:
  1668. fileio->req.count = 0;
  1669. vb2_reqbufs(q, &fileio->req);
  1670. err_kfree:
  1671. kfree(fileio);
  1672. return ret;
  1673. }
  1674. /**
  1675. * __vb2_cleanup_fileio() - free resourced used by file io emulator
  1676. * @q: videobuf2 queue
  1677. */
  1678. static int __vb2_cleanup_fileio(struct vb2_queue *q)
  1679. {
  1680. struct vb2_fileio_data *fileio = q->fileio;
  1681. if (fileio) {
  1682. /*
  1683. * Hack fileio context to enable direct calls to vb2 ioctl
  1684. * interface.
  1685. */
  1686. q->fileio = NULL;
  1687. vb2_streamoff(q, q->type);
  1688. fileio->req.count = 0;
  1689. vb2_reqbufs(q, &fileio->req);
  1690. kfree(fileio);
  1691. dprintk(3, "file io emulator closed\n");
  1692. }
  1693. return 0;
  1694. }
  1695. /**
  1696. * __vb2_perform_fileio() - perform a single file io (read or write) operation
  1697. * @q: videobuf2 queue
  1698. * @data: pointed to target userspace buffer
  1699. * @count: number of bytes to read or write
  1700. * @ppos: file handle position tracking pointer
  1701. * @nonblock: mode selector (1 means blocking calls, 0 means nonblocking)
  1702. * @read: access mode selector (1 means read, 0 means write)
  1703. */
  1704. static size_t __vb2_perform_fileio(struct vb2_queue *q, char __user *data, size_t count,
  1705. loff_t *ppos, int nonblock, int read)
  1706. {
  1707. struct vb2_fileio_data *fileio;
  1708. struct vb2_fileio_buf *buf;
  1709. int ret, index;
  1710. dprintk(3, "file io: mode %s, offset %ld, count %zd, %sblocking\n",
  1711. read ? "read" : "write", (long)*ppos, count,
  1712. nonblock ? "non" : "");
  1713. if (!data)
  1714. return -EINVAL;
  1715. /*
  1716. * Initialize emulator on first call.
  1717. */
  1718. if (!q->fileio) {
  1719. ret = __vb2_init_fileio(q, read);
  1720. dprintk(3, "file io: vb2_init_fileio result: %d\n", ret);
  1721. if (ret)
  1722. return ret;
  1723. }
  1724. fileio = q->fileio;
  1725. /*
  1726. * Hack fileio context to enable direct calls to vb2 ioctl interface.
  1727. * The pointer will be restored before returning from this function.
  1728. */
  1729. q->fileio = NULL;
  1730. index = fileio->index;
  1731. buf = &fileio->bufs[index];
  1732. /*
  1733. * Check if we need to dequeue the buffer.
  1734. */
  1735. if (buf->queued) {
  1736. struct vb2_buffer *vb;
  1737. /*
  1738. * Call vb2_dqbuf to get buffer back.
  1739. */
  1740. memset(&fileio->b, 0, sizeof(fileio->b));
  1741. fileio->b.type = q->type;
  1742. fileio->b.memory = q->memory;
  1743. fileio->b.index = index;
  1744. ret = vb2_dqbuf(q, &fileio->b, nonblock);
  1745. dprintk(5, "file io: vb2_dqbuf result: %d\n", ret);
  1746. if (ret)
  1747. goto end;
  1748. fileio->dq_count += 1;
  1749. /*
  1750. * Get number of bytes filled by the driver
  1751. */
  1752. vb = q->bufs[index];
  1753. buf->size = vb2_get_plane_payload(vb, 0);
  1754. buf->queued = 0;
  1755. }
  1756. /*
  1757. * Limit count on last few bytes of the buffer.
  1758. */
  1759. if (buf->pos + count > buf->size) {
  1760. count = buf->size - buf->pos;
  1761. dprintk(5, "reducing read count: %zd\n", count);
  1762. }
  1763. /*
  1764. * Transfer data to userspace.
  1765. */
  1766. dprintk(3, "file io: copying %zd bytes - buffer %d, offset %u\n",
  1767. count, index, buf->pos);
  1768. if (read)
  1769. ret = copy_to_user(data, buf->vaddr + buf->pos, count);
  1770. else
  1771. ret = copy_from_user(buf->vaddr + buf->pos, data, count);
  1772. if (ret) {
  1773. dprintk(3, "file io: error copying data\n");
  1774. ret = -EFAULT;
  1775. goto end;
  1776. }
  1777. /*
  1778. * Update counters.
  1779. */
  1780. buf->pos += count;
  1781. *ppos += count;
  1782. /*
  1783. * Queue next buffer if required.
  1784. */
  1785. if (buf->pos == buf->size ||
  1786. (!read && (fileio->flags & VB2_FILEIO_WRITE_IMMEDIATELY))) {
  1787. /*
  1788. * Check if this is the last buffer to read.
  1789. */
  1790. if (read && (fileio->flags & VB2_FILEIO_READ_ONCE) &&
  1791. fileio->dq_count == 1) {
  1792. dprintk(3, "file io: read limit reached\n");
  1793. /*
  1794. * Restore fileio pointer and release the context.
  1795. */
  1796. q->fileio = fileio;
  1797. return __vb2_cleanup_fileio(q);
  1798. }
  1799. /*
  1800. * Call vb2_qbuf and give buffer to the driver.
  1801. */
  1802. memset(&fileio->b, 0, sizeof(fileio->b));
  1803. fileio->b.type = q->type;
  1804. fileio->b.memory = q->memory;
  1805. fileio->b.index = index;
  1806. fileio->b.bytesused = buf->pos;
  1807. ret = vb2_qbuf(q, &fileio->b);
  1808. dprintk(5, "file io: vb2_dbuf result: %d\n", ret);
  1809. if (ret)
  1810. goto end;
  1811. /*
  1812. * Buffer has been queued, update the status
  1813. */
  1814. buf->pos = 0;
  1815. buf->queued = 1;
  1816. buf->size = q->bufs[0]->v4l2_planes[0].length;
  1817. fileio->q_count += 1;
  1818. /*
  1819. * Switch to the next buffer
  1820. */
  1821. fileio->index = (index + 1) % q->num_buffers;
  1822. /*
  1823. * Start streaming if required.
  1824. */
  1825. if (!read && !q->streaming) {
  1826. ret = vb2_streamon(q, q->type);
  1827. if (ret)
  1828. goto end;
  1829. }
  1830. }
  1831. /*
  1832. * Return proper number of bytes processed.
  1833. */
  1834. if (ret == 0)
  1835. ret = count;
  1836. end:
  1837. /*
  1838. * Restore the fileio context and block vb2 ioctl interface.
  1839. */
  1840. q->fileio = fileio;
  1841. return ret;
  1842. }
  1843. size_t vb2_read(struct vb2_queue *q, char __user *data, size_t count,
  1844. loff_t *ppos, int nonblocking)
  1845. {
  1846. return __vb2_perform_fileio(q, data, count, ppos, nonblocking, 1);
  1847. }
  1848. EXPORT_SYMBOL_GPL(vb2_read);
  1849. size_t vb2_write(struct vb2_queue *q, char __user *data, size_t count,
  1850. loff_t *ppos, int nonblocking)
  1851. {
  1852. return __vb2_perform_fileio(q, data, count, ppos, nonblocking, 0);
  1853. }
  1854. EXPORT_SYMBOL_GPL(vb2_write);
  1855. /*
  1856. * The following functions are not part of the vb2 core API, but are helper
  1857. * functions that plug into struct v4l2_ioctl_ops, struct v4l2_file_operations
  1858. * and struct vb2_ops.
  1859. * They contain boilerplate code that most if not all drivers have to do
  1860. * and so they simplify the driver code.
  1861. */
  1862. /* The queue is busy if there is a owner and you are not that owner. */
  1863. static inline bool vb2_queue_is_busy(struct video_device *vdev, struct file *file)
  1864. {
  1865. return vdev->queue->owner && vdev->queue->owner != file->private_data;
  1866. }
  1867. /* vb2 ioctl helpers */
  1868. int vb2_ioctl_reqbufs(struct file *file, void *priv,
  1869. struct v4l2_requestbuffers *p)
  1870. {
  1871. struct video_device *vdev = video_devdata(file);
  1872. int res = __verify_memory_type(vdev->queue, p->memory, p->type);
  1873. if (res)
  1874. return res;
  1875. if (vb2_queue_is_busy(vdev, file))
  1876. return -EBUSY;
  1877. res = __reqbufs(vdev->queue, p);
  1878. /* If count == 0, then the owner has released all buffers and he
  1879. is no longer owner of the queue. Otherwise we have a new owner. */
  1880. if (res == 0)
  1881. vdev->queue->owner = p->count ? file->private_data : NULL;
  1882. return res;
  1883. }
  1884. EXPORT_SYMBOL_GPL(vb2_ioctl_reqbufs);
  1885. int vb2_ioctl_create_bufs(struct file *file, void *priv,
  1886. struct v4l2_create_buffers *p)
  1887. {
  1888. struct video_device *vdev = video_devdata(file);
  1889. int res = __verify_memory_type(vdev->queue, p->memory, p->format.type);
  1890. p->index = vdev->queue->num_buffers;
  1891. /* If count == 0, then just check if memory and type are valid.
  1892. Any -EBUSY result from __verify_memory_type can be mapped to 0. */
  1893. if (p->count == 0)
  1894. return res != -EBUSY ? res : 0;
  1895. if (res)
  1896. return res;
  1897. if (vb2_queue_is_busy(vdev, file))
  1898. return -EBUSY;
  1899. res = __create_bufs(vdev->queue, p);
  1900. if (res == 0)
  1901. vdev->queue->owner = file->private_data;
  1902. return res;
  1903. }
  1904. EXPORT_SYMBOL_GPL(vb2_ioctl_create_bufs);
  1905. int vb2_ioctl_prepare_buf(struct file *file, void *priv,
  1906. struct v4l2_buffer *p)
  1907. {
  1908. struct video_device *vdev = video_devdata(file);
  1909. if (vb2_queue_is_busy(vdev, file))
  1910. return -EBUSY;
  1911. return vb2_prepare_buf(vdev->queue, p);
  1912. }
  1913. EXPORT_SYMBOL_GPL(vb2_ioctl_prepare_buf);
  1914. int vb2_ioctl_querybuf(struct file *file, void *priv, struct v4l2_buffer *p)
  1915. {
  1916. struct video_device *vdev = video_devdata(file);
  1917. /* No need to call vb2_queue_is_busy(), anyone can query buffers. */
  1918. return vb2_querybuf(vdev->queue, p);
  1919. }
  1920. EXPORT_SYMBOL_GPL(vb2_ioctl_querybuf);
  1921. int vb2_ioctl_qbuf(struct file *file, void *priv, struct v4l2_buffer *p)
  1922. {
  1923. struct video_device *vdev = video_devdata(file);
  1924. if (vb2_queue_is_busy(vdev, file))
  1925. return -EBUSY;
  1926. return vb2_qbuf(vdev->queue, p);
  1927. }
  1928. EXPORT_SYMBOL_GPL(vb2_ioctl_qbuf);
  1929. int vb2_ioctl_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
  1930. {
  1931. struct video_device *vdev = video_devdata(file);
  1932. if (vb2_queue_is_busy(vdev, file))
  1933. return -EBUSY;
  1934. return vb2_dqbuf(vdev->queue, p, file->f_flags & O_NONBLOCK);
  1935. }
  1936. EXPORT_SYMBOL_GPL(vb2_ioctl_dqbuf);
  1937. int vb2_ioctl_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
  1938. {
  1939. struct video_device *vdev = video_devdata(file);
  1940. if (vb2_queue_is_busy(vdev, file))
  1941. return -EBUSY;
  1942. return vb2_streamon(vdev->queue, i);
  1943. }
  1944. EXPORT_SYMBOL_GPL(vb2_ioctl_streamon);
  1945. int vb2_ioctl_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
  1946. {
  1947. struct video_device *vdev = video_devdata(file);
  1948. if (vb2_queue_is_busy(vdev, file))
  1949. return -EBUSY;
  1950. return vb2_streamoff(vdev->queue, i);
  1951. }
  1952. EXPORT_SYMBOL_GPL(vb2_ioctl_streamoff);
  1953. /* v4l2_file_operations helpers */
  1954. int vb2_fop_mmap(struct file *file, struct vm_area_struct *vma)
  1955. {
  1956. struct video_device *vdev = video_devdata(file);
  1957. return vb2_mmap(vdev->queue, vma);
  1958. }
  1959. EXPORT_SYMBOL_GPL(vb2_fop_mmap);
  1960. int vb2_fop_release(struct file *file)
  1961. {
  1962. struct video_device *vdev = video_devdata(file);
  1963. if (file->private_data == vdev->queue->owner) {
  1964. vb2_queue_release(vdev->queue);
  1965. vdev->queue->owner = NULL;
  1966. }
  1967. return v4l2_fh_release(file);
  1968. }
  1969. EXPORT_SYMBOL_GPL(vb2_fop_release);
  1970. ssize_t vb2_fop_write(struct file *file, char __user *buf,
  1971. size_t count, loff_t *ppos)
  1972. {
  1973. struct video_device *vdev = video_devdata(file);
  1974. struct mutex *lock = vdev->queue->lock ? vdev->queue->lock : vdev->lock;
  1975. int err = -EBUSY;
  1976. if (lock && mutex_lock_interruptible(lock))
  1977. return -ERESTARTSYS;
  1978. if (vb2_queue_is_busy(vdev, file))
  1979. goto exit;
  1980. err = vb2_write(vdev->queue, buf, count, ppos,
  1981. file->f_flags & O_NONBLOCK);
  1982. if (vdev->queue->fileio)
  1983. vdev->queue->owner = file->private_data;
  1984. exit:
  1985. if (lock)
  1986. mutex_unlock(lock);
  1987. return err;
  1988. }
  1989. EXPORT_SYMBOL_GPL(vb2_fop_write);
  1990. ssize_t vb2_fop_read(struct file *file, char __user *buf,
  1991. size_t count, loff_t *ppos)
  1992. {
  1993. struct video_device *vdev = video_devdata(file);
  1994. struct mutex *lock = vdev->queue->lock ? vdev->queue->lock : vdev->lock;
  1995. int err = -EBUSY;
  1996. if (lock && mutex_lock_interruptible(lock))
  1997. return -ERESTARTSYS;
  1998. if (vb2_queue_is_busy(vdev, file))
  1999. goto exit;
  2000. err = vb2_read(vdev->queue, buf, count, ppos,
  2001. file->f_flags & O_NONBLOCK);
  2002. if (vdev->queue->fileio)
  2003. vdev->queue->owner = file->private_data;
  2004. exit:
  2005. if (lock)
  2006. mutex_unlock(lock);
  2007. return err;
  2008. }
  2009. EXPORT_SYMBOL_GPL(vb2_fop_read);
  2010. unsigned int vb2_fop_poll(struct file *file, poll_table *wait)
  2011. {
  2012. struct video_device *vdev = video_devdata(file);
  2013. struct vb2_queue *q = vdev->queue;
  2014. struct mutex *lock = q->lock ? q->lock : vdev->lock;
  2015. unsigned long req_events = poll_requested_events(wait);
  2016. unsigned res;
  2017. void *fileio;
  2018. bool must_lock = false;
  2019. /* Try to be smart: only lock if polling might start fileio,
  2020. otherwise locking will only introduce unwanted delays. */
  2021. if (q->num_buffers == 0 && q->fileio == NULL) {
  2022. if (!V4L2_TYPE_IS_OUTPUT(q->type) && (q->io_modes & VB2_READ) &&
  2023. (req_events & (POLLIN | POLLRDNORM)))
  2024. must_lock = true;
  2025. else if (V4L2_TYPE_IS_OUTPUT(q->type) && (q->io_modes & VB2_WRITE) &&
  2026. (req_events & (POLLOUT | POLLWRNORM)))
  2027. must_lock = true;
  2028. }
  2029. /* If locking is needed, but this helper doesn't know how, then you
  2030. shouldn't be using this helper but you should write your own. */
  2031. WARN_ON(must_lock && !lock);
  2032. if (must_lock && lock && mutex_lock_interruptible(lock))
  2033. return POLLERR;
  2034. fileio = q->fileio;
  2035. res = vb2_poll(vdev->queue, file, wait);
  2036. /* If fileio was started, then we have a new queue owner. */
  2037. if (must_lock && !fileio && q->fileio)
  2038. q->owner = file->private_data;
  2039. if (must_lock && lock)
  2040. mutex_unlock(lock);
  2041. return res;
  2042. }
  2043. EXPORT_SYMBOL_GPL(vb2_fop_poll);
  2044. #ifndef CONFIG_MMU
  2045. unsigned long vb2_fop_get_unmapped_area(struct file *file, unsigned long addr,
  2046. unsigned long len, unsigned long pgoff, unsigned long flags)
  2047. {
  2048. struct video_device *vdev = video_devdata(file);
  2049. return vb2_get_unmapped_area(vdev->queue, addr, len, pgoff, flags);
  2050. }
  2051. EXPORT_SYMBOL_GPL(vb2_fop_get_unmapped_area);
  2052. #endif
  2053. /* vb2_ops helpers. Only use if vq->lock is non-NULL. */
  2054. void vb2_ops_wait_prepare(struct vb2_queue *vq)
  2055. {
  2056. mutex_unlock(vq->lock);
  2057. }
  2058. EXPORT_SYMBOL_GPL(vb2_ops_wait_prepare);
  2059. void vb2_ops_wait_finish(struct vb2_queue *vq)
  2060. {
  2061. mutex_lock(vq->lock);
  2062. }
  2063. EXPORT_SYMBOL_GPL(vb2_ops_wait_finish);
  2064. MODULE_DESCRIPTION("Driver helper framework for Video for Linux 2");
  2065. MODULE_AUTHOR("Pawel Osciak <pawel@osciak.com>, Marek Szyprowski");
  2066. MODULE_LICENSE("GPL");