videobuf2-core.h 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639
  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. #define VB2_MAX_FRAME (32)
  19. #define VB2_MAX_PLANES (8)
  20. enum vb2_memory {
  21. VB2_MEMORY_UNKNOWN = 0,
  22. VB2_MEMORY_MMAP = 1,
  23. VB2_MEMORY_USERPTR = 2,
  24. VB2_MEMORY_DMABUF = 4,
  25. };
  26. struct vb2_alloc_ctx;
  27. struct vb2_fileio_data;
  28. struct vb2_threadio_data;
  29. /**
  30. * struct vb2_mem_ops - memory handling/memory allocator operations
  31. * @alloc: allocate video memory and, optionally, allocator private data,
  32. * return NULL on failure or a pointer to allocator private,
  33. * per-buffer data on success; the returned private structure
  34. * will then be passed as buf_priv argument to other ops in this
  35. * structure. Additional gfp_flags to use when allocating the
  36. * are also passed to this operation. These flags are from the
  37. * gfp_flags field of vb2_queue.
  38. * @put: inform the allocator that the buffer will no longer be used;
  39. * usually will result in the allocator freeing the buffer (if
  40. * no other users of this buffer are present); the buf_priv
  41. * argument is the allocator private per-buffer structure
  42. * previously returned from the alloc callback.
  43. * @get_dmabuf: acquire userspace memory for a hardware operation; used for
  44. * DMABUF memory types.
  45. * @get_userptr: acquire userspace memory for a hardware operation; used for
  46. * USERPTR memory types; vaddr is the address passed to the
  47. * videobuf layer when queuing a video buffer of USERPTR type;
  48. * should return an allocator private per-buffer structure
  49. * associated with the buffer on success, NULL on failure;
  50. * the returned private structure will then be passed as buf_priv
  51. * argument to other ops in this structure.
  52. * @put_userptr: inform the allocator that a USERPTR buffer will no longer
  53. * be used.
  54. * @attach_dmabuf: attach a shared struct dma_buf for a hardware operation;
  55. * used for DMABUF memory types; alloc_ctx is the alloc context
  56. * dbuf is the shared dma_buf; returns NULL on failure;
  57. * allocator private per-buffer structure on success;
  58. * this needs to be used for further accesses to the buffer.
  59. * @detach_dmabuf: inform the exporter of the buffer that the current DMABUF
  60. * buffer is no longer used; the buf_priv argument is the
  61. * allocator private per-buffer structure previously returned
  62. * from the attach_dmabuf callback.
  63. * @map_dmabuf: request for access to the dmabuf from allocator; the allocator
  64. * of dmabuf is informed that this driver is going to use the
  65. * dmabuf.
  66. * @unmap_dmabuf: releases access control to the dmabuf - allocator is notified
  67. * that this driver is done using the dmabuf for now.
  68. * @prepare: called every time the buffer is passed from userspace to the
  69. * driver, useful for cache synchronisation, optional.
  70. * @finish: called every time the buffer is passed back from the driver
  71. * to the userspace, also optional.
  72. * @vaddr: return a kernel virtual address to a given memory buffer
  73. * associated with the passed private structure or NULL if no
  74. * such mapping exists.
  75. * @cookie: return allocator specific cookie for a given memory buffer
  76. * associated with the passed private structure or NULL if not
  77. * available.
  78. * @num_users: return the current number of users of a memory buffer;
  79. * return 1 if the videobuf layer (or actually the driver using
  80. * it) is the only user.
  81. * @mmap: setup a userspace mapping for a given memory buffer under
  82. * the provided virtual memory region.
  83. *
  84. * Required ops for USERPTR types: get_userptr, put_userptr.
  85. * Required ops for MMAP types: alloc, put, num_users, mmap.
  86. * Required ops for read/write access types: alloc, put, num_users, vaddr.
  87. * Required ops for DMABUF types: attach_dmabuf, detach_dmabuf, map_dmabuf,
  88. * unmap_dmabuf.
  89. */
  90. struct vb2_mem_ops {
  91. void *(*alloc)(void *alloc_ctx, unsigned long size,
  92. enum dma_data_direction dma_dir,
  93. gfp_t gfp_flags);
  94. void (*put)(void *buf_priv);
  95. struct dma_buf *(*get_dmabuf)(void *buf_priv, unsigned long flags);
  96. void *(*get_userptr)(void *alloc_ctx, unsigned long vaddr,
  97. unsigned long size,
  98. enum dma_data_direction dma_dir);
  99. void (*put_userptr)(void *buf_priv);
  100. void (*prepare)(void *buf_priv);
  101. void (*finish)(void *buf_priv);
  102. void *(*attach_dmabuf)(void *alloc_ctx, struct dma_buf *dbuf,
  103. unsigned long size,
  104. enum dma_data_direction dma_dir);
  105. void (*detach_dmabuf)(void *buf_priv);
  106. int (*map_dmabuf)(void *buf_priv);
  107. void (*unmap_dmabuf)(void *buf_priv);
  108. void *(*vaddr)(void *buf_priv);
  109. void *(*cookie)(void *buf_priv);
  110. unsigned int (*num_users)(void *buf_priv);
  111. int (*mmap)(void *buf_priv, struct vm_area_struct *vma);
  112. };
  113. /**
  114. * struct vb2_plane - plane information
  115. * @mem_priv: private data with this plane
  116. * @dbuf: dma_buf - shared buffer object
  117. * @dbuf_mapped: flag to show whether dbuf is mapped or not
  118. * @bytesused: number of bytes occupied by data in the plane (payload)
  119. * @length: size of this plane (NOT the payload) in bytes
  120. * @offset: when memory in the associated struct vb2_buffer is
  121. * VB2_MEMORY_MMAP, equals the offset from the start of
  122. * the device memory for this plane (or is a "cookie" that
  123. * should be passed to mmap() called on the video node)
  124. * @userptr: when memory is VB2_MEMORY_USERPTR, a userspace pointer
  125. * pointing to this plane
  126. * @fd: when memory is VB2_MEMORY_DMABUF, a userspace file
  127. * descriptor associated with this plane
  128. * @m: Union with memtype-specific data (@offset, @userptr or
  129. * @fd).
  130. * @data_offset: offset in the plane to the start of data; usually 0,
  131. * unless there is a header in front of the data
  132. * Should contain enough information to be able to cover all the fields
  133. * of struct v4l2_plane at videodev2.h
  134. */
  135. struct vb2_plane {
  136. void *mem_priv;
  137. struct dma_buf *dbuf;
  138. unsigned int dbuf_mapped;
  139. unsigned int bytesused;
  140. unsigned int length;
  141. union {
  142. unsigned int offset;
  143. unsigned long userptr;
  144. int fd;
  145. } m;
  146. unsigned int data_offset;
  147. };
  148. /**
  149. * enum vb2_io_modes - queue access methods
  150. * @VB2_MMAP: driver supports MMAP with streaming API
  151. * @VB2_USERPTR: driver supports USERPTR with streaming API
  152. * @VB2_READ: driver supports read() style access
  153. * @VB2_WRITE: driver supports write() style access
  154. * @VB2_DMABUF: driver supports DMABUF with streaming API
  155. */
  156. enum vb2_io_modes {
  157. VB2_MMAP = (1 << 0),
  158. VB2_USERPTR = (1 << 1),
  159. VB2_READ = (1 << 2),
  160. VB2_WRITE = (1 << 3),
  161. VB2_DMABUF = (1 << 4),
  162. };
  163. /**
  164. * enum vb2_buffer_state - current video buffer state
  165. * @VB2_BUF_STATE_DEQUEUED: buffer under userspace control
  166. * @VB2_BUF_STATE_PREPARING: buffer is being prepared in videobuf
  167. * @VB2_BUF_STATE_PREPARED: buffer prepared in videobuf and by the driver
  168. * @VB2_BUF_STATE_QUEUED: buffer queued in videobuf, but not in driver
  169. * @VB2_BUF_STATE_REQUEUEING: re-queue a buffer to the driver
  170. * @VB2_BUF_STATE_ACTIVE: buffer queued in driver and possibly used
  171. * in a hardware operation
  172. * @VB2_BUF_STATE_DONE: buffer returned from driver to videobuf, but
  173. * not yet dequeued to userspace
  174. * @VB2_BUF_STATE_ERROR: same as above, but the operation on the buffer
  175. * has ended with an error, which will be reported
  176. * to the userspace when it is dequeued
  177. */
  178. enum vb2_buffer_state {
  179. VB2_BUF_STATE_DEQUEUED,
  180. VB2_BUF_STATE_PREPARING,
  181. VB2_BUF_STATE_PREPARED,
  182. VB2_BUF_STATE_QUEUED,
  183. VB2_BUF_STATE_REQUEUEING,
  184. VB2_BUF_STATE_ACTIVE,
  185. VB2_BUF_STATE_DONE,
  186. VB2_BUF_STATE_ERROR,
  187. };
  188. struct vb2_queue;
  189. /**
  190. * struct vb2_buffer - represents a video buffer
  191. * @vb2_queue: the queue to which this driver belongs
  192. * @index: id number of the buffer
  193. * @type: buffer type
  194. * @memory: the method, in which the actual data is passed
  195. * @num_planes: number of planes in the buffer
  196. * on an internal driver queue
  197. * @planes: private per-plane information; do not change
  198. */
  199. struct vb2_buffer {
  200. struct vb2_queue *vb2_queue;
  201. unsigned int index;
  202. unsigned int type;
  203. unsigned int memory;
  204. unsigned int num_planes;
  205. struct vb2_plane planes[VB2_MAX_PLANES];
  206. /* private: internal use only
  207. *
  208. * state: current buffer state; do not change
  209. * queued_entry: entry on the queued buffers list, which holds
  210. * all buffers queued from userspace
  211. * done_entry: entry on the list that stores all buffers ready
  212. * to be dequeued to userspace
  213. */
  214. enum vb2_buffer_state state;
  215. struct list_head queued_entry;
  216. struct list_head done_entry;
  217. #ifdef CONFIG_VIDEO_ADV_DEBUG
  218. /*
  219. * Counters for how often these buffer-related ops are
  220. * called. Used to check for unbalanced ops.
  221. */
  222. u32 cnt_mem_alloc;
  223. u32 cnt_mem_put;
  224. u32 cnt_mem_get_dmabuf;
  225. u32 cnt_mem_get_userptr;
  226. u32 cnt_mem_put_userptr;
  227. u32 cnt_mem_prepare;
  228. u32 cnt_mem_finish;
  229. u32 cnt_mem_attach_dmabuf;
  230. u32 cnt_mem_detach_dmabuf;
  231. u32 cnt_mem_map_dmabuf;
  232. u32 cnt_mem_unmap_dmabuf;
  233. u32 cnt_mem_vaddr;
  234. u32 cnt_mem_cookie;
  235. u32 cnt_mem_num_users;
  236. u32 cnt_mem_mmap;
  237. u32 cnt_buf_init;
  238. u32 cnt_buf_prepare;
  239. u32 cnt_buf_finish;
  240. u32 cnt_buf_cleanup;
  241. u32 cnt_buf_queue;
  242. /* This counts the number of calls to vb2_buffer_done() */
  243. u32 cnt_buf_done;
  244. #endif
  245. };
  246. /**
  247. * struct vb2_ops - driver-specific callbacks
  248. *
  249. * @queue_setup: called from VIDIOC_REQBUFS and VIDIOC_CREATE_BUFS
  250. * handlers before memory allocation, or, if
  251. * *num_planes != 0, after the allocation to verify a
  252. * smaller number of buffers. Driver should return
  253. * the required number of buffers in *num_buffers, the
  254. * required number of planes per buffer in *num_planes; the
  255. * size of each plane should be set in the sizes[] array
  256. * and optional per-plane allocator specific context in the
  257. * alloc_ctxs[] array. When called from VIDIOC_REQBUFS,
  258. * fmt == NULL, the driver has to use the currently
  259. * configured format and *num_buffers is the total number
  260. * of buffers, that are being allocated. When called from
  261. * VIDIOC_CREATE_BUFS, fmt != NULL and it describes the
  262. * target frame format (if the format isn't valid the
  263. * callback must return -EINVAL). In this case *num_buffers
  264. * are being allocated additionally to q->num_buffers.
  265. * @wait_prepare: release any locks taken while calling vb2 functions;
  266. * it is called before an ioctl needs to wait for a new
  267. * buffer to arrive; required to avoid a deadlock in
  268. * blocking access type.
  269. * @wait_finish: reacquire all locks released in the previous callback;
  270. * required to continue operation after sleeping while
  271. * waiting for a new buffer to arrive.
  272. * @buf_init: called once after allocating a buffer (in MMAP case)
  273. * or after acquiring a new USERPTR buffer; drivers may
  274. * perform additional buffer-related initialization;
  275. * initialization failure (return != 0) will prevent
  276. * queue setup from completing successfully; optional.
  277. * @buf_prepare: called every time the buffer is queued from userspace
  278. * and from the VIDIOC_PREPARE_BUF ioctl; drivers may
  279. * perform any initialization required before each
  280. * hardware operation in this callback; drivers can
  281. * access/modify the buffer here as it is still synced for
  282. * the CPU; drivers that support VIDIOC_CREATE_BUFS must
  283. * also validate the buffer size; if an error is returned,
  284. * the buffer will not be queued in driver; optional.
  285. * @buf_finish: called before every dequeue of the buffer back to
  286. * userspace; the buffer is synced for the CPU, so drivers
  287. * can access/modify the buffer contents; drivers may
  288. * perform any operations required before userspace
  289. * accesses the buffer; optional. The buffer state can be
  290. * one of the following: DONE and ERROR occur while
  291. * streaming is in progress, and the PREPARED state occurs
  292. * when the queue has been canceled and all pending
  293. * buffers are being returned to their default DEQUEUED
  294. * state. Typically you only have to do something if the
  295. * state is VB2_BUF_STATE_DONE, since in all other cases
  296. * the buffer contents will be ignored anyway.
  297. * @buf_cleanup: called once before the buffer is freed; drivers may
  298. * perform any additional cleanup; optional.
  299. * @start_streaming: called once to enter 'streaming' state; the driver may
  300. * receive buffers with @buf_queue callback before
  301. * @start_streaming is called; the driver gets the number
  302. * of already queued buffers in count parameter; driver
  303. * can return an error if hardware fails, in that case all
  304. * buffers that have been already given by the @buf_queue
  305. * callback are to be returned by the driver by calling
  306. * @vb2_buffer_done(VB2_BUF_STATE_QUEUED).
  307. * If you need a minimum number of buffers before you can
  308. * start streaming, then set @min_buffers_needed in the
  309. * vb2_queue structure. If that is non-zero then
  310. * start_streaming won't be called until at least that
  311. * many buffers have been queued up by userspace.
  312. * @stop_streaming: called when 'streaming' state must be disabled; driver
  313. * should stop any DMA transactions or wait until they
  314. * finish and give back all buffers it got from buf_queue()
  315. * callback by calling @vb2_buffer_done() with either
  316. * VB2_BUF_STATE_DONE or VB2_BUF_STATE_ERROR; may use
  317. * vb2_wait_for_all_buffers() function
  318. * @buf_queue: passes buffer vb to the driver; driver may start
  319. * hardware operation on this buffer; driver should give
  320. * the buffer back by calling vb2_buffer_done() function;
  321. * it is allways called after calling STREAMON ioctl;
  322. * might be called before start_streaming callback if user
  323. * pre-queued buffers before calling STREAMON.
  324. */
  325. struct vb2_ops {
  326. int (*queue_setup)(struct vb2_queue *q, const void *parg,
  327. unsigned int *num_buffers, unsigned int *num_planes,
  328. unsigned int sizes[], void *alloc_ctxs[]);
  329. void (*wait_prepare)(struct vb2_queue *q);
  330. void (*wait_finish)(struct vb2_queue *q);
  331. int (*buf_init)(struct vb2_buffer *vb);
  332. int (*buf_prepare)(struct vb2_buffer *vb);
  333. void (*buf_finish)(struct vb2_buffer *vb);
  334. void (*buf_cleanup)(struct vb2_buffer *vb);
  335. int (*start_streaming)(struct vb2_queue *q, unsigned int count);
  336. void (*stop_streaming)(struct vb2_queue *q);
  337. void (*buf_queue)(struct vb2_buffer *vb);
  338. };
  339. struct vb2_buf_ops {
  340. int (*verify_planes_array)(struct vb2_buffer *vb, const void *pb);
  341. int (*fill_user_buffer)(struct vb2_buffer *vb, void *pb);
  342. int (*fill_vb2_buffer)(struct vb2_buffer *vb, const void *pb,
  343. struct vb2_plane *planes);
  344. int (*set_timestamp)(struct vb2_buffer *vb, const void *pb);
  345. };
  346. /**
  347. * struct vb2_queue - a videobuf queue
  348. *
  349. * @type: private buffer type whose content is defined by the vb2-core
  350. * caller. For example, for V4L2, it should match
  351. * the V4L2_BUF_TYPE_* in include/uapi/linux/videodev2.h
  352. * @io_modes: supported io methods (see vb2_io_modes enum)
  353. * @fileio_read_once: report EOF after reading the first buffer
  354. * @fileio_write_immediately: queue buffer after each write() call
  355. * @allow_zero_bytesused: allow bytesused == 0 to be passed to the driver
  356. * @lock: pointer to a mutex that protects the vb2_queue struct. The
  357. * driver can set this to a mutex to let the v4l2 core serialize
  358. * the queuing ioctls. If the driver wants to handle locking
  359. * itself, then this should be set to NULL. This lock is not used
  360. * by the videobuf2 core API.
  361. * @owner: The filehandle that 'owns' the buffers, i.e. the filehandle
  362. * that called reqbufs, create_buffers or started fileio.
  363. * This field is not used by the videobuf2 core API, but it allows
  364. * drivers to easily associate an owner filehandle with the queue.
  365. * @ops: driver-specific callbacks
  366. * @mem_ops: memory allocator specific callbacks
  367. * @buf_ops: callbacks to deliver buffer information
  368. * between user-space and kernel-space
  369. * @drv_priv: driver private data
  370. * @buf_struct_size: size of the driver-specific buffer structure;
  371. * "0" indicates the driver doesn't want to use a custom buffer
  372. * structure type. for example, sizeof(struct vb2_v4l2_buffer)
  373. * will be used for v4l2.
  374. * @timestamp_flags: Timestamp flags; V4L2_BUF_FLAG_TIMESTAMP_* and
  375. * V4L2_BUF_FLAG_TSTAMP_SRC_*
  376. * @gfp_flags: additional gfp flags used when allocating the buffers.
  377. * Typically this is 0, but it may be e.g. GFP_DMA or __GFP_DMA32
  378. * to force the buffer allocation to a specific memory zone.
  379. * @min_buffers_needed: the minimum number of buffers needed before
  380. * start_streaming() can be called. Used when a DMA engine
  381. * cannot be started unless at least this number of buffers
  382. * have been queued into the driver.
  383. */
  384. /*
  385. * Private elements (won't appear at the DocBook):
  386. * @mmap_lock: private mutex used when buffers are allocated/freed/mmapped
  387. * @memory: current memory type used
  388. * @bufs: videobuf buffer structures
  389. * @num_buffers: number of allocated/used buffers
  390. * @queued_list: list of buffers currently queued from userspace
  391. * @queued_count: number of buffers queued and ready for streaming.
  392. * @owned_by_drv_count: number of buffers owned by the driver
  393. * @done_list: list of buffers ready to be dequeued to userspace
  394. * @done_lock: lock to protect done_list list
  395. * @done_wq: waitqueue for processes waiting for buffers ready to be dequeued
  396. * @alloc_ctx: memory type/allocator-specific contexts for each plane
  397. * @streaming: current streaming state
  398. * @start_streaming_called: start_streaming() was called successfully and we
  399. * started streaming.
  400. * @error: a fatal error occurred on the queue
  401. * @waiting_for_buffers: used in poll() to check if vb2 is still waiting for
  402. * buffers. Only set for capture queues if qbuf has not yet been
  403. * called since poll() needs to return POLLERR in that situation.
  404. * @is_multiplanar: set if buffer type is multiplanar
  405. * @is_output: set if buffer type is output
  406. * @last_buffer_dequeued: used in poll() and DQBUF to immediately return if the
  407. * last decoded buffer was already dequeued. Set for capture queues
  408. * when a buffer with the V4L2_BUF_FLAG_LAST is dequeued.
  409. * @fileio: file io emulator internal data, used only if emulator is active
  410. * @threadio: thread io internal data, used only if thread is active
  411. */
  412. struct vb2_queue {
  413. unsigned int type;
  414. unsigned int io_modes;
  415. unsigned fileio_read_once:1;
  416. unsigned fileio_write_immediately:1;
  417. unsigned allow_zero_bytesused:1;
  418. struct mutex *lock;
  419. void *owner;
  420. const struct vb2_ops *ops;
  421. const struct vb2_mem_ops *mem_ops;
  422. const struct vb2_buf_ops *buf_ops;
  423. void *drv_priv;
  424. unsigned int buf_struct_size;
  425. u32 timestamp_flags;
  426. gfp_t gfp_flags;
  427. u32 min_buffers_needed;
  428. /* private: internal use only */
  429. struct mutex mmap_lock;
  430. unsigned int memory;
  431. struct vb2_buffer *bufs[VB2_MAX_FRAME];
  432. unsigned int num_buffers;
  433. struct list_head queued_list;
  434. unsigned int queued_count;
  435. atomic_t owned_by_drv_count;
  436. struct list_head done_list;
  437. spinlock_t done_lock;
  438. wait_queue_head_t done_wq;
  439. void *alloc_ctx[VB2_MAX_PLANES];
  440. unsigned int plane_sizes[VB2_MAX_PLANES];
  441. unsigned int streaming:1;
  442. unsigned int start_streaming_called:1;
  443. unsigned int error:1;
  444. unsigned int waiting_for_buffers:1;
  445. unsigned int is_multiplanar:1;
  446. unsigned int is_output:1;
  447. unsigned int last_buffer_dequeued:1;
  448. struct vb2_fileio_data *fileio;
  449. struct vb2_threadio_data *threadio;
  450. #ifdef CONFIG_VIDEO_ADV_DEBUG
  451. /*
  452. * Counters for how often these queue-related ops are
  453. * called. Used to check for unbalanced ops.
  454. */
  455. u32 cnt_queue_setup;
  456. u32 cnt_wait_prepare;
  457. u32 cnt_wait_finish;
  458. u32 cnt_start_streaming;
  459. u32 cnt_stop_streaming;
  460. #endif
  461. };
  462. void *vb2_plane_vaddr(struct vb2_buffer *vb, unsigned int plane_no);
  463. void *vb2_plane_cookie(struct vb2_buffer *vb, unsigned int plane_no);
  464. void vb2_buffer_done(struct vb2_buffer *vb, enum vb2_buffer_state state);
  465. void vb2_discard_done(struct vb2_queue *q);
  466. int vb2_wait_for_all_buffers(struct vb2_queue *q);
  467. int vb2_core_querybuf(struct vb2_queue *q, unsigned int index, void *pb);
  468. int vb2_core_reqbufs(struct vb2_queue *q, enum vb2_memory memory,
  469. unsigned int *count);
  470. int vb2_core_create_bufs(struct vb2_queue *q, enum vb2_memory memory,
  471. unsigned int *count, const void *parg);
  472. int vb2_core_prepare_buf(struct vb2_queue *q, unsigned int index, void *pb);
  473. int vb2_core_qbuf(struct vb2_queue *q, unsigned int index, void *pb);
  474. int vb2_core_dqbuf(struct vb2_queue *q, void *pb, bool nonblocking);
  475. int vb2_core_streamon(struct vb2_queue *q, unsigned int type);
  476. int vb2_core_streamoff(struct vb2_queue *q, unsigned int type);
  477. int vb2_core_expbuf(struct vb2_queue *q, int *fd, unsigned int type,
  478. unsigned int index, unsigned int plane, unsigned int flags);
  479. int vb2_core_queue_init(struct vb2_queue *q);
  480. void vb2_core_queue_release(struct vb2_queue *q);
  481. void vb2_queue_error(struct vb2_queue *q);
  482. int vb2_mmap(struct vb2_queue *q, struct vm_area_struct *vma);
  483. #ifndef CONFIG_MMU
  484. unsigned long vb2_get_unmapped_area(struct vb2_queue *q,
  485. unsigned long addr,
  486. unsigned long len,
  487. unsigned long pgoff,
  488. unsigned long flags);
  489. #endif
  490. /**
  491. * vb2_is_streaming() - return streaming status of the queue
  492. * @q: videobuf queue
  493. */
  494. static inline bool vb2_is_streaming(struct vb2_queue *q)
  495. {
  496. return q->streaming;
  497. }
  498. /**
  499. * vb2_fileio_is_active() - return true if fileio is active.
  500. * @q: videobuf queue
  501. *
  502. * This returns true if read() or write() is used to stream the data
  503. * as opposed to stream I/O. This is almost never an important distinction,
  504. * except in rare cases. One such case is that using read() or write() to
  505. * stream a format using V4L2_FIELD_ALTERNATE is not allowed since there
  506. * is no way you can pass the field information of each buffer to/from
  507. * userspace. A driver that supports this field format should check for
  508. * this in the queue_setup op and reject it if this function returns true.
  509. */
  510. static inline bool vb2_fileio_is_active(struct vb2_queue *q)
  511. {
  512. return q->fileio;
  513. }
  514. /**
  515. * vb2_is_busy() - return busy status of the queue
  516. * @q: videobuf queue
  517. *
  518. * This function checks if queue has any buffers allocated.
  519. */
  520. static inline bool vb2_is_busy(struct vb2_queue *q)
  521. {
  522. return (q->num_buffers > 0);
  523. }
  524. /**
  525. * vb2_get_drv_priv() - return driver private data associated with the queue
  526. * @q: videobuf queue
  527. */
  528. static inline void *vb2_get_drv_priv(struct vb2_queue *q)
  529. {
  530. return q->drv_priv;
  531. }
  532. /**
  533. * vb2_set_plane_payload() - set bytesused for the plane plane_no
  534. * @vb: buffer for which plane payload should be set
  535. * @plane_no: plane number for which payload should be set
  536. * @size: payload in bytes
  537. */
  538. static inline void vb2_set_plane_payload(struct vb2_buffer *vb,
  539. unsigned int plane_no, unsigned long size)
  540. {
  541. if (plane_no < vb->num_planes)
  542. vb->planes[plane_no].bytesused = size;
  543. }
  544. /**
  545. * vb2_get_plane_payload() - get bytesused for the plane plane_no
  546. * @vb: buffer for which plane payload should be set
  547. * @plane_no: plane number for which payload should be set
  548. */
  549. static inline unsigned long vb2_get_plane_payload(struct vb2_buffer *vb,
  550. unsigned int plane_no)
  551. {
  552. if (plane_no < vb->num_planes)
  553. return vb->planes[plane_no].bytesused;
  554. return 0;
  555. }
  556. /**
  557. * vb2_plane_size() - return plane size in bytes
  558. * @vb: buffer for which plane size should be returned
  559. * @plane_no: plane number for which size should be returned
  560. */
  561. static inline unsigned long
  562. vb2_plane_size(struct vb2_buffer *vb, unsigned int plane_no)
  563. {
  564. if (plane_no < vb->num_planes)
  565. return vb->planes[plane_no].length;
  566. return 0;
  567. }
  568. /**
  569. * vb2_start_streaming_called() - return streaming status of driver
  570. * @q: videobuf queue
  571. */
  572. static inline bool vb2_start_streaming_called(struct vb2_queue *q)
  573. {
  574. return q->start_streaming_called;
  575. }
  576. /**
  577. * vb2_clear_last_buffer_dequeued() - clear last buffer dequeued flag of queue
  578. * @q: videobuf queue
  579. */
  580. static inline void vb2_clear_last_buffer_dequeued(struct vb2_queue *q)
  581. {
  582. q->last_buffer_dequeued = false;
  583. }
  584. #endif /* _MEDIA_VIDEOBUF2_CORE_H */