videobuf2-core.h 44 KB

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