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