videobuf2-core.h 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348
  1. /*
  2. * videobuf2-core.h - Video Buffer 2 Core Framework
  3. *
  4. * Copyright (C) 2010 Samsung Electronics
  5. *
  6. * Author: Pawel Osciak <pawel@osciak.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation.
  11. */
  12. #ifndef _MEDIA_VIDEOBUF2_CORE_H
  13. #define _MEDIA_VIDEOBUF2_CORE_H
  14. #include <linux/mm_types.h>
  15. #include <linux/mutex.h>
  16. #include <linux/poll.h>
  17. #include <linux/dma-buf.h>
  18. #include <linux/bitops.h>
  19. #include <media/media-request.h>
  20. #include <media/frame_vector.h>
  21. #define VB2_MAX_FRAME (32)
  22. #define VB2_MAX_PLANES (8)
  23. /**
  24. * enum vb2_memory - type of memory model used to make the buffers visible
  25. * on userspace.
  26. *
  27. * @VB2_MEMORY_UNKNOWN: Buffer status is unknown or it is not used yet on
  28. * userspace.
  29. * @VB2_MEMORY_MMAP: The buffers are allocated by the Kernel and it is
  30. * memory mapped via mmap() ioctl. This model is
  31. * also used when the user is using the buffers via
  32. * read() or write() system calls.
  33. * @VB2_MEMORY_USERPTR: The buffers was allocated in userspace and it is
  34. * memory mapped via mmap() ioctl.
  35. * @VB2_MEMORY_DMABUF: The buffers are passed to userspace via DMA buffer.
  36. */
  37. enum vb2_memory {
  38. VB2_MEMORY_UNKNOWN = 0,
  39. VB2_MEMORY_MMAP = 1,
  40. VB2_MEMORY_USERPTR = 2,
  41. VB2_MEMORY_DMABUF = 4,
  42. };
  43. struct vb2_fileio_data;
  44. struct vb2_threadio_data;
  45. struct vb2_buffer;
  46. /**
  47. * struct vb2_mem_ops - memory handling/memory allocator operations.
  48. * @alloc: allocate video memory and, optionally, allocator private data,
  49. * return ERR_PTR() on failure or a pointer to allocator private,
  50. * per-buffer data on success; the returned private structure
  51. * will then be passed as @buf_priv argument to other ops in this
  52. * structure. The size argument to this function shall be
  53. * *page aligned*.
  54. * @put: inform the allocator that the buffer will no longer be used;
  55. * usually will result in the allocator freeing the buffer (if
  56. * no other users of this buffer are present); the @buf_priv
  57. * argument is the allocator private per-buffer structure
  58. * previously returned from the alloc callback.
  59. * @get_dmabuf: acquire userspace memory for a hardware operation; used for
  60. * DMABUF memory types.
  61. * @get_userptr: acquire userspace memory for a hardware operation; used for
  62. * USERPTR memory types; vaddr is the address passed to the
  63. * videobuf2 layer when queuing a video buffer of USERPTR type;
  64. * should return an allocator private per-buffer structure
  65. * associated with the buffer on success, ERR_PTR() on failure;
  66. * the returned private structure will then be passed as @buf_priv
  67. * argument to other ops in this structure.
  68. * @put_userptr: inform the allocator that a USERPTR buffer will no longer
  69. * be used.
  70. * @prepare: called every time the buffer is passed from userspace to the
  71. * driver, useful for cache synchronisation, optional.
  72. * @finish: called every time the buffer is passed back from the driver
  73. * to the userspace, also optional.
  74. * @attach_dmabuf: attach a shared &struct dma_buf for a hardware operation;
  75. * used for DMABUF memory types; dev is the alloc device
  76. * dbuf is the shared dma_buf; returns ERR_PTR() on failure;
  77. * allocator private per-buffer structure on success;
  78. * this needs to be used for further accesses to the buffer.
  79. * @detach_dmabuf: inform the exporter of the buffer that the current DMABUF
  80. * buffer is no longer used; the @buf_priv argument is the
  81. * allocator private per-buffer structure previously returned
  82. * from the attach_dmabuf callback.
  83. * @map_dmabuf: request for access to the dmabuf from allocator; the allocator
  84. * of dmabuf is informed that this driver is going to use the
  85. * dmabuf.
  86. * @unmap_dmabuf: releases access control to the dmabuf - allocator is notified
  87. * that this driver is done using the dmabuf for now.
  88. * @vaddr: return a kernel virtual address to a given memory buffer
  89. * associated with the passed private structure or NULL if no
  90. * such mapping exists.
  91. * @cookie: return allocator specific cookie for a given memory buffer
  92. * associated with the passed private structure or NULL if not
  93. * available.
  94. * @num_users: return the current number of users of a memory buffer;
  95. * return 1 if the videobuf2 layer (or actually the driver using
  96. * it) is the only user.
  97. * @mmap: setup a userspace mapping for a given memory buffer under
  98. * the provided virtual memory region.
  99. *
  100. * Those operations are used by the videobuf2 core to implement the memory
  101. * handling/memory allocators for each type of supported streaming I/O method.
  102. *
  103. * .. note::
  104. * #) Required ops for USERPTR types: get_userptr, put_userptr.
  105. *
  106. * #) Required ops for MMAP types: alloc, put, num_users, mmap.
  107. *
  108. * #) Required ops for read/write access types: alloc, put, num_users, vaddr.
  109. *
  110. * #) Required ops for DMABUF types: attach_dmabuf, detach_dmabuf,
  111. * map_dmabuf, unmap_dmabuf.
  112. */
  113. struct vb2_mem_ops {
  114. void *(*alloc)(struct vb2_buffer *vb,
  115. struct device *dev,
  116. unsigned long size);
  117. void (*put)(void *buf_priv);
  118. struct dma_buf *(*get_dmabuf)(struct vb2_buffer *vb,
  119. void *buf_priv,
  120. unsigned long flags);
  121. void *(*get_userptr)(struct vb2_buffer *vb,
  122. struct device *dev,
  123. unsigned long vaddr,
  124. unsigned long size);
  125. void (*put_userptr)(void *buf_priv);
  126. void (*prepare)(void *buf_priv);
  127. void (*finish)(void *buf_priv);
  128. void *(*attach_dmabuf)(struct vb2_buffer *vb,
  129. struct device *dev,
  130. struct dma_buf *dbuf,
  131. unsigned long size);
  132. void (*detach_dmabuf)(void *buf_priv);
  133. int (*map_dmabuf)(void *buf_priv);
  134. void (*unmap_dmabuf)(void *buf_priv);
  135. void *(*vaddr)(struct vb2_buffer *vb, void *buf_priv);
  136. void *(*cookie)(struct vb2_buffer *vb, void *buf_priv);
  137. unsigned int (*num_users)(void *buf_priv);
  138. int (*mmap)(void *buf_priv, struct vm_area_struct *vma);
  139. };
  140. /**
  141. * struct vb2_plane - plane information.
  142. * @mem_priv: private data with this plane.
  143. * @dbuf: dma_buf - shared buffer object.
  144. * @dbuf_mapped: flag to show whether dbuf is mapped or not
  145. * @dbuf_duplicated: boolean to show whether dbuf is duplicated with a
  146. * previous plane of the buffer.
  147. * @bytesused: number of bytes occupied by data in the plane (payload).
  148. * @length: size of this plane (NOT the payload) in bytes. The maximum
  149. * valid size is MAX_UINT - PAGE_SIZE.
  150. * @min_length: minimum required size of this plane (NOT the payload) in bytes.
  151. * @length is always greater or equal to @min_length, and like
  152. * @length, it is limited to MAX_UINT - PAGE_SIZE.
  153. * @m: Union with memtype-specific data.
  154. * @m.offset: when memory in the associated struct vb2_buffer is
  155. * %VB2_MEMORY_MMAP, equals the offset from the start of
  156. * the device memory for this plane (or is a "cookie" that
  157. * should be passed to mmap() called on the video node).
  158. * @m.userptr: when memory is %VB2_MEMORY_USERPTR, a userspace pointer
  159. * pointing to this plane.
  160. * @m.fd: when memory is %VB2_MEMORY_DMABUF, a userspace file
  161. * descriptor associated with this plane.
  162. * @data_offset: offset in the plane to the start of data; usually 0,
  163. * unless there is a header in front of the data.
  164. *
  165. * Should contain enough information to be able to cover all the fields
  166. * of &struct v4l2_plane at videodev2.h.
  167. */
  168. struct vb2_plane {
  169. void *mem_priv;
  170. struct dma_buf *dbuf;
  171. unsigned int dbuf_mapped;
  172. bool dbuf_duplicated;
  173. unsigned int bytesused;
  174. unsigned int length;
  175. unsigned int min_length;
  176. union {
  177. unsigned int offset;
  178. unsigned long userptr;
  179. int fd;
  180. } m;
  181. unsigned int data_offset;
  182. };
  183. /**
  184. * enum vb2_io_modes - queue access methods.
  185. * @VB2_MMAP: driver supports MMAP with streaming API.
  186. * @VB2_USERPTR: driver supports USERPTR with streaming API.
  187. * @VB2_READ: driver supports read() style access.
  188. * @VB2_WRITE: driver supports write() style access.
  189. * @VB2_DMABUF: driver supports DMABUF with streaming API.
  190. */
  191. enum vb2_io_modes {
  192. VB2_MMAP = BIT(0),
  193. VB2_USERPTR = BIT(1),
  194. VB2_READ = BIT(2),
  195. VB2_WRITE = BIT(3),
  196. VB2_DMABUF = BIT(4),
  197. };
  198. /**
  199. * enum vb2_buffer_state - current video buffer state.
  200. * @VB2_BUF_STATE_DEQUEUED: buffer under userspace control.
  201. * @VB2_BUF_STATE_IN_REQUEST: buffer is queued in media request.
  202. * @VB2_BUF_STATE_PREPARING: buffer is being prepared in videobuf2.
  203. * @VB2_BUF_STATE_QUEUED: buffer queued in videobuf2, but not in driver.
  204. * @VB2_BUF_STATE_ACTIVE: buffer queued in driver and possibly used
  205. * in a hardware operation.
  206. * @VB2_BUF_STATE_DONE: buffer returned from driver to videobuf2, but
  207. * not yet dequeued to userspace.
  208. * @VB2_BUF_STATE_ERROR: same as above, but the operation on the buffer
  209. * has ended with an error, which will be reported
  210. * to the userspace when it is dequeued.
  211. */
  212. enum vb2_buffer_state {
  213. VB2_BUF_STATE_DEQUEUED,
  214. VB2_BUF_STATE_IN_REQUEST,
  215. VB2_BUF_STATE_PREPARING,
  216. VB2_BUF_STATE_QUEUED,
  217. VB2_BUF_STATE_ACTIVE,
  218. VB2_BUF_STATE_DONE,
  219. VB2_BUF_STATE_ERROR,
  220. };
  221. struct vb2_queue;
  222. /**
  223. * struct vb2_buffer - represents a video buffer.
  224. * @vb2_queue: pointer to &struct vb2_queue with the queue to
  225. * which this driver belongs.
  226. * @index: id number of the buffer.
  227. * @type: buffer type.
  228. * @memory: the method, in which the actual data is passed.
  229. * @num_planes: number of planes in the buffer
  230. * on an internal driver queue.
  231. * @timestamp: frame timestamp in ns.
  232. * @request: the request this buffer is associated with.
  233. * @req_obj: used to bind this buffer to a request. This
  234. * request object has a refcount.
  235. */
  236. struct vb2_buffer {
  237. struct vb2_queue *vb2_queue;
  238. unsigned int index;
  239. unsigned int type;
  240. unsigned int memory;
  241. unsigned int num_planes;
  242. u64 timestamp;
  243. struct media_request *request;
  244. struct media_request_object req_obj;
  245. /* private: internal use only
  246. *
  247. * state: current buffer state; do not change
  248. * synced: this buffer has been synced for DMA, i.e. the
  249. * 'prepare' memop was called. It is cleared again
  250. * after the 'finish' memop is called.
  251. * prepared: this buffer has been prepared, i.e. the
  252. * buf_prepare op was called. It is cleared again
  253. * after the 'buf_finish' op is called.
  254. * copied_timestamp: the timestamp of this capture buffer was copied
  255. * from an output buffer.
  256. * skip_cache_sync_on_prepare: when set buffer's ->prepare() function
  257. * skips cache sync/invalidation.
  258. * skip_cache_sync_on_finish: when set buffer's ->finish() function
  259. * skips cache sync/invalidation.
  260. * planes: per-plane information; do not change
  261. * queued_entry: entry on the queued buffers list, which holds
  262. * all buffers queued from userspace
  263. * done_entry: entry on the list that stores all buffers ready
  264. * to be dequeued to userspace
  265. */
  266. enum vb2_buffer_state state;
  267. unsigned int synced:1;
  268. unsigned int prepared:1;
  269. unsigned int copied_timestamp:1;
  270. unsigned int skip_cache_sync_on_prepare:1;
  271. unsigned int skip_cache_sync_on_finish:1;
  272. struct vb2_plane planes[VB2_MAX_PLANES];
  273. struct list_head queued_entry;
  274. struct list_head done_entry;
  275. #ifdef CONFIG_VIDEO_ADV_DEBUG
  276. /*
  277. * Counters for how often these buffer-related ops are
  278. * called. Used to check for unbalanced ops.
  279. */
  280. u32 cnt_mem_alloc;
  281. u32 cnt_mem_put;
  282. u32 cnt_mem_get_dmabuf;
  283. u32 cnt_mem_get_userptr;
  284. u32 cnt_mem_put_userptr;
  285. u32 cnt_mem_prepare;
  286. u32 cnt_mem_finish;
  287. u32 cnt_mem_attach_dmabuf;
  288. u32 cnt_mem_detach_dmabuf;
  289. u32 cnt_mem_map_dmabuf;
  290. u32 cnt_mem_unmap_dmabuf;
  291. u32 cnt_mem_vaddr;
  292. u32 cnt_mem_cookie;
  293. u32 cnt_mem_num_users;
  294. u32 cnt_mem_mmap;
  295. u32 cnt_buf_out_validate;
  296. u32 cnt_buf_init;
  297. u32 cnt_buf_prepare;
  298. u32 cnt_buf_finish;
  299. u32 cnt_buf_cleanup;
  300. u32 cnt_buf_queue;
  301. u32 cnt_buf_request_complete;
  302. /* This counts the number of calls to vb2_buffer_done() */
  303. u32 cnt_buf_done;
  304. #endif
  305. };
  306. /**
  307. * struct vb2_ops - driver-specific callbacks.
  308. *
  309. * These operations are not called from interrupt context except where
  310. * mentioned specifically.
  311. *
  312. * @queue_setup: called from VIDIOC_REQBUFS() and VIDIOC_CREATE_BUFS()
  313. * handlers before memory allocation. It can be called
  314. * twice: if the original number of requested buffers
  315. * could not be allocated, then it will be called a
  316. * second time with the actually allocated number of
  317. * buffers to verify if that is OK.
  318. * The driver should return the required number of buffers
  319. * in \*num_buffers, the required number of planes per
  320. * buffer in \*num_planes, the size of each plane should be
  321. * set in the sizes\[\] array and optional per-plane
  322. * allocator specific device in the alloc_devs\[\] array.
  323. * When called from VIDIOC_REQBUFS(), \*num_planes == 0,
  324. * the driver has to use the currently configured format to
  325. * determine the plane sizes and \*num_buffers is the total
  326. * number of buffers that are being allocated. When called
  327. * from VIDIOC_CREATE_BUFS(), \*num_planes != 0 and it
  328. * describes the requested number of planes and sizes\[\]
  329. * contains the requested plane sizes. In this case
  330. * \*num_buffers are being allocated additionally to
  331. * the buffers already allocated. If either \*num_planes
  332. * or the requested sizes are invalid callback must return %-EINVAL.
  333. * @wait_prepare: release any locks taken while calling vb2 functions;
  334. * it is called before an ioctl needs to wait for a new
  335. * buffer to arrive; required to avoid a deadlock in
  336. * blocking access type.
  337. * @wait_finish: reacquire all locks released in the previous callback;
  338. * required to continue operation after sleeping while
  339. * waiting for a new buffer to arrive.
  340. * @buf_out_validate: called when the output buffer is prepared or queued
  341. * to a request; drivers can use this to validate
  342. * userspace-provided information; this is required only
  343. * for OUTPUT queues.
  344. * @buf_init: called once after allocating a buffer (in MMAP case)
  345. * or after acquiring a new USERPTR buffer; drivers may
  346. * perform additional buffer-related initialization;
  347. * initialization failure (return != 0) will prevent
  348. * queue setup from completing successfully; optional.
  349. * @buf_prepare: called every time the buffer is queued from userspace
  350. * and from the VIDIOC_PREPARE_BUF() ioctl; drivers may
  351. * perform any initialization required before each
  352. * hardware operation in this callback; drivers can
  353. * access/modify the buffer here as it is still synced for
  354. * the CPU; drivers that support VIDIOC_CREATE_BUFS() must
  355. * also validate the buffer size; if an error is returned,
  356. * the buffer will not be queued in driver; optional.
  357. * @buf_finish: called before every dequeue of the buffer back to
  358. * userspace; the buffer is synced for the CPU, so drivers
  359. * can access/modify the buffer contents; drivers may
  360. * perform any operations required before userspace
  361. * accesses the buffer; optional. The buffer state can be
  362. * one of the following: %DONE and %ERROR occur while
  363. * streaming is in progress, and the %PREPARED state occurs
  364. * when the queue has been canceled and all pending
  365. * buffers are being returned to their default %DEQUEUED
  366. * state. Typically you only have to do something if the
  367. * state is %VB2_BUF_STATE_DONE, since in all other cases
  368. * the buffer contents will be ignored anyway.
  369. * @buf_cleanup: called once before the buffer is freed; drivers may
  370. * perform any additional cleanup; optional.
  371. * @prepare_streaming: called once to prepare for 'streaming' state; this is
  372. * where validation can be done to verify everything is
  373. * okay and streaming resources can be claimed. It is
  374. * called when the VIDIOC_STREAMON ioctl is called. The
  375. * actual streaming starts when @start_streaming is called.
  376. * Optional.
  377. * @start_streaming: called once to enter 'streaming' state; the driver may
  378. * receive buffers with @buf_queue callback
  379. * before @start_streaming is called; the driver gets the
  380. * number of already queued buffers in count parameter;
  381. * driver can return an error if hardware fails, in that
  382. * case all buffers that have been already given by
  383. * the @buf_queue callback are to be returned by the driver
  384. * by calling vb2_buffer_done() with %VB2_BUF_STATE_QUEUED.
  385. * If you need a minimum number of buffers before you can
  386. * start streaming, then set
  387. * &vb2_queue->min_queued_buffers. If that is non-zero
  388. * then @start_streaming won't be called until at least
  389. * that many buffers have been queued up by userspace.
  390. * @stop_streaming: called when 'streaming' state must be disabled; driver
  391. * should stop any DMA transactions or wait until they
  392. * finish and give back all buffers it got from &buf_queue
  393. * callback by calling vb2_buffer_done() with either
  394. * %VB2_BUF_STATE_DONE or %VB2_BUF_STATE_ERROR; may use
  395. * vb2_wait_for_all_buffers() function
  396. * @unprepare_streaming:called as counterpart to @prepare_streaming; any claimed
  397. * streaming resources can be released here. It is
  398. * called when the VIDIOC_STREAMOFF ioctls is called or
  399. * when the streaming filehandle is closed. Optional.
  400. * @buf_queue: passes buffer vb to the driver; driver may start
  401. * hardware operation on this buffer; driver should give
  402. * the buffer back by calling vb2_buffer_done() function;
  403. * it is always called after calling VIDIOC_STREAMON()
  404. * ioctl; might be called before @start_streaming callback
  405. * if user pre-queued buffers before calling
  406. * VIDIOC_STREAMON().
  407. * @buf_request_complete: a buffer that was never queued to the driver but is
  408. * associated with a queued request was canceled.
  409. * The driver will have to mark associated objects in the
  410. * request as completed; required if requests are
  411. * supported.
  412. */
  413. struct vb2_ops {
  414. int (*queue_setup)(struct vb2_queue *q,
  415. unsigned int *num_buffers, unsigned int *num_planes,
  416. unsigned int sizes[], struct device *alloc_devs[]);
  417. void (*wait_prepare)(struct vb2_queue *q);
  418. void (*wait_finish)(struct vb2_queue *q);
  419. int (*buf_out_validate)(struct vb2_buffer *vb);
  420. int (*buf_init)(struct vb2_buffer *vb);
  421. int (*buf_prepare)(struct vb2_buffer *vb);
  422. void (*buf_finish)(struct vb2_buffer *vb);
  423. void (*buf_cleanup)(struct vb2_buffer *vb);
  424. int (*prepare_streaming)(struct vb2_queue *q);
  425. int (*start_streaming)(struct vb2_queue *q, unsigned int count);
  426. void (*stop_streaming)(struct vb2_queue *q);
  427. void (*unprepare_streaming)(struct vb2_queue *q);
  428. void (*buf_queue)(struct vb2_buffer *vb);
  429. void (*buf_request_complete)(struct vb2_buffer *vb);
  430. };
  431. /**
  432. * struct vb2_buf_ops - driver-specific callbacks.
  433. *
  434. * @verify_planes_array: Verify that a given user space structure contains
  435. * enough planes for the buffer. This is called
  436. * for each dequeued buffer.
  437. * @init_buffer: given a &vb2_buffer initialize the extra data after
  438. * struct vb2_buffer.
  439. * For V4L2 this is a &struct vb2_v4l2_buffer.
  440. * @fill_user_buffer: given a &vb2_buffer fill in the userspace structure.
  441. * For V4L2 this is a &struct v4l2_buffer.
  442. * @fill_vb2_buffer: given a userspace structure, fill in the &vb2_buffer.
  443. * If the userspace structure is invalid, then this op
  444. * will return an error.
  445. * @copy_timestamp: copy the timestamp from a userspace structure to
  446. * the &struct vb2_buffer.
  447. */
  448. struct vb2_buf_ops {
  449. int (*verify_planes_array)(struct vb2_buffer *vb, const void *pb);
  450. void (*init_buffer)(struct vb2_buffer *vb);
  451. void (*fill_user_buffer)(struct vb2_buffer *vb, void *pb);
  452. int (*fill_vb2_buffer)(struct vb2_buffer *vb, struct vb2_plane *planes);
  453. void (*copy_timestamp)(struct vb2_buffer *vb, const void *pb);
  454. };
  455. /**
  456. * struct vb2_queue - a videobuf2 queue.
  457. *
  458. * @type: private buffer type whose content is defined by the vb2-core
  459. * caller. For example, for V4L2, it should match
  460. * the types defined on &enum v4l2_buf_type.
  461. * @io_modes: supported io methods (see &enum vb2_io_modes).
  462. * @dev: device to use for the default allocation context if the driver
  463. * doesn't fill in the @alloc_devs array.
  464. * @dma_attrs: DMA attributes to use for the DMA.
  465. * @bidirectional: when this flag is set the DMA direction for the buffers of
  466. * this queue will be overridden with %DMA_BIDIRECTIONAL direction.
  467. * This is useful in cases where the hardware (firmware) writes to
  468. * a buffer which is mapped as read (%DMA_TO_DEVICE), or reads from
  469. * buffer which is mapped for write (%DMA_FROM_DEVICE) in order
  470. * to satisfy some internal hardware restrictions or adds a padding
  471. * needed by the processing algorithm. In case the DMA mapping is
  472. * not bidirectional but the hardware (firmware) trying to access
  473. * the buffer (in the opposite direction) this could lead to an
  474. * IOMMU protection faults.
  475. * @fileio_read_once: report EOF after reading the first buffer
  476. * @fileio_write_immediately: queue buffer after each write() call
  477. * @allow_zero_bytesused: allow bytesused == 0 to be passed to the driver
  478. * @quirk_poll_must_check_waiting_for_buffers: Return %EPOLLERR at poll when QBUF
  479. * has not been called. This is a vb1 idiom that has been adopted
  480. * also by vb2.
  481. * @supports_requests: this queue supports the Request API.
  482. * @requires_requests: this queue requires the Request API. If this is set to 1,
  483. * then supports_requests must be set to 1 as well.
  484. * @uses_qbuf: qbuf was used directly for this queue. Set to 1 the first
  485. * time this is called. Set to 0 when the queue is canceled.
  486. * If this is 1, then you cannot queue buffers from a request.
  487. * @uses_requests: requests are used for this queue. Set to 1 the first time
  488. * a request is queued. Set to 0 when the queue is canceled.
  489. * If this is 1, then you cannot queue buffers directly.
  490. * @allow_cache_hints: when set user-space can pass cache management hints in
  491. * order to skip cache flush/invalidation on ->prepare() or/and
  492. * ->finish().
  493. * @non_coherent_mem: when set queue will attempt to allocate buffers using
  494. * non-coherent memory.
  495. * @lock: pointer to a mutex that protects the &struct vb2_queue. The
  496. * driver can set this to a mutex to let the v4l2 core serialize
  497. * the queuing ioctls. If the driver wants to handle locking
  498. * itself, then this should be set to NULL. This lock is not used
  499. * by the videobuf2 core API.
  500. * @owner: The filehandle that 'owns' the buffers, i.e. the filehandle
  501. * that called reqbufs, create_buffers or started fileio.
  502. * This field is not used by the videobuf2 core API, but it allows
  503. * drivers to easily associate an owner filehandle with the queue.
  504. * @ops: driver-specific callbacks
  505. * @mem_ops: memory allocator specific callbacks
  506. * @buf_ops: callbacks to deliver buffer information.
  507. * between user-space and kernel-space.
  508. * @drv_priv: driver private data.
  509. * @subsystem_flags: Flags specific to the subsystem (V4L2/DVB/etc.). Not used
  510. * by the vb2 core.
  511. * @buf_struct_size: size of the driver-specific buffer structure;
  512. * "0" indicates the driver doesn't want to use a custom buffer
  513. * structure type. In that case a subsystem-specific struct
  514. * will be used (in the case of V4L2 that is
  515. * ``sizeof(struct vb2_v4l2_buffer)``). The first field of the
  516. * driver-specific buffer structure must be the subsystem-specific
  517. * struct (vb2_v4l2_buffer in the case of V4L2).
  518. * @timestamp_flags: Timestamp flags; ``V4L2_BUF_FLAG_TIMESTAMP_*`` and
  519. * ``V4L2_BUF_FLAG_TSTAMP_SRC_*``
  520. * @gfp_flags: additional gfp flags used when allocating the buffers.
  521. * Typically this is 0, but it may be e.g. %GFP_DMA or %__GFP_DMA32
  522. * to force the buffer allocation to a specific memory zone.
  523. * @min_queued_buffers: the minimum number of queued buffers needed before
  524. * @start_streaming can be called. Used when a DMA engine
  525. * cannot be started unless at least this number of buffers
  526. * have been queued into the driver.
  527. * VIDIOC_REQBUFS will ensure at least @min_queued_buffers + 1
  528. * buffers will be allocated. Note that VIDIOC_CREATE_BUFS will not
  529. * modify the requested buffer count.
  530. * @min_reqbufs_allocation: the minimum number of buffers to be allocated when
  531. * calling VIDIOC_REQBUFS. Note that VIDIOC_CREATE_BUFS will *not*
  532. * modify the requested buffer count and does not use this field.
  533. * Drivers can set this if there has to be a certain number of
  534. * buffers available for the hardware to work effectively.
  535. * This allows calling VIDIOC_REQBUFS with a buffer count of 1 and
  536. * it will be automatically adjusted to a workable buffer count.
  537. * If set, then @min_reqbufs_allocation must be larger than
  538. * @min_queued_buffers + 1.
  539. * If this field is > 3, then it is highly recommended that the
  540. * driver implements the V4L2_CID_MIN_BUFFERS_FOR_CAPTURE/OUTPUT
  541. * control.
  542. * @alloc_devs: &struct device memory type/allocator-specific per-plane device
  543. */
  544. /*
  545. * Private elements (won't appear at the uAPI book):
  546. * @mmap_lock: private mutex used when buffers are allocated/freed/mmapped
  547. * @memory: current memory type used
  548. * @dma_dir: DMA mapping direction.
  549. * @bufs: videobuf2 buffer structures. If it is non-NULL then
  550. * bufs_bitmap is also non-NULL.
  551. * @bufs_bitmap: bitmap tracking whether each bufs[] entry is used
  552. * @max_num_buffers: upper limit of number of allocated/used buffers.
  553. * If set to 0 v4l2 core will change it VB2_MAX_FRAME
  554. * for backward compatibility.
  555. * @queued_list: list of buffers currently queued from userspace
  556. * @queued_count: number of buffers queued and ready for streaming.
  557. * @owned_by_drv_count: number of buffers owned by the driver
  558. * @done_list: list of buffers ready to be dequeued to userspace
  559. * @done_lock: lock to protect done_list list
  560. * @done_wq: waitqueue for processes waiting for buffers ready to be dequeued
  561. * @streaming: current streaming state
  562. * @start_streaming_called: @start_streaming was called successfully and we
  563. * started streaming.
  564. * @error: a fatal error occurred on the queue
  565. * @waiting_for_buffers: used in poll() to check if vb2 is still waiting for
  566. * buffers. Only set for capture queues if qbuf has not yet been
  567. * called since poll() needs to return %EPOLLERR in that situation.
  568. * @waiting_in_dqbuf: set by the core for the duration of a blocking DQBUF, when
  569. * it has to wait for a buffer to become available with vb2_queue->lock
  570. * released. Used to prevent destroying the queue by other threads.
  571. * @is_multiplanar: set if buffer type is multiplanar
  572. * @is_output: set if buffer type is output
  573. * @is_busy: set if at least one buffer has been allocated at some time.
  574. * @copy_timestamp: set if vb2-core should set timestamps
  575. * @last_buffer_dequeued: used in poll() and DQBUF to immediately return if the
  576. * last decoded buffer was already dequeued. Set for capture queues
  577. * when a buffer with the %V4L2_BUF_FLAG_LAST is dequeued.
  578. * @fileio: file io emulator internal data, used only if emulator is active
  579. * @threadio: thread io internal data, used only if thread is active
  580. * @name: queue name, used for logging purpose. Initialized automatically
  581. * if left empty by drivers.
  582. */
  583. struct vb2_queue {
  584. unsigned int type;
  585. unsigned int io_modes;
  586. struct device *dev;
  587. unsigned long dma_attrs;
  588. unsigned int bidirectional:1;
  589. unsigned int fileio_read_once:1;
  590. unsigned int fileio_write_immediately:1;
  591. unsigned int allow_zero_bytesused:1;
  592. unsigned int quirk_poll_must_check_waiting_for_buffers:1;
  593. unsigned int supports_requests:1;
  594. unsigned int requires_requests:1;
  595. unsigned int uses_qbuf:1;
  596. unsigned int uses_requests:1;
  597. unsigned int allow_cache_hints:1;
  598. unsigned int non_coherent_mem:1;
  599. struct mutex *lock;
  600. void *owner;
  601. const struct vb2_ops *ops;
  602. const struct vb2_mem_ops *mem_ops;
  603. const struct vb2_buf_ops *buf_ops;
  604. void *drv_priv;
  605. u32 subsystem_flags;
  606. unsigned int buf_struct_size;
  607. u32 timestamp_flags;
  608. gfp_t gfp_flags;
  609. u32 min_queued_buffers;
  610. u32 min_reqbufs_allocation;
  611. struct device *alloc_devs[VB2_MAX_PLANES];
  612. /* private: internal use only */
  613. struct mutex mmap_lock;
  614. unsigned int memory;
  615. enum dma_data_direction dma_dir;
  616. struct vb2_buffer **bufs;
  617. unsigned long *bufs_bitmap;
  618. unsigned int max_num_buffers;
  619. struct list_head queued_list;
  620. unsigned int queued_count;
  621. atomic_t owned_by_drv_count;
  622. struct list_head done_list;
  623. spinlock_t done_lock;
  624. wait_queue_head_t done_wq;
  625. unsigned int streaming:1;
  626. unsigned int start_streaming_called:1;
  627. unsigned int error:1;
  628. unsigned int waiting_for_buffers:1;
  629. unsigned int waiting_in_dqbuf:1;
  630. unsigned int is_multiplanar:1;
  631. unsigned int is_output:1;
  632. unsigned int is_busy:1;
  633. unsigned int copy_timestamp:1;
  634. unsigned int last_buffer_dequeued:1;
  635. struct vb2_fileio_data *fileio;
  636. struct vb2_threadio_data *threadio;
  637. char name[32];
  638. #ifdef CONFIG_VIDEO_ADV_DEBUG
  639. /*
  640. * Counters for how often these queue-related ops are
  641. * called. Used to check for unbalanced ops.
  642. */
  643. u32 cnt_queue_setup;
  644. u32 cnt_wait_prepare;
  645. u32 cnt_wait_finish;
  646. u32 cnt_prepare_streaming;
  647. u32 cnt_start_streaming;
  648. u32 cnt_stop_streaming;
  649. u32 cnt_unprepare_streaming;
  650. #endif
  651. };
  652. /**
  653. * vb2_queue_allows_cache_hints() - Return true if the queue allows cache
  654. * and memory consistency hints.
  655. *
  656. * @q: pointer to &struct vb2_queue with videobuf2 queue
  657. */
  658. static inline bool vb2_queue_allows_cache_hints(struct vb2_queue *q)
  659. {
  660. return q->allow_cache_hints && q->memory == VB2_MEMORY_MMAP;
  661. }
  662. /**
  663. * vb2_plane_vaddr() - Return a kernel virtual address of a given plane.
  664. * @vb: pointer to &struct vb2_buffer to which the plane in
  665. * question belongs to.
  666. * @plane_no: plane number for which the address is to be returned.
  667. *
  668. * This function returns a kernel virtual address of a given plane if
  669. * such a mapping exist, NULL otherwise.
  670. */
  671. void *vb2_plane_vaddr(struct vb2_buffer *vb, unsigned int plane_no);
  672. /**
  673. * vb2_plane_cookie() - Return allocator specific cookie for the given plane.
  674. * @vb: pointer to &struct vb2_buffer to which the plane in
  675. * question belongs to.
  676. * @plane_no: plane number for which the cookie is to be returned.
  677. *
  678. * This function returns an allocator specific cookie for a given plane if
  679. * available, NULL otherwise. The allocator should provide some simple static
  680. * inline function, which would convert this cookie to the allocator specific
  681. * type that can be used directly by the driver to access the buffer. This can
  682. * be for example physical address, pointer to scatter list or IOMMU mapping.
  683. */
  684. void *vb2_plane_cookie(struct vb2_buffer *vb, unsigned int plane_no);
  685. /**
  686. * vb2_buffer_done() - inform videobuf2 that an operation on a buffer
  687. * is finished.
  688. * @vb: pointer to &struct vb2_buffer to be used.
  689. * @state: state of the buffer, as defined by &enum vb2_buffer_state.
  690. * Either %VB2_BUF_STATE_DONE if the operation finished
  691. * successfully, %VB2_BUF_STATE_ERROR if the operation finished
  692. * with an error or %VB2_BUF_STATE_QUEUED.
  693. *
  694. * This function should be called by the driver after a hardware operation on
  695. * a buffer is finished and the buffer may be returned to userspace. The driver
  696. * cannot use this buffer anymore until it is queued back to it by videobuf
  697. * by the means of &vb2_ops->buf_queue callback. Only buffers previously queued
  698. * to the driver by &vb2_ops->buf_queue can be passed to this function.
  699. *
  700. * While streaming a buffer can only be returned in state DONE or ERROR.
  701. * The &vb2_ops->start_streaming op can also return them in case the DMA engine
  702. * cannot be started for some reason. In that case the buffers should be
  703. * returned with state QUEUED to put them back into the queue.
  704. */
  705. void vb2_buffer_done(struct vb2_buffer *vb, enum vb2_buffer_state state);
  706. /**
  707. * vb2_discard_done() - discard all buffers marked as DONE.
  708. * @q: pointer to &struct vb2_queue with videobuf2 queue.
  709. *
  710. * This function is intended to be used with suspend/resume operations. It
  711. * discards all 'done' buffers as they would be too old to be requested after
  712. * resume.
  713. *
  714. * Drivers must stop the hardware and synchronize with interrupt handlers and/or
  715. * delayed works before calling this function to make sure no buffer will be
  716. * touched by the driver and/or hardware.
  717. */
  718. void vb2_discard_done(struct vb2_queue *q);
  719. /**
  720. * vb2_wait_for_all_buffers() - wait until all buffers are given back to vb2.
  721. * @q: pointer to &struct vb2_queue with videobuf2 queue.
  722. *
  723. * This function will wait until all buffers that have been given to the driver
  724. * by &vb2_ops->buf_queue are given back to vb2 with vb2_buffer_done(). It
  725. * doesn't call &vb2_ops->wait_prepare/&vb2_ops->wait_finish pair.
  726. * It is intended to be called with all locks taken, for example from
  727. * &vb2_ops->stop_streaming callback.
  728. */
  729. int vb2_wait_for_all_buffers(struct vb2_queue *q);
  730. /**
  731. * vb2_core_querybuf() - query video buffer information.
  732. * @q: pointer to &struct vb2_queue with videobuf2 queue.
  733. * @vb: pointer to struct &vb2_buffer.
  734. * @pb: buffer struct passed from userspace.
  735. *
  736. * Videobuf2 core helper to implement VIDIOC_QUERYBUF() operation. It is called
  737. * internally by VB2 by an API-specific handler, like ``videobuf2-v4l2.h``.
  738. *
  739. * The passed buffer should have been verified.
  740. *
  741. * This function fills the relevant information for the userspace.
  742. *
  743. * Return: returns zero on success; an error code otherwise.
  744. */
  745. void vb2_core_querybuf(struct vb2_queue *q, struct vb2_buffer *vb, void *pb);
  746. /**
  747. * vb2_core_reqbufs() - Initiate streaming.
  748. * @q: pointer to &struct vb2_queue with videobuf2 queue.
  749. * @memory: memory type, as defined by &enum vb2_memory.
  750. * @flags: auxiliary queue/buffer management flags. Currently, the only
  751. * used flag is %V4L2_MEMORY_FLAG_NON_COHERENT.
  752. * @count: requested buffer count.
  753. *
  754. * Videobuf2 core helper to implement VIDIOC_REQBUF() operation. It is called
  755. * internally by VB2 by an API-specific handler, like ``videobuf2-v4l2.h``.
  756. *
  757. * This function:
  758. *
  759. * #) verifies streaming parameters passed from the userspace;
  760. * #) sets up the queue;
  761. * #) negotiates number of buffers and planes per buffer with the driver
  762. * to be used during streaming;
  763. * #) allocates internal buffer structures (&struct vb2_buffer), according to
  764. * the agreed parameters;
  765. * #) for MMAP memory type, allocates actual video memory, using the
  766. * memory handling/allocation routines provided during queue initialization.
  767. *
  768. * If req->count is 0, all the memory will be freed instead.
  769. *
  770. * If the queue has been allocated previously by a previous vb2_core_reqbufs()
  771. * call and the queue is not busy, memory will be reallocated.
  772. *
  773. * Return: returns zero on success; an error code otherwise.
  774. */
  775. int vb2_core_reqbufs(struct vb2_queue *q, enum vb2_memory memory,
  776. unsigned int flags, unsigned int *count);
  777. /**
  778. * vb2_core_create_bufs() - Allocate buffers and any required auxiliary structs
  779. * @q: pointer to &struct vb2_queue with videobuf2 queue.
  780. * @memory: memory type, as defined by &enum vb2_memory.
  781. * @flags: auxiliary queue/buffer management flags.
  782. * @count: requested buffer count.
  783. * @requested_planes: number of planes requested.
  784. * @requested_sizes: array with the size of the planes.
  785. * @first_index: index of the first created buffer, all allocated buffers have
  786. * indices in the range [first_index..first_index+count-1]
  787. *
  788. * Videobuf2 core helper to implement VIDIOC_CREATE_BUFS() operation. It is
  789. * called internally by VB2 by an API-specific handler, like
  790. * ``videobuf2-v4l2.h``.
  791. *
  792. * This function:
  793. *
  794. * #) verifies parameter sanity;
  795. * #) calls the &vb2_ops->queue_setup queue operation;
  796. * #) performs any necessary memory allocations.
  797. *
  798. * Return: returns zero on success; an error code otherwise.
  799. */
  800. int vb2_core_create_bufs(struct vb2_queue *q, enum vb2_memory memory,
  801. unsigned int flags, unsigned int *count,
  802. unsigned int requested_planes,
  803. const unsigned int requested_sizes[],
  804. unsigned int *first_index);
  805. /**
  806. * vb2_core_prepare_buf() - Pass ownership of a buffer from userspace
  807. * to the kernel.
  808. * @q: pointer to &struct vb2_queue with videobuf2 queue.
  809. * @vb: pointer to struct &vb2_buffer.
  810. * @pb: buffer structure passed from userspace to
  811. * &v4l2_ioctl_ops->vidioc_prepare_buf handler in driver.
  812. *
  813. * Videobuf2 core helper to implement VIDIOC_PREPARE_BUF() operation. It is
  814. * called internally by VB2 by an API-specific handler, like
  815. * ``videobuf2-v4l2.h``.
  816. *
  817. * The passed buffer should have been verified.
  818. *
  819. * This function calls vb2_ops->buf_prepare callback in the driver
  820. * (if provided), in which driver-specific buffer initialization can
  821. * be performed.
  822. *
  823. * Return: returns zero on success; an error code otherwise.
  824. */
  825. int vb2_core_prepare_buf(struct vb2_queue *q, struct vb2_buffer *vb, void *pb);
  826. /**
  827. * vb2_core_remove_bufs() -
  828. * @q: pointer to &struct vb2_queue with videobuf2 queue.
  829. * @start: first index of the range of buffers to remove.
  830. * @count: number of buffers to remove.
  831. *
  832. * Return: returns zero on success; an error code otherwise.
  833. */
  834. int vb2_core_remove_bufs(struct vb2_queue *q, unsigned int start, unsigned int count);
  835. /**
  836. * vb2_core_qbuf() - Queue a buffer from userspace
  837. *
  838. * @q: pointer to &struct vb2_queue with videobuf2 queue.
  839. * @vb: pointer to struct &vb2_buffer.
  840. * @pb: buffer structure passed from userspace to
  841. * v4l2_ioctl_ops->vidioc_qbuf handler in driver
  842. * @req: pointer to &struct media_request, may be NULL.
  843. *
  844. * Videobuf2 core helper to implement VIDIOC_QBUF() operation. It is called
  845. * internally by VB2 by an API-specific handler, like ``videobuf2-v4l2.h``.
  846. *
  847. * This function:
  848. *
  849. * #) If @req is non-NULL, then the buffer will be bound to this
  850. * media request and it returns. The buffer will be prepared and
  851. * queued to the driver (i.e. the next two steps) when the request
  852. * itself is queued.
  853. * #) if necessary, calls &vb2_ops->buf_prepare callback in the driver
  854. * (if provided), in which driver-specific buffer initialization can
  855. * be performed;
  856. * #) if streaming is on, queues the buffer in driver by the means of
  857. * &vb2_ops->buf_queue callback for processing.
  858. *
  859. * Return: returns zero on success; an error code otherwise.
  860. */
  861. int vb2_core_qbuf(struct vb2_queue *q, struct vb2_buffer *vb, void *pb,
  862. struct media_request *req);
  863. /**
  864. * vb2_core_dqbuf() - Dequeue a buffer to the userspace
  865. * @q: pointer to &struct vb2_queue with videobuf2 queue
  866. * @pindex: pointer to the buffer index. May be NULL
  867. * @pb: buffer structure passed from userspace to
  868. * v4l2_ioctl_ops->vidioc_dqbuf handler in driver.
  869. * @nonblocking: if true, this call will not sleep waiting for a buffer if no
  870. * buffers ready for dequeuing are present. Normally the driver
  871. * would be passing (file->f_flags & O_NONBLOCK) here.
  872. *
  873. * Videobuf2 core helper to implement VIDIOC_DQBUF() operation. It is called
  874. * internally by VB2 by an API-specific handler, like ``videobuf2-v4l2.h``.
  875. *
  876. * This function:
  877. *
  878. * #) calls buf_finish callback in the driver (if provided), in which
  879. * driver can perform any additional operations that may be required before
  880. * returning the buffer to userspace, such as cache sync,
  881. * #) the buffer struct members are filled with relevant information for
  882. * the userspace.
  883. *
  884. * Return: returns zero on success; an error code otherwise.
  885. */
  886. int vb2_core_dqbuf(struct vb2_queue *q, unsigned int *pindex, void *pb,
  887. bool nonblocking);
  888. /**
  889. * vb2_core_streamon() - Implements VB2 stream ON logic
  890. *
  891. * @q: pointer to &struct vb2_queue with videobuf2 queue
  892. * @type: type of the queue to be started.
  893. * For V4L2, this is defined by &enum v4l2_buf_type type.
  894. *
  895. * Videobuf2 core helper to implement VIDIOC_STREAMON() operation. It is called
  896. * internally by VB2 by an API-specific handler, like ``videobuf2-v4l2.h``.
  897. *
  898. * Return: returns zero on success; an error code otherwise.
  899. */
  900. int vb2_core_streamon(struct vb2_queue *q, unsigned int type);
  901. /**
  902. * vb2_core_streamoff() - Implements VB2 stream OFF logic
  903. *
  904. * @q: pointer to &struct vb2_queue with videobuf2 queue
  905. * @type: type of the queue to be started.
  906. * For V4L2, this is defined by &enum v4l2_buf_type type.
  907. *
  908. * Videobuf2 core helper to implement VIDIOC_STREAMOFF() operation. It is
  909. * called internally by VB2 by an API-specific handler, like
  910. * ``videobuf2-v4l2.h``.
  911. *
  912. * Return: returns zero on success; an error code otherwise.
  913. */
  914. int vb2_core_streamoff(struct vb2_queue *q, unsigned int type);
  915. /**
  916. * vb2_core_expbuf() - Export a buffer as a file descriptor.
  917. * @q: pointer to &struct vb2_queue with videobuf2 queue.
  918. * @fd: pointer to the file descriptor associated with DMABUF
  919. * (set by driver).
  920. * @type: buffer type.
  921. * @vb: pointer to struct &vb2_buffer.
  922. * @plane: index of the plane to be exported, 0 for single plane queues
  923. * @flags: file flags for newly created file, as defined at
  924. * include/uapi/asm-generic/fcntl.h.
  925. * Currently, the only used flag is %O_CLOEXEC.
  926. * is supported, refer to manual of open syscall for more details.
  927. *
  928. *
  929. * Videobuf2 core helper to implement VIDIOC_EXPBUF() operation. It is called
  930. * internally by VB2 by an API-specific handler, like ``videobuf2-v4l2.h``.
  931. *
  932. * Return: returns zero on success; an error code otherwise.
  933. */
  934. int vb2_core_expbuf(struct vb2_queue *q, int *fd, unsigned int type,
  935. struct vb2_buffer *vb, unsigned int plane, unsigned int flags);
  936. /**
  937. * vb2_core_queue_init() - initialize a videobuf2 queue
  938. * @q: pointer to &struct vb2_queue with videobuf2 queue.
  939. * This structure should be allocated in driver
  940. *
  941. * The &vb2_queue structure should be allocated by the driver. The driver is
  942. * responsible of clearing it's content and setting initial values for some
  943. * required entries before calling this function.
  944. *
  945. * .. note::
  946. *
  947. * The following fields at @q should be set before calling this function:
  948. * &vb2_queue->ops, &vb2_queue->mem_ops, &vb2_queue->type.
  949. */
  950. int vb2_core_queue_init(struct vb2_queue *q);
  951. /**
  952. * vb2_core_queue_release() - stop streaming, release the queue and free memory
  953. * @q: pointer to &struct vb2_queue with videobuf2 queue.
  954. *
  955. * This function stops streaming and performs necessary clean ups, including
  956. * freeing video buffer memory. The driver is responsible for freeing
  957. * the &struct vb2_queue itself.
  958. */
  959. void vb2_core_queue_release(struct vb2_queue *q);
  960. /**
  961. * vb2_queue_error() - signal a fatal error on the queue
  962. * @q: pointer to &struct vb2_queue with videobuf2 queue.
  963. *
  964. * Flag that a fatal unrecoverable error has occurred and wake up all processes
  965. * waiting on the queue. Polling will now set %EPOLLERR and queuing and dequeuing
  966. * buffers will return %-EIO.
  967. *
  968. * The error flag will be cleared when canceling the queue, either from
  969. * vb2_streamoff() or vb2_queue_release(). Drivers should thus not call this
  970. * function before starting the stream, otherwise the error flag will remain set
  971. * until the queue is released when closing the device node.
  972. */
  973. void vb2_queue_error(struct vb2_queue *q);
  974. /**
  975. * vb2_mmap() - map video buffers into application address space.
  976. * @q: pointer to &struct vb2_queue with videobuf2 queue.
  977. * @vma: pointer to &struct vm_area_struct with the vma passed
  978. * to the mmap file operation handler in the driver.
  979. *
  980. * Should be called from mmap file operation handler of a driver.
  981. * This function maps one plane of one of the available video buffers to
  982. * userspace. To map whole video memory allocated on reqbufs, this function
  983. * has to be called once per each plane per each buffer previously allocated.
  984. *
  985. * When the userspace application calls mmap, it passes to it an offset returned
  986. * to it earlier by the means of &v4l2_ioctl_ops->vidioc_querybuf handler.
  987. * That offset acts as a "cookie", which is then used to identify the plane
  988. * to be mapped.
  989. *
  990. * This function finds a plane with a matching offset and a mapping is performed
  991. * by the means of a provided memory operation.
  992. *
  993. * The return values from this function are intended to be directly returned
  994. * from the mmap handler in driver.
  995. */
  996. int vb2_mmap(struct vb2_queue *q, struct vm_area_struct *vma);
  997. #ifndef CONFIG_MMU
  998. /**
  999. * vb2_get_unmapped_area - map video buffers into application address space.
  1000. * @q: pointer to &struct vb2_queue with videobuf2 queue.
  1001. * @addr: memory address.
  1002. * @len: buffer size.
  1003. * @pgoff: page offset.
  1004. * @flags: memory flags.
  1005. *
  1006. * This function is used in noMMU platforms to propose address mapping
  1007. * for a given buffer. It's intended to be used as a handler for the
  1008. * &file_operations->get_unmapped_area operation.
  1009. *
  1010. * This is called by the mmap() syscall routines will call this
  1011. * to get a proposed address for the mapping, when ``!CONFIG_MMU``.
  1012. */
  1013. unsigned long vb2_get_unmapped_area(struct vb2_queue *q,
  1014. unsigned long addr,
  1015. unsigned long len,
  1016. unsigned long pgoff,
  1017. unsigned long flags);
  1018. #endif
  1019. /**
  1020. * vb2_core_poll() - implements poll syscall() logic.
  1021. * @q: pointer to &struct vb2_queue with videobuf2 queue.
  1022. * @file: &struct file argument passed to the poll
  1023. * file operation handler.
  1024. * @wait: &poll_table wait argument passed to the poll
  1025. * file operation handler.
  1026. *
  1027. * This function implements poll file operation handler for a driver.
  1028. * For CAPTURE queues, if a buffer is ready to be dequeued, the userspace will
  1029. * be informed that the file descriptor of a video device is available for
  1030. * reading.
  1031. * For OUTPUT queues, if a buffer is ready to be dequeued, the file descriptor
  1032. * will be reported as available for writing.
  1033. *
  1034. * The return values from this function are intended to be directly returned
  1035. * from poll handler in driver.
  1036. */
  1037. __poll_t vb2_core_poll(struct vb2_queue *q, struct file *file,
  1038. poll_table *wait);
  1039. /**
  1040. * vb2_read() - implements read() syscall logic.
  1041. * @q: pointer to &struct vb2_queue with videobuf2 queue.
  1042. * @data: pointed to target userspace buffer
  1043. * @count: number of bytes to read
  1044. * @ppos: file handle position tracking pointer
  1045. * @nonblock: mode selector (1 means blocking calls, 0 means nonblocking)
  1046. */
  1047. size_t vb2_read(struct vb2_queue *q, char __user *data, size_t count,
  1048. loff_t *ppos, int nonblock);
  1049. /**
  1050. * vb2_write() - implements write() syscall logic.
  1051. * @q: pointer to &struct vb2_queue with videobuf2 queue.
  1052. * @data: pointed to target userspace buffer
  1053. * @count: number of bytes to write
  1054. * @ppos: file handle position tracking pointer
  1055. * @nonblock: mode selector (1 means blocking calls, 0 means nonblocking)
  1056. */
  1057. size_t vb2_write(struct vb2_queue *q, const char __user *data, size_t count,
  1058. loff_t *ppos, int nonblock);
  1059. /**
  1060. * typedef vb2_thread_fnc - callback function for use with vb2_thread.
  1061. *
  1062. * @vb: pointer to struct &vb2_buffer.
  1063. * @priv: pointer to a private data.
  1064. *
  1065. * This is called whenever a buffer is dequeued in the thread.
  1066. */
  1067. typedef int (*vb2_thread_fnc)(struct vb2_buffer *vb, void *priv);
  1068. /**
  1069. * vb2_thread_start() - start a thread for the given queue.
  1070. * @q: pointer to &struct vb2_queue with videobuf2 queue.
  1071. * @fnc: &vb2_thread_fnc callback function.
  1072. * @priv: priv pointer passed to the callback function.
  1073. * @thread_name:the name of the thread. This will be prefixed with "vb2-".
  1074. *
  1075. * This starts a thread that will queue and dequeue until an error occurs
  1076. * or vb2_thread_stop() is called.
  1077. *
  1078. * .. attention::
  1079. *
  1080. * This function should not be used for anything else but the videobuf2-dvb
  1081. * support. If you think you have another good use-case for this, then please
  1082. * contact the linux-media mailing list first.
  1083. */
  1084. int vb2_thread_start(struct vb2_queue *q, vb2_thread_fnc fnc, void *priv,
  1085. const char *thread_name);
  1086. /**
  1087. * vb2_thread_stop() - stop the thread for the given queue.
  1088. * @q: pointer to &struct vb2_queue with videobuf2 queue.
  1089. */
  1090. int vb2_thread_stop(struct vb2_queue *q);
  1091. /**
  1092. * vb2_is_streaming() - return streaming status of the queue.
  1093. * @q: pointer to &struct vb2_queue with videobuf2 queue.
  1094. */
  1095. static inline bool vb2_is_streaming(struct vb2_queue *q)
  1096. {
  1097. return q->streaming;
  1098. }
  1099. /**
  1100. * vb2_fileio_is_active() - return true if fileio is active.
  1101. * @q: pointer to &struct vb2_queue with videobuf2 queue.
  1102. *
  1103. * This returns true if read() or write() is used to stream the data
  1104. * as opposed to stream I/O. This is almost never an important distinction,
  1105. * except in rare cases. One such case is that using read() or write() to
  1106. * stream a format using %V4L2_FIELD_ALTERNATE is not allowed since there
  1107. * is no way you can pass the field information of each buffer to/from
  1108. * userspace. A driver that supports this field format should check for
  1109. * this in the &vb2_ops->queue_setup op and reject it if this function returns
  1110. * true.
  1111. */
  1112. static inline bool vb2_fileio_is_active(struct vb2_queue *q)
  1113. {
  1114. return q->fileio;
  1115. }
  1116. /**
  1117. * vb2_get_num_buffers() - get the number of buffer in a queue
  1118. * @q: pointer to &struct vb2_queue with videobuf2 queue.
  1119. */
  1120. static inline unsigned int vb2_get_num_buffers(struct vb2_queue *q)
  1121. {
  1122. if (q->bufs_bitmap)
  1123. return bitmap_weight(q->bufs_bitmap, q->max_num_buffers);
  1124. return 0;
  1125. }
  1126. /**
  1127. * vb2_is_busy() - return busy status of the queue.
  1128. * @q: pointer to &struct vb2_queue with videobuf2 queue.
  1129. *
  1130. * This function checks if queue has any buffers allocated.
  1131. */
  1132. static inline bool vb2_is_busy(struct vb2_queue *q)
  1133. {
  1134. return !!q->is_busy;
  1135. }
  1136. /**
  1137. * vb2_get_drv_priv() - return driver private data associated with the queue.
  1138. * @q: pointer to &struct vb2_queue with videobuf2 queue.
  1139. */
  1140. static inline void *vb2_get_drv_priv(struct vb2_queue *q)
  1141. {
  1142. return q->drv_priv;
  1143. }
  1144. /**
  1145. * vb2_set_plane_payload() - set bytesused for the plane @plane_no.
  1146. * @vb: pointer to &struct vb2_buffer to which the plane in
  1147. * question belongs to.
  1148. * @plane_no: plane number for which payload should be set.
  1149. * @size: payload in bytes.
  1150. */
  1151. static inline void vb2_set_plane_payload(struct vb2_buffer *vb,
  1152. unsigned int plane_no, unsigned long size)
  1153. {
  1154. /*
  1155. * size must never be larger than the buffer length, so
  1156. * warn and clamp to the buffer length if that's the case.
  1157. */
  1158. if (plane_no < vb->num_planes) {
  1159. if (WARN_ON_ONCE(size > vb->planes[plane_no].length))
  1160. size = vb->planes[plane_no].length;
  1161. vb->planes[plane_no].bytesused = size;
  1162. }
  1163. }
  1164. /**
  1165. * vb2_get_plane_payload() - get bytesused for the plane plane_no
  1166. * @vb: pointer to &struct vb2_buffer to which the plane in
  1167. * question belongs to.
  1168. * @plane_no: plane number for which payload should be set.
  1169. */
  1170. static inline unsigned long vb2_get_plane_payload(struct vb2_buffer *vb,
  1171. unsigned int plane_no)
  1172. {
  1173. if (plane_no < vb->num_planes)
  1174. return vb->planes[plane_no].bytesused;
  1175. return 0;
  1176. }
  1177. /**
  1178. * vb2_plane_size() - return plane size in bytes.
  1179. * @vb: pointer to &struct vb2_buffer to which the plane in
  1180. * question belongs to.
  1181. * @plane_no: plane number for which size should be returned.
  1182. */
  1183. static inline unsigned long
  1184. vb2_plane_size(struct vb2_buffer *vb, unsigned int plane_no)
  1185. {
  1186. if (plane_no < vb->num_planes)
  1187. return vb->planes[plane_no].length;
  1188. return 0;
  1189. }
  1190. /**
  1191. * vb2_start_streaming_called() - return streaming status of driver.
  1192. * @q: pointer to &struct vb2_queue with videobuf2 queue.
  1193. */
  1194. static inline bool vb2_start_streaming_called(struct vb2_queue *q)
  1195. {
  1196. return q->start_streaming_called;
  1197. }
  1198. /**
  1199. * vb2_clear_last_buffer_dequeued() - clear last buffer dequeued flag of queue.
  1200. * @q: pointer to &struct vb2_queue with videobuf2 queue.
  1201. */
  1202. static inline void vb2_clear_last_buffer_dequeued(struct vb2_queue *q)
  1203. {
  1204. q->last_buffer_dequeued = false;
  1205. }
  1206. /**
  1207. * vb2_get_buffer() - get a buffer from a queue
  1208. * @q: pointer to &struct vb2_queue with videobuf2 queue.
  1209. * @index: buffer index
  1210. *
  1211. * This function obtains a buffer from a queue, by its index.
  1212. * Keep in mind that there is no refcounting involved in this
  1213. * operation, so the buffer lifetime should be taken into
  1214. * consideration.
  1215. */
  1216. static inline struct vb2_buffer *vb2_get_buffer(struct vb2_queue *q,
  1217. unsigned int index)
  1218. {
  1219. if (!q->bufs)
  1220. return NULL;
  1221. if (index >= q->max_num_buffers)
  1222. return NULL;
  1223. if (test_bit(index, q->bufs_bitmap))
  1224. return q->bufs[index];
  1225. return NULL;
  1226. }
  1227. /*
  1228. * The following functions are not part of the vb2 core API, but are useful
  1229. * functions for videobuf2-*.
  1230. */
  1231. /**
  1232. * vb2_buffer_in_use() - return true if the buffer is in use and
  1233. * the queue cannot be freed (by the means of VIDIOC_REQBUFS(0)) call.
  1234. *
  1235. * @vb: buffer for which plane size should be returned.
  1236. * @q: pointer to &struct vb2_queue with videobuf2 queue.
  1237. */
  1238. bool vb2_buffer_in_use(struct vb2_queue *q, struct vb2_buffer *vb);
  1239. /**
  1240. * vb2_verify_memory_type() - Check whether the memory type and buffer type
  1241. * passed to a buffer operation are compatible with the queue.
  1242. *
  1243. * @q: pointer to &struct vb2_queue with videobuf2 queue.
  1244. * @memory: memory model, as defined by enum &vb2_memory.
  1245. * @type: private buffer type whose content is defined by the vb2-core
  1246. * caller. For example, for V4L2, it should match
  1247. * the types defined on enum &v4l2_buf_type.
  1248. */
  1249. int vb2_verify_memory_type(struct vb2_queue *q,
  1250. enum vb2_memory memory, unsigned int type);
  1251. /**
  1252. * vb2_request_object_is_buffer() - return true if the object is a buffer
  1253. *
  1254. * @obj: the request object.
  1255. */
  1256. bool vb2_request_object_is_buffer(struct media_request_object *obj);
  1257. /**
  1258. * vb2_request_buffer_cnt() - return the number of buffers in the request
  1259. *
  1260. * @req: the request.
  1261. */
  1262. unsigned int vb2_request_buffer_cnt(struct media_request *req);
  1263. #endif /* _MEDIA_VIDEOBUF2_CORE_H */