videobuf2-core.h 49 KB

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