videobuf2-core.c 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076
  1. /*
  2. * videobuf2-core.c - V4L2 driver helper framework
  3. *
  4. * Copyright (C) 2010 Samsung Electronics
  5. *
  6. * Author: Pawel Osciak <pawel@osciak.com>
  7. * Marek Szyprowski <m.szyprowski@samsung.com>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation.
  12. */
  13. #include <linux/err.h>
  14. #include <linux/kernel.h>
  15. #include <linux/module.h>
  16. #include <linux/mm.h>
  17. #include <linux/poll.h>
  18. #include <linux/slab.h>
  19. #include <linux/sched.h>
  20. #include <media/videobuf2-core.h>
  21. static int debug;
  22. module_param(debug, int, 0644);
  23. #define dprintk(level, fmt, arg...) \
  24. do { \
  25. if (debug >= level) \
  26. printk(KERN_DEBUG "vb2: " fmt, ## arg); \
  27. } while (0)
  28. #define call_memop(q, plane, op, args...) \
  29. (((q)->mem_ops->op) ? \
  30. ((q)->mem_ops->op(args)) : 0)
  31. #define call_qop(q, op, args...) \
  32. (((q)->ops->op) ? ((q)->ops->op(args)) : 0)
  33. #define V4L2_BUFFER_STATE_FLAGS (V4L2_BUF_FLAG_MAPPED | V4L2_BUF_FLAG_QUEUED | \
  34. V4L2_BUF_FLAG_DONE | V4L2_BUF_FLAG_ERROR | \
  35. V4L2_BUF_FLAG_PREPARED)
  36. /**
  37. * __vb2_buf_mem_alloc() - allocate video memory for the given buffer
  38. */
  39. static int __vb2_buf_mem_alloc(struct vb2_buffer *vb)
  40. {
  41. struct vb2_queue *q = vb->vb2_queue;
  42. void *mem_priv;
  43. int plane;
  44. /* Allocate memory for all planes in this buffer */
  45. for (plane = 0; plane < vb->num_planes; ++plane) {
  46. mem_priv = call_memop(q, plane, alloc, q->alloc_ctx[plane],
  47. q->plane_sizes[plane]);
  48. if (IS_ERR_OR_NULL(mem_priv))
  49. goto free;
  50. /* Associate allocator private data with this plane */
  51. vb->planes[plane].mem_priv = mem_priv;
  52. vb->v4l2_planes[plane].length = q->plane_sizes[plane];
  53. }
  54. return 0;
  55. free:
  56. /* Free already allocated memory if one of the allocations failed */
  57. for (; plane > 0; --plane)
  58. call_memop(q, plane, put, vb->planes[plane - 1].mem_priv);
  59. return -ENOMEM;
  60. }
  61. /**
  62. * __vb2_buf_mem_free() - free memory of the given buffer
  63. */
  64. static void __vb2_buf_mem_free(struct vb2_buffer *vb)
  65. {
  66. struct vb2_queue *q = vb->vb2_queue;
  67. unsigned int plane;
  68. for (plane = 0; plane < vb->num_planes; ++plane) {
  69. call_memop(q, plane, put, vb->planes[plane].mem_priv);
  70. vb->planes[plane].mem_priv = NULL;
  71. dprintk(3, "Freed plane %d of buffer %d\n",
  72. plane, vb->v4l2_buf.index);
  73. }
  74. }
  75. /**
  76. * __vb2_buf_userptr_put() - release userspace memory associated with
  77. * a USERPTR buffer
  78. */
  79. static void __vb2_buf_userptr_put(struct vb2_buffer *vb)
  80. {
  81. struct vb2_queue *q = vb->vb2_queue;
  82. unsigned int plane;
  83. for (plane = 0; plane < vb->num_planes; ++plane) {
  84. void *mem_priv = vb->planes[plane].mem_priv;
  85. if (mem_priv) {
  86. call_memop(q, plane, put_userptr, mem_priv);
  87. vb->planes[plane].mem_priv = NULL;
  88. }
  89. }
  90. }
  91. /**
  92. * __setup_offsets() - setup unique offsets ("cookies") for every plane in
  93. * every buffer on the queue
  94. */
  95. static void __setup_offsets(struct vb2_queue *q, unsigned int n)
  96. {
  97. unsigned int buffer, plane;
  98. struct vb2_buffer *vb;
  99. unsigned long off;
  100. if (q->num_buffers) {
  101. struct v4l2_plane *p;
  102. vb = q->bufs[q->num_buffers - 1];
  103. p = &vb->v4l2_planes[vb->num_planes - 1];
  104. off = PAGE_ALIGN(p->m.mem_offset + p->length);
  105. } else {
  106. off = 0;
  107. }
  108. for (buffer = q->num_buffers; buffer < q->num_buffers + n; ++buffer) {
  109. vb = q->bufs[buffer];
  110. if (!vb)
  111. continue;
  112. for (plane = 0; plane < vb->num_planes; ++plane) {
  113. vb->v4l2_planes[plane].length = q->plane_sizes[plane];
  114. vb->v4l2_planes[plane].m.mem_offset = off;
  115. dprintk(3, "Buffer %d, plane %d offset 0x%08lx\n",
  116. buffer, plane, off);
  117. off += vb->v4l2_planes[plane].length;
  118. off = PAGE_ALIGN(off);
  119. }
  120. }
  121. }
  122. /**
  123. * __vb2_queue_alloc() - allocate videobuf buffer structures and (for MMAP type)
  124. * video buffer memory for all buffers/planes on the queue and initializes the
  125. * queue
  126. *
  127. * Returns the number of buffers successfully allocated.
  128. */
  129. static int __vb2_queue_alloc(struct vb2_queue *q, enum v4l2_memory memory,
  130. unsigned int num_buffers, unsigned int num_planes)
  131. {
  132. unsigned int buffer;
  133. struct vb2_buffer *vb;
  134. int ret;
  135. for (buffer = 0; buffer < num_buffers; ++buffer) {
  136. /* Allocate videobuf buffer structures */
  137. vb = kzalloc(q->buf_struct_size, GFP_KERNEL);
  138. if (!vb) {
  139. dprintk(1, "Memory alloc for buffer struct failed\n");
  140. break;
  141. }
  142. /* Length stores number of planes for multiplanar buffers */
  143. if (V4L2_TYPE_IS_MULTIPLANAR(q->type))
  144. vb->v4l2_buf.length = num_planes;
  145. vb->state = VB2_BUF_STATE_DEQUEUED;
  146. vb->vb2_queue = q;
  147. vb->num_planes = num_planes;
  148. vb->v4l2_buf.index = q->num_buffers + buffer;
  149. vb->v4l2_buf.type = q->type;
  150. vb->v4l2_buf.memory = memory;
  151. /* Allocate video buffer memory for the MMAP type */
  152. if (memory == V4L2_MEMORY_MMAP) {
  153. ret = __vb2_buf_mem_alloc(vb);
  154. if (ret) {
  155. dprintk(1, "Failed allocating memory for "
  156. "buffer %d\n", buffer);
  157. kfree(vb);
  158. break;
  159. }
  160. /*
  161. * Call the driver-provided buffer initialization
  162. * callback, if given. An error in initialization
  163. * results in queue setup failure.
  164. */
  165. ret = call_qop(q, buf_init, vb);
  166. if (ret) {
  167. dprintk(1, "Buffer %d %p initialization"
  168. " failed\n", buffer, vb);
  169. __vb2_buf_mem_free(vb);
  170. kfree(vb);
  171. break;
  172. }
  173. }
  174. q->bufs[q->num_buffers + buffer] = vb;
  175. }
  176. __setup_offsets(q, buffer);
  177. dprintk(1, "Allocated %d buffers, %d plane(s) each\n",
  178. buffer, num_planes);
  179. return buffer;
  180. }
  181. /**
  182. * __vb2_free_mem() - release all video buffer memory for a given queue
  183. */
  184. static void __vb2_free_mem(struct vb2_queue *q, unsigned int buffers)
  185. {
  186. unsigned int buffer;
  187. struct vb2_buffer *vb;
  188. for (buffer = q->num_buffers - buffers; buffer < q->num_buffers;
  189. ++buffer) {
  190. vb = q->bufs[buffer];
  191. if (!vb)
  192. continue;
  193. /* Free MMAP buffers or release USERPTR buffers */
  194. if (q->memory == V4L2_MEMORY_MMAP)
  195. __vb2_buf_mem_free(vb);
  196. else
  197. __vb2_buf_userptr_put(vb);
  198. }
  199. }
  200. /**
  201. * __vb2_queue_free() - free buffers at the end of the queue - video memory and
  202. * related information, if no buffers are left return the queue to an
  203. * uninitialized state. Might be called even if the queue has already been freed.
  204. */
  205. static void __vb2_queue_free(struct vb2_queue *q, unsigned int buffers)
  206. {
  207. unsigned int buffer;
  208. /* Call driver-provided cleanup function for each buffer, if provided */
  209. if (q->ops->buf_cleanup) {
  210. for (buffer = q->num_buffers - buffers; buffer < q->num_buffers;
  211. ++buffer) {
  212. if (NULL == q->bufs[buffer])
  213. continue;
  214. q->ops->buf_cleanup(q->bufs[buffer]);
  215. }
  216. }
  217. /* Release video buffer memory */
  218. __vb2_free_mem(q, buffers);
  219. /* Free videobuf buffers */
  220. for (buffer = q->num_buffers - buffers; buffer < q->num_buffers;
  221. ++buffer) {
  222. kfree(q->bufs[buffer]);
  223. q->bufs[buffer] = NULL;
  224. }
  225. q->num_buffers -= buffers;
  226. if (!q->num_buffers)
  227. q->memory = 0;
  228. INIT_LIST_HEAD(&q->queued_list);
  229. }
  230. /**
  231. * __verify_planes_array() - verify that the planes array passed in struct
  232. * v4l2_buffer from userspace can be safely used
  233. */
  234. static int __verify_planes_array(struct vb2_buffer *vb, const struct v4l2_buffer *b)
  235. {
  236. /* Is memory for copying plane information present? */
  237. if (NULL == b->m.planes) {
  238. dprintk(1, "Multi-planar buffer passed but "
  239. "planes array not provided\n");
  240. return -EINVAL;
  241. }
  242. if (b->length < vb->num_planes || b->length > VIDEO_MAX_PLANES) {
  243. dprintk(1, "Incorrect planes array length, "
  244. "expected %d, got %d\n", vb->num_planes, b->length);
  245. return -EINVAL;
  246. }
  247. return 0;
  248. }
  249. /**
  250. * __buffer_in_use() - return true if the buffer is in use and
  251. * the queue cannot be freed (by the means of REQBUFS(0)) call
  252. */
  253. static bool __buffer_in_use(struct vb2_queue *q, struct vb2_buffer *vb)
  254. {
  255. unsigned int plane;
  256. for (plane = 0; plane < vb->num_planes; ++plane) {
  257. void *mem_priv = vb->planes[plane].mem_priv;
  258. /*
  259. * If num_users() has not been provided, call_memop
  260. * will return 0, apparently nobody cares about this
  261. * case anyway. If num_users() returns more than 1,
  262. * we are not the only user of the plane's memory.
  263. */
  264. if (mem_priv && call_memop(q, plane, num_users, mem_priv) > 1)
  265. return true;
  266. }
  267. return false;
  268. }
  269. /**
  270. * __buffers_in_use() - return true if any buffers on the queue are in use and
  271. * the queue cannot be freed (by the means of REQBUFS(0)) call
  272. */
  273. static bool __buffers_in_use(struct vb2_queue *q)
  274. {
  275. unsigned int buffer;
  276. for (buffer = 0; buffer < q->num_buffers; ++buffer) {
  277. if (__buffer_in_use(q, q->bufs[buffer]))
  278. return true;
  279. }
  280. return false;
  281. }
  282. /**
  283. * __fill_v4l2_buffer() - fill in a struct v4l2_buffer with information to be
  284. * returned to userspace
  285. */
  286. static int __fill_v4l2_buffer(struct vb2_buffer *vb, struct v4l2_buffer *b)
  287. {
  288. struct vb2_queue *q = vb->vb2_queue;
  289. int ret;
  290. /* Copy back data such as timestamp, flags, input, etc. */
  291. memcpy(b, &vb->v4l2_buf, offsetof(struct v4l2_buffer, m));
  292. b->input = vb->v4l2_buf.input;
  293. b->reserved = vb->v4l2_buf.reserved;
  294. if (V4L2_TYPE_IS_MULTIPLANAR(q->type)) {
  295. ret = __verify_planes_array(vb, b);
  296. if (ret)
  297. return ret;
  298. /*
  299. * Fill in plane-related data if userspace provided an array
  300. * for it. The memory and size is verified above.
  301. */
  302. memcpy(b->m.planes, vb->v4l2_planes,
  303. b->length * sizeof(struct v4l2_plane));
  304. } else {
  305. /*
  306. * We use length and offset in v4l2_planes array even for
  307. * single-planar buffers, but userspace does not.
  308. */
  309. b->length = vb->v4l2_planes[0].length;
  310. b->bytesused = vb->v4l2_planes[0].bytesused;
  311. if (q->memory == V4L2_MEMORY_MMAP)
  312. b->m.offset = vb->v4l2_planes[0].m.mem_offset;
  313. else if (q->memory == V4L2_MEMORY_USERPTR)
  314. b->m.userptr = vb->v4l2_planes[0].m.userptr;
  315. }
  316. /*
  317. * Clear any buffer state related flags.
  318. */
  319. b->flags &= ~V4L2_BUFFER_STATE_FLAGS;
  320. switch (vb->state) {
  321. case VB2_BUF_STATE_QUEUED:
  322. case VB2_BUF_STATE_ACTIVE:
  323. b->flags |= V4L2_BUF_FLAG_QUEUED;
  324. break;
  325. case VB2_BUF_STATE_ERROR:
  326. b->flags |= V4L2_BUF_FLAG_ERROR;
  327. /* fall through */
  328. case VB2_BUF_STATE_DONE:
  329. b->flags |= V4L2_BUF_FLAG_DONE;
  330. break;
  331. case VB2_BUF_STATE_PREPARED:
  332. b->flags |= V4L2_BUF_FLAG_PREPARED;
  333. break;
  334. case VB2_BUF_STATE_DEQUEUED:
  335. /* nothing */
  336. break;
  337. }
  338. if (__buffer_in_use(q, vb))
  339. b->flags |= V4L2_BUF_FLAG_MAPPED;
  340. return 0;
  341. }
  342. /**
  343. * vb2_querybuf() - query video buffer information
  344. * @q: videobuf queue
  345. * @b: buffer struct passed from userspace to vidioc_querybuf handler
  346. * in driver
  347. *
  348. * Should be called from vidioc_querybuf ioctl handler in driver.
  349. * This function will verify the passed v4l2_buffer structure and fill the
  350. * relevant information for the userspace.
  351. *
  352. * The return values from this function are intended to be directly returned
  353. * from vidioc_querybuf handler in driver.
  354. */
  355. int vb2_querybuf(struct vb2_queue *q, struct v4l2_buffer *b)
  356. {
  357. struct vb2_buffer *vb;
  358. if (b->type != q->type) {
  359. dprintk(1, "querybuf: wrong buffer type\n");
  360. return -EINVAL;
  361. }
  362. if (b->index >= q->num_buffers) {
  363. dprintk(1, "querybuf: buffer index out of range\n");
  364. return -EINVAL;
  365. }
  366. vb = q->bufs[b->index];
  367. return __fill_v4l2_buffer(vb, b);
  368. }
  369. EXPORT_SYMBOL(vb2_querybuf);
  370. /**
  371. * __verify_userptr_ops() - verify that all memory operations required for
  372. * USERPTR queue type have been provided
  373. */
  374. static int __verify_userptr_ops(struct vb2_queue *q)
  375. {
  376. if (!(q->io_modes & VB2_USERPTR) || !q->mem_ops->get_userptr ||
  377. !q->mem_ops->put_userptr)
  378. return -EINVAL;
  379. return 0;
  380. }
  381. /**
  382. * __verify_mmap_ops() - verify that all memory operations required for
  383. * MMAP queue type have been provided
  384. */
  385. static int __verify_mmap_ops(struct vb2_queue *q)
  386. {
  387. if (!(q->io_modes & VB2_MMAP) || !q->mem_ops->alloc ||
  388. !q->mem_ops->put || !q->mem_ops->mmap)
  389. return -EINVAL;
  390. return 0;
  391. }
  392. /**
  393. * vb2_reqbufs() - Initiate streaming
  394. * @q: videobuf2 queue
  395. * @req: struct passed from userspace to vidioc_reqbufs handler in driver
  396. *
  397. * Should be called from vidioc_reqbufs ioctl handler of a driver.
  398. * This function:
  399. * 1) verifies streaming parameters passed from the userspace,
  400. * 2) sets up the queue,
  401. * 3) negotiates number of buffers and planes per buffer with the driver
  402. * to be used during streaming,
  403. * 4) allocates internal buffer structures (struct vb2_buffer), according to
  404. * the agreed parameters,
  405. * 5) for MMAP memory type, allocates actual video memory, using the
  406. * memory handling/allocation routines provided during queue initialization
  407. *
  408. * If req->count is 0, all the memory will be freed instead.
  409. * If the queue has been allocated previously (by a previous vb2_reqbufs) call
  410. * and the queue is not busy, memory will be reallocated.
  411. *
  412. * The return values from this function are intended to be directly returned
  413. * from vidioc_reqbufs handler in driver.
  414. */
  415. int vb2_reqbufs(struct vb2_queue *q, struct v4l2_requestbuffers *req)
  416. {
  417. unsigned int num_buffers, allocated_buffers, num_planes = 0;
  418. int ret = 0;
  419. if (q->fileio) {
  420. dprintk(1, "reqbufs: file io in progress\n");
  421. return -EBUSY;
  422. }
  423. if (req->memory != V4L2_MEMORY_MMAP
  424. && req->memory != V4L2_MEMORY_USERPTR) {
  425. dprintk(1, "reqbufs: unsupported memory type\n");
  426. return -EINVAL;
  427. }
  428. if (req->type != q->type) {
  429. dprintk(1, "reqbufs: requested type is incorrect\n");
  430. return -EINVAL;
  431. }
  432. if (q->streaming) {
  433. dprintk(1, "reqbufs: streaming active\n");
  434. return -EBUSY;
  435. }
  436. /*
  437. * Make sure all the required memory ops for given memory type
  438. * are available.
  439. */
  440. if (req->memory == V4L2_MEMORY_MMAP && __verify_mmap_ops(q)) {
  441. dprintk(1, "reqbufs: MMAP for current setup unsupported\n");
  442. return -EINVAL;
  443. }
  444. if (req->memory == V4L2_MEMORY_USERPTR && __verify_userptr_ops(q)) {
  445. dprintk(1, "reqbufs: USERPTR for current setup unsupported\n");
  446. return -EINVAL;
  447. }
  448. if (req->count == 0 || q->num_buffers != 0 || q->memory != req->memory) {
  449. /*
  450. * We already have buffers allocated, so first check if they
  451. * are not in use and can be freed.
  452. */
  453. if (q->memory == V4L2_MEMORY_MMAP && __buffers_in_use(q)) {
  454. dprintk(1, "reqbufs: memory in use, cannot free\n");
  455. return -EBUSY;
  456. }
  457. __vb2_queue_free(q, q->num_buffers);
  458. /*
  459. * In case of REQBUFS(0) return immediately without calling
  460. * driver's queue_setup() callback and allocating resources.
  461. */
  462. if (req->count == 0)
  463. return 0;
  464. }
  465. /*
  466. * Make sure the requested values and current defaults are sane.
  467. */
  468. num_buffers = min_t(unsigned int, req->count, VIDEO_MAX_FRAME);
  469. memset(q->plane_sizes, 0, sizeof(q->plane_sizes));
  470. memset(q->alloc_ctx, 0, sizeof(q->alloc_ctx));
  471. q->memory = req->memory;
  472. /*
  473. * Ask the driver how many buffers and planes per buffer it requires.
  474. * Driver also sets the size and allocator context for each plane.
  475. */
  476. ret = call_qop(q, queue_setup, q, NULL, &num_buffers, &num_planes,
  477. q->plane_sizes, q->alloc_ctx);
  478. if (ret)
  479. return ret;
  480. /* Finally, allocate buffers and video memory */
  481. ret = __vb2_queue_alloc(q, req->memory, num_buffers, num_planes);
  482. if (ret == 0) {
  483. dprintk(1, "Memory allocation failed\n");
  484. return -ENOMEM;
  485. }
  486. allocated_buffers = ret;
  487. /*
  488. * Check if driver can handle the allocated number of buffers.
  489. */
  490. if (allocated_buffers < num_buffers) {
  491. num_buffers = allocated_buffers;
  492. ret = call_qop(q, queue_setup, q, NULL, &num_buffers,
  493. &num_planes, q->plane_sizes, q->alloc_ctx);
  494. if (!ret && allocated_buffers < num_buffers)
  495. ret = -ENOMEM;
  496. /*
  497. * Either the driver has accepted a smaller number of buffers,
  498. * or .queue_setup() returned an error
  499. */
  500. }
  501. q->num_buffers = allocated_buffers;
  502. if (ret < 0) {
  503. __vb2_queue_free(q, allocated_buffers);
  504. return ret;
  505. }
  506. /*
  507. * Return the number of successfully allocated buffers
  508. * to the userspace.
  509. */
  510. req->count = allocated_buffers;
  511. return 0;
  512. }
  513. EXPORT_SYMBOL_GPL(vb2_reqbufs);
  514. /**
  515. * vb2_create_bufs() - Allocate buffers and any required auxiliary structs
  516. * @q: videobuf2 queue
  517. * @create: creation parameters, passed from userspace to vidioc_create_bufs
  518. * handler in driver
  519. *
  520. * Should be called from vidioc_create_bufs ioctl handler of a driver.
  521. * This function:
  522. * 1) verifies parameter sanity
  523. * 2) calls the .queue_setup() queue operation
  524. * 3) performs any necessary memory allocations
  525. *
  526. * The return values from this function are intended to be directly returned
  527. * from vidioc_create_bufs handler in driver.
  528. */
  529. int vb2_create_bufs(struct vb2_queue *q, struct v4l2_create_buffers *create)
  530. {
  531. unsigned int num_planes = 0, num_buffers, allocated_buffers;
  532. int ret = 0;
  533. if (q->fileio) {
  534. dprintk(1, "%s(): file io in progress\n", __func__);
  535. return -EBUSY;
  536. }
  537. if (create->memory != V4L2_MEMORY_MMAP
  538. && create->memory != V4L2_MEMORY_USERPTR) {
  539. dprintk(1, "%s(): unsupported memory type\n", __func__);
  540. return -EINVAL;
  541. }
  542. if (create->format.type != q->type) {
  543. dprintk(1, "%s(): requested type is incorrect\n", __func__);
  544. return -EINVAL;
  545. }
  546. /*
  547. * Make sure all the required memory ops for given memory type
  548. * are available.
  549. */
  550. if (create->memory == V4L2_MEMORY_MMAP && __verify_mmap_ops(q)) {
  551. dprintk(1, "%s(): MMAP for current setup unsupported\n", __func__);
  552. return -EINVAL;
  553. }
  554. if (create->memory == V4L2_MEMORY_USERPTR && __verify_userptr_ops(q)) {
  555. dprintk(1, "%s(): USERPTR for current setup unsupported\n", __func__);
  556. return -EINVAL;
  557. }
  558. if (q->num_buffers == VIDEO_MAX_FRAME) {
  559. dprintk(1, "%s(): maximum number of buffers already allocated\n",
  560. __func__);
  561. return -ENOBUFS;
  562. }
  563. create->index = q->num_buffers;
  564. if (!q->num_buffers) {
  565. memset(q->plane_sizes, 0, sizeof(q->plane_sizes));
  566. memset(q->alloc_ctx, 0, sizeof(q->alloc_ctx));
  567. q->memory = create->memory;
  568. }
  569. num_buffers = min(create->count, VIDEO_MAX_FRAME - q->num_buffers);
  570. /*
  571. * Ask the driver, whether the requested number of buffers, planes per
  572. * buffer and their sizes are acceptable
  573. */
  574. ret = call_qop(q, queue_setup, q, &create->format, &num_buffers,
  575. &num_planes, q->plane_sizes, q->alloc_ctx);
  576. if (ret)
  577. return ret;
  578. /* Finally, allocate buffers and video memory */
  579. ret = __vb2_queue_alloc(q, create->memory, num_buffers,
  580. num_planes);
  581. if (ret < 0) {
  582. dprintk(1, "Memory allocation failed with error: %d\n", ret);
  583. return ret;
  584. }
  585. allocated_buffers = ret;
  586. /*
  587. * Check if driver can handle the so far allocated number of buffers.
  588. */
  589. if (ret < num_buffers) {
  590. num_buffers = ret;
  591. /*
  592. * q->num_buffers contains the total number of buffers, that the
  593. * queue driver has set up
  594. */
  595. ret = call_qop(q, queue_setup, q, &create->format, &num_buffers,
  596. &num_planes, q->plane_sizes, q->alloc_ctx);
  597. if (!ret && allocated_buffers < num_buffers)
  598. ret = -ENOMEM;
  599. /*
  600. * Either the driver has accepted a smaller number of buffers,
  601. * or .queue_setup() returned an error
  602. */
  603. }
  604. q->num_buffers += allocated_buffers;
  605. if (ret < 0) {
  606. __vb2_queue_free(q, allocated_buffers);
  607. return ret;
  608. }
  609. /*
  610. * Return the number of successfully allocated buffers
  611. * to the userspace.
  612. */
  613. create->count = allocated_buffers;
  614. return 0;
  615. }
  616. EXPORT_SYMBOL_GPL(vb2_create_bufs);
  617. /**
  618. * vb2_plane_vaddr() - Return a kernel virtual address of a given plane
  619. * @vb: vb2_buffer to which the plane in question belongs to
  620. * @plane_no: plane number for which the address is to be returned
  621. *
  622. * This function returns a kernel virtual address of a given plane if
  623. * such a mapping exist, NULL otherwise.
  624. */
  625. void *vb2_plane_vaddr(struct vb2_buffer *vb, unsigned int plane_no)
  626. {
  627. struct vb2_queue *q = vb->vb2_queue;
  628. if (plane_no > vb->num_planes)
  629. return NULL;
  630. return call_memop(q, plane_no, vaddr, vb->planes[plane_no].mem_priv);
  631. }
  632. EXPORT_SYMBOL_GPL(vb2_plane_vaddr);
  633. /**
  634. * vb2_plane_cookie() - Return allocator specific cookie for the given plane
  635. * @vb: vb2_buffer to which the plane in question belongs to
  636. * @plane_no: plane number for which the cookie is to be returned
  637. *
  638. * This function returns an allocator specific cookie for a given plane if
  639. * available, NULL otherwise. The allocator should provide some simple static
  640. * inline function, which would convert this cookie to the allocator specific
  641. * type that can be used directly by the driver to access the buffer. This can
  642. * be for example physical address, pointer to scatter list or IOMMU mapping.
  643. */
  644. void *vb2_plane_cookie(struct vb2_buffer *vb, unsigned int plane_no)
  645. {
  646. struct vb2_queue *q = vb->vb2_queue;
  647. if (plane_no > vb->num_planes)
  648. return NULL;
  649. return call_memop(q, plane_no, cookie, vb->planes[plane_no].mem_priv);
  650. }
  651. EXPORT_SYMBOL_GPL(vb2_plane_cookie);
  652. /**
  653. * vb2_buffer_done() - inform videobuf that an operation on a buffer is finished
  654. * @vb: vb2_buffer returned from the driver
  655. * @state: either VB2_BUF_STATE_DONE if the operation finished successfully
  656. * or VB2_BUF_STATE_ERROR if the operation finished with an error
  657. *
  658. * This function should be called by the driver after a hardware operation on
  659. * a buffer is finished and the buffer may be returned to userspace. The driver
  660. * cannot use this buffer anymore until it is queued back to it by videobuf
  661. * by the means of buf_queue callback. Only buffers previously queued to the
  662. * driver by buf_queue can be passed to this function.
  663. */
  664. void vb2_buffer_done(struct vb2_buffer *vb, enum vb2_buffer_state state)
  665. {
  666. struct vb2_queue *q = vb->vb2_queue;
  667. unsigned long flags;
  668. if (vb->state != VB2_BUF_STATE_ACTIVE)
  669. return;
  670. if (state != VB2_BUF_STATE_DONE && state != VB2_BUF_STATE_ERROR)
  671. return;
  672. dprintk(4, "Done processing on buffer %d, state: %d\n",
  673. vb->v4l2_buf.index, vb->state);
  674. /* Add the buffer to the done buffers list */
  675. spin_lock_irqsave(&q->done_lock, flags);
  676. vb->state = state;
  677. list_add_tail(&vb->done_entry, &q->done_list);
  678. atomic_dec(&q->queued_count);
  679. spin_unlock_irqrestore(&q->done_lock, flags);
  680. /* Inform any processes that may be waiting for buffers */
  681. wake_up(&q->done_wq);
  682. }
  683. EXPORT_SYMBOL_GPL(vb2_buffer_done);
  684. /**
  685. * __fill_vb2_buffer() - fill a vb2_buffer with information provided in
  686. * a v4l2_buffer by the userspace
  687. */
  688. static int __fill_vb2_buffer(struct vb2_buffer *vb, const struct v4l2_buffer *b,
  689. struct v4l2_plane *v4l2_planes)
  690. {
  691. unsigned int plane;
  692. int ret;
  693. if (V4L2_TYPE_IS_MULTIPLANAR(b->type)) {
  694. /*
  695. * Verify that the userspace gave us a valid array for
  696. * plane information.
  697. */
  698. ret = __verify_planes_array(vb, b);
  699. if (ret)
  700. return ret;
  701. /* Fill in driver-provided information for OUTPUT types */
  702. if (V4L2_TYPE_IS_OUTPUT(b->type)) {
  703. /*
  704. * Will have to go up to b->length when API starts
  705. * accepting variable number of planes.
  706. */
  707. for (plane = 0; plane < vb->num_planes; ++plane) {
  708. v4l2_planes[plane].bytesused =
  709. b->m.planes[plane].bytesused;
  710. v4l2_planes[plane].data_offset =
  711. b->m.planes[plane].data_offset;
  712. }
  713. }
  714. if (b->memory == V4L2_MEMORY_USERPTR) {
  715. for (plane = 0; plane < vb->num_planes; ++plane) {
  716. v4l2_planes[plane].m.userptr =
  717. b->m.planes[plane].m.userptr;
  718. v4l2_planes[plane].length =
  719. b->m.planes[plane].length;
  720. }
  721. }
  722. } else {
  723. /*
  724. * Single-planar buffers do not use planes array,
  725. * so fill in relevant v4l2_buffer struct fields instead.
  726. * In videobuf we use our internal V4l2_planes struct for
  727. * single-planar buffers as well, for simplicity.
  728. */
  729. if (V4L2_TYPE_IS_OUTPUT(b->type))
  730. v4l2_planes[0].bytesused = b->bytesused;
  731. if (b->memory == V4L2_MEMORY_USERPTR) {
  732. v4l2_planes[0].m.userptr = b->m.userptr;
  733. v4l2_planes[0].length = b->length;
  734. }
  735. }
  736. vb->v4l2_buf.field = b->field;
  737. vb->v4l2_buf.timestamp = b->timestamp;
  738. vb->v4l2_buf.input = b->input;
  739. vb->v4l2_buf.flags = b->flags & ~V4L2_BUFFER_STATE_FLAGS;
  740. return 0;
  741. }
  742. /**
  743. * __qbuf_userptr() - handle qbuf of a USERPTR buffer
  744. */
  745. static int __qbuf_userptr(struct vb2_buffer *vb, const struct v4l2_buffer *b)
  746. {
  747. struct v4l2_plane planes[VIDEO_MAX_PLANES];
  748. struct vb2_queue *q = vb->vb2_queue;
  749. void *mem_priv;
  750. unsigned int plane;
  751. int ret;
  752. int write = !V4L2_TYPE_IS_OUTPUT(q->type);
  753. /* Verify and copy relevant information provided by the userspace */
  754. ret = __fill_vb2_buffer(vb, b, planes);
  755. if (ret)
  756. return ret;
  757. for (plane = 0; plane < vb->num_planes; ++plane) {
  758. /* Skip the plane if already verified */
  759. if (vb->v4l2_planes[plane].m.userptr == planes[plane].m.userptr
  760. && vb->v4l2_planes[plane].length == planes[plane].length)
  761. continue;
  762. dprintk(3, "qbuf: userspace address for plane %d changed, "
  763. "reacquiring memory\n", plane);
  764. /* Check if the provided plane buffer is large enough */
  765. if (planes[plane].length < q->plane_sizes[plane]) {
  766. ret = -EINVAL;
  767. goto err;
  768. }
  769. /* Release previously acquired memory if present */
  770. if (vb->planes[plane].mem_priv)
  771. call_memop(q, plane, put_userptr,
  772. vb->planes[plane].mem_priv);
  773. vb->planes[plane].mem_priv = NULL;
  774. vb->v4l2_planes[plane].m.userptr = 0;
  775. vb->v4l2_planes[plane].length = 0;
  776. /* Acquire each plane's memory */
  777. if (q->mem_ops->get_userptr) {
  778. mem_priv = q->mem_ops->get_userptr(q->alloc_ctx[plane],
  779. planes[plane].m.userptr,
  780. planes[plane].length,
  781. write);
  782. if (IS_ERR(mem_priv)) {
  783. dprintk(1, "qbuf: failed acquiring userspace "
  784. "memory for plane %d\n", plane);
  785. ret = PTR_ERR(mem_priv);
  786. goto err;
  787. }
  788. vb->planes[plane].mem_priv = mem_priv;
  789. }
  790. }
  791. /*
  792. * Call driver-specific initialization on the newly acquired buffer,
  793. * if provided.
  794. */
  795. ret = call_qop(q, buf_init, vb);
  796. if (ret) {
  797. dprintk(1, "qbuf: buffer initialization failed\n");
  798. goto err;
  799. }
  800. /*
  801. * Now that everything is in order, copy relevant information
  802. * provided by userspace.
  803. */
  804. for (plane = 0; plane < vb->num_planes; ++plane)
  805. vb->v4l2_planes[plane] = planes[plane];
  806. return 0;
  807. err:
  808. /* In case of errors, release planes that were already acquired */
  809. for (plane = 0; plane < vb->num_planes; ++plane) {
  810. if (vb->planes[plane].mem_priv)
  811. call_memop(q, plane, put_userptr,
  812. vb->planes[plane].mem_priv);
  813. vb->planes[plane].mem_priv = NULL;
  814. vb->v4l2_planes[plane].m.userptr = 0;
  815. vb->v4l2_planes[plane].length = 0;
  816. }
  817. return ret;
  818. }
  819. /**
  820. * __qbuf_mmap() - handle qbuf of an MMAP buffer
  821. */
  822. static int __qbuf_mmap(struct vb2_buffer *vb, const struct v4l2_buffer *b)
  823. {
  824. return __fill_vb2_buffer(vb, b, vb->v4l2_planes);
  825. }
  826. /**
  827. * __enqueue_in_driver() - enqueue a vb2_buffer in driver for processing
  828. */
  829. static void __enqueue_in_driver(struct vb2_buffer *vb)
  830. {
  831. struct vb2_queue *q = vb->vb2_queue;
  832. vb->state = VB2_BUF_STATE_ACTIVE;
  833. atomic_inc(&q->queued_count);
  834. q->ops->buf_queue(vb);
  835. }
  836. static int __buf_prepare(struct vb2_buffer *vb, const struct v4l2_buffer *b)
  837. {
  838. struct vb2_queue *q = vb->vb2_queue;
  839. int ret;
  840. switch (q->memory) {
  841. case V4L2_MEMORY_MMAP:
  842. ret = __qbuf_mmap(vb, b);
  843. break;
  844. case V4L2_MEMORY_USERPTR:
  845. ret = __qbuf_userptr(vb, b);
  846. break;
  847. default:
  848. WARN(1, "Invalid queue type\n");
  849. ret = -EINVAL;
  850. }
  851. if (!ret)
  852. ret = call_qop(q, buf_prepare, vb);
  853. if (ret)
  854. dprintk(1, "qbuf: buffer preparation failed: %d\n", ret);
  855. else
  856. vb->state = VB2_BUF_STATE_PREPARED;
  857. return ret;
  858. }
  859. /**
  860. * vb2_prepare_buf() - Pass ownership of a buffer from userspace to the kernel
  861. * @q: videobuf2 queue
  862. * @b: buffer structure passed from userspace to vidioc_prepare_buf
  863. * handler in driver
  864. *
  865. * Should be called from vidioc_prepare_buf ioctl handler of a driver.
  866. * This function:
  867. * 1) verifies the passed buffer,
  868. * 2) calls buf_prepare callback in the driver (if provided), in which
  869. * driver-specific buffer initialization can be performed,
  870. *
  871. * The return values from this function are intended to be directly returned
  872. * from vidioc_prepare_buf handler in driver.
  873. */
  874. int vb2_prepare_buf(struct vb2_queue *q, struct v4l2_buffer *b)
  875. {
  876. struct vb2_buffer *vb;
  877. int ret;
  878. if (q->fileio) {
  879. dprintk(1, "%s(): file io in progress\n", __func__);
  880. return -EBUSY;
  881. }
  882. if (b->type != q->type) {
  883. dprintk(1, "%s(): invalid buffer type\n", __func__);
  884. return -EINVAL;
  885. }
  886. if (b->index >= q->num_buffers) {
  887. dprintk(1, "%s(): buffer index out of range\n", __func__);
  888. return -EINVAL;
  889. }
  890. vb = q->bufs[b->index];
  891. if (NULL == vb) {
  892. /* Should never happen */
  893. dprintk(1, "%s(): buffer is NULL\n", __func__);
  894. return -EINVAL;
  895. }
  896. if (b->memory != q->memory) {
  897. dprintk(1, "%s(): invalid memory type\n", __func__);
  898. return -EINVAL;
  899. }
  900. if (vb->state != VB2_BUF_STATE_DEQUEUED) {
  901. dprintk(1, "%s(): invalid buffer state %d\n", __func__, vb->state);
  902. return -EINVAL;
  903. }
  904. ret = __buf_prepare(vb, b);
  905. if (ret < 0)
  906. return ret;
  907. __fill_v4l2_buffer(vb, b);
  908. return 0;
  909. }
  910. EXPORT_SYMBOL_GPL(vb2_prepare_buf);
  911. /**
  912. * vb2_qbuf() - Queue a buffer from userspace
  913. * @q: videobuf2 queue
  914. * @b: buffer structure passed from userspace to vidioc_qbuf handler
  915. * in driver
  916. *
  917. * Should be called from vidioc_qbuf ioctl handler of a driver.
  918. * This function:
  919. * 1) verifies the passed buffer,
  920. * 2) if necessary, calls buf_prepare callback in the driver (if provided), in
  921. * which driver-specific buffer initialization can be performed,
  922. * 3) if streaming is on, queues the buffer in driver by the means of buf_queue
  923. * callback for processing.
  924. *
  925. * The return values from this function are intended to be directly returned
  926. * from vidioc_qbuf handler in driver.
  927. */
  928. int vb2_qbuf(struct vb2_queue *q, struct v4l2_buffer *b)
  929. {
  930. struct vb2_buffer *vb;
  931. int ret;
  932. if (q->fileio) {
  933. dprintk(1, "qbuf: file io in progress\n");
  934. return -EBUSY;
  935. }
  936. if (b->type != q->type) {
  937. dprintk(1, "qbuf: invalid buffer type\n");
  938. return -EINVAL;
  939. }
  940. if (b->index >= q->num_buffers) {
  941. dprintk(1, "qbuf: buffer index out of range\n");
  942. return -EINVAL;
  943. }
  944. vb = q->bufs[b->index];
  945. if (NULL == vb) {
  946. /* Should never happen */
  947. dprintk(1, "qbuf: buffer is NULL\n");
  948. return -EINVAL;
  949. }
  950. if (b->memory != q->memory) {
  951. dprintk(1, "qbuf: invalid memory type\n");
  952. return -EINVAL;
  953. }
  954. switch (vb->state) {
  955. case VB2_BUF_STATE_DEQUEUED:
  956. ret = __buf_prepare(vb, b);
  957. if (ret)
  958. return ret;
  959. case VB2_BUF_STATE_PREPARED:
  960. break;
  961. default:
  962. dprintk(1, "qbuf: buffer already in use\n");
  963. return -EINVAL;
  964. }
  965. /*
  966. * Add to the queued buffers list, a buffer will stay on it until
  967. * dequeued in dqbuf.
  968. */
  969. list_add_tail(&vb->queued_entry, &q->queued_list);
  970. vb->state = VB2_BUF_STATE_QUEUED;
  971. /*
  972. * If already streaming, give the buffer to driver for processing.
  973. * If not, the buffer will be given to driver on next streamon.
  974. */
  975. if (q->streaming)
  976. __enqueue_in_driver(vb);
  977. /* Fill buffer information for the userspace */
  978. __fill_v4l2_buffer(vb, b);
  979. dprintk(1, "qbuf of buffer %d succeeded\n", vb->v4l2_buf.index);
  980. return 0;
  981. }
  982. EXPORT_SYMBOL_GPL(vb2_qbuf);
  983. /**
  984. * __vb2_wait_for_done_vb() - wait for a buffer to become available
  985. * for dequeuing
  986. *
  987. * Will sleep if required for nonblocking == false.
  988. */
  989. static int __vb2_wait_for_done_vb(struct vb2_queue *q, int nonblocking)
  990. {
  991. /*
  992. * All operations on vb_done_list are performed under done_lock
  993. * spinlock protection. However, buffers may be removed from
  994. * it and returned to userspace only while holding both driver's
  995. * lock and the done_lock spinlock. Thus we can be sure that as
  996. * long as we hold the driver's lock, the list will remain not
  997. * empty if list_empty() check succeeds.
  998. */
  999. for (;;) {
  1000. int ret;
  1001. if (!q->streaming) {
  1002. dprintk(1, "Streaming off, will not wait for buffers\n");
  1003. return -EINVAL;
  1004. }
  1005. if (!list_empty(&q->done_list)) {
  1006. /*
  1007. * Found a buffer that we were waiting for.
  1008. */
  1009. break;
  1010. }
  1011. if (nonblocking) {
  1012. dprintk(1, "Nonblocking and no buffers to dequeue, "
  1013. "will not wait\n");
  1014. return -EAGAIN;
  1015. }
  1016. /*
  1017. * We are streaming and blocking, wait for another buffer to
  1018. * become ready or for streamoff. Driver's lock is released to
  1019. * allow streamoff or qbuf to be called while waiting.
  1020. */
  1021. call_qop(q, wait_prepare, q);
  1022. /*
  1023. * All locks have been released, it is safe to sleep now.
  1024. */
  1025. dprintk(3, "Will sleep waiting for buffers\n");
  1026. ret = wait_event_interruptible(q->done_wq,
  1027. !list_empty(&q->done_list) || !q->streaming);
  1028. /*
  1029. * We need to reevaluate both conditions again after reacquiring
  1030. * the locks or return an error if one occurred.
  1031. */
  1032. call_qop(q, wait_finish, q);
  1033. if (ret)
  1034. return ret;
  1035. }
  1036. return 0;
  1037. }
  1038. /**
  1039. * __vb2_get_done_vb() - get a buffer ready for dequeuing
  1040. *
  1041. * Will sleep if required for nonblocking == false.
  1042. */
  1043. static int __vb2_get_done_vb(struct vb2_queue *q, struct vb2_buffer **vb,
  1044. int nonblocking)
  1045. {
  1046. unsigned long flags;
  1047. int ret;
  1048. /*
  1049. * Wait for at least one buffer to become available on the done_list.
  1050. */
  1051. ret = __vb2_wait_for_done_vb(q, nonblocking);
  1052. if (ret)
  1053. return ret;
  1054. /*
  1055. * Driver's lock has been held since we last verified that done_list
  1056. * is not empty, so no need for another list_empty(done_list) check.
  1057. */
  1058. spin_lock_irqsave(&q->done_lock, flags);
  1059. *vb = list_first_entry(&q->done_list, struct vb2_buffer, done_entry);
  1060. list_del(&(*vb)->done_entry);
  1061. spin_unlock_irqrestore(&q->done_lock, flags);
  1062. return 0;
  1063. }
  1064. /**
  1065. * vb2_wait_for_all_buffers() - wait until all buffers are given back to vb2
  1066. * @q: videobuf2 queue
  1067. *
  1068. * This function will wait until all buffers that have been given to the driver
  1069. * by buf_queue() are given back to vb2 with vb2_buffer_done(). It doesn't call
  1070. * wait_prepare, wait_finish pair. It is intended to be called with all locks
  1071. * taken, for example from stop_streaming() callback.
  1072. */
  1073. int vb2_wait_for_all_buffers(struct vb2_queue *q)
  1074. {
  1075. if (!q->streaming) {
  1076. dprintk(1, "Streaming off, will not wait for buffers\n");
  1077. return -EINVAL;
  1078. }
  1079. wait_event(q->done_wq, !atomic_read(&q->queued_count));
  1080. return 0;
  1081. }
  1082. EXPORT_SYMBOL_GPL(vb2_wait_for_all_buffers);
  1083. /**
  1084. * vb2_dqbuf() - Dequeue a buffer to the userspace
  1085. * @q: videobuf2 queue
  1086. * @b: buffer structure passed from userspace to vidioc_dqbuf handler
  1087. * in driver
  1088. * @nonblocking: if true, this call will not sleep waiting for a buffer if no
  1089. * buffers ready for dequeuing are present. Normally the driver
  1090. * would be passing (file->f_flags & O_NONBLOCK) here
  1091. *
  1092. * Should be called from vidioc_dqbuf ioctl handler of a driver.
  1093. * This function:
  1094. * 1) verifies the passed buffer,
  1095. * 2) calls buf_finish callback in the driver (if provided), in which
  1096. * driver can perform any additional operations that may be required before
  1097. * returning the buffer to userspace, such as cache sync,
  1098. * 3) the buffer struct members are filled with relevant information for
  1099. * the userspace.
  1100. *
  1101. * The return values from this function are intended to be directly returned
  1102. * from vidioc_dqbuf handler in driver.
  1103. */
  1104. int vb2_dqbuf(struct vb2_queue *q, struct v4l2_buffer *b, bool nonblocking)
  1105. {
  1106. struct vb2_buffer *vb = NULL;
  1107. int ret;
  1108. if (q->fileio) {
  1109. dprintk(1, "dqbuf: file io in progress\n");
  1110. return -EBUSY;
  1111. }
  1112. if (b->type != q->type) {
  1113. dprintk(1, "dqbuf: invalid buffer type\n");
  1114. return -EINVAL;
  1115. }
  1116. ret = __vb2_get_done_vb(q, &vb, nonblocking);
  1117. if (ret < 0) {
  1118. dprintk(1, "dqbuf: error getting next done buffer\n");
  1119. return ret;
  1120. }
  1121. ret = call_qop(q, buf_finish, vb);
  1122. if (ret) {
  1123. dprintk(1, "dqbuf: buffer finish failed\n");
  1124. return ret;
  1125. }
  1126. switch (vb->state) {
  1127. case VB2_BUF_STATE_DONE:
  1128. dprintk(3, "dqbuf: Returning done buffer\n");
  1129. break;
  1130. case VB2_BUF_STATE_ERROR:
  1131. dprintk(3, "dqbuf: Returning done buffer with errors\n");
  1132. break;
  1133. default:
  1134. dprintk(1, "dqbuf: Invalid buffer state\n");
  1135. return -EINVAL;
  1136. }
  1137. /* Fill buffer information for the userspace */
  1138. __fill_v4l2_buffer(vb, b);
  1139. /* Remove from videobuf queue */
  1140. list_del(&vb->queued_entry);
  1141. dprintk(1, "dqbuf of buffer %d, with state %d\n",
  1142. vb->v4l2_buf.index, vb->state);
  1143. vb->state = VB2_BUF_STATE_DEQUEUED;
  1144. return 0;
  1145. }
  1146. EXPORT_SYMBOL_GPL(vb2_dqbuf);
  1147. /**
  1148. * __vb2_queue_cancel() - cancel and stop (pause) streaming
  1149. *
  1150. * Removes all queued buffers from driver's queue and all buffers queued by
  1151. * userspace from videobuf's queue. Returns to state after reqbufs.
  1152. */
  1153. static void __vb2_queue_cancel(struct vb2_queue *q)
  1154. {
  1155. unsigned int i;
  1156. /*
  1157. * Tell driver to stop all transactions and release all queued
  1158. * buffers.
  1159. */
  1160. if (q->streaming)
  1161. call_qop(q, stop_streaming, q);
  1162. q->streaming = 0;
  1163. /*
  1164. * Remove all buffers from videobuf's list...
  1165. */
  1166. INIT_LIST_HEAD(&q->queued_list);
  1167. /*
  1168. * ...and done list; userspace will not receive any buffers it
  1169. * has not already dequeued before initiating cancel.
  1170. */
  1171. INIT_LIST_HEAD(&q->done_list);
  1172. atomic_set(&q->queued_count, 0);
  1173. wake_up_all(&q->done_wq);
  1174. /*
  1175. * Reinitialize all buffers for next use.
  1176. */
  1177. for (i = 0; i < q->num_buffers; ++i)
  1178. q->bufs[i]->state = VB2_BUF_STATE_DEQUEUED;
  1179. }
  1180. /**
  1181. * vb2_streamon - start streaming
  1182. * @q: videobuf2 queue
  1183. * @type: type argument passed from userspace to vidioc_streamon handler
  1184. *
  1185. * Should be called from vidioc_streamon handler of a driver.
  1186. * This function:
  1187. * 1) verifies current state
  1188. * 2) passes any previously queued buffers to the driver and starts streaming
  1189. *
  1190. * The return values from this function are intended to be directly returned
  1191. * from vidioc_streamon handler in the driver.
  1192. */
  1193. int vb2_streamon(struct vb2_queue *q, enum v4l2_buf_type type)
  1194. {
  1195. struct vb2_buffer *vb;
  1196. int ret;
  1197. if (q->fileio) {
  1198. dprintk(1, "streamon: file io in progress\n");
  1199. return -EBUSY;
  1200. }
  1201. if (type != q->type) {
  1202. dprintk(1, "streamon: invalid stream type\n");
  1203. return -EINVAL;
  1204. }
  1205. if (q->streaming) {
  1206. dprintk(1, "streamon: already streaming\n");
  1207. return -EBUSY;
  1208. }
  1209. /*
  1210. * If any buffers were queued before streamon,
  1211. * we can now pass them to driver for processing.
  1212. */
  1213. list_for_each_entry(vb, &q->queued_list, queued_entry)
  1214. __enqueue_in_driver(vb);
  1215. /*
  1216. * Let driver notice that streaming state has been enabled.
  1217. */
  1218. ret = call_qop(q, start_streaming, q, atomic_read(&q->queued_count));
  1219. if (ret) {
  1220. dprintk(1, "streamon: driver refused to start streaming\n");
  1221. __vb2_queue_cancel(q);
  1222. return ret;
  1223. }
  1224. q->streaming = 1;
  1225. dprintk(3, "Streamon successful\n");
  1226. return 0;
  1227. }
  1228. EXPORT_SYMBOL_GPL(vb2_streamon);
  1229. /**
  1230. * vb2_streamoff - stop streaming
  1231. * @q: videobuf2 queue
  1232. * @type: type argument passed from userspace to vidioc_streamoff handler
  1233. *
  1234. * Should be called from vidioc_streamoff handler of a driver.
  1235. * This function:
  1236. * 1) verifies current state,
  1237. * 2) stop streaming and dequeues any queued buffers, including those previously
  1238. * passed to the driver (after waiting for the driver to finish).
  1239. *
  1240. * This call can be used for pausing playback.
  1241. * The return values from this function are intended to be directly returned
  1242. * from vidioc_streamoff handler in the driver
  1243. */
  1244. int vb2_streamoff(struct vb2_queue *q, enum v4l2_buf_type type)
  1245. {
  1246. if (q->fileio) {
  1247. dprintk(1, "streamoff: file io in progress\n");
  1248. return -EBUSY;
  1249. }
  1250. if (type != q->type) {
  1251. dprintk(1, "streamoff: invalid stream type\n");
  1252. return -EINVAL;
  1253. }
  1254. if (!q->streaming) {
  1255. dprintk(1, "streamoff: not streaming\n");
  1256. return -EINVAL;
  1257. }
  1258. /*
  1259. * Cancel will pause streaming and remove all buffers from the driver
  1260. * and videobuf, effectively returning control over them to userspace.
  1261. */
  1262. __vb2_queue_cancel(q);
  1263. dprintk(3, "Streamoff successful\n");
  1264. return 0;
  1265. }
  1266. EXPORT_SYMBOL_GPL(vb2_streamoff);
  1267. /**
  1268. * __find_plane_by_offset() - find plane associated with the given offset off
  1269. */
  1270. static int __find_plane_by_offset(struct vb2_queue *q, unsigned long off,
  1271. unsigned int *_buffer, unsigned int *_plane)
  1272. {
  1273. struct vb2_buffer *vb;
  1274. unsigned int buffer, plane;
  1275. /*
  1276. * Go over all buffers and their planes, comparing the given offset
  1277. * with an offset assigned to each plane. If a match is found,
  1278. * return its buffer and plane numbers.
  1279. */
  1280. for (buffer = 0; buffer < q->num_buffers; ++buffer) {
  1281. vb = q->bufs[buffer];
  1282. for (plane = 0; plane < vb->num_planes; ++plane) {
  1283. if (vb->v4l2_planes[plane].m.mem_offset == off) {
  1284. *_buffer = buffer;
  1285. *_plane = plane;
  1286. return 0;
  1287. }
  1288. }
  1289. }
  1290. return -EINVAL;
  1291. }
  1292. /**
  1293. * vb2_mmap() - map video buffers into application address space
  1294. * @q: videobuf2 queue
  1295. * @vma: vma passed to the mmap file operation handler in the driver
  1296. *
  1297. * Should be called from mmap file operation handler of a driver.
  1298. * This function maps one plane of one of the available video buffers to
  1299. * userspace. To map whole video memory allocated on reqbufs, this function
  1300. * has to be called once per each plane per each buffer previously allocated.
  1301. *
  1302. * When the userspace application calls mmap, it passes to it an offset returned
  1303. * to it earlier by the means of vidioc_querybuf handler. That offset acts as
  1304. * a "cookie", which is then used to identify the plane to be mapped.
  1305. * This function finds a plane with a matching offset and a mapping is performed
  1306. * by the means of a provided memory operation.
  1307. *
  1308. * The return values from this function are intended to be directly returned
  1309. * from the mmap handler in driver.
  1310. */
  1311. int vb2_mmap(struct vb2_queue *q, struct vm_area_struct *vma)
  1312. {
  1313. unsigned long off = vma->vm_pgoff << PAGE_SHIFT;
  1314. struct vb2_plane *vb_plane;
  1315. struct vb2_buffer *vb;
  1316. unsigned int buffer, plane;
  1317. int ret;
  1318. if (q->memory != V4L2_MEMORY_MMAP) {
  1319. dprintk(1, "Queue is not currently set up for mmap\n");
  1320. return -EINVAL;
  1321. }
  1322. /*
  1323. * Check memory area access mode.
  1324. */
  1325. if (!(vma->vm_flags & VM_SHARED)) {
  1326. dprintk(1, "Invalid vma flags, VM_SHARED needed\n");
  1327. return -EINVAL;
  1328. }
  1329. if (V4L2_TYPE_IS_OUTPUT(q->type)) {
  1330. if (!(vma->vm_flags & VM_WRITE)) {
  1331. dprintk(1, "Invalid vma flags, VM_WRITE needed\n");
  1332. return -EINVAL;
  1333. }
  1334. } else {
  1335. if (!(vma->vm_flags & VM_READ)) {
  1336. dprintk(1, "Invalid vma flags, VM_READ needed\n");
  1337. return -EINVAL;
  1338. }
  1339. }
  1340. /*
  1341. * Find the plane corresponding to the offset passed by userspace.
  1342. */
  1343. ret = __find_plane_by_offset(q, off, &buffer, &plane);
  1344. if (ret)
  1345. return ret;
  1346. vb = q->bufs[buffer];
  1347. vb_plane = &vb->planes[plane];
  1348. ret = q->mem_ops->mmap(vb_plane->mem_priv, vma);
  1349. if (ret)
  1350. return ret;
  1351. dprintk(3, "Buffer %d, plane %d successfully mapped\n", buffer, plane);
  1352. return 0;
  1353. }
  1354. EXPORT_SYMBOL_GPL(vb2_mmap);
  1355. #ifndef CONFIG_MMU
  1356. unsigned long vb2_get_unmapped_area(struct vb2_queue *q,
  1357. unsigned long addr,
  1358. unsigned long len,
  1359. unsigned long pgoff,
  1360. unsigned long flags)
  1361. {
  1362. unsigned long off = pgoff << PAGE_SHIFT;
  1363. struct vb2_buffer *vb;
  1364. unsigned int buffer, plane;
  1365. int ret;
  1366. if (q->memory != V4L2_MEMORY_MMAP) {
  1367. dprintk(1, "Queue is not currently set up for mmap\n");
  1368. return -EINVAL;
  1369. }
  1370. /*
  1371. * Find the plane corresponding to the offset passed by userspace.
  1372. */
  1373. ret = __find_plane_by_offset(q, off, &buffer, &plane);
  1374. if (ret)
  1375. return ret;
  1376. vb = q->bufs[buffer];
  1377. return (unsigned long)vb2_plane_vaddr(vb, plane);
  1378. }
  1379. EXPORT_SYMBOL_GPL(vb2_get_unmapped_area);
  1380. #endif
  1381. static int __vb2_init_fileio(struct vb2_queue *q, int read);
  1382. static int __vb2_cleanup_fileio(struct vb2_queue *q);
  1383. /**
  1384. * vb2_poll() - implements poll userspace operation
  1385. * @q: videobuf2 queue
  1386. * @file: file argument passed to the poll file operation handler
  1387. * @wait: wait argument passed to the poll file operation handler
  1388. *
  1389. * This function implements poll file operation handler for a driver.
  1390. * For CAPTURE queues, if a buffer is ready to be dequeued, the userspace will
  1391. * be informed that the file descriptor of a video device is available for
  1392. * reading.
  1393. * For OUTPUT queues, if a buffer is ready to be dequeued, the file descriptor
  1394. * will be reported as available for writing.
  1395. *
  1396. * The return values from this function are intended to be directly returned
  1397. * from poll handler in driver.
  1398. */
  1399. unsigned int vb2_poll(struct vb2_queue *q, struct file *file, poll_table *wait)
  1400. {
  1401. unsigned long flags;
  1402. unsigned int ret;
  1403. struct vb2_buffer *vb = NULL;
  1404. /*
  1405. * Start file I/O emulator only if streaming API has not been used yet.
  1406. */
  1407. if (q->num_buffers == 0 && q->fileio == NULL) {
  1408. if (!V4L2_TYPE_IS_OUTPUT(q->type) && (q->io_modes & VB2_READ)) {
  1409. ret = __vb2_init_fileio(q, 1);
  1410. if (ret)
  1411. return POLLERR;
  1412. }
  1413. if (V4L2_TYPE_IS_OUTPUT(q->type) && (q->io_modes & VB2_WRITE)) {
  1414. ret = __vb2_init_fileio(q, 0);
  1415. if (ret)
  1416. return POLLERR;
  1417. /*
  1418. * Write to OUTPUT queue can be done immediately.
  1419. */
  1420. return POLLOUT | POLLWRNORM;
  1421. }
  1422. }
  1423. /*
  1424. * There is nothing to wait for if no buffers have already been queued.
  1425. */
  1426. if (list_empty(&q->queued_list))
  1427. return POLLERR;
  1428. poll_wait(file, &q->done_wq, wait);
  1429. /*
  1430. * Take first buffer available for dequeuing.
  1431. */
  1432. spin_lock_irqsave(&q->done_lock, flags);
  1433. if (!list_empty(&q->done_list))
  1434. vb = list_first_entry(&q->done_list, struct vb2_buffer,
  1435. done_entry);
  1436. spin_unlock_irqrestore(&q->done_lock, flags);
  1437. if (vb && (vb->state == VB2_BUF_STATE_DONE
  1438. || vb->state == VB2_BUF_STATE_ERROR)) {
  1439. return (V4L2_TYPE_IS_OUTPUT(q->type)) ? POLLOUT | POLLWRNORM :
  1440. POLLIN | POLLRDNORM;
  1441. }
  1442. return 0;
  1443. }
  1444. EXPORT_SYMBOL_GPL(vb2_poll);
  1445. /**
  1446. * vb2_queue_init() - initialize a videobuf2 queue
  1447. * @q: videobuf2 queue; this structure should be allocated in driver
  1448. *
  1449. * The vb2_queue structure should be allocated by the driver. The driver is
  1450. * responsible of clearing it's content and setting initial values for some
  1451. * required entries before calling this function.
  1452. * q->ops, q->mem_ops, q->type and q->io_modes are mandatory. Please refer
  1453. * to the struct vb2_queue description in include/media/videobuf2-core.h
  1454. * for more information.
  1455. */
  1456. int vb2_queue_init(struct vb2_queue *q)
  1457. {
  1458. BUG_ON(!q);
  1459. BUG_ON(!q->ops);
  1460. BUG_ON(!q->mem_ops);
  1461. BUG_ON(!q->type);
  1462. BUG_ON(!q->io_modes);
  1463. BUG_ON(!q->ops->queue_setup);
  1464. BUG_ON(!q->ops->buf_queue);
  1465. INIT_LIST_HEAD(&q->queued_list);
  1466. INIT_LIST_HEAD(&q->done_list);
  1467. spin_lock_init(&q->done_lock);
  1468. init_waitqueue_head(&q->done_wq);
  1469. if (q->buf_struct_size == 0)
  1470. q->buf_struct_size = sizeof(struct vb2_buffer);
  1471. return 0;
  1472. }
  1473. EXPORT_SYMBOL_GPL(vb2_queue_init);
  1474. /**
  1475. * vb2_queue_release() - stop streaming, release the queue and free memory
  1476. * @q: videobuf2 queue
  1477. *
  1478. * This function stops streaming and performs necessary clean ups, including
  1479. * freeing video buffer memory. The driver is responsible for freeing
  1480. * the vb2_queue structure itself.
  1481. */
  1482. void vb2_queue_release(struct vb2_queue *q)
  1483. {
  1484. __vb2_cleanup_fileio(q);
  1485. __vb2_queue_cancel(q);
  1486. __vb2_queue_free(q, q->num_buffers);
  1487. }
  1488. EXPORT_SYMBOL_GPL(vb2_queue_release);
  1489. /**
  1490. * struct vb2_fileio_buf - buffer context used by file io emulator
  1491. *
  1492. * vb2 provides a compatibility layer and emulator of file io (read and
  1493. * write) calls on top of streaming API. This structure is used for
  1494. * tracking context related to the buffers.
  1495. */
  1496. struct vb2_fileio_buf {
  1497. void *vaddr;
  1498. unsigned int size;
  1499. unsigned int pos;
  1500. unsigned int queued:1;
  1501. };
  1502. /**
  1503. * struct vb2_fileio_data - queue context used by file io emulator
  1504. *
  1505. * vb2 provides a compatibility layer and emulator of file io (read and
  1506. * write) calls on top of streaming API. For proper operation it required
  1507. * this structure to save the driver state between each call of the read
  1508. * or write function.
  1509. */
  1510. struct vb2_fileio_data {
  1511. struct v4l2_requestbuffers req;
  1512. struct v4l2_buffer b;
  1513. struct vb2_fileio_buf bufs[VIDEO_MAX_FRAME];
  1514. unsigned int index;
  1515. unsigned int q_count;
  1516. unsigned int dq_count;
  1517. unsigned int flags;
  1518. };
  1519. /**
  1520. * __vb2_init_fileio() - initialize file io emulator
  1521. * @q: videobuf2 queue
  1522. * @read: mode selector (1 means read, 0 means write)
  1523. */
  1524. static int __vb2_init_fileio(struct vb2_queue *q, int read)
  1525. {
  1526. struct vb2_fileio_data *fileio;
  1527. int i, ret;
  1528. unsigned int count = 0;
  1529. /*
  1530. * Sanity check
  1531. */
  1532. if ((read && !(q->io_modes & VB2_READ)) ||
  1533. (!read && !(q->io_modes & VB2_WRITE)))
  1534. BUG();
  1535. /*
  1536. * Check if device supports mapping buffers to kernel virtual space.
  1537. */
  1538. if (!q->mem_ops->vaddr)
  1539. return -EBUSY;
  1540. /*
  1541. * Check if streaming api has not been already activated.
  1542. */
  1543. if (q->streaming || q->num_buffers > 0)
  1544. return -EBUSY;
  1545. /*
  1546. * Start with count 1, driver can increase it in queue_setup()
  1547. */
  1548. count = 1;
  1549. dprintk(3, "setting up file io: mode %s, count %d, flags %08x\n",
  1550. (read) ? "read" : "write", count, q->io_flags);
  1551. fileio = kzalloc(sizeof(struct vb2_fileio_data), GFP_KERNEL);
  1552. if (fileio == NULL)
  1553. return -ENOMEM;
  1554. fileio->flags = q->io_flags;
  1555. /*
  1556. * Request buffers and use MMAP type to force driver
  1557. * to allocate buffers by itself.
  1558. */
  1559. fileio->req.count = count;
  1560. fileio->req.memory = V4L2_MEMORY_MMAP;
  1561. fileio->req.type = q->type;
  1562. ret = vb2_reqbufs(q, &fileio->req);
  1563. if (ret)
  1564. goto err_kfree;
  1565. /*
  1566. * Check if plane_count is correct
  1567. * (multiplane buffers are not supported).
  1568. */
  1569. if (q->bufs[0]->num_planes != 1) {
  1570. fileio->req.count = 0;
  1571. ret = -EBUSY;
  1572. goto err_reqbufs;
  1573. }
  1574. /*
  1575. * Get kernel address of each buffer.
  1576. */
  1577. for (i = 0; i < q->num_buffers; i++) {
  1578. fileio->bufs[i].vaddr = vb2_plane_vaddr(q->bufs[i], 0);
  1579. if (fileio->bufs[i].vaddr == NULL)
  1580. goto err_reqbufs;
  1581. fileio->bufs[i].size = vb2_plane_size(q->bufs[i], 0);
  1582. }
  1583. /*
  1584. * Read mode requires pre queuing of all buffers.
  1585. */
  1586. if (read) {
  1587. /*
  1588. * Queue all buffers.
  1589. */
  1590. for (i = 0; i < q->num_buffers; i++) {
  1591. struct v4l2_buffer *b = &fileio->b;
  1592. memset(b, 0, sizeof(*b));
  1593. b->type = q->type;
  1594. b->memory = q->memory;
  1595. b->index = i;
  1596. ret = vb2_qbuf(q, b);
  1597. if (ret)
  1598. goto err_reqbufs;
  1599. fileio->bufs[i].queued = 1;
  1600. }
  1601. /*
  1602. * Start streaming.
  1603. */
  1604. ret = vb2_streamon(q, q->type);
  1605. if (ret)
  1606. goto err_reqbufs;
  1607. }
  1608. q->fileio = fileio;
  1609. return ret;
  1610. err_reqbufs:
  1611. vb2_reqbufs(q, &fileio->req);
  1612. err_kfree:
  1613. kfree(fileio);
  1614. return ret;
  1615. }
  1616. /**
  1617. * __vb2_cleanup_fileio() - free resourced used by file io emulator
  1618. * @q: videobuf2 queue
  1619. */
  1620. static int __vb2_cleanup_fileio(struct vb2_queue *q)
  1621. {
  1622. struct vb2_fileio_data *fileio = q->fileio;
  1623. if (fileio) {
  1624. /*
  1625. * Hack fileio context to enable direct calls to vb2 ioctl
  1626. * interface.
  1627. */
  1628. q->fileio = NULL;
  1629. vb2_streamoff(q, q->type);
  1630. fileio->req.count = 0;
  1631. vb2_reqbufs(q, &fileio->req);
  1632. kfree(fileio);
  1633. dprintk(3, "file io emulator closed\n");
  1634. }
  1635. return 0;
  1636. }
  1637. /**
  1638. * __vb2_perform_fileio() - perform a single file io (read or write) operation
  1639. * @q: videobuf2 queue
  1640. * @data: pointed to target userspace buffer
  1641. * @count: number of bytes to read or write
  1642. * @ppos: file handle position tracking pointer
  1643. * @nonblock: mode selector (1 means blocking calls, 0 means nonblocking)
  1644. * @read: access mode selector (1 means read, 0 means write)
  1645. */
  1646. static size_t __vb2_perform_fileio(struct vb2_queue *q, char __user *data, size_t count,
  1647. loff_t *ppos, int nonblock, int read)
  1648. {
  1649. struct vb2_fileio_data *fileio;
  1650. struct vb2_fileio_buf *buf;
  1651. int ret, index;
  1652. dprintk(3, "file io: mode %s, offset %ld, count %zd, %sblocking\n",
  1653. read ? "read" : "write", (long)*ppos, count,
  1654. nonblock ? "non" : "");
  1655. if (!data)
  1656. return -EINVAL;
  1657. /*
  1658. * Initialize emulator on first call.
  1659. */
  1660. if (!q->fileio) {
  1661. ret = __vb2_init_fileio(q, read);
  1662. dprintk(3, "file io: vb2_init_fileio result: %d\n", ret);
  1663. if (ret)
  1664. return ret;
  1665. }
  1666. fileio = q->fileio;
  1667. /*
  1668. * Hack fileio context to enable direct calls to vb2 ioctl interface.
  1669. * The pointer will be restored before returning from this function.
  1670. */
  1671. q->fileio = NULL;
  1672. index = fileio->index;
  1673. buf = &fileio->bufs[index];
  1674. /*
  1675. * Check if we need to dequeue the buffer.
  1676. */
  1677. if (buf->queued) {
  1678. struct vb2_buffer *vb;
  1679. /*
  1680. * Call vb2_dqbuf to get buffer back.
  1681. */
  1682. memset(&fileio->b, 0, sizeof(fileio->b));
  1683. fileio->b.type = q->type;
  1684. fileio->b.memory = q->memory;
  1685. fileio->b.index = index;
  1686. ret = vb2_dqbuf(q, &fileio->b, nonblock);
  1687. dprintk(5, "file io: vb2_dqbuf result: %d\n", ret);
  1688. if (ret)
  1689. goto end;
  1690. fileio->dq_count += 1;
  1691. /*
  1692. * Get number of bytes filled by the driver
  1693. */
  1694. vb = q->bufs[index];
  1695. buf->size = vb2_get_plane_payload(vb, 0);
  1696. buf->queued = 0;
  1697. }
  1698. /*
  1699. * Limit count on last few bytes of the buffer.
  1700. */
  1701. if (buf->pos + count > buf->size) {
  1702. count = buf->size - buf->pos;
  1703. dprintk(5, "reducing read count: %zd\n", count);
  1704. }
  1705. /*
  1706. * Transfer data to userspace.
  1707. */
  1708. dprintk(3, "file io: copying %zd bytes - buffer %d, offset %u\n",
  1709. count, index, buf->pos);
  1710. if (read)
  1711. ret = copy_to_user(data, buf->vaddr + buf->pos, count);
  1712. else
  1713. ret = copy_from_user(buf->vaddr + buf->pos, data, count);
  1714. if (ret) {
  1715. dprintk(3, "file io: error copying data\n");
  1716. ret = -EFAULT;
  1717. goto end;
  1718. }
  1719. /*
  1720. * Update counters.
  1721. */
  1722. buf->pos += count;
  1723. *ppos += count;
  1724. /*
  1725. * Queue next buffer if required.
  1726. */
  1727. if (buf->pos == buf->size ||
  1728. (!read && (fileio->flags & VB2_FILEIO_WRITE_IMMEDIATELY))) {
  1729. /*
  1730. * Check if this is the last buffer to read.
  1731. */
  1732. if (read && (fileio->flags & VB2_FILEIO_READ_ONCE) &&
  1733. fileio->dq_count == 1) {
  1734. dprintk(3, "file io: read limit reached\n");
  1735. /*
  1736. * Restore fileio pointer and release the context.
  1737. */
  1738. q->fileio = fileio;
  1739. return __vb2_cleanup_fileio(q);
  1740. }
  1741. /*
  1742. * Call vb2_qbuf and give buffer to the driver.
  1743. */
  1744. memset(&fileio->b, 0, sizeof(fileio->b));
  1745. fileio->b.type = q->type;
  1746. fileio->b.memory = q->memory;
  1747. fileio->b.index = index;
  1748. fileio->b.bytesused = buf->pos;
  1749. ret = vb2_qbuf(q, &fileio->b);
  1750. dprintk(5, "file io: vb2_dbuf result: %d\n", ret);
  1751. if (ret)
  1752. goto end;
  1753. /*
  1754. * Buffer has been queued, update the status
  1755. */
  1756. buf->pos = 0;
  1757. buf->queued = 1;
  1758. buf->size = q->bufs[0]->v4l2_planes[0].length;
  1759. fileio->q_count += 1;
  1760. /*
  1761. * Switch to the next buffer
  1762. */
  1763. fileio->index = (index + 1) % q->num_buffers;
  1764. /*
  1765. * Start streaming if required.
  1766. */
  1767. if (!read && !q->streaming) {
  1768. ret = vb2_streamon(q, q->type);
  1769. if (ret)
  1770. goto end;
  1771. }
  1772. }
  1773. /*
  1774. * Return proper number of bytes processed.
  1775. */
  1776. if (ret == 0)
  1777. ret = count;
  1778. end:
  1779. /*
  1780. * Restore the fileio context and block vb2 ioctl interface.
  1781. */
  1782. q->fileio = fileio;
  1783. return ret;
  1784. }
  1785. size_t vb2_read(struct vb2_queue *q, char __user *data, size_t count,
  1786. loff_t *ppos, int nonblocking)
  1787. {
  1788. return __vb2_perform_fileio(q, data, count, ppos, nonblocking, 1);
  1789. }
  1790. EXPORT_SYMBOL_GPL(vb2_read);
  1791. size_t vb2_write(struct vb2_queue *q, char __user *data, size_t count,
  1792. loff_t *ppos, int nonblocking)
  1793. {
  1794. return __vb2_perform_fileio(q, data, count, ppos, nonblocking, 0);
  1795. }
  1796. EXPORT_SYMBOL_GPL(vb2_write);
  1797. MODULE_DESCRIPTION("Driver helper framework for Video for Linux 2");
  1798. MODULE_AUTHOR("Pawel Osciak <pawel@osciak.com>, Marek Szyprowski");
  1799. MODULE_LICENSE("GPL");