LCOV - code coverage report
Current view: top level - drivers/media/common/videobuf2 - videobuf2-v4l2.c (source / functions) Hit Total Coverage
Test: gcov_data_raspi2_real_modules_combined.info Lines: 27 472 5.7 %
Date: 2020-09-30 20:25:40 Functions: 4 43 9.3 %
Branches: 18 524 3.4 %

           Branch data     Line data    Source code
       1                 :            : /*
       2                 :            :  * videobuf2-v4l2.c - V4L2 driver helper framework
       3                 :            :  *
       4                 :            :  * Copyright (C) 2010 Samsung Electronics
       5                 :            :  *
       6                 :            :  * Author: Pawel Osciak <pawel@osciak.com>
       7                 :            :  *         Marek Szyprowski <m.szyprowski@samsung.com>
       8                 :            :  *
       9                 :            :  * The vb2_thread implementation was based on code from videobuf-dvb.c:
      10                 :            :  *      (c) 2004 Gerd Knorr <kraxel@bytesex.org> [SUSE Labs]
      11                 :            :  *
      12                 :            :  * This program is free software; you can redistribute it and/or modify
      13                 :            :  * it under the terms of the GNU General Public License as published by
      14                 :            :  * the Free Software Foundation.
      15                 :            :  */
      16                 :            : 
      17                 :            : #include <linux/err.h>
      18                 :            : #include <linux/kernel.h>
      19                 :            : #include <linux/module.h>
      20                 :            : #include <linux/mm.h>
      21                 :            : #include <linux/poll.h>
      22                 :            : #include <linux/slab.h>
      23                 :            : #include <linux/sched.h>
      24                 :            : #include <linux/freezer.h>
      25                 :            : #include <linux/kthread.h>
      26                 :            : 
      27                 :            : #include <media/v4l2-dev.h>
      28                 :            : #include <media/v4l2-device.h>
      29                 :            : #include <media/v4l2-fh.h>
      30                 :            : #include <media/v4l2-event.h>
      31                 :            : #include <media/v4l2-common.h>
      32                 :            : 
      33                 :            : #include <media/videobuf2-v4l2.h>
      34                 :            : 
      35                 :            : static int debug;
      36                 :            : module_param(debug, int, 0644);
      37                 :            : 
      38                 :            : #define dprintk(level, fmt, arg...)                                           \
      39                 :            :         do {                                                                  \
      40                 :            :                 if (debug >= level)                                        \
      41                 :            :                         pr_info("vb2-v4l2: %s: " fmt, __func__, ## arg); \
      42                 :            :         } while (0)
      43                 :            : 
      44                 :            : /* Flags that are set by us */
      45                 :            : #define V4L2_BUFFER_MASK_FLAGS  (V4L2_BUF_FLAG_MAPPED | V4L2_BUF_FLAG_QUEUED | \
      46                 :            :                                  V4L2_BUF_FLAG_DONE | V4L2_BUF_FLAG_ERROR | \
      47                 :            :                                  V4L2_BUF_FLAG_PREPARED | \
      48                 :            :                                  V4L2_BUF_FLAG_IN_REQUEST | \
      49                 :            :                                  V4L2_BUF_FLAG_REQUEST_FD | \
      50                 :            :                                  V4L2_BUF_FLAG_TIMESTAMP_MASK)
      51                 :            : /* Output buffer flags that should be passed on to the driver */
      52                 :            : #define V4L2_BUFFER_OUT_FLAGS   (V4L2_BUF_FLAG_PFRAME | \
      53                 :            :                                  V4L2_BUF_FLAG_BFRAME | \
      54                 :            :                                  V4L2_BUF_FLAG_KEYFRAME | \
      55                 :            :                                  V4L2_BUF_FLAG_TIMECODE | \
      56                 :            :                                  V4L2_BUF_FLAG_M2M_HOLD_CAPTURE_BUF)
      57                 :            : 
      58                 :            : /*
      59                 :            :  * __verify_planes_array() - verify that the planes array passed in struct
      60                 :            :  * v4l2_buffer from userspace can be safely used
      61                 :            :  */
      62                 :          0 : static int __verify_planes_array(struct vb2_buffer *vb, const struct v4l2_buffer *b)
      63                 :            : {
      64         [ #  # ]:          0 :         if (!V4L2_TYPE_IS_MULTIPLANAR(b->type))
      65                 :            :                 return 0;
      66                 :            : 
      67                 :            :         /* Is memory for copying plane information present? */
      68         [ #  # ]:          0 :         if (b->m.planes == NULL) {
      69         [ #  # ]:          0 :                 dprintk(1, "multi-planar buffer passed but planes array not provided\n");
      70                 :            :                 return -EINVAL;
      71                 :            :         }
      72                 :            : 
      73   [ #  #  #  # ]:          0 :         if (b->length < vb->num_planes || b->length > VB2_MAX_PLANES) {
      74         [ #  # ]:          0 :                 dprintk(1, "incorrect planes array length, expected %d, got %d\n",
      75                 :            :                         vb->num_planes, b->length);
      76                 :            :                 return -EINVAL;
      77                 :            :         }
      78                 :            : 
      79                 :            :         return 0;
      80                 :            : }
      81                 :            : 
      82                 :          0 : static int __verify_planes_array_core(struct vb2_buffer *vb, const void *pb)
      83                 :            : {
      84                 :          0 :         return __verify_planes_array(vb, pb);
      85                 :            : }
      86                 :            : 
      87                 :            : /*
      88                 :            :  * __verify_length() - Verify that the bytesused value for each plane fits in
      89                 :            :  * the plane length and that the data offset doesn't exceed the bytesused value.
      90                 :            :  */
      91                 :          0 : static int __verify_length(struct vb2_buffer *vb, const struct v4l2_buffer *b)
      92                 :            : {
      93                 :            :         unsigned int length;
      94                 :            :         unsigned int bytesused;
      95                 :            :         unsigned int plane;
      96                 :            : 
      97   [ #  #  #  #  :          0 :         if (!V4L2_TYPE_IS_OUTPUT(b->type))
          #  #  #  #  #  
             #  #  #  #  
                      # ]
      98                 :            :                 return 0;
      99                 :            : 
     100         [ #  # ]:          0 :         if (V4L2_TYPE_IS_MULTIPLANAR(b->type)) {
     101         [ #  # ]:          0 :                 for (plane = 0; plane < vb->num_planes; ++plane) {
     102                 :          0 :                         length = (b->memory == VB2_MEMORY_USERPTR ||
     103                 :            :                                   b->memory == VB2_MEMORY_DMABUF)
     104                 :          0 :                                ? b->m.planes[plane].length
     105         [ #  # ]:          0 :                                 : vb->planes[plane].length;
     106                 :          0 :                         bytesused = b->m.planes[plane].bytesused
     107         [ #  # ]:          0 :                                   ? b->m.planes[plane].bytesused : length;
     108                 :            : 
     109         [ #  # ]:          0 :                         if (b->m.planes[plane].bytesused > length)
     110                 :            :                                 return -EINVAL;
     111                 :            : 
     112   [ #  #  #  # ]:          0 :                         if (b->m.planes[plane].data_offset > 0 &&
     113                 :            :                             b->m.planes[plane].data_offset >= bytesused)
     114                 :            :                                 return -EINVAL;
     115                 :            :                 }
     116                 :            :         } else {
     117                 :          0 :                 length = (b->memory == VB2_MEMORY_USERPTR)
     118         [ #  # ]:          0 :                         ? b->length : vb->planes[0].length;
     119                 :            : 
     120         [ #  # ]:          0 :                 if (b->bytesused > length)
     121                 :            :                         return -EINVAL;
     122                 :            :         }
     123                 :            : 
     124                 :          0 :         return 0;
     125                 :            : }
     126                 :            : 
     127                 :            : /*
     128                 :            :  * __init_vb2_v4l2_buffer() - initialize the vb2_v4l2_buffer struct
     129                 :            :  */
     130                 :          0 : static void __init_vb2_v4l2_buffer(struct vb2_buffer *vb)
     131                 :            : {
     132                 :            :         struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
     133                 :            : 
     134                 :          0 :         vbuf->request_fd = -1;
     135                 :          0 : }
     136                 :            : 
     137                 :          0 : static void __copy_timestamp(struct vb2_buffer *vb, const void *pb)
     138                 :            : {
     139                 :            :         const struct v4l2_buffer *b = pb;
     140                 :            :         struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
     141                 :          0 :         struct vb2_queue *q = vb->vb2_queue;
     142                 :            : 
     143         [ #  # ]:          0 :         if (q->is_output) {
     144                 :            :                 /*
     145                 :            :                  * For output buffers copy the timestamp if needed,
     146                 :            :                  * and the timecode field and flag if needed.
     147                 :            :                  */
     148         [ #  # ]:          0 :                 if (q->copy_timestamp)
     149                 :          0 :                         vb->timestamp = v4l2_timeval_to_ns(&b->timestamp);
     150                 :          0 :                 vbuf->flags |= b->flags & V4L2_BUF_FLAG_TIMECODE;
     151         [ #  # ]:          0 :                 if (b->flags & V4L2_BUF_FLAG_TIMECODE)
     152                 :          0 :                         vbuf->timecode = b->timecode;
     153                 :            :         }
     154                 :          0 : };
     155                 :            : 
     156                 :          0 : static void vb2_warn_zero_bytesused(struct vb2_buffer *vb)
     157                 :            : {
     158                 :            :         static bool check_once;
     159                 :            : 
     160         [ #  # ]:          0 :         if (check_once)
     161                 :          0 :                 return;
     162                 :            : 
     163                 :          0 :         check_once = true;
     164                 :            : 
     165                 :          0 :         pr_warn("use of bytesused == 0 is deprecated and will be removed in the future,\n");
     166         [ #  # ]:          0 :         if (vb->vb2_queue->allow_zero_bytesused)
     167                 :          0 :                 pr_warn("use VIDIOC_DECODER_CMD(V4L2_DEC_CMD_STOP) instead.\n");
     168                 :            :         else
     169                 :          0 :                 pr_warn("use the actual size instead.\n");
     170                 :            : }
     171                 :            : 
     172                 :          0 : static int vb2_fill_vb2_v4l2_buffer(struct vb2_buffer *vb, struct v4l2_buffer *b)
     173                 :            : {
     174                 :          0 :         struct vb2_queue *q = vb->vb2_queue;
     175                 :            :         struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
     176                 :          0 :         struct vb2_plane *planes = vbuf->planes;
     177                 :            :         unsigned int plane;
     178                 :            :         int ret;
     179                 :            : 
     180                 :          0 :         ret = __verify_length(vb, b);
     181         [ #  # ]:          0 :         if (ret < 0) {
     182         [ #  # ]:          0 :                 dprintk(1, "plane parameters verification failed: %d\n", ret);
     183                 :          0 :                 return ret;
     184                 :            :         }
     185   [ #  #  #  # ]:          0 :         if (b->field == V4L2_FIELD_ALTERNATE && q->is_output) {
     186                 :            :                 /*
     187                 :            :                  * If the format's field is ALTERNATE, then the buffer's field
     188                 :            :                  * should be either TOP or BOTTOM, not ALTERNATE since that
     189                 :            :                  * makes no sense. The driver has to know whether the
     190                 :            :                  * buffer represents a top or a bottom field in order to
     191                 :            :                  * program any DMA correctly. Using ALTERNATE is wrong, since
     192                 :            :                  * that just says that it is either a top or a bottom field,
     193                 :            :                  * but not which of the two it is.
     194                 :            :                  */
     195         [ #  # ]:          0 :                 dprintk(1, "the field is incorrectly set to ALTERNATE for an output buffer\n");
     196                 :            :                 return -EINVAL;
     197                 :            :         }
     198                 :          0 :         vbuf->sequence = 0;
     199                 :          0 :         vbuf->request_fd = -1;
     200                 :          0 :         vbuf->is_held = false;
     201                 :            : 
     202         [ #  # ]:          0 :         if (V4L2_TYPE_IS_MULTIPLANAR(b->type)) {
     203      [ #  #  # ]:          0 :                 switch (b->memory) {
     204                 :            :                 case VB2_MEMORY_USERPTR:
     205         [ #  # ]:          0 :                         for (plane = 0; plane < vb->num_planes; ++plane) {
     206                 :          0 :                                 planes[plane].m.userptr =
     207                 :          0 :                                         b->m.planes[plane].m.userptr;
     208                 :          0 :                                 planes[plane].length =
     209                 :          0 :                                         b->m.planes[plane].length;
     210                 :            :                         }
     211                 :            :                         break;
     212                 :            :                 case VB2_MEMORY_DMABUF:
     213         [ #  # ]:          0 :                         for (plane = 0; plane < vb->num_planes; ++plane) {
     214                 :          0 :                                 planes[plane].m.fd =
     215                 :          0 :                                         b->m.planes[plane].m.fd;
     216                 :          0 :                                 planes[plane].length =
     217                 :          0 :                                         b->m.planes[plane].length;
     218                 :            :                         }
     219                 :            :                         break;
     220                 :            :                 default:
     221         [ #  # ]:          0 :                         for (plane = 0; plane < vb->num_planes; ++plane) {
     222                 :          0 :                                 planes[plane].m.offset =
     223                 :          0 :                                         vb->planes[plane].m.offset;
     224                 :          0 :                                 planes[plane].length =
     225                 :          0 :                                         vb->planes[plane].length;
     226                 :            :                         }
     227                 :            :                         break;
     228                 :            :                 }
     229                 :            : 
     230                 :            :                 /* Fill in driver-provided information for OUTPUT types */
     231   [ #  #  #  #  :          0 :                 if (V4L2_TYPE_IS_OUTPUT(b->type)) {
          #  #  #  #  #  
             #  #  #  #  
                      # ]
     232                 :            :                         /*
     233                 :            :                          * Will have to go up to b->length when API starts
     234                 :            :                          * accepting variable number of planes.
     235                 :            :                          *
     236                 :            :                          * If bytesused == 0 for the output buffer, then fall
     237                 :            :                          * back to the full buffer size. In that case
     238                 :            :                          * userspace clearly never bothered to set it and
     239                 :            :                          * it's a safe assumption that they really meant to
     240                 :            :                          * use the full plane sizes.
     241                 :            :                          *
     242                 :            :                          * Some drivers, e.g. old codec drivers, use bytesused == 0
     243                 :            :                          * as a way to indicate that streaming is finished.
     244                 :            :                          * In that case, the driver should use the
     245                 :            :                          * allow_zero_bytesused flag to keep old userspace
     246                 :            :                          * applications working.
     247                 :            :                          */
     248         [ #  # ]:          0 :                         for (plane = 0; plane < vb->num_planes; ++plane) {
     249                 :          0 :                                 struct vb2_plane *pdst = &planes[plane];
     250                 :          0 :                                 struct v4l2_plane *psrc = &b->m.planes[plane];
     251                 :            : 
     252         [ #  # ]:          0 :                                 if (psrc->bytesused == 0)
     253                 :          0 :                                         vb2_warn_zero_bytesused(vb);
     254                 :            : 
     255         [ #  # ]:          0 :                                 if (vb->vb2_queue->allow_zero_bytesused)
     256                 :          0 :                                         pdst->bytesused = psrc->bytesused;
     257                 :            :                                 else
     258                 :          0 :                                         pdst->bytesused = psrc->bytesused ?
     259         [ #  # ]:          0 :                                                 psrc->bytesused : pdst->length;
     260                 :          0 :                                 pdst->data_offset = psrc->data_offset;
     261                 :            :                         }
     262                 :            :                 }
     263                 :            :         } else {
     264                 :            :                 /*
     265                 :            :                  * Single-planar buffers do not use planes array,
     266                 :            :                  * so fill in relevant v4l2_buffer struct fields instead.
     267                 :            :                  * In videobuf we use our internal V4l2_planes struct for
     268                 :            :                  * single-planar buffers as well, for simplicity.
     269                 :            :                  *
     270                 :            :                  * If bytesused == 0 for the output buffer, then fall back
     271                 :            :                  * to the full buffer size as that's a sensible default.
     272                 :            :                  *
     273                 :            :                  * Some drivers, e.g. old codec drivers, use bytesused == 0 as
     274                 :            :                  * a way to indicate that streaming is finished. In that case,
     275                 :            :                  * the driver should use the allow_zero_bytesused flag to keep
     276                 :            :                  * old userspace applications working.
     277                 :            :                  */
     278      [ #  #  # ]:          0 :                 switch (b->memory) {
     279                 :            :                 case VB2_MEMORY_USERPTR:
     280                 :          0 :                         planes[0].m.userptr = b->m.userptr;
     281                 :          0 :                         planes[0].length = b->length;
     282                 :          0 :                         break;
     283                 :            :                 case VB2_MEMORY_DMABUF:
     284                 :          0 :                         planes[0].m.fd = b->m.fd;
     285                 :          0 :                         planes[0].length = b->length;
     286                 :          0 :                         break;
     287                 :            :                 default:
     288                 :          0 :                         planes[0].m.offset = vb->planes[0].m.offset;
     289                 :          0 :                         planes[0].length = vb->planes[0].length;
     290                 :          0 :                         break;
     291                 :            :                 }
     292                 :            : 
     293                 :          0 :                 planes[0].data_offset = 0;
     294   [ #  #  #  #  :          0 :                 if (V4L2_TYPE_IS_OUTPUT(b->type)) {
          #  #  #  #  #  
             #  #  #  #  
                      # ]
     295         [ #  # ]:          0 :                         if (b->bytesused == 0)
     296                 :          0 :                                 vb2_warn_zero_bytesused(vb);
     297                 :            : 
     298         [ #  # ]:          0 :                         if (vb->vb2_queue->allow_zero_bytesused)
     299                 :          0 :                                 planes[0].bytesused = b->bytesused;
     300                 :            :                         else
     301                 :          0 :                                 planes[0].bytesused = b->bytesused ?
     302         [ #  # ]:          0 :                                         b->bytesused : planes[0].length;
     303                 :            :                 } else
     304                 :          0 :                         planes[0].bytesused = 0;
     305                 :            : 
     306                 :            :         }
     307                 :            : 
     308                 :            :         /* Zero flags that we handle */
     309                 :          0 :         vbuf->flags = b->flags & ~V4L2_BUFFER_MASK_FLAGS;
     310   [ #  #  #  #  :          0 :         if (!vb->vb2_queue->copy_timestamp || !V4L2_TYPE_IS_OUTPUT(b->type)) {
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
     311                 :            :                 /*
     312                 :            :                  * Non-COPY timestamps and non-OUTPUT queues will get
     313                 :            :                  * their timestamp and timestamp source flags from the
     314                 :            :                  * queue.
     315                 :            :                  */
     316                 :          0 :                 vbuf->flags &= ~V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
     317                 :            :         }
     318                 :            : 
     319   [ #  #  #  #  :          0 :         if (V4L2_TYPE_IS_OUTPUT(b->type)) {
          #  #  #  #  #  
             #  #  #  #  
                      # ]
     320                 :            :                 /*
     321                 :            :                  * For output buffers mask out the timecode flag:
     322                 :            :                  * this will be handled later in vb2_qbuf().
     323                 :            :                  * The 'field' is valid metadata for this output buffer
     324                 :            :                  * and so that needs to be copied here.
     325                 :            :                  */
     326                 :          0 :                 vbuf->flags &= ~V4L2_BUF_FLAG_TIMECODE;
     327                 :          0 :                 vbuf->field = b->field;
     328         [ #  # ]:          0 :                 if (!(q->subsystem_flags & VB2_V4L2_FL_SUPPORTS_M2M_HOLD_CAPTURE_BUF))
     329                 :          0 :                         vbuf->flags &= ~V4L2_BUF_FLAG_M2M_HOLD_CAPTURE_BUF;
     330                 :            :         } else {
     331                 :            :                 /* Zero any output buffer flags as this is a capture buffer */
     332                 :          0 :                 vbuf->flags &= ~V4L2_BUFFER_OUT_FLAGS;
     333                 :            :                 /* Zero last flag, this is a signal from driver to userspace */
     334                 :          0 :                 vbuf->flags &= ~V4L2_BUF_FLAG_LAST;
     335                 :            :         }
     336                 :            : 
     337                 :            :         return 0;
     338                 :            : }
     339                 :            : 
     340                 :          0 : static int vb2_queue_or_prepare_buf(struct vb2_queue *q, struct media_device *mdev,
     341                 :            :                                     struct v4l2_buffer *b, bool is_prepare,
     342                 :            :                                     struct media_request **p_req)
     343                 :            : {
     344         [ #  # ]:          0 :         const char *opname = is_prepare ? "prepare_buf" : "qbuf";
     345                 :            :         struct media_request *req;
     346                 :            :         struct vb2_v4l2_buffer *vbuf;
     347                 :            :         struct vb2_buffer *vb;
     348                 :            :         int ret;
     349                 :            : 
     350         [ #  # ]:          0 :         if (b->type != q->type) {
     351         [ #  # ]:          0 :                 dprintk(1, "%s: invalid buffer type\n", opname);
     352                 :            :                 return -EINVAL;
     353                 :            :         }
     354                 :            : 
     355         [ #  # ]:          0 :         if (b->index >= q->num_buffers) {
     356         [ #  # ]:          0 :                 dprintk(1, "%s: buffer index out of range\n", opname);
     357                 :            :                 return -EINVAL;
     358                 :            :         }
     359                 :            : 
     360         [ #  # ]:          0 :         if (q->bufs[b->index] == NULL) {
     361                 :            :                 /* Should never happen */
     362         [ #  # ]:          0 :                 dprintk(1, "%s: buffer is NULL\n", opname);
     363                 :            :                 return -EINVAL;
     364                 :            :         }
     365                 :            : 
     366         [ #  # ]:          0 :         if (b->memory != q->memory) {
     367         [ #  # ]:          0 :                 dprintk(1, "%s: invalid memory type\n", opname);
     368                 :            :                 return -EINVAL;
     369                 :            :         }
     370                 :            : 
     371                 :            :         vb = q->bufs[b->index];
     372                 :            :         vbuf = to_vb2_v4l2_buffer(vb);
     373                 :          0 :         ret = __verify_planes_array(vb, b);
     374         [ #  # ]:          0 :         if (ret)
     375                 :            :                 return ret;
     376                 :            : 
     377   [ #  #  #  #  :          0 :         if (!is_prepare && (b->flags & V4L2_BUF_FLAG_REQUEST_FD) &&
                   #  # ]
     378                 :          0 :             vb->state != VB2_BUF_STATE_DEQUEUED) {
     379         [ #  # ]:          0 :                 dprintk(1, "%s: buffer is not in dequeued state\n", opname);
     380                 :            :                 return -EINVAL;
     381                 :            :         }
     382                 :            : 
     383         [ #  # ]:          0 :         if (!vb->prepared) {
     384                 :            :                 /* Copy relevant information provided by the userspace */
     385                 :          0 :                 memset(vbuf->planes, 0,
     386                 :          0 :                        sizeof(vbuf->planes[0]) * vb->num_planes);
     387                 :          0 :                 ret = vb2_fill_vb2_v4l2_buffer(vb, b);
     388         [ #  # ]:          0 :                 if (ret)
     389                 :            :                         return ret;
     390                 :            :         }
     391                 :            : 
     392         [ #  # ]:          0 :         if (is_prepare)
     393                 :            :                 return 0;
     394                 :            : 
     395         [ #  # ]:          0 :         if (!(b->flags & V4L2_BUF_FLAG_REQUEST_FD)) {
     396         [ #  # ]:          0 :                 if (q->requires_requests) {
     397         [ #  # ]:          0 :                         dprintk(1, "%s: queue requires requests\n", opname);
     398                 :            :                         return -EBADR;
     399                 :            :                 }
     400         [ #  # ]:          0 :                 if (q->uses_requests) {
     401         [ #  # ]:          0 :                         dprintk(1, "%s: queue uses requests\n", opname);
     402                 :            :                         return -EBUSY;
     403                 :            :                 }
     404                 :            :                 return 0;
     405         [ #  # ]:          0 :         } else if (!q->supports_requests) {
     406         [ #  # ]:          0 :                 dprintk(1, "%s: queue does not support requests\n", opname);
     407                 :            :                 return -EBADR;
     408         [ #  # ]:          0 :         } else if (q->uses_qbuf) {
     409         [ #  # ]:          0 :                 dprintk(1, "%s: queue does not use requests\n", opname);
     410                 :            :                 return -EBUSY;
     411                 :            :         }
     412                 :            : 
     413                 :            :         /*
     414                 :            :          * For proper locking when queueing a request you need to be able
     415                 :            :          * to lock access to the vb2 queue, so check that there is a lock
     416                 :            :          * that we can use. In addition p_req must be non-NULL.
     417                 :            :          */
     418   [ #  #  #  #  :          0 :         if (WARN_ON(!q->lock || !p_req))
             #  #  #  # ]
     419                 :            :                 return -EINVAL;
     420                 :            : 
     421                 :            :         /*
     422                 :            :          * Make sure this op is implemented by the driver. It's easy to forget
     423                 :            :          * this callback, but is it important when canceling a buffer in a
     424                 :            :          * queued request.
     425                 :            :          */
     426   [ #  #  #  # ]:          0 :         if (WARN_ON(!q->ops->buf_request_complete))
     427                 :            :                 return -EINVAL;
     428                 :            :         /*
     429                 :            :          * Make sure this op is implemented by the driver for the output queue.
     430                 :            :          * It's easy to forget this callback, but is it important to correctly
     431                 :            :          * validate the 'field' value at QBUF time.
     432                 :            :          */
     433   [ #  #  #  #  :          0 :         if (WARN_ON((q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT ||
             #  #  #  # ]
     434                 :            :                      q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) &&
     435                 :            :                     !q->ops->buf_out_validate))
     436                 :            :                 return -EINVAL;
     437                 :            : 
     438         [ #  # ]:          0 :         if (b->request_fd < 0) {
     439         [ #  # ]:          0 :                 dprintk(1, "%s: request_fd < 0\n", opname);
     440                 :            :                 return -EINVAL;
     441                 :            :         }
     442                 :            : 
     443                 :          0 :         req = media_request_get_by_fd(mdev, b->request_fd);
     444         [ #  # ]:          0 :         if (IS_ERR(req)) {
     445         [ #  # ]:          0 :                 dprintk(1, "%s: invalid request_fd\n", opname);
     446                 :          0 :                 return PTR_ERR(req);
     447                 :            :         }
     448                 :            : 
     449                 :            :         /*
     450                 :            :          * Early sanity check. This is checked again when the buffer
     451                 :            :          * is bound to the request in vb2_core_qbuf().
     452                 :            :          */
     453         [ #  # ]:          0 :         if (req->state != MEDIA_REQUEST_STATE_IDLE &&
     454                 :            :             req->state != MEDIA_REQUEST_STATE_UPDATING) {
     455         [ #  # ]:          0 :                 dprintk(1, "%s: request is not idle\n", opname);
     456                 :          0 :                 media_request_put(req);
     457                 :          0 :                 return -EBUSY;
     458                 :            :         }
     459                 :            : 
     460                 :          0 :         *p_req = req;
     461                 :          0 :         vbuf->request_fd = b->request_fd;
     462                 :            : 
     463                 :          0 :         return 0;
     464                 :            : }
     465                 :            : 
     466                 :            : /*
     467                 :            :  * __fill_v4l2_buffer() - fill in a struct v4l2_buffer with information to be
     468                 :            :  * returned to userspace
     469                 :            :  */
     470                 :          0 : static void __fill_v4l2_buffer(struct vb2_buffer *vb, void *pb)
     471                 :            : {
     472                 :            :         struct v4l2_buffer *b = pb;
     473                 :            :         struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
     474                 :          0 :         struct vb2_queue *q = vb->vb2_queue;
     475                 :            :         unsigned int plane;
     476                 :            : 
     477                 :            :         /* Copy back data such as timestamp, flags, etc. */
     478                 :          0 :         b->index = vb->index;
     479                 :          0 :         b->type = vb->type;
     480                 :          0 :         b->memory = vb->memory;
     481                 :          0 :         b->bytesused = 0;
     482                 :            : 
     483                 :          0 :         b->flags = vbuf->flags;
     484                 :          0 :         b->field = vbuf->field;
     485                 :          0 :         b->timestamp = ns_to_timeval(vb->timestamp);
     486                 :          0 :         b->timecode = vbuf->timecode;
     487                 :          0 :         b->sequence = vbuf->sequence;
     488                 :          0 :         b->reserved2 = 0;
     489                 :          0 :         b->request_fd = 0;
     490                 :            : 
     491         [ #  # ]:          0 :         if (q->is_multiplanar) {
     492                 :            :                 /*
     493                 :            :                  * Fill in plane-related data if userspace provided an array
     494                 :            :                  * for it. The caller has already verified memory and size.
     495                 :            :                  */
     496                 :          0 :                 b->length = vb->num_planes;
     497         [ #  # ]:          0 :                 for (plane = 0; plane < vb->num_planes; ++plane) {
     498                 :          0 :                         struct v4l2_plane *pdst = &b->m.planes[plane];
     499                 :            :                         struct vb2_plane *psrc = &vb->planes[plane];
     500                 :            : 
     501                 :          0 :                         pdst->bytesused = psrc->bytesused;
     502                 :          0 :                         pdst->length = psrc->length;
     503         [ #  # ]:          0 :                         if (q->memory == VB2_MEMORY_MMAP)
     504                 :          0 :                                 pdst->m.mem_offset = psrc->m.offset;
     505         [ #  # ]:          0 :                         else if (q->memory == VB2_MEMORY_USERPTR)
     506                 :          0 :                                 pdst->m.userptr = psrc->m.userptr;
     507         [ #  # ]:          0 :                         else if (q->memory == VB2_MEMORY_DMABUF)
     508                 :          0 :                                 pdst->m.fd = psrc->m.fd;
     509                 :          0 :                         pdst->data_offset = psrc->data_offset;
     510                 :          0 :                         memset(pdst->reserved, 0, sizeof(pdst->reserved));
     511                 :            :                 }
     512                 :            :         } else {
     513                 :            :                 /*
     514                 :            :                  * We use length and offset in v4l2_planes array even for
     515                 :            :                  * single-planar buffers, but userspace does not.
     516                 :            :                  */
     517                 :          0 :                 b->length = vb->planes[0].length;
     518                 :          0 :                 b->bytesused = vb->planes[0].bytesused;
     519         [ #  # ]:          0 :                 if (q->memory == VB2_MEMORY_MMAP)
     520                 :          0 :                         b->m.offset = vb->planes[0].m.offset;
     521         [ #  # ]:          0 :                 else if (q->memory == VB2_MEMORY_USERPTR)
     522                 :          0 :                         b->m.userptr = vb->planes[0].m.userptr;
     523         [ #  # ]:          0 :                 else if (q->memory == VB2_MEMORY_DMABUF)
     524                 :          0 :                         b->m.fd = vb->planes[0].m.fd;
     525                 :            :         }
     526                 :            : 
     527                 :            :         /*
     528                 :            :          * Clear any buffer state related flags.
     529                 :            :          */
     530                 :          0 :         b->flags &= ~V4L2_BUFFER_MASK_FLAGS;
     531                 :          0 :         b->flags |= q->timestamp_flags & V4L2_BUF_FLAG_TIMESTAMP_MASK;
     532         [ #  # ]:          0 :         if (!q->copy_timestamp) {
     533                 :            :                 /*
     534                 :            :                  * For non-COPY timestamps, drop timestamp source bits
     535                 :            :                  * and obtain the timestamp source from the queue.
     536                 :            :                  */
     537                 :          0 :                 b->flags &= ~V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
     538                 :          0 :                 b->flags |= q->timestamp_flags & V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
     539                 :            :         }
     540                 :            : 
     541   [ #  #  #  #  :          0 :         switch (vb->state) {
                      # ]
     542                 :            :         case VB2_BUF_STATE_QUEUED:
     543                 :            :         case VB2_BUF_STATE_ACTIVE:
     544                 :          0 :                 b->flags |= V4L2_BUF_FLAG_QUEUED;
     545                 :          0 :                 break;
     546                 :            :         case VB2_BUF_STATE_IN_REQUEST:
     547                 :          0 :                 b->flags |= V4L2_BUF_FLAG_IN_REQUEST;
     548                 :          0 :                 break;
     549                 :            :         case VB2_BUF_STATE_ERROR:
     550                 :          0 :                 b->flags |= V4L2_BUF_FLAG_ERROR;
     551                 :            :                 /* fall through */
     552                 :            :         case VB2_BUF_STATE_DONE:
     553                 :          0 :                 b->flags |= V4L2_BUF_FLAG_DONE;
     554                 :          0 :                 break;
     555                 :            :         case VB2_BUF_STATE_PREPARING:
     556                 :            :         case VB2_BUF_STATE_DEQUEUED:
     557                 :            :                 /* nothing */
     558                 :            :                 break;
     559                 :            :         }
     560                 :            : 
     561         [ #  # ]:          0 :         if ((vb->state == VB2_BUF_STATE_DEQUEUED ||
     562                 :            :              vb->state == VB2_BUF_STATE_IN_REQUEST) &&
     563         [ #  # ]:          0 :             vb->synced && vb->prepared)
     564                 :          0 :                 b->flags |= V4L2_BUF_FLAG_PREPARED;
     565                 :            : 
     566         [ #  # ]:          0 :         if (vb2_buffer_in_use(q, vb))
     567                 :          0 :                 b->flags |= V4L2_BUF_FLAG_MAPPED;
     568         [ #  # ]:          0 :         if (vbuf->request_fd >= 0) {
     569                 :          0 :                 b->flags |= V4L2_BUF_FLAG_REQUEST_FD;
     570                 :          0 :                 b->request_fd = vbuf->request_fd;
     571                 :            :         }
     572                 :          0 : }
     573                 :            : 
     574                 :            : /*
     575                 :            :  * __fill_vb2_buffer() - fill a vb2_buffer with information provided in a
     576                 :            :  * v4l2_buffer by the userspace. It also verifies that struct
     577                 :            :  * v4l2_buffer has a valid number of planes.
     578                 :            :  */
     579                 :          0 : static int __fill_vb2_buffer(struct vb2_buffer *vb, struct vb2_plane *planes)
     580                 :            : {
     581                 :            :         struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
     582                 :            :         unsigned int plane;
     583                 :            : 
     584         [ #  # ]:          0 :         if (!vb->vb2_queue->copy_timestamp)
     585                 :          0 :                 vb->timestamp = 0;
     586                 :            : 
     587         [ #  # ]:          0 :         for (plane = 0; plane < vb->num_planes; ++plane) {
     588         [ #  # ]:          0 :                 if (vb->vb2_queue->memory != VB2_MEMORY_MMAP) {
     589                 :          0 :                         planes[plane].m = vbuf->planes[plane].m;
     590                 :          0 :                         planes[plane].length = vbuf->planes[plane].length;
     591                 :            :                 }
     592                 :          0 :                 planes[plane].bytesused = vbuf->planes[plane].bytesused;
     593                 :          0 :                 planes[plane].data_offset = vbuf->planes[plane].data_offset;
     594                 :            :         }
     595                 :          0 :         return 0;
     596                 :            : }
     597                 :            : 
     598                 :            : static const struct vb2_buf_ops v4l2_buf_ops = {
     599                 :            :         .verify_planes_array    = __verify_planes_array_core,
     600                 :            :         .init_buffer            = __init_vb2_v4l2_buffer,
     601                 :            :         .fill_user_buffer       = __fill_v4l2_buffer,
     602                 :            :         .fill_vb2_buffer        = __fill_vb2_buffer,
     603                 :            :         .copy_timestamp         = __copy_timestamp,
     604                 :            : };
     605                 :            : 
     606                 :          0 : int vb2_find_timestamp(const struct vb2_queue *q, u64 timestamp,
     607                 :            :                        unsigned int start_idx)
     608                 :            : {
     609                 :            :         unsigned int i;
     610                 :            : 
     611         [ #  # ]:          0 :         for (i = start_idx; i < q->num_buffers; i++)
     612   [ #  #  #  # ]:          0 :                 if (q->bufs[i]->copied_timestamp &&
     613                 :          0 :                     q->bufs[i]->timestamp == timestamp)
     614                 :          0 :                         return i;
     615                 :            :         return -1;
     616                 :            : }
     617                 :            : EXPORT_SYMBOL_GPL(vb2_find_timestamp);
     618                 :            : 
     619                 :            : /*
     620                 :            :  * vb2_querybuf() - query video buffer information
     621                 :            :  * @q:          videobuf queue
     622                 :            :  * @b:          buffer struct passed from userspace to vidioc_querybuf handler
     623                 :            :  *              in driver
     624                 :            :  *
     625                 :            :  * Should be called from vidioc_querybuf ioctl handler in driver.
     626                 :            :  * This function will verify the passed v4l2_buffer structure and fill the
     627                 :            :  * relevant information for the userspace.
     628                 :            :  *
     629                 :            :  * The return values from this function are intended to be directly returned
     630                 :            :  * from vidioc_querybuf handler in driver.
     631                 :            :  */
     632                 :          0 : int vb2_querybuf(struct vb2_queue *q, struct v4l2_buffer *b)
     633                 :            : {
     634                 :            :         struct vb2_buffer *vb;
     635                 :            :         int ret;
     636                 :            : 
     637         [ #  # ]:          0 :         if (b->type != q->type) {
     638         [ #  # ]:          0 :                 dprintk(1, "wrong buffer type\n");
     639                 :            :                 return -EINVAL;
     640                 :            :         }
     641                 :            : 
     642         [ #  # ]:          0 :         if (b->index >= q->num_buffers) {
     643         [ #  # ]:          0 :                 dprintk(1, "buffer index out of range\n");
     644                 :            :                 return -EINVAL;
     645                 :            :         }
     646                 :          0 :         vb = q->bufs[b->index];
     647                 :          0 :         ret = __verify_planes_array(vb, b);
     648         [ #  # ]:          0 :         if (!ret)
     649                 :          0 :                 vb2_core_querybuf(q, b->index, b);
     650                 :          0 :         return ret;
     651                 :            : }
     652                 :            : EXPORT_SYMBOL(vb2_querybuf);
     653                 :            : 
     654                 :          0 : static void fill_buf_caps(struct vb2_queue *q, u32 *caps)
     655                 :            : {
     656                 :          0 :         *caps = V4L2_BUF_CAP_SUPPORTS_ORPHANED_BUFS;
     657         [ #  # ]:          0 :         if (q->io_modes & VB2_MMAP)
     658                 :          0 :                 *caps |= V4L2_BUF_CAP_SUPPORTS_MMAP;
     659         [ #  # ]:          0 :         if (q->io_modes & VB2_USERPTR)
     660                 :          0 :                 *caps |= V4L2_BUF_CAP_SUPPORTS_USERPTR;
     661         [ #  # ]:          0 :         if (q->io_modes & VB2_DMABUF)
     662                 :          0 :                 *caps |= V4L2_BUF_CAP_SUPPORTS_DMABUF;
     663         [ #  # ]:          0 :         if (q->subsystem_flags & VB2_V4L2_FL_SUPPORTS_M2M_HOLD_CAPTURE_BUF)
     664                 :          0 :                 *caps |= V4L2_BUF_CAP_SUPPORTS_M2M_HOLD_CAPTURE_BUF;
     665                 :            : #ifdef CONFIG_MEDIA_CONTROLLER_REQUEST_API
     666                 :            :         if (q->supports_requests)
     667                 :            :                 *caps |= V4L2_BUF_CAP_SUPPORTS_REQUESTS;
     668                 :            : #endif
     669                 :          0 : }
     670                 :            : 
     671                 :          0 : int vb2_reqbufs(struct vb2_queue *q, struct v4l2_requestbuffers *req)
     672                 :            : {
     673                 :          0 :         int ret = vb2_verify_memory_type(q, req->memory, req->type);
     674                 :            : 
     675                 :          0 :         fill_buf_caps(q, &req->capabilities);
     676         [ #  # ]:          0 :         return ret ? ret : vb2_core_reqbufs(q, req->memory, &req->count);
     677                 :            : }
     678                 :            : EXPORT_SYMBOL_GPL(vb2_reqbufs);
     679                 :            : 
     680                 :          0 : int vb2_prepare_buf(struct vb2_queue *q, struct media_device *mdev,
     681                 :            :                     struct v4l2_buffer *b)
     682                 :            : {
     683                 :            :         int ret;
     684                 :            : 
     685         [ #  # ]:          0 :         if (vb2_fileio_is_active(q)) {
     686         [ #  # ]:          0 :                 dprintk(1, "file io in progress\n");
     687                 :            :                 return -EBUSY;
     688                 :            :         }
     689                 :            : 
     690         [ #  # ]:          0 :         if (b->flags & V4L2_BUF_FLAG_REQUEST_FD)
     691                 :            :                 return -EINVAL;
     692                 :            : 
     693                 :          0 :         ret = vb2_queue_or_prepare_buf(q, mdev, b, true, NULL);
     694                 :            : 
     695         [ #  # ]:          0 :         return ret ? ret : vb2_core_prepare_buf(q, b->index, b);
     696                 :            : }
     697                 :            : EXPORT_SYMBOL_GPL(vb2_prepare_buf);
     698                 :            : 
     699                 :          0 : int vb2_create_bufs(struct vb2_queue *q, struct v4l2_create_buffers *create)
     700                 :            : {
     701                 :            :         unsigned requested_planes = 1;
     702                 :            :         unsigned requested_sizes[VIDEO_MAX_PLANES];
     703                 :            :         struct v4l2_format *f = &create->format;
     704                 :          0 :         int ret = vb2_verify_memory_type(q, create->memory, f->type);
     705                 :            :         unsigned i;
     706                 :            : 
     707                 :          0 :         fill_buf_caps(q, &create->capabilities);
     708                 :          0 :         create->index = q->num_buffers;
     709         [ #  # ]:          0 :         if (create->count == 0)
     710         [ #  # ]:          0 :                 return ret != -EBUSY ? ret : 0;
     711                 :            : 
     712   [ #  #  #  #  :          0 :         switch (f->type) {
                #  #  # ]
     713                 :            :         case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
     714                 :            :         case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
     715                 :          0 :                 requested_planes = f->fmt.pix_mp.num_planes;
     716         [ #  # ]:          0 :                 if (requested_planes == 0 ||
     717                 :            :                     requested_planes > VIDEO_MAX_PLANES)
     718                 :            :                         return -EINVAL;
     719         [ #  # ]:          0 :                 for (i = 0; i < requested_planes; i++)
     720                 :          0 :                         requested_sizes[i] =
     721                 :          0 :                                 f->fmt.pix_mp.plane_fmt[i].sizeimage;
     722                 :            :                 break;
     723                 :            :         case V4L2_BUF_TYPE_VIDEO_CAPTURE:
     724                 :            :         case V4L2_BUF_TYPE_VIDEO_OUTPUT:
     725                 :          0 :                 requested_sizes[0] = f->fmt.pix.sizeimage;
     726                 :          0 :                 break;
     727                 :            :         case V4L2_BUF_TYPE_VBI_CAPTURE:
     728                 :            :         case V4L2_BUF_TYPE_VBI_OUTPUT:
     729                 :          0 :                 requested_sizes[0] = f->fmt.vbi.samples_per_line *
     730                 :          0 :                         (f->fmt.vbi.count[0] + f->fmt.vbi.count[1]);
     731                 :          0 :                 break;
     732                 :            :         case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
     733                 :            :         case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
     734                 :          0 :                 requested_sizes[0] = f->fmt.sliced.io_size;
     735                 :          0 :                 break;
     736                 :            :         case V4L2_BUF_TYPE_SDR_CAPTURE:
     737                 :            :         case V4L2_BUF_TYPE_SDR_OUTPUT:
     738                 :          0 :                 requested_sizes[0] = f->fmt.sdr.buffersize;
     739                 :          0 :                 break;
     740                 :            :         case V4L2_BUF_TYPE_META_CAPTURE:
     741                 :            :         case V4L2_BUF_TYPE_META_OUTPUT:
     742                 :          0 :                 requested_sizes[0] = f->fmt.meta.buffersize;
     743                 :          0 :                 break;
     744                 :            :         default:
     745                 :            :                 return -EINVAL;
     746                 :            :         }
     747         [ #  # ]:          0 :         for (i = 0; i < requested_planes; i++)
     748         [ #  # ]:          0 :                 if (requested_sizes[i] == 0)
     749                 :            :                         return -EINVAL;
     750         [ #  # ]:          0 :         return ret ? ret : vb2_core_create_bufs(q, create->memory,
     751                 :          0 :                 &create->count, requested_planes, requested_sizes);
     752                 :            : }
     753                 :            : EXPORT_SYMBOL_GPL(vb2_create_bufs);
     754                 :            : 
     755                 :          0 : int vb2_qbuf(struct vb2_queue *q, struct media_device *mdev,
     756                 :            :              struct v4l2_buffer *b)
     757                 :            : {
     758                 :          0 :         struct media_request *req = NULL;
     759                 :            :         int ret;
     760                 :            : 
     761         [ #  # ]:          0 :         if (vb2_fileio_is_active(q)) {
     762         [ #  # ]:          0 :                 dprintk(1, "file io in progress\n");
     763                 :            :                 return -EBUSY;
     764                 :            :         }
     765                 :            : 
     766                 :          0 :         ret = vb2_queue_or_prepare_buf(q, mdev, b, false, &req);
     767         [ #  # ]:          0 :         if (ret)
     768                 :            :                 return ret;
     769                 :          0 :         ret = vb2_core_qbuf(q, b->index, b, req);
     770         [ #  # ]:          0 :         if (req)
     771                 :          0 :                 media_request_put(req);
     772                 :          0 :         return ret;
     773                 :            : }
     774                 :            : EXPORT_SYMBOL_GPL(vb2_qbuf);
     775                 :            : 
     776                 :          0 : int vb2_dqbuf(struct vb2_queue *q, struct v4l2_buffer *b, bool nonblocking)
     777                 :            : {
     778                 :            :         int ret;
     779                 :            : 
     780         [ #  # ]:          0 :         if (vb2_fileio_is_active(q)) {
     781         [ #  # ]:          0 :                 dprintk(1, "file io in progress\n");
     782                 :            :                 return -EBUSY;
     783                 :            :         }
     784                 :            : 
     785         [ #  # ]:          0 :         if (b->type != q->type) {
     786         [ #  # ]:          0 :                 dprintk(1, "invalid buffer type\n");
     787                 :            :                 return -EINVAL;
     788                 :            :         }
     789                 :            : 
     790                 :          0 :         ret = vb2_core_dqbuf(q, NULL, b, nonblocking);
     791                 :            : 
     792         [ #  # ]:          0 :         if (!q->is_output &&
     793         [ #  # ]:          0 :             b->flags & V4L2_BUF_FLAG_DONE &&
     794                 :            :             b->flags & V4L2_BUF_FLAG_LAST)
     795                 :          0 :                 q->last_buffer_dequeued = true;
     796                 :            : 
     797                 :            :         /*
     798                 :            :          *  After calling the VIDIOC_DQBUF V4L2_BUF_FLAG_DONE must be
     799                 :            :          *  cleared.
     800                 :            :          */
     801                 :          0 :         b->flags &= ~V4L2_BUF_FLAG_DONE;
     802                 :            : 
     803                 :          0 :         return ret;
     804                 :            : }
     805                 :            : EXPORT_SYMBOL_GPL(vb2_dqbuf);
     806                 :            : 
     807                 :          0 : int vb2_streamon(struct vb2_queue *q, enum v4l2_buf_type type)
     808                 :            : {
     809         [ #  # ]:          0 :         if (vb2_fileio_is_active(q)) {
     810         [ #  # ]:          0 :                 dprintk(1, "file io in progress\n");
     811                 :            :                 return -EBUSY;
     812                 :            :         }
     813                 :          0 :         return vb2_core_streamon(q, type);
     814                 :            : }
     815                 :            : EXPORT_SYMBOL_GPL(vb2_streamon);
     816                 :            : 
     817                 :          0 : int vb2_streamoff(struct vb2_queue *q, enum v4l2_buf_type type)
     818                 :            : {
     819         [ #  # ]:          0 :         if (vb2_fileio_is_active(q)) {
     820         [ #  # ]:          0 :                 dprintk(1, "file io in progress\n");
     821                 :            :                 return -EBUSY;
     822                 :            :         }
     823                 :          0 :         return vb2_core_streamoff(q, type);
     824                 :            : }
     825                 :            : EXPORT_SYMBOL_GPL(vb2_streamoff);
     826                 :            : 
     827                 :          0 : int vb2_expbuf(struct vb2_queue *q, struct v4l2_exportbuffer *eb)
     828                 :            : {
     829                 :          0 :         return vb2_core_expbuf(q, &eb->fd, eb->type, eb->index,
     830                 :            :                                 eb->plane, eb->flags);
     831                 :            : }
     832                 :            : EXPORT_SYMBOL_GPL(vb2_expbuf);
     833                 :            : 
     834                 :       2070 : int vb2_queue_init(struct vb2_queue *q)
     835                 :            : {
     836                 :            :         /*
     837                 :            :          * Sanity check
     838                 :            :          */
     839   [ -  +  +  -  :       4140 :         if (WARN_ON(!q)                   ||
                   +  - ]
     840         [ -  + ]:       2070 :             WARN_ON(q->timestamp_flags &
     841                 :            :                     ~(V4L2_BUF_FLAG_TIMESTAMP_MASK |
     842                 :            :                       V4L2_BUF_FLAG_TSTAMP_SRC_MASK)))
     843                 :            :                 return -EINVAL;
     844                 :            : 
     845                 :            :         /* Warn that the driver should choose an appropriate timestamp type */
     846         [ -  + ]:       2070 :         WARN_ON((q->timestamp_flags & V4L2_BUF_FLAG_TIMESTAMP_MASK) ==
     847                 :            :                 V4L2_BUF_FLAG_TIMESTAMP_UNKNOWN);
     848                 :            : 
     849                 :            :         /* Warn that vb2_memory should match with v4l2_memory */
     850                 :            :         if (WARN_ON(VB2_MEMORY_MMAP != (int)V4L2_MEMORY_MMAP)
     851                 :            :                 || WARN_ON(VB2_MEMORY_USERPTR != (int)V4L2_MEMORY_USERPTR)
     852                 :            :                 || WARN_ON(VB2_MEMORY_DMABUF != (int)V4L2_MEMORY_DMABUF))
     853                 :            :                 return -EINVAL;
     854                 :            : 
     855         [ -  + ]:       2070 :         if (q->buf_struct_size == 0)
     856                 :          0 :                 q->buf_struct_size = sizeof(struct vb2_v4l2_buffer);
     857                 :            : 
     858                 :       2070 :         q->buf_ops = &v4l2_buf_ops;
     859                 :       2070 :         q->is_multiplanar = V4L2_TYPE_IS_MULTIPLANAR(q->type);
     860   [ +  +  +  -  :       2070 :         q->is_output = V4L2_TYPE_IS_OUTPUT(q->type);
          +  -  +  -  +  
             -  +  -  +  
                      - ]
     861                 :       4140 :         q->copy_timestamp = (q->timestamp_flags & V4L2_BUF_FLAG_TIMESTAMP_MASK)
     862                 :       2070 :                         == V4L2_BUF_FLAG_TIMESTAMP_COPY;
     863                 :            :         /*
     864                 :            :          * For compatibility with vb1: if QBUF hasn't been called yet, then
     865                 :            :          * return EPOLLERR as well. This only affects capture queues, output
     866                 :            :          * queues will always initialize waiting_for_buffers to false.
     867                 :            :          */
     868                 :       2070 :         q->quirk_poll_must_check_waiting_for_buffers = true;
     869                 :            : 
     870                 :       2070 :         return vb2_core_queue_init(q);
     871                 :            : }
     872                 :            : EXPORT_SYMBOL_GPL(vb2_queue_init);
     873                 :            : 
     874                 :       1242 : void vb2_queue_release(struct vb2_queue *q)
     875                 :            : {
     876                 :       1242 :         vb2_core_queue_release(q);
     877                 :       1242 : }
     878                 :            : EXPORT_SYMBOL_GPL(vb2_queue_release);
     879                 :            : 
     880                 :          0 : __poll_t vb2_poll(struct vb2_queue *q, struct file *file, poll_table *wait)
     881                 :            : {
     882                 :          0 :         struct video_device *vfd = video_devdata(file);
     883                 :            :         __poll_t res;
     884                 :            : 
     885                 :          0 :         res = vb2_core_poll(q, file, wait);
     886                 :            : 
     887         [ #  # ]:          0 :         if (test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags)) {
     888                 :          0 :                 struct v4l2_fh *fh = file->private_data;
     889                 :            : 
     890                 :          0 :                 poll_wait(file, &fh->wait, wait);
     891         [ #  # ]:          0 :                 if (v4l2_event_pending(fh))
     892                 :          0 :                         res |= EPOLLPRI;
     893                 :            :         }
     894                 :            : 
     895                 :          0 :         return res;
     896                 :            : }
     897                 :            : EXPORT_SYMBOL_GPL(vb2_poll);
     898                 :            : 
     899                 :            : /*
     900                 :            :  * The following functions are not part of the vb2 core API, but are helper
     901                 :            :  * functions that plug into struct v4l2_ioctl_ops, struct v4l2_file_operations
     902                 :            :  * and struct vb2_ops.
     903                 :            :  * They contain boilerplate code that most if not all drivers have to do
     904                 :            :  * and so they simplify the driver code.
     905                 :            :  */
     906                 :            : 
     907                 :            : /* The queue is busy if there is a owner and you are not that owner. */
     908                 :            : static inline bool vb2_queue_is_busy(struct video_device *vdev, struct file *file)
     909                 :            : {
     910   [ #  #  #  #  :          0 :         return vdev->queue->owner && vdev->queue->owner != file->private_data;
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                      # ]
     911                 :            : }
     912                 :            : 
     913                 :            : /* vb2 ioctl helpers */
     914                 :            : 
     915                 :          0 : int vb2_ioctl_reqbufs(struct file *file, void *priv,
     916                 :            :                           struct v4l2_requestbuffers *p)
     917                 :            : {
     918                 :          0 :         struct video_device *vdev = video_devdata(file);
     919                 :          0 :         int res = vb2_verify_memory_type(vdev->queue, p->memory, p->type);
     920                 :            : 
     921                 :          0 :         fill_buf_caps(vdev->queue, &p->capabilities);
     922         [ #  # ]:          0 :         if (res)
     923                 :            :                 return res;
     924         [ #  # ]:          0 :         if (vb2_queue_is_busy(vdev, file))
     925                 :            :                 return -EBUSY;
     926                 :          0 :         res = vb2_core_reqbufs(vdev->queue, p->memory, &p->count);
     927                 :            :         /* If count == 0, then the owner has released all buffers and he
     928                 :            :            is no longer owner of the queue. Otherwise we have a new owner. */
     929         [ #  # ]:          0 :         if (res == 0)
     930         [ #  # ]:          0 :                 vdev->queue->owner = p->count ? file->private_data : NULL;
     931                 :          0 :         return res;
     932                 :            : }
     933                 :            : EXPORT_SYMBOL_GPL(vb2_ioctl_reqbufs);
     934                 :            : 
     935                 :          0 : int vb2_ioctl_create_bufs(struct file *file, void *priv,
     936                 :            :                           struct v4l2_create_buffers *p)
     937                 :            : {
     938                 :          0 :         struct video_device *vdev = video_devdata(file);
     939                 :          0 :         int res = vb2_verify_memory_type(vdev->queue, p->memory,
     940                 :            :                         p->format.type);
     941                 :            : 
     942                 :          0 :         p->index = vdev->queue->num_buffers;
     943                 :          0 :         fill_buf_caps(vdev->queue, &p->capabilities);
     944                 :            :         /*
     945                 :            :          * If count == 0, then just check if memory and type are valid.
     946                 :            :          * Any -EBUSY result from vb2_verify_memory_type can be mapped to 0.
     947                 :            :          */
     948         [ #  # ]:          0 :         if (p->count == 0)
     949         [ #  # ]:          0 :                 return res != -EBUSY ? res : 0;
     950         [ #  # ]:          0 :         if (res)
     951                 :            :                 return res;
     952         [ #  # ]:          0 :         if (vb2_queue_is_busy(vdev, file))
     953                 :            :                 return -EBUSY;
     954                 :            : 
     955                 :          0 :         res = vb2_create_bufs(vdev->queue, p);
     956         [ #  # ]:          0 :         if (res == 0)
     957                 :          0 :                 vdev->queue->owner = file->private_data;
     958                 :          0 :         return res;
     959                 :            : }
     960                 :            : EXPORT_SYMBOL_GPL(vb2_ioctl_create_bufs);
     961                 :            : 
     962                 :          0 : int vb2_ioctl_prepare_buf(struct file *file, void *priv,
     963                 :            :                           struct v4l2_buffer *p)
     964                 :            : {
     965                 :          0 :         struct video_device *vdev = video_devdata(file);
     966                 :            : 
     967         [ #  # ]:          0 :         if (vb2_queue_is_busy(vdev, file))
     968                 :            :                 return -EBUSY;
     969                 :          0 :         return vb2_prepare_buf(vdev->queue, vdev->v4l2_dev->mdev, p);
     970                 :            : }
     971                 :            : EXPORT_SYMBOL_GPL(vb2_ioctl_prepare_buf);
     972                 :            : 
     973                 :          0 : int vb2_ioctl_querybuf(struct file *file, void *priv, struct v4l2_buffer *p)
     974                 :            : {
     975                 :          0 :         struct video_device *vdev = video_devdata(file);
     976                 :            : 
     977                 :            :         /* No need to call vb2_queue_is_busy(), anyone can query buffers. */
     978                 :          0 :         return vb2_querybuf(vdev->queue, p);
     979                 :            : }
     980                 :            : EXPORT_SYMBOL_GPL(vb2_ioctl_querybuf);
     981                 :            : 
     982                 :          0 : int vb2_ioctl_qbuf(struct file *file, void *priv, struct v4l2_buffer *p)
     983                 :            : {
     984                 :          0 :         struct video_device *vdev = video_devdata(file);
     985                 :            : 
     986         [ #  # ]:          0 :         if (vb2_queue_is_busy(vdev, file))
     987                 :            :                 return -EBUSY;
     988                 :          0 :         return vb2_qbuf(vdev->queue, vdev->v4l2_dev->mdev, p);
     989                 :            : }
     990                 :            : EXPORT_SYMBOL_GPL(vb2_ioctl_qbuf);
     991                 :            : 
     992                 :          0 : int vb2_ioctl_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
     993                 :            : {
     994                 :          0 :         struct video_device *vdev = video_devdata(file);
     995                 :            : 
     996         [ #  # ]:          0 :         if (vb2_queue_is_busy(vdev, file))
     997                 :            :                 return -EBUSY;
     998                 :          0 :         return vb2_dqbuf(vdev->queue, p, file->f_flags & O_NONBLOCK);
     999                 :            : }
    1000                 :            : EXPORT_SYMBOL_GPL(vb2_ioctl_dqbuf);
    1001                 :            : 
    1002                 :          0 : int vb2_ioctl_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
    1003                 :            : {
    1004                 :          0 :         struct video_device *vdev = video_devdata(file);
    1005                 :            : 
    1006         [ #  # ]:          0 :         if (vb2_queue_is_busy(vdev, file))
    1007                 :            :                 return -EBUSY;
    1008                 :          0 :         return vb2_streamon(vdev->queue, i);
    1009                 :            : }
    1010                 :            : EXPORT_SYMBOL_GPL(vb2_ioctl_streamon);
    1011                 :            : 
    1012                 :          0 : int vb2_ioctl_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
    1013                 :            : {
    1014                 :          0 :         struct video_device *vdev = video_devdata(file);
    1015                 :            : 
    1016         [ #  # ]:          0 :         if (vb2_queue_is_busy(vdev, file))
    1017                 :            :                 return -EBUSY;
    1018                 :          0 :         return vb2_streamoff(vdev->queue, i);
    1019                 :            : }
    1020                 :            : EXPORT_SYMBOL_GPL(vb2_ioctl_streamoff);
    1021                 :            : 
    1022                 :          0 : int vb2_ioctl_expbuf(struct file *file, void *priv, struct v4l2_exportbuffer *p)
    1023                 :            : {
    1024                 :          0 :         struct video_device *vdev = video_devdata(file);
    1025                 :            : 
    1026         [ #  # ]:          0 :         if (vb2_queue_is_busy(vdev, file))
    1027                 :            :                 return -EBUSY;
    1028                 :          0 :         return vb2_expbuf(vdev->queue, p);
    1029                 :            : }
    1030                 :            : EXPORT_SYMBOL_GPL(vb2_ioctl_expbuf);
    1031                 :            : 
    1032                 :            : /* v4l2_file_operations helpers */
    1033                 :            : 
    1034                 :          0 : int vb2_fop_mmap(struct file *file, struct vm_area_struct *vma)
    1035                 :            : {
    1036                 :          0 :         struct video_device *vdev = video_devdata(file);
    1037                 :            : 
    1038                 :          0 :         return vb2_mmap(vdev->queue, vma);
    1039                 :            : }
    1040                 :            : EXPORT_SYMBOL_GPL(vb2_fop_mmap);
    1041                 :            : 
    1042                 :        828 : int _vb2_fop_release(struct file *file, struct mutex *lock)
    1043                 :            : {
    1044                 :        828 :         struct video_device *vdev = video_devdata(file);
    1045                 :            : 
    1046         [ +  - ]:        828 :         if (lock)
    1047                 :        828 :                 mutex_lock(lock);
    1048         [ -  + ]:        828 :         if (file->private_data == vdev->queue->owner) {
    1049                 :            :                 vb2_queue_release(vdev->queue);
    1050                 :          0 :                 vdev->queue->owner = NULL;
    1051                 :            :         }
    1052         [ +  - ]:        828 :         if (lock)
    1053                 :        828 :                 mutex_unlock(lock);
    1054                 :        828 :         return v4l2_fh_release(file);
    1055                 :            : }
    1056                 :            : EXPORT_SYMBOL_GPL(_vb2_fop_release);
    1057                 :            : 
    1058                 :        828 : int vb2_fop_release(struct file *file)
    1059                 :            : {
    1060                 :        828 :         struct video_device *vdev = video_devdata(file);
    1061         [ -  + ]:        828 :         struct mutex *lock = vdev->queue->lock ? vdev->queue->lock : vdev->lock;
    1062                 :            : 
    1063                 :        828 :         return _vb2_fop_release(file, lock);
    1064                 :            : }
    1065                 :            : EXPORT_SYMBOL_GPL(vb2_fop_release);
    1066                 :            : 
    1067                 :          0 : ssize_t vb2_fop_write(struct file *file, const char __user *buf,
    1068                 :            :                 size_t count, loff_t *ppos)
    1069                 :            : {
    1070                 :          0 :         struct video_device *vdev = video_devdata(file);
    1071         [ #  # ]:          0 :         struct mutex *lock = vdev->queue->lock ? vdev->queue->lock : vdev->lock;
    1072                 :            :         int err = -EBUSY;
    1073                 :            : 
    1074         [ #  # ]:          0 :         if (!(vdev->queue->io_modes & VB2_WRITE))
    1075                 :            :                 return -EINVAL;
    1076   [ #  #  #  # ]:          0 :         if (lock && mutex_lock_interruptible(lock))
    1077                 :            :                 return -ERESTARTSYS;
    1078         [ #  # ]:          0 :         if (vb2_queue_is_busy(vdev, file))
    1079                 :            :                 goto exit;
    1080                 :          0 :         err = vb2_write(vdev->queue, buf, count, ppos,
    1081                 :          0 :                        file->f_flags & O_NONBLOCK);
    1082         [ #  # ]:          0 :         if (vdev->queue->fileio)
    1083                 :          0 :                 vdev->queue->owner = file->private_data;
    1084                 :            : exit:
    1085         [ #  # ]:          0 :         if (lock)
    1086                 :          0 :                 mutex_unlock(lock);
    1087                 :          0 :         return err;
    1088                 :            : }
    1089                 :            : EXPORT_SYMBOL_GPL(vb2_fop_write);
    1090                 :            : 
    1091                 :          0 : ssize_t vb2_fop_read(struct file *file, char __user *buf,
    1092                 :            :                 size_t count, loff_t *ppos)
    1093                 :            : {
    1094                 :          0 :         struct video_device *vdev = video_devdata(file);
    1095         [ #  # ]:          0 :         struct mutex *lock = vdev->queue->lock ? vdev->queue->lock : vdev->lock;
    1096                 :            :         int err = -EBUSY;
    1097                 :            : 
    1098         [ #  # ]:          0 :         if (!(vdev->queue->io_modes & VB2_READ))
    1099                 :            :                 return -EINVAL;
    1100   [ #  #  #  # ]:          0 :         if (lock && mutex_lock_interruptible(lock))
    1101                 :            :                 return -ERESTARTSYS;
    1102         [ #  # ]:          0 :         if (vb2_queue_is_busy(vdev, file))
    1103                 :            :                 goto exit;
    1104                 :          0 :         err = vb2_read(vdev->queue, buf, count, ppos,
    1105                 :          0 :                        file->f_flags & O_NONBLOCK);
    1106         [ #  # ]:          0 :         if (vdev->queue->fileio)
    1107                 :          0 :                 vdev->queue->owner = file->private_data;
    1108                 :            : exit:
    1109         [ #  # ]:          0 :         if (lock)
    1110                 :          0 :                 mutex_unlock(lock);
    1111                 :          0 :         return err;
    1112                 :            : }
    1113                 :            : EXPORT_SYMBOL_GPL(vb2_fop_read);
    1114                 :            : 
    1115                 :          0 : __poll_t vb2_fop_poll(struct file *file, poll_table *wait)
    1116                 :            : {
    1117                 :          0 :         struct video_device *vdev = video_devdata(file);
    1118                 :          0 :         struct vb2_queue *q = vdev->queue;
    1119         [ #  # ]:          0 :         struct mutex *lock = q->lock ? q->lock : vdev->lock;
    1120                 :            :         __poll_t res;
    1121                 :            :         void *fileio;
    1122                 :            : 
    1123                 :            :         /*
    1124                 :            :          * If this helper doesn't know how to lock, then you shouldn't be using
    1125                 :            :          * it but you should write your own.
    1126                 :            :          */
    1127         [ #  # ]:          0 :         WARN_ON(!lock);
    1128                 :            : 
    1129   [ #  #  #  # ]:          0 :         if (lock && mutex_lock_interruptible(lock))
    1130                 :            :                 return EPOLLERR;
    1131                 :            : 
    1132                 :          0 :         fileio = q->fileio;
    1133                 :            : 
    1134                 :          0 :         res = vb2_poll(vdev->queue, file, wait);
    1135                 :            : 
    1136                 :            :         /* If fileio was started, then we have a new queue owner. */
    1137   [ #  #  #  # ]:          0 :         if (!fileio && q->fileio)
    1138                 :          0 :                 q->owner = file->private_data;
    1139         [ #  # ]:          0 :         if (lock)
    1140                 :          0 :                 mutex_unlock(lock);
    1141                 :          0 :         return res;
    1142                 :            : }
    1143                 :            : EXPORT_SYMBOL_GPL(vb2_fop_poll);
    1144                 :            : 
    1145                 :            : #ifndef CONFIG_MMU
    1146                 :            : unsigned long vb2_fop_get_unmapped_area(struct file *file, unsigned long addr,
    1147                 :            :                 unsigned long len, unsigned long pgoff, unsigned long flags)
    1148                 :            : {
    1149                 :            :         struct video_device *vdev = video_devdata(file);
    1150                 :            : 
    1151                 :            :         return vb2_get_unmapped_area(vdev->queue, addr, len, pgoff, flags);
    1152                 :            : }
    1153                 :            : EXPORT_SYMBOL_GPL(vb2_fop_get_unmapped_area);
    1154                 :            : #endif
    1155                 :            : 
    1156                 :            : /* vb2_ops helpers. Only use if vq->lock is non-NULL. */
    1157                 :            : 
    1158                 :          0 : void vb2_ops_wait_prepare(struct vb2_queue *vq)
    1159                 :            : {
    1160                 :          0 :         mutex_unlock(vq->lock);
    1161                 :          0 : }
    1162                 :            : EXPORT_SYMBOL_GPL(vb2_ops_wait_prepare);
    1163                 :            : 
    1164                 :          0 : void vb2_ops_wait_finish(struct vb2_queue *vq)
    1165                 :            : {
    1166                 :          0 :         mutex_lock(vq->lock);
    1167                 :          0 : }
    1168                 :            : EXPORT_SYMBOL_GPL(vb2_ops_wait_finish);
    1169                 :            : 
    1170                 :            : /*
    1171                 :            :  * Note that this function is called during validation time and
    1172                 :            :  * thus the req_queue_mutex is held to ensure no request objects
    1173                 :            :  * can be added or deleted while validating. So there is no need
    1174                 :            :  * to protect the objects list.
    1175                 :            :  */
    1176                 :          0 : int vb2_request_validate(struct media_request *req)
    1177                 :            : {
    1178                 :            :         struct media_request_object *obj;
    1179                 :            :         int ret = 0;
    1180                 :            : 
    1181         [ #  # ]:          0 :         if (!vb2_request_buffer_cnt(req))
    1182                 :            :                 return -ENOENT;
    1183                 :            : 
    1184         [ #  # ]:          0 :         list_for_each_entry(obj, &req->objects, list) {
    1185         [ #  # ]:          0 :                 if (!obj->ops->prepare)
    1186                 :          0 :                         continue;
    1187                 :            : 
    1188                 :          0 :                 ret = obj->ops->prepare(obj);
    1189         [ #  # ]:          0 :                 if (ret)
    1190                 :            :                         break;
    1191                 :            :         }
    1192                 :            : 
    1193         [ #  # ]:          0 :         if (ret) {
    1194         [ #  # ]:          0 :                 list_for_each_entry_continue_reverse(obj, &req->objects, list)
    1195         [ #  # ]:          0 :                         if (obj->ops->unprepare)
    1196                 :          0 :                                 obj->ops->unprepare(obj);
    1197                 :            :                 return ret;
    1198                 :            :         }
    1199                 :            :         return 0;
    1200                 :            : }
    1201                 :            : EXPORT_SYMBOL_GPL(vb2_request_validate);
    1202                 :            : 
    1203                 :          0 : void vb2_request_queue(struct media_request *req)
    1204                 :            : {
    1205                 :            :         struct media_request_object *obj, *obj_safe;
    1206                 :            : 
    1207                 :            :         /*
    1208                 :            :          * Queue all objects. Note that buffer objects are at the end of the
    1209                 :            :          * objects list, after all other object types. Once buffer objects
    1210                 :            :          * are queued, the driver might delete them immediately (if the driver
    1211                 :            :          * processes the buffer at once), so we have to use
    1212                 :            :          * list_for_each_entry_safe() to handle the case where the object we
    1213                 :            :          * queue is deleted.
    1214                 :            :          */
    1215         [ #  # ]:          0 :         list_for_each_entry_safe(obj, obj_safe, &req->objects, list)
    1216         [ #  # ]:          0 :                 if (obj->ops->queue)
    1217                 :          0 :                         obj->ops->queue(obj);
    1218                 :          0 : }
    1219                 :            : EXPORT_SYMBOL_GPL(vb2_request_queue);
    1220                 :            : 
    1221                 :            : MODULE_DESCRIPTION("Driver helper framework for Video for Linux 2");
    1222                 :            : MODULE_AUTHOR("Pawel Osciak <pawel@osciak.com>, Marek Szyprowski");
    1223                 :            : MODULE_LICENSE("GPL");

Generated by: LCOV version 1.14