videobuf2-core.h 19 KB

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