1 /* SPDX-License-Identifier: GPL-2.0+ */
3 * termios fuctions to support arbitrary baudrates (on Linux)
5 * Copyright (c) 2021 Pali Rohár <pali@kernel.org>
6 * Copyright (c) 2021 Marek Behún <marek.behun@nic.cz>
9 #ifndef _TERMIOS_LINUX_H_
10 #define _TERMIOS_LINUX_H_
13 * We need to use raw TCGETS2/TCSETS2 or TCGETS/TCSETS ioctls with the BOTHER
14 * flag in struct termios2/termios, defined in Linux headers <asm/ioctls.h>
15 * (included by <sys/ioctl.h>) and <asm/termbits.h>. Since these headers
16 * conflict with glibc's header file <termios.h>, it is not possible to use
17 * libc's termios functions and we need to reimplement them via ioctl() calls.
19 * An arbitrary baudrate is supported when the macro BOTHER is defined. The
20 * baudrate value itself is then stored into the c_ospeed and c_ispeed members.
21 * If ioctls TCGETS2/TCSETS2 are defined and supported then these fields are
22 * present in struct termios2, otherwise these fields are present in struct
25 * Note that the Bnnn constants from <termios.h> need not be compatible with Bnnn
26 * constants from <asm/termbits.h>.
30 #include <sys/ioctl.h>
31 #include <sys/types.h>
32 #include <asm/termbits.h>
34 #if defined(BOTHER) && defined(TCGETS2)
35 #define termios termios2
38 static inline int tcgetattr(int fd, struct termios *t)
40 #if defined(BOTHER) && defined(TCGETS2)
41 return ioctl(fd, TCGETS2, t);
43 return ioctl(fd, TCGETS, t);
47 static inline int tcsetattr(int fd, int a, const struct termios *t)
52 #if defined(BOTHER) && defined(TCGETS2)
78 return ioctl(fd, cmd, t);
81 static inline int tcdrain(int fd)
83 return ioctl(fd, TCSBRK, 1);
86 static inline int tcflush(int fd, int q)
88 return ioctl(fd, TCFLSH, q);
91 static inline int tcsendbreak(int fd, int d)
93 return ioctl(fd, TCSBRK, d);
96 static inline int tcflow(int fd, int a)
98 return ioctl(fd, TCXONC, a);
101 static inline pid_t tcgetsid(int fd)
105 if (ioctl(fd, TIOCGSID, &sid) < 0)
111 static inline speed_t cfgetospeed(const struct termios *t)
113 return t->c_cflag & CBAUD;
116 static inline int cfsetospeed(struct termios *t, speed_t s)
123 t->c_cflag &= ~CBAUD;
130 static inline speed_t cfgetispeed(const struct termios *t)
132 speed_t s = (t->c_cflag >> IBSHIFT) & CBAUD;
135 return cfgetospeed(t);
140 static inline int cfsetispeed(struct termios *t, speed_t s)
150 t->c_cflag &= ~(CBAUD << IBSHIFT);
151 t->c_cflag |= s << IBSHIFT;
156 static inline speed_t cfgetispeed(const struct termios *t)
158 return cfgetospeed(t);
161 static inline int cfsetispeed(struct termios *t, speed_t s)
163 return cfsetospeed(t, s);
165 #endif /* !IBSHIFT */
167 static inline int cfsetspeed(struct termios *t, speed_t s)
169 if (cfsetospeed(t, s))
172 if (cfsetispeed(t, s))
179 static void cfmakeraw(struct termios *t)
181 t->c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR |
183 t->c_oflag &= ~OPOST;
184 t->c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
185 t->c_cflag &= ~(CSIZE | PARENB);
189 #endif /* _TERMIOS_LINUX_H_ */