videobuf2-core.h 46 KB

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