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)
94 return ioctl(fd, TCSBRKP, d);
96 return ioctl(fd, TCSBRK, 0);
100 static inline int tcflow(int fd, int a)
102 return ioctl(fd, TCXONC, a);
105 static inline pid_t tcgetsid(int fd)
109 if (ioctl(fd, TIOCGSID, &sid) < 0)
115 static inline speed_t cfgetospeed(const struct termios *t)
117 return t->c_cflag & CBAUD;
120 static inline int cfsetospeed(struct termios *t, speed_t s)
127 t->c_cflag &= ~CBAUD;
134 static inline speed_t cfgetispeed(const struct termios *t)
136 speed_t s = (t->c_cflag >> IBSHIFT) & CBAUD;
139 return cfgetospeed(t);
144 static inline int cfsetispeed(struct termios *t, speed_t s)
154 t->c_cflag &= ~(CBAUD << IBSHIFT);
155 t->c_cflag |= s << IBSHIFT;
160 static inline speed_t cfgetispeed(const struct termios *t)
162 return cfgetospeed(t);
165 static inline int cfsetispeed(struct termios *t, speed_t s)
167 return cfsetospeed(t, s);
169 #endif /* !IBSHIFT */
171 static inline int cfsetspeed(struct termios *t, speed_t s)
173 if (cfsetospeed(t, s))
176 if (cfsetispeed(t, s))
183 static void cfmakeraw(struct termios *t)
185 t->c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR |
187 t->c_oflag &= ~OPOST;
188 t->c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
189 t->c_cflag &= ~(CSIZE | PARENB);
193 #endif /* _TERMIOS_LINUX_H_ */