1 /*
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
8  * Copyright (C) 2003       Pavel Machek (pavel@ucw.cz)
9  * Copyright (C) 2005       Philippe De Muyter (phdm@macqel.be)
10  * Copyright (C) 2008       Hans Verkuil <hverkuil@xs4all.nl>
11  *
12  * These routines maintain argument size conversion between 32bit and 64bit
13  * ioctls.
14  */
15 
16 #include <linux/compat.h>
17 #include <linux/module.h>
18 #include <linux/videodev2.h>
19 #include <linux/v4l2-subdev.h>
20 #include <media/v4l2-dev.h>
21 #include <media/v4l2-fh.h>
22 #include <media/v4l2-ctrls.h>
23 #include <media/v4l2-ioctl.h>
24 
25 /**
26  * assign_in_user() - Copy from one __user var to another one
27  *
28  * @to: __user var where data will be stored
29  * @from: __user var where data will be retrieved.
30  *
31  * As this code very often needs to allocate userspace memory, it is easier
32  * to have a macro that will do both get_user() and put_user() at once.
33  *
34  * This function complements the macros defined at asm-generic/uaccess.h.
35  * It uses the same argument order as copy_in_user()
36  */
37 #define assign_in_user(to, from)					\
38 ({									\
39 	typeof(*from) __assign_tmp;					\
40 									\
41 	get_user(__assign_tmp, from) || put_user(__assign_tmp, to);	\
42 })
43 
44 /**
45  * get_user_cast() - Stores at a kernelspace local var the contents from a
46  *		pointer with userspace data that is not tagged with __user.
47  *
48  * @__x: var where data will be stored
49  * @__ptr: var where data will be retrieved.
50  *
51  * Sometimes we need to declare a pointer without __user because it
52  * comes from a pointer struct field that will be retrieved from userspace
53  * by the 64-bit native ioctl handler. This function ensures that the
54  * @__ptr will be cast to __user before calling get_user() in order to
55  * avoid warnings with static code analyzers like smatch.
56  */
57 #define get_user_cast(__x, __ptr)					\
58 ({									\
59 	get_user(__x, (typeof(*__ptr) __user *)(__ptr));		\
60 })
61 
62 /**
63  * put_user_force() - Stores the contents of a kernelspace local var
64  *		      into a userspace pointer, removing any __user cast.
65  *
66  * @__x: var where data will be stored
67  * @__ptr: var where data will be retrieved.
68  *
69  * Sometimes we need to remove the __user attribute from some data,
70  * by passing the __force macro. This function ensures that the
71  * @__ptr will be cast with __force before calling put_user(), in order to
72  * avoid warnings with static code analyzers like smatch.
73  */
74 #define put_user_force(__x, __ptr)					\
75 ({									\
76 	put_user((typeof(*__x) __force *)(__x), __ptr);			\
77 })
78 
79 /**
80  * assign_in_user_cast() - Copy from one __user var to another one
81  *
82  * @to: __user var where data will be stored
83  * @from: var where data will be retrieved that needs to be cast to __user.
84  *
85  * As this code very often needs to allocate userspace memory, it is easier
86  * to have a macro that will do both get_user_cast() and put_user() at once.
87  *
88  * This function should be used instead of assign_in_user() when the @from
89  * variable was not declared as __user. See get_user_cast() for more details.
90  *
91  * This function complements the macros defined at asm-generic/uaccess.h.
92  * It uses the same argument order as copy_in_user()
93  */
94 #define assign_in_user_cast(to, from)					\
95 ({									\
96 	typeof(*from) __assign_tmp;					\
97 									\
98 	get_user_cast(__assign_tmp, from) || put_user(__assign_tmp, to);\
99 })
100 
101 /**
102  * native_ioctl - Ancillary function that calls the native 64 bits ioctl
103  * handler.
104  *
105  * @file: pointer to &struct file with the file handler
106  * @cmd: ioctl to be called
107  * @arg: arguments passed from/to the ioctl handler
108  *
109  * This function calls the native ioctl handler at v4l2-dev, e. g. v4l2_ioctl()
110  */
111 static long native_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
112 {
113 	long ret = -ENOIOCTLCMD;
114 
115 	if (file->f_op->unlocked_ioctl)
116 		ret = file->f_op->unlocked_ioctl(file, cmd, arg);
117 
118 	return ret;
119 }
120 
121 
122 /*
123  * Per-ioctl data copy handlers.
124  *
125  * Those come in pairs, with a get_v4l2_foo() and a put_v4l2_foo() routine,
126  * where "v4l2_foo" is the name of the V4L2 struct.
127  *
128  * They basically get two __user pointers, one with a 32-bits struct that
129  * came from the userspace call and a 64-bits struct, also allocated as
130  * userspace, but filled internally by do_video_ioctl().
131  *
132  * For ioctls that have pointers inside it, the functions will also
133  * receive an ancillary buffer with extra space, used to pass extra
134  * data to the routine.
135  */
136 
137 struct v4l2_clip32 {
138 	struct v4l2_rect        c;
139 	compat_caddr_t		next;
140 };
141 
142 struct v4l2_window32 {
143 	struct v4l2_rect        w;
144 	__u32			field;	/* enum v4l2_field */
145 	__u32			chromakey;
146 	compat_caddr_t		clips; /* actually struct v4l2_clip32 * */
147 	__u32			clipcount;
148 	compat_caddr_t		bitmap;
149 	__u8                    global_alpha;
150 };
151 
152 static int get_v4l2_window32(struct v4l2_window __user *p64,
153 			     struct v4l2_window32 __user *p32,
154 			     void __user *aux_buf, u32 aux_space)
155 {
156 	struct v4l2_clip32 __user *uclips;
157 	struct v4l2_clip __user *kclips;
158 	compat_caddr_t p;
159 	u32 clipcount;
160 
161 	if (!access_ok(VERIFY_READ, p32, sizeof(*p32)) ||
162 	    copy_in_user(&p64->w, &p32->w, sizeof(p32->w)) ||
163 	    assign_in_user(&p64->field, &p32->field) ||
164 	    assign_in_user(&p64->chromakey, &p32->chromakey) ||
165 	    assign_in_user(&p64->global_alpha, &p32->global_alpha) ||
166 	    get_user(clipcount, &p32->clipcount) ||
167 	    put_user(clipcount, &p64->clipcount))
168 		return -EFAULT;
169 	if (clipcount > 2048)
170 		return -EINVAL;
171 	if (!clipcount)
172 		return put_user(NULL, &p64->clips);
173 
174 	if (get_user(p, &p32->clips))
175 		return -EFAULT;
176 	uclips = compat_ptr(p);
177 	if (aux_space < clipcount * sizeof(*kclips))
178 		return -EFAULT;
179 	kclips = aux_buf;
180 	if (put_user(kclips, &p64->clips))
181 		return -EFAULT;
182 
183 	while (clipcount--) {
184 		if (copy_in_user(&kclips->c, &uclips->c, sizeof(uclips->c)))
185 			return -EFAULT;
186 		if (put_user(clipcount ? kclips + 1 : NULL, &kclips->next))
187 			return -EFAULT;
188 		uclips++;
189 		kclips++;
190 	}
191 	return 0;
192 }
193 
194 static int put_v4l2_window32(struct v4l2_window __user *p64,
195 			     struct v4l2_window32 __user *p32)
196 {
197 	struct v4l2_clip __user *kclips;
198 	struct v4l2_clip32 __user *uclips;
199 	compat_caddr_t p;
200 	u32 clipcount;
201 
202 	if (copy_in_user(&p32->w, &p64->w, sizeof(p64->w)) ||
203 	    assign_in_user(&p32->field, &p64->field) ||
204 	    assign_in_user(&p32->chromakey, &p64->chromakey) ||
205 	    assign_in_user(&p32->global_alpha, &p64->global_alpha) ||
206 	    get_user(clipcount, &p64->clipcount) ||
207 	    put_user(clipcount, &p32->clipcount))
208 		return -EFAULT;
209 	if (!clipcount)
210 		return 0;
211 
212 	if (get_user(kclips, &p64->clips))
213 		return -EFAULT;
214 	if (get_user(p, &p32->clips))
215 		return -EFAULT;
216 	uclips = compat_ptr(p);
217 	while (clipcount--) {
218 		if (copy_in_user(&uclips->c, &kclips->c, sizeof(uclips->c)))
219 			return -EFAULT;
220 		uclips++;
221 		kclips++;
222 	}
223 	return 0;
224 }
225 
226 struct v4l2_format32 {
227 	__u32	type;	/* enum v4l2_buf_type */
228 	union {
229 		struct v4l2_pix_format	pix;
230 		struct v4l2_pix_format_mplane	pix_mp;
231 		struct v4l2_window32	win;
232 		struct v4l2_vbi_format	vbi;
233 		struct v4l2_sliced_vbi_format	sliced;
234 		struct v4l2_sdr_format	sdr;
235 		struct v4l2_meta_format	meta;
236 		__u8	raw_data[200];        /* user-defined */
237 	} fmt;
238 };
239 
240 /**
241  * struct v4l2_create_buffers32 - VIDIOC_CREATE_BUFS32 argument
242  * @index:	on return, index of the first created buffer
243  * @count:	entry: number of requested buffers,
244  *		return: number of created buffers
245  * @memory:	buffer memory type
246  * @format:	frame format, for which buffers are requested
247  * @capabilities: capabilities of this buffer type.
248  * @reserved:	future extensions
249  */
250 struct v4l2_create_buffers32 {
251 	__u32			index;
252 	__u32			count;
253 	__u32			memory;	/* enum v4l2_memory */
254 	struct v4l2_format32	format;
255 	__u32			capabilities;
256 	__u32			reserved[7];
257 };
258 
259 static int __bufsize_v4l2_format(struct v4l2_format32 __user *p32, u32 *size)
260 {
261 	u32 type;
262 
263 	if (get_user(type, &p32->type))
264 		return -EFAULT;
265 
266 	switch (type) {
267 	case V4L2_BUF_TYPE_VIDEO_OVERLAY:
268 	case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY: {
269 		u32 clipcount;
270 
271 		if (get_user(clipcount, &p32->fmt.win.clipcount))
272 			return -EFAULT;
273 		if (clipcount > 2048)
274 			return -EINVAL;
275 		*size = clipcount * sizeof(struct v4l2_clip);
276 		return 0;
277 	}
278 	default:
279 		*size = 0;
280 		return 0;
281 	}
282 }
283 
284 static int bufsize_v4l2_format(struct v4l2_format32 __user *p32, u32 *size)
285 {
286 	if (!access_ok(VERIFY_READ, p32, sizeof(*p32)))
287 		return -EFAULT;
288 	return __bufsize_v4l2_format(p32, size);
289 }
290 
291 static int __get_v4l2_format32(struct v4l2_format __user *p64,
292 			       struct v4l2_format32 __user *p32,
293 			       void __user *aux_buf, u32 aux_space)
294 {
295 	u32 type;
296 
297 	if (get_user(type, &p32->type) || put_user(type, &p64->type))
298 		return -EFAULT;
299 
300 	switch (type) {
301 	case V4L2_BUF_TYPE_VIDEO_CAPTURE:
302 	case V4L2_BUF_TYPE_VIDEO_OUTPUT:
303 		return copy_in_user(&p64->fmt.pix, &p32->fmt.pix,
304 				    sizeof(p64->fmt.pix)) ? -EFAULT : 0;
305 	case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
306 	case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
307 		return copy_in_user(&p64->fmt.pix_mp, &p32->fmt.pix_mp,
308 				    sizeof(p64->fmt.pix_mp)) ? -EFAULT : 0;
309 	case V4L2_BUF_TYPE_VIDEO_OVERLAY:
310 	case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
311 		return get_v4l2_window32(&p64->fmt.win, &p32->fmt.win,
312 					 aux_buf, aux_space);
313 	case V4L2_BUF_TYPE_VBI_CAPTURE:
314 	case V4L2_BUF_TYPE_VBI_OUTPUT:
315 		return copy_in_user(&p64->fmt.vbi, &p32->fmt.vbi,
316 				    sizeof(p64->fmt.vbi)) ? -EFAULT : 0;
317 	case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
318 	case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
319 		return copy_in_user(&p64->fmt.sliced, &p32->fmt.sliced,
320 				    sizeof(p64->fmt.sliced)) ? -EFAULT : 0;
321 	case V4L2_BUF_TYPE_SDR_CAPTURE:
322 	case V4L2_BUF_TYPE_SDR_OUTPUT:
323 		return copy_in_user(&p64->fmt.sdr, &p32->fmt.sdr,
324 				    sizeof(p64->fmt.sdr)) ? -EFAULT : 0;
325 	case V4L2_BUF_TYPE_META_CAPTURE:
326 		return copy_in_user(&p64->fmt.meta, &p32->fmt.meta,
327 				    sizeof(p64->fmt.meta)) ? -EFAULT : 0;
328 	default:
329 		return -EINVAL;
330 	}
331 }
332 
333 static int get_v4l2_format32(struct v4l2_format __user *p64,
334 			     struct v4l2_format32 __user *p32,
335 			     void __user *aux_buf, u32 aux_space)
336 {
337 	if (!access_ok(VERIFY_READ, p32, sizeof(*p32)))
338 		return -EFAULT;
339 	return __get_v4l2_format32(p64, p32, aux_buf, aux_space);
340 }
341 
342 static int bufsize_v4l2_create(struct v4l2_create_buffers32 __user *p32,
343 			       u32 *size)
344 {
345 	if (!access_ok(VERIFY_READ, p32, sizeof(*p32)))
346 		return -EFAULT;
347 	return __bufsize_v4l2_format(&p32->format, size);
348 }
349 
350 static int get_v4l2_create32(struct v4l2_create_buffers __user *p64,
351 			     struct v4l2_create_buffers32 __user *p32,
352 			     void __user *aux_buf, u32 aux_space)
353 {
354 	if (!access_ok(VERIFY_READ, p32, sizeof(*p32)) ||
355 	    copy_in_user(p64, p32,
356 			 offsetof(struct v4l2_create_buffers32, format)))
357 		return -EFAULT;
358 	return __get_v4l2_format32(&p64->format, &p32->format,
359 				   aux_buf, aux_space);
360 }
361 
362 static int __put_v4l2_format32(struct v4l2_format __user *p64,
363 			       struct v4l2_format32 __user *p32)
364 {
365 	u32 type;
366 
367 	if (get_user(type, &p64->type))
368 		return -EFAULT;
369 
370 	switch (type) {
371 	case V4L2_BUF_TYPE_VIDEO_CAPTURE:
372 	case V4L2_BUF_TYPE_VIDEO_OUTPUT:
373 		return copy_in_user(&p32->fmt.pix, &p64->fmt.pix,
374 				    sizeof(p64->fmt.pix)) ? -EFAULT : 0;
375 	case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
376 	case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
377 		return copy_in_user(&p32->fmt.pix_mp, &p64->fmt.pix_mp,
378 				    sizeof(p64->fmt.pix_mp)) ? -EFAULT : 0;
379 	case V4L2_BUF_TYPE_VIDEO_OVERLAY:
380 	case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
381 		return put_v4l2_window32(&p64->fmt.win, &p32->fmt.win);
382 	case V4L2_BUF_TYPE_VBI_CAPTURE:
383 	case V4L2_BUF_TYPE_VBI_OUTPUT:
384 		return copy_in_user(&p32->fmt.vbi, &p64->fmt.vbi,
385 				    sizeof(p64->fmt.vbi)) ? -EFAULT : 0;
386 	case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
387 	case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
388 		return copy_in_user(&p32->fmt.sliced, &p64->fmt.sliced,
389 				    sizeof(p64->fmt.sliced)) ? -EFAULT : 0;
390 	case V4L2_BUF_TYPE_SDR_CAPTURE:
391 	case V4L2_BUF_TYPE_SDR_OUTPUT:
392 		return copy_in_user(&p32->fmt.sdr, &p64->fmt.sdr,
393 				    sizeof(p64->fmt.sdr)) ? -EFAULT : 0;
394 	case V4L2_BUF_TYPE_META_CAPTURE:
395 		return copy_in_user(&p32->fmt.meta, &p64->fmt.meta,
396 				    sizeof(p64->fmt.meta)) ? -EFAULT : 0;
397 	default:
398 		return -EINVAL;
399 	}
400 }
401 
402 static int put_v4l2_format32(struct v4l2_format __user *p64,
403 			     struct v4l2_format32 __user *p32)
404 {
405 	if (!access_ok(VERIFY_WRITE, p32, sizeof(*p32)))
406 		return -EFAULT;
407 	return __put_v4l2_format32(p64, p32);
408 }
409 
410 static int put_v4l2_create32(struct v4l2_create_buffers __user *p64,
411 			     struct v4l2_create_buffers32 __user *p32)
412 {
413 	if (!access_ok(VERIFY_WRITE, p32, sizeof(*p32)) ||
414 	    copy_in_user(p32, p64,
415 			 offsetof(struct v4l2_create_buffers32, format)) ||
416 	    assign_in_user(&p32->capabilities, &p64->capabilities) ||
417 	    copy_in_user(p32->reserved, p64->reserved, sizeof(p64->reserved)))
418 		return -EFAULT;
419 	return __put_v4l2_format32(&p64->format, &p32->format);
420 }
421 
422 struct v4l2_standard32 {
423 	__u32		     index;
424 	compat_u64	     id;
425 	__u8		     name[24];
426 	struct v4l2_fract    frameperiod; /* Frames, not fields */
427 	__u32		     framelines;
428 	__u32		     reserved[4];
429 };
430 
431 static int get_v4l2_standard32(struct v4l2_standard __user *p64,
432 			       struct v4l2_standard32 __user *p32)
433 {
434 	/* other fields are not set by the user, nor used by the driver */
435 	if (!access_ok(VERIFY_READ, p32, sizeof(*p32)) ||
436 	    assign_in_user(&p64->index, &p32->index))
437 		return -EFAULT;
438 	return 0;
439 }
440 
441 static int put_v4l2_standard32(struct v4l2_standard __user *p64,
442 			       struct v4l2_standard32 __user *p32)
443 {
444 	if (!access_ok(VERIFY_WRITE, p32, sizeof(*p32)) ||
445 	    assign_in_user(&p32->index, &p64->index) ||
446 	    assign_in_user(&p32->id, &p64->id) ||
447 	    copy_in_user(p32->name, p64->name, sizeof(p32->name)) ||
448 	    copy_in_user(&p32->frameperiod, &p64->frameperiod,
449 			 sizeof(p32->frameperiod)) ||
450 	    assign_in_user(&p32->framelines, &p64->framelines) ||
451 	    copy_in_user(p32->reserved, p64->reserved, sizeof(p32->reserved)))
452 		return -EFAULT;
453 	return 0;
454 }
455 
456 struct v4l2_plane32 {
457 	__u32			bytesused;
458 	__u32			length;
459 	union {
460 		__u32		mem_offset;
461 		compat_long_t	userptr;
462 		__s32		fd;
463 	} m;
464 	__u32			data_offset;
465 	__u32			reserved[11];
466 };
467 
468 struct v4l2_buffer32 {
469 	__u32			index;
470 	__u32			type;	/* enum v4l2_buf_type */
471 	__u32			bytesused;
472 	__u32			flags;
473 	__u32			field;	/* enum v4l2_field */
474 	struct compat_timeval	timestamp;
475 	struct v4l2_timecode	timecode;
476 	__u32			sequence;
477 
478 	/* memory location */
479 	__u32			memory;	/* enum v4l2_memory */
480 	union {
481 		__u32           offset;
482 		compat_long_t   userptr;
483 		compat_caddr_t  planes;
484 		__s32		fd;
485 	} m;
486 	__u32			length;
487 	__u32			reserved2;
488 	__s32			request_fd;
489 };
490 
491 static int get_v4l2_plane32(struct v4l2_plane __user *p64,
492 			    struct v4l2_plane32 __user *p32,
493 			    enum v4l2_memory memory)
494 {
495 	compat_ulong_t p;
496 
497 	if (copy_in_user(p64, p32, 2 * sizeof(__u32)) ||
498 	    copy_in_user(&p64->data_offset, &p32->data_offset,
499 			 sizeof(p64->data_offset)))
500 		return -EFAULT;
501 
502 	switch (memory) {
503 	case V4L2_MEMORY_MMAP:
504 	case V4L2_MEMORY_OVERLAY:
505 		if (copy_in_user(&p64->m.mem_offset, &p32->m.mem_offset,
506 				 sizeof(p32->m.mem_offset)))
507 			return -EFAULT;
508 		break;
509 	case V4L2_MEMORY_USERPTR:
510 		if (get_user(p, &p32->m.userptr) ||
511 		    put_user((unsigned long)compat_ptr(p), &p64->m.userptr))
512 			return -EFAULT;
513 		break;
514 	case V4L2_MEMORY_DMABUF:
515 		if (copy_in_user(&p64->m.fd, &p32->m.fd, sizeof(p32->m.fd)))
516 			return -EFAULT;
517 		break;
518 	}
519 
520 	return 0;
521 }
522 
523 static int put_v4l2_plane32(struct v4l2_plane __user *p64,
524 			    struct v4l2_plane32 __user *p32,
525 			    enum v4l2_memory memory)
526 {
527 	unsigned long p;
528 
529 	if (copy_in_user(p32, p64, 2 * sizeof(__u32)) ||
530 	    copy_in_user(&p32->data_offset, &p64->data_offset,
531 			 sizeof(p64->data_offset)))
532 		return -EFAULT;
533 
534 	switch (memory) {
535 	case V4L2_MEMORY_MMAP:
536 	case V4L2_MEMORY_OVERLAY:
537 		if (copy_in_user(&p32->m.mem_offset, &p64->m.mem_offset,
538 				 sizeof(p64->m.mem_offset)))
539 			return -EFAULT;
540 		break;
541 	case V4L2_MEMORY_USERPTR:
542 		if (get_user(p, &p64->m.userptr) ||
543 		    put_user((compat_ulong_t)ptr_to_compat((void __user *)p),
544 			     &p32->m.userptr))
545 			return -EFAULT;
546 		break;
547 	case V4L2_MEMORY_DMABUF:
548 		if (copy_in_user(&p32->m.fd, &p64->m.fd, sizeof(p64->m.fd)))
549 			return -EFAULT;
550 		break;
551 	}
552 
553 	return 0;
554 }
555 
556 static int bufsize_v4l2_buffer(struct v4l2_buffer32 __user *p32, u32 *size)
557 {
558 	u32 type;
559 	u32 length;
560 
561 	if (!access_ok(VERIFY_READ, p32, sizeof(*p32)) ||
562 	    get_user(type, &p32->type) ||
563 	    get_user(length, &p32->length))
564 		return -EFAULT;
565 
566 	if (V4L2_TYPE_IS_MULTIPLANAR(type)) {
567 		if (length > VIDEO_MAX_PLANES)
568 			return -EINVAL;
569 
570 		/*
571 		 * We don't really care if userspace decides to kill itself
572 		 * by passing a very big length value
573 		 */
574 		*size = length * sizeof(struct v4l2_plane);
575 	} else {
576 		*size = 0;
577 	}
578 	return 0;
579 }
580 
581 static int get_v4l2_buffer32(struct v4l2_buffer __user *p64,
582 			     struct v4l2_buffer32 __user *p32,
583 			     void __user *aux_buf, u32 aux_space)
584 {
585 	u32 type;
586 	u32 length;
587 	s32 request_fd;
588 	enum v4l2_memory memory;
589 	struct v4l2_plane32 __user *uplane32;
590 	struct v4l2_plane __user *uplane;
591 	compat_caddr_t p;
592 	int ret;
593 
594 	if (!access_ok(VERIFY_READ, p32, sizeof(*p32)) ||
595 	    assign_in_user(&p64->index, &p32->index) ||
596 	    get_user(type, &p32->type) ||
597 	    put_user(type, &p64->type) ||
598 	    assign_in_user(&p64->flags, &p32->flags) ||
599 	    get_user(memory, &p32->memory) ||
600 	    put_user(memory, &p64->memory) ||
601 	    get_user(length, &p32->length) ||
602 	    put_user(length, &p64->length) ||
603 	    get_user(request_fd, &p32->request_fd) ||
604 	    put_user(request_fd, &p64->request_fd))
605 		return -EFAULT;
606 
607 	if (V4L2_TYPE_IS_OUTPUT(type))
608 		if (assign_in_user(&p64->bytesused, &p32->bytesused) ||
609 		    assign_in_user(&p64->field, &p32->field) ||
610 		    assign_in_user(&p64->timestamp.tv_sec,
611 				   &p32->timestamp.tv_sec) ||
612 		    assign_in_user(&p64->timestamp.tv_usec,
613 				   &p32->timestamp.tv_usec))
614 			return -EFAULT;
615 
616 	if (V4L2_TYPE_IS_MULTIPLANAR(type)) {
617 		u32 num_planes = length;
618 
619 		if (num_planes == 0) {
620 			/*
621 			 * num_planes == 0 is legal, e.g. when userspace doesn't
622 			 * need planes array on DQBUF
623 			 */
624 			return put_user(NULL, &p64->m.planes);
625 		}
626 		if (num_planes > VIDEO_MAX_PLANES)
627 			return -EINVAL;
628 
629 		if (get_user(p, &p32->m.planes))
630 			return -EFAULT;
631 
632 		uplane32 = compat_ptr(p);
633 		if (!access_ok(VERIFY_READ, uplane32,
634 			       num_planes * sizeof(*uplane32)))
635 			return -EFAULT;
636 
637 		/*
638 		 * We don't really care if userspace decides to kill itself
639 		 * by passing a very big num_planes value
640 		 */
641 		if (aux_space < num_planes * sizeof(*uplane))
642 			return -EFAULT;
643 
644 		uplane = aux_buf;
645 		if (put_user_force(uplane, &p64->m.planes))
646 			return -EFAULT;
647 
648 		while (num_planes--) {
649 			ret = get_v4l2_plane32(uplane, uplane32, memory);
650 			if (ret)
651 				return ret;
652 			uplane++;
653 			uplane32++;
654 		}
655 	} else {
656 		switch (memory) {
657 		case V4L2_MEMORY_MMAP:
658 		case V4L2_MEMORY_OVERLAY:
659 			if (assign_in_user(&p64->m.offset, &p32->m.offset))
660 				return -EFAULT;
661 			break;
662 		case V4L2_MEMORY_USERPTR: {
663 			compat_ulong_t userptr;
664 
665 			if (get_user(userptr, &p32->m.userptr) ||
666 			    put_user((unsigned long)compat_ptr(userptr),
667 				     &p64->m.userptr))
668 				return -EFAULT;
669 			break;
670 		}
671 		case V4L2_MEMORY_DMABUF:
672 			if (assign_in_user(&p64->m.fd, &p32->m.fd))
673 				return -EFAULT;
674 			break;
675 		}
676 	}
677 
678 	return 0;
679 }
680 
681 static int put_v4l2_buffer32(struct v4l2_buffer __user *p64,
682 			     struct v4l2_buffer32 __user *p32)
683 {
684 	u32 type;
685 	u32 length;
686 	enum v4l2_memory memory;
687 	struct v4l2_plane32 __user *uplane32;
688 	struct v4l2_plane *uplane;
689 	compat_caddr_t p;
690 	int ret;
691 
692 	if (!access_ok(VERIFY_WRITE, p32, sizeof(*p32)) ||
693 	    assign_in_user(&p32->index, &p64->index) ||
694 	    get_user(type, &p64->type) ||
695 	    put_user(type, &p32->type) ||
696 	    assign_in_user(&p32->flags, &p64->flags) ||
697 	    get_user(memory, &p64->memory) ||
698 	    put_user(memory, &p32->memory))
699 		return -EFAULT;
700 
701 	if (assign_in_user(&p32->bytesused, &p64->bytesused) ||
702 	    assign_in_user(&p32->field, &p64->field) ||
703 	    assign_in_user(&p32->timestamp.tv_sec, &p64->timestamp.tv_sec) ||
704 	    assign_in_user(&p32->timestamp.tv_usec, &p64->timestamp.tv_usec) ||
705 	    copy_in_user(&p32->timecode, &p64->timecode, sizeof(p64->timecode)) ||
706 	    assign_in_user(&p32->sequence, &p64->sequence) ||
707 	    assign_in_user(&p32->reserved2, &p64->reserved2) ||
708 	    assign_in_user(&p32->request_fd, &p64->request_fd) ||
709 	    get_user(length, &p64->length) ||
710 	    put_user(length, &p32->length))
711 		return -EFAULT;
712 
713 	if (V4L2_TYPE_IS_MULTIPLANAR(type)) {
714 		u32 num_planes = length;
715 
716 		if (num_planes == 0)
717 			return 0;
718 		/* We need to define uplane without __user, even though
719 		 * it does point to data in userspace here. The reason is
720 		 * that v4l2-ioctl.c copies it from userspace to kernelspace,
721 		 * so its definition in videodev2.h doesn't have a
722 		 * __user markup. Defining uplane with __user causes
723 		 * smatch warnings, so instead declare it without __user
724 		 * and cast it as a userspace pointer to put_v4l2_plane32().
725 		 */
726 		if (get_user(uplane, &p64->m.planes))
727 			return -EFAULT;
728 		if (get_user(p, &p32->m.planes))
729 			return -EFAULT;
730 		uplane32 = compat_ptr(p);
731 
732 		while (num_planes--) {
733 			ret = put_v4l2_plane32((void __user *)uplane,
734 					       uplane32, memory);
735 			if (ret)
736 				return ret;
737 			++uplane;
738 			++uplane32;
739 		}
740 	} else {
741 		switch (memory) {
742 		case V4L2_MEMORY_MMAP:
743 		case V4L2_MEMORY_OVERLAY:
744 			if (assign_in_user(&p32->m.offset, &p64->m.offset))
745 				return -EFAULT;
746 			break;
747 		case V4L2_MEMORY_USERPTR:
748 			if (assign_in_user(&p32->m.userptr, &p64->m.userptr))
749 				return -EFAULT;
750 			break;
751 		case V4L2_MEMORY_DMABUF:
752 			if (assign_in_user(&p32->m.fd, &p64->m.fd))
753 				return -EFAULT;
754 			break;
755 		}
756 	}
757 
758 	return 0;
759 }
760 
761 struct v4l2_framebuffer32 {
762 	__u32			capability;
763 	__u32			flags;
764 	compat_caddr_t		base;
765 	struct {
766 		__u32		width;
767 		__u32		height;
768 		__u32		pixelformat;
769 		__u32		field;
770 		__u32		bytesperline;
771 		__u32		sizeimage;
772 		__u32		colorspace;
773 		__u32		priv;
774 	} fmt;
775 };
776 
777 static int get_v4l2_framebuffer32(struct v4l2_framebuffer __user *p64,
778 				  struct v4l2_framebuffer32 __user *p32)
779 {
780 	compat_caddr_t tmp;
781 
782 	if (!access_ok(VERIFY_READ, p32, sizeof(*p32)) ||
783 	    get_user(tmp, &p32->base) ||
784 	    put_user_force(compat_ptr(tmp), &p64->base) ||
785 	    assign_in_user(&p64->capability, &p32->capability) ||
786 	    assign_in_user(&p64->flags, &p32->flags) ||
787 	    copy_in_user(&p64->fmt, &p32->fmt, sizeof(p64->fmt)))
788 		return -EFAULT;
789 	return 0;
790 }
791 
792 static int put_v4l2_framebuffer32(struct v4l2_framebuffer __user *p64,
793 				  struct v4l2_framebuffer32 __user *p32)
794 {
795 	void *base;
796 
797 	if (!access_ok(VERIFY_WRITE, p32, sizeof(*p32)) ||
798 	    get_user(base, &p64->base) ||
799 	    put_user(ptr_to_compat((void __user *)base), &p32->base) ||
800 	    assign_in_user(&p32->capability, &p64->capability) ||
801 	    assign_in_user(&p32->flags, &p64->flags) ||
802 	    copy_in_user(&p32->fmt, &p64->fmt, sizeof(p64->fmt)))
803 		return -EFAULT;
804 	return 0;
805 }
806 
807 struct v4l2_input32 {
808 	__u32	     index;		/*  Which input */
809 	__u8	     name[32];		/*  Label */
810 	__u32	     type;		/*  Type of input */
811 	__u32	     audioset;		/*  Associated audios (bitfield) */
812 	__u32        tuner;             /*  Associated tuner */
813 	compat_u64   std;
814 	__u32	     status;
815 	__u32	     capabilities;
816 	__u32	     reserved[3];
817 };
818 
819 /*
820  * The 64-bit v4l2_input struct has extra padding at the end of the struct.
821  * Otherwise it is identical to the 32-bit version.
822  */
823 static inline int get_v4l2_input32(struct v4l2_input __user *p64,
824 				   struct v4l2_input32 __user *p32)
825 {
826 	if (copy_in_user(p64, p32, sizeof(*p32)))
827 		return -EFAULT;
828 	return 0;
829 }
830 
831 static inline int put_v4l2_input32(struct v4l2_input __user *p64,
832 				   struct v4l2_input32 __user *p32)
833 {
834 	if (copy_in_user(p32, p64, sizeof(*p32)))
835 		return -EFAULT;
836 	return 0;
837 }
838 
839 struct v4l2_ext_controls32 {
840 	__u32 which;
841 	__u32 count;
842 	__u32 error_idx;
843 	__s32 request_fd;
844 	__u32 reserved[1];
845 	compat_caddr_t controls; /* actually struct v4l2_ext_control32 * */
846 };
847 
848 struct v4l2_ext_control32 {
849 	__u32 id;
850 	__u32 size;
851 	__u32 reserved2[1];
852 	union {
853 		__s32 value;
854 		__s64 value64;
855 		compat_caddr_t string; /* actually char * */
856 	};
857 } __attribute__ ((packed));
858 
859 /* Return true if this control is a pointer type. */
860 static inline bool ctrl_is_pointer(struct file *file, u32 id)
861 {
862 	struct video_device *vdev = video_devdata(file);
863 	struct v4l2_fh *fh = NULL;
864 	struct v4l2_ctrl_handler *hdl = NULL;
865 	struct v4l2_query_ext_ctrl qec = { id };
866 	const struct v4l2_ioctl_ops *ops = vdev->ioctl_ops;
867 
868 	if (test_bit(V4L2_FL_USES_V4L2_FH, &vdev->flags))
869 		fh = file->private_data;
870 
871 	if (fh && fh->ctrl_handler)
872 		hdl = fh->ctrl_handler;
873 	else if (vdev->ctrl_handler)
874 		hdl = vdev->ctrl_handler;
875 
876 	if (hdl) {
877 		struct v4l2_ctrl *ctrl = v4l2_ctrl_find(hdl, id);
878 
879 		return ctrl && ctrl->is_ptr;
880 	}
881 
882 	if (!ops || !ops->vidioc_query_ext_ctrl)
883 		return false;
884 
885 	return !ops->vidioc_query_ext_ctrl(file, fh, &qec) &&
886 		(qec.flags & V4L2_CTRL_FLAG_HAS_PAYLOAD);
887 }
888 
889 static int bufsize_v4l2_ext_controls(struct v4l2_ext_controls32 __user *p32,
890 				     u32 *size)
891 {
892 	u32 count;
893 
894 	if (!access_ok(VERIFY_READ, p32, sizeof(*p32)) ||
895 	    get_user(count, &p32->count))
896 		return -EFAULT;
897 	if (count > V4L2_CID_MAX_CTRLS)
898 		return -EINVAL;
899 	*size = count * sizeof(struct v4l2_ext_control);
900 	return 0;
901 }
902 
903 static int get_v4l2_ext_controls32(struct file *file,
904 				   struct v4l2_ext_controls __user *p64,
905 				   struct v4l2_ext_controls32 __user *p32,
906 				   void __user *aux_buf, u32 aux_space)
907 {
908 	struct v4l2_ext_control32 __user *ucontrols;
909 	struct v4l2_ext_control __user *kcontrols;
910 	u32 count;
911 	u32 n;
912 	compat_caddr_t p;
913 
914 	if (!access_ok(VERIFY_READ, p32, sizeof(*p32)) ||
915 	    assign_in_user(&p64->which, &p32->which) ||
916 	    get_user(count, &p32->count) ||
917 	    put_user(count, &p64->count) ||
918 	    assign_in_user(&p64->error_idx, &p32->error_idx) ||
919 	    assign_in_user(&p64->request_fd, &p32->request_fd) ||
920 	    copy_in_user(p64->reserved, p32->reserved, sizeof(p64->reserved)))
921 		return -EFAULT;
922 
923 	if (count == 0)
924 		return put_user(NULL, &p64->controls);
925 	if (count > V4L2_CID_MAX_CTRLS)
926 		return -EINVAL;
927 	if (get_user(p, &p32->controls))
928 		return -EFAULT;
929 	ucontrols = compat_ptr(p);
930 	if (!access_ok(VERIFY_READ, ucontrols, count * sizeof(*ucontrols)))
931 		return -EFAULT;
932 	if (aux_space < count * sizeof(*kcontrols))
933 		return -EFAULT;
934 	kcontrols = aux_buf;
935 	if (put_user_force(kcontrols, &p64->controls))
936 		return -EFAULT;
937 
938 	for (n = 0; n < count; n++) {
939 		u32 id;
940 
941 		if (copy_in_user(kcontrols, ucontrols, sizeof(*ucontrols)))
942 			return -EFAULT;
943 
944 		if (get_user(id, &kcontrols->id))
945 			return -EFAULT;
946 
947 		if (ctrl_is_pointer(file, id)) {
948 			void __user *s;
949 
950 			if (get_user(p, &ucontrols->string))
951 				return -EFAULT;
952 			s = compat_ptr(p);
953 			if (put_user(s, &kcontrols->string))
954 				return -EFAULT;
955 		}
956 		ucontrols++;
957 		kcontrols++;
958 	}
959 	return 0;
960 }
961 
962 static int put_v4l2_ext_controls32(struct file *file,
963 				   struct v4l2_ext_controls __user *p64,
964 				   struct v4l2_ext_controls32 __user *p32)
965 {
966 	struct v4l2_ext_control32 __user *ucontrols;
967 	struct v4l2_ext_control *kcontrols;
968 	u32 count;
969 	u32 n;
970 	compat_caddr_t p;
971 
972 	/*
973 	 * We need to define kcontrols without __user, even though it does
974 	 * point to data in userspace here. The reason is that v4l2-ioctl.c
975 	 * copies it from userspace to kernelspace, so its definition in
976 	 * videodev2.h doesn't have a __user markup. Defining kcontrols
977 	 * with __user causes smatch warnings, so instead declare it
978 	 * without __user and cast it as a userspace pointer where needed.
979 	 */
980 	if (!access_ok(VERIFY_WRITE, p32, sizeof(*p32)) ||
981 	    assign_in_user(&p32->which, &p64->which) ||
982 	    get_user(count, &p64->count) ||
983 	    put_user(count, &p32->count) ||
984 	    assign_in_user(&p32->error_idx, &p64->error_idx) ||
985 	    assign_in_user(&p32->request_fd, &p64->request_fd) ||
986 	    copy_in_user(p32->reserved, p64->reserved, sizeof(p32->reserved)) ||
987 	    get_user(kcontrols, &p64->controls))
988 		return -EFAULT;
989 
990 	if (!count || count > (U32_MAX/sizeof(*ucontrols)))
991 		return 0;
992 	if (get_user(p, &p32->controls))
993 		return -EFAULT;
994 	ucontrols = compat_ptr(p);
995 	if (!access_ok(VERIFY_WRITE, ucontrols, count * sizeof(*ucontrols)))
996 		return -EFAULT;
997 
998 	for (n = 0; n < count; n++) {
999 		unsigned int size = sizeof(*ucontrols);
1000 		u32 id;
1001 
1002 		if (get_user_cast(id, &kcontrols->id) ||
1003 		    put_user(id, &ucontrols->id) ||
1004 		    assign_in_user_cast(&ucontrols->size, &kcontrols->size) ||
1005 		    copy_in_user(&ucontrols->reserved2,
1006 				 (void __user *)&kcontrols->reserved2,
1007 				 sizeof(ucontrols->reserved2)))
1008 			return -EFAULT;
1009 
1010 		/*
1011 		 * Do not modify the pointer when copying a pointer control.
1012 		 * The contents of the pointer was changed, not the pointer
1013 		 * itself.
1014 		 */
1015 		if (ctrl_is_pointer(file, id))
1016 			size -= sizeof(ucontrols->value64);
1017 
1018 		if (copy_in_user(ucontrols,
1019 				 (void __user *)kcontrols, size))
1020 			return -EFAULT;
1021 
1022 		ucontrols++;
1023 		kcontrols++;
1024 	}
1025 	return 0;
1026 }
1027 
1028 struct v4l2_event32 {
1029 	__u32				type;
1030 	union {
1031 		compat_s64		value64;
1032 		__u8			data[64];
1033 	} u;
1034 	__u32				pending;
1035 	__u32				sequence;
1036 	struct compat_timespec		timestamp;
1037 	__u32				id;
1038 	__u32				reserved[8];
1039 };
1040 
1041 static int put_v4l2_event32(struct v4l2_event __user *p64,
1042 			    struct v4l2_event32 __user *p32)
1043 {
1044 	if (!access_ok(VERIFY_WRITE, p32, sizeof(*p32)) ||
1045 	    assign_in_user(&p32->type, &p64->type) ||
1046 	    copy_in_user(&p32->u, &p64->u, sizeof(p64->u)) ||
1047 	    assign_in_user(&p32->pending, &p64->pending) ||
1048 	    assign_in_user(&p32->sequence, &p64->sequence) ||
1049 	    assign_in_user(&p32->timestamp.tv_sec, &p64->timestamp.tv_sec) ||
1050 	    assign_in_user(&p32->timestamp.tv_nsec, &p64->timestamp.tv_nsec) ||
1051 	    assign_in_user(&p32->id, &p64->id) ||
1052 	    copy_in_user(p32->reserved, p64->reserved, sizeof(p32->reserved)))
1053 		return -EFAULT;
1054 	return 0;
1055 }
1056 
1057 struct v4l2_edid32 {
1058 	__u32 pad;
1059 	__u32 start_block;
1060 	__u32 blocks;
1061 	__u32 reserved[5];
1062 	compat_caddr_t edid;
1063 };
1064 
1065 static int get_v4l2_edid32(struct v4l2_edid __user *p64,
1066 			   struct v4l2_edid32 __user *p32)
1067 {
1068 	compat_uptr_t tmp;
1069 
1070 	if (!access_ok(VERIFY_READ, p32, sizeof(*p32)) ||
1071 	    assign_in_user(&p64->pad, &p32->pad) ||
1072 	    assign_in_user(&p64->start_block, &p32->start_block) ||
1073 	    assign_in_user_cast(&p64->blocks, &p32->blocks) ||
1074 	    get_user(tmp, &p32->edid) ||
1075 	    put_user_force(compat_ptr(tmp), &p64->edid) ||
1076 	    copy_in_user(p64->reserved, p32->reserved, sizeof(p64->reserved)))
1077 		return -EFAULT;
1078 	return 0;
1079 }
1080 
1081 static int put_v4l2_edid32(struct v4l2_edid __user *p64,
1082 			   struct v4l2_edid32 __user *p32)
1083 {
1084 	void *edid;
1085 
1086 	if (!access_ok(VERIFY_WRITE, p32, sizeof(*p32)) ||
1087 	    assign_in_user(&p32->pad, &p64->pad) ||
1088 	    assign_in_user(&p32->start_block, &p64->start_block) ||
1089 	    assign_in_user(&p32->blocks, &p64->blocks) ||
1090 	    get_user(edid, &p64->edid) ||
1091 	    put_user(ptr_to_compat((void __user *)edid), &p32->edid) ||
1092 	    copy_in_user(p32->reserved, p64->reserved, sizeof(p32->reserved)))
1093 		return -EFAULT;
1094 	return 0;
1095 }
1096 
1097 /*
1098  * List of ioctls that require 32-bits/64-bits conversion
1099  *
1100  * The V4L2 ioctls that aren't listed there don't have pointer arguments
1101  * and the struct size is identical for both 32 and 64 bits versions, so
1102  * they don't need translations.
1103  */
1104 
1105 #define VIDIOC_G_FMT32		_IOWR('V',  4, struct v4l2_format32)
1106 #define VIDIOC_S_FMT32		_IOWR('V',  5, struct v4l2_format32)
1107 #define VIDIOC_QUERYBUF32	_IOWR('V',  9, struct v4l2_buffer32)
1108 #define VIDIOC_G_FBUF32		_IOR ('V', 10, struct v4l2_framebuffer32)
1109 #define VIDIOC_S_FBUF32		_IOW ('V', 11, struct v4l2_framebuffer32)
1110 #define VIDIOC_QBUF32		_IOWR('V', 15, struct v4l2_buffer32)
1111 #define VIDIOC_DQBUF32		_IOWR('V', 17, struct v4l2_buffer32)
1112 #define VIDIOC_ENUMSTD32	_IOWR('V', 25, struct v4l2_standard32)
1113 #define VIDIOC_ENUMINPUT32	_IOWR('V', 26, struct v4l2_input32)
1114 #define VIDIOC_G_EDID32		_IOWR('V', 40, struct v4l2_edid32)
1115 #define VIDIOC_S_EDID32		_IOWR('V', 41, struct v4l2_edid32)
1116 #define VIDIOC_TRY_FMT32	_IOWR('V', 64, struct v4l2_format32)
1117 #define VIDIOC_G_EXT_CTRLS32    _IOWR('V', 71, struct v4l2_ext_controls32)
1118 #define VIDIOC_S_EXT_CTRLS32    _IOWR('V', 72, struct v4l2_ext_controls32)
1119 #define VIDIOC_TRY_EXT_CTRLS32  _IOWR('V', 73, struct v4l2_ext_controls32)
1120 #define	VIDIOC_DQEVENT32	_IOR ('V', 89, struct v4l2_event32)
1121 #define VIDIOC_CREATE_BUFS32	_IOWR('V', 92, struct v4l2_create_buffers32)
1122 #define VIDIOC_PREPARE_BUF32	_IOWR('V', 93, struct v4l2_buffer32)
1123 
1124 #define VIDIOC_OVERLAY32	_IOW ('V', 14, s32)
1125 #define VIDIOC_STREAMON32	_IOW ('V', 18, s32)
1126 #define VIDIOC_STREAMOFF32	_IOW ('V', 19, s32)
1127 #define VIDIOC_G_INPUT32	_IOR ('V', 38, s32)
1128 #define VIDIOC_S_INPUT32	_IOWR('V', 39, s32)
1129 #define VIDIOC_G_OUTPUT32	_IOR ('V', 46, s32)
1130 #define VIDIOC_S_OUTPUT32	_IOWR('V', 47, s32)
1131 
1132 /**
1133  * alloc_userspace() - Allocates a 64-bits userspace pointer compatible
1134  *	for calling the native 64-bits version of an ioctl.
1135  *
1136  * @size:	size of the structure itself to be allocated.
1137  * @aux_space:	extra size needed to store "extra" data, e.g. space for
1138  *		other __user data that is pointed to fields inside the
1139  *		structure.
1140  * @new_p64:	pointer to a pointer to be filled with the allocated struct.
1141  *
1142  * Return:
1143  *
1144  * if it can't allocate memory, either -ENOMEM or -EFAULT will be returned.
1145  * Zero otherwise.
1146  */
1147 static int alloc_userspace(unsigned int size, u32 aux_space,
1148 			   void __user **new_p64)
1149 {
1150 	*new_p64 = compat_alloc_user_space(size + aux_space);
1151 	if (!*new_p64)
1152 		return -ENOMEM;
1153 	if (clear_user(*new_p64, size))
1154 		return -EFAULT;
1155 	return 0;
1156 }
1157 
1158 /**
1159  * do_video_ioctl() - Ancillary function with handles a compat32 ioctl call
1160  *
1161  * @file: pointer to &struct file with the file handler
1162  * @cmd: ioctl to be called
1163  * @arg: arguments passed from/to the ioctl handler
1164  *
1165  * This function is called when a 32 bits application calls a V4L2 ioctl
1166  * and the Kernel is compiled with 64 bits.
1167  *
1168  * This function is called by v4l2_compat_ioctl32() when the function is
1169  * not private to some specific driver.
1170  *
1171  * It converts a 32-bits struct into a 64 bits one, calls the native 64-bits
1172  * ioctl handler and fills back the 32-bits struct with the results of the
1173  * native call.
1174  */
1175 static long do_video_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1176 {
1177 	void __user *p32 = compat_ptr(arg);
1178 	void __user *new_p64 = NULL;
1179 	void __user *aux_buf;
1180 	u32 aux_space;
1181 	int compatible_arg = 1;
1182 	long err = 0;
1183 
1184 	/*
1185 	 * 1. When struct size is different, converts the command.
1186 	 */
1187 	switch (cmd) {
1188 	case VIDIOC_G_FMT32: cmd = VIDIOC_G_FMT; break;
1189 	case VIDIOC_S_FMT32: cmd = VIDIOC_S_FMT; break;
1190 	case VIDIOC_QUERYBUF32: cmd = VIDIOC_QUERYBUF; break;
1191 	case VIDIOC_G_FBUF32: cmd = VIDIOC_G_FBUF; break;
1192 	case VIDIOC_S_FBUF32: cmd = VIDIOC_S_FBUF; break;
1193 	case VIDIOC_QBUF32: cmd = VIDIOC_QBUF; break;
1194 	case VIDIOC_DQBUF32: cmd = VIDIOC_DQBUF; break;
1195 	case VIDIOC_ENUMSTD32: cmd = VIDIOC_ENUMSTD; break;
1196 	case VIDIOC_ENUMINPUT32: cmd = VIDIOC_ENUMINPUT; break;
1197 	case VIDIOC_TRY_FMT32: cmd = VIDIOC_TRY_FMT; break;
1198 	case VIDIOC_G_EXT_CTRLS32: cmd = VIDIOC_G_EXT_CTRLS; break;
1199 	case VIDIOC_S_EXT_CTRLS32: cmd = VIDIOC_S_EXT_CTRLS; break;
1200 	case VIDIOC_TRY_EXT_CTRLS32: cmd = VIDIOC_TRY_EXT_CTRLS; break;
1201 	case VIDIOC_DQEVENT32: cmd = VIDIOC_DQEVENT; break;
1202 	case VIDIOC_OVERLAY32: cmd = VIDIOC_OVERLAY; break;
1203 	case VIDIOC_STREAMON32: cmd = VIDIOC_STREAMON; break;
1204 	case VIDIOC_STREAMOFF32: cmd = VIDIOC_STREAMOFF; break;
1205 	case VIDIOC_G_INPUT32: cmd = VIDIOC_G_INPUT; break;
1206 	case VIDIOC_S_INPUT32: cmd = VIDIOC_S_INPUT; break;
1207 	case VIDIOC_G_OUTPUT32: cmd = VIDIOC_G_OUTPUT; break;
1208 	case VIDIOC_S_OUTPUT32: cmd = VIDIOC_S_OUTPUT; break;
1209 	case VIDIOC_CREATE_BUFS32: cmd = VIDIOC_CREATE_BUFS; break;
1210 	case VIDIOC_PREPARE_BUF32: cmd = VIDIOC_PREPARE_BUF; break;
1211 	case VIDIOC_G_EDID32: cmd = VIDIOC_G_EDID; break;
1212 	case VIDIOC_S_EDID32: cmd = VIDIOC_S_EDID; break;
1213 	}
1214 
1215 	/*
1216 	 * 2. Allocates a 64-bits userspace pointer to store the
1217 	 * values of the ioctl and copy data from the 32-bits __user
1218 	 * argument into it.
1219 	 */
1220 	switch (cmd) {
1221 	case VIDIOC_OVERLAY:
1222 	case VIDIOC_STREAMON:
1223 	case VIDIOC_STREAMOFF:
1224 	case VIDIOC_S_INPUT:
1225 	case VIDIOC_S_OUTPUT:
1226 		err = alloc_userspace(sizeof(unsigned int), 0, &new_p64);
1227 		if (!err && assign_in_user((unsigned int __user *)new_p64,
1228 					   (compat_uint_t __user *)p32))
1229 			err = -EFAULT;
1230 		compatible_arg = 0;
1231 		break;
1232 
1233 	case VIDIOC_G_INPUT:
1234 	case VIDIOC_G_OUTPUT:
1235 		err = alloc_userspace(sizeof(unsigned int), 0, &new_p64);
1236 		compatible_arg = 0;
1237 		break;
1238 
1239 	case VIDIOC_G_EDID:
1240 	case VIDIOC_S_EDID:
1241 		err = alloc_userspace(sizeof(struct v4l2_edid), 0, &new_p64);
1242 		if (!err)
1243 			err = get_v4l2_edid32(new_p64, p32);
1244 		compatible_arg = 0;
1245 		break;
1246 
1247 	case VIDIOC_G_FMT:
1248 	case VIDIOC_S_FMT:
1249 	case VIDIOC_TRY_FMT:
1250 		err = bufsize_v4l2_format(p32, &aux_space);
1251 		if (!err)
1252 			err = alloc_userspace(sizeof(struct v4l2_format),
1253 					      aux_space, &new_p64);
1254 		if (!err) {
1255 			aux_buf = new_p64 + sizeof(struct v4l2_format);
1256 			err = get_v4l2_format32(new_p64, p32,
1257 						aux_buf, aux_space);
1258 		}
1259 		compatible_arg = 0;
1260 		break;
1261 
1262 	case VIDIOC_CREATE_BUFS:
1263 		err = bufsize_v4l2_create(p32, &aux_space);
1264 		if (!err)
1265 			err = alloc_userspace(sizeof(struct v4l2_create_buffers),
1266 					      aux_space, &new_p64);
1267 		if (!err) {
1268 			aux_buf = new_p64 + sizeof(struct v4l2_create_buffers);
1269 			err = get_v4l2_create32(new_p64, p32,
1270 						aux_buf, aux_space);
1271 		}
1272 		compatible_arg = 0;
1273 		break;
1274 
1275 	case VIDIOC_PREPARE_BUF:
1276 	case VIDIOC_QUERYBUF:
1277 	case VIDIOC_QBUF:
1278 	case VIDIOC_DQBUF:
1279 		err = bufsize_v4l2_buffer(p32, &aux_space);
1280 		if (!err)
1281 			err = alloc_userspace(sizeof(struct v4l2_buffer),
1282 					      aux_space, &new_p64);
1283 		if (!err) {
1284 			aux_buf = new_p64 + sizeof(struct v4l2_buffer);
1285 			err = get_v4l2_buffer32(new_p64, p32,
1286 						aux_buf, aux_space);
1287 		}
1288 		compatible_arg = 0;
1289 		break;
1290 
1291 	case VIDIOC_S_FBUF:
1292 		err = alloc_userspace(sizeof(struct v4l2_framebuffer), 0,
1293 				      &new_p64);
1294 		if (!err)
1295 			err = get_v4l2_framebuffer32(new_p64, p32);
1296 		compatible_arg = 0;
1297 		break;
1298 
1299 	case VIDIOC_G_FBUF:
1300 		err = alloc_userspace(sizeof(struct v4l2_framebuffer), 0,
1301 				      &new_p64);
1302 		compatible_arg = 0;
1303 		break;
1304 
1305 	case VIDIOC_ENUMSTD:
1306 		err = alloc_userspace(sizeof(struct v4l2_standard), 0,
1307 				      &new_p64);
1308 		if (!err)
1309 			err = get_v4l2_standard32(new_p64, p32);
1310 		compatible_arg = 0;
1311 		break;
1312 
1313 	case VIDIOC_ENUMINPUT:
1314 		err = alloc_userspace(sizeof(struct v4l2_input), 0, &new_p64);
1315 		if (!err)
1316 			err = get_v4l2_input32(new_p64, p32);
1317 		compatible_arg = 0;
1318 		break;
1319 
1320 	case VIDIOC_G_EXT_CTRLS:
1321 	case VIDIOC_S_EXT_CTRLS:
1322 	case VIDIOC_TRY_EXT_CTRLS:
1323 		err = bufsize_v4l2_ext_controls(p32, &aux_space);
1324 		if (!err)
1325 			err = alloc_userspace(sizeof(struct v4l2_ext_controls),
1326 					      aux_space, &new_p64);
1327 		if (!err) {
1328 			aux_buf = new_p64 + sizeof(struct v4l2_ext_controls);
1329 			err = get_v4l2_ext_controls32(file, new_p64, p32,
1330 						      aux_buf, aux_space);
1331 		}
1332 		compatible_arg = 0;
1333 		break;
1334 	case VIDIOC_DQEVENT:
1335 		err = alloc_userspace(sizeof(struct v4l2_event), 0, &new_p64);
1336 		compatible_arg = 0;
1337 		break;
1338 	}
1339 	if (err)
1340 		return err;
1341 
1342 	/*
1343 	 * 3. Calls the native 64-bits ioctl handler.
1344 	 *
1345 	 * For the functions where a conversion was not needed,
1346 	 * compatible_arg is true, and it will call it with the arguments
1347 	 * provided by userspace and stored at @p32 var.
1348 	 *
1349 	 * Otherwise, it will pass the newly allocated @new_p64 argument.
1350 	 */
1351 	if (compatible_arg)
1352 		err = native_ioctl(file, cmd, (unsigned long)p32);
1353 	else
1354 		err = native_ioctl(file, cmd, (unsigned long)new_p64);
1355 
1356 	if (err == -ENOTTY)
1357 		return err;
1358 
1359 	/*
1360 	 * 4. Special case: even after an error we need to put the
1361 	 * results back for some ioctls.
1362 	 *
1363 	 * In the case of EXT_CTRLS, the error_idx will contain information
1364 	 * on which control failed.
1365 	 *
1366 	 * In the case of S_EDID, the driver can return E2BIG and set
1367 	 * the blocks to maximum allowed value.
1368 	 */
1369 	switch (cmd) {
1370 	case VIDIOC_G_EXT_CTRLS:
1371 	case VIDIOC_S_EXT_CTRLS:
1372 	case VIDIOC_TRY_EXT_CTRLS:
1373 		if (put_v4l2_ext_controls32(file, new_p64, p32))
1374 			err = -EFAULT;
1375 		break;
1376 	case VIDIOC_S_EDID:
1377 		if (put_v4l2_edid32(new_p64, p32))
1378 			err = -EFAULT;
1379 		break;
1380 	}
1381 	if (err)
1382 		return err;
1383 
1384 	/*
1385 	 * 5. Copy the data returned at the 64 bits userspace pointer to
1386 	 * the original 32 bits structure.
1387 	 */
1388 	switch (cmd) {
1389 	case VIDIOC_S_INPUT:
1390 	case VIDIOC_S_OUTPUT:
1391 	case VIDIOC_G_INPUT:
1392 	case VIDIOC_G_OUTPUT:
1393 		if (assign_in_user((compat_uint_t __user *)p32,
1394 				   ((unsigned int __user *)new_p64)))
1395 			err = -EFAULT;
1396 		break;
1397 
1398 	case VIDIOC_G_FBUF:
1399 		err = put_v4l2_framebuffer32(new_p64, p32);
1400 		break;
1401 
1402 	case VIDIOC_DQEVENT:
1403 		err = put_v4l2_event32(new_p64, p32);
1404 		break;
1405 
1406 	case VIDIOC_G_EDID:
1407 		err = put_v4l2_edid32(new_p64, p32);
1408 		break;
1409 
1410 	case VIDIOC_G_FMT:
1411 	case VIDIOC_S_FMT:
1412 	case VIDIOC_TRY_FMT:
1413 		err = put_v4l2_format32(new_p64, p32);
1414 		break;
1415 
1416 	case VIDIOC_CREATE_BUFS:
1417 		err = put_v4l2_create32(new_p64, p32);
1418 		break;
1419 
1420 	case VIDIOC_PREPARE_BUF:
1421 	case VIDIOC_QUERYBUF:
1422 	case VIDIOC_QBUF:
1423 	case VIDIOC_DQBUF:
1424 		err = put_v4l2_buffer32(new_p64, p32);
1425 		break;
1426 
1427 	case VIDIOC_ENUMSTD:
1428 		err = put_v4l2_standard32(new_p64, p32);
1429 		break;
1430 
1431 	case VIDIOC_ENUMINPUT:
1432 		err = put_v4l2_input32(new_p64, p32);
1433 		break;
1434 	}
1435 	return err;
1436 }
1437 
1438 /**
1439  * v4l2_compat_ioctl32() - Handles a compat32 ioctl call
1440  *
1441  * @file: pointer to &struct file with the file handler
1442  * @cmd: ioctl to be called
1443  * @arg: arguments passed from/to the ioctl handler
1444  *
1445  * This function is meant to be used as .compat_ioctl fops at v4l2-dev.c
1446  * in order to deal with 32-bit calls on a 64-bits Kernel.
1447  *
1448  * This function calls do_video_ioctl() for non-private V4L2 ioctls.
1449  * If the function is a private one it calls vdev->fops->compat_ioctl32
1450  * instead.
1451  */
1452 long v4l2_compat_ioctl32(struct file *file, unsigned int cmd, unsigned long arg)
1453 {
1454 	struct video_device *vdev = video_devdata(file);
1455 	long ret = -ENOIOCTLCMD;
1456 
1457 	if (!file->f_op->unlocked_ioctl)
1458 		return ret;
1459 
1460 	if (_IOC_TYPE(cmd) == 'V' && _IOC_NR(cmd) < BASE_VIDIOC_PRIVATE)
1461 		ret = do_video_ioctl(file, cmd, arg);
1462 	else if (vdev->fops->compat_ioctl32)
1463 		ret = vdev->fops->compat_ioctl32(file, cmd, arg);
1464 
1465 	if (ret == -ENOIOCTLCMD)
1466 		pr_debug("compat_ioctl32: unknown ioctl '%c', dir=%d, #%d (0x%08x)\n",
1467 			 _IOC_TYPE(cmd), _IOC_DIR(cmd), _IOC_NR(cmd), cmd);
1468 	return ret;
1469 }
1470 EXPORT_SYMBOL_GPL(v4l2_compat_ioctl32);
1471