hidp: fix compat_ioctl
[linux-2.6-microblaze.git] / fs / compat_ioctl.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * ioctl32.c: Conversion between 32bit and 64bit native ioctls.
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  *
10  * These routines maintain argument size conversion between 32bit and 64bit
11  * ioctls.
12  */
13
14 #include <linux/joystick.h>
15
16 #include <linux/types.h>
17 #include <linux/compat.h>
18 #include <linux/kernel.h>
19 #include <linux/capability.h>
20 #include <linux/compiler.h>
21 #include <linux/sched.h>
22 #include <linux/smp.h>
23 #include <linux/ioctl.h>
24 #include <linux/if.h>
25 #include <linux/raid/md_u.h>
26 #include <linux/falloc.h>
27 #include <linux/file.h>
28 #include <linux/ppp-ioctl.h>
29 #include <linux/if_pppox.h>
30 #include <linux/mtio.h>
31 #include <linux/tty.h>
32 #include <linux/vt_kern.h>
33 #include <linux/raw.h>
34 #include <linux/blkdev.h>
35 #include <linux/rtc.h>
36 #include <linux/pci.h>
37 #include <linux/serial.h>
38 #include <linux/ctype.h>
39 #include <linux/syscalls.h>
40 #include <linux/gfp.h>
41 #include <linux/cec.h>
42
43 #include "internal.h"
44
45 #include <net/bluetooth/bluetooth.h>
46 #include <net/bluetooth/hci_sock.h>
47 #include <net/bluetooth/rfcomm.h>
48
49 #include <linux/capi.h>
50 #include <linux/gigaset_dev.h>
51
52 #ifdef CONFIG_BLOCK
53 #include <linux/cdrom.h>
54 #include <linux/fd.h>
55 #include <scsi/scsi.h>
56 #include <scsi/scsi_ioctl.h>
57 #include <scsi/sg.h>
58 #endif
59
60 #include <linux/uaccess.h>
61 #include <linux/watchdog.h>
62
63 #include <linux/soundcard.h>
64
65 #include <linux/hiddev.h>
66
67 #define __DVB_CORE__
68 #include <linux/dvb/audio.h>
69 #include <linux/dvb/dmx.h>
70 #include <linux/dvb/frontend.h>
71 #include <linux/dvb/video.h>
72
73 #include <linux/sort.h>
74
75 #ifdef CONFIG_SPARC
76 #include <linux/fb.h>
77 #include <asm/fbio.h>
78 #endif
79
80 #define convert_in_user(srcptr, dstptr)                 \
81 ({                                                      \
82         typeof(*srcptr) val;                            \
83                                                         \
84         get_user(val, srcptr) || put_user(val, dstptr); \
85 })
86
87 static int do_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
88 {
89         int err;
90
91         err = security_file_ioctl(file, cmd, arg);
92         if (err)
93                 return err;
94
95         return vfs_ioctl(file, cmd, arg);
96 }
97
98 struct compat_video_event {
99         int32_t         type;
100         compat_time_t   timestamp;
101         union {
102                 video_size_t size;
103                 unsigned int frame_rate;
104         } u;
105 };
106
107 static int do_video_get_event(struct file *file,
108                 unsigned int cmd, struct compat_video_event __user *up)
109 {
110         struct video_event __user *kevent =
111                 compat_alloc_user_space(sizeof(*kevent));
112         int err;
113
114         if (kevent == NULL)
115                 return -EFAULT;
116
117         err = do_ioctl(file, cmd, (unsigned long)kevent);
118         if (!err) {
119                 err  = convert_in_user(&kevent->type, &up->type);
120                 err |= convert_in_user(&kevent->timestamp, &up->timestamp);
121                 err |= convert_in_user(&kevent->u.size.w, &up->u.size.w);
122                 err |= convert_in_user(&kevent->u.size.h, &up->u.size.h);
123                 err |= convert_in_user(&kevent->u.size.aspect_ratio,
124                                 &up->u.size.aspect_ratio);
125                 if (err)
126                         err = -EFAULT;
127         }
128
129         return err;
130 }
131
132 struct compat_video_still_picture {
133         compat_uptr_t iFrame;
134         int32_t size;
135 };
136
137 static int do_video_stillpicture(struct file *file,
138                 unsigned int cmd, struct compat_video_still_picture __user *up)
139 {
140         struct video_still_picture __user *up_native;
141         compat_uptr_t fp;
142         int32_t size;
143         int err;
144
145         err  = get_user(fp, &up->iFrame);
146         err |= get_user(size, &up->size);
147         if (err)
148                 return -EFAULT;
149
150         up_native =
151                 compat_alloc_user_space(sizeof(struct video_still_picture));
152
153         err =  put_user(compat_ptr(fp), &up_native->iFrame);
154         err |= put_user(size, &up_native->size);
155         if (err)
156                 return -EFAULT;
157
158         err = do_ioctl(file, cmd, (unsigned long) up_native);
159
160         return err;
161 }
162
163 struct compat_video_spu_palette {
164         int length;
165         compat_uptr_t palette;
166 };
167
168 static int do_video_set_spu_palette(struct file *file,
169                 unsigned int cmd, struct compat_video_spu_palette __user *up)
170 {
171         struct video_spu_palette __user *up_native;
172         compat_uptr_t palp;
173         int length, err;
174
175         err  = get_user(palp, &up->palette);
176         err |= get_user(length, &up->length);
177         if (err)
178                 return -EFAULT;
179
180         up_native = compat_alloc_user_space(sizeof(struct video_spu_palette));
181         err  = put_user(compat_ptr(palp), &up_native->palette);
182         err |= put_user(length, &up_native->length);
183         if (err)
184                 return -EFAULT;
185
186         err = do_ioctl(file, cmd, (unsigned long) up_native);
187
188         return err;
189 }
190
191 #ifdef CONFIG_BLOCK
192 typedef struct sg_io_hdr32 {
193         compat_int_t interface_id;      /* [i] 'S' for SCSI generic (required) */
194         compat_int_t dxfer_direction;   /* [i] data transfer direction  */
195         unsigned char cmd_len;          /* [i] SCSI command length ( <= 16 bytes) */
196         unsigned char mx_sb_len;                /* [i] max length to write to sbp */
197         unsigned short iovec_count;     /* [i] 0 implies no scatter gather */
198         compat_uint_t dxfer_len;                /* [i] byte count of data transfer */
199         compat_uint_t dxferp;           /* [i], [*io] points to data transfer memory
200                                               or scatter gather list */
201         compat_uptr_t cmdp;             /* [i], [*i] points to command to perform */
202         compat_uptr_t sbp;              /* [i], [*o] points to sense_buffer memory */
203         compat_uint_t timeout;          /* [i] MAX_UINT->no timeout (unit: millisec) */
204         compat_uint_t flags;            /* [i] 0 -> default, see SG_FLAG... */
205         compat_int_t pack_id;           /* [i->o] unused internally (normally) */
206         compat_uptr_t usr_ptr;          /* [i->o] unused internally */
207         unsigned char status;           /* [o] scsi status */
208         unsigned char masked_status;    /* [o] shifted, masked scsi status */
209         unsigned char msg_status;               /* [o] messaging level data (optional) */
210         unsigned char sb_len_wr;                /* [o] byte count actually written to sbp */
211         unsigned short host_status;     /* [o] errors from host adapter */
212         unsigned short driver_status;   /* [o] errors from software driver */
213         compat_int_t resid;             /* [o] dxfer_len - actual_transferred */
214         compat_uint_t duration;         /* [o] time taken by cmd (unit: millisec) */
215         compat_uint_t info;             /* [o] auxiliary information */
216 } sg_io_hdr32_t;  /* 64 bytes long (on sparc32) */
217
218 typedef struct sg_iovec32 {
219         compat_uint_t iov_base;
220         compat_uint_t iov_len;
221 } sg_iovec32_t;
222
223 static int sg_build_iovec(sg_io_hdr_t __user *sgio, void __user *dxferp, u16 iovec_count)
224 {
225         sg_iovec_t __user *iov = (sg_iovec_t __user *) (sgio + 1);
226         sg_iovec32_t __user *iov32 = dxferp;
227         int i;
228
229         for (i = 0; i < iovec_count; i++) {
230                 u32 base, len;
231
232                 if (get_user(base, &iov32[i].iov_base) ||
233                     get_user(len, &iov32[i].iov_len) ||
234                     put_user(compat_ptr(base), &iov[i].iov_base) ||
235                     put_user(len, &iov[i].iov_len))
236                         return -EFAULT;
237         }
238
239         if (put_user(iov, &sgio->dxferp))
240                 return -EFAULT;
241         return 0;
242 }
243
244 static int sg_ioctl_trans(struct file *file, unsigned int cmd,
245                         sg_io_hdr32_t __user *sgio32)
246 {
247         sg_io_hdr_t __user *sgio;
248         u16 iovec_count;
249         u32 data;
250         void __user *dxferp;
251         int err;
252         int interface_id;
253
254         if (get_user(interface_id, &sgio32->interface_id))
255                 return -EFAULT;
256         if (interface_id != 'S')
257                 return do_ioctl(file, cmd, (unsigned long)sgio32);
258
259         if (get_user(iovec_count, &sgio32->iovec_count))
260                 return -EFAULT;
261
262         {
263                 void __user *top = compat_alloc_user_space(0);
264                 void __user *new = compat_alloc_user_space(sizeof(sg_io_hdr_t) +
265                                        (iovec_count * sizeof(sg_iovec_t)));
266                 if (new > top)
267                         return -EINVAL;
268
269                 sgio = new;
270         }
271
272         /* Ok, now construct.  */
273         if (copy_in_user(&sgio->interface_id, &sgio32->interface_id,
274                          (2 * sizeof(int)) +
275                          (2 * sizeof(unsigned char)) +
276                          (1 * sizeof(unsigned short)) +
277                          (1 * sizeof(unsigned int))))
278                 return -EFAULT;
279
280         if (get_user(data, &sgio32->dxferp))
281                 return -EFAULT;
282         dxferp = compat_ptr(data);
283         if (iovec_count) {
284                 if (sg_build_iovec(sgio, dxferp, iovec_count))
285                         return -EFAULT;
286         } else {
287                 if (put_user(dxferp, &sgio->dxferp))
288                         return -EFAULT;
289         }
290
291         {
292                 unsigned char __user *cmdp;
293                 unsigned char __user *sbp;
294
295                 if (get_user(data, &sgio32->cmdp))
296                         return -EFAULT;
297                 cmdp = compat_ptr(data);
298
299                 if (get_user(data, &sgio32->sbp))
300                         return -EFAULT;
301                 sbp = compat_ptr(data);
302
303                 if (put_user(cmdp, &sgio->cmdp) ||
304                     put_user(sbp, &sgio->sbp))
305                         return -EFAULT;
306         }
307
308         if (copy_in_user(&sgio->timeout, &sgio32->timeout,
309                          3 * sizeof(int)))
310                 return -EFAULT;
311
312         if (get_user(data, &sgio32->usr_ptr))
313                 return -EFAULT;
314         if (put_user(compat_ptr(data), &sgio->usr_ptr))
315                 return -EFAULT;
316
317         err = do_ioctl(file, cmd, (unsigned long) sgio);
318
319         if (err >= 0) {
320                 void __user *datap;
321
322                 if (copy_in_user(&sgio32->pack_id, &sgio->pack_id,
323                                  sizeof(int)) ||
324                     get_user(datap, &sgio->usr_ptr) ||
325                     put_user((u32)(unsigned long)datap,
326                              &sgio32->usr_ptr) ||
327                     copy_in_user(&sgio32->status, &sgio->status,
328                                  (4 * sizeof(unsigned char)) +
329                                  (2 * sizeof(unsigned short)) +
330                                  (3 * sizeof(int))))
331                         err = -EFAULT;
332         }
333
334         return err;
335 }
336
337 struct compat_sg_req_info { /* used by SG_GET_REQUEST_TABLE ioctl() */
338         char req_state;
339         char orphan;
340         char sg_io_owned;
341         char problem;
342         int pack_id;
343         compat_uptr_t usr_ptr;
344         unsigned int duration;
345         int unused;
346 };
347
348 static int sg_grt_trans(struct file *file,
349                 unsigned int cmd, struct compat_sg_req_info __user *o)
350 {
351         int err, i;
352         sg_req_info_t __user *r;
353         r = compat_alloc_user_space(sizeof(sg_req_info_t)*SG_MAX_QUEUE);
354         err = do_ioctl(file, cmd, (unsigned long)r);
355         if (err < 0)
356                 return err;
357         for (i = 0; i < SG_MAX_QUEUE; i++) {
358                 void __user *ptr;
359                 int d;
360
361                 if (copy_in_user(o + i, r + i, offsetof(sg_req_info_t, usr_ptr)) ||
362                     get_user(ptr, &r[i].usr_ptr) ||
363                     get_user(d, &r[i].duration) ||
364                     put_user((u32)(unsigned long)(ptr), &o[i].usr_ptr) ||
365                     put_user(d, &o[i].duration))
366                         return -EFAULT;
367         }
368         return err;
369 }
370 #endif /* CONFIG_BLOCK */
371
372 struct sock_fprog32 {
373         unsigned short  len;
374         compat_caddr_t  filter;
375 };
376
377 #define PPPIOCSPASS32   _IOW('t', 71, struct sock_fprog32)
378 #define PPPIOCSACTIVE32 _IOW('t', 70, struct sock_fprog32)
379
380 static int ppp_sock_fprog_ioctl_trans(struct file *file,
381                 unsigned int cmd, struct sock_fprog32 __user *u_fprog32)
382 {
383         struct sock_fprog __user *u_fprog64 = compat_alloc_user_space(sizeof(struct sock_fprog));
384         void __user *fptr64;
385         u32 fptr32;
386         u16 flen;
387
388         if (get_user(flen, &u_fprog32->len) ||
389             get_user(fptr32, &u_fprog32->filter))
390                 return -EFAULT;
391
392         fptr64 = compat_ptr(fptr32);
393
394         if (put_user(flen, &u_fprog64->len) ||
395             put_user(fptr64, &u_fprog64->filter))
396                 return -EFAULT;
397
398         if (cmd == PPPIOCSPASS32)
399                 cmd = PPPIOCSPASS;
400         else
401                 cmd = PPPIOCSACTIVE;
402
403         return do_ioctl(file, cmd, (unsigned long) u_fprog64);
404 }
405
406 struct ppp_option_data32 {
407         compat_caddr_t  ptr;
408         u32                     length;
409         compat_int_t            transmit;
410 };
411 #define PPPIOCSCOMPRESS32       _IOW('t', 77, struct ppp_option_data32)
412
413 struct ppp_idle32 {
414         compat_time_t xmit_idle;
415         compat_time_t recv_idle;
416 };
417 #define PPPIOCGIDLE32           _IOR('t', 63, struct ppp_idle32)
418
419 static int ppp_gidle(struct file *file, unsigned int cmd,
420                 struct ppp_idle32 __user *idle32)
421 {
422         struct ppp_idle __user *idle;
423         __kernel_time_t xmit, recv;
424         int err;
425
426         idle = compat_alloc_user_space(sizeof(*idle));
427
428         err = do_ioctl(file, PPPIOCGIDLE, (unsigned long) idle);
429
430         if (!err) {
431                 if (get_user(xmit, &idle->xmit_idle) ||
432                     get_user(recv, &idle->recv_idle) ||
433                     put_user(xmit, &idle32->xmit_idle) ||
434                     put_user(recv, &idle32->recv_idle))
435                         err = -EFAULT;
436         }
437         return err;
438 }
439
440 static int ppp_scompress(struct file *file, unsigned int cmd,
441         struct ppp_option_data32 __user *odata32)
442 {
443         struct ppp_option_data __user *odata;
444         __u32 data;
445         void __user *datap;
446
447         odata = compat_alloc_user_space(sizeof(*odata));
448
449         if (get_user(data, &odata32->ptr))
450                 return -EFAULT;
451
452         datap = compat_ptr(data);
453         if (put_user(datap, &odata->ptr))
454                 return -EFAULT;
455
456         if (copy_in_user(&odata->length, &odata32->length,
457                          sizeof(__u32) + sizeof(int)))
458                 return -EFAULT;
459
460         return do_ioctl(file, PPPIOCSCOMPRESS, (unsigned long) odata);
461 }
462
463 #ifdef CONFIG_BLOCK
464 struct mtget32 {
465         compat_long_t   mt_type;
466         compat_long_t   mt_resid;
467         compat_long_t   mt_dsreg;
468         compat_long_t   mt_gstat;
469         compat_long_t   mt_erreg;
470         compat_daddr_t  mt_fileno;
471         compat_daddr_t  mt_blkno;
472 };
473 #define MTIOCGET32      _IOR('m', 2, struct mtget32)
474
475 struct mtpos32 {
476         compat_long_t   mt_blkno;
477 };
478 #define MTIOCPOS32      _IOR('m', 3, struct mtpos32)
479
480 static int mt_ioctl_trans(struct file *file,
481                 unsigned int cmd, void __user *argp)
482 {
483         /* NULL initialization to make gcc shut up */
484         struct mtget __user *get = NULL;
485         struct mtget32 __user *umget32;
486         struct mtpos __user *pos = NULL;
487         struct mtpos32 __user *upos32;
488         unsigned long kcmd;
489         void *karg;
490         int err = 0;
491
492         switch(cmd) {
493         case MTIOCPOS32:
494                 kcmd = MTIOCPOS;
495                 pos = compat_alloc_user_space(sizeof(*pos));
496                 karg = pos;
497                 break;
498         default:        /* MTIOCGET32 */
499                 kcmd = MTIOCGET;
500                 get = compat_alloc_user_space(sizeof(*get));
501                 karg = get;
502                 break;
503         }
504         if (karg == NULL)
505                 return -EFAULT;
506         err = do_ioctl(file, kcmd, (unsigned long)karg);
507         if (err)
508                 return err;
509         switch (cmd) {
510         case MTIOCPOS32:
511                 upos32 = argp;
512                 err = convert_in_user(&pos->mt_blkno, &upos32->mt_blkno);
513                 break;
514         case MTIOCGET32:
515                 umget32 = argp;
516                 err = convert_in_user(&get->mt_type, &umget32->mt_type);
517                 err |= convert_in_user(&get->mt_resid, &umget32->mt_resid);
518                 err |= convert_in_user(&get->mt_dsreg, &umget32->mt_dsreg);
519                 err |= convert_in_user(&get->mt_gstat, &umget32->mt_gstat);
520                 err |= convert_in_user(&get->mt_erreg, &umget32->mt_erreg);
521                 err |= convert_in_user(&get->mt_fileno, &umget32->mt_fileno);
522                 err |= convert_in_user(&get->mt_blkno, &umget32->mt_blkno);
523                 break;
524         }
525         return err ? -EFAULT: 0;
526 }
527
528 #endif /* CONFIG_BLOCK */
529
530 /* Bluetooth ioctls */
531 #define HCIUARTSETPROTO         _IOW('U', 200, int)
532 #define HCIUARTGETPROTO         _IOR('U', 201, int)
533 #define HCIUARTGETDEVICE        _IOR('U', 202, int)
534 #define HCIUARTSETFLAGS         _IOW('U', 203, int)
535 #define HCIUARTGETFLAGS         _IOR('U', 204, int)
536
537 struct serial_struct32 {
538         compat_int_t    type;
539         compat_int_t    line;
540         compat_uint_t   port;
541         compat_int_t    irq;
542         compat_int_t    flags;
543         compat_int_t    xmit_fifo_size;
544         compat_int_t    custom_divisor;
545         compat_int_t    baud_base;
546         unsigned short  close_delay;
547         char    io_type;
548         char    reserved_char[1];
549         compat_int_t    hub6;
550         unsigned short  closing_wait; /* time to wait before closing */
551         unsigned short  closing_wait2; /* no longer used... */
552         compat_uint_t   iomem_base;
553         unsigned short  iomem_reg_shift;
554         unsigned int    port_high;
555      /* compat_ulong_t  iomap_base FIXME */
556         compat_int_t    reserved[1];
557 };
558
559 static int serial_struct_ioctl(struct file *file,
560                 unsigned cmd, struct serial_struct32 __user *ss32)
561 {
562         typedef struct serial_struct32 SS32;
563         int err;
564         struct serial_struct __user *ss = compat_alloc_user_space(sizeof(*ss));
565         __u32 udata;
566         unsigned int base;
567         unsigned char *iomem_base;
568
569         if (ss == NULL)
570                 return -EFAULT;
571         if (cmd == TIOCSSERIAL) {
572                 if (copy_in_user(ss, ss32, offsetof(SS32, iomem_base)) ||
573                     get_user(udata, &ss32->iomem_base))
574                         return -EFAULT;
575                 iomem_base = compat_ptr(udata);
576                 if (put_user(iomem_base, &ss->iomem_base) ||
577                     convert_in_user(&ss32->iomem_reg_shift,
578                       &ss->iomem_reg_shift) ||
579                     convert_in_user(&ss32->port_high, &ss->port_high) ||
580                     put_user(0UL, &ss->iomap_base))
581                         return -EFAULT;
582         }
583         err = do_ioctl(file, cmd, (unsigned long)ss);
584         if (cmd == TIOCGSERIAL && err >= 0) {
585                 if (copy_in_user(ss32, ss, offsetof(SS32, iomem_base)) ||
586                     get_user(iomem_base, &ss->iomem_base))
587                         return -EFAULT;
588                 base = (unsigned long)iomem_base  >> 32 ?
589                         0xffffffff : (unsigned)(unsigned long)iomem_base;
590                 if (put_user(base, &ss32->iomem_base) ||
591                     convert_in_user(&ss->iomem_reg_shift,
592                       &ss32->iomem_reg_shift) ||
593                     convert_in_user(&ss->port_high, &ss32->port_high))
594                         return -EFAULT;
595         }
596         return err;
597 }
598
599 #define RTC_IRQP_READ32         _IOR('p', 0x0b, compat_ulong_t)
600 #define RTC_IRQP_SET32          _IOW('p', 0x0c, compat_ulong_t)
601 #define RTC_EPOCH_READ32        _IOR('p', 0x0d, compat_ulong_t)
602 #define RTC_EPOCH_SET32         _IOW('p', 0x0e, compat_ulong_t)
603
604 static int rtc_ioctl(struct file *file,
605                 unsigned cmd, void __user *argp)
606 {
607         unsigned long __user *valp = compat_alloc_user_space(sizeof(*valp));
608         int ret;
609
610         if (valp == NULL)
611                 return -EFAULT;
612         switch (cmd) {
613         case RTC_IRQP_READ32:
614         case RTC_EPOCH_READ32:
615                 ret = do_ioctl(file, (cmd == RTC_IRQP_READ32) ?
616                                         RTC_IRQP_READ : RTC_EPOCH_READ,
617                                         (unsigned long)valp);
618                 if (ret)
619                         return ret;
620                 return convert_in_user(valp, (unsigned int __user *)argp);
621         case RTC_IRQP_SET32:
622                 return do_ioctl(file, RTC_IRQP_SET, (unsigned long)argp);
623         case RTC_EPOCH_SET32:
624                 return do_ioctl(file, RTC_EPOCH_SET, (unsigned long)argp);
625         }
626
627         return -ENOIOCTLCMD;
628 }
629
630 /* on ia32 l_start is on a 32-bit boundary */
631 #if defined(CONFIG_IA64) || defined(CONFIG_X86_64)
632 struct space_resv_32 {
633         __s16           l_type;
634         __s16           l_whence;
635         __s64           l_start __attribute__((packed));
636                         /* len == 0 means until end of file */
637         __s64           l_len __attribute__((packed));
638         __s32           l_sysid;
639         __u32           l_pid;
640         __s32           l_pad[4];       /* reserve area */
641 };
642
643 #define FS_IOC_RESVSP_32                _IOW ('X', 40, struct space_resv_32)
644 #define FS_IOC_RESVSP64_32      _IOW ('X', 42, struct space_resv_32)
645
646 /* just account for different alignment */
647 static int compat_ioctl_preallocate(struct file *file,
648                         struct space_resv_32    __user *p32)
649 {
650         struct space_resv       __user *p = compat_alloc_user_space(sizeof(*p));
651
652         if (copy_in_user(&p->l_type,    &p32->l_type,   sizeof(s16)) ||
653             copy_in_user(&p->l_whence,  &p32->l_whence, sizeof(s16)) ||
654             copy_in_user(&p->l_start,   &p32->l_start,  sizeof(s64)) ||
655             copy_in_user(&p->l_len,     &p32->l_len,    sizeof(s64)) ||
656             copy_in_user(&p->l_sysid,   &p32->l_sysid,  sizeof(s32)) ||
657             copy_in_user(&p->l_pid,     &p32->l_pid,    sizeof(u32)) ||
658             copy_in_user(&p->l_pad,     &p32->l_pad,    4*sizeof(u32)))
659                 return -EFAULT;
660
661         return ioctl_preallocate(file, p);
662 }
663 #endif
664
665 /*
666  * simple reversible transform to make our table more evenly
667  * distributed after sorting.
668  */
669 #define XFORM(i) (((i) ^ ((i) << 27) ^ ((i) << 17)) & 0xffffffff)
670
671 #define COMPATIBLE_IOCTL(cmd) XFORM((u32)cmd),
672 /* ioctl should not be warned about even if it's not implemented.
673    Valid reasons to use this:
674    - It is implemented with ->compat_ioctl on some device, but programs
675    call it on others too.
676    - The ioctl is not implemented in the native kernel, but programs
677    call it commonly anyways.
678    Most other reasons are not valid. */
679 #define IGNORE_IOCTL(cmd) COMPATIBLE_IOCTL(cmd)
680
681 static unsigned int ioctl_pointer[] = {
682 /* compatible ioctls first */
683 COMPATIBLE_IOCTL(0x4B50)   /* KDGHWCLK - not in the kernel, but don't complain */
684 COMPATIBLE_IOCTL(0x4B51)   /* KDSHWCLK - not in the kernel, but don't complain */
685
686 /* Big T */
687 COMPATIBLE_IOCTL(TCGETA)
688 COMPATIBLE_IOCTL(TCSETA)
689 COMPATIBLE_IOCTL(TCSETAW)
690 COMPATIBLE_IOCTL(TCSETAF)
691 COMPATIBLE_IOCTL(TCSBRK)
692 COMPATIBLE_IOCTL(TCXONC)
693 COMPATIBLE_IOCTL(TCFLSH)
694 COMPATIBLE_IOCTL(TCGETS)
695 COMPATIBLE_IOCTL(TCSETS)
696 COMPATIBLE_IOCTL(TCSETSW)
697 COMPATIBLE_IOCTL(TCSETSF)
698 COMPATIBLE_IOCTL(TIOCLINUX)
699 COMPATIBLE_IOCTL(TIOCSBRK)
700 COMPATIBLE_IOCTL(TIOCGDEV)
701 COMPATIBLE_IOCTL(TIOCCBRK)
702 COMPATIBLE_IOCTL(TIOCGSID)
703 COMPATIBLE_IOCTL(TIOCGICOUNT)
704 COMPATIBLE_IOCTL(TIOCGEXCL)
705 /* Little t */
706 COMPATIBLE_IOCTL(TIOCGETD)
707 COMPATIBLE_IOCTL(TIOCSETD)
708 COMPATIBLE_IOCTL(TIOCEXCL)
709 COMPATIBLE_IOCTL(TIOCNXCL)
710 COMPATIBLE_IOCTL(TIOCCONS)
711 COMPATIBLE_IOCTL(TIOCGSOFTCAR)
712 COMPATIBLE_IOCTL(TIOCSSOFTCAR)
713 COMPATIBLE_IOCTL(TIOCSWINSZ)
714 COMPATIBLE_IOCTL(TIOCGWINSZ)
715 COMPATIBLE_IOCTL(TIOCMGET)
716 COMPATIBLE_IOCTL(TIOCMBIC)
717 COMPATIBLE_IOCTL(TIOCMBIS)
718 COMPATIBLE_IOCTL(TIOCMSET)
719 COMPATIBLE_IOCTL(TIOCNOTTY)
720 COMPATIBLE_IOCTL(TIOCSTI)
721 COMPATIBLE_IOCTL(TIOCOUTQ)
722 COMPATIBLE_IOCTL(TIOCSPGRP)
723 COMPATIBLE_IOCTL(TIOCGPGRP)
724 COMPATIBLE_IOCTL(TIOCSERGETLSR)
725 #ifdef TIOCSRS485
726 COMPATIBLE_IOCTL(TIOCSRS485)
727 #endif
728 #ifdef TIOCGRS485
729 COMPATIBLE_IOCTL(TIOCGRS485)
730 #endif
731 #ifdef TCGETS2
732 COMPATIBLE_IOCTL(TCGETS2)
733 COMPATIBLE_IOCTL(TCSETS2)
734 COMPATIBLE_IOCTL(TCSETSW2)
735 COMPATIBLE_IOCTL(TCSETSF2)
736 #endif
737 /* Little f */
738 COMPATIBLE_IOCTL(FIOCLEX)
739 COMPATIBLE_IOCTL(FIONCLEX)
740 COMPATIBLE_IOCTL(FIOASYNC)
741 COMPATIBLE_IOCTL(FIONBIO)
742 COMPATIBLE_IOCTL(FIONREAD)  /* This is also TIOCINQ */
743 COMPATIBLE_IOCTL(FS_IOC_FIEMAP)
744 /* 0x00 */
745 COMPATIBLE_IOCTL(FIBMAP)
746 COMPATIBLE_IOCTL(FIGETBSZ)
747 /* 'X' - originally XFS but some now in the VFS */
748 COMPATIBLE_IOCTL(FIFREEZE)
749 COMPATIBLE_IOCTL(FITHAW)
750 COMPATIBLE_IOCTL(FITRIM)
751 COMPATIBLE_IOCTL(KDGETKEYCODE)
752 COMPATIBLE_IOCTL(KDSETKEYCODE)
753 COMPATIBLE_IOCTL(KDGKBTYPE)
754 COMPATIBLE_IOCTL(KDGETMODE)
755 COMPATIBLE_IOCTL(KDGKBMODE)
756 COMPATIBLE_IOCTL(KDGKBMETA)
757 COMPATIBLE_IOCTL(KDGKBENT)
758 COMPATIBLE_IOCTL(KDSKBENT)
759 COMPATIBLE_IOCTL(KDGKBSENT)
760 COMPATIBLE_IOCTL(KDSKBSENT)
761 COMPATIBLE_IOCTL(KDGKBDIACR)
762 COMPATIBLE_IOCTL(KDSKBDIACR)
763 COMPATIBLE_IOCTL(KDGKBDIACRUC)
764 COMPATIBLE_IOCTL(KDSKBDIACRUC)
765 COMPATIBLE_IOCTL(KDKBDREP)
766 COMPATIBLE_IOCTL(KDGKBLED)
767 COMPATIBLE_IOCTL(KDGETLED)
768 #ifdef CONFIG_BLOCK
769 /* Big S */
770 COMPATIBLE_IOCTL(SCSI_IOCTL_GET_IDLUN)
771 COMPATIBLE_IOCTL(SCSI_IOCTL_DOORLOCK)
772 COMPATIBLE_IOCTL(SCSI_IOCTL_DOORUNLOCK)
773 COMPATIBLE_IOCTL(SCSI_IOCTL_TEST_UNIT_READY)
774 COMPATIBLE_IOCTL(SCSI_IOCTL_GET_BUS_NUMBER)
775 COMPATIBLE_IOCTL(SCSI_IOCTL_SEND_COMMAND)
776 COMPATIBLE_IOCTL(SCSI_IOCTL_PROBE_HOST)
777 COMPATIBLE_IOCTL(SCSI_IOCTL_GET_PCI)
778 #endif
779 /* Big V (don't complain on serial console) */
780 IGNORE_IOCTL(VT_OPENQRY)
781 IGNORE_IOCTL(VT_GETMODE)
782 /* Little p (/dev/rtc, /dev/envctrl, etc.) */
783 COMPATIBLE_IOCTL(RTC_AIE_ON)
784 COMPATIBLE_IOCTL(RTC_AIE_OFF)
785 COMPATIBLE_IOCTL(RTC_UIE_ON)
786 COMPATIBLE_IOCTL(RTC_UIE_OFF)
787 COMPATIBLE_IOCTL(RTC_PIE_ON)
788 COMPATIBLE_IOCTL(RTC_PIE_OFF)
789 COMPATIBLE_IOCTL(RTC_WIE_ON)
790 COMPATIBLE_IOCTL(RTC_WIE_OFF)
791 COMPATIBLE_IOCTL(RTC_ALM_SET)
792 COMPATIBLE_IOCTL(RTC_ALM_READ)
793 COMPATIBLE_IOCTL(RTC_RD_TIME)
794 COMPATIBLE_IOCTL(RTC_SET_TIME)
795 COMPATIBLE_IOCTL(RTC_WKALM_SET)
796 COMPATIBLE_IOCTL(RTC_WKALM_RD)
797 /*
798  * These two are only for the sbus rtc driver, but
799  * hwclock tries them on every rtc device first when
800  * running on sparc.  On other architectures the entries
801  * are useless but harmless.
802  */
803 COMPATIBLE_IOCTL(_IOR('p', 20, int[7])) /* RTCGET */
804 COMPATIBLE_IOCTL(_IOW('p', 21, int[7])) /* RTCSET */
805 /* Little m */
806 COMPATIBLE_IOCTL(MTIOCTOP)
807 /* Socket level stuff */
808 COMPATIBLE_IOCTL(FIOQSIZE)
809 #ifdef CONFIG_BLOCK
810 /* md calls this on random blockdevs */
811 IGNORE_IOCTL(RAID_VERSION)
812 /* qemu/qemu-img might call these two on plain files for probing */
813 IGNORE_IOCTL(CDROM_DRIVE_STATUS)
814 IGNORE_IOCTL(FDGETPRM32)
815 /* SG stuff */
816 COMPATIBLE_IOCTL(SG_SET_TIMEOUT)
817 COMPATIBLE_IOCTL(SG_GET_TIMEOUT)
818 COMPATIBLE_IOCTL(SG_EMULATED_HOST)
819 COMPATIBLE_IOCTL(SG_GET_TRANSFORM)
820 COMPATIBLE_IOCTL(SG_SET_RESERVED_SIZE)
821 COMPATIBLE_IOCTL(SG_GET_RESERVED_SIZE)
822 COMPATIBLE_IOCTL(SG_GET_SCSI_ID)
823 COMPATIBLE_IOCTL(SG_SET_FORCE_LOW_DMA)
824 COMPATIBLE_IOCTL(SG_GET_LOW_DMA)
825 COMPATIBLE_IOCTL(SG_SET_FORCE_PACK_ID)
826 COMPATIBLE_IOCTL(SG_GET_PACK_ID)
827 COMPATIBLE_IOCTL(SG_GET_NUM_WAITING)
828 COMPATIBLE_IOCTL(SG_SET_DEBUG)
829 COMPATIBLE_IOCTL(SG_GET_SG_TABLESIZE)
830 COMPATIBLE_IOCTL(SG_GET_COMMAND_Q)
831 COMPATIBLE_IOCTL(SG_SET_COMMAND_Q)
832 COMPATIBLE_IOCTL(SG_GET_VERSION_NUM)
833 COMPATIBLE_IOCTL(SG_NEXT_CMD_LEN)
834 COMPATIBLE_IOCTL(SG_SCSI_RESET)
835 COMPATIBLE_IOCTL(SG_GET_REQUEST_TABLE)
836 COMPATIBLE_IOCTL(SG_SET_KEEP_ORPHAN)
837 COMPATIBLE_IOCTL(SG_GET_KEEP_ORPHAN)
838 #endif
839 /* PPP stuff */
840 COMPATIBLE_IOCTL(PPPIOCGFLAGS)
841 COMPATIBLE_IOCTL(PPPIOCSFLAGS)
842 COMPATIBLE_IOCTL(PPPIOCGASYNCMAP)
843 COMPATIBLE_IOCTL(PPPIOCSASYNCMAP)
844 COMPATIBLE_IOCTL(PPPIOCGUNIT)
845 COMPATIBLE_IOCTL(PPPIOCGRASYNCMAP)
846 COMPATIBLE_IOCTL(PPPIOCSRASYNCMAP)
847 COMPATIBLE_IOCTL(PPPIOCGMRU)
848 COMPATIBLE_IOCTL(PPPIOCSMRU)
849 COMPATIBLE_IOCTL(PPPIOCSMAXCID)
850 COMPATIBLE_IOCTL(PPPIOCGXASYNCMAP)
851 COMPATIBLE_IOCTL(PPPIOCSXASYNCMAP)
852 COMPATIBLE_IOCTL(PPPIOCXFERUNIT)
853 /* PPPIOCSCOMPRESS is translated */
854 COMPATIBLE_IOCTL(PPPIOCGNPMODE)
855 COMPATIBLE_IOCTL(PPPIOCSNPMODE)
856 COMPATIBLE_IOCTL(PPPIOCGDEBUG)
857 COMPATIBLE_IOCTL(PPPIOCSDEBUG)
858 /* PPPIOCSPASS is translated */
859 /* PPPIOCSACTIVE is translated */
860 /* PPPIOCGIDLE is translated */
861 COMPATIBLE_IOCTL(PPPIOCNEWUNIT)
862 COMPATIBLE_IOCTL(PPPIOCATTACH)
863 COMPATIBLE_IOCTL(PPPIOCDETACH)
864 COMPATIBLE_IOCTL(PPPIOCSMRRU)
865 COMPATIBLE_IOCTL(PPPIOCCONNECT)
866 COMPATIBLE_IOCTL(PPPIOCDISCONN)
867 COMPATIBLE_IOCTL(PPPIOCATTCHAN)
868 COMPATIBLE_IOCTL(PPPIOCGCHAN)
869 COMPATIBLE_IOCTL(PPPIOCGL2TPSTATS)
870 /* PPPOX */
871 COMPATIBLE_IOCTL(PPPOEIOCSFWD)
872 COMPATIBLE_IOCTL(PPPOEIOCDFWD)
873 /* Big A */
874 /* sparc only */
875 /* Big Q for sound/OSS */
876 COMPATIBLE_IOCTL(SNDCTL_SEQ_RESET)
877 COMPATIBLE_IOCTL(SNDCTL_SEQ_SYNC)
878 COMPATIBLE_IOCTL(SNDCTL_SYNTH_INFO)
879 COMPATIBLE_IOCTL(SNDCTL_SEQ_CTRLRATE)
880 COMPATIBLE_IOCTL(SNDCTL_SEQ_GETOUTCOUNT)
881 COMPATIBLE_IOCTL(SNDCTL_SEQ_GETINCOUNT)
882 COMPATIBLE_IOCTL(SNDCTL_SEQ_PERCMODE)
883 COMPATIBLE_IOCTL(SNDCTL_FM_LOAD_INSTR)
884 COMPATIBLE_IOCTL(SNDCTL_SEQ_TESTMIDI)
885 COMPATIBLE_IOCTL(SNDCTL_SEQ_RESETSAMPLES)
886 COMPATIBLE_IOCTL(SNDCTL_SEQ_NRSYNTHS)
887 COMPATIBLE_IOCTL(SNDCTL_SEQ_NRMIDIS)
888 COMPATIBLE_IOCTL(SNDCTL_MIDI_INFO)
889 COMPATIBLE_IOCTL(SNDCTL_SEQ_THRESHOLD)
890 COMPATIBLE_IOCTL(SNDCTL_SYNTH_MEMAVL)
891 COMPATIBLE_IOCTL(SNDCTL_FM_4OP_ENABLE)
892 COMPATIBLE_IOCTL(SNDCTL_SEQ_PANIC)
893 COMPATIBLE_IOCTL(SNDCTL_SEQ_OUTOFBAND)
894 COMPATIBLE_IOCTL(SNDCTL_SEQ_GETTIME)
895 COMPATIBLE_IOCTL(SNDCTL_SYNTH_ID)
896 COMPATIBLE_IOCTL(SNDCTL_SYNTH_CONTROL)
897 COMPATIBLE_IOCTL(SNDCTL_SYNTH_REMOVESAMPLE)
898 /* Big T for sound/OSS */
899 COMPATIBLE_IOCTL(SNDCTL_TMR_TIMEBASE)
900 COMPATIBLE_IOCTL(SNDCTL_TMR_START)
901 COMPATIBLE_IOCTL(SNDCTL_TMR_STOP)
902 COMPATIBLE_IOCTL(SNDCTL_TMR_CONTINUE)
903 COMPATIBLE_IOCTL(SNDCTL_TMR_TEMPO)
904 COMPATIBLE_IOCTL(SNDCTL_TMR_SOURCE)
905 COMPATIBLE_IOCTL(SNDCTL_TMR_METRONOME)
906 COMPATIBLE_IOCTL(SNDCTL_TMR_SELECT)
907 /* Little m for sound/OSS */
908 COMPATIBLE_IOCTL(SNDCTL_MIDI_PRETIME)
909 COMPATIBLE_IOCTL(SNDCTL_MIDI_MPUMODE)
910 COMPATIBLE_IOCTL(SNDCTL_MIDI_MPUCMD)
911 /* Big P for sound/OSS */
912 COMPATIBLE_IOCTL(SNDCTL_DSP_RESET)
913 COMPATIBLE_IOCTL(SNDCTL_DSP_SYNC)
914 COMPATIBLE_IOCTL(SNDCTL_DSP_SPEED)
915 COMPATIBLE_IOCTL(SNDCTL_DSP_STEREO)
916 COMPATIBLE_IOCTL(SNDCTL_DSP_GETBLKSIZE)
917 COMPATIBLE_IOCTL(SNDCTL_DSP_CHANNELS)
918 COMPATIBLE_IOCTL(SOUND_PCM_WRITE_FILTER)
919 COMPATIBLE_IOCTL(SNDCTL_DSP_POST)
920 COMPATIBLE_IOCTL(SNDCTL_DSP_SUBDIVIDE)
921 COMPATIBLE_IOCTL(SNDCTL_DSP_SETFRAGMENT)
922 COMPATIBLE_IOCTL(SNDCTL_DSP_GETFMTS)
923 COMPATIBLE_IOCTL(SNDCTL_DSP_SETFMT)
924 COMPATIBLE_IOCTL(SNDCTL_DSP_GETOSPACE)
925 COMPATIBLE_IOCTL(SNDCTL_DSP_GETISPACE)
926 COMPATIBLE_IOCTL(SNDCTL_DSP_NONBLOCK)
927 COMPATIBLE_IOCTL(SNDCTL_DSP_GETCAPS)
928 COMPATIBLE_IOCTL(SNDCTL_DSP_GETTRIGGER)
929 COMPATIBLE_IOCTL(SNDCTL_DSP_SETTRIGGER)
930 COMPATIBLE_IOCTL(SNDCTL_DSP_GETIPTR)
931 COMPATIBLE_IOCTL(SNDCTL_DSP_GETOPTR)
932 /* SNDCTL_DSP_MAPINBUF,  XXX needs translation */
933 /* SNDCTL_DSP_MAPOUTBUF,  XXX needs translation */
934 COMPATIBLE_IOCTL(SNDCTL_DSP_SETSYNCRO)
935 COMPATIBLE_IOCTL(SNDCTL_DSP_SETDUPLEX)
936 COMPATIBLE_IOCTL(SNDCTL_DSP_GETODELAY)
937 COMPATIBLE_IOCTL(SNDCTL_DSP_PROFILE)
938 COMPATIBLE_IOCTL(SOUND_PCM_READ_RATE)
939 COMPATIBLE_IOCTL(SOUND_PCM_READ_CHANNELS)
940 COMPATIBLE_IOCTL(SOUND_PCM_READ_BITS)
941 COMPATIBLE_IOCTL(SOUND_PCM_READ_FILTER)
942 /* Big C for sound/OSS */
943 COMPATIBLE_IOCTL(SNDCTL_COPR_RESET)
944 COMPATIBLE_IOCTL(SNDCTL_COPR_LOAD)
945 COMPATIBLE_IOCTL(SNDCTL_COPR_RDATA)
946 COMPATIBLE_IOCTL(SNDCTL_COPR_RCODE)
947 COMPATIBLE_IOCTL(SNDCTL_COPR_WDATA)
948 COMPATIBLE_IOCTL(SNDCTL_COPR_WCODE)
949 COMPATIBLE_IOCTL(SNDCTL_COPR_RUN)
950 COMPATIBLE_IOCTL(SNDCTL_COPR_HALT)
951 COMPATIBLE_IOCTL(SNDCTL_COPR_SENDMSG)
952 COMPATIBLE_IOCTL(SNDCTL_COPR_RCVMSG)
953 /* Big M for sound/OSS */
954 COMPATIBLE_IOCTL(SOUND_MIXER_READ_VOLUME)
955 COMPATIBLE_IOCTL(SOUND_MIXER_READ_BASS)
956 COMPATIBLE_IOCTL(SOUND_MIXER_READ_TREBLE)
957 COMPATIBLE_IOCTL(SOUND_MIXER_READ_SYNTH)
958 COMPATIBLE_IOCTL(SOUND_MIXER_READ_PCM)
959 COMPATIBLE_IOCTL(SOUND_MIXER_READ_SPEAKER)
960 COMPATIBLE_IOCTL(SOUND_MIXER_READ_LINE)
961 COMPATIBLE_IOCTL(SOUND_MIXER_READ_MIC)
962 COMPATIBLE_IOCTL(SOUND_MIXER_READ_CD)
963 COMPATIBLE_IOCTL(SOUND_MIXER_READ_IMIX)
964 COMPATIBLE_IOCTL(SOUND_MIXER_READ_ALTPCM)
965 COMPATIBLE_IOCTL(SOUND_MIXER_READ_RECLEV)
966 COMPATIBLE_IOCTL(SOUND_MIXER_READ_IGAIN)
967 COMPATIBLE_IOCTL(SOUND_MIXER_READ_OGAIN)
968 COMPATIBLE_IOCTL(SOUND_MIXER_READ_LINE1)
969 COMPATIBLE_IOCTL(SOUND_MIXER_READ_LINE2)
970 COMPATIBLE_IOCTL(SOUND_MIXER_READ_LINE3)
971 COMPATIBLE_IOCTL(MIXER_READ(SOUND_MIXER_DIGITAL1))
972 COMPATIBLE_IOCTL(MIXER_READ(SOUND_MIXER_DIGITAL2))
973 COMPATIBLE_IOCTL(MIXER_READ(SOUND_MIXER_DIGITAL3))
974 COMPATIBLE_IOCTL(MIXER_READ(SOUND_MIXER_PHONEIN))
975 COMPATIBLE_IOCTL(MIXER_READ(SOUND_MIXER_PHONEOUT))
976 COMPATIBLE_IOCTL(MIXER_READ(SOUND_MIXER_VIDEO))
977 COMPATIBLE_IOCTL(MIXER_READ(SOUND_MIXER_RADIO))
978 COMPATIBLE_IOCTL(MIXER_READ(SOUND_MIXER_MONITOR))
979 COMPATIBLE_IOCTL(SOUND_MIXER_READ_MUTE)
980 /* SOUND_MIXER_READ_ENHANCE,  same value as READ_MUTE */
981 /* SOUND_MIXER_READ_LOUD,  same value as READ_MUTE */
982 COMPATIBLE_IOCTL(SOUND_MIXER_READ_RECSRC)
983 COMPATIBLE_IOCTL(SOUND_MIXER_READ_DEVMASK)
984 COMPATIBLE_IOCTL(SOUND_MIXER_READ_RECMASK)
985 COMPATIBLE_IOCTL(SOUND_MIXER_READ_STEREODEVS)
986 COMPATIBLE_IOCTL(SOUND_MIXER_READ_CAPS)
987 COMPATIBLE_IOCTL(SOUND_MIXER_WRITE_VOLUME)
988 COMPATIBLE_IOCTL(SOUND_MIXER_WRITE_BASS)
989 COMPATIBLE_IOCTL(SOUND_MIXER_WRITE_TREBLE)
990 COMPATIBLE_IOCTL(SOUND_MIXER_WRITE_SYNTH)
991 COMPATIBLE_IOCTL(SOUND_MIXER_WRITE_PCM)
992 COMPATIBLE_IOCTL(SOUND_MIXER_WRITE_SPEAKER)
993 COMPATIBLE_IOCTL(SOUND_MIXER_WRITE_LINE)
994 COMPATIBLE_IOCTL(SOUND_MIXER_WRITE_MIC)
995 COMPATIBLE_IOCTL(SOUND_MIXER_WRITE_CD)
996 COMPATIBLE_IOCTL(SOUND_MIXER_WRITE_IMIX)
997 COMPATIBLE_IOCTL(SOUND_MIXER_WRITE_ALTPCM)
998 COMPATIBLE_IOCTL(SOUND_MIXER_WRITE_RECLEV)
999 COMPATIBLE_IOCTL(SOUND_MIXER_WRITE_IGAIN)
1000 COMPATIBLE_IOCTL(SOUND_MIXER_WRITE_OGAIN)
1001 COMPATIBLE_IOCTL(SOUND_MIXER_WRITE_LINE1)
1002 COMPATIBLE_IOCTL(SOUND_MIXER_WRITE_LINE2)
1003 COMPATIBLE_IOCTL(SOUND_MIXER_WRITE_LINE3)
1004 COMPATIBLE_IOCTL(MIXER_WRITE(SOUND_MIXER_DIGITAL1))
1005 COMPATIBLE_IOCTL(MIXER_WRITE(SOUND_MIXER_DIGITAL2))
1006 COMPATIBLE_IOCTL(MIXER_WRITE(SOUND_MIXER_DIGITAL3))
1007 COMPATIBLE_IOCTL(MIXER_WRITE(SOUND_MIXER_PHONEIN))
1008 COMPATIBLE_IOCTL(MIXER_WRITE(SOUND_MIXER_PHONEOUT))
1009 COMPATIBLE_IOCTL(MIXER_WRITE(SOUND_MIXER_VIDEO))
1010 COMPATIBLE_IOCTL(MIXER_WRITE(SOUND_MIXER_RADIO))
1011 COMPATIBLE_IOCTL(MIXER_WRITE(SOUND_MIXER_MONITOR))
1012 COMPATIBLE_IOCTL(SOUND_MIXER_WRITE_MUTE)
1013 /* SOUND_MIXER_WRITE_ENHANCE,  same value as WRITE_MUTE */
1014 /* SOUND_MIXER_WRITE_LOUD,  same value as WRITE_MUTE */
1015 COMPATIBLE_IOCTL(SOUND_MIXER_WRITE_RECSRC)
1016 COMPATIBLE_IOCTL(SOUND_MIXER_INFO)
1017 COMPATIBLE_IOCTL(SOUND_OLD_MIXER_INFO)
1018 COMPATIBLE_IOCTL(SOUND_MIXER_ACCESS)
1019 COMPATIBLE_IOCTL(SOUND_MIXER_AGC)
1020 COMPATIBLE_IOCTL(SOUND_MIXER_3DSE)
1021 COMPATIBLE_IOCTL(SOUND_MIXER_PRIVATE1)
1022 COMPATIBLE_IOCTL(SOUND_MIXER_PRIVATE2)
1023 COMPATIBLE_IOCTL(SOUND_MIXER_PRIVATE3)
1024 COMPATIBLE_IOCTL(SOUND_MIXER_PRIVATE4)
1025 COMPATIBLE_IOCTL(SOUND_MIXER_PRIVATE5)
1026 COMPATIBLE_IOCTL(SOUND_MIXER_GETLEVELS)
1027 COMPATIBLE_IOCTL(SOUND_MIXER_SETLEVELS)
1028 COMPATIBLE_IOCTL(OSS_GETVERSION)
1029 /* Raw devices */
1030 COMPATIBLE_IOCTL(RAW_SETBIND)
1031 COMPATIBLE_IOCTL(RAW_GETBIND)
1032 /* Watchdog */
1033 COMPATIBLE_IOCTL(WDIOC_GETSUPPORT)
1034 COMPATIBLE_IOCTL(WDIOC_GETSTATUS)
1035 COMPATIBLE_IOCTL(WDIOC_GETBOOTSTATUS)
1036 COMPATIBLE_IOCTL(WDIOC_GETTEMP)
1037 COMPATIBLE_IOCTL(WDIOC_SETOPTIONS)
1038 COMPATIBLE_IOCTL(WDIOC_KEEPALIVE)
1039 COMPATIBLE_IOCTL(WDIOC_SETTIMEOUT)
1040 COMPATIBLE_IOCTL(WDIOC_GETTIMEOUT)
1041 COMPATIBLE_IOCTL(WDIOC_SETPRETIMEOUT)
1042 COMPATIBLE_IOCTL(WDIOC_GETPRETIMEOUT)
1043 /* Big R */
1044 COMPATIBLE_IOCTL(RNDGETENTCNT)
1045 COMPATIBLE_IOCTL(RNDADDTOENTCNT)
1046 COMPATIBLE_IOCTL(RNDGETPOOL)
1047 COMPATIBLE_IOCTL(RNDADDENTROPY)
1048 COMPATIBLE_IOCTL(RNDZAPENTCNT)
1049 COMPATIBLE_IOCTL(RNDCLEARPOOL)
1050 /* Bluetooth */
1051 COMPATIBLE_IOCTL(HCIDEVUP)
1052 COMPATIBLE_IOCTL(HCIDEVDOWN)
1053 COMPATIBLE_IOCTL(HCIDEVRESET)
1054 COMPATIBLE_IOCTL(HCIDEVRESTAT)
1055 COMPATIBLE_IOCTL(HCIGETDEVLIST)
1056 COMPATIBLE_IOCTL(HCIGETDEVINFO)
1057 COMPATIBLE_IOCTL(HCIGETCONNLIST)
1058 COMPATIBLE_IOCTL(HCIGETCONNINFO)
1059 COMPATIBLE_IOCTL(HCIGETAUTHINFO)
1060 COMPATIBLE_IOCTL(HCISETRAW)
1061 COMPATIBLE_IOCTL(HCISETSCAN)
1062 COMPATIBLE_IOCTL(HCISETAUTH)
1063 COMPATIBLE_IOCTL(HCISETENCRYPT)
1064 COMPATIBLE_IOCTL(HCISETPTYPE)
1065 COMPATIBLE_IOCTL(HCISETLINKPOL)
1066 COMPATIBLE_IOCTL(HCISETLINKMODE)
1067 COMPATIBLE_IOCTL(HCISETACLMTU)
1068 COMPATIBLE_IOCTL(HCISETSCOMTU)
1069 COMPATIBLE_IOCTL(HCIBLOCKADDR)
1070 COMPATIBLE_IOCTL(HCIUNBLOCKADDR)
1071 COMPATIBLE_IOCTL(HCIINQUIRY)
1072 COMPATIBLE_IOCTL(HCIUARTSETPROTO)
1073 COMPATIBLE_IOCTL(HCIUARTGETPROTO)
1074 COMPATIBLE_IOCTL(HCIUARTGETDEVICE)
1075 COMPATIBLE_IOCTL(HCIUARTSETFLAGS)
1076 COMPATIBLE_IOCTL(HCIUARTGETFLAGS)
1077 COMPATIBLE_IOCTL(RFCOMMCREATEDEV)
1078 COMPATIBLE_IOCTL(RFCOMMRELEASEDEV)
1079 COMPATIBLE_IOCTL(RFCOMMGETDEVLIST)
1080 COMPATIBLE_IOCTL(RFCOMMGETDEVINFO)
1081 COMPATIBLE_IOCTL(RFCOMMSTEALDLC)
1082 /* CAPI */
1083 COMPATIBLE_IOCTL(CAPI_REGISTER)
1084 COMPATIBLE_IOCTL(CAPI_GET_MANUFACTURER)
1085 COMPATIBLE_IOCTL(CAPI_GET_VERSION)
1086 COMPATIBLE_IOCTL(CAPI_GET_SERIAL)
1087 COMPATIBLE_IOCTL(CAPI_GET_PROFILE)
1088 COMPATIBLE_IOCTL(CAPI_MANUFACTURER_CMD)
1089 COMPATIBLE_IOCTL(CAPI_GET_ERRCODE)
1090 COMPATIBLE_IOCTL(CAPI_INSTALLED)
1091 COMPATIBLE_IOCTL(CAPI_GET_FLAGS)
1092 COMPATIBLE_IOCTL(CAPI_SET_FLAGS)
1093 COMPATIBLE_IOCTL(CAPI_CLR_FLAGS)
1094 COMPATIBLE_IOCTL(CAPI_NCCI_OPENCOUNT)
1095 COMPATIBLE_IOCTL(CAPI_NCCI_GETUNIT)
1096 /* Siemens Gigaset */
1097 COMPATIBLE_IOCTL(GIGASET_REDIR)
1098 COMPATIBLE_IOCTL(GIGASET_CONFIG)
1099 COMPATIBLE_IOCTL(GIGASET_BRKCHARS)
1100 COMPATIBLE_IOCTL(GIGASET_VERSION)
1101 /* Misc. */
1102 COMPATIBLE_IOCTL(0x41545900)            /* ATYIO_CLKR */
1103 COMPATIBLE_IOCTL(0x41545901)            /* ATYIO_CLKW */
1104 COMPATIBLE_IOCTL(PCIIOC_CONTROLLER)
1105 COMPATIBLE_IOCTL(PCIIOC_MMAP_IS_IO)
1106 COMPATIBLE_IOCTL(PCIIOC_MMAP_IS_MEM)
1107 COMPATIBLE_IOCTL(PCIIOC_WRITE_COMBINE)
1108 /* hiddev */
1109 COMPATIBLE_IOCTL(HIDIOCGVERSION)
1110 COMPATIBLE_IOCTL(HIDIOCAPPLICATION)
1111 COMPATIBLE_IOCTL(HIDIOCGDEVINFO)
1112 COMPATIBLE_IOCTL(HIDIOCGSTRING)
1113 COMPATIBLE_IOCTL(HIDIOCINITREPORT)
1114 COMPATIBLE_IOCTL(HIDIOCGREPORT)
1115 COMPATIBLE_IOCTL(HIDIOCSREPORT)
1116 COMPATIBLE_IOCTL(HIDIOCGREPORTINFO)
1117 COMPATIBLE_IOCTL(HIDIOCGFIELDINFO)
1118 COMPATIBLE_IOCTL(HIDIOCGUSAGE)
1119 COMPATIBLE_IOCTL(HIDIOCSUSAGE)
1120 COMPATIBLE_IOCTL(HIDIOCGUCODE)
1121 COMPATIBLE_IOCTL(HIDIOCGFLAG)
1122 COMPATIBLE_IOCTL(HIDIOCSFLAG)
1123 COMPATIBLE_IOCTL(HIDIOCGCOLLECTIONINDEX)
1124 COMPATIBLE_IOCTL(HIDIOCGCOLLECTIONINFO)
1125 /* dvb */
1126 COMPATIBLE_IOCTL(AUDIO_STOP)
1127 COMPATIBLE_IOCTL(AUDIO_PLAY)
1128 COMPATIBLE_IOCTL(AUDIO_PAUSE)
1129 COMPATIBLE_IOCTL(AUDIO_CONTINUE)
1130 COMPATIBLE_IOCTL(AUDIO_SELECT_SOURCE)
1131 COMPATIBLE_IOCTL(AUDIO_SET_MUTE)
1132 COMPATIBLE_IOCTL(AUDIO_SET_AV_SYNC)
1133 COMPATIBLE_IOCTL(AUDIO_SET_BYPASS_MODE)
1134 COMPATIBLE_IOCTL(AUDIO_CHANNEL_SELECT)
1135 COMPATIBLE_IOCTL(AUDIO_GET_STATUS)
1136 COMPATIBLE_IOCTL(AUDIO_GET_CAPABILITIES)
1137 COMPATIBLE_IOCTL(AUDIO_CLEAR_BUFFER)
1138 COMPATIBLE_IOCTL(AUDIO_SET_ID)
1139 COMPATIBLE_IOCTL(AUDIO_SET_MIXER)
1140 COMPATIBLE_IOCTL(AUDIO_SET_STREAMTYPE)
1141 COMPATIBLE_IOCTL(AUDIO_SET_EXT_ID)
1142 COMPATIBLE_IOCTL(AUDIO_SET_ATTRIBUTES)
1143 COMPATIBLE_IOCTL(AUDIO_SET_KARAOKE)
1144 COMPATIBLE_IOCTL(DMX_START)
1145 COMPATIBLE_IOCTL(DMX_STOP)
1146 COMPATIBLE_IOCTL(DMX_SET_FILTER)
1147 COMPATIBLE_IOCTL(DMX_SET_PES_FILTER)
1148 COMPATIBLE_IOCTL(DMX_SET_BUFFER_SIZE)
1149 COMPATIBLE_IOCTL(DMX_GET_PES_PIDS)
1150 COMPATIBLE_IOCTL(DMX_GET_STC)
1151 COMPATIBLE_IOCTL(DMX_REQBUFS)
1152 COMPATIBLE_IOCTL(DMX_QUERYBUF)
1153 COMPATIBLE_IOCTL(DMX_EXPBUF)
1154 COMPATIBLE_IOCTL(DMX_QBUF)
1155 COMPATIBLE_IOCTL(DMX_DQBUF)
1156 COMPATIBLE_IOCTL(VIDEO_STOP)
1157 COMPATIBLE_IOCTL(VIDEO_PLAY)
1158 COMPATIBLE_IOCTL(VIDEO_FREEZE)
1159 COMPATIBLE_IOCTL(VIDEO_CONTINUE)
1160 COMPATIBLE_IOCTL(VIDEO_SELECT_SOURCE)
1161 COMPATIBLE_IOCTL(VIDEO_SET_BLANK)
1162 COMPATIBLE_IOCTL(VIDEO_GET_STATUS)
1163 COMPATIBLE_IOCTL(VIDEO_SET_DISPLAY_FORMAT)
1164 COMPATIBLE_IOCTL(VIDEO_FAST_FORWARD)
1165 COMPATIBLE_IOCTL(VIDEO_SLOWMOTION)
1166 COMPATIBLE_IOCTL(VIDEO_GET_CAPABILITIES)
1167 COMPATIBLE_IOCTL(VIDEO_CLEAR_BUFFER)
1168 COMPATIBLE_IOCTL(VIDEO_SET_ID)
1169 COMPATIBLE_IOCTL(VIDEO_SET_STREAMTYPE)
1170 COMPATIBLE_IOCTL(VIDEO_SET_FORMAT)
1171 COMPATIBLE_IOCTL(VIDEO_SET_SYSTEM)
1172 COMPATIBLE_IOCTL(VIDEO_SET_HIGHLIGHT)
1173 COMPATIBLE_IOCTL(VIDEO_SET_SPU)
1174 COMPATIBLE_IOCTL(VIDEO_GET_NAVI)
1175 COMPATIBLE_IOCTL(VIDEO_SET_ATTRIBUTES)
1176 COMPATIBLE_IOCTL(VIDEO_GET_SIZE)
1177 COMPATIBLE_IOCTL(VIDEO_GET_FRAME_RATE)
1178 /* cec */
1179 COMPATIBLE_IOCTL(CEC_ADAP_G_CAPS)
1180 COMPATIBLE_IOCTL(CEC_ADAP_G_LOG_ADDRS)
1181 COMPATIBLE_IOCTL(CEC_ADAP_S_LOG_ADDRS)
1182 COMPATIBLE_IOCTL(CEC_ADAP_G_PHYS_ADDR)
1183 COMPATIBLE_IOCTL(CEC_ADAP_S_PHYS_ADDR)
1184 COMPATIBLE_IOCTL(CEC_G_MODE)
1185 COMPATIBLE_IOCTL(CEC_S_MODE)
1186 COMPATIBLE_IOCTL(CEC_TRANSMIT)
1187 COMPATIBLE_IOCTL(CEC_RECEIVE)
1188 COMPATIBLE_IOCTL(CEC_DQEVENT)
1189
1190 /* joystick */
1191 COMPATIBLE_IOCTL(JSIOCGVERSION)
1192 COMPATIBLE_IOCTL(JSIOCGAXES)
1193 COMPATIBLE_IOCTL(JSIOCGBUTTONS)
1194 COMPATIBLE_IOCTL(JSIOCGNAME(0))
1195
1196 #ifdef TIOCGLTC
1197 COMPATIBLE_IOCTL(TIOCGLTC)
1198 COMPATIBLE_IOCTL(TIOCSLTC)
1199 #endif
1200 #ifdef TIOCSTART
1201 /*
1202  * For these two we have definitions in ioctls.h and/or termios.h on
1203  * some architectures but no actual implemention.  Some applications
1204  * like bash call them if they are defined in the headers, so we provide
1205  * entries here to avoid syslog message spew.
1206  */
1207 COMPATIBLE_IOCTL(TIOCSTART)
1208 COMPATIBLE_IOCTL(TIOCSTOP)
1209 #endif
1210
1211 /* fat 'r' ioctls. These are handled by fat with ->compat_ioctl,
1212    but we don't want warnings on other file systems. So declare
1213    them as compatible here. */
1214 #define VFAT_IOCTL_READDIR_BOTH32       _IOR('r', 1, struct compat_dirent[2])
1215 #define VFAT_IOCTL_READDIR_SHORT32      _IOR('r', 2, struct compat_dirent[2])
1216
1217 IGNORE_IOCTL(VFAT_IOCTL_READDIR_BOTH32)
1218 IGNORE_IOCTL(VFAT_IOCTL_READDIR_SHORT32)
1219
1220 #ifdef CONFIG_SPARC
1221 /* Sparc framebuffers, handled in sbusfb_compat_ioctl() */
1222 IGNORE_IOCTL(FBIOGTYPE)
1223 IGNORE_IOCTL(FBIOSATTR)
1224 IGNORE_IOCTL(FBIOGATTR)
1225 IGNORE_IOCTL(FBIOSVIDEO)
1226 IGNORE_IOCTL(FBIOGVIDEO)
1227 IGNORE_IOCTL(FBIOSCURPOS)
1228 IGNORE_IOCTL(FBIOGCURPOS)
1229 IGNORE_IOCTL(FBIOGCURMAX)
1230 IGNORE_IOCTL(FBIOPUTCMAP32)
1231 IGNORE_IOCTL(FBIOGETCMAP32)
1232 IGNORE_IOCTL(FBIOSCURSOR32)
1233 IGNORE_IOCTL(FBIOGCURSOR32)
1234 #endif
1235 };
1236
1237 /*
1238  * Convert common ioctl arguments based on their command number
1239  *
1240  * Please do not add any code in here. Instead, implement
1241  * a compat_ioctl operation in the place that handleŃ• the
1242  * ioctl for the native case.
1243  */
1244 static long do_ioctl_trans(unsigned int cmd,
1245                  unsigned long arg, struct file *file)
1246 {
1247         void __user *argp = compat_ptr(arg);
1248
1249         switch (cmd) {
1250         case PPPIOCGIDLE32:
1251                 return ppp_gidle(file, cmd, argp);
1252         case PPPIOCSCOMPRESS32:
1253                 return ppp_scompress(file, cmd, argp);
1254         case PPPIOCSPASS32:
1255         case PPPIOCSACTIVE32:
1256                 return ppp_sock_fprog_ioctl_trans(file, cmd, argp);
1257 #ifdef CONFIG_BLOCK
1258         case SG_IO:
1259                 return sg_ioctl_trans(file, cmd, argp);
1260         case SG_GET_REQUEST_TABLE:
1261                 return sg_grt_trans(file, cmd, argp);
1262         case MTIOCGET32:
1263         case MTIOCPOS32:
1264                 return mt_ioctl_trans(file, cmd, argp);
1265 #endif
1266         /* Serial */
1267         case TIOCGSERIAL:
1268         case TIOCSSERIAL:
1269                 return serial_struct_ioctl(file, cmd, argp);
1270         /* Not implemented in the native kernel */
1271         case RTC_IRQP_READ32:
1272         case RTC_IRQP_SET32:
1273         case RTC_EPOCH_READ32:
1274         case RTC_EPOCH_SET32:
1275                 return rtc_ioctl(file, cmd, argp);
1276
1277         /* dvb */
1278         case VIDEO_GET_EVENT:
1279                 return do_video_get_event(file, cmd, argp);
1280         case VIDEO_STILLPICTURE:
1281                 return do_video_stillpicture(file, cmd, argp);
1282         case VIDEO_SET_SPU_PALETTE:
1283                 return do_video_set_spu_palette(file, cmd, argp);
1284         }
1285
1286         /*
1287          * These take an integer instead of a pointer as 'arg',
1288          * so we must not do a compat_ptr() translation.
1289          */
1290         switch (cmd) {
1291         /* Big T */
1292         case TCSBRKP:
1293         case TIOCMIWAIT:
1294         case TIOCSCTTY:
1295         /* RAID */
1296         case HOT_REMOVE_DISK:
1297         case HOT_ADD_DISK:
1298         case SET_DISK_FAULTY:
1299         case SET_BITMAP_FILE:
1300         /* Big K */
1301         case KDSIGACCEPT:
1302         case KIOCSOUND:
1303         case KDMKTONE:
1304         case KDSETMODE:
1305         case KDSKBMODE:
1306         case KDSKBMETA:
1307         case KDSKBLED:
1308         case KDSETLED:
1309                 return vfs_ioctl(file, cmd, arg);
1310         }
1311
1312         return -ENOIOCTLCMD;
1313 }
1314
1315 static int compat_ioctl_check_table(unsigned int xcmd)
1316 {
1317         int i;
1318         const int max = ARRAY_SIZE(ioctl_pointer) - 1;
1319
1320         BUILD_BUG_ON(max >= (1 << 16));
1321
1322         /* guess initial offset into table, assuming a
1323            normalized distribution */
1324         i = ((xcmd >> 16) * max) >> 16;
1325
1326         /* do linear search up first, until greater or equal */
1327         while (ioctl_pointer[i] < xcmd && i < max)
1328                 i++;
1329
1330         /* then do linear search down */
1331         while (ioctl_pointer[i] > xcmd && i > 0)
1332                 i--;
1333
1334         return ioctl_pointer[i] == xcmd;
1335 }
1336
1337 COMPAT_SYSCALL_DEFINE3(ioctl, unsigned int, fd, unsigned int, cmd,
1338                        compat_ulong_t, arg32)
1339 {
1340         unsigned long arg = arg32;
1341         struct fd f = fdget(fd);
1342         int error = -EBADF;
1343         if (!f.file)
1344                 goto out;
1345
1346         /* RED-PEN how should LSM module know it's handling 32bit? */
1347         error = security_file_ioctl(f.file, cmd, arg);
1348         if (error)
1349                 goto out_fput;
1350
1351         /*
1352          * To allow the compat_ioctl handlers to be self contained
1353          * we need to check the common ioctls here first.
1354          * Just handle them with the standard handlers below.
1355          */
1356         switch (cmd) {
1357         case FIOCLEX:
1358         case FIONCLEX:
1359         case FIONBIO:
1360         case FIOASYNC:
1361         case FIOQSIZE:
1362                 break;
1363
1364 #if defined(CONFIG_IA64) || defined(CONFIG_X86_64)
1365         case FS_IOC_RESVSP_32:
1366         case FS_IOC_RESVSP64_32:
1367                 error = compat_ioctl_preallocate(f.file, compat_ptr(arg));
1368                 goto out_fput;
1369 #else
1370         case FS_IOC_RESVSP:
1371         case FS_IOC_RESVSP64:
1372                 error = ioctl_preallocate(f.file, compat_ptr(arg));
1373                 goto out_fput;
1374 #endif
1375
1376         case FICLONE:
1377         case FICLONERANGE:
1378         case FIDEDUPERANGE:
1379         case FS_IOC_FIEMAP:
1380                 goto do_ioctl;
1381
1382         case FIBMAP:
1383         case FIGETBSZ:
1384         case FIONREAD:
1385                 if (S_ISREG(file_inode(f.file)->i_mode))
1386                         break;
1387                 /*FALL THROUGH*/
1388
1389         default:
1390                 if (f.file->f_op->compat_ioctl) {
1391                         error = f.file->f_op->compat_ioctl(f.file, cmd, arg);
1392                         if (error != -ENOIOCTLCMD)
1393                                 goto out_fput;
1394                 }
1395
1396                 if (!f.file->f_op->unlocked_ioctl)
1397                         goto do_ioctl;
1398                 break;
1399         }
1400
1401         if (compat_ioctl_check_table(XFORM(cmd)))
1402                 goto found_handler;
1403
1404         error = do_ioctl_trans(cmd, arg, f.file);
1405         if (error == -ENOIOCTLCMD)
1406                 error = -ENOTTY;
1407
1408         goto out_fput;
1409
1410  found_handler:
1411         arg = (unsigned long)compat_ptr(arg);
1412  do_ioctl:
1413         error = do_vfs_ioctl(f.file, fd, cmd, arg);
1414  out_fput:
1415         fdput(f);
1416  out:
1417         return error;
1418 }
1419
1420 static int __init init_sys32_ioctl_cmp(const void *p, const void *q)
1421 {
1422         unsigned int a, b;
1423         a = *(unsigned int *)p;
1424         b = *(unsigned int *)q;
1425         if (a > b)
1426                 return 1;
1427         if (a < b)
1428                 return -1;
1429         return 0;
1430 }
1431
1432 static int __init init_sys32_ioctl(void)
1433 {
1434         sort(ioctl_pointer, ARRAY_SIZE(ioctl_pointer), sizeof(*ioctl_pointer),
1435                 init_sys32_ioctl_cmp, NULL);
1436         return 0;
1437 }
1438 __initcall(init_sys32_ioctl);