videobuf2-core.c 47 KB

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