videobuf2-core.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. /*
  2. * videobuf2-core.h - V4L2 driver helper 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/videodev2.h>
  18. struct vb2_alloc_ctx;
  19. struct vb2_fileio_data;
  20. /**
  21. * struct vb2_mem_ops - memory handling/memory allocator operations
  22. * @alloc: allocate video memory and, optionally, allocator private data,
  23. * return NULL on failure or a pointer to allocator private,
  24. * per-buffer data on success; the returned private structure
  25. * will then be passed as buf_priv argument to other ops in this
  26. * structure
  27. * @put: inform the allocator that the buffer will no longer be used;
  28. * usually will result in the allocator freeing the buffer (if
  29. * no other users of this buffer are present); the buf_priv
  30. * argument is the allocator private per-buffer structure
  31. * previously returned from the alloc callback
  32. * @get_userptr: acquire userspace memory for a hardware operation; used for
  33. * USERPTR memory types; vaddr is the address passed to the
  34. * videobuf layer when queuing a video buffer of USERPTR type;
  35. * should return an allocator private per-buffer structure
  36. * associated with the buffer on success, NULL on failure;
  37. * the returned private structure will then be passed as buf_priv
  38. * argument to other ops in this structure
  39. * @put_userptr: inform the allocator that a USERPTR buffer will no longer
  40. * be used
  41. * @vaddr: return a kernel virtual address to a given memory buffer
  42. * associated with the passed private structure or NULL if no
  43. * such mapping exists
  44. * @cookie: return allocator specific cookie for a given memory buffer
  45. * associated with the passed private structure or NULL if not
  46. * available
  47. * @num_users: return the current number of users of a memory buffer;
  48. * return 1 if the videobuf layer (or actually the driver using
  49. * it) is the only user
  50. * @mmap: setup a userspace mapping for a given memory buffer under
  51. * the provided virtual memory region
  52. *
  53. * Required ops for USERPTR types: get_userptr, put_userptr.
  54. * Required ops for MMAP types: alloc, put, num_users, mmap.
  55. * Required ops for read/write access types: alloc, put, num_users, vaddr
  56. */
  57. struct vb2_mem_ops {
  58. void *(*alloc)(void *alloc_ctx, unsigned long size);
  59. void (*put)(void *buf_priv);
  60. void *(*get_userptr)(void *alloc_ctx, unsigned long vaddr,
  61. unsigned long size, int write);
  62. void (*put_userptr)(void *buf_priv);
  63. void *(*vaddr)(void *buf_priv);
  64. void *(*cookie)(void *buf_priv);
  65. unsigned int (*num_users)(void *buf_priv);
  66. int (*mmap)(void *buf_priv, struct vm_area_struct *vma);
  67. };
  68. struct vb2_plane {
  69. void *mem_priv;
  70. int mapped:1;
  71. };
  72. /**
  73. * enum vb2_io_modes - queue access methods
  74. * @VB2_MMAP: driver supports MMAP with streaming API
  75. * @VB2_USERPTR: driver supports USERPTR with streaming API
  76. * @VB2_READ: driver supports read() style access
  77. * @VB2_WRITE: driver supports write() style access
  78. */
  79. enum vb2_io_modes {
  80. VB2_MMAP = (1 << 0),
  81. VB2_USERPTR = (1 << 1),
  82. VB2_READ = (1 << 2),
  83. VB2_WRITE = (1 << 3),
  84. };
  85. /**
  86. * enum vb2_fileio_flags - flags for selecting a mode of the file io emulator,
  87. * by default the 'streaming' style is used by the file io emulator
  88. * @VB2_FILEIO_READ_ONCE: report EOF after reading the first buffer
  89. * @VB2_FILEIO_WRITE_IMMEDIATELY: queue buffer after each write() call
  90. */
  91. enum vb2_fileio_flags {
  92. VB2_FILEIO_READ_ONCE = (1 << 0),
  93. VB2_FILEIO_WRITE_IMMEDIATELY = (1 << 1),
  94. };
  95. /**
  96. * enum vb2_buffer_state - current video buffer state
  97. * @VB2_BUF_STATE_DEQUEUED: buffer under userspace control
  98. * @VB2_BUF_STATE_QUEUED: buffer queued in videobuf, but not in driver
  99. * @VB2_BUF_STATE_ACTIVE: buffer queued in driver and possibly used
  100. * in a hardware operation
  101. * @VB2_BUF_STATE_DONE: buffer returned from driver to videobuf, but
  102. * not yet dequeued to userspace
  103. * @VB2_BUF_STATE_ERROR: same as above, but the operation on the buffer
  104. * has ended with an error, which will be reported
  105. * to the userspace when it is dequeued
  106. */
  107. enum vb2_buffer_state {
  108. VB2_BUF_STATE_DEQUEUED,
  109. VB2_BUF_STATE_QUEUED,
  110. VB2_BUF_STATE_ACTIVE,
  111. VB2_BUF_STATE_DONE,
  112. VB2_BUF_STATE_ERROR,
  113. };
  114. struct vb2_queue;
  115. /**
  116. * struct vb2_buffer - represents a video buffer
  117. * @v4l2_buf: struct v4l2_buffer associated with this buffer; can
  118. * be read by the driver and relevant entries can be
  119. * changed by the driver in case of CAPTURE types
  120. * (such as timestamp)
  121. * @v4l2_planes: struct v4l2_planes associated with this buffer; can
  122. * be read by the driver and relevant entries can be
  123. * changed by the driver in case of CAPTURE types
  124. * (such as bytesused); NOTE that even for single-planar
  125. * types, the v4l2_planes[0] struct should be used
  126. * instead of v4l2_buf for filling bytesused - drivers
  127. * should use the vb2_set_plane_payload() function for that
  128. * @vb2_queue: the queue to which this driver belongs
  129. * @num_planes: number of planes in the buffer
  130. * on an internal driver queue
  131. * @state: current buffer state; do not change
  132. * @queued_entry: entry on the queued buffers list, which holds all
  133. * buffers queued from userspace
  134. * @done_entry: entry on the list that stores all buffers ready to
  135. * be dequeued to userspace
  136. * @planes: private per-plane information; do not change
  137. * @num_planes_mapped: number of mapped planes; do not change
  138. */
  139. struct vb2_buffer {
  140. struct v4l2_buffer v4l2_buf;
  141. struct v4l2_plane v4l2_planes[VIDEO_MAX_PLANES];
  142. struct vb2_queue *vb2_queue;
  143. unsigned int num_planes;
  144. /* Private: internal use only */
  145. enum vb2_buffer_state state;
  146. struct list_head queued_entry;
  147. struct list_head done_entry;
  148. struct vb2_plane planes[VIDEO_MAX_PLANES];
  149. unsigned int num_planes_mapped;
  150. };
  151. /**
  152. * struct vb2_ops - driver-specific callbacks
  153. *
  154. * @queue_setup: called from a VIDIOC_REQBUFS handler, before
  155. * memory allocation; driver should return the required
  156. * number of buffers in num_buffers, the required number
  157. * of planes per buffer in num_planes; the size of each
  158. * plane should be set in the sizes[] array and optional
  159. * per-plane allocator specific context in alloc_ctxs[]
  160. * array
  161. * @wait_prepare: release any locks taken while calling vb2 functions;
  162. * it is called before an ioctl needs to wait for a new
  163. * buffer to arrive; required to avoid a deadlock in
  164. * blocking access type
  165. * @wait_finish: reacquire all locks released in the previous callback;
  166. * required to continue operation after sleeping while
  167. * waiting for a new buffer to arrive
  168. * @buf_init: called once after allocating a buffer (in MMAP case)
  169. * or after acquiring a new USERPTR buffer; drivers may
  170. * perform additional buffer-related initialization;
  171. * initialization failure (return != 0) will prevent
  172. * queue setup from completing successfully; optional
  173. * @buf_prepare: called every time the buffer is queued from userspace;
  174. * drivers may perform any initialization required before
  175. * each hardware operation in this callback;
  176. * if an error is returned, the buffer will not be queued
  177. * in driver; optional
  178. * @buf_finish: called before every dequeue of the buffer back to
  179. * userspace; drivers may perform any operations required
  180. * before userspace accesses the buffer; optional
  181. * @buf_cleanup: called once before the buffer is freed; drivers may
  182. * perform any additional cleanup; optional
  183. * @start_streaming: called once before entering 'streaming' state; enables
  184. * driver to receive buffers over buf_queue() callback
  185. * @stop_streaming: called when 'streaming' state must be disabled; driver
  186. * should stop any DMA transactions or wait until they
  187. * finish and give back all buffers it got from buf_queue()
  188. * callback; may use vb2_wait_for_all_buffers() function
  189. * @buf_queue: passes buffer vb to the driver; driver may start
  190. * hardware operation on this buffer; driver should give
  191. * the buffer back by calling vb2_buffer_done() function
  192. */
  193. struct vb2_ops {
  194. int (*queue_setup)(struct vb2_queue *q, unsigned int *num_buffers,
  195. unsigned int *num_planes, unsigned long sizes[],
  196. void *alloc_ctxs[]);
  197. void (*wait_prepare)(struct vb2_queue *q);
  198. void (*wait_finish)(struct vb2_queue *q);
  199. int (*buf_init)(struct vb2_buffer *vb);
  200. int (*buf_prepare)(struct vb2_buffer *vb);
  201. int (*buf_finish)(struct vb2_buffer *vb);
  202. void (*buf_cleanup)(struct vb2_buffer *vb);
  203. int (*start_streaming)(struct vb2_queue *q);
  204. int (*stop_streaming)(struct vb2_queue *q);
  205. void (*buf_queue)(struct vb2_buffer *vb);
  206. };
  207. /**
  208. * struct vb2_queue - a videobuf queue
  209. *
  210. * @type: queue type (see V4L2_BUF_TYPE_* in linux/videodev2.h
  211. * @io_modes: supported io methods (see vb2_io_modes enum)
  212. * @io_flags: additional io flags (see vb2_fileio_flags enum)
  213. * @ops: driver-specific callbacks
  214. * @mem_ops: memory allocator specific callbacks
  215. * @drv_priv: driver private data
  216. * @buf_struct_size: size of the driver-specific buffer structure;
  217. * "0" indicates the driver doesn't want to use a custom buffer
  218. * structure type, so sizeof(struct vb2_buffer) will is used
  219. *
  220. * @memory: current memory type used
  221. * @bufs: videobuf buffer structures
  222. * @num_buffers: number of allocated/used buffers
  223. * @queued_list: list of buffers currently queued from userspace
  224. * @queued_count: number of buffers owned by the driver
  225. * @done_list: list of buffers ready to be dequeued to userspace
  226. * @done_lock: lock to protect done_list list
  227. * @done_wq: waitqueue for processes waiting for buffers ready to be dequeued
  228. * @alloc_ctx: memory type/allocator-specific contexts for each plane
  229. * @streaming: current streaming state
  230. * @fileio: file io emulator internal data, used only if emulator is active
  231. */
  232. struct vb2_queue {
  233. enum v4l2_buf_type type;
  234. unsigned int io_modes;
  235. unsigned int io_flags;
  236. const struct vb2_ops *ops;
  237. const struct vb2_mem_ops *mem_ops;
  238. void *drv_priv;
  239. unsigned int buf_struct_size;
  240. /* private: internal use only */
  241. enum v4l2_memory memory;
  242. struct vb2_buffer *bufs[VIDEO_MAX_FRAME];
  243. unsigned int num_buffers;
  244. struct list_head queued_list;
  245. atomic_t queued_count;
  246. struct list_head done_list;
  247. spinlock_t done_lock;
  248. wait_queue_head_t done_wq;
  249. void *alloc_ctx[VIDEO_MAX_PLANES];
  250. unsigned int streaming:1;
  251. struct vb2_fileio_data *fileio;
  252. };
  253. void *vb2_plane_vaddr(struct vb2_buffer *vb, unsigned int plane_no);
  254. void *vb2_plane_cookie(struct vb2_buffer *vb, unsigned int plane_no);
  255. void vb2_buffer_done(struct vb2_buffer *vb, enum vb2_buffer_state state);
  256. int vb2_wait_for_all_buffers(struct vb2_queue *q);
  257. int vb2_querybuf(struct vb2_queue *q, struct v4l2_buffer *b);
  258. int vb2_reqbufs(struct vb2_queue *q, struct v4l2_requestbuffers *req);
  259. int vb2_queue_init(struct vb2_queue *q);
  260. void vb2_queue_release(struct vb2_queue *q);
  261. int vb2_qbuf(struct vb2_queue *q, struct v4l2_buffer *b);
  262. int vb2_dqbuf(struct vb2_queue *q, struct v4l2_buffer *b, bool nonblocking);
  263. int vb2_streamon(struct vb2_queue *q, enum v4l2_buf_type type);
  264. int vb2_streamoff(struct vb2_queue *q, enum v4l2_buf_type type);
  265. int vb2_mmap(struct vb2_queue *q, struct vm_area_struct *vma);
  266. unsigned int vb2_poll(struct vb2_queue *q, struct file *file, poll_table *wait);
  267. size_t vb2_read(struct vb2_queue *q, char __user *data, size_t count,
  268. loff_t *ppos, int nonblock);
  269. size_t vb2_write(struct vb2_queue *q, char __user *data, size_t count,
  270. loff_t *ppos, int nonblock);
  271. /**
  272. * vb2_is_streaming() - return streaming status of the queue
  273. * @q: videobuf queue
  274. */
  275. static inline bool vb2_is_streaming(struct vb2_queue *q)
  276. {
  277. return q->streaming;
  278. }
  279. /**
  280. * vb2_is_busy() - return busy status of the queue
  281. * @q: videobuf queue
  282. *
  283. * This function checks if queue has any buffers allocated.
  284. */
  285. static inline bool vb2_is_busy(struct vb2_queue *q)
  286. {
  287. return (q->num_buffers > 0);
  288. }
  289. /**
  290. * vb2_get_drv_priv() - return driver private data associated with the queue
  291. * @q: videobuf queue
  292. */
  293. static inline void *vb2_get_drv_priv(struct vb2_queue *q)
  294. {
  295. return q->drv_priv;
  296. }
  297. /**
  298. * vb2_set_plane_payload() - set bytesused for the plane plane_no
  299. * @vb: buffer for which plane payload should be set
  300. * @plane_no: plane number for which payload should be set
  301. * @size: payload in bytes
  302. */
  303. static inline void vb2_set_plane_payload(struct vb2_buffer *vb,
  304. unsigned int plane_no, unsigned long size)
  305. {
  306. if (plane_no < vb->num_planes)
  307. vb->v4l2_planes[plane_no].bytesused = size;
  308. }
  309. /**
  310. * vb2_get_plane_payload() - get bytesused for the plane plane_no
  311. * @vb: buffer for which plane payload should be set
  312. * @plane_no: plane number for which payload should be set
  313. * @size: payload in bytes
  314. */
  315. static inline unsigned long vb2_get_plane_payload(struct vb2_buffer *vb,
  316. unsigned int plane_no)
  317. {
  318. if (plane_no < vb->num_planes)
  319. return vb->v4l2_planes[plane_no].bytesused;
  320. return 0;
  321. }
  322. /**
  323. * vb2_plane_size() - return plane size in bytes
  324. * @vb: buffer for which plane size should be returned
  325. * @plane_no: plane number for which size should be returned
  326. */
  327. static inline unsigned long
  328. vb2_plane_size(struct vb2_buffer *vb, unsigned int plane_no)
  329. {
  330. if (plane_no < vb->num_planes)
  331. return vb->v4l2_planes[plane_no].length;
  332. return 0;
  333. }
  334. #endif /* _MEDIA_VIDEOBUF2_CORE_H */