blob: c7e9d29d0e33f8156337f8c46fb167bc484e22dd [file] [log] [blame]
Philippe De Muytercf664a62006-01-09 15:24:59 -02001/*
2 * ioctl32.c: Conversion between 32bit and 64bit native ioctls.
3 * Separated from fs stuff by Arnd Bergmann <arnd@arndb.de>
4 *
5 * Copyright (C) 1997-2000 Jakub Jelinek (jakub@redhat.com)
6 * Copyright (C) 1998 Eddie C. Dost (ecd@skynet.be)
7 * Copyright (C) 2001,2002 Andi Kleen, SuSE Labs
Pavel Macheka2531292010-07-18 14:27:13 +02008 * Copyright (C) 2003 Pavel Machek (pavel@ucw.cz)
Philippe De Muytercf664a62006-01-09 15:24:59 -02009 * Copyright (C) 2005 Philippe De Muyter (phdm@macqel.be)
Hans Verkuil92f45ba2008-12-21 10:35:25 -030010 * Copyright (C) 2008 Hans Verkuil <hverkuil@xs4all.nl>
Philippe De Muytercf664a62006-01-09 15:24:59 -020011 *
12 * These routines maintain argument size conversion between 32bit and 64bit
13 * ioctls.
14 */
15
Arnd Bergmann0d0fbf82006-01-09 15:24:57 -020016#include <linux/compat.h>
Nickolay V. Shmyrev133b7352006-01-09 15:24:58 -020017#include <linux/module.h>
Laurent Pinchartb9d0aa62011-12-18 20:41:19 -030018#include <linux/videodev2.h>
Hans Verkuiled45ce22012-08-10 06:07:12 -030019#include <linux/v4l2-subdev.h>
Laurent Pinchartb9d0aa62011-12-18 20:41:19 -030020#include <media/v4l2-dev.h>
Mauro Carvalho Chehab28644622008-07-20 20:26:54 -030021#include <media/v4l2-ioctl.h>
Arnd Bergmann0d0fbf82006-01-09 15:24:57 -020022
Hans Verkuil069b7472008-12-30 07:04:34 -030023static long native_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
Arnd Bergmann0d0fbf82006-01-09 15:24:57 -020024{
Hans Verkuil069b7472008-12-30 07:04:34 -030025 long ret = -ENOIOCTLCMD;
Arnd Bergmann0d0fbf82006-01-09 15:24:57 -020026
Philippe De Muyter13d133b2006-01-09 15:25:00 -020027 if (file->f_op->unlocked_ioctl)
28 ret = file->f_op->unlocked_ioctl(file, cmd, arg);
Arnd Bergmann0d0fbf82006-01-09 15:24:57 -020029
30 return ret;
31}
32
33
Hans Verkuild1f81da2008-10-22 00:04:45 -030034struct v4l2_clip32 {
Philippe De Muytercf664a62006-01-09 15:24:59 -020035 struct v4l2_rect c;
Mauro Carvalho Chehab6e6a8b52018-01-04 13:08:56 -050036 compat_caddr_t next;
Philippe De Muytercf664a62006-01-09 15:24:59 -020037};
38
Hans Verkuild1f81da2008-10-22 00:04:45 -030039struct v4l2_window32 {
Philippe De Muytercf664a62006-01-09 15:24:59 -020040 struct v4l2_rect w;
Mauro Carvalho Chehab4a3fad72018-01-04 06:47:28 -050041 __u32 field; /* enum v4l2_field */
Philippe De Muytercf664a62006-01-09 15:24:59 -020042 __u32 chromakey;
43 compat_caddr_t clips; /* actually struct v4l2_clip32 * */
44 __u32 clipcount;
45 compat_caddr_t bitmap;
Daniel Mentz025a26f2017-08-03 18:03:10 -040046 __u8 global_alpha;
Philippe De Muytercf664a62006-01-09 15:24:59 -020047};
48
49static int get_v4l2_window32(struct v4l2_window *kp, struct v4l2_window32 __user *up)
50{
Guy Martina113bc72006-01-11 23:40:51 -020051 if (!access_ok(VERIFY_READ, up, sizeof(struct v4l2_window32)) ||
Hans Verkuilb7b957d2018-01-24 04:35:48 -050052 copy_from_user(&kp->w, &up->w, sizeof(up->w)) ||
53 get_user(kp->field, &up->field) ||
54 get_user(kp->chromakey, &up->chromakey) ||
55 get_user(kp->clipcount, &up->clipcount) ||
56 get_user(kp->global_alpha, &up->global_alpha))
57 return -EFAULT;
Philippe De Muytercf664a62006-01-09 15:24:59 -020058 if (kp->clipcount > 2048)
59 return -EINVAL;
60 if (kp->clipcount) {
Al Viro5b1a43d2006-02-01 05:24:20 -050061 struct v4l2_clip32 __user *uclips;
62 struct v4l2_clip __user *kclips;
Philippe De Muytercf664a62006-01-09 15:24:59 -020063 int n = kp->clipcount;
Al Viro5b1a43d2006-02-01 05:24:20 -050064 compat_caddr_t p;
Philippe De Muytercf664a62006-01-09 15:24:59 -020065
Al Viro5b1a43d2006-02-01 05:24:20 -050066 if (get_user(p, &up->clips))
67 return -EFAULT;
68 uclips = compat_ptr(p);
Philippe De Muytercf664a62006-01-09 15:24:59 -020069 kclips = compat_alloc_user_space(n * sizeof(struct v4l2_clip));
70 kp->clips = kclips;
71 while (--n >= 0) {
Al Viro5b1a43d2006-02-01 05:24:20 -050072 if (copy_in_user(&kclips->c, &uclips->c, sizeof(uclips->c)))
Guy Martina113bc72006-01-11 23:40:51 -020073 return -EFAULT;
Al Viro5b1a43d2006-02-01 05:24:20 -050074 if (put_user(n ? kclips + 1 : NULL, &kclips->next))
75 return -EFAULT;
Philippe De Muytercf664a62006-01-09 15:24:59 -020076 uclips += 1;
77 kclips += 1;
78 }
79 } else
Al Viro5b1a43d2006-02-01 05:24:20 -050080 kp->clips = NULL;
Philippe De Muytercf664a62006-01-09 15:24:59 -020081 return 0;
82}
83
84static int put_v4l2_window32(struct v4l2_window *kp, struct v4l2_window32 __user *up)
85{
Roel Kluinf2e6c6ad2009-12-16 11:35:45 -030086 if (copy_to_user(&up->w, &kp->w, sizeof(kp->w)) ||
Hans Verkuilb7b957d2018-01-24 04:35:48 -050087 put_user(kp->field, &up->field) ||
88 put_user(kp->chromakey, &up->chromakey) ||
89 put_user(kp->clipcount, &up->clipcount) ||
90 put_user(kp->global_alpha, &up->global_alpha))
91 return -EFAULT;
Philippe De Muytercf664a62006-01-09 15:24:59 -020092 return 0;
93}
94
Hans Verkuild1f81da2008-10-22 00:04:45 -030095struct v4l2_format32 {
Sakari Ailus6016af82012-05-10 02:02:07 -030096 __u32 type; /* enum v4l2_buf_type */
Hans Verkuild1f81da2008-10-22 00:04:45 -030097 union {
Hans Verkuil92f45ba2008-12-21 10:35:25 -030098 struct v4l2_pix_format pix;
Pawel Osciak52a30822010-07-29 14:56:47 -030099 struct v4l2_pix_format_mplane pix_mp;
Hans Verkuil92f45ba2008-12-21 10:35:25 -0300100 struct v4l2_window32 win;
101 struct v4l2_vbi_format vbi;
102 struct v4l2_sliced_vbi_format sliced;
Hans Verkuild5beb672015-09-17 06:46:04 -0300103 struct v4l2_sdr_format sdr;
Laurent Pinchartfb9ffa62016-04-12 19:40:46 -0300104 struct v4l2_meta_format meta;
Hans Verkuild1f81da2008-10-22 00:04:45 -0300105 __u8 raw_data[200]; /* user-defined */
Philippe De Muytercf664a62006-01-09 15:24:59 -0200106 } fmt;
107};
108
Guennadi Liakhovetski09362ec2011-09-07 18:07:23 -0300109/**
110 * struct v4l2_create_buffers32 - VIDIOC_CREATE_BUFS32 argument
111 * @index: on return, index of the first created buffer
112 * @count: entry: number of requested buffers,
113 * return: number of created buffers
114 * @memory: buffer memory type
115 * @format: frame format, for which buffers are requested
116 * @reserved: future extensions
117 */
Guennadi Liakhovetski21501582011-09-28 11:34:06 -0300118struct v4l2_create_buffers32 {
Guennadi Liakhovetski09362ec2011-09-07 18:07:23 -0300119 __u32 index;
Guennadi Liakhovetski21501582011-09-28 11:34:06 -0300120 __u32 count;
Sakari Ailus6016af82012-05-10 02:02:07 -0300121 __u32 memory; /* enum v4l2_memory */
Guennadi Liakhovetski09362ec2011-09-07 18:07:23 -0300122 struct v4l2_format32 format;
Guennadi Liakhovetski21501582011-09-28 11:34:06 -0300123 __u32 reserved[8];
124};
125
126static int __get_v4l2_format32(struct v4l2_format *kp, struct v4l2_format32 __user *up)
Philippe De Muytercf664a62006-01-09 15:24:59 -0200127{
Guennadi Liakhovetskiec775812014-04-26 12:51:31 -0300128 if (get_user(kp->type, &up->type))
129 return -EFAULT;
130
Philippe De Muytercf664a62006-01-09 15:24:59 -0200131 switch (kp->type) {
132 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
Hans Verkuil92f45ba2008-12-21 10:35:25 -0300133 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
Hans Verkuil486c5212018-01-24 04:45:29 -0500134 return copy_from_user(&kp->fmt.pix, &up->fmt.pix,
135 sizeof(kp->fmt.pix)) ? -EFAULT : 0;
Pawel Osciak52a30822010-07-29 14:56:47 -0300136 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
137 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
Hans Verkuil486c5212018-01-24 04:45:29 -0500138 return copy_from_user(&kp->fmt.pix_mp, &up->fmt.pix_mp,
139 sizeof(kp->fmt.pix_mp)) ? -EFAULT : 0;
Philippe De Muytercf664a62006-01-09 15:24:59 -0200140 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
Hans Verkuil92f45ba2008-12-21 10:35:25 -0300141 case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
Philippe De Muytercf664a62006-01-09 15:24:59 -0200142 return get_v4l2_window32(&kp->fmt.win, &up->fmt.win);
143 case V4L2_BUF_TYPE_VBI_CAPTURE:
Hans Verkuil92f45ba2008-12-21 10:35:25 -0300144 case V4L2_BUF_TYPE_VBI_OUTPUT:
Hans Verkuil486c5212018-01-24 04:45:29 -0500145 return copy_from_user(&kp->fmt.vbi, &up->fmt.vbi,
146 sizeof(kp->fmt.vbi)) ? -EFAULT : 0;
Hans Verkuil92f45ba2008-12-21 10:35:25 -0300147 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
148 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
Hans Verkuil486c5212018-01-24 04:45:29 -0500149 return copy_from_user(&kp->fmt.sliced, &up->fmt.sliced,
150 sizeof(kp->fmt.sliced)) ? -EFAULT : 0;
Hans Verkuild5beb672015-09-17 06:46:04 -0300151 case V4L2_BUF_TYPE_SDR_CAPTURE:
Antti Palosaari9effc722015-10-10 13:51:00 -0300152 case V4L2_BUF_TYPE_SDR_OUTPUT:
Hans Verkuil486c5212018-01-24 04:45:29 -0500153 return copy_from_user(&kp->fmt.sdr, &up->fmt.sdr,
154 sizeof(kp->fmt.sdr)) ? -EFAULT : 0;
Laurent Pinchartfb9ffa62016-04-12 19:40:46 -0300155 case V4L2_BUF_TYPE_META_CAPTURE:
Hans Verkuil486c5212018-01-24 04:45:29 -0500156 return copy_from_user(&kp->fmt.meta, &up->fmt.meta,
157 sizeof(kp->fmt.meta)) ? -EFAULT : 0;
Philippe De Muytercf664a62006-01-09 15:24:59 -0200158 default:
Hans Verkuild5beb672015-09-17 06:46:04 -0300159 pr_info("compat_ioctl32: unexpected VIDIOC_FMT type %d\n",
Hans Verkuilb7b957d2018-01-24 04:35:48 -0500160 kp->type);
Hans Verkuil92f45ba2008-12-21 10:35:25 -0300161 return -EINVAL;
Philippe De Muytercf664a62006-01-09 15:24:59 -0200162 }
163}
164
Guennadi Liakhovetski21501582011-09-28 11:34:06 -0300165static int get_v4l2_format32(struct v4l2_format *kp, struct v4l2_format32 __user *up)
Philippe De Muytercf664a62006-01-09 15:24:59 -0200166{
Guennadi Liakhovetskiec775812014-04-26 12:51:31 -0300167 if (!access_ok(VERIFY_READ, up, sizeof(struct v4l2_format32)))
168 return -EFAULT;
Guennadi Liakhovetski21501582011-09-28 11:34:06 -0300169 return __get_v4l2_format32(kp, up);
170}
171
172static int get_v4l2_create32(struct v4l2_create_buffers *kp, struct v4l2_create_buffers32 __user *up)
173{
174 if (!access_ok(VERIFY_READ, up, sizeof(struct v4l2_create_buffers32)) ||
Guennadi Liakhovetskiec775812014-04-26 12:51:31 -0300175 copy_from_user(kp, up, offsetof(struct v4l2_create_buffers32, format)))
176 return -EFAULT;
Guennadi Liakhovetski21501582011-09-28 11:34:06 -0300177 return __get_v4l2_format32(&kp->format, &up->format);
178}
179
180static int __put_v4l2_format32(struct v4l2_format *kp, struct v4l2_format32 __user *up)
181{
Guennadi Liakhovetski6ed9b282014-05-30 20:26:38 -0300182 if (put_user(kp->type, &up->type))
183 return -EFAULT;
184
Philippe De Muytercf664a62006-01-09 15:24:59 -0200185 switch (kp->type) {
186 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
Hans Verkuil92f45ba2008-12-21 10:35:25 -0300187 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
Hans Verkuil486c5212018-01-24 04:45:29 -0500188 return copy_to_user(&up->fmt.pix, &kp->fmt.pix,
189 sizeof(kp->fmt.pix)) ? -EFAULT : 0;
Pawel Osciak52a30822010-07-29 14:56:47 -0300190 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
191 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
Hans Verkuil486c5212018-01-24 04:45:29 -0500192 return copy_to_user(&up->fmt.pix_mp, &kp->fmt.pix_mp,
193 sizeof(kp->fmt.pix_mp)) ? -EFAULT : 0;
Philippe De Muytercf664a62006-01-09 15:24:59 -0200194 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
Hans Verkuil92f45ba2008-12-21 10:35:25 -0300195 case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
Philippe De Muytercf664a62006-01-09 15:24:59 -0200196 return put_v4l2_window32(&kp->fmt.win, &up->fmt.win);
197 case V4L2_BUF_TYPE_VBI_CAPTURE:
Hans Verkuil92f45ba2008-12-21 10:35:25 -0300198 case V4L2_BUF_TYPE_VBI_OUTPUT:
Hans Verkuil486c5212018-01-24 04:45:29 -0500199 return copy_to_user(&up->fmt.vbi, &kp->fmt.vbi,
200 sizeof(kp->fmt.vbi)) ? -EFAULT : 0;
Hans Verkuil92f45ba2008-12-21 10:35:25 -0300201 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
202 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
Hans Verkuil486c5212018-01-24 04:45:29 -0500203 return copy_to_user(&up->fmt.sliced, &kp->fmt.sliced,
204 sizeof(kp->fmt.sliced)) ? -EFAULT : 0;
Hans Verkuild5beb672015-09-17 06:46:04 -0300205 case V4L2_BUF_TYPE_SDR_CAPTURE:
Antti Palosaari9effc722015-10-10 13:51:00 -0300206 case V4L2_BUF_TYPE_SDR_OUTPUT:
Hans Verkuil486c5212018-01-24 04:45:29 -0500207 return copy_to_user(&up->fmt.sdr, &kp->fmt.sdr,
208 sizeof(kp->fmt.sdr)) ? -EFAULT : 0;
Laurent Pinchartfb9ffa62016-04-12 19:40:46 -0300209 case V4L2_BUF_TYPE_META_CAPTURE:
Hans Verkuil486c5212018-01-24 04:45:29 -0500210 return copy_to_user(&up->fmt.meta, &kp->fmt.meta,
211 sizeof(kp->fmt.meta)) ? -EFAULT : 0;
Philippe De Muytercf664a62006-01-09 15:24:59 -0200212 default:
Hans Verkuild5beb672015-09-17 06:46:04 -0300213 pr_info("compat_ioctl32: unexpected VIDIOC_FMT type %d\n",
Hans Verkuilb7b957d2018-01-24 04:35:48 -0500214 kp->type);
Hans Verkuil92f45ba2008-12-21 10:35:25 -0300215 return -EINVAL;
Philippe De Muytercf664a62006-01-09 15:24:59 -0200216 }
217}
218
Guennadi Liakhovetski21501582011-09-28 11:34:06 -0300219static int put_v4l2_format32(struct v4l2_format *kp, struct v4l2_format32 __user *up)
220{
Guennadi Liakhovetski6ed9b282014-05-30 20:26:38 -0300221 if (!access_ok(VERIFY_WRITE, up, sizeof(struct v4l2_format32)))
Guennadi Liakhovetski21501582011-09-28 11:34:06 -0300222 return -EFAULT;
223 return __put_v4l2_format32(kp, up);
224}
225
226static int put_v4l2_create32(struct v4l2_create_buffers *kp, struct v4l2_create_buffers32 __user *up)
227{
228 if (!access_ok(VERIFY_WRITE, up, sizeof(struct v4l2_create_buffers32)) ||
Tiffany Linbaf43c62016-03-14 08:16:14 -0300229 copy_to_user(up, kp, offsetof(struct v4l2_create_buffers32, format)) ||
230 copy_to_user(up->reserved, kp->reserved, sizeof(kp->reserved)))
Guennadi Liakhovetski6ed9b282014-05-30 20:26:38 -0300231 return -EFAULT;
Guennadi Liakhovetski21501582011-09-28 11:34:06 -0300232 return __put_v4l2_format32(&kp->format, &up->format);
233}
234
Hans Verkuild1f81da2008-10-22 00:04:45 -0300235struct v4l2_standard32 {
Philippe De Muytercf664a62006-01-09 15:24:59 -0200236 __u32 index;
Andrzej Hajda655e9782015-08-31 08:56:15 -0300237 compat_u64 id;
Philippe De Muytercf664a62006-01-09 15:24:59 -0200238 __u8 name[24];
239 struct v4l2_fract frameperiod; /* Frames, not fields */
240 __u32 framelines;
241 __u32 reserved[4];
242};
243
244static int get_v4l2_standard32(struct v4l2_standard *kp, struct v4l2_standard32 __user *up)
245{
246 /* other fields are not set by the user, nor used by the driver */
Guy Martina113bc72006-01-11 23:40:51 -0200247 if (!access_ok(VERIFY_READ, up, sizeof(struct v4l2_standard32)) ||
Hans Verkuilb7b957d2018-01-24 04:35:48 -0500248 get_user(kp->index, &up->index))
Guy Martina113bc72006-01-11 23:40:51 -0200249 return -EFAULT;
250 return 0;
Philippe De Muytercf664a62006-01-09 15:24:59 -0200251}
252
253static int put_v4l2_standard32(struct v4l2_standard *kp, struct v4l2_standard32 __user *up)
254{
Hans Verkuild1f81da2008-10-22 00:04:45 -0300255 if (!access_ok(VERIFY_WRITE, up, sizeof(struct v4l2_standard32)) ||
Hans Verkuilb7b957d2018-01-24 04:35:48 -0500256 put_user(kp->index, &up->index) ||
257 put_user(kp->id, &up->id) ||
258 copy_to_user(up->name, kp->name, 24) ||
259 copy_to_user(&up->frameperiod, &kp->frameperiod,
260 sizeof(kp->frameperiod)) ||
261 put_user(kp->framelines, &up->framelines) ||
262 copy_to_user(up->reserved, kp->reserved, 4 * sizeof(__u32)))
263 return -EFAULT;
Guy Martina113bc72006-01-11 23:40:51 -0200264 return 0;
265}
266
Pawel Osciak52a30822010-07-29 14:56:47 -0300267struct v4l2_plane32 {
268 __u32 bytesused;
269 __u32 length;
270 union {
271 __u32 mem_offset;
272 compat_long_t userptr;
Sumit Semwal051c7782012-06-14 10:37:35 -0300273 __s32 fd;
Pawel Osciak52a30822010-07-29 14:56:47 -0300274 } m;
275 __u32 data_offset;
276 __u32 reserved[11];
277};
278
Hans Verkuild1f81da2008-10-22 00:04:45 -0300279struct v4l2_buffer32 {
Philippe De Muytercf664a62006-01-09 15:24:59 -0200280 __u32 index;
Sakari Ailus6016af82012-05-10 02:02:07 -0300281 __u32 type; /* enum v4l2_buf_type */
Philippe De Muytercf664a62006-01-09 15:24:59 -0200282 __u32 bytesused;
283 __u32 flags;
Sakari Ailus6016af82012-05-10 02:02:07 -0300284 __u32 field; /* enum v4l2_field */
Philippe De Muytercf664a62006-01-09 15:24:59 -0200285 struct compat_timeval timestamp;
286 struct v4l2_timecode timecode;
287 __u32 sequence;
288
289 /* memory location */
Sakari Ailus6016af82012-05-10 02:02:07 -0300290 __u32 memory; /* enum v4l2_memory */
Philippe De Muytercf664a62006-01-09 15:24:59 -0200291 union {
292 __u32 offset;
293 compat_long_t userptr;
Pawel Osciak52a30822010-07-29 14:56:47 -0300294 compat_caddr_t planes;
Sumit Semwal051c7782012-06-14 10:37:35 -0300295 __s32 fd;
Philippe De Muytercf664a62006-01-09 15:24:59 -0200296 } m;
297 __u32 length;
Sakari Ailus2b719d72012-05-02 09:40:03 -0300298 __u32 reserved2;
Philippe De Muytercf664a62006-01-09 15:24:59 -0200299 __u32 reserved;
300};
301
Hans Verkuil8ae632b2014-08-21 17:07:21 -0300302static int get_v4l2_plane32(struct v4l2_plane __user *up, struct v4l2_plane32 __user *up32,
Hans Verkuilb7b957d2018-01-24 04:35:48 -0500303 enum v4l2_memory memory)
Pawel Osciak52a30822010-07-29 14:56:47 -0300304{
305 void __user *up_pln;
306 compat_long_t p;
307
308 if (copy_in_user(up, up32, 2 * sizeof(__u32)) ||
Hans Verkuilb7b957d2018-01-24 04:35:48 -0500309 copy_in_user(&up->data_offset, &up32->data_offset,
310 sizeof(__u32)))
Pawel Osciak52a30822010-07-29 14:56:47 -0300311 return -EFAULT;
312
313 if (memory == V4L2_MEMORY_USERPTR) {
314 if (get_user(p, &up32->m.userptr))
315 return -EFAULT;
316 up_pln = compat_ptr(p);
317 if (put_user((unsigned long)up_pln, &up->m.userptr))
318 return -EFAULT;
Sumit Semwal051c7782012-06-14 10:37:35 -0300319 } else if (memory == V4L2_MEMORY_DMABUF) {
320 if (copy_in_user(&up->m.fd, &up32->m.fd, sizeof(int)))
321 return -EFAULT;
Pawel Osciak52a30822010-07-29 14:56:47 -0300322 } else {
323 if (copy_in_user(&up->m.mem_offset, &up32->m.mem_offset,
Hans Verkuilb7b957d2018-01-24 04:35:48 -0500324 sizeof(__u32)))
Pawel Osciak52a30822010-07-29 14:56:47 -0300325 return -EFAULT;
326 }
327
328 return 0;
329}
330
Hans Verkuil8ae632b2014-08-21 17:07:21 -0300331static int put_v4l2_plane32(struct v4l2_plane __user *up, struct v4l2_plane32 __user *up32,
Hans Verkuilb7b957d2018-01-24 04:35:48 -0500332 enum v4l2_memory memory)
Pawel Osciak52a30822010-07-29 14:56:47 -0300333{
334 if (copy_in_user(up32, up, 2 * sizeof(__u32)) ||
Hans Verkuilb7b957d2018-01-24 04:35:48 -0500335 copy_in_user(&up32->data_offset, &up->data_offset,
336 sizeof(__u32)))
Pawel Osciak52a30822010-07-29 14:56:47 -0300337 return -EFAULT;
338
339 /* For MMAP, driver might've set up the offset, so copy it back.
340 * USERPTR stays the same (was userspace-provided), so no copying. */
341 if (memory == V4L2_MEMORY_MMAP)
342 if (copy_in_user(&up32->m.mem_offset, &up->m.mem_offset,
Hans Verkuilb7b957d2018-01-24 04:35:48 -0500343 sizeof(__u32)))
Pawel Osciak52a30822010-07-29 14:56:47 -0300344 return -EFAULT;
Sumit Semwal051c7782012-06-14 10:37:35 -0300345 /* For DMABUF, driver might've set up the fd, so copy it back. */
346 if (memory == V4L2_MEMORY_DMABUF)
347 if (copy_in_user(&up32->m.fd, &up->m.fd,
Hans Verkuilb7b957d2018-01-24 04:35:48 -0500348 sizeof(int)))
Sumit Semwal051c7782012-06-14 10:37:35 -0300349 return -EFAULT;
Pawel Osciak52a30822010-07-29 14:56:47 -0300350
351 return 0;
352}
353
Philippe De Muytercf664a62006-01-09 15:24:59 -0200354static int get_v4l2_buffer32(struct v4l2_buffer *kp, struct v4l2_buffer32 __user *up)
355{
Pawel Osciak52a30822010-07-29 14:56:47 -0300356 struct v4l2_plane32 __user *uplane32;
357 struct v4l2_plane __user *uplane;
358 compat_caddr_t p;
Pawel Osciak52a30822010-07-29 14:56:47 -0300359 int ret;
Philippe De Muytercf664a62006-01-09 15:24:59 -0200360
Guy Martina113bc72006-01-11 23:40:51 -0200361 if (!access_ok(VERIFY_READ, up, sizeof(struct v4l2_buffer32)) ||
Hans Verkuilb7b957d2018-01-24 04:35:48 -0500362 get_user(kp->index, &up->index) ||
363 get_user(kp->type, &up->type) ||
364 get_user(kp->flags, &up->flags) ||
365 get_user(kp->memory, &up->memory) ||
366 get_user(kp->length, &up->length))
367 return -EFAULT;
Philippe De Muytercf664a62006-01-09 15:24:59 -0200368
Pawel Osciak52a30822010-07-29 14:56:47 -0300369 if (V4L2_TYPE_IS_OUTPUT(kp->type))
370 if (get_user(kp->bytesused, &up->bytesused) ||
Hans Verkuilb7b957d2018-01-24 04:35:48 -0500371 get_user(kp->field, &up->field) ||
372 get_user(kp->timestamp.tv_sec, &up->timestamp.tv_sec) ||
373 get_user(kp->timestamp.tv_usec,
374 &up->timestamp.tv_usec))
Al Viro5b1a43d2006-02-01 05:24:20 -0500375 return -EFAULT;
376
Pawel Osciak52a30822010-07-29 14:56:47 -0300377 if (V4L2_TYPE_IS_MULTIPLANAR(kp->type)) {
Sakari Ailusa56bc172016-11-08 14:06:44 -0200378 unsigned int num_planes;
379
380 if (kp->length == 0) {
Pawel Osciak52a30822010-07-29 14:56:47 -0300381 kp->m.planes = NULL;
382 /* num_planes == 0 is legal, e.g. when userspace doesn't
383 * need planes array on DQBUF*/
384 return 0;
Sakari Ailusa56bc172016-11-08 14:06:44 -0200385 } else if (kp->length > VIDEO_MAX_PLANES) {
386 return -EINVAL;
Philippe De Muytercf664a62006-01-09 15:24:59 -0200387 }
Pawel Osciak52a30822010-07-29 14:56:47 -0300388
389 if (get_user(p, &up->m.planes))
Guy Martina113bc72006-01-11 23:40:51 -0200390 return -EFAULT;
Pawel Osciak52a30822010-07-29 14:56:47 -0300391
392 uplane32 = compat_ptr(p);
393 if (!access_ok(VERIFY_READ, uplane32,
Hans Verkuilb7b957d2018-01-24 04:35:48 -0500394 kp->length * sizeof(struct v4l2_plane32)))
Pawel Osciak52a30822010-07-29 14:56:47 -0300395 return -EFAULT;
396
397 /* We don't really care if userspace decides to kill itself
398 * by passing a very big num_planes value */
Sakari Ailusa56bc172016-11-08 14:06:44 -0200399 uplane = compat_alloc_user_space(kp->length *
400 sizeof(struct v4l2_plane));
Hans Verkuil8ae632b2014-08-21 17:07:21 -0300401 kp->m.planes = (__force struct v4l2_plane *)uplane;
Pawel Osciak52a30822010-07-29 14:56:47 -0300402
Sakari Ailusa56bc172016-11-08 14:06:44 -0200403 for (num_planes = 0; num_planes < kp->length; num_planes++) {
Pawel Osciak52a30822010-07-29 14:56:47 -0300404 ret = get_v4l2_plane32(uplane, uplane32, kp->memory);
405 if (ret)
406 return ret;
407 ++uplane;
408 ++uplane32;
409 }
410 } else {
411 switch (kp->memory) {
412 case V4L2_MEMORY_MMAP:
Tiffany Lin7df5ab82016-01-19 05:56:50 -0200413 if (get_user(kp->m.offset, &up->m.offset))
Pawel Osciak52a30822010-07-29 14:56:47 -0300414 return -EFAULT;
415 break;
416 case V4L2_MEMORY_USERPTR:
417 {
Hans Verkuilb7b957d2018-01-24 04:35:48 -0500418 compat_long_t tmp;
Pawel Osciak52a30822010-07-29 14:56:47 -0300419
Hans Verkuilb7b957d2018-01-24 04:35:48 -0500420 if (get_user(tmp, &up->m.userptr))
421 return -EFAULT;
Pawel Osciak52a30822010-07-29 14:56:47 -0300422
Hans Verkuilb7b957d2018-01-24 04:35:48 -0500423 kp->m.userptr = (unsigned long)compat_ptr(tmp);
Pawel Osciak52a30822010-07-29 14:56:47 -0300424 }
425 break;
426 case V4L2_MEMORY_OVERLAY:
427 if (get_user(kp->m.offset, &up->m.offset))
428 return -EFAULT;
429 break;
Sumit Semwal051c7782012-06-14 10:37:35 -0300430 case V4L2_MEMORY_DMABUF:
431 if (get_user(kp->m.fd, &up->m.fd))
432 return -EFAULT;
433 break;
Pawel Osciak52a30822010-07-29 14:56:47 -0300434 }
Philippe De Muytercf664a62006-01-09 15:24:59 -0200435 }
Pawel Osciak52a30822010-07-29 14:56:47 -0300436
Philippe De Muytercf664a62006-01-09 15:24:59 -0200437 return 0;
438}
439
440static int put_v4l2_buffer32(struct v4l2_buffer *kp, struct v4l2_buffer32 __user *up)
441{
Pawel Osciak52a30822010-07-29 14:56:47 -0300442 struct v4l2_plane32 __user *uplane32;
443 struct v4l2_plane __user *uplane;
444 compat_caddr_t p;
445 int num_planes;
446 int ret;
447
Guy Martina113bc72006-01-11 23:40:51 -0200448 if (!access_ok(VERIFY_WRITE, up, sizeof(struct v4l2_buffer32)) ||
Hans Verkuilb7b957d2018-01-24 04:35:48 -0500449 put_user(kp->index, &up->index) ||
450 put_user(kp->type, &up->type) ||
451 put_user(kp->flags, &up->flags) ||
452 put_user(kp->memory, &up->memory))
453 return -EFAULT;
Pawel Osciak52a30822010-07-29 14:56:47 -0300454
Guy Martina113bc72006-01-11 23:40:51 -0200455 if (put_user(kp->bytesused, &up->bytesused) ||
Hans Verkuilb7b957d2018-01-24 04:35:48 -0500456 put_user(kp->field, &up->field) ||
457 put_user(kp->timestamp.tv_sec, &up->timestamp.tv_sec) ||
458 put_user(kp->timestamp.tv_usec, &up->timestamp.tv_usec) ||
459 copy_to_user(&up->timecode, &kp->timecode,
460 sizeof(struct v4l2_timecode)) ||
461 put_user(kp->sequence, &up->sequence) ||
462 put_user(kp->reserved2, &up->reserved2) ||
463 put_user(kp->reserved, &up->reserved) ||
464 put_user(kp->length, &up->length))
465 return -EFAULT;
Pawel Osciak52a30822010-07-29 14:56:47 -0300466
467 if (V4L2_TYPE_IS_MULTIPLANAR(kp->type)) {
468 num_planes = kp->length;
469 if (num_planes == 0)
470 return 0;
471
Hans Verkuil8ae632b2014-08-21 17:07:21 -0300472 uplane = (__force struct v4l2_plane __user *)kp->m.planes;
Pawel Osciak52a30822010-07-29 14:56:47 -0300473 if (get_user(p, &up->m.planes))
474 return -EFAULT;
475 uplane32 = compat_ptr(p);
476
477 while (--num_planes >= 0) {
478 ret = put_v4l2_plane32(uplane, uplane32, kp->memory);
479 if (ret)
480 return ret;
481 ++uplane;
482 ++uplane32;
483 }
484 } else {
485 switch (kp->memory) {
486 case V4L2_MEMORY_MMAP:
Tiffany Lin7df5ab82016-01-19 05:56:50 -0200487 if (put_user(kp->m.offset, &up->m.offset))
Pawel Osciak52a30822010-07-29 14:56:47 -0300488 return -EFAULT;
489 break;
490 case V4L2_MEMORY_USERPTR:
Tiffany Lin7df5ab82016-01-19 05:56:50 -0200491 if (put_user(kp->m.userptr, &up->m.userptr))
Pawel Osciak52a30822010-07-29 14:56:47 -0300492 return -EFAULT;
493 break;
494 case V4L2_MEMORY_OVERLAY:
495 if (put_user(kp->m.offset, &up->m.offset))
496 return -EFAULT;
497 break;
Sumit Semwal051c7782012-06-14 10:37:35 -0300498 case V4L2_MEMORY_DMABUF:
499 if (put_user(kp->m.fd, &up->m.fd))
500 return -EFAULT;
501 break;
Pawel Osciak52a30822010-07-29 14:56:47 -0300502 }
503 }
504
Philippe De Muytercf664a62006-01-09 15:24:59 -0200505 return 0;
506}
507
Hans Verkuild1f81da2008-10-22 00:04:45 -0300508struct v4l2_framebuffer32 {
Philippe De Muytercf664a62006-01-09 15:24:59 -0200509 __u32 capability;
510 __u32 flags;
Mauro Carvalho Chehab6e6a8b52018-01-04 13:08:56 -0500511 compat_caddr_t base;
Laurent Pinchartd52e2382014-05-27 09:41:05 -0300512 struct {
513 __u32 width;
514 __u32 height;
515 __u32 pixelformat;
516 __u32 field;
517 __u32 bytesperline;
518 __u32 sizeimage;
519 __u32 colorspace;
520 __u32 priv;
521 } fmt;
Philippe De Muytercf664a62006-01-09 15:24:59 -0200522};
523
Philippe De Muyter13d133b2006-01-09 15:25:00 -0200524static int get_v4l2_framebuffer32(struct v4l2_framebuffer *kp, struct v4l2_framebuffer32 __user *up)
525{
526 u32 tmp;
527
Guy Martina113bc72006-01-11 23:40:51 -0200528 if (!access_ok(VERIFY_READ, up, sizeof(struct v4l2_framebuffer32)) ||
Hans Verkuilb7b957d2018-01-24 04:35:48 -0500529 get_user(tmp, &up->base) ||
530 get_user(kp->capability, &up->capability) ||
531 get_user(kp->flags, &up->flags) ||
532 copy_from_user(&kp->fmt, &up->fmt, sizeof(up->fmt)))
533 return -EFAULT;
Hans Verkuil8ae632b2014-08-21 17:07:21 -0300534 kp->base = (__force void *)compat_ptr(tmp);
Philippe De Muyter13d133b2006-01-09 15:25:00 -0200535 return 0;
536}
537
Philippe De Muytercf664a62006-01-09 15:24:59 -0200538static int put_v4l2_framebuffer32(struct v4l2_framebuffer *kp, struct v4l2_framebuffer32 __user *up)
539{
540 u32 tmp = (u32)((unsigned long)kp->base);
541
Hans Verkuild1f81da2008-10-22 00:04:45 -0300542 if (!access_ok(VERIFY_WRITE, up, sizeof(struct v4l2_framebuffer32)) ||
Hans Verkuilb7b957d2018-01-24 04:35:48 -0500543 put_user(tmp, &up->base) ||
544 put_user(kp->capability, &up->capability) ||
545 put_user(kp->flags, &up->flags) ||
546 copy_to_user(&up->fmt, &kp->fmt, sizeof(up->fmt)))
547 return -EFAULT;
Philippe De Muytercf664a62006-01-09 15:24:59 -0200548 return 0;
549}
550
Hans Verkuil92f45ba2008-12-21 10:35:25 -0300551struct v4l2_input32 {
552 __u32 index; /* Which input */
553 __u8 name[32]; /* Label */
554 __u32 type; /* Type of input */
555 __u32 audioset; /* Associated audios (bitfield) */
556 __u32 tuner; /* Associated tuner */
Andrzej Hajda655e9782015-08-31 08:56:15 -0300557 compat_u64 std;
Hans Verkuil92f45ba2008-12-21 10:35:25 -0300558 __u32 status;
Hans Verkuil037e0862017-08-04 07:25:06 -0400559 __u32 capabilities;
560 __u32 reserved[3];
Andrzej Hajda655e9782015-08-31 08:56:15 -0300561};
Hans Verkuil92f45ba2008-12-21 10:35:25 -0300562
563/* The 64-bit v4l2_input struct has extra padding at the end of the struct.
564 Otherwise it is identical to the 32-bit version. */
565static inline int get_v4l2_input32(struct v4l2_input *kp, struct v4l2_input32 __user *up)
Philippe De Muytercf664a62006-01-09 15:24:59 -0200566{
Hans Verkuil92f45ba2008-12-21 10:35:25 -0300567 if (copy_from_user(kp, up, sizeof(struct v4l2_input32)))
Al Viro5b1a43d2006-02-01 05:24:20 -0500568 return -EFAULT;
Guy Martina113bc72006-01-11 23:40:51 -0200569 return 0;
570}
571
Hans Verkuil92f45ba2008-12-21 10:35:25 -0300572static inline int put_v4l2_input32(struct v4l2_input *kp, struct v4l2_input32 __user *up)
Guy Martina113bc72006-01-11 23:40:51 -0200573{
Hans Verkuil92f45ba2008-12-21 10:35:25 -0300574 if (copy_to_user(up, kp, sizeof(struct v4l2_input32)))
Al Viro5b1a43d2006-02-01 05:24:20 -0500575 return -EFAULT;
Guy Martina113bc72006-01-11 23:40:51 -0200576 return 0;
577}
578
Hans Verkuil92f45ba2008-12-21 10:35:25 -0300579struct v4l2_ext_controls32 {
Ricardo Ribalda0f8017b2015-10-29 08:10:28 -0200580 __u32 which;
Ricardo Ribalda28c50292015-08-21 10:19:22 -0300581 __u32 count;
582 __u32 error_idx;
583 __u32 reserved[2];
584 compat_caddr_t controls; /* actually struct v4l2_ext_control32 * */
Mauro Carvalho Chehabeb4eecc2006-07-21 18:53:23 -0300585};
586
Hans Verkuil6b5a9492009-08-11 18:47:18 -0300587struct v4l2_ext_control32 {
588 __u32 id;
589 __u32 size;
590 __u32 reserved2[1];
591 union {
592 __s32 value;
593 __s64 value64;
594 compat_caddr_t string; /* actually char * */
595 };
596} __attribute__ ((packed));
597
598/* The following function really belong in v4l2-common, but that causes
599 a circular dependency between modules. We need to think about this, but
600 for now this will do. */
601
602/* Return non-zero if this control is a pointer type. Currently only
Eduardo Valentinfdf82dc2009-08-11 18:49:12 -0300603 type STRING is a pointer type. */
Hans Verkuil6b5a9492009-08-11 18:47:18 -0300604static inline int ctrl_is_pointer(u32 id)
605{
Eduardo Valentinfdf82dc2009-08-11 18:49:12 -0300606 switch (id) {
607 case V4L2_CID_RDS_TX_PS_NAME:
608 case V4L2_CID_RDS_TX_RADIO_TEXT:
609 return 1;
610 default:
611 return 0;
612 }
Hans Verkuil6b5a9492009-08-11 18:47:18 -0300613}
614
Hans Verkuil92f45ba2008-12-21 10:35:25 -0300615static int get_v4l2_ext_controls32(struct v4l2_ext_controls *kp, struct v4l2_ext_controls32 __user *up)
Mauro Carvalho Chehabeb4eecc2006-07-21 18:53:23 -0300616{
Hans Verkuil6b5a9492009-08-11 18:47:18 -0300617 struct v4l2_ext_control32 __user *ucontrols;
Hans Verkuil92f45ba2008-12-21 10:35:25 -0300618 struct v4l2_ext_control __user *kcontrols;
Sakari Ailusa56bc172016-11-08 14:06:44 -0200619 unsigned int n;
Hans Verkuil92f45ba2008-12-21 10:35:25 -0300620 compat_caddr_t p;
621
622 if (!access_ok(VERIFY_READ, up, sizeof(struct v4l2_ext_controls32)) ||
Hans Verkuilb7b957d2018-01-24 04:35:48 -0500623 get_user(kp->which, &up->which) ||
624 get_user(kp->count, &up->count) ||
625 get_user(kp->error_idx, &up->error_idx) ||
626 copy_from_user(kp->reserved, up->reserved,
627 sizeof(kp->reserved)))
628 return -EFAULT;
Sakari Ailusa56bc172016-11-08 14:06:44 -0200629 if (kp->count == 0) {
Hans Verkuil92f45ba2008-12-21 10:35:25 -0300630 kp->controls = NULL;
631 return 0;
Sakari Ailusa56bc172016-11-08 14:06:44 -0200632 } else if (kp->count > V4L2_CID_MAX_CTRLS) {
633 return -EINVAL;
Hans Verkuil92f45ba2008-12-21 10:35:25 -0300634 }
635 if (get_user(p, &up->controls))
636 return -EFAULT;
637 ucontrols = compat_ptr(p);
Pawel Osciak52a30822010-07-29 14:56:47 -0300638 if (!access_ok(VERIFY_READ, ucontrols,
Hans Verkuilb7b957d2018-01-24 04:35:48 -0500639 kp->count * sizeof(struct v4l2_ext_control32)))
Hans Verkuil92f45ba2008-12-21 10:35:25 -0300640 return -EFAULT;
Sakari Ailusa56bc172016-11-08 14:06:44 -0200641 kcontrols = compat_alloc_user_space(kp->count *
642 sizeof(struct v4l2_ext_control));
Hans Verkuil8ae632b2014-08-21 17:07:21 -0300643 kp->controls = (__force struct v4l2_ext_control *)kcontrols;
Sakari Ailusa56bc172016-11-08 14:06:44 -0200644 for (n = 0; n < kp->count; n++) {
Hans Verkuil8ae632b2014-08-21 17:07:21 -0300645 u32 id;
646
Pawel Osciak52a30822010-07-29 14:56:47 -0300647 if (copy_in_user(kcontrols, ucontrols, sizeof(*ucontrols)))
Hans Verkuil92f45ba2008-12-21 10:35:25 -0300648 return -EFAULT;
Hans Verkuil8ae632b2014-08-21 17:07:21 -0300649 if (get_user(id, &kcontrols->id))
650 return -EFAULT;
651 if (ctrl_is_pointer(id)) {
Hans Verkuil6b5a9492009-08-11 18:47:18 -0300652 void __user *s;
653
654 if (get_user(p, &ucontrols->string))
655 return -EFAULT;
656 s = compat_ptr(p);
657 if (put_user(s, &kcontrols->string))
658 return -EFAULT;
659 }
Hans Verkuil92f45ba2008-12-21 10:35:25 -0300660 ucontrols++;
661 kcontrols++;
662 }
Mauro Carvalho Chehabeb4eecc2006-07-21 18:53:23 -0300663 return 0;
664}
665
Hans Verkuil92f45ba2008-12-21 10:35:25 -0300666static int put_v4l2_ext_controls32(struct v4l2_ext_controls *kp, struct v4l2_ext_controls32 __user *up)
Arnd Bergmann0d0fbf82006-01-09 15:24:57 -0200667{
Hans Verkuil6b5a9492009-08-11 18:47:18 -0300668 struct v4l2_ext_control32 __user *ucontrols;
Hans Verkuil8ae632b2014-08-21 17:07:21 -0300669 struct v4l2_ext_control __user *kcontrols =
670 (__force struct v4l2_ext_control __user *)kp->controls;
Hans Verkuil92f45ba2008-12-21 10:35:25 -0300671 int n = kp->count;
672 compat_caddr_t p;
Arnd Bergmann0d0fbf82006-01-09 15:24:57 -0200673
Hans Verkuil92f45ba2008-12-21 10:35:25 -0300674 if (!access_ok(VERIFY_WRITE, up, sizeof(struct v4l2_ext_controls32)) ||
Hans Verkuilb7b957d2018-01-24 04:35:48 -0500675 put_user(kp->which, &up->which) ||
676 put_user(kp->count, &up->count) ||
677 put_user(kp->error_idx, &up->error_idx) ||
678 copy_to_user(up->reserved, kp->reserved, sizeof(up->reserved)))
679 return -EFAULT;
Hans Verkuil92f45ba2008-12-21 10:35:25 -0300680 if (!kp->count)
681 return 0;
682
683 if (get_user(p, &up->controls))
684 return -EFAULT;
685 ucontrols = compat_ptr(p);
Pawel Osciak52a30822010-07-29 14:56:47 -0300686 if (!access_ok(VERIFY_WRITE, ucontrols,
Hans Verkuilb7b957d2018-01-24 04:35:48 -0500687 n * sizeof(struct v4l2_ext_control32)))
Guy Martina113bc72006-01-11 23:40:51 -0200688 return -EFAULT;
689
Hans Verkuil92f45ba2008-12-21 10:35:25 -0300690 while (--n >= 0) {
Hans Verkuil6b5a9492009-08-11 18:47:18 -0300691 unsigned size = sizeof(*ucontrols);
Hans Verkuil8ae632b2014-08-21 17:07:21 -0300692 u32 id;
Hans Verkuil6b5a9492009-08-11 18:47:18 -0300693
Hans Verkuil8ae632b2014-08-21 17:07:21 -0300694 if (get_user(id, &kcontrols->id))
695 return -EFAULT;
Hans Verkuil6b5a9492009-08-11 18:47:18 -0300696 /* Do not modify the pointer when copying a pointer control.
697 The contents of the pointer was changed, not the pointer
698 itself. */
Hans Verkuil8ae632b2014-08-21 17:07:21 -0300699 if (ctrl_is_pointer(id))
Hans Verkuil6b5a9492009-08-11 18:47:18 -0300700 size -= sizeof(ucontrols->value64);
701 if (copy_in_user(ucontrols, kcontrols, size))
Hans Verkuil92f45ba2008-12-21 10:35:25 -0300702 return -EFAULT;
703 ucontrols++;
704 kcontrols++;
Arnd Bergmann0d0fbf82006-01-09 15:24:57 -0200705 }
Hans Verkuil92f45ba2008-12-21 10:35:25 -0300706 return 0;
Arnd Bergmann0d0fbf82006-01-09 15:24:57 -0200707}
Hans Verkuil92f45ba2008-12-21 10:35:25 -0300708
Hans Verkuil2330fb82011-06-07 11:43:57 -0300709struct v4l2_event32 {
710 __u32 type;
711 union {
Andrzej Hajda655e9782015-08-31 08:56:15 -0300712 compat_s64 value64;
Hans Verkuil2330fb82011-06-07 11:43:57 -0300713 __u8 data[64];
714 } u;
715 __u32 pending;
716 __u32 sequence;
717 struct compat_timespec timestamp;
718 __u32 id;
719 __u32 reserved[8];
720};
721
722static int put_v4l2_event32(struct v4l2_event *kp, struct v4l2_event32 __user *up)
723{
724 if (!access_ok(VERIFY_WRITE, up, sizeof(struct v4l2_event32)) ||
Hans Verkuilb7b957d2018-01-24 04:35:48 -0500725 put_user(kp->type, &up->type) ||
726 copy_to_user(&up->u, &kp->u, sizeof(kp->u)) ||
727 put_user(kp->pending, &up->pending) ||
728 put_user(kp->sequence, &up->sequence) ||
729 put_user(kp->timestamp.tv_sec, &up->timestamp.tv_sec) ||
730 put_user(kp->timestamp.tv_nsec, &up->timestamp.tv_nsec) ||
731 put_user(kp->id, &up->id) ||
732 copy_to_user(up->reserved, kp->reserved, 8 * sizeof(__u32)))
733 return -EFAULT;
Hans Verkuil2330fb82011-06-07 11:43:57 -0300734 return 0;
735}
736
Hans Verkuildd519bb2014-03-07 07:18:37 -0300737struct v4l2_edid32 {
Hans Verkuiled45ce22012-08-10 06:07:12 -0300738 __u32 pad;
739 __u32 start_block;
740 __u32 blocks;
741 __u32 reserved[5];
742 compat_caddr_t edid;
743};
744
Hans Verkuildd519bb2014-03-07 07:18:37 -0300745static int get_v4l2_edid32(struct v4l2_edid *kp, struct v4l2_edid32 __user *up)
Hans Verkuiled45ce22012-08-10 06:07:12 -0300746{
747 u32 tmp;
748
Hans Verkuildd519bb2014-03-07 07:18:37 -0300749 if (!access_ok(VERIFY_READ, up, sizeof(struct v4l2_edid32)) ||
Hans Verkuilb7b957d2018-01-24 04:35:48 -0500750 get_user(kp->pad, &up->pad) ||
751 get_user(kp->start_block, &up->start_block) ||
752 get_user(kp->blocks, &up->blocks) ||
753 get_user(tmp, &up->edid) ||
754 copy_from_user(kp->reserved, up->reserved, sizeof(kp->reserved)))
755 return -EFAULT;
Hans Verkuil8ae632b2014-08-21 17:07:21 -0300756 kp->edid = (__force u8 *)compat_ptr(tmp);
Hans Verkuiled45ce22012-08-10 06:07:12 -0300757 return 0;
758}
759
Hans Verkuildd519bb2014-03-07 07:18:37 -0300760static int put_v4l2_edid32(struct v4l2_edid *kp, struct v4l2_edid32 __user *up)
Hans Verkuiled45ce22012-08-10 06:07:12 -0300761{
762 u32 tmp = (u32)((unsigned long)kp->edid);
763
Hans Verkuildd519bb2014-03-07 07:18:37 -0300764 if (!access_ok(VERIFY_WRITE, up, sizeof(struct v4l2_edid32)) ||
Hans Verkuilb7b957d2018-01-24 04:35:48 -0500765 put_user(kp->pad, &up->pad) ||
766 put_user(kp->start_block, &up->start_block) ||
767 put_user(kp->blocks, &up->blocks) ||
768 put_user(tmp, &up->edid) ||
769 copy_to_user(up->reserved, kp->reserved, sizeof(up->reserved)))
770 return -EFAULT;
Hans Verkuiled45ce22012-08-10 06:07:12 -0300771 return 0;
772}
773
774
Hans Verkuil92f45ba2008-12-21 10:35:25 -0300775#define VIDIOC_G_FMT32 _IOWR('V', 4, struct v4l2_format32)
776#define VIDIOC_S_FMT32 _IOWR('V', 5, struct v4l2_format32)
777#define VIDIOC_QUERYBUF32 _IOWR('V', 9, struct v4l2_buffer32)
778#define VIDIOC_G_FBUF32 _IOR ('V', 10, struct v4l2_framebuffer32)
779#define VIDIOC_S_FBUF32 _IOW ('V', 11, struct v4l2_framebuffer32)
780#define VIDIOC_QBUF32 _IOWR('V', 15, struct v4l2_buffer32)
781#define VIDIOC_DQBUF32 _IOWR('V', 17, struct v4l2_buffer32)
782#define VIDIOC_ENUMSTD32 _IOWR('V', 25, struct v4l2_standard32)
783#define VIDIOC_ENUMINPUT32 _IOWR('V', 26, struct v4l2_input32)
Hans Verkuildd519bb2014-03-07 07:18:37 -0300784#define VIDIOC_G_EDID32 _IOWR('V', 40, struct v4l2_edid32)
785#define VIDIOC_S_EDID32 _IOWR('V', 41, struct v4l2_edid32)
Mauro Carvalho Chehab6e6a8b52018-01-04 13:08:56 -0500786#define VIDIOC_TRY_FMT32 _IOWR('V', 64, struct v4l2_format32)
Hans Verkuil92f45ba2008-12-21 10:35:25 -0300787#define VIDIOC_G_EXT_CTRLS32 _IOWR('V', 71, struct v4l2_ext_controls32)
788#define VIDIOC_S_EXT_CTRLS32 _IOWR('V', 72, struct v4l2_ext_controls32)
789#define VIDIOC_TRY_EXT_CTRLS32 _IOWR('V', 73, struct v4l2_ext_controls32)
Hans Verkuil2330fb82011-06-07 11:43:57 -0300790#define VIDIOC_DQEVENT32 _IOR ('V', 89, struct v4l2_event32)
Guennadi Liakhovetski21501582011-09-28 11:34:06 -0300791#define VIDIOC_CREATE_BUFS32 _IOWR('V', 92, struct v4l2_create_buffers32)
792#define VIDIOC_PREPARE_BUF32 _IOWR('V', 93, struct v4l2_buffer32)
Hans Verkuil92f45ba2008-12-21 10:35:25 -0300793
794#define VIDIOC_OVERLAY32 _IOW ('V', 14, s32)
Hans Verkuil92f45ba2008-12-21 10:35:25 -0300795#define VIDIOC_STREAMON32 _IOW ('V', 18, s32)
796#define VIDIOC_STREAMOFF32 _IOW ('V', 19, s32)
797#define VIDIOC_G_INPUT32 _IOR ('V', 38, s32)
798#define VIDIOC_S_INPUT32 _IOWR('V', 39, s32)
799#define VIDIOC_G_OUTPUT32 _IOR ('V', 46, s32)
800#define VIDIOC_S_OUTPUT32 _IOWR('V', 47, s32)
Arnd Bergmann0d0fbf82006-01-09 15:24:57 -0200801
Hans Verkuil069b7472008-12-30 07:04:34 -0300802static long do_video_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
Arnd Bergmann0d0fbf82006-01-09 15:24:57 -0200803{
804 union {
Philippe De Muytercf664a62006-01-09 15:24:59 -0200805 struct v4l2_format v2f;
806 struct v4l2_buffer v2b;
807 struct v4l2_framebuffer v2fb;
Guy Martina113bc72006-01-11 23:40:51 -0200808 struct v4l2_input v2i;
Hans Verkuil92f45ba2008-12-21 10:35:25 -0300809 struct v4l2_standard v2s;
810 struct v4l2_ext_controls v2ecs;
Hans Verkuil2330fb82011-06-07 11:43:57 -0300811 struct v4l2_event v2ev;
Guennadi Liakhovetski21501582011-09-28 11:34:06 -0300812 struct v4l2_create_buffers v2crt;
Hans Verkuildd519bb2014-03-07 07:18:37 -0300813 struct v4l2_edid v2edid;
Arnd Bergmann0d0fbf82006-01-09 15:24:57 -0200814 unsigned long vx;
Hans Verkuil92f45ba2008-12-21 10:35:25 -0300815 int vi;
Arnd Bergmann0d0fbf82006-01-09 15:24:57 -0200816 } karg;
Arnd Bergmann0d0fbf82006-01-09 15:24:57 -0200817 void __user *up = compat_ptr(arg);
Philippe De Muytercf664a62006-01-09 15:24:59 -0200818 int compatible_arg = 1;
Hans Verkuil069b7472008-12-30 07:04:34 -0300819 long err = 0;
Arnd Bergmann0d0fbf82006-01-09 15:24:57 -0200820
821 /* First, convert the command. */
Hans Verkuild1f81da2008-10-22 00:04:45 -0300822 switch (cmd) {
Hans Verkuil92f45ba2008-12-21 10:35:25 -0300823 case VIDIOC_G_FMT32: cmd = VIDIOC_G_FMT; break;
824 case VIDIOC_S_FMT32: cmd = VIDIOC_S_FMT; break;
825 case VIDIOC_QUERYBUF32: cmd = VIDIOC_QUERYBUF; break;
826 case VIDIOC_G_FBUF32: cmd = VIDIOC_G_FBUF; break;
827 case VIDIOC_S_FBUF32: cmd = VIDIOC_S_FBUF; break;
828 case VIDIOC_QBUF32: cmd = VIDIOC_QBUF; break;
829 case VIDIOC_DQBUF32: cmd = VIDIOC_DQBUF; break;
830 case VIDIOC_ENUMSTD32: cmd = VIDIOC_ENUMSTD; break;
831 case VIDIOC_ENUMINPUT32: cmd = VIDIOC_ENUMINPUT; break;
832 case VIDIOC_TRY_FMT32: cmd = VIDIOC_TRY_FMT; break;
833 case VIDIOC_G_EXT_CTRLS32: cmd = VIDIOC_G_EXT_CTRLS; break;
834 case VIDIOC_S_EXT_CTRLS32: cmd = VIDIOC_S_EXT_CTRLS; break;
835 case VIDIOC_TRY_EXT_CTRLS32: cmd = VIDIOC_TRY_EXT_CTRLS; break;
Hans Verkuil2330fb82011-06-07 11:43:57 -0300836 case VIDIOC_DQEVENT32: cmd = VIDIOC_DQEVENT; break;
Hans Verkuil92f45ba2008-12-21 10:35:25 -0300837 case VIDIOC_OVERLAY32: cmd = VIDIOC_OVERLAY; break;
Hans Verkuil92f45ba2008-12-21 10:35:25 -0300838 case VIDIOC_STREAMON32: cmd = VIDIOC_STREAMON; break;
839 case VIDIOC_STREAMOFF32: cmd = VIDIOC_STREAMOFF; break;
840 case VIDIOC_G_INPUT32: cmd = VIDIOC_G_INPUT; break;
841 case VIDIOC_S_INPUT32: cmd = VIDIOC_S_INPUT; break;
842 case VIDIOC_G_OUTPUT32: cmd = VIDIOC_G_OUTPUT; break;
843 case VIDIOC_S_OUTPUT32: cmd = VIDIOC_S_OUTPUT; break;
Guennadi Liakhovetski21501582011-09-28 11:34:06 -0300844 case VIDIOC_CREATE_BUFS32: cmd = VIDIOC_CREATE_BUFS; break;
845 case VIDIOC_PREPARE_BUF32: cmd = VIDIOC_PREPARE_BUF; break;
Hans Verkuildd519bb2014-03-07 07:18:37 -0300846 case VIDIOC_G_EDID32: cmd = VIDIOC_G_EDID; break;
847 case VIDIOC_S_EDID32: cmd = VIDIOC_S_EDID; break;
Hans Verkuil92f45ba2008-12-21 10:35:25 -0300848 }
Arnd Bergmann0d0fbf82006-01-09 15:24:57 -0200849
Hans Verkuild1f81da2008-10-22 00:04:45 -0300850 switch (cmd) {
Philippe De Muytercf664a62006-01-09 15:24:59 -0200851 case VIDIOC_OVERLAY:
Philippe De Muyter13d133b2006-01-09 15:25:00 -0200852 case VIDIOC_STREAMON:
853 case VIDIOC_STREAMOFF:
Hans Verkuil92f45ba2008-12-21 10:35:25 -0300854 case VIDIOC_S_INPUT:
855 case VIDIOC_S_OUTPUT:
856 err = get_user(karg.vi, (s32 __user *)up);
857 compatible_arg = 0;
Arnd Bergmann0d0fbf82006-01-09 15:24:57 -0200858 break;
Philippe De Muyter13d133b2006-01-09 15:25:00 -0200859
Hans Verkuil92f45ba2008-12-21 10:35:25 -0300860 case VIDIOC_G_INPUT:
861 case VIDIOC_G_OUTPUT:
Philippe De Muyter13d133b2006-01-09 15:25:00 -0200862 compatible_arg = 0;
863 break;
Philippe De Muytercf664a62006-01-09 15:24:59 -0200864
Hans Verkuildd519bb2014-03-07 07:18:37 -0300865 case VIDIOC_G_EDID:
866 case VIDIOC_S_EDID:
867 err = get_v4l2_edid32(&karg.v2edid, up);
Hans Verkuiled45ce22012-08-10 06:07:12 -0300868 compatible_arg = 0;
869 break;
870
Philippe De Muytercf664a62006-01-09 15:24:59 -0200871 case VIDIOC_G_FMT:
872 case VIDIOC_S_FMT:
873 case VIDIOC_TRY_FMT:
874 err = get_v4l2_format32(&karg.v2f, up);
875 compatible_arg = 0;
876 break;
877
Guennadi Liakhovetski21501582011-09-28 11:34:06 -0300878 case VIDIOC_CREATE_BUFS:
879 err = get_v4l2_create32(&karg.v2crt, up);
880 compatible_arg = 0;
881 break;
882
883 case VIDIOC_PREPARE_BUF:
Philippe De Muytercf664a62006-01-09 15:24:59 -0200884 case VIDIOC_QUERYBUF:
885 case VIDIOC_QBUF:
886 case VIDIOC_DQBUF:
887 err = get_v4l2_buffer32(&karg.v2b, up);
888 compatible_arg = 0;
889 break;
890
Hans Verkuil92f45ba2008-12-21 10:35:25 -0300891 case VIDIOC_S_FBUF:
892 err = get_v4l2_framebuffer32(&karg.v2fb, up);
Guy Martina113bc72006-01-11 23:40:51 -0200893 compatible_arg = 0;
894 break;
895
Hans Verkuil92f45ba2008-12-21 10:35:25 -0300896 case VIDIOC_G_FBUF:
897 compatible_arg = 0;
898 break;
899
900 case VIDIOC_ENUMSTD:
Philippe De Muytercf664a62006-01-09 15:24:59 -0200901 err = get_v4l2_standard32(&karg.v2s, up);
902 compatible_arg = 0;
903 break;
904
Guy Martina113bc72006-01-11 23:40:51 -0200905 case VIDIOC_ENUMINPUT:
Guy Martina113bc72006-01-11 23:40:51 -0200906 err = get_v4l2_input32(&karg.v2i, up);
907 compatible_arg = 0;
908 break;
909
Hans Verkuil92f45ba2008-12-21 10:35:25 -0300910 case VIDIOC_G_EXT_CTRLS:
911 case VIDIOC_S_EXT_CTRLS:
912 case VIDIOC_TRY_EXT_CTRLS:
913 err = get_v4l2_ext_controls32(&karg.v2ecs, up);
Guy Martina113bc72006-01-11 23:40:51 -0200914 compatible_arg = 0;
915 break;
Hans Verkuil2330fb82011-06-07 11:43:57 -0300916 case VIDIOC_DQEVENT:
917 compatible_arg = 0;
918 break;
Hans Verkuil92f45ba2008-12-21 10:35:25 -0300919 }
Hans Verkuild1f81da2008-10-22 00:04:45 -0300920 if (err)
Hans Verkuil92f45ba2008-12-21 10:35:25 -0300921 return err;
Arnd Bergmann0d0fbf82006-01-09 15:24:57 -0200922
Hans Verkuild1f81da2008-10-22 00:04:45 -0300923 if (compatible_arg)
Hans Verkuil92f45ba2008-12-21 10:35:25 -0300924 err = native_ioctl(file, cmd, (unsigned long)up);
Philippe De Muytercf664a62006-01-09 15:24:59 -0200925 else {
926 mm_segment_t old_fs = get_fs();
Arnd Bergmann0d0fbf82006-01-09 15:24:57 -0200927
Philippe De Muytercf664a62006-01-09 15:24:59 -0200928 set_fs(KERNEL_DS);
Hans Verkuil92f45ba2008-12-21 10:35:25 -0300929 err = native_ioctl(file, cmd, (unsigned long)&karg);
Philippe De Muytercf664a62006-01-09 15:24:59 -0200930 set_fs(old_fs);
931 }
Arnd Bergmann0d0fbf82006-01-09 15:24:57 -0200932
Hans Verkuil92f45ba2008-12-21 10:35:25 -0300933 /* Special case: even after an error we need to put the
934 results back for these ioctls since the error_idx will
935 contain information on which control failed. */
936 switch (cmd) {
937 case VIDIOC_G_EXT_CTRLS:
938 case VIDIOC_S_EXT_CTRLS:
939 case VIDIOC_TRY_EXT_CTRLS:
940 if (put_v4l2_ext_controls32(&karg.v2ecs, up))
941 err = -EFAULT;
942 break;
Hans Verkuilba7ed692017-03-30 09:05:25 -0300943 case VIDIOC_S_EDID:
944 if (put_v4l2_edid32(&karg.v2edid, up))
945 err = -EFAULT;
946 break;
Arnd Bergmann0d0fbf82006-01-09 15:24:57 -0200947 }
Hans Verkuil92f45ba2008-12-21 10:35:25 -0300948 if (err)
949 return err;
950
951 switch (cmd) {
Hans Verkuil92f45ba2008-12-21 10:35:25 -0300952 case VIDIOC_S_INPUT:
953 case VIDIOC_S_OUTPUT:
954 case VIDIOC_G_INPUT:
955 case VIDIOC_G_OUTPUT:
956 err = put_user(((s32)karg.vi), (s32 __user *)up);
957 break;
958
959 case VIDIOC_G_FBUF:
960 err = put_v4l2_framebuffer32(&karg.v2fb, up);
961 break;
962
Hans Verkuil2330fb82011-06-07 11:43:57 -0300963 case VIDIOC_DQEVENT:
964 err = put_v4l2_event32(&karg.v2ev, up);
965 break;
966
Hans Verkuildd519bb2014-03-07 07:18:37 -0300967 case VIDIOC_G_EDID:
Hans Verkuildd519bb2014-03-07 07:18:37 -0300968 err = put_v4l2_edid32(&karg.v2edid, up);
Hans Verkuiled45ce22012-08-10 06:07:12 -0300969 break;
970
Hans Verkuil92f45ba2008-12-21 10:35:25 -0300971 case VIDIOC_G_FMT:
972 case VIDIOC_S_FMT:
973 case VIDIOC_TRY_FMT:
974 err = put_v4l2_format32(&karg.v2f, up);
975 break;
976
Guennadi Liakhovetski21501582011-09-28 11:34:06 -0300977 case VIDIOC_CREATE_BUFS:
978 err = put_v4l2_create32(&karg.v2crt, up);
979 break;
980
Hans Verkuil3ee6d042018-01-24 08:37:04 -0500981 case VIDIOC_PREPARE_BUF:
Hans Verkuil92f45ba2008-12-21 10:35:25 -0300982 case VIDIOC_QUERYBUF:
983 case VIDIOC_QBUF:
984 case VIDIOC_DQBUF:
985 err = put_v4l2_buffer32(&karg.v2b, up);
986 break;
987
988 case VIDIOC_ENUMSTD:
989 err = put_v4l2_standard32(&karg.v2s, up);
990 break;
991
992 case VIDIOC_ENUMINPUT:
993 err = put_v4l2_input32(&karg.v2i, up);
994 break;
995 }
Arnd Bergmann0d0fbf82006-01-09 15:24:57 -0200996 return err;
997}
998
Hans Verkuil9bb7cde2008-12-30 06:42:40 -0300999long v4l2_compat_ioctl32(struct file *file, unsigned int cmd, unsigned long arg)
Arnd Bergmann0d0fbf82006-01-09 15:24:57 -02001000{
Laurent Pinchartb9d0aa62011-12-18 20:41:19 -03001001 struct video_device *vdev = video_devdata(file);
Hans Verkuil069b7472008-12-30 07:04:34 -03001002 long ret = -ENOIOCTLCMD;
Arnd Bergmann0d0fbf82006-01-09 15:24:57 -02001003
Frederic Weisbeckerc6d7ba82010-08-13 22:29:03 +02001004 if (!file->f_op->unlocked_ioctl)
Arnd Bergmann0d0fbf82006-01-09 15:24:57 -02001005 return ret;
1006
Hans Verkuilab58a302014-02-10 08:08:44 -03001007 if (_IOC_TYPE(cmd) == 'V' && _IOC_NR(cmd) < BASE_VIDIOC_PRIVATE)
Arnd Bergmann0d0fbf82006-01-09 15:24:57 -02001008 ret = do_video_ioctl(file, cmd, arg);
Hans Verkuilab58a302014-02-10 08:08:44 -03001009 else if (vdev->fops->compat_ioctl32)
1010 ret = vdev->fops->compat_ioctl32(file, cmd, arg);
Arnd Bergmann0d0fbf82006-01-09 15:24:57 -02001011
Hans Verkuilab58a302014-02-10 08:08:44 -03001012 if (ret == -ENOIOCTLCMD)
Hans Verkuil9ec32cc2015-09-09 03:40:39 -03001013 pr_debug("compat_ioctl32: unknown ioctl '%c', dir=%d, #%d (0x%08x)\n",
1014 _IOC_TYPE(cmd), _IOC_DIR(cmd), _IOC_NR(cmd), cmd);
Hans Verkuile8efb712006-01-09 15:24:59 -02001015 return ret;
Arnd Bergmann0d0fbf82006-01-09 15:24:57 -02001016}
Hans Verkuil9bb7cde2008-12-30 06:42:40 -03001017EXPORT_SYMBOL_GPL(v4l2_compat_ioctl32);