blob: b8c347bdd625fc8aa9e1b4ccaf776056fa18063a [file] [log] [blame]
Sylvain Munautfe28ded2011-09-02 22:18:24 +02001/*
2 * serial.c
3 *
4 * Utility functions to deal with serial ports
5 *
6 * Copyright (C) 2011 Sylvain Munaut <tnt@246tNt.com>
7 *
8 * All Rights Reserved
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 */
24
25/*! \addtogroup serial
26 * @{
Neels Hofmeyr87e45502017-06-20 00:17:59 +020027 * Osmocom serial port helpers
Sylvain Munautfe28ded2011-09-02 22:18:24 +020028 */
29
Harald Welte96e2a002017-06-12 21:44:18 +020030/*! \file serial.c */
Sylvain Munautfe28ded2011-09-02 22:18:24 +020031
32#include <errno.h>
33#include <fcntl.h>
34#include <stdio.h>
35#include <termios.h>
36#include <unistd.h>
37#include <sys/ioctl.h>
38#include <sys/types.h>
39#include <sys/stat.h>
Sylvain Munaut96311842011-09-28 09:10:32 +020040#ifdef __linux__
Sylvain Munautfe28ded2011-09-02 22:18:24 +020041#include <linux/serial.h>
Sylvain Munaut96311842011-09-28 09:10:32 +020042#endif
Sylvain Munautfe28ded2011-09-02 22:18:24 +020043
44#include <osmocom/core/serial.h>
45
46
47#if 0
48# define dbg_perror(x) perror(x)
49#else
50# define dbg_perror(x) do { } while (0)
51#endif
52
Neels Hofmeyr87e45502017-06-20 00:17:59 +020053/*! Open serial device and does base init
Sylvain Munautfe28ded2011-09-02 22:18:24 +020054 * \param[in] dev Path to the device node to open
55 * \param[in] baudrate Baudrate constant (speed_t: B9600, B...)
56 * \returns >=0 file descriptor in case of success or negative errno.
57 */
58int
59osmo_serial_init(const char *dev, speed_t baudrate)
60{
Harald Welte1514f342016-12-24 17:59:36 +010061 int rc, fd=-1, v24, flags;
Sylvain Munautfe28ded2011-09-02 22:18:24 +020062 struct termios tio;
63
Harald Welte1db37822016-12-23 22:49:39 +010064 /* Use nonblock as the device might block otherwise */
65 fd = open(dev, O_RDWR | O_NOCTTY | O_SYNC | O_NONBLOCK);
Sylvain Munautfe28ded2011-09-02 22:18:24 +020066 if (fd < 0) {
67 dbg_perror("open");
68 return -errno;
69 }
70
Harald Welte1db37822016-12-23 22:49:39 +010071 /* now put it into blcoking mode */
72 flags = fcntl(fd, F_GETFL, 0);
73 if (flags < 0) {
74 dbg_perror("fcntl get flags");
Harald Welted2510452016-12-24 17:58:13 +010075 rc = -errno;
76 goto error;
Harald Welte1db37822016-12-23 22:49:39 +010077 }
78
79 flags &= ~O_NONBLOCK;
80 rc = fcntl(fd, F_SETFL, flags);
81 if (rc != 0) {
82 dbg_perror("fcntl set flags");
Harald Welted2510452016-12-24 17:58:13 +010083 rc = -errno;
84 goto error;
Harald Welte1db37822016-12-23 22:49:39 +010085 }
86
Sylvain Munautfe28ded2011-09-02 22:18:24 +020087 /* Configure serial interface */
88 rc = tcgetattr(fd, &tio);
89 if (rc < 0) {
90 dbg_perror("tcgetattr()");
91 rc = -errno;
92 goto error;
93 }
94
95 cfsetispeed(&tio, baudrate);
96 cfsetospeed(&tio, baudrate);
97
98 tio.c_cflag &= ~(PARENB | CSTOPB | CSIZE | CRTSCTS);
99 tio.c_cflag |= (CREAD | CLOCAL | CS8);
100 tio.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
101 tio.c_iflag |= (INPCK | ISTRIP);
102 tio.c_iflag &= ~(ISTRIP | IXON | IXOFF | IGNBRK | INLCR | ICRNL | IGNCR);
103 tio.c_oflag &= ~(OPOST | ONLCR);
104
105 rc = tcsetattr(fd, TCSANOW, &tio);
106 if (rc < 0) {
107 dbg_perror("tcsetattr()");
108 rc = -errno;
109 goto error;
110 }
111
112 /* Set ready to read/write */
113 v24 = TIOCM_DTR | TIOCM_RTS;
114 rc = ioctl(fd, TIOCMBIS, &v24);
115 if (rc < 0) {
116 dbg_perror("ioctl(TIOCMBIS)");
Harald Weltec68ce3b2016-12-23 22:59:27 +0100117 /* some serial porst don't support this, so let's not
118 * return an error here */
Sylvain Munautfe28ded2011-09-02 22:18:24 +0200119 }
120
121 return fd;
122
123error:
Harald Welte1514f342016-12-24 17:59:36 +0100124 if (fd >= 0)
Sylvain Munautfe28ded2011-09-02 22:18:24 +0200125 close(fd);
126 return rc;
127}
128
129static int
130_osmo_serial_set_baudrate(int fd, speed_t baudrate)
131{
132 int rc;
133 struct termios tio;
134
135 rc = tcgetattr(fd, &tio);
136 if (rc < 0) {
137 dbg_perror("tcgetattr()");
138 return -errno;
139 }
140 cfsetispeed(&tio, baudrate);
141 cfsetospeed(&tio, baudrate);
142
143 rc = tcsetattr(fd, TCSANOW, &tio);
144 if (rc < 0) {
145 dbg_perror("tcgetattr()");
146 return -errno;
147 }
148
149 return 0;
150}
151
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200152/*! Change current baudrate
Sylvain Munautfe28ded2011-09-02 22:18:24 +0200153 * \param[in] fd File descriptor of the open device
154 * \param[in] baudrate Baudrate constant (speed_t: B9600, B...)
155 * \returns 0 for success or negative errno.
156 */
157int
158osmo_serial_set_baudrate(int fd, speed_t baudrate)
159{
160 osmo_serial_clear_custom_baudrate(fd);
161 return _osmo_serial_set_baudrate(fd, baudrate);
162}
163
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200164/*! Change current baudrate to a custom one using OS specific method
Sylvain Munautfe28ded2011-09-02 22:18:24 +0200165 * \param[in] fd File descriptor of the open device
166 * \param[in] baudrate Baudrate as integer
167 * \returns 0 for success or negative errno.
168 *
169 * This function might not work on all OS or with all type of serial adapters
170 */
171int
172osmo_serial_set_custom_baudrate(int fd, int baudrate)
173{
Sylvain Munaut96311842011-09-28 09:10:32 +0200174#ifdef __linux__
Sylvain Munautfe28ded2011-09-02 22:18:24 +0200175 int rc;
176 struct serial_struct ser_info;
177
178 rc = ioctl(fd, TIOCGSERIAL, &ser_info);
179 if (rc < 0) {
180 dbg_perror("ioctl(TIOCGSERIAL)");
181 return -errno;
182 }
183
184 ser_info.flags = ASYNC_SPD_CUST | ASYNC_LOW_LATENCY;
185 ser_info.custom_divisor = ser_info.baud_base / baudrate;
186
187 rc = ioctl(fd, TIOCSSERIAL, &ser_info);
188 if (rc < 0) {
189 dbg_perror("ioctl(TIOCSSERIAL)");
190 return -errno;
191 }
192
193 return _osmo_serial_set_baudrate(fd, B38400); /* 38400 is a kind of magic ... */
Sylvain Munaut96311842011-09-28 09:10:32 +0200194#elif defined(__APPLE__)
195#ifndef IOSSIOSPEED
196#define IOSSIOSPEED _IOW('T', 2, speed_t)
197#endif
198 int rc;
199
200 unsigned int speed = baudrate;
201 rc = ioctl(fd, IOSSIOSPEED, &speed);
202 if (rc < 0) {
Sylvain Munaute40549a2011-09-28 10:55:22 +0200203 dbg_perror("ioctl(IOSSIOSPEED)");
Sylvain Munaut96311842011-09-28 09:10:32 +0200204 return -errno;
205 }
206 return 0;
207#else
208#warning osmo_serial_set_custom_baudrate: unsupported platform
209 return 0;
210#endif
Sylvain Munautfe28ded2011-09-02 22:18:24 +0200211}
212
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200213/*! Clear any custom baudrate
Sylvain Munautfe28ded2011-09-02 22:18:24 +0200214 * \param[in] fd File descriptor of the open device
215 * \returns 0 for success or negative errno.
216 *
217 * This function might not work on all OS or with all type of serial adapters
218 */
219int
220osmo_serial_clear_custom_baudrate(int fd)
221{
Sylvain Munaut96311842011-09-28 09:10:32 +0200222#ifdef __linux__
Sylvain Munaut31d3de92011-11-20 08:55:11 +0100223 int rc;
Sylvain Munautfe28ded2011-09-02 22:18:24 +0200224 struct serial_struct ser_info;
225
226 rc = ioctl(fd, TIOCGSERIAL, &ser_info);
227 if (rc < 0) {
228 dbg_perror("ioctl(TIOCGSERIAL)");
229 return -errno;
230 }
231
232 ser_info.flags = ASYNC_LOW_LATENCY;
233 ser_info.custom_divisor = 0;
234
235 rc = ioctl(fd, TIOCSSERIAL, &ser_info);
236 if (rc < 0) {
237 dbg_perror("ioctl(TIOCSSERIAL)");
238 return -errno;
239 }
Sylvain Munaut96311842011-09-28 09:10:32 +0200240#endif
Sylvain Munautfe28ded2011-09-02 22:18:24 +0200241 return 0;
242}
243
Sylvain Munautdca7d2c2012-04-18 21:53:23 +0200244/*! @} */