videobuf2-core.h 39 KB

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