videobuf2-core.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  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. #include <linux/dma-buf.h>
  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. * @attach_dmabuf: attach a shared struct dma_buf for a hardware operation;
  43. * used for DMABUF memory types; alloc_ctx is the alloc context
  44. * dbuf is the shared dma_buf; returns NULL on failure;
  45. * allocator private per-buffer structure on success;
  46. * this needs to be used for further accesses to the buffer
  47. * @detach_dmabuf: inform the exporter of the buffer that the current DMABUF
  48. * buffer is no longer used; the buf_priv argument is the
  49. * allocator private per-buffer structure previously returned
  50. * from the attach_dmabuf callback
  51. * @map_dmabuf: request for access to the dmabuf from allocator; the allocator
  52. * of dmabuf is informed that this driver is going to use the
  53. * dmabuf
  54. * @unmap_dmabuf: releases access control to the dmabuf - allocator is notified
  55. * that this driver is done using the dmabuf for now
  56. * @prepare: called every time the buffer is passed from userspace to the
  57. * driver, useful for cache synchronisation, optional
  58. * @finish: called every time the buffer is passed back from the driver
  59. * to the userspace, also optional
  60. * @vaddr: return a kernel virtual address to a given memory buffer
  61. * associated with the passed private structure or NULL if no
  62. * such mapping exists
  63. * @cookie: return allocator specific cookie for a given memory buffer
  64. * associated with the passed private structure or NULL if not
  65. * available
  66. * @num_users: return the current number of users of a memory buffer;
  67. * return 1 if the videobuf layer (or actually the driver using
  68. * it) is the only user
  69. * @mmap: setup a userspace mapping for a given memory buffer under
  70. * the provided virtual memory region
  71. *
  72. * Required ops for USERPTR types: get_userptr, put_userptr.
  73. * Required ops for MMAP types: alloc, put, num_users, mmap.
  74. * Required ops for read/write access types: alloc, put, num_users, vaddr
  75. * Required ops for DMABUF types: attach_dmabuf, detach_dmabuf, map_dmabuf,
  76. * unmap_dmabuf.
  77. */
  78. struct vb2_mem_ops {
  79. void *(*alloc)(void *alloc_ctx, unsigned long size);
  80. void (*put)(void *buf_priv);
  81. struct dma_buf *(*get_dmabuf)(void *buf_priv);
  82. void *(*get_userptr)(void *alloc_ctx, unsigned long vaddr,
  83. unsigned long size, int write);
  84. void (*put_userptr)(void *buf_priv);
  85. void (*prepare)(void *buf_priv);
  86. void (*finish)(void *buf_priv);
  87. void *(*attach_dmabuf)(void *alloc_ctx, struct dma_buf *dbuf,
  88. unsigned long size, int write);
  89. void (*detach_dmabuf)(void *buf_priv);
  90. int (*map_dmabuf)(void *buf_priv);
  91. void (*unmap_dmabuf)(void *buf_priv);
  92. void *(*vaddr)(void *buf_priv);
  93. void *(*cookie)(void *buf_priv);
  94. unsigned int (*num_users)(void *buf_priv);
  95. int (*mmap)(void *buf_priv, struct vm_area_struct *vma);
  96. };
  97. struct vb2_plane {
  98. void *mem_priv;
  99. struct dma_buf *dbuf;
  100. unsigned int dbuf_mapped;
  101. };
  102. /**
  103. * enum vb2_io_modes - queue access methods
  104. * @VB2_MMAP: driver supports MMAP with streaming API
  105. * @VB2_USERPTR: driver supports USERPTR with streaming API
  106. * @VB2_READ: driver supports read() style access
  107. * @VB2_WRITE: driver supports write() style access
  108. * @VB2_DMABUF: driver supports DMABUF with streaming API
  109. */
  110. enum vb2_io_modes {
  111. VB2_MMAP = (1 << 0),
  112. VB2_USERPTR = (1 << 1),
  113. VB2_READ = (1 << 2),
  114. VB2_WRITE = (1 << 3),
  115. VB2_DMABUF = (1 << 4),
  116. };
  117. /**
  118. * enum vb2_fileio_flags - flags for selecting a mode of the file io emulator,
  119. * by default the 'streaming' style is used by the file io emulator
  120. * @VB2_FILEIO_READ_ONCE: report EOF after reading the first buffer
  121. * @VB2_FILEIO_WRITE_IMMEDIATELY: queue buffer after each write() call
  122. */
  123. enum vb2_fileio_flags {
  124. VB2_FILEIO_READ_ONCE = (1 << 0),
  125. VB2_FILEIO_WRITE_IMMEDIATELY = (1 << 1),
  126. };
  127. /**
  128. * enum vb2_buffer_state - current video buffer state
  129. * @VB2_BUF_STATE_DEQUEUED: buffer under userspace control
  130. * @VB2_BUF_STATE_PREPARED: buffer prepared in videobuf and by the driver
  131. * @VB2_BUF_STATE_QUEUED: buffer queued in videobuf, but not in driver
  132. * @VB2_BUF_STATE_ACTIVE: buffer queued in driver and possibly used
  133. * in a hardware operation
  134. * @VB2_BUF_STATE_DONE: buffer returned from driver to videobuf, but
  135. * not yet dequeued to userspace
  136. * @VB2_BUF_STATE_ERROR: same as above, but the operation on the buffer
  137. * has ended with an error, which will be reported
  138. * to the userspace when it is dequeued
  139. */
  140. enum vb2_buffer_state {
  141. VB2_BUF_STATE_DEQUEUED,
  142. VB2_BUF_STATE_PREPARED,
  143. VB2_BUF_STATE_QUEUED,
  144. VB2_BUF_STATE_ACTIVE,
  145. VB2_BUF_STATE_DONE,
  146. VB2_BUF_STATE_ERROR,
  147. };
  148. struct vb2_queue;
  149. /**
  150. * struct vb2_buffer - represents a video buffer
  151. * @v4l2_buf: struct v4l2_buffer associated with this buffer; can
  152. * be read by the driver and relevant entries can be
  153. * changed by the driver in case of CAPTURE types
  154. * (such as timestamp)
  155. * @v4l2_planes: struct v4l2_planes associated with this buffer; can
  156. * be read by the driver and relevant entries can be
  157. * changed by the driver in case of CAPTURE types
  158. * (such as bytesused); NOTE that even for single-planar
  159. * types, the v4l2_planes[0] struct should be used
  160. * instead of v4l2_buf for filling bytesused - drivers
  161. * should use the vb2_set_plane_payload() function for that
  162. * @vb2_queue: the queue to which this driver belongs
  163. * @num_planes: number of planes in the buffer
  164. * on an internal driver queue
  165. * @state: current buffer state; do not change
  166. * @queued_entry: entry on the queued buffers list, which holds all
  167. * buffers queued from userspace
  168. * @done_entry: entry on the list that stores all buffers ready to
  169. * be dequeued to userspace
  170. * @planes: private per-plane information; do not change
  171. */
  172. struct vb2_buffer {
  173. struct v4l2_buffer v4l2_buf;
  174. struct v4l2_plane v4l2_planes[VIDEO_MAX_PLANES];
  175. struct vb2_queue *vb2_queue;
  176. unsigned int num_planes;
  177. /* Private: internal use only */
  178. enum vb2_buffer_state state;
  179. struct list_head queued_entry;
  180. struct list_head done_entry;
  181. struct vb2_plane planes[VIDEO_MAX_PLANES];
  182. };
  183. /**
  184. * struct vb2_ops - driver-specific callbacks
  185. *
  186. * @queue_setup: called from VIDIOC_REQBUFS and VIDIOC_CREATE_BUFS
  187. * handlers before memory allocation, or, if
  188. * *num_planes != 0, after the allocation to verify a
  189. * smaller number of buffers. Driver should return
  190. * the required number of buffers in *num_buffers, the
  191. * required number of planes per buffer in *num_planes; the
  192. * size of each plane should be set in the sizes[] array
  193. * and optional per-plane allocator specific context in the
  194. * alloc_ctxs[] array. When called from VIDIOC_REQBUFS,
  195. * fmt == NULL, the driver has to use the currently
  196. * configured format and *num_buffers is the total number
  197. * of buffers, that are being allocated. When called from
  198. * VIDIOC_CREATE_BUFS, fmt != NULL and it describes the
  199. * target frame format. In this case *num_buffers are being
  200. * allocated additionally to q->num_buffers.
  201. * @wait_prepare: release any locks taken while calling vb2 functions;
  202. * it is called before an ioctl needs to wait for a new
  203. * buffer to arrive; required to avoid a deadlock in
  204. * blocking access type
  205. * @wait_finish: reacquire all locks released in the previous callback;
  206. * required to continue operation after sleeping while
  207. * waiting for a new buffer to arrive
  208. * @buf_init: called once after allocating a buffer (in MMAP case)
  209. * or after acquiring a new USERPTR buffer; drivers may
  210. * perform additional buffer-related initialization;
  211. * initialization failure (return != 0) will prevent
  212. * queue setup from completing successfully; optional
  213. * @buf_prepare: called every time the buffer is queued from userspace
  214. * and from the VIDIOC_PREPARE_BUF ioctl; drivers may
  215. * perform any initialization required before each hardware
  216. * operation in this callback; if an error is returned, the
  217. * buffer will not be queued in driver; optional
  218. * @buf_finish: called before every dequeue of the buffer back to
  219. * userspace; drivers may perform any operations required
  220. * before userspace accesses the buffer; optional
  221. * @buf_cleanup: called once before the buffer is freed; drivers may
  222. * perform any additional cleanup; optional
  223. * @start_streaming: called once to enter 'streaming' state; the driver may
  224. * receive buffers with @buf_queue callback before
  225. * @start_streaming is called; the driver gets the number
  226. * of already queued buffers in count parameter; driver
  227. * can return an error if hardware fails or not enough
  228. * buffers has been queued, in such case all buffers that
  229. * have been already given by the @buf_queue callback are
  230. * invalidated.
  231. * @stop_streaming: called when 'streaming' state must be disabled; driver
  232. * should stop any DMA transactions or wait until they
  233. * finish and give back all buffers it got from buf_queue()
  234. * callback; may use vb2_wait_for_all_buffers() function
  235. * @buf_queue: passes buffer vb to the driver; driver may start
  236. * hardware operation on this buffer; driver should give
  237. * the buffer back by calling vb2_buffer_done() function;
  238. * it is allways called after calling STREAMON ioctl;
  239. * might be called before start_streaming callback if user
  240. * pre-queued buffers before calling STREAMON
  241. */
  242. struct vb2_ops {
  243. int (*queue_setup)(struct vb2_queue *q, const struct v4l2_format *fmt,
  244. unsigned int *num_buffers, unsigned int *num_planes,
  245. unsigned int sizes[], void *alloc_ctxs[]);
  246. void (*wait_prepare)(struct vb2_queue *q);
  247. void (*wait_finish)(struct vb2_queue *q);
  248. int (*buf_init)(struct vb2_buffer *vb);
  249. int (*buf_prepare)(struct vb2_buffer *vb);
  250. int (*buf_finish)(struct vb2_buffer *vb);
  251. void (*buf_cleanup)(struct vb2_buffer *vb);
  252. int (*start_streaming)(struct vb2_queue *q, unsigned int count);
  253. int (*stop_streaming)(struct vb2_queue *q);
  254. void (*buf_queue)(struct vb2_buffer *vb);
  255. };
  256. struct v4l2_fh;
  257. /**
  258. * struct vb2_queue - a videobuf queue
  259. *
  260. * @type: queue type (see V4L2_BUF_TYPE_* in linux/videodev2.h
  261. * @io_modes: supported io methods (see vb2_io_modes enum)
  262. * @io_flags: additional io flags (see vb2_fileio_flags enum)
  263. * @lock: pointer to a mutex that protects the vb2_queue struct. The
  264. * driver can set this to a mutex to let the v4l2 core serialize
  265. * the queuing ioctls. If the driver wants to handle locking
  266. * itself, then this should be set to NULL. This lock is not used
  267. * by the videobuf2 core API.
  268. * @owner: The filehandle that 'owns' the buffers, i.e. the filehandle
  269. * that called reqbufs, create_buffers or started fileio.
  270. * This field is not used by the videobuf2 core API, but it allows
  271. * drivers to easily associate an owner filehandle with the queue.
  272. * @ops: driver-specific callbacks
  273. * @mem_ops: memory allocator specific callbacks
  274. * @drv_priv: driver private data
  275. * @buf_struct_size: size of the driver-specific buffer structure;
  276. * "0" indicates the driver doesn't want to use a custom buffer
  277. * structure type, so sizeof(struct vb2_buffer) will is used
  278. *
  279. * @memory: current memory type used
  280. * @bufs: videobuf buffer structures
  281. * @num_buffers: number of allocated/used buffers
  282. * @queued_list: list of buffers currently queued from userspace
  283. * @queued_count: number of buffers owned by the driver
  284. * @done_list: list of buffers ready to be dequeued to userspace
  285. * @done_lock: lock to protect done_list list
  286. * @done_wq: waitqueue for processes waiting for buffers ready to be dequeued
  287. * @alloc_ctx: memory type/allocator-specific contexts for each plane
  288. * @streaming: current streaming state
  289. * @fileio: file io emulator internal data, used only if emulator is active
  290. */
  291. struct vb2_queue {
  292. enum v4l2_buf_type type;
  293. unsigned int io_modes;
  294. unsigned int io_flags;
  295. struct mutex *lock;
  296. struct v4l2_fh *owner;
  297. const struct vb2_ops *ops;
  298. const struct vb2_mem_ops *mem_ops;
  299. void *drv_priv;
  300. unsigned int buf_struct_size;
  301. /* private: internal use only */
  302. enum v4l2_memory memory;
  303. struct vb2_buffer *bufs[VIDEO_MAX_FRAME];
  304. unsigned int num_buffers;
  305. struct list_head queued_list;
  306. atomic_t queued_count;
  307. struct list_head done_list;
  308. spinlock_t done_lock;
  309. wait_queue_head_t done_wq;
  310. void *alloc_ctx[VIDEO_MAX_PLANES];
  311. unsigned int plane_sizes[VIDEO_MAX_PLANES];
  312. unsigned int streaming:1;
  313. struct vb2_fileio_data *fileio;
  314. };
  315. void *vb2_plane_vaddr(struct vb2_buffer *vb, unsigned int plane_no);
  316. void *vb2_plane_cookie(struct vb2_buffer *vb, unsigned int plane_no);
  317. void vb2_buffer_done(struct vb2_buffer *vb, enum vb2_buffer_state state);
  318. int vb2_wait_for_all_buffers(struct vb2_queue *q);
  319. int vb2_querybuf(struct vb2_queue *q, struct v4l2_buffer *b);
  320. int vb2_reqbufs(struct vb2_queue *q, struct v4l2_requestbuffers *req);
  321. int vb2_create_bufs(struct vb2_queue *q, struct v4l2_create_buffers *create);
  322. int vb2_prepare_buf(struct vb2_queue *q, struct v4l2_buffer *b);
  323. int __must_check vb2_queue_init(struct vb2_queue *q);
  324. void vb2_queue_release(struct vb2_queue *q);
  325. int vb2_qbuf(struct vb2_queue *q, struct v4l2_buffer *b);
  326. int vb2_expbuf(struct vb2_queue *q, struct v4l2_exportbuffer *eb);
  327. int vb2_dqbuf(struct vb2_queue *q, struct v4l2_buffer *b, bool nonblocking);
  328. int vb2_streamon(struct vb2_queue *q, enum v4l2_buf_type type);
  329. int vb2_streamoff(struct vb2_queue *q, enum v4l2_buf_type type);
  330. int vb2_mmap(struct vb2_queue *q, struct vm_area_struct *vma);
  331. #ifndef CONFIG_MMU
  332. unsigned long vb2_get_unmapped_area(struct vb2_queue *q,
  333. unsigned long addr,
  334. unsigned long len,
  335. unsigned long pgoff,
  336. unsigned long flags);
  337. #endif
  338. unsigned int vb2_poll(struct vb2_queue *q, struct file *file, poll_table *wait);
  339. size_t vb2_read(struct vb2_queue *q, char __user *data, size_t count,
  340. loff_t *ppos, int nonblock);
  341. size_t vb2_write(struct vb2_queue *q, char __user *data, size_t count,
  342. loff_t *ppos, int nonblock);
  343. /**
  344. * vb2_is_streaming() - return streaming status of the queue
  345. * @q: videobuf queue
  346. */
  347. static inline bool vb2_is_streaming(struct vb2_queue *q)
  348. {
  349. return q->streaming;
  350. }
  351. /**
  352. * vb2_is_busy() - return busy status of the queue
  353. * @q: videobuf queue
  354. *
  355. * This function checks if queue has any buffers allocated.
  356. */
  357. static inline bool vb2_is_busy(struct vb2_queue *q)
  358. {
  359. return (q->num_buffers > 0);
  360. }
  361. /**
  362. * vb2_get_drv_priv() - return driver private data associated with the queue
  363. * @q: videobuf queue
  364. */
  365. static inline void *vb2_get_drv_priv(struct vb2_queue *q)
  366. {
  367. return q->drv_priv;
  368. }
  369. /**
  370. * vb2_set_plane_payload() - set bytesused for the plane plane_no
  371. * @vb: buffer for which plane payload should be set
  372. * @plane_no: plane number for which payload should be set
  373. * @size: payload in bytes
  374. */
  375. static inline void vb2_set_plane_payload(struct vb2_buffer *vb,
  376. unsigned int plane_no, unsigned long size)
  377. {
  378. if (plane_no < vb->num_planes)
  379. vb->v4l2_planes[plane_no].bytesused = size;
  380. }
  381. /**
  382. * vb2_get_plane_payload() - get bytesused for the plane plane_no
  383. * @vb: buffer for which plane payload should be set
  384. * @plane_no: plane number for which payload should be set
  385. * @size: payload in bytes
  386. */
  387. static inline unsigned long vb2_get_plane_payload(struct vb2_buffer *vb,
  388. unsigned int plane_no)
  389. {
  390. if (plane_no < vb->num_planes)
  391. return vb->v4l2_planes[plane_no].bytesused;
  392. return 0;
  393. }
  394. /**
  395. * vb2_plane_size() - return plane size in bytes
  396. * @vb: buffer for which plane size should be returned
  397. * @plane_no: plane number for which size should be returned
  398. */
  399. static inline unsigned long
  400. vb2_plane_size(struct vb2_buffer *vb, unsigned int plane_no)
  401. {
  402. if (plane_no < vb->num_planes)
  403. return vb->v4l2_planes[plane_no].length;
  404. return 0;
  405. }
  406. /*
  407. * The following functions are not part of the vb2 core API, but are simple
  408. * helper functions that you can use in your struct v4l2_file_operations,
  409. * struct v4l2_ioctl_ops and struct vb2_ops. They will serialize if vb2_queue->lock
  410. * or video_device->lock is set, and they will set and test vb2_queue->owner
  411. * to check if the calling filehandle is permitted to do the queuing operation.
  412. */
  413. /* struct v4l2_ioctl_ops helpers */
  414. int vb2_ioctl_reqbufs(struct file *file, void *priv,
  415. struct v4l2_requestbuffers *p);
  416. int vb2_ioctl_create_bufs(struct file *file, void *priv,
  417. struct v4l2_create_buffers *p);
  418. int vb2_ioctl_prepare_buf(struct file *file, void *priv,
  419. struct v4l2_buffer *p);
  420. int vb2_ioctl_querybuf(struct file *file, void *priv, struct v4l2_buffer *p);
  421. int vb2_ioctl_qbuf(struct file *file, void *priv, struct v4l2_buffer *p);
  422. int vb2_ioctl_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p);
  423. int vb2_ioctl_streamon(struct file *file, void *priv, enum v4l2_buf_type i);
  424. int vb2_ioctl_streamoff(struct file *file, void *priv, enum v4l2_buf_type i);
  425. int vb2_ioctl_expbuf(struct file *file, void *priv,
  426. struct v4l2_exportbuffer *p);
  427. /* struct v4l2_file_operations helpers */
  428. int vb2_fop_mmap(struct file *file, struct vm_area_struct *vma);
  429. int vb2_fop_release(struct file *file);
  430. ssize_t vb2_fop_write(struct file *file, char __user *buf,
  431. size_t count, loff_t *ppos);
  432. ssize_t vb2_fop_read(struct file *file, char __user *buf,
  433. size_t count, loff_t *ppos);
  434. unsigned int vb2_fop_poll(struct file *file, poll_table *wait);
  435. #ifndef CONFIG_MMU
  436. unsigned long vb2_fop_get_unmapped_area(struct file *file, unsigned long addr,
  437. unsigned long len, unsigned long pgoff, unsigned long flags);
  438. #endif
  439. /* struct vb2_ops helpers, only use if vq->lock is non-NULL. */
  440. void vb2_ops_wait_prepare(struct vb2_queue *vq);
  441. void vb2_ops_wait_finish(struct vb2_queue *vq);
  442. #endif /* _MEDIA_VIDEOBUF2_CORE_H */