videobuf2-core.c 66 KB

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