videobuf2-core.c 47 KB

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