Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 1 | /* Serial communications layer, based on HDLC */ |
| 2 | |
Harald Welte | cc95f4b | 2017-04-30 21:39:33 +0200 | [diff] [blame] | 3 | /* (C) 2010,2017 by Harald Welte <laforge@gnumonks.org> |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 4 | * |
| 5 | * All Rights Reserved |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License along |
| 18 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 19 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | * |
| 21 | */ |
| 22 | |
Harald Welte | 7711713 | 2017-05-15 17:33:02 +0200 | [diff] [blame] | 23 | /*! \addtogroup sercomm |
| 24 | * @{ |
| 25 | */ |
| 26 | |
| 27 | /*! \file sercomm.c |
| 28 | */ |
| 29 | |
Harald Welte | 799bef5 | 2017-05-15 17:16:48 +0200 | [diff] [blame] | 30 | #include "config.h" |
| 31 | |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 32 | #include <stdint.h> |
| 33 | #include <stdio.h> |
| 34 | #include <errno.h> |
| 35 | |
| 36 | #include <osmocom/core/msgb.h> |
Harald Welte | df8e89f | 2017-05-14 21:46:08 +0200 | [diff] [blame] | 37 | #include <osmocom/core/utils.h> |
Harald Welte | 1a97e2c | 2017-05-01 00:19:38 +0200 | [diff] [blame] | 38 | #include <osmocom/core/sercomm.h> |
Harald Welte | df8e89f | 2017-05-14 21:46:08 +0200 | [diff] [blame] | 39 | #include <osmocom/core/linuxlist.h> |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 40 | |
Harald Welte | 7af6d22 | 2017-05-14 21:48:41 +0200 | [diff] [blame] | 41 | #ifndef EMBEDDED |
Harald Welte | f6adcd7 | 2017-05-01 00:19:13 +0200 | [diff] [blame] | 42 | # define DEFAULT_RX_MSG_SIZE 2048 |
Harald Welte | 7711713 | 2017-05-15 17:33:02 +0200 | [diff] [blame] | 43 | /*! \brief Protect against IRQ context */ |
Harald Welte | 799bef5 | 2017-05-15 17:16:48 +0200 | [diff] [blame] | 44 | void sercomm_drv_lock(unsigned long __attribute__((unused)) *flags) {} |
Harald Welte | 7711713 | 2017-05-15 17:33:02 +0200 | [diff] [blame] | 45 | /*! \brief Release protection against IRQ context */ |
Harald Welte | 799bef5 | 2017-05-15 17:16:48 +0200 | [diff] [blame] | 46 | void sercomm_drv_unlock(unsigned long __attribute__((unused)) *flags) {} |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 47 | #else |
Harald Welte | f6adcd7 | 2017-05-01 00:19:13 +0200 | [diff] [blame] | 48 | # define DEFAULT_RX_MSG_SIZE 256 |
Harald Welte | 799bef5 | 2017-05-15 17:16:48 +0200 | [diff] [blame] | 49 | #endif /* EMBEDDED */ |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 50 | |
Harald Welte | 799bef5 | 2017-05-15 17:16:48 +0200 | [diff] [blame] | 51 | /* weak symbols to be overridden by application */ |
| 52 | __attribute__((weak)) void sercomm_drv_start_tx(struct osmo_sercomm_inst *sercomm) {}; |
| 53 | __attribute__((weak)) int sercomm_drv_baudrate_chg(struct osmo_sercomm_inst *sercomm, uint32_t bdrt) |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 54 | { |
Harald Welte | 799bef5 | 2017-05-15 17:16:48 +0200 | [diff] [blame] | 55 | return -1; |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 56 | } |
| 57 | |
Harald Welte | 8a4eb83 | 2017-05-02 21:41:36 +0200 | [diff] [blame] | 58 | #define HDLC_FLAG 0x7E |
| 59 | #define HDLC_ESCAPE 0x7D |
| 60 | |
| 61 | #define HDLC_C_UI 0x03 |
| 62 | #define HDLC_C_P_BIT (1 << 4) |
| 63 | #define HDLC_C_F_BIT (1 << 4) |
| 64 | |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 65 | enum rx_state { |
| 66 | RX_ST_WAIT_START, |
| 67 | RX_ST_ADDR, |
| 68 | RX_ST_CTRL, |
| 69 | RX_ST_DATA, |
| 70 | RX_ST_ESCAPE, |
| 71 | }; |
| 72 | |
Harald Welte | 7711713 | 2017-05-15 17:33:02 +0200 | [diff] [blame] | 73 | /*! \brief Initialize an Osmocom sercomm instance |
| 74 | * \param sercomm Caller-allocated sercomm instance to be initialized |
| 75 | * |
| 76 | * This function initializes the sercomm instance, including the |
| 77 | * registration of the ECHO service at the ECHO DLCI |
| 78 | */ |
Harald Welte | 1358836 | 2017-04-30 23:57:55 +0200 | [diff] [blame] | 79 | void osmo_sercomm_init(struct osmo_sercomm_inst *sercomm) |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 80 | { |
| 81 | unsigned int i; |
Harald Welte | cc95f4b | 2017-04-30 21:39:33 +0200 | [diff] [blame] | 82 | for (i = 0; i < ARRAY_SIZE(sercomm->tx.dlci_queues); i++) |
| 83 | INIT_LLIST_HEAD(&sercomm->tx.dlci_queues[i]); |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 84 | |
Harald Welte | cc95f4b | 2017-04-30 21:39:33 +0200 | [diff] [blame] | 85 | sercomm->rx.msg = NULL; |
Harald Welte | f6adcd7 | 2017-05-01 00:19:13 +0200 | [diff] [blame] | 86 | if (!sercomm->rx.msg_size) |
| 87 | sercomm->rx.msg_size = DEFAULT_RX_MSG_SIZE; |
Harald Welte | cc95f4b | 2017-04-30 21:39:33 +0200 | [diff] [blame] | 88 | sercomm->initialized = 1; |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 89 | |
| 90 | /* set up the echo dlci */ |
Harald Welte | 1358836 | 2017-04-30 23:57:55 +0200 | [diff] [blame] | 91 | osmo_sercomm_register_rx_cb(sercomm, SC_DLCI_ECHO, &osmo_sercomm_sendmsg); |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 92 | } |
| 93 | |
Harald Welte | 7711713 | 2017-05-15 17:33:02 +0200 | [diff] [blame] | 94 | /*! \brief Determine if a given Osmocom sercomm instance has been initialized |
| 95 | * \param[in] sercomm Osmocom sercomm instance to be checked |
| 96 | * \returns 1 in case \a sercomm was previously initialized; 0 otherwise */ |
Harald Welte | 1358836 | 2017-04-30 23:57:55 +0200 | [diff] [blame] | 97 | int osmo_sercomm_initialized(struct osmo_sercomm_inst *sercomm) |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 98 | { |
Harald Welte | cc95f4b | 2017-04-30 21:39:33 +0200 | [diff] [blame] | 99 | return sercomm->initialized; |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 100 | } |
| 101 | |
Harald Welte | 7711713 | 2017-05-15 17:33:02 +0200 | [diff] [blame] | 102 | /*! \brief User interface for transmitting messages for a given DLCI |
| 103 | * \param[in] sercomm Osmocom sercomm instance through which to transmit |
| 104 | * \param[in] dlci DLCI through whcih to transmit \a msg |
| 105 | * \param[in] msg Message buffer to be transmitted via \a dlci on \a * sercomm |
| 106 | **/ |
Harald Welte | 1358836 | 2017-04-30 23:57:55 +0200 | [diff] [blame] | 107 | void osmo_sercomm_sendmsg(struct osmo_sercomm_inst *sercomm, uint8_t dlci, struct msgb *msg) |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 108 | { |
| 109 | unsigned long flags; |
| 110 | uint8_t *hdr; |
| 111 | |
| 112 | /* prepend address + control octet */ |
| 113 | hdr = msgb_push(msg, 2); |
| 114 | hdr[0] = dlci; |
| 115 | hdr[1] = HDLC_C_UI; |
| 116 | |
| 117 | /* This functiion can be called from any context: FIQ, IRQ |
| 118 | * and supervisor context. Proper locking is important! */ |
Harald Welte | df8e89f | 2017-05-14 21:46:08 +0200 | [diff] [blame] | 119 | sercomm_drv_lock(&flags); |
Harald Welte | cc95f4b | 2017-04-30 21:39:33 +0200 | [diff] [blame] | 120 | msgb_enqueue(&sercomm->tx.dlci_queues[dlci], msg); |
Harald Welte | df8e89f | 2017-05-14 21:46:08 +0200 | [diff] [blame] | 121 | sercomm_drv_unlock(&flags); |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 122 | |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 123 | /* tell UART that we have something to send */ |
Harald Welte | 799bef5 | 2017-05-15 17:16:48 +0200 | [diff] [blame] | 124 | sercomm_drv_start_tx(sercomm); |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 125 | } |
| 126 | |
Harald Welte | 7711713 | 2017-05-15 17:33:02 +0200 | [diff] [blame] | 127 | /*! \brief How deep is the Tx queue for a given DLCI? |
| 128 | * \param[n] sercomm Osmocom sercomm instance on which to operate |
| 129 | * \param[in] dlci DLCI whose queue depthy is to be determined |
| 130 | * \returns number of elements in the per-DLCI transmit queue */ |
Harald Welte | 1358836 | 2017-04-30 23:57:55 +0200 | [diff] [blame] | 131 | unsigned int osmo_sercomm_tx_queue_depth(struct osmo_sercomm_inst *sercomm, uint8_t dlci) |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 132 | { |
| 133 | struct llist_head *le; |
| 134 | unsigned int num = 0; |
| 135 | |
Harald Welte | cc95f4b | 2017-04-30 21:39:33 +0200 | [diff] [blame] | 136 | llist_for_each(le, &sercomm->tx.dlci_queues[dlci]) { |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 137 | num++; |
| 138 | } |
| 139 | |
| 140 | return num; |
| 141 | } |
| 142 | |
Harald Welte | 7711713 | 2017-05-15 17:33:02 +0200 | [diff] [blame] | 143 | /*! \brief wait until everything has been transmitted, then grab the lock and |
| 144 | * change the baud rate as requested |
| 145 | * \param[in] sercomm Osmocom sercomm instance |
| 146 | * \param[in] bdrt New UART Baud Rate |
| 147 | * \returns result of the operation as provided by sercomm_drv_baudrate_chg() |
| 148 | */ |
Harald Welte | 799bef5 | 2017-05-15 17:16:48 +0200 | [diff] [blame] | 149 | int osmo_sercomm_change_speed(struct osmo_sercomm_inst *sercomm, uint32_t bdrt) |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 150 | { |
| 151 | unsigned int i, count; |
| 152 | unsigned long flags; |
| 153 | |
| 154 | while (1) { |
| 155 | /* count the number of pending messages */ |
| 156 | count = 0; |
Harald Welte | cc95f4b | 2017-04-30 21:39:33 +0200 | [diff] [blame] | 157 | for (i = 0; i < ARRAY_SIZE(sercomm->tx.dlci_queues); i++) |
Harald Welte | 799bef5 | 2017-05-15 17:16:48 +0200 | [diff] [blame] | 158 | count += osmo_sercomm_tx_queue_depth(sercomm, i); |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 159 | /* if we still have any in the queue, restart */ |
| 160 | if (count == 0) |
| 161 | break; |
| 162 | } |
| 163 | |
| 164 | while (1) { |
| 165 | /* no messages in the queue, grab the lock to ensure it |
| 166 | * stays that way */ |
Harald Welte | df8e89f | 2017-05-14 21:46:08 +0200 | [diff] [blame] | 167 | sercomm_drv_lock(&flags); |
Harald Welte | cc95f4b | 2017-04-30 21:39:33 +0200 | [diff] [blame] | 168 | if (!sercomm->tx.msg && !sercomm->tx.next_char) { |
Harald Welte | 799bef5 | 2017-05-15 17:16:48 +0200 | [diff] [blame] | 169 | int rc; |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 170 | /* change speed */ |
Harald Welte | 799bef5 | 2017-05-15 17:16:48 +0200 | [diff] [blame] | 171 | rc = sercomm_drv_baudrate_chg(sercomm, bdrt); |
Harald Welte | df8e89f | 2017-05-14 21:46:08 +0200 | [diff] [blame] | 172 | sercomm_drv_unlock(&flags); |
Harald Welte | 799bef5 | 2017-05-15 17:16:48 +0200 | [diff] [blame] | 173 | return rc; |
| 174 | } else |
Harald Welte | df8e89f | 2017-05-14 21:46:08 +0200 | [diff] [blame] | 175 | sercomm_drv_unlock(&flags); |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 176 | } |
Harald Welte | 799bef5 | 2017-05-15 17:16:48 +0200 | [diff] [blame] | 177 | return -1; |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 178 | } |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 179 | |
Harald Welte | ea3d3ba | 2017-05-02 21:24:48 +0200 | [diff] [blame] | 180 | /*! \brief fetch one octet of to-be-transmitted serial data |
| 181 | * \param[in] sercomm Sercomm Instance from which to fetch pending data |
| 182 | * \param[out] ch pointer to caller-allocaed output memory |
| 183 | * \returns 1 in case of succss; 0 if no data available; negative on error */ |
Harald Welte | 1358836 | 2017-04-30 23:57:55 +0200 | [diff] [blame] | 184 | int osmo_sercomm_drv_pull(struct osmo_sercomm_inst *sercomm, uint8_t *ch) |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 185 | { |
| 186 | unsigned long flags; |
| 187 | |
| 188 | /* we may be called from interrupt context, but we stiff need to lock |
| 189 | * because sercomm could be accessed from a FIQ context ... */ |
| 190 | |
Harald Welte | df8e89f | 2017-05-14 21:46:08 +0200 | [diff] [blame] | 191 | sercomm_drv_lock(&flags); |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 192 | |
Harald Welte | cc95f4b | 2017-04-30 21:39:33 +0200 | [diff] [blame] | 193 | if (!sercomm->tx.msg) { |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 194 | unsigned int i; |
| 195 | /* dequeue a new message from the queues */ |
Harald Welte | cc95f4b | 2017-04-30 21:39:33 +0200 | [diff] [blame] | 196 | for (i = 0; i < ARRAY_SIZE(sercomm->tx.dlci_queues); i++) { |
| 197 | sercomm->tx.msg = msgb_dequeue(&sercomm->tx.dlci_queues[i]); |
| 198 | if (sercomm->tx.msg) |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 199 | break; |
| 200 | } |
Harald Welte | cc95f4b | 2017-04-30 21:39:33 +0200 | [diff] [blame] | 201 | if (sercomm->tx.msg) { |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 202 | /* start of a new message, send start flag octet */ |
| 203 | *ch = HDLC_FLAG; |
Harald Welte | cc95f4b | 2017-04-30 21:39:33 +0200 | [diff] [blame] | 204 | sercomm->tx.next_char = sercomm->tx.msg->data; |
Harald Welte | df8e89f | 2017-05-14 21:46:08 +0200 | [diff] [blame] | 205 | sercomm_drv_unlock(&flags); |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 206 | return 1; |
| 207 | } else { |
| 208 | /* no more data avilable */ |
Harald Welte | df8e89f | 2017-05-14 21:46:08 +0200 | [diff] [blame] | 209 | sercomm_drv_unlock(&flags); |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 210 | return 0; |
| 211 | } |
| 212 | } |
| 213 | |
Harald Welte | cc95f4b | 2017-04-30 21:39:33 +0200 | [diff] [blame] | 214 | if (sercomm->tx.state == RX_ST_ESCAPE) { |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 215 | /* we've already transmitted the ESCAPE octet, |
| 216 | * we now need to transmit the escaped data */ |
Harald Welte | cc95f4b | 2017-04-30 21:39:33 +0200 | [diff] [blame] | 217 | *ch = *sercomm->tx.next_char++; |
| 218 | sercomm->tx.state = RX_ST_DATA; |
| 219 | } else if (sercomm->tx.next_char >= sercomm->tx.msg->tail) { |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 220 | /* last character has already been transmitted, |
| 221 | * send end-of-message octet */ |
| 222 | *ch = HDLC_FLAG; |
| 223 | /* we've reached the end of the message buffer */ |
Harald Welte | cc95f4b | 2017-04-30 21:39:33 +0200 | [diff] [blame] | 224 | msgb_free(sercomm->tx.msg); |
| 225 | sercomm->tx.msg = NULL; |
| 226 | sercomm->tx.next_char = NULL; |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 227 | /* escaping for the two control octets */ |
Harald Welte | cc95f4b | 2017-04-30 21:39:33 +0200 | [diff] [blame] | 228 | } else if (*sercomm->tx.next_char == HDLC_FLAG || |
| 229 | *sercomm->tx.next_char == HDLC_ESCAPE || |
| 230 | *sercomm->tx.next_char == 0x00) { |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 231 | /* send an escape octet */ |
| 232 | *ch = HDLC_ESCAPE; |
| 233 | /* invert bit 5 of the next octet to be sent */ |
Harald Welte | cc95f4b | 2017-04-30 21:39:33 +0200 | [diff] [blame] | 234 | *sercomm->tx.next_char ^= (1 << 5); |
| 235 | sercomm->tx.state = RX_ST_ESCAPE; |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 236 | } else { |
| 237 | /* standard case, simply send next octet */ |
Harald Welte | cc95f4b | 2017-04-30 21:39:33 +0200 | [diff] [blame] | 238 | *ch = *sercomm->tx.next_char++; |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 239 | } |
| 240 | |
Harald Welte | df8e89f | 2017-05-14 21:46:08 +0200 | [diff] [blame] | 241 | sercomm_drv_unlock(&flags); |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 242 | return 1; |
| 243 | } |
| 244 | |
Harald Welte | ea3d3ba | 2017-05-02 21:24:48 +0200 | [diff] [blame] | 245 | /*! \brief Register a handler for a given DLCI |
| 246 | * \param sercomm Sercomm Instance in which caller wishes to register |
| 247 | * \param[in] dlci Data Ling Connection Identifier to register |
| 248 | * \param[in] cb Callback function for \a dlci |
| 249 | * \returns 0 on success; negative on error */ |
Harald Welte | 1358836 | 2017-04-30 23:57:55 +0200 | [diff] [blame] | 250 | int osmo_sercomm_register_rx_cb(struct osmo_sercomm_inst *sercomm, uint8_t dlci, dlci_cb_t cb) |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 251 | { |
Harald Welte | cc95f4b | 2017-04-30 21:39:33 +0200 | [diff] [blame] | 252 | if (dlci >= ARRAY_SIZE(sercomm->rx.dlci_handler)) |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 253 | return -EINVAL; |
| 254 | |
Harald Welte | cc95f4b | 2017-04-30 21:39:33 +0200 | [diff] [blame] | 255 | if (sercomm->rx.dlci_handler[dlci]) |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 256 | return -EBUSY; |
| 257 | |
Harald Welte | cc95f4b | 2017-04-30 21:39:33 +0200 | [diff] [blame] | 258 | sercomm->rx.dlci_handler[dlci] = cb; |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 259 | return 0; |
| 260 | } |
| 261 | |
| 262 | /* dispatch an incoming message once it is completely received */ |
Harald Welte | 1358836 | 2017-04-30 23:57:55 +0200 | [diff] [blame] | 263 | static void dispatch_rx_msg(struct osmo_sercomm_inst *sercomm, uint8_t dlci, struct msgb *msg) |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 264 | { |
Harald Welte | cc95f4b | 2017-04-30 21:39:33 +0200 | [diff] [blame] | 265 | if (dlci >= ARRAY_SIZE(sercomm->rx.dlci_handler) || |
| 266 | !sercomm->rx.dlci_handler[dlci]) { |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 267 | msgb_free(msg); |
| 268 | return; |
| 269 | } |
Harald Welte | cc95f4b | 2017-04-30 21:39:33 +0200 | [diff] [blame] | 270 | sercomm->rx.dlci_handler[dlci](sercomm, dlci, msg); |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 271 | } |
| 272 | |
Harald Welte | ea3d3ba | 2017-05-02 21:24:48 +0200 | [diff] [blame] | 273 | /*! \brief the driver has received one byte, pass it into sercomm layer |
| 274 | * \param[in] sercomm Sercomm Instance for which a byte was received |
| 275 | * \param[in] ch byte that was received from line for said instance |
| 276 | * \returns 1 on success; 0 on unrecognized char; negative on error */ |
Harald Welte | 1358836 | 2017-04-30 23:57:55 +0200 | [diff] [blame] | 277 | int osmo_sercomm_drv_rx_char(struct osmo_sercomm_inst *sercomm, uint8_t ch) |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 278 | { |
| 279 | uint8_t *ptr; |
| 280 | |
| 281 | /* we are always called from interrupt context in this function, |
| 282 | * which means that any data structures we use need to be for |
| 283 | * our exclusive access */ |
Harald Welte | cc95f4b | 2017-04-30 21:39:33 +0200 | [diff] [blame] | 284 | if (!sercomm->rx.msg) |
Harald Welte | f6adcd7 | 2017-05-01 00:19:13 +0200 | [diff] [blame] | 285 | sercomm->rx.msg = osmo_sercomm_alloc_msgb(sercomm->rx.msg_size); |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 286 | |
Harald Welte | cc95f4b | 2017-04-30 21:39:33 +0200 | [diff] [blame] | 287 | if (msgb_tailroom(sercomm->rx.msg) == 0) { |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 288 | //cons_puts("sercomm_drv_rx_char() overflow!\n"); |
Harald Welte | cc95f4b | 2017-04-30 21:39:33 +0200 | [diff] [blame] | 289 | msgb_free(sercomm->rx.msg); |
Harald Welte | f6adcd7 | 2017-05-01 00:19:13 +0200 | [diff] [blame] | 290 | sercomm->rx.msg = osmo_sercomm_alloc_msgb(sercomm->rx.msg_size); |
Harald Welte | cc95f4b | 2017-04-30 21:39:33 +0200 | [diff] [blame] | 291 | sercomm->rx.state = RX_ST_WAIT_START; |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 292 | return 0; |
| 293 | } |
| 294 | |
Harald Welte | cc95f4b | 2017-04-30 21:39:33 +0200 | [diff] [blame] | 295 | switch (sercomm->rx.state) { |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 296 | case RX_ST_WAIT_START: |
| 297 | if (ch != HDLC_FLAG) |
| 298 | break; |
Harald Welte | cc95f4b | 2017-04-30 21:39:33 +0200 | [diff] [blame] | 299 | sercomm->rx.state = RX_ST_ADDR; |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 300 | break; |
| 301 | case RX_ST_ADDR: |
Harald Welte | cc95f4b | 2017-04-30 21:39:33 +0200 | [diff] [blame] | 302 | sercomm->rx.dlci = ch; |
| 303 | sercomm->rx.state = RX_ST_CTRL; |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 304 | break; |
| 305 | case RX_ST_CTRL: |
Harald Welte | cc95f4b | 2017-04-30 21:39:33 +0200 | [diff] [blame] | 306 | sercomm->rx.ctrl = ch; |
| 307 | sercomm->rx.state = RX_ST_DATA; |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 308 | break; |
| 309 | case RX_ST_DATA: |
| 310 | if (ch == HDLC_ESCAPE) { |
| 311 | /* drop the escape octet, but change state */ |
Harald Welte | cc95f4b | 2017-04-30 21:39:33 +0200 | [diff] [blame] | 312 | sercomm->rx.state = RX_ST_ESCAPE; |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 313 | break; |
| 314 | } else if (ch == HDLC_FLAG) { |
| 315 | /* message is finished */ |
Harald Welte | cc95f4b | 2017-04-30 21:39:33 +0200 | [diff] [blame] | 316 | dispatch_rx_msg(sercomm, sercomm->rx.dlci, sercomm->rx.msg); |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 317 | /* allocate new buffer */ |
Harald Welte | cc95f4b | 2017-04-30 21:39:33 +0200 | [diff] [blame] | 318 | sercomm->rx.msg = NULL; |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 319 | /* start all over again */ |
Harald Welte | cc95f4b | 2017-04-30 21:39:33 +0200 | [diff] [blame] | 320 | sercomm->rx.state = RX_ST_WAIT_START; |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 321 | |
| 322 | /* do not add the control char */ |
| 323 | break; |
| 324 | } |
| 325 | /* default case: store the octet */ |
Harald Welte | cc95f4b | 2017-04-30 21:39:33 +0200 | [diff] [blame] | 326 | ptr = msgb_put(sercomm->rx.msg, 1); |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 327 | *ptr = ch; |
| 328 | break; |
| 329 | case RX_ST_ESCAPE: |
| 330 | /* store bif-5-inverted octet in buffer */ |
| 331 | ch ^= (1 << 5); |
Harald Welte | cc95f4b | 2017-04-30 21:39:33 +0200 | [diff] [blame] | 332 | ptr = msgb_put(sercomm->rx.msg, 1); |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 333 | *ptr = ch; |
| 334 | /* transition back to normal DATA state */ |
Harald Welte | cc95f4b | 2017-04-30 21:39:33 +0200 | [diff] [blame] | 335 | sercomm->rx.state = RX_ST_DATA; |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 336 | break; |
| 337 | } |
| 338 | |
| 339 | return 1; |
| 340 | } |
Harald Welte | 7711713 | 2017-05-15 17:33:02 +0200 | [diff] [blame] | 341 | |
| 342 | /*! @} */ |