1 /* SPDX-License-Identifier: GPL-2.0+ OR BSD-4-Clause OR BSD-3-Clause OR BSD-2-Clause */
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 PPP_TERMIOS_LINUX_H
10 #define PPP_TERMIOS_LINUX_H
15 * We need to use raw TCGETS2/TCSETS2 or TCGETS/TCSETS ioctls with the BOTHER
16 * flag in struct termios2/termios, defined in Linux headers <asm/ioctls.h>
17 * and <asm/termbits.h>. Since these headers conflict with glibc's header file
18 * <termios.h>, it is not possible to use libc's termios functions and we need
19 * to reimplement them via ioctl() calls.
21 * An arbitrary baudrate is supported when the macro BOTHER is defined. The
22 * baudrate value itself is then stored into the c_ospeed and c_ispeed members.
23 * If ioctls TCGETS2/TCSETS2 are defined and supported then these fields are
24 * present in struct termios2, otherwise these fields are present in struct
27 * Note that the Bnnn constants from <termios.h> need not be compatible with
28 * Bnnn constants from <asm/termbits.h>.
32 #include <sys/ioctl.h>
33 #include <sys/types.h>
34 #include <asm/ioctls.h>
35 #include <asm/termbits.h>
37 #if defined(BOTHER) && defined(TCGETS2)
38 #define termios termios2
41 static inline int tcgetattr(int fd, struct termios *t)
43 #if defined(BOTHER) && defined(TCGETS2)
44 return ioctl(fd, TCGETS2, t);
46 return ioctl(fd, TCGETS, t);
50 static inline int tcsetattr(int fd, int a, const struct termios *t)
55 #if defined(BOTHER) && defined(TCGETS2)
81 return ioctl(fd, cmd, t);
84 static inline int tcdrain(int fd)
86 return ioctl(fd, TCSBRK, 1);
89 static inline int tcflush(int fd, int q)
91 return ioctl(fd, TCFLSH, q);
94 static inline int tcsendbreak(int fd, int d)
97 return ioctl(fd, TCSBRKP, d);
99 return ioctl(fd, TCSBRK, 0);
103 static inline int tcflow(int fd, int a)
105 return ioctl(fd, TCXONC, a);
108 static inline pid_t tcgetsid(int fd)
112 if (ioctl(fd, TIOCGSID, &sid) < 0)
118 static inline speed_t cfgetospeed(const struct termios *t)
120 return t->c_cflag & CBAUD;
123 static inline int cfsetospeed(struct termios *t, speed_t s)
130 t->c_cflag &= ~CBAUD;
137 static inline speed_t cfgetispeed(const struct termios *t)
139 speed_t s = (t->c_cflag >> IBSHIFT) & CBAUD;
142 return cfgetospeed(t);
147 static inline int cfsetispeed(struct termios *t, speed_t s)
157 t->c_cflag &= ~(CBAUD << IBSHIFT);
158 t->c_cflag |= s << IBSHIFT;
163 static inline speed_t cfgetispeed(const struct termios *t)
165 return cfgetospeed(t);
168 static inline int cfsetispeed(struct termios *t, speed_t s)
170 return cfsetospeed(t, s);
172 #endif /* !IBSHIFT */
174 static inline int cfsetspeed(struct termios *t, speed_t s)
176 if (cfsetospeed(t, s))
179 if (cfsetispeed(t, s))
186 static void cfmakeraw(struct termios *t)
188 t->c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR |
190 t->c_oflag &= ~OPOST;
191 t->c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
192 t->c_cflag &= ~(CSIZE | PARENB);
196 #endif /* PPP_TERMIOS_LINUX_H */