Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 1 | /*! \file soft_uart.c |
| 2 | * Software UART implementation. */ |
| 3 | /* |
| 4 | * (C) 2022 by Harald Welte <laforge@gnumonks.org> |
Vadim Yanitskiy | c9fc77f | 2023-11-16 00:55:35 +0700 | [diff] [blame] | 5 | * (C) 2023 by sysmocom - s.f.m.c. GmbH <info@sysmocom.de> |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 6 | * |
| 7 | * All Rights Reserved |
| 8 | * |
| 9 | * SPDX-License-Identifier: GPL-2.0+ |
| 10 | * |
| 11 | * This program is free software; you can redistribute it and/or modify |
| 12 | * it under the terms of the GNU General Public License as published by |
| 13 | * the Free Software Foundation; either version 2 of the License, or |
| 14 | * (at your option) any later version. |
| 15 | * |
| 16 | * This program is distributed in the hope that it will be useful, |
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | * GNU General Public License for more details. |
| 20 | * |
| 21 | */ |
| 22 | |
| 23 | #include <stdbool.h> |
| 24 | #include <stdint.h> |
| 25 | #include <errno.h> |
| 26 | |
Vadim Yanitskiy | 459cb06 | 2023-11-21 20:12:36 +0700 | [diff] [blame] | 27 | #include <osmocom/core/utils.h> |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 28 | #include <osmocom/core/timer.h> |
| 29 | #include <osmocom/core/soft_uart.h> |
| 30 | |
Vadim Yanitskiy | 1a3b511 | 2023-11-16 00:41:03 +0700 | [diff] [blame] | 31 | /*! Rx/Tx flow state of a soft-UART */ |
| 32 | enum suart_flow_state { |
| 33 | SUART_FLOW_ST_IDLE, /*!< waiting for a start bit or Tx data */ |
| 34 | SUART_FLOW_ST_DATA, /*!< receiving/transmitting data bits */ |
| 35 | SUART_FLOW_ST_PARITY, /*!< receiving/transmitting parity bits */ |
| 36 | SUART_FLOW_ST_STOP, /*!< receiving/transmitting stop bits */ |
| 37 | }; |
| 38 | |
Vadim Yanitskiy | b72625d | 2023-11-16 00:32:33 +0700 | [diff] [blame] | 39 | /*! Internal state of a soft-UART */ |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 40 | struct osmo_soft_uart { |
| 41 | struct osmo_soft_uart_cfg cfg; |
| 42 | const char *name; |
Vadim Yanitskiy | 6587dd0 | 2023-11-30 03:04:32 +0700 | [diff] [blame] | 43 | /* modem status (bitmask of OSMO_SUART_STATUS_F_*) */ |
| 44 | unsigned int status; |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 45 | struct { |
| 46 | bool running; |
| 47 | uint8_t bit_count; |
| 48 | uint8_t shift_reg; |
| 49 | struct msgb *msg; |
Vadim Yanitskiy | 1c2b8c1 | 2023-11-16 00:44:42 +0700 | [diff] [blame] | 50 | ubit_t parity_bit; /* 0 (even) / 1 (odd) */ |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 51 | unsigned int flags; |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 52 | struct osmo_timer_list timer; |
Vadim Yanitskiy | 1a3b511 | 2023-11-16 00:41:03 +0700 | [diff] [blame] | 53 | enum suart_flow_state flow_state; |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 54 | } rx; |
| 55 | struct { |
| 56 | bool running; |
| 57 | uint8_t bit_count; |
| 58 | uint8_t shift_reg; |
Vadim Yanitskiy | c9fc77f | 2023-11-16 00:55:35 +0700 | [diff] [blame] | 59 | ubit_t parity_bit; /* 0 (even) / 1 (odd) */ |
| 60 | enum suart_flow_state flow_state; |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 61 | } tx; |
| 62 | }; |
| 63 | |
Vadim Yanitskiy | b72625d | 2023-11-16 00:32:33 +0700 | [diff] [blame] | 64 | /*! Default soft-UART configuration (8-N-1) */ |
Vadim Yanitskiy | 82a1ae7 | 2023-11-16 15:34:50 +0700 | [diff] [blame] | 65 | const struct osmo_soft_uart_cfg osmo_soft_uart_default_cfg = { |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 66 | .num_data_bits = 8, |
| 67 | .num_stop_bits = 1, |
| 68 | .parity_mode = OSMO_SUART_PARITY_NONE, |
| 69 | .rx_buf_size = 1024, |
| 70 | .rx_timeout_ms = 100, |
Vadim Yanitskiy | 6587dd0 | 2023-11-30 03:04:32 +0700 | [diff] [blame] | 71 | .flow_ctrl_mode = OSMO_SUART_FLOW_CTRL_NONE, |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 72 | }; |
| 73 | |
| 74 | /************************************************************************* |
| 75 | * Receiver |
| 76 | *************************************************************************/ |
| 77 | |
Vadim Yanitskiy | 03f0ed7 | 2023-11-18 00:29:08 +0700 | [diff] [blame] | 78 | /*! Flush the receive buffer, passing ownership of the msgb to the .rx_cb(). |
| 79 | * \param[in] suart soft-UART instance holding the receive buffer. */ |
| 80 | void osmo_soft_uart_flush_rx(struct osmo_soft_uart *suart) |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 81 | { |
Vadim Yanitskiy | d76cc37 | 2023-12-09 04:38:14 +0700 | [diff] [blame] | 82 | if (suart->rx.msg && msgb_length(suart->rx.msg)) { |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 83 | osmo_timer_del(&suart->rx.timer); |
| 84 | if (suart->cfg.rx_cb) { |
| 85 | suart->cfg.rx_cb(suart->cfg.priv, suart->rx.msg, suart->rx.flags); |
| 86 | /* call-back has taken ownership of msgb, no need to free() here */ |
Vadim Yanitskiy | c9fc77f | 2023-11-16 00:55:35 +0700 | [diff] [blame] | 87 | suart->rx.msg = msgb_alloc_c(suart, suart->cfg.rx_buf_size, "soft_uart_rx"); |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 88 | } else { |
| 89 | msgb_reset(suart->rx.msg); |
| 90 | } |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | /* one character was received; add to receive buffer and notify user, if needed */ |
| 95 | static void suart_rx_ch(struct osmo_soft_uart *suart, uint8_t ch) |
| 96 | { |
| 97 | unsigned int msg_len; |
| 98 | |
| 99 | OSMO_ASSERT(suart->rx.msg); |
| 100 | msgb_put_u8(suart->rx.msg, ch); |
| 101 | msg_len = msgb_length(suart->rx.msg); |
| 102 | |
Vadim Yanitskiy | bf95f82 | 2023-11-18 01:46:16 +0700 | [diff] [blame] | 103 | if (msg_len >= suart->cfg.rx_buf_size || suart->rx.flags) { |
| 104 | /* either the buffer is full, or we hit a parity and/or a framing error */ |
| 105 | osmo_soft_uart_flush_rx(suart); |
| 106 | } else if (msg_len == 1) { |
| 107 | /* first character in new message: start timer */ |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 108 | osmo_timer_schedule(&suart->rx.timer, suart->cfg.rx_timeout_ms / 1000, |
| 109 | (suart->cfg.rx_timeout_ms % 1000) * 1000); |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 110 | } |
| 111 | } |
| 112 | |
| 113 | /* receive a single bit */ |
Vadim Yanitskiy | 2203db5 | 2023-12-04 02:59:28 +0700 | [diff] [blame] | 114 | static inline void suart_rx_bit(struct osmo_soft_uart *suart, const ubit_t bit) |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 115 | { |
Vadim Yanitskiy | 1a3b511 | 2023-11-16 00:41:03 +0700 | [diff] [blame] | 116 | switch (suart->rx.flow_state) { |
| 117 | case SUART_FLOW_ST_IDLE: |
| 118 | if (bit == 0) { /* start bit condition */ |
| 119 | suart->rx.flow_state = SUART_FLOW_ST_DATA; |
| 120 | suart->rx.flags = 0x00; |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 121 | suart->rx.shift_reg = 0; |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 122 | suart->rx.bit_count = 0; |
Vadim Yanitskiy | 1c2b8c1 | 2023-11-16 00:44:42 +0700 | [diff] [blame] | 123 | suart->rx.parity_bit = 0; |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 124 | } |
Vadim Yanitskiy | 1a3b511 | 2023-11-16 00:41:03 +0700 | [diff] [blame] | 125 | break; |
| 126 | case SUART_FLOW_ST_DATA: |
| 127 | suart->rx.bit_count++; |
| 128 | suart->rx.shift_reg >>= 1; |
Vadim Yanitskiy | 1c2b8c1 | 2023-11-16 00:44:42 +0700 | [diff] [blame] | 129 | if (bit != 0) { |
| 130 | suart->rx.parity_bit = !suart->rx.parity_bit; /* flip */ |
Vadim Yanitskiy | 1a3b511 | 2023-11-16 00:41:03 +0700 | [diff] [blame] | 131 | suart->rx.shift_reg |= 0x80; |
Vadim Yanitskiy | 1c2b8c1 | 2023-11-16 00:44:42 +0700 | [diff] [blame] | 132 | } |
Vadim Yanitskiy | 1a3b511 | 2023-11-16 00:41:03 +0700 | [diff] [blame] | 133 | if (suart->rx.bit_count >= suart->cfg.num_data_bits) { |
| 134 | /* we have accumulated enough data bits */ |
| 135 | if (suart->cfg.parity_mode != OSMO_SUART_PARITY_NONE) |
| 136 | suart->rx.flow_state = SUART_FLOW_ST_PARITY; |
| 137 | else |
| 138 | suart->rx.flow_state = SUART_FLOW_ST_STOP; |
Vadim Yanitskiy | 2d2ce49 | 2023-11-19 15:37:10 +0700 | [diff] [blame] | 139 | /* align the register if needed */ |
| 140 | if (suart->cfg.num_data_bits < 8) |
| 141 | suart->rx.shift_reg >>= (8 - suart->cfg.num_data_bits); |
Vadim Yanitskiy | 1a3b511 | 2023-11-16 00:41:03 +0700 | [diff] [blame] | 142 | } |
| 143 | break; |
| 144 | case SUART_FLOW_ST_PARITY: |
Vadim Yanitskiy | 1c2b8c1 | 2023-11-16 00:44:42 +0700 | [diff] [blame] | 145 | switch (suart->cfg.parity_mode) { |
| 146 | case OSMO_SUART_PARITY_EVEN: |
| 147 | /* number of 1-bits (in both data and parity) shall be even */ |
| 148 | if (suart->rx.parity_bit != bit) |
| 149 | suart->rx.flags |= OSMO_SUART_F_PARITY_ERROR; |
| 150 | break; |
| 151 | case OSMO_SUART_PARITY_ODD: |
| 152 | /* number of 1-bits (in both data and parity) shall be odd */ |
| 153 | if (suart->rx.parity_bit == bit) |
| 154 | suart->rx.flags |= OSMO_SUART_F_PARITY_ERROR; |
| 155 | break; |
Vadim Yanitskiy | 0d78a00 | 2023-11-19 16:15:38 +0700 | [diff] [blame] | 156 | case OSMO_SUART_PARITY_MARK: |
| 157 | /* parity bit must always be 1 */ |
| 158 | if (bit != 1) |
| 159 | suart->rx.flags |= OSMO_SUART_F_PARITY_ERROR; |
| 160 | break; |
| 161 | case OSMO_SUART_PARITY_SPACE: |
| 162 | /* parity bit must always be 0 */ |
| 163 | if (bit != 0) |
| 164 | suart->rx.flags |= OSMO_SUART_F_PARITY_ERROR; |
| 165 | break; |
Vadim Yanitskiy | 1c2b8c1 | 2023-11-16 00:44:42 +0700 | [diff] [blame] | 166 | case OSMO_SUART_PARITY_NONE: /* shall not happen */ |
| 167 | default: |
| 168 | OSMO_ASSERT(0); |
| 169 | } |
| 170 | |
Vadim Yanitskiy | 1a3b511 | 2023-11-16 00:41:03 +0700 | [diff] [blame] | 171 | suart->rx.flow_state = SUART_FLOW_ST_STOP; |
| 172 | break; |
| 173 | case SUART_FLOW_ST_STOP: |
| 174 | suart->rx.bit_count++; |
| 175 | if (bit != 1) |
| 176 | suart->rx.flags |= OSMO_SUART_F_FRAMING_ERROR; |
| 177 | |
| 178 | if (suart->rx.bit_count >= (suart->cfg.num_data_bits + suart->cfg.num_stop_bits)) { |
| 179 | /* we have accumulated enough stop bits */ |
| 180 | suart_rx_ch(suart, suart->rx.shift_reg); |
| 181 | suart->rx.flow_state = SUART_FLOW_ST_IDLE; |
| 182 | } |
| 183 | break; |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 184 | } |
| 185 | } |
| 186 | |
| 187 | /* receive timer expiration: flush rx-buffer to user call-back */ |
| 188 | static void suart_rx_timer_cb(void *data) |
| 189 | { |
| 190 | struct osmo_soft_uart *suart = data; |
Vadim Yanitskiy | 03f0ed7 | 2023-11-18 00:29:08 +0700 | [diff] [blame] | 191 | osmo_soft_uart_flush_rx(suart); |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 192 | } |
| 193 | |
Vadim Yanitskiy | b72625d | 2023-11-16 00:32:33 +0700 | [diff] [blame] | 194 | /*! Feed a number of unpacked bits into the soft-UART receiver. |
| 195 | * \param[in] suart soft-UART instance to feed bits into. |
| 196 | * \param[in] ubits pointer to the unpacked bits. |
| 197 | * \param[in] n_ubits number of unpacked bits to be fed. |
Vadim Yanitskiy | 811638c | 2023-11-30 03:09:05 +0700 | [diff] [blame] | 198 | * \returns 0 on success; negative on error. |
| 199 | * -EAGAIN indicates that the receiver is disabled. */ |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 200 | int osmo_soft_uart_rx_ubits(struct osmo_soft_uart *suart, const ubit_t *ubits, size_t n_ubits) |
| 201 | { |
Vadim Yanitskiy | 811638c | 2023-11-30 03:09:05 +0700 | [diff] [blame] | 202 | if (!suart->rx.running) |
| 203 | return -EAGAIN; |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 204 | for (size_t i = 0; i < n_ubits; i++) |
Vadim Yanitskiy | 2203db5 | 2023-12-04 02:59:28 +0700 | [diff] [blame] | 205 | suart_rx_bit(suart, ubits[i]); |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 206 | return 0; |
| 207 | } |
| 208 | |
| 209 | /************************************************************************* |
| 210 | * Transmitter |
| 211 | *************************************************************************/ |
| 212 | |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 213 | /* pull a single bit out of the UART transmitter */ |
Vadim Yanitskiy | 2203db5 | 2023-12-04 02:59:28 +0700 | [diff] [blame] | 214 | static inline ubit_t suart_tx_bit(struct osmo_soft_uart *suart, struct msgb *msg) |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 215 | { |
Vadim Yanitskiy | c9fc77f | 2023-11-16 00:55:35 +0700 | [diff] [blame] | 216 | ubit_t tx_bit = 1; |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 217 | |
Vadim Yanitskiy | c9fc77f | 2023-11-16 00:55:35 +0700 | [diff] [blame] | 218 | switch (suart->tx.flow_state) { |
| 219 | case SUART_FLOW_ST_IDLE: |
Vadim Yanitskiy | ffdd997 | 2023-11-21 02:25:32 +0700 | [diff] [blame] | 220 | if (msg && msgb_length(msg) > 0) { /* if we have pending data */ |
Vadim Yanitskiy | c9fc77f | 2023-11-16 00:55:35 +0700 | [diff] [blame] | 221 | suart->tx.shift_reg = msgb_pull_u8(msg); |
| 222 | suart->tx.flow_state = SUART_FLOW_ST_DATA; |
| 223 | suart->tx.bit_count = 0; |
| 224 | suart->tx.parity_bit = 0; |
| 225 | tx_bit = 0; |
| 226 | } |
| 227 | break; |
| 228 | case SUART_FLOW_ST_DATA: |
| 229 | tx_bit = suart->tx.shift_reg & 1; |
| 230 | suart->tx.parity_bit ^= tx_bit; |
| 231 | suart->tx.shift_reg >>= 1; |
| 232 | suart->tx.bit_count++; |
| 233 | if (suart->tx.bit_count >= suart->cfg.num_data_bits) { |
| 234 | /* we have transmitted all data bits */ |
| 235 | if (suart->cfg.parity_mode != OSMO_SUART_PARITY_NONE) |
| 236 | suart->tx.flow_state = SUART_FLOW_ST_PARITY; |
| 237 | else |
| 238 | suart->tx.flow_state = SUART_FLOW_ST_STOP; |
| 239 | } |
| 240 | break; |
| 241 | case SUART_FLOW_ST_PARITY: |
| 242 | switch (suart->cfg.parity_mode) { |
| 243 | case OSMO_SUART_PARITY_EVEN: |
| 244 | /* number of 1-bits (in both data and parity) shall be even */ |
| 245 | tx_bit = suart->tx.parity_bit; |
| 246 | break; |
| 247 | case OSMO_SUART_PARITY_ODD: |
| 248 | /* number of 1-bits (in both data and parity) shall be odd */ |
| 249 | tx_bit = !suart->tx.parity_bit; |
| 250 | break; |
Vadim Yanitskiy | 0d78a00 | 2023-11-19 16:15:38 +0700 | [diff] [blame] | 251 | case OSMO_SUART_PARITY_MARK: |
| 252 | /* parity bit must always be 1 */ |
| 253 | tx_bit = 1; |
| 254 | break; |
| 255 | case OSMO_SUART_PARITY_SPACE: |
| 256 | /* parity bit must always be 0 */ |
| 257 | tx_bit = 0; |
| 258 | break; |
Vadim Yanitskiy | c9fc77f | 2023-11-16 00:55:35 +0700 | [diff] [blame] | 259 | case OSMO_SUART_PARITY_NONE: |
| 260 | default: /* shall not happen */ |
| 261 | OSMO_ASSERT(0); |
| 262 | } |
| 263 | |
| 264 | suart->tx.flow_state = SUART_FLOW_ST_STOP; |
| 265 | break; |
| 266 | case SUART_FLOW_ST_STOP: |
| 267 | suart->tx.bit_count++; |
| 268 | if (suart->tx.bit_count >= (suart->cfg.num_data_bits + suart->cfg.num_stop_bits)) { |
| 269 | /* we have transmitted all stop bits, we're done */ |
| 270 | suart->tx.flow_state = SUART_FLOW_ST_IDLE; |
| 271 | } |
| 272 | break; |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 273 | } |
Vadim Yanitskiy | c9fc77f | 2023-11-16 00:55:35 +0700 | [diff] [blame] | 274 | |
| 275 | return tx_bit; |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 276 | } |
| 277 | |
Vadim Yanitskiy | 6587dd0 | 2023-11-30 03:04:32 +0700 | [diff] [blame] | 278 | /* pull pending bits out of the UART */ |
| 279 | static size_t suart_tx_pending(struct osmo_soft_uart *suart, ubit_t *ubits, size_t n_ubits) |
| 280 | { |
| 281 | size_t i; |
| 282 | |
| 283 | for (i = 0; i < n_ubits; i++) { |
| 284 | if (suart->tx.flow_state == SUART_FLOW_ST_IDLE) |
| 285 | break; |
| 286 | ubits[i] = suart_tx_bit(suart, NULL); |
| 287 | } |
| 288 | |
| 289 | return i; |
| 290 | } |
| 291 | |
Vadim Yanitskiy | b72625d | 2023-11-16 00:32:33 +0700 | [diff] [blame] | 292 | /*! Pull a number of unpacked bits out of the soft-UART transmitter. |
| 293 | * \param[in] suart soft-UART instance to pull the bits from. |
| 294 | * \param[out] ubits pointer to a buffer where to store pulled bits. |
| 295 | * \param[in] n_ubits number of unpacked bits to be pulled. |
Vadim Yanitskiy | 6587dd0 | 2023-11-30 03:04:32 +0700 | [diff] [blame] | 296 | * \returns number of bits pulled (may be less than n_ubits); negative on error. |
Vadim Yanitskiy | 811638c | 2023-11-30 03:09:05 +0700 | [diff] [blame] | 297 | * -EAGAIN indicates that the transmitter is disabled. */ |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 298 | int osmo_soft_uart_tx_ubits(struct osmo_soft_uart *suart, ubit_t *ubits, size_t n_ubits) |
| 299 | { |
Vadim Yanitskiy | c9fc77f | 2023-11-16 00:55:35 +0700 | [diff] [blame] | 300 | const struct osmo_soft_uart_cfg *cfg = &suart->cfg; |
Vadim Yanitskiy | ffdd997 | 2023-11-21 02:25:32 +0700 | [diff] [blame] | 301 | size_t n_frame_bits, n_chars; |
| 302 | struct msgb *msg = NULL; |
Vadim Yanitskiy | c9fc77f | 2023-11-16 00:55:35 +0700 | [diff] [blame] | 303 | |
Vadim Yanitskiy | 459cb06 | 2023-11-21 20:12:36 +0700 | [diff] [blame] | 304 | if (OSMO_UNLIKELY(n_ubits == 0)) |
| 305 | return -EINVAL; |
| 306 | |
Vadim Yanitskiy | 811638c | 2023-11-30 03:09:05 +0700 | [diff] [blame] | 307 | if (!suart->tx.running) |
| 308 | return -EAGAIN; |
| 309 | |
Vadim Yanitskiy | 6587dd0 | 2023-11-30 03:04:32 +0700 | [diff] [blame] | 310 | switch (suart->cfg.flow_ctrl_mode) { |
| 311 | case OSMO_SUART_FLOW_CTRL_DTR_DSR: |
| 312 | /* if DSR is de-asserted, Tx pending bits and suspend */ |
| 313 | if (~suart->status & OSMO_SUART_STATUS_F_DSR) |
| 314 | return suart_tx_pending(suart, ubits, n_ubits); |
| 315 | /* else: keep transmitting as usual */ |
| 316 | break; |
| 317 | case OSMO_SUART_FLOW_CTRL_RTS_CTS: |
| 318 | /* if CTS is de-asserted, Tx pending bits and suspend */ |
| 319 | if (~suart->status & OSMO_SUART_STATUS_F_CTS) |
| 320 | return suart_tx_pending(suart, ubits, n_ubits); |
| 321 | /* else: keep transmitting as usual */ |
| 322 | break; |
| 323 | case OSMO_SUART_FLOW_CTRL_NONE: |
| 324 | default: |
| 325 | break; |
| 326 | } |
| 327 | |
Vadim Yanitskiy | c9fc77f | 2023-11-16 00:55:35 +0700 | [diff] [blame] | 328 | /* calculate UART frame size for the effective config */ |
| 329 | n_frame_bits = 1 + cfg->num_data_bits + cfg->num_stop_bits; |
| 330 | if (cfg->parity_mode != OSMO_SUART_PARITY_NONE) |
| 331 | n_frame_bits += 1; |
| 332 | |
Vadim Yanitskiy | ffdd997 | 2023-11-21 02:25:32 +0700 | [diff] [blame] | 333 | /* calculate the number of characters we can fit into n_ubits */ |
| 334 | n_chars = n_ubits / n_frame_bits; |
| 335 | if (n_chars == 0) { |
| 336 | /* we can transmit at least one character */ |
| 337 | if (suart->tx.flow_state == SUART_FLOW_ST_IDLE) |
| 338 | n_chars = 1; |
| 339 | } |
Vadim Yanitskiy | c9fc77f | 2023-11-16 00:55:35 +0700 | [diff] [blame] | 340 | |
Vadim Yanitskiy | ffdd997 | 2023-11-21 02:25:32 +0700 | [diff] [blame] | 341 | if (n_chars > 0) { |
| 342 | /* allocate a Tx buffer msgb */ |
| 343 | msg = msgb_alloc_c(suart, n_chars, "soft_uart_tx"); |
| 344 | OSMO_ASSERT(msg != NULL); |
| 345 | |
| 346 | /* call the .tx_cb() to populate the Tx buffer */ |
| 347 | OSMO_ASSERT(cfg->tx_cb != NULL); |
| 348 | suart->cfg.tx_cb(cfg->priv, msg); |
| 349 | } |
Vadim Yanitskiy | c9fc77f | 2023-11-16 00:55:35 +0700 | [diff] [blame] | 350 | |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 351 | for (size_t i = 0; i < n_ubits; i++) |
Vadim Yanitskiy | 2203db5 | 2023-12-04 02:59:28 +0700 | [diff] [blame] | 352 | ubits[i] = suart_tx_bit(suart, msg); |
Vadim Yanitskiy | c9fc77f | 2023-11-16 00:55:35 +0700 | [diff] [blame] | 353 | msgb_free(msg); |
| 354 | |
Vadim Yanitskiy | ffb8d5e | 2023-12-04 03:17:26 +0700 | [diff] [blame] | 355 | return n_ubits; |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 356 | } |
| 357 | |
Vadim Yanitskiy | 6587dd0 | 2023-11-30 03:04:32 +0700 | [diff] [blame] | 358 | /*! Get the modem status bitmask of the given soft-UART. |
| 359 | * \param[in] suart soft-UART instance to get the modem status. |
| 360 | * \returns bitmask of OSMO_SUART_STATUS_F_*. */ |
| 361 | unsigned int osmo_soft_uart_get_status(const struct osmo_soft_uart *suart) |
| 362 | { |
| 363 | return suart->status; |
| 364 | } |
| 365 | |
| 366 | /*! Set the modem status bitmask of the given soft-UART. |
| 367 | * \param[in] suart soft-UART instance to set the modem status. |
| 368 | * \param[in] status bitmask of OSMO_SUART_STATUS_F_*. |
Vadim Yanitskiy | b72625d | 2023-11-16 00:32:33 +0700 | [diff] [blame] | 369 | * \returns 0 on success; negative on error. */ |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 370 | int osmo_soft_uart_set_status(struct osmo_soft_uart *suart, unsigned int status) |
| 371 | { |
Vadim Yanitskiy | 6587dd0 | 2023-11-30 03:04:32 +0700 | [diff] [blame] | 372 | const struct osmo_soft_uart_cfg *cfg = &suart->cfg; |
| 373 | |
| 374 | if (cfg->status_change_cb != NULL) { |
| 375 | if (suart->status != status) |
| 376 | cfg->status_change_cb(cfg->priv, status); |
| 377 | } |
| 378 | |
| 379 | suart->status = status; |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 380 | return 0; |
| 381 | } |
| 382 | |
Vadim Yanitskiy | 6587dd0 | 2023-11-30 03:04:32 +0700 | [diff] [blame] | 383 | /*! Activate/deactivate a modem status line of the given soft-UART. |
| 384 | * \param[in] suart soft-UART instance to update the modem status. |
| 385 | * \param[in] line a modem status line, one of OSMO_SUART_STATUS_F_*. |
| 386 | * \param[in] active activate (true) or deactivate (false) the line. */ |
| 387 | void osmo_soft_uart_set_status_line(struct osmo_soft_uart *suart, |
| 388 | enum osmo_soft_uart_status line, |
| 389 | bool active) |
| 390 | { |
| 391 | unsigned int status = suart->status; |
| 392 | |
| 393 | if (active) /* assert the given line */ |
| 394 | status |= line; |
| 395 | else /* de-assert the given line */ |
| 396 | status &= ~line; |
| 397 | |
| 398 | osmo_soft_uart_set_status(suart, status); |
| 399 | } |
| 400 | |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 401 | |
| 402 | /************************************************************************* |
| 403 | * Management / Initialization |
| 404 | *************************************************************************/ |
| 405 | |
Vadim Yanitskiy | b72625d | 2023-11-16 00:32:33 +0700 | [diff] [blame] | 406 | /*! Allocate a soft-UART instance. |
| 407 | * \param[in] ctx parent talloc context. |
| 408 | * \param[in] name name of the soft-UART instance. |
Vadim Yanitskiy | 82a1ae7 | 2023-11-16 15:34:50 +0700 | [diff] [blame] | 409 | * \param[in] cfg initial configuration of the soft-UART instance. |
Vadim Yanitskiy | b72625d | 2023-11-16 00:32:33 +0700 | [diff] [blame] | 410 | * \returns pointer to allocated soft-UART instance; NULL on error. */ |
Vadim Yanitskiy | 82a1ae7 | 2023-11-16 15:34:50 +0700 | [diff] [blame] | 411 | struct osmo_soft_uart *osmo_soft_uart_alloc(void *ctx, const char *name, |
| 412 | const struct osmo_soft_uart_cfg *cfg) |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 413 | { |
| 414 | struct osmo_soft_uart *suart = talloc_zero(ctx, struct osmo_soft_uart); |
| 415 | if (!suart) |
| 416 | return NULL; |
| 417 | suart->name = talloc_strdup(suart, name); |
Vadim Yanitskiy | 82a1ae7 | 2023-11-16 15:34:50 +0700 | [diff] [blame] | 418 | |
| 419 | OSMO_ASSERT(cfg != NULL); |
| 420 | suart->cfg = *cfg; |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 421 | |
| 422 | return suart; |
| 423 | } |
| 424 | |
Vadim Yanitskiy | 877cfed | 2023-11-12 17:21:31 +0700 | [diff] [blame] | 425 | /*! Release memory taken by the given soft-UART. |
| 426 | * \param[in] suart soft-UART instance to be free()d. */ |
| 427 | void osmo_soft_uart_free(struct osmo_soft_uart *suart) |
| 428 | { |
| 429 | if (suart == NULL) |
| 430 | return; |
| 431 | |
| 432 | osmo_timer_del(&suart->rx.timer); |
| 433 | msgb_free(suart->rx.msg); |
| 434 | |
| 435 | talloc_free((void *)suart->name); |
| 436 | talloc_free(suart); |
| 437 | } |
| 438 | |
Vadim Yanitskiy | b72625d | 2023-11-16 00:32:33 +0700 | [diff] [blame] | 439 | /*! Change soft-UART configuration to the user-provided config. |
| 440 | * \param[in] suart soft-UART instance to be re-configured. |
| 441 | * \param[in] cfg the user-provided config to be applied. |
| 442 | * \returns 0 on success; negative on error. */ |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 443 | int osmo_soft_uart_configure(struct osmo_soft_uart *suart, const struct osmo_soft_uart_cfg *cfg) |
| 444 | { |
| 445 | /* consistency checks on the configuration */ |
| 446 | if (cfg->num_data_bits > 8 || cfg->num_data_bits == 0) |
| 447 | return -EINVAL; |
| 448 | if (cfg->num_stop_bits == 0) |
| 449 | return -EINVAL; |
| 450 | if (cfg->parity_mode < 0 || cfg->parity_mode >= _OSMO_SUART_PARITY_NUM) |
| 451 | return -EINVAL; |
| 452 | if (cfg->rx_buf_size == 0) |
| 453 | return -EINVAL; |
| 454 | |
| 455 | if (suart->cfg.rx_buf_size > cfg->rx_buf_size || |
| 456 | suart->cfg.rx_timeout_ms > cfg->rx_timeout_ms) { |
Vadim Yanitskiy | 03f0ed7 | 2023-11-18 00:29:08 +0700 | [diff] [blame] | 457 | osmo_soft_uart_flush_rx(suart); |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 458 | } |
| 459 | |
| 460 | suart->cfg = *cfg; |
| 461 | |
| 462 | osmo_timer_setup(&suart->rx.timer, suart_rx_timer_cb, suart); |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 463 | |
| 464 | return 0; |
| 465 | } |
| 466 | |
Vadim Yanitskiy | 59afb8f | 2023-11-29 23:58:10 +0700 | [diff] [blame] | 467 | /*! Get a name for the given soft-UART instance. |
| 468 | * \param[in] suart soft-UART instance to get the name from. |
| 469 | * \returns name of the given soft-UART instance. */ |
| 470 | const char *osmo_soft_uart_get_name(const struct osmo_soft_uart *suart) |
| 471 | { |
| 472 | return suart->name; |
| 473 | } |
| 474 | |
| 475 | /*! Set a new name for the given soft-UART instance. |
| 476 | * \param[in] suart soft-UART instance to set the name for. |
| 477 | * \param[in] name the new name. */ |
| 478 | void osmo_soft_uart_set_name(struct osmo_soft_uart *suart, const char *name) |
| 479 | { |
| 480 | osmo_talloc_replace_string(suart, (char **)&suart->name, name); |
| 481 | } |
| 482 | |
Vadim Yanitskiy | cdde671 | 2023-11-16 15:17:37 +0700 | [diff] [blame] | 483 | /*! Enable/disable receiver of the given soft-UART. |
Vadim Yanitskiy | b72625d | 2023-11-16 00:32:33 +0700 | [diff] [blame] | 484 | * \param[in] suart soft-UART instance to be re-configured. |
Vadim Yanitskiy | cdde671 | 2023-11-16 15:17:37 +0700 | [diff] [blame] | 485 | * \param[in] enable enable/disable state of the receiver. |
Vadim Yanitskiy | b72625d | 2023-11-16 00:32:33 +0700 | [diff] [blame] | 486 | * \returns 0 on success; negative on error. */ |
Vadim Yanitskiy | cdde671 | 2023-11-16 15:17:37 +0700 | [diff] [blame] | 487 | int osmo_soft_uart_set_rx(struct osmo_soft_uart *suart, bool enable) |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 488 | { |
Vadim Yanitskiy | cdde671 | 2023-11-16 15:17:37 +0700 | [diff] [blame] | 489 | if (!enable && suart->rx.running) { |
Vadim Yanitskiy | 03f0ed7 | 2023-11-18 00:29:08 +0700 | [diff] [blame] | 490 | osmo_soft_uart_flush_rx(suart); |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 491 | suart->rx.running = false; |
Vadim Yanitskiy | 1a3b511 | 2023-11-16 00:41:03 +0700 | [diff] [blame] | 492 | suart->rx.flow_state = SUART_FLOW_ST_IDLE; |
Vadim Yanitskiy | cdde671 | 2023-11-16 15:17:37 +0700 | [diff] [blame] | 493 | } else if (enable && !suart->rx.running) { |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 494 | if (!suart->rx.msg) |
Vadim Yanitskiy | 26dff79 | 2023-12-09 04:08:24 +0700 | [diff] [blame] | 495 | suart->rx.msg = msgb_alloc_c(suart, suart->cfg.rx_buf_size, "soft_uart_rx"); |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 496 | suart->rx.running = true; |
Vadim Yanitskiy | 1a3b511 | 2023-11-16 00:41:03 +0700 | [diff] [blame] | 497 | suart->rx.flow_state = SUART_FLOW_ST_IDLE; |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 498 | } |
| 499 | |
Vadim Yanitskiy | cdde671 | 2023-11-16 15:17:37 +0700 | [diff] [blame] | 500 | return 0; |
| 501 | } |
| 502 | |
| 503 | /*! Enable/disable transmitter of the given soft-UART. |
| 504 | * \param[in] suart soft-UART instance to be re-configured. |
| 505 | * \param[in] enable enable/disable state of the transmitter. |
| 506 | * \returns 0 on success; negative on error. */ |
| 507 | int osmo_soft_uart_set_tx(struct osmo_soft_uart *suart, bool enable) |
| 508 | { |
| 509 | if (!enable && suart->tx.running) { |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 510 | suart->tx.running = false; |
Vadim Yanitskiy | c9fc77f | 2023-11-16 00:55:35 +0700 | [diff] [blame] | 511 | suart->tx.flow_state = SUART_FLOW_ST_IDLE; |
Vadim Yanitskiy | cdde671 | 2023-11-16 15:17:37 +0700 | [diff] [blame] | 512 | } else if (enable && !suart->tx.running) { |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 513 | suart->tx.running = true; |
Vadim Yanitskiy | c9fc77f | 2023-11-16 00:55:35 +0700 | [diff] [blame] | 514 | suart->tx.flow_state = SUART_FLOW_ST_IDLE; |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 515 | } |
| 516 | |
| 517 | return 0; |
| 518 | } |