videobuf2-core.c 71 KB

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