videobuf2-core.h 23 KB

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