11bd4fe43Sopenharmony_ci/*
21bd4fe43Sopenharmony_ci * Copyright (c) 2022 HiSilicon (Shanghai) Technologies CO., LIMITED.
31bd4fe43Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
41bd4fe43Sopenharmony_ci * you may not use this file except in compliance with the License.
51bd4fe43Sopenharmony_ci * You may obtain a copy of the License at
61bd4fe43Sopenharmony_ci *
71bd4fe43Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
81bd4fe43Sopenharmony_ci *
91bd4fe43Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
101bd4fe43Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
111bd4fe43Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
121bd4fe43Sopenharmony_ci * See the License for the specific language governing permissions and
131bd4fe43Sopenharmony_ci * limitations under the License.
141bd4fe43Sopenharmony_ci */
151bd4fe43Sopenharmony_ci
161bd4fe43Sopenharmony_ci#ifndef _DRV_IOCTL_H
171bd4fe43Sopenharmony_ci#define _DRV_IOCTL_H
181bd4fe43Sopenharmony_ci
191bd4fe43Sopenharmony_ci/* ioctl command encoding: 32 bits total, command in lower 16 bits,
201bd4fe43Sopenharmony_ci * size of the parameter structure in the lower 14 bits of the
211bd4fe43Sopenharmony_ci * upper 16 bits.
221bd4fe43Sopenharmony_ci * Encoding the size of the parameter structure in the ioctl request
231bd4fe43Sopenharmony_ci * is useful for catching programs compiled with old versions
241bd4fe43Sopenharmony_ci * and to avoid overwriting user space outside the user buffer area.
251bd4fe43Sopenharmony_ci * The highest 2 bits are reserved for indicating the ``access mode''.
261bd4fe43Sopenharmony_ci * NOTE: This limits the max parameter size to 16kB -1 !
271bd4fe43Sopenharmony_ci */
281bd4fe43Sopenharmony_ci/*
291bd4fe43Sopenharmony_ci * The following is for compatibility across the various Linux
301bd4fe43Sopenharmony_ci * platforms.  The generic ioctl numbering scheme doesn't really enforce
311bd4fe43Sopenharmony_ci * a type field.  De facto, however, the top 8 bits of the lower 16
321bd4fe43Sopenharmony_ci * bits are indeed used as a type field, so we might just as well make
331bd4fe43Sopenharmony_ci * this explicit here.  Please be sure to use the decoding macros
341bd4fe43Sopenharmony_ci * below from now on.
351bd4fe43Sopenharmony_ci */
361bd4fe43Sopenharmony_ci#define _IOC_NRBITS    8
371bd4fe43Sopenharmony_ci#define _IOC_TYPEBITS  8
381bd4fe43Sopenharmony_ci
391bd4fe43Sopenharmony_ci/*
401bd4fe43Sopenharmony_ci * Let any architecture override either of the following before
411bd4fe43Sopenharmony_ci * including this file.
421bd4fe43Sopenharmony_ci */
431bd4fe43Sopenharmony_ci
441bd4fe43Sopenharmony_ci#ifndef _IOC_SIZEBITS
451bd4fe43Sopenharmony_ci#define _IOC_SIZEBITS  14
461bd4fe43Sopenharmony_ci#endif
471bd4fe43Sopenharmony_ci
481bd4fe43Sopenharmony_ci#ifndef _IOC_DIRBITS
491bd4fe43Sopenharmony_ci#define _IOC_DIRBITS   2
501bd4fe43Sopenharmony_ci#endif
511bd4fe43Sopenharmony_ci
521bd4fe43Sopenharmony_ci#ifndef _IOC_NRMASK
531bd4fe43Sopenharmony_ci#define _IOC_NRMASK    ((1 << _IOC_NRBITS)-1)
541bd4fe43Sopenharmony_ci#endif
551bd4fe43Sopenharmony_ci#ifndef _IOC_TYPEMASK
561bd4fe43Sopenharmony_ci#define _IOC_TYPEMASK    ((1 << _IOC_TYPEBITS)-1)
571bd4fe43Sopenharmony_ci#endif
581bd4fe43Sopenharmony_ci#ifndef _IOC_SIZEMASK
591bd4fe43Sopenharmony_ci#define _IOC_SIZEMASK    ((1 << _IOC_SIZEBITS)-1)
601bd4fe43Sopenharmony_ci#endif
611bd4fe43Sopenharmony_ci#ifndef _IOC_DIRMASK
621bd4fe43Sopenharmony_ci#define _IOC_DIRMASK    ((1 << _IOC_DIRBITS)-1)
631bd4fe43Sopenharmony_ci#endif
641bd4fe43Sopenharmony_ci
651bd4fe43Sopenharmony_ci#ifndef _IOC_NRSHIFT
661bd4fe43Sopenharmony_ci#define _IOC_NRSHIFT    0
671bd4fe43Sopenharmony_ci#endif
681bd4fe43Sopenharmony_ci#ifndef _IOC_TYPESHIFT
691bd4fe43Sopenharmony_ci#define _IOC_TYPESHIFT    (_IOC_NRSHIFT + _IOC_NRBITS)
701bd4fe43Sopenharmony_ci#endif
711bd4fe43Sopenharmony_ci#ifndef _IOC_SIZESHIFT
721bd4fe43Sopenharmony_ci#define _IOC_SIZESHIFT    (_IOC_TYPESHIFT + _IOC_TYPEBITS)
731bd4fe43Sopenharmony_ci#endif
741bd4fe43Sopenharmony_ci#ifndef _IOC_DIRSHIFT
751bd4fe43Sopenharmony_ci#define _IOC_DIRSHIFT    (_IOC_SIZESHIFT + _IOC_SIZEBITS)
761bd4fe43Sopenharmony_ci#endif
771bd4fe43Sopenharmony_ci
781bd4fe43Sopenharmony_ci/*
791bd4fe43Sopenharmony_ci * Direction bits, which any architecture can choose to override
801bd4fe43Sopenharmony_ci * before including this file.
811bd4fe43Sopenharmony_ci */
821bd4fe43Sopenharmony_ci
831bd4fe43Sopenharmony_ci#ifndef _IOC_NONE
841bd4fe43Sopenharmony_ci#define _IOC_NONE      0U
851bd4fe43Sopenharmony_ci#endif
861bd4fe43Sopenharmony_ci
871bd4fe43Sopenharmony_ci#ifndef _IOC_WRITE
881bd4fe43Sopenharmony_ci#define _IOC_WRITE     1U
891bd4fe43Sopenharmony_ci#endif
901bd4fe43Sopenharmony_ci
911bd4fe43Sopenharmony_ci#ifndef _IOC_READ
921bd4fe43Sopenharmony_ci#define _IOC_READ      2U
931bd4fe43Sopenharmony_ci#endif
941bd4fe43Sopenharmony_ci
951bd4fe43Sopenharmony_ci#ifndef _IOC
961bd4fe43Sopenharmony_ci#define _IOC(dir, type, nr, size)    \
971bd4fe43Sopenharmony_ci    (((dir) << _IOC_DIRSHIFT) |  \
981bd4fe43Sopenharmony_ci        ((type) << _IOC_TYPESHIFT) |  \
991bd4fe43Sopenharmony_ci        ((nr) << _IOC_NRSHIFT) |  \
1001bd4fe43Sopenharmony_ci        ((size) << _IOC_SIZESHIFT))
1011bd4fe43Sopenharmony_ci#endif
1021bd4fe43Sopenharmony_ci
1031bd4fe43Sopenharmony_ci#ifdef __CHECKER__
1041bd4fe43Sopenharmony_ci#define _IOC_TYPECHECK(t) (sizeof(t))
1051bd4fe43Sopenharmony_ci#else
1061bd4fe43Sopenharmony_ci/* provoke compile error for invalid uses of size argument */
1071bd4fe43Sopenharmony_ciextern unsigned int __invalid_size_argument_for_IOC;
1081bd4fe43Sopenharmony_ci#define _IOC_TYPECHECK(t)                  \
1091bd4fe43Sopenharmony_ci    ((sizeof(t) == sizeof(t[1]) &&         \
1101bd4fe43Sopenharmony_ci         sizeof(t) < (1 << _IOC_SIZEBITS)) \
1111bd4fe43Sopenharmony_ci            ? sizeof(t)                    \
1121bd4fe43Sopenharmony_ci            : __invalid_size_argument_for_IOC)
1131bd4fe43Sopenharmony_ci#endif
1141bd4fe43Sopenharmony_ci
1151bd4fe43Sopenharmony_ci/* used to create numbers */
1161bd4fe43Sopenharmony_ci#ifndef _IO
1171bd4fe43Sopenharmony_ci#define _IO(type, nr)        _IOC(_IOC_NONE, (type), (nr), 0)
1181bd4fe43Sopenharmony_ci#endif
1191bd4fe43Sopenharmony_ci
1201bd4fe43Sopenharmony_ci#ifndef _IOR
1211bd4fe43Sopenharmony_ci#define _IOR(type, nr, size)    _IOC(_IOC_READ, (type), (nr), (_IOC_TYPECHECK(size)))
1221bd4fe43Sopenharmony_ci#endif
1231bd4fe43Sopenharmony_ci
1241bd4fe43Sopenharmony_ci#ifndef _IOW
1251bd4fe43Sopenharmony_ci#define _IOW(type, nr, size)    _IOC(_IOC_WRITE, (type), (nr), (_IOC_TYPECHECK(size)))
1261bd4fe43Sopenharmony_ci#endif
1271bd4fe43Sopenharmony_ci
1281bd4fe43Sopenharmony_ci#ifndef _IOWR
1291bd4fe43Sopenharmony_ci#define _IOWR(type, nr, size)    _IOC(_IOC_READ | _IOC_WRITE, (type), (nr), (_IOC_TYPECHECK(size)))
1301bd4fe43Sopenharmony_ci#endif
1311bd4fe43Sopenharmony_ci
1321bd4fe43Sopenharmony_ci#ifndef _IOR_BAD
1331bd4fe43Sopenharmony_ci#define _IOR_BAD(type, nr, size)    _IOC(_IOC_READ, (type), (nr), sizeof(size))
1341bd4fe43Sopenharmony_ci#endif
1351bd4fe43Sopenharmony_ci
1361bd4fe43Sopenharmony_ci#ifndef _IOW_BAD
1371bd4fe43Sopenharmony_ci#define _IOW_BAD(type, nr, size)    _IOC(_IOC_WRITE, (type), (nr), sizeof(size))
1381bd4fe43Sopenharmony_ci#endif
1391bd4fe43Sopenharmony_ci
1401bd4fe43Sopenharmony_ci#ifndef _IOWR_BAD
1411bd4fe43Sopenharmony_ci#define _IOWR_BAD(type, nr, size)    _IOC(_IOC_READ | _IOC_WRITE, (type), (nr), sizeof(size))
1421bd4fe43Sopenharmony_ci#endif
1431bd4fe43Sopenharmony_ci
1441bd4fe43Sopenharmony_ci/* used to decode ioctl numbers.. */
1451bd4fe43Sopenharmony_ci
1461bd4fe43Sopenharmony_ci#ifndef _IOC_DIR
1471bd4fe43Sopenharmony_ci#define _IOC_DIR(nr)        (((nr) >> _IOC_DIRSHIFT) & _IOC_DIRMASK)
1481bd4fe43Sopenharmony_ci#endif
1491bd4fe43Sopenharmony_ci
1501bd4fe43Sopenharmony_ci#ifndef _IOC_TYPE
1511bd4fe43Sopenharmony_ci
1521bd4fe43Sopenharmony_ci#define _IOC_TYPE(nr)        (((nr) >> _IOC_TYPESHIFT) & _IOC_TYPEMASK)
1531bd4fe43Sopenharmony_ci#endif
1541bd4fe43Sopenharmony_ci
1551bd4fe43Sopenharmony_ci#ifndef _IOC_NR
1561bd4fe43Sopenharmony_ci#define _IOC_NR(nr)        (((nr) >> _IOC_NRSHIFT) & _IOC_NRMASK)
1571bd4fe43Sopenharmony_ci#endif
1581bd4fe43Sopenharmony_ci
1591bd4fe43Sopenharmony_ci#ifndef _IOC_SIZE
1601bd4fe43Sopenharmony_ci#define _IOC_SIZE(nr)        (((nr) >> _IOC_SIZESHIFT) & _IOC_SIZEMASK)
1611bd4fe43Sopenharmony_ci#endif
1621bd4fe43Sopenharmony_ci
1631bd4fe43Sopenharmony_ci
1641bd4fe43Sopenharmony_ci#ifndef IOC_IN
1651bd4fe43Sopenharmony_ci#define IOC_IN        (_IOC_WRITE << _IOC_DIRSHIFT)
1661bd4fe43Sopenharmony_ci#endif
1671bd4fe43Sopenharmony_ci
1681bd4fe43Sopenharmony_ci#ifndef IOC_OUT
1691bd4fe43Sopenharmony_ci#define IOC_OUT        (_IOC_READ << _IOC_DIRSHIFT)
1701bd4fe43Sopenharmony_ci#endif
1711bd4fe43Sopenharmony_ci
1721bd4fe43Sopenharmony_ci#ifndef IOC_INOUT
1731bd4fe43Sopenharmony_ci#define IOC_INOUT    ((_IOC_WRITE | _IOC_READ) << _IOC_DIRSHIFT)
1741bd4fe43Sopenharmony_ci#endif
1751bd4fe43Sopenharmony_ci
1761bd4fe43Sopenharmony_ci#ifndef IOCSIZE_MASK
1771bd4fe43Sopenharmony_ci#define IOCSIZE_MASK    (_IOC_SIZEMASK << _IOC_SIZESHIFT)
1781bd4fe43Sopenharmony_ci#endif
1791bd4fe43Sopenharmony_ci
1801bd4fe43Sopenharmony_ci#ifndef IOCSIZE_SHIFT
1811bd4fe43Sopenharmony_ci#define IOCSIZE_SHIFT    (_IOC_SIZESHIFT)
1821bd4fe43Sopenharmony_ci#endif
1831bd4fe43Sopenharmony_ci
1841bd4fe43Sopenharmony_ci#endif /* _DRV_IOCTL_H */
185