• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
2 
3 #ifndef _OSAL_IOCTL_H
4 #define _OSAL_IOCTL_H
5 
6 /* ioctl command encoding: 32 bits total, command in lower 16 bits,
7  * size of the parameter structure in the lower 14 bits of the
8  * upper 16 bits.
9  * Encoding the size of the parameter structure in the ioctl request
10  * is useful for catching programs compiled with old versions
11  * and to avoid overwriting user space outside the user buffer area.
12  * The highest 2 bits are reserved for indicating the ``access mode''.
13  * NOTE: This limits the max parameter size to 16kB -1 !
14  */
15 
16 /*
17  * The following is for compatibility across the various Linux
18  * platforms.  The generic ioctl numbering scheme doesn't really enforce
19  * a type field.  De facto, however, the top 8 bits of the lower 16
20  * bits are indeed used as a type field, so we might just as well make
21  * this explicit here.  Please be sure to use the decoding macros
22  * below from now on.
23  */
24 #define _IOC_NRBITS    8
25 #define _IOC_TYPEBITS  8
26 
27 /*
28  * Let any architecture override either of the following before
29  * including this file.
30  */
31 #ifndef _IOC_SIZEBITS
32 #define _IOC_SIZEBITS  14
33 #endif
34 
35 #ifndef _IOC_DIRBITS
36 #define _IOC_DIRBITS   2
37 #endif
38 
39 #define _IOC_NRMASK    ((1 << _IOC_NRBITS)-1)
40 #define _IOC_TYPEMASK    ((1 << _IOC_TYPEBITS)-1)
41 #define _IOC_SIZEMASK    ((1 << _IOC_SIZEBITS)-1)
42 #define _IOC_DIRMASK    ((1 << _IOC_DIRBITS)-1)
43 
44 #define _IOC_NRSHIFT    0
45 #define _IOC_TYPESHIFT    (_IOC_NRSHIFT+_IOC_NRBITS)
46 #define _IOC_SIZESHIFT    (_IOC_TYPESHIFT+_IOC_TYPEBITS)
47 #define _IOC_DIRSHIFT    (_IOC_SIZESHIFT+_IOC_SIZEBITS)
48 
49 /*
50  * Direction bits, which any architecture can choose to override
51  * before including this file.
52  */
53 #ifndef _IOC_NONE
54 #define _IOC_NONE      0U
55 #endif
56 
57 #ifndef _IOC_WRITE
58 #define _IOC_WRITE     1U
59 #endif
60 
61 #ifndef _IOC_READ
62 #define _IOC_READ      2U
63 #endif
64 
65 #define _IOC(dir, type, nr, size)    \
66     (((dir) << _IOC_DIRSHIFT) |  \
67         ((type) << _IOC_TYPESHIFT) |  \
68         ((nr) << _IOC_NRSHIFT) |  \
69         ((size) << _IOC_SIZESHIFT))
70 
71 #ifdef __CHECKER__
72 #define _IOC_TYPECHECK(t) (sizeof(t))
73 #else
74 #ifndef _IOC_TYPECHECK
75 /* provoke compile error for invalid uses of size argument */
76 extern unsigned int __invalid_size_argument_for_IOC;
77 #define _IOC_TYPECHECK(t)                  \
78     ((sizeof(t) == sizeof(t[1]) &&         \
79          sizeof(t) < (1 << _IOC_SIZEBITS)) \
80             ? sizeof(t)                    \
81             : __invalid_size_argument_for_IOC)
82 #endif
83 #endif
84 
85 /* used to create numbers */
86 #define _IO(type,nr)        _IOC(_IOC_NONE,(type),(nr),0)
87 #define _IOR(type,nr,size)    _IOC(_IOC_READ,(type),(nr),(_IOC_TYPECHECK(size)))
88 #define _IOW(type,nr,size)    _IOC(_IOC_WRITE,(type),(nr),(_IOC_TYPECHECK(size)))
89 #define _IOWR(type,nr,size)    _IOC(_IOC_READ|_IOC_WRITE,(type),(nr),(_IOC_TYPECHECK(size)))
90 #define _IOR_BAD(type,nr,size)    _IOC(_IOC_READ,(type),(nr),sizeof(size))
91 #define _IOW_BAD(type,nr,size)    _IOC(_IOC_WRITE,(type),(nr),sizeof(size))
92 #define _IOWR_BAD(type,nr,size)    _IOC(_IOC_READ|_IOC_WRITE,(type),(nr),sizeof(size))
93 
94 /* used to decode ioctl numbers.. */
95 #define _IOC_DIR(nr)        (((nr) >> _IOC_DIRSHIFT) & _IOC_DIRMASK)
96 #define _IOC_TYPE(nr)        (((nr) >> _IOC_TYPESHIFT) & _IOC_TYPEMASK)
97 #define _IOC_NR(nr)        (((nr) >> _IOC_NRSHIFT) & _IOC_NRMASK)
98 #define _IOC_SIZE(nr)        (((nr) >> _IOC_SIZESHIFT) & _IOC_SIZEMASK)
99 
100 #define IOC_IN        (_IOC_WRITE << _IOC_DIRSHIFT)
101 #define IOC_OUT        (_IOC_READ << _IOC_DIRSHIFT)
102 #define IOC_INOUT    ((_IOC_WRITE|_IOC_READ) << _IOC_DIRSHIFT)
103 #define IOCSIZE_MASK    (_IOC_SIZEMASK << _IOC_SIZESHIFT)
104 #define IOCSIZE_SHIFT    (_IOC_SIZESHIFT)
105 
106 #endif /* _OSAL_IOCTL_H */
107