videobuf2-core.h 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705
  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. * @min_length: minimum required size of this plane (NOT the payload) in bytes.
  121. * @length is always greater or equal to @min_length.
  122. * @offset: when memory in the associated struct vb2_buffer is
  123. * VB2_MEMORY_MMAP, equals the offset from the start of
  124. * the device memory for this plane (or is a "cookie" that
  125. * should be passed to mmap() called on the video node)
  126. * @userptr: when memory is VB2_MEMORY_USERPTR, a userspace pointer
  127. * pointing to this plane
  128. * @fd: when memory is VB2_MEMORY_DMABUF, a userspace file
  129. * descriptor associated with this plane
  130. * @m: Union with memtype-specific data (@offset, @userptr or
  131. * @fd).
  132. * @data_offset: offset in the plane to the start of data; usually 0,
  133. * unless there is a header in front of the data
  134. * Should contain enough information to be able to cover all the fields
  135. * of struct v4l2_plane at videodev2.h
  136. */
  137. struct vb2_plane {
  138. void *mem_priv;
  139. struct dma_buf *dbuf;
  140. unsigned int dbuf_mapped;
  141. unsigned int bytesused;
  142. unsigned int length;
  143. unsigned int min_length;
  144. union {
  145. unsigned int offset;
  146. unsigned long userptr;
  147. int fd;
  148. } m;
  149. unsigned int data_offset;
  150. };
  151. /**
  152. * enum vb2_io_modes - queue access methods
  153. * @VB2_MMAP: driver supports MMAP with streaming API
  154. * @VB2_USERPTR: driver supports USERPTR with streaming API
  155. * @VB2_READ: driver supports read() style access
  156. * @VB2_WRITE: driver supports write() style access
  157. * @VB2_DMABUF: driver supports DMABUF with streaming API
  158. */
  159. enum vb2_io_modes {
  160. VB2_MMAP = (1 << 0),
  161. VB2_USERPTR = (1 << 1),
  162. VB2_READ = (1 << 2),
  163. VB2_WRITE = (1 << 3),
  164. VB2_DMABUF = (1 << 4),
  165. };
  166. /**
  167. * enum vb2_buffer_state - current video buffer state
  168. * @VB2_BUF_STATE_DEQUEUED: buffer under userspace control
  169. * @VB2_BUF_STATE_PREPARING: buffer is being prepared in videobuf
  170. * @VB2_BUF_STATE_PREPARED: buffer prepared in videobuf and by the driver
  171. * @VB2_BUF_STATE_QUEUED: buffer queued in videobuf, but not in driver
  172. * @VB2_BUF_STATE_REQUEUEING: re-queue a buffer to the driver
  173. * @VB2_BUF_STATE_ACTIVE: buffer queued in driver and possibly used
  174. * in a hardware operation
  175. * @VB2_BUF_STATE_DONE: buffer returned from driver to videobuf, but
  176. * not yet dequeued to userspace
  177. * @VB2_BUF_STATE_ERROR: same as above, but the operation on the buffer
  178. * has ended with an error, which will be reported
  179. * to the userspace when it is dequeued
  180. */
  181. enum vb2_buffer_state {
  182. VB2_BUF_STATE_DEQUEUED,
  183. VB2_BUF_STATE_PREPARING,
  184. VB2_BUF_STATE_PREPARED,
  185. VB2_BUF_STATE_QUEUED,
  186. VB2_BUF_STATE_REQUEUEING,
  187. VB2_BUF_STATE_ACTIVE,
  188. VB2_BUF_STATE_DONE,
  189. VB2_BUF_STATE_ERROR,
  190. };
  191. struct vb2_queue;
  192. /**
  193. * struct vb2_buffer - represents a video buffer
  194. * @vb2_queue: the queue to which this driver belongs
  195. * @index: id number of the buffer
  196. * @type: buffer type
  197. * @memory: the method, in which the actual data is passed
  198. * @num_planes: number of planes in the buffer
  199. * on an internal driver queue
  200. * @planes: private per-plane information; do not change
  201. * @timestamp: frame timestamp in ns
  202. */
  203. struct vb2_buffer {
  204. struct vb2_queue *vb2_queue;
  205. unsigned int index;
  206. unsigned int type;
  207. unsigned int memory;
  208. unsigned int num_planes;
  209. struct vb2_plane planes[VB2_MAX_PLANES];
  210. u64 timestamp;
  211. /* private: internal use only
  212. *
  213. * state: current buffer state; do not change
  214. * queued_entry: entry on the queued buffers list, which holds
  215. * all buffers queued from userspace
  216. * done_entry: entry on the list that stores all buffers ready
  217. * to be dequeued to userspace
  218. */
  219. enum vb2_buffer_state state;
  220. struct list_head queued_entry;
  221. struct list_head done_entry;
  222. #ifdef CONFIG_VIDEO_ADV_DEBUG
  223. /*
  224. * Counters for how often these buffer-related ops are
  225. * called. Used to check for unbalanced ops.
  226. */
  227. u32 cnt_mem_alloc;
  228. u32 cnt_mem_put;
  229. u32 cnt_mem_get_dmabuf;
  230. u32 cnt_mem_get_userptr;
  231. u32 cnt_mem_put_userptr;
  232. u32 cnt_mem_prepare;
  233. u32 cnt_mem_finish;
  234. u32 cnt_mem_attach_dmabuf;
  235. u32 cnt_mem_detach_dmabuf;
  236. u32 cnt_mem_map_dmabuf;
  237. u32 cnt_mem_unmap_dmabuf;
  238. u32 cnt_mem_vaddr;
  239. u32 cnt_mem_cookie;
  240. u32 cnt_mem_num_users;
  241. u32 cnt_mem_mmap;
  242. u32 cnt_buf_init;
  243. u32 cnt_buf_prepare;
  244. u32 cnt_buf_finish;
  245. u32 cnt_buf_cleanup;
  246. u32 cnt_buf_queue;
  247. /* This counts the number of calls to vb2_buffer_done() */
  248. u32 cnt_buf_done;
  249. #endif
  250. };
  251. /**
  252. * struct vb2_ops - driver-specific callbacks
  253. *
  254. * @queue_setup: called from VIDIOC_REQBUFS and VIDIOC_CREATE_BUFS
  255. * handlers before memory allocation. It can be called
  256. * twice: if the original number of requested buffers
  257. * could not be allocated, then it will be called a
  258. * second time with the actually allocated number of
  259. * buffers to verify if that is OK.
  260. * The driver should return the required number of buffers
  261. * in *num_buffers, the required number of planes per
  262. * buffer in *num_planes, the size of each plane should be
  263. * set in the sizes[] array and optional per-plane
  264. * allocator specific context in the alloc_ctxs[] array.
  265. * When called from VIDIOC_REQBUFS, *num_planes == 0, the
  266. * driver has to use the currently configured format to
  267. * determine the plane sizes and *num_buffers is the total
  268. * number of buffers that are being allocated. When called
  269. * from VIDIOC_CREATE_BUFS, *num_planes != 0 and it
  270. * describes the requested number of planes and sizes[]
  271. * contains the requested plane sizes. If either
  272. * *num_planes or the requested sizes are invalid callback
  273. * must return -EINVAL. In this case *num_buffers are
  274. * being allocated additionally to q->num_buffers.
  275. * @wait_prepare: release any locks taken while calling vb2 functions;
  276. * it is called before an ioctl needs to wait for a new
  277. * buffer to arrive; required to avoid a deadlock in
  278. * blocking access type.
  279. * @wait_finish: reacquire all locks released in the previous callback;
  280. * required to continue operation after sleeping while
  281. * waiting for a new buffer to arrive.
  282. * @buf_init: called once after allocating a buffer (in MMAP case)
  283. * or after acquiring a new USERPTR buffer; drivers may
  284. * perform additional buffer-related initialization;
  285. * initialization failure (return != 0) will prevent
  286. * queue setup from completing successfully; optional.
  287. * @buf_prepare: called every time the buffer is queued from userspace
  288. * and from the VIDIOC_PREPARE_BUF ioctl; drivers may
  289. * perform any initialization required before each
  290. * hardware operation in this callback; drivers can
  291. * access/modify the buffer here as it is still synced for
  292. * the CPU; drivers that support VIDIOC_CREATE_BUFS must
  293. * also validate the buffer size; if an error is returned,
  294. * the buffer will not be queued in driver; optional.
  295. * @buf_finish: called before every dequeue of the buffer back to
  296. * userspace; the buffer is synced for the CPU, so drivers
  297. * can access/modify the buffer contents; drivers may
  298. * perform any operations required before userspace
  299. * accesses the buffer; optional. The buffer state can be
  300. * one of the following: DONE and ERROR occur while
  301. * streaming is in progress, and the PREPARED state occurs
  302. * when the queue has been canceled and all pending
  303. * buffers are being returned to their default DEQUEUED
  304. * state. Typically you only have to do something if the
  305. * state is VB2_BUF_STATE_DONE, since in all other cases
  306. * the buffer contents will be ignored anyway.
  307. * @buf_cleanup: called once before the buffer is freed; drivers may
  308. * perform any additional cleanup; optional.
  309. * @start_streaming: called once to enter 'streaming' state; the driver may
  310. * receive buffers with @buf_queue callback before
  311. * @start_streaming is called; the driver gets the number
  312. * of already queued buffers in count parameter; driver
  313. * can return an error if hardware fails, in that case all
  314. * buffers that have been already given by the @buf_queue
  315. * callback are to be returned by the driver by calling
  316. * @vb2_buffer_done(VB2_BUF_STATE_QUEUED).
  317. * If you need a minimum number of buffers before you can
  318. * start streaming, then set @min_buffers_needed in the
  319. * vb2_queue structure. If that is non-zero then
  320. * start_streaming won't be called until at least that
  321. * many buffers have been queued up by userspace.
  322. * @stop_streaming: called when 'streaming' state must be disabled; driver
  323. * should stop any DMA transactions or wait until they
  324. * finish and give back all buffers it got from buf_queue()
  325. * callback by calling @vb2_buffer_done() with either
  326. * VB2_BUF_STATE_DONE or VB2_BUF_STATE_ERROR; may use
  327. * vb2_wait_for_all_buffers() function
  328. * @buf_queue: passes buffer vb to the driver; driver may start
  329. * hardware operation on this buffer; driver should give
  330. * the buffer back by calling vb2_buffer_done() function;
  331. * it is allways called after calling STREAMON ioctl;
  332. * might be called before start_streaming callback if user
  333. * pre-queued buffers before calling STREAMON.
  334. */
  335. struct vb2_ops {
  336. int (*queue_setup)(struct vb2_queue *q,
  337. unsigned int *num_buffers, unsigned int *num_planes,
  338. unsigned int sizes[], void *alloc_ctxs[]);
  339. void (*wait_prepare)(struct vb2_queue *q);
  340. void (*wait_finish)(struct vb2_queue *q);
  341. int (*buf_init)(struct vb2_buffer *vb);
  342. int (*buf_prepare)(struct vb2_buffer *vb);
  343. void (*buf_finish)(struct vb2_buffer *vb);
  344. void (*buf_cleanup)(struct vb2_buffer *vb);
  345. int (*start_streaming)(struct vb2_queue *q, unsigned int count);
  346. void (*stop_streaming)(struct vb2_queue *q);
  347. void (*buf_queue)(struct vb2_buffer *vb);
  348. };
  349. /**
  350. * struct vb2_ops - driver-specific callbacks
  351. *
  352. * @fill_user_buffer: given a vb2_buffer fill in the userspace structure.
  353. * For V4L2 this is a struct v4l2_buffer.
  354. * @fill_vb2_buffer: given a userspace structure, fill in the vb2_buffer.
  355. * If the userspace structure is invalid, then this op
  356. * will return an error.
  357. * @copy_timestamp: copy the timestamp from a userspace structure to
  358. * the vb2_buffer struct.
  359. */
  360. struct vb2_buf_ops {
  361. void (*fill_user_buffer)(struct vb2_buffer *vb, void *pb);
  362. int (*fill_vb2_buffer)(struct vb2_buffer *vb, const void *pb,
  363. struct vb2_plane *planes);
  364. void (*copy_timestamp)(struct vb2_buffer *vb, const void *pb);
  365. };
  366. /**
  367. * struct vb2_queue - a videobuf queue
  368. *
  369. * @type: private buffer type whose content is defined by the vb2-core
  370. * caller. For example, for V4L2, it should match
  371. * the V4L2_BUF_TYPE_* in include/uapi/linux/videodev2.h
  372. * @io_modes: supported io methods (see vb2_io_modes enum)
  373. * @fileio_read_once: report EOF after reading the first buffer
  374. * @fileio_write_immediately: queue buffer after each write() call
  375. * @allow_zero_bytesused: allow bytesused == 0 to be passed to the driver
  376. * @lock: pointer to a mutex that protects the vb2_queue struct. The
  377. * driver can set this to a mutex to let the v4l2 core serialize
  378. * the queuing ioctls. If the driver wants to handle locking
  379. * itself, then this should be set to NULL. This lock is not used
  380. * by the videobuf2 core API.
  381. * @owner: The filehandle that 'owns' the buffers, i.e. the filehandle
  382. * that called reqbufs, create_buffers or started fileio.
  383. * This field is not used by the videobuf2 core API, but it allows
  384. * drivers to easily associate an owner filehandle with the queue.
  385. * @ops: driver-specific callbacks
  386. * @mem_ops: memory allocator specific callbacks
  387. * @buf_ops: callbacks to deliver buffer information
  388. * between user-space and kernel-space
  389. * @drv_priv: driver private data
  390. * @buf_struct_size: size of the driver-specific buffer structure;
  391. * "0" indicates the driver doesn't want to use a custom buffer
  392. * structure type. for example, sizeof(struct vb2_v4l2_buffer)
  393. * will be used for v4l2.
  394. * @timestamp_flags: Timestamp flags; V4L2_BUF_FLAG_TIMESTAMP_* and
  395. * V4L2_BUF_FLAG_TSTAMP_SRC_*
  396. * @gfp_flags: additional gfp flags used when allocating the buffers.
  397. * Typically this is 0, but it may be e.g. GFP_DMA or __GFP_DMA32
  398. * to force the buffer allocation to a specific memory zone.
  399. * @min_buffers_needed: the minimum number of buffers needed before
  400. * start_streaming() can be called. Used when a DMA engine
  401. * cannot be started unless at least this number of buffers
  402. * have been queued into the driver.
  403. */
  404. /*
  405. * Private elements (won't appear at the DocBook):
  406. * @mmap_lock: private mutex used when buffers are allocated/freed/mmapped
  407. * @memory: current memory type used
  408. * @bufs: videobuf buffer structures
  409. * @num_buffers: number of allocated/used buffers
  410. * @queued_list: list of buffers currently queued from userspace
  411. * @queued_count: number of buffers queued and ready for streaming.
  412. * @owned_by_drv_count: number of buffers owned by the driver
  413. * @done_list: list of buffers ready to be dequeued to userspace
  414. * @done_lock: lock to protect done_list list
  415. * @done_wq: waitqueue for processes waiting for buffers ready to be dequeued
  416. * @alloc_ctx: memory type/allocator-specific contexts for each plane
  417. * @streaming: current streaming state
  418. * @start_streaming_called: start_streaming() was called successfully and we
  419. * started streaming.
  420. * @error: a fatal error occurred on the queue
  421. * @waiting_for_buffers: used in poll() to check if vb2 is still waiting for
  422. * buffers. Only set for capture queues if qbuf has not yet been
  423. * called since poll() needs to return POLLERR in that situation.
  424. * @is_multiplanar: set if buffer type is multiplanar
  425. * @is_output: set if buffer type is output
  426. * @copy_timestamp: set if vb2-core should set timestamps
  427. * @last_buffer_dequeued: used in poll() and DQBUF to immediately return if the
  428. * last decoded buffer was already dequeued. Set for capture queues
  429. * when a buffer with the V4L2_BUF_FLAG_LAST is dequeued.
  430. * @fileio: file io emulator internal data, used only if emulator is active
  431. * @threadio: thread io internal data, used only if thread is active
  432. */
  433. struct vb2_queue {
  434. unsigned int type;
  435. unsigned int io_modes;
  436. unsigned fileio_read_once:1;
  437. unsigned fileio_write_immediately:1;
  438. unsigned allow_zero_bytesused:1;
  439. struct mutex *lock;
  440. void *owner;
  441. const struct vb2_ops *ops;
  442. const struct vb2_mem_ops *mem_ops;
  443. const struct vb2_buf_ops *buf_ops;
  444. void *drv_priv;
  445. unsigned int buf_struct_size;
  446. u32 timestamp_flags;
  447. gfp_t gfp_flags;
  448. u32 min_buffers_needed;
  449. /* private: internal use only */
  450. struct mutex mmap_lock;
  451. unsigned int memory;
  452. struct vb2_buffer *bufs[VB2_MAX_FRAME];
  453. unsigned int num_buffers;
  454. struct list_head queued_list;
  455. unsigned int queued_count;
  456. atomic_t owned_by_drv_count;
  457. struct list_head done_list;
  458. spinlock_t done_lock;
  459. wait_queue_head_t done_wq;
  460. void *alloc_ctx[VB2_MAX_PLANES];
  461. unsigned int streaming:1;
  462. unsigned int start_streaming_called:1;
  463. unsigned int error:1;
  464. unsigned int waiting_for_buffers:1;
  465. unsigned int is_multiplanar:1;
  466. unsigned int is_output:1;
  467. unsigned int copy_timestamp:1;
  468. unsigned int last_buffer_dequeued:1;
  469. struct vb2_fileio_data *fileio;
  470. struct vb2_threadio_data *threadio;
  471. #ifdef CONFIG_VIDEO_ADV_DEBUG
  472. /*
  473. * Counters for how often these queue-related ops are
  474. * called. Used to check for unbalanced ops.
  475. */
  476. u32 cnt_queue_setup;
  477. u32 cnt_wait_prepare;
  478. u32 cnt_wait_finish;
  479. u32 cnt_start_streaming;
  480. u32 cnt_stop_streaming;
  481. #endif
  482. };
  483. void *vb2_plane_vaddr(struct vb2_buffer *vb, unsigned int plane_no);
  484. void *vb2_plane_cookie(struct vb2_buffer *vb, unsigned int plane_no);
  485. void vb2_buffer_done(struct vb2_buffer *vb, enum vb2_buffer_state state);
  486. void vb2_discard_done(struct vb2_queue *q);
  487. int vb2_wait_for_all_buffers(struct vb2_queue *q);
  488. void vb2_core_querybuf(struct vb2_queue *q, unsigned int index, void *pb);
  489. int vb2_core_reqbufs(struct vb2_queue *q, enum vb2_memory memory,
  490. unsigned int *count);
  491. int vb2_core_create_bufs(struct vb2_queue *q, enum vb2_memory memory,
  492. unsigned int *count, unsigned requested_planes,
  493. const unsigned int requested_sizes[]);
  494. int vb2_core_prepare_buf(struct vb2_queue *q, unsigned int index, void *pb);
  495. int vb2_core_qbuf(struct vb2_queue *q, unsigned int index, void *pb);
  496. int vb2_core_dqbuf(struct vb2_queue *q, unsigned int *pindex, void *pb,
  497. bool nonblocking);
  498. int vb2_core_streamon(struct vb2_queue *q, unsigned int type);
  499. int vb2_core_streamoff(struct vb2_queue *q, unsigned int type);
  500. int vb2_core_expbuf(struct vb2_queue *q, int *fd, unsigned int type,
  501. unsigned int index, unsigned int plane, unsigned int flags);
  502. int vb2_core_queue_init(struct vb2_queue *q);
  503. void vb2_core_queue_release(struct vb2_queue *q);
  504. void vb2_queue_error(struct vb2_queue *q);
  505. int vb2_mmap(struct vb2_queue *q, struct vm_area_struct *vma);
  506. #ifndef CONFIG_MMU
  507. unsigned long vb2_get_unmapped_area(struct vb2_queue *q,
  508. unsigned long addr,
  509. unsigned long len,
  510. unsigned long pgoff,
  511. unsigned long flags);
  512. #endif
  513. unsigned int vb2_core_poll(struct vb2_queue *q, struct file *file,
  514. poll_table *wait);
  515. size_t vb2_read(struct vb2_queue *q, char __user *data, size_t count,
  516. loff_t *ppos, int nonblock);
  517. size_t vb2_write(struct vb2_queue *q, const char __user *data, size_t count,
  518. loff_t *ppos, int nonblock);
  519. /*
  520. * vb2_thread_fnc - callback function for use with vb2_thread
  521. *
  522. * This is called whenever a buffer is dequeued in the thread.
  523. */
  524. typedef int (*vb2_thread_fnc)(struct vb2_buffer *vb, void *priv);
  525. /**
  526. * vb2_thread_start() - start a thread for the given queue.
  527. * @q: videobuf queue
  528. * @fnc: callback function
  529. * @priv: priv pointer passed to the callback function
  530. * @thread_name:the name of the thread. This will be prefixed with "vb2-".
  531. *
  532. * This starts a thread that will queue and dequeue until an error occurs
  533. * or @vb2_thread_stop is called.
  534. *
  535. * This function should not be used for anything else but the videobuf2-dvb
  536. * support. If you think you have another good use-case for this, then please
  537. * contact the linux-media mailinglist first.
  538. */
  539. int vb2_thread_start(struct vb2_queue *q, vb2_thread_fnc fnc, void *priv,
  540. const char *thread_name);
  541. /**
  542. * vb2_thread_stop() - stop the thread for the given queue.
  543. * @q: videobuf queue
  544. */
  545. int vb2_thread_stop(struct vb2_queue *q);
  546. /**
  547. * vb2_is_streaming() - return streaming status of the queue
  548. * @q: videobuf queue
  549. */
  550. static inline bool vb2_is_streaming(struct vb2_queue *q)
  551. {
  552. return q->streaming;
  553. }
  554. /**
  555. * vb2_fileio_is_active() - return true if fileio is active.
  556. * @q: videobuf queue
  557. *
  558. * This returns true if read() or write() is used to stream the data
  559. * as opposed to stream I/O. This is almost never an important distinction,
  560. * except in rare cases. One such case is that using read() or write() to
  561. * stream a format using V4L2_FIELD_ALTERNATE is not allowed since there
  562. * is no way you can pass the field information of each buffer to/from
  563. * userspace. A driver that supports this field format should check for
  564. * this in the queue_setup op and reject it if this function returns true.
  565. */
  566. static inline bool vb2_fileio_is_active(struct vb2_queue *q)
  567. {
  568. return q->fileio;
  569. }
  570. /**
  571. * vb2_is_busy() - return busy status of the queue
  572. * @q: videobuf queue
  573. *
  574. * This function checks if queue has any buffers allocated.
  575. */
  576. static inline bool vb2_is_busy(struct vb2_queue *q)
  577. {
  578. return (q->num_buffers > 0);
  579. }
  580. /**
  581. * vb2_get_drv_priv() - return driver private data associated with the queue
  582. * @q: videobuf queue
  583. */
  584. static inline void *vb2_get_drv_priv(struct vb2_queue *q)
  585. {
  586. return q->drv_priv;
  587. }
  588. /**
  589. * vb2_set_plane_payload() - set bytesused for the plane plane_no
  590. * @vb: buffer for which plane payload should be set
  591. * @plane_no: plane number for which payload should be set
  592. * @size: payload in bytes
  593. */
  594. static inline void vb2_set_plane_payload(struct vb2_buffer *vb,
  595. unsigned int plane_no, unsigned long size)
  596. {
  597. if (plane_no < vb->num_planes)
  598. vb->planes[plane_no].bytesused = size;
  599. }
  600. /**
  601. * vb2_get_plane_payload() - get bytesused for the plane plane_no
  602. * @vb: buffer for which plane payload should be set
  603. * @plane_no: plane number for which payload should be set
  604. */
  605. static inline unsigned long vb2_get_plane_payload(struct vb2_buffer *vb,
  606. unsigned int plane_no)
  607. {
  608. if (plane_no < vb->num_planes)
  609. return vb->planes[plane_no].bytesused;
  610. return 0;
  611. }
  612. /**
  613. * vb2_plane_size() - return plane size in bytes
  614. * @vb: buffer for which plane size should be returned
  615. * @plane_no: plane number for which size should be returned
  616. */
  617. static inline unsigned long
  618. vb2_plane_size(struct vb2_buffer *vb, unsigned int plane_no)
  619. {
  620. if (plane_no < vb->num_planes)
  621. return vb->planes[plane_no].length;
  622. return 0;
  623. }
  624. /**
  625. * vb2_start_streaming_called() - return streaming status of driver
  626. * @q: videobuf queue
  627. */
  628. static inline bool vb2_start_streaming_called(struct vb2_queue *q)
  629. {
  630. return q->start_streaming_called;
  631. }
  632. /**
  633. * vb2_clear_last_buffer_dequeued() - clear last buffer dequeued flag of queue
  634. * @q: videobuf queue
  635. */
  636. static inline void vb2_clear_last_buffer_dequeued(struct vb2_queue *q)
  637. {
  638. q->last_buffer_dequeued = false;
  639. }
  640. /*
  641. * The following functions are not part of the vb2 core API, but are useful
  642. * functions for videobuf2-*.
  643. */
  644. bool vb2_buffer_in_use(struct vb2_queue *q, struct vb2_buffer *vb);
  645. int vb2_verify_memory_type(struct vb2_queue *q,
  646. enum vb2_memory memory, unsigned int type);
  647. #endif /* _MEDIA_VIDEOBUF2_CORE_H */