1 /* 2 * videobuf2-core.h - Video Buffer 2 Core 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 15 #include <linux/mm_types.h> 16 #include <linux/mutex.h> 17 #include <linux/poll.h> 18 #include <linux/dma-buf.h> 19 #include <linux/bitops.h> 20 #include <media/media-request.h> 21 #include <media/frame_vector.h> 22 23 #define VB2_MAX_FRAME (32) 24 #define VB2_MAX_PLANES (8) 25 26 /** 27 * enum vb2_memory - type of memory model used to make the buffers visible 28 * on userspace. 29 * 30 * @VB2_MEMORY_UNKNOWN: Buffer status is unknown or it is not used yet on 31 * userspace. 32 * @VB2_MEMORY_MMAP: The buffers are allocated by the Kernel and it is 33 * memory mapped via mmap() ioctl. This model is 34 * also used when the user is using the buffers via 35 * read() or write() system calls. 36 * @VB2_MEMORY_USERPTR: The buffers was allocated in userspace and it is 37 * memory mapped via mmap() ioctl. 38 * @VB2_MEMORY_DMABUF: The buffers are passed to userspace via DMA buffer. 39 */ 40 enum vb2_memory { 41 VB2_MEMORY_UNKNOWN = 0, 42 VB2_MEMORY_MMAP = 1, 43 VB2_MEMORY_USERPTR = 2, 44 VB2_MEMORY_DMABUF = 4, 45 }; 46 47 struct vb2_fileio_data; 48 struct vb2_threadio_data; 49 struct vb2_buffer; 50 51 /** 52 * struct vb2_mem_ops - memory handling/memory allocator operations. 53 * @alloc: allocate video memory and, optionally, allocator private data, 54 * return ERR_PTR() on failure or a pointer to allocator private, 55 * per-buffer data on success; the returned private structure 56 * will then be passed as @buf_priv argument to other ops in this 57 * structure. The size argument to this function shall be 58 * *page aligned*. 59 * @put: inform the allocator that the buffer will no longer be used; 60 * usually will result in the allocator freeing the buffer (if 61 * no other users of this buffer are present); the @buf_priv 62 * argument is the allocator private per-buffer structure 63 * previously returned from the alloc callback. 64 * @get_dmabuf: acquire userspace memory for a hardware operation; used for 65 * DMABUF memory types. 66 * @get_userptr: acquire userspace memory for a hardware operation; used for 67 * USERPTR memory types; vaddr is the address passed to the 68 * videobuf2 layer when queuing a video buffer of USERPTR type; 69 * should return an allocator private per-buffer structure 70 * associated with the buffer on success, ERR_PTR() on failure; 71 * the returned private structure will then be passed as @buf_priv 72 * argument to other ops in this structure. 73 * @put_userptr: inform the allocator that a USERPTR buffer will no longer 74 * be used. 75 * @prepare: called every time the buffer is passed from userspace to the 76 * driver, useful for cache synchronisation, optional. 77 * @finish: called every time the buffer is passed back from the driver 78 * to the userspace, also optional. 79 * @attach_dmabuf: attach a shared &struct dma_buf for a hardware operation; 80 * used for DMABUF memory types; dev is the alloc device 81 * dbuf is the shared dma_buf; returns ERR_PTR() on failure; 82 * allocator private per-buffer structure on success; 83 * this needs to be used for further accesses to the buffer. 84 * @detach_dmabuf: inform the exporter of the buffer that the current DMABUF 85 * buffer is no longer used; the @buf_priv argument is the 86 * allocator private per-buffer structure previously returned 87 * from the attach_dmabuf callback. 88 * @map_dmabuf: request for access to the dmabuf from allocator; the allocator 89 * of dmabuf is informed that this driver is going to use the 90 * dmabuf. 91 * @unmap_dmabuf: releases access control to the dmabuf - allocator is notified 92 * that this driver is done using the dmabuf for now. 93 * @vaddr: return a kernel virtual address to a given memory buffer 94 * associated with the passed private structure or NULL if no 95 * such mapping exists. 96 * @cookie: return allocator specific cookie for a given memory buffer 97 * associated with the passed private structure or NULL if not 98 * available. 99 * @num_users: return the current number of users of a memory buffer; 100 * return 1 if the videobuf2 layer (or actually the driver using 101 * it) is the only user. 102 * @mmap: setup a userspace mapping for a given memory buffer under 103 * the provided virtual memory region. 104 * 105 * Those operations are used by the videobuf2 core to implement the memory 106 * handling/memory allocators for each type of supported streaming I/O method. 107 * 108 * .. note:: 109 * #) Required ops for USERPTR types: get_userptr, put_userptr. 110 * 111 * #) Required ops for MMAP types: alloc, put, num_users, mmap. 112 * 113 * #) Required ops for read/write access types: alloc, put, num_users, vaddr. 114 * 115 * #) Required ops for DMABUF types: attach_dmabuf, detach_dmabuf, 116 * map_dmabuf, unmap_dmabuf. 117 */ 118 struct vb2_mem_ops { 119 void *(*alloc)(struct vb2_buffer *vb, 120 struct device *dev, 121 unsigned long size); 122 void (*put)(void *buf_priv); 123 struct dma_buf *(*get_dmabuf)(struct vb2_buffer *vb, 124 void *buf_priv, 125 unsigned long flags); 126 127 void *(*get_userptr)(struct vb2_buffer *vb, 128 struct device *dev, 129 unsigned long vaddr, 130 unsigned long size); 131 void (*put_userptr)(void *buf_priv); 132 133 void (*prepare)(void *buf_priv); 134 void (*finish)(void *buf_priv); 135 136 void *(*attach_dmabuf)(struct vb2_buffer *vb, 137 struct device *dev, 138 struct dma_buf *dbuf, 139 unsigned long size); 140 void (*detach_dmabuf)(void *buf_priv); 141 int (*map_dmabuf)(void *buf_priv); 142 void (*unmap_dmabuf)(void *buf_priv); 143 144 void *(*vaddr)(struct vb2_buffer *vb, void *buf_priv); 145 void *(*cookie)(struct vb2_buffer *vb, void *buf_priv); 146 147 unsigned int (*num_users)(void *buf_priv); 148 149 int (*mmap)(void *buf_priv, struct vm_area_struct *vma); 150 }; 151 152 /** 153 * struct vb2_plane - plane information. 154 * @mem_priv: private data with this plane. 155 * @dbuf: dma_buf - shared buffer object. 156 * @dbuf_mapped: flag to show whether dbuf is mapped or not 157 * @dbuf_duplicated: boolean to show whether dbuf is duplicated with a 158 * previous plane of the buffer. 159 * @bytesused: number of bytes occupied by data in the plane (payload). 160 * @length: size of this plane (NOT the payload) in bytes. The maximum 161 * valid size is MAX_UINT - PAGE_SIZE. 162 * @min_length: minimum required size of this plane (NOT the payload) in bytes. 163 * @length is always greater or equal to @min_length, and like 164 * @length, it is limited to MAX_UINT - PAGE_SIZE. 165 * @m: Union with memtype-specific data. 166 * @m.offset: when memory in the associated struct vb2_buffer is 167 * %VB2_MEMORY_MMAP, equals the offset from the start of 168 * the device memory for this plane (or is a "cookie" that 169 * should be passed to mmap() called on the video node). 170 * @m.userptr: when memory is %VB2_MEMORY_USERPTR, a userspace pointer 171 * pointing to this plane. 172 * @m.fd: when memory is %VB2_MEMORY_DMABUF, a userspace file 173 * descriptor associated with this plane. 174 * @data_offset: offset in the plane to the start of data; usually 0, 175 * unless there is a header in front of the data. 176 * 177 * Should contain enough information to be able to cover all the fields 178 * of &struct v4l2_plane at videodev2.h. 179 */ 180 struct vb2_plane { 181 void *mem_priv; 182 struct dma_buf *dbuf; 183 unsigned int dbuf_mapped; 184 bool dbuf_duplicated; 185 unsigned int bytesused; 186 unsigned int length; 187 unsigned int min_length; 188 union { 189 unsigned int offset; 190 unsigned long userptr; 191 int fd; 192 } m; 193 unsigned int data_offset; 194 }; 195 196 /** 197 * enum vb2_io_modes - queue access methods. 198 * @VB2_MMAP: driver supports MMAP with streaming API. 199 * @VB2_USERPTR: driver supports USERPTR with streaming API. 200 * @VB2_READ: driver supports read() style access. 201 * @VB2_WRITE: driver supports write() style access. 202 * @VB2_DMABUF: driver supports DMABUF with streaming API. 203 */ 204 enum vb2_io_modes { 205 VB2_MMAP = BIT(0), 206 VB2_USERPTR = BIT(1), 207 VB2_READ = BIT(2), 208 VB2_WRITE = BIT(3), 209 VB2_DMABUF = BIT(4), 210 }; 211 212 /** 213 * enum vb2_buffer_state - current video buffer state. 214 * @VB2_BUF_STATE_DEQUEUED: buffer under userspace control. 215 * @VB2_BUF_STATE_IN_REQUEST: buffer is queued in media request. 216 * @VB2_BUF_STATE_PREPARING: buffer is being prepared in videobuf2. 217 * @VB2_BUF_STATE_QUEUED: buffer queued in videobuf2, but not in driver. 218 * @VB2_BUF_STATE_ACTIVE: buffer queued in driver and possibly used 219 * in a hardware operation. 220 * @VB2_BUF_STATE_DONE: buffer returned from driver to videobuf2, but 221 * not yet dequeued to userspace. 222 * @VB2_BUF_STATE_ERROR: same as above, but the operation on the buffer 223 * has ended with an error, which will be reported 224 * to the userspace when it is dequeued. 225 */ 226 enum vb2_buffer_state { 227 VB2_BUF_STATE_DEQUEUED, 228 VB2_BUF_STATE_IN_REQUEST, 229 VB2_BUF_STATE_PREPARING, 230 VB2_BUF_STATE_QUEUED, 231 VB2_BUF_STATE_ACTIVE, 232 VB2_BUF_STATE_DONE, 233 VB2_BUF_STATE_ERROR, 234 }; 235 236 struct vb2_queue; 237 238 /** 239 * struct vb2_buffer - represents a video buffer. 240 * @vb2_queue: pointer to &struct vb2_queue with the queue to 241 * which this driver belongs. 242 * @index: id number of the buffer. 243 * @type: buffer type. 244 * @memory: the method, in which the actual data is passed. 245 * @num_planes: number of planes in the buffer 246 * on an internal driver queue. 247 * @timestamp: frame timestamp in ns. 248 * @request: the request this buffer is associated with. 249 * @req_obj: used to bind this buffer to a request. This 250 * request object has a refcount. 251 */ 252 struct vb2_buffer { 253 struct vb2_queue *vb2_queue; 254 unsigned int index; 255 unsigned int type; 256 unsigned int memory; 257 unsigned int num_planes; 258 u64 timestamp; 259 struct media_request *request; 260 struct media_request_object req_obj; 261 262 /* private: internal use only 263 * 264 * state: current buffer state; do not change 265 * synced: this buffer has been synced for DMA, i.e. the 266 * 'prepare' memop was called. It is cleared again 267 * after the 'finish' memop is called. 268 * prepared: this buffer has been prepared, i.e. the 269 * buf_prepare op was called. It is cleared again 270 * after the 'buf_finish' op is called. 271 * copied_timestamp: the timestamp of this capture buffer was copied 272 * from an output buffer. 273 * skip_cache_sync_on_prepare: when set buffer's ->prepare() function 274 * skips cache sync/invalidation. 275 * skip_cache_sync_on_finish: when set buffer's ->finish() function 276 * skips cache sync/invalidation. 277 * planes: per-plane information; do not change 278 * queued_entry: entry on the queued buffers list, which holds 279 * all buffers queued from userspace 280 * done_entry: entry on the list that stores all buffers ready 281 * to be dequeued to userspace 282 */ 283 enum vb2_buffer_state state; 284 unsigned int synced:1; 285 unsigned int prepared:1; 286 unsigned int copied_timestamp:1; 287 unsigned int skip_cache_sync_on_prepare:1; 288 unsigned int skip_cache_sync_on_finish:1; 289 290 struct vb2_plane planes[VB2_MAX_PLANES]; 291 struct list_head queued_entry; 292 struct list_head done_entry; 293 #ifdef CONFIG_VIDEO_ADV_DEBUG 294 /* 295 * Counters for how often these buffer-related ops are 296 * called. Used to check for unbalanced ops. 297 */ 298 u32 cnt_mem_alloc; 299 u32 cnt_mem_put; 300 u32 cnt_mem_get_dmabuf; 301 u32 cnt_mem_get_userptr; 302 u32 cnt_mem_put_userptr; 303 u32 cnt_mem_prepare; 304 u32 cnt_mem_finish; 305 u32 cnt_mem_attach_dmabuf; 306 u32 cnt_mem_detach_dmabuf; 307 u32 cnt_mem_map_dmabuf; 308 u32 cnt_mem_unmap_dmabuf; 309 u32 cnt_mem_vaddr; 310 u32 cnt_mem_cookie; 311 u32 cnt_mem_num_users; 312 u32 cnt_mem_mmap; 313 314 u32 cnt_buf_out_validate; 315 u32 cnt_buf_init; 316 u32 cnt_buf_prepare; 317 u32 cnt_buf_finish; 318 u32 cnt_buf_cleanup; 319 u32 cnt_buf_queue; 320 u32 cnt_buf_request_complete; 321 322 /* This counts the number of calls to vb2_buffer_done() */ 323 u32 cnt_buf_done; 324 #endif 325 }; 326 327 /** 328 * struct vb2_ops - driver-specific callbacks. 329 * 330 * These operations are not called from interrupt context except where 331 * mentioned specifically. 332 * 333 * @queue_setup: called from VIDIOC_REQBUFS() and VIDIOC_CREATE_BUFS() 334 * handlers before memory allocation. It can be called 335 * twice: if the original number of requested buffers 336 * could not be allocated, then it will be called a 337 * second time with the actually allocated number of 338 * buffers to verify if that is OK. 339 * The driver should return the required number of buffers 340 * in \*num_buffers, the required number of planes per 341 * buffer in \*num_planes, the size of each plane should be 342 * set in the sizes\[\] array and optional per-plane 343 * allocator specific device in the alloc_devs\[\] array. 344 * When called from VIDIOC_REQBUFS(), \*num_planes == 0, 345 * the driver has to use the currently configured format to 346 * determine the plane sizes and \*num_buffers is the total 347 * number of buffers that are being allocated. When called 348 * from VIDIOC_CREATE_BUFS(), \*num_planes != 0 and it 349 * describes the requested number of planes and sizes\[\] 350 * contains the requested plane sizes. In this case 351 * \*num_buffers are being allocated additionally to 352 * the buffers already allocated. If either \*num_planes 353 * or the requested sizes are invalid callback must return %-EINVAL. 354 * @buf_out_validate: called when the output buffer is prepared or queued 355 * to a request; drivers can use this to validate 356 * userspace-provided information; this is required only 357 * for OUTPUT queues. 358 * @buf_init: called once after allocating a buffer (in MMAP case) 359 * or after acquiring a new USERPTR buffer; drivers may 360 * perform additional buffer-related initialization; 361 * initialization failure (return != 0) will prevent 362 * queue setup from completing successfully; optional. 363 * @buf_prepare: called every time the buffer is queued from userspace 364 * and from the VIDIOC_PREPARE_BUF() ioctl; drivers may 365 * perform any initialization required before each 366 * hardware operation in this callback; drivers can 367 * access/modify the buffer here as it is still synced for 368 * the CPU; drivers that support VIDIOC_CREATE_BUFS() must 369 * also validate the buffer size; if an error is returned, 370 * the buffer will not be queued in driver; optional. 371 * @buf_finish: called before every dequeue of the buffer back to 372 * userspace; the buffer is synced for the CPU, so drivers 373 * can access/modify the buffer contents; drivers may 374 * perform any operations required before userspace 375 * accesses the buffer; optional. The buffer state can be 376 * one of the following: %DONE and %ERROR occur while 377 * streaming is in progress, and the %PREPARED state occurs 378 * when the queue has been canceled and all pending 379 * buffers are being returned to their default %DEQUEUED 380 * state. Typically you only have to do something if the 381 * state is %VB2_BUF_STATE_DONE, since in all other cases 382 * the buffer contents will be ignored anyway. 383 * @buf_cleanup: called once before the buffer is freed; drivers may 384 * perform any additional cleanup; optional. 385 * @prepare_streaming: called once to prepare for 'streaming' state; this is 386 * where validation can be done to verify everything is 387 * okay and streaming resources can be claimed. It is 388 * called when the VIDIOC_STREAMON ioctl is called. The 389 * actual streaming starts when @start_streaming is called. 390 * Optional. 391 * @start_streaming: called once to enter 'streaming' state; the driver may 392 * receive buffers with @buf_queue callback 393 * before @start_streaming is called; the driver gets the 394 * number of already queued buffers in count parameter; 395 * driver can return an error if hardware fails, in that 396 * case all buffers that have been already given by 397 * the @buf_queue callback are to be returned by the driver 398 * by calling vb2_buffer_done() with %VB2_BUF_STATE_QUEUED. 399 * If you need a minimum number of buffers before you can 400 * start streaming, then set 401 * &vb2_queue->min_queued_buffers. If that is non-zero 402 * then @start_streaming won't be called until at least 403 * that many buffers have been queued up by userspace. 404 * @stop_streaming: called when 'streaming' state must be disabled; driver 405 * should stop any DMA transactions or wait until they 406 * finish and give back all buffers it got from &buf_queue 407 * callback by calling vb2_buffer_done() with either 408 * %VB2_BUF_STATE_DONE or %VB2_BUF_STATE_ERROR; may use 409 * vb2_wait_for_all_buffers() function 410 * @unprepare_streaming:called as counterpart to @prepare_streaming; any claimed 411 * streaming resources can be released here. It is 412 * called when the VIDIOC_STREAMOFF ioctls is called or 413 * when the streaming filehandle is closed. Optional. 414 * @buf_queue: passes buffer vb to the driver; driver may start 415 * hardware operation on this buffer; driver should give 416 * the buffer back by calling vb2_buffer_done() function; 417 * it is always called after calling VIDIOC_STREAMON() 418 * ioctl; might be called before @start_streaming callback 419 * if user pre-queued buffers before calling 420 * VIDIOC_STREAMON(). 421 * @buf_request_complete: a buffer that was never queued to the driver but is 422 * associated with a queued request was canceled. 423 * The driver will have to mark associated objects in the 424 * request as completed; required if requests are 425 * supported. 426 */ 427 struct vb2_ops { 428 int (*queue_setup)(struct vb2_queue *q, 429 unsigned int *num_buffers, unsigned int *num_planes, 430 unsigned int sizes[], struct device *alloc_devs[]); 431 432 int (*buf_out_validate)(struct vb2_buffer *vb); 433 int (*buf_init)(struct vb2_buffer *vb); 434 int (*buf_prepare)(struct vb2_buffer *vb); 435 void (*buf_finish)(struct vb2_buffer *vb); 436 void (*buf_cleanup)(struct vb2_buffer *vb); 437 438 int (*prepare_streaming)(struct vb2_queue *q); 439 int (*start_streaming)(struct vb2_queue *q, unsigned int count); 440 void (*stop_streaming)(struct vb2_queue *q); 441 void (*unprepare_streaming)(struct vb2_queue *q); 442 443 void (*buf_queue)(struct vb2_buffer *vb); 444 445 void (*buf_request_complete)(struct vb2_buffer *vb); 446 }; 447 448 /** 449 * struct vb2_buf_ops - driver-specific callbacks. 450 * 451 * @verify_planes_array: Verify that a given user space structure contains 452 * enough planes for the buffer. This is called 453 * for each dequeued buffer. 454 * @init_buffer: given a &vb2_buffer initialize the extra data after 455 * struct vb2_buffer. 456 * For V4L2 this is a &struct vb2_v4l2_buffer. 457 * @fill_user_buffer: given a &vb2_buffer fill in the userspace structure. 458 * For V4L2 this is a &struct v4l2_buffer. 459 * @fill_vb2_buffer: given a userspace structure, fill in the &vb2_buffer. 460 * If the userspace structure is invalid, then this op 461 * will return an error. 462 * @copy_timestamp: copy the timestamp from a userspace structure to 463 * the &struct vb2_buffer. 464 */ 465 struct vb2_buf_ops { 466 int (*verify_planes_array)(struct vb2_buffer *vb, const void *pb); 467 void (*init_buffer)(struct vb2_buffer *vb); 468 void (*fill_user_buffer)(struct vb2_buffer *vb, void *pb); 469 int (*fill_vb2_buffer)(struct vb2_buffer *vb, struct vb2_plane *planes); 470 void (*copy_timestamp)(struct vb2_buffer *vb, const void *pb); 471 }; 472 473 /** 474 * struct vb2_queue - a videobuf2 queue. 475 * 476 * @type: private buffer type whose content is defined by the vb2-core 477 * caller. For example, for V4L2, it should match 478 * the types defined on &enum v4l2_buf_type. 479 * @io_modes: supported io methods (see &enum vb2_io_modes). 480 * @dev: device to use for the default allocation context if the driver 481 * doesn't fill in the @alloc_devs array. 482 * @dma_attrs: DMA attributes to use for the DMA. 483 * @bidirectional: when this flag is set the DMA direction for the buffers of 484 * this queue will be overridden with %DMA_BIDIRECTIONAL direction. 485 * This is useful in cases where the hardware (firmware) writes to 486 * a buffer which is mapped as read (%DMA_TO_DEVICE), or reads from 487 * buffer which is mapped for write (%DMA_FROM_DEVICE) in order 488 * to satisfy some internal hardware restrictions or adds a padding 489 * needed by the processing algorithm. In case the DMA mapping is 490 * not bidirectional but the hardware (firmware) trying to access 491 * the buffer (in the opposite direction) this could lead to an 492 * IOMMU protection faults. 493 * @fileio_read_once: report EOF after reading the first buffer 494 * @fileio_write_immediately: queue buffer after each write() call 495 * @allow_zero_bytesused: allow bytesused == 0 to be passed to the driver 496 * @quirk_poll_must_check_waiting_for_buffers: Return %EPOLLERR at poll when QBUF 497 * has not been called. This is a vb1 idiom that has been adopted 498 * also by vb2. 499 * @supports_requests: this queue supports the Request API. 500 * @requires_requests: this queue requires the Request API. If this is set to 1, 501 * then supports_requests must be set to 1 as well. 502 * @uses_qbuf: qbuf was used directly for this queue. Set to 1 the first 503 * time this is called. Set to 0 when the queue is canceled. 504 * If this is 1, then you cannot queue buffers from a request. 505 * @uses_requests: requests are used for this queue. Set to 1 the first time 506 * a request is queued. Set to 0 when the queue is canceled. 507 * If this is 1, then you cannot queue buffers directly. 508 * @allow_cache_hints: when set user-space can pass cache management hints in 509 * order to skip cache flush/invalidation on ->prepare() or/and 510 * ->finish(). 511 * @non_coherent_mem: when set queue will attempt to allocate buffers using 512 * non-coherent memory. 513 * @lock: pointer to a mutex that protects the &struct vb2_queue. The 514 * driver must set this to a mutex to let the v4l2 core serialize 515 * the queuing ioctls. This lock is used when waiting for a new 516 * buffer to arrive: the lock is released, we wait for the new 517 * buffer, and then retaken. 518 * @owner: The filehandle that 'owns' the buffers, i.e. the filehandle 519 * that called reqbufs, create_buffers or started fileio. 520 * This field is not used by the videobuf2 core API, but it allows 521 * drivers to easily associate an owner filehandle with the queue. 522 * @ops: driver-specific callbacks 523 * @mem_ops: memory allocator specific callbacks 524 * @buf_ops: callbacks to deliver buffer information. 525 * between user-space and kernel-space. 526 * @drv_priv: driver private data. 527 * @subsystem_flags: Flags specific to the subsystem (V4L2/DVB/etc.). Not used 528 * by the vb2 core. 529 * @buf_struct_size: size of the driver-specific buffer structure; 530 * "0" indicates the driver doesn't want to use a custom buffer 531 * structure type. In that case a subsystem-specific struct 532 * will be used (in the case of V4L2 that is 533 * ``sizeof(struct vb2_v4l2_buffer)``). The first field of the 534 * driver-specific buffer structure must be the subsystem-specific 535 * struct (vb2_v4l2_buffer in the case of V4L2). 536 * @timestamp_flags: Timestamp flags; ``V4L2_BUF_FLAG_TIMESTAMP_*`` and 537 * ``V4L2_BUF_FLAG_TSTAMP_SRC_*`` 538 * @gfp_flags: additional gfp flags used when allocating the buffers. 539 * Typically this is 0, but it may be e.g. %GFP_DMA or %__GFP_DMA32 540 * to force the buffer allocation to a specific memory zone. 541 * @min_queued_buffers: the minimum number of queued buffers needed before 542 * @start_streaming can be called. Used when a DMA engine 543 * cannot be started unless at least this number of buffers 544 * have been queued into the driver. 545 * VIDIOC_REQBUFS will ensure at least @min_queued_buffers + 1 546 * buffers will be allocated. Note that VIDIOC_CREATE_BUFS will not 547 * modify the requested buffer count. 548 * @min_reqbufs_allocation: the minimum number of buffers to be allocated when 549 * calling VIDIOC_REQBUFS. Note that VIDIOC_CREATE_BUFS will *not* 550 * modify the requested buffer count and does not use this field. 551 * Drivers can set this if there has to be a certain number of 552 * buffers available for the hardware to work effectively. 553 * This allows calling VIDIOC_REQBUFS with a buffer count of 1 and 554 * it will be automatically adjusted to a workable buffer count. 555 * If set, then @min_reqbufs_allocation must be larger than 556 * @min_queued_buffers + 1. 557 * If this field is > 3, then it is highly recommended that the 558 * driver implements the V4L2_CID_MIN_BUFFERS_FOR_CAPTURE/OUTPUT 559 * control. 560 * @alloc_devs: &struct device memory type/allocator-specific per-plane device 561 */ 562 /* 563 * Private elements (won't appear at the uAPI book): 564 * @mmap_lock: private mutex used when buffers are allocated/freed/mmapped 565 * @memory: current memory type used 566 * @dma_dir: DMA mapping direction. 567 * @bufs: videobuf2 buffer structures. If it is non-NULL then 568 * bufs_bitmap is also non-NULL. 569 * @bufs_bitmap: bitmap tracking whether each bufs[] entry is used 570 * @max_num_buffers: upper limit of number of allocated/used buffers. 571 * If set to 0 v4l2 core will change it VB2_MAX_FRAME 572 * for backward compatibility. 573 * @queued_list: list of buffers currently queued from userspace 574 * @queued_count: number of buffers queued and ready for streaming. 575 * @owned_by_drv_count: number of buffers owned by the driver 576 * @done_list: list of buffers ready to be dequeued to userspace 577 * @done_lock: lock to protect done_list list 578 * @done_wq: waitqueue for processes waiting for buffers ready to be dequeued 579 * @streaming: current streaming state 580 * @start_streaming_called: @start_streaming was called successfully and we 581 * started streaming. 582 * @error: a fatal error occurred on the queue 583 * @waiting_for_buffers: used in poll() to check if vb2 is still waiting for 584 * buffers. Only set for capture queues if qbuf has not yet been 585 * called since poll() needs to return %EPOLLERR in that situation. 586 * @waiting_in_dqbuf: set by the core for the duration of a blocking DQBUF, when 587 * it has to wait for a buffer to become available with vb2_queue->lock 588 * released. Used to prevent destroying the queue by other threads. 589 * @is_multiplanar: set if buffer type is multiplanar 590 * @is_output: set if buffer type is output 591 * @is_busy: set if at least one buffer has been allocated at some time. 592 * @copy_timestamp: set if vb2-core should set timestamps 593 * @last_buffer_dequeued: used in poll() and DQBUF to immediately return if the 594 * last decoded buffer was already dequeued. Set for capture queues 595 * when a buffer with the %V4L2_BUF_FLAG_LAST is dequeued. 596 * @fileio: file io emulator internal data, used only if emulator is active 597 * @threadio: thread io internal data, used only if thread is active 598 * @name: queue name, used for logging purpose. Initialized automatically 599 * if left empty by drivers. 600 */ 601 struct vb2_queue { 602 unsigned int type; 603 unsigned int io_modes; 604 struct device *dev; 605 unsigned long dma_attrs; 606 unsigned int bidirectional:1; 607 unsigned int fileio_read_once:1; 608 unsigned int fileio_write_immediately:1; 609 unsigned int allow_zero_bytesused:1; 610 unsigned int quirk_poll_must_check_waiting_for_buffers:1; 611 unsigned int supports_requests:1; 612 unsigned int requires_requests:1; 613 unsigned int uses_qbuf:1; 614 unsigned int uses_requests:1; 615 unsigned int allow_cache_hints:1; 616 unsigned int non_coherent_mem:1; 617 618 struct mutex *lock; 619 void *owner; 620 621 const struct vb2_ops *ops; 622 const struct vb2_mem_ops *mem_ops; 623 const struct vb2_buf_ops *buf_ops; 624 625 void *drv_priv; 626 u32 subsystem_flags; 627 unsigned int buf_struct_size; 628 u32 timestamp_flags; 629 gfp_t gfp_flags; 630 u32 min_queued_buffers; 631 u32 min_reqbufs_allocation; 632 633 struct device *alloc_devs[VB2_MAX_PLANES]; 634 635 /* private: internal use only */ 636 struct mutex mmap_lock; 637 unsigned int memory; 638 enum dma_data_direction dma_dir; 639 struct vb2_buffer **bufs; 640 unsigned long *bufs_bitmap; 641 unsigned int max_num_buffers; 642 643 struct list_head queued_list; 644 unsigned int queued_count; 645 646 atomic_t owned_by_drv_count; 647 struct list_head done_list; 648 spinlock_t done_lock; 649 wait_queue_head_t done_wq; 650 651 unsigned int streaming:1; 652 unsigned int start_streaming_called:1; 653 unsigned int error:1; 654 unsigned int waiting_for_buffers:1; 655 unsigned int waiting_in_dqbuf:1; 656 unsigned int is_multiplanar:1; 657 unsigned int is_output:1; 658 unsigned int is_busy:1; 659 unsigned int copy_timestamp:1; 660 unsigned int last_buffer_dequeued:1; 661 662 struct vb2_fileio_data *fileio; 663 struct vb2_threadio_data *threadio; 664 665 char name[32]; 666 667 #ifdef CONFIG_VIDEO_ADV_DEBUG 668 /* 669 * Counters for how often these queue-related ops are 670 * called. Used to check for unbalanced ops. 671 */ 672 u32 cnt_queue_setup; 673 u32 cnt_prepare_streaming; 674 u32 cnt_start_streaming; 675 u32 cnt_stop_streaming; 676 u32 cnt_unprepare_streaming; 677 #endif 678 }; 679 680 /** 681 * vb2_queue_allows_cache_hints() - Return true if the queue allows cache 682 * and memory consistency hints. 683 * 684 * @q: pointer to &struct vb2_queue with videobuf2 queue 685 */ 686 static inline bool vb2_queue_allows_cache_hints(struct vb2_queue *q) 687 { 688 return q->allow_cache_hints && q->memory == VB2_MEMORY_MMAP; 689 } 690 691 /** 692 * vb2_plane_vaddr() - Return a kernel virtual address of a given plane. 693 * @vb: pointer to &struct vb2_buffer to which the plane in 694 * question belongs to. 695 * @plane_no: plane number for which the address is to be returned. 696 * 697 * This function returns a kernel virtual address of a given plane if 698 * such a mapping exist, NULL otherwise. 699 */ 700 void *vb2_plane_vaddr(struct vb2_buffer *vb, unsigned int plane_no); 701 702 /** 703 * vb2_plane_cookie() - Return allocator specific cookie for the given plane. 704 * @vb: pointer to &struct vb2_buffer to which the plane in 705 * question belongs to. 706 * @plane_no: plane number for which the cookie is to be returned. 707 * 708 * This function returns an allocator specific cookie for a given plane if 709 * available, NULL otherwise. The allocator should provide some simple static 710 * inline function, which would convert this cookie to the allocator specific 711 * type that can be used directly by the driver to access the buffer. This can 712 * be for example physical address, pointer to scatter list or IOMMU mapping. 713 */ 714 void *vb2_plane_cookie(struct vb2_buffer *vb, unsigned int plane_no); 715 716 /** 717 * vb2_buffer_done() - inform videobuf2 that an operation on a buffer 718 * is finished. 719 * @vb: pointer to &struct vb2_buffer to be used. 720 * @state: state of the buffer, as defined by &enum vb2_buffer_state. 721 * Either %VB2_BUF_STATE_DONE if the operation finished 722 * successfully, %VB2_BUF_STATE_ERROR if the operation finished 723 * with an error or %VB2_BUF_STATE_QUEUED. 724 * 725 * This function should be called by the driver after a hardware operation on 726 * a buffer is finished and the buffer may be returned to userspace. The driver 727 * cannot use this buffer anymore until it is queued back to it by videobuf 728 * by the means of &vb2_ops->buf_queue callback. Only buffers previously queued 729 * to the driver by &vb2_ops->buf_queue can be passed to this function. 730 * 731 * While streaming a buffer can only be returned in state DONE or ERROR. 732 * The &vb2_ops->start_streaming op can also return them in case the DMA engine 733 * cannot be started for some reason. In that case the buffers should be 734 * returned with state QUEUED to put them back into the queue. 735 */ 736 void vb2_buffer_done(struct vb2_buffer *vb, enum vb2_buffer_state state); 737 738 /** 739 * vb2_discard_done() - discard all buffers marked as DONE. 740 * @q: pointer to &struct vb2_queue with videobuf2 queue. 741 * 742 * This function is intended to be used with suspend/resume operations. It 743 * discards all 'done' buffers as they would be too old to be requested after 744 * resume. 745 * 746 * Drivers must stop the hardware and synchronize with interrupt handlers and/or 747 * delayed works before calling this function to make sure no buffer will be 748 * touched by the driver and/or hardware. 749 */ 750 void vb2_discard_done(struct vb2_queue *q); 751 752 /** 753 * vb2_wait_for_all_buffers() - wait until all buffers are given back to vb2. 754 * @q: pointer to &struct vb2_queue with videobuf2 queue. 755 * 756 * This function will wait until all buffers that have been given to the driver 757 * by &vb2_ops->buf_queue are given back to vb2 with vb2_buffer_done(). 758 * It is intended to be called with all locks taken, for example from 759 * &vb2_ops->stop_streaming callback. 760 */ 761 int vb2_wait_for_all_buffers(struct vb2_queue *q); 762 763 /** 764 * vb2_core_querybuf() - query video buffer information. 765 * @q: pointer to &struct vb2_queue with videobuf2 queue. 766 * @vb: pointer to struct &vb2_buffer. 767 * @pb: buffer struct passed from userspace. 768 * 769 * Videobuf2 core helper to implement VIDIOC_QUERYBUF() operation. It is called 770 * internally by VB2 by an API-specific handler, like ``videobuf2-v4l2.h``. 771 * 772 * The passed buffer should have been verified. 773 * 774 * This function fills the relevant information for the userspace. 775 * 776 * Return: returns zero on success; an error code otherwise. 777 */ 778 void vb2_core_querybuf(struct vb2_queue *q, struct vb2_buffer *vb, void *pb); 779 780 /** 781 * vb2_core_reqbufs() - Initiate streaming. 782 * @q: pointer to &struct vb2_queue with videobuf2 queue. 783 * @memory: memory type, as defined by &enum vb2_memory. 784 * @flags: auxiliary queue/buffer management flags. Currently, the only 785 * used flag is %V4L2_MEMORY_FLAG_NON_COHERENT. 786 * @count: requested buffer count. 787 * 788 * Videobuf2 core helper to implement VIDIOC_REQBUF() operation. It is called 789 * internally by VB2 by an API-specific handler, like ``videobuf2-v4l2.h``. 790 * 791 * This function: 792 * 793 * #) verifies streaming parameters passed from the userspace; 794 * #) sets up the queue; 795 * #) negotiates number of buffers and planes per buffer with the driver 796 * to be used during streaming; 797 * #) allocates internal buffer structures (&struct vb2_buffer), according to 798 * the agreed parameters; 799 * #) for MMAP memory type, allocates actual video memory, using the 800 * memory handling/allocation routines provided during queue initialization. 801 * 802 * If req->count is 0, all the memory will be freed instead. 803 * 804 * If the queue has been allocated previously by a previous vb2_core_reqbufs() 805 * call and the queue is not busy, memory will be reallocated. 806 * 807 * Return: returns zero on success; an error code otherwise. 808 */ 809 int vb2_core_reqbufs(struct vb2_queue *q, enum vb2_memory memory, 810 unsigned int flags, unsigned int *count); 811 812 /** 813 * vb2_core_create_bufs() - Allocate buffers and any required auxiliary structs 814 * @q: pointer to &struct vb2_queue with videobuf2 queue. 815 * @memory: memory type, as defined by &enum vb2_memory. 816 * @flags: auxiliary queue/buffer management flags. 817 * @count: requested buffer count. 818 * @requested_planes: number of planes requested. 819 * @requested_sizes: array with the size of the planes. 820 * @first_index: index of the first created buffer, all allocated buffers have 821 * indices in the range [first_index..first_index+count-1] 822 * 823 * Videobuf2 core helper to implement VIDIOC_CREATE_BUFS() operation. It is 824 * called internally by VB2 by an API-specific handler, like 825 * ``videobuf2-v4l2.h``. 826 * 827 * This function: 828 * 829 * #) verifies parameter sanity; 830 * #) calls the &vb2_ops->queue_setup queue operation; 831 * #) performs any necessary memory allocations. 832 * 833 * Return: returns zero on success; an error code otherwise. 834 */ 835 int vb2_core_create_bufs(struct vb2_queue *q, enum vb2_memory memory, 836 unsigned int flags, unsigned int *count, 837 unsigned int requested_planes, 838 const unsigned int requested_sizes[], 839 unsigned int *first_index); 840 841 /** 842 * vb2_core_prepare_buf() - Pass ownership of a buffer from userspace 843 * to the kernel. 844 * @q: pointer to &struct vb2_queue with videobuf2 queue. 845 * @vb: pointer to struct &vb2_buffer. 846 * @pb: buffer structure passed from userspace to 847 * &v4l2_ioctl_ops->vidioc_prepare_buf handler in driver. 848 * 849 * Videobuf2 core helper to implement VIDIOC_PREPARE_BUF() operation. It is 850 * called internally by VB2 by an API-specific handler, like 851 * ``videobuf2-v4l2.h``. 852 * 853 * The passed buffer should have been verified. 854 * 855 * This function calls vb2_ops->buf_prepare callback in the driver 856 * (if provided), in which driver-specific buffer initialization can 857 * be performed. 858 * 859 * Return: returns zero on success; an error code otherwise. 860 */ 861 int vb2_core_prepare_buf(struct vb2_queue *q, struct vb2_buffer *vb, void *pb); 862 863 /** 864 * vb2_core_remove_bufs() - 865 * @q: pointer to &struct vb2_queue with videobuf2 queue. 866 * @start: first index of the range of buffers to remove. 867 * @count: number of buffers to remove. 868 * 869 * Return: returns zero on success; an error code otherwise. 870 */ 871 int vb2_core_remove_bufs(struct vb2_queue *q, unsigned int start, unsigned int count); 872 873 /** 874 * vb2_core_qbuf() - Queue a buffer from userspace 875 * 876 * @q: pointer to &struct vb2_queue with videobuf2 queue. 877 * @vb: pointer to struct &vb2_buffer. 878 * @pb: buffer structure passed from userspace to 879 * v4l2_ioctl_ops->vidioc_qbuf handler in driver 880 * @req: pointer to &struct media_request, may be NULL. 881 * 882 * Videobuf2 core helper to implement VIDIOC_QBUF() operation. It is called 883 * internally by VB2 by an API-specific handler, like ``videobuf2-v4l2.h``. 884 * 885 * This function: 886 * 887 * #) If @req is non-NULL, then the buffer will be bound to this 888 * media request and it returns. The buffer will be prepared and 889 * queued to the driver (i.e. the next two steps) when the request 890 * itself is queued. 891 * #) if necessary, calls &vb2_ops->buf_prepare callback in the driver 892 * (if provided), in which driver-specific buffer initialization can 893 * be performed; 894 * #) if streaming is on, queues the buffer in driver by the means of 895 * &vb2_ops->buf_queue callback for processing. 896 * 897 * Return: returns zero on success; an error code otherwise. 898 */ 899 int vb2_core_qbuf(struct vb2_queue *q, struct vb2_buffer *vb, void *pb, 900 struct media_request *req); 901 902 /** 903 * vb2_core_dqbuf() - Dequeue a buffer to the userspace 904 * @q: pointer to &struct vb2_queue with videobuf2 queue 905 * @pindex: pointer to the buffer index. May be NULL 906 * @pb: buffer structure passed from userspace to 907 * v4l2_ioctl_ops->vidioc_dqbuf handler in driver. 908 * @nonblocking: if true, this call will not sleep waiting for a buffer if no 909 * buffers ready for dequeuing are present. Normally the driver 910 * would be passing (file->f_flags & O_NONBLOCK) here. 911 * 912 * Videobuf2 core helper to implement VIDIOC_DQBUF() operation. It is called 913 * internally by VB2 by an API-specific handler, like ``videobuf2-v4l2.h``. 914 * 915 * This function: 916 * 917 * #) calls buf_finish callback in the driver (if provided), in which 918 * driver can perform any additional operations that may be required before 919 * returning the buffer to userspace, such as cache sync, 920 * #) the buffer struct members are filled with relevant information for 921 * the userspace. 922 * 923 * Return: returns zero on success; an error code otherwise. 924 */ 925 int vb2_core_dqbuf(struct vb2_queue *q, unsigned int *pindex, void *pb, 926 bool nonblocking); 927 928 /** 929 * vb2_core_streamon() - Implements VB2 stream ON logic 930 * 931 * @q: pointer to &struct vb2_queue with videobuf2 queue 932 * @type: type of the queue to be started. 933 * For V4L2, this is defined by &enum v4l2_buf_type type. 934 * 935 * Videobuf2 core helper to implement VIDIOC_STREAMON() operation. It is called 936 * internally by VB2 by an API-specific handler, like ``videobuf2-v4l2.h``. 937 * 938 * Return: returns zero on success; an error code otherwise. 939 */ 940 int vb2_core_streamon(struct vb2_queue *q, unsigned int type); 941 942 /** 943 * vb2_core_streamoff() - Implements VB2 stream OFF logic 944 * 945 * @q: pointer to &struct vb2_queue with videobuf2 queue 946 * @type: type of the queue to be started. 947 * For V4L2, this is defined by &enum v4l2_buf_type type. 948 * 949 * Videobuf2 core helper to implement VIDIOC_STREAMOFF() operation. It is 950 * called internally by VB2 by an API-specific handler, like 951 * ``videobuf2-v4l2.h``. 952 * 953 * Return: returns zero on success; an error code otherwise. 954 */ 955 int vb2_core_streamoff(struct vb2_queue *q, unsigned int type); 956 957 /** 958 * vb2_core_expbuf() - Export a buffer as a file descriptor. 959 * @q: pointer to &struct vb2_queue with videobuf2 queue. 960 * @fd: pointer to the file descriptor associated with DMABUF 961 * (set by driver). 962 * @type: buffer type. 963 * @vb: pointer to struct &vb2_buffer. 964 * @plane: index of the plane to be exported, 0 for single plane queues 965 * @flags: file flags for newly created file, as defined at 966 * include/uapi/asm-generic/fcntl.h. 967 * Currently, the only used flag is %O_CLOEXEC. 968 * is supported, refer to manual of open syscall for more details. 969 * 970 * 971 * Videobuf2 core helper to implement VIDIOC_EXPBUF() operation. It is called 972 * internally by VB2 by an API-specific handler, like ``videobuf2-v4l2.h``. 973 * 974 * Return: returns zero on success; an error code otherwise. 975 */ 976 int vb2_core_expbuf(struct vb2_queue *q, int *fd, unsigned int type, 977 struct vb2_buffer *vb, unsigned int plane, unsigned int flags); 978 979 /** 980 * vb2_core_queue_init() - initialize a videobuf2 queue 981 * @q: pointer to &struct vb2_queue with videobuf2 queue. 982 * This structure should be allocated in driver 983 * 984 * The &vb2_queue structure should be allocated by the driver. The driver is 985 * responsible of clearing it's content and setting initial values for some 986 * required entries before calling this function. 987 * 988 * .. note:: 989 * 990 * The following fields at @q should be set before calling this function: 991 * &vb2_queue->ops, &vb2_queue->mem_ops, &vb2_queue->type. 992 */ 993 int vb2_core_queue_init(struct vb2_queue *q); 994 995 /** 996 * vb2_core_queue_release() - stop streaming, release the queue and free memory 997 * @q: pointer to &struct vb2_queue with videobuf2 queue. 998 * 999 * This function stops streaming and performs necessary clean ups, including 1000 * freeing video buffer memory. The driver is responsible for freeing 1001 * the &struct vb2_queue itself. 1002 */ 1003 void vb2_core_queue_release(struct vb2_queue *q); 1004 1005 /** 1006 * vb2_queue_error() - signal a fatal error on the queue 1007 * @q: pointer to &struct vb2_queue with videobuf2 queue. 1008 * 1009 * Flag that a fatal unrecoverable error has occurred and wake up all processes 1010 * waiting on the queue. Polling will now set %EPOLLERR and queuing and dequeuing 1011 * buffers will return %-EIO. 1012 * 1013 * The error flag will be cleared when canceling the queue, either from 1014 * vb2_streamoff() or vb2_queue_release(). Drivers should thus not call this 1015 * function before starting the stream, otherwise the error flag will remain set 1016 * until the queue is released when closing the device node. 1017 */ 1018 void vb2_queue_error(struct vb2_queue *q); 1019 1020 /** 1021 * vb2_mmap() - map video buffers into application address space. 1022 * @q: pointer to &struct vb2_queue with videobuf2 queue. 1023 * @vma: pointer to &struct vm_area_struct with the vma passed 1024 * to the mmap file operation handler in the driver. 1025 * 1026 * Should be called from mmap file operation handler of a driver. 1027 * This function maps one plane of one of the available video buffers to 1028 * userspace. To map whole video memory allocated on reqbufs, this function 1029 * has to be called once per each plane per each buffer previously allocated. 1030 * 1031 * When the userspace application calls mmap, it passes to it an offset returned 1032 * to it earlier by the means of &v4l2_ioctl_ops->vidioc_querybuf handler. 1033 * That offset acts as a "cookie", which is then used to identify the plane 1034 * to be mapped. 1035 * 1036 * This function finds a plane with a matching offset and a mapping is performed 1037 * by the means of a provided memory operation. 1038 * 1039 * The return values from this function are intended to be directly returned 1040 * from the mmap handler in driver. 1041 */ 1042 int vb2_mmap(struct vb2_queue *q, struct vm_area_struct *vma); 1043 1044 #ifndef CONFIG_MMU 1045 /** 1046 * vb2_get_unmapped_area - map video buffers into application address space. 1047 * @q: pointer to &struct vb2_queue with videobuf2 queue. 1048 * @addr: memory address. 1049 * @len: buffer size. 1050 * @pgoff: page offset. 1051 * @flags: memory flags. 1052 * 1053 * This function is used in noMMU platforms to propose address mapping 1054 * for a given buffer. It's intended to be used as a handler for the 1055 * &file_operations->get_unmapped_area operation. 1056 * 1057 * This is called by the mmap() syscall routines will call this 1058 * to get a proposed address for the mapping, when ``!CONFIG_MMU``. 1059 */ 1060 unsigned long vb2_get_unmapped_area(struct vb2_queue *q, 1061 unsigned long addr, 1062 unsigned long len, 1063 unsigned long pgoff, 1064 unsigned long flags); 1065 #endif 1066 1067 /** 1068 * vb2_core_poll() - implements poll syscall() logic. 1069 * @q: pointer to &struct vb2_queue with videobuf2 queue. 1070 * @file: &struct file argument passed to the poll 1071 * file operation handler. 1072 * @wait: &poll_table wait argument passed to the poll 1073 * file operation handler. 1074 * 1075 * This function implements poll file operation handler for a driver. 1076 * For CAPTURE queues, if a buffer is ready to be dequeued, the userspace will 1077 * be informed that the file descriptor of a video device is available for 1078 * reading. 1079 * For OUTPUT queues, if a buffer is ready to be dequeued, the file descriptor 1080 * will be reported as available for writing. 1081 * 1082 * The return values from this function are intended to be directly returned 1083 * from poll handler in driver. 1084 */ 1085 __poll_t vb2_core_poll(struct vb2_queue *q, struct file *file, 1086 poll_table *wait); 1087 1088 /** 1089 * vb2_read() - implements read() syscall logic. 1090 * @q: pointer to &struct vb2_queue with videobuf2 queue. 1091 * @data: pointed to target userspace buffer 1092 * @count: number of bytes to read 1093 * @ppos: file handle position tracking pointer 1094 * @nonblock: mode selector (1 means blocking calls, 0 means nonblocking) 1095 */ 1096 size_t vb2_read(struct vb2_queue *q, char __user *data, size_t count, 1097 loff_t *ppos, int nonblock); 1098 /** 1099 * vb2_write() - implements write() syscall logic. 1100 * @q: pointer to &struct vb2_queue with videobuf2 queue. 1101 * @data: pointed to target userspace buffer 1102 * @count: number of bytes to write 1103 * @ppos: file handle position tracking pointer 1104 * @nonblock: mode selector (1 means blocking calls, 0 means nonblocking) 1105 */ 1106 size_t vb2_write(struct vb2_queue *q, const char __user *data, size_t count, 1107 loff_t *ppos, int nonblock); 1108 1109 /** 1110 * typedef vb2_thread_fnc - callback function for use with vb2_thread. 1111 * 1112 * @vb: pointer to struct &vb2_buffer. 1113 * @priv: pointer to a private data. 1114 * 1115 * This is called whenever a buffer is dequeued in the thread. 1116 */ 1117 typedef int (*vb2_thread_fnc)(struct vb2_buffer *vb, void *priv); 1118 1119 /** 1120 * vb2_thread_start() - start a thread for the given queue. 1121 * @q: pointer to &struct vb2_queue with videobuf2 queue. 1122 * @fnc: &vb2_thread_fnc callback function. 1123 * @priv: priv pointer passed to the callback function. 1124 * @thread_name:the name of the thread. This will be prefixed with "vb2-". 1125 * 1126 * This starts a thread that will queue and dequeue until an error occurs 1127 * or vb2_thread_stop() is called. 1128 * 1129 * .. attention:: 1130 * 1131 * This function should not be used for anything else but the videobuf2-dvb 1132 * support. If you think you have another good use-case for this, then please 1133 * contact the linux-media mailing list first. 1134 */ 1135 int vb2_thread_start(struct vb2_queue *q, vb2_thread_fnc fnc, void *priv, 1136 const char *thread_name); 1137 1138 /** 1139 * vb2_thread_stop() - stop the thread for the given queue. 1140 * @q: pointer to &struct vb2_queue with videobuf2 queue. 1141 */ 1142 int vb2_thread_stop(struct vb2_queue *q); 1143 1144 /** 1145 * vb2_is_streaming() - return streaming status of the queue. 1146 * @q: pointer to &struct vb2_queue with videobuf2 queue. 1147 */ 1148 static inline bool vb2_is_streaming(struct vb2_queue *q) 1149 { 1150 return q->streaming; 1151 } 1152 1153 /** 1154 * vb2_fileio_is_active() - return true if fileio is active. 1155 * @q: pointer to &struct vb2_queue with videobuf2 queue. 1156 * 1157 * This returns true if read() or write() is used to stream the data 1158 * as opposed to stream I/O. This is almost never an important distinction, 1159 * except in rare cases. One such case is that using read() or write() to 1160 * stream a format using %V4L2_FIELD_ALTERNATE is not allowed since there 1161 * is no way you can pass the field information of each buffer to/from 1162 * userspace. A driver that supports this field format should check for 1163 * this in the &vb2_ops->queue_setup op and reject it if this function returns 1164 * true. 1165 */ 1166 static inline bool vb2_fileio_is_active(struct vb2_queue *q) 1167 { 1168 return q->fileio; 1169 } 1170 1171 /** 1172 * vb2_get_num_buffers() - get the number of buffer in a queue 1173 * @q: pointer to &struct vb2_queue with videobuf2 queue. 1174 */ 1175 static inline unsigned int vb2_get_num_buffers(struct vb2_queue *q) 1176 { 1177 if (q->bufs_bitmap) 1178 return bitmap_weight(q->bufs_bitmap, q->max_num_buffers); 1179 1180 return 0; 1181 } 1182 1183 /** 1184 * vb2_is_busy() - return busy status of the queue. 1185 * @q: pointer to &struct vb2_queue with videobuf2 queue. 1186 * 1187 * This function checks if queue has any buffers allocated. 1188 */ 1189 static inline bool vb2_is_busy(struct vb2_queue *q) 1190 { 1191 return !!q->is_busy; 1192 } 1193 1194 /** 1195 * vb2_get_drv_priv() - return driver private data associated with the queue. 1196 * @q: pointer to &struct vb2_queue with videobuf2 queue. 1197 */ 1198 static inline void *vb2_get_drv_priv(struct vb2_queue *q) 1199 { 1200 return q->drv_priv; 1201 } 1202 1203 /** 1204 * vb2_set_plane_payload() - set bytesused for the plane @plane_no. 1205 * @vb: pointer to &struct vb2_buffer to which the plane in 1206 * question belongs to. 1207 * @plane_no: plane number for which payload should be set. 1208 * @size: payload in bytes. 1209 */ 1210 static inline void vb2_set_plane_payload(struct vb2_buffer *vb, 1211 unsigned int plane_no, unsigned long size) 1212 { 1213 /* 1214 * size must never be larger than the buffer length, so 1215 * warn and clamp to the buffer length if that's the case. 1216 */ 1217 if (plane_no < vb->num_planes) { 1218 if (WARN_ON_ONCE(size > vb->planes[plane_no].length)) 1219 size = vb->planes[plane_no].length; 1220 vb->planes[plane_no].bytesused = size; 1221 } 1222 } 1223 1224 /** 1225 * vb2_get_plane_payload() - get bytesused for the plane plane_no 1226 * @vb: pointer to &struct vb2_buffer to which the plane in 1227 * question belongs to. 1228 * @plane_no: plane number for which payload should be set. 1229 */ 1230 static inline unsigned long vb2_get_plane_payload(struct vb2_buffer *vb, 1231 unsigned int plane_no) 1232 { 1233 if (plane_no < vb->num_planes) 1234 return vb->planes[plane_no].bytesused; 1235 return 0; 1236 } 1237 1238 /** 1239 * vb2_plane_size() - return plane size in bytes. 1240 * @vb: pointer to &struct vb2_buffer to which the plane in 1241 * question belongs to. 1242 * @plane_no: plane number for which size should be returned. 1243 */ 1244 static inline unsigned long 1245 vb2_plane_size(struct vb2_buffer *vb, unsigned int plane_no) 1246 { 1247 if (plane_no < vb->num_planes) 1248 return vb->planes[plane_no].length; 1249 return 0; 1250 } 1251 1252 /** 1253 * vb2_start_streaming_called() - return streaming status of driver. 1254 * @q: pointer to &struct vb2_queue with videobuf2 queue. 1255 */ 1256 static inline bool vb2_start_streaming_called(struct vb2_queue *q) 1257 { 1258 return q->start_streaming_called; 1259 } 1260 1261 /** 1262 * vb2_clear_last_buffer_dequeued() - clear last buffer dequeued flag of queue. 1263 * @q: pointer to &struct vb2_queue with videobuf2 queue. 1264 */ 1265 static inline void vb2_clear_last_buffer_dequeued(struct vb2_queue *q) 1266 { 1267 q->last_buffer_dequeued = false; 1268 } 1269 1270 /** 1271 * vb2_get_buffer() - get a buffer from a queue 1272 * @q: pointer to &struct vb2_queue with videobuf2 queue. 1273 * @index: buffer index 1274 * 1275 * This function obtains a buffer from a queue, by its index. 1276 * Keep in mind that there is no refcounting involved in this 1277 * operation, so the buffer lifetime should be taken into 1278 * consideration. 1279 */ 1280 static inline struct vb2_buffer *vb2_get_buffer(struct vb2_queue *q, 1281 unsigned int index) 1282 { 1283 if (!q->bufs) 1284 return NULL; 1285 1286 if (index >= q->max_num_buffers) 1287 return NULL; 1288 1289 if (test_bit(index, q->bufs_bitmap)) 1290 return q->bufs[index]; 1291 return NULL; 1292 } 1293 1294 /* 1295 * The following functions are not part of the vb2 core API, but are useful 1296 * functions for videobuf2-*. 1297 */ 1298 1299 /** 1300 * vb2_buffer_in_use() - return true if the buffer is in use and 1301 * the queue cannot be freed (by the means of VIDIOC_REQBUFS(0)) call. 1302 * 1303 * @vb: buffer for which plane size should be returned. 1304 * @q: pointer to &struct vb2_queue with videobuf2 queue. 1305 */ 1306 bool vb2_buffer_in_use(struct vb2_queue *q, struct vb2_buffer *vb); 1307 1308 /** 1309 * vb2_verify_memory_type() - Check whether the memory type and buffer type 1310 * passed to a buffer operation are compatible with the queue. 1311 * 1312 * @q: pointer to &struct vb2_queue with videobuf2 queue. 1313 * @memory: memory model, as defined by enum &vb2_memory. 1314 * @type: private buffer type whose content is defined by the vb2-core 1315 * caller. For example, for V4L2, it should match 1316 * the types defined on enum &v4l2_buf_type. 1317 */ 1318 int vb2_verify_memory_type(struct vb2_queue *q, 1319 enum vb2_memory memory, unsigned int type); 1320 1321 /** 1322 * vb2_request_object_is_buffer() - return true if the object is a buffer 1323 * 1324 * @obj: the request object. 1325 */ 1326 bool vb2_request_object_is_buffer(struct media_request_object *obj); 1327 1328 /** 1329 * vb2_request_buffer_cnt() - return the number of buffers in the request 1330 * 1331 * @req: the request. 1332 */ 1333 unsigned int vb2_request_buffer_cnt(struct media_request *req); 1334 1335 #endif /* _MEDIA_VIDEOBUF2_CORE_H */ 1336