1/*
2 * Copyright (c) 2022 HiSilicon (Shanghai) Technologies CO., LIMITED.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16#ifndef _DRV_IOCTL_H
17#define _DRV_IOCTL_H
18
19/* ioctl command encoding: 32 bits total, command in lower 16 bits,
20 * size of the parameter structure in the lower 14 bits of the
21 * upper 16 bits.
22 * Encoding the size of the parameter structure in the ioctl request
23 * is useful for catching programs compiled with old versions
24 * and to avoid overwriting user space outside the user buffer area.
25 * The highest 2 bits are reserved for indicating the ``access mode''.
26 * NOTE: This limits the max parameter size to 16kB -1 !
27 */
28/*
29 * The following is for compatibility across the various Linux
30 * platforms.  The generic ioctl numbering scheme doesn't really enforce
31 * a type field.  De facto, however, the top 8 bits of the lower 16
32 * bits are indeed used as a type field, so we might just as well make
33 * this explicit here.  Please be sure to use the decoding macros
34 * below from now on.
35 */
36#define _IOC_NRBITS    8
37#define _IOC_TYPEBITS  8
38
39/*
40 * Let any architecture override either of the following before
41 * including this file.
42 */
43
44#ifndef _IOC_SIZEBITS
45#define _IOC_SIZEBITS  14
46#endif
47
48#ifndef _IOC_DIRBITS
49#define _IOC_DIRBITS   2
50#endif
51
52#ifndef _IOC_NRMASK
53#define _IOC_NRMASK    ((1 << _IOC_NRBITS)-1)
54#endif
55#ifndef _IOC_TYPEMASK
56#define _IOC_TYPEMASK    ((1 << _IOC_TYPEBITS)-1)
57#endif
58#ifndef _IOC_SIZEMASK
59#define _IOC_SIZEMASK    ((1 << _IOC_SIZEBITS)-1)
60#endif
61#ifndef _IOC_DIRMASK
62#define _IOC_DIRMASK    ((1 << _IOC_DIRBITS)-1)
63#endif
64
65#ifndef _IOC_NRSHIFT
66#define _IOC_NRSHIFT    0
67#endif
68#ifndef _IOC_TYPESHIFT
69#define _IOC_TYPESHIFT    (_IOC_NRSHIFT + _IOC_NRBITS)
70#endif
71#ifndef _IOC_SIZESHIFT
72#define _IOC_SIZESHIFT    (_IOC_TYPESHIFT + _IOC_TYPEBITS)
73#endif
74#ifndef _IOC_DIRSHIFT
75#define _IOC_DIRSHIFT    (_IOC_SIZESHIFT + _IOC_SIZEBITS)
76#endif
77
78/*
79 * Direction bits, which any architecture can choose to override
80 * before including this file.
81 */
82
83#ifndef _IOC_NONE
84#define _IOC_NONE      0U
85#endif
86
87#ifndef _IOC_WRITE
88#define _IOC_WRITE     1U
89#endif
90
91#ifndef _IOC_READ
92#define _IOC_READ      2U
93#endif
94
95#ifndef _IOC
96#define _IOC(dir, type, nr, size)    \
97    (((dir) << _IOC_DIRSHIFT) |  \
98        ((type) << _IOC_TYPESHIFT) |  \
99        ((nr) << _IOC_NRSHIFT) |  \
100        ((size) << _IOC_SIZESHIFT))
101#endif
102
103#ifdef __CHECKER__
104#define _IOC_TYPECHECK(t) (sizeof(t))
105#else
106/* provoke compile error for invalid uses of size argument */
107extern unsigned int __invalid_size_argument_for_IOC;
108#define _IOC_TYPECHECK(t)                  \
109    ((sizeof(t) == sizeof(t[1]) &&         \
110         sizeof(t) < (1 << _IOC_SIZEBITS)) \
111            ? sizeof(t)                    \
112            : __invalid_size_argument_for_IOC)
113#endif
114
115/* used to create numbers */
116#ifndef _IO
117#define _IO(type, nr)        _IOC(_IOC_NONE, (type), (nr), 0)
118#endif
119
120#ifndef _IOR
121#define _IOR(type, nr, size)    _IOC(_IOC_READ, (type), (nr), (_IOC_TYPECHECK(size)))
122#endif
123
124#ifndef _IOW
125#define _IOW(type, nr, size)    _IOC(_IOC_WRITE, (type), (nr), (_IOC_TYPECHECK(size)))
126#endif
127
128#ifndef _IOWR
129#define _IOWR(type, nr, size)    _IOC(_IOC_READ | _IOC_WRITE, (type), (nr), (_IOC_TYPECHECK(size)))
130#endif
131
132#ifndef _IOR_BAD
133#define _IOR_BAD(type, nr, size)    _IOC(_IOC_READ, (type), (nr), sizeof(size))
134#endif
135
136#ifndef _IOW_BAD
137#define _IOW_BAD(type, nr, size)    _IOC(_IOC_WRITE, (type), (nr), sizeof(size))
138#endif
139
140#ifndef _IOWR_BAD
141#define _IOWR_BAD(type, nr, size)    _IOC(_IOC_READ | _IOC_WRITE, (type), (nr), sizeof(size))
142#endif
143
144/* used to decode ioctl numbers.. */
145
146#ifndef _IOC_DIR
147#define _IOC_DIR(nr)        (((nr) >> _IOC_DIRSHIFT) & _IOC_DIRMASK)
148#endif
149
150#ifndef _IOC_TYPE
151
152#define _IOC_TYPE(nr)        (((nr) >> _IOC_TYPESHIFT) & _IOC_TYPEMASK)
153#endif
154
155#ifndef _IOC_NR
156#define _IOC_NR(nr)        (((nr) >> _IOC_NRSHIFT) & _IOC_NRMASK)
157#endif
158
159#ifndef _IOC_SIZE
160#define _IOC_SIZE(nr)        (((nr) >> _IOC_SIZESHIFT) & _IOC_SIZEMASK)
161#endif
162
163
164#ifndef IOC_IN
165#define IOC_IN        (_IOC_WRITE << _IOC_DIRSHIFT)
166#endif
167
168#ifndef IOC_OUT
169#define IOC_OUT        (_IOC_READ << _IOC_DIRSHIFT)
170#endif
171
172#ifndef IOC_INOUT
173#define IOC_INOUT    ((_IOC_WRITE | _IOC_READ) << _IOC_DIRSHIFT)
174#endif
175
176#ifndef IOCSIZE_MASK
177#define IOCSIZE_MASK    (_IOC_SIZEMASK << _IOC_SIZESHIFT)
178#endif
179
180#ifndef IOCSIZE_SHIFT
181#define IOCSIZE_SHIFT    (_IOC_SIZESHIFT)
182#endif
183
184#endif /* _DRV_IOCTL_H */
185