videobuf2-core.c 70 KB

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