Merge branch 'v4l_for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mchehab...
[firefly-linux-kernel-4.4.55.git] / drivers / media / video / v4l2-compat-ioctl32.c
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 <media/v4l2-dev.h>
20 #include <media/v4l2-ioctl.h>
21
22 static long native_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
23 {
24         long ret = -ENOIOCTLCMD;
25
26         if (file->f_op->unlocked_ioctl)
27                 ret = file->f_op->unlocked_ioctl(file, cmd, arg);
28
29         return ret;
30 }
31
32
33 struct v4l2_clip32 {
34         struct v4l2_rect        c;
35         compat_caddr_t          next;
36 };
37
38 struct v4l2_window32 {
39         struct v4l2_rect        w;
40         __u32                   field;  /* enum v4l2_field */
41         __u32                   chromakey;
42         compat_caddr_t          clips; /* actually struct v4l2_clip32 * */
43         __u32                   clipcount;
44         compat_caddr_t          bitmap;
45 };
46
47 static int get_v4l2_window32(struct v4l2_window *kp, struct v4l2_window32 __user *up)
48 {
49         if (!access_ok(VERIFY_READ, up, sizeof(struct v4l2_window32)) ||
50                 copy_from_user(&kp->w, &up->w, sizeof(up->w)) ||
51                 get_user(kp->field, &up->field) ||
52                 get_user(kp->chromakey, &up->chromakey) ||
53                 get_user(kp->clipcount, &up->clipcount))
54                         return -EFAULT;
55         if (kp->clipcount > 2048)
56                 return -EINVAL;
57         if (kp->clipcount) {
58                 struct v4l2_clip32 __user *uclips;
59                 struct v4l2_clip __user *kclips;
60                 int n = kp->clipcount;
61                 compat_caddr_t p;
62
63                 if (get_user(p, &up->clips))
64                         return -EFAULT;
65                 uclips = compat_ptr(p);
66                 kclips = compat_alloc_user_space(n * sizeof(struct v4l2_clip));
67                 kp->clips = kclips;
68                 while (--n >= 0) {
69                         if (copy_in_user(&kclips->c, &uclips->c, sizeof(uclips->c)))
70                                 return -EFAULT;
71                         if (put_user(n ? kclips + 1 : NULL, &kclips->next))
72                                 return -EFAULT;
73                         uclips += 1;
74                         kclips += 1;
75                 }
76         } else
77                 kp->clips = NULL;
78         return 0;
79 }
80
81 static int put_v4l2_window32(struct v4l2_window *kp, struct v4l2_window32 __user *up)
82 {
83         if (copy_to_user(&up->w, &kp->w, sizeof(kp->w)) ||
84                 put_user(kp->field, &up->field) ||
85                 put_user(kp->chromakey, &up->chromakey) ||
86                 put_user(kp->clipcount, &up->clipcount))
87                         return -EFAULT;
88         return 0;
89 }
90
91 static inline int get_v4l2_pix_format(struct v4l2_pix_format *kp, struct v4l2_pix_format __user *up)
92 {
93         if (copy_from_user(kp, up, sizeof(struct v4l2_pix_format)))
94                 return -EFAULT;
95         return 0;
96 }
97
98 static inline int get_v4l2_pix_format_mplane(struct v4l2_pix_format_mplane *kp,
99                                 struct v4l2_pix_format_mplane __user *up)
100 {
101         if (copy_from_user(kp, up, sizeof(struct v4l2_pix_format_mplane)))
102                 return -EFAULT;
103         return 0;
104 }
105
106 static inline int put_v4l2_pix_format(struct v4l2_pix_format *kp, struct v4l2_pix_format __user *up)
107 {
108         if (copy_to_user(up, kp, sizeof(struct v4l2_pix_format)))
109                 return -EFAULT;
110         return 0;
111 }
112
113 static inline int put_v4l2_pix_format_mplane(struct v4l2_pix_format_mplane *kp,
114                                 struct v4l2_pix_format_mplane __user *up)
115 {
116         if (copy_to_user(up, kp, sizeof(struct v4l2_pix_format_mplane)))
117                 return -EFAULT;
118         return 0;
119 }
120
121 static inline int get_v4l2_vbi_format(struct v4l2_vbi_format *kp, struct v4l2_vbi_format __user *up)
122 {
123         if (copy_from_user(kp, up, sizeof(struct v4l2_vbi_format)))
124                 return -EFAULT;
125         return 0;
126 }
127
128 static inline int put_v4l2_vbi_format(struct v4l2_vbi_format *kp, struct v4l2_vbi_format __user *up)
129 {
130         if (copy_to_user(up, kp, sizeof(struct v4l2_vbi_format)))
131                 return -EFAULT;
132         return 0;
133 }
134
135 static inline int get_v4l2_sliced_vbi_format(struct v4l2_sliced_vbi_format *kp, struct v4l2_sliced_vbi_format __user *up)
136 {
137         if (copy_from_user(kp, up, sizeof(struct v4l2_sliced_vbi_format)))
138                 return -EFAULT;
139         return 0;
140 }
141
142 static inline int put_v4l2_sliced_vbi_format(struct v4l2_sliced_vbi_format *kp, struct v4l2_sliced_vbi_format __user *up)
143 {
144         if (copy_to_user(up, kp, sizeof(struct v4l2_sliced_vbi_format)))
145                 return -EFAULT;
146         return 0;
147 }
148
149 struct v4l2_format32 {
150         __u32   type;   /* enum v4l2_buf_type */
151         union {
152                 struct v4l2_pix_format  pix;
153                 struct v4l2_pix_format_mplane   pix_mp;
154                 struct v4l2_window32    win;
155                 struct v4l2_vbi_format  vbi;
156                 struct v4l2_sliced_vbi_format   sliced;
157                 __u8    raw_data[200];        /* user-defined */
158         } fmt;
159 };
160
161 /**
162  * struct v4l2_create_buffers32 - VIDIOC_CREATE_BUFS32 argument
163  * @index:      on return, index of the first created buffer
164  * @count:      entry: number of requested buffers,
165  *              return: number of created buffers
166  * @memory:     buffer memory type
167  * @format:     frame format, for which buffers are requested
168  * @reserved:   future extensions
169  */
170 struct v4l2_create_buffers32 {
171         __u32                   index;
172         __u32                   count;
173         __u32                   memory; /* enum v4l2_memory */
174         struct v4l2_format32    format;
175         __u32                   reserved[8];
176 };
177
178 static int __get_v4l2_format32(struct v4l2_format *kp, struct v4l2_format32 __user *up)
179 {
180         switch (kp->type) {
181         case V4L2_BUF_TYPE_VIDEO_CAPTURE:
182         case V4L2_BUF_TYPE_VIDEO_OUTPUT:
183                 return get_v4l2_pix_format(&kp->fmt.pix, &up->fmt.pix);
184         case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
185         case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
186                 return get_v4l2_pix_format_mplane(&kp->fmt.pix_mp,
187                                                   &up->fmt.pix_mp);
188         case V4L2_BUF_TYPE_VIDEO_OVERLAY:
189         case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
190                 return get_v4l2_window32(&kp->fmt.win, &up->fmt.win);
191         case V4L2_BUF_TYPE_VBI_CAPTURE:
192         case V4L2_BUF_TYPE_VBI_OUTPUT:
193                 return get_v4l2_vbi_format(&kp->fmt.vbi, &up->fmt.vbi);
194         case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
195         case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
196                 return get_v4l2_sliced_vbi_format(&kp->fmt.sliced, &up->fmt.sliced);
197         case V4L2_BUF_TYPE_PRIVATE:
198                 if (copy_from_user(kp, up, sizeof(kp->fmt.raw_data)))
199                         return -EFAULT;
200                 return 0;
201         default:
202                 printk(KERN_INFO "compat_ioctl32: unexpected VIDIOC_FMT type %d\n",
203                                                                 kp->type);
204                 return -EINVAL;
205         }
206 }
207
208 static int get_v4l2_format32(struct v4l2_format *kp, struct v4l2_format32 __user *up)
209 {
210         if (!access_ok(VERIFY_READ, up, sizeof(struct v4l2_format32)) ||
211                         get_user(kp->type, &up->type))
212                         return -EFAULT;
213         return __get_v4l2_format32(kp, up);
214 }
215
216 static int get_v4l2_create32(struct v4l2_create_buffers *kp, struct v4l2_create_buffers32 __user *up)
217 {
218         if (!access_ok(VERIFY_READ, up, sizeof(struct v4l2_create_buffers32)) ||
219             copy_from_user(kp, up, offsetof(struct v4l2_create_buffers32, format.fmt)))
220                         return -EFAULT;
221         return __get_v4l2_format32(&kp->format, &up->format);
222 }
223
224 static int __put_v4l2_format32(struct v4l2_format *kp, struct v4l2_format32 __user *up)
225 {
226         switch (kp->type) {
227         case V4L2_BUF_TYPE_VIDEO_CAPTURE:
228         case V4L2_BUF_TYPE_VIDEO_OUTPUT:
229                 return put_v4l2_pix_format(&kp->fmt.pix, &up->fmt.pix);
230         case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
231         case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
232                 return put_v4l2_pix_format_mplane(&kp->fmt.pix_mp,
233                                                   &up->fmt.pix_mp);
234         case V4L2_BUF_TYPE_VIDEO_OVERLAY:
235         case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
236                 return put_v4l2_window32(&kp->fmt.win, &up->fmt.win);
237         case V4L2_BUF_TYPE_VBI_CAPTURE:
238         case V4L2_BUF_TYPE_VBI_OUTPUT:
239                 return put_v4l2_vbi_format(&kp->fmt.vbi, &up->fmt.vbi);
240         case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
241         case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
242                 return put_v4l2_sliced_vbi_format(&kp->fmt.sliced, &up->fmt.sliced);
243         case V4L2_BUF_TYPE_PRIVATE:
244                 if (copy_to_user(up, kp, sizeof(up->fmt.raw_data)))
245                         return -EFAULT;
246                 return 0;
247         default:
248                 printk(KERN_INFO "compat_ioctl32: unexpected VIDIOC_FMT type %d\n",
249                                                                 kp->type);
250                 return -EINVAL;
251         }
252 }
253
254 static int put_v4l2_format32(struct v4l2_format *kp, struct v4l2_format32 __user *up)
255 {
256         if (!access_ok(VERIFY_WRITE, up, sizeof(struct v4l2_format32)) ||
257                 put_user(kp->type, &up->type))
258                 return -EFAULT;
259         return __put_v4l2_format32(kp, up);
260 }
261
262 static int put_v4l2_create32(struct v4l2_create_buffers *kp, struct v4l2_create_buffers32 __user *up)
263 {
264         if (!access_ok(VERIFY_WRITE, up, sizeof(struct v4l2_create_buffers32)) ||
265             copy_to_user(up, kp, offsetof(struct v4l2_create_buffers32, format.fmt)))
266                         return -EFAULT;
267         return __put_v4l2_format32(&kp->format, &up->format);
268 }
269
270 struct v4l2_standard32 {
271         __u32                index;
272         __u32                id[2]; /* __u64 would get the alignment wrong */
273         __u8                 name[24];
274         struct v4l2_fract    frameperiod; /* Frames, not fields */
275         __u32                framelines;
276         __u32                reserved[4];
277 };
278
279 static int get_v4l2_standard32(struct v4l2_standard *kp, struct v4l2_standard32 __user *up)
280 {
281         /* other fields are not set by the user, nor used by the driver */
282         if (!access_ok(VERIFY_READ, up, sizeof(struct v4l2_standard32)) ||
283                 get_user(kp->index, &up->index))
284                 return -EFAULT;
285         return 0;
286 }
287
288 static int put_v4l2_standard32(struct v4l2_standard *kp, struct v4l2_standard32 __user *up)
289 {
290         if (!access_ok(VERIFY_WRITE, up, sizeof(struct v4l2_standard32)) ||
291                 put_user(kp->index, &up->index) ||
292                 copy_to_user(up->id, &kp->id, sizeof(__u64)) ||
293                 copy_to_user(up->name, kp->name, 24) ||
294                 copy_to_user(&up->frameperiod, &kp->frameperiod, sizeof(kp->frameperiod)) ||
295                 put_user(kp->framelines, &up->framelines) ||
296                 copy_to_user(up->reserved, kp->reserved, 4 * sizeof(__u32)))
297                         return -EFAULT;
298         return 0;
299 }
300
301 struct v4l2_plane32 {
302         __u32                   bytesused;
303         __u32                   length;
304         union {
305                 __u32           mem_offset;
306                 compat_long_t   userptr;
307         } m;
308         __u32                   data_offset;
309         __u32                   reserved[11];
310 };
311
312 struct v4l2_buffer32 {
313         __u32                   index;
314         __u32                   type;   /* enum v4l2_buf_type */
315         __u32                   bytesused;
316         __u32                   flags;
317         __u32                   field;  /* enum v4l2_field */
318         struct compat_timeval   timestamp;
319         struct v4l2_timecode    timecode;
320         __u32                   sequence;
321
322         /* memory location */
323         __u32                   memory; /* enum v4l2_memory */
324         union {
325                 __u32           offset;
326                 compat_long_t   userptr;
327                 compat_caddr_t  planes;
328         } m;
329         __u32                   length;
330         __u32                   reserved2;
331         __u32                   reserved;
332 };
333
334 static int get_v4l2_plane32(struct v4l2_plane *up, struct v4l2_plane32 *up32,
335                                 enum v4l2_memory memory)
336 {
337         void __user *up_pln;
338         compat_long_t p;
339
340         if (copy_in_user(up, up32, 2 * sizeof(__u32)) ||
341                 copy_in_user(&up->data_offset, &up32->data_offset,
342                                 sizeof(__u32)))
343                 return -EFAULT;
344
345         if (memory == V4L2_MEMORY_USERPTR) {
346                 if (get_user(p, &up32->m.userptr))
347                         return -EFAULT;
348                 up_pln = compat_ptr(p);
349                 if (put_user((unsigned long)up_pln, &up->m.userptr))
350                         return -EFAULT;
351         } else {
352                 if (copy_in_user(&up->m.mem_offset, &up32->m.mem_offset,
353                                         sizeof(__u32)))
354                         return -EFAULT;
355         }
356
357         return 0;
358 }
359
360 static int put_v4l2_plane32(struct v4l2_plane *up, struct v4l2_plane32 *up32,
361                                 enum v4l2_memory memory)
362 {
363         if (copy_in_user(up32, up, 2 * sizeof(__u32)) ||
364                 copy_in_user(&up32->data_offset, &up->data_offset,
365                                 sizeof(__u32)))
366                 return -EFAULT;
367
368         /* For MMAP, driver might've set up the offset, so copy it back.
369          * USERPTR stays the same (was userspace-provided), so no copying. */
370         if (memory == V4L2_MEMORY_MMAP)
371                 if (copy_in_user(&up32->m.mem_offset, &up->m.mem_offset,
372                                         sizeof(__u32)))
373                         return -EFAULT;
374
375         return 0;
376 }
377
378 static int get_v4l2_buffer32(struct v4l2_buffer *kp, struct v4l2_buffer32 __user *up)
379 {
380         struct v4l2_plane32 __user *uplane32;
381         struct v4l2_plane __user *uplane;
382         compat_caddr_t p;
383         int num_planes;
384         int ret;
385
386         if (!access_ok(VERIFY_READ, up, sizeof(struct v4l2_buffer32)) ||
387                 get_user(kp->index, &up->index) ||
388                 get_user(kp->type, &up->type) ||
389                 get_user(kp->flags, &up->flags) ||
390                 get_user(kp->memory, &up->memory))
391                         return -EFAULT;
392
393         if (V4L2_TYPE_IS_OUTPUT(kp->type))
394                 if (get_user(kp->bytesused, &up->bytesused) ||
395                         get_user(kp->field, &up->field) ||
396                         get_user(kp->timestamp.tv_sec, &up->timestamp.tv_sec) ||
397                         get_user(kp->timestamp.tv_usec,
398                                         &up->timestamp.tv_usec))
399                         return -EFAULT;
400
401         if (V4L2_TYPE_IS_MULTIPLANAR(kp->type)) {
402                 if (get_user(kp->length, &up->length))
403                         return -EFAULT;
404
405                 num_planes = kp->length;
406                 if (num_planes == 0) {
407                         kp->m.planes = NULL;
408                         /* num_planes == 0 is legal, e.g. when userspace doesn't
409                          * need planes array on DQBUF*/
410                         return 0;
411                 }
412
413                 if (get_user(p, &up->m.planes))
414                         return -EFAULT;
415
416                 uplane32 = compat_ptr(p);
417                 if (!access_ok(VERIFY_READ, uplane32,
418                                 num_planes * sizeof(struct v4l2_plane32)))
419                         return -EFAULT;
420
421                 /* We don't really care if userspace decides to kill itself
422                  * by passing a very big num_planes value */
423                 uplane = compat_alloc_user_space(num_planes *
424                                                 sizeof(struct v4l2_plane));
425                 kp->m.planes = uplane;
426
427                 while (--num_planes >= 0) {
428                         ret = get_v4l2_plane32(uplane, uplane32, kp->memory);
429                         if (ret)
430                                 return ret;
431                         ++uplane;
432                         ++uplane32;
433                 }
434         } else {
435                 switch (kp->memory) {
436                 case V4L2_MEMORY_MMAP:
437                         if (get_user(kp->length, &up->length) ||
438                                 get_user(kp->m.offset, &up->m.offset))
439                                 return -EFAULT;
440                         break;
441                 case V4L2_MEMORY_USERPTR:
442                         {
443                         compat_long_t tmp;
444
445                         if (get_user(kp->length, &up->length) ||
446                             get_user(tmp, &up->m.userptr))
447                                 return -EFAULT;
448
449                         kp->m.userptr = (unsigned long)compat_ptr(tmp);
450                         }
451                         break;
452                 case V4L2_MEMORY_OVERLAY:
453                         if (get_user(kp->m.offset, &up->m.offset))
454                                 return -EFAULT;
455                         break;
456                 }
457         }
458
459         return 0;
460 }
461
462 static int put_v4l2_buffer32(struct v4l2_buffer *kp, struct v4l2_buffer32 __user *up)
463 {
464         struct v4l2_plane32 __user *uplane32;
465         struct v4l2_plane __user *uplane;
466         compat_caddr_t p;
467         int num_planes;
468         int ret;
469
470         if (!access_ok(VERIFY_WRITE, up, sizeof(struct v4l2_buffer32)) ||
471                 put_user(kp->index, &up->index) ||
472                 put_user(kp->type, &up->type) ||
473                 put_user(kp->flags, &up->flags) ||
474                 put_user(kp->memory, &up->memory))
475                         return -EFAULT;
476
477         if (put_user(kp->bytesused, &up->bytesused) ||
478                 put_user(kp->field, &up->field) ||
479                 put_user(kp->timestamp.tv_sec, &up->timestamp.tv_sec) ||
480                 put_user(kp->timestamp.tv_usec, &up->timestamp.tv_usec) ||
481                 copy_to_user(&up->timecode, &kp->timecode, sizeof(struct v4l2_timecode)) ||
482                 put_user(kp->sequence, &up->sequence) ||
483                 put_user(kp->reserved2, &up->reserved2) ||
484                 put_user(kp->reserved, &up->reserved))
485                         return -EFAULT;
486
487         if (V4L2_TYPE_IS_MULTIPLANAR(kp->type)) {
488                 num_planes = kp->length;
489                 if (num_planes == 0)
490                         return 0;
491
492                 uplane = kp->m.planes;
493                 if (get_user(p, &up->m.planes))
494                         return -EFAULT;
495                 uplane32 = compat_ptr(p);
496
497                 while (--num_planes >= 0) {
498                         ret = put_v4l2_plane32(uplane, uplane32, kp->memory);
499                         if (ret)
500                                 return ret;
501                         ++uplane;
502                         ++uplane32;
503                 }
504         } else {
505                 switch (kp->memory) {
506                 case V4L2_MEMORY_MMAP:
507                         if (put_user(kp->length, &up->length) ||
508                                 put_user(kp->m.offset, &up->m.offset))
509                                 return -EFAULT;
510                         break;
511                 case V4L2_MEMORY_USERPTR:
512                         if (put_user(kp->length, &up->length) ||
513                                 put_user(kp->m.userptr, &up->m.userptr))
514                                 return -EFAULT;
515                         break;
516                 case V4L2_MEMORY_OVERLAY:
517                         if (put_user(kp->m.offset, &up->m.offset))
518                                 return -EFAULT;
519                         break;
520                 }
521         }
522
523         return 0;
524 }
525
526 struct v4l2_framebuffer32 {
527         __u32                   capability;
528         __u32                   flags;
529         compat_caddr_t          base;
530         struct v4l2_pix_format  fmt;
531 };
532
533 static int get_v4l2_framebuffer32(struct v4l2_framebuffer *kp, struct v4l2_framebuffer32 __user *up)
534 {
535         u32 tmp;
536
537         if (!access_ok(VERIFY_READ, up, sizeof(struct v4l2_framebuffer32)) ||
538                 get_user(tmp, &up->base) ||
539                 get_user(kp->capability, &up->capability) ||
540                 get_user(kp->flags, &up->flags))
541                         return -EFAULT;
542         kp->base = compat_ptr(tmp);
543         get_v4l2_pix_format(&kp->fmt, &up->fmt);
544         return 0;
545 }
546
547 static int put_v4l2_framebuffer32(struct v4l2_framebuffer *kp, struct v4l2_framebuffer32 __user *up)
548 {
549         u32 tmp = (u32)((unsigned long)kp->base);
550
551         if (!access_ok(VERIFY_WRITE, up, sizeof(struct v4l2_framebuffer32)) ||
552                 put_user(tmp, &up->base) ||
553                 put_user(kp->capability, &up->capability) ||
554                 put_user(kp->flags, &up->flags))
555                         return -EFAULT;
556         put_v4l2_pix_format(&kp->fmt, &up->fmt);
557         return 0;
558 }
559
560 struct v4l2_input32 {
561         __u32        index;             /*  Which input */
562         __u8         name[32];          /*  Label */
563         __u32        type;              /*  Type of input */
564         __u32        audioset;          /*  Associated audios (bitfield) */
565         __u32        tuner;             /*  Associated tuner */
566         v4l2_std_id  std;
567         __u32        status;
568         __u32        reserved[4];
569 } __attribute__ ((packed));
570
571 /* The 64-bit v4l2_input struct has extra padding at the end of the struct.
572    Otherwise it is identical to the 32-bit version. */
573 static inline int get_v4l2_input32(struct v4l2_input *kp, struct v4l2_input32 __user *up)
574 {
575         if (copy_from_user(kp, up, sizeof(struct v4l2_input32)))
576                 return -EFAULT;
577         return 0;
578 }
579
580 static inline int put_v4l2_input32(struct v4l2_input *kp, struct v4l2_input32 __user *up)
581 {
582         if (copy_to_user(up, kp, sizeof(struct v4l2_input32)))
583                 return -EFAULT;
584         return 0;
585 }
586
587 struct v4l2_ext_controls32 {
588        __u32 ctrl_class;
589        __u32 count;
590        __u32 error_idx;
591        __u32 reserved[2];
592        compat_caddr_t controls; /* actually struct v4l2_ext_control32 * */
593 };
594
595 struct v4l2_ext_control32 {
596         __u32 id;
597         __u32 size;
598         __u32 reserved2[1];
599         union {
600                 __s32 value;
601                 __s64 value64;
602                 compat_caddr_t string; /* actually char * */
603         };
604 } __attribute__ ((packed));
605
606 /* The following function really belong in v4l2-common, but that causes
607    a circular dependency between modules. We need to think about this, but
608    for now this will do. */
609
610 /* Return non-zero if this control is a pointer type. Currently only
611    type STRING is a pointer type. */
612 static inline int ctrl_is_pointer(u32 id)
613 {
614         switch (id) {
615         case V4L2_CID_RDS_TX_PS_NAME:
616         case V4L2_CID_RDS_TX_RADIO_TEXT:
617                 return 1;
618         default:
619                 return 0;
620         }
621 }
622
623 static int get_v4l2_ext_controls32(struct v4l2_ext_controls *kp, struct v4l2_ext_controls32 __user *up)
624 {
625         struct v4l2_ext_control32 __user *ucontrols;
626         struct v4l2_ext_control __user *kcontrols;
627         int n;
628         compat_caddr_t p;
629
630         if (!access_ok(VERIFY_READ, up, sizeof(struct v4l2_ext_controls32)) ||
631                 get_user(kp->ctrl_class, &up->ctrl_class) ||
632                 get_user(kp->count, &up->count) ||
633                 get_user(kp->error_idx, &up->error_idx) ||
634                 copy_from_user(kp->reserved, up->reserved, sizeof(kp->reserved)))
635                         return -EFAULT;
636         n = kp->count;
637         if (n == 0) {
638                 kp->controls = NULL;
639                 return 0;
640         }
641         if (get_user(p, &up->controls))
642                 return -EFAULT;
643         ucontrols = compat_ptr(p);
644         if (!access_ok(VERIFY_READ, ucontrols,
645                         n * sizeof(struct v4l2_ext_control32)))
646                 return -EFAULT;
647         kcontrols = compat_alloc_user_space(n * sizeof(struct v4l2_ext_control));
648         kp->controls = kcontrols;
649         while (--n >= 0) {
650                 if (copy_in_user(kcontrols, ucontrols, sizeof(*ucontrols)))
651                         return -EFAULT;
652                 if (ctrl_is_pointer(kcontrols->id)) {
653                         void __user *s;
654
655                         if (get_user(p, &ucontrols->string))
656                                 return -EFAULT;
657                         s = compat_ptr(p);
658                         if (put_user(s, &kcontrols->string))
659                                 return -EFAULT;
660                 }
661                 ucontrols++;
662                 kcontrols++;
663         }
664         return 0;
665 }
666
667 static int put_v4l2_ext_controls32(struct v4l2_ext_controls *kp, struct v4l2_ext_controls32 __user *up)
668 {
669         struct v4l2_ext_control32 __user *ucontrols;
670         struct v4l2_ext_control __user *kcontrols = kp->controls;
671         int n = kp->count;
672         compat_caddr_t p;
673
674         if (!access_ok(VERIFY_WRITE, up, sizeof(struct v4l2_ext_controls32)) ||
675                 put_user(kp->ctrl_class, &up->ctrl_class) ||
676                 put_user(kp->count, &up->count) ||
677                 put_user(kp->error_idx, &up->error_idx) ||
678                 copy_to_user(up->reserved, kp->reserved, sizeof(up->reserved)))
679                         return -EFAULT;
680         if (!kp->count)
681                 return 0;
682
683         if (get_user(p, &up->controls))
684                 return -EFAULT;
685         ucontrols = compat_ptr(p);
686         if (!access_ok(VERIFY_WRITE, ucontrols,
687                         n * sizeof(struct v4l2_ext_control32)))
688                 return -EFAULT;
689
690         while (--n >= 0) {
691                 unsigned size = sizeof(*ucontrols);
692
693                 /* Do not modify the pointer when copying a pointer control.
694                    The contents of the pointer was changed, not the pointer
695                    itself. */
696                 if (ctrl_is_pointer(kcontrols->id))
697                         size -= sizeof(ucontrols->value64);
698                 if (copy_in_user(ucontrols, kcontrols, size))
699                         return -EFAULT;
700                 ucontrols++;
701                 kcontrols++;
702         }
703         return 0;
704 }
705
706 struct v4l2_event32 {
707         __u32                           type;
708         union {
709                 __u8                    data[64];
710         } u;
711         __u32                           pending;
712         __u32                           sequence;
713         struct compat_timespec          timestamp;
714         __u32                           id;
715         __u32                           reserved[8];
716 };
717
718 static int put_v4l2_event32(struct v4l2_event *kp, struct v4l2_event32 __user *up)
719 {
720         if (!access_ok(VERIFY_WRITE, up, sizeof(struct v4l2_event32)) ||
721                 put_user(kp->type, &up->type) ||
722                 copy_to_user(&up->u, &kp->u, sizeof(kp->u)) ||
723                 put_user(kp->pending, &up->pending) ||
724                 put_user(kp->sequence, &up->sequence) ||
725                 put_compat_timespec(&kp->timestamp, &up->timestamp) ||
726                 put_user(kp->id, &up->id) ||
727                 copy_to_user(up->reserved, kp->reserved, 8 * sizeof(__u32)))
728                         return -EFAULT;
729         return 0;
730 }
731
732 #define VIDIOC_G_FMT32          _IOWR('V',  4, struct v4l2_format32)
733 #define VIDIOC_S_FMT32          _IOWR('V',  5, struct v4l2_format32)
734 #define VIDIOC_QUERYBUF32       _IOWR('V',  9, struct v4l2_buffer32)
735 #define VIDIOC_G_FBUF32         _IOR ('V', 10, struct v4l2_framebuffer32)
736 #define VIDIOC_S_FBUF32         _IOW ('V', 11, struct v4l2_framebuffer32)
737 #define VIDIOC_QBUF32           _IOWR('V', 15, struct v4l2_buffer32)
738 #define VIDIOC_DQBUF32          _IOWR('V', 17, struct v4l2_buffer32)
739 #define VIDIOC_ENUMSTD32        _IOWR('V', 25, struct v4l2_standard32)
740 #define VIDIOC_ENUMINPUT32      _IOWR('V', 26, struct v4l2_input32)
741 #define VIDIOC_TRY_FMT32        _IOWR('V', 64, struct v4l2_format32)
742 #define VIDIOC_G_EXT_CTRLS32    _IOWR('V', 71, struct v4l2_ext_controls32)
743 #define VIDIOC_S_EXT_CTRLS32    _IOWR('V', 72, struct v4l2_ext_controls32)
744 #define VIDIOC_TRY_EXT_CTRLS32  _IOWR('V', 73, struct v4l2_ext_controls32)
745 #define VIDIOC_DQEVENT32        _IOR ('V', 89, struct v4l2_event32)
746 #define VIDIOC_CREATE_BUFS32    _IOWR('V', 92, struct v4l2_create_buffers32)
747 #define VIDIOC_PREPARE_BUF32    _IOWR('V', 93, struct v4l2_buffer32)
748
749 #define VIDIOC_OVERLAY32        _IOW ('V', 14, s32)
750 #define VIDIOC_STREAMON32       _IOW ('V', 18, s32)
751 #define VIDIOC_STREAMOFF32      _IOW ('V', 19, s32)
752 #define VIDIOC_G_INPUT32        _IOR ('V', 38, s32)
753 #define VIDIOC_S_INPUT32        _IOWR('V', 39, s32)
754 #define VIDIOC_G_OUTPUT32       _IOR ('V', 46, s32)
755 #define VIDIOC_S_OUTPUT32       _IOWR('V', 47, s32)
756
757 static long do_video_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
758 {
759         union {
760                 struct v4l2_format v2f;
761                 struct v4l2_buffer v2b;
762                 struct v4l2_framebuffer v2fb;
763                 struct v4l2_input v2i;
764                 struct v4l2_standard v2s;
765                 struct v4l2_ext_controls v2ecs;
766                 struct v4l2_event v2ev;
767                 struct v4l2_create_buffers v2crt;
768                 unsigned long vx;
769                 int vi;
770         } karg;
771         void __user *up = compat_ptr(arg);
772         int compatible_arg = 1;
773         long err = 0;
774
775         /* First, convert the command. */
776         switch (cmd) {
777         case VIDIOC_G_FMT32: cmd = VIDIOC_G_FMT; break;
778         case VIDIOC_S_FMT32: cmd = VIDIOC_S_FMT; break;
779         case VIDIOC_QUERYBUF32: cmd = VIDIOC_QUERYBUF; break;
780         case VIDIOC_G_FBUF32: cmd = VIDIOC_G_FBUF; break;
781         case VIDIOC_S_FBUF32: cmd = VIDIOC_S_FBUF; break;
782         case VIDIOC_QBUF32: cmd = VIDIOC_QBUF; break;
783         case VIDIOC_DQBUF32: cmd = VIDIOC_DQBUF; break;
784         case VIDIOC_ENUMSTD32: cmd = VIDIOC_ENUMSTD; break;
785         case VIDIOC_ENUMINPUT32: cmd = VIDIOC_ENUMINPUT; break;
786         case VIDIOC_TRY_FMT32: cmd = VIDIOC_TRY_FMT; break;
787         case VIDIOC_G_EXT_CTRLS32: cmd = VIDIOC_G_EXT_CTRLS; break;
788         case VIDIOC_S_EXT_CTRLS32: cmd = VIDIOC_S_EXT_CTRLS; break;
789         case VIDIOC_TRY_EXT_CTRLS32: cmd = VIDIOC_TRY_EXT_CTRLS; break;
790         case VIDIOC_DQEVENT32: cmd = VIDIOC_DQEVENT; break;
791         case VIDIOC_OVERLAY32: cmd = VIDIOC_OVERLAY; break;
792         case VIDIOC_STREAMON32: cmd = VIDIOC_STREAMON; break;
793         case VIDIOC_STREAMOFF32: cmd = VIDIOC_STREAMOFF; break;
794         case VIDIOC_G_INPUT32: cmd = VIDIOC_G_INPUT; break;
795         case VIDIOC_S_INPUT32: cmd = VIDIOC_S_INPUT; break;
796         case VIDIOC_G_OUTPUT32: cmd = VIDIOC_G_OUTPUT; break;
797         case VIDIOC_S_OUTPUT32: cmd = VIDIOC_S_OUTPUT; break;
798         case VIDIOC_CREATE_BUFS32: cmd = VIDIOC_CREATE_BUFS; break;
799         case VIDIOC_PREPARE_BUF32: cmd = VIDIOC_PREPARE_BUF; break;
800         }
801
802         switch (cmd) {
803         case VIDIOC_OVERLAY:
804         case VIDIOC_STREAMON:
805         case VIDIOC_STREAMOFF:
806         case VIDIOC_S_INPUT:
807         case VIDIOC_S_OUTPUT:
808                 err = get_user(karg.vi, (s32 __user *)up);
809                 compatible_arg = 0;
810                 break;
811
812         case VIDIOC_G_INPUT:
813         case VIDIOC_G_OUTPUT:
814                 compatible_arg = 0;
815                 break;
816
817         case VIDIOC_G_FMT:
818         case VIDIOC_S_FMT:
819         case VIDIOC_TRY_FMT:
820                 err = get_v4l2_format32(&karg.v2f, up);
821                 compatible_arg = 0;
822                 break;
823
824         case VIDIOC_CREATE_BUFS:
825                 err = get_v4l2_create32(&karg.v2crt, up);
826                 compatible_arg = 0;
827                 break;
828
829         case VIDIOC_PREPARE_BUF:
830         case VIDIOC_QUERYBUF:
831         case VIDIOC_QBUF:
832         case VIDIOC_DQBUF:
833                 err = get_v4l2_buffer32(&karg.v2b, up);
834                 compatible_arg = 0;
835                 break;
836
837         case VIDIOC_S_FBUF:
838                 err = get_v4l2_framebuffer32(&karg.v2fb, up);
839                 compatible_arg = 0;
840                 break;
841
842         case VIDIOC_G_FBUF:
843                 compatible_arg = 0;
844                 break;
845
846         case VIDIOC_ENUMSTD:
847                 err = get_v4l2_standard32(&karg.v2s, up);
848                 compatible_arg = 0;
849                 break;
850
851         case VIDIOC_ENUMINPUT:
852                 err = get_v4l2_input32(&karg.v2i, up);
853                 compatible_arg = 0;
854                 break;
855
856         case VIDIOC_G_EXT_CTRLS:
857         case VIDIOC_S_EXT_CTRLS:
858         case VIDIOC_TRY_EXT_CTRLS:
859                 err = get_v4l2_ext_controls32(&karg.v2ecs, up);
860                 compatible_arg = 0;
861                 break;
862         case VIDIOC_DQEVENT:
863                 compatible_arg = 0;
864                 break;
865         }
866         if (err)
867                 return err;
868
869         if (compatible_arg)
870                 err = native_ioctl(file, cmd, (unsigned long)up);
871         else {
872                 mm_segment_t old_fs = get_fs();
873
874                 set_fs(KERNEL_DS);
875                 err = native_ioctl(file, cmd, (unsigned long)&karg);
876                 set_fs(old_fs);
877         }
878
879         /* Special case: even after an error we need to put the
880            results back for these ioctls since the error_idx will
881            contain information on which control failed. */
882         switch (cmd) {
883         case VIDIOC_G_EXT_CTRLS:
884         case VIDIOC_S_EXT_CTRLS:
885         case VIDIOC_TRY_EXT_CTRLS:
886                 if (put_v4l2_ext_controls32(&karg.v2ecs, up))
887                         err = -EFAULT;
888                 break;
889         }
890         if (err)
891                 return err;
892
893         switch (cmd) {
894         case VIDIOC_S_INPUT:
895         case VIDIOC_S_OUTPUT:
896         case VIDIOC_G_INPUT:
897         case VIDIOC_G_OUTPUT:
898                 err = put_user(((s32)karg.vi), (s32 __user *)up);
899                 break;
900
901         case VIDIOC_G_FBUF:
902                 err = put_v4l2_framebuffer32(&karg.v2fb, up);
903                 break;
904
905         case VIDIOC_DQEVENT:
906                 err = put_v4l2_event32(&karg.v2ev, up);
907                 break;
908
909         case VIDIOC_G_FMT:
910         case VIDIOC_S_FMT:
911         case VIDIOC_TRY_FMT:
912                 err = put_v4l2_format32(&karg.v2f, up);
913                 break;
914
915         case VIDIOC_CREATE_BUFS:
916                 err = put_v4l2_create32(&karg.v2crt, up);
917                 break;
918
919         case VIDIOC_QUERYBUF:
920         case VIDIOC_QBUF:
921         case VIDIOC_DQBUF:
922                 err = put_v4l2_buffer32(&karg.v2b, up);
923                 break;
924
925         case VIDIOC_ENUMSTD:
926                 err = put_v4l2_standard32(&karg.v2s, up);
927                 break;
928
929         case VIDIOC_ENUMINPUT:
930                 err = put_v4l2_input32(&karg.v2i, up);
931                 break;
932         }
933         return err;
934 }
935
936 long v4l2_compat_ioctl32(struct file *file, unsigned int cmd, unsigned long arg)
937 {
938         struct video_device *vdev = video_devdata(file);
939         long ret = -ENOIOCTLCMD;
940
941         if (!file->f_op->unlocked_ioctl)
942                 return ret;
943
944         switch (cmd) {
945         case VIDIOC_QUERYCAP:
946         case VIDIOC_RESERVED:
947         case VIDIOC_ENUM_FMT:
948         case VIDIOC_G_FMT32:
949         case VIDIOC_S_FMT32:
950         case VIDIOC_REQBUFS:
951         case VIDIOC_QUERYBUF32:
952         case VIDIOC_G_FBUF32:
953         case VIDIOC_S_FBUF32:
954         case VIDIOC_OVERLAY32:
955         case VIDIOC_QBUF32:
956         case VIDIOC_DQBUF32:
957         case VIDIOC_STREAMON32:
958         case VIDIOC_STREAMOFF32:
959         case VIDIOC_G_PARM:
960         case VIDIOC_S_PARM:
961         case VIDIOC_G_STD:
962         case VIDIOC_S_STD:
963         case VIDIOC_ENUMSTD32:
964         case VIDIOC_ENUMINPUT32:
965         case VIDIOC_G_CTRL:
966         case VIDIOC_S_CTRL:
967         case VIDIOC_G_TUNER:
968         case VIDIOC_S_TUNER:
969         case VIDIOC_G_AUDIO:
970         case VIDIOC_S_AUDIO:
971         case VIDIOC_QUERYCTRL:
972         case VIDIOC_QUERYMENU:
973         case VIDIOC_G_INPUT32:
974         case VIDIOC_S_INPUT32:
975         case VIDIOC_G_OUTPUT32:
976         case VIDIOC_S_OUTPUT32:
977         case VIDIOC_ENUMOUTPUT:
978         case VIDIOC_G_AUDOUT:
979         case VIDIOC_S_AUDOUT:
980         case VIDIOC_G_MODULATOR:
981         case VIDIOC_S_MODULATOR:
982         case VIDIOC_S_FREQUENCY:
983         case VIDIOC_G_FREQUENCY:
984         case VIDIOC_CROPCAP:
985         case VIDIOC_G_CROP:
986         case VIDIOC_S_CROP:
987         case VIDIOC_G_SELECTION:
988         case VIDIOC_S_SELECTION:
989         case VIDIOC_G_JPEGCOMP:
990         case VIDIOC_S_JPEGCOMP:
991         case VIDIOC_QUERYSTD:
992         case VIDIOC_TRY_FMT32:
993         case VIDIOC_ENUMAUDIO:
994         case VIDIOC_ENUMAUDOUT:
995         case VIDIOC_G_PRIORITY:
996         case VIDIOC_S_PRIORITY:
997         case VIDIOC_G_SLICED_VBI_CAP:
998         case VIDIOC_LOG_STATUS:
999         case VIDIOC_G_EXT_CTRLS32:
1000         case VIDIOC_S_EXT_CTRLS32:
1001         case VIDIOC_TRY_EXT_CTRLS32:
1002         case VIDIOC_ENUM_FRAMESIZES:
1003         case VIDIOC_ENUM_FRAMEINTERVALS:
1004         case VIDIOC_G_ENC_INDEX:
1005         case VIDIOC_ENCODER_CMD:
1006         case VIDIOC_TRY_ENCODER_CMD:
1007         case VIDIOC_DECODER_CMD:
1008         case VIDIOC_TRY_DECODER_CMD:
1009         case VIDIOC_DBG_S_REGISTER:
1010         case VIDIOC_DBG_G_REGISTER:
1011         case VIDIOC_DBG_G_CHIP_IDENT:
1012         case VIDIOC_S_HW_FREQ_SEEK:
1013         case VIDIOC_ENUM_DV_PRESETS:
1014         case VIDIOC_S_DV_PRESET:
1015         case VIDIOC_G_DV_PRESET:
1016         case VIDIOC_QUERY_DV_PRESET:
1017         case VIDIOC_S_DV_TIMINGS:
1018         case VIDIOC_G_DV_TIMINGS:
1019         case VIDIOC_DQEVENT:
1020         case VIDIOC_DQEVENT32:
1021         case VIDIOC_SUBSCRIBE_EVENT:
1022         case VIDIOC_UNSUBSCRIBE_EVENT:
1023         case VIDIOC_CREATE_BUFS32:
1024         case VIDIOC_PREPARE_BUF32:
1025         case VIDIOC_ENUM_DV_TIMINGS:
1026         case VIDIOC_QUERY_DV_TIMINGS:
1027         case VIDIOC_DV_TIMINGS_CAP:
1028                 ret = do_video_ioctl(file, cmd, arg);
1029                 break;
1030
1031         default:
1032                 if (vdev->fops->compat_ioctl32)
1033                         ret = vdev->fops->compat_ioctl32(file, cmd, arg);
1034
1035                 if (ret == -ENOIOCTLCMD)
1036                         printk(KERN_WARNING "compat_ioctl32: "
1037                                 "unknown ioctl '%c', dir=%d, #%d (0x%08x)\n",
1038                                 _IOC_TYPE(cmd), _IOC_DIR(cmd), _IOC_NR(cmd),
1039                                 cmd);
1040                 break;
1041         }
1042         return ret;
1043 }
1044 EXPORT_SYMBOL_GPL(v4l2_compat_ioctl32);