videobuf2-core.c 72 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763
  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_MASK_FLAGS (V4L2_BUF_FLAG_MAPPED | V4L2_BUF_FLAG_QUEUED | \
  37. V4L2_BUF_FLAG_DONE | V4L2_BUF_FLAG_ERROR | \
  38. V4L2_BUF_FLAG_PREPARED | \
  39. V4L2_BUF_FLAG_TIMESTAMP_MASK)
  40. /**
  41. * __vb2_buf_mem_alloc() - allocate video memory for the given buffer
  42. */
  43. static int __vb2_buf_mem_alloc(struct vb2_buffer *vb)
  44. {
  45. struct vb2_queue *q = vb->vb2_queue;
  46. void *mem_priv;
  47. int plane;
  48. /*
  49. * Allocate memory for all planes in this buffer
  50. * NOTE: mmapped areas should be page aligned
  51. */
  52. for (plane = 0; plane < vb->num_planes; ++plane) {
  53. unsigned long size = PAGE_ALIGN(q->plane_sizes[plane]);
  54. mem_priv = call_memop(q, alloc, q->alloc_ctx[plane],
  55. size, q->gfp_flags);
  56. if (IS_ERR_OR_NULL(mem_priv))
  57. goto free;
  58. /* Associate allocator private data with this plane */
  59. vb->planes[plane].mem_priv = mem_priv;
  60. vb->v4l2_planes[plane].length = q->plane_sizes[plane];
  61. }
  62. return 0;
  63. free:
  64. /* Free already allocated memory if one of the allocations failed */
  65. for (; plane > 0; --plane) {
  66. call_memop(q, put, vb->planes[plane - 1].mem_priv);
  67. vb->planes[plane - 1].mem_priv = NULL;
  68. }
  69. return -ENOMEM;
  70. }
  71. /**
  72. * __vb2_buf_mem_free() - free memory of the given buffer
  73. */
  74. static void __vb2_buf_mem_free(struct vb2_buffer *vb)
  75. {
  76. struct vb2_queue *q = vb->vb2_queue;
  77. unsigned int plane;
  78. for (plane = 0; plane < vb->num_planes; ++plane) {
  79. call_memop(q, put, vb->planes[plane].mem_priv);
  80. vb->planes[plane].mem_priv = NULL;
  81. dprintk(3, "Freed plane %d of buffer %d\n", plane,
  82. vb->v4l2_buf.index);
  83. }
  84. }
  85. /**
  86. * __vb2_buf_userptr_put() - release userspace memory associated with
  87. * a USERPTR buffer
  88. */
  89. static void __vb2_buf_userptr_put(struct vb2_buffer *vb)
  90. {
  91. struct vb2_queue *q = vb->vb2_queue;
  92. unsigned int plane;
  93. for (plane = 0; plane < vb->num_planes; ++plane) {
  94. if (vb->planes[plane].mem_priv)
  95. call_memop(q, put_userptr, vb->planes[plane].mem_priv);
  96. vb->planes[plane].mem_priv = NULL;
  97. }
  98. }
  99. /**
  100. * __vb2_plane_dmabuf_put() - release memory associated with
  101. * a DMABUF shared plane
  102. */
  103. static void __vb2_plane_dmabuf_put(struct vb2_queue *q, struct vb2_plane *p)
  104. {
  105. if (!p->mem_priv)
  106. return;
  107. if (p->dbuf_mapped)
  108. call_memop(q, unmap_dmabuf, p->mem_priv);
  109. call_memop(q, detach_dmabuf, p->mem_priv);
  110. dma_buf_put(p->dbuf);
  111. memset(p, 0, sizeof(*p));
  112. }
  113. /**
  114. * __vb2_buf_dmabuf_put() - release memory associated with
  115. * a DMABUF shared buffer
  116. */
  117. static void __vb2_buf_dmabuf_put(struct vb2_buffer *vb)
  118. {
  119. struct vb2_queue *q = vb->vb2_queue;
  120. unsigned int plane;
  121. for (plane = 0; plane < vb->num_planes; ++plane)
  122. __vb2_plane_dmabuf_put(q, &vb->planes[plane]);
  123. }
  124. /**
  125. * __setup_offsets() - setup unique offsets ("cookies") for every plane in
  126. * every buffer on the queue
  127. */
  128. static void __setup_offsets(struct vb2_queue *q, unsigned int n)
  129. {
  130. unsigned int buffer, plane;
  131. struct vb2_buffer *vb;
  132. unsigned long off;
  133. if (q->num_buffers) {
  134. struct v4l2_plane *p;
  135. vb = q->bufs[q->num_buffers - 1];
  136. p = &vb->v4l2_planes[vb->num_planes - 1];
  137. off = PAGE_ALIGN(p->m.mem_offset + p->length);
  138. } else {
  139. off = 0;
  140. }
  141. for (buffer = q->num_buffers; buffer < q->num_buffers + n; ++buffer) {
  142. vb = q->bufs[buffer];
  143. if (!vb)
  144. continue;
  145. for (plane = 0; plane < vb->num_planes; ++plane) {
  146. vb->v4l2_planes[plane].length = q->plane_sizes[plane];
  147. vb->v4l2_planes[plane].m.mem_offset = off;
  148. dprintk(3, "Buffer %d, plane %d offset 0x%08lx\n",
  149. buffer, plane, off);
  150. off += vb->v4l2_planes[plane].length;
  151. off = PAGE_ALIGN(off);
  152. }
  153. }
  154. }
  155. /**
  156. * __vb2_queue_alloc() - allocate videobuf buffer structures and (for MMAP type)
  157. * video buffer memory for all buffers/planes on the queue and initializes the
  158. * queue
  159. *
  160. * Returns the number of buffers successfully allocated.
  161. */
  162. static int __vb2_queue_alloc(struct vb2_queue *q, enum v4l2_memory memory,
  163. unsigned int num_buffers, unsigned int num_planes)
  164. {
  165. unsigned int buffer;
  166. struct vb2_buffer *vb;
  167. int ret;
  168. for (buffer = 0; buffer < num_buffers; ++buffer) {
  169. /* Allocate videobuf buffer structures */
  170. vb = kzalloc(q->buf_struct_size, GFP_KERNEL);
  171. if (!vb) {
  172. dprintk(1, "Memory alloc for buffer struct failed\n");
  173. break;
  174. }
  175. /* Length stores number of planes for multiplanar buffers */
  176. if (V4L2_TYPE_IS_MULTIPLANAR(q->type))
  177. vb->v4l2_buf.length = num_planes;
  178. vb->state = VB2_BUF_STATE_DEQUEUED;
  179. vb->vb2_queue = q;
  180. vb->num_planes = num_planes;
  181. vb->v4l2_buf.index = q->num_buffers + buffer;
  182. vb->v4l2_buf.type = q->type;
  183. vb->v4l2_buf.memory = memory;
  184. /* Allocate video buffer memory for the MMAP type */
  185. if (memory == V4L2_MEMORY_MMAP) {
  186. ret = __vb2_buf_mem_alloc(vb);
  187. if (ret) {
  188. dprintk(1, "Failed allocating memory for "
  189. "buffer %d\n", buffer);
  190. kfree(vb);
  191. break;
  192. }
  193. /*
  194. * Call the driver-provided buffer initialization
  195. * callback, if given. An error in initialization
  196. * results in queue setup failure.
  197. */
  198. ret = call_qop(q, buf_init, vb);
  199. if (ret) {
  200. dprintk(1, "Buffer %d %p initialization"
  201. " failed\n", buffer, vb);
  202. __vb2_buf_mem_free(vb);
  203. kfree(vb);
  204. break;
  205. }
  206. }
  207. q->bufs[q->num_buffers + buffer] = vb;
  208. }
  209. __setup_offsets(q, buffer);
  210. dprintk(1, "Allocated %d buffers, %d plane(s) each\n",
  211. buffer, num_planes);
  212. return buffer;
  213. }
  214. /**
  215. * __vb2_free_mem() - release all video buffer memory for a given queue
  216. */
  217. static void __vb2_free_mem(struct vb2_queue *q, unsigned int buffers)
  218. {
  219. unsigned int buffer;
  220. struct vb2_buffer *vb;
  221. for (buffer = q->num_buffers - buffers; buffer < q->num_buffers;
  222. ++buffer) {
  223. vb = q->bufs[buffer];
  224. if (!vb)
  225. continue;
  226. /* Free MMAP buffers or release USERPTR buffers */
  227. if (q->memory == V4L2_MEMORY_MMAP)
  228. __vb2_buf_mem_free(vb);
  229. else if (q->memory == V4L2_MEMORY_DMABUF)
  230. __vb2_buf_dmabuf_put(vb);
  231. else
  232. __vb2_buf_userptr_put(vb);
  233. }
  234. }
  235. /**
  236. * __vb2_queue_free() - free buffers at the end of the queue - video memory and
  237. * related information, if no buffers are left return the queue to an
  238. * uninitialized state. Might be called even if the queue has already been freed.
  239. */
  240. static void __vb2_queue_free(struct vb2_queue *q, unsigned int buffers)
  241. {
  242. unsigned int buffer;
  243. /* Call driver-provided cleanup function for each buffer, if provided */
  244. if (q->ops->buf_cleanup) {
  245. for (buffer = q->num_buffers - buffers; buffer < q->num_buffers;
  246. ++buffer) {
  247. if (NULL == q->bufs[buffer])
  248. continue;
  249. q->ops->buf_cleanup(q->bufs[buffer]);
  250. }
  251. }
  252. /* Release video buffer memory */
  253. __vb2_free_mem(q, buffers);
  254. /* Free videobuf buffers */
  255. for (buffer = q->num_buffers - buffers; buffer < q->num_buffers;
  256. ++buffer) {
  257. kfree(q->bufs[buffer]);
  258. q->bufs[buffer] = NULL;
  259. }
  260. q->num_buffers -= buffers;
  261. if (!q->num_buffers)
  262. q->memory = 0;
  263. INIT_LIST_HEAD(&q->queued_list);
  264. }
  265. /**
  266. * __verify_planes_array() - verify that the planes array passed in struct
  267. * v4l2_buffer from userspace can be safely used
  268. */
  269. static int __verify_planes_array(struct vb2_buffer *vb, const struct v4l2_buffer *b)
  270. {
  271. if (!V4L2_TYPE_IS_MULTIPLANAR(b->type))
  272. return 0;
  273. /* Is memory for copying plane information present? */
  274. if (NULL == b->m.planes) {
  275. dprintk(1, "Multi-planar buffer passed but "
  276. "planes array not provided\n");
  277. return -EINVAL;
  278. }
  279. if (b->length < vb->num_planes || b->length > VIDEO_MAX_PLANES) {
  280. dprintk(1, "Incorrect planes array length, "
  281. "expected %d, got %d\n", vb->num_planes, b->length);
  282. return -EINVAL;
  283. }
  284. return 0;
  285. }
  286. /**
  287. * __verify_length() - Verify that the bytesused value for each plane fits in
  288. * the plane length and that the data offset doesn't exceed the bytesused value.
  289. */
  290. static int __verify_length(struct vb2_buffer *vb, const struct v4l2_buffer *b)
  291. {
  292. unsigned int length;
  293. unsigned int plane;
  294. if (!V4L2_TYPE_IS_OUTPUT(b->type))
  295. return 0;
  296. if (V4L2_TYPE_IS_MULTIPLANAR(b->type)) {
  297. for (plane = 0; plane < vb->num_planes; ++plane) {
  298. length = (b->memory == V4L2_MEMORY_USERPTR)
  299. ? b->m.planes[plane].length
  300. : vb->v4l2_planes[plane].length;
  301. if (b->m.planes[plane].bytesused > length)
  302. return -EINVAL;
  303. if (b->m.planes[plane].data_offset >=
  304. b->m.planes[plane].bytesused)
  305. return -EINVAL;
  306. }
  307. } else {
  308. length = (b->memory == V4L2_MEMORY_USERPTR)
  309. ? b->length : vb->v4l2_planes[0].length;
  310. if (b->bytesused > length)
  311. return -EINVAL;
  312. }
  313. return 0;
  314. }
  315. /**
  316. * __buffer_in_use() - return true if the buffer is in use and
  317. * the queue cannot be freed (by the means of REQBUFS(0)) call
  318. */
  319. static bool __buffer_in_use(struct vb2_queue *q, struct vb2_buffer *vb)
  320. {
  321. unsigned int plane;
  322. for (plane = 0; plane < vb->num_planes; ++plane) {
  323. void *mem_priv = vb->planes[plane].mem_priv;
  324. /*
  325. * If num_users() has not been provided, call_memop
  326. * will return 0, apparently nobody cares about this
  327. * case anyway. If num_users() returns more than 1,
  328. * we are not the only user of the plane's memory.
  329. */
  330. if (mem_priv && call_memop(q, num_users, mem_priv) > 1)
  331. return true;
  332. }
  333. return false;
  334. }
  335. /**
  336. * __buffers_in_use() - return true if any buffers on the queue are in use and
  337. * the queue cannot be freed (by the means of REQBUFS(0)) call
  338. */
  339. static bool __buffers_in_use(struct vb2_queue *q)
  340. {
  341. unsigned int buffer;
  342. for (buffer = 0; buffer < q->num_buffers; ++buffer) {
  343. if (__buffer_in_use(q, q->bufs[buffer]))
  344. return true;
  345. }
  346. return false;
  347. }
  348. /**
  349. * __fill_v4l2_buffer() - fill in a struct v4l2_buffer with information to be
  350. * returned to userspace
  351. */
  352. static void __fill_v4l2_buffer(struct vb2_buffer *vb, struct v4l2_buffer *b)
  353. {
  354. struct vb2_queue *q = vb->vb2_queue;
  355. /* Copy back data such as timestamp, flags, etc. */
  356. memcpy(b, &vb->v4l2_buf, offsetof(struct v4l2_buffer, m));
  357. b->reserved2 = vb->v4l2_buf.reserved2;
  358. b->reserved = vb->v4l2_buf.reserved;
  359. if (V4L2_TYPE_IS_MULTIPLANAR(q->type)) {
  360. /*
  361. * Fill in plane-related data if userspace provided an array
  362. * for it. The caller has already verified memory and size.
  363. */
  364. b->length = vb->num_planes;
  365. memcpy(b->m.planes, vb->v4l2_planes,
  366. b->length * sizeof(struct v4l2_plane));
  367. } else {
  368. /*
  369. * We use length and offset in v4l2_planes array even for
  370. * single-planar buffers, but userspace does not.
  371. */
  372. b->length = vb->v4l2_planes[0].length;
  373. b->bytesused = vb->v4l2_planes[0].bytesused;
  374. if (q->memory == V4L2_MEMORY_MMAP)
  375. b->m.offset = vb->v4l2_planes[0].m.mem_offset;
  376. else if (q->memory == V4L2_MEMORY_USERPTR)
  377. b->m.userptr = vb->v4l2_planes[0].m.userptr;
  378. else if (q->memory == V4L2_MEMORY_DMABUF)
  379. b->m.fd = vb->v4l2_planes[0].m.fd;
  380. }
  381. /*
  382. * Clear any buffer state related flags.
  383. */
  384. b->flags &= ~V4L2_BUFFER_MASK_FLAGS;
  385. b->flags |= q->timestamp_type;
  386. switch (vb->state) {
  387. case VB2_BUF_STATE_QUEUED:
  388. case VB2_BUF_STATE_ACTIVE:
  389. b->flags |= V4L2_BUF_FLAG_QUEUED;
  390. break;
  391. case VB2_BUF_STATE_ERROR:
  392. b->flags |= V4L2_BUF_FLAG_ERROR;
  393. /* fall through */
  394. case VB2_BUF_STATE_DONE:
  395. b->flags |= V4L2_BUF_FLAG_DONE;
  396. break;
  397. case VB2_BUF_STATE_PREPARED:
  398. b->flags |= V4L2_BUF_FLAG_PREPARED;
  399. break;
  400. case VB2_BUF_STATE_DEQUEUED:
  401. /* nothing */
  402. break;
  403. }
  404. if (__buffer_in_use(q, vb))
  405. b->flags |= V4L2_BUF_FLAG_MAPPED;
  406. }
  407. /**
  408. * vb2_querybuf() - query video buffer information
  409. * @q: videobuf queue
  410. * @b: buffer struct passed from userspace to vidioc_querybuf handler
  411. * in driver
  412. *
  413. * Should be called from vidioc_querybuf ioctl handler in driver.
  414. * This function will verify the passed v4l2_buffer structure and fill the
  415. * relevant information for the userspace.
  416. *
  417. * The return values from this function are intended to be directly returned
  418. * from vidioc_querybuf handler in driver.
  419. */
  420. int vb2_querybuf(struct vb2_queue *q, struct v4l2_buffer *b)
  421. {
  422. struct vb2_buffer *vb;
  423. int ret;
  424. if (b->type != q->type) {
  425. dprintk(1, "querybuf: wrong buffer type\n");
  426. return -EINVAL;
  427. }
  428. if (b->index >= q->num_buffers) {
  429. dprintk(1, "querybuf: buffer index out of range\n");
  430. return -EINVAL;
  431. }
  432. vb = q->bufs[b->index];
  433. ret = __verify_planes_array(vb, b);
  434. if (!ret)
  435. __fill_v4l2_buffer(vb, b);
  436. return ret;
  437. }
  438. EXPORT_SYMBOL(vb2_querybuf);
  439. /**
  440. * __verify_userptr_ops() - verify that all memory operations required for
  441. * USERPTR queue type have been provided
  442. */
  443. static int __verify_userptr_ops(struct vb2_queue *q)
  444. {
  445. if (!(q->io_modes & VB2_USERPTR) || !q->mem_ops->get_userptr ||
  446. !q->mem_ops->put_userptr)
  447. return -EINVAL;
  448. return 0;
  449. }
  450. /**
  451. * __verify_mmap_ops() - verify that all memory operations required for
  452. * MMAP queue type have been provided
  453. */
  454. static int __verify_mmap_ops(struct vb2_queue *q)
  455. {
  456. if (!(q->io_modes & VB2_MMAP) || !q->mem_ops->alloc ||
  457. !q->mem_ops->put || !q->mem_ops->mmap)
  458. return -EINVAL;
  459. return 0;
  460. }
  461. /**
  462. * __verify_dmabuf_ops() - verify that all memory operations required for
  463. * DMABUF queue type have been provided
  464. */
  465. static int __verify_dmabuf_ops(struct vb2_queue *q)
  466. {
  467. if (!(q->io_modes & VB2_DMABUF) || !q->mem_ops->attach_dmabuf ||
  468. !q->mem_ops->detach_dmabuf || !q->mem_ops->map_dmabuf ||
  469. !q->mem_ops->unmap_dmabuf)
  470. return -EINVAL;
  471. return 0;
  472. }
  473. /**
  474. * __verify_memory_type() - Check whether the memory type and buffer type
  475. * passed to a buffer operation are compatible with the queue.
  476. */
  477. static int __verify_memory_type(struct vb2_queue *q,
  478. enum v4l2_memory memory, enum v4l2_buf_type type)
  479. {
  480. if (memory != V4L2_MEMORY_MMAP && memory != V4L2_MEMORY_USERPTR &&
  481. memory != V4L2_MEMORY_DMABUF) {
  482. dprintk(1, "reqbufs: unsupported memory type\n");
  483. return -EINVAL;
  484. }
  485. if (type != q->type) {
  486. dprintk(1, "reqbufs: requested type is incorrect\n");
  487. return -EINVAL;
  488. }
  489. /*
  490. * Make sure all the required memory ops for given memory type
  491. * are available.
  492. */
  493. if (memory == V4L2_MEMORY_MMAP && __verify_mmap_ops(q)) {
  494. dprintk(1, "reqbufs: MMAP for current setup unsupported\n");
  495. return -EINVAL;
  496. }
  497. if (memory == V4L2_MEMORY_USERPTR && __verify_userptr_ops(q)) {
  498. dprintk(1, "reqbufs: USERPTR for current setup unsupported\n");
  499. return -EINVAL;
  500. }
  501. if (memory == V4L2_MEMORY_DMABUF && __verify_dmabuf_ops(q)) {
  502. dprintk(1, "reqbufs: DMABUF for current setup unsupported\n");
  503. return -EINVAL;
  504. }
  505. /*
  506. * Place the busy tests at the end: -EBUSY can be ignored when
  507. * create_bufs is called with count == 0, but count == 0 should still
  508. * do the memory and type validation.
  509. */
  510. if (q->fileio) {
  511. dprintk(1, "reqbufs: file io in progress\n");
  512. return -EBUSY;
  513. }
  514. return 0;
  515. }
  516. /**
  517. * __reqbufs() - Initiate streaming
  518. * @q: videobuf2 queue
  519. * @req: struct passed from userspace to vidioc_reqbufs handler in driver
  520. *
  521. * Should be called from vidioc_reqbufs ioctl handler of a driver.
  522. * This function:
  523. * 1) verifies streaming parameters passed from the userspace,
  524. * 2) sets up the queue,
  525. * 3) negotiates number of buffers and planes per buffer with the driver
  526. * to be used during streaming,
  527. * 4) allocates internal buffer structures (struct vb2_buffer), according to
  528. * the agreed parameters,
  529. * 5) for MMAP memory type, allocates actual video memory, using the
  530. * memory handling/allocation routines provided during queue initialization
  531. *
  532. * If req->count is 0, all the memory will be freed instead.
  533. * If the queue has been allocated previously (by a previous vb2_reqbufs) call
  534. * and the queue is not busy, memory will be reallocated.
  535. *
  536. * The return values from this function are intended to be directly returned
  537. * from vidioc_reqbufs handler in driver.
  538. */
  539. static int __reqbufs(struct vb2_queue *q, struct v4l2_requestbuffers *req)
  540. {
  541. unsigned int num_buffers, allocated_buffers, num_planes = 0;
  542. int ret;
  543. if (q->streaming) {
  544. dprintk(1, "reqbufs: streaming active\n");
  545. return -EBUSY;
  546. }
  547. if (req->count == 0 || q->num_buffers != 0 || q->memory != req->memory) {
  548. /*
  549. * We already have buffers allocated, so first check if they
  550. * are not in use and can be freed.
  551. */
  552. if (q->memory == V4L2_MEMORY_MMAP && __buffers_in_use(q)) {
  553. dprintk(1, "reqbufs: memory in use, cannot free\n");
  554. return -EBUSY;
  555. }
  556. __vb2_queue_free(q, q->num_buffers);
  557. /*
  558. * In case of REQBUFS(0) return immediately without calling
  559. * driver's queue_setup() callback and allocating resources.
  560. */
  561. if (req->count == 0)
  562. return 0;
  563. }
  564. /*
  565. * Make sure the requested values and current defaults are sane.
  566. */
  567. num_buffers = min_t(unsigned int, req->count, VIDEO_MAX_FRAME);
  568. memset(q->plane_sizes, 0, sizeof(q->plane_sizes));
  569. memset(q->alloc_ctx, 0, sizeof(q->alloc_ctx));
  570. q->memory = req->memory;
  571. /*
  572. * Ask the driver how many buffers and planes per buffer it requires.
  573. * Driver also sets the size and allocator context for each plane.
  574. */
  575. ret = call_qop(q, queue_setup, q, NULL, &num_buffers, &num_planes,
  576. 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, req->memory, num_buffers, num_planes);
  581. if (ret == 0) {
  582. dprintk(1, "Memory allocation failed\n");
  583. return -ENOMEM;
  584. }
  585. allocated_buffers = ret;
  586. /*
  587. * Check if driver can handle the allocated number of buffers.
  588. */
  589. if (allocated_buffers < num_buffers) {
  590. num_buffers = allocated_buffers;
  591. ret = call_qop(q, queue_setup, q, NULL, &num_buffers,
  592. &num_planes, q->plane_sizes, q->alloc_ctx);
  593. if (!ret && allocated_buffers < num_buffers)
  594. ret = -ENOMEM;
  595. /*
  596. * Either the driver has accepted a smaller number of buffers,
  597. * or .queue_setup() returned an error
  598. */
  599. }
  600. q->num_buffers = allocated_buffers;
  601. if (ret < 0) {
  602. __vb2_queue_free(q, allocated_buffers);
  603. return ret;
  604. }
  605. /*
  606. * Return the number of successfully allocated buffers
  607. * to the userspace.
  608. */
  609. req->count = allocated_buffers;
  610. return 0;
  611. }
  612. /**
  613. * vb2_reqbufs() - Wrapper for __reqbufs() that also verifies the memory and
  614. * type values.
  615. * @q: videobuf2 queue
  616. * @req: struct passed from userspace to vidioc_reqbufs handler in driver
  617. */
  618. int vb2_reqbufs(struct vb2_queue *q, struct v4l2_requestbuffers *req)
  619. {
  620. int ret = __verify_memory_type(q, req->memory, req->type);
  621. return ret ? ret : __reqbufs(q, req);
  622. }
  623. EXPORT_SYMBOL_GPL(vb2_reqbufs);
  624. /**
  625. * __create_bufs() - Allocate buffers and any required auxiliary structs
  626. * @q: videobuf2 queue
  627. * @create: creation parameters, passed from userspace to vidioc_create_bufs
  628. * handler in driver
  629. *
  630. * Should be called from vidioc_create_bufs ioctl handler of a driver.
  631. * This function:
  632. * 1) verifies parameter sanity
  633. * 2) calls the .queue_setup() queue operation
  634. * 3) performs any necessary memory allocations
  635. *
  636. * The return values from this function are intended to be directly returned
  637. * from vidioc_create_bufs handler in driver.
  638. */
  639. static int __create_bufs(struct vb2_queue *q, struct v4l2_create_buffers *create)
  640. {
  641. unsigned int num_planes = 0, num_buffers, allocated_buffers;
  642. int ret;
  643. if (q->num_buffers == VIDEO_MAX_FRAME) {
  644. dprintk(1, "%s(): maximum number of buffers already allocated\n",
  645. __func__);
  646. return -ENOBUFS;
  647. }
  648. if (!q->num_buffers) {
  649. memset(q->plane_sizes, 0, sizeof(q->plane_sizes));
  650. memset(q->alloc_ctx, 0, sizeof(q->alloc_ctx));
  651. q->memory = create->memory;
  652. }
  653. num_buffers = min(create->count, VIDEO_MAX_FRAME - q->num_buffers);
  654. /*
  655. * Ask the driver, whether the requested number of buffers, planes per
  656. * buffer and their sizes are acceptable
  657. */
  658. ret = call_qop(q, queue_setup, q, &create->format, &num_buffers,
  659. &num_planes, q->plane_sizes, q->alloc_ctx);
  660. if (ret)
  661. return ret;
  662. /* Finally, allocate buffers and video memory */
  663. ret = __vb2_queue_alloc(q, create->memory, num_buffers,
  664. num_planes);
  665. if (ret == 0) {
  666. dprintk(1, "Memory allocation failed\n");
  667. return -ENOMEM;
  668. }
  669. allocated_buffers = ret;
  670. /*
  671. * Check if driver can handle the so far allocated number of buffers.
  672. */
  673. if (ret < num_buffers) {
  674. num_buffers = ret;
  675. /*
  676. * q->num_buffers contains the total number of buffers, that the
  677. * queue driver has set up
  678. */
  679. ret = call_qop(q, queue_setup, q, &create->format, &num_buffers,
  680. &num_planes, q->plane_sizes, q->alloc_ctx);
  681. if (!ret && allocated_buffers < num_buffers)
  682. ret = -ENOMEM;
  683. /*
  684. * Either the driver has accepted a smaller number of buffers,
  685. * or .queue_setup() returned an error
  686. */
  687. }
  688. q->num_buffers += allocated_buffers;
  689. if (ret < 0) {
  690. __vb2_queue_free(q, allocated_buffers);
  691. return -ENOMEM;
  692. }
  693. /*
  694. * Return the number of successfully allocated buffers
  695. * to the userspace.
  696. */
  697. create->count = allocated_buffers;
  698. return 0;
  699. }
  700. /**
  701. * vb2_create_bufs() - Wrapper for __create_bufs() that also verifies the
  702. * memory and type values.
  703. * @q: videobuf2 queue
  704. * @create: creation parameters, passed from userspace to vidioc_create_bufs
  705. * handler in driver
  706. */
  707. int vb2_create_bufs(struct vb2_queue *q, struct v4l2_create_buffers *create)
  708. {
  709. int ret = __verify_memory_type(q, create->memory, create->format.type);
  710. create->index = q->num_buffers;
  711. if (create->count == 0)
  712. return ret != -EBUSY ? ret : 0;
  713. return ret ? ret : __create_bufs(q, create);
  714. }
  715. EXPORT_SYMBOL_GPL(vb2_create_bufs);
  716. /**
  717. * vb2_plane_vaddr() - Return a kernel virtual address of a given plane
  718. * @vb: vb2_buffer to which the plane in question belongs to
  719. * @plane_no: plane number for which the address is to be returned
  720. *
  721. * This function returns a kernel virtual address of a given plane if
  722. * such a mapping exist, NULL otherwise.
  723. */
  724. void *vb2_plane_vaddr(struct vb2_buffer *vb, unsigned int plane_no)
  725. {
  726. struct vb2_queue *q = vb->vb2_queue;
  727. if (plane_no > vb->num_planes || !vb->planes[plane_no].mem_priv)
  728. return NULL;
  729. return call_memop(q, vaddr, vb->planes[plane_no].mem_priv);
  730. }
  731. EXPORT_SYMBOL_GPL(vb2_plane_vaddr);
  732. /**
  733. * vb2_plane_cookie() - Return allocator specific cookie for the given plane
  734. * @vb: vb2_buffer to which the plane in question belongs to
  735. * @plane_no: plane number for which the cookie is to be returned
  736. *
  737. * This function returns an allocator specific cookie for a given plane if
  738. * available, NULL otherwise. The allocator should provide some simple static
  739. * inline function, which would convert this cookie to the allocator specific
  740. * type that can be used directly by the driver to access the buffer. This can
  741. * be for example physical address, pointer to scatter list or IOMMU mapping.
  742. */
  743. void *vb2_plane_cookie(struct vb2_buffer *vb, unsigned int plane_no)
  744. {
  745. struct vb2_queue *q = vb->vb2_queue;
  746. if (plane_no > vb->num_planes || !vb->planes[plane_no].mem_priv)
  747. return NULL;
  748. return call_memop(q, cookie, vb->planes[plane_no].mem_priv);
  749. }
  750. EXPORT_SYMBOL_GPL(vb2_plane_cookie);
  751. /**
  752. * vb2_buffer_done() - inform videobuf that an operation on a buffer is finished
  753. * @vb: vb2_buffer returned from the driver
  754. * @state: either VB2_BUF_STATE_DONE if the operation finished successfully
  755. * or VB2_BUF_STATE_ERROR if the operation finished with an error
  756. *
  757. * This function should be called by the driver after a hardware operation on
  758. * a buffer is finished and the buffer may be returned to userspace. The driver
  759. * cannot use this buffer anymore until it is queued back to it by videobuf
  760. * by the means of buf_queue callback. Only buffers previously queued to the
  761. * driver by buf_queue can be passed to this function.
  762. */
  763. void vb2_buffer_done(struct vb2_buffer *vb, enum vb2_buffer_state state)
  764. {
  765. struct vb2_queue *q = vb->vb2_queue;
  766. unsigned long flags;
  767. unsigned int plane;
  768. if (vb->state != VB2_BUF_STATE_ACTIVE)
  769. return;
  770. if (state != VB2_BUF_STATE_DONE && state != VB2_BUF_STATE_ERROR)
  771. return;
  772. dprintk(4, "Done processing on buffer %d, state: %d\n",
  773. vb->v4l2_buf.index, state);
  774. /* sync buffers */
  775. for (plane = 0; plane < vb->num_planes; ++plane)
  776. call_memop(q, finish, vb->planes[plane].mem_priv);
  777. /* Add the buffer to the done buffers list */
  778. spin_lock_irqsave(&q->done_lock, flags);
  779. vb->state = state;
  780. list_add_tail(&vb->done_entry, &q->done_list);
  781. atomic_dec(&q->queued_count);
  782. spin_unlock_irqrestore(&q->done_lock, flags);
  783. /* Inform any processes that may be waiting for buffers */
  784. wake_up(&q->done_wq);
  785. }
  786. EXPORT_SYMBOL_GPL(vb2_buffer_done);
  787. /**
  788. * __fill_vb2_buffer() - fill a vb2_buffer with information provided in a
  789. * v4l2_buffer by the userspace. The caller has already verified that struct
  790. * v4l2_buffer has a valid number of planes.
  791. */
  792. static void __fill_vb2_buffer(struct vb2_buffer *vb, const struct v4l2_buffer *b,
  793. struct v4l2_plane *v4l2_planes)
  794. {
  795. unsigned int plane;
  796. if (V4L2_TYPE_IS_MULTIPLANAR(b->type)) {
  797. /* Fill in driver-provided information for OUTPUT types */
  798. if (V4L2_TYPE_IS_OUTPUT(b->type)) {
  799. /*
  800. * Will have to go up to b->length when API starts
  801. * accepting variable number of planes.
  802. */
  803. for (plane = 0; plane < vb->num_planes; ++plane) {
  804. v4l2_planes[plane].bytesused =
  805. b->m.planes[plane].bytesused;
  806. v4l2_planes[plane].data_offset =
  807. b->m.planes[plane].data_offset;
  808. }
  809. }
  810. if (b->memory == V4L2_MEMORY_USERPTR) {
  811. for (plane = 0; plane < vb->num_planes; ++plane) {
  812. v4l2_planes[plane].m.userptr =
  813. b->m.planes[plane].m.userptr;
  814. v4l2_planes[plane].length =
  815. b->m.planes[plane].length;
  816. }
  817. }
  818. if (b->memory == V4L2_MEMORY_DMABUF) {
  819. for (plane = 0; plane < vb->num_planes; ++plane) {
  820. v4l2_planes[plane].m.fd =
  821. b->m.planes[plane].m.fd;
  822. v4l2_planes[plane].length =
  823. b->m.planes[plane].length;
  824. v4l2_planes[plane].data_offset =
  825. b->m.planes[plane].data_offset;
  826. }
  827. }
  828. } else {
  829. /*
  830. * Single-planar buffers do not use planes array,
  831. * so fill in relevant v4l2_buffer struct fields instead.
  832. * In videobuf we use our internal V4l2_planes struct for
  833. * single-planar buffers as well, for simplicity.
  834. */
  835. if (V4L2_TYPE_IS_OUTPUT(b->type)) {
  836. v4l2_planes[0].bytesused = b->bytesused;
  837. v4l2_planes[0].data_offset = 0;
  838. }
  839. if (b->memory == V4L2_MEMORY_USERPTR) {
  840. v4l2_planes[0].m.userptr = b->m.userptr;
  841. v4l2_planes[0].length = b->length;
  842. }
  843. if (b->memory == V4L2_MEMORY_DMABUF) {
  844. v4l2_planes[0].m.fd = b->m.fd;
  845. v4l2_planes[0].length = b->length;
  846. v4l2_planes[0].data_offset = 0;
  847. }
  848. }
  849. vb->v4l2_buf.field = b->field;
  850. vb->v4l2_buf.timestamp = b->timestamp;
  851. vb->v4l2_buf.flags = b->flags & ~V4L2_BUFFER_MASK_FLAGS;
  852. }
  853. /**
  854. * __qbuf_userptr() - handle qbuf of a USERPTR buffer
  855. */
  856. static int __qbuf_userptr(struct vb2_buffer *vb, const struct v4l2_buffer *b)
  857. {
  858. struct v4l2_plane planes[VIDEO_MAX_PLANES];
  859. struct vb2_queue *q = vb->vb2_queue;
  860. void *mem_priv;
  861. unsigned int plane;
  862. int ret;
  863. int write = !V4L2_TYPE_IS_OUTPUT(q->type);
  864. /* Copy relevant information provided by the userspace */
  865. __fill_vb2_buffer(vb, b, planes);
  866. for (plane = 0; plane < vb->num_planes; ++plane) {
  867. /* Skip the plane if already verified */
  868. if (vb->v4l2_planes[plane].m.userptr &&
  869. vb->v4l2_planes[plane].m.userptr == planes[plane].m.userptr
  870. && vb->v4l2_planes[plane].length == planes[plane].length)
  871. continue;
  872. dprintk(3, "qbuf: userspace address for plane %d changed, "
  873. "reacquiring memory\n", plane);
  874. /* Check if the provided plane buffer is large enough */
  875. if (planes[plane].length < q->plane_sizes[plane]) {
  876. ret = -EINVAL;
  877. goto err;
  878. }
  879. /* Release previously acquired memory if present */
  880. if (vb->planes[plane].mem_priv)
  881. call_memop(q, put_userptr, vb->planes[plane].mem_priv);
  882. vb->planes[plane].mem_priv = NULL;
  883. vb->v4l2_planes[plane].m.userptr = 0;
  884. vb->v4l2_planes[plane].length = 0;
  885. /* Acquire each plane's memory */
  886. mem_priv = call_memop(q, get_userptr, q->alloc_ctx[plane],
  887. planes[plane].m.userptr,
  888. planes[plane].length, write);
  889. if (IS_ERR_OR_NULL(mem_priv)) {
  890. dprintk(1, "qbuf: failed acquiring userspace "
  891. "memory for plane %d\n", plane);
  892. ret = mem_priv ? PTR_ERR(mem_priv) : -EINVAL;
  893. goto err;
  894. }
  895. vb->planes[plane].mem_priv = mem_priv;
  896. }
  897. /*
  898. * Call driver-specific initialization on the newly acquired buffer,
  899. * if provided.
  900. */
  901. ret = call_qop(q, buf_init, vb);
  902. if (ret) {
  903. dprintk(1, "qbuf: buffer initialization failed\n");
  904. goto err;
  905. }
  906. /*
  907. * Now that everything is in order, copy relevant information
  908. * provided by userspace.
  909. */
  910. for (plane = 0; plane < vb->num_planes; ++plane)
  911. vb->v4l2_planes[plane] = planes[plane];
  912. return 0;
  913. err:
  914. /* In case of errors, release planes that were already acquired */
  915. for (plane = 0; plane < vb->num_planes; ++plane) {
  916. if (vb->planes[plane].mem_priv)
  917. call_memop(q, put_userptr, vb->planes[plane].mem_priv);
  918. vb->planes[plane].mem_priv = NULL;
  919. vb->v4l2_planes[plane].m.userptr = 0;
  920. vb->v4l2_planes[plane].length = 0;
  921. }
  922. return ret;
  923. }
  924. /**
  925. * __qbuf_mmap() - handle qbuf of an MMAP buffer
  926. */
  927. static int __qbuf_mmap(struct vb2_buffer *vb, const struct v4l2_buffer *b)
  928. {
  929. __fill_vb2_buffer(vb, b, vb->v4l2_planes);
  930. return 0;
  931. }
  932. /**
  933. * __qbuf_dmabuf() - handle qbuf of a DMABUF buffer
  934. */
  935. static int __qbuf_dmabuf(struct vb2_buffer *vb, const struct v4l2_buffer *b)
  936. {
  937. struct v4l2_plane planes[VIDEO_MAX_PLANES];
  938. struct vb2_queue *q = vb->vb2_queue;
  939. void *mem_priv;
  940. unsigned int plane;
  941. int ret;
  942. int write = !V4L2_TYPE_IS_OUTPUT(q->type);
  943. /* Verify and copy relevant information provided by the userspace */
  944. __fill_vb2_buffer(vb, b, planes);
  945. for (plane = 0; plane < vb->num_planes; ++plane) {
  946. struct dma_buf *dbuf = dma_buf_get(planes[plane].m.fd);
  947. if (IS_ERR_OR_NULL(dbuf)) {
  948. dprintk(1, "qbuf: invalid dmabuf fd for plane %d\n",
  949. plane);
  950. ret = -EINVAL;
  951. goto err;
  952. }
  953. /* use DMABUF size if length is not provided */
  954. if (planes[plane].length == 0)
  955. planes[plane].length = dbuf->size;
  956. if (planes[plane].length < planes[plane].data_offset +
  957. q->plane_sizes[plane]) {
  958. ret = -EINVAL;
  959. goto err;
  960. }
  961. /* Skip the plane if already verified */
  962. if (dbuf == vb->planes[plane].dbuf &&
  963. vb->v4l2_planes[plane].length == planes[plane].length) {
  964. dma_buf_put(dbuf);
  965. continue;
  966. }
  967. dprintk(1, "qbuf: buffer for plane %d changed\n", plane);
  968. /* Release previously acquired memory if present */
  969. __vb2_plane_dmabuf_put(q, &vb->planes[plane]);
  970. memset(&vb->v4l2_planes[plane], 0, sizeof(struct v4l2_plane));
  971. /* Acquire each plane's memory */
  972. mem_priv = call_memop(q, attach_dmabuf, q->alloc_ctx[plane],
  973. dbuf, planes[plane].length, write);
  974. if (IS_ERR(mem_priv)) {
  975. dprintk(1, "qbuf: failed to attach dmabuf\n");
  976. ret = PTR_ERR(mem_priv);
  977. dma_buf_put(dbuf);
  978. goto err;
  979. }
  980. vb->planes[plane].dbuf = dbuf;
  981. vb->planes[plane].mem_priv = mem_priv;
  982. }
  983. /* TODO: This pins the buffer(s) with dma_buf_map_attachment()).. but
  984. * really we want to do this just before the DMA, not while queueing
  985. * the buffer(s)..
  986. */
  987. for (plane = 0; plane < vb->num_planes; ++plane) {
  988. ret = call_memop(q, map_dmabuf, vb->planes[plane].mem_priv);
  989. if (ret) {
  990. dprintk(1, "qbuf: failed to map dmabuf for plane %d\n",
  991. plane);
  992. goto err;
  993. }
  994. vb->planes[plane].dbuf_mapped = 1;
  995. }
  996. /*
  997. * Call driver-specific initialization on the newly acquired buffer,
  998. * if provided.
  999. */
  1000. ret = call_qop(q, buf_init, vb);
  1001. if (ret) {
  1002. dprintk(1, "qbuf: buffer initialization failed\n");
  1003. goto err;
  1004. }
  1005. /*
  1006. * Now that everything is in order, copy relevant information
  1007. * provided by userspace.
  1008. */
  1009. for (plane = 0; plane < vb->num_planes; ++plane)
  1010. vb->v4l2_planes[plane] = planes[plane];
  1011. return 0;
  1012. err:
  1013. /* In case of errors, release planes that were already acquired */
  1014. __vb2_buf_dmabuf_put(vb);
  1015. return ret;
  1016. }
  1017. /**
  1018. * __enqueue_in_driver() - enqueue a vb2_buffer in driver for processing
  1019. */
  1020. static void __enqueue_in_driver(struct vb2_buffer *vb)
  1021. {
  1022. struct vb2_queue *q = vb->vb2_queue;
  1023. unsigned int plane;
  1024. vb->state = VB2_BUF_STATE_ACTIVE;
  1025. atomic_inc(&q->queued_count);
  1026. /* sync buffers */
  1027. for (plane = 0; plane < vb->num_planes; ++plane)
  1028. call_memop(q, prepare, vb->planes[plane].mem_priv);
  1029. q->ops->buf_queue(vb);
  1030. }
  1031. static int __buf_prepare(struct vb2_buffer *vb, const struct v4l2_buffer *b)
  1032. {
  1033. struct vb2_queue *q = vb->vb2_queue;
  1034. int ret;
  1035. ret = __verify_length(vb, b);
  1036. if (ret < 0)
  1037. return ret;
  1038. switch (q->memory) {
  1039. case V4L2_MEMORY_MMAP:
  1040. ret = __qbuf_mmap(vb, b);
  1041. break;
  1042. case V4L2_MEMORY_USERPTR:
  1043. ret = __qbuf_userptr(vb, b);
  1044. break;
  1045. case V4L2_MEMORY_DMABUF:
  1046. ret = __qbuf_dmabuf(vb, b);
  1047. break;
  1048. default:
  1049. WARN(1, "Invalid queue type\n");
  1050. ret = -EINVAL;
  1051. }
  1052. if (!ret)
  1053. ret = call_qop(q, buf_prepare, vb);
  1054. if (ret)
  1055. dprintk(1, "qbuf: buffer preparation failed: %d\n", ret);
  1056. else
  1057. vb->state = VB2_BUF_STATE_PREPARED;
  1058. return ret;
  1059. }
  1060. /**
  1061. * vb2_prepare_buf() - Pass ownership of a buffer from userspace to the kernel
  1062. * @q: videobuf2 queue
  1063. * @b: buffer structure passed from userspace to vidioc_prepare_buf
  1064. * handler in driver
  1065. *
  1066. * Should be called from vidioc_prepare_buf ioctl handler of a driver.
  1067. * This function:
  1068. * 1) verifies the passed buffer,
  1069. * 2) calls buf_prepare callback in the driver (if provided), in which
  1070. * driver-specific buffer initialization can be performed,
  1071. *
  1072. * The return values from this function are intended to be directly returned
  1073. * from vidioc_prepare_buf handler in driver.
  1074. */
  1075. int vb2_prepare_buf(struct vb2_queue *q, struct v4l2_buffer *b)
  1076. {
  1077. struct vb2_buffer *vb;
  1078. int ret;
  1079. if (q->fileio) {
  1080. dprintk(1, "%s(): file io in progress\n", __func__);
  1081. return -EBUSY;
  1082. }
  1083. if (b->type != q->type) {
  1084. dprintk(1, "%s(): invalid buffer type\n", __func__);
  1085. return -EINVAL;
  1086. }
  1087. if (b->index >= q->num_buffers) {
  1088. dprintk(1, "%s(): buffer index out of range\n", __func__);
  1089. return -EINVAL;
  1090. }
  1091. vb = q->bufs[b->index];
  1092. if (NULL == vb) {
  1093. /* Should never happen */
  1094. dprintk(1, "%s(): buffer is NULL\n", __func__);
  1095. return -EINVAL;
  1096. }
  1097. if (b->memory != q->memory) {
  1098. dprintk(1, "%s(): invalid memory type\n", __func__);
  1099. return -EINVAL;
  1100. }
  1101. if (vb->state != VB2_BUF_STATE_DEQUEUED) {
  1102. dprintk(1, "%s(): invalid buffer state %d\n", __func__, vb->state);
  1103. return -EINVAL;
  1104. }
  1105. ret = __verify_planes_array(vb, b);
  1106. if (ret < 0)
  1107. return ret;
  1108. ret = __buf_prepare(vb, b);
  1109. if (ret < 0)
  1110. return ret;
  1111. __fill_v4l2_buffer(vb, b);
  1112. return 0;
  1113. }
  1114. EXPORT_SYMBOL_GPL(vb2_prepare_buf);
  1115. /**
  1116. * vb2_qbuf() - Queue a buffer from userspace
  1117. * @q: videobuf2 queue
  1118. * @b: buffer structure passed from userspace to vidioc_qbuf handler
  1119. * in driver
  1120. *
  1121. * Should be called from vidioc_qbuf ioctl handler of a driver.
  1122. * This function:
  1123. * 1) verifies the passed buffer,
  1124. * 2) if necessary, calls buf_prepare callback in the driver (if provided), in
  1125. * which driver-specific buffer initialization can be performed,
  1126. * 3) if streaming is on, queues the buffer in driver by the means of buf_queue
  1127. * callback for processing.
  1128. *
  1129. * The return values from this function are intended to be directly returned
  1130. * from vidioc_qbuf handler in driver.
  1131. */
  1132. int vb2_qbuf(struct vb2_queue *q, struct v4l2_buffer *b)
  1133. {
  1134. struct rw_semaphore *mmap_sem = NULL;
  1135. struct vb2_buffer *vb;
  1136. int ret = 0;
  1137. /*
  1138. * In case of user pointer buffers vb2 allocator needs to get direct
  1139. * access to userspace pages. This requires getting read access on
  1140. * mmap semaphore in the current process structure. The same
  1141. * semaphore is taken before calling mmap operation, while both mmap
  1142. * and qbuf are called by the driver or v4l2 core with driver's lock
  1143. * held. To avoid a AB-BA deadlock (mmap_sem then driver's lock in
  1144. * mmap and driver's lock then mmap_sem in qbuf) the videobuf2 core
  1145. * release driver's lock, takes mmap_sem and then takes again driver's
  1146. * lock.
  1147. *
  1148. * To avoid race with other vb2 calls, which might be called after
  1149. * releasing driver's lock, this operation is performed at the
  1150. * beggining of qbuf processing. This way the queue status is
  1151. * consistent after getting driver's lock back.
  1152. */
  1153. if (q->memory == V4L2_MEMORY_USERPTR) {
  1154. mmap_sem = &current->mm->mmap_sem;
  1155. call_qop(q, wait_prepare, q);
  1156. down_read(mmap_sem);
  1157. call_qop(q, wait_finish, q);
  1158. }
  1159. if (q->fileio) {
  1160. dprintk(1, "qbuf: file io in progress\n");
  1161. ret = -EBUSY;
  1162. goto unlock;
  1163. }
  1164. if (b->type != q->type) {
  1165. dprintk(1, "qbuf: invalid buffer type\n");
  1166. ret = -EINVAL;
  1167. goto unlock;
  1168. }
  1169. if (b->index >= q->num_buffers) {
  1170. dprintk(1, "qbuf: buffer index out of range\n");
  1171. ret = -EINVAL;
  1172. goto unlock;
  1173. }
  1174. vb = q->bufs[b->index];
  1175. if (NULL == vb) {
  1176. /* Should never happen */
  1177. dprintk(1, "qbuf: buffer is NULL\n");
  1178. ret = -EINVAL;
  1179. goto unlock;
  1180. }
  1181. if (b->memory != q->memory) {
  1182. dprintk(1, "qbuf: invalid memory type\n");
  1183. ret = -EINVAL;
  1184. goto unlock;
  1185. }
  1186. ret = __verify_planes_array(vb, b);
  1187. if (ret)
  1188. goto unlock;
  1189. switch (vb->state) {
  1190. case VB2_BUF_STATE_DEQUEUED:
  1191. ret = __buf_prepare(vb, b);
  1192. if (ret)
  1193. goto unlock;
  1194. case VB2_BUF_STATE_PREPARED:
  1195. break;
  1196. default:
  1197. dprintk(1, "qbuf: buffer already in use\n");
  1198. ret = -EINVAL;
  1199. goto unlock;
  1200. }
  1201. /*
  1202. * Add to the queued buffers list, a buffer will stay on it until
  1203. * dequeued in dqbuf.
  1204. */
  1205. list_add_tail(&vb->queued_entry, &q->queued_list);
  1206. vb->state = VB2_BUF_STATE_QUEUED;
  1207. /*
  1208. * If already streaming, give the buffer to driver for processing.
  1209. * If not, the buffer will be given to driver on next streamon.
  1210. */
  1211. if (q->streaming)
  1212. __enqueue_in_driver(vb);
  1213. /* Fill buffer information for the userspace */
  1214. __fill_v4l2_buffer(vb, b);
  1215. dprintk(1, "qbuf of buffer %d succeeded\n", vb->v4l2_buf.index);
  1216. unlock:
  1217. if (mmap_sem)
  1218. up_read(mmap_sem);
  1219. return ret;
  1220. }
  1221. EXPORT_SYMBOL_GPL(vb2_qbuf);
  1222. /**
  1223. * __vb2_wait_for_done_vb() - wait for a buffer to become available
  1224. * for dequeuing
  1225. *
  1226. * Will sleep if required for nonblocking == false.
  1227. */
  1228. static int __vb2_wait_for_done_vb(struct vb2_queue *q, int nonblocking)
  1229. {
  1230. /*
  1231. * All operations on vb_done_list are performed under done_lock
  1232. * spinlock protection. However, buffers may be removed from
  1233. * it and returned to userspace only while holding both driver's
  1234. * lock and the done_lock spinlock. Thus we can be sure that as
  1235. * long as we hold the driver's lock, the list will remain not
  1236. * empty if list_empty() check succeeds.
  1237. */
  1238. for (;;) {
  1239. int ret;
  1240. if (!q->streaming) {
  1241. dprintk(1, "Streaming off, will not wait for buffers\n");
  1242. return -EINVAL;
  1243. }
  1244. if (!list_empty(&q->done_list)) {
  1245. /*
  1246. * Found a buffer that we were waiting for.
  1247. */
  1248. break;
  1249. }
  1250. if (nonblocking) {
  1251. dprintk(1, "Nonblocking and no buffers to dequeue, "
  1252. "will not wait\n");
  1253. return -EAGAIN;
  1254. }
  1255. /*
  1256. * We are streaming and blocking, wait for another buffer to
  1257. * become ready or for streamoff. Driver's lock is released to
  1258. * allow streamoff or qbuf to be called while waiting.
  1259. */
  1260. call_qop(q, wait_prepare, q);
  1261. /*
  1262. * All locks have been released, it is safe to sleep now.
  1263. */
  1264. dprintk(3, "Will sleep waiting for buffers\n");
  1265. ret = wait_event_interruptible(q->done_wq,
  1266. !list_empty(&q->done_list) || !q->streaming);
  1267. /*
  1268. * We need to reevaluate both conditions again after reacquiring
  1269. * the locks or return an error if one occurred.
  1270. */
  1271. call_qop(q, wait_finish, q);
  1272. if (ret) {
  1273. dprintk(1, "Sleep was interrupted\n");
  1274. return ret;
  1275. }
  1276. }
  1277. return 0;
  1278. }
  1279. /**
  1280. * __vb2_get_done_vb() - get a buffer ready for dequeuing
  1281. *
  1282. * Will sleep if required for nonblocking == false.
  1283. */
  1284. static int __vb2_get_done_vb(struct vb2_queue *q, struct vb2_buffer **vb,
  1285. struct v4l2_buffer *b, int nonblocking)
  1286. {
  1287. unsigned long flags;
  1288. int ret;
  1289. /*
  1290. * Wait for at least one buffer to become available on the done_list.
  1291. */
  1292. ret = __vb2_wait_for_done_vb(q, nonblocking);
  1293. if (ret)
  1294. return ret;
  1295. /*
  1296. * Driver's lock has been held since we last verified that done_list
  1297. * is not empty, so no need for another list_empty(done_list) check.
  1298. */
  1299. spin_lock_irqsave(&q->done_lock, flags);
  1300. *vb = list_first_entry(&q->done_list, struct vb2_buffer, done_entry);
  1301. /*
  1302. * Only remove the buffer from done_list if v4l2_buffer can handle all
  1303. * the planes.
  1304. */
  1305. ret = __verify_planes_array(*vb, b);
  1306. if (!ret)
  1307. list_del(&(*vb)->done_entry);
  1308. spin_unlock_irqrestore(&q->done_lock, flags);
  1309. return ret;
  1310. }
  1311. /**
  1312. * vb2_wait_for_all_buffers() - wait until all buffers are given back to vb2
  1313. * @q: videobuf2 queue
  1314. *
  1315. * This function will wait until all buffers that have been given to the driver
  1316. * by buf_queue() are given back to vb2 with vb2_buffer_done(). It doesn't call
  1317. * wait_prepare, wait_finish pair. It is intended to be called with all locks
  1318. * taken, for example from stop_streaming() callback.
  1319. */
  1320. int vb2_wait_for_all_buffers(struct vb2_queue *q)
  1321. {
  1322. if (!q->streaming) {
  1323. dprintk(1, "Streaming off, will not wait for buffers\n");
  1324. return -EINVAL;
  1325. }
  1326. wait_event(q->done_wq, !atomic_read(&q->queued_count));
  1327. return 0;
  1328. }
  1329. EXPORT_SYMBOL_GPL(vb2_wait_for_all_buffers);
  1330. /**
  1331. * __vb2_dqbuf() - bring back the buffer to the DEQUEUED state
  1332. */
  1333. static void __vb2_dqbuf(struct vb2_buffer *vb)
  1334. {
  1335. struct vb2_queue *q = vb->vb2_queue;
  1336. unsigned int i;
  1337. /* nothing to do if the buffer is already dequeued */
  1338. if (vb->state == VB2_BUF_STATE_DEQUEUED)
  1339. return;
  1340. vb->state = VB2_BUF_STATE_DEQUEUED;
  1341. /* unmap DMABUF buffer */
  1342. if (q->memory == V4L2_MEMORY_DMABUF)
  1343. for (i = 0; i < vb->num_planes; ++i) {
  1344. if (!vb->planes[i].dbuf_mapped)
  1345. continue;
  1346. call_memop(q, unmap_dmabuf, vb->planes[i].mem_priv);
  1347. vb->planes[i].dbuf_mapped = 0;
  1348. }
  1349. }
  1350. /**
  1351. * vb2_dqbuf() - Dequeue a buffer to the userspace
  1352. * @q: videobuf2 queue
  1353. * @b: buffer structure passed from userspace to vidioc_dqbuf handler
  1354. * in driver
  1355. * @nonblocking: if true, this call will not sleep waiting for a buffer if no
  1356. * buffers ready for dequeuing are present. Normally the driver
  1357. * would be passing (file->f_flags & O_NONBLOCK) here
  1358. *
  1359. * Should be called from vidioc_dqbuf ioctl handler of a driver.
  1360. * This function:
  1361. * 1) verifies the passed buffer,
  1362. * 2) calls buf_finish callback in the driver (if provided), in which
  1363. * driver can perform any additional operations that may be required before
  1364. * returning the buffer to userspace, such as cache sync,
  1365. * 3) the buffer struct members are filled with relevant information for
  1366. * the userspace.
  1367. *
  1368. * The return values from this function are intended to be directly returned
  1369. * from vidioc_dqbuf handler in driver.
  1370. */
  1371. int vb2_dqbuf(struct vb2_queue *q, struct v4l2_buffer *b, bool nonblocking)
  1372. {
  1373. struct vb2_buffer *vb = NULL;
  1374. int ret;
  1375. if (q->fileio) {
  1376. dprintk(1, "dqbuf: file io in progress\n");
  1377. return -EBUSY;
  1378. }
  1379. if (b->type != q->type) {
  1380. dprintk(1, "dqbuf: invalid buffer type\n");
  1381. return -EINVAL;
  1382. }
  1383. ret = __vb2_get_done_vb(q, &vb, b, nonblocking);
  1384. if (ret < 0)
  1385. return ret;
  1386. ret = call_qop(q, buf_finish, vb);
  1387. if (ret) {
  1388. dprintk(1, "dqbuf: buffer finish failed\n");
  1389. return ret;
  1390. }
  1391. switch (vb->state) {
  1392. case VB2_BUF_STATE_DONE:
  1393. dprintk(3, "dqbuf: Returning done buffer\n");
  1394. break;
  1395. case VB2_BUF_STATE_ERROR:
  1396. dprintk(3, "dqbuf: Returning done buffer with errors\n");
  1397. break;
  1398. default:
  1399. dprintk(1, "dqbuf: Invalid buffer state\n");
  1400. return -EINVAL;
  1401. }
  1402. /* Fill buffer information for the userspace */
  1403. __fill_v4l2_buffer(vb, b);
  1404. /* Remove from videobuf queue */
  1405. list_del(&vb->queued_entry);
  1406. /* go back to dequeued state */
  1407. __vb2_dqbuf(vb);
  1408. dprintk(1, "dqbuf of buffer %d, with state %d\n",
  1409. vb->v4l2_buf.index, vb->state);
  1410. return 0;
  1411. }
  1412. EXPORT_SYMBOL_GPL(vb2_dqbuf);
  1413. /**
  1414. * __vb2_queue_cancel() - cancel and stop (pause) streaming
  1415. *
  1416. * Removes all queued buffers from driver's queue and all buffers queued by
  1417. * userspace from videobuf's queue. Returns to state after reqbufs.
  1418. */
  1419. static void __vb2_queue_cancel(struct vb2_queue *q)
  1420. {
  1421. unsigned int i;
  1422. /*
  1423. * Tell driver to stop all transactions and release all queued
  1424. * buffers.
  1425. */
  1426. if (q->streaming)
  1427. call_qop(q, stop_streaming, q);
  1428. q->streaming = 0;
  1429. /*
  1430. * Remove all buffers from videobuf's list...
  1431. */
  1432. INIT_LIST_HEAD(&q->queued_list);
  1433. /*
  1434. * ...and done list; userspace will not receive any buffers it
  1435. * has not already dequeued before initiating cancel.
  1436. */
  1437. INIT_LIST_HEAD(&q->done_list);
  1438. atomic_set(&q->queued_count, 0);
  1439. wake_up_all(&q->done_wq);
  1440. /*
  1441. * Reinitialize all buffers for next use.
  1442. */
  1443. for (i = 0; i < q->num_buffers; ++i)
  1444. __vb2_dqbuf(q->bufs[i]);
  1445. }
  1446. /**
  1447. * vb2_streamon - start streaming
  1448. * @q: videobuf2 queue
  1449. * @type: type argument passed from userspace to vidioc_streamon handler
  1450. *
  1451. * Should be called from vidioc_streamon handler of a driver.
  1452. * This function:
  1453. * 1) verifies current state
  1454. * 2) passes any previously queued buffers to the driver and starts streaming
  1455. *
  1456. * The return values from this function are intended to be directly returned
  1457. * from vidioc_streamon handler in the driver.
  1458. */
  1459. int vb2_streamon(struct vb2_queue *q, enum v4l2_buf_type type)
  1460. {
  1461. struct vb2_buffer *vb;
  1462. int ret;
  1463. if (q->fileio) {
  1464. dprintk(1, "streamon: file io in progress\n");
  1465. return -EBUSY;
  1466. }
  1467. if (type != q->type) {
  1468. dprintk(1, "streamon: invalid stream type\n");
  1469. return -EINVAL;
  1470. }
  1471. if (q->streaming) {
  1472. dprintk(1, "streamon: already streaming\n");
  1473. return -EBUSY;
  1474. }
  1475. /*
  1476. * If any buffers were queued before streamon,
  1477. * we can now pass them to driver for processing.
  1478. */
  1479. list_for_each_entry(vb, &q->queued_list, queued_entry)
  1480. __enqueue_in_driver(vb);
  1481. /*
  1482. * Let driver notice that streaming state has been enabled.
  1483. */
  1484. ret = call_qop(q, start_streaming, q, atomic_read(&q->queued_count));
  1485. if (ret) {
  1486. dprintk(1, "streamon: driver refused to start streaming\n");
  1487. __vb2_queue_cancel(q);
  1488. return ret;
  1489. }
  1490. q->streaming = 1;
  1491. dprintk(3, "Streamon successful\n");
  1492. return 0;
  1493. }
  1494. EXPORT_SYMBOL_GPL(vb2_streamon);
  1495. /**
  1496. * vb2_streamoff - stop streaming
  1497. * @q: videobuf2 queue
  1498. * @type: type argument passed from userspace to vidioc_streamoff handler
  1499. *
  1500. * Should be called from vidioc_streamoff handler of a driver.
  1501. * This function:
  1502. * 1) verifies current state,
  1503. * 2) stop streaming and dequeues any queued buffers, including those previously
  1504. * passed to the driver (after waiting for the driver to finish).
  1505. *
  1506. * This call can be used for pausing playback.
  1507. * The return values from this function are intended to be directly returned
  1508. * from vidioc_streamoff handler in the driver
  1509. */
  1510. int vb2_streamoff(struct vb2_queue *q, enum v4l2_buf_type type)
  1511. {
  1512. if (q->fileio) {
  1513. dprintk(1, "streamoff: file io in progress\n");
  1514. return -EBUSY;
  1515. }
  1516. if (type != q->type) {
  1517. dprintk(1, "streamoff: invalid stream type\n");
  1518. return -EINVAL;
  1519. }
  1520. if (!q->streaming) {
  1521. dprintk(1, "streamoff: not streaming\n");
  1522. return -EINVAL;
  1523. }
  1524. /*
  1525. * Cancel will pause streaming and remove all buffers from the driver
  1526. * and videobuf, effectively returning control over them to userspace.
  1527. */
  1528. __vb2_queue_cancel(q);
  1529. dprintk(3, "Streamoff successful\n");
  1530. return 0;
  1531. }
  1532. EXPORT_SYMBOL_GPL(vb2_streamoff);
  1533. /**
  1534. * __find_plane_by_offset() - find plane associated with the given offset off
  1535. */
  1536. static int __find_plane_by_offset(struct vb2_queue *q, unsigned long off,
  1537. unsigned int *_buffer, unsigned int *_plane)
  1538. {
  1539. struct vb2_buffer *vb;
  1540. unsigned int buffer, plane;
  1541. /*
  1542. * Go over all buffers and their planes, comparing the given offset
  1543. * with an offset assigned to each plane. If a match is found,
  1544. * return its buffer and plane numbers.
  1545. */
  1546. for (buffer = 0; buffer < q->num_buffers; ++buffer) {
  1547. vb = q->bufs[buffer];
  1548. for (plane = 0; plane < vb->num_planes; ++plane) {
  1549. if (vb->v4l2_planes[plane].m.mem_offset == off) {
  1550. *_buffer = buffer;
  1551. *_plane = plane;
  1552. return 0;
  1553. }
  1554. }
  1555. }
  1556. return -EINVAL;
  1557. }
  1558. /**
  1559. * vb2_expbuf() - Export a buffer as a file descriptor
  1560. * @q: videobuf2 queue
  1561. * @eb: export buffer structure passed from userspace to vidioc_expbuf
  1562. * handler in driver
  1563. *
  1564. * The return values from this function are intended to be directly returned
  1565. * from vidioc_expbuf handler in driver.
  1566. */
  1567. int vb2_expbuf(struct vb2_queue *q, struct v4l2_exportbuffer *eb)
  1568. {
  1569. struct vb2_buffer *vb = NULL;
  1570. struct vb2_plane *vb_plane;
  1571. int ret;
  1572. struct dma_buf *dbuf;
  1573. if (q->memory != V4L2_MEMORY_MMAP) {
  1574. dprintk(1, "Queue is not currently set up for mmap\n");
  1575. return -EINVAL;
  1576. }
  1577. if (!q->mem_ops->get_dmabuf) {
  1578. dprintk(1, "Queue does not support DMA buffer exporting\n");
  1579. return -EINVAL;
  1580. }
  1581. if (eb->flags & ~O_CLOEXEC) {
  1582. dprintk(1, "Queue does support only O_CLOEXEC flag\n");
  1583. return -EINVAL;
  1584. }
  1585. if (eb->type != q->type) {
  1586. dprintk(1, "qbuf: invalid buffer type\n");
  1587. return -EINVAL;
  1588. }
  1589. if (eb->index >= q->num_buffers) {
  1590. dprintk(1, "buffer index out of range\n");
  1591. return -EINVAL;
  1592. }
  1593. vb = q->bufs[eb->index];
  1594. if (eb->plane >= vb->num_planes) {
  1595. dprintk(1, "buffer plane out of range\n");
  1596. return -EINVAL;
  1597. }
  1598. vb_plane = &vb->planes[eb->plane];
  1599. dbuf = call_memop(q, get_dmabuf, vb_plane->mem_priv);
  1600. if (IS_ERR_OR_NULL(dbuf)) {
  1601. dprintk(1, "Failed to export buffer %d, plane %d\n",
  1602. eb->index, eb->plane);
  1603. return -EINVAL;
  1604. }
  1605. ret = dma_buf_fd(dbuf, eb->flags);
  1606. if (ret < 0) {
  1607. dprintk(3, "buffer %d, plane %d failed to export (%d)\n",
  1608. eb->index, eb->plane, ret);
  1609. dma_buf_put(dbuf);
  1610. return ret;
  1611. }
  1612. dprintk(3, "buffer %d, plane %d exported as %d descriptor\n",
  1613. eb->index, eb->plane, ret);
  1614. eb->fd = ret;
  1615. return 0;
  1616. }
  1617. EXPORT_SYMBOL_GPL(vb2_expbuf);
  1618. /**
  1619. * vb2_mmap() - map video buffers into application address space
  1620. * @q: videobuf2 queue
  1621. * @vma: vma passed to the mmap file operation handler in the driver
  1622. *
  1623. * Should be called from mmap file operation handler of a driver.
  1624. * This function maps one plane of one of the available video buffers to
  1625. * userspace. To map whole video memory allocated on reqbufs, this function
  1626. * has to be called once per each plane per each buffer previously allocated.
  1627. *
  1628. * When the userspace application calls mmap, it passes to it an offset returned
  1629. * to it earlier by the means of vidioc_querybuf handler. That offset acts as
  1630. * a "cookie", which is then used to identify the plane to be mapped.
  1631. * This function finds a plane with a matching offset and a mapping is performed
  1632. * by the means of a provided memory operation.
  1633. *
  1634. * The return values from this function are intended to be directly returned
  1635. * from the mmap handler in driver.
  1636. */
  1637. int vb2_mmap(struct vb2_queue *q, struct vm_area_struct *vma)
  1638. {
  1639. unsigned long off = vma->vm_pgoff << PAGE_SHIFT;
  1640. struct vb2_buffer *vb;
  1641. unsigned int buffer, plane;
  1642. int ret;
  1643. unsigned long length;
  1644. if (q->memory != V4L2_MEMORY_MMAP) {
  1645. dprintk(1, "Queue is not currently set up for mmap\n");
  1646. return -EINVAL;
  1647. }
  1648. /*
  1649. * Check memory area access mode.
  1650. */
  1651. if (!(vma->vm_flags & VM_SHARED)) {
  1652. dprintk(1, "Invalid vma flags, VM_SHARED needed\n");
  1653. return -EINVAL;
  1654. }
  1655. if (V4L2_TYPE_IS_OUTPUT(q->type)) {
  1656. if (!(vma->vm_flags & VM_WRITE)) {
  1657. dprintk(1, "Invalid vma flags, VM_WRITE needed\n");
  1658. return -EINVAL;
  1659. }
  1660. } else {
  1661. if (!(vma->vm_flags & VM_READ)) {
  1662. dprintk(1, "Invalid vma flags, VM_READ needed\n");
  1663. return -EINVAL;
  1664. }
  1665. }
  1666. /*
  1667. * Find the plane corresponding to the offset passed by userspace.
  1668. */
  1669. ret = __find_plane_by_offset(q, off, &buffer, &plane);
  1670. if (ret)
  1671. return ret;
  1672. vb = q->bufs[buffer];
  1673. /*
  1674. * MMAP requires page_aligned buffers.
  1675. * The buffer length was page_aligned at __vb2_buf_mem_alloc(),
  1676. * so, we need to do the same here.
  1677. */
  1678. length = PAGE_ALIGN(vb->v4l2_planes[plane].length);
  1679. if (length < (vma->vm_end - vma->vm_start)) {
  1680. dprintk(1,
  1681. "MMAP invalid, as it would overflow buffer length\n");
  1682. return -EINVAL;
  1683. }
  1684. ret = call_memop(q, mmap, vb->planes[plane].mem_priv, vma);
  1685. if (ret)
  1686. return ret;
  1687. dprintk(3, "Buffer %d, plane %d successfully mapped\n", buffer, plane);
  1688. return 0;
  1689. }
  1690. EXPORT_SYMBOL_GPL(vb2_mmap);
  1691. #ifndef CONFIG_MMU
  1692. unsigned long vb2_get_unmapped_area(struct vb2_queue *q,
  1693. unsigned long addr,
  1694. unsigned long len,
  1695. unsigned long pgoff,
  1696. unsigned long flags)
  1697. {
  1698. unsigned long off = pgoff << PAGE_SHIFT;
  1699. struct vb2_buffer *vb;
  1700. unsigned int buffer, plane;
  1701. int ret;
  1702. if (q->memory != V4L2_MEMORY_MMAP) {
  1703. dprintk(1, "Queue is not currently set up for mmap\n");
  1704. return -EINVAL;
  1705. }
  1706. /*
  1707. * Find the plane corresponding to the offset passed by userspace.
  1708. */
  1709. ret = __find_plane_by_offset(q, off, &buffer, &plane);
  1710. if (ret)
  1711. return ret;
  1712. vb = q->bufs[buffer];
  1713. return (unsigned long)vb2_plane_vaddr(vb, plane);
  1714. }
  1715. EXPORT_SYMBOL_GPL(vb2_get_unmapped_area);
  1716. #endif
  1717. static int __vb2_init_fileio(struct vb2_queue *q, int read);
  1718. static int __vb2_cleanup_fileio(struct vb2_queue *q);
  1719. /**
  1720. * vb2_poll() - implements poll userspace operation
  1721. * @q: videobuf2 queue
  1722. * @file: file argument passed to the poll file operation handler
  1723. * @wait: wait argument passed to the poll file operation handler
  1724. *
  1725. * This function implements poll file operation handler for a driver.
  1726. * For CAPTURE queues, if a buffer is ready to be dequeued, the userspace will
  1727. * be informed that the file descriptor of a video device is available for
  1728. * reading.
  1729. * For OUTPUT queues, if a buffer is ready to be dequeued, the file descriptor
  1730. * will be reported as available for writing.
  1731. *
  1732. * If the driver uses struct v4l2_fh, then vb2_poll() will also check for any
  1733. * pending events.
  1734. *
  1735. * The return values from this function are intended to be directly returned
  1736. * from poll handler in driver.
  1737. */
  1738. unsigned int vb2_poll(struct vb2_queue *q, struct file *file, poll_table *wait)
  1739. {
  1740. struct video_device *vfd = video_devdata(file);
  1741. unsigned long req_events = poll_requested_events(wait);
  1742. struct vb2_buffer *vb = NULL;
  1743. unsigned int res = 0;
  1744. unsigned long flags;
  1745. if (test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags)) {
  1746. struct v4l2_fh *fh = file->private_data;
  1747. if (v4l2_event_pending(fh))
  1748. res = POLLPRI;
  1749. else if (req_events & POLLPRI)
  1750. poll_wait(file, &fh->wait, wait);
  1751. }
  1752. if (!V4L2_TYPE_IS_OUTPUT(q->type) && !(req_events & (POLLIN | POLLRDNORM)))
  1753. return res;
  1754. if (V4L2_TYPE_IS_OUTPUT(q->type) && !(req_events & (POLLOUT | POLLWRNORM)))
  1755. return res;
  1756. /*
  1757. * Start file I/O emulator only if streaming API has not been used yet.
  1758. */
  1759. if (q->num_buffers == 0 && q->fileio == NULL) {
  1760. if (!V4L2_TYPE_IS_OUTPUT(q->type) && (q->io_modes & VB2_READ) &&
  1761. (req_events & (POLLIN | POLLRDNORM))) {
  1762. if (__vb2_init_fileio(q, 1))
  1763. return res | POLLERR;
  1764. }
  1765. if (V4L2_TYPE_IS_OUTPUT(q->type) && (q->io_modes & VB2_WRITE) &&
  1766. (req_events & (POLLOUT | POLLWRNORM))) {
  1767. if (__vb2_init_fileio(q, 0))
  1768. return res | POLLERR;
  1769. /*
  1770. * Write to OUTPUT queue can be done immediately.
  1771. */
  1772. return res | POLLOUT | POLLWRNORM;
  1773. }
  1774. }
  1775. /*
  1776. * There is nothing to wait for if no buffers have already been queued.
  1777. */
  1778. if (list_empty(&q->queued_list))
  1779. return res | POLLERR;
  1780. if (list_empty(&q->done_list))
  1781. poll_wait(file, &q->done_wq, wait);
  1782. /*
  1783. * Take first buffer available for dequeuing.
  1784. */
  1785. spin_lock_irqsave(&q->done_lock, flags);
  1786. if (!list_empty(&q->done_list))
  1787. vb = list_first_entry(&q->done_list, struct vb2_buffer,
  1788. done_entry);
  1789. spin_unlock_irqrestore(&q->done_lock, flags);
  1790. if (vb && (vb->state == VB2_BUF_STATE_DONE
  1791. || vb->state == VB2_BUF_STATE_ERROR)) {
  1792. return (V4L2_TYPE_IS_OUTPUT(q->type)) ?
  1793. res | POLLOUT | POLLWRNORM :
  1794. res | POLLIN | POLLRDNORM;
  1795. }
  1796. return res;
  1797. }
  1798. EXPORT_SYMBOL_GPL(vb2_poll);
  1799. /**
  1800. * vb2_queue_init() - initialize a videobuf2 queue
  1801. * @q: videobuf2 queue; this structure should be allocated in driver
  1802. *
  1803. * The vb2_queue structure should be allocated by the driver. The driver is
  1804. * responsible of clearing it's content and setting initial values for some
  1805. * required entries before calling this function.
  1806. * q->ops, q->mem_ops, q->type and q->io_modes are mandatory. Please refer
  1807. * to the struct vb2_queue description in include/media/videobuf2-core.h
  1808. * for more information.
  1809. */
  1810. int vb2_queue_init(struct vb2_queue *q)
  1811. {
  1812. /*
  1813. * Sanity check
  1814. */
  1815. if (WARN_ON(!q) ||
  1816. WARN_ON(!q->ops) ||
  1817. WARN_ON(!q->mem_ops) ||
  1818. WARN_ON(!q->type) ||
  1819. WARN_ON(!q->io_modes) ||
  1820. WARN_ON(!q->ops->queue_setup) ||
  1821. WARN_ON(!q->ops->buf_queue) ||
  1822. WARN_ON(q->timestamp_type & ~V4L2_BUF_FLAG_TIMESTAMP_MASK))
  1823. return -EINVAL;
  1824. /* Warn that the driver should choose an appropriate timestamp type */
  1825. WARN_ON(q->timestamp_type == V4L2_BUF_FLAG_TIMESTAMP_UNKNOWN);
  1826. INIT_LIST_HEAD(&q->queued_list);
  1827. INIT_LIST_HEAD(&q->done_list);
  1828. spin_lock_init(&q->done_lock);
  1829. init_waitqueue_head(&q->done_wq);
  1830. if (q->buf_struct_size == 0)
  1831. q->buf_struct_size = sizeof(struct vb2_buffer);
  1832. return 0;
  1833. }
  1834. EXPORT_SYMBOL_GPL(vb2_queue_init);
  1835. /**
  1836. * vb2_queue_release() - stop streaming, release the queue and free memory
  1837. * @q: videobuf2 queue
  1838. *
  1839. * This function stops streaming and performs necessary clean ups, including
  1840. * freeing video buffer memory. The driver is responsible for freeing
  1841. * the vb2_queue structure itself.
  1842. */
  1843. void vb2_queue_release(struct vb2_queue *q)
  1844. {
  1845. __vb2_cleanup_fileio(q);
  1846. __vb2_queue_cancel(q);
  1847. __vb2_queue_free(q, q->num_buffers);
  1848. }
  1849. EXPORT_SYMBOL_GPL(vb2_queue_release);
  1850. /**
  1851. * struct vb2_fileio_buf - buffer context used by file io emulator
  1852. *
  1853. * vb2 provides a compatibility layer and emulator of file io (read and
  1854. * write) calls on top of streaming API. This structure is used for
  1855. * tracking context related to the buffers.
  1856. */
  1857. struct vb2_fileio_buf {
  1858. void *vaddr;
  1859. unsigned int size;
  1860. unsigned int pos;
  1861. unsigned int queued:1;
  1862. };
  1863. /**
  1864. * struct vb2_fileio_data - queue context used by file io emulator
  1865. *
  1866. * vb2 provides a compatibility layer and emulator of file io (read and
  1867. * write) calls on top of streaming API. For proper operation it required
  1868. * this structure to save the driver state between each call of the read
  1869. * or write function.
  1870. */
  1871. struct vb2_fileio_data {
  1872. struct v4l2_requestbuffers req;
  1873. struct v4l2_buffer b;
  1874. struct vb2_fileio_buf bufs[VIDEO_MAX_FRAME];
  1875. unsigned int index;
  1876. unsigned int q_count;
  1877. unsigned int dq_count;
  1878. unsigned int flags;
  1879. };
  1880. /**
  1881. * __vb2_init_fileio() - initialize file io emulator
  1882. * @q: videobuf2 queue
  1883. * @read: mode selector (1 means read, 0 means write)
  1884. */
  1885. static int __vb2_init_fileio(struct vb2_queue *q, int read)
  1886. {
  1887. struct vb2_fileio_data *fileio;
  1888. int i, ret;
  1889. unsigned int count = 0;
  1890. /*
  1891. * Sanity check
  1892. */
  1893. if ((read && !(q->io_modes & VB2_READ)) ||
  1894. (!read && !(q->io_modes & VB2_WRITE)))
  1895. BUG();
  1896. /*
  1897. * Check if device supports mapping buffers to kernel virtual space.
  1898. */
  1899. if (!q->mem_ops->vaddr)
  1900. return -EBUSY;
  1901. /*
  1902. * Check if streaming api has not been already activated.
  1903. */
  1904. if (q->streaming || q->num_buffers > 0)
  1905. return -EBUSY;
  1906. /*
  1907. * Start with count 1, driver can increase it in queue_setup()
  1908. */
  1909. count = 1;
  1910. dprintk(3, "setting up file io: mode %s, count %d, flags %08x\n",
  1911. (read) ? "read" : "write", count, q->io_flags);
  1912. fileio = kzalloc(sizeof(struct vb2_fileio_data), GFP_KERNEL);
  1913. if (fileio == NULL)
  1914. return -ENOMEM;
  1915. fileio->flags = q->io_flags;
  1916. /*
  1917. * Request buffers and use MMAP type to force driver
  1918. * to allocate buffers by itself.
  1919. */
  1920. fileio->req.count = count;
  1921. fileio->req.memory = V4L2_MEMORY_MMAP;
  1922. fileio->req.type = q->type;
  1923. ret = vb2_reqbufs(q, &fileio->req);
  1924. if (ret)
  1925. goto err_kfree;
  1926. /*
  1927. * Check if plane_count is correct
  1928. * (multiplane buffers are not supported).
  1929. */
  1930. if (q->bufs[0]->num_planes != 1) {
  1931. ret = -EBUSY;
  1932. goto err_reqbufs;
  1933. }
  1934. /*
  1935. * Get kernel address of each buffer.
  1936. */
  1937. for (i = 0; i < q->num_buffers; i++) {
  1938. fileio->bufs[i].vaddr = vb2_plane_vaddr(q->bufs[i], 0);
  1939. if (fileio->bufs[i].vaddr == NULL) {
  1940. ret = -EINVAL;
  1941. goto err_reqbufs;
  1942. }
  1943. fileio->bufs[i].size = vb2_plane_size(q->bufs[i], 0);
  1944. }
  1945. /*
  1946. * Read mode requires pre queuing of all buffers.
  1947. */
  1948. if (read) {
  1949. /*
  1950. * Queue all buffers.
  1951. */
  1952. for (i = 0; i < q->num_buffers; i++) {
  1953. struct v4l2_buffer *b = &fileio->b;
  1954. memset(b, 0, sizeof(*b));
  1955. b->type = q->type;
  1956. b->memory = q->memory;
  1957. b->index = i;
  1958. ret = vb2_qbuf(q, b);
  1959. if (ret)
  1960. goto err_reqbufs;
  1961. fileio->bufs[i].queued = 1;
  1962. }
  1963. /*
  1964. * Start streaming.
  1965. */
  1966. ret = vb2_streamon(q, q->type);
  1967. if (ret)
  1968. goto err_reqbufs;
  1969. }
  1970. q->fileio = fileio;
  1971. return ret;
  1972. err_reqbufs:
  1973. fileio->req.count = 0;
  1974. vb2_reqbufs(q, &fileio->req);
  1975. err_kfree:
  1976. kfree(fileio);
  1977. return ret;
  1978. }
  1979. /**
  1980. * __vb2_cleanup_fileio() - free resourced used by file io emulator
  1981. * @q: videobuf2 queue
  1982. */
  1983. static int __vb2_cleanup_fileio(struct vb2_queue *q)
  1984. {
  1985. struct vb2_fileio_data *fileio = q->fileio;
  1986. if (fileio) {
  1987. /*
  1988. * Hack fileio context to enable direct calls to vb2 ioctl
  1989. * interface.
  1990. */
  1991. q->fileio = NULL;
  1992. vb2_streamoff(q, q->type);
  1993. fileio->req.count = 0;
  1994. vb2_reqbufs(q, &fileio->req);
  1995. kfree(fileio);
  1996. dprintk(3, "file io emulator closed\n");
  1997. }
  1998. return 0;
  1999. }
  2000. /**
  2001. * __vb2_perform_fileio() - perform a single file io (read or write) operation
  2002. * @q: videobuf2 queue
  2003. * @data: pointed to target userspace buffer
  2004. * @count: number of bytes to read or write
  2005. * @ppos: file handle position tracking pointer
  2006. * @nonblock: mode selector (1 means blocking calls, 0 means nonblocking)
  2007. * @read: access mode selector (1 means read, 0 means write)
  2008. */
  2009. static size_t __vb2_perform_fileio(struct vb2_queue *q, char __user *data, size_t count,
  2010. loff_t *ppos, int nonblock, int read)
  2011. {
  2012. struct vb2_fileio_data *fileio;
  2013. struct vb2_fileio_buf *buf;
  2014. int ret, index;
  2015. dprintk(3, "file io: mode %s, offset %ld, count %zd, %sblocking\n",
  2016. read ? "read" : "write", (long)*ppos, count,
  2017. nonblock ? "non" : "");
  2018. if (!data)
  2019. return -EINVAL;
  2020. /*
  2021. * Initialize emulator on first call.
  2022. */
  2023. if (!q->fileio) {
  2024. ret = __vb2_init_fileio(q, read);
  2025. dprintk(3, "file io: vb2_init_fileio result: %d\n", ret);
  2026. if (ret)
  2027. return ret;
  2028. }
  2029. fileio = q->fileio;
  2030. /*
  2031. * Hack fileio context to enable direct calls to vb2 ioctl interface.
  2032. * The pointer will be restored before returning from this function.
  2033. */
  2034. q->fileio = NULL;
  2035. index = fileio->index;
  2036. buf = &fileio->bufs[index];
  2037. /*
  2038. * Check if we need to dequeue the buffer.
  2039. */
  2040. if (buf->queued) {
  2041. struct vb2_buffer *vb;
  2042. /*
  2043. * Call vb2_dqbuf to get buffer back.
  2044. */
  2045. memset(&fileio->b, 0, sizeof(fileio->b));
  2046. fileio->b.type = q->type;
  2047. fileio->b.memory = q->memory;
  2048. fileio->b.index = index;
  2049. ret = vb2_dqbuf(q, &fileio->b, nonblock);
  2050. dprintk(5, "file io: vb2_dqbuf result: %d\n", ret);
  2051. if (ret)
  2052. goto end;
  2053. fileio->dq_count += 1;
  2054. /*
  2055. * Get number of bytes filled by the driver
  2056. */
  2057. vb = q->bufs[index];
  2058. buf->size = vb2_get_plane_payload(vb, 0);
  2059. buf->queued = 0;
  2060. }
  2061. /*
  2062. * Limit count on last few bytes of the buffer.
  2063. */
  2064. if (buf->pos + count > buf->size) {
  2065. count = buf->size - buf->pos;
  2066. dprintk(5, "reducing read count: %zd\n", count);
  2067. }
  2068. /*
  2069. * Transfer data to userspace.
  2070. */
  2071. dprintk(3, "file io: copying %zd bytes - buffer %d, offset %u\n",
  2072. count, index, buf->pos);
  2073. if (read)
  2074. ret = copy_to_user(data, buf->vaddr + buf->pos, count);
  2075. else
  2076. ret = copy_from_user(buf->vaddr + buf->pos, data, count);
  2077. if (ret) {
  2078. dprintk(3, "file io: error copying data\n");
  2079. ret = -EFAULT;
  2080. goto end;
  2081. }
  2082. /*
  2083. * Update counters.
  2084. */
  2085. buf->pos += count;
  2086. *ppos += count;
  2087. /*
  2088. * Queue next buffer if required.
  2089. */
  2090. if (buf->pos == buf->size ||
  2091. (!read && (fileio->flags & VB2_FILEIO_WRITE_IMMEDIATELY))) {
  2092. /*
  2093. * Check if this is the last buffer to read.
  2094. */
  2095. if (read && (fileio->flags & VB2_FILEIO_READ_ONCE) &&
  2096. fileio->dq_count == 1) {
  2097. dprintk(3, "file io: read limit reached\n");
  2098. /*
  2099. * Restore fileio pointer and release the context.
  2100. */
  2101. q->fileio = fileio;
  2102. return __vb2_cleanup_fileio(q);
  2103. }
  2104. /*
  2105. * Call vb2_qbuf and give buffer to the driver.
  2106. */
  2107. memset(&fileio->b, 0, sizeof(fileio->b));
  2108. fileio->b.type = q->type;
  2109. fileio->b.memory = q->memory;
  2110. fileio->b.index = index;
  2111. fileio->b.bytesused = buf->pos;
  2112. ret = vb2_qbuf(q, &fileio->b);
  2113. dprintk(5, "file io: vb2_dbuf result: %d\n", ret);
  2114. if (ret)
  2115. goto end;
  2116. /*
  2117. * Buffer has been queued, update the status
  2118. */
  2119. buf->pos = 0;
  2120. buf->queued = 1;
  2121. buf->size = q->bufs[0]->v4l2_planes[0].length;
  2122. fileio->q_count += 1;
  2123. /*
  2124. * Switch to the next buffer
  2125. */
  2126. fileio->index = (index + 1) % q->num_buffers;
  2127. /*
  2128. * Start streaming if required.
  2129. */
  2130. if (!read && !q->streaming) {
  2131. ret = vb2_streamon(q, q->type);
  2132. if (ret)
  2133. goto end;
  2134. }
  2135. }
  2136. /*
  2137. * Return proper number of bytes processed.
  2138. */
  2139. if (ret == 0)
  2140. ret = count;
  2141. end:
  2142. /*
  2143. * Restore the fileio context and block vb2 ioctl interface.
  2144. */
  2145. q->fileio = fileio;
  2146. return ret;
  2147. }
  2148. size_t vb2_read(struct vb2_queue *q, char __user *data, size_t count,
  2149. loff_t *ppos, int nonblocking)
  2150. {
  2151. return __vb2_perform_fileio(q, data, count, ppos, nonblocking, 1);
  2152. }
  2153. EXPORT_SYMBOL_GPL(vb2_read);
  2154. size_t vb2_write(struct vb2_queue *q, char __user *data, size_t count,
  2155. loff_t *ppos, int nonblocking)
  2156. {
  2157. return __vb2_perform_fileio(q, data, count, ppos, nonblocking, 0);
  2158. }
  2159. EXPORT_SYMBOL_GPL(vb2_write);
  2160. /*
  2161. * The following functions are not part of the vb2 core API, but are helper
  2162. * functions that plug into struct v4l2_ioctl_ops, struct v4l2_file_operations
  2163. * and struct vb2_ops.
  2164. * They contain boilerplate code that most if not all drivers have to do
  2165. * and so they simplify the driver code.
  2166. */
  2167. /* The queue is busy if there is a owner and you are not that owner. */
  2168. static inline bool vb2_queue_is_busy(struct video_device *vdev, struct file *file)
  2169. {
  2170. return vdev->queue->owner && vdev->queue->owner != file->private_data;
  2171. }
  2172. /* vb2 ioctl helpers */
  2173. int vb2_ioctl_reqbufs(struct file *file, void *priv,
  2174. struct v4l2_requestbuffers *p)
  2175. {
  2176. struct video_device *vdev = video_devdata(file);
  2177. int res = __verify_memory_type(vdev->queue, p->memory, p->type);
  2178. if (res)
  2179. return res;
  2180. if (vb2_queue_is_busy(vdev, file))
  2181. return -EBUSY;
  2182. res = __reqbufs(vdev->queue, p);
  2183. /* If count == 0, then the owner has released all buffers and he
  2184. is no longer owner of the queue. Otherwise we have a new owner. */
  2185. if (res == 0)
  2186. vdev->queue->owner = p->count ? file->private_data : NULL;
  2187. return res;
  2188. }
  2189. EXPORT_SYMBOL_GPL(vb2_ioctl_reqbufs);
  2190. int vb2_ioctl_create_bufs(struct file *file, void *priv,
  2191. struct v4l2_create_buffers *p)
  2192. {
  2193. struct video_device *vdev = video_devdata(file);
  2194. int res = __verify_memory_type(vdev->queue, p->memory, p->format.type);
  2195. p->index = vdev->queue->num_buffers;
  2196. /* If count == 0, then just check if memory and type are valid.
  2197. Any -EBUSY result from __verify_memory_type can be mapped to 0. */
  2198. if (p->count == 0)
  2199. return res != -EBUSY ? res : 0;
  2200. if (res)
  2201. return res;
  2202. if (vb2_queue_is_busy(vdev, file))
  2203. return -EBUSY;
  2204. res = __create_bufs(vdev->queue, p);
  2205. if (res == 0)
  2206. vdev->queue->owner = file->private_data;
  2207. return res;
  2208. }
  2209. EXPORT_SYMBOL_GPL(vb2_ioctl_create_bufs);
  2210. int vb2_ioctl_prepare_buf(struct file *file, void *priv,
  2211. struct v4l2_buffer *p)
  2212. {
  2213. struct video_device *vdev = video_devdata(file);
  2214. if (vb2_queue_is_busy(vdev, file))
  2215. return -EBUSY;
  2216. return vb2_prepare_buf(vdev->queue, p);
  2217. }
  2218. EXPORT_SYMBOL_GPL(vb2_ioctl_prepare_buf);
  2219. int vb2_ioctl_querybuf(struct file *file, void *priv, struct v4l2_buffer *p)
  2220. {
  2221. struct video_device *vdev = video_devdata(file);
  2222. /* No need to call vb2_queue_is_busy(), anyone can query buffers. */
  2223. return vb2_querybuf(vdev->queue, p);
  2224. }
  2225. EXPORT_SYMBOL_GPL(vb2_ioctl_querybuf);
  2226. int vb2_ioctl_qbuf(struct file *file, void *priv, struct v4l2_buffer *p)
  2227. {
  2228. struct video_device *vdev = video_devdata(file);
  2229. if (vb2_queue_is_busy(vdev, file))
  2230. return -EBUSY;
  2231. return vb2_qbuf(vdev->queue, p);
  2232. }
  2233. EXPORT_SYMBOL_GPL(vb2_ioctl_qbuf);
  2234. int vb2_ioctl_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
  2235. {
  2236. struct video_device *vdev = video_devdata(file);
  2237. if (vb2_queue_is_busy(vdev, file))
  2238. return -EBUSY;
  2239. return vb2_dqbuf(vdev->queue, p, file->f_flags & O_NONBLOCK);
  2240. }
  2241. EXPORT_SYMBOL_GPL(vb2_ioctl_dqbuf);
  2242. int vb2_ioctl_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
  2243. {
  2244. struct video_device *vdev = video_devdata(file);
  2245. if (vb2_queue_is_busy(vdev, file))
  2246. return -EBUSY;
  2247. return vb2_streamon(vdev->queue, i);
  2248. }
  2249. EXPORT_SYMBOL_GPL(vb2_ioctl_streamon);
  2250. int vb2_ioctl_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
  2251. {
  2252. struct video_device *vdev = video_devdata(file);
  2253. if (vb2_queue_is_busy(vdev, file))
  2254. return -EBUSY;
  2255. return vb2_streamoff(vdev->queue, i);
  2256. }
  2257. EXPORT_SYMBOL_GPL(vb2_ioctl_streamoff);
  2258. int vb2_ioctl_expbuf(struct file *file, void *priv, struct v4l2_exportbuffer *p)
  2259. {
  2260. struct video_device *vdev = video_devdata(file);
  2261. if (vb2_queue_is_busy(vdev, file))
  2262. return -EBUSY;
  2263. return vb2_expbuf(vdev->queue, p);
  2264. }
  2265. EXPORT_SYMBOL_GPL(vb2_ioctl_expbuf);
  2266. /* v4l2_file_operations helpers */
  2267. int vb2_fop_mmap(struct file *file, struct vm_area_struct *vma)
  2268. {
  2269. struct video_device *vdev = video_devdata(file);
  2270. struct mutex *lock = vdev->queue->lock ? vdev->queue->lock : vdev->lock;
  2271. int err;
  2272. if (lock && mutex_lock_interruptible(lock))
  2273. return -ERESTARTSYS;
  2274. err = vb2_mmap(vdev->queue, vma);
  2275. if (lock)
  2276. mutex_unlock(lock);
  2277. return err;
  2278. }
  2279. EXPORT_SYMBOL_GPL(vb2_fop_mmap);
  2280. int vb2_fop_release(struct file *file)
  2281. {
  2282. struct video_device *vdev = video_devdata(file);
  2283. if (file->private_data == vdev->queue->owner) {
  2284. vb2_queue_release(vdev->queue);
  2285. vdev->queue->owner = NULL;
  2286. }
  2287. return v4l2_fh_release(file);
  2288. }
  2289. EXPORT_SYMBOL_GPL(vb2_fop_release);
  2290. ssize_t vb2_fop_write(struct file *file, char __user *buf,
  2291. size_t count, loff_t *ppos)
  2292. {
  2293. struct video_device *vdev = video_devdata(file);
  2294. struct mutex *lock = vdev->queue->lock ? vdev->queue->lock : vdev->lock;
  2295. int err = -EBUSY;
  2296. if (lock && mutex_lock_interruptible(lock))
  2297. return -ERESTARTSYS;
  2298. if (vb2_queue_is_busy(vdev, file))
  2299. goto exit;
  2300. err = vb2_write(vdev->queue, buf, count, ppos,
  2301. file->f_flags & O_NONBLOCK);
  2302. if (vdev->queue->fileio)
  2303. vdev->queue->owner = file->private_data;
  2304. exit:
  2305. if (lock)
  2306. mutex_unlock(lock);
  2307. return err;
  2308. }
  2309. EXPORT_SYMBOL_GPL(vb2_fop_write);
  2310. ssize_t vb2_fop_read(struct file *file, char __user *buf,
  2311. size_t count, loff_t *ppos)
  2312. {
  2313. struct video_device *vdev = video_devdata(file);
  2314. struct mutex *lock = vdev->queue->lock ? vdev->queue->lock : vdev->lock;
  2315. int err = -EBUSY;
  2316. if (lock && mutex_lock_interruptible(lock))
  2317. return -ERESTARTSYS;
  2318. if (vb2_queue_is_busy(vdev, file))
  2319. goto exit;
  2320. err = vb2_read(vdev->queue, buf, count, ppos,
  2321. file->f_flags & O_NONBLOCK);
  2322. if (vdev->queue->fileio)
  2323. vdev->queue->owner = file->private_data;
  2324. exit:
  2325. if (lock)
  2326. mutex_unlock(lock);
  2327. return err;
  2328. }
  2329. EXPORT_SYMBOL_GPL(vb2_fop_read);
  2330. unsigned int vb2_fop_poll(struct file *file, poll_table *wait)
  2331. {
  2332. struct video_device *vdev = video_devdata(file);
  2333. struct vb2_queue *q = vdev->queue;
  2334. struct mutex *lock = q->lock ? q->lock : vdev->lock;
  2335. unsigned long req_events = poll_requested_events(wait);
  2336. unsigned res;
  2337. void *fileio;
  2338. bool must_lock = false;
  2339. /* Try to be smart: only lock if polling might start fileio,
  2340. otherwise locking will only introduce unwanted delays. */
  2341. if (q->num_buffers == 0 && q->fileio == NULL) {
  2342. if (!V4L2_TYPE_IS_OUTPUT(q->type) && (q->io_modes & VB2_READ) &&
  2343. (req_events & (POLLIN | POLLRDNORM)))
  2344. must_lock = true;
  2345. else if (V4L2_TYPE_IS_OUTPUT(q->type) && (q->io_modes & VB2_WRITE) &&
  2346. (req_events & (POLLOUT | POLLWRNORM)))
  2347. must_lock = true;
  2348. }
  2349. /* If locking is needed, but this helper doesn't know how, then you
  2350. shouldn't be using this helper but you should write your own. */
  2351. WARN_ON(must_lock && !lock);
  2352. if (must_lock && lock && mutex_lock_interruptible(lock))
  2353. return POLLERR;
  2354. fileio = q->fileio;
  2355. res = vb2_poll(vdev->queue, file, wait);
  2356. /* If fileio was started, then we have a new queue owner. */
  2357. if (must_lock && !fileio && q->fileio)
  2358. q->owner = file->private_data;
  2359. if (must_lock && lock)
  2360. mutex_unlock(lock);
  2361. return res;
  2362. }
  2363. EXPORT_SYMBOL_GPL(vb2_fop_poll);
  2364. #ifndef CONFIG_MMU
  2365. unsigned long vb2_fop_get_unmapped_area(struct file *file, unsigned long addr,
  2366. unsigned long len, unsigned long pgoff, unsigned long flags)
  2367. {
  2368. struct video_device *vdev = video_devdata(file);
  2369. struct mutex *lock = vdev->queue->lock ? vdev->queue->lock : vdev->lock;
  2370. int ret;
  2371. if (lock && mutex_lock_interruptible(lock))
  2372. return -ERESTARTSYS;
  2373. ret = vb2_get_unmapped_area(vdev->queue, addr, len, pgoff, flags);
  2374. if (lock)
  2375. mutex_unlock(lock);
  2376. return ret;
  2377. }
  2378. EXPORT_SYMBOL_GPL(vb2_fop_get_unmapped_area);
  2379. #endif
  2380. /* vb2_ops helpers. Only use if vq->lock is non-NULL. */
  2381. void vb2_ops_wait_prepare(struct vb2_queue *vq)
  2382. {
  2383. mutex_unlock(vq->lock);
  2384. }
  2385. EXPORT_SYMBOL_GPL(vb2_ops_wait_prepare);
  2386. void vb2_ops_wait_finish(struct vb2_queue *vq)
  2387. {
  2388. mutex_lock(vq->lock);
  2389. }
  2390. EXPORT_SYMBOL_GPL(vb2_ops_wait_finish);
  2391. MODULE_DESCRIPTION("Driver helper framework for Video for Linux 2");
  2392. MODULE_AUTHOR("Pawel Osciak <pawel@osciak.com>, Marek Szyprowski");
  2393. MODULE_LICENSE("GPL");