]> sigrok.org Git - libserialport.git/blob - linux_termios.c
Document this madness.
[libserialport.git] / linux_termios.c
1 /*
2  * This file is part of the libserialport project.
3  *
4  * Copyright (C) 2013 Martin Ling <martin-libserialport@earth.li>
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License as
8  * published by the Free Software Foundation, either version 3 of the
9  * License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 /*
21  * At the time of writing, glibc does not support the Linux kernel interfaces
22  * for setting non-standard baud rates and flow control. We therefore have to
23  * prepare the correct ioctls ourselves, for which we need the declarations in
24  * linux/termios.h.
25  *
26  * We can't include linux/termios.h in serialport.c however, because its
27  * contents conflict with the termios.h provided by glibc. So this file exists
28  * to isolate the bits of code which use the kernel termios declarations.
29  *
30  * The details vary by architecture. Some architectures have c_ispeed/c_ospeed
31  * in struct termios, accessed with TCSETS/TCGETS. Others have these fields in
32  * struct termios2, accessed with TCSETS2/TCGETS2. Some architectures have the
33  * TCSETX/TCGETX ioctls used with struct termiox, others do not.
34  */
35
36 #include <linux/termios.h>
37 #include "linux_termios.h"
38
39 int get_termios_get_ioctl(void)
40 {
41 #ifdef HAVE_TERMIOS2
42         return TCGETS2;
43 #else
44         return TCGETS;
45 #endif
46 }
47
48 int get_termios_set_ioctl(void)
49 {
50 #ifdef HAVE_TERMIOS2
51         return TCSETS2;
52 #else
53         return TCSETS;
54 #endif
55 }
56
57 int get_termios_size(void)
58 {
59 #ifdef HAVE_TERMIOS2
60         return sizeof(struct termios2);
61 #else
62         return sizeof(struct termios);
63 #endif
64 }
65
66 int get_termios_speed(void *data)
67 {
68 #ifdef HAVE_TERMIOS2
69         struct termios2 *term = (struct termios2 *) data;
70 #else
71         struct termios *term = (struct termios *) data;
72 #endif
73         if (term->c_ispeed != term->c_ospeed)
74                 return -1;
75         else
76                 return term->c_ispeed;
77 }
78
79 void set_termios_speed(void *data, int speed)
80 {
81 #ifdef HAVE_TERMIOS2
82         struct termios2 *term = (struct termios2 *) data;
83 #else
84         struct termios *term = (struct termios *) data;
85 #endif
86         term->c_cflag &= ~CBAUD;
87         term->c_cflag |= BOTHER;
88         term->c_ispeed = term->c_ospeed = speed;
89 }
90
91 #ifdef HAVE_TERMIOX
92 int get_termiox_size(void)
93 {
94         return sizeof(struct termiox);
95 }
96
97 int get_termiox_flow(void *data)
98 {
99         struct termiox *termx = (struct termiox *) data;
100
101         int flags = 0;
102         if (termx->x_cflag & RTSXOFF)
103                 flags |= RTS_FLOW;
104         if (termx->x_cflag & CTSXON)
105                 flags |= CTS_FLOW;
106         if (termx->x_cflag & DTRXOFF)
107                 flags |= DTR_FLOW;
108         if (termx->x_cflag & DSRXON)
109                 flags |= DSR_FLOW;
110
111         return flags;
112 }
113
114 void set_termiox_flow(void *data, int flags)
115 {
116         struct termiox *termx = (struct termiox *) data;
117
118         termx->x_cflag &= ~(RTSXOFF | CTSXON | DTRXOFF | DSRXON);
119
120         if (flags & RTS_FLOW)
121                 termx->x_cflag |= RTSXOFF;
122         if (flags & CTS_FLOW)
123                 termx->x_cflag |= CTSXON;
124         if (flags & DTR_FLOW)
125                 termx->x_cflag |= DTRXOFF;
126         if (flags & DSR_FLOW)
127                 termx->x_cflag |= DSRXON;
128 }
129 #endif