1 /* SPDX-License-Identifier: GPL-2.0-or-later */ 2 /* 3 * Memory-to-memory device framework for Video for Linux 2. 4 * 5 * Helper functions for devices that use memory buffers for both source 6 * and destination. 7 * 8 * Copyright (c) 2009 Samsung Electronics Co., Ltd. 9 * Pawel Osciak, <pawel@osciak.com> 10 * Marek Szyprowski, <m.szyprowski@samsung.com> 11 */ 12 13 #ifndef _MEDIA_V4L2_MEM2MEM_H 14 #define _MEDIA_V4L2_MEM2MEM_H 15 16 #include <media/videobuf2-v4l2.h> 17 18 /** 19 * struct v4l2_m2m_ops - mem-to-mem device driver callbacks 20 * @device_run: required. Begin the actual job (transaction) inside this 21 * callback. 22 * The job does NOT have to end before this callback returns 23 * (and it will be the usual case). When the job finishes, 24 * v4l2_m2m_job_finish() or v4l2_m2m_buf_done_and_job_finish() 25 * has to be called. 26 * @job_ready: optional. Should return 0 if the driver does not have a job 27 * fully prepared to run yet (i.e. it will not be able to finish a 28 * transaction without sleeping). If not provided, it will be 29 * assumed that one source and one destination buffer are all 30 * that is required for the driver to perform one full transaction. 31 * This method may not sleep. 32 * @job_abort: optional. Informs the driver that it has to abort the currently 33 * running transaction as soon as possible (i.e. as soon as it can 34 * stop the device safely; e.g. in the next interrupt handler), 35 * even if the transaction would not have been finished by then. 36 * After the driver performs the necessary steps, it has to call 37 * v4l2_m2m_job_finish() or v4l2_m2m_buf_done_and_job_finish() as 38 * if the transaction ended normally. 39 * This function does not have to (and will usually not) wait 40 * until the device enters a state when it can be stopped. 41 */ 42 struct v4l2_m2m_ops { 43 void (*device_run)(void *priv); 44 int (*job_ready)(void *priv); 45 void (*job_abort)(void *priv); 46 }; 47 48 struct video_device; 49 struct v4l2_m2m_dev; 50 51 /** 52 * struct v4l2_m2m_queue_ctx - represents a queue for buffers ready to be 53 * processed 54 * 55 * @q: pointer to struct &vb2_queue 56 * @rdy_queue: List of V4L2 mem-to-mem queues 57 * @rdy_spinlock: spin lock to protect the struct usage 58 * @num_rdy: number of buffers ready to be processed 59 * @buffered: is the queue buffered? 60 * 61 * Queue for buffers ready to be processed as soon as this 62 * instance receives access to the device. 63 */ 64 65 struct v4l2_m2m_queue_ctx { 66 struct vb2_queue q; 67 68 struct list_head rdy_queue; 69 spinlock_t rdy_spinlock; 70 u8 num_rdy; 71 bool buffered; 72 }; 73 74 /** 75 * struct v4l2_m2m_ctx - Memory to memory context structure 76 * 77 * @q_lock: struct &mutex lock 78 * @new_frame: valid in the device_run callback: if true, then this 79 * starts a new frame; if false, then this is a new slice 80 * for an existing frame. This is always true unless 81 * V4L2_BUF_CAP_SUPPORTS_M2M_HOLD_CAPTURE_BUF is set, which 82 * indicates slicing support. 83 * @m2m_dev: opaque pointer to the internal data to handle M2M context 84 * @cap_q_ctx: Capture (output to memory) queue context 85 * @out_q_ctx: Output (input from memory) queue context 86 * @queue: List of memory to memory contexts 87 * @job_flags: Job queue flags, used internally by v4l2-mem2mem.c: 88 * %TRANS_QUEUED, %TRANS_RUNNING and %TRANS_ABORT. 89 * @finished: Wait queue used to signalize when a job queue finished. 90 * @priv: Instance private data 91 * 92 * The memory to memory context is specific to a file handle, NOT to e.g. 93 * a device. 94 */ 95 struct v4l2_m2m_ctx { 96 /* optional cap/out vb2 queues lock */ 97 struct mutex *q_lock; 98 99 bool new_frame; 100 101 /* internal use only */ 102 struct v4l2_m2m_dev *m2m_dev; 103 104 struct v4l2_m2m_queue_ctx cap_q_ctx; 105 106 struct v4l2_m2m_queue_ctx out_q_ctx; 107 108 /* For device job queue */ 109 struct list_head queue; 110 unsigned long job_flags; 111 wait_queue_head_t finished; 112 113 void *priv; 114 }; 115 116 /** 117 * struct v4l2_m2m_buffer - Memory to memory buffer 118 * 119 * @vb: pointer to struct &vb2_v4l2_buffer 120 * @list: list of m2m buffers 121 */ 122 struct v4l2_m2m_buffer { 123 struct vb2_v4l2_buffer vb; 124 struct list_head list; 125 }; 126 127 /** 128 * v4l2_m2m_get_curr_priv() - return driver private data for the currently 129 * running instance or NULL if no instance is running 130 * 131 * @m2m_dev: opaque pointer to the internal data to handle M2M context 132 */ 133 void *v4l2_m2m_get_curr_priv(struct v4l2_m2m_dev *m2m_dev); 134 135 /** 136 * v4l2_m2m_get_vq() - return vb2_queue for the given type 137 * 138 * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx 139 * @type: type of the V4L2 buffer, as defined by enum &v4l2_buf_type 140 */ 141 struct vb2_queue *v4l2_m2m_get_vq(struct v4l2_m2m_ctx *m2m_ctx, 142 enum v4l2_buf_type type); 143 144 /** 145 * v4l2_m2m_try_schedule() - check whether an instance is ready to be added to 146 * the pending job queue and add it if so. 147 * 148 * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx 149 * 150 * There are three basic requirements an instance has to meet to be able to run: 151 * 1) at least one source buffer has to be queued, 152 * 2) at least one destination buffer has to be queued, 153 * 3) streaming has to be on. 154 * 155 * If a queue is buffered (for example a decoder hardware ringbuffer that has 156 * to be drained before doing streamoff), allow scheduling without v4l2 buffers 157 * on that queue. 158 * 159 * There may also be additional, custom requirements. In such case the driver 160 * should supply a custom callback (job_ready in v4l2_m2m_ops) that should 161 * return 1 if the instance is ready. 162 * An example of the above could be an instance that requires more than one 163 * src/dst buffer per transaction. 164 */ 165 void v4l2_m2m_try_schedule(struct v4l2_m2m_ctx *m2m_ctx); 166 167 /** 168 * v4l2_m2m_job_finish() - inform the framework that a job has been finished 169 * and have it clean up 170 * 171 * @m2m_dev: opaque pointer to the internal data to handle M2M context 172 * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx 173 * 174 * Called by a driver to yield back the device after it has finished with it. 175 * Should be called as soon as possible after reaching a state which allows 176 * other instances to take control of the device. 177 * 178 * This function has to be called only after &v4l2_m2m_ops->device_run 179 * callback has been called on the driver. To prevent recursion, it should 180 * not be called directly from the &v4l2_m2m_ops->device_run callback though. 181 */ 182 void v4l2_m2m_job_finish(struct v4l2_m2m_dev *m2m_dev, 183 struct v4l2_m2m_ctx *m2m_ctx); 184 185 /** 186 * v4l2_m2m_buf_done_and_job_finish() - return source/destination buffers with 187 * state and inform the framework that a job has been finished and have it 188 * clean up 189 * 190 * @m2m_dev: opaque pointer to the internal data to handle M2M context 191 * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx 192 * @state: vb2 buffer state passed to v4l2_m2m_buf_done(). 193 * 194 * Drivers that set V4L2_BUF_CAP_SUPPORTS_M2M_HOLD_CAPTURE_BUF must use this 195 * function instead of job_finish() to take held buffers into account. It is 196 * optional for other drivers. 197 * 198 * This function removes the source buffer from the ready list and returns 199 * it with the given state. The same is done for the destination buffer, unless 200 * it is marked 'held'. In that case the buffer is kept on the ready list. 201 * 202 * After that the job is finished (see job_finish()). 203 * 204 * This allows for multiple output buffers to be used to fill in a single 205 * capture buffer. This is typically used by stateless decoders where 206 * multiple e.g. H.264 slices contribute to a single decoded frame. 207 */ 208 void v4l2_m2m_buf_done_and_job_finish(struct v4l2_m2m_dev *m2m_dev, 209 struct v4l2_m2m_ctx *m2m_ctx, 210 enum vb2_buffer_state state); 211 212 static inline void 213 v4l2_m2m_buf_done(struct vb2_v4l2_buffer *buf, enum vb2_buffer_state state) 214 { 215 vb2_buffer_done(&buf->vb2_buf, state); 216 } 217 218 /** 219 * v4l2_m2m_reqbufs() - multi-queue-aware REQBUFS multiplexer 220 * 221 * @file: pointer to struct &file 222 * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx 223 * @reqbufs: pointer to struct &v4l2_requestbuffers 224 */ 225 int v4l2_m2m_reqbufs(struct file *file, struct v4l2_m2m_ctx *m2m_ctx, 226 struct v4l2_requestbuffers *reqbufs); 227 228 /** 229 * v4l2_m2m_querybuf() - multi-queue-aware QUERYBUF multiplexer 230 * 231 * @file: pointer to struct &file 232 * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx 233 * @buf: pointer to struct &v4l2_buffer 234 * 235 * See v4l2_m2m_mmap() documentation for details. 236 */ 237 int v4l2_m2m_querybuf(struct file *file, struct v4l2_m2m_ctx *m2m_ctx, 238 struct v4l2_buffer *buf); 239 240 /** 241 * v4l2_m2m_qbuf() - enqueue a source or destination buffer, depending on 242 * the type 243 * 244 * @file: pointer to struct &file 245 * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx 246 * @buf: pointer to struct &v4l2_buffer 247 */ 248 int v4l2_m2m_qbuf(struct file *file, struct v4l2_m2m_ctx *m2m_ctx, 249 struct v4l2_buffer *buf); 250 251 /** 252 * v4l2_m2m_dqbuf() - dequeue a source or destination buffer, depending on 253 * the type 254 * 255 * @file: pointer to struct &file 256 * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx 257 * @buf: pointer to struct &v4l2_buffer 258 */ 259 int v4l2_m2m_dqbuf(struct file *file, struct v4l2_m2m_ctx *m2m_ctx, 260 struct v4l2_buffer *buf); 261 262 /** 263 * v4l2_m2m_prepare_buf() - prepare a source or destination buffer, depending on 264 * the type 265 * 266 * @file: pointer to struct &file 267 * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx 268 * @buf: pointer to struct &v4l2_buffer 269 */ 270 int v4l2_m2m_prepare_buf(struct file *file, struct v4l2_m2m_ctx *m2m_ctx, 271 struct v4l2_buffer *buf); 272 273 /** 274 * v4l2_m2m_create_bufs() - create a source or destination buffer, depending 275 * on the type 276 * 277 * @file: pointer to struct &file 278 * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx 279 * @create: pointer to struct &v4l2_create_buffers 280 */ 281 int v4l2_m2m_create_bufs(struct file *file, struct v4l2_m2m_ctx *m2m_ctx, 282 struct v4l2_create_buffers *create); 283 284 /** 285 * v4l2_m2m_expbuf() - export a source or destination buffer, depending on 286 * the type 287 * 288 * @file: pointer to struct &file 289 * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx 290 * @eb: pointer to struct &v4l2_exportbuffer 291 */ 292 int v4l2_m2m_expbuf(struct file *file, struct v4l2_m2m_ctx *m2m_ctx, 293 struct v4l2_exportbuffer *eb); 294 295 /** 296 * v4l2_m2m_streamon() - turn on streaming for a video queue 297 * 298 * @file: pointer to struct &file 299 * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx 300 * @type: type of the V4L2 buffer, as defined by enum &v4l2_buf_type 301 */ 302 int v4l2_m2m_streamon(struct file *file, struct v4l2_m2m_ctx *m2m_ctx, 303 enum v4l2_buf_type type); 304 305 /** 306 * v4l2_m2m_streamoff() - turn off streaming for a video queue 307 * 308 * @file: pointer to struct &file 309 * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx 310 * @type: type of the V4L2 buffer, as defined by enum &v4l2_buf_type 311 */ 312 int v4l2_m2m_streamoff(struct file *file, struct v4l2_m2m_ctx *m2m_ctx, 313 enum v4l2_buf_type type); 314 315 /** 316 * v4l2_m2m_poll() - poll replacement, for destination buffers only 317 * 318 * @file: pointer to struct &file 319 * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx 320 * @wait: pointer to struct &poll_table_struct 321 * 322 * Call from the driver's poll() function. Will poll both queues. If a buffer 323 * is available to dequeue (with dqbuf) from the source queue, this will 324 * indicate that a non-blocking write can be performed, while read will be 325 * returned in case of the destination queue. 326 */ 327 __poll_t v4l2_m2m_poll(struct file *file, struct v4l2_m2m_ctx *m2m_ctx, 328 struct poll_table_struct *wait); 329 330 /** 331 * v4l2_m2m_mmap() - source and destination queues-aware mmap multiplexer 332 * 333 * @file: pointer to struct &file 334 * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx 335 * @vma: pointer to struct &vm_area_struct 336 * 337 * Call from driver's mmap() function. Will handle mmap() for both queues 338 * seamlessly for videobuffer, which will receive normal per-queue offsets and 339 * proper videobuf queue pointers. The differentiation is made outside videobuf 340 * by adding a predefined offset to buffers from one of the queues and 341 * subtracting it before passing it back to videobuf. Only drivers (and 342 * thus applications) receive modified offsets. 343 */ 344 int v4l2_m2m_mmap(struct file *file, struct v4l2_m2m_ctx *m2m_ctx, 345 struct vm_area_struct *vma); 346 347 /** 348 * v4l2_m2m_init() - initialize per-driver m2m data 349 * 350 * @m2m_ops: pointer to struct v4l2_m2m_ops 351 * 352 * Usually called from driver's ``probe()`` function. 353 * 354 * Return: returns an opaque pointer to the internal data to handle M2M context 355 */ 356 struct v4l2_m2m_dev *v4l2_m2m_init(const struct v4l2_m2m_ops *m2m_ops); 357 358 #if defined(CONFIG_MEDIA_CONTROLLER) 359 void v4l2_m2m_unregister_media_controller(struct v4l2_m2m_dev *m2m_dev); 360 int v4l2_m2m_register_media_controller(struct v4l2_m2m_dev *m2m_dev, 361 struct video_device *vdev, int function); 362 #else 363 static inline void 364 v4l2_m2m_unregister_media_controller(struct v4l2_m2m_dev *m2m_dev) 365 { 366 } 367 368 static inline int 369 v4l2_m2m_register_media_controller(struct v4l2_m2m_dev *m2m_dev, 370 struct video_device *vdev, int function) 371 { 372 return 0; 373 } 374 #endif 375 376 /** 377 * v4l2_m2m_release() - cleans up and frees a m2m_dev structure 378 * 379 * @m2m_dev: opaque pointer to the internal data to handle M2M context 380 * 381 * Usually called from driver's ``remove()`` function. 382 */ 383 void v4l2_m2m_release(struct v4l2_m2m_dev *m2m_dev); 384 385 /** 386 * v4l2_m2m_ctx_init() - allocate and initialize a m2m context 387 * 388 * @m2m_dev: opaque pointer to the internal data to handle M2M context 389 * @drv_priv: driver's instance private data 390 * @queue_init: a callback for queue type-specific initialization function 391 * to be used for initializing videobuf_queues 392 * 393 * Usually called from driver's ``open()`` function. 394 */ 395 struct v4l2_m2m_ctx *v4l2_m2m_ctx_init(struct v4l2_m2m_dev *m2m_dev, 396 void *drv_priv, 397 int (*queue_init)(void *priv, struct vb2_queue *src_vq, struct vb2_queue *dst_vq)); 398 399 static inline void v4l2_m2m_set_src_buffered(struct v4l2_m2m_ctx *m2m_ctx, 400 bool buffered) 401 { 402 m2m_ctx->out_q_ctx.buffered = buffered; 403 } 404 405 static inline void v4l2_m2m_set_dst_buffered(struct v4l2_m2m_ctx *m2m_ctx, 406 bool buffered) 407 { 408 m2m_ctx->cap_q_ctx.buffered = buffered; 409 } 410 411 /** 412 * v4l2_m2m_ctx_release() - release m2m context 413 * 414 * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx 415 * 416 * Usually called from driver's release() function. 417 */ 418 void v4l2_m2m_ctx_release(struct v4l2_m2m_ctx *m2m_ctx); 419 420 /** 421 * v4l2_m2m_buf_queue() - add a buffer to the proper ready buffers list. 422 * 423 * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx 424 * @vbuf: pointer to struct &vb2_v4l2_buffer 425 * 426 * Call from videobuf_queue_ops->ops->buf_queue, videobuf_queue_ops callback. 427 */ 428 void v4l2_m2m_buf_queue(struct v4l2_m2m_ctx *m2m_ctx, 429 struct vb2_v4l2_buffer *vbuf); 430 431 /** 432 * v4l2_m2m_num_src_bufs_ready() - return the number of source buffers ready for 433 * use 434 * 435 * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx 436 */ 437 static inline 438 unsigned int v4l2_m2m_num_src_bufs_ready(struct v4l2_m2m_ctx *m2m_ctx) 439 { 440 return m2m_ctx->out_q_ctx.num_rdy; 441 } 442 443 /** 444 * v4l2_m2m_num_dst_bufs_ready() - return the number of destination buffers 445 * ready for use 446 * 447 * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx 448 */ 449 static inline 450 unsigned int v4l2_m2m_num_dst_bufs_ready(struct v4l2_m2m_ctx *m2m_ctx) 451 { 452 return m2m_ctx->cap_q_ctx.num_rdy; 453 } 454 455 /** 456 * v4l2_m2m_next_buf() - return next buffer from the list of ready buffers 457 * 458 * @q_ctx: pointer to struct @v4l2_m2m_queue_ctx 459 */ 460 struct vb2_v4l2_buffer *v4l2_m2m_next_buf(struct v4l2_m2m_queue_ctx *q_ctx); 461 462 /** 463 * v4l2_m2m_next_src_buf() - return next source buffer from the list of ready 464 * buffers 465 * 466 * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx 467 */ 468 static inline struct vb2_v4l2_buffer * 469 v4l2_m2m_next_src_buf(struct v4l2_m2m_ctx *m2m_ctx) 470 { 471 return v4l2_m2m_next_buf(&m2m_ctx->out_q_ctx); 472 } 473 474 /** 475 * v4l2_m2m_next_dst_buf() - return next destination buffer from the list of 476 * ready buffers 477 * 478 * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx 479 */ 480 static inline struct vb2_v4l2_buffer * 481 v4l2_m2m_next_dst_buf(struct v4l2_m2m_ctx *m2m_ctx) 482 { 483 return v4l2_m2m_next_buf(&m2m_ctx->cap_q_ctx); 484 } 485 486 /** 487 * v4l2_m2m_last_buf() - return last buffer from the list of ready buffers 488 * 489 * @q_ctx: pointer to struct @v4l2_m2m_queue_ctx 490 */ 491 struct vb2_v4l2_buffer *v4l2_m2m_last_buf(struct v4l2_m2m_queue_ctx *q_ctx); 492 493 /** 494 * v4l2_m2m_last_src_buf() - return last destination buffer from the list of 495 * ready buffers 496 * 497 * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx 498 */ 499 static inline struct vb2_v4l2_buffer * 500 v4l2_m2m_last_src_buf(struct v4l2_m2m_ctx *m2m_ctx) 501 { 502 return v4l2_m2m_last_buf(&m2m_ctx->out_q_ctx); 503 } 504 505 /** 506 * v4l2_m2m_last_dst_buf() - return last destination buffer from the list of 507 * ready buffers 508 * 509 * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx 510 */ 511 static inline struct vb2_v4l2_buffer * 512 v4l2_m2m_last_dst_buf(struct v4l2_m2m_ctx *m2m_ctx) 513 { 514 return v4l2_m2m_last_buf(&m2m_ctx->cap_q_ctx); 515 } 516 517 /** 518 * v4l2_m2m_for_each_dst_buf() - iterate over a list of destination ready 519 * buffers 520 * 521 * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx 522 * @b: current buffer of type struct v4l2_m2m_buffer 523 */ 524 #define v4l2_m2m_for_each_dst_buf(m2m_ctx, b) \ 525 list_for_each_entry(b, &m2m_ctx->cap_q_ctx.rdy_queue, list) 526 527 /** 528 * v4l2_m2m_for_each_src_buf() - iterate over a list of source ready buffers 529 * 530 * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx 531 * @b: current buffer of type struct v4l2_m2m_buffer 532 */ 533 #define v4l2_m2m_for_each_src_buf(m2m_ctx, b) \ 534 list_for_each_entry(b, &m2m_ctx->out_q_ctx.rdy_queue, list) 535 536 /** 537 * v4l2_m2m_for_each_dst_buf_safe() - iterate over a list of destination ready 538 * buffers safely 539 * 540 * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx 541 * @b: current buffer of type struct v4l2_m2m_buffer 542 * @n: used as temporary storage 543 */ 544 #define v4l2_m2m_for_each_dst_buf_safe(m2m_ctx, b, n) \ 545 list_for_each_entry_safe(b, n, &m2m_ctx->cap_q_ctx.rdy_queue, list) 546 547 /** 548 * v4l2_m2m_for_each_src_buf_safe() - iterate over a list of source ready 549 * buffers safely 550 * 551 * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx 552 * @b: current buffer of type struct v4l2_m2m_buffer 553 * @n: used as temporary storage 554 */ 555 #define v4l2_m2m_for_each_src_buf_safe(m2m_ctx, b, n) \ 556 list_for_each_entry_safe(b, n, &m2m_ctx->out_q_ctx.rdy_queue, list) 557 558 /** 559 * v4l2_m2m_get_src_vq() - return vb2_queue for source buffers 560 * 561 * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx 562 */ 563 static inline 564 struct vb2_queue *v4l2_m2m_get_src_vq(struct v4l2_m2m_ctx *m2m_ctx) 565 { 566 return &m2m_ctx->out_q_ctx.q; 567 } 568 569 /** 570 * v4l2_m2m_get_dst_vq() - return vb2_queue for destination buffers 571 * 572 * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx 573 */ 574 static inline 575 struct vb2_queue *v4l2_m2m_get_dst_vq(struct v4l2_m2m_ctx *m2m_ctx) 576 { 577 return &m2m_ctx->cap_q_ctx.q; 578 } 579 580 /** 581 * v4l2_m2m_buf_remove() - take off a buffer from the list of ready buffers and 582 * return it 583 * 584 * @q_ctx: pointer to struct @v4l2_m2m_queue_ctx 585 */ 586 struct vb2_v4l2_buffer *v4l2_m2m_buf_remove(struct v4l2_m2m_queue_ctx *q_ctx); 587 588 /** 589 * v4l2_m2m_src_buf_remove() - take off a source buffer from the list of ready 590 * buffers and return it 591 * 592 * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx 593 */ 594 static inline struct vb2_v4l2_buffer * 595 v4l2_m2m_src_buf_remove(struct v4l2_m2m_ctx *m2m_ctx) 596 { 597 return v4l2_m2m_buf_remove(&m2m_ctx->out_q_ctx); 598 } 599 600 /** 601 * v4l2_m2m_dst_buf_remove() - take off a destination buffer from the list of 602 * ready buffers and return it 603 * 604 * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx 605 */ 606 static inline struct vb2_v4l2_buffer * 607 v4l2_m2m_dst_buf_remove(struct v4l2_m2m_ctx *m2m_ctx) 608 { 609 return v4l2_m2m_buf_remove(&m2m_ctx->cap_q_ctx); 610 } 611 612 /** 613 * v4l2_m2m_buf_remove_by_buf() - take off exact buffer from the list of ready 614 * buffers 615 * 616 * @q_ctx: pointer to struct @v4l2_m2m_queue_ctx 617 * @vbuf: the buffer to be removed 618 */ 619 void v4l2_m2m_buf_remove_by_buf(struct v4l2_m2m_queue_ctx *q_ctx, 620 struct vb2_v4l2_buffer *vbuf); 621 622 /** 623 * v4l2_m2m_src_buf_remove_by_buf() - take off exact source buffer from the list 624 * of ready buffers 625 * 626 * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx 627 * @vbuf: the buffer to be removed 628 */ 629 static inline void v4l2_m2m_src_buf_remove_by_buf(struct v4l2_m2m_ctx *m2m_ctx, 630 struct vb2_v4l2_buffer *vbuf) 631 { 632 v4l2_m2m_buf_remove_by_buf(&m2m_ctx->out_q_ctx, vbuf); 633 } 634 635 /** 636 * v4l2_m2m_dst_buf_remove_by_buf() - take off exact destination buffer from the 637 * list of ready buffers 638 * 639 * @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx 640 * @vbuf: the buffer to be removed 641 */ 642 static inline void v4l2_m2m_dst_buf_remove_by_buf(struct v4l2_m2m_ctx *m2m_ctx, 643 struct vb2_v4l2_buffer *vbuf) 644 { 645 v4l2_m2m_buf_remove_by_buf(&m2m_ctx->cap_q_ctx, vbuf); 646 } 647 648 struct vb2_v4l2_buffer * 649 v4l2_m2m_buf_remove_by_idx(struct v4l2_m2m_queue_ctx *q_ctx, unsigned int idx); 650 651 static inline struct vb2_v4l2_buffer * 652 v4l2_m2m_src_buf_remove_by_idx(struct v4l2_m2m_ctx *m2m_ctx, unsigned int idx) 653 { 654 return v4l2_m2m_buf_remove_by_idx(&m2m_ctx->out_q_ctx, idx); 655 } 656 657 static inline struct vb2_v4l2_buffer * 658 v4l2_m2m_dst_buf_remove_by_idx(struct v4l2_m2m_ctx *m2m_ctx, unsigned int idx) 659 { 660 return v4l2_m2m_buf_remove_by_idx(&m2m_ctx->cap_q_ctx, idx); 661 } 662 663 /** 664 * v4l2_m2m_buf_copy_metadata() - copy buffer metadata from 665 * the output buffer to the capture buffer 666 * 667 * @out_vb: the output buffer that is the source of the metadata. 668 * @cap_vb: the capture buffer that will receive the metadata. 669 * @copy_frame_flags: copy the KEY/B/PFRAME flags as well. 670 * 671 * This helper function copies the timestamp, timecode (if the TIMECODE 672 * buffer flag was set), field and the TIMECODE, KEYFRAME, BFRAME, PFRAME 673 * and TSTAMP_SRC_MASK flags from @out_vb to @cap_vb. 674 * 675 * If @copy_frame_flags is false, then the KEYFRAME, BFRAME and PFRAME 676 * flags are not copied. This is typically needed for encoders that 677 * set this bits explicitly. 678 */ 679 void v4l2_m2m_buf_copy_metadata(const struct vb2_v4l2_buffer *out_vb, 680 struct vb2_v4l2_buffer *cap_vb, 681 bool copy_frame_flags); 682 683 /* v4l2 request helper */ 684 685 void v4l2_m2m_request_queue(struct media_request *req); 686 687 /* v4l2 ioctl helpers */ 688 689 int v4l2_m2m_ioctl_reqbufs(struct file *file, void *priv, 690 struct v4l2_requestbuffers *rb); 691 int v4l2_m2m_ioctl_create_bufs(struct file *file, void *fh, 692 struct v4l2_create_buffers *create); 693 int v4l2_m2m_ioctl_querybuf(struct file *file, void *fh, 694 struct v4l2_buffer *buf); 695 int v4l2_m2m_ioctl_expbuf(struct file *file, void *fh, 696 struct v4l2_exportbuffer *eb); 697 int v4l2_m2m_ioctl_qbuf(struct file *file, void *fh, 698 struct v4l2_buffer *buf); 699 int v4l2_m2m_ioctl_dqbuf(struct file *file, void *fh, 700 struct v4l2_buffer *buf); 701 int v4l2_m2m_ioctl_prepare_buf(struct file *file, void *fh, 702 struct v4l2_buffer *buf); 703 int v4l2_m2m_ioctl_streamon(struct file *file, void *fh, 704 enum v4l2_buf_type type); 705 int v4l2_m2m_ioctl_streamoff(struct file *file, void *fh, 706 enum v4l2_buf_type type); 707 int v4l2_m2m_ioctl_try_encoder_cmd(struct file *file, void *fh, 708 struct v4l2_encoder_cmd *ec); 709 int v4l2_m2m_ioctl_try_decoder_cmd(struct file *file, void *fh, 710 struct v4l2_decoder_cmd *dc); 711 int v4l2_m2m_ioctl_stateless_try_decoder_cmd(struct file *file, void *fh, 712 struct v4l2_decoder_cmd *dc); 713 int v4l2_m2m_ioctl_stateless_decoder_cmd(struct file *file, void *priv, 714 struct v4l2_decoder_cmd *dc); 715 int v4l2_m2m_fop_mmap(struct file *file, struct vm_area_struct *vma); 716 __poll_t v4l2_m2m_fop_poll(struct file *file, poll_table *wait); 717 718 #endif /* _MEDIA_V4L2_MEM2MEM_H */ 719 720