1 /* 2 * videobuf2-v4l2.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_V4L2_H 13 #define _MEDIA_VIDEOBUF2_V4L2_H 14 15 #include <linux/videodev2.h> 16 #include <media/videobuf2-core.h> 17 18 #if VB2_MAX_FRAME != VIDEO_MAX_FRAME 19 #error VB2_MAX_FRAME != VIDEO_MAX_FRAME 20 #endif 21 22 #if VB2_MAX_PLANES != VIDEO_MAX_PLANES 23 #error VB2_MAX_PLANES != VIDEO_MAX_PLANES 24 #endif 25 26 struct video_device; 27 28 /** 29 * struct vb2_v4l2_buffer - video buffer information for v4l2. 30 * 31 * @vb2_buf: embedded struct &vb2_buffer. 32 * @flags: buffer informational flags. 33 * @field: field order of the image in the buffer, as defined by 34 * &enum v4l2_field. 35 * @timecode: frame timecode. 36 * @sequence: sequence count of this frame. 37 * @request_fd: the request_fd associated with this buffer 38 * @is_held: if true, then this capture buffer was held 39 * @planes: plane information (userptr/fd, length, bytesused, data_offset). 40 * 41 * Should contain enough information to be able to cover all the fields 42 * of &struct v4l2_buffer at ``videodev2.h``. 43 */ 44 struct vb2_v4l2_buffer { 45 struct vb2_buffer vb2_buf; 46 47 __u32 flags; 48 __u32 field; 49 struct v4l2_timecode timecode; 50 __u32 sequence; 51 __s32 request_fd; 52 bool is_held; 53 struct vb2_plane planes[VB2_MAX_PLANES]; 54 }; 55 56 /* VB2 V4L2 flags as set in vb2_queue.subsystem_flags */ 57 #define VB2_V4L2_FL_SUPPORTS_M2M_HOLD_CAPTURE_BUF (1 << 0) 58 59 /* 60 * to_vb2_v4l2_buffer() - cast struct vb2_buffer * to struct vb2_v4l2_buffer * 61 */ 62 #define to_vb2_v4l2_buffer(vb) \ 63 container_of(vb, struct vb2_v4l2_buffer, vb2_buf) 64 65 /** 66 * vb2_find_timestamp() - Find buffer with given timestamp in the queue 67 * 68 * @q: pointer to &struct vb2_queue with videobuf2 queue. 69 * @timestamp: the timestamp to find. 70 * @start_idx: the start index (usually 0) in the buffer array to start 71 * searching from. Note that there may be multiple buffers 72 * with the same timestamp value, so you can restart the search 73 * by setting @start_idx to the previously found index + 1. 74 * 75 * Returns the buffer index of the buffer with the given @timestamp, or 76 * -1 if no buffer with @timestamp was found. 77 */ 78 int vb2_find_timestamp(const struct vb2_queue *q, u64 timestamp, 79 unsigned int start_idx); 80 81 /** 82 * vb2_find_buffer() - Find a buffer with given timestamp 83 * 84 * @q: pointer to &struct vb2_queue with videobuf2 queue. 85 * @timestamp: the timestamp to find. 86 * 87 * Returns the buffer with the given @timestamp, or NULL if not found. 88 */ 89 struct vb2_buffer *vb2_find_buffer(struct vb2_queue *q, u64 timestamp); 90 91 int vb2_querybuf(struct vb2_queue *q, struct v4l2_buffer *b); 92 93 /** 94 * vb2_reqbufs() - Wrapper for vb2_core_reqbufs() that also verifies 95 * the memory and type values. 96 * 97 * @q: pointer to &struct vb2_queue with videobuf2 queue. 98 * @req: &struct v4l2_requestbuffers passed from userspace to 99 * &v4l2_ioctl_ops->vidioc_reqbufs handler in driver. 100 */ 101 int vb2_reqbufs(struct vb2_queue *q, struct v4l2_requestbuffers *req); 102 103 /** 104 * vb2_create_bufs() - Wrapper for vb2_core_create_bufs() that also verifies 105 * the memory and type values. 106 * 107 * @q: pointer to &struct vb2_queue with videobuf2 queue. 108 * @create: creation parameters, passed from userspace to 109 * &v4l2_ioctl_ops->vidioc_create_bufs handler in driver 110 */ 111 int vb2_create_bufs(struct vb2_queue *q, struct v4l2_create_buffers *create); 112 113 /** 114 * vb2_prepare_buf() - Pass ownership of a buffer from userspace to the kernel 115 * 116 * @q: pointer to &struct vb2_queue with videobuf2 queue. 117 * @mdev: pointer to &struct media_device, may be NULL. 118 * @b: buffer structure passed from userspace to 119 * &v4l2_ioctl_ops->vidioc_prepare_buf handler in driver 120 * 121 * Should be called from &v4l2_ioctl_ops->vidioc_prepare_buf ioctl handler 122 * of a driver. 123 * 124 * This function: 125 * 126 * #) verifies the passed buffer, 127 * #) calls &vb2_ops->buf_prepare callback in the driver (if provided), 128 * in which driver-specific buffer initialization can be performed. 129 * #) if @b->request_fd is non-zero and @mdev->ops->req_queue is set, 130 * then bind the prepared buffer to the request. 131 * 132 * The return values from this function are intended to be directly returned 133 * from &v4l2_ioctl_ops->vidioc_prepare_buf handler in driver. 134 */ 135 int vb2_prepare_buf(struct vb2_queue *q, struct media_device *mdev, 136 struct v4l2_buffer *b); 137 138 /** 139 * vb2_qbuf() - Queue a buffer from userspace 140 * @q: pointer to &struct vb2_queue with videobuf2 queue. 141 * @mdev: pointer to &struct media_device, may be NULL. 142 * @b: buffer structure passed from userspace to 143 * &v4l2_ioctl_ops->vidioc_qbuf handler in driver 144 * 145 * Should be called from &v4l2_ioctl_ops->vidioc_qbuf handler of a driver. 146 * 147 * This function: 148 * 149 * #) verifies the passed buffer; 150 * #) if @b->request_fd is non-zero and @mdev->ops->req_queue is set, 151 * then bind the buffer to the request. 152 * #) if necessary, calls &vb2_ops->buf_prepare callback in the driver 153 * (if provided), in which driver-specific buffer initialization can 154 * be performed; 155 * #) if streaming is on, queues the buffer in driver by the means of 156 * &vb2_ops->buf_queue callback for processing. 157 * 158 * The return values from this function are intended to be directly returned 159 * from &v4l2_ioctl_ops->vidioc_qbuf handler in driver. 160 */ 161 int vb2_qbuf(struct vb2_queue *q, struct media_device *mdev, 162 struct v4l2_buffer *b); 163 164 /** 165 * vb2_expbuf() - Export a buffer as a file descriptor 166 * @q: pointer to &struct vb2_queue with videobuf2 queue. 167 * @eb: export buffer structure passed from userspace to 168 * &v4l2_ioctl_ops->vidioc_expbuf handler in driver 169 * 170 * The return values from this function are intended to be directly returned 171 * from &v4l2_ioctl_ops->vidioc_expbuf handler in driver. 172 */ 173 int vb2_expbuf(struct vb2_queue *q, struct v4l2_exportbuffer *eb); 174 175 /** 176 * vb2_dqbuf() - Dequeue a buffer to the userspace 177 * @q: pointer to &struct vb2_queue with videobuf2 queue. 178 * @b: buffer structure passed from userspace to 179 * &v4l2_ioctl_ops->vidioc_dqbuf handler in driver 180 * @nonblocking: if true, this call will not sleep waiting for a buffer if no 181 * buffers ready for dequeuing are present. Normally the driver 182 * would be passing (&file->f_flags & %O_NONBLOCK) here 183 * 184 * Should be called from &v4l2_ioctl_ops->vidioc_dqbuf ioctl handler 185 * of a driver. 186 * 187 * This function: 188 * 189 * #) verifies the passed buffer; 190 * #) calls &vb2_ops->buf_finish callback in the driver (if provided), in which 191 * driver can perform any additional operations that may be required before 192 * returning the buffer to userspace, such as cache sync; 193 * #) the buffer struct members are filled with relevant information for 194 * the userspace. 195 * 196 * The return values from this function are intended to be directly returned 197 * from &v4l2_ioctl_ops->vidioc_dqbuf handler in driver. 198 */ 199 int vb2_dqbuf(struct vb2_queue *q, struct v4l2_buffer *b, bool nonblocking); 200 201 /** 202 * vb2_streamon - start streaming 203 * @q: pointer to &struct vb2_queue with videobuf2 queue. 204 * @type: type argument passed from userspace to vidioc_streamon handler, 205 * as defined by &enum v4l2_buf_type. 206 * 207 * Should be called from &v4l2_ioctl_ops->vidioc_streamon handler of a driver. 208 * 209 * This function: 210 * 211 * 1) verifies current state 212 * 2) passes any previously queued buffers to the driver and starts streaming 213 * 214 * The return values from this function are intended to be directly returned 215 * from &v4l2_ioctl_ops->vidioc_streamon handler in the driver. 216 */ 217 int vb2_streamon(struct vb2_queue *q, enum v4l2_buf_type type); 218 219 /** 220 * vb2_streamoff - stop streaming 221 * @q: pointer to &struct vb2_queue with videobuf2 queue. 222 * @type: type argument passed from userspace to vidioc_streamoff handler 223 * 224 * Should be called from vidioc_streamoff handler of a driver. 225 * 226 * This function: 227 * 228 * #) verifies current state, 229 * #) stop streaming and dequeues any queued buffers, including those previously 230 * passed to the driver (after waiting for the driver to finish). 231 * 232 * This call can be used for pausing playback. 233 * The return values from this function are intended to be directly returned 234 * from vidioc_streamoff handler in the driver 235 */ 236 int vb2_streamoff(struct vb2_queue *q, enum v4l2_buf_type type); 237 238 /** 239 * vb2_queue_init() - initialize a videobuf2 queue 240 * @q: pointer to &struct vb2_queue with videobuf2 queue. 241 * 242 * The vb2_queue structure should be allocated by the driver. The driver is 243 * responsible of clearing it's content and setting initial values for some 244 * required entries before calling this function. 245 * q->ops, q->mem_ops, q->type and q->io_modes are mandatory. Please refer 246 * to the struct vb2_queue description in include/media/videobuf2-core.h 247 * for more information. 248 */ 249 int __must_check vb2_queue_init(struct vb2_queue *q); 250 251 /** 252 * vb2_queue_init_name() - initialize a videobuf2 queue with a name 253 * @q: pointer to &struct vb2_queue with videobuf2 queue. 254 * @name: the queue name 255 * 256 * This function initializes the vb2_queue exactly like vb2_queue_init(), 257 * and additionally sets the queue name. The queue name is used for logging 258 * purpose, and should uniquely identify the queue within the context of the 259 * device it belongs to. This is useful to attribute kernel log messages to the 260 * right queue for m2m devices or other devices that handle multiple queues. 261 */ 262 int __must_check vb2_queue_init_name(struct vb2_queue *q, const char *name); 263 264 /** 265 * vb2_queue_release() - stop streaming, release the queue and free memory 266 * @q: pointer to &struct vb2_queue with videobuf2 queue. 267 * 268 * This function stops streaming and performs necessary clean ups, including 269 * freeing video buffer memory. The driver is responsible for freeing 270 * the vb2_queue structure itself. 271 */ 272 void vb2_queue_release(struct vb2_queue *q); 273 274 /** 275 * vb2_queue_change_type() - change the type of an inactive vb2_queue 276 * @q: pointer to &struct vb2_queue with videobuf2 queue. 277 * @type: the type to change to (V4L2_BUF_TYPE_VIDEO_*) 278 * 279 * This function changes the type of the vb2_queue. This is only possible 280 * if the queue is not busy (i.e. no buffers have been allocated). 281 * 282 * vb2_queue_change_type() can be used to support multiple buffer types using 283 * the same queue. The driver can implement v4l2_ioctl_ops.vidioc_reqbufs and 284 * v4l2_ioctl_ops.vidioc_create_bufs functions and call vb2_queue_change_type() 285 * before calling vb2_ioctl_reqbufs() or vb2_ioctl_create_bufs(), and thus 286 * "lock" the buffer type until the buffers have been released. 287 */ 288 int vb2_queue_change_type(struct vb2_queue *q, unsigned int type); 289 290 /** 291 * vb2_poll() - implements poll userspace operation 292 * @q: pointer to &struct vb2_queue with videobuf2 queue. 293 * @file: file argument passed to the poll file operation handler 294 * @wait: wait argument passed to the poll file operation handler 295 * 296 * This function implements poll file operation handler for a driver. 297 * For CAPTURE queues, if a buffer is ready to be dequeued, the userspace will 298 * be informed that the file descriptor of a video device is available for 299 * reading. 300 * For OUTPUT queues, if a buffer is ready to be dequeued, the file descriptor 301 * will be reported as available for writing. 302 * 303 * If the driver uses struct v4l2_fh, then vb2_poll() will also check for any 304 * pending events. 305 * 306 * The return values from this function are intended to be directly returned 307 * from poll handler in driver. 308 */ 309 __poll_t vb2_poll(struct vb2_queue *q, struct file *file, poll_table *wait); 310 311 /* 312 * The following functions are not part of the vb2 core API, but are simple 313 * helper functions that you can use in your struct v4l2_file_operations, 314 * struct v4l2_ioctl_ops and struct vb2_ops. They will serialize if vb2_queue->lock 315 * or video_device->lock is set, and they will set and test the queue owner 316 * (vb2_queue->owner) to check if the calling filehandle is permitted to do the 317 * queuing operation. 318 */ 319 320 /** 321 * vb2_queue_is_busy() - check if the queue is busy 322 * @q: pointer to &struct vb2_queue with videobuf2 queue. 323 * @file: file through which the vb2 queue access is performed 324 * 325 * The queue is considered busy if it has an owner and the owner is not the 326 * @file. 327 * 328 * Queue ownership is acquired and checked by some of the v4l2_ioctl_ops helpers 329 * below. Drivers can also use this function directly when they need to 330 * open-code ioctl handlers, for instance to add additional checks between the 331 * queue ownership test and the call to the corresponding vb2 operation. 332 */ 333 static inline bool vb2_queue_is_busy(struct vb2_queue *q, struct file *file) 334 { 335 return q->owner && q->owner != file->private_data; 336 } 337 338 /* struct v4l2_ioctl_ops helpers */ 339 340 int vb2_ioctl_reqbufs(struct file *file, void *priv, 341 struct v4l2_requestbuffers *p); 342 int vb2_ioctl_create_bufs(struct file *file, void *priv, 343 struct v4l2_create_buffers *p); 344 int vb2_ioctl_prepare_buf(struct file *file, void *priv, 345 struct v4l2_buffer *p); 346 int vb2_ioctl_querybuf(struct file *file, void *priv, struct v4l2_buffer *p); 347 int vb2_ioctl_qbuf(struct file *file, void *priv, struct v4l2_buffer *p); 348 int vb2_ioctl_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p); 349 int vb2_ioctl_streamon(struct file *file, void *priv, enum v4l2_buf_type i); 350 int vb2_ioctl_streamoff(struct file *file, void *priv, enum v4l2_buf_type i); 351 int vb2_ioctl_expbuf(struct file *file, void *priv, 352 struct v4l2_exportbuffer *p); 353 354 /* struct v4l2_file_operations helpers */ 355 356 int vb2_fop_mmap(struct file *file, struct vm_area_struct *vma); 357 int vb2_fop_release(struct file *file); 358 int _vb2_fop_release(struct file *file, struct mutex *lock); 359 ssize_t vb2_fop_write(struct file *file, const char __user *buf, 360 size_t count, loff_t *ppos); 361 ssize_t vb2_fop_read(struct file *file, char __user *buf, 362 size_t count, loff_t *ppos); 363 __poll_t vb2_fop_poll(struct file *file, poll_table *wait); 364 #ifndef CONFIG_MMU 365 unsigned long vb2_fop_get_unmapped_area(struct file *file, unsigned long addr, 366 unsigned long len, unsigned long pgoff, unsigned long flags); 367 #endif 368 369 /** 370 * vb2_video_unregister_device - unregister the video device and release queue 371 * 372 * @vdev: pointer to &struct video_device 373 * 374 * If the driver uses vb2_fop_release()/_vb2_fop_release(), then it should use 375 * vb2_video_unregister_device() instead of video_unregister_device(). 376 * 377 * This function will call video_unregister_device() and then release the 378 * vb2_queue if streaming is in progress. This will stop streaming and 379 * this will simplify the unbind sequence since after this call all subdevs 380 * will have stopped streaming as well. 381 */ 382 void vb2_video_unregister_device(struct video_device *vdev); 383 384 /** 385 * vb2_ops_wait_prepare - helper function to lock a struct &vb2_queue 386 * 387 * @vq: pointer to &struct vb2_queue 388 * 389 * ..note:: only use if vq->lock is non-NULL. 390 */ 391 void vb2_ops_wait_prepare(struct vb2_queue *vq); 392 393 /** 394 * vb2_ops_wait_finish - helper function to unlock a struct &vb2_queue 395 * 396 * @vq: pointer to &struct vb2_queue 397 * 398 * ..note:: only use if vq->lock is non-NULL. 399 */ 400 void vb2_ops_wait_finish(struct vb2_queue *vq); 401 402 struct media_request; 403 int vb2_request_validate(struct media_request *req); 404 void vb2_request_queue(struct media_request *req); 405 406 #endif /* _MEDIA_VIDEOBUF2_V4L2_H */ 407