Sylvain Munaut | fe28ded | 2011-09-02 22:18:24 +0200 | [diff] [blame] | 1 | /* |
| 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 | * @{ |
Harald Welte | 96e2a00 | 2017-06-12 21:44:18 +0200 | [diff] [blame] | 27 | * \brief Osmocom serial port helpers |
Sylvain Munaut | fe28ded | 2011-09-02 22:18:24 +0200 | [diff] [blame] | 28 | */ |
| 29 | |
Harald Welte | 96e2a00 | 2017-06-12 21:44:18 +0200 | [diff] [blame] | 30 | /*! \file serial.c */ |
Sylvain Munaut | fe28ded | 2011-09-02 22:18:24 +0200 | [diff] [blame] | 31 | |
| 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 Munaut | 9631184 | 2011-09-28 09:10:32 +0200 | [diff] [blame] | 40 | #ifdef __linux__ |
Sylvain Munaut | fe28ded | 2011-09-02 22:18:24 +0200 | [diff] [blame] | 41 | #include <linux/serial.h> |
Sylvain Munaut | 9631184 | 2011-09-28 09:10:32 +0200 | [diff] [blame] | 42 | #endif |
Sylvain Munaut | fe28ded | 2011-09-02 22:18:24 +0200 | [diff] [blame] | 43 | |
| 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 | |
| 53 | /*! \brief Open serial device and does base init |
| 54 | * \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 | */ |
| 58 | int |
| 59 | osmo_serial_init(const char *dev, speed_t baudrate) |
| 60 | { |
Harald Welte | 1514f34 | 2016-12-24 17:59:36 +0100 | [diff] [blame] | 61 | int rc, fd=-1, v24, flags; |
Sylvain Munaut | fe28ded | 2011-09-02 22:18:24 +0200 | [diff] [blame] | 62 | struct termios tio; |
| 63 | |
Harald Welte | 1db3782 | 2016-12-23 22:49:39 +0100 | [diff] [blame] | 64 | /* Use nonblock as the device might block otherwise */ |
| 65 | fd = open(dev, O_RDWR | O_NOCTTY | O_SYNC | O_NONBLOCK); |
Sylvain Munaut | fe28ded | 2011-09-02 22:18:24 +0200 | [diff] [blame] | 66 | if (fd < 0) { |
| 67 | dbg_perror("open"); |
| 68 | return -errno; |
| 69 | } |
| 70 | |
Harald Welte | 1db3782 | 2016-12-23 22:49:39 +0100 | [diff] [blame] | 71 | /* now put it into blcoking mode */ |
| 72 | flags = fcntl(fd, F_GETFL, 0); |
| 73 | if (flags < 0) { |
| 74 | dbg_perror("fcntl get flags"); |
Harald Welte | d251045 | 2016-12-24 17:58:13 +0100 | [diff] [blame] | 75 | rc = -errno; |
| 76 | goto error; |
Harald Welte | 1db3782 | 2016-12-23 22:49:39 +0100 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | flags &= ~O_NONBLOCK; |
| 80 | rc = fcntl(fd, F_SETFL, flags); |
| 81 | if (rc != 0) { |
| 82 | dbg_perror("fcntl set flags"); |
Harald Welte | d251045 | 2016-12-24 17:58:13 +0100 | [diff] [blame] | 83 | rc = -errno; |
| 84 | goto error; |
Harald Welte | 1db3782 | 2016-12-23 22:49:39 +0100 | [diff] [blame] | 85 | } |
| 86 | |
Sylvain Munaut | fe28ded | 2011-09-02 22:18:24 +0200 | [diff] [blame] | 87 | /* 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 Welte | c68ce3b | 2016-12-23 22:59:27 +0100 | [diff] [blame] | 117 | /* some serial porst don't support this, so let's not |
| 118 | * return an error here */ |
Sylvain Munaut | fe28ded | 2011-09-02 22:18:24 +0200 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | return fd; |
| 122 | |
| 123 | error: |
Harald Welte | 1514f34 | 2016-12-24 17:59:36 +0100 | [diff] [blame] | 124 | if (fd >= 0) |
Sylvain Munaut | fe28ded | 2011-09-02 22:18:24 +0200 | [diff] [blame] | 125 | close(fd); |
| 126 | return rc; |
| 127 | } |
| 128 | |
| 129 | static 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 | |
| 152 | /*! \brief Change current baudrate |
| 153 | * \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 | */ |
| 157 | int |
| 158 | osmo_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 | |
| 164 | /*! \brief Change current baudrate to a custom one using OS specific method |
| 165 | * \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 | */ |
| 171 | int |
| 172 | osmo_serial_set_custom_baudrate(int fd, int baudrate) |
| 173 | { |
Sylvain Munaut | 9631184 | 2011-09-28 09:10:32 +0200 | [diff] [blame] | 174 | #ifdef __linux__ |
Sylvain Munaut | fe28ded | 2011-09-02 22:18:24 +0200 | [diff] [blame] | 175 | 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 Munaut | 9631184 | 2011-09-28 09:10:32 +0200 | [diff] [blame] | 194 | #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 Munaut | e40549a | 2011-09-28 10:55:22 +0200 | [diff] [blame] | 203 | dbg_perror("ioctl(IOSSIOSPEED)"); |
Sylvain Munaut | 9631184 | 2011-09-28 09:10:32 +0200 | [diff] [blame] | 204 | return -errno; |
| 205 | } |
| 206 | return 0; |
| 207 | #else |
| 208 | #warning osmo_serial_set_custom_baudrate: unsupported platform |
| 209 | return 0; |
| 210 | #endif |
Sylvain Munaut | fe28ded | 2011-09-02 22:18:24 +0200 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | /*! \brief Clear any custom baudrate |
| 214 | * \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 | */ |
| 219 | int |
| 220 | osmo_serial_clear_custom_baudrate(int fd) |
| 221 | { |
Sylvain Munaut | 9631184 | 2011-09-28 09:10:32 +0200 | [diff] [blame] | 222 | #ifdef __linux__ |
Sylvain Munaut | 31d3de9 | 2011-11-20 08:55:11 +0100 | [diff] [blame] | 223 | int rc; |
Sylvain Munaut | fe28ded | 2011-09-02 22:18:24 +0200 | [diff] [blame] | 224 | 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 Munaut | 9631184 | 2011-09-28 09:10:32 +0200 | [diff] [blame] | 240 | #endif |
Sylvain Munaut | fe28ded | 2011-09-02 22:18:24 +0200 | [diff] [blame] | 241 | return 0; |
| 242 | } |
| 243 | |
Sylvain Munaut | dca7d2c | 2012-04-18 21:53:23 +0200 | [diff] [blame] | 244 | /*! @} */ |