1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Memory-to-memory device framework for Video for Linux 2 and vb2. 4 * 5 * Helper functions for devices that use vb2 buffers for both their 6 * source and destination. 7 * 8 * Copyright (c) 2009-2010 Samsung Electronics Co., Ltd. 9 * Pawel Osciak, <pawel@osciak.com> 10 * Marek Szyprowski, <m.szyprowski@samsung.com> 11 */ 12 #include <linux/module.h> 13 #include <linux/sched.h> 14 #include <linux/slab.h> 15 16 #include <media/media-device.h> 17 #include <media/videobuf2-v4l2.h> 18 #include <media/v4l2-mem2mem.h> 19 #include <media/v4l2-dev.h> 20 #include <media/v4l2-device.h> 21 #include <media/v4l2-fh.h> 22 #include <media/v4l2-event.h> 23 24 MODULE_DESCRIPTION("Mem to mem device framework for vb2"); 25 MODULE_AUTHOR("Pawel Osciak, <pawel@osciak.com>"); 26 MODULE_LICENSE("GPL"); 27 28 static bool debug; 29 module_param(debug, bool, 0644); 30 31 #define dprintk(fmt, arg...) \ 32 do { \ 33 if (debug) \ 34 printk(KERN_DEBUG "%s: " fmt, __func__, ## arg);\ 35 } while (0) 36 37 38 /* Instance is already queued on the job_queue */ 39 #define TRANS_QUEUED (1 << 0) 40 /* Instance is currently running in hardware */ 41 #define TRANS_RUNNING (1 << 1) 42 /* Instance is currently aborting */ 43 #define TRANS_ABORT (1 << 2) 44 45 46 /* The job queue is not running new jobs */ 47 #define QUEUE_PAUSED (1 << 0) 48 49 50 /* Offset base for buffers on the destination queue - used to distinguish 51 * between source and destination buffers when mmapping - they receive the same 52 * offsets but for different queues */ 53 #define DST_QUEUE_OFF_BASE (1 << 30) 54 55 enum v4l2_m2m_entity_type { 56 MEM2MEM_ENT_TYPE_SOURCE, 57 MEM2MEM_ENT_TYPE_SINK, 58 MEM2MEM_ENT_TYPE_PROC 59 }; 60 61 static const char * const m2m_entity_name[] = { 62 "source", 63 "sink", 64 "proc" 65 }; 66 67 /** 68 * struct v4l2_m2m_dev - per-device context 69 * @source: &struct media_entity pointer with the source entity 70 * Used only when the M2M device is registered via 71 * v4l2_m2m_register_media_controller(). 72 * @source_pad: &struct media_pad with the source pad. 73 * Used only when the M2M device is registered via 74 * v4l2_m2m_register_media_controller(). 75 * @sink: &struct media_entity pointer with the sink entity 76 * Used only when the M2M device is registered via 77 * v4l2_m2m_register_media_controller(). 78 * @sink_pad: &struct media_pad with the sink pad. 79 * Used only when the M2M device is registered via 80 * v4l2_m2m_register_media_controller(). 81 * @proc: &struct media_entity pointer with the M2M device itself. 82 * @proc_pads: &struct media_pad with the @proc pads. 83 * Used only when the M2M device is registered via 84 * v4l2_m2m_unregister_media_controller(). 85 * @intf_devnode: &struct media_intf devnode pointer with the interface 86 * with controls the M2M device. 87 * @curr_ctx: currently running instance 88 * @job_queue: instances queued to run 89 * @job_spinlock: protects job_queue 90 * @job_work: worker to run queued jobs. 91 * @job_queue_flags: flags of the queue status, %QUEUE_PAUSED. 92 * @m2m_ops: driver callbacks 93 */ 94 struct v4l2_m2m_dev { 95 struct v4l2_m2m_ctx *curr_ctx; 96 #ifdef CONFIG_MEDIA_CONTROLLER 97 struct media_entity *source; 98 struct media_pad source_pad; 99 struct media_entity sink; 100 struct media_pad sink_pad; 101 struct media_entity proc; 102 struct media_pad proc_pads[2]; 103 struct media_intf_devnode *intf_devnode; 104 #endif 105 106 struct list_head job_queue; 107 spinlock_t job_spinlock; 108 struct work_struct job_work; 109 unsigned long job_queue_flags; 110 111 const struct v4l2_m2m_ops *m2m_ops; 112 }; 113 114 static struct v4l2_m2m_queue_ctx *get_queue_ctx(struct v4l2_m2m_ctx *m2m_ctx, 115 enum v4l2_buf_type type) 116 { 117 if (V4L2_TYPE_IS_OUTPUT(type)) 118 return &m2m_ctx->out_q_ctx; 119 else 120 return &m2m_ctx->cap_q_ctx; 121 } 122 123 struct vb2_queue *v4l2_m2m_get_vq(struct v4l2_m2m_ctx *m2m_ctx, 124 enum v4l2_buf_type type) 125 { 126 return &get_queue_ctx(m2m_ctx, type)->q; 127 } 128 EXPORT_SYMBOL(v4l2_m2m_get_vq); 129 130 struct vb2_v4l2_buffer *v4l2_m2m_next_buf(struct v4l2_m2m_queue_ctx *q_ctx) 131 { 132 struct v4l2_m2m_buffer *b; 133 unsigned long flags; 134 135 spin_lock_irqsave(&q_ctx->rdy_spinlock, flags); 136 137 if (list_empty(&q_ctx->rdy_queue)) { 138 spin_unlock_irqrestore(&q_ctx->rdy_spinlock, flags); 139 return NULL; 140 } 141 142 b = list_first_entry(&q_ctx->rdy_queue, struct v4l2_m2m_buffer, list); 143 spin_unlock_irqrestore(&q_ctx->rdy_spinlock, flags); 144 return &b->vb; 145 } 146 EXPORT_SYMBOL_GPL(v4l2_m2m_next_buf); 147 148 struct vb2_v4l2_buffer *v4l2_m2m_last_buf(struct v4l2_m2m_queue_ctx *q_ctx) 149 { 150 struct v4l2_m2m_buffer *b; 151 unsigned long flags; 152 153 spin_lock_irqsave(&q_ctx->rdy_spinlock, flags); 154 155 if (list_empty(&q_ctx->rdy_queue)) { 156 spin_unlock_irqrestore(&q_ctx->rdy_spinlock, flags); 157 return NULL; 158 } 159 160 b = list_last_entry(&q_ctx->rdy_queue, struct v4l2_m2m_buffer, list); 161 spin_unlock_irqrestore(&q_ctx->rdy_spinlock, flags); 162 return &b->vb; 163 } 164 EXPORT_SYMBOL_GPL(v4l2_m2m_last_buf); 165 166 struct vb2_v4l2_buffer *v4l2_m2m_buf_remove(struct v4l2_m2m_queue_ctx *q_ctx) 167 { 168 struct v4l2_m2m_buffer *b; 169 unsigned long flags; 170 171 spin_lock_irqsave(&q_ctx->rdy_spinlock, flags); 172 if (list_empty(&q_ctx->rdy_queue)) { 173 spin_unlock_irqrestore(&q_ctx->rdy_spinlock, flags); 174 return NULL; 175 } 176 b = list_first_entry(&q_ctx->rdy_queue, struct v4l2_m2m_buffer, list); 177 list_del(&b->list); 178 q_ctx->num_rdy--; 179 spin_unlock_irqrestore(&q_ctx->rdy_spinlock, flags); 180 181 return &b->vb; 182 } 183 EXPORT_SYMBOL_GPL(v4l2_m2m_buf_remove); 184 185 void v4l2_m2m_buf_remove_by_buf(struct v4l2_m2m_queue_ctx *q_ctx, 186 struct vb2_v4l2_buffer *vbuf) 187 { 188 struct v4l2_m2m_buffer *b; 189 unsigned long flags; 190 191 spin_lock_irqsave(&q_ctx->rdy_spinlock, flags); 192 b = container_of(vbuf, struct v4l2_m2m_buffer, vb); 193 list_del(&b->list); 194 q_ctx->num_rdy--; 195 spin_unlock_irqrestore(&q_ctx->rdy_spinlock, flags); 196 } 197 EXPORT_SYMBOL_GPL(v4l2_m2m_buf_remove_by_buf); 198 199 struct vb2_v4l2_buffer * 200 v4l2_m2m_buf_remove_by_idx(struct v4l2_m2m_queue_ctx *q_ctx, unsigned int idx) 201 202 { 203 struct v4l2_m2m_buffer *b, *tmp; 204 struct vb2_v4l2_buffer *ret = NULL; 205 unsigned long flags; 206 207 spin_lock_irqsave(&q_ctx->rdy_spinlock, flags); 208 list_for_each_entry_safe(b, tmp, &q_ctx->rdy_queue, list) { 209 if (b->vb.vb2_buf.index == idx) { 210 list_del(&b->list); 211 q_ctx->num_rdy--; 212 ret = &b->vb; 213 break; 214 } 215 } 216 spin_unlock_irqrestore(&q_ctx->rdy_spinlock, flags); 217 218 return ret; 219 } 220 EXPORT_SYMBOL_GPL(v4l2_m2m_buf_remove_by_idx); 221 222 /* 223 * Scheduling handlers 224 */ 225 226 void *v4l2_m2m_get_curr_priv(struct v4l2_m2m_dev *m2m_dev) 227 { 228 unsigned long flags; 229 void *ret = NULL; 230 231 spin_lock_irqsave(&m2m_dev->job_spinlock, flags); 232 if (m2m_dev->curr_ctx) 233 ret = m2m_dev->curr_ctx->priv; 234 spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags); 235 236 return ret; 237 } 238 EXPORT_SYMBOL(v4l2_m2m_get_curr_priv); 239 240 /** 241 * v4l2_m2m_try_run() - select next job to perform and run it if possible 242 * @m2m_dev: per-device context 243 * 244 * Get next transaction (if present) from the waiting jobs list and run it. 245 * 246 * Note that this function can run on a given v4l2_m2m_ctx context, 247 * but call .device_run for another context. 248 */ 249 static void v4l2_m2m_try_run(struct v4l2_m2m_dev *m2m_dev) 250 { 251 unsigned long flags; 252 253 spin_lock_irqsave(&m2m_dev->job_spinlock, flags); 254 if (NULL != m2m_dev->curr_ctx) { 255 spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags); 256 dprintk("Another instance is running, won't run now\n"); 257 return; 258 } 259 260 if (list_empty(&m2m_dev->job_queue)) { 261 spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags); 262 dprintk("No job pending\n"); 263 return; 264 } 265 266 if (m2m_dev->job_queue_flags & QUEUE_PAUSED) { 267 spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags); 268 dprintk("Running new jobs is paused\n"); 269 return; 270 } 271 272 m2m_dev->curr_ctx = list_first_entry(&m2m_dev->job_queue, 273 struct v4l2_m2m_ctx, queue); 274 m2m_dev->curr_ctx->job_flags |= TRANS_RUNNING; 275 spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags); 276 277 dprintk("Running job on m2m_ctx: %p\n", m2m_dev->curr_ctx); 278 m2m_dev->m2m_ops->device_run(m2m_dev->curr_ctx->priv); 279 } 280 281 /* 282 * __v4l2_m2m_try_queue() - queue a job 283 * @m2m_dev: m2m device 284 * @m2m_ctx: m2m context 285 * 286 * Check if this context is ready to queue a job. 287 * 288 * This function can run in interrupt context. 289 */ 290 static void __v4l2_m2m_try_queue(struct v4l2_m2m_dev *m2m_dev, 291 struct v4l2_m2m_ctx *m2m_ctx) 292 { 293 unsigned long flags_job; 294 struct vb2_v4l2_buffer *dst, *src; 295 296 dprintk("Trying to schedule a job for m2m_ctx: %p\n", m2m_ctx); 297 298 if (!m2m_ctx->out_q_ctx.q.streaming || 299 (!m2m_ctx->cap_q_ctx.q.streaming && !m2m_ctx->ignore_cap_streaming)) { 300 if (!m2m_ctx->ignore_cap_streaming) 301 dprintk("Streaming needs to be on for both queues\n"); 302 else 303 dprintk("Streaming needs to be on for the OUTPUT queue\n"); 304 return; 305 } 306 307 spin_lock_irqsave(&m2m_dev->job_spinlock, flags_job); 308 309 /* If the context is aborted then don't schedule it */ 310 if (m2m_ctx->job_flags & TRANS_ABORT) { 311 dprintk("Aborted context\n"); 312 goto job_unlock; 313 } 314 315 if (m2m_ctx->job_flags & TRANS_QUEUED) { 316 dprintk("On job queue already\n"); 317 goto job_unlock; 318 } 319 320 src = v4l2_m2m_next_src_buf(m2m_ctx); 321 dst = v4l2_m2m_next_dst_buf(m2m_ctx); 322 if (!src && !m2m_ctx->out_q_ctx.buffered) { 323 dprintk("No input buffers available\n"); 324 goto job_unlock; 325 } 326 if (!dst && !m2m_ctx->cap_q_ctx.buffered) { 327 dprintk("No output buffers available\n"); 328 goto job_unlock; 329 } 330 331 m2m_ctx->new_frame = true; 332 333 if (src && dst && dst->is_held && 334 dst->vb2_buf.copied_timestamp && 335 dst->vb2_buf.timestamp != src->vb2_buf.timestamp) { 336 dprintk("Timestamp mismatch, returning held capture buffer\n"); 337 dst->is_held = false; 338 v4l2_m2m_dst_buf_remove(m2m_ctx); 339 v4l2_m2m_buf_done(dst, VB2_BUF_STATE_DONE); 340 dst = v4l2_m2m_next_dst_buf(m2m_ctx); 341 342 if (!dst && !m2m_ctx->cap_q_ctx.buffered) { 343 dprintk("No output buffers available after returning held buffer\n"); 344 goto job_unlock; 345 } 346 } 347 348 if (src && dst && (m2m_ctx->out_q_ctx.q.subsystem_flags & 349 VB2_V4L2_FL_SUPPORTS_M2M_HOLD_CAPTURE_BUF)) 350 m2m_ctx->new_frame = !dst->vb2_buf.copied_timestamp || 351 dst->vb2_buf.timestamp != src->vb2_buf.timestamp; 352 353 if (m2m_ctx->has_stopped) { 354 dprintk("Device has stopped\n"); 355 goto job_unlock; 356 } 357 358 if (m2m_dev->m2m_ops->job_ready 359 && (!m2m_dev->m2m_ops->job_ready(m2m_ctx->priv))) { 360 dprintk("Driver not ready\n"); 361 goto job_unlock; 362 } 363 364 list_add_tail(&m2m_ctx->queue, &m2m_dev->job_queue); 365 m2m_ctx->job_flags |= TRANS_QUEUED; 366 367 job_unlock: 368 spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags_job); 369 } 370 371 /** 372 * v4l2_m2m_try_schedule() - schedule and possibly run a job for any context 373 * @m2m_ctx: m2m context 374 * 375 * Check if this context is ready to queue a job. If suitable, 376 * run the next queued job on the mem2mem device. 377 * 378 * This function shouldn't run in interrupt context. 379 * 380 * Note that v4l2_m2m_try_schedule() can schedule one job for this context, 381 * and then run another job for another context. 382 */ 383 void v4l2_m2m_try_schedule(struct v4l2_m2m_ctx *m2m_ctx) 384 { 385 struct v4l2_m2m_dev *m2m_dev = m2m_ctx->m2m_dev; 386 387 __v4l2_m2m_try_queue(m2m_dev, m2m_ctx); 388 v4l2_m2m_try_run(m2m_dev); 389 } 390 EXPORT_SYMBOL_GPL(v4l2_m2m_try_schedule); 391 392 /** 393 * v4l2_m2m_device_run_work() - run pending jobs for the context 394 * @work: Work structure used for scheduling the execution of this function. 395 */ 396 static void v4l2_m2m_device_run_work(struct work_struct *work) 397 { 398 struct v4l2_m2m_dev *m2m_dev = 399 container_of(work, struct v4l2_m2m_dev, job_work); 400 401 v4l2_m2m_try_run(m2m_dev); 402 } 403 404 /** 405 * v4l2_m2m_cancel_job() - cancel pending jobs for the context 406 * @m2m_ctx: m2m context with jobs to be canceled 407 * 408 * In case of streamoff or release called on any context, 409 * 1] If the context is currently running, then abort job will be called 410 * 2] If the context is queued, then the context will be removed from 411 * the job_queue 412 */ 413 static void v4l2_m2m_cancel_job(struct v4l2_m2m_ctx *m2m_ctx) 414 { 415 struct v4l2_m2m_dev *m2m_dev; 416 unsigned long flags; 417 418 m2m_dev = m2m_ctx->m2m_dev; 419 spin_lock_irqsave(&m2m_dev->job_spinlock, flags); 420 421 m2m_ctx->job_flags |= TRANS_ABORT; 422 if (m2m_ctx->job_flags & TRANS_RUNNING) { 423 spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags); 424 if (m2m_dev->m2m_ops->job_abort) 425 m2m_dev->m2m_ops->job_abort(m2m_ctx->priv); 426 dprintk("m2m_ctx %p running, will wait to complete\n", m2m_ctx); 427 wait_event(m2m_ctx->finished, 428 !(m2m_ctx->job_flags & TRANS_RUNNING)); 429 } else if (m2m_ctx->job_flags & TRANS_QUEUED) { 430 list_del(&m2m_ctx->queue); 431 m2m_ctx->job_flags &= ~(TRANS_QUEUED | TRANS_RUNNING); 432 spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags); 433 dprintk("m2m_ctx: %p had been on queue and was removed\n", 434 m2m_ctx); 435 } else { 436 /* Do nothing, was not on queue/running */ 437 spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags); 438 } 439 } 440 441 /* 442 * Schedule the next job, called from v4l2_m2m_job_finish() or 443 * v4l2_m2m_buf_done_and_job_finish(). 444 */ 445 static void v4l2_m2m_schedule_next_job(struct v4l2_m2m_dev *m2m_dev, 446 struct v4l2_m2m_ctx *m2m_ctx) 447 { 448 /* 449 * This instance might have more buffers ready, but since we do not 450 * allow more than one job on the job_queue per instance, each has 451 * to be scheduled separately after the previous one finishes. 452 */ 453 __v4l2_m2m_try_queue(m2m_dev, m2m_ctx); 454 455 /* 456 * We might be running in atomic context, 457 * but the job must be run in non-atomic context. 458 */ 459 schedule_work(&m2m_dev->job_work); 460 } 461 462 /* 463 * Assumes job_spinlock is held, called from v4l2_m2m_job_finish() or 464 * v4l2_m2m_buf_done_and_job_finish(). 465 */ 466 static bool _v4l2_m2m_job_finish(struct v4l2_m2m_dev *m2m_dev, 467 struct v4l2_m2m_ctx *m2m_ctx) 468 { 469 if (!m2m_dev->curr_ctx || m2m_dev->curr_ctx != m2m_ctx) { 470 dprintk("Called by an instance not currently running\n"); 471 return false; 472 } 473 474 list_del(&m2m_dev->curr_ctx->queue); 475 m2m_dev->curr_ctx->job_flags &= ~(TRANS_QUEUED | TRANS_RUNNING); 476 wake_up(&m2m_dev->curr_ctx->finished); 477 m2m_dev->curr_ctx = NULL; 478 return true; 479 } 480 481 void v4l2_m2m_job_finish(struct v4l2_m2m_dev *m2m_dev, 482 struct v4l2_m2m_ctx *m2m_ctx) 483 { 484 unsigned long flags; 485 bool schedule_next; 486 487 /* 488 * This function should not be used for drivers that support 489 * holding capture buffers. Those should use 490 * v4l2_m2m_buf_done_and_job_finish() instead. 491 */ 492 WARN_ON(m2m_ctx->out_q_ctx.q.subsystem_flags & 493 VB2_V4L2_FL_SUPPORTS_M2M_HOLD_CAPTURE_BUF); 494 spin_lock_irqsave(&m2m_dev->job_spinlock, flags); 495 schedule_next = _v4l2_m2m_job_finish(m2m_dev, m2m_ctx); 496 spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags); 497 498 if (schedule_next) 499 v4l2_m2m_schedule_next_job(m2m_dev, m2m_ctx); 500 } 501 EXPORT_SYMBOL(v4l2_m2m_job_finish); 502 503 void v4l2_m2m_buf_done_and_job_finish(struct v4l2_m2m_dev *m2m_dev, 504 struct v4l2_m2m_ctx *m2m_ctx, 505 enum vb2_buffer_state state) 506 { 507 struct vb2_v4l2_buffer *src_buf, *dst_buf; 508 bool schedule_next = false; 509 unsigned long flags; 510 511 spin_lock_irqsave(&m2m_dev->job_spinlock, flags); 512 src_buf = v4l2_m2m_src_buf_remove(m2m_ctx); 513 dst_buf = v4l2_m2m_next_dst_buf(m2m_ctx); 514 515 if (WARN_ON(!src_buf || !dst_buf)) 516 goto unlock; 517 dst_buf->is_held = src_buf->flags & V4L2_BUF_FLAG_M2M_HOLD_CAPTURE_BUF; 518 if (!dst_buf->is_held) { 519 v4l2_m2m_dst_buf_remove(m2m_ctx); 520 v4l2_m2m_buf_done(dst_buf, state); 521 } 522 /* 523 * If the request API is being used, returning the OUTPUT 524 * (src) buffer will wake-up any process waiting on the 525 * request file descriptor. 526 * 527 * Therefore, return the CAPTURE (dst) buffer first, 528 * to avoid signalling the request file descriptor 529 * before the CAPTURE buffer is done. 530 */ 531 v4l2_m2m_buf_done(src_buf, state); 532 schedule_next = _v4l2_m2m_job_finish(m2m_dev, m2m_ctx); 533 unlock: 534 spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags); 535 536 if (schedule_next) 537 v4l2_m2m_schedule_next_job(m2m_dev, m2m_ctx); 538 } 539 EXPORT_SYMBOL(v4l2_m2m_buf_done_and_job_finish); 540 541 void v4l2_m2m_suspend(struct v4l2_m2m_dev *m2m_dev) 542 { 543 unsigned long flags; 544 struct v4l2_m2m_ctx *curr_ctx; 545 546 spin_lock_irqsave(&m2m_dev->job_spinlock, flags); 547 m2m_dev->job_queue_flags |= QUEUE_PAUSED; 548 curr_ctx = m2m_dev->curr_ctx; 549 spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags); 550 551 if (curr_ctx) 552 wait_event(curr_ctx->finished, 553 !(curr_ctx->job_flags & TRANS_RUNNING)); 554 } 555 EXPORT_SYMBOL(v4l2_m2m_suspend); 556 557 void v4l2_m2m_resume(struct v4l2_m2m_dev *m2m_dev) 558 { 559 unsigned long flags; 560 561 spin_lock_irqsave(&m2m_dev->job_spinlock, flags); 562 m2m_dev->job_queue_flags &= ~QUEUE_PAUSED; 563 spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags); 564 565 v4l2_m2m_try_run(m2m_dev); 566 } 567 EXPORT_SYMBOL(v4l2_m2m_resume); 568 569 int v4l2_m2m_reqbufs(struct file *file, struct v4l2_m2m_ctx *m2m_ctx, 570 struct v4l2_requestbuffers *reqbufs) 571 { 572 struct vb2_queue *vq; 573 int ret; 574 575 vq = v4l2_m2m_get_vq(m2m_ctx, reqbufs->type); 576 ret = vb2_reqbufs(vq, reqbufs); 577 /* If count == 0, then the owner has released all buffers and he 578 is no longer owner of the queue. Otherwise we have an owner. */ 579 if (ret == 0) 580 vq->owner = reqbufs->count ? file->private_data : NULL; 581 582 return ret; 583 } 584 EXPORT_SYMBOL_GPL(v4l2_m2m_reqbufs); 585 586 static void v4l2_m2m_adjust_mem_offset(struct vb2_queue *vq, 587 struct v4l2_buffer *buf) 588 { 589 /* Adjust MMAP memory offsets for the CAPTURE queue */ 590 if (buf->memory == V4L2_MEMORY_MMAP && V4L2_TYPE_IS_CAPTURE(vq->type)) { 591 if (V4L2_TYPE_IS_MULTIPLANAR(vq->type)) { 592 unsigned int i; 593 594 for (i = 0; i < buf->length; ++i) 595 buf->m.planes[i].m.mem_offset 596 += DST_QUEUE_OFF_BASE; 597 } else { 598 buf->m.offset += DST_QUEUE_OFF_BASE; 599 } 600 } 601 } 602 603 int v4l2_m2m_querybuf(struct file *file, struct v4l2_m2m_ctx *m2m_ctx, 604 struct v4l2_buffer *buf) 605 { 606 struct vb2_queue *vq; 607 int ret; 608 609 vq = v4l2_m2m_get_vq(m2m_ctx, buf->type); 610 ret = vb2_querybuf(vq, buf); 611 if (ret) 612 return ret; 613 614 /* Adjust MMAP memory offsets for the CAPTURE queue */ 615 v4l2_m2m_adjust_mem_offset(vq, buf); 616 617 return 0; 618 } 619 EXPORT_SYMBOL_GPL(v4l2_m2m_querybuf); 620 621 /* 622 * This will add the LAST flag and mark the buffer management 623 * state as stopped. 624 * This is called when the last capture buffer must be flagged as LAST 625 * in draining mode from the encoder/decoder driver buf_queue() callback 626 * or from v4l2_update_last_buf_state() when a capture buffer is available. 627 */ 628 void v4l2_m2m_last_buffer_done(struct v4l2_m2m_ctx *m2m_ctx, 629 struct vb2_v4l2_buffer *vbuf) 630 { 631 vbuf->flags |= V4L2_BUF_FLAG_LAST; 632 vb2_buffer_done(&vbuf->vb2_buf, VB2_BUF_STATE_DONE); 633 634 v4l2_m2m_mark_stopped(m2m_ctx); 635 } 636 EXPORT_SYMBOL_GPL(v4l2_m2m_last_buffer_done); 637 638 /* When stop command is issued, update buffer management state */ 639 static int v4l2_update_last_buf_state(struct v4l2_m2m_ctx *m2m_ctx) 640 { 641 struct vb2_v4l2_buffer *next_dst_buf; 642 643 if (m2m_ctx->is_draining) 644 return -EBUSY; 645 646 if (m2m_ctx->has_stopped) 647 return 0; 648 649 m2m_ctx->last_src_buf = v4l2_m2m_last_src_buf(m2m_ctx); 650 m2m_ctx->is_draining = true; 651 652 /* 653 * The processing of the last output buffer queued before 654 * the STOP command is expected to mark the buffer management 655 * state as stopped with v4l2_m2m_mark_stopped(). 656 */ 657 if (m2m_ctx->last_src_buf) 658 return 0; 659 660 /* 661 * In case the output queue is empty, try to mark the last capture 662 * buffer as LAST. 663 */ 664 next_dst_buf = v4l2_m2m_dst_buf_remove(m2m_ctx); 665 if (!next_dst_buf) { 666 /* 667 * Wait for the next queued one in encoder/decoder driver 668 * buf_queue() callback using the v4l2_m2m_dst_buf_is_last() 669 * helper or in v4l2_m2m_qbuf() if encoder/decoder is not yet 670 * streaming. 671 */ 672 m2m_ctx->next_buf_last = true; 673 return 0; 674 } 675 676 v4l2_m2m_last_buffer_done(m2m_ctx, next_dst_buf); 677 678 return 0; 679 } 680 681 /* 682 * Updates the encoding/decoding buffer management state, should 683 * be called from encoder/decoder drivers start_streaming() 684 */ 685 void v4l2_m2m_update_start_streaming_state(struct v4l2_m2m_ctx *m2m_ctx, 686 struct vb2_queue *q) 687 { 688 /* If start streaming again, untag the last output buffer */ 689 if (V4L2_TYPE_IS_OUTPUT(q->type)) 690 m2m_ctx->last_src_buf = NULL; 691 } 692 EXPORT_SYMBOL_GPL(v4l2_m2m_update_start_streaming_state); 693 694 /* 695 * Updates the encoding/decoding buffer management state, should 696 * be called from encoder/decoder driver stop_streaming() 697 */ 698 void v4l2_m2m_update_stop_streaming_state(struct v4l2_m2m_ctx *m2m_ctx, 699 struct vb2_queue *q) 700 { 701 if (V4L2_TYPE_IS_OUTPUT(q->type)) { 702 /* 703 * If in draining state, either mark next dst buffer as 704 * done or flag next one to be marked as done either 705 * in encoder/decoder driver buf_queue() callback using 706 * the v4l2_m2m_dst_buf_is_last() helper or in v4l2_m2m_qbuf() 707 * if encoder/decoder is not yet streaming 708 */ 709 if (m2m_ctx->is_draining) { 710 struct vb2_v4l2_buffer *next_dst_buf; 711 712 m2m_ctx->last_src_buf = NULL; 713 next_dst_buf = v4l2_m2m_dst_buf_remove(m2m_ctx); 714 if (!next_dst_buf) 715 m2m_ctx->next_buf_last = true; 716 else 717 v4l2_m2m_last_buffer_done(m2m_ctx, 718 next_dst_buf); 719 } 720 } else { 721 v4l2_m2m_clear_state(m2m_ctx); 722 } 723 } 724 EXPORT_SYMBOL_GPL(v4l2_m2m_update_stop_streaming_state); 725 726 static void v4l2_m2m_force_last_buf_done(struct v4l2_m2m_ctx *m2m_ctx, 727 struct vb2_queue *q) 728 { 729 struct vb2_buffer *vb; 730 struct vb2_v4l2_buffer *vbuf; 731 unsigned int i; 732 733 if (WARN_ON(q->is_output)) 734 return; 735 if (list_empty(&q->queued_list)) 736 return; 737 738 vb = list_first_entry(&q->queued_list, struct vb2_buffer, queued_entry); 739 for (i = 0; i < vb->num_planes; i++) 740 vb2_set_plane_payload(vb, i, 0); 741 742 /* 743 * Since the buffer hasn't been queued to the ready queue, 744 * mark is active and owned before marking it LAST and DONE 745 */ 746 vb->state = VB2_BUF_STATE_ACTIVE; 747 atomic_inc(&q->owned_by_drv_count); 748 749 vbuf = to_vb2_v4l2_buffer(vb); 750 vbuf->field = V4L2_FIELD_NONE; 751 752 v4l2_m2m_last_buffer_done(m2m_ctx, vbuf); 753 } 754 755 int v4l2_m2m_qbuf(struct file *file, struct v4l2_m2m_ctx *m2m_ctx, 756 struct v4l2_buffer *buf) 757 { 758 struct video_device *vdev = video_devdata(file); 759 struct vb2_queue *vq; 760 int ret; 761 762 vq = v4l2_m2m_get_vq(m2m_ctx, buf->type); 763 if (V4L2_TYPE_IS_CAPTURE(vq->type) && 764 (buf->flags & V4L2_BUF_FLAG_REQUEST_FD)) { 765 dprintk("%s: requests cannot be used with capture buffers\n", 766 __func__); 767 return -EPERM; 768 } 769 770 ret = vb2_qbuf(vq, vdev->v4l2_dev->mdev, buf); 771 if (ret) 772 return ret; 773 774 /* Adjust MMAP memory offsets for the CAPTURE queue */ 775 v4l2_m2m_adjust_mem_offset(vq, buf); 776 777 /* 778 * If the capture queue is streaming, but streaming hasn't started 779 * on the device, but was asked to stop, mark the previously queued 780 * buffer as DONE with LAST flag since it won't be queued on the 781 * device. 782 */ 783 if (V4L2_TYPE_IS_CAPTURE(vq->type) && 784 vb2_is_streaming(vq) && !vb2_start_streaming_called(vq) && 785 (v4l2_m2m_has_stopped(m2m_ctx) || v4l2_m2m_dst_buf_is_last(m2m_ctx))) 786 v4l2_m2m_force_last_buf_done(m2m_ctx, vq); 787 else if (!(buf->flags & V4L2_BUF_FLAG_IN_REQUEST)) 788 v4l2_m2m_try_schedule(m2m_ctx); 789 790 return 0; 791 } 792 EXPORT_SYMBOL_GPL(v4l2_m2m_qbuf); 793 794 int v4l2_m2m_dqbuf(struct file *file, struct v4l2_m2m_ctx *m2m_ctx, 795 struct v4l2_buffer *buf) 796 { 797 struct vb2_queue *vq; 798 int ret; 799 800 vq = v4l2_m2m_get_vq(m2m_ctx, buf->type); 801 ret = vb2_dqbuf(vq, buf, file->f_flags & O_NONBLOCK); 802 if (ret) 803 return ret; 804 805 /* Adjust MMAP memory offsets for the CAPTURE queue */ 806 v4l2_m2m_adjust_mem_offset(vq, buf); 807 808 return 0; 809 } 810 EXPORT_SYMBOL_GPL(v4l2_m2m_dqbuf); 811 812 int v4l2_m2m_prepare_buf(struct file *file, struct v4l2_m2m_ctx *m2m_ctx, 813 struct v4l2_buffer *buf) 814 { 815 struct video_device *vdev = video_devdata(file); 816 struct vb2_queue *vq; 817 int ret; 818 819 vq = v4l2_m2m_get_vq(m2m_ctx, buf->type); 820 ret = vb2_prepare_buf(vq, vdev->v4l2_dev->mdev, buf); 821 if (ret) 822 return ret; 823 824 /* Adjust MMAP memory offsets for the CAPTURE queue */ 825 v4l2_m2m_adjust_mem_offset(vq, buf); 826 827 return 0; 828 } 829 EXPORT_SYMBOL_GPL(v4l2_m2m_prepare_buf); 830 831 int v4l2_m2m_create_bufs(struct file *file, struct v4l2_m2m_ctx *m2m_ctx, 832 struct v4l2_create_buffers *create) 833 { 834 struct vb2_queue *vq; 835 836 vq = v4l2_m2m_get_vq(m2m_ctx, create->format.type); 837 return vb2_create_bufs(vq, create); 838 } 839 EXPORT_SYMBOL_GPL(v4l2_m2m_create_bufs); 840 841 int v4l2_m2m_expbuf(struct file *file, struct v4l2_m2m_ctx *m2m_ctx, 842 struct v4l2_exportbuffer *eb) 843 { 844 struct vb2_queue *vq; 845 846 vq = v4l2_m2m_get_vq(m2m_ctx, eb->type); 847 return vb2_expbuf(vq, eb); 848 } 849 EXPORT_SYMBOL_GPL(v4l2_m2m_expbuf); 850 851 int v4l2_m2m_streamon(struct file *file, struct v4l2_m2m_ctx *m2m_ctx, 852 enum v4l2_buf_type type) 853 { 854 struct vb2_queue *vq; 855 int ret; 856 857 vq = v4l2_m2m_get_vq(m2m_ctx, type); 858 ret = vb2_streamon(vq, type); 859 if (!ret) 860 v4l2_m2m_try_schedule(m2m_ctx); 861 862 return ret; 863 } 864 EXPORT_SYMBOL_GPL(v4l2_m2m_streamon); 865 866 int v4l2_m2m_streamoff(struct file *file, struct v4l2_m2m_ctx *m2m_ctx, 867 enum v4l2_buf_type type) 868 { 869 struct v4l2_m2m_dev *m2m_dev; 870 struct v4l2_m2m_queue_ctx *q_ctx; 871 unsigned long flags_job, flags; 872 int ret; 873 874 /* wait until the current context is dequeued from job_queue */ 875 v4l2_m2m_cancel_job(m2m_ctx); 876 877 q_ctx = get_queue_ctx(m2m_ctx, type); 878 ret = vb2_streamoff(&q_ctx->q, type); 879 if (ret) 880 return ret; 881 882 m2m_dev = m2m_ctx->m2m_dev; 883 spin_lock_irqsave(&m2m_dev->job_spinlock, flags_job); 884 /* We should not be scheduled anymore, since we're dropping a queue. */ 885 if (m2m_ctx->job_flags & TRANS_QUEUED) 886 list_del(&m2m_ctx->queue); 887 m2m_ctx->job_flags = 0; 888 889 spin_lock_irqsave(&q_ctx->rdy_spinlock, flags); 890 /* Drop queue, since streamoff returns device to the same state as after 891 * calling reqbufs. */ 892 INIT_LIST_HEAD(&q_ctx->rdy_queue); 893 q_ctx->num_rdy = 0; 894 spin_unlock_irqrestore(&q_ctx->rdy_spinlock, flags); 895 896 if (m2m_dev->curr_ctx == m2m_ctx) { 897 m2m_dev->curr_ctx = NULL; 898 wake_up(&m2m_ctx->finished); 899 } 900 spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags_job); 901 902 return 0; 903 } 904 EXPORT_SYMBOL_GPL(v4l2_m2m_streamoff); 905 906 static __poll_t v4l2_m2m_poll_for_data(struct file *file, 907 struct v4l2_m2m_ctx *m2m_ctx, 908 struct poll_table_struct *wait) 909 { 910 struct vb2_queue *src_q, *dst_q; 911 __poll_t rc = 0; 912 unsigned long flags; 913 914 src_q = v4l2_m2m_get_src_vq(m2m_ctx); 915 dst_q = v4l2_m2m_get_dst_vq(m2m_ctx); 916 917 /* 918 * There has to be at least one buffer queued on each queued_list, which 919 * means either in driver already or waiting for driver to claim it 920 * and start processing. 921 */ 922 if ((!vb2_is_streaming(src_q) || src_q->error || 923 list_empty(&src_q->queued_list)) && 924 (!vb2_is_streaming(dst_q) || dst_q->error || 925 (list_empty(&dst_q->queued_list) && !dst_q->last_buffer_dequeued))) 926 return EPOLLERR; 927 928 spin_lock_irqsave(&src_q->done_lock, flags); 929 if (!list_empty(&src_q->done_list)) 930 rc |= EPOLLOUT | EPOLLWRNORM; 931 spin_unlock_irqrestore(&src_q->done_lock, flags); 932 933 spin_lock_irqsave(&dst_q->done_lock, flags); 934 /* 935 * If the last buffer was dequeued from the capture queue, signal 936 * userspace. DQBUF(CAPTURE) will return -EPIPE. 937 */ 938 if (!list_empty(&dst_q->done_list) || dst_q->last_buffer_dequeued) 939 rc |= EPOLLIN | EPOLLRDNORM; 940 spin_unlock_irqrestore(&dst_q->done_lock, flags); 941 942 return rc; 943 } 944 945 __poll_t v4l2_m2m_poll(struct file *file, struct v4l2_m2m_ctx *m2m_ctx, 946 struct poll_table_struct *wait) 947 { 948 struct v4l2_fh *fh = file_to_v4l2_fh(file); 949 struct vb2_queue *src_q = v4l2_m2m_get_src_vq(m2m_ctx); 950 struct vb2_queue *dst_q = v4l2_m2m_get_dst_vq(m2m_ctx); 951 __poll_t req_events = poll_requested_events(wait); 952 __poll_t rc = 0; 953 954 /* 955 * poll_wait() MUST be called on the first invocation on all the 956 * potential queues of interest, even if we are not interested in their 957 * events during this first call. Failure to do so will result in 958 * queue's events to be ignored because the poll_table won't be capable 959 * of adding new wait queues thereafter. 960 */ 961 poll_wait(file, &src_q->done_wq, wait); 962 poll_wait(file, &dst_q->done_wq, wait); 963 964 if (req_events & (EPOLLOUT | EPOLLWRNORM | EPOLLIN | EPOLLRDNORM)) 965 rc = v4l2_m2m_poll_for_data(file, m2m_ctx, wait); 966 967 poll_wait(file, &fh->wait, wait); 968 if (v4l2_event_pending(fh)) 969 rc |= EPOLLPRI; 970 971 return rc; 972 } 973 EXPORT_SYMBOL_GPL(v4l2_m2m_poll); 974 975 int v4l2_m2m_mmap(struct file *file, struct v4l2_m2m_ctx *m2m_ctx, 976 struct vm_area_struct *vma) 977 { 978 unsigned long offset = vma->vm_pgoff << PAGE_SHIFT; 979 struct vb2_queue *vq; 980 981 if (offset < DST_QUEUE_OFF_BASE) { 982 vq = v4l2_m2m_get_src_vq(m2m_ctx); 983 } else { 984 vq = v4l2_m2m_get_dst_vq(m2m_ctx); 985 vma->vm_pgoff -= (DST_QUEUE_OFF_BASE >> PAGE_SHIFT); 986 } 987 988 return vb2_mmap(vq, vma); 989 } 990 EXPORT_SYMBOL(v4l2_m2m_mmap); 991 992 #ifndef CONFIG_MMU 993 unsigned long v4l2_m2m_get_unmapped_area(struct file *file, unsigned long addr, 994 unsigned long len, unsigned long pgoff, 995 unsigned long flags) 996 { 997 struct v4l2_fh *fh = file_to_v4l2_fh(file); 998 unsigned long offset = pgoff << PAGE_SHIFT; 999 struct vb2_queue *vq; 1000 1001 if (offset < DST_QUEUE_OFF_BASE) { 1002 vq = v4l2_m2m_get_src_vq(fh->m2m_ctx); 1003 } else { 1004 vq = v4l2_m2m_get_dst_vq(fh->m2m_ctx); 1005 pgoff -= (DST_QUEUE_OFF_BASE >> PAGE_SHIFT); 1006 } 1007 1008 return vb2_get_unmapped_area(vq, addr, len, pgoff, flags); 1009 } 1010 EXPORT_SYMBOL_GPL(v4l2_m2m_get_unmapped_area); 1011 #endif 1012 1013 #if defined(CONFIG_MEDIA_CONTROLLER) 1014 void v4l2_m2m_unregister_media_controller(struct v4l2_m2m_dev *m2m_dev) 1015 { 1016 media_remove_intf_links(&m2m_dev->intf_devnode->intf); 1017 media_devnode_remove(m2m_dev->intf_devnode); 1018 1019 media_entity_remove_links(m2m_dev->source); 1020 media_entity_remove_links(&m2m_dev->sink); 1021 media_entity_remove_links(&m2m_dev->proc); 1022 media_device_unregister_entity(m2m_dev->source); 1023 media_device_unregister_entity(&m2m_dev->sink); 1024 media_device_unregister_entity(&m2m_dev->proc); 1025 kfree(m2m_dev->source->name); 1026 kfree(m2m_dev->sink.name); 1027 kfree(m2m_dev->proc.name); 1028 } 1029 EXPORT_SYMBOL_GPL(v4l2_m2m_unregister_media_controller); 1030 1031 static int v4l2_m2m_register_entity(struct media_device *mdev, 1032 struct v4l2_m2m_dev *m2m_dev, enum v4l2_m2m_entity_type type, 1033 struct video_device *vdev, int function) 1034 { 1035 struct media_entity *entity; 1036 struct media_pad *pads; 1037 char *name; 1038 unsigned int len; 1039 int num_pads; 1040 int ret; 1041 1042 switch (type) { 1043 case MEM2MEM_ENT_TYPE_SOURCE: 1044 entity = m2m_dev->source; 1045 pads = &m2m_dev->source_pad; 1046 pads[0].flags = MEDIA_PAD_FL_SOURCE; 1047 num_pads = 1; 1048 break; 1049 case MEM2MEM_ENT_TYPE_SINK: 1050 entity = &m2m_dev->sink; 1051 pads = &m2m_dev->sink_pad; 1052 pads[0].flags = MEDIA_PAD_FL_SINK; 1053 num_pads = 1; 1054 break; 1055 case MEM2MEM_ENT_TYPE_PROC: 1056 entity = &m2m_dev->proc; 1057 pads = m2m_dev->proc_pads; 1058 pads[0].flags = MEDIA_PAD_FL_SINK; 1059 pads[1].flags = MEDIA_PAD_FL_SOURCE; 1060 num_pads = 2; 1061 break; 1062 default: 1063 return -EINVAL; 1064 } 1065 1066 entity->obj_type = MEDIA_ENTITY_TYPE_BASE; 1067 if (type != MEM2MEM_ENT_TYPE_PROC) { 1068 entity->info.dev.major = VIDEO_MAJOR; 1069 entity->info.dev.minor = vdev->minor; 1070 } 1071 len = strlen(vdev->name) + 2 + strlen(m2m_entity_name[type]); 1072 name = kmalloc(len, GFP_KERNEL); 1073 if (!name) 1074 return -ENOMEM; 1075 snprintf(name, len, "%s-%s", vdev->name, m2m_entity_name[type]); 1076 entity->name = name; 1077 entity->function = function; 1078 1079 ret = media_entity_pads_init(entity, num_pads, pads); 1080 if (ret) { 1081 kfree(entity->name); 1082 entity->name = NULL; 1083 return ret; 1084 } 1085 ret = media_device_register_entity(mdev, entity); 1086 if (ret) { 1087 kfree(entity->name); 1088 entity->name = NULL; 1089 return ret; 1090 } 1091 1092 return 0; 1093 } 1094 1095 int v4l2_m2m_register_media_controller(struct v4l2_m2m_dev *m2m_dev, 1096 struct video_device *vdev, int function) 1097 { 1098 struct media_device *mdev = vdev->v4l2_dev->mdev; 1099 struct media_link *link; 1100 int ret; 1101 1102 if (!mdev) 1103 return 0; 1104 1105 /* A memory-to-memory device consists in two 1106 * DMA engine and one video processing entities. 1107 * The DMA engine entities are linked to a V4L interface 1108 */ 1109 1110 /* Create the three entities with their pads */ 1111 m2m_dev->source = &vdev->entity; 1112 ret = v4l2_m2m_register_entity(mdev, m2m_dev, 1113 MEM2MEM_ENT_TYPE_SOURCE, vdev, MEDIA_ENT_F_IO_V4L); 1114 if (ret) 1115 return ret; 1116 ret = v4l2_m2m_register_entity(mdev, m2m_dev, 1117 MEM2MEM_ENT_TYPE_PROC, vdev, function); 1118 if (ret) 1119 goto err_rel_entity0; 1120 ret = v4l2_m2m_register_entity(mdev, m2m_dev, 1121 MEM2MEM_ENT_TYPE_SINK, vdev, MEDIA_ENT_F_IO_V4L); 1122 if (ret) 1123 goto err_rel_entity1; 1124 1125 /* Connect the three entities */ 1126 ret = media_create_pad_link(m2m_dev->source, 0, &m2m_dev->proc, 0, 1127 MEDIA_LNK_FL_IMMUTABLE | MEDIA_LNK_FL_ENABLED); 1128 if (ret) 1129 goto err_rel_entity2; 1130 1131 ret = media_create_pad_link(&m2m_dev->proc, 1, &m2m_dev->sink, 0, 1132 MEDIA_LNK_FL_IMMUTABLE | MEDIA_LNK_FL_ENABLED); 1133 if (ret) 1134 goto err_rm_links0; 1135 1136 /* Create video interface */ 1137 m2m_dev->intf_devnode = media_devnode_create(mdev, 1138 MEDIA_INTF_T_V4L_VIDEO, 0, 1139 VIDEO_MAJOR, vdev->minor); 1140 if (!m2m_dev->intf_devnode) { 1141 ret = -ENOMEM; 1142 goto err_rm_links1; 1143 } 1144 1145 /* Connect the two DMA engines to the interface */ 1146 link = media_create_intf_link(m2m_dev->source, 1147 &m2m_dev->intf_devnode->intf, 1148 MEDIA_LNK_FL_IMMUTABLE | MEDIA_LNK_FL_ENABLED); 1149 if (!link) { 1150 ret = -ENOMEM; 1151 goto err_rm_devnode; 1152 } 1153 1154 link = media_create_intf_link(&m2m_dev->sink, 1155 &m2m_dev->intf_devnode->intf, 1156 MEDIA_LNK_FL_IMMUTABLE | MEDIA_LNK_FL_ENABLED); 1157 if (!link) { 1158 ret = -ENOMEM; 1159 goto err_rm_intf_link; 1160 } 1161 return 0; 1162 1163 err_rm_intf_link: 1164 media_remove_intf_links(&m2m_dev->intf_devnode->intf); 1165 err_rm_devnode: 1166 media_devnode_remove(m2m_dev->intf_devnode); 1167 err_rm_links1: 1168 media_entity_remove_links(&m2m_dev->sink); 1169 err_rm_links0: 1170 media_entity_remove_links(&m2m_dev->proc); 1171 media_entity_remove_links(m2m_dev->source); 1172 err_rel_entity2: 1173 media_device_unregister_entity(&m2m_dev->proc); 1174 kfree(m2m_dev->proc.name); 1175 err_rel_entity1: 1176 media_device_unregister_entity(&m2m_dev->sink); 1177 kfree(m2m_dev->sink.name); 1178 err_rel_entity0: 1179 media_device_unregister_entity(m2m_dev->source); 1180 kfree(m2m_dev->source->name); 1181 return ret; 1182 return 0; 1183 } 1184 EXPORT_SYMBOL_GPL(v4l2_m2m_register_media_controller); 1185 #endif 1186 1187 struct v4l2_m2m_dev *v4l2_m2m_init(const struct v4l2_m2m_ops *m2m_ops) 1188 { 1189 struct v4l2_m2m_dev *m2m_dev; 1190 1191 if (!m2m_ops || WARN_ON(!m2m_ops->device_run)) 1192 return ERR_PTR(-EINVAL); 1193 1194 m2m_dev = kzalloc(sizeof *m2m_dev, GFP_KERNEL); 1195 if (!m2m_dev) 1196 return ERR_PTR(-ENOMEM); 1197 1198 m2m_dev->curr_ctx = NULL; 1199 m2m_dev->m2m_ops = m2m_ops; 1200 INIT_LIST_HEAD(&m2m_dev->job_queue); 1201 spin_lock_init(&m2m_dev->job_spinlock); 1202 INIT_WORK(&m2m_dev->job_work, v4l2_m2m_device_run_work); 1203 1204 return m2m_dev; 1205 } 1206 EXPORT_SYMBOL_GPL(v4l2_m2m_init); 1207 1208 void v4l2_m2m_release(struct v4l2_m2m_dev *m2m_dev) 1209 { 1210 kfree(m2m_dev); 1211 } 1212 EXPORT_SYMBOL_GPL(v4l2_m2m_release); 1213 1214 struct v4l2_m2m_ctx *v4l2_m2m_ctx_init(struct v4l2_m2m_dev *m2m_dev, 1215 void *drv_priv, 1216 int (*queue_init)(void *priv, struct vb2_queue *src_vq, struct vb2_queue *dst_vq)) 1217 { 1218 struct v4l2_m2m_ctx *m2m_ctx; 1219 struct v4l2_m2m_queue_ctx *out_q_ctx, *cap_q_ctx; 1220 int ret; 1221 1222 m2m_ctx = kzalloc(sizeof *m2m_ctx, GFP_KERNEL); 1223 if (!m2m_ctx) 1224 return ERR_PTR(-ENOMEM); 1225 1226 m2m_ctx->priv = drv_priv; 1227 m2m_ctx->m2m_dev = m2m_dev; 1228 init_waitqueue_head(&m2m_ctx->finished); 1229 1230 out_q_ctx = &m2m_ctx->out_q_ctx; 1231 cap_q_ctx = &m2m_ctx->cap_q_ctx; 1232 1233 INIT_LIST_HEAD(&out_q_ctx->rdy_queue); 1234 INIT_LIST_HEAD(&cap_q_ctx->rdy_queue); 1235 spin_lock_init(&out_q_ctx->rdy_spinlock); 1236 spin_lock_init(&cap_q_ctx->rdy_spinlock); 1237 1238 INIT_LIST_HEAD(&m2m_ctx->queue); 1239 1240 ret = queue_init(drv_priv, &out_q_ctx->q, &cap_q_ctx->q); 1241 1242 if (ret) 1243 goto err; 1244 /* 1245 * Both queues should use same the mutex to lock the m2m context. 1246 * This lock is used in some v4l2_m2m_* helpers. 1247 */ 1248 if (WARN_ON(out_q_ctx->q.lock != cap_q_ctx->q.lock)) { 1249 ret = -EINVAL; 1250 goto err; 1251 } 1252 m2m_ctx->q_lock = out_q_ctx->q.lock; 1253 1254 return m2m_ctx; 1255 err: 1256 kfree(m2m_ctx); 1257 return ERR_PTR(ret); 1258 } 1259 EXPORT_SYMBOL_GPL(v4l2_m2m_ctx_init); 1260 1261 void v4l2_m2m_ctx_release(struct v4l2_m2m_ctx *m2m_ctx) 1262 { 1263 /* wait until the current context is dequeued from job_queue */ 1264 v4l2_m2m_cancel_job(m2m_ctx); 1265 1266 vb2_queue_release(&m2m_ctx->cap_q_ctx.q); 1267 vb2_queue_release(&m2m_ctx->out_q_ctx.q); 1268 1269 kfree(m2m_ctx); 1270 } 1271 EXPORT_SYMBOL_GPL(v4l2_m2m_ctx_release); 1272 1273 void v4l2_m2m_buf_queue(struct v4l2_m2m_ctx *m2m_ctx, 1274 struct vb2_v4l2_buffer *vbuf) 1275 { 1276 struct v4l2_m2m_buffer *b = container_of(vbuf, 1277 struct v4l2_m2m_buffer, vb); 1278 struct v4l2_m2m_queue_ctx *q_ctx; 1279 unsigned long flags; 1280 1281 q_ctx = get_queue_ctx(m2m_ctx, vbuf->vb2_buf.vb2_queue->type); 1282 1283 spin_lock_irqsave(&q_ctx->rdy_spinlock, flags); 1284 list_add_tail(&b->list, &q_ctx->rdy_queue); 1285 q_ctx->num_rdy++; 1286 spin_unlock_irqrestore(&q_ctx->rdy_spinlock, flags); 1287 } 1288 EXPORT_SYMBOL_GPL(v4l2_m2m_buf_queue); 1289 1290 void v4l2_m2m_buf_copy_metadata(const struct vb2_v4l2_buffer *out_vb, 1291 struct vb2_v4l2_buffer *cap_vb) 1292 { 1293 const u32 mask = V4L2_BUF_FLAG_TIMECODE | V4L2_BUF_FLAG_TSTAMP_SRC_MASK; 1294 1295 cap_vb->vb2_buf.timestamp = out_vb->vb2_buf.timestamp; 1296 1297 if (out_vb->flags & V4L2_BUF_FLAG_TIMECODE) 1298 cap_vb->timecode = out_vb->timecode; 1299 cap_vb->field = out_vb->field; 1300 cap_vb->flags &= ~mask; 1301 cap_vb->flags |= out_vb->flags & mask; 1302 cap_vb->vb2_buf.copied_timestamp = 1; 1303 } 1304 EXPORT_SYMBOL_GPL(v4l2_m2m_buf_copy_metadata); 1305 1306 void v4l2_m2m_request_queue(struct media_request *req) 1307 { 1308 struct media_request_object *obj, *obj_safe; 1309 struct v4l2_m2m_ctx *m2m_ctx = NULL; 1310 1311 /* 1312 * Queue all objects. Note that buffer objects are at the end of the 1313 * objects list, after all other object types. Once buffer objects 1314 * are queued, the driver might delete them immediately (if the driver 1315 * processes the buffer at once), so we have to use 1316 * list_for_each_entry_safe() to handle the case where the object we 1317 * queue is deleted. 1318 */ 1319 list_for_each_entry_safe(obj, obj_safe, &req->objects, list) { 1320 struct v4l2_m2m_ctx *m2m_ctx_obj; 1321 struct vb2_buffer *vb; 1322 1323 if (!obj->ops->queue) 1324 continue; 1325 1326 if (vb2_request_object_is_buffer(obj)) { 1327 /* Sanity checks */ 1328 vb = container_of(obj, struct vb2_buffer, req_obj); 1329 WARN_ON(!V4L2_TYPE_IS_OUTPUT(vb->vb2_queue->type)); 1330 m2m_ctx_obj = container_of(vb->vb2_queue, 1331 struct v4l2_m2m_ctx, 1332 out_q_ctx.q); 1333 WARN_ON(m2m_ctx && m2m_ctx_obj != m2m_ctx); 1334 m2m_ctx = m2m_ctx_obj; 1335 } 1336 1337 /* 1338 * The buffer we queue here can in theory be immediately 1339 * unbound, hence the use of list_for_each_entry_safe() 1340 * above and why we call the queue op last. 1341 */ 1342 obj->ops->queue(obj); 1343 } 1344 1345 WARN_ON(!m2m_ctx); 1346 1347 if (m2m_ctx) 1348 v4l2_m2m_try_schedule(m2m_ctx); 1349 } 1350 EXPORT_SYMBOL_GPL(v4l2_m2m_request_queue); 1351 1352 /* Videobuf2 ioctl helpers */ 1353 1354 int v4l2_m2m_ioctl_reqbufs(struct file *file, void *priv, 1355 struct v4l2_requestbuffers *rb) 1356 { 1357 struct v4l2_fh *fh = file_to_v4l2_fh(file); 1358 1359 return v4l2_m2m_reqbufs(file, fh->m2m_ctx, rb); 1360 } 1361 EXPORT_SYMBOL_GPL(v4l2_m2m_ioctl_reqbufs); 1362 1363 int v4l2_m2m_ioctl_create_bufs(struct file *file, void *priv, 1364 struct v4l2_create_buffers *create) 1365 { 1366 struct v4l2_fh *fh = file_to_v4l2_fh(file); 1367 1368 return v4l2_m2m_create_bufs(file, fh->m2m_ctx, create); 1369 } 1370 EXPORT_SYMBOL_GPL(v4l2_m2m_ioctl_create_bufs); 1371 1372 int v4l2_m2m_ioctl_remove_bufs(struct file *file, void *priv, 1373 struct v4l2_remove_buffers *remove) 1374 { 1375 struct v4l2_fh *fh = file_to_v4l2_fh(file); 1376 struct vb2_queue *q = v4l2_m2m_get_vq(fh->m2m_ctx, remove->type); 1377 1378 if (q->type != remove->type) 1379 return -EINVAL; 1380 1381 return vb2_core_remove_bufs(q, remove->index, remove->count); 1382 } 1383 EXPORT_SYMBOL_GPL(v4l2_m2m_ioctl_remove_bufs); 1384 1385 int v4l2_m2m_ioctl_querybuf(struct file *file, void *priv, 1386 struct v4l2_buffer *buf) 1387 { 1388 struct v4l2_fh *fh = file_to_v4l2_fh(file); 1389 1390 return v4l2_m2m_querybuf(file, fh->m2m_ctx, buf); 1391 } 1392 EXPORT_SYMBOL_GPL(v4l2_m2m_ioctl_querybuf); 1393 1394 int v4l2_m2m_ioctl_qbuf(struct file *file, void *priv, 1395 struct v4l2_buffer *buf) 1396 { 1397 struct v4l2_fh *fh = file_to_v4l2_fh(file); 1398 1399 return v4l2_m2m_qbuf(file, fh->m2m_ctx, buf); 1400 } 1401 EXPORT_SYMBOL_GPL(v4l2_m2m_ioctl_qbuf); 1402 1403 int v4l2_m2m_ioctl_dqbuf(struct file *file, void *priv, 1404 struct v4l2_buffer *buf) 1405 { 1406 struct v4l2_fh *fh = file_to_v4l2_fh(file); 1407 1408 return v4l2_m2m_dqbuf(file, fh->m2m_ctx, buf); 1409 } 1410 EXPORT_SYMBOL_GPL(v4l2_m2m_ioctl_dqbuf); 1411 1412 int v4l2_m2m_ioctl_prepare_buf(struct file *file, void *priv, 1413 struct v4l2_buffer *buf) 1414 { 1415 struct v4l2_fh *fh = file_to_v4l2_fh(file); 1416 1417 return v4l2_m2m_prepare_buf(file, fh->m2m_ctx, buf); 1418 } 1419 EXPORT_SYMBOL_GPL(v4l2_m2m_ioctl_prepare_buf); 1420 1421 int v4l2_m2m_ioctl_expbuf(struct file *file, void *priv, 1422 struct v4l2_exportbuffer *eb) 1423 { 1424 struct v4l2_fh *fh = file_to_v4l2_fh(file); 1425 1426 return v4l2_m2m_expbuf(file, fh->m2m_ctx, eb); 1427 } 1428 EXPORT_SYMBOL_GPL(v4l2_m2m_ioctl_expbuf); 1429 1430 int v4l2_m2m_ioctl_streamon(struct file *file, void *priv, 1431 enum v4l2_buf_type type) 1432 { 1433 struct v4l2_fh *fh = file_to_v4l2_fh(file); 1434 1435 return v4l2_m2m_streamon(file, fh->m2m_ctx, type); 1436 } 1437 EXPORT_SYMBOL_GPL(v4l2_m2m_ioctl_streamon); 1438 1439 int v4l2_m2m_ioctl_streamoff(struct file *file, void *priv, 1440 enum v4l2_buf_type type) 1441 { 1442 struct v4l2_fh *fh = file_to_v4l2_fh(file); 1443 1444 return v4l2_m2m_streamoff(file, fh->m2m_ctx, type); 1445 } 1446 EXPORT_SYMBOL_GPL(v4l2_m2m_ioctl_streamoff); 1447 1448 int v4l2_m2m_ioctl_try_encoder_cmd(struct file *file, void *priv, 1449 struct v4l2_encoder_cmd *ec) 1450 { 1451 if (ec->cmd != V4L2_ENC_CMD_STOP && ec->cmd != V4L2_ENC_CMD_START) 1452 return -EINVAL; 1453 1454 ec->flags = 0; 1455 return 0; 1456 } 1457 EXPORT_SYMBOL_GPL(v4l2_m2m_ioctl_try_encoder_cmd); 1458 1459 int v4l2_m2m_ioctl_try_decoder_cmd(struct file *file, void *priv, 1460 struct v4l2_decoder_cmd *dc) 1461 { 1462 if (dc->cmd != V4L2_DEC_CMD_STOP && dc->cmd != V4L2_DEC_CMD_START) 1463 return -EINVAL; 1464 1465 dc->flags = 0; 1466 1467 if (dc->cmd == V4L2_DEC_CMD_STOP) { 1468 dc->stop.pts = 0; 1469 } else if (dc->cmd == V4L2_DEC_CMD_START) { 1470 dc->start.speed = 0; 1471 dc->start.format = V4L2_DEC_START_FMT_NONE; 1472 } 1473 return 0; 1474 } 1475 EXPORT_SYMBOL_GPL(v4l2_m2m_ioctl_try_decoder_cmd); 1476 1477 /* 1478 * Updates the encoding state on ENC_CMD_STOP/ENC_CMD_START 1479 * Should be called from the encoder driver encoder_cmd() callback 1480 */ 1481 int v4l2_m2m_encoder_cmd(struct file *file, struct v4l2_m2m_ctx *m2m_ctx, 1482 struct v4l2_encoder_cmd *ec) 1483 { 1484 if (ec->cmd != V4L2_ENC_CMD_STOP && ec->cmd != V4L2_ENC_CMD_START) 1485 return -EINVAL; 1486 1487 if (ec->cmd == V4L2_ENC_CMD_STOP) 1488 return v4l2_update_last_buf_state(m2m_ctx); 1489 1490 if (m2m_ctx->is_draining) 1491 return -EBUSY; 1492 1493 if (m2m_ctx->has_stopped) 1494 m2m_ctx->has_stopped = false; 1495 1496 return 0; 1497 } 1498 EXPORT_SYMBOL_GPL(v4l2_m2m_encoder_cmd); 1499 1500 /* 1501 * Updates the decoding state on DEC_CMD_STOP/DEC_CMD_START 1502 * Should be called from the decoder driver decoder_cmd() callback 1503 */ 1504 int v4l2_m2m_decoder_cmd(struct file *file, struct v4l2_m2m_ctx *m2m_ctx, 1505 struct v4l2_decoder_cmd *dc) 1506 { 1507 if (dc->cmd != V4L2_DEC_CMD_STOP && dc->cmd != V4L2_DEC_CMD_START) 1508 return -EINVAL; 1509 1510 if (dc->cmd == V4L2_DEC_CMD_STOP) 1511 return v4l2_update_last_buf_state(m2m_ctx); 1512 1513 if (m2m_ctx->is_draining) 1514 return -EBUSY; 1515 1516 if (m2m_ctx->has_stopped) 1517 m2m_ctx->has_stopped = false; 1518 1519 return 0; 1520 } 1521 EXPORT_SYMBOL_GPL(v4l2_m2m_decoder_cmd); 1522 1523 int v4l2_m2m_ioctl_encoder_cmd(struct file *file, void *priv, 1524 struct v4l2_encoder_cmd *ec) 1525 { 1526 struct v4l2_fh *fh = file_to_v4l2_fh(file); 1527 1528 return v4l2_m2m_encoder_cmd(file, fh->m2m_ctx, ec); 1529 } 1530 EXPORT_SYMBOL_GPL(v4l2_m2m_ioctl_encoder_cmd); 1531 1532 int v4l2_m2m_ioctl_decoder_cmd(struct file *file, void *priv, 1533 struct v4l2_decoder_cmd *dc) 1534 { 1535 struct v4l2_fh *fh = file_to_v4l2_fh(file); 1536 1537 return v4l2_m2m_decoder_cmd(file, fh->m2m_ctx, dc); 1538 } 1539 EXPORT_SYMBOL_GPL(v4l2_m2m_ioctl_decoder_cmd); 1540 1541 int v4l2_m2m_ioctl_stateless_try_decoder_cmd(struct file *file, void *priv, 1542 struct v4l2_decoder_cmd *dc) 1543 { 1544 if (dc->cmd != V4L2_DEC_CMD_FLUSH) 1545 return -EINVAL; 1546 1547 dc->flags = 0; 1548 1549 return 0; 1550 } 1551 EXPORT_SYMBOL_GPL(v4l2_m2m_ioctl_stateless_try_decoder_cmd); 1552 1553 int v4l2_m2m_ioctl_stateless_decoder_cmd(struct file *file, void *priv, 1554 struct v4l2_decoder_cmd *dc) 1555 { 1556 struct v4l2_fh *fh = file_to_v4l2_fh(file); 1557 struct vb2_v4l2_buffer *out_vb, *cap_vb; 1558 struct v4l2_m2m_dev *m2m_dev = fh->m2m_ctx->m2m_dev; 1559 unsigned long flags; 1560 int ret; 1561 1562 ret = v4l2_m2m_ioctl_stateless_try_decoder_cmd(file, priv, dc); 1563 if (ret < 0) 1564 return ret; 1565 1566 spin_lock_irqsave(&m2m_dev->job_spinlock, flags); 1567 out_vb = v4l2_m2m_last_src_buf(fh->m2m_ctx); 1568 cap_vb = v4l2_m2m_last_dst_buf(fh->m2m_ctx); 1569 1570 /* 1571 * If there is an out buffer pending, then clear any HOLD flag. 1572 * 1573 * By clearing this flag we ensure that when this output 1574 * buffer is processed any held capture buffer will be released. 1575 */ 1576 if (out_vb) { 1577 out_vb->flags &= ~V4L2_BUF_FLAG_M2M_HOLD_CAPTURE_BUF; 1578 } else if (cap_vb && cap_vb->is_held) { 1579 /* 1580 * If there were no output buffers, but there is a 1581 * capture buffer that is held, then release that 1582 * buffer. 1583 */ 1584 cap_vb->is_held = false; 1585 v4l2_m2m_dst_buf_remove(fh->m2m_ctx); 1586 v4l2_m2m_buf_done(cap_vb, VB2_BUF_STATE_DONE); 1587 } 1588 spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags); 1589 1590 return 0; 1591 } 1592 EXPORT_SYMBOL_GPL(v4l2_m2m_ioctl_stateless_decoder_cmd); 1593 1594 /* 1595 * v4l2_file_operations helpers. It is assumed here same lock is used 1596 * for the output and the capture buffer queue. 1597 */ 1598 1599 int v4l2_m2m_fop_mmap(struct file *file, struct vm_area_struct *vma) 1600 { 1601 struct v4l2_fh *fh = file_to_v4l2_fh(file); 1602 1603 return v4l2_m2m_mmap(file, fh->m2m_ctx, vma); 1604 } 1605 EXPORT_SYMBOL_GPL(v4l2_m2m_fop_mmap); 1606 1607 __poll_t v4l2_m2m_fop_poll(struct file *file, poll_table *wait) 1608 { 1609 struct v4l2_fh *fh = file_to_v4l2_fh(file); 1610 struct v4l2_m2m_ctx *m2m_ctx = fh->m2m_ctx; 1611 __poll_t ret; 1612 1613 if (m2m_ctx->q_lock) 1614 mutex_lock(m2m_ctx->q_lock); 1615 1616 ret = v4l2_m2m_poll(file, m2m_ctx, wait); 1617 1618 if (m2m_ctx->q_lock) 1619 mutex_unlock(m2m_ctx->q_lock); 1620 1621 return ret; 1622 } 1623 EXPORT_SYMBOL_GPL(v4l2_m2m_fop_poll); 1624 1625