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