Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * videobuf2-v4l2.h - V4L2 driver helper framework
0003  *
0004  * Copyright (C) 2010 Samsung Electronics
0005  *
0006  * Author: Pawel Osciak <pawel@osciak.com>
0007  *
0008  * This program is free software; you can redistribute it and/or modify
0009  * it under the terms of the GNU General Public License as published by
0010  * the Free Software Foundation.
0011  */
0012 #ifndef _MEDIA_VIDEOBUF2_V4L2_H
0013 #define _MEDIA_VIDEOBUF2_V4L2_H
0014 
0015 #include <linux/videodev2.h>
0016 #include <media/videobuf2-core.h>
0017 
0018 #if VB2_MAX_FRAME != VIDEO_MAX_FRAME
0019 #error VB2_MAX_FRAME != VIDEO_MAX_FRAME
0020 #endif
0021 
0022 #if VB2_MAX_PLANES != VIDEO_MAX_PLANES
0023 #error VB2_MAX_PLANES != VIDEO_MAX_PLANES
0024 #endif
0025 
0026 struct video_device;
0027 
0028 /**
0029  * struct vb2_v4l2_buffer - video buffer information for v4l2.
0030  *
0031  * @vb2_buf:    embedded struct &vb2_buffer.
0032  * @flags:  buffer informational flags.
0033  * @field:  field order of the image in the buffer, as defined by
0034  *      &enum v4l2_field.
0035  * @timecode:   frame timecode.
0036  * @sequence:   sequence count of this frame.
0037  * @request_fd: the request_fd associated with this buffer
0038  * @is_held:    if true, then this capture buffer was held
0039  * @planes: plane information (userptr/fd, length, bytesused, data_offset).
0040  *
0041  * Should contain enough information to be able to cover all the fields
0042  * of &struct v4l2_buffer at ``videodev2.h``.
0043  */
0044 struct vb2_v4l2_buffer {
0045     struct vb2_buffer   vb2_buf;
0046 
0047     __u32           flags;
0048     __u32           field;
0049     struct v4l2_timecode    timecode;
0050     __u32           sequence;
0051     __s32           request_fd;
0052     bool            is_held;
0053     struct vb2_plane    planes[VB2_MAX_PLANES];
0054 };
0055 
0056 /* VB2 V4L2 flags as set in vb2_queue.subsystem_flags */
0057 #define VB2_V4L2_FL_SUPPORTS_M2M_HOLD_CAPTURE_BUF (1 << 0)
0058 
0059 /*
0060  * to_vb2_v4l2_buffer() - cast struct vb2_buffer * to struct vb2_v4l2_buffer *
0061  */
0062 #define to_vb2_v4l2_buffer(vb) \
0063     container_of(vb, struct vb2_v4l2_buffer, vb2_buf)
0064 
0065 /**
0066  * vb2_find_timestamp() - Find buffer with given timestamp in the queue
0067  *
0068  * @q:      pointer to &struct vb2_queue with videobuf2 queue.
0069  * @timestamp:  the timestamp to find.
0070  * @start_idx:  the start index (usually 0) in the buffer array to start
0071  *      searching from. Note that there may be multiple buffers
0072  *      with the same timestamp value, so you can restart the search
0073  *      by setting @start_idx to the previously found index + 1.
0074  *
0075  * Returns the buffer index of the buffer with the given @timestamp, or
0076  * -1 if no buffer with @timestamp was found.
0077  */
0078 int vb2_find_timestamp(const struct vb2_queue *q, u64 timestamp,
0079                unsigned int start_idx);
0080 
0081 /**
0082  * vb2_find_buffer() - Find a buffer with given timestamp
0083  *
0084  * @q:      pointer to &struct vb2_queue with videobuf2 queue.
0085  * @timestamp:  the timestamp to find.
0086  *
0087  * Returns the buffer with the given @timestamp, or NULL if not found.
0088  */
0089 struct vb2_buffer *vb2_find_buffer(struct vb2_queue *q, u64 timestamp);
0090 
0091 int vb2_querybuf(struct vb2_queue *q, struct v4l2_buffer *b);
0092 
0093 /**
0094  * vb2_reqbufs() - Wrapper for vb2_core_reqbufs() that also verifies
0095  * the memory and type values.
0096  *
0097  * @q:      pointer to &struct vb2_queue with videobuf2 queue.
0098  * @req:    &struct v4l2_requestbuffers passed from userspace to
0099  *      &v4l2_ioctl_ops->vidioc_reqbufs handler in driver.
0100  */
0101 int vb2_reqbufs(struct vb2_queue *q, struct v4l2_requestbuffers *req);
0102 
0103 /**
0104  * vb2_create_bufs() - Wrapper for vb2_core_create_bufs() that also verifies
0105  * the memory and type values.
0106  *
0107  * @q:      pointer to &struct vb2_queue with videobuf2 queue.
0108  * @create: creation parameters, passed from userspace to
0109  *      &v4l2_ioctl_ops->vidioc_create_bufs handler in driver
0110  */
0111 int vb2_create_bufs(struct vb2_queue *q, struct v4l2_create_buffers *create);
0112 
0113 /**
0114  * vb2_prepare_buf() - Pass ownership of a buffer from userspace to the kernel
0115  *
0116  * @q:      pointer to &struct vb2_queue with videobuf2 queue.
0117  * @mdev:   pointer to &struct media_device, may be NULL.
0118  * @b:      buffer structure passed from userspace to
0119  *      &v4l2_ioctl_ops->vidioc_prepare_buf handler in driver
0120  *
0121  * Should be called from &v4l2_ioctl_ops->vidioc_prepare_buf ioctl handler
0122  * of a driver.
0123  *
0124  * This function:
0125  *
0126  * #) verifies the passed buffer,
0127  * #) calls &vb2_ops->buf_prepare callback in the driver (if provided),
0128  *    in which driver-specific buffer initialization can be performed.
0129  * #) if @b->request_fd is non-zero and @mdev->ops->req_queue is set,
0130  *    then bind the prepared buffer to the request.
0131  *
0132  * The return values from this function are intended to be directly returned
0133  * from &v4l2_ioctl_ops->vidioc_prepare_buf handler in driver.
0134  */
0135 int vb2_prepare_buf(struct vb2_queue *q, struct media_device *mdev,
0136             struct v4l2_buffer *b);
0137 
0138 /**
0139  * vb2_qbuf() - Queue a buffer from userspace
0140  * @q:      pointer to &struct vb2_queue with videobuf2 queue.
0141  * @mdev:   pointer to &struct media_device, may be NULL.
0142  * @b:      buffer structure passed from userspace to
0143  *      &v4l2_ioctl_ops->vidioc_qbuf handler in driver
0144  *
0145  * Should be called from &v4l2_ioctl_ops->vidioc_qbuf handler of a driver.
0146  *
0147  * This function:
0148  *
0149  * #) verifies the passed buffer;
0150  * #) if @b->request_fd is non-zero and @mdev->ops->req_queue is set,
0151  *    then bind the buffer to the request.
0152  * #) if necessary, calls &vb2_ops->buf_prepare callback in the driver
0153  *    (if provided), in which driver-specific buffer initialization can
0154  *    be performed;
0155  * #) if streaming is on, queues the buffer in driver by the means of
0156  *    &vb2_ops->buf_queue callback for processing.
0157  *
0158  * The return values from this function are intended to be directly returned
0159  * from &v4l2_ioctl_ops->vidioc_qbuf handler in driver.
0160  */
0161 int vb2_qbuf(struct vb2_queue *q, struct media_device *mdev,
0162          struct v4l2_buffer *b);
0163 
0164 /**
0165  * vb2_expbuf() - Export a buffer as a file descriptor
0166  * @q:      pointer to &struct vb2_queue with videobuf2 queue.
0167  * @eb:     export buffer structure passed from userspace to
0168  *      &v4l2_ioctl_ops->vidioc_expbuf handler in driver
0169  *
0170  * The return values from this function are intended to be directly returned
0171  * from &v4l2_ioctl_ops->vidioc_expbuf handler in driver.
0172  */
0173 int vb2_expbuf(struct vb2_queue *q, struct v4l2_exportbuffer *eb);
0174 
0175 /**
0176  * vb2_dqbuf() - Dequeue a buffer to the userspace
0177  * @q:      pointer to &struct vb2_queue with videobuf2 queue.
0178  * @b:      buffer structure passed from userspace to
0179  *      &v4l2_ioctl_ops->vidioc_dqbuf handler in driver
0180  * @nonblocking: if true, this call will not sleep waiting for a buffer if no
0181  *       buffers ready for dequeuing are present. Normally the driver
0182  *       would be passing (&file->f_flags & %O_NONBLOCK) here
0183  *
0184  * Should be called from &v4l2_ioctl_ops->vidioc_dqbuf ioctl handler
0185  * of a driver.
0186  *
0187  * This function:
0188  *
0189  * #) verifies the passed buffer;
0190  * #) calls &vb2_ops->buf_finish callback in the driver (if provided), in which
0191  *    driver can perform any additional operations that may be required before
0192  *    returning the buffer to userspace, such as cache sync;
0193  * #) the buffer struct members are filled with relevant information for
0194  *    the userspace.
0195  *
0196  * The return values from this function are intended to be directly returned
0197  * from &v4l2_ioctl_ops->vidioc_dqbuf handler in driver.
0198  */
0199 int vb2_dqbuf(struct vb2_queue *q, struct v4l2_buffer *b, bool nonblocking);
0200 
0201 /**
0202  * vb2_streamon - start streaming
0203  * @q:      pointer to &struct vb2_queue with videobuf2 queue.
0204  * @type:   type argument passed from userspace to vidioc_streamon handler,
0205  *      as defined by &enum v4l2_buf_type.
0206  *
0207  * Should be called from &v4l2_ioctl_ops->vidioc_streamon handler of a driver.
0208  *
0209  * This function:
0210  *
0211  * 1) verifies current state
0212  * 2) passes any previously queued buffers to the driver and starts streaming
0213  *
0214  * The return values from this function are intended to be directly returned
0215  * from &v4l2_ioctl_ops->vidioc_streamon handler in the driver.
0216  */
0217 int vb2_streamon(struct vb2_queue *q, enum v4l2_buf_type type);
0218 
0219 /**
0220  * vb2_streamoff - stop streaming
0221  * @q:      pointer to &struct vb2_queue with videobuf2 queue.
0222  * @type:   type argument passed from userspace to vidioc_streamoff handler
0223  *
0224  * Should be called from vidioc_streamoff handler of a driver.
0225  *
0226  * This function:
0227  *
0228  * #) verifies current state,
0229  * #) stop streaming and dequeues any queued buffers, including those previously
0230  *    passed to the driver (after waiting for the driver to finish).
0231  *
0232  * This call can be used for pausing playback.
0233  * The return values from this function are intended to be directly returned
0234  * from vidioc_streamoff handler in the driver
0235  */
0236 int vb2_streamoff(struct vb2_queue *q, enum v4l2_buf_type type);
0237 
0238 /**
0239  * vb2_queue_init() - initialize a videobuf2 queue
0240  * @q:      pointer to &struct vb2_queue with videobuf2 queue.
0241  *
0242  * The vb2_queue structure should be allocated by the driver. The driver is
0243  * responsible of clearing it's content and setting initial values for some
0244  * required entries before calling this function.
0245  * q->ops, q->mem_ops, q->type and q->io_modes are mandatory. Please refer
0246  * to the struct vb2_queue description in include/media/videobuf2-core.h
0247  * for more information.
0248  */
0249 int __must_check vb2_queue_init(struct vb2_queue *q);
0250 
0251 /**
0252  * vb2_queue_init_name() - initialize a videobuf2 queue with a name
0253  * @q:      pointer to &struct vb2_queue with videobuf2 queue.
0254  * @name:   the queue name
0255  *
0256  * This function initializes the vb2_queue exactly like vb2_queue_init(),
0257  * and additionally sets the queue name. The queue name is used for logging
0258  * purpose, and should uniquely identify the queue within the context of the
0259  * device it belongs to. This is useful to attribute kernel log messages to the
0260  * right queue for m2m devices or other devices that handle multiple queues.
0261  */
0262 int __must_check vb2_queue_init_name(struct vb2_queue *q, const char *name);
0263 
0264 /**
0265  * vb2_queue_release() - stop streaming, release the queue and free memory
0266  * @q:      pointer to &struct vb2_queue with videobuf2 queue.
0267  *
0268  * This function stops streaming and performs necessary clean ups, including
0269  * freeing video buffer memory. The driver is responsible for freeing
0270  * the vb2_queue structure itself.
0271  */
0272 void vb2_queue_release(struct vb2_queue *q);
0273 
0274 /**
0275  * vb2_queue_change_type() - change the type of an inactive vb2_queue
0276  * @q:      pointer to &struct vb2_queue with videobuf2 queue.
0277  * @type:   the type to change to (V4L2_BUF_TYPE_VIDEO_*)
0278  *
0279  * This function changes the type of the vb2_queue. This is only possible
0280  * if the queue is not busy (i.e. no buffers have been allocated).
0281  *
0282  * vb2_queue_change_type() can be used to support multiple buffer types using
0283  * the same queue. The driver can implement v4l2_ioctl_ops.vidioc_reqbufs and
0284  * v4l2_ioctl_ops.vidioc_create_bufs functions and call vb2_queue_change_type()
0285  * before calling vb2_ioctl_reqbufs() or vb2_ioctl_create_bufs(), and thus
0286  * "lock" the buffer type until the buffers have been released.
0287  */
0288 int vb2_queue_change_type(struct vb2_queue *q, unsigned int type);
0289 
0290 /**
0291  * vb2_poll() - implements poll userspace operation
0292  * @q:      pointer to &struct vb2_queue with videobuf2 queue.
0293  * @file:   file argument passed to the poll file operation handler
0294  * @wait:   wait argument passed to the poll file operation handler
0295  *
0296  * This function implements poll file operation handler for a driver.
0297  * For CAPTURE queues, if a buffer is ready to be dequeued, the userspace will
0298  * be informed that the file descriptor of a video device is available for
0299  * reading.
0300  * For OUTPUT queues, if a buffer is ready to be dequeued, the file descriptor
0301  * will be reported as available for writing.
0302  *
0303  * If the driver uses struct v4l2_fh, then vb2_poll() will also check for any
0304  * pending events.
0305  *
0306  * The return values from this function are intended to be directly returned
0307  * from poll handler in driver.
0308  */
0309 __poll_t vb2_poll(struct vb2_queue *q, struct file *file, poll_table *wait);
0310 
0311 /*
0312  * The following functions are not part of the vb2 core API, but are simple
0313  * helper functions that you can use in your struct v4l2_file_operations,
0314  * struct v4l2_ioctl_ops and struct vb2_ops. They will serialize if vb2_queue->lock
0315  * or video_device->lock is set, and they will set and test the queue owner
0316  * (vb2_queue->owner) to check if the calling filehandle is permitted to do the
0317  * queuing operation.
0318  */
0319 
0320 /**
0321  * vb2_queue_is_busy() - check if the queue is busy
0322  * @q:      pointer to &struct vb2_queue with videobuf2 queue.
0323  * @file:   file through which the vb2 queue access is performed
0324  *
0325  * The queue is considered busy if it has an owner and the owner is not the
0326  * @file.
0327  *
0328  * Queue ownership is acquired and checked by some of the v4l2_ioctl_ops helpers
0329  * below. Drivers can also use this function directly when they need to
0330  * open-code ioctl handlers, for instance to add additional checks between the
0331  * queue ownership test and the call to the corresponding vb2 operation.
0332  */
0333 static inline bool vb2_queue_is_busy(struct vb2_queue *q, struct file *file)
0334 {
0335     return q->owner && q->owner != file->private_data;
0336 }
0337 
0338 /* struct v4l2_ioctl_ops helpers */
0339 
0340 int vb2_ioctl_reqbufs(struct file *file, void *priv,
0341               struct v4l2_requestbuffers *p);
0342 int vb2_ioctl_create_bufs(struct file *file, void *priv,
0343               struct v4l2_create_buffers *p);
0344 int vb2_ioctl_prepare_buf(struct file *file, void *priv,
0345               struct v4l2_buffer *p);
0346 int vb2_ioctl_querybuf(struct file *file, void *priv, struct v4l2_buffer *p);
0347 int vb2_ioctl_qbuf(struct file *file, void *priv, struct v4l2_buffer *p);
0348 int vb2_ioctl_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p);
0349 int vb2_ioctl_streamon(struct file *file, void *priv, enum v4l2_buf_type i);
0350 int vb2_ioctl_streamoff(struct file *file, void *priv, enum v4l2_buf_type i);
0351 int vb2_ioctl_expbuf(struct file *file, void *priv,
0352     struct v4l2_exportbuffer *p);
0353 
0354 /* struct v4l2_file_operations helpers */
0355 
0356 int vb2_fop_mmap(struct file *file, struct vm_area_struct *vma);
0357 int vb2_fop_release(struct file *file);
0358 int _vb2_fop_release(struct file *file, struct mutex *lock);
0359 ssize_t vb2_fop_write(struct file *file, const char __user *buf,
0360         size_t count, loff_t *ppos);
0361 ssize_t vb2_fop_read(struct file *file, char __user *buf,
0362         size_t count, loff_t *ppos);
0363 __poll_t vb2_fop_poll(struct file *file, poll_table *wait);
0364 #ifndef CONFIG_MMU
0365 unsigned long vb2_fop_get_unmapped_area(struct file *file, unsigned long addr,
0366         unsigned long len, unsigned long pgoff, unsigned long flags);
0367 #endif
0368 
0369 /**
0370  * vb2_video_unregister_device - unregister the video device and release queue
0371  *
0372  * @vdev: pointer to &struct video_device
0373  *
0374  * If the driver uses vb2_fop_release()/_vb2_fop_release(), then it should use
0375  * vb2_video_unregister_device() instead of video_unregister_device().
0376  *
0377  * This function will call video_unregister_device() and then release the
0378  * vb2_queue if streaming is in progress. This will stop streaming and
0379  * this will simplify the unbind sequence since after this call all subdevs
0380  * will have stopped streaming as well.
0381  */
0382 void vb2_video_unregister_device(struct video_device *vdev);
0383 
0384 /**
0385  * vb2_ops_wait_prepare - helper function to lock a struct &vb2_queue
0386  *
0387  * @vq: pointer to &struct vb2_queue
0388  *
0389  * ..note:: only use if vq->lock is non-NULL.
0390  */
0391 void vb2_ops_wait_prepare(struct vb2_queue *vq);
0392 
0393 /**
0394  * vb2_ops_wait_finish - helper function to unlock a struct &vb2_queue
0395  *
0396  * @vq: pointer to &struct vb2_queue
0397  *
0398  * ..note:: only use if vq->lock is non-NULL.
0399  */
0400 void vb2_ops_wait_finish(struct vb2_queue *vq);
0401 
0402 struct media_request;
0403 int vb2_request_validate(struct media_request *req);
0404 void vb2_request_queue(struct media_request *req);
0405 
0406 #endif /* _MEDIA_VIDEOBUF2_V4L2_H */