1 /**
2 * D header file for POSIX.
3 *
4 * License: $(HTTP www.boost.org/LICENSE_1_0.txt, Boost License 1.0).
5 */
6
7 module core.sys.posix.sys.ioccom;
8
9 version (OSX)
10 version = Darwin;
11 else version (iOS)
12 version = Darwin;
13 else version (TVOS)
14 version = Darwin;
15 else version (WatchOS)
16 version = Darwin;
17
version(Posix)18 version (Posix):
19
20 nothrow @nogc:
21 @system:
22
23 version (Darwin)
24 {
25 /* OSX ioctl's (based on FreeBSD) encode the command in the lower 16-bits
26 * and the size of any in/out parameters in the lower 13 bits of the upper
27 * 16-bits of a 32 bit unsigned integer. The high 3 bits of the upper
28 * 16-bits encode the in/out status of the parameter.
29 */
30 enum uint IOCPARM_MASK = 0x1fff; // parameter length, at most 13 bits
31 uint IOCPARM_LEN(uint x) // to extract the encoded parameter length
32 {
33 return ((x >> 16) & IOCPARM_MASK);
34 }
35 uint IOCBASECMD(uint x) // to extract the encoded command
36 {
37 return (x & ~(IOCPARM_MASK << 16));
38 }
39 uint IOCGROUP(uint x) // to extract the encoded group
40 {
41 return ((x >> 8) & 0xff);
42 }
43
44 enum uint IOCPARM_MAX = (IOCPARM_MASK + 1); // max size of ioctl args
45
46 enum uint IOC_VOID = 0x20000000; // no parameters
47 enum uint IOC_OUT = 0x40000000; // copy parameters back
48 enum uint IOC_IN = 0x80000000; // copy parameters into
49 enum uint IOC_INOUT = (IOC_IN | IOC_OUT); // copy parameter into and get back
50 enum uint IOC_DIRMASK = 0xe0000000; // mask to extract above direction parameters
51
52 // encode the ioctl info into 32 bits
53 uint _IOC(T=typeof(null))(uint inorout, uint group, uint num, size_t len)
54 {
55 return (inorout | ((len & IOCPARM_MASK) << 16) | (group << 8) | num);
56 }
57
58 // encode a command with no parameters
59 uint _IO(char g, int n)
60 {
61 return _IOC(IOC_VOID, cast(uint)g, cast(uint)n, cast(size_t)0);
62 }
63 // encode a command that returns info
64 uint _IOR(T)(char g, int n)
65 {
66 return _IOC!(T)(IOC_OUT, cast(uint)g, cast(uint)n, T.sizeof);
67 }
68 // encode a command that takes info
69 uint _IOW(T)(char g, int n)
70 {
71 return _IOC!(T)(IOC_IN, cast(uint)g, cast(uint)n, T.sizeof);
72 }
73 // encode a command that takes info and returns info
74 uint _IOWR(T)(char g, int n)
75 {
76 return _IOC!(T)(IOC_INOUT, cast(uint)g, cast(uint)n, T.sizeof);
77 }
78 }
version(FreeBSD)79 else version (FreeBSD)
80 {
81 /* FreeBSD ioctl's encode the command in the lower 16-bits
82 * and the size of any in/out parameters in the lower 13 bits of the upper
83 * 16-bits of a 32 bit unsigned integer. The high 3 bits of the upper
84 * 16-bits encode the in/out status of the parameter.
85 */
86 enum uint IOCPARM_SHIFT = 13; // number of bits for ioctl size
87 enum uint IOCPARM_MASK = ((1 << IOCPARM_SHIFT) - 1); // parameter length mask
88 uint IOCPARM_LEN(uint x) // to extract the encoded parameter length
89 {
90 return ((x >> 16) & IOCPARM_MASK);
91 }
92 uint IOCBASECMD(uint x) // to extract the encoded command
93 {
94 return (x & ~(IOCPARM_MASK << 16));
95 }
96 uint IOCGROUP(uint x) // to extract the encoded group
97 {
98 return ((x >> 8) & 0xff);
99 }
100
101 enum uint IOCPARM_MAX = (1 << IOCPARM_SHIFT); // max size of ioctl args
102
103 enum uint IOC_VOID = 0x20000000; // no parameters
104 enum uint IOC_OUT = 0x40000000; // copy parameters back
105 enum uint IOC_IN = 0x80000000; // copy parameters into
106 enum uint IOC_INOUT = (IOC_IN | IOC_OUT);
107 enum uint IOC_DIRMASK = (IOC_VOID|IOC_OUT|IOC_IN);
108
109 // encode the ioctl info into 32 bits
110 uint _IOC(uint inorout, uint group, uint num, size_t len)
111 {
112 return (inorout | ((len & IOCPARM_MASK) << 16) | (group << 8) | num);
113 }
114
115 // encode a command with no parameters
116 uint _IO(char g, int n)
117 {
118 return _IOC(IOC_VOID, cast(uint)g, cast(uint)n, cast(size_t)0);
119 }
120 uint _IOWINT(char g, int n)
121 {
122 return _IOC(IOC_VOID, cast(uint)g, cast(uint)n, int.sizeof);
123 }
124 // encode a command that returns info
125 uint _IOR(T)(char g, int n)
126 {
127 return _IOC(IOC_OUT, cast(uint)g, cast(uint)n, T.sizeof);
128 }
129 // encode a command that takes info
130 uint _IOW(T)(char g, int n)
131 {
132 return _IOC(IOC_IN, cast(uint)g, cast(uint)n, T.sizeof);
133 }
134 // encode a command that takes info and returns info
135 uint _IOWR(T)(char g, int n)
136 {
137 return _IOC(IOC_INOUT, cast(uint)g, cast(uint)n, T.sizeof);
138 }
139 }
version(OpenBSD)140 else version (OpenBSD)
141 {
142 /* OpenBSD ioctl's encode the command in the lower 16-bits
143 * and the size of any in/out parameters in the lower 13 bits of the upper
144 * 16-bits of a 32 bit unsigned integer. The high 3 bits of the upper
145 * 16-bits encode the in/out status of the parameter.
146 */
147 enum uint IOCPARM_MASK = 0x1fff; // parameter length mask
148 uint IOCPARM_LEN(uint x) // to extract the encoded parameter length
149 {
150 return ((x >> 16) & IOCPARM_MASK);
151 }
152 uint IOCBASECMD(uint x) // to extract the encoded command
153 {
154 return (x & ~(IOCPARM_MASK << 16));
155 }
156 uint IOCGROUP(uint x) // to extract the encoded group
157 {
158 return ((x >> 8) & 0xff);
159 }
160
161 enum uint IOCPARM_MAX = (1 << 12); // max size of ioctl args
162
163 enum uint IOC_VOID = 0x20000000; // no parameters
164 enum uint IOC_OUT = 0x40000000; // copy parameters back
165 enum uint IOC_IN = 0x80000000; // copy parameters into
166 enum uint IOC_INOUT = (IOC_IN | IOC_OUT);
167 enum uint IOC_DIRMASK = 0xe0000000;
168
169 // encode the ioctl info into 32 bits
170 uint _IOC(uint inorout, uint group, uint num, size_t len)
171 {
172 return (inorout | ((len & IOCPARM_MASK) << 16) | (group << 8) | num);
173 }
174
175 // encode a command with no parameters
176 uint _IO(char g, int n)
177 {
178 return _IOC(IOC_VOID, cast(uint)g, cast(uint)n, cast(size_t)0);
179 }
180 // encode a command that returns info
181 uint _IOR(T)(char g, int n)
182 {
183 return _IOC(IOC_OUT, cast(uint)g, cast(uint)n, T.sizeof);
184 }
185 // encode a command that takes info
186 uint _IOW(T)(char g, int n)
187 {
188 return _IOC(IOC_IN, cast(uint)g, cast(uint)n, T.sizeof);
189 }
190 // encode a command that takes info and returns info
191 uint _IOWR(T)(char g, int n)
192 {
193 return _IOC(IOC_INOUT, cast(uint)g, cast(uint)n, T.sizeof);
194 }
195 }
196