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 <kabel@kernel.org>
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/ioctls.h>
33 #include <asm/termbits.h>
35 #if defined(BOTHER) && defined(TCGETS2)
36 #define termios termios2
39 static inline int tcgetattr(int fd, struct termios *t)
41 #if defined(BOTHER) && defined(TCGETS2)
42 return ioctl(fd, TCGETS2, t);
44 return ioctl(fd, TCGETS, t);
48 static inline int tcsetattr(int fd, int a, const struct termios *t)
53 #if defined(BOTHER) && defined(TCGETS2)
79 return ioctl(fd, cmd, t);
82 static inline int tcdrain(int fd)
84 return ioctl(fd, TCSBRK, 1);
87 static inline int tcflush(int fd, int q)
89 return ioctl(fd, TCFLSH, q);
92 static inline int tcsendbreak(int fd, int d)
95 return ioctl(fd, TCSBRKP, d);
97 return ioctl(fd, TCSBRK, 0);
101 static inline int tcflow(int fd, int a)
103 return ioctl(fd, TCXONC, a);
106 static inline pid_t tcgetsid(int fd)
110 if (ioctl(fd, TIOCGSID, &sid) < 0)
116 static inline speed_t cfgetospeed(const struct termios *t)
118 return t->c_cflag & CBAUD;
121 static inline int cfsetospeed(struct termios *t, speed_t s)
128 t->c_cflag &= ~CBAUD;
135 static inline speed_t cfgetispeed(const struct termios *t)
137 speed_t s = (t->c_cflag >> IBSHIFT) & CBAUD;
140 return cfgetospeed(t);
145 static inline int cfsetispeed(struct termios *t, speed_t s)
155 t->c_cflag &= ~(CBAUD << IBSHIFT);
156 t->c_cflag |= s << IBSHIFT;
161 static inline speed_t cfgetispeed(const struct termios *t)
163 return cfgetospeed(t);
166 static inline int cfsetispeed(struct termios *t, speed_t s)
168 return cfsetospeed(t, s);
170 #endif /* !IBSHIFT */
172 static inline int cfsetspeed(struct termios *t, speed_t s)
174 if (cfsetospeed(t, s))
177 if (cfsetispeed(t, s))
184 static void cfmakeraw(struct termios *t)
186 t->c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR |
188 t->c_oflag &= ~OPOST;
189 t->c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
190 t->c_cflag &= ~(CSIZE | PARENB);
194 #endif /* _TERMIOS_LINUX_H_ */