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