Commit a5f2db53 authored by Mauro Carvalho Chehab's avatar Mauro Carvalho Chehab

v4l2-ioctl: properly return -EINVAL when parameters are wrong

Whan an ioctl is implemented, but the parameters are invalid,
the error code should be -EINVAL. However, if the ioctl is
not defined, it should return -ENOTTY instead.
Reported-by: default avatarHans Verkuil <hverkuil@xs4all.nl>
Signed-off-by: default avatarMauro Carvalho Chehab <mchehab@redhat.com>
parent 449d1a0a
...@@ -55,6 +55,14 @@ ...@@ -55,6 +55,14 @@
memset((u8 *)(p) + offsetof(typeof(*(p)), field) + sizeof((p)->field), \ memset((u8 *)(p) + offsetof(typeof(*(p)), field) + sizeof((p)->field), \
0, sizeof(*(p)) - offsetof(typeof(*(p)), field) - sizeof((p)->field)) 0, sizeof(*(p)) - offsetof(typeof(*(p)), field) - sizeof((p)->field))
#define no_ioctl_err(foo) ( ( \
ops->vidioc_##foo##_fmt_vid_cap || \
ops->vidioc_##foo##_fmt_vid_out || \
ops->vidioc_##foo##_fmt_vid_cap_mplane || \
ops->vidioc_##foo##_fmt_vid_out_mplane || \
ops->vidioc_##foo##_fmt_vid_overlay || \
ops->vidioc_##foo##_fmt_type_private) ? -EINVAL : -ENOTTY)
struct std_descr { struct std_descr {
v4l2_std_id std; v4l2_std_id std;
const char *descr; const char *descr;
...@@ -591,7 +599,7 @@ static long __video_do_ioctl(struct file *file, ...@@ -591,7 +599,7 @@ static long __video_do_ioctl(struct file *file,
ret = v4l2_prio_check(vfd->prio, vfh->prio); ret = v4l2_prio_check(vfd->prio, vfh->prio);
if (ret) if (ret)
goto exit_prio; goto exit_prio;
ret = -EINVAL; ret = -ENOTTY;
break; break;
} }
} }
...@@ -638,7 +646,7 @@ static long __video_do_ioctl(struct file *file, ...@@ -638,7 +646,7 @@ static long __video_do_ioctl(struct file *file,
enum v4l2_priority *p = arg; enum v4l2_priority *p = arg;
if (!ops->vidioc_s_priority && !use_fh_prio) if (!ops->vidioc_s_priority && !use_fh_prio)
break; break;
dbgarg(cmd, "setting priority to %d\n", *p); dbgarg(cmd, "setting priority to %d\n", *p);
if (ops->vidioc_s_priority) if (ops->vidioc_s_priority)
ret = ops->vidioc_s_priority(file, fh, *p); ret = ops->vidioc_s_priority(file, fh, *p);
...@@ -654,37 +662,37 @@ static long __video_do_ioctl(struct file *file, ...@@ -654,37 +662,37 @@ static long __video_do_ioctl(struct file *file,
switch (f->type) { switch (f->type) {
case V4L2_BUF_TYPE_VIDEO_CAPTURE: case V4L2_BUF_TYPE_VIDEO_CAPTURE:
if (ops->vidioc_enum_fmt_vid_cap) if (likely(ops->vidioc_enum_fmt_vid_cap))
ret = ops->vidioc_enum_fmt_vid_cap(file, fh, f); ret = ops->vidioc_enum_fmt_vid_cap(file, fh, f);
break; break;
case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE: case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
if (ops->vidioc_enum_fmt_vid_cap_mplane) if (likely(ops->vidioc_enum_fmt_vid_cap_mplane))
ret = ops->vidioc_enum_fmt_vid_cap_mplane(file, ret = ops->vidioc_enum_fmt_vid_cap_mplane(file,
fh, f); fh, f);
break; break;
case V4L2_BUF_TYPE_VIDEO_OVERLAY: case V4L2_BUF_TYPE_VIDEO_OVERLAY:
if (ops->vidioc_enum_fmt_vid_overlay) if (likely(ops->vidioc_enum_fmt_vid_overlay))
ret = ops->vidioc_enum_fmt_vid_overlay(file, ret = ops->vidioc_enum_fmt_vid_overlay(file,
fh, f); fh, f);
break; break;
case V4L2_BUF_TYPE_VIDEO_OUTPUT: case V4L2_BUF_TYPE_VIDEO_OUTPUT:
if (ops->vidioc_enum_fmt_vid_out) if (likely(ops->vidioc_enum_fmt_vid_out))
ret = ops->vidioc_enum_fmt_vid_out(file, fh, f); ret = ops->vidioc_enum_fmt_vid_out(file, fh, f);
break; break;
case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE: case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
if (ops->vidioc_enum_fmt_vid_out_mplane) if (likely(ops->vidioc_enum_fmt_vid_out_mplane))
ret = ops->vidioc_enum_fmt_vid_out_mplane(file, ret = ops->vidioc_enum_fmt_vid_out_mplane(file,
fh, f); fh, f);
break; break;
case V4L2_BUF_TYPE_PRIVATE: case V4L2_BUF_TYPE_PRIVATE:
if (ops->vidioc_enum_fmt_type_private) if (likely(ops->vidioc_enum_fmt_type_private))
ret = ops->vidioc_enum_fmt_type_private(file, ret = ops->vidioc_enum_fmt_type_private(file,
fh, f); fh, f);
break; break;
default: default:
break; break;
} }
if (!ret) if (likely (!ret))
dbgarg(cmd, "index=%d, type=%d, flags=%d, " dbgarg(cmd, "index=%d, type=%d, flags=%d, "
"pixelformat=%c%c%c%c, description='%s'\n", "pixelformat=%c%c%c%c, description='%s'\n",
f->index, f->type, f->flags, f->index, f->type, f->flags,
...@@ -693,6 +701,8 @@ static long __video_do_ioctl(struct file *file, ...@@ -693,6 +701,8 @@ static long __video_do_ioctl(struct file *file,
(f->pixelformat >> 16) & 0xff, (f->pixelformat >> 16) & 0xff,
(f->pixelformat >> 24) & 0xff, (f->pixelformat >> 24) & 0xff,
f->description); f->description);
else if (ret == -ENOTTY)
ret = no_ioctl_err(enum);
break; break;
} }
case VIDIOC_G_FMT: case VIDIOC_G_FMT:
...@@ -744,7 +754,7 @@ static long __video_do_ioctl(struct file *file, ...@@ -744,7 +754,7 @@ static long __video_do_ioctl(struct file *file,
v4l_print_pix_fmt_mplane(vfd, &f->fmt.pix_mp); v4l_print_pix_fmt_mplane(vfd, &f->fmt.pix_mp);
break; break;
case V4L2_BUF_TYPE_VIDEO_OVERLAY: case V4L2_BUF_TYPE_VIDEO_OVERLAY:
if (ops->vidioc_g_fmt_vid_overlay) if (likely(ops->vidioc_g_fmt_vid_overlay))
ret = ops->vidioc_g_fmt_vid_overlay(file, ret = ops->vidioc_g_fmt_vid_overlay(file,
fh, f); fh, f);
break; break;
...@@ -789,34 +799,36 @@ static long __video_do_ioctl(struct file *file, ...@@ -789,34 +799,36 @@ static long __video_do_ioctl(struct file *file,
v4l_print_pix_fmt_mplane(vfd, &f->fmt.pix_mp); v4l_print_pix_fmt_mplane(vfd, &f->fmt.pix_mp);
break; break;
case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY: case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
if (ops->vidioc_g_fmt_vid_out_overlay) if (likely(ops->vidioc_g_fmt_vid_out_overlay))
ret = ops->vidioc_g_fmt_vid_out_overlay(file, ret = ops->vidioc_g_fmt_vid_out_overlay(file,
fh, f); fh, f);
break; break;
case V4L2_BUF_TYPE_VBI_CAPTURE: case V4L2_BUF_TYPE_VBI_CAPTURE:
if (ops->vidioc_g_fmt_vbi_cap) if (likely(ops->vidioc_g_fmt_vbi_cap))
ret = ops->vidioc_g_fmt_vbi_cap(file, fh, f); ret = ops->vidioc_g_fmt_vbi_cap(file, fh, f);
break; break;
case V4L2_BUF_TYPE_VBI_OUTPUT: case V4L2_BUF_TYPE_VBI_OUTPUT:
if (ops->vidioc_g_fmt_vbi_out) if (likely(ops->vidioc_g_fmt_vbi_out))
ret = ops->vidioc_g_fmt_vbi_out(file, fh, f); ret = ops->vidioc_g_fmt_vbi_out(file, fh, f);
break; break;
case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE: case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
if (ops->vidioc_g_fmt_sliced_vbi_cap) if (likely(ops->vidioc_g_fmt_sliced_vbi_cap))
ret = ops->vidioc_g_fmt_sliced_vbi_cap(file, ret = ops->vidioc_g_fmt_sliced_vbi_cap(file,
fh, f); fh, f);
break; break;
case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT: case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
if (ops->vidioc_g_fmt_sliced_vbi_out) if (likely(ops->vidioc_g_fmt_sliced_vbi_out))
ret = ops->vidioc_g_fmt_sliced_vbi_out(file, ret = ops->vidioc_g_fmt_sliced_vbi_out(file,
fh, f); fh, f);
break; break;
case V4L2_BUF_TYPE_PRIVATE: case V4L2_BUF_TYPE_PRIVATE:
if (ops->vidioc_g_fmt_type_private) if (likely(ops->vidioc_g_fmt_type_private))
ret = ops->vidioc_g_fmt_type_private(file, ret = ops->vidioc_g_fmt_type_private(file,
fh, f); fh, f);
break; break;
} }
if (unlikely(ret == -ENOTTY))
ret = no_ioctl_err(g);
break; break;
} }
...@@ -926,33 +938,36 @@ static long __video_do_ioctl(struct file *file, ...@@ -926,33 +938,36 @@ static long __video_do_ioctl(struct file *file,
break; break;
case V4L2_BUF_TYPE_VBI_CAPTURE: case V4L2_BUF_TYPE_VBI_CAPTURE:
CLEAR_AFTER_FIELD(f, fmt.vbi); CLEAR_AFTER_FIELD(f, fmt.vbi);
if (ops->vidioc_s_fmt_vbi_cap) if (likely(ops->vidioc_s_fmt_vbi_cap))
ret = ops->vidioc_s_fmt_vbi_cap(file, fh, f); ret = ops->vidioc_s_fmt_vbi_cap(file, fh, f);
break; break;
case V4L2_BUF_TYPE_VBI_OUTPUT: case V4L2_BUF_TYPE_VBI_OUTPUT:
CLEAR_AFTER_FIELD(f, fmt.vbi); CLEAR_AFTER_FIELD(f, fmt.vbi);
if (ops->vidioc_s_fmt_vbi_out) if (likely(ops->vidioc_s_fmt_vbi_out))
ret = ops->vidioc_s_fmt_vbi_out(file, fh, f); ret = ops->vidioc_s_fmt_vbi_out(file, fh, f);
break; break;
case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE: case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
CLEAR_AFTER_FIELD(f, fmt.sliced); CLEAR_AFTER_FIELD(f, fmt.sliced);
if (ops->vidioc_s_fmt_sliced_vbi_cap) if (likely(ops->vidioc_s_fmt_sliced_vbi_cap))
ret = ops->vidioc_s_fmt_sliced_vbi_cap(file, ret = ops->vidioc_s_fmt_sliced_vbi_cap(file,
fh, f); fh, f);
break; break;
case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT: case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
CLEAR_AFTER_FIELD(f, fmt.sliced); CLEAR_AFTER_FIELD(f, fmt.sliced);
if (ops->vidioc_s_fmt_sliced_vbi_out) if (likely(ops->vidioc_s_fmt_sliced_vbi_out))
ret = ops->vidioc_s_fmt_sliced_vbi_out(file, ret = ops->vidioc_s_fmt_sliced_vbi_out(file,
fh, f); fh, f);
break; break;
case V4L2_BUF_TYPE_PRIVATE: case V4L2_BUF_TYPE_PRIVATE:
/* CLEAR_AFTER_FIELD(f, fmt.raw_data); <- does nothing */ /* CLEAR_AFTER_FIELD(f, fmt.raw_data); <- does nothing */
if (ops->vidioc_s_fmt_type_private) if (likely(ops->vidioc_s_fmt_type_private))
ret = ops->vidioc_s_fmt_type_private(file, ret = ops->vidioc_s_fmt_type_private(file,
fh, f); fh, f);
break; break;
} }
if (unlikely(ret == -ENOTTY))
ret = no_ioctl_err(g);
break; break;
} }
case VIDIOC_TRY_FMT: case VIDIOC_TRY_FMT:
...@@ -1008,7 +1023,7 @@ static long __video_do_ioctl(struct file *file, ...@@ -1008,7 +1023,7 @@ static long __video_do_ioctl(struct file *file,
break; break;
case V4L2_BUF_TYPE_VIDEO_OVERLAY: case V4L2_BUF_TYPE_VIDEO_OVERLAY:
CLEAR_AFTER_FIELD(f, fmt.win); CLEAR_AFTER_FIELD(f, fmt.win);
if (ops->vidioc_try_fmt_vid_overlay) if (likely(ops->vidioc_try_fmt_vid_overlay))
ret = ops->vidioc_try_fmt_vid_overlay(file, ret = ops->vidioc_try_fmt_vid_overlay(file,
fh, f); fh, f);
break; break;
...@@ -1057,40 +1072,43 @@ static long __video_do_ioctl(struct file *file, ...@@ -1057,40 +1072,43 @@ static long __video_do_ioctl(struct file *file,
break; break;
case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY: case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
CLEAR_AFTER_FIELD(f, fmt.win); CLEAR_AFTER_FIELD(f, fmt.win);
if (ops->vidioc_try_fmt_vid_out_overlay) if (likely(ops->vidioc_try_fmt_vid_out_overlay))
ret = ops->vidioc_try_fmt_vid_out_overlay(file, ret = ops->vidioc_try_fmt_vid_out_overlay(file,
fh, f); fh, f);
break; break;
case V4L2_BUF_TYPE_VBI_CAPTURE: case V4L2_BUF_TYPE_VBI_CAPTURE:
CLEAR_AFTER_FIELD(f, fmt.vbi); CLEAR_AFTER_FIELD(f, fmt.vbi);
if (ops->vidioc_try_fmt_vbi_cap) if (likely(ops->vidioc_try_fmt_vbi_cap))
ret = ops->vidioc_try_fmt_vbi_cap(file, fh, f); ret = ops->vidioc_try_fmt_vbi_cap(file, fh, f);
break; break;
case V4L2_BUF_TYPE_VBI_OUTPUT: case V4L2_BUF_TYPE_VBI_OUTPUT:
CLEAR_AFTER_FIELD(f, fmt.vbi); CLEAR_AFTER_FIELD(f, fmt.vbi);
if (ops->vidioc_try_fmt_vbi_out) if (likely(ops->vidioc_try_fmt_vbi_out))
ret = ops->vidioc_try_fmt_vbi_out(file, fh, f); ret = ops->vidioc_try_fmt_vbi_out(file, fh, f);
break; break;
case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE: case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
CLEAR_AFTER_FIELD(f, fmt.sliced); CLEAR_AFTER_FIELD(f, fmt.sliced);
if (ops->vidioc_try_fmt_sliced_vbi_cap) if (likely(ops->vidioc_try_fmt_sliced_vbi_cap))
ret = ops->vidioc_try_fmt_sliced_vbi_cap(file, ret = ops->vidioc_try_fmt_sliced_vbi_cap(file,
fh, f); fh, f);
break; break;
case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT: case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
CLEAR_AFTER_FIELD(f, fmt.sliced); CLEAR_AFTER_FIELD(f, fmt.sliced);
if (ops->vidioc_try_fmt_sliced_vbi_out) if (likely(ops->vidioc_try_fmt_sliced_vbi_out))
ret = ops->vidioc_try_fmt_sliced_vbi_out(file, ret = ops->vidioc_try_fmt_sliced_vbi_out(file,
fh, f); fh, f);
else
ret = no_ioctl_err(try);
break; break;
case V4L2_BUF_TYPE_PRIVATE: case V4L2_BUF_TYPE_PRIVATE:
/* CLEAR_AFTER_FIELD(f, fmt.raw_data); <- does nothing */ /* CLEAR_AFTER_FIELD(f, fmt.raw_data); <- does nothing */
if (ops->vidioc_try_fmt_type_private) if (likely(ops->vidioc_try_fmt_type_private))
ret = ops->vidioc_try_fmt_type_private(file, ret = ops->vidioc_try_fmt_type_private(file,
fh, f); fh, f);
break; break;
} }
if (unlikely(ret == -ENOTTY))
ret = no_ioctl_err(g);
break; break;
} }
/* FIXME: Those buf reqs could be handled here, /* FIXME: Those buf reqs could be handled here,
...@@ -1262,16 +1280,15 @@ static long __video_do_ioctl(struct file *file, ...@@ -1262,16 +1280,15 @@ static long __video_do_ioctl(struct file *file,
{ {
v4l2_std_id *id = arg; v4l2_std_id *id = arg;
ret = 0;
/* Calls the specific handler */ /* Calls the specific handler */
if (ops->vidioc_g_std) if (ops->vidioc_g_std)
ret = ops->vidioc_g_std(file, fh, id); ret = ops->vidioc_g_std(file, fh, id);
else if (vfd->current_norm) else if (vfd->current_norm) {
ret = 0;
*id = vfd->current_norm; *id = vfd->current_norm;
else }
ret = -EINVAL;
if (!ret) if (likely(!ret))
dbgarg(cmd, "std=0x%08Lx\n", (long long unsigned)*id); dbgarg(cmd, "std=0x%08Lx\n", (long long unsigned)*id);
break; break;
} }
...@@ -1288,8 +1305,6 @@ static long __video_do_ioctl(struct file *file, ...@@ -1288,8 +1305,6 @@ static long __video_do_ioctl(struct file *file,
/* Calls the specific handler */ /* Calls the specific handler */
if (ops->vidioc_s_std) if (ops->vidioc_s_std)
ret = ops->vidioc_s_std(file, fh, &norm); ret = ops->vidioc_s_std(file, fh, &norm);
else
ret = -EINVAL;
/* Updates standard information */ /* Updates standard information */
if (ret >= 0) if (ret >= 0)
...@@ -1812,7 +1827,7 @@ static long __video_do_ioctl(struct file *file, ...@@ -1812,7 +1827,7 @@ static long __video_do_ioctl(struct file *file,
if (ops->vidioc_g_std) if (ops->vidioc_g_std)
ret = ops->vidioc_g_std(file, fh, &std); ret = ops->vidioc_g_std(file, fh, &std);
else if (std == 0) else if (std == 0)
ret = -EINVAL; ret = -ENOTTY;
if (ret == 0) if (ret == 0)
v4l2_video_std_frame_period(std, v4l2_video_std_frame_period(std,
&p->parm.capture.timeperframe); &p->parm.capture.timeperframe);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment