Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * Video capture interface for Linux version 2
0004  *
0005  * A generic framework to process V4L2 ioctl commands.
0006  *
0007  * Authors: Alan Cox, <alan@lxorguk.ukuu.org.uk> (version 1)
0008  *              Mauro Carvalho Chehab <mchehab@kernel.org> (version 2)
0009  */
0010 
0011 #include <linux/compat.h>
0012 #include <linux/mm.h>
0013 #include <linux/module.h>
0014 #include <linux/slab.h>
0015 #include <linux/types.h>
0016 #include <linux/kernel.h>
0017 #include <linux/version.h>
0018 
0019 #include <linux/videodev2.h>
0020 
0021 #include <media/media-device.h> /* for media_set_bus_info() */
0022 #include <media/v4l2-common.h>
0023 #include <media/v4l2-ioctl.h>
0024 #include <media/v4l2-ctrls.h>
0025 #include <media/v4l2-fh.h>
0026 #include <media/v4l2-event.h>
0027 #include <media/v4l2-device.h>
0028 #include <media/videobuf2-v4l2.h>
0029 #include <media/v4l2-mc.h>
0030 #include <media/v4l2-mem2mem.h>
0031 
0032 #include <trace/events/v4l2.h>
0033 
0034 /* Zero out the end of the struct pointed to by p.  Everything after, but
0035  * not including, the specified field is cleared. */
0036 #define CLEAR_AFTER_FIELD(p, field) \
0037     memset((u8 *)(p) + offsetof(typeof(*(p)), field) + sizeof((p)->field), \
0038     0, sizeof(*(p)) - offsetof(typeof(*(p)), field) - sizeof((p)->field))
0039 
0040 #define is_valid_ioctl(vfd, cmd) test_bit(_IOC_NR(cmd), (vfd)->valid_ioctls)
0041 
0042 struct std_descr {
0043     v4l2_std_id std;
0044     const char *descr;
0045 };
0046 
0047 static const struct std_descr standards[] = {
0048     { V4L2_STD_NTSC,    "NTSC"      },
0049     { V4L2_STD_NTSC_M,  "NTSC-M"    },
0050     { V4L2_STD_NTSC_M_JP,   "NTSC-M-JP" },
0051     { V4L2_STD_NTSC_M_KR,   "NTSC-M-KR" },
0052     { V4L2_STD_NTSC_443,    "NTSC-443"  },
0053     { V4L2_STD_PAL,     "PAL"       },
0054     { V4L2_STD_PAL_BG,  "PAL-BG"    },
0055     { V4L2_STD_PAL_B,   "PAL-B"     },
0056     { V4L2_STD_PAL_B1,  "PAL-B1"    },
0057     { V4L2_STD_PAL_G,   "PAL-G"     },
0058     { V4L2_STD_PAL_H,   "PAL-H"     },
0059     { V4L2_STD_PAL_I,   "PAL-I"     },
0060     { V4L2_STD_PAL_DK,  "PAL-DK"    },
0061     { V4L2_STD_PAL_D,   "PAL-D"     },
0062     { V4L2_STD_PAL_D1,  "PAL-D1"    },
0063     { V4L2_STD_PAL_K,   "PAL-K"     },
0064     { V4L2_STD_PAL_M,   "PAL-M"     },
0065     { V4L2_STD_PAL_N,   "PAL-N"     },
0066     { V4L2_STD_PAL_Nc,  "PAL-Nc"    },
0067     { V4L2_STD_PAL_60,  "PAL-60"    },
0068     { V4L2_STD_SECAM,   "SECAM"     },
0069     { V4L2_STD_SECAM_B, "SECAM-B"   },
0070     { V4L2_STD_SECAM_G, "SECAM-G"   },
0071     { V4L2_STD_SECAM_H, "SECAM-H"   },
0072     { V4L2_STD_SECAM_DK,    "SECAM-DK"  },
0073     { V4L2_STD_SECAM_D, "SECAM-D"   },
0074     { V4L2_STD_SECAM_K, "SECAM-K"   },
0075     { V4L2_STD_SECAM_K1,    "SECAM-K1"  },
0076     { V4L2_STD_SECAM_L, "SECAM-L"   },
0077     { V4L2_STD_SECAM_LC,    "SECAM-Lc"  },
0078     { 0,            "Unknown"   }
0079 };
0080 
0081 /* video4linux standard ID conversion to standard name
0082  */
0083 const char *v4l2_norm_to_name(v4l2_std_id id)
0084 {
0085     u32 myid = id;
0086     int i;
0087 
0088     /* HACK: ppc32 architecture doesn't have __ucmpdi2 function to handle
0089        64 bit comparisons. So, on that architecture, with some gcc
0090        variants, compilation fails. Currently, the max value is 30bit wide.
0091      */
0092     BUG_ON(myid != id);
0093 
0094     for (i = 0; standards[i].std; i++)
0095         if (myid == standards[i].std)
0096             break;
0097     return standards[i].descr;
0098 }
0099 EXPORT_SYMBOL(v4l2_norm_to_name);
0100 
0101 /* Returns frame period for the given standard */
0102 void v4l2_video_std_frame_period(int id, struct v4l2_fract *frameperiod)
0103 {
0104     if (id & V4L2_STD_525_60) {
0105         frameperiod->numerator = 1001;
0106         frameperiod->denominator = 30000;
0107     } else {
0108         frameperiod->numerator = 1;
0109         frameperiod->denominator = 25;
0110     }
0111 }
0112 EXPORT_SYMBOL(v4l2_video_std_frame_period);
0113 
0114 /* Fill in the fields of a v4l2_standard structure according to the
0115    'id' and 'transmission' parameters.  Returns negative on error.  */
0116 int v4l2_video_std_construct(struct v4l2_standard *vs,
0117                  int id, const char *name)
0118 {
0119     vs->id = id;
0120     v4l2_video_std_frame_period(id, &vs->frameperiod);
0121     vs->framelines = (id & V4L2_STD_525_60) ? 525 : 625;
0122     strscpy(vs->name, name, sizeof(vs->name));
0123     return 0;
0124 }
0125 EXPORT_SYMBOL(v4l2_video_std_construct);
0126 
0127 /* Fill in the fields of a v4l2_standard structure according to the
0128  * 'id' and 'vs->index' parameters. Returns negative on error. */
0129 int v4l_video_std_enumstd(struct v4l2_standard *vs, v4l2_std_id id)
0130 {
0131     v4l2_std_id curr_id = 0;
0132     unsigned int index = vs->index, i, j = 0;
0133     const char *descr = "";
0134 
0135     /* Return -ENODATA if the id for the current input
0136        or output is 0, meaning that it doesn't support this API. */
0137     if (id == 0)
0138         return -ENODATA;
0139 
0140     /* Return norm array in a canonical way */
0141     for (i = 0; i <= index && id; i++) {
0142         /* last std value in the standards array is 0, so this
0143            while always ends there since (id & 0) == 0. */
0144         while ((id & standards[j].std) != standards[j].std)
0145             j++;
0146         curr_id = standards[j].std;
0147         descr = standards[j].descr;
0148         j++;
0149         if (curr_id == 0)
0150             break;
0151         if (curr_id != V4L2_STD_PAL &&
0152                 curr_id != V4L2_STD_SECAM &&
0153                 curr_id != V4L2_STD_NTSC)
0154             id &= ~curr_id;
0155     }
0156     if (i <= index)
0157         return -EINVAL;
0158 
0159     v4l2_video_std_construct(vs, curr_id, descr);
0160     return 0;
0161 }
0162 
0163 /* ----------------------------------------------------------------- */
0164 /* some arrays for pretty-printing debug messages of enum types      */
0165 
0166 const char *v4l2_field_names[] = {
0167     [V4L2_FIELD_ANY]        = "any",
0168     [V4L2_FIELD_NONE]       = "none",
0169     [V4L2_FIELD_TOP]        = "top",
0170     [V4L2_FIELD_BOTTOM]     = "bottom",
0171     [V4L2_FIELD_INTERLACED] = "interlaced",
0172     [V4L2_FIELD_SEQ_TB]     = "seq-tb",
0173     [V4L2_FIELD_SEQ_BT]     = "seq-bt",
0174     [V4L2_FIELD_ALTERNATE]  = "alternate",
0175     [V4L2_FIELD_INTERLACED_TB] = "interlaced-tb",
0176     [V4L2_FIELD_INTERLACED_BT] = "interlaced-bt",
0177 };
0178 EXPORT_SYMBOL(v4l2_field_names);
0179 
0180 const char *v4l2_type_names[] = {
0181     [0]                = "0",
0182     [V4L2_BUF_TYPE_VIDEO_CAPTURE]      = "vid-cap",
0183     [V4L2_BUF_TYPE_VIDEO_OVERLAY]      = "vid-overlay",
0184     [V4L2_BUF_TYPE_VIDEO_OUTPUT]       = "vid-out",
0185     [V4L2_BUF_TYPE_VBI_CAPTURE]        = "vbi-cap",
0186     [V4L2_BUF_TYPE_VBI_OUTPUT]         = "vbi-out",
0187     [V4L2_BUF_TYPE_SLICED_VBI_CAPTURE] = "sliced-vbi-cap",
0188     [V4L2_BUF_TYPE_SLICED_VBI_OUTPUT]  = "sliced-vbi-out",
0189     [V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY] = "vid-out-overlay",
0190     [V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE] = "vid-cap-mplane",
0191     [V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE] = "vid-out-mplane",
0192     [V4L2_BUF_TYPE_SDR_CAPTURE]        = "sdr-cap",
0193     [V4L2_BUF_TYPE_SDR_OUTPUT]         = "sdr-out",
0194     [V4L2_BUF_TYPE_META_CAPTURE]       = "meta-cap",
0195     [V4L2_BUF_TYPE_META_OUTPUT]    = "meta-out",
0196 };
0197 EXPORT_SYMBOL(v4l2_type_names);
0198 
0199 static const char *v4l2_memory_names[] = {
0200     [V4L2_MEMORY_MMAP]    = "mmap",
0201     [V4L2_MEMORY_USERPTR] = "userptr",
0202     [V4L2_MEMORY_OVERLAY] = "overlay",
0203     [V4L2_MEMORY_DMABUF] = "dmabuf",
0204 };
0205 
0206 #define prt_names(a, arr) (((unsigned)(a)) < ARRAY_SIZE(arr) ? arr[a] : "unknown")
0207 
0208 /* ------------------------------------------------------------------ */
0209 /* debug help functions                                               */
0210 
0211 static void v4l_print_querycap(const void *arg, bool write_only)
0212 {
0213     const struct v4l2_capability *p = arg;
0214 
0215     pr_cont("driver=%.*s, card=%.*s, bus=%.*s, version=0x%08x, capabilities=0x%08x, device_caps=0x%08x\n",
0216         (int)sizeof(p->driver), p->driver,
0217         (int)sizeof(p->card), p->card,
0218         (int)sizeof(p->bus_info), p->bus_info,
0219         p->version, p->capabilities, p->device_caps);
0220 }
0221 
0222 static void v4l_print_enuminput(const void *arg, bool write_only)
0223 {
0224     const struct v4l2_input *p = arg;
0225 
0226     pr_cont("index=%u, name=%.*s, type=%u, audioset=0x%x, tuner=%u, std=0x%08Lx, status=0x%x, capabilities=0x%x\n",
0227         p->index, (int)sizeof(p->name), p->name, p->type, p->audioset,
0228         p->tuner, (unsigned long long)p->std, p->status,
0229         p->capabilities);
0230 }
0231 
0232 static void v4l_print_enumoutput(const void *arg, bool write_only)
0233 {
0234     const struct v4l2_output *p = arg;
0235 
0236     pr_cont("index=%u, name=%.*s, type=%u, audioset=0x%x, modulator=%u, std=0x%08Lx, capabilities=0x%x\n",
0237         p->index, (int)sizeof(p->name), p->name, p->type, p->audioset,
0238         p->modulator, (unsigned long long)p->std, p->capabilities);
0239 }
0240 
0241 static void v4l_print_audio(const void *arg, bool write_only)
0242 {
0243     const struct v4l2_audio *p = arg;
0244 
0245     if (write_only)
0246         pr_cont("index=%u, mode=0x%x\n", p->index, p->mode);
0247     else
0248         pr_cont("index=%u, name=%.*s, capability=0x%x, mode=0x%x\n",
0249             p->index, (int)sizeof(p->name), p->name,
0250             p->capability, p->mode);
0251 }
0252 
0253 static void v4l_print_audioout(const void *arg, bool write_only)
0254 {
0255     const struct v4l2_audioout *p = arg;
0256 
0257     if (write_only)
0258         pr_cont("index=%u\n", p->index);
0259     else
0260         pr_cont("index=%u, name=%.*s, capability=0x%x, mode=0x%x\n",
0261             p->index, (int)sizeof(p->name), p->name,
0262             p->capability, p->mode);
0263 }
0264 
0265 static void v4l_print_fmtdesc(const void *arg, bool write_only)
0266 {
0267     const struct v4l2_fmtdesc *p = arg;
0268 
0269     pr_cont("index=%u, type=%s, flags=0x%x, pixelformat=%p4cc, mbus_code=0x%04x, description='%.*s'\n",
0270         p->index, prt_names(p->type, v4l2_type_names),
0271         p->flags, &p->pixelformat, p->mbus_code,
0272         (int)sizeof(p->description), p->description);
0273 }
0274 
0275 static void v4l_print_format(const void *arg, bool write_only)
0276 {
0277     const struct v4l2_format *p = arg;
0278     const struct v4l2_pix_format *pix;
0279     const struct v4l2_pix_format_mplane *mp;
0280     const struct v4l2_vbi_format *vbi;
0281     const struct v4l2_sliced_vbi_format *sliced;
0282     const struct v4l2_window *win;
0283     const struct v4l2_meta_format *meta;
0284     u32 pixelformat;
0285     u32 planes;
0286     unsigned i;
0287 
0288     pr_cont("type=%s", prt_names(p->type, v4l2_type_names));
0289     switch (p->type) {
0290     case V4L2_BUF_TYPE_VIDEO_CAPTURE:
0291     case V4L2_BUF_TYPE_VIDEO_OUTPUT:
0292         pix = &p->fmt.pix;
0293         pr_cont(", width=%u, height=%u, pixelformat=%p4cc, field=%s, bytesperline=%u, sizeimage=%u, colorspace=%d, flags=0x%x, ycbcr_enc=%u, quantization=%u, xfer_func=%u\n",
0294             pix->width, pix->height, &pix->pixelformat,
0295             prt_names(pix->field, v4l2_field_names),
0296             pix->bytesperline, pix->sizeimage,
0297             pix->colorspace, pix->flags, pix->ycbcr_enc,
0298             pix->quantization, pix->xfer_func);
0299         break;
0300     case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
0301     case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
0302         mp = &p->fmt.pix_mp;
0303         pixelformat = mp->pixelformat;
0304         pr_cont(", width=%u, height=%u, format=%p4cc, field=%s, colorspace=%d, num_planes=%u, flags=0x%x, ycbcr_enc=%u, quantization=%u, xfer_func=%u\n",
0305             mp->width, mp->height, &pixelformat,
0306             prt_names(mp->field, v4l2_field_names),
0307             mp->colorspace, mp->num_planes, mp->flags,
0308             mp->ycbcr_enc, mp->quantization, mp->xfer_func);
0309         planes = min_t(u32, mp->num_planes, VIDEO_MAX_PLANES);
0310         for (i = 0; i < planes; i++)
0311             printk(KERN_DEBUG "plane %u: bytesperline=%u sizeimage=%u\n", i,
0312                     mp->plane_fmt[i].bytesperline,
0313                     mp->plane_fmt[i].sizeimage);
0314         break;
0315     case V4L2_BUF_TYPE_VIDEO_OVERLAY:
0316     case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
0317         win = &p->fmt.win;
0318         /* Note: we can't print the clip list here since the clips
0319          * pointer is a userspace pointer, not a kernelspace
0320          * pointer. */
0321         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",
0322             win->w.width, win->w.height, win->w.left, win->w.top,
0323             prt_names(win->field, v4l2_field_names),
0324             win->chromakey, win->clipcount, win->clips,
0325             win->bitmap, win->global_alpha);
0326         break;
0327     case V4L2_BUF_TYPE_VBI_CAPTURE:
0328     case V4L2_BUF_TYPE_VBI_OUTPUT:
0329         vbi = &p->fmt.vbi;
0330         pr_cont(", sampling_rate=%u, offset=%u, samples_per_line=%u, sample_format=%p4cc, start=%u,%u, count=%u,%u\n",
0331             vbi->sampling_rate, vbi->offset,
0332             vbi->samples_per_line, &vbi->sample_format,
0333             vbi->start[0], vbi->start[1],
0334             vbi->count[0], vbi->count[1]);
0335         break;
0336     case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
0337     case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
0338         sliced = &p->fmt.sliced;
0339         pr_cont(", service_set=0x%08x, io_size=%d\n",
0340                 sliced->service_set, sliced->io_size);
0341         for (i = 0; i < 24; i++)
0342             printk(KERN_DEBUG "line[%02u]=0x%04x, 0x%04x\n", i,
0343                 sliced->service_lines[0][i],
0344                 sliced->service_lines[1][i]);
0345         break;
0346     case V4L2_BUF_TYPE_SDR_CAPTURE:
0347     case V4L2_BUF_TYPE_SDR_OUTPUT:
0348         pixelformat = p->fmt.sdr.pixelformat;
0349         pr_cont(", pixelformat=%p4cc\n", &pixelformat);
0350         break;
0351     case V4L2_BUF_TYPE_META_CAPTURE:
0352     case V4L2_BUF_TYPE_META_OUTPUT:
0353         meta = &p->fmt.meta;
0354         pixelformat = meta->dataformat;
0355         pr_cont(", dataformat=%p4cc, buffersize=%u\n",
0356             &pixelformat, meta->buffersize);
0357         break;
0358     }
0359 }
0360 
0361 static void v4l_print_framebuffer(const void *arg, bool write_only)
0362 {
0363     const struct v4l2_framebuffer *p = arg;
0364 
0365     pr_cont("capability=0x%x, flags=0x%x, base=0x%p, width=%u, height=%u, pixelformat=%p4cc, bytesperline=%u, sizeimage=%u, colorspace=%d\n",
0366         p->capability, p->flags, p->base, p->fmt.width, p->fmt.height,
0367         &p->fmt.pixelformat, p->fmt.bytesperline, p->fmt.sizeimage,
0368         p->fmt.colorspace);
0369 }
0370 
0371 static void v4l_print_buftype(const void *arg, bool write_only)
0372 {
0373     pr_cont("type=%s\n", prt_names(*(u32 *)arg, v4l2_type_names));
0374 }
0375 
0376 static void v4l_print_modulator(const void *arg, bool write_only)
0377 {
0378     const struct v4l2_modulator *p = arg;
0379 
0380     if (write_only)
0381         pr_cont("index=%u, txsubchans=0x%x\n", p->index, p->txsubchans);
0382     else
0383         pr_cont("index=%u, name=%.*s, capability=0x%x, rangelow=%u, rangehigh=%u, txsubchans=0x%x\n",
0384             p->index, (int)sizeof(p->name), p->name, p->capability,
0385             p->rangelow, p->rangehigh, p->txsubchans);
0386 }
0387 
0388 static void v4l_print_tuner(const void *arg, bool write_only)
0389 {
0390     const struct v4l2_tuner *p = arg;
0391 
0392     if (write_only)
0393         pr_cont("index=%u, audmode=%u\n", p->index, p->audmode);
0394     else
0395         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",
0396             p->index, (int)sizeof(p->name), p->name, p->type,
0397             p->capability, p->rangelow,
0398             p->rangehigh, p->signal, p->afc,
0399             p->rxsubchans, p->audmode);
0400 }
0401 
0402 static void v4l_print_frequency(const void *arg, bool write_only)
0403 {
0404     const struct v4l2_frequency *p = arg;
0405 
0406     pr_cont("tuner=%u, type=%u, frequency=%u\n",
0407                 p->tuner, p->type, p->frequency);
0408 }
0409 
0410 static void v4l_print_standard(const void *arg, bool write_only)
0411 {
0412     const struct v4l2_standard *p = arg;
0413 
0414     pr_cont("index=%u, id=0x%Lx, name=%.*s, fps=%u/%u, framelines=%u\n",
0415         p->index,
0416         (unsigned long long)p->id, (int)sizeof(p->name), p->name,
0417         p->frameperiod.numerator,
0418         p->frameperiod.denominator,
0419         p->framelines);
0420 }
0421 
0422 static void v4l_print_std(const void *arg, bool write_only)
0423 {
0424     pr_cont("std=0x%08Lx\n", *(const long long unsigned *)arg);
0425 }
0426 
0427 static void v4l_print_hw_freq_seek(const void *arg, bool write_only)
0428 {
0429     const struct v4l2_hw_freq_seek *p = arg;
0430 
0431     pr_cont("tuner=%u, type=%u, seek_upward=%u, wrap_around=%u, spacing=%u, rangelow=%u, rangehigh=%u\n",
0432         p->tuner, p->type, p->seek_upward, p->wrap_around, p->spacing,
0433         p->rangelow, p->rangehigh);
0434 }
0435 
0436 static void v4l_print_requestbuffers(const void *arg, bool write_only)
0437 {
0438     const struct v4l2_requestbuffers *p = arg;
0439 
0440     pr_cont("count=%d, type=%s, memory=%s\n",
0441         p->count,
0442         prt_names(p->type, v4l2_type_names),
0443         prt_names(p->memory, v4l2_memory_names));
0444 }
0445 
0446 static void v4l_print_buffer(const void *arg, bool write_only)
0447 {
0448     const struct v4l2_buffer *p = arg;
0449     const struct v4l2_timecode *tc = &p->timecode;
0450     const struct v4l2_plane *plane;
0451     int i;
0452 
0453     pr_cont("%02d:%02d:%02d.%06ld index=%d, type=%s, request_fd=%d, flags=0x%08x, field=%s, sequence=%d, memory=%s",
0454             (int)p->timestamp.tv_sec / 3600,
0455             ((int)p->timestamp.tv_sec / 60) % 60,
0456             ((int)p->timestamp.tv_sec % 60),
0457             (long)p->timestamp.tv_usec,
0458             p->index,
0459             prt_names(p->type, v4l2_type_names), p->request_fd,
0460             p->flags, prt_names(p->field, v4l2_field_names),
0461             p->sequence, prt_names(p->memory, v4l2_memory_names));
0462 
0463     if (V4L2_TYPE_IS_MULTIPLANAR(p->type) && p->m.planes) {
0464         pr_cont("\n");
0465         for (i = 0; i < p->length; ++i) {
0466             plane = &p->m.planes[i];
0467             printk(KERN_DEBUG
0468                 "plane %d: bytesused=%d, data_offset=0x%08x, offset/userptr=0x%lx, length=%d\n",
0469                 i, plane->bytesused, plane->data_offset,
0470                 plane->m.userptr, plane->length);
0471         }
0472     } else {
0473         pr_cont(", bytesused=%d, offset/userptr=0x%lx, length=%d\n",
0474             p->bytesused, p->m.userptr, p->length);
0475     }
0476 
0477     printk(KERN_DEBUG "timecode=%02d:%02d:%02d type=%d, flags=0x%08x, frames=%d, userbits=0x%08x\n",
0478             tc->hours, tc->minutes, tc->seconds,
0479             tc->type, tc->flags, tc->frames, *(__u32 *)tc->userbits);
0480 }
0481 
0482 static void v4l_print_exportbuffer(const void *arg, bool write_only)
0483 {
0484     const struct v4l2_exportbuffer *p = arg;
0485 
0486     pr_cont("fd=%d, type=%s, index=%u, plane=%u, flags=0x%08x\n",
0487         p->fd, prt_names(p->type, v4l2_type_names),
0488         p->index, p->plane, p->flags);
0489 }
0490 
0491 static void v4l_print_create_buffers(const void *arg, bool write_only)
0492 {
0493     const struct v4l2_create_buffers *p = arg;
0494 
0495     pr_cont("index=%d, count=%d, memory=%s, capabilities=0x%08x, ",
0496         p->index, p->count, prt_names(p->memory, v4l2_memory_names),
0497         p->capabilities);
0498     v4l_print_format(&p->format, write_only);
0499 }
0500 
0501 static void v4l_print_streamparm(const void *arg, bool write_only)
0502 {
0503     const struct v4l2_streamparm *p = arg;
0504 
0505     pr_cont("type=%s", prt_names(p->type, v4l2_type_names));
0506 
0507     if (p->type == V4L2_BUF_TYPE_VIDEO_CAPTURE ||
0508         p->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
0509         const struct v4l2_captureparm *c = &p->parm.capture;
0510 
0511         pr_cont(", capability=0x%x, capturemode=0x%x, timeperframe=%d/%d, extendedmode=%d, readbuffers=%d\n",
0512             c->capability, c->capturemode,
0513             c->timeperframe.numerator, c->timeperframe.denominator,
0514             c->extendedmode, c->readbuffers);
0515     } else if (p->type == V4L2_BUF_TYPE_VIDEO_OUTPUT ||
0516            p->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
0517         const struct v4l2_outputparm *c = &p->parm.output;
0518 
0519         pr_cont(", capability=0x%x, outputmode=0x%x, timeperframe=%d/%d, extendedmode=%d, writebuffers=%d\n",
0520             c->capability, c->outputmode,
0521             c->timeperframe.numerator, c->timeperframe.denominator,
0522             c->extendedmode, c->writebuffers);
0523     } else {
0524         pr_cont("\n");
0525     }
0526 }
0527 
0528 static void v4l_print_queryctrl(const void *arg, bool write_only)
0529 {
0530     const struct v4l2_queryctrl *p = arg;
0531 
0532     pr_cont("id=0x%x, type=%d, name=%.*s, min/max=%d/%d, step=%d, default=%d, flags=0x%08x\n",
0533             p->id, p->type, (int)sizeof(p->name), p->name,
0534             p->minimum, p->maximum,
0535             p->step, p->default_value, p->flags);
0536 }
0537 
0538 static void v4l_print_query_ext_ctrl(const void *arg, bool write_only)
0539 {
0540     const struct v4l2_query_ext_ctrl *p = arg;
0541 
0542     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",
0543             p->id, p->type, (int)sizeof(p->name), p->name,
0544             p->minimum, p->maximum,
0545             p->step, p->default_value, p->flags,
0546             p->elem_size, p->elems, p->nr_of_dims,
0547             p->dims[0], p->dims[1], p->dims[2], p->dims[3]);
0548 }
0549 
0550 static void v4l_print_querymenu(const void *arg, bool write_only)
0551 {
0552     const struct v4l2_querymenu *p = arg;
0553 
0554     pr_cont("id=0x%x, index=%d\n", p->id, p->index);
0555 }
0556 
0557 static void v4l_print_control(const void *arg, bool write_only)
0558 {
0559     const struct v4l2_control *p = arg;
0560     const char *name = v4l2_ctrl_get_name(p->id);
0561 
0562     if (name)
0563         pr_cont("name=%s, ", name);
0564     pr_cont("id=0x%x, value=%d\n", p->id, p->value);
0565 }
0566 
0567 static void v4l_print_ext_controls(const void *arg, bool write_only)
0568 {
0569     const struct v4l2_ext_controls *p = arg;
0570     int i;
0571 
0572     pr_cont("which=0x%x, count=%d, error_idx=%d, request_fd=%d",
0573             p->which, p->count, p->error_idx, p->request_fd);
0574     for (i = 0; i < p->count; i++) {
0575         unsigned int id = p->controls[i].id;
0576         const char *name = v4l2_ctrl_get_name(id);
0577 
0578         if (name)
0579             pr_cont(", name=%s", name);
0580         if (!p->controls[i].size)
0581             pr_cont(", id/val=0x%x/0x%x", id, p->controls[i].value);
0582         else
0583             pr_cont(", id/size=0x%x/%u", id, p->controls[i].size);
0584     }
0585     pr_cont("\n");
0586 }
0587 
0588 static void v4l_print_cropcap(const void *arg, bool write_only)
0589 {
0590     const struct v4l2_cropcap *p = arg;
0591 
0592     pr_cont("type=%s, bounds wxh=%dx%d, x,y=%d,%d, defrect wxh=%dx%d, x,y=%d,%d, pixelaspect %d/%d\n",
0593         prt_names(p->type, v4l2_type_names),
0594         p->bounds.width, p->bounds.height,
0595         p->bounds.left, p->bounds.top,
0596         p->defrect.width, p->defrect.height,
0597         p->defrect.left, p->defrect.top,
0598         p->pixelaspect.numerator, p->pixelaspect.denominator);
0599 }
0600 
0601 static void v4l_print_crop(const void *arg, bool write_only)
0602 {
0603     const struct v4l2_crop *p = arg;
0604 
0605     pr_cont("type=%s, wxh=%dx%d, x,y=%d,%d\n",
0606         prt_names(p->type, v4l2_type_names),
0607         p->c.width, p->c.height,
0608         p->c.left, p->c.top);
0609 }
0610 
0611 static void v4l_print_selection(const void *arg, bool write_only)
0612 {
0613     const struct v4l2_selection *p = arg;
0614 
0615     pr_cont("type=%s, target=%d, flags=0x%x, wxh=%dx%d, x,y=%d,%d\n",
0616         prt_names(p->type, v4l2_type_names),
0617         p->target, p->flags,
0618         p->r.width, p->r.height, p->r.left, p->r.top);
0619 }
0620 
0621 static void v4l_print_jpegcompression(const void *arg, bool write_only)
0622 {
0623     const struct v4l2_jpegcompression *p = arg;
0624 
0625     pr_cont("quality=%d, APPn=%d, APP_len=%d, COM_len=%d, jpeg_markers=0x%x\n",
0626         p->quality, p->APPn, p->APP_len,
0627         p->COM_len, p->jpeg_markers);
0628 }
0629 
0630 static void v4l_print_enc_idx(const void *arg, bool write_only)
0631 {
0632     const struct v4l2_enc_idx *p = arg;
0633 
0634     pr_cont("entries=%d, entries_cap=%d\n",
0635             p->entries, p->entries_cap);
0636 }
0637 
0638 static void v4l_print_encoder_cmd(const void *arg, bool write_only)
0639 {
0640     const struct v4l2_encoder_cmd *p = arg;
0641 
0642     pr_cont("cmd=%d, flags=0x%x\n",
0643             p->cmd, p->flags);
0644 }
0645 
0646 static void v4l_print_decoder_cmd(const void *arg, bool write_only)
0647 {
0648     const struct v4l2_decoder_cmd *p = arg;
0649 
0650     pr_cont("cmd=%d, flags=0x%x\n", p->cmd, p->flags);
0651 
0652     if (p->cmd == V4L2_DEC_CMD_START)
0653         pr_info("speed=%d, format=%u\n",
0654                 p->start.speed, p->start.format);
0655     else if (p->cmd == V4L2_DEC_CMD_STOP)
0656         pr_info("pts=%llu\n", p->stop.pts);
0657 }
0658 
0659 static void v4l_print_dbg_chip_info(const void *arg, bool write_only)
0660 {
0661     const struct v4l2_dbg_chip_info *p = arg;
0662 
0663     pr_cont("type=%u, ", p->match.type);
0664     if (p->match.type == V4L2_CHIP_MATCH_I2C_DRIVER)
0665         pr_cont("name=%.*s, ",
0666                 (int)sizeof(p->match.name), p->match.name);
0667     else
0668         pr_cont("addr=%u, ", p->match.addr);
0669     pr_cont("name=%.*s\n", (int)sizeof(p->name), p->name);
0670 }
0671 
0672 static void v4l_print_dbg_register(const void *arg, bool write_only)
0673 {
0674     const struct v4l2_dbg_register *p = arg;
0675 
0676     pr_cont("type=%u, ", p->match.type);
0677     if (p->match.type == V4L2_CHIP_MATCH_I2C_DRIVER)
0678         pr_cont("name=%.*s, ",
0679                 (int)sizeof(p->match.name), p->match.name);
0680     else
0681         pr_cont("addr=%u, ", p->match.addr);
0682     pr_cont("reg=0x%llx, val=0x%llx\n",
0683             p->reg, p->val);
0684 }
0685 
0686 static void v4l_print_dv_timings(const void *arg, bool write_only)
0687 {
0688     const struct v4l2_dv_timings *p = arg;
0689 
0690     switch (p->type) {
0691     case V4L2_DV_BT_656_1120:
0692         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",
0693                 p->bt.interlaced, p->bt.pixelclock,
0694                 p->bt.width, p->bt.height,
0695                 p->bt.polarities, p->bt.hfrontporch,
0696                 p->bt.hsync, p->bt.hbackporch,
0697                 p->bt.vfrontporch, p->bt.vsync,
0698                 p->bt.vbackporch, p->bt.il_vfrontporch,
0699                 p->bt.il_vsync, p->bt.il_vbackporch,
0700                 p->bt.standards, p->bt.flags);
0701         break;
0702     default:
0703         pr_cont("type=%d\n", p->type);
0704         break;
0705     }
0706 }
0707 
0708 static void v4l_print_enum_dv_timings(const void *arg, bool write_only)
0709 {
0710     const struct v4l2_enum_dv_timings *p = arg;
0711 
0712     pr_cont("index=%u, ", p->index);
0713     v4l_print_dv_timings(&p->timings, write_only);
0714 }
0715 
0716 static void v4l_print_dv_timings_cap(const void *arg, bool write_only)
0717 {
0718     const struct v4l2_dv_timings_cap *p = arg;
0719 
0720     switch (p->type) {
0721     case V4L2_DV_BT_656_1120:
0722         pr_cont("type=bt-656/1120, width=%u-%u, height=%u-%u, pixelclock=%llu-%llu, standards=0x%x, capabilities=0x%x\n",
0723             p->bt.min_width, p->bt.max_width,
0724             p->bt.min_height, p->bt.max_height,
0725             p->bt.min_pixelclock, p->bt.max_pixelclock,
0726             p->bt.standards, p->bt.capabilities);
0727         break;
0728     default:
0729         pr_cont("type=%u\n", p->type);
0730         break;
0731     }
0732 }
0733 
0734 static void v4l_print_frmsizeenum(const void *arg, bool write_only)
0735 {
0736     const struct v4l2_frmsizeenum *p = arg;
0737 
0738     pr_cont("index=%u, pixelformat=%p4cc, type=%u",
0739         p->index, &p->pixel_format, p->type);
0740     switch (p->type) {
0741     case V4L2_FRMSIZE_TYPE_DISCRETE:
0742         pr_cont(", wxh=%ux%u\n",
0743             p->discrete.width, p->discrete.height);
0744         break;
0745     case V4L2_FRMSIZE_TYPE_STEPWISE:
0746         pr_cont(", min=%ux%u, max=%ux%u, step=%ux%u\n",
0747                 p->stepwise.min_width,
0748                 p->stepwise.min_height,
0749                 p->stepwise.max_width,
0750                 p->stepwise.max_height,
0751                 p->stepwise.step_width,
0752                 p->stepwise.step_height);
0753         break;
0754     case V4L2_FRMSIZE_TYPE_CONTINUOUS:
0755     default:
0756         pr_cont("\n");
0757         break;
0758     }
0759 }
0760 
0761 static void v4l_print_frmivalenum(const void *arg, bool write_only)
0762 {
0763     const struct v4l2_frmivalenum *p = arg;
0764 
0765     pr_cont("index=%u, pixelformat=%p4cc, wxh=%ux%u, type=%u",
0766         p->index, &p->pixel_format, p->width, p->height, p->type);
0767     switch (p->type) {
0768     case V4L2_FRMIVAL_TYPE_DISCRETE:
0769         pr_cont(", fps=%d/%d\n",
0770                 p->discrete.numerator,
0771                 p->discrete.denominator);
0772         break;
0773     case V4L2_FRMIVAL_TYPE_STEPWISE:
0774         pr_cont(", min=%d/%d, max=%d/%d, step=%d/%d\n",
0775                 p->stepwise.min.numerator,
0776                 p->stepwise.min.denominator,
0777                 p->stepwise.max.numerator,
0778                 p->stepwise.max.denominator,
0779                 p->stepwise.step.numerator,
0780                 p->stepwise.step.denominator);
0781         break;
0782     case V4L2_FRMIVAL_TYPE_CONTINUOUS:
0783     default:
0784         pr_cont("\n");
0785         break;
0786     }
0787 }
0788 
0789 static void v4l_print_event(const void *arg, bool write_only)
0790 {
0791     const struct v4l2_event *p = arg;
0792     const struct v4l2_event_ctrl *c;
0793 
0794     pr_cont("type=0x%x, pending=%u, sequence=%u, id=%u, timestamp=%llu.%9.9llu\n",
0795             p->type, p->pending, p->sequence, p->id,
0796             p->timestamp.tv_sec, p->timestamp.tv_nsec);
0797     switch (p->type) {
0798     case V4L2_EVENT_VSYNC:
0799         printk(KERN_DEBUG "field=%s\n",
0800             prt_names(p->u.vsync.field, v4l2_field_names));
0801         break;
0802     case V4L2_EVENT_CTRL:
0803         c = &p->u.ctrl;
0804         printk(KERN_DEBUG "changes=0x%x, type=%u, ",
0805             c->changes, c->type);
0806         if (c->type == V4L2_CTRL_TYPE_INTEGER64)
0807             pr_cont("value64=%lld, ", c->value64);
0808         else
0809             pr_cont("value=%d, ", c->value);
0810         pr_cont("flags=0x%x, minimum=%d, maximum=%d, step=%d, default_value=%d\n",
0811             c->flags, c->minimum, c->maximum,
0812             c->step, c->default_value);
0813         break;
0814     case V4L2_EVENT_FRAME_SYNC:
0815         pr_cont("frame_sequence=%u\n",
0816             p->u.frame_sync.frame_sequence);
0817         break;
0818     }
0819 }
0820 
0821 static void v4l_print_event_subscription(const void *arg, bool write_only)
0822 {
0823     const struct v4l2_event_subscription *p = arg;
0824 
0825     pr_cont("type=0x%x, id=0x%x, flags=0x%x\n",
0826             p->type, p->id, p->flags);
0827 }
0828 
0829 static void v4l_print_sliced_vbi_cap(const void *arg, bool write_only)
0830 {
0831     const struct v4l2_sliced_vbi_cap *p = arg;
0832     int i;
0833 
0834     pr_cont("type=%s, service_set=0x%08x\n",
0835             prt_names(p->type, v4l2_type_names), p->service_set);
0836     for (i = 0; i < 24; i++)
0837         printk(KERN_DEBUG "line[%02u]=0x%04x, 0x%04x\n", i,
0838                 p->service_lines[0][i],
0839                 p->service_lines[1][i]);
0840 }
0841 
0842 static void v4l_print_freq_band(const void *arg, bool write_only)
0843 {
0844     const struct v4l2_frequency_band *p = arg;
0845 
0846     pr_cont("tuner=%u, type=%u, index=%u, capability=0x%x, rangelow=%u, rangehigh=%u, modulation=0x%x\n",
0847             p->tuner, p->type, p->index,
0848             p->capability, p->rangelow,
0849             p->rangehigh, p->modulation);
0850 }
0851 
0852 static void v4l_print_edid(const void *arg, bool write_only)
0853 {
0854     const struct v4l2_edid *p = arg;
0855 
0856     pr_cont("pad=%u, start_block=%u, blocks=%u\n",
0857         p->pad, p->start_block, p->blocks);
0858 }
0859 
0860 static void v4l_print_u32(const void *arg, bool write_only)
0861 {
0862     pr_cont("value=%u\n", *(const u32 *)arg);
0863 }
0864 
0865 static void v4l_print_newline(const void *arg, bool write_only)
0866 {
0867     pr_cont("\n");
0868 }
0869 
0870 static void v4l_print_default(const void *arg, bool write_only)
0871 {
0872     pr_cont("driver-specific ioctl\n");
0873 }
0874 
0875 static bool check_ext_ctrls(struct v4l2_ext_controls *c, unsigned long ioctl)
0876 {
0877     __u32 i;
0878 
0879     /* zero the reserved fields */
0880     c->reserved[0] = 0;
0881     for (i = 0; i < c->count; i++)
0882         c->controls[i].reserved2[0] = 0;
0883 
0884     switch (c->which) {
0885     case V4L2_CID_PRIVATE_BASE:
0886         /*
0887          * V4L2_CID_PRIVATE_BASE cannot be used as control class
0888          * when using extended controls.
0889          * Only when passed in through VIDIOC_G_CTRL and VIDIOC_S_CTRL
0890          * is it allowed for backwards compatibility.
0891          */
0892         if (ioctl == VIDIOC_G_CTRL || ioctl == VIDIOC_S_CTRL)
0893             return false;
0894         break;
0895     case V4L2_CTRL_WHICH_DEF_VAL:
0896         /* Default value cannot be changed */
0897         if (ioctl == VIDIOC_S_EXT_CTRLS ||
0898             ioctl == VIDIOC_TRY_EXT_CTRLS) {
0899             c->error_idx = c->count;
0900             return false;
0901         }
0902         return true;
0903     case V4L2_CTRL_WHICH_CUR_VAL:
0904         return true;
0905     case V4L2_CTRL_WHICH_REQUEST_VAL:
0906         c->error_idx = c->count;
0907         return false;
0908     }
0909 
0910     /* Check that all controls are from the same control class. */
0911     for (i = 0; i < c->count; i++) {
0912         if (V4L2_CTRL_ID2WHICH(c->controls[i].id) != c->which) {
0913             c->error_idx = ioctl == VIDIOC_TRY_EXT_CTRLS ? i :
0914                                       c->count;
0915             return false;
0916         }
0917     }
0918     return true;
0919 }
0920 
0921 static int check_fmt(struct file *file, enum v4l2_buf_type type)
0922 {
0923     const u32 vid_caps = V4L2_CAP_VIDEO_CAPTURE |
0924                  V4L2_CAP_VIDEO_CAPTURE_MPLANE |
0925                  V4L2_CAP_VIDEO_OUTPUT |
0926                  V4L2_CAP_VIDEO_OUTPUT_MPLANE |
0927                  V4L2_CAP_VIDEO_M2M | V4L2_CAP_VIDEO_M2M_MPLANE;
0928     const u32 meta_caps = V4L2_CAP_META_CAPTURE |
0929                   V4L2_CAP_META_OUTPUT;
0930     struct video_device *vfd = video_devdata(file);
0931     const struct v4l2_ioctl_ops *ops = vfd->ioctl_ops;
0932     bool is_vid = vfd->vfl_type == VFL_TYPE_VIDEO &&
0933               (vfd->device_caps & vid_caps);
0934     bool is_vbi = vfd->vfl_type == VFL_TYPE_VBI;
0935     bool is_sdr = vfd->vfl_type == VFL_TYPE_SDR;
0936     bool is_tch = vfd->vfl_type == VFL_TYPE_TOUCH;
0937     bool is_meta = vfd->vfl_type == VFL_TYPE_VIDEO &&
0938                (vfd->device_caps & meta_caps);
0939     bool is_rx = vfd->vfl_dir != VFL_DIR_TX;
0940     bool is_tx = vfd->vfl_dir != VFL_DIR_RX;
0941 
0942     if (ops == NULL)
0943         return -EINVAL;
0944 
0945     switch (type) {
0946     case V4L2_BUF_TYPE_VIDEO_CAPTURE:
0947         if ((is_vid || is_tch) && is_rx &&
0948             (ops->vidioc_g_fmt_vid_cap || ops->vidioc_g_fmt_vid_cap_mplane))
0949             return 0;
0950         break;
0951     case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
0952         if ((is_vid || is_tch) && is_rx && ops->vidioc_g_fmt_vid_cap_mplane)
0953             return 0;
0954         break;
0955     case V4L2_BUF_TYPE_VIDEO_OVERLAY:
0956         if (is_vid && is_rx && ops->vidioc_g_fmt_vid_overlay)
0957             return 0;
0958         break;
0959     case V4L2_BUF_TYPE_VIDEO_OUTPUT:
0960         if (is_vid && is_tx &&
0961             (ops->vidioc_g_fmt_vid_out || ops->vidioc_g_fmt_vid_out_mplane))
0962             return 0;
0963         break;
0964     case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
0965         if (is_vid && is_tx && ops->vidioc_g_fmt_vid_out_mplane)
0966             return 0;
0967         break;
0968     case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
0969         if (is_vid && is_tx && ops->vidioc_g_fmt_vid_out_overlay)
0970             return 0;
0971         break;
0972     case V4L2_BUF_TYPE_VBI_CAPTURE:
0973         if (is_vbi && is_rx && ops->vidioc_g_fmt_vbi_cap)
0974             return 0;
0975         break;
0976     case V4L2_BUF_TYPE_VBI_OUTPUT:
0977         if (is_vbi && is_tx && ops->vidioc_g_fmt_vbi_out)
0978             return 0;
0979         break;
0980     case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
0981         if (is_vbi && is_rx && ops->vidioc_g_fmt_sliced_vbi_cap)
0982             return 0;
0983         break;
0984     case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
0985         if (is_vbi && is_tx && ops->vidioc_g_fmt_sliced_vbi_out)
0986             return 0;
0987         break;
0988     case V4L2_BUF_TYPE_SDR_CAPTURE:
0989         if (is_sdr && is_rx && ops->vidioc_g_fmt_sdr_cap)
0990             return 0;
0991         break;
0992     case V4L2_BUF_TYPE_SDR_OUTPUT:
0993         if (is_sdr && is_tx && ops->vidioc_g_fmt_sdr_out)
0994             return 0;
0995         break;
0996     case V4L2_BUF_TYPE_META_CAPTURE:
0997         if (is_meta && is_rx && ops->vidioc_g_fmt_meta_cap)
0998             return 0;
0999         break;
1000     case V4L2_BUF_TYPE_META_OUTPUT:
1001         if (is_meta && is_tx && ops->vidioc_g_fmt_meta_out)
1002             return 0;
1003         break;
1004     default:
1005         break;
1006     }
1007     return -EINVAL;
1008 }
1009 
1010 static void v4l_sanitize_colorspace(u32 pixelformat, u32 *colorspace,
1011                     u32 *encoding, u32 *quantization,
1012                     u32 *xfer_func)
1013 {
1014     bool is_hsv = pixelformat == V4L2_PIX_FMT_HSV24 ||
1015               pixelformat == V4L2_PIX_FMT_HSV32;
1016 
1017     if (!v4l2_is_colorspace_valid(*colorspace)) {
1018         *colorspace = V4L2_COLORSPACE_DEFAULT;
1019         *encoding = V4L2_YCBCR_ENC_DEFAULT;
1020         *quantization = V4L2_QUANTIZATION_DEFAULT;
1021         *xfer_func = V4L2_XFER_FUNC_DEFAULT;
1022     }
1023 
1024     if ((!is_hsv && !v4l2_is_ycbcr_enc_valid(*encoding)) ||
1025         (is_hsv && !v4l2_is_hsv_enc_valid(*encoding)))
1026         *encoding = V4L2_YCBCR_ENC_DEFAULT;
1027 
1028     if (!v4l2_is_quant_valid(*quantization))
1029         *quantization = V4L2_QUANTIZATION_DEFAULT;
1030 
1031     if (!v4l2_is_xfer_func_valid(*xfer_func))
1032         *xfer_func = V4L2_XFER_FUNC_DEFAULT;
1033 }
1034 
1035 static void v4l_sanitize_format(struct v4l2_format *fmt)
1036 {
1037     unsigned int offset;
1038 
1039     /* Make sure num_planes is not bogus */
1040     if (fmt->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE ||
1041         fmt->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
1042         fmt->fmt.pix_mp.num_planes = min_t(u32, fmt->fmt.pix_mp.num_planes,
1043                            VIDEO_MAX_PLANES);
1044 
1045     /*
1046      * The v4l2_pix_format structure has been extended with fields that were
1047      * not previously required to be set to zero by applications. The priv
1048      * field, when set to a magic value, indicates the the extended fields
1049      * are valid. Otherwise they will contain undefined values. To simplify
1050      * the API towards drivers zero the extended fields and set the priv
1051      * field to the magic value when the extended pixel format structure
1052      * isn't used by applications.
1053      */
1054     if (fmt->type == V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1055         fmt->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1056         if (fmt->fmt.pix.priv != V4L2_PIX_FMT_PRIV_MAGIC) {
1057             fmt->fmt.pix.priv = V4L2_PIX_FMT_PRIV_MAGIC;
1058 
1059             offset = offsetof(struct v4l2_pix_format, priv)
1060                    + sizeof(fmt->fmt.pix.priv);
1061             memset(((void *)&fmt->fmt.pix) + offset, 0,
1062                    sizeof(fmt->fmt.pix) - offset);
1063         }
1064     }
1065 
1066     /* Replace invalid colorspace values with defaults. */
1067     if (fmt->type == V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1068         fmt->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1069         v4l_sanitize_colorspace(fmt->fmt.pix.pixelformat,
1070                     &fmt->fmt.pix.colorspace,
1071                     &fmt->fmt.pix.ycbcr_enc,
1072                     &fmt->fmt.pix.quantization,
1073                     &fmt->fmt.pix.xfer_func);
1074     } else if (fmt->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE ||
1075            fmt->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
1076         u32 ycbcr_enc = fmt->fmt.pix_mp.ycbcr_enc;
1077         u32 quantization = fmt->fmt.pix_mp.quantization;
1078         u32 xfer_func = fmt->fmt.pix_mp.xfer_func;
1079 
1080         v4l_sanitize_colorspace(fmt->fmt.pix_mp.pixelformat,
1081                     &fmt->fmt.pix_mp.colorspace, &ycbcr_enc,
1082                     &quantization, &xfer_func);
1083 
1084         fmt->fmt.pix_mp.ycbcr_enc = ycbcr_enc;
1085         fmt->fmt.pix_mp.quantization = quantization;
1086         fmt->fmt.pix_mp.xfer_func = xfer_func;
1087     }
1088 }
1089 
1090 static int v4l_querycap(const struct v4l2_ioctl_ops *ops,
1091                 struct file *file, void *fh, void *arg)
1092 {
1093     struct v4l2_capability *cap = (struct v4l2_capability *)arg;
1094     struct video_device *vfd = video_devdata(file);
1095     int ret;
1096 
1097     cap->version = LINUX_VERSION_CODE;
1098     cap->device_caps = vfd->device_caps;
1099     cap->capabilities = vfd->device_caps | V4L2_CAP_DEVICE_CAPS;
1100 
1101     media_set_bus_info(cap->bus_info, sizeof(cap->bus_info),
1102                vfd->dev_parent);
1103 
1104     ret = ops->vidioc_querycap(file, fh, cap);
1105 
1106     /*
1107      * Drivers must not change device_caps, so check for this and
1108      * warn if this happened.
1109      */
1110     WARN_ON(cap->device_caps != vfd->device_caps);
1111     /*
1112      * Check that capabilities is a superset of
1113      * vfd->device_caps | V4L2_CAP_DEVICE_CAPS
1114      */
1115     WARN_ON((cap->capabilities &
1116          (vfd->device_caps | V4L2_CAP_DEVICE_CAPS)) !=
1117         (vfd->device_caps | V4L2_CAP_DEVICE_CAPS));
1118     cap->capabilities |= V4L2_CAP_EXT_PIX_FORMAT;
1119     cap->device_caps |= V4L2_CAP_EXT_PIX_FORMAT;
1120 
1121     return ret;
1122 }
1123 
1124 static int v4l_g_input(const struct v4l2_ioctl_ops *ops,
1125                struct file *file, void *fh, void *arg)
1126 {
1127     struct video_device *vfd = video_devdata(file);
1128 
1129     if (vfd->device_caps & V4L2_CAP_IO_MC) {
1130         *(int *)arg = 0;
1131         return 0;
1132     }
1133 
1134     return ops->vidioc_g_input(file, fh, arg);
1135 }
1136 
1137 static int v4l_g_output(const struct v4l2_ioctl_ops *ops,
1138             struct file *file, void *fh, void *arg)
1139 {
1140     struct video_device *vfd = video_devdata(file);
1141 
1142     if (vfd->device_caps & V4L2_CAP_IO_MC) {
1143         *(int *)arg = 0;
1144         return 0;
1145     }
1146 
1147     return ops->vidioc_g_output(file, fh, arg);
1148 }
1149 
1150 static int v4l_s_input(const struct v4l2_ioctl_ops *ops,
1151                 struct file *file, void *fh, void *arg)
1152 {
1153     struct video_device *vfd = video_devdata(file);
1154     int ret;
1155 
1156     ret = v4l_enable_media_source(vfd);
1157     if (ret)
1158         return ret;
1159 
1160     if (vfd->device_caps & V4L2_CAP_IO_MC)
1161         return  *(int *)arg ? -EINVAL : 0;
1162 
1163     return ops->vidioc_s_input(file, fh, *(unsigned int *)arg);
1164 }
1165 
1166 static int v4l_s_output(const struct v4l2_ioctl_ops *ops,
1167                 struct file *file, void *fh, void *arg)
1168 {
1169     struct video_device *vfd = video_devdata(file);
1170 
1171     if (vfd->device_caps & V4L2_CAP_IO_MC)
1172         return  *(int *)arg ? -EINVAL : 0;
1173 
1174     return ops->vidioc_s_output(file, fh, *(unsigned int *)arg);
1175 }
1176 
1177 static int v4l_g_priority(const struct v4l2_ioctl_ops *ops,
1178                 struct file *file, void *fh, void *arg)
1179 {
1180     struct video_device *vfd;
1181     u32 *p = arg;
1182 
1183     vfd = video_devdata(file);
1184     *p = v4l2_prio_max(vfd->prio);
1185     return 0;
1186 }
1187 
1188 static int v4l_s_priority(const struct v4l2_ioctl_ops *ops,
1189                 struct file *file, void *fh, void *arg)
1190 {
1191     struct video_device *vfd;
1192     struct v4l2_fh *vfh;
1193     u32 *p = arg;
1194 
1195     vfd = video_devdata(file);
1196     if (!test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags))
1197         return -ENOTTY;
1198     vfh = file->private_data;
1199     return v4l2_prio_change(vfd->prio, &vfh->prio, *p);
1200 }
1201 
1202 static int v4l_enuminput(const struct v4l2_ioctl_ops *ops,
1203                 struct file *file, void *fh, void *arg)
1204 {
1205     struct video_device *vfd = video_devdata(file);
1206     struct v4l2_input *p = arg;
1207 
1208     /*
1209      * We set the flags for CAP_DV_TIMINGS &
1210      * CAP_STD here based on ioctl handler provided by the
1211      * driver. If the driver doesn't support these
1212      * for a specific input, it must override these flags.
1213      */
1214     if (is_valid_ioctl(vfd, VIDIOC_S_STD))
1215         p->capabilities |= V4L2_IN_CAP_STD;
1216 
1217     if (vfd->device_caps & V4L2_CAP_IO_MC) {
1218         if (p->index)
1219             return -EINVAL;
1220         strscpy(p->name, vfd->name, sizeof(p->name));
1221         p->type = V4L2_INPUT_TYPE_CAMERA;
1222         return 0;
1223     }
1224 
1225     return ops->vidioc_enum_input(file, fh, p);
1226 }
1227 
1228 static int v4l_enumoutput(const struct v4l2_ioctl_ops *ops,
1229                 struct file *file, void *fh, void *arg)
1230 {
1231     struct video_device *vfd = video_devdata(file);
1232     struct v4l2_output *p = arg;
1233 
1234     /*
1235      * We set the flags for CAP_DV_TIMINGS &
1236      * CAP_STD here based on ioctl handler provided by the
1237      * driver. If the driver doesn't support these
1238      * for a specific output, it must override these flags.
1239      */
1240     if (is_valid_ioctl(vfd, VIDIOC_S_STD))
1241         p->capabilities |= V4L2_OUT_CAP_STD;
1242 
1243     if (vfd->device_caps & V4L2_CAP_IO_MC) {
1244         if (p->index)
1245             return -EINVAL;
1246         strscpy(p->name, vfd->name, sizeof(p->name));
1247         p->type = V4L2_OUTPUT_TYPE_ANALOG;
1248         return 0;
1249     }
1250 
1251     return ops->vidioc_enum_output(file, fh, p);
1252 }
1253 
1254 static void v4l_fill_fmtdesc(struct v4l2_fmtdesc *fmt)
1255 {
1256     const unsigned sz = sizeof(fmt->description);
1257     const char *descr = NULL;
1258     u32 flags = 0;
1259 
1260     /*
1261      * We depart from the normal coding style here since the descriptions
1262      * should be aligned so it is easy to see which descriptions will be
1263      * longer than 31 characters (the max length for a description).
1264      * And frankly, this is easier to read anyway.
1265      *
1266      * Note that gcc will use O(log N) comparisons to find the right case.
1267      */
1268     switch (fmt->pixelformat) {
1269     /* Max description length mask: descr = "0123456789012345678901234567890" */
1270     case V4L2_PIX_FMT_RGB332:   descr = "8-bit RGB 3-3-2"; break;
1271     case V4L2_PIX_FMT_RGB444:   descr = "16-bit A/XRGB 4-4-4-4"; break;
1272     case V4L2_PIX_FMT_ARGB444:  descr = "16-bit ARGB 4-4-4-4"; break;
1273     case V4L2_PIX_FMT_XRGB444:  descr = "16-bit XRGB 4-4-4-4"; break;
1274     case V4L2_PIX_FMT_RGBA444:  descr = "16-bit RGBA 4-4-4-4"; break;
1275     case V4L2_PIX_FMT_RGBX444:  descr = "16-bit RGBX 4-4-4-4"; break;
1276     case V4L2_PIX_FMT_ABGR444:  descr = "16-bit ABGR 4-4-4-4"; break;
1277     case V4L2_PIX_FMT_XBGR444:  descr = "16-bit XBGR 4-4-4-4"; break;
1278     case V4L2_PIX_FMT_BGRA444:  descr = "16-bit BGRA 4-4-4-4"; break;
1279     case V4L2_PIX_FMT_BGRX444:  descr = "16-bit BGRX 4-4-4-4"; break;
1280     case V4L2_PIX_FMT_RGB555:   descr = "16-bit A/XRGB 1-5-5-5"; break;
1281     case V4L2_PIX_FMT_ARGB555:  descr = "16-bit ARGB 1-5-5-5"; break;
1282     case V4L2_PIX_FMT_XRGB555:  descr = "16-bit XRGB 1-5-5-5"; break;
1283     case V4L2_PIX_FMT_ABGR555:  descr = "16-bit ABGR 1-5-5-5"; break;
1284     case V4L2_PIX_FMT_XBGR555:  descr = "16-bit XBGR 1-5-5-5"; break;
1285     case V4L2_PIX_FMT_RGBA555:  descr = "16-bit RGBA 5-5-5-1"; break;
1286     case V4L2_PIX_FMT_RGBX555:  descr = "16-bit RGBX 5-5-5-1"; break;
1287     case V4L2_PIX_FMT_BGRA555:  descr = "16-bit BGRA 5-5-5-1"; break;
1288     case V4L2_PIX_FMT_BGRX555:  descr = "16-bit BGRX 5-5-5-1"; break;
1289     case V4L2_PIX_FMT_RGB565:   descr = "16-bit RGB 5-6-5"; break;
1290     case V4L2_PIX_FMT_RGB555X:  descr = "16-bit A/XRGB 1-5-5-5 BE"; break;
1291     case V4L2_PIX_FMT_ARGB555X: descr = "16-bit ARGB 1-5-5-5 BE"; break;
1292     case V4L2_PIX_FMT_XRGB555X: descr = "16-bit XRGB 1-5-5-5 BE"; break;
1293     case V4L2_PIX_FMT_RGB565X:  descr = "16-bit RGB 5-6-5 BE"; break;
1294     case V4L2_PIX_FMT_BGR666:   descr = "18-bit BGRX 6-6-6-14"; break;
1295     case V4L2_PIX_FMT_BGR24:    descr = "24-bit BGR 8-8-8"; break;
1296     case V4L2_PIX_FMT_RGB24:    descr = "24-bit RGB 8-8-8"; break;
1297     case V4L2_PIX_FMT_BGR32:    descr = "32-bit BGRA/X 8-8-8-8"; break;
1298     case V4L2_PIX_FMT_ABGR32:   descr = "32-bit BGRA 8-8-8-8"; break;
1299     case V4L2_PIX_FMT_XBGR32:   descr = "32-bit BGRX 8-8-8-8"; break;
1300     case V4L2_PIX_FMT_RGB32:    descr = "32-bit A/XRGB 8-8-8-8"; break;
1301     case V4L2_PIX_FMT_ARGB32:   descr = "32-bit ARGB 8-8-8-8"; break;
1302     case V4L2_PIX_FMT_XRGB32:   descr = "32-bit XRGB 8-8-8-8"; break;
1303     case V4L2_PIX_FMT_BGRA32:   descr = "32-bit ABGR 8-8-8-8"; break;
1304     case V4L2_PIX_FMT_BGRX32:   descr = "32-bit XBGR 8-8-8-8"; break;
1305     case V4L2_PIX_FMT_RGBA32:   descr = "32-bit RGBA 8-8-8-8"; break;
1306     case V4L2_PIX_FMT_RGBX32:   descr = "32-bit RGBX 8-8-8-8"; break;
1307     case V4L2_PIX_FMT_GREY:     descr = "8-bit Greyscale"; break;
1308     case V4L2_PIX_FMT_Y4:       descr = "4-bit Greyscale"; break;
1309     case V4L2_PIX_FMT_Y6:       descr = "6-bit Greyscale"; break;
1310     case V4L2_PIX_FMT_Y10:      descr = "10-bit Greyscale"; break;
1311     case V4L2_PIX_FMT_Y12:      descr = "12-bit Greyscale"; break;
1312     case V4L2_PIX_FMT_Y14:      descr = "14-bit Greyscale"; break;
1313     case V4L2_PIX_FMT_Y16:      descr = "16-bit Greyscale"; break;
1314     case V4L2_PIX_FMT_Y16_BE:   descr = "16-bit Greyscale BE"; break;
1315     case V4L2_PIX_FMT_Y10BPACK: descr = "10-bit Greyscale (Packed)"; break;
1316     case V4L2_PIX_FMT_Y10P:     descr = "10-bit Greyscale (MIPI Packed)"; break;
1317     case V4L2_PIX_FMT_IPU3_Y10: descr = "10-bit greyscale (IPU3 Packed)"; break;
1318     case V4L2_PIX_FMT_Y8I:      descr = "Interleaved 8-bit Greyscale"; break;
1319     case V4L2_PIX_FMT_Y12I:     descr = "Interleaved 12-bit Greyscale"; break;
1320     case V4L2_PIX_FMT_Z16:      descr = "16-bit Depth"; break;
1321     case V4L2_PIX_FMT_INZI:     descr = "Planar 10:16 Greyscale Depth"; break;
1322     case V4L2_PIX_FMT_CNF4:     descr = "4-bit Depth Confidence (Packed)"; break;
1323     case V4L2_PIX_FMT_PAL8:     descr = "8-bit Palette"; break;
1324     case V4L2_PIX_FMT_UV8:      descr = "8-bit Chrominance UV 4-4"; break;
1325     case V4L2_PIX_FMT_YVU410:   descr = "Planar YVU 4:1:0"; break;
1326     case V4L2_PIX_FMT_YVU420:   descr = "Planar YVU 4:2:0"; break;
1327     case V4L2_PIX_FMT_YUYV:     descr = "YUYV 4:2:2"; break;
1328     case V4L2_PIX_FMT_YYUV:     descr = "YYUV 4:2:2"; break;
1329     case V4L2_PIX_FMT_YVYU:     descr = "YVYU 4:2:2"; break;
1330     case V4L2_PIX_FMT_UYVY:     descr = "UYVY 4:2:2"; break;
1331     case V4L2_PIX_FMT_VYUY:     descr = "VYUY 4:2:2"; break;
1332     case V4L2_PIX_FMT_YUV422P:  descr = "Planar YUV 4:2:2"; break;
1333     case V4L2_PIX_FMT_YUV411P:  descr = "Planar YUV 4:1:1"; break;
1334     case V4L2_PIX_FMT_Y41P:     descr = "YUV 4:1:1 (Packed)"; break;
1335     case V4L2_PIX_FMT_YUV444:   descr = "16-bit A/XYUV 4-4-4-4"; break;
1336     case V4L2_PIX_FMT_YUV555:   descr = "16-bit A/XYUV 1-5-5-5"; break;
1337     case V4L2_PIX_FMT_YUV565:   descr = "16-bit YUV 5-6-5"; break;
1338     case V4L2_PIX_FMT_YUV24:    descr = "24-bit YUV 4:4:4 8-8-8"; break;
1339     case V4L2_PIX_FMT_YUV32:    descr = "32-bit A/XYUV 8-8-8-8"; break;
1340     case V4L2_PIX_FMT_AYUV32:   descr = "32-bit AYUV 8-8-8-8"; break;
1341     case V4L2_PIX_FMT_XYUV32:   descr = "32-bit XYUV 8-8-8-8"; break;
1342     case V4L2_PIX_FMT_VUYA32:   descr = "32-bit VUYA 8-8-8-8"; break;
1343     case V4L2_PIX_FMT_VUYX32:   descr = "32-bit VUYX 8-8-8-8"; break;
1344     case V4L2_PIX_FMT_YUVA32:   descr = "32-bit YUVA 8-8-8-8"; break;
1345     case V4L2_PIX_FMT_YUVX32:   descr = "32-bit YUVX 8-8-8-8"; break;
1346     case V4L2_PIX_FMT_YUV410:   descr = "Planar YUV 4:1:0"; break;
1347     case V4L2_PIX_FMT_YUV420:   descr = "Planar YUV 4:2:0"; break;
1348     case V4L2_PIX_FMT_HI240:    descr = "8-bit Dithered RGB (BTTV)"; break;
1349     case V4L2_PIX_FMT_M420:     descr = "YUV 4:2:0 (M420)"; break;
1350     case V4L2_PIX_FMT_NV12:     descr = "Y/CbCr 4:2:0"; break;
1351     case V4L2_PIX_FMT_NV21:     descr = "Y/CrCb 4:2:0"; break;
1352     case V4L2_PIX_FMT_NV16:     descr = "Y/CbCr 4:2:2"; break;
1353     case V4L2_PIX_FMT_NV61:     descr = "Y/CrCb 4:2:2"; break;
1354     case V4L2_PIX_FMT_NV24:     descr = "Y/CbCr 4:4:4"; break;
1355     case V4L2_PIX_FMT_NV42:     descr = "Y/CrCb 4:4:4"; break;
1356     case V4L2_PIX_FMT_P010:     descr = "10-bit Y/CbCr 4:2:0"; break;
1357     case V4L2_PIX_FMT_NV12_4L4: descr = "Y/CbCr 4:2:0 (4x4 Linear)"; break;
1358     case V4L2_PIX_FMT_NV12_16L16:   descr = "Y/CbCr 4:2:0 (16x16 Linear)"; break;
1359     case V4L2_PIX_FMT_NV12_32L32:   descr = "Y/CbCr 4:2:0 (32x32 Linear)"; break;
1360     case V4L2_PIX_FMT_P010_4L4: descr = "10-bit Y/CbCr 4:2:0 (4x4 Linear)"; break;
1361     case V4L2_PIX_FMT_NV12M:    descr = "Y/CbCr 4:2:0 (N-C)"; break;
1362     case V4L2_PIX_FMT_NV21M:    descr = "Y/CrCb 4:2:0 (N-C)"; break;
1363     case V4L2_PIX_FMT_NV16M:    descr = "Y/CbCr 4:2:2 (N-C)"; break;
1364     case V4L2_PIX_FMT_NV61M:    descr = "Y/CrCb 4:2:2 (N-C)"; break;
1365     case V4L2_PIX_FMT_NV12MT:   descr = "Y/CbCr 4:2:0 (64x32 MB, N-C)"; break;
1366     case V4L2_PIX_FMT_NV12MT_16X16: descr = "Y/CbCr 4:2:0 (16x16 MB, N-C)"; break;
1367     case V4L2_PIX_FMT_YUV420M:  descr = "Planar YUV 4:2:0 (N-C)"; break;
1368     case V4L2_PIX_FMT_YVU420M:  descr = "Planar YVU 4:2:0 (N-C)"; break;
1369     case V4L2_PIX_FMT_YUV422M:  descr = "Planar YUV 4:2:2 (N-C)"; break;
1370     case V4L2_PIX_FMT_YVU422M:  descr = "Planar YVU 4:2:2 (N-C)"; break;
1371     case V4L2_PIX_FMT_YUV444M:  descr = "Planar YUV 4:4:4 (N-C)"; break;
1372     case V4L2_PIX_FMT_YVU444M:  descr = "Planar YVU 4:4:4 (N-C)"; break;
1373     case V4L2_PIX_FMT_SBGGR8:   descr = "8-bit Bayer BGBG/GRGR"; break;
1374     case V4L2_PIX_FMT_SGBRG8:   descr = "8-bit Bayer GBGB/RGRG"; break;
1375     case V4L2_PIX_FMT_SGRBG8:   descr = "8-bit Bayer GRGR/BGBG"; break;
1376     case V4L2_PIX_FMT_SRGGB8:   descr = "8-bit Bayer RGRG/GBGB"; break;
1377     case V4L2_PIX_FMT_SBGGR10:  descr = "10-bit Bayer BGBG/GRGR"; break;
1378     case V4L2_PIX_FMT_SGBRG10:  descr = "10-bit Bayer GBGB/RGRG"; break;
1379     case V4L2_PIX_FMT_SGRBG10:  descr = "10-bit Bayer GRGR/BGBG"; break;
1380     case V4L2_PIX_FMT_SRGGB10:  descr = "10-bit Bayer RGRG/GBGB"; break;
1381     case V4L2_PIX_FMT_SBGGR10P: descr = "10-bit Bayer BGBG/GRGR Packed"; break;
1382     case V4L2_PIX_FMT_SGBRG10P: descr = "10-bit Bayer GBGB/RGRG Packed"; break;
1383     case V4L2_PIX_FMT_SGRBG10P: descr = "10-bit Bayer GRGR/BGBG Packed"; break;
1384     case V4L2_PIX_FMT_SRGGB10P: descr = "10-bit Bayer RGRG/GBGB Packed"; break;
1385     case V4L2_PIX_FMT_IPU3_SBGGR10: descr = "10-bit bayer BGGR IPU3 Packed"; break;
1386     case V4L2_PIX_FMT_IPU3_SGBRG10: descr = "10-bit bayer GBRG IPU3 Packed"; break;
1387     case V4L2_PIX_FMT_IPU3_SGRBG10: descr = "10-bit bayer GRBG IPU3 Packed"; break;
1388     case V4L2_PIX_FMT_IPU3_SRGGB10: descr = "10-bit bayer RGGB IPU3 Packed"; break;
1389     case V4L2_PIX_FMT_SBGGR10ALAW8: descr = "8-bit Bayer BGBG/GRGR (A-law)"; break;
1390     case V4L2_PIX_FMT_SGBRG10ALAW8: descr = "8-bit Bayer GBGB/RGRG (A-law)"; break;
1391     case V4L2_PIX_FMT_SGRBG10ALAW8: descr = "8-bit Bayer GRGR/BGBG (A-law)"; break;
1392     case V4L2_PIX_FMT_SRGGB10ALAW8: descr = "8-bit Bayer RGRG/GBGB (A-law)"; break;
1393     case V4L2_PIX_FMT_SBGGR10DPCM8: descr = "8-bit Bayer BGBG/GRGR (DPCM)"; break;
1394     case V4L2_PIX_FMT_SGBRG10DPCM8: descr = "8-bit Bayer GBGB/RGRG (DPCM)"; break;
1395     case V4L2_PIX_FMT_SGRBG10DPCM8: descr = "8-bit Bayer GRGR/BGBG (DPCM)"; break;
1396     case V4L2_PIX_FMT_SRGGB10DPCM8: descr = "8-bit Bayer RGRG/GBGB (DPCM)"; break;
1397     case V4L2_PIX_FMT_SBGGR12:  descr = "12-bit Bayer BGBG/GRGR"; break;
1398     case V4L2_PIX_FMT_SGBRG12:  descr = "12-bit Bayer GBGB/RGRG"; break;
1399     case V4L2_PIX_FMT_SGRBG12:  descr = "12-bit Bayer GRGR/BGBG"; break;
1400     case V4L2_PIX_FMT_SRGGB12:  descr = "12-bit Bayer RGRG/GBGB"; break;
1401     case V4L2_PIX_FMT_SBGGR12P: descr = "12-bit Bayer BGBG/GRGR Packed"; break;
1402     case V4L2_PIX_FMT_SGBRG12P: descr = "12-bit Bayer GBGB/RGRG Packed"; break;
1403     case V4L2_PIX_FMT_SGRBG12P: descr = "12-bit Bayer GRGR/BGBG Packed"; break;
1404     case V4L2_PIX_FMT_SRGGB12P: descr = "12-bit Bayer RGRG/GBGB Packed"; break;
1405     case V4L2_PIX_FMT_SBGGR14:  descr = "14-bit Bayer BGBG/GRGR"; break;
1406     case V4L2_PIX_FMT_SGBRG14:  descr = "14-bit Bayer GBGB/RGRG"; break;
1407     case V4L2_PIX_FMT_SGRBG14:  descr = "14-bit Bayer GRGR/BGBG"; break;
1408     case V4L2_PIX_FMT_SRGGB14:  descr = "14-bit Bayer RGRG/GBGB"; break;
1409     case V4L2_PIX_FMT_SBGGR14P: descr = "14-bit Bayer BGBG/GRGR Packed"; break;
1410     case V4L2_PIX_FMT_SGBRG14P: descr = "14-bit Bayer GBGB/RGRG Packed"; break;
1411     case V4L2_PIX_FMT_SGRBG14P: descr = "14-bit Bayer GRGR/BGBG Packed"; break;
1412     case V4L2_PIX_FMT_SRGGB14P: descr = "14-bit Bayer RGRG/GBGB Packed"; break;
1413     case V4L2_PIX_FMT_SBGGR16:  descr = "16-bit Bayer BGBG/GRGR"; break;
1414     case V4L2_PIX_FMT_SGBRG16:  descr = "16-bit Bayer GBGB/RGRG"; break;
1415     case V4L2_PIX_FMT_SGRBG16:  descr = "16-bit Bayer GRGR/BGBG"; break;
1416     case V4L2_PIX_FMT_SRGGB16:  descr = "16-bit Bayer RGRG/GBGB"; break;
1417     case V4L2_PIX_FMT_SN9C20X_I420: descr = "GSPCA SN9C20X I420"; break;
1418     case V4L2_PIX_FMT_SPCA501:  descr = "GSPCA SPCA501"; break;
1419     case V4L2_PIX_FMT_SPCA505:  descr = "GSPCA SPCA505"; break;
1420     case V4L2_PIX_FMT_SPCA508:  descr = "GSPCA SPCA508"; break;
1421     case V4L2_PIX_FMT_STV0680:  descr = "GSPCA STV0680"; break;
1422     case V4L2_PIX_FMT_TM6000:   descr = "A/V + VBI Mux Packet"; break;
1423     case V4L2_PIX_FMT_CIT_YYVYUY:   descr = "GSPCA CIT YYVYUY"; break;
1424     case V4L2_PIX_FMT_KONICA420:    descr = "GSPCA KONICA420"; break;
1425     case V4L2_PIX_FMT_MM21:     descr = "Mediatek 8-bit Block Format"; break;
1426     case V4L2_PIX_FMT_HSV24:    descr = "24-bit HSV 8-8-8"; break;
1427     case V4L2_PIX_FMT_HSV32:    descr = "32-bit XHSV 8-8-8-8"; break;
1428     case V4L2_SDR_FMT_CU8:      descr = "Complex U8"; break;
1429     case V4L2_SDR_FMT_CU16LE:   descr = "Complex U16LE"; break;
1430     case V4L2_SDR_FMT_CS8:      descr = "Complex S8"; break;
1431     case V4L2_SDR_FMT_CS14LE:   descr = "Complex S14LE"; break;
1432     case V4L2_SDR_FMT_RU12LE:   descr = "Real U12LE"; break;
1433     case V4L2_SDR_FMT_PCU16BE:  descr = "Planar Complex U16BE"; break;
1434     case V4L2_SDR_FMT_PCU18BE:  descr = "Planar Complex U18BE"; break;
1435     case V4L2_SDR_FMT_PCU20BE:  descr = "Planar Complex U20BE"; break;
1436     case V4L2_TCH_FMT_DELTA_TD16:   descr = "16-bit Signed Deltas"; break;
1437     case V4L2_TCH_FMT_DELTA_TD08:   descr = "8-bit Signed Deltas"; break;
1438     case V4L2_TCH_FMT_TU16:     descr = "16-bit Unsigned Touch Data"; break;
1439     case V4L2_TCH_FMT_TU08:     descr = "8-bit Unsigned Touch Data"; break;
1440     case V4L2_META_FMT_VSP1_HGO:    descr = "R-Car VSP1 1-D Histogram"; break;
1441     case V4L2_META_FMT_VSP1_HGT:    descr = "R-Car VSP1 2-D Histogram"; break;
1442     case V4L2_META_FMT_UVC:     descr = "UVC Payload Header Metadata"; break;
1443     case V4L2_META_FMT_D4XX:    descr = "Intel D4xx UVC Metadata"; break;
1444     case V4L2_META_FMT_VIVID:       descr = "Vivid Metadata"; break;
1445     case V4L2_META_FMT_RK_ISP1_PARAMS:  descr = "Rockchip ISP1 3A Parameters"; break;
1446     case V4L2_META_FMT_RK_ISP1_STAT_3A: descr = "Rockchip ISP1 3A Statistics"; break;
1447     case V4L2_PIX_FMT_NV12M_8L128:  descr = "NV12M (8x128 Linear)"; break;
1448     case V4L2_PIX_FMT_NV12M_10BE_8L128: descr = "10-bit NV12M (8x128 Linear, BE)"; break;
1449 
1450     default:
1451         /* Compressed formats */
1452         flags = V4L2_FMT_FLAG_COMPRESSED;
1453         switch (fmt->pixelformat) {
1454         /* Max description length mask: descr = "0123456789012345678901234567890" */
1455         case V4L2_PIX_FMT_MJPEG:    descr = "Motion-JPEG"; break;
1456         case V4L2_PIX_FMT_JPEG:     descr = "JFIF JPEG"; break;
1457         case V4L2_PIX_FMT_DV:       descr = "1394"; break;
1458         case V4L2_PIX_FMT_MPEG:     descr = "MPEG-1/2/4"; break;
1459         case V4L2_PIX_FMT_H264:     descr = "H.264"; break;
1460         case V4L2_PIX_FMT_H264_NO_SC:   descr = "H.264 (No Start Codes)"; break;
1461         case V4L2_PIX_FMT_H264_MVC: descr = "H.264 MVC"; break;
1462         case V4L2_PIX_FMT_H264_SLICE:   descr = "H.264 Parsed Slice Data"; break;
1463         case V4L2_PIX_FMT_H263:     descr = "H.263"; break;
1464         case V4L2_PIX_FMT_MPEG1:    descr = "MPEG-1 ES"; break;
1465         case V4L2_PIX_FMT_MPEG2:    descr = "MPEG-2 ES"; break;
1466         case V4L2_PIX_FMT_MPEG2_SLICE:  descr = "MPEG-2 Parsed Slice Data"; break;
1467         case V4L2_PIX_FMT_MPEG4:    descr = "MPEG-4 Part 2 ES"; break;
1468         case V4L2_PIX_FMT_XVID:     descr = "Xvid"; break;
1469         case V4L2_PIX_FMT_VC1_ANNEX_G:  descr = "VC-1 (SMPTE 412M Annex G)"; break;
1470         case V4L2_PIX_FMT_VC1_ANNEX_L:  descr = "VC-1 (SMPTE 412M Annex L)"; break;
1471         case V4L2_PIX_FMT_VP8:      descr = "VP8"; break;
1472         case V4L2_PIX_FMT_VP8_FRAME:    descr = "VP8 Frame"; break;
1473         case V4L2_PIX_FMT_VP9:      descr = "VP9"; break;
1474         case V4L2_PIX_FMT_VP9_FRAME:    descr = "VP9 Frame"; break;
1475         case V4L2_PIX_FMT_HEVC:     descr = "HEVC"; break; /* aka H.265 */
1476         case V4L2_PIX_FMT_HEVC_SLICE:   descr = "HEVC Parsed Slice Data"; break;
1477         case V4L2_PIX_FMT_FWHT:     descr = "FWHT"; break; /* used in vicodec */
1478         case V4L2_PIX_FMT_FWHT_STATELESS:   descr = "FWHT Stateless"; break; /* used in vicodec */
1479         case V4L2_PIX_FMT_CPIA1:    descr = "GSPCA CPiA YUV"; break;
1480         case V4L2_PIX_FMT_WNVA:     descr = "WNVA"; break;
1481         case V4L2_PIX_FMT_SN9C10X:  descr = "GSPCA SN9C10X"; break;
1482         case V4L2_PIX_FMT_PWC1:     descr = "Raw Philips Webcam Type (Old)"; break;
1483         case V4L2_PIX_FMT_PWC2:     descr = "Raw Philips Webcam Type (New)"; break;
1484         case V4L2_PIX_FMT_ET61X251: descr = "GSPCA ET61X251"; break;
1485         case V4L2_PIX_FMT_SPCA561:  descr = "GSPCA SPCA561"; break;
1486         case V4L2_PIX_FMT_PAC207:   descr = "GSPCA PAC207"; break;
1487         case V4L2_PIX_FMT_MR97310A: descr = "GSPCA MR97310A"; break;
1488         case V4L2_PIX_FMT_JL2005BCD:    descr = "GSPCA JL2005BCD"; break;
1489         case V4L2_PIX_FMT_SN9C2028: descr = "GSPCA SN9C2028"; break;
1490         case V4L2_PIX_FMT_SQ905C:   descr = "GSPCA SQ905C"; break;
1491         case V4L2_PIX_FMT_PJPG:     descr = "GSPCA PJPG"; break;
1492         case V4L2_PIX_FMT_OV511:    descr = "GSPCA OV511"; break;
1493         case V4L2_PIX_FMT_OV518:    descr = "GSPCA OV518"; break;
1494         case V4L2_PIX_FMT_JPGL:     descr = "JPEG Lite"; break;
1495         case V4L2_PIX_FMT_SE401:    descr = "GSPCA SE401"; break;
1496         case V4L2_PIX_FMT_S5C_UYVY_JPG: descr = "S5C73MX interleaved UYVY/JPEG"; break;
1497         case V4L2_PIX_FMT_MT21C:    descr = "Mediatek Compressed Format"; break;
1498         case V4L2_PIX_FMT_QC08C:    descr = "QCOM Compressed 8-bit Format"; break;
1499         case V4L2_PIX_FMT_QC10C:    descr = "QCOM Compressed 10-bit Format"; break;
1500         default:
1501             if (fmt->description[0])
1502                 return;
1503             WARN(1, "Unknown pixelformat 0x%08x\n", fmt->pixelformat);
1504             flags = 0;
1505             snprintf(fmt->description, sz, "%p4cc",
1506                  &fmt->pixelformat);
1507             break;
1508         }
1509     }
1510 
1511     if (descr)
1512         WARN_ON(strscpy(fmt->description, descr, sz) < 0);
1513     fmt->flags |= flags;
1514 }
1515 
1516 static int v4l_enum_fmt(const struct v4l2_ioctl_ops *ops,
1517                 struct file *file, void *fh, void *arg)
1518 {
1519     struct video_device *vdev = video_devdata(file);
1520     struct v4l2_fmtdesc *p = arg;
1521     int ret = check_fmt(file, p->type);
1522     u32 mbus_code;
1523     u32 cap_mask;
1524 
1525     if (ret)
1526         return ret;
1527     ret = -EINVAL;
1528 
1529     if (!(vdev->device_caps & V4L2_CAP_IO_MC))
1530         p->mbus_code = 0;
1531 
1532     mbus_code = p->mbus_code;
1533     CLEAR_AFTER_FIELD(p, type);
1534     p->mbus_code = mbus_code;
1535 
1536     switch (p->type) {
1537     case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1538     case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
1539         cap_mask = V4L2_CAP_VIDEO_CAPTURE_MPLANE |
1540                V4L2_CAP_VIDEO_M2M_MPLANE;
1541         if (!!(vdev->device_caps & cap_mask) !=
1542             (p->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE))
1543             break;
1544 
1545         if (unlikely(!ops->vidioc_enum_fmt_vid_cap))
1546             break;
1547         ret = ops->vidioc_enum_fmt_vid_cap(file, fh, arg);
1548         break;
1549     case V4L2_BUF_TYPE_VIDEO_OVERLAY:
1550         if (unlikely(!ops->vidioc_enum_fmt_vid_overlay))
1551             break;
1552         ret = ops->vidioc_enum_fmt_vid_overlay(file, fh, arg);
1553         break;
1554     case V4L2_BUF_TYPE_VIDEO_OUTPUT:
1555     case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
1556         cap_mask = V4L2_CAP_VIDEO_OUTPUT_MPLANE |
1557                V4L2_CAP_VIDEO_M2M_MPLANE;
1558         if (!!(vdev->device_caps & cap_mask) !=
1559             (p->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE))
1560             break;
1561 
1562         if (unlikely(!ops->vidioc_enum_fmt_vid_out))
1563             break;
1564         ret = ops->vidioc_enum_fmt_vid_out(file, fh, arg);
1565         break;
1566     case V4L2_BUF_TYPE_SDR_CAPTURE:
1567         if (unlikely(!ops->vidioc_enum_fmt_sdr_cap))
1568             break;
1569         ret = ops->vidioc_enum_fmt_sdr_cap(file, fh, arg);
1570         break;
1571     case V4L2_BUF_TYPE_SDR_OUTPUT:
1572         if (unlikely(!ops->vidioc_enum_fmt_sdr_out))
1573             break;
1574         ret = ops->vidioc_enum_fmt_sdr_out(file, fh, arg);
1575         break;
1576     case V4L2_BUF_TYPE_META_CAPTURE:
1577         if (unlikely(!ops->vidioc_enum_fmt_meta_cap))
1578             break;
1579         ret = ops->vidioc_enum_fmt_meta_cap(file, fh, arg);
1580         break;
1581     case V4L2_BUF_TYPE_META_OUTPUT:
1582         if (unlikely(!ops->vidioc_enum_fmt_meta_out))
1583             break;
1584         ret = ops->vidioc_enum_fmt_meta_out(file, fh, arg);
1585         break;
1586     }
1587     if (ret == 0)
1588         v4l_fill_fmtdesc(p);
1589     return ret;
1590 }
1591 
1592 static void v4l_pix_format_touch(struct v4l2_pix_format *p)
1593 {
1594     /*
1595      * The v4l2_pix_format structure contains fields that make no sense for
1596      * touch. Set them to default values in this case.
1597      */
1598 
1599     p->field = V4L2_FIELD_NONE;
1600     p->colorspace = V4L2_COLORSPACE_RAW;
1601     p->flags = 0;
1602     p->ycbcr_enc = 0;
1603     p->quantization = 0;
1604     p->xfer_func = 0;
1605 }
1606 
1607 static int v4l_g_fmt(const struct v4l2_ioctl_ops *ops,
1608                 struct file *file, void *fh, void *arg)
1609 {
1610     struct v4l2_format *p = arg;
1611     struct video_device *vfd = video_devdata(file);
1612     int ret = check_fmt(file, p->type);
1613 
1614     if (ret)
1615         return ret;
1616 
1617     /*
1618      * fmt can't be cleared for these overlay types due to the 'clips'
1619      * 'clipcount' and 'bitmap' pointers in struct v4l2_window.
1620      * Those are provided by the user. So handle these two overlay types
1621      * first, and then just do a simple memset for the other types.
1622      */
1623     switch (p->type) {
1624     case V4L2_BUF_TYPE_VIDEO_OVERLAY:
1625     case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY: {
1626         struct v4l2_clip *clips = p->fmt.win.clips;
1627         u32 clipcount = p->fmt.win.clipcount;
1628         void __user *bitmap = p->fmt.win.bitmap;
1629 
1630         memset(&p->fmt, 0, sizeof(p->fmt));
1631         p->fmt.win.clips = clips;
1632         p->fmt.win.clipcount = clipcount;
1633         p->fmt.win.bitmap = bitmap;
1634         break;
1635     }
1636     default:
1637         memset(&p->fmt, 0, sizeof(p->fmt));
1638         break;
1639     }
1640 
1641     switch (p->type) {
1642     case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1643         if (unlikely(!ops->vidioc_g_fmt_vid_cap))
1644             break;
1645         p->fmt.pix.priv = V4L2_PIX_FMT_PRIV_MAGIC;
1646         ret = ops->vidioc_g_fmt_vid_cap(file, fh, arg);
1647         /* just in case the driver zeroed it again */
1648         p->fmt.pix.priv = V4L2_PIX_FMT_PRIV_MAGIC;
1649         if (vfd->vfl_type == VFL_TYPE_TOUCH)
1650             v4l_pix_format_touch(&p->fmt.pix);
1651         return ret;
1652     case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
1653         return ops->vidioc_g_fmt_vid_cap_mplane(file, fh, arg);
1654     case V4L2_BUF_TYPE_VIDEO_OVERLAY:
1655         return ops->vidioc_g_fmt_vid_overlay(file, fh, arg);
1656     case V4L2_BUF_TYPE_VBI_CAPTURE:
1657         return ops->vidioc_g_fmt_vbi_cap(file, fh, arg);
1658     case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
1659         return ops->vidioc_g_fmt_sliced_vbi_cap(file, fh, arg);
1660     case V4L2_BUF_TYPE_VIDEO_OUTPUT:
1661         if (unlikely(!ops->vidioc_g_fmt_vid_out))
1662             break;
1663         p->fmt.pix.priv = V4L2_PIX_FMT_PRIV_MAGIC;
1664         ret = ops->vidioc_g_fmt_vid_out(file, fh, arg);
1665         /* just in case the driver zeroed it again */
1666         p->fmt.pix.priv = V4L2_PIX_FMT_PRIV_MAGIC;
1667         return ret;
1668     case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
1669         return ops->vidioc_g_fmt_vid_out_mplane(file, fh, arg);
1670     case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
1671         return ops->vidioc_g_fmt_vid_out_overlay(file, fh, arg);
1672     case V4L2_BUF_TYPE_VBI_OUTPUT:
1673         return ops->vidioc_g_fmt_vbi_out(file, fh, arg);
1674     case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
1675         return ops->vidioc_g_fmt_sliced_vbi_out(file, fh, arg);
1676     case V4L2_BUF_TYPE_SDR_CAPTURE:
1677         return ops->vidioc_g_fmt_sdr_cap(file, fh, arg);
1678     case V4L2_BUF_TYPE_SDR_OUTPUT:
1679         return ops->vidioc_g_fmt_sdr_out(file, fh, arg);
1680     case V4L2_BUF_TYPE_META_CAPTURE:
1681         return ops->vidioc_g_fmt_meta_cap(file, fh, arg);
1682     case V4L2_BUF_TYPE_META_OUTPUT:
1683         return ops->vidioc_g_fmt_meta_out(file, fh, arg);
1684     }
1685     return -EINVAL;
1686 }
1687 
1688 static int v4l_s_fmt(const struct v4l2_ioctl_ops *ops,
1689                 struct file *file, void *fh, void *arg)
1690 {
1691     struct v4l2_format *p = arg;
1692     struct video_device *vfd = video_devdata(file);
1693     int ret = check_fmt(file, p->type);
1694     unsigned int i;
1695 
1696     if (ret)
1697         return ret;
1698 
1699     ret = v4l_enable_media_source(vfd);
1700     if (ret)
1701         return ret;
1702     v4l_sanitize_format(p);
1703 
1704     switch (p->type) {
1705     case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1706         if (unlikely(!ops->vidioc_s_fmt_vid_cap))
1707             break;
1708         CLEAR_AFTER_FIELD(p, fmt.pix);
1709         ret = ops->vidioc_s_fmt_vid_cap(file, fh, arg);
1710         /* just in case the driver zeroed it again */
1711         p->fmt.pix.priv = V4L2_PIX_FMT_PRIV_MAGIC;
1712         if (vfd->vfl_type == VFL_TYPE_TOUCH)
1713             v4l_pix_format_touch(&p->fmt.pix);
1714         return ret;
1715     case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
1716         if (unlikely(!ops->vidioc_s_fmt_vid_cap_mplane))
1717             break;
1718         CLEAR_AFTER_FIELD(p, fmt.pix_mp.xfer_func);
1719         for (i = 0; i < p->fmt.pix_mp.num_planes; i++)
1720             CLEAR_AFTER_FIELD(&p->fmt.pix_mp.plane_fmt[i],
1721                       bytesperline);
1722         return ops->vidioc_s_fmt_vid_cap_mplane(file, fh, arg);
1723     case V4L2_BUF_TYPE_VIDEO_OVERLAY:
1724         if (unlikely(!ops->vidioc_s_fmt_vid_overlay))
1725             break;
1726         CLEAR_AFTER_FIELD(p, fmt.win);
1727         return ops->vidioc_s_fmt_vid_overlay(file, fh, arg);
1728     case V4L2_BUF_TYPE_VBI_CAPTURE:
1729         if (unlikely(!ops->vidioc_s_fmt_vbi_cap))
1730             break;
1731         CLEAR_AFTER_FIELD(p, fmt.vbi.flags);
1732         return ops->vidioc_s_fmt_vbi_cap(file, fh, arg);
1733     case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
1734         if (unlikely(!ops->vidioc_s_fmt_sliced_vbi_cap))
1735             break;
1736         CLEAR_AFTER_FIELD(p, fmt.sliced.io_size);
1737         return ops->vidioc_s_fmt_sliced_vbi_cap(file, fh, arg);
1738     case V4L2_BUF_TYPE_VIDEO_OUTPUT:
1739         if (unlikely(!ops->vidioc_s_fmt_vid_out))
1740             break;
1741         CLEAR_AFTER_FIELD(p, fmt.pix);
1742         ret = ops->vidioc_s_fmt_vid_out(file, fh, arg);
1743         /* just in case the driver zeroed it again */
1744         p->fmt.pix.priv = V4L2_PIX_FMT_PRIV_MAGIC;
1745         return ret;
1746     case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
1747         if (unlikely(!ops->vidioc_s_fmt_vid_out_mplane))
1748             break;
1749         CLEAR_AFTER_FIELD(p, fmt.pix_mp.xfer_func);
1750         for (i = 0; i < p->fmt.pix_mp.num_planes; i++)
1751             CLEAR_AFTER_FIELD(&p->fmt.pix_mp.plane_fmt[i],
1752                       bytesperline);
1753         return ops->vidioc_s_fmt_vid_out_mplane(file, fh, arg);
1754     case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
1755         if (unlikely(!ops->vidioc_s_fmt_vid_out_overlay))
1756             break;
1757         CLEAR_AFTER_FIELD(p, fmt.win);
1758         return ops->vidioc_s_fmt_vid_out_overlay(file, fh, arg);
1759     case V4L2_BUF_TYPE_VBI_OUTPUT:
1760         if (unlikely(!ops->vidioc_s_fmt_vbi_out))
1761             break;
1762         CLEAR_AFTER_FIELD(p, fmt.vbi.flags);
1763         return ops->vidioc_s_fmt_vbi_out(file, fh, arg);
1764     case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
1765         if (unlikely(!ops->vidioc_s_fmt_sliced_vbi_out))
1766             break;
1767         CLEAR_AFTER_FIELD(p, fmt.sliced.io_size);
1768         return ops->vidioc_s_fmt_sliced_vbi_out(file, fh, arg);
1769     case V4L2_BUF_TYPE_SDR_CAPTURE:
1770         if (unlikely(!ops->vidioc_s_fmt_sdr_cap))
1771             break;
1772         CLEAR_AFTER_FIELD(p, fmt.sdr.buffersize);
1773         return ops->vidioc_s_fmt_sdr_cap(file, fh, arg);
1774     case V4L2_BUF_TYPE_SDR_OUTPUT:
1775         if (unlikely(!ops->vidioc_s_fmt_sdr_out))
1776             break;
1777         CLEAR_AFTER_FIELD(p, fmt.sdr.buffersize);
1778         return ops->vidioc_s_fmt_sdr_out(file, fh, arg);
1779     case V4L2_BUF_TYPE_META_CAPTURE:
1780         if (unlikely(!ops->vidioc_s_fmt_meta_cap))
1781             break;
1782         CLEAR_AFTER_FIELD(p, fmt.meta);
1783         return ops->vidioc_s_fmt_meta_cap(file, fh, arg);
1784     case V4L2_BUF_TYPE_META_OUTPUT:
1785         if (unlikely(!ops->vidioc_s_fmt_meta_out))
1786             break;
1787         CLEAR_AFTER_FIELD(p, fmt.meta);
1788         return ops->vidioc_s_fmt_meta_out(file, fh, arg);
1789     }
1790     return -EINVAL;
1791 }
1792 
1793 static int v4l_try_fmt(const struct v4l2_ioctl_ops *ops,
1794                 struct file *file, void *fh, void *arg)
1795 {
1796     struct v4l2_format *p = arg;
1797     struct video_device *vfd = video_devdata(file);
1798     int ret = check_fmt(file, p->type);
1799     unsigned int i;
1800 
1801     if (ret)
1802         return ret;
1803 
1804     v4l_sanitize_format(p);
1805 
1806     switch (p->type) {
1807     case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1808         if (unlikely(!ops->vidioc_try_fmt_vid_cap))
1809             break;
1810         CLEAR_AFTER_FIELD(p, fmt.pix);
1811         ret = ops->vidioc_try_fmt_vid_cap(file, fh, arg);
1812         /* just in case the driver zeroed it again */
1813         p->fmt.pix.priv = V4L2_PIX_FMT_PRIV_MAGIC;
1814         if (vfd->vfl_type == VFL_TYPE_TOUCH)
1815             v4l_pix_format_touch(&p->fmt.pix);
1816         return ret;
1817     case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
1818         if (unlikely(!ops->vidioc_try_fmt_vid_cap_mplane))
1819             break;
1820         CLEAR_AFTER_FIELD(p, fmt.pix_mp.xfer_func);
1821         for (i = 0; i < p->fmt.pix_mp.num_planes; i++)
1822             CLEAR_AFTER_FIELD(&p->fmt.pix_mp.plane_fmt[i],
1823                       bytesperline);
1824         return ops->vidioc_try_fmt_vid_cap_mplane(file, fh, arg);
1825     case V4L2_BUF_TYPE_VIDEO_OVERLAY:
1826         if (unlikely(!ops->vidioc_try_fmt_vid_overlay))
1827             break;
1828         CLEAR_AFTER_FIELD(p, fmt.win);
1829         return ops->vidioc_try_fmt_vid_overlay(file, fh, arg);
1830     case V4L2_BUF_TYPE_VBI_CAPTURE:
1831         if (unlikely(!ops->vidioc_try_fmt_vbi_cap))
1832             break;
1833         CLEAR_AFTER_FIELD(p, fmt.vbi.flags);
1834         return ops->vidioc_try_fmt_vbi_cap(file, fh, arg);
1835     case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
1836         if (unlikely(!ops->vidioc_try_fmt_sliced_vbi_cap))
1837             break;
1838         CLEAR_AFTER_FIELD(p, fmt.sliced.io_size);
1839         return ops->vidioc_try_fmt_sliced_vbi_cap(file, fh, arg);
1840     case V4L2_BUF_TYPE_VIDEO_OUTPUT:
1841         if (unlikely(!ops->vidioc_try_fmt_vid_out))
1842             break;
1843         CLEAR_AFTER_FIELD(p, fmt.pix);
1844         ret = ops->vidioc_try_fmt_vid_out(file, fh, arg);
1845         /* just in case the driver zeroed it again */
1846         p->fmt.pix.priv = V4L2_PIX_FMT_PRIV_MAGIC;
1847         return ret;
1848     case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
1849         if (unlikely(!ops->vidioc_try_fmt_vid_out_mplane))
1850             break;
1851         CLEAR_AFTER_FIELD(p, fmt.pix_mp.xfer_func);
1852         for (i = 0; i < p->fmt.pix_mp.num_planes; i++)
1853             CLEAR_AFTER_FIELD(&p->fmt.pix_mp.plane_fmt[i],
1854                       bytesperline);
1855         return ops->vidioc_try_fmt_vid_out_mplane(file, fh, arg);
1856     case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
1857         if (unlikely(!ops->vidioc_try_fmt_vid_out_overlay))
1858             break;
1859         CLEAR_AFTER_FIELD(p, fmt.win);
1860         return ops->vidioc_try_fmt_vid_out_overlay(file, fh, arg);
1861     case V4L2_BUF_TYPE_VBI_OUTPUT:
1862         if (unlikely(!ops->vidioc_try_fmt_vbi_out))
1863             break;
1864         CLEAR_AFTER_FIELD(p, fmt.vbi.flags);
1865         return ops->vidioc_try_fmt_vbi_out(file, fh, arg);
1866     case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
1867         if (unlikely(!ops->vidioc_try_fmt_sliced_vbi_out))
1868             break;
1869         CLEAR_AFTER_FIELD(p, fmt.sliced.io_size);
1870         return ops->vidioc_try_fmt_sliced_vbi_out(file, fh, arg);
1871     case V4L2_BUF_TYPE_SDR_CAPTURE:
1872         if (unlikely(!ops->vidioc_try_fmt_sdr_cap))
1873             break;
1874         CLEAR_AFTER_FIELD(p, fmt.sdr.buffersize);
1875         return ops->vidioc_try_fmt_sdr_cap(file, fh, arg);
1876     case V4L2_BUF_TYPE_SDR_OUTPUT:
1877         if (unlikely(!ops->vidioc_try_fmt_sdr_out))
1878             break;
1879         CLEAR_AFTER_FIELD(p, fmt.sdr.buffersize);
1880         return ops->vidioc_try_fmt_sdr_out(file, fh, arg);
1881     case V4L2_BUF_TYPE_META_CAPTURE:
1882         if (unlikely(!ops->vidioc_try_fmt_meta_cap))
1883             break;
1884         CLEAR_AFTER_FIELD(p, fmt.meta);
1885         return ops->vidioc_try_fmt_meta_cap(file, fh, arg);
1886     case V4L2_BUF_TYPE_META_OUTPUT:
1887         if (unlikely(!ops->vidioc_try_fmt_meta_out))
1888             break;
1889         CLEAR_AFTER_FIELD(p, fmt.meta);
1890         return ops->vidioc_try_fmt_meta_out(file, fh, arg);
1891     }
1892     return -EINVAL;
1893 }
1894 
1895 static int v4l_streamon(const struct v4l2_ioctl_ops *ops,
1896                 struct file *file, void *fh, void *arg)
1897 {
1898     return ops->vidioc_streamon(file, fh, *(unsigned int *)arg);
1899 }
1900 
1901 static int v4l_streamoff(const struct v4l2_ioctl_ops *ops,
1902                 struct file *file, void *fh, void *arg)
1903 {
1904     return ops->vidioc_streamoff(file, fh, *(unsigned int *)arg);
1905 }
1906 
1907 static int v4l_g_tuner(const struct v4l2_ioctl_ops *ops,
1908                 struct file *file, void *fh, void *arg)
1909 {
1910     struct video_device *vfd = video_devdata(file);
1911     struct v4l2_tuner *p = arg;
1912     int err;
1913 
1914     p->type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
1915             V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1916     err = ops->vidioc_g_tuner(file, fh, p);
1917     if (!err)
1918         p->capability |= V4L2_TUNER_CAP_FREQ_BANDS;
1919     return err;
1920 }
1921 
1922 static int v4l_s_tuner(const struct v4l2_ioctl_ops *ops,
1923                 struct file *file, void *fh, void *arg)
1924 {
1925     struct video_device *vfd = video_devdata(file);
1926     struct v4l2_tuner *p = arg;
1927     int ret;
1928 
1929     ret = v4l_enable_media_source(vfd);
1930     if (ret)
1931         return ret;
1932     p->type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
1933             V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1934     return ops->vidioc_s_tuner(file, fh, p);
1935 }
1936 
1937 static int v4l_g_modulator(const struct v4l2_ioctl_ops *ops,
1938                 struct file *file, void *fh, void *arg)
1939 {
1940     struct video_device *vfd = video_devdata(file);
1941     struct v4l2_modulator *p = arg;
1942     int err;
1943 
1944     if (vfd->vfl_type == VFL_TYPE_RADIO)
1945         p->type = V4L2_TUNER_RADIO;
1946 
1947     err = ops->vidioc_g_modulator(file, fh, p);
1948     if (!err)
1949         p->capability |= V4L2_TUNER_CAP_FREQ_BANDS;
1950     return err;
1951 }
1952 
1953 static int v4l_s_modulator(const struct v4l2_ioctl_ops *ops,
1954                 struct file *file, void *fh, void *arg)
1955 {
1956     struct video_device *vfd = video_devdata(file);
1957     struct v4l2_modulator *p = arg;
1958 
1959     if (vfd->vfl_type == VFL_TYPE_RADIO)
1960         p->type = V4L2_TUNER_RADIO;
1961 
1962     return ops->vidioc_s_modulator(file, fh, p);
1963 }
1964 
1965 static int v4l_g_frequency(const struct v4l2_ioctl_ops *ops,
1966                 struct file *file, void *fh, void *arg)
1967 {
1968     struct video_device *vfd = video_devdata(file);
1969     struct v4l2_frequency *p = arg;
1970 
1971     if (vfd->vfl_type == VFL_TYPE_SDR)
1972         p->type = V4L2_TUNER_SDR;
1973     else
1974         p->type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
1975                 V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1976     return ops->vidioc_g_frequency(file, fh, p);
1977 }
1978 
1979 static int v4l_s_frequency(const struct v4l2_ioctl_ops *ops,
1980                 struct file *file, void *fh, void *arg)
1981 {
1982     struct video_device *vfd = video_devdata(file);
1983     const struct v4l2_frequency *p = arg;
1984     enum v4l2_tuner_type type;
1985     int ret;
1986 
1987     ret = v4l_enable_media_source(vfd);
1988     if (ret)
1989         return ret;
1990     if (vfd->vfl_type == VFL_TYPE_SDR) {
1991         if (p->type != V4L2_TUNER_SDR && p->type != V4L2_TUNER_RF)
1992             return -EINVAL;
1993     } else {
1994         type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
1995                 V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1996         if (type != p->type)
1997             return -EINVAL;
1998     }
1999     return ops->vidioc_s_frequency(file, fh, p);
2000 }
2001 
2002 static int v4l_enumstd(const struct v4l2_ioctl_ops *ops,
2003                 struct file *file, void *fh, void *arg)
2004 {
2005     struct video_device *vfd = video_devdata(file);
2006     struct v4l2_standard *p = arg;
2007 
2008     return v4l_video_std_enumstd(p, vfd->tvnorms);
2009 }
2010 
2011 static int v4l_s_std(const struct v4l2_ioctl_ops *ops,
2012                 struct file *file, void *fh, void *arg)
2013 {
2014     struct video_device *vfd = video_devdata(file);
2015     v4l2_std_id id = *(v4l2_std_id *)arg, norm;
2016     int ret;
2017 
2018     ret = v4l_enable_media_source(vfd);
2019     if (ret)
2020         return ret;
2021     norm = id & vfd->tvnorms;
2022     if (vfd->tvnorms && !norm)  /* Check if std is supported */
2023         return -EINVAL;
2024 
2025     /* Calls the specific handler */
2026     return ops->vidioc_s_std(file, fh, norm);
2027 }
2028 
2029 static int v4l_querystd(const struct v4l2_ioctl_ops *ops,
2030                 struct file *file, void *fh, void *arg)
2031 {
2032     struct video_device *vfd = video_devdata(file);
2033     v4l2_std_id *p = arg;
2034     int ret;
2035 
2036     ret = v4l_enable_media_source(vfd);
2037     if (ret)
2038         return ret;
2039     /*
2040      * If no signal is detected, then the driver should return
2041      * V4L2_STD_UNKNOWN. Otherwise it should return tvnorms with
2042      * any standards that do not apply removed.
2043      *
2044      * This means that tuners, audio and video decoders can join
2045      * their efforts to improve the standards detection.
2046      */
2047     *p = vfd->tvnorms;
2048     return ops->vidioc_querystd(file, fh, arg);
2049 }
2050 
2051 static int v4l_s_hw_freq_seek(const struct v4l2_ioctl_ops *ops,
2052                 struct file *file, void *fh, void *arg)
2053 {
2054     struct video_device *vfd = video_devdata(file);
2055     struct v4l2_hw_freq_seek *p = arg;
2056     enum v4l2_tuner_type type;
2057     int ret;
2058 
2059     ret = v4l_enable_media_source(vfd);
2060     if (ret)
2061         return ret;
2062     /* s_hw_freq_seek is not supported for SDR for now */
2063     if (vfd->vfl_type == VFL_TYPE_SDR)
2064         return -EINVAL;
2065 
2066     type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
2067         V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
2068     if (p->type != type)
2069         return -EINVAL;
2070     return ops->vidioc_s_hw_freq_seek(file, fh, p);
2071 }
2072 
2073 static int v4l_overlay(const struct v4l2_ioctl_ops *ops,
2074                 struct file *file, void *fh, void *arg)
2075 {
2076     return ops->vidioc_overlay(file, fh, *(unsigned int *)arg);
2077 }
2078 
2079 static int v4l_reqbufs(const struct v4l2_ioctl_ops *ops,
2080                 struct file *file, void *fh, void *arg)
2081 {
2082     struct v4l2_requestbuffers *p = arg;
2083     int ret = check_fmt(file, p->type);
2084 
2085     if (ret)
2086         return ret;
2087 
2088     CLEAR_AFTER_FIELD(p, flags);
2089 
2090     return ops->vidioc_reqbufs(file, fh, p);
2091 }
2092 
2093 static int v4l_querybuf(const struct v4l2_ioctl_ops *ops,
2094                 struct file *file, void *fh, void *arg)
2095 {
2096     struct v4l2_buffer *p = arg;
2097     int ret = check_fmt(file, p->type);
2098 
2099     return ret ? ret : ops->vidioc_querybuf(file, fh, p);
2100 }
2101 
2102 static int v4l_qbuf(const struct v4l2_ioctl_ops *ops,
2103                 struct file *file, void *fh, void *arg)
2104 {
2105     struct v4l2_buffer *p = arg;
2106     int ret = check_fmt(file, p->type);
2107 
2108     return ret ? ret : ops->vidioc_qbuf(file, fh, p);
2109 }
2110 
2111 static int v4l_dqbuf(const struct v4l2_ioctl_ops *ops,
2112                 struct file *file, void *fh, void *arg)
2113 {
2114     struct v4l2_buffer *p = arg;
2115     int ret = check_fmt(file, p->type);
2116 
2117     return ret ? ret : ops->vidioc_dqbuf(file, fh, p);
2118 }
2119 
2120 static int v4l_create_bufs(const struct v4l2_ioctl_ops *ops,
2121                 struct file *file, void *fh, void *arg)
2122 {
2123     struct v4l2_create_buffers *create = arg;
2124     int ret = check_fmt(file, create->format.type);
2125 
2126     if (ret)
2127         return ret;
2128 
2129     CLEAR_AFTER_FIELD(create, flags);
2130 
2131     v4l_sanitize_format(&create->format);
2132 
2133     ret = ops->vidioc_create_bufs(file, fh, create);
2134 
2135     if (create->format.type == V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2136         create->format.type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
2137         create->format.fmt.pix.priv = V4L2_PIX_FMT_PRIV_MAGIC;
2138 
2139     return ret;
2140 }
2141 
2142 static int v4l_prepare_buf(const struct v4l2_ioctl_ops *ops,
2143                 struct file *file, void *fh, void *arg)
2144 {
2145     struct v4l2_buffer *b = arg;
2146     int ret = check_fmt(file, b->type);
2147 
2148     return ret ? ret : ops->vidioc_prepare_buf(file, fh, b);
2149 }
2150 
2151 static int v4l_g_parm(const struct v4l2_ioctl_ops *ops,
2152                 struct file *file, void *fh, void *arg)
2153 {
2154     struct video_device *vfd = video_devdata(file);
2155     struct v4l2_streamparm *p = arg;
2156     v4l2_std_id std;
2157     int ret = check_fmt(file, p->type);
2158 
2159     if (ret)
2160         return ret;
2161     if (ops->vidioc_g_parm)
2162         return ops->vidioc_g_parm(file, fh, p);
2163     if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
2164         p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
2165         return -EINVAL;
2166     if (vfd->device_caps & V4L2_CAP_READWRITE)
2167         p->parm.capture.readbuffers = 2;
2168     ret = ops->vidioc_g_std(file, fh, &std);
2169     if (ret == 0)
2170         v4l2_video_std_frame_period(std, &p->parm.capture.timeperframe);
2171     return ret;
2172 }
2173 
2174 static int v4l_s_parm(const struct v4l2_ioctl_ops *ops,
2175                 struct file *file, void *fh, void *arg)
2176 {
2177     struct v4l2_streamparm *p = arg;
2178     int ret = check_fmt(file, p->type);
2179 
2180     if (ret)
2181         return ret;
2182 
2183     /* Note: extendedmode is never used in drivers */
2184     if (V4L2_TYPE_IS_OUTPUT(p->type)) {
2185         memset(p->parm.output.reserved, 0,
2186                sizeof(p->parm.output.reserved));
2187         p->parm.output.extendedmode = 0;
2188         p->parm.output.outputmode &= V4L2_MODE_HIGHQUALITY;
2189     } else {
2190         memset(p->parm.capture.reserved, 0,
2191                sizeof(p->parm.capture.reserved));
2192         p->parm.capture.extendedmode = 0;
2193         p->parm.capture.capturemode &= V4L2_MODE_HIGHQUALITY;
2194     }
2195     return ops->vidioc_s_parm(file, fh, p);
2196 }
2197 
2198 static int v4l_queryctrl(const struct v4l2_ioctl_ops *ops,
2199                 struct file *file, void *fh, void *arg)
2200 {
2201     struct video_device *vfd = video_devdata(file);
2202     struct v4l2_queryctrl *p = arg;
2203     struct v4l2_fh *vfh =
2204         test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
2205 
2206     if (vfh && vfh->ctrl_handler)
2207         return v4l2_queryctrl(vfh->ctrl_handler, p);
2208     if (vfd->ctrl_handler)
2209         return v4l2_queryctrl(vfd->ctrl_handler, p);
2210     if (ops->vidioc_queryctrl)
2211         return ops->vidioc_queryctrl(file, fh, p);
2212     return -ENOTTY;
2213 }
2214 
2215 static int v4l_query_ext_ctrl(const struct v4l2_ioctl_ops *ops,
2216                 struct file *file, void *fh, void *arg)
2217 {
2218     struct video_device *vfd = video_devdata(file);
2219     struct v4l2_query_ext_ctrl *p = arg;
2220     struct v4l2_fh *vfh =
2221         test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
2222 
2223     if (vfh && vfh->ctrl_handler)
2224         return v4l2_query_ext_ctrl(vfh->ctrl_handler, p);
2225     if (vfd->ctrl_handler)
2226         return v4l2_query_ext_ctrl(vfd->ctrl_handler, p);
2227     if (ops->vidioc_query_ext_ctrl)
2228         return ops->vidioc_query_ext_ctrl(file, fh, p);
2229     return -ENOTTY;
2230 }
2231 
2232 static int v4l_querymenu(const struct v4l2_ioctl_ops *ops,
2233                 struct file *file, void *fh, void *arg)
2234 {
2235     struct video_device *vfd = video_devdata(file);
2236     struct v4l2_querymenu *p = arg;
2237     struct v4l2_fh *vfh =
2238         test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
2239 
2240     if (vfh && vfh->ctrl_handler)
2241         return v4l2_querymenu(vfh->ctrl_handler, p);
2242     if (vfd->ctrl_handler)
2243         return v4l2_querymenu(vfd->ctrl_handler, p);
2244     if (ops->vidioc_querymenu)
2245         return ops->vidioc_querymenu(file, fh, p);
2246     return -ENOTTY;
2247 }
2248 
2249 static int v4l_g_ctrl(const struct v4l2_ioctl_ops *ops,
2250                 struct file *file, void *fh, void *arg)
2251 {
2252     struct video_device *vfd = video_devdata(file);
2253     struct v4l2_control *p = arg;
2254     struct v4l2_fh *vfh =
2255         test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
2256     struct v4l2_ext_controls ctrls;
2257     struct v4l2_ext_control ctrl;
2258 
2259     if (vfh && vfh->ctrl_handler)
2260         return v4l2_g_ctrl(vfh->ctrl_handler, p);
2261     if (vfd->ctrl_handler)
2262         return v4l2_g_ctrl(vfd->ctrl_handler, p);
2263     if (ops->vidioc_g_ctrl)
2264         return ops->vidioc_g_ctrl(file, fh, p);
2265     if (ops->vidioc_g_ext_ctrls == NULL)
2266         return -ENOTTY;
2267 
2268     ctrls.which = V4L2_CTRL_ID2WHICH(p->id);
2269     ctrls.count = 1;
2270     ctrls.controls = &ctrl;
2271     ctrl.id = p->id;
2272     ctrl.value = p->value;
2273     if (check_ext_ctrls(&ctrls, VIDIOC_G_CTRL)) {
2274         int ret = ops->vidioc_g_ext_ctrls(file, fh, &ctrls);
2275 
2276         if (ret == 0)
2277             p->value = ctrl.value;
2278         return ret;
2279     }
2280     return -EINVAL;
2281 }
2282 
2283 static int v4l_s_ctrl(const struct v4l2_ioctl_ops *ops,
2284                 struct file *file, void *fh, void *arg)
2285 {
2286     struct video_device *vfd = video_devdata(file);
2287     struct v4l2_control *p = arg;
2288     struct v4l2_fh *vfh =
2289         test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
2290     struct v4l2_ext_controls ctrls;
2291     struct v4l2_ext_control ctrl;
2292     int ret;
2293 
2294     if (vfh && vfh->ctrl_handler)
2295         return v4l2_s_ctrl(vfh, vfh->ctrl_handler, p);
2296     if (vfd->ctrl_handler)
2297         return v4l2_s_ctrl(NULL, vfd->ctrl_handler, p);
2298     if (ops->vidioc_s_ctrl)
2299         return ops->vidioc_s_ctrl(file, fh, p);
2300     if (ops->vidioc_s_ext_ctrls == NULL)
2301         return -ENOTTY;
2302 
2303     ctrls.which = V4L2_CTRL_ID2WHICH(p->id);
2304     ctrls.count = 1;
2305     ctrls.controls = &ctrl;
2306     ctrl.id = p->id;
2307     ctrl.value = p->value;
2308     if (!check_ext_ctrls(&ctrls, VIDIOC_S_CTRL))
2309         return -EINVAL;
2310     ret = ops->vidioc_s_ext_ctrls(file, fh, &ctrls);
2311     p->value = ctrl.value;
2312     return ret;
2313 }
2314 
2315 static int v4l_g_ext_ctrls(const struct v4l2_ioctl_ops *ops,
2316                 struct file *file, void *fh, void *arg)
2317 {
2318     struct video_device *vfd = video_devdata(file);
2319     struct v4l2_ext_controls *p = arg;
2320     struct v4l2_fh *vfh =
2321         test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
2322 
2323     p->error_idx = p->count;
2324     if (vfh && vfh->ctrl_handler)
2325         return v4l2_g_ext_ctrls(vfh->ctrl_handler,
2326                     vfd, vfd->v4l2_dev->mdev, p);
2327     if (vfd->ctrl_handler)
2328         return v4l2_g_ext_ctrls(vfd->ctrl_handler,
2329                     vfd, vfd->v4l2_dev->mdev, p);
2330     if (ops->vidioc_g_ext_ctrls == NULL)
2331         return -ENOTTY;
2332     return check_ext_ctrls(p, VIDIOC_G_EXT_CTRLS) ?
2333                 ops->vidioc_g_ext_ctrls(file, fh, p) : -EINVAL;
2334 }
2335 
2336 static int v4l_s_ext_ctrls(const struct v4l2_ioctl_ops *ops,
2337                 struct file *file, void *fh, void *arg)
2338 {
2339     struct video_device *vfd = video_devdata(file);
2340     struct v4l2_ext_controls *p = arg;
2341     struct v4l2_fh *vfh =
2342         test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
2343 
2344     p->error_idx = p->count;
2345     if (vfh && vfh->ctrl_handler)
2346         return v4l2_s_ext_ctrls(vfh, vfh->ctrl_handler,
2347                     vfd, vfd->v4l2_dev->mdev, p);
2348     if (vfd->ctrl_handler)
2349         return v4l2_s_ext_ctrls(NULL, vfd->ctrl_handler,
2350                     vfd, vfd->v4l2_dev->mdev, p);
2351     if (ops->vidioc_s_ext_ctrls == NULL)
2352         return -ENOTTY;
2353     return check_ext_ctrls(p, VIDIOC_S_EXT_CTRLS) ?
2354                 ops->vidioc_s_ext_ctrls(file, fh, p) : -EINVAL;
2355 }
2356 
2357 static int v4l_try_ext_ctrls(const struct v4l2_ioctl_ops *ops,
2358                 struct file *file, void *fh, void *arg)
2359 {
2360     struct video_device *vfd = video_devdata(file);
2361     struct v4l2_ext_controls *p = arg;
2362     struct v4l2_fh *vfh =
2363         test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
2364 
2365     p->error_idx = p->count;
2366     if (vfh && vfh->ctrl_handler)
2367         return v4l2_try_ext_ctrls(vfh->ctrl_handler,
2368                       vfd, vfd->v4l2_dev->mdev, p);
2369     if (vfd->ctrl_handler)
2370         return v4l2_try_ext_ctrls(vfd->ctrl_handler,
2371                       vfd, vfd->v4l2_dev->mdev, p);
2372     if (ops->vidioc_try_ext_ctrls == NULL)
2373         return -ENOTTY;
2374     return check_ext_ctrls(p, VIDIOC_TRY_EXT_CTRLS) ?
2375             ops->vidioc_try_ext_ctrls(file, fh, p) : -EINVAL;
2376 }
2377 
2378 /*
2379  * The selection API specified originally that the _MPLANE buffer types
2380  * shouldn't be used. The reasons for this are lost in the mists of time
2381  * (or just really crappy memories). Regardless, this is really annoying
2382  * for userspace. So to keep things simple we map _MPLANE buffer types
2383  * to their 'regular' counterparts before calling the driver. And we
2384  * restore it afterwards. This way applications can use either buffer
2385  * type and drivers don't need to check for both.
2386  */
2387 static int v4l_g_selection(const struct v4l2_ioctl_ops *ops,
2388                struct file *file, void *fh, void *arg)
2389 {
2390     struct v4l2_selection *p = arg;
2391     u32 old_type = p->type;
2392     int ret;
2393 
2394     if (p->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
2395         p->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2396     else if (p->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
2397         p->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
2398     ret = ops->vidioc_g_selection(file, fh, p);
2399     p->type = old_type;
2400     return ret;
2401 }
2402 
2403 static int v4l_s_selection(const struct v4l2_ioctl_ops *ops,
2404                struct file *file, void *fh, void *arg)
2405 {
2406     struct v4l2_selection *p = arg;
2407     u32 old_type = p->type;
2408     int ret;
2409 
2410     if (p->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
2411         p->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2412     else if (p->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
2413         p->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
2414     ret = ops->vidioc_s_selection(file, fh, p);
2415     p->type = old_type;
2416     return ret;
2417 }
2418 
2419 static int v4l_g_crop(const struct v4l2_ioctl_ops *ops,
2420                 struct file *file, void *fh, void *arg)
2421 {
2422     struct video_device *vfd = video_devdata(file);
2423     struct v4l2_crop *p = arg;
2424     struct v4l2_selection s = {
2425         .type = p->type,
2426     };
2427     int ret;
2428 
2429     /* simulate capture crop using selection api */
2430 
2431     /* crop means compose for output devices */
2432     if (V4L2_TYPE_IS_OUTPUT(p->type))
2433         s.target = V4L2_SEL_TGT_COMPOSE;
2434     else
2435         s.target = V4L2_SEL_TGT_CROP;
2436 
2437     if (test_bit(V4L2_FL_QUIRK_INVERTED_CROP, &vfd->flags))
2438         s.target = s.target == V4L2_SEL_TGT_COMPOSE ?
2439             V4L2_SEL_TGT_CROP : V4L2_SEL_TGT_COMPOSE;
2440 
2441     ret = v4l_g_selection(ops, file, fh, &s);
2442 
2443     /* copying results to old structure on success */
2444     if (!ret)
2445         p->c = s.r;
2446     return ret;
2447 }
2448 
2449 static int v4l_s_crop(const struct v4l2_ioctl_ops *ops,
2450                 struct file *file, void *fh, void *arg)
2451 {
2452     struct video_device *vfd = video_devdata(file);
2453     struct v4l2_crop *p = arg;
2454     struct v4l2_selection s = {
2455         .type = p->type,
2456         .r = p->c,
2457     };
2458 
2459     /* simulate capture crop using selection api */
2460 
2461     /* crop means compose for output devices */
2462     if (V4L2_TYPE_IS_OUTPUT(p->type))
2463         s.target = V4L2_SEL_TGT_COMPOSE;
2464     else
2465         s.target = V4L2_SEL_TGT_CROP;
2466 
2467     if (test_bit(V4L2_FL_QUIRK_INVERTED_CROP, &vfd->flags))
2468         s.target = s.target == V4L2_SEL_TGT_COMPOSE ?
2469             V4L2_SEL_TGT_CROP : V4L2_SEL_TGT_COMPOSE;
2470 
2471     return v4l_s_selection(ops, file, fh, &s);
2472 }
2473 
2474 static int v4l_cropcap(const struct v4l2_ioctl_ops *ops,
2475                 struct file *file, void *fh, void *arg)
2476 {
2477     struct video_device *vfd = video_devdata(file);
2478     struct v4l2_cropcap *p = arg;
2479     struct v4l2_selection s = { .type = p->type };
2480     int ret = 0;
2481 
2482     /* setting trivial pixelaspect */
2483     p->pixelaspect.numerator = 1;
2484     p->pixelaspect.denominator = 1;
2485 
2486     if (s.type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
2487         s.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2488     else if (s.type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
2489         s.type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
2490 
2491     /*
2492      * The determine_valid_ioctls() call already should ensure
2493      * that this can never happen, but just in case...
2494      */
2495     if (WARN_ON(!ops->vidioc_g_selection))
2496         return -ENOTTY;
2497 
2498     if (ops->vidioc_g_pixelaspect)
2499         ret = ops->vidioc_g_pixelaspect(file, fh, s.type,
2500                         &p->pixelaspect);
2501 
2502     /*
2503      * Ignore ENOTTY or ENOIOCTLCMD error returns, just use the
2504      * square pixel aspect ratio in that case.
2505      */
2506     if (ret && ret != -ENOTTY && ret != -ENOIOCTLCMD)
2507         return ret;
2508 
2509     /* Use g_selection() to fill in the bounds and defrect rectangles */
2510 
2511     /* obtaining bounds */
2512     if (V4L2_TYPE_IS_OUTPUT(p->type))
2513         s.target = V4L2_SEL_TGT_COMPOSE_BOUNDS;
2514     else
2515         s.target = V4L2_SEL_TGT_CROP_BOUNDS;
2516 
2517     if (test_bit(V4L2_FL_QUIRK_INVERTED_CROP, &vfd->flags))
2518         s.target = s.target == V4L2_SEL_TGT_COMPOSE_BOUNDS ?
2519             V4L2_SEL_TGT_CROP_BOUNDS : V4L2_SEL_TGT_COMPOSE_BOUNDS;
2520 
2521     ret = v4l_g_selection(ops, file, fh, &s);
2522     if (ret)
2523         return ret;
2524     p->bounds = s.r;
2525 
2526     /* obtaining defrect */
2527     if (s.target == V4L2_SEL_TGT_COMPOSE_BOUNDS)
2528         s.target = V4L2_SEL_TGT_COMPOSE_DEFAULT;
2529     else
2530         s.target = V4L2_SEL_TGT_CROP_DEFAULT;
2531 
2532     ret = v4l_g_selection(ops, file, fh, &s);
2533     if (ret)
2534         return ret;
2535     p->defrect = s.r;
2536 
2537     return 0;
2538 }
2539 
2540 static int v4l_log_status(const struct v4l2_ioctl_ops *ops,
2541                 struct file *file, void *fh, void *arg)
2542 {
2543     struct video_device *vfd = video_devdata(file);
2544     int ret;
2545 
2546     if (vfd->v4l2_dev)
2547         pr_info("%s: =================  START STATUS  =================\n",
2548             vfd->v4l2_dev->name);
2549     ret = ops->vidioc_log_status(file, fh);
2550     if (vfd->v4l2_dev)
2551         pr_info("%s: ==================  END STATUS  ==================\n",
2552             vfd->v4l2_dev->name);
2553     return ret;
2554 }
2555 
2556 static int v4l_dbg_g_register(const struct v4l2_ioctl_ops *ops,
2557                 struct file *file, void *fh, void *arg)
2558 {
2559 #ifdef CONFIG_VIDEO_ADV_DEBUG
2560     struct v4l2_dbg_register *p = arg;
2561     struct video_device *vfd = video_devdata(file);
2562     struct v4l2_subdev *sd;
2563     int idx = 0;
2564 
2565     if (!capable(CAP_SYS_ADMIN))
2566         return -EPERM;
2567     if (p->match.type == V4L2_CHIP_MATCH_SUBDEV) {
2568         if (vfd->v4l2_dev == NULL)
2569             return -EINVAL;
2570         v4l2_device_for_each_subdev(sd, vfd->v4l2_dev)
2571             if (p->match.addr == idx++)
2572                 return v4l2_subdev_call(sd, core, g_register, p);
2573         return -EINVAL;
2574     }
2575     if (ops->vidioc_g_register && p->match.type == V4L2_CHIP_MATCH_BRIDGE &&
2576         (ops->vidioc_g_chip_info || p->match.addr == 0))
2577         return ops->vidioc_g_register(file, fh, p);
2578     return -EINVAL;
2579 #else
2580     return -ENOTTY;
2581 #endif
2582 }
2583 
2584 static int v4l_dbg_s_register(const struct v4l2_ioctl_ops *ops,
2585                 struct file *file, void *fh, void *arg)
2586 {
2587 #ifdef CONFIG_VIDEO_ADV_DEBUG
2588     const struct v4l2_dbg_register *p = arg;
2589     struct video_device *vfd = video_devdata(file);
2590     struct v4l2_subdev *sd;
2591     int idx = 0;
2592 
2593     if (!capable(CAP_SYS_ADMIN))
2594         return -EPERM;
2595     if (p->match.type == V4L2_CHIP_MATCH_SUBDEV) {
2596         if (vfd->v4l2_dev == NULL)
2597             return -EINVAL;
2598         v4l2_device_for_each_subdev(sd, vfd->v4l2_dev)
2599             if (p->match.addr == idx++)
2600                 return v4l2_subdev_call(sd, core, s_register, p);
2601         return -EINVAL;
2602     }
2603     if (ops->vidioc_s_register && p->match.type == V4L2_CHIP_MATCH_BRIDGE &&
2604         (ops->vidioc_g_chip_info || p->match.addr == 0))
2605         return ops->vidioc_s_register(file, fh, p);
2606     return -EINVAL;
2607 #else
2608     return -ENOTTY;
2609 #endif
2610 }
2611 
2612 static int v4l_dbg_g_chip_info(const struct v4l2_ioctl_ops *ops,
2613                 struct file *file, void *fh, void *arg)
2614 {
2615 #ifdef CONFIG_VIDEO_ADV_DEBUG
2616     struct video_device *vfd = video_devdata(file);
2617     struct v4l2_dbg_chip_info *p = arg;
2618     struct v4l2_subdev *sd;
2619     int idx = 0;
2620 
2621     switch (p->match.type) {
2622     case V4L2_CHIP_MATCH_BRIDGE:
2623         if (ops->vidioc_s_register)
2624             p->flags |= V4L2_CHIP_FL_WRITABLE;
2625         if (ops->vidioc_g_register)
2626             p->flags |= V4L2_CHIP_FL_READABLE;
2627         strscpy(p->name, vfd->v4l2_dev->name, sizeof(p->name));
2628         if (ops->vidioc_g_chip_info)
2629             return ops->vidioc_g_chip_info(file, fh, arg);
2630         if (p->match.addr)
2631             return -EINVAL;
2632         return 0;
2633 
2634     case V4L2_CHIP_MATCH_SUBDEV:
2635         if (vfd->v4l2_dev == NULL)
2636             break;
2637         v4l2_device_for_each_subdev(sd, vfd->v4l2_dev) {
2638             if (p->match.addr != idx++)
2639                 continue;
2640             if (sd->ops->core && sd->ops->core->s_register)
2641                 p->flags |= V4L2_CHIP_FL_WRITABLE;
2642             if (sd->ops->core && sd->ops->core->g_register)
2643                 p->flags |= V4L2_CHIP_FL_READABLE;
2644             strscpy(p->name, sd->name, sizeof(p->name));
2645             return 0;
2646         }
2647         break;
2648     }
2649     return -EINVAL;
2650 #else
2651     return -ENOTTY;
2652 #endif
2653 }
2654 
2655 static int v4l_dqevent(const struct v4l2_ioctl_ops *ops,
2656                 struct file *file, void *fh, void *arg)
2657 {
2658     return v4l2_event_dequeue(fh, arg, file->f_flags & O_NONBLOCK);
2659 }
2660 
2661 static int v4l_subscribe_event(const struct v4l2_ioctl_ops *ops,
2662                 struct file *file, void *fh, void *arg)
2663 {
2664     return ops->vidioc_subscribe_event(fh, arg);
2665 }
2666 
2667 static int v4l_unsubscribe_event(const struct v4l2_ioctl_ops *ops,
2668                 struct file *file, void *fh, void *arg)
2669 {
2670     return ops->vidioc_unsubscribe_event(fh, arg);
2671 }
2672 
2673 static int v4l_g_sliced_vbi_cap(const struct v4l2_ioctl_ops *ops,
2674                 struct file *file, void *fh, void *arg)
2675 {
2676     struct v4l2_sliced_vbi_cap *p = arg;
2677     int ret = check_fmt(file, p->type);
2678 
2679     if (ret)
2680         return ret;
2681 
2682     /* Clear up to type, everything after type is zeroed already */
2683     memset(p, 0, offsetof(struct v4l2_sliced_vbi_cap, type));
2684 
2685     return ops->vidioc_g_sliced_vbi_cap(file, fh, p);
2686 }
2687 
2688 static int v4l_enum_freq_bands(const struct v4l2_ioctl_ops *ops,
2689                 struct file *file, void *fh, void *arg)
2690 {
2691     struct video_device *vfd = video_devdata(file);
2692     struct v4l2_frequency_band *p = arg;
2693     enum v4l2_tuner_type type;
2694     int err;
2695 
2696     if (vfd->vfl_type == VFL_TYPE_SDR) {
2697         if (p->type != V4L2_TUNER_SDR && p->type != V4L2_TUNER_RF)
2698             return -EINVAL;
2699         type = p->type;
2700     } else {
2701         type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
2702                 V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
2703         if (type != p->type)
2704             return -EINVAL;
2705     }
2706     if (ops->vidioc_enum_freq_bands) {
2707         err = ops->vidioc_enum_freq_bands(file, fh, p);
2708         if (err != -ENOTTY)
2709             return err;
2710     }
2711     if (is_valid_ioctl(vfd, VIDIOC_G_TUNER)) {
2712         struct v4l2_tuner t = {
2713             .index = p->tuner,
2714             .type = type,
2715         };
2716 
2717         if (p->index)
2718             return -EINVAL;
2719         err = ops->vidioc_g_tuner(file, fh, &t);
2720         if (err)
2721             return err;
2722         p->capability = t.capability | V4L2_TUNER_CAP_FREQ_BANDS;
2723         p->rangelow = t.rangelow;
2724         p->rangehigh = t.rangehigh;
2725         p->modulation = (type == V4L2_TUNER_RADIO) ?
2726             V4L2_BAND_MODULATION_FM : V4L2_BAND_MODULATION_VSB;
2727         return 0;
2728     }
2729     if (is_valid_ioctl(vfd, VIDIOC_G_MODULATOR)) {
2730         struct v4l2_modulator m = {
2731             .index = p->tuner,
2732         };
2733 
2734         if (type != V4L2_TUNER_RADIO)
2735             return -EINVAL;
2736         if (p->index)
2737             return -EINVAL;
2738         err = ops->vidioc_g_modulator(file, fh, &m);
2739         if (err)
2740             return err;
2741         p->capability = m.capability | V4L2_TUNER_CAP_FREQ_BANDS;
2742         p->rangelow = m.rangelow;
2743         p->rangehigh = m.rangehigh;
2744         p->modulation = (type == V4L2_TUNER_RADIO) ?
2745             V4L2_BAND_MODULATION_FM : V4L2_BAND_MODULATION_VSB;
2746         return 0;
2747     }
2748     return -ENOTTY;
2749 }
2750 
2751 struct v4l2_ioctl_info {
2752     unsigned int ioctl;
2753     u32 flags;
2754     const char * const name;
2755     int (*func)(const struct v4l2_ioctl_ops *ops, struct file *file,
2756             void *fh, void *p);
2757     void (*debug)(const void *arg, bool write_only);
2758 };
2759 
2760 /* This control needs a priority check */
2761 #define INFO_FL_PRIO        (1 << 0)
2762 /* This control can be valid if the filehandle passes a control handler. */
2763 #define INFO_FL_CTRL        (1 << 1)
2764 /* Queuing ioctl */
2765 #define INFO_FL_QUEUE       (1 << 2)
2766 /* Always copy back result, even on error */
2767 #define INFO_FL_ALWAYS_COPY (1 << 3)
2768 /* Zero struct from after the field to the end */
2769 #define INFO_FL_CLEAR(v4l2_struct, field)           \
2770     ((offsetof(struct v4l2_struct, field) +         \
2771       sizeof_field(struct v4l2_struct, field)) << 16)
2772 #define INFO_FL_CLEAR_MASK  (_IOC_SIZEMASK << 16)
2773 
2774 #define DEFINE_V4L_STUB_FUNC(_vidioc)               \
2775     static int v4l_stub_ ## _vidioc(            \
2776             const struct v4l2_ioctl_ops *ops,   \
2777             struct file *file, void *fh, void *p)   \
2778     {                           \
2779         return ops->vidioc_ ## _vidioc(file, fh, p);    \
2780     }
2781 
2782 #define IOCTL_INFO(_ioctl, _func, _debug, _flags)       \
2783     [_IOC_NR(_ioctl)] = {                   \
2784         .ioctl = _ioctl,                \
2785         .flags = _flags,                \
2786         .name = #_ioctl,                \
2787         .func = _func,                  \
2788         .debug = _debug,                \
2789     }
2790 
2791 DEFINE_V4L_STUB_FUNC(g_fbuf)
2792 DEFINE_V4L_STUB_FUNC(s_fbuf)
2793 DEFINE_V4L_STUB_FUNC(expbuf)
2794 DEFINE_V4L_STUB_FUNC(g_std)
2795 DEFINE_V4L_STUB_FUNC(g_audio)
2796 DEFINE_V4L_STUB_FUNC(s_audio)
2797 DEFINE_V4L_STUB_FUNC(g_edid)
2798 DEFINE_V4L_STUB_FUNC(s_edid)
2799 DEFINE_V4L_STUB_FUNC(g_audout)
2800 DEFINE_V4L_STUB_FUNC(s_audout)
2801 DEFINE_V4L_STUB_FUNC(g_jpegcomp)
2802 DEFINE_V4L_STUB_FUNC(s_jpegcomp)
2803 DEFINE_V4L_STUB_FUNC(enumaudio)
2804 DEFINE_V4L_STUB_FUNC(enumaudout)
2805 DEFINE_V4L_STUB_FUNC(enum_framesizes)
2806 DEFINE_V4L_STUB_FUNC(enum_frameintervals)
2807 DEFINE_V4L_STUB_FUNC(g_enc_index)
2808 DEFINE_V4L_STUB_FUNC(encoder_cmd)
2809 DEFINE_V4L_STUB_FUNC(try_encoder_cmd)
2810 DEFINE_V4L_STUB_FUNC(decoder_cmd)
2811 DEFINE_V4L_STUB_FUNC(try_decoder_cmd)
2812 DEFINE_V4L_STUB_FUNC(s_dv_timings)
2813 DEFINE_V4L_STUB_FUNC(g_dv_timings)
2814 DEFINE_V4L_STUB_FUNC(enum_dv_timings)
2815 DEFINE_V4L_STUB_FUNC(query_dv_timings)
2816 DEFINE_V4L_STUB_FUNC(dv_timings_cap)
2817 
2818 static const struct v4l2_ioctl_info v4l2_ioctls[] = {
2819     IOCTL_INFO(VIDIOC_QUERYCAP, v4l_querycap, v4l_print_querycap, 0),
2820     IOCTL_INFO(VIDIOC_ENUM_FMT, v4l_enum_fmt, v4l_print_fmtdesc, 0),
2821     IOCTL_INFO(VIDIOC_G_FMT, v4l_g_fmt, v4l_print_format, 0),
2822     IOCTL_INFO(VIDIOC_S_FMT, v4l_s_fmt, v4l_print_format, INFO_FL_PRIO),
2823     IOCTL_INFO(VIDIOC_REQBUFS, v4l_reqbufs, v4l_print_requestbuffers, INFO_FL_PRIO | INFO_FL_QUEUE),
2824     IOCTL_INFO(VIDIOC_QUERYBUF, v4l_querybuf, v4l_print_buffer, INFO_FL_QUEUE | INFO_FL_CLEAR(v4l2_buffer, length)),
2825     IOCTL_INFO(VIDIOC_G_FBUF, v4l_stub_g_fbuf, v4l_print_framebuffer, 0),
2826     IOCTL_INFO(VIDIOC_S_FBUF, v4l_stub_s_fbuf, v4l_print_framebuffer, INFO_FL_PRIO),
2827     IOCTL_INFO(VIDIOC_OVERLAY, v4l_overlay, v4l_print_u32, INFO_FL_PRIO),
2828     IOCTL_INFO(VIDIOC_QBUF, v4l_qbuf, v4l_print_buffer, INFO_FL_QUEUE),
2829     IOCTL_INFO(VIDIOC_EXPBUF, v4l_stub_expbuf, v4l_print_exportbuffer, INFO_FL_QUEUE | INFO_FL_CLEAR(v4l2_exportbuffer, flags)),
2830     IOCTL_INFO(VIDIOC_DQBUF, v4l_dqbuf, v4l_print_buffer, INFO_FL_QUEUE),
2831     IOCTL_INFO(VIDIOC_STREAMON, v4l_streamon, v4l_print_buftype, INFO_FL_PRIO | INFO_FL_QUEUE),
2832     IOCTL_INFO(VIDIOC_STREAMOFF, v4l_streamoff, v4l_print_buftype, INFO_FL_PRIO | INFO_FL_QUEUE),
2833     IOCTL_INFO(VIDIOC_G_PARM, v4l_g_parm, v4l_print_streamparm, INFO_FL_CLEAR(v4l2_streamparm, type)),
2834     IOCTL_INFO(VIDIOC_S_PARM, v4l_s_parm, v4l_print_streamparm, INFO_FL_PRIO),
2835     IOCTL_INFO(VIDIOC_G_STD, v4l_stub_g_std, v4l_print_std, 0),
2836     IOCTL_INFO(VIDIOC_S_STD, v4l_s_std, v4l_print_std, INFO_FL_PRIO),
2837     IOCTL_INFO(VIDIOC_ENUMSTD, v4l_enumstd, v4l_print_standard, INFO_FL_CLEAR(v4l2_standard, index)),
2838     IOCTL_INFO(VIDIOC_ENUMINPUT, v4l_enuminput, v4l_print_enuminput, INFO_FL_CLEAR(v4l2_input, index)),
2839     IOCTL_INFO(VIDIOC_G_CTRL, v4l_g_ctrl, v4l_print_control, INFO_FL_CTRL | INFO_FL_CLEAR(v4l2_control, id)),
2840     IOCTL_INFO(VIDIOC_S_CTRL, v4l_s_ctrl, v4l_print_control, INFO_FL_PRIO | INFO_FL_CTRL),
2841     IOCTL_INFO(VIDIOC_G_TUNER, v4l_g_tuner, v4l_print_tuner, INFO_FL_CLEAR(v4l2_tuner, index)),
2842     IOCTL_INFO(VIDIOC_S_TUNER, v4l_s_tuner, v4l_print_tuner, INFO_FL_PRIO),
2843     IOCTL_INFO(VIDIOC_G_AUDIO, v4l_stub_g_audio, v4l_print_audio, 0),
2844     IOCTL_INFO(VIDIOC_S_AUDIO, v4l_stub_s_audio, v4l_print_audio, INFO_FL_PRIO),
2845     IOCTL_INFO(VIDIOC_QUERYCTRL, v4l_queryctrl, v4l_print_queryctrl, INFO_FL_CTRL | INFO_FL_CLEAR(v4l2_queryctrl, id)),
2846     IOCTL_INFO(VIDIOC_QUERYMENU, v4l_querymenu, v4l_print_querymenu, INFO_FL_CTRL | INFO_FL_CLEAR(v4l2_querymenu, index)),
2847     IOCTL_INFO(VIDIOC_G_INPUT, v4l_g_input, v4l_print_u32, 0),
2848     IOCTL_INFO(VIDIOC_S_INPUT, v4l_s_input, v4l_print_u32, INFO_FL_PRIO),
2849     IOCTL_INFO(VIDIOC_G_EDID, v4l_stub_g_edid, v4l_print_edid, INFO_FL_ALWAYS_COPY),
2850     IOCTL_INFO(VIDIOC_S_EDID, v4l_stub_s_edid, v4l_print_edid, INFO_FL_PRIO | INFO_FL_ALWAYS_COPY),
2851     IOCTL_INFO(VIDIOC_G_OUTPUT, v4l_g_output, v4l_print_u32, 0),
2852     IOCTL_INFO(VIDIOC_S_OUTPUT, v4l_s_output, v4l_print_u32, INFO_FL_PRIO),
2853     IOCTL_INFO(VIDIOC_ENUMOUTPUT, v4l_enumoutput, v4l_print_enumoutput, INFO_FL_CLEAR(v4l2_output, index)),
2854     IOCTL_INFO(VIDIOC_G_AUDOUT, v4l_stub_g_audout, v4l_print_audioout, 0),
2855     IOCTL_INFO(VIDIOC_S_AUDOUT, v4l_stub_s_audout, v4l_print_audioout, INFO_FL_PRIO),
2856     IOCTL_INFO(VIDIOC_G_MODULATOR, v4l_g_modulator, v4l_print_modulator, INFO_FL_CLEAR(v4l2_modulator, index)),
2857     IOCTL_INFO(VIDIOC_S_MODULATOR, v4l_s_modulator, v4l_print_modulator, INFO_FL_PRIO),
2858     IOCTL_INFO(VIDIOC_G_FREQUENCY, v4l_g_frequency, v4l_print_frequency, INFO_FL_CLEAR(v4l2_frequency, tuner)),
2859     IOCTL_INFO(VIDIOC_S_FREQUENCY, v4l_s_frequency, v4l_print_frequency, INFO_FL_PRIO),
2860     IOCTL_INFO(VIDIOC_CROPCAP, v4l_cropcap, v4l_print_cropcap, INFO_FL_CLEAR(v4l2_cropcap, type)),
2861     IOCTL_INFO(VIDIOC_G_CROP, v4l_g_crop, v4l_print_crop, INFO_FL_CLEAR(v4l2_crop, type)),
2862     IOCTL_INFO(VIDIOC_S_CROP, v4l_s_crop, v4l_print_crop, INFO_FL_PRIO),
2863     IOCTL_INFO(VIDIOC_G_SELECTION, v4l_g_selection, v4l_print_selection, INFO_FL_CLEAR(v4l2_selection, r)),
2864     IOCTL_INFO(VIDIOC_S_SELECTION, v4l_s_selection, v4l_print_selection, INFO_FL_PRIO | INFO_FL_CLEAR(v4l2_selection, r)),
2865     IOCTL_INFO(VIDIOC_G_JPEGCOMP, v4l_stub_g_jpegcomp, v4l_print_jpegcompression, 0),
2866     IOCTL_INFO(VIDIOC_S_JPEGCOMP, v4l_stub_s_jpegcomp, v4l_print_jpegcompression, INFO_FL_PRIO),
2867     IOCTL_INFO(VIDIOC_QUERYSTD, v4l_querystd, v4l_print_std, 0),
2868     IOCTL_INFO(VIDIOC_TRY_FMT, v4l_try_fmt, v4l_print_format, 0),
2869     IOCTL_INFO(VIDIOC_ENUMAUDIO, v4l_stub_enumaudio, v4l_print_audio, INFO_FL_CLEAR(v4l2_audio, index)),
2870     IOCTL_INFO(VIDIOC_ENUMAUDOUT, v4l_stub_enumaudout, v4l_print_audioout, INFO_FL_CLEAR(v4l2_audioout, index)),
2871     IOCTL_INFO(VIDIOC_G_PRIORITY, v4l_g_priority, v4l_print_u32, 0),
2872     IOCTL_INFO(VIDIOC_S_PRIORITY, v4l_s_priority, v4l_print_u32, INFO_FL_PRIO),
2873     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)),
2874     IOCTL_INFO(VIDIOC_LOG_STATUS, v4l_log_status, v4l_print_newline, 0),
2875     IOCTL_INFO(VIDIOC_G_EXT_CTRLS, v4l_g_ext_ctrls, v4l_print_ext_controls, INFO_FL_CTRL | INFO_FL_ALWAYS_COPY),
2876     IOCTL_INFO(VIDIOC_S_EXT_CTRLS, v4l_s_ext_ctrls, v4l_print_ext_controls, INFO_FL_PRIO | INFO_FL_CTRL | INFO_FL_ALWAYS_COPY),
2877     IOCTL_INFO(VIDIOC_TRY_EXT_CTRLS, v4l_try_ext_ctrls, v4l_print_ext_controls, INFO_FL_CTRL | INFO_FL_ALWAYS_COPY),
2878     IOCTL_INFO(VIDIOC_ENUM_FRAMESIZES, v4l_stub_enum_framesizes, v4l_print_frmsizeenum, INFO_FL_CLEAR(v4l2_frmsizeenum, pixel_format)),
2879     IOCTL_INFO(VIDIOC_ENUM_FRAMEINTERVALS, v4l_stub_enum_frameintervals, v4l_print_frmivalenum, INFO_FL_CLEAR(v4l2_frmivalenum, height)),
2880     IOCTL_INFO(VIDIOC_G_ENC_INDEX, v4l_stub_g_enc_index, v4l_print_enc_idx, 0),
2881     IOCTL_INFO(VIDIOC_ENCODER_CMD, v4l_stub_encoder_cmd, v4l_print_encoder_cmd, INFO_FL_PRIO | INFO_FL_CLEAR(v4l2_encoder_cmd, flags)),
2882     IOCTL_INFO(VIDIOC_TRY_ENCODER_CMD, v4l_stub_try_encoder_cmd, v4l_print_encoder_cmd, INFO_FL_CLEAR(v4l2_encoder_cmd, flags)),
2883     IOCTL_INFO(VIDIOC_DECODER_CMD, v4l_stub_decoder_cmd, v4l_print_decoder_cmd, INFO_FL_PRIO),
2884     IOCTL_INFO(VIDIOC_TRY_DECODER_CMD, v4l_stub_try_decoder_cmd, v4l_print_decoder_cmd, 0),
2885     IOCTL_INFO(VIDIOC_DBG_S_REGISTER, v4l_dbg_s_register, v4l_print_dbg_register, 0),
2886     IOCTL_INFO(VIDIOC_DBG_G_REGISTER, v4l_dbg_g_register, v4l_print_dbg_register, 0),
2887     IOCTL_INFO(VIDIOC_S_HW_FREQ_SEEK, v4l_s_hw_freq_seek, v4l_print_hw_freq_seek, INFO_FL_PRIO),
2888     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)),
2889     IOCTL_INFO(VIDIOC_G_DV_TIMINGS, v4l_stub_g_dv_timings, v4l_print_dv_timings, 0),
2890     IOCTL_INFO(VIDIOC_DQEVENT, v4l_dqevent, v4l_print_event, 0),
2891     IOCTL_INFO(VIDIOC_SUBSCRIBE_EVENT, v4l_subscribe_event, v4l_print_event_subscription, 0),
2892     IOCTL_INFO(VIDIOC_UNSUBSCRIBE_EVENT, v4l_unsubscribe_event, v4l_print_event_subscription, 0),
2893     IOCTL_INFO(VIDIOC_CREATE_BUFS, v4l_create_bufs, v4l_print_create_buffers, INFO_FL_PRIO | INFO_FL_QUEUE),
2894     IOCTL_INFO(VIDIOC_PREPARE_BUF, v4l_prepare_buf, v4l_print_buffer, INFO_FL_QUEUE),
2895     IOCTL_INFO(VIDIOC_ENUM_DV_TIMINGS, v4l_stub_enum_dv_timings, v4l_print_enum_dv_timings, INFO_FL_CLEAR(v4l2_enum_dv_timings, pad)),
2896     IOCTL_INFO(VIDIOC_QUERY_DV_TIMINGS, v4l_stub_query_dv_timings, v4l_print_dv_timings, INFO_FL_ALWAYS_COPY),
2897     IOCTL_INFO(VIDIOC_DV_TIMINGS_CAP, v4l_stub_dv_timings_cap, v4l_print_dv_timings_cap, INFO_FL_CLEAR(v4l2_dv_timings_cap, pad)),
2898     IOCTL_INFO(VIDIOC_ENUM_FREQ_BANDS, v4l_enum_freq_bands, v4l_print_freq_band, 0),
2899     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)),
2900     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)),
2901 };
2902 #define V4L2_IOCTLS ARRAY_SIZE(v4l2_ioctls)
2903 
2904 static bool v4l2_is_known_ioctl(unsigned int cmd)
2905 {
2906     if (_IOC_NR(cmd) >= V4L2_IOCTLS)
2907         return false;
2908     return v4l2_ioctls[_IOC_NR(cmd)].ioctl == cmd;
2909 }
2910 
2911 static struct mutex *v4l2_ioctl_get_lock(struct video_device *vdev,
2912                      struct v4l2_fh *vfh, unsigned int cmd,
2913                      void *arg)
2914 {
2915     if (_IOC_NR(cmd) >= V4L2_IOCTLS)
2916         return vdev->lock;
2917     if (vfh && vfh->m2m_ctx &&
2918         (v4l2_ioctls[_IOC_NR(cmd)].flags & INFO_FL_QUEUE)) {
2919         if (vfh->m2m_ctx->q_lock)
2920             return vfh->m2m_ctx->q_lock;
2921     }
2922     if (vdev->queue && vdev->queue->lock &&
2923             (v4l2_ioctls[_IOC_NR(cmd)].flags & INFO_FL_QUEUE))
2924         return vdev->queue->lock;
2925     return vdev->lock;
2926 }
2927 
2928 /* Common ioctl debug function. This function can be used by
2929    external ioctl messages as well as internal V4L ioctl */
2930 void v4l_printk_ioctl(const char *prefix, unsigned int cmd)
2931 {
2932     const char *dir, *type;
2933 
2934     if (prefix)
2935         printk(KERN_DEBUG "%s: ", prefix);
2936 
2937     switch (_IOC_TYPE(cmd)) {
2938     case 'd':
2939         type = "v4l2_int";
2940         break;
2941     case 'V':
2942         if (_IOC_NR(cmd) >= V4L2_IOCTLS) {
2943             type = "v4l2";
2944             break;
2945         }
2946         pr_cont("%s", v4l2_ioctls[_IOC_NR(cmd)].name);
2947         return;
2948     default:
2949         type = "unknown";
2950         break;
2951     }
2952 
2953     switch (_IOC_DIR(cmd)) {
2954     case _IOC_NONE:              dir = "--"; break;
2955     case _IOC_READ:              dir = "r-"; break;
2956     case _IOC_WRITE:             dir = "-w"; break;
2957     case _IOC_READ | _IOC_WRITE: dir = "rw"; break;
2958     default:                     dir = "*ERR*"; break;
2959     }
2960     pr_cont("%s ioctl '%c', dir=%s, #%d (0x%08x)",
2961         type, _IOC_TYPE(cmd), dir, _IOC_NR(cmd), cmd);
2962 }
2963 EXPORT_SYMBOL(v4l_printk_ioctl);
2964 
2965 static long __video_do_ioctl(struct file *file,
2966         unsigned int cmd, void *arg)
2967 {
2968     struct video_device *vfd = video_devdata(file);
2969     struct mutex *req_queue_lock = NULL;
2970     struct mutex *lock; /* ioctl serialization mutex */
2971     const struct v4l2_ioctl_ops *ops = vfd->ioctl_ops;
2972     bool write_only = false;
2973     struct v4l2_ioctl_info default_info;
2974     const struct v4l2_ioctl_info *info;
2975     void *fh = file->private_data;
2976     struct v4l2_fh *vfh = NULL;
2977     int dev_debug = vfd->dev_debug;
2978     long ret = -ENOTTY;
2979 
2980     if (ops == NULL) {
2981         pr_warn("%s: has no ioctl_ops.\n",
2982                 video_device_node_name(vfd));
2983         return ret;
2984     }
2985 
2986     if (test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags))
2987         vfh = file->private_data;
2988 
2989     /*
2990      * We need to serialize streamon/off with queueing new requests.
2991      * These ioctls may trigger the cancellation of a streaming
2992      * operation, and that should not be mixed with queueing a new
2993      * request at the same time.
2994      */
2995     if (v4l2_device_supports_requests(vfd->v4l2_dev) &&
2996         (cmd == VIDIOC_STREAMON || cmd == VIDIOC_STREAMOFF)) {
2997         req_queue_lock = &vfd->v4l2_dev->mdev->req_queue_mutex;
2998 
2999         if (mutex_lock_interruptible(req_queue_lock))
3000             return -ERESTARTSYS;
3001     }
3002 
3003     lock = v4l2_ioctl_get_lock(vfd, vfh, cmd, arg);
3004 
3005     if (lock && mutex_lock_interruptible(lock)) {
3006         if (req_queue_lock)
3007             mutex_unlock(req_queue_lock);
3008         return -ERESTARTSYS;
3009     }
3010 
3011     if (!video_is_registered(vfd)) {
3012         ret = -ENODEV;
3013         goto unlock;
3014     }
3015 
3016     if (v4l2_is_known_ioctl(cmd)) {
3017         info = &v4l2_ioctls[_IOC_NR(cmd)];
3018 
3019         if (!test_bit(_IOC_NR(cmd), vfd->valid_ioctls) &&
3020             !((info->flags & INFO_FL_CTRL) && vfh && vfh->ctrl_handler))
3021             goto done;
3022 
3023         if (vfh && (info->flags & INFO_FL_PRIO)) {
3024             ret = v4l2_prio_check(vfd->prio, vfh->prio);
3025             if (ret)
3026                 goto done;
3027         }
3028     } else {
3029         default_info.ioctl = cmd;
3030         default_info.flags = 0;
3031         default_info.debug = v4l_print_default;
3032         info = &default_info;
3033     }
3034 
3035     write_only = _IOC_DIR(cmd) == _IOC_WRITE;
3036     if (info != &default_info) {
3037         ret = info->func(ops, file, fh, arg);
3038     } else if (!ops->vidioc_default) {
3039         ret = -ENOTTY;
3040     } else {
3041         ret = ops->vidioc_default(file, fh,
3042             vfh ? v4l2_prio_check(vfd->prio, vfh->prio) >= 0 : 0,
3043             cmd, arg);
3044     }
3045 
3046 done:
3047     if (dev_debug & (V4L2_DEV_DEBUG_IOCTL | V4L2_DEV_DEBUG_IOCTL_ARG)) {
3048         if (!(dev_debug & V4L2_DEV_DEBUG_STREAMING) &&
3049             (cmd == VIDIOC_QBUF || cmd == VIDIOC_DQBUF))
3050             goto unlock;
3051 
3052         v4l_printk_ioctl(video_device_node_name(vfd), cmd);
3053         if (ret < 0)
3054             pr_cont(": error %ld", ret);
3055         if (!(dev_debug & V4L2_DEV_DEBUG_IOCTL_ARG))
3056             pr_cont("\n");
3057         else if (_IOC_DIR(cmd) == _IOC_NONE)
3058             info->debug(arg, write_only);
3059         else {
3060             pr_cont(": ");
3061             info->debug(arg, write_only);
3062         }
3063     }
3064 
3065 unlock:
3066     if (lock)
3067         mutex_unlock(lock);
3068     if (req_queue_lock)
3069         mutex_unlock(req_queue_lock);
3070     return ret;
3071 }
3072 
3073 static int check_array_args(unsigned int cmd, void *parg, size_t *array_size,
3074                 void __user **user_ptr, void ***kernel_ptr)
3075 {
3076     int ret = 0;
3077 
3078     switch (cmd) {
3079     case VIDIOC_PREPARE_BUF:
3080     case VIDIOC_QUERYBUF:
3081     case VIDIOC_QBUF:
3082     case VIDIOC_DQBUF: {
3083         struct v4l2_buffer *buf = parg;
3084 
3085         if (V4L2_TYPE_IS_MULTIPLANAR(buf->type) && buf->length > 0) {
3086             if (buf->length > VIDEO_MAX_PLANES) {
3087                 ret = -EINVAL;
3088                 break;
3089             }
3090             *user_ptr = (void __user *)buf->m.planes;
3091             *kernel_ptr = (void **)&buf->m.planes;
3092             *array_size = sizeof(struct v4l2_plane) * buf->length;
3093             ret = 1;
3094         }
3095         break;
3096     }
3097 
3098     case VIDIOC_G_EDID:
3099     case VIDIOC_S_EDID: {
3100         struct v4l2_edid *edid = parg;
3101 
3102         if (edid->blocks) {
3103             if (edid->blocks > 256) {
3104                 ret = -EINVAL;
3105                 break;
3106             }
3107             *user_ptr = (void __user *)edid->edid;
3108             *kernel_ptr = (void **)&edid->edid;
3109             *array_size = edid->blocks * 128;
3110             ret = 1;
3111         }
3112         break;
3113     }
3114 
3115     case VIDIOC_S_EXT_CTRLS:
3116     case VIDIOC_G_EXT_CTRLS:
3117     case VIDIOC_TRY_EXT_CTRLS: {
3118         struct v4l2_ext_controls *ctrls = parg;
3119 
3120         if (ctrls->count != 0) {
3121             if (ctrls->count > V4L2_CID_MAX_CTRLS) {
3122                 ret = -EINVAL;
3123                 break;
3124             }
3125             *user_ptr = (void __user *)ctrls->controls;
3126             *kernel_ptr = (void **)&ctrls->controls;
3127             *array_size = sizeof(struct v4l2_ext_control)
3128                     * ctrls->count;
3129             ret = 1;
3130         }
3131         break;
3132     }
3133     case VIDIOC_G_FMT:
3134     case VIDIOC_S_FMT:
3135     case VIDIOC_TRY_FMT: {
3136         struct v4l2_format *fmt = parg;
3137 
3138         if (fmt->type != V4L2_BUF_TYPE_VIDEO_OVERLAY &&
3139             fmt->type != V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY)
3140             break;
3141         if (fmt->fmt.win.clipcount > 2048)
3142             return -EINVAL;
3143         if (!fmt->fmt.win.clipcount)
3144             break;
3145 
3146         *user_ptr = (void __user *)fmt->fmt.win.clips;
3147         *kernel_ptr = (void **)&fmt->fmt.win.clips;
3148         *array_size = sizeof(struct v4l2_clip)
3149                 * fmt->fmt.win.clipcount;
3150 
3151         ret = 1;
3152         break;
3153     }
3154     }
3155 
3156     return ret;
3157 }
3158 
3159 static unsigned int video_translate_cmd(unsigned int cmd)
3160 {
3161 #if !defined(CONFIG_64BIT) && defined(CONFIG_COMPAT_32BIT_TIME)
3162     switch (cmd) {
3163     case VIDIOC_DQEVENT_TIME32:
3164         return VIDIOC_DQEVENT;
3165     case VIDIOC_QUERYBUF_TIME32:
3166         return VIDIOC_QUERYBUF;
3167     case VIDIOC_QBUF_TIME32:
3168         return VIDIOC_QBUF;
3169     case VIDIOC_DQBUF_TIME32:
3170         return VIDIOC_DQBUF;
3171     case VIDIOC_PREPARE_BUF_TIME32:
3172         return VIDIOC_PREPARE_BUF;
3173     }
3174 #endif
3175     if (in_compat_syscall())
3176         return v4l2_compat_translate_cmd(cmd);
3177 
3178     return cmd;
3179 }
3180 
3181 static int video_get_user(void __user *arg, void *parg,
3182               unsigned int real_cmd, unsigned int cmd,
3183               bool *always_copy)
3184 {
3185     unsigned int n = _IOC_SIZE(real_cmd);
3186     int err = 0;
3187 
3188     if (!(_IOC_DIR(cmd) & _IOC_WRITE)) {
3189         /* read-only ioctl */
3190         memset(parg, 0, n);
3191         return 0;
3192     }
3193 
3194     /*
3195      * In some cases, only a few fields are used as input,
3196      * i.e. when the app sets "index" and then the driver
3197      * fills in the rest of the structure for the thing
3198      * with that index.  We only need to copy up the first
3199      * non-input field.
3200      */
3201     if (v4l2_is_known_ioctl(real_cmd)) {
3202         u32 flags = v4l2_ioctls[_IOC_NR(real_cmd)].flags;
3203 
3204         if (flags & INFO_FL_CLEAR_MASK)
3205             n = (flags & INFO_FL_CLEAR_MASK) >> 16;
3206         *always_copy = flags & INFO_FL_ALWAYS_COPY;
3207     }
3208 
3209     if (cmd == real_cmd) {
3210         if (copy_from_user(parg, (void __user *)arg, n))
3211             err = -EFAULT;
3212     } else if (in_compat_syscall()) {
3213         memset(parg, 0, n);
3214         err = v4l2_compat_get_user(arg, parg, cmd);
3215     } else {
3216         memset(parg, 0, n);
3217 #if !defined(CONFIG_64BIT) && defined(CONFIG_COMPAT_32BIT_TIME)
3218         switch (cmd) {
3219         case VIDIOC_QUERYBUF_TIME32:
3220         case VIDIOC_QBUF_TIME32:
3221         case VIDIOC_DQBUF_TIME32:
3222         case VIDIOC_PREPARE_BUF_TIME32: {
3223             struct v4l2_buffer_time32 vb32;
3224             struct v4l2_buffer *vb = parg;
3225 
3226             if (copy_from_user(&vb32, arg, sizeof(vb32)))
3227                 return -EFAULT;
3228 
3229             *vb = (struct v4l2_buffer) {
3230                 .index      = vb32.index,
3231                 .type       = vb32.type,
3232                 .bytesused  = vb32.bytesused,
3233                 .flags      = vb32.flags,
3234                 .field      = vb32.field,
3235                 .timestamp.tv_sec   = vb32.timestamp.tv_sec,
3236                 .timestamp.tv_usec  = vb32.timestamp.tv_usec,
3237                 .timecode   = vb32.timecode,
3238                 .sequence   = vb32.sequence,
3239                 .memory     = vb32.memory,
3240                 .m.userptr  = vb32.m.userptr,
3241                 .length     = vb32.length,
3242                 .request_fd = vb32.request_fd,
3243             };
3244             break;
3245         }
3246         }
3247 #endif
3248     }
3249 
3250     /* zero out anything we don't copy from userspace */
3251     if (!err && n < _IOC_SIZE(real_cmd))
3252         memset((u8 *)parg + n, 0, _IOC_SIZE(real_cmd) - n);
3253     return err;
3254 }
3255 
3256 static int video_put_user(void __user *arg, void *parg,
3257               unsigned int real_cmd, unsigned int cmd)
3258 {
3259     if (!(_IOC_DIR(cmd) & _IOC_READ))
3260         return 0;
3261 
3262     if (cmd == real_cmd) {
3263         /*  Copy results into user buffer  */
3264         if (copy_to_user(arg, parg, _IOC_SIZE(cmd)))
3265             return -EFAULT;
3266         return 0;
3267     }
3268 
3269     if (in_compat_syscall())
3270         return v4l2_compat_put_user(arg, parg, cmd);
3271 
3272 #if !defined(CONFIG_64BIT) && defined(CONFIG_COMPAT_32BIT_TIME)
3273     switch (cmd) {
3274     case VIDIOC_DQEVENT_TIME32: {
3275         struct v4l2_event *ev = parg;
3276         struct v4l2_event_time32 ev32;
3277 
3278         memset(&ev32, 0, sizeof(ev32));
3279 
3280         ev32.type   = ev->type;
3281         ev32.pending    = ev->pending;
3282         ev32.sequence   = ev->sequence;
3283         ev32.timestamp.tv_sec   = ev->timestamp.tv_sec;
3284         ev32.timestamp.tv_nsec  = ev->timestamp.tv_nsec;
3285         ev32.id     = ev->id;
3286 
3287         memcpy(&ev32.u, &ev->u, sizeof(ev->u));
3288         memcpy(&ev32.reserved, &ev->reserved, sizeof(ev->reserved));
3289 
3290         if (copy_to_user(arg, &ev32, sizeof(ev32)))
3291             return -EFAULT;
3292         break;
3293     }
3294     case VIDIOC_QUERYBUF_TIME32:
3295     case VIDIOC_QBUF_TIME32:
3296     case VIDIOC_DQBUF_TIME32:
3297     case VIDIOC_PREPARE_BUF_TIME32: {
3298         struct v4l2_buffer *vb = parg;
3299         struct v4l2_buffer_time32 vb32;
3300 
3301         memset(&vb32, 0, sizeof(vb32));
3302 
3303         vb32.index  = vb->index;
3304         vb32.type   = vb->type;
3305         vb32.bytesused  = vb->bytesused;
3306         vb32.flags  = vb->flags;
3307         vb32.field  = vb->field;
3308         vb32.timestamp.tv_sec   = vb->timestamp.tv_sec;
3309         vb32.timestamp.tv_usec  = vb->timestamp.tv_usec;
3310         vb32.timecode   = vb->timecode;
3311         vb32.sequence   = vb->sequence;
3312         vb32.memory = vb->memory;
3313         vb32.m.userptr  = vb->m.userptr;
3314         vb32.length = vb->length;
3315         vb32.request_fd = vb->request_fd;
3316 
3317         if (copy_to_user(arg, &vb32, sizeof(vb32)))
3318             return -EFAULT;
3319         break;
3320     }
3321     }
3322 #endif
3323 
3324     return 0;
3325 }
3326 
3327 long
3328 video_usercopy(struct file *file, unsigned int orig_cmd, unsigned long arg,
3329            v4l2_kioctl func)
3330 {
3331     char    sbuf[128];
3332     void    *mbuf = NULL, *array_buf = NULL;
3333     void    *parg = (void *)arg;
3334     long    err  = -EINVAL;
3335     bool    has_array_args;
3336     bool    always_copy = false;
3337     size_t  array_size = 0;
3338     void __user *user_ptr = NULL;
3339     void    **kernel_ptr = NULL;
3340     unsigned int cmd = video_translate_cmd(orig_cmd);
3341     const size_t ioc_size = _IOC_SIZE(cmd);
3342 
3343     /*  Copy arguments into temp kernel buffer  */
3344     if (_IOC_DIR(cmd) != _IOC_NONE) {
3345         if (ioc_size <= sizeof(sbuf)) {
3346             parg = sbuf;
3347         } else {
3348             /* too big to allocate from stack */
3349             mbuf = kmalloc(ioc_size, GFP_KERNEL);
3350             if (NULL == mbuf)
3351                 return -ENOMEM;
3352             parg = mbuf;
3353         }
3354 
3355         err = video_get_user((void __user *)arg, parg, cmd,
3356                      orig_cmd, &always_copy);
3357         if (err)
3358             goto out;
3359     }
3360 
3361     err = check_array_args(cmd, parg, &array_size, &user_ptr, &kernel_ptr);
3362     if (err < 0)
3363         goto out;
3364     has_array_args = err;
3365 
3366     if (has_array_args) {
3367         array_buf = kvmalloc(array_size, GFP_KERNEL);
3368         err = -ENOMEM;
3369         if (array_buf == NULL)
3370             goto out;
3371         if (in_compat_syscall())
3372             err = v4l2_compat_get_array_args(file, array_buf,
3373                              user_ptr, array_size,
3374                              orig_cmd, parg);
3375         else
3376             err = copy_from_user(array_buf, user_ptr, array_size) ?
3377                                 -EFAULT : 0;
3378         if (err)
3379             goto out;
3380         *kernel_ptr = array_buf;
3381     }
3382 
3383     /* Handles IOCTL */
3384     err = func(file, cmd, parg);
3385     if (err == -ENOTTY || err == -ENOIOCTLCMD) {
3386         err = -ENOTTY;
3387         goto out;
3388     }
3389 
3390     if (err == 0) {
3391         if (cmd == VIDIOC_DQBUF)
3392             trace_v4l2_dqbuf(video_devdata(file)->minor, parg);
3393         else if (cmd == VIDIOC_QBUF)
3394             trace_v4l2_qbuf(video_devdata(file)->minor, parg);
3395     }
3396 
3397     /*
3398      * Some ioctls can return an error, but still have valid
3399      * results that must be returned.
3400      */
3401     if (err < 0 && !always_copy)
3402         goto out;
3403 
3404     if (has_array_args) {
3405         *kernel_ptr = (void __force *)user_ptr;
3406         if (in_compat_syscall()) {
3407             int put_err;
3408 
3409             put_err = v4l2_compat_put_array_args(file, user_ptr,
3410                                  array_buf,
3411                                  array_size,
3412                                  orig_cmd, parg);
3413             if (put_err)
3414                 err = put_err;
3415         } else if (copy_to_user(user_ptr, array_buf, array_size)) {
3416             err = -EFAULT;
3417         }
3418     }
3419 
3420     if (video_put_user((void __user *)arg, parg, cmd, orig_cmd))
3421         err = -EFAULT;
3422 out:
3423     kvfree(array_buf);
3424     kfree(mbuf);
3425     return err;
3426 }
3427 
3428 long video_ioctl2(struct file *file,
3429            unsigned int cmd, unsigned long arg)
3430 {
3431     return video_usercopy(file, cmd, arg, __video_do_ioctl);
3432 }
3433 EXPORT_SYMBOL(video_ioctl2);