LCOV - code coverage report
Current view: top level - drivers/media/v4l2-core - v4l2-mem2mem.c (source / functions) Hit Total Coverage
Test: gcov_data_raspi2_real_modules_combined.info Lines: 94 480 19.6 %
Date: 2020-09-30 20:25:40 Functions: 6 58 10.3 %
Branches: 29 352 8.2 %

           Branch data     Line data    Source code
       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                 :            : /* Offset base for buffers on the destination queue - used to distinguish
      47                 :            :  * between source and destination buffers when mmapping - they receive the same
      48                 :            :  * offsets but for different queues */
      49                 :            : #define DST_QUEUE_OFF_BASE      (1 << 30)
      50                 :            : 
      51                 :            : enum v4l2_m2m_entity_type {
      52                 :            :         MEM2MEM_ENT_TYPE_SOURCE,
      53                 :            :         MEM2MEM_ENT_TYPE_SINK,
      54                 :            :         MEM2MEM_ENT_TYPE_PROC
      55                 :            : };
      56                 :            : 
      57                 :            : static const char * const m2m_entity_name[] = {
      58                 :            :         "source",
      59                 :            :         "sink",
      60                 :            :         "proc"
      61                 :            : };
      62                 :            : 
      63                 :            : /**
      64                 :            :  * struct v4l2_m2m_dev - per-device context
      65                 :            :  * @source:             &struct media_entity pointer with the source entity
      66                 :            :  *                      Used only when the M2M device is registered via
      67                 :            :  *                      v4l2_m2m_unregister_media_controller().
      68                 :            :  * @source_pad:         &struct media_pad with the source pad.
      69                 :            :  *                      Used only when the M2M device is registered via
      70                 :            :  *                      v4l2_m2m_unregister_media_controller().
      71                 :            :  * @sink:               &struct media_entity pointer with the sink entity
      72                 :            :  *                      Used only when the M2M device is registered via
      73                 :            :  *                      v4l2_m2m_unregister_media_controller().
      74                 :            :  * @sink_pad:           &struct media_pad with the sink pad.
      75                 :            :  *                      Used only when the M2M device is registered via
      76                 :            :  *                      v4l2_m2m_unregister_media_controller().
      77                 :            :  * @proc:               &struct media_entity pointer with the M2M device itself.
      78                 :            :  * @proc_pads:          &struct media_pad with the @proc pads.
      79                 :            :  *                      Used only when the M2M device is registered via
      80                 :            :  *                      v4l2_m2m_unregister_media_controller().
      81                 :            :  * @intf_devnode:       &struct media_intf devnode pointer with the interface
      82                 :            :  *                      with controls the M2M device.
      83                 :            :  * @curr_ctx:           currently running instance
      84                 :            :  * @job_queue:          instances queued to run
      85                 :            :  * @job_spinlock:       protects job_queue
      86                 :            :  * @job_work:           worker to run queued jobs.
      87                 :            :  * @m2m_ops:            driver callbacks
      88                 :            :  */
      89                 :            : struct v4l2_m2m_dev {
      90                 :            :         struct v4l2_m2m_ctx     *curr_ctx;
      91                 :            : #ifdef CONFIG_MEDIA_CONTROLLER
      92                 :            :         struct media_entity     *source;
      93                 :            :         struct media_pad        source_pad;
      94                 :            :         struct media_entity     sink;
      95                 :            :         struct media_pad        sink_pad;
      96                 :            :         struct media_entity     proc;
      97                 :            :         struct media_pad        proc_pads[2];
      98                 :            :         struct media_intf_devnode *intf_devnode;
      99                 :            : #endif
     100                 :            : 
     101                 :            :         struct list_head        job_queue;
     102                 :            :         spinlock_t              job_spinlock;
     103                 :            :         struct work_struct      job_work;
     104                 :            : 
     105                 :            :         const struct v4l2_m2m_ops *m2m_ops;
     106                 :            : };
     107                 :            : 
     108                 :          0 : static struct v4l2_m2m_queue_ctx *get_queue_ctx(struct v4l2_m2m_ctx *m2m_ctx,
     109                 :            :                                                 enum v4l2_buf_type type)
     110                 :            : {
     111   [ #  #  #  #  :          0 :         if (V4L2_TYPE_IS_OUTPUT(type))
             #  #  #  # ]
     112                 :          0 :                 return &m2m_ctx->out_q_ctx;
     113                 :            :         else
     114                 :          0 :                 return &m2m_ctx->cap_q_ctx;
     115                 :            : }
     116                 :            : 
     117                 :          0 : struct vb2_queue *v4l2_m2m_get_vq(struct v4l2_m2m_ctx *m2m_ctx,
     118                 :            :                                        enum v4l2_buf_type type)
     119                 :            : {
     120                 :            :         struct v4l2_m2m_queue_ctx *q_ctx;
     121                 :            : 
     122                 :          0 :         q_ctx = get_queue_ctx(m2m_ctx, type);
     123   [ #  #  #  #  :          0 :         if (!q_ctx)
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
     124                 :            :                 return NULL;
     125                 :            : 
     126                 :          0 :         return &q_ctx->q;
     127                 :            : }
     128                 :            : EXPORT_SYMBOL(v4l2_m2m_get_vq);
     129                 :            : 
     130                 :          0 : 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                 :          0 :         spin_lock_irqsave(&q_ctx->rdy_spinlock, flags);
     136                 :            : 
     137         [ #  # ]:          0 :         if (list_empty(&q_ctx->rdy_queue)) {
     138                 :            :                 spin_unlock_irqrestore(&q_ctx->rdy_spinlock, flags);
     139                 :          0 :                 return NULL;
     140                 :            :         }
     141                 :            : 
     142                 :          0 :         b = list_first_entry(&q_ctx->rdy_queue, struct v4l2_m2m_buffer, list);
     143                 :            :         spin_unlock_irqrestore(&q_ctx->rdy_spinlock, flags);
     144                 :          0 :         return &b->vb;
     145                 :            : }
     146                 :            : EXPORT_SYMBOL_GPL(v4l2_m2m_next_buf);
     147                 :            : 
     148                 :          0 : 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                 :          0 :         spin_lock_irqsave(&q_ctx->rdy_spinlock, flags);
     154                 :            : 
     155         [ #  # ]:          0 :         if (list_empty(&q_ctx->rdy_queue)) {
     156                 :            :                 spin_unlock_irqrestore(&q_ctx->rdy_spinlock, flags);
     157                 :          0 :                 return NULL;
     158                 :            :         }
     159                 :            : 
     160                 :          0 :         b = list_last_entry(&q_ctx->rdy_queue, struct v4l2_m2m_buffer, list);
     161                 :            :         spin_unlock_irqrestore(&q_ctx->rdy_spinlock, flags);
     162                 :          0 :         return &b->vb;
     163                 :            : }
     164                 :            : EXPORT_SYMBOL_GPL(v4l2_m2m_last_buf);
     165                 :            : 
     166                 :          0 : 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                 :          0 :         spin_lock_irqsave(&q_ctx->rdy_spinlock, flags);
     172         [ #  # ]:          0 :         if (list_empty(&q_ctx->rdy_queue)) {
     173                 :            :                 spin_unlock_irqrestore(&q_ctx->rdy_spinlock, flags);
     174                 :          0 :                 return NULL;
     175                 :            :         }
     176                 :          0 :         b = list_first_entry(&q_ctx->rdy_queue, struct v4l2_m2m_buffer, list);
     177                 :            :         list_del(&b->list);
     178                 :          0 :         q_ctx->num_rdy--;
     179                 :            :         spin_unlock_irqrestore(&q_ctx->rdy_spinlock, flags);
     180                 :            : 
     181                 :          0 :         return &b->vb;
     182                 :            : }
     183                 :            : EXPORT_SYMBOL_GPL(v4l2_m2m_buf_remove);
     184                 :            : 
     185                 :          0 : 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                 :          0 :         spin_lock_irqsave(&q_ctx->rdy_spinlock, flags);
     192                 :            :         b = container_of(vbuf, struct v4l2_m2m_buffer, vb);
     193                 :            :         list_del(&b->list);
     194                 :          0 :         q_ctx->num_rdy--;
     195                 :            :         spin_unlock_irqrestore(&q_ctx->rdy_spinlock, flags);
     196                 :          0 : }
     197                 :            : EXPORT_SYMBOL_GPL(v4l2_m2m_buf_remove_by_buf);
     198                 :            : 
     199                 :            : struct vb2_v4l2_buffer *
     200                 :          0 : 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                 :          0 :         spin_lock_irqsave(&q_ctx->rdy_spinlock, flags);
     208         [ #  # ]:          0 :         list_for_each_entry_safe(b, tmp, &q_ctx->rdy_queue, list) {
     209         [ #  # ]:          0 :                 if (b->vb.vb2_buf.index == idx) {
     210                 :            :                         list_del(&b->list);
     211                 :          0 :                         q_ctx->num_rdy--;
     212                 :          0 :                         ret = &b->vb;
     213                 :          0 :                         break;
     214                 :            :                 }
     215                 :            :         }
     216                 :            :         spin_unlock_irqrestore(&q_ctx->rdy_spinlock, flags);
     217                 :            : 
     218                 :          0 :         return ret;
     219                 :            : }
     220                 :            : EXPORT_SYMBOL_GPL(v4l2_m2m_buf_remove_by_idx);
     221                 :            : 
     222                 :            : /*
     223                 :            :  * Scheduling handlers
     224                 :            :  */
     225                 :            : 
     226                 :          0 : void *v4l2_m2m_get_curr_priv(struct v4l2_m2m_dev *m2m_dev)
     227                 :            : {
     228                 :            :         unsigned long flags;
     229                 :            :         void *ret = NULL;
     230                 :            : 
     231                 :          0 :         spin_lock_irqsave(&m2m_dev->job_spinlock, flags);
     232         [ #  # ]:          0 :         if (m2m_dev->curr_ctx)
     233                 :          0 :                 ret = m2m_dev->curr_ctx->priv;
     234                 :            :         spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags);
     235                 :            : 
     236                 :          0 :         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                 :          0 : static void v4l2_m2m_try_run(struct v4l2_m2m_dev *m2m_dev)
     250                 :            : {
     251                 :            :         unsigned long flags;
     252                 :            : 
     253                 :          0 :         spin_lock_irqsave(&m2m_dev->job_spinlock, flags);
     254         [ #  # ]:          0 :         if (NULL != m2m_dev->curr_ctx) {
     255                 :            :                 spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags);
     256         [ #  # ]:          0 :                 dprintk("Another instance is running, won't run now\n");
     257                 :            :                 return;
     258                 :            :         }
     259                 :            : 
     260         [ #  # ]:          0 :         if (list_empty(&m2m_dev->job_queue)) {
     261                 :            :                 spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags);
     262         [ #  # ]:          0 :                 dprintk("No job pending\n");
     263                 :            :                 return;
     264                 :            :         }
     265                 :            : 
     266                 :          0 :         m2m_dev->curr_ctx = list_first_entry(&m2m_dev->job_queue,
     267                 :            :                                    struct v4l2_m2m_ctx, queue);
     268                 :          0 :         m2m_dev->curr_ctx->job_flags |= TRANS_RUNNING;
     269                 :            :         spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags);
     270                 :            : 
     271         [ #  # ]:          0 :         dprintk("Running job on m2m_ctx: %p\n", m2m_dev->curr_ctx);
     272                 :          0 :         m2m_dev->m2m_ops->device_run(m2m_dev->curr_ctx->priv);
     273                 :            : }
     274                 :            : 
     275                 :            : /*
     276                 :            :  * __v4l2_m2m_try_queue() - queue a job
     277                 :            :  * @m2m_dev: m2m device
     278                 :            :  * @m2m_ctx: m2m context
     279                 :            :  *
     280                 :            :  * Check if this context is ready to queue a job.
     281                 :            :  *
     282                 :            :  * This function can run in interrupt context.
     283                 :            :  */
     284                 :          0 : static void __v4l2_m2m_try_queue(struct v4l2_m2m_dev *m2m_dev,
     285                 :            :                                  struct v4l2_m2m_ctx *m2m_ctx)
     286                 :            : {
     287                 :            :         unsigned long flags_job;
     288                 :            :         struct vb2_v4l2_buffer *dst, *src;
     289                 :            : 
     290         [ #  # ]:          0 :         dprintk("Trying to schedule a job for m2m_ctx: %p\n", m2m_ctx);
     291                 :            : 
     292         [ #  # ]:          0 :         if (!m2m_ctx->out_q_ctx.q.streaming
     293         [ #  # ]:          0 :             || !m2m_ctx->cap_q_ctx.q.streaming) {
     294         [ #  # ]:          0 :                 dprintk("Streaming needs to be on for both queues\n");
     295                 :          0 :                 return;
     296                 :            :         }
     297                 :            : 
     298                 :          0 :         spin_lock_irqsave(&m2m_dev->job_spinlock, flags_job);
     299                 :            : 
     300                 :            :         /* If the context is aborted then don't schedule it */
     301         [ #  # ]:          0 :         if (m2m_ctx->job_flags & TRANS_ABORT) {
     302         [ #  # ]:          0 :                 dprintk("Aborted context\n");
     303                 :            :                 goto job_unlock;
     304                 :            :         }
     305                 :            : 
     306         [ #  # ]:          0 :         if (m2m_ctx->job_flags & TRANS_QUEUED) {
     307         [ #  # ]:          0 :                 dprintk("On job queue already\n");
     308                 :            :                 goto job_unlock;
     309                 :            :         }
     310                 :            : 
     311                 :            :         src = v4l2_m2m_next_src_buf(m2m_ctx);
     312                 :            :         dst = v4l2_m2m_next_dst_buf(m2m_ctx);
     313   [ #  #  #  # ]:          0 :         if (!src && !m2m_ctx->out_q_ctx.buffered) {
     314         [ #  # ]:          0 :                 dprintk("No input buffers available\n");
     315                 :            :                 goto job_unlock;
     316                 :            :         }
     317   [ #  #  #  # ]:          0 :         if (!dst && !m2m_ctx->cap_q_ctx.buffered) {
     318         [ #  # ]:          0 :                 dprintk("No output buffers available\n");
     319                 :            :                 goto job_unlock;
     320                 :            :         }
     321                 :            : 
     322                 :          0 :         m2m_ctx->new_frame = true;
     323                 :            : 
     324   [ #  #  #  #  :          0 :         if (src && dst && dst->is_held &&
                   #  # ]
     325         [ #  # ]:          0 :             dst->vb2_buf.copied_timestamp &&
     326                 :          0 :             dst->vb2_buf.timestamp != src->vb2_buf.timestamp) {
     327                 :          0 :                 dst->is_held = false;
     328                 :            :                 v4l2_m2m_dst_buf_remove(m2m_ctx);
     329                 :            :                 v4l2_m2m_buf_done(dst, VB2_BUF_STATE_DONE);
     330                 :            :                 dst = v4l2_m2m_next_dst_buf(m2m_ctx);
     331                 :            : 
     332   [ #  #  #  # ]:          0 :                 if (!dst && !m2m_ctx->cap_q_ctx.buffered) {
     333         [ #  # ]:          0 :                         dprintk("No output buffers available after returning held buffer\n");
     334                 :            :                         goto job_unlock;
     335                 :            :                 }
     336                 :            :         }
     337                 :            : 
     338   [ #  #  #  # ]:          0 :         if (src && dst && (m2m_ctx->out_q_ctx.q.subsystem_flags &
     339                 :            :                            VB2_V4L2_FL_SUPPORTS_M2M_HOLD_CAPTURE_BUF))
     340   [ #  #  #  # ]:          0 :                 m2m_ctx->new_frame = !dst->vb2_buf.copied_timestamp ||
     341                 :          0 :                         dst->vb2_buf.timestamp != src->vb2_buf.timestamp;
     342                 :            : 
     343         [ #  # ]:          0 :         if (m2m_dev->m2m_ops->job_ready
     344         [ #  # ]:          0 :                 && (!m2m_dev->m2m_ops->job_ready(m2m_ctx->priv))) {
     345         [ #  # ]:          0 :                 dprintk("Driver not ready\n");
     346                 :            :                 goto job_unlock;
     347                 :            :         }
     348                 :            : 
     349                 :          0 :         list_add_tail(&m2m_ctx->queue, &m2m_dev->job_queue);
     350                 :          0 :         m2m_ctx->job_flags |= TRANS_QUEUED;
     351                 :            : 
     352                 :            : job_unlock:
     353                 :            :         spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags_job);
     354                 :            : }
     355                 :            : 
     356                 :            : /**
     357                 :            :  * v4l2_m2m_try_schedule() - schedule and possibly run a job for any context
     358                 :            :  * @m2m_ctx: m2m context
     359                 :            :  *
     360                 :            :  * Check if this context is ready to queue a job. If suitable,
     361                 :            :  * run the next queued job on the mem2mem device.
     362                 :            :  *
     363                 :            :  * This function shouldn't run in interrupt context.
     364                 :            :  *
     365                 :            :  * Note that v4l2_m2m_try_schedule() can schedule one job for this context,
     366                 :            :  * and then run another job for another context.
     367                 :            :  */
     368                 :          0 : void v4l2_m2m_try_schedule(struct v4l2_m2m_ctx *m2m_ctx)
     369                 :            : {
     370                 :          0 :         struct v4l2_m2m_dev *m2m_dev = m2m_ctx->m2m_dev;
     371                 :            : 
     372                 :          0 :         __v4l2_m2m_try_queue(m2m_dev, m2m_ctx);
     373                 :          0 :         v4l2_m2m_try_run(m2m_dev);
     374                 :          0 : }
     375                 :            : EXPORT_SYMBOL_GPL(v4l2_m2m_try_schedule);
     376                 :            : 
     377                 :            : /**
     378                 :            :  * v4l2_m2m_device_run_work() - run pending jobs for the context
     379                 :            :  * @work: Work structure used for scheduling the execution of this function.
     380                 :            :  */
     381                 :          0 : static void v4l2_m2m_device_run_work(struct work_struct *work)
     382                 :            : {
     383                 :            :         struct v4l2_m2m_dev *m2m_dev =
     384                 :          0 :                 container_of(work, struct v4l2_m2m_dev, job_work);
     385                 :            : 
     386                 :          0 :         v4l2_m2m_try_run(m2m_dev);
     387                 :          0 : }
     388                 :            : 
     389                 :            : /**
     390                 :            :  * v4l2_m2m_cancel_job() - cancel pending jobs for the context
     391                 :            :  * @m2m_ctx: m2m context with jobs to be canceled
     392                 :            :  *
     393                 :            :  * In case of streamoff or release called on any context,
     394                 :            :  * 1] If the context is currently running, then abort job will be called
     395                 :            :  * 2] If the context is queued, then the context will be removed from
     396                 :            :  *    the job_queue
     397                 :            :  */
     398                 :        621 : static void v4l2_m2m_cancel_job(struct v4l2_m2m_ctx *m2m_ctx)
     399                 :            : {
     400                 :            :         struct v4l2_m2m_dev *m2m_dev;
     401                 :            :         unsigned long flags;
     402                 :            :         bool det_abort_req;
     403                 :            : 
     404                 :        621 :         m2m_dev = m2m_ctx->m2m_dev;
     405                 :        621 :         spin_lock_irqsave(&m2m_dev->job_spinlock, flags);
     406                 :            : 
     407                 :       1242 :         det_abort_req = !list_empty(&m2m_ctx->det_list);
     408                 :        621 :         m2m_ctx->job_flags |= TRANS_ABORT;
     409         [ -  + ]:        621 :         if (m2m_ctx->job_flags & TRANS_RUNNING) {
     410                 :            :                 spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags);
     411         [ #  # ]:          0 :                 if (m2m_dev->m2m_ops->job_abort)
     412                 :          0 :                         m2m_dev->m2m_ops->job_abort(m2m_ctx->priv);
     413                 :            :                 det_abort_req = false;
     414         [ #  # ]:          0 :                 dprintk("m2m_ctx %p running, will wait to complete\n", m2m_ctx);
     415   [ #  #  #  # ]:          0 :                 wait_event(m2m_ctx->finished,
     416                 :            :                                 !(m2m_ctx->job_flags & TRANS_RUNNING));
     417         [ -  + ]:        621 :         } else if (m2m_ctx->job_flags & TRANS_QUEUED) {
     418                 :            :                 list_del(&m2m_ctx->queue);
     419                 :          0 :                 m2m_ctx->job_flags &= ~(TRANS_QUEUED | TRANS_RUNNING);
     420                 :            :                 spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags);
     421         [ #  # ]:          0 :                 dprintk("m2m_ctx: %p had been on queue and was removed\n",
     422                 :            :                         m2m_ctx);
     423                 :            :         } else {
     424                 :            :                 /* Do nothing, was not on queue/running */
     425                 :            :                 spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags);
     426                 :            :         }
     427                 :            : 
     428                 :            :         /* Wait for detached buffers to come back too */
     429   [ -  +  #  # ]:        621 :         if (det_abort_req && m2m_dev->m2m_ops->job_abort)
     430                 :          0 :                 m2m_dev->m2m_ops->job_abort(m2m_ctx->priv);
     431   [ -  +  #  # ]:       1242 :         wait_event(m2m_ctx->det_empty, list_empty(&m2m_ctx->det_list));
     432                 :        621 : }
     433                 :            : 
     434                 :            : /*
     435                 :            :  * Schedule the next job, called from v4l2_m2m_job_finish() or
     436                 :            :  * v4l2_m2m_buf_done_and_job_finish().
     437                 :            :  */
     438                 :          0 : static void v4l2_m2m_schedule_next_job(struct v4l2_m2m_dev *m2m_dev,
     439                 :            :                                        struct v4l2_m2m_ctx *m2m_ctx)
     440                 :            : {
     441                 :            :         /*
     442                 :            :          * This instance might have more buffers ready, but since we do not
     443                 :            :          * allow more than one job on the job_queue per instance, each has
     444                 :            :          * to be scheduled separately after the previous one finishes.
     445                 :            :          */
     446                 :          0 :         __v4l2_m2m_try_queue(m2m_dev, m2m_ctx);
     447                 :            : 
     448                 :            :         /*
     449                 :            :          * We might be running in atomic context,
     450                 :            :          * but the job must be run in non-atomic context.
     451                 :            :          */
     452                 :          0 :         schedule_work(&m2m_dev->job_work);
     453                 :          0 : }
     454                 :            : 
     455                 :            : /*
     456                 :            :  * Assumes job_spinlock is held, called from v4l2_m2m_job_finish() or
     457                 :            :  * v4l2_m2m_buf_done_and_job_finish().
     458                 :            :  */
     459                 :          0 : static bool _v4l2_m2m_job_finish(struct v4l2_m2m_dev *m2m_dev,
     460                 :            :                                  struct v4l2_m2m_ctx *m2m_ctx)
     461                 :            : {
     462   [ #  #  #  # ]:          0 :         if (!m2m_dev->curr_ctx || m2m_dev->curr_ctx != m2m_ctx) {
     463         [ #  # ]:          0 :                 dprintk("Called by an instance not currently running\n");
     464                 :            :                 return false;
     465                 :            :         }
     466                 :            : 
     467                 :            :         list_del(&m2m_dev->curr_ctx->queue);
     468                 :          0 :         m2m_dev->curr_ctx->job_flags &= ~(TRANS_QUEUED | TRANS_RUNNING);
     469                 :          0 :         m2m_ctx->cap_detached = false;
     470                 :          0 :         wake_up(&m2m_dev->curr_ctx->finished);
     471                 :          0 :         m2m_dev->curr_ctx = NULL;
     472                 :          0 :         return true;
     473                 :            : }
     474                 :            : 
     475                 :          0 : void v4l2_m2m_job_finish(struct v4l2_m2m_dev *m2m_dev,
     476                 :            :                          struct v4l2_m2m_ctx *m2m_ctx)
     477                 :            : {
     478                 :            :         unsigned long flags;
     479                 :            :         bool schedule_next;
     480                 :            : 
     481                 :            :         /*
     482                 :            :          * This function should not be used for drivers that support
     483                 :            :          * holding capture buffers. Those should use
     484                 :            :          * v4l2_m2m_buf_done_and_job_finish() instead.
     485                 :            :          */
     486         [ #  # ]:          0 :         WARN_ON(m2m_ctx->out_q_ctx.q.subsystem_flags &
     487                 :            :                 VB2_V4L2_FL_SUPPORTS_M2M_HOLD_CAPTURE_BUF);
     488                 :          0 :         spin_lock_irqsave(&m2m_dev->job_spinlock, flags);
     489                 :          0 :         schedule_next = _v4l2_m2m_job_finish(m2m_dev, m2m_ctx);
     490                 :            :         spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags);
     491                 :            : 
     492         [ #  # ]:          0 :         if (schedule_next)
     493                 :          0 :                 v4l2_m2m_schedule_next_job(m2m_dev, m2m_ctx);
     494                 :          0 : }
     495                 :            : EXPORT_SYMBOL(v4l2_m2m_job_finish);
     496                 :            : 
     497                 :          0 : struct vb2_v4l2_buffer *_v4l2_m2m_cap_buf_detach(struct v4l2_m2m_ctx *m2m_ctx)
     498                 :            : {
     499                 :            :         struct vb2_v4l2_buffer *buf;
     500                 :            : 
     501                 :            :         buf = v4l2_m2m_dst_buf_remove(m2m_ctx);
     502                 :          0 :         list_add_tail(&container_of(buf, struct v4l2_m2m_buffer, vb)->list,
     503                 :            :                       &m2m_ctx->det_list);
     504                 :          0 :         m2m_ctx->cap_detached = true;
     505                 :          0 :         buf->is_held = true;
     506                 :          0 :         buf->det_state = VB2_BUF_STATE_ACTIVE;
     507                 :            : 
     508                 :          0 :         return buf;
     509                 :            : }
     510                 :            : 
     511                 :          0 : struct vb2_v4l2_buffer *v4l2_m2m_cap_buf_detach(struct v4l2_m2m_dev *m2m_dev,
     512                 :            :                                                 struct v4l2_m2m_ctx *m2m_ctx)
     513                 :            : {
     514                 :            :         unsigned long flags;
     515                 :            :         struct vb2_v4l2_buffer *src_buf, *dst_buf;
     516                 :            : 
     517                 :          0 :         spin_lock_irqsave(&m2m_dev->job_spinlock, flags);
     518                 :            : 
     519                 :            :         dst_buf = NULL;
     520                 :            :         src_buf = v4l2_m2m_next_src_buf(m2m_ctx);
     521                 :            : 
     522   [ #  #  #  # ]:          0 :         if (!(src_buf->flags & V4L2_BUF_FLAG_M2M_HOLD_CAPTURE_BUF) &&
     523                 :          0 :             !m2m_ctx->cap_detached)
     524                 :          0 :                 dst_buf = _v4l2_m2m_cap_buf_detach(m2m_ctx);
     525                 :            : 
     526                 :            :         spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags);
     527                 :          0 :         return dst_buf;
     528                 :            : }
     529                 :            : EXPORT_SYMBOL(v4l2_m2m_cap_buf_detach);
     530                 :            : 
     531                 :          0 : static void _v4l2_m2m_cap_buf_return(struct v4l2_m2m_ctx *m2m_ctx,
     532                 :            :                                      struct vb2_v4l2_buffer *buf,
     533                 :            :                                      enum vb2_buffer_state state)
     534                 :            : {
     535                 :          0 :         buf->det_state = state;
     536                 :            : 
     537                 :            :         /*
     538                 :            :          * Always signal done in the order we got stuff
     539                 :            :          * Stop if we find a buf that is still in use
     540                 :            :          */
     541         [ #  # ]:          0 :         while (!list_empty(&m2m_ctx->det_list)) {
     542                 :          0 :                 buf = &list_first_entry(&m2m_ctx->det_list,
     543                 :            :                                         struct v4l2_m2m_buffer, list)->vb;
     544                 :          0 :                 state = buf->det_state;
     545         [ #  # ]:          0 :                 if (state != VB2_BUF_STATE_DONE &&
     546                 :            :                     state != VB2_BUF_STATE_ERROR)
     547                 :          0 :                         return;
     548                 :            :                 list_del(&container_of(buf, struct v4l2_m2m_buffer, vb)->list);
     549                 :          0 :                 buf->det_state = VB2_BUF_STATE_DEQUEUED;
     550                 :            :                 v4l2_m2m_buf_done(buf, state);
     551                 :            :         }
     552                 :          0 :         wake_up(&m2m_ctx->det_empty);
     553                 :            : }
     554                 :            : 
     555                 :          0 : void v4l2_m2m_cap_buf_return(struct v4l2_m2m_dev *m2m_dev,
     556                 :            :                              struct v4l2_m2m_ctx *m2m_ctx,
     557                 :            :                              struct vb2_v4l2_buffer *buf,
     558                 :            :                              enum vb2_buffer_state state)
     559                 :            : {
     560                 :            :         unsigned long flags;
     561                 :            : 
     562         [ #  # ]:          0 :         if (!buf)
     563                 :          0 :                 return;
     564                 :            : 
     565                 :          0 :         spin_lock_irqsave(&m2m_dev->job_spinlock, flags);
     566                 :          0 :         _v4l2_m2m_cap_buf_return(m2m_ctx, buf, state);
     567                 :            :         spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags);
     568                 :            : }
     569                 :            : EXPORT_SYMBOL(v4l2_m2m_cap_buf_return);
     570                 :            : 
     571                 :          0 : void v4l2_m2m_buf_done_and_job_finish(struct v4l2_m2m_dev *m2m_dev,
     572                 :            :                                       struct v4l2_m2m_ctx *m2m_ctx,
     573                 :            :                                       enum vb2_buffer_state state)
     574                 :            : {
     575                 :            :         struct vb2_v4l2_buffer *src_buf, *dst_buf;
     576                 :            :         bool schedule_next = false;
     577                 :            :         unsigned long flags;
     578                 :            : 
     579                 :          0 :         spin_lock_irqsave(&m2m_dev->job_spinlock, flags);
     580                 :            :         src_buf = v4l2_m2m_src_buf_remove(m2m_ctx);
     581                 :            : 
     582   [ #  #  #  # ]:          0 :         if (WARN_ON(!src_buf))
     583                 :            :                 goto unlock;
     584                 :            :         v4l2_m2m_buf_done(src_buf, state);
     585                 :            : 
     586         [ #  # ]:          0 :         if (!m2m_ctx->cap_detached) {
     587                 :            :                 dst_buf = v4l2_m2m_next_dst_buf(m2m_ctx);
     588   [ #  #  #  # ]:          0 :                 if (WARN_ON(!dst_buf))
     589                 :            :                         goto unlock;
     590                 :            : 
     591                 :          0 :                 dst_buf->is_held = src_buf->flags
     592                 :          0 :                                     & V4L2_BUF_FLAG_M2M_HOLD_CAPTURE_BUF;
     593                 :            : 
     594         [ #  # ]:          0 :                 if (!dst_buf->is_held) {
     595                 :          0 :                         dst_buf = _v4l2_m2m_cap_buf_detach(m2m_ctx);
     596                 :          0 :                         _v4l2_m2m_cap_buf_return(m2m_ctx, dst_buf, state);
     597                 :            :                 }
     598                 :            :         }
     599                 :          0 :         schedule_next = _v4l2_m2m_job_finish(m2m_dev, m2m_ctx);
     600                 :            : unlock:
     601                 :            :         spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags);
     602                 :            : 
     603         [ #  # ]:          0 :         if (schedule_next)
     604                 :          0 :                 v4l2_m2m_schedule_next_job(m2m_dev, m2m_ctx);
     605                 :          0 : }
     606                 :            : EXPORT_SYMBOL(v4l2_m2m_buf_done_and_job_finish);
     607                 :            : 
     608                 :          0 : int v4l2_m2m_reqbufs(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
     609                 :            :                      struct v4l2_requestbuffers *reqbufs)
     610                 :            : {
     611                 :            :         struct vb2_queue *vq;
     612                 :            :         int ret;
     613                 :            : 
     614                 :          0 :         vq = v4l2_m2m_get_vq(m2m_ctx, reqbufs->type);
     615                 :          0 :         ret = vb2_reqbufs(vq, reqbufs);
     616                 :            :         /* If count == 0, then the owner has released all buffers and he
     617                 :            :            is no longer owner of the queue. Otherwise we have an owner. */
     618         [ #  # ]:          0 :         if (ret == 0)
     619         [ #  # ]:          0 :                 vq->owner = reqbufs->count ? file->private_data : NULL;
     620                 :            : 
     621                 :          0 :         return ret;
     622                 :            : }
     623                 :            : EXPORT_SYMBOL_GPL(v4l2_m2m_reqbufs);
     624                 :            : 
     625                 :          0 : int v4l2_m2m_querybuf(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
     626                 :            :                       struct v4l2_buffer *buf)
     627                 :            : {
     628                 :            :         struct vb2_queue *vq;
     629                 :            :         int ret = 0;
     630                 :            :         unsigned int i;
     631                 :            : 
     632                 :          0 :         vq = v4l2_m2m_get_vq(m2m_ctx, buf->type);
     633                 :          0 :         ret = vb2_querybuf(vq, buf);
     634                 :            : 
     635                 :            :         /* Adjust MMAP memory offsets for the CAPTURE queue */
     636   [ #  #  #  #  :          0 :         if (buf->memory == V4L2_MEMORY_MMAP && !V4L2_TYPE_IS_OUTPUT(vq->type)) {
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
     637         [ #  # ]:          0 :                 if (V4L2_TYPE_IS_MULTIPLANAR(vq->type)) {
     638         [ #  # ]:          0 :                         for (i = 0; i < buf->length; ++i)
     639                 :          0 :                                 buf->m.planes[i].m.mem_offset
     640                 :          0 :                                         += DST_QUEUE_OFF_BASE;
     641                 :            :                 } else {
     642                 :          0 :                         buf->m.offset += DST_QUEUE_OFF_BASE;
     643                 :            :                 }
     644                 :            :         }
     645                 :            : 
     646                 :          0 :         return ret;
     647                 :            : }
     648                 :            : EXPORT_SYMBOL_GPL(v4l2_m2m_querybuf);
     649                 :            : 
     650                 :          0 : int v4l2_m2m_qbuf(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
     651                 :            :                   struct v4l2_buffer *buf)
     652                 :            : {
     653                 :          0 :         struct video_device *vdev = video_devdata(file);
     654                 :            :         struct vb2_queue *vq;
     655                 :            :         int ret;
     656                 :            : 
     657                 :          0 :         vq = v4l2_m2m_get_vq(m2m_ctx, buf->type);
     658   [ #  #  #  #  :          0 :         if (!V4L2_TYPE_IS_OUTPUT(vq->type) &&
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
     659                 :          0 :             (buf->flags & V4L2_BUF_FLAG_REQUEST_FD)) {
     660         [ #  # ]:          0 :                 dprintk("%s: requests cannot be used with capture buffers\n",
     661                 :            :                         __func__);
     662                 :            :                 return -EPERM;
     663                 :            :         }
     664                 :          0 :         ret = vb2_qbuf(vq, vdev->v4l2_dev->mdev, buf);
     665   [ #  #  #  # ]:          0 :         if (!ret && !(buf->flags & V4L2_BUF_FLAG_IN_REQUEST))
     666                 :          0 :                 v4l2_m2m_try_schedule(m2m_ctx);
     667                 :            : 
     668                 :          0 :         return ret;
     669                 :            : }
     670                 :            : EXPORT_SYMBOL_GPL(v4l2_m2m_qbuf);
     671                 :            : 
     672                 :          0 : int v4l2_m2m_dqbuf(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
     673                 :            :                    struct v4l2_buffer *buf)
     674                 :            : {
     675                 :            :         struct vb2_queue *vq;
     676                 :            : 
     677                 :          0 :         vq = v4l2_m2m_get_vq(m2m_ctx, buf->type);
     678                 :          0 :         return vb2_dqbuf(vq, buf, file->f_flags & O_NONBLOCK);
     679                 :            : }
     680                 :            : EXPORT_SYMBOL_GPL(v4l2_m2m_dqbuf);
     681                 :            : 
     682                 :          0 : int v4l2_m2m_prepare_buf(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
     683                 :            :                          struct v4l2_buffer *buf)
     684                 :            : {
     685                 :          0 :         struct video_device *vdev = video_devdata(file);
     686                 :            :         struct vb2_queue *vq;
     687                 :            : 
     688                 :          0 :         vq = v4l2_m2m_get_vq(m2m_ctx, buf->type);
     689                 :          0 :         return vb2_prepare_buf(vq, vdev->v4l2_dev->mdev, buf);
     690                 :            : }
     691                 :            : EXPORT_SYMBOL_GPL(v4l2_m2m_prepare_buf);
     692                 :            : 
     693                 :          0 : int v4l2_m2m_create_bufs(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
     694                 :            :                          struct v4l2_create_buffers *create)
     695                 :            : {
     696                 :            :         struct vb2_queue *vq;
     697                 :            : 
     698                 :          0 :         vq = v4l2_m2m_get_vq(m2m_ctx, create->format.type);
     699                 :          0 :         return vb2_create_bufs(vq, create);
     700                 :            : }
     701                 :            : EXPORT_SYMBOL_GPL(v4l2_m2m_create_bufs);
     702                 :            : 
     703                 :          0 : int v4l2_m2m_expbuf(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
     704                 :            :                   struct v4l2_exportbuffer *eb)
     705                 :            : {
     706                 :            :         struct vb2_queue *vq;
     707                 :            : 
     708                 :          0 :         vq = v4l2_m2m_get_vq(m2m_ctx, eb->type);
     709                 :          0 :         return vb2_expbuf(vq, eb);
     710                 :            : }
     711                 :            : EXPORT_SYMBOL_GPL(v4l2_m2m_expbuf);
     712                 :            : 
     713                 :          0 : int v4l2_m2m_streamon(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
     714                 :            :                       enum v4l2_buf_type type)
     715                 :            : {
     716                 :            :         struct vb2_queue *vq;
     717                 :            :         int ret;
     718                 :            : 
     719                 :            :         vq = v4l2_m2m_get_vq(m2m_ctx, type);
     720                 :          0 :         ret = vb2_streamon(vq, type);
     721         [ #  # ]:          0 :         if (!ret)
     722                 :          0 :                 v4l2_m2m_try_schedule(m2m_ctx);
     723                 :            : 
     724                 :          0 :         return ret;
     725                 :            : }
     726                 :            : EXPORT_SYMBOL_GPL(v4l2_m2m_streamon);
     727                 :            : 
     728                 :          0 : int v4l2_m2m_streamoff(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
     729                 :            :                        enum v4l2_buf_type type)
     730                 :            : {
     731                 :            :         struct v4l2_m2m_dev *m2m_dev;
     732                 :            :         struct v4l2_m2m_queue_ctx *q_ctx;
     733                 :            :         unsigned long flags_job, flags;
     734                 :            :         int ret;
     735                 :            : 
     736                 :            :         /* wait until the current context is dequeued from job_queue */
     737                 :          0 :         v4l2_m2m_cancel_job(m2m_ctx);
     738                 :            : 
     739                 :          0 :         q_ctx = get_queue_ctx(m2m_ctx, type);
     740                 :          0 :         ret = vb2_streamoff(&q_ctx->q, type);
     741         [ #  # ]:          0 :         if (ret)
     742                 :            :                 return ret;
     743                 :            : 
     744                 :          0 :         m2m_dev = m2m_ctx->m2m_dev;
     745                 :          0 :         spin_lock_irqsave(&m2m_dev->job_spinlock, flags_job);
     746                 :            :         /* We should not be scheduled anymore, since we're dropping a queue. */
     747         [ #  # ]:          0 :         if (m2m_ctx->job_flags & TRANS_QUEUED)
     748                 :            :                 list_del(&m2m_ctx->queue);
     749                 :          0 :         m2m_ctx->job_flags = 0;
     750                 :            : 
     751                 :          0 :         spin_lock_irqsave(&q_ctx->rdy_spinlock, flags);
     752                 :            :         /* Drop queue, since streamoff returns device to the same state as after
     753                 :            :          * calling reqbufs. */
     754                 :          0 :         INIT_LIST_HEAD(&q_ctx->rdy_queue);
     755                 :          0 :         q_ctx->num_rdy = 0;
     756                 :            :         spin_unlock_irqrestore(&q_ctx->rdy_spinlock, flags);
     757                 :            : 
     758         [ #  # ]:          0 :         if (m2m_dev->curr_ctx == m2m_ctx) {
     759                 :          0 :                 m2m_dev->curr_ctx = NULL;
     760                 :          0 :                 wake_up(&m2m_ctx->finished);
     761                 :            :         }
     762                 :            :         spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags_job);
     763                 :            : 
     764                 :          0 :         return 0;
     765                 :            : }
     766                 :            : EXPORT_SYMBOL_GPL(v4l2_m2m_streamoff);
     767                 :            : 
     768                 :          0 : static __poll_t v4l2_m2m_poll_for_data(struct file *file,
     769                 :            :                                        struct v4l2_m2m_ctx *m2m_ctx,
     770                 :            :                                        struct poll_table_struct *wait)
     771                 :            : {
     772                 :            :         struct vb2_queue *src_q, *dst_q;
     773                 :            :         struct vb2_buffer *src_vb = NULL, *dst_vb = NULL;
     774                 :            :         __poll_t rc = 0;
     775                 :            :         unsigned long flags;
     776                 :            : 
     777                 :            :         src_q = v4l2_m2m_get_src_vq(m2m_ctx);
     778                 :            :         dst_q = v4l2_m2m_get_dst_vq(m2m_ctx);
     779                 :            : 
     780                 :          0 :         poll_wait(file, &src_q->done_wq, wait);
     781                 :          0 :         poll_wait(file, &dst_q->done_wq, wait);
     782                 :            : 
     783                 :            :         /*
     784                 :            :          * There has to be at least one buffer queued on each queued_list, which
     785                 :            :          * means either in driver already or waiting for driver to claim it
     786                 :            :          * and start processing.
     787                 :            :          */
     788   [ #  #  #  # ]:          0 :         if ((!src_q->streaming || src_q->error ||
     789         [ #  # ]:          0 :              list_empty(&src_q->queued_list)) &&
     790         [ #  # ]:          0 :             (!dst_q->streaming || dst_q->error ||
     791                 :          0 :              list_empty(&dst_q->queued_list)))
     792                 :            :                 return EPOLLERR;
     793                 :            : 
     794                 :          0 :         spin_lock_irqsave(&dst_q->done_lock, flags);
     795         [ #  # ]:          0 :         if (list_empty(&dst_q->done_list)) {
     796                 :            :                 /*
     797                 :            :                  * If the last buffer was dequeued from the capture queue,
     798                 :            :                  * return immediately. DQBUF will return -EPIPE.
     799                 :            :                  */
     800         [ #  # ]:          0 :                 if (dst_q->last_buffer_dequeued) {
     801                 :            :                         spin_unlock_irqrestore(&dst_q->done_lock, flags);
     802                 :          0 :                         return EPOLLIN | EPOLLRDNORM;
     803                 :            :                 }
     804                 :            :         }
     805                 :            :         spin_unlock_irqrestore(&dst_q->done_lock, flags);
     806                 :            : 
     807                 :          0 :         spin_lock_irqsave(&src_q->done_lock, flags);
     808         [ #  # ]:          0 :         if (!list_empty(&src_q->done_list))
     809                 :          0 :                 src_vb = list_first_entry(&src_q->done_list, struct vb2_buffer,
     810                 :            :                                                 done_entry);
     811   [ #  #  #  # ]:          0 :         if (src_vb && (src_vb->state == VB2_BUF_STATE_DONE
     812                 :          0 :                         || src_vb->state == VB2_BUF_STATE_ERROR))
     813                 :            :                 rc |= EPOLLOUT | EPOLLWRNORM;
     814                 :            :         spin_unlock_irqrestore(&src_q->done_lock, flags);
     815                 :            : 
     816                 :          0 :         spin_lock_irqsave(&dst_q->done_lock, flags);
     817         [ #  # ]:          0 :         if (!list_empty(&dst_q->done_list))
     818                 :          0 :                 dst_vb = list_first_entry(&dst_q->done_list, struct vb2_buffer,
     819                 :            :                                                 done_entry);
     820   [ #  #  #  # ]:          0 :         if (dst_vb && (dst_vb->state == VB2_BUF_STATE_DONE
     821                 :          0 :                         || dst_vb->state == VB2_BUF_STATE_ERROR))
     822                 :          0 :                 rc |= EPOLLIN | EPOLLRDNORM;
     823                 :            :         spin_unlock_irqrestore(&dst_q->done_lock, flags);
     824                 :            : 
     825                 :          0 :         return rc;
     826                 :            : }
     827                 :            : 
     828                 :          0 : __poll_t v4l2_m2m_poll(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
     829                 :            :                        struct poll_table_struct *wait)
     830                 :            : {
     831                 :          0 :         struct video_device *vfd = video_devdata(file);
     832                 :            :         __poll_t req_events = poll_requested_events(wait);
     833                 :            :         __poll_t rc = 0;
     834                 :            : 
     835         [ #  # ]:          0 :         if (req_events & (EPOLLOUT | EPOLLWRNORM | EPOLLIN | EPOLLRDNORM))
     836                 :          0 :                 rc = v4l2_m2m_poll_for_data(file, m2m_ctx, wait);
     837                 :            : 
     838         [ #  # ]:          0 :         if (test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags)) {
     839                 :          0 :                 struct v4l2_fh *fh = file->private_data;
     840                 :            : 
     841                 :          0 :                 poll_wait(file, &fh->wait, wait);
     842         [ #  # ]:          0 :                 if (v4l2_event_pending(fh))
     843                 :          0 :                         rc |= EPOLLPRI;
     844                 :            :         }
     845                 :            : 
     846                 :          0 :         return rc;
     847                 :            : }
     848                 :            : EXPORT_SYMBOL_GPL(v4l2_m2m_poll);
     849                 :            : 
     850                 :          0 : int v4l2_m2m_mmap(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
     851                 :            :                          struct vm_area_struct *vma)
     852                 :            : {
     853                 :          0 :         unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
     854                 :            :         struct vb2_queue *vq;
     855                 :            : 
     856   [ #  #  #  # ]:          0 :         if (offset < DST_QUEUE_OFF_BASE) {
     857                 :            :                 vq = v4l2_m2m_get_src_vq(m2m_ctx);
     858                 :            :         } else {
     859                 :            :                 vq = v4l2_m2m_get_dst_vq(m2m_ctx);
     860                 :          0 :                 vma->vm_pgoff -= (DST_QUEUE_OFF_BASE >> PAGE_SHIFT);
     861                 :            :         }
     862                 :            : 
     863                 :          0 :         return vb2_mmap(vq, vma);
     864                 :            : }
     865                 :            : EXPORT_SYMBOL(v4l2_m2m_mmap);
     866                 :            : 
     867                 :            : #if defined(CONFIG_MEDIA_CONTROLLER)
     868                 :          0 : void v4l2_m2m_unregister_media_controller(struct v4l2_m2m_dev *m2m_dev)
     869                 :            : {
     870                 :          0 :         media_remove_intf_links(&m2m_dev->intf_devnode->intf);
     871                 :          0 :         media_devnode_remove(m2m_dev->intf_devnode);
     872                 :            : 
     873                 :          0 :         media_entity_remove_links(m2m_dev->source);
     874                 :          0 :         media_entity_remove_links(&m2m_dev->sink);
     875                 :          0 :         media_entity_remove_links(&m2m_dev->proc);
     876                 :          0 :         media_device_unregister_entity(m2m_dev->source);
     877                 :          0 :         media_device_unregister_entity(&m2m_dev->sink);
     878                 :          0 :         media_device_unregister_entity(&m2m_dev->proc);
     879                 :          0 :         kfree(m2m_dev->source->name);
     880                 :          0 :         kfree(m2m_dev->sink.name);
     881                 :          0 :         kfree(m2m_dev->proc.name);
     882                 :          0 : }
     883                 :            : EXPORT_SYMBOL_GPL(v4l2_m2m_unregister_media_controller);
     884                 :            : 
     885                 :       1863 : static int v4l2_m2m_register_entity(struct media_device *mdev,
     886                 :            :         struct v4l2_m2m_dev *m2m_dev, enum v4l2_m2m_entity_type type,
     887                 :            :         struct video_device *vdev, int function)
     888                 :            : {
     889                 :            :         struct media_entity *entity;
     890                 :            :         struct media_pad *pads;
     891                 :            :         char *name;
     892                 :            :         unsigned int len;
     893                 :            :         int num_pads;
     894                 :            :         int ret;
     895                 :            : 
     896   [ +  +  +  - ]:       1863 :         switch (type) {
     897                 :            :         case MEM2MEM_ENT_TYPE_SOURCE:
     898                 :        621 :                 entity = m2m_dev->source;
     899                 :        621 :                 pads = &m2m_dev->source_pad;
     900                 :        621 :                 pads[0].flags = MEDIA_PAD_FL_SOURCE;
     901                 :            :                 num_pads = 1;
     902                 :        621 :                 break;
     903                 :            :         case MEM2MEM_ENT_TYPE_SINK:
     904                 :        621 :                 entity = &m2m_dev->sink;
     905                 :        621 :                 pads = &m2m_dev->sink_pad;
     906                 :        621 :                 pads[0].flags = MEDIA_PAD_FL_SINK;
     907                 :            :                 num_pads = 1;
     908                 :        621 :                 break;
     909                 :            :         case MEM2MEM_ENT_TYPE_PROC:
     910                 :        621 :                 entity = &m2m_dev->proc;
     911                 :        621 :                 pads = m2m_dev->proc_pads;
     912                 :        621 :                 pads[0].flags = MEDIA_PAD_FL_SINK;
     913                 :        621 :                 pads[1].flags = MEDIA_PAD_FL_SOURCE;
     914                 :            :                 num_pads = 2;
     915                 :        621 :                 break;
     916                 :            :         default:
     917                 :            :                 return -EINVAL;
     918                 :            :         }
     919                 :            : 
     920                 :       1863 :         entity->obj_type = MEDIA_ENTITY_TYPE_BASE;
     921         [ +  + ]:       1863 :         if (type != MEM2MEM_ENT_TYPE_PROC) {
     922                 :       1242 :                 entity->info.dev.major = VIDEO_MAJOR;
     923                 :       1242 :                 entity->info.dev.minor = vdev->minor;
     924                 :            :         }
     925                 :       1863 :         len = strlen(vdev->name) + 2 + strlen(m2m_entity_name[type]);
     926                 :            :         name = kmalloc(len, GFP_KERNEL);
     927         [ +  - ]:       1863 :         if (!name)
     928                 :            :                 return -ENOMEM;
     929                 :       1863 :         snprintf(name, len, "%s-%s", vdev->name, m2m_entity_name[type]);
     930                 :       1863 :         entity->name = name;
     931                 :       1863 :         entity->function = function;
     932                 :            : 
     933                 :       1863 :         ret = media_entity_pads_init(entity, num_pads, pads);
     934         [ +  - ]:       1863 :         if (ret)
     935                 :            :                 return ret;
     936                 :       1863 :         ret = media_device_register_entity(mdev, entity);
     937         [ -  + ]:       1863 :         if (ret)
     938                 :          0 :                 return ret;
     939                 :            : 
     940                 :            :         return 0;
     941                 :            : }
     942                 :            : 
     943                 :        621 : int v4l2_m2m_register_media_controller(struct v4l2_m2m_dev *m2m_dev,
     944                 :            :                 struct video_device *vdev, int function)
     945                 :            : {
     946                 :        621 :         struct media_device *mdev = vdev->v4l2_dev->mdev;
     947                 :            :         struct media_link *link;
     948                 :            :         int ret;
     949                 :            : 
     950         [ +  - ]:        621 :         if (!mdev)
     951                 :            :                 return 0;
     952                 :            : 
     953                 :            :         /* A memory-to-memory device consists in two
     954                 :            :          * DMA engine and one video processing entities.
     955                 :            :          * The DMA engine entities are linked to a V4L interface
     956                 :            :          */
     957                 :            : 
     958                 :            :         /* Create the three entities with their pads */
     959                 :        621 :         m2m_dev->source = &vdev->entity;
     960                 :        621 :         ret = v4l2_m2m_register_entity(mdev, m2m_dev,
     961                 :            :                         MEM2MEM_ENT_TYPE_SOURCE, vdev, MEDIA_ENT_F_IO_V4L);
     962         [ +  - ]:        621 :         if (ret)
     963                 :            :                 return ret;
     964                 :        621 :         ret = v4l2_m2m_register_entity(mdev, m2m_dev,
     965                 :            :                         MEM2MEM_ENT_TYPE_PROC, vdev, function);
     966         [ +  - ]:        621 :         if (ret)
     967                 :            :                 goto err_rel_entity0;
     968                 :        621 :         ret = v4l2_m2m_register_entity(mdev, m2m_dev,
     969                 :            :                         MEM2MEM_ENT_TYPE_SINK, vdev, MEDIA_ENT_F_IO_V4L);
     970         [ +  - ]:        621 :         if (ret)
     971                 :            :                 goto err_rel_entity1;
     972                 :            : 
     973                 :            :         /* Connect the three entities */
     974                 :        621 :         ret = media_create_pad_link(m2m_dev->source, 0, &m2m_dev->proc, 0,
     975                 :            :                         MEDIA_LNK_FL_IMMUTABLE | MEDIA_LNK_FL_ENABLED);
     976         [ +  - ]:        621 :         if (ret)
     977                 :            :                 goto err_rel_entity2;
     978                 :            : 
     979                 :        621 :         ret = media_create_pad_link(&m2m_dev->proc, 1, &m2m_dev->sink, 0,
     980                 :            :                         MEDIA_LNK_FL_IMMUTABLE | MEDIA_LNK_FL_ENABLED);
     981         [ +  - ]:        621 :         if (ret)
     982                 :            :                 goto err_rm_links0;
     983                 :            : 
     984                 :            :         /* Create video interface */
     985                 :        621 :         m2m_dev->intf_devnode = media_devnode_create(mdev,
     986                 :            :                         MEDIA_INTF_T_V4L_VIDEO, 0,
     987                 :        621 :                         VIDEO_MAJOR, vdev->minor);
     988         [ +  - ]:        621 :         if (!m2m_dev->intf_devnode) {
     989                 :            :                 ret = -ENOMEM;
     990                 :            :                 goto err_rm_links1;
     991                 :            :         }
     992                 :            : 
     993                 :            :         /* Connect the two DMA engines to the interface */
     994                 :        621 :         link = media_create_intf_link(m2m_dev->source,
     995                 :            :                         &m2m_dev->intf_devnode->intf,
     996                 :            :                         MEDIA_LNK_FL_IMMUTABLE | MEDIA_LNK_FL_ENABLED);
     997         [ +  - ]:        621 :         if (!link) {
     998                 :            :                 ret = -ENOMEM;
     999                 :            :                 goto err_rm_devnode;
    1000                 :            :         }
    1001                 :            : 
    1002                 :        621 :         link = media_create_intf_link(&m2m_dev->sink,
    1003                 :        621 :                         &m2m_dev->intf_devnode->intf,
    1004                 :            :                         MEDIA_LNK_FL_IMMUTABLE | MEDIA_LNK_FL_ENABLED);
    1005         [ -  + ]:        621 :         if (!link) {
    1006                 :            :                 ret = -ENOMEM;
    1007                 :            :                 goto err_rm_intf_link;
    1008                 :            :         }
    1009                 :            :         return 0;
    1010                 :            : 
    1011                 :            : err_rm_intf_link:
    1012                 :          0 :         media_remove_intf_links(&m2m_dev->intf_devnode->intf);
    1013                 :            : err_rm_devnode:
    1014                 :          0 :         media_devnode_remove(m2m_dev->intf_devnode);
    1015                 :            : err_rm_links1:
    1016                 :          0 :         media_entity_remove_links(&m2m_dev->sink);
    1017                 :            : err_rm_links0:
    1018                 :          0 :         media_entity_remove_links(&m2m_dev->proc);
    1019                 :          0 :         media_entity_remove_links(m2m_dev->source);
    1020                 :            : err_rel_entity2:
    1021                 :          0 :         media_device_unregister_entity(&m2m_dev->proc);
    1022                 :          0 :         kfree(m2m_dev->proc.name);
    1023                 :            : err_rel_entity1:
    1024                 :          0 :         media_device_unregister_entity(&m2m_dev->sink);
    1025                 :          0 :         kfree(m2m_dev->sink.name);
    1026                 :            : err_rel_entity0:
    1027                 :          0 :         media_device_unregister_entity(m2m_dev->source);
    1028                 :          0 :         kfree(m2m_dev->source->name);
    1029                 :          0 :         return ret;
    1030                 :            :         return 0;
    1031                 :            : }
    1032                 :            : EXPORT_SYMBOL_GPL(v4l2_m2m_register_media_controller);
    1033                 :            : #endif
    1034                 :            : 
    1035                 :        621 : struct v4l2_m2m_dev *v4l2_m2m_init(const struct v4l2_m2m_ops *m2m_ops)
    1036                 :            : {
    1037                 :            :         struct v4l2_m2m_dev *m2m_dev;
    1038                 :            : 
    1039   [ +  -  -  +  :        621 :         if (!m2m_ops || WARN_ON(!m2m_ops->device_run))
                   +  - ]
    1040                 :            :                 return ERR_PTR(-EINVAL);
    1041                 :            : 
    1042                 :        621 :         m2m_dev = kzalloc(sizeof *m2m_dev, GFP_KERNEL);
    1043         [ +  - ]:        621 :         if (!m2m_dev)
    1044                 :            :                 return ERR_PTR(-ENOMEM);
    1045                 :            : 
    1046                 :        621 :         m2m_dev->curr_ctx = NULL;
    1047                 :        621 :         m2m_dev->m2m_ops = m2m_ops;
    1048                 :        621 :         INIT_LIST_HEAD(&m2m_dev->job_queue);
    1049                 :        621 :         spin_lock_init(&m2m_dev->job_spinlock);
    1050                 :       1242 :         INIT_WORK(&m2m_dev->job_work, v4l2_m2m_device_run_work);
    1051                 :            : 
    1052                 :        621 :         return m2m_dev;
    1053                 :            : }
    1054                 :            : EXPORT_SYMBOL_GPL(v4l2_m2m_init);
    1055                 :            : 
    1056                 :          0 : void v4l2_m2m_release(struct v4l2_m2m_dev *m2m_dev)
    1057                 :            : {
    1058                 :          0 :         kfree(m2m_dev);
    1059                 :          0 : }
    1060                 :            : EXPORT_SYMBOL_GPL(v4l2_m2m_release);
    1061                 :            : 
    1062                 :        621 : struct v4l2_m2m_ctx *v4l2_m2m_ctx_init(struct v4l2_m2m_dev *m2m_dev,
    1063                 :            :                 void *drv_priv,
    1064                 :            :                 int (*queue_init)(void *priv, struct vb2_queue *src_vq, struct vb2_queue *dst_vq))
    1065                 :            : {
    1066                 :            :         struct v4l2_m2m_ctx *m2m_ctx;
    1067                 :            :         struct v4l2_m2m_queue_ctx *out_q_ctx, *cap_q_ctx;
    1068                 :            :         int ret;
    1069                 :            : 
    1070                 :        621 :         m2m_ctx = kzalloc(sizeof *m2m_ctx, GFP_KERNEL);
    1071         [ +  - ]:        621 :         if (!m2m_ctx)
    1072                 :            :                 return ERR_PTR(-ENOMEM);
    1073                 :            : 
    1074                 :        621 :         m2m_ctx->priv = drv_priv;
    1075                 :        621 :         m2m_ctx->m2m_dev = m2m_dev;
    1076                 :        621 :         init_waitqueue_head(&m2m_ctx->finished);
    1077                 :        621 :         init_waitqueue_head(&m2m_ctx->det_empty);
    1078                 :            : 
    1079                 :            :         out_q_ctx = &m2m_ctx->out_q_ctx;
    1080                 :            :         cap_q_ctx = &m2m_ctx->cap_q_ctx;
    1081                 :            : 
    1082                 :        621 :         INIT_LIST_HEAD(&out_q_ctx->rdy_queue);
    1083                 :        621 :         INIT_LIST_HEAD(&cap_q_ctx->rdy_queue);
    1084                 :        621 :         INIT_LIST_HEAD(&m2m_ctx->det_list);
    1085                 :        621 :         spin_lock_init(&out_q_ctx->rdy_spinlock);
    1086                 :        621 :         spin_lock_init(&cap_q_ctx->rdy_spinlock);
    1087                 :            : 
    1088                 :        621 :         INIT_LIST_HEAD(&m2m_ctx->queue);
    1089                 :            : 
    1090                 :        621 :         ret = queue_init(drv_priv, &out_q_ctx->q, &cap_q_ctx->q);
    1091                 :            : 
    1092         [ +  - ]:        621 :         if (ret)
    1093                 :            :                 goto err;
    1094                 :            :         /*
    1095                 :            :          * Both queues should use same the mutex to lock the m2m context.
    1096                 :            :          * This lock is used in some v4l2_m2m_* helpers.
    1097                 :            :          */
    1098   [ -  +  +  - ]:        621 :         if (WARN_ON(out_q_ctx->q.lock != cap_q_ctx->q.lock)) {
    1099                 :            :                 ret = -EINVAL;
    1100                 :            :                 goto err;
    1101                 :            :         }
    1102                 :        621 :         m2m_ctx->q_lock = out_q_ctx->q.lock;
    1103                 :            : 
    1104                 :        621 :         return m2m_ctx;
    1105                 :            : err:
    1106                 :          0 :         kfree(m2m_ctx);
    1107                 :          0 :         return ERR_PTR(ret);
    1108                 :            : }
    1109                 :            : EXPORT_SYMBOL_GPL(v4l2_m2m_ctx_init);
    1110                 :            : 
    1111                 :        621 : void v4l2_m2m_ctx_release(struct v4l2_m2m_ctx *m2m_ctx)
    1112                 :            : {
    1113                 :            :         /* wait until the current context is dequeued from job_queue */
    1114                 :        621 :         v4l2_m2m_cancel_job(m2m_ctx);
    1115                 :            : 
    1116                 :        621 :         vb2_queue_release(&m2m_ctx->cap_q_ctx.q);
    1117                 :        621 :         vb2_queue_release(&m2m_ctx->out_q_ctx.q);
    1118                 :            : 
    1119                 :        621 :         kfree(m2m_ctx);
    1120                 :        621 : }
    1121                 :            : EXPORT_SYMBOL_GPL(v4l2_m2m_ctx_release);
    1122                 :            : 
    1123                 :          0 : void v4l2_m2m_buf_queue(struct v4l2_m2m_ctx *m2m_ctx,
    1124                 :            :                 struct vb2_v4l2_buffer *vbuf)
    1125                 :            : {
    1126                 :            :         struct v4l2_m2m_buffer *b = container_of(vbuf,
    1127                 :            :                                 struct v4l2_m2m_buffer, vb);
    1128                 :            :         struct v4l2_m2m_queue_ctx *q_ctx;
    1129                 :            :         unsigned long flags;
    1130                 :            : 
    1131                 :          0 :         q_ctx = get_queue_ctx(m2m_ctx, vbuf->vb2_buf.vb2_queue->type);
    1132         [ #  # ]:          0 :         if (!q_ctx)
    1133                 :          0 :                 return;
    1134                 :            : 
    1135                 :          0 :         spin_lock_irqsave(&q_ctx->rdy_spinlock, flags);
    1136                 :          0 :         list_add_tail(&b->list, &q_ctx->rdy_queue);
    1137                 :          0 :         q_ctx->num_rdy++;
    1138                 :            :         spin_unlock_irqrestore(&q_ctx->rdy_spinlock, flags);
    1139                 :            : }
    1140                 :            : EXPORT_SYMBOL_GPL(v4l2_m2m_buf_queue);
    1141                 :            : 
    1142                 :          0 : void v4l2_m2m_buf_copy_metadata(const struct vb2_v4l2_buffer *out_vb,
    1143                 :            :                                 struct vb2_v4l2_buffer *cap_vb,
    1144                 :            :                                 bool copy_frame_flags)
    1145                 :            : {
    1146                 :            :         u32 mask = V4L2_BUF_FLAG_TIMECODE | V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
    1147                 :            : 
    1148         [ #  # ]:          0 :         if (copy_frame_flags)
    1149                 :            :                 mask |= V4L2_BUF_FLAG_KEYFRAME | V4L2_BUF_FLAG_PFRAME |
    1150                 :            :                         V4L2_BUF_FLAG_BFRAME;
    1151                 :            : 
    1152                 :          0 :         cap_vb->vb2_buf.timestamp = out_vb->vb2_buf.timestamp;
    1153                 :            : 
    1154         [ #  # ]:          0 :         if (out_vb->flags & V4L2_BUF_FLAG_TIMECODE)
    1155                 :          0 :                 cap_vb->timecode = out_vb->timecode;
    1156                 :          0 :         cap_vb->field = out_vb->field;
    1157                 :          0 :         cap_vb->flags &= ~mask;
    1158                 :          0 :         cap_vb->flags |= out_vb->flags & mask;
    1159                 :          0 :         cap_vb->vb2_buf.copied_timestamp = 1;
    1160                 :          0 : }
    1161                 :            : EXPORT_SYMBOL_GPL(v4l2_m2m_buf_copy_metadata);
    1162                 :            : 
    1163                 :          0 : void v4l2_m2m_request_queue(struct media_request *req)
    1164                 :            : {
    1165                 :            :         struct media_request_object *obj, *obj_safe;
    1166                 :            :         struct v4l2_m2m_ctx *m2m_ctx = NULL;
    1167                 :            : 
    1168                 :            :         /*
    1169                 :            :          * Queue all objects. Note that buffer objects are at the end of the
    1170                 :            :          * objects list, after all other object types. Once buffer objects
    1171                 :            :          * are queued, the driver might delete them immediately (if the driver
    1172                 :            :          * processes the buffer at once), so we have to use
    1173                 :            :          * list_for_each_entry_safe() to handle the case where the object we
    1174                 :            :          * queue is deleted.
    1175                 :            :          */
    1176         [ #  # ]:          0 :         list_for_each_entry_safe(obj, obj_safe, &req->objects, list) {
    1177                 :            :                 struct v4l2_m2m_ctx *m2m_ctx_obj;
    1178                 :            :                 struct vb2_buffer *vb;
    1179                 :            : 
    1180         [ #  # ]:          0 :                 if (!obj->ops->queue)
    1181                 :          0 :                         continue;
    1182                 :            : 
    1183         [ #  # ]:          0 :                 if (vb2_request_object_is_buffer(obj)) {
    1184                 :            :                         /* Sanity checks */
    1185                 :            :                         vb = container_of(obj, struct vb2_buffer, req_obj);
    1186   [ #  #  #  #  :          0 :                         WARN_ON(!V4L2_TYPE_IS_OUTPUT(vb->vb2_queue->type));
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
    1187                 :          0 :                         m2m_ctx_obj = container_of(vb->vb2_queue,
    1188                 :            :                                                    struct v4l2_m2m_ctx,
    1189                 :            :                                                    out_q_ctx.q);
    1190         [ #  # ]:          0 :                         WARN_ON(m2m_ctx && m2m_ctx_obj != m2m_ctx);
    1191                 :            :                         m2m_ctx = m2m_ctx_obj;
    1192                 :            :                 }
    1193                 :            : 
    1194                 :            :                 /*
    1195                 :            :                  * The buffer we queue here can in theory be immediately
    1196                 :            :                  * unbound, hence the use of list_for_each_entry_safe()
    1197                 :            :                  * above and why we call the queue op last.
    1198                 :            :                  */
    1199                 :          0 :                 obj->ops->queue(obj);
    1200                 :            :         }
    1201                 :            : 
    1202         [ #  # ]:          0 :         WARN_ON(!m2m_ctx);
    1203                 :            : 
    1204         [ #  # ]:          0 :         if (m2m_ctx)
    1205                 :          0 :                 v4l2_m2m_try_schedule(m2m_ctx);
    1206                 :          0 : }
    1207                 :            : EXPORT_SYMBOL_GPL(v4l2_m2m_request_queue);
    1208                 :            : 
    1209                 :            : /* Videobuf2 ioctl helpers */
    1210                 :            : 
    1211                 :          0 : int v4l2_m2m_ioctl_reqbufs(struct file *file, void *priv,
    1212                 :            :                                 struct v4l2_requestbuffers *rb)
    1213                 :            : {
    1214                 :          0 :         struct v4l2_fh *fh = file->private_data;
    1215                 :            : 
    1216                 :          0 :         return v4l2_m2m_reqbufs(file, fh->m2m_ctx, rb);
    1217                 :            : }
    1218                 :            : EXPORT_SYMBOL_GPL(v4l2_m2m_ioctl_reqbufs);
    1219                 :            : 
    1220                 :          0 : int v4l2_m2m_ioctl_create_bufs(struct file *file, void *priv,
    1221                 :            :                                 struct v4l2_create_buffers *create)
    1222                 :            : {
    1223                 :          0 :         struct v4l2_fh *fh = file->private_data;
    1224                 :            : 
    1225                 :          0 :         return v4l2_m2m_create_bufs(file, fh->m2m_ctx, create);
    1226                 :            : }
    1227                 :            : EXPORT_SYMBOL_GPL(v4l2_m2m_ioctl_create_bufs);
    1228                 :            : 
    1229                 :          0 : int v4l2_m2m_ioctl_querybuf(struct file *file, void *priv,
    1230                 :            :                                 struct v4l2_buffer *buf)
    1231                 :            : {
    1232                 :          0 :         struct v4l2_fh *fh = file->private_data;
    1233                 :            : 
    1234                 :          0 :         return v4l2_m2m_querybuf(file, fh->m2m_ctx, buf);
    1235                 :            : }
    1236                 :            : EXPORT_SYMBOL_GPL(v4l2_m2m_ioctl_querybuf);
    1237                 :            : 
    1238                 :          0 : int v4l2_m2m_ioctl_qbuf(struct file *file, void *priv,
    1239                 :            :                                 struct v4l2_buffer *buf)
    1240                 :            : {
    1241                 :          0 :         struct v4l2_fh *fh = file->private_data;
    1242                 :            : 
    1243                 :          0 :         return v4l2_m2m_qbuf(file, fh->m2m_ctx, buf);
    1244                 :            : }
    1245                 :            : EXPORT_SYMBOL_GPL(v4l2_m2m_ioctl_qbuf);
    1246                 :            : 
    1247                 :          0 : int v4l2_m2m_ioctl_dqbuf(struct file *file, void *priv,
    1248                 :            :                                 struct v4l2_buffer *buf)
    1249                 :            : {
    1250                 :          0 :         struct v4l2_fh *fh = file->private_data;
    1251                 :            : 
    1252                 :          0 :         return v4l2_m2m_dqbuf(file, fh->m2m_ctx, buf);
    1253                 :            : }
    1254                 :            : EXPORT_SYMBOL_GPL(v4l2_m2m_ioctl_dqbuf);
    1255                 :            : 
    1256                 :          0 : int v4l2_m2m_ioctl_prepare_buf(struct file *file, void *priv,
    1257                 :            :                                struct v4l2_buffer *buf)
    1258                 :            : {
    1259                 :          0 :         struct v4l2_fh *fh = file->private_data;
    1260                 :            : 
    1261                 :          0 :         return v4l2_m2m_prepare_buf(file, fh->m2m_ctx, buf);
    1262                 :            : }
    1263                 :            : EXPORT_SYMBOL_GPL(v4l2_m2m_ioctl_prepare_buf);
    1264                 :            : 
    1265                 :          0 : int v4l2_m2m_ioctl_expbuf(struct file *file, void *priv,
    1266                 :            :                                 struct v4l2_exportbuffer *eb)
    1267                 :            : {
    1268                 :          0 :         struct v4l2_fh *fh = file->private_data;
    1269                 :            : 
    1270                 :          0 :         return v4l2_m2m_expbuf(file, fh->m2m_ctx, eb);
    1271                 :            : }
    1272                 :            : EXPORT_SYMBOL_GPL(v4l2_m2m_ioctl_expbuf);
    1273                 :            : 
    1274                 :          0 : int v4l2_m2m_ioctl_streamon(struct file *file, void *priv,
    1275                 :            :                                 enum v4l2_buf_type type)
    1276                 :            : {
    1277                 :          0 :         struct v4l2_fh *fh = file->private_data;
    1278                 :            : 
    1279                 :          0 :         return v4l2_m2m_streamon(file, fh->m2m_ctx, type);
    1280                 :            : }
    1281                 :            : EXPORT_SYMBOL_GPL(v4l2_m2m_ioctl_streamon);
    1282                 :            : 
    1283                 :          0 : int v4l2_m2m_ioctl_streamoff(struct file *file, void *priv,
    1284                 :            :                                 enum v4l2_buf_type type)
    1285                 :            : {
    1286                 :          0 :         struct v4l2_fh *fh = file->private_data;
    1287                 :            : 
    1288                 :          0 :         return v4l2_m2m_streamoff(file, fh->m2m_ctx, type);
    1289                 :            : }
    1290                 :            : EXPORT_SYMBOL_GPL(v4l2_m2m_ioctl_streamoff);
    1291                 :            : 
    1292                 :          0 : int v4l2_m2m_ioctl_try_encoder_cmd(struct file *file, void *fh,
    1293                 :            :                                    struct v4l2_encoder_cmd *ec)
    1294                 :            : {
    1295         [ #  # ]:          0 :         if (ec->cmd != V4L2_ENC_CMD_STOP && ec->cmd != V4L2_ENC_CMD_START)
    1296                 :            :                 return -EINVAL;
    1297                 :            : 
    1298                 :          0 :         ec->flags = 0;
    1299                 :          0 :         return 0;
    1300                 :            : }
    1301                 :            : EXPORT_SYMBOL_GPL(v4l2_m2m_ioctl_try_encoder_cmd);
    1302                 :            : 
    1303                 :          0 : int v4l2_m2m_ioctl_try_decoder_cmd(struct file *file, void *fh,
    1304                 :            :                                    struct v4l2_decoder_cmd *dc)
    1305                 :            : {
    1306         [ #  # ]:          0 :         if (dc->cmd != V4L2_DEC_CMD_STOP && dc->cmd != V4L2_DEC_CMD_START)
    1307                 :            :                 return -EINVAL;
    1308                 :            : 
    1309                 :          0 :         dc->flags = 0;
    1310                 :            : 
    1311         [ #  # ]:          0 :         if (dc->cmd == V4L2_DEC_CMD_STOP) {
    1312                 :          0 :                 dc->stop.pts = 0;
    1313         [ #  # ]:          0 :         } else if (dc->cmd == V4L2_DEC_CMD_START) {
    1314                 :          0 :                 dc->start.speed = 0;
    1315                 :          0 :                 dc->start.format = V4L2_DEC_START_FMT_NONE;
    1316                 :            :         }
    1317                 :            :         return 0;
    1318                 :            : }
    1319                 :            : EXPORT_SYMBOL_GPL(v4l2_m2m_ioctl_try_decoder_cmd);
    1320                 :            : 
    1321                 :          0 : int v4l2_m2m_ioctl_stateless_try_decoder_cmd(struct file *file, void *fh,
    1322                 :            :                                              struct v4l2_decoder_cmd *dc)
    1323                 :            : {
    1324   [ #  #  #  # ]:          0 :         if (dc->cmd != V4L2_DEC_CMD_FLUSH)
    1325                 :            :                 return -EINVAL;
    1326                 :            : 
    1327                 :          0 :         dc->flags = 0;
    1328                 :            : 
    1329                 :          0 :         return 0;
    1330                 :            : }
    1331                 :            : EXPORT_SYMBOL_GPL(v4l2_m2m_ioctl_stateless_try_decoder_cmd);
    1332                 :            : 
    1333                 :          0 : int v4l2_m2m_ioctl_stateless_decoder_cmd(struct file *file, void *priv,
    1334                 :            :                                          struct v4l2_decoder_cmd *dc)
    1335                 :            : {
    1336                 :          0 :         struct v4l2_fh *fh = file->private_data;
    1337                 :            :         struct vb2_v4l2_buffer *out_vb, *cap_vb;
    1338                 :          0 :         struct v4l2_m2m_dev *m2m_dev = fh->m2m_ctx->m2m_dev;
    1339                 :            :         unsigned long flags;
    1340                 :            :         int ret;
    1341                 :            : 
    1342                 :            :         ret = v4l2_m2m_ioctl_stateless_try_decoder_cmd(file, priv, dc);
    1343         [ #  # ]:          0 :         if (ret < 0)
    1344                 :            :                 return ret;
    1345                 :            : 
    1346                 :          0 :         spin_lock_irqsave(&m2m_dev->job_spinlock, flags);
    1347                 :          0 :         out_vb = v4l2_m2m_last_src_buf(fh->m2m_ctx);
    1348                 :          0 :         cap_vb = v4l2_m2m_last_dst_buf(fh->m2m_ctx);
    1349                 :            : 
    1350                 :            :         /*
    1351                 :            :          * If there is an out buffer pending, then clear any HOLD flag.
    1352                 :            :          *
    1353                 :            :          * By clearing this flag we ensure that when this output
    1354                 :            :          * buffer is processed any held capture buffer will be released.
    1355                 :            :          */
    1356         [ #  # ]:          0 :         if (out_vb) {
    1357                 :          0 :                 out_vb->flags &= ~V4L2_BUF_FLAG_M2M_HOLD_CAPTURE_BUF;
    1358   [ #  #  #  # ]:          0 :         } else if (cap_vb && cap_vb->is_held) {
    1359                 :            :                 /*
    1360                 :            :                  * If there were no output buffers, but there is a
    1361                 :            :                  * capture buffer that is held, then release that
    1362                 :            :                  * buffer.
    1363                 :            :                  */
    1364                 :          0 :                 cap_vb->is_held = false;
    1365                 :          0 :                 v4l2_m2m_dst_buf_remove(fh->m2m_ctx);
    1366                 :            :                 v4l2_m2m_buf_done(cap_vb, VB2_BUF_STATE_DONE);
    1367                 :            :         }
    1368                 :            :         spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags);
    1369                 :            : 
    1370                 :          0 :         return 0;
    1371                 :            : }
    1372                 :            : EXPORT_SYMBOL_GPL(v4l2_m2m_ioctl_stateless_decoder_cmd);
    1373                 :            : 
    1374                 :            : /*
    1375                 :            :  * v4l2_file_operations helpers. It is assumed here same lock is used
    1376                 :            :  * for the output and the capture buffer queue.
    1377                 :            :  */
    1378                 :            : 
    1379                 :          0 : int v4l2_m2m_fop_mmap(struct file *file, struct vm_area_struct *vma)
    1380                 :            : {
    1381                 :          0 :         struct v4l2_fh *fh = file->private_data;
    1382                 :            : 
    1383                 :          0 :         return v4l2_m2m_mmap(file, fh->m2m_ctx, vma);
    1384                 :            : }
    1385                 :            : EXPORT_SYMBOL_GPL(v4l2_m2m_fop_mmap);
    1386                 :            : 
    1387                 :          0 : __poll_t v4l2_m2m_fop_poll(struct file *file, poll_table *wait)
    1388                 :            : {
    1389                 :          0 :         struct v4l2_fh *fh = file->private_data;
    1390                 :          0 :         struct v4l2_m2m_ctx *m2m_ctx = fh->m2m_ctx;
    1391                 :            :         __poll_t ret;
    1392                 :            : 
    1393         [ #  # ]:          0 :         if (m2m_ctx->q_lock)
    1394                 :          0 :                 mutex_lock(m2m_ctx->q_lock);
    1395                 :            : 
    1396                 :          0 :         ret = v4l2_m2m_poll(file, m2m_ctx, wait);
    1397                 :            : 
    1398         [ #  # ]:          0 :         if (m2m_ctx->q_lock)
    1399                 :          0 :                 mutex_unlock(m2m_ctx->q_lock);
    1400                 :            : 
    1401                 :          0 :         return ret;
    1402                 :            : }
    1403                 :            : EXPORT_SYMBOL_GPL(v4l2_m2m_fop_poll);
    1404                 :            : 

Generated by: LCOV version 1.14