videobuf2-core.h 25 KB

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