videobuf2-core.h 15 KB

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