2 * ioctl32.c: Conversion between 32bit and 64bit native ioctls.
3 * Separated from fs stuff by Arnd Bergmann <arnd@arndb.de>
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@suse.cz)
9 * Copyright (C) 2005 Philippe De Muyter (phdm@macqel.be)
11 * These routines maintain argument size conversion between 32bit and 64bit
15 #include <linux/config.h>
16 #include <linux/compat.h>
17 #include <linux/videodev.h>
18 #include <linux/videodev2.h>
19 #include <linux/module.h>
20 #include <linux/smp_lock.h>
23 struct video_tuner32 {
26 compat_ulong_t rangelow, rangehigh;
27 u32 flags; /* It is really u32 in videodev.h */
31 static int get_video_tuner32(struct video_tuner *kp, struct video_tuner32 __user *up)
33 if(get_user(kp->tuner, &up->tuner))
35 __copy_from_user(kp->name, up->name, 32);
36 __get_user(kp->rangelow, &up->rangelow);
37 __get_user(kp->rangehigh, &up->rangehigh);
38 __get_user(kp->flags, &up->flags);
39 __get_user(kp->mode, &up->mode);
40 __get_user(kp->signal, &up->signal);
44 static int put_video_tuner32(struct video_tuner *kp, struct video_tuner32 __user *up)
46 if(put_user(kp->tuner, &up->tuner))
48 __copy_to_user(up->name, kp->name, 32);
49 __put_user(kp->rangelow, &up->rangelow);
50 __put_user(kp->rangehigh, &up->rangehigh);
51 __put_user(kp->flags, &up->flags);
52 __put_user(kp->mode, &up->mode);
53 __put_user(kp->signal, &up->signal);
57 struct video_buffer32 {
59 compat_int_t height, width, depth, bytesperline;
62 static int get_video_buffer32(struct video_buffer *kp, struct video_buffer32 __user *up)
66 if (get_user(tmp, &up->base))
69 /* This is actually a physical address stored
72 kp->base = (void *)(unsigned long) tmp;
74 __get_user(kp->height, &up->height);
75 __get_user(kp->width, &up->width);
76 __get_user(kp->depth, &up->depth);
77 __get_user(kp->bytesperline, &up->bytesperline);
81 static int put_video_buffer32(struct video_buffer *kp, struct video_buffer32 __user *up)
83 u32 tmp = (u32)((unsigned long)kp->base);
85 if(put_user(tmp, &up->base))
87 __put_user(kp->height, &up->height);
88 __put_user(kp->width, &up->width);
89 __put_user(kp->depth, &up->depth);
90 __put_user(kp->bytesperline, &up->bytesperline);
95 s32 x, y, width, height; /* Its really s32 in videodev.h */
99 struct video_window32 {
100 u32 x, y, width, height, chromakey, flags;
101 compat_caddr_t clips;
102 compat_int_t clipcount;
105 static int native_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
107 int ret = -ENOIOCTLCMD;
109 if (file->f_op->unlocked_ioctl)
110 ret = file->f_op->unlocked_ioctl(file, cmd, arg);
111 else if (file->f_op->ioctl) {
113 ret = file->f_op->ioctl(file->f_dentry->d_inode, file, cmd, arg);
121 /* You get back everything except the clips... */
122 static int put_video_window32(struct video_window *kp, struct video_window32 __user *up)
124 if(put_user(kp->x, &up->x))
126 __put_user(kp->y, &up->y);
127 __put_user(kp->width, &up->width);
128 __put_user(kp->height, &up->height);
129 __put_user(kp->chromakey, &up->chromakey);
130 __put_user(kp->flags, &up->flags);
131 __put_user(kp->clipcount, &up->clipcount);
144 enum v4l2_field field;
146 compat_caddr_t clips; /* actually struct v4l2_clip32 * */
148 compat_caddr_t bitmap;
151 static int get_v4l2_window32(struct v4l2_window *kp, struct v4l2_window32 __user *up)
153 if (copy_from_user(&kp->w, &up->w, sizeof(up->w)))
155 __get_user(kp->field, &up->field);
156 __get_user(kp->chromakey, &up->chromakey);
157 __get_user(kp->clipcount, &up->clipcount);
158 if (kp->clipcount > 2048)
161 struct v4l2_clip32 *uclips = compat_ptr(up->clips);
162 struct v4l2_clip *kclips;
163 int n = kp->clipcount;
165 kclips = compat_alloc_user_space(n * sizeof(struct v4l2_clip));
168 copy_from_user(&kclips->c, &uclips->c, sizeof(uclips->c));
169 kclips->next = n ? kclips + 1 : 0;
178 static int put_v4l2_window32(struct v4l2_window *kp, struct v4l2_window32 __user *up)
180 if (copy_to_user(&up->w, &kp->w, sizeof(up->w)))
182 __put_user(kp->field, &up->field);
183 __put_user(kp->chromakey, &up->chromakey);
184 __put_user(kp->clipcount, &up->clipcount);
188 static inline int get_v4l2_pix_format(struct v4l2_pix_format *kp, struct v4l2_pix_format __user *up)
190 return copy_from_user(kp, up, sizeof(struct v4l2_pix_format));
193 static inline int put_v4l2_pix_format(struct v4l2_pix_format *kp, struct v4l2_pix_format __user *up)
195 return copy_to_user(up, kp, sizeof(struct v4l2_pix_format));
198 static inline int get_v4l2_vbi_format(struct v4l2_vbi_format *kp, struct v4l2_vbi_format __user *up)
200 return copy_from_user(kp, up, sizeof(struct v4l2_vbi_format));
203 static inline int put_v4l2_vbi_format(struct v4l2_vbi_format *kp, struct v4l2_vbi_format __user *up)
205 return copy_to_user(up, kp, sizeof(struct v4l2_vbi_format));
210 enum v4l2_buf_type type;
213 struct v4l2_pix_format pix; // V4L2_BUF_TYPE_VIDEO_CAPTURE
214 struct v4l2_window32 win; // V4L2_BUF_TYPE_VIDEO_OVERLAY
215 struct v4l2_vbi_format vbi; // V4L2_BUF_TYPE_VBI_CAPTURE
216 __u8 raw_data[200]; // user-defined
220 static int get_v4l2_format32(struct v4l2_format *kp, struct v4l2_format32 __user *up)
222 if(get_user(kp->type, &up->type))
225 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
226 return get_v4l2_pix_format(&kp->fmt.pix, &up->fmt.pix);
227 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
228 return get_v4l2_window32(&kp->fmt.win, &up->fmt.win);
229 case V4L2_BUF_TYPE_VBI_CAPTURE:
230 return get_v4l2_vbi_format(&kp->fmt.vbi, &up->fmt.vbi);
232 printk("compat_ioctl : unexpected VIDIOC_FMT type %d\n",
238 static int put_v4l2_format32(struct v4l2_format *kp, struct v4l2_format32 __user *up)
240 if(put_user(kp->type, &up->type))
243 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
244 return put_v4l2_pix_format(&kp->fmt.pix, &up->fmt.pix);
245 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
246 return put_v4l2_window32(&kp->fmt.win, &up->fmt.win);
247 case V4L2_BUF_TYPE_VBI_CAPTURE:
248 return put_v4l2_vbi_format(&kp->fmt.vbi, &up->fmt.vbi);
254 struct v4l2_standard32
257 __u32 id[2]; /* __u64 would get the alignment wrong */
259 struct v4l2_fract frameperiod; /* Frames, not fields */
264 static int get_v4l2_standard32(struct v4l2_standard *kp, struct v4l2_standard32 __user *up)
266 /* other fields are not set by the user, nor used by the driver */
267 return get_user(kp->index, &up->index);
270 static int put_v4l2_standard32(struct v4l2_standard *kp, struct v4l2_standard32 __user *up)
272 if(put_user(kp->index, &up->index))
274 __copy_to_user(up->id, &kp->id, sizeof(__u64));
275 __copy_to_user(up->name, kp->name, 24);
276 __put_user(kp->frameperiod, &up->frameperiod);
277 __put_user(kp->framelines, &up->framelines);
278 __copy_to_user(up->reserved, kp->reserved, 4 * sizeof(__u32));
285 enum v4l2_buf_type type;
288 enum v4l2_field field;
289 struct compat_timeval timestamp;
290 struct v4l2_timecode timecode;
293 /* memory location */
294 enum v4l2_memory memory;
297 compat_long_t userptr;
304 static int get_v4l2_buffer32(struct v4l2_buffer *kp, struct v4l2_buffer32 __user *up)
307 if (get_user(kp->index, &up->index))
309 __get_user(kp->type, &up->type);
310 __get_user(kp->flags, &up->flags);
311 __get_user(kp->memory, &up->memory);
312 __get_user(kp->input, &up->input);
314 case V4L2_MEMORY_MMAP:
316 case V4L2_MEMORY_USERPTR:
318 unsigned long tmp = (unsigned long)compat_ptr(up->m.userptr);
320 __get_user(kp->length, &up->length);
321 __get_user(kp->m.userptr, &tmp);
324 case V4L2_MEMORY_OVERLAY:
325 __get_user(kp->m.offset, &up->m.offset);
331 static int put_v4l2_buffer32(struct v4l2_buffer *kp, struct v4l2_buffer32 __user *up)
333 if (put_user(kp->index, &up->index))
335 __put_user(kp->type, &up->type);
336 __put_user(kp->flags, &up->flags);
337 __put_user(kp->memory, &up->memory);
338 __put_user(kp->input, &up->input);
340 case V4L2_MEMORY_MMAP:
341 __put_user(kp->length, &up->length);
342 __put_user(kp->m.offset, &up->m.offset);
344 case V4L2_MEMORY_USERPTR:
345 __put_user(kp->length, &up->length);
346 __put_user(kp->m.userptr, &up->m.userptr);
348 case V4L2_MEMORY_OVERLAY:
349 __put_user(kp->m.offset, &up->m.offset);
352 __put_user(kp->bytesused, &up->bytesused);
353 __put_user(kp->field, &up->field);
354 __put_user(kp->timestamp.tv_sec, &up->timestamp.tv_sec);
355 __put_user(kp->timestamp.tv_usec, &up->timestamp.tv_usec);
356 __copy_to_user(&up->timecode, &kp->timecode, sizeof(struct v4l2_timecode));
357 __put_user(kp->sequence, &up->sequence);
358 __put_user(kp->reserved, &up->reserved);
362 struct v4l2_framebuffer32
367 struct v4l2_pix_format fmt;
370 static int get_v4l2_framebuffer32(struct v4l2_framebuffer *kp, struct v4l2_framebuffer32 __user *up)
374 if (get_user(tmp, &up->base))
376 kp->base = compat_ptr(tmp);
377 __get_user(kp->capability, &up->capability);
378 __get_user(kp->flags, &up->flags);
379 get_v4l2_pix_format(&kp->fmt, &up->fmt);
383 static int put_v4l2_framebuffer32(struct v4l2_framebuffer *kp, struct v4l2_framebuffer32 __user *up)
385 u32 tmp = (u32)((unsigned long)kp->base);
387 if(put_user(tmp, &up->base))
389 __put_user(kp->capability, &up->capability);
390 __put_user(kp->flags, &up->flags);
391 put_v4l2_pix_format(&kp->fmt, &up->fmt);
395 struct v4l2_input32 /* identical layout, but different size */
397 __u32 index; /* Which input */
398 __u8 name[32]; /* Label */
399 __u32 type; /* Type of input */
400 __u32 audioset; /* Associated audios (bitfield) */
401 __u32 tuner; /* Associated tuner */
402 __u32 std[2]; /* __u64 would get the padding wrong */
407 #define VIDIOCGTUNER32 _IOWR('v',4, struct video_tuner32)
408 #define VIDIOCSTUNER32 _IOW('v',5, struct video_tuner32)
409 #define VIDIOCGWIN32 _IOR('v',9, struct video_window32)
410 #define VIDIOCSWIN32 _IOW('v',10, struct video_window32)
411 #define VIDIOCGFBUF32 _IOR('v',11, struct video_buffer32)
412 #define VIDIOCSFBUF32 _IOW('v',12, struct video_buffer32)
413 #define VIDIOCGFREQ32 _IOR('v',14, u32)
414 #define VIDIOCSFREQ32 _IOW('v',15, u32)
416 #define VIDIOC_G_FMT32 _IOWR ('V', 4, struct v4l2_format32)
417 #define VIDIOC_S_FMT32 _IOWR ('V', 5, struct v4l2_format32)
418 #define VIDIOC_QUERYBUF32 _IOWR ('V', 9, struct v4l2_buffer32)
419 #define VIDIOC_G_FBUF32 _IOR ('V', 10, struct v4l2_framebuffer32)
420 #define VIDIOC_S_FBUF32 _IOW ('V', 11, struct v4l2_framebuffer32)
421 /* VIDIOC_OVERLAY is now _IOW, but was _IOWR */
422 #define VIDIOC_OVERLAY32 _IOWR ('V', 14, compat_int_t)
423 #define VIDIOC_QBUF32 _IOWR ('V', 15, struct v4l2_buffer32)
424 #define VIDIOC_DQBUF32 _IOWR ('V', 17, struct v4l2_buffer32)
425 #define VIDIOC_STREAMON32 _IOW ('V', 18, compat_int_t)
426 #define VIDIOC_STREAMOFF32 _IOW ('V', 19, compat_int_t)
427 #define VIDIOC_ENUMSTD32 _IOWR ('V', 25, struct v4l2_standard32)
428 #define VIDIOC_ENUMINPUT32 _IOWR ('V', 26, struct v4l2_input32)
429 /* VIDIOC_S_CTRL is now _IOWR, but was _IOW */
430 #define VIDIOC_S_CTRL32 _IOW ('V', 28, struct v4l2_control)
431 #define VIDIOC_G_INPUT32 _IOR ('V', 38, compat_int_t)
432 #define VIDIOC_S_INPUT32 _IOWR ('V', 39, compat_int_t)
433 #define VIDIOC_TRY_FMT32 _IOWR ('V', 64, struct v4l2_format32)
436 MaxClips = (~0U-sizeof(struct video_window))/sizeof(struct video_clip)
439 static int do_set_window(struct file *file, unsigned int cmd, unsigned long arg)
441 struct video_window32 __user *up = compat_ptr(arg);
442 struct video_window __user *vw;
443 struct video_clip __user *p;
447 if (get_user(nclips, &up->clipcount))
450 /* Peculiar interface... */
452 nclips = VIDEO_CLIPMAP_SIZE;
454 if (nclips > MaxClips)
457 vw = compat_alloc_user_space(sizeof(struct video_window) +
458 nclips * sizeof(struct video_clip));
460 p = nclips ? (struct video_clip __user *)(vw + 1) : NULL;
462 if (get_user(n, &up->x) || put_user(n, &vw->x) ||
463 get_user(n, &up->y) || put_user(n, &vw->y) ||
464 get_user(n, &up->width) || put_user(n, &vw->width) ||
465 get_user(n, &up->height) || put_user(n, &vw->height) ||
466 get_user(n, &up->chromakey) || put_user(n, &vw->chromakey) ||
467 get_user(n, &up->flags) || put_user(n, &vw->flags) ||
468 get_user(n, &up->clipcount) || put_user(n, &vw->clipcount) ||
469 get_user(n, &up->clips) || put_user(p, &vw->clips))
473 struct video_clip32 __user *u = compat_ptr(n);
477 for (i = 0; i < nclips; i++, u++, p++) {
479 if (get_user(v, &u->x) ||
480 put_user(v, &p->x) ||
481 get_user(v, &u->y) ||
482 put_user(v, &p->y) ||
483 get_user(v, &u->width) ||
484 put_user(v, &p->width) ||
485 get_user(v, &u->height) ||
486 put_user(v, &p->height) ||
487 put_user(NULL, &p->next))
492 return native_ioctl(file, VIDIOCSWIN, (unsigned long)vw);
495 static int do_video_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
498 struct video_tuner vt;
499 struct video_buffer vb;
500 struct video_window vw;
501 struct v4l2_format v2f;
502 struct v4l2_buffer v2b;
503 struct v4l2_framebuffer v2fb;
504 struct v4l2_standard v2s;
507 void __user *up = compat_ptr(arg);
508 int compatible_arg = 1;
511 /* First, convert the command. */
513 case VIDIOCGTUNER32: cmd = VIDIOCGTUNER; break;
514 case VIDIOCSTUNER32: cmd = VIDIOCSTUNER; break;
515 case VIDIOCGWIN32: cmd = VIDIOCGWIN; break;
516 case VIDIOCGFBUF32: cmd = VIDIOCGFBUF; break;
517 case VIDIOCSFBUF32: cmd = VIDIOCSFBUF; break;
518 case VIDIOCGFREQ32: cmd = VIDIOCGFREQ; break;
519 case VIDIOCSFREQ32: cmd = VIDIOCSFREQ; break;
520 case VIDIOC_G_FMT32: cmd = VIDIOC_G_FMT; break;
521 case VIDIOC_S_FMT32: cmd = VIDIOC_S_FMT; break;
522 case VIDIOC_QUERYBUF32: cmd = VIDIOC_QUERYBUF; break;
523 case VIDIOC_QBUF32: cmd = VIDIOC_QBUF; break;
524 case VIDIOC_DQBUF32: cmd = VIDIOC_DQBUF; break;
525 case VIDIOC_STREAMON32: cmd = VIDIOC_STREAMON; break;
526 case VIDIOC_STREAMOFF32: cmd = VIDIOC_STREAMOFF; break;
527 case VIDIOC_G_FBUF32: cmd = VIDIOC_G_FBUF; break;
528 case VIDIOC_S_FBUF32: cmd = VIDIOC_S_FBUF; break;
529 case VIDIOC_OVERLAY32: cmd = VIDIOC_OVERLAY; break;
530 case VIDIOC_ENUMSTD32: cmd = VIDIOC_ENUMSTD; break;
531 case VIDIOC_ENUMINPUT32: cmd = VIDIOC_ENUMINPUT; break;
532 case VIDIOC_S_CTRL32: cmd = VIDIOC_S_CTRL; break;
533 case VIDIOC_G_INPUT32: cmd = VIDIOC_G_INPUT; break;
534 case VIDIOC_S_INPUT32: cmd = VIDIOC_S_INPUT; break;
535 case VIDIOC_TRY_FMT32: cmd = VIDIOC_TRY_FMT; break;
541 err = get_video_tuner32(&karg.vt, up);
547 err = get_video_buffer32(&karg.vb, up);
554 case VIDIOC_STREAMON:
555 case VIDIOC_STREAMOFF:
556 err = get_user(karg.vx, (u32 __user *)up);
561 err = get_v4l2_framebuffer32(&karg.v2fb, up);
568 err = get_v4l2_format32(&karg.v2f, up);
572 case VIDIOC_QUERYBUF:
575 err = get_v4l2_buffer32(&karg.v2b, up);
580 err = get_v4l2_standard32(&karg.v2s, up);
596 err = native_ioctl(file, cmd, (unsigned long)up);
598 mm_segment_t old_fs = get_fs();
601 err = native_ioctl(file, cmd, (unsigned long)&karg);
607 err = put_video_tuner32(&karg.vt, up);
611 err = put_video_window32(&karg.vw, up);
615 err = put_video_buffer32(&karg.vb, up);
619 err = put_v4l2_framebuffer32(&karg.v2fb, up);
625 err = put_v4l2_format32(&karg.v2f, up);
628 case VIDIOC_QUERYBUF:
631 err = put_v4l2_buffer32(&karg.v2b, up);
635 err = put_v4l2_standard32(&karg.v2s, up);
640 err = put_user(((u32)karg.vx), (u32 __user *)up);
648 long v4l_compat_ioctl32(struct file *file, unsigned int cmd, unsigned long arg)
650 int ret = -ENOIOCTLCMD;
652 if (!file->f_op->ioctl)
657 ret = do_set_window(file, cmd, arg);
666 case VIDIOC_QUERYCAP:
667 case VIDIOC_ENUM_FMT:
671 case VIDIOC_QUERYBUF32:
672 case VIDIOC_G_FBUF32:
673 case VIDIOC_S_FBUF32:
674 case VIDIOC_OVERLAY32:
677 case VIDIOC_STREAMON32:
678 case VIDIOC_STREAMOFF32:
682 case VIDIOC_ENUMSTD32:
683 case VIDIOC_ENUMINPUT32:
685 case VIDIOC_S_CTRL32:
686 case VIDIOC_QUERYCTRL:
687 case VIDIOC_G_INPUT32:
688 case VIDIOC_S_INPUT32:
689 case VIDIOC_TRY_FMT32:
690 ret = do_video_ioctl(file, cmd, arg);
693 /* Little v, the video4linux ioctls (conflict?) */
710 /* BTTV specific... */
711 case _IOW('v', BASE_VIDIOCPRIVATE+0, char [256]):
712 case _IOR('v', BASE_VIDIOCPRIVATE+1, char [256]):
713 case _IOR('v' , BASE_VIDIOCPRIVATE+2, unsigned int):
714 case _IOW('v' , BASE_VIDIOCPRIVATE+3, char [16]): /* struct bttv_pll_info */
715 case _IOR('v' , BASE_VIDIOCPRIVATE+4, int):
716 case _IOR('v' , BASE_VIDIOCPRIVATE+5, int):
717 case _IOR('v' , BASE_VIDIOCPRIVATE+6, int):
718 case _IOR('v' , BASE_VIDIOCPRIVATE+7, int):
719 ret = native_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
725 long v4l_compat_ioctl32(struct file *file, unsigned int cmd, unsigned long arg)
730 EXPORT_SYMBOL_GPL(v4l_compat_ioctl32);
732 MODULE_LICENSE("GPL");