1 /**
2  * D header file for POSIX.
3  *
4  * Copyright: Copyright Alex Rønne Petersen 2011 - 2012.
5  * License:   $(HTTP www.boost.org/LICENSE_1_0.txt, Boost License 1.0).
6  * Authors:   Alex Rønne Petersen
7  * Standards: The Open Group Base Specifications Issue 6, IEEE Std 1003.1, 2004 Edition
8  */
9 
10 /*          Copyright Alex Rønne Petersen 2011 - 2012.
11  * Distributed under the Boost Software License, Version 1.0.
12  *    (See accompanying file LICENSE or copy at
13  *          http://www.boost.org/LICENSE_1_0.txt)
14  */
15 module core.sys.posix.sys.ioctl;
16 
17 import core.stdc.config;
18 public import core.sys.posix.sys.ttycom;
19 
20 version (OSX)
21     version = Darwin;
22 else version (iOS)
23     version = Darwin;
24 else version (TVOS)
25     version = Darwin;
26 else version (WatchOS)
27     version = Darwin;
28 
29 version (Posix):
30 
31 extern (C) nothrow @nogc:
32 
33 version (linux)
34 {
35     import core.sys.posix.termios; // tcflag_t, speed_t, cc_t
36 
37     enum _IOC_NRBITS = 8;
38     enum _IOC_TYPEBITS = 8;
39     enum _IOC_SIZEBITS = 14;
40     enum _IOC_DIRBITS = 2;
41 
42     enum _IOC_NRMASK = (1 << _IOC_NRBITS) - 1;
43     enum _IOC_TYPEMASK = (1 << _IOC_TYPEBITS) - 1;
44     enum _IOC_SIZEMASK = (1 << _IOC_SIZEBITS) - 1;
45     enum _IOC_DIRMASK = (1 << _IOC_DIRBITS) - 1;
46 
47     enum _IOC_NRSHIFT = 0;
48     enum _IOC_TYPESHIFT = _IOC_NRSHIFT + _IOC_NRBITS;
49     enum _IOC_SIZESHIFT = _IOC_TYPESHIFT + _IOC_TYPEBITS;
50     enum _IOC_DIRSHIFT = _IOC_SIZESHIFT + _IOC_SIZEBITS;
51 
52     enum _IOC_NONE = 0;
53     enum _IOC_WRITE = 1;
54     enum _IOC_READ = 2;
55 
56     extern (D) int _IOC(T = typeof(null))(int dir, int type, int nr)
57     {
58         return (dir << _IOC_DIRSHIFT) |
59                (type << _IOC_TYPESHIFT) |
60                (nr << _IOC_NRSHIFT) |
61                (is(T == typeof(null)) ? 0 : T.sizeof << _IOC_SIZESHIFT);
62     }
63 
64     extern (D) int _IO(int type, int nr)
65     {
66         return _IOC(_IOC_NONE, type, nr);
67     }
68 
69     extern (D) int _IOR(T)(int type, int nr)
70     {
71         return _IOC!T(_IOC_READ, type, nr);
72     }
73 
74     extern (D) int _IOW(T)(int type, int nr)
75     {
76         return _IOC!T(_IOC_WRITE, type, nr);
77     }
78 
79     extern (D) int _IOWR(T)(int type, int nr)
80     {
81         return _IOC!T(_IOC_READ | _IOC_WRITE, type, nr);
82     }
83 
84     extern (D) int _IOC_DIR(int nr)
85     {
86         return (nr >> _IOC_DIRSHIFT) & _IOC_DIRMASK;
87     }
88 
89     extern (D) int _IOC_TYPE(int nr)
90     {
91         return (nr >> _IOC_TYPESHIFT) & _IOC_TYPEMASK;
92     }
93 
94     extern (D) int _IOC_NR(int nr)
95     {
96         return (nr >> _IOC_NRSHIFT) & _IOC_NRMASK;
97     }
98 
99     extern (D) int _IOC_SIZE(int nr)
100     {
101         return (nr >> _IOC_SIZESHIFT) & _IOC_SIZEMASK;
102     }
103 
104     enum IOC_IN = _IOC_WRITE << _IOC_DIRSHIFT;
105     enum IOC_OUT = _IOC_READ << _IOC_DIRSHIFT;
106     enum IOC_INOUT = (_IOC_READ | _IOC_WRITE) << _IOC_DIRSHIFT;
107     enum IOCSIZE_MASK = _IOC_SIZEMASK << _IOC_DIRSHIFT;
108     enum IOCSIZE_SHIFT = _IOC_SIZESHIFT;
109 
110     enum NCCS = 19;
111 
112     struct termios2
113     {
114         tcflag_t c_iflag;
115         tcflag_t c_oflag;
116         tcflag_t c_cflag;
117         tcflag_t c_lflag;
118         cc_t c_line;
119         cc_t[NCCS] c_cc;
120         speed_t c_ispeed;
121         speed_t c_ospeed;
122     }
123 
124     struct winsize
125     {
126         ushort ws_row;
127         ushort ws_col;
128         ushort ws_xpixel;
129         ushort ws_ypixel;
130     }
131 
132     enum NCC = 8;
133 
134     struct termio
135     {
136         ushort c_iflag;
137         ushort c_oflag;
138         ushort c_cflag;
139         ushort c_lflag;
140         ubyte c_line;
141         ubyte[NCC] c_cc;
142     }
143 
144     enum TIOCM_LE = 0x001;
145     enum TIOCM_DTR = 0x002;
146     enum TIOCM_RTS = 0x004;
147     enum TIOCM_ST = 0x008;
148     enum TIOCM_SR = 0x010;
149     enum TIOCM_CTS = 0x020;
150     enum TIOCM_CAR = 0x040;
151     enum TIOCM_RNG = 0x080;
152     enum TIOCM_DSR = 0x100;
153     enum TIOCM_CD = TIOCM_CAR;
154     enum TIOCM_RI = TIOCM_RNG;
155 
156     enum N_TTY = 0;
157     enum N_SLIP = 1;
158     enum N_MOUSE = 2;
159     enum N_PPP = 3;
160     enum N_STRIP = 4;
161     enum N_AX25 = 5;
162     enum N_X25 = 6;
163     enum N_6PACK = 7;
164     enum N_MASC = 8;
165     enum N_R3964 = 9;
166     enum N_PROFIBUS_FDL = 10;
167     enum N_IRDA = 11;
168     enum N_SMSBLOCK = 12;
169     enum N_HDLC = 13;
170     enum N_SYNC_PPP = 14;
171     enum N_HCI = 15;
172 
173     enum TCGETS = 0x5401;
174     enum TCSETS = 0x5402;
175     enum TCSETSW = 0x5403;
176     enum TCSETSF = 0x5404;
177     enum TCGETA = 0x5405;
178     enum TCSETA = 0x5406;
179     enum TCSETAW = 0x5407;
180     enum TCSETAF = 0x5408;
181     enum TCSBRK = 0x5409;
182     enum TCXONC = 0x540A;
183     enum TCFLSH = 0x540B;
184     enum TIOCEXCL = 0x540C;
185     enum TIOCNXCL = 0x540D;
186     enum TIOCSCTTY = 0x540E;
187     enum TIOCGPGRP = 0x540F;
188     enum TIOCSPGRP = 0x5410;
189     enum TIOCOUTQ = 0x5411;
190     enum TIOCSTI = 0x5412;
191     enum TIOCGWINSZ = 0x5413;
192     enum TIOCSWINSZ = 0x5414;
193     enum TIOCMGET = 0x5415;
194     enum TIOCMBIS = 0x5416;
195     enum TIOCMBIC = 0x5417;
196     enum TIOCMSET = 0x5418;
197     enum TIOCGSOFTCAR = 0x5419;
198     enum TIOCSSOFTCAR = 0x541A;
199     enum FIONREAD = 0x541B;
200     enum TIOCINQ = FIONREAD;
201     enum TIOCLINUX = 0x541C;
202     enum TIOCCONS = 0x541D;
203     enum TIOCGSERIAL = 0x541E;
204     enum TIOCSSERIAL = 0x541F;
205     enum TIOCPKT = 0x5420;
206     enum FIONBIO = 0x5421;
207     enum TIOCNOTTY = 0x5422;
208     enum TIOCSETD = 0x5423;
209     enum TIOCGETD = 0x5424;
210     enum TCSBRKP = 0x5425;
211     enum TIOCSBRK = 0x5427;
212     enum TIOCCBRK = 0x5428;
213     enum TIOCGSID = 0x5429;
214 
215     enum TCGETS2 = _IOR!termios2('T', 0x2A);
216     enum TCSETS2 = _IOW!termios2('T', 0x2B);
217     enum TCSETSW2 = _IOW!termios2('T', 0x2C);
218     enum TCSETSF2 = _IOW!termios2('T', 0x2D);
219 
220     enum TIOCGRS485 = 0x542E;
221     enum TIOCSRS485 = 0x542F;
222 
223     enum TIOCGPTN   = _IOR!uint('T', 0x30);
224     enum TIOCSPTLCK = _IOW!int('T', 0x31);
225     enum TIOCGDEV   = _IOR!uint('T', 0x32);
226 
227     enum TCGETX = 0x5432;
228     enum TCSETX = 0x5433;
229     enum TCSETXF = 0x5434;
230     enum TCSETXW = 0x5435;
231 
232     enum TIOCSIG = _IOW!int('T', 0x36);
233 
234     enum TIOCVHANGUP = 0x5437;
235 
236     enum FIONCLEX = 0x5450;
237     enum FIOCLEX = 0x5451;
238     enum FIOASYNC = 0x5452;
239     enum TIOCSERCONFIG = 0x5453;
240     enum TIOCSERGWILD = 0x5454;
241     enum TIOCSERSWILD = 0x5455;
242     enum TIOCGLCKTRMIOS = 0x5456;
243     enum TIOCSLCKTRMIOS = 0x5457;
244     enum TIOCSERGSTRUCT = 0x5458;
245     enum TIOCSERGETLSR = 0x5459;
246     enum TIOCSERGETMULTI = 0x545A;
247     enum TIOCSERSETMULTI = 0x545B;
248 
249     enum TIOCMIWAIT = 0x545C;
250     enum TIOCGICOUNT = 0x545D;
251 
252     enum FIOQSIZE = 0x5460;
253 
254     enum TIOCPKT_DATA = 0;
255     enum TIOCPKT_FLUSHREAD = 1;
256     enum TIOCPKT_FLUSHWRITE = 2;
257     enum TIOCPKT_STOP = 4;
258     enum TIOCPKT_START = 8;
259     enum TIOCPKT_NOSTOP = 16;
260     enum TIOCPKT_DOSTOP = 32;
261     enum TIOCPKT_IOCTL = 64;
262 
263     enum TIOCSER_TEMT = 0x01;
264 
265     enum SIOCADDRT = 0x890B;
266     enum SIOCDELRT = 0x890C;
267     enum SIOCRTMSG = 0x890D;
268 
269     enum SIOCGIFNAME = 0x8910;
270     enum SIOCSIFLINK = 0x8911;
271     enum SIOCGIFCONF = 0x8912;
272     enum SIOCGIFFLAGS = 0x8913;
273     enum SIOCSIFFLAGS = 0x8914;
274     enum SIOCGIFADDR = 0x8915;
275     enum SIOCSIFADDR = 0x8916;
276     enum SIOCGIFDSTADDR = 0x8917;
277     enum SIOCSIFDSTADDR = 0x8918;
278     enum SIOCGIFBRDADDR = 0x8919;
279     enum SIOCSIFBRDADDR = 0x891a;
280     enum SIOCGIFNETMASK = 0x891b;
281     enum SIOCSIFNETMASK = 0x891c;
282     enum SIOCGIFMETRIC = 0x891d;
283     enum SIOCSIFMETRIC = 0x891e;
284     enum SIOCGIFMEM = 0x891f;
285     enum SIOCSIFMEM = 0x8920;
286     enum SIOCGIFMTU = 0x8921;
287     enum SIOCSIFMTU = 0x8922;
288     enum SIOCSIFNAME = 0x8923;
289     enum SIOCSIFHWADDR = 0x8924;
290     enum SIOCGIFENCAP = 0x8925;
291     enum SIOCSIFENCAP = 0x8926;
292     enum SIOCGIFHWADDR = 0x8927;
293     enum SIOCGIFSLAVE = 0x8929;
294     enum SIOCSIFSLAVE = 0x8930;
295     enum SIOCADDMULTI = 0x8931;
296     enum SIOCDELMULTI = 0x8932;
297     enum SIOCGIFINDEX = 0x8933;
298     enum SIOGIFINDEX = SIOCGIFINDEX;
299     enum SIOCSIFPFLAGS = 0x8934;
300     enum SIOCGIFPFLAGS = 0x8935;
301     enum SIOCDIFADDR = 0x8936;
302     enum SIOCSIFHWBROADCAST = 0x8937;
303     enum SIOCGIFCOUNT = 0x8938;
304 
305     enum SIOCGIFBR = 0x8940;
306     enum SIOCSIFBR = 0x8941;
307 
308     enum SIOCGIFTXQLEN = 0x8942;
309     enum SIOCSIFTXQLEN = 0x8943;
310 
311     enum SIOCDARP = 0x8953;
312     enum SIOCGARP = 0x8954;
313     enum SIOCSARP = 0x8955;
314 
315     enum SIOCDRARP = 0x8960;
316     enum SIOCGRARP = 0x8961;
317     enum SIOCSRARP = 0x8962;
318 
319     enum SIOCGIFMAP = 0x8970;
320     enum SIOCSIFMAP = 0x8971;
321 
322     enum SIOCADDDLCI = 0x8980;
323     enum SIOCDELDLCI = 0x8981;
324 
325     enum SIOCDEVPRIVATE = 0x89F0;
326 
327     enum SIOCPROTOPRIVATE = 0x89E0;
328 }
329 else version (Darwin)
330 {
331     import core.sys.posix.termios; // termios
332     import core.sys.posix.sys.time; // timeval
333 
334     public import core.sys.posix.sys.ttycom; // Terminal related ioctls
335 
336     struct ttysize
337     {
338         ushort ts_lines;
339         ushort ts_cols;
340         ushort ts_xxx;
341         ushort ts_yyy;
342     }
343 
344     enum uint TIOCGSIZE = TIOCGWINSZ;
345     enum uint TIOCSSIZE = TIOCSWINSZ;
346 
347     public import core.sys.posix.sys.filio; // File related ioctls
348 }
349 else version (FreeBSD)
350 {
351     struct fiodgname_arg
352     {
353         int len;
354         void* buf;
355     }
356 }
357 else version (NetBSD)
358 {
359     struct winsize
360     {
361         ushort ws_row;
362         ushort ws_col;
363         ushort ws_xpixel;
364         ushort ws_ypixel;
365     }
366 }
367 else version (OpenBSD)
368 {
369     import core.sys.posix.termios; // termios
370     import core.sys.posix.sys.time; // timeval
371 
372     public import core.sys.posix.sys.ttycom; // Terminal related ioctls
373 
374     struct winsize
375     {
376         ushort ws_row;
377         ushort ws_col;
378         ushort ws_xpixel;
379         ushort ws_ypixel;
380     }
381 
382     public import core.sys.posix.sys.filio; // File related ioctls
383 }
384 else version (DragonFlyBSD)
385 {
386     struct fiodgname_arg
387     {
388         int len;
389         void* buf;
390     }
391 
392     struct winsize
393     {
394         ushort ws_row;
395         ushort ws_col;
396         ushort ws_xpixel;
397         ushort ws_ypixel;
398     }
399 }
400 else version (Solaris)
401 {
402 }
403 else
404 {
405     static assert(false, "Unsupported platform");
406 }
407 
408 ///
409 version (CRuntime_Glibc)
410 {
411     int ioctl(int __fd, c_ulong __request, ...);
412 }
413 else version (Darwin)
414 {
415     int ioctl(int fildes, c_ulong request, ...);
416 }
417 else version (FreeBSD)
418 {
419     int ioctl(int, c_ulong, ...);
420 }
421 else version (NetBSD)
422 {
423     int ioctl(int, c_ulong, ...);
424 }
425 else version (OpenBSD)
426 {
427     int ioctl(int, c_ulong, ...);
428 }
429 else version (DragonFlyBSD)
430 {
431     int ioctl(int, c_ulong, ...);
432 }
433 else version (Solaris)
434 {
435     int ioctl(int fildes, int request, ...);
436 }
437 else version (CRuntime_Bionic)
438 {
439     int ioctl(int, int, ...);
440 }
441 else version (CRuntime_Musl)
442 {
443     int ioctl(int, int, ...);
444 }
445 else version (CRuntime_UClibc)
446 {
447     int ioctl(int __fd, c_ulong __request, ...);
448 }
449 else
450 {
451     static assert(false, "Unsupported platform");
452 }