LCOV - code coverage report
Current view: top level - drivers/media/v4l2-core - v4l2-ioctl.c (source / functions) Hit Total Coverage
Test: Real Lines: 73 1279 5.7 %
Date: 2020-10-17 15:46:16 Functions: 0 143 0.0 %
Legend: Neither, QEMU, Real, Both Branches: 0 0 -

           Branch data     Line data    Source code
       1                 :            : // SPDX-License-Identifier: GPL-2.0-or-later
       2                 :            : /*
       3                 :            :  * Video capture interface for Linux version 2
       4                 :            :  *
       5                 :            :  * A generic framework to process V4L2 ioctl commands.
       6                 :            :  *
       7                 :            :  * Authors:     Alan Cox, <alan@lxorguk.ukuu.org.uk> (version 1)
       8                 :            :  *              Mauro Carvalho Chehab <mchehab@kernel.org> (version 2)
       9                 :            :  */
      10                 :            : 
      11                 :            : #include <linux/mm.h>
      12                 :            : #include <linux/module.h>
      13                 :            : #include <linux/slab.h>
      14                 :            : #include <linux/types.h>
      15                 :            : #include <linux/kernel.h>
      16                 :            : #include <linux/version.h>
      17                 :            : 
      18                 :            : #include <linux/videodev2.h>
      19                 :            : 
      20                 :            : #include <media/v4l2-common.h>
      21                 :            : #include <media/v4l2-ioctl.h>
      22                 :            : #include <media/v4l2-ctrls.h>
      23                 :            : #include <media/v4l2-fh.h>
      24                 :            : #include <media/v4l2-event.h>
      25                 :            : #include <media/v4l2-device.h>
      26                 :            : #include <media/videobuf2-v4l2.h>
      27                 :            : #include <media/v4l2-mc.h>
      28                 :            : #include <media/v4l2-mem2mem.h>
      29                 :            : 
      30                 :            : #include <trace/events/v4l2.h>
      31                 :            : 
      32                 :            : /* Zero out the end of the struct pointed to by p.  Everything after, but
      33                 :            :  * not including, the specified field is cleared. */
      34                 :            : #define CLEAR_AFTER_FIELD(p, field) \
      35                 :            :         memset((u8 *)(p) + offsetof(typeof(*(p)), field) + sizeof((p)->field), \
      36                 :            :         0, sizeof(*(p)) - offsetof(typeof(*(p)), field) - sizeof((p)->field))
      37                 :            : 
      38                 :            : #define is_valid_ioctl(vfd, cmd) test_bit(_IOC_NR(cmd), (vfd)->valid_ioctls)
      39                 :            : 
      40                 :            : struct std_descr {
      41                 :            :         v4l2_std_id std;
      42                 :            :         const char *descr;
      43                 :            : };
      44                 :            : 
      45                 :            : static const struct std_descr standards[] = {
      46                 :            :         { V4L2_STD_NTSC,        "NTSC"      },
      47                 :            :         { V4L2_STD_NTSC_M,      "NTSC-M"    },
      48                 :            :         { V4L2_STD_NTSC_M_JP,   "NTSC-M-JP" },
      49                 :            :         { V4L2_STD_NTSC_M_KR,   "NTSC-M-KR" },
      50                 :            :         { V4L2_STD_NTSC_443,    "NTSC-443"  },
      51                 :            :         { V4L2_STD_PAL,         "PAL"       },
      52                 :            :         { V4L2_STD_PAL_BG,      "PAL-BG"    },
      53                 :            :         { V4L2_STD_PAL_B,       "PAL-B"     },
      54                 :            :         { V4L2_STD_PAL_B1,      "PAL-B1"    },
      55                 :            :         { V4L2_STD_PAL_G,       "PAL-G"     },
      56                 :            :         { V4L2_STD_PAL_H,       "PAL-H"     },
      57                 :            :         { V4L2_STD_PAL_I,       "PAL-I"     },
      58                 :            :         { V4L2_STD_PAL_DK,      "PAL-DK"    },
      59                 :            :         { V4L2_STD_PAL_D,       "PAL-D"     },
      60                 :            :         { V4L2_STD_PAL_D1,      "PAL-D1"    },
      61                 :            :         { V4L2_STD_PAL_K,       "PAL-K"     },
      62                 :            :         { V4L2_STD_PAL_M,       "PAL-M"     },
      63                 :            :         { V4L2_STD_PAL_N,       "PAL-N"     },
      64                 :            :         { V4L2_STD_PAL_Nc,      "PAL-Nc"    },
      65                 :            :         { V4L2_STD_PAL_60,      "PAL-60"    },
      66                 :            :         { V4L2_STD_SECAM,       "SECAM"     },
      67                 :            :         { V4L2_STD_SECAM_B,     "SECAM-B"   },
      68                 :            :         { V4L2_STD_SECAM_G,     "SECAM-G"   },
      69                 :            :         { V4L2_STD_SECAM_H,     "SECAM-H"   },
      70                 :            :         { V4L2_STD_SECAM_DK,    "SECAM-DK"  },
      71                 :            :         { V4L2_STD_SECAM_D,     "SECAM-D"   },
      72                 :            :         { V4L2_STD_SECAM_K,     "SECAM-K"   },
      73                 :            :         { V4L2_STD_SECAM_K1,    "SECAM-K1"  },
      74                 :            :         { V4L2_STD_SECAM_L,     "SECAM-L"   },
      75                 :            :         { V4L2_STD_SECAM_LC,    "SECAM-Lc"  },
      76                 :            :         { 0,                    "Unknown"   }
      77                 :            : };
      78                 :            : 
      79                 :            : /* video4linux standard ID conversion to standard name
      80                 :            :  */
      81                 :          0 : const char *v4l2_norm_to_name(v4l2_std_id id)
      82                 :            : {
      83                 :            :         u32 myid = id;
      84                 :            :         int i;
      85                 :            : 
      86                 :            :         /* HACK: ppc32 architecture doesn't have __ucmpdi2 function to handle
      87                 :            :            64 bit comparisons. So, on that architecture, with some gcc
      88                 :            :            variants, compilation fails. Currently, the max value is 30bit wide.
      89                 :            :          */
      90                 :          0 :         BUG_ON(myid != id);
      91                 :            : 
      92                 :          0 :         for (i = 0; standards[i].std; i++)
      93                 :          0 :                 if (myid == standards[i].std)
      94                 :            :                         break;
      95                 :          0 :         return standards[i].descr;
      96                 :            : }
      97                 :            : EXPORT_SYMBOL(v4l2_norm_to_name);
      98                 :            : 
      99                 :            : /* Returns frame period for the given standard */
     100                 :          0 : void v4l2_video_std_frame_period(int id, struct v4l2_fract *frameperiod)
     101                 :            : {
     102                 :          0 :         if (id & V4L2_STD_525_60) {
     103                 :          0 :                 frameperiod->numerator = 1001;
     104                 :          0 :                 frameperiod->denominator = 30000;
     105                 :            :         } else {
     106                 :          0 :                 frameperiod->numerator = 1;
     107                 :          0 :                 frameperiod->denominator = 25;
     108                 :            :         }
     109                 :          0 : }
     110                 :            : EXPORT_SYMBOL(v4l2_video_std_frame_period);
     111                 :            : 
     112                 :            : /* Fill in the fields of a v4l2_standard structure according to the
     113                 :            :    'id' and 'transmission' parameters.  Returns negative on error.  */
     114                 :          0 : int v4l2_video_std_construct(struct v4l2_standard *vs,
     115                 :            :                              int id, const char *name)
     116                 :            : {
     117                 :          0 :         vs->id = id;
     118                 :            :         v4l2_video_std_frame_period(id, &vs->frameperiod);
     119                 :          0 :         vs->framelines = (id & V4L2_STD_525_60) ? 525 : 625;
     120                 :          0 :         strscpy(vs->name, name, sizeof(vs->name));
     121                 :          0 :         return 0;
     122                 :            : }
     123                 :            : EXPORT_SYMBOL(v4l2_video_std_construct);
     124                 :            : 
     125                 :            : /* Fill in the fields of a v4l2_standard structure according to the
     126                 :            :  * 'id' and 'vs->index' parameters. Returns negative on error. */
     127                 :          0 : int v4l_video_std_enumstd(struct v4l2_standard *vs, v4l2_std_id id)
     128                 :            : {
     129                 :            :         v4l2_std_id curr_id = 0;
     130                 :          0 :         unsigned int index = vs->index, i, j = 0;
     131                 :            :         const char *descr = "";
     132                 :            : 
     133                 :            :         /* Return -ENODATA if the id for the current input
     134                 :            :            or output is 0, meaning that it doesn't support this API. */
     135                 :          0 :         if (id == 0)
     136                 :            :                 return -ENODATA;
     137                 :            : 
     138                 :            :         /* Return norm array in a canonical way */
     139                 :          0 :         for (i = 0; i <= index && id; i++) {
     140                 :            :                 /* last std value in the standards array is 0, so this
     141                 :            :                    while always ends there since (id & 0) == 0. */
     142                 :          0 :                 while ((id & standards[j].std) != standards[j].std)
     143                 :          0 :                         j++;
     144                 :          0 :                 curr_id = standards[j].std;
     145                 :          0 :                 descr = standards[j].descr;
     146                 :          0 :                 j++;
     147                 :          0 :                 if (curr_id == 0)
     148                 :            :                         break;
     149                 :          0 :                 if (curr_id != V4L2_STD_PAL &&
     150                 :          0 :                                 curr_id != V4L2_STD_SECAM &&
     151                 :            :                                 curr_id != V4L2_STD_NTSC)
     152                 :          0 :                         id &= ~curr_id;
     153                 :            :         }
     154                 :          0 :         if (i <= index)
     155                 :            :                 return -EINVAL;
     156                 :            : 
     157                 :          0 :         v4l2_video_std_construct(vs, curr_id, descr);
     158                 :          0 :         return 0;
     159                 :            : }
     160                 :            : 
     161                 :            : /* ----------------------------------------------------------------- */
     162                 :            : /* some arrays for pretty-printing debug messages of enum types      */
     163                 :            : 
     164                 :            : const char *v4l2_field_names[] = {
     165                 :            :         [V4L2_FIELD_ANY]        = "any",
     166                 :            :         [V4L2_FIELD_NONE]       = "none",
     167                 :            :         [V4L2_FIELD_TOP]        = "top",
     168                 :            :         [V4L2_FIELD_BOTTOM]     = "bottom",
     169                 :            :         [V4L2_FIELD_INTERLACED] = "interlaced",
     170                 :            :         [V4L2_FIELD_SEQ_TB]     = "seq-tb",
     171                 :            :         [V4L2_FIELD_SEQ_BT]     = "seq-bt",
     172                 :            :         [V4L2_FIELD_ALTERNATE]  = "alternate",
     173                 :            :         [V4L2_FIELD_INTERLACED_TB] = "interlaced-tb",
     174                 :            :         [V4L2_FIELD_INTERLACED_BT] = "interlaced-bt",
     175                 :            : };
     176                 :            : EXPORT_SYMBOL(v4l2_field_names);
     177                 :            : 
     178                 :            : const char *v4l2_type_names[] = {
     179                 :            :         [0]                                = "0",
     180                 :            :         [V4L2_BUF_TYPE_VIDEO_CAPTURE]      = "vid-cap",
     181                 :            :         [V4L2_BUF_TYPE_VIDEO_OVERLAY]      = "vid-overlay",
     182                 :            :         [V4L2_BUF_TYPE_VIDEO_OUTPUT]       = "vid-out",
     183                 :            :         [V4L2_BUF_TYPE_VBI_CAPTURE]        = "vbi-cap",
     184                 :            :         [V4L2_BUF_TYPE_VBI_OUTPUT]         = "vbi-out",
     185                 :            :         [V4L2_BUF_TYPE_SLICED_VBI_CAPTURE] = "sliced-vbi-cap",
     186                 :            :         [V4L2_BUF_TYPE_SLICED_VBI_OUTPUT]  = "sliced-vbi-out",
     187                 :            :         [V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY] = "vid-out-overlay",
     188                 :            :         [V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE] = "vid-cap-mplane",
     189                 :            :         [V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE] = "vid-out-mplane",
     190                 :            :         [V4L2_BUF_TYPE_SDR_CAPTURE]        = "sdr-cap",
     191                 :            :         [V4L2_BUF_TYPE_SDR_OUTPUT]         = "sdr-out",
     192                 :            :         [V4L2_BUF_TYPE_META_CAPTURE]       = "meta-cap",
     193                 :            :         [V4L2_BUF_TYPE_META_OUTPUT]        = "meta-out",
     194                 :            : };
     195                 :            : EXPORT_SYMBOL(v4l2_type_names);
     196                 :            : 
     197                 :            : static const char *v4l2_memory_names[] = {
     198                 :            :         [V4L2_MEMORY_MMAP]    = "mmap",
     199                 :            :         [V4L2_MEMORY_USERPTR] = "userptr",
     200                 :            :         [V4L2_MEMORY_OVERLAY] = "overlay",
     201                 :            :         [V4L2_MEMORY_DMABUF] = "dmabuf",
     202                 :            : };
     203                 :            : 
     204                 :            : #define prt_names(a, arr) (((unsigned)(a)) < ARRAY_SIZE(arr) ? arr[a] : "unknown")
     205                 :            : 
     206                 :            : /* ------------------------------------------------------------------ */
     207                 :            : /* debug help functions                                               */
     208                 :            : 
     209                 :          0 : static void v4l_print_querycap(const void *arg, bool write_only)
     210                 :            : {
     211                 :            :         const struct v4l2_capability *p = arg;
     212                 :            : 
     213                 :          0 :         pr_cont("driver=%.*s, card=%.*s, bus=%.*s, version=0x%08x, capabilities=0x%08x, device_caps=0x%08x\n",
     214                 :            :                 (int)sizeof(p->driver), p->driver,
     215                 :            :                 (int)sizeof(p->card), p->card,
     216                 :            :                 (int)sizeof(p->bus_info), p->bus_info,
     217                 :            :                 p->version, p->capabilities, p->device_caps);
     218                 :          0 : }
     219                 :            : 
     220                 :          0 : static void v4l_print_enuminput(const void *arg, bool write_only)
     221                 :            : {
     222                 :            :         const struct v4l2_input *p = arg;
     223                 :            : 
     224                 :          0 :         pr_cont("index=%u, name=%.*s, type=%u, audioset=0x%x, tuner=%u, std=0x%08Lx, status=0x%x, capabilities=0x%x\n",
     225                 :            :                 p->index, (int)sizeof(p->name), p->name, p->type, p->audioset,
     226                 :            :                 p->tuner, (unsigned long long)p->std, p->status,
     227                 :            :                 p->capabilities);
     228                 :          0 : }
     229                 :            : 
     230                 :          0 : static void v4l_print_enumoutput(const void *arg, bool write_only)
     231                 :            : {
     232                 :            :         const struct v4l2_output *p = arg;
     233                 :            : 
     234                 :          0 :         pr_cont("index=%u, name=%.*s, type=%u, audioset=0x%x, modulator=%u, std=0x%08Lx, capabilities=0x%x\n",
     235                 :            :                 p->index, (int)sizeof(p->name), p->name, p->type, p->audioset,
     236                 :            :                 p->modulator, (unsigned long long)p->std, p->capabilities);
     237                 :          0 : }
     238                 :            : 
     239                 :          0 : static void v4l_print_audio(const void *arg, bool write_only)
     240                 :            : {
     241                 :            :         const struct v4l2_audio *p = arg;
     242                 :            : 
     243                 :          0 :         if (write_only)
     244                 :          0 :                 pr_cont("index=%u, mode=0x%x\n", p->index, p->mode);
     245                 :            :         else
     246                 :          0 :                 pr_cont("index=%u, name=%.*s, capability=0x%x, mode=0x%x\n",
     247                 :            :                         p->index, (int)sizeof(p->name), p->name,
     248                 :            :                         p->capability, p->mode);
     249                 :          0 : }
     250                 :            : 
     251                 :          0 : static void v4l_print_audioout(const void *arg, bool write_only)
     252                 :            : {
     253                 :            :         const struct v4l2_audioout *p = arg;
     254                 :            : 
     255                 :          0 :         if (write_only)
     256                 :          0 :                 pr_cont("index=%u\n", p->index);
     257                 :            :         else
     258                 :          0 :                 pr_cont("index=%u, name=%.*s, capability=0x%x, mode=0x%x\n",
     259                 :            :                         p->index, (int)sizeof(p->name), p->name,
     260                 :            :                         p->capability, p->mode);
     261                 :          0 : }
     262                 :            : 
     263                 :          0 : static void v4l_print_fmtdesc(const void *arg, bool write_only)
     264                 :            : {
     265                 :            :         const struct v4l2_fmtdesc *p = arg;
     266                 :            : 
     267                 :          0 :         pr_cont("index=%u, type=%s, flags=0x%x, pixelformat=%c%c%c%c, description='%.*s'\n",
     268                 :            :                 p->index, prt_names(p->type, v4l2_type_names),
     269                 :            :                 p->flags, (p->pixelformat & 0xff),
     270                 :            :                 (p->pixelformat >>  8) & 0xff,
     271                 :            :                 (p->pixelformat >> 16) & 0xff,
     272                 :            :                 (p->pixelformat >> 24) & 0xff,
     273                 :            :                 (int)sizeof(p->description), p->description);
     274                 :          0 : }
     275                 :            : 
     276                 :          0 : static void v4l_print_format(const void *arg, bool write_only)
     277                 :            : {
     278                 :            :         const struct v4l2_format *p = arg;
     279                 :            :         const struct v4l2_pix_format *pix;
     280                 :            :         const struct v4l2_pix_format_mplane *mp;
     281                 :            :         const struct v4l2_vbi_format *vbi;
     282                 :            :         const struct v4l2_sliced_vbi_format *sliced;
     283                 :            :         const struct v4l2_window *win;
     284                 :            :         const struct v4l2_sdr_format *sdr;
     285                 :            :         const struct v4l2_meta_format *meta;
     286                 :            :         u32 planes;
     287                 :            :         unsigned i;
     288                 :            : 
     289                 :          0 :         pr_cont("type=%s", prt_names(p->type, v4l2_type_names));
     290                 :          0 :         switch (p->type) {
     291                 :            :         case V4L2_BUF_TYPE_VIDEO_CAPTURE:
     292                 :            :         case V4L2_BUF_TYPE_VIDEO_OUTPUT:
     293                 :            :                 pix = &p->fmt.pix;
     294                 :          0 :                 pr_cont(", width=%u, height=%u, pixelformat=%c%c%c%c, field=%s, bytesperline=%u, sizeimage=%u, colorspace=%d, flags=0x%x, ycbcr_enc=%u, quantization=%u, xfer_func=%u\n",
     295                 :            :                         pix->width, pix->height,
     296                 :            :                         (pix->pixelformat & 0xff),
     297                 :            :                         (pix->pixelformat >>  8) & 0xff,
     298                 :            :                         (pix->pixelformat >> 16) & 0xff,
     299                 :            :                         (pix->pixelformat >> 24) & 0xff,
     300                 :            :                         prt_names(pix->field, v4l2_field_names),
     301                 :            :                         pix->bytesperline, pix->sizeimage,
     302                 :            :                         pix->colorspace, pix->flags, pix->ycbcr_enc,
     303                 :            :                         pix->quantization, pix->xfer_func);
     304                 :          0 :                 break;
     305                 :            :         case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
     306                 :            :         case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
     307                 :            :                 mp = &p->fmt.pix_mp;
     308                 :          0 :                 pr_cont(", width=%u, height=%u, format=%c%c%c%c, field=%s, colorspace=%d, num_planes=%u, flags=0x%x, ycbcr_enc=%u, quantization=%u, xfer_func=%u\n",
     309                 :            :                         mp->width, mp->height,
     310                 :            :                         (mp->pixelformat & 0xff),
     311                 :            :                         (mp->pixelformat >>  8) & 0xff,
     312                 :            :                         (mp->pixelformat >> 16) & 0xff,
     313                 :            :                         (mp->pixelformat >> 24) & 0xff,
     314                 :            :                         prt_names(mp->field, v4l2_field_names),
     315                 :            :                         mp->colorspace, mp->num_planes, mp->flags,
     316                 :            :                         mp->ycbcr_enc, mp->quantization, mp->xfer_func);
     317                 :          0 :                 planes = min_t(u32, mp->num_planes, VIDEO_MAX_PLANES);
     318                 :          0 :                 for (i = 0; i < planes; i++)
     319                 :          0 :                         printk(KERN_DEBUG "plane %u: bytesperline=%u sizeimage=%u\n", i,
     320                 :            :                                         mp->plane_fmt[i].bytesperline,
     321                 :            :                                         mp->plane_fmt[i].sizeimage);
     322                 :            :                 break;
     323                 :            :         case V4L2_BUF_TYPE_VIDEO_OVERLAY:
     324                 :            :         case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
     325                 :            :                 win = &p->fmt.win;
     326                 :            :                 /* Note: we can't print the clip list here since the clips
     327                 :            :                  * pointer is a userspace pointer, not a kernelspace
     328                 :            :                  * pointer. */
     329                 :          0 :                 pr_cont(", wxh=%dx%d, x,y=%d,%d, field=%s, chromakey=0x%08x, clipcount=%u, clips=%p, bitmap=%p, global_alpha=0x%02x\n",
     330                 :            :                         win->w.width, win->w.height, win->w.left, win->w.top,
     331                 :            :                         prt_names(win->field, v4l2_field_names),
     332                 :            :                         win->chromakey, win->clipcount, win->clips,
     333                 :            :                         win->bitmap, win->global_alpha);
     334                 :          0 :                 break;
     335                 :            :         case V4L2_BUF_TYPE_VBI_CAPTURE:
     336                 :            :         case V4L2_BUF_TYPE_VBI_OUTPUT:
     337                 :            :                 vbi = &p->fmt.vbi;
     338                 :          0 :                 pr_cont(", sampling_rate=%u, offset=%u, samples_per_line=%u, sample_format=%c%c%c%c, start=%u,%u, count=%u,%u\n",
     339                 :            :                         vbi->sampling_rate, vbi->offset,
     340                 :            :                         vbi->samples_per_line,
     341                 :            :                         (vbi->sample_format & 0xff),
     342                 :            :                         (vbi->sample_format >>  8) & 0xff,
     343                 :            :                         (vbi->sample_format >> 16) & 0xff,
     344                 :            :                         (vbi->sample_format >> 24) & 0xff,
     345                 :            :                         vbi->start[0], vbi->start[1],
     346                 :            :                         vbi->count[0], vbi->count[1]);
     347                 :          0 :                 break;
     348                 :            :         case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
     349                 :            :         case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
     350                 :            :                 sliced = &p->fmt.sliced;
     351                 :          0 :                 pr_cont(", service_set=0x%08x, io_size=%d\n",
     352                 :            :                                 sliced->service_set, sliced->io_size);
     353                 :          0 :                 for (i = 0; i < 24; i++)
     354                 :          0 :                         printk(KERN_DEBUG "line[%02u]=0x%04x, 0x%04x\n", i,
     355                 :          0 :                                 sliced->service_lines[0][i],
     356                 :          0 :                                 sliced->service_lines[1][i]);
     357                 :            :                 break;
     358                 :            :         case V4L2_BUF_TYPE_SDR_CAPTURE:
     359                 :            :         case V4L2_BUF_TYPE_SDR_OUTPUT:
     360                 :            :                 sdr = &p->fmt.sdr;
     361                 :          0 :                 pr_cont(", pixelformat=%c%c%c%c\n",
     362                 :            :                         (sdr->pixelformat >>  0) & 0xff,
     363                 :            :                         (sdr->pixelformat >>  8) & 0xff,
     364                 :            :                         (sdr->pixelformat >> 16) & 0xff,
     365                 :            :                         (sdr->pixelformat >> 24) & 0xff);
     366                 :          0 :                 break;
     367                 :            :         case V4L2_BUF_TYPE_META_CAPTURE:
     368                 :            :         case V4L2_BUF_TYPE_META_OUTPUT:
     369                 :            :                 meta = &p->fmt.meta;
     370                 :          0 :                 pr_cont(", dataformat=%c%c%c%c, buffersize=%u\n",
     371                 :            :                         (meta->dataformat >>  0) & 0xff,
     372                 :            :                         (meta->dataformat >>  8) & 0xff,
     373                 :            :                         (meta->dataformat >> 16) & 0xff,
     374                 :            :                         (meta->dataformat >> 24) & 0xff,
     375                 :            :                         meta->buffersize);
     376                 :          0 :                 break;
     377                 :            :         }
     378                 :          0 : }
     379                 :            : 
     380                 :          0 : static void v4l_print_framebuffer(const void *arg, bool write_only)
     381                 :            : {
     382                 :            :         const struct v4l2_framebuffer *p = arg;
     383                 :            : 
     384                 :          0 :         pr_cont("capability=0x%x, flags=0x%x, base=0x%p, width=%u, height=%u, pixelformat=%c%c%c%c, bytesperline=%u, sizeimage=%u, colorspace=%d\n",
     385                 :            :                         p->capability, p->flags, p->base,
     386                 :            :                         p->fmt.width, p->fmt.height,
     387                 :            :                         (p->fmt.pixelformat & 0xff),
     388                 :            :                         (p->fmt.pixelformat >>  8) & 0xff,
     389                 :            :                         (p->fmt.pixelformat >> 16) & 0xff,
     390                 :            :                         (p->fmt.pixelformat >> 24) & 0xff,
     391                 :            :                         p->fmt.bytesperline, p->fmt.sizeimage,
     392                 :            :                         p->fmt.colorspace);
     393                 :          0 : }
     394                 :            : 
     395                 :          0 : static void v4l_print_buftype(const void *arg, bool write_only)
     396                 :            : {
     397                 :          0 :         pr_cont("type=%s\n", prt_names(*(u32 *)arg, v4l2_type_names));
     398                 :          0 : }
     399                 :            : 
     400                 :          0 : static void v4l_print_modulator(const void *arg, bool write_only)
     401                 :            : {
     402                 :            :         const struct v4l2_modulator *p = arg;
     403                 :            : 
     404                 :          0 :         if (write_only)
     405                 :          0 :                 pr_cont("index=%u, txsubchans=0x%x\n", p->index, p->txsubchans);
     406                 :            :         else
     407                 :          0 :                 pr_cont("index=%u, name=%.*s, capability=0x%x, rangelow=%u, rangehigh=%u, txsubchans=0x%x\n",
     408                 :            :                         p->index, (int)sizeof(p->name), p->name, p->capability,
     409                 :            :                         p->rangelow, p->rangehigh, p->txsubchans);
     410                 :          0 : }
     411                 :            : 
     412                 :          0 : static void v4l_print_tuner(const void *arg, bool write_only)
     413                 :            : {
     414                 :            :         const struct v4l2_tuner *p = arg;
     415                 :            : 
     416                 :          0 :         if (write_only)
     417                 :          0 :                 pr_cont("index=%u, audmode=%u\n", p->index, p->audmode);
     418                 :            :         else
     419                 :          0 :                 pr_cont("index=%u, name=%.*s, type=%u, capability=0x%x, rangelow=%u, rangehigh=%u, signal=%u, afc=%d, rxsubchans=0x%x, audmode=%u\n",
     420                 :            :                         p->index, (int)sizeof(p->name), p->name, p->type,
     421                 :            :                         p->capability, p->rangelow,
     422                 :            :                         p->rangehigh, p->signal, p->afc,
     423                 :            :                         p->rxsubchans, p->audmode);
     424                 :          0 : }
     425                 :            : 
     426                 :          0 : static void v4l_print_frequency(const void *arg, bool write_only)
     427                 :            : {
     428                 :            :         const struct v4l2_frequency *p = arg;
     429                 :            : 
     430                 :          0 :         pr_cont("tuner=%u, type=%u, frequency=%u\n",
     431                 :            :                                 p->tuner, p->type, p->frequency);
     432                 :          0 : }
     433                 :            : 
     434                 :          0 : static void v4l_print_standard(const void *arg, bool write_only)
     435                 :            : {
     436                 :            :         const struct v4l2_standard *p = arg;
     437                 :            : 
     438                 :          0 :         pr_cont("index=%u, id=0x%Lx, name=%.*s, fps=%u/%u, framelines=%u\n",
     439                 :            :                 p->index,
     440                 :            :                 (unsigned long long)p->id, (int)sizeof(p->name), p->name,
     441                 :            :                 p->frameperiod.numerator,
     442                 :            :                 p->frameperiod.denominator,
     443                 :            :                 p->framelines);
     444                 :          0 : }
     445                 :            : 
     446                 :          0 : static void v4l_print_std(const void *arg, bool write_only)
     447                 :            : {
     448                 :          0 :         pr_cont("std=0x%08Lx\n", *(const long long unsigned *)arg);
     449                 :          0 : }
     450                 :            : 
     451                 :          0 : static void v4l_print_hw_freq_seek(const void *arg, bool write_only)
     452                 :            : {
     453                 :            :         const struct v4l2_hw_freq_seek *p = arg;
     454                 :            : 
     455                 :          0 :         pr_cont("tuner=%u, type=%u, seek_upward=%u, wrap_around=%u, spacing=%u, rangelow=%u, rangehigh=%u\n",
     456                 :            :                 p->tuner, p->type, p->seek_upward, p->wrap_around, p->spacing,
     457                 :            :                 p->rangelow, p->rangehigh);
     458                 :          0 : }
     459                 :            : 
     460                 :          0 : static void v4l_print_requestbuffers(const void *arg, bool write_only)
     461                 :            : {
     462                 :            :         const struct v4l2_requestbuffers *p = arg;
     463                 :            : 
     464                 :          0 :         pr_cont("count=%d, type=%s, memory=%s\n",
     465                 :            :                 p->count,
     466                 :            :                 prt_names(p->type, v4l2_type_names),
     467                 :            :                 prt_names(p->memory, v4l2_memory_names));
     468                 :          0 : }
     469                 :            : 
     470                 :          0 : static void v4l_print_buffer(const void *arg, bool write_only)
     471                 :            : {
     472                 :            :         const struct v4l2_buffer *p = arg;
     473                 :            :         const struct v4l2_timecode *tc = &p->timecode;
     474                 :            :         const struct v4l2_plane *plane;
     475                 :            :         int i;
     476                 :            : 
     477                 :          0 :         pr_cont("%02ld:%02d:%02d.%08ld index=%d, type=%s, request_fd=%d, flags=0x%08x, field=%s, sequence=%d, memory=%s",
     478                 :            :                         p->timestamp.tv_sec / 3600,
     479                 :            :                         (int)(p->timestamp.tv_sec / 60) % 60,
     480                 :            :                         (int)(p->timestamp.tv_sec % 60),
     481                 :            :                         (long)p->timestamp.tv_usec,
     482                 :            :                         p->index,
     483                 :            :                         prt_names(p->type, v4l2_type_names), p->request_fd,
     484                 :            :                         p->flags, prt_names(p->field, v4l2_field_names),
     485                 :            :                         p->sequence, prt_names(p->memory, v4l2_memory_names));
     486                 :            : 
     487                 :          0 :         if (V4L2_TYPE_IS_MULTIPLANAR(p->type) && p->m.planes) {
     488                 :          0 :                 pr_cont("\n");
     489                 :          0 :                 for (i = 0; i < p->length; ++i) {
     490                 :          0 :                         plane = &p->m.planes[i];
     491                 :          0 :                         printk(KERN_DEBUG
     492                 :            :                                 "plane %d: bytesused=%d, data_offset=0x%08x, offset/userptr=0x%lx, length=%d\n",
     493                 :            :                                 i, plane->bytesused, plane->data_offset,
     494                 :            :                                 plane->m.userptr, plane->length);
     495                 :            :                 }
     496                 :            :         } else {
     497                 :          0 :                 pr_cont(", bytesused=%d, offset/userptr=0x%lx, length=%d\n",
     498                 :            :                         p->bytesused, p->m.userptr, p->length);
     499                 :            :         }
     500                 :            : 
     501                 :          0 :         printk(KERN_DEBUG "timecode=%02d:%02d:%02d type=%d, flags=0x%08x, frames=%d, userbits=0x%08x\n",
     502                 :          0 :                         tc->hours, tc->minutes, tc->seconds,
     503                 :          0 :                         tc->type, tc->flags, tc->frames, *(__u32 *)tc->userbits);
     504                 :          0 : }
     505                 :            : 
     506                 :          0 : static void v4l_print_exportbuffer(const void *arg, bool write_only)
     507                 :            : {
     508                 :            :         const struct v4l2_exportbuffer *p = arg;
     509                 :            : 
     510                 :          0 :         pr_cont("fd=%d, type=%s, index=%u, plane=%u, flags=0x%08x\n",
     511                 :            :                 p->fd, prt_names(p->type, v4l2_type_names),
     512                 :            :                 p->index, p->plane, p->flags);
     513                 :          0 : }
     514                 :            : 
     515                 :          0 : static void v4l_print_create_buffers(const void *arg, bool write_only)
     516                 :            : {
     517                 :            :         const struct v4l2_create_buffers *p = arg;
     518                 :            : 
     519                 :          0 :         pr_cont("index=%d, count=%d, memory=%s, ",
     520                 :            :                         p->index, p->count,
     521                 :            :                         prt_names(p->memory, v4l2_memory_names));
     522                 :          0 :         v4l_print_format(&p->format, write_only);
     523                 :          0 : }
     524                 :            : 
     525                 :          0 : static void v4l_print_streamparm(const void *arg, bool write_only)
     526                 :            : {
     527                 :            :         const struct v4l2_streamparm *p = arg;
     528                 :            : 
     529                 :          0 :         pr_cont("type=%s", prt_names(p->type, v4l2_type_names));
     530                 :            : 
     531                 :          0 :         if (p->type == V4L2_BUF_TYPE_VIDEO_CAPTURE ||
     532                 :            :             p->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
     533                 :            :                 const struct v4l2_captureparm *c = &p->parm.capture;
     534                 :            : 
     535                 :          0 :                 pr_cont(", capability=0x%x, capturemode=0x%x, timeperframe=%d/%d, extendedmode=%d, readbuffers=%d\n",
     536                 :            :                         c->capability, c->capturemode,
     537                 :            :                         c->timeperframe.numerator, c->timeperframe.denominator,
     538                 :            :                         c->extendedmode, c->readbuffers);
     539                 :          0 :         } else if (p->type == V4L2_BUF_TYPE_VIDEO_OUTPUT ||
     540                 :            :                    p->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
     541                 :            :                 const struct v4l2_outputparm *c = &p->parm.output;
     542                 :            : 
     543                 :          0 :                 pr_cont(", capability=0x%x, outputmode=0x%x, timeperframe=%d/%d, extendedmode=%d, writebuffers=%d\n",
     544                 :            :                         c->capability, c->outputmode,
     545                 :            :                         c->timeperframe.numerator, c->timeperframe.denominator,
     546                 :            :                         c->extendedmode, c->writebuffers);
     547                 :            :         } else {
     548                 :          0 :                 pr_cont("\n");
     549                 :            :         }
     550                 :          0 : }
     551                 :            : 
     552                 :          0 : static void v4l_print_queryctrl(const void *arg, bool write_only)
     553                 :            : {
     554                 :            :         const struct v4l2_queryctrl *p = arg;
     555                 :            : 
     556                 :          0 :         pr_cont("id=0x%x, type=%d, name=%.*s, min/max=%d/%d, step=%d, default=%d, flags=0x%08x\n",
     557                 :            :                         p->id, p->type, (int)sizeof(p->name), p->name,
     558                 :            :                         p->minimum, p->maximum,
     559                 :            :                         p->step, p->default_value, p->flags);
     560                 :          0 : }
     561                 :            : 
     562                 :          0 : static void v4l_print_query_ext_ctrl(const void *arg, bool write_only)
     563                 :            : {
     564                 :            :         const struct v4l2_query_ext_ctrl *p = arg;
     565                 :            : 
     566                 :          0 :         pr_cont("id=0x%x, type=%d, name=%.*s, min/max=%lld/%lld, step=%lld, default=%lld, flags=0x%08x, elem_size=%u, elems=%u, nr_of_dims=%u, dims=%u,%u,%u,%u\n",
     567                 :            :                         p->id, p->type, (int)sizeof(p->name), p->name,
     568                 :            :                         p->minimum, p->maximum,
     569                 :            :                         p->step, p->default_value, p->flags,
     570                 :            :                         p->elem_size, p->elems, p->nr_of_dims,
     571                 :            :                         p->dims[0], p->dims[1], p->dims[2], p->dims[3]);
     572                 :          0 : }
     573                 :            : 
     574                 :          0 : static void v4l_print_querymenu(const void *arg, bool write_only)
     575                 :            : {
     576                 :            :         const struct v4l2_querymenu *p = arg;
     577                 :            : 
     578                 :          0 :         pr_cont("id=0x%x, index=%d\n", p->id, p->index);
     579                 :          0 : }
     580                 :            : 
     581                 :          0 : static void v4l_print_control(const void *arg, bool write_only)
     582                 :            : {
     583                 :            :         const struct v4l2_control *p = arg;
     584                 :            : 
     585                 :          0 :         pr_cont("id=0x%x, value=%d\n", p->id, p->value);
     586                 :          0 : }
     587                 :            : 
     588                 :          0 : static void v4l_print_ext_controls(const void *arg, bool write_only)
     589                 :            : {
     590                 :            :         const struct v4l2_ext_controls *p = arg;
     591                 :            :         int i;
     592                 :            : 
     593                 :          0 :         pr_cont("which=0x%x, count=%d, error_idx=%d, request_fd=%d",
     594                 :            :                         p->which, p->count, p->error_idx, p->request_fd);
     595                 :          0 :         for (i = 0; i < p->count; i++) {
     596                 :          0 :                 if (!p->controls[i].size)
     597                 :          0 :                         pr_cont(", id/val=0x%x/0x%x",
     598                 :            :                                 p->controls[i].id, p->controls[i].value);
     599                 :            :                 else
     600                 :          0 :                         pr_cont(", id/size=0x%x/%u",
     601                 :            :                                 p->controls[i].id, p->controls[i].size);
     602                 :            :         }
     603                 :          0 :         pr_cont("\n");
     604                 :          0 : }
     605                 :            : 
     606                 :          0 : static void v4l_print_cropcap(const void *arg, bool write_only)
     607                 :            : {
     608                 :            :         const struct v4l2_cropcap *p = arg;
     609                 :            : 
     610                 :          0 :         pr_cont("type=%s, bounds wxh=%dx%d, x,y=%d,%d, defrect wxh=%dx%d, x,y=%d,%d, pixelaspect %d/%d\n",
     611                 :            :                 prt_names(p->type, v4l2_type_names),
     612                 :            :                 p->bounds.width, p->bounds.height,
     613                 :            :                 p->bounds.left, p->bounds.top,
     614                 :            :                 p->defrect.width, p->defrect.height,
     615                 :            :                 p->defrect.left, p->defrect.top,
     616                 :            :                 p->pixelaspect.numerator, p->pixelaspect.denominator);
     617                 :          0 : }
     618                 :            : 
     619                 :          0 : static void v4l_print_crop(const void *arg, bool write_only)
     620                 :            : {
     621                 :            :         const struct v4l2_crop *p = arg;
     622                 :            : 
     623                 :          0 :         pr_cont("type=%s, wxh=%dx%d, x,y=%d,%d\n",
     624                 :            :                 prt_names(p->type, v4l2_type_names),
     625                 :            :                 p->c.width, p->c.height,
     626                 :            :                 p->c.left, p->c.top);
     627                 :          0 : }
     628                 :            : 
     629                 :          0 : static void v4l_print_selection(const void *arg, bool write_only)
     630                 :            : {
     631                 :            :         const struct v4l2_selection *p = arg;
     632                 :            : 
     633                 :          0 :         pr_cont("type=%s, target=%d, flags=0x%x, wxh=%dx%d, x,y=%d,%d\n",
     634                 :            :                 prt_names(p->type, v4l2_type_names),
     635                 :            :                 p->target, p->flags,
     636                 :            :                 p->r.width, p->r.height, p->r.left, p->r.top);
     637                 :          0 : }
     638                 :            : 
     639                 :          0 : static void v4l_print_jpegcompression(const void *arg, bool write_only)
     640                 :            : {
     641                 :            :         const struct v4l2_jpegcompression *p = arg;
     642                 :            : 
     643                 :          0 :         pr_cont("quality=%d, APPn=%d, APP_len=%d, COM_len=%d, jpeg_markers=0x%x\n",
     644                 :            :                 p->quality, p->APPn, p->APP_len,
     645                 :            :                 p->COM_len, p->jpeg_markers);
     646                 :          0 : }
     647                 :            : 
     648                 :          0 : static void v4l_print_enc_idx(const void *arg, bool write_only)
     649                 :            : {
     650                 :            :         const struct v4l2_enc_idx *p = arg;
     651                 :            : 
     652                 :          0 :         pr_cont("entries=%d, entries_cap=%d\n",
     653                 :            :                         p->entries, p->entries_cap);
     654                 :          0 : }
     655                 :            : 
     656                 :          0 : static void v4l_print_encoder_cmd(const void *arg, bool write_only)
     657                 :            : {
     658                 :            :         const struct v4l2_encoder_cmd *p = arg;
     659                 :            : 
     660                 :          0 :         pr_cont("cmd=%d, flags=0x%x\n",
     661                 :            :                         p->cmd, p->flags);
     662                 :          0 : }
     663                 :            : 
     664                 :          0 : static void v4l_print_decoder_cmd(const void *arg, bool write_only)
     665                 :            : {
     666                 :            :         const struct v4l2_decoder_cmd *p = arg;
     667                 :            : 
     668                 :          0 :         pr_cont("cmd=%d, flags=0x%x\n", p->cmd, p->flags);
     669                 :            : 
     670                 :          0 :         if (p->cmd == V4L2_DEC_CMD_START)
     671                 :          0 :                 pr_info("speed=%d, format=%u\n",
     672                 :            :                                 p->start.speed, p->start.format);
     673                 :          0 :         else if (p->cmd == V4L2_DEC_CMD_STOP)
     674                 :          0 :                 pr_info("pts=%llu\n", p->stop.pts);
     675                 :          0 : }
     676                 :            : 
     677                 :          0 : static void v4l_print_dbg_chip_info(const void *arg, bool write_only)
     678                 :            : {
     679                 :            :         const struct v4l2_dbg_chip_info *p = arg;
     680                 :            : 
     681                 :          0 :         pr_cont("type=%u, ", p->match.type);
     682                 :          0 :         if (p->match.type == V4L2_CHIP_MATCH_I2C_DRIVER)
     683                 :          0 :                 pr_cont("name=%.*s, ",
     684                 :            :                                 (int)sizeof(p->match.name), p->match.name);
     685                 :            :         else
     686                 :          0 :                 pr_cont("addr=%u, ", p->match.addr);
     687                 :          0 :         pr_cont("name=%.*s\n", (int)sizeof(p->name), p->name);
     688                 :          0 : }
     689                 :            : 
     690                 :          0 : static void v4l_print_dbg_register(const void *arg, bool write_only)
     691                 :            : {
     692                 :            :         const struct v4l2_dbg_register *p = arg;
     693                 :            : 
     694                 :          0 :         pr_cont("type=%u, ", p->match.type);
     695                 :          0 :         if (p->match.type == V4L2_CHIP_MATCH_I2C_DRIVER)
     696                 :          0 :                 pr_cont("name=%.*s, ",
     697                 :            :                                 (int)sizeof(p->match.name), p->match.name);
     698                 :            :         else
     699                 :          0 :                 pr_cont("addr=%u, ", p->match.addr);
     700                 :          0 :         pr_cont("reg=0x%llx, val=0x%llx\n",
     701                 :            :                         p->reg, p->val);
     702                 :          0 : }
     703                 :            : 
     704                 :          0 : static void v4l_print_dv_timings(const void *arg, bool write_only)
     705                 :            : {
     706                 :            :         const struct v4l2_dv_timings *p = arg;
     707                 :            : 
     708                 :          0 :         switch (p->type) {
     709                 :            :         case V4L2_DV_BT_656_1120:
     710                 :          0 :                 pr_cont("type=bt-656/1120, interlaced=%u, pixelclock=%llu, width=%u, height=%u, polarities=0x%x, hfrontporch=%u, hsync=%u, hbackporch=%u, vfrontporch=%u, vsync=%u, vbackporch=%u, il_vfrontporch=%u, il_vsync=%u, il_vbackporch=%u, standards=0x%x, flags=0x%x\n",
     711                 :            :                                 p->bt.interlaced, p->bt.pixelclock,
     712                 :            :                                 p->bt.width, p->bt.height,
     713                 :            :                                 p->bt.polarities, p->bt.hfrontporch,
     714                 :            :                                 p->bt.hsync, p->bt.hbackporch,
     715                 :            :                                 p->bt.vfrontporch, p->bt.vsync,
     716                 :            :                                 p->bt.vbackporch, p->bt.il_vfrontporch,
     717                 :            :                                 p->bt.il_vsync, p->bt.il_vbackporch,
     718                 :            :                                 p->bt.standards, p->bt.flags);
     719                 :          0 :                 break;
     720                 :            :         default:
     721                 :          0 :                 pr_cont("type=%d\n", p->type);
     722                 :          0 :                 break;
     723                 :            :         }
     724                 :          0 : }
     725                 :            : 
     726                 :          0 : static void v4l_print_enum_dv_timings(const void *arg, bool write_only)
     727                 :            : {
     728                 :            :         const struct v4l2_enum_dv_timings *p = arg;
     729                 :            : 
     730                 :          0 :         pr_cont("index=%u, ", p->index);
     731                 :          0 :         v4l_print_dv_timings(&p->timings, write_only);
     732                 :          0 : }
     733                 :            : 
     734                 :          0 : static void v4l_print_dv_timings_cap(const void *arg, bool write_only)
     735                 :            : {
     736                 :            :         const struct v4l2_dv_timings_cap *p = arg;
     737                 :            : 
     738                 :          0 :         switch (p->type) {
     739                 :            :         case V4L2_DV_BT_656_1120:
     740                 :          0 :                 pr_cont("type=bt-656/1120, width=%u-%u, height=%u-%u, pixelclock=%llu-%llu, standards=0x%x, capabilities=0x%x\n",
     741                 :            :                         p->bt.min_width, p->bt.max_width,
     742                 :            :                         p->bt.min_height, p->bt.max_height,
     743                 :            :                         p->bt.min_pixelclock, p->bt.max_pixelclock,
     744                 :            :                         p->bt.standards, p->bt.capabilities);
     745                 :          0 :                 break;
     746                 :            :         default:
     747                 :          0 :                 pr_cont("type=%u\n", p->type);
     748                 :          0 :                 break;
     749                 :            :         }
     750                 :          0 : }
     751                 :            : 
     752                 :          0 : static void v4l_print_frmsizeenum(const void *arg, bool write_only)
     753                 :            : {
     754                 :            :         const struct v4l2_frmsizeenum *p = arg;
     755                 :            : 
     756                 :          0 :         pr_cont("index=%u, pixelformat=%c%c%c%c, type=%u",
     757                 :            :                         p->index,
     758                 :            :                         (p->pixel_format & 0xff),
     759                 :            :                         (p->pixel_format >>  8) & 0xff,
     760                 :            :                         (p->pixel_format >> 16) & 0xff,
     761                 :            :                         (p->pixel_format >> 24) & 0xff,
     762                 :            :                         p->type);
     763                 :          0 :         switch (p->type) {
     764                 :            :         case V4L2_FRMSIZE_TYPE_DISCRETE:
     765                 :          0 :                 pr_cont(", wxh=%ux%u\n",
     766                 :            :                         p->discrete.width, p->discrete.height);
     767                 :          0 :                 break;
     768                 :            :         case V4L2_FRMSIZE_TYPE_STEPWISE:
     769                 :          0 :                 pr_cont(", min=%ux%u, max=%ux%u, step=%ux%u\n",
     770                 :            :                                 p->stepwise.min_width,
     771                 :            :                                 p->stepwise.min_height,
     772                 :            :                                 p->stepwise.max_width,
     773                 :            :                                 p->stepwise.max_height,
     774                 :            :                                 p->stepwise.step_width,
     775                 :            :                                 p->stepwise.step_height);
     776                 :          0 :                 break;
     777                 :            :         case V4L2_FRMSIZE_TYPE_CONTINUOUS:
     778                 :            :                 /* fall through */
     779                 :            :         default:
     780                 :          0 :                 pr_cont("\n");
     781                 :          0 :                 break;
     782                 :            :         }
     783                 :          0 : }
     784                 :            : 
     785                 :          0 : static void v4l_print_frmivalenum(const void *arg, bool write_only)
     786                 :            : {
     787                 :            :         const struct v4l2_frmivalenum *p = arg;
     788                 :            : 
     789                 :          0 :         pr_cont("index=%u, pixelformat=%c%c%c%c, wxh=%ux%u, type=%u",
     790                 :            :                         p->index,
     791                 :            :                         (p->pixel_format & 0xff),
     792                 :            :                         (p->pixel_format >>  8) & 0xff,
     793                 :            :                         (p->pixel_format >> 16) & 0xff,
     794                 :            :                         (p->pixel_format >> 24) & 0xff,
     795                 :            :                         p->width, p->height, p->type);
     796                 :          0 :         switch (p->type) {
     797                 :            :         case V4L2_FRMIVAL_TYPE_DISCRETE:
     798                 :          0 :                 pr_cont(", fps=%d/%d\n",
     799                 :            :                                 p->discrete.numerator,
     800                 :            :                                 p->discrete.denominator);
     801                 :          0 :                 break;
     802                 :            :         case V4L2_FRMIVAL_TYPE_STEPWISE:
     803                 :          0 :                 pr_cont(", min=%d/%d, max=%d/%d, step=%d/%d\n",
     804                 :            :                                 p->stepwise.min.numerator,
     805                 :            :                                 p->stepwise.min.denominator,
     806                 :            :                                 p->stepwise.max.numerator,
     807                 :            :                                 p->stepwise.max.denominator,
     808                 :            :                                 p->stepwise.step.numerator,
     809                 :            :                                 p->stepwise.step.denominator);
     810                 :          0 :                 break;
     811                 :            :         case V4L2_FRMIVAL_TYPE_CONTINUOUS:
     812                 :            :                 /* fall through */
     813                 :            :         default:
     814                 :          0 :                 pr_cont("\n");
     815                 :          0 :                 break;
     816                 :            :         }
     817                 :          0 : }
     818                 :            : 
     819                 :          0 : static void v4l_print_event(const void *arg, bool write_only)
     820                 :            : {
     821                 :            :         const struct v4l2_event *p = arg;
     822                 :            :         const struct v4l2_event_ctrl *c;
     823                 :            : 
     824                 :          0 :         pr_cont("type=0x%x, pending=%u, sequence=%u, id=%u, timestamp=%lu.%9.9lu\n",
     825                 :            :                         p->type, p->pending, p->sequence, p->id,
     826                 :            :                         p->timestamp.tv_sec, p->timestamp.tv_nsec);
     827                 :          0 :         switch (p->type) {
     828                 :            :         case V4L2_EVENT_VSYNC:
     829                 :          0 :                 printk(KERN_DEBUG "field=%s\n",
     830                 :          0 :                         prt_names(p->u.vsync.field, v4l2_field_names));
     831                 :          0 :                 break;
     832                 :            :         case V4L2_EVENT_CTRL:
     833                 :            :                 c = &p->u.ctrl;
     834                 :          0 :                 printk(KERN_DEBUG "changes=0x%x, type=%u, ",
     835                 :            :                         c->changes, c->type);
     836                 :          0 :                 if (c->type == V4L2_CTRL_TYPE_INTEGER64)
     837                 :          0 :                         pr_cont("value64=%lld, ", c->value64);
     838                 :            :                 else
     839                 :          0 :                         pr_cont("value=%d, ", c->value);
     840                 :          0 :                 pr_cont("flags=0x%x, minimum=%d, maximum=%d, step=%d, default_value=%d\n",
     841                 :            :                         c->flags, c->minimum, c->maximum,
     842                 :            :                         c->step, c->default_value);
     843                 :          0 :                 break;
     844                 :            :         case V4L2_EVENT_FRAME_SYNC:
     845                 :          0 :                 pr_cont("frame_sequence=%u\n",
     846                 :            :                         p->u.frame_sync.frame_sequence);
     847                 :          0 :                 break;
     848                 :            :         }
     849                 :          0 : }
     850                 :            : 
     851                 :          0 : static void v4l_print_event_subscription(const void *arg, bool write_only)
     852                 :            : {
     853                 :            :         const struct v4l2_event_subscription *p = arg;
     854                 :            : 
     855                 :          0 :         pr_cont("type=0x%x, id=0x%x, flags=0x%x\n",
     856                 :            :                         p->type, p->id, p->flags);
     857                 :          0 : }
     858                 :            : 
     859                 :          0 : static void v4l_print_sliced_vbi_cap(const void *arg, bool write_only)
     860                 :            : {
     861                 :            :         const struct v4l2_sliced_vbi_cap *p = arg;
     862                 :            :         int i;
     863                 :            : 
     864                 :          0 :         pr_cont("type=%s, service_set=0x%08x\n",
     865                 :            :                         prt_names(p->type, v4l2_type_names), p->service_set);
     866                 :          0 :         for (i = 0; i < 24; i++)
     867                 :          0 :                 printk(KERN_DEBUG "line[%02u]=0x%04x, 0x%04x\n", i,
     868                 :          0 :                                 p->service_lines[0][i],
     869                 :          0 :                                 p->service_lines[1][i]);
     870                 :          0 : }
     871                 :            : 
     872                 :          0 : static void v4l_print_freq_band(const void *arg, bool write_only)
     873                 :            : {
     874                 :            :         const struct v4l2_frequency_band *p = arg;
     875                 :            : 
     876                 :          0 :         pr_cont("tuner=%u, type=%u, index=%u, capability=0x%x, rangelow=%u, rangehigh=%u, modulation=0x%x\n",
     877                 :            :                         p->tuner, p->type, p->index,
     878                 :            :                         p->capability, p->rangelow,
     879                 :            :                         p->rangehigh, p->modulation);
     880                 :          0 : }
     881                 :            : 
     882                 :          0 : static void v4l_print_edid(const void *arg, bool write_only)
     883                 :            : {
     884                 :            :         const struct v4l2_edid *p = arg;
     885                 :            : 
     886                 :          0 :         pr_cont("pad=%u, start_block=%u, blocks=%u\n",
     887                 :            :                 p->pad, p->start_block, p->blocks);
     888                 :          0 : }
     889                 :            : 
     890                 :          0 : static void v4l_print_u32(const void *arg, bool write_only)
     891                 :            : {
     892                 :          0 :         pr_cont("value=%u\n", *(const u32 *)arg);
     893                 :          0 : }
     894                 :            : 
     895                 :          0 : static void v4l_print_newline(const void *arg, bool write_only)
     896                 :            : {
     897                 :          0 :         pr_cont("\n");
     898                 :          0 : }
     899                 :            : 
     900                 :          0 : static void v4l_print_default(const void *arg, bool write_only)
     901                 :            : {
     902                 :          0 :         pr_cont("driver-specific ioctl\n");
     903                 :          0 : }
     904                 :            : 
     905                 :          0 : static int check_ext_ctrls(struct v4l2_ext_controls *c, int allow_priv)
     906                 :            : {
     907                 :            :         __u32 i;
     908                 :            : 
     909                 :            :         /* zero the reserved fields */
     910                 :          0 :         c->reserved[0] = 0;
     911                 :          0 :         for (i = 0; i < c->count; i++)
     912                 :          0 :                 c->controls[i].reserved2[0] = 0;
     913                 :            : 
     914                 :            :         /* V4L2_CID_PRIVATE_BASE cannot be used as control class
     915                 :            :            when using extended controls.
     916                 :            :            Only when passed in through VIDIOC_G_CTRL and VIDIOC_S_CTRL
     917                 :            :            is it allowed for backwards compatibility.
     918                 :            :          */
     919                 :          0 :         if (!allow_priv && c->which == V4L2_CID_PRIVATE_BASE)
     920                 :            :                 return 0;
     921                 :          0 :         if (!c->which)
     922                 :            :                 return 1;
     923                 :            :         /* Check that all controls are from the same control class. */
     924                 :          0 :         for (i = 0; i < c->count; i++) {
     925                 :          0 :                 if (V4L2_CTRL_ID2WHICH(c->controls[i].id) != c->which) {
     926                 :          0 :                         c->error_idx = i;
     927                 :          0 :                         return 0;
     928                 :            :                 }
     929                 :            :         }
     930                 :            :         return 1;
     931                 :            : }
     932                 :            : 
     933                 :          0 : static int check_fmt(struct file *file, enum v4l2_buf_type type)
     934                 :            : {
     935                 :          0 :         struct video_device *vfd = video_devdata(file);
     936                 :          0 :         const struct v4l2_ioctl_ops *ops = vfd->ioctl_ops;
     937                 :          0 :         bool is_vid = vfd->vfl_type == VFL_TYPE_GRABBER;
     938                 :          0 :         bool is_vbi = vfd->vfl_type == VFL_TYPE_VBI;
     939                 :          0 :         bool is_sdr = vfd->vfl_type == VFL_TYPE_SDR;
     940                 :          0 :         bool is_tch = vfd->vfl_type == VFL_TYPE_TOUCH;
     941                 :          0 :         bool is_rx = vfd->vfl_dir != VFL_DIR_TX;
     942                 :          0 :         bool is_tx = vfd->vfl_dir != VFL_DIR_RX;
     943                 :            : 
     944                 :          0 :         if (ops == NULL)
     945                 :            :                 return -EINVAL;
     946                 :            : 
     947                 :          0 :         switch (type) {
     948                 :            :         case V4L2_BUF_TYPE_VIDEO_CAPTURE:
     949                 :          0 :                 if ((is_vid || is_tch) && is_rx &&
     950                 :          0 :                     (ops->vidioc_g_fmt_vid_cap || ops->vidioc_g_fmt_vid_cap_mplane))
     951                 :            :                         return 0;
     952                 :            :                 break;
     953                 :            :         case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
     954                 :          0 :                 if (is_vid && is_rx && ops->vidioc_g_fmt_vid_cap_mplane)
     955                 :            :                         return 0;
     956                 :            :                 break;
     957                 :            :         case V4L2_BUF_TYPE_VIDEO_OVERLAY:
     958                 :          0 :                 if (is_vid && is_rx && ops->vidioc_g_fmt_vid_overlay)
     959                 :            :                         return 0;
     960                 :            :                 break;
     961                 :            :         case V4L2_BUF_TYPE_VIDEO_OUTPUT:
     962                 :          0 :                 if (is_vid && is_tx &&
     963                 :          0 :                     (ops->vidioc_g_fmt_vid_out || ops->vidioc_g_fmt_vid_out_mplane))
     964                 :            :                         return 0;
     965                 :            :                 break;
     966                 :            :         case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
     967                 :          0 :                 if (is_vid && is_tx && ops->vidioc_g_fmt_vid_out_mplane)
     968                 :            :                         return 0;
     969                 :            :                 break;
     970                 :            :         case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
     971                 :          0 :                 if (is_vid && is_tx && ops->vidioc_g_fmt_vid_out_overlay)
     972                 :            :                         return 0;
     973                 :            :                 break;
     974                 :            :         case V4L2_BUF_TYPE_VBI_CAPTURE:
     975                 :          0 :                 if (is_vbi && is_rx && ops->vidioc_g_fmt_vbi_cap)
     976                 :            :                         return 0;
     977                 :            :                 break;
     978                 :            :         case V4L2_BUF_TYPE_VBI_OUTPUT:
     979                 :          0 :                 if (is_vbi && is_tx && ops->vidioc_g_fmt_vbi_out)
     980                 :            :                         return 0;
     981                 :            :                 break;
     982                 :            :         case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
     983                 :          0 :                 if (is_vbi && is_rx && ops->vidioc_g_fmt_sliced_vbi_cap)
     984                 :            :                         return 0;
     985                 :            :                 break;
     986                 :            :         case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
     987                 :          0 :                 if (is_vbi && is_tx && ops->vidioc_g_fmt_sliced_vbi_out)
     988                 :            :                         return 0;
     989                 :            :                 break;
     990                 :            :         case V4L2_BUF_TYPE_SDR_CAPTURE:
     991                 :          0 :                 if (is_sdr && is_rx && ops->vidioc_g_fmt_sdr_cap)
     992                 :            :                         return 0;
     993                 :            :                 break;
     994                 :            :         case V4L2_BUF_TYPE_SDR_OUTPUT:
     995                 :          0 :                 if (is_sdr && is_tx && ops->vidioc_g_fmt_sdr_out)
     996                 :            :                         return 0;
     997                 :            :                 break;
     998                 :            :         case V4L2_BUF_TYPE_META_CAPTURE:
     999                 :          0 :                 if (is_vid && is_rx && ops->vidioc_g_fmt_meta_cap)
    1000                 :            :                         return 0;
    1001                 :            :                 break;
    1002                 :            :         case V4L2_BUF_TYPE_META_OUTPUT:
    1003                 :          0 :                 if (is_vid && is_tx && ops->vidioc_g_fmt_meta_out)
    1004                 :            :                         return 0;
    1005                 :            :                 break;
    1006                 :            :         default:
    1007                 :            :                 break;
    1008                 :            :         }
    1009                 :          0 :         return -EINVAL;
    1010                 :            : }
    1011                 :            : 
    1012                 :          0 : static void v4l_sanitize_format(struct v4l2_format *fmt)
    1013                 :            : {
    1014                 :            :         unsigned int offset;
    1015                 :            : 
    1016                 :            :         /* Make sure num_planes is not bogus */
    1017                 :          0 :         if (fmt->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE ||
    1018                 :            :             fmt->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
    1019                 :          0 :                 fmt->fmt.pix_mp.num_planes = min_t(u32, fmt->fmt.pix_mp.num_planes,
    1020                 :            :                                                VIDEO_MAX_PLANES);
    1021                 :            : 
    1022                 :            :         /*
    1023                 :            :          * The v4l2_pix_format structure has been extended with fields that were
    1024                 :            :          * not previously required to be set to zero by applications. The priv
    1025                 :            :          * field, when set to a magic value, indicates the the extended fields
    1026                 :            :          * are valid. Otherwise they will contain undefined values. To simplify
    1027                 :            :          * the API towards drivers zero the extended fields and set the priv
    1028                 :            :          * field to the magic value when the extended pixel format structure
    1029                 :            :          * isn't used by applications.
    1030                 :            :          */
    1031                 :            : 
    1032                 :          0 :         if (fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
    1033                 :            :             fmt->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
    1034                 :            :                 return;
    1035                 :            : 
    1036                 :          0 :         if (fmt->fmt.pix.priv == V4L2_PIX_FMT_PRIV_MAGIC)
    1037                 :            :                 return;
    1038                 :            : 
    1039                 :          0 :         fmt->fmt.pix.priv = V4L2_PIX_FMT_PRIV_MAGIC;
    1040                 :            : 
    1041                 :            :         offset = offsetof(struct v4l2_pix_format, priv)
    1042                 :            :                + sizeof(fmt->fmt.pix.priv);
    1043                 :          0 :         memset(((void *)&fmt->fmt.pix) + offset, 0,
    1044                 :            :                sizeof(fmt->fmt.pix) - offset);
    1045                 :            : }
    1046                 :            : 
    1047                 :          3 : static int v4l_querycap(const struct v4l2_ioctl_ops *ops,
    1048                 :            :                                 struct file *file, void *fh, void *arg)
    1049                 :            : {
    1050                 :            :         struct v4l2_capability *cap = (struct v4l2_capability *)arg;
    1051                 :          3 :         struct video_device *vfd = video_devdata(file);
    1052                 :            :         int ret;
    1053                 :            : 
    1054                 :          3 :         cap->version = LINUX_VERSION_CODE;
    1055                 :          3 :         cap->device_caps = vfd->device_caps;
    1056                 :          3 :         cap->capabilities = vfd->device_caps | V4L2_CAP_DEVICE_CAPS;
    1057                 :            : 
    1058                 :          3 :         ret = ops->vidioc_querycap(file, fh, cap);
    1059                 :            : 
    1060                 :            :         /*
    1061                 :            :          * Drivers must not change device_caps, so check for this and
    1062                 :            :          * warn if this happened.
    1063                 :            :          */
    1064                 :          3 :         WARN_ON(cap->device_caps != vfd->device_caps);
    1065                 :            :         /*
    1066                 :            :          * Check that capabilities is a superset of
    1067                 :            :          * vfd->device_caps | V4L2_CAP_DEVICE_CAPS
    1068                 :            :          */
    1069                 :          3 :         WARN_ON((cap->capabilities &
    1070                 :            :                  (vfd->device_caps | V4L2_CAP_DEVICE_CAPS)) !=
    1071                 :            :                 (vfd->device_caps | V4L2_CAP_DEVICE_CAPS));
    1072                 :          3 :         cap->capabilities |= V4L2_CAP_EXT_PIX_FORMAT;
    1073                 :          3 :         cap->device_caps |= V4L2_CAP_EXT_PIX_FORMAT;
    1074                 :            : 
    1075                 :          3 :         return ret;
    1076                 :            : }
    1077                 :            : 
    1078                 :          0 : static int v4l_s_input(const struct v4l2_ioctl_ops *ops,
    1079                 :            :                                 struct file *file, void *fh, void *arg)
    1080                 :            : {
    1081                 :          0 :         struct video_device *vfd = video_devdata(file);
    1082                 :            :         int ret;
    1083                 :            : 
    1084                 :          0 :         ret = v4l_enable_media_source(vfd);
    1085                 :          0 :         if (ret)
    1086                 :            :                 return ret;
    1087                 :          0 :         return ops->vidioc_s_input(file, fh, *(unsigned int *)arg);
    1088                 :            : }
    1089                 :            : 
    1090                 :          0 : static int v4l_s_output(const struct v4l2_ioctl_ops *ops,
    1091                 :            :                                 struct file *file, void *fh, void *arg)
    1092                 :            : {
    1093                 :          0 :         return ops->vidioc_s_output(file, fh, *(unsigned int *)arg);
    1094                 :            : }
    1095                 :            : 
    1096                 :          0 : static int v4l_g_priority(const struct v4l2_ioctl_ops *ops,
    1097                 :            :                                 struct file *file, void *fh, void *arg)
    1098                 :            : {
    1099                 :            :         struct video_device *vfd;
    1100                 :            :         u32 *p = arg;
    1101                 :            : 
    1102                 :          0 :         vfd = video_devdata(file);
    1103                 :          0 :         *p = v4l2_prio_max(vfd->prio);
    1104                 :          0 :         return 0;
    1105                 :            : }
    1106                 :            : 
    1107                 :          0 : static int v4l_s_priority(const struct v4l2_ioctl_ops *ops,
    1108                 :            :                                 struct file *file, void *fh, void *arg)
    1109                 :            : {
    1110                 :            :         struct video_device *vfd;
    1111                 :            :         struct v4l2_fh *vfh;
    1112                 :            :         u32 *p = arg;
    1113                 :            : 
    1114                 :          0 :         vfd = video_devdata(file);
    1115                 :          0 :         if (!test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags))
    1116                 :            :                 return -ENOTTY;
    1117                 :          0 :         vfh = file->private_data;
    1118                 :          0 :         return v4l2_prio_change(vfd->prio, &vfh->prio, *p);
    1119                 :            : }
    1120                 :            : 
    1121                 :          0 : static int v4l_enuminput(const struct v4l2_ioctl_ops *ops,
    1122                 :            :                                 struct file *file, void *fh, void *arg)
    1123                 :            : {
    1124                 :          0 :         struct video_device *vfd = video_devdata(file);
    1125                 :            :         struct v4l2_input *p = arg;
    1126                 :            : 
    1127                 :            :         /*
    1128                 :            :          * We set the flags for CAP_DV_TIMINGS &
    1129                 :            :          * CAP_STD here based on ioctl handler provided by the
    1130                 :            :          * driver. If the driver doesn't support these
    1131                 :            :          * for a specific input, it must override these flags.
    1132                 :            :          */
    1133                 :          0 :         if (is_valid_ioctl(vfd, VIDIOC_S_STD))
    1134                 :          0 :                 p->capabilities |= V4L2_IN_CAP_STD;
    1135                 :            : 
    1136                 :          0 :         return ops->vidioc_enum_input(file, fh, p);
    1137                 :            : }
    1138                 :            : 
    1139                 :          0 : static int v4l_enumoutput(const struct v4l2_ioctl_ops *ops,
    1140                 :            :                                 struct file *file, void *fh, void *arg)
    1141                 :            : {
    1142                 :          0 :         struct video_device *vfd = video_devdata(file);
    1143                 :            :         struct v4l2_output *p = arg;
    1144                 :            : 
    1145                 :            :         /*
    1146                 :            :          * We set the flags for CAP_DV_TIMINGS &
    1147                 :            :          * CAP_STD here based on ioctl handler provided by the
    1148                 :            :          * driver. If the driver doesn't support these
    1149                 :            :          * for a specific output, it must override these flags.
    1150                 :            :          */
    1151                 :          0 :         if (is_valid_ioctl(vfd, VIDIOC_S_STD))
    1152                 :          0 :                 p->capabilities |= V4L2_OUT_CAP_STD;
    1153                 :            : 
    1154                 :          0 :         return ops->vidioc_enum_output(file, fh, p);
    1155                 :            : }
    1156                 :            : 
    1157                 :          0 : static void v4l_fill_fmtdesc(struct v4l2_fmtdesc *fmt)
    1158                 :            : {
    1159                 :            :         const unsigned sz = sizeof(fmt->description);
    1160                 :            :         const char *descr = NULL;
    1161                 :            :         u32 flags = 0;
    1162                 :            : 
    1163                 :            :         /*
    1164                 :            :          * We depart from the normal coding style here since the descriptions
    1165                 :            :          * should be aligned so it is easy to see which descriptions will be
    1166                 :            :          * longer than 31 characters (the max length for a description).
    1167                 :            :          * And frankly, this is easier to read anyway.
    1168                 :            :          *
    1169                 :            :          * Note that gcc will use O(log N) comparisons to find the right case.
    1170                 :            :          */
    1171                 :          0 :         switch (fmt->pixelformat) {
    1172                 :            :         /* Max description length mask: descr = "0123456789012345678901234567890" */
    1173                 :            :         case V4L2_PIX_FMT_RGB332:       descr = "8-bit RGB 3-3-2"; break;
    1174                 :          0 :         case V4L2_PIX_FMT_RGB444:       descr = "16-bit A/XRGB 4-4-4-4"; break;
    1175                 :          0 :         case V4L2_PIX_FMT_ARGB444:      descr = "16-bit ARGB 4-4-4-4"; break;
    1176                 :          0 :         case V4L2_PIX_FMT_XRGB444:      descr = "16-bit XRGB 4-4-4-4"; break;
    1177                 :          0 :         case V4L2_PIX_FMT_RGBA444:      descr = "16-bit RGBA 4-4-4-4"; break;
    1178                 :          0 :         case V4L2_PIX_FMT_RGBX444:      descr = "16-bit RGBX 4-4-4-4"; break;
    1179                 :          0 :         case V4L2_PIX_FMT_ABGR444:      descr = "16-bit ABGR 4-4-4-4"; break;
    1180                 :          0 :         case V4L2_PIX_FMT_XBGR444:      descr = "16-bit XBGR 4-4-4-4"; break;
    1181                 :          0 :         case V4L2_PIX_FMT_BGRA444:      descr = "16-bit BGRA 4-4-4-4"; break;
    1182                 :          0 :         case V4L2_PIX_FMT_BGRX444:      descr = "16-bit BGRX 4-4-4-4"; break;
    1183                 :          0 :         case V4L2_PIX_FMT_RGB555:       descr = "16-bit A/XRGB 1-5-5-5"; break;
    1184                 :          0 :         case V4L2_PIX_FMT_ARGB555:      descr = "16-bit ARGB 1-5-5-5"; break;
    1185                 :          0 :         case V4L2_PIX_FMT_XRGB555:      descr = "16-bit XRGB 1-5-5-5"; break;
    1186                 :          0 :         case V4L2_PIX_FMT_ABGR555:      descr = "16-bit ABGR 1-5-5-5"; break;
    1187                 :          0 :         case V4L2_PIX_FMT_XBGR555:      descr = "16-bit XBGR 1-5-5-5"; break;
    1188                 :          0 :         case V4L2_PIX_FMT_RGBA555:      descr = "16-bit RGBA 5-5-5-1"; break;
    1189                 :          0 :         case V4L2_PIX_FMT_RGBX555:      descr = "16-bit RGBX 5-5-5-1"; break;
    1190                 :          0 :         case V4L2_PIX_FMT_BGRA555:      descr = "16-bit BGRA 5-5-5-1"; break;
    1191                 :          0 :         case V4L2_PIX_FMT_BGRX555:      descr = "16-bit BGRX 5-5-5-1"; break;
    1192                 :          0 :         case V4L2_PIX_FMT_RGB565:       descr = "16-bit RGB 5-6-5"; break;
    1193                 :          0 :         case V4L2_PIX_FMT_RGB555X:      descr = "16-bit A/XRGB 1-5-5-5 BE"; break;
    1194                 :          0 :         case V4L2_PIX_FMT_ARGB555X:     descr = "16-bit ARGB 1-5-5-5 BE"; break;
    1195                 :          0 :         case V4L2_PIX_FMT_XRGB555X:     descr = "16-bit XRGB 1-5-5-5 BE"; break;
    1196                 :          0 :         case V4L2_PIX_FMT_RGB565X:      descr = "16-bit RGB 5-6-5 BE"; break;
    1197                 :          0 :         case V4L2_PIX_FMT_BGR666:       descr = "18-bit BGRX 6-6-6-14"; break;
    1198                 :          0 :         case V4L2_PIX_FMT_BGR24:        descr = "24-bit BGR 8-8-8"; break;
    1199                 :          0 :         case V4L2_PIX_FMT_RGB24:        descr = "24-bit RGB 8-8-8"; break;
    1200                 :          0 :         case V4L2_PIX_FMT_BGR32:        descr = "32-bit BGRA/X 8-8-8-8"; break;
    1201                 :          0 :         case V4L2_PIX_FMT_ABGR32:       descr = "32-bit BGRA 8-8-8-8"; break;
    1202                 :          0 :         case V4L2_PIX_FMT_XBGR32:       descr = "32-bit BGRX 8-8-8-8"; break;
    1203                 :          0 :         case V4L2_PIX_FMT_RGB32:        descr = "32-bit A/XRGB 8-8-8-8"; break;
    1204                 :          0 :         case V4L2_PIX_FMT_ARGB32:       descr = "32-bit ARGB 8-8-8-8"; break;
    1205                 :          0 :         case V4L2_PIX_FMT_XRGB32:       descr = "32-bit XRGB 8-8-8-8"; break;
    1206                 :          0 :         case V4L2_PIX_FMT_BGRA32:       descr = "32-bit ABGR 8-8-8-8"; break;
    1207                 :          0 :         case V4L2_PIX_FMT_BGRX32:       descr = "32-bit XBGR 8-8-8-8"; break;
    1208                 :          0 :         case V4L2_PIX_FMT_RGBA32:       descr = "32-bit RGBA 8-8-8-8"; break;
    1209                 :          0 :         case V4L2_PIX_FMT_RGBX32:       descr = "32-bit RGBX 8-8-8-8"; break;
    1210                 :          0 :         case V4L2_PIX_FMT_GREY:         descr = "8-bit Greyscale"; break;
    1211                 :          0 :         case V4L2_PIX_FMT_Y4:           descr = "4-bit Greyscale"; break;
    1212                 :          0 :         case V4L2_PIX_FMT_Y6:           descr = "6-bit Greyscale"; break;
    1213                 :          0 :         case V4L2_PIX_FMT_Y10:          descr = "10-bit Greyscale"; break;
    1214                 :          0 :         case V4L2_PIX_FMT_Y12:          descr = "12-bit Greyscale"; break;
    1215                 :          0 :         case V4L2_PIX_FMT_Y14:          descr = "14-bit Greyscale"; break;
    1216                 :          0 :         case V4L2_PIX_FMT_Y16:          descr = "16-bit Greyscale"; break;
    1217                 :          0 :         case V4L2_PIX_FMT_Y16_BE:       descr = "16-bit Greyscale BE"; break;
    1218                 :          0 :         case V4L2_PIX_FMT_Y10BPACK:     descr = "10-bit Greyscale (Packed)"; break;
    1219                 :          0 :         case V4L2_PIX_FMT_Y10P:         descr = "10-bit Greyscale (MIPI Packed)"; break;
    1220                 :          0 :         case V4L2_PIX_FMT_Y12P:         descr = "12-bit Greyscale (MIPI Packed)"; break;
    1221                 :          0 :         case V4L2_PIX_FMT_Y14P:         descr = "14-bit Greyscale (MIPI Packed)"; break;
    1222                 :          0 :         case V4L2_PIX_FMT_Y8I:          descr = "Interleaved 8-bit Greyscale"; break;
    1223                 :          0 :         case V4L2_PIX_FMT_Y12I:         descr = "Interleaved 12-bit Greyscale"; break;
    1224                 :          0 :         case V4L2_PIX_FMT_Z16:          descr = "16-bit Depth"; break;
    1225                 :          0 :         case V4L2_PIX_FMT_INZI:         descr = "Planar 10:16 Greyscale Depth"; break;
    1226                 :          0 :         case V4L2_PIX_FMT_CNF4:         descr = "4-bit Depth Confidence (Packed)"; break;
    1227                 :          0 :         case V4L2_PIX_FMT_PAL8:         descr = "8-bit Palette"; break;
    1228                 :          0 :         case V4L2_PIX_FMT_UV8:          descr = "8-bit Chrominance UV 4-4"; break;
    1229                 :          0 :         case V4L2_PIX_FMT_YVU410:       descr = "Planar YVU 4:1:0"; break;
    1230                 :          0 :         case V4L2_PIX_FMT_YVU420:       descr = "Planar YVU 4:2:0"; break;
    1231                 :          0 :         case V4L2_PIX_FMT_YUYV:         descr = "YUYV 4:2:2"; break;
    1232                 :          0 :         case V4L2_PIX_FMT_YYUV:         descr = "YYUV 4:2:2"; break;
    1233                 :          0 :         case V4L2_PIX_FMT_YVYU:         descr = "YVYU 4:2:2"; break;
    1234                 :          0 :         case V4L2_PIX_FMT_UYVY:         descr = "UYVY 4:2:2"; break;
    1235                 :          0 :         case V4L2_PIX_FMT_VYUY:         descr = "VYUY 4:2:2"; break;
    1236                 :          0 :         case V4L2_PIX_FMT_YUV422P:      descr = "Planar YUV 4:2:2"; break;
    1237                 :          0 :         case V4L2_PIX_FMT_YUV411P:      descr = "Planar YUV 4:1:1"; break;
    1238                 :          0 :         case V4L2_PIX_FMT_Y41P:         descr = "YUV 4:1:1 (Packed)"; break;
    1239                 :          0 :         case V4L2_PIX_FMT_YUV444:       descr = "16-bit A/XYUV 4-4-4-4"; break;
    1240                 :          0 :         case V4L2_PIX_FMT_YUV555:       descr = "16-bit A/XYUV 1-5-5-5"; break;
    1241                 :          0 :         case V4L2_PIX_FMT_YUV565:       descr = "16-bit YUV 5-6-5"; break;
    1242                 :          0 :         case V4L2_PIX_FMT_YUV32:        descr = "32-bit A/XYUV 8-8-8-8"; break;
    1243                 :          0 :         case V4L2_PIX_FMT_AYUV32:       descr = "32-bit AYUV 8-8-8-8"; break;
    1244                 :          0 :         case V4L2_PIX_FMT_XYUV32:       descr = "32-bit XYUV 8-8-8-8"; break;
    1245                 :          0 :         case V4L2_PIX_FMT_VUYA32:       descr = "32-bit VUYA 8-8-8-8"; break;
    1246                 :          0 :         case V4L2_PIX_FMT_VUYX32:       descr = "32-bit VUYX 8-8-8-8"; break;
    1247                 :          0 :         case V4L2_PIX_FMT_YUV410:       descr = "Planar YUV 4:1:0"; break;
    1248                 :          0 :         case V4L2_PIX_FMT_YUV420:       descr = "Planar YUV 4:2:0"; break;
    1249                 :          0 :         case V4L2_PIX_FMT_HI240:        descr = "8-bit Dithered RGB (BTTV)"; break;
    1250                 :          0 :         case V4L2_PIX_FMT_HM12:         descr = "YUV 4:2:0 (16x16 Macroblocks)"; break;
    1251                 :          0 :         case V4L2_PIX_FMT_M420:         descr = "YUV 4:2:0 (M420)"; break;
    1252                 :          0 :         case V4L2_PIX_FMT_NV12:         descr = "Y/CbCr 4:2:0"; break;
    1253                 :          0 :         case V4L2_PIX_FMT_NV21:         descr = "Y/CrCb 4:2:0"; break;
    1254                 :          0 :         case V4L2_PIX_FMT_NV16:         descr = "Y/CbCr 4:2:2"; break;
    1255                 :          0 :         case V4L2_PIX_FMT_NV61:         descr = "Y/CrCb 4:2:2"; break;
    1256                 :          0 :         case V4L2_PIX_FMT_NV24:         descr = "Y/CbCr 4:4:4"; break;
    1257                 :          0 :         case V4L2_PIX_FMT_NV42:         descr = "Y/CrCb 4:4:4"; break;
    1258                 :          0 :         case V4L2_PIX_FMT_NV12M:        descr = "Y/CbCr 4:2:0 (N-C)"; break;
    1259                 :          0 :         case V4L2_PIX_FMT_NV21M:        descr = "Y/CrCb 4:2:0 (N-C)"; break;
    1260                 :          0 :         case V4L2_PIX_FMT_NV16M:        descr = "Y/CbCr 4:2:2 (N-C)"; break;
    1261                 :          0 :         case V4L2_PIX_FMT_NV61M:        descr = "Y/CrCb 4:2:2 (N-C)"; break;
    1262                 :          0 :         case V4L2_PIX_FMT_NV12MT:       descr = "Y/CbCr 4:2:0 (64x32 MB, N-C)"; break;
    1263                 :          0 :         case V4L2_PIX_FMT_NV12MT_16X16: descr = "Y/CbCr 4:2:0 (16x16 MB, N-C)"; break;
    1264                 :          0 :         case V4L2_PIX_FMT_NV12_COL128:  descr = "Y/CbCr 4:2:0 (128b cols)"; break;
    1265                 :          0 :         case V4L2_PIX_FMT_NV12_10_COL128: descr = "10-bit Y/CbCr 4:2:0 (128b cols)"; break;
    1266                 :          0 :         case V4L2_PIX_FMT_YUV420M:      descr = "Planar YUV 4:2:0 (N-C)"; break;
    1267                 :          0 :         case V4L2_PIX_FMT_YVU420M:      descr = "Planar YVU 4:2:0 (N-C)"; break;
    1268                 :          0 :         case V4L2_PIX_FMT_YUV422M:      descr = "Planar YUV 4:2:2 (N-C)"; break;
    1269                 :          0 :         case V4L2_PIX_FMT_YVU422M:      descr = "Planar YVU 4:2:2 (N-C)"; break;
    1270                 :          0 :         case V4L2_PIX_FMT_YUV444M:      descr = "Planar YUV 4:4:4 (N-C)"; break;
    1271                 :          0 :         case V4L2_PIX_FMT_YVU444M:      descr = "Planar YVU 4:4:4 (N-C)"; break;
    1272                 :          0 :         case V4L2_PIX_FMT_SBGGR8:       descr = "8-bit Bayer BGBG/GRGR"; break;
    1273                 :          0 :         case V4L2_PIX_FMT_SGBRG8:       descr = "8-bit Bayer GBGB/RGRG"; break;
    1274                 :          0 :         case V4L2_PIX_FMT_SGRBG8:       descr = "8-bit Bayer GRGR/BGBG"; break;
    1275                 :          0 :         case V4L2_PIX_FMT_SRGGB8:       descr = "8-bit Bayer RGRG/GBGB"; break;
    1276                 :          0 :         case V4L2_PIX_FMT_SBGGR10:      descr = "10-bit Bayer BGBG/GRGR"; break;
    1277                 :          0 :         case V4L2_PIX_FMT_SGBRG10:      descr = "10-bit Bayer GBGB/RGRG"; break;
    1278                 :          0 :         case V4L2_PIX_FMT_SGRBG10:      descr = "10-bit Bayer GRGR/BGBG"; break;
    1279                 :          0 :         case V4L2_PIX_FMT_SRGGB10:      descr = "10-bit Bayer RGRG/GBGB"; break;
    1280                 :          0 :         case V4L2_PIX_FMT_SBGGR10P:     descr = "10-bit Bayer BGBG/GRGR Packed"; break;
    1281                 :          0 :         case V4L2_PIX_FMT_SGBRG10P:     descr = "10-bit Bayer GBGB/RGRG Packed"; break;
    1282                 :          0 :         case V4L2_PIX_FMT_SGRBG10P:     descr = "10-bit Bayer GRGR/BGBG Packed"; break;
    1283                 :          0 :         case V4L2_PIX_FMT_SRGGB10P:     descr = "10-bit Bayer RGRG/GBGB Packed"; break;
    1284                 :          0 :         case V4L2_PIX_FMT_IPU3_SBGGR10: descr = "10-bit bayer BGGR IPU3 Packed"; break;
    1285                 :          0 :         case V4L2_PIX_FMT_IPU3_SGBRG10: descr = "10-bit bayer GBRG IPU3 Packed"; break;
    1286                 :          0 :         case V4L2_PIX_FMT_IPU3_SGRBG10: descr = "10-bit bayer GRBG IPU3 Packed"; break;
    1287                 :          0 :         case V4L2_PIX_FMT_IPU3_SRGGB10: descr = "10-bit bayer RGGB IPU3 Packed"; break;
    1288                 :          0 :         case V4L2_PIX_FMT_SBGGR10ALAW8: descr = "8-bit Bayer BGBG/GRGR (A-law)"; break;
    1289                 :          0 :         case V4L2_PIX_FMT_SGBRG10ALAW8: descr = "8-bit Bayer GBGB/RGRG (A-law)"; break;
    1290                 :          0 :         case V4L2_PIX_FMT_SGRBG10ALAW8: descr = "8-bit Bayer GRGR/BGBG (A-law)"; break;
    1291                 :          0 :         case V4L2_PIX_FMT_SRGGB10ALAW8: descr = "8-bit Bayer RGRG/GBGB (A-law)"; break;
    1292                 :          0 :         case V4L2_PIX_FMT_SBGGR10DPCM8: descr = "8-bit Bayer BGBG/GRGR (DPCM)"; break;
    1293                 :          0 :         case V4L2_PIX_FMT_SGBRG10DPCM8: descr = "8-bit Bayer GBGB/RGRG (DPCM)"; break;
    1294                 :          0 :         case V4L2_PIX_FMT_SGRBG10DPCM8: descr = "8-bit Bayer GRGR/BGBG (DPCM)"; break;
    1295                 :          0 :         case V4L2_PIX_FMT_SRGGB10DPCM8: descr = "8-bit Bayer RGRG/GBGB (DPCM)"; break;
    1296                 :          0 :         case V4L2_PIX_FMT_SBGGR12:      descr = "12-bit Bayer BGBG/GRGR"; break;
    1297                 :          0 :         case V4L2_PIX_FMT_SGBRG12:      descr = "12-bit Bayer GBGB/RGRG"; break;
    1298                 :          0 :         case V4L2_PIX_FMT_SGRBG12:      descr = "12-bit Bayer GRGR/BGBG"; break;
    1299                 :          0 :         case V4L2_PIX_FMT_SRGGB12:      descr = "12-bit Bayer RGRG/GBGB"; break;
    1300                 :          0 :         case V4L2_PIX_FMT_SBGGR12P:     descr = "12-bit Bayer BGBG/GRGR Packed"; break;
    1301                 :          0 :         case V4L2_PIX_FMT_SGBRG12P:     descr = "12-bit Bayer GBGB/RGRG Packed"; break;
    1302                 :          0 :         case V4L2_PIX_FMT_SGRBG12P:     descr = "12-bit Bayer GRGR/BGBG Packed"; break;
    1303                 :          0 :         case V4L2_PIX_FMT_SRGGB12P:     descr = "12-bit Bayer RGRG/GBGB Packed"; break;
    1304                 :          0 :         case V4L2_PIX_FMT_SBGGR14:      descr = "14-bit Bayer BGBG/GRGR"; break;
    1305                 :          0 :         case V4L2_PIX_FMT_SGBRG14:      descr = "14-bit Bayer GBGB/RGRG"; break;
    1306                 :          0 :         case V4L2_PIX_FMT_SGRBG14:      descr = "14-bit Bayer GRGR/BGBG"; break;
    1307                 :          0 :         case V4L2_PIX_FMT_SRGGB14:      descr = "14-bit Bayer RGRG/GBGB"; break;
    1308                 :          0 :         case V4L2_PIX_FMT_SBGGR14P:     descr = "14-bit Bayer BGBG/GRGR Packed"; break;
    1309                 :          0 :         case V4L2_PIX_FMT_SGBRG14P:     descr = "14-bit Bayer GBGB/RGRG Packed"; break;
    1310                 :          0 :         case V4L2_PIX_FMT_SGRBG14P:     descr = "14-bit Bayer GRGR/BGBG Packed"; break;
    1311                 :          0 :         case V4L2_PIX_FMT_SRGGB14P:     descr = "14-bit Bayer RGRG/GBGB Packed"; break;
    1312                 :          0 :         case V4L2_PIX_FMT_SBGGR16:      descr = "16-bit Bayer BGBG/GRGR"; break;
    1313                 :          0 :         case V4L2_PIX_FMT_SGBRG16:      descr = "16-bit Bayer GBGB/RGRG"; break;
    1314                 :          0 :         case V4L2_PIX_FMT_SGRBG16:      descr = "16-bit Bayer GRGR/BGBG"; break;
    1315                 :          0 :         case V4L2_PIX_FMT_SRGGB16:      descr = "16-bit Bayer RGRG/GBGB"; break;
    1316                 :          0 :         case V4L2_PIX_FMT_SN9C20X_I420: descr = "GSPCA SN9C20X I420"; break;
    1317                 :          0 :         case V4L2_PIX_FMT_SPCA501:      descr = "GSPCA SPCA501"; break;
    1318                 :          0 :         case V4L2_PIX_FMT_SPCA505:      descr = "GSPCA SPCA505"; break;
    1319                 :          0 :         case V4L2_PIX_FMT_SPCA508:      descr = "GSPCA SPCA508"; break;
    1320                 :          0 :         case V4L2_PIX_FMT_STV0680:      descr = "GSPCA STV0680"; break;
    1321                 :          0 :         case V4L2_PIX_FMT_TM6000:       descr = "A/V + VBI Mux Packet"; break;
    1322                 :          0 :         case V4L2_PIX_FMT_CIT_YYVYUY:   descr = "GSPCA CIT YYVYUY"; break;
    1323                 :          0 :         case V4L2_PIX_FMT_KONICA420:    descr = "GSPCA KONICA420"; break;
    1324                 :          0 :         case V4L2_PIX_FMT_HSV24:        descr = "24-bit HSV 8-8-8"; break;
    1325                 :          0 :         case V4L2_PIX_FMT_HSV32:        descr = "32-bit XHSV 8-8-8-8"; break;
    1326                 :          0 :         case V4L2_SDR_FMT_CU8:          descr = "Complex U8"; break;
    1327                 :          0 :         case V4L2_SDR_FMT_CU16LE:       descr = "Complex U16LE"; break;
    1328                 :          0 :         case V4L2_SDR_FMT_CS8:          descr = "Complex S8"; break;
    1329                 :          0 :         case V4L2_SDR_FMT_CS14LE:       descr = "Complex S14LE"; break;
    1330                 :          0 :         case V4L2_SDR_FMT_RU12LE:       descr = "Real U12LE"; break;
    1331                 :          0 :         case V4L2_SDR_FMT_PCU16BE:      descr = "Planar Complex U16BE"; break;
    1332                 :          0 :         case V4L2_SDR_FMT_PCU18BE:      descr = "Planar Complex U18BE"; break;
    1333                 :          0 :         case V4L2_SDR_FMT_PCU20BE:      descr = "Planar Complex U20BE"; break;
    1334                 :          0 :         case V4L2_TCH_FMT_DELTA_TD16:   descr = "16-bit Signed Deltas"; break;
    1335                 :          0 :         case V4L2_TCH_FMT_DELTA_TD08:   descr = "8-bit Signed Deltas"; break;
    1336                 :          0 :         case V4L2_TCH_FMT_TU16:         descr = "16-bit Unsigned Touch Data"; break;
    1337                 :          0 :         case V4L2_TCH_FMT_TU08:         descr = "8-bit Unsigned Touch Data"; break;
    1338                 :          0 :         case V4L2_META_FMT_VSP1_HGO:    descr = "R-Car VSP1 1-D Histogram"; break;
    1339                 :          0 :         case V4L2_META_FMT_VSP1_HGT:    descr = "R-Car VSP1 2-D Histogram"; break;
    1340                 :          0 :         case V4L2_META_FMT_UVC:         descr = "UVC Payload Header Metadata"; break;
    1341                 :          0 :         case V4L2_META_FMT_D4XX:        descr = "Intel D4xx UVC Metadata"; break;
    1342                 :          0 :         case V4L2_META_FMT_SENSOR_DATA: descr = "Sensor Ancillary Metadata"; break;
    1343                 :          0 :         case V4L2_META_FMT_BCM2835_ISP_STATS: descr = "BCM2835 ISP Image Statistics"; break;
    1344                 :            : 
    1345                 :            :         default:
    1346                 :            :                 /* Compressed formats */
    1347                 :            :                 flags = V4L2_FMT_FLAG_COMPRESSED;
    1348                 :          0 :                 switch (fmt->pixelformat) {
    1349                 :            :                 /* Max description length mask: descr = "0123456789012345678901234567890" */
    1350                 :            :                 case V4L2_PIX_FMT_MJPEG:        descr = "Motion-JPEG"; break;
    1351                 :          0 :                 case V4L2_PIX_FMT_JPEG:         descr = "JFIF JPEG"; break;
    1352                 :          0 :                 case V4L2_PIX_FMT_DV:           descr = "1394"; break;
    1353                 :          0 :                 case V4L2_PIX_FMT_MPEG:         descr = "MPEG-1/2/4"; break;
    1354                 :          0 :                 case V4L2_PIX_FMT_H264:         descr = "H.264"; break;
    1355                 :          0 :                 case V4L2_PIX_FMT_H264_NO_SC:   descr = "H.264 (No Start Codes)"; break;
    1356                 :          0 :                 case V4L2_PIX_FMT_H264_MVC:     descr = "H.264 MVC"; break;
    1357                 :          0 :                 case V4L2_PIX_FMT_H264_SLICE:   descr = "H.264 Parsed Slice Data"; break;
    1358                 :          0 :                 case V4L2_PIX_FMT_H263:         descr = "H.263"; break;
    1359                 :          0 :                 case V4L2_PIX_FMT_MPEG1:        descr = "MPEG-1 ES"; break;
    1360                 :          0 :                 case V4L2_PIX_FMT_MPEG2:        descr = "MPEG-2 ES"; break;
    1361                 :          0 :                 case V4L2_PIX_FMT_MPEG2_SLICE:  descr = "MPEG-2 Parsed Slice Data"; break;
    1362                 :          0 :                 case V4L2_PIX_FMT_MPEG4:        descr = "MPEG-4 Part 2 ES"; break;
    1363                 :          0 :                 case V4L2_PIX_FMT_XVID:         descr = "Xvid"; break;
    1364                 :          0 :                 case V4L2_PIX_FMT_VC1_ANNEX_G:  descr = "VC-1 (SMPTE 412M Annex G)"; break;
    1365                 :          0 :                 case V4L2_PIX_FMT_VC1_ANNEX_L:  descr = "VC-1 (SMPTE 412M Annex L)"; break;
    1366                 :          0 :                 case V4L2_PIX_FMT_VP8:          descr = "VP8"; break;
    1367                 :          0 :                 case V4L2_PIX_FMT_VP8_FRAME:    descr = "VP8 Frame"; break;
    1368                 :          0 :                 case V4L2_PIX_FMT_VP9:          descr = "VP9"; break;
    1369                 :          0 :                 case V4L2_PIX_FMT_HEVC:         descr = "HEVC"; break; /* aka H.265 */
    1370                 :          0 :                 case V4L2_PIX_FMT_HEVC_SLICE:   descr = "HEVC Parsed Slice Data"; break;
    1371                 :          0 :                 case V4L2_PIX_FMT_FWHT:         descr = "FWHT"; break; /* used in vicodec */
    1372                 :          0 :                 case V4L2_PIX_FMT_FWHT_STATELESS:       descr = "FWHT Stateless"; break; /* used in vicodec */
    1373                 :          0 :                 case V4L2_PIX_FMT_CPIA1:        descr = "GSPCA CPiA YUV"; break;
    1374                 :          0 :                 case V4L2_PIX_FMT_WNVA:         descr = "WNVA"; break;
    1375                 :          0 :                 case V4L2_PIX_FMT_SN9C10X:      descr = "GSPCA SN9C10X"; break;
    1376                 :          0 :                 case V4L2_PIX_FMT_PWC1:         descr = "Raw Philips Webcam Type (Old)"; break;
    1377                 :          0 :                 case V4L2_PIX_FMT_PWC2:         descr = "Raw Philips Webcam Type (New)"; break;
    1378                 :          0 :                 case V4L2_PIX_FMT_ET61X251:     descr = "GSPCA ET61X251"; break;
    1379                 :          0 :                 case V4L2_PIX_FMT_SPCA561:      descr = "GSPCA SPCA561"; break;
    1380                 :          0 :                 case V4L2_PIX_FMT_PAC207:       descr = "GSPCA PAC207"; break;
    1381                 :          0 :                 case V4L2_PIX_FMT_MR97310A:     descr = "GSPCA MR97310A"; break;
    1382                 :          0 :                 case V4L2_PIX_FMT_JL2005BCD:    descr = "GSPCA JL2005BCD"; break;
    1383                 :          0 :                 case V4L2_PIX_FMT_SN9C2028:     descr = "GSPCA SN9C2028"; break;
    1384                 :          0 :                 case V4L2_PIX_FMT_SQ905C:       descr = "GSPCA SQ905C"; break;
    1385                 :          0 :                 case V4L2_PIX_FMT_PJPG:         descr = "GSPCA PJPG"; break;
    1386                 :          0 :                 case V4L2_PIX_FMT_OV511:        descr = "GSPCA OV511"; break;
    1387                 :          0 :                 case V4L2_PIX_FMT_OV518:        descr = "GSPCA OV518"; break;
    1388                 :          0 :                 case V4L2_PIX_FMT_JPGL:         descr = "JPEG Lite"; break;
    1389                 :          0 :                 case V4L2_PIX_FMT_SE401:        descr = "GSPCA SE401"; break;
    1390                 :          0 :                 case V4L2_PIX_FMT_S5C_UYVY_JPG: descr = "S5C73MX interleaved UYVY/JPEG"; break;
    1391                 :          0 :                 case V4L2_PIX_FMT_MT21C:        descr = "Mediatek Compressed Format"; break;
    1392                 :          0 :                 case V4L2_PIX_FMT_SUNXI_TILED_NV12: descr = "Sunxi Tiled NV12 Format"; break;
    1393                 :            :                 default:
    1394                 :          0 :                         if (fmt->description[0])
    1395                 :          0 :                                 return;
    1396                 :          0 :                         WARN(1, "Unknown pixelformat 0x%08x\n", fmt->pixelformat);
    1397                 :            :                         flags = 0;
    1398                 :          0 :                         snprintf(fmt->description, sz, "%c%c%c%c%s",
    1399                 :          0 :                                         (char)(fmt->pixelformat & 0x7f),
    1400                 :          0 :                                         (char)((fmt->pixelformat >> 8) & 0x7f),
    1401                 :          0 :                                         (char)((fmt->pixelformat >> 16) & 0x7f),
    1402                 :          0 :                                         (char)((fmt->pixelformat >> 24) & 0x7f),
    1403                 :          0 :                                         (fmt->pixelformat & (1UL << 31)) ? "-BE" : "");
    1404                 :          0 :                         break;
    1405                 :            :                 }
    1406                 :            :         }
    1407                 :            : 
    1408                 :          0 :         if (descr)
    1409                 :          0 :                 WARN_ON(strscpy(fmt->description, descr, sz) < 0);
    1410                 :          0 :         fmt->flags |= flags;
    1411                 :            : }
    1412                 :            : 
    1413                 :          0 : static int v4l_enum_fmt(const struct v4l2_ioctl_ops *ops,
    1414                 :            :                                 struct file *file, void *fh, void *arg)
    1415                 :            : {
    1416                 :          0 :         struct video_device *vdev = video_devdata(file);
    1417                 :            :         struct v4l2_fmtdesc *p = arg;
    1418                 :          0 :         int ret = check_fmt(file, p->type);
    1419                 :            :         u32 cap_mask;
    1420                 :            : 
    1421                 :          0 :         if (ret)
    1422                 :            :                 return ret;
    1423                 :            :         ret = -EINVAL;
    1424                 :            : 
    1425                 :          0 :         switch (p->type) {
    1426                 :            :         case V4L2_BUF_TYPE_VIDEO_CAPTURE:
    1427                 :            :         case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
    1428                 :            :                 cap_mask = V4L2_CAP_VIDEO_CAPTURE_MPLANE |
    1429                 :            :                            V4L2_CAP_VIDEO_M2M_MPLANE;
    1430                 :          0 :                 if (!!(vdev->device_caps & cap_mask) !=
    1431                 :            :                     (p->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE))
    1432                 :            :                         break;
    1433                 :            : 
    1434                 :          0 :                 if (unlikely(!ops->vidioc_enum_fmt_vid_cap))
    1435                 :            :                         break;
    1436                 :          0 :                 ret = ops->vidioc_enum_fmt_vid_cap(file, fh, arg);
    1437                 :          0 :                 break;
    1438                 :            :         case V4L2_BUF_TYPE_VIDEO_OVERLAY:
    1439                 :          0 :                 if (unlikely(!ops->vidioc_enum_fmt_vid_overlay))
    1440                 :            :                         break;
    1441                 :          0 :                 ret = ops->vidioc_enum_fmt_vid_overlay(file, fh, arg);
    1442                 :          0 :                 break;
    1443                 :            :         case V4L2_BUF_TYPE_VIDEO_OUTPUT:
    1444                 :            :         case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
    1445                 :            :                 cap_mask = V4L2_CAP_VIDEO_OUTPUT_MPLANE |
    1446                 :            :                            V4L2_CAP_VIDEO_M2M_MPLANE;
    1447                 :          0 :                 if (!!(vdev->device_caps & cap_mask) !=
    1448                 :            :                     (p->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE))
    1449                 :            :                         break;
    1450                 :            : 
    1451                 :          0 :                 if (unlikely(!ops->vidioc_enum_fmt_vid_out))
    1452                 :            :                         break;
    1453                 :          0 :                 ret = ops->vidioc_enum_fmt_vid_out(file, fh, arg);
    1454                 :          0 :                 break;
    1455                 :            :         case V4L2_BUF_TYPE_SDR_CAPTURE:
    1456                 :          0 :                 if (unlikely(!ops->vidioc_enum_fmt_sdr_cap))
    1457                 :            :                         break;
    1458                 :          0 :                 ret = ops->vidioc_enum_fmt_sdr_cap(file, fh, arg);
    1459                 :          0 :                 break;
    1460                 :            :         case V4L2_BUF_TYPE_SDR_OUTPUT:
    1461                 :          0 :                 if (unlikely(!ops->vidioc_enum_fmt_sdr_out))
    1462                 :            :                         break;
    1463                 :          0 :                 ret = ops->vidioc_enum_fmt_sdr_out(file, fh, arg);
    1464                 :          0 :                 break;
    1465                 :            :         case V4L2_BUF_TYPE_META_CAPTURE:
    1466                 :          0 :                 if (unlikely(!ops->vidioc_enum_fmt_meta_cap))
    1467                 :            :                         break;
    1468                 :          0 :                 ret = ops->vidioc_enum_fmt_meta_cap(file, fh, arg);
    1469                 :          0 :                 break;
    1470                 :            :         case V4L2_BUF_TYPE_META_OUTPUT:
    1471                 :          0 :                 if (unlikely(!ops->vidioc_enum_fmt_meta_out))
    1472                 :            :                         break;
    1473                 :          0 :                 ret = ops->vidioc_enum_fmt_meta_out(file, fh, arg);
    1474                 :          0 :                 break;
    1475                 :            :         }
    1476                 :          0 :         if (ret == 0)
    1477                 :          0 :                 v4l_fill_fmtdesc(p);
    1478                 :          0 :         return ret;
    1479                 :            : }
    1480                 :            : 
    1481                 :            : static void v4l_pix_format_touch(struct v4l2_pix_format *p)
    1482                 :            : {
    1483                 :            :         /*
    1484                 :            :          * The v4l2_pix_format structure contains fields that make no sense for
    1485                 :            :          * touch. Set them to default values in this case.
    1486                 :            :          */
    1487                 :            : 
    1488                 :          0 :         p->field = V4L2_FIELD_NONE;
    1489                 :          0 :         p->colorspace = V4L2_COLORSPACE_RAW;
    1490                 :          0 :         p->flags = 0;
    1491                 :          0 :         p->ycbcr_enc = 0;
    1492                 :          0 :         p->quantization = 0;
    1493                 :          0 :         p->xfer_func = 0;
    1494                 :            : }
    1495                 :            : 
    1496                 :          0 : static int v4l_g_fmt(const struct v4l2_ioctl_ops *ops,
    1497                 :            :                                 struct file *file, void *fh, void *arg)
    1498                 :            : {
    1499                 :            :         struct v4l2_format *p = arg;
    1500                 :          0 :         struct video_device *vfd = video_devdata(file);
    1501                 :          0 :         int ret = check_fmt(file, p->type);
    1502                 :            : 
    1503                 :          0 :         if (ret)
    1504                 :            :                 return ret;
    1505                 :            : 
    1506                 :            :         /*
    1507                 :            :          * fmt can't be cleared for these overlay types due to the 'clips'
    1508                 :            :          * 'clipcount' and 'bitmap' pointers in struct v4l2_window.
    1509                 :            :          * Those are provided by the user. So handle these two overlay types
    1510                 :            :          * first, and then just do a simple memset for the other types.
    1511                 :            :          */
    1512                 :          0 :         switch (p->type) {
    1513                 :            :         case V4L2_BUF_TYPE_VIDEO_OVERLAY:
    1514                 :            :         case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY: {
    1515                 :          0 :                 struct v4l2_clip __user *clips = p->fmt.win.clips;
    1516                 :          0 :                 u32 clipcount = p->fmt.win.clipcount;
    1517                 :          0 :                 void __user *bitmap = p->fmt.win.bitmap;
    1518                 :            : 
    1519                 :          0 :                 memset(&p->fmt, 0, sizeof(p->fmt));
    1520                 :          0 :                 p->fmt.win.clips = clips;
    1521                 :          0 :                 p->fmt.win.clipcount = clipcount;
    1522                 :          0 :                 p->fmt.win.bitmap = bitmap;
    1523                 :          0 :                 break;
    1524                 :            :         }
    1525                 :            :         default:
    1526                 :          0 :                 memset(&p->fmt, 0, sizeof(p->fmt));
    1527                 :          0 :                 break;
    1528                 :            :         }
    1529                 :            : 
    1530                 :          0 :         switch (p->type) {
    1531                 :            :         case V4L2_BUF_TYPE_VIDEO_CAPTURE:
    1532                 :          0 :                 if (unlikely(!ops->vidioc_g_fmt_vid_cap))
    1533                 :            :                         break;
    1534                 :          0 :                 p->fmt.pix.priv = V4L2_PIX_FMT_PRIV_MAGIC;
    1535                 :          0 :                 ret = ops->vidioc_g_fmt_vid_cap(file, fh, arg);
    1536                 :            :                 /* just in case the driver zeroed it again */
    1537                 :          0 :                 p->fmt.pix.priv = V4L2_PIX_FMT_PRIV_MAGIC;
    1538                 :          0 :                 if (vfd->vfl_type == VFL_TYPE_TOUCH)
    1539                 :            :                         v4l_pix_format_touch(&p->fmt.pix);
    1540                 :          0 :                 return ret;
    1541                 :            :         case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
    1542                 :          0 :                 return ops->vidioc_g_fmt_vid_cap_mplane(file, fh, arg);
    1543                 :            :         case V4L2_BUF_TYPE_VIDEO_OVERLAY:
    1544                 :          0 :                 return ops->vidioc_g_fmt_vid_overlay(file, fh, arg);
    1545                 :            :         case V4L2_BUF_TYPE_VBI_CAPTURE:
    1546                 :          0 :                 return ops->vidioc_g_fmt_vbi_cap(file, fh, arg);
    1547                 :            :         case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
    1548                 :          0 :                 return ops->vidioc_g_fmt_sliced_vbi_cap(file, fh, arg);
    1549                 :            :         case V4L2_BUF_TYPE_VIDEO_OUTPUT:
    1550                 :          0 :                 if (unlikely(!ops->vidioc_g_fmt_vid_out))
    1551                 :            :                         break;
    1552                 :          0 :                 p->fmt.pix.priv = V4L2_PIX_FMT_PRIV_MAGIC;
    1553                 :          0 :                 ret = ops->vidioc_g_fmt_vid_out(file, fh, arg);
    1554                 :            :                 /* just in case the driver zeroed it again */
    1555                 :          0 :                 p->fmt.pix.priv = V4L2_PIX_FMT_PRIV_MAGIC;
    1556                 :          0 :                 return ret;
    1557                 :            :         case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
    1558                 :          0 :                 return ops->vidioc_g_fmt_vid_out_mplane(file, fh, arg);
    1559                 :            :         case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
    1560                 :          0 :                 return ops->vidioc_g_fmt_vid_out_overlay(file, fh, arg);
    1561                 :            :         case V4L2_BUF_TYPE_VBI_OUTPUT:
    1562                 :          0 :                 return ops->vidioc_g_fmt_vbi_out(file, fh, arg);
    1563                 :            :         case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
    1564                 :          0 :                 return ops->vidioc_g_fmt_sliced_vbi_out(file, fh, arg);
    1565                 :            :         case V4L2_BUF_TYPE_SDR_CAPTURE:
    1566                 :          0 :                 return ops->vidioc_g_fmt_sdr_cap(file, fh, arg);
    1567                 :            :         case V4L2_BUF_TYPE_SDR_OUTPUT:
    1568                 :          0 :                 return ops->vidioc_g_fmt_sdr_out(file, fh, arg);
    1569                 :            :         case V4L2_BUF_TYPE_META_CAPTURE:
    1570                 :          0 :                 return ops->vidioc_g_fmt_meta_cap(file, fh, arg);
    1571                 :            :         case V4L2_BUF_TYPE_META_OUTPUT:
    1572                 :          0 :                 return ops->vidioc_g_fmt_meta_out(file, fh, arg);
    1573                 :            :         }
    1574                 :            :         return -EINVAL;
    1575                 :            : }
    1576                 :            : 
    1577                 :          0 : static int v4l_s_fmt(const struct v4l2_ioctl_ops *ops,
    1578                 :            :                                 struct file *file, void *fh, void *arg)
    1579                 :            : {
    1580                 :            :         struct v4l2_format *p = arg;
    1581                 :          0 :         struct video_device *vfd = video_devdata(file);
    1582                 :          0 :         int ret = check_fmt(file, p->type);
    1583                 :            :         unsigned int i;
    1584                 :            : 
    1585                 :          0 :         if (ret)
    1586                 :            :                 return ret;
    1587                 :            : 
    1588                 :          0 :         ret = v4l_enable_media_source(vfd);
    1589                 :          0 :         if (ret)
    1590                 :            :                 return ret;
    1591                 :          0 :         v4l_sanitize_format(p);
    1592                 :            : 
    1593                 :          0 :         switch (p->type) {
    1594                 :            :         case V4L2_BUF_TYPE_VIDEO_CAPTURE:
    1595                 :          0 :                 if (unlikely(!ops->vidioc_s_fmt_vid_cap))
    1596                 :            :                         break;
    1597                 :          0 :                 CLEAR_AFTER_FIELD(p, fmt.pix);
    1598                 :          0 :                 ret = ops->vidioc_s_fmt_vid_cap(file, fh, arg);
    1599                 :            :                 /* just in case the driver zeroed it again */
    1600                 :          0 :                 p->fmt.pix.priv = V4L2_PIX_FMT_PRIV_MAGIC;
    1601                 :          0 :                 if (vfd->vfl_type == VFL_TYPE_TOUCH)
    1602                 :            :                         v4l_pix_format_touch(&p->fmt.pix);
    1603                 :          0 :                 return ret;
    1604                 :            :         case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
    1605                 :          0 :                 if (unlikely(!ops->vidioc_s_fmt_vid_cap_mplane))
    1606                 :            :                         break;
    1607                 :          0 :                 CLEAR_AFTER_FIELD(p, fmt.pix_mp.xfer_func);
    1608                 :          0 :                 for (i = 0; i < p->fmt.pix_mp.num_planes; i++)
    1609                 :          0 :                         CLEAR_AFTER_FIELD(&p->fmt.pix_mp.plane_fmt[i],
    1610                 :            :                                           bytesperline);
    1611                 :          0 :                 return ops->vidioc_s_fmt_vid_cap_mplane(file, fh, arg);
    1612                 :            :         case V4L2_BUF_TYPE_VIDEO_OVERLAY:
    1613                 :          0 :                 if (unlikely(!ops->vidioc_s_fmt_vid_overlay))
    1614                 :            :                         break;
    1615                 :          0 :                 CLEAR_AFTER_FIELD(p, fmt.win);
    1616                 :          0 :                 return ops->vidioc_s_fmt_vid_overlay(file, fh, arg);
    1617                 :            :         case V4L2_BUF_TYPE_VBI_CAPTURE:
    1618                 :          0 :                 if (unlikely(!ops->vidioc_s_fmt_vbi_cap))
    1619                 :            :                         break;
    1620                 :          0 :                 CLEAR_AFTER_FIELD(p, fmt.vbi.flags);
    1621                 :          0 :                 return ops->vidioc_s_fmt_vbi_cap(file, fh, arg);
    1622                 :            :         case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
    1623                 :          0 :                 if (unlikely(!ops->vidioc_s_fmt_sliced_vbi_cap))
    1624                 :            :                         break;
    1625                 :          0 :                 CLEAR_AFTER_FIELD(p, fmt.sliced.io_size);
    1626                 :          0 :                 return ops->vidioc_s_fmt_sliced_vbi_cap(file, fh, arg);
    1627                 :            :         case V4L2_BUF_TYPE_VIDEO_OUTPUT:
    1628                 :          0 :                 if (unlikely(!ops->vidioc_s_fmt_vid_out))
    1629                 :            :                         break;
    1630                 :          0 :                 CLEAR_AFTER_FIELD(p, fmt.pix);
    1631                 :          0 :                 ret = ops->vidioc_s_fmt_vid_out(file, fh, arg);
    1632                 :            :                 /* just in case the driver zeroed it again */
    1633                 :          0 :                 p->fmt.pix.priv = V4L2_PIX_FMT_PRIV_MAGIC;
    1634                 :          0 :                 return ret;
    1635                 :            :         case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
    1636                 :          0 :                 if (unlikely(!ops->vidioc_s_fmt_vid_out_mplane))
    1637                 :            :                         break;
    1638                 :          0 :                 CLEAR_AFTER_FIELD(p, fmt.pix_mp.xfer_func);
    1639                 :          0 :                 for (i = 0; i < p->fmt.pix_mp.num_planes; i++)
    1640                 :          0 :                         CLEAR_AFTER_FIELD(&p->fmt.pix_mp.plane_fmt[i],
    1641                 :            :                                           bytesperline);
    1642                 :          0 :                 return ops->vidioc_s_fmt_vid_out_mplane(file, fh, arg);
    1643                 :            :         case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
    1644                 :          0 :                 if (unlikely(!ops->vidioc_s_fmt_vid_out_overlay))
    1645                 :            :                         break;
    1646                 :          0 :                 CLEAR_AFTER_FIELD(p, fmt.win);
    1647                 :          0 :                 return ops->vidioc_s_fmt_vid_out_overlay(file, fh, arg);
    1648                 :            :         case V4L2_BUF_TYPE_VBI_OUTPUT:
    1649                 :          0 :                 if (unlikely(!ops->vidioc_s_fmt_vbi_out))
    1650                 :            :                         break;
    1651                 :          0 :                 CLEAR_AFTER_FIELD(p, fmt.vbi.flags);
    1652                 :          0 :                 return ops->vidioc_s_fmt_vbi_out(file, fh, arg);
    1653                 :            :         case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
    1654                 :          0 :                 if (unlikely(!ops->vidioc_s_fmt_sliced_vbi_out))
    1655                 :            :                         break;
    1656                 :          0 :                 CLEAR_AFTER_FIELD(p, fmt.sliced.io_size);
    1657                 :          0 :                 return ops->vidioc_s_fmt_sliced_vbi_out(file, fh, arg);
    1658                 :            :         case V4L2_BUF_TYPE_SDR_CAPTURE:
    1659                 :          0 :                 if (unlikely(!ops->vidioc_s_fmt_sdr_cap))
    1660                 :            :                         break;
    1661                 :          0 :                 CLEAR_AFTER_FIELD(p, fmt.sdr.buffersize);
    1662                 :          0 :                 return ops->vidioc_s_fmt_sdr_cap(file, fh, arg);
    1663                 :            :         case V4L2_BUF_TYPE_SDR_OUTPUT:
    1664                 :          0 :                 if (unlikely(!ops->vidioc_s_fmt_sdr_out))
    1665                 :            :                         break;
    1666                 :          0 :                 CLEAR_AFTER_FIELD(p, fmt.sdr.buffersize);
    1667                 :          0 :                 return ops->vidioc_s_fmt_sdr_out(file, fh, arg);
    1668                 :            :         case V4L2_BUF_TYPE_META_CAPTURE:
    1669                 :          0 :                 if (unlikely(!ops->vidioc_s_fmt_meta_cap))
    1670                 :            :                         break;
    1671                 :          0 :                 CLEAR_AFTER_FIELD(p, fmt.meta);
    1672                 :          0 :                 return ops->vidioc_s_fmt_meta_cap(file, fh, arg);
    1673                 :            :         case V4L2_BUF_TYPE_META_OUTPUT:
    1674                 :          0 :                 if (unlikely(!ops->vidioc_s_fmt_meta_out))
    1675                 :            :                         break;
    1676                 :          0 :                 CLEAR_AFTER_FIELD(p, fmt.meta);
    1677                 :          0 :                 return ops->vidioc_s_fmt_meta_out(file, fh, arg);
    1678                 :            :         }
    1679                 :            :         return -EINVAL;
    1680                 :            : }
    1681                 :            : 
    1682                 :          0 : static int v4l_try_fmt(const struct v4l2_ioctl_ops *ops,
    1683                 :            :                                 struct file *file, void *fh, void *arg)
    1684                 :            : {
    1685                 :            :         struct v4l2_format *p = arg;
    1686                 :          0 :         struct video_device *vfd = video_devdata(file);
    1687                 :          0 :         int ret = check_fmt(file, p->type);
    1688                 :            :         unsigned int i;
    1689                 :            : 
    1690                 :          0 :         if (ret)
    1691                 :            :                 return ret;
    1692                 :            : 
    1693                 :          0 :         v4l_sanitize_format(p);
    1694                 :            : 
    1695                 :          0 :         switch (p->type) {
    1696                 :            :         case V4L2_BUF_TYPE_VIDEO_CAPTURE:
    1697                 :          0 :                 if (unlikely(!ops->vidioc_try_fmt_vid_cap))
    1698                 :            :                         break;
    1699                 :          0 :                 CLEAR_AFTER_FIELD(p, fmt.pix);
    1700                 :          0 :                 ret = ops->vidioc_try_fmt_vid_cap(file, fh, arg);
    1701                 :            :                 /* just in case the driver zeroed it again */
    1702                 :          0 :                 p->fmt.pix.priv = V4L2_PIX_FMT_PRIV_MAGIC;
    1703                 :          0 :                 if (vfd->vfl_type == VFL_TYPE_TOUCH)
    1704                 :            :                         v4l_pix_format_touch(&p->fmt.pix);
    1705                 :          0 :                 return ret;
    1706                 :            :         case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
    1707                 :          0 :                 if (unlikely(!ops->vidioc_try_fmt_vid_cap_mplane))
    1708                 :            :                         break;
    1709                 :          0 :                 CLEAR_AFTER_FIELD(p, fmt.pix_mp.xfer_func);
    1710                 :          0 :                 for (i = 0; i < p->fmt.pix_mp.num_planes; i++)
    1711                 :          0 :                         CLEAR_AFTER_FIELD(&p->fmt.pix_mp.plane_fmt[i],
    1712                 :            :                                           bytesperline);
    1713                 :          0 :                 return ops->vidioc_try_fmt_vid_cap_mplane(file, fh, arg);
    1714                 :            :         case V4L2_BUF_TYPE_VIDEO_OVERLAY:
    1715                 :          0 :                 if (unlikely(!ops->vidioc_try_fmt_vid_overlay))
    1716                 :            :                         break;
    1717                 :          0 :                 CLEAR_AFTER_FIELD(p, fmt.win);
    1718                 :          0 :                 return ops->vidioc_try_fmt_vid_overlay(file, fh, arg);
    1719                 :            :         case V4L2_BUF_TYPE_VBI_CAPTURE:
    1720                 :          0 :                 if (unlikely(!ops->vidioc_try_fmt_vbi_cap))
    1721                 :            :                         break;
    1722                 :          0 :                 CLEAR_AFTER_FIELD(p, fmt.vbi.flags);
    1723                 :          0 :                 return ops->vidioc_try_fmt_vbi_cap(file, fh, arg);
    1724                 :            :         case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
    1725                 :          0 :                 if (unlikely(!ops->vidioc_try_fmt_sliced_vbi_cap))
    1726                 :            :                         break;
    1727                 :          0 :                 CLEAR_AFTER_FIELD(p, fmt.sliced.io_size);
    1728                 :          0 :                 return ops->vidioc_try_fmt_sliced_vbi_cap(file, fh, arg);
    1729                 :            :         case V4L2_BUF_TYPE_VIDEO_OUTPUT:
    1730                 :          0 :                 if (unlikely(!ops->vidioc_try_fmt_vid_out))
    1731                 :            :                         break;
    1732                 :          0 :                 CLEAR_AFTER_FIELD(p, fmt.pix);
    1733                 :          0 :                 ret = ops->vidioc_try_fmt_vid_out(file, fh, arg);
    1734                 :            :                 /* just in case the driver zeroed it again */
    1735                 :          0 :                 p->fmt.pix.priv = V4L2_PIX_FMT_PRIV_MAGIC;
    1736                 :          0 :                 return ret;
    1737                 :            :         case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
    1738                 :          0 :                 if (unlikely(!ops->vidioc_try_fmt_vid_out_mplane))
    1739                 :            :                         break;
    1740                 :          0 :                 CLEAR_AFTER_FIELD(p, fmt.pix_mp.xfer_func);
    1741                 :          0 :                 for (i = 0; i < p->fmt.pix_mp.num_planes; i++)
    1742                 :          0 :                         CLEAR_AFTER_FIELD(&p->fmt.pix_mp.plane_fmt[i],
    1743                 :            :                                           bytesperline);
    1744                 :          0 :                 return ops->vidioc_try_fmt_vid_out_mplane(file, fh, arg);
    1745                 :            :         case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
    1746                 :          0 :                 if (unlikely(!ops->vidioc_try_fmt_vid_out_overlay))
    1747                 :            :                         break;
    1748                 :          0 :                 CLEAR_AFTER_FIELD(p, fmt.win);
    1749                 :          0 :                 return ops->vidioc_try_fmt_vid_out_overlay(file, fh, arg);
    1750                 :            :         case V4L2_BUF_TYPE_VBI_OUTPUT:
    1751                 :          0 :                 if (unlikely(!ops->vidioc_try_fmt_vbi_out))
    1752                 :            :                         break;
    1753                 :          0 :                 CLEAR_AFTER_FIELD(p, fmt.vbi.flags);
    1754                 :          0 :                 return ops->vidioc_try_fmt_vbi_out(file, fh, arg);
    1755                 :            :         case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
    1756                 :          0 :                 if (unlikely(!ops->vidioc_try_fmt_sliced_vbi_out))
    1757                 :            :                         break;
    1758                 :          0 :                 CLEAR_AFTER_FIELD(p, fmt.sliced.io_size);
    1759                 :          0 :                 return ops->vidioc_try_fmt_sliced_vbi_out(file, fh, arg);
    1760                 :            :         case V4L2_BUF_TYPE_SDR_CAPTURE:
    1761                 :          0 :                 if (unlikely(!ops->vidioc_try_fmt_sdr_cap))
    1762                 :            :                         break;
    1763                 :          0 :                 CLEAR_AFTER_FIELD(p, fmt.sdr.buffersize);
    1764                 :          0 :                 return ops->vidioc_try_fmt_sdr_cap(file, fh, arg);
    1765                 :            :         case V4L2_BUF_TYPE_SDR_OUTPUT:
    1766                 :          0 :                 if (unlikely(!ops->vidioc_try_fmt_sdr_out))
    1767                 :            :                         break;
    1768                 :          0 :                 CLEAR_AFTER_FIELD(p, fmt.sdr.buffersize);
    1769                 :          0 :                 return ops->vidioc_try_fmt_sdr_out(file, fh, arg);
    1770                 :            :         case V4L2_BUF_TYPE_META_CAPTURE:
    1771                 :          0 :                 if (unlikely(!ops->vidioc_try_fmt_meta_cap))
    1772                 :            :                         break;
    1773                 :          0 :                 CLEAR_AFTER_FIELD(p, fmt.meta);
    1774                 :          0 :                 return ops->vidioc_try_fmt_meta_cap(file, fh, arg);
    1775                 :            :         case V4L2_BUF_TYPE_META_OUTPUT:
    1776                 :          0 :                 if (unlikely(!ops->vidioc_try_fmt_meta_out))
    1777                 :            :                         break;
    1778                 :          0 :                 CLEAR_AFTER_FIELD(p, fmt.meta);
    1779                 :          0 :                 return ops->vidioc_try_fmt_meta_out(file, fh, arg);
    1780                 :            :         }
    1781                 :            :         return -EINVAL;
    1782                 :            : }
    1783                 :            : 
    1784                 :          0 : static int v4l_streamon(const struct v4l2_ioctl_ops *ops,
    1785                 :            :                                 struct file *file, void *fh, void *arg)
    1786                 :            : {
    1787                 :          0 :         return ops->vidioc_streamon(file, fh, *(unsigned int *)arg);
    1788                 :            : }
    1789                 :            : 
    1790                 :          0 : static int v4l_streamoff(const struct v4l2_ioctl_ops *ops,
    1791                 :            :                                 struct file *file, void *fh, void *arg)
    1792                 :            : {
    1793                 :          0 :         return ops->vidioc_streamoff(file, fh, *(unsigned int *)arg);
    1794                 :            : }
    1795                 :            : 
    1796                 :          0 : static int v4l_g_tuner(const struct v4l2_ioctl_ops *ops,
    1797                 :            :                                 struct file *file, void *fh, void *arg)
    1798                 :            : {
    1799                 :          0 :         struct video_device *vfd = video_devdata(file);
    1800                 :            :         struct v4l2_tuner *p = arg;
    1801                 :            :         int err;
    1802                 :            : 
    1803                 :          0 :         p->type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
    1804                 :            :                         V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
    1805                 :          0 :         err = ops->vidioc_g_tuner(file, fh, p);
    1806                 :          0 :         if (!err)
    1807                 :          0 :                 p->capability |= V4L2_TUNER_CAP_FREQ_BANDS;
    1808                 :          0 :         return err;
    1809                 :            : }
    1810                 :            : 
    1811                 :          0 : static int v4l_s_tuner(const struct v4l2_ioctl_ops *ops,
    1812                 :            :                                 struct file *file, void *fh, void *arg)
    1813                 :            : {
    1814                 :          0 :         struct video_device *vfd = video_devdata(file);
    1815                 :            :         struct v4l2_tuner *p = arg;
    1816                 :            :         int ret;
    1817                 :            : 
    1818                 :          0 :         ret = v4l_enable_media_source(vfd);
    1819                 :          0 :         if (ret)
    1820                 :            :                 return ret;
    1821                 :          0 :         p->type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
    1822                 :            :                         V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
    1823                 :          0 :         return ops->vidioc_s_tuner(file, fh, p);
    1824                 :            : }
    1825                 :            : 
    1826                 :          0 : static int v4l_g_modulator(const struct v4l2_ioctl_ops *ops,
    1827                 :            :                                 struct file *file, void *fh, void *arg)
    1828                 :            : {
    1829                 :          0 :         struct video_device *vfd = video_devdata(file);
    1830                 :            :         struct v4l2_modulator *p = arg;
    1831                 :            :         int err;
    1832                 :            : 
    1833                 :          0 :         if (vfd->vfl_type == VFL_TYPE_RADIO)
    1834                 :          0 :                 p->type = V4L2_TUNER_RADIO;
    1835                 :            : 
    1836                 :          0 :         err = ops->vidioc_g_modulator(file, fh, p);
    1837                 :          0 :         if (!err)
    1838                 :          0 :                 p->capability |= V4L2_TUNER_CAP_FREQ_BANDS;
    1839                 :          0 :         return err;
    1840                 :            : }
    1841                 :            : 
    1842                 :          0 : static int v4l_s_modulator(const struct v4l2_ioctl_ops *ops,
    1843                 :            :                                 struct file *file, void *fh, void *arg)
    1844                 :            : {
    1845                 :          0 :         struct video_device *vfd = video_devdata(file);
    1846                 :            :         struct v4l2_modulator *p = arg;
    1847                 :            : 
    1848                 :          0 :         if (vfd->vfl_type == VFL_TYPE_RADIO)
    1849                 :          0 :                 p->type = V4L2_TUNER_RADIO;
    1850                 :            : 
    1851                 :          0 :         return ops->vidioc_s_modulator(file, fh, p);
    1852                 :            : }
    1853                 :            : 
    1854                 :          0 : static int v4l_g_frequency(const struct v4l2_ioctl_ops *ops,
    1855                 :            :                                 struct file *file, void *fh, void *arg)
    1856                 :            : {
    1857                 :          0 :         struct video_device *vfd = video_devdata(file);
    1858                 :            :         struct v4l2_frequency *p = arg;
    1859                 :            : 
    1860                 :          0 :         if (vfd->vfl_type == VFL_TYPE_SDR)
    1861                 :          0 :                 p->type = V4L2_TUNER_SDR;
    1862                 :            :         else
    1863                 :          0 :                 p->type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
    1864                 :            :                                 V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
    1865                 :          0 :         return ops->vidioc_g_frequency(file, fh, p);
    1866                 :            : }
    1867                 :            : 
    1868                 :          0 : static int v4l_s_frequency(const struct v4l2_ioctl_ops *ops,
    1869                 :            :                                 struct file *file, void *fh, void *arg)
    1870                 :            : {
    1871                 :          0 :         struct video_device *vfd = video_devdata(file);
    1872                 :            :         const struct v4l2_frequency *p = arg;
    1873                 :            :         enum v4l2_tuner_type type;
    1874                 :            :         int ret;
    1875                 :            : 
    1876                 :          0 :         ret = v4l_enable_media_source(vfd);
    1877                 :          0 :         if (ret)
    1878                 :            :                 return ret;
    1879                 :          0 :         if (vfd->vfl_type == VFL_TYPE_SDR) {
    1880                 :          0 :                 if (p->type != V4L2_TUNER_SDR && p->type != V4L2_TUNER_RF)
    1881                 :            :                         return -EINVAL;
    1882                 :            :         } else {
    1883                 :          0 :                 type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
    1884                 :            :                                 V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
    1885                 :          0 :                 if (type != p->type)
    1886                 :            :                         return -EINVAL;
    1887                 :            :         }
    1888                 :          0 :         return ops->vidioc_s_frequency(file, fh, p);
    1889                 :            : }
    1890                 :            : 
    1891                 :          0 : static int v4l_enumstd(const struct v4l2_ioctl_ops *ops,
    1892                 :            :                                 struct file *file, void *fh, void *arg)
    1893                 :            : {
    1894                 :          0 :         struct video_device *vfd = video_devdata(file);
    1895                 :            :         struct v4l2_standard *p = arg;
    1896                 :            : 
    1897                 :          0 :         return v4l_video_std_enumstd(p, vfd->tvnorms);
    1898                 :            : }
    1899                 :            : 
    1900                 :          0 : static int v4l_s_std(const struct v4l2_ioctl_ops *ops,
    1901                 :            :                                 struct file *file, void *fh, void *arg)
    1902                 :            : {
    1903                 :          0 :         struct video_device *vfd = video_devdata(file);
    1904                 :          0 :         v4l2_std_id id = *(v4l2_std_id *)arg, norm;
    1905                 :            :         int ret;
    1906                 :            : 
    1907                 :          0 :         ret = v4l_enable_media_source(vfd);
    1908                 :          0 :         if (ret)
    1909                 :            :                 return ret;
    1910                 :          0 :         norm = id & vfd->tvnorms;
    1911                 :          0 :         if (vfd->tvnorms && !norm)   /* Check if std is supported */
    1912                 :            :                 return -EINVAL;
    1913                 :            : 
    1914                 :            :         /* Calls the specific handler */
    1915                 :          0 :         return ops->vidioc_s_std(file, fh, norm);
    1916                 :            : }
    1917                 :            : 
    1918                 :          0 : static int v4l_querystd(const struct v4l2_ioctl_ops *ops,
    1919                 :            :                                 struct file *file, void *fh, void *arg)
    1920                 :            : {
    1921                 :          0 :         struct video_device *vfd = video_devdata(file);
    1922                 :            :         v4l2_std_id *p = arg;
    1923                 :            :         int ret;
    1924                 :            : 
    1925                 :          0 :         ret = v4l_enable_media_source(vfd);
    1926                 :          0 :         if (ret)
    1927                 :            :                 return ret;
    1928                 :            :         /*
    1929                 :            :          * If no signal is detected, then the driver should return
    1930                 :            :          * V4L2_STD_UNKNOWN. Otherwise it should return tvnorms with
    1931                 :            :          * any standards that do not apply removed.
    1932                 :            :          *
    1933                 :            :          * This means that tuners, audio and video decoders can join
    1934                 :            :          * their efforts to improve the standards detection.
    1935                 :            :          */
    1936                 :          0 :         *p = vfd->tvnorms;
    1937                 :          0 :         return ops->vidioc_querystd(file, fh, arg);
    1938                 :            : }
    1939                 :            : 
    1940                 :          0 : static int v4l_s_hw_freq_seek(const struct v4l2_ioctl_ops *ops,
    1941                 :            :                                 struct file *file, void *fh, void *arg)
    1942                 :            : {
    1943                 :          0 :         struct video_device *vfd = video_devdata(file);
    1944                 :            :         struct v4l2_hw_freq_seek *p = arg;
    1945                 :            :         enum v4l2_tuner_type type;
    1946                 :            :         int ret;
    1947                 :            : 
    1948                 :          0 :         ret = v4l_enable_media_source(vfd);
    1949                 :          0 :         if (ret)
    1950                 :            :                 return ret;
    1951                 :            :         /* s_hw_freq_seek is not supported for SDR for now */
    1952                 :          0 :         if (vfd->vfl_type == VFL_TYPE_SDR)
    1953                 :            :                 return -EINVAL;
    1954                 :            : 
    1955                 :          0 :         type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
    1956                 :            :                 V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
    1957                 :          0 :         if (p->type != type)
    1958                 :            :                 return -EINVAL;
    1959                 :          0 :         return ops->vidioc_s_hw_freq_seek(file, fh, p);
    1960                 :            : }
    1961                 :            : 
    1962                 :          0 : static int v4l_overlay(const struct v4l2_ioctl_ops *ops,
    1963                 :            :                                 struct file *file, void *fh, void *arg)
    1964                 :            : {
    1965                 :          0 :         return ops->vidioc_overlay(file, fh, *(unsigned int *)arg);
    1966                 :            : }
    1967                 :            : 
    1968                 :          0 : static int v4l_reqbufs(const struct v4l2_ioctl_ops *ops,
    1969                 :            :                                 struct file *file, void *fh, void *arg)
    1970                 :            : {
    1971                 :            :         struct v4l2_requestbuffers *p = arg;
    1972                 :          0 :         int ret = check_fmt(file, p->type);
    1973                 :            : 
    1974                 :          0 :         if (ret)
    1975                 :            :                 return ret;
    1976                 :            : 
    1977                 :          0 :         CLEAR_AFTER_FIELD(p, capabilities);
    1978                 :            : 
    1979                 :          0 :         return ops->vidioc_reqbufs(file, fh, p);
    1980                 :            : }
    1981                 :            : 
    1982                 :          0 : static int v4l_querybuf(const struct v4l2_ioctl_ops *ops,
    1983                 :            :                                 struct file *file, void *fh, void *arg)
    1984                 :            : {
    1985                 :            :         struct v4l2_buffer *p = arg;
    1986                 :          0 :         int ret = check_fmt(file, p->type);
    1987                 :            : 
    1988                 :          0 :         return ret ? ret : ops->vidioc_querybuf(file, fh, p);
    1989                 :            : }
    1990                 :            : 
    1991                 :          0 : static int v4l_qbuf(const struct v4l2_ioctl_ops *ops,
    1992                 :            :                                 struct file *file, void *fh, void *arg)
    1993                 :            : {
    1994                 :            :         struct v4l2_buffer *p = arg;
    1995                 :          0 :         int ret = check_fmt(file, p->type);
    1996                 :            : 
    1997                 :          0 :         return ret ? ret : ops->vidioc_qbuf(file, fh, p);
    1998                 :            : }
    1999                 :            : 
    2000                 :          0 : static int v4l_dqbuf(const struct v4l2_ioctl_ops *ops,
    2001                 :            :                                 struct file *file, void *fh, void *arg)
    2002                 :            : {
    2003                 :            :         struct v4l2_buffer *p = arg;
    2004                 :          0 :         int ret = check_fmt(file, p->type);
    2005                 :            : 
    2006                 :          0 :         return ret ? ret : ops->vidioc_dqbuf(file, fh, p);
    2007                 :            : }
    2008                 :            : 
    2009                 :          0 : static int v4l_create_bufs(const struct v4l2_ioctl_ops *ops,
    2010                 :            :                                 struct file *file, void *fh, void *arg)
    2011                 :            : {
    2012                 :            :         struct v4l2_create_buffers *create = arg;
    2013                 :          0 :         int ret = check_fmt(file, create->format.type);
    2014                 :            : 
    2015                 :          0 :         if (ret)
    2016                 :            :                 return ret;
    2017                 :            : 
    2018                 :          0 :         CLEAR_AFTER_FIELD(create, capabilities);
    2019                 :            : 
    2020                 :          0 :         v4l_sanitize_format(&create->format);
    2021                 :            : 
    2022                 :          0 :         ret = ops->vidioc_create_bufs(file, fh, create);
    2023                 :            : 
    2024                 :          0 :         if (create->format.type == V4L2_BUF_TYPE_VIDEO_CAPTURE ||
    2025                 :            :             create->format.type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
    2026                 :          0 :                 create->format.fmt.pix.priv = V4L2_PIX_FMT_PRIV_MAGIC;
    2027                 :            : 
    2028                 :          0 :         return ret;
    2029                 :            : }
    2030                 :            : 
    2031                 :          0 : static int v4l_prepare_buf(const struct v4l2_ioctl_ops *ops,
    2032                 :            :                                 struct file *file, void *fh, void *arg)
    2033                 :            : {
    2034                 :            :         struct v4l2_buffer *b = arg;
    2035                 :          0 :         int ret = check_fmt(file, b->type);
    2036                 :            : 
    2037                 :          0 :         return ret ? ret : ops->vidioc_prepare_buf(file, fh, b);
    2038                 :            : }
    2039                 :            : 
    2040                 :          0 : static int v4l_g_parm(const struct v4l2_ioctl_ops *ops,
    2041                 :            :                                 struct file *file, void *fh, void *arg)
    2042                 :            : {
    2043                 :            :         struct v4l2_streamparm *p = arg;
    2044                 :            :         v4l2_std_id std;
    2045                 :          0 :         int ret = check_fmt(file, p->type);
    2046                 :            : 
    2047                 :          0 :         if (ret)
    2048                 :            :                 return ret;
    2049                 :          0 :         if (ops->vidioc_g_parm)
    2050                 :          0 :                 return ops->vidioc_g_parm(file, fh, p);
    2051                 :          0 :         if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
    2052                 :            :             p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
    2053                 :            :                 return -EINVAL;
    2054                 :          0 :         p->parm.capture.readbuffers = 2;
    2055                 :          0 :         ret = ops->vidioc_g_std(file, fh, &std);
    2056                 :          0 :         if (ret == 0)
    2057                 :          0 :                 v4l2_video_std_frame_period(std, &p->parm.capture.timeperframe);
    2058                 :          0 :         return ret;
    2059                 :            : }
    2060                 :            : 
    2061                 :          0 : static int v4l_s_parm(const struct v4l2_ioctl_ops *ops,
    2062                 :            :                                 struct file *file, void *fh, void *arg)
    2063                 :            : {
    2064                 :            :         struct v4l2_streamparm *p = arg;
    2065                 :          0 :         int ret = check_fmt(file, p->type);
    2066                 :            : 
    2067                 :          0 :         if (ret)
    2068                 :            :                 return ret;
    2069                 :            : 
    2070                 :            :         /* Note: extendedmode is never used in drivers */
    2071                 :          0 :         if (V4L2_TYPE_IS_OUTPUT(p->type)) {
    2072                 :          0 :                 memset(p->parm.output.reserved, 0,
    2073                 :            :                        sizeof(p->parm.output.reserved));
    2074                 :          0 :                 p->parm.output.extendedmode = 0;
    2075                 :          0 :                 p->parm.output.outputmode &= V4L2_MODE_HIGHQUALITY;
    2076                 :            :         } else {
    2077                 :          0 :                 memset(p->parm.capture.reserved, 0,
    2078                 :            :                        sizeof(p->parm.capture.reserved));
    2079                 :          0 :                 p->parm.capture.extendedmode = 0;
    2080                 :          0 :                 p->parm.capture.capturemode &= V4L2_MODE_HIGHQUALITY;
    2081                 :            :         }
    2082                 :          0 :         return ops->vidioc_s_parm(file, fh, p);
    2083                 :            : }
    2084                 :            : 
    2085                 :          0 : static int v4l_queryctrl(const struct v4l2_ioctl_ops *ops,
    2086                 :            :                                 struct file *file, void *fh, void *arg)
    2087                 :            : {
    2088                 :          0 :         struct video_device *vfd = video_devdata(file);
    2089                 :            :         struct v4l2_queryctrl *p = arg;
    2090                 :            :         struct v4l2_fh *vfh =
    2091                 :          0 :                 test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
    2092                 :            : 
    2093                 :          0 :         if (vfh && vfh->ctrl_handler)
    2094                 :          0 :                 return v4l2_queryctrl(vfh->ctrl_handler, p);
    2095                 :          0 :         if (vfd->ctrl_handler)
    2096                 :          0 :                 return v4l2_queryctrl(vfd->ctrl_handler, p);
    2097                 :          0 :         if (ops->vidioc_queryctrl)
    2098                 :          0 :                 return ops->vidioc_queryctrl(file, fh, p);
    2099                 :            :         return -ENOTTY;
    2100                 :            : }
    2101                 :            : 
    2102                 :          0 : static int v4l_query_ext_ctrl(const struct v4l2_ioctl_ops *ops,
    2103                 :            :                                 struct file *file, void *fh, void *arg)
    2104                 :            : {
    2105                 :          0 :         struct video_device *vfd = video_devdata(file);
    2106                 :            :         struct v4l2_query_ext_ctrl *p = arg;
    2107                 :            :         struct v4l2_fh *vfh =
    2108                 :          0 :                 test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
    2109                 :            : 
    2110                 :          0 :         if (vfh && vfh->ctrl_handler)
    2111                 :          0 :                 return v4l2_query_ext_ctrl(vfh->ctrl_handler, p);
    2112                 :          0 :         if (vfd->ctrl_handler)
    2113                 :          0 :                 return v4l2_query_ext_ctrl(vfd->ctrl_handler, p);
    2114                 :          0 :         if (ops->vidioc_query_ext_ctrl)
    2115                 :          0 :                 return ops->vidioc_query_ext_ctrl(file, fh, p);
    2116                 :            :         return -ENOTTY;
    2117                 :            : }
    2118                 :            : 
    2119                 :          0 : static int v4l_querymenu(const struct v4l2_ioctl_ops *ops,
    2120                 :            :                                 struct file *file, void *fh, void *arg)
    2121                 :            : {
    2122                 :          0 :         struct video_device *vfd = video_devdata(file);
    2123                 :            :         struct v4l2_querymenu *p = arg;
    2124                 :            :         struct v4l2_fh *vfh =
    2125                 :          0 :                 test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
    2126                 :            : 
    2127                 :          0 :         if (vfh && vfh->ctrl_handler)
    2128                 :          0 :                 return v4l2_querymenu(vfh->ctrl_handler, p);
    2129                 :          0 :         if (vfd->ctrl_handler)
    2130                 :          0 :                 return v4l2_querymenu(vfd->ctrl_handler, p);
    2131                 :          0 :         if (ops->vidioc_querymenu)
    2132                 :          0 :                 return ops->vidioc_querymenu(file, fh, p);
    2133                 :            :         return -ENOTTY;
    2134                 :            : }
    2135                 :            : 
    2136                 :          0 : static int v4l_g_ctrl(const struct v4l2_ioctl_ops *ops,
    2137                 :            :                                 struct file *file, void *fh, void *arg)
    2138                 :            : {
    2139                 :          0 :         struct video_device *vfd = video_devdata(file);
    2140                 :            :         struct v4l2_control *p = arg;
    2141                 :            :         struct v4l2_fh *vfh =
    2142                 :          0 :                 test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
    2143                 :            :         struct v4l2_ext_controls ctrls;
    2144                 :            :         struct v4l2_ext_control ctrl;
    2145                 :            : 
    2146                 :          0 :         if (vfh && vfh->ctrl_handler)
    2147                 :          0 :                 return v4l2_g_ctrl(vfh->ctrl_handler, p);
    2148                 :          0 :         if (vfd->ctrl_handler)
    2149                 :          0 :                 return v4l2_g_ctrl(vfd->ctrl_handler, p);
    2150                 :          0 :         if (ops->vidioc_g_ctrl)
    2151                 :          0 :                 return ops->vidioc_g_ctrl(file, fh, p);
    2152                 :          0 :         if (ops->vidioc_g_ext_ctrls == NULL)
    2153                 :            :                 return -ENOTTY;
    2154                 :            : 
    2155                 :          0 :         ctrls.which = V4L2_CTRL_ID2WHICH(p->id);
    2156                 :          0 :         ctrls.count = 1;
    2157                 :          0 :         ctrls.controls = &ctrl;
    2158                 :          0 :         ctrl.id = p->id;
    2159                 :          0 :         ctrl.value = p->value;
    2160                 :          0 :         if (check_ext_ctrls(&ctrls, 1)) {
    2161                 :          0 :                 int ret = ops->vidioc_g_ext_ctrls(file, fh, &ctrls);
    2162                 :            : 
    2163                 :          0 :                 if (ret == 0)
    2164                 :          0 :                         p->value = ctrl.value;
    2165                 :          0 :                 return ret;
    2166                 :            :         }
    2167                 :            :         return -EINVAL;
    2168                 :            : }
    2169                 :            : 
    2170                 :          0 : static int v4l_s_ctrl(const struct v4l2_ioctl_ops *ops,
    2171                 :            :                                 struct file *file, void *fh, void *arg)
    2172                 :            : {
    2173                 :          0 :         struct video_device *vfd = video_devdata(file);
    2174                 :            :         struct v4l2_control *p = arg;
    2175                 :            :         struct v4l2_fh *vfh =
    2176                 :          0 :                 test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
    2177                 :            :         struct v4l2_ext_controls ctrls;
    2178                 :            :         struct v4l2_ext_control ctrl;
    2179                 :            : 
    2180                 :          0 :         if (vfh && vfh->ctrl_handler)
    2181                 :          0 :                 return v4l2_s_ctrl(vfh, vfh->ctrl_handler, p);
    2182                 :          0 :         if (vfd->ctrl_handler)
    2183                 :          0 :                 return v4l2_s_ctrl(NULL, vfd->ctrl_handler, p);
    2184                 :          0 :         if (ops->vidioc_s_ctrl)
    2185                 :          0 :                 return ops->vidioc_s_ctrl(file, fh, p);
    2186                 :          0 :         if (ops->vidioc_s_ext_ctrls == NULL)
    2187                 :            :                 return -ENOTTY;
    2188                 :            : 
    2189                 :          0 :         ctrls.which = V4L2_CTRL_ID2WHICH(p->id);
    2190                 :          0 :         ctrls.count = 1;
    2191                 :          0 :         ctrls.controls = &ctrl;
    2192                 :          0 :         ctrl.id = p->id;
    2193                 :          0 :         ctrl.value = p->value;
    2194                 :          0 :         if (check_ext_ctrls(&ctrls, 1))
    2195                 :          0 :                 return ops->vidioc_s_ext_ctrls(file, fh, &ctrls);
    2196                 :            :         return -EINVAL;
    2197                 :            : }
    2198                 :            : 
    2199                 :          0 : static int v4l_g_ext_ctrls(const struct v4l2_ioctl_ops *ops,
    2200                 :            :                                 struct file *file, void *fh, void *arg)
    2201                 :            : {
    2202                 :          0 :         struct video_device *vfd = video_devdata(file);
    2203                 :            :         struct v4l2_ext_controls *p = arg;
    2204                 :            :         struct v4l2_fh *vfh =
    2205                 :          0 :                 test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
    2206                 :            : 
    2207                 :          0 :         p->error_idx = p->count;
    2208                 :          0 :         if (vfh && vfh->ctrl_handler)
    2209                 :          0 :                 return v4l2_g_ext_ctrls(vfh->ctrl_handler,
    2210                 :          0 :                                         vfd, vfd->v4l2_dev->mdev, p);
    2211                 :          0 :         if (vfd->ctrl_handler)
    2212                 :          0 :                 return v4l2_g_ext_ctrls(vfd->ctrl_handler,
    2213                 :          0 :                                         vfd, vfd->v4l2_dev->mdev, p);
    2214                 :          0 :         if (ops->vidioc_g_ext_ctrls == NULL)
    2215                 :            :                 return -ENOTTY;
    2216                 :          0 :         return check_ext_ctrls(p, 0) ? ops->vidioc_g_ext_ctrls(file, fh, p) :
    2217                 :            :                                         -EINVAL;
    2218                 :            : }
    2219                 :            : 
    2220                 :          0 : static int v4l_s_ext_ctrls(const struct v4l2_ioctl_ops *ops,
    2221                 :            :                                 struct file *file, void *fh, void *arg)
    2222                 :            : {
    2223                 :          0 :         struct video_device *vfd = video_devdata(file);
    2224                 :            :         struct v4l2_ext_controls *p = arg;
    2225                 :            :         struct v4l2_fh *vfh =
    2226                 :          0 :                 test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
    2227                 :            : 
    2228                 :          0 :         p->error_idx = p->count;
    2229                 :          0 :         if (vfh && vfh->ctrl_handler)
    2230                 :          0 :                 return v4l2_s_ext_ctrls(vfh, vfh->ctrl_handler,
    2231                 :          0 :                                         vfd, vfd->v4l2_dev->mdev, p);
    2232                 :          0 :         if (vfd->ctrl_handler)
    2233                 :          0 :                 return v4l2_s_ext_ctrls(NULL, vfd->ctrl_handler,
    2234                 :          0 :                                         vfd, vfd->v4l2_dev->mdev, p);
    2235                 :          0 :         if (ops->vidioc_s_ext_ctrls == NULL)
    2236                 :            :                 return -ENOTTY;
    2237                 :          0 :         return check_ext_ctrls(p, 0) ? ops->vidioc_s_ext_ctrls(file, fh, p) :
    2238                 :            :                                         -EINVAL;
    2239                 :            : }
    2240                 :            : 
    2241                 :          0 : static int v4l_try_ext_ctrls(const struct v4l2_ioctl_ops *ops,
    2242                 :            :                                 struct file *file, void *fh, void *arg)
    2243                 :            : {
    2244                 :          0 :         struct video_device *vfd = video_devdata(file);
    2245                 :            :         struct v4l2_ext_controls *p = arg;
    2246                 :            :         struct v4l2_fh *vfh =
    2247                 :          0 :                 test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
    2248                 :            : 
    2249                 :          0 :         p->error_idx = p->count;
    2250                 :          0 :         if (vfh && vfh->ctrl_handler)
    2251                 :          0 :                 return v4l2_try_ext_ctrls(vfh->ctrl_handler,
    2252                 :          0 :                                           vfd, vfd->v4l2_dev->mdev, p);
    2253                 :          0 :         if (vfd->ctrl_handler)
    2254                 :          0 :                 return v4l2_try_ext_ctrls(vfd->ctrl_handler,
    2255                 :          0 :                                           vfd, vfd->v4l2_dev->mdev, p);
    2256                 :          0 :         if (ops->vidioc_try_ext_ctrls == NULL)
    2257                 :            :                 return -ENOTTY;
    2258                 :          0 :         return check_ext_ctrls(p, 0) ? ops->vidioc_try_ext_ctrls(file, fh, p) :
    2259                 :            :                                         -EINVAL;
    2260                 :            : }
    2261                 :            : 
    2262                 :            : /*
    2263                 :            :  * The selection API specified originally that the _MPLANE buffer types
    2264                 :            :  * shouldn't be used. The reasons for this are lost in the mists of time
    2265                 :            :  * (or just really crappy memories). Regardless, this is really annoying
    2266                 :            :  * for userspace. So to keep things simple we map _MPLANE buffer types
    2267                 :            :  * to their 'regular' counterparts before calling the driver. And we
    2268                 :            :  * restore it afterwards. This way applications can use either buffer
    2269                 :            :  * type and drivers don't need to check for both.
    2270                 :            :  */
    2271                 :          0 : static int v4l_g_selection(const struct v4l2_ioctl_ops *ops,
    2272                 :            :                            struct file *file, void *fh, void *arg)
    2273                 :            : {
    2274                 :            :         struct v4l2_selection *p = arg;
    2275                 :          0 :         u32 old_type = p->type;
    2276                 :            :         int ret;
    2277                 :            : 
    2278                 :          0 :         if (p->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
    2279                 :          0 :                 p->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
    2280                 :          0 :         else if (p->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
    2281                 :          0 :                 p->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
    2282                 :          0 :         ret = ops->vidioc_g_selection(file, fh, p);
    2283                 :          0 :         p->type = old_type;
    2284                 :          0 :         return ret;
    2285                 :            : }
    2286                 :            : 
    2287                 :          0 : static int v4l_s_selection(const struct v4l2_ioctl_ops *ops,
    2288                 :            :                            struct file *file, void *fh, void *arg)
    2289                 :            : {
    2290                 :            :         struct v4l2_selection *p = arg;
    2291                 :          0 :         u32 old_type = p->type;
    2292                 :            :         int ret;
    2293                 :            : 
    2294                 :          0 :         if (p->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
    2295                 :          0 :                 p->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
    2296                 :          0 :         else if (p->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
    2297                 :          0 :                 p->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
    2298                 :          0 :         ret = ops->vidioc_s_selection(file, fh, p);
    2299                 :          0 :         p->type = old_type;
    2300                 :          0 :         return ret;
    2301                 :            : }
    2302                 :            : 
    2303                 :          0 : static int v4l_g_crop(const struct v4l2_ioctl_ops *ops,
    2304                 :            :                                 struct file *file, void *fh, void *arg)
    2305                 :            : {
    2306                 :          0 :         struct video_device *vfd = video_devdata(file);
    2307                 :            :         struct v4l2_crop *p = arg;
    2308                 :          0 :         struct v4l2_selection s = {
    2309                 :          0 :                 .type = p->type,
    2310                 :            :         };
    2311                 :            :         int ret;
    2312                 :            : 
    2313                 :            :         /* simulate capture crop using selection api */
    2314                 :            : 
    2315                 :            :         /* crop means compose for output devices */
    2316                 :          0 :         if (V4L2_TYPE_IS_OUTPUT(p->type))
    2317                 :          0 :                 s.target = V4L2_SEL_TGT_COMPOSE;
    2318                 :            :         else
    2319                 :            :                 s.target = V4L2_SEL_TGT_CROP;
    2320                 :            : 
    2321                 :          0 :         if (test_bit(V4L2_FL_QUIRK_INVERTED_CROP, &vfd->flags))
    2322                 :          0 :                 s.target = s.target == V4L2_SEL_TGT_COMPOSE ?
    2323                 :            :                         V4L2_SEL_TGT_CROP : V4L2_SEL_TGT_COMPOSE;
    2324                 :            : 
    2325                 :            :         ret = v4l_g_selection(ops, file, fh, &s);
    2326                 :            : 
    2327                 :            :         /* copying results to old structure on success */
    2328                 :          0 :         if (!ret)
    2329                 :          0 :                 p->c = s.r;
    2330                 :          0 :         return ret;
    2331                 :            : }
    2332                 :            : 
    2333                 :          0 : static int v4l_s_crop(const struct v4l2_ioctl_ops *ops,
    2334                 :            :                                 struct file *file, void *fh, void *arg)
    2335                 :            : {
    2336                 :          0 :         struct video_device *vfd = video_devdata(file);
    2337                 :            :         struct v4l2_crop *p = arg;
    2338                 :          0 :         struct v4l2_selection s = {
    2339                 :          0 :                 .type = p->type,
    2340                 :            :                 .r = p->c,
    2341                 :            :         };
    2342                 :            : 
    2343                 :            :         /* simulate capture crop using selection api */
    2344                 :            : 
    2345                 :            :         /* crop means compose for output devices */
    2346                 :          0 :         if (V4L2_TYPE_IS_OUTPUT(p->type))
    2347                 :          0 :                 s.target = V4L2_SEL_TGT_COMPOSE;
    2348                 :            :         else
    2349                 :            :                 s.target = V4L2_SEL_TGT_CROP;
    2350                 :            : 
    2351                 :          0 :         if (test_bit(V4L2_FL_QUIRK_INVERTED_CROP, &vfd->flags))
    2352                 :          0 :                 s.target = s.target == V4L2_SEL_TGT_COMPOSE ?
    2353                 :            :                         V4L2_SEL_TGT_CROP : V4L2_SEL_TGT_COMPOSE;
    2354                 :            : 
    2355                 :          0 :         return v4l_s_selection(ops, file, fh, &s);
    2356                 :            : }
    2357                 :            : 
    2358                 :          0 : static int v4l_cropcap(const struct v4l2_ioctl_ops *ops,
    2359                 :            :                                 struct file *file, void *fh, void *arg)
    2360                 :            : {
    2361                 :          0 :         struct video_device *vfd = video_devdata(file);
    2362                 :            :         struct v4l2_cropcap *p = arg;
    2363                 :          0 :         struct v4l2_selection s = { .type = p->type };
    2364                 :            :         int ret = 0;
    2365                 :            : 
    2366                 :            :         /* setting trivial pixelaspect */
    2367                 :          0 :         p->pixelaspect.numerator = 1;
    2368                 :          0 :         p->pixelaspect.denominator = 1;
    2369                 :            : 
    2370                 :          0 :         if (s.type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
    2371                 :          0 :                 s.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
    2372                 :          0 :         else if (s.type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
    2373                 :          0 :                 s.type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
    2374                 :            : 
    2375                 :            :         /*
    2376                 :            :          * The determine_valid_ioctls() call already should ensure
    2377                 :            :          * that this can never happen, but just in case...
    2378                 :            :          */
    2379                 :          0 :         if (WARN_ON(!ops->vidioc_g_selection))
    2380                 :            :                 return -ENOTTY;
    2381                 :            : 
    2382                 :          0 :         if (ops->vidioc_g_pixelaspect)
    2383                 :          0 :                 ret = ops->vidioc_g_pixelaspect(file, fh, s.type,
    2384                 :            :                                                 &p->pixelaspect);
    2385                 :            : 
    2386                 :            :         /*
    2387                 :            :          * Ignore ENOTTY or ENOIOCTLCMD error returns, just use the
    2388                 :            :          * square pixel aspect ratio in that case.
    2389                 :            :          */
    2390                 :          0 :         if (ret && ret != -ENOTTY && ret != -ENOIOCTLCMD)
    2391                 :            :                 return ret;
    2392                 :            : 
    2393                 :            :         /* Use g_selection() to fill in the bounds and defrect rectangles */
    2394                 :            : 
    2395                 :            :         /* obtaining bounds */
    2396                 :          0 :         if (V4L2_TYPE_IS_OUTPUT(p->type))
    2397                 :          0 :                 s.target = V4L2_SEL_TGT_COMPOSE_BOUNDS;
    2398                 :            :         else
    2399                 :          0 :                 s.target = V4L2_SEL_TGT_CROP_BOUNDS;
    2400                 :            : 
    2401                 :          0 :         if (test_bit(V4L2_FL_QUIRK_INVERTED_CROP, &vfd->flags))
    2402                 :          0 :                 s.target = s.target == V4L2_SEL_TGT_COMPOSE_BOUNDS ?
    2403                 :            :                         V4L2_SEL_TGT_CROP_BOUNDS : V4L2_SEL_TGT_COMPOSE_BOUNDS;
    2404                 :            : 
    2405                 :            :         ret = v4l_g_selection(ops, file, fh, &s);
    2406                 :          0 :         if (ret)
    2407                 :            :                 return ret;
    2408                 :          0 :         p->bounds = s.r;
    2409                 :            : 
    2410                 :            :         /* obtaining defrect */
    2411                 :          0 :         if (s.target == V4L2_SEL_TGT_COMPOSE_BOUNDS)
    2412                 :          0 :                 s.target = V4L2_SEL_TGT_COMPOSE_DEFAULT;
    2413                 :            :         else
    2414                 :          0 :                 s.target = V4L2_SEL_TGT_CROP_DEFAULT;
    2415                 :            : 
    2416                 :            :         ret = v4l_g_selection(ops, file, fh, &s);
    2417                 :          0 :         if (ret)
    2418                 :            :                 return ret;
    2419                 :          0 :         p->defrect = s.r;
    2420                 :            : 
    2421                 :          0 :         return 0;
    2422                 :            : }
    2423                 :            : 
    2424                 :          0 : static int v4l_log_status(const struct v4l2_ioctl_ops *ops,
    2425                 :            :                                 struct file *file, void *fh, void *arg)
    2426                 :            : {
    2427                 :          0 :         struct video_device *vfd = video_devdata(file);
    2428                 :            :         int ret;
    2429                 :            : 
    2430                 :          0 :         if (vfd->v4l2_dev)
    2431                 :          0 :                 pr_info("%s: =================  START STATUS  =================\n",
    2432                 :            :                         vfd->v4l2_dev->name);
    2433                 :          0 :         ret = ops->vidioc_log_status(file, fh);
    2434                 :          0 :         if (vfd->v4l2_dev)
    2435                 :          0 :                 pr_info("%s: ==================  END STATUS  ==================\n",
    2436                 :            :                         vfd->v4l2_dev->name);
    2437                 :          0 :         return ret;
    2438                 :            : }
    2439                 :            : 
    2440                 :          0 : static int v4l_dbg_g_register(const struct v4l2_ioctl_ops *ops,
    2441                 :            :                                 struct file *file, void *fh, void *arg)
    2442                 :            : {
    2443                 :            : #ifdef CONFIG_VIDEO_ADV_DEBUG
    2444                 :            :         struct v4l2_dbg_register *p = arg;
    2445                 :            :         struct video_device *vfd = video_devdata(file);
    2446                 :            :         struct v4l2_subdev *sd;
    2447                 :            :         int idx = 0;
    2448                 :            : 
    2449                 :            :         if (!capable(CAP_SYS_ADMIN))
    2450                 :            :                 return -EPERM;
    2451                 :            :         if (p->match.type == V4L2_CHIP_MATCH_SUBDEV) {
    2452                 :            :                 if (vfd->v4l2_dev == NULL)
    2453                 :            :                         return -EINVAL;
    2454                 :            :                 v4l2_device_for_each_subdev(sd, vfd->v4l2_dev)
    2455                 :            :                         if (p->match.addr == idx++)
    2456                 :            :                                 return v4l2_subdev_call(sd, core, g_register, p);
    2457                 :            :                 return -EINVAL;
    2458                 :            :         }
    2459                 :            :         if (ops->vidioc_g_register && p->match.type == V4L2_CHIP_MATCH_BRIDGE &&
    2460                 :            :             (ops->vidioc_g_chip_info || p->match.addr == 0))
    2461                 :            :                 return ops->vidioc_g_register(file, fh, p);
    2462                 :            :         return -EINVAL;
    2463                 :            : #else
    2464                 :          0 :         return -ENOTTY;
    2465                 :            : #endif
    2466                 :            : }
    2467                 :            : 
    2468                 :          0 : static int v4l_dbg_s_register(const struct v4l2_ioctl_ops *ops,
    2469                 :            :                                 struct file *file, void *fh, void *arg)
    2470                 :            : {
    2471                 :            : #ifdef CONFIG_VIDEO_ADV_DEBUG
    2472                 :            :         const struct v4l2_dbg_register *p = arg;
    2473                 :            :         struct video_device *vfd = video_devdata(file);
    2474                 :            :         struct v4l2_subdev *sd;
    2475                 :            :         int idx = 0;
    2476                 :            : 
    2477                 :            :         if (!capable(CAP_SYS_ADMIN))
    2478                 :            :                 return -EPERM;
    2479                 :            :         if (p->match.type == V4L2_CHIP_MATCH_SUBDEV) {
    2480                 :            :                 if (vfd->v4l2_dev == NULL)
    2481                 :            :                         return -EINVAL;
    2482                 :            :                 v4l2_device_for_each_subdev(sd, vfd->v4l2_dev)
    2483                 :            :                         if (p->match.addr == idx++)
    2484                 :            :                                 return v4l2_subdev_call(sd, core, s_register, p);
    2485                 :            :                 return -EINVAL;
    2486                 :            :         }
    2487                 :            :         if (ops->vidioc_s_register && p->match.type == V4L2_CHIP_MATCH_BRIDGE &&
    2488                 :            :             (ops->vidioc_g_chip_info || p->match.addr == 0))
    2489                 :            :                 return ops->vidioc_s_register(file, fh, p);
    2490                 :            :         return -EINVAL;
    2491                 :            : #else
    2492                 :          0 :         return -ENOTTY;
    2493                 :            : #endif
    2494                 :            : }
    2495                 :            : 
    2496                 :          0 : static int v4l_dbg_g_chip_info(const struct v4l2_ioctl_ops *ops,
    2497                 :            :                                 struct file *file, void *fh, void *arg)
    2498                 :            : {
    2499                 :            : #ifdef CONFIG_VIDEO_ADV_DEBUG
    2500                 :            :         struct video_device *vfd = video_devdata(file);
    2501                 :            :         struct v4l2_dbg_chip_info *p = arg;
    2502                 :            :         struct v4l2_subdev *sd;
    2503                 :            :         int idx = 0;
    2504                 :            : 
    2505                 :            :         switch (p->match.type) {
    2506                 :            :         case V4L2_CHIP_MATCH_BRIDGE:
    2507                 :            :                 if (ops->vidioc_s_register)
    2508                 :            :                         p->flags |= V4L2_CHIP_FL_WRITABLE;
    2509                 :            :                 if (ops->vidioc_g_register)
    2510                 :            :                         p->flags |= V4L2_CHIP_FL_READABLE;
    2511                 :            :                 strscpy(p->name, vfd->v4l2_dev->name, sizeof(p->name));
    2512                 :            :                 if (ops->vidioc_g_chip_info)
    2513                 :            :                         return ops->vidioc_g_chip_info(file, fh, arg);
    2514                 :            :                 if (p->match.addr)
    2515                 :            :                         return -EINVAL;
    2516                 :            :                 return 0;
    2517                 :            : 
    2518                 :            :         case V4L2_CHIP_MATCH_SUBDEV:
    2519                 :            :                 if (vfd->v4l2_dev == NULL)
    2520                 :            :                         break;
    2521                 :            :                 v4l2_device_for_each_subdev(sd, vfd->v4l2_dev) {
    2522                 :            :                         if (p->match.addr != idx++)
    2523                 :            :                                 continue;
    2524                 :            :                         if (sd->ops->core && sd->ops->core->s_register)
    2525                 :            :                                 p->flags |= V4L2_CHIP_FL_WRITABLE;
    2526                 :            :                         if (sd->ops->core && sd->ops->core->g_register)
    2527                 :            :                                 p->flags |= V4L2_CHIP_FL_READABLE;
    2528                 :            :                         strscpy(p->name, sd->name, sizeof(p->name));
    2529                 :            :                         return 0;
    2530                 :            :                 }
    2531                 :            :                 break;
    2532                 :            :         }
    2533                 :            :         return -EINVAL;
    2534                 :            : #else
    2535                 :          0 :         return -ENOTTY;
    2536                 :            : #endif
    2537                 :            : }
    2538                 :            : 
    2539                 :          0 : static int v4l_dqevent(const struct v4l2_ioctl_ops *ops,
    2540                 :            :                                 struct file *file, void *fh, void *arg)
    2541                 :            : {
    2542                 :          0 :         return v4l2_event_dequeue(fh, arg, file->f_flags & O_NONBLOCK);
    2543                 :            : }
    2544                 :            : 
    2545                 :          0 : static int v4l_subscribe_event(const struct v4l2_ioctl_ops *ops,
    2546                 :            :                                 struct file *file, void *fh, void *arg)
    2547                 :            : {
    2548                 :          0 :         return ops->vidioc_subscribe_event(fh, arg);
    2549                 :            : }
    2550                 :            : 
    2551                 :          0 : static int v4l_unsubscribe_event(const struct v4l2_ioctl_ops *ops,
    2552                 :            :                                 struct file *file, void *fh, void *arg)
    2553                 :            : {
    2554                 :          0 :         return ops->vidioc_unsubscribe_event(fh, arg);
    2555                 :            : }
    2556                 :            : 
    2557                 :          0 : static int v4l_g_sliced_vbi_cap(const struct v4l2_ioctl_ops *ops,
    2558                 :            :                                 struct file *file, void *fh, void *arg)
    2559                 :            : {
    2560                 :            :         struct v4l2_sliced_vbi_cap *p = arg;
    2561                 :          0 :         int ret = check_fmt(file, p->type);
    2562                 :            : 
    2563                 :          0 :         if (ret)
    2564                 :            :                 return ret;
    2565                 :            : 
    2566                 :            :         /* Clear up to type, everything after type is zeroed already */
    2567                 :          0 :         memset(p, 0, offsetof(struct v4l2_sliced_vbi_cap, type));
    2568                 :            : 
    2569                 :          0 :         return ops->vidioc_g_sliced_vbi_cap(file, fh, p);
    2570                 :            : }
    2571                 :            : 
    2572                 :          0 : static int v4l_enum_freq_bands(const struct v4l2_ioctl_ops *ops,
    2573                 :            :                                 struct file *file, void *fh, void *arg)
    2574                 :            : {
    2575                 :          0 :         struct video_device *vfd = video_devdata(file);
    2576                 :            :         struct v4l2_frequency_band *p = arg;
    2577                 :            :         enum v4l2_tuner_type type;
    2578                 :            :         int err;
    2579                 :            : 
    2580                 :          0 :         if (vfd->vfl_type == VFL_TYPE_SDR) {
    2581                 :          0 :                 if (p->type != V4L2_TUNER_SDR && p->type != V4L2_TUNER_RF)
    2582                 :            :                         return -EINVAL;
    2583                 :            :                 type = p->type;
    2584                 :            :         } else {
    2585                 :          0 :                 type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
    2586                 :            :                                 V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
    2587                 :          0 :                 if (type != p->type)
    2588                 :            :                         return -EINVAL;
    2589                 :            :         }
    2590                 :          0 :         if (ops->vidioc_enum_freq_bands) {
    2591                 :          0 :                 err = ops->vidioc_enum_freq_bands(file, fh, p);
    2592                 :          0 :                 if (err != -ENOTTY)
    2593                 :            :                         return err;
    2594                 :            :         }
    2595                 :          0 :         if (is_valid_ioctl(vfd, VIDIOC_G_TUNER)) {
    2596                 :          0 :                 struct v4l2_tuner t = {
    2597                 :          0 :                         .index = p->tuner,
    2598                 :            :                         .type = type,
    2599                 :            :                 };
    2600                 :            : 
    2601                 :          0 :                 if (p->index)
    2602                 :            :                         return -EINVAL;
    2603                 :          0 :                 err = ops->vidioc_g_tuner(file, fh, &t);
    2604                 :          0 :                 if (err)
    2605                 :            :                         return err;
    2606                 :          0 :                 p->capability = t.capability | V4L2_TUNER_CAP_FREQ_BANDS;
    2607                 :          0 :                 p->rangelow = t.rangelow;
    2608                 :          0 :                 p->rangehigh = t.rangehigh;
    2609                 :          0 :                 p->modulation = (type == V4L2_TUNER_RADIO) ?
    2610                 :            :                         V4L2_BAND_MODULATION_FM : V4L2_BAND_MODULATION_VSB;
    2611                 :          0 :                 return 0;
    2612                 :            :         }
    2613                 :          0 :         if (is_valid_ioctl(vfd, VIDIOC_G_MODULATOR)) {
    2614                 :          0 :                 struct v4l2_modulator m = {
    2615                 :          0 :                         .index = p->tuner,
    2616                 :            :                 };
    2617                 :            : 
    2618                 :          0 :                 if (type != V4L2_TUNER_RADIO)
    2619                 :            :                         return -EINVAL;
    2620                 :          0 :                 if (p->index)
    2621                 :            :                         return -EINVAL;
    2622                 :          0 :                 err = ops->vidioc_g_modulator(file, fh, &m);
    2623                 :          0 :                 if (err)
    2624                 :            :                         return err;
    2625                 :          0 :                 p->capability = m.capability | V4L2_TUNER_CAP_FREQ_BANDS;
    2626                 :          0 :                 p->rangelow = m.rangelow;
    2627                 :          0 :                 p->rangehigh = m.rangehigh;
    2628                 :          0 :                 p->modulation = (type == V4L2_TUNER_RADIO) ?
    2629                 :            :                         V4L2_BAND_MODULATION_FM : V4L2_BAND_MODULATION_VSB;
    2630                 :          0 :                 return 0;
    2631                 :            :         }
    2632                 :            :         return -ENOTTY;
    2633                 :            : }
    2634                 :            : 
    2635                 :            : struct v4l2_ioctl_info {
    2636                 :            :         unsigned int ioctl;
    2637                 :            :         u32 flags;
    2638                 :            :         const char * const name;
    2639                 :            :         int (*func)(const struct v4l2_ioctl_ops *ops, struct file *file,
    2640                 :            :                     void *fh, void *p);
    2641                 :            :         void (*debug)(const void *arg, bool write_only);
    2642                 :            : };
    2643                 :            : 
    2644                 :            : /* This control needs a priority check */
    2645                 :            : #define INFO_FL_PRIO            (1 << 0)
    2646                 :            : /* This control can be valid if the filehandle passes a control handler. */
    2647                 :            : #define INFO_FL_CTRL            (1 << 1)
    2648                 :            : /* Queuing ioctl */
    2649                 :            : #define INFO_FL_QUEUE           (1 << 2)
    2650                 :            : /* Always copy back result, even on error */
    2651                 :            : #define INFO_FL_ALWAYS_COPY     (1 << 3)
    2652                 :            : /* Zero struct from after the field to the end */
    2653                 :            : #define INFO_FL_CLEAR(v4l2_struct, field)                       \
    2654                 :            :         ((offsetof(struct v4l2_struct, field) +                 \
    2655                 :            :           sizeof(((struct v4l2_struct *)0)->field)) << 16)
    2656                 :            : #define INFO_FL_CLEAR_MASK      (_IOC_SIZEMASK << 16)
    2657                 :            : 
    2658                 :            : #define DEFINE_V4L_STUB_FUNC(_vidioc)                           \
    2659                 :            :         static int v4l_stub_ ## _vidioc(                        \
    2660                 :            :                         const struct v4l2_ioctl_ops *ops,       \
    2661                 :            :                         struct file *file, void *fh, void *p)   \
    2662                 :            :         {                                                       \
    2663                 :            :                 return ops->vidioc_ ## _vidioc(file, fh, p); \
    2664                 :            :         }
    2665                 :            : 
    2666                 :            : #define IOCTL_INFO(_ioctl, _func, _debug, _flags)               \
    2667                 :            :         [_IOC_NR(_ioctl)] = {                                   \
    2668                 :            :                 .ioctl = _ioctl,                                \
    2669                 :            :                 .flags = _flags,                                \
    2670                 :            :                 .name = #_ioctl,                                \
    2671                 :            :                 .func = _func,                                  \
    2672                 :            :                 .debug = _debug,                                \
    2673                 :            :         }
    2674                 :            : 
    2675                 :          0 : DEFINE_V4L_STUB_FUNC(g_fbuf)
    2676                 :          0 : DEFINE_V4L_STUB_FUNC(s_fbuf)
    2677                 :          0 : DEFINE_V4L_STUB_FUNC(expbuf)
    2678                 :          0 : DEFINE_V4L_STUB_FUNC(g_std)
    2679                 :          0 : DEFINE_V4L_STUB_FUNC(g_audio)
    2680                 :          0 : DEFINE_V4L_STUB_FUNC(s_audio)
    2681                 :          0 : DEFINE_V4L_STUB_FUNC(g_input)
    2682                 :          0 : DEFINE_V4L_STUB_FUNC(g_edid)
    2683                 :          0 : DEFINE_V4L_STUB_FUNC(s_edid)
    2684                 :          0 : DEFINE_V4L_STUB_FUNC(g_output)
    2685                 :          0 : DEFINE_V4L_STUB_FUNC(g_audout)
    2686                 :          0 : DEFINE_V4L_STUB_FUNC(s_audout)
    2687                 :          0 : DEFINE_V4L_STUB_FUNC(g_jpegcomp)
    2688                 :          0 : DEFINE_V4L_STUB_FUNC(s_jpegcomp)
    2689                 :          0 : DEFINE_V4L_STUB_FUNC(enumaudio)
    2690                 :          0 : DEFINE_V4L_STUB_FUNC(enumaudout)
    2691                 :          0 : DEFINE_V4L_STUB_FUNC(enum_framesizes)
    2692                 :          0 : DEFINE_V4L_STUB_FUNC(enum_frameintervals)
    2693                 :          0 : DEFINE_V4L_STUB_FUNC(g_enc_index)
    2694                 :          0 : DEFINE_V4L_STUB_FUNC(encoder_cmd)
    2695                 :          0 : DEFINE_V4L_STUB_FUNC(try_encoder_cmd)
    2696                 :          0 : DEFINE_V4L_STUB_FUNC(decoder_cmd)
    2697                 :          0 : DEFINE_V4L_STUB_FUNC(try_decoder_cmd)
    2698                 :          0 : DEFINE_V4L_STUB_FUNC(s_dv_timings)
    2699                 :          0 : DEFINE_V4L_STUB_FUNC(g_dv_timings)
    2700                 :          0 : DEFINE_V4L_STUB_FUNC(enum_dv_timings)
    2701                 :          0 : DEFINE_V4L_STUB_FUNC(query_dv_timings)
    2702                 :          0 : DEFINE_V4L_STUB_FUNC(dv_timings_cap)
    2703                 :            : 
    2704                 :            : static const struct v4l2_ioctl_info v4l2_ioctls[] = {
    2705                 :            :         IOCTL_INFO(VIDIOC_QUERYCAP, v4l_querycap, v4l_print_querycap, 0),
    2706                 :            :         IOCTL_INFO(VIDIOC_ENUM_FMT, v4l_enum_fmt, v4l_print_fmtdesc, INFO_FL_CLEAR(v4l2_fmtdesc, type)),
    2707                 :            :         IOCTL_INFO(VIDIOC_G_FMT, v4l_g_fmt, v4l_print_format, 0),
    2708                 :            :         IOCTL_INFO(VIDIOC_S_FMT, v4l_s_fmt, v4l_print_format, INFO_FL_PRIO),
    2709                 :            :         IOCTL_INFO(VIDIOC_REQBUFS, v4l_reqbufs, v4l_print_requestbuffers, INFO_FL_PRIO | INFO_FL_QUEUE),
    2710                 :            :         IOCTL_INFO(VIDIOC_QUERYBUF, v4l_querybuf, v4l_print_buffer, INFO_FL_QUEUE | INFO_FL_CLEAR(v4l2_buffer, length)),
    2711                 :            :         IOCTL_INFO(VIDIOC_G_FBUF, v4l_stub_g_fbuf, v4l_print_framebuffer, 0),
    2712                 :            :         IOCTL_INFO(VIDIOC_S_FBUF, v4l_stub_s_fbuf, v4l_print_framebuffer, INFO_FL_PRIO),
    2713                 :            :         IOCTL_INFO(VIDIOC_OVERLAY, v4l_overlay, v4l_print_u32, INFO_FL_PRIO),
    2714                 :            :         IOCTL_INFO(VIDIOC_QBUF, v4l_qbuf, v4l_print_buffer, INFO_FL_QUEUE),
    2715                 :            :         IOCTL_INFO(VIDIOC_EXPBUF, v4l_stub_expbuf, v4l_print_exportbuffer, INFO_FL_QUEUE | INFO_FL_CLEAR(v4l2_exportbuffer, flags)),
    2716                 :            :         IOCTL_INFO(VIDIOC_DQBUF, v4l_dqbuf, v4l_print_buffer, INFO_FL_QUEUE),
    2717                 :            :         IOCTL_INFO(VIDIOC_STREAMON, v4l_streamon, v4l_print_buftype, INFO_FL_PRIO | INFO_FL_QUEUE),
    2718                 :            :         IOCTL_INFO(VIDIOC_STREAMOFF, v4l_streamoff, v4l_print_buftype, INFO_FL_PRIO | INFO_FL_QUEUE),
    2719                 :            :         IOCTL_INFO(VIDIOC_G_PARM, v4l_g_parm, v4l_print_streamparm, INFO_FL_CLEAR(v4l2_streamparm, type)),
    2720                 :            :         IOCTL_INFO(VIDIOC_S_PARM, v4l_s_parm, v4l_print_streamparm, INFO_FL_PRIO),
    2721                 :            :         IOCTL_INFO(VIDIOC_G_STD, v4l_stub_g_std, v4l_print_std, 0),
    2722                 :            :         IOCTL_INFO(VIDIOC_S_STD, v4l_s_std, v4l_print_std, INFO_FL_PRIO),
    2723                 :            :         IOCTL_INFO(VIDIOC_ENUMSTD, v4l_enumstd, v4l_print_standard, INFO_FL_CLEAR(v4l2_standard, index)),
    2724                 :            :         IOCTL_INFO(VIDIOC_ENUMINPUT, v4l_enuminput, v4l_print_enuminput, INFO_FL_CLEAR(v4l2_input, index)),
    2725                 :            :         IOCTL_INFO(VIDIOC_G_CTRL, v4l_g_ctrl, v4l_print_control, INFO_FL_CTRL | INFO_FL_CLEAR(v4l2_control, id)),
    2726                 :            :         IOCTL_INFO(VIDIOC_S_CTRL, v4l_s_ctrl, v4l_print_control, INFO_FL_PRIO | INFO_FL_CTRL),
    2727                 :            :         IOCTL_INFO(VIDIOC_G_TUNER, v4l_g_tuner, v4l_print_tuner, INFO_FL_CLEAR(v4l2_tuner, index)),
    2728                 :            :         IOCTL_INFO(VIDIOC_S_TUNER, v4l_s_tuner, v4l_print_tuner, INFO_FL_PRIO),
    2729                 :            :         IOCTL_INFO(VIDIOC_G_AUDIO, v4l_stub_g_audio, v4l_print_audio, 0),
    2730                 :            :         IOCTL_INFO(VIDIOC_S_AUDIO, v4l_stub_s_audio, v4l_print_audio, INFO_FL_PRIO),
    2731                 :            :         IOCTL_INFO(VIDIOC_QUERYCTRL, v4l_queryctrl, v4l_print_queryctrl, INFO_FL_CTRL | INFO_FL_CLEAR(v4l2_queryctrl, id)),
    2732                 :            :         IOCTL_INFO(VIDIOC_QUERYMENU, v4l_querymenu, v4l_print_querymenu, INFO_FL_CTRL | INFO_FL_CLEAR(v4l2_querymenu, index)),
    2733                 :            :         IOCTL_INFO(VIDIOC_G_INPUT, v4l_stub_g_input, v4l_print_u32, 0),
    2734                 :            :         IOCTL_INFO(VIDIOC_S_INPUT, v4l_s_input, v4l_print_u32, INFO_FL_PRIO),
    2735                 :            :         IOCTL_INFO(VIDIOC_G_EDID, v4l_stub_g_edid, v4l_print_edid, INFO_FL_ALWAYS_COPY),
    2736                 :            :         IOCTL_INFO(VIDIOC_S_EDID, v4l_stub_s_edid, v4l_print_edid, INFO_FL_PRIO | INFO_FL_ALWAYS_COPY),
    2737                 :            :         IOCTL_INFO(VIDIOC_G_OUTPUT, v4l_stub_g_output, v4l_print_u32, 0),
    2738                 :            :         IOCTL_INFO(VIDIOC_S_OUTPUT, v4l_s_output, v4l_print_u32, INFO_FL_PRIO),
    2739                 :            :         IOCTL_INFO(VIDIOC_ENUMOUTPUT, v4l_enumoutput, v4l_print_enumoutput, INFO_FL_CLEAR(v4l2_output, index)),
    2740                 :            :         IOCTL_INFO(VIDIOC_G_AUDOUT, v4l_stub_g_audout, v4l_print_audioout, 0),
    2741                 :            :         IOCTL_INFO(VIDIOC_S_AUDOUT, v4l_stub_s_audout, v4l_print_audioout, INFO_FL_PRIO),
    2742                 :            :         IOCTL_INFO(VIDIOC_G_MODULATOR, v4l_g_modulator, v4l_print_modulator, INFO_FL_CLEAR(v4l2_modulator, index)),
    2743                 :            :         IOCTL_INFO(VIDIOC_S_MODULATOR, v4l_s_modulator, v4l_print_modulator, INFO_FL_PRIO),
    2744                 :            :         IOCTL_INFO(VIDIOC_G_FREQUENCY, v4l_g_frequency, v4l_print_frequency, INFO_FL_CLEAR(v4l2_frequency, tuner)),
    2745                 :            :         IOCTL_INFO(VIDIOC_S_FREQUENCY, v4l_s_frequency, v4l_print_frequency, INFO_FL_PRIO),
    2746                 :            :         IOCTL_INFO(VIDIOC_CROPCAP, v4l_cropcap, v4l_print_cropcap, INFO_FL_CLEAR(v4l2_cropcap, type)),
    2747                 :            :         IOCTL_INFO(VIDIOC_G_CROP, v4l_g_crop, v4l_print_crop, INFO_FL_CLEAR(v4l2_crop, type)),
    2748                 :            :         IOCTL_INFO(VIDIOC_S_CROP, v4l_s_crop, v4l_print_crop, INFO_FL_PRIO),
    2749                 :            :         IOCTL_INFO(VIDIOC_G_SELECTION, v4l_g_selection, v4l_print_selection, INFO_FL_CLEAR(v4l2_selection, r)),
    2750                 :            :         IOCTL_INFO(VIDIOC_S_SELECTION, v4l_s_selection, v4l_print_selection, INFO_FL_PRIO | INFO_FL_CLEAR(v4l2_selection, r)),
    2751                 :            :         IOCTL_INFO(VIDIOC_G_JPEGCOMP, v4l_stub_g_jpegcomp, v4l_print_jpegcompression, 0),
    2752                 :            :         IOCTL_INFO(VIDIOC_S_JPEGCOMP, v4l_stub_s_jpegcomp, v4l_print_jpegcompression, INFO_FL_PRIO),
    2753                 :            :         IOCTL_INFO(VIDIOC_QUERYSTD, v4l_querystd, v4l_print_std, 0),
    2754                 :            :         IOCTL_INFO(VIDIOC_TRY_FMT, v4l_try_fmt, v4l_print_format, 0),
    2755                 :            :         IOCTL_INFO(VIDIOC_ENUMAUDIO, v4l_stub_enumaudio, v4l_print_audio, INFO_FL_CLEAR(v4l2_audio, index)),
    2756                 :            :         IOCTL_INFO(VIDIOC_ENUMAUDOUT, v4l_stub_enumaudout, v4l_print_audioout, INFO_FL_CLEAR(v4l2_audioout, index)),
    2757                 :            :         IOCTL_INFO(VIDIOC_G_PRIORITY, v4l_g_priority, v4l_print_u32, 0),
    2758                 :            :         IOCTL_INFO(VIDIOC_S_PRIORITY, v4l_s_priority, v4l_print_u32, INFO_FL_PRIO),
    2759                 :            :         IOCTL_INFO(VIDIOC_G_SLICED_VBI_CAP, v4l_g_sliced_vbi_cap, v4l_print_sliced_vbi_cap, INFO_FL_CLEAR(v4l2_sliced_vbi_cap, type)),
    2760                 :            :         IOCTL_INFO(VIDIOC_LOG_STATUS, v4l_log_status, v4l_print_newline, 0),
    2761                 :            :         IOCTL_INFO(VIDIOC_G_EXT_CTRLS, v4l_g_ext_ctrls, v4l_print_ext_controls, INFO_FL_CTRL),
    2762                 :            :         IOCTL_INFO(VIDIOC_S_EXT_CTRLS, v4l_s_ext_ctrls, v4l_print_ext_controls, INFO_FL_PRIO | INFO_FL_CTRL),
    2763                 :            :         IOCTL_INFO(VIDIOC_TRY_EXT_CTRLS, v4l_try_ext_ctrls, v4l_print_ext_controls, INFO_FL_CTRL),
    2764                 :            :         IOCTL_INFO(VIDIOC_ENUM_FRAMESIZES, v4l_stub_enum_framesizes, v4l_print_frmsizeenum, INFO_FL_CLEAR(v4l2_frmsizeenum, pixel_format)),
    2765                 :            :         IOCTL_INFO(VIDIOC_ENUM_FRAMEINTERVALS, v4l_stub_enum_frameintervals, v4l_print_frmivalenum, INFO_FL_CLEAR(v4l2_frmivalenum, height)),
    2766                 :            :         IOCTL_INFO(VIDIOC_G_ENC_INDEX, v4l_stub_g_enc_index, v4l_print_enc_idx, 0),
    2767                 :            :         IOCTL_INFO(VIDIOC_ENCODER_CMD, v4l_stub_encoder_cmd, v4l_print_encoder_cmd, INFO_FL_PRIO | INFO_FL_CLEAR(v4l2_encoder_cmd, flags)),
    2768                 :            :         IOCTL_INFO(VIDIOC_TRY_ENCODER_CMD, v4l_stub_try_encoder_cmd, v4l_print_encoder_cmd, INFO_FL_CLEAR(v4l2_encoder_cmd, flags)),
    2769                 :            :         IOCTL_INFO(VIDIOC_DECODER_CMD, v4l_stub_decoder_cmd, v4l_print_decoder_cmd, INFO_FL_PRIO),
    2770                 :            :         IOCTL_INFO(VIDIOC_TRY_DECODER_CMD, v4l_stub_try_decoder_cmd, v4l_print_decoder_cmd, 0),
    2771                 :            :         IOCTL_INFO(VIDIOC_DBG_S_REGISTER, v4l_dbg_s_register, v4l_print_dbg_register, 0),
    2772                 :            :         IOCTL_INFO(VIDIOC_DBG_G_REGISTER, v4l_dbg_g_register, v4l_print_dbg_register, 0),
    2773                 :            :         IOCTL_INFO(VIDIOC_S_HW_FREQ_SEEK, v4l_s_hw_freq_seek, v4l_print_hw_freq_seek, INFO_FL_PRIO),
    2774                 :            :         IOCTL_INFO(VIDIOC_S_DV_TIMINGS, v4l_stub_s_dv_timings, v4l_print_dv_timings, INFO_FL_PRIO | INFO_FL_CLEAR(v4l2_dv_timings, bt.flags)),
    2775                 :            :         IOCTL_INFO(VIDIOC_G_DV_TIMINGS, v4l_stub_g_dv_timings, v4l_print_dv_timings, 0),
    2776                 :            :         IOCTL_INFO(VIDIOC_DQEVENT, v4l_dqevent, v4l_print_event, 0),
    2777                 :            :         IOCTL_INFO(VIDIOC_SUBSCRIBE_EVENT, v4l_subscribe_event, v4l_print_event_subscription, 0),
    2778                 :            :         IOCTL_INFO(VIDIOC_UNSUBSCRIBE_EVENT, v4l_unsubscribe_event, v4l_print_event_subscription, 0),
    2779                 :            :         IOCTL_INFO(VIDIOC_CREATE_BUFS, v4l_create_bufs, v4l_print_create_buffers, INFO_FL_PRIO | INFO_FL_QUEUE),
    2780                 :            :         IOCTL_INFO(VIDIOC_PREPARE_BUF, v4l_prepare_buf, v4l_print_buffer, INFO_FL_QUEUE),
    2781                 :            :         IOCTL_INFO(VIDIOC_ENUM_DV_TIMINGS, v4l_stub_enum_dv_timings, v4l_print_enum_dv_timings, INFO_FL_CLEAR(v4l2_enum_dv_timings, pad)),
    2782                 :            :         IOCTL_INFO(VIDIOC_QUERY_DV_TIMINGS, v4l_stub_query_dv_timings, v4l_print_dv_timings, INFO_FL_ALWAYS_COPY),
    2783                 :            :         IOCTL_INFO(VIDIOC_DV_TIMINGS_CAP, v4l_stub_dv_timings_cap, v4l_print_dv_timings_cap, INFO_FL_CLEAR(v4l2_dv_timings_cap, pad)),
    2784                 :            :         IOCTL_INFO(VIDIOC_ENUM_FREQ_BANDS, v4l_enum_freq_bands, v4l_print_freq_band, 0),
    2785                 :            :         IOCTL_INFO(VIDIOC_DBG_G_CHIP_INFO, v4l_dbg_g_chip_info, v4l_print_dbg_chip_info, INFO_FL_CLEAR(v4l2_dbg_chip_info, match)),
    2786                 :            :         IOCTL_INFO(VIDIOC_QUERY_EXT_CTRL, v4l_query_ext_ctrl, v4l_print_query_ext_ctrl, INFO_FL_CTRL | INFO_FL_CLEAR(v4l2_query_ext_ctrl, id)),
    2787                 :            : };
    2788                 :            : #define V4L2_IOCTLS ARRAY_SIZE(v4l2_ioctls)
    2789                 :            : 
    2790                 :            : static bool v4l2_is_known_ioctl(unsigned int cmd)
    2791                 :            : {
    2792                 :          3 :         if (_IOC_NR(cmd) >= V4L2_IOCTLS)
    2793                 :            :                 return false;
    2794                 :          3 :         return v4l2_ioctls[_IOC_NR(cmd)].ioctl == cmd;
    2795                 :            : }
    2796                 :            : 
    2797                 :          3 : static struct mutex *v4l2_ioctl_get_lock(struct video_device *vdev,
    2798                 :            :                                          struct v4l2_fh *vfh, unsigned int cmd,
    2799                 :            :                                          void *arg)
    2800                 :            : {
    2801                 :          3 :         if (_IOC_NR(cmd) >= V4L2_IOCTLS)
    2802                 :          0 :                 return vdev->lock;
    2803                 :            : #if IS_ENABLED(CONFIG_V4L2_MEM2MEM_DEV)
    2804                 :          3 :         if (vfh && vfh->m2m_ctx &&
    2805                 :          3 :             (v4l2_ioctls[_IOC_NR(cmd)].flags & INFO_FL_QUEUE)) {
    2806                 :          0 :                 if (vfh->m2m_ctx->q_lock)
    2807                 :            :                         return vfh->m2m_ctx->q_lock;
    2808                 :            :         }
    2809                 :            : #endif
    2810                 :          3 :         if (vdev->queue && vdev->queue->lock &&
    2811                 :          3 :                         (v4l2_ioctls[_IOC_NR(cmd)].flags & INFO_FL_QUEUE))
    2812                 :            :                 return vdev->queue->lock;
    2813                 :          3 :         return vdev->lock;
    2814                 :            : }
    2815                 :            : 
    2816                 :            : /* Common ioctl debug function. This function can be used by
    2817                 :            :    external ioctl messages as well as internal V4L ioctl */
    2818                 :          0 : void v4l_printk_ioctl(const char *prefix, unsigned int cmd)
    2819                 :            : {
    2820                 :            :         const char *dir, *type;
    2821                 :            : 
    2822                 :          0 :         if (prefix)
    2823                 :          0 :                 printk(KERN_DEBUG "%s: ", prefix);
    2824                 :            : 
    2825                 :          0 :         switch (_IOC_TYPE(cmd)) {
    2826                 :            :         case 'd':
    2827                 :            :                 type = "v4l2_int";
    2828                 :            :                 break;
    2829                 :            :         case 'V':
    2830                 :          0 :                 if (_IOC_NR(cmd) >= V4L2_IOCTLS) {
    2831                 :            :                         type = "v4l2";
    2832                 :            :                         break;
    2833                 :            :                 }
    2834                 :          0 :                 pr_cont("%s", v4l2_ioctls[_IOC_NR(cmd)].name);
    2835                 :          0 :                 return;
    2836                 :            :         default:
    2837                 :            :                 type = "unknown";
    2838                 :          0 :                 break;
    2839                 :            :         }
    2840                 :            : 
    2841                 :          0 :         switch (_IOC_DIR(cmd)) {
    2842                 :            :         case _IOC_NONE:              dir = "--"; break;
    2843                 :            :         case _IOC_READ:              dir = "r-"; break;
    2844                 :            :         case _IOC_WRITE:             dir = "-w"; break;
    2845                 :            :         case _IOC_READ | _IOC_WRITE: dir = "rw"; break;
    2846                 :            :         default:                     dir = "*ERR*"; break;
    2847                 :            :         }
    2848                 :          0 :         pr_cont("%s ioctl '%c', dir=%s, #%d (0x%08x)",
    2849                 :            :                 type, _IOC_TYPE(cmd), dir, _IOC_NR(cmd), cmd);
    2850                 :            : }
    2851                 :            : EXPORT_SYMBOL(v4l_printk_ioctl);
    2852                 :            : 
    2853                 :          3 : static long __video_do_ioctl(struct file *file,
    2854                 :            :                 unsigned int cmd, void *arg)
    2855                 :            : {
    2856                 :          3 :         struct video_device *vfd = video_devdata(file);
    2857                 :            :         struct mutex *req_queue_lock = NULL;
    2858                 :            :         struct mutex *lock; /* ioctl serialization mutex */
    2859                 :          3 :         const struct v4l2_ioctl_ops *ops = vfd->ioctl_ops;
    2860                 :            :         bool write_only = false;
    2861                 :            :         struct v4l2_ioctl_info default_info;
    2862                 :            :         const struct v4l2_ioctl_info *info;
    2863                 :          3 :         void *fh = file->private_data;
    2864                 :            :         struct v4l2_fh *vfh = NULL;
    2865                 :          3 :         int dev_debug = vfd->dev_debug;
    2866                 :            :         long ret = -ENOTTY;
    2867                 :            : 
    2868                 :          3 :         if (ops == NULL) {
    2869                 :          0 :                 pr_warn("%s: has no ioctl_ops.\n",
    2870                 :            :                                 video_device_node_name(vfd));
    2871                 :          0 :                 return ret;
    2872                 :            :         }
    2873                 :            : 
    2874                 :          3 :         if (test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags))
    2875                 :            :                 vfh = file->private_data;
    2876                 :            : 
    2877                 :            :         /*
    2878                 :            :          * We need to serialize streamon/off with queueing new requests.
    2879                 :            :          * These ioctls may trigger the cancellation of a streaming
    2880                 :            :          * operation, and that should not be mixed with queueing a new
    2881                 :            :          * request at the same time.
    2882                 :            :          */
    2883                 :          3 :         if (v4l2_device_supports_requests(vfd->v4l2_dev) &&
    2884                 :          0 :             (cmd == VIDIOC_STREAMON || cmd == VIDIOC_STREAMOFF)) {
    2885                 :          0 :                 req_queue_lock = &vfd->v4l2_dev->mdev->req_queue_mutex;
    2886                 :            : 
    2887                 :          0 :                 if (mutex_lock_interruptible(req_queue_lock))
    2888                 :            :                         return -ERESTARTSYS;
    2889                 :            :         }
    2890                 :            : 
    2891                 :          3 :         lock = v4l2_ioctl_get_lock(vfd, vfh, cmd, arg);
    2892                 :            : 
    2893                 :          3 :         if (lock && mutex_lock_interruptible(lock)) {
    2894                 :          0 :                 if (req_queue_lock)
    2895                 :          0 :                         mutex_unlock(req_queue_lock);
    2896                 :            :                 return -ERESTARTSYS;
    2897                 :            :         }
    2898                 :            : 
    2899                 :          3 :         if (!video_is_registered(vfd)) {
    2900                 :            :                 ret = -ENODEV;
    2901                 :            :                 goto unlock;
    2902                 :            :         }
    2903                 :            : 
    2904                 :          3 :         if (v4l2_is_known_ioctl(cmd)) {
    2905                 :          3 :                 info = &v4l2_ioctls[_IOC_NR(cmd)];
    2906                 :            : 
    2907                 :          3 :                 if (!test_bit(_IOC_NR(cmd), vfd->valid_ioctls) &&
    2908                 :          0 :                     !((info->flags & INFO_FL_CTRL) && vfh && vfh->ctrl_handler))
    2909                 :            :                         goto done;
    2910                 :            : 
    2911                 :          3 :                 if (vfh && (info->flags & INFO_FL_PRIO)) {
    2912                 :          0 :                         ret = v4l2_prio_check(vfd->prio, vfh->prio);
    2913                 :          0 :                         if (ret)
    2914                 :            :                                 goto done;
    2915                 :            :                 }
    2916                 :            :         } else {
    2917                 :          0 :                 default_info.ioctl = cmd;
    2918                 :          0 :                 default_info.flags = 0;
    2919                 :          0 :                 default_info.debug = v4l_print_default;
    2920                 :            :                 info = &default_info;
    2921                 :            :         }
    2922                 :            : 
    2923                 :          3 :         write_only = _IOC_DIR(cmd) == _IOC_WRITE;
    2924                 :          3 :         if (info != &default_info) {
    2925                 :          3 :                 ret = info->func(ops, file, fh, arg);
    2926                 :          0 :         } else if (!ops->vidioc_default) {
    2927                 :            :                 ret = -ENOTTY;
    2928                 :            :         } else {
    2929                 :          0 :                 ret = ops->vidioc_default(file, fh,
    2930                 :          0 :                         vfh ? v4l2_prio_check(vfd->prio, vfh->prio) >= 0 : 0,
    2931                 :            :                         cmd, arg);
    2932                 :            :         }
    2933                 :            : 
    2934                 :            : done:
    2935                 :          3 :         if (dev_debug & (V4L2_DEV_DEBUG_IOCTL | V4L2_DEV_DEBUG_IOCTL_ARG)) {
    2936                 :          0 :                 if (!(dev_debug & V4L2_DEV_DEBUG_STREAMING) &&
    2937                 :          0 :                     (cmd == VIDIOC_QBUF || cmd == VIDIOC_DQBUF))
    2938                 :            :                         goto unlock;
    2939                 :            : 
    2940                 :          0 :                 v4l_printk_ioctl(video_device_node_name(vfd), cmd);
    2941                 :          0 :                 if (ret < 0)
    2942                 :          0 :                         pr_cont(": error %ld", ret);
    2943                 :          0 :                 if (!(dev_debug & V4L2_DEV_DEBUG_IOCTL_ARG))
    2944                 :          0 :                         pr_cont("\n");
    2945                 :          0 :                 else if (_IOC_DIR(cmd) == _IOC_NONE)
    2946                 :          0 :                         info->debug(arg, write_only);
    2947                 :            :                 else {
    2948                 :          0 :                         pr_cont(": ");
    2949                 :          0 :                         info->debug(arg, write_only);
    2950                 :            :                 }
    2951                 :            :         }
    2952                 :            : 
    2953                 :            : unlock:
    2954                 :          3 :         if (lock)
    2955                 :          3 :                 mutex_unlock(lock);
    2956                 :          3 :         if (req_queue_lock)
    2957                 :          0 :                 mutex_unlock(req_queue_lock);
    2958                 :          3 :         return ret;
    2959                 :            : }
    2960                 :            : 
    2961                 :          3 : static int check_array_args(unsigned int cmd, void *parg, size_t *array_size,
    2962                 :            :                             void __user **user_ptr, void ***kernel_ptr)
    2963                 :            : {
    2964                 :            :         int ret = 0;
    2965                 :            : 
    2966                 :          3 :         switch (cmd) {
    2967                 :            :         case VIDIOC_PREPARE_BUF:
    2968                 :            :         case VIDIOC_QUERYBUF:
    2969                 :            :         case VIDIOC_QBUF:
    2970                 :            :         case VIDIOC_DQBUF: {
    2971                 :            :                 struct v4l2_buffer *buf = parg;
    2972                 :            : 
    2973                 :          0 :                 if (V4L2_TYPE_IS_MULTIPLANAR(buf->type) && buf->length > 0) {
    2974                 :          0 :                         if (buf->length > VIDEO_MAX_PLANES) {
    2975                 :            :                                 ret = -EINVAL;
    2976                 :            :                                 break;
    2977                 :            :                         }
    2978                 :          0 :                         *user_ptr = (void __user *)buf->m.planes;
    2979                 :          0 :                         *kernel_ptr = (void **)&buf->m.planes;
    2980                 :          0 :                         *array_size = sizeof(struct v4l2_plane) * buf->length;
    2981                 :            :                         ret = 1;
    2982                 :            :                 }
    2983                 :            :                 break;
    2984                 :            :         }
    2985                 :            : 
    2986                 :            :         case VIDIOC_G_EDID:
    2987                 :            :         case VIDIOC_S_EDID: {
    2988                 :            :                 struct v4l2_edid *edid = parg;
    2989                 :            : 
    2990                 :          0 :                 if (edid->blocks) {
    2991                 :          0 :                         if (edid->blocks > 256) {
    2992                 :            :                                 ret = -EINVAL;
    2993                 :            :                                 break;
    2994                 :            :                         }
    2995                 :          0 :                         *user_ptr = (void __user *)edid->edid;
    2996                 :          0 :                         *kernel_ptr = (void **)&edid->edid;
    2997                 :          0 :                         *array_size = edid->blocks * 128;
    2998                 :            :                         ret = 1;
    2999                 :            :                 }
    3000                 :            :                 break;
    3001                 :            :         }
    3002                 :            : 
    3003                 :            :         case VIDIOC_S_EXT_CTRLS:
    3004                 :            :         case VIDIOC_G_EXT_CTRLS:
    3005                 :            :         case VIDIOC_TRY_EXT_CTRLS: {
    3006                 :            :                 struct v4l2_ext_controls *ctrls = parg;
    3007                 :            : 
    3008                 :          0 :                 if (ctrls->count != 0) {
    3009                 :          0 :                         if (ctrls->count > V4L2_CID_MAX_CTRLS) {
    3010                 :            :                                 ret = -EINVAL;
    3011                 :            :                                 break;
    3012                 :            :                         }
    3013                 :          0 :                         *user_ptr = (void __user *)ctrls->controls;
    3014                 :          0 :                         *kernel_ptr = (void **)&ctrls->controls;
    3015                 :          0 :                         *array_size = sizeof(struct v4l2_ext_control)
    3016                 :          0 :                                     * ctrls->count;
    3017                 :            :                         ret = 1;
    3018                 :            :                 }
    3019                 :            :                 break;
    3020                 :            :         }
    3021                 :            :         }
    3022                 :            : 
    3023                 :          3 :         return ret;
    3024                 :            : }
    3025                 :            : 
    3026                 :            : long
    3027                 :          3 : video_usercopy(struct file *file, unsigned int cmd, unsigned long arg,
    3028                 :            :                v4l2_kioctl func)
    3029                 :            : {
    3030                 :            :         char    sbuf[128];
    3031                 :            :         void    *mbuf = NULL;
    3032                 :          3 :         void    *parg = (void *)arg;
    3033                 :            :         long    err  = -EINVAL;
    3034                 :            :         bool    has_array_args;
    3035                 :            :         bool    always_copy = false;
    3036                 :          3 :         size_t  array_size = 0;
    3037                 :          3 :         void __user *user_ptr = NULL;
    3038                 :          3 :         void    **kernel_ptr = NULL;
    3039                 :          3 :         const size_t ioc_size = _IOC_SIZE(cmd);
    3040                 :            : 
    3041                 :            :         /*  Copy arguments into temp kernel buffer  */
    3042                 :          3 :         if (_IOC_DIR(cmd) != _IOC_NONE) {
    3043                 :          3 :                 if (ioc_size <= sizeof(sbuf)) {
    3044                 :            :                         parg = sbuf;
    3045                 :            :                 } else {
    3046                 :            :                         /* too big to allocate from stack */
    3047                 :            :                         mbuf = kvmalloc(ioc_size, GFP_KERNEL);
    3048                 :          0 :                         if (NULL == mbuf)
    3049                 :            :                                 return -ENOMEM;
    3050                 :            :                         parg = mbuf;
    3051                 :            :                 }
    3052                 :            : 
    3053                 :            :                 err = -EFAULT;
    3054                 :          3 :                 if (_IOC_DIR(cmd) & _IOC_WRITE) {
    3055                 :            :                         unsigned int n = ioc_size;
    3056                 :            : 
    3057                 :            :                         /*
    3058                 :            :                          * In some cases, only a few fields are used as input,
    3059                 :            :                          * i.e. when the app sets "index" and then the driver
    3060                 :            :                          * fills in the rest of the structure for the thing
    3061                 :            :                          * with that index.  We only need to copy up the first
    3062                 :            :                          * non-input field.
    3063                 :            :                          */
    3064                 :          0 :                         if (v4l2_is_known_ioctl(cmd)) {
    3065                 :          0 :                                 u32 flags = v4l2_ioctls[_IOC_NR(cmd)].flags;
    3066                 :            : 
    3067                 :          0 :                                 if (flags & INFO_FL_CLEAR_MASK)
    3068                 :          0 :                                         n = (flags & INFO_FL_CLEAR_MASK) >> 16;
    3069                 :          0 :                                 always_copy = flags & INFO_FL_ALWAYS_COPY;
    3070                 :            :                         }
    3071                 :            : 
    3072                 :          0 :                         if (copy_from_user(parg, (void __user *)arg, n))
    3073                 :            :                                 goto out;
    3074                 :            : 
    3075                 :            :                         /* zero out anything we don't copy from userspace */
    3076                 :          0 :                         if (n < ioc_size)
    3077                 :          0 :                                 memset((u8 *)parg + n, 0, ioc_size - n);
    3078                 :            :                 } else {
    3079                 :            :                         /* read-only ioctl */
    3080                 :          3 :                         memset(parg, 0, ioc_size);
    3081                 :            :                 }
    3082                 :            :         }
    3083                 :            : 
    3084                 :          3 :         err = check_array_args(cmd, parg, &array_size, &user_ptr, &kernel_ptr);
    3085                 :          3 :         if (err < 0)
    3086                 :            :                 goto out;
    3087                 :          3 :         has_array_args = err;
    3088                 :            : 
    3089                 :          3 :         if (has_array_args) {
    3090                 :            :                 /*
    3091                 :            :                  * When adding new types of array args, make sure that the
    3092                 :            :                  * parent argument to ioctl (which contains the pointer to the
    3093                 :            :                  * array) fits into sbuf (so that mbuf will still remain
    3094                 :            :                  * unused up to here).
    3095                 :            :                  */
    3096                 :          0 :                 mbuf = kvmalloc(array_size, GFP_KERNEL);
    3097                 :            :                 err = -ENOMEM;
    3098                 :          0 :                 if (NULL == mbuf)
    3099                 :            :                         goto out_array_args;
    3100                 :            :                 err = -EFAULT;
    3101                 :          0 :                 if (copy_from_user(mbuf, user_ptr, array_size))
    3102                 :            :                         goto out_array_args;
    3103                 :          0 :                 *kernel_ptr = mbuf;
    3104                 :            :         }
    3105                 :            : 
    3106                 :            :         /* Handles IOCTL */
    3107                 :          3 :         err = func(file, cmd, parg);
    3108                 :          3 :         if (err == -ENOTTY || err == -ENOIOCTLCMD) {
    3109                 :            :                 err = -ENOTTY;
    3110                 :            :                 goto out;
    3111                 :            :         }
    3112                 :            : 
    3113                 :          3 :         if (err == 0) {
    3114                 :          3 :                 if (cmd == VIDIOC_DQBUF)
    3115                 :          0 :                         trace_v4l2_dqbuf(video_devdata(file)->minor, parg);
    3116                 :          3 :                 else if (cmd == VIDIOC_QBUF)
    3117                 :          0 :                         trace_v4l2_qbuf(video_devdata(file)->minor, parg);
    3118                 :            :         }
    3119                 :            : 
    3120                 :          3 :         if (has_array_args) {
    3121                 :          0 :                 *kernel_ptr = (void __force *)user_ptr;
    3122                 :          0 :                 if (copy_to_user(user_ptr, mbuf, array_size))
    3123                 :            :                         err = -EFAULT;
    3124                 :            :                 goto out_array_args;
    3125                 :            :         }
    3126                 :            :         /*
    3127                 :            :          * Some ioctls can return an error, but still have valid
    3128                 :            :          * results that must be returned.
    3129                 :            :          */
    3130                 :          3 :         if (err < 0 && !always_copy)
    3131                 :            :                 goto out;
    3132                 :            : 
    3133                 :            : out_array_args:
    3134                 :            :         /*  Copy results into user buffer  */
    3135                 :          3 :         switch (_IOC_DIR(cmd)) {
    3136                 :            :         case _IOC_READ:
    3137                 :            :         case (_IOC_WRITE | _IOC_READ):
    3138                 :          3 :                 if (copy_to_user((void __user *)arg, parg, ioc_size))
    3139                 :            :                         err = -EFAULT;
    3140                 :            :                 break;
    3141                 :            :         }
    3142                 :            : 
    3143                 :            : out:
    3144                 :          3 :         kvfree(mbuf);
    3145                 :          3 :         return err;
    3146                 :            : }
    3147                 :            : 
    3148                 :          3 : long video_ioctl2(struct file *file,
    3149                 :            :                unsigned int cmd, unsigned long arg)
    3150                 :            : {
    3151                 :          3 :         return video_usercopy(file, cmd, arg, __video_do_ioctl);
    3152                 :            : }
    3153                 :            : EXPORT_SYMBOL(video_ioctl2);
    

Generated by: LCOV version 1.14