videobuf2-core.c 67 KB

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