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