[media] v4l: subdev: Unify argument validation across IOCTLs
authorSakari Ailus <sakari.ailus@linux.intel.com>
Wed, 4 Jun 2014 11:22:03 +0000 (08:22 -0300)
committerMauro Carvalho Chehab <m.chehab@samsung.com>
Tue, 22 Jul 2014 04:01:46 +0000 (01:01 -0300)
Separate validation of different argument types. There's no reason to do
this separately for every IOCTL.

Signed-off-by: Sakari Ailus <sakari.ailus@linux.intel.com>
Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
Signed-off-by: Mauro Carvalho Chehab <m.chehab@samsung.com>
drivers/media/v4l2-core/v4l2-subdev.c

index b984f33de7f4aa330d490015b552e08917a64c68..b92cf6958331a8f3b9b6023a5427ebd2abea878a 100644 (file)
@@ -126,6 +126,55 @@ static int subdev_close(struct file *file)
        return 0;
 }
 
+static int check_format(struct v4l2_subdev *sd,
+                       struct v4l2_subdev_format *format)
+{
+       if (format->which != V4L2_SUBDEV_FORMAT_TRY &&
+           format->which != V4L2_SUBDEV_FORMAT_ACTIVE)
+               return -EINVAL;
+
+       if (format->pad >= sd->entity.num_pads)
+               return -EINVAL;
+
+       return 0;
+}
+
+static int check_crop(struct v4l2_subdev *sd, struct v4l2_subdev_crop *crop)
+{
+       if (crop->which != V4L2_SUBDEV_FORMAT_TRY &&
+           crop->which != V4L2_SUBDEV_FORMAT_ACTIVE)
+               return -EINVAL;
+
+       if (crop->pad >= sd->entity.num_pads)
+               return -EINVAL;
+
+       return 0;
+}
+
+static int check_selection(struct v4l2_subdev *sd,
+                          struct v4l2_subdev_selection *sel)
+{
+       if (sel->which != V4L2_SUBDEV_FORMAT_TRY &&
+           sel->which != V4L2_SUBDEV_FORMAT_ACTIVE)
+               return -EINVAL;
+
+       if (sel->pad >= sd->entity.num_pads)
+               return -EINVAL;
+
+       return 0;
+}
+
+static int check_edid(struct v4l2_subdev *sd, struct v4l2_subdev_edid *edid)
+{
+       if (edid->pad >= sd->entity.num_pads)
+               return -EINVAL;
+
+       if (edid->blocks && edid->edid == NULL)
+               return -EINVAL;
+
+       return 0;
+}
+
 static long subdev_do_ioctl(struct file *file, unsigned int cmd, void *arg)
 {
        struct video_device *vdev = video_devdata(file);
@@ -134,6 +183,7 @@ static long subdev_do_ioctl(struct file *file, unsigned int cmd, void *arg)
 #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)
        struct v4l2_subdev_fh *subdev_fh = to_v4l2_subdev_fh(vfh);
 #endif
+       int rval;
 
        switch (cmd) {
        case VIDIOC_QUERYCTRL:
@@ -206,12 +256,9 @@ static long subdev_do_ioctl(struct file *file, unsigned int cmd, void *arg)
        case VIDIOC_SUBDEV_G_FMT: {
                struct v4l2_subdev_format *format = arg;
 
-               if (format->which != V4L2_SUBDEV_FORMAT_TRY &&
-                   format->which != V4L2_SUBDEV_FORMAT_ACTIVE)
-                       return -EINVAL;
-
-               if (format->pad >= sd->entity.num_pads)
-                       return -EINVAL;
+               rval = check_format(sd, format);
+               if (rval)
+                       return rval;
 
                return v4l2_subdev_call(sd, pad, get_fmt, subdev_fh, format);
        }
@@ -219,12 +266,9 @@ static long subdev_do_ioctl(struct file *file, unsigned int cmd, void *arg)
        case VIDIOC_SUBDEV_S_FMT: {
                struct v4l2_subdev_format *format = arg;
 
-               if (format->which != V4L2_SUBDEV_FORMAT_TRY &&
-                   format->which != V4L2_SUBDEV_FORMAT_ACTIVE)
-                       return -EINVAL;
-
-               if (format->pad >= sd->entity.num_pads)
-                       return -EINVAL;
+               rval = check_format(sd, format);
+               if (rval)
+                       return rval;
 
                return v4l2_subdev_call(sd, pad, set_fmt, subdev_fh, format);
        }
@@ -232,14 +276,10 @@ static long subdev_do_ioctl(struct file *file, unsigned int cmd, void *arg)
        case VIDIOC_SUBDEV_G_CROP: {
                struct v4l2_subdev_crop *crop = arg;
                struct v4l2_subdev_selection sel;
-               int rval;
-
-               if (crop->which != V4L2_SUBDEV_FORMAT_TRY &&
-                   crop->which != V4L2_SUBDEV_FORMAT_ACTIVE)
-                       return -EINVAL;
 
-               if (crop->pad >= sd->entity.num_pads)
-                       return -EINVAL;
+               rval = check_crop(sd, crop);
+               if (rval)
+                       return rval;
 
                rval = v4l2_subdev_call(sd, pad, get_crop, subdev_fh, crop);
                if (rval != -ENOIOCTLCMD)
@@ -261,14 +301,10 @@ static long subdev_do_ioctl(struct file *file, unsigned int cmd, void *arg)
        case VIDIOC_SUBDEV_S_CROP: {
                struct v4l2_subdev_crop *crop = arg;
                struct v4l2_subdev_selection sel;
-               int rval;
-
-               if (crop->which != V4L2_SUBDEV_FORMAT_TRY &&
-                   crop->which != V4L2_SUBDEV_FORMAT_ACTIVE)
-                       return -EINVAL;
 
-               if (crop->pad >= sd->entity.num_pads)
-                       return -EINVAL;
+               rval = check_crop(sd, crop);
+               if (rval)
+                       return rval;
 
                rval = v4l2_subdev_call(sd, pad, set_crop, subdev_fh, crop);
                if (rval != -ENOIOCTLCMD)
@@ -339,12 +375,9 @@ static long subdev_do_ioctl(struct file *file, unsigned int cmd, void *arg)
        case VIDIOC_SUBDEV_G_SELECTION: {
                struct v4l2_subdev_selection *sel = arg;
 
-               if (sel->which != V4L2_SUBDEV_FORMAT_TRY &&
-                   sel->which != V4L2_SUBDEV_FORMAT_ACTIVE)
-                       return -EINVAL;
-
-               if (sel->pad >= sd->entity.num_pads)
-                       return -EINVAL;
+               rval = check_selection(sd, sel);
+               if (rval)
+                       return rval;
 
                return v4l2_subdev_call(
                        sd, pad, get_selection, subdev_fh, sel);
@@ -353,12 +386,9 @@ static long subdev_do_ioctl(struct file *file, unsigned int cmd, void *arg)
        case VIDIOC_SUBDEV_S_SELECTION: {
                struct v4l2_subdev_selection *sel = arg;
 
-               if (sel->which != V4L2_SUBDEV_FORMAT_TRY &&
-                   sel->which != V4L2_SUBDEV_FORMAT_ACTIVE)
-                       return -EINVAL;
-
-               if (sel->pad >= sd->entity.num_pads)
-                       return -EINVAL;
+               rval = check_selection(sd, sel);
+               if (rval)
+                       return rval;
 
                return v4l2_subdev_call(
                        sd, pad, set_selection, subdev_fh, sel);
@@ -367,10 +397,9 @@ static long subdev_do_ioctl(struct file *file, unsigned int cmd, void *arg)
        case VIDIOC_G_EDID: {
                struct v4l2_subdev_edid *edid = arg;
 
-               if (edid->pad >= sd->entity.num_pads)
-                       return -EINVAL;
-               if (edid->blocks && edid->edid == NULL)
-                       return -EINVAL;
+               rval = check_edid(sd, edid);
+               if (rval)
+                       return rval;
 
                return v4l2_subdev_call(sd, pad, get_edid, edid);
        }
@@ -378,10 +407,9 @@ static long subdev_do_ioctl(struct file *file, unsigned int cmd, void *arg)
        case VIDIOC_S_EDID: {
                struct v4l2_subdev_edid *edid = arg;
 
-               if (edid->pad >= sd->entity.num_pads)
-                       return -EINVAL;
-               if (edid->blocks && edid->edid == NULL)
-                       return -EINVAL;
+               rval = check_edid(sd, edid);
+               if (rval)
+                       return rval;
 
                return v4l2_subdev_call(sd, pad, set_edid, edid);
        }