blob: d73989b625a28ae3ac75520e3d739145967c6f3a [file] [log] [blame]
Pali Rohár93b55632021-09-24 23:07:06 +02001/* SPDX-License-Identifier: GPL-2.0+ */
2/*
3 * termios fuctions to support arbitrary baudrates (on Linux)
4 *
5 * Copyright (c) 2021 Pali Rohár <pali@kernel.org>
6 * Copyright (c) 2021 Marek Behún <marek.behun@nic.cz>
7 */
8
9#ifndef _TERMIOS_LINUX_H_
10#define _TERMIOS_LINUX_H_
11
12/*
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.
18 *
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
23 * termios.
24 *
25 * Note that the Bnnn constants from <termios.h> need not be compatible with Bnnn
26 * constants from <asm/termbits.h>.
27 */
28
29#include <errno.h>
30#include <sys/ioctl.h>
31#include <sys/types.h>
32#include <asm/termbits.h>
33
34#if defined(BOTHER) && defined(TCGETS2)
35#define termios termios2
36#endif
37
38static inline int tcgetattr(int fd, struct termios *t)
39{
40#if defined(BOTHER) && defined(TCGETS2)
41 return ioctl(fd, TCGETS2, t);
42#else
43 return ioctl(fd, TCGETS, t);
44#endif
45}
46
47static inline int tcsetattr(int fd, int a, const struct termios *t)
48{
49 int cmd;
50
51 switch (a) {
52#if defined(BOTHER) && defined(TCGETS2)
53 case TCSANOW:
54 cmd = TCSETS2;
55 break;
56 case TCSADRAIN:
57 cmd = TCSETSW2;
58 break;
59 case TCSAFLUSH:
60 cmd = TCSETSF2;
61 break;
62#else
63 case TCSANOW:
64 cmd = TCSETS;
65 break;
66 case TCSADRAIN:
67 cmd = TCSETSW;
68 break;
69 case TCSAFLUSH:
70 cmd = TCSETSF;
71 break;
72#endif
73 default:
74 errno = EINVAL;
75 return -1;
76 }
77
78 return ioctl(fd, cmd, t);
79}
80
81static inline int tcdrain(int fd)
82{
83 return ioctl(fd, TCSBRK, 1);
84}
85
86static inline int tcflush(int fd, int q)
87{
88 return ioctl(fd, TCFLSH, q);
89}
90
91static inline int tcsendbreak(int fd, int d)
92{
93 return ioctl(fd, TCSBRK, d);
94}
95
96static inline int tcflow(int fd, int a)
97{
98 return ioctl(fd, TCXONC, a);
99}
100
101static inline pid_t tcgetsid(int fd)
102{
103 pid_t sid;
104
105 if (ioctl(fd, TIOCGSID, &sid) < 0)
106 return (pid_t)-1;
107
108 return sid;
109}
110
111static inline speed_t cfgetospeed(const struct termios *t)
112{
113 return t->c_cflag & CBAUD;
114}
115
116static inline int cfsetospeed(struct termios *t, speed_t s)
117{
118 if (s & ~CBAUD) {
119 errno = EINVAL;
120 return -1;
121 }
122
123 t->c_cflag &= ~CBAUD;
124 t->c_cflag |= s;
125
126 return 0;
127}
128
129#ifdef IBSHIFT
130static inline speed_t cfgetispeed(const struct termios *t)
131{
132 speed_t s = (t->c_cflag >> IBSHIFT) & CBAUD;
133
134 if (s == B0)
135 return cfgetospeed(t);
136 else
137 return s;
138}
139
140static inline int cfsetispeed(struct termios *t, speed_t s)
141{
142 if (s == 0)
143 s = B0;
144
145 if (s & ~CBAUD) {
146 errno = EINVAL;
147 return -1;
148 }
149
150 t->c_cflag &= ~(CBAUD << IBSHIFT);
151 t->c_cflag |= s << IBSHIFT;
152
153 return 0;
154}
155#else /* !IBSHIFT */
156static inline speed_t cfgetispeed(const struct termios *t)
157{
158 return cfgetospeed(t);
159}
160
161static inline int cfsetispeed(struct termios *t, speed_t s)
162{
163 return cfsetospeed(t, s);
164}
165#endif /* !IBSHIFT */
166
167static inline int cfsetspeed(struct termios *t, speed_t s)
168{
169 if (cfsetospeed(t, s))
170 return -1;
171#ifdef IBSHIFT
172 if (cfsetispeed(t, s))
173 return -1;
174#endif
175
176 return 0;
177}
178
179static void cfmakeraw(struct termios *t)
180{
181 t->c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR |
182 ICRNL | IXON);
183 t->c_oflag &= ~OPOST;
184 t->c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
185 t->c_cflag &= ~(CSIZE | PARENB);
186 t->c_cflag |= CS8;
187}
188
189#endif /* _TERMIOS_LINUX_H_ */