blob: 44032263245a082df09055996aecbded131419c0 [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 * @{
27 */
28
29/*! \file serial.c
Katerina Barone-Adesic28c6a02013-02-15 13:27:59 +010030 * Osmocom serial port helpers
Sylvain Munautfe28ded2011-09-02 22:18:24 +020031 */
32
33#include <errno.h>
34#include <fcntl.h>
35#include <stdio.h>
36#include <termios.h>
37#include <unistd.h>
38#include <sys/ioctl.h>
39#include <sys/types.h>
40#include <sys/stat.h>
Sylvain Munaut96311842011-09-28 09:10:32 +020041#ifdef __linux__
Sylvain Munautfe28ded2011-09-02 22:18:24 +020042#include <linux/serial.h>
Sylvain Munaut96311842011-09-28 09:10:32 +020043#endif
Sylvain Munautfe28ded2011-09-02 22:18:24 +020044
45#include <osmocom/core/serial.h>
46
47
48#if 0
49# define dbg_perror(x) perror(x)
50#else
51# define dbg_perror(x) do { } while (0)
52#endif
53
54/*! \brief Open serial device and does base init
55 * \param[in] dev Path to the device node to open
56 * \param[in] baudrate Baudrate constant (speed_t: B9600, B...)
57 * \returns >=0 file descriptor in case of success or negative errno.
58 */
59int
60osmo_serial_init(const char *dev, speed_t baudrate)
61{
Harald Welte1db37822016-12-23 22:49:39 +010062 int rc, fd=0, v24, flags;
Sylvain Munautfe28ded2011-09-02 22:18:24 +020063 struct termios tio;
64
Harald Welte1db37822016-12-23 22:49:39 +010065 /* Use nonblock as the device might block otherwise */
66 fd = open(dev, O_RDWR | O_NOCTTY | O_SYNC | O_NONBLOCK);
Sylvain Munautfe28ded2011-09-02 22:18:24 +020067 if (fd < 0) {
68 dbg_perror("open");
69 return -errno;
70 }
71
Harald Welte1db37822016-12-23 22:49:39 +010072 /* now put it into blcoking mode */
73 flags = fcntl(fd, F_GETFL, 0);
74 if (flags < 0) {
75 dbg_perror("fcntl get flags");
76 return -1;
77 }
78
79 flags &= ~O_NONBLOCK;
80 rc = fcntl(fd, F_SETFL, flags);
81 if (rc != 0) {
82 dbg_perror("fcntl set flags");
83 return -1;
84 }
85
Sylvain Munautfe28ded2011-09-02 22:18:24 +020086 /* Configure serial interface */
87 rc = tcgetattr(fd, &tio);
88 if (rc < 0) {
89 dbg_perror("tcgetattr()");
90 rc = -errno;
91 goto error;
92 }
93
94 cfsetispeed(&tio, baudrate);
95 cfsetospeed(&tio, baudrate);
96
97 tio.c_cflag &= ~(PARENB | CSTOPB | CSIZE | CRTSCTS);
98 tio.c_cflag |= (CREAD | CLOCAL | CS8);
99 tio.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
100 tio.c_iflag |= (INPCK | ISTRIP);
101 tio.c_iflag &= ~(ISTRIP | IXON | IXOFF | IGNBRK | INLCR | ICRNL | IGNCR);
102 tio.c_oflag &= ~(OPOST | ONLCR);
103
104 rc = tcsetattr(fd, TCSANOW, &tio);
105 if (rc < 0) {
106 dbg_perror("tcsetattr()");
107 rc = -errno;
108 goto error;
109 }
110
111 /* Set ready to read/write */
112 v24 = TIOCM_DTR | TIOCM_RTS;
113 rc = ioctl(fd, TIOCMBIS, &v24);
114 if (rc < 0) {
115 dbg_perror("ioctl(TIOCMBIS)");
116 rc = -errno;
117 goto error;
118 }
119
120 return fd;
121
122error:
123 if (fd)
124 close(fd);
125 return rc;
126}
127
128static int
129_osmo_serial_set_baudrate(int fd, speed_t baudrate)
130{
131 int rc;
132 struct termios tio;
133
134 rc = tcgetattr(fd, &tio);
135 if (rc < 0) {
136 dbg_perror("tcgetattr()");
137 return -errno;
138 }
139 cfsetispeed(&tio, baudrate);
140 cfsetospeed(&tio, baudrate);
141
142 rc = tcsetattr(fd, TCSANOW, &tio);
143 if (rc < 0) {
144 dbg_perror("tcgetattr()");
145 return -errno;
146 }
147
148 return 0;
149}
150
151/*! \brief Change current baudrate
152 * \param[in] fd File descriptor of the open device
153 * \param[in] baudrate Baudrate constant (speed_t: B9600, B...)
154 * \returns 0 for success or negative errno.
155 */
156int
157osmo_serial_set_baudrate(int fd, speed_t baudrate)
158{
159 osmo_serial_clear_custom_baudrate(fd);
160 return _osmo_serial_set_baudrate(fd, baudrate);
161}
162
163/*! \brief Change current baudrate to a custom one using OS specific method
164 * \param[in] fd File descriptor of the open device
165 * \param[in] baudrate Baudrate as integer
166 * \returns 0 for success or negative errno.
167 *
168 * This function might not work on all OS or with all type of serial adapters
169 */
170int
171osmo_serial_set_custom_baudrate(int fd, int baudrate)
172{
Sylvain Munaut96311842011-09-28 09:10:32 +0200173#ifdef __linux__
Sylvain Munautfe28ded2011-09-02 22:18:24 +0200174 int rc;
175 struct serial_struct ser_info;
176
177 rc = ioctl(fd, TIOCGSERIAL, &ser_info);
178 if (rc < 0) {
179 dbg_perror("ioctl(TIOCGSERIAL)");
180 return -errno;
181 }
182
183 ser_info.flags = ASYNC_SPD_CUST | ASYNC_LOW_LATENCY;
184 ser_info.custom_divisor = ser_info.baud_base / baudrate;
185
186 rc = ioctl(fd, TIOCSSERIAL, &ser_info);
187 if (rc < 0) {
188 dbg_perror("ioctl(TIOCSSERIAL)");
189 return -errno;
190 }
191
192 return _osmo_serial_set_baudrate(fd, B38400); /* 38400 is a kind of magic ... */
Sylvain Munaut96311842011-09-28 09:10:32 +0200193#elif defined(__APPLE__)
194#ifndef IOSSIOSPEED
195#define IOSSIOSPEED _IOW('T', 2, speed_t)
196#endif
197 int rc;
198
199 unsigned int speed = baudrate;
200 rc = ioctl(fd, IOSSIOSPEED, &speed);
201 if (rc < 0) {
Sylvain Munaute40549a2011-09-28 10:55:22 +0200202 dbg_perror("ioctl(IOSSIOSPEED)");
Sylvain Munaut96311842011-09-28 09:10:32 +0200203 return -errno;
204 }
205 return 0;
206#else
207#warning osmo_serial_set_custom_baudrate: unsupported platform
208 return 0;
209#endif
Sylvain Munautfe28ded2011-09-02 22:18:24 +0200210}
211
212/*! \brief Clear any custom baudrate
213 * \param[in] fd File descriptor of the open device
214 * \returns 0 for success or negative errno.
215 *
216 * This function might not work on all OS or with all type of serial adapters
217 */
218int
219osmo_serial_clear_custom_baudrate(int fd)
220{
Sylvain Munaut96311842011-09-28 09:10:32 +0200221#ifdef __linux__
Sylvain Munaut31d3de92011-11-20 08:55:11 +0100222 int rc;
Sylvain Munautfe28ded2011-09-02 22:18:24 +0200223 struct serial_struct ser_info;
224
225 rc = ioctl(fd, TIOCGSERIAL, &ser_info);
226 if (rc < 0) {
227 dbg_perror("ioctl(TIOCGSERIAL)");
228 return -errno;
229 }
230
231 ser_info.flags = ASYNC_LOW_LATENCY;
232 ser_info.custom_divisor = 0;
233
234 rc = ioctl(fd, TIOCSSERIAL, &ser_info);
235 if (rc < 0) {
236 dbg_perror("ioctl(TIOCSSERIAL)");
237 return -errno;
238 }
Sylvain Munaut96311842011-09-28 09:10:32 +0200239#endif
Sylvain Munautfe28ded2011-09-02 22:18:24 +0200240 return 0;
241}
242
Sylvain Munautdca7d2c2012-04-18 21:53:23 +0200243/*! @} */