1 /* 2 * Memory-to-memory device framework for Video for Linux 2 and videobuf. 3 * 4 * Helper functions for devices that use videobuf buffers for both their 5 * source and destination. 6 * 7 * Copyright (c) 2009-2010 Samsung Electronics Co., Ltd. 8 * Pawel Osciak, <pawel@osciak.com> 9 * Marek Szyprowski, <m.szyprowski@samsung.com> 10 * 11 * This program is free software; you can redistribute it and/or modify 12 * it under the terms of the GNU General Public License as published by the 13 * Free Software Foundation; either version 2 of the License, or (at your 14 * option) any later version. 15 */ 16 #include <linux/module.h> 17 #include <linux/sched.h> 18 #include <linux/slab.h> 19 20 #include <media/media-device.h> 21 #include <media/videobuf2-v4l2.h> 22 #include <media/v4l2-mem2mem.h> 23 #include <media/v4l2-dev.h> 24 #include <media/v4l2-device.h> 25 #include <media/v4l2-fh.h> 26 #include <media/v4l2-event.h> 27 28 MODULE_DESCRIPTION("Mem to mem device framework for videobuf"); 29 MODULE_AUTHOR("Pawel Osciak, <pawel@osciak.com>"); 30 MODULE_LICENSE("GPL"); 31 32 static bool debug; 33 module_param(debug, bool, 0644); 34 35 #define dprintk(fmt, arg...) \ 36 do { \ 37 if (debug) \ 38 printk(KERN_DEBUG "%s: " fmt, __func__, ## arg);\ 39 } while (0) 40 41 42 /* Instance is already queued on the job_queue */ 43 #define TRANS_QUEUED (1 << 0) 44 /* Instance is currently running in hardware */ 45 #define TRANS_RUNNING (1 << 1) 46 /* Instance is currently aborting */ 47 #define TRANS_ABORT (1 << 2) 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 * @curr_ctx: currently running instance 70 * @job_queue: instances queued to run 71 * @job_spinlock: protects job_queue 72 * @m2m_ops: driver callbacks 73 */ 74 struct v4l2_m2m_dev { 75 struct v4l2_m2m_ctx *curr_ctx; 76 #ifdef CONFIG_MEDIA_CONTROLLER 77 struct media_entity *source; 78 struct media_pad source_pad; 79 struct media_entity sink; 80 struct media_pad sink_pad; 81 struct media_entity proc; 82 struct media_pad proc_pads[2]; 83 struct media_intf_devnode *intf_devnode; 84 #endif 85 86 struct list_head job_queue; 87 spinlock_t job_spinlock; 88 89 const struct v4l2_m2m_ops *m2m_ops; 90 }; 91 92 static struct v4l2_m2m_queue_ctx *get_queue_ctx(struct v4l2_m2m_ctx *m2m_ctx, 93 enum v4l2_buf_type type) 94 { 95 if (V4L2_TYPE_IS_OUTPUT(type)) 96 return &m2m_ctx->out_q_ctx; 97 else 98 return &m2m_ctx->cap_q_ctx; 99 } 100 101 struct vb2_queue *v4l2_m2m_get_vq(struct v4l2_m2m_ctx *m2m_ctx, 102 enum v4l2_buf_type type) 103 { 104 struct v4l2_m2m_queue_ctx *q_ctx; 105 106 q_ctx = get_queue_ctx(m2m_ctx, type); 107 if (!q_ctx) 108 return NULL; 109 110 return &q_ctx->q; 111 } 112 EXPORT_SYMBOL(v4l2_m2m_get_vq); 113 114 void *v4l2_m2m_next_buf(struct v4l2_m2m_queue_ctx *q_ctx) 115 { 116 struct v4l2_m2m_buffer *b; 117 unsigned long flags; 118 119 spin_lock_irqsave(&q_ctx->rdy_spinlock, flags); 120 121 if (list_empty(&q_ctx->rdy_queue)) { 122 spin_unlock_irqrestore(&q_ctx->rdy_spinlock, flags); 123 return NULL; 124 } 125 126 b = list_first_entry(&q_ctx->rdy_queue, struct v4l2_m2m_buffer, list); 127 spin_unlock_irqrestore(&q_ctx->rdy_spinlock, flags); 128 return &b->vb; 129 } 130 EXPORT_SYMBOL_GPL(v4l2_m2m_next_buf); 131 132 void *v4l2_m2m_last_buf(struct v4l2_m2m_queue_ctx *q_ctx) 133 { 134 struct v4l2_m2m_buffer *b; 135 unsigned long flags; 136 137 spin_lock_irqsave(&q_ctx->rdy_spinlock, flags); 138 139 if (list_empty(&q_ctx->rdy_queue)) { 140 spin_unlock_irqrestore(&q_ctx->rdy_spinlock, flags); 141 return NULL; 142 } 143 144 b = list_last_entry(&q_ctx->rdy_queue, struct v4l2_m2m_buffer, list); 145 spin_unlock_irqrestore(&q_ctx->rdy_spinlock, flags); 146 return &b->vb; 147 } 148 EXPORT_SYMBOL_GPL(v4l2_m2m_last_buf); 149 150 void *v4l2_m2m_buf_remove(struct v4l2_m2m_queue_ctx *q_ctx) 151 { 152 struct v4l2_m2m_buffer *b; 153 unsigned long flags; 154 155 spin_lock_irqsave(&q_ctx->rdy_spinlock, flags); 156 if (list_empty(&q_ctx->rdy_queue)) { 157 spin_unlock_irqrestore(&q_ctx->rdy_spinlock, flags); 158 return NULL; 159 } 160 b = list_first_entry(&q_ctx->rdy_queue, struct v4l2_m2m_buffer, list); 161 list_del(&b->list); 162 q_ctx->num_rdy--; 163 spin_unlock_irqrestore(&q_ctx->rdy_spinlock, flags); 164 165 return &b->vb; 166 } 167 EXPORT_SYMBOL_GPL(v4l2_m2m_buf_remove); 168 169 void v4l2_m2m_buf_remove_by_buf(struct v4l2_m2m_queue_ctx *q_ctx, 170 struct vb2_v4l2_buffer *vbuf) 171 { 172 struct v4l2_m2m_buffer *b; 173 unsigned long flags; 174 175 spin_lock_irqsave(&q_ctx->rdy_spinlock, flags); 176 b = container_of(vbuf, struct v4l2_m2m_buffer, vb); 177 list_del(&b->list); 178 q_ctx->num_rdy--; 179 spin_unlock_irqrestore(&q_ctx->rdy_spinlock, flags); 180 } 181 EXPORT_SYMBOL_GPL(v4l2_m2m_buf_remove_by_buf); 182 183 struct vb2_v4l2_buffer * 184 v4l2_m2m_buf_remove_by_idx(struct v4l2_m2m_queue_ctx *q_ctx, unsigned int idx) 185 186 { 187 struct v4l2_m2m_buffer *b, *tmp; 188 struct vb2_v4l2_buffer *ret = NULL; 189 unsigned long flags; 190 191 spin_lock_irqsave(&q_ctx->rdy_spinlock, flags); 192 list_for_each_entry_safe(b, tmp, &q_ctx->rdy_queue, list) { 193 if (b->vb.vb2_buf.index == idx) { 194 list_del(&b->list); 195 q_ctx->num_rdy--; 196 ret = &b->vb; 197 break; 198 } 199 } 200 spin_unlock_irqrestore(&q_ctx->rdy_spinlock, flags); 201 202 return ret; 203 } 204 EXPORT_SYMBOL_GPL(v4l2_m2m_buf_remove_by_idx); 205 206 /* 207 * Scheduling handlers 208 */ 209 210 void *v4l2_m2m_get_curr_priv(struct v4l2_m2m_dev *m2m_dev) 211 { 212 unsigned long flags; 213 void *ret = NULL; 214 215 spin_lock_irqsave(&m2m_dev->job_spinlock, flags); 216 if (m2m_dev->curr_ctx) 217 ret = m2m_dev->curr_ctx->priv; 218 spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags); 219 220 return ret; 221 } 222 EXPORT_SYMBOL(v4l2_m2m_get_curr_priv); 223 224 /** 225 * v4l2_m2m_try_run() - select next job to perform and run it if possible 226 * @m2m_dev: per-device context 227 * 228 * Get next transaction (if present) from the waiting jobs list and run it. 229 */ 230 static void v4l2_m2m_try_run(struct v4l2_m2m_dev *m2m_dev) 231 { 232 unsigned long flags; 233 234 spin_lock_irqsave(&m2m_dev->job_spinlock, flags); 235 if (NULL != m2m_dev->curr_ctx) { 236 spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags); 237 dprintk("Another instance is running, won't run now\n"); 238 return; 239 } 240 241 if (list_empty(&m2m_dev->job_queue)) { 242 spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags); 243 dprintk("No job pending\n"); 244 return; 245 } 246 247 m2m_dev->curr_ctx = list_first_entry(&m2m_dev->job_queue, 248 struct v4l2_m2m_ctx, queue); 249 m2m_dev->curr_ctx->job_flags |= TRANS_RUNNING; 250 spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags); 251 252 dprintk("Running job on m2m_ctx: %p\n", m2m_dev->curr_ctx); 253 m2m_dev->m2m_ops->device_run(m2m_dev->curr_ctx->priv); 254 } 255 256 /* 257 * __v4l2_m2m_try_queue() - queue a job 258 * @m2m_dev: m2m device 259 * @m2m_ctx: m2m context 260 * 261 * Check if this context is ready to queue a job. 262 * 263 * This function can run in interrupt context. 264 */ 265 static void __v4l2_m2m_try_queue(struct v4l2_m2m_dev *m2m_dev, 266 struct v4l2_m2m_ctx *m2m_ctx) 267 { 268 unsigned long flags_job, flags_out, flags_cap; 269 270 dprintk("Trying to schedule a job for m2m_ctx: %p\n", m2m_ctx); 271 272 if (!m2m_ctx->out_q_ctx.q.streaming 273 || !m2m_ctx->cap_q_ctx.q.streaming) { 274 dprintk("Streaming needs to be on for both queues\n"); 275 return; 276 } 277 278 spin_lock_irqsave(&m2m_dev->job_spinlock, flags_job); 279 280 /* If the context is aborted then don't schedule it */ 281 if (m2m_ctx->job_flags & TRANS_ABORT) { 282 spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags_job); 283 dprintk("Aborted context\n"); 284 return; 285 } 286 287 if (m2m_ctx->job_flags & TRANS_QUEUED) { 288 spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags_job); 289 dprintk("On job queue already\n"); 290 return; 291 } 292 293 spin_lock_irqsave(&m2m_ctx->out_q_ctx.rdy_spinlock, flags_out); 294 if (list_empty(&m2m_ctx->out_q_ctx.rdy_queue) 295 && !m2m_ctx->out_q_ctx.buffered) { 296 spin_unlock_irqrestore(&m2m_ctx->out_q_ctx.rdy_spinlock, 297 flags_out); 298 spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags_job); 299 dprintk("No input buffers available\n"); 300 return; 301 } 302 spin_lock_irqsave(&m2m_ctx->cap_q_ctx.rdy_spinlock, flags_cap); 303 if (list_empty(&m2m_ctx->cap_q_ctx.rdy_queue) 304 && !m2m_ctx->cap_q_ctx.buffered) { 305 spin_unlock_irqrestore(&m2m_ctx->cap_q_ctx.rdy_spinlock, 306 flags_cap); 307 spin_unlock_irqrestore(&m2m_ctx->out_q_ctx.rdy_spinlock, 308 flags_out); 309 spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags_job); 310 dprintk("No output buffers available\n"); 311 return; 312 } 313 spin_unlock_irqrestore(&m2m_ctx->cap_q_ctx.rdy_spinlock, flags_cap); 314 spin_unlock_irqrestore(&m2m_ctx->out_q_ctx.rdy_spinlock, flags_out); 315 316 if (m2m_dev->m2m_ops->job_ready 317 && (!m2m_dev->m2m_ops->job_ready(m2m_ctx->priv))) { 318 spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags_job); 319 dprintk("Driver not ready\n"); 320 return; 321 } 322 323 list_add_tail(&m2m_ctx->queue, &m2m_dev->job_queue); 324 m2m_ctx->job_flags |= TRANS_QUEUED; 325 326 spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags_job); 327 } 328 329 /** 330 * v4l2_m2m_try_schedule() - schedule and possibly run a job for any context 331 * @m2m_ctx: m2m context 332 * 333 * Check if this context is ready to queue a job. If suitable, 334 * run the next queued job on the mem2mem device. 335 * 336 * This function shouldn't run in interrupt context. 337 * 338 * Note that v4l2_m2m_try_schedule() can schedule one job for this context, 339 * and then run another job for another context. 340 */ 341 void v4l2_m2m_try_schedule(struct v4l2_m2m_ctx *m2m_ctx) 342 { 343 struct v4l2_m2m_dev *m2m_dev = m2m_ctx->m2m_dev; 344 345 __v4l2_m2m_try_queue(m2m_dev, m2m_ctx); 346 v4l2_m2m_try_run(m2m_dev); 347 } 348 EXPORT_SYMBOL_GPL(v4l2_m2m_try_schedule); 349 350 /** 351 * v4l2_m2m_cancel_job() - cancel pending jobs for the context 352 * @m2m_ctx: m2m context with jobs to be canceled 353 * 354 * In case of streamoff or release called on any context, 355 * 1] If the context is currently running, then abort job will be called 356 * 2] If the context is queued, then the context will be removed from 357 * the job_queue 358 */ 359 static void v4l2_m2m_cancel_job(struct v4l2_m2m_ctx *m2m_ctx) 360 { 361 struct v4l2_m2m_dev *m2m_dev; 362 unsigned long flags; 363 364 m2m_dev = m2m_ctx->m2m_dev; 365 spin_lock_irqsave(&m2m_dev->job_spinlock, flags); 366 367 m2m_ctx->job_flags |= TRANS_ABORT; 368 if (m2m_ctx->job_flags & TRANS_RUNNING) { 369 spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags); 370 if (m2m_dev->m2m_ops->job_abort) 371 m2m_dev->m2m_ops->job_abort(m2m_ctx->priv); 372 dprintk("m2m_ctx %p running, will wait to complete", m2m_ctx); 373 wait_event(m2m_ctx->finished, 374 !(m2m_ctx->job_flags & TRANS_RUNNING)); 375 } else if (m2m_ctx->job_flags & TRANS_QUEUED) { 376 list_del(&m2m_ctx->queue); 377 m2m_ctx->job_flags &= ~(TRANS_QUEUED | TRANS_RUNNING); 378 spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags); 379 dprintk("m2m_ctx: %p had been on queue and was removed\n", 380 m2m_ctx); 381 } else { 382 /* Do nothing, was not on queue/running */ 383 spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags); 384 } 385 } 386 387 void v4l2_m2m_job_finish(struct v4l2_m2m_dev *m2m_dev, 388 struct v4l2_m2m_ctx *m2m_ctx) 389 { 390 unsigned long flags; 391 392 spin_lock_irqsave(&m2m_dev->job_spinlock, flags); 393 if (!m2m_dev->curr_ctx || m2m_dev->curr_ctx != m2m_ctx) { 394 spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags); 395 dprintk("Called by an instance not currently running\n"); 396 return; 397 } 398 399 list_del(&m2m_dev->curr_ctx->queue); 400 m2m_dev->curr_ctx->job_flags &= ~(TRANS_QUEUED | TRANS_RUNNING); 401 wake_up(&m2m_dev->curr_ctx->finished); 402 m2m_dev->curr_ctx = NULL; 403 404 spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags); 405 406 /* This instance might have more buffers ready, but since we do not 407 * allow more than one job on the job_queue per instance, each has 408 * to be scheduled separately after the previous one finishes. */ 409 v4l2_m2m_try_schedule(m2m_ctx); 410 } 411 EXPORT_SYMBOL(v4l2_m2m_job_finish); 412 413 int v4l2_m2m_reqbufs(struct file *file, struct v4l2_m2m_ctx *m2m_ctx, 414 struct v4l2_requestbuffers *reqbufs) 415 { 416 struct vb2_queue *vq; 417 int ret; 418 419 vq = v4l2_m2m_get_vq(m2m_ctx, reqbufs->type); 420 ret = vb2_reqbufs(vq, reqbufs); 421 /* If count == 0, then the owner has released all buffers and he 422 is no longer owner of the queue. Otherwise we have an owner. */ 423 if (ret == 0) 424 vq->owner = reqbufs->count ? file->private_data : NULL; 425 426 return ret; 427 } 428 EXPORT_SYMBOL_GPL(v4l2_m2m_reqbufs); 429 430 int v4l2_m2m_querybuf(struct file *file, struct v4l2_m2m_ctx *m2m_ctx, 431 struct v4l2_buffer *buf) 432 { 433 struct vb2_queue *vq; 434 int ret = 0; 435 unsigned int i; 436 437 vq = v4l2_m2m_get_vq(m2m_ctx, buf->type); 438 ret = vb2_querybuf(vq, buf); 439 440 /* Adjust MMAP memory offsets for the CAPTURE queue */ 441 if (buf->memory == V4L2_MEMORY_MMAP && !V4L2_TYPE_IS_OUTPUT(vq->type)) { 442 if (V4L2_TYPE_IS_MULTIPLANAR(vq->type)) { 443 for (i = 0; i < buf->length; ++i) 444 buf->m.planes[i].m.mem_offset 445 += DST_QUEUE_OFF_BASE; 446 } else { 447 buf->m.offset += DST_QUEUE_OFF_BASE; 448 } 449 } 450 451 return ret; 452 } 453 EXPORT_SYMBOL_GPL(v4l2_m2m_querybuf); 454 455 int v4l2_m2m_qbuf(struct file *file, struct v4l2_m2m_ctx *m2m_ctx, 456 struct v4l2_buffer *buf) 457 { 458 struct vb2_queue *vq; 459 int ret; 460 461 vq = v4l2_m2m_get_vq(m2m_ctx, buf->type); 462 ret = vb2_qbuf(vq, buf); 463 if (!ret) 464 v4l2_m2m_try_schedule(m2m_ctx); 465 466 return ret; 467 } 468 EXPORT_SYMBOL_GPL(v4l2_m2m_qbuf); 469 470 int v4l2_m2m_dqbuf(struct file *file, struct v4l2_m2m_ctx *m2m_ctx, 471 struct v4l2_buffer *buf) 472 { 473 struct vb2_queue *vq; 474 475 vq = v4l2_m2m_get_vq(m2m_ctx, buf->type); 476 return vb2_dqbuf(vq, buf, file->f_flags & O_NONBLOCK); 477 } 478 EXPORT_SYMBOL_GPL(v4l2_m2m_dqbuf); 479 480 int v4l2_m2m_prepare_buf(struct file *file, struct v4l2_m2m_ctx *m2m_ctx, 481 struct v4l2_buffer *buf) 482 { 483 struct vb2_queue *vq; 484 int ret; 485 486 vq = v4l2_m2m_get_vq(m2m_ctx, buf->type); 487 ret = vb2_prepare_buf(vq, buf); 488 if (!ret) 489 v4l2_m2m_try_schedule(m2m_ctx); 490 491 return ret; 492 } 493 EXPORT_SYMBOL_GPL(v4l2_m2m_prepare_buf); 494 495 int v4l2_m2m_create_bufs(struct file *file, struct v4l2_m2m_ctx *m2m_ctx, 496 struct v4l2_create_buffers *create) 497 { 498 struct vb2_queue *vq; 499 500 vq = v4l2_m2m_get_vq(m2m_ctx, create->format.type); 501 return vb2_create_bufs(vq, create); 502 } 503 EXPORT_SYMBOL_GPL(v4l2_m2m_create_bufs); 504 505 int v4l2_m2m_expbuf(struct file *file, struct v4l2_m2m_ctx *m2m_ctx, 506 struct v4l2_exportbuffer *eb) 507 { 508 struct vb2_queue *vq; 509 510 vq = v4l2_m2m_get_vq(m2m_ctx, eb->type); 511 return vb2_expbuf(vq, eb); 512 } 513 EXPORT_SYMBOL_GPL(v4l2_m2m_expbuf); 514 515 int v4l2_m2m_streamon(struct file *file, struct v4l2_m2m_ctx *m2m_ctx, 516 enum v4l2_buf_type type) 517 { 518 struct vb2_queue *vq; 519 int ret; 520 521 vq = v4l2_m2m_get_vq(m2m_ctx, type); 522 ret = vb2_streamon(vq, type); 523 if (!ret) 524 v4l2_m2m_try_schedule(m2m_ctx); 525 526 return ret; 527 } 528 EXPORT_SYMBOL_GPL(v4l2_m2m_streamon); 529 530 int v4l2_m2m_streamoff(struct file *file, struct v4l2_m2m_ctx *m2m_ctx, 531 enum v4l2_buf_type type) 532 { 533 struct v4l2_m2m_dev *m2m_dev; 534 struct v4l2_m2m_queue_ctx *q_ctx; 535 unsigned long flags_job, flags; 536 int ret; 537 538 /* wait until the current context is dequeued from job_queue */ 539 v4l2_m2m_cancel_job(m2m_ctx); 540 541 q_ctx = get_queue_ctx(m2m_ctx, type); 542 ret = vb2_streamoff(&q_ctx->q, type); 543 if (ret) 544 return ret; 545 546 m2m_dev = m2m_ctx->m2m_dev; 547 spin_lock_irqsave(&m2m_dev->job_spinlock, flags_job); 548 /* We should not be scheduled anymore, since we're dropping a queue. */ 549 if (m2m_ctx->job_flags & TRANS_QUEUED) 550 list_del(&m2m_ctx->queue); 551 m2m_ctx->job_flags = 0; 552 553 spin_lock_irqsave(&q_ctx->rdy_spinlock, flags); 554 /* Drop queue, since streamoff returns device to the same state as after 555 * calling reqbufs. */ 556 INIT_LIST_HEAD(&q_ctx->rdy_queue); 557 q_ctx->num_rdy = 0; 558 spin_unlock_irqrestore(&q_ctx->rdy_spinlock, flags); 559 560 if (m2m_dev->curr_ctx == m2m_ctx) { 561 m2m_dev->curr_ctx = NULL; 562 wake_up(&m2m_ctx->finished); 563 } 564 spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags_job); 565 566 return 0; 567 } 568 EXPORT_SYMBOL_GPL(v4l2_m2m_streamoff); 569 570 __poll_t v4l2_m2m_poll(struct file *file, struct v4l2_m2m_ctx *m2m_ctx, 571 struct poll_table_struct *wait) 572 { 573 struct video_device *vfd = video_devdata(file); 574 __poll_t req_events = poll_requested_events(wait); 575 struct vb2_queue *src_q, *dst_q; 576 struct vb2_buffer *src_vb = NULL, *dst_vb = NULL; 577 __poll_t rc = 0; 578 unsigned long flags; 579 580 if (test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags)) { 581 struct v4l2_fh *fh = file->private_data; 582 583 if (v4l2_event_pending(fh)) 584 rc = EPOLLPRI; 585 else if (req_events & EPOLLPRI) 586 poll_wait(file, &fh->wait, wait); 587 if (!(req_events & (EPOLLOUT | EPOLLWRNORM | EPOLLIN | EPOLLRDNORM))) 588 return rc; 589 } 590 591 src_q = v4l2_m2m_get_src_vq(m2m_ctx); 592 dst_q = v4l2_m2m_get_dst_vq(m2m_ctx); 593 594 /* 595 * There has to be at least one buffer queued on each queued_list, which 596 * means either in driver already or waiting for driver to claim it 597 * and start processing. 598 */ 599 if ((!src_q->streaming || list_empty(&src_q->queued_list)) 600 && (!dst_q->streaming || list_empty(&dst_q->queued_list))) { 601 rc |= EPOLLERR; 602 goto end; 603 } 604 605 spin_lock_irqsave(&src_q->done_lock, flags); 606 if (list_empty(&src_q->done_list)) 607 poll_wait(file, &src_q->done_wq, wait); 608 spin_unlock_irqrestore(&src_q->done_lock, flags); 609 610 spin_lock_irqsave(&dst_q->done_lock, flags); 611 if (list_empty(&dst_q->done_list)) { 612 /* 613 * If the last buffer was dequeued from the capture queue, 614 * return immediately. DQBUF will return -EPIPE. 615 */ 616 if (dst_q->last_buffer_dequeued) { 617 spin_unlock_irqrestore(&dst_q->done_lock, flags); 618 return rc | EPOLLIN | EPOLLRDNORM; 619 } 620 621 poll_wait(file, &dst_q->done_wq, wait); 622 } 623 spin_unlock_irqrestore(&dst_q->done_lock, flags); 624 625 spin_lock_irqsave(&src_q->done_lock, flags); 626 if (!list_empty(&src_q->done_list)) 627 src_vb = list_first_entry(&src_q->done_list, struct vb2_buffer, 628 done_entry); 629 if (src_vb && (src_vb->state == VB2_BUF_STATE_DONE 630 || src_vb->state == VB2_BUF_STATE_ERROR)) 631 rc |= EPOLLOUT | EPOLLWRNORM; 632 spin_unlock_irqrestore(&src_q->done_lock, flags); 633 634 spin_lock_irqsave(&dst_q->done_lock, flags); 635 if (!list_empty(&dst_q->done_list)) 636 dst_vb = list_first_entry(&dst_q->done_list, struct vb2_buffer, 637 done_entry); 638 if (dst_vb && (dst_vb->state == VB2_BUF_STATE_DONE 639 || dst_vb->state == VB2_BUF_STATE_ERROR)) 640 rc |= EPOLLIN | EPOLLRDNORM; 641 spin_unlock_irqrestore(&dst_q->done_lock, flags); 642 643 end: 644 return rc; 645 } 646 EXPORT_SYMBOL_GPL(v4l2_m2m_poll); 647 648 int v4l2_m2m_mmap(struct file *file, struct v4l2_m2m_ctx *m2m_ctx, 649 struct vm_area_struct *vma) 650 { 651 unsigned long offset = vma->vm_pgoff << PAGE_SHIFT; 652 struct vb2_queue *vq; 653 654 if (offset < DST_QUEUE_OFF_BASE) { 655 vq = v4l2_m2m_get_src_vq(m2m_ctx); 656 } else { 657 vq = v4l2_m2m_get_dst_vq(m2m_ctx); 658 vma->vm_pgoff -= (DST_QUEUE_OFF_BASE >> PAGE_SHIFT); 659 } 660 661 return vb2_mmap(vq, vma); 662 } 663 EXPORT_SYMBOL(v4l2_m2m_mmap); 664 665 #if defined(CONFIG_MEDIA_CONTROLLER) 666 void v4l2_m2m_unregister_media_controller(struct v4l2_m2m_dev *m2m_dev) 667 { 668 media_remove_intf_links(&m2m_dev->intf_devnode->intf); 669 media_devnode_remove(m2m_dev->intf_devnode); 670 671 media_entity_remove_links(m2m_dev->source); 672 media_entity_remove_links(&m2m_dev->sink); 673 media_entity_remove_links(&m2m_dev->proc); 674 media_device_unregister_entity(m2m_dev->source); 675 media_device_unregister_entity(&m2m_dev->sink); 676 media_device_unregister_entity(&m2m_dev->proc); 677 kfree(m2m_dev->source->name); 678 kfree(m2m_dev->sink.name); 679 kfree(m2m_dev->proc.name); 680 } 681 EXPORT_SYMBOL_GPL(v4l2_m2m_unregister_media_controller); 682 683 static int v4l2_m2m_register_entity(struct media_device *mdev, 684 struct v4l2_m2m_dev *m2m_dev, enum v4l2_m2m_entity_type type, 685 struct video_device *vdev, int function) 686 { 687 struct media_entity *entity; 688 struct media_pad *pads; 689 char *name; 690 unsigned int len; 691 int num_pads; 692 int ret; 693 694 switch (type) { 695 case MEM2MEM_ENT_TYPE_SOURCE: 696 entity = m2m_dev->source; 697 pads = &m2m_dev->source_pad; 698 pads[0].flags = MEDIA_PAD_FL_SOURCE; 699 num_pads = 1; 700 break; 701 case MEM2MEM_ENT_TYPE_SINK: 702 entity = &m2m_dev->sink; 703 pads = &m2m_dev->sink_pad; 704 pads[0].flags = MEDIA_PAD_FL_SINK; 705 num_pads = 1; 706 break; 707 case MEM2MEM_ENT_TYPE_PROC: 708 entity = &m2m_dev->proc; 709 pads = m2m_dev->proc_pads; 710 pads[0].flags = MEDIA_PAD_FL_SINK; 711 pads[1].flags = MEDIA_PAD_FL_SOURCE; 712 num_pads = 2; 713 break; 714 default: 715 return -EINVAL; 716 } 717 718 entity->obj_type = MEDIA_ENTITY_TYPE_BASE; 719 if (type != MEM2MEM_ENT_TYPE_PROC) { 720 entity->info.dev.major = VIDEO_MAJOR; 721 entity->info.dev.minor = vdev->minor; 722 } 723 len = strlen(vdev->name) + 2 + strlen(m2m_entity_name[type]); 724 name = kmalloc(len, GFP_KERNEL); 725 if (!name) 726 return -ENOMEM; 727 snprintf(name, len, "%s-%s", vdev->name, m2m_entity_name[type]); 728 entity->name = name; 729 entity->function = function; 730 731 ret = media_entity_pads_init(entity, num_pads, pads); 732 if (ret) 733 return ret; 734 ret = media_device_register_entity(mdev, entity); 735 if (ret) 736 return ret; 737 738 return 0; 739 } 740 741 int v4l2_m2m_register_media_controller(struct v4l2_m2m_dev *m2m_dev, 742 struct video_device *vdev, int function) 743 { 744 struct media_device *mdev = vdev->v4l2_dev->mdev; 745 struct media_link *link; 746 int ret; 747 748 if (!mdev) 749 return 0; 750 751 /* A memory-to-memory device consists in two 752 * DMA engine and one video processing entities. 753 * The DMA engine entities are linked to a V4L interface 754 */ 755 756 /* Create the three entities with their pads */ 757 m2m_dev->source = &vdev->entity; 758 ret = v4l2_m2m_register_entity(mdev, m2m_dev, 759 MEM2MEM_ENT_TYPE_SOURCE, vdev, MEDIA_ENT_F_IO_V4L); 760 if (ret) 761 return ret; 762 ret = v4l2_m2m_register_entity(mdev, m2m_dev, 763 MEM2MEM_ENT_TYPE_PROC, vdev, function); 764 if (ret) 765 goto err_rel_entity0; 766 ret = v4l2_m2m_register_entity(mdev, m2m_dev, 767 MEM2MEM_ENT_TYPE_SINK, vdev, MEDIA_ENT_F_IO_V4L); 768 if (ret) 769 goto err_rel_entity1; 770 771 /* Connect the three entities */ 772 ret = media_create_pad_link(m2m_dev->source, 0, &m2m_dev->proc, 1, 773 MEDIA_LNK_FL_IMMUTABLE | MEDIA_LNK_FL_ENABLED); 774 if (ret) 775 goto err_rel_entity2; 776 777 ret = media_create_pad_link(&m2m_dev->proc, 0, &m2m_dev->sink, 0, 778 MEDIA_LNK_FL_IMMUTABLE | MEDIA_LNK_FL_ENABLED); 779 if (ret) 780 goto err_rm_links0; 781 782 /* Create video interface */ 783 m2m_dev->intf_devnode = media_devnode_create(mdev, 784 MEDIA_INTF_T_V4L_VIDEO, 0, 785 VIDEO_MAJOR, vdev->minor); 786 if (!m2m_dev->intf_devnode) { 787 ret = -ENOMEM; 788 goto err_rm_links1; 789 } 790 791 /* Connect the two DMA engines to the interface */ 792 link = media_create_intf_link(m2m_dev->source, 793 &m2m_dev->intf_devnode->intf, 794 MEDIA_LNK_FL_IMMUTABLE | MEDIA_LNK_FL_ENABLED); 795 if (!link) { 796 ret = -ENOMEM; 797 goto err_rm_devnode; 798 } 799 800 link = media_create_intf_link(&m2m_dev->sink, 801 &m2m_dev->intf_devnode->intf, 802 MEDIA_LNK_FL_IMMUTABLE | MEDIA_LNK_FL_ENABLED); 803 if (!link) { 804 ret = -ENOMEM; 805 goto err_rm_intf_link; 806 } 807 return 0; 808 809 err_rm_intf_link: 810 media_remove_intf_links(&m2m_dev->intf_devnode->intf); 811 err_rm_devnode: 812 media_devnode_remove(m2m_dev->intf_devnode); 813 err_rm_links1: 814 media_entity_remove_links(&m2m_dev->sink); 815 err_rm_links0: 816 media_entity_remove_links(&m2m_dev->proc); 817 media_entity_remove_links(m2m_dev->source); 818 err_rel_entity2: 819 media_device_unregister_entity(&m2m_dev->proc); 820 kfree(m2m_dev->proc.name); 821 err_rel_entity1: 822 media_device_unregister_entity(&m2m_dev->sink); 823 kfree(m2m_dev->sink.name); 824 err_rel_entity0: 825 media_device_unregister_entity(m2m_dev->source); 826 kfree(m2m_dev->source->name); 827 return ret; 828 return 0; 829 } 830 EXPORT_SYMBOL_GPL(v4l2_m2m_register_media_controller); 831 #endif 832 833 struct v4l2_m2m_dev *v4l2_m2m_init(const struct v4l2_m2m_ops *m2m_ops) 834 { 835 struct v4l2_m2m_dev *m2m_dev; 836 837 if (!m2m_ops || WARN_ON(!m2m_ops->device_run)) 838 return ERR_PTR(-EINVAL); 839 840 m2m_dev = kzalloc(sizeof *m2m_dev, GFP_KERNEL); 841 if (!m2m_dev) 842 return ERR_PTR(-ENOMEM); 843 844 m2m_dev->curr_ctx = NULL; 845 m2m_dev->m2m_ops = m2m_ops; 846 INIT_LIST_HEAD(&m2m_dev->job_queue); 847 spin_lock_init(&m2m_dev->job_spinlock); 848 849 return m2m_dev; 850 } 851 EXPORT_SYMBOL_GPL(v4l2_m2m_init); 852 853 void v4l2_m2m_release(struct v4l2_m2m_dev *m2m_dev) 854 { 855 kfree(m2m_dev); 856 } 857 EXPORT_SYMBOL_GPL(v4l2_m2m_release); 858 859 struct v4l2_m2m_ctx *v4l2_m2m_ctx_init(struct v4l2_m2m_dev *m2m_dev, 860 void *drv_priv, 861 int (*queue_init)(void *priv, struct vb2_queue *src_vq, struct vb2_queue *dst_vq)) 862 { 863 struct v4l2_m2m_ctx *m2m_ctx; 864 struct v4l2_m2m_queue_ctx *out_q_ctx, *cap_q_ctx; 865 int ret; 866 867 m2m_ctx = kzalloc(sizeof *m2m_ctx, GFP_KERNEL); 868 if (!m2m_ctx) 869 return ERR_PTR(-ENOMEM); 870 871 m2m_ctx->priv = drv_priv; 872 m2m_ctx->m2m_dev = m2m_dev; 873 init_waitqueue_head(&m2m_ctx->finished); 874 875 out_q_ctx = &m2m_ctx->out_q_ctx; 876 cap_q_ctx = &m2m_ctx->cap_q_ctx; 877 878 INIT_LIST_HEAD(&out_q_ctx->rdy_queue); 879 INIT_LIST_HEAD(&cap_q_ctx->rdy_queue); 880 spin_lock_init(&out_q_ctx->rdy_spinlock); 881 spin_lock_init(&cap_q_ctx->rdy_spinlock); 882 883 INIT_LIST_HEAD(&m2m_ctx->queue); 884 885 ret = queue_init(drv_priv, &out_q_ctx->q, &cap_q_ctx->q); 886 887 if (ret) 888 goto err; 889 /* 890 * If both queues use same mutex assign it as the common buffer 891 * queues lock to the m2m context. This lock is used in the 892 * v4l2_m2m_ioctl_* helpers. 893 */ 894 if (out_q_ctx->q.lock == cap_q_ctx->q.lock) 895 m2m_ctx->q_lock = out_q_ctx->q.lock; 896 897 return m2m_ctx; 898 err: 899 kfree(m2m_ctx); 900 return ERR_PTR(ret); 901 } 902 EXPORT_SYMBOL_GPL(v4l2_m2m_ctx_init); 903 904 void v4l2_m2m_ctx_release(struct v4l2_m2m_ctx *m2m_ctx) 905 { 906 /* wait until the current context is dequeued from job_queue */ 907 v4l2_m2m_cancel_job(m2m_ctx); 908 909 vb2_queue_release(&m2m_ctx->cap_q_ctx.q); 910 vb2_queue_release(&m2m_ctx->out_q_ctx.q); 911 912 kfree(m2m_ctx); 913 } 914 EXPORT_SYMBOL_GPL(v4l2_m2m_ctx_release); 915 916 void v4l2_m2m_buf_queue(struct v4l2_m2m_ctx *m2m_ctx, 917 struct vb2_v4l2_buffer *vbuf) 918 { 919 struct v4l2_m2m_buffer *b = container_of(vbuf, 920 struct v4l2_m2m_buffer, vb); 921 struct v4l2_m2m_queue_ctx *q_ctx; 922 unsigned long flags; 923 924 q_ctx = get_queue_ctx(m2m_ctx, vbuf->vb2_buf.vb2_queue->type); 925 if (!q_ctx) 926 return; 927 928 spin_lock_irqsave(&q_ctx->rdy_spinlock, flags); 929 list_add_tail(&b->list, &q_ctx->rdy_queue); 930 q_ctx->num_rdy++; 931 spin_unlock_irqrestore(&q_ctx->rdy_spinlock, flags); 932 } 933 EXPORT_SYMBOL_GPL(v4l2_m2m_buf_queue); 934 935 /* Videobuf2 ioctl helpers */ 936 937 int v4l2_m2m_ioctl_reqbufs(struct file *file, void *priv, 938 struct v4l2_requestbuffers *rb) 939 { 940 struct v4l2_fh *fh = file->private_data; 941 942 return v4l2_m2m_reqbufs(file, fh->m2m_ctx, rb); 943 } 944 EXPORT_SYMBOL_GPL(v4l2_m2m_ioctl_reqbufs); 945 946 int v4l2_m2m_ioctl_create_bufs(struct file *file, void *priv, 947 struct v4l2_create_buffers *create) 948 { 949 struct v4l2_fh *fh = file->private_data; 950 951 return v4l2_m2m_create_bufs(file, fh->m2m_ctx, create); 952 } 953 EXPORT_SYMBOL_GPL(v4l2_m2m_ioctl_create_bufs); 954 955 int v4l2_m2m_ioctl_querybuf(struct file *file, void *priv, 956 struct v4l2_buffer *buf) 957 { 958 struct v4l2_fh *fh = file->private_data; 959 960 return v4l2_m2m_querybuf(file, fh->m2m_ctx, buf); 961 } 962 EXPORT_SYMBOL_GPL(v4l2_m2m_ioctl_querybuf); 963 964 int v4l2_m2m_ioctl_qbuf(struct file *file, void *priv, 965 struct v4l2_buffer *buf) 966 { 967 struct v4l2_fh *fh = file->private_data; 968 969 return v4l2_m2m_qbuf(file, fh->m2m_ctx, buf); 970 } 971 EXPORT_SYMBOL_GPL(v4l2_m2m_ioctl_qbuf); 972 973 int v4l2_m2m_ioctl_dqbuf(struct file *file, void *priv, 974 struct v4l2_buffer *buf) 975 { 976 struct v4l2_fh *fh = file->private_data; 977 978 return v4l2_m2m_dqbuf(file, fh->m2m_ctx, buf); 979 } 980 EXPORT_SYMBOL_GPL(v4l2_m2m_ioctl_dqbuf); 981 982 int v4l2_m2m_ioctl_prepare_buf(struct file *file, void *priv, 983 struct v4l2_buffer *buf) 984 { 985 struct v4l2_fh *fh = file->private_data; 986 987 return v4l2_m2m_prepare_buf(file, fh->m2m_ctx, buf); 988 } 989 EXPORT_SYMBOL_GPL(v4l2_m2m_ioctl_prepare_buf); 990 991 int v4l2_m2m_ioctl_expbuf(struct file *file, void *priv, 992 struct v4l2_exportbuffer *eb) 993 { 994 struct v4l2_fh *fh = file->private_data; 995 996 return v4l2_m2m_expbuf(file, fh->m2m_ctx, eb); 997 } 998 EXPORT_SYMBOL_GPL(v4l2_m2m_ioctl_expbuf); 999 1000 int v4l2_m2m_ioctl_streamon(struct file *file, void *priv, 1001 enum v4l2_buf_type type) 1002 { 1003 struct v4l2_fh *fh = file->private_data; 1004 1005 return v4l2_m2m_streamon(file, fh->m2m_ctx, type); 1006 } 1007 EXPORT_SYMBOL_GPL(v4l2_m2m_ioctl_streamon); 1008 1009 int v4l2_m2m_ioctl_streamoff(struct file *file, void *priv, 1010 enum v4l2_buf_type type) 1011 { 1012 struct v4l2_fh *fh = file->private_data; 1013 1014 return v4l2_m2m_streamoff(file, fh->m2m_ctx, type); 1015 } 1016 EXPORT_SYMBOL_GPL(v4l2_m2m_ioctl_streamoff); 1017 1018 /* 1019 * v4l2_file_operations helpers. It is assumed here same lock is used 1020 * for the output and the capture buffer queue. 1021 */ 1022 1023 int v4l2_m2m_fop_mmap(struct file *file, struct vm_area_struct *vma) 1024 { 1025 struct v4l2_fh *fh = file->private_data; 1026 1027 return v4l2_m2m_mmap(file, fh->m2m_ctx, vma); 1028 } 1029 EXPORT_SYMBOL_GPL(v4l2_m2m_fop_mmap); 1030 1031 __poll_t v4l2_m2m_fop_poll(struct file *file, poll_table *wait) 1032 { 1033 struct v4l2_fh *fh = file->private_data; 1034 struct v4l2_m2m_ctx *m2m_ctx = fh->m2m_ctx; 1035 __poll_t ret; 1036 1037 if (m2m_ctx->q_lock) 1038 mutex_lock(m2m_ctx->q_lock); 1039 1040 ret = v4l2_m2m_poll(file, m2m_ctx, wait); 1041 1042 if (m2m_ctx->q_lock) 1043 mutex_unlock(m2m_ctx->q_lock); 1044 1045 return ret; 1046 } 1047 EXPORT_SYMBOL_GPL(v4l2_m2m_fop_poll); 1048 1049