]> sigrok.org Git - libserialport.git/blame - linux_termios.c
Build: Include config.h first in all source files
[libserialport.git] / linux_termios.c
CommitLineData
7a6d2196
ML
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
70518e4f
ML
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
1a584c45 36#include <config.h>
546e9ae5 37#include <stdlib.h>
7a6d2196
ML
38#include <linux/termios.h>
39#include "linux_termios.h"
40
546e9ae5 41SP_PRIV unsigned long get_termios_get_ioctl(void)
7a6d2196 42{
f1c916ed 43#ifdef HAVE_STRUCT_TERMIOS2
7a6d2196
ML
44 return TCGETS2;
45#else
46 return TCGETS;
47#endif
48}
49
546e9ae5 50SP_PRIV unsigned long get_termios_set_ioctl(void)
7a6d2196 51{
f1c916ed 52#ifdef HAVE_STRUCT_TERMIOS2
7a6d2196
ML
53 return TCSETS2;
54#else
55 return TCSETS;
56#endif
57}
58
546e9ae5 59SP_PRIV size_t get_termios_size(void)
7a6d2196 60{
f1c916ed 61#ifdef HAVE_STRUCT_TERMIOS2
7a6d2196
ML
62 return sizeof(struct termios2);
63#else
64 return sizeof(struct termios);
65#endif
66}
67
f1c916ed 68#if (defined(HAVE_TERMIOS_SPEED) || defined(HAVE_TERMIOS2_SPEED)) && defined(HAVE_DECL_BOTHER)
970f279a 69SP_PRIV int get_termios_speed(void *data)
7a6d2196 70{
f1c916ed 71#ifdef HAVE_STRUCT_TERMIOS2
7a6d2196
ML
72 struct termios2 *term = (struct termios2 *) data;
73#else
74 struct termios *term = (struct termios *) data;
75#endif
76 if (term->c_ispeed != term->c_ospeed)
77 return -1;
78 else
79 return term->c_ispeed;
80}
81
970f279a 82SP_PRIV void set_termios_speed(void *data, int speed)
7a6d2196 83{
f1c916ed 84#ifdef HAVE_STRUCT_TERMIOS2
7a6d2196
ML
85 struct termios2 *term = (struct termios2 *) data;
86#else
87 struct termios *term = (struct termios *) data;
88#endif
89 term->c_cflag &= ~CBAUD;
90 term->c_cflag |= BOTHER;
91 term->c_ispeed = term->c_ospeed = speed;
92}
5cea279a 93#endif
40978c2b 94
f1c916ed 95#ifdef HAVE_STRUCT_TERMIOX
546e9ae5 96SP_PRIV size_t get_termiox_size(void)
40978c2b
ML
97{
98 return sizeof(struct termiox);
99}
100
970f279a 101SP_PRIV int get_termiox_flow(void *data, int *rts, int *cts, int *dtr, int *dsr)
40978c2b
ML
102{
103 struct termiox *termx = (struct termiox *) data;
40978c2b 104 int flags = 0;
24abdb68 105
bd791fe1
ML
106 *rts = (termx->x_cflag & RTSXOFF);
107 *cts = (termx->x_cflag & CTSXON);
108 *dtr = (termx->x_cflag & DTRXOFF);
109 *dsr = (termx->x_cflag & DSRXON);
40978c2b
ML
110
111 return flags;
112}
113
970f279a 114SP_PRIV void set_termiox_flow(void *data, int rts, int cts, int dtr, int dsr)
40978c2b
ML
115{
116 struct termiox *termx = (struct termiox *) data;
117
118 termx->x_cflag &= ~(RTSXOFF | CTSXON | DTRXOFF | DSRXON);
119
bd791fe1 120 if (rts)
40978c2b 121 termx->x_cflag |= RTSXOFF;
bd791fe1 122 if (cts)
40978c2b 123 termx->x_cflag |= CTSXON;
bd791fe1 124 if (dtr)
40978c2b 125 termx->x_cflag |= DTRXOFF;
bd791fe1 126 if (dsr)
40978c2b
ML
127 termx->x_cflag |= DSRXON;
128}
129#endif