]> git.ipfire.org Git - thirdparty/gcc.git/blob - libphobos/libdruntime/core/sys/posix/sys/ioccom.d
libphobos: Merge upstream druntime 5bb8ce19
[thirdparty/gcc.git] / libphobos / libdruntime / core / sys / posix / sys / ioccom.d
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 (Posix):
10
11 nothrow @nogc:
12
13 version (OSX)
14 {
15 /* OSX ioctl's (based on FreeBSD) encode the command in the lower 16-bits
16 * and the size of any in/out parameters in the lower 13 bits of the upper
17 * 16-bits of a 32 bit unsigned integer. The high 3 bits of the upper
18 * 16-bits encode the in/out status of the parameter.
19 */
20 enum uint IOCPARM_MASK = 0x1fff; // parameter length, at most 13 bits
21 uint IOCPARM_LEN(uint x) // to extract the encoded parameter length
22 {
23 return ((x >> 16) & IOCPARM_MASK);
24 }
25 uint IOCBASECMD(uint x) // to extract the encoded command
26 {
27 return (x & ~(IOCPARM_MASK << 16));
28 }
29 uint IOCGROUP(uint x) // to extract the encoded group
30 {
31 return ((x >> 8) & 0xff);
32 }
33
34 enum uint IOCPARM_MAX = (IOCPARM_MASK + 1); // max size of ioctl args
35
36 enum uint IOC_VOID = 0x20000000; // no parameters
37 enum uint IOC_OUT = 0x40000000; // copy parameters back
38 enum uint IOC_IN = 0x80000000; // copy parameters into
39 enum uint IOC_INOUT = (IOC_IN | IOC_OUT); // copy parameter into and get back
40 enum uint IOC_DIRMASK = 0xe0000000; // mask to extract above direction parameters
41
42 // encode the ioctl info into 32 bits
43 uint _IOC(T=typeof(null))(uint inorout, uint group, uint num, size_t len)
44 {
45 return (inorout | ((len & IOCPARM_MASK) << 16) | (group << 8) | num);
46 }
47
48 // encode a command with no parameters
49 uint _IO(char g, int n)
50 {
51 return _IOC(IOC_VOID, cast(uint)g, cast(uint)n, cast(size_t)0);
52 }
53 // encode a command that returns info
54 uint _IOR(T)(char g, int n)
55 {
56 return _IOC!(T)(IOC_OUT, cast(uint)g, cast(uint)n, T.sizeof);
57 }
58 // encode a command that takes info
59 uint _IOW(T)(char g, int n)
60 {
61 return _IOC!(T)(IOC_IN, cast(uint)g, cast(uint)n, T.sizeof);
62 }
63 // encode a command that takes info and returns info
64 uint _IOWR(T)(char g, int n)
65 {
66 return _IOC!(T)(IOC_INOUT, cast(uint)g, cast(uint)n, T.sizeof);
67 }
68 }
69 else version (FreeBSD)
70 {
71 /* FreeBSD ioctl's encode the command in the lower 16-bits
72 * and the size of any in/out parameters in the lower 13 bits of the upper
73 * 16-bits of a 32 bit unsigned integer. The high 3 bits of the upper
74 * 16-bits encode the in/out status of the parameter.
75 */
76 enum uint IOCPARM_SHIFT = 13; // number of bits for ioctl size
77 enum uint IOCPARM_MASK = ((1 << IOCPARM_SHIFT) - 1); // parameter length mask
78 uint IOCPARM_LEN(uint x) // to extract the encoded parameter length
79 {
80 return ((x >> 16) & IOCPARM_MASK);
81 }
82 uint IOCBASECMD(uint x) // to extract the encoded command
83 {
84 return (x & ~(IOCPARM_MASK << 16));
85 }
86 uint IOCGROUP(uint x) // to extract the encoded group
87 {
88 return ((x >> 8) & 0xff);
89 }
90
91 enum uint IOCPARM_MAX = (1 << IOCPARM_SHIFT); // max size of ioctl args
92
93 enum uint IOC_VOID = 0x20000000; // no parameters
94 enum uint IOC_OUT = 0x40000000; // copy parameters back
95 enum uint IOC_IN = 0x80000000; // copy parameters into
96 enum uint IOC_INOUT = (IOC_IN | IOC_OUT);
97 enum uint IOC_DIRMASK = (IOC_VOID|IOC_OUT|IOC_IN);
98
99 // encode the ioctl info into 32 bits
100 uint _IOC(uint inorout, uint group, uint num, size_t len)
101 {
102 return (inorout | ((len & IOCPARM_MASK) << 16) | (group << 8) | num);
103 }
104
105 // encode a command with no parameters
106 uint _IO(char g, int n)
107 {
108 return _IOC(IOC_VOID, cast(uint)g, cast(uint)n, cast(size_t)0);
109 }
110 uint _IOWINT(char g, int n)
111 {
112 return _IOC(IOC_VOID, cast(uint)g, cast(uint)n, int.sizeof);
113 }
114 // encode a command that returns info
115 uint _IOR(T)(char g, int n)
116 {
117 return _IOC(IOC_OUT, cast(uint)g, cast(uint)n, T.sizeof);
118 }
119 // encode a command that takes info
120 uint _IOW(T)(char g, int n)
121 {
122 return _IOC(IOC_IN, cast(uint)g, cast(uint)n, T.sizeof);
123 }
124 // encode a command that takes info and returns info
125 uint _IOWR(T)(char g, int n)
126 {
127 return _IOC(IOC_INOUT, cast(uint)g, cast(uint)n, T.sizeof);
128 }
129 }