videobuf2-core.h 51 KB

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